我正在關注如何使用raycasting處理可點擊對象的mrdoobs示例。我也看過許多類似的問題,並嘗試過無數的事情。 光線投射工程...如果我在距離小於1.Threejs raycaster only only close close
儘管(默認值),Raycaster設置爲接近0和遠無窮大。 我還沒有看到任何設置距離的代碼示例。
我希望有另一雙眼睛。
// snippet
glow.clickables = [];
var cubeGeo = new THREE.CubeGeometry(2, 2, 2);
cubeGeo.computeFaceNormals();
var cube = new THREE.Mesh(cubeGeo, redmat);
cube.position.y = 10;
cube.position.x = 0;
cube.position.z = -12;
cube.overdraw = true;
glow.Vis.scene.add(cube);
glow.clickables.push(cube);
onclick_();
var onclick_ = function() {
$('#world').on('mousedown', function(e){
var mouseX = (event.offsetX/$('#world').width()) * 2 - 1;
var mouseY = - (event.offsetY/$('#world').height()) * 2 + 1;
var vector = new THREE.Vector3(mouseX, mouseY, 0.1); //what should z be set to?
//console.info(vector); // A vector between -1,1 for both x and y. Z is whatever is set on the line above
projector.unprojectVector(vector, glow.Vis.camera);
var conts = glow.Vis.controls.getObject().position; // control 3dObject which the camera is added to.
var clickRay = new THREE.Raycaster(conts, vector.sub(conts).normalize());
var intersects = clickRay.intersectObjects(glow.clickables);
console.info(intersects.length);
if(intersects.length > 0) {
alert("Click detected!");
}
});
}
不太確定,但我認爲矢量Z應該設置爲1.0,並且感覺相當合理,小於這可能導致遠處的交叉點不被檢測到。 – yaku
謝謝!設置爲1固定我的問題 – user508771