Files
2026-02-21 16:45:37 +08:00

198 lines
4.1 KiB
C#

using UnityEngine;
[AddComponentMenu("Modifiers/Selection/Volume")]
public class MegaVolSelect : MegaSelectionMod
{
private float[] modselection;
public Vector3 origin = Vector3.zero;
public float falloff = 1f;
public float radius = 1f;
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 MegaVolumeType volType = MegaVolumeType.Sphere;
public Vector3 boxsize = Vector3.one;
public float weight = 1f;
public Transform target;
public override MegaModChannel ChannelsReq()
{
return (MegaModChannel)65;
}
public override string ModName()
{
return "Vol Select";
}
public override string GetHelpURL()
{
return "?page_id=1307";
}
public float[] GetSel()
{
return modselection;
}
private float GetDistBox(Vector3 p)
{
Vector3 vector = p - origin;
float num = 0f;
Vector3 vector2 = vector;
if (vector2.x < 0f - boxsize.x)
{
float num2 = vector2.x + boxsize.x;
num += num2 * num2;
vector2.x = 0f - boxsize.x;
}
else if (vector2.x > boxsize.x)
{
float num2 = vector2.x - boxsize.x;
num += num2 * num2;
vector2.x = boxsize.x;
}
if (vector2.y < 0f - boxsize.y)
{
float num2 = vector2.y + boxsize.y;
num += num2 * num2;
vector2.y = 0f - boxsize.y;
}
else if (vector2.y > boxsize.y)
{
float num2 = vector2.y - boxsize.y;
num += num2 * num2;
vector2.y = boxsize.y;
}
if (vector2.z < 0f - boxsize.z)
{
float num2 = vector2.z + boxsize.z;
num += num2 * num2;
vector2.z = 0f - boxsize.z;
}
else if (vector2.z > boxsize.z)
{
float num2 = vector2.z - boxsize.z;
num += num2 * num2;
vector2.z = boxsize.z;
}
return Mathf.Sqrt(num);
}
public override void GetSelection(MegaModifiers mc)
{
if ((bool)target)
{
origin = base.transform.worldToLocalMatrix.MultiplyPoint(target.position);
}
if (modselection == null || modselection.Length != mc.verts.Length)
{
modselection = new float[mc.verts.Length];
}
if (freezeSelection)
{
mc.selection = modselection;
return;
}
if (volType == MegaVolumeType.Sphere)
{
if (useCurrentVerts)
{
for (int i = 0; i < verts.Length; i++)
{
float num = Vector3.Distance(origin, verts[i]) - radius;
if (num < 0f)
{
modselection[i] = weight;
continue;
}
float num2 = Mathf.Exp((0f - falloff) * Mathf.Abs(num));
modselection[i] = num2 * weight;
}
}
else
{
for (int j = 0; j < verts.Length; j++)
{
float num3 = Vector3.Distance(origin, verts[j]) - radius;
if (num3 < 0f)
{
modselection[j] = weight;
continue;
}
float num4 = Mathf.Exp((0f - falloff) * Mathf.Abs(num3));
modselection[j] = num4 * weight;
}
}
}
else if (useCurrentVerts)
{
for (int k = 0; k < verts.Length; k++)
{
float distBox = GetDistBox(verts[k]);
if (distBox < 0f)
{
modselection[k] = weight;
continue;
}
float num5 = Mathf.Exp((0f - falloff) * Mathf.Abs(distBox));
modselection[k] = num5 * weight;
}
}
else
{
for (int l = 0; l < verts.Length; l++)
{
float distBox2 = GetDistBox(verts[l]);
if (distBox2 < 0f)
{
modselection[l] = weight;
continue;
}
float num6 = Mathf.Exp((0f - falloff) * Mathf.Abs(distBox2));
modselection[l] = num6 * weight;
}
}
if ((mc.dirtyChannels & MegaModChannel.Verts) == 0)
{
mc.InitVertSource();
}
mc.selection = modselection;
}
public override void DrawGizmo(MegaModContext context)
{
if (ModEnabled)
{
base.DrawGizmo(context);
Matrix4x4 localToWorldMatrix = base.gameObject.transform.localToWorldMatrix;
Gizmos.matrix = localToWorldMatrix;
if (base.enabled && volType == MegaVolumeType.Box)
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(origin, boxsize * 2f);
}
if (base.enabled && volType == MegaVolumeType.Sphere)
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(origin, radius);
}
Gizmos.matrix = Matrix4x4.identity;
}
}
}