@@ -179,7 +179,7 @@ export const main = async () => {
179179 if ( points . isNotSpawned ( i ) )
180180 continue ;
181181
182- points . positions [ points . activeIdx [ i ] ] += dt * points . signs [ i ] * points . speeds [ i ] ;
182+ points . positions [ points . activeIdx [ i ] ] += dt * points . speeds [ i ] ;
183183 if ( points . isOutOfBounds ( i ) ) {
184184 points . free ( i ) ;
185185 continue ;
@@ -272,22 +272,19 @@ class CubeInstanceArray {
272272 private data : ArrayBuffer ;
273273 public count = 0 ;
274274 public instanceCenters : Float32Array ; // float vec3
275- public instanceColors : Float32Array ; // float vec4
275+ public instanceColors : Float32Array ; // float vec3
276276 public instanceSizes : Float32Array ; // float
277277
278278 constructor ( instanceNum : number , maxCubesPerPosition : number ) {
279279 const instances = instanceNum * maxCubesPerPosition ;
280280 const vec3Elts = 3 * instances ;
281- const vec4Elts = 4 * instances ;
282- this . data = new ArrayBuffer ( ( vec3Elts + vec4Elts + instances ) * Float32Array . BYTES_PER_ELEMENT ) ;
281+ this . data = new ArrayBuffer ( ( vec3Elts * 2 + instances ) * Float32Array . BYTES_PER_ELEMENT ) ;
283282
284283 let offset = 0 ;
285284 this . instanceCenters = new Float32Array ( this . data , offset , vec3Elts ) ;
286285 offset += vec3Elts * Float32Array . BYTES_PER_ELEMENT ;
287-
288- this . instanceColors = new Float32Array ( this . data , offset , vec4Elts ) ;
289- offset += vec4Elts * Float32Array . BYTES_PER_ELEMENT ;
290-
286+ this . instanceColors = new Float32Array ( this . data , offset , vec3Elts ) ;
287+ offset += vec3Elts * Float32Array . BYTES_PER_ELEMENT ;
291288 this . instanceSizes = new Float32Array ( this . data , offset , instances ) ;
292289 }
293290
@@ -320,7 +317,6 @@ class PointsArray {
320317 public nextFreeOrSpawned : Int32Array ; // int32
321318 public activeIdx : Int32Array ; // int32
322319 public directions : Int8Array ; // char
323- public signs : Int8Array ; // char (signed)
324320
325321 // freelist constants, if name collisions become an issue wrap these in a frozen object
326322 private static readonly HEAD_IDX = 0 ;
@@ -339,7 +335,7 @@ class PointsArray {
339335 vec3Bytes * 3 + // positions, colors, scales
340336 floatBytes + // speeds
341337 int32Bytes * 2 + // nextFreeOrSpawned, activeIdx
342- int8Bytes * 2 // directions, signs
338+ int8Bytes // directions
343339 ) ;
344340
345341 let offset = 0 ;
@@ -356,8 +352,6 @@ class PointsArray {
356352 this . activeIdx = new Int32Array ( this . data , offset , poolSize ) ;
357353 offset += int32Bytes ;
358354 this . directions = new Int8Array ( this . data , offset , poolSize ) ;
359- offset += int8Bytes ;
360- this . signs = new Int8Array ( this . data , offset , poolSize ) ;
361355
362356 // initialize freelist
363357 for ( let i = 0 ; i < poolSize - 1 ; i ++ )
@@ -368,36 +362,44 @@ class PointsArray {
368362 }
369363
370364 public spawn ( ) {
371- if ( this . freelist [ PointsArray . HEAD_IDX ] == PointsArray . LIST_END ) return PointsArray . LIST_END ;
365+ // pop from freelist, return if none available
366+ if ( this . freelist [ PointsArray . HEAD_IDX ] == PointsArray . LIST_END )
367+ return PointsArray . LIST_END ;
368+
372369 const idx = this . freelist [ PointsArray . HEAD_IDX ] ;
370+
373371 this . freelist [ PointsArray . HEAD_IDX ] = this . nextFreeOrSpawned [ idx ] ;
374372 if ( this . freelist [ PointsArray . HEAD_IDX ] == PointsArray . LIST_END )
375373 this . freelist [ 1 ] = PointsArray . LIST_END ;
376374 this . nextFreeOrSpawned [ idx ] = PointsArray . IS_SPAWNED ;
377- const lerpA = PRNG . nextRange ( 0 , 1 ) ;
378- const lerpB = 1 - lerpA ;
379- const [ pr , pg , pb ] = getVec3Idx ( idx ) ;
380- this . colors [ pr ] = ( 0xC7 / 255 ) * lerpA + ( 0x61 / 255 ) * lerpB ;
381- this . colors [ pg ] = ( 0x51 / 255 ) * lerpA + ( 0x0C / 255 ) * lerpB ;
382- this . colors [ pb ] = ( 0x08 / 255 ) * lerpA + ( 0xCF / 255 ) * lerpB ;
383-
384- const [ bx , by , bz ] = getVec3Idx ( idx ) ;
375+
376+ // got the next available point, initialize
377+ const lerpVal = PRNG . nextRange ( 0 , 1 ) ;
378+ const oneMinusLerpVal = 1 - lerpVal ;
379+ const [ px , py , pz ] = getVec3Idx ( idx ) ;
380+ this . colors [ px ] = ( 0xC7 / 255 ) * lerpVal + ( 0x61 / 255 ) * oneMinusLerpVal ;
381+ this . colors [ py ] = ( 0x51 / 255 ) * lerpVal + ( 0x0C / 255 ) * oneMinusLerpVal ;
382+ this . colors [ pz ] = ( 0x08 / 255 ) * lerpVal + ( 0xCF / 255 ) * oneMinusLerpVal ;
383+
385384 this . directions [ idx ] = 1 << ( Math . trunc ( Math . random ( ) * Direction . LEN ) ) as DirVals ;
386- this . signs [ idx ] = this . directions [ idx ] & ( Direction . PX | Direction . PY | Direction . PZ ) ? 1 : - 1 ;
387- this . activeIdx [ idx ] = this . directions [ idx ] & ( Direction . PX | Direction . NX ) ? bx
388- : this . directions [ idx ] & ( Direction . PY | Direction . NY ) ? by : bz ;
385+ this . activeIdx [ idx ] = this . directions [ idx ] & ( Direction . PX | Direction . NX ) ? px
386+ : this . directions [ idx ] & ( Direction . PY | Direction . NY ) ? py : pz ;
389387
390- this . speeds [ idx ] = PRNG . nextRange ( Spawn . SPEED . MIN , Spawn . SPEED . MAX ) ;
388+ const sign = this . directions [ idx ] & ( Direction . PX | Direction . PY | Direction . PZ ) ? 1 : - 1 ;
389+ this . speeds [ idx ] = PRNG . nextRange ( Spawn . SPEED . MIN , Spawn . SPEED . MAX ) * sign ;
391390
392391 const pointRadius = PRNG . nextRange ( Spawn . RADIUS . MIN , Spawn . RADIUS . MAX ) ;
393- this . scales [ bx ] = pointRadius ;
394- this . scales [ by ] = pointRadius ;
395- this . scales [ bz ] = pointRadius ;
392+
393+ this . scales [ px ] = pointRadius ;
394+ this . scales [ py ] = pointRadius ;
395+ this . scales [ pz ] = pointRadius ;
396396 this . scales [ this . activeIdx [ idx ] ] = PRNG . nextRange ( Spawn . LENGTH . MIN , Spawn . LENGTH . MAX ) ;
397- this . positions [ bx ] = X_MIN_CUBE_CENTER + Math . trunc ( CUBES_X * Math . random ( ) ) * Cube . INTERVAL ;
398- this . positions [ by ] = Y_MIN_CUBE_CENTER + Math . trunc ( CUBES_Y * Math . random ( ) ) * Cube . INTERVAL ;
399- this . positions [ bz ] = Z_MIN_CUBE_CENTER + Math . trunc ( CUBES_Z * Math . random ( ) ) * Cube . INTERVAL ;
397+
398+ this . positions [ px ] = X_MIN_CUBE_CENTER + Math . trunc ( CUBES_X * Math . random ( ) ) * Cube . INTERVAL ;
399+ this . positions [ py ] = Y_MIN_CUBE_CENTER + Math . trunc ( CUBES_Y * Math . random ( ) ) * Cube . INTERVAL ;
400+ this . positions [ pz ] = Z_MIN_CUBE_CENTER + Math . trunc ( CUBES_Z * Math . random ( ) ) * Cube . INTERVAL ;
400401 this . positions [ this . activeIdx [ idx ] ] = this . getStartPos ( idx ) ;
402+
401403 return idx ;
402404 }
403405
0 commit comments