From 1da2eb3b1fabe382106300e04249ee898e5a103b Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Sat, 4 Apr 2026 19:25:43 +0200 Subject: [PATCH] Update scene document version and enhance box brush calculations --- src/document/scene-document.ts | 3 +- src/geometry/box-brush.ts | 65 ++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/document/scene-document.ts b/src/document/scene-document.ts index 4f387758..44dc08f1 100644 --- a/src/document/scene-document.ts +++ b/src/document/scene-document.ts @@ -6,7 +6,8 @@ import type { InteractionLink } from "../interactions/interaction-links"; import { cloneMaterialRegistry, createStarterMaterialRegistry, type MaterialDef } from "../materials/starter-material-library"; import { createDefaultWorldSettings, type WorldSettings } from "./world-settings"; -export const SCENE_DOCUMENT_VERSION = 17 as const; +export const SCENE_DOCUMENT_VERSION = 18 as const; +export const WHITEBOX_FLOAT_TRANSFORM_SCENE_DOCUMENT_VERSION = 18 as const; export const PLAYER_START_COLLIDER_SETTINGS_SCENE_DOCUMENT_VERSION = 17 as const; export const IMPORTED_MODEL_COLLIDERS_SCENE_DOCUMENT_VERSION = 16 as const; export const ENTITY_NAMES_SCENE_DOCUMENT_VERSION = 15 as const; diff --git a/src/geometry/box-brush.ts b/src/geometry/box-brush.ts index dd56415d..f9cb34f6 100644 --- a/src/geometry/box-brush.ts +++ b/src/geometry/box-brush.ts @@ -1,3 +1,5 @@ +import { Euler, MathUtils, Vector3 } from "three"; + import type { Vec3 } from "../core/vector"; import type { BoxBrush } from "../document/brushes"; @@ -15,33 +17,52 @@ export function getBoxBrushHalfSize(brush: BoxBrush): Vec3 { } export function getBoxBrushBounds(brush: BoxBrush): BoxBrushBounds { - const halfSize = getBoxBrushHalfSize(brush); + 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); + } return { - min: { - x: brush.center.x - halfSize.x, - y: brush.center.y - halfSize.y, - z: brush.center.z - halfSize.z - }, - max: { - x: brush.center.x + halfSize.x, - y: brush.center.y + halfSize.y, - z: brush.center.z + halfSize.z - } + min, + max }; } export function getBoxBrushCornerPositions(brush: BoxBrush): Vec3[] { - const bounds = getBoxBrushBounds(brush); - - return [ - { x: bounds.min.x, y: bounds.min.y, z: bounds.min.z }, - { x: bounds.max.x, y: bounds.min.y, z: bounds.min.z }, - { x: bounds.min.x, y: bounds.max.y, z: bounds.min.z }, - { x: bounds.max.x, y: bounds.max.y, z: bounds.min.z }, - { x: bounds.min.x, y: bounds.min.y, z: bounds.max.z }, - { x: bounds.max.x, y: bounds.min.y, z: bounds.max.z }, - { x: bounds.min.x, y: bounds.max.y, z: bounds.max.z }, - { x: bounds.max.x, y: bounds.max.y, z: bounds.max.z } + const halfSize = getBoxBrushHalfSize(brush); + const rotation = new Euler( + MathUtils.degToRad(brush.rotationDegrees.x), + MathUtils.degToRad(brush.rotationDegrees.y), + MathUtils.degToRad(brush.rotationDegrees.z), + "XYZ" + ); + const offsets = [ + new Vector3(-halfSize.x, -halfSize.y, -halfSize.z), + new Vector3(halfSize.x, -halfSize.y, -halfSize.z), + new Vector3(-halfSize.x, halfSize.y, -halfSize.z), + new Vector3(halfSize.x, halfSize.y, -halfSize.z), + new Vector3(-halfSize.x, -halfSize.y, halfSize.z), + new Vector3(halfSize.x, -halfSize.y, halfSize.z), + new Vector3(-halfSize.x, halfSize.y, halfSize.z), + new Vector3(halfSize.x, halfSize.y, halfSize.z) ]; + + 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 + }; + }); }