AS3:计算3D旋转对象的精确高度

时间:2016-01-14 23:59:01

标签: actionscript-3 matrix projection

我正在尝试计算3D旋转形状的确切高度,并考虑其perspectiveProjection。我意识到有很多方法可以获得良好的近似,比如使用localToGlobal,甚至只使用它的height属性,但所有这些方法都返回一个整数,我需要更精确的东西。以下是对该问题的更详细解释:

rotations 考虑一下这个红色框和黑色条,我试图计算确切的高度:

const w:Number = 100, h:Number = 100;
var redBox:Shape = new Shape();
redBox.graphics.beginFill(0xff0000);
redBox.graphics.drawRect(-w/2,0,w,h);
redBox.graphics.endFill();
redBox.x = 100;
redBox.y = 200;
addChild(redBox);
redBox.rotationX = -45;

var heightBar:Shape = new Shape();
var heightBarH:Number = Math.cos(redBox.rotationX*Math.PI/180)*h; // ≈70.71
heightBar.graphics.beginFill(0x000000);
heightBar.graphics.drawRect(0,0,10,heightBarH);
heightBar.graphics.endFill();
heightBar.x = redBox.x + 100;
heightBar.y = redBox.y;
addChild(heightBar);

当redBox.rotationX接近-90时,heightBarH变为0,如果没有perspectiveProjection,这就是你所期望的。如您所见,由于此透视投影,redBox的高度不会接近0。我正在尝试计算heightBarH,同时考虑perspectiveProjection。

计算精确高度的唯一方法似乎是拥有形状顶点的x,y和z坐标Vector.<Number>,将旋转应用于matrix3D并投影顶点到点,但我没有用这种方法取得任何成功。

var verts:Vector.<Number> = new <Number>[
    -w/2,0,0,
    -w/2,h,0,
    w/2,h,0,
    w/2,0,0,
    -w/2,0,0
];
var pVerts:Vector.<Number> = new <Number>[];
var uv:Vector.<Number> = new <Number>[];
var matrix3D:Matrix3D = root.transform.perspectiveProjection.toMatrix3D();
matrix3D.identity(); //Not sure why I need to do this, but my matrix3D will be full of Infinity and NaN if I don't.
matrix3D.appendRotation(45,Vector3D.X_AXIS);
Utils3D.projectVectors(matrix3D, verts, pVerts, uv);
var drawnShape:Shape = new Shape();
drawnShape.graphics.beginFill(0xff0000);
var shape_commands:Vector.<int> = new <int> [
    GraphicsPathCommand.MOVE_TO,
    GraphicsPathCommand.LINE_TO,
    GraphicsPathCommand.LINE_TO,
    GraphicsPathCommand.LINE_TO,
    GraphicsPathCommand.LINE_TO,
];
drawnShape.graphics.drawPath(shape_commands,pVerts);
drawnShape.graphics.endFill();
drawnShape.x = stage.stageWidth/2;
drawnShape.y = stage.stageHeight/2;
addChild(drawnShape);

这似乎不会导致具有正确perspectiveProjection的形状。任何见解都将不胜感激。

0 个答案:

没有答案