- Home /
Parallax offset in custom shader ?
Hi everyone,
I'm wondering how to use Unity's parallax offset (also called height mapping) in a custom surface shader. So far I've found this:
float2 offset = ParallaxOffset(tex2D(_HeightMap, IN.uv_MainTex).w, _HeightIntensity, IN.viewDir);
IN.uv_MainTex.x -= offset.x;
IN.uv_MainTex.y += offset.y;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex); // etc
However, this only works if the fragment's normal is the world forward vector (0, 0, 1). How can I make it depend on the fragment's normal ?
Thanks in advance.
Answer by jonathb · Jan 18, 2017 at 06:22 PM
Found it ! Actually no crazy vector rotation was needed, it seems in order for parallax mapping to work correctly you also need to apply it to the bumpmap. Weird, but thanksfully simple to do:
float2 offsetMainTex = ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
float2 offsetBumpMap = ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
fixed4 c = tex2D (_MainTex, IN.uv_MainTex + offsetMainTex) * _Color;
o.Albedo = c.rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap + offsetBumpMap));
Answer by quizcanners · Feb 25, 2018 at 09:56 PM
You don't really need to do the same calculation twice. Also if your MainTex and Bump offset and tiling is the same:
IN.uv_MainTex += ParallaxOffset(tex2D(_ParallaxMap, IN.uv_MainTex).r, _Parallax, IN.viewDir);
o.Albedo = (tex2D (_MainTex , IN.uv_MainTex ) * _Color).rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex ));
Your answer
Follow this Question
Related Questions
Offset and Tiling Problem in Parallax 0 Answers
Simple Shader: Pushing vertecies towards Camera 0 Answers
renderer.material.mainTextureOffset based on player movement 0 Answers
Uniformly spreading a texture across multiple objects using the Unity standard shader 0 Answers
How to change pixel world position in surface shader before lighting 0 Answers