@motion-core/motion-gpu 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/advanced.d.ts +1 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +12 -6
- package/dist/core/advanced.d.ts +1 -0
- package/dist/core/advanced.d.ts.map +1 -0
- package/dist/core/advanced.js +12 -5
- package/dist/core/current-value.d.ts +1 -0
- package/dist/core/current-value.d.ts.map +1 -0
- package/dist/core/current-value.js +35 -34
- package/dist/core/current-value.js.map +1 -0
- package/dist/core/error-diagnostics.d.ts +1 -0
- package/dist/core/error-diagnostics.d.ts.map +1 -0
- package/dist/core/error-diagnostics.js +70 -137
- package/dist/core/error-diagnostics.js.map +1 -0
- package/dist/core/error-report.d.ts +1 -0
- package/dist/core/error-report.d.ts.map +1 -0
- package/dist/core/error-report.js +184 -233
- package/dist/core/error-report.js.map +1 -0
- package/dist/core/frame-registry.d.ts +1 -0
- package/dist/core/frame-registry.d.ts.map +1 -0
- package/dist/core/frame-registry.js +546 -662
- package/dist/core/frame-registry.js.map +1 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +11 -12
- package/dist/core/material-preprocess.d.ts +1 -0
- package/dist/core/material-preprocess.d.ts.map +1 -0
- package/dist/core/material-preprocess.js +128 -151
- package/dist/core/material-preprocess.js.map +1 -0
- package/dist/core/material.d.ts +1 -0
- package/dist/core/material.d.ts.map +1 -0
- package/dist/core/material.js +263 -317
- package/dist/core/material.js.map +1 -0
- package/dist/core/recompile-policy.d.ts +1 -0
- package/dist/core/recompile-policy.d.ts.map +1 -0
- package/dist/core/recompile-policy.js +18 -13
- package/dist/core/recompile-policy.js.map +1 -0
- package/dist/core/render-graph.d.ts +1 -0
- package/dist/core/render-graph.d.ts.map +1 -0
- package/dist/core/render-graph.js +61 -68
- package/dist/core/render-graph.js.map +1 -0
- package/dist/core/render-targets.d.ts +1 -0
- package/dist/core/render-targets.d.ts.map +1 -0
- package/dist/core/render-targets.js +52 -53
- package/dist/core/render-targets.js.map +1 -0
- package/dist/core/renderer.d.ts +1 -0
- package/dist/core/renderer.d.ts.map +1 -0
- package/dist/core/renderer.js +942 -1081
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/runtime-loop.d.ts +1 -0
- package/dist/core/runtime-loop.d.ts.map +1 -0
- package/dist/core/runtime-loop.js +305 -362
- package/dist/core/runtime-loop.js.map +1 -0
- package/dist/core/scheduler-helpers.d.ts +1 -0
- package/dist/core/scheduler-helpers.d.ts.map +1 -0
- package/dist/core/scheduler-helpers.js +52 -51
- package/dist/core/scheduler-helpers.js.map +1 -0
- package/dist/core/shader.d.ts +1 -0
- package/dist/core/shader.d.ts.map +1 -0
- package/dist/core/shader.js +92 -117
- package/dist/core/shader.js.map +1 -0
- package/dist/core/texture-loader.d.ts +1 -0
- package/dist/core/texture-loader.d.ts.map +1 -0
- package/dist/core/texture-loader.js +205 -273
- package/dist/core/texture-loader.js.map +1 -0
- package/dist/core/textures.d.ts +1 -0
- package/dist/core/textures.d.ts.map +1 -0
- package/dist/core/textures.js +106 -116
- package/dist/core/textures.js.map +1 -0
- package/dist/core/types.d.ts +1 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +0 -4
- package/dist/core/uniforms.d.ts +1 -0
- package/dist/core/uniforms.d.ts.map +1 -0
- package/dist/core/uniforms.js +170 -191
- package/dist/core/uniforms.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -6
- package/dist/passes/BlitPass.d.ts +1 -0
- package/dist/passes/BlitPass.d.ts.map +1 -0
- package/dist/passes/BlitPass.js +23 -18
- package/dist/passes/BlitPass.js.map +1 -0
- package/dist/passes/CopyPass.d.ts +1 -0
- package/dist/passes/CopyPass.d.ts.map +1 -0
- package/dist/passes/CopyPass.js +58 -52
- package/dist/passes/CopyPass.js.map +1 -0
- package/dist/passes/FullscreenPass.d.ts +1 -0
- package/dist/passes/FullscreenPass.d.ts.map +1 -0
- package/dist/passes/FullscreenPass.js +127 -130
- package/dist/passes/FullscreenPass.js.map +1 -0
- package/dist/passes/ShaderPass.d.ts +1 -0
- package/dist/passes/ShaderPass.d.ts.map +1 -0
- package/dist/passes/ShaderPass.js +40 -37
- package/dist/passes/ShaderPass.js.map +1 -0
- package/dist/passes/index.d.ts +1 -0
- package/dist/passes/index.d.ts.map +1 -0
- package/dist/passes/index.js +4 -3
- package/dist/react/FragCanvas.d.ts +1 -0
- package/dist/react/FragCanvas.d.ts.map +1 -0
- package/dist/react/FragCanvas.js +234 -211
- package/dist/react/FragCanvas.js.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts +1 -0
- package/dist/react/MotionGPUErrorOverlay.d.ts.map +1 -0
- package/dist/react/MotionGPUErrorOverlay.js +96 -13
- package/dist/react/MotionGPUErrorOverlay.js.map +1 -0
- package/dist/react/Portal.d.ts +1 -0
- package/dist/react/Portal.d.ts.map +1 -0
- package/dist/react/Portal.js +18 -21
- package/dist/react/Portal.js.map +1 -0
- package/dist/react/advanced.d.ts +1 -0
- package/dist/react/advanced.d.ts.map +1 -0
- package/dist/react/advanced.js +12 -6
- package/dist/react/frame-context.d.ts +1 -0
- package/dist/react/frame-context.d.ts.map +1 -0
- package/dist/react/frame-context.js +88 -94
- package/dist/react/frame-context.js.map +1 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +10 -9
- package/dist/react/motiongpu-context.d.ts +1 -0
- package/dist/react/motiongpu-context.d.ts.map +1 -0
- package/dist/react/motiongpu-context.js +18 -15
- package/dist/react/motiongpu-context.js.map +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts +1 -0
- package/dist/react/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/react/use-motiongpu-user-context.js +83 -82
- package/dist/react/use-motiongpu-user-context.js.map +1 -0
- package/dist/react/use-texture.d.ts +1 -0
- package/dist/react/use-texture.d.ts.map +1 -0
- package/dist/react/use-texture.js +132 -152
- package/dist/react/use-texture.js.map +1 -0
- package/dist/svelte/FragCanvas.svelte.d.ts +1 -0
- package/dist/svelte/FragCanvas.svelte.d.ts.map +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts +1 -0
- package/dist/svelte/MotionGPUErrorOverlay.svelte.d.ts.map +1 -0
- package/dist/svelte/Portal.svelte.d.ts +1 -0
- package/dist/svelte/Portal.svelte.d.ts.map +1 -0
- package/dist/svelte/advanced.d.ts +1 -0
- package/dist/svelte/advanced.d.ts.map +1 -0
- package/dist/svelte/advanced.js +11 -6
- package/dist/svelte/frame-context.d.ts +1 -0
- package/dist/svelte/frame-context.d.ts.map +1 -0
- package/dist/svelte/frame-context.js +27 -27
- package/dist/svelte/frame-context.js.map +1 -0
- package/dist/svelte/index.d.ts +1 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +10 -9
- package/dist/svelte/motiongpu-context.d.ts +1 -0
- package/dist/svelte/motiongpu-context.d.ts.map +1 -0
- package/dist/svelte/motiongpu-context.js +24 -21
- package/dist/svelte/motiongpu-context.js.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts +1 -0
- package/dist/svelte/use-motiongpu-user-context.d.ts.map +1 -0
- package/dist/svelte/use-motiongpu-user-context.js +69 -70
- package/dist/svelte/use-motiongpu-user-context.js.map +1 -0
- package/dist/svelte/use-texture.d.ts +1 -0
- package/dist/svelte/use-texture.d.ts.map +1 -0
- package/dist/svelte/use-texture.js +125 -147
- package/dist/svelte/use-texture.js.map +1 -0
- package/package.json +12 -7
- package/src/lib/advanced.ts +6 -0
- package/src/lib/core/advanced.ts +12 -0
- package/src/lib/core/current-value.ts +64 -0
- package/src/lib/core/error-diagnostics.ts +236 -0
- package/src/lib/core/error-report.ts +406 -0
- package/src/lib/core/frame-registry.ts +1189 -0
- package/src/lib/core/index.ts +77 -0
- package/src/lib/core/material-preprocess.ts +284 -0
- package/src/lib/core/material.ts +667 -0
- package/src/lib/core/recompile-policy.ts +31 -0
- package/src/lib/core/render-graph.ts +143 -0
- package/src/lib/core/render-targets.ts +107 -0
- package/src/lib/core/renderer.ts +1547 -0
- package/src/lib/core/runtime-loop.ts +458 -0
- package/src/lib/core/scheduler-helpers.ts +136 -0
- package/src/lib/core/shader.ts +258 -0
- package/src/lib/core/texture-loader.ts +476 -0
- package/src/lib/core/textures.ts +235 -0
- package/src/lib/core/types.ts +582 -0
- package/src/lib/core/uniforms.ts +282 -0
- package/src/lib/index.ts +6 -0
- package/src/lib/passes/BlitPass.ts +54 -0
- package/src/lib/passes/CopyPass.ts +80 -0
- package/src/lib/passes/FullscreenPass.ts +173 -0
- package/src/lib/passes/ShaderPass.ts +88 -0
- package/src/lib/passes/index.ts +3 -0
- package/src/lib/react/FragCanvas.tsx +345 -0
- package/src/lib/react/MotionGPUErrorOverlay.tsx +392 -0
- package/src/lib/react/Portal.tsx +34 -0
- package/src/lib/react/advanced.ts +36 -0
- package/src/lib/react/frame-context.ts +169 -0
- package/src/lib/react/index.ts +51 -0
- package/src/lib/react/motiongpu-context.ts +88 -0
- package/src/lib/react/use-motiongpu-user-context.ts +186 -0
- package/src/lib/react/use-texture.ts +233 -0
- package/src/lib/svelte/FragCanvas.svelte +249 -0
- package/src/lib/svelte/MotionGPUErrorOverlay.svelte +382 -0
- package/src/lib/svelte/Portal.svelte +31 -0
- package/src/lib/svelte/advanced.ts +32 -0
- package/src/lib/svelte/frame-context.ts +87 -0
- package/src/lib/svelte/index.ts +51 -0
- package/src/lib/svelte/motiongpu-context.ts +97 -0
- package/src/lib/svelte/use-motiongpu-user-context.ts +145 -0
- package/src/lib/svelte/use-texture.ts +232 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import type { RenderMode } from '../core/types.js';
|
|
3
|
+
import type { CurrentReadable, CurrentWritable } from '../core/current-value.js';
|
|
4
|
+
import type {
|
|
5
|
+
FrameProfilingSnapshot,
|
|
6
|
+
FrameRunTimings,
|
|
7
|
+
FrameScheduleSnapshot
|
|
8
|
+
} from '../core/frame-registry.js';
|
|
9
|
+
import type { MotionGPUScheduler as CoreMotionGPUScheduler } from '../core/scheduler-helpers.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* React context payload exposed by `<FragCanvas>`.
|
|
13
|
+
*/
|
|
14
|
+
export interface MotionGPUContext {
|
|
15
|
+
/**
|
|
16
|
+
* Underlying canvas element used by the renderer.
|
|
17
|
+
*/
|
|
18
|
+
canvas: HTMLCanvasElement | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Reactive canvas pixel size.
|
|
21
|
+
*/
|
|
22
|
+
size: CurrentReadable<{ width: number; height: number }>;
|
|
23
|
+
/**
|
|
24
|
+
* Device pixel ratio multiplier.
|
|
25
|
+
*/
|
|
26
|
+
dpr: CurrentWritable<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Max frame delta clamp passed to scheduled callbacks.
|
|
29
|
+
*/
|
|
30
|
+
maxDelta: CurrentWritable<number>;
|
|
31
|
+
/**
|
|
32
|
+
* Scheduler render mode (`always`, `on-demand`, `manual`).
|
|
33
|
+
*/
|
|
34
|
+
renderMode: CurrentWritable<RenderMode>;
|
|
35
|
+
/**
|
|
36
|
+
* Global toggle for automatic rendering.
|
|
37
|
+
*/
|
|
38
|
+
autoRender: CurrentWritable<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Namespaced user context store shared within the canvas subtree.
|
|
41
|
+
*/
|
|
42
|
+
user: MotionGPUUserContext;
|
|
43
|
+
/**
|
|
44
|
+
* Marks current frame as invalidated.
|
|
45
|
+
*/
|
|
46
|
+
invalidate: () => void;
|
|
47
|
+
/**
|
|
48
|
+
* Requests one manual frame advance.
|
|
49
|
+
*/
|
|
50
|
+
advance: () => void;
|
|
51
|
+
/**
|
|
52
|
+
* Public scheduler API.
|
|
53
|
+
*/
|
|
54
|
+
scheduler: MotionGPUScheduler;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type MotionGPUScheduler = CoreMotionGPUScheduler;
|
|
58
|
+
export type { FrameProfilingSnapshot, FrameRunTimings, FrameScheduleSnapshot };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Namespace identifier for user-owned context entries.
|
|
62
|
+
*/
|
|
63
|
+
export type MotionGPUUserNamespace = string | symbol;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Shared user context store exposed by `FragCanvas`.
|
|
67
|
+
*/
|
|
68
|
+
export type MotionGPUUserContext = CurrentWritable<Record<MotionGPUUserNamespace, unknown>>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Internal React context container.
|
|
72
|
+
*/
|
|
73
|
+
export const MotionGPUReactContext = createContext<MotionGPUContext | null>(null);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns active MotionGPU runtime context.
|
|
77
|
+
*
|
|
78
|
+
* @returns Active context.
|
|
79
|
+
* @throws {Error} When called outside `<FragCanvas>`.
|
|
80
|
+
*/
|
|
81
|
+
export function useMotionGPU(): MotionGPUContext {
|
|
82
|
+
const context = useContext(MotionGPUReactContext);
|
|
83
|
+
if (!context) {
|
|
84
|
+
throw new Error('useMotionGPU must be used inside <FragCanvas>');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return context;
|
|
88
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { useCallback, useMemo } from 'react';
|
|
2
|
+
import type { CurrentReadable } from '../core/current-value.js';
|
|
3
|
+
import {
|
|
4
|
+
useMotionGPU,
|
|
5
|
+
type MotionGPUUserContext,
|
|
6
|
+
type MotionGPUUserNamespace
|
|
7
|
+
} from './motiongpu-context.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Internal shape of the user context store.
|
|
11
|
+
*/
|
|
12
|
+
type UserContextStore = Record<MotionGPUUserNamespace, unknown>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Object-like context payload used by merge semantics.
|
|
16
|
+
*/
|
|
17
|
+
type UserContextEntry = Record<string, unknown>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Controls how a namespaced user context value behaves when already present.
|
|
21
|
+
*/
|
|
22
|
+
export interface SetMotionGPUUserContextOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Conflict strategy when namespace already exists:
|
|
25
|
+
* - `skip`: keep current value
|
|
26
|
+
* - `replace`: replace current value
|
|
27
|
+
* - `merge`: shallow merge object values, fallback to replace otherwise
|
|
28
|
+
*
|
|
29
|
+
* @default 'skip'
|
|
30
|
+
*/
|
|
31
|
+
existing?: 'merge' | 'replace' | 'skip';
|
|
32
|
+
/**
|
|
33
|
+
* How function inputs should be interpreted:
|
|
34
|
+
* - `factory`: call function and store its return value
|
|
35
|
+
* - `value`: store function itself
|
|
36
|
+
*
|
|
37
|
+
* @default 'factory'
|
|
38
|
+
*/
|
|
39
|
+
functionValue?: 'factory' | 'value';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Checks whether a value is a non-array object suitable for shallow merge.
|
|
44
|
+
*/
|
|
45
|
+
function isObjectEntry(value: unknown): value is UserContextEntry {
|
|
46
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Sets a namespaced user context value in the provided user store.
|
|
51
|
+
*
|
|
52
|
+
* Returns the effective value stored under the namespace.
|
|
53
|
+
*/
|
|
54
|
+
function setMotionGPUUserContextInStore<UCT = unknown>(
|
|
55
|
+
userStore: MotionGPUUserContext,
|
|
56
|
+
namespace: MotionGPUUserNamespace,
|
|
57
|
+
value: UCT | (() => UCT),
|
|
58
|
+
options?: SetMotionGPUUserContextOptions
|
|
59
|
+
): UCT | undefined {
|
|
60
|
+
const mode = options?.existing ?? 'skip';
|
|
61
|
+
const functionValueMode = options?.functionValue ?? 'factory';
|
|
62
|
+
let resolvedValue: UCT | undefined;
|
|
63
|
+
|
|
64
|
+
userStore.update((context) => {
|
|
65
|
+
const hasExisting = namespace in context;
|
|
66
|
+
if (hasExisting && mode === 'skip') {
|
|
67
|
+
resolvedValue = context[namespace] as UCT | undefined;
|
|
68
|
+
return context;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const nextValue =
|
|
72
|
+
typeof value === 'function' && functionValueMode === 'factory'
|
|
73
|
+
? (value as () => UCT)()
|
|
74
|
+
: (value as UCT);
|
|
75
|
+
if (hasExisting && mode === 'merge') {
|
|
76
|
+
const currentValue = context[namespace];
|
|
77
|
+
if (isObjectEntry(currentValue) && isObjectEntry(nextValue)) {
|
|
78
|
+
resolvedValue = {
|
|
79
|
+
...currentValue,
|
|
80
|
+
...nextValue
|
|
81
|
+
} as UCT;
|
|
82
|
+
return {
|
|
83
|
+
...context,
|
|
84
|
+
[namespace]: resolvedValue
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
resolvedValue = nextValue;
|
|
90
|
+
return {
|
|
91
|
+
...context,
|
|
92
|
+
[namespace]: nextValue
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return resolvedValue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Returns a read-only view of the entire motiongpu user context store.
|
|
101
|
+
*/
|
|
102
|
+
export function useMotionGPUUserContext<
|
|
103
|
+
UC extends UserContextStore = UserContextStore
|
|
104
|
+
>(): CurrentReadable<UC>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Reads a namespaced user context value as a reactive readable store.
|
|
108
|
+
*/
|
|
109
|
+
export function useMotionGPUUserContext<
|
|
110
|
+
UC extends UserContextStore = UserContextStore,
|
|
111
|
+
K extends keyof UC & MotionGPUUserNamespace = keyof UC & MotionGPUUserNamespace
|
|
112
|
+
>(namespace: K): CurrentReadable<UC[K] | undefined>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Read-only user context hook:
|
|
116
|
+
* - no args: returns full user context store
|
|
117
|
+
* - namespace: returns namespaced store view
|
|
118
|
+
*
|
|
119
|
+
* @param namespace - Optional namespace key.
|
|
120
|
+
*/
|
|
121
|
+
export function useMotionGPUUserContext<
|
|
122
|
+
UC extends UserContextStore = UserContextStore,
|
|
123
|
+
K extends keyof UC & MotionGPUUserNamespace = keyof UC & MotionGPUUserNamespace
|
|
124
|
+
>(namespace?: K): CurrentReadable<UC> | CurrentReadable<UC[K] | undefined> {
|
|
125
|
+
const userStore = useMotionGPU().user;
|
|
126
|
+
const allStore = useMemo<CurrentReadable<UC>>(
|
|
127
|
+
() => ({
|
|
128
|
+
get current() {
|
|
129
|
+
return userStore.current as UC;
|
|
130
|
+
},
|
|
131
|
+
subscribe(run) {
|
|
132
|
+
return userStore.subscribe((context) => run(context as UC));
|
|
133
|
+
}
|
|
134
|
+
}),
|
|
135
|
+
[userStore]
|
|
136
|
+
);
|
|
137
|
+
const scopedStore = useMemo<CurrentReadable<UC[K] | undefined>>(
|
|
138
|
+
() => ({
|
|
139
|
+
get current() {
|
|
140
|
+
return userStore.current[namespace as MotionGPUUserNamespace] as UC[K] | undefined;
|
|
141
|
+
},
|
|
142
|
+
subscribe(run) {
|
|
143
|
+
return userStore.subscribe((context) =>
|
|
144
|
+
run(context[namespace as MotionGPUUserNamespace] as UC[K] | undefined)
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}),
|
|
148
|
+
[namespace, userStore]
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
if (namespace === undefined) {
|
|
152
|
+
return allStore;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return scopedStore;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Returns a stable setter bound to the active MotionGPU user context store.
|
|
160
|
+
*
|
|
161
|
+
* @returns Setter function that preserves namespace write semantics.
|
|
162
|
+
*/
|
|
163
|
+
export function useSetMotionGPUUserContext() {
|
|
164
|
+
const userStore = useMotionGPU().user;
|
|
165
|
+
return useCallback(
|
|
166
|
+
<UCT = unknown>(
|
|
167
|
+
namespace: MotionGPUUserNamespace,
|
|
168
|
+
value: UCT | (() => UCT),
|
|
169
|
+
options?: SetMotionGPUUserContextOptions
|
|
170
|
+
): UCT | undefined => setMotionGPUUserContextInStore(userStore, namespace, value, options),
|
|
171
|
+
[userStore]
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Sets a namespaced user context value with explicit write semantics.
|
|
177
|
+
*
|
|
178
|
+
* Returns the effective value stored under the namespace.
|
|
179
|
+
*/
|
|
180
|
+
export function setMotionGPUUserContext<UCT = unknown>(
|
|
181
|
+
namespace: MotionGPUUserNamespace,
|
|
182
|
+
value: UCT | (() => UCT),
|
|
183
|
+
options?: SetMotionGPUUserContextOptions
|
|
184
|
+
): UCT | undefined {
|
|
185
|
+
return setMotionGPUUserContextInStore(useMotionGPU().user, namespace, value, options);
|
|
186
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
createCurrentWritable as currentWritable,
|
|
4
|
+
type CurrentReadable
|
|
5
|
+
} from '../core/current-value.js';
|
|
6
|
+
import {
|
|
7
|
+
isAbortError,
|
|
8
|
+
loadTexturesFromUrls,
|
|
9
|
+
type LoadedTexture,
|
|
10
|
+
type TextureLoadOptions
|
|
11
|
+
} from '../core/texture-loader.js';
|
|
12
|
+
import { toMotionGPUErrorReport, type MotionGPUErrorReport } from '../core/error-report.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Reactive state returned by `useTexture`.
|
|
16
|
+
*/
|
|
17
|
+
export interface UseTextureResult {
|
|
18
|
+
/**
|
|
19
|
+
* Loaded textures or `null` when unavailable/failed.
|
|
20
|
+
*/
|
|
21
|
+
textures: CurrentReadable<LoadedTexture[] | null>;
|
|
22
|
+
/**
|
|
23
|
+
* `true` while an active load request is running.
|
|
24
|
+
*/
|
|
25
|
+
loading: CurrentReadable<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* Last loading error.
|
|
28
|
+
*/
|
|
29
|
+
error: CurrentReadable<Error | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Last loading error normalized to MotionGPU diagnostics report shape.
|
|
32
|
+
*/
|
|
33
|
+
errorReport: CurrentReadable<MotionGPUErrorReport | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Reloads all textures using current URL input.
|
|
36
|
+
*/
|
|
37
|
+
reload: () => Promise<void>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Supported URL input variants for `useTexture`.
|
|
42
|
+
*/
|
|
43
|
+
export type TextureUrlInput = string[] | (() => string[]);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Normalizes unknown thrown values to an `Error` instance.
|
|
47
|
+
*/
|
|
48
|
+
function toError(error: unknown): Error {
|
|
49
|
+
if (error instanceof Error) {
|
|
50
|
+
return error;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return new Error('Unknown texture loading error');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Releases GPU-side resources for a list of loaded textures.
|
|
58
|
+
*/
|
|
59
|
+
function disposeTextures(list: LoadedTexture[] | null): void {
|
|
60
|
+
for (const texture of list ?? []) {
|
|
61
|
+
texture.dispose();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface MergedAbortSignal {
|
|
66
|
+
signal: AbortSignal;
|
|
67
|
+
dispose: () => void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function mergeAbortSignals(
|
|
71
|
+
primary: AbortSignal,
|
|
72
|
+
secondary: AbortSignal | undefined
|
|
73
|
+
): MergedAbortSignal {
|
|
74
|
+
if (!secondary) {
|
|
75
|
+
return {
|
|
76
|
+
signal: primary,
|
|
77
|
+
dispose: () => {}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (typeof AbortSignal.any === 'function') {
|
|
82
|
+
return {
|
|
83
|
+
signal: AbortSignal.any([primary, secondary]),
|
|
84
|
+
dispose: () => {}
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const fallback = new AbortController();
|
|
89
|
+
let disposed = false;
|
|
90
|
+
const cleanup = (): void => {
|
|
91
|
+
if (disposed) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
disposed = true;
|
|
95
|
+
primary.removeEventListener('abort', abort);
|
|
96
|
+
secondary.removeEventListener('abort', abort);
|
|
97
|
+
};
|
|
98
|
+
const abort = (): void => fallback.abort();
|
|
99
|
+
|
|
100
|
+
primary.addEventListener('abort', abort, { once: true });
|
|
101
|
+
secondary.addEventListener('abort', abort, { once: true });
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
signal: fallback.signal,
|
|
105
|
+
dispose: cleanup
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Loads textures from URLs and exposes reactive loading/error state.
|
|
111
|
+
*
|
|
112
|
+
* @param urlInput - URLs array or lazy URL provider.
|
|
113
|
+
* @param options - Loader options passed to URL fetch/decode pipeline.
|
|
114
|
+
* @returns Reactive texture loading state with reload support.
|
|
115
|
+
*/
|
|
116
|
+
export function useTexture(
|
|
117
|
+
urlInput: TextureUrlInput,
|
|
118
|
+
options: TextureLoadOptions = {}
|
|
119
|
+
): UseTextureResult {
|
|
120
|
+
const texturesRef = useRef(currentWritable<LoadedTexture[] | null>(null));
|
|
121
|
+
const loadingRef = useRef(currentWritable(true));
|
|
122
|
+
const errorRef = useRef(currentWritable<Error | null>(null));
|
|
123
|
+
const errorReportRef = useRef(currentWritable<MotionGPUErrorReport | null>(null));
|
|
124
|
+
const activeControllerRef = useRef<AbortController | null>(null);
|
|
125
|
+
const runningLoadRef = useRef<Promise<void> | null>(null);
|
|
126
|
+
const reloadQueuedRef = useRef(false);
|
|
127
|
+
const requestVersionRef = useRef(0);
|
|
128
|
+
const disposedRef = useRef(false);
|
|
129
|
+
const optionsRef = useRef(options);
|
|
130
|
+
const urlInputRef = useRef(urlInput);
|
|
131
|
+
|
|
132
|
+
optionsRef.current = options;
|
|
133
|
+
urlInputRef.current = urlInput;
|
|
134
|
+
|
|
135
|
+
const getUrls = useCallback((): string[] => {
|
|
136
|
+
const currentInput = urlInputRef.current;
|
|
137
|
+
return typeof currentInput === 'function' ? currentInput() : currentInput;
|
|
138
|
+
}, []);
|
|
139
|
+
|
|
140
|
+
const executeLoad = useCallback(async (): Promise<void> => {
|
|
141
|
+
if (disposedRef.current) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const version = ++requestVersionRef.current;
|
|
146
|
+
const controller = new AbortController();
|
|
147
|
+
activeControllerRef.current = controller;
|
|
148
|
+
loadingRef.current.set(true);
|
|
149
|
+
errorRef.current.set(null);
|
|
150
|
+
errorReportRef.current.set(null);
|
|
151
|
+
|
|
152
|
+
const previous = texturesRef.current.current;
|
|
153
|
+
const mergedSignal = mergeAbortSignals(controller.signal, optionsRef.current.signal);
|
|
154
|
+
try {
|
|
155
|
+
const loaded = await loadTexturesFromUrls(getUrls(), {
|
|
156
|
+
...optionsRef.current,
|
|
157
|
+
signal: mergedSignal.signal
|
|
158
|
+
});
|
|
159
|
+
if (disposedRef.current || version !== requestVersionRef.current) {
|
|
160
|
+
disposeTextures(loaded);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
texturesRef.current.set(loaded);
|
|
165
|
+
disposeTextures(previous);
|
|
166
|
+
} catch (nextError) {
|
|
167
|
+
if (disposedRef.current || version !== requestVersionRef.current) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isAbortError(nextError)) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
disposeTextures(previous);
|
|
176
|
+
texturesRef.current.set(null);
|
|
177
|
+
const normalizedError = toError(nextError);
|
|
178
|
+
errorRef.current.set(normalizedError);
|
|
179
|
+
errorReportRef.current.set(toMotionGPUErrorReport(normalizedError, 'initialization'));
|
|
180
|
+
} finally {
|
|
181
|
+
if (!disposedRef.current && version === requestVersionRef.current) {
|
|
182
|
+
loadingRef.current.set(false);
|
|
183
|
+
}
|
|
184
|
+
if (activeControllerRef.current === controller) {
|
|
185
|
+
activeControllerRef.current = null;
|
|
186
|
+
}
|
|
187
|
+
mergedSignal.dispose();
|
|
188
|
+
}
|
|
189
|
+
}, [getUrls]);
|
|
190
|
+
|
|
191
|
+
const runLoadLoop = useCallback(async (): Promise<void> => {
|
|
192
|
+
do {
|
|
193
|
+
reloadQueuedRef.current = false;
|
|
194
|
+
await executeLoad();
|
|
195
|
+
} while (reloadQueuedRef.current && !disposedRef.current);
|
|
196
|
+
}, [executeLoad]);
|
|
197
|
+
|
|
198
|
+
const load = useCallback((): Promise<void> => {
|
|
199
|
+
activeControllerRef.current?.abort();
|
|
200
|
+
if (runningLoadRef.current) {
|
|
201
|
+
reloadQueuedRef.current = true;
|
|
202
|
+
return runningLoadRef.current;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const pending = runLoadLoop();
|
|
206
|
+
const trackedPending = pending.finally(() => {
|
|
207
|
+
if (runningLoadRef.current === trackedPending) {
|
|
208
|
+
runningLoadRef.current = null;
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
runningLoadRef.current = trackedPending;
|
|
212
|
+
return trackedPending;
|
|
213
|
+
}, [runLoadLoop]);
|
|
214
|
+
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
void load();
|
|
217
|
+
|
|
218
|
+
return () => {
|
|
219
|
+
disposedRef.current = true;
|
|
220
|
+
requestVersionRef.current += 1;
|
|
221
|
+
activeControllerRef.current?.abort();
|
|
222
|
+
disposeTextures(texturesRef.current.current);
|
|
223
|
+
};
|
|
224
|
+
}, [load]);
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
textures: texturesRef.current,
|
|
228
|
+
loading: loadingRef.current,
|
|
229
|
+
error: errorRef.current,
|
|
230
|
+
errorReport: errorReportRef.current,
|
|
231
|
+
reload: load
|
|
232
|
+
};
|
|
233
|
+
}
|