Trying to replicate Unreal Engine's depth Fade node






This is my progress so far, but it has a limitations, I’m not able to consider the transparency of objects behind the surface., does anybody know if the

"getmessage("trace’…) "

can check the opacity of the hitpoint surface

4 Likes

Cool !

From what I recall you can’t retrieve much from a trace, IIRC object name, index and maybe a few other things but that’s about it…
At least I think it’s impossible to get a vertex color , a texture value or something along these lines.

OSL supports more, but not the blender implementation as some parts are still left as todo for a while …

But I just worked with OSL for a few months last year so I’m not an expert by far :slight_smile:

1 Like

Thanks Mate!,

I guess at the moment dealing with transparency of background objects is not possible. its so sad, I have been trying to achieve this for months and now that I have some success, there is another obstacle, well we can wait for new developments!

I’m not good at this but this document was posted by the blender developers and it says that getmessage() can retrieve shader parameters, by any chance could we get the transparency of the hit surface




added some refraction to the depth mask

4 Likes



Playing around with the Node

5 Likes

Hey ! First of congrats on your results , it looks pretty cool !!

The documentation you posted about is misleading, it’s from the OSL docs, and as I was saying latter, OSL can do that but current blender implementation doesn’t support it.

Basically in another software that supports OSL you might get better luck, in blender I’m not sure OSL was improved since it’s first introduction. Maybe OSL on GPU will bring back interest and this might be added.
While being able to do some raycast in the shader already opens a lot of possibilities, now if we can retrieve other data it really broaden the possibilities…

1 Like

Thank you so much,

But it was all for nothing, I later saw that the trace function isn’t supported in optix yet, and retrieving shader parameters is still remains a mystery :joy:

so, I have taken it a step further and planned to add this as inbuilt feature and hence compiled the source.

3 Likes

Outch, yeah it’s sad that OSL gpu lack these features,
I was hoping that it would bring more interest to OSL and eventually we might see support for trace on GPU and even more being able to sample attributes with a trace.

I’m wondering, what are you trying to implement ? the depth fade node or building blocks to recreate it in cycles ? or you’re going to work on improving OSL implementation ?

1 Like

Question from someone who doesn’t know OSL well. Wouldn’t this mask be achievable with Geometry Nodes? Is there a benefit of doing that in Shader Editor?

1 Like

Disclaimer that I’m similarly only a dabbler in OSL…
It should be possible but you will be limited by vertex density. Remember that shaders are per-pixel whereas GN is per-vertex.
Thus a shader solution will work always but a GN solution will depend on the geometry and will need to have an equally complex (or potentially more complex) separate shader material to make it work.

2 Likes

Yeah @zeroskilz is right, for that particular task and given the meshes are decently subdivided that might work.

Last year I tried to trace shadows to get a BW mask I can use to mix two textures.
Could have been done with shader to RGB in eevee but I needed that in Cycles.

I turns out that the OSL version was much faster than the GN solution, and on top of that much cleaner because of the per pixel evaluation.

I would be awesome to have full OSL on the GPU, which doesn’t bring a lot more than node, except being able to trace rays / raycast, loops , and sometime it’s cleaner to write code than to wire a lot of nodes together…

3 Likes

I’ve just planned to create that node, I’m not quiet if I could do it, but the thing is I need to make blender be able to cast diffuse rays in the inverse incident direction.

coz I don’t think building only the node is possible, coz I have read in Blender shaders do not emit any kind of rays. they only are used to control how rays effect the objects.

1 Like

Indeed ! The only nodes that emit rays are AO node and Bevel node, maybe it’s possible to take inspiration from that and build a raycast node ?

1 Like

Cool, I didnt knew that, i’ll definitely check them AO and Bevel nodes, maybe i could simply build a node without messing around too much, thanks for that tip

1 Like

I’ve been thinking about this since I learned about this :smiley: How’s the progress?
Also, will you share OSL file? However far you got

1 Like

Glad you are interested but, I’m not getting enough time try to implement this functionality in the core blender. But sure I can share the OSL code, but it only works on CPU

#include "stdosl.h"

shader distance_mask(

float MaxDistance = 10.0,

float BendFactor = 1.5, // Default IOR

float AdditionalDistortion = 0, // Default distortion factor

output color depth_mask = color(1.0, 1.0, 1.0)

)

{

// Declare variables

point hit_point_background;

float dist_background;

// Calculate the refracted ray direction

vector refracted_direction = refract(I, -N, 1.0 / BendFactor);

// Apply additional distortion to the refracted direction

refracted_direction += normalize(N) * AdditionalDistortion;

// Trace the hit towards the background

if (trace(P, -refracted_direction, "maxdist", MaxDistance))

{

// Retrieve hit point information using getmessage()

getmessage("trace", "P", hit_point_background);

// Calculate distance to the background

dist_background = distance(hit_point_background, P);

// Normalize the distance

float normalized_dist = (dist_background / MaxDistance);

// Convert the normalized distance to a grayscale color

depth_mask = color(normalized_dist, normalized_dist, normalized_dist);

}

else

{

// If there is no hit, set to a default value (e.g., fully white)

depth_mask = color(1.0, 1.0, 1.0);

}

}

Is there any special reason for using OSL trace for this effect?

Wouldn’t a simple VolumeAbsortion node do this almost for free?

though we can get the depth using volume absorption, we don’t get the depth as a texture on the surface,

if we only had that we can use it to add fake volume effects like they do in unreal engine. just to improve performance and also see here https://www.youtube.com/watch?v=CyRksVjviwg&t=2s

and also can be used in water shaders and much more if used creatively