• 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 #ifndef SCENE_SRC_RESOURCE_MATERIAL_H
17 #define SCENE_SRC_RESOURCE_MATERIAL_H
18 
19 #include <atomic>
20 #include <scene/ext/ecs_lazy_property.h>
21 #include <scene/ext/intf_create_entity.h>
22 #include <scene/interface/intf_material.h>
23 #include <scene/interface/resource/types.h>
24 
25 #include <meta/api/property/property_event_handler.h>
26 #include <meta/api/resource/derived_from_resource.h>
27 #include <meta/ext/implementation_macros.h>
28 #include <meta/ext/object.h>
29 
30 #include "component/material_component.h"
31 #include "render_resource.h"
32 
SCENE_BEGIN_NAMESPACE()33 SCENE_BEGIN_NAMESPACE()
34 
35 class Material : public META_NS::IntroduceInterfaces<META_NS::DerivedFromResource, NamedSceneObject, IMaterial,
36                      ICreateEntity, META_NS::Resource> {
37     META_OBJECT(Material, ClassId::Material, IntroduceInterfaces)
38 
39 public:
40     Material() : Super(ClassId::MaterialResourceTemplate) {}
41 
42     bool SetEcsObject(const IEcsObject::Ptr&) override;
43     bool InitDynamicProperty(const META_NS::IProperty::Ptr& p, BASE_NS::string_view path) override;
44 
45 public:
46     META_BEGIN_STATIC_DATA()
47     META_STATIC_FORWARDED_PROPERTY_DATA(IMaterial, MaterialType, Type)
48     META_STATIC_FORWARDED_PROPERTY_DATA(IMaterial, float, AlphaCutoff)
49     SCENE_STATIC_DYNINIT_PROPERTY_DATA(IMaterial, IShader::Ptr, MaterialShader, "MaterialComponent.materialShader")
50     SCENE_STATIC_DYNINIT_PROPERTY_DATA(IMaterial, IShader::Ptr, DepthShader, "MaterialComponent.depthShader")
51     SCENE_STATIC_DYNINIT_PROPERTY_DATA(IMaterial, SCENE_NS::RenderSort, RenderSort, "")
52     SCENE_STATIC_DYNINIT_PROPERTY_DATA(IMaterial, SCENE_NS::LightingFlags, LightingFlags, "")
53     SCENE_STATIC_DYNINIT_ARRAY_PROPERTY_DATA(IMaterial, ITexture::Ptr, Textures, "")
54     SCENE_STATIC_DYNINIT_PROPERTY_DATA(IMaterial, META_NS::IMetadata::Ptr, CustomProperties, "")
55     META_END_STATIC_DATA()
56 
57     META_FORWARD_PROPERTY(MaterialType, Type, material_->Type())
58     META_FORWARD_PROPERTY(float, AlphaCutoff, material_->AlphaCutoff())
59     META_IMPLEMENT_PROPERTY(IShader::Ptr, MaterialShader)
60     META_IMPLEMENT_PROPERTY(IShader::Ptr, DepthShader)
61     META_IMPLEMENT_PROPERTY(SCENE_NS::RenderSort, RenderSort)
62     META_IMPLEMENT_PROPERTY(SCENE_NS::LightingFlags, LightingFlags)
63     META_IMPLEMENT_READONLY_ARRAY_PROPERTY(ITexture::Ptr, Textures)
64     META_IMPLEMENT_READONLY_PROPERTY(META_NS::IMetadata::Ptr, CustomProperties)
65 
66     META_NS::IMetadata::Ptr GetCustomProperties() const override;
67     META_NS::IProperty::Ptr GetCustomProperty(BASE_NS::string_view name) const override;
68 
69     CORE_NS::Entity CreateEntity(const IInternalScene::Ptr& scene) override;
70 
71     CORE_NS::ResourceType GetResourceType() const override
72     {
73         return ClassId::MaterialResource.Id().ToUid();
74     }
75 
76     bool SetResource(const CORE_NS::IResource::Ptr& p) override;
77     CORE_NS::IResource::Ptr CreateResource() const override;
78 
79 private:
80     Future<bool> UpdateTextures(const META_NS::IProperty::Ptr& p);
81     bool ConstructTextures(const META_NS::IProperty::Ptr& p);
82     bool UpdateTextureNames(
83         const BASE_NS::vector<ITexture::Ptr>& textures, const IInternalMaterial::ActiveTextureSlotInfo& tsi);
84     /**
85      * @brief Synchronize custom properties from ECS to engine and construct engine values for them.
86      * @param synced_values If not null pointer, store the synced engine values here.
87      * @return True if succeeded, false otherwise.
88      */
89     bool SyncCustomProperties(BASE_NS::vector<META_NS::IEngineValue::Ptr>* synced_values) const;
90     void CopyTextureData(const META_NS::IProperty::ConstPtr& p);
91 
92     Future<bool> UpdateCustoms(const META_NS::IProperty::Ptr& p);
93     bool UpdateCustomProperties(const META_NS::IProperty::Ptr&);
94 
95 private:
96     IInternalMaterial::Ptr material_;
97     std::atomic<bool> textureSyncScheduled_ {};
98     std::atomic<bool> customsSyncScheduled_ {};
99     META_NS::PropertyChangedEventHandler shaderChanged_;
100 };
101 
102 SCENE_END_NAMESPACE()
103 
104 #endif
105