321 lines
7.5 KiB
C#
321 lines
7.5 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Selection/Multi Volume")]
|
|
public class MegaMultiVolSelect : MegaSelectionMod
|
|
{
|
|
private float[] modselection;
|
|
|
|
public Color gizCol = new Color(0.5f, 0.5f, 0.5f, 0.25f);
|
|
|
|
public float gizSize = 0.01f;
|
|
|
|
public bool useCurrentVerts = true;
|
|
|
|
public bool displayWeights = true;
|
|
|
|
public bool freezeSelection;
|
|
|
|
public List<MegaVolume> volumes = new List<MegaVolume>();
|
|
|
|
public override MegaModChannel ChannelsReq()
|
|
{
|
|
return (MegaModChannel)65;
|
|
}
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Multi Vol Select";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=3904";
|
|
}
|
|
|
|
public float[] GetSel()
|
|
{
|
|
return modselection;
|
|
}
|
|
|
|
private float GetDistBox(MegaVolume vol, Vector3 p)
|
|
{
|
|
Vector3 vector = p - vol.origin;
|
|
float num = 0f;
|
|
Vector3 vector2 = vector;
|
|
if (vector2.x < 0f - vol.boxsize.x)
|
|
{
|
|
float num2 = vector2.x + vol.boxsize.x;
|
|
num += num2 * num2;
|
|
vector2.x = 0f - vol.boxsize.x;
|
|
}
|
|
else if (vector2.x > vol.boxsize.x)
|
|
{
|
|
float num2 = vector2.x - vol.boxsize.x;
|
|
num += num2 * num2;
|
|
vector2.x = vol.boxsize.x;
|
|
}
|
|
if (vector2.y < 0f - vol.boxsize.y)
|
|
{
|
|
float num2 = vector2.y + vol.boxsize.y;
|
|
num += num2 * num2;
|
|
vector2.y = 0f - vol.boxsize.y;
|
|
}
|
|
else if (vector2.y > vol.boxsize.y)
|
|
{
|
|
float num2 = vector2.y - vol.boxsize.y;
|
|
num += num2 * num2;
|
|
vector2.y = vol.boxsize.y;
|
|
}
|
|
if (vector2.z < 0f - vol.boxsize.z)
|
|
{
|
|
float num2 = vector2.z + vol.boxsize.z;
|
|
num += num2 * num2;
|
|
vector2.z = 0f - vol.boxsize.z;
|
|
}
|
|
else if (vector2.z > vol.boxsize.z)
|
|
{
|
|
float num2 = vector2.z - vol.boxsize.z;
|
|
num += num2 * num2;
|
|
vector2.z = vol.boxsize.z;
|
|
}
|
|
return Mathf.Sqrt(num);
|
|
}
|
|
|
|
public override void GetSelection(MegaModifiers mc)
|
|
{
|
|
if (modselection == null || modselection.Length != mc.verts.Length)
|
|
{
|
|
modselection = new float[mc.verts.Length];
|
|
}
|
|
int num = 0;
|
|
if (!freezeSelection)
|
|
{
|
|
if (volumes != null && volumes.Count > 0)
|
|
{
|
|
for (int i = 0; i < volumes.Count; i++)
|
|
{
|
|
MegaVolume megaVolume = volumes[i];
|
|
if (!megaVolume.enabled)
|
|
{
|
|
continue;
|
|
}
|
|
Vector3 zero = Vector3.zero;
|
|
zero = (megaVolume.origin = ((!megaVolume.target) ? megaVolume.origin : base.transform.worldToLocalMatrix.MultiplyPoint(megaVolume.target.position)));
|
|
if (num == 0)
|
|
{
|
|
if (megaVolume.volType == MegaVolumeType.Sphere)
|
|
{
|
|
if (useCurrentVerts)
|
|
{
|
|
for (int j = 0; j < verts.Length; j++)
|
|
{
|
|
float num2 = Vector3.Distance(zero, verts[j]) - megaVolume.radius;
|
|
if (num2 < 0f)
|
|
{
|
|
modselection[j] = megaVolume.weight;
|
|
continue;
|
|
}
|
|
float num3 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(num2));
|
|
modselection[j] = num3 * megaVolume.weight;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int k = 0; k < verts.Length; k++)
|
|
{
|
|
float num4 = Vector3.Distance(zero, verts[k]) - megaVolume.radius;
|
|
if (num4 < 0f)
|
|
{
|
|
modselection[k] = megaVolume.weight;
|
|
continue;
|
|
}
|
|
float num5 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(num4));
|
|
modselection[k] = num5 * megaVolume.weight;
|
|
}
|
|
}
|
|
}
|
|
else if (useCurrentVerts)
|
|
{
|
|
for (int l = 0; l < verts.Length; l++)
|
|
{
|
|
float distBox = GetDistBox(megaVolume, verts[l]);
|
|
if (distBox < 0f)
|
|
{
|
|
modselection[l] = megaVolume.weight;
|
|
continue;
|
|
}
|
|
float num6 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(distBox));
|
|
if (num6 > 1f)
|
|
{
|
|
num6 = 1f;
|
|
}
|
|
modselection[l] = num6 * megaVolume.weight;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int m = 0; m < verts.Length; m++)
|
|
{
|
|
float distBox2 = GetDistBox(megaVolume, verts[m]);
|
|
if (distBox2 < 0f)
|
|
{
|
|
modselection[m] = megaVolume.weight;
|
|
continue;
|
|
}
|
|
float num7 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(distBox2));
|
|
if (num7 > 1f)
|
|
{
|
|
num7 = 1f;
|
|
}
|
|
modselection[m] = num7 * megaVolume.weight;
|
|
}
|
|
}
|
|
}
|
|
else if (megaVolume.volType == MegaVolumeType.Box)
|
|
{
|
|
if (useCurrentVerts)
|
|
{
|
|
for (int n = 0; n < verts.Length; n++)
|
|
{
|
|
float distBox3 = GetDistBox(megaVolume, verts[n]);
|
|
float num8 = modselection[n];
|
|
if (distBox3 < 0f)
|
|
{
|
|
num8 += megaVolume.weight;
|
|
}
|
|
else
|
|
{
|
|
float num9 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(distBox3));
|
|
num8 += num9 * megaVolume.weight;
|
|
}
|
|
if (num8 > 1f)
|
|
{
|
|
modselection[n] = 1f;
|
|
}
|
|
else
|
|
{
|
|
modselection[n] = num8;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int num10 = 0; num10 < verts.Length; num10++)
|
|
{
|
|
float distBox4 = GetDistBox(megaVolume, verts[num10]);
|
|
float num11 = modselection[num10];
|
|
if (distBox4 < 0f)
|
|
{
|
|
num11 += megaVolume.weight;
|
|
}
|
|
else
|
|
{
|
|
float num12 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(distBox4));
|
|
num11 += num12 * megaVolume.weight;
|
|
}
|
|
if (num11 > 1f)
|
|
{
|
|
modselection[num10] = 1f;
|
|
}
|
|
else
|
|
{
|
|
modselection[num10] = num11;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (useCurrentVerts)
|
|
{
|
|
for (int num13 = 0; num13 < verts.Length; num13++)
|
|
{
|
|
float num14 = Vector3.Distance(zero, verts[num13]) - megaVolume.radius;
|
|
float num15 = modselection[num13];
|
|
if (num14 < 0f)
|
|
{
|
|
num15 += megaVolume.weight;
|
|
}
|
|
else
|
|
{
|
|
float num16 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(num14));
|
|
num15 += num16 * megaVolume.weight;
|
|
}
|
|
if (num15 > 1f)
|
|
{
|
|
modselection[num13] = 1f;
|
|
}
|
|
else
|
|
{
|
|
modselection[num13] = num15;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int num17 = 0; num17 < verts.Length; num17++)
|
|
{
|
|
float num18 = Vector3.Distance(zero, verts[num17]) - megaVolume.radius;
|
|
float num19 = modselection[num17];
|
|
if (num18 < 0f)
|
|
{
|
|
num19 += megaVolume.weight;
|
|
}
|
|
else
|
|
{
|
|
float num20 = Mathf.Exp((0f - megaVolume.falloff) * Mathf.Abs(num18));
|
|
num19 += num20 * megaVolume.weight;
|
|
}
|
|
if (num19 > 1f)
|
|
{
|
|
modselection[num17] = 1f;
|
|
}
|
|
else
|
|
{
|
|
modselection[num17] = num19;
|
|
}
|
|
}
|
|
}
|
|
num++;
|
|
}
|
|
}
|
|
if (num == 0)
|
|
{
|
|
for (int num21 = 0; num21 < verts.Length; num21++)
|
|
{
|
|
modselection[num21] = 0f;
|
|
}
|
|
}
|
|
}
|
|
if ((mc.dirtyChannels & MegaModChannel.Verts) == 0)
|
|
{
|
|
mc.InitVertSource();
|
|
}
|
|
mc.selection = modselection;
|
|
}
|
|
|
|
public override void DrawGizmo(MegaModContext context)
|
|
{
|
|
if (!ModEnabled)
|
|
{
|
|
return;
|
|
}
|
|
base.DrawGizmo(context);
|
|
Matrix4x4 localToWorldMatrix = base.gameObject.transform.localToWorldMatrix;
|
|
Gizmos.matrix = localToWorldMatrix;
|
|
for (int i = 0; i < volumes.Count; i++)
|
|
{
|
|
if (volumes[i].enabled && volumes[i].volType == MegaVolumeType.Box)
|
|
{
|
|
Gizmos.color = volumes[i].regcol;
|
|
Gizmos.DrawWireCube(volumes[i].origin, volumes[i].boxsize * 2f);
|
|
}
|
|
if (volumes[i].enabled && volumes[i].volType == MegaVolumeType.Sphere)
|
|
{
|
|
Gizmos.color = volumes[i].regcol;
|
|
Gizmos.DrawWireSphere(volumes[i].origin, volumes[i].radius);
|
|
}
|
|
}
|
|
Gizmos.matrix = Matrix4x4.identity;
|
|
}
|
|
}
|