MegaSpherifyWarp.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Spherify")]
  3. public class MegaSpherifyWarp : MegaWarp
  4. {
  5. public float percent = 0.0f;
  6. public float FallOff = 0.0f;
  7. float per;
  8. float xsize,ysize,zsize;
  9. float size;
  10. public override string WarpName() { return "Spherify"; }
  11. public override string GetHelpURL() { return "?page_id=322"; }
  12. public override Vector3 Map(int i, Vector3 p)
  13. {
  14. p = tm.MultiplyPoint3x4(p);
  15. Vector3 ip = p;
  16. float dist = p.magnitude;
  17. float dcy1 = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  18. float vdist = dist; //Mathf.Sqrt(xw * xw + yw * yw + zw * zw);
  19. float mfac = size / vdist;
  20. float dcy = Mathf.Exp(-FallOff * Mathf.Abs(vdist));
  21. p.x = p.x + (Mathf.Sign(p.x) * ((Mathf.Abs(p.x * mfac) - Mathf.Abs(p.x)) * per) * dcy);
  22. p.y = p.y + (Mathf.Sign(p.y) * ((Mathf.Abs(p.y * mfac) - Mathf.Abs(p.y)) * per) * dcy);
  23. p.z = p.z + (Mathf.Sign(p.z) * ((Mathf.Abs(p.z * mfac) - Mathf.Abs(p.z)) * per) * dcy);
  24. p = Vector3.Lerp(ip, p, dcy1);
  25. return invtm.MultiplyPoint3x4(p);
  26. }
  27. void Update()
  28. {
  29. Prepare(Decay);
  30. }
  31. public override bool Prepare(float decay)
  32. {
  33. tm = transform.worldToLocalMatrix;
  34. invtm = tm.inverse;
  35. totaldecay = Decay + decay;
  36. if ( totaldecay < 0.0f )
  37. totaldecay = 0.0f;
  38. xsize = Width; //bbox.max.x - bbox.min.x;
  39. ysize = Height; //bbox.max.y - bbox.min.y;
  40. zsize = Length; //bbox.max.z - bbox.min.z;
  41. size = (xsize > ysize) ? xsize : ysize;
  42. size = (zsize > size) ? zsize : size;
  43. size /= 2.0f;
  44. // Get the percentage to spherify at this time
  45. per = percent / 100.0f;
  46. return true;
  47. }
  48. }