Sample a 2D texture array in a shader
Sample a 2D texture array in ShaderLab and High-level Shader Language (HLSL).
Read time 2 minutesLast updated 23 days ago
To sample a texture array in a custom shader, follow these steps:
-
To create a texture array material property, add amaterial property declaration. For example:
2DArrayProperties{ _MainTex ("Texture array", 2DArray) = "" {}} -
To set the texture array as a shader input, add amacro. For example:
UNITY_DECLARE_TEX2DARRAYUNITY_DECLARE_TEX2DARRAY(_MainTex); -
To sample a slice of the texture array, use themethod. For example to sample slice 1 of the texture array:
UNITY_SAMPLE_TEX2DARRAYhalf4 frag (v2f i) : SV_Target { float4 color = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(0, 0, 1));} -
To sample a mipmap level of a slice of the texture array, use themethod. For example to sample slice 1 of the texture array at mipmap level 0:
UNITY_SAMPLE_TEX2DARRAY_LODhalf4 frag (v2f i) : SV_Target { float4 color = UNITY_SAMPLE_TEX2DARRAY_LOD(_MainTex, float3(0, 0, 1), 0);}
Target platforms that support 2D texture arrays
To target custom shaders only at platforms that support texture arrays, use either of the following:
- to target shader model 3.5 and higher.
#pragma target 3.5 - to target GPUs that support texture arrays.
#pragma require 2darray
Example
The following shader example samples a texture array using object space vertex positions as coordinates, and runs only on GPUs that support texture arrays.
Shader "Example/Sample2DArrayTexture"{ Properties { // Create material properties for the 2D texture array and the slices to sample _MyArr ("Tex", 2DArray) = "" {} _SliceRange ("Slices", Range(0,16)) = 6 _UVScale ("UVScale", Float) = 1.0 } SubShader { Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag // Compile the shader only on platforms that support texture arrays #pragma require 2darray #include "UnityCG.cginc" struct v2f { float3 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; float _SliceRange; float _UVScale; v2f vert (float4 vertex : POSITION) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, vertex); o.uv.xy = (vertex.xy + 0.5) * _UVScale; // Set the z coordinate to the slice indices o.uv.z = (vertex.z + 0.5) * _SliceRange; return o; } // Set the texture array as a shader input UNITY_DECLARE_TEX2DARRAY(_MyArr); half4 frag (v2f i) : SV_Target { // Sample the texture array return UNITY_SAMPLE_TEX2DARRAY(_MyArr, i.uv); } ENDHLSL } }}