Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: refactor invalidate handling in 3D viewer components for improved performance

CristianOlivera1a8ada60628d7
diff --git a/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx b/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
index de1bb8c..51a2669 100644
--- a/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
@@ -101,6 +101,11 @@ export function DoubleIPhoneScene({
const outlinePassRef = useRef<OutlinePass | null>(null);
const isUserInteractingRef = useRef(false);
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, [invalidate]);
+
const areAnglesEqual = useCallback((angles1: { x: number; y: number } | null, angles2: { x: number; y: number }) => {
if (!angles1) return false;
return Math.abs(angles1.x - angles2.x) < EPSILON && Math.abs(angles1.y - angles2.y) < EPSILON;
@@ -172,7 +177,7 @@ export function DoubleIPhoneScene({
composerRef.current?.setSize(freshW, freshH);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(freshW, freshH);
- invalidate();
+ invalidateRef.current();
},
hasBuiltInShadow: false,
getVisualSize: () => {
@@ -182,7 +187,7 @@ export function DoubleIPhoneScene({
capturedOnApi?.(api);
return () => capturedOnApi?.(null);
- }, [gl, scene, camera, cameraRef, invalidate, zoom]);
+ }, [gl, scene, camera, cameraRef, zoom]);
const applyTexture = useCallback(() => {
if (videoElement) return;
@@ -230,16 +235,16 @@ export function DoubleIPhoneScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = targetImgUrl;
lastLoadedCropKeyRef.current = cropKey;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
if (mat.map) mat.map.dispose();
mat.color.set(0x111111);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
};
img.src = targetImgUrl;
- }, [imageUrl, imageMaskConfig, cropArea, gl, videoElement, invalidate]);
+ }, [imageUrl, imageMaskConfig, cropArea, gl, videoElement]);
const applyVideoTextureIfReady = useCallback(() => {
const mat = screenMatRef.current;
@@ -251,9 +256,9 @@ export function DoubleIPhoneScene({
mat.map = tex;
mat.color.set(0xffffff);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
}
- }, [invalidate]);
+ }, []);
useEffect(() => {
if (!videoElement) {
@@ -347,7 +352,7 @@ export function DoubleIPhoneScene({
if (!isMounted) return;
applyTextureRef.current();
onLoaded?.();
- invalidate();
+ invalidateRef.current();
}, 50);
return () => {
@@ -359,7 +364,7 @@ export function DoubleIPhoneScene({
screenMatRef.current.dispose();
}
};
- }, [gltf.scene, applyVideoTextureIfReady, onLoaded, invalidate]);
+ }, [gltf.scene, onLoaded]);
const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
@@ -383,19 +388,19 @@ export function DoubleIPhoneScene({
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = currentAngles;
}, 0);
return () => clearTimeout(id);
- }, [initialRotationX, initialRotationY, zoom, invalidate, areAnglesEqual]);
+ }, [initialRotationX, initialRotationY, zoom, areAnglesEqual]);
useEffect(() => {
if (rootRef.current) {
rootRef.current.rotation.z = initialRotationZ * DEG;
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, invalidate]);
+ }, [initialRotationZ]);
useEffect(() => {
const composer = new EffectComposer(gl);
@@ -419,7 +424,7 @@ export function DoubleIPhoneScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -427,14 +432,14 @@ export function DoubleIPhoneScene({
composerRef.current = null;
outlinePassRef.current = null;
};
- }, [gl, scene, camera, size.width, size.height, invalidate]);
+ }, [gl, scene, camera, size.width, size.height]);
useEffect(() => {
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -446,8 +451,8 @@ export function DoubleIPhoneScene({
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
- }, [isSelected, isHovered, invalidate, rootRef]);
+ invalidateRef.current();
+ }, [isSelected, isHovered, rootRef]);
useFrame(() => {
composerRef.current?.render();
diff --git a/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx b/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
index 6ffd602..e5ff927 100644
--- a/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
@@ -78,6 +78,11 @@ export function IPadMiniScene({
const outlinePassRef = useRef<OutlinePass | null>(null);
const isUserInteractingRef = useRef(false);
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, [invalidate]);
+
useLayoutEffect(() => {
onApiRef.current = onApi;
});
@@ -122,13 +127,13 @@ export function IPadMiniScene({
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(freshW, freshH);
- invalidate();
+ invalidateRef.current();
},
hasBuiltInShadow: true,
};
capturedOnApi?.(api);
return () => capturedOnApi?.(null);
- }, [gl, scene, camera, cameraRef, invalidate]);
+ }, [gl, scene, camera, cameraRef]);
const applyTexture = useCallback(() => {
if (videoElement) return;
@@ -176,16 +181,16 @@ export function IPadMiniScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = targetImgUrl;
lastLoadedCropKeyRef.current = cropKey;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
if (mat.map) mat.map.dispose();
mat.color.set(0x111111);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
};
img.src = targetImgUrl;
- }, [imageUrl, cropArea, gl, videoElement, invalidate]);
+ }, [imageUrl, cropArea, gl, videoElement]);
const applyVideoTextureIfReady = useCallback(() => {
const mat = screenMatRef.current;
@@ -201,9 +206,9 @@ export function IPadMiniScene({
mat.map = tex;
mat.color.set(0xffffff);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
}
- }, [invalidate]);
+ }, []);
useEffect(() => {
if (!videoElement) {
@@ -313,7 +318,7 @@ export function IPadMiniScene({
if (!isMounted) return;
applyTextureRef.current();
onLoaded?.();
- invalidate();
+ invalidateRef.current();
}, 50);
return () => {
@@ -325,7 +330,7 @@ export function IPadMiniScene({
screenMatRef.current.dispose();
}
};
- }, [gltf.scene, applyVideoTextureIfReady, onLoaded, invalidate]);
+ }, [gltf.scene, onLoaded]);
const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
@@ -347,18 +352,18 @@ export function IPadMiniScene({
const theta = initialRotationY * DEG;
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = { x: initialRotationX, y: initialRotationY };
}, 0);
return () => clearTimeout(id);
- }, [initialRotationX, initialRotationY, zoom, invalidate]);
+ }, [initialRotationX, initialRotationY, zoom]);
useEffect(() => {
if (rootRef.current) {
rootRef.current.rotation.z = initialRotationZ * DEG;
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, rootRef, invalidate]);
+ }, [initialRotationZ, rootRef]);
useEffect(() => {
const composer = new EffectComposer(gl);
@@ -382,7 +387,7 @@ export function IPadMiniScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -390,14 +395,14 @@ export function IPadMiniScene({
composerRef.current = null;
outlinePassRef.current = null;
};
- }, [gl, scene, camera, size.width, size.height, invalidate]);
+ }, [gl, scene, camera, size.width, size.height]);
useEffect(() => {
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -409,8 +414,8 @@ export function IPadMiniScene({
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
- }, [isSelected, isHovered, invalidate, rootRef]);
+ invalidateRef.current();
+ }, [isSelected, isHovered, rootRef]);
useFrame(() => {
composerRef.current?.render();
diff --git a/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx b/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
index f585727..c426194 100644
--- a/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
@@ -169,6 +169,10 @@ export function IPhone13ProMaxScene({
const onApiRef = useRef(onApi);
const composerRef = useRef<EffectComposer | null>(null);
const outlinePassRef = useRef<OutlinePass | null>(null);
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, []);
useLayoutEffect(() => {
onApiRef.current = onApi;
});
@@ -209,7 +213,7 @@ export function IPhone13ProMaxScene({
composerRef.current?.setSize(freshW, freshH);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(freshW, freshH);
- invalidate();
+ invalidateRef.current();
},
getVisualSize: () => {
const canvas = gl.domElement;
@@ -370,7 +374,7 @@ export function IPhone13ProMaxScene({
tex.anisotropy = gl.capabilities.getMaxAnisotropy();
mat.map = tex;
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
lastLoadedImageUrlRef.current = imageUrl;
lastLoadedMaskKeyRef.current = maskKey;
lastLoadedCropKeyRef.current = cropKey;
@@ -402,7 +406,7 @@ export function IPhone13ProMaxScene({
const theta = initialRotationY * DEG;
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = { x: initialRotationX, y: initialRotationY };
}, 0);
return () => clearTimeout(id);
@@ -412,9 +416,9 @@ export function IPhone13ProMaxScene({
const root = rootRef.current;
if (root) {
root.rotation.z = initialRotationZ * (Math.PI / 180);
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, invalidate]);
+ }, [initialRotationZ]);
const shadowT = Math.max(0, Math.min(1, shadowIntensity));
const showContactShadow = shadowT > 0.01;
@@ -516,7 +520,7 @@ export function IPhone13ProMaxScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -530,8 +534,8 @@ export function IPhone13ProMaxScene({
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -541,8 +545,8 @@ export function IPhone13ProMaxScene({
const color = isSelected ? 0x3b82f6 : 0xffffff;
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
- }, [isSelected, isHovered, invalidate]);
+ invalidateRef.current();
+ }, [isSelected, isHovered]);
useFrame(() => {
composerRef.current?.render();
diff --git a/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx b/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
index f13e4a0..8b6f876 100644
--- a/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
@@ -111,7 +111,11 @@ export function IPhone17ProMaxScene({
const composerRef = useRef<EffectComposer | null>(null);
const outlinePassRef = useRef<OutlinePass | null>(null);
- useLayoutEffect(() => {
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, []);
+ useLayoutEffect(() => {
onApiRef.current = onApi;
});
@@ -140,8 +144,8 @@ export function IPhone17ProMaxScene({
}
}
});
- invalidate();
- }, [clonedScene, invalidate]);
+ invalidateRef.current();
+ }, [clonedScene]);
useEffect(() => {
const capturedOnApi = onApiRef.current;
@@ -191,7 +195,7 @@ export function IPhone17ProMaxScene({
composerRef.current?.setSize(freshW, freshH);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(freshW, freshH);
- invalidate();
+ invalidateRef.current();
},
hasBuiltInShadow: true,
@@ -202,7 +206,7 @@ export function IPhone17ProMaxScene({
capturedOnApi?.(api);
return () => capturedOnApi?.(null);
- }, [gl, scene, camera, cameraRef, invalidate]);
+ }, [gl, scene, camera, cameraRef]);
useEffect(() => {
onLoaded?.();
}, [onLoaded]);
@@ -233,7 +237,7 @@ export function IPhone17ProMaxScene({
TEX_H,
cropArea
);
- invalidate();
+ invalidateRef.current();
};
if (videoElement.readyState >= 1) {
@@ -257,7 +261,7 @@ export function IPhone17ProMaxScene({
mat.color.set(0xffffff);
mat.needsUpdate = true;
if (mat.map) mat.map.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
};
applyVideoTex();
@@ -268,7 +272,7 @@ export function IPhone17ProMaxScene({
}
tex.dispose();
};
- }, [videoElement, cropArea, invalidate]);
+ }, [videoElement, cropArea]);
useEffect(() => {
const mat = wallpaperMatRef.current;
@@ -303,7 +307,7 @@ export function IPhone17ProMaxScene({
lastLoadedImageUrlRef.current = placeholderKey;
lastLoadedMaskKeyRef.current = null;
lastLoadedCropKeyRef.current = null;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
if (mat.map) {
@@ -313,7 +317,7 @@ export function IPhone17ProMaxScene({
mat.color.set(0x1a1a1a);
mat.needsUpdate = true;
lastLoadedImageUrlRef.current = placeholderKey;
- invalidate();
+ invalidateRef.current();
};
img.src = PLACEHOLDER_PHONE_URL;
return;
@@ -344,13 +348,13 @@ export function IPhone17ProMaxScene({
mat.map = tex;
mat.color.set(0xffffff);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
lastLoadedImageUrlRef.current = imageUrl;
lastLoadedMaskKeyRef.current = maskKey;
lastLoadedCropKeyRef.current = cropKey;
};
img.src = imageUrl;
- }, [imageUrl, imageMaskConfig, cropArea, gl, videoElement, invalidate]);
+ }, [imageUrl, imageMaskConfig, cropArea, gl, videoElement]);
useEffect(() => {
return () => {
@@ -372,19 +376,19 @@ export function IPhone17ProMaxScene({
const theta = initialRotationY * DEG;
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = { x: initialRotationX, y: initialRotationY };
}, 0);
return () => clearTimeout(id);
- }, [initialRotationX, initialRotationY, zoom, invalidate]);
+ }, [initialRotationX, initialRotationY, zoom]);
useEffect(() => {
const root = rootRef.current;
if (root) {
root.rotation.z = initialRotationZ * (Math.PI / 180);
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, invalidate]);
+ }, [initialRotationZ]);
useEffect(() => {
const composer = new EffectComposer(gl);
@@ -407,7 +411,7 @@ export function IPhone17ProMaxScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -415,14 +419,14 @@ export function IPhone17ProMaxScene({
composerRef.current = null;
outlinePassRef.current = null;
};
- }, [gl, scene, camera, size.width, size.height, invalidate]);
+ }, [gl, scene, camera, size.width, size.height]);
useEffect(() => {
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -432,8 +436,8 @@ export function IPhone17ProMaxScene({
const color = isSelected ? 0x3b82f6 : 0xffffff;
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
- }, [isSelected, isHovered, invalidate]);
+ invalidateRef.current();
+ }, [isSelected, isHovered]);
useFrame(() => {
composerRef.current?.render();
diff --git a/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx b/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
index 4856355..55bfc03 100644
--- a/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
@@ -103,6 +103,10 @@ export function LaptopScene({
const outlinePassRef = useRef<OutlinePass | null>(null);
const isUserInteractingRef = useRef(false);
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, []);
useLayoutEffect(() => {
onApiRef.current = onApi;
});
@@ -149,7 +153,7 @@ export function LaptopScene({
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(freshW, freshH);
- invalidate();
+ invalidateRef.current();
},
hasBuiltInShadow: false,
getVisualSize: () => {
@@ -206,7 +210,7 @@ export function LaptopScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = placeholderKey;
lastLoadedCropKeyRef.current = null;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
lastLoadedUrlRef.current = placeholderKey;
@@ -255,14 +259,14 @@ export function LaptopScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = imageUrl;
lastLoadedCropKeyRef.current = cropKey;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
lastLoadedUrlRef.current = imageUrl;
lastLoadedCropKeyRef.current = cropKey;
};
img.src = imageUrl;
- }, [imageUrl, cropArea, gl, videoElement, invalidate]);
+ }, [imageUrl, cropArea, gl, videoElement]);
const applyVideoTextureIfReady = useCallback(() => {
const mat = screenMatRef.current;
@@ -274,9 +278,9 @@ export function LaptopScene({
mat.map = tex;
mat.color.set(0xffffff);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
}
- }, [invalidate]);
+ }, []);
useEffect(() => {
if (!videoElement) {
@@ -373,7 +377,7 @@ export function LaptopScene({
tex.anisotropy = gl.capabilities.getMaxAnisotropy();
keyboardMaterial.alphaMap = tex;
keyboardMaterial.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
});
const finalizeSetup = (group: THREE.Group) => {
@@ -383,7 +387,7 @@ export function LaptopScene({
if (!isMounted) return;
applyTextureRef.current();
onLoadedRef.current?.();
- invalidate();
+ invalidateRef.current();
}, 50);
};
@@ -464,7 +468,7 @@ export function LaptopScene({
screenMatRef.current.dispose();
}
};
- }, [gl, invalidate]);
+ }, [gl]);
const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
@@ -486,18 +490,18 @@ export function LaptopScene({
const theta = initialRotationY * DEG;
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = { x: initialRotationX, y: initialRotationY };
}, 0);
return () => clearTimeout(id);
- }, [initialRotationX, initialRotationY, zoom, invalidate]);
+ }, [initialRotationX, initialRotationY, zoom]);
useEffect(() => {
if (rootRef.current) {
rootRef.current.rotation.z = initialRotationZ * DEG;
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, modelGroup, invalidate]);
+ }, [initialRotationZ, modelGroup]);
useEffect(() => {
const lid = lidGroupRef.current;
@@ -506,8 +510,8 @@ export function LaptopScene({
const t = Math.max(0, Math.min(1, openingProgress));
lid.rotation.x = lerp(LID_CLOSED_X, LID_OPEN_X, t);
mat.opacity = 0.96 * t;
- invalidate();
- }, [openingProgress, modelGroup, invalidate]);
+ invalidateRef.current();
+ }, [openingProgress, modelGroup]);
useEffect(() => {
const composer = new EffectComposer(gl);
@@ -531,7 +535,7 @@ export function LaptopScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -539,14 +543,14 @@ export function LaptopScene({
composerRef.current = null;
outlinePassRef.current = null;
};
- }, [gl, scene, camera, size.width, size.height, invalidate]);
+ }, [gl, scene, camera, size.width, size.height]);
useEffect(() => {
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -558,7 +562,7 @@ export function LaptopScene({
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
+ invalidateRef.current();
}, [isSelected, isHovered, invalidate, rootRef]);
useFrame(() => {
diff --git a/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx b/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx
index 347b448..c89f96f 100644
--- a/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx
+++ b/app/components/ui/editor/mockups-3d/Mockup3DStage.tsx
@@ -67,12 +67,20 @@ interface StageProps extends Mockup3DStageProps {
function Motion3DApplicator({
rootRef,
motionTransform,
+ device,
}: {
rootRef: React.MutableRefObject<THREE.Group | null>;
motionTransform: Mockup3DMotionTransform;
+ device: ImageDeviceId;
}) {
const baseRef = useRef<{ rx: number; ry: number; rz: number; sx: number; sy: number; sz: number; px: number; py: number; pz: number } | null>(null);
+ // Reset the captured base transform whenever the device changes so the
+ // new model's own scale/rotation/position is re-captured fresh.
+ useEffect(() => {
+ baseRef.current = null;
+ }, [device]);
+
useEffect(() => {
const root = rootRef.current;
if (!root || baseRef.current) return;
@@ -87,7 +95,7 @@ function Motion3DApplicator({
py: root.position.y,
pz: root.position.z,
};
- }, [rootRef]);
+ }, [rootRef, device]);
useFrame(() => {
const root = rootRef.current;
@@ -175,7 +183,7 @@ export function Mockup3DStage({ device, rootRef: externalRootRef, cameraRef: ext
handleMount(gl.domElement);
}}
>
- <Motion3DApplicator rootRef={rootRef} motionTransform={props.motionTransform ?? REST_MOCKUP_3D_MOTION} />
+ <Motion3DApplicator rootRef={rootRef} motionTransform={props.motionTransform ?? REST_MOCKUP_3D_MOTION} device={device} />
<Suspense fallback={null}>
{device === "iphone-13-pro-max" && (
<IPhone13ProMaxScene {...props} rootRef={rootRef} cameraRef={cameraRef} onLoaded={markLoaded} />
diff --git a/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx b/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
index ab21cab..ca5a98f 100644
--- a/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
@@ -203,6 +203,11 @@ export function Phone3DScene({
return () => capturedOnApi?.(null);
}, [gl, scene, camera, cameraRef, invalidate, zoom]);
+ const invalidateRef = useRef(invalidate);
+ useEffect(() => {
+ invalidateRef.current = invalidate;
+ }, [invalidate]);
+
const applyTexture = useCallback(() => {
if (videoElement) return;
const mat = screenMatRef.current;
@@ -255,7 +260,7 @@ export function Phone3DScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = placeholderKey;
lastLoadedCropKeyRef.current = null;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
const currentMat = screenMatRef.current;
@@ -267,7 +272,7 @@ export function Phone3DScene({
currentMat.color.set(0x1a1a1a);
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = placeholderKey;
- invalidate();
+ invalidateRef.current();
};
img.src = PLACEHOLDER_PHONE_URL;
return;
@@ -331,7 +336,7 @@ export function Phone3DScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = imageUrl;
lastLoadedCropKeyRef.current = cropKey;
- invalidate();
+ invalidateRef.current();
};
img.onerror = () => {
const currentMat = screenMatRef.current;
@@ -344,10 +349,10 @@ export function Phone3DScene({
currentMat.needsUpdate = true;
lastLoadedUrlRef.current = imageUrl;
lastLoadedCropKeyRef.current = cropKey;
- invalidate();
+ invalidateRef.current();
};
img.src = imageUrl;
- }, [imageUrl, imageMaskConfig, cropArea, modelUrl, gl, videoElement, invalidate]);
+ }, [imageUrl, imageMaskConfig, cropArea, modelUrl, gl, videoElement]);
const applyVideoTextureIfReady = useCallback(() => {
const mat = screenMatRef.current;
@@ -363,9 +368,9 @@ export function Phone3DScene({
mat.map = tex;
mat.color.set(0xffffff);
mat.needsUpdate = true;
- invalidate();
+ invalidateRef.current();
}
- }, [modelUrl, invalidate]);
+ }, [modelUrl]);
useEffect(() => {
if (!videoElement) {
@@ -433,6 +438,11 @@ export function Phone3DScene({
applyTextureRef.current = applyTexture;
}, [applyTexture]);
+ const applyVideoTextureIfReadyRef = useRef(applyVideoTextureIfReady);
+ useEffect(() => {
+ applyVideoTextureIfReadyRef.current = applyVideoTextureIfReady;
+ }, [applyVideoTextureIfReady]);
+
useEffect(() => {
applyTexture();
}, [applyTexture]);
@@ -441,6 +451,7 @@ export function Phone3DScene({
let isMounted = true;
const device = getDeviceFromModelUrl(modelUrl);
const isDefaultPhone = device === "phone";
+ const invalidateNow = invalidateRef.current;
const finalizeSetup = (group: THREE.Group) => {
if (!isMounted) return;
@@ -449,7 +460,7 @@ export function Phone3DScene({
if (!isMounted) return;
applyTextureRef.current();
onLoaded?.();
- invalidate();
+ invalidateNow();
}, 50);
};
@@ -487,7 +498,7 @@ export function Phone3DScene({
child.material = basicMat;
child.renderOrder = 10;
screenMatRef.current = basicMat;
- applyVideoTextureIfReady();
+ applyVideoTextureIfReadyRef.current();
} else if (mat.isMeshStandardMaterial) {
applyMetalMaterial(mat, matName);
}
@@ -520,7 +531,7 @@ export function Phone3DScene({
depthWrite: false,
});
screenMatRef.current = basicMat;
- applyVideoTextureIfReady();
+ applyVideoTextureIfReadyRef.current();
const hw = planeW / 2;
const hh = planeH / 2;
@@ -560,7 +571,7 @@ export function Phone3DScene({
return () => {
isMounted = false;
};
- }, [modelUrl, applyVideoTextureIfReady, onLoaded, invalidate]);
+ }, [modelUrl, onLoaded]);
const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
@@ -582,18 +593,18 @@ export function Phone3DScene({
const theta = initialRotationY * DEG;
orbit.object.position.setFromSphericalCoords(radius, phi, theta);
orbit.update();
- invalidate();
+ invalidateRef.current();
prevRotationRef.current = { x: initialRotationX, y: initialRotationY };
}, 0);
return () => clearTimeout(id);
- }, [initialRotationX, initialRotationY, invalidate]);
+ }, [initialRotationX, initialRotationY]);
useEffect(() => {
if (rootRef.current) {
rootRef.current.rotation.z = initialRotationZ * DEG;
- invalidate();
+ invalidateRef.current();
}
- }, [initialRotationZ, invalidate]);
+ }, [initialRotationZ]);
useEffect(() => {
const composer = new EffectComposer(gl);
@@ -617,7 +628,7 @@ export function Phone3DScene({
composerRef.current = composer;
outlinePassRef.current = outlinePass;
- invalidate();
+ invalidateRef.current();
return () => {
outlinePass.dispose();
@@ -625,14 +636,14 @@ export function Phone3DScene({
composerRef.current = null;
outlinePassRef.current = null;
};
- }, [gl, scene, camera, size.width, size.height, invalidate]);
+ }, [gl, scene, camera, size.width, size.height]);
useEffect(() => {
composerRef.current?.setSize(size.width, size.height);
composerRef.current?.setPixelRatio(gl.getPixelRatio());
outlinePassRef.current?.resolution.set(size.width, size.height);
- invalidate();
- }, [size, gl, invalidate]);
+ invalidateRef.current();
+ }, [size, gl]);
useEffect(() => {
const outlinePass = outlinePassRef.current;
@@ -644,8 +655,8 @@ export function Phone3DScene({
outlinePass.visibleEdgeColor.set(color);
outlinePass.hiddenEdgeColor.set(color);
- invalidate();
- }, [isSelected, isHovered, invalidate, rootRef]);
+ invalidateRef.current();
+ }, [isSelected, isHovered, rootRef]);
useFrame(() => {
composerRef.current?.render();