Create professional demos and 3D mockups in seconds, directly in your browser
Fix: Movement video in 3d models
CristianOlivera1d21375f59815
diff --git a/app/components/ui/editor/VideoCanvas.tsx b/app/components/ui/editor/VideoCanvas.tsxindex 46a05d4..b347a0b 100644--- a/app/components/ui/editor/VideoCanvas.tsx+++ b/app/components/ui/editor/VideoCanvas.tsx@@ -33,7 +33,7 @@ import DropMedia from "@/components/ui/DropMedia";import { renderCanvasElements } from "@/lib/canvas-elements-render.utils";import { drawMaskedImage } from "@/lib/masked-image-draw.utils";import { drawMockupAndMedia, type MockupDrawContext } from "@/lib/mockup-media-draw.utils";-import { drawPhone3DCompositeWithZoom, type Phone3DCompositeContext } from "@/lib/phone3d-composite-draw.utils";+import { drawPhone3DCompositeWithZoom, Phone3DApi, type Phone3DCompositeContext } from "@/lib/phone3d-composite-draw.utils";import { buildMockupMotionCss, MockupMotionTransform, REST_MOCKUP_MOTION, sampleCombinedMockupMotion, sampleCombined3DMotion, REST_MOCKUP_3D_MOTION, Mockup3DMotionTransform, MOTION_PRESET_3D_IDS } from "@/lib/mockup-motion";import { filterVisibleElements } from "@/lib/canvas-elements-timeline.utils";import { ZoomPointOverlay, ZOOM_POINT_VISUAL_SCALE } from "@/components/ui/ZoomPointOverlay";@@ -41,7 +41,6 @@ import {DEFAULT_MOCKUP_CAMERA_FOV,sampleMockupCameraKeyframes,} from "@/lib/viewer-controls3d";-import type { MockupCameraKeyframe, MockupCameraPose } from "@/lib/viewer-controls3d";import { normalizeCamera3DConfig, normalizeCameraZoom } from "@/types/camera.types";import { globalLayerCache } from "@/lib/dsa/layer-cache";@@ -287,12 +286,7 @@ function VideoCanvasInner({const imagePhoneCanvasRef = useRef<HTMLCanvasElement | null>(null);// Root THREE.Group ref for applying 3D motion during export rendering.const imagePhoneRootRef = useRef<THREE.Group | null>(null);- const imagePhoneApiRef = useRef<{- renderAt: (w: number, h: number, motion?: Mockup3DMotionTransform, cameraPose?: MockupCameraPose) => void;- restorePreview: () => void;- hasBuiltInShadow?: boolean;- getVisualSize?: () => { width: number; height: number; offsetY?: number } | null; // ← offsetY- } | null>(null);+ const imagePhoneApiRef = useRef<Phone3DApi | null>(null);const [activePhoneDevice, setActivePhoneDevice] = useState<ImageDeviceId | null>(null);const [phoneTransitioning, setPhoneTransitioning] = useState(false);const rafDragRef = useRef<number | null>(null);@@ -1374,7 +1368,7 @@ function VideoCanvasInner({};// Function to draw a frame on the export canvas- const drawFrame = async (highQuality: boolean = true, explicitTimelineTime?: number, frameOverride?: CanvasImageSource | VideoFrame) => {+ const drawFrameInner = async (highQuality: boolean = true, explicitTimelineTime?: number, frameOverride?: CanvasImageSource | VideoFrame) => {const canvas = exportCanvasRef.current;const canvasCtxOptions: CanvasRenderingContext2DSettings = { alpha: true, colorSpace: 'srgb', desynchronized: false, willReadFrequently: false };const ctx = canvas?.getContext('2d', canvasCtxOptions);@@ -1452,11 +1446,16 @@ function VideoCanvasInner({effectivePhoneMaskConfig, maskCompositeCanvasRef,imagePhoneRootRef,motion3DForFrame,+ previewMotion3D: mockup3DMotionPreview,cameraPoseForFrame: mediaType === "video"? sampleMockupCameraKeyframes(mockupCameraKeyframes, frameTime, DEFAULT_MOCKUP_CAMERA_FOV[imagePhoneDevice]) ?? undefined: undefined,};+ if (mediaType === "video" && imagePhoneActive) {+ imagePhoneApiRef.current?.setExportFrame?.(mediaSource);+ }+ctx.setTransform(1, 0, 0, 1, 0, 0);ctx.clearRect(0, 0, canvasWidth, canvasHeight);@@ -1889,6 +1888,16 @@ function VideoCanvasInner({}};+ const drawFrame = async (...args: Parameters<typeof drawFrameInner>): Promise<void> => {+ try {+ await drawFrameInner(...args);+ } finally {+ if (mediaType === "video" && imagePhoneActive) {+ imagePhoneApiRef.current?.setExportFrame?.(null);+ }+ }+ };+const [activeVideoElement, setActiveVideoElement] = useState<HTMLVideoElement | null>(null);useEffect(() => {setActiveVideoElement(mediaType === "video" ? videoRef.current : null);diff --git a/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx b/app/components/ui/editor/mockups-3d/Mockup3DStage.tsxindex 98244d7..cb2936a 100644--- a/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx+++ b/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx@@ -1,8 +1,8 @@"use client";import { Device3DBoundary } from "./Device3DBoundary";import { IPhoneDuoScene } from "./IPhoneDuo3DViewer";-import { Canvas } from "@react-three/fiber";-import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react";+import { Canvas, useThree } from "@react-three/fiber";+import { Suspense, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";import * as THREE from "three";import { IPhone13ProMax3DApi, IPhone13ProMaxScene } from "./IPhone13ProMax3DViewer";import { DoubleIPhone3DApi, DoubleIPhoneScene } from "./DoubleIPhone3DViewer";@@ -22,14 +22,16 @@ import type { MockupCameraPose } from "@/lib/viewer-controls3d";import { REST_MOCKUP_3D_MOTION, type Mockup3DMotionTransform } from "@/lib/mockup-motion-3d";import { useFrame } from "@react-three/fiber";import { getMockupCameraPosition, type MockupCameraKeyframe } from "@/lib/viewer-controls3d";+import { ExportFrameSource, ExportScreenSwapper, RenderAtWithMotion } from "@/lib/export-screen-swapper";-export type Mockup3DApi =+export type Mockup3DApi = (| IPhone13ProMax3DApi| DoubleIPhone3DApi| Phone3DApi| Laptop3DApi| IPhone17ProMax3DApi- | IPadMini63DApi;+ | IPadMini63DApi+) & { setExportFrame?: ExportFrameFn };export interface Mockup3DStageProps {imageUrl?: string | null;@@ -38,8 +40,8 @@ export interface Mockup3DStageProps {initialRotationX?: number;initialRotationY?: number;initialRotationZ?: number;- openingProgress?: number; // laptop / foldable phone- modelUrl?: string; // solo devices genéricos (phone/iphone)+ openingProgress?: number;+ modelUrl?: string;onRotationChange?: (rx: number, ry: number) => void;cameraConfig?: MockupCameraConfig;cameraNavigationMode?: CameraNavigationMode;@@ -61,9 +63,7 @@ export interface Mockup3DStageProps {isHovered?: boolean;onHoverChange?: (isHovered: boolean) => void;onSelectChange?: (isSelected: boolean) => void;- /** 3D motion transform applied additively to the root group every frame. */motionTransform?: Mockup3DMotionTransform;- /** External root group ref, used for export-time motion application. */rootRef?: React.MutableRefObject<THREE.Group | null>;}@@ -119,8 +119,7 @@ function Motion3DApplicator({useFrame(() => {const root = rootRef.current;if (!root) return;- // Capture the base transform lazily inside useFrame in case the group- // mounts after the initial useEffect (R3F suspends scene children).+if (!baseRef.current) {baseRef.current = {rx: root.rotation.x,@@ -150,6 +149,42 @@ function Motion3DApplicator({return null;}+type ExportFrameFn = (src: ExportFrameSource | null) => void;++function ExportScreenBridge({+ bridgeRef,+ cropArea,+ videoElement,+}: {+ bridgeRef: React.MutableRefObject<ExportFrameFn | null>;+ cropArea?: { x: number; y: number; width: number; height: number } | null;+ videoElement?: HTMLVideoElement | null;+}) {+ const { scene, gl } = useThree();+ const cropRef = useRef(cropArea);+ const videoRef = useRef(videoElement);+ useLayoutEffect(() => {+ cropRef.current = cropArea;+ videoRef.current = videoElement;+ });++ useEffect(() => {+ const swapper = new ExportScreenSwapper(+ scene,+ () => cropRef.current,+ () => videoRef.current,+ gl.capabilities.getMaxAnisotropy(),+ );+ bridgeRef.current = (src) => swapper.setFrame(src);+ return () => {+ swapper.dispose();+ bridgeRef.current = null;+ };+ }, [scene, gl, bridgeRef]);++ return null;+}+export function Mockup3DStage(props: StageProps) {return <Device3DBoundary key={props.device} modelUrl={PHONE_DEVICE_URLS[props.device]}><StageContent {...props} /></Device3DBoundary>;}@@ -160,6 +195,7 @@ function StageContent({ device, rootRef: externalRootRef, cameraRef: externalCamconst rootRef = externalRootRef ?? internalRootRef;const cameraRef = externalCameraRef ?? internalCameraRef;const cameraPathRef = useRef<THREE.Group | null>(null);+ const exportBridgeRef = useRef<ExportFrameFn | null>(null);const canvasElRef = useRef<HTMLCanvasElement | null>(null);const zoomRef = useRef(props.zoom ?? 1);const onApi = props.onApi;@@ -188,9 +224,12 @@ function StageContent({ device, rootRef: externalRootRef, cameraRef: externalCamreturn;}- const wrappedApi = {+ const renderBase: RenderAtWithMotion = api.renderAt;++ const wrappedApi: Mockup3DApi = {...api,- renderAt: (width: number, height: number, _motion?: Mockup3DMotionTransform, cameraPose?: MockupCameraPose) => {+ setExportFrame: (src) => exportBridgeRef.current?.(src),+ renderAt: (width: number, height: number, motion?: Mockup3DMotionTransform, cameraPose?: MockupCameraPose) => {const camera = cameraRef.current;const restoreCamera = camera && cameraPose? applyMockupCameraPoseForRender(camera, cameraPose, device, zoomRef.current)@@ -199,14 +238,14 @@ function StageContent({ device, rootRef: externalRootRef, cameraRef: externalCamconst wasCameraPathVisible = cameraPath?.visible ?? false;if (cameraPath) cameraPath.visible = false;try {- api.renderAt(width, height);+ renderBase(width, height, motion);} finally {if (cameraPath) cameraPath.visible = wasCameraPathVisible;restoreCamera?.();}},};- onApi?.(wrappedApi as Mockup3DApi);+ onApi?.(wrappedApi);}, [device, onApi, cameraRef]);const handleMount = (canvas: HTMLCanvasElement) => {@@ -236,6 +275,7 @@ function StageContent({ device, rootRef: externalRootRef, cameraRef: externalCamhandleMount(gl.domElement);}}>+ <ExportScreenBridge bridgeRef={exportBridgeRef} cropArea={props.cropArea} videoElement={props.videoElement} /><Motion3DApplicator rootRef={rootRef} motionTransform={props.motionTransform ?? REST_MOCKUP_3D_MOTION} device={device} baseRotationZ={props.initialRotationZ ?? 0} />{props.cameraKeyframes && props.cameraKeyframes.length > 0 && (<CameraPathdiff --git a/hooks/useVideoExport.ts b/hooks/useVideoExport.tsindex bdbd22a..93d9b0f 100644--- a/hooks/useVideoExport.ts+++ b/hooks/useVideoExport.ts@@ -8,7 +8,7 @@ import { QUALITY_SETTINGS, DEFAULT_EXPORT_FPS } from "@/lib/constants";import { ensureVideoReady, waitForVideoFrame, downloadBlob } from "@/lib/video.utils";import { FFmpeg } from "@ffmpeg/ffmpeg";import { toBlobURL } from "@ffmpeg/util";-import { blobToUint8Array, buildAtempoChain, canvasToBlobFast, getActiveClipAtTime, resolveClipTrimStart } from "@/lib/ffmpeg.utils";+import { blobToUint8Array, canvasToBlobFast, getActiveClipAtTime, resolveClipTrimStart } from "@/lib/ffmpeg.utils";import type { VideoTrackClip } from "@/types/video-track.types";export type { ExportQuality, ExportSettings, ExportProgress };diff --git a/lib/export-screen-swapper.ts b/lib/export-screen-swapper.tsnew file mode 100644index 0000000..30ec0a4--- /dev/null+++ b/lib/export-screen-swapper.ts@@ -0,0 +1,266 @@+import * as THREE from "three";+import { applyTextureCover, type CropAreaLike } from "@/lib/phone3d.utils";+import { Mockup3DMotionTransform } from "./mockup-motion";++export type ExportFrameSource = CanvasImageSource | VideoFrame;+export type ExportFrameFn = (src: ExportFrameSource | null) => void;++type MapSlot = "map" | "emissiveMap";+type MapMaterial = THREE.Material & Partial<Record<MapSlot, THREE.Texture | null>>;+const MAP_SLOTS: readonly MapSlot[] = ["map", "emissiveMap"];++interface MapBinding {+ kind: "map";+ material: MapMaterial;+ slot: MapSlot;+ original: THREE.VideoTexture;+}++interface UniformBinding {+ kind: "uniform";+ material: THREE.ShaderMaterial;+ original: THREE.VideoTexture;+ saved: {+ transform: THREE.Matrix3 | null;+ isVideo: number | null;+ resolution: THREE.Vector2 | null;+ };+}++type Binding = MapBinding | UniformBinding;++interface ExportEntry {+ canvas: HTMLCanvasElement;+ tex: THREE.CanvasTexture;+ matrix: THREE.Matrix3 | null;+}+++function readMatrix3(u: THREE.IUniform | undefined): THREE.Matrix3 | null {+ const v: unknown = u?.value;+ return v instanceof THREE.Matrix3 ? v : null;+}+function readVector2(u: THREE.IUniform | undefined): THREE.Vector2 | null {+ const v: unknown = u?.value;+ return v instanceof THREE.Vector2 ? v : null;+}+function readNumber(u: THREE.IUniform | undefined): number | null {+ const v: unknown = u?.value;+ return typeof v === "number" ? v : null;+}+function readVideoTexture(u: THREE.IUniform | undefined): THREE.VideoTexture | null {+ const v: unknown = u?.value;+ return v instanceof THREE.VideoTexture ? v : null;+}++function frameSize(src: ExportFrameSource): { w: number; h: number } {+ if ("displayWidth" in src) return { w: src.displayWidth, h: src.displayHeight };+ if ("videoWidth" in src) return { w: src.videoWidth, h: src.videoHeight };+ if ("naturalWidth" in src) return { w: src.naturalWidth, h: src.naturalHeight };+ if (typeof src.width === "number" && typeof src.height === "number") {+ return { w: src.width, h: src.height };+ }+ return { w: 0, h: 0 };+}++function inferTargetAspect(+ repeatX: number,+ repeatY: number,+ videoW: number,+ videoH: number,+ crop: CropAreaLike | null | undefined,+): number | null {+ let cropW = 1;+ let cropH = 1;+ if (crop) {+ const x = Math.max(0, Math.min(100, crop.x));+ const y = Math.max(0, Math.min(100, crop.y));+ cropW = Math.max(1, Math.min(100 - x, crop.width)) / 100;+ cropH = Math.max(1, Math.min(100 - y, crop.height)) / 100;+ }+ const croppedAspect = (videoW * cropW) / (videoH * cropH);+ const aspect = (croppedAspect * (repeatX / cropW)) / (repeatY / cropH);+ return Number.isFinite(aspect) && aspect > 0 ? aspect : null;+}++export type RenderAtWithMotion = (w: number, h: number, motion?: Mockup3DMotionTransform) => void;++export class ExportScreenSwapper {+ private bindings: Binding[] = [];+ private entries = new Map<THREE.Texture, ExportEntry>();+ private aspects = new WeakMap<THREE.Texture, number>();+ private warned = false;++ constructor(+ private scene: THREE.Scene,+ private getCrop: () => CropAreaLike | null | undefined,+ private getVideo: () => HTMLVideoElement | null | undefined,+ private anisotropy: number,+ ) { }++ setFrame(src: ExportFrameSource | null): void {+ if (!src) {+ this.restore();+ return;+ }+ const { w, h } = frameSize(src);+ if (!w || !h) return;++ if (this.bindings.length === 0) this.collect();+ if (this.bindings.length === 0) {+ if (!this.warned) {+ this.warned = true;+ console.warn("[export] No se encontró ninguna VideoTexture en la escena 3D; la pantalla no se actualizará.");+ }+ return;+ }++ const drawn = new Set<THREE.Texture>();+ for (const b of this.bindings) {+ const entry = this.ensureEntry(b, w, h);+ if (!drawn.has(b.original)) {+ drawn.add(b.original);+ entry.canvas.getContext("2d")?.drawImage(src, 0, 0, w, h);+ entry.tex.needsUpdate = true;+ }++ if (b.kind === "map") {+ b.material[b.slot] = entry.tex;+ continue;+ }+ const u = b.material.uniforms;+ u.screenMap.value = entry.tex;+ const transform = readMatrix3(u.screenTransform);+ if (transform && entry.matrix) transform.copy(entry.matrix);+ if (u.isVideo) u.isVideo.value = 0;+ readVector2(u.resolution)?.set(w, h);+ }+ }++ dispose(): void {+ this.restore();+ for (const e of this.entries.values()) e.tex.dispose();+ this.entries.clear();+ }++ private collect(): void {+ const seen = new Set<string>();+ this.scene.traverse((obj) => {+ if (!(obj instanceof THREE.Mesh)) return;+ const mats: THREE.Material[] = Array.isArray(obj.material) ? obj.material : [obj.material];++ for (const m of mats) {+ if (m instanceof THREE.ShaderMaterial) {+ const original = readVideoTexture(m.uniforms.screenMap);+ const key = `${m.uuid}:screenMap`;+ if (!original || seen.has(key)) continue;+ seen.add(key);+ this.bindings.push({+ kind: "uniform",+ material: m,+ original,+ saved: {+ transform: readMatrix3(m.uniforms.screenTransform)?.clone() ?? null,+ isVideo: readNumber(m.uniforms.isVideo),+ resolution: readVector2(m.uniforms.resolution)?.clone() ?? null,+ },+ });+ continue;+ }++ const mm = m as MapMaterial;+ for (const slot of MAP_SLOTS) {+ const t = mm[slot];+ const key = `${m.uuid}:${slot}`;+ if (!(t instanceof THREE.VideoTexture) || seen.has(key)) continue;+ seen.add(key);+ this.bindings.push({ kind: "map", material: mm, slot, original: t });+ }+ }+ });+ }++ private ensureEntry(b: Binding, w: number, h: number): ExportEntry {+ const vt = b.original;+ const existing = this.entries.get(vt);+ if (existing && existing.canvas.width === w && existing.canvas.height === h) return existing;++ existing?.tex.dispose();+ const canvas = existing?.canvas ?? document.createElement("canvas");+ canvas.width = w;+ canvas.height = h;++ const tex = new THREE.CanvasTexture(canvas);+ tex.flipY = vt.flipY;+ tex.colorSpace = vt.colorSpace;+ tex.generateMipmaps = true;+ tex.minFilter = THREE.LinearMipmapLinearFilter;+ tex.magFilter = THREE.LinearFilter;+ tex.wrapS = THREE.ClampToEdgeWrapping;+ tex.wrapT = THREE.ClampToEdgeWrapping;+ tex.anisotropy = this.anisotropy;++ const crop = this.getCrop();+ let aspect = this.aspects.get(vt);+ if (aspect === undefined) {+ const video = this.getVideo();+ const sx = b.kind === "map" ? vt.repeat.x : (b.saved.transform?.elements[0] ?? 0);+ const sy = b.kind === "map" ? vt.repeat.y : (b.saved.transform?.elements[4] ?? 0);+ const inferred =+ video && video.videoWidth > 0 && video.videoHeight > 0 && sx > 0 && sy > 0+ ? inferTargetAspect(sx, sy, video.videoWidth, video.videoHeight, crop)+ : null;+ if (inferred !== null) {+ this.aspects.set(vt, inferred);+ aspect = inferred;+ }+ }++ let matrix: THREE.Matrix3 | null = null;+ if (b.kind === "map") {+ tex.center.copy(vt.center);+ tex.rotation = vt.rotation;+ if (aspect !== undefined) {+ applyTextureCover(tex, w, h, aspect * 1000, 1000, crop);+ } else {+ tex.repeat.copy(vt.repeat);+ tex.offset.copy(vt.offset);+ }+ } else if (aspect !== undefined) {+ const tmp = new THREE.Texture();+ tmp.flipY = vt.flipY;+ applyTextureCover(tmp, w, h, aspect * 1000, 1000, crop);+ tmp.updateMatrix();+ matrix = tmp.matrix.clone();+ tmp.dispose();+ } else {+ matrix = b.saved.transform?.clone() ?? null;+ }++ const entry: ExportEntry = { canvas, tex, matrix };+ this.entries.set(vt, entry);+ return entry;+ }++ private restore(): void {+ for (const b of this.bindings) {+ const entry = this.entries.get(b.original);+ if (!entry) continue;++ if (b.kind === "map") {+ if (b.material[b.slot] === entry.tex) b.material[b.slot] = b.original;+ continue;+ }++ const u = b.material.uniforms;+ if (u.screenMap.value !== entry.tex) continue;+ u.screenMap.value = b.original;+ const transform = readMatrix3(u.screenTransform);+ if (transform && b.saved.transform) transform.copy(b.saved.transform);+ if (u.isVideo && b.saved.isVideo !== null) u.isVideo.value = b.saved.isVideo;+ const resolution = readVector2(u.resolution);+ if (resolution && b.saved.resolution) resolution.copy(b.saved.resolution);+ }+ this.bindings = [];+ }+}\ No newline at end of filediff --git a/lib/phone3d-composite-draw.utils.ts b/lib/phone3d-composite-draw.utils.tsindex 3fd264a..2534444 100644--- a/lib/phone3d-composite-draw.utils.ts+++ b/lib/phone3d-composite-draw.utils.ts@@ -1,14 +1,14 @@import type { MutableRefObject } from "react";+import * as THREE from "three";import { drawMaskedImage } from "@/lib/masked-image-draw.utils";import { PHONE_H, PHONE_W, DEVICE_3D_DIMENSIONS, type ImageMaskConfigLike } from "@/lib/phone3d.utils";-import type { Mockup3DMotionTransform } from "@/lib/mockup-motion-3d";-import { REST_MOCKUP_3D_MOTION } from "@/lib/mockup-motion-3d";-import * as THREE from "three";+import { REST_MOCKUP_3D_MOTION, type Mockup3DMotionTransform } from "@/lib/mockup-motion-3d";import type { MockupCameraPose } from "@/lib/viewer-controls3d";export interface Phone3DApi {renderAt: (w: number, h: number, motion?: Mockup3DMotionTransform, cameraPose?: MockupCameraPose) => void;restorePreview: () => void;+ setExportFrame?: (src: CanvasImageSource | null) => void;hasBuiltInShadow?: boolean;getVisualSize?: () => { width: number; height: number; offsetY?: number } | null;}@@ -25,19 +25,29 @@ export interface Phone3DCompositeContext {imagePhoneShadowColor: string;effectivePhoneMaskConfig: ImageMaskConfigLike | null | undefined;maskCompositeCanvasRef: MutableRefObject<HTMLCanvasElement | null>;- /** Root group ref for applying 3D motion during export. */imagePhoneRootRef?: MutableRefObject<THREE.Group | null>;- /** 3D motion transform to apply before rendering each export frame. */motion3DForFrame?: Mockup3DMotionTransform;+ previewMotion3D?: Mockup3DMotionTransform;cameraPoseForFrame?: MockupCameraPose;}-// Cache of the base transform so we can restore after export rendering.-let cachedBase: {+interface RootPose {rx: number; ry: number; rz: number;sx: number; sy: number; sz: number;px: number; py: number; pz: number;-} | null = null;+}++const readPose = (g: THREE.Group): RootPose => ({+ rx: g.rotation.x, ry: g.rotation.y, rz: g.rotation.z,+ sx: g.scale.x, sy: g.scale.y, sz: g.scale.z,+ px: g.position.x, py: g.position.y, pz: g.position.z,+});++const writePose = (g: THREE.Group, p: RootPose): void => {+ g.rotation.set(p.rx, p.ry, p.rz);+ g.scale.set(p.sx, p.sy, p.sz);+ g.position.set(p.px, p.py, p.pz);+};export function drawPhone3DCompositeWithZoom(c: CanvasRenderingContext2D,@@ -54,11 +64,12 @@ export function drawPhone3DCompositeWithZoom(imagePhoneCanvasRef, imagePhoneApiRef, canvasDimensions, imagePhoneDevice,imagePhoneScale, imagePhoneX, imagePhoneY, imagePhoneShadow, imagePhoneShadowColor,effectivePhoneMaskConfig, maskCompositeCanvasRef,- imagePhoneRootRef, motion3DForFrame,- cameraPoseForFrame,+ imagePhoneRootRef, motion3DForFrame, previewMotion3D, cameraPoseForFrame,} = ctx2;- const phoneGL = imagePhoneCanvasRef.current!;+ const phoneGL = imagePhoneCanvasRef.current;+ if (!phoneGL) return;+const domW = canvasDimensions?.width ?? canvasWidth;const pxScale = canvasWidth / domW;const zScale = zs.scale;@@ -72,97 +83,72 @@ export function drawPhone3DCompositeWithZoom(const baseW = deviceDims.width * imagePhoneScale * pxScale;const baseH = deviceDims.height * imagePhoneScale * pxScale;- // Apply 3D motion to the root group before high-quality render so the- // exported frame reflects the motion at this frame time. We capture the- // base transform on first use and restore it after rendering.- const root = imagePhoneRootRef?.current;const motion = motion3DForFrame ?? REST_MOCKUP_3D_MOTION;- const hasMotion = root && motion !== REST_MOCKUP_3D_MOTION;-- if (hasMotion && root) {- if (!cachedBase) {- cachedBase = {- rx: root.rotation.x, ry: root.rotation.y, rz: root.rotation.z,- sx: root.scale.x, sy: root.scale.y, sz: root.scale.z,- px: root.position.x, py: root.position.y, pz: root.position.z,- };- }- root.rotation.x = cachedBase.rx + motion.rotX;- root.rotation.y = cachedBase.ry + motion.rotY;- root.rotation.z = cachedBase.rz + motion.rotZ;- root.scale.x = cachedBase.sx * motion.scale;- root.scale.y = cachedBase.sy * motion.scale;- root.scale.z = cachedBase.sz * motion.scale;- root.position.x = cachedBase.px + motion.posX;- root.position.y = cachedBase.py + motion.posY;- root.position.z = cachedBase.pz + motion.posZ;- }-+ const preview = previewMotion3D ?? REST_MOCKUP_3D_MOTION;const shouldRender3DFrame = highQuality || cameraPoseForFrame !== undefined;- if (shouldRender3DFrame) {- imagePhoneApiRef.current?.renderAt(baseW, baseH, motion, cameraPoseForFrame);- }- c.save();- if (zScale !== 1) {- c.translate(pivotX, pivotY);- c.scale(zScale, zScale);- c.translate(-pivotX, -pivotY);- }- const hasBuiltInShadow = imagePhoneApiRef.current?.hasBuiltInShadow ?? false;- if (imagePhoneShadow > 0.01 && !hasBuiltInShadow) {- const sT = imagePhoneShadow * imagePhoneShadow;- const sBlur = sT * 60;- const sOpacity = sT * 0.7;- c.save();- c.globalAlpha = sOpacity;- c.filter = `blur(${Math.max(2, sBlur * 0.6) * pxScale}px)`;- c.beginPath();- c.ellipse(- baseCx,- baseCy + baseH / 2 + sBlur * 0.2 * pxScale,- baseW * (0.6 - sT * 0.1) / 2,- Math.max(4, sBlur * 0.55) * pxScale / 2,- 0, 0, Math.PI * 2- );- c.fillStyle = imagePhoneShadowColor;- c.fill();- c.restore();- }- if (imagePhoneShadow > 0.01) {- c.shadowColor = imagePhoneShadowColor;- c.shadowBlur = 28 * imagePhoneShadow * pxScale;- c.shadowOffsetX = 0;- c.shadowOffsetY = 18 * imagePhoneShadow * pxScale;+ const root = imagePhoneRootRef?.current ?? null;+ const saved = root ? readPose(root) : null;+ if (root && saved) {+ const ps = preview.scale !== 0 ? preview.scale : 1;+ writePose(root, {+ rx: saved.rx - preview.rotX + motion.rotX,+ ry: saved.ry - preview.rotY + motion.rotY,+ rz: saved.rz - preview.rotZ + motion.rotZ,+ sx: (saved.sx / ps) * motion.scale,+ sy: (saved.sy / ps) * motion.scale,+ sz: (saved.sz / ps) * motion.scale,+ px: saved.px - preview.posX + motion.posX,+ py: saved.py - preview.posY + motion.posY,+ pz: saved.pz - preview.posZ + motion.posZ,+ });}- drawMaskedImage(c, phoneGL, baseCx - baseW / 2, baseCy - baseH / 2, baseW, baseH, effectivePhoneMaskConfig, maskCompositeCanvasRef);+ c.save();+ try {+ if (shouldRender3DFrame) {+ imagePhoneApiRef.current?.renderAt(baseW, baseH, motion, cameraPoseForFrame);+ }- if (imagePhoneShadow > 0.01) {- c.shadowColor = "transparent";- c.shadowBlur = 0;- c.shadowOffsetY = 0;- }+ if (zScale !== 1) {+ c.translate(pivotX, pivotY);+ c.scale(zScale, zScale);+ c.translate(-pivotX, -pivotY);+ }- c.restore();+ const hasBuiltInShadow = imagePhoneApiRef.current?.hasBuiltInShadow ?? false;+ if (imagePhoneShadow > 0.01 && !hasBuiltInShadow) {+ const sT = imagePhoneShadow * imagePhoneShadow;+ const sBlur = sT * 60;+ c.save();+ c.globalAlpha = sT * 0.7;+ c.filter = `blur(${Math.max(2, sBlur * 0.6) * pxScale}px)`;+ c.beginPath();+ c.ellipse(+ baseCx,+ baseCy + baseH / 2 + sBlur * 0.2 * pxScale,+ (baseW * (0.6 - sT * 0.1)) / 2,+ (Math.max(4, sBlur * 0.55) * pxScale) / 2,+ 0, 0, Math.PI * 2,+ );+ c.fillStyle = imagePhoneShadowColor;+ c.fill();+ c.restore();+ }- if (shouldRender3DFrame) {- imagePhoneApiRef.current?.restorePreview();- }+ if (imagePhoneShadow > 0.01) {+ c.shadowColor = imagePhoneShadowColor;+ c.shadowBlur = 28 * imagePhoneShadow * pxScale;+ c.shadowOffsetX = 0;+ c.shadowOffsetY = 18 * imagePhoneShadow * pxScale;+ }- // Restore the root group's base transform after rendering so the live- // preview's Motion3DApplicator doesn't compound on our changes.- if (hasMotion && root && cachedBase) {- root.rotation.x = cachedBase.rx;- root.rotation.y = cachedBase.ry;- root.rotation.z = cachedBase.rz;- root.scale.x = cachedBase.sx;- root.scale.y = cachedBase.sy;- root.scale.z = cachedBase.sz;- root.position.x = cachedBase.px;- root.position.y = cachedBase.py;- root.position.z = cachedBase.pz;+ drawMaskedImage(c, phoneGL, baseCx - baseW / 2, baseCy - baseH / 2, baseW, baseH, effectivePhoneMaskConfig, maskCompositeCanvasRef);+ } finally {+ c.restore();+ if (shouldRender3DFrame) imagePhoneApiRef.current?.restorePreview();+ if (root && saved) writePose(root, saved);}-}+}\ No newline at end of file