diff --git a/code/AssetLib/Obj/ObjFileData.h b/code/AssetLib/Obj/ObjFileData.h index 3d504d0bb..0dd13f545 100644 --- a/code/AssetLib/Obj/ObjFileData.h +++ b/code/AssetLib/Obj/ObjFileData.h @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include "Common/Maybe.h" namespace Assimp { namespace ObjFile { @@ -183,15 +184,15 @@ struct Material { aiColor3D transparent; //! PBR Roughness - ai_real roughness; + Maybe roughness; //! PBR Metallic - ai_real metallic; + Maybe metallic; //! PBR Metallic - aiColor3D sheen; + Maybe sheen; //! PBR Clearcoat Thickness - ai_real clearcoat_thickness; + Maybe clearcoat_thickness; //! PBR Clearcoat Rougness - ai_real clearcoat_roughness; + Maybe clearcoat_roughness; //! PBR Anisotropy ai_real anisotropy; @@ -206,11 +207,11 @@ struct Material { illumination_model(1), ior(ai_real(1.0)), transparent(ai_real(1.0), ai_real(1.0), ai_real(1.0)), - roughness(ai_real(1.0)), - metallic(ai_real(0.0)), - sheen(ai_real(1.0), ai_real(1.0), ai_real(1.0)), - clearcoat_thickness(ai_real(0.0)), - clearcoat_roughness(ai_real(0.0)), + roughness(), + metallic(), + sheen(), + clearcoat_thickness(), + clearcoat_roughness(), anisotropy(ai_real(0.0)), bump_multiplier(ai_real(1.0)) { std::fill_n(clamp, static_cast(TextureTypeCount), false); diff --git a/code/AssetLib/Obj/ObjFileImporter.cpp b/code/AssetLib/Obj/ObjFileImporter.cpp index 68fdb2172..429c6ea9d 100644 --- a/code/AssetLib/Obj/ObjFileImporter.cpp +++ b/code/AssetLib/Obj/ObjFileImporter.cpp @@ -616,11 +616,16 @@ void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pSc mat->AddProperty(&pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS); mat->AddProperty(&pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY); mat->AddProperty(&pCurrentMaterial->transparent, 1, AI_MATKEY_COLOR_TRANSPARENT); - mat->AddProperty(&pCurrentMaterial->roughness, 1, AI_MATKEY_ROUGHNESS_FACTOR); - mat->AddProperty(&pCurrentMaterial->metallic, 1, AI_MATKEY_METALLIC_FACTOR); - mat->AddProperty(&pCurrentMaterial->sheen, 1, AI_MATKEY_SHEEN_COLOR_FACTOR); - mat->AddProperty(&pCurrentMaterial->clearcoat_thickness, 1, AI_MATKEY_CLEARCOAT_FACTOR); - mat->AddProperty(&pCurrentMaterial->clearcoat_roughness, 1, AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR); + if (pCurrentMaterial->roughness) + mat->AddProperty(&pCurrentMaterial->roughness.Get(), 1, AI_MATKEY_ROUGHNESS_FACTOR); + if (pCurrentMaterial->metallic) + mat->AddProperty(&pCurrentMaterial->metallic.Get(), 1, AI_MATKEY_METALLIC_FACTOR); + if (pCurrentMaterial->sheen) + mat->AddProperty(&pCurrentMaterial->sheen.Get(), 1, AI_MATKEY_SHEEN_COLOR_FACTOR); + if (pCurrentMaterial->clearcoat_thickness) + mat->AddProperty(&pCurrentMaterial->clearcoat_thickness.Get(), 1, AI_MATKEY_CLEARCOAT_FACTOR); + if (pCurrentMaterial->clearcoat_roughness) + mat->AddProperty(&pCurrentMaterial->clearcoat_roughness.Get(), 1, AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR); mat->AddProperty(&pCurrentMaterial->anisotropy, 1, AI_MATKEY_ANISOTROPY_FACTOR); // Adding refraction index diff --git a/code/AssetLib/Obj/ObjFileMtlImporter.cpp b/code/AssetLib/Obj/ObjFileMtlImporter.cpp index bb70a5ed0..a73277701 100644 --- a/code/AssetLib/Obj/ObjFileMtlImporter.cpp +++ b/code/AssetLib/Obj/ObjFileMtlImporter.cpp @@ -205,7 +205,7 @@ void ObjFileMtlImporter::load() { break; case 's': ++m_DataIt; - getColorRGBA(&m_pModel->m_pCurrentMaterial->sheen); + getColorRGBA(m_pModel->m_pCurrentMaterial->sheen); break; case 'c': ++m_DataIt; @@ -268,6 +268,12 @@ void ObjFileMtlImporter::getColorRGBA(aiColor3D *pColor) { pColor->b = b; } +void ObjFileMtlImporter::getColorRGBA(Maybe &value) { + aiColor3D v; + getColorRGBA(&v); + value = Maybe(v); +} + // ------------------------------------------------------------------- // Loads the kind of illumination model. void ObjFileMtlImporter::getIlluminationModel(int &illum_model) { @@ -275,6 +281,7 @@ void ObjFileMtlImporter::getIlluminationModel(int &illum_model) { illum_model = atoi(&m_buffer[0]); } + // ------------------------------------------------------------------- // Loads a single float value. void ObjFileMtlImporter::getFloatValue(ai_real &value) { @@ -284,10 +291,19 @@ void ObjFileMtlImporter::getFloatValue(ai_real &value) { value = 0.0f; return; } - + value = (ai_real)fast_atof(&m_buffer[0]); } +void ObjFileMtlImporter::getFloatValue(Maybe &value) { + m_DataIt = CopyNextWord(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE); + size_t len = std::strlen(&m_buffer[0]); + if (len) + value = Maybe(fast_atof(&m_buffer[0])); + else + value = Maybe(); +} + // ------------------------------------------------------------------- // Creates a material from loaded data. void ObjFileMtlImporter::createMaterial() { diff --git a/code/AssetLib/Obj/ObjFileMtlImporter.h b/code/AssetLib/Obj/ObjFileMtlImporter.h index cda7415e6..6e73cf45c 100644 --- a/code/AssetLib/Obj/ObjFileMtlImporter.h +++ b/code/AssetLib/Obj/ObjFileMtlImporter.h @@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include "Common/Maybe.h" struct aiColor3D; struct aiString; @@ -81,10 +82,12 @@ private: void load(); /// Get color data. void getColorRGBA(aiColor3D *pColor); + void getColorRGBA(Maybe &value); /// Get illumination model from loaded data void getIlluminationModel(int &illum_model); /// Gets a float value from data. void getFloatValue(ai_real &value); + void getFloatValue(Maybe &value); /// Creates a new material from loaded data. void createMaterial(); /// Get texture name from loaded data. diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index 2eff6117f..5b08c0f37 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -182,6 +182,7 @@ SET( Common_SRCS Common/DefaultIOSystem.cpp Common/ZipArchiveIOSystem.cpp Common/PolyTools.h + Common/Maybe.h Common/Importer.cpp Common/IFF.h Common/SGSpatialSort.cpp diff --git a/code/Common/Maybe.h b/code/Common/Maybe.h new file mode 100644 index 000000000..3eb2e1d97 --- /dev/null +++ b/code/Common/Maybe.h @@ -0,0 +1,29 @@ +#pragma once +#include + +template +struct Maybe { +private: + T _val; + bool _valid; + +public: + Maybe() : + _valid(false) {} + + explicit Maybe(const T &val) : + _val(val), _valid(true) { + } + + operator bool() const { + return _valid; + } + + const T &Get() const { + ai_assert(_valid); + return _val; + } + +private: + Maybe &operator&() = delete; +};