Search Unity

Slice of scene geometry to Texture

Discussion in 'Shaders' started by rostik1975, Jun 6, 2019.

  1. rostik1975

    rostik1975

    Joined:
    May 14, 2015
    Posts:
    44
    Hi,

    Is it possible to make a 'slice' of all scene objects to a texture?
    When I use a top-down ortho camera with near clip set up to some required distance, it does render the slice, but with holes, while I need these holes to be 'capped', to get the rendered texture with a fill, not just outlines of the meshes.

    One possible solution is to set a Clipping shader (https://www.ronja-tutorials.com/2018/08/06/plane-clipping.html) to ALL scene objects (including a Terrain converted to mesh), and then make a render of the slice, but this sounds weird.

    More desired solution is to use a shader for a horizontal plane that counts all intersections with all scene meshes, and fills these intersections with solid color. But I am not sure the shader script could perform these calculations. Could you please help with advice?

    Thank you.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Clipping shaders show a "fill color" by making the back faces of the geometry being rendered a solid color. This works for any closed surfaces, like a sphere, box, or that monkey head, but not on anything like terrain. Though depending on the setup, having the camera use a solid color background might be "good enough" for terrain unless you wanted to place extra geometry below terrain.
     
  3. rostik1975

    rostik1975

    Joined:
    May 14, 2015
    Posts:
    44
    Thank you for your reply. Unfortunately this is true only if the backfaces are available by camera.
    In my case I want to achieve an effect of duckweed distribution on water for my 'swamp scene'.
    None of existing water foam shaders helped me because all they use cameraDepth which is relative to the camera view, not to the real water depth. You can see on the image that duckweed is placed on a wrong place (2) where underwater object is.

    So I decided to render a 2D texture for the whole level to use as a distribution map for the duckweed in a shader combining with min and max weed distance. What I need is just a water/non water texture for the given Y-coordinate (the water surface), that's why I set narrow range for the camera clip.
    I've tried the mentioned clipping shader and the result is still not ok.

    One of possible solutions is just to paint the fills and small objects on the map in Photoshop manually, or to use a shader that could realtime inspect the water plane to find mesh intersections, to place duckweed around them. The second solution is more desired that's why i asked if this is possible.

    duckweed.jpg no_backfaces.gif rendered_surface_map.gif
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Yep. That is indeed the problem.

    Your best bet would be to render the scene using a replacement shader pass that is a solid black with culling off, or rendering out pre-scaled linear depth, scaled to the range you want the duckweed to be visible. Use an orthographic camera with a white clear color, and a distant far plane so it captures everything beyond the near clipping plane, which I'd have on or just barely above the water surface. I would then do a few blur / expansion passes on top of that so the hard white edges bleed out.

    With a bit of messing around you could get something like this:
    upload_2019-6-6_13-46-46.png
     
  5. rostik1975

    rostik1975

    Joined:
    May 14, 2015
    Posts:
    44
    Thank you very much. I've modified the shader to distribute duckweed (foam) in accordance to pre-rendered texture, and added some noise and wrote a script that setPixels on the texture to display disappearing trails on the water surface.
     

    Attached Files: