Search Unity

Flip and/or Mirror Camera not working as expected

Discussion in 'Scripting' started by hughda, Jul 24, 2019.

  1. hughda

    hughda

    Joined:
    Jul 11, 2017
    Posts:
    7
    Hi Dudes,

    I'm kind of stuck with this one. I used this example https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnPreCull.html to invert one of multiple cameras in my scene As long as [bool] flip and [bool] mirror are equal, my camera is inverted as expected. But as soon as they differ, I get both, original & mirrored image. May anyone help me on this???

    Code (CSharp):
    1. public class FlipAndMirror : MonoBehaviour
    2.     {
    3.     public Camera main_camera;
    4.  
    5.     public void FlipAndMirrorMainCamera(bool flip, bool mirror)
    6.         {
    7.        
    8.         main_camera.ResetWorldToCameraMatrix();
    9.         main_camera.ResetProjectionMatrix();
    10.        
    11.         if (flip&&mirror)
    12.             {
    13.             main_camera.projectionMatrix = main_camera.projectionMatrix * Matrix4x4.Scale(new Vector3(-1, -1, 1));
    14.             }  
    15.            
    16.         if (!flip&&mirror)  
    17.             {
    18.             main_camera.projectionMatrix = main_camera.projectionMatrix * Matrix4x4.Scale(new Vector3(-1, 1, 1));
    19.             DebugThis("here I am"+ main_camera.projectionMatrix);
    20.            
    21.             }              
    22.         if (flip&&!mirror)  
    23.             {
    24.             main_camera.projectionMatrix = main_camera.projectionMatrix * Matrix4x4.Scale(new Vector3(1, -1, 1));
    25.             }  
    26.            
    27.         if (!flip&&!mirror)      
    28.             {
    29.             main_camera.projectionMatrix = main_camera.projectionMatrix * Matrix4x4.Scale(new Vector3(1, 1, 1));          
    30.             }  
    31.         }
    32.     }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,072
    I don't understand what you mean by "get both original & mirrored".
     
  3. hughda

    hughda

    Joined:
    Jul 11, 2017
    Posts:
    7
    thx for your response,

    for example: First i call FlipandMirrorMainCamera(true,true), my camera is inverted on both axis and
    i see the objects in the scene inverted as well. Now i call FlipandMirrorMainCamera(false,true) and i see my objects inverted, as well as not inverted. The camera shows 2 Objects.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,072
    If you're seeing two images of the same object at the same time, that seems unlikely to be the fault of the code you posted. I don't think you can get that result just by messing with the projection matrix, and certainly not with a scale.

    Is it possible that you have two cameras whose images are compositing?
     
  5. hughda

    hughda

    Joined:
    Jul 11, 2017
    Posts:
    7
    Setting Clear Flags from Skybox to Solid Color fixed it. But i am still kind of confused.