156 lines
2.5 KiB
C#
156 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Twist")]
|
|
public class MegaTwist : MegaModifier
|
|
{
|
|
public float angle;
|
|
|
|
public bool doRegion;
|
|
|
|
public float from;
|
|
|
|
public float to;
|
|
|
|
public float Bias;
|
|
|
|
public MegaAxis axis;
|
|
|
|
private bool doBias;
|
|
|
|
private float height;
|
|
|
|
private float angleOverHeight;
|
|
|
|
private float theAngle;
|
|
|
|
private float bias;
|
|
|
|
private Matrix4x4 mat = default(Matrix4x4);
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Twist";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=341";
|
|
}
|
|
|
|
private void CalcHeight(MegaAxis axis, float angle, MegaBox3 bbx)
|
|
{
|
|
switch (axis)
|
|
{
|
|
case MegaAxis.X:
|
|
height = bbx.max.x - bbx.min.x;
|
|
break;
|
|
case MegaAxis.Z:
|
|
height = bbx.max.y - bbx.min.y;
|
|
break;
|
|
case MegaAxis.Y:
|
|
height = bbx.max.z - bbx.min.z;
|
|
break;
|
|
}
|
|
if (height == 0f)
|
|
{
|
|
theAngle = 0f;
|
|
angleOverHeight = 0f;
|
|
}
|
|
else
|
|
{
|
|
theAngle = angle;
|
|
angleOverHeight = angle / height;
|
|
}
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
if (theAngle == 0f)
|
|
{
|
|
return p;
|
|
}
|
|
p = tm.MultiplyPoint3x4(p);
|
|
float x = p.x;
|
|
float z = p.z;
|
|
float num = ((!doRegion) ? p.y : ((p.y < from) ? from : ((!(p.y > to)) ? p.y : to)));
|
|
float num3;
|
|
if (doBias)
|
|
{
|
|
float num2 = num / height;
|
|
num3 = theAngle * Mathf.Pow(Mathf.Abs(num2), bias);
|
|
if (num2 < 0f)
|
|
{
|
|
num3 = 0f - num3;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
num3 = num * angleOverHeight;
|
|
}
|
|
float num4 = Mathf.Cos((float)Math.PI / 180f * num3);
|
|
float num5 = Mathf.Sin((float)Math.PI / 180f * num3);
|
|
p.x = num4 * x + num5 * z;
|
|
p.z = (0f - num5) * x + num4 * z;
|
|
p = invtm.MultiplyPoint3x4(p);
|
|
return p;
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
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);
|
|
if (Bias != 0f)
|
|
{
|
|
bias = 1f - (Bias + 100f) / 200f;
|
|
if (bias < 1E-05f)
|
|
{
|
|
bias = 1E-05f;
|
|
}
|
|
if (bias > 0.99999f)
|
|
{
|
|
bias = 0.99999f;
|
|
}
|
|
bias = Mathf.Log(bias) / Mathf.Log(0.5f);
|
|
doBias = true;
|
|
}
|
|
else
|
|
{
|
|
bias = 1f;
|
|
doBias = false;
|
|
}
|
|
if (from > to)
|
|
{
|
|
from = to;
|
|
}
|
|
if (to < from)
|
|
{
|
|
to = from;
|
|
}
|
|
CalcHeight(axis, angle, mc.bbox);
|
|
return true;
|
|
}
|
|
|
|
public override void ExtraGizmo(MegaModContext mc)
|
|
{
|
|
if (doRegion)
|
|
{
|
|
DrawFromTo(axis, from, to, mc);
|
|
}
|
|
}
|
|
}
|