Skip to content

Instantly share code, notes, and snippets.

@matthewholliday
Last active June 5, 2022 23:29
Show Gist options
  • Save matthewholliday/8b961b97fe78d9bb05178842c2a96a5b to your computer and use it in GitHub Desktop.
Save matthewholliday/8b961b97fe78d9bb05178842c2a96a5b to your computer and use it in GitHub Desktop.
Unity Normal Shader Template
Shader "MatthewShaders/NormalShader"
{
Properties
{
//Setting the default color to be green:
_MainTint ("Diffuse Tint", Color) = (0,1,0,1)
//"bump map" is a synonym for normal map- we are telling Unity that this will contain normal data:
_NormalTex ("Normal Map", 2D) = "bump" {}
//Intensity of the normal map:
_NormalMapIntensity ("Normal Intensity", Range(0,3)) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _NormalTex;
float4 _MainTint;
struct Input
{
float2 uv_NormalTex;
};
float _NormalMapIntensity;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
//Use the tint provided as the base color for the material:
o.Albedo = _MainTint;
//Get the normal data out of the normal map texture using:
float3 normalMap = UnpackNormal(
tex2D(
_NormalTex,
IN.uv_NormalTex
)
);
//Apply intensity:
normalMap.x *= _NormalMapIntensity;
normalMap.y *= _NormalMapIntensity;
//Apply the new normal to the lighting model
o.Normal =
normalize(
normalMap.rgb
);
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment