Skip to content

Commit

Permalink
Add Vector4 as new variant type
Browse files Browse the repository at this point in the history
(+ mono and (visual) shader integration)
  • Loading branch information
Geometror committed Apr 8, 2022
1 parent bf153b8 commit fd32876
Show file tree
Hide file tree
Showing 85 changed files with 3,845 additions and 88 deletions.
1 change: 1 addition & 0 deletions core/core_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ void register_global_constants() {
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_RECT2I", Variant::RECT2I);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_VECTOR3", Variant::VECTOR3);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_VECTOR3I", Variant::VECTOR3I);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_VECTOR4", Variant::VECTOR4);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_TRANSFORM2D", Variant::TRANSFORM2D);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_PLANE", Variant::PLANE);
BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_QUATERNION", Variant::QUATERNION);
Expand Down
7 changes: 7 additions & 0 deletions core/extension/extension_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() {
}

const uint32_t vec3_elems = 3;
const uint32_t vec4_elems = 4;
const uint32_t ptrsize_32 = 4;
const uint32_t ptrsize_64 = 8;
static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };
Expand Down Expand Up @@ -127,6 +128,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() {
{ Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
{ Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
{ Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },
{ Variant::VECTOR4, vec4_elems * sizeof(float), vec4_elems * sizeof(float), vec4_elems * sizeof(double), vec4_elems * sizeof(double) },
{ Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },
{ Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },
{ Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
Expand Down Expand Up @@ -165,6 +167,7 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() {
static_assert(type_size_array[Variant::RECT2I][sizeof(void *)] == sizeof(Rect2i), "Size of Rect2i mismatch");
static_assert(type_size_array[Variant::VECTOR3][sizeof(void *)] == sizeof(Vector3), "Size of Vector3 mismatch");
static_assert(type_size_array[Variant::VECTOR3I][sizeof(void *)] == sizeof(Vector3i), "Size of Vector3i mismatch");
static_assert(type_size_array[Variant::VECTOR4][sizeof(void *)] == sizeof(Vector4), "Size of Vector4 mismatch");
static_assert(type_size_array[Variant::TRANSFORM2D][sizeof(void *)] == sizeof(Transform2D), "Size of Transform2D mismatch");
static_assert(type_size_array[Variant::PLANE][sizeof(void *)] == sizeof(Plane), "Size of Plane mismatch");
static_assert(type_size_array[Variant::QUATERNION][sizeof(void *)] == sizeof(Quaternion), "Size of Quaternion mismatch");
Expand Down Expand Up @@ -250,6 +253,10 @@ Dictionary NativeExtensionAPIDump::generate_extension_api() {
{ Variant::VECTOR3I, "x", 0, 0, 0, 0 },
{ Variant::VECTOR3I, "y", sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) },
{ Variant::VECTOR3I, "z", 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
{ Variant::VECTOR4, "x", 0, 0, 0, 0 },
{ Variant::VECTOR4, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
{ Variant::VECTOR4, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
{ Variant::VECTOR4, "w", 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(double), 3 * sizeof(double) },
{ Variant::TRANSFORM2D, "x", 0, 0, 0, 0 },
{ Variant::TRANSFORM2D, "y", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
{ Variant::TRANSFORM2D, "origin", 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
Expand Down
4 changes: 4 additions & 0 deletions core/extension/gdnative_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ static GDNativeVariantFromTypeConstructorFunc gdnative_get_variant_from_type_con
return VariantTypeConstructor<Vector3>::variant_from_type;
case GDNATIVE_VARIANT_TYPE_VECTOR3I:
return VariantTypeConstructor<Vector3i>::variant_from_type;
case GDNATIVE_VARIANT_TYPE_VECTOR4:
return VariantTypeConstructor<Vector4>::variant_from_type;
case GDNATIVE_VARIANT_TYPE_TRANSFORM2D:
return VariantTypeConstructor<Transform2D>::variant_from_type;
case GDNATIVE_VARIANT_TYPE_PLANE:
Expand Down Expand Up @@ -402,6 +404,8 @@ static GDNativeTypeFromVariantConstructorFunc gdnative_get_type_from_variant_con
return VariantTypeConstructor<Vector3>::type_from_variant;
case GDNATIVE_VARIANT_TYPE_VECTOR3I:
return VariantTypeConstructor<Vector3i>::type_from_variant;
case GDNATIVE_VARIANT_TYPE_VECTOR4:
return VariantTypeConstructor<Vector4>::type_from_variant;
case GDNATIVE_VARIANT_TYPE_TRANSFORM2D:
return VariantTypeConstructor<Transform2D>::type_from_variant;
case GDNATIVE_VARIANT_TYPE_PLANE:
Expand Down
1 change: 1 addition & 0 deletions core/extension/gdnative_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ typedef enum {
GDNATIVE_VARIANT_TYPE_RECT2I,
GDNATIVE_VARIANT_TYPE_VECTOR3,
GDNATIVE_VARIANT_TYPE_VECTOR3I,
GDNATIVE_VARIANT_TYPE_VECTOR4,
GDNATIVE_VARIANT_TYPE_TRANSFORM2D,
GDNATIVE_VARIANT_TYPE_PLANE,
GDNATIVE_VARIANT_TYPE_QUATERNION,
Expand Down
39 changes: 39 additions & 0 deletions core/io/marshalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,32 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
(*r_len) += 4 * 3;
}

} break;
case Variant::VECTOR4: {
Vector4 val;
if (type & ENCODE_FLAG_64) {
ERR_FAIL_COND_V((size_t)len < sizeof(double) * 4, ERR_INVALID_DATA);
val.x = decode_double(&buf[0]);
val.y = decode_double(&buf[sizeof(double)]);
val.z = decode_double(&buf[sizeof(double) * 2]);
val.w = decode_double(&buf[sizeof(double) * 3]);

if (r_len) {
(*r_len) += sizeof(double) * 4;
}
} else {
ERR_FAIL_COND_V((size_t)len < sizeof(float) * 4, ERR_INVALID_DATA);
val.x = decode_float(&buf[0]);
val.y = decode_float(&buf[sizeof(float)]);
val.z = decode_float(&buf[sizeof(float) * 2]);
val.w = decode_float(&buf[sizeof(float) * 3]);

if (r_len) {
(*r_len) += sizeof(float) * 4;
}
}
r_variant = val;

} break;
case Variant::TRANSFORM2D: {
Transform2D val;
Expand Down Expand Up @@ -1071,6 +1097,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
#ifdef REAL_T_IS_DOUBLE
case Variant::VECTOR2:
case Variant::VECTOR3:
case Variant::VECTOR4:
case Variant::PACKED_VECTOR2_ARRAY:
case Variant::PACKED_VECTOR3_ARRAY:
case Variant::TRANSFORM2D:
Expand Down Expand Up @@ -1255,6 +1282,18 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo

r_len += 3 * 4;

} break;
case Variant::VECTOR4: {
if (buf) {
Vector4 v4 = p_variant;
encode_real(v4.x, &buf[0]);
encode_real(v4.y, &buf[sizeof(real_t)]);
encode_real(v4.z, &buf[sizeof(real_t) * 2]);
encode_real(v4.w, &buf[sizeof(real_t) * 3]);
}

r_len += 4 * sizeof(real_t);

} break;
case Variant::TRANSFORM2D: {
if (buf) {
Expand Down
1 change: 1 addition & 0 deletions core/io/packed_data_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpd
case Variant::VECTOR2:
case Variant::RECT2:
case Variant::VECTOR3:
case Variant::VECTOR4:
case Variant::TRANSFORM2D:
case Variant::PLANE:
case Variant::QUATERNION:
Expand Down
18 changes: 18 additions & 0 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ enum {
VARIANT_VECTOR3I = 47,
VARIANT_PACKED_INT64_ARRAY = 48,
VARIANT_PACKED_FLOAT64_ARRAY = 49,
VARIANT_VECTOR4 = 50,
OBJECT_EMPTY = 0,
OBJECT_EXTERNAL_RESOURCE = 1,
OBJECT_INTERNAL_RESOURCE = 2,
Expand Down Expand Up @@ -236,6 +237,14 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) {
v.z = f->get_32();
r_v = v;
} break;
case VARIANT_VECTOR4: {
Vector4 v;
v.x = f->get_real();
v.y = f->get_real();
v.z = f->get_real();
v.w = f->get_real();
r_v = v;
} break;
case VARIANT_PLANE: {
Plane v;
v.normal.x = f->get_real();
Expand Down Expand Up @@ -1459,6 +1468,15 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia
f->store_32(val.y);
f->store_32(val.z);

} break;
case Variant::VECTOR4: {
f->store_32(VARIANT_VECTOR4);
Vector4 val = p_property;
f->store_32(val.x);
f->store_32(val.y);
f->store_32(val.z);
f->store_32(val.w);

} break;
case Variant::PLANE: {
f->store_32(VARIANT_PLANE);
Expand Down
11 changes: 11 additions & 0 deletions core/math/math_fieldwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ Variant fieldwise_assign(const Variant &p_target, const Variant &p_source, const
return target;
}

case Variant::VECTOR4: {
SETUP_TYPE(Vector4)

/**/ TRY_TRANSFER_FIELD("x", x)
else TRY_TRANSFER_FIELD("y", y)
else TRY_TRANSFER_FIELD("z", z)
else TRY_TRANSFER_FIELD("w", w)

return target;
}

case Variant::PLANE: {
SETUP_TYPE(Plane)

Expand Down
100 changes: 100 additions & 0 deletions core/math/vector4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*************************************************************************/
/* vector4.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "vector4.h"

#include "core/math/basis.h"
#include "core/string/ustring.h"

void Vector4::set_axis(const int p_axis, const real_t p_value) {
ERR_FAIL_INDEX(p_axis, AXIS_COUNT);
coord[p_axis] = p_value;
}

real_t Vector4::get_axis(const int p_axis) const {
ERR_FAIL_INDEX_V(p_axis, AXIS_COUNT, 0);
return operator[](p_axis);
}

Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const {
return Vector4(
CLAMP(x, p_min.x, p_max.x),
CLAMP(y, p_min.y, p_max.y),
CLAMP(z, p_min.z, p_max.z),
CLAMP(w, p_min.w, p_max.w));
}

void Vector4::snap(const Vector4 p_step) {
x = Math::snapped(x, p_step.x);
y = Math::snapped(y, p_step.y);
z = Math::snapped(z, p_step.z);
w = Math::snapped(w, p_step.w);
}

Vector4 Vector4::snapped(const Vector4 p_step) const {
Vector4 v = *this;
v.snap(p_step);
return v;
}

Vector4 Vector4::limit_length(const real_t p_len) const {
const real_t l = length();
Vector4 v = *this;
if (l > 0 && p_len < l) {
v /= l;
v *= p_len;
}

return v;
}

Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const {
Vector4 res = *this;
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight);
return res;
}

Vector4 Vector4::move_toward(const Vector4 &p_to, const real_t p_delta) const {
Vector4 v = *this;
Vector4 vd = p_to - v;
real_t len = vd.length();
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta;
}

bool Vector4::is_equal_approx(const Vector4 &p_v) const {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z) && Math::is_equal_approx(w, p_v.w);
}

Vector4::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
}
Loading

0 comments on commit fd32876

Please sign in to comment.