Skip to content
On this page

Color Data & Sampling

A texel is not a color. It is four numbers between zero and one that someone decided to call a color. Understanding that decision, especially the difference between how a color is stored and how it is interpreted, is the difference between textures that look right and textures that look washed out.

// What a sample returns, and how the hardware reads it:
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
// tex.r, tex.g, tex.b, tex.a are all floats in 0..1

A texel is a little bundle of channels

Pick any texture and zoom in, and you are looking at a grid of texels. Each texel stores a fixed set of channels, and the format is decided when the texture is imported, not when you sample it. The most common format is 8 bits per channel: one byte each for red, green, blue, and alpha, giving 256 distinct levels per channel and 16 million colors. A 1024 by 1024 texture in that format takes 4 megabytes of GPU memory, which is why texture size is one of the first things to check when a scene gets heavy.

Other formats trade channels or precision for space. A grayscale mask is one 8 bit channel. A normal map is three channels of 8 or 16 bits. A height or mask texture is often a single channel. The rule to keep: a sample returns one float per channel, always in the range zero to one, and the channel count is a property of the texture, not of your code.

Shader Story: Textures - Color Data

Stored versus interpreted: the gamma question

Here is the subtlety that trips people up. The bytes in a texture file are just byte values. The question is how you turn those values into light. Human vision and image formats like JPEG and PNG store colors in a gamma (or sRGB) space, where the values are perceptually spaced: they spend more levels where the eye is sensitive, in the darks. Light itself, though, adds and mixes linearly.

That is why render pipelines mark color textures as sRGB on import. The hardware decodes them back to linear as part of the sample, so when you add a texture color to a light contribution, the math is physically correct. Data textures, normal maps, masks, height maps, are the opposite: they are numbers, not colors, and must stay linear, or their geometry gets subtly warped. One import setting controls both, and getting it wrong is the classic cause of textures that look too bright, too contrasty, or too soft.

The takeaway is simple: ask yourself for every texture whether it is color or data, because color textures get sRGB on and data textures get it off, and everything else in this category builds on that habit.


What the shader does

The shader samples a color texture and shows its four channels one at a time. A toggle switches the import interpretation between sRGB and linear, and the difference shows up exactly where you would expect: the mids and shadows shift, the highlights barely move.

Shader Story: Textures: Color Data & Sampling - schematic

Illustrative schematic: One color texture, the four channels.


One texture, four views. The enum isolates a single channel of the sample into the output, and a second toggle picks the decode so you can compare the two interpretations on the same surface.

Shader code
Shader "DecompiledArt/Textures/Basics/ColorData/ColorData"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)

        [Enum(Red,0,Green,1,Blue,2,Alpha,3,RGB,4)] _Channel("Channel", Float) = 4
        [Enum(Linear,0,sRGB,1)] _Decode("Decode", Float) = 1
    }

    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(_MainTex);   SAMPLER(sampler_MainTex);

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

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

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _Channel;
                half  _Decode;
            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
            {
                half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);

                // A texture imported as linear is decoded manually to compare.
                if (_Decode < 0.5h)
                {
                    tex.rgb = pow(tex.rgb, half3(2.2, 2.2, 2.2));
                }

                half v = tex.rgb;   // default: show everything
                if (_Channel < 0.5h)      v = tex.r;
                else if (_Channel < 1.5h) v = tex.g;
                else if (_Channel < 2.5h) v = tex.b;
                else if (_Channel < 3.5h) v = tex.a;

                return half4(_BaseColor.rgb * v, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Sampling & Tinting • Normal Map Sampling

Related functions: Lerp • Remap


❤️ 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.