Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question can i ask about normal map scripting?

Discussion in 'Shaders' started by coladraco, Sep 23, 2022.

  1. coladraco

    coladraco

    Joined:
    Dec 9, 2021
    Posts:
    12
    here is normal map what is baked from blender, type changed "Normal map" in unity
    normal.png


    in surface shader, it works so.. i think that maybe has problem with my script.
    surfaceshader.png


    and here is code..


    Code (CSharp):
    1. Shader "Unlit/NewUnlitShader"
    2. {
    3.     Properties
    4.     {
    5.         _NormalMap ("Texture", 2D) = "Normal" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             HLSLPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    20.      
    21.      
    22.      
    23.             struct appdata
    24.             {
    25.                 float4 vertex:POSITION;
    26.                 float2 uv:TEXCOORD0;
    27.                 float3 tangent:TANGENT;
    28.                 float3 normal:NORMAL;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 vertex:SV_POSITION;
    34.                 float2 uv:TEXCOORD0;
    35.                 float3 tangent:TEXCOORD1;
    36.                 float3 normal:TEXCOORD2;
    37.                 float3 bitangent:TEXCOORD3;
    38.             };
    39.  
    40.             TEXTURE2D(_NormalMap);
    41.             SAMPLER(sampler_NormalMap);
    42.  
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex=TransformObjectToHClip(v.vertex.xyz);
    47.                 o.uv=v.uv;
    48.                 o.tangent=v.tangent;
    49.                 o.normal=v.normal;
    50.                 return o;
    51.  
    52.             }
    53.  
    54.             real4 frag (v2f i) : SV_Target
    55.             {
    56.                 //calculate bitangent
    57.                 i.bitangent=cross(i.normal,i.tangent);
    58.                 //tbn matrix
    59.                 float4x4 tbn=float4x4(i.tangent,0,i.bitangent,0,i.normal,0,0,0,0,1);
    60.                 //compose matrix
    61.                 tbn=mul(GetObjectToWorldMatrix(),transpose(tbn));
    62.                 //get tangent space normal
    63.                 float3 normalmap=SAMPLE_TEXTURE2D(_NormalMap,sampler_NormalMap,i.uv).wyxz*2-1;
    64.                 //world transform
    65.                 normalmap=mul(tbn,normalmap);
    66.                 //calculate diffuse
    67.                 float ndl=saturate(dot(normalmap,_MainLightPosition.xyz));
    68.  
    69.                 //output
    70.                 real4 col=real4(ndl,ndl,ndl,1);
    71.                 return col;
    72.             }
    73.             ENDHLSL
    74.         }
    75.     }
    76. }
    (i do my best..)


    according to the result (ratio of shadow), it roughly works. or my delusion..

    direction.png

    the mesh is triangulated, and why it has wave??


    ----------------------------------------------------------------------------------------------------------------

    i changed green channel negative and got some progress..


    negY.png
     
    Last edited: Sep 23, 2022
  2. coladraco

    coladraco

    Joined:
    Dec 9, 2021
    Posts:
    12
    done!.png

    finally did it..

    in my case, it must be recalculated what is normalmap's blue channel..(in UnpackNormal function)

    and flip green channel too!