MegaUVTiles.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. [AddComponentMenu("Modifiers/UV/Tiles")]
  3. public class MegaUVTiles : MegaModifier
  4. {
  5. public int Frame = 0;
  6. public int TileWidth = 16;
  7. public int TileHeight = 16;
  8. public Vector2 off = Vector2.zero;
  9. public Vector2 scale = Vector2.one;
  10. public bool Animate = false;
  11. public int EndFrame = 0;
  12. public float fps = 1.0f;
  13. public float AnimTime = 0.0f;
  14. public bool flipy = false;
  15. public bool flipx = false;
  16. public MegaRepeatMode loopMode = MegaRepeatMode.Loop;
  17. [HideInInspector]
  18. public int twidth;
  19. [HideInInspector]
  20. public int theight;
  21. [HideInInspector]
  22. public float frm = 0.0f;
  23. Material mat;
  24. float tuvw;
  25. float tuvh;
  26. int xtiles;
  27. int ytiles;
  28. int maxframe;
  29. // 3 channels of UV
  30. public override MegaModChannel ChannelsReq() { return MegaModChannel.UV; }
  31. public override MegaModChannel ChannelsChanged() { return MegaModChannel.UV; }
  32. public override string ModName() { return "UVTiles"; }
  33. public override string GetHelpURL() { return "?page_id=354"; }
  34. // Going to have to allow submesh support so need to say which material we are effecting
  35. // Same for all uv mods really, see how max does it, prob mat id, max 32 mats possibly
  36. void Init()
  37. {
  38. MeshRenderer mr = GetComponent<MeshRenderer>();
  39. mat = mr.sharedMaterial;
  40. if ( mat != null )
  41. {
  42. Texture tex = mat.GetTexture("_MainTex");
  43. if ( tex != null )
  44. {
  45. twidth = tex.width;
  46. theight = tex.height;
  47. }
  48. }
  49. }
  50. // TODO: Show deform
  51. public override Vector3 Map(int i, Vector3 p)
  52. {
  53. return p;
  54. }
  55. public override void Modify(MegaModifiers mc)
  56. {
  57. Vector2[] uvs = mc.GetSourceUvs();
  58. Vector2[] newuvs = mc.GetDestUvs();
  59. if ( mat == null || twidth == 0.0f )
  60. Init();
  61. if ( uvs.Length > 0 && twidth > 0.0f )
  62. {
  63. xtiles = twidth / TileWidth;
  64. ytiles = theight / TileHeight;
  65. tuvw = (float)TileWidth / (float)twidth;
  66. tuvh = (float)TileHeight / (float)theight;
  67. maxframe = xtiles * ytiles;
  68. Frame = Frame % maxframe;
  69. int x = Frame % xtiles;
  70. int y = Frame / xtiles;
  71. float su = (x * tuvw);
  72. float sv = (y * tuvh);
  73. for ( int i = 0; i < uvs.Length; i++ )
  74. {
  75. Vector2 uv = Vector2.Scale(uvs[i] + off, scale);
  76. if ( flipy ) uv.y = 1.0f - uv.y;
  77. if ( flipx ) uv.x = 1.0f - uv.x;
  78. uv.x = su + (tuvw * uv.x);
  79. uv.y = 1.0f - (sv + (tuvh * uv.y));
  80. newuvs[i] = uv;
  81. }
  82. }
  83. }
  84. public override bool ModLateUpdate(MegaModContext mc)
  85. {
  86. if ( Animate )
  87. {
  88. if ( Application.isPlaying )
  89. AnimTime += Time.deltaTime;
  90. float l = (float)EndFrame / fps;
  91. switch ( loopMode )
  92. {
  93. case MegaRepeatMode.Loop: frm = Mathf.Repeat(AnimTime, l); break;
  94. case MegaRepeatMode.PingPong: frm = Mathf.PingPong(AnimTime, l); break;
  95. case MegaRepeatMode.Clamp: frm = Mathf.Clamp(AnimTime, 0.0f, l); break;
  96. }
  97. Frame = (int)((frm / l) * EndFrame);
  98. }
  99. return Prepare(mc);
  100. }
  101. public override bool Prepare(MegaModContext mc)
  102. {
  103. return true;
  104. }
  105. }