Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class RotationController : MonoBehaviour
  5. {
  6. #region Read-Only Fields
  7. public float RotationPeriod => m_RotationPeriod;
  8. public Vector3 Position => transform.position;
  9. public Vector3 RotationAxis => m_RotationAxis;
  10. public float CorrelusStrength => m_correlusStrength;
  11. public float EnvironmentRotationPeriod => m_RotationPeriod * m_SkyboxRotationMultiplier;
  12. #endregion Read-Only Fields
  13. [SerializeField]
  14. private float m_RotationPeriod;
  15. [SerializeField]
  16. private Vector3 m_RotationAxis = Vector3.forward;
  17. [SerializeField]
  18. private float m_correlusStrength = 100;
  19. [SerializeField]
  20. private Material m_Skybox;
  21. [SerializeField]
  22. private float m_SkyboxRotationMultiplier = 1;
  23. public Vector3 getDownDirection(Vector3 objectPosition, bool normalized = true)
  24. {
  25. if (normalized)
  26. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis).normalized;
  27. else
  28. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis);
  29. }
  30. public Vector3 getPerpendicularDirection(Vector3 objectPosition)
  31. {
  32. return Vector3.Cross(RotationAxis, getDownDirection(objectPosition)).normalized;
  33. }
  34. public Quaternion getUpRotation(Vector3 objectPosition)
  35. {
  36. return Quaternion.FromToRotation(Vector3.up, -getDownDirection(objectPosition));
  37. }
  38. private void Start()
  39. {
  40. if (m_Skybox == null)
  41. m_Skybox = RenderSettings.skybox;
  42. }
  43. private void Update()
  44. {
  45. if (m_Skybox != null)
  46. {
  47. m_Skybox.SetVector("_RotationAxis", m_RotationAxis);
  48. m_Skybox.SetFloat("_Rotation", (Time.time * EnvironmentRotationPeriod) % 360);
  49. }
  50. }
  51. }