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 // External includes
8 #include <component.h>
9 #include <componentptr.h>
10 #include <cameracomponent.h>
11 #include <transformcomponent.h>
12 #include <renderfrustumcomponent.h>
13 #include <rendergnomoncomponent.h>
14 #include <entity.h>
15 
16 namespace nap
17 {
18  // Forward declares
19  class LightComponentInstance;
20  class RenderAdvancedService;
21 
25  enum class ELightType : uint8
26  {
27  Custom = 0,
28  Directional = 1,
29  Point = 2,
30  Spot = 3
31  };
32 
36  enum class EShadowMapType : uint8
37  {
38  Quad = 0,
39  Cube = 1
40  };
41 
45  namespace uniform
46  {
47  inline constexpr const char* lightStruct = "light"; // Default light UBO struct name
48  inline constexpr const char* shadowStruct = "shadow"; // Default shadow UBO struct name
49 
50  namespace light
51  {
52  inline constexpr const char* color = "color";
53  inline constexpr const char* intensity = "intensity";
54  inline constexpr const char* origin = "origin";
55  inline constexpr const char* direction = "direction";
56  inline constexpr const char* attenuation = "attenuation";
57  inline constexpr const char* angle = "angle";
58  inline constexpr const char* falloff = "falloff";
59  inline constexpr const char* flags = "flags";
60  inline constexpr const char* lights = "lights";
61  inline constexpr const char* count = "count";
62  inline constexpr const uint defaultMemberCount = 8;
63  }
64 
65  namespace shadow
66  {
67  inline constexpr const char* lightViewProjectionMatrix = "lightViewProjectionMatrix";
68  inline constexpr const char* nearFar = "nearFar";
69  inline constexpr const char* strength = "strength";
70  inline constexpr const char* spread = "spread";
71  inline constexpr const char* flags = "flags";
72  inline constexpr const char* count = "count";
73  }
74  }
75 
79  namespace sampler
80  {
81  namespace light
82  {
83  inline constexpr const char* shadowMaps = "shadowMaps";
84  inline constexpr const char* cubeShadowMaps = "cubeShadowMaps";
85  }
86  }
87 
88 
135  class NAPAPI LightComponent : public Component
136  {
137  RTTI_ENABLE(Component)
138  DECLARE_COMPONENT(LightComponent, LightComponentInstance)
139  public:
143  struct Locator
144  {
145  float mLineWidth = 1.0f;
146  float mGnomonSize = 1.0f;
147  };
148 
153  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;
154 
155  bool mEnabled = true;
156  bool mCastShadows = false;
157  RGBColorFloat mColor = {1.0f, 1.0f, 1.0f};
158  float mIntensity = 1.0f;
159  float mShadowStrength = 1.0f;
160  float mShadowSpread = 2.0f;
162  };
163 
164 
212  {
213  friend class RenderAdvancedService;
214  RTTI_ENABLE(ComponentInstance)
215  public:
216  // Constructor
218  ComponentInstance(entity, resource) { }
219 
224  virtual ~LightComponentInstance() { };
225 
231  virtual bool init(utility::ErrorState& errorState) override;
232 
236  virtual void onDestroy() override;
237 
242  virtual void enable(bool enable) { mIsEnabled = enable; }
243 
247  bool isEnabled() const { return mIsEnabled; };
248 
254  bool canCastShadows() const { return mSpawnedCamera != nullptr; }
255 
259  bool castsShadows() const { return canCastShadows() && mIsShadowEnabled; }
260 
264  virtual ELightType getLightType() const = 0;
265 
269  virtual EShadowMapType getShadowMapType() const = 0;
270 
274  virtual uint getShadowMapSize() const { return mShadowMapSize; }
275 
279  virtual float getIntensity() const { return mIntensity; }
280 
284  void setIntensity(float intensity) { mIntensity = intensity; }
285 
289  virtual float getShadowStrength() const { return mShadowStrength; }
290 
294  virtual void setShadowStrength(float strength) { mShadowStrength = strength; }
295 
299  virtual float getShadowSpread() const { return mShadowSpread; }
300 
304  virtual void setShadowSpread(float spread) { mShadowSpread = spread; }
305 
309  virtual const RGBColorFloat& getColor() const { return mColor; }
310 
314  void setColor(const RGBColorFloat& color) { mColor = color; }
315 
319  const glm::vec3 getLightPosition() const { return math::extractPosition(getTransform().getGlobalTransform()); }
320 
324  const glm::vec3 getLightDirection() const { return -glm::normalize(getTransform().getGlobalTransform()[2]); }
325 
329  const TransformComponentInstance& getTransform() const { assert(mTransform != nullptr); return *mTransform; }
330 
334  TransformComponentInstance& getTransform() { assert(mTransform != nullptr); return *mTransform; }
335 
339  bool hasCamera() const { return mSpawnedCamera != nullptr; }
340 
344  CameraComponentInstance& getCamera() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
345 
349  CameraComponentInstance& getCamera() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
350 
354  const RenderGnomonComponentInstance& getGnomon() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
355 
359  RenderGnomonComponentInstance& getGnomon() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
360 
364  const RenderFrustumComponentInstance* getFrustrum() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
365 
369  RenderFrustumComponentInstance* getFrustrum() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
370 
371  float mIntensity = 1.0f;
372  RGBColorFloat mColor = { 1.0f, 1.0f, 1.0f };
373 
374  protected:
380  void registerUniformLightProperty(const std::string& memberName);
381 
391  SpawnedEntityInstance spawnShadowCamera(const nap::Entity& entity, nap::utility::ErrorState& error);
392 
393  LightComponent* mResource = nullptr;
394  TransformComponentInstance* mTransform = nullptr;
395  RenderAdvancedService* mService = nullptr;
397 
398  bool mIsEnabled = true;
399  bool mIsShadowEnabled = false;
400  float mShadowStrength = 1.0f;
401  float mShadowSpread = 2.0f;
402  uint mShadowMapSize = 512;
403 
404  private:
405  std::vector<nap::rtti::Property> mUniformList;
406  };
407 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::LightComponent::mLocator
Locator mLocator
Property: 'Locator' Locator settings.
Definition: lightcomponent.h:161
nap::LightComponentInstance::getFrustrum
const RenderFrustumComponentInstance * getFrustrum() const
Definition: lightcomponent.h:364
nap::uniform::shadow::spread
constexpr const char * spread
Definition: lightcomponent.h:70
nap::ELightType::Point
@ Point
nap::LightComponentInstance::getShadowStrength
virtual float getShadowStrength() const
Definition: lightcomponent.h:289
nap::RGBColor< float >
nap::LightComponentInstance::LightComponentInstance
LightComponentInstance(EntityInstance &entity, Component &resource)
Definition: lightcomponent.h:217
nap::LightComponentInstance
Definition: lightcomponent.h:211
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera() const
Definition: lightcomponent.h:344
nap::uniform::light::angle
constexpr const char * angle
Definition: lightcomponent.h:57
nap::LightComponentInstance::enable
virtual void enable(bool enable)
Definition: lightcomponent.h:242
nap::uniform::light::origin
constexpr const char * origin
Definition: lightcomponent.h:54
nap::LightComponentInstance::canCastShadows
bool canCastShadows() const
Definition: lightcomponent.h:254
nap::CameraComponentInstance
Definition: cameracomponent.h:38
nap::uniform::light::intensity
constexpr const char * intensity
Definition: lightcomponent.h:53
nap::ELightType::Directional
@ Directional
nap::uint8
uint8_t uint8
Definition: numeric.h:16
nap::LightComponentInstance::castsShadows
bool castsShadows() const
Definition: lightcomponent.h:259
nap::LightComponentInstance::getGnomon
const RenderGnomonComponentInstance & getGnomon() const
Definition: lightcomponent.h:354
nap::LightComponent
Definition: lightcomponent.h:135
nap::uniform::light::attenuation
constexpr const char * attenuation
Definition: lightcomponent.h:56
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:369
nap::EShadowMapType
EShadowMapType
Definition: lightcomponent.h:36
nap::LightComponentInstance::~LightComponentInstance
virtual ~LightComponentInstance()
Definition: lightcomponent.h:224
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera()
Definition: lightcomponent.h:349
nap::LightComponentInstance::getColor
virtual const RGBColorFloat & getColor() const
Definition: lightcomponent.h:309
nap::LightComponentInstance::isEnabled
bool isEnabled() const
Definition: lightcomponent.h:247
nap::LightComponentInstance::getIntensity
virtual float getIntensity() const
Definition: lightcomponent.h:279
nap::RenderGnomonComponentInstance
Definition: rendergnomoncomponent.h:47
nap::sampler::light::shadowMaps
constexpr const char * shadowMaps
Definition: lightcomponent.h:83
nap::LightComponentInstance::hasCamera
bool hasCamera() const
Definition: lightcomponent.h:339
nap::EShadowMapType::Cube
@ Cube
nap::LightComponentInstance::mSpawnedCamera
SpawnedEntityInstance mSpawnedCamera
Definition: lightcomponent.h:396
nap::uniform::shadow::flags
constexpr const char * flags
Definition: lightcomponent.h:71
nap::LightComponentInstance::getLightPosition
const glm::vec3 getLightPosition() const
Definition: lightcomponent.h:319
nap::getLightType
uint NAPAPI getLightType(LightFlags flags)
nap::LightComponentInstance::setShadowSpread
virtual void setShadowSpread(float spread)
Definition: lightcomponent.h:304
nap::uniform::light::direction
constexpr const char * direction
Definition: lightcomponent.h:55
nap::uniform::shadowStruct
constexpr const char * shadowStruct
Definition: lightcomponent.h:48
nap::uniform::shadow::count
constexpr const char * count
Definition: lightcomponent.h:72
nap::EntityInstance
Definition: entity.h:34
nap::LightComponentInstance::getTransform
TransformComponentInstance & getTransform()
Definition: lightcomponent.h:334
nap::uniform::light::falloff
constexpr const char * falloff
Definition: lightcomponent.h:58
nap::uniform::light::lights
constexpr const char * lights
Definition: lightcomponent.h:60
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap::ELightType
ELightType
Definition: lightcomponent.h:25
nap::uniform::shadow::strength
constexpr const char * strength
Definition: lightcomponent.h:69
nap::LightComponentInstance::getTransform
const TransformComponentInstance & getTransform() const
Definition: lightcomponent.h:329
nap::uniform::light::flags
constexpr const char * flags
Definition: lightcomponent.h:59
nap::math::extractPosition
glm::vec3 NAPAPI extractPosition(const glm::mat4x4 &matrix)
nap::ELightType::Spot
@ Spot
nap::LightComponent::Locator
Definition: lightcomponent.h:143
nap
Definition: templateapp.h:17
nap::uniform::light::color
constexpr const char * color
Definition: lightcomponent.h:52
nap::uniform::light::defaultMemberCount
constexpr const uint defaultMemberCount
Definition: lightcomponent.h:62
nap::ELightType::Custom
@ Custom
nap::uniform::shadow::nearFar
constexpr const char * nearFar
Definition: lightcomponent.h:68
nap::uniform::light::count
constexpr const char * count
Definition: lightcomponent.h:61
nap::LightComponentInstance::getLightDirection
const glm::vec3 getLightDirection() const
Definition: lightcomponent.h:324
nap::LightComponentInstance::getShadowSpread
virtual float getShadowSpread() const
Definition: lightcomponent.h:299
nap::uniform::lightStruct
constexpr const char * lightStruct
Definition: lightcomponent.h:47
nap::LightComponentInstance::setShadowStrength
virtual void setShadowStrength(float strength)
Definition: lightcomponent.h:294
nap::uniform::shadow::lightViewProjectionMatrix
constexpr const char * lightViewProjectionMatrix
Definition: lightcomponent.h:67
nap::LightComponentInstance::getShadowMapSize
virtual uint getShadowMapSize() const
Definition: lightcomponent.h:274
nap::LightComponentInstance::getGnomon
RenderGnomonComponentInstance & getGnomon()
Definition: lightcomponent.h:359
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:314
nap::LightComponentInstance::setIntensity
void setIntensity(float intensity)
Definition: lightcomponent.h:284
nap::getShadowMapType
uint NAPAPI getShadowMapType(LightFlags flags)
nap::EShadowMapType::Quad
@ Quad
nap::sampler::light::cubeShadowMaps
constexpr const char * cubeShadowMaps
Definition: lightcomponent.h:84