OutlineCreator.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Shatter Toolkit
  2. // Copyright 2015 Gustav Olsson
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace ShatterToolkit.Examples
  6. {
  7. public class OutlineCreator : MonoBehaviour
  8. {
  9. protected List<Vector3> points = new List<Vector3>();
  10. protected List<int> edges = new List<int>();
  11. protected List<int> triangles = new List<int>();
  12. protected List<int> triangleEdges = new List<int>();
  13. protected bool isTriangulated = false;
  14. protected bool isLoopClosed = false;
  15. protected int loopStart = 0;
  16. public int LoopPointCount
  17. {
  18. get { return points.Count - loopStart; }
  19. }
  20. public void Reset()
  21. {
  22. points.Clear();
  23. edges.Clear();
  24. triangles.Clear();
  25. triangleEdges.Clear();
  26. isTriangulated = false;
  27. isLoopClosed = false;
  28. loopStart = 0;
  29. }
  30. public void Update()
  31. {
  32. if (Input.GetMouseButtonDown(0))
  33. {
  34. if (isTriangulated)
  35. {
  36. Reset();
  37. }
  38. // Add the mouse world position to the list of points
  39. Vector3 screenPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10.0f);
  40. Vector3 worldPosition = Camera.main.ScreenToWorldPoint(screenPosition);
  41. points.Add(worldPosition);
  42. // Add an edge from the previous point to the new point
  43. if (LoopPointCount >= 2)
  44. {
  45. edges.Add(points.Count - 2);
  46. edges.Add(points.Count - 1);
  47. }
  48. // Set flag
  49. isLoopClosed = false;
  50. }
  51. else if (Input.GetMouseButtonDown(1))
  52. {
  53. if (LoopPointCount >= 3)
  54. {
  55. // Add the last edge to close the current loop
  56. edges.Add(points.Count - 1);
  57. edges.Add(loopStart);
  58. // Set flag
  59. isLoopClosed = true;
  60. // Start a new loop
  61. loopStart = points.Count;
  62. }
  63. }
  64. if (Input.GetKeyDown(KeyCode.Space))
  65. {
  66. if (!isTriangulated && isLoopClosed)
  67. {
  68. // Triangulate
  69. int[] newEdges, newTriangles, newTriangleEdges;
  70. ITriangulator triangulator = new Triangulator(points, edges, Vector3.up);
  71. triangulator.Fill(out newEdges, out newTriangles, out newTriangleEdges);
  72. // Add the new edges and triangles
  73. edges.AddRange(newEdges);
  74. triangles.AddRange(newTriangles);
  75. triangleEdges.AddRange(newTriangleEdges);
  76. // Set flag
  77. isTriangulated = true;
  78. }
  79. }
  80. }
  81. public void OnGUI()
  82. {
  83. GUI.Box(new Rect(0, 0, 500, 100), "Please turn on Gizmos!\n" +
  84. "Create an outline by left-clicking in a clockwise order on the screen.\n" +
  85. "Right-click to close a loop.\n" +
  86. "Create a hole by left-clicking in a counter-clockwise order inside a shape.\n" +
  87. "Be careful not to overlap edges.\n" +
  88. "Press SPACE to triangulate the closed loops!");
  89. }
  90. public void OnDrawGizmos()
  91. {
  92. Gizmos.color = Color.black;
  93. // Draw points
  94. for (int i = 0; i < points.Count; i++)
  95. {
  96. Gizmos.DrawSphere(points[i], 0.1f);
  97. }
  98. // Draw edges
  99. for (int i = 0; i < edges.Count / 2; i++)
  100. {
  101. int edge = i * 2;
  102. Gizmos.DrawLine(points[edges[edge + 0]], points[edges[edge + 1]]);
  103. }
  104. // Draw triangles
  105. for (int i = 0; i < triangles.Count / 3; i++)
  106. {
  107. int triangle = i * 3;
  108. Vector3 center = (points[triangles[triangle + 0]] + points[triangles[triangle + 1]] + points[triangles[triangle + 2]]) / 3.0f;
  109. // Draw a red line from the triangle center to first triangle edge
  110. Gizmos.color = Color.red;
  111. Gizmos.DrawLine(center, (points[edges[triangleEdges[triangle + 0] * 2 + 0]] + points[edges[triangleEdges[triangle + 0] * 2 + 1]]) * 0.5f);
  112. // Draw a green line from the triangle center to the second triangle edge
  113. Gizmos.color = Color.green;
  114. Gizmos.DrawLine(center, (points[edges[triangleEdges[triangle + 1] * 2 + 0]] + points[edges[triangleEdges[triangle + 1] * 2 + 1]]) * 0.5f);
  115. // Draw a blue line from the triangle center to the third triangle edge
  116. Gizmos.color = Color.blue;
  117. Gizmos.DrawLine(center, (points[edges[triangleEdges[triangle + 2] * 2 + 0]] + points[edges[triangleEdges[triangle + 2] * 2 + 1]]) * 0.5f);
  118. }
  119. }
  120. }
  121. }