0

How to intersect non indexed buffer geometry? I want to detect intersection between two defferent faces of two different non indexed buffergeometry. I set ray from middle of each faces, but raycaster don`t work with non indexed geometry.

I want to see result like this: enter image description here

Thank you very much!

my code


var box = new THREE.Mesh(geometry.toNonIndexed(), material);

var raycaster = new THREE.Raycaster();
var intersects = [];

var pos = box.geometry.attributes.position;
var ori = new THREE.Vector3();
var dir = new THREE.Vector3();

var a = new THREE.Vector3(),
  b = new THREE.Vector3(),
  c = new THREE.Vector3(),
  tri = new THREE.Triangle();



var faces = pos.count / 3;
for (let i = 0; i < faces; i++) {
  a.fromBufferAttribute(pos, i * 3 + 0);
  b.fromBufferAttribute(pos, i * 3 + 1);
  c.fromBufferAttribute(pos, i * 3 + 2);
  tri.set(a, b, c);
  tri.getMidpoint(ori);
  tri.getNormal(dir)
  // scene.add(new THREE.ArrowHelper(raycaster.ray.direction, raycaster.ray.origin, 30, 0xff0000));
  raycaster.set(ori, dir);
  intersects = raycaster.intersectObject(box, true);

  if (intersects.length > 0) {

    console.log(intersects[0]);

  }
}
vinkovsky
  • 310
  • 4
  • 16
  • Seems you need to comment that line too: `raycaster.ray.origin, 30, 0xff0000));` – prisoner849 Jun 09 '19 at 10:01
  • Thank you for your replying! I mistaked when copied code from my project. This line commented and don`t use – vinkovsky Jun 09 '19 at 14:30
  • @ArkadiyVinkovskiy (1) Try calling `scene.updateMatrixWorld()` prior to raycasting. (2) You are raycasting `box` against itself. Add a different box. – WestLangley Jun 09 '19 at 17:57
  • 1
    Hi @WestLangley! Thank you! I converted my object to indexed geometry and set `scene.updateMatrixWorld()` all works very well but how i can check intersection in all objects that adding on scene? The main idea of ray cast is to blocked occupied side of box. If i click on side of box, new box appear on scene. You can see this in my project: https://alovert.ru – vinkovsky Jun 10 '19 at 13:10
  • @WestLangley i load new object on scene by same function. And in this function i check intersection. Maybe problem in this. **Simplified code:** https://1drv.ms/u/s!AqtIFlFVWz4MhNhoQ26CDkMJScPZXw – vinkovsky Jun 10 '19 at 13:26
  • (1) Do not call `updateMatrixWorld()` for each face! Call it only once. I suggest you use the debugger to step through your code. I can't debug it for you. (2) `raycaster.intersectObjects(scene.children, true);` -- "Objects", with an "s". – WestLangley Jun 10 '19 at 16:44
  • Ok, i try it. Much thanks ! – vinkovsky Jun 10 '19 at 17:05

0 Answers0