在Haxe中,如何加载从Blender导出的json模型?

时间:2012-10-05 05:11:39

标签: three.js haxe

我最近开始玩Haxe和Three.js。如何使用JSONLoader加载3D对象。我对Haxe的做事方式很陌生,并没有把我的脑袋缠在整个外部的东西上。

我正在使用这个lib来简化事情:

https://github.com/rjanicek/three.js-haXe

除了JSONLoader或任何加载器之外,大多数Three.js类都在lib中抽象。如何在Haxe中加载从Blender导出的json模型?

2 个答案:

答案 0 :(得分:1)

好像我使用了错误的lib:)

这是一个更好的抽象:

https://github.com/labe-me/haxe-three.js

要加载3D模型,您将采用以下方式:

package co.za.anber;
import js.three.Three;
import js.Lib;

class Main 
{
    static private var scene:Scene;

    static function main() 
    {
        //Get the dimensions of the scene
        var w = Lib.window.innerWidth;
        var h = Lib.window.innerHeight;

        scene = new Scene();

        //add some light
        var pointLight = new PointLight(0xffffff, 1, 0);
        pointLight.position.set(10, 50, 130);
        scene.add(pointLight);
        //add a camera
        var camera = new PerspectiveCamera(70, w/h, 1, 1000);
        camera.position.z = 500;
        scene.add(camera);

        //setup renderer in the document
        var renderer = new WebGLRenderer(null);
        renderer.setSize(w, h);
        Lib.document.body.appendChild(renderer.domElement);


        //Load the Blender exported Mesh. 
        //This is where we load the Mesh and setup the onload handler. This was the part I wasn't so sure about.
        var loader:JSONLoader = new JSONLoader(true);
        //I don't like in-line functions. You need to make the returning function into a Dynamic type.
        var callbackModel:Dynamic  = function( geometry:Dynamic ){createScene(geometry); };
        loader.load("Suzanne.js", callbackModel);


        //Listen for mouse move. In-line function from somewhere else.
        var mouseX = 0, mouseY = 0;
        untyped Lib.document.addEventListener('mousemove', function(event){
            mouseX = (event.clientX - Lib.window.innerWidth/2);
            mouseY = (event.clientY - Lib.window.innerHeight/2);
        }, false);

        //Render the scene @60 frames per second. Inline function from somewhere else.
        var timer = new haxe.Timer(Math.round(1000/60));
        timer.run = function(){
            camera.position.x += (mouseX - camera.position.x) * 0.05;
            camera.position.y += (-mouseY - camera.position.y) * 0.05;
            camera.lookAt(scene.position);
            renderer.render(scene, camera);
        }
    }

    /**
     * Onload complete handler. Here we can add our Mesh.
     * @param   geometry
     */
    static function createScene( geometry:Dynamic):Void{
        var mesh:Mesh = new Mesh( geometry, new MeshLambertMaterial( { color: 0x00FF00 } ) );
        //We scale it up to be visible!
        mesh.scale.set( 150.15, 150.5, 150.5 );
        scene.add( mesh );
    }


}

希望这有助于某人。

答案 1 :(得分:0)

看看this,它可以帮到你。