GlowComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <ToolCore/ToolSystem.h>
  25. #include <ToolCore/Project/Project.h>
  26. #include <AtomicGlow/GlowService/GlowService.h>
  27. #include "GlowComponent.h"
  28. using namespace ToolCore;
  29. using namespace AtomicGlow;
  30. namespace AtomicEditor
  31. {
  32. GlowComponent::GlowComponent(Context *context) : EditorComponent(context)
  33. {
  34. GlowSettings glowSettings;
  35. glowSettings.SetDefaults();
  36. SetFromGlowSettings(glowSettings);
  37. }
  38. GlowComponent::~GlowComponent()
  39. {
  40. }
  41. void GlowComponent::SetFromGlowSettings(const GlowSettings& settings)
  42. {
  43. lexelDensity_ = settings.lexelDensity_;
  44. giEnabled_ = settings.giEnabled_;
  45. giGranularity_ = settings.giGranularity_;
  46. giMaxBounces_ = settings.giMaxBounces_;
  47. aoEnabled_ = settings.aoEnabled_;
  48. aoDepth_ = settings.aoDepth_;
  49. nsamples_ = settings.nsamples_;
  50. aoMin_ = settings.aoMin_;
  51. aoMultiply_ = settings.aoMultiply_;
  52. }
  53. void GlowComponent::CopyToGlowSettings(GlowSettings& settings) const
  54. {
  55. settings.lexelDensity_ = lexelDensity_ ;
  56. settings.sceneLexelDensityScale_ = 1.0f;
  57. settings.giEnabled_= giEnabled_;
  58. settings.giGranularity_ = giGranularity_;
  59. settings.giMaxBounces_ = giMaxBounces_;
  60. settings.aoEnabled_ = aoEnabled_;
  61. settings.aoDepth_ = aoDepth_;
  62. settings.nsamples_ = nsamples_;
  63. settings.aoMin_ = aoMin_;
  64. settings.aoMultiply_ = aoMultiply_;
  65. }
  66. bool GlowComponent::Bake()
  67. {
  68. GlowService* glowService = GetSubsystem<GlowService>();
  69. if (!glowService)
  70. {
  71. ATOMIC_LOGERROR("GlowComponent::Bake() - Unable ot get glow service");
  72. return false;
  73. }
  74. SubscribeToEvent(E_ATOMICGLOWSERVICEBAKERESULT, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowServiceBakeResult));
  75. SubscribeToEvent(E_ATOMICGLOWSERVICELOGEVENT, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowServiceLogEvent));
  76. SubscribeToEvent(E_ATOMICGLOWBAKECANCEL, ATOMIC_HANDLER(GlowComponent, HandleAtomicGlowBakeCancel));
  77. GlowSettings settings;
  78. CopyToGlowSettings(settings);
  79. settings.Validate();
  80. SetFromGlowSettings(settings);
  81. ToolSystem* toolSystem = GetSubsystem<ToolSystem>();
  82. if (!toolSystem)
  83. return false;
  84. Project* project = toolSystem->GetProject();
  85. if (!project)
  86. return false;
  87. return glowService->Bake(project->GetProjectPath(), GetScene(), settings);
  88. }
  89. void GlowComponent::HandleAtomicGlowBakeCancel(StringHash eventType, VariantMap& eventData)
  90. {
  91. GetSubsystem<GlowService>()->CancelBake();
  92. }
  93. void GlowComponent::HandleAtomicGlowServiceBakeResult(StringHash eventType, VariantMap& eventData)
  94. {
  95. using namespace AtomicGlowServiceBakeResult;
  96. // convert to a glow component event, which contains the same fields
  97. SendEvent(E_ATOMICGLOWBAKERESULT, eventData);
  98. }
  99. void GlowComponent::HandleAtomicGlowServiceLogEvent(StringHash eventType, VariantMap& eventData)
  100. {
  101. using namespace AtomicGlowServiceLogEvent;
  102. // convert to a glow component event, which contains the same fields
  103. SendEvent(E_ATOMICGLOWLOGEVENT, eventData);
  104. }
  105. void GlowComponent::RegisterObject(Context* context)
  106. {
  107. context->RegisterFactory<GlowComponent>();
  108. ATOMIC_ATTRIBUTE("Lexel Density", float, lexelDensity_, 0.1f, AM_FILE);
  109. ATOMIC_ATTRIBUTE("GI Enabled", bool, giEnabled_, false, AM_FILE);
  110. ATOMIC_ATTRIBUTE("GI Granularity", int, giGranularity_, 16, AM_FILE);
  111. ATOMIC_ATTRIBUTE("GI Max Cycles", int, giMaxBounces_, 3, AM_FILE);
  112. ATOMIC_ATTRIBUTE("AO Enabled", bool, aoEnabled_, false, AM_FILE);
  113. ATOMIC_ATTRIBUTE("AO Depth", float, aoDepth_, 0.25f, AM_FILE);
  114. ATOMIC_ATTRIBUTE("AO Samples", int, nsamples_, 64, AM_FILE);
  115. ATOMIC_ATTRIBUTE("AO Min", float, aoMin_, 0.45f, AM_FILE);
  116. ATOMIC_ATTRIBUTE("AO Multiply", float, aoMultiply_, 1.0f, AM_FILE);
  117. }
  118. }