Search Unity

Second Camera: Projectio matrix issue

Discussion in 'Scripting' started by Jannizzle, Jan 29, 2015.

  1. Jannizzle

    Jannizzle

    Joined:
    Nov 14, 2014
    Posts:
    5
    Hello Guys and gals,
    I want to implement a function which mirrors the camera view along the x-axis.

    Code (CSharp):
    1.  public void Vertigo()
    2.     {    
    3.         camera.projectionMatrix = camera.projectionMatrix * Matrix4x4.Scale(new Vector3(1, -1, 1));
    4.         camera2.projectionMatrix = camera2.projectionMatrix * Matrix4x4.Scale(new Vector3(1, -1, 1));      
    5.     }
    This works perfectly fine for the main camera but the second camera (camera2) does not flip the image, but instead goes black.

    The main camera projects the game scene and camera2 just a scrolling back and midground.

    Any ideas why the above code just works with the main camera but not for the second one?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Why is y -1, not x?

    Maybe the new matrix points the wrong way, i assume your background is positioned 'infront' of the camera. perhaps after changing the matrix, the camera is rendering backwards, and this is fine for cam 1 because the camera has some transform to compensate
     
    Jannizzle likes this.
  3. Jannizzle

    Jannizzle

    Joined:
    Nov 14, 2014
    Posts:
    5
    Thanks. You sir are indeed right. I manually turned the background plane away from the camera facing in the opposit direction. calling the vertigo function showed the background upside down.
    My first idea is to use two back to back planes to get the desired effect.
    Better ideas?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Can you just use a 2-sided material? A shader that does not cull backfacing

    ie: heres the default diffuse shader, with no cull :
    Code (CSharp):
    1. Shader "Custom/2-Sided Diffuse" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.  
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.         cull off //this line
    11.  
    12.         CGPROGRAM
    13.         #pragma surface surf Lambert
    14.  
    15.         sampler2D _MainTex;
    16.         fixed4 _Color;
    17.  
    18.         struct Input {
    19.             float2 uv_MainTex;
    20.         };
    21.  
    22.         void surf (Input IN, inout SurfaceOutput o) {
    23.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    24.             o.Albedo = c.rgb;
    25.             o.Alpha = c.a;
    26.         }
    27.         ENDCG
    28.     }
    29.  
    30.     Fallback "VertexLit"
    31. }
    32.