Upgrade custom shaders for URP compatibility
Custom Shaders written for the Built-In Render Pipeline 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:
- Example Built-In Render Pipeline custom shader
- Make the custom shader URP compatible
- Enable tiling and offset for the shader
- Complete shader code
Example Built-In Render Pipeline custom shader
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
}
}
}
Make the custom shader URP compatible
Built-In Render Pipeline shaders have two issues, which you can see in the Inspector window:
- A warning that Material property is found in another cbuffer.
- The SRP Batcher property displays not compatible.
The following steps show how to solve these issues and make a shader compatible with URP and the SRP Batcher.
Change
CGPROGRAMandENDCGtoHLSLPROGRAMandENDHLSL.Update the include statement to reference the
Core.hlslfile.#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"Note:
Core.hlslincludes 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 v2fcode block with the followingstruct Varyingscode block. This changes the struct to use the URP name ofVaryingsinstead ofv2f, 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
Varyingsstruct, define a new struct with the nameAttributes. This is equivalent to the Built-In Render Pipeline'sappdatastruct.Add the variables shown below to the
Attributesstruct.struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; };Update the
v2f vertfunction definition to use the newVaryingsstruct and take an instance of theAttributesstruct as an input, as shown below.Varyings vert(Attributes IN)Update the vert function to output an instance of the
Varyingsstruct and use theTransformObjectToHClipfunction to convert from object space to clip space. The function also needs to take the inputAttributesUV and pass it to the outputVaryingsUV.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.
OSmeans object space, andHCSmeans homogeneous clip space.Place a
CBUFFERcode block around the properties the shader uses, along with theUnityPerMaterialparameter.CBUFFER_START(UnityPerMaterial) float4 _Color; sampler2D _MainTex; CBUFFER_ENDNote: For a shader to be SRP Batcher compatible, you must declare all material properties within a
CBUFFERcode block. Even if a shader has multiple passes, all passes must use the sameCBUFFERblock.Update the
fragfunction to use theVaryingsinput and the typehalf4, as shown below. Thefragfunction 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:
- The warning that Material property is found in another cbuffer no longer appears.
- The SRP Batcher property displays compatible.
Enable tiling and offset for the shader
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
_MainTexto_BaseMapalong with any references to this property.Remove the
[NoScaleOffset]ShaderLab attribute from the_BaseMapproperty. You can now see Tiling and Offset properties in the shader's Inspector window.Add the
[MainTexture]ShaderLab attribute to the_BaseMapproperty and the[MainColor]attribute to the_Colorproperty. 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. ThePropertiessection 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)andSAMPLER(sampler_BaseMap)macros above theCBUFFERblock. 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 _BaseMapvariable inside theCBUFFERblock tofloat4 _BaseMap_ST. This variable now stores the tiling and offset values set in the Inspector.CBUFFER_START(UnityPerMaterial) float4 _Color; float4 _BaseMap_ST; CBUFFER_ENDChange the
fragfunction to access the texture with a macro instead oftex2Ddirectly. To do this, replacetex2Dwith theSAMPLE_TEXTURE2Dmacro and addsampler_BaseMapas 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
vertfunction, changeOUT.uvto use a macro instead of passing the texture coordinates asIN.uvdirectly. To do this, replaceIN.uvwithTRANSFORM_TEX(IN.uv, _BaseMap). Yourvertfunction 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
vertfunction after theCBUFFERblock, as theTRANSFORM_TEXmacro uses the parameter with the_STsuffix.
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.
Complete shader code
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
}
}
}