Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft. flo-bit.dev/shapecraft
Something went wrong. Try again.
10 kB · 668 lines
JavaScript
at commit da69385e
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669class Vector4 {
constructor( x = 0, y = 0, z = 0, w = 1 ) {
Vector4.prototype.isVector4 = true;
this.x = x; this.y = y; this.z = z; this.w = w;
}
get width() {
return this.z;
}
set width( value ) {
this.z = value;
}
get height() {
return this.w;
}
set height( value ) {
this.w = value;
}
set( x, y, z, w ) {
this.x = x; this.y = y; this.z = z; this.w = w;
return this;
}
setScalar( scalar ) {
this.x = scalar; this.y = scalar; this.z = scalar; this.w = scalar;
return this;
}
setX( x ) {
this.x = x;
return this;
}
setY( y ) {
this.y = y;
return this;
}
setZ( z ) {
this.z = z;
return this;
}
setW( w ) {
this.w = w;
return this;
}
setComponent( index, value ) {
switch ( index ) {
case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; case 3: this.w = value; break; default: throw new Error( 'index is out of range: ' + index );
}
return this;
}
getComponent( index ) {
switch ( index ) {
case 0: return this.x; case 1: return this.y; case 2: return this.z; case 3: return this.w; default: throw new Error( 'index is out of range: ' + index );
}
}
clone() {
return new this.constructor( this.x, this.y, this.z, this.w );
}
copy( v ) {
this.x = v.x; this.y = v.y; this.z = v.z; this.w = ( v.w !== undefined ) ? v.w : 1;
return this;
}
add( v ) {
this.x += v.x; this.y += v.y; this.z += v.z; this.w += v.w;
return this;
}
addScalar( s ) {
this.x += s; this.y += s; this.z += s; this.w += s;
return this;
}
addVectors( a, b ) {
this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; this.w = a.w + b.w;
return this;
}
addScaledVector( v, s ) {
this.x += v.x * s; this.y += v.y * s; this.z += v.z * s; this.w += v.w * s;
return this;
}
sub( v ) {
this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w;
return this;
}
subScalar( s ) {
this.x -= s; this.y -= s; this.z -= s; this.w -= s;
return this;
}
subVectors( a, b ) {
this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; this.w = a.w - b.w;
return this;
}
multiply( v ) {
this.x *= v.x; this.y *= v.y; this.z *= v.z; this.w *= v.w;
return this;
}
multiplyScalar( scalar ) {
this.x *= scalar; this.y *= scalar; this.z *= scalar; this.w *= scalar;
return this;
}
applyMatrix4( m ) {
const x = this.x, y = this.y, z = this.z, w = this.w; const e = m.elements;
this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w; this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w; this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w; this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
return this;
}
divide( v ) {
this.x /= v.x; this.y /= v.y; this.z /= v.z; this.w /= v.w;
return this;
}
divideScalar( scalar ) {
return this.multiplyScalar( 1 / scalar );
}
setAxisAngleFromQuaternion( q ) {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
// q is assumed to be normalized
this.w = 2 * Math.acos( q.w );
const s = Math.sqrt( 1 - q.w * q.w );
if ( s < 0.0001 ) {
this.x = 1; this.y = 0; this.z = 0;
} else {
this.x = q.x / s; this.y = q.y / s; this.z = q.z / s;
}
return this;
}
setAxisAngleFromRotationMatrix( m ) {
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
let angle, x, y, z; // variables for result const epsilon = 0.01, // margin to allow for rounding errors epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
te = m.elements,
m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ], m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ], m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
if ( ( Math.abs( m12 - m21 ) < epsilon ) && ( Math.abs( m13 - m31 ) < epsilon ) && ( Math.abs( m23 - m32 ) < epsilon ) ) {
// singularity found // first check for identity matrix which must have +1 for all terms // in leading diagonal and zero in other terms
if ( ( Math.abs( m12 + m21 ) < epsilon2 ) && ( Math.abs( m13 + m31 ) < epsilon2 ) && ( Math.abs( m23 + m32 ) < epsilon2 ) && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
// this singularity is identity matrix so angle = 0
this.set( 1, 0, 0, 0 );
return this; // zero angle, arbitrary axis
}
// otherwise this singularity is angle = 180
angle = Math.PI;
const xx = ( m11 + 1 ) / 2; const yy = ( m22 + 1 ) / 2; const zz = ( m33 + 1 ) / 2; const xy = ( m12 + m21 ) / 4; const xz = ( m13 + m31 ) / 4; const yz = ( m23 + m32 ) / 4;
if ( ( xx > yy ) && ( xx > zz ) ) {
// m11 is the largest diagonal term
if ( xx < epsilon ) {
x = 0; y = 0.707106781; z = 0.707106781;
} else {
x = Math.sqrt( xx ); y = xy / x; z = xz / x;
}
} else if ( yy > zz ) {
// m22 is the largest diagonal term
if ( yy < epsilon ) {
x = 0.707106781; y = 0; z = 0.707106781;
} else {
y = Math.sqrt( yy ); x = xy / y; z = yz / y;
}
} else {
// m33 is the largest diagonal term so base result on this
if ( zz < epsilon ) {
x = 0.707106781; y = 0.707106781; z = 0;
} else {
z = Math.sqrt( zz ); x = xz / z; y = yz / z;
}
}
this.set( x, y, z, angle );
return this; // return 180 deg rotation
}
// as we have reached here there are no singularities so we can handle normally
let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) + ( m13 - m31 ) * ( m13 - m31 ) + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
if ( Math.abs( s ) < 0.001 ) s = 1;
// prevent divide by zero, should not happen if matrix is orthogonal and should be // caught by singularity test above, but I've left it in just in case
this.x = ( m32 - m23 ) / s; this.y = ( m13 - m31 ) / s; this.z = ( m21 - m12 ) / s; this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
return this;
}
setFromMatrixPosition( m ) {
const e = m.elements;
this.x = e[ 12 ]; this.y = e[ 13 ]; this.z = e[ 14 ]; this.w = e[ 15 ];
return this;
}
min( v ) {
this.x = Math.min( this.x, v.x ); this.y = Math.min( this.y, v.y ); this.z = Math.min( this.z, v.z ); this.w = Math.min( this.w, v.w );
return this;
}
max( v ) {
this.x = Math.max( this.x, v.x ); this.y = Math.max( this.y, v.y ); this.z = Math.max( this.z, v.z ); this.w = Math.max( this.w, v.w );
return this;
}
clamp( min, max ) {
// assumes min < max, componentwise
this.x = Math.max( min.x, Math.min( max.x, this.x ) ); this.y = Math.max( min.y, Math.min( max.y, this.y ) ); this.z = Math.max( min.z, Math.min( max.z, this.z ) ); this.w = Math.max( min.w, Math.min( max.w, this.w ) );
return this;
}
clampScalar( minVal, maxVal ) {
this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); this.z = Math.max( minVal, Math.min( maxVal, this.z ) ); this.w = Math.max( minVal, Math.min( maxVal, this.w ) );
return this;
}
clampLength( min, max ) {
const length = this.length();
return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) );
}
floor() {
this.x = Math.floor( this.x ); this.y = Math.floor( this.y ); this.z = Math.floor( this.z ); this.w = Math.floor( this.w );
return this;
}
ceil() {
this.x = Math.ceil( this.x ); this.y = Math.ceil( this.y ); this.z = Math.ceil( this.z ); this.w = Math.ceil( this.w );
return this;
}
round() {
this.x = Math.round( this.x ); this.y = Math.round( this.y ); this.z = Math.round( this.z ); this.w = Math.round( this.w );
return this;
}
roundToZero() {
this.x = Math.trunc( this.x ); this.y = Math.trunc( this.y ); this.z = Math.trunc( this.z ); this.w = Math.trunc( this.w );
return this;
}
negate() {
this.x = - this.x; this.y = - this.y; this.z = - this.z; this.w = - this.w;
return this;
}
dot( v ) {
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
}
lengthSq() {
return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
}
length() {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
}
manhattanLength() {
return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
}
normalize() {
return this.divideScalar( this.length() || 1 );
}
setLength( length ) {
return this.normalize().multiplyScalar( length );
}
lerp( v, alpha ) {
this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; this.z += ( v.z - this.z ) * alpha; this.w += ( v.w - this.w ) * alpha;
return this;
}
lerpVectors( v1, v2, alpha ) {
this.x = v1.x + ( v2.x - v1.x ) * alpha; this.y = v1.y + ( v2.y - v1.y ) * alpha; this.z = v1.z + ( v2.z - v1.z ) * alpha; this.w = v1.w + ( v2.w - v1.w ) * alpha;
return this;
}
equals( v ) {
return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
}
fromArray( array, offset = 0 ) {
this.x = array[ offset ]; this.y = array[ offset + 1 ]; this.z = array[ offset + 2 ]; this.w = array[ offset + 3 ];
return this;
}
toArray( array = [], offset = 0 ) {
array[ offset ] = this.x; array[ offset + 1 ] = this.y; array[ offset + 2 ] = this.z; array[ offset + 3 ] = this.w;
return array;
}
fromBufferAttribute( attribute, index ) {
this.x = attribute.getX( index ); this.y = attribute.getY( index ); this.z = attribute.getZ( index ); this.w = attribute.getW( index );
return this;
}
random() {
this.x = Math.random(); this.y = Math.random(); this.z = Math.random(); this.w = Math.random();
return this;
}
*[ Symbol.iterator ]() {
yield this.x; yield this.y; yield this.z; yield this.w;
}
}
export { Vector4 };