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_MESH_MESH_H
17 #define SCENE_SRC_MESH_MESH_H
18
19 #include <mutex>
20 #include <scene/ext/intf_create_entity.h>
21 #include <scene/ext/named_scene_object.h>
22 #include <scene/interface/intf_mesh.h>
23
24 #include <meta/ext/implementation_macros.h>
25 #include <meta/ext/object.h>
26 #include <meta/interface/intf_owner.h>
27
28 #include "../component/mesh_component.h"
29
SCENE_BEGIN_NAMESPACE()30 SCENE_BEGIN_NAMESPACE()
31
32 class Mesh : public META_NS::IntroduceInterfaces<NamedSceneObject, IMesh, ICreateEntity, META_NS::IPropertyOwner> {
33 META_OBJECT(Mesh, ClassId::Mesh, IntroduceInterfaces)
34 public:
35 META_BEGIN_STATIC_DATA()
36 META_STATIC_PROPERTY_DATA(IMesh, IMaterial::Ptr, MaterialOverride)
37 META_END_STATIC_DATA()
38
39 META_IMPLEMENT_PROPERTY(IMaterial::Ptr, MaterialOverride)
40 META_FORWARD_READONLY_PROPERTY(BASE_NS::Math::Vec3, AABBMin, mesh_->AABBMin())
41 META_FORWARD_READONLY_PROPERTY(BASE_NS::Math::Vec3, AABBMax, mesh_->AABBMax())
42
43 Future<BASE_NS::vector<ISubMesh::Ptr>> GetSubmeshes() const override;
44 Future<bool> SetSubmeshes(const BASE_NS::vector<ISubMesh::Ptr>&) override;
45
46 public:
47 bool SetEcsObject(const IEcsObject::Ptr&) override;
48 CORE_NS::Entity CreateEntity(const IInternalScene::Ptr& scene) override;
49
50 private:
51 void OnPropertyChanged(const META_NS::IProperty&) override;
52
53 void SetInternalMesh(IInternalMesh::Ptr m);
54 BASE_NS::vector<CORE3D_NS::MeshComponent::Submesh> ConstructComponentSubmeshes(
55 const BASE_NS::vector<ISubMesh::Ptr>& submeshes) const;
56 void ApplyMaterialOverride(BASE_NS::vector<CORE3D_NS::MeshComponent::Submesh>& submeshes, bool forceStore = false);
57 void RestoreOriginalMaterials(BASE_NS::vector<CORE3D_NS::MeshComponent::Submesh>& submeshes);
58 void OnMaterialOverrideChanged();
59
60 IEcsObject::Ptr GetMeshEcsObject() const;
61
62 protected:
63 mutable std::mutex mutex_;
64 CORE_NS::EntityReference meshEntity_;
65 IInternalMesh::Ptr mesh_;
66 BASE_NS::vector<CORE_NS::Entity> submeshMaterials_;
67 };
68
69 SCENE_END_NAMESPACE()
70
71 #endif