• 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 #include "bridge/declarative_frontend/jsview/models/model_view_impl.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
20 #include "bridge/declarative_frontend/jsview/js_utils.h"
21 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
22 #include "bridge/declarative_frontend/view_stack_processor.h"
23 #include "core/components_ng/base/view_abstract_model.h"
24 
25 namespace OHOS::Ace::Framework {
26 
27 #define GET_COMPONENT_OR_RETURN()                                                       \
28     auto top = ViewStackProcessor::GetInstance()->GetMainComponent();                   \
29     auto svComponent = AceType::DynamicCast<OHOS::Ace::SceneViewerComponent>(top);      \
30     if (!svComponent) {                                                                 \
31         LOGE("%s Component is not SceneViewComponent", __func__);                       \
32         return;                                                                         \
33     }                                                                                   \
34     svComponent
35 
Create(const std::string & src)36 void ModelViewImpl::Create(const std::string& src)
37 {
38     LOGD("ModelViewImpl::Create()");
39     auto svComponent = AceType::MakeRefPtr<OHOS::Ace::SceneViewerComponent>();
40 
41     LOGD("fullSrcPath: %s", src.c_str());
42     svComponent->SetGltfSrc(src);
43 
44     ViewStackProcessor::GetInstance()->Push(svComponent);
45 }
46 
SetBackground(const std::string & value)47 void ModelViewImpl::SetBackground(const std::string& value)
48 {
49     LOGD("fullBgPath: %s", value.c_str());
50     GET_COMPONENT_OR_RETURN()->SetBackgroundSrc(value);
51 }
52 
SetHandleCameraMove(const bool value)53 void ModelViewImpl::SetHandleCameraMove(const bool value)
54 {
55     GET_COMPONENT_OR_RETURN()->SetHandleCameraMove(value);
56 }
57 
SetTransparent(const bool value)58 void ModelViewImpl::SetTransparent(const bool value)
59 {
60     GET_COMPONENT_OR_RETURN()->SetTransparent(value);
61 }
62 
SetCameraPosition(AnimatableFloat x,AnimatableFloat y,AnimatableFloat z,AnimatableFloat distance,bool isAngular)63 void ModelViewImpl::SetCameraPosition(AnimatableFloat x, AnimatableFloat y, AnimatableFloat z,
64     AnimatableFloat distance, bool isAngular)
65 {
66     GET_COMPONENT_OR_RETURN()->SetCameraPosition(x, y, z, distance, isAngular);
67 }
68 
SetCameraRotation(Quaternion quat)69 void ModelViewImpl::SetCameraRotation(Quaternion quat)
70 {
71     GET_COMPONENT_OR_RETURN()->SetCameraRotation(quat);
72 }
73 
SetCameraFrustum(float zNear,float zFar,float fovDegrees)74 void ModelViewImpl::SetCameraFrustum(float zNear, float zFar, float fovDegrees)
75 {
76     GET_COMPONENT_OR_RETURN()->SetCameraFrustum(zNear, zFar, fovDegrees);
77 }
78 
SetCameraLookAt(Vec3 lookAtVec)79 void ModelViewImpl::SetCameraLookAt(Vec3 lookAtVec)
80 {
81     GET_COMPONENT_OR_RETURN()->SetCameraLookAt(lookAtVec);
82 }
83 
SetCameraUp(Vec3 upVec)84 void ModelViewImpl::SetCameraUp(Vec3 upVec)
85 {
86     GET_COMPONENT_OR_RETURN()->SetCameraUp(upVec);
87 }
88 
AddLight(const RefPtr<OHOS::Render3D::SVLight> & light)89 void ModelViewImpl::AddLight(const RefPtr<OHOS::Render3D::SVLight>& light)
90 {
91     GET_COMPONENT_OR_RETURN()->AddLight(light);
92 }
93 
AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry> & shape)94 void ModelViewImpl::AddGeometry(const RefPtr<OHOS::Render3D::SVGeometry>& shape)
95 {
96     GET_COMPONENT_OR_RETURN()->AddGeometry(shape);
97 }
98 
AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation> & animation)99 void ModelViewImpl::AddGLTFAnimation(const RefPtr<OHOS::Render3D::GLTFAnimation>& animation)
100 {
101     GET_COMPONENT_OR_RETURN()->AddGLTFAnimation(animation);
102 }
103 
AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor> & customRender)104 void ModelViewImpl::AddCustomRender(const RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>& customRender)
105 {
106     GET_COMPONENT_OR_RETURN()->AddCustomRender(customRender);
107 }
108 
SetWidth(Dimension & width)109 void ModelViewImpl::SetWidth(Dimension& width)
110 {
111     LOGD("ModelViewImpl::SetWidth() %f", width.Value());
112     /*
113     Model's texture width & height to be updated in the backend during animation via RenderNode.
114     So Async animation shall be disabled.
115     */
116     auto* stack = ViewStackProcessor::GetInstance();
117     auto option = stack->GetImplicitAnimationOption();
118     option.SetAllowRunningAsynchronously(false);
119     stack->SetImplicitAnimationOption(option);
120     if (LessNotEqual(width.Value(), 0.0)) {
121         width.SetValue(0.0);
122     }
123     ViewAbstractModel::GetInstance()->SetWidth(width);
124 }
125 
SetHeight(Dimension & height)126 void ModelViewImpl::SetHeight(Dimension& height)
127 {
128     LOGD("ModelViewImpl::SetHeight() %f", height.Value());
129     auto* stack = ViewStackProcessor::GetInstance();
130     auto option = stack->GetImplicitAnimationOption();
131     option.SetAllowRunningAsynchronously(false);
132     stack->SetImplicitAnimationOption(option);
133     if (LessNotEqual(height.Value(), 0.0)) {
134         height.SetValue(0.0);
135     }
136     ViewAbstractModel::GetInstance()->SetHeight(height);
137 }
138 
SetShader(const std::string & path)139 void ModelViewImpl::SetShader(const std::string& path)
140 {
141     LOGD("ModelViewImpl::SetShader() NOT IMPLEMENTED YET");
142 }
143 
AddShaderImageTexture(const std::string & path)144 void ModelViewImpl::AddShaderImageTexture(const std::string& path)
145 {
146     LOGD("ModelViewImpl::AddShaderImageTexture() NOT IMPLEMENTED YET");
147 }
148 
AddShaderInputBuffer(const RefPtr<OHOS::Render3D::ShaderInputBuffer> & buffer)149 void ModelViewImpl::AddShaderInputBuffer(const RefPtr<OHOS::Render3D::ShaderInputBuffer>& buffer)
150 {
151     LOGD("ModelViewImpl::AddShaderInputBuffer() NOT IMPLEMENTED YET");
152 }
153 
154 } // namespace OHOS::Ace::Framework
155