我有一个使用Three.js对3D对象进行着色的功能。我的变量j存在,如果我使用alert(j),它可以工作,但是当我使用这个变量作为我的数组的索引(colormap)时,它不能。
colormap=[[0,0,0.5625],[0,0,0.625],[0,0,0.6875],[0,0,0.75],[0,0,0.8125],[0,0,0.875],[0,0,0.9375],[0,0,1],[0,0.0625,1],[0,0.125,1],[0,0.1875,1],[0,0.25,1],[0,0.3125,1],[0,0.375,1],[0,0.4375,1],[0,0.5,1],[0,0.5625,1],[0,0.625,1],[0,0.6875,1],[0,0.75,1],[0,0.8125,1],[0,0.875,1],[0,0.9375,1],[0,1,1],[0.0625,1,0.9375],[0.125,1,0.875],[0.1875,1,0.8125],[0.25,1,0.75],[0.3125,1,0.6875],[0.375,1,0.625],[0.4375,1,0.5625],[0.5,1,0.5],[0.5625,1,0.4375],[0.625,1,0.375],[0.6875,1,0.3125],[0.75,1,0.25],[0.8125,1,0.1875],[0.875,1,0.125],[0.9375,1,0.0625],[1,1,0],[1,0.9375,0],[1,0.875,0],[1,0.8125,0],[1,0.75,0],[1,0.6875,0],[1,0.625,0],[1,0.5625,0],[1,0.5,0],[1,0.4375,0],[1,0.375,0],[1,0.3125,0],[1,0.25,0],[1,0.1875,0],[1,0.125,0],[1,0.0625,0],[1,0,0],[0.9375,0,0],[0.875,0,0],[0.8125,0,0],[0.75,0,0],[0.6875,0,0],[0.625,0,0],[0.5625,0,0],[0.5,0,0]];
j = 0;// indice colore nella matrice
var callbackMale = function ( geometry, materials )
{
// surf contain surface
var cmin=surf.min(), cmax=surf.max(), colorLength=colormap.length;
for ( var i = 0, l = geometry.vertices.length; i < l; i ++ )
{
var face = geometry.faces[ i ];
j = Math.round(((surf[i]-cmin)/(cmax-cmin)*colorLength));
if(j < 0)
j=0;
else if( j >= colorLength)
j=colorLength;
alert(j)
var ind = colormap[j]; // j undefined
for(var k = 0 ; k < 3 ; k++)
{
face.vertexColors[ k ] = new THREE.Color().setRGB(ind[0],ind[1],ind[2]);
}//fine for interno
}//fine for esterno
}发布于 2013-03-14 18:10:30
j的类型是什么?整型、浮点型还是其他类型?我认为你试图将一个浮点数放入一个整型数中,但它不起作用。
发布于 2013-03-14 18:13:46
您的循环将超出数组的界限,从而导致来自colormap[j]的未定义的值。
else if( j >= colorLength)
j=colorLength;把你的值jsut作为数组的边界,因为数组是从零开始的。将最大值调整为小一:
else if( j >= colorLength)
j=colorLength-1;https://stackoverflow.com/questions/15406175
复制相似问题