From a48ab1e377150070c65b933b13a697bb44fc21ab Mon Sep 17 00:00:00 2001 From: SeanLin Date: Mon, 16 Sep 2019 11:10:06 +0800 Subject: [PATCH] Support Double Sided Material The material has the "isDoubleSided" bool parameter. Because some models have double-sided texture settings, the new bool parameter is added to determine whether glTF2 'doubleSided' is enabled. The default value of this parameter is false. --- src/fbx/Fbx2Raw.cpp | 2 +- src/gltf/Raw2Gltf.cpp | 15 +++++++++------ src/gltf/properties/MaterialData.cpp | 16 +++++++++------- src/gltf/properties/MaterialData.hpp | 2 ++ src/raw/RawModel.cpp | 7 +++++-- src/raw/RawModel.hpp | 4 +++- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/fbx/Fbx2Raw.cpp b/src/fbx/Fbx2Raw.cpp index 0815cdb..d4d5151 100644 --- a/src/fbx/Fbx2Raw.cpp +++ b/src/fbx/Fbx2Raw.cpp @@ -469,7 +469,7 @@ static void ReadMesh( const RawMaterialType materialType = GetMaterialType(raw, textures, vertexTransparency, skinning.IsSkinned()); const int rawMaterialIndex = raw.AddMaterial( - materialId, materialName, materialType, textures, rawMatProps, userProperties); + materialId, materialName, materialType, textures, rawMatProps, userProperties, false ); raw.AddTriangle( rawVertexIndices[0], diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index 20c1bb2..e3273c4 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -393,12 +393,12 @@ ModelData* Raw2Gltf( }, false); - if (aoMetRoughTex != nullptr) { - // if we successfully built a texture, factors are just multiplicative identity - metallic = roughness = 1.0f; - } else { - // no shininess texture, - roughness = getRoughness(props->shininess); + if (aoMetRoughTex != nullptr) { + // if we successfully built a texture, factors are just multiplicative identity + metallic = roughness = 1.0f; + } else { + // no shininess texture, + roughness = getRoughness(props->shininess); } } else { @@ -444,9 +444,12 @@ ModelData* Raw2Gltf( occlusionTexture = simpleTex(RAW_TEXTURE_USAGE_OCCLUSION).get(); } + // Setting DoubleSided + const bool isDoubleSided = material.isDoubleSided; std::shared_ptr mData = gltf->materials.hold(new MaterialData( material.name, isTransparent, + isDoubleSided, material.info->shadingModel, normalTexture, occlusionTexture, diff --git a/src/gltf/properties/MaterialData.cpp b/src/gltf/properties/MaterialData.cpp index bddebd6..e67c215 100644 --- a/src/gltf/properties/MaterialData.cpp +++ b/src/gltf/properties/MaterialData.cpp @@ -73,6 +73,7 @@ void to_json(json& j, const PBRMetallicRoughness& d) { MaterialData::MaterialData( std::string name, bool isTransparent, + bool isDoubleSided, const RawShadingModel shadingModel, const TextureData* normalTexture, const TextureData* occlusionTexture, @@ -84,6 +85,7 @@ MaterialData::MaterialData( name(std::move(name)), shadingModel(shadingModel), isTransparent(isTransparent), + isDoubleSided(isDoubleSided), normalTexture(Tex::ref(normalTexture)), occlusionTexture(Tex::ref(occlusionTexture)), emissiveTexture(Tex::ref(emissiveTexture)), @@ -92,13 +94,13 @@ MaterialData::MaterialData( pbrMetallicRoughness(pbrMetallicRoughness) {} json MaterialData::serialize() const { - json result = { - {"name", name}, - {"alphaMode", isTransparent ? "BLEND" : "OPAQUE"}, - {"extras", - {{"fromFBX", - {{"shadingModel", Describe(shadingModel)}, - {"isTruePBR", shadingModel == RAW_SHADING_MODEL_PBR_MET_ROUGH}}}}}}; + json result = {{"name", name}, + {"alphaMode", isTransparent ? "BLEND" : "OPAQUE"}, + {"doubleSided", isDoubleSided}, + {"extras", + {{"fromFBX", + {{"shadingModel", Describe(shadingModel)}, + {"isTruePBR", shadingModel == RAW_SHADING_MODEL_PBR_MET_ROUGH}}}}}}; if (normalTexture != nullptr) { result["normalTexture"] = *normalTexture; diff --git a/src/gltf/properties/MaterialData.hpp b/src/gltf/properties/MaterialData.hpp index d7a2ae5..0b226c9 100644 --- a/src/gltf/properties/MaterialData.hpp +++ b/src/gltf/properties/MaterialData.hpp @@ -43,6 +43,7 @@ struct MaterialData : Holdable { MaterialData( std::string name, bool isTransparent, + bool isDoubleSided, RawShadingModel shadingModel, const TextureData* normalTexture, const TextureData* occlusionTexture, @@ -56,6 +57,7 @@ struct MaterialData : Holdable { const std::string name; const RawShadingModel shadingModel; const bool isTransparent; + const bool isDoubleSided; const std::unique_ptr normalTexture; const std::unique_ptr occlusionTexture; const std::unique_ptr emissiveTexture; diff --git a/src/raw/RawModel.cpp b/src/raw/RawModel.cpp index 68f1d6e..1cf97b5 100644 --- a/src/raw/RawModel.cpp +++ b/src/raw/RawModel.cpp @@ -139,7 +139,8 @@ int RawModel::AddMaterial(const RawMaterial& material) { material.type, material.textures, material.info, - material.userProperties); + material.userProperties, + material.isDoubleSided); } int RawModel::AddMaterial( @@ -148,7 +149,8 @@ int RawModel::AddMaterial( const RawMaterialType materialType, const int textures[RAW_TEXTURE_USAGE_MAX], std::shared_ptr materialInfo, - const std::vector& userProperties) { + const std::vector& userProperties, + const bool isDoubleSided ) { for (size_t i = 0; i < materials.size(); i++) { if (materials[i].name != name) { continue; @@ -181,6 +183,7 @@ int RawModel::AddMaterial( material.type = materialType; material.info = materialInfo; material.userProperties = userProperties; + material.isDoubleSided = isDoubleSided; for (int i = 0; i < RAW_TEXTURE_USAGE_MAX; i++) { material.textures[i] = textures[i]; diff --git a/src/raw/RawModel.hpp b/src/raw/RawModel.hpp index 97ec993..afbd3d1 100644 --- a/src/raw/RawModel.hpp +++ b/src/raw/RawModel.hpp @@ -269,6 +269,7 @@ struct RawMaterial { std::shared_ptr info; int textures[RAW_TEXTURE_USAGE_MAX]; std::vector userProperties; + bool isDoubleSided; }; enum RawLightType { @@ -381,7 +382,8 @@ class RawModel { const RawMaterialType materialType, const int textures[RAW_TEXTURE_USAGE_MAX], std::shared_ptr materialInfo, - const std::vector& userProperties); + const std::vector& userProperties, + const bool isDoubleSided ); int AddLight( const char* name, RawLightType lightType,