From d21ee95173bfdce5c1002f27b269fd92da5cc7cd Mon Sep 17 00:00:00 2001 From: zhangxin <59186205+chichinohaha@users.noreply.github.com> Date: Tue, 20 Apr 2021 17:59:24 +0800 Subject: [PATCH] Preview the animation in fbx's inspector (#8503) * Preview the animation in fbx's inspector * replaceChinese --- editor/inspector/assets/fbx/animation.js | 161 +++++++++++----- editor/inspector/assets/fbx/index.js | 19 +- editor/inspector/assets/fbx/preview.js | 228 ++++++++++++++++++++--- 3 files changed, 320 insertions(+), 88 deletions(-) diff --git a/editor/inspector/assets/fbx/animation.js b/editor/inspector/assets/fbx/animation.js index 1ed0c7317d..36f1e2c3fb 100644 --- a/editor/inspector/assets/fbx/animation.js +++ b/editor/inspector/assets/fbx/animation.js @@ -1,5 +1,3 @@ -'use strict'; - exports.template = `
@@ -303,7 +301,7 @@ exports.$ = { */ const Elements = { showType: { - ready() { + ready () { const panel = this; panel.animationTimeShowType = panel.$.showType.value === 0 ? 'time' : 'frame'; panel.$.showType.addEventListener('change', (event) => { @@ -311,20 +309,20 @@ const Elements = { Elements.clips.update.call(panel); }); }, - update() { + update () { const panel = this; panel.animationTimeShowType = panel.$.showType.value === 0 ? 'time' : 'frame'; }, }, infos: { - ready() { + ready () { const panel = this; Object.assign(panel, { animationInfos: null, }); }, - update() { + update () { const panel = this; if (panel.meta && panel.meta.userData.animationImportSettings) { @@ -343,7 +341,7 @@ const Elements = { }, }, clips: { - ready() { + ready () { const panel = this; Object.assign(panel, { @@ -352,7 +350,7 @@ const Elements = { currentClipInfo: null, }); }, - update() { + update () { const panel = this; panel.$.clips.innerText = ''; @@ -403,20 +401,10 @@ const Elements = { line.setAttribute('active', true); } table.appendChild(line); - + line.setAttribute('rawCLipIndex', rawClipIndex); + line.setAttribute('splitClipIndex', splitClipIndex); line.addEventListener('click', () => { - panel.rawClipIndex = rawClipIndex; - panel.splitClipIndex = splitClipIndex; - - Elements.editor.update.call(panel); - - panel.$.clips.querySelectorAll('.line').forEach((child) => { - if (child === line) { - child.setAttribute('active', true); - } else { - child.removeAttribute('active'); - } - }); + panel.onSelect(rawClipIndex, splitClipIndex); }); const name = document.createElement('div'); @@ -475,7 +463,7 @@ const Elements = { }, }, editor: { - ready() { + ready () { const panel = this; Object.assign(panel, { @@ -502,7 +490,7 @@ const Elements = { panel.onWrapModeChangeBind = panel.onWrapModeChange.bind(panel); panel.$.wrapMode.addEventListener('confirm', panel.onWrapModeChangeBind); - function observer() { + function observer () { const rect = panel.$.editor.getBoundingClientRect(); panel.gridTableWith = rect.width - 60; @@ -518,7 +506,7 @@ const Elements = { panel.resizeObserver.observe(panel.$.editor); observer(); }, - close() { + close () { const panel = this; panel.resizeObserver.unobserve(panel.$.editor); @@ -534,7 +522,7 @@ const Elements = { panel.$.wrapMode.removeEventListener('confirm', panel.onWrapModeChangeBind); }, - update() { + update () { const panel = this; panel.updateRawClipInfo(); @@ -618,6 +606,35 @@ exports.update = function (assetList, metaList) { element.update.call(this); } } + // animation name -> uuid + if (this.meta && this.meta.subMetas) { + const animationNameToUUIDMap = new Map(); + Object.keys(this.meta.subMetas).forEach((id) => { + const subMeta = this.meta.subMetas[id]; + if (subMeta.importer === 'gltf-animation') { + const sourceName = subMeta.name; + const animName = sourceName.slice(0, sourceName.lastIndexOf('.')); + animationNameToUUIDMap.set(animName, subMeta.uuid); + } + }); + + this.animationNameToUUIDMap = animationNameToUUIDMap; + } + + if (this.meta && this.meta.userData.animationImportSettings) { + this.animationInfos = this.meta.userData.animationImportSettings; + // Collect clip names for renaming and creating to determine whether the name is repeated + this.clipNames = new Set(); + for (const animationInfo of this.animationInfos) { + this.clipNames.add(animationInfo.name); + for (const subAnimInfo of animationInfo.splits) { + this.clipNames.add(subAnimInfo.name); + } + } + this.onSelect(0, 0); + } else { + this.animationInfos = null; + } }; exports.ready = function () { @@ -639,7 +656,57 @@ exports.close = function () { }; exports.methods = { - getRightName(name) { + onSelect (rawClipIndex, splitClipIndex) { + this.rawClipIndex = rawClipIndex; + this.splitClipIndex = splitClipIndex; + const isElementSelect = (element) => element.getAttribute('rawClipIndex') == rawClipIndex && element.getAttribute('splitClipIndex') == splitClipIndex; + Elements.editor.update.call(this); + + this.$.clips.querySelectorAll('.line').forEach((child) => { + if (isElementSelect(child)) { + child.setAttribute('active', true); + } else { + child.removeAttribute('active'); + } + }); + const curClipInfo = this.getCurClipInfo(); + Editor.Message.broadcast('fbx-inspector:animation-change', curClipInfo); + }, + getCurClipInfo () { + const animInfo = this.animationInfos[this.rawClipIndex]; + const splitInfo = animInfo.splits[this.splitClipIndex]; + + if (!animInfo) { + return; + } + + const clipUUID = this.animationNameToUUIDMap.get(animInfo.name); + let duration = animInfo.duration; + let fps = animInfo.fps; + let from = 0; + let to = duration; + if (splitInfo) { + from = splitInfo.from; + to = splitInfo.to; + duration = to - from; + if (splitInfo.fps !== undefined) { + fps = splitInfo.fps; + } + + // if (this.animationNameToUUIDMap.has(splitInfo.name)) { + // clipUUID = this.animationNameToUUIDMap.get(splitInfo.name); + // } + } + + return { + clipUUID, + duration, + fps, + from, + to, + }; + }, + getRightName (name) { if (!name) { return null; } @@ -647,14 +714,14 @@ exports.methods = { do { const result = name.match(/(.*)_(\d{0,3})/); if (result) { - name = result[1] + '_' + (Number(result[2]) + 1); + name = `${result[1]}_${Number(result[2]) + 1}`; } else { - name = name + '_1'; + name += '_1'; } } while (panel.clipNames.has(name)); return name; }, - newClipTemplate() { + newClipTemplate () { const panel = this; // Verify the name return { @@ -664,7 +731,7 @@ exports.methods = { wrapMode: 2 /* Loop */, }; }, - updateCurrentClipInfo() { + updateCurrentClipInfo () { const panel = this; if (!panel.animationInfos) { panel.currentClipInfo = null; @@ -721,7 +788,7 @@ exports.methods = { panel.$.wrapMode.value = panel.currentClipInfo.wrapMode; }, - updateRawClipInfo() { + updateRawClipInfo () { const panel = this; if (!panel.animationInfos) { panel.rawClipInfo = null; @@ -737,7 +804,7 @@ exports.methods = { panel.$.clipDuration.innerText = duration.toFixed(2); }, - updateGridConfig() { + updateGridConfig () { const panel = this; if (!panel.currentClipInfo) { @@ -762,7 +829,7 @@ exports.methods = { labelStep, }; }, - getStepAndSpacing(width, frames) { + getStepAndSpacing (width, frames) { const config = { minSpacing: 10, maxSpacing: 20, @@ -782,7 +849,7 @@ exports.methods = { spacing, }; }, - onMouseDown(type) { + onMouseDown (type) { const panel = this; const info = panel.currentClipInfo; @@ -811,7 +878,7 @@ exports.methods = { document.addEventListener('mousemove', panel.onMouseMoveBind); document.addEventListener('mouseup', panel.onMouseUpBind); }, - onMouseMove(event) { + onMouseMove (event) { const panel = this; event.preventDefault(); @@ -824,10 +891,10 @@ exports.methods = { const { type } = panel.virtualControl; let x = event.x - panel.$.rulerMaking.getBoundingClientRect().x; if ( - x > panel.gridConfig.width || - x < 0 || - (type === 'left' && x > panel.currentClipInfo.ctrlEnd) || - (type === 'right' && x < panel.currentClipInfo.ctrlStart) + x > panel.gridConfig.width + || x < 0 + || (type === 'left' && x > panel.currentClipInfo.ctrlEnd) + || (type === 'right' && x < panel.currentClipInfo.ctrlStart) ) { return; } @@ -838,7 +905,7 @@ exports.methods = { panel.virtualControl.value = currentTime; if (type === 'left') { panel.virtualControl.startFrame = currentFrame; - x = x - 6; + x -= 6; } else { panel.virtualControl.endFrame = currentFrame; } @@ -852,7 +919,7 @@ exports.methods = { panel.dispatch('change'); }); }, - onMouseUp() { + onMouseUp () { const panel = this; if (!panel.virtualControl) { @@ -879,8 +946,10 @@ exports.methods = { Object.assign(panel.$.controlRight.style, panel.currentClipInfo.ctrlEndStyle); panel.$.controlVirtual.style.display = 'none'; + const curClipInfo = panel.getCurClipInfo(); + Editor.Message.broadcast('fbx-inspector:animation-change', curClipInfo); }, - updateVirtualControl() { + updateVirtualControl () { const panel = this; Object.assign(panel.$.controlVirtual.style, panel.virtualControl.style); @@ -894,7 +963,7 @@ exports.methods = { } } }, - onClipName(event) { + onClipName (event) { const panel = this; if (!panel.currentClipInfo) { @@ -919,7 +988,7 @@ exports.methods = { panel.dispatch('change'); Elements.clips.update.call(panel); }, - onCutClip(event) { + onCutClip (event) { const panel = this; const path = event.target.getAttribute('path'); @@ -930,7 +999,7 @@ exports.methods = { panel.dispatch('change'); }, - onFpsChange(event) { + onFpsChange (event) { const panel = this; panel.animationInfos[panel.rawClipIndex].splits[panel.splitClipIndex].fps = Number(event.target.value); @@ -938,7 +1007,7 @@ exports.methods = { Elements.editor.update.call(panel); panel.dispatch('change'); }, - onWrapModeChange(event) { + onWrapModeChange (event) { const panel = this; panel.animationInfos[panel.rawClipIndex].splits[panel.splitClipIndex].wrapMode = Number(event.target.value); diff --git a/editor/inspector/assets/fbx/index.js b/editor/inspector/assets/fbx/index.js index 2de994814d..70648dff98 100644 --- a/editor/inspector/assets/fbx/index.js +++ b/editor/inspector/assets/fbx/index.js @@ -1,5 +1,4 @@ 'use strict'; - const path = require('path'); exports.template = ` @@ -47,7 +46,7 @@ const Components = { */ const Elements = { tabs: { - ready() { + ready () { const panel = this; panel.$.tabs.addEventListener('change', () => { @@ -57,7 +56,7 @@ const Elements = { panel.activeTab = 'animation'; }, - update() { + update () { const panel = this; panel.$.tabs.innerText = ''; @@ -83,16 +82,16 @@ const Elements = { }, }, tabPanel: { - ready() { + ready () { const panel = this; panel.$.tabPanel.addEventListener('change', () => { panel.dispatch('change'); }); }, - update() { + update () { const panel = this; - + Editor.Message.broadcast('fbx-inspector:change-tab', panel.activeTab); panel.$.tabPanel.setAttribute('src', Components[panel.activeTab]); panel.$.tabPanel.update(panel.assetList, panel.metaList); }, @@ -134,16 +133,14 @@ exports.methods = { /** * Update whether a data is editable in multi-select state */ - updateInvalid(element, prop) { - const invalid = this.metaList.some((meta) => { - return meta.userData[prop] !== this.meta.userData[prop]; - }); + updateInvalid (element, prop) { + const invalid = this.metaList.some((meta) => meta.userData[prop] !== this.meta.userData[prop]); element.invalid = invalid; }, /** * Update read-only status */ - updateReadonly(element) { + updateReadonly (element) { if (this.asset.readonly) { element.setAttribute('disabled', true); } else { diff --git a/editor/inspector/assets/fbx/preview.js b/editor/inspector/assets/fbx/preview.js index 0d9be2ff4b..15f2bd9c36 100644 --- a/editor/inspector/assets/fbx/preview.js +++ b/editor/inspector/assets/fbx/preview.js @@ -1,11 +1,24 @@ -'use strict'; - exports.template = `
-
+
+ + + + + + + + + +
+
+
@@ -17,10 +30,10 @@ exports.style = ` margin-top: 10px; border-top: 1px solid var(--color-normal-border); } -.preview > .info { +.preview > .model-info { padding-top: 8px; } -.preview > .info > ui-label { +.preview > .model-info > ui-label { margin-right: 6px; } .preview > .image { @@ -35,23 +48,56 @@ exports.style = ` } `; +const PLAY_STATE = { + STOP: 0, + PLAYING: 1, + PAUSE: 2, +}; + exports.$ = { + model: '.model', container: '.preview', vertices: '.vertices', triangles: '.triangles', image: '.image', canvas: '.canvas', + animationInfo: '.animation-info', + modelInfo: '.model-info', + playButton: '#playButton', + playButtonIcon: '#playButtonIcon', + stopButton: '#stopButton', + animationTimeSlider: '#animationTimeSlider', + timeLabel: '#timeLabel', }; const Elements = { + playButton: { + ready () { + this.$.playButton.addEventListener('confirm', this.onPlayButtonClick.bind(this)); + }, + }, + stopButton: { + ready () { + this.$.stopButton.addEventListener('confirm', this.onStopButtonClick.bind(this)); + }, + }, + animationTimeSlider: { + ready () { + const slider = this.$.animationTimeSlider; + slider.addEventListener('change', this.onAnimationTimeSliderChange.bind(this)); + slider.addEventListener('confirm', this.onAnimationTimeSliderConfirm.bind(this)); + }, + }, preview: { - ready() { + ready () { const panel = this; panel.$.canvas.addEventListener('mousedown', (event) => { - Editor.Message.request('scene', 'on-model-preview-mouse-down', { x: event.x, y: event.y }); - function mousemove(event) { + // event.target.requestPointerLock(); + Editor.Message.send('scene', 'on-model-preview-mouse-down', { x: event.x, y: event.y }); + function mousemove (event) { + Editor.Message.send('scene', 'on-model-preview-mouse-move', { movementX: event.movementX, movementY: event.movementY, @@ -60,7 +106,11 @@ const Elements = { panel.isPreviewDataDirty = true; } - function mouseup(event) { + + function mouseup (event) { + // document.exitPointerLock(); + + Editor.Message.send('scene', 'on-model-preview-mouse-up', { x: event.x, y: event.y, @@ -75,16 +125,14 @@ const Elements = { document.addEventListener('mousemove', mousemove); document.addEventListener('mouseup', mouseup); - panel.isPreviewDataDirty = true; }); const GlPreview = Editor._Module.require('PreviewExtends').default; panel.glPreview = new GlPreview('scene:model-preview', 'query-model-preview-data'); - panel.isPreviewDataDirty = true; }, - async update() { + async update () { const panel = this; if (!panel.$.canvas) { @@ -99,29 +147,22 @@ const Elements = { panel.refreshPreview(); }, }, - info: { - ready() { - const panel = this; - - panel.infoUpdate = Elements.info.update.bind(panel); - Editor.Message.addBroadcastListener('scene:model-preview-model-info', panel.infoUpdate); + modelInfo: { + ready () { + this.infoUpdate = Elements.modelInfo.update.bind(this); + Editor.Message.addBroadcastListener('scene:model-preview-model-info', this.infoUpdate); }, - update(info) { + update (info) { if (!info) { return; } - - const panel = this; - - panel.$.vertices.value = 'Vertices:' + info.vertices; - panel.$.triangles.value = 'Triangles:' + info.polygons; - - panel.isPreviewDataDirty = true; + this.$.vertices.value = `Vertices:${info.vertices}`; + this.$.triangles.value = `Triangles:${info.polygons}`; + this.isPreviewDataDirty = true; }, - close() { - const panel = this; - Editor.Message.removeBroadcastListener('scene:model-preview-model-info', panel.infoUpdate); - Editor.Message.request('scene', 'hide-model-preview'); + close () { + Editor.Message.removeBroadcastListener('scene:model-preview-model-info', this.infoUpdate); + Editor.Message.send('scene', 'hide-model-preview'); }, }, }; @@ -138,15 +179,43 @@ exports.update = function (assetList, metaList) { element.update.call(this); } } + this.setCurPlayState(PLAY_STATE.STOP); + this.refreshPreview(); }; exports.ready = function () { + function data () { + return { + gridWidth: 0, + gridTableWith: 0, + activeTab: 'animation', + isPreviewDataDirty: true, + curEditClipInfo: null, + curPlayState: PLAY_STATE.STOP, + curTotalFrames: 0, + }; + } + Object.assign(this, data()); for (const prop in Elements) { const element = Elements[prop]; if (element.ready) { element.ready.call(this); } } + this.onTabChangedCallback = this.onTabChanged.bind(this); + this.onModelAnimationUpdateCallback = this.onModelAnimationUpdate.bind(this); + this.onAnimationPlayStateChangedCallback = this.onAnimationPlayStateChanged.bind(this); + this.onEditClipInfoChanged = async (clipInfo) => { + if (clipInfo) { + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'setEditClip', clipInfo.clipUUID); + this.setCurEditClipInfo(clipInfo); + console.log('clip info', clipInfo); + } + }; + Editor.Message.addBroadcastListener('scene:model-preview-animation-time-change', this.onModelAnimationUpdateCallback); + Editor.Message.addBroadcastListener('scene:model-preview-animation-state-change', this.onAnimationPlayStateChangedCallback); + Editor.Message.addBroadcastListener('fbx-inspector:change-tab', this.onTabChangedCallback); + Editor.Message.addBroadcastListener('fbx-inspector:animation-change', this.onEditClipInfoChanged); }; exports.close = function () { @@ -156,10 +225,14 @@ exports.close = function () { element.close.call(this); } } + + Editor.Message.removeBroadcastListener('scene:model-preview-animation-time-change', this.onModelAnimationUpdateCallback); + Editor.Message.removeBroadcastListener('scene:model-preview-animation-state-change', this.onAnimationPlayStateChangedCallback); + Editor.Message.removeBroadcastListener('fbx-inspector:change-tab', this.onTabChangedCallback); }; exports.methods = { - async refreshPreview() { + async refreshPreview () { const panel = this; // After await, the panel no longer exists @@ -200,4 +273,97 @@ exports.methods = { panel.refreshPreview(); }); }, + async onTabChanged (activeTab) { + if (typeof activeTab === 'string') { + this.activeTab = activeTab; + this.$.animationInfo.style.display = this.activeTab === 'animation' ? '' : 'none'; + this.$.modelInfo.style.display = this.activeTab === 'model' ? '' : 'none'; + await this.stopAnimation(); + } + }, + async onStopButtonClick (event) { + event.stopPropagation(); + await this.stopAnimation(); + }, + async stopAnimation () { + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'stop'); + }, + async onPlayButtonClick (event) { + event.stopPropagation(); + console.log('play button clicked', this.curEditClipInfo); + if (this.curEditClipInfo) { + switch (this.curPlayState) { + case PLAY_STATE.PAUSE: + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'resume'); + break; + case PLAY_STATE.PLAYING: + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'pause'); + break; + case PLAY_STATE.STOP: + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'play', this.curEditClipInfo.clipUUID); + break; + default: + break; + } + + this.isPreviewDataDirty = true; + } + }, + async onAnimationTimeSliderChange (event) { + event.stopPropagation(); + const frame = event.target.value; + const curTime = (frame / this.curTotalFrames) * this.curEditClipInfo.duration; + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'setCurEditTime', curTime); + this.isPreviewDataDirty = true; + }, + + onAnimationTimeSliderConfirm (event) { + event.stopPropagation(); + }, + + onModelAnimationUpdate (time) { + if (this.$.animationTimeSlider) { + this.$.animationTimeSlider.value = Math.round((time - this.curEditClipInfo.from) * this.curEditClipInfo.fps); + } + if (this.$.timeLabel) { + this.$.timeLabel.value = `Time:${(time - this.curEditClipInfo.from).toFixed(2)}(s)`; + } + + this.isPreviewDataDirty = true; + }, + setCurPlayState (state) { + this.curPlayState = state; + let buttonIconName = ''; + switch (state) { + case PLAY_STATE.STOP: + buttonIconName = 'play'; + break; + case PLAY_STATE.PLAYING: + buttonIconName = 'pause'; + break; + case PLAY_STATE.PAUSE: + buttonIconName = 'play'; + break; + default: + break; + } + + if (this.$.playButtonIcon) { + this.$.playButtonIcon.value = buttonIconName; + } + }, + async setCurEditClipInfo (clipInfo) { + this.curEditClipInfo = clipInfo; + if (clipInfo) { + this.curTotalFrames = Math.round(clipInfo.duration * clipInfo.fps); + if (this.$.animationTimeSlider) { + this.$.animationTimeSlider.max = this.curTotalFrames; + } + await Editor.Message.request('scene', 'execute-model-preview-animation-operation', 'setPlaybackRange', clipInfo.from, clipInfo.to); + await this.stopAnimation(); + } + }, + onAnimationPlayStateChanged (state) { + this.setCurPlayState(state); + }, };