Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Render only visible part of object/mesh?

Discussion in 'General Graphics' started by Flavelius, Mar 30, 2017.

  1. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    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:

    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?
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    I thought about replacement shaders, but as i haven't used them before, i'm not sure how i'd do it.
     
  3. pixelChisel

    pixelChisel

    Joined:
    May 19, 2016
    Posts:
    17
    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:
     
    Flavelius likes this.
  4. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    Thanks, the second one looks like i can find what i need in it.
     
  5. Lost-in-the-Garden

    Lost-in-the-Garden

    Joined:
    Nov 18, 2015
    Posts:
    176
    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

    Code (CSharp):
    1. Shader "litg/Depth Write"
    2. {
    3.     Properties {
    4.     }
    5.  
    6.     SubShader
    7.     {
    8.         Tags
    9.         {
    10.             "IgnoreProjector" = "True"
    11.             "Queue" = "Geometry-100"
    12.         }
    13.  
    14.         Pass
    15.         {
    16.             Cull Off
    17.             ZWrite On
    18.             ColorMask 0
    19.             Lighting Off
    20.             Fog { Mode Off }
    21.            
    22.  
    23.             CGPROGRAM
    24.  
    25.             #pragma vertex vert
    26.             #pragma fragment frag
    27.  
    28.             #include "UnityCG.cginc"
    29.  
    30.             struct vertexData
    31.             {
    32.                 float4 vertex : POSITION;
    33.             };
    34.  
    35.             struct fragmentData
    36.             {
    37.                 float4 position : SV_POSITION;
    38.             };
    39.  
    40.             fragmentData vert(vertexData v)
    41.             {
    42.                 fragmentData o;
    43.                 o.position = mul(UNITY_MATRIX_MVP, v.vertex);
    44.                 return o;
    45.             }
    46.  
    47.             float4 frag(fragmentData i) : SV_Target
    48.             {
    49.                 return 1;
    50.             }
    51.  
    52.             ENDCG
    53.         }  
    54.     }
    55. }
    56.  
     
  6. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    Thanks for the idea, that sounds like something i could use.
     
  7. brokenm

    brokenm

    Joined:
    Aug 28, 2014
    Posts:
    18
  8. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    926
    perfect, thanks!