On this page
Sampling & Tinting
The first texture you ever use works in two lines: sample the texture at the UV, then multiply by a color. That multiply is called tinting, and it is the quiet workhorse of every material that has ever shipped.
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
half3 color = _BaseColor.rgb * tex.rgb;
A sample is a question with two parts¶
Sampling needs two things, and it is worth naming them. The texture is the data, the grid of texels. The sampler is the behavior: how the GPU picks a texel when your UV lands between pixels, and what it does when the UV lands outside the texture. In practice you always pass both together, and the pipeline generates the sampler from the texture’s import settings, so in most code they appear as a pair that moves together.
The sample itself returns the value at your UV as floats between zero and one, already interpolated between neighboring texels if the texture is set to linear filtering. You never write that interpolation, the hardware does it, but knowing that it happens is exactly what the Filtering & Mipmaps chapter is about.
Tinting: multiply, and why it is the right operation¶
A texture usually does not come pre colored for your material. It comes as a pattern, a scratch, a scanline, a rust stain, and the artist wants that pattern in the material’s color. So you multiply each channel of the sample by a base color. Where the texture is dark, the result stays dark. Where it is bright, the base color shows through at full strength. The texture shapes the color, and the color shapes the texture, in one multiply.
Multiplication is the right operation for a reason: it keeps zero at zero. A black texel stays black no matter what color you pick, which is why textures for tinting are designed with black as “nothing” and white as “full”. If you find yourself wanting a tint that can also add brightness, that is a different material decision, and you will express it with a lerp or an additive term, not by reaching for a different interpretation of the multiply.
The first texture shader¶
Put the two ideas together and you have the most common fragment stage in the world: a sample, a multiply, done. Every base color map, every detail map, every mask you will read in a material starts here, and the rest of this category is about what you do to the UV before the sample and what you do to the result after it.
One habit worth forming now: sample the texture once into a variable, then work with the variable. Sampling is the most expensive thing a fragment shader does, and a texture read you can reuse for color, mask, and normal in the same pass is a texture read you pay for once instead of three times.
What the shader does¶
The shader multiplies a texture by a base color: the texture provides the pattern and the color provides the hue. The single multiply does the blending, so the same texture re-tints itself with the color value.
Illustrative schematic: Texture pattern times a tint color.
The canonical first texture material. One sample, one multiply, and an alpha pass through from the base color.
Shader "DecompiledArt/Textures/Basics/SamplingTinting/SamplingTinting"
{
Properties
{
_MainTex("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(_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;
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);
half3 color = _BaseColor.rgb * tex.rgb;
return half4(color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Basic UVs • Color Data & Sampling • Tiling & Offset
❤️ 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.