Skip to content

Commit

Permalink
TinyGltfImporter: implement KHR_texture_transform.
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Mar 28, 2020
1 parent e4266d5 commit 7345d78
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 9 deletions.
2 changes: 2 additions & 0 deletions doc/changelog-plugins.dox
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace Magnum {
- Support for the `KHR_lights_punctual` extension in
@ref Trade::TinyGltfImporter "TinyGltfImporter", replacing the obsolete
unuspported `KHR_lights_cmn` (see [mosra/magnum-plugins#77](https://github.com/mosra/magnum-plugins/pull/77))
- Support for the `KHR_texture_transform` extension in
@ref Trade::TinyGltfImporter "TinyGltfImporter"
- @ref Trade::DdsImporter "DdsImporter" and
@ref Trade::BasisImporter "BasisImporter", now support loading image mip
levels; @ref Trade::AssimpImporter "AssimpImporter",
Expand Down
110 changes: 107 additions & 3 deletions src/MagnumPlugins/TinyGltfImporter/Test/TinyGltfImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,29 @@ constexpr struct {
{"invalid texture index pbrSpecularGlossiness specular", "specularGlossinessTexture index 2 out of bounds for 2 textures"},
{"invalid texCoord index pbrMetallicRoughness", "multiple texture coordinate sets are not supported"},
{"invalid texCoord index pbrSpecularGlossiness diffuse", "multiple texture coordinate sets are not supported"},
{"invalid texCoord index pbrSpecularGlossiness specular", "multiple texture coordinate sets are not supported"}
{"invalid texCoord index pbrSpecularGlossiness specular", "multiple texture coordinate sets are not supported"},
{"invalid texture transform texCoord index", "transform of multiple texture coordinate sets is not supported"},
{"missing first texture transform", R"(specularGlossinessTexture has an inconsistent texture transform, expected
Matrix(1, 0, 0,
0, 1, 0,
0, 0, 1) but got
Matrix(1, 0, 0,
0, 1, -1,
0, 0, 1))"},
{"missing second texture transform", R"(specularGlossinessTexture has an inconsistent texture transform, expected
Matrix(1, 0, 0,
0, 1, -1,
0, 0, 1) but got
Matrix(1, 0, 0,
0, 1, 0,
0, 0, 1))"},
{"inconsistent texture transform", R"(specularGlossinessTexture has an inconsistent texture transform, expected
Matrix(1, 0, 0,
0, 1, -1,
0, 0, 1) but got
Matrix(0.5, 0, 0,
0, 0.5, 0.5,
0, 0, 1))"}
};

constexpr struct {
Expand Down Expand Up @@ -1841,7 +1863,7 @@ void TinyGltfImporterTest::materialPbrMetallicRoughness() {
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(TINYGLTFIMPORTER_TEST_DIR,
"material-metallicroughness.gltf")));

CORRADE_COMPARE(importer->materialCount(), 2);
CORRADE_COMPARE(importer->materialCount(), 5);
CORRADE_COMPARE(importer->materialForName("texture"), 1);
CORRADE_COMPARE(importer->materialName(1), "texture");

Expand Down Expand Up @@ -1870,15 +1892,60 @@ void TinyGltfImporterTest::materialPbrMetallicRoughness() {
CORRADE_COMPARE(phong.diffuseTexture(), 0);
CORRADE_COMPARE(phong.specularColor(), 0xffffff_rgbf);
CORRADE_COMPARE(phong.shininess(), 80.0f);
} {
const char* name = "texture transform all";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{
{0.164968f, 0.472002f, 0.0f},
{-0.472002f, 0.164968f, 0.0f},
{0.472002f, -0.164968f, 1.0f}
}));
} {
const char* name = "texture transform offset + scale";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{
{0.5f, 0.0f, 0.0f},
{0.0f, 0.5f, 0.0f},
{0.0f, -0.5f, 1.0f}
}));
} {
const char* name = "texture transform nothing";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{}));
}

/* Texture transform consistency across multiple textures of the same
materials are tested for pbrSpecularGlossiness, as this one doesn't have
more than one texture handled at the moment */
}

void TinyGltfImporterTest::materialPbrSpecularGlossiness() {
Containers::Pointer<AbstractImporter> importer = _manager.instantiate("TinyGltfImporter");
CORRADE_VERIFY(importer->openFile(Utility::Directory::join(TINYGLTFIMPORTER_TEST_DIR,
"material-specularglossiness.gltf")));

CORRADE_COMPARE(importer->materialCount(), 2);
CORRADE_COMPARE(importer->materialCount(), 5);

{
auto material = importer->material("colors");
Expand All @@ -1905,6 +1972,43 @@ void TinyGltfImporterTest::materialPbrSpecularGlossiness() {
CORRADE_COMPARE(phong.specularColor(), (Color3{0.4f, 0.5f, 0.6f}));
CORRADE_COMPARE(phong.specularTexture(), 1);
CORRADE_COMPARE(phong.shininess(), 80.0f);
} {
const char* name = "texture transform same for both textures";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::SpecularTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{
{0.164968f, 0.472002f, 0.0f},
{-0.472002f, 0.164968f, 0.0f},
{0.472002f, 0.835032f, 1.0f}
}));
} {
const char* name = "texture transform first none, second identity";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::SpecularTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{}));
} {
const char* name = "texture transform first identity, second none";
auto material = importer->material(name);
CORRADE_ITERATION(name);
CORRADE_VERIFY(material);
CORRADE_VERIFY(material->importerState());
CORRADE_COMPARE(material->type(), MaterialType::Phong);

auto& phong = static_cast<const PhongMaterialData&>(*material);
CORRADE_COMPARE(phong.flags(), PhongMaterialData::Flag::DiffuseTexture|PhongMaterialData::Flag::SpecularTexture|PhongMaterialData::Flag::TextureTransformation);
CORRADE_COMPARE(phong.textureMatrix(), (Matrix3{}));
}
}

Expand Down
73 changes: 73 additions & 0 deletions src/MagnumPlugins/TinyGltfImporter/Test/material-invalid.gltf
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,79 @@
}
}
}
},
{
"name": "invalid texture transform texCoord index",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"texCoord": 1
}
}
}
}
},
{
"name": "missing first texture transform",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0,
"texCoord": 0
},
"specularGlossinessTexture": {
"index": 1,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 1.0]
}
}
}
}
}
},
{
"name": "missing second texture transform",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 1.0]
}
}
},
"specularGlossinessTexture": {
"index": 1
}
}
}
},
{
"name": "inconsistent texture transform",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 1.0]
}
}
},
"specularGlossinessTexture": {
"index": 1,
"extensions": {
"KHR_texture_transform": {
"scale": [0.5, 0.5]
}
}
}
}
}
}
],
"textures": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@
"index": 0
}
}
},
{
"name": "texture transform all",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 1.0],
"rotation": 1.23456,
"scale": [0.5, 0.5]
}
}
}
}
},
{
"name": "texture transform offset + scale",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 1.0],
"scale": [0.5, 0.5]
}
}
}
}
},
{
"name": "texture transform nothing",
"pbrMetallicRoughness": {
"baseColorTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {}
}
}
}
}
],
"samplers": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,67 @@
}
}
}
},
{
"name": "texture transform same for both textures",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"rotation": 1.23456,
"scale": [0.5, 0.5]
}
}
},
"specularGlossinessTexture": {
"index": 1,
"extensions": {
"KHR_texture_transform": {
"rotation": 1.23456,
"scale": [0.5, 0.5]
}
}
}
}
}
},
{
"name": "texture transform first identity, second none",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0,
"extensions": {
"KHR_texture_transform": {
"scale": [1.0, 1.0]
}
}
},
"specularGlossinessTexture": {
"index": 1
}
}
}
},
{
"name": "texture transform first none, second identity",
"extensions": {
"KHR_materials_pbrSpecularGlossiness": {
"diffuseTexture": {
"index": 0
},
"specularGlossinessTexture": {
"index": 1,
"extensions": {
"KHR_texture_transform": {
"offset": [0.0, 0.0]
}
}
}
}
}
}
],
"samplers": [
Expand Down
Loading

0 comments on commit 7345d78

Please sign in to comment.