# thinInstace hide or move from grounds

Hi,

I am using thinIntance of trees to fill area, here is PG Babylon.js Playground

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

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.

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

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

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.

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.

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

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

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

1 Like

You are always a big help, Thank you