Skip to content

Commit

Permalink
feat: #47 apply material to MeshRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jan 12, 2020
1 parent 7f65558 commit 61d0be4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
15 changes: 7 additions & 8 deletions Projects/Demo/Source/View/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void CViewInit::initScene()
camObj->addComponent<CEditorCamera>()->setMoveSpeed(2.0f);

CCamera *camera = camObj->getComponent<CCamera>();
camera->setPosition(core::vector3df(3.0f, 3.0f, 3.0f));
camera->setPosition(core::vector3df(2.0f, 1.0f, 2.0f));
camera->lookAt(core::vector3df(0.0f, 0.0f, 0.0f), core::vector3df(0.0f, 1.0f, 0.0f));

// sky
Expand All @@ -78,19 +78,19 @@ void CViewInit::initScene()
// grid
// zone->createEmptyObject()->addComponent<CGridPlane>();

/*
// sponza
/*
CMeshManager *meshManager = CMeshManager::getInstance();
CEntityPrefab *prefab = NULL;
std::vector<std::string> textureFolders;
textureFolders.push_back("Demo/Sponza/Textures");
// load model
CEntityPrefab *prefab = meshManager->loadModel("Demo/Sponza/Sponza.dae", textureFolders[0].c_str(), false);
prefab = meshManager->loadModel("Demo/Sponza/Sponza.dae", NULL, false);
if (prefab != NULL)
{
// export model material
// CMaterialManager::getInstance()->exportMaterial(prefab, "../Assets/Demo/Sponza", "Sponza.xml");
ArrayMaterial& materials = CMaterialManager::getInstance()->loadMaterial("Demo/Sponza/Sponza.xml", true, textureFolders);
// create render mesh object
Expand All @@ -108,8 +108,7 @@ void CViewInit::initScene()
CAnimationClip *animWalkForward = animManager->loadAnimation("Demo/Model3D/Hero@WalkForward.dae");
CAnimationClip *animRunForward = animManager->loadAnimation("Demo/Model3D/Hero@RunForward.dae");
CMeshManager *meshManager = CMeshManager::getInstance();
CEntityPrefab *prefab = meshManager->loadModel("Demo/Model3D/Hero.dae", "Demo/Model3D/Textures", false);
prefab = meshManager->loadModel("Demo/Model3D/Hero.dae", "Demo/Model3D/Textures", false);
if (prefab != NULL)
{
// instance object 1
Expand Down Expand Up @@ -191,8 +190,8 @@ void CViewInit::onUpdate()
// retry download
delete m_getFile;
m_getFile = NULL;
}
}
}
}
#else

#if defined(WINDOWS_STORE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ This file is part of the "Skylicht Engine".
!#
*/

#pragma once

#include "pch.h"
#include "CAnimationTimeline.h"

Expand Down
5 changes: 5 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterial.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ namespace Skylicht
m_owner = obj;
}

inline const char* getName()
{
return m_materialName.c_str();
}

CMaterial* clone(CGameObject *gameObject);

void deleteAllParams();
Expand Down
5 changes: 3 additions & 2 deletions Projects/Skylicht/Engine/Source/MeshManager/CMeshManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace Skylicht
output = new CEntityPrefab();

// add search texture path
importer->addTextureFolder(texturePath);
if (texturePath != NULL)
importer->addTextureFolder(texturePath);

// load model
if (importer->loadModel(resource, output, loadNormalMap, loadTexcoord2, createBatching) == true)
Expand All @@ -75,5 +76,5 @@ namespace Skylicht
}

return output;
}
}
}
36 changes: 31 additions & 5 deletions Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ namespace Skylicht

CRenderMesh::~CRenderMesh()
{
for (CMaterial *m : m_materials)
delete m;
m_materials.clear();

CEntityManager *entityManager = m_gameObject->getEntityManager();
for (u32 i = 0, n = m_entities.size(); i < n; i++)
{
entityManager->removeEntity(m_entities[i]);
}

m_entities.clear();
m_renderers.clear();
}

void CRenderMesh::initComponent()
Expand All @@ -74,7 +78,6 @@ namespace Skylicht

// map new entity index from src prefab
std::map<int, int> entityIndex;
std::vector<CRenderMeshData*> renderers;

// copy entity data
for (int i = 0; i < numEntities; i++)
Expand Down Expand Up @@ -114,7 +117,8 @@ namespace Skylicht
if (spawnRender->isSkinnedMesh() && spawnRender->isSoftwareSkinning() == true)
spawnRender->initSoftwareSkinning();

renderers.push_back(spawnRender);
// add to list renderer
m_renderers.push_back(spawnRender);

// add world inv transform for culling system
spawnEntity->addData<CWorldInvTransformData>();
Expand Down Expand Up @@ -147,7 +151,7 @@ namespace Skylicht
bool addInvData = false;

// re-map joint with new entity in CEntityManager
for (CRenderMeshData *&r : renderers)
for (CRenderMeshData *&r : m_renderers)
{
if (r->isSkinnedMesh() == true)
{
Expand Down Expand Up @@ -187,6 +191,28 @@ namespace Skylicht

void CRenderMesh::initMaterial(ArrayMaterial& materials)
{
int bufferID = 0;

for (CMaterial *m : materials)
{
CMaterial *material = m->clone(m_gameObject);
for (CRenderMeshData *renderer : m_renderers)
{
bufferID = 0;
CMesh *mesh = renderer->getMesh();
for (std::string& materialName : mesh->MaterialName)
{
if (materialName == m->getName())
{
material->addAffectMesh(mesh->getMeshBuffer(bufferID));
}
bufferID++;
}
}
m_materials.push_back(material);
}

for (CMaterial *m : m_materials)
m->applyMaterial();
}
}
14 changes: 13 additions & 1 deletion Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This file is part of the "Skylicht Engine".
#include "Components/CComponentSystem.h"
#include "Material/CMaterial.h"
#include "Material/CMaterialManager.h"
#include "RenderMesh/CRenderMeshData.h"

namespace Skylicht
{
Expand All @@ -37,8 +38,9 @@ namespace Skylicht
CEntity* m_root;
core::array<CEntity*> m_entities;

std::vector<CRenderMeshData*> m_renderers;

ArrayMaterial m_materials;
std::map<std::string, CMaterial*> m_materialName;
public:
CRenderMesh();

Expand All @@ -54,6 +56,16 @@ namespace Skylicht

void initMaterial(ArrayMaterial& materials);

inline int getMaterialCount()
{
return (int)m_materials.size();
}

CMaterial* getMaterial(int i)
{
return m_materials[i];
}

core::array<CEntity*>& getEntities()
{
return m_entities;
Expand Down

0 comments on commit 61d0be4

Please sign in to comment.