From 6ee88a78dfbdb22e66907e3f34ee7e3b1e4e345a Mon Sep 17 00:00:00 2001
From: MaZiFAU <63099053+MaZiFAU@users.noreply.github.com>
Date: Sun, 24 Sep 2023 23:13:10 +0200
Subject: [PATCH] Support for fetching BoxCollider => TriangleFact

---
 Assets/Scenes/Worlds/RiverWorld.unity         |  2 +-
 Assets/Scripts/GlobalBehaviour.cs             |  7 +++
 Assets/Scripts/Loading/Stage.cs               |  2 +-
 Assets/Scripts/StageBehaviour.cs              | 60 +++++++++++++++----
 Assets/Scripts/StageStatic.cs                 |  2 +
 .../Extensions/IEnumeratorExtensions.cs       |  9 +++
 6 files changed, 67 insertions(+), 15 deletions(-)

diff --git a/Assets/Scenes/Worlds/RiverWorld.unity b/Assets/Scenes/Worlds/RiverWorld.unity
index 301e7353..db57f12c 100644
--- a/Assets/Scenes/Worlds/RiverWorld.unity
+++ b/Assets/Scenes/Worlds/RiverWorld.unity
@@ -3870,7 +3870,7 @@ Mesh:
   m_CorrespondingSourceObject: {fileID: 0}
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
-  m_Name: pb_Mesh1141600
+  m_Name: pb_Mesh272762
   serializedVersion: 10
   m_SubMeshes:
   - serializedVersion: 2
diff --git a/Assets/Scripts/GlobalBehaviour.cs b/Assets/Scripts/GlobalBehaviour.cs
index 9d76c011..2c503ce4 100644
--- a/Assets/Scripts/GlobalBehaviour.cs
+++ b/Assets/Scripts/GlobalBehaviour.cs
@@ -4,6 +4,7 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Text.RegularExpressions;
+using UnityEditor.PackageManager.Requests;
 using UnityEngine;
 using UnityEngine.Networking;
 
@@ -97,6 +98,9 @@ IEnumerator _GetScrollsfromServer()
             //    else
             //        break;
             //}
+            //while (request.result == UnityWebRequest.Result.InProgress)
+            //    yield return null;
+
             System.DateTime answerTime = System.DateTime.UtcNow;
 
             string jsonString = null;
@@ -165,6 +169,9 @@ IEnumerator _GetContextfromServer()
                 else
                     break;
             }
+            while(request.result == UnityWebRequest.Result.InProgress)
+                yield return null;
+
             System.DateTime answerTime = System.DateTime.UtcNow;
 
             string jsonString = null;
diff --git a/Assets/Scripts/Loading/Stage.cs b/Assets/Scripts/Loading/Stage.cs
index 9e00f966..79459544 100644
--- a/Assets/Scripts/Loading/Stage.cs
+++ b/Assets/Scripts/Loading/Stage.cs
@@ -73,7 +73,7 @@ public PlayerRecord player_record
             value.CopyExposedSolutionFacts(this);
         }
     }
-
+    [JsonProperty] // unused, but could be for resuming
     private string record_index;
 
     /// <summary>
diff --git a/Assets/Scripts/StageBehaviour.cs b/Assets/Scripts/StageBehaviour.cs
index 4ca3d8c0..18186009 100644
--- a/Assets/Scripts/StageBehaviour.cs
+++ b/Assets/Scripts/StageBehaviour.cs
@@ -1,3 +1,4 @@
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Xml.Serialization;
@@ -15,36 +16,69 @@ public class StageBehaviour : MonoBehaviour
     void Start()
     {
         StageStatic.LoadInitStage(true /*StageStatic.stage.player_record.solved*/, gameObject);
+        GenerateMMTCollider(gameObject);
     }
 
     public static List<TriangleFact> GenerateMMTCollider(GameObject obj = null)
     {
         bool debug = false;
         var t0 = System.DateTime.Now;
-
         List<TriangleFact> tris = new();
-        foreach (MeshCollider coll in FindObjectsOfType<MeshCollider>())
+
+        foreach (BoxCollider coll in FindObjectsOfType<BoxCollider>())
         {
-            if (!coll.sharedMesh.isReadable)
-                continue;
+            Vector3[] minmax = new Vector3[]
+            {
+                coll.center - coll.size/2,
+                coll.center + coll.size/2,
+            };
 
-            for (int i = 0; i < coll.sharedMesh.triangles.Length; i += 3)
+            _QuadAdd(0, 1, 3, 2);
+            _QuadAdd(1, 5, 7, 3);
+            _QuadAdd(5, 4, 6, 7);
+            _QuadAdd(4, 0, 2, 6);
+
+            _QuadAdd(2, 3, 7, 6);
+            _QuadAdd(0, 4, 5, 1);
+
+            void _QuadAdd(uint i, uint j, uint k, uint l)
+            {
+                _TrisAdd(i, k, j);
+                _TrisAdd(k, i, l);
+            }
+
+            void _TrisAdd(uint i, uint j, uint k)
             {
                 tris.Add(new(
                     new[] {
-                        _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 0]], coll.transform.localToWorldMatrix),
-                        _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 1]], coll.transform.localToWorldMatrix),
-                        _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 2]], coll.transform.localToWorldMatrix),
-                    },
-                    null)
-                );
+                        _Transform(new Vector3(minmax[i>>0&1][0], minmax[i>>1&1][1], minmax[i>>2&1][2]), coll.transform.localToWorldMatrix),
+                        _Transform(new Vector3(minmax[j>>0&1][0], minmax[j>>1&1][1], minmax[j>>2&1][2]), coll.transform.localToWorldMatrix),
+                        _Transform(new Vector3(minmax[k>>0&1][0], minmax[k>>1&1][1], minmax[k>>2&1][2]), coll.transform.localToWorldMatrix),
+                }));
             }
         }
 
+        //foreach (MeshCollider coll in FindObjectsOfType<MeshCollider>())
+        //{
+        //    if (!coll.sharedMesh.isReadable)
+        //        continue;
+
+        //    for (int i = 0; i < coll.sharedMesh.triangles.Length; i += 3)
+        //    {
+        //        tris.Add(new(
+        //            new[] {
+        //                _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 0]], coll.transform.localToWorldMatrix),
+        //                _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 1]], coll.transform.localToWorldMatrix),
+        //                _Transform(coll.sharedMesh.vertices[coll.sharedMesh.triangles[i + 2]], coll.transform.localToWorldMatrix),
+        //            })
+        //        );
+        //    }
+        //}
+
         if (debug && obj != null)
         {
             var t1 = System.DateTime.Now - t0;
-            Debug.Log($"{tris.Count} Tris in {t1.Milliseconds}");
+            Debug.Log($"{tris.Count} Tris in {t1.Milliseconds}ms");
 
             Mesh mesh = new()
             {
@@ -52,8 +86,8 @@ public static List<TriangleFact> GenerateMMTCollider(GameObject obj = null)
                 vertices = tris.SelectMany(t => t.Verticies).ToArray(),
                 triangles = Enumerable.Range(0, tris.Count * 3).ToArray(),
             };
-            mesh.Optimize();
             mesh.RecalculateNormals();
+            mesh.Optimize();
 
             MeshFilter filter = obj.GetComponent<MeshFilter>();
             filter.sharedMesh = mesh;
diff --git a/Assets/Scripts/StageStatic.cs b/Assets/Scripts/StageStatic.cs
index c7e3cead..bd27fad7 100644
--- a/Assets/Scripts/StageStatic.cs
+++ b/Assets/Scripts/StageStatic.cs
@@ -318,6 +318,8 @@ public static void ShallowLoadStages(bool force = false)
          && !force)
             return;
 
+        //GlobalBehaviour.InitiateContext.FastForward(); //active wait
+
         List<FileInfo>[] crawler = new List<FileInfo>[StageChapters.Count];
 
         _ = new Stage(); // Init static members
diff --git a/Assets/Scripts/Utility/Extensions/IEnumeratorExtensions.cs b/Assets/Scripts/Utility/Extensions/IEnumeratorExtensions.cs
index 5dd0533f..6d8c37ce 100644
--- a/Assets/Scripts/Utility/Extensions/IEnumeratorExtensions.cs
+++ b/Assets/Scripts/Utility/Extensions/IEnumeratorExtensions.cs
@@ -11,6 +11,15 @@ public static void FastForward(this IEnumerator that)
                 next.FastForward();
     }
 
+    public static IEnumerator ProxyHandle(this IEnumerator that)
+    {
+        while (that.MoveNext())
+            if(that.Current is IEnumerator next)
+                yield return next.ProxyHandle();
+            else
+                yield return null;
+    }
+
     public static IEnumerator yield_break => _YieldBreak;
     private static IEnumerator _YieldBreak = YieldBreak();
     public static IEnumerator YieldBreak()
-- 
GitLab