Forked from unitycoder/Texture2DArraySurface.Shader
Created
February 11, 2020 14:52
-
-
Save smkplus/cee0f5147ee27e1793f9b0e3f0400497 to your computer and use it in GitHub Desktop.
GPU Instancing + Texture2DArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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