It seems that the THREE.Geometry methods, .computeFaceNormals() & .computeVertexNormals(), assign values to a built-in attribute array called "normal."
If I want to utilize both vertex- & face-normals in one shader, I need to:
- Calculate face-normals
- Copy the results from 'normals' to a new attribute array, like 'fNormals'
- Compute vertex-normals
- Render...
Currently, following this method, my attempt looks like this:
var shader = new THREE.ShaderMaterial(
{
attributes: {
fNormal: { type: "v3", value: [] }
},
//uniforms, vertexShader, etc...
} );
//load a mesh & apply the shader to it...
//set up both vertex & face normals for use in the shader
mesh.geometry.computeFaceNormals();
//copy the face-normals to the 'fNormal' array:
for (var i=0; i < mesh.geometry.attributes[ 'normal' ].length; i++){
var cloned = mesh.geometry.attributes[ 'normal' ].value[i];
shader.attributes[ 'fNormal' ].value[i] = cloned;
}
//now compute vertex normals and store them in the 'normal' attribute:
mesh.geometry.computeVertexNormals();
EDIT: Correcting errors as I debug. Still figuring out how to handle the generated normals...
Appreciate any insights-