• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "material_type.h"
17 
18 #include <scene/ext/intf_render_resource.h>
19 #include <scene/ext/util.h>
20 
21 #include <meta/api/metadata_util.h>
22 
23 #include "serialization/util.h"
24 
SCENE_BEGIN_NAMESPACE()25 SCENE_BEGIN_NAMESPACE()
26 
27 bool MaterialResourceType::Build(const META_NS::IMetadata::Ptr& d)
28 {
29     bool res = Super::Build(d);
30     if (res) {
31         auto context = GetInterfaceBuildArg<IRenderContext>(d, "RenderContext");
32         if (!context) {
33             CORE_LOG_E("Invalid arguments to construct MaterialResourceType");
34             return false;
35         }
36         context_ = context;
37     }
38     return res;
39 }
GetResourceName() const40 BASE_NS::string MaterialResourceType::GetResourceName() const
41 {
42     return "Material";
43 }
GetResourceType() const44 BASE_NS::Uid MaterialResourceType::GetResourceType() const
45 {
46     return ClassId::MaterialResource.Id().ToUid();
47 }
48 
LoadResource(const StorageInfo & s) const49 CORE_NS::IResource::Ptr MaterialResourceType::LoadResource(const StorageInfo& s) const
50 {
51     auto scene = interface_pointer_cast<IScene>(s.context);
52     if (!scene) {
53         CORE_LOG_W("missing context, cannot load material resource");
54         return nullptr;
55     }
56     CORE_NS::IResource::Ptr res = scene->CreateObject<CORE_NS::IResource>(ClassId::Material).GetResult();
57     if (res && s.options) {
58         if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
59                 META_NS::ClassId::ObjectResourceOptions)) {
60             opts->Load(*s.options, s.self, s.context);
61             if (auto i = interface_cast<META_NS::IDerivedFromResource>(res)) {
62                 auto base = opts->GetBaseResource();
63                 if (base.IsValid()) {
64                     auto r = s.self->GetResource(base, s.context);
65                     if (!r) {
66                         CORE_LOG_W("Could not load base resource for material %s", s.id.ToString().c_str());
67                     }
68                     i->SetResource(r);
69                 }
70             }
71             auto in = interface_cast<META_NS::IMetadata>(opts);
72             auto out = interface_cast<META_NS::IMetadata>(res);
73             if (in && out) {
74                 SerCopy(*in, *out);
75             }
76         }
77     }
78     return res;
79 }
SaveResource(const CORE_NS::IResource::ConstPtr & p,const StorageInfo & s) const80 bool MaterialResourceType::SaveResource(const CORE_NS::IResource::ConstPtr& p, const StorageInfo& s) const
81 {
82     if (s.options) {
83         if (auto opts = META_NS::GetObjectRegistry().Create<META_NS::IObjectResourceOptions>(
84                 META_NS::ClassId::ObjectResourceOptions)) {
85             auto in = interface_cast<META_NS::IMetadata>(p);
86             auto out = interface_cast<META_NS::IMetadata>(opts);
87             if (auto i = interface_cast<META_NS::IDerivedFromResource>(p)) {
88                 opts->SetBaseResource(i->GetResource());
89             }
90             if (in && out) {
91                 SerCloneAllToDefaultIfSet(*in, *out);
92                 opts->Save(*s.options, s.self);
93             }
94         }
95     }
96     return true;
97 }
ReloadResource(const StorageInfo &,const CORE_NS::IResource::Ptr &) const98 bool MaterialResourceType::ReloadResource(const StorageInfo&, const CORE_NS::IResource::Ptr&) const
99 {
100     return false;
101 }
102 
103 SCENE_END_NAMESPACE()