Search Unity

transparency depending on depth buffer

Discussion in 'Shaders' started by touming, Apr 22, 2008.

  1. touming

    touming

    Joined:
    Apr 19, 2008
    Posts:
    14
    hi community,

    i want to write a shader, wich uses the current depth buffer values as tranparency values.
    i would also like to use a threshold value, to be able to clamp certain values.
    Is something like that already out there, or do you have any idea how to do this? it doesn't sound really fancy but i have not much experience with the shader language in unity...

    thanks very much
    tommy
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Generally reading the depth buffer is not possible in shaders (it's only possible in DirectX10, which requires Vista plus a DX10 hardware plus a DX10-enabled application).

    What do you actually want to do?
     
  3. touming

    touming

    Joined:
    Apr 19, 2008
    Posts:
    14
    actually what i want to do is to render a cube partially transparent.
    the parts near the viewpoint should be more transparent then parts far away from the viewpoint.
    such that it creates an illusion of looking inside the cube, no matter from wich side you look at it.
    i thought the way to realize this is to use the depth values as alpha values, so you get always a smooth transition.
    is it really only possible with DX10?

    thanks
    tommy
     
  4. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    isn't that like a Fresnel effect? couldn't get away with doing it with a gradient like the water shader does?

    No i don't how to do it! :wink:

    cheers.
     
  5. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    What you want to do is get the depth in the vertex shader by measuring the distance from the vertex to the camera (make sure you are in the correct vector space first). Then you can adjust those values so that they equal 0 at the front of a cube and 1 at the back, and feed that into the alpha somehow. The simplest way to do this will probably be with a helper script that passes some constants to the shader every frame.

    But you still have the ever present alpha-depth problem (transparent objects can't write to the depth buffer) and thus you will need to perhaps make each cube face a different object so that sorting can handle the depth for you.
     
  6. gjoel

    gjoel

    Joined:
    Apr 21, 2009
    Posts:
    9
    So how is depth-buffer access currently looking? :)

    I am looking to do a soft-particles like effect (ie. rendering transparent geometry on top of an opaque scene, fading out clipping) - but I can't figure out how to access the scene depth-values.

    I am aware of the dx10-problem (which apparently extends to opengl for unity), but solutions to circumvent this exists - e.g. rendering depth to the alpha-channel of opaque objects (and making that available as a texture in the transparency pass), or using MRTs, or doing an entirely different render-pass to output depth.

    Anyway, I would love to know if this is now possible :) It seems I could use the same solution as shown to do soft-particles using shader replacement, which essentially is the separate depth-pass approach.
     
  7. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes, for now (and for majority of hardware for the foreseeable future) a separate rendering pass to get the depth is required.