Skip to content

Commit e3c5619

Browse files
WestLangleymrdoob
authored andcommitted
Added Geometry.computeFlatVertexNormals() (mrdoob#9222)
* Added computeFlatVertexNormals() * Compute face normals only when necessary
1 parent bf47cde commit e3c5619

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

docs/api/core/Geometry.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ <h3>[method:null computeVertexNormals]( [page:Boolean areaWeighted] )</h3>
256256
</div>
257257
<div>
258258
Computes vertex normals by averaging face normals.<br />
259-
Face normals must be existing / computed beforehand.
259+
</div>
260+
261+
<h3>[method:null computeFlatVertexNormals]()</h3>
262+
<div>
263+
Computes flat vertex normals. Sets the vertex normal of each vertex of each face to be the same as the face's normal.<br />
260264
</div>
261265

262266
<h3>[method:null computeMorphNormals]()</h3>

src/core/Geometry.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
453453

454454
} else {
455455

456+
this.computeFaceNormals();
457+
456458
for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
457459

458460
face = this.faces[ f ];
@@ -501,6 +503,42 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
501503

502504
},
503505

506+
computeFlatVertexNormals: function () {
507+
508+
var f, fl, face;
509+
510+
this.computeFaceNormals();
511+
512+
for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
513+
514+
face = this.faces[ f ];
515+
516+
var vertexNormals = face.vertexNormals;
517+
518+
if ( vertexNormals.length === 3 ) {
519+
520+
vertexNormals[ 0 ].copy( face.normal );
521+
vertexNormals[ 1 ].copy( face.normal );
522+
vertexNormals[ 2 ].copy( face.normal );
523+
524+
} else {
525+
526+
vertexNormals[ 0 ] = face.normal.clone();
527+
vertexNormals[ 1 ] = face.normal.clone();
528+
vertexNormals[ 2 ] = face.normal.clone();
529+
530+
}
531+
532+
}
533+
534+
if ( this.faces.length > 0 ) {
535+
536+
this.normalsNeedUpdate = true;
537+
538+
}
539+
540+
},
541+
504542
computeMorphNormals: function () {
505543

506544
var i, il, f, fl, face;

0 commit comments

Comments
 (0)