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

Merge materials #64

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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);
jrz371 marked this conversation as resolved.
Show resolved Hide resolved

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>();
jrz371 marked this conversation as resolved.
Show resolved Hide resolved

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;
jrz371 marked this conversation as resolved.
Show resolved Hide resolved
}

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;
jrz371 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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();
jrz371 marked this conversation as resolved.
Show resolved Hide resolved
}
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);
jrz371 marked this conversation as resolved.
Show resolved Hide resolved
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