Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if point is inside a custom mesh geometry

Tags:

three.js

What would be the easiest way to check if a point is inside a custom (irregular) mesh geometry?

like image 623
Zardoz Avatar asked May 03 '13 14:05

Zardoz


2 Answers

If your mesh is close-up. You can use the THREE.js built-in ray-caster. Sample code is as

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>
like image 136
yue you Avatar answered Sep 25 '22 15:09

yue you


Just raycast once from the point to any direction, then check the intersects num, if is odd, the point is in the geometry, here is the demo

like image 41
gonnavis Avatar answered Sep 25 '22 15:09

gonnavis