Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Turn preview plane manager into class and simplify edit skeleton implementation

Scott Petrovic578a88b98e5d
diff --git a/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts b/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts
index e53dfb8..8920a5c 100644
--- a/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts
+++ b/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts
@@ -1,151 +1,182 @@
import { Group, Mesh, PlaneGeometry, MeshBasicMaterial, type Scene, DoubleSide } from 'three'
-const preview_plane_group_name: string = 'preview_plane_group'
-
/**
- * Add a preview 3D plane to the scene at the origin
+ * PreviewPlaneManager - Singleton class for managing a preview 3D plane in the edit skeleton step
* The plane can be adjusted in height for later use in the skinning process
- * @param root The main scene
- * @param height The height position of the plane (Y coordinate)
- * @param size The size of the plane (width and depth)
- * @returns The created plane mesh
*/
-export function add_preview_plane (
- root: Scene,
- height: number = 0.0,
- size: number = 5.0
-): Mesh {
- // Remove existing preview plane if it exists
- remove_preview_plane(root)
-
- // Create new preview plane group
- const preview_plane_group = new Group()
- preview_plane_group.name = preview_plane_group_name
-
- // Store current plane info for future reference
- preview_plane_group.userData.height = height
- preview_plane_group.userData.size = size
-
- // Create plane geometry and material
- const geometry = new PlaneGeometry(size, size)
- const material = new MeshBasicMaterial({
- color: 0x00ff00, // Green color for visibility
- transparent: true,
- opacity: 0.3,
- side: DoubleSide, // Make it visible from both sides
- wireframe: false
- })
-
- // Create the plane mesh
- const plane_mesh = new Mesh(geometry, material)
- plane_mesh.name = 'preview_plane'
+export class PreviewPlaneManager {
+ private static instance: PreviewPlaneManager
+ private readonly preview_plane_group_name: string = 'preview_plane_group'
- // Position the plane at the specified height
- plane_mesh.position.set(0, height, 0)
-
- // Rotate the plane to be horizontal (lying flat)
- plane_mesh.rotation.x = -Math.PI / 2
+ // State tracking
+ private scene_ref: Scene | null = null
+ private plane_group: Group | null = null
+ private plane_mesh: Mesh | null = null
+ private current_height: number = 0.0
+ private current_size: number = 2.0
+ private is_visible: boolean = false
- // Add plane to the group and group to scene
- preview_plane_group.add(plane_mesh)
- root.add(preview_plane_group)
+ private constructor () { }
- return plane_mesh
-}
+ /**
+ * Get the singleton instance of PreviewPlaneManager
+ */
+ public static getInstance (): PreviewPlaneManager {
+ if (PreviewPlaneManager.instance === undefined) {
+ PreviewPlaneManager.instance = new PreviewPlaneManager()
+ }
+ return PreviewPlaneManager.instance
+ }
-/**
- * Update the height of the existing preview plane
- * @param root The main scene
- * @param height The new height position for the plane
- */
-export function update_preview_plane_height (root: Scene, height: number): void {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name) as Group | undefined
-
- if (preview_plane_group !== undefined) {
- const plane_mesh = preview_plane_group.getObjectByName('preview_plane') as Mesh
- if (plane_mesh !== undefined) {
- plane_mesh.position.y = height
- preview_plane_group.userData.height = height
+ /**
+ * Initialize the manager with a scene reference
+ * @param scene The main scene
+ */
+ public initialize (scene: Scene): void {
+ this.scene_ref = scene
+ }
+
+ /**
+ * Add a preview 3D plane to the scene at the origin
+ * @param height The height position of the plane (Y coordinate)
+ * @param size The size of the plane (width and depth)
+ * @returns The created plane mesh
+ */
+ public add_plane (height: number = 0.0, size: number = 5.0): Mesh {
+ if (this.scene_ref === null) {
+ throw new Error('PreviewPlaneManager not initialized with scene reference')
}
+
+ // Remove existing plane if it exists
+ this.remove_plane()
+
+ // Update state
+ this.current_height = height
+ this.current_size = size
+ this.is_visible = true
+
+ // Create new preview plane group
+ this.plane_group = new Group()
+ this.plane_group.name = this.preview_plane_group_name
+
+ // Create plane geometry and material
+ const geometry = new PlaneGeometry(size, size)
+ const material = new MeshBasicMaterial({
+ color: 0x00ff00, // Green color for visibility
+ transparent: true,
+ opacity: 0.7,
+ side: DoubleSide, // Make it visible from both sides
+ wireframe: false
+ })
+
+ // Create the plane mesh
+ this.plane_mesh = new Mesh(geometry, material)
+ this.plane_mesh.name = 'preview_plane'
+
+ // Position the plane at the specified height
+ this.plane_mesh.position.set(0, height, 0)
+
+ // Rotate the plane to be horizontal (lying flat)
+ this.plane_mesh.rotation.x = -Math.PI / 2
+
+ // Add plane to the group and group to scene
+ this.plane_group.add(this.plane_mesh)
+ this.scene_ref.add(this.plane_group)
+
+ return this.plane_mesh
}
-}
-/**
- * Update the size of the existing preview plane
- * @param root The main scene
- * @param size The new size for the plane
- */
-export function update_preview_plane_size (root: Scene, size: number): void {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name) as Group | undefined
-
- if (preview_plane_group !== undefined) {
- const plane_mesh = preview_plane_group.getObjectByName('preview_plane') as Mesh
- if (plane_mesh !== undefined) {
+ /**
+ * Update the height of the existing preview plane
+ * @param height The new height position for the plane
+ */
+ public update_height (height: number): void {
+ if (this.plane_mesh !== null && this.is_visible) {
+ this.plane_mesh.position.y = height
+ this.current_height = height
+ }
+ }
+
+ /**
+ * Update the size of the existing preview plane
+ * @param size The new size for the plane
+ */
+ public update_size (size: number): void {
+ if (this.plane_mesh !== null && this.is_visible) {
// Update the geometry with new size
const new_geometry = new PlaneGeometry(size, size)
- plane_mesh.geometry.dispose() // Clean up old geometry
- plane_mesh.geometry = new_geometry
- preview_plane_group.userData.size = size
+ this.plane_mesh.geometry.dispose() // Clean up old geometry
+ this.plane_mesh.geometry = new_geometry
+ this.current_size = size
}
}
-}
-/**
- * Get the current height of the preview plane
- * @param root The main scene
- * @returns The current height of the plane, or null if no plane exists
- */
-export function get_preview_plane_height (root: Scene): number | null {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name) as Group | undefined
-
- if (preview_plane_group !== undefined) {
- return preview_plane_group.userData.height ?? 0.0
+ /**
+ * Get the current height of the preview plane
+ * @returns The current height of the plane, or null if no plane exists
+ */
+ public get_height (): number | null {
+ return this.is_visible ? this.current_height : null
}
-
- return null
-}
-/**
- * Get the current size of the preview plane
- * @param root The main scene
- * @returns The current size of the plane, or null if no plane exists
- */
-export function get_preview_plane_size (root: Scene): number | null {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name) as Group | undefined
-
- if (preview_plane_group !== undefined) {
- return preview_plane_group.userData.size ?? 5.0
+ /**
+ * Get the current size of the preview plane
+ * @returns The current size of the plane, or null if no plane exists
+ */
+ public get_size (): number | null {
+ return this.is_visible ? this.current_size : null
}
-
- return null
-}
-/**
- * Check if preview plane exists in the scene
- * @param root The main scene
- * @returns True if preview plane exists, false otherwise
- */
-export function preview_plane_exists (root: Scene): boolean {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name)
- return preview_plane_group !== undefined
-}
+ /**
+ * Check if preview plane exists and is visible
+ * @returns True if preview plane exists and is visible, false otherwise
+ */
+ public is_plane_visible (): boolean {
+ return this.is_visible
+ }
-/**
- * Remove the preview plane from the scene
- * @param root The main scene
- */
-export function remove_preview_plane (root: Scene): void {
- const preview_plane_group = root.getObjectByName(preview_plane_group_name)
- if (preview_plane_group?.parent != null) {
- // Clean up geometry and material before removing
- const plane_mesh = preview_plane_group.getObjectByName('preview_plane') as Mesh
- if (plane_mesh !== undefined) {
- plane_mesh.geometry.dispose()
- if (plane_mesh.material instanceof MeshBasicMaterial) {
- plane_mesh.material.dispose()
+ /**
+ * Set the visibility of the preview plane
+ * @param visible Whether the plane should be visible
+ */
+ public set_visibility (visible: boolean): void {
+ if (visible && !this.is_visible) {
+ // Add the plane if it should be visible but isn't
+ this.add_plane(this.current_height, this.current_size)
+ } else if (!visible && this.is_visible) {
+ // Remove the plane if it should be hidden but is visible
+ this.remove_plane()
+ }
+ }
+
+ /**
+ * Remove the preview plane from the scene
+ */
+ public remove_plane (): void {
+ if (this.plane_group !== null && this.scene_ref !== null) {
+ // Clean up geometry and material before removing
+ if (this.plane_mesh !== null) {
+ this.plane_mesh.geometry.dispose()
+ if (this.plane_mesh.material instanceof MeshBasicMaterial) {
+ this.plane_mesh.material.dispose()
+ }
}
+
+ this.scene_ref.remove(this.plane_group)
+ this.plane_group = null
+ this.plane_mesh = null
+ this.is_visible = false
}
-
- preview_plane_group.parent.remove(preview_plane_group)
+ }
+
+ /**
+ * Clean up all resources and reset state
+ */
+ public cleanup (): void {
+ this.remove_plane()
+ this.scene_ref = null
+ this.current_height = 0.0
+ this.current_size = 5.0
+ this.is_visible = false
}
}
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index e055820..16e7e3d 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -2,7 +2,7 @@ import { UI } from '../../UI.ts'
import { Generators } from '../../Generators.ts'
import { Utility } from '../../Utilities.ts'
import { UndoRedoSystem } from './UndoRedoSystem.ts'
-import * as PreviewPlaneManager from './PreviewPlaneManager.ts'
+import { PreviewPlaneManager } from './PreviewPlaneManager.ts'
import {
Vector3,
Euler,
@@ -52,6 +52,7 @@ export class StepEditSkeleton extends EventTarget {
private readonly joint_texture = new TextureLoader().load('images/skeleton-joint-point.png')
private _added_event_listeners: boolean = false
+ private readonly preview_plane_manager: PreviewPlaneManager = PreviewPlaneManager.getInstance()
constructor () {
super()
@@ -98,6 +99,8 @@ export class StepEditSkeleton extends EventTarget {
public setup_scene (main_scene: Scene): void {
// add the skeleton to the scene
this._main_scene_ref = main_scene
+ // Initialize the preview plane manager with the scene
+ this.preview_plane_manager.initialize(main_scene)
}
public begin (): void {
@@ -185,16 +188,7 @@ export class StepEditSkeleton extends EventTarget {
*/
public set_preview_plane_visible (visible: boolean): void {
this.preview_plane_visible = visible
-
- if (this._main_scene_ref === null) {
- return
- }
-
- if (visible) {
- PreviewPlaneManager.add_preview_plane(this._main_scene_ref, this.preview_plane_height)
- } else {
- PreviewPlaneManager.remove_preview_plane(this._main_scene_ref)
- }
+ this.preview_plane_manager.set_visibility(visible)
}
/**
@@ -210,10 +204,7 @@ export class StepEditSkeleton extends EventTarget {
*/
public set_preview_plane_height (height: number): void {
this.preview_plane_height = height
-
- if (this._main_scene_ref !== null && this.preview_plane_visible) {
- PreviewPlaneManager.update_preview_plane_height(this._main_scene_ref, height)
- }
+ this.preview_plane_manager.update_height(height)
}
/**
@@ -373,10 +364,8 @@ export class StepEditSkeleton extends EventTarget {
* Remove the preview plane from the scene
*/
private remove_preview_plane (): void {
- if (this._main_scene_ref !== null) {
- PreviewPlaneManager.remove_preview_plane(this._main_scene_ref)
- this.preview_plane_visible = false
- }
+ this.preview_plane_manager.cleanup()
+ this.preview_plane_visible = false
}
/*