157 lines
2.9 KiB
C#
157 lines
2.9 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Warps/Stretch")]
|
|
public class MegaStretchWarp : MegaWarp
|
|
{
|
|
public float amount;
|
|
|
|
public bool doRegion;
|
|
|
|
public float to;
|
|
|
|
public float from;
|
|
|
|
public float amplify;
|
|
|
|
public MegaAxis axis;
|
|
|
|
private float heightMax;
|
|
|
|
private float heightMin;
|
|
|
|
private float amplifier;
|
|
|
|
private Matrix4x4 mat = default(Matrix4x4);
|
|
|
|
public override string WarpName()
|
|
{
|
|
return "Stretch";
|
|
}
|
|
|
|
public override string GetIcon()
|
|
{
|
|
return "MegaStretch icon.png";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=2560";
|
|
}
|
|
|
|
private void CalcBulge(MegaAxis axis, float stretch, float amplify)
|
|
{
|
|
amount = stretch;
|
|
amplifier = ((!(amplify >= 0f)) ? (1f / (0f - amplify + 1f)) : (amplify + 1f));
|
|
if (!doRegion)
|
|
{
|
|
switch (axis)
|
|
{
|
|
case MegaAxis.X:
|
|
heightMin = (0f - Width) * 0.5f;
|
|
heightMax = Width * 0.5f;
|
|
break;
|
|
case MegaAxis.Z:
|
|
heightMin = 0f;
|
|
heightMax = Height;
|
|
break;
|
|
case MegaAxis.Y:
|
|
heightMin = (0f - Length) * 0.5f;
|
|
heightMax = Length * 0.5f;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
heightMin = from;
|
|
heightMax = to;
|
|
}
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
if (amount == 0f || heightMax - heightMin == 0f)
|
|
{
|
|
return p;
|
|
}
|
|
if (doRegion && to - from == 0f)
|
|
{
|
|
return p;
|
|
}
|
|
p = tm.MultiplyPoint3x4(p);
|
|
Vector3 a = p;
|
|
float magnitude = p.magnitude;
|
|
float t = Mathf.Exp((0f - totaldecay) * Mathf.Abs(magnitude));
|
|
float num = ((doRegion && p.y > to) ? ((to - heightMin) / (heightMax - heightMin)) : ((!doRegion || !(p.y < from)) ? ((p.y - heightMin) / (heightMax - heightMin)) : ((from - heightMin) / (heightMax - heightMin))));
|
|
float num2;
|
|
float num3;
|
|
if (amount < 0f)
|
|
{
|
|
num2 = amplifier * (0f - amount) + 1f;
|
|
num3 = -1f / (amount - 1f);
|
|
}
|
|
else
|
|
{
|
|
num2 = 1f / (amplifier * amount + 1f);
|
|
num3 = amount + 1f;
|
|
}
|
|
float num4 = 4f * (1f - num2);
|
|
float num5 = -4f * (1f - num2);
|
|
float num6 = 1f;
|
|
float num7 = (num4 * num + num5) * num + num6;
|
|
p.x *= num7;
|
|
p.z *= num7;
|
|
if (doRegion && p.y < from)
|
|
{
|
|
p.y += (num3 - 1f) * from;
|
|
}
|
|
else if (doRegion && p.y <= to)
|
|
{
|
|
p.y *= num3;
|
|
}
|
|
else if (doRegion && p.y > to)
|
|
{
|
|
p.y += (num3 - 1f) * to;
|
|
}
|
|
else
|
|
{
|
|
p.y *= num3;
|
|
}
|
|
p = Vector3.Lerp(a, p, t);
|
|
p = invtm.MultiplyPoint3x4(p);
|
|
return p;
|
|
}
|
|
|
|
public override bool Prepare(float decay)
|
|
{
|
|
tm = base.transform.worldToLocalMatrix;
|
|
invtm = tm.inverse;
|
|
mat = Matrix4x4.identity;
|
|
switch (axis)
|
|
{
|
|
case MegaAxis.X:
|
|
MegaMatrix.RotateZ(ref mat, (float)Math.PI / 2f);
|
|
break;
|
|
case MegaAxis.Y:
|
|
MegaMatrix.RotateX(ref mat, -(float)Math.PI / 2f);
|
|
break;
|
|
}
|
|
SetAxis(mat);
|
|
CalcBulge(axis, amount, amplify);
|
|
totaldecay = Decay + decay;
|
|
if (totaldecay < 0f)
|
|
{
|
|
totaldecay = 0f;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override void ExtraGizmo()
|
|
{
|
|
if (doRegion)
|
|
{
|
|
DrawFromTo(axis, from, to);
|
|
}
|
|
}
|
|
}
|