From d4ed6d22b5a8531e995ab2f8477c32cf39cd814c Mon Sep 17 00:00:00 2001 From: apistol78 Date: Mon, 27 May 2024 16:01:36 +0200 Subject: [PATCH] Traktor: Implemented support for entity state in InstanceMeshComponent so we can hide/show in editor properly. --- code/Mesh/Instance/InstanceMeshComponent.cpp | 25 ++++++++++++++++++-- code/Mesh/Instance/InstanceMeshComponent.h | 2 ++ code/World/Entity/CullingComponent.cpp | 2 ++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/code/Mesh/Instance/InstanceMeshComponent.cpp b/code/Mesh/Instance/InstanceMeshComponent.cpp index 172ee920e2..dd8310df49 100644 --- a/code/Mesh/Instance/InstanceMeshComponent.cpp +++ b/code/Mesh/Instance/InstanceMeshComponent.cpp @@ -42,9 +42,8 @@ void InstanceMeshComponent::destroy() void InstanceMeshComponent::setWorld(world::World* world) { // Remove from last world. - if (m_world != nullptr) + if (m_world != nullptr && m_cullingInstance != nullptr) { - T_FATAL_ASSERT(m_cullingInstance != nullptr); world::CullingComponent* culling = m_world->getComponent< world::CullingComponent >(); culling->releaseInstance(m_cullingInstance); } @@ -60,6 +59,28 @@ void InstanceMeshComponent::setWorld(world::World* world) m_world = world; } +void InstanceMeshComponent::setState(const world::EntityState& state, const world::EntityState& mask) +{ + const bool visible = (state.visible && mask.visible); + if (visible) + { + if (!m_cullingInstance) + { + world::CullingComponent* culling = m_world->getComponent< world::CullingComponent >(); + m_cullingInstance = culling->allocateInstance(this, (intptr_t)m_mesh.getResource()); + m_cullingInstance->setTransform(m_transform.get0()); + } + } + else + { + if (m_cullingInstance) + { + world::CullingComponent* culling = m_world->getComponent< world::CullingComponent >(); + culling->releaseInstance(m_cullingInstance); + } + } +} + void InstanceMeshComponent::setTransform(const Transform& transform) { MeshComponent::setTransform(transform); diff --git a/code/Mesh/Instance/InstanceMeshComponent.h b/code/Mesh/Instance/InstanceMeshComponent.h index 8b75c2c4e8..dcb0a2689d 100644 --- a/code/Mesh/Instance/InstanceMeshComponent.h +++ b/code/Mesh/Instance/InstanceMeshComponent.h @@ -43,6 +43,8 @@ class T_DLLCLASS InstanceMeshComponent virtual void setWorld(world::World* world) override final; + virtual void setState(const world::EntityState& state, const world::EntityState& mask) override final; + virtual void setTransform(const Transform& transform) override final; virtual Aabb3 getBoundingBox() const override final; diff --git a/code/World/Entity/CullingComponent.cpp b/code/World/Entity/CullingComponent.cpp index 785b05a327..fbdd7ec248 100644 --- a/code/World/Entity/CullingComponent.cpp +++ b/code/World/Entity/CullingComponent.cpp @@ -200,9 +200,11 @@ void CullingComponent::releaseInstance(Instance*& instance) { T_FATAL_ASSERT(instance->owner == this); auto it = std::find(m_instances.begin(), m_instances.end(), instance); + T_FATAL_ASSERT(it != m_instances.end()); m_instances.erase(it); delete instance; instance = nullptr; + m_instanceBufferDirty = true; } void CullingComponent::Instance::setTransform(const Transform& transform)