Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions glTF-BinExporter/GlTFExporterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -132,9 +132,9 @@ private SaveFileDialog GetSaveFileDialog()
};
}

public static bool DoExport(string fileName, glTFExportOptions options, bool binary, IEnumerable<Rhino.DocObjects.RhinoObject> rhinoObjects, Rhino.Render.LinearWorkflow workflow)
public static bool DoExport(string fileName, glTFExportOptions options, bool binary, RhinoDoc doc, IEnumerable<Rhino.DocObjects.RhinoObject> 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)
Expand Down
84 changes: 60 additions & 24 deletions glTF-BinExporter/RhinoDocGltfConverter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -20,8 +18,9 @@ public struct ObjectExportData

class RhinoDocGltfConverter
{
public RhinoDocGltfConverter(glTFExportOptions options, bool binary, IEnumerable<RhinoObject> objects, LinearWorkflow workflow)
public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc doc, IEnumerable<RhinoObject> objects, LinearWorkflow workflow)
{
this.doc = doc;
this.options = options;
this.binary = binary;
this.objects = objects;
Expand All @@ -30,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<RhinoObject> objects = null;

private bool binary = false;
Expand All @@ -50,6 +52,8 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do

private Dictionary<int, Node> layers = new Dictionary<int, Node>();

private Dictionary<int, int> layerMaterialIndices = new Dictionary<int, int>();

public Gltf ConvertToGltf()
{
dummy.Scene = 0;
Expand All @@ -68,7 +72,7 @@ public Gltf ConvertToGltf()
WrapT = Sampler.WrapTEnum.REPEAT,
});

if(options.UseDracoCompression)
if (options.UseDracoCompression)
{
dummy.ExtensionsUsed.Add(Constants.DracoMeshCompressionExtensionTag);
dummy.ExtensionsRequired.Add(Constants.DracoMeshCompressionExtensionTag);
Expand All @@ -79,7 +83,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);

Expand All @@ -94,17 +98,17 @@ public Gltf ConvertToGltf()

int nodeIndex = dummy.Nodes.AddAndReturnIndex(node);

if(options.ExportLayers)
if (options.ExportLayers)
{
AddToLayer(RhinoDoc.ActiveDoc.Layers[exportData.Object.Attributes.LayerIndex], nodeIndex);
AddToLayer(doc.Layers[exportData.Object.Attributes.LayerIndex], nodeIndex);
}
else
{
dummy.Scenes[dummy.Scene].Nodes.Add(nodeIndex);
}
}

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()
Expand All @@ -119,7 +123,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)
{
Expand All @@ -140,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)
{
Expand All @@ -165,8 +169,11 @@ public byte[] GetBinaryBuffer()

int[] GetMaterials(RenderMaterial[] materials, RhinoObject rhinoObject)
{
RhinoObject[] subObjects = rhinoObject.GetSubObjects();
int[] materialIndices = new int[materials.Length];

Dictionary<Color4f, int> colorIndices = new Dictionary<Color4f, int>();

for (int i = 0; i < materials.Length; i++)
{
var material = materials[i];
Expand All @@ -176,18 +183,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))
{
Expand All @@ -202,6 +227,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, doc.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()
Expand All @@ -218,11 +254,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(doc.Layers[layerIndex].Color);
}
else
{
Expand All @@ -239,7 +275,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;

Expand Down Expand Up @@ -303,7 +339,7 @@ public bool MeshIsValidForExport(Rhino.Geometry.Mesh mesh)
return false;
}

if(!options.ExportOpenMeshes && !mesh.IsClosed)
if (!options.ExportOpenMeshes && !mesh.IsClosed)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion glTF-BinExporter/glTFBinExporterPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected override WriteFileResult WriteFile(string filename, int index, RhinoDo

IEnumerable<Rhino.DocObjects.RhinoObject> 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;
}
Expand Down