• 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 OHOS_RENDER_3D_LUME_COMMON_H
17 #define OHOS_RENDER_3D_LUME_COMMON_H
18 
19 #include <3d/ecs/components/camera_component.h>
20 #include <3d/ecs/components/environment_component.h>
21 #include <3d/ecs/components/light_component.h>
22 #include <3d/ecs/components/render_configuration_component.h>
23 #include <3d/ecs/components/post_process_component.h>
24 #include <3d/ecs/components/transform_component.h>
25 #include <3d/ecs/components/material_component.h>
26 #include <3d/ecs/components/name_component.h>
27 #include <3d/ecs/components/node_component.h>
28 #include <3d/ecs/components/render_handle_component.h>
29 #include <3d/ecs/components/render_mesh_component.h>
30 #include <3d/ecs/components/uri_component.h>
31 #include <3d/ecs/systems/intf_morphing_system.h>
32 #include <3d/ecs/systems/intf_node_system.h>
33 #include <3d/ecs/systems/intf_render_system.h>
34 #include <3d/ecs/systems/intf_animation_system.h>
35 #include <3d/gltf/gltf.h>
36 #include <3d/intf_graphics_context.h>
37 #include <3d/implementation_uids.h>
38 
39 #include <base/containers/string_view.h>
40 #include <base/math/mathf.h>
41 #include <base/math/matrix_util.h>
42 #include <base/math/quaternion.h>
43 #include <base/math/quaternion_util.h>
44 #include <base/math/vector.h>
45 #include <base/math/vector_util.h>
46 
47 #include <core/ecs/intf_entity_manager.h>
48 #include <core/engine_info.h>
49 #include <core/intf_engine.h>
50 #include <core/namespace.h>
51 
52 #include <render/intf_plugin.h>
53 #include <render/intf_render_context.h>
54 #include <render/device/intf_device.h>
55 #include <render/implementation_uids.h>
56 
57 #include "custom/lume_custom_render.h"
58 #include "i_engine.h"
59 
60 #if _OPEN_BASIS_
61 #include "implementation_uids.h"
62 #include "product_basis.h"
63 #include "product_content_basis.h"
64 #endif
65 
66 namespace OHOS::Render3D {
67 struct GltfImportInfo {
68     enum AnimationTarget { AnimateMainScene, AnimateImportedScene };
69     const char* fileName_;
70     AnimationTarget target_;
71     CORE3D_NS::GltfResourceImportFlags resourceImportFlags_;
72     CORE3D_NS::GltfSceneImportFlags sceneImportFlags_;
73 };
74 
75 struct LightInfo {
76     int type_;
77     float intensity_;
78     bool shadow_;
79     BASE_NS::Math::Vec3 color_;
80     BASE_NS::Math::Vec3 position_;
81     BASE_NS::Math::Quat rotation_;
82 };
83 
84 class OrbitCameraHelper final {
85 public:
86     OrbitCameraHelper();
87     ~OrbitCameraHelper() = default;
88 
89     OrbitCameraHelper(const OrbitCameraHelper&) = delete;
90     OrbitCameraHelper& operator=(const OrbitCameraHelper&) = delete;
91 
92     void SetOrbitFromEye(const BASE_NS::Math::Vec3& eyePosition, const BASE_NS::Math::Quat& rotation,
93         float orbitDistance);
94 
95     void SetOrbitFromTarget(const BASE_NS::Math::Vec3& targetPosition, const BASE_NS::Math::Quat& rotation,
96         float orbitDistance);
97 
98     BASE_NS::Math::Vec3 GetCameraPosition();
99     BASE_NS::Math::Quat GetCameraRotation();
100     void Update(uint64_t /* delta */);
101     void ResetPointerEvents();
102     void HandlePointerEvent(const PointerEvent& event);
103 
104 private:
105     void OnPress(const PointerEvent& event);
106     void OnRelease(const PointerEvent& event);
107     void OnMove(const PointerEvent& event);
108     void UpdateCameraRotation(float dx, float dy);
109     unsigned int pressedButtonsBits_;
110     // Just a very simple and stupid touch gesture system.
111     int touchPointerCount_;
112     PointerEvent touchPointers_[2];
113     // Double touch handling
114     PointerEvent midPoint_;
115 
116     float orbitDistance_;
117     BASE_NS::Math::Vec3 cameraTargetPosition_;
118     BASE_NS::Math::Quat cameraRotation_;
119 };
120 
121 class LumeCommon : public IEngine {
122 public:
123     LumeCommon() = default;
124     ~LumeCommon() override;
125     void Clone(IEngine* proto) override;
126     void UnloadEngineLib() override;
127     bool LoadEngineLib() override;
128     bool InitEngine(EGLContext eglContext, const PlatformData& data) override;
129     void DeInitEngine() override;
130 
131     void InitializeScene(uint32_t key) override;
132 
133     void LoadEnvModel(const std::string& modelPath, BackgroundType type) override;
134     void LoadSceneModel(const std::string& modelPath) override;
135 
136     void UpdateGeometries(const std::vector<std::shared_ptr<Geometry>>& shapes) override;
137     void UpdateGLTFAnimations(const std::vector<std::shared_ptr<GLTFAnimation>>& animations) override;
138     void UpdateLights(const std::vector<std::shared_ptr<OHOS::Render3D::Light>>& lights) override;
139 
140     void UpdateCustomRender(const std::shared_ptr<CustomRenderDescriptor>& customRender) override;
141     void UpdateShaderPath(const std::string& shaderPath) override;
142     void UpdateImageTexturePaths(const std::vector<std::string>& imageTextures) override;
143     void UpdateShaderInputBuffer(const std::shared_ptr<OHOS::Render3D::ShaderInputBuffer>& shaderInputBuffer) override;
144 
145     void SetupCameraViewPort(uint32_t width, uint32_t height) override;
146     void SetupCameraTransform(const OHOS::Render3D::Position& position, const OHOS::Render3D::Vec3& lookAt,
147         const OHOS::Render3D::Vec3& up, const OHOS::Render3D::Quaternion& rotation) override;
148     void SetupCameraViewProjection(float zNear, float zFar, float fovDegrees) override;
149 
150     void UnloadSceneModel() override;
151     void UnloadEnvModel() override;
152     void DrawFrame() override;
153 
154     void OnTouchEvent(const PointerEvent& event) override;
155     void OnWindowChange(const TextureInfo& textureInfo) override;
156 
157 #if defined(MULTI_ECS_UPDATE_AT_ONCE) && (MULTI_ECS_UPDATE_AT_ONCE == 1)
158     void DeferDraw() override;
159     void DrawMultiEcs(const std::unordered_map<void*, void*>& ecss) override;
160 #endif
161     bool NeedsRepaint() override;
162 
163 protected:
164     virtual CORE_NS::PlatformCreateInfo ToEnginePlatformData(const PlatformData& data) const = 0;
165     virtual void RegisterAssertPath() = 0;
166     void LoadSystemGraph(BASE_NS::string sysGraph);
167     void CreateEcs(uint32_t key);
168     void CreateScene();
169     void DestroyScene();
170     void CreateEnvScene(CORE3D_NS::EnvironmentComponent::Background type);
171     void DestroyEnvScene();
172     void DestroySceneNodeAndRes(CORE_NS::Entity& importedEntity, BASE_NS::vector<CORE3D_NS::GLTFResourceData>& res);
173     void CreateCamera();
174     void LoadCustGeometry(const std::vector<std::shared_ptr<Geometry>>& shapes);
175     void SetupPostprocess();
176     void AddTextureMemoryBarrrier();
177     void SetupCustomRenderTarget(const TextureInfo &info);
178     void CreateLight();
179     void ProcessGLTFAnimations();
180     int FindGLTFAnimationIndex(const std::string& name);
181     void UpdateSingleGLTFAnimation(int index, const std::shared_ptr<GLTFAnimation>& gltfAnimation);
182     bool LoadAndImport(const GltfImportInfo& info, CORE_NS::Entity& importedEntity,
183         BASE_NS::vector<CORE3D_NS::GLTFResourceData>& res);
184 
185     bool CreateSwapchain(void* nativeWindow);
186     bool DestroySwapchain();
187     void DestroyResource();
188     void Tick(const uint64_t deltaTime);
189 
190     CORE_NS::IEngine::Ptr CreateCoreEngine(const Core::PlatformCreateInfo &info);
191     CORE_NS::IEngine::Ptr GetCoreEngine();
192 
193     RENDER_NS::RenderHandleReference SetupGpuImageTarget();
194     RENDER_NS::RenderHandleReference SetupGpuDepthTarget();
195     RENDER_NS::GpuImageDesc GetImageDesc();
196     RENDER_NS::IRenderContext::Ptr CreateRenderContext(EGLContext gfxContext);
197     RENDER_NS::IDevice* GetDevice();
198     RENDER_NS::IRenderContext::Ptr GetRenderContext();
199 
200     CORE3D_NS::IGraphicsContext::Ptr CreateGfx3DContext();
201     CORE3D_NS::IGraphicsContext::Ptr GetGraphicsContext();
202 
203     bool IsValidQuaternion(const OHOS::Render3D::Quaternion& quat);
204     void CollectRenderHandles();
205     void GetLightPositionAndRotation(const std::shared_ptr<OHOS::Render3D::Light>& light,
206         BASE_NS::Math::Vec3& position, BASE_NS::Math::Quat& rotation);
207     std::shared_ptr<LumeCustomRender> CustomRenderFactory(const std::string& renderNodeGraph, bool needsFrameCallback);
208     CORE_NS::IEngine::Ptr engine_;
209     CORE_NS::IEcs::Ptr ecs_;
210     CORE_NS::Entity cameraEntity_;
211     CORE_NS::Entity sceneEntity_;
212     CORE_NS::Entity postprocessEntity_;
213     std::vector<CORE_NS::Entity> lightEntities_;
214 
215     CORE_NS::Entity importedSceneEntity_;
216     BASE_NS::vector<CORE3D_NS::GLTFResourceData> importedSceneResources_;
217 
218     CORE_NS::Entity importedEnvEntity_;
219     BASE_NS::vector<CORE3D_NS::GLTFResourceData> importedEnvResources_;
220 
221     BASE_NS::vector<CORE3D_NS::IAnimationPlayback*> animations_;
222     BASE_NS::Math::Vec3 cameraPosition_{ 0.0f, 0.0f, 4.0f };
223     BASE_NS::Math::Quat cameraRotation_{ 0.0f, 0.0f, 0.0f, 1.0f };
224 
225     CORE3D_NS::IGraphicsContext::Ptr graphicsContext_;
226     CORE3D_NS::ITransformComponentManager* transformManager_;
227     CORE3D_NS::ICameraComponentManager* cameraManager_;
228     CORE3D_NS::IRenderConfigurationComponentManager* sceneManager_;
229     CORE3D_NS::ILightComponentManager* lightManager_;
230     CORE3D_NS::IPostProcessComponentManager* postprocessManager_;
231 
232     RENDER_NS::IRenderContext::Ptr renderContext_;
233     RENDER_NS::RenderHandleReference gpuResourceImgHandle_;
234     RENDER_NS::RenderHandleReference gpuDepthTargetHandle_;
235     RENDER_NS::IDevice *device_ = nullptr;
236     RENDER_NS::RenderHandleReference swapchainHandle_;
237 
238     OrbitCameraHelper orbitCamera_;
239     std::vector<std::shared_ptr<GLTFAnimation>> gltfAnimations_;
240     std::vector<std::shared_ptr<Geometry>> shapes_;
241     std::unordered_map<std::string, std::shared_ptr<Geometry>> shapesMap_;
242 
243     CORE3D_NS::IMaterialComponentManager* materialManager_ { nullptr };
244     CORE3D_NS::IMeshComponentManager* meshManager_ { nullptr };
245     CORE3D_NS::INameComponentManager* nameManager_ { nullptr };
246     CORE3D_NS::IUriComponentManager* uriManager_ { nullptr };
247     CORE3D_NS::IRenderHandleComponentManager* gpuHandleManager_ { nullptr };
248     CORE3D_NS::INodeSystem* nodeSystem_ { nullptr };
249     CORE3D_NS::IRenderMeshComponentManager* renderMeshManager_ { nullptr };
250     // RENDER_NS::RenderHandleReference swapchainHandle_;
251 
252     // Shader
253     std::shared_ptr<LumeCustomRender> customRender_;
254     BASE_NS::vector<RENDER_NS::RenderHandleReference> renderHandles_;
255 
256     bool autoAspect_ = true;
257     float originalYfov_ = BASE_NS::Math::DEG2RAD * 60.0f;
258     float orthoScale_ = 3.0f;
259     bool enablePostprocess_ = true;
260     void *libHandle_ = nullptr;
261     uint32_t key_;
262     float zNear_ = 0.5f;
263     float zFar_ = 200.0f;
264     float fovDegrees_ = 60.0f;
265     bool animProgress_ = false;
266     bool cameraUpdated_ = true;
267     bool needsRedraw_ = false;
268     bool needsFrameCallback_ = false;
269     void* nativeWindow_ = nullptr;
270     bool activateProductBasis_ = false;
271     bool activateProductContentBasis_ = false;
272     bool useMultiSwapChain_ = false;
273     bool loadedScene_ = false;
274     bool hasShaderInput_ = false;
275     EGLSurface eglSurface_ = EGL_NO_SURFACE;
276     TextureInfo textureInfo_;
277 };
278 } // namespace OHOS::Render3D
279 #endif // OHOS_RENDER_3D_LUME_COMMON_H
280