123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using UnityEngine;
- [AddComponentMenu("Modifiers/UV/Tiles")]
- public class MegaUVTiles : MegaModifier
- {
- public int Frame = 0;
- public int TileWidth = 16;
- public int TileHeight = 16;
- public Vector2 off = Vector2.zero;
- public Vector2 scale = Vector2.one;
- public bool Animate = false;
- public int EndFrame = 0;
- public float fps = 1.0f;
- public float AnimTime = 0.0f;
- public bool flipy = false;
- public bool flipx = false;
- public MegaRepeatMode loopMode = MegaRepeatMode.Loop;
- [HideInInspector]
- public int twidth;
- [HideInInspector]
- public int theight;
- [HideInInspector]
- public float frm = 0.0f;
- Material mat;
- float tuvw;
- float tuvh;
- int xtiles;
- int ytiles;
- int maxframe;
- // 3 channels of UV
- public override MegaModChannel ChannelsReq() { return MegaModChannel.UV; }
- public override MegaModChannel ChannelsChanged() { return MegaModChannel.UV; }
- public override string ModName() { return "UVTiles"; }
- public override string GetHelpURL() { return "?page_id=354"; }
- // Going to have to allow submesh support so need to say which material we are effecting
- // Same for all uv mods really, see how max does it, prob mat id, max 32 mats possibly
- void Init()
- {
- MeshRenderer mr = GetComponent<MeshRenderer>();
- mat = mr.sharedMaterial;
- if ( mat != null )
- {
- Texture tex = mat.GetTexture("_MainTex");
- if ( tex != null )
- {
- twidth = tex.width;
- theight = tex.height;
- }
- }
- }
- // TODO: Show deform
- public override Vector3 Map(int i, Vector3 p)
- {
- return p;
- }
- public override void Modify(MegaModifiers mc)
- {
- Vector2[] uvs = mc.GetSourceUvs();
- Vector2[] newuvs = mc.GetDestUvs();
- if ( mat == null || twidth == 0.0f )
- Init();
- if ( uvs.Length > 0 && twidth > 0.0f )
- {
- xtiles = twidth / TileWidth;
- ytiles = theight / TileHeight;
- tuvw = (float)TileWidth / (float)twidth;
- tuvh = (float)TileHeight / (float)theight;
- maxframe = xtiles * ytiles;
- Frame = Frame % maxframe;
- int x = Frame % xtiles;
- int y = Frame / xtiles;
- float su = (x * tuvw);
- float sv = (y * tuvh);
- for ( int i = 0; i < uvs.Length; i++ )
- {
- Vector2 uv = Vector2.Scale(uvs[i] + off, scale);
- if ( flipy ) uv.y = 1.0f - uv.y;
- if ( flipx ) uv.x = 1.0f - uv.x;
- uv.x = su + (tuvw * uv.x);
- uv.y = 1.0f - (sv + (tuvh * uv.y));
- newuvs[i] = uv;
- }
- }
- }
- public override bool ModLateUpdate(MegaModContext mc)
- {
- if ( Animate )
- {
- if ( Application.isPlaying )
- AnimTime += Time.deltaTime;
- float l = (float)EndFrame / fps;
- switch ( loopMode )
- {
- case MegaRepeatMode.Loop: frm = Mathf.Repeat(AnimTime, l); break;
- case MegaRepeatMode.PingPong: frm = Mathf.PingPong(AnimTime, l); break;
- case MegaRepeatMode.Clamp: frm = Mathf.Clamp(AnimTime, 0.0f, l); break;
- }
- Frame = (int)((frm / l) * EndFrame);
- }
- return Prepare(mc);
- }
- public override bool Prepare(MegaModContext mc)
- {
- return true;
- }
- }
|