Skip to content

Commit 61759da

Browse files
committed
Fix some gizmo behavior to make more consistent
1 parent 42f8bfa commit 61759da

8 files changed

+160
-83
lines changed

core/math/basis.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ Basis Basis::orthonormalized() const {
9494
return c;
9595
}
9696

97+
void Basis::orthogonalize() {
98+
Vector3 scl = get_scale();
99+
orthonormalize();
100+
scale_local(scl);
101+
}
102+
103+
Basis Basis::orthogonalized() const {
104+
Basis c = *this;
105+
c.orthogonalize();
106+
return c;
107+
}
108+
97109
bool Basis::is_orthogonal() const {
98110
Basis identity;
99111
Basis m = (*this) * transposed();
@@ -237,6 +249,24 @@ void Basis::scale_local(const Vector3 &p_scale) {
237249
*this = scaled_local(p_scale);
238250
}
239251

252+
void Basis::scale_orthogonal(const Vector3 &p_scale) {
253+
*this = scaled_orthogonal(p_scale);
254+
}
255+
256+
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
257+
Basis m = *this;
258+
Vector3 s = Vector3(-1, -1, -1) + p_scale;
259+
Vector3 dots;
260+
Basis b;
261+
for (int i = 0; i < 3; i++) {
262+
for (int j = 0; j < 3; j++) {
263+
dots[j] += s[i] * abs(m.get_axis(i).normalized().dot(b.get_axis(j)));
264+
}
265+
}
266+
m.scale_local(Vector3(1, 1, 1) + dots);
267+
return m;
268+
}
269+
240270
float Basis::get_uniform_scale() const {
241271
return (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0;
242272
}
@@ -931,6 +961,15 @@ void Basis::_set_diagonal(const Vector3 &p_diag) {
931961
elements[2][2] = p_diag.z;
932962
}
933963

964+
Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
965+
Basis b;
966+
b.elements[0] = elements[0].lerp(p_to.elements[0], p_weight);
967+
b.elements[1] = elements[1].lerp(p_to.elements[1], p_weight);
968+
b.elements[2] = elements[2].lerp(p_to.elements[2], p_weight);
969+
970+
return b;
971+
}
972+
934973
Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
935974
//consider scale
936975
Quaternion from(*this);

core/math/basis.h

+7
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class Basis {
123123
void scale_local(const Vector3 &p_scale);
124124
Basis scaled_local(const Vector3 &p_scale) const;
125125

126+
void scale_orthogonal(const Vector3 &p_scale);
127+
Basis scaled_orthogonal(const Vector3 &p_scale) const;
128+
126129
void make_scale_uniform();
127130
float get_uniform_scale() const;
128131

@@ -168,6 +171,7 @@ class Basis {
168171
bool is_diagonal() const;
169172
bool is_rotation() const;
170173

174+
Basis lerp(const Basis &p_to, const real_t &p_weight) const;
171175
Basis slerp(const Basis &p_to, const real_t &p_weight) const;
172176
void rotate_sh(real_t *p_values);
173177

@@ -233,6 +237,9 @@ class Basis {
233237
void orthonormalize();
234238
Basis orthonormalized() const;
235239

240+
void orthogonalize();
241+
Basis orthogonalized() const;
242+
236243
#ifdef MATH_CHECKS
237244
bool is_symmetric() const;
238245
#endif

core/math/transform_3d.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, con
8080
origin = p_eye;
8181
}
8282

83-
Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
83+
Transform3D Transform3D::sphere_interpolate_with(const Transform3D &p_transform, real_t p_c) const {
8484
/* not sure if very "efficient" but good enough? */
8585

86+
Transform3D interp;
87+
8688
Vector3 src_scale = basis.get_scale();
8789
Quaternion src_rot = basis.get_rotation_quaternion();
8890
Vector3 src_loc = origin;
@@ -91,13 +93,21 @@ Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t
9193
Quaternion dst_rot = p_transform.basis.get_rotation_quaternion();
9294
Vector3 dst_loc = p_transform.origin;
9395

94-
Transform3D interp;
9596
interp.basis.set_quaternion_scale(src_rot.slerp(dst_rot, p_c).normalized(), src_scale.lerp(dst_scale, p_c));
9697
interp.origin = src_loc.lerp(dst_loc, p_c);
9798

9899
return interp;
99100
}
100101

102+
Transform3D Transform3D::interpolate_with(const Transform3D &p_transform, real_t p_c) const {
103+
Transform3D interp;
104+
105+
interp.basis = basis.lerp(p_transform.basis, p_c);
106+
interp.origin = origin.lerp(p_transform.origin, p_c);
107+
108+
return interp;
109+
}
110+
101111
void Transform3D::scale(const Vector3 &p_scale) {
102112
basis.scale(p_scale);
103113
origin *= p_scale;
@@ -139,6 +149,16 @@ Transform3D Transform3D::orthonormalized() const {
139149
return _copy;
140150
}
141151

152+
void Transform3D::orthogonalize() {
153+
basis.orthogonalize();
154+
}
155+
156+
Transform3D Transform3D::orthogonalized() const {
157+
Transform3D _copy = *this;
158+
_copy.orthogonalize();
159+
return _copy;
160+
}
161+
142162
bool Transform3D::is_equal_approx(const Transform3D &p_transform) const {
143163
return basis.is_equal_approx(p_transform.basis) && origin.is_equal_approx(p_transform.origin);
144164
}

core/math/transform_3d.h

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Transform3D {
6969

7070
void orthonormalize();
7171
Transform3D orthonormalized() const;
72+
void orthogonalize();
73+
Transform3D orthogonalized() const;
7274
bool is_equal_approx(const Transform3D &p_transform) const;
7375

7476
bool operator==(const Transform3D &p_transform) const;
@@ -99,6 +101,7 @@ class Transform3D {
99101
void operator*=(const real_t p_val);
100102
Transform3D operator*(const real_t p_val) const;
101103

104+
Transform3D sphere_interpolate_with(const Transform3D &p_transform, real_t p_c) const;
102105
Transform3D interpolate_with(const Transform3D &p_transform, real_t p_c) const;
103106

104107
_FORCE_INLINE_ Transform3D inverse_xform(const Transform3D &t) const {

core/variant/variant_setget.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2120,7 +2120,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
21202120
}
21212121
return;
21222122
case BASIS: {
2123-
r_dst = Transform3D(*a._data._basis).interpolate_with(Transform3D(*b._data._basis), c).basis;
2123+
r_dst = a._data._basis->lerp(*b._data._basis, c);
21242124
}
21252125
return;
21262126
case TRANSFORM3D: {

0 commit comments

Comments
 (0)