# Raycaster.intersectObject - on big Terrain work very slow (heavy load on the processor)

**URL:** <https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398>\
**Category:** Questions\
**Tags:** raycaster, performance\
**Created:** [March 15, 2021, 10:40am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398 "2021-03-15T10:40:54Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 15, 2021, 10:40am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/1 "2021-03-15T10:40:54Z")

</div>

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/d/a/da78fa3eb398666eb887d579874796fc035400c0.jpeg)

Good afternoon, I have a large map of the area with mountains and depressions. I need to move the character taking into account the height, tobish point Y. This miscalculation is done on the server, every step of the movement, since the map is loaded there in memory, this is not a problem. But the [Raycaster.intersectObject] function causes too much load and the system will start to hang. The only way out that I found was to virtually split the map into sections and calculate the height for each section and write it to a file, but this worked, even though the height was not ideal, since the rendering step was 0.2, but if you make it just smaller, then the files will be very large. If anyone knows, please tell me how you can get out of this situation, maybe there are better calculations that do not load the system so much?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 15, 2021, 11:04am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/2 "2021-03-15T11:04:39Z")

</div>

You can speed up the performance of ray intersection tests in various ways. A typical approach is the usage of a bounding volume hierarchy. Check out:

> **[gkjohnson/three-mesh-bvh](https://github.com/gkjohnson/three-mesh-bvh)**
>
> A BVH implementation to speed up raycasting against three.js meshes.

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 15, 2021, 11:18am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/3 "2021-03-15T11:18:52Z")

</div>

Thanks a lot I will try.

---

<div class="post-metadata">

**Author:** ![seanwasere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/seanwasere/32/20864_2.png) [@seanwasere](https://discourse.threejs.org/u/seanwasere)\
**Post date:** [March 15, 2021, 11:24am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/4 "2021-03-15T11:24:11Z")

</div>

Or, break up your terrain into smaller tiles  
[Three.js TypeScript Tutorials by Sean Bradley : https://sbcode.net/threejs](https://sbcode.net/extra_html/earth.html)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/b/a/ba4b8d2e486777d1b19b207af648be4a9387dafb.jpeg)

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 15, 2021, 2:04pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/5 "2021-03-15T14:04:32Z")

</div>

Yes, this is a great idea, but I have a ready-made sample, and I have nothing to break it with, I tried to disassemble the geometry into parts, but there is, as I understand it, connected points, this leads to an error.

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 15, 2021, 4:18pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/6 "2021-03-15T16:18:55Z")

</div>

No, it didn’t help, but are there any utilities to break the geometry into pieces?

---

<div class="post-metadata">

**Author:** ![alessandro\_guarino](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/alessandro_guarino/32/16309_2.png) [@alessandro\_guarino](https://discourse.threejs.org/u/alessandro_guarino)\
**Post date:** [March 15, 2021, 4:31pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/7 "2021-03-15T16:31:17Z")

</div>

Alternative way  
Many points of your ground looks unnecessary for collision.  
cuold you try to use an auxiliary geometry (not shown) only for raycast, very reduced in vertex count.

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 18, 2021, 11:02am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/8 "2021-03-18T11:02:08Z")

</div>

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/7/b/7bab2256a8adf4b61db0013fa87d16a5440bf991.jpeg)

Thanks a lot to everyone, but dividing the map into sections managed to relieve all the load, soon I will finally make the first large geometric map.

---

<div class="post-metadata">

**Author:** ![gkjohnson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gkjohnson/32/2298_2.png) [@gkjohnson](https://discourse.threejs.org/u/gkjohnson)\
**Post date:** [March 18, 2021, 7:08pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/9 "2021-03-18T19:08:17Z")

</div>

> No, it didn’t help

If the map is one big piece of geometry then three-mesh-bvh should have been able to reduce the time it takes to raycast pretty easily. If this is still an issue can you provide more information on how you used it and what didn’t work? Perhaps the docs can be updated to be made more clear. Improving raycast performance against complex geometry is the primary purpose of the extension.

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 20, 2021, 9:56am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/10 "2021-03-20T09:56:52Z")

</div>

> [@gkjohnson](#):
>
> If the map is one big piece of geometry then three-mesh-bvh should have been able to reduce the time it takes to raycast pretty easily. If this is still an issue can you provide more information on how you used it and what didn’t work? Perhaps the docs can be updated to be made more clear. Improving raycast performance against complex geometry is the primary purpose of the extension.

Sorry for the delay, didn’t see the message right away.

global.THREE = require(‘three-full’);

global.BVH = require(‘three-mesh-bvh’);  
THREE.BufferGeometry.prototype.computeBoundsTree = BVH.computeBoundsTree;  
THREE.BufferGeometry.prototype.disposeBoundsTree = BVH.disposeBoundsTree;  
THREE.Mesh.prototype.raycast = BVH.acceleratedRaycast;

and after function check

this.rayFloor.set(targetPos, new THREE.Vector3( 0, -1, 0 ) );   
this.rayFloor.firstHitOnly = true;  
this.rayFloor.intersectObject(obj, true);

---

<div class="post-metadata">

**Author:** ![gkjohnson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gkjohnson/32/2298_2.png) [@gkjohnson](https://discourse.threejs.org/u/gkjohnson)\
**Post date:** [March 20, 2021, 6:00pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/11 "2021-03-20T18:00:35Z")

</div>

> global.THREE = require(‘three-full’);

I’m unfamiliar with “three-full” but it looks like it imports a completely different copy of three.js. three-mesh-bvh relies on the “three” package so I can’t guarantee that it works when using the “three-full” package. That aside it looks like [it’s deprecated](https://github.com/Itee/three-full) and is based on a fairly old version of three.js. Either way I’d recommend using the official three.js distribution.

> ```js
> this.rayFloor.set(targetPos, new THREE.Vector3( 0, -1, 0 ) );
> this.rayFloor.firstHitOnly = true;
> this.rayFloor.intersectObject(obj, true);
> 
> ```

Having said that you do need to call `computeBoundsTree` before raycast performance can be improved, which I don’t see called in your above snippet. The BVH is not generated automatically on raycasting. You’ll need to add a line like this when generating your terrain geometry:

```js
terrainMesh.geometry.computeBoundsTree( { lazyGeneration: false } );

```

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 21, 2021, 7:56am UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/12 "2021-03-21T07:56:11Z")

</div>

Thank you very much, I will try, if everything is fine, then it will dump the load a little more.

---

<div class="post-metadata">

**Author:** ![KiborgMaster](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kiborgmaster/32/69616_2.png) [@KiborgMaster](https://discourse.threejs.org/u/KiborgMaster)\
**Post date:** [March 23, 2021, 1:04pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/13 "2021-03-23T13:04:55Z")

</div>

Thank you very much yes it helped  
 → geometry.computeBoundsTree ({lazyGeneration: false});

The load dropped to 5%, and it was about 22%.  
Now I am thinking of making in one beam and a system of movement in geometry and contact with obstacles.

AGAIN THANKS TO YOU))))

---

<div class="post-metadata">

**Author:** ![kranzkie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kranzkie/32/25466_2.png) [@kranzkie](https://discourse.threejs.org/u/kranzkie)\
**Post date:** [April 7, 2022, 12:43pm UTC](https://discourse.threejs.org/t/raycaster-intersectobject-on-big-terrain-work-very-slow-heavy-load-on-the-processor/24398/14 "2022-04-07T12:43:34Z")

</div>

Hi, I had a similar issue to you and came across this topic.

I put my terrain object on a specific layer.  
I set my raycaster to only look at that layer number and saw huge performance gains!

```javascript
raycaster.layers.set( 1 );
object.layers.enable( 1 );

```

Hope it helps 🙂
