-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.glsl
More file actions
378 lines (333 loc) · 9.88 KB
/
Copy pathfield.glsl
File metadata and controls
378 lines (333 loc) · 9.88 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 resolution;
float PI = 4.0*atan(1.0);
vec3 sunLight = normalize( vec3( 0.35, 0.2, 0.3 ) );
vec3 cameraPos;
vec3 sunColour = vec3(1.0, .75, .6);
const mat2 rotate2D = mat2(1.932, 1.623, -1.623, 1.95);
float gTime = 0.0;
//--------------------------------------------------------------------------
// Noise functions...
float Hash( float n )
{
return fract(sin(n)*43758.5453123);
}
//--------------------------------------------------------------------------
float Hash(vec2 p)
{
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
//----------------------------------------------------------------------
float Noise( in vec2 x )
{
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
float n = p.x + p.y*57.0;
float res = mix(mix( Hash(n+ 0.0), Hash(n+ 1.0),f.x),
mix( Hash(n+ 57.0), Hash(n+ 58.0),f.x),f.y);
return res;
}
vec2 Voronoi( in vec2 x )
{
vec2 p = floor( x );
vec2 f = fract( x );
float res=100.0,id;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
vec2 b = vec2( float(i), float(j) );
vec2 r = vec2( b ) - f + Hash( p + b );
float d = dot(r,r);
if( d < res )
{
res = d;
id = Hash(p+b);
}
}
return vec2(max(.4-sqrt(res), 0.0),id);
}
//--------------------------------------------------------------------------
vec2 Terrain( in vec2 p)
{
float type = 0.0;
vec2 pos = p*0.003;
float w = 50.0;
float f = .0;
for (int i = 0; i < 3; i++)
{
f += Noise(pos) * w;
w = w * 0.62;
pos *= 2.5;
}
return vec2(f, type);
}
//--------------------------------------------------------------------------
vec2 Map(in vec3 p)
{
vec2 h = Terrain(p.xz);
return vec2(p.y - h.x, h.y);
}
//--------------------------------------------------------------------------
float FractalNoise(in vec2 xy)
{
float w = .7;
float f = 0.0;
for (int i = 0; i < 3; i++)
{
f += Noise(xy) * w;
w = w*0.6;
xy = 2.0 * xy;
}
return f;
}
//--------------------------------------------------------------------------
// Grab all sky information for a given ray from camera
vec3 GetSky(in vec3 rd)
{
float sunAmount = max( dot( rd, sunLight), 0.0 );
float v = pow(1.0-max(rd.y,0.0),6.);
vec3 sky = mix(vec3(.1, .2, .3), vec3(.32, .32, .32), v);
sky = sky + sunColour * sunAmount * sunAmount * .25;
sky = sky + sunColour * min(pow(sunAmount, 800.0)*1.5, .3);
return clamp(sky, 0.0, 1.0);
}
//--------------------------------------------------------------------------
// Merge grass into the sky background for correct fog colouring...
vec3 ApplyFog( in vec3 rgb, in float dis, in vec3 dir)
{
float fogAmount = clamp(dis*dis* 0.0000012, 0.0, 1.0);
return mix( rgb, GetSky(dir), fogAmount );
}
//--------------------------------------------------------------------------
vec3 DE(vec3 p)
{
float base = Terrain(p.xz).x - 1.9;
float height = Noise(p.xz*2.0)*.75 + Noise(p.xz)*.35 + Noise(p.xz*.5)*.2;
//p.y += height;
float y = p.y - base-height;
y = y*y;
vec2 ret = Voronoi((p.xz*2.5+sin(y*4.0+p.zx*12.3)*.12+vec2(sin(time*2.3+1.5*p.z),sin(time*3.6+1.5*p.x))*y*.5));
float f = ret.x * .6 + y * .58;
return vec3( y - f*1.4, clamp(f * 1.5, 0.0, 1.0), ret.y);
}
//--------------------------------------------------------------------------
// eiffie's code for calculating the aperture size for a given distance...
float CircleOfConfusion(float t)
{
return max(t * .04, (2.0 / resolution.y) * (1.0+t));
}
//--------------------------------------------------------------------------
float Linstep(float a, float b, float t)
{
return clamp((t-a)/(b-a),0.,1.);
}
//--------------------------------------------------------------------------
vec3 GrassBlades(in vec3 rO, in vec3 rD, in vec3 mat, in float dist)
{
float d = 0.0;
float f;
// Only calculate cCoC once is enough here...
float rCoC = CircleOfConfusion(dist*.3);
float alpha = 0.0;
vec4 col = vec4(mat*0.15, 0.0);
for (int i = 0; i < 15; i++)
{
if (col.w > .99) break;
vec3 p = rO + rD * d;
vec3 ret = DE(p);
ret.x += .5 * rCoC;
if (ret.x < rCoC)
{
alpha = (1.0 - col.y) * Linstep(-rCoC, rCoC, -ret.x);//calculate the mix like cloud density
f = clamp(ret.y, 0.0, 1.0);
// Mix material with white tips for grass...
vec3 gra = mix(mat, vec3(.35, .35, min(pow(ret.z, 4.0)*35.0, .35)), pow(ret.y, 9.0)*.7) * ret.y;
col += vec4(gra * alpha, alpha);
}
d += max(ret.x * .7, .1);
}
if(col.w < .2)
col.xyz = vec3(0.1, .15, 0.05);
return col.xyz;
}
//--------------------------------------------------------------------------
// Calculate sun light...
void DoLighting(inout vec3 mat, in vec3 pos, in vec3 normal, in vec3 eyeDir, in float dis)
{
float h = dot(sunLight,normal);
mat = mat * sunColour*(max(h, 0.0)+.2);
}
//--------------------------------------------------------------------------
vec3 TerrainColour(vec3 pos, vec3 dir, vec3 normal, float dis, float type)
{
vec3 mat;
if (type == 0.0)
{
// Random colour...
mat = mix(vec3(.0,.3,.0), vec3(.2,.3,.0), Noise(pos.xz*.025));
// Random shadows...
float t = FractalNoise(pos.xz * .1)+.5;
// Do grass blade tracing...
mat = GrassBlades(pos, dir, mat, dis) * t;
DoLighting(mat, pos, normal,dir, dis);
}
mat = ApplyFog(mat, dis, dir);
return mat;
}
//--------------------------------------------------------------------------
// Home in on the surface by dividing by two and split...
float BinarySubdivision(in vec3 rO, in vec3 rD, float t, float oldT)
{
float halfwayT = 0.0;
for (int n = 0; n < 5; n++)
{
halfwayT = (oldT + t ) * .5;
if (Map(rO + halfwayT*rD).x < .05)
{
t = halfwayT;
}else
{
oldT = halfwayT;
}
}
return t;
}
//--------------------------------------------------------------------------
bool Scene(in vec3 rO, in vec3 rD, out float resT, out float type )
{
float t = 5.;
float oldT = 0.0;
float delta = 0.;
vec2 h = vec2(1.0, 1.0);
bool hit = false;
for( int j=0; j < 80; j++ )
{
vec3 p = rO + t*rD;
if (p.y < 105.0 && !hit)
{
h = Map(p); // ...Get this position's height mapping.
// Are we inside, and close enough to fudge a hit?...
if( h.x < 0.05)
{
// Yes! So home in on height map...
resT = BinarySubdivision(rO, rD, t, oldT);
type = h.y;
hit = true;
}else
{
// Delta ray advance - a fudge between the height returned
// and the distance already travelled.
// Compromise between speed and accuracy...
delta = max(0.04, 0.35*h.x) + (t*0.04);
oldT = t;
t += delta;
}
}
}
return hit;
}
//--------------------------------------------------------------------------
vec3 CameraPath( float t )
{
//t = time + t;
vec2 p = vec2(200.0 * sin(3.54*t), 200.0 * cos(2.0*t) );
return vec3(p.x+55.0, 12.0+sin(t*.3)*6.5, -94.0+p.y);
}
//--------------------------------------------------------------------------
vec3 PostEffects(vec3 rgb, vec2 xy)
{
// Gamma first...
rgb = pow(rgb, vec3(0.45));
// Then...
#define CONTRAST 1.1
#define SATURATION 1.3
#define BRIGHTNESS 1.3
rgb = mix(vec3(.5), mix(vec3(dot(vec3(.2125, .7154, .0721), rgb*BRIGHTNESS)), rgb*BRIGHTNESS, SATURATION), CONTRAST);
// Vignette...
rgb *= .4+0.5*pow(40.0*xy.x*xy.y*(1.0-xy.x)*(1.0-xy.y), 0.2 );
return rgb;
}
//--------------------------------------------------------------------------
void main(void)
{
float gTime = (time*5.0+2352.0)*.006;
vec2 xy = gl_FragCoord.xy / resolution.xy;
vec2 uv = (-1.0 + 2.0 * xy) * vec2(resolution.x/resolution.y,1.0);
vec3 camTar;
if (xy.y < .13 || xy.y >= .87)
{
// Top and bottom cine-crop - what a waste! :)
gl_FragColor=vec4(vec4(0.0));
return;
}
#ifdef STEREO
float isCyan = mod(gl_FragCoord.x + mod(gl_FragCoord.y,2.0),2.0);
#endif
cameraPos = CameraPath(gTime + 0.0);
camTar = CameraPath(gTime + .009);
cameraPos.y += Terrain(CameraPath(gTime + .009).xz).x;
camTar.y = cameraPos.y;
float roll = .4*sin(gTime+.5);
vec3 cw = normalize(camTar-cameraPos);
vec3 cp = vec3(sin(roll), cos(roll),0.0);
vec3 cu = cross(cw,cp);
vec3 cv = cross(cu,cw);
vec3 dir = normalize(uv.x*cu + uv.y*cv + 1.3*cw);
mat3 camMat = mat3(cu, cv, cw);
#ifdef STEREO
cameraPos += .85*cu*isCyan; // move camera to the right - the rd vector is still good
#endif
vec3 col;
float distance;
float type;
if( !Scene(cameraPos, dir, distance, type) )
{
// Missed scene, now just get the sky...
col = GetSky(dir);
}
else
{
// Get world coordinate of landscape...
vec3 pos = cameraPos + distance * dir;
// Get normal from sampling the high definition height map
// Use the distance to sample larger gaps to help stop aliasing...
vec2 p = vec2(0.1, 0.0);
vec3 nor = vec3(0.0, Terrain(pos.xz).x, 0.0);
vec3 v2 = nor-vec3(p.x, Terrain(pos.xz+p).x, 0.0);
vec3 v3 = nor-vec3(0.0, Terrain(pos.xz-p.yx).x, -p.x);
nor = cross(v2, v3);
nor = normalize(nor);
// Get the colour using all available data...
col = TerrainColour(pos, dir, nor, distance, type);
}
// bri is the brightness of sun at the centre of the camera direction.
// Yeah, the lens flares is not exactly subtle, but it was good fun making it.
float bri = dot(cw, sunLight)*.75;
if (bri > 0.0)
{
vec2 sunPos = vec2( dot( sunLight, cu ), dot( sunLight, cv ) );
vec2 uvT = uv-sunPos;
uvT = uvT*(length(uvT));
bri = pow(bri, 6.0)*.8;
// glare = the red shifted blob...
float glare1 = max(dot(normalize(vec3(dir.x, dir.y+.3, dir.z)),sunLight),0.0)*1.4;
// glare2 is the yellow ring...
float glare2 = max(1.0-length(uvT+sunPos*.5)*4.0, 0.0);
uvT = mix (uvT, uv, -2.3);
// glare3 is a purple splodge...
float glare3 = max(1.0-length(uvT+sunPos*5.0)*1.2, 0.0);
col += bri * vec3(1.0, .0, .0) * pow(glare1, 12.5)*.05;
col += bri * vec3(1.0, 1.0, 0.2) * pow(glare2, 2.0)*2.5;
col += bri * sunColour * pow(glare3, 2.0)*3.0;
}
col = PostEffects(col, xy);
#ifdef STEREO
col *= vec3( isCyan, 1.0-isCyan, 1.0-isCyan );
#endif
gl_FragColor=vec4(col,1.0);
}
//--------------------------------------------------------------------------