Folding.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Folding : MonoBehaviour
  5. {
  6. [Header(" Gizmos Settings ")]
  7. public bool showGizmos;
  8. [Header(" Settings ")]
  9. [SerializeField] private RotationAxis rotationAxis;
  10. [SerializeField] private float foldingAngle;
  11. [SerializeField] private Folding[] requiredFoldings;
  12. private bool folded;
  13. [Header(" Mesh Settings ")]
  14. List<int> backFoldedVerticesIndices = new List<int>();
  15. List<int> frontFoldedVerticesIndices = new List<int>();
  16. public RotationAxis GetRotationAxis()
  17. {
  18. return rotationAxis;
  19. }
  20. public void SetRotationAxis(RotationAxis rt)
  21. {
  22. rotationAxis = rt;
  23. }
  24. public float GetRotationAngle()
  25. {
  26. return foldingAngle;
  27. }
  28. public Folding[] GetRequiredFoldings()
  29. {
  30. return requiredFoldings;
  31. }
  32. public void SetFoldedState(bool folded)
  33. {
  34. if (folded)
  35. {
  36. Debug.Log($"Folding {gameObject.name} folded");
  37. }
  38. for(int i = 0; i < requiredFoldings.Length; i++)
  39. {
  40. requiredFoldings[i].gameObject.SetActive(folded);
  41. }
  42. this.folded = folded;
  43. }
  44. public bool IsFolded()
  45. {
  46. return folded;
  47. }
  48. public Plane GetFoldingPlane()
  49. {
  50. return rotationAxis.AsPlane();
  51. }
  52. public Vector3 GetFoldingPosition()
  53. {
  54. return rotationAxis.position;
  55. }
  56. public void SetBackFoldedVerticesIndices(int[] foldedVerticesIndices)
  57. {
  58. backFoldedVerticesIndices.AddRange(foldedVerticesIndices);
  59. }
  60. public void SetFrontFoldedVerticesIndices(int[] foldedVerticesIndices)
  61. {
  62. frontFoldedVerticesIndices.AddRange(foldedVerticesIndices);
  63. }
  64. public int[] GetBackFoldedVerticesIndices()
  65. {
  66. return backFoldedVerticesIndices.ToArray();
  67. }
  68. public int[] GetFrontFoldedVerticesIndices()
  69. {
  70. return frontFoldedVerticesIndices.ToArray();
  71. }
  72. public void ClearFoldedVertices()
  73. {
  74. backFoldedVerticesIndices.Clear();
  75. frontFoldedVerticesIndices.Clear();
  76. }
  77. }
  78. [System.Serializable]
  79. public struct RotationAxis
  80. {
  81. public Vector3 position;
  82. public float angle;
  83. public Vector3 AsVector()
  84. {
  85. Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
  86. return fourPlanePoints[2] - fourPlanePoints[1];
  87. }
  88. public Plane AsPlane()
  89. {
  90. Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
  91. return new Plane(fourPlanePoints[0], fourPlanePoints[2], fourPlanePoints[1]);
  92. }
  93. public Vector3[] AsLine()
  94. {
  95. Vector3[] fourPlanePoints = PlaneUtils.GetFourPlanePoints(position, angle);
  96. return new Vector3[] { fourPlanePoints[2], fourPlanePoints[1] };
  97. }
  98. }