Toast Engine
Loading...
Searching...
No Matches
node_3d.hpp
Go to the documentation of this file.
1
8
9#pragma once
10#include "box.hpp"
11#include "node.hpp"
13
14#include <optional>
15#include <vector>
16
17#define GLM_ENABLE_EXPERIMENTAL
18#include <glm/glm.hpp>
19#include <glm/gtc/matrix_transform.hpp>
20#include <glm/gtc/quaternion.hpp>
21#include <glm/gtx/matrix_decompose.hpp>
22
23namespace toast {
24
25class [[ToastNode, Color("Red")]] TOAST_API Node3D : public Node {
26 friend class INodeOwner;
27
28public:
29 // clang-format off
30 [[Reflect, Unit("m")]] alignas(16) mutable glm::vec3 position = glm::vec3(0.0f);
31 [[Reflect, Unit("°")]] alignas(16) mutable glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
32 [[Reflect]] alignas(16) mutable glm::vec3 scale = glm::vec3(1.0f);
33
34 [[Reflect, NoSerialize, Unit("m"), Group("World Transform")]] alignas(16) mutable glm::vec3 world_position = glm::vec3(0.0f);
35 [[Reflect, NoSerialize, Unit("°"), Group("World Transform")]] alignas(16) mutable glm::quat world_rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
36 [[Reflect, NoSerialize, Group("World Transform")]] alignas(16) mutable glm::vec3 world_scale = glm::vec3(1.0f);
37 // clang-format on
38
46 [[Reflect]]
47 auto raycast(
48 glm::vec3 dir, glm::vec3 offset = glm::vec3(0.0f), float max_distance = -1.0f, bool ignore_self = true,
49 const std::vector<int>& layers = std::vector<int> {}
50 ) const -> std::optional<physics::RayHit>;
51
56 [[Reflect]]
57 auto raymulticast(
58 glm::vec3 dir, glm::vec3 offset = glm::vec3(0.0f), float max_distance = -1.0f, int max_hits = -1, bool ignore_self = true,
59 const std::vector<int>& layers = std::vector<int> {}
60 ) const -> std::vector<physics::RayHit>;
61
68 [[Reflect]]
69 auto linecast(
70 glm::vec3 from, glm::vec3 to, int max_hits = -1, bool ignore_self = true,
71 const std::vector<int>& layers = std::vector<int> {}
72 ) const -> std::vector<physics::RayHit>;
73
79 void lookAt(glm::vec3 target, glm::vec3 up = {0.0f, 0.0f, 1.0f});
80
85 void lookAtZ(glm::vec3 target);
86
90 [[nodiscard]]
91 auto up() const -> glm::vec3;
92
96 [[nodiscard]]
97 auto forward() const -> glm::vec3;
98
99 void syncTransform() const;
100
101 void syncWorldTransform() const;
102
103 void refreshTransformParent();
104
105 [[nodiscard]]
106 auto getTransform() const noexcept -> const glm::mat4&;
107
108 [[nodiscard]]
109 auto getWorldTransform() const noexcept -> const glm::mat4&;
110
111 virtual void onEditorTransformChanged() { }
112
113 static inline const glm::vec3 world_up = {0.0f, 0.0f, 1.0f};
114 static inline const glm::vec3 world_forward = {0.0f, 1.0f, 0.0f};
115 static inline const glm::vec3 world_right = {1.0f, 0.0f, 0.0f};
116
117protected:
118 void init();
119
120private:
121 mutable bool m_dirty_world = true;
122 Box<Node3D> m_transform_parent;
123
124 alignas(16) mutable glm::vec3 m_previous_position = glm::vec3(0.0f);
125 alignas(16) mutable glm::quat m_previous_rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
126 alignas(16) mutable glm::vec3 m_previous_scale = glm::vec3(1.0f);
127 alignas(16) mutable glm::vec3 m_previous_world_position = glm::vec3(0.0f);
128 alignas(16) mutable glm::quat m_previous_world_rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
129 alignas(16) mutable glm::vec3 m_previous_world_scale = glm::vec3(1.0f);
130
131 mutable glm::mat4 m_transform = glm::mat4(1.0f);
132 mutable glm::mat4 m_world_transform = glm::mat4(1.0f);
133};
134
135}
pseudo shared pointer for Nodes
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
auto raycast(glm::vec3 dir, glm::vec3 offset=glm::vec3(0.0f), float max_distance=-1.0f, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::optional< physics::RayHit >
The nearest thing a ray from this node hits.
Definition node_3d.cpp:29
static const glm::vec3 world_right
Y axis.
Definition node_3d.hpp:115
auto forward() const -> glm::vec3
Definition node_3d.cpp:120
static const glm::vec3 world_forward
Z axis.
Definition node_3d.hpp:114
void lookAtZ(glm::vec3 target)
lookAt() constrained to the XY plane; the node faces the target with no roll
Definition node_3d.cpp:89
auto up() const -> glm::vec3
Definition node_3d.cpp:116
auto raymulticast(glm::vec3 dir, glm::vec3 offset=glm::vec3(0.0f), float max_distance=-1.0f, int max_hits=-1, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::vector< physics::RayHit >
Up to max_hits things collisions.
Definition node_3d.cpp:40
void lookAt(glm::vec3 target, glm::vec3 up={0.0f, 0.0f, 1.0f})
Orients the node so its forward axis points at a world-space target.
Definition node_3d.cpp:60
auto linecast(glm::vec3 from, glm::vec3 to, int max_hits=-1, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::vector< physics::RayHit >
Everything on the line between two points, nearest to from first.
Definition node_3d.cpp:53
Definition reflect.hpp:244
Base object type for the scene graph.
Raycast Implmentation mostly from https://iquilezles.org/articles/intersectors/ and https://www....