Easy Apps 4 You

Get Adobe Flash player

CalcNormals


function calcNormals(vertexList, normalList, i)
{
var vertex1 = [vertexList[i + 0], vertexList[i + 1], vertexList[i + 2]];
var vertex2 = [vertexList[i + 3], vertexList[i + 4], vertexList[i + 5]];
var vertex3 = [vertexList[i + 6], vertexList[i + 7], vertexList[i + 8]];
 
var ax = vertex1[0] - vertex2[0];
var ay = vertex1[1] - vertex2[1];
var az = vertex1[2] - vertex2[2];
 
var bx = vertex2[0] - vertex3[0];
var by = vertex2[1] - vertex3[1];
var bz = vertex2[2] - vertex3[2];
 
var nx = ay * bz - az * by;
var ny = az * bx - ax * bz;
var nz = ax * by - ay * bx;
 
var len = Math.sqrt(nx * nx + ny * ny + nz * nz);
 
if (len == 0)
{
len = 1;
}
 
normalList.push(nx / len, ny / len, nz / len);
normalList.push(nx / len, ny / len, nz / len);
normalList.push(nx / len, ny / len, nz / len);
}