Search Unity

Depth-Based Translucency?

Discussion in 'Shaders' started by ReadyPlayGames, Apr 27, 2015.

  1. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    I am trying to create a shader that has translucency, but the amount of light that passes through depends on how "thick" the object is, or how far the light would have to travel.

    The paper that I read (http://http.developer.nvidia.com/GPUGems/gpugems_ch16.html) had the camera render the depth from the light into a texture, then reference that texture to find the distance the light would travel. However, since I don't know how many lights are in a scene at a given time, I can't very well do that (or can I? I don't know!).

    My alternative idea was to "trick" it by rendering the depth of the scene into a depth texture, but with culling set to "front." My reasoning was that we could find the differences in depths, and at least "approximate" the distance the light would have traveled through. It's not ideal, but it's all I have.

    I can't get SetReplacementShader (http://docs.unity3d.com/ScriptReference/Camera.SetReplacementShader.html) to actually replace anything on my camera. I set up a secondary camera as a child of the main, and applied this script to it: http://pastie.org/private/bezkrrcm4b11vbobvrjfqw. I put a renderTexture in the "Target Texture" for the camera, but nothing is being written there.

    What am I doing wrong? Am I even going about this the right way? Am I crazy?
     
  2. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Actually what that paper is talking about is the BASICS on how we do shadows nowadays!
    And usually you limit it to the directional light. (note, singular), as it consumes quite a bit of performance.
    Now however for your case I'd suggest that you simply paint your translucency texture based on how thick the material is there.
    If you Really want to go towards perfection based on the lights directions you could use Unity shader replacment to render ONLY the translucent objects, as that shouldn't be to heavy on performance. Even for 10+ lights.
     
  3. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    What do you mean by "how thick the material is?" Do I need to have a "thickness" variable in the shader? I'm confused.
     
  4. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    No thickness in the shader, just another map that would need to be painted/baked to get an idea of how much translucency to use. It's the normal way of doing translucent objects.
    Your, (Awesome I might add), solution would not require this in exchange for complexity and performance.
     
  5. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
  6. kode80

    kode80

    Joined:
    Aug 1, 2013
    Posts:
    151
    Check out my SSR article, I'm doing exactly this to calculate geometry depth.
    http://www.kode80.com/blog/2015/03/11/screen-space-reflections-in-unity-5/index.html
     
  7. antislash

    antislash

    Joined:
    Apr 23, 2015
    Posts:
    646
    Hi Kopde80
    i am also intrested in translucency,
    i sas wondering if your method can compute depth for non convex objects (eg human ears, that is the most common use for it)

    Thanks

    victor
     
  8. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    Kode80,
    I see that you use two depth textures. I'm wondering if I could get by with just one (the front culled one) and use the COMPUTE_EYEDEPTH macro for my shader. I'd like to make my shader as freestanding as possible. I have been unable to get COMPUTE_EYEDEPTH to work right though.
     
  9. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    Alright never mind, I got it working by using COMPUTE_DEPTH_01 instead