Skip to content

Commit

Permalink
Update save/load mesh instancing
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 4, 2024
1 parent f58eae8 commit 6f7aef5
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 4 deletions.
41 changes: 41 additions & 0 deletions Projects/Skylicht/Engine/Entity/CEntityChildsData.cpp
Original file line number Diff line number Diff line change
@@ -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()
{

}
}
43 changes: 43 additions & 0 deletions Projects/Skylicht/Engine/Entity/CEntityChildsData.h
Original file line number Diff line number Diff line change
@@ -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<CEntity*> Childs;

public:
CEntityChildsData();

virtual ~CEntityChildsData();
};

DECLARE_PUBLIC_DATA_TYPE_INDEX(CEntityChildsData);
}
35 changes: 33 additions & 2 deletions Projects/Skylicht/Engine/Entity/CEntityHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<CEntityChildsData>();

childs->Childs.push_back(entity);
}

m_entities.push_back(entity);
if (parent == NULL)
m_entities.push_back(entity);

return entity;
}

Expand All @@ -118,6 +127,8 @@ namespace Skylicht
{
if (m_entities[i] == entity)
{
removeChilds(entity);

m_entities.erase(i);
entityManager->removeEntity(entity);
}
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions Projects/Skylicht/Engine/Entity/CEntityHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ namespace Skylicht

void setEntities(CEntity** entities, u32 count);

void removeChilds(CEntity* entity);
};
}
41 changes: 40 additions & 1 deletion Projects/Skylicht/Engine/RenderMesh/CRenderMeshInstancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Samples/Spine2D/Source/CViewDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 6f7aef5

Please sign in to comment.