커스텀 셰이더에서 텍스처 배열을 샘플링하려면 다음 단계를 따르십시오.
텍스처 배열 머티리얼 프로퍼티를 생성하려면 2DArray 머티리얼 프로퍼티 선언을 추가합니다. 예시:
Properties
{
_MainTex ("Texture array", 2DArray) = "" {}
}
텍스처 배열을 셰이더 입력으로 설정하려면 UNITY_DECLARE_TEX2DARRAY 매크로를 추가합니다. 예시:
UNITY_DECLARE_TEX2DARRAY(_MainTex);
텍스처 배열의 슬라이스를 샘플링하려면 UNITY_SAMPLE_TEX2DARRAY 메서드를 사용합니다. 예를 들어 텍스처 배열의 슬라이스 1을 샘플링하는 방법은 다음과 같습니다.
half4 frag (v2f i) : SV_Target {
float4 color = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(0, 0, 1));
}
텍스처 배열 슬라이스의 밉맵 레벨을 샘플링하려면 UNITY_SAMPLE_TEX2DARRAY_LOD 메서드를 사용합니다. 예를 들어 밉맵 레벨 0에서 텍스처 배열의 슬라이스 1을 샘플링하는 방법은 다음과 같습니다.
half4 frag (v2f i) : SV_Target {
float4 color = UNITY_SAMPLE_TEX2DARRAY_LOD(_MainTex, float3(0, 0, 1), 0);
}
텍스처 배열을 지원하는 플랫폼에서만 커스텀 셰이더를 타게팅하려면 다음 중 하나를 사용하십시오.
#pragma target 3.5
#pragma require 2darray
다음 셰이더 예시는 오브젝트 공간 버텍스 위치를 좌표로 사용하여 텍스처 배열을 샘플링하고 텍스처 배열을 지원하는 GPU에서만 실행됩니다.
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
{
CGPROGRAM
#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);
}
ENDCG
}
}
}