The Unity shaderA program that runs on the GPU. More info
See in Glossary in this example draws a texture on the meshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
See in Glossary.
Use the Unity shader source file from section URP unlit shader with color input and make the following changes to the ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary code:
In the Properties block, replace the existing code with the _BaseMap
property definition.
Properties
{
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
}
When you declare a texture property in the Properties block, Unity adds the _BaseMap
property with the label Base Map to the Material, and adds the Tiling and the Offset controls.
When you declare a property with the [MainTexture]
attribute, Unity uses this property as the main texture of the Material.
Note: For compatibility reasons, the
_MainTex
property name is a reserved name. Unity uses a property with the name_MainTex
as the main texture even it does not have the[MainTexture]
attribute.
In struct Attributes
and struct Varyings
, add the uv
variable for the UV coordinates on the texture:
float2 uv : TEXCOORD0;
Define the texture as a 2D texture and specify a sampler for it. Add the following lines before the CBUFFER block:
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
The TEXTURE2D and the SAMPLER macros are defined in one of the files referenced in Core.hlsl
.
For tiling and offset to work, it’s necessary to declare the texture property with the _ST
suffix in the ‘CBUFFER’ block. The _ST
suffix is necessary because some macros (for example, TRANSFORM_TEX
) use it.
Note: To ensure that the Unity shader is SRP Batcher compatible, declare all Material properties inside a single
CBUFFER
block with the nameUnityPerMaterial
. For more information on the SRP Batcher, refer to the documentation on the Scriptable Render Pipeline (SRP) Batcher.
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
CBUFFER_END
To apply the tiling and offset transformation, add the following line in the vertex shaderA program that runs on each vertex of a 3D model when the model is being rendered. More info
See in Glossary:
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
The TRANSFORM_TEX
macro is defined in the Macros.hlsl
file. The #include
declaration contains a reference to that file.
In the fragment shader, use the SAMPLE_TEXTURE2D
macro to sample the texture:
half4 frag(Varyings IN) : SV_Target
{
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
return color;
}
Now you can select a texture in the Base Map field in the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary window. The shader draws that texture on the mesh.
Below is the complete ShaderLab code for this example.
// This shader draws a texture on the mesh.
Shader "Example/URPUnlitShaderTexture"
{
// The _BaseMap variable is visible in the Material's Inspector, as a field
// called Base Map.
Properties
{
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
// The uv variable contains the UV coordinate on the texture for the
// given vertex.
float2 uv : TEXCOORD0;
};
// This macro declares _BaseMap as a Texture2D object.
TEXTURE2D(_BaseMap);
// This macro declares the sampler for the _BaseMap texture.
SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
// The following line declares the _BaseMap_ST variable, so that you
// can use the _BaseMap variable in the fragment shader. The _ST
// suffix is necessary for the tiling and offset function to work.
float4 _BaseMap_ST;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
// The TRANSFORM_TEX macro performs the tiling and offset
// transformation.
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// The SAMPLE_TEXTURE2D marco samples the texture with the given
// sampler.
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
return color;
}
ENDHLSL
}
}
}
Section Visualizing normal vectors shows how to visualize normal vectors on the mesh.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.