2using System.Collections.Generic;
3using System.Globalization;
7using Valve.VR.InteractionSystem;
8using static Valve.VR.InteractionSystem.Hand;
11public class Macro : MonoBehaviour
18 internal static List<ThrowableCanDisable> allActiveTCD =
new List<ThrowableCanDisable>();
19 public static string T(
string key)
24 public static float StoF(
string value)
26 if (
string.IsNullOrEmpty(value))
return float.NaN;
28 var ci = CultureInfo.CurrentCulture;
29 var nfi = ci.NumberFormat;
31 temp = temp.Replace(
',', nfi.CurrencyDecimalSeparator[0]);
32 temp = temp.Replace(
'.', nfi.CurrencyDecimalSeparator[0]);
33 var single = Convert.ToSingle(temp, ci);
37 public static string FtoS(
float value,
char separator =
'.')
39 var temp = value.ToString(
"F6");
40 temp = temp.Replace(
',', separator);
41 temp = temp.Replace(
'.', separator);
47 return System.Net.Mail.Attachment.CreateAttachmentFromString(
"", encoded).Name;
54 foreach (var node
in sn.children)
68 return fileName !=
"" ? fileName : go;
77 ext = ext.Replace(
".",
"");
78 switch (ext.ToLower())
151 Queue<Transform> queue =
new Queue<Transform>();
152 queue.Enqueue(aParent);
153 while (queue.Count > 0)
155 var c = queue.Dequeue();
158 foreach (Transform t
in c)
166 var ci = CultureInfo.CurrentCulture;
167 var nfi = ci.NumberFormat;
168 value = value.Replace(
',', nfi.CurrencyDecimalSeparator[0]);
169 value = value.Replace(
'.', nfi.CurrencyDecimalSeparator[0]);
175 value = value.Replace(
',', separator);
176 value = value.Replace(
'.', separator);
182 return Path.GetFileName(value);
187 var retval = Mathf.Abs(v.x);
189 if (Mathf.Abs(v.y) < retval)
190 retval = Mathf.Abs(v.y);
192 if (Mathf.Abs(v.z) < retval)
193 retval = Mathf.Abs(v.z);
200 var retval = Mathf.Abs(v.x);
202 if (Mathf.Abs(v.y) > retval)
203 retval = Mathf.Abs(v.y);
205 if (Mathf.Abs(v.z) > retval)
206 retval = Mathf.Abs(v.z);
215 if (node.name !=
null && Path.GetExtension(node.name) !=
"" && !list.Contains(node.name))
221 if (node.changedTexture.matSettings !=
null)
223 foreach (var item
in node.changedTexture.matSettings)
225 if (!list.Contains(item.textureName))
226 list.Add(item.textureName);
227 if (!list.Contains(item.occlusionName))
228 list.Add(item.occlusionName);
229 if (!list.Contains(item.metallicName))
230 list.Add(item.metallicName);
231 if (!list.Contains(item.normalName))
232 list.Add(item.normalName);
237 catch (NullReferenceException e)
241 foreach (var item
in node.children)
245 if (list.Contains(
null))
251 int index = text.IndexOf(openedBracket);
252 int bracketCount = 1;
253 var textArray = text.ToCharArray();
255 for (
int i = index + 1; i < textArray.Length; i++)
257 if (textArray[i] == openedBracket)
261 else if (textArray[i] == closedBracket)
266 if (bracketCount == 0)
278 foreach (var m
in mats)
280 m.SetFloat(
"_Mode", 2);
281 m.SetInt(
"_SrcBlend", (
int)
UnityEngine.Rendering.BlendMode.SrcAlpha);
282 m.SetInt(
"_DstBlend", (
int)
UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
284 m.DisableKeyword(
"_ALPHATEST_ON");
285 m.EnableKeyword(
"_ALPHABLEND_ON");
286 m.DisableKeyword(
"_ALPHAPREMULTIPLY_ON");
287 m.renderQueue = 3000;
292 foreach (var m
in mats)
294 m.color =
new Color(m.color.r, m.color.g, m.color.b, value);
298 public static Vector3
LerpAngle(Vector3 from, Vector3 to,
float t)
300 return new Vector3(Mathf.LerpAngle(from.x, to.x, t), Mathf.LerpAngle(from.y, to.y, t), Mathf.LerpAngle(from.z, to.z, t));
334 return new Vector3(x, y, z);
337 public static float MinMaxClamp(
float value,
float min,
float max)
340 value = Mathf.Min(max, Mathf.Max(min, value));
359 public static void Resize(Texture2D texture2D,
int targetX,
int targetY,
bool mipmap =
true, FilterMode filter = FilterMode.Bilinear)
362 RenderTexture rt = RenderTexture.GetTemporary(targetX, targetY, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
365 RenderTexture.active = rt;
368 Graphics.Blit(texture2D, rt);
370 texture2D.Resize(targetX, targetY, texture2D.format, mipmap);
371 texture2D.filterMode = filter;
376 texture2D.ReadPixels(
new Rect(0.0f, 0.0f, targetX, targetY), 0, 0);
382 Debug.LogError(
"Read/Write is not enabled on texture " + texture2D.name);
386 RenderTexture.ReleaseTemporary(rt);
390 var isStatic = go.GetComponent<
StaticGO>();
391 var hasRigidbody = go.GetComponent<Rigidbody>();
393 var hasVisualOffsetter = go.GetComponent<
VisualOffset>();
395 var hasInteractable = go.GetComponent<Interactable>();
407 Destroy(hasThrowable);
414 Destroy(hasInteractable);
419 Destroy(hasRigidbody);
425 if (!hasVisualOffsetter)
432 var tcds = FindObjectsOfType<ThrowableCanDisable>(
true);
433 foreach (var tcd
in tcds)
435 allActiveTCD.Add(tcd);
441 var isStatic = go.GetComponent<
StaticGO>();
442 var hasRigidbody = go.GetComponent<Rigidbody>();
444 var hasVisualOffsetter = go.GetComponent<
VisualOffset>();
446 var hasInteractable = go.GetComponent<Interactable>();
457 go.AddComponent<Rigidbody>().isKinematic = isKinematic;
463 if (!hasVisualOffsetter)
469 if (!hasInteractable)
471 go.AddComponent<Interactable>();
478 allActiveTCD.Add(hasThrowable);
484 var renderers = go.GetComponentsInChildren<Renderer>();
485 if (renderers ==
null || renderers.Length == 0)
return default;
486 var bound = renderers[0].bounds;
487 if (renderers.Length > 1)
488 for (
int i = 1; i < renderers.Length; i++)
490 Renderer item = renderers[i];
491 bound.Encapsulate(item.bounds);
499 byte[] compressedBytes;
501 using (var uncompressedStream =
new System.IO.MemoryStream(byteArray))
503 using (var compressedStream =
new System.IO.MemoryStream())
505 using (var compressorStream =
new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionLevel.Fastest))
507 uncompressedStream.CopyTo(compressorStream);
509 compressedBytes = compressedStream.ToArray();
513 return compressedBytes;
518 byte[] decompressedBytes;
519 using (var compressedStream =
new System.IO.MemoryStream(bytes))
521 using (var decompressorStream =
new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Decompress))
523 using (var decompressedStream =
new System.IO.MemoryStream())
525 decompressorStream.CopyTo(decompressedStream);
527 decompressedBytes = decompressedStream.ToArray();
531 return decompressedBytes;
537 foreach (Transform child
in go.transform)
539 SetLayer(child.gameObject, layerName);
541 foreach (Transform c
in child)
546 public static void SetLayer(GameObject go,
string layerName)
549 go.layer = LayerMask.NameToLayer(layerName);
552 public static bool IsInChild(Transform t,
string childName)
554 foreach (Transform c
in t)
556 if (c.name == childName)
571 if (bounds ==
null)
return null;
572 if (bounds ==
default)
return null;
574 var allCollider = FindObjectsOfType<Collider>();
576 List<Collider> result =
new List<Collider>();
578 foreach (Collider c
in allCollider)
580 if (c.bounds == bounds)
continue;
583 if (c.name.Contains(
"Hand"))
continue;
585 if (c.GetComponentInParent<Hand>())
continue;
587 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
590 if (c.bounds.Intersects(bounds)) result.Add(c);
593 return result.ToArray();
598 if (bounds ==
null)
return false;
599 if (bounds ==
default)
return false;
600 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
601 List<Bounds> fingerBounds =
new List<Bounds>();
602 foreach (CapsuleCollider c
in fingerColliders)
604 fingerBounds.Add(c.bounds);
606 var allTCD = allActiveTCD;
607 List<MeshRenderer> allCollider =
new List<MeshRenderer>();
608 foreach (var tcd
in allTCD)
610 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
613 foreach (MeshRenderer c
in allCollider)
615 for (
int i = 0; i < fingerBounds.Count; i++)
617 if (!c || c.bounds == fingerBounds[i])
continue;
620 if (c.name.Contains(
"Hand"))
continue;
622 if (c.GetComponentInParent<Hand>())
continue;
624 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
627 if (c.bounds.Intersects(fingerBounds[i]) && tcd.isActiveAndEnabled ==
false && !tcd.enabled && tcd.gameObject.activeSelf)
639 if (bounds ==
null)
return false;
640 if (bounds ==
default)
return false;
642 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
644 var fingerColliders = bounds.GetComponentsInChildren<Collider>();
647 List<Bounds> fingerBounds =
new List<Bounds>();
649 foreach (CapsuleCollider c
in fingerColliders)
651 fingerBounds.Add(c.bounds);
654 foreach (Collider c
in fingerColliders)
656 fingerBounds.Add(c.bounds);
659 var allTCD = allActiveTCD;
660 List<MeshRenderer> allCollider =
new List<MeshRenderer>();
661 foreach (var tcd
in allTCD)
663 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
666 foreach (MeshRenderer c
in allCollider)
668 for (
int i = 0; i < fingerBounds.Count; i++)
670 if (!c || c.bounds == fingerBounds[i])
continue;
673 if (c.name.Contains(
"Hand"))
continue;
675 if (c.GetComponentInParent<Hand>())
continue;
677 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
680 if (c.bounds.Intersects(fingerBounds[i]) && c.gameObject.GetComponentInParent<
TwoHandGrab>())
692 if (bounds ==
null)
return false;
693 if (bounds ==
default)
return false;
694 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
695 List<Bounds> fingerBounds =
new List<Bounds>();
697 foreach (CapsuleCollider c
in fingerColliders)
699 fingerBounds.Add(c.bounds);
701 var allTCD = allActiveTCD;
702 List<MeshRenderer> allCollider =
new List<MeshRenderer>();
703 foreach (var tcd
in allTCD)
705 if (tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
708 foreach (MeshRenderer c
in allCollider)
710 for (
int i = 0; i < fingerBounds.Count; i++)
712 if (!c || c.bounds == fingerBounds[i])
continue;
715 if (c.name.Contains(
"Hand"))
continue;
717 if (c.GetComponentInParent<Hand>())
continue;
719 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
723 if (c.bounds.Intersects(fingerBounds[i]) && tcd && tcd.attachmentFlags.HasFlag(AttachmentFlags.SnapOnAttach))
736 System.Diagnostics.Stopwatch stopwatch =
new System.Diagnostics.Stopwatch();
737 if (bounds ==
null)
return null;
738 if (bounds ==
default)
return null;
739 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
740 List<Bounds> fingerBounds =
new List<Bounds>();
742 foreach (CapsuleCollider c
in fingerColliders)
744 fingerBounds.Add(c.bounds);
746 var allTCD = allActiveTCD;
747 List<Collider> allCollider =
new List<Collider>();
748 foreach (var tcd
in allTCD)
750 if(tcd) allCollider.Add(tcd.GetComponentInChildren<Collider>());
753 List<GameObject> result =
new List<GameObject>();
755 foreach (Collider c
in allCollider)
757 for (
int i = 0; i < fingerBounds.Count; i++)
759 if (!c || c.bounds == fingerBounds[i])
continue;
762 if (c.name.Contains(
"Hand"))
continue;
764 if (c.GetComponentInParent<Hand>())
continue;
766 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
769 if (
GetBounds(c.gameObject).Intersects(fingerBounds[i])) result.Add(fingerColliders[i].gameObject);
773 Debug.Log($
"5 th foreach: {stopwatch.Elapsed}ms");
779 if (bounds ==
null)
return null;
780 if (bounds ==
default)
return null;
781 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
782 List<Bounds> fingerBounds =
new List<Bounds>();
783 foreach (CapsuleCollider c
in fingerColliders)
785 fingerBounds.Add(c.bounds);
787 var allTCD = allActiveTCD;
788 List<MeshRenderer> allCollider =
new List<MeshRenderer>();
789 foreach (var tcd
in allTCD)
791 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
794 List<GameObject> result =
new List<GameObject>();
796 foreach (MeshRenderer c
in allCollider)
798 for (
int i = 0; i < fingerBounds.Count; i++)
800 if (!c || c.bounds == fingerBounds[i])
continue;
803 if (c.name.Contains(
"Hand"))
continue;
805 if (c.GetComponentInParent<Hand>())
continue;
807 if (c.gameObject.layer == LayerMask.NameToLayer(
"HoldItemLayer"))
continue;
810 if (
GetBounds(c.gameObject).Intersects(fingerBounds[i])) result.Add(fingerColliders[i].gameObject);
832 public static GameObject[]
CollidesWithWhat(Collider[] allCollider, Renderer renderer, Vector3 targetPos, Vector3 targetRot,
bool breakAtFirstFind =
true)
834 if (renderer ==
null)
return null;
835 if (renderer ==
default)
return null;
836 if (!(renderer is MeshRenderer))
return null;
838 var transform = renderer.transform;
840 List<GameObject> result =
new List<GameObject>();
843 var vertices =
new List<Vector3>();
844 renderer.GetComponent<MeshFilter>().mesh.GetVertices(vertices);
846 foreach (Collider c
in allCollider)
848 if (c.GetComponent<Renderer>() == renderer)
continue;
850 for (
int i = 0; i < vertices.Count; i++)
852 if (vertices.Count > 500 && i % 2 != 0)
856 Vector3 v = vertices[i];
857 var vec = v * transform.lossyScale.x + targetPos;
861 result.Add(c.gameObject);
863 if (breakAtFirstFind)
864 return result.ToArray();
871 return result.ToArray();
873 public static bool IsInside(Collider c, Vector3 point)
875 var mc = c.GetComponent<MeshCollider>();
876 bool oldValue =
false;
880 oldValue = mc.convex;
883 Vector3 closest = c.ClosestPoint(point);
887 mc.convex = oldValue;
889 return closest == point;
894 Vector3 dir = point - pivot;
895 dir = Quaternion.Euler(angles) * dir;
911 Vector3 rhs = point - lineStart;
912 Vector3 vector = lineEnd - lineStart;
913 float magnitude = vector.magnitude;
914 Vector3 vector2 = vector;
915 if (magnitude > 1E-06f)
917 vector2 /= magnitude;
920 float value = Vector3.Dot(vector2, rhs);
921 value = Mathf.Clamp(value, 0f, magnitude);
922 return lineStart + vector2 * value;
937 return Vector3.Magnitude(
ProjectPointLine(point, lineStart, lineEnd) - point);
940 public static bool VectorCompare(Vector3 me, Vector3 other,
float percentage)
942 var dx = me.x - other.x;
943 if (Mathf.Abs(dx) > me.x * percentage)
946 var dy = me.y - other.y;
947 if (Mathf.Abs(dy) > me.y * percentage)
950 var dz = me.z - other.z;
952 return Mathf.Abs(dz) >= me.z * percentage;
954 public static Vector3
AngleLerp(Vector3 StartAngle, Vector3 FinishAngle,
float t)
956 float xLerp = Mathf.LerpAngle(StartAngle.x, FinishAngle.x, t);
957 float yLerp = Mathf.LerpAngle(StartAngle.y, FinishAngle.y, t);
958 float zLerp = Mathf.LerpAngle(StartAngle.z, FinishAngle.z, t);
959 Vector3 Lerped =
new Vector3(xLerp, yLerp, zLerp);
string TranslateText(string key)
static Localization_SOURCE Instance
static string T(string key)
static Bounds GetBounds(GameObject go)
static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
static string StripPath(string value)
static bool IsInside(Collider c, Vector3 point)
static Collider[] CollidesWithWhat(Bounds bounds, bool skipHand=true)
static bool VectorCompare(Vector3 me, Vector3 other, float percentage)
static void GetAllPossibleFileName(GILES.Serialization.pb_SceneNode node, ref List< string > list)
static string NormalizeFraction(string value)
static void cacheAllTCD()
static bool IsSupported(string ext)
static byte[] Decompress(byte[] bytes)
static string GetFileNameForGameObject(GILES.Serialization.pb_SceneNode sn, string go, out string modelName)
static void SetLayerRecursive(GameObject go, string layerName)
static bool CollidesWithActiveTCD(GameObject bounds, bool skipHand=true)
static GameObject[] CollidesWithWhat(Collider[] allCollider, Renderer renderer, Vector3 targetPos, Vector3 targetRot, bool breakAtFirstFind=true)
static List< GameObject > CollidesWithFingers(GameObject bounds, bool skipHand=true)
static float SmallestAxis(Vector3 v)
static byte[] Compress(byte[] byteArray)
static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
static int FindClosingBracketIndex(string text, char openedBracket='{', char closedBracket='}')
static Vector3 AngleLerp(Vector3 StartAngle, Vector3 FinishAngle, float t)
static bool CollidesWithSnapOnAttach(GameObject bounds, bool skipHand=true)
static string DecodeEncodedUTF8String(string encoded)
static float ClampAngle(float value)
static float LargestAxis(Vector3 v)
static void SetStatic(GameObject go)
static Vector3 ProjectPointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
static void SetInteractable(GameObject go, bool isKinematic=true)
static string FtoS(float value, char separator='.')
static Vector3 LerpAngle(Vector3 from, Vector3 to, float t)
static Transform FindDeepChild(Transform aParent, string aName)
static List< GameObject > CollidesWithFingersColliders(GameObject bounds, bool skipHand=true)
static void SetLayer(GameObject go, string layerName)
static void ChangeMaterialsModeToFadeMode(Material[] mats)
static float MinMaxClamp(float value, float min, float max)
static bool CollidesWithTwoHandGrab(GameObject bounds, bool skipHand=true)
static bool IsInChild(Transform t, string childName)
static string NormalizeFraction(string value, char separator)
static float StoF(string value)
static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap=true, FilterMode filter=FilterMode.Bilinear)
static void SetMaterialAlpha(Material[] mats, float value)
static Vector3 ClampAngle(Vector3 value)