EditablePath.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace UnityEditor.Experimental.Rendering.Universal.Path2D
  5. {
  6. [Serializable]
  7. internal class EditablePath : IEditablePath
  8. {
  9. [SerializeField]
  10. private ShapeType m_ShapeType;
  11. [SerializeField]
  12. private IndexedSelection m_Selection = new IndexedSelection();
  13. [SerializeField]
  14. private List<ControlPoint> m_ControlPoints = new List<ControlPoint>();
  15. [SerializeField]
  16. private bool m_IsOpenEnded;
  17. private Matrix4x4 m_LocalToWorldMatrix = Matrix4x4.identity;
  18. private Matrix4x4 m_WorldToLocalMatrix = Matrix4x4.identity;
  19. private Vector3 m_Forward = Vector3.forward;
  20. private Vector3 m_Up = Vector3.up;
  21. private Vector3 m_Right = Vector3.right;
  22. public ShapeType shapeType
  23. {
  24. get { return m_ShapeType; }
  25. set { m_ShapeType = value; }
  26. }
  27. public IUndoObject undoObject { get; set; }
  28. public Matrix4x4 localToWorldMatrix
  29. {
  30. get { return m_LocalToWorldMatrix; }
  31. set
  32. {
  33. m_LocalToWorldMatrix = value;
  34. m_WorldToLocalMatrix = value.inverse;
  35. }
  36. }
  37. public Vector3 forward
  38. {
  39. get { return m_Forward; }
  40. set { m_Forward = value; }
  41. }
  42. public Vector3 up
  43. {
  44. get { return m_Up; }
  45. set { m_Up = value; }
  46. }
  47. public Vector3 right
  48. {
  49. get { return m_Right; }
  50. set { m_Right = value; }
  51. }
  52. public Matrix4x4 worldToLocalMatrix
  53. {
  54. get { return m_WorldToLocalMatrix; }
  55. }
  56. public bool isOpenEnded
  57. {
  58. get
  59. {
  60. if (pointCount < 3)
  61. return true;
  62. return m_IsOpenEnded;
  63. }
  64. set { m_IsOpenEnded = value; }
  65. }
  66. public ISelection<int> selection
  67. {
  68. get { return m_Selection; }
  69. }
  70. public int pointCount
  71. {
  72. get { return m_ControlPoints.Count; }
  73. }
  74. public ControlPoint GetPoint(int index)
  75. {
  76. return TransformPoint(localToWorldMatrix, m_ControlPoints[index]);
  77. }
  78. public void SetPoint(int index, ControlPoint controlPoint)
  79. {
  80. m_ControlPoints[index] = TransformPoint(worldToLocalMatrix, controlPoint);
  81. }
  82. public void AddPoint(ControlPoint controlPoint)
  83. {
  84. m_ControlPoints.Insert(pointCount, TransformPoint(worldToLocalMatrix, controlPoint));
  85. }
  86. public void InsertPoint(int index, ControlPoint controlPoint)
  87. {
  88. m_ControlPoints.Insert(index, TransformPoint(worldToLocalMatrix, controlPoint));
  89. }
  90. public void RemovePoint(int index)
  91. {
  92. m_ControlPoints.RemoveAt(index);
  93. }
  94. public void Clear()
  95. {
  96. m_ControlPoints.Clear();
  97. }
  98. private ControlPoint TransformPoint(Matrix4x4 transformMatrix, ControlPoint controlPoint)
  99. {
  100. if (transformMatrix == Matrix4x4.identity)
  101. return controlPoint;
  102. var newControlPoint = new ControlPoint()
  103. {
  104. position = transformMatrix.MultiplyPoint3x4(controlPoint.position),
  105. tangentMode = controlPoint.tangentMode,
  106. continuousCache = controlPoint.continuousCache,
  107. brokenCache = controlPoint.brokenCache,
  108. mirrorLeft = controlPoint.mirrorLeft
  109. };
  110. newControlPoint.rightTangent = transformMatrix.MultiplyPoint3x4(controlPoint.rightTangent);
  111. newControlPoint.leftTangent = transformMatrix.MultiplyPoint3x4(controlPoint.leftTangent);
  112. return newControlPoint;
  113. }
  114. public bool Select(ISelector<Vector3> selector)
  115. {
  116. var changed = false;
  117. for (var i = 0; i < pointCount; ++i)
  118. changed |= selection.Select(i, selector.Select(GetPoint(i).position));
  119. return changed;
  120. }
  121. }
  122. }