Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Start creating a preview plane manager to position head separation

Scott Petrovicb4b661e82446
diff --git a/src/create.html b/src/create.html
index 254f968..e6b01d5 100644
--- a/src/create.html
+++ b/src/create.html
@@ -262,6 +262,22 @@
<label for="mirror-skeleton">Mirror Left/Right Joints</label>
</div>
+ <hr />
+
+ <!-- Preview plane controls -->
+ <div style="display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1rem;">
+ <div class="styled-checkbox">
+ <input type="checkbox" id="preview-plane-checkbox" />
+ <label for="preview-plane-checkbox">Show Preview Plane</label>
+ </div>
+
+ <div style="display: flex; align-items: center; gap: 0.5rem;">
+ <label for="preview-plane-height-input">Plane Height:</label>
+ <input type="range" id="preview-plane-height-input" min="0.20" max="2.00" step="0.01" value="0.50" />
+ <span id="preview-plane-height-label">0.50</span>
+ </div>
+ </div>
+
<hr />
<!-- add number input for changing scale of skeleton-->
diff --git a/src/lib/UI.ts b/src/lib/UI.ts
index 87f2513..83fc073 100644
--- a/src/lib/UI.ts
+++ b/src/lib/UI.ts
@@ -38,6 +38,11 @@ export class UI {
dom_transform_type_radio_group: HTMLElement | null = null
dom_transform_space_radio_group: HTMLElement | null = null
+ // preview plane controls
+ dom_preview_plane_checkbox: HTMLInputElement | null = null
+ dom_preview_plane_height_input: HTMLInputElement | null = null
+ dom_preview_plane_height_label: HTMLElement | null = null
+
dom_skinning_algorithm_selection: HTMLElement | null = null
dom_skinned_mesh_tools: HTMLElement | null = null
dom_skinned_mesh_animation_tools: HTMLElement | null = null
@@ -69,7 +74,6 @@ export class UI {
dom_animation_count: HTMLElement | null = null
dom_animations_listing_count: HTMLElement | null = null
-
dom_build_version: HTMLElement | null = null
dom_attribution_link: HTMLAnchorElement | null = null
@@ -118,6 +122,11 @@ export class UI {
this.dom_transform_type_radio_group = document.querySelector('#transform-control-type-group')
this.dom_transform_space_radio_group = document.querySelector('#transform-space-group')
+ // preview plane controls
+ this.dom_preview_plane_checkbox = document.querySelector('#preview-plane-checkbox')
+ this.dom_preview_plane_height_input = document.querySelector('#preview-plane-height-input')
+ this.dom_preview_plane_height_label = document.querySelector('#preview-plane-height-label')
+
this.dom_bind_pose_button = document.querySelector('#action_bind_pose')
// this.dom_scale_skeleton_input_box = document.querySelector('#scale-input')
this.dom_move_to_origin_button = document.querySelector('#action_move_to_origin')
diff --git a/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts b/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts
new file mode 100644
index 0000000..e53dfb8
--- /dev/null
+++ b/src/lib/processes/edit-skeleton/PreviewPlaneManager.ts
@@ -0,0 +1,151 @@
+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
+ * 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'
+
+ // 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
+
+ // Add plane to the group and group to scene
+ preview_plane_group.add(plane_mesh)
+ root.add(preview_plane_group)
+
+ return plane_mesh
+}
+
+/**
+ * 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
+ }
+ }
+}
+
+/**
+ * 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 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
+ }
+ }
+}
+
+/**
+ * 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
+ }
+
+ 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
+ }
+
+ 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
+}
+
+/**
+ * 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()
+ }
+ }
+
+ preview_plane_group.parent.remove(preview_plane_group)
+ }
+}
diff --git a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
index 4425cf7..e055820 100644
--- a/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
+++ b/src/lib/processes/edit-skeleton/StepEditSkeleton.ts
@@ -2,8 +2,21 @@ import { UI } from '../../UI.ts'
import { Generators } from '../../Generators.ts'
import { Utility } from '../../Utilities.ts'
import { UndoRedoSystem } from './UndoRedoSystem.ts'
-import { Vector3, Euler, Object3D, Skeleton, type Scene, type Bone, BufferGeometry,
- PointsMaterial, Points, Float32BufferAttribute, TextureLoader, type Camera } from 'three'
+import * as PreviewPlaneManager from './PreviewPlaneManager.ts'
+import {
+ Vector3,
+ Euler,
+ Object3D,
+ Skeleton,
+ type Scene,
+ type Bone,
+ BufferGeometry,
+ PointsMaterial,
+ Points,
+ Float32BufferAttribute,
+ TextureLoader,
+ type Camera
+} from 'three'
import { SkinningFormula } from '../../enums/SkinningFormula.ts'
/*
@@ -32,6 +45,10 @@ export class StepEditSkeleton extends EventTarget {
private joint_hover_point: Object3D | null = null
private _main_scene_ref: Scene | null = null
+ // Preview plane state
+ private preview_plane_visible: boolean = false
+ private preview_plane_height: number = 0.0
+
private readonly joint_texture = new TextureLoader().load('images/skeleton-joint-point.png')
private _added_event_listeners: boolean = false
@@ -162,6 +179,50 @@ export class StepEditSkeleton extends EventTarget {
return this.skinning_algorithm
}
+ /**
+ * Toggle the visibility of the preview plane
+ * @param visible Whether the plane should be visible
+ */
+ 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)
+ }
+ }
+
+ /**
+ * Get the current visibility state of the preview plane
+ */
+ public is_preview_plane_visible (): boolean {
+ return this.preview_plane_visible
+ }
+
+ /**
+ * Set the height of the preview plane
+ * @param height The Y coordinate height for the plane
+ */
+ 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)
+ }
+ }
+
+ /**
+ * Get the current height of the preview plane
+ */
+ public get_preview_plane_height (): number {
+ return this.preview_plane_height
+ }
+
public add_event_listeners (): void {
if (this.ui.dom_move_to_origin_button !== null) {
this.ui.dom_move_to_origin_button.addEventListener('click', () => {
@@ -215,6 +276,24 @@ export class StepEditSkeleton extends EventTarget {
this.undo_redo_system.addEventListener('undoRedoStateChanged', (event: any) => {
this.update_undo_redo_button_states(event.detail.canUndo, event.detail.canRedo)
})
+
+ // Add preview plane event listeners
+ this.ui.dom_preview_plane_checkbox?.addEventListener('change', (event) => {
+ const target = event.target as HTMLInputElement
+ this.set_preview_plane_visible(target.checked)
+ })
+
+ this.ui.dom_preview_plane_height_input?.addEventListener('input', (event) => {
+ const target = event.target as HTMLInputElement
+ const height = parseFloat(target.value)
+ const final_height = isNaN(height) ? 0.0 : height
+ this.set_preview_plane_height(final_height)
+
+ // Update the label to show current value
+ if (this.ui.dom_preview_plane_height_label !== null) {
+ this.ui.dom_preview_plane_height_label.textContent = final_height.toFixed(1)
+ }
+ })
}
// returning back to edit skeleton step later will call this to reset undo state
@@ -273,11 +352,31 @@ export class StepEditSkeleton extends EventTarget {
if (this.ui.dom_redo_button !== null) {
this.ui.dom_redo_button.removeEventListener('click', () => {})
}
+
+ // Remove preview plane event listeners
+ if (this.ui.dom_preview_plane_checkbox !== null) {
+ this.ui.dom_preview_plane_checkbox.removeEventListener('change', () => {})
+ }
+
+ if (this.ui.dom_preview_plane_height_input !== null) {
+ this.ui.dom_preview_plane_height_input.removeEventListener('input', () => {})
+ }
}
public cleanup_on_exit_step (): void {
this.remove_event_listeners()
this.clear_hover_point_if_exists()
+ this.remove_preview_plane()
+ }
+
+ /**
+ * 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
+ }
}
/*
@@ -428,6 +527,4 @@ export class StepEditSkeleton extends EventTarget {
this.joint_hover_point = null
}
}
-
-
}