Added gltf2 KHR_materials_volume import support.

pull/4112/head
diharaw 2021-09-29 17:05:17 +01:00
parent 96e2f1cd5b
commit 200bf8df7a
4 changed files with 64 additions and 0 deletions

View File

@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* KHR_materials_sheen full
* KHR_materials_clearcoat full
* KHR_materials_transmission full
* KHR_materials_volume full
*/
#ifndef GLTF2ASSET_H_INC
#define GLTF2ASSET_H_INC
@ -713,6 +714,7 @@ const vec3 defaultEmissiveFactor = { 0, 0, 0 };
const vec4 defaultDiffuseFactor = { 1, 1, 1, 1 };
const vec3 defaultSpecularFactor = { 1, 1, 1 };
const vec3 defaultSheenFactor = { 0, 0, 0 };
const vec3 defaultAttenuationColor = { 1, 1, 1 };
struct TextureInfo {
Ref<Texture> texture;
@ -777,6 +779,16 @@ struct MaterialTransmission {
float transmissionFactor = 0.f;
};
struct MaterialVolume {
float thicknessFactor = 0.f;
TextureInfo thicknessTexture;
float attenuationDistance = 0.f;
vec3 attenuationColor;
MaterialVolume() { SetDefaults(); }
void SetDefaults();
};
//! The material appearance of a primitive.
struct Material : public Object {
//PBR metallic roughness properties
@ -803,6 +815,9 @@ struct Material : public Object {
//extension: KHR_materials_transmission
Nullable<MaterialTransmission> materialTransmission;
//extension: KHR_materials_volume
Nullable<MaterialVolume> materialVolume;
//extension: KHR_materials_unlit
bool unlit;
@ -1091,6 +1106,7 @@ public:
bool KHR_materials_sheen;
bool KHR_materials_clearcoat;
bool KHR_materials_transmission;
bool KHR_materials_volume;
bool KHR_draco_mesh_compression;
bool FB_ngon_encoding;
bool KHR_texture_basisu;

View File

@ -1215,6 +1215,19 @@ inline void Material::Read(Value &material, Asset &r) {
}
}
if (r.extensionsUsed.KHR_materials_volume) {
if (Value *curMaterialVolume = FindObject(*extensions, "KHR_materials_volume")) {
MaterialVolume volume;
ReadMember(*curMaterialVolume, "thicknessFactor", volume.thicknessFactor);
ReadTextureProperty(r, *curMaterialVolume, "thicknessTexture", volume.thicknessTexture);
ReadMember(*curMaterialVolume, "attenuationDistance", volume.attenuationDistance);
ReadMember(*curMaterialVolume, "attenuationColor", volume.attenuationColor);
this->materialVolume = Nullable<MaterialVolume>(volume);
}
}
unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit");
}
}
@ -1260,6 +1273,13 @@ inline void MaterialSheen::SetDefaults() {
sheenRoughnessFactor = 0.f;
}
inline void MaterialVolume::SetDefaults() {
//KHR_materials_volume properties
thicknessFactor = 0.f;
attenuationDistance = INFINITY;
SetVector(attenuationColor, defaultAttenuationColor);
}
namespace {
template <int N>
@ -1931,6 +1951,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) {
CHECK_EXT(KHR_materials_sheen);
CHECK_EXT(KHR_materials_clearcoat);
CHECK_EXT(KHR_materials_transmission);
CHECK_EXT(KHR_materials_volume);
CHECK_EXT(KHR_draco_mesh_compression);
CHECK_EXT(KHR_texture_basisu);

View File

@ -341,6 +341,16 @@ static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, M
SetMaterialTextureProperty(embeddedTexIdxs, r, transmission.transmissionTexture, aimat, AI_MATKEY_TRANSMISSION_TEXTURE);
}
// KHR_materials_volume
if (mat.materialVolume.isPresent) {
MaterialVolume &volume = mat.materialVolume.value;
aimat->AddProperty(&volume.thicknessFactor, 1, AI_MATKEY_VOLUME_THICKNESS_FACTOR);
SetMaterialTextureProperty(embeddedTexIdxs, r, volume.thicknessTexture, aimat, AI_MATKEY_VOLUME_THICKNESS_TEXTURE);
aimat->AddProperty(&volume.attenuationDistance, 1, AI_MATKEY_VOLUME_ATTENUATION_DISTANCE);
SetMaterialColorProperty(r, volume.attenuationColor, aimat, AI_MATKEY_VOLUME_ATTENUATION_COLOR);
}
return aimat;
} catch (...) {
delete aimat;

View File

@ -318,6 +318,12 @@ enum aiTextureType {
*/
aiTextureType_TRANSMISSION = 21,
/** Volume
* Simulates transmission through the surface
* May include further information such as wall thickness
*/
aiTextureType_VOLUME = 22,
/** Unknown texture
*
* A texture reference that does not match any of the definitions
@ -1028,6 +1034,17 @@ extern "C" {
// Multiplied by AI_MATKEY_TRANSMISSION_FACTOR
#define AI_MATKEY_TRANSMISSION_TEXTURE aiTextureType_TRANSMISSION, 0
// Volume
// ------------
// https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_volume
// Base percentage of light transmitted through the surface. 0.0 = Opaque, 1.0 = Fully transparent
#define AI_MATKEY_VOLUME_THICKNESS_FACTOR "$mat.volume.thicknessFactor", 0, 0
// Texture defining percentage of light transmitted through the surface.
// Multiplied by AI_MATKEY_THICKNESS_FACTOR
#define AI_MATKEY_VOLUME_THICKNESS_TEXTURE aiTextureType_VOLUME, 0
#define AI_MATKEY_VOLUME_ATTENUATION_DISTANCE "$mat.volume.attenuationDistance", 0, 0
#define AI_MATKEY_VOLUME_ATTENUATION_COLOR "$mat.volume.attenuationColor", 0, 0
// Emissive
// --------
#define AI_MATKEY_USE_EMISSIVE_MAP "$mat.useEmissiveMap", 0, 0