123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class ZEDTransformController : MonoBehaviour
- {
-
-
-
-
- [Header("Object motion relative to:")]
- [Tooltip("Rotational reference point for moving forward, backward, left, right, etc.")]
- public RelativeMotion motion;
-
-
-
- [Tooltip("Whether to rotate in the opposite direction specified, eg rotating 'right' means counter-clockwise.")]
- public bool invertRotation;
-
-
-
- [Tooltip("If true, the object will teleport to a meter in front of the ZED once it's finished initializing.")]
- public bool repositionAtStart = true;
-
-
-
-
- [Tooltip("Reference to the scene's ZEDManager component. Used when RelativeMotion is set to Camera, " +
- "and also for positioning it at start. If left blank, the first ZED camera indexed will be set.")]
- public ZEDManager zedManager = null;
-
-
-
- [Space(5)]
- [Header("Motion Options")]
- [Tooltip("How fast the object moves/translates, in meters per second. ")]
- public float movementSpeed = 0.5F;
-
-
-
- [Tooltip("How fast the object rotates, in revolutions per second.")]
- public float rotationSpeed = 0.1f;
-
-
-
-
- [Tooltip("How quickly an object gets bigger or smaller. Scale increases/decreases by this factor every second.")]
- public float scaleSpeed = 0.25F;
-
-
-
- [Tooltip("The largest amount to which the object can scale.")]
- public float maxScale = 2.0F;
-
-
-
- [Tooltip("The smallest amount down to which the object can scale. ")]
- public float minScale = 0.25F;
-
-
-
-
- [Space(5)]
- [Tooltip("Optional reference to a light that is enabled only when moving.")]
- public Light spotLight;
-
-
-
-
- private List<ZEDControllerTracker_DemoInputs> objectTrackers = new List<ZEDControllerTracker_DemoInputs>();
-
-
-
-
- private Camera leftCamera;
-
-
-
- private bool isMoving;
- private IEnumerator Start()
- {
- isMoving = false;
- if (!zedManager)
- {
- zedManager = FindObjectOfType<ZEDManager>();
- if (ZEDManager.GetInstances().Count > 1)
- {
- Debug.Log("Warning: ZEDTransformController's zedManager field was not specified, but there are multiple ZEDManagers in the scene " +
- "so first available ZED was assigned. This can cause the object to move relative to the wrong camera. " +
- "It's recommended to assign the desired ZEDManager in the Inspector.");
- }
- }
-
- yield return new WaitForSeconds(1f);
- var trackers = FindObjectsOfType<ZEDControllerTracker>();
- foreach (ZEDControllerTracker_DemoInputs tracker in trackers)
- {
- objectTrackers.Add(tracker);
- }
- if (repositionAtStart)
- {
- zedManager.OnZEDReady += RepositionInFrontOfZED;
- }
- }
- private void Update()
- {
- Vector3 moveAxis = Vector3.zero;
- float inputRotation = 0f;
- float inputScale = 0f;
-
- if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.Q))
- {
- inputRotation = -1 * (rotationSpeed * 360) * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.E))
- {
- inputRotation = 1 * (rotationSpeed * 360) * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
- {
- moveAxis = Vector3.forward * movementSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
- {
- moveAxis = Vector3.back * movementSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.A))
- {
- moveAxis = Vector3.left * movementSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.D))
- {
- moveAxis = Vector3.right * movementSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.R))
- {
- moveAxis = Vector3.up * movementSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.F))
- {
- moveAxis = Vector3.down * movementSpeed * Time.deltaTime;
- }
- Quaternion gravity = Quaternion.identity;
- if (moveAxis != Vector3.zero)
- {
- isMoving = true;
- if (motion == RelativeMotion.Itself)
- {
- transform.Translate(moveAxis.x, moveAxis.y, moveAxis.z);
- }
- else if (motion == RelativeMotion.Camera)
- {
- gravity = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
- transform.localPosition += zedManager.GetMainCameraTransform().right * moveAxis.x;
- transform.localPosition += zedManager.GetMainCameraTransform().forward * moveAxis.z;
- transform.localPosition += gravity * zedManager.GetMainCameraTransform().up * moveAxis.y;
- }
- }
- else
- {
- isMoving = false;
- }
- if (Input.GetKey(KeyCode.Mouse0))
- inputScale = 1f;
- else if (Input.GetKey(KeyCode.Mouse1))
- inputScale = -1f;
- if (zedManager)
- {
- Vector3 moveaxis = new Vector3();
-
-
- if (objectTrackers.Count > 0)
- {
- moveaxis = objectTrackers[0].CheckNavigateUIAxis();
- if (Mathf.Abs(moveaxis.x) < 0.1f) moveaxis.x = 0;
- if (Mathf.Abs(moveaxis.y) < 0.1f) moveaxis.y = 0;
- inputRotation += moveaxis.x * rotationSpeed * 360f * Time.deltaTime;
- gravity = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
- transform.localPosition += gravity * zedManager.GetMainCameraTransform().up * moveaxis.y * movementSpeed * Time.deltaTime;
- if (objectTrackers[0].CheckClickButton(ControllerButtonState.Held))
- {
- inputScale = 1f;
- }
- }
-
- if (objectTrackers.Count > 1)
- {
- moveaxis = objectTrackers[1].CheckNavigateUIAxis();
- if (Mathf.Abs(moveaxis.x) < 0.1f) moveaxis.x = 0;
- if (Mathf.Abs(moveaxis.y) < 0.1f) moveaxis.y = 0;
- if (moveaxis.x != 0 || moveaxis.y != 0)
- {
- isMoving = true;
- gravity = Quaternion.FromToRotation(zedManager.GetZedRootTansform().up, Vector3.up);
- transform.localPosition += zedManager.GetMainCameraTransform().right * moveaxis.x * movementSpeed * Time.deltaTime;
- transform.localPosition += gravity * zedManager.GetMainCameraTransform().forward * moveaxis.y * movementSpeed * Time.deltaTime;
- }
- else
- isMoving = false;
- if (objectTrackers[1].CheckClickButton(ControllerButtonState.Held))
- {
- inputScale = -1f;
- }
- }
- }
-
- float h = inputRotation;
- if (invertRotation)
- transform.Rotate(0, h, 0);
- else
- transform.Rotate(0, -h, 0);
-
- float s = scaleSpeed * (inputScale * Time.deltaTime);
- transform.localScale = new Vector3(transform.localScale.x + s,
- transform.localScale.y + s,
- transform.localScale.z + s);
- if (transform.localScale.x > maxScale)
- transform.localScale = new Vector3(maxScale, maxScale, maxScale);
- else if (transform.localScale.x < minScale)
- transform.localScale = new Vector3(minScale, minScale, minScale);
-
- if (spotLight != null)
- {
- SetMovementLight();
- }
- }
-
-
-
-
- void SetMovementLight()
- {
-
- if (!spotLight.enabled && isMoving)
- {
- spotLight.enabled = true;
- }
- else if (spotLight.enabled && !isMoving)
- {
- spotLight.enabled = false;
- }
-
- if (spotLight.enabled && spotLight.type == LightType.Spot)
- {
- spotLight.spotAngle = transform.localScale.x * 180 * 2;
- spotLight.range = transform.localScale.x * 4f;
- if (spotLight.range > 2)
- spotLight.range = 2;
- }
- }
-
-
-
-
- void RepositionInFrontOfZED()
- {
-
-
- if (zedManager.estimateInitialPosition && zedManager.enableTracking)
- {
- Vector3 initpos = Vector3.zero;
- Quaternion initrot = Quaternion.identity;
- zedManager.zedCamera.EstimateInitialPosition(ref initrot, ref initpos);
- transform.position = initpos + (initrot * Vector3.forward);
- Quaternion newRot = Quaternion.LookRotation(initpos - transform.position, Vector3.up);
- transform.eulerAngles = new Vector3(0, newRot.eulerAngles.y + 180, 0);
- }
- else
- {
- transform.position = zedManager.OriginPosition + zedManager.OriginRotation * (Vector3.forward);
- Quaternion newRot = Quaternion.LookRotation(zedManager.OriginPosition - transform.position, Vector3.up);
- transform.eulerAngles = new Vector3(0, newRot.eulerAngles.y + 180, 0);
- }
-
- if(zedManager.estimateInitialPosition)
- {
- if (transform.position.y < 0.5f) transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
- }
- }
- IEnumerator RepositionAfterZEDReady()
- {
- for (int i = 0; i < 50; i++)
- {
- print(zedManager.GetZedRootTansform().position);
- yield return null;
- }
- }
-
-
-
- public enum RelativeMotion
- {
- Itself,
- Camera
- }
- }
|