Skip to content

Instantly share code, notes, and snippets.

@onotchi
Created May 11, 2017 13:52
Show Gist options
  • Save onotchi/2f9d6d00dbc964a24b07258eb1e81cf0 to your computer and use it in GitHub Desktop.
Save onotchi/2f9d6d00dbc964a24b07258eb1e81cf0 to your computer and use it in GitHub Desktop.
ゾワゾワするシェーダー
Shader "Onoty3D/Toon/Lit ZowaZowa" {
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
_YBorder("YBorder", float) = 0
_YRange("YRange", float) = 0.005
_Volume("Volume", float) = 0.001
_Pow("Pow", Range(0, 3)) = 2
_Gap("Gap", Range(1, 3)) = 2
_Fineness("Fineness", float) = 12
_Edge("Edge", Range(0, 1)) = 0.1
_Tess("Tessellation", Range(1, 32)) = 4
}
SubShader{
Tags{ "RenderType" = "Opaque" }
LOD 200
Cull[_CullMode]
CGPROGRAM
#pragma surface surf ToonRamp vertex:vert tessellate:tessFixed
#pragma target 5.0
sampler2D _Ramp;
#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;
}
float _Tess;
float4 tessFixed()
{
return _Tess;
}
struct Input {
float2 uv_MainTex : TEXCOORD0;
};
float _YBorder;
float _YRange;
float _Volume;
float _Gap;
float _Pow;
float _Fineness;
float _Edge;
void vert(inout appdata_full v) {
float4x4 modelMatrix = unity_ObjectToWorld;
float4x4 modelMatrixInverse = unity_WorldToObject;
float3 normalDirection = normalize(mul(v.normal, modelMatrixInverse)).xyz;
float3 viewDirection = normalize(_WorldSpaceCameraPos - mul(modelMatrix, v.vertex).xyz);
float dotValue = saturate(dot(normalDirection, viewDirection));
float fineness = _Fineness * 100;
if (dotValue < _Edge) {
if (abs(v.vertex.y - _YBorder) < _YRange) {
//ゾワゾワを表現する部分はとりあえずいろいろ試して個人的にいい感じになった数式で特に思想があるわけではありません。
//float3 value = v.normal * _Volume * pow(_Gap * (sin(v.vertex.y * _Fineness) + sin(v.vertex.x * _Fineness) + sin(v.vertex.z * _Fineness)), _Pow);
float3 value = v.normal * _Volume * pow(_Gap * (sin(v.normal.y * fineness) + sin(v.normal.x * fineness) + sin(v.normal.z * fineness)), _Pow);
v.vertex.x += value.x;
v.vertex.y += value.y;
v.vertex.z += value.z;
}
}
}
sampler2D _MainTex;
float4 _Color;
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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment