@@ -15,6 +15,7 @@ use crate::builtin::{real, Plane, RMat4, RealConv, Transform3D, Vector2, Vector4
15
15
use std:: ops:: Mul ;
16
16
17
17
use super :: meta:: impl_godot_as_self;
18
+ use super :: { Aabb , Rect2 , Vector3 } ;
18
19
19
20
/// A 4x4 matrix used for 3D projective transformations. It can represent
20
21
/// transformations such as translation, rotation, scaling, shearing, and
@@ -85,6 +86,24 @@ impl Projection {
85
86
)
86
87
}
87
88
89
+ /// Creates a new Projection that scales a given projection to fit around
90
+ /// a given AABB in projection space.
91
+ ///
92
+ /// _Godot equivalent: Projection.create_fit_aabb()_
93
+ pub fn create_fit_aabb ( aabb : Aabb ) -> Self {
94
+ let translate_unscaled = -2.0 * aabb. position - aabb. size ; // -(start+end)
95
+
96
+ let scale = Vector3 :: splat ( 2.0 ) / aabb. size ;
97
+ let translate = translate_unscaled / aabb. size ;
98
+
99
+ Self :: from_cols (
100
+ Vector4 :: new ( scale. x , 0.0 , 0.0 , 0.0 ) ,
101
+ Vector4 :: new ( 0.0 , scale. y , 0.0 , 0.0 ) ,
102
+ Vector4 :: new ( 0.0 , 0.0 , scale. z , 0.0 ) ,
103
+ Vector4 :: new ( translate. x , translate. y , translate. z , 1.0 ) ,
104
+ )
105
+ }
106
+
88
107
/// Creates a new Projection for projecting positions onto a head-mounted
89
108
/// display with the given X:Y aspect ratio, distance between eyes, display
90
109
/// width, distance to lens, oversampling factor, and depth clipping planes.
@@ -183,6 +202,18 @@ impl Projection {
183
202
)
184
203
}
185
204
205
+ /// Creates a new Projection that projects positions into the given Rect2.
206
+ ///
207
+ /// _Godot equivalent: Projection.create_light_atlas_rect()_
208
+ pub fn create_light_atlas_rect ( rect : Rect2 ) -> Self {
209
+ Self :: from_cols (
210
+ Vector4 :: new ( rect. size . x , 0.0 , 0.0 , 0.0 ) ,
211
+ Vector4 :: new ( 0.0 , rect. size . y , 0.0 , 0.0 ) ,
212
+ Vector4 :: new ( 0.0 , 0.0 , 1.0 , 0.0 ) ,
213
+ Vector4 :: new ( rect. position . x , rect. position . y , 0.0 , 1.0 ) ,
214
+ )
215
+ }
216
+
186
217
/// Creates a new Projection that projects positions using an orthogonal
187
218
/// projection with the given clipping planes.
188
219
///
@@ -337,8 +368,13 @@ impl Projection {
337
368
/// has the given horizontal field of view (in degrees) and aspect ratio.
338
369
///
339
370
/// _Godot equivalent: Projection.get_fovy()_
340
- pub fn fovy_of ( fov_x : real , aspect : real ) -> real {
341
- real:: from_f64 ( InnerProjection :: get_fovy ( fov_x. as_f64 ( ) , aspect. as_f64 ( ) ) )
371
+ #[ doc( alias = "get_fovy" ) ]
372
+ pub fn create_fovy ( fov_x : real , aspect : real ) -> real {
373
+ let half_angle_fov_x = f64:: to_radians ( fov_x. as_f64 ( ) * 0.5 ) ;
374
+ let vertical_transform = f64:: atan ( aspect. as_f64 ( ) * f64:: tan ( half_angle_fov_x) ) ;
375
+ let full_angle_fov_y = f64:: to_degrees ( vertical_transform * 2.0 ) ;
376
+
377
+ real:: from_f64 ( full_angle_fov_y)
342
378
}
343
379
344
380
/// Returns the factor by which the visible level of detail is scaled by
0 commit comments