World Position Shader Examples (Line of Sight Prototype)
Hi everyone!

(I've been in Berlin for a week for Unite Berlin 2018, apologies for the late post. Grass interaction post is coming very soon after this one)  

In this article I'm showing a few more examples to use with the World Position Based Shader 

 Just to be sure, sharing the code again:

C# : PasteBin Link
Shader Code: PasteBin Link 


Editing the double texture shader to use alpha instead of the second texture is very simple.
All you need to do is use step with the alpha, so it cuts off outside of the radius given

c.a = step(_DissolveAmount, _Radius);`

Here is the full code, very similar to the Dissolve Shader :

Shader: PasteBin Link


Here's a probably more practical example, making parts disappear around the position.

Which is of course just the opposite of the appear shader

 c.a = step(_Radius, _DissolveAmount);

Adding Arrays To Shader

At this point, the shader works with 1 point of influence, which is fine for a lot of cases, but I wanted to try and send multiple positions through.

So we have to add an array of Vector3.

I couldn't figure out how to make the array work with a variable amount at runtime. So for this example it's hardcoded to 100 positions. But this is easy to change.

To send an array to the shader, make an array of positions of your interactors, and use Shader.SetGlobalVectorArray.

So something like this:

for (int i = 0; i < 100; i++)
          positions[i] = objects[i].transform.position;
Shader.SetGlobalVectorArray("_Positions", positions);

Then in the shader, go through the array and add position + radius to a float3

float3 combinedRadius;
for  (int i = 0; i < 100; i++)
           float3 dis =  distance(_Positions[i], IN.worldPos);
           float3 sphereR = 1-  saturate(dis / _Radius);
           combinedRadius += (sphereR);

Use this combined value instead of the single radius

Disappear shader with multiple radius: PasteBin Link 

Line of Sight + Fog of War Prototype

Finally putting it all together for a sort of RTS prototype, using the earlier shader and adding in the RenderTexture paintmap from the Map System Tip 

o.Albedo = (texture * paintMap) ;
o.Alpha = clamp(c.a - (( 1 - paintMap) * 0.5), 0,1);

First making sure the Albedo shows the cloud texture on the unexplored regions.
Then clamp the alpha on the paintMap and make sure it shows darker on the explored, but not active in line of sight areas.

 You can also see the Appear shader in the "enemy" units, only showing up when the player units are close 

I added this shader to a plane above the map, it might work as an Image Effect too.

Thanks for reading!

Become a patron to

Unlock 115 exclusive posts
Be part of the community
Connect via private message