#Implementing 3D occlusion shader for a GridMap?

1 messages · Page 1 of 1 (latest)

green grove
#

In my 3D game, when a player runs behind a tree I want to hide the tree, so the player can continue to see themselves. However, this tree is an obstacle in a GridMap, which makes this difficult.

Raycasting from the camera can tell you "the GridMap is in the way" but it cannot tell you which cell. You can use local_to_map for small objects which only span a single cell, but for large objects like this tree the cell could be far away from ray collision.

Instead of raycasts, I considered checking the GridMap cells in front the player and setting a shader parameter for those meshes. However, GridMap doesn't expose any kind of "get_mesh_at(cell)" method, so I wouldn't have an object to set the shader parameter on. ...However, GridMap does expose a get_meshes() method which exposes all the meshes and their transforms.

So the best idea I can think of is, "call get_meshes(), reverse-engineer a lookup table based on the transforms, and use that to get the mesh at a specific cell and turn it invisible."

Is this weird "reverse lookup table to get the mesh at a specific cell" really the best approach here? Or is there a simpler approach, or is this a case where GridMaps are the wrong tool for the job?

cosmic stirrupBOT
forest citrus
#

maybe you are missing a step..

   var local_pos: Vector3 = grid_map.to_local(sample_pos)
   var cell: Vector3i = grid_map.local_to_map(local_pos)

first convert to local, then call local_to_map to get the cell.

var item_id: int = grid_map.get_cell_item(cell)

I think you also need to move the raycast point slightly into the small (along the inverse normal) to make sure you get the right cell...

green grove
#

Regardless, even if I can find the "cell item" -- that points to an index in the mesh library, not a mesh instance in the world

#

I appreciate the help though, it's at least good to know that other experienced developers don't have an easy answer. I thought of another strange solution last night, which would be to erase cells from the GridMap as the player approaches them, replacing them with real meshes which I can manipulate with shader parameters (sort of like an ad-hoc LOD GridMap system)

forest citrus
#

I wouldn't call myself very experienced in this... So take what I say with a grain of salt.. is it possible to, instead of hiding the tree, draw another player on top of the tree instead? Although perhaps that doesn't give you the look you are going for...

green grove
forest citrus
#

I assume you want something like this? the sand floor and the "tree" in the middle are grid_map tiles...

#

I am not sure how you set things up, since your assets look 2D.. 🙂 is the tree a mesh (plane mesh?) .. and I assume it's occluding some ground cells behind it, yes>

#

And I suppose another way to deal with this is to have your trees be separate items (Scenes) rather than grid_map nodes.. maybe that simplifies life.. but perhaps makes creating the scene more difficult..

#

Anyway, the way this works is by replacing the "tree" mesh library item with another "faded tree" item, which is exactly the same, but has transparency set on it...