Skip to content

Commit

Permalink
Construct Vector2 & Vector2i from int or float.
Browse files Browse the repository at this point in the history
  • Loading branch information
WhalesState committed Nov 25, 2024
1 parent 54849aa commit 8eef1fc
Show file tree
Hide file tree
Showing 301 changed files with 1,583 additions and 1,521 deletions.
2 changes: 1 addition & 1 deletion core/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Vector2 Input::get_joy_vibration_strength(int p_device) {
if (joy_vibration.has(p_device)) {
return Vector2(joy_vibration[p_device].weak_magnitude, joy_vibration[p_device].strong_magnitude);
} else {
return Vector2(0, 0);
return Vector2();
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/io/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2854,7 +2854,7 @@ Rect2i Image::get_used_rect() const {

Ref<Image> Image::get_region(const Rect2i &p_region) const {
Ref<Image> img = memnew(Image(p_region.size.x, p_region.size.y, mipmaps, format));
img->blit_rect(Ref<Image>((Image *)this), p_region, Point2i(0, 0));
img->blit_rect(Ref<Image>((Image *)this), p_region, Point2i());
return img;
}

Expand Down
2 changes: 1 addition & 1 deletion core/math/a_star_grid_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AStarGrid2D : public RefCounted {
private:
Rect2i region;
Vector2 offset;
Size2 cell_size = Size2(1, 1);
Size2 cell_size = Size2(1);
bool dirty = false;
CellShape cell_shape = CELL_SHAPE_SQUARE;

Expand Down
2 changes: 1 addition & 1 deletion core/math/geometry_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ class Geometry3D {

_FORCE_INLINE_ static Vector3 octahedron_map_decode(const Vector2 &p_uv) {
// https://twitter.com/Stubbesaurus/status/937994790553227264
const Vector2 f = p_uv * 2.0f - Vector2(1.0f, 1.0f);
const Vector2 f = p_uv * 2.0f - Vector2(1);
Vector3 n = Vector3(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
const real_t t = CLAMP(-n.z, 0.0f, 1.0f);
n.x += n.x >= 0 ? -t : t;
Expand Down
4 changes: 4 additions & 0 deletions core/math/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ struct [[nodiscard]] Vector2 {
x = p_x;
y = p_y;
}
_FORCE_INLINE_ Vector2(real_t p_v) {
x = p_v;
y = p_v;
}
};

_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
Expand Down
4 changes: 4 additions & 0 deletions core/math/vector2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ struct [[nodiscard]] Vector2i {
x = p_x;
y = p_y;
}
inline Vector2i(int32_t p_v) {
x = p_v;
y = p_v;
}
};

// Multiplication operators required to workaround issues with LLVM using implicit conversion.
Expand Down
20 changes: 18 additions & 2 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
} break;
case VECTOR2: {
static const Type valid[] = {
INT,
FLOAT,
VECTOR2I,
NIL,
};
Expand All @@ -243,6 +245,8 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
} break;
case VECTOR2I: {
static const Type valid[] = {
INT,
FLOAT,
VECTOR2,
NIL,
};
Expand Down Expand Up @@ -577,6 +581,8 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type
} break;
case VECTOR2: {
static const Type valid[] = {
INT,
FLOAT,
VECTOR2I,
NIL,
};
Expand All @@ -586,6 +592,8 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type
} break;
case VECTOR2I: {
static const Type valid[] = {
INT,
FLOAT,
VECTOR2,
NIL,
};
Expand Down Expand Up @@ -1852,7 +1860,11 @@ String Variant::to_json_string() const {
}

Variant::operator Vector2() const {
if (type == VECTOR2) {
if (type == INT) {
return Vector2(_data._int, _data._int);
} else if (type == FLOAT) {
return Vector2(_data._float, _data._float);
} else if (type == VECTOR2) {
return *reinterpret_cast<const Vector2 *>(_data._mem);
} else if (type == VECTOR2I) {
return *reinterpret_cast<const Vector2i *>(_data._mem);
Expand All @@ -1870,7 +1882,11 @@ Variant::operator Vector2() const {
}

Variant::operator Vector2i() const {
if (type == VECTOR2I) {
if (type == INT) {
return Vector2(_data._int, _data._int);
} else if (type == FLOAT) {
return Vector2(_data._float, _data._float);
} else if (type == VECTOR2I) {
return *reinterpret_cast<const Vector2i *>(_data._mem);
} else if (type == VECTOR2) {
return *reinterpret_cast<const Vector2 *>(_data._mem);
Expand Down
2 changes: 2 additions & 0 deletions core/variant/variant_construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,13 @@ void Variant::_register_variant_constructors() {
add_constructor<VariantConstructNoArgs<Vector2>>(sarray());
add_constructor<VariantConstructor<Vector2, Vector2>>(sarray("from"));
add_constructor<VariantConstructor<Vector2, Vector2i>>(sarray("from"));
add_constructor<VariantConstructor<Vector2, double>>(sarray("from"));
add_constructor<VariantConstructor<Vector2, double, double>>(sarray("x", "y"));

add_constructor<VariantConstructNoArgs<Vector2i>>(sarray());
add_constructor<VariantConstructor<Vector2i, Vector2i>>(sarray("from"));
add_constructor<VariantConstructor<Vector2i, Vector2>>(sarray("from"));
add_constructor<VariantConstructor<Vector2i, int64_t>>(sarray("from"));
add_constructor<VariantConstructor<Vector2i, int64_t, int64_t>>(sarray("x", "y"));

add_constructor<VariantConstructNoArgs<Rect2>>(sarray());
Expand Down
24 changes: 16 additions & 8 deletions core/variant/variant_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,25 +710,33 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream,
return err;
}

if (args.size() != 2) {
r_err_str = "Expected 2 arguments for constructor";
if (args.size() == 0) {
value = Vector2();
} else if (args.size() == 1) {
value = Vector2(args[0], args[0]);
} else if (args.size() == 2) {
value = Vector2(args[0], args[1]);
} else {
r_err_str = "Expected at most 2 arguments for constructor";
return ERR_PARSE_ERROR;
}

value = Vector2(args[0], args[1]);
} else if (id == "Vector2i") {
Vector<int32_t> args;
Error err = _parse_construct<int32_t>(p_stream, args, line, r_err_str);
if (err) {
return err;
}

if (args.size() != 2) {
r_err_str = "Expected 2 arguments for constructor";
if (args.size() == 0) {
value = Vector2i();
} else if (args.size() == 1) {
value = Vector2i(args[0], args[0]);
} else if (args.size() == 2) {
value = Vector2i(args[0], args[1]);
} else {
r_err_str = "Expected at most 2 arguments for constructor";
return ERR_PARSE_ERROR;
}

value = Vector2i(args[0], args[1]);
} else if (id == "Rect2") {
Vector<real_t> args;
Error err = _parse_construct<real_t>(p_stream, args, line, r_err_str);
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Vector2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
Constructs a new [Vector2] from [Vector2i].
</description>
</constructor>
<constructor name="Vector2">
<return type="Vector2" />
<param index="0" name="from" type="float" />
<description>
Constructs a new [Vector2] from [float].
</description>
</constructor>
<constructor name="Vector2">
<return type="Vector2" />
<param index="0" name="x" type="float" />
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/Vector2i.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
Constructs a new [Vector2i] from the given [Vector2] by truncating components' fractional parts (rounding towards zero). For a different behavior consider passing the result of [method Vector2.ceil], [method Vector2.floor] or [method Vector2.round] to this constructor instead.
</description>
</constructor>
<constructor name="Vector2i">
<return type="Vector2i" />
<param index="0" name="from" type="int" />
<description>
Constructs a new [Vector2] from [int].
</description>
</constructor>
<constructor name="Vector2i">
<return type="Vector2i" />
<param index="0" name="x" type="int" />
Expand Down
4 changes: 2 additions & 2 deletions drivers/gles3/effects/copy_effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ void CopyEffects::gaussian_blur(GLuint p_source_texture, int p_mipmap_count, con
}

float_size = Size2(base_size);
normalized_dest_region.position = Size2(dest_region.position) / float_size;
normalized_dest_region.size = Size2(dest_region.size) / float_size;
normalized_dest_region.position = Size2(dest_region.position / float_size);
normalized_dest_region.size = Size2(dest_region.size / float_size);

copy.shader.version_set_uniform(CopyShaderGLES3::COPY_SECTION, normalized_dest_region.position.x, normalized_dest_region.position.y, normalized_dest_region.size.x, normalized_dest_region.size.y, copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
copy.shader.version_set_uniform(CopyShaderGLES3::SOURCE_SECTION, normalized_source_region.position.x, normalized_source_region.position.y, normalized_source_region.size.x, normalized_source_region.size.y, copy.shader_version, CopyShaderGLES3::MODE_GAUSSIAN_BLUR);
Expand Down
6 changes: 3 additions & 3 deletions drivers/gles3/rasterizer_canvas_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend
Rect2 dst_rect(np->rect.position.x, np->rect.position.y, np->rect.size.x, np->rect.size.y);

if (np->texture == RID()) {
texpixel_size = Size2(1, 1);
texpixel_size = Size2(1);
src_rect = Rect2(0, 0, 1, 1);

} else {
Expand Down Expand Up @@ -1890,7 +1890,7 @@ void RasterizerCanvasGLES3::render_sdf(RID p_render_target, LightOccluderInstanc
Transform2D to_clip;
to_clip.columns[0] *= 2.0;
to_clip.columns[1] *= 2.0;
to_clip.columns[2] = -Vector2(1.0, 1.0);
to_clip.columns[2] = Vector2(-1);

to_clip = to_clip * to_sdf.affine_inverse();

Expand Down Expand Up @@ -2340,7 +2340,7 @@ void RasterizerCanvasGLES3::_prepare_canvas_texture(RID p_texture, RS::CanvasIte
}

// Enforce a 1x1 size if default white texture.
size_cache = ct->diffuse == default_texture_id ? Size2i(1, 1) : Size2i(texture->width, texture->height);
size_cache = ct->diffuse == default_texture_id ? Size2i(1) : Size2i(texture->width, texture->height);

GLES3::Texture *normal_map = texture_storage->get_texture(ct->normal_map);

Expand Down
14 changes: 7 additions & 7 deletions drivers/gles3/rasterizer_scene_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ void RasterizerSceneGLES3::_setup_environment(const RenderDataGLES3 *p_render_da
scene_state.ubo.viewport_size[0] = p_screen_size.x;
scene_state.ubo.viewport_size[1] = p_screen_size.y;

Size2 screen_pixel_size = Vector2(1.0, 1.0) / Size2(p_screen_size);
Size2 screen_pixel_size = Size2(1) / Size2(p_screen_size);
scene_state.ubo.screen_pixel_size[0] = screen_pixel_size.x;
scene_state.ubo.screen_pixel_size[1] = screen_pixel_size.y;

Expand Down Expand Up @@ -3639,7 +3639,7 @@ void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *

scene_state.ubo.emissive_exposure_normalization = -1.0; // Use default exposure normalization.

_setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false);
_setup_environment(&render_data, true, Vector2(1), true, Color(), false);

PassMode pass_mode = PASS_MODE_MATERIAL;

Expand Down Expand Up @@ -3681,19 +3681,19 @@ void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHT_SPOT;
base_spec_constant |= SceneShaderGLES3::DISABLE_LIGHTMAP;

RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, base_spec_constant, true, Vector2(0, 0));
RenderListParameters render_list_params(render_list[RENDER_LIST_SECONDARY].elements.ptr(), render_list[RENDER_LIST_SECONDARY].elements.size(), false, base_spec_constant, true, Vector2());

const int uv_offset_count = 9;
static const Vector2 uv_offsets[uv_offset_count] = {
Vector2(-1, 1),
Vector2(1, 1),
Vector2(1),
Vector2(1, -1),
Vector2(-1, -1),
Vector2(-1),
Vector2(-1, 0),
Vector2(1, 0),
Vector2(0, -1),
Vector2(0, 1),
Vector2(0, 0),
Vector2(),
};

for (int i = 0; i < uv_offset_count; i++) {
Expand All @@ -3704,7 +3704,7 @@ void RasterizerSceneGLES3::_render_uv2(const PagedArray<RenderGeometryInstance *
_render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());
}

render_list_params.uv_offset = Vector2(0, 0);
render_list_params.uv_offset = Vector2();
render_list_params.force_wireframe = false;
_render_list_template<PASS_MODE_MATERIAL>(&render_list_params, &render_data, 0, render_list[RENDER_LIST_SECONDARY].elements.size());

Expand Down
8 changes: 4 additions & 4 deletions drivers/gles3/rasterizer_scene_gles3.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class RasterizerSceneGLES3 : public RendererSceneRender {
bool reverse_cull = false;
uint64_t spec_constant_base_flags = 0;
bool force_wireframe = false;
Vector2 uv_offset = Vector2(0, 0);
Vector2 uv_offset = Vector2();

RenderListParameters(GeometryInstanceSurface **p_elements, int p_element_count, bool p_reverse_cull, uint64_t p_spec_constant_base_flags, bool p_force_wireframe = false, Vector2 p_uv_offset = Vector2()) {
elements = p_elements;
Expand Down Expand Up @@ -641,8 +641,8 @@ class RasterizerSceneGLES3 : public RendererSceneRender {
void _setup_lights(const RenderDataGLES3 *p_render_data, bool p_using_shadows, uint32_t &r_directional_light_count, uint32_t &r_omni_light_count, uint32_t &r_spot_light_count, uint32_t &r_directional_shadow_count);
void _setup_environment(const RenderDataGLES3 *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_pancake_shadows, float p_shadow_bias = 0.0);
void _fill_render_list(RenderListType p_render_list, const RenderDataGLES3 *p_render_data, PassMode p_pass_mode, bool p_append = false);
void _render_shadows(const RenderDataGLES3 *p_render_data, const Size2i &p_viewport_size = Size2i(1, 1));
void _render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, float p_lod_distance_multiplier = 0, float p_screen_mesh_lod_threshold = 0.0, RenderingMethod::RenderInfo *p_render_info = nullptr, const Size2i &p_viewport_size = Size2i(1, 1), const Transform3D &p_main_cam_transform = Transform3D());
void _render_shadows(const RenderDataGLES3 *p_render_data, const Size2i &p_viewport_size = Size2i(1));
void _render_shadow_pass(RID p_light, RID p_shadow_atlas, int p_pass, const PagedArray<RenderGeometryInstance *> &p_instances, float p_lod_distance_multiplier = 0, float p_screen_mesh_lod_threshold = 0.0, RenderingMethod::RenderInfo *p_render_info = nullptr, const Size2i &p_viewport_size = Size2i(1), const Transform3D &p_main_cam_transform = Transform3D());
void _render_post_processing(const RenderDataGLES3 *p_render_data);

template <PassMode p_pass_mode>
Expand Down Expand Up @@ -713,7 +713,7 @@ class RasterizerSceneGLES3 : public RendererSceneRender {
GLuint half_res_framebuffer = 0;
GLuint quarter_res_pass = 0;
GLuint quarter_res_framebuffer = 0;
Size2i screen_size = Size2i(0, 0);
Size2i screen_size = Size2i();

// Radiance Cubemap
GLuint radiance = 0;
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/storage/light_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ bool LightStorage::reflection_probe_instance_begin_render(RID p_instance, RID p_

if (atlas->render_buffers.is_null()) {
atlas->render_buffers.instantiate();
atlas->render_buffers->configure_for_probe(Size2i(atlas->size, atlas->size));
atlas->render_buffers->configure_for_probe(Size2i(atlas->size));
}

// First we check if our atlas is initialized.
Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/storage/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ void TextureStorage::update_texture_atlas() {

for (int i = 0; i < item_count; i++) {
TextureAtlas::Texture *t = texture_atlas.textures.getptr(items[i].texture);
t->uv_rect.position = items[i].pos * border + Vector2i(border / 2, border / 2);
t->uv_rect.position = items[i].pos * border + Vector2i(border / 2);
t->uv_rect.size = items[i].pixel_size;

t->uv_rect.position /= Size2(texture_atlas.size);
Expand Down
4 changes: 2 additions & 2 deletions drivers/gles3/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ struct Texture {
};

struct RenderTarget {
Point2i position = Point2i(0, 0);
Size2i size = Size2i(0, 0);
Point2i position = Point2i();
Size2i size = Size2i();
uint32_t view_count = 1;
int mipmap_count = 1;
RID self;
Expand Down
2 changes: 1 addition & 1 deletion drivers/vulkan/rendering_device_driver_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ Error RenderingDeviceDriverVulkan::_check_device_capabilities() {
vrs_capabilities.max_fragment_size.y = vrs_properties.maxFragmentSize.height; // generally the same as width

// We'll attempt to default to a texel size of 16x16.
vrs_capabilities.texel_size = Vector2i(16, 16).clamp(vrs_capabilities.min_texel_size, vrs_capabilities.max_texel_size);
vrs_capabilities.texel_size = Vector2i(16).clamp(vrs_capabilities.min_texel_size, vrs_capabilities.max_texel_size);

print_verbose(String(" Attachment fragment shading rate") + String(", min texel size: (") + itos(vrs_capabilities.min_texel_size.x) + String(", ") + itos(vrs_capabilities.min_texel_size.y) + String(")") + String(", max texel size: (") + itos(vrs_capabilities.max_texel_size.x) + String(", ") + itos(vrs_capabilities.max_texel_size.y) + String(")") + String(", max fragment size: (") + itos(vrs_capabilities.max_fragment_size.x) + String(", ") + itos(vrs_capabilities.max_fragment_size.y) + String(")"));
}
Expand Down
2 changes: 1 addition & 1 deletion editor/animation_bezier_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ void AnimationBezierTrackEdit::set_animation_and_track(const Ref<Animation> &p_a
}

Size2 AnimationBezierTrackEdit::get_minimum_size() const {
return Vector2(1, 1);
return Size2(1);
}

void AnimationBezierTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
Expand Down
Loading

0 comments on commit 8eef1fc

Please sign in to comment.