Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Unity 5 - Shader to "dissolve" mesh edges?

Discussion in 'Shaders' started by odival, Aug 11, 2015.

  1. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    I'm not a coder (much less a shader coder) thus I'm not sure this is the best place to post this thread, so bear with me:
    I need some help. In my Unity5 game I'm using two cameras, First camera renders the actual terrain and game objects (wich has a far plane of a 1000 units) and the Second camera renders a 3d skybox of sorts, a miniature of the actual terrain (it's far plane is 3200 units, but it covers the entirety of the miniature). With this setup I'm able to fake the illusion of having a very far horizon, as in the image below:
    *
    However, the 1st camera's far plane edge is very harsh, and doesn't blend well with the 2nd camera's background. Is there anyway to blur this edge, so it trasition seamlessly with the backdrop, as in the image below (a mockup I made in photoshop)?

    Any help is apreciated! =)

    *The blue line on the left image is just to outline the boundaries of the two cameras, it's not like this in the actual game.
    EDIT: Made a better explanation of the problem, and what I need.
     
    Last edited: Aug 12, 2015
  2. Marco-Sperling

    Marco-Sperling

    Joined:
    Mar 5, 2012
    Posts:
    565
    Have you tried using fog to achieve similar results? With an additional depth of field blur image effect on your camera maybe?
     
  3. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Thx for the tip, but I'm afraid It doesn't work.The depth of field also blurred the 2nd camera's background. The fog just give a nice laser outline effect, but it's not what I need.
    I need a solution to blur or dissolve just the 1st camera far plane edge. =(

     
  4. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    You're most likely going to need to make use of the alpha channel and set up some form of transparency to achieve this. You also won't be able to optimize it with stencil as at the dissolve point, both cameras need to draw.

    If you're looking for a way to patch up terrain between tiles of different resolution, the simplest way is to edit the edge vertices of the higher resolution tile to match up with the position of vertices of its neighboring, lower resolution tile by using linear interpolation. No need for any fancy stitching involving changes in topology :)
     
  5. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Sorry, I'm a noob regarding shaders. So, this use of the alpha channel for transparency would be done into a shader right? Would you know any shader that does anything even remotely ressembling what I asked (blurring the far plane of a camera view) so that I could study it as example?
     
  6. Dolkar

    Dolkar

    Joined:
    Jun 8, 2013
    Posts:
    576
    Hmm... what shaders are you using on the two terrains? The Unity terrain shader is quite complex and it's definitely not easy to modify them to do what you want. If any of them use the default, standard shader, it would make things much easier. You could also be able to customize the depth of field shader to only blur the small depth range of the boundary, but because you're using different depth ranges for the two cameras, the depth is not continuous and I think it wouldn't work so well.

    You really did stumble upon a difficult problem, didn't you? I can't see any easy solutions here... Fading while rendering involves customizing the base shaders and blurring in post process requires proper depth information.
     
  7. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,183
    This isn't a terrible idea. It's used in shadow of the colossus and far cry 3.

    The idea is that the futher distance is a cubemap rendered every so often (a reflection probe in Unity is ideal for this) and most of the time only a skybox or low poly proxy needs rendering. In this case it might be preferable to render the far distance in post, and feather it back in using the depth from the first pass. This way, you can control exactly how it blends.

    This approach does have it's flaws though, for example you would need to do this before any transparent passes - ie command buffer might help, or render your transparent stuff after the composite pass.

    It's not a good idea to use a shader to feather between them on the actual terrain because it won't look as good as sampling depth.
     
  8. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Hi Dolkar, I'm using standard shader for both terrains. I saw a lot about transparency shaders based on distance, and custom terrain shaders, but it's all way above my head. I might be wrong but since the DOF blurs both cameras, I believe that a custom terrain shader would be the way to go. What you think? Would it be possible to make a DOF effect that only blurries an especific area at specific distance around the player?


    Sorry hippocoder, but I'm struggling to understand this concept. So there would be only one camera that, from a certain distance onwards would only render the cubemap? Sorry if I'm completely wrong... Could you provide some links about this technique?

    EDIT: Another idea: Would it be possible to script a "transparent fog", that would render everything inside it transparent? This way instead of a weird fog line at the boundary of the far plane, it would give the illusion that the border is fading.

    Thanks guys =)
     
    Last edited: Aug 12, 2015
  9. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    Here's my idea that is about as simple as I can imagine.. not performance efficient, compared to other ways I can think of, but easier.

    Take a second camera, and use it to render to texture. Use a replacement shader, with the exact same far clip plane settings as the main camera (though you could definitely bring the near clip up quite a bit for performance). Output black values into the render texture. As it approaches the far clip plane, start outputting white or greyscale colors up to white. Output depth into the alpha channel, or if you just use white (no greyscale) then it's no big deal.

    Take a post processing blur, and have it blur only the areas where white pixels are, or for a fancier approach, blur based on the level of white. You'll need to be careful here to only blur pixels that have a depth equivalent to what was written, or what is farther than the white level. It would probably be best to use a command buffer, render the terrain early in the queue, render the blur, then render all other objects, which would mean you do not have to do anything with depth.

    Not exactly a hugely simple process.. But this would be the approach I take for simplicity. Obviously not tested.. it would not blend over into the skybox much, but would rather blend the edges of the terrain into the skybox.
     
  10. odival

    odival

    Joined:
    Jun 11, 2014
    Posts:
    57
    Unfortunately I do not have the skill to meddle with those solutions atm. However, after tweking the 3D skybox miniature terrain and a few other things (mostly concearning the sync between both cameras) I was able to get a reasonable transition between both terrains (see gap by the yellow arrow).
    My biggest problem will probably be with the props between both terrains (as pointed by the red arrow). I'm not yet sure how I should design forests and the likes to alleviate the discrepancy, but I guess it's better than nothing, right? And the fps cost is not much.

     
  11. DragonRider

    DragonRider

    Joined:
    Jul 19, 2012
    Posts:
    9
    Check out
    https://github.com/keijiro/FadeToSkybox
    It seems to do what you're asking, but with a static cubemap as the background. The shader could probably be modified to use a Reflection Probe instead of a Cubemap.
    The script uses Unity's built-in fog settings to control the strength of the transition, so it's fairly customizable.
     
    odival likes this.