Browse Source

Fix rotation issue

Garrett Johnson 5 years ago
parent
commit
80a94c5458
2 changed files with 25 additions and 17 deletions
  1. 5 1
      example/i3dmExample.js
  2. 20 16
      src/three/I3DMLoader.js

+ 5 - 1
example/i3dmExample.js

@@ -36,7 +36,7 @@ function init() {
 	document.body.appendChild( renderer.domElement );
 
 	camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 4000 );
-	camera.position.set( 20, 20, 20 );
+	camera.position.set( 100, 100, 100 );
 
 	// controls
 	controls = new OrbitControls( camera, renderer.domElement );
@@ -82,6 +82,8 @@ function init() {
 
 			if ( instance ) {
 
+				res.scene.updateMatrixWorld( true );
+
 				const pos = new Vector3();
 				const quat = new Quaternion();
 				const sca = new Vector3();
@@ -91,6 +93,7 @@ function init() {
 				for ( let i = 0, l = instance.count; i < l; i ++ ) {
 
 					instance.getMatrixAt( i, mat );
+					mat.premultiply( instance.matrixWorld );
 					mat.decompose( pos, quat, sca );
 					averagePos.add( pos );
 
@@ -98,6 +101,7 @@ function init() {
 
 				averagePos.divideScalar( instance.count );
 				controls.target.copy( averagePos );
+				camera.position.add( averagePos )
 				controls.update();
 
 			}

+ 20 - 16
src/three/I3DMLoader.js

@@ -46,31 +46,35 @@ export class I3DMLoader extends I3DMLoaderBase {
 						// SCALE_NON_UNIFORM
 						// BATCH_ID
 
-						let instances = [];
-						const traverse = c => {
+						const instanceMap = new Map();
+						const instances = [];
+						model.scene.traverse( child => {
 
-							const children = c.children;
-							for ( let i = 0, l = children.length; i < l; i ++ ) {
+							if ( child.isMesh ) {
 
-								const child = children[ i ];
-								if ( child.isMesh ) {
+								const { geometry, material } = child;
+								const instancedMesh = new InstancedMesh( geometry, material, INSTANCES_LENGTH );
+								instances.push( instancedMesh );
+								instanceMap.set( child, instancedMesh );
 
-									// TODO: Why is the model being rotated after?
-									const { geometry, material } = child;
-									const instancedMesh = new InstancedMesh( geometry, material, INSTANCES_LENGTH );
-									children[ i ] = instancedMesh;
-									instances.push( instancedMesh );
+							}
+
+						} );
 
-								} else {
+						// replace the meshes with instanced meshes
+						instanceMap.forEach( ( instancedMesh, mesh ) => {
 
-									traverse( child );
+							const parent = mesh.parent;
+							if ( parent ) {
 
-								}
+								// Mesh have no children
+								parent.remove( mesh );
+								parent.add( instancedMesh );
 
 							}
 
-						};
-						traverse( model.scene );
+
+						} );
 
 						for ( let i = 0; i < INSTANCES_LENGTH; i ++ ) {