在使用VR的时候,用头盔看场景会出现闪面的问题,闪面原因之一是Camera的Clipping Planes的值过于小的引起的,而我们将Clipping Planes的值调高后,手柄又会看不见,所以我们创建两个摄影机,一个摄影机专门用来渲染手柄。另一个渲染画面
代码:先创建两个相机
private static void InitVRCamera()
{
var eye = CreateCameraEye();
eye.transform.SetParent(vrCmaeraPrent);
var gamePadEye = CreateCameraGamepadEye();
gamePadEye.transform.SetParent(vrCmaeraPrent);
}
在渲染的时候通过layer来区分渲染
//主VR摄像机
private static GameObject CreateCameraEye()
{
var createCameraEye = new GameObject("Camera (eye)")
{
tag = "MainCamera"
};
var camera = createCameraEye.AddComponent<Camera>();
camera.clearFlags = CameraClearFlags.Skybox;
camera.backgroundColor = new Color32(49, 77, 121, 5);
camera.useOcclusionCulling = false;
camera.orthographic = false;
camera.nearClipPlane = 0.5f;
camera.farClipPlane = 100000000;
camera.depth = -1;
camera.useOcclusionCulling = true;
camera.allowHDR = false;
camera.stereoTargetEye = StereoTargetEyeMask.Both;
camera.cullingMask = Layer.SceneVRCullingMask;
createCameraEye.AddComponent<FlareLayer>();
createCameraEye.AddComponent<SteamVR_Camera>();
GameObject earsGameObject = CreateCameraEars();
earsGameObject.transform.SetParent(createCameraEye.transform);
var collider = createCameraEye.AddComponent<CapsuleCollider>();
collider.radius = .5f;
collider.height = 2;
var rigidbody = createCameraEye.AddComponent<Rigidbody>();
rigidbody.useGravity = false;
rigidbody.isKinematic = true;
return createCameraEye;
}
//渲染手柄的摄像机
private static GameObject CreateCameraGamepadEye()
{
var gamepadGameObject = new GameObject("CameraGamePad (eye)");
var gamepadCamera = gamepadGameObject.AddComponent<Camera>();
gamepadCamera.nearClipPlane = 0.01f;
gamepadCamera.farClipPlane = 5f;
gamepadCamera.cullingMask = Layer.VRGamepadCullingMask; //只渲染手柄的layer
gamepadCamera.depth = 5;
gamepadCamera.clearFlags = CameraClearFlags.Depth;
return gamepadGameObject;
}
这样就可以有效的解决一部分闪面问题