mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-17 15:15:17 +00:00
ui-pagescene: lift PageEditor + PageScene shell into lib as <Stage3D />
Final step of the Stage3D migration. The lib now exposes a single
top-level component:
<Stage3D adapter={adapter} sceneToken={...} cacheJsonString={...}
onSceneSerialized={...} />
…that owns the EngineProvider, body composition (canvas, outliner,
control panel, prompt box, model selector, controls, etc.), and
the React-lifecycle-driven set3DPageMounted. Hosts only build their
own PageSceneAdapter and decide where to stash the cache string.
Lib additions:
- src/lib/Stage3D.tsx — top-level shell (provider + drag/loading
layer + body)
- src/lib/Stage3DBody.tsx — ex-PageEditor; consumes adapter for
uploadImage / uploadPlaneFromMediaToken / getViewportSize
- Public re-export of Stage3D + Stage3DProps
Apps changes:
- pages/PageScene/PageScene.tsx reduced to a 50-line wrapper that
builds the Tauri adapter, owns the host-specific concerns
(tab-cache via useTabStore, URL ↔ scene-token sync via react-
router-dom, currentUserToken mirror), and renders <Stage3D />
- pages/PageScene/PageEditor.tsx — deleted (now Stage3DBody in lib)
Apps PageScene/ is now exactly three files, all genuinely host-
specific:
- PageScene.tsx (Tauri-flavored wrapper)
- useTauriPageSceneAdapter.tsx (Tauri-bound adapter implementation)
- index.tsx (exports PageScene)
The artcraft-website can now mount the same 3D editor by writing its
own ~50-line wrapper + a useWebPageSceneAdapter, with zero duplicated
editor body or controls.
Typecheck stays at 47, lib build clean.
This commit is contained in:
1 parent
d3b0a0d78c
commit
42e1787576
7 files changed
+235
-226
No files matched your search
@@ -1,39 +1,21 @@
|
||||
// 3D editor route — mounted by MainApp's tab switch only when
|
||||
// activeTabId === "3D". React mount/unmount drives the engine
|
||||
// lifecycle: provider mounts → engine constructs once canvas refs
|
||||
// are populated; provider unmounts → engine tears down via the
|
||||
// EngineProvider cleanup. The host's tab cache (useTabStore) holds
|
||||
// the serialized scene JSON across mount/unmount so we don't lose
|
||||
// scene state when the user briefly visits another tab.
|
||||
// Artcraft Tauri-specific 3D editor route. Mounts the lib's Stage3D
|
||||
// with a Tauri-built adapter; manages tab-cache and the URL ↔ scene
|
||||
// token sync (both are host-platform concerns the lib stays out of).
|
||||
//
|
||||
// The website host mounts the same Stage3D with a web-built adapter
|
||||
// and its own router-flavored equivalents of these effects.
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useLocation, useNavigate, useParams } from "react-router-dom";
|
||||
import { useSignalEffect } from "@preact/signals-react/runtime";
|
||||
import {
|
||||
DragComponent,
|
||||
EditorLoadingBar,
|
||||
EngineProvider,
|
||||
PrecisionSelector,
|
||||
usePageSceneStore,
|
||||
} from "@storyteller/ui-pagescene";
|
||||
import { Stage3D, usePageSceneStore } from "@storyteller/ui-pagescene";
|
||||
import { useTabStore } from "~/pages/Stores/TabState";
|
||||
import { authentication, scene } from "~/signals";
|
||||
import { getCurrentLocationWithoutParams } from "~/utilities";
|
||||
import { PageEditor } from "~/pages/PageScene/PageEditor";
|
||||
import { useTauriPageSceneAdapter } from "./useTauriPageSceneAdapter";
|
||||
|
||||
export const PageScene = ({ sceneToken }: { sceneToken?: string }) => {
|
||||
// Route-aware bookkeeping — lifted out of ControlsTopButtons so the
|
||||
// lib stays router-agnostic. Keeps the URL in sync with the
|
||||
// currently loaded scene's token so a refresh / share preserves
|
||||
// the scene selection.
|
||||
const params = useParams();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
// Tab-cache plumbing. The provider only knows about its own React
|
||||
// lifecycle; the host decides where the in-memory cache string
|
||||
// lives. Reading the current value once on render is fine — the
|
||||
// provider snapshots it on mount, and the cache is per-mount.
|
||||
// Tab-cache plumbing. Stage3D is single-instance and tab-agnostic;
|
||||
// the host decides where the in-memory cache string lives.
|
||||
const tabStore = useTabStore();
|
||||
const cacheJsonString = tabStore.getTabData("3D") as string | undefined;
|
||||
const onSceneSerialized = (json: string) => {
|
||||
@@ -46,29 +28,19 @@ export const PageScene = ({ sceneToken }: { sceneToken?: string }) => {
|
||||
onSceneSerialized,
|
||||
});
|
||||
|
||||
// The 3D-page-mounted flag is read by the engine's remountEngine()
|
||||
// gate. With MainApp's tab switch, our React mount IS the signal —
|
||||
// no need for callers (TopBar, appMenu, ImageTo3DExperience) to
|
||||
// imperatively flip it.
|
||||
useEffect(() => {
|
||||
usePageSceneStore.getState().set3DPageMounted(true);
|
||||
return () => {
|
||||
usePageSceneStore.getState().set3DPageMounted(false);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Mirror the host's authentication signal into the lib store so
|
||||
// lib-resident UI (ControlsTopButtons) can do ownership permission
|
||||
// checks without depending on the host's signal system.
|
||||
// ControlsTopButtons can do ownership permission checks reactively.
|
||||
useSignalEffect(() => {
|
||||
usePageSceneStore
|
||||
.getState()
|
||||
.setCurrentUserToken(authentication.userInfo.value?.user_token);
|
||||
});
|
||||
|
||||
// URL ↔ loaded scene sync. Mirrors the previous useSignalEffect
|
||||
// that lived inside ControlsTopButtons; lives here so the lib
|
||||
// doesn't need react-router-dom.
|
||||
// URL ↔ loaded scene sync. Lives in the host wrapper so the lib
|
||||
// stays router-agnostic.
|
||||
const params = useParams();
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
useSignalEffect(() => {
|
||||
if (scene.value.isInitializing) return;
|
||||
const currentLocation = getCurrentLocationWithoutParams(
|
||||
@@ -89,16 +61,11 @@ export const PageScene = ({ sceneToken }: { sceneToken?: string }) => {
|
||||
});
|
||||
|
||||
return (
|
||||
<EngineProvider
|
||||
sceneToken={sceneToken}
|
||||
<Stage3D
|
||||
adapter={adapter}
|
||||
sceneToken={sceneToken}
|
||||
cacheJsonString={cacheJsonString}
|
||||
onSceneSerialized={onSceneSerialized}
|
||||
>
|
||||
<PageEditor />
|
||||
<DragComponent />
|
||||
<PrecisionSelector />
|
||||
<EditorLoadingBar />
|
||||
</EngineProvider>
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -1 +1 @@
|
||||
{"fileNames":["../../node_modules/typescript/lib/lib.es5.d.ts","../../node_modules/typescript/lib/lib.es2015.d.ts","../../node_modules/typescript/lib/lib.es2016.d.ts","../../node_modules/typescript/lib/lib.es2017.d.ts","../../node_modules/typescript/lib/lib.es2018.d.ts","../../node_modules/typescript/lib/lib.es2019.d.ts","../../node_modules/typescript/lib/lib.es2020.d.ts","../../node_modules/typescript/lib/lib.es2021.d.ts","../../node_modules/typescript/lib/lib.es2022.d.ts","../../node_modules/typescript/lib/lib.es2023.d.ts","../../node_modules/typescript/lib/lib.es2024.d.ts","../../node_modules/typescript/lib/lib.esnext.d.ts","../../node_modules/typescript/lib/lib.dom.d.ts","../../node_modules/typescript/lib/lib.dom.iterable.d.ts","../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../node_modules/typescript/lib/lib.es2016.intl.d.ts","../../node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../../node_modules/typescript/lib/lib.es2017.date.d.ts","../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../node_modules/typescript/lib/lib.es2019.intl.d.ts","../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../node_modules/typescript/lib/lib.es2020.date.d.ts","../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../node_modules/typescript/lib/lib.es2020.number.d.ts","../../node_modules/typescript/lib/lib.es2021.promise.d.ts","../../node_modules/typescript/lib/lib.es2021.string.d.ts","../../node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../node_modules/typescript/lib/lib.es2021.intl.d.ts","../../node_modules/typescript/lib/lib.es2022.array.d.ts","../../node_modules/typescript/lib/lib.es2022.error.d.ts","../../node_modules/typescript/lib/lib.es2022.intl.d.ts","../../node_modules/typescript/lib/lib.es2022.object.d.ts","../../node_modules/typescript/lib/lib.es2022.string.d.ts","../../node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../node_modules/typescript/lib/lib.es2023.array.d.ts","../../node_modules/typescript/lib/lib.es2023.collection.d.ts","../../node_modules/typescript/lib/lib.es2023.intl.d.ts","../../node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts","../../node_modules/typescript/lib/lib.es2024.collection.d.ts","../../node_modules/typescript/lib/lib.es2024.object.d.ts","../../node_modules/typescript/lib/lib.es2024.promise.d.ts","../../node_modules/typescript/lib/lib.es2024.regexp.d.ts","../../node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2024.string.d.ts","../../node_modules/typescript/lib/lib.esnext.array.d.ts","../../node_modules/typescript/lib/lib.esnext.collection.d.ts","../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../node_modules/typescript/lib/lib.esnext.disposable.d.ts","../../node_modules/typescript/lib/lib.esnext.promise.d.ts","../../node_modules/typescript/lib/lib.esnext.decorators.d.ts","../../node_modules/typescript/lib/lib.esnext.iterator.d.ts","../../node_modules/typescript/lib/lib.esnext.float16.d.ts","../../node_modules/typescript/lib/lib.decorators.d.ts","../../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../node_modules/@remix-run/node/dist/globals.d.ts","../../node_modules/@types/cookie/index.d.ts","../../node_modules/@remix-run/server-runtime/dist/crypto.d.ts","../../node_modules/@remix-run/server-runtime/dist/cookies.d.ts","../../node_modules/@remix-run/server-runtime/dLine truncated
|
||||
{"fileNames":["../../node_modules/typescript/lib/lib.es5.d.ts","../../node_modules/typescript/lib/lib.es2015.d.ts","../../node_modules/typescript/lib/lib.es2016.d.ts","../../node_modules/typescript/lib/lib.es2017.d.ts","../../node_modules/typescript/lib/lib.es2018.d.ts","../../node_modules/typescript/lib/lib.es2019.d.ts","../../node_modules/typescript/lib/lib.es2020.d.ts","../../node_modules/typescript/lib/lib.es2021.d.ts","../../node_modules/typescript/lib/lib.es2022.d.ts","../../node_modules/typescript/lib/lib.es2023.d.ts","../../node_modules/typescript/lib/lib.es2024.d.ts","../../node_modules/typescript/lib/lib.esnext.d.ts","../../node_modules/typescript/lib/lib.dom.d.ts","../../node_modules/typescript/lib/lib.dom.iterable.d.ts","../../node_modules/typescript/lib/lib.es2015.core.d.ts","../../node_modules/typescript/lib/lib.es2015.collection.d.ts","../../node_modules/typescript/lib/lib.es2015.generator.d.ts","../../node_modules/typescript/lib/lib.es2015.iterable.d.ts","../../node_modules/typescript/lib/lib.es2015.promise.d.ts","../../node_modules/typescript/lib/lib.es2015.proxy.d.ts","../../node_modules/typescript/lib/lib.es2015.reflect.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.d.ts","../../node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2016.array.include.d.ts","../../node_modules/typescript/lib/lib.es2016.intl.d.ts","../../node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts","../../node_modules/typescript/lib/lib.es2017.date.d.ts","../../node_modules/typescript/lib/lib.es2017.object.d.ts","../../node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2017.string.d.ts","../../node_modules/typescript/lib/lib.es2017.intl.d.ts","../../node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","../../node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","../../node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","../../node_modules/typescript/lib/lib.es2018.intl.d.ts","../../node_modules/typescript/lib/lib.es2018.promise.d.ts","../../node_modules/typescript/lib/lib.es2018.regexp.d.ts","../../node_modules/typescript/lib/lib.es2019.array.d.ts","../../node_modules/typescript/lib/lib.es2019.object.d.ts","../../node_modules/typescript/lib/lib.es2019.string.d.ts","../../node_modules/typescript/lib/lib.es2019.symbol.d.ts","../../node_modules/typescript/lib/lib.es2019.intl.d.ts","../../node_modules/typescript/lib/lib.es2020.bigint.d.ts","../../node_modules/typescript/lib/lib.es2020.date.d.ts","../../node_modules/typescript/lib/lib.es2020.promise.d.ts","../../node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2020.string.d.ts","../../node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","../../node_modules/typescript/lib/lib.es2020.intl.d.ts","../../node_modules/typescript/lib/lib.es2020.number.d.ts","../../node_modules/typescript/lib/lib.es2021.promise.d.ts","../../node_modules/typescript/lib/lib.es2021.string.d.ts","../../node_modules/typescript/lib/lib.es2021.weakref.d.ts","../../node_modules/typescript/lib/lib.es2021.intl.d.ts","../../node_modules/typescript/lib/lib.es2022.array.d.ts","../../node_modules/typescript/lib/lib.es2022.error.d.ts","../../node_modules/typescript/lib/lib.es2022.intl.d.ts","../../node_modules/typescript/lib/lib.es2022.object.d.ts","../../node_modules/typescript/lib/lib.es2022.string.d.ts","../../node_modules/typescript/lib/lib.es2022.regexp.d.ts","../../node_modules/typescript/lib/lib.es2023.array.d.ts","../../node_modules/typescript/lib/lib.es2023.collection.d.ts","../../node_modules/typescript/lib/lib.es2023.intl.d.ts","../../node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts","../../node_modules/typescript/lib/lib.es2024.collection.d.ts","../../node_modules/typescript/lib/lib.es2024.object.d.ts","../../node_modules/typescript/lib/lib.es2024.promise.d.ts","../../node_modules/typescript/lib/lib.es2024.regexp.d.ts","../../node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts","../../node_modules/typescript/lib/lib.es2024.string.d.ts","../../node_modules/typescript/lib/lib.esnext.array.d.ts","../../node_modules/typescript/lib/lib.esnext.collection.d.ts","../../node_modules/typescript/lib/lib.esnext.intl.d.ts","../../node_modules/typescript/lib/lib.esnext.disposable.d.ts","../../node_modules/typescript/lib/lib.esnext.promise.d.ts","../../node_modules/typescript/lib/lib.esnext.decorators.d.ts","../../node_modules/typescript/lib/lib.esnext.iterator.d.ts","../../node_modules/typescript/lib/lib.esnext.float16.d.ts","../../node_modules/typescript/lib/lib.decorators.d.ts","../../node_modules/typescript/lib/lib.decorators.legacy.d.ts","../../node_modules/@remix-run/node/dist/globals.d.ts","../../node_modules/@types/cookie/index.d.ts","../../node_modules/@remix-run/server-runtime/dist/crypto.d.ts","../../node_modules/@remix-run/server-runtime/dist/cookies.d.ts","../../node_modules/@remix-run/server-runtime/dLine truncated
|
||||
@@ -1,5 +1,9 @@
|
||||
// Public API for @storyteller/ui-pagescene.
|
||||
|
||||
// Top-level component — the host mounts this inside its route/tab.
|
||||
export { Stage3D } from "./lib/Stage3D";
|
||||
export type { Stage3DProps } from "./lib/Stage3D";
|
||||
|
||||
// Adapter (platform abstraction)
|
||||
export type {
|
||||
PageSceneAdapter,
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
// Public top-level component for the 3D editor. Hosts mount this
|
||||
// inside their route/tab and pass a PageSceneAdapter; everything
|
||||
// else (engine lifecycle, body composition, controls) is lib-owned.
|
||||
//
|
||||
// Usage (artcraft Tauri):
|
||||
// <Stage3D adapter={tauriAdapter} sceneToken={params.sceneToken} />
|
||||
//
|
||||
// Usage (artcraft-website):
|
||||
// <Stage3D adapter={webAdapter} sceneToken={params.sceneToken} />
|
||||
|
||||
import { useEffect } from "react";
|
||||
import type { PageSceneAdapter } from "./adapter";
|
||||
import { EngineProvider } from "./contexts/EngineContext/EngineProvider";
|
||||
import { DragComponent } from "./comps/DragComponent/DragComponent";
|
||||
import { EditorLoadingBar } from "./comps/EditorLoadingBar";
|
||||
import { PrecisionSelector } from "./comps/PrecisionSelector/PrecisionSelector";
|
||||
import { Stage3DBody } from "./Stage3DBody";
|
||||
import { usePageSceneStore } from "./PageSceneStore";
|
||||
|
||||
export interface Stage3DProps {
|
||||
adapter: PageSceneAdapter;
|
||||
sceneToken?: string;
|
||||
/** In-memory restore — host stashes serialized scene JSON across
|
||||
* remount and supplies it here. The lib snapshots on mount only. */
|
||||
cacheJsonString?: string;
|
||||
/** Called on unmount with the serialized scene JSON. The host
|
||||
* decides where to put it (tab store, localStorage, nowhere). */
|
||||
onSceneSerialized?: (json: string) => void;
|
||||
}
|
||||
|
||||
export const Stage3D = ({
|
||||
adapter,
|
||||
sceneToken,
|
||||
cacheJsonString,
|
||||
onSceneSerialized,
|
||||
}: Stage3DProps) => {
|
||||
// Engine's remountEngine() gate reads is3DPageMounted. With Stage3D
|
||||
// mounting only when the host's tab/route puts us on screen, the
|
||||
// React lifecycle IS the signal — host code shouldn't need to flip
|
||||
// the flag manually.
|
||||
useEffect(() => {
|
||||
usePageSceneStore.getState().set3DPageMounted(true);
|
||||
return () => {
|
||||
usePageSceneStore.getState().set3DPageMounted(false);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<EngineProvider
|
||||
sceneToken={sceneToken}
|
||||
adapter={adapter}
|
||||
cacheJsonString={cacheJsonString}
|
||||
onSceneSerialized={onSceneSerialized}
|
||||
>
|
||||
<Stage3DBody />
|
||||
<DragComponent />
|
||||
<PrecisionSelector />
|
||||
<EditorLoadingBar />
|
||||
</EngineProvider>
|
||||
);
|
||||
};
|
||||
+109
-156
@@ -1,41 +1,13 @@
|
||||
// 3D editor body — the visual composition of canvas, controls,
|
||||
// outliner, prompt box, model selector, etc. Mounted by <Stage3D />
|
||||
// inside an EngineProvider; consumes the active editor via context
|
||||
// and host plumbing via the adapter on `editor.adapter`.
|
||||
//
|
||||
// Lib-resident so artcraft Tauri and artcraft-website share the
|
||||
// exact same 3D editor UX. Only the platform-specific PageSceneAdapter
|
||||
// implementation differs between hosts.
|
||||
|
||||
import React, { useContext, useEffect, useRef } from "react";
|
||||
import { useSignals } from "@preact/signals-react/runtime";
|
||||
import { pageHeight, pageWidth } from "~/signals";
|
||||
|
||||
import {
|
||||
CameraAspectRatio,
|
||||
ControlPanelSceneObject,
|
||||
Controls3D,
|
||||
ControlsTopButtons,
|
||||
EditorCanvas,
|
||||
EngineContext,
|
||||
FocalLengthDisplay,
|
||||
GridVisibleChangedEvent,
|
||||
OnboardingHelper,
|
||||
Outliner,
|
||||
PoseModeSelector,
|
||||
PreviewEngineCamera,
|
||||
SceneContainer,
|
||||
addCharacter,
|
||||
addObject,
|
||||
pickDropPosition,
|
||||
setCameraAspect,
|
||||
useEditorCanvas,
|
||||
useFreeCam,
|
||||
usePageSceneStore,
|
||||
useViewportKeyboard,
|
||||
useViewportPointer,
|
||||
} from "@storyteller/ui-pagescene";
|
||||
import type { MediaItem } from "@storyteller/ui-pagescene";
|
||||
import { PromptBox3D, commonToCameraAspect } from "@storyteller/ui-promptbox";
|
||||
import { PopoverItem } from "@storyteller/ui-popover";
|
||||
import { LoadingDots } from "@storyteller/ui-loading";
|
||||
|
||||
import { uploadImage } from "~/components/reusable/UploadModalMedia/uploadImage";
|
||||
import { uploadPlaneFromMediaToken } from "~/components/reusable/UploadModalMedia/uploadPlane";
|
||||
import { AssetType } from "~/enums";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { UploaderState } from "~/models";
|
||||
import {
|
||||
GalleryItem,
|
||||
onImageDrop,
|
||||
@@ -48,19 +20,43 @@ import {
|
||||
useSelectedProviderForModel,
|
||||
ClassyModelSelector,
|
||||
} from "@storyteller/ui-model-selector";
|
||||
import { ImageModel } from "@storyteller/model-list";
|
||||
import type { ImageModel } from "@storyteller/model-list";
|
||||
import type { GenerationProvider } from "@storyteller/api-enums";
|
||||
import { HelpMenuButton } from "@storyteller/ui-help-menu";
|
||||
import {
|
||||
CostCalculatorButton,
|
||||
useCostBreakdownModalStore,
|
||||
} from "@storyteller/ui-pricing-modal";
|
||||
import { GenerationProvider } from "@storyteller/api-enums";
|
||||
import { LoadingDots } from "@storyteller/ui-loading";
|
||||
import { PromptBox3D, commonToCameraAspect } from "@storyteller/ui-promptbox";
|
||||
import type { PopoverItem } from "@storyteller/ui-popover";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import { EngineContext } from "./contexts/EngineContext/EngineContext";
|
||||
import { ControlPanelSceneObject } from "./comps/ControlPanelSceneObject";
|
||||
import { Controls3D } from "./comps/Controls3D";
|
||||
import { ControlsTopButtons } from "./comps/ControlsTopButtons";
|
||||
import { EditorCanvas } from "./comps/EngineCanvases";
|
||||
import { FocalLengthDisplay } from "./comps/FocalLengthDisplay/FocalLengthDisplay";
|
||||
import { OnboardingHelper } from "./comps/OnboardingHelper";
|
||||
import { Outliner } from "./comps/Outliner";
|
||||
import { PoseModeSelector } from "./comps/PoseModeSelector";
|
||||
import { PreviewEngineCamera } from "./comps/PreviewEngineCamera";
|
||||
import { SceneContainer } from "./comps/SceneContainer";
|
||||
import { addCharacter, addObject, setCameraAspect } from "./actions";
|
||||
import { useEditorCanvas } from "./hooks/useEditorCanvas";
|
||||
import { useFreeCam } from "./hooks/useFreeCam";
|
||||
import { useViewportPointer } from "./hooks/useViewportPointer";
|
||||
import { useViewportKeyboard } from "./hooks/useViewportKeyboard";
|
||||
import { GridVisibleChangedEvent } from "./engine/events/EngineEvent";
|
||||
import { pickDropPosition } from "./engine/pickDropPosition";
|
||||
import { AssetType, CameraAspectRatio } from "./enums";
|
||||
import { usePageSceneStore } from "./PageSceneStore";
|
||||
import type { MediaItem } from "./models/assets";
|
||||
|
||||
const PAGE_ID: ModelPage = ModelPage.Stage3D;
|
||||
|
||||
export const PageEditor = () => {
|
||||
useSignals();
|
||||
|
||||
export const Stage3DBody = () => {
|
||||
const camAspect = usePageSceneStore((s) => s.cameraAspectRatio);
|
||||
const outlinerShowing = usePageSceneStore((s) => s.outlinerShowing);
|
||||
const editorLoader = usePageSceneStore((s) => s.editorLoader);
|
||||
@@ -81,11 +77,7 @@ export const PageEditor = () => {
|
||||
const updateCamera = usePageSceneStore((s) => s.updateCamera);
|
||||
const deleteCamera = usePageSceneStore((s) => s.deleteCamera);
|
||||
const setSelectedCameraId = usePageSceneStore((s) => s.setSelectedCameraId);
|
||||
// Only stops propagation for clicks on the overlay div itself (the
|
||||
// transparent gaps between control buttons). Clicks that bubbled up
|
||||
// from child controls pass through untouched, so React-tree-portal'd
|
||||
// modals further down the JSX (the upload modals inside Controls3D)
|
||||
// aren't silenced when the user clicks inside them.
|
||||
|
||||
const handleOverlayClick = (event: React.MouseEvent<HTMLDivElement>) => {
|
||||
if (event.target === event.currentTarget) {
|
||||
event.stopPropagation();
|
||||
@@ -108,48 +100,48 @@ export const PageEditor = () => {
|
||||
(s) => s.estimatedCreditsByPage[PAGE_ID],
|
||||
);
|
||||
|
||||
const height = pageHeight.value - 56;
|
||||
const editor = useContext(EngineContext);
|
||||
|
||||
// Viewport-driven layout sizing. Falls back to window.innerWidth/
|
||||
// innerHeight when the host doesn't supply getViewportSize.
|
||||
const viewportFallback =
|
||||
typeof window !== "undefined"
|
||||
? { width: window.innerWidth, height: window.innerHeight }
|
||||
: { width: 0, height: 0 };
|
||||
const viewport = editor?.adapter.getViewportSize?.() ?? viewportFallback;
|
||||
const height = viewport.height - 56;
|
||||
|
||||
const getScale = () => {
|
||||
const height = pageHeight.value - 56;
|
||||
const scaleHeight = height < 610 ? height / 610 : 1;
|
||||
|
||||
const h = viewport.height - 56;
|
||||
const scaleHeight = h < 610 ? h / 610 : 1;
|
||||
if (
|
||||
camAspect === CameraAspectRatio.VERTICAL_9_16 &&
|
||||
outlinerShowing &&
|
||||
height < 900
|
||||
h < 900
|
||||
) {
|
||||
if (pageWidth.value > 2000) {
|
||||
return scaleHeight;
|
||||
}
|
||||
if (viewport.width > 2000) return scaleHeight;
|
||||
return scaleHeight * 0.78;
|
||||
}
|
||||
|
||||
if (
|
||||
camAspect === CameraAspectRatio.SQUARE_1_1 &&
|
||||
pageWidth.value < 2000
|
||||
viewport.width < 2000
|
||||
) {
|
||||
return scaleHeight * 0.85;
|
||||
}
|
||||
|
||||
return scaleHeight;
|
||||
};
|
||||
|
||||
// These are callbacks required by promptbox
|
||||
const editorEngine = useContext(EngineContext);
|
||||
const editorCanvas = useEditorCanvas();
|
||||
useFreeCam(editorCanvas, editorEngine);
|
||||
useViewportPointer(editorCanvas, editorEngine);
|
||||
useViewportKeyboard(editorEngine);
|
||||
useFreeCam(editorCanvas, editor);
|
||||
useViewportPointer(editorCanvas, editor);
|
||||
useViewportKeyboard(editor);
|
||||
|
||||
const handleCameraSelect = (selectedItem: PopoverItem) => {
|
||||
const selectedCamera = cameras.find(
|
||||
(cam) => cam.label === selectedItem.label,
|
||||
);
|
||||
if (selectedCamera && editorEngine) {
|
||||
if (selectedCamera && editor) {
|
||||
setSelectedCameraId(selectedCamera.id);
|
||||
|
||||
// Show focal length display temporarily
|
||||
// TODO: Rename dragging to visible - BFlat
|
||||
setFocalLengthDragging({
|
||||
isDragging: true,
|
||||
focalLength: selectedCamera.focalLength,
|
||||
@@ -161,8 +153,7 @@ export const PageEditor = () => {
|
||||
});
|
||||
}, 1500);
|
||||
|
||||
// Update the main camera to match the selected camera's properties
|
||||
const cam = editorEngine.cameraController.camera;
|
||||
const cam = editor.cameraController.camera;
|
||||
if (cam) {
|
||||
cam.position.set(
|
||||
selectedCamera.position.x,
|
||||
@@ -174,22 +165,16 @@ export const PageEditor = () => {
|
||||
selectedCamera.lookAt.y,
|
||||
selectedCamera.lookAt.z,
|
||||
);
|
||||
|
||||
cam.fov = editorEngine.cameraController.focalLengthToFov(
|
||||
cam.fov = editor.cameraController.focalLengthToFov(
|
||||
selectedCamera.focalLength,
|
||||
);
|
||||
cam.updateProjectionMatrix();
|
||||
|
||||
// Reset free-cam motion so a switch doesn't carry over a
|
||||
// half-applied drag from the previous camera.
|
||||
if (editorEngine.cameraController.freeCamState) {
|
||||
editorEngine.cameraController.freeCamState.velocity.set(0, 0, 0);
|
||||
if (editor.cameraController.freeCamState) {
|
||||
editor.cameraController.freeCamState.velocity.set(0, 0, 0);
|
||||
}
|
||||
|
||||
editorEngine.renderScene();
|
||||
editor.renderScene();
|
||||
}
|
||||
|
||||
// Force update camera properties in the state
|
||||
updateCamera(selectedCamera.id, {
|
||||
focalLength: selectedCamera.focalLength,
|
||||
position: selectedCamera.position,
|
||||
@@ -200,56 +185,39 @@ export const PageEditor = () => {
|
||||
};
|
||||
|
||||
const handleAddCamera = () => {
|
||||
// Check if we've reached the maximum number of cameras
|
||||
if (cameras.length >= 6) {
|
||||
console.warn("Maximum number of cameras (6) reached");
|
||||
return;
|
||||
}
|
||||
|
||||
const newIndex = cameras.length + 1;
|
||||
const newId = `cam${newIndex}`;
|
||||
|
||||
// This is for generating random orbital position for the new camera using spherical coordinates
|
||||
const radius = Math.random() * 5 + 7; // Distance from center: 7 to 12 units
|
||||
const theta = Math.random() * Math.PI * 2; // Azimuthal angle: 0 to 2π
|
||||
const phi = Math.PI / 3 + (Math.random() * Math.PI) / 6; // Polar angle: π/3 to π/2 (60° to 90° from horizontal)
|
||||
|
||||
// Convert spherical coordinates to Cartesian coordinates
|
||||
const radius = Math.random() * 5 + 7;
|
||||
const theta = Math.random() * Math.PI * 2;
|
||||
const phi = Math.PI / 3 + (Math.random() * Math.PI) / 6;
|
||||
const randomX = radius * Math.sin(phi) * Math.cos(theta);
|
||||
const randomY = Math.abs(radius * Math.cos(phi)) + 2; // Ensure Y is positive and at least 2 units up
|
||||
const randomY = Math.abs(radius * Math.cos(phi)) + 2;
|
||||
const randomZ = radius * Math.sin(phi) * Math.sin(theta);
|
||||
|
||||
addCamera({
|
||||
id: newId,
|
||||
label: `Camera ${newIndex}`,
|
||||
focalLength: 24,
|
||||
position: {
|
||||
x: randomX,
|
||||
y: randomY,
|
||||
z: randomZ,
|
||||
},
|
||||
position: { x: randomX, y: randomY, z: randomZ },
|
||||
rotation: { x: 0, y: 0, z: 0 },
|
||||
lookAt: { x: 0, y: 0, z: 0 },
|
||||
});
|
||||
|
||||
// Switch to the newly created camera
|
||||
setSelectedCameraId(newId);
|
||||
|
||||
// Update the engine camera to match the new camera's properties
|
||||
const cam = editorEngine?.cameraController.camera;
|
||||
if (editorEngine && cam) {
|
||||
const cam = editor?.cameraController.camera;
|
||||
if (editor && cam) {
|
||||
cam.position.set(randomX, randomY, randomZ);
|
||||
cam.lookAt(0, 0, 0);
|
||||
cam.fov = editorEngine.cameraController.focalLengthToFov(24);
|
||||
cam.fov = editor.cameraController.focalLengthToFov(24);
|
||||
cam.updateProjectionMatrix();
|
||||
|
||||
// Reset free-cam motion so the camera doesn't drift after
|
||||
// teleport.
|
||||
if (editorEngine.cameraController.freeCamState) {
|
||||
editorEngine.cameraController.freeCamState.velocity.set(0, 0, 0);
|
||||
if (editor.cameraController.freeCamState) {
|
||||
editor.cameraController.freeCamState.velocity.set(0, 0, 0);
|
||||
}
|
||||
|
||||
editorEngine.renderScene();
|
||||
editor.renderScene();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -265,48 +233,44 @@ export const PageEditor = () => {
|
||||
};
|
||||
|
||||
const onAspectRatioSelect = (newRatio: CameraAspectRatio) => {
|
||||
if (!editorEngine) return;
|
||||
setCameraAspect(editorEngine, newRatio);
|
||||
if (!editor) return;
|
||||
setCameraAspect(editor, newRatio);
|
||||
};
|
||||
|
||||
// Cold-load sync: align the editor letterbox with the picker's
|
||||
// initial display (which falls back to `model.defaultAspectRatio`).
|
||||
// Fires once when the engine and a `supportsNewAspectRatio()` model
|
||||
// are both ready, then never again — every later change goes through
|
||||
// user picks. We can't sync per-model-switch because almost every
|
||||
// model declares `defaultAspectRatio: Square`, which would force the
|
||||
// letterbox to Square on every swap and override the user's pick.
|
||||
// initial display once when the engine + a `supportsNewAspectRatio()`
|
||||
// model are both ready. Per-model-switch sync is intentionally NOT
|
||||
// done because every model defaults to Square, which would override
|
||||
// the user's pick.
|
||||
const didColdSyncRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (didColdSyncRef.current) return;
|
||||
if (!editorEngine || !selectedImageModel?.supportsNewAspectRatio()) return;
|
||||
if (!editor || !selectedImageModel?.supportsNewAspectRatio()) return;
|
||||
const def = selectedImageModel.defaultAspectRatio;
|
||||
if (!def) return;
|
||||
const mapped = commonToCameraAspect(def);
|
||||
if (!mapped) return;
|
||||
setCameraAspect(editorEngine, mapped);
|
||||
setCameraAspect(editor, mapped);
|
||||
didColdSyncRef.current = true;
|
||||
}, [editorEngine, selectedImageModel]);
|
||||
}, [editor, selectedImageModel]);
|
||||
|
||||
// Gallery → 3D scene drop handler. PageEditor now only mounts on the
|
||||
// 3D tab, so we don't need the previous activeTabId branch — the
|
||||
// subscription is implicitly 3D-only by virtue of where it lives.
|
||||
// Gallery → 3D scene drop handler. Stage3D mounts only when 3D is
|
||||
// active so this is implicitly 3D-only.
|
||||
useEffect(() => {
|
||||
const handler = onImageDrop(
|
||||
(item: GalleryItem, position: { x: number; y: number }) => {
|
||||
(async () => {
|
||||
if (!editorEngine) {
|
||||
if (!editor) {
|
||||
console.warn("Cannot drop asset: editor engine not ready");
|
||||
return;
|
||||
}
|
||||
const worldPosition = pickDropPosition(
|
||||
{
|
||||
getCamera: () => editorEngine.cameraController.camera,
|
||||
getCanvas: () => editorEngine.renderer?.domElement,
|
||||
getRaycastTargets: () =>
|
||||
editorEngine.activeScene.scene.children,
|
||||
getCamera: () => editor.cameraController.camera,
|
||||
getCanvas: () => editor.renderer?.domElement,
|
||||
getRaycastTargets: () => editor.activeScene.scene.children,
|
||||
removeTransformControls: () =>
|
||||
editorEngine.utils.removeTransformControls(true),
|
||||
editor.utils.removeTransformControls(true),
|
||||
},
|
||||
position.x,
|
||||
position.y,
|
||||
@@ -321,9 +285,9 @@ export const PageEditor = () => {
|
||||
name: item.label || (isCharacter ? "Character" : "3D Object"),
|
||||
};
|
||||
if (isCharacter) {
|
||||
await addCharacter(editorEngine, mediaItem, worldPosition);
|
||||
await addCharacter(editor, mediaItem, worldPosition);
|
||||
} else {
|
||||
await addObject(editorEngine, mediaItem, worldPosition);
|
||||
await addObject(editor, mediaItem, worldPosition);
|
||||
}
|
||||
} else {
|
||||
const mediaItem: MediaItem = {
|
||||
@@ -332,12 +296,12 @@ export const PageEditor = () => {
|
||||
media_id: item.id || uuidv4(),
|
||||
name: item.label || "Image Plane",
|
||||
};
|
||||
await addObject(editorEngine, mediaItem, worldPosition);
|
||||
await addObject(editor, mediaItem, worldPosition);
|
||||
|
||||
await uploadPlaneFromMediaToken({
|
||||
await editor.adapter.uploadPlaneFromMediaToken({
|
||||
title: item.label || "Image Plane",
|
||||
mediaToken: item.id,
|
||||
progressCallback: (state: UploaderState) => {
|
||||
progressCallback: (state) => {
|
||||
if (state.status)
|
||||
console.log("Upload status:", state.status);
|
||||
},
|
||||
@@ -354,7 +318,7 @@ export const PageEditor = () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
if (handler) removeImageDropListener(handler as any);
|
||||
};
|
||||
}, [editorEngine]);
|
||||
}, [editor]);
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -364,26 +328,19 @@ export const PageEditor = () => {
|
||||
className="relative flex w-screen"
|
||||
style={{ height: "calc(100vh - 68px)" }}
|
||||
>
|
||||
{/* Engine section/side panel */}
|
||||
<div
|
||||
id="engine-n-panels-wrapper"
|
||||
className="flex"
|
||||
style={{
|
||||
height,
|
||||
}}
|
||||
style={{ height }}
|
||||
>
|
||||
<div className="relative w-full overflow-hidden bg-transparent">
|
||||
<SceneContainer>
|
||||
<EditorCanvas />
|
||||
</SceneContainer>
|
||||
|
||||
{/* Focal Length Display */}
|
||||
<FocalLengthDisplay />
|
||||
|
||||
{/* Pose Mode Selector */}
|
||||
<PoseModeSelector />
|
||||
|
||||
{/* Top controls */}
|
||||
<div
|
||||
className="absolute left-0 top-0 w-full"
|
||||
onClick={handleOverlayClick}
|
||||
@@ -394,12 +351,9 @@ export const PageEditor = () => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Bottom controls */}
|
||||
<div
|
||||
className="absolute bottom-0 left-0"
|
||||
style={{
|
||||
width: pageWidth.value,
|
||||
}}
|
||||
style={{ width: viewport.width }}
|
||||
onClick={handleOverlayClick}
|
||||
>
|
||||
<div
|
||||
@@ -420,7 +374,7 @@ export const PageEditor = () => {
|
||||
enableHotkeyInput={enableHotkeyInput}
|
||||
gridVisibility={gridVisible}
|
||||
setGridVisibility={(visible: boolean) =>
|
||||
editorEngine?.bus.emit(new GridVisibleChangedEvent(visible))
|
||||
editor?.bus.emit(new GridVisibleChangedEvent(visible))
|
||||
}
|
||||
selectedCameraId={selectedCameraId}
|
||||
deleteCamera={deleteCamera}
|
||||
@@ -428,7 +382,12 @@ export const PageEditor = () => {
|
||||
setFocalLengthDragging={setFocalLengthDragging}
|
||||
isPromptBoxFocused={isPromptBoxFocused}
|
||||
setIsPromptBoxFocused={setIsPromptBoxFocused}
|
||||
uploadImage={uploadImage}
|
||||
uploadImage={
|
||||
editor
|
||||
? (((arg: Parameters<typeof editor.adapter.uploadImage>[0]) =>
|
||||
editor.adapter.uploadImage(arg)) as never)
|
||||
: undefined
|
||||
}
|
||||
handleCameraSelect={handleCameraSelect}
|
||||
handleAddCamera={handleAddCamera}
|
||||
handleCameraNameChange={handleCameraNameChange}
|
||||
@@ -438,16 +397,10 @@ export const PageEditor = () => {
|
||||
selectedProvider={selectedProvider}
|
||||
credits={imageCredits}
|
||||
setEnginePrompt={(prompt) => {
|
||||
console.log("setEnginePrompt", prompt);
|
||||
if (!editorEngine) {
|
||||
console.log("editorEngine is not available");
|
||||
return;
|
||||
}
|
||||
editorEngine!.positive_prompt = prompt;
|
||||
if (!editor) return;
|
||||
editor.positive_prompt = prompt;
|
||||
}}
|
||||
snapshotCurrentFrame={editorEngine?.snapShotOfCurrentFrame.bind(
|
||||
editorEngine,
|
||||
)}
|
||||
snapshotCurrentFrame={editor?.snapShotOfCurrentFrame.bind(editor)}
|
||||
/>
|
||||
|
||||
<LoadingDots
|
||||
@@ -11,21 +11,12 @@
|
||||
{
|
||||
"path": "../label"
|
||||
},
|
||||
{
|
||||
"path": "../gallery-modal"
|
||||
},
|
||||
{
|
||||
"path": "../popover"
|
||||
},
|
||||
{
|
||||
"path": "../settings-modal"
|
||||
},
|
||||
{
|
||||
"path": "../button-icon-select"
|
||||
},
|
||||
{
|
||||
"path": "../loading"
|
||||
},
|
||||
{
|
||||
"path": "../badge"
|
||||
},
|
||||
@@ -50,12 +41,33 @@
|
||||
{
|
||||
"path": "../button-dropdown"
|
||||
},
|
||||
{
|
||||
"path": "../popover"
|
||||
},
|
||||
{
|
||||
"path": "../promptbox"
|
||||
},
|
||||
{
|
||||
"path": "../loading"
|
||||
},
|
||||
{
|
||||
"path": "../pricing-modal"
|
||||
},
|
||||
{
|
||||
"path": "../help-menu"
|
||||
},
|
||||
{
|
||||
"path": "../../api-enums"
|
||||
},
|
||||
{
|
||||
"path": "../../model-list"
|
||||
},
|
||||
{
|
||||
"path": "../model-selector"
|
||||
},
|
||||
{
|
||||
"path": "../gallery-modal"
|
||||
},
|
||||
{
|
||||
"path": "../../common"
|
||||
},
|
||||
|
||||
@@ -59,21 +59,12 @@
|
||||
{
|
||||
"path": "../label/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../gallery-modal/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../popover/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../settings-modal/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../button-icon-select/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../loading/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../badge/tsconfig.lib.json"
|
||||
},
|
||||
@@ -98,12 +89,33 @@
|
||||
{
|
||||
"path": "../button-dropdown/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../popover/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../promptbox/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../loading/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../pricing-modal/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../help-menu/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../../api-enums/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../../model-list/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../model-selector/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../gallery-modal/tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "../../common/tsconfig.lib.json"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user