Search Unity

Resolved Using Multiple Cameras

Discussion in 'VR' started by razielanarki, Jul 19, 2016.

Thread Status:
Not open for further replies.
  1. razielanarki

    razielanarki

    Joined:
    Jun 1, 2014
    Posts:
    58
    Hi!

    I've tried using multiple camera layers, but anything other than the main camera becomes funky.

    - first i tried to parent an second camera below the main camera:
    ... the result was that the second camera moved in exactly the opposite direction than the main camera

    - then i added this script to the second camera:
    Code (CSharp):
    1.     public class Camera2Fixer : MonoBehaviour
    2.     {
    3.         void OnPreCull ()
    4.         {
    5.             transform.position = Camera.main.transform.position;
    6.             transform.rotation = Camera.main.transform.rotation;
    7.         }
    8.     }
    ... and it did nothing.

    - then i moved the second camera to the root of the scene, with the script still attached.
    the second camera stopped moving altogether. :(

    PS: everything looks fine in the editor, the problems are only visible on the Lens itself.


    can anyone explain this? how can i achieve layered cameras?
     
    Last edited: Jul 19, 2016
  2. BrandonFogerty

    BrandonFogerty

    Joined:
    Jan 29, 2016
    Posts:
    83
    Hi raziel_anarki!

    Could you please give me a little more detail with regards to what your use case is. I want to better understand what kind of effect you are trying to achieve so I can understand the problem better. Do the camera's use different culling masks at all? What is each camera's depth setting?

    Thanks!
     
  3. BrandonFogerty

    BrandonFogerty

    Joined:
    Jan 29, 2016
    Posts:
    83
    raziel_anarki,

    I just verified the issue you are running into. I will look into fixing this issue soon. Thanks for bringing it to our attention.
     
    razielanarki likes this.
  4. razielanarki

    razielanarki

    Joined:
    Jun 1, 2014
    Posts:
    58
    thanks!

    yes i intended to use different culling masks, with the second camera rendering a separate layer which the main camera does not, sort of overlayed over/in the scene.
    the main camera was one depth level below the second one (and the second one only cleared the depth buffer).
     
  5. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    You should be able to do this by setting the Target Eye to "None (Main Display) for cameras other than MainCamera. This will prevent the VR system from synching the head position/orientation to the transform of the camera.

    2016-08-01_1952.png
     
  6. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    After some additional research, it turns out this does not work on the device as expected. We'll continue looking into ways to support multiple cameras in a future build, and will provide a work-around in the short term.
     
    razielanarki likes this.
  7. BrandonFogerty

    BrandonFogerty

    Joined:
    Jan 29, 2016
    Posts:
    83
    Hi @raziel_anarki,

    I looked into this matter further. Apparently all cameras take on the hmd's transformation. This is intentional.

    However based on your use case, I think you only need one camera. You can make your overlay geometry children of the main camera. If you do this, the child objects will appear to be in a fixed location because they will move relative to your head. You can use the following shader if you want to make sure the overlay child objects always render last and in front of everything else without writing to the depth buffer. Just apply a material that references the following shader to each game object that should be an overlay.

    Code (CSharp):
    1. Shader "Unlit/Overlay"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "Queue"="Overlay" "RenderType"="Transparent" }
    10.      
    11.         // Don't write to the depth buffer
    12.         ZWrite off
    13.         // Always render the object in front
    14.         ZTest Always
    15.         // Blend the object with the back buffer based on the
    16.         // image's alpha channel
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.  
    19.         Pass
    20.         {
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.          
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float4 vertex : SV_POSITION;
    36.                 float2 uv : TEXCOORD0;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.          
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    46.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.  
    48.                 return o;
    49.             }
    50.          
    51.             fixed4 frag (v2f i) : SV_Target
    52.             {
    53.                 fixed4 col = tex2D(_MainTex, i.uv);
    54.                 return col;
    55.             }
    56.             ENDCG
    57.         }
    58.     }
    59. }
     
    Dustin-Horne and razielanarki like this.
  8. TheoTowers

    TheoTowers

    Joined:
    May 8, 2013
    Posts:
    19
    Has there been any development on this? It still seems to be happening in 5.4.0f3 HTP.
     
  9. Cricro76

    Cricro76

    Joined:
    Jun 5, 2017
    Posts:
    1
    Hi, I'm also interested in understanding if the second camera issue has been solved. I'm trying to use the second camera with the "official" Unity Post-Processing stack (and specifically applying the Bloom effect on some bright objects): again, putting the second camera into the Project and attaching the "Post Processing Behaviour" component is causing the main camera to float around VERY erratically. Should we stick to the shader mentioned above by @BrandonFogerty ? What can I do in order to apply the Bloom effect in this situation? Thank you for your kind attention.
     
  10. unity_andrewc

    unity_andrewc

    Unity Technologies

    Joined:
    Dec 14, 2015
    Posts:
    220
    Would you mind clarifying what you mean about the camera floating around erratically? I don't know of an active bug filed against this, could you please file one for us? Thanks!
     
  11. daniel_lochner

    daniel_lochner

    Joined:
    Jun 9, 2016
    Posts:
    171
    Hi, I'm currently battling with this issue myself.

    Just interested to understand why this is the intentional behaviour? Could it maybe instead track cameras that are tagged as the "MainCamera" or attached to the XRRig?

    Thanks.
     
  12. mfuad

    mfuad

    Unity Technologies

    Joined:
    Jun 12, 2018
    Posts:
    335
    Hi @daniel_lochner, check out our suggestions in the thread you created and let us know if that helps.
     
Thread Status:
Not open for further replies.