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* flags = "flags";
71  inline constexpr const char* count = "count";
72  }
73  }
74 
78  namespace sampler
79  {
80  namespace light
81  {
82  inline constexpr const char* shadowMaps = "shadowMaps";
83  inline constexpr const char* cubeShadowMaps = "cubeShadowMaps";
84  }
85  }
86 
87 
134  class NAPAPI LightComponent : public Component
135  {
136  RTTI_ENABLE(Component)
137  DECLARE_COMPONENT(LightComponent, LightComponentInstance)
138  public:
142  struct Locator
143  {
144  float mLineWidth = 1.0f;
145  float mGnomonSize = 1.0f;
146  };
147 
152  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;
153 
154  bool mEnabled = true;
155  bool mCastShadows = false;
156  RGBColorFloat mColor = {1.0f, 1.0f, 1.0f};
157  float mIntensity = 1.0f;
158  float mShadowStrength = 1.0f;
160  };
161 
162 
210  {
211  friend class RenderAdvancedService;
212  RTTI_ENABLE(ComponentInstance)
213  public:
214  // Constructor
216  ComponentInstance(entity, resource) { }
217 
222  virtual ~LightComponentInstance() { };
223 
229  virtual bool init(utility::ErrorState& errorState) override;
230 
234  virtual void onDestroy() override;
235 
240  virtual void enable(bool enable) { mIsEnabled = enable; }
241 
245  bool isEnabled() const { return mIsEnabled; };
246 
252  bool canCastShadows() const { return mSpawnedCamera != nullptr; }
253 
257  bool castsShadows() const { return canCastShadows() && mIsShadowEnabled; }
258 
262  virtual ELightType getLightType() const = 0;
263 
267  virtual EShadowMapType getShadowMapType() const = 0;
268 
272  virtual uint getShadowMapSize() const { return mShadowMapSize; }
273 
277  virtual float getIntensity() const { return mIntensity; }
278 
282  void setIntensity(float intensity) { mIntensity = intensity; }
283 
287  virtual float getShadowStrength() const { return mShadowStrength; }
288 
292  virtual void setShadowStrength(float strength) { mShadowStrength = strength; }
293 
297  virtual const RGBColorFloat& getColor() const { return mColor; }
298 
302  void setColor(const RGBColorFloat& color) { mColor = color; }
303 
307  const glm::vec3 getLightPosition() const { return math::extractPosition(getTransform().getGlobalTransform()); }
308 
312  const glm::vec3 getLightDirection() const { return -glm::normalize(getTransform().getGlobalTransform()[2]); }
313 
317  const TransformComponentInstance& getTransform() const { assert(mTransform != nullptr); return *mTransform; }
318 
322  TransformComponentInstance& getTransform() { assert(mTransform != nullptr); return *mTransform; }
323 
327  bool hasCamera() const { return mSpawnedCamera != nullptr; }
328 
332  CameraComponentInstance& getCamera() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
333 
337  CameraComponentInstance& getCamera() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<CameraComponentInstance>(); }
338 
342  const RenderGnomonComponentInstance& getGnomon() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
343 
347  RenderGnomonComponentInstance& getGnomon() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->getComponent<RenderGnomonComponentInstance>(); }
348 
352  const RenderFrustumComponentInstance* getFrustrum() const { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
353 
357  RenderFrustumComponentInstance* getFrustrum() { assert(mSpawnedCamera != nullptr); return mSpawnedCamera->findComponent<RenderFrustumComponentInstance>(); }
358 
359  float mIntensity = 1.0f;
360  RGBColorFloat mColor = { 1.0f, 1.0f, 1.0f };
361 
362  protected:
368  void registerUniformLightProperty(const std::string& memberName);
369 
379  SpawnedEntityInstance spawnShadowCamera(const nap::Entity& entity, nap::utility::ErrorState& error);
380 
381  LightComponent* mResource = nullptr;
382  TransformComponentInstance* mTransform = nullptr;
383  RenderAdvancedService* mService = nullptr;
385 
386  bool mIsEnabled = true;
387  bool mIsShadowEnabled = false;
388  float mShadowStrength = 1.0f;
389  uint mShadowMapSize = 512;
390 
391  private:
392  std::vector<nap::rtti::Property> mUniformList;
393  };
394 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::LightComponent::mLocator
Locator mLocator
Property: 'Locator' Locator settings.
Definition: lightcomponent.h:159
nap::LightComponentInstance::getFrustrum
const RenderFrustumComponentInstance * getFrustrum() const
Definition: lightcomponent.h:352
nap::ELightType::Point
@ Point
nap::LightComponentInstance::getShadowStrength
virtual float getShadowStrength() const
Definition: lightcomponent.h:287
nap::RGBColor< float >
nap::LightComponentInstance::LightComponentInstance
LightComponentInstance(EntityInstance &entity, Component &resource)
Definition: lightcomponent.h:215
nap::LightComponentInstance
Definition: lightcomponent.h:209
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera() const
Definition: lightcomponent.h:332
nap::uniform::light::angle
constexpr const char * angle
Definition: lightcomponent.h:57
nap::LightComponentInstance::enable
virtual void enable(bool enable)
Definition: lightcomponent.h:240
nap::uniform::light::origin
constexpr const char * origin
Definition: lightcomponent.h:54
nap::LightComponentInstance::canCastShadows
bool canCastShadows() const
Definition: lightcomponent.h:252
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:257
nap::LightComponentInstance::getGnomon
const RenderGnomonComponentInstance & getGnomon() const
Definition: lightcomponent.h:342
nap::LightComponent
Definition: lightcomponent.h:134
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:357
nap::EShadowMapType
EShadowMapType
Definition: lightcomponent.h:36
nap::LightComponentInstance::~LightComponentInstance
virtual ~LightComponentInstance()
Definition: lightcomponent.h:222
nap::LightComponentInstance::getCamera
CameraComponentInstance & getCamera()
Definition: lightcomponent.h:337
nap::LightComponentInstance::getColor
virtual const RGBColorFloat & getColor() const
Definition: lightcomponent.h:297
nap::LightComponentInstance::isEnabled
bool isEnabled() const
Definition: lightcomponent.h:245
nap::LightComponentInstance::getIntensity
virtual float getIntensity() const
Definition: lightcomponent.h:277
nap::RenderGnomonComponentInstance
Definition: rendergnomoncomponent.h:47
nap::sampler::light::shadowMaps
constexpr const char * shadowMaps
Definition: lightcomponent.h:82
nap::LightComponentInstance::hasCamera
bool hasCamera() const
Definition: lightcomponent.h:327
nap::EShadowMapType::Cube
@ Cube
nap::LightComponentInstance::mSpawnedCamera
SpawnedEntityInstance mSpawnedCamera
Definition: lightcomponent.h:384
nap::uniform::shadow::flags
constexpr const char * flags
Definition: lightcomponent.h:70
nap::LightComponentInstance::getLightPosition
const glm::vec3 getLightPosition() const
Definition: lightcomponent.h:307
nap::getLightType
uint NAPAPI getLightType(LightFlags flags)
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:71
nap::EntityInstance
Definition: entity.h:34
nap::LightComponentInstance::getTransform
TransformComponentInstance & getTransform()
Definition: lightcomponent.h:322
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:317
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:142
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:312
nap::uniform::lightStruct
constexpr const char * lightStruct
Definition: lightcomponent.h:47
nap::LightComponentInstance::setShadowStrength
virtual void setShadowStrength(float strength)
Definition: lightcomponent.h:292
nap::uniform::shadow::lightViewProjectionMatrix
constexpr const char * lightViewProjectionMatrix
Definition: lightcomponent.h:67
nap::LightComponentInstance::getShadowMapSize
virtual uint getShadowMapSize() const
Definition: lightcomponent.h:272
nap::LightComponentInstance::getGnomon
RenderGnomonComponentInstance & getGnomon()
Definition: lightcomponent.h:347
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:302
nap::LightComponentInstance::setIntensity
void setIntensity(float intensity)
Definition: lightcomponent.h:282
nap::getShadowMapType
uint NAPAPI getShadowMapType(LightFlags flags)
nap::EShadowMapType::Quad
@ Quad
nap::sampler::light::cubeShadowMaps
constexpr const char * cubeShadowMaps
Definition: lightcomponent.h:83