@@ -6,20 +6,20 @@ import { Camera } from './lib/camera'
66import { initBasicRenderProgram , drawScene , createShadowMap , initShadowRenderProgram } from './lib/BasicRenderProgram'
77import { loadObj } from './lib/loaders/ObjLoader'
88import { InputState } from './lib/input'
9- import { m4fromPositionAndEuler , m4lookAt , m4vectorMultiply , m4yRotation } from './lib/mat4'
9+ import { m4fromPositionAndEuler , m4lookAt } from './lib/mat4'
1010import { initGlState } from './lib/gl'
1111import { getWorldRayFromClipSpaceAndCamera , rayIntersectsScene , sortBySceneDepth } from './lib/raycast'
1212import { getPointerClickInClipSpace } from './lib/events'
13- import { Vec3 , Vec4 , calculateOrbitPosition } from './lib/vec'
13+ import { Color , POS_ORIGIN , ROT_NONE , Vec3 , calculateOrbitPosition } from './lib/vec'
1414
1515
1616type NodeName = "yellow tree" | "orange tree" | "green tree" | "floor" ;
1717
18- const meshColorMap : Record < NodeName , Vec3 > = {
19- "yellow tree" : [ 0.7 , 0.7 , 0.01 ] ,
20- "orange tree" : [ 0.6 , 0.3 , 0.001 ] ,
21- "green tree" : [ 0.1 , 0.6 , 0.1 ] ,
22- "floor" : [ 0.2 , 0.2 , 0.4 ]
18+ const meshColorMap : Record < NodeName , Color > = {
19+ "yellow tree" : { r : 0.7 , g : 0.7 , b : 0.01 } ,
20+ "orange tree" : { r : 0.6 , g : 0.3 , b : 0.001 } ,
21+ "green tree" : { r : 0.1 , g : 0.6 , b : 0.1 } ,
22+ "floor" : { r : 0.2 , g : 0.2 , b : 0.4 }
2323} ;
2424
2525type Orbit = {
@@ -33,7 +33,7 @@ const orbit: Orbit = {
3333 azimuth : Math . PI * - 0.2 , // horizontal angle, in radians
3434 elevation : 3 * Math . PI / 4 , // vertical angle, in radians
3535 radius : 15 ,
36- target : [ - 3 , 2 , - 2 ] ,
36+ target : { x : - 3 , y : 2 , z : - 2 } ,
3737 sensitivity : 0.01 ,
3838}
3939
@@ -78,35 +78,35 @@ if (!basicRenderProgram) {
7878
7979const vertices = await loadObj ( '/rainbowtree.obj' ) ;
8080
81- const yellowTree = initSceneNode ( m4fromPositionAndEuler ( [ 0 , 0 , 0 ] , [ 0 , Math . PI / 2 , 0 ] ) ,
81+ const yellowTree = initSceneNode ( m4fromPositionAndEuler ( POS_ORIGIN , { x : 0 , y : Math . PI / 2 , z : 0 } ) ,
8282 {
8383 vertices,
8484 material : {
8585 color : meshColorMap [ "yellow tree" ] ,
86- specularColor : [ 0.2 , 0.2 , 0.2 ] ,
86+ specularColor : { r : 0.2 , g : 0.2 , b : 0.2 } ,
8787 shininess : 0.9
8888 } } ,
8989 "yellow tree" )
9090
9191
9292const orangeTree = initSceneNode (
93- m4fromPositionAndEuler ( [ 5 , 0 , 0 ] , [ 0 , Math . PI / 2 , 0 ] ) ,
93+ m4fromPositionAndEuler ( { x : 5 , y : 0 , z : 0 } , { x : 0 , y : Math . PI / 2 , z : 0 } ) ,
9494 {
9595 vertices,
9696 material : {
9797 color : meshColorMap [ "orange tree" ] ,
98- specularColor : [ 0.2 , 0.2 , 0.2 ] ,
98+ specularColor : { r : 0.2 , g : 0.2 , b : 0.2 } ,
9999 shininess : 0.9
100100 } } ,
101101 "orange tree" )
102102
103103const greenTree = initSceneNode (
104- m4fromPositionAndEuler ( [ 5 , 0 , 0 ] , [ 0 , Math . PI / 2 , 0 ] ) ,
104+ m4fromPositionAndEuler ( { x : 5 , y : 0 , z : 0 } , { x : 0 , y : Math . PI / 2 , z : 0 } ) ,
105105 {
106106 vertices,
107107 material : {
108108 color : meshColorMap [ "green tree" ] ,
109- specularColor : [ 0.2 , 0.2 , 0.2 ] ,
109+ specularColor : { r : 0.2 , g : 0.2 , b : 0.2 } ,
110110 shininess : 0.5
111111 } } ,
112112 "green tree" )
@@ -145,12 +145,12 @@ const floorVertices: Vertices = {
145145}
146146
147147
148- const floorNode = initSceneNode ( m4fromPositionAndEuler ( [ 0 , 0.1 , 0 ] , [ 0 , 0 , 0 ] ) ,
148+ const floorNode = initSceneNode ( m4fromPositionAndEuler ( { x : 0 , y : 0.1 , z : 0 } , ROT_NONE ) ,
149149 {
150150 vertices : floorVertices ,
151151 material : {
152152 color : meshColorMap [ "floor" ] ,
153- specularColor : [ 0.2 , 0.2 , 0.2 ] ,
153+ specularColor : { r : 0.2 , g : 0.2 , b : 0.2 } ,
154154 shininess : 0.9
155155 } } ,
156156 "floor" )
@@ -161,17 +161,17 @@ const scene = [yellowTree, floorNode]
161161
162162// create lights
163163const ambientLight : AmbientLight = {
164- color : [ 0.2 , 0.2 , 0.2 ]
164+ color : { r : 0.2 , g : 0.2 , b : 0.2 }
165165 } ;
166166
167167const directionalLight : DirectionalLight = {
168- rotation : [ 0.0 , - 0.8 , - 0.5 ] ,
169- color : [ 0.6 , 0.6 , 0.6 ] ,
168+ rotation : { x : 0 , y : - 0.8 , z : - 0.5 } ,
169+ color : { r : 0.6 , g : 0.6 , b : 0.6 } ,
170170 } ;
171171
172172const pointLight : PointLight = {
173- position : [ 0 , 5.0 , 5 ] ,
174- color : [ 0.7 , 0.7 , 0.7 ] ,
173+ position : { x : 0 , y : 5.0 , z : 5 } ,
174+ color : { r : 0.7 , g : 0.7 , b : 0.7 } ,
175175 constant : 1.0 ,
176176 linear : 0.009 ,
177177 quadratic : 0.032
@@ -184,7 +184,7 @@ const cameraPosition = calculateOrbitPosition(
184184 orbit . radius
185185 ) ;
186186
187- const up : Vec3 = [ 0 , 1 , 0 ]
187+ const up : Vec3 = { x : 0 , y : 1 , z : 0 }
188188const camera : Camera = {
189189 fieldOfViewRadians : degToRad ( 60 ) ,
190190 aspect : canvas . clientWidth / canvas . clientHeight ,
@@ -197,7 +197,7 @@ const camera: Camera = {
197197
198198
199199const input : InputState = {
200- pointerPosition : [ 0 , 0 ]
200+ pointerPosition : { x : 0 , y : 0 }
201201}
202202
203203
@@ -250,12 +250,12 @@ canvas.addEventListener('pointerdown', () => {
250250
251251///////////////////////////
252252
253- let lastTime = 0 ;
253+ // let lastTime = 0;
254254function animate ( time : DOMHighResTimeStamp ) {
255255 time *= 0.001 // convert from millis to seconds
256- const dt = time - lastTime ;
257- lastTime = time ;
258- updateLight ( pointLight , dt )
256+ // const dt = time - lastTime;
257+ // lastTime = time;
258+ // updateLight(pointLight, dt) // this both break under the shadow mapping
259259 // updateDirectionalLight(directionalLight, time)
260260 resizeCanvasToDisplaySize ( canvas ) ;
261261 camera . aspect = canvas . clientWidth / canvas . clientHeight ;
@@ -302,24 +302,24 @@ function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement): void {
302302 }
303303
304304}
305+
306+
305307// function updateDirectionalLight(light: DirectionalLight, time: number) {
306308
307309// const oldRotation = light.rotation
308310
309311
310- // light.rotation = [ oldRotation[0], Math.sin(time), oldRotation[2]]
312+ // light.rotation = { x: oldRotation.x, y: Math.sin(time), z: oldRotation.z }
311313
312314// }
313315
314- function updateLight ( pointLight : PointLight , dt : number ) {
315- const rotator = m4yRotation ( Math . PI / ( dt * 10000 ) ) ;
316- const oldTransform : Vec4 = [
317- pointLight . position [ 0 ] ,
318- pointLight . position [ 1 ] ,
319- pointLight . position [ 2 ] ,
320- 0.0
321- ] ;
316+ // function updateLight(pointLight: PointLight, dt: number) {
317+ // const rotator = m4yRotation(Math.PI / (dt * 10000));
318+ // const oldTransform: Vec4 = {
319+ // ...pointLight.position,
320+ // w: 0.0
321+ // };
322322
323- const newTransform = m4vectorMultiply ( oldTransform , rotator ) ;
324- pointLight . position = [ newTransform [ 0 ] , newTransform [ 1 ] , newTransform [ 2 ] ]
325- }
323+ // const newTransform = m4PositionMultiply (oldTransform, rotator);
324+ // pointLight.position = { x: newTransform.x, y: newTransform.y, z: newTransform.z }
325+ // }
0 commit comments