Skip to content
On this page

Texture Arrays

A texture array is a stack of 2D textures that share one binding slot and one sampler, with a third coordinate that picks which slice you read. It is the modern answer to a very old problem: how do you let one material reach a whole family of textures without declaring a family of slots?

TEXTURE2D_ARRAY(_TexArray);   SAMPLER(sampler_TexArray);

half4 tex = SAMPLE_TEXTURE2D_ARRAY(_TexArray, sampler_TexArray, uv, sliceIndex);

The third coordinate

A normal 2D sample takes a float2 and returns a color. An array sample takes a float2 and a slice index and returns the color from that slice. Every slice in the array is the same size and shares the mip chain and the sampler, so the slices behave like a battery of identical textures that live side by side in memory.

// Slice 0 is the first texture, slice 1 the next, and so on.
// A float slice index blends between the two nearest slices.
float  slice = _Slice;                    // or a per-object value
half4 tex = SAMPLE_TEXTURE2D_ARRAY(_TexArray, sampler_TexArray, uv, slice);

That float detail matters. Because the index is a floating point value, an index of 1.5 samples halfway between slice 1 and slice 2 and the hardware blends them. You can animate a slice with a fraction and get a smooth crossfade between two textures for free, which is a neat shortcut for the Blending over Time chapter.

When an array beats many slots

Three situations come up constantly. Padding free atlases: an atlas crams many textures into one big image and wastes space on the gaps, because a sample near an edge can bleed into a neighbor. Slices in an array are isolated, so you can pack edge to edge with zero bleeding and zero padding. Material packs: a grass system with ten blendable textures, or a character with a set of detail maps, binds one array and selects per object, instead of ten slots that every material variant must declare. LOD and quality sets: several resolutions of the same content in one resource, selected by one value.

Shader Story: Textures - Texture Arrays

What the author needs to know

The authoring side is the part that is not visible in code. An array is a fixed set of same sized images, usually authored as a folder of textures or a single stacked file, depending on the platform’s import path. The slice order is the contract: your shader and your content pipeline must agree on which texture is slice 5. That is why the index is often passed from the object or a data table, not hard coded, so reordering the art does not require touching every shader.

The cost side is simple: an array is the memory of all its slices, because they are one allocation. Ten 512 by 512 slices cost the same as ten textures, plus the shared mip chain. You are not saving memory by using an array. You are saving bindings, slots, and branching, which is a different and often more valuable kind of saving.


What the shader does

The shader samples an array of textures by a slice coordinate. Whole numbers switch textures, and fractions between them crossfade, so both the discrete and the continuous side of the slice are visible in one pass.

Shader Story: Textures: Texture Arrays - schematic

Illustrative schematic: Slice 0 to 3: whole numbers switch, fractions crossfade.


One array, one slice value, one sample. The slider runs past 3 and wraps with frac so the demo loops smoothly.

Shader code
Shader "DecompiledArt/Textures/Basics/TextureArrays/TextureArrays"
{
    Properties
    {
        [NoScaleOffset] _TexArray("Texture Array (4 slices)", 2DArray) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _Slice("Slice", Range(0, 3.999)) = 0
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }

        Pass
        {
            Name "ForwardOnly"
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURE2D_ARRAY(_TexArray);   SAMPLER(sampler_TexArray);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv         : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                float _Slice;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = IN.uv;
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                float  slice = _Slice;
                half4 tex = SAMPLE_TEXTURE2D_ARRAY(_TexArray, sampler_TexArray, IN.uv, slice);
                return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

The property type 2DArray tells the import system the slot expects an array, and the [NoScaleOffset] flag keeps tiling and offset out of a coordinate that is meant to be exact.


Sampling & Tinting • Blending over Time • Cubemaps


❤️ Support Shader Story

If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.