Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Turn transparent when behind wall, how to do this ?

Discussion in 'Shaders' started by windmaomao, Dec 4, 2009.

  1. windmaomao

    windmaomao

    Joined:
    Jun 11, 2009
    Posts:
    50
    This might sounds newbie description, but I don't know if this is just the shader thing.

    Ok, I have an player and a wall, when the player is in front of the wall, it's just regular shading. And when the player goes behind the wall, the ghost shape of the player is projected on the wall. I saw this effect in couple of games, the latest one is Torchlight.

    Just curious if anybody knows how this can be achieved by shader. thanks.

    Fang
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    I made this a while ago.

    Code (csharp):
    1. Shader "FX/VertexLit ShowThrough" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _OccludeColor ("Occlusion Color", Color) = (0,0,1,1)
    6.     }
    7.     SubShader {
    8.         Tags {"Queue"="Geometry+5"}
    9.         // occluded pass
    10.         Pass {
    11.             ZWrite Off
    12.             Blend One Zero
    13.             ZTest Greater
    14.             Color [_OccludeColor]
    15.         }
    16.         // Vertex lights
    17.         Pass {
    18.             Tags {"LightMode" = "Vertex"}
    19.             ZWrite On
    20.             Lighting On
    21.             SeperateSpecular On
    22.             Material {
    23.                 Diffuse [_Color]
    24.                 Ambient [_Color]
    25.                 // Emission [_PPLAmbient]
    26.             }
    27.             SetTexture [_MainTex] {
    28.                 ConstantColor [_Color]
    29.                 Combine texture * primary DOUBLE, texture * constant
    30.             }
    31.         }
    32.     }
    33.     FallBack "Diffuse", 1
    34. }
     
  3. windmaomao

    windmaomao

    Joined:
    Jun 11, 2009
    Posts:
    50
    hi Daniel,

    thank you very much. Another newbie question, is this supposed to be applied to the wall, right ? And how do I make the switch from show-through to no show-through? I actually just start to read the shaderlib manual.

    Fang
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    That shader goes on things that you want to appear through walls. The trick is that it has two Pass blocks, one of which uses ZTest LEqual to only draw in front of things, and one which uses ZTest Greater to only draw when it is behind things. Thus for every pixel, the model will be drawn using one pass or the other, but never both. The Queue = Geometry+5 tag makes sure that objects with this shader wait until everything else is drawn so that they can decide whether they are behind or in front of something.
     
  5. windmaomao

    windmaomao

    Joined:
    Jun 11, 2009
    Posts:
    50
    oh, man, this sounds exactly I wanted, I'll test it tonight. thanks you very much Daniel, you made the task trivial now :)
     
  6. windmaomao

    windmaomao

    Joined:
    Jun 11, 2009
    Posts:
    50
    Thanks Daniel, I just tested, it works perfect, when the object under shader is behind something, it's plotted using a blue color. Awesome. I'm sure this shader can turn into other kool stuff as well.
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    Daniel, that's really cool. Thanks for giving me ideas!

    Do you have any method to turn objects transparent only when certain meshes go behind them? (e.g. the player character)? I really like this effect in games. Seems like a much more complicated situation, but I've seen it a lot, so perhaps there is a common solution?
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,356
    You mean turning a wall transparent when it blocks the camera's view of the player? That can be done with a script that casts a ray at the player and modifies the materials of whatever it hits. I believe there was a script in the Goober demo from way back when that did that. I'm not sure if there's an equivalent script in the Lerpz tutorial.
     
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
    I figured that was how it needed to be done. But I hadn't thought of what you posted in this thread, so I didn't know if there was some shader trick I didn't know about, to "automate" this.
     
  10. rylmtn

    rylmtn

    Joined:
    Nov 18, 2017
    Posts:
    1
    I'm new to unity and trying to use this code but I keep getting and error on the first lane saying 'Shader' unexpected symbol. Any tips on how to fix this? or am I just creating the shader wrong?
     
  11. Balikk

    Balikk

    Joined:
    Oct 23, 2014
    Posts:
    49
    Hi @rylmtn ,

    I think you made a little mistake. You created a " C# " script and not a " shader => standard surface shader " to paste this code.
     
  12. ediit

    ediit

    Joined:
    Mar 17, 2018
    Posts:
    4
    I'm trying to use this shader for an object lit by light probes and the model appears black. Does anyone know how to integrate light probe info into this?
     
  13. blafed

    blafed

    Joined:
    Sep 24, 2015
    Posts:
    3
    good solution but i want to make show only outline without filling
     
unityunity