【发布时间】:2017-04-29 12:45:50
【问题描述】:
我正在尝试使用 raycaster 来确定二维点表面上的点...
function dynamic(x) { x.dynamic = true; return x; }
geometry.addAttribute("position", dynamic(new THREE.BufferAttribute(positions, 3)));
geometry.addAttribute("color", dynamic(new THREE.BufferAttribute(colors, 3)));
var grid = new THREE.Points(geometry, material);
scene.add(grid);
function mouseDownHandler(event) {
if (event.button == 0) {
event.preventDefault();
var mouse = new THREE.Vector2();
var box = canvas.getBoundingClientRect();
mouse.x = ((event.clientX - box.left) / (box.right - box.left)) * 2.0 - 1.0;
mouse.y = ((event.clientY - box.bottom) / (box.top - box.bottom)) * 2.0 - 1.0;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObject(grid);
if (intersects.length > 0) {
// Why does my intersect not work?
// I _believe_ I'm calculating the mouse positions correctly.
// But, the intersects are always way off. (Or, I'm misinterpreting
// something...)
console.log("mouse: ", mouse);
console.log("First Intersect:", intersects[0]);
console.log("First Point:", intersects[0].point);
}
}
}
我在这里有完整的代码: https://som.hex21.com/threejs/wtf/ 是页面,和 https://som.hex21.com/threejs/wtf/js/gridsearch.js 是代码..
我的问题在于 mouseDownHandler。基本上,当您单击鼠标时,我希望它告诉我单击了哪个点。但是,当我点击网格左下角的say时,鼠标(靠近){-1,-1},我认为这是预期的,对吧?)而右上角靠近{1,1}。
我应该如何读取被点击点的索引?我点击了所有四个角点,并且我希望它们中的至少 一个 会是 0,就我构建数据的方式而言。 (只有 2 个嵌套的 for 循环)但是,它们从来都不是,并且 intersects[0].point 中的点似乎总是很遥远。我做错了什么?
谢谢!
【问题讨论】:
标签: javascript three.js