From 31d2e87231a24af258a0996ff8a8b62ea802a820 Mon Sep 17 00:00:00 2001
From: "bocska.karina" <bocska.karina@hallgato.ppke.hu>
Date: Tue, 29 Oct 2024 17:19:27 +0100
Subject: [PATCH] editstate and fittedquad fixes

---
 Assets/Prefabs/VertexPoint.prefab             |  2 +-
 Assets/Scenes/SampleScene.unity               |  6 ++--
 Assets/Scrips/Components/Block/BlockDeform.cs |  6 ++++
 Assets/Scrips/Components/Block/BlockRotate.cs |  7 +++-
 Assets/Scrips/Components/Quad/FittedQuad.cs   | 27 +++++++++++---
 Assets/Scrips/Components/Quad/VertexPoint.cs  |  2 +-
 Assets/Scrips/Contexts/EditState.cs           |  8 +++++
 .../Scrips/Controllers/ProgramController.cs   | 32 +++++++++++++++++
 .../Controllers/SelectPointsController.cs     | 16 ++++-----
 Packages/manifest.json                        |  1 +
 Packages/packages-lock.json                   | 35 ++++++++++++++-----
 11 files changed, 114 insertions(+), 28 deletions(-)

diff --git a/Assets/Prefabs/VertexPoint.prefab b/Assets/Prefabs/VertexPoint.prefab
index 2780af8..a48fadf 100644
--- a/Assets/Prefabs/VertexPoint.prefab
+++ b/Assets/Prefabs/VertexPoint.prefab
@@ -287,7 +287,7 @@ MonoBehaviour:
   m_AttachPointCompatibilityMode: 0
   m_StartingSingleGrabTransformers: []
   m_StartingMultipleGrabTransformers: []
-  m_AddDefaultGrabTransformers: 1
+  m_AddDefaultGrabTransformers: 0
 --- !u!114 &1183694293685742046
 MonoBehaviour:
   m_ObjectHideFlags: 0
diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity
index b9b3e57..48e8495 100644
--- a/Assets/Scenes/SampleScene.unity
+++ b/Assets/Scenes/SampleScene.unity
@@ -18387,7 +18387,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 5443163050158697355, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_Enabled
-      value: 0
+      value: 1
       objectReference: {fileID: 0}
     - target: {fileID: 6150670977401591343, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_AnchorMax.y
@@ -22151,7 +22151,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 5443163050158697355, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_Enabled
-      value: 0
+      value: 1
       objectReference: {fileID: 0}
     - target: {fileID: 6150670977401591343, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_AnchorMax.y
@@ -22191,7 +22191,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 7753147258609537286, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_MethodName
-      value: LoadProgram
+      value: ExportMesh
       objectReference: {fileID: 0}
     - target: {fileID: 7753147258609537286, guid: fa5ef3b56cfeb4f4ba855cdf2610a578, type: 3}
       propertyPath: m_OnClick.m_PersistentCalls.m_Calls.Array.data[0].m_TargetAssemblyTypeName
diff --git a/Assets/Scrips/Components/Block/BlockDeform.cs b/Assets/Scrips/Components/Block/BlockDeform.cs
index b7624ad..eac6053 100644
--- a/Assets/Scrips/Components/Block/BlockDeform.cs
+++ b/Assets/Scrips/Components/Block/BlockDeform.cs
@@ -115,6 +115,12 @@ public class BlockDeform : MonoBehaviour
             InitAnchors();
 
         EditState.Instance.RegisterListener(EditChangeHandler);
+        EditChangeHandler();
+    }
+
+    private void OnDestroy()
+    {
+        EditState.Instance.RemoveListener(EditChangeHandler);
     }
 
     private void InitAnchors()
diff --git a/Assets/Scrips/Components/Block/BlockRotate.cs b/Assets/Scrips/Components/Block/BlockRotate.cs
index 6853fb4..47f2f94 100644
--- a/Assets/Scrips/Components/Block/BlockRotate.cs
+++ b/Assets/Scrips/Components/Block/BlockRotate.cs
@@ -89,8 +89,13 @@ public class BlockRotate : MonoBehaviour
     {
         _rotateAnchor = Instantiate(RotateAnchorPrefab, this.transform.position, Quaternion.identity);
         _rotateAnchor.GetComponent<RotateAnchor>().Initialize(this);
-        _rotateAnchor.SetActive(false);
         EditState.Instance.RegisterListener(EditStateHandler);
+        EditStateHandler();
+    }
+
+    private void OnDestroy()
+    {
+        EditState.Instance.RemoveListener(EditStateHandler);
     }
 
     private void EditStateHandler()
diff --git a/Assets/Scrips/Components/Quad/FittedQuad.cs b/Assets/Scrips/Components/Quad/FittedQuad.cs
index 42c9fb5..37ee8bf 100644
--- a/Assets/Scrips/Components/Quad/FittedQuad.cs
+++ b/Assets/Scrips/Components/Quad/FittedQuad.cs
@@ -19,6 +19,12 @@ public class FittedQuad : MonoBehaviour
     {
         InitAnchors();
         EditState.Instance.RegisterListener(EditStateHandler);
+        EditStateHandler();
+    }
+
+    private void OnDestroy()
+    {
+        EditState.Instance.RemoveListener(EditStateHandler);
     }
 
     private void EditStateHandler()
@@ -47,12 +53,19 @@ public class FittedQuad : MonoBehaviour
         }
     }
 
-    public void UpdateVertices()
+    public void UpdateVertices(bool globalCoordinates = false)
     {
         Vector3[] vertices = _mesh.vertices;
         for (int i = 0; i < vertices.Length; i++)
         {
-            vertices[i] = _vertexAnchors[i].transform.localPosition;
+            if (globalCoordinates)
+            {
+                vertices[i] = Quaternion.Inverse(_vertexAnchors[i].transform.rotation) * (_vertexAnchors[i].transform.position - this.transform.position);
+            }
+            else
+            {
+                vertices[i] = _vertexAnchors[i].transform.localPosition;
+            }
         }
 
         _mesh.vertices = vertices;
@@ -63,11 +76,15 @@ public class FittedQuad : MonoBehaviour
 
     public void SetAnchors(Vector3[] positions)
     {
-        for (int i = 0; i < positions.Length; i++)
+        Vector3[] vertices = _mesh.vertices;
+        for (int i = 0; i < vertices.Length; i++)
         {
-            _vertexAnchors[i].transform.localPosition = positions[i];
+            vertices[i] = positions[i];
         }
 
-        UpdateVertices();
+        _mesh.vertices = vertices;
+        _mesh.RecalculateBounds();
+        MeshCollider collider = GetComponent<MeshCollider>();
+        collider.sharedMesh = _mesh;
     }
 }
diff --git a/Assets/Scrips/Components/Quad/VertexPoint.cs b/Assets/Scrips/Components/Quad/VertexPoint.cs
index 74b0bf1..f266ed4 100644
--- a/Assets/Scrips/Components/Quad/VertexPoint.cs
+++ b/Assets/Scrips/Components/Quad/VertexPoint.cs
@@ -35,6 +35,6 @@ public class VertexPoint : MonoBehaviour
 
     private void FixedUpdate()
     {
-        if (_isSelected) _parentQuad.UpdateVertices();
+        if (_isSelected) _parentQuad.UpdateVertices(true);
     }
 }
diff --git a/Assets/Scrips/Contexts/EditState.cs b/Assets/Scrips/Contexts/EditState.cs
index 05caf7c..952eb41 100644
--- a/Assets/Scrips/Contexts/EditState.cs
+++ b/Assets/Scrips/Contexts/EditState.cs
@@ -58,4 +58,12 @@ public class EditState : MonoBehaviour
             _registeredListeners.Add(listener);
         }
     }
+
+    public void RemoveListener(Action listener)
+    {
+        if (_registeredListeners.Contains(listener))
+        {
+            _registeredListeners.Remove(listener);
+        }
+    }
 }
diff --git a/Assets/Scrips/Controllers/ProgramController.cs b/Assets/Scrips/Controllers/ProgramController.cs
index 90e800e..a480726 100644
--- a/Assets/Scrips/Controllers/ProgramController.cs
+++ b/Assets/Scrips/Controllers/ProgramController.cs
@@ -6,6 +6,8 @@ using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 using UnityEngine.InputSystem;
+using UnityEditor.Formats.Fbx.Exporter;
+using Autodesk.Fbx;
 
 public class ProgramController : MonoBehaviour
 {
@@ -138,9 +140,39 @@ public class ProgramController : MonoBehaviour
             UserTransform.position = save.UserTransform[0];
             UserTransform.eulerAngles = save.UserTransform[1];
             spawnBlockEvent.LoadBlocks(save.Blocks, save.BlockAnchors);
+            selectPointsController.LoadPlanes(save.Quads);
 
+        }
+    }
+
+    public void ExportMesh()
+    {
+        string filePath = Path.Combine(Application.dataPath, "mesh.fbx");
+        ModelExporter.ExportObject(filePath, this.gameObject);
+    }
+
+    protected void ExportScene(string fileName)
+    {
+        using (FbxManager fbxManager = FbxManager.Create())
+        {
+            // configure IO settings.
+            fbxManager.SetIOSettings(FbxIOSettings.Create(fbxManager, Globals.IOSROOT));
+
+            // Export the scene
+            using (FbxExporter exporter = FbxExporter.Create(fbxManager, "myExporter"))
+            {
+
+                // Initialize the exporter.
+                bool status = exporter.Initialize(fileName, -1, fbxManager.GetIOSettings());
 
+                // Create a new scene to export
+                FbxScene scene = FbxScene.Create(fbxManager, "myScene");
+
+                // Export the scene to the file.
+                exporter.Export(scene);
+            }
         }
     }
 
+
 }
diff --git a/Assets/Scrips/Controllers/SelectPointsController.cs b/Assets/Scrips/Controllers/SelectPointsController.cs
index 2ad5cec..eeab879 100644
--- a/Assets/Scrips/Controllers/SelectPointsController.cs
+++ b/Assets/Scrips/Controllers/SelectPointsController.cs
@@ -143,14 +143,14 @@ public class SelectPointsController : MonoBehaviour
         return projectedPoint;
     }
 
-    private void OnDrawGizmosSelected()
-    {
-        Gizmos.color = Color.red;
-        foreach (Vector3 pt in _debugPoints)
-        {
-            Gizmos.DrawSphere(pt, 0.05f);
-        }
-    }
+    // private void OnDrawGizmosSelected()
+    // {
+    //     Gizmos.color = Color.red;
+    //     foreach (Vector3 pt in _debugPoints)
+    //     {
+    //         Gizmos.DrawSphere(pt, 0.05f);
+    //     }
+    // }
 
     public void AddSelection(GameObject selection)
     {
diff --git a/Packages/manifest.json b/Packages/manifest.json
index 2fda25c..ad258d2 100644
--- a/Packages/manifest.json
+++ b/Packages/manifest.json
@@ -3,6 +3,7 @@
     "com.unity.collab-proxy": "2.3.1",
     "com.unity.feature.development": "1.0.1",
     "com.unity.feature.vr": "1.0.0",
+    "com.unity.formats.fbx": "4.2.1",
     "com.unity.learn.iet-framework": "3.1.3",
     "com.unity.render-pipelines.universal": "14.0.10",
     "com.unity.textmeshpro": "3.0.6",
diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json
index bd608f9..7094282 100644
--- a/Packages/packages-lock.json
+++ b/Packages/packages-lock.json
@@ -1,5 +1,12 @@
 {
   "dependencies": {
+    "com.autodesk.fbx": {
+      "version": "4.2.1",
+      "depth": 1,
+      "source": "registry",
+      "dependencies": {},
+      "url": "https://packages.unity.com"
+    },
     "com.unity.burst": {
       "version": "1.8.12",
       "depth": 1,
@@ -54,6 +61,16 @@
         "com.unity.xr.openxr": "1.9.1"
       }
     },
+    "com.unity.formats.fbx": {
+      "version": "4.2.1",
+      "depth": 0,
+      "source": "registry",
+      "dependencies": {
+        "com.autodesk.fbx": "4.2.1",
+        "com.unity.timeline": "1.7.1"
+      },
+      "url": "https://packages.unity.com"
+    },
     "com.unity.ide.rider": {
       "version": "3.0.27",
       "depth": 1,
@@ -201,9 +218,9 @@
       "depth": 0,
       "source": "registry",
       "dependencies": {
+        "com.unity.modules.audio": "1.0.0",
         "com.unity.modules.director": "1.0.0",
         "com.unity.modules.animation": "1.0.0",
-        "com.unity.modules.audio": "1.0.0",
         "com.unity.modules.particlesystem": "1.0.0"
       },
       "url": "https://packages.unity.com"
@@ -241,14 +258,14 @@
       "depth": 0,
       "source": "registry",
       "dependencies": {
+        "com.unity.ugui": "1.0.0",
         "com.unity.inputsystem": "1.7.0",
         "com.unity.mathematics": "1.2.6",
-        "com.unity.ugui": "1.0.0",
-        "com.unity.xr.core-utils": "2.2.3",
-        "com.unity.xr.legacyinputhelpers": "2.1.10",
         "com.unity.modules.audio": "1.0.0",
         "com.unity.modules.imgui": "1.0.0",
-        "com.unity.modules.physics": "1.0.0"
+        "com.unity.xr.core-utils": "2.2.3",
+        "com.unity.modules.physics": "1.0.0",
+        "com.unity.xr.legacyinputhelpers": "2.1.10"
       },
       "url": "https://packages.unity.com"
     },
@@ -267,9 +284,9 @@
       "depth": 0,
       "source": "registry",
       "dependencies": {
-        "com.unity.modules.subsystems": "1.0.0",
         "com.unity.modules.vr": "1.0.0",
         "com.unity.modules.xr": "1.0.0",
+        "com.unity.modules.subsystems": "1.0.0",
         "com.unity.xr.legacyinputhelpers": "2.1.7"
       },
       "url": "https://packages.unity.com"
@@ -288,10 +305,10 @@
       "depth": 0,
       "source": "registry",
       "dependencies": {
-        "com.unity.xr.management": "4.4.0",
-        "com.unity.xr.legacyinputhelpers": "2.1.2",
         "com.unity.inputsystem": "1.6.3",
-        "com.unity.xr.core-utils": "2.1.1"
+        "com.unity.xr.core-utils": "2.1.1",
+        "com.unity.xr.management": "4.4.0",
+        "com.unity.xr.legacyinputhelpers": "2.1.2"
       },
       "url": "https://packages.unity.com"
     },
-- 
GitLab