MegaCylindrifyWarp.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/Warps/Cylindrify")]
  3. public class MegaCylindrifyWarp : MegaWarp
  4. {
  5. public float Percent = 0.0f;
  6. public override string WarpName() { return "Cylindrify"; }
  7. public override string GetHelpURL() { return "?page_id=166"; }
  8. float size1;
  9. float per;
  10. public override Vector3 Map(int i, Vector3 p)
  11. {
  12. p = tm.MultiplyPoint3x4(p);
  13. Vector3 ip = p;
  14. float dist = p.magnitude;
  15. float dcy = Mathf.Exp(-totaldecay * Mathf.Abs(dist));
  16. float k = ((size1 / Mathf.Sqrt(p.x * p.x + p.z * p.z) / 2.0f - 1.0f) * per * dcy) + 1.0f;
  17. p.x *= k;
  18. p.z *= k;
  19. p = Vector3.Lerp(ip, p, dcy);
  20. return invtm.MultiplyPoint3x4(p);
  21. }
  22. void Update()
  23. {
  24. Prepare(Decay);
  25. }
  26. public MegaAxis axis;
  27. Matrix4x4 mat = new Matrix4x4();
  28. public override bool Prepare(float decay)
  29. {
  30. totaldecay = Decay + decay;
  31. if ( totaldecay < 0.0f )
  32. totaldecay = 0.0f;
  33. tm = transform.worldToLocalMatrix;
  34. invtm = tm.inverse;
  35. mat = Matrix4x4.identity;
  36. switch ( axis )
  37. {
  38. case MegaAxis.X: MegaMatrix.RotateZ(ref mat, Mathf.PI * 0.5f); break;
  39. case MegaAxis.Y: MegaMatrix.RotateX(ref mat, -Mathf.PI * 0.5f); break;
  40. case MegaAxis.Z: break;
  41. }
  42. SetAxis(mat);
  43. float xsize = Width; //bbox.max.x - bbox.min.x;
  44. float zsize = Length; //bbox.max.z - bbox.min.z;
  45. size1 = (xsize > zsize) ? xsize : zsize;
  46. // Get the percentage to spherify at this time
  47. per = Percent / 100.0f;
  48. return true;
  49. }
  50. }