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 CORE_ECS_SKINNINGSYSTEM_H
17 #define CORE_ECS_SKINNINGSYSTEM_H
18 
19 #include <ComponentTools/component_query.h>
20 #include <PropertyTools/property_api_impl.h>
21 
22 #include <3d/ecs/systems/intf_skinning_system.h>
23 #include <base/math/matrix.h>
24 #include <core/namespace.h>
25 #include <core/threading/intf_thread_pool.h>
26 
27 CORE3D_BEGIN_NAMESPACE()
28 class INodeComponentManager;
29 class ISkinComponentManager;
30 class ISkinIbmComponentManager;
31 class ISkinJointsComponentManager;
32 class IJointMatricesComponentManager;
33 class IPreviousJointMatricesComponentManager;
34 class IWorldMatrixComponentManager;
35 class INodeSystem;
36 class ICameraComponentManager;
37 class ILightComponentManager;
38 class IMeshComponentManager;
39 class IRenderMeshComponentManager;
40 class IPicking;
41 
42 struct JointMatricesComponent;
43 
44 class SkinningSystem final : public ISkinningSystem {
45 public:
46     explicit SkinningSystem(CORE_NS::IEcs& ecs);
47     ~SkinningSystem() override = default;
48     BASE_NS::string_view GetName() const override;
49     BASE_NS::Uid GetUid() const override;
50     CORE_NS::IPropertyHandle* GetProperties() override;
51     const CORE_NS::IPropertyHandle* GetProperties() const override;
52     void SetProperties(const CORE_NS::IPropertyHandle&) override;
53 
54     bool IsActive() const override;
55     void SetActive(bool state) override;
56 
57     void Initialize() override;
58     bool Update(bool frameRenderingQueued, uint64_t time, uint64_t delta) override;
59     void Uninitialize() override;
60 
61     const CORE_NS::IEcs& GetECS() const override;
62 
63     void CreateInstance(CORE_NS::Entity const& skinIbmEntity, BASE_NS::array_view<const CORE_NS::Entity> const& joints,
64         CORE_NS::Entity const& entity, CORE_NS::Entity const& skeleton) override;
65     void CreateInstance(
66         CORE_NS::Entity const& skinIbmEntity, CORE_NS::Entity const& entity, CORE_NS::Entity const& skeleton) override;
67     void DestroyInstance(CORE_NS::Entity const& entity) override;
68 
69 private:
70     void UpdateSkin(const CORE_NS::ComponentQuery::ResultRow& row);
71     void UpdateJointTransformations(bool isEnabled, const BASE_NS::array_view<CORE_NS::Entity const>& jointEntities,
72         const BASE_NS::array_view<BASE_NS::Math::Mat4X4 const>& iblMatrices, JointMatricesComponent& jointMatrices,
73         const BASE_NS::Math::Mat4X4& skinEntityWorldInverse);
74 
75     bool active_;
76     CORE_NS::IEcs& ecs_;
77 
78     IPicking& picking_;
79 
80     ISkinComponentManager& skinManager_;
81     ISkinIbmComponentManager& skinIbmManager_;
82     ISkinJointsComponentManager& skinJointsManager_;
83     IJointMatricesComponentManager& jointMatricesManager_;
84     IPreviousJointMatricesComponentManager& previousJointMatricesManager_;
85     IWorldMatrixComponentManager& worldMatrixManager_;
86     INodeComponentManager& nodeManager_;
87     IRenderMeshComponentManager& renderMeshManager_;
88     IMeshComponentManager& meshManager_;
89     INodeSystem* nodeSystem_ = nullptr;
90 
91     CORE_NS::ComponentQuery componentQuery_;
92 
93     uint32_t worldMatrixGeneration_ { 0 };
94     uint32_t jointMatricesGeneration_ { 0 };
95     CORE_NS::PropertyApiImpl<void> SKINNING_SYSTEM_PROPERTIES;
96 
97     CORE_NS::IThreadPool::Ptr threadPool_;
98     class SkinTask;
99     BASE_NS::vector<SkinTask> tasks_;
100     BASE_NS::vector<CORE_NS::IThreadPool::IResult::Ptr> taskResults_;
101 };
102 CORE3D_END_NAMESPACE()
103 
104 #endif // CORE_ECS_SKINNINGSYSTEM_H
105