Skip to content

Commit 2f542d4

Browse files
committed
Added transparency for wavefront export, fixes vpenades#266
1 parent 32f4936 commit 2f542d4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/SharpGLTF.Toolkit/IO/WavefrontWriter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ private static void _WriteMaterialsFile(StreamWriter sw, IEnumerable<Material> m
174174
sw.WriteLine(Invariant($"Kd {m.DiffuseColor.X} {m.DiffuseColor.Y} {m.DiffuseColor.Z}"));
175175
sw.WriteLine(Invariant($"Ks {m.SpecularColor.X} {m.SpecularColor.Y} {m.SpecularColor.Z}"));
176176

177+
if (m.Opacity != 1)
178+
{
179+
sw.WriteLine(Invariant($"d {m.Opacity}"));
180+
sw.WriteLine(Invariant($"Tr {1.0f-m.Opacity}"));
181+
}
182+
177183
if (m.DiffuseTexture.IsValid)
178184
{
179185
var imgName = imageNameByImage[m.DiffuseTexture];
@@ -307,6 +313,12 @@ private static Material GetMaterialFromTriangle(Schema2.Material srcMaterial)
307313

308314
dstMaterial.DiffuseColor = new Vector3(diffuse.X, diffuse.Y, diffuse.Z);
309315
dstMaterial.SpecularColor = new Vector3(0.2f);
316+
dstMaterial.Opacity = 1.0f;
317+
318+
if (srcMaterial.Alpha == AlphaMode.BLEND && diffuse.W < 1)
319+
{
320+
dstMaterial.Opacity = Math.Max(0, diffuse.W);
321+
}
310322

311323
dstMaterial.DiffuseTexture = srcMaterial.GetDiffuseTexture()?.PrimaryImage?.Content ?? default;
312324

@@ -322,11 +334,12 @@ public struct Material : IEquatable<Material>
322334
{
323335
public Vector3 DiffuseColor;
324336
public Vector3 SpecularColor;
337+
public float Opacity;
325338
public Memory.MemoryImage DiffuseTexture;
326339

327340
public readonly override int GetHashCode()
328341
{
329-
return DiffuseColor.GetHashCode() ^ SpecularColor.GetHashCode() ^ DiffuseTexture.GetHashCode();
342+
return DiffuseColor.GetHashCode() ^ SpecularColor.GetHashCode() ^ DiffuseTexture.GetHashCode() ^ Opacity.GetHashCode();
330343
}
331344

332345
public readonly override bool Equals(object obj) { return obj is Material other && this.Equals(other); }
@@ -335,6 +348,7 @@ public readonly bool Equals(Material other)
335348
{
336349
if (this.DiffuseColor != other.DiffuseColor) return false;
337350
if (this.SpecularColor != other.SpecularColor) return false;
351+
if (this.Opacity != other.Opacity) return false;
338352
if (this.DiffuseTexture != other.DiffuseTexture) return false;
339353
return true;
340354
}

0 commit comments

Comments
 (0)