194 lines
3.6 KiB
C#
194 lines
3.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Bend")]
|
|
public class MegaBend : MegaModifier
|
|
{
|
|
[HideInInspector]
|
|
public float angle;
|
|
|
|
[HideInInspector]
|
|
public float dir;
|
|
|
|
[HideInInspector]
|
|
public MegaAxis axis;
|
|
|
|
[HideInInspector]
|
|
public bool doRegion;
|
|
|
|
[HideInInspector]
|
|
public float from;
|
|
|
|
[HideInInspector]
|
|
public float to;
|
|
|
|
private Matrix4x4 mat = default(Matrix4x4);
|
|
|
|
private Matrix4x4 tmAbove = default(Matrix4x4);
|
|
|
|
private Matrix4x4 tmBelow = default(Matrix4x4);
|
|
|
|
private float r;
|
|
|
|
private float oor;
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Bend";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=41";
|
|
}
|
|
|
|
public override void SetValues(MegaModifier mod)
|
|
{
|
|
MegaBend megaBend = (MegaBend)mod;
|
|
angle = megaBend.angle;
|
|
dir = megaBend.dir;
|
|
axis = megaBend.axis;
|
|
doRegion = megaBend.doRegion;
|
|
from = megaBend.from;
|
|
to = megaBend.to;
|
|
}
|
|
|
|
private void CalcR(MegaAxis axis, float ang)
|
|
{
|
|
float num = 0f;
|
|
if (!doRegion)
|
|
{
|
|
switch (axis)
|
|
{
|
|
case MegaAxis.X:
|
|
num = bbox.max.x - bbox.min.x;
|
|
break;
|
|
case MegaAxis.Z:
|
|
num = bbox.max.y - bbox.min.y;
|
|
break;
|
|
case MegaAxis.Y:
|
|
num = bbox.max.z - bbox.min.z;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
num = to - from;
|
|
}
|
|
if (Mathf.Abs(ang) < 1E-06f)
|
|
{
|
|
r = 0f;
|
|
}
|
|
else
|
|
{
|
|
r = num / ang;
|
|
}
|
|
oor = 1f / r;
|
|
}
|
|
|
|
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);
|
|
}
|
|
float x = p.x;
|
|
float y = p.y;
|
|
float f = (float)Math.PI - y * oor;
|
|
float num = Mathf.Cos(f);
|
|
float num2 = Mathf.Sin(f);
|
|
p.x = r * num + r - x * num;
|
|
p.y = r * num2 - x * num2;
|
|
return invtm.MultiplyPoint3x4(p);
|
|
}
|
|
|
|
private void Calc()
|
|
{
|
|
if (from > to)
|
|
{
|
|
from = to;
|
|
}
|
|
if (to < from)
|
|
{
|
|
to = from;
|
|
}
|
|
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 ModLateUpdate(MegaModContext mc)
|
|
{
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
Calc();
|
|
return true;
|
|
}
|
|
|
|
public override void ExtraGizmo(MegaModContext mc)
|
|
{
|
|
if (doRegion)
|
|
{
|
|
DrawFromTo(axis, from, to, mc);
|
|
}
|
|
}
|
|
}
|