r/shaders

Having a devil of a time trying to get my privacy glass shader to ignore foreground objects (shader file provided)
▲ 3 r/shaders+2 crossposts

Having a devil of a time trying to get my privacy glass shader to ignore foreground objects (shader file provided)

Using Unity version 6000.0.72f1, URP package version 17

Link to the shader file: https://drive.google.com/file/d/1DbuKtRRFbwcuorUZYInWtSWPIlsI6M-p/view?usp=drive_link

Screenshot of the graph for anyone who wants to take a quick look without downloading the shader:

https://preview.redd.it/1g2ksqmqvwwg1.png?width=1648&format=png&auto=webp&s=b862d7184482d62378bacc5d70918d0ccbf989e2

I've been having a blast trying to make this privacy window shader and I think it looks really cool in action, but I keep running into issues where foreground objects affect the shader's distortion and blur. Here's how it looks prior to adding any depth-testing.

https://preview.redd.it/elsu3neqrwwg1.png?width=518&format=png&auto=webp&s=33fac4625815bb6f120c292e448de0db106415ab

I've tried maybe 4 or 5 different ways of fixing this that I can think of, primarily by testing the distorted screen UVs against the original UVs, and it helps a little bit, but there are thin pixel outlines no matter what I do. Here's the latest implementation that works almost perfectly:

Only glass distortion:

https://preview.redd.it/ulu279zewwwg1.png?width=1163&format=png&auto=webp&s=3dcca496000d382ff9c7032ae94c94cc23ff9681

With blur and distortion applied:

https://preview.redd.it/a1uy236iwwwg1.png?width=1085&format=png&auto=webp&s=06b67f55651508fdfcaf7dc37134ee93d77b5a77

As you can see it removes about 95% of the foreground object's influence, but those last pixels of colour really detract from the presentation and it's driving me nuts that I can't figure out what's causing it to happen. As far as I can tell I'm correctly applying the depth logic, so where are these pixels coming from?

The box blur function is an HLSL custom function that needs to be explicitly made depth-aware (and to my knowledge, should be), and the no-blur pathway just routes through a Scene Color node to bypass adding any kind of blur, putting out to the Emissive colour fragment shader the distorted UV and a final glass tint colour.

Here's the box blur code for anyone who wants to inspect that, too:

#ifndef SHADERGRAPH_PREVIEW
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"

float3 sum = float3(0.0, 0.0, 0.0);

float2 offsets[4] = {
    float2(-1.0, -1.0),
    float2(1.0, -1.0),
    float2(-1.0, 1.0),
    float2(1.0, 1.0)
};

[unroll]
for(int i = 0; i < 4; i++) {
    float2 offsetUV = UV + (offsets[i] * BlurRadius);

    float rawDepth = SAMPLE_TEXTURE2D_X_LOD(_CameraDepthTexture, sampler_CameraDepthTexture, offsetUV, 0).r;
    float sampleDepth = LinearEyeDepth(rawDepth, _ZBufferParams);

    if (sampleDepth < FragmentDepth) {
        offsetUV = UV;
    }

    sum += SAMPLE_TEXTURE2D_X_LOD(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, offsetUV, 0).rgb;
}

Out = sum * 0.25;

#else
Out = float3(0.0, 0.0, 0.0);
#endif

Any help or improvements would be greatly appreciated. Thanks!

Also let me know if any more context or disambiguation is needed.

Anyone who fixes the shader (and shares the fix here!) is 100% welcome to use it for themselves. I just want the darn thing working.

Also bonus points if anyone can also advise on rendering transparent materials through this shader too, currently it occludes any transparencies behind it.

reddit.com
u/DVXC — 1 hour ago
▲ 1 r/shaders+1 crossposts

Raymarcher has black artifacting?

I recently programmed a raymarcher in shadertoy, but where the rays are at sharp angles, there's black artifacts. You can kind of see it in the picture on the right side where the sphere meets the cube. Here's the shadertoy link: https://www.shadertoy.com/view/fc2XWW

u/pizza-goblin_9000 — 1 day ago