旋转.obj文件OBJMTLLoader three.js

时间:2014-03-09 00:52:32

标签: javascript three.js

我将OBJMTLLoader类用于一个obj文件,并且使用object.rotation.y -= 0.5在对象的固定点周围旋转效果很好。使用相同的代码(减去更改摄像机位置),我将.obj文件替换为另一个,旋转现在进行圆周运动,就像在摄像机周围而不是停留在原位。知道为什么我使用相同的代码吗?

由于

修改

var OBJLoaded;    

function init()
{
    container = document.getElementById('player');
    camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1,  2000);
    camera.position.x = 110;
    camera.position.z = -160;
    camera.position.y = 15;

    // camera.position.z = 40;
    // camera.position.y = 2;

    //scene
    scene = new THREE.Scene();

    var ambient = new THREE.AmbientLight( 0x444444 );
    scene.add( ambient );

    var directionalLight = new THREE.DirectionalLight( 0xffffff );
    directionalLight.position.set( 100, 90, 200 );
    scene.add( directionalLight );

    //model
    var loader = new THREE.OBJMTLLoader();
    //loader.load('./assets/Stereo_LowPoly.obj', './assets/Stereo_LowPoly.mtl',    function(object)
    loader.load('./assets/studio_beats.obj', './assets/studio_beats.mtl', function(object)
    { 
        OBJLoaded = object;
        console.log(object);
        scene.add( object );
    });

    renderer = new THREE.WebGLRenderer({alpha: true});
    renderer.setClearColor(0x000000, 0);
    renderer.setSize($('#player').width(), $('#player').height());
    container.appendChild(renderer.domElement);

    scene.add(camera);
}

function animateBoombox()
{
    requestAnimationFrame(animateBoombox);
    render();
}   

function render()
{
    var rotSpeed = 0.004;
    if (OBJLoaded) 
    { 
        OBJLoaded.rotation.y -= rotSpeed;
    }
    renderer.render(scene, camera);
}

评论的部件(摄像头和对象加载)用于加载的上一个对象。这工作正常,但未注释的部分不一样。

1 个答案:

答案 0 :(得分:4)

您加载的对象有一个支点,它来自模型创建器软件...您需要在使用three.js加载对象之前更改对象的轴心点。

如果你不能,你应该像我在装载机回调中那样做:

            var loader = new THREE.OBJMTLLoader();
            loader.load('your_file.obj', 'your_file.mtl', function (object) {
                object.traverse(function (child) {
                    child.centroid = new THREE.Vector3();
                    for (var i = 0, l = child.geometry.vertices.length; i < l; i++) {
                        child.centroid.add(child.geometry.vertices[i].clone());
                    }
                    child.centroid.divideScalar(child.geometry.vertices.length);
                    var offset = child.centroid.clone();
                    child.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-offset.x, -offset.y, -offset.z));
                    child.position.copy(child.centroid);
                    child.geometry.computeBoundingBox();
                });
            });

然后旋转你的对象......