GlowComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright (c) 2014-2017 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/Core/Context.h>
  24. #include <AtomicGlow/GlowService/GlowService.h>
  25. #include "GlowComponent.h"
  26. using namespace AtomicGlow;
  27. namespace AtomicEditor
  28. {
  29. GlowComponent::GlowComponent(Context *context) : EditorComponent(context)
  30. {
  31. GlowSettings glowSettings;
  32. glowSettings.SetDefaults();
  33. SetFromGlowSettings(glowSettings);
  34. }
  35. GlowComponent::~GlowComponent()
  36. {
  37. }
  38. void GlowComponent::SetFromGlowSettings(const GlowSettings& settings)
  39. {
  40. lexelDensity_ = settings.lexelDensity_;
  41. giEnabled_ = settings.giEnabled_;
  42. giGranularity_ = settings.giGranularity_;
  43. giMaxBounces_ = settings.giMaxBounces_;
  44. aoEnabled_ = settings.aoEnabled_;
  45. aoDepth_ = settings.aoDepth_;
  46. nsamples_ = settings.nsamples_;
  47. aoMin_ = settings.aoMin_;
  48. aoMultiply_ = settings.aoMultiply_;
  49. }
  50. void GlowComponent::CopyToGlowSettings(GlowSettings& settings) const
  51. {
  52. settings.lexelDensity_ = lexelDensity_ ;
  53. settings.sceneLexelDensityScale_ = 1.0f;
  54. settings.giEnabled_= giEnabled_;
  55. settings.giGranularity_ = giGranularity_;
  56. settings.giMaxBounces_ = giMaxBounces_;
  57. settings.aoEnabled_ = aoEnabled_;
  58. settings.aoDepth_ = aoDepth_;
  59. settings.nsamples_ = nsamples_;
  60. settings.aoMin_ = aoMin_;
  61. settings.aoMultiply_ = aoMultiply_;
  62. }
  63. bool GlowComponent::Bake()
  64. {
  65. GlowService* glowService = GetSubsystem<GlowService>();
  66. if (!glowService)
  67. {
  68. ATOMIC_LOGERROR("GlowComponent::Bake() - Unable ot get glow service");
  69. return false;
  70. }
  71. SubscribeToEvent(E_ATOMICGLOWSERVICEBAKERESULT, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowServiceBakeResult));
  72. SubscribeToEvent(E_ATOMICGLOWSERVICELOGEVENT, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowServiceLogEvent));
  73. SubscribeToEvent(E_ATOMICGLOWBAKECANCEL, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowBakeCancel));
  74. GlowSettings settings;
  75. CopyToGlowSettings(settings);
  76. settings.Validate();
  77. SetFromGlowSettings(settings);
  78. return glowService->Bake(GetScene(), settings);
  79. }
  80. void GlowComponent::HandleAtomicGlowBakeCancel(StringHash eventType, VariantMap& eventData)
  81. {
  82. GetSubsystem<GlowService>()->CancelBake();
  83. }
  84. void GlowComponent::HandleAtomicGlowServiceBakeResult(StringHash eventType, VariantMap& eventData)
  85. {
  86. using namespace AtomicGlowServiceBakeResult;
  87. // convert to a glow component event, which contains the same fields
  88. SendEvent(E_ATOMICGLOWBAKERESULT, eventData);
  89. }
  90. void GlowComponent::HandleAtomicGlowServiceLogEvent(StringHash eventType, VariantMap& eventData)
  91. {
  92. using namespace AtomicGlowServiceLogEvent;
  93. // convert to a glow component event, which contains the same fields
  94. SendEvent(E_ATOMICGLOWLOGEVENT, eventData);
  95. }
  96. void GlowComponent::RegisterObject(Context* context)
  97. {
  98. context->RegisterFactory<GlowComponent>();
  99. ATOMIC_ATTRIBUTE("Lexel Density", float, lexelDensity_, 0.1f, AM_FILE);
  100. ATOMIC_ATTRIBUTE("GI Enabled", bool, giEnabled_, false, AM_FILE);
  101. ATOMIC_ATTRIBUTE("GI Granularity", int, giGranularity_, 16, AM_FILE);
  102. ATOMIC_ATTRIBUTE("GI Max Cycles", int, giMaxBounces_, 3, AM_FILE);
  103. ATOMIC_ATTRIBUTE("AO Enabled", bool, aoEnabled_, false, AM_FILE);
  104. ATOMIC_ATTRIBUTE("AO Depth", float, aoDepth_, 0.25f, AM_FILE);
  105. ATOMIC_ATTRIBUTE("AO Samples", int, nsamples_, 64, AM_FILE);
  106. ATOMIC_ATTRIBUTE("AO Min", float, aoMin_, 0.45f, AM_FILE);
  107. ATOMIC_ATTRIBUTE("AO Multiply", float, aoMultiply_, 1.0f, AM_FILE);
  108. }
  109. }