Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created May 11, 2017 12:51
Show Gist options
  • Save onotchi/3417980f92e207b10962f2830974c7c6 to your computer and use it in GitHub Desktop.
Save onotchi/3417980f92e207b10962f2830974c7c6 to your computer and use it in GitHub Desktop.
白枠を表示させるシェーダー
Shader "Onoty3D/Toon/Lit" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
_StencilRefMain("Stcl Ref Main", Range(0, 255)) = 128
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Cull [_CullMode]
Stencil{
Ref [_StencilRefMain]
Comp Always
Pass Replace
}
CGPROGRAM
#pragma surface surf ToonRamp
sampler2D _Ramp;
// custom lighting function that uses a texture ramp based
// on angle between light direction and normal
#pragma lighting ToonRamp exclude_path:prepass
inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
{
#ifndef USING_DIRECTIONAL_LIGHT
lightDir = normalize(lightDir);
#endif
half d = dot (s.Normal, lightDir)*0.5 + 0.5;
half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
c.a = 0;
return c;
}
sampler2D _MainTex;
float4 _Color;
struct Input {
float2 uv_MainTex : TEXCOORD0;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Diffuse"
}
Shader "Onoty3D/Toon/Lit OuterFrame" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
_StencilRefMain("Stcl Ref Main", Range(0, 255)) = 128
_StencilRefOuterFrame("Stcl Ref Outer Frame", Range(0, 255)) = 112
_StencilRefShadow("Stcl Ref Outer Shadow", Range(0, 255)) = 96
_OuterFrameColor("Outer Frame Color", Color) = (1,1,1,1)
_OuterFrameWidth("Outer Frame Width", Range(0.0, 1.0)) = 0.02
_OuterShadowColor("Outer Shadow Color", Color) = (0.5,0.5,0.5,0.5)
_OuterShadowOffset("Outer Shadow Offset", Vector) = (-0.5,-0.5,0,0)
}
SubShader {
Tags { "RenderType"="Opaque" }
UsePass "Onoty3D/Toon/Lit/FORWARD"
//STENCIL OUTER FRAME"
Pass{
Name "OUTER FRAME"
Stencil{
Ref [_StencilRefOuterFrame]
Comp Greater
Pass Replace
}
Cull Front
ZWrite On
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
float _OuterFrameWidth;
float4 _OuterFrameColor;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _OuterFrameWidth;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
half4 frag(v2f i) : COLOR{
return _OuterFrameColor;
}
ENDCG
}
//STENCIL SHADOW
Pass{
Name "OUTER SHADOW"
Stencil{
Ref[_StencilRefShadow]
Comp Greater
Pass replace
}
Cull Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
float _OuterFrameWidth;
float4 _OuterShadowColor;
float _StencilScale;
float2 _OuterShadowOffset;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _OuterFrameWidth;
o.pos.x += _OuterShadowOffset.x;
o.pos.y += _OuterShadowOffset.y;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
half4 frag(v2f i) : COLOR{
return _OuterShadowColor;
}
ENDCG
}
}
Fallback "Onoty3D/Toon/Lit"
}
Shader "Onoty3D/Toon/Lit Outline" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
_StencilRefMain("Stcl Ref Main", Range(0, 255)) = 128
_OutlineColor("Outline Color", Color) = (0,0,0,1)
_OutlineWidth("Outline Width", Range(.001, 0.03)) = .005
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
UNITY_FOG_COORDS(0)
fixed4 color : COLOR;
};
uniform float _OutlineWidth;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
float3 norm = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, v.normal));
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _OutlineWidth;
o.color = _OutlineColor;
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
ENDCG
SubShader {
Tags { "RenderType"="Opaque" }
UsePass "Onoty3D/Toon/Lit/FORWARD"
Pass{
Name "OUTLINE"
Tags{ "LightMode" = "Always" }
Stencil{
Ref[_StencilRefMain]
Comp always
Pass replace
}
Cull Front
ZWrite On
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
fixed4 frag(v2f i) : SV_Target
{
UNITY_APPLY_FOG(i.fogCoord, i.color);
return i.color;
}
ENDCG
}
}
Fallback "Onoty3D/Toon/Lit"
}
Shader "Onoty3D/Toon/Lit Outline OuterFrame" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
[Enum(OFF,0,FRONT,1,BACK,2)] _CullMode("Cull Mode", int) = 2 //OFF/FRONT/BACK
_StencilRefMain("Stcl Ref Main", Range(0, 255)) = 128
_StencilRefOuterFrame("Stcl Ref Outer Frame", Range(0, 255)) = 112
_StencilRefShadow("Stcl Ref Outer Shadow", Range(0, 255)) = 96
_OuterFrameColor("Outer Frame Color", Color) = (1,1,1,1)
_OuterFrameWidth("Outer Frame Width", Range(0.0, 1.0)) = 0.02
_OuterShadowColor("Outer Shadow Color", Color) = (0.5,0.5,0.5,0.5)
_OuterShadowOffset("Outer Shadow Offset", Vector) = (-0.5,-0.5,0,0)
_OutlineColor("Outline Color", Color) = (0,0,0,1)
_OutlineWidth("Outline Width", Range(.001, 0.03)) = .005
}
SubShader {
Tags { "RenderType"="Opaque" }
UsePass "Onoty3D/Toon/Lit/FORWARD"
UsePass "Onoty3D/Toon/Lit Outline/OUTLINE"
UsePass "Onoty3D/Toon/Lit OuterFrame/OUTER FRAME"
UsePass "Onoty3D/Toon/Lit OuterFrame/OUTER SHADOW"
}
Fallback "Onoty3D/Toon/Lit"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment