SafetyAreaCommandBuffer.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. public class SafetyAreaCommandBuffer : SingletonMono<SafetyAreaCommandBuffer>
  7. {
  8. public Mesh quadMesh;
  9. public Material quadMaterial;
  10. public Material blitMaterial;
  11. //public Color clearColor;
  12. private CommandBuffer commandBuffer;
  13. private float fadeValue;
  14. private bool isLoading = false;
  15. // Start is called before the first frame update
  16. IEnumerator Start()
  17. {
  18. yield return new WaitUntil(()=>API_GSXR_Slam.GSXR_Is_SlamInitialized() && API_GSXR_Slam.GSXR_Get_EyeCameras().Count != 0);
  19. commandBuffer = new CommandBuffer();
  20. commandBuffer.name = "LQRCommandBufferTest";
  21. }
  22. public void AddLoading()
  23. {
  24. if (API_GSXR_Slam.GSXR_Get_EyeCameras().Count == 0) return;
  25. RemoveLoading();
  26. fadeValue = 0f;
  27. isLoading = true;
  28. List<Camera> cameraList = API_GSXR_Slam.GSXR_Get_EyeCameras();
  29. for (int i = 0; i < cameraList.Count; i++)
  30. {
  31. Camera camera = cameraList[i];
  32. camera.AddCommandBuffer(CameraEvent.AfterEverything, commandBuffer);
  33. }
  34. }
  35. public void RemoveLoading()
  36. {
  37. if (!isLoading) return;
  38. isLoading = false;
  39. List<Camera> cameraList = API_GSXR_Slam.GSXR_Get_EyeCameras();
  40. for (int i = 0; i < cameraList.Count; i++)
  41. {
  42. Camera camera = cameraList[i];
  43. camera.RemoveCommandBuffer(CameraEvent.AfterEverything, commandBuffer);
  44. }
  45. }
  46. private void Update()
  47. {
  48. if (isLoading)
  49. {
  50. fadeValue += Time.deltaTime;
  51. if (fadeValue > 2f)
  52. {
  53. RemoveLoading();
  54. }
  55. }
  56. }
  57. private float rotateValue = 0f;
  58. // Update is called once per frame
  59. void LateUpdate()
  60. {
  61. if (!isLoading) return;
  62. if (commandBuffer == null) return;
  63. if (API_GSXR_Slam.GSXR_Get_Head() == null) return;
  64. commandBuffer.Clear();
  65. commandBuffer.ClearRenderTarget(true, true, Color.black);
  66. if (fadeValue > 0.5f)
  67. {
  68. Matrix4x4 matrix = new Matrix4x4();
  69. matrix.SetTRS(API_GSXR_Slam.GSXR_Get_Head().position + API_GSXR_Slam.GSXR_Get_Head().forward * 15f,
  70. Quaternion.Euler(API_GSXR_Slam.GSXR_Get_Head().rotation.eulerAngles.x, API_GSXR_Slam.GSXR_Get_Head().rotation.eulerAngles.y, rotateValue += 250 * Time.deltaTime),
  71. Vector3.one * 0.8f);
  72. commandBuffer.DrawMesh(quadMesh, matrix, quadMaterial);
  73. }
  74. }
  75. }