瀏覽代碼

SimplifyModifier: cache vertex indices to speed faces (#22042)

* vertex indexing optimization for PR

* edit to jsm source as requested

* easy compromise: add newId (renamed from index for consistency) as a formal member

* consolidated id and newId as suggested; removed id constructor argument as irrelevant
NNskelly 4 年之前
父節點
當前提交
df136333c9
共有 2 個文件被更改,包括 14 次插入17 次删除
  1. 7 7
      examples/js/modifiers/SimplifyModifier.js
  2. 7 10
      examples/jsm/modifiers/SimplifyModifier.js

+ 7 - 7
examples/js/modifiers/SimplifyModifier.js

@@ -53,7 +53,7 @@
 			for ( let i = 0; i < positionAttribute.count; i ++ ) {
 
 				const v = new THREE.Vector3().fromBufferAttribute( positionAttribute, i );
-				const vertex = new Vertex( v, i );
+				const vertex = new Vertex( v );
 				vertices.push( vertex );
 
 			} // add faces
@@ -121,6 +121,8 @@
 
 				const vertex = vertices[ i ].position;
 				position.push( vertex.x, vertex.y, vertex.z );
+				// cache final index to GREATLY speed up faces reconstruction
+				vertices[ i ].id = i;
 
 			} //
 
@@ -128,10 +130,7 @@
 			for ( let i = 0; i < faces.length; i ++ ) {
 
 				const face = faces[ i ];
-				const a = vertices.indexOf( face.v1 );
-				const b = vertices.indexOf( face.v2 );
-				const c = vertices.indexOf( face.v3 );
-				index.push( a, b, c );
+				index.push( face.v1.id, face.v2.id, face.v3.id );
 
 			} //
 
@@ -440,10 +439,11 @@
 
 	class Vertex {
 
-		constructor( v, id ) {
+		constructor( v ) {
 
 			this.position = v;
-			this.id = id; // old index id
+			
+			this.id = -1; // external use position in vertices list (for e.g. face generation)
 
 			this.faces = []; // faces vertex is connected
 

+ 7 - 10
examples/jsm/modifiers/SimplifyModifier.js

@@ -64,7 +64,7 @@ class SimplifyModifier {
 
 			const v = new Vector3().fromBufferAttribute( positionAttribute, i );
 
-			const vertex = new Vertex( v, i );
+			const vertex = new Vertex( v );
 			vertices.push( vertex );
 
 		}
@@ -141,6 +141,8 @@ class SimplifyModifier {
 
 			const vertex = vertices[ i ].position;
 			position.push( vertex.x, vertex.y, vertex.z );
+			// cache final index to GREATLY speed up faces reconstruction
+			vertices[ i ].id = i;
 
 		}
 
@@ -149,12 +151,7 @@ class SimplifyModifier {
 		for ( let i = 0; i < faces.length; i ++ ) {
 
 			const face = faces[ i ];
-
-			const a = vertices.indexOf( face.v1 );
-			const b = vertices.indexOf( face.v2 );
-			const c = vertices.indexOf( face.v3 );
-
-			index.push( a, b, c );
+			index.push( face.v1.id, face.v2.id, face.v3.id );
 
 		}
 
@@ -500,11 +497,11 @@ class Triangle {
 
 class Vertex {
 
-	constructor( v, id ) {
+	constructor( v ) {
 
 		this.position = v;
-
-		this.id = id; // old index id
+			
+		this.id = -1; // external use position in vertices list (for e.g. face generation)
 
 		this.faces = []; // faces vertex is connected
 		this.neighbors = []; // neighbouring vertices aka "adjacentVertices"