Graphics Reference
In-Depth Information
cube.vertices.length ; i++) {
avgVertexNormals.push(new
THREE.Vector3(0,0,0));
avgVertexCount.push(0);
}
// first add all the normals
cube.faces.forEach(function (f) {
var vA = f.vertexNormals[0];
var vB = f.vertexNormals[1];
var vC = f.vertexNormals[2];
// update the count
avgVertexCount[f.a]+=1;
avgVertexCount[f.b]+=1;
avgVertexCount[f.c]+=1;
// add the vector
avgVertexNormals[f.a].add(vA);
avgVertexNormals[f.b].add(vB);
avgVertexNormals[f.c].add(vC);
});
// then calculate the average
for (var i = 0 ; i <
avgVertexNormals.length ; i++) {
avgVertexNormals[i].divideScalar(avgVertexCount[i]);
}
We won't explain this code snippet in detail, but what we do here is that we
calculate the normal vector of each vertex based on the normal vectors of the
faces the particular vector is part of. The final normal vector is stored in the
avgVertexNormals array.
4. Next, we look at a helper function that we'll call from the render loop in the
next step. This function determines the new position of each vertex based on
the velocity function we defined in step 1 and the normal vector we calculated
in step 3:
Search WWH ::




Custom Search