Tanoda
Macro.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.IO;
5using UnityEngine;
6#if !UNITY_WEBGL
7using Valve.VR.InteractionSystem;
8using static Valve.VR.InteractionSystem.Hand;
9#endif
10
11public class Macro : MonoBehaviour
12{
18 internal static List<ThrowableCanDisable> allActiveTCD = new List<ThrowableCanDisable>();
19 public static string T(string key)
20 {
22 }
23
24 public static float StoF(string value)
25 {
26 if (string.IsNullOrEmpty(value)) return float.NaN;
27
28 var ci = CultureInfo.CurrentCulture;
29 var nfi = ci.NumberFormat;
30 var temp = value;
31 temp = temp.Replace(',', nfi.CurrencyDecimalSeparator[0]);
32 temp = temp.Replace('.', nfi.CurrencyDecimalSeparator[0]);
33 var single = Convert.ToSingle(temp, ci);
34 return single;
35 }
36
37 public static string FtoS(float value, char separator = '.')
38 {
39 var temp = value.ToString("F6");
40 temp = temp.Replace(',', separator);
41 temp = temp.Replace('.', separator);
42 return temp;
43 }
44
45 public static string DecodeEncodedUTF8String(string encoded)
46 {
47 return System.Net.Mail.Attachment.CreateAttachmentFromString("", encoded).Name;
48 }
49
50
51 public static string GetFileNameForGameObject(GILES.Serialization.pb_SceneNode sn, string go, out string modelName)
52 {
53 var fileName = "";
54 foreach (var node in sn.children)
55 {
56 if (Macro.IsSupported(Path.GetExtension(node.name)))
57 {
58 fileName = node.name;
59 }
60 if (node.name == go)
61 {
62 modelName = go;
63 return go;
64 }
65 var fn = GetFileNameForGameObject(node, go, out modelName);
66 if (fn == go)
67 {
68 return fileName != "" ? fileName : go;
69 }
70 }
71 modelName = "";
72 return "";
73 }
74
75 public static bool IsSupported(string ext)
76 {
77 ext = ext.Replace(".", "");
78 switch (ext.ToLower())
79 {
80 case "png":
81 case "jpg":
82 case "jpeg":
83 case "bmp":
84 case "3d":
85 case "3ds":
86 case "3mf":
87 case "ac":
88 case "ac3d":
89 case "acc":
90 case "amf":
91 case "amj":
92 case "ase":
93 case "ask":
94 case "b3d":
95 case "blend":
96 case "bvh":
97 case "cob":
98 case "dae":
99 case "dxf":
100 case "enff":
101 case "fbx":
102#if !DANA
103 case "gltf":
104 case "glb":
105#endif
106 case "ifc":
107 case "irr":
108 case "irrmesh":
109 case "lwo":
110 case "lws":
111 case "lxo":
112 case "md2":
113 case "md3":
114 case "md5":
115 case "mdc":
116 case "mdl":
117 case "mesh":
118 case "xml":
119 case "mot":
120 case "ms3d":
121 case "ndo":
122 case "nff":
123 case "obj":
124 case "off":
125 case "ogex":
126 case "ply":
127 case "pmx":
128 case "prj":
129 case "q3o":
130 case "q3s":
131 case "raw":
132 case "scn":
133 case "sib":
134 case "smd":
135 case "stl":
136 case "ter":
137 case "uc":
138 case "vta":
139 case "x":
140 case "x3d":
141 case "xgl":
142 case "zgl":
143 return true;
144 default:
145 return false;
146 }
147 }
148
149 public static Transform FindDeepChild(Transform aParent, string aName)
150 {
151 Queue<Transform> queue = new Queue<Transform>();
152 queue.Enqueue(aParent);
153 while (queue.Count > 0)
154 {
155 var c = queue.Dequeue();
156 if (c.name == aName)
157 return c;
158 foreach (Transform t in c)
159 queue.Enqueue(t);
160 }
161 return null;
162 }
163
164 public static string NormalizeFraction(string value)
165 {
166 var ci = CultureInfo.CurrentCulture;
167 var nfi = ci.NumberFormat;
168 value = value.Replace(',', nfi.CurrencyDecimalSeparator[0]);
169 value = value.Replace('.', nfi.CurrencyDecimalSeparator[0]);
170 return value;
171 }
172
173 public static string NormalizeFraction(string value, char separator)
174 {
175 value = value.Replace(',', separator);
176 value = value.Replace('.', separator);
177 return value;
178 }
179
180 public static string StripPath(string value)
181 {
182 return Path.GetFileName(value);
183 }
184
185 public static float SmallestAxis(Vector3 v)
186 {
187 var retval = Mathf.Abs(v.x);
188
189 if (Mathf.Abs(v.y) < retval)
190 retval = Mathf.Abs(v.y);
191
192 if (Mathf.Abs(v.z) < retval)
193 retval = Mathf.Abs(v.z);
194
195 return retval;
196 }
197
198 public static float LargestAxis(Vector3 v)
199 {
200 var retval = Mathf.Abs(v.x);
201
202 if (Mathf.Abs(v.y) > retval)
203 retval = Mathf.Abs(v.y);
204
205 if (Mathf.Abs(v.z) > retval)
206 retval = Mathf.Abs(v.z);
207
208 return retval;
209 }
210
211
212
213 public static void GetAllPossibleFileName(GILES.Serialization.pb_SceneNode node, ref List<string> list)
214 {
215 if (node.name != null && Path.GetExtension(node.name) != "" && !list.Contains(node.name))
216 {
217 list.Add(node.name);
218 }
219 try
220 {
221 if (node.changedTexture.matSettings != null)
222 {
223 foreach (var item in node.changedTexture.matSettings)
224 {
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);
233 }
234 }
235
236 }
237 catch (NullReferenceException e)
238 {
239 // ignored
240 }
241 foreach (var item in node.children)
242 {
243 GetAllPossibleFileName(item, ref list);
244 }
245 if (list.Contains(null))
246 list.Remove(null);
247 }
248
249 public static int FindClosingBracketIndex(string text, char openedBracket = '{', char closedBracket = '}')
250 {
251 int index = text.IndexOf(openedBracket);
252 int bracketCount = 1;
253 var textArray = text.ToCharArray();
254
255 for (int i = index + 1; i < textArray.Length; i++)
256 {
257 if (textArray[i] == openedBracket)
258 {
259 bracketCount++;
260 }
261 else if (textArray[i] == closedBracket)
262 {
263 bracketCount--;
264 }
265
266 if (bracketCount == 0)
267 {
268 index = i;
269 break;
270 }
271 }
272
273 return index;
274 }
275
276 public static void ChangeMaterialsModeToFadeMode(Material[] mats)
277 {
278 foreach (var m in mats)
279 {
280 m.SetFloat("_Mode", 2);
281 m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
282 m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
283 //m.SetInt("_ZWrite", 0);
284 m.DisableKeyword("_ALPHATEST_ON");
285 m.EnableKeyword("_ALPHABLEND_ON");
286 m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
287 m.renderQueue = 3000;
288 }
289 }
290 public static void SetMaterialAlpha(Material[] mats, float value)
291 {
292 foreach (var m in mats)
293 {
294 m.color = new Color(m.color.r, m.color.g, m.color.b, value);
295 }
296 }
297
298 public static Vector3 LerpAngle(Vector3 from, Vector3 to, float t)
299 {
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));
301 }
302
303 public static Vector3 ClampAngle(Vector3 value)
304 {
305 var x = value.x;
306 var y = value.y;
307 var z = value.z;
308 while (x > 180)
309 {
310 x -= 360f;
311 }
312 while (y > 180)
313 {
314 y -= 360f;
315 }
316 while (z > 180)
317 {
318 z -= 360f;
319 }
320
321 while (x < -180)
322 {
323 x += 360f;
324 }
325 while (y < -180)
326 {
327 y += 360f;
328 }
329 while (z < -180)
330 {
331 z += 360f;
332 }
333
334 return new Vector3(x, y, z);
335 }
336
337 public static float MinMaxClamp(float value, float min, float max)
338 {
339 value = ClampAngle(value);
340 value = Mathf.Min(max, Mathf.Max(min, value));
341 return value;
342 }
343
344 public static float ClampAngle(float value)
345 {
346 while (value > 180)
347 {
348 value -= 360f;
349 }
350
351 while (value < -180)
352 {
353 value += 360f;
354 }
355
356 return value;
357 }
358
359 public static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap = true, FilterMode filter = FilterMode.Bilinear)
360 {
361 //create a temporary RenderTexture with the target size
362 RenderTexture rt = RenderTexture.GetTemporary(targetX, targetY, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
363
364 //set the active RenderTexture to the temporary texture so we can read from it
365 RenderTexture.active = rt;
366
367 //Copy the texture data on the GPU - this is where the magic happens [(;]
368 Graphics.Blit(texture2D, rt);
369 //resize the texture to the target values (this sets the pixel data as undefined)
370 texture2D.Resize(targetX, targetY, texture2D.format, mipmap);
371 texture2D.filterMode = filter;
372
373 try
374 {
375 //reads the pixel values from the temporary RenderTexture onto the resized texture
376 texture2D.ReadPixels(new Rect(0.0f, 0.0f, targetX, targetY), 0, 0);
377 //actually upload the changed pixels to the graphics card
378 texture2D.Apply();
379 }
380 catch
381 {
382 Debug.LogError("Read/Write is not enabled on texture " + texture2D.name);
383 }
384
385
386 RenderTexture.ReleaseTemporary(rt);
387 }
388 public static void SetStatic(GameObject go)
389 {
390 var isStatic = go.GetComponent<StaticGO>();
391 var hasRigidbody = go.GetComponent<Rigidbody>();
392 var hasOffsetter = go.GetComponent<OffsetHolder>();
393 var hasVisualOffsetter = go.GetComponent<VisualOffset>();
394#if !UNITY_WEBGL
395 var hasInteractable = go.GetComponent<Interactable>();
396 var hasThrowable = go.GetComponent<ThrowableCanDisable>();
397
398#endif
399 if (!isStatic)
400 {
401 go.AddComponent<StaticGO>();
402 }
403
404#if !UNITY_WEBGL
405 if (hasThrowable)
406 {
407 Destroy(hasThrowable);
408 }
409#endif
410
411#if !UNITY_WEBGL
412 if (hasInteractable)
413 {
414 Destroy(hasInteractable);
415 }
416#endif
417 if (hasRigidbody)
418 {
419 Destroy(hasRigidbody);
420 }
421 if (!hasOffsetter)
422 {
423 go.AddComponent<OffsetHolder>();
424 }
425 if (!hasVisualOffsetter)
426 {
427 go.AddComponent<VisualOffset>();
428 }
429 }
430 public static void cacheAllTCD()
431 {
432 var tcds = FindObjectsOfType<ThrowableCanDisable>(true);
433 foreach (var tcd in tcds)
434 {
435 allActiveTCD.Add(tcd);
436 }
437
438 }
439 public static void SetInteractable(GameObject go, bool isKinematic = true)
440 {
441 var isStatic = go.GetComponent<StaticGO>();
442 var hasRigidbody = go.GetComponent<Rigidbody>();
443 var hasOffsetter = go.GetComponent<OffsetHolder>();
444 var hasVisualOffsetter = go.GetComponent<VisualOffset>();
445#if !UNITY_WEBGL
446 var hasInteractable = go.GetComponent<Interactable>();
447 var hasThrowable = go.GetComponent<ThrowableCanDisable>();
448
449#endif
450 if (isStatic)
451 {
452 Destroy(isStatic);
453 }
454
455 if (!hasRigidbody)
456 {
457 go.AddComponent<Rigidbody>().isKinematic = isKinematic;
458 }
459 if (!hasOffsetter)
460 {
461 go.AddComponent<OffsetHolder>();
462 }
463 if (!hasVisualOffsetter)
464 {
465 go.AddComponent<VisualOffset>();
466 }
467
468#if !UNITY_WEBGL
469 if (!hasInteractable)
470 {
471 go.AddComponent<Interactable>();
472 }
473
474 if (!hasThrowable)
475 {
476 go.AddComponent<ThrowableCanDisable>();
477 }
478 allActiveTCD.Add(hasThrowable);
479 //Macro.cacheAllTCD();
480#endif
481 }
482 public static Bounds GetBounds(GameObject go)
483 {
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++)
489 {
490 Renderer item = renderers[i];
491 bound.Encapsulate(item.bounds);
492 }
493
494 return bound;
495 }
496
497 public static byte[] Compress(byte[] byteArray)
498 {
499 byte[] compressedBytes;
500
501 using (var uncompressedStream = new System.IO.MemoryStream(byteArray))
502 {
503 using (var compressedStream = new System.IO.MemoryStream())
504 {
505 using (var compressorStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionLevel.Fastest))
506 {
507 uncompressedStream.CopyTo(compressorStream);
508 }
509 compressedBytes = compressedStream.ToArray();
510 }
511 }
512
513 return compressedBytes;
514 }
515
516 public static byte[] Decompress(byte[] bytes)
517 {
518 byte[] decompressedBytes;
519 using (var compressedStream = new System.IO.MemoryStream(bytes))
520 {
521 using (var decompressorStream = new System.IO.Compression.GZipStream(compressedStream, System.IO.Compression.CompressionMode.Decompress))
522 {
523 using (var decompressedStream = new System.IO.MemoryStream())
524 {
525 decompressorStream.CopyTo(decompressedStream);
526
527 decompressedBytes = decompressedStream.ToArray();
528 }
529 }
530 }
531 return decompressedBytes;
532 }
533
534 public static void SetLayerRecursive(GameObject go, string layerName)
535 {
536 SetLayer(go, layerName);
537 foreach (Transform child in go.transform)
538 {
539 SetLayer(child.gameObject, layerName);
540
541 foreach (Transform c in child)
542 SetLayerRecursive(c.gameObject, layerName);
543 }
544 }
545
546 public static void SetLayer(GameObject go, string layerName)
547 {
548 if (go)
549 go.layer = LayerMask.NameToLayer(layerName);
550 }
551
552 public static bool IsInChild(Transform t, string childName)
553 {
554 foreach (Transform c in t)
555 {
556 if (c.name == childName)
557 {
558 return true;
559 }
560 else
561 {
562 if(IsInChild(c, childName))
563 return true;
564 }
565 }
566 return false;
567 }
568
569 public static Collider[] CollidesWithWhat(Bounds bounds, bool skipHand = true)
570 {
571 if (bounds == null) return null;
572 if (bounds == default) return null;
573
574 var allCollider = FindObjectsOfType<Collider>();
575
576 List<Collider> result = new List<Collider>();
577
578 foreach (Collider c in allCollider)
579 {
580 if (c.bounds == bounds) continue;
581 if (skipHand)
582 {
583 if (c.name.Contains("Hand")) continue;
584#if !UNITY_WEBGL
585 if (c.GetComponentInParent<Hand>()) continue;
586#endif
587 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
588 }
589
590 if (c.bounds.Intersects(bounds)) result.Add(c);
591 }
592
593 return result.ToArray();
594 }
595
596 public static bool CollidesWithActiveTCD(GameObject bounds, bool skipHand = true)
597 {
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)
603 {
604 fingerBounds.Add(c.bounds);
605 }
606 var allTCD = allActiveTCD;
607 List<MeshRenderer> allCollider = new List<MeshRenderer>();
608 foreach (var tcd in allTCD)
609 {
610 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
611 }
612
613 foreach (MeshRenderer c in allCollider)
614 {
615 for (int i = 0; i < fingerBounds.Count; i++)
616 {
617 if (!c || c.bounds == fingerBounds[i]) continue;
618 if (skipHand)
619 {
620 if (c.name.Contains("Hand")) continue;
621#if !UNITY_WEBGL
622 if (c.GetComponentInParent<Hand>()) continue;
623#endif
624 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
625 }
626 var tcd = c.GetComponent<ThrowableCanDisable>();
627 if (c.bounds.Intersects(fingerBounds[i]) && tcd.isActiveAndEnabled == false && !tcd.enabled && tcd.gameObject.activeSelf)
628 {
629 //Debug.Break();
630 return true;
631 }
632 }
633 }
634 return false;
635
636 }
637 public static bool CollidesWithTwoHandGrab(GameObject bounds, bool skipHand = true)
638 {
639 if (bounds == null) return false;
640 if (bounds == default) return false;
641#if DANA
642 var fingerColliders = bounds.GetComponentsInChildren<CapsuleCollider>();
643#else
644 var fingerColliders = bounds.GetComponentsInChildren<Collider>();
645#endif
646
647 List<Bounds> fingerBounds = new List<Bounds>();
648#if DANA
649 foreach (CapsuleCollider c in fingerColliders)
650 {
651 fingerBounds.Add(c.bounds);
652 }
653#else
654 foreach (Collider c in fingerColliders)
655 {
656 fingerBounds.Add(c.bounds);
657 }
658#endif
659 var allTCD = allActiveTCD;
660 List<MeshRenderer> allCollider = new List<MeshRenderer>();
661 foreach (var tcd in allTCD)
662 {
663 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
664 }
665
666 foreach (MeshRenderer c in allCollider)
667 {
668 for (int i = 0; i < fingerBounds.Count; i++)
669 {
670 if (!c || c.bounds == fingerBounds[i]) continue;
671 if (skipHand)
672 {
673 if (c.name.Contains("Hand")) continue;
674#if !UNITY_WEBGL
675 if (c.GetComponentInParent<Hand>()) continue;
676#endif
677 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
678 }
679
680 if (c.bounds.Intersects(fingerBounds[i]) && c.gameObject.GetComponentInParent<TwoHandGrab>())
681 {
682 return true;
683 }
684 }
685 }
686 return false;
687
688 }
689 public static bool CollidesWithSnapOnAttach(GameObject bounds, bool skipHand = true)
690 {
691#if !UNITY_WEBGL
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>();
696
697 foreach (CapsuleCollider c in fingerColliders)
698 {
699 fingerBounds.Add(c.bounds);
700 }
701 var allTCD = allActiveTCD;
702 List<MeshRenderer> allCollider = new List<MeshRenderer>();
703 foreach (var tcd in allTCD)
704 {
705 if (tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
706 }
707
708 foreach (MeshRenderer c in allCollider)
709 {
710 for (int i = 0; i < fingerBounds.Count; i++)
711 {
712 if (!c || c.bounds == fingerBounds[i]) continue;
713 if (skipHand)
714 {
715 if (c.name.Contains("Hand")) continue;
716#if !UNITY_WEBGL
717 if (c.GetComponentInParent<Hand>()) continue;
718#endif
719 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
720 }
721 var tcd = c.gameObject.GetComponentInParent<ThrowableCanDisable>();
722
723 if (c.bounds.Intersects(fingerBounds[i]) && tcd && tcd.attachmentFlags.HasFlag(AttachmentFlags.SnapOnAttach))
724 {
725 return true;
726 }
727 }
728 }
729#endif
730 return false;
731
732 }
733
734 public static List<GameObject> CollidesWithFingersColliders(GameObject bounds, bool skipHand = true)
735 {
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>();
741 stopwatch.Start();
742 foreach (CapsuleCollider c in fingerColliders)
743 {
744 fingerBounds.Add(c.bounds);
745 }
746 var allTCD = allActiveTCD;
747 List<Collider> allCollider = new List<Collider>();
748 foreach (var tcd in allTCD)
749 {
750 if(tcd) allCollider.Add(tcd.GetComponentInChildren<Collider>());
751 }
752
753 List<GameObject> result = new List<GameObject>();
754
755 foreach (Collider c in allCollider)
756 {
757 for (int i = 0; i < fingerBounds.Count; i++)
758 {
759 if (!c || c.bounds == fingerBounds[i]) continue;
760 if (skipHand)
761 {
762 if (c.name.Contains("Hand")) continue;
763#if !UNITY_WEBGL
764 if (c.GetComponentInParent<Hand>()) continue;
765#endif
766 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
767 }
768
769 if (GetBounds(c.gameObject).Intersects(fingerBounds[i])) result.Add(fingerColliders[i].gameObject);
770 }
771 }
772 stopwatch.Stop();
773 Debug.Log($"5 th foreach: {stopwatch.Elapsed}ms");
774
775 return result;
776 }
777 public static List<GameObject> CollidesWithFingers(GameObject bounds, bool skipHand = true)
778 {
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)
784 {
785 fingerBounds.Add(c.bounds);
786 }
787 var allTCD = allActiveTCD;
788 List<MeshRenderer> allCollider = new List<MeshRenderer>();
789 foreach (var tcd in allTCD)
790 {
791 if(tcd) allCollider.Add(tcd.GetComponentInChildren<MeshRenderer>());
792 }
793
794 List<GameObject> result = new List<GameObject>();
795
796 foreach (MeshRenderer c in allCollider)
797 {
798 for (int i = 0; i < fingerBounds.Count; i++)
799 {
800 if (!c || c.bounds == fingerBounds[i]) continue;
801 if (skipHand)
802 {
803 if (c.name.Contains("Hand")) continue;
804#if !UNITY_WEBGL
805 if (c.GetComponentInParent<Hand>()) continue;
806#endif
807 if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
808 }
809
810 if (GetBounds(c.gameObject).Intersects(fingerBounds[i])) result.Add(fingerColliders[i].gameObject);
811 }
812// foreach (var bound in fingerBounds)
813// {
814// if (c.bounds == bound) continue;
815// if (skipHand)
816// {
817// if (c.name.Contains("Hand")) continue;
818//#if !UNITY_WEBGL
819// if (c.GetComponentInParent<Hand>()) continue;
820//#endif
821// if (c.gameObject.layer == LayerMask.NameToLayer("HoldItemLayer")) continue;
822// }
823
824// if (c.bounds.Intersects(bound)) result.Add();
825// }
826
827 }
828
829 return result;
830 }
831
832 public static GameObject[] CollidesWithWhat(Collider[] allCollider, Renderer renderer, Vector3 targetPos, Vector3 targetRot, bool breakAtFirstFind = true)
833 {
834 if (renderer == null) return null;
835 if (renderer == default) return null;
836 if (!(renderer is MeshRenderer)) return null;
837
838 var transform = renderer.transform;
839
840 List<GameObject> result = new List<GameObject>();
841
842
843 var vertices = new List<Vector3>();
844 renderer.GetComponent<MeshFilter>().mesh.GetVertices(vertices);
845
846 foreach (Collider c in allCollider)
847 {
848 if (c.GetComponent<Renderer>() == renderer) continue;
849
850 for (int i = 0; i < vertices.Count; i++)
851 {
852 if (vertices.Count > 500 && i % 2 != 0)
853 {
854 continue;
855 }
856 Vector3 v = vertices[i];
857 var vec = v * transform.lossyScale.x + targetPos;
858 vec = RotatePointAroundPivot(vec, targetPos, targetRot);
859 if (IsInside(c, vec))
860 {
861 result.Add(c.gameObject);
862
863 if (breakAtFirstFind)
864 return result.ToArray();
865
866 break;
867 }
868 }
869 }
870
871 return result.ToArray();
872 }
873 public static bool IsInside(Collider c, Vector3 point)
874 {
875 var mc = c.GetComponent<MeshCollider>();
876 bool oldValue = false;
877
878 if (mc != null)
879 {
880 oldValue = mc.convex;
881 mc.convex = true;
882 }
883 Vector3 closest = c.ClosestPoint(point);
884
885 if (mc != null)
886 {
887 mc.convex = oldValue;
888 }
889 return closest == point;
890 }
891
892 public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
893 {
894 Vector3 dir = point - pivot; // get point direction relative to pivot
895 dir = Quaternion.Euler(angles) * dir; // rotate it
896 point = dir + pivot; // calculate rotated point
897 return point; // return it
898 }
899 //
900 // Summary:
901 // Project point onto a line.
902 //
903 // Parameters:
904 // point:
905 //
906 // lineStart:
907 //
908 // lineEnd:
909 public static Vector3 ProjectPointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
910 {
911 Vector3 rhs = point - lineStart;
912 Vector3 vector = lineEnd - lineStart;
913 float magnitude = vector.magnitude;
914 Vector3 vector2 = vector;
915 if (magnitude > 1E-06f)
916 {
917 vector2 /= magnitude;
918 }
919
920 float value = Vector3.Dot(vector2, rhs);
921 value = Mathf.Clamp(value, 0f, magnitude);
922 return lineStart + vector2 * value;
923 }
924
925 //
926 // Summary:
927 // Calculate distance between a point and a line.
928 //
929 // Parameters:
930 // point:
931 //
932 // lineStart:
933 //
934 // lineEnd:
935 public static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
936 {
937 return Vector3.Magnitude(ProjectPointLine(point, lineStart, lineEnd) - point);
938 }
939 //With percentage i.e. between 0 and 1
940 public static bool VectorCompare(Vector3 me, Vector3 other, float percentage)
941 {
942 var dx = me.x - other.x;
943 if (Mathf.Abs(dx) > me.x * percentage)
944 return false;
945
946 var dy = me.y - other.y;
947 if (Mathf.Abs(dy) > me.y * percentage)
948 return false;
949
950 var dz = me.z - other.z;
951
952 return Mathf.Abs(dz) >= me.z * percentage;
953 }
954 public static Vector3 AngleLerp(Vector3 StartAngle, Vector3 FinishAngle, float t)
955 {
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);
960 return Lerped;
961 }
962
963}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32
string TranslateText(string key)
static Localization_SOURCE Instance
Definition: Macro.cs:12
static string T(string key)
Definition: Macro.cs:19
static Bounds GetBounds(GameObject go)
Definition: Macro.cs:482
static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
Definition: Macro.cs:892
static string StripPath(string value)
Definition: Macro.cs:180
static bool IsInside(Collider c, Vector3 point)
Definition: Macro.cs:873
static Collider[] CollidesWithWhat(Bounds bounds, bool skipHand=true)
Definition: Macro.cs:569
static bool VectorCompare(Vector3 me, Vector3 other, float percentage)
Definition: Macro.cs:940
static void GetAllPossibleFileName(GILES.Serialization.pb_SceneNode node, ref List< string > list)
Definition: Macro.cs:213
static string NormalizeFraction(string value)
Definition: Macro.cs:164
static void cacheAllTCD()
Definition: Macro.cs:430
static bool IsSupported(string ext)
Definition: Macro.cs:75
static byte[] Decompress(byte[] bytes)
Definition: Macro.cs:516
static string GetFileNameForGameObject(GILES.Serialization.pb_SceneNode sn, string go, out string modelName)
Definition: Macro.cs:51
static void SetLayerRecursive(GameObject go, string layerName)
Definition: Macro.cs:534
static bool CollidesWithActiveTCD(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:596
static GameObject[] CollidesWithWhat(Collider[] allCollider, Renderer renderer, Vector3 targetPos, Vector3 targetRot, bool breakAtFirstFind=true)
Definition: Macro.cs:832
static List< GameObject > CollidesWithFingers(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:777
static float SmallestAxis(Vector3 v)
Definition: Macro.cs:185
static byte[] Compress(byte[] byteArray)
Definition: Macro.cs:497
static float DistancePointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Definition: Macro.cs:935
static int FindClosingBracketIndex(string text, char openedBracket='{', char closedBracket='}')
Definition: Macro.cs:249
static Vector3 AngleLerp(Vector3 StartAngle, Vector3 FinishAngle, float t)
Definition: Macro.cs:954
static bool CollidesWithSnapOnAttach(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:689
static string DecodeEncodedUTF8String(string encoded)
Definition: Macro.cs:45
static float ClampAngle(float value)
Definition: Macro.cs:344
static float LargestAxis(Vector3 v)
Definition: Macro.cs:198
static void SetStatic(GameObject go)
Definition: Macro.cs:388
static Vector3 ProjectPointLine(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Definition: Macro.cs:909
static void SetInteractable(GameObject go, bool isKinematic=true)
Definition: Macro.cs:439
static string FtoS(float value, char separator='.')
Definition: Macro.cs:37
static Vector3 LerpAngle(Vector3 from, Vector3 to, float t)
Definition: Macro.cs:298
static Transform FindDeepChild(Transform aParent, string aName)
Definition: Macro.cs:149
static List< GameObject > CollidesWithFingersColliders(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:734
static void SetLayer(GameObject go, string layerName)
Definition: Macro.cs:546
static void ChangeMaterialsModeToFadeMode(Material[] mats)
Definition: Macro.cs:276
static float MinMaxClamp(float value, float min, float max)
Definition: Macro.cs:337
static bool CollidesWithTwoHandGrab(GameObject bounds, bool skipHand=true)
Definition: Macro.cs:637
static bool IsInChild(Transform t, string childName)
Definition: Macro.cs:552
static string NormalizeFraction(string value, char separator)
Definition: Macro.cs:173
static float StoF(string value)
Definition: Macro.cs:24
static void Resize(Texture2D texture2D, int targetX, int targetY, bool mipmap=true, FilterMode filter=FilterMode.Bilinear)
Definition: Macro.cs:359
static void SetMaterialAlpha(Material[] mats, float value)
Definition: Macro.cs:290
static Vector3 ClampAngle(Vector3 value)
Definition: Macro.cs:303