Search Unity

World Relative UVs with normalmaps

Discussion in 'Shaders' started by Setsuki, Oct 10, 2013.

  1. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Hello !
    I am currently using a shader allowing me to place my texture relative to the world. I simply put the textures used for the floor (normals pointing up) and for the walls (normals pointing elsewhere).
    Code (csharp):
    1.  
    2. struct Input
    3. {
    4.     float3 worldNormal;
    5.     float3 worldPos;
    6. };
    7.  
    8.  
    9. void surf (Input IN, inout SurfaceOutput o)
    10. {
    11. float2 UV;
    12. fixed4 c;
    13.  
    14.     if(abs(IN.worldNormal.x)>0.5)
    15.     {
    16.         UV = IN.worldPos.zy; // side
    17.         c = tex2D(_MainTexWall, UV* _Scale); // use WALLSIDE texture
    18.     }
    19.     else if(abs(IN.worldNormal.z)>0.5)
    20.     {
    21.         UV = IN.worldPos.xy; // front
    22.         c = tex2D(_MainTexWall, UV* _Scale); // use WALL texture
    23.     }
    24.     else
    25.     {
    26.         UV = IN.worldPos.xz; // top
    27.         c = tex2D(_MainTexFlr, UV* _FlrScale); // use FLR texture
    28.     }
    29.  
    30. o.Albedo = c.rgb;
    31. }
    32.  
    I want to add normalmapping to this shader. Some of you might already know what my question is going to be since the problem is written in the doc :
    I can't read worldNormal as soon as I add
    Code (csharp):
    1.  o.Normal = something
    and
    Code (csharp):
    1.  
    2. struct Input
    3. {
    4.     float3 worldNormal;
    5.     float3 worldPos;
    6.         INTERNAL_DATA
    7. };
    8.  
    But using WorldNormalVector(IN, o.Normal) doesn't seem to do much if i haven't changed the value of o.Normal before...

    I feel like in order to read my worldNormal, I need to write it myself, which doesn't seem possible since I can't read it...

    So, does anybody have any idea so I can add normalmapping to my textures ?

    Thanks a lot for your help !
     
  2. Setsuki

    Setsuki

    Joined:
    Aug 21, 2012
    Posts:
    31
    Here is the solution by Orihaus in the comments of http://www.blog.radiator.debacle.us/2012/01/joys-of-using-world-space-procedural.html

    Code (csharp):
    1.  
    2.     Shader "WorldUV/Bumped Diffuse" {
    3.         Properties
    4.         {
    5.                 _MainTex ("Base", 2D) = "white" {}
    6.                 _BumpMap ("Normalmap", 2D) = "bump" {}
    7.                 _Scale ("Scale", float) = 1.0
    8.         }
    9.        
    10.         SubShader
    11.         {
    12.                 Tags { "RenderType"="Opaque" }
    13.                 LOD 400
    14.        
    15.                 CGPROGRAM
    16.                 #pragma surface surf Lambert
    17.                
    18.                
    19.                 struct Input
    20.                 {
    21.                     float4 color : COLOR;
    22.                         float2 uv_MainTex;
    23.                         float2 uv_BumpMap;
    24.                         float3 worldPos;
    25.                         float3 worldNormal; INTERNAL_DATA
    26.                 };
    27.                
    28.                 sampler2D _MainTex;
    29.                 sampler2D _BumpMap;
    30.                 fixed4 _Color;
    31.                
    32.                 half _Scale;
    33.                
    34.                 void surf (Input IN, inout SurfaceOutput o)
    35.                 {
    36.                         float3 correctWorldNormal = WorldNormalVector(IN, float3( 0, 0, 1 ) );
    37.                         float2 uv = IN.worldPos.zx;
    38.                        
    39.                         if( abs( correctWorldNormal.x ) > 0.5 ) uv = IN.worldPos.zy;
    40.                         if( abs( correctWorldNormal.z ) > 0.5 ) uv = IN.worldPos.xy;
    41.  
    42.                         uv.x *= _Scale;
    43.                         uv.y *= _Scale;
    44.                        
    45.                         fixed4 tex = tex2D( _MainTex, uv );
    46.                         o.Albedo = IN.color * tex.rgb ;
    47.                         o.Normal = UnpackNormal( tex2D( _BumpMap, uv ) );
    48.                 }
    49.                
    50.                 ENDCG
    51.         }
    52.     FallBack "Bumped Specular"
    53.     }
    54.