godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.07k stars 68 forks source link

Support GLSL style initialization and functions for vectors #4822

Open QbieShay opened 1 year ago

QbieShay commented 1 year ago

Describe the project you are working on

Various VFX

Describe the problem or limitation you are having in your project

I work a lot with text shaders and sometimes with GDScript tool. It makes me significantly slower to have to switch paradigm for interacting with math types when coding for gdscript.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Support additional initializers and more override for math functions so that one can use vector types in GDScript like they'd do in GLSL.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

In order of importance, for me:

  1. Support GLSL style initializers (Vector2(float), Vector3(float), Color(float))
  2. Math functions that support vector types (floor(Vector2), floor(Vector3), floor(Color) Less important:
  3. Add a Vector4 type that aliases to color and can use xyz (Production edit: Done in https://github.com/godotengine/godot/pull/63219)
  4. Add swizzling to Vectors (marginal, probably not worth it)

Below a comparison of supporting 2 vs not supporting it. Screenshot from 2022-07-06 11-56-52

If this enhancement will not be used often, can it be worked around with a few lines of script?

It's a modification to GDScript

Is there a reason why this should be core and not an add-on in the asset library?

It's a modification to GDScript

AThousandShips commented 1 year ago

You already have the floor method for vectors on the vectors

QbieShay commented 1 year ago

Oh. Thanks for pointing it out. I didn't think of that.

I think the proposal still stands though.

AThousandShips commented 1 year ago

No problem! But I absolutely agree that Vector2/3(x) should be added, it's very helpful and reduces risk of errors

I'd even suggest Vector3(Vector2, float) and Vector3(float, Vector2)

cdemirer commented 1 year ago

Also see: #3884 (which is similar but has a more restricted scope)

vpellen commented 1 year ago

Looks like godotengine/godot/pull/59970 is working on Vector4. I'd second the request for swizzling, not just as a getter, but also as a setter - the example I often want is something like velocity.xz = input.xy

dreadpon commented 9 months ago

I very much support this proposal Main issue for me is the lack of constructors that take single ints/floats as well as "previous" kind of vector

Right now I have to do

var axis_val = 1.5
var vector = Vector3(axis_val , axis_val , axis_val)

Event though var vector = Vector3.ONE * axis_val is an option, it both looks ugly and performs an operation on a vector instead of just initializing it.

I think it will be beneficial to have convenience constructors or factory methods to create Vector2/3 from a single float/int and Vector3/4 from Vector2/3 (setting the last member to zero).

So it could take a form of

var axis_val = 1.5
var vector2 = Vector2(axis_val) # same as Vector2(axis_val, axis_val)
var vector3 = Vector3(vector2) # same as Vector3(vector2.x, vector2.y, 0.0)

Or

var axis_val = 1.5
var vector2 = Vector2.from_float(axis_val) # same as Vector2(axis_val, axis_val)
var vector3 = Vector3.from_vector2(vector2) # same as Vector3(vector2.x, vector2.y, 0.0)

Depending on the philosophy of why it wasn't already there (Unity doesn't seem to have one, but Unreal does). I certainly would prefer a proper constructor if engine supports that.

Of course it would be great if it also performed implicit casts as proposed in https://github.com/godotengine/godot-proposals/issues/5545

Ariorick commented 1 week ago

I support it with my full heart! It would be so much easier to work with This one should not be that hard to make, is there some ideological issue?

Calinou commented 1 week ago

This one should not be that hard to make, is there some ideological issue?

The main difficulty is implementing this without defining a ton of additional constructors, particularly in Vector4 and Vector4i which have four components (and therefore a lot of potential combinations). See https://github.com/godotengine/godot/pull/93012 where this has shown to be an issue.