2014-01-14 75 views
0

我想移動容器(其中包含我的3d物體)三個js如何移動場景

你能告訴我怎麼做嗎? 我使用三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'; 

} 

}

+0

請更具體地說明你的意思是「移動」。你有沒有檢查threejs的例子? – 2pha

+0

是的,我試過一些例子。我可以移動3D物體。我可以移動很多對象,但我移動了數組對象。 :(這不太好,我想是這樣,我想移動容器(可能是「飛」),我不知道。 – Luc

回答

0

如果我理解正確的話,你想移動場景內所有的3D對象?

我這樣做的方式是創建一個THREE.Group()。例如。

var holder = new THREE.Group(); 

然後將所有模型/燈/等添加到該持有者組。然後將該持有人添加到場景中。

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

然後移動持有人將移動添加到它的所有對象。