Skip to content
Open
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
24 changes: 24 additions & 0 deletions glTF-BinExporter/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ public static class Constants
public const string NormalAttributeTag = "NORMAL";
public const string TexCoord0AttributeTag = "TEXCOORD_0";
public const string VertexColorAttributeTag = "COLOR_0";

public static readonly Transform ZtoYUp = new Transform()
{
M00 = 1,
M01 = 0,
M02 = 0,
M03 = 0,

M10 = 0,
M11 = 0,
M12 = 1,
M13 = 0,

M20 = 0,
M21 = -1,
M22 = 0,
M23 = 0,

M30 = 0,
M31 = 0,
M32 = 0,
M33 = 1,
};

}

public class DracoGeometryInfo
Expand Down
82 changes: 49 additions & 33 deletions glTF-BinExporter/RhinoDocGltfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using Rhino.DocObjects;
using Rhino;
using glTFLoader.Schema;
using Rhino.Render;
using Rhino.Display;

Expand Down Expand Up @@ -53,7 +52,7 @@ public RhinoDocGltfConverter(glTFExportOptions options, bool binary, RhinoDoc do

private List<byte> binaryBuffer = new List<byte>();

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

private RenderMaterial defaultMaterial = null;
private RenderMaterial DefaultMaterial
Expand All @@ -68,22 +67,22 @@ private RenderMaterial DefaultMaterial
return defaultMaterial;
}
}
public Gltf ConvertToGltf()
public glTFLoader.Schema.Gltf ConvertToGltf()
{
dummy.Scene = 0;
dummy.Scenes.Add(new gltfSchemaSceneDummy());

dummy.Asset = new Asset()
dummy.Asset = new glTFLoader.Schema.Asset()
{
Version = "2.0",
};

dummy.Samplers.Add(new Sampler()
dummy.Samplers.Add(new glTFLoader.Schema.Sampler()
{
MinFilter = Sampler.MinFilterEnum.LINEAR,
MagFilter = Sampler.MagFilterEnum.LINEAR,
WrapS = Sampler.WrapSEnum.REPEAT,
WrapT = Sampler.WrapTEnum.REPEAT,
MinFilter = glTFLoader.Schema.Sampler.MinFilterEnum.LINEAR,
MagFilter = glTFLoader.Schema.Sampler.MagFilterEnum.LINEAR,
WrapS = glTFLoader.Schema.Sampler.WrapSEnum.REPEAT,
WrapT = glTFLoader.Schema.Sampler.WrapTEnum.REPEAT,
});

if(options.UseDracoCompression)
Expand All @@ -96,8 +95,30 @@ public Gltf ConvertToGltf()
dummy.ExtensionsUsed.Add(glTFExtensions.KHR_materials_clearcoat.Tag);
dummy.ExtensionsUsed.Add(glTFExtensions.KHR_materials_ior.Tag);
dummy.ExtensionsUsed.Add(glTFExtensions.KHR_materials_specular.Tag);
dummy.ExtensionsUsed.Add(glTFExtensions.KHR_materials_volume.Tag);

var sanitized = SanitizeRhinoObjects(objects);
IEnumerable<Rhino.DocObjects.RhinoObject> pointClouds = objects.Where(x => x.ObjectType == ObjectType.PointSet);

foreach(Rhino.DocObjects.RhinoObject rhinoObject in pointClouds)
{
RhinoPointCloudGltfConverter converter = new RhinoPointCloudGltfConverter(rhinoObject, options, binary, dummy, binaryBuffer);
int meshIndex = converter.AddPointCloud();

if(meshIndex != -1)
{
glTFLoader.Schema.Node node = new glTFLoader.Schema.Node()
{
Mesh = meshIndex,
Name = GetObjectName(rhinoObject),
};

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

AddNode(nodeIndex, rhinoObject);
}
}

List<ObjectExportData> sanitized = SanitizeRhinoObjects(objects);

foreach(ObjectExportData exportData in sanitized)
{
Expand All @@ -114,32 +135,37 @@ public Gltf ConvertToGltf()

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

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

if (binary && binaryBuffer.Count > 0)
{
//have to add the empty buffer for the binary file header
dummy.Buffers.Add(new glTFLoader.Schema.Buffer()
{
ByteLength = (int)binaryBuffer.Count,
ByteLength = binaryBuffer.Count,
Uri = null,
});
}

return dummy.ToSchemaGltf();
}

private void AddNode(int nodeIndex, Rhino.DocObjects.RhinoObject rhinoObject)
{
if (options.ExportLayers)
{
AddToLayer(doc.Layers[rhinoObject.Attributes.LayerIndex], nodeIndex);
}
else
{
dummy.Scenes[dummy.Scene].Nodes.Add(nodeIndex);
}
}

private void AddToLayer(Layer layer, int child)
{
if(layers.TryGetValue(layer.Index, out Node node))
if(layers.TryGetValue(layer.Index, out glTFLoader.Schema.Node node))
{
if (node.Children == null)
{
Expand All @@ -152,7 +178,7 @@ private void AddToLayer(Layer layer, int child)
}
else
{
node = new Node()
node = new glTFLoader.Schema.Node()
{
Name = layer.Name,
Children = new int[1] { child },
Expand All @@ -177,7 +203,7 @@ private void AddToLayer(Layer layer, int child)

public string GetObjectName(RhinoObject rhinoObject)
{
return string.IsNullOrEmpty(rhinoObject.Name) ? null : rhinoObject.Name;
return string.IsNullOrEmpty(rhinoObject.Name) ? rhinoObject.Id.ToString() : rhinoObject.Name;
}

public byte[] GetBinaryBuffer()
Expand Down Expand Up @@ -218,7 +244,7 @@ int CreateSolidColorMaterial(Color4f color)
{
glTFLoader.Schema.Material material = new glTFLoader.Schema.Material()
{
PbrMetallicRoughness = new MaterialPbrMetallicRoughness()
PbrMetallicRoughness = new glTFLoader.Schema.MaterialPbrMetallicRoughness()
{
BaseColorFactor = color.ToFloatArray(),
}
Expand Down Expand Up @@ -318,16 +344,6 @@ public bool MeshIsValidForExport(Rhino.Geometry.Mesh mesh)
return true;
}

private string GetDebugName(RhinoObject rhinoObject)
{
if (string.IsNullOrEmpty(rhinoObject.Name))
{
return "(Unnamed)";
}

return rhinoObject.Name;
}

public List<ObjectExportData> SanitizeRhinoObjects(IEnumerable<RhinoObject> rhinoObjects)
{
List<ObjectExportData> explodedObjects = new List<ObjectExportData>();
Expand Down
54 changes: 35 additions & 19 deletions glTF-BinExporter/RhinoMaterialGltfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public int AddMaterial()
Rhino.DocObjects.Texture clearcoatTexture = pbr.GetTexture(TextureType.PBR_Clearcoat);
Rhino.DocObjects.Texture clearcoatRoughessTexture = pbr.GetTexture(TextureType.PBR_ClearcoatRoughness);
Rhino.DocObjects.Texture clearcoatNormalTexture = pbr.GetTexture(TextureType.PBR_ClearcoatBump);
Rhino.DocObjects.Texture specularTexture = pbr.GetTexture(TextureType.PBR_Specular);
Rhino.DocObjects.Texture specularTintTexture = pbr.GetTexture(TextureType.PBR_SpecularTint);

HandleBaseColor(rhinoMaterial, material);

Expand Down Expand Up @@ -104,7 +104,15 @@ public int AddMaterial()
if (emissiveTexture != null && emissiveTexture.Enabled)
{
material.EmissiveTexture = AddTexture(emissiveTexture.FileReference.FullPath);

material.EmissiveFactor = new float[]
{
1.0f,
1.0f,
1.0f,
};
}
else
{
float emissionMultiplier = 1.0f;

var param = rhinoMaterial.RenderMaterial.GetParameter("emission-multiplier");
Expand All @@ -114,20 +122,19 @@ public int AddMaterial()
emissionMultiplier = (float)Convert.ToDouble(param);
}

float r = rhinoMaterial.PhysicallyBased.Emission.R / emissionMultiplier;
float g = rhinoMaterial.PhysicallyBased.Emission.G / emissionMultiplier;
float b = rhinoMaterial.PhysicallyBased.Emission.B / emissionMultiplier;

r = Math.Max(Math.Min(r, 0.0f), 1.0f);
g = Math.Max(Math.Min(g, 0.0f), 1.0f);
b = Math.Max(Math.Min(b, 0.0f), 1.0f);

material.EmissiveFactor = new float[]
{
emissionMultiplier,
emissionMultiplier,
emissionMultiplier,
};
}
else
{
material.EmissiveFactor = new float[]
{
rhinoMaterial.PhysicallyBased.Emission.R,
rhinoMaterial.PhysicallyBased.Emission.G,
rhinoMaterial.PhysicallyBased.Emission.B,
r,
g,
b,
};
}

Expand Down Expand Up @@ -197,19 +204,28 @@ public int AddMaterial()

glTFExtensions.KHR_materials_specular specular = new glTFExtensions.KHR_materials_specular();

if(specularTexture != null && specularTexture.Enabled)
if(specularTintTexture != null && specularTintTexture.Enabled)
{
//Specular is stored in the textures alpha channel
specular.SpecularTexture = GetSingleChannelTexture(specularTexture, RgbaChannel.Alpha, false);
specular.SpecularFactor = GetTextureWeight(specularTexture);
specular.SpecularTexture = GetSingleChannelTexture(specularTintTexture, RgbaChannel.Alpha, false);
specular.SpecularFactor = GetTextureWeight(specularTintTexture);
}
else
{
specular.SpecularFactor = (float)pbr.Specular;
specular.SpecularFactor = (float)pbr.SpecularTint;
}

material.Extensions.Add(glTFExtensions.KHR_materials_specular.Tag, specular);

//Volume extension

glTFExtensions.KHR_materials_volume volume = new glTFExtensions.KHR_materials_volume();

//0 is thin surface, all else is the geometries thickness. We don't support anything other than this.
volume.ThicknessFactor = 1.0f;

material.Extensions.Add(glTFExtensions.KHR_materials_volume.Tag, volume);

return dummy.Materials.AddAndReturnIndex(material);
}

Expand Down Expand Up @@ -453,7 +469,7 @@ private int AddTextureToBuffers(string texturePath)
var texture = new glTFLoader.Schema.Texture()
{
Source = imageIdx,
Sampler = 0
Sampler = 0,
};

return dummy.Textures.AddAndReturnIndex(texture);
Expand Down
27 changes: 2 additions & 25 deletions glTF-BinExporter/RhinoMeshGltfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,6 @@ public RhinoMeshGltfConverter(ObjectExportData exportData, int? materialIndex, g

private DracoGeometryInfo currentGeometryInfo = null;

private readonly Transform ZtoYUp = new Transform()
{
M00 = 1,
M01 = 0,
M02 = 0,
M03 = 0,

M10 = 0,
M11 = 0,
M12 = 1,
M13 = 0,

M20 = 0,
M21 = -1,
M22 = 0,
M23 = 0,

M30 = 0,
M31 = 0,
M32 = 0,
M33 = 1,
};

public int AddMesh()
{
List<glTFLoader.Schema.MeshPrimitive> primitives = GetPrimitives();
Expand All @@ -71,7 +48,7 @@ private void PreprocessMesh(Mesh rhinoMesh)
{
if (options.MapRhinoZToGltfY)
{
rhinoMesh.Transform(ZtoYUp);
rhinoMesh.Transform(Constants.ZtoYUp);
}

rhinoMesh.TextureCoordinates.ReverseTextureCoordinates(1);
Expand Down Expand Up @@ -158,7 +135,7 @@ private void PreprocessMesh(Mesh rhinoMesh)

primitive.Extensions.Add(glTFExtensions.KHR_draco_mesh_compression.Tag, dracoCompressionObject);
}

primitive.Material = materialIndex;

primitives.Add(primitive);
Expand Down
Loading