Module_RGBCamera.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity
  5. {
  6. public class Module_RGBCamera : MonoBehaviour {
  7. private WebCamTexture webcamTexture;
  8. public string deviceName;
  9. private static MeshRenderer mat;
  10. int _width;
  11. int _height;
  12. bool hasWebCamera = false;
  13. Coroutine waitSlamInit = null;
  14. void Start() {
  15. PlayCamera();
  16. }
  17. void OnDestroy() {
  18. StopCamera();
  19. }
  20. public void PlayCamera() {
  21. DebugMy.Log("Module_RGBCamera Step 0: PlayCamera !", this, true);
  22. if (waitSlamInit == null) {
  23. waitSlamInit = StartCoroutine(WaitSlamInitAction());
  24. }
  25. }
  26. public void StopCamera() {
  27. DebugMy.Log("Module_RGBCamera Step 7: StopCamera !", this, true);
  28. if (waitSlamInit != null) {
  29. StopCoroutine(waitSlamInit);
  30. waitSlamInit = null;
  31. }
  32. if (webcamTexture != null && webcamTexture.isPlaying) {
  33. webcamTexture.Stop();
  34. webcamTexture = null;
  35. }
  36. }
  37. IEnumerator WaitSlamInitAction() {
  38. if (webcamTexture != null && webcamTexture.isPlaying) {
  39. DebugMy.Log("Module_RGBCamera : webcamTexture already isPlaying !", this, true);
  40. yield break;
  41. }
  42. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning);
  43. //DebugMy.Log("Module_RGBCamera Step 1: SlamManager.IsRunning !", this, true);
  44. yield return new WaitUntil(() => ConnectCamera());
  45. DebugMy.Log("Module_RGBCamera Step 2: ConnectCamera OK !", this, true);
  46. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  47. DebugMy.Log("Module_RGBCamera Step 3: RequestUserAuthorization OK !", this, true);
  48. if (Application.HasUserAuthorization(UserAuthorization.WebCam)) {
  49. DebugMy.Log("Module_RGBCamera Step 4: HasUserAuthorization OK !", this, true);
  50. yield return new WaitUntil(() => (WebCamTexture.devices.Length > 0));
  51. WebCamDevice[] devices = WebCamTexture.devices;
  52. for (int i = 0; i < devices.Length; i++)
  53. DebugMy.Log("Module_RGBCamera :device["+i+"]:" + devices[i].name, this, true);
  54. if (devices.Length > 0) {
  55. _width = 640;
  56. _height = 480;
  57. if (devices[0].availableResolutions.Length > 0) {
  58. _width = devices[0].availableResolutions[0].width;
  59. _height = devices[0].availableResolutions[0].height;
  60. }
  61. deviceName = devices[0].name;
  62. DebugMy.Log("Module_RGBCamera Step 5:" + deviceName + " " + _width + " " + _height, this, true);
  63. webcamTexture = new WebCamTexture(deviceName, _width, _height, 30);
  64. webcamTexture.Play();
  65. DebugMy.Log("Module_RGBCamera Step 6: webcamTexture Play", this, true);
  66. mat = this.gameObject.GetComponent<MeshRenderer>();
  67. if (mat)
  68. mat.material.mainTexture = webcamTexture;
  69. }
  70. } else {
  71. DebugMy.Log("Module_RGBCamera Step 8: Error No WebCamera", this, true);
  72. }
  73. waitSlamInit = null;
  74. }
  75. bool ConnectCamera() {
  76. return true;
  77. }
  78. }
  79. }