Search Unity

How to make these kind of effect? (from wireframe)

Discussion in 'Shaders' started by rpopic2, Jan 8, 2020.

  1. rpopic2

    rpopic2

    Joined:
    Apr 15, 2018
    Posts:
    15
    Like this wireframe gizmo in unity, it is transparent, but hides what's at back, like any other opaque objects.

    When I try to do this in unity, it hides the line even when that square is at back of the line
    And i've searched web for 4 days and couldn't find ...
    Please help.
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Render the object using two passes. First is a pass using a depth only shader. Then render again as wireframe.
     
  3. rpopic2

    rpopic2

    Joined:
    Apr 15, 2018
    Posts:
    15
    I tried doing that, but in order to check depth, zwrite should be on, but i need to render a transparent image instead of wireframes, which requires zwrite to be off. Or maybe i've mistaken your reply. It would be so much appreciated if you provide an example. Thanks a lot for replying.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Actually, you don't want to render a transparent image, you want to render no image. You want a shader that renders only to the depth buffer and nothing else.
    Code (csharp):
    1. Shader "Depth Only"
    2. {
    3.   SubShader {
    4.     Pass {
    5.       ColorMask 0 // Disables writes to the color buffer
    6.       ZWrite On // on by default, but added here for clarity
    7.  
    8.       CGPROGRAM
    9.       #pragma vertex vert
    10.       #pragma fragment frag
    11.       #include "UnityCG.cginc"
    12.  
    13.       float4 vert (float4 vertex : POSITION) : SV_POSITION
    14.       {
    15.         // bare minimum vertex shader, only outputs clip space position
    16.         return UnityObjectToClipPos(vertex);
    17.       }
    18.  
    19.       void frag() {} // fragment shader with no output or semantic target
    20.     }
    21.   }
    22. }
    Render your object with that pass, then again with either a wireframe shader, or with a black shader and GL.wireframe enabled.
     
  5. rpopic2

    rpopic2

    Joined:
    Apr 15, 2018
    Posts:
    15
    But my actual goal is to render transparent image instead of the wireframe. I found that adding a transparent pass doesn't work somehow. Maybe it is because of render queue?
    So I came up with idea of splitting gameobject into two separate object and it did work. But that makes everything complex. Any ideas? Thanks in advance.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    You need Unity to render the depth only pass for all objects, then the "normal" pass for them afterward. If you have two passes in a single shader Unity won't always render all of the objects with the depth pass first, but instead both passes for each object before rendering the next object.

    The only easy way to ensure that order is to use two materials with queues enforing that order. If your objects have only one material, you can add both materials to a renderer component instead of having to duplicate the object. Just set the materials array to 2 and assign both (the order in the array doesn't matter as long as the queue on the materials are setup properly). If an object has more than one material normally, then you will need to duplicate the object.