Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
136 changes: 91 additions & 45 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,10 @@ 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>();

private int defaultMaterialIndex = -1;

public Gltf ConvertToGltf()
{
dummy.Scene = 0;
Expand All @@ -68,7 +74,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,9 +85,10 @@ public Gltf ConvertToGltf()

var sanitized = SanitizeRhinoObjects(objects);

foreach(ObjectExportData exportData in sanitized)
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();
Expand All @@ -94,17 +101,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 +126,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 +147,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,41 +172,76 @@ public byte[] GetBinaryBuffer()

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

for (int i = 0; i < materials.Length; i++)
Dictionary<Color4f, int> colorIndices = new Dictionary<Color4f, int>();

for (int i = 0; i < subObjects.Length; i++)
{
var material = materials[i];

if (!options.ExportMaterials)
if(material == null)
{
return null;
}

if (material == null && options.UseDisplayColorForUnsetMaterials)
{
Color4f objectColor = GetObjectColor(rhinoObject);
materialIndices[i] = CreateSolidColorMaterial(objectColor, GetObjectName(rhinoObject));
continue;
if(options.UseDisplayColorForUnsetMaterials)
{
if(subObjects[i].Attributes.ColorSource == ObjectColorSource.ColorFromLayer)
{
materialIndices[i] = GetLayerMaterial(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;
}
}
else
{
materialIndices[i] = GetDefaultMaterial();
}
}
else if (material == null)
else
{
material = Rhino.DocObjects.Material.DefaultMaterial.RenderMaterial;
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;
}
}

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);
}
return materialIndices;
}

materialIndices[i] = materialIndex;
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;
}

return materialIndices;
private int GetLayerMaterial(RhinoObject rhinoObject)
{
if (!layerMaterialIndices.TryGetValue(rhinoObject.Attributes.LayerIndex, out int layerMaterialIndex))
{
Color4f objectColor = GetLayerColor(rhinoObject);
layerMaterialIndex = CreateSolidColorMaterial(objectColor, doc.Layers[rhinoObject.Attributes.LayerIndex].Name);
layerMaterialIndices.Add(rhinoObject.Attributes.LayerIndex, layerMaterialIndex);
}
return layerMaterialIndex;
}

int CreateSolidColorMaterial(Color4f color, string name)
Expand All @@ -218,18 +260,22 @@ 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 GetLayerColor(rhinoObject);
}
else
{
return new Color4f(rhinoObject.Attributes.ObjectColor);
}
}

Color4f GetLayerColor(RhinoObject rhinoObject)
{
int layerIndex = rhinoObject.Attributes.LayerIndex;
return new Color4f(doc.Layers[layerIndex].Color);
}

public Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject)
{

Expand All @@ -239,7 +285,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 +349,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