using System.Collections.Generic; using UnityEngine; [AddComponentMenu("Modifiers/Morph Ref")] public class MegaMorphRef : MegaMorphBase { public MegaMorph source; public bool UseLimit; public float Max; public float Min; public int[] mapping; public float importScale = 1f; public bool flipyz; public bool negx; [HideInInspector] public float tolerance = 0.0001f; public bool showmapping; public float mappingSize = 0.001f; public int mapStart; public int mapEnd; private Vector3[] dif; private static Vector3[] endpoint = new Vector3[4]; private static Vector3[] splinepoint = new Vector3[4]; private static Vector3[] temppoint = new Vector3[2]; private Vector3[] p1; private Vector3[] p2; private Vector3[] p3; private Vector3[] p4; public List pers = new List(4); [HideInInspector] public int compressedmem; [HideInInspector] public int compressedmem1; [HideInInspector] public int memuse; public bool animate; public float atime; public float animtime; public float looptime; public MegaRepeatMode repeatMode; public float speed = 1f; private static int framenum; private Vector3[] _verts; private Vector3[] _sverts; private bool mtmorphed; private int[] setStart; private int[] setEnd; private int[] copyStart; private int[] copyEnd; public override string ModName() { return "Morph Ref"; } public override string GetHelpURL() { return "?page_id=257"; } public void SetSource(MegaMorph src) { source = src; if ((bool)source) { if (chanBank == null) { chanBank = new List(); } chanBank.Clear(); for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan megaMorphChan = new MegaMorphChan(); megaMorphChan.control = source.chanBank[i].control; megaMorphChan.Percent = source.chanBank[i].Percent; megaMorphChan.mName = source.chanBank[i].mName; chanBank.Add(megaMorphChan); } } } public override bool ModLateUpdate(MegaModContext mc) { if ((bool)source) { if (animate) { animtime += Time.deltaTime * speed; switch (repeatMode) { case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break; case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0f, looptime); break; } SetAnim(animtime); } if (dif == null) { dif = new Vector3[mc.mod.verts.Length]; } } return Prepare(mc); } public override bool Prepare(MegaModContext mc) { if ((bool)source) { return source.Prepare(mc); } return false; } private void SetVerts(int j, Vector3[] p) { switch (j) { case 0: p1 = p; break; case 1: p2 = p; break; case 2: p3 = p; break; case 3: p4 = p; break; } } private void SetVerts(MegaMorphChan chan, int j, Vector3[] p) { switch (j) { case 0: chan.p1 = p; break; case 1: chan.p2 = p; break; case 2: chan.p3 = p; break; case 3: chan.p4 = p; break; } } public override void Modify(MegaModifiers mc) { if (source == null) { return; } if (source.nonMorphedVerts != null && source.nonMorphedVerts.Length > 1) { ModifyCompressed(mc); return; } framenum++; mc.ChangeSourceVerts(); bool flag = true; bool flag2 = false; float min = 0f; float max = 100f; if (UseLimit) { min = Min; max = Max; } for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan megaMorphChan = source.chanBank[i]; megaMorphChan.UpdatePercent(); float num = (UseLimit ? Mathf.Clamp(megaMorphChan.Percent, min, max) : ((!megaMorphChan.mUseLimit) ? Mathf.Clamp(megaMorphChan.Percent, 0f, 100f) : Mathf.Clamp(megaMorphChan.Percent, megaMorphChan.mSpinmin, megaMorphChan.mSpinmax))); if (num == 0f && (num != 0f || megaMorphChan.fChannelPercent == 0f)) { continue; } megaMorphChan.fChannelPercent = num; if (megaMorphChan.mTargetCache == null || megaMorphChan.mTargetCache.Count <= 0 || !megaMorphChan.mActiveOverride) { continue; } flag2 = true; if (megaMorphChan.mUseLimit) { } if (flag) { flag = false; for (int j = 0; j < source.oPoints.Length; j++) { dif[j] = source.oPoints[j]; } } if (megaMorphChan.mTargetCache.Count == 1) { for (int k = 0; k < source.oPoints.Length; k++) { Vector3 vector = megaMorphChan.mDeltas[k]; dif[k].x += vector.x * num; dif[k].y += vector.y * num; dif[k].z += vector.z * num; } continue; } int count = megaMorphChan.mTargetCache.Count; float num2 = num; int l; for (l = 1; l <= count && num2 >= megaMorphChan.GetTargetPercent(l - 2); l++) { } if (l > count) { l = count; } p4 = source.oPoints; if (l == 1) { p1 = source.oPoints; p2 = megaMorphChan.mTargetCache[0].points; p3 = megaMorphChan.mTargetCache[1].points; } else if (l == count) { int num3 = count - 1; for (int num4 = 2; num4 >= 0; num4--) { num3--; if (num3 == -2) { SetVerts(num4, source.oPoints); } else { SetVerts(num4, megaMorphChan.mTargetCache[num3 + 1].points); } } } else { int num5 = l; for (int num6 = 3; num6 >= 0; num6--) { num5--; if (num5 == -2) { SetVerts(num6, source.oPoints); } else { SetVerts(num6, megaMorphChan.mTargetCache[num5 + 1].points); } } } float targetPercent = megaMorphChan.GetTargetPercent(l - 3); float targetPercent2 = megaMorphChan.GetTargetPercent(l - 2); float num7 = num2 - targetPercent; float num8 = targetPercent2 - targetPercent; float u = num7 / num8; for (int m = 0; m < source.oPoints.Length; m++) { Vector3 vector2 = source.oPoints[m]; endpoint[0] = p1[m]; endpoint[1] = p2[m]; endpoint[2] = p3[m]; endpoint[3] = p4[m]; if (l == 1) { splinepoint[0] = endpoint[0]; splinepoint[3] = endpoint[1]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; float sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[1] - Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; splinepoint[1] = endpoint[0] + megaMorphChan.mCurvature * (splinepoint[2] - endpoint[0]); } } else if (l == count) { splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[2]; float sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[1] = endpoint[1] - Vector3.Dot(temppoint[1], temppoint[0]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; splinepoint[2] = endpoint[2] + megaMorphChan.mCurvature * (splinepoint[1] - endpoint[2]); } } else { temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; float sqrMagnitude = temppoint[1].sqrMagnitude; splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; } else { splinepoint[1] = endpoint[1] + Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; } temppoint[1] = endpoint[3] - endpoint[1]; temppoint[0] = endpoint[2] - endpoint[1]; sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[2] - Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; } } Vector3 b; MegaUtils.Bez3D(out b, ref splinepoint, u); dif[m].x += b.x - vector2.x; dif[m].y += b.y - vector2.y; dif[m].z += b.z - vector2.z; } } if (flag2) { for (int n = 0; n < source.mapping.Length; n++) { sverts[n] = dif[source.mapping[n]]; } } else { for (int num9 = 0; num9 < verts.Length; num9++) { sverts[num9] = verts[num9]; } } } private bool Changed(int v, int c) { for (int i = 0; i < source.chanBank[c].mTargetCache.Count; i++) { if (!source.oPoints[v].Equals(source.chanBank[c].mTargetCache[i].points[v])) { return true; } } return false; } public void ModifyCompressed(MegaModifiers mc) { framenum++; mc.ChangeSourceVerts(); bool flag = true; bool flag2 = false; for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan megaMorphChan = source.chanBank[i]; MegaMorphChan megaMorphChan2 = chanBank[i]; megaMorphChan.UpdatePercent(); float num = ((!megaMorphChan.mUseLimit) ? Mathf.Clamp(megaMorphChan2.Percent, 0f, 100f) : Mathf.Clamp(megaMorphChan2.Percent, megaMorphChan.mSpinmin, megaMorphChan.mSpinmax)); if (num == 0f && (num != 0f || megaMorphChan.fChannelPercent == 0f)) { continue; } megaMorphChan.fChannelPercent = num; if (megaMorphChan.mTargetCache == null || megaMorphChan.mTargetCache.Count <= 0 || !megaMorphChan.mActiveOverride) { continue; } flag2 = true; if (megaMorphChan.mUseLimit) { } if (flag) { flag = false; for (int j = 0; j < source.morphedVerts.Length; j++) { int num2 = source.morphedVerts[j]; dif[num2] = source.oPoints[num2]; } } if (megaMorphChan.mTargetCache.Count == 1) { for (int k = 0; k < source.morphedVerts.Length; k++) { int num3 = source.morphedVerts[k]; Vector3 vector = megaMorphChan.mDeltas[num3]; dif[num3].x += vector.x * num; dif[num3].y += vector.y * num; dif[num3].z += vector.z * num; } continue; } int count = megaMorphChan.mTargetCache.Count; float num4 = num; int l; for (l = 1; l <= count && num4 >= megaMorphChan.GetTargetPercent(l - 2); l++) { } if (l > count) { l = count; } p4 = source.oPoints; if (l == 1) { p1 = source.oPoints; p2 = megaMorphChan.mTargetCache[0].points; p3 = megaMorphChan.mTargetCache[1].points; } else if (l == count) { int num5 = count - 1; for (int num6 = 2; num6 >= 0; num6--) { num5--; if (num5 == -2) { SetVerts(num6, source.oPoints); } else { SetVerts(num6, megaMorphChan.mTargetCache[num5 + 1].points); } } } else { int num7 = l; for (int num8 = 3; num8 >= 0; num8--) { num7--; if (num7 == -2) { SetVerts(num8, source.oPoints); } else { SetVerts(num8, megaMorphChan.mTargetCache[num7 + 1].points); } } } float targetPercent = megaMorphChan.GetTargetPercent(l - 3); float targetPercent2 = megaMorphChan.GetTargetPercent(l - 2); float num9 = num4 - targetPercent; float num10 = targetPercent2 - targetPercent; float u = num9 / num10; for (int m = 0; m < source.morphedVerts.Length; m++) { int num11 = source.morphedVerts[m]; Vector3 vector2 = source.oPoints[num11]; endpoint[0] = p1[num11]; endpoint[1] = p2[num11]; endpoint[2] = p3[num11]; endpoint[3] = p4[num11]; if (l == 1) { splinepoint[0] = endpoint[0]; splinepoint[3] = endpoint[1]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; float sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[1] - Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; splinepoint[1] = endpoint[0] + megaMorphChan.mCurvature * (splinepoint[2] - endpoint[0]); } } else if (l == count) { splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[2]; float sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; splinepoint[2] = endpoint[1]; } else { splinepoint[1] = endpoint[1] - Vector3.Dot(temppoint[1], temppoint[0]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; splinepoint[2] = endpoint[2] + megaMorphChan.mCurvature * (splinepoint[1] - endpoint[2]); } } else { temppoint[1] = endpoint[2] - endpoint[0]; temppoint[0] = endpoint[1] - endpoint[0]; float sqrMagnitude = temppoint[1].sqrMagnitude; splinepoint[0] = endpoint[1]; splinepoint[3] = endpoint[2]; if (sqrMagnitude == 0f) { splinepoint[1] = endpoint[0]; } else { splinepoint[1] = endpoint[1] + Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; } temppoint[1] = endpoint[3] - endpoint[1]; temppoint[0] = endpoint[2] - endpoint[1]; sqrMagnitude = temppoint[1].sqrMagnitude; if (sqrMagnitude == 0f) { splinepoint[2] = endpoint[1]; } else { splinepoint[2] = endpoint[2] - Vector3.Dot(temppoint[0], temppoint[1]) * megaMorphChan.mCurvature / sqrMagnitude * temppoint[1]; } } Vector3 b; MegaUtils.Bez3D(out b, ref splinepoint, u); dif[num11].x += b.x - vector2.x; dif[num11].y += b.y - vector2.y; dif[num11].z += b.z - vector2.z; } } if (flag2) { for (int n = 0; n < source.morphMappingFrom.Length; n++) { sverts[source.morphMappingTo[n]] = dif[source.morphMappingFrom[n]]; } for (int num12 = 0; num12 < source.nonMorphMappingFrom.Length; num12++) { sverts[source.nonMorphMappingTo[num12]] = source.oPoints[source.nonMorphMappingFrom[num12]]; } } else { for (int num13 = 0; num13 < verts.Length; num13++) { sverts[num13] = verts[num13]; } } } public override void PrepareMT(MegaModifiers mc, int cores) { PrepareForMT(mc, cores); } public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores) { ModifyCompressedMT(mc, index, cores); } public void PrepareForMT(MegaModifiers mc, int cores) { if (setStart == null) { BuildMorphVertInfo(cores); } mtmorphed = false; for (int i = 0; i < source.chanBank.Count; i++) { MegaMorphChan megaMorphChan = source.chanBank[i]; megaMorphChan.UpdatePercent(); float num = ((!megaMorphChan.mUseLimit) ? Mathf.Clamp(megaMorphChan.Percent, 0f, 100f) : Mathf.Clamp(megaMorphChan.Percent, megaMorphChan.mSpinmin, megaMorphChan.mSpinmax)); if (num == 0f && (num != 0f || megaMorphChan.fChannelPercent == 0f)) { continue; } megaMorphChan.fChannelPercent = num; if (megaMorphChan.mTargetCache == null || megaMorphChan.mTargetCache.Count <= 0 || !megaMorphChan.mActiveOverride) { continue; } mtmorphed = true; if (megaMorphChan.mTargetCache.Count <= 1) { continue; } int count = megaMorphChan.mTargetCache.Count; megaMorphChan.fProgression = megaMorphChan.fChannelPercent; megaMorphChan.segment = 1; while (megaMorphChan.segment <= count && megaMorphChan.fProgression >= megaMorphChan.GetTargetPercent(megaMorphChan.segment - 2)) { megaMorphChan.segment++; } if (megaMorphChan.segment > count) { megaMorphChan.segment = count; } megaMorphChan.p4 = source.oPoints; if (megaMorphChan.segment == 1) { megaMorphChan.p1 = source.oPoints; megaMorphChan.p2 = megaMorphChan.mTargetCache[0].points; megaMorphChan.p3 = megaMorphChan.mTargetCache[1].points; continue; } if (megaMorphChan.segment == count) { int num2 = count - 1; for (int num3 = 2; num3 >= 0; num3--) { num2--; if (num2 == -2) { SetVerts(megaMorphChan, num3, source.oPoints); } else { SetVerts(megaMorphChan, num3, megaMorphChan.mTargetCache[num2 + 1].points); } } continue; } int num4 = megaMorphChan.segment; for (int num5 = 3; num5 >= 0; num5--) { num4--; if (num4 == -2) { SetVerts(megaMorphChan, num5, source.oPoints); } else { SetVerts(megaMorphChan, num5, megaMorphChan.mTargetCache[num4 + 1].points); } } } if (!mtmorphed) { for (int j = 0; j < verts.Length; j++) { sverts[j] = verts[j]; } } } public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores) { if (!mtmorphed) { return; } int num = source.morphedVerts.Length / cores; int num2 = tindex * num; int num3 = num2 + num; if (tindex == cores - 1) { num3 = source.morphedVerts.Length; } framenum++; bool flag = true; Vector3[] array = new Vector3[4]; Vector3[] p = new Vector3[4]; Vector3[] array2 = new Vector3[2]; for (int i = 0; i < chanBank.Count; i++) { MegaMorphChan megaMorphChan = chanBank[i]; if (megaMorphChan.fChannelPercent == 0f || megaMorphChan.mTargetCache == null || megaMorphChan.mTargetCache.Count <= 0 || !megaMorphChan.mActiveOverride) { continue; } if (flag) { flag = false; for (int j = num2; j < num3; j++) { int num4 = source.morphedVerts[j]; dif[num4] = source.oPoints[num4]; } } if (megaMorphChan.mTargetCache.Count == 1) { for (int k = num2; k < num3; k++) { int num5 = source.morphedVerts[k]; Vector3 vector = megaMorphChan.mDeltas[num5]; dif[num5].x += vector.x * megaMorphChan.fChannelPercent; dif[num5].y += vector.y * megaMorphChan.fChannelPercent; dif[num5].z += vector.z * megaMorphChan.fChannelPercent; } continue; } float targetPercent = megaMorphChan.GetTargetPercent(megaMorphChan.segment - 3); float targetPercent2 = megaMorphChan.GetTargetPercent(megaMorphChan.segment - 2); float num6 = megaMorphChan.fProgression - targetPercent; float num7 = targetPercent2 - targetPercent; float u = num6 / num7; for (int l = num2; l < num3; l++) { int num8 = source.morphedVerts[l]; Vector3 vector2 = source.oPoints[num8]; array[0] = megaMorphChan.p1[num8]; array[1] = megaMorphChan.p2[num8]; array[2] = megaMorphChan.p3[num8]; array[3] = megaMorphChan.p4[num8]; if (megaMorphChan.segment == 1) { p[0] = array[0]; p[3] = array[1]; array2[1] = array[2] - array[0]; array2[0] = array[1] - array[0]; float sqrMagnitude = array2[1].sqrMagnitude; if (sqrMagnitude == 0f) { p[1] = array[0]; p[2] = array[1]; } else { p[2] = array[1] - Vector3.Dot(array2[0], array2[1]) * megaMorphChan.mCurvature / sqrMagnitude * array2[1]; p[1] = array[0] + megaMorphChan.mCurvature * (p[2] - array[0]); } } else if (megaMorphChan.segment == megaMorphChan.mTargetCache.Count) { p[0] = array[1]; p[3] = array[2]; array2[1] = array[2] - array[0]; array2[0] = array[1] - array[2]; float sqrMagnitude = array2[1].sqrMagnitude; if (sqrMagnitude == 0f) { p[1] = array[0]; p[2] = array[1]; } else { p[1] = array[1] - Vector3.Dot(array2[1], array2[0]) * megaMorphChan.mCurvature / sqrMagnitude * array2[1]; p[2] = array[2] + megaMorphChan.mCurvature * (p[1] - array[2]); } } else { array2[1] = array[2] - array[0]; array2[0] = array[1] - array[0]; float sqrMagnitude = array2[1].sqrMagnitude; p[0] = array[1]; p[3] = array[2]; if (sqrMagnitude == 0f) { p[1] = array[0]; } else { p[1] = array[1] + Vector3.Dot(array2[0], array2[1]) * megaMorphChan.mCurvature / sqrMagnitude * array2[1]; } array2[1] = array[3] - array[1]; array2[0] = array[2] - array[1]; sqrMagnitude = array2[1].sqrMagnitude; if (sqrMagnitude == 0f) { p[2] = array[1]; } else { p[2] = array[2] - Vector3.Dot(array2[0], array2[1]) * megaMorphChan.mCurvature / sqrMagnitude * array2[1]; } } Vector3 b; MegaUtils.Bez3D(out b, ref p, u); dif[num8].x += b.x - vector2.x; dif[num8].y += b.y - vector2.y; dif[num8].z += b.z - vector2.z; } } if (mtmorphed) { for (int m = setStart[tindex]; m < setEnd[tindex]; m++) { sverts[source.morphMappingTo[m]] = dif[source.morphMappingFrom[m]]; } for (int n = copyStart[tindex]; n < copyEnd[tindex]; n++) { sverts[source.nonMorphMappingTo[n]] = source.oPoints[source.nonMorphMappingFrom[n]]; } } } private int Find(int index) { int num = source.morphedVerts[index]; for (int i = 0; i < source.morphMappingFrom.Length; i++) { if (source.morphMappingFrom[i] > num) { return i; } } return source.morphMappingFrom.Length - 1; } private void BuildMorphVertInfo(int cores) { int num = source.morphedVerts.Length / cores; setStart = new int[cores]; setEnd = new int[cores]; copyStart = new int[cores]; copyEnd = new int[cores]; int num2 = 0; int num3 = 0; for (int i = 0; i < cores; i++) { setStart[i] = num2; if (i < cores - 1) { setEnd[i] = Find(num3 + num); } num2 = setEnd[i]; num3 += num; } setEnd[cores - 1] = source.morphMappingFrom.Length; num2 = 0; num = source.nonMorphMappingFrom.Length / cores; for (int j = 0; j < cores; j++) { copyStart[j] = num2; copyEnd[j] = num2 + num; num2 += num; } copyEnd[cores - 1] = source.nonMorphMappingFrom.Length; } public void SetAnimTime(float t) { animtime = t; switch (repeatMode) { case MegaRepeatMode.Loop: animtime = Mathf.Repeat(animtime, looptime); break; case MegaRepeatMode.Clamp: animtime = Mathf.Clamp(animtime, 0f, looptime); break; } SetAnim(animtime); } }