MegaMeltWarp.cs 5.4 KB

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