mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-28 20:04:26 +00:00
fix(webui): fix 3d asset viewer frame seeking and drag jitter
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
import { FileTypeDefinition } from '../types'
|
||||
import ModelViewer from './model-viewer'
|
||||
|
||||
export function secondToDegree(second: number): number {
|
||||
const frameIndex = Math.min(Math.max(0, Math.round(second * 6)), 23)
|
||||
return (frameIndex * 15) % 360
|
||||
}
|
||||
import { secondToDegree } from './utils'
|
||||
export { secondToDegree } from './utils'
|
||||
|
||||
export const modelTypeDefinition: FileTypeDefinition = {
|
||||
id: '3d',
|
||||
|
||||
@@ -6,11 +6,13 @@ import { useAnnotationStore } from '@/ui/stores/annotation-store'
|
||||
import type { FileViewerProps, MediaController } from '../types'
|
||||
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
|
||||
import { ModelControlBar } from './model-control-bar'
|
||||
import { secondToDegree } from './index'
|
||||
|
||||
const TOTAL_FRAMES = 24
|
||||
const FPS = 6
|
||||
const FRAME_DURATION = 1 / FPS
|
||||
import {
|
||||
secondToDegree,
|
||||
secondToFrame,
|
||||
calculateFrameCenterTime,
|
||||
TOTAL_FRAMES,
|
||||
FRAME_DURATION,
|
||||
} from './utils'
|
||||
|
||||
export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
(
|
||||
@@ -101,7 +103,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
const normalizedFrame = ((frameIndex % TOTAL_FRAMES) + TOTAL_FRAMES) % TOTAL_FRAMES
|
||||
const targetSec = normalizedFrame * FRAME_DURATION
|
||||
if (videoRef.current) {
|
||||
videoRef.current.currentTime = targetSec
|
||||
videoRef.current.currentTime = calculateFrameCenterTime(normalizedFrame)
|
||||
}
|
||||
setCurrentTime(targetSec)
|
||||
onTimeUpdate?.(targetSec)
|
||||
@@ -111,6 +113,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
|
||||
const seekToDegree = useCallback(
|
||||
(degree: number) => {
|
||||
videoRef.current?.pause()
|
||||
const frame = Math.round((((degree % 360) + 360) % 360) / 15) % TOTAL_FRAMES
|
||||
seekToFrame(frame)
|
||||
},
|
||||
@@ -139,7 +142,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
videoRef.current?.pause()
|
||||
},
|
||||
seekTo: (second: number) => {
|
||||
seekToFrame(Math.floor(second * FPS))
|
||||
seekToFrame(secondToFrame(second))
|
||||
},
|
||||
getCurrentTime: () => videoRef.current?.currentTime || 0,
|
||||
getDuration: () => 4,
|
||||
@@ -165,11 +168,11 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
togglePlay()
|
||||
} else if (e.key === 'ArrowLeft') {
|
||||
e.preventDefault()
|
||||
const currentFrame = Math.round(currentTime * FPS)
|
||||
const currentFrame = secondToFrame(currentTime)
|
||||
seekToFrame(currentFrame - 1)
|
||||
} else if (e.key === 'ArrowRight') {
|
||||
e.preventDefault()
|
||||
const currentFrame = Math.round(currentTime * FPS)
|
||||
const currentFrame = secondToFrame(currentTime)
|
||||
seekToFrame(currentFrame + 1)
|
||||
}
|
||||
}
|
||||
@@ -185,7 +188,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
target.setPointerCapture(e.pointerId)
|
||||
videoRef.current?.pause()
|
||||
|
||||
const currentFrame = Math.floor(currentTime * FPS)
|
||||
const currentFrame = Math.round(currentDegree / 15) % TOTAL_FRAMES
|
||||
dragStartRef.current = {
|
||||
clientX: e.clientX,
|
||||
frame: currentFrame,
|
||||
@@ -308,7 +311,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
onLoadedData={() => {
|
||||
setIsLoading(false)
|
||||
if (startTime && startTime > 0) {
|
||||
seekToFrame(Math.floor(startTime * FPS))
|
||||
seekToFrame(secondToFrame(startTime))
|
||||
}
|
||||
}}
|
||||
onPlay={() => {
|
||||
@@ -320,7 +323,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
|
||||
onPause?.()
|
||||
}}
|
||||
onTimeUpdate={() => {
|
||||
if (videoRef.current && !isDraggingRotation) {
|
||||
if (videoRef.current && !isDraggingRotation && isPlaying) {
|
||||
setCurrentTime(videoRef.current.currentTime)
|
||||
onTimeUpdate?.(videoRef.current.currentTime)
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
// Dragging left (deltaX < 0) rotates turntable forward (angle increases)
|
||||
const nextRawDegree = dragStartRef.current.startDegree - deltaX / PX_PER_DEGREE
|
||||
const snapped = (((Math.round(nextRawDegree / STEP) * STEP) % 360) + 360) % 360
|
||||
setDragDegree(snapped)
|
||||
setDragDegree(nextRawDegree)
|
||||
onChange(snapped)
|
||||
},
|
||||
[isDragging, disabled, onChange],
|
||||
@@ -79,7 +79,8 @@ export const RulerSlider: React.FC<RulerSliderProps> = ({
|
||||
if (!isDragging) return
|
||||
setIsDragging(false)
|
||||
if (dragDegree !== null) {
|
||||
onChange(dragDegree)
|
||||
const snapped = (((Math.round(dragDegree / STEP) * STEP) % 360) + 360) % 360
|
||||
onChange(snapped)
|
||||
}
|
||||
setDragDegree(null)
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
calculateFrameCenterTime,
|
||||
secondToFrame,
|
||||
secondToDegree,
|
||||
TOTAL_FRAMES,
|
||||
FPS,
|
||||
FRAME_DURATION,
|
||||
} from './utils'
|
||||
|
||||
describe('model viewer utils', () => {
|
||||
describe('calculateFrameCenterTime', () => {
|
||||
it('calculates the center time for frame 0', () => {
|
||||
expect(calculateFrameCenterTime(0)).toBeCloseTo(0.5 / FPS)
|
||||
})
|
||||
|
||||
it('calculates the center time for all frames 0 to 23', () => {
|
||||
for (let i = 0; i < TOTAL_FRAMES; i++) {
|
||||
const center = calculateFrameCenterTime(i)
|
||||
expect(center).toBeCloseTo((i + 0.5) * FRAME_DURATION)
|
||||
}
|
||||
})
|
||||
|
||||
it('handles negative and wrapping frames properly', () => {
|
||||
expect(calculateFrameCenterTime(-1)).toBeCloseTo(calculateFrameCenterTime(23))
|
||||
expect(calculateFrameCenterTime(24)).toBeCloseTo(calculateFrameCenterTime(0))
|
||||
})
|
||||
})
|
||||
|
||||
describe('secondToFrame', () => {
|
||||
it('correctly maps frame start times to frame index without float truncation', () => {
|
||||
for (let i = 0; i < TOTAL_FRAMES; i++) {
|
||||
const startSec = i * FRAME_DURATION
|
||||
expect(secondToFrame(startSec)).toBe(i)
|
||||
}
|
||||
})
|
||||
|
||||
it('correctly maps frame center times to frame index', () => {
|
||||
for (let i = 0; i < TOTAL_FRAMES; i++) {
|
||||
const centerSec = calculateFrameCenterTime(i)
|
||||
expect(secondToFrame(centerSec)).toBe(i)
|
||||
}
|
||||
})
|
||||
|
||||
it('clamps values below 0 and above total duration', () => {
|
||||
expect(secondToFrame(-1)).toBe(0)
|
||||
expect(secondToFrame(100)).toBe(TOTAL_FRAMES - 1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('secondToDegree', () => {
|
||||
it('returns 0 for 0s', () => {
|
||||
expect(secondToDegree(0)).toBe(0)
|
||||
})
|
||||
|
||||
it('returns exact degrees (0..345) for every frame center', () => {
|
||||
for (let i = 0; i < TOTAL_FRAMES; i++) {
|
||||
const center = calculateFrameCenterTime(i)
|
||||
expect(secondToDegree(center)).toBe((i * 15) % 360)
|
||||
}
|
||||
})
|
||||
|
||||
it('returns exact degrees for frame start times', () => {
|
||||
for (let i = 0; i < TOTAL_FRAMES; i++) {
|
||||
const start = i * FRAME_DURATION
|
||||
expect(secondToDegree(start)).toBe((i * 15) % 360)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,28 @@
|
||||
export const TOTAL_FRAMES = 24
|
||||
export const FPS = 6
|
||||
export const FRAME_DURATION = 1 / FPS
|
||||
|
||||
/**
|
||||
* Calculates the presentation time at the center of a frame.
|
||||
* Seeking to the frame center avoids PTS boundary inaccuracies in browser video decoders.
|
||||
*/
|
||||
export function calculateFrameCenterTime(frameIndex: number): number {
|
||||
const normalizedFrame = ((frameIndex % TOTAL_FRAMES) + TOTAL_FRAMES) % TOTAL_FRAMES
|
||||
return (normalizedFrame + 0.5) * FRAME_DURATION
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a playback second into a frame index (0..23), clamped.
|
||||
* Works with frame start times, frame center times, or continuous playback seconds.
|
||||
*/
|
||||
export function secondToFrame(second: number): number {
|
||||
return Math.min(Math.max(0, Math.floor(second * FPS + 1e-4)), TOTAL_FRAMES - 1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a second to turntable degree (0, 15, 30, ..., 345).
|
||||
*/
|
||||
export function secondToDegree(second: number): number {
|
||||
const frameIndex = secondToFrame(second)
|
||||
return (frameIndex * 15) % 360
|
||||
}
|
||||
Reference in New Issue
Block a user