• 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_INTERFACE_IRAYCAST_H
17 #define SCENE_INTERFACE_IRAYCAST_H
18 
19 #include <scene/base/types.h>
20 #include <scene/interface/intf_layer.h>
21 #include <scene/interface/intf_node.h>
22 
23 SCENE_BEGIN_NAMESPACE()
24 
25 /**
26  * @brief Options for casting a ray
27  */
28 struct RayCastOptions {
29     /**
30      * @brief Include only nodes that belong to the layers in the mask.
31      *        If casted from camera, the camera's LayerMask is used, otherwise ALL_LAYER_MASK.
32      */
33     uint64_t layerMask = NONE_LAYER_MASK;
34     /**
35      * @brief Include only nodes from this hierarchy
36      */
37     INode::ConstPtr node;
38 };
39 
40 /**
41  * @brief Ray/Node hit position
42  */
43 struct NodeHit {
44     /// Node the ray hit
45     INode::Ptr node;
46     /// Distance of the hit from the start position
47     float distance {};
48     /// Distance of the AABB center from the start position
49     float distanceToCenter {};
50     /// World position of the hit
51     BASE_NS::Math::Vec3 position {};
52 };
53 
54 using NodeHits = BASE_NS::vector<NodeHit>;
55 
56 /**
57  * @brief Interface to cast a ray and get list of intersections
58  */
59 class IRayCast : public CORE_NS::IInterface {
60     META_INTERFACE(CORE_NS::IInterface, IRayCast, "050c1a67-e0f8-4fba-8207-4dbea377a2be")
61 public:
62     /**
63      * @brief Cast a ray from position and return all intersections
64      * @param pos Start position (World coordinates)
65      * @param dir Direction of the ray
66      * @param options Ray cast options
67      * @return List of hits, ordered from closest to furthest
68      */
69     virtual Future<NodeHits> CastRay(
70         const BASE_NS::Math::Vec3& pos, const BASE_NS::Math::Vec3& dir, const RayCastOptions& options) const = 0;
71 };
72 
73 /**
74  * @brief Interface to cast a ray from camera and get list of intersections
75  */
76 class ICameraRayCast : public CORE_NS::IInterface {
77     META_INTERFACE(CORE_NS::IInterface, ICameraRayCast, "b3bbdb5d-91aa-4107-adfe-c2e277607eeb")
78 public:
79     /**
80      * @brief Cast a ray from camera 2D screen coordinates to the camera direction
81      * @param pos Start position (Normalized screen coordinates, where (0, 0) is the upper left corner of the screen and
82      * (1, 1) the lower right corner.)
83      * @param options Ray cast options
84      * @return List of hits, ordered from closest to furthest
85      */
86     virtual Future<NodeHits> CastRay(const BASE_NS::Math::Vec2& pos, const RayCastOptions& options) const = 0;
87 
88     /**
89      * @brief Project a screen position onto a plane in the 3D scene.
90      * @param pos 2D normalized screen coordinates and the z coordinate determines the projection plane's
91      *            location between the near clipping plane (0.0) and the far clipping plane (1.0).
92      * @return Position in world coordinates
93      */
94     virtual Future<BASE_NS::Math::Vec3> ScreenPositionToWorld(const BASE_NS::Math::Vec3& pos) const = 0;
95     /**
96      * @brief Project a position from the 3D scene onto the screen.
97      * @param pos 3D world coordinates.
98      * @return Normalized screen coordinates.
99      */
100     virtual Future<BASE_NS::Math::Vec3> WorldPositionToScreen(const BASE_NS::Math::Vec3& pos) const = 0;
101 };
102 
103 SCENE_END_NAMESPACE()
104 
105 META_TYPE(SCENE_NS::RayCastOptions)
106 META_TYPE(SCENE_NS::NodeHit)
107 
108 META_INTERFACE_TYPE(SCENE_NS::IRayCast)
109 META_INTERFACE_TYPE(SCENE_NS::ICameraRayCast)
110 
111 #endif
112