From 8ec5de37149f924cc4726bfffaa6d1f925b58c07 Mon Sep 17 00:00:00 2001
From: Abbeynesh Gnaneswaran <ag15g21@soton.ac.uk>
Date: Thu, 28 Nov 2024 13:40:00 +0000
Subject: [PATCH] Loads .obj into the scene

---
 AVVR/Assets/_Scripts/Import Scenery.cs          | 17 +++++++++++++++++
 AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs | 15 +++++++++++++--
 AVVR/ProjectSettings/EditorBuildSettings.asset  |  3 +++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/AVVR/Assets/_Scripts/Import Scenery.cs b/AVVR/Assets/_Scripts/Import Scenery.cs
index 8a98b1c..c1b3f96 100644
--- a/AVVR/Assets/_Scripts/Import Scenery.cs	
+++ b/AVVR/Assets/_Scripts/Import Scenery.cs	
@@ -155,6 +155,23 @@ public class ImportScenery : MonoBehaviour
         }
     }
 
+    public static GameObject GetNewMesh(string objPath, string mtlPath)
+    {
+        if (objPath == null || objPath == "" || !objPath.EndsWith(".obj"))
+        {
+            return null;
+        }
+
+        if (mtlPath == null || mtlPath == "" || !mtlPath.EndsWith(".mtl"))
+        {
+            return null;
+        }
+            
+        // use objloader library to load in new game object from mesh filepath
+        GameObject newObj = new OBJLoader().Load(objPath, mtlPath);
+        return newObj;
+    }
+
     static void InitialiseMesh(GameObject uninitialisedGameObject)
     {
         // add assign materials component to game object (will iterate through children to assign)
diff --git a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs
index ae37c88..ede5b1b 100644
--- a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs
+++ b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs
@@ -9,6 +9,7 @@ public class BuildRoom : MonoBehaviour {
     public Button buildButton; // The submit/build room button
 
     public GameObject interactableAudioSourcePrefab;
+    public Dictionary<string, object> allData;
 
     void Update() {
         // Check if all inputs have been entered and enable/disable the button
@@ -58,7 +59,7 @@ public class BuildRoom : MonoBehaviour {
     // }
     public void OnButtonPress() {
         // Compile all the inputs into one dictionary
-        Dictionary<string, object> allData = new Dictionary<string, object>();
+        allData = new Dictionary<string, object>();
         foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) {
             allData[labelledInputValidator.name] = labelledInputValidator.inputValidator.GetData();
         }
@@ -67,7 +68,10 @@ public class BuildRoom : MonoBehaviour {
 
         // Load the VR prefab scene
         SceneManager.LoadScene("VRPrefabScene");
+        SceneManager.sceneLoaded += OnSceneLoaded;
+    }
 
+    void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
         // Extract speaker data from the dictionary
         if (allData.ContainsKey("speakers"))
         {
@@ -96,7 +100,14 @@ public class BuildRoom : MonoBehaviour {
             }
         }
 
-        // TODO: Handle .obj and .mtl files
+        // Handles .obj and .mtl files
+        Dictionary<string, object> objDict = (Dictionary<string, object>) allData["obj"];
+        string objPath = (string) objDict["path"];
+
+        Dictionary<string, object> mtlDict = (Dictionary<string, object>) allData["mtl"];
+        string mtlPath = (string) mtlDict["path"];
+
+        ImportScenery.ImportScene(ImportScenery.GetNewMesh(objPath, mtlPath));
     }
 
     string DictionaryToString(Dictionary<string, object> dictionary, int depth = 0) {
diff --git a/AVVR/ProjectSettings/EditorBuildSettings.asset b/AVVR/ProjectSettings/EditorBuildSettings.asset
index b6d8a1c..0a193e3 100644
--- a/AVVR/ProjectSettings/EditorBuildSettings.asset
+++ b/AVVR/ProjectSettings/EditorBuildSettings.asset
@@ -11,6 +11,9 @@ EditorBuildSettings:
   - enabled: 1
     path: Assets/Scenes/MenuInterface.unity
     guid: 71ca599599e48294ea268eef3bfef344
+  - enabled: 1
+    path: Assets/Prefabs/VRPrefabScene.unity
+    guid: 6929320f83fe53548ac8d17e96c11caa
   m_configObjects:
     Unity.XR.Oculus.Settings: {fileID: 11400000, guid: bfa1182bd221b4ca89619141f66f1260, type: 2}
     Unity.XR.WindowsMR.Settings: {fileID: 11400000, guid: dc5a169419fa04987b057f65238cf3ba, type: 2}
-- 
GitLab