Logo by Cyclops - Contribute your own Logo!

END OF AN ERA, FRACTALFORUMS.COM IS CONTINUED ON FRACTALFORUMS.ORG

it was a great time but no longer maintainable by c.Kleinhuis contact him for any data retrieval,
thanks and see you perhaps in 10 years again

this forum will stay online for reference
News: Visit us on facebook
 
*
Welcome, Guest. Please login or register. April 24, 2024, 01:28:05 PM


Login with username, password and session length


The All New FractalForums is now in Public Beta Testing! Visit FractalForums.org and check it out!


Pages: [1]   Go Down
  Print  
Share this topic on DiggShare this topic on FacebookShare this topic on GoogleShare this topic on RedditShare this topic on StumbleUponShare this topic on Twitter
Author Topic: Sphere Tracing Shadows  (Read 1441 times)
0 Members and 1 Guest are viewing this topic.
asimes
Fractal Lover
**
Posts: 212



asimes
WWW
« on: November 23, 2014, 07:22:22 AM »

I have been working on a simple Sphere Tracer that handles shadows, but it is slightly inaccurate. For most objects like boxes, spheres, Menger Cubes, etc. this inaccuracy does not matter much. However, I have been trying to apply it to the Mandelbox and now that slight inaccuracy is a serious problem, some of the surface looks like noise. I wrote the code originally in Processing and have ported it to WebGL, I am guessing that most people here are more comfortable with GLSL so I'll use that for posting code

Trying to follow along some explanations by Iñigo Quilez and Syntopia, I decided that the general idea of implementing shadows should work something like this:
- March in some direction along a ray from the camera until close enough to a surface (my "close enough" value is called MIN_DIST and is set to 0.0001) and consider that the surface "intersection" even though it may be up to MIN_DIST inaccurate
- For each light source, march from the light source in the direction of the surface intersection until close enough to some surface which also may be up to MIN_DIST inaccurate

The problem with the second part is detecting when the first surface to be hit during the light march is be occluding the surface intersection. My original idea for detecting this was to set a maximum distance for the light march which would be the distance from the light to the surface minus 2.0*MIN_DIST. If the current distance was less than MIN_DIST before the maximum was reached then the surface intersection should be occluded, otherwise if the maximum is reached then the surface intersection is not occluded. This turned out to not work so well but increasing the maximum distance to be the distance from the light to the surface intersection minus 10.0*MIN_DIST worked fine for most objects (but not the Mandelbox)

How is this normally handled? For anyone willing to look, below is a section of my GLSL code that handles the camera march and light marches:
- MAX_DIST is 400.0 (more than enough for my testing scene)
- MIN_DIST is 0.001
- I can't iterate an unknown number of times in WebGL but the limit of 1000 is normally never reached
- MIN_DIST_TEN is MIN_DIST*10.0

Code:
float sphereTrace(vec3 inPos, vec3 inDir) {
vec3 march;
float currDist = MAX_DIST;

float t = MIN_DIST;
for (int i = 0; i < 1000; i++) {
march = inPos+inDir*t;

currDist = worldDist(march);
t += currDist;

if (currDist < MIN_DIST || t > MAX_DIST) break;
}

if (currDist < MIN_DIST) {
march = inPos+inDir*t;

float val = POLISHED_SILVER_AMBIENT;
for (int i = 0; i < NUM_LIGHTS; i++)
if (lightTrace(march, lightPos[i])) val += shade(march, lightPos[i]);
return val;
}

return 0.0;
}

bool lightTrace(vec3 inMarch, vec3 inLightPos) {
float shadowMaxDist = length(inMarch-inLightPos)-MIN_DIST_TEN;

vec3 march;
vec3 dir = normalize(inMarch-inLightPos);

float t = MIN_DIST;
for (int i = 0; i < 1000; i++) {
march = inLightPos+dir*t;

float currDist = worldDist(march);
t += currDist;

if (t >= shadowMaxDist) return true;
else if (currDist < MIN_DIST) return false;
}

return true;
}
« Last Edit: November 23, 2014, 07:49:26 AM by asimes » Logged
asimes
Fractal Lover
**
Posts: 212



asimes
WWW
« Reply #1 on: November 23, 2014, 07:32:54 AM »

Edit: The artifacts are a lot more noticeable if the images are viewed in a separate window, their size is scaled down here and the scaling down is hiding the artifacts

Here's an example of the difference between Mandelbox (20 iterations, scale 2.0) and Menger (4 iterations):



And some Mandelbox samples at scales: 2.0, 2.5, and 3.0 (I guess the Mandelbox shrinks at larger scales?)

« Last Edit: November 23, 2014, 07:54:11 AM by asimes » Logged
eiffie
Guest
« Reply #2 on: November 23, 2014, 08:38:38 PM »

Usually we start at the surface plus MINDIST towards the light and march in that direction. Then the distance marched does not need to be very accurate.
Logged
asimes
Fractal Lover
**
Posts: 212



asimes
WWW
« Reply #3 on: November 24, 2014, 09:16:55 AM »

eiffie, thanks didn't occur to me to start from the surface intersection and then go towards the light

I gave it a shot in Processing. I march to the surface, backstep one MIN_DIST in the direction of the camera, and then start the light march at 2.0*MIN_DIST in the direction of the light from there. For whatever reason starting the light march one MIN_DIST in the direction of the light from the backstep gave me artifacts, doesn't make sense why it happens to be honest but I have decent results with 2.0*MIN_DIST in the direction of the light

Thanks again, below are the results of MIN_DIST and 2.0*MIN_DIST in the direction of the light from the backstep:

Logged
asimes
Fractal Lover
**
Posts: 212



asimes
WWW
« Reply #4 on: December 06, 2014, 12:38:20 AM »

Still not sure about the MIN_DIST detail, but I found that the artifacts are not very apparent at higher resolution. I had a chance to implement this on some high resolution displays, it looks good at MIN_DIST*3.0 but sometimes surfaces seem to occlude themselves at 2.0 or 1.0:

Logged
mclarekin
Fractal Senior
******
Posts: 1739



« Reply #5 on: December 06, 2014, 02:10:54 AM »

Looking good.

And a programming post that I could actually kind of understand, thanks
Logged
Pages: [1]   Go Down
  Print  
 
Jump to:  

Related Topics
Subject Started by Replies Views Last post
Shadows & light Mandelbulb3D Gallery Bent-Winged Angel 0 1250 Last post June 26, 2010, 01:08:25 AM
by Bent-Winged Angel
IN THE SHADOWS Images Showcase (Rate My Fractal) Well En Taoed 0 931 Last post August 03, 2010, 05:08:03 PM
by Well En Taoed
Hard shadows in M3D Mandelbulb 3d bib 6 2863 Last post November 17, 2010, 10:13:35 PM
by Jesse
Hello there! *out of the shadows* Meet & Greet SpiralVortex 1 516 Last post May 15, 2013, 08:18:32 AM
by cKleinhuis
Enhanced sphere tracing paper 3D Fractal Generation subblue 5 4030 Last post November 11, 2014, 07:01:51 PM
by eiffie

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.163 seconds with 27 queries. (Pretty URLs adds 0.007s, 2q)