Added GLTF Extension KHR_materials_anisotropy (#5950)

* added gltf extension KHR_materials_anisotropy

* Update glTF2Importer.cpp

---------

Co-authored-by: lutz.hoeren <lutz.hoeren@redplant.de>
Co-authored-by: Kim Kulling <kimkulling@users.noreply.github.com>
This commit is contained in:
Lutz Hören
2025-01-21 00:02:46 +01:00
committed by GitHub
parent 79d451bf4b
commit ba4455c9c0
9 changed files with 108 additions and 3 deletions

View File

@@ -53,6 +53,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* KHR_materials_volume full
* KHR_materials_ior full
* KHR_materials_emissive_strength full
* KHR_materials_anisotropy full
*/
#ifndef GLTF2ASSET_H_INC
#define GLTF2ASSET_H_INC
@@ -821,6 +822,15 @@ struct MaterialEmissiveStrength {
void SetDefaults();
};
struct MaterialAnisotropy {
float anisotropyStrength = 0.f;
float anisotropyRotation = 0.f;
TextureInfo anisotropyTexture;
MaterialAnisotropy() { SetDefaults(); }
void SetDefaults();
};
//! The material appearance of a primitive.
struct Material : public Object {
//PBR metallic roughness properties
@@ -859,6 +869,9 @@ struct Material : public Object {
//extension: KHR_materials_emissive_strength
Nullable<MaterialEmissiveStrength> materialEmissiveStrength;
//extension: KHR_materials_anisotropy
Nullable<MaterialAnisotropy> materialAnisotropy;
//extension: KHR_materials_unlit
bool unlit;
@@ -1133,6 +1146,7 @@ public:
bool KHR_materials_volume;
bool KHR_materials_ior;
bool KHR_materials_emissive_strength;
bool KHR_materials_anisotropy;
bool KHR_draco_mesh_compression;
bool FB_ngon_encoding;
bool KHR_texture_basisu;
@@ -1149,6 +1163,7 @@ public:
KHR_materials_volume(false),
KHR_materials_ior(false),
KHR_materials_emissive_strength(false),
KHR_materials_anisotropy(false),
KHR_draco_mesh_compression(false),
FB_ngon_encoding(false),
KHR_texture_basisu(false) {

View File

@@ -1403,6 +1403,18 @@ inline void Material::Read(Value &material, Asset &r) {
}
}
if (r.extensionsUsed.KHR_materials_anisotropy) {
if (Value *curMaterialAnisotropy = FindObject(*extensions, "KHR_materials_anisotropy")) {
MaterialAnisotropy anisotropy;
ReadMember(*curMaterialAnisotropy, "anisotropyStrength", anisotropy.anisotropyStrength);
ReadMember(*curMaterialAnisotropy, "anisotropyRotation", anisotropy.anisotropyRotation);
ReadTextureProperty(r, *curMaterialAnisotropy, "anisotropyTexture", anisotropy.anisotropyTexture);
this->materialAnisotropy = Nullable<MaterialAnisotropy>(anisotropy);
}
}
unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit");
}
}
@@ -1456,6 +1468,12 @@ inline void MaterialEmissiveStrength::SetDefaults() {
emissiveStrength = 0.f;
}
inline void MaterialAnisotropy::SetDefaults() {
//KHR_materials_anisotropy properties
anisotropyStrength = 0.f;
anisotropyRotation = 0.f;
}
inline void Mesh::Read(Value &pJSON_Object, Asset &pAsset_Root) {
Value *curName = FindMember(pJSON_Object, "name");
if (nullptr != curName && curName->IsString()) {
@@ -2153,6 +2171,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) {
CHECK_EXT(KHR_materials_volume);
CHECK_EXT(KHR_materials_ior);
CHECK_EXT(KHR_materials_emissive_strength);
CHECK_EXT(KHR_materials_anisotropy);
CHECK_EXT(KHR_draco_mesh_compression);
CHECK_EXT(KHR_texture_basisu);

View File

@@ -53,6 +53,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* KHR_materials_volume: full
* KHR_materials_ior: full
* KHR_materials_emissive_strength: full
* KHR_materials_anisotropy: full
*/
#ifndef GLTF2ASSETWRITER_H_INC
#define GLTF2ASSETWRITER_H_INC

View File

@@ -546,6 +546,26 @@ namespace glTF2 {
}
}
if (m.materialAnisotropy.isPresent) {
Value materialAnisotropy(rapidjson::Type::kObjectType);
MaterialAnisotropy &anisotropy = m.materialAnisotropy.value;
if (anisotropy.anisotropyStrength != 0.f) {
WriteFloat(materialAnisotropy, anisotropy.anisotropyStrength, "anisotropyStrength", w.mAl);
}
if (anisotropy.anisotropyRotation != 0.f) {
WriteFloat(materialAnisotropy, anisotropy.anisotropyRotation, "anisotropyRotation", w.mAl);
}
WriteTex(materialAnisotropy, anisotropy.anisotropyTexture, "anisotropyTexture", w.mAl);
if (!materialAnisotropy.ObjectEmpty()) {
exts.AddMember("KHR_materials_anisotropy", materialAnisotropy, w.mAl);
}
}
if (!exts.ObjectEmpty()) {
obj.AddMember("extensions", exts, w.mAl);
}
@@ -1018,6 +1038,10 @@ namespace glTF2 {
exts.PushBack(StringRef("KHR_materials_emissive_strength"), mAl);
}
if (this->mAsset.extensionsUsed.KHR_materials_anisotropy) {
exts.PushBack(StringRef("KHR_materials_anisotropy"), mAl);
}
if (this->mAsset.extensionsUsed.FB_ngon_encoding) {
exts.PushBack(StringRef("FB_ngon_encoding"), mAl);
}

View File

@@ -804,6 +804,22 @@ bool glTF2Exporter::GetMatEmissiveStrength(const aiMaterial &mat, glTF2::Materia
return mat.Get(AI_MATKEY_EMISSIVE_INTENSITY, emissiveStrength.emissiveStrength) == aiReturn_SUCCESS;
}
bool glTF2Exporter::GetMatAnisotropy(const aiMaterial &mat, glTF2::MaterialAnisotropy &anisotropy) {
if (mat.Get(AI_MATKEY_ANISOTROPY_FACTOR, anisotropy.anisotropyStrength) != aiReturn_SUCCESS) {
return false;
}
// do not export anisotropy when strength is zero
if (anisotropy.anisotropyStrength == 0.0f) {
return false;
}
mat.Get(AI_MATKEY_ANISOTROPY_ROTATION, anisotropy.anisotropyRotation);
GetMatTex(mat, anisotropy.anisotropyTexture, AI_MATKEY_ANISOTROPY_TEXTURE);
return true;
}
void glTF2Exporter::ExportMaterials() {
aiString aiName;
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
@@ -956,6 +972,12 @@ void glTF2Exporter::ExportMaterials() {
mAsset->extensionsUsed.KHR_materials_emissive_strength = true;
m->materialEmissiveStrength = Nullable<MaterialEmissiveStrength>(emissiveStrength);
}
MaterialAnisotropy anisotropy;
if (GetMatAnisotropy(mat, anisotropy)) {
mAsset->extensionsUsed.KHR_materials_anisotropy = true;
m->materialAnisotropy = Nullable<MaterialAnisotropy>(anisotropy);
}
}
}
}

View File

@@ -84,6 +84,7 @@ struct MaterialTransmission;
struct MaterialVolume;
struct MaterialIOR;
struct MaterialEmissiveStrength;
struct MaterialAnisotropy;
// Vec/matrix types, as raw float arrays
typedef float(vec2)[2];
@@ -126,6 +127,7 @@ protected:
bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume);
bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior);
bool GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength);
bool GetMatAnisotropy(const aiMaterial &mat, glTF2::MaterialAnisotropy &anisotropy);
void ExportMetadata();
void ExportMaterials();
void ExportMeshes();

View File

@@ -2,7 +2,7 @@
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2024, assimp team
Copyright (c) 2006-2025, assimp team
All rights reserved.
@@ -71,6 +71,7 @@ using namespace glTF2;
using namespace glTFCommon;
namespace {
// generate bi-tangents from normals and tangents according to spec
struct Tangent {
aiVector3D xyz;
@@ -369,11 +370,21 @@ static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, M
// KHR_materials_emissive_strength
if (mat.materialEmissiveStrength.isPresent) {
MaterialEmissiveStrength &emissiveStrength = mat.materialEmissiveStrength.value;
const MaterialEmissiveStrength &emissiveStrength = mat.materialEmissiveStrength.value;
aimat->AddProperty(&emissiveStrength.emissiveStrength, 1, AI_MATKEY_EMISSIVE_INTENSITY);
}
// KHR_materials_anisotropy
if (mat.materialAnisotropy.isPresent) {
const MaterialAnisotropy &anisotropy = mat.materialAnisotropy.value;
aimat->AddProperty(&anisotropy.anisotropyStrength, 1, AI_MATKEY_ANISOTROPY_FACTOR);
aimat->AddProperty(&anisotropy.anisotropyRotation, 1, AI_MATKEY_ANISOTROPY_ROTATION);
SetMaterialTextureProperty(embeddedTexIdxs, r, anisotropy.anisotropyTexture, aimat, AI_MATKEY_ANISOTROPY_TEXTURE);
}
return aimat;
} catch (...) {
delete aimat;

View File

@@ -334,12 +334,17 @@ enum aiTextureType {
aiTextureType_MAYA_SPECULAR_COLOR = 24,
aiTextureType_MAYA_SPECULAR_ROUGHNESS = 25,
/** Anisotropy
* Simulates a surface with directional properties
*/
aiTextureType_ANISOTROPY = 26,
#ifndef SWIG
_aiTextureType_Force32Bit = INT_MAX
#endif
};
#define AI_TEXTURE_TYPE_MAX aiTextureType_MAYA_SPECULAR_ROUGHNESS
#define AI_TEXTURE_TYPE_MAX aiTextureType_ANISOTROPY
// -------------------------------------------------------------------------------
/**
@@ -1073,6 +1078,11 @@ extern "C" {
#define AI_MATKEY_EMISSIVE_INTENSITY "$mat.emissiveIntensity", 0, 0
#define AI_MATKEY_USE_AO_MAP "$mat.useAOMap", 0, 0
// Anisotropy
// ----------
#define AI_MATKEY_ANISOTROPY_ROTATION "$mat.anisotropyRotation", 0, 0
#define AI_MATKEY_ANISOTROPY_TEXTURE aiTextureType_ANISOTROPY, 0
// ---------------------------------------------------------------------------
// Pure key names for all texture-related properties
//! @cond MATS_DOC_FULL

View File

@@ -267,6 +267,7 @@ TEST_F(MaterialSystemTest, testMaterialTextureTypeEnum) {
case aiTextureType_SHEEN:
case aiTextureType_CLEARCOAT:
case aiTextureType_TRANSMISSION:
case aiTextureType_ANISOTROPY:
case aiTextureType_UNKNOWN:
if (i > maxTextureType)
maxTextureType = i;