Modular/Procedural Visibility of Meshes

Hi,

I would like to show/hide parts of a mesh. As an example think of a building that is placed, which hides 90% of the structure on placement. Each time the building is being worked on, some percentage is revealed. How do I achieve something like this?

1 Reply

There are a number of different possible solutions. If you are talking about a 3D object, then there is an additional hurdle to overcome. Meshes are one sided. Most shaders cull out back sides. So imagine you had a box with the top off. If you look down inside the box you won't see the insides...you will see through the box. A typical solution is to modify the shader to turn the culling off. This will display both sides, but it will display both sides as lit from the front side. So you lighting may not look right.

Here are some ideas. Some are "in theory" since I've never written the specific code or used the specific concept.

1) Use a mask

There are shaders that use a mask and a cutoff. You can create a mask to along with your textures. In the shader you change the cutoff to reveal more the building. This answer uses this method in 2D for a circular progress bar:

If you want your building to go up in stages, then you can define the different parts of your building to have different materials. Then at runtime, you modify the alpha of the main color for each material to expose different areas. This requires a shader that supports transparency and has a main color.

3) Texture atlas and changing UV

This one is a bit more complex. It assumes you are using a texture atlas for mapping the texture onto the building. You can dynamically change the uv in the material to point to different parts of the atlas. Parts that have not be exposed, point to a part of the atlas that is clear. As they are exposed, uvs are changed to another part of the texture.

4) Vertex colors

Using a shader that supports vertex colors and transparency, you can change the vertex colors in the material.

5) Custom shader

You can write a shader based on a parameter only show part of the pixels (based on height for example).

6) Direct manipulation of the texture.

You can directly modify the texture used in the material and alpha values.

I guess the first one is the easiest for me to do, since I am not a technical guy. I rather focus on game mechanics and logic. Number 2 is probably easy to do too, but got more overhead the more buildings I am using in my project. 5 and 6 are also worth looking into.

Well, it's quite simple. Mesh renderers have a setting for a materials array. By setting that to length 2, you can assign two materials to the same mesh. One of these will be the normal material, and the other will be the Inverted version found in my shaders. This will draw both faces (note that the renderer will have to draw the object twice to do this, so it will be more expensive). My shader calculates the correct lighting for the back faces automatically, so it will look like a correctly lit, 2-sided object. There is an example of a two-sided mesh in the demo scene, which you can look at if you have any trouble.