Help with custom refraction material

Hi, I’m trying to create my own material that acts like a refractive surface (think frosted glass)

I’m currently taking the scene colour and feeding it some UV’s. I’m lerping between 2 UV’s, the unaltered screen position and the screen position + the red and green components from a normal map. This lerp is based on the normalized scene depth, so things close to the camera use more of the unaltered UV, and things far away are more distorted. It works surprisingly well for what I’m doing (not distorting the player’s arms when they are in front of the refractive object), however it’s not perfect.

What would be really useful to me is a function that returns 0 if it’s in front of the material, and 1 if it’s behind. Does such a thing exist / how might I be able to create it?

Thanks in advance.

Can you post a picture of the result so far? I think a node to transform screen to world space might be useful.

Here you go


Maybe create a material function with an if/else node that is connected to two checks, one to get scene depth of refractive object and one to get scene depth of target object, and compare those depths. If depth1 (refractive) > depth2 (target), then output 0 (meaning the target, or nearby, object is in front of the refractive material object. If depth1 < depth2, then output 1. It’s an If node in the material editor, not if/else…and has an = input also…however, it’s probably not the entire workup.

Thanks, I’ll try it out

It did do what I originally wanted, but it turns out that it wasn’t the best for the effect I was trying to achieve. I ended up subtracting the distance from the camera to the refractive object from the scene depth before the divide, and that removed my artefacts :slight_smile:

I forgot about subtraction lol. I’m a noob too. Probably more of a noob than you at that sort of effect, especially.