using System; using UnityEngine; [AddComponentMenu("Modifiers/Warps/Twist")] public class MegaTwistWarp : MegaWarp { 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 WarpName() { return "Twist"; } public override string GetIcon() { return "MegaTwist icon.png"; } public override string GetHelpURL() { return "?page_id=2556"; } private void CalcHeight(MegaAxis axis, float angle) { switch (axis) { case MegaAxis.X: height = Width; break; case MegaAxis.Z: height = Height; break; case MegaAxis.Y: height = Length; 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); Vector3 a = p; float magnitude = p.magnitude; float t = Mathf.Exp((0f - totaldecay) * Mathf.Abs(magnitude)); 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 = 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); 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; } CalcHeight(axis, angle); totaldecay = Decay + decay; if (totaldecay < 0f) { totaldecay = 0f; } if (from > to) { from = to; } if (to < from) { to = from; } return true; } public override void ExtraGizmo() { if (doRegion) { DrawFromTo(axis, from, to); } } }