From 7722b6cdc84f90889d9cf0c87951b0f2946af524 Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 9 Oct 2026 22:55:49 +0800 Subject: [PATCH] fix(viewer): synchronize 3D turntable playback via frame callback to prevent skipped degrees --- CHANGELOG.md | 1 + .../viewers/model/model-viewer.test.tsx | 82 ++++++++++++++++++- .../components/viewers/model/model-viewer.tsx | 68 ++++++++++++++- packages/webui/e2e/tests/model-viewer.spec.ts | 36 ++++++++ 4 files changed, 184 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6c5dc86..f83522db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed - **viewer**: Fix an issue where certain angles in the 3D asset viewer appeared stuck on the previous frame, and eliminate view jitter when rotating at 345° +- **viewer**: Fix an issue where 3D asset auto-rotation during playback skipped 15° increments in the degree indicator ### Changed diff --git a/packages/webui/components/viewers/model/model-viewer.test.tsx b/packages/webui/components/viewers/model/model-viewer.test.tsx index 9ec5bd42..479636a0 100644 --- a/packages/webui/components/viewers/model/model-viewer.test.tsx +++ b/packages/webui/components/viewers/model/model-viewer.test.tsx @@ -1,5 +1,5 @@ // @vitest-environment happy-dom -import { cleanup, fireEvent, render } from '@testing-library/react' +import { act, cleanup, fireEvent, render } from '@testing-library/react' import React, { createRef } from 'react' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { ModelViewer } from './model-viewer' @@ -191,4 +191,84 @@ describe('ModelViewer', () => { fireEvent.click(minusBtn) expect(onTimeUpdate).toHaveBeenCalledWith(0) }) + + it('updates playback smoothly on each frame via requestVideoFrameCallback without skipping degrees', () => { + let frameCallback: ((now: number, metadata: { mediaTime: number }) => void) | null = null + const rvfcMock = vi.fn().mockImplementation((cb) => { + frameCallback = cb + return 101 + }) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(HTMLVideoElement.prototype as any).requestVideoFrameCallback = rvfcMock + + const onTimeUpdate = vi.fn() + const { container, getByTestId } = render( + , + ) + + const video = container.querySelector('video') + expect(video).toBeDefined() + + // Start playing + fireEvent.play(video!) + expect(rvfcMock).toHaveBeenCalled() + + // Simulate decoded frames arriving sequentially at 6 FPS (every 1/6 second) + const degreeReadout = getByTestId('degree-readout') + + // Frame 0: 0.083s -> 0° + act(() => { + frameCallback!(100, { mediaTime: 0.083 }) + }) + expect(degreeReadout.textContent).toContain('0° / 360°') + + // Frame 1: 0.25s -> 15° + act(() => { + frameCallback!(267, { mediaTime: 0.25 }) + }) + expect(degreeReadout.textContent).toContain('15° / 360°') + + // Frame 2: 0.417s -> 30° + act(() => { + frameCallback!(433, { mediaTime: 0.417 }) + }) + expect(degreeReadout.textContent).toContain('30° / 360°') + + // Frame 3: 0.583s -> 45° + act(() => { + frameCallback!(600, { mediaTime: 0.583 }) + }) + expect(degreeReadout.textContent).toContain('45° / 360°') + + // Frame 4: 0.75s -> 60° + act(() => { + frameCallback!(767, { mediaTime: 0.75 }) + }) + expect(degreeReadout.textContent).toContain('60° / 360°') + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + delete (HTMLVideoElement.prototype as any).requestVideoFrameCallback + }) + + it('cancels frame callback loop when video is paused', () => { + const cancelMock = vi.fn() + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(HTMLVideoElement.prototype as any).requestVideoFrameCallback = vi.fn().mockReturnValue(123) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(HTMLVideoElement.prototype as any).cancelVideoFrameCallback = cancelMock + + const { container } = render() + const video = container.querySelector('video') + + fireEvent.play(video!) + fireEvent.pause(video!) + + expect(cancelMock).toHaveBeenCalledWith(123) + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + delete (HTMLVideoElement.prototype as any).requestVideoFrameCallback + // eslint-disable-next-line @typescript-eslint/no-explicit-any + delete (HTMLVideoElement.prototype as any).cancelVideoFrameCallback + }) }) diff --git a/packages/webui/components/viewers/model/model-viewer.tsx b/packages/webui/components/viewers/model/model-viewer.tsx index e686e700..e8708ac3 100644 --- a/packages/webui/components/viewers/model/model-viewer.tsx +++ b/packages/webui/components/viewers/model/model-viewer.tsx @@ -97,6 +97,67 @@ export const ModelViewer = React.forwardRef( // Current angle const currentDegree = secondToDegree(currentTime) + const onTimeUpdateRef = useRef(onTimeUpdate) + useEffect(() => { + onTimeUpdateRef.current = onTimeUpdate + }, [onTimeUpdate]) + + // Synchronized playback frame loop (via requestVideoFrameCallback / requestAnimationFrame) + useEffect(() => { + if (!isPlaying) return + const video = videoRef.current + if (!video) return + + let animationFrameId: number | null = null + let rvfcId: number | null = null + let isCancelled = false + + type VideoWithRvfc = HTMLVideoElement & { + requestVideoFrameCallback?: ( + callback: (now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => void, + ) => number + cancelVideoFrameCallback?: (id: number) => void + } + + const videoWithRvfc = video as VideoWithRvfc + const rvfcSupported = typeof videoWithRvfc.requestVideoFrameCallback === 'function' + + const updateTime = (time: number) => { + if (!isDraggingRotation) { + setCurrentTime(time) + onTimeUpdateRef.current?.(time) + } + } + + if (rvfcSupported) { + const onFrame = (_now: DOMHighResTimeStamp, metadata: { mediaTime: number }) => { + if (isCancelled) return + updateTime(metadata.mediaTime) + rvfcId = videoWithRvfc.requestVideoFrameCallback!(onFrame) + } + rvfcId = videoWithRvfc.requestVideoFrameCallback!(onFrame) + } else { + const loop = () => { + if (isCancelled) return + if (videoRef.current) { + updateTime(videoRef.current.currentTime) + } + animationFrameId = requestAnimationFrame(loop) + } + animationFrameId = requestAnimationFrame(loop) + } + + return () => { + isCancelled = true + if (rvfcId !== null && typeof videoWithRvfc.cancelVideoFrameCallback === 'function') { + videoWithRvfc.cancelVideoFrameCallback(rvfcId) + } + if (animationFrameId !== null) { + cancelAnimationFrame(animationFrameId) + } + } + }, [isPlaying, isDraggingRotation]) + // Seek helper const seekToFrame = useCallback( (frameIndex: number) => { @@ -322,10 +383,13 @@ export const ModelViewer = React.forwardRef( setIsPlaying(false) onPause?.() }} + onEnded={() => { + setIsPlaying(false) + }} onTimeUpdate={() => { - if (videoRef.current && !isDraggingRotation && isPlaying) { + if (videoRef.current && !isDraggingRotation && !isPlaying) { setCurrentTime(videoRef.current.currentTime) - onTimeUpdate?.(videoRef.current.currentTime) + onTimeUpdateRef.current?.(videoRef.current.currentTime) } }} onError={() => { diff --git a/packages/webui/e2e/tests/model-viewer.spec.ts b/packages/webui/e2e/tests/model-viewer.spec.ts index f107e097..66474705 100644 --- a/packages/webui/e2e/tests/model-viewer.spec.ts +++ b/packages/webui/e2e/tests/model-viewer.spec.ts @@ -144,4 +144,40 @@ test.describe('3D Asset Viewer E2E Tests', () => { await page.mouse.up() }) + + /** + * ISSUE 3 VERIFICATION (Playback Frame Stepping): + * Asserts that auto-rotating via play/pause advances through consecutive 15° steps + * (e.g. 0° -> 15° -> 30° -> 45°) without skipping intermediate frames like 30°. + */ + test('Issue 3: verifies auto-rotation playback updates at consistent 15 degree increments', async ({ + page, + }) => { + const viewer = new ModelViewerPage(page) + await viewer.goto() + + // Start playback + await viewer.playToggle.click() + await page.waitForTimeout(100) + + // Collect observed degree values over 1.5 seconds of playback (~9 frames at 6 FPS) + const observedDegrees = new Set() + const startTime = Date.now() + + while (Date.now() - startTime < 1500) { + const deg = await viewer.getDegree() + if (!Number.isNaN(deg)) { + observedDegrees.add(deg) + } + await page.waitForTimeout(30) + } + + // Pause playback + await viewer.playToggle.click() + + // Verify that consecutive 15° frames including 30° and 60° were captured + expect(observedDegrees.has(30)).toBe(true) + expect(observedDegrees.has(45)).toBe(true) + expect(observedDegrees.has(60)).toBe(true) + }) })