Search Unity

how enable unity5.4 gpu instancing?

Discussion in 'Shaders' started by dreamerflyer, Oct 9, 2017.

  1. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    I do not know what is wrong.. 5.4 gpu.png
    Code (CSharp):
    1. Shader "Instanced/NewInstancedSurfaceShader" {
    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.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.        
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         // And generate the shadow pass with instancing support
    15.         #pragma surface surf Standard fullforwardshadows addshadow
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         // Enable instancing for this shader
    21.         #pragma multi_compile_instancing
    22.  
    23.         // Config maxcount. See manual page.
    24.         // #pragma instancing_options
    25.  
    26.         sampler2D _MainTex;
    27.  
    28.         struct Input {
    29.             float2 uv_MainTex;
    30.         };
    31.  
    32.         half _Glossiness;
    33.         half _Metallic;
    34.  
    35.         // Declare instanced properties inside a cbuffer.
    36.         // Each instanced property is an array of by default 500(D3D)/128(GL) elements. Since D3D and GL imposes a certain limitation
    37.         // of 64KB and 16KB respectively on the size of a cubffer, the default array size thus allows two matrix arrays in one cbuffer.
    38.         // Use maxcount option on #pragma instancing_options directive to specify array size other than default (divided by 4 when used
    39.         // for GL).
    40.         UNITY_INSTANCING_CBUFFER_START(Props)
    41.             UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)    // Make _Color an instanced property (i.e. an array)
    42.         UNITY_INSTANCING_CBUFFER_END
    43.  
    44.         void surf (Input IN, inout SurfaceOutputStandard o) {
    45.             // Albedo comes from a texture tinted by color
    46.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * UNITY_ACCESS_INSTANCED_PROP(_Color);
    47.             o.Albedo = c.rgb;
    48.             // Metallic and smoothness come from slider variables
    49.             o.Metallic = _Metallic;
    50.             o.Smoothness = _Glossiness;
    51.             o.Alpha = c.a;
    52.         }
    53.         ENDCG
    54.     }
    55.     FallBack "Diffuse"
    56. }
    57.  
    and C#:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ExampleClass : MonoBehaviour
    4. {
    5.     public Mesh mesh;
    6.     public Material material;
    7.     public Transform parent;
    8.     Transform[] all;
    9.     private void Awake()
    10.     {
    11.         all = parent.GetComponentsInChildren<Transform>();
    12.     }
    13.     public void Update()
    14.     {
    15.         // will make the mesh appear in the scene at origin position
    16.         for(int i=0;i< all.Length;i++)
    17.         {
    18.             Graphics.DrawMesh(mesh, all[i].position, all[i].rotation, material, 0);
    19.  
    20.         }
    21.     }
    22. }
    23.  
     
  2. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    any help?
     
  3. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
  4. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Don't mark the objects as static, because then they will be batched instead of hardware instanced.

    Furthermore, hardware instancing has been introduced in 5.4, 5.5 and 5.6, so I would recommend switching to 5.6 for proper hardware instancing that is future proof.

    For custom drawing using hardware instancing, look at DrawMeshInstanced. (Which was only introduced in 5.5 is not available in 5.4)
     
  5. dreamerflyer

    dreamerflyer

    Joined:
    Jun 11, 2011
    Posts:
    927
    ok, thanks~