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_UTIL_SCENE_UTIL_H
17 #define CORE_UTIL_SCENE_UTIL_H
18 
19 #include <3d/ecs/components/camera_component.h>
20 #include <3d/ecs/components/light_component.h>
21 #include <3d/util/intf_scene_util.h>
22 #include <base/containers/string_view.h>
23 #include <base/containers/vector.h>
24 #include <base/math/matrix.h>
25 #include <base/math/quaternion.h>
26 #include <base/math/vector.h>
27 #include <core/namespace.h>
28 #include <core/plugin/intf_plugin.h>
29 
30 CORE3D_BEGIN_NAMESPACE()
31 class IGraphicsContext;
32 
33 namespace CameraMatrixUtil {
34 BASE_NS::Math::Mat4X4 CalculateProjectionMatrix(const CameraComponent& cameraComponent, bool& isCameraNegative);
35 }
36 
37 class SceneUtil : public ISceneUtil {
38 public:
39     explicit SceneUtil(IGraphicsContext& graphicsContext);
40     ~SceneUtil() override = default;
41 
42     CORE_NS::Entity CreateCamera(CORE_NS::IEcs& ecs, const BASE_NS::Math::Vec3& position,
43         const BASE_NS::Math::Quat& rotation, float zNear, float zFar, float fovDegrees) const override;
44     void UpdateCameraViewport(
45         CORE_NS::IEcs& ecs, CORE_NS::Entity entity, const BASE_NS::Math::UVec2& renderResolution) const override;
46     void UpdateCameraViewport(CORE_NS::IEcs& ecs, CORE_NS::Entity entity, const BASE_NS::Math::UVec2& renderResolution,
47         bool autoAspect, float fovY, float orthoScale) const override;
48     void CameraLookAt(CORE_NS::IEcs& ecs, CORE_NS::Entity entity, const BASE_NS::Math::Vec3& eye,
49         const BASE_NS::Math::Vec3& target, const BASE_NS::Math::Vec3& up) override;
50 
51     CORE_NS::Entity CreateLight(CORE_NS::IEcs& ecs, const LightComponent& lightComponent,
52         const BASE_NS::Math::Vec3& position, const BASE_NS::Math::Quat& rotation) const override;
53 
54     /** Reflection plane object */
55     struct ReflectionPlane {
56         // Reflection plane entity.
57         CORE_NS::Entity entity;
58         // Reflection plane mesh.
59         CORE_NS::Entity mesh;
60         // Reflection plane material.
61         CORE_NS::Entity material;
62         // Reflection plane color target.
63         CORE_NS::EntityReference colorTarget;
64         // Reflection plane depth target.
65         CORE_NS::EntityReference depthTarget;
66     };
67 
68     void CreateReflectionPlaneComponent(CORE_NS::IEcs& ecs, const CORE_NS::Entity& nodeEntity) override;
69 
70     IAnimationPlayback* RetargetSkinAnimation(CORE_NS::IEcs& ecs, CORE_NS::Entity targetEntity,
71         CORE_NS::Entity sourceEntity, CORE_NS::Entity animationEntity) const override;
72 
73     void GetDefaultMaterialShaderData(CORE_NS::IEcs& ecs, const ISceneUtil::MaterialShaderInfo& info,
74         MaterialComponent::Shader& materialShader, MaterialComponent::Shader& depthShader) const override;
75     void GetDefaultMaterialShaderData(CORE_NS::IEcs& ecs, const ISceneUtil::MaterialShaderInfo& info,
76         const BASE_NS::string_view renderSlot, MaterialComponent::Shader& shader) const override;
77 
78     void ShareSkin(CORE_NS::IEcs& ecs, CORE_NS::Entity targetEntity, CORE_NS::Entity sourceEntity) const override;
79 
80     void RegisterSceneLoader(const ISceneLoader::Ptr& loader) override;
81     void UnregisterSceneLoader(const ISceneLoader::Ptr& loader) override;
82     ISceneLoader::Ptr GetSceneLoader(BASE_NS::string_view uri) const override;
83 
84 private:
85     IGraphicsContext& graphicsContext_;
86     BASE_NS::vector<ISceneLoader::Ptr> sceneLoaders_;
87 };
88 CORE3D_END_NAMESPACE()
89 
90 #endif // CORE_UTIL_SCENE_UTIL_H
91