Skip to content

Instantly share code, notes, and snippets.

@josephbk117
Created December 22, 2018 10:53
Show Gist options
  • Save josephbk117/b06ab08cd17cc51c79b9630d5c52a4ba to your computer and use it in GitHub Desktop.
Save josephbk117/b06ab08cd17cc51c79b9630d5c52a4ba to your computer and use it in GitHub Desktop.
Replacement Shaders & Shader Keywords
using UnityEngine;
public class ReplacementShaderTest : MonoBehaviour
{
public Shader replaceShader;
private string[] KeyWords = new string[] { "UV_VIS", "LOCAL_POS_VIS", "WORLD_POS_VIS", "DEPTH_VIS", "LOCAL_NORMAL_VIS", "WORLD_NORMAL_VIS" };
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
GetComponent<Camera>().SetReplacementShader(replaceShader, ""); // All objects will have shader applied on it
else if (Input.GetKeyDown(KeyCode.W))
GetComponent<Camera>().SetReplacementShader(replaceShader, "MyTag"); // Only objects which have shaders that have a sub-shader that has a tag called "MyTag"
else if (Input.GetKeyDown(KeyCode.S))
GetComponent<Camera>().ResetReplacementShader(); // Removes all applied replacement shaders
else if (Input.GetKeyDown(KeyCode.Z))
EnableKeyWord(0);
else if (Input.GetKeyDown(KeyCode.Alpha1))
EnableKeyWord(1);
else if (Input.GetKeyDown(KeyCode.Alpha2))
EnableKeyWord(2);
else if (Input.GetKeyDown(KeyCode.Alpha3))
EnableKeyWord(3);
else if (Input.GetKeyDown(KeyCode.Alpha4))
EnableKeyWord(4);
else if (Input.GetKeyDown(KeyCode.Alpha5))
EnableKeyWord(5);
else if (Input.anyKeyDown)
EnableKeyWord(-1); // Goto default functionality of sub-shader ( by disbaling all shader keywords )
}
private void EnableKeyWord(int index)
{
for (int i = 0; i < KeyWords.Length; i++)
{
if (i == index)
Shader.EnableKeyword(KeyWords[i]);
else
Shader.DisableKeyword(KeyWords[i]);
}
}
}
Shader "BitshiftProgrammer/Replacer"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
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;
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;
v2f vert (appdata v)
{
v2f o;
o.localPos = v.vertex;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.worldPos = normalize(o.worldPos);
o.depth = -mul(UNITY_MATRIX_MV, v.vertex).z * _ProjectionParams.w;
o.uv = v.uv;
o.normal = v.normal;
#if WORLD_NORMAL_VIS
o.normal = mul(unity_ObjectToWorld, o.normal);
#endif
return o;
}
fixed4 frag (v2f i) : SV_Target
{
#if UV_VIS
return fixed4(i.uv, 0, 1);
#endif
#if LOCAL_POS_VIS
return fixed4(i.localPos, 1.0);
#endif
#if WORLD_POS_VIS
return fixed4(i.worldPos, 1.0);
#endif
#if DEPTH_VIS
return i.depth;
#endif
#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" }
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;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag () : SV_Target
{
#if UV_VIS
return fixed4(1,0,0,1);
#endif
#if LOCAL_POS_VIS
return fixed4(0,1,0,1);
#endif
#if WORLD_POS_VIS
return fixed4(0,0,1,1);
#endif
#if DEPTH_VIS
return fixed4(1,1,0,1);
#endif
#if LOCAL_NORMAL_VIS
return fixed4(1,0,1,1);
#endif
#if WORLD_NORMAL_VIS
return fixed4(0,1,1,1);
#endif
return fixed4(1,1,1,1);
}
ENDCG
}
}
}
Shader "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;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Mobile/VertexLit"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment