To sample a texture array in a custom shaderA program that runs on the GPU. More info
See in Glossary, follow these steps:
To create a texture array material property, add a 2DArray
material property declaration. For example:
Properties
{
_MainTex ("Texture array", 2DArray) = "" {}
}
To set the texture array as a shader input, add a UNITY_DECLARE_TEX2DARRAY
macro. For example:
UNITY_DECLARE_TEX2DARRAY(_MainTex);
To sample a slice of the texture array, use the UNITY_SAMPLE_TEX2DARRAY
method. For example to sample slice 1 of the texture array:
half4 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 the UNITY_SAMPLE_TEX2DARRAY_LOD
method. For example to sample slice 1 of the texture array at mipmap level 0:
half4 frag (v2f i) : SV_Target {
float4 color = UNITY_SAMPLE_TEX2DARRAY_LOD(_MainTex, float3(0, 0, 1), 0);
}
To target custom shaders only at platforms that support texture arrays, use either of the following:
#pragma target 3.5
to target shader model 3.5 and higher.#pragma require 2darray
to target GPUs that support texture arrays.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
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.