Make object transparent using compositing?

I want to achieve something like this:

Rather than this:

In the first image, the soldier is actually fully opaque, but the result alpha of the whole soldier render is multiplied by a factor. In the second one, the object is using the standard alpha channel for transparency with Z sorting, which causes it to be more or less transparent in some places due to faces overlapping. I don’t want that.

Is this possible to do with a shader?

I suppose it would be possible using multiple cameras and render textures, but that seems a bit long-winded. Also I’m trying to accomplish this in Unity Free, which does not support render textures.

Any ideas?

Thanks.

Searching text isn’t effective to get an answer for this type of thing, I think. I looked at the Google image results for “unity render transparent object”, and clicked on an illustration of the problem. I bet there’s a similar search term that would have been more “you” before I influenced your thinking.

1 Like

Thanks @Jessy .

Shader "Transparent/Diffuse ZWrite" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
}
SubShader {
    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    LOD 200

    // extra pass that renders to depth buffer only
    Pass {
        ZWrite On
        ColorMask 0
    }

    // paste in forward rendering passes from Transparent/Diffuse
    UsePass "Transparent/Diffuse/FORWARD"
}
Fallback "Transparent/VertexLit"
}

Where that extra Zwrite pass at line 11 is the actual solution.

I wonder if there’s a way to set a thing like this up in Shader Forge. From what I gathered, Shader Forge does not allow setting up manual passes. That’s a shame. I suppose I could design the shader like I want in shader forge and hack the last bit (edit: just tried it and works fine).

2 Likes

Glad you found it. I was about to roll-eyes, sigh and link the documentation :wink:
Everyone will find 9 out of 10 solutions are solved with a bit of google magic!

1 Like

Haha, thanks. Not sure what I googled. While finding plenty of texts stating self-intersecting geometry is a real pain for transparent objects and one needs to sort triangles in code and so on, I didn’t find this simple solution. Exactly what I was looking for.

I guess part of google fu is searching for the term you should be searching for.