From 267d2b138dd0ce54f6f4fc8573ba9f94e36a843d Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Tue, 22 Oct 2024 18:50:41 +0200 Subject: [PATCH 1/6] Add the functionality to import Gltf and Glb files This commit will add the importer, spawner and layer prefabs to import, persist and load a Gltf/Glb file. There is however the issue of textures not loading, but I think this has to do with the changes that we did to limit the number of shader variants for Gltfast; and as such stripping the texture loading. --- Assets/Prefabs/UI/Layers/AddLayerPanel.prefab | 2 +- Assets/Scenes/Main.unity | 2 +- .../FileTypeAdapter.asset | 34 +++++ Assets/Scriptables/PrefabLibrary.asset | 1 + Assets/_Functionalities/GltfImporter.meta | 3 + .../GltfImporter/GltfImporter.asset | 30 ++++ .../GltfImporter/GltfImporter.asset.meta | 8 ++ .../GltfImporter/Prefabs.meta | 8 ++ .../GltfImporter/Prefabs/GltfLayer.prefab | 131 ++++++++++++++++++ .../Prefabs/GltfLayer.prefab.meta | 7 + .../GltfImporter/ScriptableObjects.meta | 8 ++ .../ScriptableObjects/GltfImportAdapter.asset | 16 +++ .../GltfImportAdapter.asset.meta | 8 ++ .../GltfImporter/Scripts.meta | 3 + .../GltfImporter/Scripts/GltfImportAdapter.cs | 30 ++++ .../Scripts/GltfImportAdapter.cs.meta | 3 + .../GltfImporter/Scripts/GltfPropertyData.cs | 38 +++++ .../Scripts/GltfPropertyData.cs.meta | 3 + .../GltfImporter/Scripts/GltfSpawner.cs | 46 ++++++ .../GltfImporter/Scripts/GltfSpawner.cs.meta | 3 + 20 files changed, 382 insertions(+), 2 deletions(-) create mode 100644 Assets/_Functionalities/GltfImporter.meta create mode 100644 Assets/_Functionalities/GltfImporter/GltfImporter.asset create mode 100644 Assets/_Functionalities/GltfImporter/GltfImporter.asset.meta create mode 100644 Assets/_Functionalities/GltfImporter/Prefabs.meta create mode 100644 Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab create mode 100644 Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab.meta create mode 100644 Assets/_Functionalities/GltfImporter/ScriptableObjects.meta create mode 100644 Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset create mode 100644 Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset.meta create mode 100644 Assets/_Functionalities/GltfImporter/Scripts.meta create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs.meta create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs.meta create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs create mode 100644 Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs.meta diff --git a/Assets/Prefabs/UI/Layers/AddLayerPanel.prefab b/Assets/Prefabs/UI/Layers/AddLayerPanel.prefab index d12a5a3cf..566578466 100644 --- a/Assets/Prefabs/UI/Layers/AddLayerPanel.prefab +++ b/Assets/Prefabs/UI/Layers/AddLayerPanel.prefab @@ -4743,7 +4743,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 3a728a3527b1f334390e5bfd25ce6aa5, type: 3} m_Name: m_EditorClassIdentifier: - fileExtentions: obj,csv,json,geojson + fileExtentions: obj,csv,json,geojson,glb,gltf multiSelect: 0 onFilesSelected: m_PersistentCalls: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index e039973c4..8bfbbd364 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -2177,7 +2177,7 @@ PrefabInstance: - target: {fileID: 1748615501916967598, guid: 0d52ffd9c87ab484b88610c4acf5db24, type: 3} propertyPath: fileExtentions - value: nl3d,obj,csv,json,geojson + value: nl3d,obj,csv,json,geojson,gltf,glb objectReference: {fileID: 0} - target: {fileID: 2388221261345448311, guid: 0d52ffd9c87ab484b88610c4acf5db24, type: 3} diff --git a/Assets/Scriptables/DataTypeImportAdapters/FileTypeAdapter.asset b/Assets/Scriptables/DataTypeImportAdapters/FileTypeAdapter.asset index 601815276..09dd87b70 100644 --- a/Assets/Scriptables/DataTypeImportAdapters/FileTypeAdapter.asset +++ b/Assets/Scriptables/DataTypeImportAdapters/FileTypeAdapter.asset @@ -29,6 +29,40 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 + - Extension: glb + FileReceived: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 11400000, guid: 32ff31abfa785a04b9ca89ebc8abcddc, type: 2} + m_TargetAssemblyTypeName: Netherlands3D.Twin.Functionalities.GltfImporter.GltfImportAdapter, + Assembly-CSharp + m_MethodName: Execute + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - Extension: gltf + FileReceived: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 11400000, guid: 32ff31abfa785a04b9ca89ebc8abcddc, type: 2} + m_TargetAssemblyTypeName: Netherlands3D.Twin.Functionalities.GltfImporter.GltfImportAdapter, + Assembly-CSharp + m_MethodName: Execute + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 - Extension: csv FileReceived: m_PersistentCalls: diff --git a/Assets/Scriptables/PrefabLibrary.asset b/Assets/Scriptables/PrefabLibrary.asset index fc52482ac..e2d08e0fd 100644 --- a/Assets/Scriptables/PrefabLibrary.asset +++ b/Assets/Scriptables/PrefabLibrary.asset @@ -55,3 +55,4 @@ MonoBehaviour: - {fileID: 2205593157182618917, guid: c57dd4a997e79ce43895144d0702166c, type: 3} - {fileID: 8209116542904522024, guid: 34882a73ff6122243a0e3e9811473e20, type: 3} - {fileID: 7611614742349668521, guid: 7ddb78a6acbf44d4e84910b5684042b7, type: 3} + - {fileID: 8209116542904522024, guid: a0c050d9c6beacb4593d8b7fccc08d05, type: 3} diff --git a/Assets/_Functionalities/GltfImporter.meta b/Assets/_Functionalities/GltfImporter.meta new file mode 100644 index 000000000..f294995f1 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 99cd6eca4d5b4b6eb347a25509f222d0 +timeCreated: 1729609517 \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/GltfImporter.asset b/Assets/_Functionalities/GltfImporter/GltfImporter.asset new file mode 100644 index 000000000..41d2e5eb6 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/GltfImporter.asset @@ -0,0 +1,30 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 714e859e1c414d479484fe937804ce24, type: 3} + m_Name: GltfImporter + m_EditorClassIdentifier: + data: + Id: 3d-modellen-toevoegen + IsEnabled: 1 + Title: 3D modellen toevoegen + Caption: Gltf / Glb + Header: Eigen .Gltf bestanden tonen in de 3D viewer + Description: "Maak het mogelijk jouw eigen .gltf of .glb modellen in de 3D wereld + te plaatsen. \n\nZorg dat het 3D model aan de volgende eisen voldoet:\n\n- Gltf + of Glb formaat\n- Maximaal 300MB bestandsgrootte\n" + configuration: {fileID: 0} + OnEnable: + m_PersistentCalls: + m_Calls: [] + OnDisable: + m_PersistentCalls: + m_Calls: [] diff --git a/Assets/_Functionalities/GltfImporter/GltfImporter.asset.meta b/Assets/_Functionalities/GltfImporter/GltfImporter.asset.meta new file mode 100644 index 000000000..d8873fa75 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/GltfImporter.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1a1a839e196ea364b8ee482eebb3e3f6 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Functionalities/GltfImporter/Prefabs.meta b/Assets/_Functionalities/GltfImporter/Prefabs.meta new file mode 100644 index 000000000..27fe39256 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Prefabs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 63d843effe7c2a648a73965d7df4d794 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab b/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab new file mode 100644 index 000000000..617fceda6 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab @@ -0,0 +1,131 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &9204913902124509902 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 4013969559654378043} + - component: {fileID: 1016696483101979518} + - component: {fileID: 1646213389209705716} + - component: {fileID: 8128677453936289607} + - component: {fileID: 2362124063262672280} + - component: {fileID: 3482478533686408333} + - component: {fileID: 3094970985324844986} + m_Layer: 0 + m_Name: GltfLayer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &4013969559654378043 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1016696483101979518 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 92795e992dcbe4b3c8ada171c5551674, type: 3} + m_Name: + m_EditorClassIdentifier: + prefabIdentifier: gltflayer + onShow: + m_PersistentCalls: + m_Calls: [] + onHide: + m_PersistentCalls: + m_Calls: [] + objectCreated: + m_PersistentCalls: + m_Calls: [] +--- !u!114 &1646213389209705716 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 630f741154ed48488dc6602ef25dab3a, type: 3} + m_Name: + m_EditorClassIdentifier: + worldTransformShifter: {fileID: 8128677453936289607} + referenceCoordinateSystem: 2 + onPreShift: + m_PersistentCalls: + m_Calls: [] + onPostShift: + m_PersistentCalls: + m_Calls: [] +--- !u!114 &8128677453936289607 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 50fab43e07a540b89d245bd4ec8e4887, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &2362124063262672280 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b8cceefce4ff4530b78245056d126688, type: 3} + m_Name: + m_EditorClassIdentifier: + propertySectionPrefab: {fileID: 964705144372893445, guid: b2b695c4876427c439761dd2521bf672, + type: 3} +--- !u!114 &3482478533686408333 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9ffc363b02a0945c7ad8cdc45e55f5ae, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &3094970985324844986 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9204913902124509902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4ec1a3dc060341bc9f895829eb82cc1e, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab.meta b/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab.meta new file mode 100644 index 000000000..31df603a6 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Prefabs/GltfLayer.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: a0c050d9c6beacb4593d8b7fccc08d05 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Functionalities/GltfImporter/ScriptableObjects.meta b/Assets/_Functionalities/GltfImporter/ScriptableObjects.meta new file mode 100644 index 000000000..a7fae38ec --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/ScriptableObjects.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b9c8b461cab3db9448997d05d94f9148 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset b/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset new file mode 100644 index 000000000..f26f05fb6 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset @@ -0,0 +1,16 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 72250ba7f5614f81ad22fff0a1354a4c, type: 3} + m_Name: GltfImportAdapter + m_EditorClassIdentifier: + layerPrefab: {fileID: 3094970985324844986, guid: a0c050d9c6beacb4593d8b7fccc08d05, + type: 3} diff --git a/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset.meta b/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset.meta new file mode 100644 index 000000000..720d246f1 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/ScriptableObjects/GltfImportAdapter.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 32ff31abfa785a04b9ca89ebc8abcddc +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Functionalities/GltfImporter/Scripts.meta b/Assets/_Functionalities/GltfImporter/Scripts.meta new file mode 100644 index 000000000..7ea8fa2cd --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e3954afa414b42bba94ce8dde59578d4 +timeCreated: 1729609517 \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs new file mode 100644 index 000000000..257bd9455 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs @@ -0,0 +1,30 @@ +using System.IO; +using Netherlands3D.Twin.Layers.Properties; +using Netherlands3D.Twin.Projects; +using UnityEngine; + +namespace Netherlands3D.Twin.Functionalities.GltfImporter +{ + [CreateAssetMenu(menuName = "Netherlands3D/Adapters/GltfImportAdapter", fileName = "GltfImportAdapter", order = 0)] + public class GltfImportAdapter : ScriptableObject, IDataTypeAdapter + { + [SerializeField] private GltfSpawner layerPrefab; + + public bool Supports(LocalFile localFile) + { + return localFile.LocalFilePath.EndsWith(".glb") + || localFile.LocalFilePath.EndsWith(".gltf"); + } + + public void Execute(LocalFile localFile) + { + var fullPath = localFile.LocalFilePath; + var fileName = Path.GetFileName(fullPath); + GltfSpawner newLayer = Instantiate(layerPrefab); + newLayer.gameObject.name = fileName; + + var propertyData = newLayer.PropertyData as GltfPropertyData; + propertyData.Uri = AssetUriFactory.CreateProjectAssetUri(fullPath); + } + } +} \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs.meta b/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs.meta new file mode 100644 index 000000000..a79b80312 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfImportAdapter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 72250ba7f5614f81ad22fff0a1354a4c +timeCreated: 1729609517 \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs new file mode 100644 index 000000000..328f5881e --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; +using Netherlands3D.Twin.Layers; +using Netherlands3D.Twin.Layers.Properties; +using Newtonsoft.Json; +using UnityEngine.Events; + +namespace Netherlands3D.Twin.Functionalities.GltfImporter +{ + [DataContract(Namespace = "https://netherlands3d.eu/schemas/projects/layers/properties", Name = "Gltf")] + public class GltfPropertyData : LayerPropertyData, ILayerPropertyDataWithAssets + { + [DataMember] private Uri uri; + + [JsonIgnore] public readonly UnityEvent OnUriChanged = new(); + + [JsonIgnore] + public Uri Uri + { + get => uri; + set + { + uri = value; + OnUriChanged.Invoke(value); + } + } + + public IEnumerable GetAssets() + { + return new List + { + new (this, uri) + }; + } + } +} diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs.meta b/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs.meta new file mode 100644 index 000000000..2b9aa44c8 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfPropertyData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4627542af8e2470e9f772142dcb69623 +timeCreated: 1729609517 \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs new file mode 100644 index 000000000..d26589185 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using GLTFast; +using Netherlands3D.Twin.Layers.Properties; +using UnityEngine; + +namespace Netherlands3D.Twin.Functionalities.GltfImporter +{ + public class GltfSpawner : MonoBehaviour, ILayerWithPropertyData + { + private GltfPropertyData propertyData = new(); + public LayerPropertyData PropertyData => propertyData; + + private void Awake() + { + gameObject.transform.position = ObjectPlacementUtility.GetSpawnPoint(); + } + + private async void Start() + { + var localPath = propertyData.Uri.LocalPath.TrimStart('/', '\\'); + var path = Path.Combine(Application.persistentDataPath, localPath); + + byte[] data = await File.ReadAllBytesAsync(path); + var gltf = new GltfImport(); + bool success = await gltf.LoadGltfBinary(data, new Uri(path)); + if (success) { + await gltf.InstantiateMainSceneAsync(transform); + } + } + + public void LoadProperties(List properties) + { + var propertyData = properties.OfType().FirstOrDefault(); + if (propertyData == null) return; + + // Property data is set here, and the parsing and loading of the actual data is done + // in the start method, there a coroutine is started to load the data in a streaming fashion. + // If we do that here, then this may conflict with the loading of the project file and it would + // cause duplication when adding a layer manually instead of through the loading mechanism + this.propertyData = propertyData; + } + } +} \ No newline at end of file diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs.meta b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs.meta new file mode 100644 index 000000000..267cbb270 --- /dev/null +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4ec1a3dc060341bc9f895829eb82cc1e +timeCreated: 1729609517 \ No newline at end of file From 1d24b901d09e0911b07f74aa7a7f39832c3c9cb1 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Wed, 23 Oct 2024 06:42:27 +0200 Subject: [PATCH 2/6] For some reason, the wrong prefab reference was stored --- Assets/Scriptables/PrefabLibrary.asset | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Scriptables/PrefabLibrary.asset b/Assets/Scriptables/PrefabLibrary.asset index e2d08e0fd..c07f9e4c3 100644 --- a/Assets/Scriptables/PrefabLibrary.asset +++ b/Assets/Scriptables/PrefabLibrary.asset @@ -55,4 +55,4 @@ MonoBehaviour: - {fileID: 2205593157182618917, guid: c57dd4a997e79ce43895144d0702166c, type: 3} - {fileID: 8209116542904522024, guid: 34882a73ff6122243a0e3e9811473e20, type: 3} - {fileID: 7611614742349668521, guid: 7ddb78a6acbf44d4e84910b5684042b7, type: 3} - - {fileID: 8209116542904522024, guid: a0c050d9c6beacb4593d8b7fccc08d05, type: 3} + - {fileID: 1016696483101979518, guid: a0c050d9c6beacb4593d8b7fccc08d05, type: 3} From 94c5a2af1204d1a9a8e28e7325acb4bb4f107783 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Wed, 23 Oct 2024 07:22:08 +0200 Subject: [PATCH 3/6] Add debug messages to verify if it enters the spawner, and loads the data --- .../_Functionalities/GltfImporter/Scripts/GltfSpawner.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs index d26589185..ee8a0295a 100644 --- a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs @@ -23,11 +23,19 @@ private async void Start() var localPath = propertyData.Uri.LocalPath.TrimStart('/', '\\'); var path = Path.Combine(Application.persistentDataPath, localPath); + Debug.Log("Reading GLB/GLTF file"); byte[] data = await File.ReadAllBytesAsync(path); var gltf = new GltfImport(); + Debug.Log("Loading GLB/GLTF binary data"); bool success = await gltf.LoadGltfBinary(data, new Uri(path)); if (success) { + Debug.Log("Creating Scene object(s) for GLB/GLTF"); await gltf.InstantiateMainSceneAsync(transform); + Debug.Log("Created Scene object(s) for GLB/GLTF"); + } + else + { + Debug.LogError("Failed to load GLB/GLTF binary data"); } } From 444282a00f67d939f84f216e0f6fafac31202e6f Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Wed, 23 Oct 2024 07:50:52 +0200 Subject: [PATCH 4/6] Removed async from readall ytes, it seems to block --- Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs index ee8a0295a..82418fe72 100644 --- a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs @@ -24,7 +24,7 @@ private async void Start() var path = Path.Combine(Application.persistentDataPath, localPath); Debug.Log("Reading GLB/GLTF file"); - byte[] data = await File.ReadAllBytesAsync(path); + byte[] data = File.ReadAllBytes(path); var gltf = new GltfImport(); Debug.Log("Loading GLB/GLTF binary data"); bool success = await gltf.LoadGltfBinary(data, new Uri(path)); From 7fe46e7d75154320869c5c69514c8e8be0090e97 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Wed, 23 Oct 2024 08:50:40 +0200 Subject: [PATCH 5/6] Rewrite async handling to use a coroutine in the end --- .../GltfImporter/Scripts/GltfSpawner.cs | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs index 82418fe72..6fc5306ff 100644 --- a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs @@ -1,7 +1,9 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; using GLTFast; using Netherlands3D.Twin.Layers.Properties; using UnityEngine; @@ -18,25 +20,30 @@ private void Awake() gameObject.transform.position = ObjectPlacementUtility.GetSpawnPoint(); } - private async void Start() + private IEnumerator Start() { var localPath = propertyData.Uri.LocalPath.TrimStart('/', '\\'); var path = Path.Combine(Application.persistentDataPath, localPath); + yield return LoadModel(path); + } + + private async Task LoadModel(string path) + { Debug.Log("Reading GLB/GLTF file"); - byte[] data = File.ReadAllBytes(path); + byte[] data = await File.ReadAllBytesAsync(path); var gltf = new GltfImport(); Debug.Log("Loading GLB/GLTF binary data"); bool success = await gltf.LoadGltfBinary(data, new Uri(path)); - if (success) { - Debug.Log("Creating Scene object(s) for GLB/GLTF"); - await gltf.InstantiateMainSceneAsync(transform); - Debug.Log("Created Scene object(s) for GLB/GLTF"); - } - else + if (!success) { Debug.LogError("Failed to load GLB/GLTF binary data"); + return; } + + Debug.Log("Creating Scene object(s) for GLB/GLTF"); + await gltf.InstantiateMainSceneAsync(transform); + Debug.Log("Created Scene object(s) for GLB/GLTF"); } public void LoadProperties(List properties) From 13e05cda1e7305c809a7a3923c99926c50e98905 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Wed, 23 Oct 2024 15:32:48 +0200 Subject: [PATCH 6/6] Test by splitting coroutine from start --- .../_Functionalities/GltfImporter/Scripts/GltfSpawner.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs index 6fc5306ff..e53375b83 100644 --- a/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs +++ b/Assets/_Functionalities/GltfImporter/Scripts/GltfSpawner.cs @@ -20,11 +20,16 @@ private void Awake() gameObject.transform.position = ObjectPlacementUtility.GetSpawnPoint(); } - private IEnumerator Start() + private void Start() { var localPath = propertyData.Uri.LocalPath.TrimStart('/', '\\'); var path = Path.Combine(Application.persistentDataPath, localPath); + StartCoroutine(StartLoading(path)); + } + + private IEnumerator StartLoading(string path) + { yield return LoadModel(path); } @@ -32,6 +37,7 @@ private async Task LoadModel(string path) { Debug.Log("Reading GLB/GLTF file"); byte[] data = await File.ReadAllBytesAsync(path); + Debug.Log("Instantiate GltfImport"); var gltf = new GltfImport(); Debug.Log("Loading GLB/GLTF binary data"); bool success = await gltf.LoadGltfBinary(data, new Uri(path));