Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smkplus/cee0f5147ee27e1793f9b0e3f0400497 to your computer and use it in GitHub Desktop.
Save smkplus/cee0f5147ee27e1793f9b0e3f0400497 to your computer and use it in GitHub Desktop.
GPU Instancing + Texture2DArray
// https://www.reddit.com/r/Unity3D/comments/6uueox/gpu_instancing_texture2darray/
Texture2D[] textures;
int textureWidth = 256
int textureHeight = 256
Texture2DArray textureArray = new Texture2DArray(textureWidth, textureHeight, textures.Length, TextureFormat.RGBA32, false);
for (int i = 0; i < textures.Length; i++)
{
Graphics.CopyTexture(textures[i], 0, 0, textureArray, i, 0); // i is the index of the texture
}
material.SetTexture("_Textures", textureArray);
Shader "Custom/Texture2DArraySurfaceShader"
{
Properties
{
_Textures("Textures", 2DArray) = "" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.5
#include "UnityCG.cginc"
UNITY_DECLARE_TEX2DARRAY(_Textures);
struct Input
{
fixed2 uv_Textures;
};
UNITY_INSTANCING_CBUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
UNITY_DEFINE_INSTANCED_PROP(float, _TextureIndex)
UNITY_INSTANCING_CBUFFER_END
void surf (Input IN, inout SurfaceOutputStandard o)
{
fixed4 c = UNITY_SAMPLE_TEX2DARRAY(_Textures, float3(IN.uv_Textures, UNITY_ACCESS_INSTANCED_PROP(_TextureIndex)) * UNITY_ACCESS_INSTANCED_PROP(_Color);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment