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 #include "camera_node.h"
17
18 #include <3d/util/intf_scene_util.h>
19
SCENE_BEGIN_NAMESPACE()20 SCENE_BEGIN_NAMESPACE()
21
22 bool CameraNode::Build(const META_NS::IMetadata::Ptr& d)
23 {
24 return Super::Build(d);
25 }
26
CreateEntity(const IInternalScene::Ptr & scene)27 CORE_NS::Entity CameraNode::CreateEntity(const IInternalScene::Ptr& scene)
28 {
29 const auto& sceneUtil = scene->GetGraphicsContext().GetSceneUtil();
30 return sceneUtil.CreateCamera(
31 *scene->GetEcsContext().GetNativeEcs(), BASE_NS::Math::Vec3(0.0f, 0.0f, 2.5f), {}, 0.1f, 100.f, 60.f);
32 }
33
SetEcsObject(const IEcsObject::Ptr & o)34 bool CameraNode::SetEcsObject(const IEcsObject::Ptr& o)
35 {
36 if (Super::SetEcsObject(o)) {
37 auto att = GetSelf<META_NS::IAttach>()->GetAttachments<ICamera>();
38 if (!att.empty()) {
39 camera_ = att.front();
40 return true;
41 }
42 }
43 return false;
44 }
45
SetActive(bool active)46 Future<bool> CameraNode::SetActive(bool active)
47 {
48 CORE_ASSERT(camera_);
49 return camera_->SetActive(active);
50 }
IsActive() const51 bool CameraNode::IsActive() const
52 {
53 CORE_ASSERT(camera_);
54 return camera_->IsActive();
55 }
SetRenderTarget(const IRenderTarget::Ptr & target)56 Future<bool> CameraNode::SetRenderTarget(const IRenderTarget::Ptr& target)
57 {
58 CORE_ASSERT(camera_);
59 return camera_->SetRenderTarget(target);
60 }
61
CastRay(const BASE_NS::Math::Vec2 & pos,const RayCastOptions & options) const62 Future<NodeHits> CameraNode::CastRay(const BASE_NS::Math::Vec2& pos, const RayCastOptions& options) const
63 {
64 CORE_ASSERT(camera_);
65 if (auto i = interface_cast<ICameraRayCast>(camera_)) {
66 return i->CastRay(pos, options);
67 }
68 return {};
69 }
ScreenPositionToWorld(const BASE_NS::Math::Vec3 & pos) const70 Future<BASE_NS::Math::Vec3> CameraNode::ScreenPositionToWorld(const BASE_NS::Math::Vec3& pos) const
71 {
72 CORE_ASSERT(camera_);
73 if (auto i = interface_cast<ICameraRayCast>(camera_)) {
74 return i->ScreenPositionToWorld(pos);
75 }
76 return {};
77 }
WorldPositionToScreen(const BASE_NS::Math::Vec3 & pos) const78 Future<BASE_NS::Math::Vec3> CameraNode::WorldPositionToScreen(const BASE_NS::Math::Vec3& pos) const
79 {
80 CORE_ASSERT(camera_);
81 if (auto i = interface_cast<ICameraRayCast>(camera_)) {
82 return i->WorldPositionToScreen(pos);
83 }
84 return {};
85 }
86 SCENE_END_NAMESPACE()