forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen_space_curvature.shader
49 lines (45 loc) · 1.13 KB
/
screen_space_curvature.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//reference: http://madebyevan.com/shaders/curvature/
//It looks the best with high-poly geometry
Shader "Screen Space Curvature Shader"
{
Subshader
{
Tags { "RenderType"="Opaque" }
Pass
{
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 3.0
struct structure
{
float4 gl_Position : SV_POSITION;
float3 normal : NORMAL;
float3 vertex : TEXCOORD0;
};
structure vertex_shader (float4 vertex:POSITION, float3 normal:NORMAL)
{
structure vs;
vs.gl_Position = UnityObjectToClipPos (vertex);
vs.normal = normal;
vs.vertex = vertex;
return vs;
}
float4 pixel_shader (structure ps) : COLOR
{
float3 n = normalize(ps.normal);
float3 dx = ddx(n);
float3 dy = ddy(n);
float3 xneg = n - dx;
float3 xpos = n + dx;
float3 yneg = n - dy;
float3 ypos = n + dy;
float depth = length(ps.vertex);
float curvature = (cross(xneg,xpos).y-cross(yneg,ypos).x)*4.0/depth;
return (curvature+0.5);
}
ENDCG
}
}
}