|
| 1 | +""" |
| 2 | +Test for PBR material concatenation bug fix. |
| 3 | +
|
| 4 | +Tests that concatenating meshes with PBR materials preserves |
| 5 | +metallicFactor and roughnessFactor values and creates correct |
| 6 | +UV mappings for textures. |
| 7 | +""" |
| 8 | + |
| 9 | +try: |
| 10 | + from . import generic as g |
| 11 | +except BaseException: |
| 12 | + import generic as g |
| 13 | + |
| 14 | + |
| 15 | +class PBRConcatenateTest(g.unittest.TestCase): |
| 16 | + def test_pbr_factors_preserved(self): |
| 17 | + """Test that metallicFactor and roughnessFactor are preserved when |
| 18 | + concatenating meshes with PBR materials. |
| 19 | +
|
| 20 | + This tests the fix for the bug where these factors became None after |
| 21 | + concatenation, causing meshes to appear shiny/metallic. |
| 22 | + """ |
| 23 | + # Create two boxes with PBR materials |
| 24 | + box1 = g.trimesh.creation.box(extents=[1, 1, 1]) |
| 25 | + mat1 = g.trimesh.visual.material.PBRMaterial( |
| 26 | + baseColorFactor=[255, 51, 51, 255], # Red |
| 27 | + metallicFactor=0.0, |
| 28 | + roughnessFactor=1.0, |
| 29 | + ) |
| 30 | + box1.visual = g.trimesh.visual.TextureVisuals(material=mat1) |
| 31 | + |
| 32 | + box2 = g.trimesh.creation.box(extents=[1, 1, 1]) |
| 33 | + mat2 = g.trimesh.visual.material.PBRMaterial( |
| 34 | + baseColorFactor=[51, 255, 51, 255], # Green |
| 35 | + metallicFactor=0.0, |
| 36 | + roughnessFactor=1.0, |
| 37 | + ) |
| 38 | + box2.visual = g.trimesh.visual.TextureVisuals(material=mat2) |
| 39 | + box2.apply_translation([1.5, 0, 0]) |
| 40 | + |
| 41 | + # Concatenate the meshes |
| 42 | + merged = g.trimesh.util.concatenate([box1, box2]) |
| 43 | + |
| 44 | + # Factor are `None` because textures are created for metallicFactor and |
| 45 | + # roughnessFactor |
| 46 | + assert merged.visual.material.metallicFactor is None |
| 47 | + assert merged.visual.material.roughnessFactor is None |
| 48 | + |
| 49 | + # Check that textures were created |
| 50 | + assert merged.visual.material.baseColorTexture is not None |
| 51 | + assert merged.visual.material.metallicRoughnessTexture is not None |
| 52 | + |
| 53 | + def test_pbr_uv_mapping(self): |
| 54 | + """Test that UV coordinates correctly map to material values in the |
| 55 | + packed textures after concatenation. |
| 56 | + """ |
| 57 | + # Create two boxes with different colors but same PBR values |
| 58 | + box1 = g.trimesh.creation.box(extents=[0.5, 0.5, 0.5]) |
| 59 | + mat1 = g.trimesh.visual.material.PBRMaterial( |
| 60 | + baseColorFactor=[255, 0, 0, 255], |
| 61 | + metallicFactor=0.0, |
| 62 | + roughnessFactor=1.0, |
| 63 | + ) |
| 64 | + box1.visual = g.trimesh.visual.TextureVisuals(material=mat1) |
| 65 | + |
| 66 | + box2 = g.trimesh.creation.box(extents=[0.5, 0.5, 0.5]) |
| 67 | + mat2 = g.trimesh.visual.material.PBRMaterial( |
| 68 | + baseColorFactor=[0, 255, 0, 255], |
| 69 | + metallicFactor=0.0, |
| 70 | + roughnessFactor=1.0, |
| 71 | + ) |
| 72 | + box2.visual = g.trimesh.visual.TextureVisuals(material=mat2) |
| 73 | + box2.apply_translation([1, 0, 0]) |
| 74 | + |
| 75 | + # Concatenate |
| 76 | + merged = g.trimesh.util.concatenate([box1, box2]) |
| 77 | + |
| 78 | + # Get the metallicRoughness texture |
| 79 | + tex = g.np.array(merged.visual.material.metallicRoughnessTexture) |
| 80 | + |
| 81 | + # Check that all UV coordinates map to the correct material values |
| 82 | + # In glTF: Blue channel = metallic, Green channel = roughness |
| 83 | + for u, v in merged.visual.uv: |
| 84 | + # Convert UV to pixel coordinates |
| 85 | + px = round(u * (tex.shape[1] - 1)) |
| 86 | + py = round((1 - v) * (tex.shape[0] - 1)) |
| 87 | + |
| 88 | + if 0 <= px < tex.shape[1] and 0 <= py < tex.shape[0]: |
| 89 | + roughness = tex[py, px, 1] # Green channel |
| 90 | + metallic = tex[py, px, 2] # Blue channel |
| 91 | + |
| 92 | + # All vertices should map to roughness=255 (1.0) and metallic=0 (0.0) |
| 93 | + assert roughness == 255, ( |
| 94 | + f"UV ({u:.3f}, {v:.3f}) maps to wrong roughness: {roughness}" |
| 95 | + ) |
| 96 | + assert metallic == 0, ( |
| 97 | + f"UV ({u:.3f}, {v:.3f}) maps to wrong metallic: {metallic}" |
| 98 | + ) |
| 99 | + |
| 100 | + def test_pbr_identical_materials(self): |
| 101 | + """Test that concatenating meshes with identical PBR materials |
| 102 | + preserves the scalar values without creating textures.""" |
| 103 | + # Create two boxes with identical materials |
| 104 | + box1 = g.trimesh.creation.box(extents=[0.5, 0.5, 0.5]) |
| 105 | + mat1 = g.trimesh.visual.material.PBRMaterial( |
| 106 | + baseColorFactor=[128, 128, 128, 255], |
| 107 | + metallicFactor=0.5, |
| 108 | + roughnessFactor=0.7, |
| 109 | + ) |
| 110 | + box1.visual = g.trimesh.visual.TextureVisuals(material=mat1) |
| 111 | + |
| 112 | + box2 = g.trimesh.creation.box(extents=[0.5, 0.5, 0.5]) |
| 113 | + mat2 = g.trimesh.visual.material.PBRMaterial( |
| 114 | + baseColorFactor=[128, 128, 128, 255], # Same color |
| 115 | + metallicFactor=0.5, # Same metallic |
| 116 | + roughnessFactor=0.7, # Same roughness |
| 117 | + ) |
| 118 | + box2.visual = g.trimesh.visual.TextureVisuals(material=mat2) |
| 119 | + box2.apply_translation([1, 0, 0]) |
| 120 | + |
| 121 | + # Concatenate |
| 122 | + merged = g.trimesh.util.concatenate([box1, box2]) |
| 123 | + |
| 124 | + # When materials are identical, scalar values should be preserved |
| 125 | + assert merged.visual.material.metallicFactor == 0.5 |
| 126 | + assert merged.visual.material.roughnessFactor == 0.7 |
| 127 | + |
| 128 | + # No metallicRoughness texture should be created for identical materials |
| 129 | + assert merged.visual.material.metallicRoughnessTexture is None |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + g.trimesh.util.attach_to_log() |
| 134 | + g.unittest.main() |
0 commit comments