LightEvent.cpp 2.1 KB

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