1
1
/* Licensed under a BSD license. See license.html for license */
2
+ /* eslint strict: off */
2
3
"use strict" ;
3
4
4
5
var vertexShaderSource = `
@@ -100,11 +101,9 @@ function main() {
100
101
return d * Math . PI / 180 ;
101
102
}
102
103
103
- var cameraAngleRadians = degToRad ( 0 ) ;
104
- var fieldOfViewRadians = degToRad ( 40 ) ;
105
- var cameraHeight = 40 ;
106
- var zNear = 1 ;
107
- var zFar = 150 ;
104
+ const fieldOfViewRadians = degToRad ( 40 ) ;
105
+ const zNear = 1 ;
106
+ const zFar = 150 ;
108
107
109
108
var uniformsThatAreTheSameForAllObjects = {
110
109
u_lightWorldPos : [ - 50 , 30 , 100 ] ,
@@ -129,13 +128,51 @@ function main() {
129
128
u_specularFactor : 1 ,
130
129
} ;
131
130
132
- requestAnimationFrame ( drawScene ) ;
131
+ let requestId ;
132
+ let running ;
133
+ let then = 0 ;
134
+ let time = 0 ;
135
+ function startAnimation ( ) {
136
+ running = true ;
137
+ requestAnimation ( ) ;
138
+ }
139
+
140
+ function stopAnimation ( ) {
141
+ running = false ;
142
+ }
143
+
144
+ function requestAnimation ( ) {
145
+ if ( ! requestId ) {
146
+ requestId = requestAnimationFrame ( drawScene ) ;
147
+ }
148
+ }
149
+
150
+ const motionQuery = matchMedia ( '(prefers-reduced-motion)' ) ;
151
+ function handleReduceMotionChanged ( ) {
152
+ if ( motionQuery . matches ) {
153
+ stopAnimation ( ) ;
154
+ } else {
155
+ startAnimation ( ) ;
156
+ }
157
+ }
158
+ motionQuery . addEventListener ( 'change' , handleReduceMotionChanged ) ;
159
+ handleReduceMotionChanged ( ) ;
160
+ requestAnimation ( ) ;
161
+
162
+ const observer = new ResizeObserver ( requestAnimation ) ;
163
+ observer . observe ( gl . canvas ) ;
133
164
134
165
// Draw the scene.
135
- function drawScene ( time ) {
136
- webglUtils . resizeCanvasToDisplaySize ( canvas ) ;
166
+ function drawScene ( now ) {
167
+ requestId = undefined ;
168
+
169
+ const elapsed = Math . min ( now - then , 1000 / 10 ) ;
170
+ then = now ;
171
+ if ( running ) {
172
+ time += elapsed * 0.001 ;
173
+ }
137
174
138
- time *= 0.001 ; // convert to seconds
175
+ webglUtils . resizeCanvasToDisplaySize ( canvas ) ;
139
176
140
177
// Set the viewport to match the canvas
141
178
gl . viewport ( 0 , 0 , gl . canvas . width , gl . canvas . height ) ;
@@ -145,21 +182,21 @@ function main() {
145
182
gl . clear ( gl . COLOR_BUFFER_BIT | gl . DEPTH_BUFFER_BIT ) ;
146
183
147
184
// Compute the projection matrix
148
- var aspect = gl . canvas . clientWidth / gl . canvas . clientHeight ;
149
- var projectionMatrix = m4 . perspective ( fieldOfViewRadians , aspect , 1 , 150 ) ;
185
+ const aspect = gl . canvas . clientWidth / gl . canvas . clientHeight ;
186
+ const projectionMatrix = m4 . perspective ( fieldOfViewRadians , aspect , 1 , 150 ) ;
150
187
151
188
// Compute the camera's matrix using look at.
152
- var orbitRadius = 100 ;
153
- var orbitTime = 1 + time * 0.05 ;
154
- var cameraPosition = [ Math . cos ( orbitTime ) * orbitRadius , Math . sin ( orbitTime * 1.123 ) * orbitRadius , Math . sin ( orbitTime ) * orbitRadius ] ;
155
- var target = [ 0 , 0 , 0 ] ;
156
- var up = [ 0 , 1 , 0 ] ;
157
- var cameraMatrix = m4 . lookAt ( cameraPosition , target , up , uniformsThatAreTheSameForAllObjects . u_viewInverse ) ;
189
+ const orbitRadius = 100 ;
190
+ const orbitTime = 1 + time * 0.05 ;
191
+ const cameraPosition = [ Math . cos ( orbitTime ) * orbitRadius , Math . sin ( orbitTime * 1.123 ) * orbitRadius , Math . sin ( orbitTime ) * orbitRadius ] ;
192
+ const target = [ 0 , 0 , 0 ] ;
193
+ const up = [ 0 , 1 , 0 ] ;
194
+ const cameraMatrix = m4 . lookAt ( cameraPosition , target , up , uniformsThatAreTheSameForAllObjects . u_viewInverse ) ;
158
195
159
196
// Make a view matrix from the camera matrix.
160
- var viewMatrix = m4 . inverse ( cameraMatrix ) ;
197
+ const viewMatrix = m4 . inverse ( cameraMatrix ) ;
161
198
162
- var viewProjectionMatrix = m4 . multiply ( projectionMatrix , viewMatrix ) ;
199
+ const viewProjectionMatrix = m4 . multiply ( projectionMatrix , viewMatrix ) ;
163
200
164
201
gl . useProgram ( programInfo . program ) ;
165
202
@@ -170,27 +207,27 @@ function main() {
170
207
webglUtils . setUniforms ( programInfo , uniformsThatAreTheSameForAllObjects ) ;
171
208
172
209
// Draw objects
173
- var num = 4 ;
174
- var spread = 20 ;
175
- for ( var zz = - num ; zz <= num ; ++ zz ) {
176
- for ( var yy = - num ; yy <= num ; ++ yy ) {
177
- for ( var xx = - num ; xx <= num ; ++ xx ) {
210
+ const num = 4 ;
211
+ const spread = 20 ;
212
+ for ( let zz = - num ; zz <= num ; ++ zz ) {
213
+ for ( let yy = - num ; yy <= num ; ++ yy ) {
214
+ for ( let xx = - num ; xx <= num ; ++ xx ) {
178
215
var worldMatrix = m4 . translation ( xx * spread , yy * spread , zz * spread ) ;
179
216
180
217
// Multiply the matrices.
181
218
m4 . multiply (
182
219
viewProjectionMatrix , worldMatrix ,
183
220
uniformsThatAreComputedForEachObject . u_worldViewProjection ) ;
184
- var matrix = m4 . multiply ( viewMatrix , worldMatrix ) ;
221
+ // var matrix = m4.multiply(viewMatrix, worldMatrix);
185
222
m4 . transpose ( m4 . inverse ( worldMatrix ) , uniformsThatAreComputedForEachObject . u_worldInverseTranspose ) ;
186
223
187
224
// Set the uniforms we just computed
188
225
webglUtils . setUniforms ( programInfo , uniformsThatAreComputedForEachObject ) ;
189
226
190
227
// Set a color for this object.
191
- materialUniforms . u_diffuse [ 0 ] = 1 ; xx / num * 0.5 + 0.5 ;
192
- materialUniforms . u_diffuse [ 1 ] = 1 ; yy / num * 0.5 + 0.5 ;
193
- materialUniforms . u_diffuse [ 2 ] = 1 ; zz / num * 0.5 + 0.5 ;
228
+ materialUniforms . u_diffuse [ 0 ] = 1 ; // xx / num * 0.5 + 0.5;
229
+ materialUniforms . u_diffuse [ 1 ] = 1 ; // yy / num * 0.5 + 0.5;
230
+ materialUniforms . u_diffuse [ 2 ] = 1 ; // zz / num * 0.5 + 0.5;
194
231
195
232
// Set the uniforms that are specific to the this object.
196
233
webglUtils . setUniforms ( programInfo , materialUniforms ) ;
@@ -200,8 +237,9 @@ function main() {
200
237
}
201
238
}
202
239
}
203
-
204
- requestAnimationFrame ( drawScene ) ;
240
+ if ( running ) {
241
+ requestAnimation ( drawScene ) ;
242
+ }
205
243
}
206
244
}
207
245
0 commit comments