Skip to content

Commit

Permalink
feat: #47 Add export material
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jan 12, 2020
1 parent 9c457c3 commit 4d8a876
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Projects/Skylicht/Engine/Source/GameObject/CGameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This file is part of the "Skylicht Engine".
#pragma once

#include "pch.h"
#include "Material/CShaderParams.h"
#include "Material/Shader/CShaderParams.h"
#include "Components/CComponentSystem.h"
#include "Transform/CTransformEuler.h"
#include "Transform/CTransformMatrix.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This file is part of the "Skylicht Engine".
#include "Utils/CPath.h"

#include "GameObject/CGameObject.h"
#include "Material/CShaderManager.h"
#include "Material/Shader/CShaderManager.h"

#include "Entity/CEntityPrefab.h"
#include "RenderMesh/CRenderMeshData.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,16 @@ namespace Skylicht

struct SUniformUI
{
EUIControlType ControlType;
std::string Name;
std::string AutoReplace;
int Step;
EUIControlType ControlType;
std::string Name;
std::string AutoReplace;
int Step;

SUniform *UniformInfo;
CShader *Shader;
SUniform *UniformInfo;
CShader *Shader;

SUniformUI *Parent;
core::array<SUniformUI*> Childs;
SUniformUI *Parent;
core::array<SUniformUI*> Childs;

SUniformUI(CShader *shader)
{
Expand Down
98 changes: 98 additions & 0 deletions Projects/Skylicht/Engine/Source/MeshManager/CMeshManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "CMeshManager.h"

#include "Importer/Collada/CColladaLoader.h"
#include "RenderMesh/CRenderMeshData.h"
#include "Material/CShaderManager.h"
#include "Material/CShader.h"

namespace Skylicht
{
Expand Down Expand Up @@ -73,4 +76,99 @@ namespace Skylicht

return output;
}

void CMeshManager::exportMaterial(CEntityPrefab *prefab, const char *folder, const char *filename)
{
std::string matFile = folder;
matFile += "/";
matFile += filename;

IrrlichtDevice *device = getIrrlichtDevice();
io::IFileSystem *fs = device->getFileSystem();

io::IWriteFile *writeFile = fs->createAndWriteFile(matFile.c_str());
if (writeFile == NULL)
return;

std::string buffer;
std::map<std::string, bool> saved;

char data[1024];

buffer += "<Materials>\n";

CEntity** entities = prefab->getEntities();
for (int i = 0, n = prefab->getNumEntities(); i < n; i++)
{
CRenderMeshData *renderer = entities[i]->getData<CRenderMeshData>();
if (renderer != NULL)
{
CMesh *mesh = renderer->getMesh();
if (mesh != NULL)
{
for (int j = 0, m = (int)mesh->getMeshBufferCount(); j < m; j++)
{
ITexture* texture = NULL;
IMeshBuffer* meshBuffer = mesh->getMeshBuffer(j);

if (meshBuffer != NULL && j < (int)mesh->MaterialName.size())
{
const char *materialName = mesh->MaterialName[j].c_str();
if (saved[materialName] == false)
{
SMaterial& material = meshBuffer->getMaterial();

CShader *shader = CShaderManager::getInstance()->getShaderByID(material.MaterialType);
if (shader != NULL)
{
sprintf(data, "\t<Material name='%s' shader='%s'>\n", materialName, shader->getShaderPath().c_str());
buffer += data;

// write texture slot
buffer += "\t\t<Textures>\n";
for (int i = 0; i < MATERIAL_MAX_TEXTURES; i++)
{
ITexture *texture = material.TextureLayer[i].Texture;
if (texture != NULL)
{
sprintf(data, "\t\t\t<Texture slot='%d' path='%s'/>\n", i, texture->getName().getPath().c_str());
buffer += data;
}
}
buffer += "\t\t</Textures>\n";

// write default shader params
buffer += "\t\t<Params>\n";
for (int i = 0, n = shader->getNumUI(); i < n; i++)
{
CShader::SUniformUI *uniformUI = shader->getUniformUI(i);
CShader::SUniform* info = uniformUI->UniformInfo;
if (info != NULL)
{
sprintf(data, "\t\t\t<Param name='%s' floatSize='%d' floatValue='%f,%f,%f,%f'/>\n",
info->Name.c_str(),
info->FloatSize,
info->Value[0],
info->Value[1],
info->Value[2],
info->Value[3]);
buffer += data;
}
}
buffer += "\t\t</Params>\n";

buffer += "\t</Material>\n";
saved[materialName] = true;
}
}
}
}
}
}
}

buffer += "</Materials>";
writeFile->write(buffer.c_str(), buffer.size());
writeFile->drop();
}
}
2 changes: 2 additions & 0 deletions Projects/Skylicht/Engine/Source/MeshManager/CMeshManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Skylicht

CEntityPrefab* loadModel(const char *resource, const char *texturePath, bool loadNormalMap = true, bool loadTexcoord2 = false, bool createBatching = false);

void exportMaterial(CEntityPrefab *prefab, const char *folder, const char *filename);

void releaseAllPrefabs();
};
}

0 comments on commit 4d8a876

Please sign in to comment.