Add PackedVector4Array Variant type

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>
Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
K. S. Ernest (iFire) Lee 2024-04-08 07:51:34 -07:00 committed by Rémi Verschelde
parent b9e022302a
commit f9b488508c
No known key found for this signature in database
GPG key ID: C3336907360768E1
79 changed files with 1037 additions and 89 deletions

View file

@ -1178,6 +1178,73 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int
r_variant = carray;
} break;
case Variant::PACKED_VECTOR4_ARRAY: {
ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA);
int32_t count = decode_uint32(buf);
buf += 4;
len -= 4;
Vector<Vector4> varray;
if (header & HEADER_DATA_FLAG_64) {
ERR_FAIL_MUL_OF(count, sizeof(double) * 4, ERR_INVALID_DATA);
ERR_FAIL_COND_V(count < 0 || count * sizeof(double) * 4 > (size_t)len, ERR_INVALID_DATA);
if (r_len) {
(*r_len) += 4; // Size of count number.
}
if (count) {
varray.resize(count);
Vector4 *w = varray.ptrw();
for (int32_t i = 0; i < count; i++) {
w[i].x = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 0);
w[i].y = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 1);
w[i].z = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 2);
w[i].w = decode_double(buf + i * sizeof(double) * 4 + sizeof(double) * 3);
}
int adv = sizeof(double) * 4 * count;
if (r_len) {
(*r_len) += adv;
}
len -= adv;
buf += adv;
}
} else {
ERR_FAIL_MUL_OF(count, sizeof(float) * 4, ERR_INVALID_DATA);
ERR_FAIL_COND_V(count < 0 || count * sizeof(float) * 4 > (size_t)len, ERR_INVALID_DATA);
if (r_len) {
(*r_len) += 4; // Size of count number.
}
if (count) {
varray.resize(count);
Vector4 *w = varray.ptrw();
for (int32_t i = 0; i < count; i++) {
w[i].x = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 0);
w[i].y = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 1);
w[i].z = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 2);
w[i].w = decode_float(buf + i * sizeof(float) * 4 + sizeof(float) * 3);
}
int adv = sizeof(float) * 4 * count;
if (r_len) {
(*r_len) += adv;
}
len -= adv;
buf += adv;
}
}
r_variant = varray;
} break;
default: {
ERR_FAIL_V(ERR_BUG);
@ -1263,6 +1330,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
case Variant::VECTOR4:
case Variant::PACKED_VECTOR2_ARRAY:
case Variant::PACKED_VECTOR3_ARRAY:
case Variant::PACKED_VECTOR4_ARRAY:
case Variant::TRANSFORM2D:
case Variant::TRANSFORM3D:
case Variant::PROJECTION:
@ -1946,6 +2014,32 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo
r_len += 4 * 4 * len;
} break;
case Variant::PACKED_VECTOR4_ARRAY: {
Vector<Vector4> data = p_variant;
int len = data.size();
if (buf) {
encode_uint32(len, buf);
buf += 4;
}
r_len += 4;
if (buf) {
for (int i = 0; i < len; i++) {
Vector4 v = data.get(i);
encode_real(v.x, &buf[0]);
encode_real(v.y, &buf[sizeof(real_t)]);
encode_real(v.z, &buf[sizeof(real_t) * 2]);
encode_real(v.w, &buf[sizeof(real_t) * 3]);
buf += sizeof(real_t) * 4;
}
}
r_len += sizeof(real_t) * 4 * len;
} break;
default: {
ERR_FAIL_V(ERR_BUG);