-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add Vector4 to the engine #629
Comments
You mentioned shaders, the shading language has vec4. Although it could be useful in GDScript as well. |
@AndreaCatania Bullet has |
In bullet I didn't used it, but I used it for the Flex Engine implementation |
I don't think this deserves to be in the core. The need for them is very limited, as even in the core we don't need them (we have the quaternion class for case 3) |
I don't really see why this isn't core. Just for linking with the shader language vec4s if nothing else. Unless Quats already do this, and can be passed to shaders, etc... as arbitrary 4-tuples. |
That would be really useful for many cases. It won't hurt anyone or anything to have this implemented. |
Reduz stated that submissions will be |
While the debate for the usefulness of Vector4 continues, if anyone is interested in having Vector4 in their own projects, I created a C# math library which includes Vector4 among other things. You still won't be able to use it as a parameter for Godot's built-in methods, at least not directly, but it can be used for manual math or storing the values for later. |
Currently Godot Engine uses plane or color for vec4. |
When it comes to shoehorning existing types for usage as a
A I would suggest using |
I do not remember this being the case, especially since issues like godotengine/godot#23711 are reproduced when using rect2 with negative size. |
As someone who's not a good programmer, I don't understand what's expected of me when I'm trying to get a Vector4 into a shader. If I'm supposed to use Plane, or Color, of Quat, or whatever, that's really something that needs to be documented. How do I know nothing's going to break when I'm passing one of these between the CPU and GPU using a value outside their normal range - say, due to normalization, or filtering? And how easy and intuitive can you explain to a shader newbie how and why these other types are to be used as a Vector4? Likewise, when someone goes looking for this information, finding it should be straightforward. I'm not sure it makes life easier for those looking for standard functionality like this to be expected to look for something other than "Vector4" - seems like they're going to end up having to ask someone for help figuring out the expected workaround. On the other hand, in shaders themselves, vectors and colors are the same datatype, which makes a lot of sense. If Color had the same functionality as a Vec4 (maybe with x, y, z, and w property aliases) and you explained up front it was for the sake of interoperability between the CPU and GPU, I think you could justify that to anyone who's learning about shaders anyway. Of course, some reassurance in the documentation that Color and Vec4 really will have the same value after being passed and operate the same way on either side would be helpful. There's a lot that's not obvious about shader programming and no one want's to "experiment" with what works right now and end up with inscrutable bugs later. |
Hardware support - SIMD, alignment, cache linesJust to mention one of the reasons that Nearly all modern CPUs and GPUs have SIMD support for 4 floats (in the form of e.g. This therefore makes it a special case - it is a hardware supported type. One argument against a Generic SIMD sized structureThere are thus a large number of data structures in programming that are fitted into these 4 floats, rather than the more generalized case of non-aligned structs. Indeed these are so efficient that you will often find that smaller structs like This has led to the current situation where users (and contributors) have had to shoehorn other types into e.g.
This does create an argument for a generic type that can either be used directly or to transfer data to / from faster native forms (be these shaders, packed SIMD capable arrays etc). Integer operationsOne thing I would consider, if we were to make a generic type, would be to ask whether we can make it truly generic, as in the ability to address it either as floats or This could potentially future proof things and maybe we would only need 1 struct, instead of having to create umpteen variant versions, and conversions between them etc. (Data is data, a CPU essentially doesn't care whether you treat 32 bits as a float or an int). I thought this was worth mentioning, as this is how SIMD code works, but it may be deemed too complex for users to understand in terms of any bound functions. And there is a slight complication here is the compiling the engine C++ or Variant or bothI will say that I'm primarily interested in the c++ side of this myself in terms of engine code, modules and extensions, but there is also a decent argument for binding this, whether or not part of Variant I can't say. |
I would argue against adding another type for a few reasons. However, Furthermore, Therefore, rather than add yet another type, I would propose that either the status quo be kept, or t might also be nice to add a As for native formats, we already have PackedByteArray and various packed types which can be made from this. But I agree, it's not all-encompassing and both misses many formats and requires copying between types. I really appreciate Javascript's TypedArray system, where there is an I tried writing conversion code in GDScript for various vertex buffer formats here: https://github.com/V-Sekai/Unidot-Importer/blob/main/aligned_byte_buffer.gd supporting various strides and types (i needed to pass these to mesh APIs, so I wanted to minimize overhead in GDScript). Some of the conversions were clean but others were extremely messy. I feel like the discussion about a generic type and/or SIMD / integer operations is interesting but might belong in its own proposal. I'm kind of scared about making things too generic just to solve the Vector4 problem because that creates complexity everywhere in the type system. Edit: I'm not opposed to making |
Plane -> Vector4 isn't a bad idea, and ditto for PackedVector4Array |
It is a bad idea, because there's no reason for Vector4 to have methods specific to Plane (like I really can't grasp what is the real problem of having such basic data type in 3D game engine core. |
@Listwon: You have a point. Maybe have the Plane inherit from V4? |
@Zireael07 That wouldn't make much sense. There are also methods that would be on Vector4 that don't make sense on Plane, such as length and distance methods, actually I think most methods wouldn't make sense. |
|
As mentioned in godotengine/godot#59125 (comment), |
This has been closed, even though Vector4 and Vector4i have not been implemented in the mono classes. Isn't making the mono classes part of "implementing" the new feature? If so, this is incomplete. |
@philomelus This is already being worked on. These new types not existing in the Mono module prevents compiling the Mono module, so it's not as if this could be forgotten. |
Describe the project you are working on:
Many projects can benefit from this feature, this is mostly for discussion.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
A Vector4 class would be similar to Vector2 and Vector3, but with four components: X, Y, Z, and W.
Potential use cases for a Vector4 class that I can think of:
Parameters for noise functions, simplex etc: Simplex Noise functions (2D, 3D, and 4D) godot#1268 and Add SimplexNoise and NoiseTexture as new resources godot#21569
For use with shaders: Need more vertex attributes (for terrain shading) godot#9134, Vector4 is not highlighted properly in code editor godot#2855 (comment), Using set_shader_param with a color returns non-linear colors godot#28504, Allow passing Matrix and Vector4 to shaders #258
Representing an Axis-Angle 3D rotation (not the same as Quaternions, simpler, less capable, Godot's rotate function already does this with a Vector3 and a float)
Represeting 3D vectors in Direction-Magnitude format (direction is normalized).
Simply for allowing the user to store 4 values together with a built-in struct.
There's likely many more uses for Vector4 than I'm thinking of. I'm not desperate for it, but I would find a Vector4 class useful. What matters is what Godot users would need it to do. It would be engine bloat implementing Vector4 "just because". So I'm opening this discussion to ask around about if users would find Vector4 useful and what kind of things they would use it for.
If you can think of a use case for Vector4 that you'd use, feel free to post it here.
If this enhancement will not be used often, can it be worked around with a few lines of script?:
Yes-ish. I have a C# implementation here. It's not really feasible to make within GDScript, but the real solution there would be to allow custom structs in GDScript: #279
Is there a reason why this should be core and not an add-on in the asset library?:
Having Vector4 in core would allow engine functions to use it as a built-in type whenever it's necessary to work with four numbers. An analysis of how many engine functions could benefit from this would be a good idea first. Again, this proposal is mostly for discussion.
It might indeed be a better idea for this to be an add-on, but this isn't possible in GDScript.
The text was updated successfully, but these errors were encountered: