LightEvent.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Error LightEvent::init(Second startTime, Second duration, SceneNode* light)
  10. {
  11. Event::init(startTime, duration);
  12. m_associatedNodes.emplaceBack(getAllocator(), light);
  13. LightComponent& lightc = light->getFirstComponentOfType<LightComponent>();
  14. switch(lightc.getLightComponentType())
  15. {
  16. case LightComponentType::POINT:
  17. m_originalRadius = lightc.getRadius();
  18. break;
  19. case LightComponentType::SPOT:
  20. ANKI_ASSERT("TODO");
  21. break;
  22. default:
  23. ANKI_ASSERT(0);
  24. break;
  25. }
  26. m_originalDiffColor = lightc.getDiffuseColor();
  27. return Error::NONE;
  28. }
  29. Error LightEvent::update(Second prevUpdateTime, Second crntTime)
  30. {
  31. const F32 freq = getRandomRange(m_freq - m_freqDeviation, m_freq + m_freqDeviation);
  32. F32 factor = F32(sin(crntTime * freq * PI)) / 2.0f + 0.5f;
  33. LightComponent& lightc = m_associatedNodes[0]->getFirstComponentOfType<LightComponent>();
  34. // Update radius
  35. if(m_radiusMultiplier != 0.0)
  36. {
  37. switch(lightc.getLightComponentType())
  38. {
  39. case LightComponentType::POINT:
  40. lightc.setRadius(m_originalRadius + factor * m_radiusMultiplier);
  41. break;
  42. case LightComponentType::SPOT:
  43. ANKI_ASSERT("TODO");
  44. break;
  45. default:
  46. ANKI_ASSERT(0);
  47. break;
  48. }
  49. }
  50. // Update the color and the lens flare's color if they are the same
  51. if(m_intensityMultiplier != Vec4(0.0))
  52. {
  53. Vec4 outCol = m_originalDiffColor + factor * m_intensityMultiplier;
  54. LensFlareComponent* lfc = m_associatedNodes[0]->tryGetFirstComponentOfType<LensFlareComponent>();
  55. if(lfc && lfc->getColorMultiplier().xyz() == lightc.getDiffuseColor().xyz())
  56. {
  57. lfc->setColorMultiplier(Vec4(outCol.xyz(), lfc->getColorMultiplier().w()));
  58. }
  59. lightc.setDiffuseColor(outCol);
  60. }
  61. return Error::NONE;
  62. }
  63. } // end namespace anki