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

Use glTFLib as a backend #26

Merged
merged 13 commits into from
Feb 14, 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "glTFLib"]
path = glTFLib
url = https://github.com/mcneel/glTFLib.git
106 changes: 104 additions & 2 deletions glTF-BinExporter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,130 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30611.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "glTF-BinExporter", "glTF-BinExporter\glTF-BinExporter.csproj", "{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "glTF-BinExporter", "glTF-BinExporter\glTF-BinExporter.csproj", "{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}"
EndProject
Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "glTF-BinExporterWin", "glTF-BinExporter\glTF-BinExporterWin.csproj", "{7F49BC9A-169E-4C15-95FA-099B5A7EC715}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "glTF-BinExporterWin", "glTF-BinExporter\glTF-BinExporterWin.csproj", "{7F49BC9A-169E-4C15-95FA-099B5A7EC715}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "glTFLib", "glTFLib", "{276539E0-5857-46E7-9A2B-D8F7C8F5D112}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator", "glTFLib\glTF-CSharp-Loader\Generator\Generator.csproj", "{5F1EB9C4-C06D-438C-8F71-B455DDF85456}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratorLib", "glTFLib\glTF-CSharp-Loader\GeneratorLib\GeneratorLib.csproj", "{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneratorUnitTests", "glTFLib\glTF-CSharp-Loader\GeneratorUnitTests\GeneratorUnitTests.csproj", "{431A50BA-F27A-4685-9061-FFA171106F32}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "glTFLoader", "glTFLib\glTF-CSharp-Loader\glTFLoader\glTFLoader.csproj", "{4C88FA85-98F7-4E37-978C-1885D175B428}"
ProjectSection(ProjectDependencies) = postProject
{5F1EB9C4-C06D-438C-8F71-B455DDF85456} = {5F1EB9C4-C06D-438C-8F71-B455DDF85456}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "glTFLoaderUnitTests", "glTFLib\glTF-CSharp-Loader\glTFLoaderUnitTests\glTFLoaderUnitTests.csproj", "{41F1BEA4-2895-4982-9AB7-A863C61C2075}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|x64.ActiveCfg = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|x64.Build.0 = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|x86.ActiveCfg = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Debug|x86.Build.0 = Debug|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|Any CPU.Build.0 = Release|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|x64.ActiveCfg = Release|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|x64.Build.0 = Release|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|x86.ActiveCfg = Release|Any CPU
{4491BB4A-48C4-4681-9F9E-FEAA285F20CC}.Release|x86.Build.0 = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|x64.ActiveCfg = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|x64.Build.0 = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|x86.ActiveCfg = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Debug|x86.Build.0 = Debug|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|Any CPU.Build.0 = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|x64.ActiveCfg = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|x64.Build.0 = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|x86.ActiveCfg = Release|Any CPU
{7F49BC9A-169E-4C15-95FA-099B5A7EC715}.Release|x86.Build.0 = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|x64.ActiveCfg = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|x64.Build.0 = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|x86.ActiveCfg = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Debug|x86.Build.0 = Debug|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|Any CPU.Build.0 = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|x64.ActiveCfg = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|x64.Build.0 = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|x86.ActiveCfg = Release|Any CPU
{5F1EB9C4-C06D-438C-8F71-B455DDF85456}.Release|x86.Build.0 = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|x64.ActiveCfg = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|x64.Build.0 = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|x86.ActiveCfg = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Debug|x86.Build.0 = Debug|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|Any CPU.Build.0 = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|x64.ActiveCfg = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|x64.Build.0 = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|x86.ActiveCfg = Release|Any CPU
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622}.Release|x86.Build.0 = Release|Any CPU
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|x64.ActiveCfg = Debug|x64
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|x64.Build.0 = Debug|x64
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|x86.ActiveCfg = Debug|x86
{431A50BA-F27A-4685-9061-FFA171106F32}.Debug|x86.Build.0 = Debug|x86
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|Any CPU.Build.0 = Release|Any CPU
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|x64.ActiveCfg = Release|x64
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|x64.Build.0 = Release|x64
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|x86.ActiveCfg = Release|x86
{431A50BA-F27A-4685-9061-FFA171106F32}.Release|x86.Build.0 = Release|x86
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|x64.Build.0 = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|x86.ActiveCfg = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Debug|x86.Build.0 = Debug|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|Any CPU.Build.0 = Release|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|x64.ActiveCfg = Release|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|x64.Build.0 = Release|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|x86.ActiveCfg = Release|Any CPU
{4C88FA85-98F7-4E37-978C-1885D175B428}.Release|x86.Build.0 = Release|Any CPU
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|x64.ActiveCfg = Debug|x64
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|x64.Build.0 = Debug|x64
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|x86.ActiveCfg = Debug|x86
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Debug|x86.Build.0 = Debug|x86
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|Any CPU.Build.0 = Release|Any CPU
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|x64.ActiveCfg = Release|x64
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|x64.Build.0 = Release|x64
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|x86.ActiveCfg = Release|x86
{41F1BEA4-2895-4982-9AB7-A863C61C2075}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5F1EB9C4-C06D-438C-8F71-B455DDF85456} = {276539E0-5857-46E7-9A2B-D8F7C8F5D112}
{DF4D02CB-5035-4A9F-AB22-62FB38AA1622} = {276539E0-5857-46E7-9A2B-D8F7C8F5D112}
{431A50BA-F27A-4685-9061-FFA171106F32} = {276539E0-5857-46E7-9A2B-D8F7C8F5D112}
{4C88FA85-98F7-4E37-978C-1885D175B428} = {276539E0-5857-46E7-9A2B-D8F7C8F5D112}
{41F1BEA4-2895-4982-9AB7-A863C61C2075} = {276539E0-5857-46E7-9A2B-D8F7C8F5D112}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CC6F7860-F467-4BA2-9079-EFDC3EB9F98A}
EndGlobalSection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Drawing;
using System.Drawing;
using Rhino.Display;

namespace glTF_BinExporter.glTF
namespace glTF_BinExporter
{
public static class ColorUtils
{
Expand Down
56 changes: 56 additions & 0 deletions glTF-BinExporter/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Rhino.DocObjects;

namespace glTF_BinExporter
{
public static class Constants
{
public static ObjectType[] ValidObjectTypes = new ObjectType[] {
ObjectType.Brep,
ObjectType.InstanceReference,
ObjectType.Mesh,
ObjectType.Extrusion,
ObjectType.Surface,
ObjectType.SubD
};

public static byte[][] Paddings = new byte[][]
{
new byte[] { },
new byte[] { 0, 0, 0 },
new byte[] { 0, 0 },
new byte[] { 0 },
};

public const string TextBufferHeader = "data:application/octet-stream;base64,";
public const string PositionAttributeTag = "POSITION";
public const string NormalAttributeTag = "NORMAL";
public const string TexCoord0AttributeTag = "TEXCOORD_0";

public const string DracoMeshCompressionExtensionTag = "KHR_draco_mesh_compression";
}

public class DracoGeometryInfo
{
public bool success;

public int bufferIndex;
public int byteOffset;
public int byteLength;

public int verticesNum;
public float[] verticesMin;
public float[] verticesMax;

public int trianglesNum;
public float trianglesMin;
public float trianglesMax;

public int normalsNum;
public float[] normalsMin;
public float[] normalsMax;

public int texCoordsNum;
public float[] texCoordsMin;
public float[] texCoordsMax;
}
}
32 changes: 12 additions & 20 deletions glTF-BinExporter/GlTFExporterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;
using System.Collections.Generic;

namespace glTF_BinExporter.glTF
namespace glTF_BinExporter
{

[System.Runtime.InteropServices.Guid("82936404-bd41-46f4-8fe7-e594c2a7e8af")]
Expand Down Expand Up @@ -90,25 +90,17 @@ public static bool DoExport(string fileName, glTFExportOptions opts, IEnumerable
{
try
{
// Writes the result to a memory stream, then dumps it to a file.
using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
using (MemoryStream memoryStream = new MemoryStream(1024))
{

if (opts.UseBinary)
{
GlTFUtils.ExportBinary(memoryStream, rhinoObjects, opts);
}
else
{
GlTFUtils.ExportText(memoryStream, rhinoObjects, opts);
}

memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
fileStream.Flush();
fileStream.Close();
RhinoDocGltfConverter converter = new RhinoDocGltfConverter(opts, rhinoObjects);
glTFLoader.Schema.Gltf gltf = converter.ConvertToGltf();

if(opts.UseBinary)
{
byte[] bytes = converter.GetBinaryBuffer();
glTFLoader.Interface.SaveBinaryModel(gltf, bytes, fileName);
}
else
{
glTFLoader.Interface.SaveModel(gltf, fileName);
}

RhinoApp.WriteLine("Successfully exported selected geometry to glTF(Binary).");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Rhino.Geometry;
using Rhino.Render;

namespace glTF_BinExporter.glTF
namespace glTF_BinExporter
{
/// <summary>
/// Functions for helping with adding RhinoObjects to the RootModel.
Expand Down Expand Up @@ -62,8 +62,10 @@ public static Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) {
/// </summary>
/// <param name="rhinoObjects"></param>
/// <returns></returns>
public static List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid>> SanitizeRhinoObjects(IEnumerable<RhinoObject> rhinoObjects) {
var rhinoObjectsRes = new List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid>>();
public static List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>> SanitizeRhinoObjects(IEnumerable<RhinoObject> rhinoObjects)
{

var rhinoObjectsRes = new List<Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>>();

foreach (var rhinoObject in rhinoObjects)
{
Expand All @@ -79,7 +81,7 @@ public static Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) {
if (isValidGeometry && rhinoObject.ObjectType != ObjectType.InstanceReference)
{
// None-block. Just add it to the result list
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid>(GetMeshes(rhinoObject), mat, renderMatId));
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>(GetMeshes(rhinoObject), mat, renderMatId, rhinoObject));
} else if (rhinoObject.ObjectType == ObjectType.InstanceReference) {
// Cast to InstanceObject/BlockInstance
var instanceObject = (InstanceObject)rhinoObject;
Expand All @@ -97,7 +99,7 @@ public static Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) {
}

// Add the exploded, transformed geo to the result list
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid>(meshes, mat, renderMatId));
rhinoObjectsRes.Add(new Tuple<Rhino.Geometry.Mesh[], Rhino.DocObjects.Material, Guid, RhinoObject>(meshes, mat, renderMatId, item.rhinoObject));
}
} else {
// TODO: Should give better error message here.
Expand All @@ -107,57 +109,6 @@ public static Rhino.Geometry.Mesh[] GetMeshes(RhinoObject rhinoObject) {
return rhinoObjectsRes;
}

public static void ExportText(MemoryStream outStream, IEnumerable<RhinoObject> rhinoObjects, glTFExportOptions exportOptions)
{
RootModel mdl = new RootModel(exportOptions);

var sanitizedRhinoObjects = SanitizeRhinoObjects(rhinoObjects);

foreach (var kv in sanitizedRhinoObjects)
{
// Regular objects
if (exportOptions.UseDracoCompression)
{
mdl.AddRhinoObjectDraco(kv.Item1, kv.Item2, kv.Item3);
}
else
{
mdl.AddRhinoObject(kv.Item1, kv.Item2, kv.Item3);
}

foreach (var mesh in kv.Item1) {
mesh.Dispose();
}
}

var json = mdl.SerializeToJSON();
byte[] jsonBytes = Encoding.ASCII.GetBytes(json);

outStream.Write(jsonBytes, 0, jsonBytes.Length);
outStream.Flush();
}

public static void ExportBinary(MemoryStream outStream, IEnumerable<RhinoObject> rhinoObjects, glTFExportOptions exportOptions)
{
RootModel mdl = new RootModel(exportOptions);

var sanitizedRhinoObjects = SanitizeRhinoObjects(rhinoObjects);

foreach (var kv in sanitizedRhinoObjects)
{
// Regular objects
if (exportOptions.UseDracoCompression) {
mdl.AddRhinoObjectDraco(kv.Item1, kv.Item2, kv.Item3);
} else {
mdl.AddRhinoObject(kv.Item1, kv.Item2, kv.Item3);
}
}

mdl.SerializeToGLB(outStream);

outStream.Flush();
}

public static int AddAndReturnIndex<T>(this List<T> list, T item)
{
list.Add(item);
Expand Down
Loading