On this page
Basic UVs
A UV is not a texture but an address: two numbers that say which texel belongs to this point on the mesh, and the mesh stores them per vertex, packed into a single four-component float.
float4 uv : TEXCOORD0; // .xy is UV set 0, .zw is UV set 1
The attribute is a float4, and that matters¶
When you declare a UV attribute in the vertex stage, you declare a float4, not a float2. The GPU gives you four numbers per vertex, and the first two, .xy, are the UV set the artist painted for the main texture. The last two, .zw, are a second UV set in the same slot, the one artists use for lightmaps, trims, or a detail texture.
float2 uv0 = IN.uv.xy; // main set
float2 uv1 = IN.uv.zw; // second set, same attribute
This packing is why one TEXCOORD slot can feed two independent textures without adding a vertex attribute. The same idea scales: a third set rides on TEXCOORD1, and so on. Each set is its own little map of the surface, and each one answers the question “where does this point live on that texture.”
The 0 to 1 convention¶
By convention, UVs run from zero to one, with the origin at the lower left of the texture. That means a correctly mapped mesh has every UV inside the unit square, and a sample at (0.5, 0.5) hits the middle of the texture regardless of how big the mesh is in the world. This is what makes textures resolution independent in the first place: the mesh stores fractions of a texture, not pixel counts.
Two consequences follow. First, UVs can sit exactly on the edges, and what happens there is controlled by the texture’s wrap mode, which is covered in Filtering & Mipmaps. Second, because the square is a fraction of a texture, you can deliberately go outside it. Push UVs past 1.0 with a Repeat wrap mode and the texture tiles, which is the entire basis of tiling and is the next chapter.
UVs are also a convenient place to hide data, for the same reason vertex color is. A second UV set can carry an authored mask, a per-vertex weight, or a packed parameter, because the pipeline already interpolates it across the triangle for free. The mesh data chapter covers that trick in more detail: Mesh Data.
What the shader does¶
The shader reads the mesh’s two UV sets. The enum isolates the first set (main UVs) or the second (.zw), so the same mesh shows two texture questions at once.
Illustrative schematic: Two UV sets on one mesh.
One mesh, two UV sets, one toggle. The vertex stage unpacks the float4 into the pair you asked for, and the fragment stage samples a texture with it.
Shader "DecompiledArt/Textures/Basics/BasicUVs/BasicUVs"
{
Properties
{
[Enum(UV0,0,UV1,1)] _UvSet("UV Set", Float) = 0
_Tex("Texture", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 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(_Tex); SAMPLER(sampler_Tex);
struct Attributes
{
float4 positionOS : POSITION;
float4 uv : TEXCOORD0; // .xy is set 0, .zw is set 1
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half _UvSet;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = (_UvSet < 0.5h) ? IN.uv.xy : IN.uv.zw;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_Tex, sampler_Tex, IN.uv);
return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Sampling & Tinting • Tiling & Offset • Mesh Data
❤️ 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.
Your support helps keep this library open and free for everyone.