Custom ShadersA program that runs on the GPU. More info
See in Glossary written for the Built-In Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary are not compatible with the Universal Render Pipeline (URP), and you can’t upgrade them automatically with the Render Pipeline Converter. Instead, you must rewrite the incompatible sections of shader code to work with URP.
You can also recreate custom shaders in Shader Graph. For more information, refer to documentation on ShaderGraph.
Note: You can identify any materials in a scene that use custom shaders when you upgrade to URP as they turn magenta (bright pink) to indicate an error.
This guide demonstrates how to upgrade a custom unlit shader from Built-In Render Pipeline to be fully compatible with URP through the following sections:
The following shader is a simple unlit shader that works with the Built-In Render Pipeline. This guide demonstrates how to upgrade this shader to be compatible with URP.
Shader "Custom/UnlitShader" { Properties { [NoScaleOffset] _MainTex("Main Texture", 2D) = "white" {} _Color("Color", Color) = (1,1,1,1) } SubShader { Tags { "RenderType" = "Opaque" } Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 position : SV_POSITION; float2 uv: TEXCOORD0; }; float4 _Color; sampler2D _MainTex; v2f vert(appdata_base v) { v2f o; o.position = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } fixed4 frag(v2f i) : SV_Target { fixed4 texel = tex2D(_MainTex, i.uv); return texel * _Color; } ENDCG } } }
Built-In Render Pipeline shaders have two issues, which you can see 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 following steps show how to solve these issues and make a shader compatible with URP and the SRP Batcher.
Change CGPROGRAM
and ENDCG
to HLSLPROGRAM
and ENDHLSL
.
Update the include statement to reference the Core.hlsl
file.
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
Note:
Core.hlsl
includes the core SRP library, URP shader variables, and matrix defines and transformations, but it does not include lighting functions or default structs.
Add "RenderPipeline" = "UniversalPipeline"
to the shader tags.
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Note: URP does not support all ShaderLab tags. For more information on which tags URP supports, refer to URP ShaderLab Pass tags.
Replace the struct v2f
code block with the following struct Varyings
code block. This changes the struct to use the URP name of Varyings
instead of v2f
, and updates the shader to use the correct variables for URP.
struct Varyings { // The positions in this struct must have the SV_POSITION semantic. float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; };
Beneath the include statement and above the Varyings
struct, define a new struct with the name Attributes
. This is equivalent to the Built-In Render Pipeline’s appdata
struct.
Add the variables shown below to the Attributes
struct.
struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; };
Update the v2f vert
function definition to use the new Varyings
struct and take an instance of the Attributes
struct as an input, as shown below.
Varyings vert(Attributes IN)
Update the vert function to output an instance of the Varyings
struct and use the TransformObjectToHClip
function to convert from object space to clip space. The function also needs to take the input Attributes
UV and pass it to the output Varyings
UV.
Varyings vert(Attributes IN) { Varyings OUT; OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz); OUT.uv = IN.uv; return OUT; }
Note: URP shaders use suffixes to indicate the space.
OS
means object space, andHCS
means homogeneous clip space.
Place a CBUFFER
code block around the properties the shader uses, along with the UnityPerMaterial
parameter.
CBUFFER_START(UnityPerMaterial) float4 _Color; sampler2D _MainTex; CBUFFER_END
Note: For a shader to be SRP Batcher compatible, you must declare all material properties within a
CBUFFER
code block. Even if a shader has multiple passes, all passes must use the sameCBUFFER
block.
Update the frag
function to use the Varyings
input and the type half4
, as shown below. The frag
function must now use this type, as URP shaders do not support fixed types.
half4 frag(Varyings IN) : SV_Target { half4 texel = tex2D(_MainTex, IN.uv); return texel * _Color; }
This custom unlit shader is now compatible with the SRP Batcher and ready for use within URP. You can check this in the Inspector window:
Although the shader is now compatible with URP and the SRP Batcher, you can’t use use the Tiling and Offset properties without further changes. To add this functionality to the custom unlit shader, use the following steps.
Rename the property _MainTex
to _BaseMap
along with any references to this property.
Remove the [NoScaleOffset]
ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary attribute from the _BaseMap
property. You can now see Tiling and Offset properties in the shader’s Inspector window.
Add the [MainTexture]
ShaderLab attribute to the _BaseMap
property and the [MainColor]
attribute to the _Color
property. This tells the Editor which property to return when you request the main texture or main color from another part of your project or in the Editor. The Properties
section of your shader should now look as follows:
Properties { [MainTexture] _BaseMap("Main Texture", 2D) = "white" {} [MainColor] _Color("Color", Color) = (1,1,1,1) }
Add the TEXTURE2D(_BaseMap)
and SAMPLER(sampler_BaseMap)
macros above the CBUFFER
block. These macros define the texture and sampler state variables for use later. For more information on sampler states, refer to Using sampler states.
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
Change the sampler2D _BaseMap
variable inside the CBUFFER
block to float4 _BaseMap_ST
. This variable now stores the tiling and offset values set in the Inspector.
CBUFFER_START(UnityPerMaterial) float4 _Color; float4 _BaseMap_ST; CBUFFER_END
Change the frag
function to access the texture with a macro instead of tex2D
directly. To do this, replace tex2D
with the SAMPLE_TEXTURE2D
macro and add sampler_BaseMap
as an additional parameter, as shown below:
half4 frag(Varyings IN) : SV_Target { half4 texel = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv); return texel * _Color; }
In the vert
function, change OUT.uv
to use a macro instead of passing the texture coordinates as IN.uv
directly. To do this, replace IN.uv
with TRANSFORM_TEX(IN.uv, _BaseMap)
. Your vert
function should now look like the following example:
Varyings vert(Attributes IN) { Varyings OUT; OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz); OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap); return OUT; }
Note: It’s important that you define the
vert
function after theCBUFFER
block, as theTRANSFORM_TEX
macro uses the parameter with the_ST
suffix.
This shader now has a texture, modified by a color, and is fully SRP Batcher compatible. It also fully supports the Tiling and Offset properties.
To see an example of the complete shader code, refer to the Complete shader code section of this page.
Shader "Custom/UnlitShader" { Properties { _BaseMap("Base Map", 2D) = "white" {} _Color("Color", Color) = (1,1,1,1) } 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; float2 uv: TEXCOORD0; }; struct Varyings { float4 positionCS : SV_POSITION; float2 uv: TEXCOORD0; }; TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap); CBUFFER_START(UnityPerMaterial) float4 _Color; float4 _BaseMap_ST; CBUFFER_END Varyings vert(Attributes IN) { Varyings OUT; OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz); OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap); return OUT; } half4 frag(Varyings IN) : SV_Target { float4 texel = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv); return texel * _Color; } ENDHLSL } } }
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.