MegaVolSelect.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Selection/Volume")]
  3. public class MegaVolSelect : MegaSelectionMod
  4. {
  5. public override MegaModChannel ChannelsReq() { return MegaModChannel.Col | MegaModChannel.Verts; }
  6. public override string ModName() { return "Vol Select"; }
  7. public override string GetHelpURL() { return "?page_id=1307"; }
  8. float[] modselection;
  9. public float[] GetSel() { return modselection; }
  10. public Vector3 origin = Vector3.zero;
  11. public float falloff = 1.0f;
  12. public float radius = 1.0f;
  13. public Color gizCol = new Color(0.5f, 0.5f, 0.5f, 0.25f);
  14. public float gizSize = 0.01f;
  15. public bool useCurrentVerts = true;
  16. public bool displayWeights = true;
  17. public bool freezeSelection = false;
  18. public MegaVolumeType volType = MegaVolumeType.Sphere;
  19. public Vector3 boxsize = Vector3.one;
  20. public float weight = 1.0f;
  21. public bool inverse = false;
  22. public Transform target;
  23. float GetDistBox(Vector3 p)
  24. {
  25. // Work in the box's coordinate system.
  26. Vector3 diff = p - origin;
  27. float sqrDistance = 0.0f;
  28. float delta;
  29. Vector3 closest = diff;
  30. if ( closest.x < -boxsize.x )
  31. {
  32. delta = closest.x + boxsize.x;
  33. sqrDistance += delta * delta;
  34. closest.x = -boxsize.x;
  35. }
  36. else
  37. {
  38. if ( closest.x > boxsize.x )
  39. {
  40. delta = closest.x - boxsize.x;
  41. sqrDistance += delta * delta;
  42. closest.x = boxsize.x;
  43. }
  44. }
  45. if ( closest.y < -boxsize.y )
  46. {
  47. delta = closest.y + boxsize.y;
  48. sqrDistance += delta * delta;
  49. closest.y = -boxsize.y;
  50. }
  51. else
  52. {
  53. if ( closest.y > boxsize.y )
  54. {
  55. delta = closest.y - boxsize.y;
  56. sqrDistance += delta * delta;
  57. closest.y = boxsize.y;
  58. }
  59. }
  60. if ( closest.z < -boxsize.z )
  61. {
  62. delta = closest.z + boxsize.z;
  63. sqrDistance += delta * delta;
  64. closest.z = -boxsize.z;
  65. }
  66. else
  67. {
  68. if ( closest.z > boxsize.z )
  69. {
  70. delta = closest.z - boxsize.z;
  71. sqrDistance += delta * delta;
  72. closest.z = boxsize.z;
  73. }
  74. }
  75. return Mathf.Sqrt(sqrDistance); // * 0.5f;
  76. }
  77. public override void GetSelection(MegaModifiers mc)
  78. {
  79. if ( target )
  80. {
  81. origin = transform.worldToLocalMatrix.MultiplyPoint(target.position);
  82. }
  83. if ( modselection == null || modselection.Length != mc.verts.Length )
  84. {
  85. modselection = new float[mc.verts.Length];
  86. }
  87. if ( freezeSelection )
  88. {
  89. mc.selection = modselection;
  90. return;
  91. }
  92. // we dont need to update if nothing changes
  93. if ( volType == MegaVolumeType.Sphere )
  94. {
  95. if ( useCurrentVerts )
  96. {
  97. if ( inverse )
  98. {
  99. for ( int i = 0; i < verts.Length; i++ )
  100. {
  101. float d = Vector3.Distance(origin, verts[i]) - radius;
  102. if ( d < 0.0f )
  103. modselection[i] = weight;
  104. else
  105. {
  106. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  107. modselection[i] = w * weight; //mc.cols[i][c];
  108. }
  109. modselection[i] = 1.0f - modselection[i];
  110. if ( modselection[i] < 0.0f )
  111. modselection[i] = 0.0f;
  112. }
  113. }
  114. else
  115. {
  116. for ( int i = 0; i < verts.Length; i++ )
  117. {
  118. float d = Vector3.Distance(origin, verts[i]) - radius;
  119. if ( d < 0.0f )
  120. modselection[i] = weight;
  121. else
  122. {
  123. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  124. modselection[i] = w * weight; //mc.cols[i][c];
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. if ( inverse )
  132. {
  133. for ( int i = 0; i < verts.Length; i++ )
  134. {
  135. float d = Vector3.Distance(origin, verts[i]) - radius;
  136. if ( d < 0.0f )
  137. modselection[i] = weight;
  138. else
  139. {
  140. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  141. modselection[i] = w * weight; //mc.cols[i][c];
  142. }
  143. modselection[i] = 1.0f - modselection[i];
  144. if ( modselection[i] < 0.0f )
  145. modselection[i] = 0.0f;
  146. }
  147. }
  148. else
  149. {
  150. for ( int i = 0; i < verts.Length; i++ )
  151. {
  152. float d = Vector3.Distance(origin, verts[i]) - radius;
  153. if ( d < 0.0f )
  154. modselection[i] = weight;
  155. else
  156. {
  157. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  158. modselection[i] = w * weight; //mc.cols[i][c];
  159. }
  160. }
  161. }
  162. }
  163. }
  164. else
  165. {
  166. if ( useCurrentVerts )
  167. {
  168. if ( inverse )
  169. {
  170. for ( int i = 0; i < verts.Length; i++ )
  171. {
  172. float d = GetDistBox(verts[i]);
  173. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  174. modselection[i] = 1.0f - (w * weight); //mc.cols[i][c];
  175. if ( modselection[i] < 0.0f )
  176. modselection[i] = 0.0f;
  177. }
  178. }
  179. else
  180. {
  181. for ( int i = 0; i < verts.Length; i++ )
  182. {
  183. float d = GetDistBox(verts[i]);
  184. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  185. modselection[i] = w * weight; //mc.cols[i][c];
  186. }
  187. }
  188. }
  189. else
  190. {
  191. if ( inverse )
  192. {
  193. for ( int i = 0; i < verts.Length; i++ )
  194. {
  195. float d = GetDistBox(verts[i]);
  196. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  197. modselection[i] = 1.0f - (w * weight); //mc.cols[i][c];
  198. if ( modselection[i] < 0.0f )
  199. modselection[i] = 0.0f;
  200. }
  201. }
  202. else
  203. {
  204. for ( int i = 0; i < verts.Length; i++ )
  205. {
  206. float d = GetDistBox(verts[i]);
  207. float w = Mathf.Exp(-falloff * Mathf.Abs(d));
  208. modselection[i] = w * weight; //mc.cols[i][c];
  209. }
  210. }
  211. }
  212. }
  213. // We only need the copy if we are first mod
  214. if ( (mc.dirtyChannels & MegaModChannel.Verts) == 0 )
  215. mc.InitVertSource();
  216. mc.selection = modselection;
  217. }
  218. public override void DrawGizmo(MegaModContext context)
  219. {
  220. if ( ModEnabled )
  221. {
  222. base.DrawGizmo(context);
  223. Matrix4x4 tm = gameObject.transform.localToWorldMatrix;
  224. Gizmos.matrix = tm;
  225. if ( enabled && volType == MegaVolumeType.Box )
  226. {
  227. Gizmos.color = Color.yellow;
  228. Gizmos.DrawWireCube(origin, boxsize * 2.0f); // * 0.5f);
  229. }
  230. if ( enabled && volType == MegaVolumeType.Sphere )
  231. {
  232. Gizmos.color = Color.yellow;
  233. Gizmos.DrawWireSphere(origin, radius); // * 0.5f);
  234. }
  235. Gizmos.matrix = Matrix4x4.identity;
  236. }
  237. }
  238. }