Skip to content

Commit

Permalink
#150 Fix update texture & color on render instancing
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Aug 22, 2022
1 parent 7c24b83 commit b09b717
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
13 changes: 13 additions & 0 deletions Projects/Skylicht/Components/Source/Primitive/CPrimitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,17 @@ namespace Skylicht

return m_material;
}

void CPrimitive::setCustomMaterial(CMaterial* material)
{
if (material)
{
m_useCustomMaterial = true;
m_customMaterial = material;
}
else
{
m_useCustomMaterial = false;
}
}
}
17 changes: 17 additions & 0 deletions Projects/Skylicht/Components/Source/Primitive/CPrimitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,28 @@ namespace Skylicht

CMaterial* getMaterial();

void setCustomMaterial(CMaterial* material);

inline void setInstancing(bool b)
{
m_instancing = b;
}

inline bool isInstancing()
{
return m_instancing;
}

inline void setEnableNormalMap(bool b)
{
m_useNormalMap = b;
}

inline bool isUseNormalMap()
{
return m_useNormalMap;
}

inline CPrimiviteData::EPrimitive getType()
{
return m_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ namespace Skylicht

CMesh* mesh = it.first.Mesh;
CShader* shader = it.first.Shader;
ITexture* const* textures = it.first.Textures;

int materialType = shader->getInstancingShader()->getMaterialRenderID();

if (!rp->canRenderShader(shader))
Expand All @@ -219,6 +221,11 @@ namespace Skylicht

for (u32 i = 0, n = mesh->MeshBuffers.size(); i < n; i++)
{
video::SMaterial& irrMat = mesh->getMeshBuffer(i)->getMaterial();

for (int i = 0; i < MATERIAL_MAX_TEXTURES; i++)
irrMat.setTexture(i, textures[i]);

rp->drawInstancingMeshBuffer(mesh, i, materialType, entityManager, false);
}
}
Expand Down
4 changes: 4 additions & 0 deletions Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ namespace Skylicht

xmlRead->drop();

// need update shader params
for (CMaterial* m : result)
m->updateShaderParams();

return result;
}

Expand Down
7 changes: 3 additions & 4 deletions Samples/Instancing/Source/CViewInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void CViewInit::initScene()
std::vector<std::string> textureFolders;
ArrayMaterial& winTurbineMaterials = materialMgr->loadMaterial("SampleModels/WindTurbine/WindTurbine.mat", true, textureFolders);
ArrayMaterial& bladeTurbineMaterials = materialMgr->loadMaterial("SampleModels/WindTurbine/WindTurbine_Blades.mat", true, textureFolders);
ArrayMaterial& sandMaterials = materialMgr->loadMaterial("SampleModels/WindTurbine/Sand.mat", true, textureFolders);

SColor ambientColor(255, 190, 220, 250);
float space = 50.0f;
Expand All @@ -109,10 +110,8 @@ void CViewInit::initScene()
CPlane* plane = ground->addComponent<CPlane>();
plane->removeAllEntities();
plane->setInstancing(true);

CMaterial* material = plane->getMaterial();
material->setUniform4("uColor", SColor(255, 200, 200, 200));
material->updateShaderParams();
plane->setEnableNormalMap(true);
plane->setCustomMaterial(sandMaterials[0]);

int n = numObjectInRow / 2;
for (int x = -n; x < n; x++)
Expand Down

0 comments on commit b09b717

Please sign in to comment.