Skip to content
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
2 changes: 2 additions & 0 deletions Common_glTF_Exporter/Common_glTF_Exporter.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Transform\ModelScale.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Analytics.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\DirectoryUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\ExportLog.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Hyperlink.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\LogConfiguration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\MaterialUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\GeometryUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\GLTFBinaryDataUtils.cs" />
Expand Down
40 changes: 31 additions & 9 deletions Common_glTF_Exporter/Core/GlTFExportContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public GLTFExportContext(Document doc)
/// <returns>TRUE if starded.</returns>
public bool Start()
{
ExportLog.Write("Export Started");
preferences = Common_glTF_Exporter.Windows.MainWindow.Settings.GetInfo();

cancelation = false;
Expand All @@ -110,6 +111,9 @@ public bool Start()
currentGeometry = new IndexedDictionary<GeometryDataObject>();
currentVertices = new IndexedDictionary<VertexLookupIntObject>();

currentMaterial = GLTFExportUtils.CreateDefaultGLTFMaterial(1, true);
materials.AddOrUpdateCurrentMaterial(currentMaterial.UniqueId, currentMaterial, false);

return true;
}

Expand All @@ -134,6 +138,12 @@ public void Finish()
scenes, nodes, meshes, materials, accessors, textures, images);
Compression.Run(preferences, ProgressBarWindow.ViewModel);
}

if (currentElement != null)
{
ExportLog.Write($"Last Element {currentElement.Id}");
}
ExportLog.Write("Export Finished");
}

/// <summary>
Expand Down Expand Up @@ -268,18 +278,25 @@ public void OnElementEnd(ElementId elementId)
/// <param name="node">Material node.</param>
public void OnMaterial(MaterialNode node)
{
if (preferences.materials == MaterialsEnum.materials || preferences.materials == MaterialsEnum.textures)
if (preferences.materials == MaterialsEnum.materials || preferences.materials == MaterialsEnum.textures)
{
if (node.MaterialId == ElementId.InvalidElementId)
{
currentMaterial = GLTFExportUtils.GetGLTFMaterial(materials, node.Transparency, false);
}
else
else
{
currentMaterial = RevitMaterials.Export(node, preferences, currentDocument);
string materialId = node.MaterialId.ToString();
if (materials.Contains(materialId))
{
currentMaterial = materials.GetElement(materialId);
}
else
{
currentMaterial = RevitMaterials.Export(node, preferences, currentDocument);
}
materials.AddOrUpdateCurrentMaterial(materialId, currentMaterial, false);
}

materials.AddOrUpdateCurrentMaterial(currentMaterial.UniqueId, currentMaterial, false);
}
}

Expand All @@ -292,7 +309,8 @@ public void OnMaterial(MaterialNode node)
/// <param name="polymesh">PolymeshTopology.</param>
public void OnPolymesh(PolymeshTopology polymesh)
{
GLTFExportUtils.AddOrUpdateCurrentItem(currentElement, currentGeometry, currentVertices, currentMaterial);
GLTFExportUtils.AddOrUpdateCurrentItem(currentElement, currentGeometry,
currentVertices, currentMaterial);

var geomItem = currentGeometry.CurrentItem;
var vertItem = currentVertices.CurrentItem;
Expand Down Expand Up @@ -375,6 +393,7 @@ public void OnViewEnd(ElementId elementId)

public RenderNodeAction OnLinkBegin(LinkNode node)
{
ExportLog.Write($"On LinkBegin {node.NodeName}");
isLink = true;

currentDocument = node.GetDocument();
Expand All @@ -388,6 +407,7 @@ public RenderNodeAction OnLinkBegin(LinkNode node)

public void OnLinkEnd(LinkNode node)
{
ExportLog.Write($"On LinkEnd {node.NodeName}");
isLink = false;
// Note: This method is invoked even for instances that were skipped.
transformStack.Pop();
Expand Down Expand Up @@ -416,9 +436,11 @@ public void OnRPC(RPCNode node)
continue;
}

currentMaterial = MaterialUtils.GetGltfMeshMaterial(currentDocument, preferences, mesh, materials, true);

materials.AddOrUpdateCurrentMaterial(currentMaterial.UniqueId, currentMaterial, true);
if (preferences.materials == MaterialsEnum.materials || preferences.materials == MaterialsEnum.textures)
{
currentMaterial = MaterialUtils.GetGltfMeshMaterial(currentDocument, preferences, mesh, materials, true);
materials.AddOrUpdateCurrentMaterial(currentMaterial.UniqueId, currentMaterial, true);
}

GLTFExportUtils.AddOrUpdateCurrentItem(currentElement, currentGeometry, currentVertices, currentMaterial);

Expand Down
1 change: 1 addition & 0 deletions Common_glTF_Exporter/ExternalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme
catch (Exception ex)
{
Analytics.Send("Error", ex.Message).GetAwaiter();
ExportLog.WriteException(ex);
MessageWindow.Show("Error", ex.Message);
return Result.Failed;
}
Expand Down
93 changes: 59 additions & 34 deletions Common_glTF_Exporter/Materials/AssetProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,70 @@ namespace Common_glTF_Exporter.Materials
{
public static class AssetPropertiesUtils
{
private static readonly string[] DIFFUSE_NAMES = {
Autodesk.Revit.DB.Visual.AdvancedOpaque.OpaqueAlbedo,
Autodesk.Revit.DB.Visual.Generic.GenericDiffuse,
Autodesk.Revit.DB.Visual.AdvancedWood.WoodCurlyDistortionMap,
Autodesk.Revit.DB.Visual.Hardwood.HardwoodColor,
Autodesk.Revit.DB.Visual.AdvancedMetal.SurfaceAlbedo
private static readonly Dictionary<string, string> DiffusePropertyMap = new Dictionary<string, string>
{
{ "ConcreteSchema", Autodesk.Revit.DB.Visual.Concrete.ConcreteBmMap },
{ "PrismGenericSchema", Autodesk.Revit.DB.Visual.Generic.GenericDiffuse },
{ "GenericSchema", Autodesk.Revit.DB.Visual.Generic.GenericDiffuse },
{ "PrismMetalSchema", Autodesk.Revit.DB.Visual.AdvancedMetal.SurfaceAlbedo },
{ "PrismWoodSchema", Autodesk.Revit.DB.Visual.AdvancedWood.WoodCurlyDistortionMap },
{ "HardwoodSchema", Autodesk.Revit.DB.Visual.Hardwood.HardwoodColor },
{ "PrismMasonryCMUSchema", Autodesk.Revit.DB.Visual.MasonryCMU.MasonryCMUPatternMap },
{ "PrismOpaqueSchema", Autodesk.Revit.DB.Visual.AdvancedOpaque.OpaqueAlbedo }
};

public static Asset GetDiffuseBitmap(Asset theAsset, string baseSchema)
{
if (theAsset == null || string.IsNullOrEmpty(baseSchema))
return null;

if (!DiffusePropertyMap.TryGetValue(baseSchema, out string diffusePropertyName))
return null;

var prop = theAsset.FindByName(diffusePropertyName);
if (prop?.NumberOfConnectedProperties > 0)
{
return prop.GetSingleConnectedAsset();
}

return null;
}

public static Asset GetDiffuseBitmap(Asset theAsset)
private static readonly Dictionary<string, string> ColorPropertyMap = new Dictionary<string, string>
{
foreach (var name in DIFFUSE_NAMES)
{ "PrismGenericSchema", Generic.GenericDiffuse },
{ "ConcreteSchema", Concrete.ConcreteColor },
{ "WallPaintSchema", WallPaint.WallpaintColor },
{ "PlasticVinylSchema", PlasticVinyl.PlasticvinylColor },
{ "MetallicPaintSchema", MetallicPaint.MetallicpaintBaseColor },
{ "CeramicSchema", Ceramic.CeramicColor },
{ "MetalSchema", Metal.MetalColor },
{ "PrismMetalSchema", AdvancedMetal.MetalF0 },
{ "PrismOpaqueSchema", AdvancedOpaque.OpaqueAlbedo },
{ "PrismLayeredSchema", AdvancedLayered.LayeredDiffuse },
{ "GenericSchema", Generic.GenericDiffuse },
{ "AdvancedMetalSchema", AdvancedMetal.SurfaceAlbedo }
};

public static Autodesk.Revit.DB.Color GetAppearanceColor(Asset theAsset, string baseSchema)
{
if (theAsset == null || string.IsNullOrEmpty(baseSchema))
return null;

if (!ColorPropertyMap.TryGetValue(baseSchema, out string colorPropertyName))
return null;

var colorProperty = theAsset.FindByName(colorPropertyName) as AssetPropertyDoubleArray4d;

if (colorProperty != null)
{
var prop = theAsset.FindByName(name);
if (prop?.NumberOfConnectedProperties > 0)
{
var connected = prop.GetSingleConnectedAsset();
if (connected != null)
return connected;
}
IList<double> colorValues = colorProperty.GetValueAsDoubles();

return new Autodesk.Revit.DB.Color(
(byte)(colorValues[0] * 255.0),
(byte)(colorValues[1] * 255.0),
(byte)(colorValues[2] * 255.0)
);
}

return null;
Expand Down Expand Up @@ -67,25 +111,6 @@ public static string GetTexturePath(Asset connectedAsset)
return null;
}

public static Autodesk.Revit.DB.Color GetAppearenceColor(Asset theAsset)
{
Autodesk.Revit.DB.Color appearenceColor = Autodesk.Revit.DB.Color.InvalidColorValue;

AssetPropertyDoubleArray4d colorProperty =
theAsset.FindByName(Generic.GenericDiffuse) as AssetPropertyDoubleArray4d;

if (colorProperty != null)
{
var colour = colorProperty.GetValueAsDoubles();
appearenceColor = new Autodesk.Revit.DB.Color(
(byte)(colour[0] * 255.0),
(byte)(colour[1] * 255.0),
(byte)(colour[2] * 255.0));
}

return appearenceColor;
}

public static float GetRotationRadians(Asset connectedAsset)
{
AssetPropertyDouble rotation =
Expand Down
88 changes: 50 additions & 38 deletions Common_glTF_Exporter/Materials/MaterialProperties.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Xml.Linq;
using Autodesk.Revit.DB;
using Common_glTF_Exporter.Core;

Expand All @@ -24,60 +26,70 @@ public static void SetMaterialColour(MaterialNode node,
{
if (gl_mat.EmbeddedTexturePath == null)
{
float sr = node.Color.Red / 255f;
float sg = node.Color.Green / 255f;
float sb = node.Color.Blue / 255f;
(float, float, float) baseColours;

if (gl_mat.TintColour == null)
if (gl_mat.BaseColor == null)
{
// A linear conversion is needed to reflect the real colour
float lr = SrgbToLinear(sr);
float lg = SrgbToLinear(sg);
float lb = SrgbToLinear(sb);

pbr.baseColorFactor = new List<float>(4)
{
lr,
lg,
lb,
opacity
};
baseColours = RgbToUnit(node.Color);
}
else
{
// Apply tint by multiplying base color by tint color
float tr = gl_mat.TintColour.Red / 255f;
float tg = gl_mat.TintColour.Green / 255f;
float tb = gl_mat.TintColour.Blue / 255f;
baseColours = RgbToUnit(gl_mat.BaseColor);
}

// Convert to linear space after applying tint
float lr = SrgbToLinear(sr * tr);
float lg = SrgbToLinear(sg * tg);
float lb = SrgbToLinear(sb * tb);
if (gl_mat.TintColour == null)
{
pbr.baseColorFactor = GetLinearColour(baseColours, opacity);
}
else
{
(float, float, float) baseTintColour = RgbToUnit(gl_mat.TintColour);
(float, float, float) blendColour = BlendColour(baseColours, baseTintColour);

pbr.baseColorFactor = new List<float>(4)
{
lr,
lg,
lb,
opacity
};
pbr.baseColorFactor = GetLinearColour(blendColour, opacity);
}
}
else
{
gl_mat.pbrMetallicRoughness.baseColorFactor = new List<float>(4)
{
1,
1,
1,
opacity
};
gl_mat.pbrMetallicRoughness.baseColorFactor = GetDefaultColour(opacity);
}

gl_mat.pbrMetallicRoughness = pbr;
}

public static List<float> GetDefaultColour(float opacity)
{
return new List<float>(4) { 1, 1, 1, opacity };
}

public static (float, float, float) BlendColour((float, float, float) colourA,
(float, float, float) colourB)
{
float lr = colourA.Item1 * colourB.Item1;
float lg = colourA.Item2 * colourB.Item2;
float lb = colourA.Item3 * colourB.Item3;

return (lr, lg, lb);
}

public static List<float> GetLinearColour((float, float, float) baseColours, float opacity)
{
float lr = SrgbToLinear(baseColours.Item1);
float lg = SrgbToLinear(baseColours.Item2);
float lb = SrgbToLinear(baseColours.Item3);

return new List<float>(4){ lr, lg, lb, opacity};
}

public static (float, float, float) RgbToUnit(Autodesk.Revit.DB.Color color)
{
float sr = color.Red / 255f;
float sg = color.Green / 255f;
float sb = color.Blue / 255f;

return (sr, sg, sb);

}

public static float SrgbToLinear(float srgb)
{
Expand Down
17 changes: 14 additions & 3 deletions Common_glTF_Exporter/Materials/MaterialTextures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Material = Autodesk.Revit.DB.Material;
using System;
using Common_glTF_Exporter.Utils;
using System.Diagnostics;

namespace Common_glTF_Exporter.Materials
{
Expand All @@ -36,15 +37,26 @@ public static GLTFMaterial SetMaterialTextures(Material material, GLTFMaterial g
}

Asset theAsset = appearanceElem.GetRenderingAsset();
AssetPropertyString baseSchema = theAsset.FindByName("BaseSchema") as AssetPropertyString;
if (baseSchema == null)
{
return gl_mat;
}

Asset connectedAsset = AssetPropertiesUtils.GetDiffuseBitmap(theAsset);
string schemaName = baseSchema.Value;
Asset connectedAsset = AssetPropertiesUtils.GetDiffuseBitmap(theAsset, schemaName);
string texturePath = AssetPropertiesUtils.GetTexturePath(connectedAsset);
gl_mat.TintColour = AssetPropertiesUtils.GetTint(theAsset);


if (!string.IsNullOrEmpty(texturePath) && File.Exists(texturePath))
{
SetTextureProperties(gl_mat, texturePath, connectedAsset, theAsset, opacity);
}
else
{
gl_mat.BaseColor = AssetPropertiesUtils.GetAppearanceColor(theAsset, schemaName);
}

return gl_mat;
}
Expand All @@ -60,8 +72,7 @@ private static void SetTextureProperties(GLTFMaterial gl_mat, string texturePath
float offsetY = AssetPropertiesUtils.GetOffset(connectedAsset, UnifiedBitmap.TextureRealWorldOffsetY);
float rotation = AssetPropertiesUtils.GetRotationRadians(connectedAsset);

gl_mat.Fadevalue = AssetPropertiesUtils.GetFade(theAsset);
gl_mat.BaseColor = AssetPropertiesUtils.GetAppearenceColor(theAsset);
gl_mat.Fadevalue = AssetPropertiesUtils.GetFade(theAsset);

float[] gltfScale = new float[] { 1f / scaleX, 1f / scaleY };
float[] gltfOffset = new float[]
Expand Down
Loading
Loading