Lines3D类一个让我困惑的问题
在我做的其中一个关于Lines3D实验时候碰到需要动态改变line的始末座标的问题。看了一下lines3D类,渲染方式也是采用仿射变换实现。在实例化lines3D时,提供了几个参数,其中包括有始末顶点座标v0和v1(Vertex3D类)。猜想如果能改变这两个座标,是否能实现线条的跳动效果?但答案竟然是否定。我使用如下代码:
for each(var line:Line3D in lines.lines)
{
line.v0 = new Vertex3D(nx0, ny0, nz0);
line.v1 = new Vertex3D(nx1, ny1, nz1);
}
{
line.v0 = new Vertex3D(nx0, ny0, nz0);
line.v1 = new Vertex3D(nx1, ny1, nz1);
}
发现line消失了,这正是我不解之处。查看了几个相关的父类的project方法,没发现原因。根据每帧的重新渲染,假如改变始末座标,应该能正常达到目的。暂时还是放下这个问题,尝试用另外一种方法去实现。我尝试每帧把所有line remove掉,再重新绘制。我使用如下代码:
lines.lines.length = 0;
var x0:Number = 0;
var y0:Number = 0;
var z0:Number = 0;
var pv:Vertex3D = new Vertex3D(x0,y0,z0);
var nv:Vertex3D;
for(var n:Number = 0; n <= 10; n ++)
{
nv = new Vertex3D(x0 + 50*n, Math.random() * 300 - 150 + 50*n, Math.random() * 300 - 150 + 50*n);
lines.addNewLine(1, pv.x, pv.y, pv.z, nv.x, nv.y, nv.z);
pv = nv;
}
var y0:Number = 0;
var z0:Number = 0;
var pv:Vertex3D = new Vertex3D(x0,y0,z0);
var nv:Vertex3D;
for(var n:Number = 0; n <= 10; n ++)
{
nv = new Vertex3D(x0 + 50*n, Math.random() * 300 - 150 + 50*n, Math.random() * 300 - 150 + 50*n);
lines.addNewLine(1, pv.x, pv.y, pv.z, nv.x, nv.y, nv.z);
pv = nv;
}
OK,目的似乎达到。但发现内存使用疯狂上升,增量达到1m/s。大概1分钟后,直接卡死了。猜想是否在仿射变换后,绘制到canvas的线条没有被清除?追究了一番后,发现问题不是出在这里。Back to basic,从addNewLine方法继续网上追踪,发现在Lines3D类的addLine方法:
lines.push(line);
line.instance = this;
if(geometry.vertices.indexOf(line.v0) == -1)
{
geometry.vertices.push(line.v0);
}
if(geometry.vertices.indexOf(line.v1) == -1)
{
geometry.vertices.push(line.v1);
}
if(line.cV){
if(geometry.vertices.indexOf(line.cV) == -1)
{
geometry.vertices.push(line.cV);
}
}
line.instance = this;
if(geometry.vertices.indexOf(line.v0) == -1)
{
geometry.vertices.push(line.v0);
}
if(geometry.vertices.indexOf(line.v1) == -1)
{
geometry.vertices.push(line.v1);
}
if(line.cV){
if(geometry.vertices.indexOf(line.cV) == -1)
{
geometry.vertices.push(line.cV);
}
}
geometry.vertices喺不断push,难怪内存使用上升得十分快。看来问题找到了。解决方法也很简单:
lines.lines.length = 0;
lines.geometry.vertices.length = 0;
var x0:Number = 0;
var y0:Number = 0;
var z0:Number = 0;
var pv:Vertex3D = new Vertex3D(x0,y0,z0);
var nv:Vertex3D;
for(var n:Number = 0; n <= 10; n ++)
{
nv = new Vertex3D(x0 + 50*n, Math.random() * 300 - 150 + 50*n, Math.random() * 300 - 150 + 50*n);
lines.addNewLine(1, pv.x, pv.y, pv.z, nv.x, nv.y, nv.z);
pv = nv;
}
lines.geometry.vertices.length = 0;
var x0:Number = 0;
var y0:Number = 0;
var z0:Number = 0;
var pv:Vertex3D = new Vertex3D(x0,y0,z0);
var nv:Vertex3D;
for(var n:Number = 0; n <= 10; n ++)
{
nv = new Vertex3D(x0 + 50*n, Math.random() * 300 - 150 + 50*n, Math.random() * 300 - 150 + 50*n);
lines.addNewLine(1, pv.x, pv.y, pv.z, nv.x, nv.y, nv.z);
pv = nv;
}
内存使用基本稳定下来了。接下来尝试做一些基于lines3D的特效,下回分解。