-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertexAnim.js
419 lines (355 loc) · 14.2 KB
/
vertexAnim.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const DEGREE_TO_RADIUS = Math.PI / 180;
const CAMERA_POSITION = vec3.fromValues(0.0, 0.0, 10.0);
const PLANE_WIDTH = 3;
const PLANE_HEIGHT = 3;
var mPlaneTexture = null;
var mVertices = [];
var mTexCoods = [];
var mViewportWidth = 0;
var mViewportHeight = 0;
var mPitching = 0.0;
var mYawing = 0.5;
var mAngle = 0.0;
var mScale = 1.0;
var mProjectionMatrix = mat4.create();
var mModelMatrix = mat4.create();
var mViewMatrix = mat4.create();
var mProgram = null;
var mContinuous = true;
var mThen = 0;
var mBuffers = null;
var mGl = null;
function main() {
const canvas = document.querySelector("#glcanvas");
// Initialize the GL context
mGl = canvas.getContext("webgl") || canvas.getContext('experimental-webgl');
// Only continue if WebGL is available and working
if (!mGl) {
alert("Unable to initialize WebGL. Your browser or machine may not support it.");
return;
}
mViewportWidth = canvas.clientWidth;
mViewportHeight = canvas.clientHeight;
mGl.viewport(0, 0, mViewportWidth, mViewportHeight);
// create view matrix
mViewMatrix = mat4.create();
mat4.lookAt(mViewMatrix, CAMERA_POSITION, vec3.fromValues(0.0, 0.0, 0.0), vec3.fromValues(0.0, 1.0, 0.0));
// Create a perspective matrix
const fov = 45 * DEGREE_TO_RADIUS; // in radians
const aspect = mGl.canvas.clientWidth / mGl.canvas.clientHeight;
const zNear = 0.1;
const zFar = 1000.0;
// note: glmatrix.js always has the first argument
// as the destination to receive the result.
mProjectionMatrix = mat4.create();
mat4.perspective(mProjectionMatrix, fov, aspect, zNear, zFar);
mPlaneTexture = loadTexture(mGl, './texture/ming.jpg');
// init shader
updateShader();
mBuffers = initBuffers(mGl);
requestAnimationFrame(render);
}
// Draw the scene repeatedly
function render(now) {
now *= 0.001; // convert to seconds
const deltaTime = now - mThen;
mThen = now;
// draw scene
drawScene(mGl, mProgram, mBuffers, deltaTime);
if (mContinuous)
requestAnimationFrame(render);
}
function updateShader() {
// Vertex shader program
const vsSource = document.getElementById('id_vertex_shader').value;
// Fragment shader program
const fsSource = document.getElementById('id_fragment_shader').value;
// Initialize a shader program
const vertexShader = loadShader(mGl, mGl.VERTEX_SHADER, vsSource);
const fragmentShader = loadShader(mGl, mGl.FRAGMENT_SHADER, fsSource);
// Create the shader program
const shaderProgram = mGl.createProgram();
mGl.attachShader(shaderProgram, vertexShader);
mGl.attachShader(shaderProgram, fragmentShader);
mGl.linkProgram(shaderProgram);
// If creating the shader program failed, alert
if (!mGl.getProgramParameter(shaderProgram, mGl.LINK_STATUS)) {
alert('Unable to initialize the shader program: ' + mGl.getProgramInfoLog(shaderProgram));
return null;
}
// Collect all the info needed to use the shader program
mProgram = {
program: shaderProgram,
attribLocations: {
vertexPosition: mGl.getAttribLocation(shaderProgram, 'aPosition'),
textureCoord: mGl.getAttribLocation(shaderProgram, 'aTexCoord')
},
uniformLocations: {
uProjectionMatrixHandle: mGl.getUniformLocation(shaderProgram, 'uProjectionMatrix'),
uModelMatrixHandle: mGl.getUniformLocation(shaderProgram, 'uModelMatrix'),
uViewMatrixHandle: mGl.getUniformLocation(shaderProgram, 'uViewMatrix'),
uWidthSpanHandle: mGl.getUniformLocation(shaderProgram, 'uWidthSpan'),
uHeightSpanHandle: mGl.getUniformLocation(shaderProgram, 'uHeightSpan'),
uAngleHandle: mGl.getUniformLocation(shaderProgram, 'uAngle'),
uTexSamplerHandle: mGl.getUniformLocation(shaderProgram, 'uTexSampler')
},
};
}
// creates a shader of the given type, uploads the source and compiles it.
function loadShader(mGl, type, source) {
const shader = mGl.createShader(type);
// Send the source to the shader object
mGl.shaderSource(shader, source);
// Compile the shader program
mGl.compileShader(shader);
// See if it compiled successfully
if (!mGl.getShaderParameter(shader, mGl.COMPILE_STATUS)) {
alert('An error occurred compiling the shaders: ' + mGl.getShaderInfoLog(shader));
mGl.deleteShader(shader);
return null;
}
return shader;
}
function initBuffers(gl) {
// Now create an array of positions for the plane
const rows = 12;
const cols = 12;
mVertices = createPlaneVertices(4, 3, rows, cols);
mTexCoods = generateTexCoord(rows, cols);
// Create a buffer for the sphere's positions.
const positionBuffer = gl.createBuffer();
// Select the positionBuffer as the one to apply buffer operations to from here out.
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(mVertices), gl.STATIC_DRAW);
// Create a buffer for the viewFrustum's color.
const uvBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(mTexCoods), gl.STATIC_DRAW);
return {
position: positionBuffer,
uv: uvBuffer
};
}
// Initialize a texture and load an image.
// When the image finished loading copy it into the texture.
function loadTexture(gl, url) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
// Because images have to be download over the internet
// they might take a moment until they are ready.
// Until then put a single pixel in the texture so we can
// use it immediately. When the image has finished downloading
// we'll update the texture with the contents of the image.
const level = 0;
const internalFormat = gl.RGBA;
const width = 1;
const height = 1;
const border = 0;
const srcFormat = gl.RGBA;
const srcType = gl.UNSIGNED_BYTE;
const pixel = new Uint8Array([255, 255, 255, 255]);
// 1表示翻转,0表示不翻转,参考 https://juejin.im/post/5d4423c4f265da038f47ef87
// gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
width, height, border, srcFormat, srcType,
pixel);
const image = new Image();
image.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat,
srcFormat, srcType, image);
// WebGL1 has different requirements for power of 2 images
// vs non power of 2 images so check if the image is a
// power of 2 in both dimensions.
if (isPowerOf2(image.width) && isPowerOf2(image.height)) {
// Yes, it's a power of 2. Generate mips.
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.MIRRORED_REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.generateMipmap(gl.TEXTURE_2D);
} else {
// No, it's not a power of 2. Turn off mips and set
// wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.MIRRORED_REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
}
};
image.src = url;
return texture;
}
function isPowerOf2(value) {
return (value & (value - 1)) == 0;
}
function createPlaneVertices(width, height, rows, cols) {
const widthSize = width / rows;
const heightSize = height / cols;
// const vCnt = cols * rows * 6;
var vertices = [];
var count = 0;
for (var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var zsx = -widthSize * cols / 2 + i * widthSize;
var zsy = heightSize * rows / 2 - j * heightSize;
var zsz = 0;
vertices[count++] = zsx;
vertices[count++] = zsy;
vertices[count++] = zsz;
vertices[count++] = zsx;
vertices[count++] = zsy - heightSize;
vertices[count++] = zsz;
vertices[count++] = zsx + widthSize;
vertices[count++] = zsy;
vertices[count++] = zsz;
vertices[count++] = zsx + widthSize;
vertices[count++] = zsy;
vertices[count++] = zsz;
vertices[count++] = zsx;
vertices[count++] = zsy - heightSize;
vertices[count++] = zsz;
vertices[count++] = zsx + widthSize;
vertices[count++] = zsy - heightSize;
vertices[count++] = zsz;
}
}
return vertices;
}
function generateTexCoord(rows, cols) {
var result = [];
var sizeW = 1.0 / rows;
var sizeH = 1.0 / cols;
var index = 0;
for(var i = 0; i < cols; i++) {
for(var j = 0; j < rows; j++) {
var s = j * sizeW;
var t = i * sizeH;
result[index++] = s;
result[index++] = t;
result[index++] = s;
result[index++] = t + sizeH;
result[index++] = s + sizeW;
result[index++] = t;
result[index++] = s + sizeW;
result[index++] = t;
result[index++] = s;
result[index++] = t + sizeH;
result[index++] = s + sizeW;
result[index++] = t + sizeH;
}
}
return result;
}
function onKeyPress(event) {
switch (String.fromCharCode(event.keyCode)) {
case 'P':
case 'p':
pause();
break;
case 'R':
case 'r':
resume();
break;
default:
break;
}
}
function pause() {
mContinuous = false;
}
function resume() {
mContinuous = true;
requestAnimationFrame(render);
}
function drawScene(gl, programInfo, buffers, deltaTime) {
// Update the rotation for the next draw
// mYawing -= deltaTime * 0.38;
// mPitching += deltaTime * 0.1;
mAngle += 0.6 * (Math.PI / 16);
gl.clearColor(1.0, 1.0, 1.0, 1.0); // Clear to black, fully opaque
gl.clearDepth(1.0); // Clear everything
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near things obscure far things
// Clear the canvas before we start drawing on it.
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Set the drawing position to the "identity" point, which is the center of the scene.
mModelMatrix = mat4.create();
mat4.translate(mModelMatrix, // destination matrix
mModelMatrix, // matrix to translate
[-0.0, 0.0, -0.0]); // amount to translate
mat4.rotate(mModelMatrix, // destination matrix
mModelMatrix, // matrix to rotate
mPitching, // amount to rotate in radians
[1, 0, 0]); // axis to rotate around
mat4.rotate(mModelMatrix, // destination matrix
mModelMatrix, // matrix to rotate
mYawing, // amount to rotate in radians
[0, 1, 0]); // axis to rotate around
mat4.scale(mModelMatrix, mModelMatrix, [mScale, mScale, mScale]);
// Tell WebGL how to pull out the positions from the position
// buffer into the vertexPosition attribute.
{
const numComponents = 3;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexPosition);
}
// Tell WebGL how to pull out the texture coordinates from the texture
// coordinate buffer into the textureCoord attribute.
{
const numComponents = 2;
const type = gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.uv);
gl.vertexAttribPointer(
programInfo.attribLocations.textureCoord,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfo.attribLocations.textureCoord);
}
// Tell WebGL to use our program when drawing
gl.useProgram(programInfo.program);
// Specify the diffuseTexture to map onto the faces.
// Tell WebGL we want to affect diffuseTexture unit 0
gl.activeTexture(gl.TEXTURE0);
// Bind the diffuseTexture to diffuseTexture unit 0
gl.bindTexture(gl.TEXTURE_2D, mPlaneTexture);
// Tell the shader we bound the diffuseTexture to diffuseTexture unit 0
gl.uniform1i(programInfo.uniformLocations.uTexSamplerHandle, 0);
// Set the shader uniforms
gl.uniformMatrix4fv(
programInfo.uniformLocations.uProjectionMatrixHandle,
false, mProjectionMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.uViewMatrixHandle,
false, mViewMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.uModelMatrixHandle,
false, mModelMatrix);
gl.uniform1f(programInfo.uniformLocations.uWidthSpanHandle, PLANE_WIDTH);
gl.uniform1f(programInfo.uniformLocations.uHeightSpanHandle, PLANE_HEIGHT);
gl.uniform1f(programInfo.uniformLocations.uAngleHandle, mAngle);
const offset = 0;
gl.drawArrays(gl.TRIANGLES, offset, mVertices.length / 3);
gl.disableVertexAttribArray(programInfo.attribLocations.vertexPosition);
gl.disableVertexAttribArray(programInfo.attribLocations.textureCoord);
}