Skip to content

Conversation

WhalesState
Copy link
Contributor

@WhalesState WhalesState commented Aug 17, 2025

Refactors vector initializations by replacing repeated scalar arguments and redundant operations with from_scalar, making the code shorter, easier to read, and slightly more efficient.

A shorter, more concise name would be appreciated, such as fill, splat, or uniform.

Use cases:

  • Temporary variable elimination:
    const real_t radius2 = Math::sqrt(double(new_simplex->circum_r2)) + 0.0001;
    Vector3 extents = Vector3(radius2, radius2, radius2);
    Changed to:
    Vector3 extents = Vector3::from_scalar(Math::sqrt(double(new_simplex->circum_r2)) + 0.0001);
  • Line shortening:
    Size2i(options6.ShadingRateImageTileSize, options6.ShadingRateImageTileSize)
    Changed to:
    Size2i::from_scalar(options6.ShadingRateImageTileSize)
  • Redundant operation elimination:
    Vector2i(border / 2, border / 2)
    Size2(400, 400) * EDSCALE
    Changed to:
    Vector2i::from_scalar(border / 2)
    Size2::from_scalar(400 * EDSCALE)
  • Dummy vector scaling elimination:
    Vector3(1, 1, 1) * cascade.cell_size * probe_cells;
    Changed to:
    Vector3::from_scalar(cascade.cell_size * probe_cells);

@WhalesState WhalesState requested review from a team as code owners August 17, 2025 12:02
@KoBeWi KoBeWi added this to the 4.x milestone Aug 17, 2025
@fire
Copy link
Member

fire commented Aug 17, 2025

Why is this needed for your project? Historically, we've been explicit on casting vectors.

Can you write a proposal for this enhancement?

@WhalesState

This comment was marked as outdated.

@WhalesState
Copy link
Contributor Author

@WhalesState WhalesState marked this pull request as draft August 17, 2025 16:18
@WhalesState
Copy link
Contributor Author

Why is this needed for your project? Historically, we've been explicit on casting vectors.

Can you write a proposal for this enhancement?

It's nearly the same as using the constructors, no need to write a proposal unless it proves it's useful.

@tool
extends Node2D


func _ready() -> void:
	var time: int = Time.get_ticks_msec()
	for i in 10000000:
		var v1 = Vector2(78 * 2.558, 78 * 2.558)
		var v2 = Vector2(1, 1) * 255.867 * sqrt(2)
		var f = sqrt(4) * 45.5
		var v3 = Vector2(f, f)
	print("Time Before: ", Time.get_ticks_msec() - time)
	time = Time.get_ticks_msec()
	for i in 10000000:
		var v1 = Vector2.from_scalar(78 * 2.558)
		var v2 = Vector2.from_scalar(255.867 * sqrt(2))
		var v3 = Vector2.from_scalar(sqrt(4) * 45.5)
	print("Time After: ", Time.get_ticks_msec() - time)

Time Before: 6868
Time After: 6767

@AThousandShips
Copy link
Member

Why not for Vector4(i)

@WhalesState
Copy link
Contributor Author

Why not for Vector4(i)

I didn’t add it because I couldn’t find any use cases for it in the Godot source at the time. Of course, if you think it would be useful, it can definitely be added, I just wanted to avoid introducing extra API that might not get used.

@AThousandShips
Copy link
Member

I think it would be the most useful there and it'd be very confusing to have it in two but not the third

@WhalesState
Copy link
Contributor Author

@KoBeWi KoBeWi removed this from the 4.x milestone Aug 19, 2025
@WhalesState
Copy link
Contributor Author

Can be optionally implemented later after #110743.

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.

5 participants