1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "postprocess.h" 16 17 #include <3d/ecs/components/name_component.h> 18 19 #include "converting_value.h" 20 #include "bloom.h" 21 #include "tonemap.h" 22 #include "util.h" 23 SCENE_BEGIN_NAMESPACE()24SCENE_BEGIN_NAMESPACE() 25 26 CORE_NS::Entity PostProcess::CreateEntity(const IInternalScene::Ptr& scene) 27 { 28 auto& ecs = scene->GetEcsContext(); 29 auto pp = ecs.FindComponent<CORE3D_NS::PostProcessComponent>(); 30 if (!pp) { 31 return {}; 32 } 33 auto nameManager = ecs.FindComponent<CORE3D_NS::NameComponent>(); 34 if (!nameManager) { 35 return {}; 36 } 37 auto ent = ecs.GetNativeEcs()->GetEntityManager().Create(); 38 pp->Create(ent); 39 nameManager->Create(ent); 40 return ent; 41 } SetEcsObject(const IEcsObject::Ptr & obj)42bool PostProcess::SetEcsObject(const IEcsObject::Ptr& obj) 43 { 44 auto p = META_NS::GetObjectRegistry().Create<IInternalPostProcess>(ClassId::PostProcessComponent); 45 if (auto acc = interface_cast<IEcsObjectAccess>(p)) { 46 if (acc->SetEcsObject(obj)) { 47 pp_ = p; 48 } 49 } 50 return pp_ != nullptr; 51 } GetEcsObject() const52IEcsObject::Ptr PostProcess::GetEcsObject() const 53 { 54 auto acc = interface_cast<IEcsObjectAccess>(pp_); 55 return acc ? acc->GetEcsObject() : nullptr; 56 } InitDynamicProperty(const META_NS::IProperty::Ptr & p,BASE_NS::string_view path)57bool PostProcess::InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path) 58 { 59 auto& r = META_NS::GetObjectRegistry(); 60 if (p->GetName() == "Tonemap") { 61 auto obj = r.Create<ITonemap>(ClassId::Tonemap); 62 if (auto i = interface_cast<IPPEffectInit>(obj)) { 63 i->Init(pp_->EnableFlags()); 64 } 65 if (auto access = interface_cast<IEcsObjectAccess>(obj)) { 66 access->SetEcsObject(GetEcsObject()); 67 } 68 if (META_NS::Property<ITonemap::Ptr> prop { p }) { 69 prop->SetValue(obj); 70 return true; 71 } 72 } 73 if (p->GetName() == "Bloom") { 74 auto obj = r.Create<IBloom>(ClassId::Bloom); 75 if (auto i = interface_cast<IPPEffectInit>(obj)) { 76 i->Init(pp_->EnableFlags()); 77 } 78 if (auto access = interface_cast<IEcsObjectAccess>(obj)) { 79 access->SetEcsObject(GetEcsObject()); 80 } 81 if (META_NS::Property<IBloom::Ptr> prop { p }) { 82 prop->SetValue(obj); 83 return true; 84 } 85 } 86 return false; 87 } 88 SCENE_END_NAMESPACE() 89