1 /*
2  * Copyright (c) 2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_SCENE_VIEWER_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_SCENE_VIEWER_COMPONENT_H
18 
19 #include <string>
20 
21 #include "base/geometry/animatable_float.h"
22 #include "base/geometry/quaternion.h"
23 #include "base/geometry/vec3.h"
24 #include "base/utils/utils.h"
25 #include "core/pipeline/base/element.h"
26 #include "core/pipeline/base/measurable.h"
27 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/custom/custom_render_descriptor.h"
28 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/constants.h"
29 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/geometry/geometry.h"
30 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/gltf_animation.h"
31 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/light.h"
32 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/position.h"
33 
34 namespace OHOS::Ace {
35 
36 constexpr double INVALID_DOUBLE = std::numeric_limits<double>::max();
37 
38 // A component can show glTF model.
39 class SceneViewerComponent : public RenderComponent, public Measurable {
40     DECLARE_ACE_TYPE(SceneViewerComponent, RenderComponent, Measurable);
41 
42 public:
43     SceneViewerComponent() = default;
44     ~SceneViewerComponent() override = default;
45     RefPtr<Element> CreateElement() override;
46     RefPtr<RenderNode> CreateRenderNode() override;
47 
SceneViewerComponent(const std::string & src)48     SceneViewerComponent(const std::string& src)
49     {
50         gltfSrc_ = src;
51     }
52 
GetGltfSrc()53     const std::string& GetGltfSrc() const
54     {
55         return gltfSrc_;
56     }
57 
SetGltfSrc(const std::string & src)58     void SetGltfSrc(const std::string& src)
59     {
60         gltfSrc_ = src;
61     }
62 
GetClickedEventId()63     const EventMarker& GetClickedEventId() const
64     {
65         return clickEventId_;
66     }
67 
SetClickedEventId(const EventMarker & onClickId)68     void SetClickedEventId(const EventMarker& onClickId)
69     {
70         clickEventId_ = onClickId;
71     }
72 
SetHandleCameraMove(bool enabled)73     void SetHandleCameraMove(bool enabled)
74     {
75         isHandleCameraMove_ = enabled;
76     }
77 
IsHandleCameraMove()78     bool IsHandleCameraMove() const
79     {
80         return isHandleCameraMove_;
81     }
82 
SetCameraRotation(Quaternion quat)83     void SetCameraRotation(Quaternion quat)
84     {
85         cameraRotation_ = quat;
86     }
87 
GetCameraRotation()88     const Quaternion GetCameraRotation() const
89     {
90         return cameraRotation_;
91     }
92 
SetCameraPosition(AnimatableFloat x,AnimatableFloat y,AnimatableFloat z,AnimatableFloat distance,bool isAngular)93     void SetCameraPosition(AnimatableFloat x, AnimatableFloat y, AnimatableFloat z,
94         AnimatableFloat distance, bool isAngular)
95     {
96         cameraPosition_.Set(x, y, z, distance, isAngular);
97     }
98 
GetCameraPosition()99     const OHOS::Render3D::Position& GetCameraPosition()
100     {
101         return cameraPosition_;
102     }
103 
GetCameraPositionVec()104     const Vec3 GetCameraPositionVec()
105     {
106         return cameraPosition_.GetVec();
107     }
108 
GetCameraPosDistance()109     const AnimatableFloat GetCameraPosDistance() const
110     {
111         return cameraPosition_.GetDistance();
112     }
113 
GetIsCameraPosInAngles()114     bool GetIsCameraPosInAngles()
115     {
116         return cameraPosition_.GetIsAngular();
117     }
118 
SetCameraLookAt(Vec3 lookAtVec)119     void SetCameraLookAt(Vec3 lookAtVec)
120     {
121         cameraLookAtVec_ = lookAtVec;
122     }
123 
GetCameraLookAtVec()124     const Vec3 GetCameraLookAtVec() const
125     {
126         return cameraLookAtVec_;
127     }
128 
SetCameraUp(Vec3 upVec)129     void SetCameraUp(Vec3 upVec)
130     {
131         cameraUpVec_ = upVec;
132     }
133 
GetCameraUpVec()134     const Vec3 GetCameraUpVec() const
135     {
136         return cameraUpVec_;
137     }
138 
SetCameraFrustum(float zNear,float zFar,float fovDegrees)139     void SetCameraFrustum(float zNear, float zFar, float fovDegrees)
140     {
141         zNear_ = zNear;
142         zFar_ = zFar;
143         fovDegrees_ = fovDegrees;
144     }
145 
GetZNear()146     float GetZNear() const
147     {
148         return zNear_;
149     }
150 
GetZFar()151     float GetZFar() const
152     {
153         return zFar_;
154     }
155 
GetFovDegrees()156     float GetFovDegrees() const
157     {
158         return fovDegrees_;
159     }
160 
IsTransparent()161     bool IsTransparent() const
162     {
163         return isTransparent_;
164     }
165 
SetTransparent(bool enabled)166     void SetTransparent(bool enabled)
167     {
168         isTransparent_ = enabled;
169     }
170 
GetBackgroundSrc()171     const std::string& GetBackgroundSrc() const
172     {
173         return backgroundSrc_;
174     }
175 
SetBackgroundSrc(const std::string & src)176     void SetBackgroundSrc(const std::string& src)
177     {
178         backgroundSrc_ = src;
179     }
180 
AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry> & shape)181     void AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry>& shape)
182     {
183         shapes_.push_back(shape);
184     }
185 
GetGeometries()186     std::vector<RefPtr<OHOS::Render3D::SVGeometry>>& GetGeometries()
187     {
188         return shapes_;
189     }
190 
AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation> & animation)191     void AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation>& animation)
192     {
193         gltfAnimations_.push_back(animation);
194     }
195 
GetGLTFAnimations()196     std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>>& GetGLTFAnimations()
197     {
198         return gltfAnimations_;
199     }
200 
AddLight(const RefPtr<OHOS::Render3D::SVLight> & light)201     void AddLight(const RefPtr<OHOS::Render3D::SVLight>& light)
202     {
203         lights_.push_back(light);
204     }
205 
GetLights()206     std::vector<RefPtr<OHOS::Render3D::SVLight>>& GetLights()
207     {
208         return lights_;
209     }
210 
AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor> & customRender)211     void AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>& customRender)
212     {
213         customRenders_.push_back(customRender);
214     }
215 
GetCustomRenders()216     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>>& GetCustomRenders()
217     {
218         return customRenders_;
219     }
220 
221 private:
222     std::string gltfSrc_;
223     std::string backgroundSrc_;
224     EventMarker clickEventId_;
225     // Handle camera move by default.
226     bool isHandleCameraMove_ = true;
227     bool isTransparent_ = false;
228 
229     // Camera properties.
230     float zNear_ = 0.5f;
231     float zFar_ = 50.0f;
232     float fovDegrees_ = 60.0f;
233     OHOS::Render3D::Position cameraPosition_;
234     Vec3 cameraLookAtVec_ { 0.0f, 0.0f, 0.0f };
235     Vec3 cameraUpVec_ { 0.0f, 1.0f, 0.0f };
236 
237     // Invalid by default. Either camera of rotation or lookat is used at the end.
238     Quaternion cameraRotation_ { INVALID_DOUBLE, INVALID_DOUBLE, INVALID_DOUBLE, INVALID_DOUBLE };
239 
240     // Light objects.
241     std::vector<RefPtr<OHOS::Render3D::SVLight>> lights_;
242 
243     // Geometry
244     std::vector<RefPtr<OHOS::Render3D::SVGeometry>> shapes_;
245 
246     // GLTF Animations.
247     std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>> gltfAnimations_;
248 
249     // Shader
250     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>> customRenders_;
251 };
252 
253 } // namespace OHOS::Ace
254 
255 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_SCENE_VIEWER_COMPONENT_H
256