120 lines
2.4 KiB
C#
120 lines
2.4 KiB
C#
using System.Threading;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Curve Deform")]
|
|
public class MegaCurveDeform : MegaModifier
|
|
{
|
|
public MegaAxis axis;
|
|
|
|
public AnimationCurve defCurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 0f), new Keyframe(1f, 0f));
|
|
|
|
public float MaxDeviation = 1f;
|
|
|
|
private float width;
|
|
|
|
private int ax;
|
|
|
|
public float Pos;
|
|
|
|
public bool UsePos;
|
|
|
|
private Keyframe key = default(Keyframe);
|
|
|
|
private static object resourceLock = new object();
|
|
|
|
public override string ModName()
|
|
{
|
|
return "CurveDeform";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=655";
|
|
}
|
|
|
|
public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
|
|
{
|
|
if (selection != null)
|
|
{
|
|
DoWorkWeighted(mc, index, start, end, cores);
|
|
return;
|
|
}
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
sverts[i] = MapMT(i, verts[i]);
|
|
}
|
|
}
|
|
|
|
public override void DoWorkWeighted(MegaModifiers mc, int index, int start, int end, int cores)
|
|
{
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
Vector3 vector = verts[i];
|
|
float num = selection[i];
|
|
if (num > 0.001f)
|
|
{
|
|
Vector3 vector2 = MapMT(i, verts[i]);
|
|
sverts[i].x = vector.x + (vector2.x - vector.x) * num;
|
|
sverts[i].y = vector.y + (vector2.y - vector.y) * num;
|
|
sverts[i].z = vector.z + (vector2.z - vector.z) * num;
|
|
}
|
|
else
|
|
{
|
|
sverts[i] = vector;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Vector3 MapMT(int i, Vector3 p)
|
|
{
|
|
p = tm.MultiplyPoint3x4(p);
|
|
float num = (p[ax] - bbox.min[ax]) / width;
|
|
if (UsePos)
|
|
{
|
|
num += Pos;
|
|
}
|
|
Monitor.Enter(resourceLock);
|
|
p.y += defCurve.Evaluate(num) * MaxDeviation;
|
|
Monitor.Exit(resourceLock);
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
p = tm.MultiplyPoint3x4(p);
|
|
float num = (p[ax] - bbox.min[ax]) / width;
|
|
if (UsePos)
|
|
{
|
|
num += Pos;
|
|
}
|
|
p.y += defCurve.Evaluate(num) * MaxDeviation;
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
ax = (int)axis;
|
|
width = bbox.max[ax] - bbox.min[ax];
|
|
return true;
|
|
}
|
|
|
|
public float GetPos(float alpha)
|
|
{
|
|
return defCurve.Evaluate(alpha);
|
|
}
|
|
|
|
public void SetKey(int index, float t, float v, float intan, float outtan)
|
|
{
|
|
key.time = t;
|
|
key.value = v;
|
|
key.inTangent = intan;
|
|
key.outTangent = outtan;
|
|
defCurve.MoveKey(index, key);
|
|
}
|
|
}
|