• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ecs/components/camera_component.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& ecs = scene->GetEcsContext();
30     auto ccm = CORE_NS::GetManager<CORE3D_NS::ICameraComponentManager>(*ecs.GetNativeEcs());
31     if (!ccm) {
32         return {};
33     }
34 
35     auto ent = ecs.GetNativeEcs()->GetEntityManager().Create();
36     ecs.AddDefaultComponents(ent);
37 
38     CORE3D_NS::CameraComponent cc;
39     cc.sceneFlags |= CORE3D_NS::CameraComponent::SceneFlagBits::ACTIVE_RENDER_BIT;
40     ccm->Set(ent, cc);
41 
42     return ent;
43 }
44 
SetEcsObject(const IEcsObject::Ptr & o)45 bool CameraNode::SetEcsObject(const IEcsObject::Ptr& o)
46 {
47     if (Super::SetEcsObject(o)) {
48         auto att = GetSelf<META_NS::IAttach>()->GetAttachments<ICamera>();
49         if (!att.empty()) {
50             camera_ = att.front();
51             return true;
52         }
53     }
54     return false;
55 }
56 
SetActive(bool active)57 Future<bool> CameraNode::SetActive(bool active)
58 {
59     CORE_ASSERT(camera_);
60     return camera_->SetActive(active);
61 }
IsActive() const62 bool CameraNode::IsActive() const
63 {
64     CORE_ASSERT(camera_);
65     return camera_->IsActive();
66 }
SetRenderTarget(const IRenderTarget::Ptr & target)67 Future<bool> CameraNode::SetRenderTarget(const IRenderTarget::Ptr& target)
68 {
69     CORE_ASSERT(camera_);
70     return camera_->SetRenderTarget(target);
71 }
72 
CastRay(const BASE_NS::Math::Vec2 & pos,const RayCastOptions & options) const73 Future<NodeHits> CameraNode::CastRay(const BASE_NS::Math::Vec2& pos, const RayCastOptions& options) const
74 {
75     CORE_ASSERT(camera_);
76     if (auto i = interface_cast<ICameraRayCast>(camera_)) {
77         return i->CastRay(pos, options);
78     }
79     return {};
80 }
SendInputEvent(PointerEvent & event)81 void CameraNode::SendInputEvent(PointerEvent& event)
82 {
83     CORE_ASSERT(camera_);
84     camera_->SendInputEvent(event);
85 }
ScreenPositionToWorld(const BASE_NS::Math::Vec3 & pos) const86 Future<BASE_NS::Math::Vec3> CameraNode::ScreenPositionToWorld(const BASE_NS::Math::Vec3& pos) const
87 {
88     CORE_ASSERT(camera_);
89     if (auto i = interface_cast<ICameraRayCast>(camera_)) {
90         return i->ScreenPositionToWorld(pos);
91     }
92     return {};
93 }
WorldPositionToScreen(const BASE_NS::Math::Vec3 & pos) const94 Future<BASE_NS::Math::Vec3> CameraNode::WorldPositionToScreen(const BASE_NS::Math::Vec3& pos) const
95 {
96     CORE_ASSERT(camera_);
97     if (auto i = interface_cast<ICameraRayCast>(camera_)) {
98         return i->WorldPositionToScreen(pos);
99     }
100     return {};
101 }
102 
103 SCENE_END_NAMESPACE()
104