0
\$\begingroup\$

Potentially a big question, but it seems like it should be simple enough. I have an object in my game composed of multiple parts (connected GameObjects with their own renderers). I want to make this entire object semi-transparent, without seeing the parts of the object through itself.

Here's an example of what I have so far: Transparent Object Test

There are two cubes, with another stretched cube connecting them. The sphere in the background is just to show an object in the background. Notice how you can see the ends of the connecting cube inside the other cubes. That's what I don't want. I only want the front-most faces of the overall "composite object" to be visible. I want inner faces of the object to be occluded, while keeping the object as a whole semi-transparent.

Turning on "depth write" almost sort of works, but it gives me this: Transparent Object Test with depth write enabled

I've searched for hours to try and figure out how to implement this. This is the closest thing I've found, but it's for URP, and I can't figure out how to translate it over to HDRP. I've run into talk about creating shaders, but at this point I've never touched shader design (on any platform), and can hardly begin to figure out how I would accomplish this.

I feel like I can't be the first person to have this problem in this setup. Can anyone outline for me how to get the transparency I need, specifically in Unity's High Definition Render Pipeline?

EDIT:

Trying the suggestion from:
Fade in rendering mode doesn't work as intended

I've duplicated the object and applied the following shader to one of the overlapping copies.

Shader "Unlit/DepthReserve"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue"="AlphaTest+1" }
        LOD 100

        Blend Zero One

        Pass
        {

        }
    }
}

This very nearly works, but it results in some strange glitching. Watch how the background object erratically pokes through the transparent one when I move the camera.

Glitchy background object

If there's a way to fix that, this method (as hacky a workaround as it is) could work for me.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Will the connected parts always be connected in the same way? Can they be combined into one mesh? \$\endgroup\$
    – Kevin
    Commented Nov 16, 2023 at 2:42
  • 1
    \$\begingroup\$ Does this answer your question? \$\endgroup\$
    – DMGregory
    Commented Nov 16, 2023 at 2:51
  • \$\begingroup\$ @Kevin No, the parts will move via joints \$\endgroup\$ Commented Nov 16, 2023 at 3:37
  • \$\begingroup\$ @DMGregory Not quite, but it has gotten me closer, I think. I've updated the question with my further attempts if you care to take another look. \$\endgroup\$ Commented Nov 16, 2023 at 4:40

1 Answer 1

0
\$\begingroup\$

Blend Zero One is equivalent to rendering nothing and just showing the background as the shader output is multiplied by zero and the frame buffer is multiplied by one.

However, using Alpha overrides the Blend mode. This is just a guess, but it could be there is some blend mode race condition because of this override.

What you want, is to render in the AlphaTest pass (which is front-to-back sorted with z-writes), with an Alpha Transparent Blend mode. Try this?

Shader "Unlit/DepthReserve"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "Queue" = "AlphaTest+1" "RenderType" = "Transparent" }
        LOD 100

        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {

        }
    }
}

This thread may be helpful:

https://forum.unity.com/threads/difference-between-alphatest-and-transparent-renderqueue.458750/

you can also add #pragma debug and look at the compiled shader

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .