Skip to content

Instantly share code, notes, and snippets.

@bzgeb
Created April 26, 2020 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzgeb/bd6db50a9ca9b650d05727a8a8db1ac8 to your computer and use it in GitHub Desktop.
Save bzgeb/bd6db50a9ca9b650d05727a8a8db1ac8 to your computer and use it in GitHub Desktop.
ToonLighting Shader
Shader "ToonLighting"
{
Properties
{
_Color("Color", Color) = (0.5, 0.65, 1, 1)
_Diffuse("Diffuse %", Range(0, 1)) = 1
_DiffuseThreshold("Diffuse Threshold", Range(0, 1)) = 0.1
_DiffuseWidth("Diffuse Width", Range(0, 1)) = 0.01
[HDR] _SpecularColor("Specular Color", Color) = (0.9, 0.9, 0.9, 1)
_SpecularFactor ("Specular %", Range(0, 1)) = 0.1
_SpecularPower ("Specular Power", Float) = 100
_SpecularThreshold("Specular Threshold", Range(0, 1)) = 0.985
_SpecularWidth("Specular Width", Range(0, 1)) = 0.01
_AmbientFactor("Ambient %", Range(0, 1)) = 1
_AmbientColor("Ambient Color", Color) = (0, 0, 0, 0)
[HDR] _RimColor("Rim Color", Color) = (1, 1, 1, 1)
_RimAmount("Rim Amount", Range(0, 1)) = 0.716
_RimThreshold("Rim Threshold", Range(0, 1)) = 0.1
}
SubShader
{
Pass
{
Tags
{
"LightMode" = "ForwardBase"
"PassFlags" = "OnlyDirectional"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "ToonLighting.cginc"
ENDCG
}
/*
Pass
{
Tags
{
"LightMode" = "ForwardAdd"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "ToonRampLighting.cginc"
ENDCG
}
*/
UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
}
}
#if !defined(TOON_LIGHTING_INCLUDED)
#define TOON_LIGHTING_INCLUDED
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
uniform float4 _Color;
uniform float _Diffuse;
uniform float _DiffuseThreshold;
uniform float _DiffuseWidth;
uniform float4 _SpecularColor;
uniform float _SpecularFactor;
uniform float _SpecularPower;
uniform float _SpecularThreshold;
uniform float _SpecularWidth;
uniform float _AmbientFactor;
uniform float4 _AmbientColor;
uniform float4 _RimColor;
uniform float _RimAmount;
uniform float _RimThreshold;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float3 worldNormal: NORMAL;
float3 worldPos : TEXCOORD2;
SHADOW_COORDS(3)
};
float3 SmoothSpecularBlinnPhong(float3 normalDir, float3 lightDir, float3 worldSpaceViewDir, float3 specularColor, float specularFactor, float attenuation, float specularPower)
{
float3 halfwayDir = normalize(lightDir + worldSpaceViewDir);
return specularColor * specularFactor * attenuation * pow(max(0, smoothstep(_SpecularThreshold - _SpecularWidth, _SpecularThreshold + _SpecularWidth, dot(normalDir, halfwayDir))), specularPower);
}
float3 SmoothDiffuseLambert(float3 normalVal, float3 lightDir, float3 lightColor, float diffuseFactor, float attenuation)
{
return lightColor * diffuseFactor * attenuation * max(0, smoothstep(_DiffuseThreshold - _DiffuseWidth, _DiffuseThreshold + _DiffuseWidth, dot(normalVal, lightDir)));
}
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
TRANSFER_SHADOW(o)
return o;
}
float4 frag (v2f i) : SV_Target
{
float3 normal = normalize(i.worldNormal);
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
float3 worldSpaceViewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
float attenuation = SHADOW_ATTENUATION(i);
// Specular Contribution
float3 specularColor = SmoothSpecularBlinnPhong(normal, lightDir, worldSpaceViewDir, _SpecularColor, _SpecularFactor, attenuation, _SpecularPower);
// Diffuse Contribution
float3 lightColor = _LightColor0.xyz;
float3 diffuseColor = SmoothDiffuseLambert(normal, lightDir, lightColor, _Diffuse, attenuation);
float lightIntensity = attenuation * max(0, smoothstep(_DiffuseThreshold - _DiffuseWidth, _DiffuseThreshold + _DiffuseWidth, dot(normal, lightDir)));
//float3 ambientColor = _AmbientFactor * UNITY_LIGHTMODEL_AMBIENT * (1 - lightIntensity);
float3 ambientColor = _AmbientFactor * _AmbientColor * (1 - lightIntensity);
//float3 ambientColor = _AmbientFactor * tex2D(_ColorRamp, float2(lightIntensity, 0.5));
float4 rimDot = 1 - dot(worldSpaceViewDir, normal);
float rimIntensity = rimDot * pow(lightIntensity, _RimThreshold);
rimIntensity = smoothstep(_RimAmount - 0.01, _RimAmount + 0.01, rimIntensity);
float4 rim = rimIntensity * _RimColor;
return float4(_Color * diffuseColor + specularColor + ambientColor + rim, 1);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment