Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Double Sided Material and fix image file close issue #226

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fbx/Fbx2Raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,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],
Expand Down
15 changes: 9 additions & 6 deletions src/gltf/Raw2Gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,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 {
Expand Down Expand Up @@ -392,9 +392,12 @@ ModelData* Raw2Gltf(
occlusionTexture = simpleTex(RAW_TEXTURE_USAGE_OCCLUSION).get();
}

// Setting DoubleSided
const bool isDoubleSided = material.isDoubleSided;
std::shared_ptr<MaterialData> mData = gltf->materials.hold(new MaterialData(
material.name,
isTransparent,
isDoubleSided,
material.info->shadingModel,
normalTexture,
occlusionTexture,
Expand Down
3 changes: 3 additions & 0 deletions src/gltf/properties/MaterialData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
Expand All @@ -94,6 +96,7 @@ MaterialData::MaterialData(
json MaterialData::serialize() const {
json result = {{"name", name},
{"alphaMode", isTransparent ? "BLEND" : "OPAQUE"},
{"doubleSided", isDoubleSided},
{"extras",
{{"fromFBX",
{{"shadingModel", Describe(shadingModel)},
Expand Down
2 changes: 2 additions & 0 deletions src/gltf/properties/MaterialData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct MaterialData : Holdable {
MaterialData(
std::string name,
bool isTransparent,
bool isDoubleSided,
RawShadingModel shadingModel,
const TextureData* normalTexture,
const TextureData* occlusionTexture,
Expand All @@ -56,6 +57,7 @@ struct MaterialData : Holdable {
const std::string name;
const RawShadingModel shadingModel;
const bool isTransparent;
const bool isDoubleSided;
const std::unique_ptr<const Tex> normalTexture;
const std::unique_ptr<const Tex> occlusionTexture;
const std::unique_ptr<const Tex> emissiveTexture;
Expand Down
7 changes: 5 additions & 2 deletions src/raw/RawModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ int RawModel::AddMaterial(const RawMaterial& material) {
material.type,
material.textures,
material.info,
material.userProperties);
material.userProperties,
material.isDoubleSided);
}

int RawModel::AddMaterial(
Expand All @@ -151,7 +152,8 @@ int RawModel::AddMaterial(
const RawMaterialType materialType,
const int textures[RAW_TEXTURE_USAGE_MAX],
std::shared_ptr<RawMatProps> materialInfo,
const std::vector<std::string>& userProperties) {
const std::vector<std::string>& userProperties,
const bool isDoubleSided ) {
for (size_t i = 0; i < materials.size(); i++) {
if (materials[i].name != name) {
continue;
Expand Down Expand Up @@ -184,6 +186,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];
Expand Down
4 changes: 3 additions & 1 deletion src/raw/RawModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ struct RawMaterial {
std::shared_ptr<RawMatProps> info;
int textures[RAW_TEXTURE_USAGE_MAX];
std::vector<std::string> userProperties;
bool isDoubleSided;
};

enum RawLightType {
Expand Down Expand Up @@ -370,7 +371,8 @@ class RawModel {
const RawMaterialType materialType,
const int textures[RAW_TEXTURE_USAGE_MAX],
std::shared_ptr<RawMatProps> materialInfo,
const std::vector<std::string>& userProperties);
const std::vector<std::string>& userProperties,
const bool isDoubleSided );
int AddLight(
const char* name,
RawLightType lightType,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/Image_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ ImageProperties GetImageProperties(char const* filePath) {
if (success && channels == 4 && imageHasTransparentPixels(f)) {
result.occlusion = IMAGE_TRANSPARENT;
}

fclose(f);
return result;
}

Expand Down