• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         LOGD("ACE-3D SceneViewerComponent::SetGltfSrc() %s", src.c_str());
61         gltfSrc_ = src;
62     }
63 
GetClickedEventId()64     const EventMarker& GetClickedEventId() const
65     {
66         return clickEventId_;
67     }
68 
SetClickedEventId(const EventMarker & onClickId)69     void SetClickedEventId(const EventMarker& onClickId)
70     {
71         clickEventId_ = onClickId;
72     }
73 
SetHandleCameraMove(bool enabled)74     void SetHandleCameraMove(bool enabled)
75     {
76         isHandleCameraMove_ = enabled;
77     }
78 
IsHandleCameraMove()79     bool IsHandleCameraMove() const
80     {
81         return isHandleCameraMove_;
82     }
83 
SetCameraRotation(Quaternion quat)84     void SetCameraRotation(Quaternion quat)
85     {
86         cameraRotation_ = quat;
87     }
88 
GetCameraRotation()89     const Quaternion GetCameraRotation() const
90     {
91         return cameraRotation_;
92     }
93 
SetCameraPosition(AnimatableFloat x,AnimatableFloat y,AnimatableFloat z,AnimatableFloat distance,bool isAngular)94     void SetCameraPosition(AnimatableFloat x, AnimatableFloat y, AnimatableFloat z,
95         AnimatableFloat distance, bool isAngular)
96     {
97         cameraPosition_.Set(x, y, z, distance, isAngular);
98     }
99 
GetCameraPosition()100     const OHOS::Render3D::Position& GetCameraPosition()
101     {
102         return cameraPosition_;
103     }
104 
GetCameraPositionVec()105     const Vec3 GetCameraPositionVec()
106     {
107         return cameraPosition_.GetVec();
108     }
109 
GetCameraPosDistance()110     const AnimatableFloat GetCameraPosDistance() const
111     {
112         return cameraPosition_.GetDistance();
113     }
114 
GetIsCameraPosInAngles()115     bool GetIsCameraPosInAngles()
116     {
117         return cameraPosition_.GetIsAngular();
118     }
119 
SetCameraLookAt(Vec3 lookAtVec)120     void SetCameraLookAt(Vec3 lookAtVec)
121     {
122         cameraLookAtVec_ = lookAtVec;
123     }
124 
GetCameraLookAtVec()125     const Vec3 GetCameraLookAtVec() const
126     {
127         return cameraLookAtVec_;
128     }
129 
SetCameraUp(Vec3 upVec)130     void SetCameraUp(Vec3 upVec)
131     {
132         cameraUpVec_ = upVec;
133     }
134 
GetCameraUpVec()135     const Vec3 GetCameraUpVec() const
136     {
137         return cameraUpVec_;
138     }
139 
SetCameraFrustum(float zNear,float zFar,float fovDegrees)140     void SetCameraFrustum(float zNear, float zFar, float fovDegrees)
141     {
142         zNear_ = zNear;
143         zFar_ = zFar;
144         fovDegrees_ = fovDegrees;
145     }
146 
GetZNear()147     float GetZNear() const
148     {
149         return zNear_;
150     }
151 
GetZFar()152     float GetZFar() const
153     {
154         return zFar_;
155     }
156 
GetFovDegrees()157     float GetFovDegrees() const
158     {
159         return fovDegrees_;
160     }
161 
IsTransparent()162     bool IsTransparent() const
163     {
164         return isTransparent_;
165     }
166 
SetTransparent(bool enabled)167     void SetTransparent(bool enabled)
168     {
169         isTransparent_ = enabled;
170     }
171 
GetBackgroundSrc()172     const std::string& GetBackgroundSrc() const
173     {
174         return backgroundSrc_;
175     }
176 
SetBackgroundSrc(const std::string & src)177     void SetBackgroundSrc(const std::string& src)
178     {
179         backgroundSrc_ = src;
180     }
181 
AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry> & shape)182     void AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry>& shape)
183     {
184         shapes_.push_back(shape);
185     }
186 
GetGeometries()187     std::vector<RefPtr<OHOS::Render3D::SVGeometry>>& GetGeometries()
188     {
189         return shapes_;
190     }
191 
AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation> & animation)192     void AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation>& animation)
193     {
194         gltfAnimations_.push_back(animation);
195     }
196 
GetGLTFAnimations()197     std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>>& GetGLTFAnimations()
198     {
199         return gltfAnimations_;
200     }
201 
AddLight(const RefPtr<OHOS::Render3D::SVLight> & light)202     void AddLight(const RefPtr<OHOS::Render3D::SVLight>& light)
203     {
204         lights_.push_back(light);
205     }
206 
GetLights()207     std::vector<RefPtr<OHOS::Render3D::SVLight>>& GetLights()
208     {
209         return lights_;
210     }
211 
AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor> & customRender)212     void AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>& customRender)
213     {
214         customRenders_.push_back(customRender);
215     }
216 
GetCustomRenders()217     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>>& GetCustomRenders()
218     {
219         return customRenders_;
220     }
221 
222 private:
223     std::string gltfSrc_;
224     std::string backgroundSrc_;
225     EventMarker clickEventId_;
226     // Handle camera move by default.
227     bool isHandleCameraMove_ = true;
228     bool isTransparent_ = false;
229 
230     // Camera properties.
231     float zNear_ = 0.5f;
232     float zFar_ = 50.0f;
233     float fovDegrees_ = 60.0f;
234     OHOS::Render3D::Position cameraPosition_;
235     Vec3 cameraLookAtVec_ { 0.0f, 0.0f, 0.0f };
236     Vec3 cameraUpVec_ { 0.0f, 1.0f, 0.0f };
237 
238     // Invalid by default. Either camera of rotation or lookat is used at the end.
239     Quaternion cameraRotation_ { INVALID_DOUBLE, INVALID_DOUBLE, INVALID_DOUBLE, INVALID_DOUBLE };
240 
241     // Light objects.
242     std::vector<RefPtr<OHOS::Render3D::SVLight>> lights_;
243 
244     // Geometry
245     std::vector<RefPtr<OHOS::Render3D::SVGeometry>> shapes_;
246 
247     // GLTF Animations.
248     std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>> gltfAnimations_;
249 
250     // Shader
251     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>> customRenders_;
252 };
253 
254 } // namespace OHOS::Ace
255 
256 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_SCENE_VIEWER_COMPONENT_H
257