Teleport.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. using Nxr.Internal;
  16. namespace NXR.Samples
  17. {
  18. //
  19. [RequireComponent(typeof(Collider))]
  20. public class Teleport : MonoBehaviour, INxrGazeResponder
  21. {
  22. private Vector3 startingPosition;
  23. private bool gazeAt = false;
  24. void Start()
  25. {
  26. startingPosition = transform.localPosition;
  27. SetGazedAt(false);
  28. }
  29. public void SetGazedAt(bool gazedAt)
  30. {
  31. gazeAt = gazedAt;
  32. Material mat = GetComponent<Renderer>().material;
  33. Color color = gazedAt ? Color.green : Color.red;
  34. mat.color = color;
  35. mat.SetColor("_BaseColor", color);
  36. }
  37. public void Reset()
  38. {
  39. transform.localPosition = startingPosition;
  40. }
  41. public void ToggleSplitMode()
  42. {
  43. NxrViewer.Instance.SplitScreenModeEnabled = !NxrViewer.Instance.SplitScreenModeEnabled;
  44. }
  45. public void ToggleDirectRender()
  46. {
  47. }
  48. public void TeleportRandomly()
  49. {
  50. Vector3 direction = UnityEngine.Random.onUnitSphere;
  51. direction.y = Mathf.Clamp(direction.y, 0.5f, 1f);
  52. float distance = 2 * UnityEngine.Random.value + 1.5f;
  53. transform.localPosition = direction * distance;
  54. }
  55. #region INxrGazeResponder implementation
  56. /// Called when the user is looking on a GameObject with this script,
  57. /// as long as it is set to an appropriate layer (see NxrGaze).
  58. public void OnGazeEnter()
  59. {
  60. SetGazedAt(true);
  61. }
  62. /// Called when the user stops looking on the GameObject, after OnGazeEnter
  63. /// was already called.
  64. public void OnGazeExit()
  65. {
  66. SetGazedAt(false);
  67. }
  68. /// Called when the viewer's trigger is used, between OnGazeEnter and OnGazeExit.
  69. public void OnGazeTrigger()
  70. {
  71. TeleportRandomly();
  72. }
  73. public void OnUpdateIntersectionPosition(Vector3 position)
  74. {
  75. // Debug.Log("Teleport.OnUpdateIntersectionPosition=" + position.ToString());
  76. }
  77. #endregion
  78. }
  79. }