当前位置:网站首页>Unity VR resource flash surface in scene

Unity VR resource flash surface in scene

2022-07-06 01:17:00 The way of growth

In the use of VR When , Seeing the scene with a helmet will cause the problem of flashing , One of the reasons for the flash is Camera Of Clipping Planes The value of is too small , And we will Clipping Planes After increasing the value of , The handle will be invisible again , So we create two cameras , A camera is dedicated to rendering handles . Another rendering
Code : First create two cameras

 private static void InitVRCamera()
 {
    
      var eye = CreateCameraEye();
      eye.transform.SetParent(vrCmaeraPrent);
      var gamePadEye = CreateCameraGamepadEye();
      gamePadEye.transform.SetParent(vrCmaeraPrent);
 }

When rendering, pass layer To distinguish rendering

		// Lord VR The camera 
  		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;
        }

		// Render handle camera 
	    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;   // Render handle only layer
            gamepadCamera.depth = 5;
            gamepadCamera.clearFlags = CameraClearFlags.Depth;
            return gamepadGameObject;
        }

This can effectively solve part of the flash surface problem

原网站

版权声明
本文为[The way of growth]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140134424492.html