Version: 2019.4
ShaderLab: SubShader LOD value
Debugging DirectX 11/12 shaders with Visual Studio

Using texture arrays in shaders

Similar to regular 2D textures (Texture2D class, sampler2D in shaders), cube maps (Cubemap class, samplerCUBE in shaders), and 3D textures (Texture3D class, sampler3D in shaders), Unity also supports 2D texture arrays.

Since texture arrays do not work on all platforms, shaders need to use an appropriate compilation target or feature requirement to access them. The minimum shader model compilation target that supports texture arrays is 3.5, and the feature name is 2darray.

Utilice estas macros para declarar y muestrar arreglos de textura:

  • UNITY_DECLARE_TEX2DARRAY(name) declara una variable texture array sampler dentro del código HLSL.
  • UNITY_SAMPLE_TEX2DARRAY(name,uv) muestrea un arreglo de textura con un float3 UV; El componente z de la coordenada es un índice de elemento del arreglo.
  • UNITY_SAMPLE_TEX2DARRAY_LOD(name,uv,lod) muestrea un arreglo de textura con un nivel mipmap explícito.

Ejemplos

El siguiente ejemplo shader muestrea un arreglo de textura utilizando las posiciones de vértice del espacio de objetos como coordenadas:

Shader "Example/Sample2DArrayTexture"
{
    Properties
    {
        _MyArr ("Tex", 2DArray) = "" {}
        _SliceRange ("Slices", Range(0,16)) = 6
        _UVScale ("UVScale", Float) = 1.0
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // texture arrays are not available everywhere,
            // only compile shader on platforms where they are
            #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;
                o.uv.z = (vertex.z + 0.5) * _SliceRange;
                return o;
            }
            
            UNITY_DECLARE_TEX2DARRAY(_MyArr);

            half4 frag (v2f i) : SV_Target
            {
                return UNITY_SAMPLE_TEX2DARRAY(_MyArr, i.uv);
            }
            ENDCG
        }
    }
}

Mirar también


  • 2018–03–20 Page amended
  • Shader #pragma directives added in Unity 2018.1
ShaderLab: SubShader LOD value
Debugging DirectX 11/12 shaders with Visual Studio
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961