diff --git a/src/editor/canvas/DirectorCanvas.tsx b/src/editor/canvas/DirectorCanvas.tsx index fd7caed..a9a013d 100644 --- a/src/editor/canvas/DirectorCanvas.tsx +++ b/src/editor/canvas/DirectorCanvas.tsx @@ -45,7 +45,11 @@ import { LensMonitor } from "./LensMonitor"; import { renderSceneWithBokeh } from "./viewportBokeh"; import { ViewportBackground } from "./ViewportBackground"; import { ViewportToolbar } from "./ViewportToolbar"; -import { getViewportAspectFrameRect, type ViewportSafeAreaInsets } from "./viewportAspectFrame"; +import { + getViewportAspectFrameRect, + resolveCameraViewRenderRect, + type ViewportSafeAreaInsets, +} from "./viewportAspectFrame"; /** * 导演视角的初始位姿:OrbitControls 需要一个轨道中心点,所以这里存的是「注视点」; @@ -387,8 +391,7 @@ function drawViewportCaptureLabels({ frameRect, heightScale, labels, - viewportHeight, - viewportWidth, + projectionRect, widthScale, }: { camera: ThreePerspectiveCamera; @@ -396,8 +399,11 @@ function drawViewportCaptureLabels({ frameRect: ViewportCaptureFrameRect; heightScale: number; labels: ViewportCaptureLabel[]; - viewportHeight: number; - viewportWidth: number; + /** + * 这帧 3D 场景实际渲染到画布的哪一块(CSS 像素,左上角原点)。 + * 导演视角是整块画布;机位视角是画幅框(无画幅框时传感器画幅遮幅),所以标签必须按这块做 NDC → 像素换算。 + */ + projectionRect: ViewportCaptureFrameRect; widthScale: number; }) { const drawingContext = context as CanvasRenderingContext2D & { @@ -423,8 +429,8 @@ function drawViewportCaptureLabels({ const projected = label.worldPosition.clone().project(camera); if (projected.z < -1 || projected.z > 1) return; - const viewportX = (projected.x * 0.5 + 0.5) * viewportWidth; - const viewportY = (-projected.y * 0.5 + 0.5) * viewportHeight; + const viewportX = projectionRect.left + (projected.x * 0.5 + 0.5) * projectionRect.width; + const viewportY = projectionRect.top + (-projected.y * 0.5 + 0.5) * projectionRect.height; const x = (viewportX - frameRect.left) * widthScale; const y = (viewportY - frameRect.top) * heightScale; const textWidth = context.measureText(label.text).width; @@ -451,17 +457,18 @@ function captureViewportCanvas( captureLabels?: { camera: ThreePerspectiveCamera; labels: ViewportCaptureLabel[]; - } + }, + /** + * 机位视角:这帧 3D 场景渲染到画布里的哪一块(CSS 像素,左上角原点)。 + * 传了它就按它裁切(截图与视口机位画面逐像素一致);不传则按画幅框裁切(导演视角的既有行为)。 + */ + renderViewportRect?: ViewportCaptureFrameRect ) { const viewportWidth = canvas.clientWidth || canvas.width; const viewportHeight = canvas.clientHeight || canvas.height; - const frameRect = getViewportAspectFrameRect( - aspectRatio, - viewportWidth, - viewportHeight, - bottomPadding, - safeAreaInsets - ); + const frameRect = + renderViewportRect ?? + getViewportAspectFrameRect(aspectRatio, viewportWidth, viewportHeight, bottomPadding, safeAreaInsets); const labels = captureLabels?.labels ?? []; if (!frameRect && labels.length === 0) { @@ -503,8 +510,7 @@ function captureViewportCanvas( frameRect: exportFrameRect, heightScale, labels, - viewportHeight, - viewportWidth, + projectionRect: renderViewportRect ?? { left: 0, top: 0, width: viewportWidth, height: viewportHeight }, widthScale, }); } @@ -567,25 +573,60 @@ function CanvasCaptureBridge({ metaTarget?: [number, number, number], lens?: ResolvedLens | null ) => { - withViewportCaptureHelpersHidden(scene, () => { - // 机位视角带镜头参数 → bokeh 后处理渲染(浅景深);否则普通渲染(导演视角 / 环绕预览)。 - if (lens) renderSceneWithBokeh(gl, scene, workingCamera, lens); - else gl.render(scene, workingCamera); - }); - return { - label, - dataUrl: captureViewportCanvas(gl.domElement, viewportAspectRatio, bottomPadding, safeAreaInsets, { - camera: workingCamera, - labels: getViewportCaptureLabels(scene), - }), - meta: buildScreenshotMeta({ - mode: viewMode, - cameraId: cameraId ?? (viewMode === "camera" ? activeCamera?.id ?? null : null), - fov: workingCamera.fov, - position: [workingCamera.position.x, workingCamera.position.y, workingCamera.position.z], - target: metaTarget ?? [target.x, target.y, target.z], - }), - }; + const canvasCssWidth = gl.domElement.clientWidth || gl.domElement.width; + const canvasCssHeight = gl.domElement.clientHeight || gl.domElement.height; + // 机位视角(带镜头参数)与视口「机位视角」用同一个渲染矩形:按传感器画幅在画布上等比适配 + // (画布更宽 → 高度铺满、左右留白居中)。相机 aspect 必须与矩形一致,否则投影被拉伸; + // 渲染进矩形后按同一矩形裁切,截图才和视口所见 / 镜头监视器逐像素一致。 + const renderRect = lens + ? resolveCameraViewRenderRect( + canvasCssWidth, + canvasCssHeight, + lens.sensor.widthMm / lens.sensor.heightMm + ) + : null; + // 没有镜头参数(导演视角 / 四方位环绕预览)时按整块画布渲染再裁画幅框: + // 相机 aspect 对齐画布宽高比,裁出来的画幅框才不变形。 + const previousAspect = workingCamera.aspect; + const nextAspect = renderRect ? renderRect.aspect : canvasCssWidth / canvasCssHeight; + if (nextAspect > 0 && Number.isFinite(nextAspect) && Math.abs(previousAspect - nextAspect) > 1e-4) { + workingCamera.aspect = nextAspect; + workingCamera.updateProjectionMatrix(); + } + + try { + withViewportCaptureHelpersHidden(scene, () => { + // 机位视角带镜头参数 → bokeh 后处理渲染(浅景深);否则普通渲染(导演视角 / 环绕预览)。 + if (lens) renderSceneWithBokeh(gl, scene, workingCamera, lens, renderRect ?? undefined); + else gl.render(scene, workingCamera); + }); + return { + label, + dataUrl: captureViewportCanvas( + gl.domElement, + viewportAspectRatio, + bottomPadding, + safeAreaInsets, + { + camera: workingCamera, + labels: getViewportCaptureLabels(scene), + }, + renderRect ?? undefined + ), + meta: buildScreenshotMeta({ + mode: viewMode, + cameraId: cameraId ?? (viewMode === "camera" ? activeCamera?.id ?? null : null), + fov: workingCamera.fov, + position: [workingCamera.position.x, workingCamera.position.y, workingCamera.position.z], + target: metaTarget ?? [target.x, target.y, target.z], + }), + }; + } finally { + if (Math.abs(workingCamera.aspect - previousAspect) > 1e-4) { + workingCamera.aspect = previousAspect; + workingCamera.updateProjectionMatrix(); + } + } }; if (preset === "current") { @@ -1135,6 +1176,8 @@ export function DirectorCanvas() { state.viewMode); + // 机位视角下,这块监视器直接顶到主视口「机位画面」的位置上,成为那块画幅本身: + // 机位视角显示的**就是这块 canvas 的像素**(同一份渲染、同一个画幅), + // 于是「机位视角 == 镜头监视器 == 导出」天然一致,不会再出现取景 / 虚化两边对不上; + // 主视口那边同时不再重复渲染一遍完整景深管线(少一半开销,之前又卡又糊的主因)。 + const rootRef = useRef(null); + const [viewportFrame, setViewportFrame] = useState<{ left: number; top: number; width: number; height: number } | null>( + null + ); + + useLayoutEffect(() => { + // 画幅是相对「画布」等比适配出来的,而这块监视器的父元素正是 .canvas-frame(position: relative) + const frame = rootRef.current?.parentElement ?? null; + if (!frame) return undefined; + + let animationFrameId = 0; + const measure = () => { + const rect = resolveCameraViewRenderRect(frame.clientWidth, frame.clientHeight, sensorAspect); + setViewportFrame((current) => + current && rect && current.left === rect.left && current.top === rect.top && current.width === rect.width && current.height === rect.height + ? current + : rect + ); + }; + const scheduleMeasure = () => { + cancelAnimationFrame(animationFrameId); + animationFrameId = requestAnimationFrame(measure); + }; + + measure(); + window.addEventListener("resize", scheduleMeasure); + + if (typeof ResizeObserver === "undefined") { + return () => { + cancelAnimationFrame(animationFrameId); + window.removeEventListener("resize", scheduleMeasure); + }; + } + + const resizeObserver = new ResizeObserver(scheduleMeasure); + resizeObserver.observe(frame); + return () => { + cancelAnimationFrame(animationFrameId); + window.removeEventListener("resize", scheduleMeasure); + resizeObserver.disconnect(); + }; + }, [sensorAspect]); + + const viewportCameraFrame = viewMode === "camera" && camera ? viewportFrame : null; const [dragging, setDragging] = useState(false); const dragRef = useRef<{ startX: number; @@ -253,11 +309,29 @@ export function LensMonitor({ rightOffset = 12 }: { rightOffset?: number }) { }; return ( -
+
void; safeAreaInsets?: ViewportSafeAreaInsets; + /** + * 画幅框外是否做磨砂(backdrop blur)。导演视角下是一层构图遮罩;**机位视角下必须关掉**: + * 那里框外仍然是相机画面(机位视角按传感器画幅铺满画布),磨砂会把相机画面整片糊掉, + * 和镜头监视器 / 导出对不上。关掉后只留一层压暗 + 画幅框边线 + 九宫格,构图参考照样在。 + */ + blurOutsideFrame?: boolean; }) { const overlayRef = useRef(null); const [overlaySize, setOverlaySize] = useState({ width: 0, height: 0 }); @@ -116,7 +123,12 @@ export function ViewportAspectOverlay({ return (
{maskStyle ? ( -