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