1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#pragma once
4
5#include "BsCorePrerequisites.h"
6#include "Reflection/BsIReflectable.h"
7#include "CoreThread/BsCoreObject.h"
8#include "Scene/BsSceneActor.h"
9
10namespace bs
11{
12 namespace ct
13 {
14 class RendererTask;
15 }
16
17 /** @addtogroup Implementation
18 * @{
19 */
20
21 /** Signals which portion of a Skybox is dirty. */
22 enum class SkyboxDirtyFlag
23 {
24 // First few bits reserved by ActorDiryFlag
25 Texture = 1 << 4
26 };
27
28
29 /** Base class for both core and sim thread implementations of a skybox. */
30 class BS_CORE_EXPORT SkyboxBase : public SceneActor
31 {
32 public:
33 SkyboxBase() = default;
34 virtual ~SkyboxBase() = default;
35
36 /**
37 * Brightness multiplier that will be applied to skybox values before they're being used. Allows you to make the
38 * skybox more or less bright. Equal to one by default.
39 */
40 void setBrightness(float brightness) { mBrightness = brightness; _markCoreDirty(); }
41
42 /** @copydoc setBrightness */
43 float getBrightness() const { return mBrightness; }
44
45 protected:
46 float mBrightness = 1.0f; /**< Multiplier to apply to evaluated skybox values before using them. */
47 };
48
49 /** Templated base class for both core and sim thread implementations of a skybox. */
50 template<bool Core>
51 class BS_CORE_EXPORT TSkybox : public SkyboxBase
52 {
53 public:
54 using TextureType = CoreVariantHandleType<Texture, Core>;
55
56 virtual ~TSkybox() = default;
57
58 /**
59 * Determines an environment map to use for sampling skybox radiance. Must be a cube-map texture, and should ideally
60 * contain HDR data.
61 */
62 TextureType getTexture() const { return mTexture; }
63
64 /** Enumerates all the fields in the type and executes the specified processor action for each field. */
65 template<class P>
66 void rttiEnumFields(P p);
67 protected:
68 TextureType mTexture;
69 };
70
71 /** @} */
72 /** @addtogroup Renderer-Internal
73 * @{
74 */
75
76 namespace ct { class Skybox; }
77
78 /** Allows you to specify an environment map to use for sampling radiance of the sky. */
79 class BS_CORE_EXPORT Skybox : public IReflectable, public CoreObject, public TSkybox<false>
80 {
81 public:
82 ~Skybox();
83
84 /** @copydoc TSkybox::getTexture */
85 void setTexture(const HTexture& texture);
86
87 /** Retrieves an implementation of the skybox usable only from the core thread. */
88 SPtr<ct::Skybox> getCore() const;
89
90 /** Creates a new skybox. */
91 static SPtr<Skybox> create();
92
93 protected:
94 Skybox();
95
96 /**
97 * Filters the skybox radiance texture, generating filtered radiance (for reflections) and irradiance. Should be
98 * called any time the skybox texture changes.
99 */
100 void filterTexture();
101
102 /** @copydoc CoreObject::createCore */
103 SPtr<ct::CoreObject> createCore() const override;
104
105 /** @copydoc SkyboxBase::_markCoreDirty */
106 void _markCoreDirty(ActorDirtyFlag flags = ActorDirtyFlag::Everything) override;
107
108 /** @copydoc CoreObject::syncToCore */
109 CoreSyncData syncToCore(FrameAlloc* allocator) override;
110
111 SPtr<Texture> mFilteredRadiance;
112 SPtr<Texture> mIrradiance;
113 SPtr<ct::RendererTask> mRendererTask;
114
115 /************************************************************************/
116 /* RTTI */
117 /************************************************************************/
118 public:
119 friend class SkyboxRTTI;
120 static RTTITypeBase* getRTTIStatic();
121 RTTITypeBase* getRTTI() const override;
122
123 /** Creates a new skybox instance without initializing it. */
124 static SPtr<Skybox> createEmpty();
125 };
126
127 namespace ct
128 {
129 /** Core thread usable version of a bs::Skybox */
130 class BS_CORE_EXPORT Skybox : public CoreObject, public TSkybox<true>
131 {
132 public:
133 ~Skybox();
134
135 /**
136 * Returns a texture containing filtered version of the radiance texture used for reflections. This might not
137 * be available if it hasn't been generated yet.
138 */
139 SPtr<Texture> getFilteredRadiance() const { return mFilteredRadiance; }
140
141 /**
142 * Returns a texture containing sky irradiance. This might not be available if it hasn't been generated yet.
143 */
144 SPtr<Texture> getIrradiance() const { return mIrradiance; }
145
146 protected:
147 friend class bs::Skybox;
148
149 Skybox(const SPtr<Texture>& radiance, const SPtr<Texture>& filteredRadiance, const SPtr<Texture>& irradiance);
150
151 /** @copydoc CoreObject::initialize */
152 void initialize() override;
153
154 /** @copydoc CoreObject::syncToCore */
155 void syncToCore(const CoreSyncData& data) override;
156
157 SPtr<Texture> mFilteredRadiance;
158 SPtr<Texture> mIrradiance;
159 };
160 }
161
162 /** @} */
163}
164