Search Unity

Calculating skybox in shader for world objects?

Discussion in 'Shaders' started by zerotech15, Nov 28, 2016.

  1. zerotech15

    zerotech15

    Joined:
    Jan 28, 2016
    Posts:
    94
    Hello guys,

    I'm trying to create a vertical fog effect with 2-color gradient skybox. The current implementation is based on calculating the world position of a fragment and lerping its color to a fog color. It isn't good enough since I need this effect to work with the gradient skybox. So basically I need to render a skybox in world shader. Is there any efficient way to calculate the color of the skybox for the specific fragment to achieve the smooth color blending?

    Current implementation:
    upload_2016-11-28_3-33-26.png

    Need something like that (edited in Photoshop)
    upload_2016-11-28_3-40-6.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Sure, especially if your skybox is as simple as a two color gradient. You just need to write a custom shader that does the same calculations as your skybox shader (which is likely a lerp based on the normalized world space view direction vector's Y) and use that for the fog color.

    Are you using a custom shader already, or a built in shader with the shadows turned off?
     
  3. zerotech15

    zerotech15

    Joined:
    Jan 28, 2016
    Posts:
    94
    Woah, thanks! View direction worked perfectly. Yes, I'm using a custom shader for this.
    upload_2016-11-28_4-38-26.png
     
  4. meebou

    meebou

    Joined:
    Mar 6, 2017
    Posts:
    46
    Hey, any code examples or tutorials how to achieve this effect? I am searching the whole internet...