Introduction To Replacement Shaders & Shader Keywords PDF
Introduction To Replacement Shaders & Shader Keywords PDF
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 1/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
similar tag.
If you keep the replacementTag as "" (empty) then the shader
same properties and all the variations are de ned in one shader le.
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 2/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
Shader "BitshiftProgrammer/ExampleReplacement"
{ Programmer : Game Development, VFX & C# Blog
Bitshift
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature YELLOW_COL // Shader keyword
#pragma shader_feature RED_COL // Shader keyword
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
directives.
Shader.EnableKeyword(string keyword)
Bitshift Programmer : Game Development, VFX & C# Blog
Global state set to 'enabled' for that keyword and any shader that uses
Global state set to 'disabled' for that keyword and any shader that
Shader.EnableKeyword("RED_COL");
But you will still see red because the YELLOW_COL keyword was
not disabled.
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 4/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
As you can see the ground and the cubes are not rendered as the tag
Now we will actually implement this with the help of 2 shaders and 1
C# script.
We need 2 shaders :
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 5/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
1)The shader that actually gets replaced when replacing with tag
Bitshift Programmer : Game Development, VFX & C# Blog
2)The shader that will be put in it's place
"BitshiftProgrammer/ShaderToBeReplacedWithTag"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Cull off
Tags { "RenderType"="Opaque" "MyTag"="OnlyColors" } // Tagged with "MyTag"
CGPROGRAM
#pragma surface surf Lambert noforwardadd
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
So as you can see it's a simple lit surface shader but with an extra tag
called "MyTag".
Now the shader that will replace that one.
Shader "BitshiftProgrammer/Replacer"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 6/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
Pass
Bitshift{ Programmer : Game Development, VFX & C# Blog
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature UV_VIS
#pragma shader_feature LOCAL_POS_VIS
#pragma shader_feature WORLD_POS_VIS
#pragma shader_feature DEPTH_VIS
#pragma shader_feature LOCAL_NORMAL_VIS
#pragma shader_feature WORLD_NORMAL_VIS
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float3 localPos : TEXCOORD2;
float depth : TEXCOORD3;
float3 normal : TEXCOORD4;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
#if DEPTH_VIS
return i.depth;
Bitshift Programmer
#endif : Game Development, VFX & C# Blog
#if LOCAL_NORMAL_VIS || WORLD_NORMAL_VIS
return fixed4(normalize(i.normal) * 0.5 + 0.5,1.0);
#endif
return tex2D(_MainTex, i.uv); //If no shader feature is enabled
}
ENDCG
}
}
SubShader
{
Tags { "RenderType"="Opaque" "MyTag"="OnlyColors" } //Subshader with same tag
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature UV_VIS
#pragma shader_feature LOCAL_POS_VIS
#pragma shader_feature WORLD_POS_VIS
#pragma shader_feature DEPTH_VIS
#pragma shader_feature LOCAL_NORMAL_VIS
#pragma shader_feature WORLD_NORMAL_VIS
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
You can see the same shader keywords have been used in both
subshaders but di erent outputs have been described so that we know
what is happening.
Now the C# script that will allow us control how everything is being
rendered.
using UnityEngine;
public class ReplacementShaderTest : MonoBehaviour
{
public Shader replaceShader;
private string[] KeyWords = new string[] { "UV_VIS", "LOCAL_POS_VIS", "WOR
by leaving a like on
Bitshift Programmer Facebook Page and be updated as soon as there
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 10/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 12/12
10/13/2020 Introduction To Replacement Shaders & Shader Keywords
Popular Posts
Bitshift Programmer : Game Development, VFX & C# Blog
How The Tri-Planar Terrain Shader Looks Like Apart from the manually
drawn tile footpath the cli walls and grass has been generated through a
tri-planar shader. How The Tri-Planar Terrain Shader Works The shader
that we make will work with the existing terrain system and no need for …
Traversal
Catsoft Stu...
★★★★★
POLYGON
Powered by Blogger
https://www.bitshiftprogrammer.com/2018/12/introduction-to-replacement-shaders-and-shader-keywords.html 11/12