158 lines
2.8 KiB
C#
158 lines
2.8 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/UV/Tiles")]
|
|
public class MegaUVTiles : MegaModifier
|
|
{
|
|
public int Frame;
|
|
|
|
public int TileWidth = 16;
|
|
|
|
public int TileHeight = 16;
|
|
|
|
public Vector2 off = Vector2.zero;
|
|
|
|
public Vector2 scale = Vector2.one;
|
|
|
|
public bool Animate;
|
|
|
|
public int EndFrame;
|
|
|
|
public float fps = 1f;
|
|
|
|
public float AnimTime;
|
|
|
|
public bool flipy;
|
|
|
|
public bool flipx;
|
|
|
|
public MegaRepeatMode loopMode;
|
|
|
|
[HideInInspector]
|
|
public int twidth;
|
|
|
|
[HideInInspector]
|
|
public int theight;
|
|
|
|
[HideInInspector]
|
|
public float frm;
|
|
|
|
private Material mat;
|
|
|
|
private float tuvw;
|
|
|
|
private float tuvh;
|
|
|
|
private int xtiles;
|
|
|
|
private int ytiles;
|
|
|
|
private int maxframe;
|
|
|
|
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";
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
MeshRenderer component = GetComponent<MeshRenderer>();
|
|
mat = component.sharedMaterial;
|
|
if (mat != null)
|
|
{
|
|
Texture texture = mat.GetTexture("_MainTex");
|
|
if (texture != null)
|
|
{
|
|
twidth = texture.width;
|
|
theight = texture.height;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
return p;
|
|
}
|
|
|
|
public override void Modify(MegaModifiers mc)
|
|
{
|
|
Vector2[] sourceUvs = mc.GetSourceUvs();
|
|
Vector2[] destUvs = mc.GetDestUvs();
|
|
if (mat == null || (float)twidth == 0f)
|
|
{
|
|
Init();
|
|
}
|
|
if (sourceUvs.Length <= 0 || !((float)twidth > 0f))
|
|
{
|
|
return;
|
|
}
|
|
xtiles = twidth / TileWidth;
|
|
ytiles = theight / TileHeight;
|
|
tuvw = (float)TileWidth / (float)twidth;
|
|
tuvh = (float)TileHeight / (float)theight;
|
|
maxframe = xtiles * ytiles;
|
|
Frame %= maxframe;
|
|
int num = Frame % xtiles;
|
|
int num2 = Frame / xtiles;
|
|
float num3 = (float)num * tuvw;
|
|
float num4 = (float)num2 * tuvh;
|
|
for (int i = 0; i < sourceUvs.Length; i++)
|
|
{
|
|
Vector2 vector = Vector2.Scale(sourceUvs[i] + off, scale);
|
|
if (flipy)
|
|
{
|
|
vector.y = 1f - vector.y;
|
|
}
|
|
if (flipx)
|
|
{
|
|
vector.x = 1f - vector.x;
|
|
}
|
|
vector.x = num3 + tuvw * vector.x;
|
|
vector.y = 1f - (num4 + tuvh * vector.y);
|
|
destUvs[i] = vector;
|
|
}
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
if (Animate)
|
|
{
|
|
AnimTime += Time.deltaTime;
|
|
float num = (float)EndFrame / fps;
|
|
switch (loopMode)
|
|
{
|
|
case MegaRepeatMode.Loop:
|
|
frm = Mathf.Repeat(AnimTime, num);
|
|
break;
|
|
case MegaRepeatMode.PingPong:
|
|
frm = Mathf.PingPong(AnimTime, num);
|
|
break;
|
|
case MegaRepeatMode.Clamp:
|
|
frm = Mathf.Clamp(AnimTime, 0f, num);
|
|
break;
|
|
}
|
|
Frame = (int)(frm / num * (float)EndFrame);
|
|
}
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
return true;
|
|
}
|
|
}
|