Download

Raytraced distance fields in material

How can I raytrace SDFs inside material from given point? The similar way as distance field shadows, but from custom point in space. I’m trying to cutoff some parts of big explosions so they wouldn’t pass thru walls.

Sounds like a very expensive method, will you be able to see the explosion from both sides at the same time? If not you can disable the explosion FX if player its behind a wall. Make walls a little bit ticker also.

If that doesnt make it, you will have to check whether or not the explosion is inside or outside a volume, and set a mask.

As far as I know the raymarch will be too expensive.

I’d guess it all depends… I don’t have illusions about the cost. But there is stencil shadows, there is DF shadows, there is even volumetric light. And it all have it’s use regardless of how cheap or expensive it is. It just depends off it’s use implementation, target platform and settings.
Besides, I’m talking now not about small single sprite explosion with some sparks. I’m talking about general purpose method for FX’s of any scale or shape to be occluded by static geometry of the level. Explosion are just example. It’ll be useful in some forcefields, shileds, explosions etc. of any scale and geometry shapes formed by sprites or 3d models.
And yes I’m aware of most traditional methods to do explosions and faking it not to pass everything around, so thanks on that. But question is not about it, but on how to raytrace…

There are plenty of papers and tutorials on ray marching through distance fields. The basic idea is that you get the distance field at your start point, and the resulting number is the distance you move along your ray direction to test again. Keep repeating until you either reach the destination or get a very small value indicating that you are close enough to a surface to count as a hit.

It is a reasonable idea for what you want to accomplish. You could probably get away with a fairly small number of tests if you don’t need a lot of precision.

I think I’m starting to getting somewhere with this idea.

Looks good, looking forward to your progress

This is what I end up with for now. Not sure about how efficient this method actually are.
Perhaps it’s possible to do that in more efficient way. Would be nice to see some comments on this.

[video] https://www.youtube.com/watch?v=pwaWNSA6daU [/video]

It’s all based on basic version of the code from ray traced metaballs from one of the official streams.

This is what I end up with for now. Not sure how efficient this thing are.
Would be nice to see some comments on how to make it better, faster…
https://youtube.com/watch?v=pwaWNSA6daU
In the video there 256 iterations. It’s gives good quality for plane, some kind of 2d light.
But for the projection on sphere there is 64 iterations perfectly enough.
Also the smaler the object is, the lower iteration count needed.

It’s all based on simple version of the code from official video about ray traced metaballs.

Here’s the thing:

Material Setup:
Mat_Setup.jpg

Custom Node: Evaluate DistanceField


return GetDistanceToNearestSurfaceGlobal(curpos);

Custom Node: Raytracer


float3 RayDir = normalize(WorldPos-ActorPos);
float3 curpos = ActorPos;
float maxDist = distance(ActorPos, WorldPos);
float curdist;
float minstepsize = Radius/maxsteps;
int i = 0;
float res = 1;
float t =0;
float dist = distance(ActorPos, curpos);

while((i < maxsteps)&&(dist < maxDist))
{
    dist = distance(ActorPos, curpos);
    curdist = CustomExpression0(Parameters, curpos);
    if (curdist < thresh ) {
        return 0;
    }
    curpos += RayDir * minstepsize;
    res = min(res, (curdist-thresh)*(dist/100)/t/Softness);
    t += 1/curdist;
    i++;
}

return res;