Skip to content

Commit

Permalink
Merge pull request KhronosGroup#1852 from KhronosGroup/fix_1810_new_m…
Browse files Browse the repository at this point in the history
…irror_texture_option

Fix KhronosGroup#1810 - New mirror texture option in Blender 3.5
  • Loading branch information
julienduroure authored Feb 11, 2023
2 parents a38cc49 + 67efef8 commit 80e53bb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion addons/io_scene_gltf2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'name': 'glTF 2.0 format',
'author': 'Julien Duroure, Scurest, Norbert Nopper, Urs Hanselmann, Moritz Becher, Benjamin Schmithüsen, Jim Eckerlein, and many external contributors',
"version": (3, 5, 26),
'blender': (3, 4, 0),
'blender': (3, 5, 0),
'location': 'File > Import-Export',
'description': 'Import-Export as glTF 2.0',
'warning': '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,16 @@ def __gather_wrap(blender_shader_node, export_settings):
elif blender_shader_node.extension == 'CLIP':
# Not possible in glTF, but ClampToEdge is closest
wrap_s = TextureWrap.ClampToEdge
elif blender_shader_node.extension == 'MIRROR':
wrap_s = TextureWrap.MirroredRepeat
else:
wrap_s = TextureWrap.Repeat
wrap_t = wrap_s

# Starting Blender 3.5, MIRROR is now an extension of image node
# So this manual uv wrapping trick is no more usefull for MIRROR x MIRROR
# But still works for old files
# Still needed for heterogen heterogeneous sampler, like MIRROR x REPEAT, for example
# Take manual wrapping into account
result = detect_manual_uv_wrapping(blender_shader_node)
if result:
Expand Down
8 changes: 5 additions & 3 deletions addons/io_scene_gltf2/blender/imp/gltf2_blender_texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ def texture(
wrap_s = TextureWrap.Repeat
if wrap_t is None:
wrap_t = TextureWrap.Repeat
# If wrapping is REPEATxREPEAT or CLAMPxCLAMP, just set tex_img.extension
if (wrap_s, wrap_t) == (TextureWrap.Repeat, TextureWrap.Repeat):
# If wrapping is the same in both directions, just set tex_img.extension
if wrap_s == wrap_t == TextureWrap.Repeat:
tex_img.extension = 'REPEAT'
elif (wrap_s, wrap_t) == (TextureWrap.ClampToEdge, TextureWrap.ClampToEdge):
elif wrap_s == wrap_t == TextureWrap.ClampToEdge:
tex_img.extension = 'EXTEND'
elif wrap_s == wrap_t == TextureWrap.MirroredRepeat:
tex_img.extension = 'MIRROR'
else:
# Otherwise separate the UV components and use math nodes to compute
# the wrapped UV coordinates
Expand Down
Binary file added tests/scenes/09_tex_wrapping_mirror.blend
Binary file not shown.
39 changes: 26 additions & 13 deletions tests/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,19 @@ describe('Exporter', function() {
assert.equalEpsilon(transform.offset[1], 0.2272593289624576);
});

it('export mirror wrapping', function() {
let gltfPath = path.resolve(outDirPath, '09_tex_wrapping_mirror.gltf');
const asset = JSON.parse(fs.readFileSync(gltfPath));

const materials = asset.materials;
assert.deepStrictEqual(materials.length, 2);
assert.deepStrictEqual(asset.samplers.length, 1);

const samp = asset.samplers[0];
assert.deepStrictEqual(samp.wrapS, 33648); // MIRRORED_REPEAT
assert.deepStrictEqual(samp.wrapT, 33648); // MIRRORED_REPEAT
});

it('exports custom normals', function() {
let gltfPath = path.resolve(outDirPath, '10_custom_normals.gltf');
const asset = JSON.parse(fs.readFileSync(gltfPath));
Expand Down Expand Up @@ -801,7 +814,7 @@ describe('Exporter', function() {

const mat_volume = asset.materials.find(mat => mat.name === 'Volume');
assert.ok("KHR_materials_volume" in mat_volume.extensions);

const mat_thickness_zero = asset.materials.find(mat => mat.name === 'ThicknessZero');
assert.ok(!("KHR_materials_volume" in mat_thickness_zero.extensions));

Expand All @@ -816,7 +829,7 @@ describe('Exporter', function() {

const ior_2_no_transmission = asset.materials.find(mat => mat.name === 'ior_2_no_transmission');
assert.strictEqual(ior_2_no_transmission.extensions, undefined);

const ior_2 = asset.materials.find(mat => mat.name === 'ior_2');
assert.ok("KHR_materials_ior" in ior_2.extensions);

Expand All @@ -834,7 +847,7 @@ describe('Exporter', function() {

const variant_blue_index = asset.extensions['KHR_materials_variants']['variants'].findIndex(variant => variant.name === "Variant_Blue");
const variant_red_index = asset.extensions['KHR_materials_variants']['variants'].findIndex(variant => variant.name === "Variant_Red");


const mat_blue_index = asset.materials.findIndex(mat => mat.name === "Blue");
const mat_green_index = asset.materials.findIndex(mat => mat.name === "Green");
Expand Down Expand Up @@ -993,7 +1006,7 @@ describe('Exporter', function() {
const _vertex_vector_data = getAccessorData(gltfPath, asset, primitive.attributes._VERTEX_VECTOR, bufferCache);
assert.strictEqual(_vertex_vector.count, 24);
const expected_vertex_vector = [0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 6.0, 7.0, 8.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 15.0, 16.0, 17.0, 15.0, 16.0, 17.0,
9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 15.0, 16.0, 17.0, 15.0, 16.0, 17.0,
18.0, 19.0, 20.0, 18.0, 19.0, 20.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 21.0, 22.0, 23.0, 21.0, 22.0, 23.0];
assert.deepStrictEqual(_vertex_vector_data, expected_vertex_vector);

Expand Down Expand Up @@ -1708,7 +1721,7 @@ describe('Importer / Exporter (Roundtrip)', function() {

const transmissionFactor = asset.materials.filter(m => m.name === "transmissionFactor")[0];
assert.equalEpsilon(transmissionFactor.extensions["KHR_materials_transmission"]["transmissionFactor"], 0.8);

const transmissionTexture = asset.materials.filter(m => m.name === "transmissionTexture")[0];
assert.equalEpsilon(transmissionTexture.extensions["KHR_materials_transmission"]["transmissionFactor"], 1.0);
assert.ok(transmissionTexture.extensions["KHR_materials_transmission"]["transmissionTexture"]["index"] <= 1);
Expand Down Expand Up @@ -1812,15 +1825,15 @@ describe('Importer / Exporter (Roundtrip)', function() {

const ior_2_no_transmission = asset.materials.find(mat => mat.name === 'ior_2_no_transmission');
assert.strictEqual(ior_2_no_transmission.extensions, undefined);

const ior_2 = asset.materials.find(mat => mat.name === 'ior_2');
assert.ok("KHR_materials_ior" in ior_2.extensions);

const ior_145 = asset.materials.find(mat => mat.name === 'ior_1.45');
assert.ok("KHR_materials_ior" in ior_145.extensions);

const ior_15 = asset.materials.find(mat => mat.name === 'ior_1.5');
assert.ok(!("KHR_materials_ior" in ior_15.extensions));
assert.ok(!("KHR_materials_ior" in ior_15.extensions));

});

Expand All @@ -1832,7 +1845,7 @@ describe('Importer / Exporter (Roundtrip)', function() {

const variant_blue_index = asset.extensions['KHR_materials_variants']['variants'].findIndex(variant => variant.name === "Variant_Blue");
const variant_red_index = asset.extensions['KHR_materials_variants']['variants'].findIndex(variant => variant.name === "Variant_Red");


const mat_blue_index = asset.materials.findIndex(mat => mat.name === "Blue");
const mat_green_index = asset.materials.findIndex(mat => mat.name === "Green");
Expand Down Expand Up @@ -1970,10 +1983,10 @@ describe('Importer / Exporter (Roundtrip)', function() {
assert.ok("specularTexture" in mat_SpecTex.extensions['KHR_materials_specular']);
assert.ok(!("specularColorTexture" in mat_SpecTex.extensions['KHR_materials_specular']));


assert.equalEpsilon(mat_SpecTexFac.extensions['KHR_materials_specular']['specularFactor'], 0.75);
assert.ok("specularTexture" in mat_SpecTexFac.extensions['KHR_materials_specular']);
assert.ok(!("specularColorTexture" in mat_SpecTexFac.extensions['KHR_materials_specular']));
assert.ok(!("specularColorTexture" in mat_SpecTexFac.extensions['KHR_materials_specular']));

if ('specularFactor' in mat_SpecColorTex.extensions['KHR_materials_specular']) {
assert.equalEpsilon(mat_SpecColorTex.extensions['KHR_materials_specular']['specularFactor'], 1.0);
Expand Down Expand Up @@ -2058,7 +2071,7 @@ describe('Importer / Exporter (Roundtrip)', function() {
assert.ok(!("specularTexture" in mat_SpecTex.extensions['KHR_materials_specular']));
assert.ok(!("specularColorTexture" in mat_SpecTex.extensions['KHR_materials_specular']));


if ('specularFactor' in mat_SpecTexFac.extensions['KHR_materials_specular']) {
assert.equalEpsilon(mat_SpecTexFac.extensions['KHR_materials_specular']['specularFactor'], 1.0);
} else {
Expand All @@ -2084,9 +2097,9 @@ describe('Importer / Exporter (Roundtrip)', function() {
}
assert.equalEpsilonArray(mat_SpecColorTexFac.extensions['KHR_materials_specular']['specularColorFactor'], [0.0, 0.0, 0.0]);
assert.ok(!("specularTexture" in mat_SpecColorTexFac.extensions['KHR_materials_specular']));
assert.ok(!("specularColorTexture" in mat_SpecColorTexFac.extensions['KHR_materials_specular']));
assert.ok(!("specularColorTexture" in mat_SpecColorTexFac.extensions['KHR_materials_specular']));

});
});

it('roundtrips factors', function() {
let dir = '22_factors';
Expand Down

0 comments on commit 80e53bb

Please sign in to comment.