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.

149 lines
4.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class MeshData : MonoBehaviour
  6. {
  7. [SerializeField, HideInInspector]
  8. private int m_currentSubdivisions = 1;
  9. [SerializeField, HideInInspector]
  10. private List<SubData> m_data = new List<SubData>();
  11. [SerializeField, HideInInspector]
  12. private List<ColliderData> m_colliderData = new List<ColliderData>();
  13. [System.Serializable]
  14. public class SubData
  15. {
  16. public Mesh originalMesh;
  17. public Mesh currentMesh;
  18. public Vector3[] subdividedVertices;
  19. public MeshFilter meshFilter;
  20. public SubData(MeshFilter meshFilter)
  21. {
  22. this.meshFilter = meshFilter;
  23. originalMesh = this.meshFilter.sharedMesh;
  24. }
  25. }
  26. [System.Serializable]
  27. public class ColliderData
  28. {
  29. public Collider originalCollider;
  30. public MeshCollider currentCollider;
  31. public SubData attachedData;
  32. public ColliderData(Collider collider, SubData data)
  33. {
  34. originalCollider = collider;
  35. originalCollider.enabled = false;
  36. currentCollider = originalCollider.gameObject.AddComponent<MeshCollider>();
  37. attachedData = data;
  38. Update();
  39. }
  40. public void Update()
  41. {
  42. currentCollider.sharedMesh = attachedData.currentMesh;
  43. }
  44. }
  45. public static MeshData Add(GameObject gameObject)
  46. {
  47. MeshData _meshData = gameObject.AddComponent<MeshData>();
  48. foreach (MeshFilter meshFilter in gameObject.GetComponentsInChildren<MeshFilter>())
  49. {
  50. SubData _data = new SubData(meshFilter);
  51. _meshData.m_data.Add(_data);
  52. _meshData.SubdivideData(_data);
  53. }
  54. foreach(Collider collider in gameObject.GetComponentsInChildren<Collider>())
  55. {
  56. MeshFilter meshFilter = collider.gameObject.GetComponentInChildren<MeshFilter>();
  57. SubData data = _meshData.m_data.FirstOrDefault(p => p.meshFilter == meshFilter);
  58. if (data != null)
  59. _meshData.m_colliderData.Add(new ColliderData(collider, data));
  60. }
  61. return _meshData;
  62. }
  63. public void Remove()
  64. {
  65. foreach (SubData data in m_data)
  66. {
  67. data.meshFilter.sharedMesh = data.originalMesh;
  68. DestroyImmediate(data.currentMesh);
  69. }
  70. foreach(ColliderData data in m_colliderData)
  71. {
  72. data.originalCollider.enabled = true;
  73. DestroyImmediate(data.currentCollider);
  74. }
  75. DestroyImmediate(this);
  76. }
  77. public void SubdivideMesh(int subdivisionAmount)
  78. {
  79. if (m_currentSubdivisions == subdivisionAmount)
  80. return;
  81. m_currentSubdivisions = subdivisionAmount;
  82. foreach (SubData data in m_data)
  83. SubdivideData(data);
  84. foreach (ColliderData collisionData in m_colliderData)
  85. collisionData.Update();
  86. }
  87. public void CurveMesh(Vector3 Centre, Vector3 Axis)
  88. {
  89. foreach (SubData data in m_data)
  90. CurveData(data, Centre, Axis);
  91. foreach (ColliderData collisionData in m_colliderData)
  92. collisionData.Update();
  93. }
  94. private void CurveData(SubData data, Vector3 Centre, Vector3 Axis)
  95. {
  96. data.currentMesh.vertices = Utility.Meshes.Curve.CurveVertices(data.subdividedVertices, Centre, Axis, data.meshFilter.transform,transform.position);
  97. }
  98. private void SubdivideData(SubData data)
  99. {
  100. if (data.currentMesh != null)
  101. DestroyImmediate(data.currentMesh);
  102. data.currentMesh = null;
  103. data.currentMesh = Utility.Meshes.Subdivion.DuplicateMesh(data.originalMesh);
  104. Debug.Log($"Subdividing by: {m_currentSubdivisions}");
  105. Utility.Meshes.Subdivion.Subdivide(data.currentMesh, m_currentSubdivisions);
  106. data.currentMesh.RecalculateBounds();
  107. data.currentMesh.RecalculateNormals();
  108. data.currentMesh.RecalculateTangents();
  109. data.subdividedVertices = data.currentMesh.vertices;
  110. data.meshFilter.sharedMesh = data.currentMesh;
  111. }
  112. }