检查点是否位于自定义(不规则)网格几何体内的最简单方法是什么?
答案 0 :(得分:4)
Three.js提供raycaster,可以完成这项工作。示例代码为
const point = new THREE.Vector3(2,2,2) // Your point
const geometry = new THREE.BoxBufferGeometry( 5, 5, 5 )
const material = new THREE.MeshBasicMaterial( { color: 0xffff00 } )
const mesh = new THREE.Mesh( geometry, material )
const raycaster = new THREE.Raycaster()
raycaster.set(point, new THREE.Vector3(1,1,1))
const intersects = raycaster.intersectObject(mesh)
if( intersects.length %2 === 1) { // Points is in objet
console.log(`Point is in object`)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script>
答案 1 :(得分:2)
这是计算几何问题。你可以看看Finding if point is inside geometry。由于几何形状不规则,因此问题要困难得多。
但是如果精度不是太重要,您可以检查该点是否在几何体的边界框内。
答案 2 :(得分:2)
从点到任意方向只进行一次光线投射,然后检查相交数,如果是奇数,则点在几何中,这里是demo
答案 3 :(得分:-1)
最好使用射线方向和面法线的点积进行检查
在three.js(r103)上进行了测试
const point = new THREE.Vector3(2, 2, 2) // Your point
const direction = new THREE.Vector3(1, 1, 1);
const geometry = new THREE.BoxGeometry(5, 5, 5)
const material = new THREE.MeshBasicMaterial({ color: 0xffff00, side: THREE.DoubleSide });
const mesh = new THREE.Mesh(geometry, material)
const raycaster = new THREE.Raycaster()
raycaster.set(point, direction)
const intersects = raycaster.intersectObject(mesh);
if (intersects.length && direction.dot(intersects[0].face.normal) > 0) {
console.log(`Point is in object`);
} else {
console.log(`Point is out of object`);
}
在极少数情况下,您可以获得偶数个交点,且交点位于网格内
(尝试point = new THREE.Vector3(0, 0, 0)
,应该给出4个交点)