我正在尝试使用 Five3d 包在 3d 中绘制一个精灵并为其设置动画,然后在应用程序的根上创建一个形状,并在移动时将其覆盖在 3d 中的精灵上。我已经尽一切努力让它工作,但似乎无法让它正确排列。看起来透视已经出来了。在检查了 3d x,y 和全局 x,y 之间的关系后,我让它工作了,这导致我编写了下面显示的方法“local3dToGlobalXY”,但这仅在舞台为 1024 / 768 时才有效,奇怪。有人告诉我 Five3d 包中的 Sprite2d 可以做到这一点,但也不能让它工作。它与我的“local3dToGlobalXY”方法具有相同的结果。
红星是 3d 中的精灵,两个十字准线被绘制到不同的范围内。一个被拉入 Sprite2d,这是我被推荐做的,另一个被拉入根。
希望有人遇到过这种情况或可以指出我做错了什么。
谢谢,J
/**
* ...
* @author James Jordan
*/
public class Main extends Sprite
{
private var s3D:Sprite3D;
private var _scene:Scene3D;
private var _s:Sprite3D;
private var star3D:Shape3D;
private var t:Timer;
private var crossHairs2D:Sprite;
private var otherPlane:Sprite2D;
private var _plane:Sprite3D;
private var crossHairsRoot:Sprite;
private var animationPlane:Sprite3D;
private var crossHairsPlane:Sprite2D;
private var starsPlane:Sprite3D;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
animationPlane = new Sprite3D();
starsPlane = new Sprite3D();
animationPlane.addChild(starsPlane);
crossHairsPlane = new Sprite2D();
animationPlane.addChild(crossHairsPlane);
_scene = new Scene3D();
_scene.x = stage.stageWidth / 2;
_scene.y = stage.stageHeight / 2;
_scene.addChild(animationPlane);
addChild(_scene);
star3D = drawStar(starsPlane);
crossHairs2D = drawGlobalShape(crossHairsPlane, 0xff000, 10);
crossHairsRoot = drawGlobalShape(root, 0x0000ff, 5);
t = new Timer(1000, 0);
t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();
}
private function onTimer(e:TimerEvent):void
{
TweenLite.to(star3D, 1, { x:(Math.random() * stage.stageWidth) - (stage.stageWidth/2), y:(Math.random() * stage.stageHeight) - (stage.stageHeight / 2), z:(Math.random() * 500), onUpdate:onTweenUpdate } );
}
private function onTweenUpdate():void
{
var p:Point = local3dToGlobalXY(starsPlane, new Point3D(star3D.x, star3D.y, star3D.z));
crossHairs2D.x = p.x - stage.stageWidth / 2;
crossHairs2D.y = p.y - stage.stageHeight / 2;
crossHairsRoot.x = p.x;
crossHairsRoot.y = p.y;
}
private function local3dToGlobalXY(_s:Sprite3D, p3d:Point3D):Point
{
var m:Matrix3D = _s.concatenatedMatrix;
var newP3d:Point3D = m.transformPoint(p3d);
var globalPoint:Point = _s.local3DToGlobal(new Vector3D(newP3d.x, newP3d.y, newP3d.z));
return globalPoint;
}
private function drawStar(parent:Sprite3D):Shape3D {
var starShape:Shape3D = new Shape3D();
DrawingUtils.star(starShape.graphics3D, 5, 30, 20, (45 / 180) * Math.PI, 0xff0000);
parent.addChild(starShape);
return starShape;
}
private function drawGlobalShape(p:*, c:uint, lineWeight:Number):Sprite
{
var d:Sprite = new Sprite();
d.graphics.lineStyle(lineWeight, c);
d.graphics.moveTo( -20, 0);
d.graphics.lineTo(20, 0);
d.graphics.moveTo( 0, -20);
d.graphics.lineTo(0, 20);
d.graphics.endFill();
p.addChild(d);
return d;
}
}