VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
scene.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 11.09.2025.
5//
6#pragma once
7
8#include <memory>
9#include <vector>
10
12#include "immediate/immediate.h"
15#include "scene/constants.h"
16#include "skybox/sky.h"
17
18namespace visutwin::canvas
19{
28 class Scene : public EventHandler
29 {
30 public:
31 static constexpr const char* EVENT_SETLAYERS = "set:layers";
32
33 Scene(const std::shared_ptr<GraphicsDevice>& graphicsDevice);
34 ~Scene() = default;
35
36 bool clusteredLightingEnabled() const { return _clusteredLightingEnabled; }
37 void setClusteredLightingEnabled(bool value) { _clusteredLightingEnabled = value; }
38
39 const LightingParams& lighting() const { return _lighting; }
40 const std::shared_ptr<LayerComposition>& layers() const { return _layers; }
41 const Color& ambientLight() const { return _ambientLight; }
42 const FogParams& fog() const { return _fog; }
43
44 void setAmbientLight(float r, float g, float b) { _ambientLight = Color(r, g, b); }
45
46 // Sets the mip level of the skybox to be displayed
47 void setSkyboxMip(int value);
48 int skyboxMip() const { return _skyboxMip; }
49
50 void setLayers(const std::shared_ptr<LayerComposition>& layers);
51
52 Immediate* immediate() const { return _immediate; }
53
54 void setSkyboxIntensity(float value);
55 float skyboxIntensity() const { return _skyboxIntensity; }
56
57 void setExposure(float value) { _exposure = value; }
58 float exposure() const { return _exposure; }
59
60 void setSkyType(int value);
61 int skyType() const { return _skyType; }
62
63 void setEnvAtlas(Texture* value);
64 Texture* envAtlas() const { return _envAtlas; }
65 Sky* sky() const { return _sky.get(); }
66
67 // high-res cubemap for skybox rendering.
68 // Separate from envAtlas which is only used for PBR lighting.
69 void setSkybox(Texture* value);
70 Texture* skybox() const { return _skyboxCubeMap; }
71
72 void setToneMapping(int value) { _toneMapping = value; }
73 int toneMapping() const { return _toneMapping; }
74
75 bool debugNormalMapsEnabled() const { return _debugNormalMapsEnabled; }
76 void setDebugNormalMapsEnabled(const bool enabled) { _debugNormalMapsEnabled = enabled; }
77 void setFogEnabled(const bool enabled) { _fog.enabled = enabled; }
78 void setFogColor(const Color& color) { _fog.color = color; }
79 void setFogLinear(const float start, const float end)
80 {
81 _fog.start = start;
82 _fog.end = end;
83 }
84 void setFogDensity(const float density) { _fog.density = density; }
85
90 void setPrefilteredCubemaps(const std::vector<Texture*>& cubemaps);
91
92 // Atmosphere scattering (Nishita).
93 void setAtmosphereEnabled(bool value) { _atmosphereEnabled = value; }
94 bool atmosphereEnabled() const { return _atmosphereEnabled; }
95
98 void setAtmosphereUniforms(const void* data, size_t size);
99
101 const void* atmosphereUniformData() const { return &_atmosphereUniforms; }
102 size_t atmosphereUniformSize() const { return sizeof(_atmosphereUniforms); }
103
104 private:
105 void resetSkyMesh();
106
107 std::shared_ptr<GraphicsDevice> _device;
108
109 // DEVIATION: disabled until clustered lighting (WorldClusters, LightTextureAtlas)
110 // is fully ported. With true, the non-clustered local shadow path in
111 // ForwardRenderer::buildFrameGraph is skipped and cullLocalLights is never called.
112 bool _clusteredLightingEnabled = false;
113
114 LightingParams _lighting;
115
116 // The color of the scene's ambient light, specified in sRGB color space
117 Color _ambientLight = Color(0, 0, 0);
118 FogParams _fog;
119
120 int _skyboxMip = 0;
121
122 // This flag indicates changes were made to the scene which may require recompilation of
123 // shaders that reference global settings
124 bool _updateShaders = true;
125
126 std::shared_ptr<LayerComposition> _layers;
127
128 std::unique_ptr<Sky> _sky;
129
130 Immediate* _immediate = nullptr;
131
132 float _skyboxIntensity = 1.0f;
133 float _exposure = 1.0f;
134 int _skyType = SKYTYPE_INFINITE;
135
136 Texture* _envAtlas = nullptr;
137 Texture* _skyboxCubeMap = nullptr;
138 int _toneMapping = TONEMAP_LINEAR;
139 bool _debugNormalMapsEnabled = false;
140 bool _atmosphereEnabled = false;
141
142 // Packed atmosphere uniforms (96 bytes). Layout matches GPU AtmosphereData.
143 struct alignas(16) AtmosphereUniformsStorage {
144 float data[24] = {};
145 } _atmosphereUniforms;
146
147 std::vector<Texture*> _prefilteredCubemaps;
148 Texture* _internalEnvAtlas = nullptr;
149 };
150}
size_t atmosphereUniformSize() const
Definition scene.h:102
void setDebugNormalMapsEnabled(const bool enabled)
Definition scene.h:76
Texture * envAtlas() const
Definition scene.h:64
const void * atmosphereUniformData() const
Access raw atmosphere uniform data for the device.
Definition scene.h:101
const Color & ambientLight() const
Definition scene.h:41
bool atmosphereEnabled() const
Definition scene.h:94
bool clusteredLightingEnabled() const
Definition scene.h:36
int toneMapping() const
Definition scene.h:73
void setLayers(const std::shared_ptr< LayerComposition > &layers)
Definition scene.cpp:37
void setPrefilteredCubemaps(const std::vector< Texture * > &cubemaps)
Definition scene.cpp:104
int skyType() const
Definition scene.h:61
float exposure() const
Definition scene.h:58
void setSkyboxIntensity(float value)
Definition scene.cpp:47
Immediate * immediate() const
Definition scene.h:52
const LightingParams & lighting() const
Definition scene.h:39
void setAmbientLight(float r, float g, float b)
Definition scene.h:44
int skyboxMip() const
Definition scene.h:48
void setClusteredLightingEnabled(bool value)
Definition scene.h:37
void setAtmosphereUniforms(const void *data, size_t size)
Definition scene.cpp:97
bool debugNormalMapsEnabled() const
Definition scene.h:75
float skyboxIntensity() const
Definition scene.h:55
void setSkybox(Texture *value)
Definition scene.cpp:89
Texture * skybox() const
Definition scene.h:70
Scene(const std::shared_ptr< GraphicsDevice > &graphicsDevice)
Definition scene.cpp:14
void setSkyType(int value)
Definition scene.cpp:54
Sky * sky() const
Definition scene.h:65
void setEnvAtlas(Texture *value)
Definition scene.cpp:65
void setFogDensity(const float density)
Definition scene.h:84
void setFogLinear(const float start, const float end)
Definition scene.h:79
void setFogEnabled(const bool enabled)
Definition scene.h:77
const FogParams & fog() const
Definition scene.h:42
const std::shared_ptr< LayerComposition > & layers() const
Definition scene.h:40
static constexpr const char * EVENT_SETLAYERS
Definition scene.h:31
void setAtmosphereEnabled(bool value)
Definition scene.h:93
void setSkyboxMip(int value)
Definition scene.cpp:19
void setExposure(float value)
Definition scene.h:57
void setFogColor(const Color &color)
Definition scene.h:78
void setToneMapping(int value)
Definition scene.h:72
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
RGBA color with floating-point components in [0, 1].
Definition color.h:18