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 "Math/BsVector3.h" |
8 | #include "Math/BsQuaternion.h" |
9 | #include "Image/BsColor.h" |
10 | #include "Math/BsSphere.h" |
11 | #include "CoreThread/BsCoreObject.h" |
12 | #include "Scene/BsSceneActor.h" |
13 | |
14 | namespace bs |
15 | { |
16 | /** @addtogroup Renderer-Internal |
17 | * @{ |
18 | */ |
19 | |
20 | /** Light type that determines how is light information parsed by the renderer and other systems. */ |
21 | enum BS_SCRIPT_EXPORT(m:Rendering) class LightType |
22 | { |
23 | Directional, |
24 | Radial, |
25 | Spot, |
26 | |
27 | Count BS_SCRIPT_EXPORT(ex:true) // Keep at end |
28 | }; |
29 | |
30 | /** @} */ |
31 | |
32 | /** @addtogroup Implementation |
33 | * @{ |
34 | */ |
35 | |
36 | /** Base class for both sim and core thread Light implementations. */ |
37 | class BS_CORE_EXPORT LightBase : public SceneActor |
38 | { |
39 | public: |
40 | LightBase(); |
41 | LightBase(LightType type, Color color, float intensity, float attRadius, float srcRadius, |
42 | bool castsShadows, Degree spotAngle, Degree spotFalloffAngle); |
43 | |
44 | virtual ~LightBase() { } |
45 | |
46 | /** Determines the type of the light. */ |
47 | LightType getType() const { return mType; } |
48 | |
49 | /** @copydoc getType() */ |
50 | void setType(LightType type) { mType = type; _markCoreDirty(); updateBounds(); } |
51 | |
52 | /** Determines does this light cast shadows when rendered. */ |
53 | void setCastsShadow(bool castsShadow) { mCastsShadows = castsShadow; _markCoreDirty(); } |
54 | |
55 | /** @copydoc setCastsShadow */ |
56 | bool getCastsShadow() const { return mCastsShadows; } |
57 | |
58 | /** |
59 | * Shadow bias determines offset at which the shadows are rendered from the shadow caster. Bias value of 0 means |
60 | * the shadow will be renderered exactly at the casters position. If your geometry has thin areas this will |
61 | * produce an artifact called shadow acne, in which case you can increase the shadow bias value to eliminate it. |
62 | * Note that increasing the shadow bias will on the other hand make the shadow be offset from the caster and may |
63 | * make the caster appear as if floating (Peter Panning artifact). Neither is perfect, so it is preferable to ensure |
64 | * all your geometry has thickness and keep the bias at zero, or even at negative values. |
65 | * |
66 | * Default value is 0.5. Should be in rough range [-1, 1]. |
67 | */ |
68 | void setShadowBias(float bias) { mShadowBias = bias; _markCoreDirty(); } |
69 | |
70 | /** @copydoc setShadowBias() */ |
71 | float getShadowBias() const { return mShadowBias; } |
72 | |
73 | /** Determines the color emitted by the light. */ |
74 | void setColor(const Color& color) { mColor = color; _markCoreDirty(); } |
75 | |
76 | /** @copydoc setColor() */ |
77 | Color getColor() const { return mColor; } |
78 | |
79 | /** |
80 | * Range at which the light contribution fades out to zero. Use setUseAutoAttenuation to provide a radius |
81 | * automatically dependant on light intensity. The radius will cut-off light contribution and therefore manually set |
82 | * very small radius can end up being very physically incorrect. |
83 | */ |
84 | void setAttenuationRadius(float radius); |
85 | |
86 | /** @copydoc setAttenuationRadius */ |
87 | float getAttenuationRadius() const { return mAttRadius; } |
88 | |
89 | /** |
90 | * Radius of the light source. If non-zero then this light represents an area light, otherwise it is a punctual |
91 | * light. Area lights have different attenuation then punctual lights, and their appearance in specular reflections |
92 | * is realistic. Shape of the area light depends on light type: |
93 | * - For directional light the shape is a disc projected on the hemisphere on the sky. This parameter |
94 | * represents angular radius (in degrees) of the disk and should be very small (think of how much space the Sun |
95 | * takes on the sky - roughly 0.25 degree radius). |
96 | * - For radial light the shape is a sphere and the source radius is the radius of the sphere. |
97 | * - For spot lights the shape is a disc oriented in the direction of the spot light and the source radius is the |
98 | * radius of the disc. |
99 | */ |
100 | void setSourceRadius(float radius); |
101 | |
102 | /** @copydoc setSourceRadius */ |
103 | float getSourceRadius() const { return mSourceRadius; } |
104 | |
105 | /** |
106 | * If enabled the attenuation radius will automatically be controlled in order to provide reasonable light radius, |
107 | * depending on its intensity. |
108 | */ |
109 | void setUseAutoAttenuation(bool enabled); |
110 | |
111 | /** @copydoc setUseAutoAttenuation */ |
112 | bool getUseAutoAttenuation() const { return mAutoAttenuation; } |
113 | |
114 | /** |
115 | * Determines the power of the light source. This will be luminous flux for radial & spot lights, |
116 | * luminance for directional lights with no area, and illuminance for directional lights with area (non-zero source |
117 | * radius). |
118 | */ |
119 | void setIntensity(float intensity); |
120 | |
121 | /** @copydoc setIntensity */ |
122 | float getIntensity() const { return mIntensity; } |
123 | |
124 | /** Determines the total angle covered by a spot light. */ |
125 | void setSpotAngle(const Degree& spotAngle) { mSpotAngle = spotAngle; _markCoreDirty(); updateBounds(); } |
126 | |
127 | /** @copydoc setSpotAngle */ |
128 | Degree getSpotAngle() const { return mSpotAngle; } |
129 | |
130 | /** |
131 | * Determines the falloff angle covered by a spot light. Falloff angle determines at what point does light intensity |
132 | * starts quadratically falling off as the angle approaches the total spot angle. |
133 | */ |
134 | void setSpotFalloffAngle(const Degree& spotFallofAngle) |
135 | { mSpotFalloffAngle = spotFallofAngle; _markCoreDirty(); updateBounds(); } |
136 | |
137 | /** @copydoc setSpotFalloffAngle */ |
138 | Degree getSpotFalloffAngle() const { return mSpotFalloffAngle; } |
139 | |
140 | /** Returns world space bounds that completely encompass the light's area of influence. */ |
141 | Sphere getBounds() const { return mBounds; } |
142 | |
143 | /** |
144 | * Returns the luminance of the light source. This is the value that should be used in lighting equations. |
145 | * |
146 | * @note |
147 | * For point light sources this method returns luminous intensity and not luminance. We use the same method for both |
148 | * as a convenience since in either case its used as a measure of intensity in lighting equations. |
149 | */ |
150 | float getLuminance() const; |
151 | |
152 | /** Enumerates all the fields in the type and executes the specified processor action for each field. */ |
153 | template<class P> |
154 | void rttiEnumFields(P p); |
155 | |
156 | protected: |
157 | /** Updates the internal bounds for the light. Call this whenever a property affecting the bounds changes. */ |
158 | void updateBounds(); |
159 | |
160 | /** Calculates maximum light range based on light intensity. */ |
161 | void updateAttenuationRange(); |
162 | |
163 | /** @copydoc SceneActor::setTransform */ |
164 | void setTransform(const Transform& transform) override; |
165 | |
166 | LightType mType; /**< Type of light that determines how are the rest of the parameters interpreted. */ |
167 | bool mCastsShadows; /**< Determines whether the light casts shadows. */ |
168 | Color mColor; /**< Color of the light. */ |
169 | float mAttRadius; /**< Radius at which light intensity falls off to zero. */ |
170 | float mSourceRadius; /**< Radius of the light source. If > 0 the light is an area light. */ |
171 | float mIntensity; /**< Power of the light source. @see setIntensity. */ |
172 | Degree mSpotAngle; /**< Total angle covered by a spot light. */ |
173 | Degree mSpotFalloffAngle; /**< Spot light angle at which falloff starts. Must be smaller than total angle. */ |
174 | Sphere mBounds; /**< Sphere that bounds the light area of influence. */ |
175 | bool mAutoAttenuation; /**< Determines is attenuation radius is automatically determined. */ |
176 | float mShadowBias; /**< See setShadowBias() */ |
177 | }; |
178 | |
179 | /** @} */ |
180 | /** @addtogroup Renderer-Internal |
181 | * @{ |
182 | */ |
183 | |
184 | namespace ct { class Light; } |
185 | |
186 | /** Illuminates a portion of the scene covered by the light. */ |
187 | class BS_CORE_EXPORT Light : public IReflectable, public CoreObject, public LightBase |
188 | { |
189 | public: |
190 | /** Retrieves an implementation of the light usable only from the core thread. */ |
191 | SPtr<ct::Light> getCore() const; |
192 | |
193 | /** |
194 | * Creates a new light with provided settings. |
195 | * |
196 | * @param[in] type Type of light that determines how are the rest of the parameters interpreted. |
197 | * @param[in] color Color of the light. |
198 | * @param[in] intensity Power of the light source. This will be luminous flux for radial & spot lights, |
199 | * luminance for directional lights with no area, and illuminance for directional |
200 | * lights with area (non-zero source radius). |
201 | * @param[in] attRadius Radius at which light's influence fades out to zero. |
202 | * @param[in] castsShadows Determines whether the light casts shadows. |
203 | * @param[in] spotAngle Total angle covered by a spot light. |
204 | * @param[in] spotFalloffAngle Spot light angle at which falloff starts. Must be smaller than total angle. |
205 | */ |
206 | static SPtr<Light> create(LightType type = LightType::Radial, Color color = Color::White, |
207 | float intensity = 100.0f, float attRadius = 10.0f, bool castsShadows = false, |
208 | Degree spotAngle = Degree(45), Degree spotFalloffAngle = Degree(40)); |
209 | |
210 | protected: |
211 | Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, |
212 | bool castsShadows, Degree spotAngle, Degree spotFalloffAngle); |
213 | |
214 | /** @copydoc CoreObject::createCore */ |
215 | SPtr<ct::CoreObject> createCore() const override; |
216 | |
217 | /** @copydoc LightBase::_markCoreDirty */ |
218 | void _markCoreDirty(ActorDirtyFlag flag = ActorDirtyFlag::Everything) override; |
219 | |
220 | /** @copydoc CoreObject::syncToCore */ |
221 | CoreSyncData syncToCore(FrameAlloc* allocator) override; |
222 | |
223 | /** Creates a light with without initializing it. Used for serialization. */ |
224 | static SPtr<Light> createEmpty(); |
225 | |
226 | /************************************************************************/ |
227 | /* RTTI */ |
228 | /************************************************************************/ |
229 | public: |
230 | friend class LightRTTI; |
231 | static RTTITypeBase* getRTTIStatic(); |
232 | RTTITypeBase* getRTTI() const override; |
233 | |
234 | protected: |
235 | Light() = default; // Serialization only |
236 | }; |
237 | |
238 | namespace ct |
239 | { |
240 | /** Core thread usable version of bs::Light. */ |
241 | class BS_CORE_EXPORT Light : public CoreObject, public LightBase |
242 | { |
243 | public: |
244 | ~Light(); |
245 | |
246 | /** Sets an ID that can be used for uniquely identifying this object by the renderer. */ |
247 | void setRendererId(UINT32 id) { mRendererId = id; } |
248 | |
249 | /** Retrieves an ID that can be used for uniquely identifying this object by the renderer. */ |
250 | UINT32 getRendererId() const { return mRendererId; } |
251 | |
252 | static const UINT32 LIGHT_CONE_NUM_SIDES; |
253 | static const UINT32 LIGHT_CONE_NUM_SLICES; |
254 | protected: |
255 | friend class bs::Light; |
256 | |
257 | Light(LightType type, Color color, float intensity, float attRadius, float srcRadius, bool castsShadows, |
258 | Degree spotAngle, Degree spotFalloffAngle); |
259 | |
260 | /** @copydoc CoreObject::initialize */ |
261 | void initialize() override; |
262 | |
263 | /** @copydoc CoreObject::syncToCore */ |
264 | void syncToCore(const CoreSyncData& data) override; |
265 | |
266 | UINT32 mRendererId; |
267 | }; |
268 | } |
269 | |
270 | /** @} */ |
271 | } |
272 | |