khora_data/ecs/components/global_transform.rs
1// Copyright 2025 eraflo
2//
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
15use crate::ecs::component::Component;
16use khora_core::math::Mat4;
17
18/// A component that stores the final, calculated, world-space transformation of an entity.
19///
20/// This component's value is the result of combining the entity's local `Transform`
21/// with the `GlobalTransform` of its `Parent`, recursively up to the root of the scene.
22///
23/// It is intended to be **read-only** for most systems (like rendering and physics).
24/// It should only be written to by the dedicated transform propagation system.
25/// This acts as a cache to avoid re-calculating the full transform hierarchy every time
26/// it's needed.
27#[derive(Debug, Clone, Copy, PartialEq)]
28pub struct GlobalTransform(pub Mat4);
29
30impl Component for GlobalTransform {}
31
32impl GlobalTransform {
33 /// Creates a new `GlobalTransform` from a `Mat4`.
34 pub fn new(matrix: Mat4) -> Self {
35 Self(matrix)
36 }
37
38 /// Creates a new identity `GlobalTransform`.
39 pub fn identity() -> Self {
40 Self(Mat4::IDENTITY)
41 }
42}
43
44impl Default for GlobalTransform {
45 /// Returns the identity `GlobalTransform`.
46 fn default() -> Self {
47 Self::identity()
48 }
49}