Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/flo-bit/shapecraft. flo-bit.dev/shapecraft
Something went wrong. Try again.
21 kB · 1111 lines
JavaScript
at commit da69385e
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112import { Vector3 } from '../math/Vector3.js';import { Vector2 } from '../math/Vector2.js';import { Box3 } from '../math/Box3.js';import { EventDispatcher } from './EventDispatcher.js';import { BufferAttribute, Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from './BufferAttribute.js';import { Sphere } from '../math/Sphere.js';import { Object3D } from './Object3D.js';import { Matrix4 } from '../math/Matrix4.js';import { Matrix3 } from '../math/Matrix3.js';import * as MathUtils from '../math/MathUtils.js';import { arrayNeedsUint32 } from '../utils.js';
let _id = 0;
const _m1 = /*@__PURE__*/ new Matrix4();const _obj = /*@__PURE__*/ new Object3D();const _offset = /*@__PURE__*/ new Vector3();const _box = /*@__PURE__*/ new Box3();const _boxMorphTargets = /*@__PURE__*/ new Box3();const _vector = /*@__PURE__*/ new Vector3();
class BufferGeometry extends EventDispatcher {
constructor() {
super();
this.isBufferGeometry = true;
Object.defineProperty( this, 'id', { value: _id ++ } );
this.uuid = MathUtils.generateUUID();
this.name = ''; this.type = 'BufferGeometry';
this.index = null; this.indirect = null; this.attributes = {};
this.morphAttributes = {}; this.morphTargetsRelative = false;
this.groups = [];
this.boundingBox = null; this.boundingSphere = null;
this.drawRange = { start: 0, count: Infinity };
this.userData = {};
}
getIndex() {
return this.index;
}
setIndex( index ) {
if ( Array.isArray( index ) ) {
this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
} else {
this.index = index;
}
return this;
}
setIndirect( indirect ) {
this.indirect = indirect;
return this;
}
getIndirect() {
return this.indirect;
}
getAttribute( name ) {
return this.attributes[ name ];
}
setAttribute( name, attribute ) {
this.attributes[ name ] = attribute;
return this;
}
deleteAttribute( name ) {
delete this.attributes[ name ];
return this;
}
hasAttribute( name ) {
return this.attributes[ name ] !== undefined;
}
addGroup( start, count, materialIndex = 0 ) {
this.groups.push( {
start: start, count: count, materialIndex: materialIndex
} );
}
clearGroups() {
this.groups = [];
}
setDrawRange( start, count ) {
this.drawRange.start = start; this.drawRange.count = count;
}
applyMatrix4( matrix ) {
const position = this.attributes.position;
if ( position !== undefined ) {
position.applyMatrix4( matrix );
position.needsUpdate = true;
}
const normal = this.attributes.normal;
if ( normal !== undefined ) {
const normalMatrix = new Matrix3().getNormalMatrix( matrix );
normal.applyNormalMatrix( normalMatrix );
normal.needsUpdate = true;
}
const tangent = this.attributes.tangent;
if ( tangent !== undefined ) {
tangent.transformDirection( matrix );
tangent.needsUpdate = true;
}
if ( this.boundingBox !== null ) {
this.computeBoundingBox();
}
if ( this.boundingSphere !== null ) {
this.computeBoundingSphere();
}
return this;
}
applyQuaternion( q ) {
_m1.makeRotationFromQuaternion( q );
this.applyMatrix4( _m1 );
return this;
}
rotateX( angle ) {
// rotate geometry around world x-axis
_m1.makeRotationX( angle );
this.applyMatrix4( _m1 );
return this;
}
rotateY( angle ) {
// rotate geometry around world y-axis
_m1.makeRotationY( angle );
this.applyMatrix4( _m1 );
return this;
}
rotateZ( angle ) {
// rotate geometry around world z-axis
_m1.makeRotationZ( angle );
this.applyMatrix4( _m1 );
return this;
}
translate( x, y, z ) {
// translate geometry
_m1.makeTranslation( x, y, z );
this.applyMatrix4( _m1 );
return this;
}
scale( x, y, z ) {
// scale geometry
_m1.makeScale( x, y, z );
this.applyMatrix4( _m1 );
return this;
}
lookAt( vector ) {
_obj.lookAt( vector );
_obj.updateMatrix();
this.applyMatrix4( _obj.matrix );
return this;
}
center() {
this.computeBoundingBox();
this.boundingBox.getCenter( _offset ).negate();
this.translate( _offset.x, _offset.y, _offset.z );
return this;
}
setFromPoints( points ) {
const positionAttribute = this.getAttribute( 'position' );
if ( positionAttribute === undefined ) {
const position = [];
for ( let i = 0, l = points.length; i < l; i ++ ) {
const point = points[ i ]; position.push( point.x, point.y, point.z || 0 );
}
this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
} else {
for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
const point = points[ i ]; positionAttribute.setXYZ( i, point.x, point.y, point.z || 0 );
}
if ( points.length > positionAttribute.count ) {
console.warn( 'THREE.BufferGeometry: Buffer size too small for points data. Use .dispose() and create a new geometry.' );
}
positionAttribute.needsUpdate = true;
}
return this;
}
computeBoundingBox() {
if ( this.boundingBox === null ) {
this.boundingBox = new Box3();
}
const position = this.attributes.position; const morphAttributesPosition = this.morphAttributes.position;
if ( position && position.isGLBufferAttribute ) {
console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box.', this );
this.boundingBox.set( new Vector3( - Infinity, - Infinity, - Infinity ), new Vector3( + Infinity, + Infinity, + Infinity ) );
return;
}
if ( position !== undefined ) {
this.boundingBox.setFromBufferAttribute( position );
// process morph attributes if present
if ( morphAttributesPosition ) {
for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
const morphAttribute = morphAttributesPosition[ i ]; _box.setFromBufferAttribute( morphAttribute );
if ( this.morphTargetsRelative ) {
_vector.addVectors( this.boundingBox.min, _box.min ); this.boundingBox.expandByPoint( _vector );
_vector.addVectors( this.boundingBox.max, _box.max ); this.boundingBox.expandByPoint( _vector );
} else {
this.boundingBox.expandByPoint( _box.min ); this.boundingBox.expandByPoint( _box.max );
}
}
}
} else {
this.boundingBox.makeEmpty();
}
if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
}
}
computeBoundingSphere() {
if ( this.boundingSphere === null ) {
this.boundingSphere = new Sphere();
}
const position = this.attributes.position; const morphAttributesPosition = this.morphAttributes.position;
if ( position && position.isGLBufferAttribute ) {
console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere.', this );
this.boundingSphere.set( new Vector3(), Infinity );
return;
}
if ( position ) {
// first, find the center of the bounding sphere
const center = this.boundingSphere.center;
_box.setFromBufferAttribute( position );
// process morph attributes if present
if ( morphAttributesPosition ) {
for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
const morphAttribute = morphAttributesPosition[ i ]; _boxMorphTargets.setFromBufferAttribute( morphAttribute );
if ( this.morphTargetsRelative ) {
_vector.addVectors( _box.min, _boxMorphTargets.min ); _box.expandByPoint( _vector );
_vector.addVectors( _box.max, _boxMorphTargets.max ); _box.expandByPoint( _vector );
} else {
_box.expandByPoint( _boxMorphTargets.min ); _box.expandByPoint( _boxMorphTargets.max );
}
}
}
_box.getCenter( center );
// second, try to find a boundingSphere with a radius smaller than the // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
let maxRadiusSq = 0;
for ( let i = 0, il = position.count; i < il; i ++ ) {
_vector.fromBufferAttribute( position, i );
maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
}
// process morph attributes if present
if ( morphAttributesPosition ) {
for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
const morphAttribute = morphAttributesPosition[ i ]; const morphTargetsRelative = this.morphTargetsRelative;
for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
_vector.fromBufferAttribute( morphAttribute, j );
if ( morphTargetsRelative ) {
_offset.fromBufferAttribute( position, j ); _vector.add( _offset );
}
maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector ) );
}
}
}
this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
if ( isNaN( this.boundingSphere.radius ) ) {
console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
}
}
}
computeTangents() {
const index = this.index; const attributes = this.attributes;
// based on http://www.terathon.com/code/tangent.html // (per vertex tangents)
if ( index === null || attributes.position === undefined || attributes.normal === undefined || attributes.uv === undefined ) {
console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' ); return;
}
const positionAttribute = attributes.position; const normalAttribute = attributes.normal; const uvAttribute = attributes.uv;
if ( this.hasAttribute( 'tangent' ) === false ) {
this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * positionAttribute.count ), 4 ) );
}
const tangentAttribute = this.getAttribute( 'tangent' );
const tan1 = [], tan2 = [];
for ( let i = 0; i < positionAttribute.count; i ++ ) {
tan1[ i ] = new Vector3(); tan2[ i ] = new Vector3();
}
const vA = new Vector3(), vB = new Vector3(), vC = new Vector3(),
uvA = new Vector2(), uvB = new Vector2(), uvC = new Vector2(),
sdir = new Vector3(), tdir = new Vector3();
function handleTriangle( a, b, c ) {
vA.fromBufferAttribute( positionAttribute, a ); vB.fromBufferAttribute( positionAttribute, b ); vC.fromBufferAttribute( positionAttribute, c );
uvA.fromBufferAttribute( uvAttribute, a ); uvB.fromBufferAttribute( uvAttribute, b ); uvC.fromBufferAttribute( uvAttribute, c );
vB.sub( vA ); vC.sub( vA );
uvB.sub( uvA ); uvC.sub( uvA );
const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
// silently ignore degenerate uv triangles having coincident or colinear vertices
if ( ! isFinite( r ) ) return;
sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r ); tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
tan1[ a ].add( sdir ); tan1[ b ].add( sdir ); tan1[ c ].add( sdir );
tan2[ a ].add( tdir ); tan2[ b ].add( tdir ); tan2[ c ].add( tdir );
}
let groups = this.groups;
if ( groups.length === 0 ) {
groups = [ { start: 0, count: index.count } ];
}
for ( let i = 0, il = groups.length; i < il; ++ i ) {
const group = groups[ i ];
const start = group.start; const count = group.count;
for ( let j = start, jl = start + count; j < jl; j += 3 ) {
handleTriangle( index.getX( j + 0 ), index.getX( j + 1 ), index.getX( j + 2 ) );
}
}
const tmp = new Vector3(), tmp2 = new Vector3(); const n = new Vector3(), n2 = new Vector3();
function handleVertex( v ) {
n.fromBufferAttribute( normalAttribute, v ); n2.copy( n );
const t = tan1[ v ];
// Gram-Schmidt orthogonalize
tmp.copy( t ); tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
// Calculate handedness
tmp2.crossVectors( n2, t ); const test = tmp2.dot( tan2[ v ] ); const w = ( test < 0.0 ) ? - 1.0 : 1.0;
tangentAttribute.setXYZW( v, tmp.x, tmp.y, tmp.z, w );
}
for ( let i = 0, il = groups.length; i < il; ++ i ) {
const group = groups[ i ];
const start = group.start; const count = group.count;
for ( let j = start, jl = start + count; j < jl; j += 3 ) {
handleVertex( index.getX( j + 0 ) ); handleVertex( index.getX( j + 1 ) ); handleVertex( index.getX( j + 2 ) );
}
}
}
computeVertexNormals() {
const index = this.index; const positionAttribute = this.getAttribute( 'position' );
if ( positionAttribute !== undefined ) {
let normalAttribute = this.getAttribute( 'normal' );
if ( normalAttribute === undefined ) {
normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 ); this.setAttribute( 'normal', normalAttribute );
} else {
// reset existing normals to zero
for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
normalAttribute.setXYZ( i, 0, 0, 0 );
}
}
const pA = new Vector3(), pB = new Vector3(), pC = new Vector3(); const nA = new Vector3(), nB = new Vector3(), nC = new Vector3(); const cb = new Vector3(), ab = new Vector3();
// indexed elements
if ( index ) {
for ( let i = 0, il = index.count; i < il; i += 3 ) {
const vA = index.getX( i + 0 ); const vB = index.getX( i + 1 ); const vC = index.getX( i + 2 );
pA.fromBufferAttribute( positionAttribute, vA ); pB.fromBufferAttribute( positionAttribute, vB ); pC.fromBufferAttribute( positionAttribute, vC );
cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab );
nA.fromBufferAttribute( normalAttribute, vA ); nB.fromBufferAttribute( normalAttribute, vB ); nC.fromBufferAttribute( normalAttribute, vC );
nA.add( cb ); nB.add( cb ); nC.add( cb );
normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z ); normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z ); normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
}
} else {
// non-indexed elements (unconnected triangle soup)
for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
pA.fromBufferAttribute( positionAttribute, i + 0 ); pB.fromBufferAttribute( positionAttribute, i + 1 ); pC.fromBufferAttribute( positionAttribute, i + 2 );
cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab );
normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z ); normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z ); normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
}
}
this.normalizeNormals();
normalAttribute.needsUpdate = true;
}
}
normalizeNormals() {
const normals = this.attributes.normal;
for ( let i = 0, il = normals.count; i < il; i ++ ) {
_vector.fromBufferAttribute( normals, i );
_vector.normalize();
normals.setXYZ( i, _vector.x, _vector.y, _vector.z );
}
}
toNonIndexed() {
function convertBufferAttribute( attribute, indices ) {
const array = attribute.array; const itemSize = attribute.itemSize; const normalized = attribute.normalized;
const array2 = new array.constructor( indices.length * itemSize );
let index = 0, index2 = 0;
for ( let i = 0, l = indices.length; i < l; i ++ ) {
if ( attribute.isInterleavedBufferAttribute ) {
index = indices[ i ] * attribute.data.stride + attribute.offset;
} else {
index = indices[ i ] * itemSize;
}
for ( let j = 0; j < itemSize; j ++ ) {
array2[ index2 ++ ] = array[ index ++ ];
}
}
return new BufferAttribute( array2, itemSize, normalized );
}
//
if ( this.index === null ) {
console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' ); return this;
}
const geometry2 = new BufferGeometry();
const indices = this.index.array; const attributes = this.attributes;
// attributes
for ( const name in attributes ) {
const attribute = attributes[ name ];
const newAttribute = convertBufferAttribute( attribute, indices );
geometry2.setAttribute( name, newAttribute );
}
// morph attributes
const morphAttributes = this.morphAttributes;
for ( const name in morphAttributes ) {
const morphArray = []; const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
const attribute = morphAttribute[ i ];
const newAttribute = convertBufferAttribute( attribute, indices );
morphArray.push( newAttribute );
}
geometry2.morphAttributes[ name ] = morphArray;
}
geometry2.morphTargetsRelative = this.morphTargetsRelative;
// groups
const groups = this.groups;
for ( let i = 0, l = groups.length; i < l; i ++ ) {
const group = groups[ i ]; geometry2.addGroup( group.start, group.count, group.materialIndex );
}
return geometry2;
}
toJSON() {
const data = { metadata: { version: 4.6, type: 'BufferGeometry', generator: 'BufferGeometry.toJSON' } };
// standard BufferGeometry serialization
data.uuid = this.uuid; data.type = this.type; if ( this.name !== '' ) data.name = this.name; if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
if ( this.parameters !== undefined ) {
const parameters = this.parameters;
for ( const key in parameters ) {
if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
}
return data;
}
// for simplicity the code assumes attributes are not shared across geometries, see #15811
data.data = { attributes: {} };
const index = this.index;
if ( index !== null ) {
data.data.index = { type: index.array.constructor.name, array: Array.prototype.slice.call( index.array ) };
}
const attributes = this.attributes;
for ( const key in attributes ) {
const attribute = attributes[ key ];
data.data.attributes[ key ] = attribute.toJSON( data.data );
}
const morphAttributes = {}; let hasMorphAttributes = false;
for ( const key in this.morphAttributes ) {
const attributeArray = this.morphAttributes[ key ];
const array = [];
for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
const attribute = attributeArray[ i ];
array.push( attribute.toJSON( data.data ) );
}
if ( array.length > 0 ) {
morphAttributes[ key ] = array;
hasMorphAttributes = true;
}
}
if ( hasMorphAttributes ) {
data.data.morphAttributes = morphAttributes; data.data.morphTargetsRelative = this.morphTargetsRelative;
}
const groups = this.groups;
if ( groups.length > 0 ) {
data.data.groups = JSON.parse( JSON.stringify( groups ) );
}
const boundingSphere = this.boundingSphere;
if ( boundingSphere !== null ) {
data.data.boundingSphere = { center: boundingSphere.center.toArray(), radius: boundingSphere.radius };
}
return data;
}
clone() {
return new this.constructor().copy( this );
}
copy( source ) {
// reset
this.index = null; this.attributes = {}; this.morphAttributes = {}; this.groups = []; this.boundingBox = null; this.boundingSphere = null;
// used for storing cloned, shared data
const data = {};
// name
this.name = source.name;
// index
const index = source.index;
if ( index !== null ) {
this.setIndex( index.clone( data ) );
}
// attributes
const attributes = source.attributes;
for ( const name in attributes ) {
const attribute = attributes[ name ]; this.setAttribute( name, attribute.clone( data ) );
}
// morph attributes
const morphAttributes = source.morphAttributes;
for ( const name in morphAttributes ) {
const array = []; const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
array.push( morphAttribute[ i ].clone( data ) );
}
this.morphAttributes[ name ] = array;
}
this.morphTargetsRelative = source.morphTargetsRelative;
// groups
const groups = source.groups;
for ( let i = 0, l = groups.length; i < l; i ++ ) {
const group = groups[ i ]; this.addGroup( group.start, group.count, group.materialIndex );
}
// bounding box
const boundingBox = source.boundingBox;
if ( boundingBox !== null ) {
this.boundingBox = boundingBox.clone();
}
// bounding sphere
const boundingSphere = source.boundingSphere;
if ( boundingSphere !== null ) {
this.boundingSphere = boundingSphere.clone();
}
// draw range
this.drawRange.start = source.drawRange.start; this.drawRange.count = source.drawRange.count;
// user data
this.userData = source.userData;
return this;
}
dispose() {
this.dispatchEvent( { type: 'dispose' } );
}
}
export { BufferGeometry };