From 13021a9e6a843470cdf1affd60641337f6945199 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Wed, 7 Apr 2021 11:11:33 +0300 Subject: [PATCH 1/7] Removed unused color utils --- glTF-BinExporter/ColorUtils.cs | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 glTF-BinExporter/ColorUtils.cs diff --git a/glTF-BinExporter/ColorUtils.cs b/glTF-BinExporter/ColorUtils.cs deleted file mode 100644 index 542ca0a..0000000 --- a/glTF-BinExporter/ColorUtils.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Drawing; -using Rhino.Display; - -namespace glTF_BinExporter -{ - public static class ColorUtils - { - /// - /// Convert from sRGB to Linear. - /// - /// - /// - public static Color4f ConvertSRGBToLinear(Color color) - { - return ConvertSRGBToLinear(new Color4f(color)); - } - - public static Color4f ConvertSRGBToLinear(Color4f color) - { - return Color4f.ApplyGamma(color, 2.4f); - } - } -} From 3440c3e3e9480842ff19cd8f5e4fbe86af8036e0 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Wed, 7 Apr 2021 12:48:08 +0300 Subject: [PATCH 2/7] Added tabs and title to export dialog --- glTF-BinExporter/glTFExportOptionsDialog.cs | 63 ++++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/glTF-BinExporter/glTFExportOptionsDialog.cs b/glTF-BinExporter/glTFExportOptionsDialog.cs index 4e35025..73933d3 100644 --- a/glTF-BinExporter/glTFExportOptionsDialog.cs +++ b/glTF-BinExporter/glTFExportOptionsDialog.cs @@ -9,6 +9,9 @@ namespace glTF_BinExporter { class ExportOptionsDialog : Dialog { + private const int DefaultPadding = 5; + private static readonly Eto.Drawing.Size DefaultSpacing = new Eto.Drawing.Size(2, 2); + private CheckBox useDracoCompressionCheck = new CheckBox(); private Label dracoCompressionLabel = new Label(); @@ -29,6 +32,9 @@ class ExportOptionsDialog : Dialog public ExportOptionsDialog(glTFExportOptions options) { this.options = options; + Resizable = false; + + Title = "glTF Export Options"; useDracoCompressionCheck.Text = "Use Draco Compression"; @@ -61,8 +67,8 @@ public ExportOptionsDialog(glTFExportOptions options) var gBox = new GroupBox() { Text = "Draco Quantization Bits" }; gBox.Content = new TableLayout() { - Padding = 5, - Spacing = new Eto.Drawing.Size(2, 2), + Padding = DefaultPadding, + Spacing = DefaultSpacing, Rows = { new TableRow("Position", "Normal", "Texture"), new TableRow(dracoQuantizationBitsInputPosition, dracoQuantizationBitsInputNormal, dracoQuantizationBitsInputTexture) @@ -71,15 +77,58 @@ public ExportOptionsDialog(glTFExportOptions options) var layout = new DynamicLayout() { - Padding = 5, - Spacing = new Eto.Drawing.Size(2, 2) + Padding = DefaultPadding, + Spacing = DefaultSpacing, }; - layout.AddRow(mapZtoY, null); + layout.AddSeparateRow(useDracoCompressionCheck, null); layout.AddSeparateRow(dracoCompressionLabel, dracoCompressionLevelInput, null); layout.AddSeparateRow(gBox, null); - layout.AddSeparateRow(null, cancelButton, okButton); - this.Content = layout; + + TabControl tabControl = new TabControl(); + + TabPage formattingPage = new TabPage() + { + Text = "Formatting", + Content = new TableLayout() + { + Padding = DefaultPadding, + Spacing = DefaultSpacing, + Rows = + { + new TableRow(mapZtoY), + }, + }, + }; + + TabPage compressionPage = new TabPage() + { + Text = "Compression", + Content = layout, + }; + + tabControl.Pages.Add(formattingPage); + tabControl.Pages.Add(compressionPage); + + this.Content = new TableLayout() + { + Padding = DefaultPadding, + Spacing = DefaultSpacing, + Rows = + { + new TableRow(tabControl), + null, + new TableRow(new TableLayout() + { + Padding = DefaultPadding, + Spacing = DefaultSpacing, + Rows = + { + new TableRow(cancelButton, okButton), + } + }), + } + }; } private void MapZtoY_CheckedChanged(object sender, EventArgs e) From 713702a920adf3a595f83e27fe7baca22937f6a0 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Thu, 8 Apr 2021 11:38:42 +0300 Subject: [PATCH 3/7] Save export options in plugin settings --- glTF-BinExporter/GlTFExporterCommand.cs | 90 +++++++---- glTF-BinExporter/RhinoDocGltfConverter.cs | 16 +- .../RhinoMaterialGltfConverter.cs | 8 +- glTF-BinExporter/glTF-BinExporterPlugin.cs | 73 --------- glTF-BinExporter/glTFBinExporterPlugin.cs | 143 ++++++++++++++++++ glTF-BinExporter/glTFExportOptions.cs | 13 +- glTF-BinExporter/glTFExportOptionsDialog.cs | 83 ++++------ 7 files changed, 253 insertions(+), 173 deletions(-) delete mode 100644 glTF-BinExporter/glTF-BinExporterPlugin.cs create mode 100644 glTF-BinExporter/glTFBinExporterPlugin.cs diff --git a/glTF-BinExporter/GlTFExporterCommand.cs b/glTF-BinExporter/GlTFExporterCommand.cs index 8daa789..5c8e763 100644 --- a/glTF-BinExporter/GlTFExporterCommand.cs +++ b/glTF-BinExporter/GlTFExporterCommand.cs @@ -38,46 +38,74 @@ protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode) return Result.Cancel; } - bool binary = GlTFUtils.IsFileGltfBinary(dialog.FileName); + bool binary = GlTFUtils.IsFileGltfBinary(dialog.FileName); + + if(!GetExportOptions(mode, out glTFExportOptions opts)) + { + return Result.Cancel; + } + + Rhino.DocObjects.RhinoObject[] rhinoObjects = go.Objects().Select(o => o.Object()).ToArray(); + + if(!DoExport(dialog.FileName, opts, binary, rhinoObjects, doc.RenderSettings.LinearWorkflow)) + { + return Result.Failure; + } - var opts = new glTFExportOptions() { UseDracoCompression = false, DracoCompressionLevel = 10, DracoQuantizationBitsPosition = 11, DracoQuantizationBitsNormal = 8, DracoQuantizationBitsTexture = 10, UseBinary = binary }; + return Result.Success; + } - if (mode == RunMode.Scripted) + private bool GetExportOptions(RunMode mode, out glTFExportOptions options) + { + if(mode == RunMode.Scripted) { + options = new glTFExportOptions(); - Rhino.Input.RhinoGet.GetBool("Compression", true, "None", "Draco", ref opts.UseDracoCompression); - - if (opts.UseDracoCompression) + if(Rhino.Input.RhinoGet.GetBool("Compression", true, "None", "Draco", ref options.UseDracoCompression) != Result.Success) { - Rhino.Input.RhinoGet.GetInteger("Draco Compression Level (max=10)", true, ref opts.DracoCompressionLevel, 1, 10); - Rhino.Input.RhinoGet.GetInteger("Quantization Position", true, ref opts.DracoQuantizationBitsPosition, 8, 32); - Rhino.Input.RhinoGet.GetInteger("Quantization Normal", true, ref opts.DracoQuantizationBitsNormal, 8, 32); - Rhino.Input.RhinoGet.GetInteger("Quantization Texture", true, ref opts.DracoQuantizationBitsTexture, 8, 32); + return false; } - Rhino.Input.RhinoGet.GetBool("Map Rhino Z to glTF Y", true, "No", "Yes", ref opts.MapRhinoZToGltfY); - } - else - { - ExportOptionsDialog optionsDlg = new ExportOptionsDialog(opts); + if (options.UseDracoCompression) + { + if(Rhino.Input.RhinoGet.GetInteger("Draco Compression Level (max=10)", true, ref options.DracoCompressionLevel, 1, 10) != Result.Success) + { + return false; + } - if (optionsDlg.ShowModal() == null) + if(Rhino.Input.RhinoGet.GetInteger("Quantization Position", true, ref options.DracoQuantizationBitsPosition, 8, 32) != Result.Success) + { + return false; + } + + if(Rhino.Input.RhinoGet.GetInteger("Quantization Normal", true, ref options.DracoQuantizationBitsNormal, 8, 32) != Result.Success) + { + return false; + } + + if(Rhino.Input.RhinoGet.GetInteger("Quantization Texture", true, ref options.DracoQuantizationBitsTexture, 8, 32) != Result.Success) + { + return false; + } + } + + if(Rhino.Input.RhinoGet.GetBool("Map Rhino Z to glTF Y", true, "No", "Yes", ref options.MapRhinoZToGltfY) != Result.Success) { - return Result.Cancel; + return false; } - } - var rhinoObjects = go - .Objects() - .Select(o => o.Object()) - .ToArray(); - - if(!DoExport(dialog.FileName, opts, rhinoObjects, doc.RenderSettings.LinearWorkflow)) + return true; + } + else { - return Result.Failure; - } - - return Result.Success; + ExportOptionsDialog optionsDlg = new ExportOptionsDialog(); + + Eto.Forms.DialogResult result = optionsDlg.ShowModal(); + + options = glTFBinExporterPlugin.GetSavedOptions(); + + return result == Eto.Forms.DialogResult.Ok; + } } private SaveFileDialog GetSaveFileDialog() @@ -90,14 +118,14 @@ private SaveFileDialog GetSaveFileDialog() }; } - public static bool DoExport(string fileName, glTFExportOptions opts, IEnumerable rhinoObjects, Rhino.Render.LinearWorkflow workflow) + public static bool DoExport(string fileName, glTFExportOptions options, bool binary, IEnumerable rhinoObjects, Rhino.Render.LinearWorkflow workflow) { try { - RhinoDocGltfConverter converter = new RhinoDocGltfConverter(opts, rhinoObjects, workflow); + RhinoDocGltfConverter converter = new RhinoDocGltfConverter(options, binary, rhinoObjects, workflow); glTFLoader.Schema.Gltf gltf = converter.ConvertToGltf(); - if(opts.UseBinary) + if(binary) { byte[] bytes = converter.GetBinaryBuffer(); glTFLoader.Interface.SaveBinaryModel(gltf, bytes.Length == 0 ? null : bytes, fileName); diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index fd706dc..261a273 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -17,21 +17,25 @@ namespace glTF_BinExporter { class RhinoDocGltfConverter { - public RhinoDocGltfConverter(glTFExportOptions options, IEnumerable objects, LinearWorkflow workflow) + public RhinoDocGltfConverter(glTFExportOptions options, bool binary, IEnumerable objects, LinearWorkflow workflow) { this.options = options; + this.binary = binary; this.objects = objects; this.workflow = workflow; } - public RhinoDocGltfConverter(glTFExportOptions options, RhinoDoc doc, LinearWorkflow workflow) + public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc doc, LinearWorkflow workflow) { this.options = options; + this.binary = binary; this.objects = doc.Objects; this.workflow = null; } private IEnumerable objects = null; + + private bool binary = false; private glTFExportOptions options = null; private LinearWorkflow workflow = null; @@ -98,7 +102,7 @@ public Gltf ConvertToGltf() } else { - if (options.UseBinary) + if (binary) { AddRhinoObjectBinary(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); } @@ -109,7 +113,7 @@ public Gltf ConvertToGltf() } } - if(options.UseBinary && 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() @@ -367,7 +371,7 @@ private byte[] GetDracoBytes(string fileName) public void WriteDracoBytes(byte[] bytes, out int bufferIndex, out int byteOffset) { - if(options.UseBinary) + if(binary) { byteOffset = (int)binaryBuffer.Count; binaryBuffer.AddRange(bytes); @@ -869,7 +873,7 @@ int GetMaterial(Rhino.DocObjects.Material material, Guid materialId) { if(!materialsMap.TryGetValue(materialId, out int materialIndex)) { - RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, dummy, binaryBuffer, material, workflow); + RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); materialIndex = materialConverter.AddMaterial(); materialsMap.Add(materialId, materialIndex); } diff --git a/glTF-BinExporter/RhinoMaterialGltfConverter.cs b/glTF-BinExporter/RhinoMaterialGltfConverter.cs index 95c5f86..9534f29 100644 --- a/glTF-BinExporter/RhinoMaterialGltfConverter.cs +++ b/glTF-BinExporter/RhinoMaterialGltfConverter.cs @@ -15,9 +15,10 @@ namespace glTF_BinExporter { class RhinoMaterialGltfConverter { - public RhinoMaterialGltfConverter(glTFExportOptions options, gltfSchemaDummy dummy, List binaryBuffer, Rhino.DocObjects.Material rhinoMaterial, LinearWorkflow workflow) + public RhinoMaterialGltfConverter(glTFExportOptions options, bool binary, gltfSchemaDummy dummy, List binaryBuffer, Rhino.DocObjects.Material rhinoMaterial, LinearWorkflow workflow) { this.options = options; + this.binary = binary; this.dummy = dummy; this.binaryBuffer = binaryBuffer; this.rhinoMaterial = rhinoMaterial; @@ -25,6 +26,7 @@ public RhinoMaterialGltfConverter(glTFExportOptions options, gltfSchemaDummy dum } private glTFExportOptions options = null; + private bool binary = false; private gltfSchemaDummy dummy = null; private List binaryBuffer = null; private LinearWorkflow workflow = null; @@ -312,7 +314,7 @@ private glTFLoader.Schema.Image GetImageFromFileText(string fileName) private glTFLoader.Schema.Image GetImageFromFile(string fileName) { - if (options.UseBinary) + if (binary) { return GetImageFromFileBinary(fileName); } @@ -580,7 +582,7 @@ private glTFLoader.Schema.TextureInfo GetTextureInfoFromBitmap(Bitmap bitmap) private glTFLoader.Schema.Image GetImageFromBitmap(Bitmap bitmap) { - if (options.UseBinary) + if (binary) { return GetImageFromBitmapBinary(bitmap); } diff --git a/glTF-BinExporter/glTF-BinExporterPlugin.cs b/glTF-BinExporter/glTF-BinExporterPlugin.cs deleted file mode 100644 index c28de27..0000000 --- a/glTF-BinExporter/glTF-BinExporterPlugin.cs +++ /dev/null @@ -1,73 +0,0 @@ -using Rhino; -using Rhino.FileIO; -using Rhino.PlugIns; -using System.Collections.Generic; -using System.IO; - -namespace glTF_BinExporter -{ - /// - /// Every RhinoCommon .rhp assembly must have one and only one PlugIn-derived - /// class. DO NOT create instances of this class yourself. It is the - /// responsibility of Rhino to create an instance of this class. - /// To complete plug-in information, please also see all PlugInDescription - /// attributes in AssemblyInfo.cs (you might need to click "Project" -> - /// "Show All Files" to see it in the "Solution Explorer" window). - /// - public class glTF_BinExporterPlugin : Rhino.PlugIns.FileExportPlugIn - { - ///Gets the only instance of the glTF_BinExporterPlugin plug-in. - public static glTF_BinExporterPlugin Instance { get; private set; } - - public glTF_BinExporterPlugin() - { - Instance = this; - } - - protected override FileTypeList AddFileTypes(FileWriteOptions options) - { - FileTypeList typeList = new FileTypeList(); - - typeList.AddFileType("glTF Binary File", ".glb"); - typeList.AddFileType("glTF Text File", ".gltf"); - - return typeList; - } - - protected override WriteFileResult WriteFile(string filename, int index, RhinoDoc doc, FileWriteOptions options) - { - bool binary = GlTFUtils.IsFileGltfBinary(filename); - - glTFExportOptions gltfOptions = new glTFExportOptions(); - gltfOptions.UseBinary = binary; - - ExportOptionsDialog optionsDlg = new ExportOptionsDialog(gltfOptions); - - if(optionsDlg.ShowModal() == null) - { - return WriteFileResult.Cancel; - } - - IEnumerable objects = GetObjectsToExport(doc, options); - - if(!GlTFExporterCommand.DoExport(filename, gltfOptions, objects, doc.RenderSettings.LinearWorkflow)) - { - return WriteFileResult.Failure; - } - - return WriteFileResult.Success; - } - - private IEnumerable GetObjectsToExport(RhinoDoc doc, FileWriteOptions options) - { - if(options.WriteSelectedObjectsOnly) - { - return doc.Objects.GetSelectedObjects(false, false); - } - else - { - return doc.Objects; - } - } - } -} diff --git a/glTF-BinExporter/glTFBinExporterPlugin.cs b/glTF-BinExporter/glTFBinExporterPlugin.cs new file mode 100644 index 0000000..628dca6 --- /dev/null +++ b/glTF-BinExporter/glTFBinExporterPlugin.cs @@ -0,0 +1,143 @@ +using Rhino; +using Rhino.FileIO; +using Rhino.PlugIns; +using System; +using System.Collections.Generic; +using System.IO; + +namespace glTF_BinExporter +{ + public class glTFBinExporterPlugin : Rhino.PlugIns.FileExportPlugIn + { + public static glTFBinExporterPlugin Instance { get; private set; } + + public glTFBinExporterPlugin() + { + Instance = this; + } + + protected override FileTypeList AddFileTypes(FileWriteOptions options) + { + FileTypeList typeList = new FileTypeList(); + + typeList.AddFileType("glTF Binary File", ".glb", true); + typeList.AddFileType("glTF Text File", ".gltf", true); + + return typeList; + } + + protected override WriteFileResult WriteFile(string filename, int index, RhinoDoc doc, FileWriteOptions options) + { + bool binary = GlTFUtils.IsFileGltfBinary(filename); + + ExportOptionsDialog optionsDlg = new ExportOptionsDialog(); + + if(optionsDlg.ShowModal() != Eto.Forms.DialogResult.Ok) + { + return WriteFileResult.Cancel; + } + + glTFExportOptions exportOptions = glTFBinExporterPlugin.GetSavedOptions(); + + IEnumerable objects = GetObjectsToExport(doc, options); + + if(!GlTFExporterCommand.DoExport(filename, exportOptions, binary, objects, doc.RenderSettings.LinearWorkflow)) + { + return WriteFileResult.Failure; + } + + return WriteFileResult.Success; + } + + private IEnumerable GetObjectsToExport(RhinoDoc doc, FileWriteOptions options) + { + if(options.WriteSelectedObjectsOnly) + { + return doc.Objects.GetSelectedObjects(false, false); + } + else + { + return doc.Objects; + } + } + + protected override void DisplayOptionsDialog(IntPtr parent, string description, string extension) + { + ExportOptionsDialog exportOptionsDialog = new ExportOptionsDialog(); + + exportOptionsDialog.ShowModal(); + } + + #region Settings + + private const string UseDracoCompressionKey = "UseDracoCompression"; + public const bool UseDracoCompressionDefault = false; + + public static bool UseDracoCompression + { + get => Instance.Settings.GetBool(UseDracoCompressionKey, UseDracoCompressionDefault); + set => Instance.Settings.SetBool(UseDracoCompressionKey, value); + } + + private const string MapRhinoZToGltfYKey = "MapZYpToYUp"; + public const bool MapRhinoZToGltfYDefault = true; + + public static bool MapRhinoZToGltfY + { + get => Instance.Settings.GetBool(MapRhinoZToGltfYKey, MapRhinoZToGltfYDefault); + set => Instance.Settings.SetBool(MapRhinoZToGltfYKey, value); + } + + private const string DracoCompressionLevelKey = "DracoCompressionLevel"; + public const int DracoCompressionLevelDefault = 10; + + public static int DracoCompressionLevel + { + get => Instance.Settings.GetInteger(DracoCompressionLevelKey, DracoCompressionLevelDefault); + set => Instance.Settings.SetInteger(DracoCompressionLevelKey, value); + } + + private const string DracoQuantizationBitsPositionKey = "DracoQuantizationBitsPosition"; + public const int DracoQuantizationBitsPositionDefault = 11; + + public static int DracoQuantizationBitsPosition + { + get => Instance.Settings.GetInteger(DracoQuantizationBitsPositionKey, DracoQuantizationBitsPositionDefault); + set => Instance.Settings.SetInteger(DracoQuantizationBitsPositionKey, value); + } + + private const string DracoQuantizationBitsNormalKey = "DracoQuantizationBitsNormal"; + public const int DracoQuantizationBitsNormalDefault = 8; + + public static int DracoQuantizationBitsNormal + { + get => Instance.Settings.GetInteger(DracoQuantizationBitsNormalKey, DracoQuantizationBitsNormalDefault); + set => Instance.Settings.SetInteger(DracoQuantizationBitsNormalKey, value); + } + + private const string DracoQuantizationBitsTextureKey = "DracoQuantizationBitsTextureKey"; + public const int DracoQuantizationBitsTextureDefault = 10; + + public static int DracoQuantizationBitsTexture + { + get => Instance.Settings.GetInteger(DracoQuantizationBitsTextureKey, DracoQuantizationBitsTextureDefault); + set => Instance.Settings.SetInteger(DracoQuantizationBitsTextureKey, value); + } + + public static glTFExportOptions GetSavedOptions() + { + return new glTFExportOptions() + { + MapRhinoZToGltfY = MapRhinoZToGltfY, + UseDracoCompression = UseDracoCompression, + DracoCompressionLevel = DracoCompressionLevel, + DracoQuantizationBitsPosition = DracoQuantizationBitsPosition, + DracoQuantizationBitsNormal = DracoQuantizationBitsNormal, + DracoQuantizationBitsTexture = DracoQuantizationBitsTexture, + }; + } + + #endregion + + } +} diff --git a/glTF-BinExporter/glTFExportOptions.cs b/glTF-BinExporter/glTFExportOptions.cs index 8825a78..06ba84b 100644 --- a/glTF-BinExporter/glTFExportOptions.cs +++ b/glTF-BinExporter/glTFExportOptions.cs @@ -8,12 +8,11 @@ namespace glTF_BinExporter { public class glTFExportOptions { - public bool UseDracoCompression = false; - public bool UseBinary = true; - public bool MapRhinoZToGltfY = true; - public int DracoCompressionLevel = 10; - public int DracoQuantizationBitsPosition = 11; - public int DracoQuantizationBitsNormal = 8; - public int DracoQuantizationBitsTexture = 10; + public bool MapRhinoZToGltfY = glTFBinExporterPlugin.MapRhinoZToGltfYDefault; + public bool UseDracoCompression = glTFBinExporterPlugin.UseDracoCompressionDefault; + public int DracoCompressionLevel = glTFBinExporterPlugin.DracoCompressionLevelDefault; + public int DracoQuantizationBitsPosition = glTFBinExporterPlugin.DracoQuantizationBitsPositionDefault; + public int DracoQuantizationBitsNormal = glTFBinExporterPlugin.DracoQuantizationBitsNormalDefault; + public int DracoQuantizationBitsTexture = glTFBinExporterPlugin.DracoQuantizationBitsTextureDefault; } } diff --git a/glTF-BinExporter/glTFExportOptionsDialog.cs b/glTF-BinExporter/glTFExportOptionsDialog.cs index 73933d3..b8218fa 100644 --- a/glTF-BinExporter/glTFExportOptionsDialog.cs +++ b/glTF-BinExporter/glTFExportOptionsDialog.cs @@ -7,7 +7,7 @@ namespace glTF_BinExporter { - class ExportOptionsDialog : Dialog + class ExportOptionsDialog : Dialog { private const int DefaultPadding = 5; private static readonly Eto.Drawing.Size DefaultSpacing = new Eto.Drawing.Size(2, 2); @@ -27,11 +27,8 @@ class ExportOptionsDialog : Dialog private CheckBox mapZtoY = new CheckBox(); - private glTFExportOptions options = null; - - public ExportOptionsDialog(glTFExportOptions options) + public ExportOptionsDialog() { - this.options = options; Resizable = false; Title = "glTF Export Options"; @@ -54,12 +51,6 @@ public ExportOptionsDialog(glTFExportOptions options) OptionsToDialog(); useDracoCompressionCheck.CheckedChanged += UseDracoCompressionCheck_CheckedChanged; - dracoCompressionLevelInput.ValueChanged += DracoCompressionLevelInput_ValueChanged; - dracoQuantizationBitsInputPosition.ValueChanged += DracoQuantizationBitsInputPosition_ValueChanged; - dracoQuantizationBitsInputNormal.ValueChanged += DracoQuantizationBitsInputNormal_ValueChanged; - dracoQuantizationBitsInputTexture.ValueChanged += DracoQuantizationBitsInputTexture_ValueChanged; - - mapZtoY.CheckedChanged += MapZtoY_CheckedChanged; cancelButton.Click += CancelButton_Click; okButton.Click += OkButton_Click; @@ -131,80 +122,66 @@ public ExportOptionsDialog(glTFExportOptions options) }; } - private void MapZtoY_CheckedChanged(object sender, EventArgs e) - { - options.MapRhinoZToGltfY = mapZtoY.Checked.HasValue ? mapZtoY.Checked.Value : false; - } - private void OptionsToDialog() { - useDracoCompressionCheck.Checked = options.UseDracoCompression; + useDracoCompressionCheck.Checked = glTFBinExporterPlugin.UseDracoCompression; - EnableDisableDracoControls(options.UseDracoCompression); + EnableDisableDracoControls(glTFBinExporterPlugin.UseDracoCompression); - dracoCompressionLevelInput.Value = options.DracoCompressionLevel; + dracoCompressionLevelInput.Value = glTFBinExporterPlugin.DracoCompressionLevel; - mapZtoY.Checked = options.MapRhinoZToGltfY; + mapZtoY.Checked = glTFBinExporterPlugin.MapRhinoZToGltfY; - dracoQuantizationBitsInputPosition.Value = options.DracoQuantizationBitsPosition; - dracoQuantizationBitsInputNormal.Value = options.DracoQuantizationBitsNormal; - dracoQuantizationBitsInputTexture.Value = options.DracoQuantizationBitsTexture; + dracoQuantizationBitsInputPosition.Value = glTFBinExporterPlugin.DracoQuantizationBitsPosition; + dracoQuantizationBitsInputNormal.Value = glTFBinExporterPlugin.DracoQuantizationBitsNormal; + dracoQuantizationBitsInputTexture.Value = glTFBinExporterPlugin.DracoQuantizationBitsTexture; } - private void EnableDisableDracoControls(bool enable) + private void DialogToOptions() { - dracoCompressionLevelInput.Enabled = enable; - dracoQuantizationBitsInputPosition.Enabled = enable; - dracoQuantizationBitsInputNormal.Enabled = enable; - dracoQuantizationBitsInputTexture.Enabled = enable; - } + glTFBinExporterPlugin.UseDracoCompression = GetCheckboxValue(useDracoCompressionCheck); - private void UseDracoCompressionCheck_CheckedChanged(object sender, EventArgs e) - { - bool useDraco = useDracoCompressionCheck.Checked.HasValue ? useDracoCompressionCheck.Checked.Value : false; + glTFBinExporterPlugin.DracoCompressionLevel = (int)dracoCompressionLevelInput.Value; - options.UseDracoCompression = useDraco; + glTFBinExporterPlugin.MapRhinoZToGltfY = GetCheckboxValue(mapZtoY); - EnableDisableDracoControls(useDraco); - } + glTFBinExporterPlugin.DracoQuantizationBitsPosition = (int)dracoQuantizationBitsInputPosition.Value; - private void DracoCompressionLevelInput_ValueChanged(object sender, EventArgs e) - { - int level = (int)dracoCompressionLevelInput.Value; + glTFBinExporterPlugin.DracoQuantizationBitsNormal = (int)dracoQuantizationBitsInputNormal.Value; - options.DracoCompressionLevel = level; + glTFBinExporterPlugin.DracoQuantizationBitsTexture = (int)dracoQuantizationBitsInputTexture.Value; } - private void DracoQuantizationBitsInputPosition_ValueChanged(object sender, EventArgs e) + private bool GetCheckboxValue(CheckBox checkBox) { - int bits = (int)dracoQuantizationBitsInputPosition.Value; - - options.DracoQuantizationBitsPosition = bits; + return checkBox.Checked.HasValue ? checkBox.Checked.Value : false; } - private void DracoQuantizationBitsInputNormal_ValueChanged(object sender, EventArgs e) + private void EnableDisableDracoControls(bool enable) { - int bits = (int)dracoQuantizationBitsInputNormal.Value; - - options.DracoQuantizationBitsNormal = bits; + dracoCompressionLevelInput.Enabled = enable; + dracoQuantizationBitsInputPosition.Enabled = enable; + dracoQuantizationBitsInputNormal.Enabled = enable; + dracoQuantizationBitsInputTexture.Enabled = enable; } - private void DracoQuantizationBitsInputTexture_ValueChanged(object sender, EventArgs e) + private void UseDracoCompressionCheck_CheckedChanged(object sender, EventArgs e) { - int bits = (int)dracoQuantizationBitsInputTexture.Value; + bool useDraco = GetCheckboxValue(useDracoCompressionCheck); - options.DracoQuantizationBitsTexture = bits; + EnableDisableDracoControls(useDraco); } private void CancelButton_Click(object sender, EventArgs e) { - this.Close(null); + this.Close(DialogResult.Cancel); } private void OkButton_Click(object sender, EventArgs e) { - this.Close(options); - } + DialogToOptions(); + this.Close(DialogResult.Ok); + } } } From efdbffce7be43845cd4a1de21e27ea5b75d17675 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Thu, 8 Apr 2021 12:26:31 +0300 Subject: [PATCH 4/7] Added option to not export materials --- glTF-BinExporter/RhinoDocGltfConverter.cs | 13 ++++-- glTF-BinExporter/glTFBinExporterPlugin.cs | 46 +++++++++++++-------- glTF-BinExporter/glTFExportOptions.cs | 1 + glTF-BinExporter/glTFExportOptionsDialog.cs | 12 +++--- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 261a273..8f524f1 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -133,7 +133,7 @@ public byte[] GetBinaryBuffer() private void AddRhinoObjectDraco(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) { - int materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(material, materialId); var primitives = new List(); @@ -391,7 +391,7 @@ public void WriteDracoBytes(byte[] bytes, out int bufferIndex, out int byteOffse private void AddRhinoObjectBinary(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) { - int materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(material, materialId); var primitives = new List(); @@ -558,7 +558,7 @@ private void AddRhinoObjectBinary(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocOb private void AddRhinoObjectText(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) { - int materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(material, materialId); var primitives = new List(); @@ -869,8 +869,13 @@ private byte[] GetTextureCoordinatesBytes(Rhino.Geometry.Collections.MeshTexture return bytesEnumerable.ToArray(); } - int GetMaterial(Rhino.DocObjects.Material material, Guid materialId) + int? GetMaterial(Rhino.DocObjects.Material material, Guid materialId) { + if(!options.ExportMaterials) + { + return null; + } + if(!materialsMap.TryGetValue(materialId, out int materialIndex)) { RhinoMaterialGltfConverter materialConverter = new RhinoMaterialGltfConverter(options, binary, dummy, binaryBuffer, material, workflow); diff --git a/glTF-BinExporter/glTFBinExporterPlugin.cs b/glTF-BinExporter/glTFBinExporterPlugin.cs index 628dca6..d6f61de 100644 --- a/glTF-BinExporter/glTFBinExporterPlugin.cs +++ b/glTF-BinExporter/glTFBinExporterPlugin.cs @@ -70,58 +70,67 @@ protected override void DisplayOptionsDialog(IntPtr parent, string description, #region Settings - private const string UseDracoCompressionKey = "UseDracoCompression"; + private const string useDracoCompressionKey = "UseDracoCompression"; public const bool UseDracoCompressionDefault = false; public static bool UseDracoCompression { - get => Instance.Settings.GetBool(UseDracoCompressionKey, UseDracoCompressionDefault); - set => Instance.Settings.SetBool(UseDracoCompressionKey, value); + get => Instance.Settings.GetBool(useDracoCompressionKey, UseDracoCompressionDefault); + set => Instance.Settings.SetBool(useDracoCompressionKey, value); } - private const string MapRhinoZToGltfYKey = "MapZYpToYUp"; + private const string mapRhinoZToGltfYKey = "MapZYpToYUp"; public const bool MapRhinoZToGltfYDefault = true; public static bool MapRhinoZToGltfY { - get => Instance.Settings.GetBool(MapRhinoZToGltfYKey, MapRhinoZToGltfYDefault); - set => Instance.Settings.SetBool(MapRhinoZToGltfYKey, value); + get => Instance.Settings.GetBool(mapRhinoZToGltfYKey, MapRhinoZToGltfYDefault); + set => Instance.Settings.SetBool(mapRhinoZToGltfYKey, value); } - private const string DracoCompressionLevelKey = "DracoCompressionLevel"; + private const string exportMaterialsKey = "ExportMaterials"; + public const bool ExportMaterialsDefault = true; + + public static bool ExportMaterials + { + get => Instance.Settings.GetBool(exportMaterialsKey, ExportMaterialsDefault); + set => Instance.Settings.SetBool(exportMaterialsKey, value); + } + + private const string dracoCompressionLevelKey = "DracoCompressionLevel"; public const int DracoCompressionLevelDefault = 10; public static int DracoCompressionLevel { - get => Instance.Settings.GetInteger(DracoCompressionLevelKey, DracoCompressionLevelDefault); - set => Instance.Settings.SetInteger(DracoCompressionLevelKey, value); + get => Instance.Settings.GetInteger(dracoCompressionLevelKey, DracoCompressionLevelDefault); + set => Instance.Settings.SetInteger(dracoCompressionLevelKey, value); } - private const string DracoQuantizationBitsPositionKey = "DracoQuantizationBitsPosition"; + private const string dracoQuantizationBitsPositionKey = "DracoQuantizationBitsPosition"; public const int DracoQuantizationBitsPositionDefault = 11; public static int DracoQuantizationBitsPosition { - get => Instance.Settings.GetInteger(DracoQuantizationBitsPositionKey, DracoQuantizationBitsPositionDefault); - set => Instance.Settings.SetInteger(DracoQuantizationBitsPositionKey, value); + get => Instance.Settings.GetInteger(dracoQuantizationBitsPositionKey, DracoQuantizationBitsPositionDefault); + set => Instance.Settings.SetInteger(dracoQuantizationBitsPositionKey, value); } - private const string DracoQuantizationBitsNormalKey = "DracoQuantizationBitsNormal"; + private const string dracoQuantizationBitsNormalKey = "DracoQuantizationBitsNormal"; public const int DracoQuantizationBitsNormalDefault = 8; public static int DracoQuantizationBitsNormal { - get => Instance.Settings.GetInteger(DracoQuantizationBitsNormalKey, DracoQuantizationBitsNormalDefault); - set => Instance.Settings.SetInteger(DracoQuantizationBitsNormalKey, value); + get => Instance.Settings.GetInteger(dracoQuantizationBitsNormalKey, DracoQuantizationBitsNormalDefault); + set => Instance.Settings.SetInteger(dracoQuantizationBitsNormalKey, value); } - private const string DracoQuantizationBitsTextureKey = "DracoQuantizationBitsTextureKey"; + private const string dracoQuantizationBitsTextureKey = "DracoQuantizationBitsTextureKey"; public const int DracoQuantizationBitsTextureDefault = 10; public static int DracoQuantizationBitsTexture { - get => Instance.Settings.GetInteger(DracoQuantizationBitsTextureKey, DracoQuantizationBitsTextureDefault); - set => Instance.Settings.SetInteger(DracoQuantizationBitsTextureKey, value); + get => Instance.Settings.GetInteger(dracoQuantizationBitsTextureKey, DracoQuantizationBitsTextureDefault); + set => Instance.Settings.SetInteger(dracoQuantizationBitsTextureKey, value); } public static glTFExportOptions GetSavedOptions() @@ -129,6 +138,7 @@ public static glTFExportOptions GetSavedOptions() return new glTFExportOptions() { MapRhinoZToGltfY = MapRhinoZToGltfY, + ExportMaterials = ExportMaterials, UseDracoCompression = UseDracoCompression, DracoCompressionLevel = DracoCompressionLevel, DracoQuantizationBitsPosition = DracoQuantizationBitsPosition, diff --git a/glTF-BinExporter/glTFExportOptions.cs b/glTF-BinExporter/glTFExportOptions.cs index 06ba84b..4c7cabf 100644 --- a/glTF-BinExporter/glTFExportOptions.cs +++ b/glTF-BinExporter/glTFExportOptions.cs @@ -9,6 +9,7 @@ namespace glTF_BinExporter public class glTFExportOptions { public bool MapRhinoZToGltfY = glTFBinExporterPlugin.MapRhinoZToGltfYDefault; + public bool ExportMaterials = glTFBinExporterPlugin.ExportMaterialsDefault; public bool UseDracoCompression = glTFBinExporterPlugin.UseDracoCompressionDefault; public int DracoCompressionLevel = glTFBinExporterPlugin.DracoCompressionLevelDefault; public int DracoQuantizationBitsPosition = glTFBinExporterPlugin.DracoQuantizationBitsPositionDefault; diff --git a/glTF-BinExporter/glTFExportOptionsDialog.cs b/glTF-BinExporter/glTFExportOptionsDialog.cs index b8218fa..fca2707 100644 --- a/glTF-BinExporter/glTFExportOptionsDialog.cs +++ b/glTF-BinExporter/glTFExportOptionsDialog.cs @@ -26,6 +26,7 @@ class ExportOptionsDialog : Dialog private Button okButton = new Button(); private CheckBox mapZtoY = new CheckBox(); + private CheckBox exportMaterials = new CheckBox(); public ExportOptionsDialog() { @@ -48,6 +49,8 @@ public ExportOptionsDialog() mapZtoY.Text = "Map Rhino Z to glTF Y"; + exportMaterials.Text = "Export Materials"; + OptionsToDialog(); useDracoCompressionCheck.CheckedChanged += UseDracoCompressionCheck_CheckedChanged; @@ -88,6 +91,7 @@ public ExportOptionsDialog() Rows = { new TableRow(mapZtoY), + new TableRow(exportMaterials), }, }, }; @@ -132,6 +136,8 @@ private void OptionsToDialog() mapZtoY.Checked = glTFBinExporterPlugin.MapRhinoZToGltfY; + exportMaterials.Checked = glTFBinExporterPlugin.ExportMaterials; + dracoQuantizationBitsInputPosition.Value = glTFBinExporterPlugin.DracoQuantizationBitsPosition; dracoQuantizationBitsInputNormal.Value = glTFBinExporterPlugin.DracoQuantizationBitsNormal; dracoQuantizationBitsInputTexture.Value = glTFBinExporterPlugin.DracoQuantizationBitsTexture; @@ -140,15 +146,11 @@ private void OptionsToDialog() private void DialogToOptions() { glTFBinExporterPlugin.UseDracoCompression = GetCheckboxValue(useDracoCompressionCheck); - glTFBinExporterPlugin.DracoCompressionLevel = (int)dracoCompressionLevelInput.Value; - glTFBinExporterPlugin.MapRhinoZToGltfY = GetCheckboxValue(mapZtoY); - + glTFBinExporterPlugin.ExportMaterials = GetCheckboxValue(exportMaterials); glTFBinExporterPlugin.DracoQuantizationBitsPosition = (int)dracoQuantizationBitsInputPosition.Value; - glTFBinExporterPlugin.DracoQuantizationBitsNormal = (int)dracoQuantizationBitsInputNormal.Value; - glTFBinExporterPlugin.DracoQuantizationBitsTexture = (int)dracoQuantizationBitsInputTexture.Value; } From b4ad4fcc1cc611cbd972226de555b2ce7da0f6c7 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Thu, 8 Apr 2021 12:41:59 +0300 Subject: [PATCH 5/7] Made some constants readonly --- glTF-BinExporter/Constants.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glTF-BinExporter/Constants.cs b/glTF-BinExporter/Constants.cs index 528509a..b9936ea 100644 --- a/glTF-BinExporter/Constants.cs +++ b/glTF-BinExporter/Constants.cs @@ -4,7 +4,7 @@ namespace glTF_BinExporter { public static class Constants { - public static ObjectType[] ValidObjectTypes = new ObjectType[] { + public static readonly ObjectType[] ValidObjectTypes = new ObjectType[] { ObjectType.Brep, ObjectType.InstanceReference, ObjectType.Mesh, @@ -13,7 +13,7 @@ public static class Constants ObjectType.SubD }; - public static byte[][] Paddings = new byte[][] + public static readonly byte[][] Paddings = new byte[][] { new byte[] { }, new byte[] { 0, 0, 0 }, From 6e760d0b3fa38703e04d04de26b4bc59765d51b3 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Fri, 9 Apr 2021 09:40:17 +0300 Subject: [PATCH 6/7] Added option to use display color if material not set --- glTF-BinExporter/GlTFUtils.cs | 41 +++++++++-- glTF-BinExporter/RhinoDocGltfConverter.cs | 71 ++++++++++++++----- .../RhinoMaterialGltfConverter.cs | 6 +- glTF-BinExporter/glTFBinExporterPlugin.cs | 10 +++ glTF-BinExporter/glTFExportOptions.cs | 1 + glTF-BinExporter/glTFExportOptionsDialog.cs | 34 ++++++--- 6 files changed, 130 insertions(+), 33 deletions(-) diff --git a/glTF-BinExporter/GlTFUtils.cs b/glTF-BinExporter/GlTFUtils.cs index b4568e0..7859231 100644 --- a/glTF-BinExporter/GlTFUtils.cs +++ b/glTF-BinExporter/GlTFUtils.cs @@ -10,6 +10,14 @@ namespace glTF_BinExporter { + + public struct ObjectExportData + { + public Mesh[] Meshes; + public RenderMaterial RenderMaterial; + public RhinoObject Object; + } + /// /// Functions for helping with adding RhinoObjects to the RootModel. /// @@ -88,9 +96,9 @@ private static string GetDebugName(RhinoObject rhinoObject) /// /// /// - public static List> SanitizeRhinoObjects(IEnumerable rhinoObjects) + public static List SanitizeRhinoObjects(IEnumerable rhinoObjects) { - var rhinoObjectsRes = new List>(); + var rhinoObjectsRes = new List(); foreach (var rhinoObject in rhinoObjects) { @@ -101,9 +109,7 @@ private static string GetDebugName(RhinoObject rhinoObject) } // FIXME: This is broken. Even though objects use the same material, different Materials are returned here. - var mat = rhinoObject.GetMaterial(true); - var renderMatId = mat.Id; - bool isPBR = mat.IsPhysicallyBased; + var mat = rhinoObject.RenderMaterial; // This is always true when called from the Main plugin command, as it uses the same ObjectType array as filter. // Keeping it around in case someone calls this from somewhere else. @@ -115,7 +121,12 @@ private static string GetDebugName(RhinoObject rhinoObject) if(meshes.Length > 0) //Objects need a mesh to export { - rhinoObjectsRes.Add(new Tuple(meshes, mat, renderMatId, rhinoObject)); + rhinoObjectsRes.Add(new ObjectExportData() + { + Meshes = meshes, + RenderMaterial = mat, + Object = rhinoObject, + }); } } else if (rhinoObject.ObjectType == ObjectType.InstanceReference) @@ -139,7 +150,12 @@ private static string GetDebugName(RhinoObject rhinoObject) if(meshes.Length > 0) //Objects need a mesh to export { - rhinoObjectsRes.Add(new Tuple(meshes, mat, renderMatId, item.rhinoObject)); + rhinoObjectsRes.Add(new ObjectExportData() + { + Meshes = meshes, + RenderMaterial = mat, + Object = item.rhinoObject, + }); } } } @@ -186,5 +202,16 @@ public static bool IsFileGltfBinary(string filename) return extension.ToLower() == ".glb"; } + public static float[] ToFloatArray(this Rhino.Display.Color4f color) + { + return new float[] + { + color.R, + color.G, + color.B, + color.A, + }; + } + } } diff --git a/glTF-BinExporter/RhinoDocGltfConverter.cs b/glTF-BinExporter/RhinoDocGltfConverter.cs index 8f524f1..679fb8b 100644 --- a/glTF-BinExporter/RhinoDocGltfConverter.cs +++ b/glTF-BinExporter/RhinoDocGltfConverter.cs @@ -94,21 +94,21 @@ public Gltf ConvertToGltf() var sanitized = GlTFUtils.SanitizeRhinoObjects(objects); - foreach(Tuple tuple in sanitized) + foreach(ObjectExportData exportData in sanitized) { if(options.UseDracoCompression) { - AddRhinoObjectDraco(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); + AddRhinoObjectDraco(exportData); } else { if (binary) { - AddRhinoObjectBinary(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); + AddRhinoObjectBinary(exportData); } else { - AddRhinoObjectText(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); + AddRhinoObjectText(exportData); } } } @@ -131,14 +131,14 @@ public byte[] GetBinaryBuffer() return binaryBuffer.ToArray(); } - private void AddRhinoObjectDraco(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) + private void AddRhinoObjectDraco(ObjectExportData data) { - var materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(data.RenderMaterial, data.Object); var primitives = new List(); // For each rhino mesh, create gl-buffers, gl-meshes, etc. - foreach (var rhinoMesh in rhinoMeshes) + foreach (var rhinoMesh in data.Meshes) { if(options.MapRhinoZToGltfY) { @@ -389,13 +389,13 @@ public void WriteDracoBytes(byte[] bytes, out int bufferIndex, out int byteOffse } } - private void AddRhinoObjectBinary(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) + private void AddRhinoObjectBinary(ObjectExportData data) { - var materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(data.RenderMaterial, data.Object); var primitives = new List(); - foreach (var rhinoMesh in rhinoMeshes) + foreach (var rhinoMesh in data.Meshes) { if (options.MapRhinoZToGltfY) { @@ -548,7 +548,7 @@ private void AddRhinoObjectBinary(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocOb var node = new Node() { Mesh = idxMesh, - Name = string.IsNullOrEmpty(rhinoObject.Name) ? null : rhinoObject.Name, + Name = string.IsNullOrEmpty(data.Object.Name) ? null : data.Object.Name, }; int idxNode = dummy.Nodes.AddAndReturnIndex(node); @@ -556,13 +556,13 @@ private void AddRhinoObjectBinary(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocOb dummy.Scenes[dummy.Scene].Nodes.Add(idxNode); } - private void AddRhinoObjectText(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObjects.Material material, Guid materialId, RhinoObject rhinoObject) + private void AddRhinoObjectText(ObjectExportData data) { - var materialIndex = GetMaterial(material, materialId); + var materialIndex = GetMaterial(data.RenderMaterial, data.Object); var primitives = new List(); - foreach (var rhinoMesh in rhinoMeshes) + foreach (var rhinoMesh in data.Meshes) { if (options.MapRhinoZToGltfY) { @@ -706,7 +706,7 @@ private void AddRhinoObjectText(Rhino.Geometry.Mesh[] rhinoMeshes, Rhino.DocObje var node = new Node() { Mesh = idxMesh, - Name = string.IsNullOrEmpty(rhinoObject.Name) ? null : rhinoObject.Name, + Name = string.IsNullOrEmpty(data.Object.Name) ? null : data.Object.Name, }; int idxNode = dummy.Nodes.AddAndReturnIndex(node); @@ -869,12 +869,24 @@ private byte[] GetTextureCoordinatesBytes(Rhino.Geometry.Collections.MeshTexture return bytesEnumerable.ToArray(); } - int? GetMaterial(Rhino.DocObjects.Material material, Guid materialId) + int? GetMaterial(RenderMaterial material, RhinoObject rhinoObject) { if(!options.ExportMaterials) { return null; } + + if(material == null && options.UseDisplayColorForUnsetMaterials) + { + Color4f objectColor = GetObjectColor(rhinoObject); + return CreateSolidColorMaterial(objectColor); + } + else if(material == null) + { + material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial; + } + + Guid materialId = material.Id; if(!materialsMap.TryGetValue(materialId, out int materialIndex)) { @@ -886,5 +898,32 @@ private byte[] GetTextureCoordinatesBytes(Rhino.Geometry.Collections.MeshTexture return materialIndex; } + int CreateSolidColorMaterial(Color4f color) + { + glTFLoader.Schema.Material material = new glTFLoader.Schema.Material() + { + PbrMetallicRoughness = new MaterialPbrMetallicRoughness() + { + BaseColorFactor = color.ToFloatArray(), + } + }; + + return dummy.Materials.AddAndReturnIndex(material); + } + + Color4f GetObjectColor(RhinoObject rhinoObject) + { + if(rhinoObject.Attributes.ColorSource == ObjectColorSource.ColorFromLayer) + { + int layerIndex = rhinoObject.Attributes.LayerIndex; + + return new Color4f(rhinoObject.Document.Layers[layerIndex].Color); + } + else + { + return new Color4f(rhinoObject.Attributes.ObjectColor); + } + } + } } diff --git a/glTF-BinExporter/RhinoMaterialGltfConverter.cs b/glTF-BinExporter/RhinoMaterialGltfConverter.cs index 9534f29..a9a4258 100644 --- a/glTF-BinExporter/RhinoMaterialGltfConverter.cs +++ b/glTF-BinExporter/RhinoMaterialGltfConverter.cs @@ -15,13 +15,14 @@ namespace glTF_BinExporter { class RhinoMaterialGltfConverter { - public RhinoMaterialGltfConverter(glTFExportOptions options, bool binary, gltfSchemaDummy dummy, List binaryBuffer, Rhino.DocObjects.Material rhinoMaterial, LinearWorkflow workflow) + public RhinoMaterialGltfConverter(glTFExportOptions options, bool binary, gltfSchemaDummy dummy, List binaryBuffer, RenderMaterial renderMaterial, LinearWorkflow workflow) { this.options = options; this.binary = binary; this.dummy = dummy; this.binaryBuffer = binaryBuffer; - this.rhinoMaterial = rhinoMaterial; + this.rhinoMaterial = renderMaterial.SimulatedMaterial(RenderTexture.TextureGeneration.Allow); + this.renderMaterial = renderMaterial; this.workflow = workflow; } @@ -32,6 +33,7 @@ public RhinoMaterialGltfConverter(glTFExportOptions options, bool binary, gltfSc private LinearWorkflow workflow = null; private Rhino.DocObjects.Material rhinoMaterial = null; + private RenderMaterial renderMaterial = null; public int AddMaterial() { diff --git a/glTF-BinExporter/glTFBinExporterPlugin.cs b/glTF-BinExporter/glTFBinExporterPlugin.cs index d6f61de..02c580c 100644 --- a/glTF-BinExporter/glTFBinExporterPlugin.cs +++ b/glTF-BinExporter/glTFBinExporterPlugin.cs @@ -97,6 +97,15 @@ public static bool ExportMaterials set => Instance.Settings.SetBool(exportMaterialsKey, value); } + private const string useDisplayColorForUnsetMaterialsKey = "UseDisplayColorForUnsetMaterials"; + public const bool UseDisplayColorForUnsetMaterialsDefault = true; + + public static bool UseDisplayColorForUnsetMaterials + { + get => Instance.Settings.GetBool(useDisplayColorForUnsetMaterialsKey, UseDisplayColorForUnsetMaterialsDefault); + set => Instance.Settings.SetBool(useDisplayColorForUnsetMaterialsKey, value); + } + private const string dracoCompressionLevelKey = "DracoCompressionLevel"; public const int DracoCompressionLevelDefault = 10; @@ -139,6 +148,7 @@ public static glTFExportOptions GetSavedOptions() { MapRhinoZToGltfY = MapRhinoZToGltfY, ExportMaterials = ExportMaterials, + UseDisplayColorForUnsetMaterials = UseDisplayColorForUnsetMaterials, UseDracoCompression = UseDracoCompression, DracoCompressionLevel = DracoCompressionLevel, DracoQuantizationBitsPosition = DracoQuantizationBitsPosition, diff --git a/glTF-BinExporter/glTFExportOptions.cs b/glTF-BinExporter/glTFExportOptions.cs index 4c7cabf..a4c1213 100644 --- a/glTF-BinExporter/glTFExportOptions.cs +++ b/glTF-BinExporter/glTFExportOptions.cs @@ -10,6 +10,7 @@ public class glTFExportOptions { public bool MapRhinoZToGltfY = glTFBinExporterPlugin.MapRhinoZToGltfYDefault; public bool ExportMaterials = glTFBinExporterPlugin.ExportMaterialsDefault; + public bool UseDisplayColorForUnsetMaterials = glTFBinExporterPlugin.UseDisplayColorForUnsetMaterialsDefault; public bool UseDracoCompression = glTFBinExporterPlugin.UseDracoCompressionDefault; public int DracoCompressionLevel = glTFBinExporterPlugin.DracoCompressionLevelDefault; public int DracoQuantizationBitsPosition = glTFBinExporterPlugin.DracoQuantizationBitsPositionDefault; diff --git a/glTF-BinExporter/glTFExportOptionsDialog.cs b/glTF-BinExporter/glTFExportOptionsDialog.cs index fca2707..2cca7fc 100644 --- a/glTF-BinExporter/glTFExportOptionsDialog.cs +++ b/glTF-BinExporter/glTFExportOptionsDialog.cs @@ -27,6 +27,7 @@ class ExportOptionsDialog : Dialog private CheckBox mapZtoY = new CheckBox(); private CheckBox exportMaterials = new CheckBox(); + private CheckBox useDisplayColorForUnsetMaterial = new CheckBox(); public ExportOptionsDialog() { @@ -50,10 +51,12 @@ public ExportOptionsDialog() mapZtoY.Text = "Map Rhino Z to glTF Y"; exportMaterials.Text = "Export Materials"; + useDisplayColorForUnsetMaterial.Text = "Use display color for objects with no material set"; OptionsToDialog(); useDracoCompressionCheck.CheckedChanged += UseDracoCompressionCheck_CheckedChanged; + exportMaterials.CheckedChanged += ExportMaterials_CheckedChanged; cancelButton.Click += CancelButton_Click; okButton.Click += OkButton_Click; @@ -92,6 +95,7 @@ public ExportOptionsDialog() { new TableRow(mapZtoY), new TableRow(exportMaterials), + new TableRow(useDisplayColorForUnsetMaterial), }, }, }; @@ -128,16 +132,16 @@ public ExportOptionsDialog() private void OptionsToDialog() { - useDracoCompressionCheck.Checked = glTFBinExporterPlugin.UseDracoCompression; + mapZtoY.Checked = glTFBinExporterPlugin.MapRhinoZToGltfY; + exportMaterials.Checked = glTFBinExporterPlugin.ExportMaterials; + EnableDisableMaterialControls(glTFBinExporterPlugin.ExportMaterials); + useDisplayColorForUnsetMaterial.Checked = glTFBinExporterPlugin.UseDisplayColorForUnsetMaterials; + + useDracoCompressionCheck.Checked = glTFBinExporterPlugin.UseDracoCompression; EnableDisableDracoControls(glTFBinExporterPlugin.UseDracoCompression); dracoCompressionLevelInput.Value = glTFBinExporterPlugin.DracoCompressionLevel; - - mapZtoY.Checked = glTFBinExporterPlugin.MapRhinoZToGltfY; - - exportMaterials.Checked = glTFBinExporterPlugin.ExportMaterials; - dracoQuantizationBitsInputPosition.Value = glTFBinExporterPlugin.DracoQuantizationBitsPosition; dracoQuantizationBitsInputNormal.Value = glTFBinExporterPlugin.DracoQuantizationBitsNormal; dracoQuantizationBitsInputTexture.Value = glTFBinExporterPlugin.DracoQuantizationBitsTexture; @@ -145,10 +149,12 @@ private void OptionsToDialog() private void DialogToOptions() { - glTFBinExporterPlugin.UseDracoCompression = GetCheckboxValue(useDracoCompressionCheck); - glTFBinExporterPlugin.DracoCompressionLevel = (int)dracoCompressionLevelInput.Value; glTFBinExporterPlugin.MapRhinoZToGltfY = GetCheckboxValue(mapZtoY); glTFBinExporterPlugin.ExportMaterials = GetCheckboxValue(exportMaterials); + glTFBinExporterPlugin.UseDisplayColorForUnsetMaterials = GetCheckboxValue(useDisplayColorForUnsetMaterial); + + glTFBinExporterPlugin.UseDracoCompression = GetCheckboxValue(useDracoCompressionCheck); + glTFBinExporterPlugin.DracoCompressionLevel = (int)dracoCompressionLevelInput.Value; glTFBinExporterPlugin.DracoQuantizationBitsPosition = (int)dracoQuantizationBitsInputPosition.Value; glTFBinExporterPlugin.DracoQuantizationBitsNormal = (int)dracoQuantizationBitsInputNormal.Value; glTFBinExporterPlugin.DracoQuantizationBitsTexture = (int)dracoQuantizationBitsInputTexture.Value; @@ -174,6 +180,18 @@ private void UseDracoCompressionCheck_CheckedChanged(object sender, EventArgs e) EnableDisableDracoControls(useDraco); } + private void ExportMaterials_CheckedChanged(object sender, EventArgs e) + { + bool enabled = GetCheckboxValue(exportMaterials); + + EnableDisableMaterialControls(enabled); + } + + private void EnableDisableMaterialControls(bool enabled) + { + useDisplayColorForUnsetMaterial.Enabled = enabled; + } + private void CancelButton_Click(object sender, EventArgs e) { this.Close(DialogResult.Cancel); From 6637a3692a7b6f3dc646179763675ca705b1a401 Mon Sep 17 00:00:00 2001 From: Joshua Kennedy Date: Fri, 9 Apr 2021 09:52:35 +0300 Subject: [PATCH 7/7] Added new options to export command --- glTF-BinExporter/GlTFExporterCommand.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/glTF-BinExporter/GlTFExporterCommand.cs b/glTF-BinExporter/GlTFExporterCommand.cs index 5c8e763..f062585 100644 --- a/glTF-BinExporter/GlTFExporterCommand.cs +++ b/glTF-BinExporter/GlTFExporterCommand.cs @@ -66,6 +66,19 @@ private bool GetExportOptions(RunMode mode, out glTFExportOptions options) return false; } + if(Rhino.Input.RhinoGet.GetBool("Export Materials", true, "No", "Yes", ref options.ExportMaterials) != Result.Success) + { + return false; + } + + if(options.ExportMaterials) + { + if(Rhino.Input.RhinoGet.GetBool("Use display color for objects with unset material", true, "No", "Yes", ref options.UseDisplayColorForUnsetMaterials) != Result.Success) + { + return false; + } + } + if (options.UseDracoCompression) { if(Rhino.Input.RhinoGet.GetInteger("Draco Compression Level (max=10)", true, ref options.DracoCompressionLevel, 1, 10) != Result.Success)