CameraNode.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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::kNone;
  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. frc->setShadowCascadeCount(getConfig().getSceneShadowCascadeCount());
  64. // Extended frustum for RT
  65. if(getSceneGraph().getGrManager().getDeviceCapabilities().m_rayTracingEnabled
  66. && getConfig().getSceneRayTracedShadows())
  67. {
  68. FrustumComponent* rtFrustumComponent = newComponent<FrustumComponent>();
  69. rtFrustumComponent->setFrustumType(FrustumType::kOrthographic);
  70. rtFrustumComponent->setEnabledVisibilityTests(FrustumComponentVisibilityTestFlag::RAY_TRACING_SHADOWS);
  71. const F32 dist = getConfig().getSceneRayTracingExtendedFrustumDistance();
  72. rtFrustumComponent->setOrthographic(0.1f, dist * 2.0f, dist, -dist, dist, -dist);
  73. rtFrustumComponent->setLodDistance(0, getConfig().getLod0MaxDistance());
  74. rtFrustumComponent->setLodDistance(1, getConfig().getLod1MaxDistance());
  75. }
  76. }
  77. void CameraNode::onMoveComponentUpdate(MoveComponent& move)
  78. {
  79. const Transform& worldTransform = move.getWorldTransform();
  80. // Frustum
  81. U count = 0;
  82. iterateComponentsOfType<FrustumComponent>([&](FrustumComponent& fc) {
  83. if(count == 0)
  84. {
  85. fc.setWorldTransform(worldTransform);
  86. }
  87. else
  88. {
  89. // Extended RT frustum, re-align it so the frustum is positioned at the center of the camera eye
  90. ANKI_ASSERT(fc.getFrustumType() == FrustumType::kOrthographic);
  91. const F32 far = fc.getFar();
  92. Transform extendedFrustumTransform = Transform::getIdentity();
  93. Vec3 newOrigin = worldTransform.getOrigin().xyz();
  94. newOrigin.z() += far / 2.0f;
  95. extendedFrustumTransform.setOrigin(newOrigin.xyz0());
  96. fc.setWorldTransform(extendedFrustumTransform);
  97. }
  98. ++count;
  99. });
  100. }
  101. PerspectiveCameraNode::PerspectiveCameraNode(SceneGraph* scene, CString name)
  102. : CameraNode(scene, name)
  103. {
  104. initCommon(FrustumType::kPerspective);
  105. }
  106. PerspectiveCameraNode::~PerspectiveCameraNode()
  107. {
  108. }
  109. OrthographicCameraNode::OrthographicCameraNode(SceneGraph* scene, CString name)
  110. : CameraNode(scene, name)
  111. {
  112. initCommon(FrustumType::kOrthographic);
  113. }
  114. OrthographicCameraNode::~OrthographicCameraNode()
  115. {
  116. }
  117. } // end namespace anki