-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathlesson15.html
365 lines (332 loc) · 12 KB
/
lesson15.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
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
<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>漫反射光照</title>
<link rel="stylesheet" href="../css/common.css">
</head>
<body>
<script src="../utils/common.js"></script>
<script src="../utils/webgl-helper.js"></script>
<script src="../utils/webgl-matrix.js"></script>
<script src="../utils/vector3.js"></script>
<script src="../utils/geometry.js"></script>
<!-- 顶点着色器源码 -->
<script type="shader-source" id="vertexShaderForLight">
// 接收顶点坐标 (x, y)
precision mediump float;
//入射光方向向量
uniform vec3 u_LightPosition;
void main(){
gl_Position = vec4(u_LightPosition, 1);
}
</script>
<!-- 顶点着色器源码 -->
<script type="shader-source" id="fragmentShaderForLight">
// 接收顶点坐标 (x, y)
precision mediump float;
//光线颜色
uniform vec3 u_LightColor;
void main(){
gl_FragColor = vec4(u_LightColor, 1);
}
</script>
<!-- 顶点着色器源码 -->
<script type="shader-source" id="vertexShader">
precision mediump float;
//顶点坐标(x, y, z)
attribute vec3 a_Position;
//颜色信息
attribute vec4 a_Color;
varying vec4 v_Color;
//顶点法线
attribute vec3 a_Normal;
varying vec3 v_Normal;
//变换矩阵
uniform mat4 u_Matrix;
//uniform mat4 u_Matrix_Model;
varying vec3 v_Position;
//法线变换矩阵
uniform mat4 u_NormalMatrix;
//模型变换矩阵
uniform mat4 u_ModelMatrix;
void main(){
gl_Position = u_Matrix * vec4(a_Position, 1);
v_Color = a_Color;
v_Normal = mat3(u_NormalMatrix) * a_Normal;
v_Position = vec3(u_ModelMatrix * vec4(a_Position,1));
}
</script>
<!-- 片元着色器源码 -->
<script type="shader-source" id="fragmentShader">
precision mediump float;
//插值后的颜色
varying vec4 v_Color;
//光线颜色
uniform vec3 u_LightColor;
//环境光分量
uniform float u_AmbientFactor;
//入射光方向向量
uniform vec3 u_LightPosition;
varying vec3 v_Position;
varying vec3 v_Normal;
void main(){
// 环境光分量
vec3 ambient = u_AmbientFactor * u_LightColor; //环境光分量
// 光源照射方向向量
vec3 lightDirection = u_LightPosition - v_Position;
// 漫反射因子
float diffuseFactor = dot(normalize(lightDirection), normalize(v_Normal));
//
diffuseFactor = max(diffuseFactor, 0.0);
// 漫反射分量
vec3 diffuseLightColor = u_LightColor * diffuseFactor;
//
gl_FragColor = v_Color * vec4((ambient + diffuseLightColor),1);
}
</script>
<canvas id="canvas" width="564" height="662"></canvas>
<div class="operation-container">
<!-- <a href="javascript:;" id="animate" class="animate">转动</a>-->
<div>
<span>沿 y 轴缩放:</span>
<input id="modelScaleY" class="range" type="range" min="0" max="5" step="0.2" value="1">
<label class="range-label" id="modelScaleYValue"></label>
</div>
<div>
<span>物体 x 轴坐标:</span>
<input id="modelX" class="range" type="range" min="0" max="50" step="0.5" value="0">
<label class="range-label" id="modelXValue"></label>
</div>
<div>
<span>物体 y 轴坐标:</span>
<input id="modelY" class="range" type="range" min="0" max="50" step="0.5" value="0">
<label class="range-label" id="modelYValue"></label>
</div>
<div>
<span>物体 z 轴坐标:</span>
<input id="modelZ" class="range" type="range" min="0" max="50" step="0.5" value="0">
<label class="range-label" id="modelZValue"></label>
</div>
<div>
<span>光源 x 轴坐标:</span>
<input id="lightX" class="range" type="range" min="0" max="50" step="0.5" value="0">
<label class="range-label" id="lightXValue"></label>
</div>
<div>
<span>光源 y 轴坐标:</span>
<input id="lightY" class="range" type="range" min="0" max="50" step="0.5" value="0">
<label class="range-label" id="lightYValue"></label>
</div>
<div>
<span>光源 z 轴坐标:</span>
<input id="lightZ" class="range" type="range" min="0" max="50" step="0.5" value="20">
<label class="range-label" id="lightZValue"></label>
</div>
<div>
<span>x 轴转动角度:</span>
<input id="xRotation" class="range" type="range" min="0" max="360" step="1" value="0">
<label class="range-label" id="xRotationValue"></label>
</div>
<div>
<span>y 轴转动角度:</span>
<input id="yRotation" class="range" type="range" min="0" max="360" step="1" value="0">
<label id="yRotationValue"></label>
</div>
<div>
<span>z 轴转动角度:</span>
<input id="zRotation" class="range" type="range" min="0" max="360" step="1" value="0">
<label id="zRotationValue"></label>
</div>
<div>
<span>环境光强度:</span>
<input id="ambientFactor" class="range" type="range" min="0" max="1" step="0.01" value="0.2">
<label id="ambientFactorValue"></label>
</div>
<div>
<span>环境光颜色:</span>
<input id="lightColor" class="color" type="color" value="#FFFFFF">
<label id="lightColorValue" style="width: 200px"></label>
</div>
<button id="switchButton">播放</button>
</div>
<script>
var canvas = getCanvas('#canvas');
var Vector3 = window.lib3d.Vector3;
resizeCanvas(canvas);
var gl = getContext(canvas);
var program = createSimpleProgramFromScript(
gl,
'vertexShader',
'fragmentShader'
);
gl.useProgram(program);
var rate = canvas.width / canvas.height;
var per = matrix.ortho(-rate * 25, rate * 25, -25, 25, 100, -100);
var cube = createSphere(8, 6, 12); //createCube(10, 10, 10);
cube = transformIndicesToUnIndices(cube);
createColorForVertex(cube);
var positions = cube.positions;
var indices = cube.indices;
var colors = cube.colors;
var normals = cube.normals;
var a_Position = gl.getAttribLocation(program, 'a_Position');
var a_Color = gl.getAttribLocation(program, 'a_Color');
var a_Normal = gl.getAttribLocation(program, 'a_Normal');
var u_Matrix = gl.getUniformLocation(program, 'u_Matrix');
var u_Texture = gl.getUniformLocation(program, 'u_Texture');
var u_AmbientFactor = gl.getUniformLocation(program, 'u_AmbientFactor');
var u_LightColor = gl.getUniformLocation(program, 'u_LightColor');
var u_LightPosition = gl.getUniformLocation(program, 'u_LightPosition');
var u_NormalMatrix = gl.getUniformLocation(program, 'u_NormalMatrix');
var u_ModelMatrix = gl.getUniformLocation(program, 'u_ModelMatrix');
gl.enableVertexAttribArray(a_Position);
gl.enableVertexAttribArray(a_Color);
gl.enableVertexAttribArray(a_Normal);
// 计算投影矩阵
var aspect = canvas.clientWidth / canvas.clientHeight;
var fieldOfViewRadians = 60;
var projectionMatrix = matrix.perspective(
fieldOfViewRadians,
aspect,
1,
2000
);
// 计算相机在圆上的位置矩阵
var cameraPosition = new Vector3(0, 0, 12);
var target = new Vector3(0, 0, 0);
var up = new Vector3(0, 1, 0);
var cameraMatrix = matrix.lookAt(cameraPosition, target, up);
var modelMatrix = matrix.identity();
// 从相机矩阵取逆获取视图矩阵
var viewMatrix = matrix.inverse(cameraMatrix);
var viewProjectionMatrix = matrix.multiply(projectionMatrix, viewMatrix);
gl.uniformMatrix4fv(u_Matrix, false, per);
var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 0, 0);
var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
gl.vertexAttribPointer(a_Color, 4, gl.UNSIGNED_BYTE, true, 0, 0);
var normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, normals, gl.STATIC_DRAW);
gl.vertexAttribPointer(a_Normal, 3, gl.FLOAT, false, 0, 0);
function render(gl) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if (positions.length <= 0) {
return;
}
var primitiveType = gl.TRIANGLES;
gl.drawArrays(primitiveType, 0, positions.length / 3);
}
gl.clearColor(0, 0, 0, 1.0);
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE);
var angle = 0;
var xAngle = 0;
var timer = null;
switchButton.addEventListener('click', animate);
var direction = 0.05;
function animate(e) {
if (timer) {
switchButton.innerText = '播放';
clearInterval(timer);
timer = null;
} else {
timer = setInterval(() => {
if (uniforms.xRotation == 360) {
uniforms.xRotation = 0;
}
uniforms.xRotation += 1;
setUniforms();
render(gl);
}, 50);
switchButton.innerText = '暂停';
}
}
function setUniforms() {
gl.uniform3f(
u_LightColor,
uniforms['lightColor'].r / 255,
uniforms['lightColor'].g / 255,
uniforms['lightColor'].b / 255
);
gl.uniform3f(
u_LightPosition,
uniforms['lightX'],
uniforms['lightY'],
uniforms['lightZ']
);
gl.uniform1f(u_AmbientFactor, uniforms['ambientFactor']);
modelMatrix = matrix.rotationY((Math.PI / 180) * uniforms['yRotation']);
modelMatrix = matrix.rotateX(
modelMatrix,
(Math.PI / 180) * uniforms['xRotation']
);
modelMatrix = matrix.rotateZ(
modelMatrix,
(Math.PI / 180) * uniforms['zRotation']
);
modelMatrix = matrix.multiply(
modelMatrix,
matrix.scalation(1, uniforms['modelScaleY'], 1)
);
modelMatrix = matrix.translate(
modelMatrix,
uniforms['modelX'],
uniforms['modelY'],
uniforms['modelZ']
);
var uMatrix = matrix.multiply(
per,
matrix.multiply(viewMatrix, modelMatrix)
);
gl.uniformMatrix4fv(u_NormalMatrix, false, modelMatrix);
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix);
gl.uniformMatrix4fv(u_Matrix, false, uMatrix);
}
$$('.range').forEach(function(input) {
input.addEventListener('input', function() {
var uniformAttr = uniforms[this.id];
if (this.id == 'lightColor') {
uniforms[this.id] = getRGBFromColor(this.value);
$$(this.id + 'Value').innerHTML =
'r : ' +
uniformAttr.r +
', g : ' +
uniformAttr.g +
', b : ' +
uniformAttr.b;
} else {
uniforms[this.id] = Number(this.value);
$$(this.id + 'Value').innerHTML = this.value;
}
setUniforms();
render(gl);
});
});
var uniforms = {
lightColor: {
r: 255,
g: 255,
b: 255
},
ambientFactor: 0.2,
lightX: 0,
lightY: 0,
lightZ: 20,
xRotation: 0,
yRotation: 0,
zRotation: 0,
modelX: 0,
modelY: 0,
modelZ: 0,
modelScaleY: 1
};
setUniforms();
render(gl);
</script>
</body></html>