2026-04-04 19:25:43 +02:00
|
|
|
import { Euler, MathUtils, Vector3 } from "three";
|
|
|
|
|
|
2026-03-31 02:02:34 +02:00
|
|
|
import type { Vec3 } from "../core/vector";
|
2026-04-15 07:43:30 +02:00
|
|
|
import type { Brush } from "../document/brushes";
|
2026-04-05 02:22:33 +02:00
|
|
|
import { getBoxBrushLocalVertexPosition } from "./box-brush-mesh";
|
2026-04-15 07:43:30 +02:00
|
|
|
import { getBrushVertexIds } from "./whitebox-topology";
|
2026-03-31 02:02:34 +02:00
|
|
|
|
|
|
|
|
export interface BoxBrushBounds {
|
|
|
|
|
min: Vec3;
|
|
|
|
|
max: Vec3;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 07:43:30 +02:00
|
|
|
export function getBoxBrushHalfSize(brush: Brush): Vec3 {
|
2026-03-31 02:02:34 +02:00
|
|
|
return {
|
|
|
|
|
x: brush.size.x * 0.5,
|
|
|
|
|
y: brush.size.y * 0.5,
|
|
|
|
|
z: brush.size.z * 0.5
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 07:43:30 +02:00
|
|
|
export function getBoxBrushBounds(brush: Brush): BoxBrushBounds {
|
2026-04-04 19:25:43 +02:00
|
|
|
const corners = getBoxBrushCornerPositions(brush);
|
|
|
|
|
const firstCorner = corners[0];
|
|
|
|
|
const min = { ...firstCorner };
|
|
|
|
|
const max = { ...firstCorner };
|
|
|
|
|
|
|
|
|
|
for (const corner of corners.slice(1)) {
|
|
|
|
|
min.x = Math.min(min.x, corner.x);
|
|
|
|
|
min.y = Math.min(min.y, corner.y);
|
|
|
|
|
min.z = Math.min(min.z, corner.z);
|
|
|
|
|
max.x = Math.max(max.x, corner.x);
|
|
|
|
|
max.y = Math.max(max.y, corner.y);
|
|
|
|
|
max.z = Math.max(max.z, corner.z);
|
|
|
|
|
}
|
2026-03-31 02:02:34 +02:00
|
|
|
|
|
|
|
|
return {
|
2026-04-04 19:25:43 +02:00
|
|
|
min,
|
|
|
|
|
max
|
2026-03-31 02:02:34 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-15 07:43:30 +02:00
|
|
|
export function getBoxBrushCornerPositions(brush: Brush): Vec3[] {
|
2026-04-04 19:25:43 +02:00
|
|
|
const rotation = new Euler(
|
|
|
|
|
MathUtils.degToRad(brush.rotationDegrees.x),
|
|
|
|
|
MathUtils.degToRad(brush.rotationDegrees.y),
|
|
|
|
|
MathUtils.degToRad(brush.rotationDegrees.z),
|
|
|
|
|
"XYZ"
|
|
|
|
|
);
|
2026-04-15 07:43:30 +02:00
|
|
|
const offsets = getBrushVertexIds(brush).map((vertexId) => {
|
2026-04-05 02:23:08 +02:00
|
|
|
const localVertex = getBoxBrushLocalVertexPosition(brush, vertexId);
|
2026-04-05 02:22:33 +02:00
|
|
|
return new Vector3(localVertex.x, localVertex.y, localVertex.z);
|
|
|
|
|
});
|
2026-04-04 19:25:43 +02:00
|
|
|
|
|
|
|
|
return offsets.map((offset) => {
|
|
|
|
|
const rotatedOffset = offset.clone().applyEuler(rotation);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
x: brush.center.x + rotatedOffset.x,
|
|
|
|
|
y: brush.center.y + rotatedOffset.y,
|
|
|
|
|
z: brush.center.z + rotatedOffset.z
|
|
|
|
|
};
|
|
|
|
|
});
|
2026-03-31 02:02:34 +02:00
|
|
|
}
|