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 <scene_plugin/api/environment_uid.h>
16 
17 #include <meta/ext/concrete_base_object.h>
18 
19 #include "bind_templates.inl"
20 #include "node_impl.h"
21 
22 using namespace BASE_NS;
23 using namespace META_NS;
24 using namespace CORE_NS;
25 using namespace SCENE_NS;
26 
27 namespace {
28 
29 static constexpr Math::Vec4 ZERO = Math::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
30 static constexpr Math::Vec4 ONE = Math::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
31 
32 class EnvImpl : public ConcreteBaseMetaObjectFwd<EnvImpl, NodeImpl, SCENE_NS::ClassId::Environment, IEnvironment> {
33     using Super = ConcreteBaseMetaObjectFwd<EnvImpl, NodeImpl, SCENE_NS::ClassId::Environment, IEnvironment>;
34 
35     META_IMPLEMENT_INTERFACE_PROPERTY(
36         IEnvironment, IEnvironment::BackgroundType, Background, IEnvironment::BackgroundType::NONE)
37     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, Math::Vec4, IndirectDiffuseFactor, ONE)
38     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, Math::Vec4, IndirectSpecularFactor, ONE)
39     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, Math::Vec4, EnvMapFactor, ONE)
40     META_IMPLEMENT_INTERFACE_PROPERTY(SCENE_NS::IEnvironment, IBitmap::Ptr, RadianceImage, {})
41     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, uint32_t, RadianceCubemapMipCount, 0)
42     META_IMPLEMENT_INTERFACE_PROPERTY(SCENE_NS::IEnvironment, IBitmap::Ptr, EnvironmentImage, {})
43     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, float, EnvironmentMapLodLevel, 0.0f)
44     META_IMPLEMENT_INTERFACE_ARRAY_PROPERTY(
45         IEnvironment, Math::Vec3, IrradianceCoefficients, {})
46     META_IMPLEMENT_INTERFACE_PROPERTY(IEnvironment, Math::Quat, EnvironmentRotation, Math::Quat(0.f, 0.f, 0.f, 1.f))
47     META_IMPLEMENT_INTERFACE_PROPERTY(SCENE_NS::IEnvironment, Math::Vec4, AdditionalFactor, ZERO)
48     META_IMPLEMENT_INTERFACE_PROPERTY(
49         IEnvironment, uint64_t, ShaderHandle, CORE_NS::Entity {}.id)
50 
51     static constexpr string_view ENV_COMPONENT_NAME = "EnvironmentComponent";
52     static constexpr size_t ENV_COMPONENT_NAME_LEN = ENV_COMPONENT_NAME.size() + 1;
53     static constexpr string_view ENV_BG = "EnvironmentComponent.background";
54     static constexpr string_view ENV_DF = "EnvironmentComponent.indirectDiffuseFactor";
55     static constexpr string_view ENV_SF = "EnvironmentComponent.indirectSpecularFactor";
56     static constexpr string_view ENV_MF = "EnvironmentComponent.envMapFactor";
57     static constexpr string_view ENV_CH = "EnvironmentComponent.radianceCubemap";
58     static constexpr string_view ENV_CMC = "EnvironmentComponent.radianceCubemapMipCount";
59     static constexpr string_view ENV_MH = "EnvironmentComponent.envMap";
60     static constexpr string_view ENV_MLL = "EnvironmentComponent.envMapLodLevel";
61     static constexpr string_view ENV_IC = "EnvironmentComponent.irradianceCoefficients";
62     static constexpr size_t ENV_IC_SIZE = ENV_IC.size();
63     static constexpr string_view ENV_ER = "EnvironmentComponent.environmentRotation";
64     static constexpr string_view ENV_AF = "EnvironmentComponent.additionalFactor";
65     static constexpr string_view ENV_SH = "EnvironmentComponent.shader";
66 
Build(const IMetadata::Ptr & data)67     bool Build(const IMetadata::Ptr& data) override
68     {
69         bool ret = false;
70         if (ret = Super::Build(data); ret) {
71             PropertyNameMask()[ENV_COMPONENT_NAME] = { ENV_BG.substr(ENV_COMPONENT_NAME_LEN),
72                 ENV_DF.substr(ENV_COMPONENT_NAME_LEN), ENV_SF.substr(ENV_COMPONENT_NAME_LEN),
73                 ENV_MF.substr(ENV_COMPONENT_NAME_LEN), ENV_CH.substr(ENV_COMPONENT_NAME_LEN),
74                 ENV_CMC.substr(ENV_COMPONENT_NAME_LEN), ENV_MH.substr(ENV_COMPONENT_NAME_LEN),
75                 ENV_MLL.substr(ENV_COMPONENT_NAME_LEN), ENV_IC.substr(ENV_COMPONENT_NAME_LEN),
76                 ENV_ER.substr(ENV_COMPONENT_NAME_LEN), ENV_AF.substr(ENV_COMPONENT_NAME_LEN),
77                 ENV_SH.substr(ENV_COMPONENT_NAME_LEN) };
78             DisableInputHandling();
79         }
80         return ret;
81     }
82 
CompleteInitialization(const string & path)83     bool CompleteInitialization(const string& path) override
84     {
85         if (!NodeImpl::CompleteInitialization(path)) {
86             return false;
87         }
88 
89         if (auto meta = interface_pointer_cast<IMetadata>(ecsObject_)) {
90             ConvertBindChanges<IEnvironment::BackgroundType, uint8_t>(
91                 propHandler_, META_ACCESS_PROPERTY(Background), meta, ENV_BG);
92             BindChanges<Math::Vec4>(propHandler_, META_ACCESS_PROPERTY(IndirectDiffuseFactor), meta, ENV_DF);
93             BindChanges<Math::Vec4>(propHandler_, META_ACCESS_PROPERTY(IndirectSpecularFactor), meta, ENV_SF);
94             BindChanges<Math::Vec4>(propHandler_, META_ACCESS_PROPERTY(EnvMapFactor), meta, ENV_MF);
95 
96             ConvertBindChanges<IBitmap::Ptr, EntityReference, ImageConverter>(
97                 propHandler_, META_ACCESS_PROPERTY(RadianceImage), meta, ENV_CH);
98 
99             ConvertBindChanges<IBitmap::Ptr, EntityReference, ImageConverter>(
100                 propHandler_, META_ACCESS_PROPERTY(EnvironmentImage), meta, ENV_MH);
101 
102             BindChanges<uint32_t>(propHandler_, META_ACCESS_PROPERTY(RadianceCubemapMipCount), meta, ENV_CMC);
103             BindChanges<float>(propHandler_, META_ACCESS_PROPERTY(EnvironmentMapLodLevel), meta, ENV_MLL);
104             BindChanges<Math::Quat>(propHandler_, META_ACCESS_PROPERTY(EnvironmentRotation), meta, ENV_ER);
105             BindChanges<Math::Vec4>(propHandler_, META_ACCESS_PROPERTY(AdditionalFactor), meta, ENV_AF);
106             BindChanges<uint64_t>(propHandler_, META_ACCESS_PROPERTY(ShaderHandle), meta, ENV_SH);
107 
108             BindChanges<Math::Vec3>(propHandler_, META_ACCESS_PROPERTY(IrradianceCoefficients), meta, ENV_IC);
109         }
110 
111         return true;
112     }
113 
114 };
115 } // namespace
SCENE_BEGIN_NAMESPACE()116 SCENE_BEGIN_NAMESPACE()
117 
118 void RegisterEnvImpl()
119 {
120     auto& registry = GetObjectRegistry();
121     registry.RegisterObjectType<EnvImpl>();
122 }
UnregisterEnvImpl()123 void UnregisterEnvImpl()
124 {
125     auto& registry = GetObjectRegistry();
126     registry.UnregisterObjectType<EnvImpl>();
127 }
128 SCENE_END_NAMESPACE()
129