Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export dialog tabs #51

Merged
merged 7 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions glTF-BinExporter/ColorUtils.cs

This file was deleted.

4 changes: 2 additions & 2 deletions glTF-BinExporter/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace glTF_BinExporter
{
public static class Constants
{
public static ObjectType[] ValidObjectTypes = new ObjectType[] {
public static readonly ObjectType[] ValidObjectTypes = new ObjectType[] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💎

ObjectType.Brep,
ObjectType.InstanceReference,
ObjectType.Mesh,
Expand All @@ -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 },
Expand Down
101 changes: 71 additions & 30 deletions glTF-BinExporter/GlTFExporterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,87 @@ 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();

var opts = new glTFExportOptions() { UseDracoCompression = false, DracoCompressionLevel = 10, DracoQuantizationBitsPosition = 11, DracoQuantizationBitsNormal = 8, DracoQuantizationBitsTexture = 10, UseBinary = binary };
if(!DoExport(dialog.FileName, opts, binary, rhinoObjects, doc.RenderSettings.LinearWorkflow))
{
return Result.Failure;
}

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(Rhino.Input.RhinoGet.GetBool("Compression", true, "None", "Draco", ref options.UseDracoCompression) != Result.Success)
{
return false;
}

if (opts.UseDracoCompression)
if(Rhino.Input.RhinoGet.GetBool("Export Materials", true, "No", "Yes", ref options.ExportMaterials) != 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.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 (optionsDlg.ShowModal() == null)
if (options.UseDracoCompression)
{
return Result.Cancel;
if(Rhino.Input.RhinoGet.GetInteger("Draco Compression Level (max=10)", true, ref options.DracoCompressionLevel, 1, 10) != Result.Success)
{
return false;
}

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;
}
}
}

var rhinoObjects = go
.Objects()
.Select(o => o.Object())
.ToArray();

if(!DoExport(dialog.FileName, opts, rhinoObjects, doc.RenderSettings.LinearWorkflow))
if(Rhino.Input.RhinoGet.GetBool("Map Rhino Z to glTF Y", true, "No", "Yes", ref options.MapRhinoZToGltfY) != Result.Success)
{
return false;
}

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()
Expand All @@ -90,14 +131,14 @@ private SaveFileDialog GetSaveFileDialog()
};
}

public static bool DoExport(string fileName, glTFExportOptions opts, IEnumerable<Rhino.DocObjects.RhinoObject> rhinoObjects, Rhino.Render.LinearWorkflow workflow)
public static bool DoExport(string fileName, glTFExportOptions options, bool binary, IEnumerable<Rhino.DocObjects.RhinoObject> 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);
Expand Down
41 changes: 34 additions & 7 deletions glTF-BinExporter/GlTFUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

namespace glTF_BinExporter
{

public struct ObjectExportData
{
public Mesh[] Meshes;
public RenderMaterial RenderMaterial;
public RhinoObject Object;
}

/// <summary>
/// Functions for helping with adding RhinoObjects to the RootModel.
/// </summary>
Expand Down Expand Up @@ -88,9 +96,9 @@ private static string GetDebugName(RhinoObject rhinoObject)
/// </summary>
/// <param name="rhinoObjects"></param>
/// <returns></returns>
public static List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>> SanitizeRhinoObjects(IEnumerable<RhinoObject> rhinoObjects)
public static List<ObjectExportData> SanitizeRhinoObjects(IEnumerable<RhinoObject> rhinoObjects)
{
var rhinoObjectsRes = new List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>>();
var rhinoObjectsRes = new List<ObjectExportData>();

foreach (var rhinoObject in rhinoObjects)
{
Expand All @@ -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.
Expand All @@ -115,7 +121,12 @@ private static string GetDebugName(RhinoObject rhinoObject)

if(meshes.Length > 0) //Objects need a mesh to export
{
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>(meshes, mat, renderMatId, rhinoObject));
rhinoObjectsRes.Add(new ObjectExportData()
{
Meshes = meshes,
RenderMaterial = mat,
Object = rhinoObject,
});
}
}
else if (rhinoObject.ObjectType == ObjectType.InstanceReference)
Expand All @@ -139,7 +150,12 @@ private static string GetDebugName(RhinoObject rhinoObject)

if(meshes.Length > 0) //Objects need a mesh to export
{
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>(meshes, mat, renderMatId, item.rhinoObject));
rhinoObjectsRes.Add(new ObjectExportData()
{
Meshes = meshes,
RenderMaterial = mat,
Object = item.rhinoObject,
});
}
}
}
Expand Down Expand Up @@ -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,
};
}

}
}
Loading