MegaMelt.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using UnityEngine;
  2. public enum MegaMeltMat
  3. {
  4. Ice = 0,
  5. Glass,
  6. Jelly,
  7. Plastic,
  8. Custom,
  9. }
  10. [AddComponentMenu("Modifiers/Melt")]
  11. public class MegaMelt : MegaModifier
  12. {
  13. public float Amount = 0.0f;
  14. public float Spread = 19.0f;
  15. public MegaMeltMat MaterialType = MegaMeltMat.Ice;
  16. public float Solidity = 1.0f;
  17. public MegaAxis axis = MegaAxis.X;
  18. public bool FlipAxis = false;
  19. float zba = 0.0f;
  20. public float flatness = 0.0f;
  21. float size = 0.0f;
  22. float bulger = 0.0f;
  23. float ybr,zbr,visvaluea;
  24. int confiner,vistypea;
  25. float cx,cy,cz;
  26. float xsize,ysize,zsize;
  27. float ooxsize,ooysize,oozsize;
  28. public override string ModName() { return "Melt"; }
  29. public override string GetHelpURL() { return "?page_id=225"; }
  30. float hypot(float x, float y)
  31. {
  32. return Mathf.Sqrt(x * x + y * y);
  33. }
  34. public override Vector3 Map(int i, Vector3 p)
  35. {
  36. float x, y, z;
  37. float xw,yw,zw;
  38. float vdist,mfac,dx,dy;
  39. float defsinex = 0.0f, coldef = 0.0f, realmax = 0.0f;
  40. // Mult by mc
  41. p = tm.MultiplyPoint3x4(p);
  42. x = p.x; y = p.y; z = p.z;
  43. xw = x - cx; yw = y - cy; zw = z - cz;
  44. if ( xw == 0.0f && yw == 0.0f && zw == 0.0f ) xw = yw = zw = 1.0f;
  45. if ( x == 0.0f && y == 0.0f && z == 0.0f ) x = y = z = 1.0f;
  46. // Find distance from centre
  47. vdist = Mathf.Sqrt(xw * xw + yw * yw + zw * zw);
  48. mfac = size / vdist;
  49. if ( axis == MegaAxis.Y )
  50. {
  51. dx = xw + Mathf.Sign(xw) * ((Mathf.Abs(xw * mfac)) * (bulger * ybr));
  52. dy = yw + Mathf.Sign(yw) * ((Mathf.Abs(yw * mfac)) * (bulger * ybr));
  53. x = (dx + cx);
  54. y = (dy + cy);
  55. }
  56. if ( axis == MegaAxis.Z ) //Y )
  57. {
  58. dx = xw + Mathf.Sign(xw) * ((Mathf.Abs(xw * mfac)) * (bulger * ybr));
  59. dy = zw + Mathf.Sign(zw) * ((Mathf.Abs(zw * mfac)) * (bulger * ybr));
  60. x = (dx + cx);
  61. z = (dy + cz);
  62. }
  63. if ( axis == MegaAxis.X ) //Z )
  64. {
  65. dx = zw + Mathf.Sign(zw) * ((Mathf.Abs(zw * mfac)) * (bulger * ybr));
  66. dy = yw + Mathf.Sign(yw) * ((Mathf.Abs(yw * mfac)) * (bulger * ybr));
  67. z = (dx + cz);
  68. y = (dy + cy);
  69. }
  70. if ( axis == MegaAxis.Y ) if ( p.z < (bbox.min.z + zbr) ) goto skipmelt;
  71. if ( axis == MegaAxis.Z ) if ( p.y < (bbox.min.y + zbr) ) goto skipmelt;
  72. if ( axis == MegaAxis.X ) if ( p.x < (bbox.min.x + zbr) ) goto skipmelt;
  73. if ( axis == MegaAxis.Y ) realmax = hypot((bbox.max.x - cx), (bbox.max.y - cy));
  74. if ( axis == MegaAxis.Z ) realmax = hypot((bbox.max.x - cx), (bbox.max.z - cz));
  75. if ( axis == MegaAxis.X ) realmax = hypot((bbox.max.z - cz), (bbox.max.y - cy));
  76. if ( axis == MegaAxis.Y )
  77. {
  78. defsinex = hypot((x - cx), (y - cy));
  79. coldef = realmax - hypot((x - cx), (y - cy));
  80. }
  81. if ( axis == MegaAxis.Z )
  82. {
  83. defsinex = hypot((x - cx), (z - cz));
  84. coldef = realmax - hypot((x - cx), (z - cz));
  85. }
  86. if ( axis == MegaAxis.X )
  87. {
  88. defsinex = hypot((z - cz), (y - cy));
  89. coldef = realmax - hypot((z - cz), (y - cy));
  90. }
  91. if ( coldef < 0.0f )
  92. coldef = 0.0f;
  93. defsinex += (coldef / visvaluea);
  94. if ( axis == MegaAxis.Y )
  95. {
  96. if ( FlipAxis )
  97. {
  98. float nminz = bbox.min.z + (((z - bbox.min.z) * oozsize) * flatness);
  99. z -= (defsinex * bulger);
  100. if ( z <= nminz ) z = nminz;
  101. if ( z <= (nminz + zbr) ) z = (nminz + zbr);
  102. }
  103. else
  104. {
  105. float nmaxz = bbox.max.z - (((bbox.max.z - z) * oozsize) * flatness);
  106. z += (defsinex * bulger);
  107. if ( z >= nmaxz ) z = nmaxz;
  108. if ( z >= (nmaxz + zbr) ) z = (nmaxz + zbr);
  109. }
  110. }
  111. if ( axis == MegaAxis.Z )
  112. {
  113. if ( !FlipAxis )
  114. {
  115. float nminy = bbox.min.y + (((y - bbox.min.y) * ooysize) * flatness);
  116. y -= (defsinex * bulger);
  117. if ( y <= nminy ) y = nminy;
  118. if ( y <= (nminy + zbr) ) y = (nminy + zbr);
  119. }
  120. else
  121. {
  122. float nmaxy = bbox.max.y - (((bbox.max.y - y) * ooysize) * flatness);
  123. y += (defsinex * bulger);
  124. if ( y >= nmaxy ) y = nmaxy;
  125. if ( y >= (nmaxy + zbr) ) y = (nmaxy + zbr);
  126. }
  127. }
  128. if ( axis == MegaAxis.X )
  129. {
  130. if ( !FlipAxis )
  131. {
  132. float nminx = bbox.min.x + (((x - bbox.min.x) * ooxsize) * flatness);
  133. x -= (defsinex * bulger);
  134. if ( x <= nminx ) x = nminx;
  135. if ( x <= (nminx + zbr) ) x = (nminx + zbr);
  136. }
  137. else
  138. {
  139. float nmaxx = bbox.max.x - (((bbox.max.x - x) * ooxsize) * flatness);
  140. x += (defsinex * bulger);
  141. if ( x >= nmaxx ) x = nmaxx;
  142. if ( x >= (nmaxx + zbr) ) x = (nmaxx + zbr);
  143. }
  144. }
  145. // [jump point] don't melt this point...
  146. skipmelt:
  147. p.x = x; p.y = y; p.z = z;
  148. p = invtm.MultiplyPoint3x4(p);
  149. return p;
  150. }
  151. public override void ModStart(MegaModifiers mc)
  152. {
  153. cx = bbox.center.x;
  154. cy = bbox.center.y;
  155. cz = bbox.center.z;
  156. // Compute the size and center
  157. xsize = (bbox.max.x - bbox.min.x);
  158. ysize = (bbox.max.y - bbox.min.y);
  159. zsize = (bbox.max.z - bbox.min.z);
  160. size = (xsize > ysize) ? xsize : ysize;
  161. size = (zsize > size) ? zsize : size;
  162. size /= 2.0f;
  163. }
  164. public override bool ModLateUpdate(MegaModContext mc)
  165. {
  166. return Prepare(mc);
  167. }
  168. public override bool Prepare(MegaModContext mc)
  169. {
  170. cx = bbox.center.x;
  171. cy = bbox.center.y;
  172. cz = bbox.center.z;
  173. // Compute the size and center
  174. xsize = (bbox.max.x - bbox.min.x);
  175. ysize = (bbox.max.y - bbox.min.y);
  176. zsize = (bbox.max.z - bbox.min.z);
  177. ooxsize = 1.0f / xsize;
  178. ooysize = 1.0f / ysize;
  179. oozsize = 1.0f / zsize;
  180. size = (xsize > ysize) ? xsize : ysize;
  181. size = (zsize > size) ? zsize : size;
  182. size /= 2.0f;
  183. switch ( MaterialType )
  184. {
  185. case MegaMeltMat.Ice: visvaluea = 2.0f; break;
  186. case MegaMeltMat.Glass: visvaluea = 12.0f; break;
  187. case MegaMeltMat.Jelly: visvaluea = 0.4f; break;
  188. case MegaMeltMat.Plastic: visvaluea = 0.7f; break;
  189. case MegaMeltMat.Custom: visvaluea = Solidity; break;
  190. }
  191. if ( Amount < 0.0f )
  192. Amount = 0.0f;
  193. ybr = Spread / 100.0f;
  194. zbr = zba / 10.0f;
  195. bulger = Amount / 100.0f;
  196. return true;
  197. }
  198. }