Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of additional Projection methods #529

Merged
merged 1 commit into from
Dec 10, 2023

Conversation

Lemiczek
Copy link
Contributor

@Lemiczek Lemiczek commented Dec 6, 2023

This commit implements create_fit_aabb(), create_light_atlas_rect() and re-implements a native fovy() as a replacement for fovy_of wrapper that called godot directly.

This concerns the tracking issue #310, as Projection is tagged there.

I've looked at the math behind the related methods straight from the source here: https://github.com/godotengine/godot/blob/4.2/core/math/projection.cpp, re-implemented them, and added tests comparing with the direct calls to Godot.

I'm unsure if we are allowed to borrow implementations from Godot itself, if not, feel free to close this PR. 😅

@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-529

@lilizoey lilizoey added feature Adds functionality to the library c: core Core components labels Dec 7, 2023
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Great that you added tests 🙂

@Lemiczek Lemiczek force-pushed the projection_addition_aabb_fovy branch from 6fc203f to 9743e05 Compare December 7, 2023 13:14
Comment on lines 93 to 98
pub fn create_fit_aabb(aabb: Aabb) -> Self {
let min = aabb.position;
let max = aabb.position + aabb.size;

let scale = Vector3::splat(2.0) / (max - min);
let translate = -(max + min) / (max - min);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(max - min) is just aabb.size -- no need to recalculate it, let alone twice.

Generally not sure we gain much from min/max variables. The only remaining expression is then -(max + min), which is the same as (-2.0 * aabb.position - aabb.size).

Maybe a small comment is enough.

Suggested change
pub fn create_fit_aabb(aabb: Aabb) -> Self {
let min = aabb.position;
let max = aabb.position + aabb.size;
let scale = Vector3::splat(2.0) / (max - min);
let translate = -(max + min) / (max - min);
pub fn create_fit_aabb(aabb: Aabb) -> Self {
let translate_unscaled = -2.0 * aabb.position - aabb.size; // -(start+end)
let scale = Vector3::splat(2.0) / aabb.size;
let translate = translate_unscaled / aabb.size;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Makes more sense instead of min/max. 👍

Comment on lines 369 to 377
/// has the given horizontal field of view (in degrees) and aspect ratio.
///
/// _Godot equivalent: Projection.get_fovy()_
pub fn fovy_of(fov_x: real, aspect: real) -> real {
real::from_f64(InnerProjection::get_fovy(fov_x.as_f64(), aspect.as_f64()))
pub fn fovy(fov_x: real, aspect: real) -> real {
let half_angle_fov_x = f64::to_radians(fov_x.as_f64() * 0.5);
let vertical_transform = f64::atan(aspect.as_f64() * f64::tan(half_angle_fov_x));
let full_angle_fov_y = f64::to_degrees(vertical_transform * 2.0);

real::from_f64(full_angle_fov_y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We named the method fovy_of in the initial implementation, see #124 (comment).

But I think create_fovy might actually be a better name, in line with the other constructors. Godot's get_fovy for a static method is terrible naming.

Can you name it create_fovy and add a #[doc(alias)] with the Godot name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added #[doc(alias = "get_fovy")], and changed the function name to create_fovy :)

This commit implements `create_fit_aabb()`, `create_light_atlas_rect()` and implements a native `fovy()`
as a replacement for `fovy_of` wrapper that called godot directly.
@Lemiczek Lemiczek force-pushed the projection_addition_aabb_fovy branch from 9743e05 to 1b45761 Compare December 9, 2023 20:05
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@Bromeon Bromeon added this pull request to the merge queue Dec 10, 2023
Merged via the queue into godot-rust:master with commit 0eb3f7a Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants