PlanetariumMover.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. //#define __ENABLE__SOUND__
  8. /// <summary>
  9. /// Lets the user move, rotate and scale the planetarium object in the ZED planetarium sample scene.
  10. /// Also makes sure that relevant fields not tied directly to a transform, like sound distance and light radius,
  11. /// get properly scaled with the planetarium itself.
  12. /// </summary>
  13. public class PlanetariumMover : MonoBehaviour
  14. {
  15. /// <summary>
  16. /// How fast the solar system changes size when scaled.
  17. /// </summary>
  18. [Tooltip("How fast the solar system changes size when scaled. ")]
  19. public float speedGrowth = 1.0f;
  20. /// <summary>
  21. /// How quickly the solar system moves up, right, forward, etc.
  22. /// </summary>
  23. [Tooltip("How quickly the solar system moves up, right, forward, etc. ")]
  24. public float speedMove = 1.0f;
  25. /// <summary>
  26. /// How quickly the solar system rotates on the Y axis when the user rotates it (unrelated to planet orbits).
  27. /// </summary>
  28. [Tooltip("How quickly the solar system rotates on the Y axis when the user rotates it (unrelated to planet orbits). ")]
  29. public float speedRotation = 20.0f;
  30. /// <summary>
  31. /// The Planetarium object being governed. This is the 'Planetarium' GameObject in the ZED planetarium scene.
  32. /// </summary>
  33. [Tooltip("The Planetarium object being governed. This is the 'Planetarium' GameObject in the ZED planetarium scene.")]
  34. public GameObject planetarium;
  35. //public static bool scaling = false;
  36. /// <summary>
  37. /// The scene's ZEDManager component.
  38. /// </summary>
  39. private ZEDManager manager;
  40. /// <summary>
  41. /// The Sun GameObject.
  42. /// </summary>
  43. private GameObject suncontainer;
  44. /// <summary>
  45. /// Light component on the Sun GameObject.
  46. /// </summary>
  47. private Light sunlight;
  48. /// <summary>
  49. /// Light component on the SunSpotLight GameObject.
  50. /// </summary>
  51. private Light spotLightSun;
  52. /// <summary>
  53. /// Light component on the SunHaloLight GameObject.
  54. /// </summary>
  55. private Light halolightsun; //for Halo
  56. /// <summary>
  57. /// The current scale setting. Equal to transform.scale.x, which is used as a proxy for all three values.
  58. /// </summary>
  59. private float currentscale;
  60. /// <summary>
  61. /// The range of the sunlight Light component.
  62. /// </summary>
  63. private float currentlightrange = 1;
  64. /// <summary>
  65. /// The range of the SunSpotLight's Light component.
  66. /// </summary>
  67. private float currentlightrangesunspot = 1;
  68. /// <summary>
  69. /// The range of the SunHaloLight's Light component.
  70. /// </summary>
  71. private float currentlightrangesunhalo = 0.6f;
  72. /// <summary>
  73. /// The largest you can scale the planetarium.
  74. /// </summary>
  75. private const float MAX_LIMIT_SCALE = 3.0f;
  76. /// <summary>
  77. /// The smallest you can scale the planetarium.
  78. /// </summary>
  79. private const float MIN_LIMIT_SCALE = 0.05f;
  80. /// <summary>
  81. /// When scaling the planetarium, the amount changed each frame is divided by this number.
  82. /// </summary>
  83. private float scaler = 5;
  84. #if __ENABLE__SOUND__
  85. public AudioSource sunSound;
  86. public AudioSource jupiterSound;
  87. #endif
  88. private float currentMaxSoundDistanceSun;
  89. private float currentMaxSoundDistanceJupiter;
  90. void Start()
  91. {
  92. if (!planetarium)
  93. {
  94. planetarium = GameObject.Find("Planetarium");
  95. }
  96. currentscale = planetarium.transform.localScale.x;
  97. suncontainer = planetarium.transform.Find("Sun").gameObject;
  98. sunlight = suncontainer.GetComponent<Light>();
  99. currentlightrange = sunlight.range * (1 / currentscale);
  100. manager = planetarium.transform.parent.GetComponentInChildren<ZEDManager>();
  101. spotLightSun = suncontainer.transform.Find("SunSpotLight").GetComponent<Light>();
  102. halolightsun = suncontainer.transform.Find("SunHaloLight").GetComponent<Light>();
  103. currentlightrangesunspot = spotLightSun.range * (1 / currentscale);
  104. currentlightrangesunhalo = halolightsun.range * (1 / currentscale);
  105. #if __ENABLE__SOUND__
  106. currentMaxSoundDistanceJupiter = jupiterSound.maxDistance * (1 / currentScale);
  107. currentMaxSoundDistanceSun = sunSound.maxDistance * (1 / currentScale);
  108. #endif
  109. }
  110. private void OnEnable()
  111. {
  112. manager.OnZEDReady += ZEDReady;
  113. }
  114. private void OnDisable()
  115. {
  116. manager.OnZEDReady -= ZEDReady;
  117. }
  118. /// <summary>
  119. /// Called when the ZED is finished initializing, using the ZEDManager.OnZEDReady callback.
  120. /// </summary>
  121. void ZEDReady()
  122. {
  123. if (manager)
  124. planetarium.transform.position = manager.OriginPosition + manager.OriginRotation * Vector3.forward;
  125. }
  126. // Update is called once per frame
  127. void Update()
  128. {
  129. string[] names = Input.GetJoystickNames();
  130. bool hasJoystick = false;
  131. if (names.Length > 0)
  132. hasJoystick = names[0].Length > 0;
  133. /// Adjust Planetarium X/Y/Z position
  134. float axisH = Input.GetAxis("Horizontal");
  135. float axisV = Input.GetAxis("Vertical");
  136. Quaternion gravity = Quaternion.identity;
  137. gravity = Quaternion.FromToRotation(manager.GetZedRootTansform().up, Vector3.up);
  138. planetarium.transform.localPosition += manager.GetMainCameraTransform().right * axisH * speedMove * Time.deltaTime;
  139. planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().forward * axisV * speedMove * Time.deltaTime;
  140. /// Adjust Scale of Virtual objects,lights, sounds
  141. bool ScaleUpButton = Input.GetButton("Fire1") || Input.GetKey(KeyCode.JoystickButton5) || (Input.GetAxis("Fire1") >= 1);
  142. bool ScaleDownButton = Input.GetButton("Fire2") || (Input.GetAxis("Fire2") >= 1);
  143. currentscale += System.Convert.ToInt32(ScaleUpButton) * speedGrowth * Time.deltaTime / scaler;
  144. currentscale -= System.Convert.ToInt32(ScaleDownButton) * speedGrowth * Time.deltaTime / scaler;
  145. if (currentscale < MIN_LIMIT_SCALE) currentscale = MIN_LIMIT_SCALE;
  146. if (currentscale > MAX_LIMIT_SCALE) currentscale = MAX_LIMIT_SCALE;
  147. planetarium.transform.localScale = new Vector3(currentscale, currentscale, currentscale);
  148. sunlight.range = currentlightrange * currentscale;
  149. spotLightSun.range = currentlightrangesunspot * currentscale;
  150. halolightsun.range = currentlightrangesunhalo * currentscale;
  151. #if __ENABLE__SOUND__
  152. jupiterSound.maxDistance = currentMaxSoundDistanceJupiter * currentScale;
  153. sunSound.maxDistance = currentMaxSoundDistanceSun * currentScale;
  154. #endif
  155. /// Adjust Rotation of Planetarium
  156. if (CheckAxes("DPad X") && hasJoystick)
  157. {
  158. float axisX = Input.GetAxis("DPad X"); //multiply by 10 since sensibility is at 0.1 by default
  159. planetarium.transform.Rotate(gravity * manager.GetMainCameraTransform().up * axisX * speedRotation, Space.World);
  160. }
  161. else
  162. {
  163. float axisX = System.Convert.ToInt32(Input.GetKey(KeyCode.R));
  164. planetarium.transform.Rotate(gravity * manager.GetMainCameraTransform().up * axisX * speedRotation, Space.World);
  165. }
  166. //adjust Height of Planetarium
  167. if (CheckAxes("DPad Y") && hasJoystick)
  168. {
  169. float axisY = Input.GetAxis("DPad Y");
  170. planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().up * axisY * speedMove * Time.deltaTime;
  171. }
  172. else
  173. {
  174. float axisY = System.Convert.ToInt32(Input.GetKey(KeyCode.PageUp)) - System.Convert.ToInt32(Input.GetKey(KeyCode.PageDown));
  175. planetarium.transform.localPosition += gravity * manager.GetMainCameraTransform().up * axisY * speedMove * Time.deltaTime;
  176. }
  177. }
  178. public static bool CheckAxes(string choice)
  179. {
  180. #if UNITY_EDITOR
  181. var inputManager = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  182. SerializedObject obj = new SerializedObject(inputManager);
  183. SerializedProperty axisArray = obj.FindProperty("m_Axes");
  184. if (axisArray.arraySize == 0)
  185. Debug.Log("No Axes");
  186. for (int i = 0; i < axisArray.arraySize; ++i)
  187. {
  188. var axis = axisArray.GetArrayElementAtIndex(i);
  189. var name = axis.FindPropertyRelative("m_Name").stringValue;
  190. if (name == choice)
  191. return true;
  192. }
  193. return false;
  194. #else
  195. return true;
  196. #endif
  197. }
  198. }