- Home /
Shader issue: Normal map from tangent to world space - Textures flipping?
I'm currently trying to write a snow shader. I'd like the snow to appear on top of a mesh, of course. The snow covered areas should also depend on the material's normal (bump) map so that it appears on top of the details provided by the normal map as well.
To achieve this I convert the normal map from tangent to world space in the pixel shader. The amount of snow then corresponds to the normal map's y component.
This works quite well so far except for one issue: When rotating the GameObject at certain angles the textures seem to flip so that the snow is no longer on top of the details provided by the normal map but beneath them.
Here's a video illustrating the issue (the snow is painted blue for testing purposes): http://www.youtube.com/watch?v=6VG-yoeqqWk. As you can see the blue snow at first appears on top of the rocks "sticking out" of the cube. But at 45° the textures flip (they flip again at 135°, 225° and 315°).
What could be wrong with my shader?
Shader "SnowMaterial"
{
Properties
{
_SnowColor("_SnowColor", Color) = (1,1,1,1)
_SnowBump("_SnowBump", 2D) = "bump" {}
_MainTex("Base (RGB) Gloss (A)", 2D) = "black" {}
_BumpMap("Normalmap", 2D) = "bump" {}
}
SubShader
{
Tags
{
"Queue" = "Geometry"
"IgnoreProjector" = "False"
"RenderType" = "Opaque"
}
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Fog { }
CGPROGRAM
#pragma surface surf BlinnPhong vertex:vert
#pragma target 3.0
float4 _SnowColor;
sampler2D _SnowBump;
sampler2D _MainTex;
sampler2D _BumpMap;
struct Input
{
float3 sWorldNormal;
float2 uv_SnowBump;
float2 uv_MainTex;
float2 uv_BumpMap;
float4 tangent;
};
void vert(inout appdata_full v, out Input o)
{
v.normal = (float4( v.normal.x, v.normal.y, v.normal.z, 1.0)).xyz;
o.sWorldNormal = mul((float3x3)_Object2World, SCALED_NORMAL);
o.tangent = mul(v.tangent, _Object2World);
}
void surf(Input IN, inout SurfaceOutput o)
{
float4 mainTex = tex2D(_MainTex, IN.uv_MainTex);
float3 mainBump = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
float3 snowBump = UnpackNormal(tex2D(_SnowBump, IN.uv_SnowBump));
float3 tangent = normalize(IN.tangent.xyz);
float3 normal = normalize(IN.sWorldNormal);
float3 binormal = normalize(cross(normal, tangent) * IN.tangent.w);
float3x3 tangentToWorld = transpose(float3x3(tangent, binormal, normal));
float3 amount = saturate(mul(tangentToWorld, normalize(mainBump)));
o.Albedo = lerp(mainTex, _SnowColor, amount.y);
o.Normal = lerp(mainBump, snowBump, amount.y);
}
ENDCG
}
Fallback "Diffuse"
}
Answer by SvenEV · Aug 11, 2013 at 03:03 PM
Looks like
o.tangent = mul(v.tangent, _Object2World);
should actually be
o.tangent = mul(_Object2World, v.tangent);
Now the snow is always on top of the rocks at any rotation.
It's very long time to expect relpy.. but i got a one question.
line 62 : float3x3 tangentToWorld = transpose(float3x3(tangent, binormal, normal));
why did you transpose matrix?
As i know, float3x3(tangent, binormal, normal) is computed to
[ [ Tx, Ty, Tz ] [ Bx, By, Bz ] [ Nx, Ny, Nz ] ]
and that is the result of tangentToWorld $$anonymous$$atrix..?
I'm confused cause i learned tangent space few days ago. Please let me know your idea!
Thx
Your answer
Follow this Question
Related Questions
Normal mapping mirrored UVs? 2 Answers
Mobile bump shader not working 1 Answer
Normal map mask 2 Answers
Mobile Bumped Diffuse problem 1 Answer
Two bump map shader 0 Answers