Unity Shaders: Spherical Mask
In the last post I showed you a simple dissolve shader, this time I'd like to talk about what a spherical mask is and how to use it.
Take a look at the gif image above, basically we have a player object (represented by a stretched cube) and a flat plane.
As you can see, the flat plane has a grayscale chessboard texture. Wherever the player moves, the pixels of the chessboard beneath the player become colored if they're inside a certain radius of action starting from the player position.
It's like as if there was an invisible sphere that surrounds the player and any pixel inside this sphere, turns from grayscale to color.
If you take a look at the code in the SphericalMask shader file, you see that at line 49 I first compute the distance from the player to the flat plane.
Then I subtract the _MaskRadius value that represents the radius of action, from the computed distance d.
I also divide by _MaskSoftness to smooth out the transition from grayscale to color.
Finally, at line 53, I simply lerp between grayscale and colored values based on the effectAmount I have just computed.
One last thing. At line 36 in the SphericalMask shader, you see there's a uniform called _PlayerPos.
As you've probably already guessed, that's the position of the player.
I pass the player position to the SphericalMask shader by using the C# script BasicSphericalMask. This script passes the _PlayerPos as a uniform so that it's available globally to all shaders.
This way we have to attach the BasicSphericalMask script only once on a single object and it will work with all the objects with the SphericalMask shader.
I've put up a repository on Github with all three examples if you want to test them out.
Take a look at the gif image above, basically we have a player object (represented by a stretched cube) and a flat plane.
As you can see, the flat plane has a grayscale chessboard texture. Wherever the player moves, the pixels of the chessboard beneath the player become colored if they're inside a certain radius of action starting from the player position.
It's like as if there was an invisible sphere that surrounds the player and any pixel inside this sphere, turns from grayscale to color.
If you take a look at the code in the SphericalMask shader file, you see that at line 49 I first compute the distance from the player to the flat plane.
Then I subtract the _MaskRadius value that represents the radius of action, from the computed distance d.
I also divide by _MaskSoftness to smooth out the transition from grayscale to color.
Finally, at line 53, I simply lerp between grayscale and colored values based on the effectAmount I have just computed.
One last thing. At line 36 in the SphericalMask shader, you see there's a uniform called _PlayerPos.
As you've probably already guessed, that's the position of the player.
I pass the player position to the SphericalMask shader by using the C# script BasicSphericalMask. This script passes the _PlayerPos as a uniform so that it's available globally to all shaders.
This way we have to attach the BasicSphericalMask script only once on a single object and it will work with all the objects with the SphericalMask shader.
I've put up a repository on Github with all three examples if you want to test them out.