Last active
February 11, 2023 02:47
-
-
Save sketchpunk/bfd6ef74ed5073402ae9ac246b7ce1c7 to your computer and use it in GitHub Desktop.
Vector 3 Allocator with Mutable Slices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const alloc = new Vec3Allocator( 3 ); | |
alloc.push( [1,1,1] ); // idx 0 | |
alloc.push( [2,2,2] ); // idx 1 | |
alloc.push( [3,3,3] ); // idx 2 | |
alloc.setAt( 2, [5,5,5] ); // Modify 3rd Vec3 | |
alloc.resize( 4 ); // Resize to hold 4 Vec3, [0,0,0] | |
// Get Mutable Slice | |
const vmut = alloc.getMut( 0 ); // [1,1,1] | |
vmut[ 1 ] = 77; // [1,77,1] | |
// Get Mutable Vec3 Array | |
const v = alloc.getVec3( 1 ); // [2,2,2] | |
v .add( [10,10,10] ) // [12,12,12] | |
.scale( 0.5 ) // [6,6,6] | |
.x += 2 // [8,6,6] | |
console.log( 'Array', alloc.toArray() ); | |
// [1, 77, 1, 8, 6, 6, 5, 5, 5, 0, 0, 0] | |
console.log( 'From Vec3 Like', new Vec3( [1,2,3] ) ); | |
console.log( 'Set Components', new Vec3( 4, 5, 6 ) ); | |
console.log( 'Mutable Slice', new Vec3( alloc.arrayBuffer, 2 ) ); // [5,5,5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vec3 extends Float32Array{ | |
constructor( arg0=null, arg1=null, arg2=null ){ | |
// Slice of an existing Array Buffer | |
if( arg0 instanceof ArrayBuffer && typeof arg1 === 'number' ){ | |
super( arg0, arg1 * 3 * 4, 3 ); | |
}else{ | |
super( 3 ); | |
// First Argument is a vector3 like | |
if( ( Array.isArray( arg0 ) || arg0 instanceof Float32Array ) && arg0.length === 3 ){ | |
this.set( arg0 ); | |
// Setting XYZ | |
}else if( typeof arg0 === 'number' && typeof arg1 === 'number' && typeof arg2 === 'number' ){ | |
this[ 0 ] = arg0; | |
this[ 1 ] = arg1; | |
this[ 2 ] = arg2; | |
} | |
} | |
} | |
get x(){ return this[ 0 ]; } | |
get y(){ return this[ 1 ]; } | |
get z(){ return this[ 2 ]; } | |
set x( v ){ this[ 0 ] = v; } | |
set y( v ){ this[ 1 ] = v; } | |
set z( v ){ this[ 2 ] = v; } | |
add( v ){ | |
this[ 0 ] += v[ 0 ]; | |
this[ 1 ] += v[ 1 ]; | |
this[ 2 ] += v[ 2 ]; | |
return this; | |
} | |
scale( s ){ | |
this[ 0 ] *= s; | |
this[ 1 ] *= s; | |
this[ 2 ] *= s; | |
return this | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vec3Allocator{ | |
// #region MAIN | |
_buf = null; // Float32Array Buffer | |
_capacity = 0; // Max vecs possible | |
_size = 0; // Current vec count | |
_autoResize = 0; // How many vecs to auto resize allocator | |
_idx = 0; | |
constructor( size=10 ){ | |
this.resize( size ); | |
} | |
// #endregion | |
// #region GETTERS | |
get size(){ return this._size; } | |
get byteSize(){ return this._buf.byteLength; } | |
get arrayBuffer(){ return this._buf.buffer; } | |
toArray(){ return Array.from( this._buf ); } | |
// #endregion | |
// #region METHODS | |
push( v ){ | |
if( this._size >= this._capacity ){ console.log( 'Vec3Allocator at capacity' ); return this; } | |
this._buf.set( v, this._idx ); // Copy contents to buffer, V must be a [0,0,0] of some kind. | |
this._size += 1; // Extra Vec | |
this._idx += 3; // Move Buffer Index by 3 Floats = 1 Vec3 | |
return this; | |
} | |
setAt( idx, v ){ | |
if( idx >= this._capacity ){ console.log( 'GetMut: Index out of bounds' ); return this; } | |
this._buf.set( v, idx * 3 ); | |
return this; | |
} | |
getMut( idx ){ | |
if( idx >= this._capacity ){ console.log( 'GetMut: Index out of bounds' ); return null; } | |
return new Float32Array( this._buf.buffer, idx * 3 * 4, 3 ); // vecIndex * 3 Float * 4Byte Per Float | |
} | |
getVec3( idx ){ | |
if( idx >= this._capacity ){ console.log( 'GetVec3: Index out of bounds' ); return null; } | |
return new Vec3( this._buf.buffer, idx ); // vecIndex * 3 Float * 4Byte Per Float | |
} | |
// Resize buffer allocation | |
resize( size=0 ){ | |
if( size > this._capacity ){ | |
const newBuf = new Float32Array( size * 3 ); // Create new buffer | |
if( this._buf ) newBuf.set( this._buf ); // Copy old buffer contents | |
this._buf = newBuf; | |
this._capacity = size; | |
} | |
return this; | |
} | |
// #endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment