diff --git a/AVVR/Assets/Scenes/MenuInterface.unity b/AVVR/Assets/Scenes/MenuInterface.unity index 13f4dabf3ed0b150813e25172752404ca3b78cec..c79311b532f50ba5aa688a93ee2b23e05c30c6ff 100644 --- a/AVVR/Assets/Scenes/MenuInterface.unity +++ b/AVVR/Assets/Scenes/MenuInterface.unity @@ -4704,7 +4704,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 8298045291338814965, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_LocalPosition.y - value: 0 + value: 0.25 objectReference: {fileID: 0} - target: {fileID: 8298045291338814965, guid: 9a92d51909392834aa8dc2db232d87b8, type: 3} propertyPath: m_LocalPosition.z @@ -6172,7 +6172,7 @@ Transform: m_GameObject: {fileID: 1600246031} serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalPosition: {x: 0, y: 0.155, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs index f8602eabd76fa7f0b3001fc6c309636698b96a0c..5914a481076edd7ee0a3b202dd8efb13f2b53b2c 100644 --- a/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs +++ b/AVVR/Assets/_Scripts/MenuInterface/BuildRoom.cs @@ -5,25 +5,31 @@ using System.IO; using UnityEngine; using UnityEngine.UI; -public class BuildRoom : MonoBehaviour { +public class BuildRoom : MonoBehaviour +{ public LabelledInputValidator[] labelledInputValidators; // All the input validators and their respective labels public Button buildButton; // The submit/build room button public GameObject audioSourcePrefab; // Reference to the audio source prefab private ImportScenery importScenery; // Reference to ImportScenery component - void Start() { + void Start() + { // Get or add ImportScenery component importScenery = gameObject.GetComponent<ImportScenery>(); - if (importScenery == null) { + if (importScenery == null) + { importScenery = gameObject.AddComponent<ImportScenery>(); } } - void Update() { + void Update() + { // Check if all inputs have been entered and enable/disable the button - foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) { + foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) + { InputValidator inputValidator = labelledInputValidator.inputValidator; - if (!inputValidator.IsValid()) { + if (!inputValidator.IsValid()) + { buildButton.interactable = false; return; } @@ -65,10 +71,12 @@ public class BuildRoom : MonoBehaviour { // } // } // } - public void OnButtonPress() { + public void OnButtonPress() + { // Compile all the inputs into one dictionary Dictionary<string, object> allData = new Dictionary<string, object>(); - foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) { + foreach (LabelledInputValidator labelledInputValidator in labelledInputValidators) + { allData[labelledInputValidator.name] = labelledInputValidator.inputValidator.GetData(); } @@ -81,94 +89,139 @@ public class BuildRoom : MonoBehaviour { Dictionary<string, object> startPosition = allData["startPosition"] as Dictionary<string, object>; Dictionary<string, object> speakers = allData["speakers"] as Dictionary<string, object>; - if (objData == null || mtlData == null || roomDimensions == null || startPosition == null || speakers == null) { + if (objData == null || mtlData == null || roomDimensions == null || startPosition == null || speakers == null) + { Debug.LogError("Invalid data structure"); return; } // Import the scene using ImportScenery string objPath = objData["path"].ToString(); - string mtlPath = mtlData["path"].ToString(); + string mtlPath = mtlData["path"].ToString(); ImportScenery.ImportScene(ImportScenery.GetNewMesh(objPath, mtlPath)); - - // Set player start position - GameObject xrOrigin = GameObject.Find("Complete XR Origin Set Up Variant AVVR"); - if (xrOrigin != null) { - Vector3 position = new Vector3( - Convert.ToSingle(startPosition["x"]), - Convert.ToSingle(startPosition["y"]), - Convert.ToSingle(startPosition["z"]) + // find ModelWrapper in scene + GameObject importedScene = GameObject.Find("ModelWrapper"); + + // Scale the imported scene to match target dimensions + if (importedScene != null) + { + // Get target dimensions from input + Vector3 targetDimensions = new Vector3( + Convert.ToSingle(roomDimensions["x"]), + Convert.ToSingle(roomDimensions["y"]), + Convert.ToSingle(roomDimensions["z"]) ); - xrOrigin.transform.position = position; - } - // Create audio sources - foreach (KeyValuePair<string, object> speaker in speakers) { - Dictionary<string, object> speakerData = speaker.Value as Dictionary<string, object>; - if (speakerData != null) { - CreateAudioSource( - speakerData["path"].ToString(), - Convert.ToSingle(speakerData["x"]), - Convert.ToSingle(speakerData["y"]), - Convert.ToSingle(speakerData["z"]) + // Calculate current dimensions + DimensionResults results = MeshDimensionCalculatorLogic.CalculateDimensions(importedScene); + if (results != null) + { + // Reset position first + MeshDimensionCalculatorLogic.ResetToOrigin(importedScene); + + // Scale to target dimensions + MeshDimensionCalculatorLogic.ScaleToTargetDimensions(importedScene, results.dimensions, + targetDimensions); + + // Reset floor to Y=0 + MeshDimensionCalculatorLogic.ResetFloorToZero(importedScene, results.totalMin.y); + } + + // Set player start position + GameObject xrOrigin = GameObject.Find("Complete XR Origin Set Up Variant AVVR"); + if (xrOrigin != null) + { + Vector3 position = new Vector3( + Convert.ToSingle(startPosition["x"]), + Convert.ToSingle(startPosition["y"]), + Convert.ToSingle(startPosition["z"]) ); + xrOrigin.transform.position = position; } - } - } - private void CreateAudioSource(string audioPath, float x, float y, float z) { - // Find existing InteractableAudioSource in scene - GameObject audioSourceObj = GameObject.Find("InteractableAudioSource"); - if (audioSourceObj == null) { - Debug.LogError("Could not find InteractableAudioSource in scene!"); - return; + // Create audio sources + foreach (KeyValuePair<string, object> speaker in speakers) + { + Dictionary<string, object> speakerData = speaker.Value as Dictionary<string, object>; + if (speakerData != null) + { + CreateAudioSource( + speakerData["path"].ToString(), + Convert.ToSingle(speakerData["x"]), + Convert.ToSingle(speakerData["y"]), + Convert.ToSingle(speakerData["z"]) + ); + } + } } - // Update position - audioSourceObj.transform.position = new Vector3(x, y, z); + void CreateAudioSource(string audioPath, float x, float y, float z) + { + // Find existing InteractableAudioSource in scene + GameObject audioSourceObj = GameObject.Find("InteractableAudioSource"); + if (audioSourceObj == null) + { + Debug.LogError("Could not find InteractableAudioSource in scene!"); + return; + } - // Load and update audio clip - StartCoroutine(LoadAudio(audioPath, audioSourceObj)); - } + // Update position + audioSourceObj.transform.position = new Vector3(x, y, z); - private IEnumerator LoadAudio(string path, GameObject audioSourceObj) { - WWW www = new WWW("file://" + path); - yield return www; + // Load and update audio clip + StartCoroutine(LoadAudio(audioPath, audioSourceObj)); + } - if (string.IsNullOrEmpty(www.error)) { - AudioSource audioSource = audioSourceObj.GetComponent<AudioSource>(); - if (audioSource != null) { - audioSource.clip = www.GetAudioClip(); - Debug.Log($"Successfully loaded audio clip: {path}"); + IEnumerator LoadAudio(string path, GameObject audioSourceObj) + { + WWW www = new WWW("file://" + path); + yield return www; + + if (string.IsNullOrEmpty(www.error)) + { + AudioSource audioSource = audioSourceObj.GetComponent<AudioSource>(); + if (audioSource != null) + { + audioSource.clip = www.GetAudioClip(); + Debug.Log($"Successfully loaded audio clip: {path}"); + } + } + else + { + Debug.LogError($"Error loading audio clip: {www.error}"); } - } else { - Debug.LogError($"Error loading audio clip: {www.error}"); } - } - - string DictionaryToString(Dictionary<string, object> dictionary, int depth = 0) { - string indent = new string(' ', depth * 4); - string result = "{\n"; - - foreach (var kvp in dictionary) { - result += indent + $" \"{kvp.Key}\": "; - if (kvp.Value is Dictionary<string, object> nestedDict) { - result += DictionaryToString(nestedDict, depth + 1); - } else { - result += $"{kvp.Value}"; + string DictionaryToString(Dictionary<string, object> dictionary, int depth = 0) + { + string indent = new string(' ', depth * 4); + string result = "{\n"; + + foreach (var kvp in dictionary) + { + result += indent + $" \"{kvp.Key}\": "; + + if (kvp.Value is Dictionary<string, object> nestedDict) + { + result += DictionaryToString(nestedDict, depth + 1); + } + else + { + result += $"{kvp.Value}"; + } + + result += ",\n"; } - result += ",\n"; + result += indent + "}"; + return result; } - - result += indent + "}"; - return result; } -} -[Serializable] -public class LabelledInputValidator { - public string name; - public InputValidator inputValidator; + [Serializable] + public class LabelledInputValidator + { + public string name; + public InputValidator inputValidator; + } } \ No newline at end of file