diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 0faa56cb7b5..0332d14e10d 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1012,6 +1012,62 @@ struct _VariantCall { return dest; } + static PackedVector2Array func_PackedByteArray_decode_vector2_array(PackedByteArray *p_instance) { + uint64_t size = p_instance->size(); + PackedVector2Array dest; + if (size == 0) { + return dest; + } + ERR_FAIL_COND_V_MSG(size % sizeof(Vector2), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector2)) + " (size of Vector2) to convert to PackedVector2Array."); + const uint8_t *r = p_instance->ptr(); + dest.resize(size / sizeof(Vector2)); + ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed. + memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector2)); + return dest; + } + + static PackedVector3Array func_PackedByteArray_decode_vector3_array(PackedByteArray *p_instance) { + uint64_t size = p_instance->size(); + PackedVector3Array dest; + if (size == 0) { + return dest; + } + ERR_FAIL_COND_V_MSG(size % sizeof(Vector3), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector3)) + " (size of Vector3) to convert to PackedVector3Array."); + const uint8_t *r = p_instance->ptr(); + dest.resize(size / sizeof(Vector3)); + ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed. + memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector3)); + return dest; + } + + static PackedVector4Array func_PackedByteArray_decode_vector4_array(PackedByteArray *p_instance) { + uint64_t size = p_instance->size(); + PackedVector4Array dest; + if (size == 0) { + return dest; + } + ERR_FAIL_COND_V_MSG(size % sizeof(Vector4), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Vector4)) + " (size of Vector4) to convert to PackedVector4Array."); + const uint8_t *r = p_instance->ptr(); + dest.resize(size / sizeof(Vector4)); + ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed. + memcpy(dest.ptrw(), r, dest.size() * sizeof(Vector4)); + return dest; + } + + static PackedColorArray func_PackedByteArray_decode_color_array(PackedByteArray *p_instance) { + uint64_t size = p_instance->size(); + PackedColorArray dest; + if (size == 0) { + return dest; + } + ERR_FAIL_COND_V_MSG(size % sizeof(Color), dest, "PackedByteArray size must be a multiple of " + itos(sizeof(Color)) + " (size of Color variant) to convert to PackedColorArray."); + const uint8_t *r = p_instance->ptr(); + dest.resize(size / sizeof(Color)); + ERR_FAIL_COND_V(dest.is_empty(), dest); // Avoid UB in case resize failed. + memcpy(dest.ptrw(), r, dest.size() * sizeof(Color)); + return dest; + } + static void func_PackedByteArray_encode_u8(PackedByteArray *p_instance, int64_t p_offset, int64_t p_value) { uint64_t size = p_instance->size(); ERR_FAIL_COND(p_offset < 0 || p_offset > int64_t(size) - 1); @@ -2560,6 +2616,10 @@ static void _register_variant_builtin_methods_array() { bind_function(PackedByteArray, to_int64_array, _VariantCall::func_PackedByteArray_decode_s64_array, sarray(), varray()); bind_function(PackedByteArray, to_float32_array, _VariantCall::func_PackedByteArray_decode_float_array, sarray(), varray()); bind_function(PackedByteArray, to_float64_array, _VariantCall::func_PackedByteArray_decode_double_array, sarray(), varray()); + bind_function(PackedByteArray, to_vector2_array, _VariantCall::func_PackedByteArray_decode_vector2_array, sarray(), varray()); + bind_function(PackedByteArray, to_vector3_array, _VariantCall::func_PackedByteArray_decode_vector3_array, sarray(), varray()); + bind_function(PackedByteArray, to_vector4_array, _VariantCall::func_PackedByteArray_decode_vector4_array, sarray(), varray()); + bind_function(PackedByteArray, to_color_array, _VariantCall::func_PackedByteArray_decode_color_array, sarray(), varray()); bind_functionnc(PackedByteArray, bswap16, _VariantCall::func_PackedByteArray_bswap16, sarray("offset", "count"), varray(0, -1)); bind_functionnc(PackedByteArray, bswap32, _VariantCall::func_PackedByteArray_bswap32, sarray("offset", "count"), varray(0, -1)); diff --git a/doc/classes/PackedByteArray.xml b/doc/classes/PackedByteArray.xml index 1776577ab04..a614d6e357f 100644 --- a/doc/classes/PackedByteArray.xml +++ b/doc/classes/PackedByteArray.xml @@ -497,6 +497,13 @@ Sorts the elements of the array in ascending order. + + + + Returns a copy of the data converted to a [PackedColorArray], where each block of 16 bytes has been converted to a [Color] variant. + [b]Note:[/b] The size of the input array must be a multiple of 16 (size of four 32-bit float variables). The size of the new array will be [code]byte_array.size() / 16[/code]. If the original data can't be converted to [Color] variants, the resulting data is undefined. + + @@ -529,6 +536,27 @@ If the original data can't be converted to signed 64-bit integers, the resulting data is undefined. + + + + Returns a copy of the data converted to a [PackedVector2Array], where each block of 8 bytes or 16 bytes (32-bit or 64-bit) has been converted to a [Vector2] variant. + [b]Note:[/b] The size of the input array must be a multiple of 8 or 16 (depending on the build settings, see [Vector2] for more details). The size of the new array will be [code]byte_array.size() / (8 or 16)[/code]. If the original data can't be converted to [Vector2] variants, the resulting data is undefined. + + + + + + Returns a copy of the data converted to a [PackedVector3Array], where each block of 12 or 24 bytes (32-bit or 64-bit) has been converted to a [Vector3] variant. + [b]Note:[/b] The size of the input array must be a multiple of 12 or 24 (depending on the build settings, see [Vector3] for more details). The size of the new array will be [code]byte_array.size() / (12 or 24)[/code]. If the original data can't be converted to [Vector3] variants, the resulting data is undefined. + + + + + + Returns a copy of the data converted to a [PackedVector4Array], where each block of 16 or 32 bytes (32-bit or 64-bit) has been converted to a [Vector4] variant. + [b]Note:[/b] The size of the input array must be a multiple of 16 or 32 (depending on the build settings, see [Vector4] for more details). The size of the new array will be [code]byte_array.size() / (16 or 32)[/code]. If the original data can't be converted to [Vector4] variants, the resulting data is undefined. + +