Ghost Flask Shader

Ghost Flask Shader

In our game we show ghost projection of resurrection flask when the player can't place the flask.

Shader code:


It is a simple two-pass shader.
First pass (ZTest Greater) renders pixel, if the distance between the object and camera in this pixel’s coordinate is greater than ZBuffer value - meaning our object is being overlapped by something.

Second pass (ZTest LEqual) renders pixel if the distance is less or equal to ZBuffer value. Same as a regular render.

Vertex shaders are identical:

We transform coordinates and normals, calculate direction from a point to camera and transform UV-coordinates, to get tiling working.

Fragment shaders are identical as well, with one exception - the first pass uses color from property _BlockedColor (we have red in the game), the second uses _NormalColor (pale yellow). We will use second as an example.

First line:

The scalar product between the camera direction vector and the normal shows similarity between the angles of these vectors.
“1” means they are unidirectional, “0” stands for 90º angle, “-1” - if they are opposite (but the last case is not going to happen, because this geometry is cut off with the rule). Subtract this value from one to get the opposite effect. Subsequently, this will be our alpha - the edges of the object, turned away from the camera, will be perfectly visible, and the polygons, facing the camera directly, will not be visible.
It makes a great ghost effect - the classic "ghost shader" from many games.

Second line:

Let us multiply the results and write the value into the output color alpha channel. Multiply all of this by _Multiplier - to control the transparency.
In the end, multiply by _Animation to set the appear/disappear animation, changing this value from zero to one and back smoothly.

Last line is self-explanatory:

Basically, that’s it. If you have any questions, please contact us in social medias or write to email!

Share if it was useful