-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
223 lines (203 loc) · 8.02 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Earth</title>
<style>
html, body, div{
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div>
<canvas id='glpaper'></canvas>
</div>
<script id="single-vs" type="shader">
uniform mat4 u_worldViewProjection;
uniform vec3 u_lightWorldPos;
uniform mat4 u_world;
uniform mat4 u_viewInverse;
uniform mat4 u_worldInverseTranspose;
attribute vec4 position;
attribute vec3 normal;
attribute vec2 texcoord;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
void main() {
v_texCoord = texcoord;
v_position = u_worldViewProjection * position;
v_normal = (u_worldInverseTranspose * vec4(normal, 0)).xyz;
v_surfaceToLight = u_lightWorldPos - (u_world * position).xyz;
v_surfaceToView = (u_viewInverse[3] - (u_world * position)).xyz;
gl_Position = v_position;
}
</script>
<script id="single-fs" type="shader">
precision mediump float;
varying vec4 v_position;
varying vec2 v_texCoord;
varying vec3 v_normal;
varying vec3 v_surfaceToLight;
varying vec3 v_surfaceToView;
uniform vec4 u_lightColor;
uniform vec4 u_ambient;
uniform sampler2D u_diffuse;
uniform vec4 u_specular;
uniform float u_shininess;
uniform float u_specularFactor;
vec4 lit(float l ,float h, float m) {
return vec4(1.0,
max(l, 0.0),
(l > 0.0) ? pow(max(0.0, h), m) : 0.0,
1.0);
}
void main() {
vec4 diffuseColor = texture2D(u_diffuse, v_texCoord);
vec3 a_normal = normalize(v_normal);
vec3 surfaceToLight = normalize(v_surfaceToLight);
vec3 surfaceToView = normalize(v_surfaceToView);
vec3 halfVector = normalize(surfaceToLight + surfaceToView);
vec4 litR = lit(dot(a_normal, surfaceToLight),
dot(a_normal, halfVector), u_shininess);
vec4 outColor = vec4((
u_lightColor * (diffuseColor * litR.y + diffuseColor * u_ambient +
u_specular * litR.z * u_specularFactor)).rgb,
diffuseColor.a);
gl_FragColor = outColor;
}
</script>
<script id="texture-vs" type="shader">
precision mediump float;
attribute vec2 position;
attribute vec3 color;
varying vec3 v_color;
void main() {
v_color = color;
vec2 newPos = position / vec2(180.0, -90.0);
gl_Position = vec4(newPos, 0.0, 1.0);
}
</script>
<script id="texture-fs" type="shader">
precision mediump float;
varying vec3 v_color;
void main() {
gl_FragColor = vec4(v_color, 1.0);
}
</script>
<script src="./chroma.min.js"></script>
<script src="./earcut.min.js"></script>
<script src="./gl-matrix-min.js"></script>
<script src="./gl.js"></script>
<script src="./primitive.js"></script>
<script>
var renderGreyTex, renderColorTex, renderMultiColor, renderLineTex, imgTex;
PGGL.geojsonProvider('world.json').then(function(vdata){
PGGL.getWebglContext('glpaper');
renderGreyTex = PGGL.getRenderTexture(vdata, true, {color: [0.4, 0.4, 0.4]});
renderColorTex = PGGL.getRenderTexture(vdata, false);
renderMultiColor = PGGL.getRenderTexture(vdata, false,{multiColor: true});
renderLineTex = PGGL.getRenderTexture(vdata, false, {drawMode: PGGL.gl.LINES});
imgTex = PGGL.getImgTexture('worldmap.jpg', false, renderGreyTex);
var textures = [renderGreyTex, renderColorTex, renderMultiColor, renderLineTex, imgTex];
var next = 1;
document.getElementById('glpaper')
.addEventListener('click', function() {
PGGL.gl.bindTexture(PGGL.gl.TEXTURE_2D, textures[next++]);
next = next >= textures.length ? 0 : next;
});
renderSphere();
});
var addClickEvent = function() {
var el = document.getElementById('glpaper');
if (el.addEventListener) {
el.addEventListener('click', onCanvesClick(), false);
}
else {
el.attachEvent('onclick', onCanvesClick());
}
}
var onCanvesClick = function() {
var next = imgTex;
PGGL.gl.bindTexture(PGGL.gl.TEXTURE_2D, next);
next = next === renderTex ? imgTex : renderTex;
}
var renderSphere = function () {
var program = PGGL.createProgram('single-vs', 'single-fs');
var sphere = PGGL.createSphereVertices(1, 60, 30);
var uniformsInfo = {
u_lightWorldPos: [1, 6, -10],
u_lightColor: [1, 1, 1, 1],
u_ambient: [0.2, 0.2, 0.2, 1],
u_specular: [1, 1, 1, 1],
u_shininess: 500,
u_specularFactor: 2,
};
var eye = [1, 2, -3];
var target = [0, 0, 0];
var up = [0, 1, 0];
var projMatrix = new Float32Array(16);
var viewMatrix = new Float32Array(16);
var worldMatrix = new Float32Array(16);
var identityMatrix = new Float32Array(16);
var u_ViewInverse = new Float32Array(16);
var u_world = new Float32Array(16);
var u_worldInverseTranspose = new Float32Array(16);
var u_worldViewProjection = new Float32Array(16);
var programInfo = PGGL.createProgramInfo(program, [
{
property: 'position',
bufferData: sphere.position,
components: 3
},
{
property: 'normal',
bufferData: sphere.normal,
components: 3
},
{
property: 'texcoord',
bufferData: sphere.texcoord,
components: 2
}
], sphere.indices);
function render(time) {
time *= 0.0002;
mat4.identity(identityMatrix);
mat4.identity(worldMatrix);
mat4.lookAt(viewMatrix, eye, target, up);
mat4.perspective(projMatrix, glMatrix.toRadian(45), PGGL.gl.canvas.clientWidth / PGGL.gl.canvas.clientHeight, 0.1, 1000);
mat4.invert(u_ViewInverse, viewMatrix);
mat4.rotate(worldMatrix, identityMatrix, time, [0, 1, 0]);
mat4.invert(u_worldInverseTranspose, worldMatrix)
mat4.transpose(u_worldInverseTranspose, u_worldInverseTranspose);
mat4.mul(u_worldViewProjection, projMatrix, viewMatrix);
mat4.mul(u_worldViewProjection, u_worldViewProjection, worldMatrix);
Object.assign(uniformsInfo, {
u_ViewInverse: u_ViewInverse,
u_world: worldMatrix,
u_worldInverseTranspose: u_worldInverseTranspose,
u_worldViewProjection: u_worldViewProjection
});
programInfo.uniformsInfo = uniformsInfo;
PGGL.draw(programInfo);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
</script>
</body>
</html>