Selaa lähdekoodia

Added tryKeepNormals options

Max Godefroy (Clyde) 4 vuotta sitten
vanhempi
commit
53158c094d
1 muutettua tiedostoa jossa 28 lisäystä ja 1 poistoa
  1. 28 1
      examples/jsm/modifiers/EdgeSplitModifier.js

+ 28 - 1
examples/jsm/modifiers/EdgeSplitModifier.js

@@ -16,6 +16,8 @@ var EdgeSplitModifier = function () {
 	var indexes;
 	var indexes;
 	var pointToIndexMap, splitIndexes;
 	var pointToIndexMap, splitIndexes;
 
 
+	let oldNormals;
+
 
 
 	function computeNormals() {
 	function computeNormals() {
 
 
@@ -158,7 +160,7 @@ var EdgeSplitModifier = function () {
 	}
 	}
 
 
 
 
-	this.modify = function ( geometry, cutOffAngle ) {
+	this.modify = function ( geometry, cutOffAngle, tryKeepNormals = true ) {
 
 
 		const wasNotBufferGeometry = geometry.isBufferGeometry === undefined;
 		const wasNotBufferGeometry = geometry.isBufferGeometry === undefined;
 		if ( ! geometry.isBufferGeometry ) {
 		if ( ! geometry.isBufferGeometry ) {
@@ -169,6 +171,7 @@ var EdgeSplitModifier = function () {
 
 
 
 
 		let hadNormals = false;
 		let hadNormals = false;
+		oldNormals = null;
 		if ( geometry.attributes.normal ) {
 		if ( geometry.attributes.normal ) {
 
 
 			hadNormals = true;
 			hadNormals = true;
@@ -176,6 +179,9 @@ var EdgeSplitModifier = function () {
 			if ( wasNotBufferGeometry === false )
 			if ( wasNotBufferGeometry === false )
 				geometry = geometry.clone();
 				geometry = geometry.clone();
 
 
+			if ( tryKeepNormals && geometry.index )
+				oldNormals = geometry.attributes.normal.array;
+
 			geometry.deleteAttribute( 'normal' );
 			geometry.deleteAttribute( 'normal' );
 
 
 		}
 		}
@@ -257,6 +263,27 @@ var EdgeSplitModifier = function () {
 
 
 			geometry.computeVertexNormals();
 			geometry.computeVertexNormals();
 
 
+			if ( oldNormals !== null ) {
+
+				const changedNormals = new Array( oldNormals.length / 3 ).fill( false );
+
+				for ( const splitData of splitIndexes )
+					changedNormals[ splitData.original ] = true;
+
+				for ( let i = 0; i < changedNormals.length; i ++ ) {
+
+					if ( changedNormals[ i ] === false ) {
+
+						for ( let j = 0; j < 3; j ++ )
+							geometry.attributes.normal.array[ 3 * i + j ] = oldNormals[ 3 * i + j ];
+
+					}
+
+				}
+
+
+			}
+
 		}
 		}
 
 
 		return geometry;
 		return geometry;