Search Unity

Property instancing Surface shader

Discussion in 'Shaders' started by jister, Jan 26, 2020.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    no idea why it doesn't work, all seems fine to me... but if i change tilling or _CleanTexture value on one instance the other one also applies it.
    Code (CSharp):
    1. Shader "Custom/WaterDirt"
    2. {
    3.     Properties
    4.     {
    5.         [Toggle(DEBUG_NOISE)]_debug("Noise map debug", int) = 0
    6.         [NoScaleOffset]_MainTex("Albedo (RGB)", 2D) = "white" {}
    7.         _TillingOffsetA ("Tilling & Offset", Vector) = (1,1,0,0)
    8.         [NoScaleOffset]_BumpMap("Bumpmap", 2D) = "bump" {}
    9.         [NoScaleOffset]_NoiseTex ("NoiseMask (RGB)", 2D) = "white" {}
    10.         _TillingOffsetN("Tilling & Offset", Vector) = (1,1,0,0)
    11.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    12.         _CleanTexture("Clean Texture", Range(0,1)) = 0.0
    13.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    14.         _Metallic ("Metallic", Range(0,1)) = 0.0
    15.     }
    16.     SubShader
    17.     {
    18.         Tags {"Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout"}
    19.         LOD 300
    20.  
    21.         CGPROGRAM
    22.         #pragma surface surf Standard fullforwardshadows alphatest:_Cutoff
    23.         #pragma shader_feature DEBUG_NOISE
    24.         #pragma multi_compile_instancing
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.         sampler2D _BumpMap;
    29.         sampler2D _NoiseTex;
    30.  
    31.         struct Input
    32.         {
    33.             float2 uv_MainTex;
    34.         };
    35.  
    36.         half _Glossiness;
    37.         half _Metallic;
    38.        
    39.         // #pragma instancing_options assumeuniformscaling
    40.         UNITY_INSTANCING_BUFFER_START(Props)
    41.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _TillingOffsetA)
    42.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _TillingOffsetN)
    43.             UNITY_DEFINE_INSTANCED_PROP(half, _CleanTexture)
    44.         UNITY_INSTANCING_BUFFER_END(Props)
    45.  
    46.         void surf (Input IN, inout SurfaceOutputStandard o)
    47.         {
    48.             fixed4 toA = UNITY_ACCESS_INSTANCED_PROP(Props, _TillingOffsetA);
    49.             fixed4 a = tex2D(_MainTex, toA.zw + IN.uv_MainTex * toA.xy);
    50.             fixed3 b = UnpackNormal(tex2D(_MainTex, toA.zw + IN.uv_MainTex * toA.xy));
    51.             fixed4 toN = UNITY_ACCESS_INSTANCED_PROP(Props, _TillingOffsetN);
    52.             fixed3 n = tex2D (_NoiseTex, toN.zw + IN.uv_MainTex * toN.xy);
    53.             o.Albedo = a.rgb;
    54. #ifdef DEBUG_NOISE
    55.             o.Albedo = n.rgb;
    56. #endif
    57.             o.Alpha = a.a * n.r *  UNITY_ACCESS_INSTANCED_PROP(Props, _CleanTexture);
    58.             o.Metallic = _Metallic;
    59.             o.Smoothness = _Glossiness;
    60.         }
    61.         ENDCG
    62.     }
    63.     FallBack "Diffuse"
    64. }
    65.  
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Two questions:
    1. Is the "enable GPU instancing" check box set on the material? If not, then it won't be using instancing to draw.
    2. How are you setting the properties? If you're directly modifying the material then it will change it for all of them. You have to use a
      MaterialPropertyBlock
      on the renderer component or pre-set with an array of values if using
      DrawMeshInstanced()
      calls.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    aha ok, i forgot about the values only working when set through code... :( kind of a bummer.