Skip to content

Instantly share code, notes, and snippets.

@josephbk117
Last active November 18, 2018 18:57
Show Gist options
  • Save josephbk117/53a43f9c9c362456524534f8b69587bd to your computer and use it in GitHub Desktop.
Save josephbk117/53a43f9c9c362456524534f8b69587bd to your computer and use it in GitHub Desktop.
unity shader that shows texture based on the object's normal
/*Please do support www.bitshiftprogrammer.com by joining the facebook page : fb.com/BitshiftProgrammer
Legal Stuff:
This code is free to use no restrictions but attribution would be appreciated.
Any damage caused either partly or completly due to usage of this stuff is not my responsibility*/
Shader "BitShiftProgrammer/DirectionalColour"
{
Properties
{
_Tex1 ("Texture 1", 2D) = "white" {}
_Tex2("Texture 2", 2D) = "white"{}
_Tex3("Texture 3", 2D) = "white"{}
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
fixed4 norm : COLOR;
float4 vertex : SV_POSITION;
};
sampler2D _Tex1;
sampler2D _Tex2;
sampler2D _Tex3;
float4 _Tex1_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.norm.xyz = v.normal;
o.norm.w = 1.0;
o.uv = TRANSFORM_TEX(v.uv, _Tex1);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col1 = tex2D(_Tex1, i.uv);
fixed4 col2 = tex2D(_Tex2, i.uv);
fixed4 col3 = tex2D(_Tex3, i.uv);
col1 *= abs(dot(i.norm.xyz, float3(0, 0, 1)));
col2 *= abs(dot(i.norm.xyz, float3(0, 1, 0)));
col3 *= abs(dot(i.norm.xyz, float3(1, 0, 0)));
return ((col1 +col2 + col3)/3) * 1.2;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment