2016-09-29 51 views
-1

問題:

當我點擊多維數據集時,TransformControls附加在多維數據集上。但是當我點擊光助手時,什麼也沒有發生。當我點擊燈光助手時,我想將TransformControls附加在燈光上。你能幫助我嗎?如何偵聽onclick事件SpotLight Helper並將TransformControls附加到光源上?

這是最簡單的例子。

http://codepen.io/kranzy/pen/ALOYZw

,或者如果你需要代碼:

var scene = new THREE.Scene(); 
var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000); 
var renderer = new THREE.WebGLRenderer(); 
renderer.setSize(window.innerWidth, window.innerHeight); 
document.body.appendChild(renderer.domElement); 
var geometry = new THREE.BoxGeometry(1, 1, 1); 
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); 
var transformControls = new THREE.TransformControls(camera,renderer.domElement); 
scene.add(transformControls); 
var objects = []; 
var cube = new THREE.Mesh(geometry, material); 
cube.position.x = -10; 
scene.add(cube); 
objects.push(cube); 
var light = new THREE.SpotLight(); 
scene.add(light); 
var help = new THREE.SpotLightHelper(light); 
scene.add(help); 
objects.push(help); 
camera.position.z = 5; 
var render = function() { 
    requestAnimationFrame(render); 
    cube.rotation.x += 0.1; 
    cube.rotation.y += 0.1; 
    renderer.render(scene, camera); 
}; 
render(); 
window.addEventListener('resize', onWindowResize, false); 
function onWindowResize(){ 
    camera.aspect = window.innerWidth/window.innerHeight; 
    camera.updateProjectionMatrix(); 
    renderer.setSize(window.innerWidth, window.innerHeight); 
} 
document.addEventListener('mousedown', onDocumentMouseDown, false); 
document.addEventListener('touchstart', onDocumentTouchStart, false); 
function onDocumentTouchStart(event){ 
    event.preventDefault(); 
    event.clientX = event.touches[0].clientX; 
    event.clientY = event.touches[0].clientY; 
    onDocumentMouseDown(event); 
} 
function onDocumentMouseDown(event){ 
    event.preventDefault(); 
    mouse = new THREE.Vector2(); 
    mouse.x = (event.clientX/window.innerWidth) * 2 - 1; 
    mouse.y = - (event.clientY/window.innerHeight) * 2 + 1; 
    raycaster = new THREE.Raycaster(); 
    raycaster.setFromCamera(mouse, camera); 
    var intersects = raycaster.intersectObjects(objects); 
    if(intersects.length > 0){ 
     SELECTED = intersects[ 0 ].object; 
     console.log(SELECTED); 
     if (SELECTED.constructor === THREE.SpotLightHelper) { 
      transformControls.attach(SELECTED.light); 
     } else { 
      transformControls.attach(SELECTED); 
     } 
    } 
} 
+1

OK?每次評論時,如果沒有必要,我會收到通知停止評論請 – amanuel2

+0

你不能點擊一個輕助手。它沒有網格,因此它不會與光線投射相交。我錯過了什麼嗎? –

+0

但爲什麼@ http://threejs.org/editor我可以輕鬆地點擊輕助手?它太難了。失去了4代表,因爲downvote,現在我給你50代表。你可以嘗試更多? –

回答

2

問題是raycaster。 將raycaster.intersectObjects()的第二個參數添加到true。

所以最終的代碼是 var intersects = raycaster.intersectObjects(objects, true);

這裏是附加transformControls光工作樣例: http://codepen.io/anon/pen/PGJxBj

我希望這可以幫助應該你..

+0

非常感謝你!因爲我只有13歲,我不知道第二個參數。現在我射線2次:第一次與第二參數真正檢測輕助手,第二次與第二參數假檢測其他事情。它會導致重複的代碼,但它的作品。無論如何,我不是爲一支球隊工作的。 –

+0

我會將此添加到堆棧溢出文檔 –

相關問題