Update box brush calculations to use face and vertex IDs

This commit is contained in:
2026-04-05 02:23:08 +02:00
parent ce5e18641b
commit b23cb0dd32
2 changed files with 17 additions and 31 deletions

View File

@@ -8,7 +8,7 @@ import {
type BoxFaceId, type BoxFaceId,
type BoxVertexId type BoxVertexId
} from "../document/brushes"; } from "../document/brushes";
import { getBoxBrushHalfSize } from "./box-brush"; import { getBoxBrushFaceVertexIds, getBoxBrushLocalVertexPosition } from "./box-brush-mesh";
type Sign = -1 | 1; type Sign = -1 | 1;
@@ -172,16 +172,18 @@ export function getBoxBrushVertexSigns(vertexId: BoxVertexId): BoxVertexSigns {
} }
export function getBoxBrushFaceWorldCenter(brush: BoxBrush, faceId: BoxFaceId): Vec3 { export function getBoxBrushFaceWorldCenter(brush: BoxBrush, faceId: BoxFaceId): Vec3 {
const halfSize = getBoxBrushHalfSize(brush); const faceVertexIds = getBoxBrushFaceVertexIds(faceId);
const meta = getBoxBrushFaceTransformMeta(faceId); const localCenter = faceVertexIds.reduce(
const localCenter = { (accumulator, vertexId) => {
x: 0, const vertex = getBoxBrushLocalVertexPosition(brush, vertexId);
y: 0, return {
z: 0 x: accumulator.x + vertex.x * 0.25,
}; y: accumulator.y + vertex.y * 0.25,
z: accumulator.z + vertex.z * 0.25
const axisOffset = meta.sign * (meta.axis === "x" ? halfSize.x : meta.axis === "y" ? halfSize.y : halfSize.z); };
localCenter[meta.axis] = axisOffset; },
{ x: 0, y: 0, z: 0 }
);
return transformBoxBrushLocalPointToWorld(brush, localCenter); return transformBoxBrushLocalPointToWorld(brush, localCenter);
} }
@@ -199,14 +201,7 @@ export function getBoxBrushFaceIdsForAxis(axis: BoxAxis): BoxFaceId[] {
} }
export function getBoxBrushVertexLocalPosition(brush: BoxBrush, vertexId: BoxVertexId): Vec3 { export function getBoxBrushVertexLocalPosition(brush: BoxBrush, vertexId: BoxVertexId): Vec3 {
const halfSize = getBoxBrushHalfSize(brush); return getBoxBrushLocalVertexPosition(brush, vertexId);
const signs = BOX_VERTEX_SIGNS[vertexId];
return {
x: signs.x * halfSize.x,
y: signs.y * halfSize.y,
z: signs.z * halfSize.z
};
} }
export function getBoxBrushVertexWorldPosition(brush: BoxBrush, vertexId: BoxVertexId): Vec3 { export function getBoxBrushVertexWorldPosition(brush: BoxBrush, vertexId: BoxVertexId): Vec3 {

View File

@@ -1,7 +1,7 @@
import { Euler, MathUtils, Vector3 } from "three"; import { Euler, MathUtils, Vector3 } from "three";
import type { Vec3 } from "../core/vector"; import type { Vec3 } from "../core/vector";
import { type BoxBrush } from "../document/brushes"; import { BOX_VERTEX_IDS, type BoxBrush } from "../document/brushes";
import { getBoxBrushLocalVertexPosition } from "./box-brush-mesh"; import { getBoxBrushLocalVertexPosition } from "./box-brush-mesh";
export interface BoxBrushBounds { export interface BoxBrushBounds {
@@ -45,17 +45,8 @@ export function getBoxBrushCornerPositions(brush: BoxBrush): Vec3[] {
MathUtils.degToRad(brush.rotationDegrees.z), MathUtils.degToRad(brush.rotationDegrees.z),
"XYZ" "XYZ"
); );
const offsets = [ const offsets = BOX_VERTEX_IDS.map((vertexId) => {
"negX_negY_negZ", const localVertex = getBoxBrushLocalVertexPosition(brush, vertexId);
"posX_negY_negZ",
"negX_posY_negZ",
"posX_posY_negZ",
"negX_negY_posZ",
"posX_negY_posZ",
"negX_posY_posZ",
"posX_posY_posZ"
].map((vertexId) => {
const localVertex = getBoxBrushLocalVertexPosition(brush, vertexId as keyof BoxBrush["geometry"]["vertices"] & never);
return new Vector3(localVertex.x, localVertex.y, localVertex.z); return new Vector3(localVertex.x, localVertex.y, localVertex.z);
}); });