@@ -68,10 +68,10 @@ export class Space {
68
68
// webgl default is x(right), y(up), z(out of screen = towards the viewer) = right-handed coordinate system.
69
69
cameraPosition : Vector3 = { x : 0 , y : 0 , z : 0 } ;
70
70
cameraDirection : Vector3 = { x : 0 , y : 0 , z : - 1 } ;
71
+ originalFlowDirection : Vector3 = { x : 0 , y : 0 , z : 1 } ;
72
+ flowDirection : Vector3 = { x : 0 , y : 0 , z : 1 } ;
71
73
stars : Star [ ] = [ ] ;
72
74
73
- prevTimeStamp : number = 0 ;
74
-
75
75
shaderProgram : WebGLProgram ;
76
76
vertexBuffer : WebGLBuffer ;
77
77
@@ -83,7 +83,6 @@ export class Space {
83
83
this . canvas . height = this . canvas . clientHeight ;
84
84
this . context = this . canvas . getContext ( 'webgl' ) ! ;
85
85
this . context . viewport ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
86
- this . prevTimeStamp = performance . now ( ) ;
87
86
this . shaderProgram = this . context . createProgram ( ) ! ;
88
87
this . vertexBuffer = this . context . createBuffer ( ) ! ;
89
88
}
@@ -145,7 +144,6 @@ void main() {
145
144
146
145
initiateStars ( ) {
147
146
this . alpha = 0.0 ;
148
- this . prevTimeStamp = performance . now ( ) ;
149
147
this . stars = [ ] ;
150
148
this . stars . push ( { // default test star.
151
149
position : { x : 0 , y : 0 , z : - 5 } ,
@@ -204,11 +202,13 @@ void main() {
204
202
this . stars . forEach ( star => {
205
203
let position = star . position ;
206
204
// move the star against the camera direction.
207
- position = vectorSubtract ( position , vectorMultiplyScalar ( this . cameraDirection , flowAmount * star . speedRatio ) ) ;
205
+ position = vectorAdd ( position , vectorMultiplyScalar ( this . flowDirection , flowAmount * star . speedRatio ) ) ;
208
206
star . alpha = clampValue ( star . alpha + dt * star . speedRatio , 0.0 , 1.0 ) ;
209
- if ( Math . abs ( position . z ) > STAR_VISUAL_RANGE ||
207
+ const outOfVisualRange = Math . abs ( position . z ) > STAR_VISUAL_RANGE ||
210
208
Math . abs ( position . x ) > STAR_VISUAL_RANGE ||
211
- Math . abs ( position . y ) > STAR_VISUAL_RANGE ) {
209
+ Math . abs ( position . y ) > STAR_VISUAL_RANGE ;
210
+ const outOfScreen = dotProduct ( position , this . cameraDirection ) < 0 ;
211
+ if ( outOfVisualRange || outOfScreen ) {
212
212
position . x = rangedRandom ( - 1 , 1 ) * STAR_VISUAL_RANGE ;
213
213
position . y = rangedRandom ( - 1 , 1 ) * STAR_VISUAL_RANGE ;
214
214
position . z = rangedRandom ( - 1 , 1 ) * STAR_VISUAL_RANGE ;
@@ -298,12 +298,6 @@ void main() {
298
298
}
299
299
300
300
render ( ) {
301
- const nowTimeStamp = performance . now ( ) ;
302
- const deltaTimeSec = ( nowTimeStamp - this . prevTimeStamp ) * 0.001 ;
303
- this . prevTimeStamp = nowTimeStamp ;
304
-
305
- this . flowStars ( deltaTimeSec ) ;
306
-
307
301
this . updateVertexBuffer ( ) ;
308
302
309
303
// clear black.
@@ -351,4 +345,29 @@ void main() {
351
345
// Draw points
352
346
gl . drawArrays ( gl . POINTS , 0 , this . stars . length ) ;
353
347
}
348
+
349
+ setFlowDirection ( yaw : number , pitch : number ) {
350
+ // rotate only cameraDirection vector.
351
+ const flowDirection = this . originalFlowDirection ;
352
+
353
+ // rotate around y-axis.
354
+ const cosYaw = Math . cos ( yaw ) ;
355
+ const sinYaw = Math . sin ( yaw ) ;
356
+ const newCameraDirection = {
357
+ x : flowDirection . x * cosYaw - flowDirection . z * sinYaw ,
358
+ y : flowDirection . y ,
359
+ z : flowDirection . x * sinYaw + flowDirection . z * cosYaw
360
+ } ;
361
+
362
+ // rotate around x-axis.
363
+ const cosPitch = Math . cos ( pitch ) ;
364
+ const sinPitch = Math . sin ( pitch ) ;
365
+ const newCameraDirection2 = {
366
+ x : newCameraDirection . x ,
367
+ y : newCameraDirection . y * cosPitch - newCameraDirection . z * sinPitch ,
368
+ z : newCameraDirection . y * sinPitch + newCameraDirection . z * cosPitch
369
+ } ;
370
+
371
+ this . flowDirection = newCameraDirection2 ;
372
+ }
354
373
}
0 commit comments