Skip to content
On this page

Mesh Data

A shader never draws a “thing.” It draws triangles, and every triangle corner carries a small handful of numbers. Before you write a line of logic, you need to know what those numbers are, where they come from, and how the GPU carries them to your fragment stage.

// What the GPU hands you, one per vertex, before you write any logic
float4 position : POSITION;    // where the corner sits
float3 normal   : NORMAL;      // which way the surface faces
float4 tangent  : TANGENT;     // the U direction, for tangent space
float2 uv       : TEXCOORD0;   // where to sample a texture
float4 color    : COLOR;       // per-vertex color, or packed data

A mesh is a bag of attributes

Everything you have ever seen rendered in real time is a set of triangles. A triangle is three vertices, and a vertex is a small bundle of values the GPU is allowed to read. Position, normal, tangent, UV, and vertex color are the five you will meet on almost every mesh, and each one answers a different question.

Position is where the corner sits in space. It arrives in object space, the mesh’s own local frame centered on the asset, and that is the space you start from whenever you move a vertex. Most of the time you then carry it to world space before you are done.

Normal is the direction the surface faces at that point. It is the most important input to lighting, because every model in this library, from Lambertian to Blinn-Phong to Fresnel, starts by asking how aligned a normal is with a light. Normals can be per face, which keeps edges hard, or per vertex, which the rasterizer blends into smooth shading.

Tangent is a unit vector that runs along the U direction of the UV as it travels over the surface. It sounds abstract until you need a normal map, because the tangent, together with the normal and a derived bitangent, defines a small local frame on the surface. That frame is called tangent space, and it is exactly where a normal map lives.

UV coordinates are a two dimensional point, conventionally between zero and one, that maps a corner to a spot on a texture. Sample a texture at a corner’s UV and you know which texel belongs there. Most meshes carry two or more UV sets, which also makes UVs a handy place to hide extra per-vertex data.

Vertex color is an RGBA value per vertex, white by default. Artists paint it to tint a surface or to bake in authored data, and the shader decodes whatever convention the team wrote down. A painted red channel might mean stiffness; an alpha channel might mean influence. The color is a container, and the convention is the meaning.

Shader Story: Foundations - Mesh Data

Interpolation does the quiet work

Here is the part that is easy to miss. All five of these values are per vertex, but your fragment shader does not read them per vertex. The rasterizer interpolates them across the inside of each triangle, so by the time your fragment stage runs, every value is a weighted blend of the three corners of the triangle that pixel belongs to.

That single fact explains a lot of behavior. A sphere with per-vertex normals looks round even though it is built from flat triangles, because the interpolated normal rolls smoothly from facet to facet. A sharp corner looks soft when its two faces share one normal, and the value at the center of a large triangle differs from the value at its corners. All of it is the same mechanism. When a value that lives at a corner seems to smear across a surface, you are watching this interpolation.

The practical consequence is that you choose your per-vertex values to be what you want at the corners, because the GPU decides what happens in between. You cannot tell the rasterizer to use one corner’s value across the whole triangle. That gap, between the flat normal the mesh has and the bumpy normal you want, is the entire reason normal maps exist, and it is covered in Normal Maps.

These attributes arrive in object space, and the first thing most shaders do is carry position, normal, and tangent to world space. How that transform works lives in Coordinate Spaces and Matrices.


What the shader does

The shader colors each pixel by a single attribute, selected by an enum: Position shows the silhouette, Normal shows the orientation, Tangent shows the U direction, UV shows where the textures will land, and Color shows whatever the artist painted. The vertex stage moves the chosen attribute into a varying and the fragment stage outputs it as color, so the same mesh reads as five different pictures depending on the toggle.

Shader Story: Foundations: Mesh Data - schematic

Illustrative schematic: One mesh, five attributes: the shader shows one at a time.


One mesh, five attributes, one toggle. The vertex stage reads the attribute, moves it to the space the pass needs, and hands it to the fragment stage, where the rasterizer’s interpolation is visible as a smooth gradient across each triangle.

Shader code
Shader "DecompiledArt/Foundations/MeshData/MeshData"
{
    Properties
    {
        [Enum(Position,0,Normal,1,Tangent,2,UV,3,Color,4)] _Attribute("Attribute", Float) = 1
        [MainColor] _BaseColor("Base Color", Color) = (0.4, 0.7, 1.0, 1.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"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS   : NORMAL;
                float4 tangentOS  : TANGENT;
                float2 uv         : TEXCOORD0;
                float4 color      : COLOR;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                half3  value      : TEXCOORD0;   // whichever attribute we are inspecting
            };

            CBUFFER_START(UnityPerMaterial)
                half  _Attribute;
                half4 _BaseColor;
            CBUFFER_END

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

                half3 v = half3(1, 1, 1);   // default: silhouette
                if (_Attribute < 0.5)         v = half3(1, 1, 1);              // Position
                else if (_Attribute < 1.5)    v = IN.normalOS * 0.5h + 0.5h;   // Normal, -1..1 -> 0..1
                else if (_Attribute < 2.5)    v = IN.tangentOS.xyz * 0.5h + 0.5h; // Tangent
                else if (_Attribute < 3.5)    v = IN.uv;                        // UV, already 0..1
                else                          v = IN.color.rgb;                 // Vertex color

                OUT.value = v;
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                // By here the value is the interpolated blend of the three corners.
                return half4(saturate(IN.value), _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

The Render Pipeline • Normal Maps • Coordinate Spaces

Related functions: Dot • Normalize


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