-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnowywoods.frag
431 lines (375 loc) · 11.8 KB
/
snowywoods.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Define all the things!
//==RAY MARCHING CONSTANTS=========================================================
#define EPSILON .001525
#define MAX_VIEW_STEPS 64
#define MAX_SHADOW_STEPS 64
#define OCCLUSION_SAMPLES 6.0
#define OCCLUSION_FACTOR 1.75
#define MAX_DEPTH 15.0
#define FEATURE_BUMP_FACTOR .03
#define GROUND_BUMP_FACTOR .03
#define SMIN_FACTOR 48.15
//==OBJECT CONSTANTS========================================================
#define GROUND_NORMAL vec3(0.0, 1.0, 0.0)
#define GROUND_HEIGHT 1.0
#define TREE_PROP vec3(0., 0.1, .1)
#define TREE_REP vec3(1.0, 0.0, 2.0)
#define TREE_SNOW_PROP vec3(-.015, 0.0965, .0825)
#define TREE_SNOW_REP vec3(1.0, 0.0, 2.0)
#define SNOW_BOTTOM vec3(-0.1,-1.0, 0.1)
#define SNOW_TOP vec3(0.005, -.0, 0.1)
#define SNOW_RAD .1
#define SNOW_BUMP iChannel0
#define TREE_BUMP iChannel1
#define TREE_SNOW_BUMP iChannel2
#define TREE_TEX_SCALE .4
#define SNOW_TEX_SCALE .35
#define TREE_SNOW_TEX_SCALE .45
#define LIGHT_SPREAD .2
#define LIGHT_COLOR vec3(1.0, 1.0, .95)
#define LIGHT_BRIGHT 0.667
#define AMBIENT_COLOR vec3(.6, .6, 1.0)
#define AMBIENT_LIGHT .0125
#define PEN_FACTOR 20.0
//==RENDERING STRUCTURES===========================================================
/*
A structure for a spotlight.
*/
struct SpotLight
{
vec3 position, direction;
vec3 color;
float brightness;
float spread;
float penumbraFactor;
};
/*
A structure for a less simple material.
*/
struct Material
{
vec3 color;
float gloss;
};
//==CAMERA FUNCTIONS================================================================
/*
TEKF https://www.shadertoy.com/view/XdsGDB
Set up a camera looking at the scene.
origin - camera is positioned relative to, and looking at, this point
dist - how far camera is from origin
rotation - about x & y axes, by left-hand screw rule, relative to camera looking along +z
zoom - the relative length of the lens
*/
void camPolar( out vec3 pos, out vec3 dir, in vec3 origin, in vec2 rotation, in float dist, in float zoom )
{
// get rotation coefficients
vec2 c = cos(rotation);
vec4 s;
s.xy = sin(rotation);
s.zw = -s.xy;
// ray in view space
dir.xy = gl_FragCoord.xy - iResolution.xy*.5;
dir.z = iResolution.y*zoom;
dir = normalize(dir);
// rotate ray
dir.yz = dir.yz*c.x + dir.zy*s.zx;
dir.xz = dir.xz*c.y + dir.zx*s.yw;
// position camera
pos = origin - dist*vec3(c.x*s.y,s.z,c.x*c.y);
}
//==TEXTURING FUNCTIONS=============================================================
/*
Takes a 3D coordinate, and returns a texel based on which plane(s)
it lies in.
*/
vec3 tex3D( in vec3 pos, in vec3 normal, sampler2D sampler )
{
return texture2D( sampler, pos.yz ).rgb*abs(normal.x)+
texture2D( sampler, pos.zx ).rgb*abs(normal.y)+
texture2D( sampler, pos.xy ).rgb*abs(normal.z);
}
//==ASSORTED MATH STUFFS============================================================
/*
Returns a smoothed minimum between two values. (By Inigo Quilez)
*/
float smin( float a, float b, float k )
{
float res = exp( -k*a ) + exp( -k*b );
return -log( res )/k;
}
//==DISTANCE FUNCTIONS==============================================================
// aka the Inigo Quilez section
/*
Returns the distance to the surface of a plane with the given height and normal.
This is signed as well; if you are below the plane you will get negative values.
*/
float distPlaneBump(vec3 samplePos, vec3 planeNormal, float planeHeight, sampler2D image, float scale)
{
float bump = 0.0;
float dist = dot(samplePos, normalize(planeNormal)) + planeHeight;
if(dist < GROUND_BUMP_FACTOR*2.0)
bump = texture2D(image, samplePos.xz*scale).r*GROUND_BUMP_FACTOR;
return (dist-GROUND_BUMP_FACTOR)+bump;
}
/*
Returns the distance to a repeated cylinder, with no image based surface displacement.
*/
float distCylinder( vec3 pos, vec3 properties )
{
pos.xz += sin(pos.zx)*.25;
pos.xz = mod(pos.xz,TREE_REP.xz);
pos.xz -= vec2(TREE_REP.xz*.5);
return length(pos.xz-properties.xy)-properties.z;
}
/*
Returns the distance to a repeated cylinder, with image based surface displacement.
*/
float distCylinderBump( vec3 pos, vec3 properties, sampler2D image, float scale)
{
// Add in a bit of positional variation.
pos.xz += sin(pos.zx)*.25;
// Mod the position along the XZ plane for repetition.
pos.xz = mod(pos.xz,TREE_REP.xz);
// Bring the location back to the center of the modded domain.
pos.xz -= vec2(TREE_REP.xz*.5);
// Create a variable to store potential bump.
float bump = 0.0;
// Find the distance from the point to the object
// accounting for minimum bump height.
float dist = length(pos.xz-properties.xy)-properties.z;
// If it's less than the maximum bump height we need to figure
// out the specific distance to the object including proper bump.
if(dist < FEATURE_BUMP_FACTOR*2.0)
{
// Get a general okay flat-surface normal.
vec2 normal = normalize(pos.xz-properties.xy);
// Get the bumpheight by sampling the red channel of a texture.
bump = tex3D(pos*scale+pos, vec3(normal.x, 0.0, normal.y), image).r*FEATURE_BUMP_FACTOR;
}
return dist-bump;
}
/*
Returns the distance to a repeated capsule shape.
*/
float distCapsule(vec3 pos, vec3 a, vec3 b, float r, sampler2D image, float scale)
{
pos.xz += sin(pos.zx)*.25;
pos.xz = mod(pos.xz,TREE_REP.xz);
pos.xz -= vec2(TREE_REP.xz*.5);
vec3 pa = (pos) - a;
vec3 ba = b - a;
float h = clamp( dot(pa, ba)/dot(ba, ba), 0.0, 1.0 );
float bump = 0.0;
float dist = length( pa - ba*h ) - r;
if(dist < FEATURE_BUMP_FACTOR)
{
vec2 normal = normalize(pos.xy);
vec3(normal.x, 0.0, normal.y);
bump = tex3D(pos*scale, vec3(normal.x, 0.0, normal.y), image).r*FEATURE_BUMP_FACTOR*.5;
}
return dist + bump;
}
/*
Returns the distance to the nearest snow feature, whether it's the ground, the
tree collection, or the windswept bits.
*/
float distSnow(vec3 pos)
{
return min(smin(distPlaneBump(pos, GROUND_NORMAL, GROUND_HEIGHT, SNOW_BUMP, SNOW_TEX_SCALE),
distCapsule(pos, SNOW_BOTTOM, SNOW_TOP, SNOW_RAD, SNOW_BUMP, SNOW_TEX_SCALE), SMIN_FACTOR),
distCylinderBump(pos, TREE_SNOW_PROP, TREE_SNOW_BUMP, SNOW_TEX_SCALE));
}
/*
Returns the distance to the nearest tree.
*/
float distTree(vec3 pos)
{
return distCylinderBump(pos, TREE_PROP, TREE_BUMP, TREE_TEX_SCALE);
}
/*
Returns the base normal of a repeated cylinder.
*/
vec3 getCylinderNormal(vec3 pos, vec3 properties)
{
// Perform modulation to keep in line with our cylinder distance function.
pos.xz = mod(pos.xz,TREE_REP.xz);
pos.xz -= vec2(TREE_REP.xz*.5);
// Since we can assume that the cylinder is vertical,
// the only coordinates that matter are x and z.
// This speeds up normal generation quite a bit.
vec2 normal = normalize(pos.xz-properties.xy);
return vec3(normal.x, 0.0, normal.y);
}
/*
Returns the distance to the nearest point in the scene.
*/
float getDist(vec3 samplePos)
{
return min(distTree(samplePos),
distSnow(samplePos));
}
/*
Returns the color of the nearest object in the scene.
*/
vec3 getColor(vec3 samplePos)
{
// If we are closer to snow, return the color of the snow.
if(distSnow(samplePos) < distTree(samplePos))
{
return vec3(1.0);
}
// Otherwise return our "wood" texture.
else return tex3D(samplePos*4.0, getCylinderNormal(samplePos, TREE_PROP), iChannel0)*vec3(.75, .75, .85);
}
//==RAY MARCHING FUNCTIONS=========================================================
/*
Marches the 3D point <pos> along the given direction.
When the point is either stepped the maximum number of times,
has passed the maximum distance, or is within a set distance
from geometry the function returns.
Note that the position is passed by reference and is modified
for use within the function.
*/
void marchThroughField(inout vec3 pos, vec3 dir)
{
float dist;
for(int i = 0; i < MAX_VIEW_STEPS; i++)
{
dist = getDist(pos);
if(dist < EPSILON || dist > MAX_DEPTH)
return;
else
pos += dir*dist*.75;
}
return;
}
//==LIGHTING FUNCTIONS==============================================================
/*
Returns the surface normal of a point in the distance function.
*/
vec3 getNormal(vec3 pos)
{
float d=getDist(pos);
// Create the normal vector by comparing the distance near our point.
return normalize(vec3( getDist(pos+vec3(EPSILON,0,0))-d, getDist(pos+vec3(0,EPSILON,0))-d, getDist(pos+vec3(0,0,EPSILON))-d ));
}
/*
Returns the amount of fog in a scene at the given distance.
*/
vec3 addFog(float dist, vec3 before, vec3 fogColor)
{
return mix(before, fogColor, pow((dist/MAX_DEPTH),2.0));
}
/*
Calculates the ambient occlusion factor at a given point in space.
Written from Inigo Quilez' algorithm.
*/
float calcOcclusion(vec3 pos, vec3 norm)
{
float result = 1.0;
float s = -OCCLUSION_SAMPLES;
const float unit = 1.0/OCCLUSION_SAMPLES;
for(float i = unit; i < 1.0; i+=unit)
{
result -= pow(2.0,i*s)*(i-getDist(pos+i*norm));
}
return result*OCCLUSION_FACTOR;
}
/*
Calculates how much light remains if shadows are considered.
Generally IQ's soft shadow algorithm.
*/
float calcShadow( vec3 origin, vec3 lightDir, SpotLight light)
{
float dist;
float result = 1.0;
float lightDist = length(light.position-origin);
vec3 pos = vec3(origin)+(lightDir*(EPSILON+FEATURE_BUMP_FACTOR));
for(int i = 0; i < MAX_SHADOW_STEPS; i++)
{
dist = getDist(pos);
if(dist < EPSILON)
{
return 0.0;
}
if(length(pos-origin) > lightDist || length(pos-origin) > MAX_DEPTH)
{
return result;
}
pos+=lightDir*dist;
if( length(pos-origin) < lightDist )
{
result = min( result, lightDist*light.penumbraFactor*dist / length(pos-origin) );
}
}
return result;
}
/*
Returns the product of the Phong lighting equation on a point in space given
a (SPOT) light and the surface's material.
*/
vec3 calcLighting(vec3 samplePos, vec3 eye, SpotLight light, Material material)
{
float lightDist = length(light.position-samplePos);
vec3 lightDir = normalize(light.position-samplePos);
vec3 eyeDir = normalize(samplePos-eye);
vec3 surfaceNormal = getNormal(samplePos);
vec3 reflection = normalize(reflect(eyeDir, surfaceNormal));
float specular, diffuse, ambient = AMBIENT_LIGHT;
float attenuation, shadow, occlusion;
float spotCos = dot(-lightDir, light.direction);
float spotCoefficient = smoothstep( 1.0-light.spread, 1.0, spotCos );
// If it's outside of the light's cone we don't need to calculate any terms.
if(spotCos < 1.0-light.spread)
{
specular = 0.0;
diffuse = 0.0;
attenuation = 0.0;
shadow = 0.0;
}
else
{
specular = pow(max( 0.0, dot(lightDir, reflection)*spotCoefficient), 80.4);
diffuse = max( 0.0, dot(lightDir, surfaceNormal)*spotCoefficient);
attenuation = min(1.0, (1.0/(lightDist/light.brightness)));
shadow = calcShadow(samplePos, lightDir, light);
}
occlusion = calcOcclusion(samplePos, surfaceNormal);
vec3 objectColor = getColor(samplePos);
return light.color*objectColor*clamp(((specular+diffuse)*shadow*attenuation), 0.0, 1.0)+(ambient*occlusion*AMBIENT_COLOR*objectColor);
}
/*
Shade a point after it has been marched and found.
*/
vec3 shade(vec3 pos, vec3 eye, SpotLight light, Material material)
{
vec3 fog = textureCube(iChannel3, normalize(eye-pos)).rgb*AMBIENT_COLOR;
float dist = length(eye-pos);
if(dist > MAX_DEPTH)
{
return fog;
}
else
{
vec3 litTexel = calcLighting(pos, eye, light, material);
return addFog(dist, litTexel, fog);
}
}
void main(void)
{
vec3 pos, dir, eye;
vec2 facing = (iMouse.yx/iResolution.yx)-vec2(.5);
vec3 camPos = vec3(0.0, .025*abs(sin(iGlobalTime*5.0)), iGlobalTime-.5);
vec3 lightPos = vec3(0.0, .025*abs(sin(iGlobalTime*5.0))-.01, iGlobalTime);
vec3 lightDir = normalize(vec3(sin(facing.y), sin(facing.x)*2.0, cos(facing.y)));
facing.x = -facing.x;
SpotLight light = SpotLight(lightPos,
lightDir,
LIGHT_COLOR,
LIGHT_BRIGHT, LIGHT_SPREAD, PEN_FACTOR);
Material mat = Material(vec3(1.0), 1.0);
camPolar(pos, dir, camPos, facing, .05, 1.0);
eye = vec3(pos);
marchThroughField(pos, dir);
gl_FragColor = pow(vec4(shade(pos, eye, light, mat), 1.0), vec4(1.0/2.2));
}