-
Notifications
You must be signed in to change notification settings - Fork 0
/
step10.glsl
256 lines (216 loc) · 5.57 KB
/
step10.glsl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
vec2 hash( vec2 p )
{
p = vec2( dot(p,vec2(127.1,311.7)),
dot(p,vec2(269.5,183.3)) );
return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}
float noise( in vec2 p )
{
const float K1 = 0.366025404; // (sqrt(3)-1)/2;
const float K2 = 0.211324865; // (3-sqrt(3))/6;
vec2 i = floor( p + (p.x+p.y)*K1 );
vec2 a = p - i + (i.x+i.y)*K2;
vec2 o = step(a.yx,a.xy);
vec2 b = a - o + K2;
vec2 c = a - 1.0 + 2.0*K2;
vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
return dot( n, vec3(70.0) );
}
float ridged( in vec2 p)
{
return 2.0*(0.5 - abs(0.5- noise(p)));
}
float turbulence(in vec2 p, in float amplitude, in float fbase, in float attenuation, in int noctave) {
int i;
float res = .0;
float f = fbase;
for (i=0;i<noctave;i++) {
res = res+amplitude*ridged(f*p);
amplitude = amplitude*attenuation;
f = f*2.;
}
return res;
}
float terrain(vec3 p)
{
float y = turbulence(p.xz, 0.71, 0.2, 0.38, 8);
return y-p.y;
}
// 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));
float v = terrain(p);
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);
}
float colorBand2(float value, float colorSteps) {
float currentStep;
float stepSize = 0.00001/ colorSteps;
modf(value / stepSize, currentStep);
return currentStep * stepSize;
}
// Shading and lighting
// p : point,
// n : normal at point
float conv(float val)
{
float val2 = val + 0.45;
if(val2 == 0.0)
{
return 0.0;
}
else
{
return val2*iResolution.x/1.5;
}
}
vec3 Shade(vec3 p, vec3 n, int s, vec3 rd)
{
// point light
const vec3 Color = vec3(1.0, 1.0, 1.0);
vec3 l = normalize(rd - p);
float turbTerre = turbulence(p.zy, 0.2, 0.7, 0.2, 80);
float turbEau = turbulence(vec2(p.z,-5.0), 0.1, 0.1*iTime, 0.2, 8);
// Not even Phong shading, use weighted cosine instead for smooth transitions
float diff = 0.5*(1.0+dot(n, l));
vec3 c;
float py = conv(p.y);
if( mod(p.y,0.07)<=0.0040 )
{
c= vec3(0.0,0.0,0.0);
}
else
{
c = py*1.4/iResolution.x*vec3(0.7+turbTerre/2.0,0.5+turbTerre/2.0,0.2+turbTerre/2.0)+0.2*diff*Color;
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, rd);
}
float turbEau = turbulence(vec2(rd.y,-5.0), 0.1, 20.0+mod(iTime*0.1,2.0), 0.2, 8);
float py = conv(pos.y);
if(py<=-4.0)
{
rgb= vec3(0.0, 0.25+turbEau/4.0,0.565+turbEau/4.0);
}
fragColor=vec4(rgb, 1.0);
}