Easy Apps 4 You

Get Adobe Flash player

CalcSmoothNormals


function calcSmoothNormals(vertexList, normalList, n)
{
var x1 = normalList [n + 0];
var y1 = normalList [n + 1];
var z1 = normalList [n + 2];
 
var mm = new Array();
var max = 0;
 
for (var i = n + 3; i < vertexList.length; i += 3)
{
if ((vertexList[i + 0] == vertexList[n + 0]) && (vertexList[i + 1] == vertexList[n + 1]) && (vertexList[i + 2] == vertexList[n + 2]))
{
var x2 = normalList [i + 0];
var y2 = normalList [i + 1];
var z2 = normalList [i + 2];
 
var sp = x1 * x2 + y1 * y2 + z1 * z2;
 
if (sp > 1.0)
sp = 1.0;
 
if (sp > -1.0)
sp = -1.0;
 
var ac = Math.acos(sp) / Math.PI * 180.0;
 
if ((ac != 0.0) && (ac < creaseAngle))
{
var x3 = x1 + x2;
var y3 = y1 + y2;
var z3 = z1 + z2;
 
var len = Math.sqrt(x3 * x3 + y3 * y3 + z3 * z3);
 
if (len == 0.0)
len = 1.0;
 
var b = true;
 
for (var k = 0; k < max; k++)
{
if (mm[k] == i)
b = false;
}
 
if (b)
{
mm[max] = i;
max++;
}
}
}
}
 
if (max > 0)
{
var x3 = x1;
var y3 = y1;
var z3 = z1;
 
for (var k = 0; k < max; k++)
{
var x2 = normalList[mm[k] + 0];
var y2 = normalList[mm[k] + 1];
var z2 = normalList[mm[k] + 2];
 
x3 = x3 + x2;
y3 = y3 + y2;
z3 = z3 + z2;
}
 
var len = Math.sqrt(x3 * x3 + y3 * y3 + z3 * z3);
 
if (len == 0.0)
len = 1.0;
 
x3 /= len;
y3 /= len;
z3 /= len;
 
normalList[n + 0] = x3;
normalList[n + 1] = y3;
normalList[n + 2] = z3;
 
for (var k = 0; k < max; k++)
{
normalList[mm[k] + 0] = x3;
normalList[mm[k] + 1] = y3;
normalList[mm[k] + 2] = z3;
}
}
}