浏览代码

BatchedMesh Example: fix floating point error in rotation (#27046)

* Batched Mesh Example: fix case where decomposing caused rotation to settle on rotating on x axis

* Store matrices instead of euler objects
Garrett Johnson 1 年之前
父节点
当前提交
7905b2b4de
共有 1 个文件被更改,包括 11 次插入12 次删除
  1. 11 12
      examples/webgl_mesh_batch.html

+ 11 - 12
examples/webgl_mesh_batch.html

@@ -41,7 +41,7 @@
 		let camera, controls, scene, renderer;
 		let geometries, mesh;
 		const ids = [];
-		const dummy = new THREE.Object3D();
+		const matrix = new THREE.Matrix4();
 
 		//
 
@@ -172,6 +172,7 @@
 			const vertexCount = api.count * 512;
 			const indexCount = api.count * 1024;
 
+			const euler = new THREE.Euler();
 			const matrix = new THREE.Matrix4();
 			mesh = new BatchedMesh( geometryCount, vertexCount, indexCount, createMaterial() );
 			mesh.userData.rotationSpeeds = [];
@@ -181,7 +182,11 @@
 
 				const id = mesh.applyGeometry( geometries[ i % geometries.length ] );
 				mesh.setMatrixAt( id, randomizeMatrix( matrix ) );
-				mesh.userData.rotationSpeeds.push( randomizeRotationSpeed( new THREE.Euler() ) );
+
+				const rotationMatrix = new THREE.Matrix4();
+				rotationMatrix.makeRotationFromEuler( randomizeRotationSpeed( euler ) );
+				mesh.userData.rotationSpeeds.push( rotationMatrix );
+
 				ids.push( id );
 
 			}
@@ -274,18 +279,12 @@
 
 				for ( let i = 0; i < loopNum; i ++ ) {
 
-					const rotationSpeed = mesh.userData.rotationSpeeds[ i ];
+					const rotationMatrix = mesh.userData.rotationSpeeds[ i ];
 					const id = ids[ i ];
 
-					mesh.getMatrixAt( id, dummy.matrix );
-					dummy.matrix.decompose( dummy.position, dummy.quaternion, dummy.scale );
-					dummy.rotation.set(
-						dummy.rotation.x + rotationSpeed.x,
-						dummy.rotation.y + rotationSpeed.y,
-						dummy.rotation.z + rotationSpeed.z
-					);
-					dummy.updateMatrix();
-					mesh.setMatrixAt( id, dummy.matrix );
+					mesh.getMatrixAt( id, matrix );
+					matrix.multiply( rotationMatrix );
+					mesh.setMatrixAt( id, matrix );
 
 				}