|
| 1 | +using System.Collections; |
| 2 | +using UnityEngine; |
| 3 | + |
| 4 | +namespace WebXR.Interactions |
| 5 | +{ |
| 6 | + public class MixedRealityCaptureController : MonoBehaviour |
| 7 | + { |
| 8 | + private enum ControllerState |
| 9 | + { |
| 10 | + None, |
| 11 | + SetCameraPoint, |
| 12 | + SetTopPoint, |
| 13 | + SetBottomPoint, |
| 14 | + Confirm, |
| 15 | + CalcAndSet, |
| 16 | + Playing, |
| 17 | + Ended |
| 18 | + } |
| 19 | + |
| 20 | + private const float WEBCAM_DISTANCE_CALIBRATION = 0.5f; |
| 21 | + private const float WEBCAM_SIZE_CALIBRATION = 0.25f; |
| 22 | + private const float WEBCAM_MIN_SIZE = 0.001f; |
| 23 | + private readonly Quaternion LEFT_STEP_ROTATION = Quaternion.Euler(0, -90, 0); |
| 24 | + |
| 25 | + [SerializeField] |
| 26 | + private bool enableInXR = false; |
| 27 | + private WebXRState currentXRState = WebXRState.NORMAL; |
| 28 | + |
| 29 | + [SerializeField] |
| 30 | + private Transform camerasBase; |
| 31 | + [SerializeField] |
| 32 | + private Transform cameraFollower; |
| 33 | + [SerializeField] |
| 34 | + private Camera spectatorCamera; |
| 35 | + [SerializeField] |
| 36 | + private Transform spectatorCameraTransform; |
| 37 | + [SerializeField] |
| 38 | + private Transform spectatorCameraParent; |
| 39 | + [SerializeField] |
| 40 | + private Transform webcamParent; |
| 41 | + [SerializeField] |
| 42 | + private Transform calibrationPointCamera; |
| 43 | + [SerializeField] |
| 44 | + private Transform calibrationPointTop; |
| 45 | + [SerializeField] |
| 46 | + private Transform calibrationPointBottom; |
| 47 | + [SerializeField] |
| 48 | + private GameObject calibrationHintCamera; |
| 49 | + [SerializeField] |
| 50 | + private GameObject calibrationHintTop; |
| 51 | + [SerializeField] |
| 52 | + private GameObject calibrationHintBottom; |
| 53 | + [SerializeField] |
| 54 | + private WebXRController webxrController; |
| 55 | + |
| 56 | + private ControllerState state = ControllerState.None; |
| 57 | + private float webcamBaseSize = 1f; |
| 58 | + |
| 59 | + private void OnEnable() |
| 60 | + { |
| 61 | + WebXRManager.OnXRChange += HandleOnXRChange; |
| 62 | + currentXRState = WebXRManager.Instance.XRState; |
| 63 | + TryUpdateControllerState(); |
| 64 | + } |
| 65 | + |
| 66 | + private void OnDisable() |
| 67 | + { |
| 68 | + StopAllCoroutines(); |
| 69 | + WebXRManager.OnXRChange -= HandleOnXRChange; |
| 70 | + Ended(); |
| 71 | + } |
| 72 | + |
| 73 | + private void HandleOnXRChange(WebXRState state, int viewsCount, Rect leftRect, Rect rightRect) |
| 74 | + { |
| 75 | + currentXRState = state; |
| 76 | + TryUpdateControllerState(); |
| 77 | + } |
| 78 | + |
| 79 | + public void EnableInXR(bool value) |
| 80 | + { |
| 81 | + enableInXR = value; |
| 82 | + TryUpdateControllerState(); |
| 83 | + } |
| 84 | + |
| 85 | + private void TryUpdateControllerState() |
| 86 | + { |
| 87 | + bool enableState = enableInXR && currentXRState != WebXRState.NORMAL; |
| 88 | + if (enableState && (state == ControllerState.None || state == ControllerState.Ended)) |
| 89 | + { |
| 90 | + StartController(); |
| 91 | + } |
| 92 | + else if (!enableState && state != ControllerState.Ended) |
| 93 | + { |
| 94 | + state = ControllerState.Ended; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void StartController() |
| 99 | + { |
| 100 | + StartCoroutine(ControllerProcess()); |
| 101 | + } |
| 102 | + |
| 103 | + private IEnumerator ControllerProcess() |
| 104 | + { |
| 105 | + state = ControllerState.None; |
| 106 | + calibrationPointCamera.gameObject.SetActive(false); |
| 107 | + calibrationPointTop.gameObject.SetActive(false); |
| 108 | + calibrationPointBottom.gameObject.SetActive(false); |
| 109 | + calibrationHintCamera.SetActive(true); |
| 110 | + calibrationHintTop.SetActive(false); |
| 111 | + calibrationHintBottom.SetActive(false); |
| 112 | + webcamParent.localScale = Vector3.one * WEBCAM_SIZE_CALIBRATION; |
| 113 | + webcamParent.gameObject.SetActive(true); |
| 114 | + spectatorCameraParent.SetPositionAndRotation(camerasBase.position, camerasBase.rotation); |
| 115 | + state = ControllerState.SetCameraPoint; |
| 116 | + while (state != ControllerState.Ended) |
| 117 | + { |
| 118 | + switch (state) |
| 119 | + { |
| 120 | + case ControllerState.SetCameraPoint: |
| 121 | + WhileCalibrating(); |
| 122 | + SetPoint(calibrationPointCamera, calibrationHintCamera, ControllerState.SetTopPoint, calibrationHintTop); |
| 123 | + break; |
| 124 | + case ControllerState.SetTopPoint: |
| 125 | + WhileCalibrating(); |
| 126 | + SetPoint(calibrationPointTop, calibrationHintTop, ControllerState.SetBottomPoint, calibrationHintBottom); |
| 127 | + break; |
| 128 | + case ControllerState.SetBottomPoint: |
| 129 | + WhileCalibrating(); |
| 130 | + SetBottomPoint(); |
| 131 | + break; |
| 132 | + case ControllerState.Confirm: |
| 133 | + WhileCalibrating(); |
| 134 | + Confirm(); |
| 135 | + break; |
| 136 | + case ControllerState.CalcAndSet: |
| 137 | + CalcAndSet(); |
| 138 | + state = ControllerState.Playing; |
| 139 | + break; |
| 140 | + case ControllerState.Playing: |
| 141 | + Playing(); |
| 142 | + break; |
| 143 | + } |
| 144 | + yield return null; |
| 145 | + } |
| 146 | + Ended(); |
| 147 | + } |
| 148 | + |
| 149 | + private void Ended() |
| 150 | + { |
| 151 | + calibrationPointCamera.gameObject.SetActive(false); |
| 152 | + calibrationPointTop.gameObject.SetActive(false); |
| 153 | + calibrationPointBottom.gameObject.SetActive(false); |
| 154 | + webcamParent.gameObject.SetActive(false); |
| 155 | + } |
| 156 | + |
| 157 | + private void SetPoint(Transform point, GameObject hint, ControllerState nextState, GameObject nextHint) |
| 158 | + { |
| 159 | + if (GetControllersButtonDown()) |
| 160 | + { |
| 161 | + point.position = webxrController.transform.position; |
| 162 | + point.gameObject.SetActive(true); |
| 163 | + hint.SetActive(false); |
| 164 | + nextHint.SetActive(true); |
| 165 | + state = nextState; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private void SetBottomPoint() |
| 170 | + { |
| 171 | + if (GetControllersButtonDown()) |
| 172 | + { |
| 173 | + float cameraToTopDistance = Vector3.Distance(calibrationPointCamera.position, calibrationPointTop.position); |
| 174 | + Vector3 cameraToBottomDirection = (webxrController.transform.position - calibrationPointCamera.position).normalized; |
| 175 | + calibrationPointBottom.position = calibrationPointCamera.position + cameraToBottomDirection * cameraToTopDistance; |
| 176 | + calibrationPointBottom.gameObject.SetActive(true); |
| 177 | + calibrationHintBottom.SetActive(false); |
| 178 | + state = ControllerState.Confirm; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private void Confirm() |
| 183 | + { |
| 184 | + if (GetControllersButtonDown()) |
| 185 | + { |
| 186 | + calibrationPointCamera.gameObject.SetActive(false); |
| 187 | + calibrationPointTop.gameObject.SetActive(false); |
| 188 | + calibrationPointBottom.gameObject.SetActive(false); |
| 189 | + state = ControllerState.CalcAndSet; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private void CalcAndSet() |
| 194 | + { |
| 195 | + SetCameraVerticalFOV(); |
| 196 | + SetCameraPositionRotation(); |
| 197 | + } |
| 198 | + |
| 199 | + private void WhileCalibrating() |
| 200 | + { |
| 201 | + spectatorCameraTransform.SetPositionAndRotation(cameraFollower.position, cameraFollower.rotation); |
| 202 | + webcamParent.SetPositionAndRotation(cameraFollower.position + cameraFollower.forward * WEBCAM_DISTANCE_CALIBRATION, cameraFollower.rotation); |
| 203 | + } |
| 204 | + |
| 205 | + private void Playing() |
| 206 | + { |
| 207 | + var headPosition = spectatorCameraTransform.InverseTransformPoint(cameraFollower.position); |
| 208 | + var newWebcamPosition = spectatorCameraTransform.TransformPoint(new Vector3(0, 0, headPosition.z)); |
| 209 | + webcamParent.SetPositionAndRotation(newWebcamPosition, spectatorCameraTransform.rotation); |
| 210 | + float webcamSize = Mathf.Max(Mathf.LerpUnclamped(0f, webcamBaseSize, headPosition.z), WEBCAM_MIN_SIZE); |
| 211 | + webcamParent.localScale = Vector3.one * webcamSize; |
| 212 | + } |
| 213 | + |
| 214 | + private void SetCameraVerticalFOV() |
| 215 | + { |
| 216 | + float distanceA = Vector3.Distance(calibrationPointTop.position, calibrationPointBottom.position); |
| 217 | + float distanceB = Vector3.Distance(calibrationPointCamera.position, calibrationPointTop.position); |
| 218 | + float distanceC = Vector3.Distance(calibrationPointCamera.position, calibrationPointBottom.position); |
| 219 | + float A = Mathf.Acos((distanceB * distanceB + distanceC * distanceC - distanceA * distanceA) / (2 * distanceB * distanceC)); |
| 220 | + spectatorCamera.fieldOfView = Mathf.Rad2Deg * A; |
| 221 | + webcamBaseSize = Mathf.Tan(A * 0.5f) * 2f; // Half of FOV as direction * 2 |
| 222 | + } |
| 223 | + |
| 224 | + private void SetCameraPositionRotation() |
| 225 | + { |
| 226 | + Plane plane = new Plane(calibrationPointCamera.position, calibrationPointTop.position, calibrationPointBottom.position); |
| 227 | + Vector3 up = (calibrationPointTop.position - calibrationPointBottom.position).normalized; |
| 228 | + spectatorCameraTransform.SetPositionAndRotation(calibrationPointCamera.position, Quaternion.LookRotation(plane.normal, up) * LEFT_STEP_ROTATION); |
| 229 | + } |
| 230 | + |
| 231 | + bool GetControllersButtonDown() |
| 232 | + { |
| 233 | + return (webxrController.isHandActive || webxrController.isControllerActive) |
| 234 | + && (webxrController.GetButtonDown(WebXRController.ButtonTypes.Trigger)); |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments