Search Unity

Custom Shader on Skybox

Discussion in 'Shaders' started by eneroth3, Nov 14, 2018.

  1. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Hi all,

    I've created a custom vertex+fragment shader to use as sky. Originally I intended to use it at the interior side of windows to make it look like a hole out, even if I had other rooms behind the window.

    Today I tried to also apply it to the Skybox. While the sky itself looks quite good and fits my art style, this made the shadows go haywire. The shadows are extremely dark and extremely sharp.

    Unfortunately I can't find much information about custom shaders for the sky. Does anyone know what determines the shadows? Is my blue sky too dark? Or is the sun maybe too bright? Or are is Unity directly accessing other properties of the shader than its visual output?

    Thanks

    2018-11-14_20h41_32.png 2018-11-14_20h40_54.png 2018-11-14_20h41_24.png 2018-11-14_20h55_28.png
     
  2. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Maybe I've posted this in the wrong sub-forum, as its more related to lighting than shaders.

    Anyhow, it would be good to get a reply; I'm kind of stuck here and there doesn't seem to be any relevant documentation.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    If you took your existing shader and just applied it to the skybox property, it's not going to work properly. There's a bit of magic in Unity's skybox handling in that the shader needs to have the word "Skybox" in the name, as well as use "PreviewType"="Skybox" in the tags.
     
  4. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Do you know what this magic is? From what I've found changing the name to include Skybox or change the preview should remove the warning saying this isn't a skybox shader. However I haven't found anything saying it affects lighting in the scene. It would also be very useful if I could have the same material as sky outdoors, and on the windows indoors. Duplicating the shader means I have to update in two places if I make some change to how the sky is rendered.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Actually, I think the "Skybox" string isn't the problem here. The problem is your skybox shader cares about the view direction.

    The ambient lighting is generated by sampling the default reflection probe. The default reflection probe is a cubemap. A cubemap is generated by rendering the scene with a square 90 degree view in the 6 world axis directions. When rendering those, it's setting the UNITY_MATRIX_V matrix ... and your shader doesn't like when the camera is facing perfectly up or perfectly down as it's doing a dot product using the forward direction extracted from that matrix. When it's looking straight up or down, the 2D view vector is (0,0) which normalizes to a NAN. (edit)

    Here's a default sphere and a chrome sphere side by side with your sky shader from the other thread:
    upload_2018-11-15_12-11-42.png

    The ambient light is a spherical harmonic, so it handles having a top and bottom of different colors relatively well, but it's not good at representing only a horizon. Having a black top and bottom to the reflection probe means the produced SH is also going to be mostly black, if not entirely so.

    Here's the same two spheres with the view direction set by a material property (set to (0,0,1)):
    upload_2018-11-15_12-11-6.png
     
    Last edited: Nov 15, 2018
    eneroth3 likes this.
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    I'd actually recommend you set the Environment Lighting manually, using a solid color rather than the skybox. Updating the ambient lighting in real time from a dynamic skybox is a relatively expensive thing, and isn't something Unity does without some script side pestering. Even more so for the Environment Reflections (default reflection probe), I'd disable that entirely (set to Custom and leave as None). Plus since Unity's environment lighting and reflections always assume Y is the vertical axis, that's going to complicate things for your game.
     
  7. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Thanks! Again you save the day!

    I have seen rare black flickering on my windows in my interior scenes when the camera points upwards, but never connected the two. The flickering windows also seemed to be affected by some floating point imprecision, maybe related to my portals transforming the cameras, as not all windows flickered synchronized.

    If I add the camera forward vector with either camera up or down (depending on elevation) before projecting to the horizontal plane and normalizing, I could probably get rid of the flickering when looking straight up or down.

    It would however create some seams in skybox, as the top and bottom would depend on the totally arbitrary up direction of their camera. Using a solid color seems to be a good alternative here.
     
  8. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Here's the updated shader if anyone find the trick for getting vertical rotation, even when looking straight up or down, useful.

    Code readability has decreased a bit though but I think I'll have to look into it some other day, as it's getting rather late here. Write drunk (=tired); edit sober (=well rested) :p .
     

    Attached Files: