Skip to content

Instantly share code, notes, and snippets.

@Ni55aN
Last active November 7, 2021 22:58
Show Gist options
  • Save Ni55aN/90c017fafbefd3e31ef8d98ab6566cfa to your computer and use it in GitHub Desktop.
Save Ni55aN/90c017fafbefd3e31ef8d98ab6566cfa to your computer and use it in GitHub Desktop.
three.js smooth normals with crease angle
function calcNormal( normals, normal, angle ){
let allowed = normals.filter( n => n.angleTo( normal ) < angle * Math.PI / 180 );
return allowed.reduce( (a, b) => a.clone().add( b ) ).normalize();
}
THREE.GeometryUtils.computeVertexNormals = function(geometry, angle){
geometry.computeFaceNormals();
var vertices = geometry.vertices.map( () => [] ); // vertices with normals array
geometry.faces.map( face => {
vertices[ face.a ].push( face.normal );
vertices[ face.b ].push( face.normal );
vertices[ face.c ].push( face.normal );
});
geometry.faces.map( face => {
face.vertexNormals[ 0 ] = calcNormal( vertices[ face.a ], face.normal, angle );
face.vertexNormals[ 1 ] = calcNormal( vertices[ face.b ], face.normal, angle );
face.vertexNormals[ 2 ] = calcNormal( vertices[ face.c ], face.normal, angle );
});
if ( geometry.faces.length > 0 )
geometry.normalsNeedUpdate = true;
}
@Ni55aN
Copy link
Author

Ni55aN commented Nov 7, 2021

@xaliphostes nope, didn't work on that.
Maybe you can find some links in mrdoob/three.js#14859

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment