GizmoUtility.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Magica Cloth.
  2. // Copyright (c) MagicaSoft, 2020-2022.
  3. // https://magicasoft.jp
  4. using UnityEngine;
  5. namespace MagicaCloth
  6. {
  7. public static class GizmoUtility
  8. {
  9. // ギズモカラー定義
  10. public static readonly Color ColorDynamic = new Color(1.0f, 1.0f, 1.0f);
  11. public static readonly Color ColorKinematic = new Color(1.0f, 1.0f, 0.0f);
  12. public static readonly Color ColorInvalid = new Color(0.5f, 0.5f, 0.5f);
  13. public static readonly Color ColorCollider = new Color(0.0f, 1.0f, 0.0f);
  14. public static readonly Color ColorNonSelectedCollider = new Color(0.5f, 0.3f, 0.0f);
  15. public static readonly Color ColorTriangle = new Color(1.0f, 0.0f, 1.0f);
  16. public static readonly Color ColorStructLine = new Color(0.0f, 1.0f, 1.0f);
  17. public static readonly Color ColorBendLine = new Color(0.0f, 0.5f, 1.0f);
  18. public static readonly Color ColorNearLine = new Color(0.55f, 0.5f, 0.7f);
  19. public static readonly Color ColorRotationLine = new Color(1.0f, 0.65f, 0.0f);
  20. public static readonly Color ColorAdjustLine = new Color(1.0f, 1.0f, 0.0f);
  21. public static readonly Color ColorAirLine = new Color(0.55f, 0.5f, 0.7f);
  22. public static readonly Color ColorBasePosition = new Color(1.0f, 0.0f, 0.0f);
  23. public static readonly Color ColorDirectionMoveLimit = new Color(0.0f, 1.0f, 1.0f);
  24. public static readonly Color ColorPenetration = new Color(1.0f, 0.3f, 0.0f);
  25. public static readonly Color ColorCollisionNormal = new Color(0.6f, 0.2f, 1.0f);
  26. public static readonly Color ColorVelocity = new Color(1.0f, 0.6f, 0.2f);
  27. public static readonly Color ColorSkinningBone = new Color(1.0f, 0.5f, 0.0f);
  28. public static readonly Color ColorDeformerPoint = new Color(1.0f, 1.0f, 1.0f);
  29. public static readonly Color ColorDeformerPointRange = new Color(0.5f, 0.2f, 0.0f);
  30. public static readonly Color ColorWind = new Color(0.55f, 0.592f, 0.796f);
  31. /// <summary>
  32. /// ワイヤーカプセルを描画する
  33. /// </summary>
  34. /// <param name="pos">基準座標</param>
  35. /// <param name="rot">基準回転</param>
  36. /// <param name="ldir">カプセルの方向</param>
  37. /// <param name="lup">カプセルの上方向</param>
  38. /// <param name="length">カプセルの長さ(片側)</param>
  39. /// <param name="startRadius">始点の半径</param>
  40. /// <param name="endRadius">終点の半径</param>
  41. public static void DrawWireCapsule(
  42. Vector3 pos, Quaternion rot, Vector3 scl,
  43. Vector3 ldir, Vector3 lup,
  44. float length, float startRadius, float endRadius,
  45. bool resetMatrix = true
  46. )
  47. {
  48. //Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  49. Gizmos.matrix = Matrix4x4.TRS(pos, rot, scl);
  50. var l = ldir * length;
  51. Gizmos.DrawWireSphere(-l, startRadius);
  52. Gizmos.DrawWireSphere(l, endRadius);
  53. for (int i = 0; i < 360; i += 45)
  54. {
  55. var q = Quaternion.AngleAxis(i, ldir);
  56. var up1 = q * (lup * startRadius);
  57. var up2 = q * (lup * endRadius);
  58. Gizmos.DrawLine(-l + up1, l + up2);
  59. }
  60. // 45度ずらしてもう1回球を描く
  61. Gizmos.matrix = Matrix4x4.TRS(pos, rot * Quaternion.AngleAxis(45, ldir), scl);
  62. Gizmos.DrawWireSphere(-l, startRadius);
  63. Gizmos.DrawWireSphere(l, endRadius);
  64. if (resetMatrix)
  65. Gizmos.matrix = Matrix4x4.identity;
  66. }
  67. /// <summary>
  68. /// ワイヤー球を描画する
  69. /// </summary>
  70. /// <param name="pos">基準座標</param>
  71. /// <param name="rot">基準回転</param>
  72. /// <param name="radius">半径</param>
  73. /// <param name="resetMatrix"></param>
  74. public static void DrawWireSphere(
  75. Vector3 pos, Quaternion rot, Vector3 scl, float radius,
  76. bool drawSphere, bool drawAxis,
  77. bool resetMatrix = true)
  78. {
  79. //Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  80. Gizmos.matrix = Matrix4x4.TRS(pos, rot, scl);
  81. // 球
  82. if (drawSphere)
  83. Gizmos.DrawWireSphere(Vector3.zero, radius);
  84. // 軸
  85. if (drawAxis)
  86. {
  87. const float axisRadius = 0.03f;
  88. Gizmos.color = Color.red;
  89. Gizmos.DrawLine(Vector3.zero, Vector3.right * axisRadius);
  90. Gizmos.color = Color.green;
  91. Gizmos.DrawLine(Vector3.zero, Vector3.up * axisRadius);
  92. Gizmos.color = Color.blue;
  93. Gizmos.DrawLine(Vector3.zero, Vector3.forward * axisRadius);
  94. }
  95. // 45度ずらしてもう1回球を描く
  96. //Gizmos.matrix = Matrix4x4.TRS(pos, rot * Quaternion.AngleAxis(45, Vector3.up), Vector3.one);
  97. //Gizmos.DrawWireSphere(Vector3.zero, radius);
  98. if (resetMatrix)
  99. Gizmos.matrix = Matrix4x4.identity;
  100. }
  101. /// <summary>
  102. /// ワイヤーボックスを描画する
  103. /// </summary>
  104. /// <param name="pos"></param>
  105. /// <param name="rot"></param>
  106. /// <param name="size"></param>
  107. /// <param name="resetMatrix"></param>
  108. public static void DrawWireCube(Vector3 pos, Quaternion rot, Vector3 size, bool resetMatrix = true)
  109. {
  110. Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  111. Gizmos.DrawWireCube(Vector3.zero, size);
  112. if (resetMatrix)
  113. Gizmos.matrix = Matrix4x4.identity;
  114. }
  115. public static void DrawWireCone(Vector3 pos, Quaternion rot, float length, float radius, int div = 8)
  116. {
  117. Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  118. var epos = Vector3.forward * length;
  119. Vector3 oldpos = epos;
  120. for (int i = 0; i < div; i++)
  121. {
  122. float t = (float)i / (float)div;
  123. var q = Quaternion.AngleAxis(t * 360.0f, Vector3.forward);
  124. var x = q * Vector3.right * radius;
  125. Gizmos.DrawLine(Vector3.zero, epos + x);
  126. Gizmos.DrawLine(epos, epos + x);
  127. if (i > 0)
  128. Gizmos.DrawLine(oldpos, epos + x);
  129. oldpos = epos + x;
  130. }
  131. Gizmos.DrawLine(oldpos, epos + Vector3.right * radius);
  132. Gizmos.matrix = Matrix4x4.identity;
  133. }
  134. /// <summary>
  135. /// ワイヤー矢印を描画する
  136. /// </summary>
  137. /// <param name="pos"></param>
  138. /// <param name="rot"></param>
  139. /// <param name="size"></param>
  140. /// <param name="cross">十字描画</param>
  141. public static void DrawWireArrow(Vector3 pos, Quaternion rot, Vector3 size, bool cross = false)
  142. {
  143. Gizmos.matrix = Matrix4x4.TRS(pos, rot, size);
  144. Vector3[] points = new Vector3[]
  145. {
  146. new Vector3(0.0f, 0.0f, -1.0f),
  147. new Vector3(0.0f, 0.5f, -1.0f),
  148. new Vector3(0.0f, 0.5f, 0.0f),
  149. new Vector3(0.0f, 1.0f, 0.0f),
  150. new Vector3(0.0f, 0.0f, 1.0f),
  151. };
  152. float addAngle = cross ? 90.0f : 180.0f;
  153. int loop = cross ? 4 : 2;
  154. for (int j = 0; j < loop; j++)
  155. {
  156. for (int i = 0; i < points.Length - 1; i++)
  157. {
  158. Gizmos.DrawLine(points[i], points[i + 1]);
  159. }
  160. rot = rot * Quaternion.AngleAxis(addAngle, Vector3.forward);
  161. Gizmos.matrix = Matrix4x4.TRS(pos, rot, size);
  162. }
  163. Gizmos.matrix = Matrix4x4.identity;
  164. }
  165. /// <summary>
  166. /// XYZ軸を描画する
  167. /// </summary>
  168. /// <param name="pos"></param>
  169. /// <param name="rot"></param>
  170. /// <param name="size"></param>
  171. /// <param name="resetMatrix"></param>
  172. public static void DrawAxis(Vector3 pos, Quaternion rot, float size, bool resetMatrix = true)
  173. {
  174. Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  175. Gizmos.color = Color.red;
  176. Gizmos.DrawRay(Vector3.zero, Vector3.right * size);
  177. Gizmos.color = Color.green;
  178. Gizmos.DrawRay(Vector3.zero, Vector3.up * size);
  179. Gizmos.color = Color.blue;
  180. Gizmos.DrawRay(Vector3.zero, Vector3.forward * size);
  181. if (resetMatrix)
  182. Gizmos.matrix = Matrix4x4.identity;
  183. }
  184. /// <summary>
  185. /// ボーン形状を描画する
  186. /// </summary>
  187. /// <param name="pos"></param>
  188. /// <param name="tpos"></param>
  189. /// <param name="size"></param>
  190. public static void DrawBone(Vector3 pos, Vector3 tpos, float size)
  191. {
  192. var v = tpos - pos;
  193. var rot = Quaternion.FromToRotation(Vector3.forward, v);
  194. Gizmos.matrix = Matrix4x4.TRS(pos, rot, Vector3.one);
  195. Gizmos.color = ColorSkinningBone;
  196. Gizmos.DrawWireSphere(Vector3.zero, size);
  197. //Gizmos.DrawLine(Vector3.zero, Vector3.forward * v.magnitude);
  198. float bsize = size * 0.8f;
  199. float zoff = size;
  200. var gpos = Vector3.forward * v.magnitude;
  201. var p0 = new Vector3(bsize, bsize, zoff);
  202. var p1 = new Vector3(bsize, -bsize, zoff);
  203. var p2 = new Vector3(-bsize, -bsize, zoff);
  204. var p3 = new Vector3(-bsize, bsize, zoff);
  205. Gizmos.DrawLine(p0, gpos);
  206. Gizmos.DrawLine(p1, gpos);
  207. Gizmos.DrawLine(p2, gpos);
  208. Gizmos.DrawLine(p3, gpos);
  209. Gizmos.DrawLine(p0, p1);
  210. Gizmos.DrawLine(p1, p2);
  211. Gizmos.DrawLine(p2, p3);
  212. Gizmos.DrawLine(p3, p0);
  213. Gizmos.matrix = Matrix4x4.identity;
  214. }
  215. }
  216. }