Search Unity

Instance Properties Shader graph? im using a texture atlas

Discussion in 'Shaders' started by Unlimited_Energy, Mar 31, 2020.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Hi,

    I upgraded to Unity 2019 HDRp and Im trying to figure out how to make a shader using instance properties so that I can change the uv cords at runtime based on a selection int he game and maintain instancing still. I wrote a custom shader in teh legacy pipeline that does not work anymore. Im wondering how you do instance properties in shader graph now.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Shader Graph sub forum is over here -> https://forum.unity.com/forums/shader-graph.346/

    And currently the way you support instanced properties in Shader Graph is ... by not using Shader Graph because it doesn't support instanced properties. Instancing is supposedly on their list of things to look at to add, but I have no idea what kind of time frame, or even if, it'll happen.

    Instanced properties are sort of supported when used with the DOTS hybrid renderer, but that's a whole big thing you probably don't want to dive into just yet. I certainly haven't.
     
    Unlimited_Energy likes this.
  3. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Thank you. SO i have a question. I have 24 plants in my game that have 24 different textures all needing to be rendered in a small area so it was killing my performance before I wrote a shader that did instance properties. Has anything changed performance wise in the 2019.3 HDRP pipeline that I would not need to worry about this? Any new performance magic that will happen without my intervention when it comes to many different shaders and materials being rendered. The area where all these plants are is in a small area and they scale as they are doing this. I cannot change this, but would love to know if this is less of an issue in 2019.3 hdrp as compared to the legacy system when it came to "needing" to use instance properties ina situation like mine. Many textures, materials many scaling objects using potentially all of them at the same time, which is why I combined them into a atlas and used the instance properties since these objects transform change over a long period of time. Thanks for any help bgolus, I know you are one of the "Graphics/shader" gurus on here.

    Here was my original instance shader, its not working now in 2019.3 HDRP pipeline.
    Code (CSharp):
    1. Shader "Custom/LeafInstanced" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         //_MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _MainTex("Texture", 2D) = "white" {}
    8.         _TextureST("Texture ST", Vector) = (1,1,0,0)
    9.         _BumpMap("Bumpmap", 2D) = "bump" {}
    10.         _Cutoff("Base Alpha cutoff", Range(0,.9)) = .5
    11.     }
    12.     SubShader {
    13.         Tags {  "RenderType" = "Transparent" "Queue" = "Geometry+1" "IgnoreProjector" = "false" }
    14.         LOD 200
    15.  
    16.         CGPROGRAM
    17.         // Physically based Standard lighting model, and enable shadows on all light types
    18.         #pragma surface surf Standard  alphatest:_Cutoff //addshadow
    19.  
    20.         // Use shader model 3.0 target, to get nicer looking lighting
    21.         #pragma target 3.0
    22.  
    23.         sampler2D _MainTex;
    24.         sampler2D _BumpMap;
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.             //float2 texcoord;
    28.             float2 uv_BumpMap;
    29.         };
    30.  
    31.     /*    void vert(inout appdata_full v, out Input o)
    32.         {
    33.             o.texcoord = v.texcoord;
    34.         }*/
    35.  
    36.         half _Glossiness;
    37.         half _Metallic;
    38.         //fixed4 _Color;
    39.         //float4 _MainTex_ST;
    40.         //float4 _Splat0_ST, _Splat1_ST;
    41.  
    42.         // #pragma instancing_options assumeuniformscaling
    43.         UNITY_INSTANCING_BUFFER_START(Props)
    44.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
    45.             UNITY_DEFINE_INSTANCED_PROP(float4, _TextureST)
    46.         UNITY_INSTANCING_BUFFER_END(Props)
    47.  
    48.         void surf (Input IN, inout SurfaceOutputStandard o)
    49.         {
    50.             float4 texST = UNITY_ACCESS_INSTANCED_PROP(Props, _TextureST);
    51.             // Apply tiling and offset, and compute uv for cell specified.
    52.             float2 uv = IN.uv_MainTex * texST.xy + texST.zw;
    53.             //Normal map
    54.             o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    55.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    56.             //uv = uv * texST.xy + texST.zw;
    57.            
    58.             // Albedo comes from a texture tinted by color
    59.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
    60.             o.Albedo = c.rgb;
    61.             // Metallic and smoothness come from slider variables
    62.             o.Metallic = _Metallic;
    63.             o.Smoothness = _Glossiness;
    64.             o.Alpha = c.a;
    65.        
    66.         }
    67.         ENDCG
    68.     }
    69.     FallBack "Transparent/Cutout/Diffuse"
    70. }
    71.  
     
    Last edited: Mar 31, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Yes. The SRP Batcher for the SRPs handles multiple materials using the same shader much more efficiently. In many cases you get instancing-like performance without using instancing at all. It just needs to be checked on in the SRP asset settings.

    Yep. It's a dead shader if you want to use the HDRP. You have to use Shader Graph or write the shader code entirely by hand without the benefit of a text based shader generator (which is what Surface Shader code you have above actually is). Both SRPs use completely new lighting setups that are not compatible with any preexisting lit shader that work for the built in rendering paths.
     
  5. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    If you're not using a Mesh Renderer and If you are drawing instances via DrawMeshInstanced() you can use ShaderGraph for the properties set.

    Arrays are currently not supported in Shader Graph. But there's a hack using a Custom Function node.
    Check out this fantasic FAQ from Cyan:
    https://www.cyanilux.com/faq/#sg-gpu-instancing