Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Shadows on Vertex Displacement Shaders?

Discussion in 'Shaders' started by HellsPlumber, Feb 22, 2019.

  1. HellsPlumber

    HellsPlumber

    Joined:
    Aug 17, 2014
    Posts:
    33
    Hi!
    For our game, I've created a vertex displacement surface shader for a "curved world" look.

    Sadly, this causes issues with shadows as they're only added once the vertex displacement takes effect, rather than before. I've shown the issue here: https://i.gyazo.com/3382d5044bb0ed5800060182e35e798f.mp4

    In desperation I wrote a C# script to disable the curved shader (via shader keywords) on the camera's PreRender function, then re-enabled it on PostRender but sadly this didn't work.

    I'm fairly inexperienced with writing shaders, so I was hoping someone would be able to help.
    Is there a way to apply shadows to a shader BEFORE vertex displacement takes place?

    Thanks for any help/advice!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    8,685
    Yes and no.

    On PC the on screen position is also the shadow position, and there's no way to separate the two from within the object's shader due to how Unity does directional light shadows. Specifically, Unity generates a depth texture from the camera's point of view, then uses that depth texture to calculate the main directional light's shadows for each pixel on screen. And the camera depth texture and shadow maps use the same shader. You'd have to do some manual hacks to a shadow caster pass (the one used by both) to detect if it's the screen space depth or the shadow map depth and only bend for the screen space depth. You'd then also have to modify the built-in screen space shadows shader (there's an override option in graphics settings) to calculate the pre-bent position from a post-bent world position.

    For mobile it's slightly easier. But not something that can be done with a surface shader. You'd have to modify the code that calculates the shadow map position to not apply the bend before sampling the shadow map. You may also need the same hack to not bend the shadow caster pass when being used to render a shadow map.
     
  3. HellsPlumber

    HellsPlumber

    Joined:
    Aug 17, 2014
    Posts:
    33
    Thanks for the help.
    Seems my best option is to change the lighting to move horizontally across the sky so shadows are only on level parts of the geometry.

    I thought the Ambient Occlusion post processing effect might help make up for the lack of proper shadows, but it seems to do the opposite of what shadows do!

    But sadly the ambient occlusion appear in the un-curved shape, as if it takes place before the curved shader does, so we just end up with ghostly buildings overlaying the curved ones.
     
unityunity