Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created May 11, 2017 13:48
Show Gist options
  • Save onotchi/b582dc3a6256d5591d85eb1912026950 to your computer and use it in GitHub Desktop.
Save onotchi/b582dc3a6256d5591d85eb1912026950 to your computer and use it in GitHub Desktop.
ToonシェーダーでPhong Tessellation
Shader "Onoty3D/Toon/Phong Tessellation/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
_EdgeLength("Edge length", Range(2,50)) = 5
_Phong("Phong Strengh", Range(0,1)) = 0.5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Cull[_CullMode]
CGPROGRAM
#pragma surface surf ToonRamp vertex:dispNone tessellate:tessEdge tessphong:_Phong
#include "Tessellation.cginc"
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float2 texcoord2 : TEXCOORD2;
};
void dispNone(inout appdata v) { }
float _Phong;
float _EdgeLength;
float4 tessEdge(appdata v0, appdata v1, appdata v2)
{
return UnityEdgeLengthBasedTess(v0.vertex, v1.vertex, v2.vertex, _EdgeLength);
}
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/Phong Tessellation/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
_EdgeLength("Edge length", Range(2,50)) = 5
_Phong("Phong Strengh", Range(0,1)) = 0.5
_OutlineColor("Outline Color", Color) = (0,0,0,1)
_Outline("Outline width", Range(.0, 0.03)) = .005
}
SubShader{
UsePass "Onoty3D/Toon/Phong Tessellation/Lit/FORWARD"
//Tags{ "RenderType" = "Opaque" }
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
CGINCLUDE
ENDCG
Cull Front
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf NoLighting alpha vertex:vert tessellate:tessEdge tessphong:_Phong
#include "UnityCG.cginc"
#include "Tessellation.cginc"
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float2 texcoord2 : TEXCOORD2;
};
struct Input {
float2 uv_MainTex : TEXCOORD0;
};
sampler2D _MainTex;
uniform float _Outline;
uniform float4 _OutlineColor;
void vert(inout appdata v) {
v.vertex.xyz += v.normal * _Outline;
}
half4 LightingNoLighting(SurfaceOutput s, half3 lightDir, half atten) {
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
void surf(Input IN, inout SurfaceOutput o) {
o.Albedo = _OutlineColor;
o.Alpha = _OutlineColor.a;
}
float _Phong;
float _EdgeLength;
float4 tessEdge(appdata v0, appdata v1, appdata v2)
{
return UnityEdgeLengthBasedTess(v0.vertex, v1.vertex, v2.vertex, _EdgeLength);
}
ENDCG
}
Fallback "Onoty3D/Toon/Phong Tessellation/Lit"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment