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
CGPROGRAM
andENDCG
toHLSLPROGRAM
andENDHLSL
.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 followingstruct Varyings
code block. This changes the struct to use the URP naming convention ofVaryings
instead 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
Varyings
struct, define a new struct with the nameAttributes
. This is equivalent to the Built-In Render Pipeline's appdata structs but with the new URP naming conventions.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 newVaryings
struct and take an instance of theAttributes
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 theTransformObjectToHClip
function to convert from object space to clip space. The function also needs to take the inputAttributes
UV and pass it to the outputVaryings
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 theUnityPerMaterial
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 theVaryings
input and the typehalf4
, as shown below. Thefrag
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:
- 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
_MainTex
to_BaseMap
along with any references to this property. This brings the shader code closer to standard URP shader conventions.Remove the
[NoScaleOffset]
ShaderLab 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. TheProperties
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)
andSAMPLER(sampler_BaseMap)
macros above theCBUFFER
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 theCBUFFER
block 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_END
Change the
frag
function to access the texture with a macro instead oftex2D
directly. To do this, replacetex2D
with theSAMPLE_TEXTURE2D
macro and addsampler_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, changeOUT.uv
to use a macro instead of passing the texture coordinates asIN.uv
directly. To do this, replaceIN.uv
withTRANSFORM_TEX(IN.uv, _BaseMap)
. Yourvert
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.
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
}
}
}