-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraymarching.frag
136 lines (104 loc) · 2.57 KB
/
raymarching.frag
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifdef GL_ES
precision mediump float;
#endif
#define MAX_STEPS 100
#define MAX_DIST 100.
#define SURF_DIST .01
uniform vec2 u_resolution;
uniform float u_time;
mat3 Rot(float a){
float s = sin(a), c = cos(a);
return mat3(c, -s, 0.,
s, c, 0.,
0., 0., 1.);
}
float sdSphere(vec3 p, vec4 s){
return length(p-s.xyz)-s.w;
}
float sdCapsule(vec3 p, vec3 a, vec3 b, float r){
vec3 ab = b-a;
vec3 ap = p-a;
float t = dot(ab,ap) / dot(ab,ab);
t = clamp(t, 0.,1.);
vec3 c = a + t*ab;
return length(p-c)-r;
}
float sdBox(vec3 p, vec3 b){
return length(max(abs(p)-b, 0.));
}
float sdTorus( vec3 p, vec2 t ){
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}
float round(float p){
return floor(p + .5);
}
vec3 repeatedY(vec3 p, float s, float l)
{
p.y = p.y - s*clamp(
round(p.y/s), -l, l);
return p;
}
float GetDist(vec3 p){
vec4 s = vec4(0,1,6,1.);
float sphereDist = sdSphere(p,s);
float planeDist = p.y;
float capsuleDist = sdCapsule(
p, vec3(0,1,4),
vec3(1,0.5,2), .2);
float boxDist = sdBox(
p-vec3(-2,.5,4),vec3(.5));
float torusDist = sdTorus(
repeatedY(
Rot(u_time)*(p - vec3(0, 2., 3)),
.5, 1.), vec2(.5,.1));
float d = min(sphereDist, planeDist);
d = min(d, capsuleDist);
d = min(d, boxDist);
d = min(d, torusDist);
return torusDist;
}
float RayMarch(vec3 ro, vec3 rd){
float d0 = 0.;
for(int i=0; i < MAX_STEPS; i++){
vec3 p = ro + rd*d0;
float dS = GetDist(p);
d0 += dS;
if(d0 > MAX_DIST || dS < SURF_DIST) break;
}
return d0;
}
vec3 GetNormal(vec3 p){
float d = GetDist(p);
vec2 e = vec2(.01,0);
vec3 n = d - vec3(
GetDist(p-e.xyy),
GetDist(p-e.yxy),
GetDist(p-e.yyx)
);
return normalize(n);
}
float GetLight(vec3 p){
vec3 lightPos = vec3(0,2,6);
lightPos.xz += 2.*vec2(sin(u_time), cos(u_time)) * 2.;
vec3 l = normalize(lightPos - p);
vec3 n = GetNormal(p);
float dif = clamp(dot(n, l), 0., 1.);
float d = RayMarch(p+n*SURF_DIST*2., l);
if(d < length(lightPos-p)) dif *= .1;
return dif;
}
void main(){
// Normalize uv
vec2 uv = (gl_FragCoord.xy * 2. - u_resolution.xy)
/u_resolution.y;
vec3 col = vec3(0.1294, 0.1333, 0.0588);
// Camera Pos and Dir
vec3 ro = vec3(0.,2.,1.);
vec3 rd = normalize(vec3(uv, 1));
float d = RayMarch(ro, rd);
vec3 p = ro + rd*d;
float dif = GetLight(p);
col += vec3(dif);
gl_FragColor = vec4(col, 1.);
}