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
public class Camera2Fixer : MonoBehaviour
{
void OnPreCull ()
{
transform.position = Camera.main.transform.position;
transform.rotation = Camera.main.transform.rotation;
}
}
… 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?
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?
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).
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.
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.
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.
Shader "Unlit/Overlay"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "Queue"="Overlay" "RenderType"="Transparent" }
// Don't write to the depth buffer
ZWrite off
// Always render the object in front
ZTest Always
// Blend the object with the back buffer based on the
// image's alpha channel
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
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.
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!
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?