Search Unity

Reflection Camera rendering backfaces only

Discussion in 'Scripting' started by gallickgunner, Jun 24, 2017.

  1. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    So i am in the process of making my own water shader, writing it from scratch. Doing this for my own learning. I read the standard water script and i don't understand one thing.

    The standard script creates the clipping plane this way

    Code (CSharp):
    1. Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign)
    2.         {
    3.             Vector3 offsetPos = pos + normal * clipPlaneOffset;
    4.             Matrix4x4 m = cam.worldToCameraMatrix;
    5.             Vector3 cpos = m.MultiplyPoint(offsetPos);
    6.             Vector3 cnormal = m.MultiplyVector(normal).normalized * sideSign;
    7.             return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos, cnormal));
    8.         }
    I don't understand why they calculated the dot product? The 4th term is supposed to be the distance from the origin. If that's the case then shouldn't the distance be just the magnitude of "cpos" as the position vector would go from the origin to the center of the plane? Also, i don't understand why they negated it.

    I didn't follow the standard script and made the view matrix for the reflection matrix my own way ( defining the side, up and look at vectors as basis). And the result is good. However there is one small problem. I am getting this image.


    I don't know why the cube is getting distorted ;s The trees seem fine. The only difference between my and the standard water script is the way i created the reflection camera's view matrix.

    EDIT:- wait a sec, is this even distorted? or is the camera rendering the back faces only?
     
    Last edited: Jun 25, 2017
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Backfaces only from what i can tell ^^
     
    gallickgunner likes this.
  3. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    So i set Culling to Off in a custom shader and its giving the proper result. Anybody can tell me what's the problem? Why do i have to set it to off and why is it rendering backfaces only, shouldn't it render only the front faces by default?
     
  4. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    Bump. Anybody? Also as everybody know the reflection camera's up vector is in the opposite direction. If i set the up vector same as the original one, i don't get the upside down picture, however there is no culling problem as mentioned above. What has the up vector to do with the camera mixing up the front and backfaces? Sorry i can't understand
     
    Last edited: Jun 27, 2017
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    So, first of all, is the object a primitive one, so, standard box with right normals and stuff?
     
  6. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    yep it's a standard 3D cube. No fancy stuff. If the up vector of the reflection camera isn't flipped this is what i get.
     
  7. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    Bump anybody ;s
     
  8. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    BUMPPPPPP ;s
     
  9. BTCallahan

    BTCallahan

    Joined:
    Jun 6, 2015
    Posts:
    71
    Is Cull set to Back, Front, or Off?
     
  10. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    I also tried it with a standard shader and same result, so its supposed to be back.
    If i explicitly set it to Off or Front then it gives the correct result. Apparently it's messing up its back and front faces.
     
  11. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
  12. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    I just did a quick test with my water.
    I don't know if this is your problem, but at least i could reproduce it.

    Before and after rendering, culling is inverted in my code.
    Code (CSharp):
    1. bool oldCulling = GL.invertCulling;
    2. GL.invertCulling = !oldCulling;
    3. reflectionCamera.Render();
    4. GL.invertCulling = oldCulling;
    If i disable this, my camera's render result is inverted.
    Trees look fine because they have a double sided shader in my case.

    Maybe you are missing this switch?
     
    gallickgunner likes this.
  13. gallickgunner

    gallickgunner

    Joined:
    Jun 5, 2017
    Posts:
    16
    OMG, i think this is it. i couldn't understand this part from the original script so i didn't add it to mine.
     
  14. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    231
    Same here, getting all objects rendered into the reflection 'inside out'.

    But I don't know how to invert the culling in URP..

    Would it not be possible to flip the matrix and then read from the reflection texture upside-down as well? (It's the winding order that decides the culling, or am I mistaken?)

    Even this asset (standard water for URP) is wrong when I use it in Unity 2022

    EDIT: my linear algebra was too rusty to figure it out myself, but I found this little snippet and it works perfectly! (That is: flip the projection matrix to render upside down so that you don't need to invert culling, then just read the reflection RenderTexture upside-down in the water surface shader as well)
     
    Last edited: Dec 23, 2022
    AshwinMods and alexis_morin like this.