Search Unity

Question Billboard effect shader in GPU Instanced Shader

Discussion in 'Shaders' started by Ellva, Feb 23, 2022.

  1. Ellva

    Ellva

    Joined:
    Jan 13, 2020
    Posts:
    7
    In the GPU Instanced scripts. https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html
    I want to change the shader to Billboard seems like the shader
    Code (CSharp):
    1. Shader "Unlit/Billboard"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "Queue"="Transparent" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             ZWrite Off
    15.             Blend SrcAlpha OneMinusSrcAlpha
    16.             Cull Off
    17.  
    18.             HLSLPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             #include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitInput.hlsl"
    23.             #include "Packages/com.unity.render-pipelines.universal/Shaders/SimpleLitForwardPass.hlsl"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float4 vertex : SV_POSITION;
    35.                 float2 uv : TEXCOORD0;
    36.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    37.             };
    38.  
    39.             TEXTURE2D(_MainTex);
    40.             SAMPLER(sampler_MainTex);
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0, 0, 0, 1)) + float4(v.vertex.xyz, 0.0));
    46.                 o.uv = v.uv;
    47.                 return o;
    48.             }
    49.  
    50.             float4 frag (v2f input) : SV_Target
    51.             {
    52.                 float4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
    53.                 return col;
    54.             }
    55.             ENDHLSL
    56.         }
    57.     }
    58. }
    59.  
    I change the official shader to
    Code (CSharp):
    1. Shader "Instanced/InstancedShader" {
    2.     Properties{
    3.         _MainTex("Albedo (RGB)", 2D) = "white" {}
    4.     }
    5.         SubShader{
    6.  
    7.             Pass {
    8.  
    9.                 Tags {"LightMode" = "ForwardBase"}
    10.  
    11.                 CGPROGRAM
    12.  
    13.                 #pragma vertex vert
    14.                 #pragma fragment frag
    15.                 #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    16.                 #pragma target 4.5
    17.  
    18.                 #include "UnityCG.cginc"
    19.                 #include "UnityLightingCommon.cginc"
    20.                 #include "AutoLight.cginc"
    21.  
    22.                 sampler2D _MainTex;
    23.  
    24.             #if SHADER_TARGET >= 45
    25.                 StructuredBuffer<float4> positionBuffer;
    26.             #endif
    27.  
    28.                 struct v2f
    29.                 {
    30.                     float4 pos : SV_POSITION;
    31.                     float2 uv_MainTex : TEXCOORD0;
    32.                     float3 ambient : TEXCOORD1;
    33.                     float3 diffuse : TEXCOORD2;
    34.                     float3 color : TEXCOORD3;
    35.                     SHADOW_COORDS(4)
    36.                 };
    37.  
    38.                 void rotate2D(inout float2 v, float r)
    39.                 {
    40.                     float s, c;
    41.                     sincos(r, s, c);
    42.                     v = float2(v.x * c - v.y * s, v.x * s + v.y * c);
    43.                 }
    44.  
    45.                 v2f vert(appdata_full v, uint instanceID : SV_InstanceID)
    46.                 {
    47.                 #if SHADER_TARGET >= 45
    48.                     float4 data = positionBuffer[instanceID];
    49.                 #else
    50.                     float4 data = 0;
    51.                 #endif
    52.  
    53.                     float rotation = data.w * data.w * _Time.x * 0.5f;
    54.                     rotate2D(data.xz, rotation);
    55.  
    56.                     float3 localPosition = v.vertex.xyz * data.w;
    57.                     float3 worldPosition = data.xyz + localPosition;
    58.                     float3 worldNormal = v.normal;
    59.  
    60.  
    61.  
    62.                     float3 ndotl = saturate(dot(worldNormal, _WorldSpaceLightPos0.xyz));
    63.                     float3 ambient = ShadeSH9(float4(worldNormal, 1.0f));
    64.                     float3 diffuse = (ndotl * _LightColor0.rgb);
    65.                     float3 color = v.color;
    66.                     float4 vPos = mul(unity_WorldToObject, float4(worldPosition, 1.0f));
    67.                     v2f o;
    68.                     o.pos = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0, 0, 0, 1)) + float4(vPos.xyz, 0));
    69.                     o.uv_MainTex = v.texcoord;
    70.                     o.ambient = ambient;
    71.                     o.diffuse = diffuse;
    72.                     o.color = color;
    73.                     TRANSFER_SHADOW(o)
    74.                     return o;
    75.                 }
    76.  
    77.                 fixed4 frag(v2f i) : SV_Target
    78.                 {
    79.                     fixed shadow = SHADOW_ATTENUATION(i);
    80.                     fixed4 albedo = tex2D(_MainTex, i.uv_MainTex);
    81.                     float3 lighting = i.diffuse * shadow + i.ambient;
    82.                     fixed4 output = fixed4(albedo.rgb * i.color * lighting, albedo.w);
    83.                     UNITY_APPLY_FOG(i.fogCoord, output);
    84.                     return output;
    85.                 }
    86.  
    87.                 ENDCG
    88.             }
    89.     }
    90. }
    but the position calculate seed not correct.

    float4 vPos = mul(unity_WorldToObject, float4(worldPosition, 1.0f));
    v2f o;
    o.pos = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0, 0, 0, 1)) + float4(vPos.xyz, 0));


    anyone can help me, thanks!