From a6fb9cc649967f8bc29aaaa06ea11fcecbe6526c Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Sat, 21 Aug 2021 13:54:40 +0200 Subject: [PATCH 01/14] Merge materials on layer or object level as well as the default material. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 74 ++++++++++++++++------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 0ca9ab1..14e76e7 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -1,13 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Rhino.DocObjects; +using glTFLoader.Schema; using Rhino; -using glTFLoader.Schema; -using Rhino.Render; using Rhino.Display; +using Rhino.DocObjects; +using Rhino.Render; +using System; +using System.Collections.Generic; +using System.Linq; namespace glTF_BinExporter { @@ -50,6 +48,8 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do private Dictionary layers = new Dictionary(); + private Dictionary layerMaterialIndices = new Dictionary(); + public Gltf ConvertToGltf() { dummy.Scene = 0; @@ -68,7 +68,7 @@ public Gltf ConvertToGltf() WrapT = Sampler.WrapTEnum.REPEAT, }); - if(options.UseDracoCompression) + if (options.UseDracoCompression) { dummy.ExtensionsUsed.Add(Constants.DracoMeshCompressionExtensionTag); dummy.ExtensionsRequired.Add(Constants.DracoMeshCompressionExtensionTag); @@ -79,7 +79,7 @@ public Gltf ConvertToGltf() var sanitized = SanitizeRhinoObjects(objects); - foreach(ObjectExportData exportData in sanitized) + foreach (ObjectExportData exportData in sanitized) { int[] materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); @@ -94,7 +94,7 @@ public Gltf ConvertToGltf() int nodeIndex = dummy.Nodes.AddAndReturnIndex(node); - if(options.ExportLayers) + if (options.ExportLayers) { AddToLayer(RhinoDoc.ActiveDoc.Layers[exportData.Object.Attributes.LayerIndex], nodeIndex); } @@ -104,7 +104,7 @@ public Gltf ConvertToGltf() } } - if(binary && binaryBuffer.Count > 0) + if (binary && binaryBuffer.Count > 0) { //have to add the empty buffer for the binary file header dummy.Buffers.Add(new glTFLoader.Schema.Buffer() @@ -119,7 +119,7 @@ public Gltf ConvertToGltf() private void AddToLayer(Layer layer, int child) { - if(layers.TryGetValue(layer.Index, out Node node)) + if (layers.TryGetValue(layer.Index, out Node node)) { if (node.Children == null) { @@ -165,8 +165,11 @@ public byte[] GetBinaryBuffer() int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) { + RhinoObject[] subObjects = rhinoObject.GetSubObjects(); int[] materialIndices = new int[materials.Length]; + Dictionary colorIndices = new Dictionary(); + for (int i = 0; i < materials.Length; i++) { var material = materials[i]; @@ -176,18 +179,36 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) return null; } + Guid materialId; if (material == null && options.UseDisplayColorForUnsetMaterials) { - Color4f objectColor = GetObjectColor(rhinoObject); - materialIndices[i] = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject)); + if (options.ExportLayers) + { + if (subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) + { + materialIndices[i] = GetLayerMaterial(rhinoObject); + continue; + } + } + + Color4f objectColor = GetObjectColor(subObjects[i]); + if (!colorIndices.TryGetValue(objectColor, out int colorIndex)) + { + colorIndex = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject)); + colorIndices.Add(objectColor, colorIndex); + } + materialIndices[i] = colorIndex; continue; } else if (material == null) { material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; + materialId = new Guid(); + } + else + { + materialId = material.Id; } - - Guid materialId = material.Id; if (!materialsMap.TryGetValue(materialId, out int materialIndex)) { @@ -202,6 +223,17 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) return materialIndices; } + private int GetLayerMaterial(RhinoObject rhinoObject) + { + if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex)) + { + Color4f objectColor = GetObjectColor(rhinoObject); + layerMaterialIndex = CreateSolidColorMaterial(objectColor, RhinoDoc.ActiveDoc.Layers[rhinoObject.Attributes.LayerIndex].Name); + layerMaterialIndices.Add(rhinoObject.Attributes.LayerIndex, layerMaterialIndex); + } + return layerMaterialIndex; + } + int CreateSolidColorMaterial(Color4f color, string name) { glTFLoader.Schema.Material material = new glTFLoader.Schema.Material() @@ -218,11 +250,11 @@ int CreateSolidColorMaterial(Color4f color, string name) Color4f GetObjectColor(RhinoObject rhinoObject) { - if(rhinoObject.Attributes.ColorSource == ObjectColorSource.ColorFromLayer) + if (rhinoObject.Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { int layerIndex = rhinoObject.Attributes.LayerIndex; - return new Color4f(rhinoObject.Document.Layers[layerIndex].Color); + return new Color4f(RhinoDoc.ActiveDoc.Layers[layerIndex].Color); } else { @@ -239,7 +271,7 @@ public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) return new Rhino.Geometry.Mesh[] { meshObj.MeshGeometry }; } - else if(rhinoObject.ObjectType == ObjectType.SubD) + else if (rhinoObject.ObjectType == ObjectType.SubD) { SubDObject subdObject = rhinoObject as SubDObject; @@ -303,7 +335,7 @@ public bool MeshIsValidForExport(Rhino.Geometry.Mesh mesh) return false; } - if(!options.ExportOpenMeshes && !mesh.IsClosed) + if (!options.ExportOpenMeshes && !mesh.IsClosed) { return false; } From b60665a606dd62218cededda79d4cadd86b6fed7 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Mon, 23 Aug 2021 11:04:04 +0300 Subject: [PATCH 02/14] Store the RhinoDoc on the RhinoDocGltfConverter --- glTF-BinExporter/GlTFExporterCommand.cs | 6 +++--- glTF-BinExporter/RhinoDocGltfConverter.cs | 6 +++++- glTF-BinExporter/glTFBinExporterPlugin.cs | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/glTF-BinExporter/GlTFExporterCommand.cs b/glTF-BinExporter/GlTFExporterCommand.cs index 650ca0b..b8c854c 100644 --- a/glTF-BinExporter/GlTFExporterCommand.cs +++ b/glTF-BinExporter/GlTFExporterCommand.cs @@ -47,7 +47,7 @@ protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode) Rhino.DocObjects.RhinoObject[] rhinoObjects = go.Objects().Select(o => o.Object()).ToArray(); - if(!DoExport(dialog.FileName, opts, binary, rhinoObjects, doc.RenderSettings.LinearWorkflow)) + if(!DoExport(dialog.FileName, opts, binary, doc, rhinoObjects, doc.RenderSettings.LinearWorkflow)) { return Result.Failure; } @@ -132,9 +132,9 @@ private SaveFileDialog GetSaveFileDialog() }; } - public static bool DoExport(string fileName, glTFExportOptions options, bool binary, IEnumerable rhinoObjects, Rhino.Render.LinearWorkflow workflow) + public static bool DoExport(string fileName, glTFExportOptions options, bool binary, RhinoDoc doc, IEnumerable rhinoObjects, Rhino.Render.LinearWorkflow workflow) { - RhinoDocGltfConverter converter = new RhinoDocGltfConverter(options, binary, rhinoObjects, workflow); + RhinoDocGltfConverter converter = new RhinoDocGltfConverter(options, binary, doc, rhinoObjects, workflow); glTFLoader.Schema.Gltf gltf = converter.ConvertToGltf(); if (binary) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 14e76e7..667d416 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -18,8 +18,9 @@ public struct ObjectExportData class RhinoDocGltfConverter { - public RhinoDocGltfConverter(glTFExportOptions options, bool binary, IEnumerable objects, LinearWorkflow workflow) + public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc doc, IEnumerable objects, LinearWorkflow workflow) { + this.doc = doc; this.options = options; this.binary = binary; this.objects = objects; @@ -28,12 +29,15 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, IEnumerable public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc doc, LinearWorkflow workflow) { + this.doc = doc; this.options = options; this.binary = binary; this.objects = doc.Objects; this.workflow = null; } + private RhinoDoc doc = null; + private IEnumerable objects = null; private bool binary = false; diff --git a/glTF-BinExporter/glTFBinExporterPlugin.cs b/glTF-BinExporter/glTFBinExporterPlugin.cs index 20995fd..330e846 100644 --- a/glTF-BinExporter/glTFBinExporterPlugin.cs +++ b/glTF-BinExporter/glTFBinExporterPlugin.cs @@ -54,7 +54,7 @@ protected override WriteFileResult WriteFile(string filename, int index, RhinoDo IEnumerable objects = GetObjectsToExport(doc, options); - if(!GlTFExporterCommand.DoExport(filename, exportOptions, binary, objects, doc.RenderSettings.LinearWorkflow)) + if(!GlTFExporterCommand.DoExport(filename, exportOptions, binary, doc, objects, doc.RenderSettings.LinearWorkflow)) { return WriteFileResult.Failure; } From c879f6c0e12125a5f1b34176d1cfe80bdb12aa8a Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 10:22:11 +0200 Subject: [PATCH 03/14] Use stored RhinoDoc instead of ActiveDoc. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 667d416..eec4841 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -100,7 +100,7 @@ public Gltf ConvertToGltf() if (options.ExportLayers) { - AddToLayer(RhinoDoc.ActiveDoc.Layers[exportData.Object.Attributes.LayerIndex], nodeIndex); + AddToLayer(doc.Layers[exportData.Object.Attributes.LayerIndex], nodeIndex); } else { @@ -144,7 +144,7 @@ private void AddToLayer(Layer layer, int child) layers.Add(layer.Index, node); int nodeIndex = dummy.Nodes.AddAndReturnIndex(node); - Layer parentLayer = RhinoDoc.ActiveDoc.Layers.FindId(layer.ParentLayerId); + Layer parentLayer = doc.Layers.FindId(layer.ParentLayerId); if (parentLayer == null) { @@ -232,7 +232,7 @@ private int GetLayerMaterial(RhinoObject rhinoObject) if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex)) { Color4f objectColor = GetObjectColor(rhinoObject); - layerMaterialIndex = CreateSolidColorMaterial(objectColor, RhinoDoc.ActiveDoc.Layers[rhinoObject.Attributes.LayerIndex].Name); + layerMaterialIndex = CreateSolidColorMaterial(objectColor, doc.Layers[rhinoObject.Attributes.LayerIndex].Name); layerMaterialIndices.Add(rhinoObject.Attributes.LayerIndex, layerMaterialIndex); } return layerMaterialIndex; @@ -258,7 +258,7 @@ Color4f GetObjectColor(RhinoObject rhinoObject) { int layerIndex = rhinoObject.Attributes.LayerIndex; - return new Color4f(RhinoDoc.ActiveDoc.Layers[layerIndex].Color); + return new Color4f(doc.Layers[layerIndex].Color); } else { From d12d92a4ff07f463ad185d5563aa4b72eb971369 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 12:46:10 +0200 Subject: [PATCH 04/14] Add GetLayerColor method. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index eec4841..00dd444 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -231,7 +231,7 @@ private int GetLayerMaterial(RhinoObject rhinoObject) { if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex)) { - Color4f objectColor = GetObjectColor(rhinoObject); + Color4f objectColor = GetLayerColor(rhinoObject); layerMaterialIndex = CreateSolidColorMaterial(objectColor, doc.Layers[rhinoObject.Attributes.LayerIndex].Name); layerMaterialIndices.Add(rhinoObject.Attributes.LayerIndex, layerMaterialIndex); } @@ -256,9 +256,7 @@ Color4f GetObjectColor(RhinoObject rhinoObject) { if (rhinoObject.Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { - int layerIndex = rhinoObject.Attributes.LayerIndex; - - return new Color4f(doc.Layers[layerIndex].Color); + return GetLayerColor(rhinoObject); } else { @@ -266,6 +264,12 @@ Color4f GetObjectColor(RhinoObject rhinoObject) } } + Color4f GetLayerColor(RhinoObject rhinoObject) + { + int layerIndex = rhinoObject.Attributes.LayerIndex; + return new Color4f(doc.Layers[layerIndex].Color); + } + public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) { From d580c7253965f76d2715632074cfc380b93d0c71 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 13:22:22 +0200 Subject: [PATCH 05/14] Move options.ExportMaterials outside of the GetMaterials function. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 00dd444..458f669 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -85,7 +85,8 @@ public Gltf ConvertToGltf() foreach (ObjectExportData exportData in sanitized) { - int[] materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); + int[] materialIndices = null; + if(options.ExportMaterials) materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); RhinoMeshGltfConverter meshConverter = new RhinoMeshGltfConverter(exportData, materialIndices, options, binary, dummy, binaryBuffer); int meshIndex = meshConverter.AddMesh(); From 4497214f807f9e7555bd1c88d7a18cdb7dce768b Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 13:38:30 +0200 Subject: [PATCH 06/14] Rewrite GetMaterials. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 71 ++++++++++++----------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 458f669..3f2fa65 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -54,6 +54,8 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do private Dictionary layerMaterialIndices = new Dictionary(); + private int defaultMaterialIndex = -1; + public Gltf ConvertToGltf() { dummy.Scene = 0; @@ -175,59 +177,62 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) Dictionary colorIndices = new Dictionary(); - for (int i = 0; i < materials.Length; i++) + for (int i = 0; i < subObjects.Length; i++) { var material = materials[i]; - if (!options.ExportMaterials) - { - return null; - } - - Guid materialId; - if (material == null && options.UseDisplayColorForUnsetMaterials) + if(material == null) { - if (options.ExportLayers) + if(options.UseDisplayColorForUnsetMaterials) { - if (subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) + if(subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { materialIndices[i] = GetLayerMaterial(rhinoObject); - continue; + } + else + { + Color4f objectColor = GetObjectColor(subObjects[i]); + if (!colorIndices.TryGetValue(objectColor, out int colorIndex)) + { + colorIndex = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject)); + colorIndices.Add(objectColor, colorIndex); + } + materialIndices[i] = colorIndex; } } - - Color4f objectColor = GetObjectColor(subObjects[i]); - if (!colorIndices.TryGetValue(objectColor, out int colorIndex)) + else { - colorIndex = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject)); - colorIndices.Add(objectColor, colorIndex); + materialIndices[i] = GetDefaultMaterial(); } - materialIndices[i] = colorIndex; - continue; - } - else if (material == null) - { - material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; - materialId = new Guid(); } else { - materialId = material.Id; - } - - if (!materialsMap.TryGetValue(materialId, out int materialIndex)) - { - RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); - materialIndex = materialConverter.AddMaterial(); - materialsMap.Add(materialId, materialIndex); + Guid materialId = material.Id; + if (!materialsMap.TryGetValue(materialId, out int materialIndex)) + { + RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); + materialIndex = materialConverter.AddMaterial(); + materialsMap.Add(materialId, materialIndex); + } + materialIndices[i] = materialIndex; } - - materialIndices[i] = materialIndex; } return materialIndices; } + private int GetDefaultMaterial() + { + if(defaultMaterialIndex == -1) + { + RenderMaterial material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; + material.Name = "DefaultMaterial"; + RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); + defaultMaterialIndex = materialConverter.AddMaterial(); + } + return defaultMaterialIndex; + } + private int GetLayerMaterial(RhinoObject rhinoObject) { if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex)) From 28b49e2a54bd1ca6b47c0142bea30e8ee34096b2 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 13:56:39 +0200 Subject: [PATCH 07/14] Add GetColorMaterial. Change Color4f to Color. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 33 ++++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 3f2fa65..c03119f 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -5,6 +5,7 @@ using Rhino.Render; using System; using System.Collections.Generic; +using System.Drawing; using System.Linq; namespace glTF_BinExporter @@ -175,7 +176,7 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) RhinoObject[] subObjects = rhinoObject.GetSubObjects(); int[] materialIndices = new int[materials.Length]; - Dictionary colorIndices = new Dictionary(); + Dictionary colorIndices = new Dictionary(); for (int i = 0; i < subObjects.Length; i++) { @@ -191,13 +192,7 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) } else { - Color4f objectColor = GetObjectColor(subObjects[i]); - if (!colorIndices.TryGetValue(objectColor, out int colorIndex)) - { - colorIndex = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject)); - colorIndices.Add(objectColor, colorIndex); - } - materialIndices[i] = colorIndex; + materialIndices[i] = GetColorMaterial(subObjects[i], colorIndices); } } else @@ -221,6 +216,18 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) return materialIndices; } + private int GetColorMaterial(RhinoObject rhinoObject, Dictionary colorMaterials) + { + Color objectColor = GetObjectColor(rhinoObject); + int colorIndex = objectColor.ToArgb(); + if (!colorMaterials.TryGetValue(colorIndex, out int colorMaterialIndex)) + { + colorMaterialIndex = CreateSolidColorMaterial(new Color4f(objectColor), GetObjectName(rhinoObject)); + colorMaterials.Add(colorIndex, colorIndex); + } + return colorMaterialIndex; + } + private int GetDefaultMaterial() { if(defaultMaterialIndex == -1) @@ -237,7 +244,7 @@ private int GetLayerMaterial(RhinoObject rhinoObject) { if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex)) { - Color4f objectColor = GetLayerColor(rhinoObject); + Color4f objectColor = new Color4f(GetLayerColor(rhinoObject)); layerMaterialIndex = CreateSolidColorMaterial(objectColor, doc.Layers[rhinoObject.Attributes.LayerIndex].Name); layerMaterialIndices.Add(rhinoObject.Attributes.LayerIndex, layerMaterialIndex); } @@ -258,7 +265,7 @@ int CreateSolidColorMaterial(Color4f color, string name) return dummy.Materials.AddAndReturnIndex(material); } - Color4f GetObjectColor(RhinoObject rhinoObject) + Color GetObjectColor(RhinoObject rhinoObject) { if (rhinoObject.Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { @@ -266,14 +273,14 @@ Color4f GetObjectColor(RhinoObject rhinoObject) } else { - return new Color4f(rhinoObject.Attributes.ObjectColor); + return rhinoObject.Attributes.ObjectColor; } } - Color4f GetLayerColor(RhinoObject rhinoObject) + Color GetLayerColor(RhinoObject rhinoObject) { int layerIndex = rhinoObject.Attributes.LayerIndex; - return new Color4f(doc.Layers[layerIndex].Color); + return doc.Layers[layerIndex].Color; } public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) From cf27ee360418e6c0cf25b21e635c7bb35cc2ac7c Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Mon, 23 Aug 2021 14:35:37 +0200 Subject: [PATCH 08/14] Share color materials of all objects. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index c03119f..dea1dd8 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -55,6 +55,8 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do private Dictionary layerMaterialIndices = new Dictionary(); + private Dictionary colorMaterials = new Dictionary(); + private int defaultMaterialIndex = -1; public Gltf ConvertToGltf() @@ -176,8 +178,6 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) RhinoObject[] subObjects = rhinoObject.GetSubObjects(); int[] materialIndices = new int[materials.Length]; - Dictionary colorIndices = new Dictionary(); - for (int i = 0; i < subObjects.Length; i++) { var material = materials[i]; @@ -192,7 +192,7 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) } else { - materialIndices[i] = GetColorMaterial(subObjects[i], colorIndices); + materialIndices[i] = GetColorMaterial(subObjects[i]); } } else @@ -216,18 +216,30 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) return materialIndices; } - private int GetColorMaterial(RhinoObject rhinoObject, Dictionary colorMaterials) + private int GetColorMaterial(RhinoObject rhinoObject) { Color objectColor = GetObjectColor(rhinoObject); int colorIndex = objectColor.ToArgb(); if (!colorMaterials.TryGetValue(colorIndex, out int colorMaterialIndex)) { - colorMaterialIndex = CreateSolidColorMaterial(new Color4f(objectColor), GetObjectName(rhinoObject)); + colorMaterialIndex = CreateSolidColorMaterial(new Color4f(objectColor), GetColorName(objectColor)); colorMaterials.Add(colorIndex, colorIndex); } return colorMaterialIndex; } + private string GetColorName(Color color) + { + if (color.IsNamedColor) + { + return color.Name; + } + else + { + return color.ToString(); + } + } + private int GetDefaultMaterial() { if(defaultMaterialIndex == -1) From 70ec7ef8c3fae22d977d7cc38e722d75470538ac Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Tue, 24 Aug 2021 12:10:17 +0200 Subject: [PATCH 09/14] Cleaner sub-object support. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 57 ++++++++++++++--------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index dea1dd8..600302b 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -91,7 +91,7 @@ public Gltf ConvertToGltf() foreach (ObjectExportData exportData in sanitized) { int[] materialIndices = null; - if(options.ExportMaterials) materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); + if (options.ExportMaterials) materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); RhinoMeshGltfConverter meshConverter = new RhinoMeshGltfConverter(exportData, materialIndices, options, binary, dummy, binaryBuffer); int meshIndex = meshConverter.AddMesh(); @@ -176,15 +176,16 @@ public byte[] GetBinaryBuffer() int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) { RhinoObject[] subObjects = rhinoObject.GetSubObjects(); + if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; int[] materialIndices = new int[materials.Length]; - for (int i = 0; i < subObjects.Length; i++) + for (int i = 0; i < materials.Length; i++) { var material = materials[i]; - if(material == null) + if (material == null) { - if(options.UseDisplayColorForUnsetMaterials) + if (options.UseDisplayColorForUnsetMaterials) { if(subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { @@ -223,7 +224,7 @@ private int GetColorMaterial(RhinoObject rhinoObject) if (!colorMaterials.TryGetValue(colorIndex, out int colorMaterialIndex)) { colorMaterialIndex = CreateSolidColorMaterial(new Color4f(objectColor), GetColorName(objectColor)); - colorMaterials.Add(colorIndex, colorIndex); + colorMaterials.Add(colorIndex, colorMaterialIndex); } return colorMaterialIndex; } @@ -242,7 +243,7 @@ private string GetColorName(Color color) private int GetDefaultMaterial() { - if(defaultMaterialIndex == -1) + if (defaultMaterialIndex == -1) { RenderMaterial material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; material.Name = "DefaultMaterial"; @@ -328,10 +329,26 @@ public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) // Need to get a Mesh from the None-mesh object. Using the FastRenderMesh here. Could be made configurable. // First make sure the internal rhino mesh has been created - //rhinoObject.CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); + rhinoObject.CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); // Then get the internal rhino meshes - Rhino.Geometry.Mesh[] meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); + //Rhino.Geometry.Mesh[] meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); + + var subObjects = rhinoObject.GetSubObjects(); + if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + Rhino.Geometry.Mesh[] meshes = new Rhino.Geometry.Mesh[subObjects.Length]; + + for (int i = 0; i < subObjects.Length; i++) + { + var subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + if(subMeshes.Length == 0) + { + subObjects[i].CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); + subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + + } + meshes[i] = subMeshes[0]; + } List validMeshes = new List(); @@ -400,23 +417,19 @@ public List SanitizeRhinoObjects(IEnumerable rhin // Need to get a Mesh from the None-mesh object. Using the FastRenderMesh here. Could be made configurable. // First make sure the internal rhino mesh has been created - rhinoObject.CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); + //rhinoObject.CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); - var mats = new RenderMaterial[rhinoObject.MeshCount(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh)]; - for (int i = 0; i < mats.Length; i++) - { - foreach (var component in rhinoObject.SubobjectMaterialComponents) - { - if (component.Index == i) - { - mats[i] = rhinoObject.GetRenderMaterial(component); - break; - } - } + var subObjects = rhinoObject.GetSubObjects(); + if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + var mats = new RenderMaterial[subObjects.Length]; - if (mats[i] == null) + var components = rhinoObject.SubobjectMaterialComponents; + for (int i = 0; i < subObjects.Length; i++) + { + mats[i] = rhinoObject.RenderMaterial; + foreach (var component in components) { - mats[i] = rhinoObject.RenderMaterial; + if (component.Index == i) mats[i] = rhinoObject.GetRenderMaterial(component); } } From 5b1e7fc56e00d3ec05e9f47f8e4dafa354c90310 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Sun, 29 Aug 2021 13:04:00 +0200 Subject: [PATCH 10/14] Add support for layer materials. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 39 +++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 600302b..98ae2a3 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -419,24 +419,11 @@ public List SanitizeRhinoObjects(IEnumerable rhin // First make sure the internal rhino mesh has been created //rhinoObject.CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); - var subObjects = rhinoObject.GetSubObjects(); - if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; - var mats = new RenderMaterial[subObjects.Length]; - - var components = rhinoObject.SubobjectMaterialComponents; - for (int i = 0; i < subObjects.Length; i++) - { - mats[i] = rhinoObject.RenderMaterial; - foreach (var component in components) - { - if (component.Index == i) mats[i] = rhinoObject.GetRenderMaterial(component); - } - } - var isValidGeometry = Constants.ValidObjectTypes.Contains(rhinoObject.ObjectType); if (isValidGeometry && rhinoObject.ObjectType != ObjectType.InstanceReference) { + var mats = GetRenderMaterials(rhinoObject); var meshes = GetMeshes(rhinoObject); if (meshes.Length > 0) //Objects need a mesh to export @@ -461,6 +448,7 @@ public List SanitizeRhinoObjects(IEnumerable rhin // Transform the exploded geo into its correct place foreach (var item in objects.Zip(transforms, (rObj, trans) => (rhinoObject: rObj, trans))) { + var mats = GetRenderMaterials(item.rhinoObject); var meshes = GetMeshes(item.rhinoObject); foreach (var mesh in meshes) @@ -488,6 +476,29 @@ public List SanitizeRhinoObjects(IEnumerable rhin return rhinoObjectsRes; } + private RenderMaterial[] GetRenderMaterials(RhinoObject rhinoObject) + { + var subObjects = rhinoObject.GetSubObjects(); + if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + var mats = new RenderMaterial[subObjects.Length]; + + var components = rhinoObject.SubobjectMaterialComponents; + for (int i = 0; i < subObjects.Length; i++) + { + mats[i] = rhinoObject.RenderMaterial; + foreach (var component in components) + { + if (component.Index == i) mats[i] = rhinoObject.GetRenderMaterial(component); + } + if (mats[i] == null) + { + mats[i] = doc.Layers[rhinoObject.Attributes.LayerIndex].RenderMaterial; + } + } + + return mats; + } + private void ExplodeRecursive(InstanceObject instanceObject, Rhino.Geometry.Transform instanceTransform, List pieces, List transforms) { for (int i = 0; i < instanceObject.InstanceDefinition.ObjectCount; i++) From 2b6ff8a2133b5a6194663f7fc58adbdf4ae1f490 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Sun, 29 Aug 2021 13:04:51 +0200 Subject: [PATCH 11/14] Add error checking in GetMeshes. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 98ae2a3..cfbf5d6 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -345,9 +345,9 @@ public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) { subObjects[i].CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); - } - meshes[i] = subMeshes[0]; + if (subMeshes.Length == 1) meshes[i] = subMeshes[0]; + else if (subMeshes.Length > 1) RhinoApp.WriteLine("This shouldn't have happened: An sub object created more than one mesh! RhinoDocGltfConverter.cs line:349"); } List validMeshes = new List(); From 29898f2b31d61e7836a86a4ea4b286e9454ed574 Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Sun, 29 Aug 2021 13:05:10 +0200 Subject: [PATCH 12/14] Fix support for InstanceObjects. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index cfbf5d6..0e27cfd 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -448,6 +448,14 @@ public List SanitizeRhinoObjects(IEnumerable rhin // Transform the exploded geo into its correct place foreach (var item in objects.Zip(transforms, (rObj, trans) => (rhinoObject: rObj, trans))) { + if (!doc.Layers[item.rhinoObject.Attributes.LayerIndex].IsVisible) continue; + + if (!item.rhinoObject.IsMeshable(Rhino.Geometry.MeshType.Any)) + { + RhinoApp.WriteLine("Skipping " + GetDebugName(item.rhinoObject) + ", object is not meshable. Object is a " + item.rhinoObject.ObjectType.ToString()); + continue; + } + var mats = GetRenderMaterials(item.rhinoObject); var meshes = GetMeshes(item.rhinoObject); From 137bd7440d65e966a74aa5eb082102f48c2deb67 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Wed, 1 Sep 2021 09:48:06 +0300 Subject: [PATCH 13/14] Fix style on doc converter --- glTF-BinExporter/RhinoDocGltfConverter.cs | 73 ++++++++++++++++------- 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 0e27cfd..20d184c 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -57,7 +57,7 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do private Dictionary colorMaterials = new Dictionary(); - private int defaultMaterialIndex = -1; + private int? defaultMaterialIndex = null; public Gltf ConvertToGltf() { @@ -91,7 +91,11 @@ public Gltf ConvertToGltf() foreach (ObjectExportData exportData in sanitized) { int[] materialIndices = null; - if (options.ExportMaterials) materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); + + if (options.ExportMaterials) + { + materialIndices = GetMaterials(exportData.RenderMaterials, exportData.Object); + } RhinoMeshGltfConverter meshConverter = new RhinoMeshGltfConverter(exportData, materialIndices, options, binary, dummy, binaryBuffer); int meshIndex = meshConverter.AddMesh(); @@ -165,7 +169,7 @@ private void AddToLayer(Layer layer, int child) public string GetObjectName(RhinoObject rhinoObject) { - return string.IsNullOrEmpty(rhinoObject.Name) ? null : rhinoObject.Name; + return string.IsNullOrEmpty(rhinoObject.Name) ? rhinoObject.Id.ToString() : rhinoObject.Name; } public byte[] GetBinaryBuffer() @@ -176,7 +180,12 @@ public byte[] GetBinaryBuffer() int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) { RhinoObject[] subObjects = rhinoObject.GetSubObjects(); - if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + + if (subObjects.Length == 0) + { + subObjects = new RhinoObject[1] { rhinoObject }; + } + int[] materialIndices = new int[materials.Length]; for (int i = 0; i < materials.Length; i++) @@ -220,12 +229,15 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) private int GetColorMaterial(RhinoObject rhinoObject) { Color objectColor = GetObjectColor(rhinoObject); + int colorIndex = objectColor.ToArgb(); + if (!colorMaterials.TryGetValue(colorIndex, out int colorMaterialIndex)) { colorMaterialIndex = CreateSolidColorMaterial(new Color4f(objectColor), GetColorName(objectColor)); colorMaterials.Add(colorIndex, colorMaterialIndex); } + return colorMaterialIndex; } @@ -243,14 +255,15 @@ private string GetColorName(Color color) private int GetDefaultMaterial() { - if (defaultMaterialIndex == -1) + if (defaultMaterialIndex == null) { RenderMaterial material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; material.Name = "DefaultMaterial"; RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); defaultMaterialIndex = materialConverter.AddMaterial(); } - return defaultMaterialIndex; + + return defaultMaterialIndex.Value; } private int GetLayerMaterial(RhinoObject rhinoObject) @@ -335,19 +348,32 @@ public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) //Rhino.Geometry.Mesh[] meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); var subObjects = rhinoObject.GetSubObjects(); - if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + + if (subObjects.Length == 0) + { + subObjects = new RhinoObject[1] { rhinoObject }; + } + Rhino.Geometry.Mesh[] meshes = new Rhino.Geometry.Mesh[subObjects.Length]; for (int i = 0; i < subObjects.Length; i++) { var subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + if(subMeshes.Length == 0) { subObjects[i].CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); } - if (subMeshes.Length == 1) meshes[i] = subMeshes[0]; - else if (subMeshes.Length > 1) RhinoApp.WriteLine("This shouldn't have happened: An sub object created more than one mesh! RhinoDocGltfConverter.cs line:349"); + + if (subMeshes.Length == 1) + { + meshes[i] = subMeshes[0]; + } + else if (subMeshes.Length > 1) + { + RhinoApp.WriteLine("This shouldn't have happened: An sub object created more than one mesh! RhinoDocGltfConverter.cs line:349"); + } } List validMeshes = new List(); @@ -393,16 +419,6 @@ public bool MeshIsValidForExport(Rhino.Geometry.Mesh mesh) return true; } - private string GetDebugName(RhinoObject rhinoObject) - { - if (string.IsNullOrEmpty(rhinoObject.Name)) - { - return "(Unnamed)"; - } - - return rhinoObject.Name; - } - public List SanitizeRhinoObjects(IEnumerable rhinoObjects) { var rhinoObjectsRes = new List(); @@ -411,7 +427,7 @@ public List SanitizeRhinoObjects(IEnumerable rhin { if (!rhinoObject.IsMeshable(Rhino.Geometry.MeshType.Any)) { - RhinoApp.WriteLine("Skipping " + GetDebugName(rhinoObject) + ", object is not meshable. Object is a " + rhinoObject.ObjectType.ToString()); + RhinoApp.WriteLine("Skipping " + GetObjectName(rhinoObject) + ", object is not meshable. Object is a " + rhinoObject.ObjectType.ToString()); continue; } @@ -452,7 +468,7 @@ public List SanitizeRhinoObjects(IEnumerable rhin if (!item.rhinoObject.IsMeshable(Rhino.Geometry.MeshType.Any)) { - RhinoApp.WriteLine("Skipping " + GetDebugName(item.rhinoObject) + ", object is not meshable. Object is a " + item.rhinoObject.ObjectType.ToString()); + RhinoApp.WriteLine("Skipping " + GetObjectName(item.rhinoObject) + ", object is not meshable. Object is a " + item.rhinoObject.ObjectType.ToString()); continue; } @@ -487,17 +503,28 @@ public List SanitizeRhinoObjects(IEnumerable rhin private RenderMaterial[] GetRenderMaterials(RhinoObject rhinoObject) { var subObjects = rhinoObject.GetSubObjects(); - if (subObjects.Length == 0) subObjects = new RhinoObject[1] { rhinoObject }; + + if (subObjects.Length == 0) + { + subObjects = new RhinoObject[1] { rhinoObject }; + } + var mats = new RenderMaterial[subObjects.Length]; var components = rhinoObject.SubobjectMaterialComponents; + for (int i = 0; i < subObjects.Length; i++) { mats[i] = rhinoObject.RenderMaterial; + foreach (var component in components) { - if (component.Index == i) mats[i] = rhinoObject.GetRenderMaterial(component); + if (component.Index == i) + { + mats[i] = rhinoObject.GetRenderMaterial(component); + } } + if (mats[i] == null) { mats[i] = doc.Layers[rhinoObject.Attributes.LayerIndex].RenderMaterial; From c694aed1f713a0c57248138c763e02f3e1a58f6b Mon Sep 17 00:00:00 2001 From: Peter Krattenmacher Date: Sat, 4 Sep 2021 10:21:20 +0200 Subject: [PATCH 14/14] Fix for extrusions. --- glTF-BinExporter/RhinoDocGltfConverter.cs | 52 ++++++++++++++--------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 20d184c..8a36e89 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -196,7 +196,7 @@ int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject) { if (options.UseDisplayColorForUnsetMaterials) { - if(subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) + if (subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer) { materialIndices[i] = GetLayerMaterial(rhinoObject); } @@ -347,32 +347,44 @@ public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) // Then get the internal rhino meshes //Rhino.Geometry.Mesh[] meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); - var subObjects = rhinoObject.GetSubObjects(); + Rhino.Geometry.Mesh[] meshes; - if (subObjects.Length == 0) + if (rhinoObject.ObjectType == ObjectType.Extrusion) { - subObjects = new RhinoObject[1] { rhinoObject }; + meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); } - - Rhino.Geometry.Mesh[] meshes = new Rhino.Geometry.Mesh[subObjects.Length]; - - for (int i = 0; i < subObjects.Length; i++) + else { - var subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); - - if(subMeshes.Length == 0) + var subObjects = rhinoObject.GetSubObjects(); + if (subObjects.Length == 0) { - subObjects[i].CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); - subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + meshes = rhinoObject.GetMeshes(Rhino.Geometry.MeshType.Preview); } - - if (subMeshes.Length == 1) - { - meshes[i] = subMeshes[0]; - } - else if (subMeshes.Length > 1) + else { - RhinoApp.WriteLine("This shouldn't have happened: An sub object created more than one mesh! RhinoDocGltfConverter.cs line:349"); + meshes = new Rhino.Geometry.Mesh[subObjects.Length]; + + for (int i = 0; i < subObjects.Length; i++) + { + var subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + if (subMeshes.Length == 1) + { + meshes[i] = subMeshes[0]; + } + else + { + if (subMeshes.Length == 0) + { + subObjects[i].CreateMeshes(Rhino.Geometry.MeshType.Preview, Rhino.Geometry.MeshingParameters.FastRenderMesh, true); + subMeshes = subObjects[i].GetMeshes(Rhino.Geometry.MeshType.Preview); + RhinoApp.WriteLine("This shouldn't have happened: An sub object had no mesh! RhinoDocGltfConverter.GetMeshes()"); + } + else if (subMeshes.Length > 1) + { + RhinoApp.WriteLine("This shouldn't have happened: An sub object created more than one mesh! RhinoDocGltfConverter.cs.GetMeshes()"); + } + } + } } }