浏览代码

Added Geometry.computeFlatVertexNormals() (#9222)

* Added computeFlatVertexNormals()

* Compute face normals only when necessary
WestLangley 8 年之前
父节点
当前提交
e3c5619ffd
共有 2 个文件被更改,包括 43 次插入1 次删除
  1. 5 1
      docs/api/core/Geometry.html
  2. 38 0
      src/core/Geometry.js

+ 5 - 1
docs/api/core/Geometry.html

@@ -256,7 +256,11 @@
 		</div>
 		</div>
 		<div>
 		<div>
 		Computes vertex normals by averaging face normals.<br />
 		Computes vertex normals by averaging face normals.<br />
-		Face normals must be existing / computed beforehand.
+		</div>
+
+		<h3>[method:null computeFlatVertexNormals]()</h3>
+		<div>
+		Computes flat vertex normals. Sets the vertex normal of each vertex of each face to be the same as the face's normal.<br />
 		</div>
 		</div>
 
 
 		<h3>[method:null computeMorphNormals]()</h3>
 		<h3>[method:null computeMorphNormals]()</h3>

+ 38 - 0
src/core/Geometry.js

@@ -453,6 +453,8 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 
 		} else {
 		} else {
 
 
+			this.computeFaceNormals();
+
 			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 			for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
 
 
 				face = this.faces[ f ];
 				face = this.faces[ f ];
@@ -501,6 +503,42 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 
 	},
 	},
 
 
+	computeFlatVertexNormals: function () {
+
+		var f, fl, face;
+
+		this.computeFaceNormals();
+
+		for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
+
+			face = this.faces[ f ];
+
+			var vertexNormals = face.vertexNormals;
+
+			if ( vertexNormals.length === 3 ) {
+
+				vertexNormals[ 0 ].copy( face.normal );
+				vertexNormals[ 1 ].copy( face.normal );
+				vertexNormals[ 2 ].copy( face.normal );
+
+			} else {
+
+				vertexNormals[ 0 ] = face.normal.clone();
+				vertexNormals[ 1 ] = face.normal.clone();
+				vertexNormals[ 2 ] = face.normal.clone();
+
+			}
+
+		}
+
+		if ( this.faces.length > 0 ) {
+
+			this.normalsNeedUpdate = true;
+
+		}
+
+	},
+
 	computeMorphNormals: function () {
 	computeMorphNormals: function () {
 
 
 		var i, il, f, fl, face;
 		var i, il, f, fl, face;