MegaCurveSculpt.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using UnityEngine;
  2. using System.Threading;
  3. [AddComponentMenu("Modifiers/Curve Sculpt")]
  4. public class MegaCurveSculpt : MegaModifier
  5. {
  6. public AnimationCurve defCurveX = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(1.0f, 0.0f));
  7. public AnimationCurve defCurveY = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(1.0f, 0.0f));
  8. public AnimationCurve defCurveZ = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(1.0f, 0.0f));
  9. public AnimationCurve defCurveSclX = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 1.0f));
  10. public AnimationCurve defCurveSclY = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 1.0f));
  11. public AnimationCurve defCurveSclZ = new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 1.0f));
  12. public Vector3 OffsetAmount = Vector3.one;
  13. public Vector3 ScaleAmount = Vector3.one;
  14. Vector3 size = Vector3.zero;
  15. public MegaAxis offsetX = MegaAxis.X;
  16. public MegaAxis offsetY = MegaAxis.Y;
  17. public MegaAxis offsetZ = MegaAxis.Z;
  18. public MegaAxis scaleX = MegaAxis.X;
  19. public MegaAxis scaleY = MegaAxis.Y;
  20. public MegaAxis scaleZ = MegaAxis.Z;
  21. public bool symX = false;
  22. public bool symY = false;
  23. public bool symZ = false;
  24. public override string ModName() { return "CurveSculpt"; }
  25. public override string GetHelpURL() { return "?page_id=655"; }
  26. static object resourceLock = new object();
  27. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  28. {
  29. if ( selection != null )
  30. {
  31. DoWorkWeighted(mc, index, start, end, cores);
  32. return;
  33. }
  34. for ( int i = start; i < end; i++ )
  35. sverts[i] = MapMT(i, verts[i]);
  36. }
  37. public override void DoWorkWeighted(MegaModifiers mc, int index, int start, int end, int cores)
  38. {
  39. for ( int i = start; i < end; i++ )
  40. {
  41. Vector3 p = verts[i];
  42. float w = selection[i]; //[(int)weightChannel];
  43. if ( w > 0.001f )
  44. {
  45. Vector3 mp = MapMT(i, verts[i]);
  46. sverts[i].x = p.x + (mp.x - p.x) * w;
  47. sverts[i].y = p.y + (mp.y - p.y) * w;
  48. sverts[i].z = p.z + (mp.z - p.z) * w;
  49. }
  50. else
  51. sverts[i] = p; //verts[i];
  52. }
  53. }
  54. public Vector3 MapMT(int i, Vector3 p)
  55. {
  56. float alpha = 0.0f;
  57. p = tm.MultiplyPoint3x4(p);
  58. alpha = (p.x - bbox.min.x) / size.x;
  59. Monitor.Enter(resourceLock);
  60. p[(int)scaleX] *= defCurveSclX.Evaluate(alpha) * ScaleAmount.x;
  61. p[(int)offsetX] += defCurveX.Evaluate(alpha) * OffsetAmount.x;
  62. alpha = (p.y - bbox.min.y) / size.y;
  63. p[(int)scaleY] *= defCurveSclY.Evaluate(alpha) * ScaleAmount.y;
  64. p[(int)offsetY] += defCurveY.Evaluate(alpha) * OffsetAmount.y;
  65. alpha = (p.z - bbox.min.z) / size.z;
  66. p[(int)scaleZ] *= defCurveSclZ.Evaluate(alpha) * ScaleAmount.z;
  67. p[(int)offsetZ] += defCurveZ.Evaluate(alpha) * OffsetAmount.z;
  68. Monitor.Exit(resourceLock);
  69. return invtm.MultiplyPoint3x4(p);
  70. }
  71. public override Vector3 Map(int i, Vector3 p)
  72. {
  73. float alpha = 0.0f;
  74. p = tm.MultiplyPoint3x4(p);
  75. alpha = (p.x - bbox.min.x) / size.x;
  76. p[(int)scaleX] *= defCurveSclX.Evaluate(alpha) * ScaleAmount.x;
  77. p[(int)offsetX] += defCurveX.Evaluate(alpha) * OffsetAmount.x;
  78. alpha = (p.y - bbox.min.y) / size.y;
  79. p[(int)scaleY] *= defCurveSclY.Evaluate(alpha) * ScaleAmount.y;
  80. p[(int)offsetY] += defCurveY.Evaluate(alpha) * OffsetAmount.y;
  81. alpha = (p.z - bbox.min.z) / size.z;
  82. p[(int)scaleZ] *= defCurveSclZ.Evaluate(alpha) * ScaleAmount.z;
  83. p[(int)offsetZ] += defCurveZ.Evaluate(alpha) * OffsetAmount.z;
  84. return invtm.MultiplyPoint3x4(p);
  85. }
  86. public override bool ModLateUpdate(MegaModContext mc)
  87. {
  88. return Prepare(mc);
  89. }
  90. public override bool Prepare(MegaModContext mc)
  91. {
  92. size = bbox.max - bbox.min;
  93. return true;
  94. }
  95. }