Browse Source

EdgeSplitModifier - Recompute normals if normal attribute used by input

Max Godefroy (Clyde) 4 năm trước cách đây
mục cha
commit
16930a01a3

+ 22 - 1
examples/js/modifiers/EdgeSplitModifier.js

@@ -1,3 +1,4 @@
+import { BufferGeometry } from "../../../build/three.module";
 
 THREE.EdgeSplitModifier = function () {
 
@@ -153,9 +154,23 @@ THREE.EdgeSplitModifier = function () {
 
 	this.modify = function ( geometry, cutOffAngle ) {
 
+		const wasNotBufferGeometry = ! geometry.isBufferGeometry;
 		if ( ! geometry.isBufferGeometry ) {
 
-			geometry = new THREE.BufferGeometry().fromGeometry( geometry );
+			geometry = new BufferGeometry().fromGeometry( geometry );
+
+		}
+
+
+		let hadNormals = false;
+		if ( geometry.attributes.normal ) {
+
+			hadNormals = true;
+
+			if ( ! wasNotBufferGeometry )
+				geometry = geometry.clone();
+
+			geometry.deleteAttribute( 'normal' );
 
 		}
 
@@ -232,6 +247,12 @@ THREE.EdgeSplitModifier = function () {
 
 		}
 
+		if ( hadNormals ) {
+
+			geometry.computeVertexNormals();
+
+		}
+
 		return geometry;
 
 	};

+ 13 - 0
examples/jsm/modifiers/EdgeSplitModifier.js

@@ -160,6 +160,7 @@ var EdgeSplitModifier = function () {
 
 	this.modify = function ( geometry, cutOffAngle ) {
 
+		const wasNotBufferGeometry = ! geometry.isBufferGeometry;
 		if ( ! geometry.isBufferGeometry ) {
 
 			geometry = new BufferGeometry().fromGeometry( geometry );
@@ -167,8 +168,14 @@ var EdgeSplitModifier = function () {
 		}
 
 
+		let hadNormals = false;
 		if ( geometry.attributes.normal ) {
 
+			hadNormals = true;
+
+			if ( ! wasNotBufferGeometry )
+				geometry = geometry.clone();
+
 			geometry.deleteAttribute( 'normal' );
 
 		}
@@ -246,6 +253,12 @@ var EdgeSplitModifier = function () {
 
 		}
 
+		if ( hadNormals ) {
+
+			geometry.computeVertexNormals();
+
+		}
+
 		return geometry;
 
 	};

+ 1 - 6
examples/webgl_modifier_edgesplit.html

@@ -66,10 +66,7 @@
 					function ( group ) {
 
 						const cerberus = group.children[ 0 ];
-
-						// Retrieve Cerberus vertex positions only
 						const modelGeometry = cerberus.geometry;
-						modelGeometry.deleteAttribute( 'normal' );
 
 						modifier = new EdgeSplitModifier();
 						baseGeometry = modelGeometry;
@@ -140,12 +137,10 @@
 
 				} else {
 
-					geometry = BufferGeometryUtils.mergeVertices( baseGeometry );
+					geometry = baseGeometry;
 
 				}
 
-				geometry.computeVertexNormals();
-
 				return geometry;
 
 			}