Search Unity

GPU Instancing and different textures.

Discussion in 'General Graphics' started by Necronomicron, Nov 19, 2017.

  1. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    I've found this and made everything like there, but my FPS became even worse (1.5 times less).

    This is how I load textures:
    Code (CSharp):
    1.         Texture2D[] unitTextures = Resources.LoadAll<Texture2D>("Unit textures");
    2.        unitTextureArray = new Texture2DArray(unitTextures[0].width, unitTextures[0].height, unitTextures.Length, unitTextures[0].format, true);
    3.  
    4.         for (int i = 0; i < unitTextures.Length; i++)
    5.         {
    6.            typeIndices[(Elements)System.Enum.Parse(typeof(Elements), unitTextures[i].name.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)[0])] = i;
    7.             Graphics.CopyTexture(unitTextures[i], 0, unitTextureArray, i);
    8.         }
    9.  
    10.         unitMaterial.SetTexture("_Textures", unitTextureArray);
    I use single prefab to instantiate game object.

    This is how I change index:
    Code (CSharp):
    1.     void SetUnitTexture(MeshRenderer mr, Elements type)
    2.     {
    3.         MaterialPropertyBlock props = new MaterialPropertyBlock();
    4.         props.SetFloat("_TextureIndex", typeIndices[type]);
    5.         mr.SetPropertyBlock(props);
    6.     }
    I tried to create without mips, but FPS is the same yet objects become pixelated and ugly.

    I also created own simple surface shader (previously I used standard one):
    Code (CSharp):
    1. Shader "SeekSick6/Unit" {
    2.     Properties {
    3.         _Textures("Textures", 2DArray) = "" {}
    4.         _Glossiness("Smoothness", Range(0, 1)) = 1
    5.         _Metallic("Metallic", Range(0, 1)) = 0
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.      
    11.         CGPROGRAM
    12.         // Physically based Standard lighting model, and enable shadows on all light types
    13.         #pragma surface surf Standard fullforwardshadows
    14.  
    15.         // Use shader model 3.0 target, to get nicer looking lighting
    16.         #pragma target 3.5
    17.         #include "UnityCG.cginc"
    18.  
    19.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    20.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    21.         #pragma instancing_options assumeuniformscaling
    22.  
    23.         UNITY_DECLARE_TEX2DARRAY(_Textures);
    24.      
    25.         struct Input {
    26.             float2 uv_Textures;
    27.         };
    28.  
    29.         half _Glossiness;
    30.         half _Metallic;
    31.  
    32.         UNITY_INSTANCING_CBUFFER_START(Props)
    33.             UNITY_DEFINE_INSTANCED_PROP(float, _TextureIndex)
    34.         UNITY_INSTANCING_CBUFFER_END
    35.  
    36.         void surf (Input IN, inout SurfaceOutputStandard o)
    37.         {
    38.             fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_Textures, float3(IN.uv_Textures, UNITY_ACCESS_INSTANCED_PROP(_TextureIndex)));
    39.             o.Albedo = c.rgb;
    40.             o.Metallic = _Metallic;
    41.             o.Smoothness = _Glossiness;
    42.             o.Alpha = c.a;
    43.         }
    44.         ENDCG
    45.     }
    46.     FallBack "Diffuse"
    47. }
    Although I scale objects not proportionally, removing `#pragma instancing_options assumeuniformscaling` changes nothing.

    What am I doing wrong?
     
    almaione-sdn likes this.
  2. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108
    Why can't I edit my own thread?
     
  3. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108