NAP
lightcomponent.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local includes
8 #include "renderfrustumcomponent.h"
9 
10 // External includes
11 #include <component.h>
12 #include <componentptr.h>
13 #include <cameracomponent.h>
14 #include <transformcomponent.h>
15 #include <rendergnomoncomponent.h>
16 #include <entity.h>
17 
18 namespace nap
19 {
20  // Forward declares
21  class LightComponentInstance;
22  class RenderAdvancedService;
23 
27  enum class ELightType : uint8
28  {
29  Custom = 0,
30  Directional = 1,
31  Point = 2,
32  Spot = 3
33  };
34 
38  enum class EShadowMapType : uint8
39  {
40  Quad = 0,
41  Cube = 1
42  };
43 
47  namespace uniform
48  {
49  inline constexpr const char* lightStruct = "light"; // Default light UBO struct name
50  inline constexpr const char* shadowStruct = "shadow"; // Default shadow UBO struct name
51 
52  namespace light
53  {
54  inline constexpr const char* color = "color";
55  inline constexpr const char* intensity = "intensity";
56  inline constexpr const char* origin = "origin";
57  inline constexpr const char* direction = "direction";
58  inline constexpr const char* attenuation = "attenuation";
59  inline constexpr const char* angle = "angle";
60  inline constexpr const char* falloff = "falloff";
61  inline constexpr const char* flags = "flags";
62  inline constexpr const char* lights = "lights";
63  inline constexpr const char* count = "count";
64  inline constexpr const uint defaultMemberCount = 8;
65  }
66 
67  namespace shadow
68  {
69  inline constexpr const char* lightViewProjectionMatrix = "lightViewProjectionMatrix";
70  inline constexpr const char* nearFar = "nearFar";
71  inline constexpr const char* strength = "strength";
72  inline constexpr const char* spread = "spread";
73  inline constexpr const char* flags = "flags";
74  inline constexpr const char* count = "count";
75  }
76  }
77 
81  namespace sampler
82  {
83  namespace light
84  {
85  inline constexpr const char* shadowMaps = "shadowMaps";
86  inline constexpr const char* cubeShadowMaps = "cubeShadowMaps";
87  }
88  }
89 
90 
137  class NAPAPI LightComponent : public Component
138  {
139  RTTI_ENABLE(Component)
140  DECLARE_COMPONENT(LightComponent, LightComponentInstance)
141  public:
145  struct Locator
146  {
147  float mLineWidth = 1.0f;
148  float mGnomonSize = 1.0f;
149  };
150 
155  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;
156 
157  bool mEnabled = true;
158  bool mCastShadows = false;
159  RGBColorFloat mColor = {1.0f, 1.0f, 1.0f};
160  float mIntensity = 1.0f;
161  float mShadowStrength = 1.0f;
162  float mShadowSpread = 2.0f;
164  };
165 
166 
214  {
215  friend class RenderAdvancedService;
216  RTTI_ENABLE(ComponentInstance)
217  public:
218  // Constructor
220  ComponentInstance(entity, resource) { }
221 
226  virtual ~LightComponentInstance() { };
227 
233  virtual bool init(utility::ErrorState& errorState) override;
234 
238  virtual void onDestroy() override;
239 
244  virtual void enable(bool enable) { mIsEnabled = enable; }
245 
249  bool isEnabled() const { return mIsEnabled; };
250 
256  bool canCastShadows() const { return mSpawnedCamera != nullptr; }
257 
261  bool castsShadows() const { return canCastShadows() && mIsShadowEnabled; }
262 
266  virtual ELightType getLightType() const = 0;
267 
271  virtual EShadowMapType getShadowMapType() const = 0;
272 
276  virtual uint getShadowMapSize() const { return mShadowMapSize; }
277 
281  virtual float getIntensity() const { return mIntensity; }
282 
286  void setIntensity(float intensity) { mIntensity = intensity; }
287 
291  virtual float getShadowStrength() const { return mShadowStrength; }
292 
296  virtual void setShadowStrength(float strength) { mShadowStrength = strength; }
297 
301  virtual float getShadowSpread() const { return mShadowSpread; }
302 
306  virtual void setShadowSpread(float spread) { mShadowSpread = spread; }
307 
311  virtual const RGBColorFloat& getColor() const { return mColor; }
312 
316  void setColor(const RGBColorFloat& color) { mColor = color; }
317 
321  const glm::vec3 getLightPosition() const { return math::extractPosition(getTransform().getGlobalTransform()); }
322 
326  const glm::vec3 getLightDirection() const { return -glm::normalize(getTransform().getGlobalTransform()[2]); }
327 
331  const TransformComponentInstance& getTransform() const { assert(mTransform != nullptr); return *mTransform; }
332 
336  TransformComponentInstance& getTransform() { assert(mTransform != nullptr); return *mTransform; }
337 
341  bool hasCamera() const { return mSpawnedCamera != nullptr; }
342 
346  CameraComponentInstance& getCamera() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
347 
351  CameraComponentInstance& getCamera() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
352 
356  const RenderGnomonComponentInstance& getGnomon() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
357 
361  RenderGnomonComponentInstance& getGnomon() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
362 
366  const RenderFrustumComponentInstance* getFrustrum() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
367 
371  RenderFrustumComponentInstance* getFrustrum() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
372 
373  float mIntensity = 1.0f;
374  RGBColorFloat mColor = { 1.0f, 1.0f, 1.0f };
375 
376  protected:
382  void registerUniformLightProperty(const std::string& memberName);
383 
393  SpawnedEntityInstance spawnShadowCamera(const nap::Entity& entity, nap::utility::ErrorState& error);
394 
395  LightComponent* mResource = nullptr;
396  TransformComponentInstance* mTransform = nullptr;
397  RenderAdvancedService* mService = nullptr;
399 
400  bool mIsEnabled = true;
401  bool mIsShadowEnabled = false;
402  float mShadowStrength = 1.0f;
403  float mShadowSpread = 2.0f;
404  uint mShadowMapSize = 512;
405 
406  private:
407  std::vector<nap::rtti::Property> mUniformList;
408  };
409 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::LightComponent::mLocator
Locator mLocator
Property: 'Locator' Locator settings.
Definition: lightcomponent.h:163
nap::LightComponentInstance::getFrustrum
const RenderFrustumComponentInstance * getFrustrum() const
Definition: lightcomponent.h:366
nap::uniform::shadow::spread
constexpr const char * spread
Definition: lightcomponent.h:72
nap::ELightType::Point
@ Point
nap::LightComponentInstance::getShadowStrength
virtual float getShadowStrength() const
Definition: lightcomponent.h:291
nap::RGBColor< float >
nap::LightComponentInstance::LightComponentInstance
LightComponentInstance(EntityInstance &entity, Component &resource)
Definition: lightcomponent.h:219
nap::LightComponentInstance
Definition: lightcomponent.h:213
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera() const
Definition: lightcomponent.h:346
nap::uniform::light::angle
constexpr const char * angle
Definition: lightcomponent.h:59
nap::LightComponentInstance::enable
virtual void enable(bool enable)
Definition: lightcomponent.h:244
nap::uniform::light::origin
constexpr const char * origin
Definition: lightcomponent.h:56
nap::LightComponentInstance::canCastShadows
bool canCastShadows() const
Definition: lightcomponent.h:256
nap::CameraComponentInstance
Definition: cameracomponent.h:38
nap::uniform::light::intensity
constexpr const char * intensity
Definition: lightcomponent.h:55
nap::ELightType::Directional
@ Directional
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::LightComponentInstance::castsShadows
bool castsShadows() const
Definition: lightcomponent.h:261
nap::LightComponentInstance::getGnomon
const RenderGnomonComponentInstance & getGnomon() const
Definition: lightcomponent.h:356
nap::LightComponent
Definition: lightcomponent.h:137
nap::uniform::light::attenuation
constexpr const char * attenuation
Definition: lightcomponent.h:58
nap::RenderAdvancedService
Definition: renderadvancedservice.h:99
nap::SpawnedEntityInstance
Definition: entity.h:245
nap::utility::ErrorState
Definition: errorstate.h:19
nap::LightComponentInstance::getFrustrum
RenderFrustumComponentInstance * getFrustrum()
Definition: lightcomponent.h:371
nap::EShadowMapType
EShadowMapType
Definition: lightcomponent.h:38
nap::LightComponentInstance::~LightComponentInstance
virtual ~LightComponentInstance()
Definition: lightcomponent.h:226
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera()
Definition: lightcomponent.h:351
nap::LightComponentInstance::getColor
virtual const RGBColorFloat & getColor() const
Definition: lightcomponent.h:311
nap::LightComponentInstance::isEnabled
bool isEnabled() const
Definition: lightcomponent.h:249
nap::LightComponentInstance::getIntensity
virtual float getIntensity() const
Definition: lightcomponent.h:281
nap::RenderGnomonComponentInstance
Definition: rendergnomoncomponent.h:47
nap::sampler::light::shadowMaps
constexpr const char * shadowMaps
Definition: lightcomponent.h:85
nap::LightComponentInstance::hasCamera
bool hasCamera() const
Definition: lightcomponent.h:341
nap::EShadowMapType::Cube
@ Cube
nap::LightComponentInstance::mSpawnedCamera
SpawnedEntityInstance mSpawnedCamera
Definition: lightcomponent.h:398
nap::uniform::shadow::flags
constexpr const char * flags
Definition: lightcomponent.h:73
nap::LightComponentInstance::getLightPosition
const glm::vec3 getLightPosition() const
Definition: lightcomponent.h:321
nap::getLightType
uint NAPAPI getLightType(LightFlags flags)
nap::LightComponentInstance::setShadowSpread
virtual void setShadowSpread(float spread)
Definition: lightcomponent.h:306
nap::uniform::light::direction
constexpr const char * direction
Definition: lightcomponent.h:57
nap::uniform::shadowStruct
constexpr const char * shadowStruct
Definition: lightcomponent.h:50
nap::uniform::shadow::count
constexpr const char * count
Definition: lightcomponent.h:74
nap::EntityInstance
Definition: entity.h:34
nap::LightComponentInstance::getTransform
TransformComponentInstance & getTransform()
Definition: lightcomponent.h:336
nap::uniform::light::falloff
constexpr const char * falloff
Definition: lightcomponent.h:60
nap::uniform::light::lights
constexpr const char * lights
Definition: lightcomponent.h:62
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap::ELightType
ELightType
Definition: lightcomponent.h:27
nap::uniform::shadow::strength
constexpr const char * strength
Definition: lightcomponent.h:71
nap::LightComponentInstance::getTransform
const TransformComponentInstance & getTransform() const
Definition: lightcomponent.h:331
nap::uniform::light::flags
constexpr const char * flags
Definition: lightcomponent.h:61
nap::math::extractPosition
glm::vec3 NAPAPI extractPosition(const glm::mat4x4 &matrix)
nap::ELightType::Spot
@ Spot
nap::LightComponent::Locator
Definition: lightcomponent.h:145
nap
Definition: templateapp.h:17
nap::uniform::light::color
constexpr const char * color
Definition: lightcomponent.h:54
nap::uniform::light::defaultMemberCount
constexpr const uint defaultMemberCount
Definition: lightcomponent.h:64
nap::ELightType::Custom
@ Custom
nap::uniform::shadow::nearFar
constexpr const char * nearFar
Definition: lightcomponent.h:70
nap::uniform::light::count
constexpr const char * count
Definition: lightcomponent.h:63
nap::LightComponentInstance::getLightDirection
const glm::vec3 getLightDirection() const
Definition: lightcomponent.h:326
nap::LightComponentInstance::getShadowSpread
virtual float getShadowSpread() const
Definition: lightcomponent.h:301
nap::uniform::lightStruct
constexpr const char * lightStruct
Definition: lightcomponent.h:49
nap::LightComponentInstance::setShadowStrength
virtual void setShadowStrength(float strength)
Definition: lightcomponent.h:296
nap::uniform::shadow::lightViewProjectionMatrix
constexpr const char * lightViewProjectionMatrix
Definition: lightcomponent.h:69
nap::LightComponentInstance::getShadowMapSize
virtual uint getShadowMapSize() const
Definition: lightcomponent.h:276
nap::LightComponentInstance::getGnomon
RenderGnomonComponentInstance & getGnomon()
Definition: lightcomponent.h:361
nap::TransformComponentInstance
Definition: transformcomponent.h:73
nap::RenderFrustumComponentInstance
Definition: renderfrustumcomponent.h:55
nap::Entity
Definition: entity.h:339
nap::LightComponentInstance::setColor
void setColor(const RGBColorFloat &color)
Definition: lightcomponent.h:316
nap::LightComponentInstance::setIntensity
void setIntensity(float intensity)
Definition: lightcomponent.h:286
nap::getShadowMapType
uint NAPAPI getShadowMapType(LightFlags flags)
nap::EShadowMapType::Quad
@ Quad
nap::sampler::light::cubeShadowMaps
constexpr const char * cubeShadowMaps
Definition: lightcomponent.h:86