Skip to content

Commit

Permalink
feat: ✨ Introduce AABB Rotation logic to create non-axis aligned boun…
Browse files Browse the repository at this point in the history
…ding box

Special Thanks to @Donnerknalli !
  • Loading branch information
Marco Klein committed May 22, 2023
1 parent e4c90c7 commit ccc5815
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Source/Visuals/Extensions/VisualInstance3DExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,54 @@

namespace GoDough.Visuals.Extensions {
public static class VisualInstance3DExtensions {
public static Aabb GetGlobalAabb(this VisualInstance3D visualInstance3D) {
public static Aabb GetGlobalAabb(this MeshInstance3D visualInstance3D) {
// From PR:
//
// https://github.com/godotengine/godot/pull/66940
//
// get_global_transform().xform(get_aabb());

return visualInstance3D.GlobalTransform * visualInstance3D.GetAabb();
}

public static Aabb GetRotatedAndTransformedAabb(this MeshInstance3D visualInstance3D) {
var source = visualInstance3D.GetAabb();

var vertices = new Vector3[]{
source.Position, //bottomLeftFront,
new Vector3(source.End.X, source.Position.Y, source.Position.Z), //bottomRightFront,
new Vector3(source.End.X, source.End.Y, source.Position.Z), //topRightFront,
new Vector3(source.Position.X, source.End.Y, source.Position.Z), //topLeftFront,
new Vector3(source.Position.X, source.End.Y, source.End.Z), //topLeftBack,
new Vector3(source.End.X, source.End.Y, source.End.Z), //topRightBack,
new Vector3(source.End.X, source.Position.Y, source.End.Z), //bottomRightBack,
new Vector3(source.Position.X, source.Position.Y, source.End.Z), //bottomLeftBack,
};

var min = Vector3.Inf;
var max = -Vector3.Inf;

var scale = visualInstance3D.GlobalTransform.Basis.Scale;
var rotation = visualInstance3D.GlobalRotation;

for (var vertexIndex = 0; vertexIndex < vertices.Length; vertexIndex++) {
var vertex = vertices[vertexIndex];

var transformedVertex = scale * vertex;
vertices[vertexIndex] = transformedVertex;

if (transformedVertex < min) {
min = transformedVertex;
}

if (transformedVertex > max) {
max = transformedVertex;
}
}

var size = -(max - min);

return new Aabb(min, size);
}
}
}

0 comments on commit ccc5815

Please sign in to comment.