From 0dba6d0dde6177c83039b3e1e6153889da40d257 Mon Sep 17 00:00:00 2001 From: Andy Baker Date: Tue, 18 Jun 2024 10:18:36 +0100 Subject: [PATCH 1/3] Add plugin hook for ShouldNodeExport --- Runtime/Scripts/GLTFSceneExporter.cs | 9 +++++++-- Runtime/Scripts/Plugins/Core/GltfExportPlugin.cs | 1 + Runtime/Scripts/Plugins/LodsExport.cs | 4 +++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Runtime/Scripts/GLTFSceneExporter.cs b/Runtime/Scripts/GLTFSceneExporter.cs index 9b238217f..f8e239c46 100644 --- a/Runtime/Scripts/GLTFSceneExporter.cs +++ b/Runtime/Scripts/GLTFSceneExporter.cs @@ -999,7 +999,8 @@ private SceneId ExportScene(string name, Transform[] rootObjTransforms) scene.Nodes = new List(rootObjTransforms.Length); foreach (var transform in rootObjTransforms) { - scene.Nodes.Add(ExportNode(transform)); + var node = ExportNode(transform); + if (node != null) scene.Nodes.Add(node); } _root.Scenes.Add(scene); @@ -1018,6 +1019,9 @@ private NodeId ExportNode(Transform nodeTransform) if (_exportedTransforms.TryGetValue(nodeTransform.GetInstanceID(), out var existingNodeId)) return new NodeId() { Id = existingNodeId, Root = _root }; + foreach (var plugin in _plugins) + if (!(plugin?.ShouldNodeExport(this, _root, nodeTransform) ?? true)) return null; + exportNodeMarker.Begin(); var node = new Node(); @@ -1158,7 +1162,8 @@ private NodeId ExportNode(Transform nodeTransform) foreach (var child in nonPrimitives) { if (!ShouldExportTransform(child.transform)) continue; - parentOfChilds.Children.Add(ExportNode(child.transform)); + var childNode = ExportNode(child.transform); + if (childNode != null) parentOfChilds.Children.Add(childNode); } } diff --git a/Runtime/Scripts/Plugins/Core/GltfExportPlugin.cs b/Runtime/Scripts/Plugins/Core/GltfExportPlugin.cs index 5eaa56e20..96d363718 100644 --- a/Runtime/Scripts/Plugins/Core/GltfExportPlugin.cs +++ b/Runtime/Scripts/Plugins/Core/GltfExportPlugin.cs @@ -15,6 +15,7 @@ public abstract class GLTFExportPluginContext { public virtual void BeforeSceneExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot) {} public virtual void AfterSceneExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot) {} + public virtual bool ShouldNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Transform transform) => true; public virtual void BeforeNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Transform transform, Node node) {} public virtual void AfterNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Transform transform, Node node) {} public virtual bool BeforeMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Material material, GLTFMaterial materialNode) => false; diff --git a/Runtime/Scripts/Plugins/LodsExport.cs b/Runtime/Scripts/Plugins/LodsExport.cs index 2b525e6d3..1d7ad7d31 100644 --- a/Runtime/Scripts/Plugins/LodsExport.cs +++ b/Runtime/Scripts/Plugins/LodsExport.cs @@ -46,7 +46,9 @@ public override void AfterNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfro return; } - nodeIds[index] = exporter.ExportNode(lod.renderers[0].gameObject).Id; + var lodNode = exporter.ExportNode(lod.renderers[0].gameObject); + if (lodNode != null) nodeIds[index] = lodNode.Id; + coverages[index] = lod.screenRelativeTransitionHeight; } // if (usesCulling) coverages[coverages.Length - 1] = 0; From c94e011644a066a2f4a012438ee64c1517901f08 Mon Sep 17 00:00:00 2001 From: Andy Baker Date: Thu, 29 Aug 2024 11:10:45 +0100 Subject: [PATCH 2/3] Revert automatic whitespace changes --- Runtime/Scripts/GLTFSceneExporter.cs | 38 ++++++++++++++-------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Runtime/Scripts/GLTFSceneExporter.cs b/Runtime/Scripts/GLTFSceneExporter.cs index 476d89d81..f08c355b3 100644 --- a/Runtime/Scripts/GLTFSceneExporter.cs +++ b/Runtime/Scripts/GLTFSceneExporter.cs @@ -39,7 +39,7 @@ public ExportContext(GLTFSettings settings) } public GLTFSceneExporter.RetrieveTexturePathDelegate TexturePathRetriever = (texture) => texture.name; - + // TODO Should we make all the callbacks on ExportContext obsolete? // Pro: We can remove them from the API // Con: No direct way to "just add callbacks" right now, always needs a plugin. @@ -53,7 +53,7 @@ public ExportContext(GLTFSettings settings) public GLTFSceneExporter.AfterTextureExportDelegate AfterTextureExport; public GLTFSceneExporter.AfterPrimitiveExportDelegate AfterPrimitiveExport; public GLTFSceneExporter.AfterMeshExportDelegate AfterMeshExport; - + internal GLTFExportPluginContext GetExportContextCallbacks() => new ExportContextCallbacks(this); #pragma warning disable CS0618 // Type or member is obsolete @@ -65,7 +65,7 @@ internal ExportContextCallbacks(ExportContext context) { _exportContext = context; } - + public override void BeforeSceneExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot) => _exportContext.BeforeSceneExport?.Invoke(exporter, gltfRoot); public override void AfterSceneExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot) => _exportContext.AfterSceneExport?.Invoke(exporter, gltfRoot); public override void AfterNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Transform transform, Node node) => _exportContext.AfterNodeExport?.Invoke(exporter, gltfRoot, transform, node); @@ -121,7 +121,7 @@ public partial class GLTFSceneExporter public delegate void AfterPrimitiveExportDelegate(GLTFSceneExporter exporter, Mesh mesh, MeshPrimitive primitive, int index); public delegate void AfterMeshExportDelegate(GLTFSceneExporter exporter, Mesh mesh, GLTFMesh gltfMesh, int index); - + private class LegacyCallbacksPlugin : GLTFExportPluginContext { public override void AfterSceneExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot) => GLTFSceneExporter.AfterSceneExport?.Invoke(exporter, gltfRoot); @@ -148,7 +148,7 @@ public override bool BeforeMaterialExport(GLTFSceneExporter exporter, GLTFRoot g } public override void AfterMaterialExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Material material, GLTFMaterial materialNode) => GLTFSceneExporter.AfterMaterialExport?.Invoke(exporter, gltfRoot, material, materialNode); } - + private static ILogger Debug = UnityEngine.Debug.unityLogger; private List _plugins = new List(); @@ -330,7 +330,7 @@ private Material GetConversionMaterial(TextureExportSettings textureMapType) case TextureExportSettings.Conversion.MetalGlossOcclusionChannelSwap: if (_metalGlossOcclusionChannelSwapMaterial && _metalGlossOcclusionChannelSwapMaterial.HasProperty("_SmoothnessMultiplier")) _metalGlossOcclusionChannelSwapMaterial.SetFloat("_SmoothnessMultiplier", textureMapType.smoothnessMultiplier); - + return _metalGlossOcclusionChannelSwapMaterial; default: return null; @@ -379,7 +379,7 @@ public struct ExportFileResult private Material _metalGlossChannelSwapMaterial; private Material _metalGlossOcclusionChannelSwapMaterial; - + private Material _normalChannelMaterial; private const uint MagicGLTF = 0x46546C67; @@ -578,12 +578,12 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportContext context) Debug = context.logger; else Debug = UnityEngine.Debug.unityLogger; - + // legacy: implicit plugin for all the static methods on GLTFSceneExporter _plugins.Add(new LegacyCallbacksPlugin()); // legacy: implicit plugin for all the methods on ExportContext _plugins.Add(context.GetExportContextCallbacks()); - + // create export plugin instances foreach (var plugin in settings.ExportPlugins) { @@ -601,7 +601,7 @@ public GLTFSceneExporter(Transform[] rootTransforms, ExportContext context) var metalGlossOcclusionChannelSwapShader = Resources.Load("MetalGlossOcclusionChannelSwap", typeof(Shader)) as Shader; _metalGlossOcclusionChannelSwapMaterial = new Material(metalGlossOcclusionChannelSwapShader); - + var normalChannelShader = Resources.Load("NormalChannel", typeof(Shader)) as Shader; _normalChannelMaterial = new Material(normalChannelShader); @@ -882,7 +882,7 @@ public void SaveGLTFandBin(string path, string fileName, bool exportTextures = t if (exportTextures) ExportImages(path); - + ExportFiles(path); gltfWriteOutMarker.End(); @@ -917,7 +917,7 @@ public static string GetFileName(string directory, string fileNameThatMayHaveExt private static string EnsureValidFileName(string filename) { if (filename == null) return ""; - + var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars())); var invalidReStr = string.Format(@"[{0}]+", invalidChars); @@ -965,7 +965,7 @@ private bool ShouldExportTransform(Transform transform) // Root transforms should *always* be exported since this is a deliberate decision by the user calling - it should override any other setting that would prevent the export (e.g. if a user calls Export with disabled or hidden objects the exporter should never prevent this) var isRoot = _rootTransforms.Contains(transform); if (isRoot) return true; - + if (!settings.ExportDisabledGameObjects && !transform.gameObject.activeSelf) { return false; @@ -978,7 +978,7 @@ private bool ShouldExportTransform(Transform transform) private SceneId ExportScene(string name, Transform[] rootObjTransforms) { if (rootObjTransforms == null || rootObjTransforms.Length < 1) return null; - + exportSceneMarker.Begin(); var scene = new GLTFScene(); @@ -1033,14 +1033,14 @@ private NodeId ExportNode(Transform nodeTransform) if (!(plugin?.ShouldNodeExport(this, _root, nodeTransform) ?? true)) return null; exportNodeMarker.Begin(); - + var node = new Node(); if (ExportNames) { node.Name = nodeTransform.name; } - + // TODO think more about how this callback is used – could e.g. be modifying the hierarchy, // and we would want to prevent exporting children of this node. // Could also be that we want to add a mesh based on some condition @@ -1106,7 +1106,7 @@ private NodeId ExportNode(Transform nodeTransform) node.Mesh = ExportMesh(nodeTransform.name, uniquePrimitives); RegisterPrimitivesWithNode(node, uniquePrimitives); - // Node - BlendShape Weights + // Node - BlendShape Weights if (uniquePrimitives[0].SkinnedMeshRenderer) { var meshObj = uniquePrimitives[0].Mesh; @@ -1214,7 +1214,7 @@ private void FilterPrimitives(Transform transform, out GameObject[] primitives, prims.Add(transform.gameObject); } } - + for (var i = 0; i < childCount; i++) { var go = transform.GetChild(i).gameObject; @@ -1366,7 +1366,7 @@ public TextureId GetTextureId(GLTFRoot root, UniqueTexture textureObj) } return null; } - + public MeshId GetMeshId(Mesh meshObj) { foreach (var primOwner in _primOwner) From d98a676d5bae3f27e3e48d03c8128e80536db385 Mon Sep 17 00:00:00 2001 From: hybridherbst Date: Mon, 25 Nov 2024 14:03:18 +0100 Subject: [PATCH 3/3] fix: assets with absolute URLs or paths that contain URL encoded information did not load correctly --- Runtime/Scripts/Loader/FileLoader.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Runtime/Scripts/Loader/FileLoader.cs b/Runtime/Scripts/Loader/FileLoader.cs index b3397a48b..c5479e51c 100644 --- a/Runtime/Scripts/Loader/FileLoader.cs +++ b/Runtime/Scripts/Loader/FileLoader.cs @@ -60,6 +60,11 @@ public static string CombinePaths(string basePath, string relativePath) } public Task LoadStreamAsync(string relativeFilePath) { + if (relativeFilePath.StartsWith("http://") || relativeFilePath.StartsWith("https://")) + { + return new UnityWebRequestLoader("").LoadStreamAsync(relativeFilePath); + } + #if UNITY_EDITOR string path = CombinePaths(_rootDirectoryPath, relativeFilePath); @@ -99,7 +104,7 @@ public Stream LoadStream(string relativeFilePath) string pathToLoad = Path.Combine(_rootDirectoryPath, relativeFilePath); if (!File.Exists(pathToLoad)) { - pathToLoad = Path.Combine(_rootDirectoryPath, Uri.UnescapeDataString(relativeFilePath)); + pathToLoad = Uri.UnescapeDataString(Path.Combine(_rootDirectoryPath, relativeFilePath)); } if (!File.Exists(pathToLoad))