如何在three.js中拖动多个对象?

时间:2015-08-24 14:14:14

标签: three.js

当我点击任何一个对象时,我想将多个对象拖放到一起。我试过group()

var MovingCubeGeom = new THREE.CubeGeometry(7, 10, 7);
    MovingCube = new THREE.Mesh(MovingCubeGeom, new THREE.MeshLambertMaterial({ color: 0xFF0000, transparent: true }));
    MovingCube.position.set(0, 0, 0);
    // scene.add(MovingCube);


    var MovingCubeGeom1 = new THREE.CubeGeometry(7, 20, 10);
    MovingCube1 = new THREE.Mesh(MovingCubeGeom1, new THREE.MeshLambertMaterial({ color: 0xFF0000 }));
    MovingCube1.position.set(20, 0, 0);
    //scene.add(MovingCube1);

    //making a group
    //here we are grouping differnt object two cubes and instead of MovingCube if we 
    //put group we can do all the translation and rotation, scaling all operation to 
    // the group of objects

    group = new THREE.Group(); //create a container
    group.add(MovingCube); //add a mesh with geometry to it
    group.add(MovingCube1);
    scene.add(group); 
function onMouseDown(event) {

    // Get mouse position
    mouse.x = (event.clientX / renderer.domElement.width) * 2 - 1;
    mouse.y = -(event.clientY / renderer.domElement.height) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    intersects = raycaster.intersectObjects(group); //doubt object or scene.children
    scaling = 10;

    if (intersects.length > 0) {

        // Disable the controls
        controls.enabled = false;
        // Set the selection - first intersected object
        selection = intersects[0].object;
        // Calculate the offset
        intersects = raycaster.intersectObject(scene.parent);
        intersects[0].point.y = intersects[0].point.y + scaling;
        offset.copy(intersects[0].point).sub(scene.parent.position);
    }

}

function onMouseUp(event) {
    // Enable the controls
    controls.enabled = true;
    selection = null;
}

function onMouseMove(event) {

    event.preventDefault();
    // Get mouse position
    mouse.x = (event.clientX / renderer.domElement.width) * 2 - 1;
    mouse.y = -(event.clientY / renderer.domElement.height) * 2 + 1;

    raycaster.setFromCamera(mouse, camera);

    if (selection) {

        // Check the position where the plane is intersected
        intersects = raycaster.intersectObject(floor);
        intersects[0].point.y = intersects[0].point.y + scaling;
        // Reposition the object based on the intersection point with the plane
        selection.position.copy(intersects[0].point.sub(offset));
    } 
}

这是我的代码,但我不能移动该组。而不是所有场景都在移动。

0 个答案:

没有答案