I've played around a little with parallax effects in the Procgen Eyeball shader .
But there are more visuals you can use this effect for.
So in this post i'll show off a few experiments/examples.
What is Parallax?It's the effect of fake depth by moving UVs using the view direction
This plane is flat, but has a lot of fake depth in it.First, how to set it up:
Parallax offset
Unity already has a built-in function that handles the calculation for a parallax offset
(From the unity github)
Here the h is a texture sample of a depthmap, height is the depth/strength of the parallax effect, and of course viewdir is the view direction
Let's use it in a basic shader:
Create a new surface shader and declare a few extra properties
_DepthMap("DepthMap", 2D) = "black" {}
_Normal("Normal", 2D) = "bump" {}
_Parallax("Parallax Depth", Range(0,10)) = 0.0
And don't forget to declare under the Input struct too!
To use the depth map, project it to uv's just like a regular texture:
float dTex = tex2D(_DepthMap, IN.uv_MainTex).r;
Only a single channel is needed for the float, so grab the .r
As you've seen in the code above, the View Direction is also needed, so declare a "float3 viewDir;" in the struct Input to access the built-in variable. In the Input struct, also add a uv for the normal map :"float2 uv_Normal;"
float2 parallax = ParallaxOffset( dTex , _Parallax, IN.viewDir);
This is the offset to add to the uv map
o.Normal = UnpackNormal(tex2D(_Normal, IN.uv_Normal + parallax));
And thats it for the most basic parallax effect on the normal map.
Note: If you don't setup o.Normal, Unity will use the world normals instead of the tangent normals you need for this effect, so even if you don't want to use a normal, add the line anyway, just keep the texture slot empty
It should look something like this
Settings:
Shader Code:(详见购买资源里basicparallax.shader文本)
So knowing this setup, what else can be done?
Instead of adding the parallax offset to the normal map, add it to the diffuse maintex
Now we get this strange deep liquid kinda look
Shader Code:(详见购买资源里basicparallaxdiffuse.shader文本)
Trying something different: What happens if you keep adding the parallax offset over eachother? You get this trippy layered floor effect, reminds me a bit of those infinity mirrors
for (float i = 0; i < _Layers; i++)
{
IN.uv_MainTex += ParallaxOffset(tex2D(_DepthMap, IN.uv_MainTex), _Parallax, IN.viewDir);
}
Just add onto the uv using a for-loop with a variable so you can set the amount of layers you want.
Shader Code: (详见购买资源里basicparallaxdiffuselayers.shader文本)
Settings:
Gemstones(宝石材质)
Here's an example combines the parallax effect with parts of the old Specular Shader(
详见购买资源里toonlitspecular.shader文本) I made a while ago.
It has rim lighting, color gradient, and specular.
toonlitspecular(就是文中说的old Specular Shader)
Shader Code: (详见购买资源里的名为gemstone.shader文本)
Settings:
Ice(冰材质)