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

Export PBR material information from 3D models #5

Open
wants to merge 1 commit into
base: master
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
17 changes: 15 additions & 2 deletions cli/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
#include <iostream>
#include <fstream>

#include "Material.h"
#include "Scene3D.h"
#include "LoadScene.h"
#include "SaveScene.h"
Expand All @@ -29,6 +30,9 @@ int main(int argc, char **argv)
if (argc < 2)
{
std::cerr << "Missing input parameter." << std::endl;
std::cerr << std::endl;
std::cerr << "Usage: dli-exporter [originalModelName] [exportedModelName[.dli]] [OPTION]" << std::endl;
std::cerr << " -m, --material Export PBR materials from the original model" << std::endl;
return 1;
}

Expand Down Expand Up @@ -56,23 +60,32 @@ int main(int argc, char **argv)

outPath = outPath.substr(0, inPath.rfind('.'));

std::string extraArgs{};
if (argc > 3)
{
extraArgs = argv[3];
}
bool importMaterials = (extraArgs == "-m" || extraArgs == "--material");

Scene3D scene_data;
MeshIds meshIds;
GetSceneNodes(scene_data, meshIds, nullptr, scene, scene->mRootNode);
PackSceneNodeMeshIds(scene_data, meshIds);
GetSceneMeshes(scene_data, meshIds, scene);
GetSceneCameras(scene_data, scene);
GetSceneLights(scene_data, scene);
GetSceneMaterials(scene_data, scene);
GetAnimations(scene_data, scene);

std::string outBin = outPath + ".bin";
std::ofstream ofsBin(outBin, ios::binary);
std::ofstream ofsDli(outPath + ".dli");

int result = 0;
if (!ConvertScene(&scene_data, outBin, ofsDli, ofsBin, false))
if (!ConvertScene(&scene_data, outBin, ofsDli, ofsBin, importMaterials))
{
result = 1;
}

return result;
}
3 changes: 2 additions & 1 deletion core/include/LoadScene.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef LOADSCENE_H
#define LOADSCENE_H
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@ void GetSceneMeshes(Scene3D& scene_data, const MeshIds& meshIds, const aiScene *

void GetSceneCameras( Scene3D &scene_data, const aiScene *scene );
void GetSceneLights( Scene3D& scene_data, const aiScene* scene );
void GetSceneMaterials( Scene3D &scene_data, const aiScene *scene );
void GetAnimations( Scene3D &scene_data, const aiScene *scene );

#endif // LOADSCENE_H
60 changes: 60 additions & 0 deletions core/include/Material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef MATERIALS_H
#define MATERIALS_H

/*
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

//#include "Scene3D.h"
#include <vector>

struct MaterialInfo
{
std::string name;
std::string albedoMap;
float color[4];

std::string normalMap;
std::string metallicMap;
float metallicFactor;
std::string roughnessMap;
float roughnessFactor;

std::string albedoMetallicMap;
std::string normalRoughnessMap;
std::string metallicRoughnessMap;

std::string subsurfaceMap;

bool mipmap;
bool alpha;

std::string splitTexture;

MaterialInfo()
:color{1.0f,1.0f,1.0f,1.0f},
metallicFactor{ 1.0f },
roughnessFactor{ 1.0f },
mipmap{ true },
alpha{ false }
{
}
};


void LoadGltfMaterials(const aiScene *scene, std::vector<MaterialInfo>& materials);

#endif // MATERIALS_H
8 changes: 6 additions & 2 deletions core/include/Scene3D.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SCENE3D_H
#define SCENE3D_H
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
#include "Camera3D.h"
#include "Light.h"
#include "Animation3D.h"
#include "Material.h"
#include <vector>

using namespace std;
Expand All @@ -38,6 +39,7 @@ class Scene3D
unsigned int GetNumCameras() const;
unsigned int GetNumLights() const;
unsigned int GetNumAnimations() const;
unsigned int GetNumMaterials() const;
void AddNode(Node3D *enode);
Node3D* GetNode(unsigned int idx);
Node3D* FindNodeNamed(const std::string& name) const;
Expand All @@ -53,7 +55,8 @@ class Scene3D
void AddAnimation(Animation3D &eanim);
bool HasAnimations();
Animation3D* GetAnimation(unsigned int idx);
protected:
void AddMaterial(MaterialInfo &eMaterial);
MaterialInfo* GetMaterial(unsigned int idx);

private:
vector<Node3D*> m_nodes;
Expand All @@ -62,6 +65,7 @@ class Scene3D
vector<Camera3D> m_cameras;
vector<Light> m_lights;
vector<Animation3D> m_animations;
vector<MaterialInfo> m_materials;
};

#endif // SCENE3D_H
15 changes: 14 additions & 1 deletion core/src/LoadScene.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -532,6 +532,19 @@ void GetSceneLights( Scene3D& scene_data, const aiScene* scene)
}
}

void GetSceneMaterials( Scene3D& scene_data, const aiScene* scene)
{
std::vector<MaterialInfo> materials;
LoadGltfMaterials(scene, materials);

printf("materials.count = %lu\n", materials.size());

for (auto material : materials)
{
scene_data.AddMaterial(material);
}
}

void GetAnimations( Scene3D &scene_data, const aiScene *scene )
{
if(!scene->HasAnimations())
Expand Down
82 changes: 82 additions & 0 deletions core/src/Material.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <assimp/scene.h>
#include <assimp/pbrmaterial.h>
#include "Material.h"

// Disables warning C4003 for the macro AI_MATKEY_TEXTURE
// The AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE defines the two parameters required.
#undef AI_MATKEY_TEXTURE
#define AI_MATKEY_TEXTURE(...) _AI_MATKEY_TEXTURE_BASE,##__VA_ARGS__

void LoadGltfMaterials(const aiScene * scene, std::vector<MaterialInfo>& materials)
{
for (uint32_t i = 0; i < scene->mNumMaterials; ++i)
{
aiMaterial* mat = scene->mMaterials[i];
MaterialInfo info;
info.name = mat->GetName().C_Str();

aiString value;
aiString tex;
if( mat->Get(AI_MATKEY_TEXTURE(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE), tex) == AI_SUCCESS)
{
info.albedoMap = tex.C_Str();
}

if (mat->Get(AI_MATKEY_TEXTURE(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE), tex) == AI_SUCCESS)
{
info.splitTexture = tex.C_Str();
}

if (mat->Get(AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0), tex) == AI_SUCCESS)
{
info.normalMap = tex.C_Str();
}

aiColor4D col;
if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR, col) == AI_SUCCESS)
{
info.color[0] = col.r;
info.color[1] = col.g;
info.color[2] = col.b;
info.color[3] = col.a;
}

float factor = 1.0;
if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR, factor) == AI_SUCCESS)
{
info.metallicFactor = factor;
}

if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR, factor) == AI_SUCCESS)
{
info.roughnessFactor = factor;
}

if (mat->Get(AI_MATKEY_GLTF_ALPHAMODE, value) == AI_SUCCESS)
{
if (!strcmp(value.data, "BLEND"))
{
info.alpha = true;
}
}
materials.push_back(info);
}
}

Loading