2
0

LightEvent.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Events/LightEvent.h>
  6. #include <AnKi/Scene/Components/LightComponent.h>
  7. #include <AnKi/Scene/Components/LensFlareComponent.h>
  8. namespace anki
  9. {
  10. Error LightEvent::init(Second startTime, Second duration, SceneNode* light)
  11. {
  12. Event::init(startTime, duration);
  13. m_associatedNodes.emplaceBack(getAllocator(), light);
  14. LightComponent& lightc = light->getFirstComponentOfType<LightComponent>();
  15. switch(lightc.getLightComponentType())
  16. {
  17. case LightComponentType::POINT:
  18. m_originalRadius = lightc.getRadius();
  19. break;
  20. case LightComponentType::SPOT:
  21. ANKI_ASSERT("TODO");
  22. break;
  23. default:
  24. ANKI_ASSERT(0);
  25. break;
  26. }
  27. m_originalDiffColor = lightc.getDiffuseColor();
  28. return Error::NONE;
  29. }
  30. Error LightEvent::update(Second prevUpdateTime, Second crntTime)
  31. {
  32. const F32 freq = getRandomRange(m_freq - m_freqDeviation, m_freq + m_freqDeviation);
  33. F32 factor = F32(sin(crntTime * freq * PI)) / 2.0f + 0.5f;
  34. LightComponent& lightc = m_associatedNodes[0]->getFirstComponentOfType<LightComponent>();
  35. // Update radius
  36. if(m_radiusMultiplier != 0.0)
  37. {
  38. switch(lightc.getLightComponentType())
  39. {
  40. case LightComponentType::POINT:
  41. lightc.setRadius(m_originalRadius + factor * m_radiusMultiplier);
  42. break;
  43. case LightComponentType::SPOT:
  44. ANKI_ASSERT("TODO");
  45. break;
  46. default:
  47. ANKI_ASSERT(0);
  48. break;
  49. }
  50. }
  51. // Update the color and the lens flare's color if they are the same
  52. if(m_intensityMultiplier != Vec4(0.0))
  53. {
  54. Vec4 outCol = m_originalDiffColor + factor * m_intensityMultiplier;
  55. LensFlareComponent* lfc = m_associatedNodes[0]->tryGetFirstComponentOfType<LensFlareComponent>();
  56. if(lfc && lfc->getColorMultiplier().xyz() == lightc.getDiffuseColor().xyz())
  57. {
  58. lfc->setColorMultiplier(Vec4(outCol.xyz(), lfc->getColorMultiplier().w()));
  59. }
  60. lightc.setDiffuseColor(outCol);
  61. }
  62. return Error::NONE;
  63. }
  64. } // end namespace anki