Skip to content

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

feat: refactor camera position handling in 3D viewer components to use sphericalCameraPos for improved flexibility

CristianOlivera13d30e856dc91
diff --git a/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx b/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
index 9ac4b0f..27b14ec 100644
--- a/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/DoubleIPhone3DViewer.tsx
@@ -14,7 +14,7 @@ import {
applyTextureCover
} from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
@@ -59,7 +59,6 @@ const DEG = Math.PI / 180;
const PLACEHOLDER_PHONE_URL = "/images/mockups-3d/placeholder-phone.avif";
const MODEL_URL = "/models/double_iphone_13_pro.glb";
const DRACO_URL = "/draco/";
-const DEFAULT_CAMERA_POS: [number, number, number] = [0, 0, 1.5];
useGLTF.preload(MODEL_URL, DRACO_URL);
@@ -370,7 +369,7 @@ export function DoubleIPhoneScene({
onLoaded?.();
}, [onLoaded]);
- const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
+ const prevRotationRef = useRef<{ x: number; y: number } | null>({ x: initialRotationX, y: initialRotationY });
useEffect(() => {
if (isUserInteractingRef.current) return;
@@ -464,7 +463,7 @@ export function DoubleIPhoneScene({
return (
<>
- <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={DEFAULT_CAMERA_POS} zoom={zoom} />
+ <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={sphericalCameraPos(initialRotationX, initialRotationY)} zoom={zoom} />
<OrbitControls
ref={orbitRef}
enableZoom={false}
diff --git a/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx b/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
index 291a446..b0898a7 100644
--- a/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPadMini63DViewer.tsx
@@ -12,7 +12,7 @@ import {
applyTextureCover
} from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
@@ -54,7 +54,6 @@ const DEG = Math.PI / 180;
const PLACEHOLDER_IPAD_URL = "/images/mockups-3d/placeholder-phone.avif";
const MODEL_URL = "/models/ipad_mini_6_2021.glb";
const DRACO_URL = "/draco/";
-const DEFAULT_CAMERA_POS: [number, number, number] = [0, 0, 1.5];
const TARGET_W = 1040;
const TARGET_H = 1500;
@@ -336,7 +335,7 @@ export function IPadMiniScene({
onLoaded?.();
}, [onLoaded]);
- const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
+ const prevRotationRef = useRef<{ x: number; y: number } | null>({ x: initialRotationX, y: initialRotationY });
useEffect(() => {
if (isUserInteractingRef.current) return;
@@ -432,7 +431,7 @@ export function IPadMiniScene({
return (
<>
- <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={DEFAULT_CAMERA_POS} zoom={zoom} />
+ <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={sphericalCameraPos(initialRotationX, initialRotationY)} zoom={zoom} />
<OrbitControls
ref={orbitRef}
enableZoom={false}
diff --git a/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx b/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
index c426194..1453431 100644
--- a/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPhone13ProMax3DViewer.tsx
@@ -6,7 +6,7 @@ import { useEffect, useRef, useState, Suspense, useLayoutEffect, useMemo } from
import * as THREE from "three";
import { createCoverScreenCanvas, applyCropToImage, type ImageMaskConfigLike, applyTextureCover } from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
@@ -48,7 +48,6 @@ interface Props {
const TEX_W = 1284;
const TEX_H = 2778;
const PLACEHOLDER_PHONE_URL = "/images/mockups-3d/placeholder-phone.avif";
-const DEFAULT_CAMERA_POS: [number, number, number] = [0, 0, 1.5];
const DRACO_URL = "/draco/";
useGLTF.preload("/models/apple_iphone_13_pro_max.glb", DRACO_URL);
@@ -553,7 +552,7 @@ export function IPhone13ProMaxScene({
}, 1);
return (
<>
- <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={DEFAULT_CAMERA_POS} zoom={zoom} />
+ <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={sphericalCameraPos(initialRotationX, initialRotationY)} zoom={zoom} />
<Environment files={HDRI_FILES[environment as EnvironmentPreset]} environmentIntensity={glow} background={false} />
<OrbitControls
ref={orbitRef}
diff --git a/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx b/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
index 8b6f876..13f2b0c 100644
--- a/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/IPhone17ProMax3DViewer.tsx
@@ -6,7 +6,7 @@ import { useEffect, useRef, useState, Suspense, useLayoutEffect, useMemo, useCal
import * as THREE from "three";
import { createCoverScreenCanvas, applyCropToImage, parseShadowColor, type ImageMaskConfigLike, applyTextureCover } from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
@@ -48,7 +48,6 @@ interface Props {
const TEX_W = 1284;
const TEX_H = 2778;
const PLACEHOLDER_PHONE_URL = "/images/mockups-3d/placeholder-phone.avif";
-const DEFAULT_CAMERA_POS: [number, number, number] = [0, 0, 1.5];
const DRACO_URL = "/draco/";
useGLTF.preload("/models/iphone-17-pro-max.glb", DRACO_URL);
@@ -364,7 +363,7 @@ export function IPhone17ProMaxScene({
};
}, []);
- const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
+ const prevRotationRef = useRef<{ x: number; y: number } | null>({ x: initialRotationX, y: initialRotationY });
useEffect(() => {
if (prevRotationRef.current?.x === initialRotationX && prevRotationRef.current?.y === initialRotationY) return;
const id = setTimeout(() => {
@@ -456,7 +455,7 @@ export function IPhone17ProMaxScene({
fov={40}
near={0.01}
far={100}
- position={DEFAULT_CAMERA_POS}
+ position={sphericalCameraPos(initialRotationX, initialRotationY)}
zoom={zoom}
/>
<Environment files={HDRI_FILES[environment as EnvironmentPreset]} environmentIntensity={glow} background={false} />
diff --git a/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx b/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
index 55bfc03..3dac289 100644
--- a/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/Laptop3DViewer.tsx
@@ -13,7 +13,7 @@ import {
applyTextureCover
} from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
@@ -470,7 +470,7 @@ export function LaptopScene({
};
}, [gl]);
- const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
+ const prevRotationRef = useRef<{ x: number; y: number } | null>({ x: initialRotationX, y: initialRotationY });
useEffect(() => {
if (isUserInteractingRef.current) return;
@@ -571,7 +571,7 @@ export function LaptopScene({
return (
<>
- <PerspectiveCamera ref={cameraRef} makeDefault fov={CAM_FOV} near={10} far={1000} position={[0, 0, CAM_RADIUS / zoom]} />
+ <PerspectiveCamera ref={cameraRef} makeDefault fov={CAM_FOV} near={10} far={1000} position={sphericalCameraPos(initialRotationX, initialRotationY, CAM_RADIUS / zoom)} />
<OrbitControls
ref={orbitRef}
enableZoom={false}
diff --git a/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx b/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
index ca5a98f..bf3a139 100644
--- a/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
+++ b/app/components/ui/editor/mockups-3d/Phone3DViewer.tsx
@@ -19,7 +19,7 @@ import {
applyTextureCover
} from "@/lib/phone3d.utils";
import type { OrbitControls as OrbitControlsType } from 'three-stdlib';
-import { EnvironmentPreset, HDRI_FILES } from "@/lib/viewer-controls3d";
+import { EnvironmentPreset, HDRI_FILES, sphericalCameraPos } from "@/lib/viewer-controls3d";
import { GetMediaMaskStyles } from "@/lib/media-mask.utils";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
@@ -63,6 +63,7 @@ interface Props {
}
const DEG = Math.PI / 180;
+
const specificGltfCache = new Map<string, Promise<THREE.Group>>();
const PLACEHOLDER_PHONE_URL = "/images/mockups-3d/placeholder-phone.avif";
@@ -573,7 +574,7 @@ export function Phone3DScene({
};
}, [modelUrl, onLoaded]);
- const prevRotationRef = useRef<{ x: number; y: number } | null>(null);
+ const prevRotationRef = useRef<{ x: number; y: number } | null>({ x: initialRotationX, y: initialRotationY });
useEffect(() => {
if (isUserInteractingRef.current) return;
@@ -664,7 +665,7 @@ export function Phone3DScene({
return (
<>
- <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={[0, 0, 1.5]} zoom={zoom} />
+ <PerspectiveCamera ref={cameraRef} makeDefault fov={40} near={0.01} far={100} position={sphericalCameraPos(initialRotationX, initialRotationY)} zoom={zoom} />
<OrbitControls
ref={orbitRef}
enableZoom={false}