适合在单击按钮时筛选对象

时间:2019-04-17 05:39:18

标签: three.js

在这里,我试图平移并移动到其他位置时要筛选对象。我尝试使用controls.reset()函数在其中重置相机,但是我试图在不重置相机位置的情况下将对象筛选到中心点。这是小提琴https://jsfiddle.net/Ajay_Venkatesh/thpb8csv/4/

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 个答案:

没有答案