@@ -41,7 +41,7 @@ window.addEventListener('resize', () =>
4141// Canvas selector
4242const canvas = document . querySelector ( 'canvas.canvasGL' )
4343
44- // Init Scene
44+ // Initialize Scene
4545const scene = new THREE . Scene ( )
4646const updateAllMaterials = ( ) =>
4747{
@@ -56,12 +56,16 @@ const updateAllMaterials = () =>
5656 } )
5757}
5858
59+ /** Set scene background color */
5960// TODO add skybox
6061scene . background = new THREE . Color ( Theme . backgroundColor ) ;
6162
63+
6264/**
63- * Perspective Camera
65+ * Create Perspective Camera
6466 */
67+
68+
6569const camera = new THREE . PerspectiveCamera ( 75 , sizes . width / sizes . height , 1 , 1000 )
6670camera . position . set ( 0 , 0.8 , 0 ) ;
6771camera . lookAt ( new THREE . Vector3 ( 0 , 0 , 0 ) ) ;
@@ -71,38 +75,40 @@ scene.add(camera)
7175/**
7276 *
7377 *
74- * Character controls helper.
75- * These helpers provide smooth camera movement through proxy objects.
78+ * Character controls helper variables.
79+ * We create these vectors to calculate camera offset and adjust its position
80+ * see tick function
7681 *
7782 * */
7883
79- let t = new THREE . Vector3 ;
80- let dir = new THREE . Vector3 ;
81- let a = new THREE . Vector3 ;
82- let b = new THREE . Vector3 ;
83- const distance = 5.2 ;
84- let velocity = 0.0 ;
85- let speed = 0.0 ;
84+ const character_position = new THREE . Vector3 ;
85+ const camera_position = new THREE . Vector3 ;
86+ const tail_position = new THREE . Vector3 ;
87+ const camera_offset = new THREE . Vector3 ;
88+
89+ const distance = 4 ;
90+ let velocity = 0.0 ; // velocity provides smooth speed gain
91+ let speed = 0.0 ; // default idle speed
92+
93+ // Helper objects to provide camera movements
8694let tail = new THREE . Object3D ;
8795let follower = new THREE . Object3D ;
8896
89- follower . position . y = 0.5
90- follower . position . z = - distance ;
91-
92- tail . add ( camera )
97+ tail . position . y = 0.5
98+ tail . position . z = - distance ;
9399
100+ follower . add ( camera )
94101
95102/** Initialize keyboard and add event listeners to control character */
96103
97104let keyboard = { } ;
98105
99106function keyDown ( event ) {
100-
101107 keyboard [ event . keyCode ] = true ;
102108}
103109
104110function keyUp ( event ) {
105- keyboard [ event . keyCode ] = false ;
111+ keyboard [ event . keyCode ] = false ;
106112}
107113
108114window . addEventListener ( 'keydown' , keyDown ) ;
@@ -126,27 +132,35 @@ gltfLoader.load(
126132 '/models/Character/Character.glb' ,
127133 ( gltf ) =>
128134 {
135+ /** Scale character */
129136 gltf . scene . scale . set ( 1 , 1 , 1 )
130- character = gltf . scene // make scene available outside this scope
131- characterAnimation = gltf . animations // make animations available outside this scope
132- character . add ( follower ) // apply follower object to the model
133- gltf . scene . castShadow = true //enable shadows
137+
138+ /** Make scene and animation available outside the scope */
139+ character = gltf . scene
140+ characterAnimation = gltf . animations
141+
142+ /** Apply tail object to the character */
143+ character . add ( tail )
144+
145+ /** Enable shadows */
146+ gltf . scene . castShadow = true
134147 gltf . scene . receiveShadow = true
135148
136149 scene . add ( gltf . scene )
137150
138- camera . lookAt ( character . position ) ; // set camera to look at the character by default
151+ /** Set camare to look at the character by default */
152+ camera . lookAt ( character . position ) ;
139153
140- // Se animations
154+ // Get animations
141155 mixer = new THREE . AnimationMixer ( character )
142156 action = {
143157 idle : mixer . clipAction ( characterAnimation [ 0 ] ) ,
144158 run : mixer . clipAction ( characterAnimation [ 1 ] ) ,
145159 walk : mixer . clipAction ( characterAnimation [ 2 ] ) ,
146160 back : mixer . clipAction ( characterAnimation [ 3 ] )
147161 }
148- prevAnim = "idle" ; //default idle animation
149- action . idle . play ( )
162+ prevAnim = "idle" ; // set default idle animation
163+ action . idle . play ( ) // play animation
150164 }
151165)
152166
@@ -270,23 +284,26 @@ function crossfadeAnimation(newAction){
270284 newAction . crossFadeFrom ( action [ prevAnim ] , 0.3 )
271285}
272286
273- //** Tick function to update screen */
287+ //** Tick function to update */
274288const tick = ( ) =>
275289{
290+ /** Calculate shades */
276291 updateAllMaterials ( )
292+
293+ /** Count elapsed time */
277294 const elapsedTime = clock . getElapsedTime ( )
278295 const deltaTime = elapsedTime - previousTime
279296 previousTime = elapsedTime
280297
281- // Model animation
298+ /** Update animation mixer on each frame */
282299 if ( mixer )
283300 {
284301 mixer . update ( deltaTime )
285302 }
286303
287- /*** Character control */
304+ /*** Character controls */
288305
289- // Running
306+ /** Running forwards */
290307 if ( keyboard [ 87 ] && keyboard [ 16 ] ) {
291308 speed = 0.2 ;
292309
@@ -298,7 +315,7 @@ const tick = () =>
298315 }
299316 }
300317
301- // Walking
318+ /** Walking forward */
302319 if ( keyboard [ 87 ] && ! keyboard [ 16 ] ) {
303320 speed = 0.09 ;
304321
@@ -310,7 +327,7 @@ const tick = () =>
310327 }
311328 }
312329
313- // Walking backwards
330+ /** Walking backwards */
314331 if ( keyboard [ 83 ] ) {
315332 speed = - 0.09 ;
316333
@@ -322,7 +339,7 @@ const tick = () =>
322339 }
323340 }
324341
325- //Stopped
342+ /** Ilde state */
326343 if ( ! keyboard [ 87 ] && ! keyboard [ 83 ] ) {
327344 speed = 0 ;
328345 if ( action . idle && prevAnim != "idle" ) {
@@ -334,7 +351,7 @@ const tick = () =>
334351 }
335352
336353
337- /* Turns */
354+ /* Turn character */
338355 if ( keyboard [ 65 ] ) {
339356 if ( character ) {
340357 character . rotateY ( 0.05 ) ;
@@ -347,25 +364,61 @@ const tick = () =>
347364 }
348365
349366 if ( character ) {
367+
368+
369+ /**
370+ *
371+ * Move character along Z axis
372+ */
373+
350374 velocity += ( speed - velocity ) * .3 ;
375+
351376 character . translateZ ( velocity ) ;
352-
353- a . lerp ( character . position , 0.4 ) ;
354- b . copy ( tail . position ) ;
355-
356- dir . copy ( a ) . sub ( b ) . normalize ( ) ;
357- const dis = a . distanceTo ( b ) - distance ;
358- tail . position . addScaledVector ( dir , dis ) ;
359- tail . position . lerp ( t , 0.02 ) ;
360- t . setFromMatrixPosition ( follower . matrixWorld ) ;
377+
378+
379+ /**
380+ * Smoothly move character position Vector3 to actiual character position
381+ *
382+ */
383+ character_position . lerp ( character . position , 0.4 ) ;
384+
385+ /**
386+ * Set default camera position Vector3 to the follower
387+ */
388+ camera_position . copy ( follower . position ) ;
389+
390+ /**
391+ * Set tail_position Vector3 to absolute tail position
392+ */
393+ tail_position . setFromMatrixPosition ( tail . matrixWorld ) ;
394+
395+
396+ /*
397+ *
398+ * Calculate a offset camera vector, based on the character's
399+ * position. Offset represented by the direction vector3 from
400+ * the camera (follower) to the character
401+ */
402+
403+ camera_offset . copy ( character_position ) . sub ( camera_position ) . normalize ( ) ;
404+ const distanceDifference = character_position . distanceTo ( camera_position ) - distance ;
405+ follower . position . addScaledVector ( camera_offset , distanceDifference ) ;
406+
407+
408+ /**
409+ * Lerp camera to the tail position
410+ */
411+ follower . position . lerp ( tail_position , 0.02 ) ;
412+
413+ /**
414+ * Update position of the character
415+ * to point camera
416+ */
361417
362418 const position = new THREE . Vector3 ( character . position . x , 2.2 , character . position . z )
363419 camera . lookAt ( position )
364420 }
365421
366-
367-
368-
369422 // Update orbit controls
370423 controls . update ( )
371424
0 commit comments