forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
directional_derivative.shader
71 lines (63 loc) · 1.67 KB
/
directional_derivative.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Lighting is computed without normal vectors, for faster rendering.
//Reference: http://iquilezles.org/www/articles/derivative/derivative.htm
Shader "Directional derivative"
{
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 3.0
struct type
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
float sphere (float3 p, float3 c, float r)
{
return length(p-c)-r;
}
float map (float3 p)
{
return sphere(p,float3(0,0,0),1.0);
}
float4 lighting (float3 p, float e)
{
float4 a = float4 (0.1,0.1,0.1,1.0); //ambient light color
float4 b = float4(1.0,1.0,0.0,1.0); //directional light color
float3 l = normalize(float3(6,15,-7)); //directional light direction
float c = (map(p+l*e)-map(p))/e; //directional derivative equation
return saturate(c)*b+a; //return diffuse color
}
float4 raymarch (float3 ro, float3 rd)
{
for (int i=0; i<128; i++)
{
float t = map(ro);
if (t < 0.001) return lighting(ro,0.001);
ro+=t*rd;
}
return 0;
}
type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
{
type vs;
vs.vertex = UnityObjectToClipPos (vertex);
vs.uv = uv;
return vs;
}
float4 pixel_shader (type ps) : COLOR
{
float2 resolution = float2(1024,1024);
float2 fragCoord = ps.uv*resolution;
float2 uv = (2.0*fragCoord-resolution)/resolution.y;
float3 worldPosition = float3(0,0,-10);
float3 viewDirection = normalize(float3(uv,2.0));
return raymarch(worldPosition,viewDirection);
}
ENDCG
}
}
}