将物体带回到起始位置

时间:2019-04-15 14:30:06

标签: three.js

在这里,我试图将对象带回单击按钮的起始位置。我尝试过用静态位置固定对象的位置,但不知道它在哪里工作。我一直在寻找解决方案,他们说要将摄像机移至FOV方向轴(例如计算x,y,z)。 详细地说,如果我将一个对象从初始位置平移或旋转到另一个位置,则在按钮单击中,我必须将其撤消到起始位置。 这是https://jsfiddle.net/Ajay_Venkatesh/thpb8csv/1/

'use strict';

var camera, scene, renderer;
var cube, cube_geometry, cube_material;
var controls;

init();
render();

function init() {

    scene = new THREE.Scene();

    // renderer

    renderer = new THREE.WebGLRenderer({
        alpha: true
    });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // camera

    camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 12;

    // controls

    controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.addEventListener('change', render);
    controls.enableZoom = false;

    // mesh - cube

    cube_geometry = new THREE.CubeGeometry(5, 5, 5);

    for (var i = 0; i < cube_geometry.faces.length; i += 2) {

        var color = Math.random() * 0xffffff;

        cube_geometry.faces[i].color.setHex(color);
        cube_geometry.faces[i + 1].color.setHex(color);
    }

    cube_material = new THREE.MeshLambertMaterial({
        color: 0xffffff,
        vertexColors: THREE.FaceColors
    });

    cube = new THREE.Mesh(cube_geometry, cube_material);
    scene.add(cube);

    // Lights

    var light = new THREE.DirectionalLight(0xffffff);
    light.position.set(1, 1, 1);
    scene.add(light);

    var light = new THREE.DirectionalLight(0x002288);
    light.position.set(-1, -1, -1);
    scene.add(light);

    var light = new THREE.AmbientLight(0x222222);
    scene.add(light);

    // events

    window.addEventListener('resize', onWindowResize, false);

}

function render() {

    renderer.render(scene, camera);

}

function onWindowResize(event) {

    renderer.setSize(window.innerWidth, window.innerHeight);

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

}

0 个答案:

没有答案