Import a 3D Model and automatically assign and export animations
TypeScript
Simplify modal dialog a bit
Scott Petrovica0bf9508dc61
diff --git a/src/credits.css b/src/credits.cssdeleted file mode 100644index 801e535..0000000--- a/src/credits.css+++ /dev/null@@ -1,28 +0,0 @@-#credits-dialog {- display:none;- position: fixed;- top: 0;- left: 0;- width: 100vw;- height: 100vh;- background: rgba(0,0,0,0.5); /* grey out bg, so don't use theme colors */- z-index: 10000;- align-items: center;- justify-content: center;-}--#credits-contents {- background: var(--bg-primary);- color: var(--text-primary);- padding: 2rem;- border-radius: 8px;- max-width: 90vw;- max-height: 80vh;- overflow-y: auto;- position: relative;- box-shadow: 0 4px 32px #000a;-}--button#close-credits-dialog {--}\ No newline at end of filediff --git a/src/dialog.css b/src/dialog.cssnew file mode 100644index 0000000..e642ab7--- /dev/null+++ b/src/dialog.css@@ -0,0 +1,35 @@+.modal-dialog-overlay {+ position: fixed;+ top: 0; left: 0; right: 0; bottom: 0;+ background: rgba(0,0,0,0.4);+ display: flex;+ align-items: center;+ justify-content: center;+ z-index: 9999;+}++.modal-dialog-content {+ background: var(--bg-primary);+ padding: 2em;+ border-radius: 8px;+ box-shadow: 0 2px 16px rgba(0,0,0,0.2);+ max-width: 400px;+ text-align: center;+}++.modal-dialog-content h2 {+ margin-top: 0;+ color: var(--text-primary);+ font-size: 140%;+ display: block;+}++.modal-dialog-content p {+ color: var(--text-primary);+}++.modal-dialog-close {+ margin-top: 1em;+ padding: 0.5em 1.5em;+ font-size: 1em;+}\ No newline at end of filediff --git a/src/index.html b/src/index.htmlindex 902df69..9f3944c 100644--- a/src/index.html+++ b/src/index.html@@ -12,7 +12,7 @@<link href="./styles.css" rel="stylesheet" crossorigin="anonymous" /><link rel="stylesheet" href="./radio-button-group.css" crossorigin="anonymous" /><link rel="stylesheet" href="./animation-player.css" crossorigin="anonymous" />- <link rel="stylesheet" href="./credits.css" crossorigin="anonymous" />+ <link rel="stylesheet" href="./dialog.css" crossorigin="anonymous" /><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />@@ -49,8 +49,8 @@<div id="tool-selection-group"><div style="margin-bottom: 1rem; padding: 0;">- <div id="current-step-index"></div>- <div id="current-step-label"></div>+ <span id="current-step-index"></span>+ <span id="current-step-label"></span></div><div id="load-model-tools">@@ -258,20 +258,8 @@<!-- javascript will populate this item with animationsthat we get from the animations file(s) -->- <div id="animations-items"> </div>-- <!-- Attribution. clicking will open dialog with attribution information -->- <a href="#" id="attribution-link">Attribution</a>- <div id="attribution-message" style="display: none;"></div>- <div id="credits-dialog">- <div id="credits-contents" >- <div style="display: flex; align-items: center; gap: 2rem;">- <h2 style="margin:0;">Attribution for skeleton & animations</h2>- <button id="close-credits-dialog" >×</button>- </div>- <div id="credits-content"></div>- </div>- </div>+ <div id="animations-items"> </div>+<!-- slider to control arm extension (hide this feature until the base functionality is more stable) --><div id="a-pose-correction-options">@@ -355,6 +343,8 @@<!-- 3d view control hit box. It is just a container to help with 3d view controls. --><div id="view-control-hitbox"></div>-+ <!-- Attribution. clicking will open dialog with attribution information -->+ <a href="#" id="attribution-link">Contributors</a>+</body></html>\ No newline at end of filediff --git a/src/lib/CreditsDialog.ts b/src/lib/CreditsDialog.tsdeleted file mode 100644index 76cf08c..0000000--- a/src/lib/CreditsDialog.ts+++ /dev/null@@ -1,81 +0,0 @@-// Attribution/Credits Dialog Logic as a TypeScript class-export interface CreditEntry {- name: string- url: string-}--export class CreditsDialog {- private readonly attribution_link: HTMLElement | null- private readonly credits_dialog: HTMLElement | null- private readonly close_button: HTMLElement | null- private readonly credits_content: HTMLElement | null- private readonly skeleton_credits: Record<string, CreditEntry[]>-- constructor () {- this.attribution_link = document.getElementById('attribution-link')- this.credits_dialog = document.getElementById('credits-dialog')- this.close_button = document.getElementById('close-credits-dialog')- this.credits_content = document.getElementById('credits-content')-- this.skeleton_credits = {- human: [- { name: 'Quaternius', url: 'https://quaternius.com/' },- { name: 'Scott Petrovic', url: 'https://scottpetrovic.com' }- ],- fox: [- { name: 'Scott Petrovic', url: 'https://scottpetrovic.com' }- ],- bird: [- { name: 'Scott Petrovic', url: 'https://scottpetrovic.com' }- ]- }-- this.addEventListeners()- }-- private get_current_skeleton_type (): string {- const sel = document.getElementById('skeleton-selection') as HTMLSelectElement | null- if (sel && sel.value) {- return sel.value- }- return 'human'- }-- public show (): void {- if ((this.credits_dialog == null) || (this.credits_content == null)) return-- const skeleton_type = this.get_current_skeleton_type()- const credits = this.skeleton_credits[skeleton_type] || []- this.credits_dialog.style.display = 'flex'-- if (credits.length === 0) {- this.credits_content.innerHTML = '<p>No credits available for this skeleton type.</p>'- return- }-- this.credits_content.innerHTML = '<ul style="padding-left: 1.2em;">' +- credits.map(c => `<li><a href="${c.url}" target="_blank" rel="noopener">${c.name}</a></li>`)- .join('') + '</ul>'- }-- public hide (): void {- if (this.credits_dialog != null) this.credits_dialog.style.display = 'none'- }-- private addEventListeners (): void {- this.attribution_link?.addEventListener('click', (e) => {- e.preventDefault()- this.show()- })-- this.close_button?.addEventListener('click', () => {- this.hide()- })-- this.credits_dialog?.addEventListener('click', (e) => {- if (e.target === this.credits_dialog) {- this.hide()- }- })- }-}diff --git a/src/lib/EventListeners.ts b/src/lib/EventListeners.tsindex 462fee6..6a1e870 100644--- a/src/lib/EventListeners.ts+++ b/src/lib/EventListeners.ts@@ -25,6 +25,11 @@ export class EventListeners {}})+ // attribution link clicking brings up contributors dialog+ this.bootstrap.ui.dom_attribution_link?.addEventListener('click', (event: MouseEvent) => {+ event.preventDefault()+ this.bootstrap.show_contributors_dialog()+ })// listen for view helper changesdocument.getElementById('view-control-hitbox')?.addEventListener('pointerdown', (event: PointerEvent) => {diff --git a/src/lib/ModalDialog.ts b/src/lib/ModalDialog.tsnew file mode 100644index 0000000..75e3d49--- /dev/null+++ b/src/lib/ModalDialog.ts@@ -0,0 +1,40 @@+// Generic modal dialog for displaying any content (credits, errors, etc.)+export class ModalDialog {+ private dialog_element: HTMLDivElement | null = null+ private close_button: HTMLButtonElement | null = null++ constructor (+ private readonly title: string,+ private readonly content_html: string,+ private readonly options?: { onClose?: () => void, customClass?: string }+ ) {}++ public show (): void {+ this.remove()+ this.dialog_element = document.createElement('div')+ this.dialog_element.className = 'modal-dialog-overlay' + (this.options?.customClass ? ' ' + this.options.customClass : '')++ // HTML template for the content+ this.dialog_element.innerHTML = `+ <div class="modal-dialog-content">+ <h2>${this.title}</h2>+ <div class="modal-dialog-body">${this.content_html}</div>+ <button class="modal-dialog-close">Close</button>+ </div>+ `+ document.body.appendChild(this.dialog_element)+ this.close_button = this.dialog_element.querySelector('.modal-dialog-close')+ this.close_button?.addEventListener('click', () => { this.remove() })+ this.dialog_element.addEventListener('click', (e) => {+ if (e.target === this.dialog_element) this.remove()+ })+ }++ public remove (): void {+ if (this.dialog_element && this.dialog_element.parentNode) {+ this.dialog_element.parentNode.removeChild(this.dialog_element)+ this.dialog_element = null+ if (this.options?.onClose) this.options.onClose()+ }+ }+}diff --git a/src/lib/UI.ts b/src/lib/UI.tsindex 8d2cce6..286c311 100644--- a/src/lib/UI.ts+++ b/src/lib/UI.ts@@ -65,6 +65,7 @@ export class UI {dom_build_version: HTMLElement | null = null+ dom_attribution_link: HTMLAnchorElement | null = nullprivate constructor () {this.initialize_dom_elements()@@ -145,6 +146,8 @@ export class UI {this.dom_build_version = document.querySelector('#build-version')+ this.dom_attribution_link = document.querySelector('#attribution-link')+// UI for exporting the animationthis.dom_export_button_hidden_link = document.querySelector('#download-hidden-link')this.dom_animation_count = document.querySelector('#animation-selection-count')diff --git a/src/lib/processes/load-model/StepLoadModel.ts b/src/lib/processes/load-model/StepLoadModel.tsindex 0a718d8..3839e89 100644--- a/src/lib/processes/load-model/StepLoadModel.ts+++ b/src/lib/processes/load-model/StepLoadModel.ts@@ -1,3 +1,4 @@+import { ZipLoadErrorDialog } from './ZipLoadErrorDialog.ts'import { UI } from '../../UI.ts'import { ZipGLTFLoader } from './ZipGLTFLoader.ts'import { Box3 } from 'three/src/math/Box3.js'@@ -179,7 +180,11 @@ export class StepLoadModel extends EventTarget {this.process_loaded_scene(scene) // loaded successfully...continue}, (err) => {console.error('Failed to load GLTF from ZIP:', err)- }).catch((err) => { console.error('Error loading ZIP:', err) })+ new ZipLoadErrorDialog('Failed to load GLTF from ZIP: ' + (err?.message || err))+ }).catch((err) => {+ console.error('Error loading ZIP:', err)+ new ZipLoadErrorDialog('Error loading ZIP: ' + (err?.message || err))+ })}// support both data URLs@@ -187,9 +192,16 @@ export class StepLoadModel extends EventTarget {fetch(model_file_path).then(async res => await res.arrayBuffer()).then(buffer => { handle_zip(buffer) })- .catch(err => { console.error('Failed to fetch ZIP data:', err) })+ .catch(err => {+ console.error('Failed to fetch ZIP data:', err)+ new ZipLoadErrorDialog('Failed to fetch ZIP data: ' + (err?.message || err))+ })+ } else if (model_file_path instanceof ArrayBuffer) {+ handle_zip(model_file_path)} else {- console.error('ZIP file data is not in a supported format')+ const msg = 'ZIP file data is not in a supported format';+ console.error(msg)+ new ZipLoadErrorDialog(msg)}}diff --git a/src/lib/processes/load-model/ZipGLTFLoader.ts b/src/lib/processes/load-model/ZipGLTFLoader.tsindex 131c2f4..a014683 100644--- a/src/lib/processes/load-model/ZipGLTFLoader.ts+++ b/src/lib/processes/load-model/ZipGLTFLoader.ts@@ -1,4 +1,5 @@import JSZip from 'jszip'+import { EventDispatcher } from 'three'import { type GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'import { type Scene } from 'three/src/scenes/Scene.js'@@ -6,17 +7,18 @@ import { type Scene } from 'three/src/scenes/Scene.js'* Loads a GLTF (with BIN and textures) from a ZIP file buffer, using in-memory URLs for all assets.* Usage: new ZipGLTFLoader(gltfLoaderInstance).loadFromZip(zipBuffer, (scene) => { ... })*/-export class ZipGLTFLoader {+export class ZipGLTFLoader extends EventDispatcher {private readonly loader: GLTFLoaderconstructor (loader: GLTFLoader) {+ super()this.loader = loader}/*** Main entry: Loads a GLTF (with BIN and textures) from a ZIP file buffer, using in-memory URLs for all assets.*/- async load_from_zip (zipData: ArrayBuffer,+ public async load_from_zip (zipData: ArrayBuffer,onLoad: (scene: Scene) => void,onError?: (err: any) => void): Promise<void> {@@ -135,7 +137,7 @@ export class ZipGLTFLoader {buffer = file_map[decoded] || file_map[decoded.toLowerCase()]} catch {}}-+if (buffer) {// Guess MIME typelet mime = 'application/octet-stream'diff --git a/src/lib/processes/load-model/ZipLoadErrorDialog.ts b/src/lib/processes/load-model/ZipLoadErrorDialog.tsnew file mode 100644index 0000000..0e335c0--- /dev/null+++ b/src/lib/processes/load-model/ZipLoadErrorDialog.ts@@ -0,0 +1,13 @@++import { ModalDialog } from '../../ModalDialog.ts'+++export class ZipLoadErrorDialog {+ private modal: ModalDialog | null = null;++ constructor(message: string) {+ this.modal = new ModalDialog('ZIP Load Error', `<p>${message}</p>`)+ this.modal.show()+ }+}+diff --git a/src/script.ts b/src/script.tsindex 01994fb..a20d65e 100644--- a/src/script.ts+++ b/src/script.ts@@ -1,4 +1,3 @@-import { CreditsDialog } from './lib/CreditsDialog.ts';import * as THREE from 'three'import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'@@ -28,6 +27,7 @@ import { EventListeners } from './lib/EventListeners.ts'import { ModelPreviewDisplay } from './lib/enums/ModelPreviewDisplay.ts'import { TransformControlType } from './lib/enums/TransformControlType.ts'import { ThemeManager } from './lib/ThemeManager.ts'+import { ModalDialog } from './lib/ModalDialog.ts'export class Bootstrap {public readonly camera = Generators.create_camera()@@ -88,9 +88,6 @@ export class Bootstrap {this.process_step = this.process_step_changed(ProcessStep.LoadModel)this.animate() // start the render loop which will continue rendering the scenethis.inject_build_version()-- // Create the credits dialog- const creds_dialog: CreditsDialog = new CreditsDialog()}private inject_build_version (): void {@@ -516,6 +513,23 @@ export class Bootstrap {return true}++ public show_contributors_dialog (): void {+ // Show the contributors dialog+ console.log('Showing contributors dialog')+ // instantiate modal dialog and show contributors+ const modal = new ModalDialog('Contributors', this.get_contributors_list())+ modal.show()+ }++ private get_contributors_list (): string {+ // Return a list of contributors+ return `+ <p>Contributor 1</p>+ <p>Contributor 2</p>+ <p>Contributor 3</p>+ `+ }} // end Bootstrap class// Create an instance of the Bootstrap class when the script is loadeddiff --git a/src/styles.css b/src/styles.cssindex b08ef4c..0b3ab8b 100644--- a/src/styles.css+++ b/src/styles.css@@ -60,6 +60,10 @@ h1, h2, h3, h4, h5, h6, input, button, select {display: inline-block;}+div, p {+ display: block;+}+a {color: var(--link-color);font-size: 80%;@@ -583,4 +587,16 @@ position: fixed;padding: 0.5rem;font-style: italic;font-size: 90%;+}++#attribution-link {+ color: var(--text-primary);+ text-decoration: underline;+ position: absolute;+ top: 4.1rem;+ left: 6em;+ font-size: 90%;+}+#attribution-link:hover {+ text-decoration: none;}\ No newline at end of file