Search Unity

Tangent space normal map seams problem [Fixed]

Discussion in 'General Graphics' started by florianalexandru05, Feb 4, 2016.

  1. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    912
    So I made a cave, UVed it, painted the texture and even baked the normals. I fixed the seams and I even repainted the normal map so it's seamless but the shading where the seams are turn out wrong. I tried everything out there but nothing works! Object space normals work fine so it has something to do with the tangent space normal. http://wiki.polycount.com/wiki/Normal_Map_Technical_Details I tired using the software in the link to convert the object space to a tangent space normal, no seams but the shading is wrong so I need the traditional tangled normal to work. Anybody have a idea?



    The normal maps don't appear to have any seems yet they show up in every 3d software/engine out there.




    You can take a look at the cave

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,139
    If you're using tangent space normals and you have a seam in your UVs there's likely going to be a visual seam in the tangent space normal map if you view it directly. You can't just use your tangent space normal map as a texture and see if it's working, and you definitely don't want to paint out seams you see when you do.

    Code (CSharp):
    1.  
    2. Shader "Unlit/Texture" {
    3. Properties {
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     [NoScaleOffset] _BumpMap ("Normal Map", 2D) = "bump" {}
    6.     [KeywordEnum(None, Mesh Normals, Mesh Tangents, Tangent Normals, World Normals)] _Display ("Debug Display", Float) = 0
    7. }
    8.  
    9. SubShader {
    10.     Tags { "RenderType"="Opaque" }
    11.     LOD 100
    12.    
    13.     Pass {  
    14.         CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #pragma multi_compile _ _DISPLAY_MESH_NORMALS _DISPLAY_MESH_TANGENTS _DISPLAY_TANGENT_NORMALS _DISPLAY_WORLD_NORMALS
    18.            
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct v2f {
    22.                 float4 vertex : SV_POSITION;
    23.                 half2 texcoord : TEXCOORD0;
    24.                 half3 tspace0 : TEXCOORD1; // tangent.x, bitangent.x, normal.x
    25.                 half3 tspace1 : TEXCOORD2; // tangent.y, bitangent.y, normal.y
    26.                 half3 tspace2 : TEXCOORD3; // tangent.z, bitangent.z, normal.z
    27.             };
    28.  
    29.             sampler2D _MainTex;
    30.             sampler2D _BumpMap;
    31.  
    32.             fixed4 _LightColor0;
    33.            
    34.             v2f vert (appdata_tan v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    38.                 o.texcoord = v.texcoord;
    39.  
    40.                 half3 wNormal = UnityObjectToWorldNormal(v.normal);
    41.                 half3 wTangent = UnityObjectToWorldDir(v.tangent.xyz);
    42.                 // compute bitangent from cross product of normal and tangent
    43.                 half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    44.                 half3 wBitangent = cross(wNormal, wTangent) * tangentSign;
    45.                 // output the tangent space matrix
    46.                 o.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x);
    47.                 o.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y);
    48.                 o.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z);
    49.                 return o;
    50.             }
    51.            
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 #ifdef _DISPLAY_MESH_NORMALS
    55.                 return fixed4(fixed3(i.tspace0.z, i.tspace1.z, i.tspace2.z) * 0.5 + 0.5, 1);
    56.                 #endif
    57.  
    58.                 #ifdef _DISPLAY_MESH_TANGENTS
    59.                 return fixed4(fixed3(i.tspace0.x, i.tspace1.x, i.tspace2.x) * 0.5 + 0.5, 1);
    60.                 #endif
    61.  
    62.                 half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.texcoord));
    63.  
    64.                 #ifdef _DISPLAY_TANGENT_NORMALS
    65.                 return fixed4(tnormal * 0.5 + 0.5, 1);
    66.                 #endif
    67.  
    68.                 half3 worldNormal;
    69.                 worldNormal.x = dot(i.tspace0, tnormal);
    70.                 worldNormal.y = dot(i.tspace1, tnormal);
    71.                 worldNormal.z = dot(i.tspace2, tnormal);
    72.  
    73.                 #ifdef _DISPLAY_WORLD_NORMALS
    74.                 return fixed4(worldNormal * 0.5 + 0.5, 1);
    75.                 #endif
    76.  
    77.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    78.                 half ndotl = dot(worldNormal, -_WorldSpaceLightPos0.xyz);
    79.                 col.rgb *= (ndotl * 0.5 + 0.5) * _LightColor0.rgb;
    80.  
    81.                 return col;
    82.             }
    83.         ENDCG
    84.     }
    85. }
    86.  
    87. }
    88.  
    Stick this shader on your mesh and try the different debug display options. None is just the diffuse texture with a half lambert lighting from the directional light, mesh normals are the mesh's world space normals, mesh tangents are the mesh's world space tangents, tangent normals is the tangent space normal map (basically just the normal texture), and world normals are world space normals from the tangent space normal map.

    If you look at the mesh tangents there will be some hard edges, and this is to be expected. Tangents are basically the direction of the UVs across the surface. If you look at the tangent normals there should also be some hard edges. Where you don't want edges is in the world space normals.
     
  3. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    912
    Thanks though that is not really a solution to my problem since I am selling these as game assets and I need the tangent space normals to work. The problem was fixed, seems there was a option in blender I overlooked. I'll share about it later so people might know about it if they encounter the same problem.

    Nice shader, I will take a look at it.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,139
    The shader is simply a way to visualize how the system actually works (and to check for errors).
     
  5. gritchie

    gritchie

    Joined:
    Mar 17, 2016
    Posts:
    1
    What was the blender option you overlooked?
     
    florianalexandru05 likes this.
  6. florianalexandru05

    florianalexandru05

    Joined:
    Mar 31, 2014
    Posts:
    912
    This one:

    Screenshot_78.png
     
    Pandur1982 likes this.