-
Notifications
You must be signed in to change notification settings - Fork 0
/
step3.glgv
172 lines (142 loc) · 3.55 KB
/
step3.glgv
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const int Steps = 1000;
const float Epsilon = 0.01; // Marching epsilon
const float T=0.5;
const float rA=1.0; // Minimum ray marching distance from origin
const float rB=50.0; // Maximum
// Transforms
vec3 rotateY(vec3 p, float a)
{
p.x = p.x*cos(a) + p.z*sin(a);
p.z = p.z*cos(a) - p.x*sin(a);
return p;
}
// Smooth falloff function
// r : small radius
// R : Large radius
float falloff( float r, float R )
{
float x = clamp(r/R,0.0,1.0);
float y = (1.0-x*x);
return y*y*y;
}
// Primitive functions
// Point skeleton
// p : point
// c : center of skeleton
// e : energy associated to skeleton
// R : large radius
float point(vec3 p, vec3 c, float e,float R)
{
return e*falloff(length(p-c),R);
}
// Blending
// a : field function of left sub-tree
// b : field function of right sub-tree
float Blend(float a,float b)
{
return a+b;
}
// Potential field of the object
// p : point
float object(vec3 p)
{
float v = Blend(point(p,vec3( -2.5, 0.0,0.0),1.0,4.5),
point(p,vec3( 2.5, 0.0,0.0),1.0,4.5));
return v-T;
}
// Calculate object normal
// p : point
vec3 ObjectNormal(in vec3 p )
{
float eps = 0.0001;
vec3 n;
float v = object(p);
n.x = object( vec3(p.x+eps, p.y, p.z) ) - v;
n.y = object( vec3(p.x, p.y+eps, p.z) ) - v;
n.z = object( vec3(p.x, p.y, p.z+eps) ) - v;
return normalize(n);
}
// Trace ray using ray marching
// o : ray origin
// u : ray direction
// h : hit
// s : Number of steps
float Trace(vec3 o, vec3 u, out bool h,out int s)
{
h = false;
// Don't start at the origin
// instead move a little bit forward
float t=rA;
for(int i=0; i<Steps; i++)
{
s=i;
vec3 p = o+t*u;
float v = object(p);
// Hit object (1)
if (v > 0.0)
{
s=i;
h = true;
break;
}
// Move along ray
t += max(Epsilon,-v/2.0);
// Escape marched far away
if (t>rB)
{
break;
}
}
return t;
}
// Background color
vec3 background(vec3 rd)
{
return mix(vec3(0.8, 0.8, 0.9), vec3(0.6, 0.9, 1.0), rd.y*1.0+0.25);
}
// Shading and lighting
// p : point,
// n : normal at point
vec3 Shade(vec3 p, vec3 n, int s)
{
// point light
const vec3 lightPos = vec3(5.0, 5.0, 5.0);
const vec3 lightColor = vec3(1.0, 1.0, 1.0);
vec3 l = normalize(lightPos - p);
// Not even Phong shading, use weighted cosine instead for smooth transitions
float diff = 0.5*(1.0+dot(n, l));
vec3 c = 0.5*vec3(0.5,0.5,0.5)+0.5*diff*lightColor;
float fog = 0.7*float(s)/(float(Steps-1));
c = (1.0-fog)*c+fog*vec3(1.0,1.0,1.0);
return c;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 pixel = (gl_FragCoord.xy / iResolution.xy)*2.0-1.0;
// compute ray origin and direction
float asp = iResolution.x / iResolution.y;
vec3 rd = vec3(asp*pixel.x, pixel.y, -4.0);
vec3 ro = vec3(0.0, 0.0, 15.0);
vec2 mouse = iMouse.xy / iResolution.xy;
float a=-mouse.x*iTime*0.25;
//rd.z = rd.z+2.0*mouse.y;
rd = normalize(rd);
ro = rotateY(ro, a);
rd = rotateY(rd, a);
// Trace ray
bool hit;
// Number of steps
int s;
float t = Trace(ro, rd, hit,s);
vec3 pos=ro+t*rd;
// Shade background
vec3 rgb = background(rd);
if (hit)
{
// Compute normal
vec3 n = ObjectNormal(pos);
// Shade object with light
rgb = Shade(pos, n, s);
}
fragColor=vec4(rgb, 1.0);
}