0

我最近开始使用 Haxe 和 Three.js。如何使用 JSONLoader 加载 3D 对象。我对 Haxe 的做事方式非常陌生,并且还没有完全了解外部事物。

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

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

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

4

2 回答 2

1

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

这是一个更好的抽象:

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 );
    }


}

希望这对某人有所帮助。

于 2012-10-16T05:16:56.327 回答
0

看看这个,应该对你有帮助。

于 2012-10-06T08:08:50.327 回答