Skip to content

Commit c4d346a

Browse files
authored
Merge pull request #113 from EverseDevelopment/FixMissingTextures
Fix missing textures
2 parents 91dd261 + e8aa349 commit c4d346a

File tree

18 files changed

+296
-314
lines changed

18 files changed

+296
-314
lines changed

Common_glTF_Exporter/Common_glTF_Exporter.projitems

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<Compile Include="$(MSBuildThisFileDirectory)Materials\AssetProperties.cs" />
4141
<Compile Include="$(MSBuildThisFileDirectory)Materials\BitmapsUtils.cs" />
4242
<Compile Include="$(MSBuildThisFileDirectory)Materials\MaterialProperties.cs" />
43+
<Compile Include="$(MSBuildThisFileDirectory)Materials\MaterialTextures.cs" />
4344
<Compile Include="$(MSBuildThisFileDirectory)Model\GlbComponents.cs" />
4445
<Compile Include="$(MSBuildThisFileDirectory)Model\Links.cs" />
4546
<Compile Include="$(MSBuildThisFileDirectory)Model\LoadContext.cs" />
@@ -69,9 +70,6 @@
6970
<Compile Include="$(MSBuildThisFileDirectory)Utils\GLTFBinaryDataUtils.cs" />
7071
<Compile Include="$(MSBuildThisFileDirectory)Utils\SettingsConfig.cs" />
7172
<Compile Include="$(MSBuildThisFileDirectory)Utils\Theme.cs" />
72-
<Compile Include="$(MSBuildThisFileDirectory)UVs\CylindricalUv.cs" />
73-
<Compile Include="$(MSBuildThisFileDirectory)UVs\PlanarUv.cs" />
74-
<Compile Include="$(MSBuildThisFileDirectory)UVs\VertexUvs.cs" />
7573
<Compile Include="$(MSBuildThisFileDirectory)Version\DownloadFile.cs" />
7674
<Compile Include="$(MSBuildThisFileDirectory)Version\InternetConnection.cs" />
7775
<Compile Include="$(MSBuildThisFileDirectory)Version\LatestVersion.cs" />

Common_glTF_Exporter/Core/GLTFMaterial.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class GLTFMaterial
3333
[JsonIgnore]
3434
public double Fadevalue { get; set; } = 1;
3535

36+
[JsonIgnore]
37+
public Autodesk.Revit.DB.Color TintColour { get; set; }
38+
3639
[JsonIgnore]
3740
public Autodesk.Revit.DB.Color BaseColor { get; set; }
3841

Common_glTF_Exporter/Core/GlTFExportContext.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Common_glTF_Exporter.EportUtils;
1212
using System.Windows.Media.Media3D;
1313
using System.Windows.Controls;
14-
using Common_glTF_Exporter.UVs;
1514

1615
namespace Common_glTF_Exporter.Core
1716
{
@@ -277,7 +276,7 @@ public void OnMaterial(MaterialNode node)
277276
}
278277
else
279278
{
280-
currentMaterial = RevitMaterials.Export(node, ref materials, preferences, currentDocument);
279+
currentMaterial = RevitMaterials.Export(node, preferences, currentDocument);
281280
}
282281

283282
materials.AddOrUpdateCurrentMaterial(currentMaterial.UniqueId, currentMaterial, false);
@@ -298,6 +297,7 @@ public void OnPolymesh(PolymeshTopology polymesh)
298297
var geomItem = currentGeometry.CurrentItem;
299298
var vertItem = currentVertices.CurrentItem;
300299

300+
IList<UV> uvs = polymesh.GetUVs();
301301
IList<XYZ> pts = polymesh.GetPoints();
302302
for (int i = 0; i < pts.Count; i++)
303303
{
@@ -313,7 +313,11 @@ public void OnPolymesh(PolymeshTopology polymesh)
313313
new PointIntObject(vertex), geomItem.Vertices);
314314
geomItem.Faces.Add(vertexIndex);
315315

316-
VertexUvs.AddUvToVertex(vertex, geomItem, currentMaterial, preferences, currentFace);
316+
if (preferences.materials == MaterialsEnum.textures && currentMaterial?.EmbeddedTexturePath != null)
317+
{
318+
UV uv = uvs[index];
319+
geomItem.Uvs.Add(uv);
320+
}
317321
}
318322
}
319323

Common_glTF_Exporter/Materials/AssetProperties.cs

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using Autodesk.Revit.DB;
22
using Autodesk.Revit.DB.Visual;
33
using System;
4+
using System.Collections.Generic;
45
using System.IO;
56

67
namespace Common_glTF_Exporter.Materials
78
{
89
public static class AssetPropertiesUtils
910
{
10-
private static readonly string[] DIFFUSE_NAMES = { "opaque_albedo", "generic_diffuse" };
11-
private const string PATHPROPERTY = "unifiedbitmap_Bitmap";
11+
private static readonly string[] DIFFUSE_NAMES = {
12+
Autodesk.Revit.DB.Visual.AdvancedOpaque.OpaqueAlbedo,
13+
Autodesk.Revit.DB.Visual.Generic.GenericDiffuse,
14+
Autodesk.Revit.DB.Visual.AdvancedWood.WoodCurlyDistortionMap,
15+
Autodesk.Revit.DB.Visual.Hardwood.HardwoodColor,
16+
Autodesk.Revit.DB.Visual.AdvancedMetal.SurfaceAlbedo
17+
};
1218
private const string AUTODESKPATHTEXTURES = @"Autodesk Shared\Materials\Textures\";
13-
private const string ROTATIONPROPERTY = "texture_WAngle";
14-
private const string DIFUSSEDEFINITION = "generic_diffuse";
1519

1620
public static Asset GetDiffuseBitmap(Asset theAsset)
1721
{
@@ -33,7 +37,7 @@ public static string GetTexturePath(Asset connectedAsset)
3337
{
3438
if (connectedAsset != null)
3539
{
36-
var bitmapPathProp = connectedAsset.FindByName(PATHPROPERTY) as AssetPropertyString;
40+
var bitmapPathProp = connectedAsset.FindByName(UnifiedBitmap.UnifiedbitmapBitmap) as AssetPropertyString;
3741

3842
if (bitmapPathProp != null && !string.IsNullOrEmpty(bitmapPathProp.Value))
3943
{
@@ -59,7 +63,7 @@ public static Autodesk.Revit.DB.Color GetAppearenceColor(Asset theAsset)
5963
Autodesk.Revit.DB.Color appearenceColor = Autodesk.Revit.DB.Color.InvalidColorValue;
6064

6165
AssetPropertyDoubleArray4d colorProperty =
62-
theAsset.FindByName(DIFUSSEDEFINITION) as AssetPropertyDoubleArray4d;
66+
theAsset.FindByName(Generic.GenericDiffuse) as AssetPropertyDoubleArray4d;
6367

6468
if (colorProperty != null)
6569
{
@@ -75,7 +79,8 @@ public static Autodesk.Revit.DB.Color GetAppearenceColor(Asset theAsset)
7579

7680
public static float GetRotationRadians(Asset connectedAsset)
7781
{
78-
AssetPropertyDouble rotation = connectedAsset.FindByName(ROTATIONPROPERTY) as AssetPropertyDouble;
82+
AssetPropertyDouble rotation =
83+
connectedAsset.FindByName(UnifiedBitmap.TextureWAngle) as AssetPropertyDouble;
7984

8085
if (rotation != null)
8186
{
@@ -87,7 +92,8 @@ public static float GetRotationRadians(Asset connectedAsset)
8792

8893
public static float GetScale(Asset connectedAsset, string textureName)
8994
{
90-
AssetPropertyDistance scale = connectedAsset.FindByName(textureName) as AssetPropertyDistance;
95+
AssetPropertyDistance scale =
96+
connectedAsset.FindByName(textureName) as AssetPropertyDistance;
9197

9298
if (scale != null)
9399
{
@@ -103,5 +109,45 @@ public static float GetScale(Asset connectedAsset, string textureName)
103109

104110
return 1;
105111
}
112+
113+
public static Autodesk.Revit.DB.Color GetTint(Asset asset)
114+
{
115+
bool tintOn = true;
116+
117+
AssetProperty tintEnabledProp = asset.FindByName(Generic.CommonTintToggle);
118+
if (tintEnabledProp is AssetPropertyBoolean apb)
119+
{
120+
tintOn = apb.Value;
121+
}
122+
123+
if (tintOn)
124+
{
125+
AssetProperty tintProp = asset.FindByName(Generic.CommonTintColor);
126+
if (tintProp is AssetPropertyDoubleArray4d tintArray4d)
127+
{
128+
IList<double> rgba = tintArray4d.GetValueAsDoubles();
129+
130+
byte r = (byte)(rgba[0] * 255.0);
131+
byte g = (byte)(rgba[1] * 255.0);
132+
byte b = (byte)(rgba[2] * 255.0);
133+
134+
return new Autodesk.Revit.DB.Color(r, g, b);
135+
}
136+
}
137+
138+
return null;
139+
}
140+
141+
public static double GetFade(Asset asset)
142+
{
143+
AssetPropertyDouble fadeProp = asset.FindByName(Generic.GenericDiffuseImageFade) as AssetPropertyDouble;
144+
145+
if (fadeProp != null)
146+
{
147+
return fadeProp.Value;
148+
}
149+
150+
return 1;
151+
}
106152
}
107153
}

Common_glTF_Exporter/Materials/BitmapsUtils.cs

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,91 @@ public static (string, ImageFormat) GetMimeType(string path)
3030
}
3131

3232
public static byte[] BlendImageWithColor(
33-
byte[] imageBytes,
34-
double fade,
35-
Autodesk.Revit.DB.Color flatColor,
36-
ImageFormat mimeType)
33+
byte[] imageBytes,
34+
double fade,
35+
Autodesk.Revit.DB.Color flatColor,
36+
ImageFormat mimeType,
37+
Autodesk.Revit.DB.Color tintColor)
3738
{
38-
if (fade >= 1.0)
39-
return imageBytes;
39+
bool noFlatBlend = fade >= 1.0 || flatColor == null;
40+
if (noFlatBlend && tintColor == null)
41+
return imageBytes;
4042

41-
float fFade = Clamp((float)fade, 0f, 1f);
42-
float fInv = 1f - fFade;
4343

44-
float lr = SrgbToLinear(flatColor.Red / 255f);
45-
float lg = SrgbToLinear(flatColor.Green / 255f);
46-
float lb = SrgbToLinear(flatColor.Blue / 255f);
44+
float fFade = noFlatBlend ? 1f
45+
: Clamp((float)fade, 0f, 1f);
46+
float fInv = 1f - fFade; // 0 if no blend
4747

48-
byte[] resultBytes;
48+
// face colour in linear space (0 if unused)
49+
float lrFlat = 0f, lgFlat = 0f, lbFlat = 0f;
50+
if (!noFlatBlend)
51+
{
52+
lrFlat = SrgbToLinear(flatColor.Red / 255f);
53+
lgFlat = SrgbToLinear(flatColor.Green / 255f);
54+
lbFlat = SrgbToLinear(flatColor.Blue / 255f);
55+
}
56+
57+
// tint factors (1,1,1 ⇒ no change)
58+
float lrTint = 1f, lgTint = 1f, lbTint = 1f;
59+
if (tintColor != null)
60+
{
61+
lrTint = SrgbToLinear(tintColor.Red / 255f);
62+
lgTint = SrgbToLinear(tintColor.Green / 255f);
63+
lbTint = SrgbToLinear(tintColor.Blue / 255f);
64+
65+
lrTint = Math.Min(lrTint + 0.10f, 1.0f);
66+
lgTint = Math.Min(lgTint + 0.10f, 1.0f);
67+
lbTint = Math.Min(lbTint + 0.10f, 1.0f);
68+
}
4969

50-
using (MemoryStream inputMs = new MemoryStream(imageBytes))
51-
using (Bitmap bitmap = new Bitmap(inputMs))
70+
byte[] resultBytes;
71+
using (var inputMs = new MemoryStream(imageBytes))
72+
using (var bitmap = new Bitmap(inputMs))
5273
{
5374
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
54-
BitmapData data = bitmap.LockBits(
55-
rect,
56-
ImageLockMode.ReadWrite,
57-
PixelFormat.Format32bppArgb);
75+
BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadWrite,
76+
PixelFormat.Format32bppArgb);
5877

5978
int byteCount = Math.Abs(data.Stride) * bitmap.Height;
6079
byte[] pixels = new byte[byteCount];
6180
Marshal.Copy(data.Scan0, pixels, 0, byteCount);
6281

6382
for (int i = 0; i < byteCount; i += 4)
6483
{
65-
// bytes BGRA → float 0-1
84+
//BGRA → linear
6685
float sb = pixels[i + 0] / 255f;
6786
float sg = pixels[i + 1] / 255f;
6887
float sr = pixels[i + 2] / 255f;
69-
70-
// sRGB → lineal
71-
float lbSrc = SrgbToLinear(sb);
72-
float lgSrc = SrgbToLinear(sg);
73-
float lrSrc = SrgbToLinear(sr);
74-
75-
// lineal interpolation
76-
float lbMix = lbSrc * fFade + lb * fInv;
77-
float lgMix = lgSrc * fFade + lg * fInv;
78-
float lrMix = lrSrc * fFade + lr * fInv;
79-
80-
pixels[i + 0] = (byte)(Clamp(LinearToSrgb(lbMix), 0f, 1f) * 255f + 0.5f);
81-
pixels[i + 1] = (byte)(Clamp(LinearToSrgb(lgMix), 0f, 1f) * 255f + 0.5f);
82-
pixels[i + 2] = (byte)(Clamp(LinearToSrgb(lrMix), 0f, 1f) * 255f + 0.5f);
88+
float lb = SrgbToLinear(sb);
89+
float lg = SrgbToLinear(sg);
90+
float lr = SrgbToLinear(sr);
91+
92+
// BLEND WITH FLAT COLOUR
93+
lb = lb * fFade + lbFlat * fInv;
94+
lg = lg * fFade + lgFlat * fInv;
95+
lr = lr * fFade + lrFlat * fInv;
96+
97+
//APPLY TINT
98+
lb *= lbTint;
99+
lg *= lgTint;
100+
lr *= lrTint;
101+
102+
//* linear → sRGB, write back
103+
pixels[i + 0] = (byte)(Clamp(LinearToSrgb(lb), 0f, 1f) * 255f + 0.5f);
104+
pixels[i + 1] = (byte)(Clamp(LinearToSrgb(lg), 0f, 1f) * 255f + 0.5f);
105+
pixels[i + 2] = (byte)(Clamp(LinearToSrgb(lr), 0f, 1f) * 255f + 0.5f);
106+
// alpha (pixels[i+3]) untouched
83107
}
84108

85109
Marshal.Copy(pixels, 0, data.Scan0, byteCount);
86110
bitmap.UnlockBits(data);
87111

88-
using (MemoryStream outputMs = new MemoryStream())
112+
using (var outputMs = new MemoryStream())
89113
{
90114
bitmap.Save(outputMs, mimeType);
91115
resultBytes = outputMs.ToArray();
92116
}
93117
}
94-
95118
return resultBytes;
96119
}
97120

Common_glTF_Exporter/Materials/MaterialProperties.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,6 @@ public static class MaterialProperties
1111
private const string OPAQUE = "OPAQUE";
1212
public static void SetProperties(MaterialNode node, float opacity, ref GLTFPBR pbr, ref GLTFMaterial gl_mat)
1313
{
14-
float sr = node.Color.Red / 255f;
15-
float sg = node.Color.Green / 255f;
16-
float sb = node.Color.Blue / 255f;
17-
18-
// A linear conversion is needed to reflect the real colour
19-
float lr = SrgbToLinear(sr);
20-
float lg = SrgbToLinear(sg);
21-
float lb = SrgbToLinear(sb);
22-
23-
pbr.baseColorFactor = new List<float>(4)
24-
{
25-
lr,
26-
lg,
27-
lb,
28-
opacity
29-
};
30-
3114
pbr.metallicFactor = 0f;
3215
pbr.roughnessFactor = opacity != 1 ? 0.5f : 1f;
3316
gl_mat.pbrMetallicRoughness = pbr;
@@ -36,6 +19,42 @@ public static void SetProperties(MaterialNode node, float opacity, ref GLTFPBR p
3619
gl_mat.alphaCutoff = null;
3720
}
3821

22+
public static void SetMaterialColour(MaterialNode node,
23+
float opacity, ref GLTFPBR pbr, ref GLTFMaterial gl_mat)
24+
{
25+
if (gl_mat.EmbeddedTexturePath == null)
26+
{
27+
float sr = node.Color.Red / 255f;
28+
float sg = node.Color.Green / 255f;
29+
float sb = node.Color.Blue / 255f;
30+
31+
// A linear conversion is needed to reflect the real colour
32+
float lr = SrgbToLinear(sr);
33+
float lg = SrgbToLinear(sg);
34+
float lb = SrgbToLinear(sb);
35+
36+
pbr.baseColorFactor = new List<float>(4)
37+
{
38+
lr,
39+
lg,
40+
lb,
41+
opacity
42+
};
43+
}
44+
else
45+
{
46+
gl_mat.pbrMetallicRoughness.baseColorFactor = new List<float>(4)
47+
{
48+
1,
49+
1,
50+
1,
51+
opacity
52+
};
53+
}
54+
55+
gl_mat.pbrMetallicRoughness = pbr;
56+
}
57+
3958
public static float SrgbToLinear(float srgb)
4059
{
4160
return srgb <= 0.04045f

0 commit comments

Comments
 (0)