# thinInstace hide or move from grounds

**URL:** <https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705>\
**Category:** Questions\
**Created:** [November 14, 2020, 4:42pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705 "2020-11-14T16:42:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 14, 2020, 4:42pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/1 "2020-11-14T16:42:55Z")

</div>

Hi,

I am using thinIntance of trees to fill area, here is PG [Babylon.js Playground](https://www.babylonjs-playground.com/#09HB40)

I have two grounds `ground2` and `ground3`, I don’t wants to show trees on those grounds. what is the best way to hide or move those trees from that area?

Thanks

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 14, 2020, 6:41pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/2 "2020-11-14T18:41:19Z")

</div>

Don’t create them in the first place if their position lies inside those planes?

To remove a thin instance from the screen you don’t have any other choice than removing its matrix from the matrix list.

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 14, 2020, 8:37pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/3 "2020-11-14T20:37:16Z")

</div>

please how to check if tree position is inside those planes? as you see I have on line 71,72 where I am setting random position of X,Z

```
 if (i > 20) {
                startX = rangeX / 2 - Math.random() * rangeX;
                startZ = rangeZ / 2 - Math.random() * rangeZ;
            }

```

if I know `startX` and `startZ` is inside my grounds, I can just skip that tree to be created in above loop.  
Thanks

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 14, 2020, 9:27pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/4 "2020-11-14T21:27:22Z")

</div>

An easy way would be to check for intersection between the planes and a vertical ray which would pass by the tree bounding box center (or the tree position).

You can have a look here for how to use the ray class for mesh intersection: [Get line length between two mesh](https://forum.babylonjs.com/t/get-line-length-between-two-mesh/15395)

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 15, 2020, 3:28am UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/5 "2020-11-15T03:28:37Z")

</div>

Thanks, it would be my first time using ray.

I checked the example and went also through Ray docs but can’t figure out how to apply ray on thinIncase , in the example PG its seems simple since it contains a mesh `box` which could be used with `ray.intersectsMesh(box)`.

a small snippet example or updated my PG, with thinInstance will be really helpful to understand for me.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 15, 2020, 12:09pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/6 "2020-11-15T12:09:18Z")

</div>

What you need is to build a vertical ray that goes through the tree position. Then you need to test for intersection between this ray and ground2/ground3. If there’s at least one intersection, don’t add the matrix in the matrix list.

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 15, 2020, 1:16pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/8 "2020-11-15T13:16:15Z")

</div>

I think I am close, just trying to figure out why origin of ray is different from tree position  
[https://www.babylonjs-playground.com/#09HB40#7](https://www.babylonjs-playground.com/#09HB40#7)

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 15, 2020, 1:53pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/9 "2020-11-15T13:53:46Z")

</div>

any idea what I am doing wrong here? line 81

```
        var rayPosition = new BABYLON.Vector3(startX,-5,startZ);
        var ray = new BABYLON.Ray(rayPosition, new BABYLON.Vector3(0, 10, 0), 50);

```

can’t align ray origin position with tree position [https://www.babylonjs-playground.com/#09HB40#8](https://www.babylonjs-playground.com/#09HB40#8)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 15, 2020, 2:11pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/10 "2020-11-15T14:11:38Z")

</div>

You should not bake the change of position inside the master mesh, else you off-center all the trees.

Also, you need to take into account the x/z offsets you applied on the master mesh when creating the ray:

Finally, you should truncate the matrix buffer to account for the trees that have not been added to the buffer:

[https://www.babylonjs-playground.com/#09HB40#9](https://www.babylonjs-playground.com/#09HB40#9)

---

<div class="post-metadata">

**Author:** ![Dshah\_H](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dshah_h/32/3607_2.png) [@Dshah\_H](https://forum.babylonjs.com/u/Dshah_H)\
**Post date:** [November 15, 2020, 2:53pm UTC](https://forum.babylonjs.com/t/thininstace-hide-or-move-from-grounds/15705/11 "2020-11-15T14:53:51Z")

</div>

You are always a big help, Thank you 🙇‍♂️
