-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
127 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import classNames from 'classnames'; | ||
import { isEqual } from 'lodash-es'; | ||
import React, { memo, useCallback, useRef } from 'react'; | ||
|
||
import PageLoading from '@/components/PageLoading'; | ||
import { useLoadVrm } from '@/hooks/useLoadVrm'; | ||
import { sessionSelectors, useSessionStore } from '@/store/session'; | ||
import { useViewerStore } from '@/store/viewer'; | ||
|
||
import ToolBar from '../components/ToolBar'; | ||
import { useStyles } from './style'; | ||
|
||
interface Props { | ||
className?: string; | ||
height?: number | string; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
function AgentViewer(props: Props) { | ||
const { className, style, height } = props; | ||
const { styles } = useStyles(); | ||
const ref = useRef<HTMLDivElement>(null); | ||
const viewer = useViewerStore((s) => s.viewer); | ||
const [currentAgent] = useSessionStore((s) => [sessionSelectors.currentAgent(s), isEqual]); | ||
|
||
const { loading, loadVrm } = useLoadVrm(viewer); | ||
|
||
const canvasRef = useCallback( | ||
(canvas: HTMLCanvasElement) => { | ||
if (canvas) { | ||
viewer.setup(canvas); | ||
loadVrm(currentAgent?.meta.model); | ||
} | ||
}, | ||
[viewer, currentAgent?.meta.model], | ||
); | ||
|
||
return ( | ||
<div ref={ref} className={classNames(styles.viewer, className)} style={style}> | ||
<ToolBar className={styles.toolbar} viewer={viewer} /> | ||
{loading ? <PageLoading title={'模型加载中,请稍后...'} className={styles.loading} /> : null} | ||
<canvas ref={canvasRef} className={styles.canvas} style={{ height }}></canvas> | ||
</div> | ||
); | ||
} | ||
|
||
export default memo(AgentViewer); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createStyles } from 'antd-style'; | ||
|
||
export const useStyles = createStyles(({ css, token }) => ({ | ||
viewer: css` | ||
position: relative; | ||
height: 100%; | ||
min-height: 0; | ||
`, | ||
toolbar: css` | ||
position: absolute; | ||
right: ${token.paddingMD}px; | ||
bottom: 50%; | ||
display: flex; | ||
`, | ||
loading: css` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
`, | ||
canvas: css` | ||
display: block; | ||
width: 100%; | ||
max-width: 100%; | ||
max-height: 100%; | ||
`, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import localforage from 'localforage'; | ||
import { useState } from 'react'; | ||
|
||
import { Viewer } from '@/features/vrmViewer/viewer'; | ||
|
||
export const useLoadVrm = (viewer: Viewer) => { | ||
const [loading, setLoading] = useState(false); | ||
|
||
const loadVrm = async (url?: string) => { | ||
let vrmUrl = url; | ||
if (url && url.startsWith('model:')) { | ||
const blob = await localforage.getItem(url); | ||
if (blob) { | ||
vrmUrl = window.URL.createObjectURL(blob as Blob); | ||
} else { | ||
vrmUrl = undefined; | ||
} | ||
} | ||
if (vrmUrl) { | ||
setLoading(true); | ||
viewer.loadVrm(vrmUrl).finally(() => { | ||
setLoading(false); | ||
}); | ||
} else { | ||
viewer.unloadVRM(); | ||
} | ||
}; | ||
|
||
return { loading, loadVrm }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters