123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- using UnityEngine;
- using System.Collections.Generic;
- public enum MegaVolumeType
- {
- Box,
- Sphere,
- }
- [System.Serializable]
- public class MegaVolume
- {
- public MegaVolume()
- {
- falloff = 1.0f;
- enabled = true;
- weight = 1.0f;
- name = "None";
- uselimits = false;
- inverse = false;
- }
- public bool enabled = true;
- public float weight = 1.0f;
- public string name = "None";
- public Color regcol = Color.yellow;
- public Vector3 origin = Vector3.zero;
- public Vector3 boxsize = Vector3.one;
- // need type ie box or sphere
- public float falloff = 1.0f;
- public MegaVolumeType volType = MegaVolumeType.Sphere;
- public float radius = 1.0f;
- public bool uselimits = false;
- public Vector3 size = Vector3.zero;
- public Transform target;
- public bool inverse = false;
- static public MegaVolume Create()
- {
- MegaVolume vol = new MegaVolume();
- return vol;
- }
- }
- [AddComponentMenu("Modifiers/Selection/Multi Volume")]
- public class MegaMultiVolSelect : MegaSelectionMod
- {
- public override MegaModChannel ChannelsReq() { return MegaModChannel.Col | MegaModChannel.Verts; }
- public override string ModName() { return "Multi Vol Select"; }
- public override string GetHelpURL() { return "?page_id=3904"; }
- float[] modselection;
- public float[] GetSel() { return 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 = false;
- public List<MegaVolume> volumes = new List<MegaVolume>();
- float GetDistBox(MegaVolume vol, Vector3 p)
- {
- // Work in the box's coordinate system.
- Vector3 diff = p - vol.origin;
- // Compute squared distance and closest point on box.
- float sqrDistance = 0.0f;
- float delta;
- Vector3 closest = diff;
- if ( closest.x < -vol.boxsize.x )
- {
- delta = closest.x + vol.boxsize.x;
- sqrDistance += delta * delta;
- closest.x = -vol.boxsize.x;
- }
- else
- {
- if ( closest.x > vol.boxsize.x )
- {
- delta = closest.x - vol.boxsize.x;
- sqrDistance += delta * delta;
- closest.x = vol.boxsize.x;
- }
- }
- if ( closest.y < -vol.boxsize.y )
- {
- delta = closest.y + vol.boxsize.y;
- sqrDistance += delta * delta;
- closest.y = -vol.boxsize.y;
- }
- else
- {
- if ( closest.y > vol.boxsize.y )
- {
- delta = closest.y - vol.boxsize.y;
- sqrDistance += delta * delta;
- closest.y = vol.boxsize.y;
- }
- }
- if ( closest.z < -vol.boxsize.z )
- {
- delta = closest.z + vol.boxsize.z;
- sqrDistance += delta * delta;
- closest.z = -vol.boxsize.z;
- }
- else
- {
- if ( closest.z > vol.boxsize.z )
- {
- delta = closest.z - vol.boxsize.z;
- sqrDistance += delta * delta;
- closest.z = vol.boxsize.z;
- }
- }
- return Mathf.Sqrt(sqrDistance); // * 0.5f;
- }
- public override void GetSelection(MegaModifiers mc)
- {
- if ( modselection == null || modselection.Length != mc.verts.Length )
- modselection = new float[mc.verts.Length];
- int volcount = 0;
- if ( !freezeSelection )
- {
- if ( volumes != null && volumes.Count > 0 )
- {
- for ( int v = 0; v < volumes.Count; v++ )
- {
- MegaVolume vol = volumes[v];
- if ( vol.enabled )
- {
- Vector3 origin = Vector3.zero;
- if ( vol.target )
- origin = transform.worldToLocalMatrix.MultiplyPoint(vol.target.position);
- else
- origin = vol.origin;
- vol.origin = origin;
- if ( volcount == 0 )
- {
- if ( vol.volType == MegaVolumeType.Sphere )
- {
- if ( useCurrentVerts )
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = Vector3.Distance(origin, verts[i]) - vol.radius;
- if ( d < 0.0f )
- modselection[i] = vol.weight;
- else
- {
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- modselection[i] = w * vol.weight; //mc.cols[i][c];
- }
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- else
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = Vector3.Distance(origin, verts[i]) - vol.radius;
- if ( d < 0.0f )
- modselection[i] = vol.weight;
- else
- {
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- modselection[i] = w * vol.weight; //mc.cols[i][c];
- }
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- }
- else
- {
- if ( useCurrentVerts )
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = GetDistBox(vol, verts[i]);
- //if ( d < 0.0f )
- //modselection[i] = vol.weight;
- //else
- //{
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- if ( w > 1.0f )
- w = 1.0f;
- modselection[i] = w * vol.weight; //mc.cols[i][c];
- //}
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- else
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = GetDistBox(vol, verts[i]);
- //if ( d < 0.0f )
- //modselection[i] = vol.weight;
- //else
- //{
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- if ( w > 1.0f )
- w = 1.0f;
- modselection[i] = w * vol.weight; //mc.cols[i][c];
- //}
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- }
- }
- else
- {
- if ( vol.volType == MegaVolumeType.Box )
- {
- if ( useCurrentVerts )
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = GetDistBox(vol, verts[i]);
- float wg = modselection[i];
- //if ( d < 0.0f )
- //wg += vol.weight;
- //else
- //{
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- wg += w * vol.weight; //mc.cols[i][c];
- //}
- if ( wg > 1.0f )
- modselection[i] = 1.0f;
- else
- modselection[i] = wg;
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- else
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = GetDistBox(vol, verts[i]);
- float wg = modselection[i];
- //if ( d < 0.0f )
- //wg += vol.weight;
- //else
- //{
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- wg += w * vol.weight; //mc.cols[i][c];
- //}
- if ( wg > 1.0f )
- modselection[i] = 1.0f;
- else
- modselection[i] = wg;
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- }
- else
- {
- if ( useCurrentVerts )
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = Vector3.Distance(origin, verts[i]) - vol.radius;
- float wg = modselection[i];
- if ( d < 0.0f )
- wg += vol.weight;
- else
- {
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- wg += w * vol.weight; //mc.cols[i][c];
- }
- if ( wg > 1.0f )
- modselection[i] = 1.0f;
- else
- modselection[i] = wg;
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- else
- {
- for ( int i = 0; i < verts.Length; i++ )
- {
- float d = Vector3.Distance(origin, verts[i]) - vol.radius;
- float wg = modselection[i];
- if ( d < 0.0f )
- wg += vol.weight;
- else
- {
- float w = Mathf.Exp(-vol.falloff * Mathf.Abs(d));
- wg += w * vol.weight; //mc.cols[i][c];
- }
- if ( wg > 1.0f )
- modselection[i] = 1.0f;
- else
- modselection[i] = wg;
- if ( vol.inverse )
- modselection[i] = 1.0f - modselection[i];
- }
- }
- }
- }
- volcount++;
- }
- }
- }
- if ( volcount == 0 )
- {
- for ( int i = 0; i < verts.Length; i++ )
- modselection[i] = 0.0f;
- }
- }
- if ( (mc.dirtyChannels & MegaModChannel.Verts) == 0 )
- mc.InitVertSource();
- mc.selection = modselection;
- }
- public override void DrawGizmo(MegaModContext context)
- {
- if ( ModEnabled )
- {
- base.DrawGizmo(context);
- Matrix4x4 tm = gameObject.transform.localToWorldMatrix;
- Gizmos.matrix = tm;
- for ( int i = 0; i < volumes.Count; i++ )
- {
- if ( volumes[i].enabled && volumes[i].volType == MegaVolumeType.Box )
- {
- Gizmos.color = volumes[i].regcol; //Color.yellow;
- Gizmos.DrawWireCube(volumes[i].origin, volumes[i].boxsize * 2.0f); // * 0.5f);
- }
- if ( volumes[i].enabled && volumes[i].volType == MegaVolumeType.Sphere )
- {
- Gizmos.color = volumes[i].regcol; //Color.yellow;
- Gizmos.DrawWireSphere(volumes[i].origin, volumes[i].radius); // * 0.5f);
- }
- }
- Gizmos.matrix = Matrix4x4.identity;
- }
- }
- }
|