• 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 #ifndef SCENE_SRC_CORE_INTERNAL_SCENE_H
17 #define SCENE_SRC_CORE_INTERNAL_SCENE_H
18 
19 #include <scene/base/types.h>
20 #include <scene/ext/intf_component_factory.h>
21 #include <scene/ext/intf_internal_scene.h>
22 #include <scene/interface/intf_scene.h>
23 #include <shared_mutex>
24 
25 #include <render/intf_render_context.h>
26 
27 #include <meta/api/make_callback.h>
28 #include <meta/ext/object.h>
29 #include <meta/interface/animation/intf_animation.h>
30 #include <meta/interface/intf_task_queue.h>
31 
32 #include "camera.h"
33 #include "ecs.h"
34 #include "ecs_object.h"
35 #include "intf_internal_raycast.h"
36 
SCENE_BEGIN_NAMESPACE()37 SCENE_BEGIN_NAMESPACE()
38 
39 class InternalScene : public META_NS::IntroduceInterfaces<IInternalScene, IInternalRayCast> {
40 public:
41     InternalScene(const IScene::Ptr& scene, META_NS::ITaskQueue::Ptr engine, META_NS::ITaskQueue::Ptr app,
42         BASE_NS::shared_ptr<RENDER_NS::IRenderContext> context);
43 
44     ~InternalScene() override;
45 
46     void SetSelf(const IInternalScene::Ptr& s)
47     {
48         self_ = s;
49     }
50 
51     bool Initialize() override;
52     void Uninitialize() override;
53 
54     META_NS::ITaskQueue::Ptr GetEngineTaskQueue() const override;
55     META_NS::ITaskQueue::Ptr GetAppTaskQueue() const override;
56 
57     INode::Ptr CreateNode(BASE_NS::string_view path, META_NS::ObjectId id) override;
58     INode::Ptr FindNode(BASE_NS::string_view path, META_NS::ObjectId id) const override;
59     INode::Ptr FindNode(CORE_NS::Entity ent, META_NS::ObjectId id) const override;
60     bool ReleaseNode(INode::Ptr&& node, bool recursive) override;
61     bool RemoveNode(const INode::Ptr& node) override;
62     META_NS::IObject::Ptr CreateObject(META_NS::ObjectId id) override;
63     BASE_NS::vector<INode::Ptr> GetChildren(const IEcsObject::Ptr&) const override;
64     bool RemoveChild(
65         const BASE_NS::shared_ptr<IEcsObject>& object, const BASE_NS::shared_ptr<IEcsObject>& child) override;
66     bool AddChild(const BASE_NS::shared_ptr<IEcsObject>& object, const INode::Ptr& child, size_t index) override;
67 
68     BASE_NS::vector<ICamera::Ptr> GetCameras() const override;
69     BASE_NS::vector<META_NS::IAnimation::Ptr> GetAnimations() const override;
70 
71     IEcsContext& GetEcsContext() override
72     {
73         return *ecs_;
74     }
75     RENDER_NS::IRenderContext& GetRenderContext() override
76     {
77         return *renderContext_;
78     }
79     CORE3D_NS::IGraphicsContext& GetGraphicsContext() override
80     {
81         return *graphicsContext3D_;
82     }
83 
84     BASE_NS::shared_ptr<IScene> GetScene() const override;
85     void SchedulePropertyUpdate(const IEcsObject::Ptr& obj) override;
86     void SyncProperties() override;
87     void Update(bool syncProperties = true) override;
88 
89     void RegisterComponent(const BASE_NS::Uid&, const IComponentFactory::Ptr&) override;
90     void UnregisterComponent(const BASE_NS::Uid&) override;
91     BASE_NS::shared_ptr<IComponentFactory> FindComponentFactory(const BASE_NS::Uid&) const override;
92 
93     bool SyncProperty(const META_NS::IProperty::ConstPtr& p, META_NS::EngineSyncDirection dir) override;
94 
95     void AddRenderingCamera(const IInternalCamera::Ptr& camera) override;
96     void RemoveRenderingCamera(const IInternalCamera::Ptr& camera) override;
97 
98     bool SetRenderMode(RenderMode) override;
99     RenderMode GetRenderMode() const override;
100     void SetSystemGraphUri(const BASE_NS::string& uri) override;
101     BASE_NS::string GetSystemGraphUri() override;
102 
103     void AppendCustomRenderNodeGraph(RENDER_NS::RenderHandleReference rng) override;
104     void RenderFrame() override;
105     bool HasPendingRender() const override;
106 
107 public:
108     NodeHits CastRay(
109         const BASE_NS::Math::Vec3& pos, const BASE_NS::Math::Vec3& dir, const RayCastOptions& options) const override;
110     NodeHits CastRay(const IEcsObject::ConstPtr& entity, const BASE_NS::Math::Vec2& pos,
111         const RayCastOptions& options) const override;
112     BASE_NS::Math::Vec3 ScreenPositionToWorld(
113         const IEcsObject::ConstPtr& entity, const BASE_NS::Math::Vec3& pos) const override;
114     BASE_NS::Math::Vec3 WorldPositionToScreen(
115         const IEcsObject::ConstPtr& entity, const BASE_NS::Math::Vec3& pos) const override;
116 
117 private:
118     INode::Ptr ConstructNode(CORE_NS::Entity, META_NS::ObjectId id) const;
119     INode::Ptr ConstructNodeImpl(CORE_NS::Entity ent, INode::Ptr node) const;
120     IComponent::Ptr CreateComponent(CORE_NS::IComponentManager* m, const IEcsObject::Ptr& ecsObject) const;
121     void AttachComponents(const INode::Ptr& node, const IEcsObject::Ptr& ecsObject, CORE_NS::Entity ent) const;
122     META_NS::ObjectId DeducePrimaryNodeType(CORE_NS::Entity ent) const;
123     void NotifyRenderingCameras();
124 
125     NodeHits MapHitResults(const BASE_NS::vector<CORE3D_NS::RayCastResult>& res, const RayCastOptions& options) const;
126     bool UpdateSyncProperties(bool resetPending);
127     void ReleaseChildNodes(const IEcsObject::Ptr& eobj);
128     void RemoveNodesRecursively(CORE_NS::Entity);
129 
130 private:
131     IScene::WeakPtr scene_;
132     IInternalScene::WeakPtr self_;
133     META_NS::ITaskQueue::Ptr engineQueue_;
134     META_NS::ITaskQueue::Ptr appQueue_;
135     BASE_NS::shared_ptr<RENDER_NS::IRenderContext> renderContext_;
136     CORE3D_NS::IGraphicsContext::Ptr graphicsContext3D_;
137 
138     BASE_NS::unique_ptr<Ecs> ecs_;
139     BASE_NS::unordered_map<BASE_NS::Uid, IComponentFactory::Ptr> componentFactories_;
140 
141     mutable BASE_NS::unordered_map<CORE_NS::Entity, INode::Ptr> nodes_;
142     mutable BASE_NS::unordered_map<CORE_NS::Entity, META_NS::IAnimation::WeakPtr> animations_;
143     mutable BASE_NS::vector<IInternalCamera::WeakPtr> renderingCameras_;
144 
145     uint64_t firstTime_ { ~0u };
146     uint64_t previousFrameTime_ { ~0u };
147     uint64_t deltaTime_ { 1u };
148     RenderMode mode_ { RenderMode::ALWAYS };
149     BASE_NS::string systemGraph_ = "rofs3D://systemGraph.json";
150     BASE_NS::vector<RENDER_NS::RenderHandleReference> customRenderNodeGraphs_;
151 
152 private: // locked bits
153     mutable std::shared_mutex mutex_;
154     bool pendingRender_ {};
155     BASE_NS::unordered_map<void*, IEcsObject::WeakPtr> syncs_;
156 };
157 
158 SCENE_END_NAMESPACE()
159 
160 #endif