Skip to content

Commit

Permalink
added blend_variant support for packed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jun 26, 2023
1 parent 9fa20fb commit 117f764
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
87 changes: 87 additions & 0 deletions scene/resources/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5781,6 +5781,71 @@ Variant Animation::blend_variant(const Variant &a, const Variant &b, float c) {
return (a.operator Transform3D()) * Transform3D().interpolate_with((b.operator Transform3D()), c);
}
default: {
if (a.is_array()) {
switch (a.get_type()) {
case Variant::ARRAY: {
Array arr_a = a;
Array arr_b = b;
int size = arr_a.size();
if (size == 0 || arr_b.size() != size) {
return a;
}

Array result;

if (arr_a.is_typed() && arr_b.is_typed()) {
uint32_t type_a = arr_a.get_typed_builtin();
uint32_t type_b = arr_b.get_typed_builtin();

if (type_a != type_b) {
// Do not blend different typed Arrays, make an exception for numbers.
if ((type_a != Variant::FLOAT && type_b != Variant::INT) &&
type_a != Variant::INT && type_b != Variant::FLOAT) {
return a;
}
}

result.set_typed(type_a, StringName(), Variant());
}

result.resize(size);
for (int i = 0; i < size; i++) {
result[i] = blend_variant(arr_a[i], arr_b[i], c);
}
return result;
} break;
case Variant::PACKED_BYTE_ARRAY: {
return blend_packed_array(PackedByteArray(a), PackedByteArray(b), c);
}
case Variant::PACKED_INT32_ARRAY: {
return blend_packed_array(PackedInt32Array(a), PackedInt32Array(b), c);
}
case Variant::PACKED_INT64_ARRAY: {
return blend_packed_array(PackedInt64Array(a), PackedInt64Array(b), c);
}
case Variant::PACKED_FLOAT32_ARRAY: {
return blend_packed_array(PackedFloat32Array(a), PackedFloat32Array(b), c);
}
case Variant::PACKED_FLOAT64_ARRAY: {
return blend_packed_array(PackedFloat64Array(a), PackedFloat64Array(b), c);
}
case Variant::PACKED_STRING_ARRAY: {
return blend_packed_array(PackedStringArray(a), PackedStringArray(b), c);
}
case Variant::PACKED_VECTOR2_ARRAY: {
return blend_packed_array(PackedVector2Array(a), PackedVector2Array(b), c);
}
case Variant::PACKED_VECTOR3_ARRAY: {
return blend_packed_array(PackedVector3Array(a), PackedVector3Array(b), c);
}
case Variant::PACKED_COLOR_ARRAY: {
return blend_packed_array(PackedColorArray(a), PackedColorArray(b), c);
}
default: {
return a;
}
}
}
return c < 0.5 ? a : b;
}
}
Expand Down Expand Up @@ -6018,6 +6083,27 @@ Vector<T> Animation::subtract_packed_array(Vector<T> a, Vector<T> b) {
}
}

template <class T>
Vector<T> Animation::blend_packed_array(Vector<T> a, Vector<T> b, float c) {
int size = a.size();
if (size == 0 || b.size() != size) {
return a;
}
else {
Vector<T> result;
result.resize(size);

T* result_ptr = result.ptrw();
const T* a_ptr = a.ptr();
const T* b_ptr = b.ptr();

for (int i = 0; i < size; i++) {
result_ptr[i] = blend_variant(a_ptr[i], b_ptr[i], c);
}
return result;
}
}

template <class T>
Vector<T> Animation::interpolate_packed_array(Vector<T> a, Vector<T> b, float c) {
int size = a.size();
Expand All @@ -6039,6 +6125,7 @@ Vector<T> Animation::interpolate_packed_array(Vector<T> a, Vector<T> b, float c)
}
}


Animation::Animation() {
}

Expand Down
2 changes: 2 additions & 0 deletions scene/resources/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ class Animation : public Resource {
template <class T>
static Vector<T> subtract_packed_array(Vector<T> a, Vector<T> b);
template <class T>
static Vector<T> blend_packed_array(Vector<T> a, Vector<T> b, float c);
template <class T>
static Vector<T> interpolate_packed_array(Vector<T> a, Vector<T> b, float c);

Animation();
Expand Down

0 comments on commit 117f764

Please sign in to comment.