Skip to content

Commit 5f061cb

Browse files
authored
Merge pull request #229 from Geodan/use_opaque_mode_when_basecolor_is_opaque
use opaque mode when basecolor is opaque
2 parents fb10348 + 2a1d89a commit 5f061cb

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/wkb2gltf.core.tests/ShaderTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ namespace Wkb2Gltf.Tests;
44

55
public class ShaderTests
66
{
7+
[Test]
8+
public void TestShaderOpaque()
9+
{
10+
var pbr = new PbrMetallicRoughness() {
11+
BaseColor = "#ff0000ff"
12+
};
13+
14+
Assert.That(pbr.IsBaseColorOpaque(), Is.True);
15+
16+
pbr.BaseColor = "#ff0000";
17+
Assert.That(pbr.IsBaseColorOpaque(), Is.True);
18+
19+
pbr.BaseColor = "#ff000053";
20+
Assert.That(pbr.IsBaseColorOpaque(), Is.False);
21+
22+
23+
24+
}
25+
26+
727
[Test]
828

929
public void ShaderEqualsTest()

src/wkb2gltf.core/GlbCreator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static byte[] GetGlb(List<List<Triangle>> triangles, string copyright = "
2121
var materialCache = new MaterialsCache();
2222
var shader = new Shader();
2323
shader.PbrMetallicRoughness = new PbrMetallicRoughness() { BaseColor = defaultColor, MetallicRoughness = defaultMetallicRoughness };
24+
2425
var defaultMaterial = MaterialCreator.CreateMaterial(shader, defaultDoubleSided, defaultAlphaMode);
2526

2627
var meshBatchId = new MeshBuilder<VertexPositionNormal, VertexWithBatchId, VertexEmpty>("mesh");
@@ -31,6 +32,10 @@ public static byte[] GetGlb(List<List<Triangle>> triangles, string copyright = "
3132
MaterialBuilder material;
3233

3334
if (triangle.Shader != null) {
35+
if (triangle.Shader.PbrMetallicRoughness!=null && triangle.Shader.PbrMetallicRoughness.IsBaseColorOpaque()) {
36+
defaultAlphaMode = SharpGLTF.Materials.AlphaMode.OPAQUE;
37+
}
38+
3439
material = materialCache.GetMaterialBuilderByShader(triangle.Shader, doubleSided, defaultAlphaMode);
3540
}
3641
else {

src/wkb2gltf.core/PbrMetallicRoughness.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ public override int GetHashCode()
2121
return base.GetHashCode();
2222
}
2323

24+
public bool IsBaseColorOpaque()
25+
{
26+
if (BaseColor.Length == 9) {
27+
var alpha = BaseColor.Substring(7, 2);
28+
if (alpha.ToUpper() == "FF") {
29+
return true;
30+
}
31+
return false;
32+
}
33+
return true;
34+
}
2435
}

styling.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ So Diffuse Red = 230, Diffuse Green = 0, Diffuse Blue = 128, Alpha = 0
277277
### AlphaMode
278278

279279
It is possible to specify glTF material alphaMode property (see: https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#alpha-coverage) using `--default_alpha_mode` option for all materials. By default it is set to OPAQUE. Other options are BLEND and MASK.
280-
Using non OPAQUE default alpha mode for all materials might affect rendering performance. It should be used only when most of the materials have alpha value different than 1.
280+
Using non OPAQUE default alpha mode for all materials might affect rendering performance.
281+
282+
When a color has 6 digits (like #RRGGBB) then alphaMode is set to OPAQUE for that material.
283+
284+
When a color has 8 digits (like #RRGGBBAA) and AA is equal to 255 (0xFF) then alphaMode is set to OPAQUE for that material.
281285

282286
Sample setting alpha mode to BLEND for 50% transparent polygon in Amsterdam:
283287

0 commit comments

Comments
 (0)