Search Unity

Shader Instancing , no longer affected by directional ligth

Discussion in 'Shaders' started by GameMadeByGamers, Jul 12, 2017.

  1. GameMadeByGamers

    GameMadeByGamers

    Joined:
    Nov 25, 2014
    Posts:
    54
    Like mention in the title , i got this billboard shader but am enable to make the directionnal ligth work with instancing on. Do i need to make two pass?

    Code (CSharp):
    1.  
    2.  
    3. Shader "Billboard" {
    4.  
    5.     Properties
    6.     {
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    9.         _MainTex("Albedo Map", 2D) = "white" {}
    10.         _BumpMap("Normal Map", 2D) = "bump" {}
    11.         _BumpIntensity("NormalMap Intensity", Range(-1, 2)) = 1
    12.         _BumpIntensity("NormalMap Intensity", Float) = 1
    13.     }
    14.      
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19.             "Queue" = "Transparent"
    20.             "IgnoreProjector" = "True"
    21.             "RenderType" = "TransparentCutout"
    22.             "DisableBatching" = "True"
    23.         }
    24.  
    25.         LOD 200
    26.         Cull Off
    27.         Lighting On
    28.         ZWrite On
    29.         Fog{ Mode Off }
    30.  
    31.  
    32.         CGPROGRAM
    33.         #pragma target 3.0
    34.         #pragma surface surf Lambert alpha vertex:vert  alphatest:_Cutoff fullforwardshadows
    35.         #pragma multi_compile DUMMY PIXELSNAP_ON
    36.         #pragma exclude_renderers flash
    37.         #define PI 3.1415926535897932384626433832795
    38.         #define RAD2DEG 57.2957795131
    39.         #define SINGLEFRAMEANGLE (360/1)
    40.         #define UVOFFSETX (1/1)
    41.  
    42.         sampler2D _MainTex;
    43.         sampler2D _BumpMap;
    44.         fixed _BumpIntensity;
    45.  
    46.         UNITY_INSTANCING_CBUFFER_START(MyProperties)
    47.             UNITY_DEFINE_INSTANCED_PROP(half4, _Color)
    48.         UNITY_INSTANCING_CBUFFER_END
    49.  
    50.         float atan2Approximation(float y, float x) // http://http.developer.nvidia.com/Cg/atan2.html
    51.         {
    52.             float t0, t1, t2, t3, t4;
    53.             t3 = abs(x);
    54.             t1 = abs(y);
    55.             t0 = max(t3, t1);
    56.             t1 = min(t3, t1);
    57.             t3 = float(1) / t0;
    58.             t3 = t1 * t3;
    59.             t4 = t3 * t3;
    60.             t0 = -0.013480470;
    61.             t0 = t0 * t4 + 0.057477314;
    62.             t0 = t0 * t4 - 0.121239071;
    63.             t0 = t0 * t4 + 0.195635925;
    64.             t0 = t0 * t4 - 0.332994597;
    65.             t0 = t0 * t4 + 0.999995630;
    66.             t3 = t0 * t3;
    67.             t3 = (abs(y) > abs(x)) ? 1.570796327 - t3 : t3;
    68.             t3 = (x < 0) ? PI - t3 : t3;
    69.             t3 = (y < 0) ? -t3 : t3;
    70.             return t3;
    71.         }
    72.  
    73.         struct Input
    74.         {
    75.             float4 pos : SV_POSITION;
    76.             half2 uv : TEXCOORD0;
    77.  
    78.             float2 uv_MainTex;
    79.             float2 uv_BumpMap;
    80.             float2 uv_EmissionMap;
    81.  
    82.             UNITY_VERTEX_INPUT_INSTANCE_ID
    83.         };
    84.  
    85.         void vert(inout appdata_full v, out Input o)
    86.         {
    87.             UNITY_INITIALIZE_OUTPUT(Input, o)
    88.             UNITY_SETUP_INSTANCE_ID(v);
    89.             UNITY_TRANSFER_INSTANCE_ID(v, o);
    90.  
    91.  
    92.             // get the camera basis vectors
    93.             float3 forward = -normalize(UNITY_MATRIX_V._m20_m21_m22);
    94.             float3 up = normalize(UNITY_MATRIX_V._m10_m11_m12);
    95.             float3 right = normalize(UNITY_MATRIX_V._m00_m01_m02);
    96.  
    97.             // rotate to face camera
    98.             float4x4 rotationMatrix = float4x4(right, 0,
    99.                 up, 0,
    100.                 forward, 0,
    101.                 0, 0, 0, 1);
    102.  
    103.             //float offset = unity_ObjectToWorld._m22 / 2;
    104.             float offset = 0;
    105.             v.vertex = mul(v.vertex + float4(0, offset, 0, 0), rotationMatrix) + float4(0, -offset, 0, 0);
    106.             v.normal = mul(v.normal,rotationMatrix);
    107.         }
    108.  
    109.         void surf(Input IN, inout SurfaceOutput o)
    110.         {
    111.             half4 albedo = tex2D(_MainTex, IN.uv_MainTex) *  UNITY_ACCESS_INSTANCED_PROP(_Color);
    112.             o.Albedo = albedo.rgb;
    113.             o.Alpha = albedo.a;
    114.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    115.             _BumpIntensity = 1 / _BumpIntensity;
    116.             o.Normal.z = o.Normal.z * _BumpIntensity;
    117.             o.Normal = normalize((half3)o.Normal);
    118.  
    119.             UNITY_SETUP_INSTANCE_ID(IN);
    120.         }
    121.  
    122.         half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten)
    123.         {
    124.             half4 c;
    125.             c.rgb = s.Albedo * _LightColor0.rgb * (atten);
    126.             c.a = s.Alpha;
    127.             return c;
    128.         }
    129.  
    130.         ENDCG
    131.     }
    132.         Fallback "Off"
    133. }
    Instancing OFF
    upload_2017-7-12_11-24-17.png

    Instancing ON
    upload_2017-7-12_11-23-30.png