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.

67 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. #endregion Read-Only Fields
  12. [SerializeField]
  13. private float m_RotationPeriod;
  14. [SerializeField]
  15. private Vector3 m_RotationAxis = Vector3.forward;
  16. [SerializeField]
  17. private float m_correlusStrength = 100;
  18. [SerializeField]
  19. private Material m_Skybox;
  20. [SerializeField]
  21. private float m_SkyboxRotationMultiplier = 1;
  22. public Vector3 getDownDirection(Vector3 objectPosition, bool normalized = true)
  23. {
  24. if (normalized)
  25. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis).normalized;
  26. else
  27. return Vector3.ProjectOnPlane((objectPosition - transform.position), m_RotationAxis);
  28. }
  29. public Vector3 getPerpendicularDirection(Vector3 objectPosition)
  30. {
  31. return Vector3.Cross(RotationAxis, getDownDirection(objectPosition)).normalized;
  32. }
  33. public Quaternion getUpRotation(Vector3 objectPosition)
  34. {
  35. return Quaternion.FromToRotation(Vector3.up, -getDownDirection(objectPosition));
  36. }
  37. private void Start()
  38. {
  39. if (m_Skybox == null)
  40. m_Skybox = RenderSettings.skybox;
  41. }
  42. private void Update()
  43. {
  44. if (m_Skybox != null)
  45. {
  46. m_Skybox.SetVector("_RotationAxis", m_RotationAxis);
  47. m_Skybox.SetFloat("_Rotation", (Time.time * m_SkyboxRotationMultiplier * m_RotationPeriod) % 360);
  48. }
  49. }
  50. }