셰이더에서 2D 텍스처 배열 샘플링
Sample a 2D texture array in ShaderLab and High-level Shader Language (HLSL).
읽는 시간 2분최근 업데이트: 한 달 전
커스텀 셰이더에서 텍스처 배열을 샘플링하려면 다음 단계를 따르십시오.
-
텍스처 배열 머티리얼 프로퍼티를 생성하려면머티리얼 프로퍼티 선언을 추가합니다. 예시:
2DArrayProperties{ _MainTex ("Texture array", 2DArray) = "" {}} -
텍스처 배열을 셰이더 입력으로 설정하려면매크로를 추가합니다. 예시:
UNITY_DECLARE_TEX2DARRAYUNITY_DECLARE_TEX2DARRAY(_MainTex); -
텍스처 배열의 슬라이스를 샘플링하려면메서드를 사용합니다. 예를 들어 텍스처 배열의 슬라이스 1을 샘플링하는 방법은 다음과 같습니다.
UNITY_SAMPLE_TEX2DARRAYhalf4 frag (v2f i) : SV_Target { float4 color = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(0, 0, 1));} -
텍스처 배열 슬라이스의 밉맵 레벨을 샘플링하려면메서드를 사용합니다. 예를 들어 밉맵 레벨 0에서 텍스처 배열의 슬라이스 1을 샘플링하는 방법은 다음과 같습니다.
UNITY_SAMPLE_TEX2DARRAY_LODhalf4 frag (v2f i) : SV_Target { float4 color = UNITY_SAMPLE_TEX2DARRAY_LOD(_MainTex, float3(0, 0, 1), 0);}
2D 텍스처 배열을 지원하는 타겟 플랫폼
텍스처 배열을 지원하는 플랫폼에서만 커스텀 셰이더를 타게팅하려면 다음 중 하나를 사용하십시오.
- 셰이더 모델 3.5 이상을 타게팅하는
#pragma target 3.5 - 텍스처 배열을 지원하는 GPU를 타게팅하는
#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 } }}