From 6f7aef509093fd4546c3766668747e43876a4d3d Mon Sep 17 00:00:00 2001 From: "duc.phamhong" Date: Fri, 4 Oct 2024 14:17:27 +0700 Subject: [PATCH] Update save/load mesh instancing --- .../Engine/Entity/CEntityChildsData.cpp | 41 ++++++++++++++++++ .../Engine/Entity/CEntityChildsData.h | 43 +++++++++++++++++++ .../Skylicht/Engine/Entity/CEntityHandler.cpp | 35 ++++++++++++++- .../Skylicht/Engine/Entity/CEntityHandler.h | 1 + .../RenderMesh/CRenderMeshInstancing.cpp | 41 +++++++++++++++++- Samples/Spine2D/Source/CViewDemo.cpp | 2 +- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 Projects/Skylicht/Engine/Entity/CEntityChildsData.cpp create mode 100644 Projects/Skylicht/Engine/Entity/CEntityChildsData.h diff --git a/Projects/Skylicht/Engine/Entity/CEntityChildsData.cpp b/Projects/Skylicht/Engine/Entity/CEntityChildsData.cpp new file mode 100644 index 000000000..237393660 --- /dev/null +++ b/Projects/Skylicht/Engine/Entity/CEntityChildsData.cpp @@ -0,0 +1,41 @@ +/* +!@ +MIT License + +Copyright (c) 2022 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#include "pch.h" +#include "CEntityChildsData.h" + +namespace Skylicht +{ + IMPLEMENT_DATA_TYPE_INDEX(CEntityChildsData); + + CEntityChildsData::CEntityChildsData() + { + + } + + CEntityChildsData::~CEntityChildsData() + { + + } +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Entity/CEntityChildsData.h b/Projects/Skylicht/Engine/Entity/CEntityChildsData.h new file mode 100644 index 000000000..39b7d1cad --- /dev/null +++ b/Projects/Skylicht/Engine/Entity/CEntityChildsData.h @@ -0,0 +1,43 @@ +/* +!@ +MIT License + +Copyright (c) 2022 Skylicht Technology CO., LTD + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +This file is part of the "Skylicht Engine". +https://github.com/skylicht-lab/skylicht-engine +!# +*/ + +#pragma once + +#include "Entity/IEntityData.h" + +namespace Skylicht +{ + class SKYLICHT_API CEntityChildsData : public IEntityData + { + public: + core::array Childs; + + public: + CEntityChildsData(); + + virtual ~CEntityChildsData(); + }; + + DECLARE_PUBLIC_DATA_TYPE_INDEX(CEntityChildsData); +} \ No newline at end of file diff --git a/Projects/Skylicht/Engine/Entity/CEntityHandler.cpp b/Projects/Skylicht/Engine/Entity/CEntityHandler.cpp index 2aa6fe0df..e7714b881 100644 --- a/Projects/Skylicht/Engine/Entity/CEntityHandler.cpp +++ b/Projects/Skylicht/Engine/Entity/CEntityHandler.cpp @@ -25,6 +25,7 @@ This file is part of the "Skylicht Engine". #include "pch.h" #include "CEntityHandler.h" #include "CEntityHandleData.h" +#include "CEntityChildsData.h" #include "CEntityManager.h" #include "GameObject/CGameObject.h" #include "GameObject/CZone.h" @@ -104,9 +105,17 @@ namespace Skylicht { transformData->Name = name; transformData->ParentIndex = parent->getIndex(); + + CEntityChildsData* childs = GET_ENTITY_DATA(parent, CEntityChildsData); + if (childs == NULL) + childs = parent->addData(); + + childs->Childs.push_back(entity); } - m_entities.push_back(entity); + if (parent == NULL) + m_entities.push_back(entity); + return entity; } @@ -118,6 +127,8 @@ namespace Skylicht { if (m_entities[i] == entity) { + removeChilds(entity); + m_entities.erase(i); entityManager->removeEntity(entity); } @@ -134,10 +145,30 @@ namespace Skylicht return; for (int i = (int)m_entities.size() - 1; i >= 0; i--) - entityManager->removeEntity(m_entities[i]); + { + CEntity* entity = m_entities[i]; + + removeChilds(entity); + entityManager->removeEntity(entity); + } + m_entities.clear(); } + void CEntityHandler::removeChilds(CEntity* entity) + { + CEntityManager* entityManager = m_gameObject->getEntityManager(); + CEntityChildsData* childs = GET_ENTITY_DATA(entity, CEntityChildsData); + + if (childs) + { + u32 childCount = childs->Childs.size(); + for (u32 i = 0; i < childCount; i++) + entityManager->removeEntity(childs->Childs[i]); + childs->Childs.clear(); + } + } + void CEntityHandler::setEntities(CEntity** entities, u32 count) { m_entities.clear(); diff --git a/Projects/Skylicht/Engine/Entity/CEntityHandler.h b/Projects/Skylicht/Engine/Entity/CEntityHandler.h index 88a4caa19..0ff70f540 100644 --- a/Projects/Skylicht/Engine/Entity/CEntityHandler.h +++ b/Projects/Skylicht/Engine/Entity/CEntityHandler.h @@ -70,5 +70,6 @@ namespace Skylicht void setEntities(CEntity** entities, u32 count); + void removeChilds(CEntity* entity); }; } \ No newline at end of file diff --git a/Projects/Skylicht/Engine/RenderMesh/CRenderMeshInstancing.cpp b/Projects/Skylicht/Engine/RenderMesh/CRenderMeshInstancing.cpp index d66c9c1de..1df173da7 100644 --- a/Projects/Skylicht/Engine/RenderMesh/CRenderMeshInstancing.cpp +++ b/Projects/Skylicht/Engine/RenderMesh/CRenderMeshInstancing.cpp @@ -16,7 +16,7 @@ namespace Skylicht { ACTIVATOR_REGISTER(CRenderMeshInstancing); - CATEGORY_COMPONENT(CRenderMeshInstancing, "Mesh", "Renderer/Instancing"); + CATEGORY_COMPONENT(CRenderMeshInstancing, "Render Mesh Instancing", "Renderer"); CRenderMeshInstancing::CRenderMeshInstancing() : m_root(NULL), @@ -220,6 +220,23 @@ namespace Skylicht object->autoRelease(new CFilePathProperty(object, "mesh", m_meshFile.c_str(), meshExts)); object->autoRelease(new CFilePathProperty(object, "material", m_materialFile.c_str(), materialExts)); + + // save entities transform + CArraySerializable* entities = new CArraySerializable("Entities"); + object->addProperty(entities); + object->autoRelease(entities); + + int numPrimities = (int)m_entities.size(); + for (int i = 0; i < numPrimities; i++) + { + CMatrixProperty* transformData = new CMatrixProperty(entities, "transform"); + entities->autoRelease(transformData); + + // get world transform data + CWorldTransformData* world = GET_ENTITY_DATA(m_entities[i], CWorldTransformData); + transformData->set(world->Relative); + } + return object; } @@ -264,6 +281,28 @@ namespace Skylicht if (materials.size() > 0) initMaterial(materials); } + + // load entities + CArraySerializable* entities = (CArraySerializable*)object->getProperty("Entities"); + if (entities == NULL) + return; + + int numEntities = entities->getElementCount(); + + removeAllEntities(); + + for (int i = 0; i < numEntities; i++) + { + CMatrixProperty* transformData = (CMatrixProperty*)entities->getElement(i); + if (transformData == NULL) + return; + + CEntity* entity = spawn(); + + // set transform + CWorldTransformData* world = GET_ENTITY_DATA(entity, CWorldTransformData); + world->Relative = transformData->get(); + } } void CRenderMeshInstancing::refreshModelAndMaterial() diff --git a/Samples/Spine2D/Source/CViewDemo.cpp b/Samples/Spine2D/Source/CViewDemo.cpp index 20df79c9c..231ef27f0 100644 --- a/Samples/Spine2D/Source/CViewDemo.cpp +++ b/Samples/Spine2D/Source/CViewDemo.cpp @@ -111,7 +111,7 @@ void CViewDemo::renderSpine(CGUIElement* element) spine::Bone* crosshair = skeleton->findBone("crosshair"); if (crosshair) { - float x = 200.0f, y = 200; + float x = 200.0f, y = 200.0f; crosshair->setX(x); crosshair->setY(y); }