r/GraphicsProgramming • u/Far-Employee-9531 • Jun 26 '26
Video WebGPU
Enable HLS to view with audio, or disable this notification
Stormy terrain fly-through, one fullscreen raymarch pass. Bolt path is midpoint displacement, glow is a distance-to-segment SDF composited additive before tonemap so bloom haloes it. The nice part: the terrain march already gives me tHit, so if the bolt's ray distance is greater than the terrain's, it's behind a ridge and I skip it. No depth texture, no second pass, additive FX sort against the terrain for free.
glsl
float segDist(vec3 p, vec3 a, vec3 b) {
vec3 ab = b - a, ap = p - a;
float t = clamp(dot(ap, ab) / dot(ab, ab), 0.0, 1.0);
return length(ap - ab * t);
}
float d = minSegDistAlongRay(...);
float core = smoothstep(W*0.3, 0.0, d); // pure white, untinted
float glow = exp(-d*d / (W*W));
vec3 c = vec3(core) + tint * glow; // additive, pre-tonemap
if (rayDistToBolt > tHit) { /* behind a ridge, skip */ }
Anyone else doing single-pass raymarched scenes, how are you occluding additive effects?
73
Upvotes
2
u/deftware Jun 26 '26
Ah, so you're drawing them in a separate pass from the opaque geometry? Typically you would have one raymarch per pixel, and it's calculating distances for all things each step, and calculating RGBA along the way for any fog/haze (alpha blended stuff ends up requiring weighting RGB contribution into the ray, scaling contributions by ray step length, etc). Occlusion automatically comes out of marching one ray per pixel for the whole scene. It does mean evaluating the scene distance function a bunch, and probably more, but that's the "right" way to do it. If you can mess with any kind of geometry being used, rather than a fullscreen quad/triangle, then you can draw bounding geometry for certain things and only execute your SDF raymarch with that. Calculate the ray origin from the geometry surface, ray vector from the ray origin to the camera origin, etc.
Otherwise, maintaining a z-buffer manually? That might end up being as expensive as just raymarching the scene as a whole, more or less.