Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Created January 2, 2021 09:15
Show Gist options
  • Save shivaduke28/c277013306b5a0e5cca0b42352f1f1b5 to your computer and use it in GitHub Desktop.
Save shivaduke28/c277013306b5a0e5cca0b42352f1f1b5 to your computer and use it in GitHub Desktop.
lit shader with normal map
Shader "MyShader/SimpleLit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_NormalTex ("Normal", 2D) = "white" {}
_Light ("Light", Vector) = (1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 light_dir : TEXCOORD2;
};
sampler2D _MainTex;
sampler2D _NormalTex;
float4 _MainTex_ST;
float3 _Light;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float3 light = UnityWorldToObjectDir(_Light);
TANGENT_SPACE_ROTATION;
o.light_dir = mul(rotation, light);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
float3 normal = UnpackNormal(tex2D(_NormalTex, i.uv));
col *= saturate(dot(normal, i.light_dir));
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment