From 44757af34a0047e974ba6d30b9b027d0fffecc5d Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Tue, 5 Sep 2017 16:29:00 -0400 Subject: [PATCH] Implement pbrSpecularGlossiness property as Nullable --- code/glTF2Asset.h | 8 +++++--- code/glTF2Asset.inl | 23 ++++++++++++++--------- code/glTF2AssetWriter.inl | 21 +++++++++++---------- code/glTF2Exporter.cpp | 15 +++++++++------ code/glTF2Importer.cpp | 14 +++++++------- 5 files changed, 46 insertions(+), 35 deletions(-) diff --git a/code/glTF2Asset.h b/code/glTF2Asset.h index 486e23bc0..07f8614ab 100644 --- a/code/glTF2Asset.h +++ b/code/glTF2Asset.h @@ -711,12 +711,14 @@ namespace glTF2 struct PbrSpecularGlossiness { - bool on = false; vec4 diffuseFactor; vec3 specularFactor; float glossinessFactor; TextureInfo diffuseTexture; - TextureInfo specularGlossinessTexture; + TextureInfo specularGlossinessTexture; + + PbrSpecularGlossiness() { SetDefaults(); } + void SetDefaults(); }; //! The material appearance of a primitive. @@ -735,7 +737,7 @@ namespace glTF2 bool doubleSided; //extension: KHR_materials_pbrSpecularGlossiness - PbrSpecularGlossiness pbrSpecularGlossiness; + Nullable pbrSpecularGlossiness; Material() { SetDefaults(); } void Read(Value& obj, Asset& r); diff --git a/code/glTF2Asset.inl b/code/glTF2Asset.inl index 07e0a5e8e..e2d812563 100644 --- a/code/glTF2Asset.inl +++ b/code/glTF2Asset.inl @@ -790,13 +790,15 @@ inline void Material::Read(Value& material, Asset& r) if (Value* extensions = FindObject(material, "extensions")) { if (r.extensionsUsed.KHR_materials_pbrSpecularGlossiness) { if (Value* pbrSpecularGlossiness = FindObject(*extensions, "KHR_materials_pbrSpecularGlossiness")) { - this->pbrSpecularGlossiness.on = true; + PbrSpecularGlossiness pbrSG; - ReadMember(*pbrSpecularGlossiness, "diffuseFactor", this->pbrSpecularGlossiness.diffuseFactor); - ReadTextureProperty(r, *pbrSpecularGlossiness, "diffuseTexture", this->pbrSpecularGlossiness.diffuseTexture); - ReadTextureProperty(r, *pbrSpecularGlossiness, "specularGlossinessTexture", this->pbrSpecularGlossiness.specularGlossinessTexture); - ReadMember(*pbrSpecularGlossiness, "specularFactor", this->pbrSpecularGlossiness.specularFactor); - ReadMember(*pbrSpecularGlossiness, "glossinessFactor", this->pbrSpecularGlossiness.glossinessFactor); + ReadMember(*pbrSpecularGlossiness, "diffuseFactor", pbrSG.diffuseFactor); + ReadTextureProperty(r, *pbrSpecularGlossiness, "diffuseTexture", pbrSG.diffuseTexture); + ReadTextureProperty(r, *pbrSpecularGlossiness, "specularGlossinessTexture", pbrSG.specularGlossinessTexture); + ReadMember(*pbrSpecularGlossiness, "specularFactor", pbrSG.specularFactor); + ReadMember(*pbrSpecularGlossiness, "glossinessFactor", pbrSG.glossinessFactor); + + this->pbrSpecularGlossiness = Nullable(pbrSG); } } } @@ -821,11 +823,14 @@ inline void Material::SetDefaults() alphaMode = "OPAQUE"; alphaCutoff = 0.5; doubleSided = false; +} +inline void PbrSpecularGlossiness::SetDefaults() +{ //pbrSpecularGlossiness properties - SetVector(pbrSpecularGlossiness.diffuseFactor, defaultDiffuseFactor); - SetVector(pbrSpecularGlossiness.specularFactor, defaultSpecularFactor); - pbrSpecularGlossiness.glossinessFactor = 1.0; + SetVector(diffuseFactor, defaultDiffuseFactor); + SetVector(specularFactor, defaultSpecularFactor); + glossinessFactor = 1.0; } namespace { diff --git a/code/glTF2AssetWriter.inl b/code/glTF2AssetWriter.inl index 475c7a0d9..ad68a14e1 100644 --- a/code/glTF2AssetWriter.inl +++ b/code/glTF2AssetWriter.inl @@ -324,22 +324,23 @@ namespace glTF2 { Value exts; exts.SetObject(); - if (m.pbrSpecularGlossiness.on) { + if (m.pbrSpecularGlossiness.isPresent) { Value pbrSpecularGlossiness; pbrSpecularGlossiness.SetObject(); - { - //pbrSpecularGlossiness - WriteVec(pbrSpecularGlossiness, m.pbrSpecularGlossiness.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl); - WriteVec(pbrSpecularGlossiness, m.pbrSpecularGlossiness.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl); - if (m.pbrSpecularGlossiness.glossinessFactor != 1) { - WriteFloat(obj, m.pbrSpecularGlossiness.glossinessFactor, "glossinessFactor", w.mAl); - } + PbrSpecularGlossiness &pbrSG = m.pbrSpecularGlossiness.value; - WriteTex(pbrSpecularGlossiness, m.pbrSpecularGlossiness.diffuseTexture, "diffuseTexture", w.mAl); - WriteTex(pbrSpecularGlossiness, m.pbrSpecularGlossiness.specularGlossinessTexture, "specularGlossinessTexture", w.mAl); + //pbrSpecularGlossiness + WriteVec(pbrSpecularGlossiness, pbrSG.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl); + WriteVec(pbrSpecularGlossiness, pbrSG.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl); + + if (pbrSG.glossinessFactor != 1) { + WriteFloat(obj, pbrSG.glossinessFactor, "glossinessFactor", w.mAl); } + WriteTex(pbrSpecularGlossiness, pbrSG.diffuseTexture, "diffuseTexture", w.mAl); + WriteTex(pbrSpecularGlossiness, pbrSG.specularGlossinessTexture, "specularGlossinessTexture", w.mAl); + if (!pbrSpecularGlossiness.ObjectEmpty()) { exts.AddMember("KHR_materials_pbrSpecularGlossiness", pbrSpecularGlossiness, w.mAl); } diff --git a/code/glTF2Exporter.cpp b/code/glTF2Exporter.cpp index 073309b06..600e2b3d4 100644 --- a/code/glTF2Exporter.cpp +++ b/code/glTF2Exporter.cpp @@ -439,12 +439,15 @@ void glTF2Exporter::ExportMaterials() KHR_materials_pbrSpecularGlossiness = true; } - m->pbrSpecularGlossiness.on = true; - GetMatColor(mat, m->pbrSpecularGlossiness.diffuseFactor, "$clr.diffuse", 0, 1); - GetMatColor(mat, m->pbrSpecularGlossiness.specularFactor, "$clr.specular", 0, 1); - mat->Get("$mat.gltf.glossinessFactor", 0, 0, m->pbrSpecularGlossiness.glossinessFactor); - GetMatTex(mat, m->pbrSpecularGlossiness.diffuseTexture, aiTextureType_DIFFUSE, 1); - GetMatTex(mat, m->pbrSpecularGlossiness.specularGlossinessTexture, aiTextureType_UNKNOWN, 1); + PbrSpecularGlossiness pbrSG; + + GetMatColor(mat, pbrSG.diffuseFactor, "$clr.diffuse", 0, 1); + GetMatColor(mat, pbrSG.specularFactor, "$clr.specular", 0, 1); + mat->Get("$mat.gltf.glossinessFactor", 0, 0, pbrSG.glossinessFactor); + GetMatTex(mat, pbrSG.diffuseTexture, aiTextureType_DIFFUSE, 1); + GetMatTex(mat, pbrSG.specularGlossinessTexture, aiTextureType_UNKNOWN, 1); + + m->pbrSpecularGlossiness = Nullable(pbrSG); } } } diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index 82ea5f3e7..ff806257f 100644 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -249,13 +249,13 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r) aimat->AddProperty(&mat.alphaCutoff, 1, "$mat.gltf.alphaCutoff"); //pbrSpecularGlossiness - if (mat.pbrSpecularGlossiness.on) { - aimat->AddProperty(&mat.pbrSpecularGlossiness.on, 1, "$mat.gltf.pbrSpecularGlossiness.on"); - SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.diffuseFactor, aimat, "$clr.diffuse", 0, 1); - SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.specularFactor, aimat, "$clr.specular", 0, 1); - aimat->AddProperty(&mat.pbrSpecularGlossiness.glossinessFactor, 1, "$mat.gltf.pbrSpecularGlossiness.glossinessFactor"); - SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.diffuseTexture, aimat, aiTextureType_DIFFUSE, 1); - SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.specularGlossinessTexture, aimat, aiTextureType_UNKNOWN, 1); + if (mat.pbrSpecularGlossiness.isPresent) { + aimat->AddProperty(&mat.pbrSpecularGlossiness.isPresent, 1, "$mat.gltf.pbrSpecularGlossiness.on"); + SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.value.diffuseFactor, aimat, "$clr.diffuse", 0, 1); + SetMaterialColorProperty(r, mat.pbrSpecularGlossiness.value.specularFactor, aimat, "$clr.specular", 0, 1); + aimat->AddProperty(&mat.pbrSpecularGlossiness.value.glossinessFactor, 1, "$mat.gltf.pbrSpecularGlossiness.glossinessFactor"); + SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.value.diffuseTexture, aimat, aiTextureType_DIFFUSE, 1); + SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrSpecularGlossiness.value.specularGlossinessTexture, aimat, aiTextureType_UNKNOWN, 1); } } }