【问题标题】:Loop Rotation on any axis for a 3D obj Three js3D obj 三个 js 在任意轴上的循环旋转
【发布时间】:2017-09-11 04:08:56
【问题描述】:

我试图让一个导入的 3d 对象在任何轴上连续旋转,例如经典立方体、球体等。但它不起作用,根本不动,我不明白为什么。代码如下:

var scene6, camera6, renderer6, light, shipMtl, shipObj;

function init() {
    scene6 = new THREE.Scene();
    camera6 = new THREE.PerspectiveCamera(35, 1, 1, 1000);
    camera6.position.z = 400;

    //LIGHTS
    light = new THREE.PointLight(0xffffff, 2, 0);
    light.position.set(200, 100, 300);
    scene6.add(light);


    //3D MODEL
    shipMtl = new THREE.MTLLoader();
    shipMtl.load('../models/spaceCraft1.mtl', function(materials) {
        materials.preload();
        shipObj = new THREE.OBJLoader();
        shipObj.setMaterials(materials);
        shipObj.load('../models/spaceCraft1.obj', function(object) {
            object.scale.set(10, 10, 10);
            object.rotation.x += .01;
            scene6.add(object);
        });
    });

    renderer6 = new THREE.WebGLRenderer({ canvas: document.getElementById('model'), antialias: true });
    renderer6.setClearColor(0x000000);
    renderer6.setPixelRatio(window.devicePixelRatio);

    animate6();
}

function animate6() {

    requestAnimationFrame(animate6);
    renderer6.render(scene6, camera6);

}

window.onload = init;

感谢您的帮助。

【问题讨论】:

    标签: javascript 3d three.js


    【解决方案1】:

    对象的旋转仅更改一次:加载模型时。

    如果你想连续旋转它,你需要更新它的旋转每一帧。因此,您应该将object.rotation.x += 0.01 行移到animate() 函数内。

    var scene6, camera6, renderer6, light, shipMtl, shipObj;
    var obj; // global reference to your model, once loaded
    
    function init() {
        scene6 = new THREE.Scene();
        camera6 = new THREE.PerspectiveCamera(35, 1, 1, 1000);
        camera6.position.z = 400;
    
        //LIGHTS
        light = new THREE.PointLight(0xffffff, 2, 0);
        light.position.set(200, 100, 300);
        scene6.add(light);
    
        //3D MODEL
        shipMtl = new THREE.MTLLoader();
        shipMtl.load('../models/spaceCraft1.mtl', function(materials) {
            materials.preload();
            shipObj = new THREE.OBJLoader();
            shipObj.setMaterials(materials);
            shipObj.load('../models/spaceCraft1.obj', function(object) {
                object.scale.set(10, 10, 10);
                // no need to change the rotation here
                obj = object; // put your object as global
                scene6.add(object);
            });
        });
    
        renderer6 = new THREE.WebGLRenderer({ canvas: document.getElementById('model'), antialias: true });
        renderer6.setClearColor(0x000000);
        renderer6.setPixelRatio(window.devicePixelRatio);
    
        animate6();
    }
    
    function animate6() {
        requestAnimationFrame(animate6);
    
        obj.rotation.x += 0.01;
    
        renderer6.render(scene6, camera6);
    }
    
    window.onload = init;
    

    【讨论】:

    • 很高兴它有帮助!如果它有效,请不要忘记接受答案。
    猜你喜欢
    • 2023-03-14
    • 2013-11-12
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2019-11-17
    相关资源
    最近更新 更多