Render only visible part of object/mesh?

Hi,
i’m looking for a way to render only the visible part of a mesh in the scene (occluded by other objects) (ideally without multiple cameras) to a rendertexture.
Like this:

3013399--224879--rendervisible.jpg

where i only need the red part (everything else should not be rendered).
Is there any techique i can use to do that?

I thought about replacement shaders, but as i haven’t used them before, i’m not sure how i’d do it.

There’s a youtube channel called “Making stuff look good in video games”. You’ll probably find your answer in those two videos:

Intro to replacement shaders:

Replacement shaders + stencil buffer:

1 Like

Thanks, the second one looks like i can find what i need in it.

you can render the occluders only to the depth buffer without writing to the color buffer and then render the object you want to see.

Here’s the shader. The important bits are ZWrite = on and ColorMask = 0

Shader "litg/Depth Write"
{
    Properties {
    }

    SubShader
    {
        Tags
        {
            "IgnoreProjector" = "True"
            "Queue" = "Geometry-100"
        }

        Pass
        {
            Cull Off
            ZWrite On
            ColorMask 0
            Lighting Off
            Fog { Mode Off }
           

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct vertexData
            {
                float4 vertex : POSITION;
            };

            struct fragmentData
            {
                float4 position : SV_POSITION;
            };

            fragmentData vert(vertexData v)
            {
                fragmentData o;
                o.position = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }

            float4 frag(fragmentData i) : SV_Target
            {
                return 1;
            }

            ENDCG
        }   
    }
}

Thanks for the idea, that sounds like something i could use.

Stencil is what you need Unity - Manual: ShaderLab command: Stencil see example

perfect, thanks!