CameraNode.cpp 3.8 KB

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