Skip to content

Commit 7d74b3a

Browse files
authored
Minor fixes and added texture filtering
1 parent 044d2d6 commit 7d74b3a

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

assets/frag.glsl

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
#define SUN_SHARPNESS 2.0
4141
#define SUN_SIZE 0.004
4242
#define VIGNETTE_STRENGTH 0.5
43-
#define FRACTAL_ITER 16
43+
#define FRACTAL_ITER 14
44+
#define ENABLE_FILTERING 1
4445

4546
uniform mat4 iMat;
4647
uniform vec2 iResolution;
@@ -198,7 +199,7 @@ vec4 col_scene(vec4 p) {
198199
// Main code
199200
//##########################################
200201

201-
//A faster formula to find the gradient/normal diraction of the DE(the w component is the average DE)
202+
//A faster formula to find the gradient/normal direction of the DE(the w component is the average DE)
202203
//credit to http://www.iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
203204
vec4 calcGrad(vec4 p, float dx)
204205
{
@@ -209,6 +210,16 @@ vec4 calcGrad(vec4 p, float dx)
209210
k.xxxx*DE(p + k.xxxz*dx)) / vec4(4*dx,4*dx,4*dx,4);
210211
}
211212

213+
//find the average color of the fractal in a radius dx
214+
vec4 smoothColor(vec4 p, float dx)
215+
{
216+
const vec3 k = vec3(1,-1,0);
217+
return (COL(p + k.xyyz*dx) +
218+
COL(p + k.yyxz*dx) +
219+
COL(p + k.yxyz*dx) +
220+
COL(p + k.xxxz*dx))/4;
221+
}
222+
212223
vec4 ray_march(inout vec4 p, vec4 ray, float sharpness) {
213224
//March the ray
214225
float d = DE(p);
@@ -222,8 +233,8 @@ vec4 ray_march(inout vec4 p, vec4 ray, float sharpness) {
222233
for (; s < MAX_MARCHES; s += 1.0) {
223234
//if the distance from the surface is less than the distance per pixel we stop
224235
float min_dist = max(FOVperPixel*td, MIN_DIST);
225-
if (d < min_dist ) {
226-
s += 0.14*d / min_dist ;
236+
if (d < min_dist) {
237+
s += 0.1*d /min_dist;
227238
break;
228239
} else if (td > MAX_DIST) {
229240
break;
@@ -245,21 +256,24 @@ vec4 scene(inout vec4 p, inout vec4 ray, float vignette) {
245256

246257
//Determine the color for this pixel
247258
vec4 col = vec4(0.0);
248-
249259
float min_dist = max(FOVperPixel*td, MIN_DIST);
250260
if (d < min_dist) {
251261
//Get the surface normal
252-
vec4 grad = calcGrad(p,td*FOVperPixel*0.2);
262+
vec4 grad = calcGrad(p,min_dist*0.5);
253263
vec3 n = normalize(grad.xyz);
254264

255265
//find closest surface point, without this we get weird coloring artifacts
256266
p.xyz -= n*d;
257-
258-
vec3 reflected = ray.xyz - 2.0*dot(ray.xyz, n) * n;
259267

268+
vec3 reflected = ray.xyz - 2.0*dot(ray.xyz, n) * n;
269+
260270
//Get coloring
261-
vec4 orig_col = clamp(COL(p), 0.0, 1.0);
262-
col.w = orig_col.w;
271+
#if ENABLE_FILTERING
272+
vec4 orig_col = clamp(smoothColor(p, min_dist*0.5), 0.0, 1.0);
273+
#else
274+
vec4 orig_col = clamp(COL(p), 0.0, 1.0);
275+
#endif
276+
col.w = orig_col.w;
263277

264278
//Get if this point is in shadow
265279
float k = 1.0;
@@ -325,7 +339,7 @@ void main() {
325339
vec2 screen_pos = (gl_FragCoord.xy + delta) / iResolution.xy;
326340

327341
//Calculate the view angle per pixel
328-
FOVperPixel = 90*PI/(180*iResolution.x);
342+
FOVperPixel = 1*90*PI/(180*iResolution.x);
329343

330344
vec2 uv = 2*screen_pos - 1;
331345
uv.x *= iResolution.x / iResolution.y;

0 commit comments

Comments
 (0)