VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
cameraComponent.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 05.09.2025.
5//
6#pragma once
7
8#include <algorithm>
9#include <vector>
10
11#include "core/math/matrix4.h"
15#include "scene/camera.h"
16#include "scene/constants.h"
17
18namespace visutwin::canvas
19{
21 class RenderPassTAA;
22
24 {
25 };
26
28 {
29 bool enabled = false;
30 bool nearBlur = false;
31 float focusDistance = 100.0f;
32 float focusRange = 10.0f;
33 float blurRadius = 3.0f;
34 int blurRings = 4;
36 bool highQuality = true;
37 };
38
40 {
41 bool enabled = false;
42 bool highQuality = true;
43 float jitter = 1.0f;
44 };
45
46 // SSAO settings matching upstream CameraFrame.ssao
48 {
49 bool enabled = false;
50 bool blurEnabled = true;
51 float intensity = 0.5f;
52 float power = 6.0f;
53 float radius = 30.0f;
54 int samples = 12;
55 float minAngle = 10.0f;
56 float scale = 1.0f;
57 bool randomize = false;
58 // SSAO compositing mode:
59 // "combine" — post-process compose pass (default, multiplies final image)
60 // "lighting" — per-material forward pass (modulates AO during PBR lighting)
61 std::string_view type = "combine";
62 };
63
64 // Rendering settings matching upstream CameraFrame.rendering
66 {
67 float renderTargetScale = 1.0f;
68 float sharpness = 0.0f;
69 float bloomIntensity = 0.0f; // 0 = disabled
71
72 // Vignette
73 bool vignetteEnabled = false;
74 float vignetteInner = 0.5f;
75 float vignetteOuter = 1.0f;
76 float vignetteCurvature = 0.5f;
77 float vignetteIntensity = 0.3f;
78 };
79
80 /*
81 * The CameraComponent enables an Entity to render the scene. A scene requires at least
82 * one enabled camera component to be rendered. The camera's view direction is along the negative
83 * z-axis of the owner entity.
84 */
86 {
87 public:
90
91 static const std::vector<CameraComponent*>& instances() { return _instances; }
92
93 const Matrix4& projectionMatrix() const { return _camera->projectionMatrix(); }
94
95 void initializeComponentData() override;
96
97 // Gets the render passes the camera uses for rendering, instead of its default rendering
98 const std::vector<std::shared_ptr<RenderPass>>& renderPasses() const { return _camera->renderPasses(); }
99
100 bool renderSceneColorMap() const { return _renderSceneColorMap > 0; }
101
102 bool renderSceneDepthMap() const { return _renderSceneDepthMap > 0; }
103
104 void requestSceneColorMap(bool enabled);
105 void requestSceneDepthMap(bool enabled);
106
107 Camera* camera() { return _camera; }
108
109 const Camera* camera() const { return _camera; }
110
111 void* onPostprocessing() const
112 {
113 return (_dof.enabled || _taa.enabled || _ssao.enabled || _rendering.bloomIntensity > 0.0f || _rendering.vignetteEnabled)
114 ? const_cast<CameraComponent*>(this) : nullptr;
115 }
116
117 const std::vector<int>& layers() const { return _layers; }
118 void setLayers(const std::vector<int>& layers) { _layers = layers; }
119
120 const DofSettings& dof() const { return _dof; }
121 DofSettings& dof() { return _dof; }
122 void setDofEnabled(bool enabled);
123 void setDof(const DofSettings& value)
124 {
125 setDofEnabled(value.enabled);
126 _dof = value;
127 }
128 std::shared_ptr<RenderTarget> dofSceneRenderTarget() const { return _dofSceneRenderTarget; }
129
130 const TaaSettings& taa() const { return _taa; }
131 TaaSettings& taa() { return _taa; }
132 void setTaaEnabled(bool enabled);
133 void setTaa(const TaaSettings& value)
134 {
135 setTaaEnabled(value.enabled);
136 _taa = value;
137 if (_camera) {
138 _camera->setJitter(_taa.enabled ? std::max(_taa.jitter, 0.0f) : 0.0f);
139 }
140 }
141 std::shared_ptr<RenderPassTAA> ensureTaaPass(const std::shared_ptr<GraphicsDevice>& device, Texture* sourceTexture);
142
143 const SsaoSettings& ssao() const { return _ssao; }
144 SsaoSettings& ssao() { return _ssao; }
145 void setSsaoEnabled(bool enabled);
146 void setSsao(const SsaoSettings& value)
147 {
148 setSsaoEnabled(value.enabled);
149 _ssao = value;
150 }
151
152 const RenderingSettings& rendering() const { return _rendering; }
153 RenderingSettings& rendering() { return _rendering; }
154 void setRendering(const RenderingSettings& value) { _rendering = value; }
155
156 // Persistent camera frame for postprocessing (TAA, DOF, etc.)
157 std::shared_ptr<RenderPassCameraFrame> cameraFrame() const { return _cameraFrame; }
158 void setCameraFrame(const std::shared_ptr<RenderPassCameraFrame>& frame) { _cameraFrame = frame; }
159
160 bool rendersLayer(const int layerId) const
161 {
162 if (_layers.empty()) {
163 return true;
164 }
165 return std::find(_layers.begin(), _layers.end(), layerId) != _layers.end();
166 }
167
168 private:
169 static std::vector<CameraComponent*> _instances;
170
171 Camera* _camera = nullptr;
172
173 // A counter of requests of color map rendering
174 int _renderSceneColorMap = 0;
175
176 // A counter of requests of depth map rendering
177 int _renderSceneDepthMap = 0;
178
179 DofSettings _dof;
180 TaaSettings _taa;
181 SsaoSettings _ssao;
182 RenderingSettings _rendering;
183 std::shared_ptr<Texture> _dofSceneColorTexture;
184 std::shared_ptr<RenderTarget> _dofSceneRenderTarget;
185 std::shared_ptr<RenderPassTAA> _taaPass;
186 std::shared_ptr<RenderPassCameraFrame> _cameraFrame;
187 void ensureDofRenderTarget();
188 bool requiresPostprocessRenderTarget() const { return _dof.enabled || _taa.enabled || _ssao.enabled; }
189 void updatePostprocessRenderTargetBinding() const;
190
191 // default camera layers include world, depth, skybox, UI and immediate.
193 };
194}
static const std::vector< CameraComponent * > & instances()
void setDof(const DofSettings &value)
void setRendering(const RenderingSettings &value)
void setCameraFrame(const std::shared_ptr< RenderPassCameraFrame > &frame)
const std::vector< int > & layers() const
CameraComponent(IComponentSystem *system, Entity *entity)
std::shared_ptr< RenderTarget > dofSceneRenderTarget() const
const DofSettings & dof() const
const Matrix4 & projectionMatrix() const
const TaaSettings & taa() const
const SsaoSettings & ssao() const
std::shared_ptr< RenderPassTAA > ensureTaaPass(const std::shared_ptr< GraphicsDevice > &device, Texture *sourceTexture)
const RenderingSettings & rendering() const
const std::vector< std::shared_ptr< RenderPass > > & renderPasses() const
std::shared_ptr< RenderPassCameraFrame > cameraFrame() const
bool rendersLayer(const int layerId) const
void setLayers(const std::vector< int > &layers)
void setTaa(const TaaSettings &value)
void setSsao(const SsaoSettings &value)
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
Entity * entity() const
Definition component.cpp:16
virtual bool enabled() const
Definition component.h:49
Component(IComponentSystem *system, Entity *entity)
Definition component.cpp:12
IComponentSystem * system() const
Definition component.h:47
ECS entity — a GraphNode that hosts components defining its behavior.
Definition entity.h:32
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
constexpr int LAYERID_DEPTH
Definition constants.h:18
constexpr int LAYERID_IMMEDIATE
Definition constants.h:27
constexpr int LAYERID_SKYBOX
Definition constants.h:21
constexpr int LAYERID_WORLD
Definition constants.h:15
constexpr int LAYERID_UI
Definition constants.h:24
4x4 column-major transformation matrix with SIMD acceleration.
Definition matrix4.h:31