Flash CS4 3D Sprite 旋转 Z 顺序问题
我怀疑这是一个数学问题而不是闪光问题。 简而言之,我有两个围绕其共同中心点旋转的立方体。 到目前为止,一切都很好。 使用appendTranslation和appendRotation矩阵函数,我成功地让两个立方体旋转正常。
问题是绘制顺序似乎对 Z 缓冲影响不大。 我已经成功地正确设置了 Z 缓冲区并按深度顺序对立方体进行排序。 然而,尽管绘制顺序正确,球杆只有一半的时间被正确遮挡。
想象一下 x 轴平面上的两个立方体,两侧等间距绕 Y 轴旋转。 在 90 度旋转时,一个立方体将遮挡另一个立方体,在 270 度旋转时反之亦然。 我的问题是,这种情况仅在这些地方之一正确发生。 这似乎是一个符号问题,因为向后旋转(因此 -90 和 -270)仅在相反的位置具有相同的效果。
这是相关的代码:
PixelCubeGroup.as - 旋转在每个周期被调用
function rotateObj(rotx:Number,roty:Number,rotz:Number):void {
var i:int;
for(i=0; i<pixelVec.length; i++){
var v:Vector3D = centerPos;
v = v.subtract(pixelVec[i].getPos());
pixelVec[i].rotateObj(rotx,roty,rotz,v);
}
}
PixelCube.as 绘制和旋转的东西
function rotateObj(xrot:Number,yrot:Number,zrot:Number, pivot:Vector3D){
// rotate the cube by changing the values in the matrix 3D around a pivot point
m.identity();
m.appendTranslation(-pivot.x,-pivot.y, -pivot.z);
m.appendRotation(xrot, Vector3D.X_AXIS);
m.appendRotation(yrot, Vector3D.Y_AXIS);
m.appendRotation(zrot, Vector3D.Z_AXIS);
m.appendTranslation(pivot.x,pivot.y,pivot.z);
}
function draw():void
{
var renderV:Vector.<Number> = new Vector.<Number>();
currentV = new Vector.<Vector3D>();
for each(var vv:Vector3D in v)
{
// apply the matrix to the vertices in 'v'
vv = m.transformVector(vv);
vv.w = (400 + vv.z) / 400;
vv.project();
renderV.push(vv.x, vv.y);
currentV.push(vv);
}
// draw things out
container.graphics.clear();
container.x = xPos;
container.y = yPos;
container.z = 1;
var id = 0
plane.sort(sortByZ);
for each(var p:Vector.<int> in plane)
{
container.graphics.beginFill(palleteColours[id],1.0);
var planeV:Vector.<Number> = new Vector.<Number>();
for (var i:int = 0; i < planeSides; i++ )
{
planeV.push(renderV[p[i]* 2], renderV[p[i] * 2 + 1]);
}
container.graphics.drawTriangles(planeV, indices, null, TriangleCulling.NEGATIVE );
container.graphics.endFill();
id ++;
}
}
我已经设法将其范围缩小到平移线。 在不平移的情况下执行两个立方体的旋转效果很好并且尊重绘图顺序,而这则不然。 我怀疑 Z Value 正在潜入某个地方。
I suspect this is a maths issue rather than a flash one. In a nutshell, I have two cubes that rotate around their common center point. So far so good. Using the appendTranslation and appendRotation matrix functions, I've managed to get the two cubes to rotate ok.
The problem is the drawing order seems to have little effect on the Z Buffering. I've managed to Z Buffer correctly and order the cubes in order of depth. However, despite getting the drawing order right, the cueb is only properly occluded half of the time.
Imagine two cubes on the x-axis plane, either side at equal spacing rotating around the Y axis. At 90 degrees one cube will occlude the other and vice-versa at 270 degrees rotation. My problem is that this occurs correctly only at one of these places. It appears to be a sign issue as rotating backwards (so -90 and -270) has the same effect only at the opposite places.
Here is the relevant code:
PixelCubeGroup.as - Rotate is called each cycle
function rotateObj(rotx:Number,roty:Number,rotz:Number):void {
var i:int;
for(i=0; i<pixelVec.length; i++){
var v:Vector3D = centerPos;
v = v.subtract(pixelVec[i].getPos());
pixelVec[i].rotateObj(rotx,roty,rotz,v);
}
}
PixelCube.as The things that are drawn and rotated
function rotateObj(xrot:Number,yrot:Number,zrot:Number, pivot:Vector3D){
// rotate the cube by changing the values in the matrix 3D around a pivot point
m.identity();
m.appendTranslation(-pivot.x,-pivot.y, -pivot.z);
m.appendRotation(xrot, Vector3D.X_AXIS);
m.appendRotation(yrot, Vector3D.Y_AXIS);
m.appendRotation(zrot, Vector3D.Z_AXIS);
m.appendTranslation(pivot.x,pivot.y,pivot.z);
}
function draw():void
{
var renderV:Vector.<Number> = new Vector.<Number>();
currentV = new Vector.<Vector3D>();
for each(var vv:Vector3D in v)
{
// apply the matrix to the vertices in 'v'
vv = m.transformVector(vv);
vv.w = (400 + vv.z) / 400;
vv.project();
renderV.push(vv.x, vv.y);
currentV.push(vv);
}
// draw things out
container.graphics.clear();
container.x = xPos;
container.y = yPos;
container.z = 1;
var id = 0
plane.sort(sortByZ);
for each(var p:Vector.<int> in plane)
{
container.graphics.beginFill(palleteColours[id],1.0);
var planeV:Vector.<Number> = new Vector.<Number>();
for (var i:int = 0; i < planeSides; i++ )
{
planeV.push(renderV[p[i]* 2], renderV[p[i] * 2 + 1]);
}
container.graphics.drawTriangles(planeV, indices, null, TriangleCulling.NEGATIVE );
container.graphics.endFill();
id ++;
}
}
I've managed to narrow it down to the translation lines. Performing a rotation of two cubes without translation works fine and respects the drawing order, whereas this does not. I suspect a rogue Z Value is getting in there somewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
排序了。 显然,绘图/Z 顺序(如果您愿意)由调用 addChild() 和 removeChild() 的顺序控制,而不是由任何图形调用的顺序控制。 Adobe 有点傻!
Sorted it. Apparently, the drawing / Z order if you will is governed by the order that addChild() and removeChild() are called, rather than the order of any graphics calls. Bit silly there Adobe!