Просмотр исходного кода

Object3D: Added pivot property. (#32745)

mrdoob 1 месяц назад
Родитель
Сommit
30776bb7ce

+ 7 - 39
examples/jsm/loaders/LWOLoader.js

@@ -23,7 +23,8 @@ import {
 	RepeatWrapping,
 	RepeatWrapping,
 	SRGBColorSpace,
 	SRGBColorSpace,
 	TextureLoader,
 	TextureLoader,
-	Vector2
+	Vector2,
+	Vector3
 } from 'three';
 } from 'three';
 
 
 import { IFFParser } from './lwo/IFFParser.js';
 import { IFFParser } from './lwo/IFFParser.js';
@@ -185,8 +186,6 @@ class LWOTreeParser {
 
 
 		} );
 		} );
 
 
-		this.applyPivots( finalMeshes );
-
 		return finalMeshes;
 		return finalMeshes;
 
 
 	}
 	}
@@ -204,38 +203,14 @@ class LWOTreeParser {
 		if ( layer.name ) mesh.name = layer.name;
 		if ( layer.name ) mesh.name = layer.name;
 		else mesh.name = this.defaultLayerName + '_layer_' + layer.number;
 		else mesh.name = this.defaultLayerName + '_layer_' + layer.number;
 
 
-		mesh.userData.pivot = layer.pivot;
-
-		return mesh;
-
-	}
-
-	// TODO: may need to be reversed in z to convert LWO to three.js coordinates
-	applyPivots( meshes ) {
-
-		meshes.forEach( function ( mesh ) {
-
-			mesh.traverse( function ( child ) {
-
-				const pivot = child.userData.pivot;
-
-				child.position.x += pivot[ 0 ];
-				child.position.y += pivot[ 1 ];
-				child.position.z += pivot[ 2 ];
+		const pivot = layer.pivot;
+		if ( pivot[ 0 ] !== 0 || pivot[ 1 ] !== 0 || pivot[ 2 ] !== 0 ) {
 
 
-				if ( child.parent ) {
+			mesh.pivot = new Vector3( pivot[ 0 ], pivot[ 1 ], pivot[ 2 ] );
 
 
-					const parentPivot = child.parent.userData.pivot;
-
-					child.position.x -= parentPivot[ 0 ];
-					child.position.y -= parentPivot[ 1 ];
-					child.position.z -= parentPivot[ 2 ];
-
-				}
-
-			} );
+		}
 
 
-		} );
+		return mesh;
 
 
 	}
 	}
 
 
@@ -813,13 +788,6 @@ class GeometryParser {
 		this.parseUVs( geometry, layer );
 		this.parseUVs( geometry, layer );
 		this.parseMorphTargets( geometry, layer );
 		this.parseMorphTargets( geometry, layer );
 
 
-		// TODO: z may need to be reversed to account for coordinate system change
-		geometry.translate( - layer.pivot[ 0 ], - layer.pivot[ 1 ], - layer.pivot[ 2 ] );
-
-		// let userData = geometry.userData;
-		// geometry = geometry.toNonIndexed()
-		// geometry.userData = userData;
-
 		return geometry;
 		return geometry;
 
 
 	}
 	}

+ 8 - 0
examples/jsm/loaders/usd/USDComposer.js

@@ -19,6 +19,7 @@ import {
 	Bone,
 	Bone,
 	SRGBColorSpace,
 	SRGBColorSpace,
 	Texture,
 	Texture,
+	Vector3,
 	VectorKeyframeTrack
 	VectorKeyframeTrack
 } from 'three';
 } from 'three';
 
 
@@ -256,6 +257,13 @@ class USDComposer {
 
 
 		}
 		}
 
 
+		if ( data[ 'xformOp:translate:pivot' ] ) {
+
+			const p = data[ 'xformOp:translate:pivot' ];
+			obj.pivot = new Vector3( p[ 0 ], p[ 1 ], p[ 2 ] );
+
+		}
+
 		if ( data[ 'xformOp:scale' ] ) {
 		if ( data[ 'xformOp:scale' ] ) {
 
 
 			const s = data[ 'xformOp:scale' ];
 			const s = data[ 'xformOp:scale' ];

+ 31 - 0
src/core/Object3D.js

@@ -375,6 +375,16 @@ class Object3D extends EventDispatcher {
 		 */
 		 */
 		this.userData = {};
 		this.userData = {};
 
 
+		/**
+		 * The pivot point for rotation and scale transformations.
+		 * When set, rotation and scale are applied around this point
+		 * instead of the object's origin.
+		 *
+		 * @type {?Vector3}
+		 * @default null
+		 */
+		this.pivot = null;
+
 	}
 	}
 
 
 	/**
 	/**
@@ -1122,6 +1132,19 @@ class Object3D extends EventDispatcher {
 
 
 		this.matrix.compose( this.position, this.quaternion, this.scale );
 		this.matrix.compose( this.position, this.quaternion, this.scale );
 
 
+		const pivot = this.pivot;
+
+		if ( pivot !== null ) {
+
+			const px = pivot.x, py = pivot.y, pz = pivot.z;
+			const te = this.matrix.elements;
+
+			te[ 12 ] += px - te[ 0 ] * px - te[ 4 ] * py - te[ 8 ] * pz;
+			te[ 13 ] += py - te[ 1 ] * px - te[ 5 ] * py - te[ 9 ] * pz;
+			te[ 14 ] += pz - te[ 2 ] * px - te[ 6 ] * py - te[ 10 ] * pz;
+
+		}
+
 		this.matrixWorldNeedsUpdate = true;
 		this.matrixWorldNeedsUpdate = true;
 
 
 	}
 	}
@@ -1287,6 +1310,8 @@ class Object3D extends EventDispatcher {
 		object.matrix = this.matrix.toArray();
 		object.matrix = this.matrix.toArray();
 		object.up = this.up.toArray();
 		object.up = this.up.toArray();
 
 
+		if ( this.pivot !== null ) object.pivot = this.pivot.toArray();
+
 		if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
 		if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
 
 
 		// object specific properties
 		// object specific properties
@@ -1562,6 +1587,12 @@ class Object3D extends EventDispatcher {
 		this.quaternion.copy( source.quaternion );
 		this.quaternion.copy( source.quaternion );
 		this.scale.copy( source.scale );
 		this.scale.copy( source.scale );
 
 
+		if ( source.pivot !== null ) {
+
+			this.pivot = source.pivot.clone();
+
+		}
+
 		this.matrix.copy( source.matrix );
 		this.matrix.copy( source.matrix );
 		this.matrixWorld.copy( source.matrixWorld );
 		this.matrixWorld.copy( source.matrixWorld );
 
 

+ 3 - 0
src/loaders/ObjectLoader.js

@@ -19,6 +19,7 @@ import {
 } from '../constants.js';
 } from '../constants.js';
 import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
 import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
 import { Color } from '../math/Color.js';
 import { Color } from '../math/Color.js';
+import { Vector3 } from '../math/Vector3.js';
 import { Object3D } from '../core/Object3D.js';
 import { Object3D } from '../core/Object3D.js';
 import { Group } from '../objects/Group.js';
 import { Group } from '../objects/Group.js';
 import { InstancedMesh } from '../objects/InstancedMesh.js';
 import { InstancedMesh } from '../objects/InstancedMesh.js';
@@ -1114,6 +1115,8 @@ class ObjectLoader extends Loader {
 
 
 		if ( data.up !== undefined ) object.up.fromArray( data.up );
 		if ( data.up !== undefined ) object.up.fromArray( data.up );
 
 
+		if ( data.pivot !== undefined ) object.pivot = new Vector3().fromArray( data.pivot );
+
 		if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
 		if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
 		if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
 		if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
 
 

粤ICP备19079148号