Jump to content

Volume Caustics


konstantin magnus

Recommended Posts

  • 4 months later...
  • 2 years later...

Thank you Tesan! 

light_2d_od.thumb.jpg.561c22c82ee7276adef04ce78ce420f4.jpg

I've just translated the first example from C to VEX for a volume wrangle.

// Use Monte Carlo integration and ray marching
// of signed distance field (SDF) to render emissive circles.
// Source: https://github.com/miloyip/light2d/blob/master/basic.c

int samples = chi('samples');

function float sd_circle(vector2 uv, pos; float radius){
    float dist = distance(uv, pos) - radius;
    return dist;
}

function float sd_scene(vector2 uv){
    float d_0 = sd_circle(uv, {0.3, 0.18}, 0.12);
    float d_1 = sd_circle(uv, {0.25, 0.18}, 0.12);
    float d_2 = sd_circle(uv, {-0.3, -0.2}, 0.02);
    float dist = min( max(d_0, -d_1), d_2);
    return max(dist);
}

function float trace(vector2 uv, dist){
    float t = 0.0;
    for(int i = 0; i < 100 && t < 2.0; i++){
        float sd = sd_scene(uv + dist * t);
        if(sd < 1e-5) return 1.5;
        t += sd;
    }
    return 0.0;
}

function float sample(vector2 uv; int samples){
    float sum = 0.0;
    for(int i = 0; i < samples; i++){
        float a = 2.0 * PI * ((rand(uv) + i) / float(samples));
        sum += trace(uv, set(cos(a), sin(a)));
    }
    return sum / float(samples); 
}

vector2 uv = set(v@P.x, v@P.y);

f@d = sample(uv, samples);

 

light_2D.hip

  • Like 5
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...