三js如何移动场景

时间:2014-01-14 01:58:57

标签: three.js

我想移动容器(包含我的3d对象)

你能告诉我怎么样? 我正在使用Three JS

THREE.DragControls = function(camera, scene, domElement) {

this.childs = [];
this.childOffsets = [];
if (scene instanceof THREE.Scene) {
    childs = scene.children;
    scene = scene.children;
}
var _projector = new THREE.Projector();

var mouse = new THREE.Vector3();
var offset = new THREE.Vector3();
var selected;

domElement.addEventListener('mousemove', onDocumentMouseMove, false);
domElement.addEventListener('mousedown', onDocumentMouseDown, false);
domElement.addEventListener('mouseup', onDocumentMouseUp, false);

function onDocumentMouseMove(event) {

    event.preventDefault();

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

    var ray = _projector.pickingRay(mouse, camera);

    if (selected) {
        var targetPos = ray.ray.direction.clone().multiplyScalar(selected.distance).add(ray.ray.origin);
        selected.object.position.copy(targetPos.sub(offset));

        return;

    }

    var intersects = ray.intersectObjects(scene);

    if (intersects.length > 0) {

        domElement.style.cursor = 'pointer';

    } else {

        domElement.style.cursor = 'auto';

    }

}

function onDocumentMouseDown(event) {

    event.preventDefault();

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

    var ray = _projector.pickingRay(mouse, camera);
    var intersects = ray.intersectObjects(scene);

    if (intersects.length > 0) {
        selected = intersects[0];

        offset.copy(selected.point).sub(selected.object.position);

        domElement.style.cursor = 'move';

    }
}

function onDocumentMouseUp(event) {

    event.preventDefault();

    if (selected) {
        selected = null;
    }

    domElement.style.cursor = 'auto';

}

}

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想在场景中移动所有3D物体?

我这样做的方法是创建一个THREE.Group()。 E.g。

var holder = new THREE.Group();

然后将所有模型/灯光等添加到该持有者组。然后将持有者添加到场景中。

var scene = new THREE.Scene();
holder.add(model);
scene.add(holder);

然后移动支架将移动添加到其中的所有对象。