Search code examples
three.jsautodeskautodesk-forgeautodesk-viewer

Ray intersect with the model


Is it possible to cast rays from the camera and know if it intersects the model?

On the same note, is it possible to calculate distances to the intersection points this way?

I want this so I'll be able to know programmatically if a wall has a window or is it flat, if it has a window then there will be a jump in the distances of the intersections.


Solution

  • Is it possible to cast rays from the camera and know if it intersects the model? There is several options you can use:

    If you want to intersect anything, you can use the viewer built-in api let posClientCoords = new THREE.Vector3(x, y, 1.0) let result =this.viewer.utilities.viewerImpl.hitTestViewport(posClientCoords , false); if ( result !== null && result.fragId >= 0 ) { // here you go }

    If you prefer to ray cast a selection of objects, you can use the Three.js raycaster let vray =new THREE.Vector3( ptarget.x - psource.x, ptarget.y - psource.y, ptarget.z - psource.z ) vray.normalize () let ray =new THREE.Raycaster( psource, vray, 0, max_dist ) let intersectResults = ray.intersectObjects ( wallMeshes, true )

    wallMeshes is an array of proxy graphics that you get from the viewer viewer.impl.getRenderProxy(viewer.model, fragId), null, null, null )

    On the same note, is it possible to calculate distances to the intersection points this way? Each of these API will return the hit point (intersection point between the ray and the object)

    An example is posted here