Browse Source

tryKeepNormals added to JS module

Max Godefroy (Clyde) 4 năm trước cách đây
mục cha
commit
a003059a0b
1 tập tin đã thay đổi với 27 bổ sung1 xóa
  1. 27 1
      examples/js/modifiers/EdgeSplitModifier.js

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

@@ -7,6 +7,7 @@ THREE.EdgeSplitModifier = function () {
 	var positions, normals;
 	var indexes;
 	var pointToIndexMap, splitIndexes;
+	let oldNormals;
 
 
 	function computeNormals() {
@@ -150,7 +151,7 @@ THREE.EdgeSplitModifier = function () {
 	}
 
 
-	this.modify = function ( geometry, cutOffAngle ) {
+	this.modify = function ( geometry, cutOffAngle, tryKeepNormals = true ) {
 
 		const wasNotBufferGeometry = geometry.isBufferGeometry === undefined;
 		if ( ! geometry.isBufferGeometry ) {
@@ -161,6 +162,7 @@ THREE.EdgeSplitModifier = function () {
 
 
 		let hadNormals = false;
+		oldNormals = null;
 		if ( geometry.attributes.normal ) {
 
 			hadNormals = true;
@@ -168,6 +170,9 @@ THREE.EdgeSplitModifier = function () {
 			if ( wasNotBufferGeometry === false )
 				geometry = geometry.clone();
 
+			if ( tryKeepNormals && geometry.index )
+				oldNormals = geometry.attributes.normal.array;
+
 			geometry.deleteAttribute( 'normal' );
 
 		}
@@ -249,6 +254,27 @@ THREE.EdgeSplitModifier = function () {
 
 			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;