Search Unity

Question What data should be passed to the shader for normal rendering?

Discussion in 'Shaders' started by Zimaell, Feb 25, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    I bake the animation into a texture for better performance, the main step is already done, the vertices themselves are baked and rendered using a shader.

    But of course I don’t see on it an answer and a cast of a shadow, or maybe something else.

    The question is - what else do I need to bake into the texture to transfer, and what can just be calculated?

    (if possible examples or links, or in which direction to move)
    below is an example, baked on the left, original on the right (low poly of course)
    (URP Project)
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      154.5 KB
      Views:
      81
  2. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    I made changes in the shader and now it accepts normals, but it still does not react to light, here is the shader
    Code (CSharp):
    1. Shader "Animations/VertexAnimation"{
    2.     Properties{
    3.         _MainTex ("Texture", 2D) = "white" {}
    4.         _AnimMap ("Animation Texture", 2D) ="white" {}
    5.         _AnimNorm ("Animation Normals", 2D) ="white" {}
    6.         }
    7.     SubShader{
    8.         Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalRenderPipeline" }
    9.         Cull off
    10.         Pass{
    11.             Tags{ "LightMode"="UniversalForward" }
    12.  
    13.             HLSLPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    17.  
    18.             struct appdata{
    19.                 float2 uv       : TEXCOORD0;
    20.                 float4 pos      : POSITION;
    21.                 half3 normal    : NORMAL;
    22.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    23.                 };
    24.  
    25.             struct v2f{
    26.                 float2 uv       : TEXCOORD0;
    27.                 float4 vertex   : SV_POSITION;
    28.                 float3 normal   : TEXCOORD1;
    29.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    30.                 };
    31.  
    32.             CBUFFER_START(UnityPerMaterial)
    33.                 sampler2D _MainTex;
    34.                 float4 _MainTex_ST;
    35.                 sampler2D _AnimMap;
    36.                 float4 _AnimMap_TexelSize;
    37.                 sampler2D _AnimNorm;
    38.                 float4 _AnimNorm_TexelSize;
    39.             CBUFFER_END
    40.  
    41.             float4 ObjectToClipPos (float3 pos){
    42.                 return mul (UNITY_MATRIX_VP, mul (UNITY_MATRIX_M, float4 (pos,1)));
    43.                 }
    44.  
    45.             v2f vert(appdata v, uint vid : SV_VertexID){
    46.                 UNITY_SETUP_INSTANCE_ID(v);
    47.                 float x = (vid + 0.5) * _AnimMap_TexelSize.x;
    48.                 float y = fmod(_Time.y / 1, 1);
    49.                 float4 pos = tex2Dlod(_AnimMap, float4(x, y, 0, 0));
    50.                 float3 normal = tex2Dlod(_AnimNorm, float4(x, y, 0, 0));
    51.                 v2f o;
    52.                 o.vertex = ObjectToClipPos(pos);
    53.                 o.normal = TransformObjectToWorldNormal(normal);
    54.                 o.uv = v.uv;
    55.                 return o;
    56.                 }
    57.  
    58.             half4 frag (v2f i) : SV_Target{
    59.                 half diff = dot(i.normal, float3(0,1,0)) * 0.5 + 0.5;
    60.                 half4 col = tex2D(_MainTex, i.uv);
    61.                 return diff * col;
    62.                 }
    63.        
    64.             ENDHLSL
    65.             }
    66.        
    67.         }
    68.     }
    what else is missing?
     

    Attached Files:

  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
  4. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    766
    Hi, you can check URP's Lit shader (link is for 2023.1b) to calculate lighting.

    The UniversalFragmentPBR is URP's lighting function in "Lighting.hlsl".
    Code (CSharp):
    1. half4 color = UniversalFragmentPBR(inputData, surfaceData);
    I would suggest using shader graph to create this VAT shader because you don't need to manually calculate lighting and adding new URP features to the shader.

    You can create custom functions if there's no equivalent node in shader graph.
    Shader Graph.jpg
     
    Zimaell likes this.
  6. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    766
    By the way, you should use "SAMPLE_TEXTURE2D_LOD(_AnimMap, sampler_AnimMap, uv, lod)" rather than "tex2Dlod()" in shader graph's custom functions.

    Anyway, there's a node for sampling texture2D lod.
     
    Zimaell likes this.
  7. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    409
    after digging through the lit shader, I crossed it with my code, in general it turned out, now the baked animation with all the necessary overlays, you can’t distinguish it from the original...
    thank you all for your help.
     
    wwWwwwW1 likes this.