123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- class Object3D extends EventDispatcher {
- constructor() {
- super();
- this.isObject3D = true;
- Object.defineProperty( this, 'id', { value: _object3DId ++ } );
- this.uuid = generateUUID();
- this.name = '';
- this.type = 'Object3D';
- this.parent = null;
- this.children = [];
- this.up = Object3D.DefaultUp.clone();
- const position = new Vector3();
- const rotation = new Euler();
- const quaternion = new Quaternion();
- const scale = new Vector3( 1, 1, 1 );
- function onRotationChange() {
- quaternion.setFromEuler( rotation, false );
- }
- function onQuaternionChange() {
- rotation.setFromQuaternion( quaternion, undefined, false );
- }
- rotation._onChange( onRotationChange );
- quaternion._onChange( onQuaternionChange );
- Object.defineProperties( this, {
- position: {
- configurable: true,
- enumerable: true,
- value: position
- },
- rotation: {
- configurable: true,
- enumerable: true,
- value: rotation
- },
- quaternion: {
- configurable: true,
- enumerable: true,
- value: quaternion
- },
- scale: {
- configurable: true,
- enumerable: true,
- value: scale
- },
- modelViewMatrix: {
- value: new Matrix4()
- },
- normalMatrix: {
- value: new Matrix3()
- }
- } );
- this.matrix = new Matrix4();
- this.matrixWorld = new Matrix4();
- this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate;
- this.matrixWorldNeedsUpdate = false;
- this.matrixWorldAutoUpdate = Object3D.DefaultMatrixWorldAutoUpdate; // checked by the renderer
- this.layers = new Layers();
- this.visible = true;
- this.choice = null;
- this.castShadow = false;
- this.receiveShadow = false;
- this.frustumCulled = true;
- this.renderOrder = 0;
- this.animations = [];
- this.userData = {};
- }
- onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {}
- onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {}
- applyMatrix4( matrix ) {
- if ( this.matrixAutoUpdate ) this.updateMatrix();
- this.matrix.premultiply( matrix );
- this.matrix.decompose( this.position, this.quaternion, this.scale );
- }
- applyQuaternion( q ) {
- this.quaternion.premultiply( q );
- return this;
- }
- setRotationFromAxisAngle( axis, angle ) {
- // assumes axis is normalized
- this.quaternion.setFromAxisAngle( axis, angle );
- }
- setRotationFromEuler( euler ) {
- this.quaternion.setFromEuler( euler, true );
- }
- setRotationFromMatrix( m ) {
- // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
- this.quaternion.setFromRotationMatrix( m );
- }
- setRotationFromQuaternion( q ) {
- // assumes q is normalized
- this.quaternion.copy( q );
- }
- rotateOnAxis( axis, angle ) {
- // rotate object on axis in object space
- // axis is assumed to be normalized
- _q1.setFromAxisAngle( axis, angle );
- this.quaternion.multiply( _q1 );
- return this;
- }
- rotateOnWorldAxis( axis, angle ) {
- // rotate object on axis in world space
- // axis is assumed to be normalized
- // method assumes no rotated parent
- _q1.setFromAxisAngle( axis, angle );
- this.quaternion.premultiply( _q1 );
- return this;
- }
- rotateX( angle ) {
- return this.rotateOnAxis( _xAxis, angle );
- }
- rotateY( angle ) {
- return this.rotateOnAxis( _yAxis, angle );
- }
- rotateZ( angle ) {
- return this.rotateOnAxis( _zAxis, angle );
- }
- translateOnAxis( axis, distance ) {
- // translate object by distance along axis in object space
- // axis is assumed to be normalized
- _v1$4.copy( axis ).applyQuaternion( this.quaternion );
- this.position.add( _v1$4.multiplyScalar( distance ) );
- return this;
- }
- translateX( distance ) {
- return this.translateOnAxis( _xAxis, distance );
- }
- translateY( distance ) {
- return this.translateOnAxis( _yAxis, distance );
- }
- translateZ( distance ) {
- return this.translateOnAxis( _zAxis, distance );
- }
- localToWorld( vector ) {
- return vector.applyMatrix4( this.matrixWorld );
- }
- worldToLocal( vector ) {
- return vector.applyMatrix4( _m1$1.copy( this.matrixWorld ).invert() );
- }
- lookAt( x, y, z ) {
- // This method does not support objects having non-uniformly-scaled parent(s)
- if ( x.isVector3 ) {
- _target.copy( x );
- } else {
- _target.set( x, y, z );
- }
- const parent = this.parent;
- this.updateWorldMatrix( true, false );
- _position$3.setFromMatrixPosition( this.matrixWorld );
- if ( this.isCamera || this.isLight ) {
- _m1$1.lookAt( _position$3, _target, this.up );
- } else {
- _m1$1.lookAt( _target, _position$3, this.up );
- }
- this.quaternion.setFromRotationMatrix( _m1$1 );
- if ( parent ) {
- _m1$1.extractRotation( parent.matrixWorld );
- _q1.setFromRotationMatrix( _m1$1 );
- this.quaternion.premultiply( _q1.invert() );
- }
- }
- add( object ) {
- if ( arguments.length > 1 ) {
- for ( let i = 0; i < arguments.length; i ++ ) {
- this.add( arguments[ i ] );
- }
- return this;
- }
- if ( object === this ) {
- console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object );
- return this;
- }
- if ( object && object.isObject3D ) {
- if ( object.parent !== null ) {
- object.parent.remove( object );
- }
- object.parent = this;
- this.children.push( object );
- object.dispatchEvent( _addedEvent );
- } else {
- console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object );
- }
- return this;
- }
- remove( object ) {
- if ( arguments.length > 1 ) {
- for ( let i = 0; i < arguments.length; i ++ ) {
- this.remove( arguments[ i ] );
- }
- return this;
- }
- const index = this.children.indexOf( object );
- if ( index !== - 1 ) {
- object.parent = null;
- this.children.splice( index, 1 );
- object.dispatchEvent( _removedEvent );
- }
- return this;
- }
- removeFromParent() {
- const parent = this.parent;
- if ( parent !== null ) {
- parent.remove( this );
- }
- return this;
- }
- clear() {
- for ( let i = 0; i < this.children.length; i ++ ) {
- const object = this.children[ i ];
- object.parent = null;
- object.dispatchEvent( _removedEvent );
- }
- this.children.length = 0;
- return this;
- }
- attach( object ) {
- // adds object as a child of this, while maintaining the object's world transform
- // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s)
- this.updateWorldMatrix( true, false );
- _m1$1.copy( this.matrixWorld ).invert();
- if ( object.parent !== null ) {
- object.parent.updateWorldMatrix( true, false );
- _m1$1.multiply( object.parent.matrixWorld );
- }
- object.applyMatrix4( _m1$1 );
- this.add( object );
- object.updateWorldMatrix( false, true );
- return this;
- }
- getObjectById( id ) {
- return this.getObjectByProperty( 'id', id );
- }
- getObjectByName( name ) {
- return this.getObjectByProperty( 'name', name );
- }
- getObjectByProperty( name, value ) {
- if ( this[ name ] === value ) return this;
- for ( let i = 0, l = this.children.length; i < l; i ++ ) {
- const child = this.children[ i ];
- const object = child.getObjectByProperty( name, value );
- if ( object !== undefined ) {
- return object;
- }
- }
- return undefined;
- }
- getWorldPosition( target ) {
- this.updateWorldMatrix( true, false );
- return target.setFromMatrixPosition( this.matrixWorld );
- }
- getWorldQuaternion( target ) {
- this.updateWorldMatrix( true, false );
- this.matrixWorld.decompose( _position$3, target, _scale$2 );
- return target;
- }
- getWorldScale( target ) {
- this.updateWorldMatrix( true, false );
- this.matrixWorld.decompose( _position$3, _quaternion$2, target );
- return target;
- }
- getWorldDirection( target ) {
- this.updateWorldMatrix( true, false );
- const e = this.matrixWorld.elements;
- return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
- }
- raycast( /* raycaster, intersects */ ) {}
- traverse( callback ) {
- callback( this );
- const children = this.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- children[ i ].traverse( callback );
- }
- }
- traverseVisible( callback ) {
- if ( this.visible === false ) return;
- callback( this );
- const children = this.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- children[ i ].traverseVisible( callback );
- }
- }
- traverseAncestors( callback ) {
- const parent = this.parent;
- if ( parent !== null ) {
- callback( parent );
- parent.traverseAncestors( callback );
- }
- }
- updateMatrix() {
- this.matrix.compose( this.position, this.quaternion, this.scale );
- this.matrixWorldNeedsUpdate = true;
- }
- updateMatrixWorld( force ) {
- if ( this.matrixAutoUpdate ) this.updateMatrix();
- if ( this.matrixWorldNeedsUpdate || force ) {
- if ( this.parent === null ) {
- this.matrixWorld.copy( this.matrix );
- } else {
- this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
- }
- this.matrixWorldNeedsUpdate = false;
- force = true;
- }
- // update children
- const children = this.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- const child = children[ i ];
- if ( child.matrixWorldAutoUpdate === true || force === true ) {
- child.updateMatrixWorld( force );
- }
- }
- }
- updateWorldMatrix( updateParents, updateChildren ) {
- const parent = this.parent;
- if ( updateParents === true && parent !== null && parent.matrixWorldAutoUpdate === true ) {
- parent.updateWorldMatrix( true, false );
- }
- if ( this.matrixAutoUpdate ) this.updateMatrix();
- if ( this.parent === null ) {
- this.matrixWorld.copy( this.matrix );
- } else {
- this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
- }
- // update children
- if ( updateChildren === true ) {
- const children = this.children;
- for ( let i = 0, l = children.length; i < l; i ++ ) {
- const child = children[ i ];
- if ( child.matrixWorldAutoUpdate === true ) {
- child.updateWorldMatrix( false, true );
- }
- }
- }
- }
- toJSON( meta ) {
- // meta is a string when called from JSON.stringify
- const isRootObject = ( meta === undefined || typeof meta === 'string' );
- const output = {};
- // meta is a hash used to collect geometries, materials.
- // not providing it implies that this is the root object
- // being serialized.
- if ( isRootObject ) {
- // initialize meta obj
- meta = {
- geometries: {},
- materials: {},
- textures: {},
- images: {},
- shapes: {},
- skeletons: {},
- animations: {},
- nodes: {}
- };
- output.metadata = {
- version: 4.5,
- type: 'Object',
- generator: 'Object3D.toJSON'
- };
- }
- // standard Object3D serialization
- const object = {};
- object.uuid = this.uuid;
- object.type = this.type;
- object.choice = this.choice;
- if ( this.name !== '' ) object.name = this.name;
- if ( this.castShadow === true ) object.castShadow = true;
- if ( this.receiveShadow === true ) object.receiveShadow = true;
- if ( this.visible === false ) object.visible = false;
- // if ( this.choice === false ) object.choice = false;
- if ( this.frustumCulled === false ) object.frustumCulled = false;
- if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
- if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData;
- object.layers = this.layers.mask;
- object.matrix = this.matrix.toArray();
- if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
- // object specific properties
- if ( this.isInstancedMesh ) {
- object.type = 'InstancedMesh';
- object.count = this.count;
- object.instanceMatrix = this.instanceMatrix.toJSON();
- if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON();
- }
- //
- function serialize( library, element ) {
- if ( library[ element.uuid ] === undefined ) {
- library[ element.uuid ] = element.toJSON( meta );
- }
- return element.uuid;
- }
- if ( this.isScene ) {
- if ( this.background ) {
- if ( this.background.isColor ) {
- object.background = this.background.toJSON();
- } else if ( this.background.isTexture ) {
- object.background = this.background.toJSON( meta ).uuid;
- }
- }
- if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) {
- object.environment = this.environment.toJSON( meta ).uuid;
- }
- } else if ( this.isMesh || this.isLine || this.isPoints ) {
- object.geometry = serialize( meta.geometries, this.geometry );
- const parameters = this.geometry.parameters;
- if ( parameters !== undefined && parameters.shapes !== undefined ) {
- const shapes = parameters.shapes;
- if ( Array.isArray( shapes ) ) {
- for ( let i = 0, l = shapes.length; i < l; i ++ ) {
- const shape = shapes[ i ];
- serialize( meta.shapes, shape );
- }
- } else {
- serialize( meta.shapes, shapes );
- }
- }
- }
- if ( this.isSkinnedMesh ) {
- object.bindMode = this.bindMode;
- object.bindMatrix = this.bindMatrix.toArray();
- if ( this.skeleton !== undefined ) {
- serialize( meta.skeletons, this.skeleton );
- object.skeleton = this.skeleton.uuid;
- }
- }
- if ( this.material !== undefined ) {
- if ( Array.isArray( this.material ) ) {
- const uuids = [];
- for ( let i = 0, l = this.material.length; i < l; i ++ ) {
- uuids.push( serialize( meta.materials, this.material[ i ] ) );
- }
- object.material = uuids;
- } else {
- object.material = serialize( meta.materials, this.material );
- }
- }
- //
- if ( this.children.length > 0 ) {
- object.children = [];
- for ( let i = 0; i < this.children.length; i ++ ) {
- object.children.push( this.children[ i ].toJSON( meta ).object );
- }
- }
- //
- if ( this.animations.length > 0 ) {
- object.animations = [];
- for ( let i = 0; i < this.animations.length; i ++ ) {
- const animation = this.animations[ i ];
- object.animations.push( serialize( meta.animations, animation ) );
- }
- }
- if ( isRootObject ) {
- const geometries = extractFromCache( meta.geometries );
- const materials = extractFromCache( meta.materials );
- const textures = extractFromCache( meta.textures );
- const images = extractFromCache( meta.images );
- const shapes = extractFromCache( meta.shapes );
- const skeletons = extractFromCache( meta.skeletons );
- const animations = extractFromCache( meta.animations );
- const nodes = extractFromCache( meta.nodes );
- if ( geometries.length > 0 ) output.geometries = geometries;
- if ( materials.length > 0 ) output.materials = materials;
- if ( textures.length > 0 ) output.textures = textures;
- if ( images.length > 0 ) output.images = images;
- if ( shapes.length > 0 ) output.shapes = shapes;
- if ( skeletons.length > 0 ) output.skeletons = skeletons;
- if ( animations.length > 0 ) output.animations = animations;
- if ( nodes.length > 0 ) output.nodes = nodes;
- }
- output.object = object;
- return output;
- // extract data from the cache hash
- // remove metadata on each item
- // and return as array
- function extractFromCache( cache ) {
- const values = [];
- for ( const key in cache ) {
- const data = cache[ key ];
- delete data.metadata;
- values.push( data );
- }
- return values;
- }
- }
- clone( recursive ) {
- return new this.constructor().copy( this, recursive );
- }
- copy( source, recursive = true ) {
- this.name = source.name;
- this.up.copy( source.up );
- this.position.copy( source.position );
- this.rotation.order = source.rotation.order;
- this.quaternion.copy( source.quaternion );
- this.scale.copy( source.scale );
- this.matrix.copy( source.matrix );
- this.matrixWorld.copy( source.matrixWorld );
- this.matrixAutoUpdate = source.matrixAutoUpdate;
- this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
- this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate;
- this.layers.mask = source.layers.mask;
- this.visible = source.visible;
- this.choice = source.choice;
- this.castShadow = source.castShadow;
- this.receiveShadow = source.receiveShadow;
- this.frustumCulled = source.frustumCulled;
- this.renderOrder = source.renderOrder;
- this.userData = JSON.parse( JSON.stringify( source.userData ) );
- if ( recursive === true ) {
- for ( let i = 0; i < source.children.length; i ++ ) {
- const child = source.children[ i ];
- this.add( child.clone() );
- }
- }
- return this;
- }
- }
|