Five3d local3dtoglobal [英] five3d local3dtoglobal

查看:14
本文介绍了Five3d local3dtoglobal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Five3d 包在 3d 中绘制精灵并为其设置动画,然后在应用程序的根部创建一个形状,并在移动时将其覆盖在 3d 中的精灵上.我已经尽一切努力让它工作,但似乎无法让它正确排列.好像透视没了.在检查了 3d x,y 和全局 x,y 之间的关系后,我让它工作了,这导致我编写了如下所示的方法local3dToGlobalXY",但这仅在舞台为 1024/768 时才有效,很奇怪.有人告诉我 Five3d 包中的 Sprite2d 可以做到这一点,但也无法让它发挥作用.它与我的local3dToGlobalXY"方法具有相同的结果.

I am trying to use the Five3d package to draw a sprite in 3d and animate it then create a shape on the root of the app and overlay it over the sprite in 3d as it moves. I have tried everything to get it to work but can't seem to get it to line up properly. It looks like the perspective is out. I had it working after inspecting the relationship between the 3d x,y and global x,y which lead me to write the method 'local3dToGlobalXY' shown below but this only worked when the stage was 1024 / 768, strange. I was told that the Sprite2d in the Five3d package does this but can't get that to work either. It has the same results as my 'local3dToGlobalXY' method.

我在这里上举了一个例子,这个例子的项目是此处.

I have put an example up here and the project for the example is here.

红星是 3d 中的精灵,两个十字准线绘制到不同的范围内.一个被绘制到 Sprite2d 中,这是我被推荐做的,另一个被绘制到根中.

The red star is a sprite that is in 3d and the two crosshairs are draw into different scopes. One is drawn into a Sprite2d which is what I was recomended to do and the other is drawn into the root.

希望有人遇到过这种情况或者可以指出我做错了什么.

Hopefully someone has encountered this or can point out what I have done wrong.

谢谢,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;

    }

}

推荐答案

这是你的视角和 Z 坐标.删除 TweenLite.to 语句中的随机 Z tween,您会看到它们完美地排列在一起.

It's your perspective and Z coordinate. Remove the random Z tween in the TweenLite.to statement and you'll see that they line up perfectly.

你到底想在这里做什么?除了偏移星星之外,Z 参数似乎没有添加任何内容(根据与观察者的距离进行缩放)?

What exactly are you trying to do here? Other than offset the star, the Z param doesn't seem to add anything (scaling based on distance from observer)?

这篇关于Five3d local3dtoglobal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆