2
0

CameraNode.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/CameraNode.h>
  6. #include <AnKi/Scene/Components/FrustumComponent.h>
  7. #include <AnKi/Scene/Components/MoveComponent.h>
  8. #include <AnKi/Scene/Components/SpatialComponent.h>
  9. #include <AnKi/Scene/SceneGraph.h>
  10. #include <AnKi/Core/ConfigSet.h>
  11. namespace anki {
  12. /// Feedback component.
  13. class CameraNode::MoveFeedbackComponent : public SceneComponent
  14. {
  15. ANKI_SCENE_COMPONENT(CameraNode::MoveFeedbackComponent)
  16. public:
  17. MoveFeedbackComponent(SceneNode* node)
  18. : SceneComponent(node, getStaticClassId(), true)
  19. {
  20. }
  21. Error update(SceneComponentUpdateInfo& info, Bool& updated) override
  22. {
  23. updated = false;
  24. MoveComponent& move = info.m_node->getFirstComponentOfType<MoveComponent>();
  25. if(move.getTimestamp() == info.m_node->getGlobalTimestamp())
  26. {
  27. CameraNode& cam = static_cast<CameraNode&>(*info.m_node);
  28. cam.onMoveComponentUpdate(move);
  29. }
  30. return Error::NONE;
  31. }
  32. };
  33. ANKI_SCENE_COMPONENT_STATICS(CameraNode::MoveFeedbackComponent)
  34. CameraNode::CameraNode(SceneGraph* scene, CString name)
  35. : SceneNode(scene, name)
  36. {
  37. }
  38. CameraNode::~CameraNode()
  39. {
  40. }
  41. void CameraNode::initCommon(FrustumType frustumType)
  42. {
  43. // Move component
  44. newComponent<MoveComponent>();
  45. // Feedback component
  46. newComponent<MoveFeedbackComponent>();
  47. // Frustum component
  48. FrustumComponent* frc = newComponent<FrustumComponent>();
  49. frc->setFrustumType(frustumType);
  50. const FrustumComponentVisibilityTestFlag visibilityFlags =
  51. FrustumComponentVisibilityTestFlag::RENDER_COMPONENTS | FrustumComponentVisibilityTestFlag::LIGHT_COMPONENTS
  52. | FrustumComponentVisibilityTestFlag::LENS_FLARE_COMPONENTS
  53. | FrustumComponentVisibilityTestFlag::REFLECTION_PROBES | FrustumComponentVisibilityTestFlag::REFLECTION_PROXIES
  54. | FrustumComponentVisibilityTestFlag::OCCLUDERS | FrustumComponentVisibilityTestFlag::DECALS
  55. | FrustumComponentVisibilityTestFlag::FOG_DENSITY_COMPONENTS
  56. | FrustumComponentVisibilityTestFlag::GLOBAL_ILLUMINATION_PROBES | FrustumComponentVisibilityTestFlag::EARLY_Z
  57. | FrustumComponentVisibilityTestFlag::ALL_SHADOWS_ENABLED
  58. | FrustumComponentVisibilityTestFlag::GENERIC_COMPUTE_JOB_COMPONENTS
  59. | FrustumComponentVisibilityTestFlag::UI_COMPONENTS | FrustumComponentVisibilityTestFlag::SKYBOX;
  60. frc->setEnabledVisibilityTests(visibilityFlags);
  61. frc->setLodDistance(0, getConfig().getLod0MaxDistance());
  62. frc->setLodDistance(1, getConfig().getLod1MaxDistance());
  63. // Extended frustum for RT
  64. if(getSceneGraph().getGrManager().getDeviceCapabilities().m_rayTracingEnabled
  65. && getConfig().getSceneRayTracedShadows())
  66. {
  67. FrustumComponent* rtFrustumComponent = newComponent<FrustumComponent>();
  68. rtFrustumComponent->setFrustumType(FrustumType::ORTHOGRAPHIC);
  69. rtFrustumComponent->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::RAY_TRACING_SHADOWS);
  70. const F32 dist = getConfig().getSceneRayTracingExtendedFrustumDistance();
  71. rtFrustumComponent->setOrthographic(0.1f, dist * 2.0f, dist, -dist, dist, -dist);
  72. rtFrustumComponent->setLodDistance(0, getConfig().getLod0MaxDistance());
  73. rtFrustumComponent->setLodDistance(1, getConfig().getLod1MaxDistance());
  74. }
  75. }
  76. void CameraNode::onMoveComponentUpdate(MoveComponent& move)
  77. {
  78. const Transform& worldTransform = move.getWorldTransform();
  79. // Frustum
  80. U count = 0;
  81. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fc) {
  82. if(count == 0)
  83. {
  84. fc.setWorldTransform(worldTransform);
  85. }
  86. else
  87. {
  88. // Extended RT frustum, re-align it so the frustum is positioned at the center of the camera eye
  89. ANKI_ASSERT(fc.getFrustumType() == FrustumType::ORTHOGRAPHIC);
  90. const F32 far = fc.getFar();
  91. Transform extendedFrustumTransform = Transform::getIdentity();
  92. Vec3 newOrigin = worldTransform.getOrigin().xyz();
  93. newOrigin.z() += far / 2.0f;
  94. extendedFrustumTransform.setOrigin(newOrigin.xyz0());
  95. fc.setWorldTransform(extendedFrustumTransform);
  96. }
  97. ++count;
  98. });
  99. }
  100. PerspectiveCameraNode::PerspectiveCameraNode(SceneGraph* scene, CString name)
  101. : CameraNode(scene, name)
  102. {
  103. initCommon(FrustumType::PERSPECTIVE);
  104. }
  105. PerspectiveCameraNode::~PerspectiveCameraNode()
  106. {
  107. }
  108. OrthographicCameraNode::OrthographicCameraNode(SceneGraph* scene, CString name)
  109. : CameraNode(scene, name)
  110. {
  111. initCommon(FrustumType::ORTHOGRAPHIC);
  112. }
  113. OrthographicCameraNode::~OrthographicCameraNode()
  114. {
  115. }
  116. } // end namespace anki