Revert "Removed KHR_materials_ior support."

This reverts commit 0738742611.
pull/4112/head
diharaw 2021-10-07 09:36:53 +01:00
parent 0738742611
commit b01d008bc0
8 changed files with 72 additions and 0 deletions

View File

@ -50,6 +50,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* KHR_materials_clearcoat full
* KHR_materials_transmission full
* KHR_materials_volume full
* KHR_materials_ior full
*/
#ifndef GLTF2ASSET_H_INC
#define GLTF2ASSET_H_INC
@ -789,6 +790,13 @@ struct MaterialVolume {
void SetDefaults();
};
struct MaterialIOR {
float ior = 0.f;
MaterialIOR() { SetDefaults(); }
void SetDefaults();
};
//! The material appearance of a primitive.
struct Material : public Object {
//PBR metallic roughness properties
@ -818,6 +826,9 @@ struct Material : public Object {
//extension: KHR_materials_volume
Nullable<MaterialVolume> materialVolume;
//extension: KHR_materials_ior
Nullable<MaterialIOR> materialIOR;
//extension: KHR_materials_unlit
bool unlit;
@ -1107,6 +1118,7 @@ public:
bool KHR_materials_clearcoat;
bool KHR_materials_transmission;
bool KHR_materials_volume;
bool KHR_materials_ior;
bool KHR_draco_mesh_compression;
bool FB_ngon_encoding;
bool KHR_texture_basisu;

View File

@ -1228,6 +1228,16 @@ inline void Material::Read(Value &material, Asset &r) {
}
}
if (r.extensionsUsed.KHR_materials_ior) {
if (Value *curMaterialIOR = FindObject(*extensions, "KHR_materials_ior")) {
MaterialIOR ior;
ReadMember(*curMaterialIOR, "ior", ior.ior);
this->materialIOR = Nullable<MaterialIOR>(ior);
}
}
unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit");
}
}
@ -1280,6 +1290,11 @@ inline void MaterialVolume::SetDefaults() {
SetVector(attenuationColor, defaultAttenuationColor);
}
inline void MaterialIOR::SetDefaults() {
//KHR_materials_ior properties
ior = 1.5f;
}
namespace {
template <int N>
@ -1952,6 +1967,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) {
CHECK_EXT(KHR_materials_clearcoat);
CHECK_EXT(KHR_materials_transmission);
CHECK_EXT(KHR_materials_volume);
CHECK_EXT(KHR_materials_ior);
CHECK_EXT(KHR_draco_mesh_compression);
CHECK_EXT(KHR_texture_basisu);

View File

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

View File

@ -496,6 +496,20 @@ namespace glTF2 {
}
}
if (m.materialIOR.isPresent) {
Value materialIOR(rapidjson::Type::kObjectType);
MaterialIOR &ior = m.materialIOR.value;
if (ior.ior != 1.5f) {
WriteFloat(materialIOR, ior.ior, "ior", w.mAl);
}
if (!materialIOR.ObjectEmpty()) {
exts.AddMember("KHR_materials_ior", materialIOR, w.mAl);
}
}
if (!exts.ObjectEmpty()) {
obj.AddMember("extensions", exts, w.mAl);
}
@ -916,6 +930,10 @@ namespace glTF2 {
exts.PushBack(StringRef("KHR_materials_volume"), mAl);
}
if (this->mAsset.extensionsUsed.KHR_materials_ior) {
exts.PushBack(StringRef("KHR_materials_ior"), mAl);
}
if (this->mAsset.extensionsUsed.FB_ngon_encoding) {
exts.PushBack(StringRef("FB_ngon_encoding"), mAl);
}

View File

@ -716,6 +716,10 @@ bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &v
return result || volume.thicknessTexture.texture;
}
bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) {
return mat.Get(AI_MATKEY_IOR, ior.ior) == aiReturn_SUCCESS;
}
void glTF2Exporter::ExportMaterials() {
aiString aiName;
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
@ -842,6 +846,12 @@ void glTF2Exporter::ExportMaterials() {
mAsset->extensionsUsed.KHR_materials_volume = true;
m->materialVolume = Nullable<MaterialVolume>(volume);
}
MaterialIOR ior;
if (GetMatIOR(mat, ior)) {
mAsset->extensionsUsed.KHR_materials_ior = true;
m->materialIOR = Nullable<MaterialIOR>(ior);
}
}
}
}

View File

@ -78,6 +78,7 @@ struct MaterialSheen;
struct MaterialClearcoat;
struct MaterialTransmission;
struct MaterialVolume;
struct MaterialIOR;
// Vec/matrix types, as raw float arrays
typedef float(vec2)[2];
@ -116,6 +117,7 @@ protected:
bool GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat);
bool GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission);
bool GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume);
bool GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior);
void ExportMetadata();
void ExportMaterials();
void ExportMeshes();

View File

@ -351,6 +351,13 @@ static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, M
SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR);
}
// KHR_materials_ior
if (mat.materialIOR.isPresent) {
MaterialIOR &ior = mat.materialIOR.value;
aimat->AddProperty(&ior.ior, 1, AI_MATKEY_IOR);
}
return aimat;
} catch (...) {
delete aimat;

View File

@ -1041,6 +1041,12 @@ extern "C" {
// The color that white light turns into due to absorption when reaching the attenuation distance.
#define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0
// IOR
// ------------
// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_ior
// Index of Refraction.
#define AI_MATKEY_IOR "$mat.ior", 0, 0
// Emissive
// --------
#define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0