WhiteBoxComponent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "WhiteBoxComponent.h"
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <Rendering/WhiteBoxMaterial.h>
  12. #include <Rendering/WhiteBoxRenderData.h>
  13. #include <Rendering/WhiteBoxRenderDataUtil.h>
  14. #include <Rendering/WhiteBoxRenderMeshInterface.h>
  15. #include <WhiteBox/WhiteBoxBus.h>
  16. namespace WhiteBox
  17. {
  18. void WhiteBoxComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. WhiteBoxRenderData::Reflect(context);
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<WhiteBoxComponent, AZ::Component>()->Version(1)->Field(
  24. "WhiteBoxRenderData", &WhiteBoxComponent::m_whiteBoxRenderData);
  25. }
  26. }
  27. WhiteBoxComponent::WhiteBoxComponent() = default;
  28. WhiteBoxComponent::~WhiteBoxComponent() = default;
  29. void WhiteBoxComponent::Activate()
  30. {
  31. WhiteBoxRequestBus::BroadcastResult(m_renderMesh, &WhiteBoxRequests::CreateRenderMeshInterface, GetEntityId());
  32. const AZ::EntityId entityId = GetEntityId();
  33. AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity();
  34. AZ::TransformBus::EventResult(worldFromLocal, entityId, &AZ::TransformBus::Events::GetWorldTM);
  35. // generate the mesh
  36. m_renderMesh->BuildMesh(m_whiteBoxRenderData, worldFromLocal);
  37. m_renderMesh->UpdateMaterial(m_whiteBoxRenderData.m_material);
  38. m_renderMesh->SetVisiblity(m_whiteBoxRenderData.m_material.m_visible);
  39. AzFramework::VisibleGeometryRequestBus::Handler::BusConnect(entityId);
  40. AZ::TransformNotificationBus::Handler::BusConnect(entityId);
  41. WhiteBoxComponentRequestBus::Handler::BusConnect(AZ::EntityComponentIdPair(entityId, GetId()));
  42. }
  43. void WhiteBoxComponent::Deactivate()
  44. {
  45. WhiteBoxComponentRequestBus::Handler::BusDisconnect();
  46. AZ::TransformNotificationBus::Handler::BusDisconnect();
  47. AzFramework::VisibleGeometryRequestBus::Handler::BusDisconnect();
  48. m_renderMesh.reset();
  49. }
  50. void WhiteBoxComponent::GenerateWhiteBoxMesh(const WhiteBoxRenderData& whiteBoxRenderData)
  51. {
  52. m_whiteBoxRenderData = whiteBoxRenderData;
  53. }
  54. void WhiteBoxComponent::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, const AZ::Transform& world)
  55. {
  56. m_renderMesh->UpdateTransform(world);
  57. }
  58. void WhiteBoxComponent::BuildVisibleGeometry(
  59. [[maybe_unused]] const AZ::Aabb& bounds, AzFramework::VisibleGeometryContainer& geometryContainer) const
  60. {
  61. // Convert the white box render data into visible geometry data
  62. const AzFramework::VisibleGeometry geometry = BuildVisibleGeometryFromWhiteBoxRenderData(GetEntityId(), m_whiteBoxRenderData);
  63. if (!geometry.m_indices.empty() && !geometry.m_vertices.empty())
  64. {
  65. geometryContainer.push_back(geometry);
  66. }
  67. }
  68. bool WhiteBoxComponent::WhiteBoxIsVisible() const
  69. {
  70. return m_renderMesh->IsVisible();
  71. }
  72. } // namespace WhiteBox