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
|
#version 330 core
uniform vec2 resolution;
uniform float time;
out vec3 color;
#define MAX_STEPS 100
#define MAX_DIST 100.
#define SURF_DIST .01
#define NORMAL_ACCURACY 0.01
#include "objects/sphere.frag"
float GetDist(vec3 p) {
float sphereDist = SphereSDF(p,vec3(0, 1, 6),1);
float planeDist = p.y;
float d = min(sphereDist, planeDist);
return d;
}
#include "algorithms/raymarching.frag"
#include "algorithms/normal.frag"
float GetLight(vec3 p) {
vec3 lightPos = vec3(0, 5, 6);
lightPos.xz += vec2(sin(time), cos(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()
{
vec2 uv = (gl_FragCoord.xy-.5*resolution.xy)/resolution.y;
vec3 col = vec3(0);
vec3 ro = vec3(0, 1, 0);
vec3 rd = normalize(vec3(uv.x, uv.y, 1));
float d = RayMarch(ro, rd);
vec3 p = ro + rd * d;
float dif = GetLight(p);
col = vec3(dif);
col = pow(col, vec3(.4545)); // gamma correction
color = col;
}
|