Search Unity

Shader shows objects from "inside out"

Discussion in 'Shaders' started by x0RRY, Feb 22, 2021.

  1. x0RRY

    x0RRY

    Joined:
    Oct 29, 2019
    Posts:
    5
    Hi everyone,

    I'm very inexperienced when it comes to shaders, so please bear with me.

    For an academic project, I inherited a Unity 4.2.0 Professional Project. The scene makes use of a custom shader, which implements a spherical projection for the camera to make the size and shape of opbjects invariant to the position in the camera.

    To modernize and share things with other people, we upgraded to Unity 2019.2.17f1. Everything works fine except the Shader. I even tried the freely available non-professional version of Unity 4.2.0 but I still get the same results which are as follows:

    All objects are flipped on the head and drawn "inside out". The only reasonable guess I have would be that the normals of the vertices might somehow be flipped or some other change in the coordinate system will cause the shader to mess up this bad.

    I hope I described the issue good enough for maybe a few ideas? Any help would greatly be appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Unity 4 predates Unity adding reversed Z depth rendering. Technically from Unity 5 on everything is rendered flipped "inside out" from how Unity 4 rendered, but most of the time the shader's render state is modified at runtime to correct for this because OpenGL still renders the original way. Reversed Z depth doesn't have any benefits for OpenGL, so they didn't do it for that API.

    My guess is this specific shader has expectations that are no longer true when rendering with reversed Z depth. It could be it's modifying the vertex shader's output clip position directly, which will behave very different. Unity's runtime modifications don't touch the actual HLSL shader code, so it won't fix that.
     
    x0RRY likes this.
  3. x0RRY

    x0RRY

    Joined:
    Oct 29, 2019
    Posts:
    5
    Thanks a lot! This sounds like it might indeed be the issue, although it won't really explain why it also breaks in the non-professional Version of Unity 4.2.0?

    How should I approach this Problem? If I understand your post correctly, I will have to fix this in the shader and not in Unity? Do you maybe have any Resources on the topic of reversed Z depth rendering and how to compensate it in a shader? The shader itself is quite small so I think it could be reasonable for me to fix it.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    In Unity 4 there were some rendering features that weren't available to non-pro versions. Profiler, Asset Bundles, Plugins, Linear Rendering, and Render Textures to name a few. Those last two being possible reasons why it breaks in non-pro Unity 4. Render textures are a requirement for doing any kind of post process effect, which is what it sounds like you're trying to deal with. Though if it breaks in the exact same way as it did in Unity 2019 with it being inside out, then I have no idea since neither of those should cause that.
     
  5. x0RRY

    x0RRY

    Joined:
    Oct 29, 2019
    Posts:
    5
    With your help I was able to fix it in Unity 2019 (which is all that matters) by reversing the z buffer in the shader by using
    Code (CSharp):
    1. z = 1.0f - z;
    Thanks a lot! Now, all that remains are a few lighting issues which I have to check in detail.

    By the way, the mentioned solution does not work in the non-pro Unity 4, even though the "inside out" effect is exactly the same. Doesn't matter though, as I'll be able to work with Unity 2019 from now on...
     
    bgolus likes this.