diff --git a/Projects/Skylicht/Components/Source/Primitive/CPrimitive.cpp b/Projects/Skylicht/Components/Source/Primitive/CPrimitive.cpp index f0c730769..35534e3a6 100644 --- a/Projects/Skylicht/Components/Source/Primitive/CPrimitive.cpp +++ b/Projects/Skylicht/Components/Source/Primitive/CPrimitive.cpp @@ -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; + } + } } \ No newline at end of file diff --git a/Projects/Skylicht/Components/Source/Primitive/CPrimitive.h b/Projects/Skylicht/Components/Source/Primitive/CPrimitive.h index fef688dc1..5d0a7444b 100644 --- a/Projects/Skylicht/Components/Source/Primitive/CPrimitive.h +++ b/Projects/Skylicht/Components/Source/Primitive/CPrimitive.h @@ -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; diff --git a/Projects/Skylicht/Components/Source/Primitive/CPrimitiveRendererInstancing.cpp b/Projects/Skylicht/Components/Source/Primitive/CPrimitiveRendererInstancing.cpp index 9ba200dd1..c8f01c2cd 100644 --- a/Projects/Skylicht/Components/Source/Primitive/CPrimitiveRendererInstancing.cpp +++ b/Projects/Skylicht/Components/Source/Primitive/CPrimitiveRendererInstancing.cpp @@ -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)) @@ -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); } } diff --git a/Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp b/Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp index a8f4d7f81..b60cf6923 100644 --- a/Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp +++ b/Projects/Skylicht/Engine/Source/Material/CMaterialManager.cpp @@ -230,6 +230,10 @@ namespace Skylicht xmlRead->drop(); + // need update shader params + for (CMaterial* m : result) + m->updateShaderParams(); + return result; } diff --git a/Samples/Instancing/Source/CViewInit.cpp b/Samples/Instancing/Source/CViewInit.cpp index a4c46f3f0..fd048ff44 100644 --- a/Samples/Instancing/Source/CViewInit.cpp +++ b/Samples/Instancing/Source/CViewInit.cpp @@ -98,6 +98,7 @@ void CViewInit::initScene() std::vector 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; @@ -109,10 +110,8 @@ void CViewInit::initScene() CPlane* plane = ground->addComponent(); 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++)