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

Implement Vector4, Vector4i, Projection built-in types. #63219

Merged
merged 1 commit into from
Jul 25, 2022

Conversation

reduz
Copy link
Member

@reduz reduz commented Jul 19, 2022

Implement built-in classes Vector4, Vector4i and Projection.

  • Two versions of Vector4 (float and integer).
  • A Projection class, which is a 4x4 matrix specialized in projection types.

These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.

TODO:

  • Shaders should be changed to use these for exposed uniforms. Can be done in a subsequent PR.
  • Enums do not seem to be registered properly, need to check what is going on.

FAQ:

Q: Why Projection and not Matrix4?
A: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a purpose. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is always used as a Projection, hence the naming.

Bugsquad edit: This closes godotengine/godot-proposals#629.

Bugsquad edit: Supersedes #59970.

@reduz reduz requested review from a team as code owners July 19, 2022 23:17
@Calinou Calinou added this to the 4.0 milestone Jul 19, 2022
@Calinou
Copy link
Member

Calinou commented Jul 19, 2022

Shaders should be changed to use these for exposed uniforms. Can be done in a subsequent PR.

This may be done in #59970 already, so it's worth checking that PR's code just in case.

core/math/vector4.h Outdated Show resolved Hide resolved
@@ -91,11 +94,14 @@ class Variant {
VECTOR3,
VECTOR3I,
TRANSFORM2D,
VECTOR4,
VECTOR4I,
Copy link
Member

@aaronfranke aaronfranke Jul 19, 2022

Choose a reason for hiding this comment

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

I know it's arbitrary, but this order doesn't really make sense. Why should Transform2D go in-between Vector3 and Vector4? I think we should consider reordering the types in the enum, maybe logical grouping, maybe alphabetical.

For logical grouping it would make sense to have Vector2/2i/3/3i/4/4i first, then Plane/Quat(/Color?), then Rect2/2i/AABB, then Transform2D, Basis, Transform3D, Projection.

EDIT: Updated my suggestion, now all types with 4 elements are together.

Copy link
Member Author

Choose a reason for hiding this comment

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

Its together with the other 4 component types so to me it makes sense that way. Moving then is far too much work also, so won't do without a very good reason.

Copy link
Member Author

Choose a reason for hiding this comment

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

Upon further inspection, I think the problem is that TRANSFORM2D should go above VECTOR3. We could change this in a separate PR I guess.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also keep in mind, while I guess this isn't a problem since we're still in Alpha and we accept breakage, that adding new types in the middle will segfault any existing GDExtension that assumes the old numbering.

@clayjohn
Copy link
Member

For posterity can you comment briefly on the difference between this PR and #59970?

@reduz
Copy link
Member Author

reduz commented Jul 20, 2022

@clayjohn That one is only Vector4 and from my understanding it misses a few bits of the type registration in the core areas so it was not entirely correct. Because all the 3 types I added interact with each other, it was far easier to me to implement everything together.

That said, that PR still implements a few more things like shader interaction, which I did not. I need to add that.

@reduz reduz force-pushed the implement-vector4-projection branch from 596b048 to 900503c Compare July 20, 2022 10:23
@reduz reduz requested review from a team as code owners July 20, 2022 10:23
@reduz reduz force-pushed the implement-vector4-projection branch from 900503c to 1dba423 Compare July 20, 2022 12:15
@reduz reduz requested a review from a team as a code owner July 20, 2022 12:15
@reduz reduz force-pushed the implement-vector4-projection branch 4 times, most recently from ef913f3 to 49ccf4b Compare July 20, 2022 17:57
@nathanfranke
Copy link
Contributor

Should PackedVector4Array also be added? I don't see that added in variant.h. It would be helpful for shader uniform vec4 arrays.

bruvzg
bruvzg previously requested changes Jul 20, 2022
Copy link
Member

@bruvzg bruvzg left a comment

Choose a reason for hiding this comment

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

A few mismatched argument names in the bindings.

core/variant/variant_call.cpp Outdated Show resolved Hide resolved
core/variant/variant_call.cpp Outdated Show resolved Hide resolved
@bruvzg
Copy link
Member

bruvzg commented Jul 20, 2022

Follow-up changes in the godot-cpp - godotengine/godot-cpp#793

@BastiaanOlij
Copy link
Contributor

Love this @reduz , haven't got much to add that hasn't already been said. Agree with the names change to Projection, makes far more sense.

One thing I think we should discuss as a possibility is merging Color and Vector4, adding r,g,b,a as a union, and then having Color be a typedef of Vector4 just like Size2 is a typedef of Vector2.

@aaronfranke
Copy link
Member

aaronfranke commented Jul 21, 2022

@BastiaanOlij Color always stores 32-bit floats, while Vector4 uses real_t, so it wouldn't be appropriate. It would make more sense to do this with Plane and Quaternion and Rect2, but I'm not in favor of doing this to any of them.

EDIT: Also, Color has things like automatic color space conversions, which can be confusing to work with.

Copy link
Contributor

@BastiaanOlij BastiaanOlij left a comment

Choose a reason for hiding this comment

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

Love this change

@BastiaanOlij
Copy link
Contributor

@BastiaanOlij Color always stores 32-bit floats, while Vector4 uses real_t, so it wouldn't be appropriate. It would make more sense to do this with Plane and Quaternion and Rect2, but I'm not in favor of doing this to any of them.

I guess it would indeed be overkill to change Color to real_t. My motivation here is because in shaders Color is a vec4 and interchangeable and there are a lot of functions that overlap.

From a usage point of view Color and Vector4 are different enough that on the game engine side it is definitely a defensible position that they should be separate classes if only to prevent confusion from users.

@reduz reduz force-pushed the implement-vector4-projection branch 5 times, most recently from cbf7273 to 440a66f Compare July 23, 2022 11:07
Implement built-in classes Vector4, Vector4i and Projection.

* Two versions of Vector4 (float and integer).
* A Projection class, which is a 4x4 matrix specialized in projection types.

These types have been requested for a long time, but given they were very corner case they were not added before.
Because in Godot 4, reimplementing parts of the rendering engine is now possible, access to these types (heavily used by the rendering code) becomes a necessity.

**Q**: Why Projection and not Matrix4?
**A**: Godot does not use Matrix2, Matrix3, Matrix4x3, etc. naming convention because, within the engine, these types always have a *purpose*. As such, Godot names them: Transform2D, Transform3D or Basis. In this case, this 4x4 matrix is _always_ used as a _Projection_, hence the naming.
@reduz reduz force-pushed the implement-vector4-projection branch from 440a66f to 455c06e Compare July 23, 2022 12:00
@@ -91,11 +94,14 @@ class Variant {
VECTOR3,
VECTOR3I,
TRANSFORM2D,
VECTOR4,
Copy link
Contributor

Choose a reason for hiding this comment

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

PackedVector4Array?

Copy link
Member Author

Choose a reason for hiding this comment

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

you can still use a regular array for this use case or PackedColorArray, so its not suuuuper important.

@akien-mga akien-mga requested a review from bruvzg July 25, 2022 09:03
@akien-mga akien-mga merged commit 3084a48 into godotengine:master Jul 25, 2022
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Vector4 to the engine
8 participants