180 lines
3.4 KiB
C#
180 lines
3.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Warps/Bend")]
|
|
public class MegaBendWarp : MegaWarp
|
|
{
|
|
public float angle;
|
|
|
|
public float dir;
|
|
|
|
public MegaAxis axis;
|
|
|
|
public bool doRegion;
|
|
|
|
public float from;
|
|
|
|
public float to;
|
|
|
|
private Matrix4x4 mat = default(Matrix4x4);
|
|
|
|
private Matrix4x4 tmAbove = default(Matrix4x4);
|
|
|
|
private Matrix4x4 tmBelow = default(Matrix4x4);
|
|
|
|
private float r;
|
|
|
|
public override string WarpName()
|
|
{
|
|
return "Bend";
|
|
}
|
|
|
|
public override string GetIcon()
|
|
{
|
|
return "MegaBend icon.png";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=2551";
|
|
}
|
|
|
|
private void CalcR(MegaAxis axis, float ang)
|
|
{
|
|
float num = 0f;
|
|
if (!doRegion)
|
|
{
|
|
switch (axis)
|
|
{
|
|
case MegaAxis.X:
|
|
num = Width;
|
|
break;
|
|
case MegaAxis.Z:
|
|
num = Height;
|
|
break;
|
|
case MegaAxis.Y:
|
|
num = Length;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
num = to - from;
|
|
}
|
|
if (Mathf.Abs(ang) < 1E-06f)
|
|
{
|
|
r = 0f;
|
|
}
|
|
else
|
|
{
|
|
r = num / ang;
|
|
}
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
if (r == 0f && !doRegion)
|
|
{
|
|
return p;
|
|
}
|
|
p = tm.MultiplyPoint3x4(p);
|
|
if (doRegion)
|
|
{
|
|
if (p.y <= from)
|
|
{
|
|
return invtm.MultiplyPoint3x4(tmBelow.MultiplyPoint3x4(p));
|
|
}
|
|
if (p.y >= to)
|
|
{
|
|
return invtm.MultiplyPoint3x4(tmAbove.MultiplyPoint3x4(p));
|
|
}
|
|
}
|
|
if (r == 0f)
|
|
{
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
Vector3 a = p;
|
|
float magnitude = p.magnitude;
|
|
float t = Mathf.Exp((0f - totaldecay) * Mathf.Abs(magnitude));
|
|
float x = p.x;
|
|
float y = p.y;
|
|
float num = y / r;
|
|
float num2 = Mathf.Cos((float)Math.PI - num);
|
|
float num3 = Mathf.Sin((float)Math.PI - num);
|
|
float x2 = r * num2 + r - x * num2;
|
|
p.x = x2;
|
|
float y2 = r * num3 - x * num3;
|
|
p.y = y2;
|
|
p = Vector3.Lerp(a, p, t);
|
|
p = invtm.MultiplyPoint3x4(p);
|
|
return p;
|
|
}
|
|
|
|
private void Calc()
|
|
{
|
|
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;
|
|
}
|
|
MegaMatrix.RotateY(ref mat, (float)Math.PI / 180f * dir);
|
|
SetAxis(mat);
|
|
CalcR(axis, (float)Math.PI / 180f * (0f - angle));
|
|
if (doRegion)
|
|
{
|
|
doRegion = false;
|
|
float num = to - from;
|
|
float num2;
|
|
float num3;
|
|
if (num == 0f)
|
|
{
|
|
num2 = (num3 = 1f);
|
|
}
|
|
else
|
|
{
|
|
num2 = to / num;
|
|
num3 = from / num;
|
|
}
|
|
tmAbove = Matrix4x4.identity;
|
|
MegaMatrix.Translate(ref tmAbove, 0f, 0f - to, 0f);
|
|
MegaMatrix.RotateZ(ref tmAbove, -(float)Math.PI / 180f * angle * num2);
|
|
MegaMatrix.Translate(ref tmAbove, 0f, to, 0f);
|
|
Vector3 vector = new Vector3(0f, to, 0f);
|
|
MegaMatrix.Translate(ref tmAbove, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(vector))) - vector);
|
|
tmBelow = Matrix4x4.identity;
|
|
MegaMatrix.Translate(ref tmBelow, 0f, 0f - from, 0f);
|
|
MegaMatrix.RotateZ(ref tmBelow, -(float)Math.PI / 180f * angle * num3);
|
|
MegaMatrix.Translate(ref tmBelow, 0f, from, 0f);
|
|
vector = new Vector3(0f, from, 0f);
|
|
MegaMatrix.Translate(ref tmBelow, tm.MultiplyPoint3x4(Map(0, invtm.MultiplyPoint3x4(vector))) - vector);
|
|
doRegion = true;
|
|
}
|
|
}
|
|
|
|
public override bool Prepare(float decay)
|
|
{
|
|
Calc();
|
|
totaldecay = Decay + decay;
|
|
if (totaldecay < 0f)
|
|
{
|
|
totaldecay = 0f;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override void ExtraGizmo()
|
|
{
|
|
if (doRegion)
|
|
{
|
|
DrawFromTo(axis, from, to);
|
|
}
|
|
}
|
|
}
|