MegaUVAdjust.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using UnityEngine;
  2. // Perhaps we should have UVModifier
  3. [AddComponentMenu("Modifiers/UV/Adjust")]
  4. public class MegaUVAdjust : MegaModifier
  5. {
  6. public bool animate = false;
  7. public float rotspeed = 0.0f;
  8. public float spiralspeed = 0.0f;
  9. public Vector3 speed = Vector3.zero;
  10. public float spiral = 0.0f;
  11. public float spirallim = 360.0f;
  12. // 3 channels of UV
  13. public override MegaModChannel ChannelsReq() { return MegaModChannel.UV; }
  14. public override MegaModChannel ChannelsChanged() { return MegaModChannel.UV; }
  15. public override string ModName() { return "UVAdjust"; }
  16. public override string GetHelpURL() { return "?page_id=352"; }
  17. public override Vector3 Map(int i, Vector3 p)
  18. {
  19. return p;
  20. }
  21. public override void Modify(MegaModifiers mc)
  22. {
  23. Vector2[] uvs = mc.GetSourceUvs();
  24. Vector2[] newuvs = mc.GetDestUvs();
  25. if ( uvs.Length > 0 )
  26. {
  27. Vector3 pos = -gizmoPos;
  28. Vector3 scl = gizmoScale;
  29. Vector3 rot = gizmoRot;
  30. Matrix4x4 tm1 = Matrix4x4.identity;
  31. Vector3 p = Vector3.zero;
  32. for ( int i = 0; i < uvs.Length; i++ )
  33. {
  34. p.x = uvs[i].x - Offset.x - 0.5f;
  35. p.z = uvs[i].y - Offset.z - 0.5f;
  36. p.y = 0.0f;
  37. float d = Mathf.Sqrt(p.x * p.x + p.z * p.z) * spiral;
  38. rot = new Vector3(gizmoRot.x, gizmoRot.y + d, gizmoRot.z);
  39. tm1 = Matrix4x4.TRS(pos, Quaternion.Euler(rot), scl);
  40. p = tm1.MultiplyPoint(p);
  41. newuvs[i].x = p.x;
  42. newuvs[i].y = p.z;
  43. }
  44. }
  45. }
  46. //public override bool ModLateUpdate(Modifiers mc)
  47. public override bool ModLateUpdate(MegaModContext mc)
  48. {
  49. if ( animate )
  50. {
  51. if ( Application.isPlaying )
  52. spiral += spiralspeed * Time.deltaTime;
  53. if ( Mathf.Abs(spiral) > spirallim )
  54. {
  55. if ( spiral < 0.0f )
  56. spiral = -spirallim;
  57. else
  58. spiral = spirallim;
  59. spiralspeed = -spiralspeed;
  60. }
  61. if ( Application.isPlaying )
  62. {
  63. gizmoRot.y += rotspeed * Time.deltaTime;
  64. gizmoRot.y = Mathf.Repeat(gizmoRot.y, 360.0f);
  65. gizmoPos += speed * Time.deltaTime;
  66. }
  67. }
  68. return Prepare(mc);
  69. }
  70. public override bool Prepare(MegaModContext mc)
  71. {
  72. return true;
  73. }
  74. public override void DrawGizmo(MegaModContext context)
  75. {
  76. tm = Matrix4x4.identity;
  77. invtm = tm.inverse;
  78. if ( !Prepare(context) )
  79. return;
  80. Vector3 min = new Vector3(-0.5f, 0.0f, -0.5f);
  81. Vector3 max = new Vector3(0.5f, 0.0f, 0.5f);
  82. min += Offset;
  83. max += Offset;
  84. Matrix4x4 mat = Matrix4x4.identity;
  85. Vector3 scl = gizmoScale;
  86. scl.x = 1.0f / scl.x;
  87. scl.y = 1.0f / scl.y;
  88. scl.z = 1.0f / scl.z;
  89. mat.SetTRS(Vector3.Scale(-gizmoPos - Offset, bbox.Size()), Quaternion.Euler(-gizmoRot), scl);
  90. if ( context.mod.sourceObj != null )
  91. Gizmos.matrix = context.mod.sourceObj.transform.localToWorldMatrix;
  92. else
  93. Gizmos.matrix = transform.localToWorldMatrix;
  94. corners[0] = mat.MultiplyPoint(Vector3.Scale(new Vector3(min.x, min.y, min.z), bbox.Size()));
  95. corners[1] = mat.MultiplyPoint(Vector3.Scale(new Vector3(min.x, min.y, max.z), bbox.Size()));
  96. corners[2] = mat.MultiplyPoint(Vector3.Scale(new Vector3(max.x, min.y, max.z), bbox.Size()));
  97. corners[3] = mat.MultiplyPoint(Vector3.Scale(new Vector3(max.x, min.y, min.z), bbox.Size()));
  98. DrawEdge(corners[0], corners[1]);
  99. DrawEdge(corners[1], corners[2]);
  100. DrawEdge(corners[2], corners[3]);
  101. DrawEdge(corners[3], corners[0]);
  102. }
  103. }