@react-three/fiber 10.0.0-canary.d6fdf72 → 10.0.0-canary.f046a54
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/index.cjs +230 -1195
- package/dist/index.d.cts +25 -720
- package/dist/index.d.mts +25 -720
- package/dist/index.d.ts +25 -720
- package/dist/index.mjs +203 -1167
- package/dist/legacy.cjs +185 -1150
- package/dist/legacy.d.cts +25 -720
- package/dist/legacy.d.mts +25 -720
- package/dist/legacy.d.ts +25 -720
- package/dist/legacy.mjs +158 -1122
- package/dist/webgpu/index.cjs +261 -1208
- package/dist/webgpu/index.d.cts +25 -720
- package/dist/webgpu/index.d.mts +25 -720
- package/dist/webgpu/index.d.ts +25 -720
- package/dist/webgpu/index.mjs +234 -1180
- package/package.json +2 -1
- package/readme.md +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as webgpu from 'three/webgpu';
|
|
2
|
-
import { RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, WebGLCubeRenderTarget, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGPURenderer } from 'three/webgpu';
|
|
2
|
+
import { RenderTarget, CubeReflectionMapping, EquirectangularReflectionMapping, CubeTextureLoader, Scene, WebGLCubeRenderTarget, HalfFloatType, Color, Frustum, Matrix4, Group, BoxGeometry, MeshBasicNodeMaterial, Mesh, Node, NodeUpdateType, Layers, ShaderMaterial, Uniform, SRGBColorSpace, RGBAFormat, UnsignedByteType, Vector3, Vector2, TextureLoader, Texture as Texture$1, CanvasTarget, Raycaster, OrthographicCamera, PerspectiveCamera, PCFShadowMap, VSMShadowMap, BasicShadowMap, ACESFilmicToneMapping, WebGPURenderer } from 'three/webgpu';
|
|
3
3
|
import { WebGLRenderTarget, WebGLRenderer } from 'three';
|
|
4
4
|
import { Inspector } from 'three/addons/inspector/Inspector.js';
|
|
5
5
|
import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
|
|
@@ -15,6 +15,8 @@ import { UltraHDRLoader } from 'three/examples/jsm/loaders/UltraHDRLoader.js';
|
|
|
15
15
|
import { GainMapLoader } from '@monogrid/gainmap-js';
|
|
16
16
|
import Tb, { unstable_scheduleCallback, unstable_IdlePriority } from 'scheduler';
|
|
17
17
|
import { createWithEqualityFn } from 'zustand/traditional';
|
|
18
|
+
import { getScheduler } from '@pmndrs/scheduler';
|
|
19
|
+
export { Scheduler, getScheduler } from '@pmndrs/scheduler';
|
|
18
20
|
import { suspend, preload, clear } from 'suspend-react';
|
|
19
21
|
|
|
20
22
|
function _mergeNamespaces(n, m) {
|
|
@@ -425,9 +427,9 @@ function Environment(props) {
|
|
|
425
427
|
return props.ground ? /* @__PURE__ */ jsx(EnvironmentGround, { ...props }) : props.map ? /* @__PURE__ */ jsx(EnvironmentMap, { ...props }) : props.children ? /* @__PURE__ */ jsx(EnvironmentPortal, { ...props }) : /* @__PURE__ */ jsx(EnvironmentCube, { ...props });
|
|
426
428
|
}
|
|
427
429
|
|
|
428
|
-
var __defProp
|
|
429
|
-
var __defNormalProp
|
|
430
|
-
var __publicField
|
|
430
|
+
var __defProp = Object.defineProperty;
|
|
431
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
432
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
431
433
|
const act = React["act"];
|
|
432
434
|
const useIsomorphicLayoutEffect = /* @__PURE__ */ (() => typeof window !== "undefined" && (window.document?.createElement || window.navigator?.product === "ReactNative"))() ? React.useLayoutEffect : React.useEffect;
|
|
433
435
|
function useMutableCallback(fn) {
|
|
@@ -459,7 +461,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
459
461
|
return _a = class extends React.Component {
|
|
460
462
|
constructor() {
|
|
461
463
|
super(...arguments);
|
|
462
|
-
__publicField
|
|
464
|
+
__publicField(this, "state", { error: false });
|
|
463
465
|
}
|
|
464
466
|
componentDidCatch(err) {
|
|
465
467
|
this.props.set(err);
|
|
@@ -467,7 +469,7 @@ const ErrorBoundary = /* @__PURE__ */ (() => {
|
|
|
467
469
|
render() {
|
|
468
470
|
return this.state.error ? null : this.props.children;
|
|
469
471
|
}
|
|
470
|
-
}, __publicField
|
|
472
|
+
}, __publicField(_a, "getDerivedStateFromError", () => ({ error: true })), _a;
|
|
471
473
|
})();
|
|
472
474
|
|
|
473
475
|
const is = {
|
|
@@ -988,6 +990,19 @@ function applyProps(object, props) {
|
|
|
988
990
|
} else if (isVectorLike(target) && is.num(value)) {
|
|
989
991
|
if ("setScalar" in target && typeof target.setScalar === "function") target.setScalar(value);
|
|
990
992
|
else target.set(value);
|
|
993
|
+
} else if (root instanceof ShaderMaterial && key === "uniforms" && is.obj(value)) {
|
|
994
|
+
if (!is.obj(root.uniforms)) root.uniforms = {};
|
|
995
|
+
const uniforms = root.uniforms;
|
|
996
|
+
const nextUniforms = value;
|
|
997
|
+
for (const name in nextUniforms) {
|
|
998
|
+
const uniform = nextUniforms[name];
|
|
999
|
+
const targetUniform = uniforms[name];
|
|
1000
|
+
if (targetUniform) Object.assign(targetUniform, uniform);
|
|
1001
|
+
else {
|
|
1002
|
+
const nextUniform = uniform instanceof Uniform ? uniform.clone() : new Uniform(uniform.value);
|
|
1003
|
+
uniforms[name] = nextUniform;
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
991
1006
|
} else {
|
|
992
1007
|
root[key] = value;
|
|
993
1008
|
if (key.endsWith("Node") && root.isMaterial) {
|
|
@@ -1627,1036 +1642,12 @@ function notifyAlpha({ message, link }) {
|
|
|
1627
1642
|
}
|
|
1628
1643
|
}
|
|
1629
1644
|
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
/** Ordered list of phase nodes */
|
|
1637
|
-
__publicField$1(this, "phases", []);
|
|
1638
|
-
/** Quick lookup by name */
|
|
1639
|
-
__publicField$1(this, "phaseMap", /* @__PURE__ */ new Map());
|
|
1640
|
-
/** Cached ordered names (invalidated on changes) */
|
|
1641
|
-
__publicField$1(this, "orderedNamesCache", null);
|
|
1642
|
-
this.initializeDefaultPhases();
|
|
1643
|
-
}
|
|
1644
|
-
//* Initialization --------------------------------
|
|
1645
|
-
initializeDefaultPhases() {
|
|
1646
|
-
for (const name of DEFAULT_PHASES) {
|
|
1647
|
-
const node = { name, isAutoGenerated: false };
|
|
1648
|
-
this.phases.push(node);
|
|
1649
|
-
this.phaseMap.set(name, node);
|
|
1650
|
-
}
|
|
1651
|
-
this.invalidateCache();
|
|
1652
|
-
}
|
|
1653
|
-
//* Public API --------------------------------
|
|
1654
|
-
/**
|
|
1655
|
-
* Add a named phase to the graph
|
|
1656
|
-
* @param name - Phase name (must be unique)
|
|
1657
|
-
* @param options - Position options (before or after another phase)
|
|
1658
|
-
*/
|
|
1659
|
-
addPhase(name, options = {}) {
|
|
1660
|
-
if (this.phaseMap.has(name)) {
|
|
1661
|
-
console.warn(`[useFrame] Phase "${name}" already exists`);
|
|
1662
|
-
return;
|
|
1663
|
-
}
|
|
1664
|
-
const { before, after } = options;
|
|
1665
|
-
const node = { name, isAutoGenerated: false };
|
|
1666
|
-
let insertIndex = this.phases.length;
|
|
1667
|
-
const targetIndex = this.getPhaseIndex(before ?? after);
|
|
1668
|
-
if (targetIndex !== -1) {
|
|
1669
|
-
insertIndex = before ? targetIndex : targetIndex + 1;
|
|
1670
|
-
} else {
|
|
1671
|
-
const constraintType = before ? "before" : "after";
|
|
1672
|
-
console.warn(`[useFrame] Phase "${before ?? after}" not found for '${constraintType}' constraint`);
|
|
1673
|
-
}
|
|
1674
|
-
this.phases.splice(insertIndex, 0, node);
|
|
1675
|
-
this.phaseMap.set(name, node);
|
|
1676
|
-
this.invalidateCache();
|
|
1677
|
-
}
|
|
1678
|
-
/**
|
|
1679
|
-
* Get ordered list of phase names
|
|
1680
|
-
*/
|
|
1681
|
-
getOrderedPhases() {
|
|
1682
|
-
if (this.orderedNamesCache === null) this.orderedNamesCache = this.phases.map((p) => p.name);
|
|
1683
|
-
return this.orderedNamesCache;
|
|
1684
|
-
}
|
|
1685
|
-
/**
|
|
1686
|
-
* Check if a phase exists
|
|
1687
|
-
*/
|
|
1688
|
-
hasPhase(name) {
|
|
1689
|
-
return this.phaseMap.has(name);
|
|
1690
|
-
}
|
|
1691
|
-
/**
|
|
1692
|
-
* Get the index of a phase (-1 if not found)
|
|
1693
|
-
*/
|
|
1694
|
-
getPhaseIndex(name) {
|
|
1695
|
-
if (!name) return -1;
|
|
1696
|
-
return this.phases.findIndex((p) => p.name === name);
|
|
1697
|
-
}
|
|
1698
|
-
/**
|
|
1699
|
-
* Ensure a phase exists, creating an auto-generated one if needed.
|
|
1700
|
-
* Used for resolving before/after constraints.
|
|
1701
|
-
*
|
|
1702
|
-
* @param name - The phase name to ensure exists
|
|
1703
|
-
* @returns The phase name (may be auto-generated like 'before:render')
|
|
1704
|
-
*/
|
|
1705
|
-
ensurePhase(name) {
|
|
1706
|
-
if (this.phaseMap.has(name)) return name;
|
|
1707
|
-
const node = { name, isAutoGenerated: true };
|
|
1708
|
-
this.phases.push(node);
|
|
1709
|
-
this.phaseMap.set(name, node);
|
|
1710
|
-
this.invalidateCache();
|
|
1711
|
-
return name;
|
|
1712
|
-
}
|
|
1713
|
-
/**
|
|
1714
|
-
* Resolve where a job with before/after constraints should go.
|
|
1715
|
-
* Creates auto-generated phases if needed.
|
|
1716
|
-
*
|
|
1717
|
-
* @param before - Phase(s) to run before
|
|
1718
|
-
* @param after - Phase(s) to run after
|
|
1719
|
-
* @returns The resolved phase name
|
|
1720
|
-
*/
|
|
1721
|
-
resolveConstraintPhase(before, after) {
|
|
1722
|
-
const beforeArr = before ? Array.isArray(before) ? before : [before] : [];
|
|
1723
|
-
const afterArr = after ? Array.isArray(after) ? after : [after] : [];
|
|
1724
|
-
if (beforeArr.length > 0) {
|
|
1725
|
-
return this.ensureAutoPhase(beforeArr[0], "before", 0);
|
|
1726
|
-
}
|
|
1727
|
-
if (afterArr.length > 0) {
|
|
1728
|
-
return this.ensureAutoPhase(afterArr[0], "after", 1);
|
|
1729
|
-
}
|
|
1730
|
-
return "update";
|
|
1731
|
-
}
|
|
1732
|
-
/**
|
|
1733
|
-
* Ensure an auto-generated phase exists relative to a target phase.
|
|
1734
|
-
* Creates the phase if it doesn't exist, inserting it at the correct position.
|
|
1735
|
-
*
|
|
1736
|
-
* @param target - The target phase name to position relative to
|
|
1737
|
-
* @param prefix - Prefix for auto-generated phase name ('before' or 'after')
|
|
1738
|
-
* @param offset - Insertion offset (0 for before, 1 for after)
|
|
1739
|
-
* @returns The auto-generated phase name
|
|
1740
|
-
*/
|
|
1741
|
-
ensureAutoPhase(target, prefix, offset) {
|
|
1742
|
-
const autoName = `${prefix}:${target}`;
|
|
1743
|
-
if (this.phaseMap.has(autoName)) return autoName;
|
|
1744
|
-
const node = { name: autoName, isAutoGenerated: true };
|
|
1745
|
-
const targetIndex = this.getPhaseIndex(target);
|
|
1746
|
-
if (targetIndex !== -1) this.phases.splice(targetIndex + offset, 0, node);
|
|
1747
|
-
else this.phases.push(node);
|
|
1748
|
-
this.phaseMap.set(autoName, node);
|
|
1749
|
-
this.invalidateCache();
|
|
1750
|
-
return autoName;
|
|
1751
|
-
}
|
|
1752
|
-
// Internal --------------------------------
|
|
1753
|
-
invalidateCache() {
|
|
1754
|
-
this.orderedNamesCache = null;
|
|
1755
|
-
}
|
|
1756
|
-
}
|
|
1757
|
-
|
|
1758
|
-
function rebuildSortedJobs(jobs, phaseGraph) {
|
|
1759
|
-
const orderedPhases = phaseGraph.getOrderedPhases();
|
|
1760
|
-
const buckets = /* @__PURE__ */ new Map();
|
|
1761
|
-
for (const phase of orderedPhases) {
|
|
1762
|
-
buckets.set(phase, []);
|
|
1763
|
-
}
|
|
1764
|
-
for (const job of jobs.values()) {
|
|
1765
|
-
if (!job.enabled) continue;
|
|
1766
|
-
let bucket = buckets.get(job.phase);
|
|
1767
|
-
if (!bucket) {
|
|
1768
|
-
bucket = [];
|
|
1769
|
-
buckets.set(job.phase, bucket);
|
|
1770
|
-
}
|
|
1771
|
-
bucket.push(job);
|
|
1772
|
-
}
|
|
1773
|
-
const sortedBuckets = [];
|
|
1774
|
-
for (const phase of orderedPhases) {
|
|
1775
|
-
const bucket = buckets.get(phase);
|
|
1776
|
-
if (!bucket || bucket.length === 0) continue;
|
|
1777
|
-
bucket.sort((a, b) => {
|
|
1778
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1779
|
-
return a.index - b.index;
|
|
1780
|
-
});
|
|
1781
|
-
sortedBuckets.push(hasCrossJobConstraints(bucket) ? topologicalSort(bucket) : bucket);
|
|
1782
|
-
}
|
|
1783
|
-
for (const [phase, bucket] of buckets) {
|
|
1784
|
-
if (!orderedPhases.includes(phase) && bucket.length > 0) {
|
|
1785
|
-
bucket.sort((a, b) => {
|
|
1786
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1787
|
-
return a.index - b.index;
|
|
1788
|
-
});
|
|
1789
|
-
sortedBuckets.push(bucket);
|
|
1790
|
-
}
|
|
1791
|
-
}
|
|
1792
|
-
return sortedBuckets.flat();
|
|
1793
|
-
}
|
|
1794
|
-
function hasCrossJobConstraints(bucket) {
|
|
1795
|
-
const jobIds = new Set(bucket.map((j) => j.id));
|
|
1796
|
-
for (const job of bucket) {
|
|
1797
|
-
for (const ref of job.before) {
|
|
1798
|
-
if (jobIds.has(ref)) return true;
|
|
1799
|
-
}
|
|
1800
|
-
for (const ref of job.after) {
|
|
1801
|
-
if (jobIds.has(ref)) return true;
|
|
1802
|
-
}
|
|
1803
|
-
}
|
|
1804
|
-
return false;
|
|
1805
|
-
}
|
|
1806
|
-
function topologicalSort(jobs) {
|
|
1807
|
-
const n = jobs.length;
|
|
1808
|
-
if (n <= 1) return jobs;
|
|
1809
|
-
const jobMap = /* @__PURE__ */ new Map();
|
|
1810
|
-
const inDegree = /* @__PURE__ */ new Map();
|
|
1811
|
-
const adjacency = /* @__PURE__ */ new Map();
|
|
1812
|
-
for (const job of jobs) {
|
|
1813
|
-
jobMap.set(job.id, job);
|
|
1814
|
-
inDegree.set(job.id, 0);
|
|
1815
|
-
adjacency.set(job.id, []);
|
|
1816
|
-
}
|
|
1817
|
-
for (const job of jobs) {
|
|
1818
|
-
for (const ref of job.before) {
|
|
1819
|
-
if (jobMap.has(ref)) {
|
|
1820
|
-
adjacency.get(job.id).push(ref);
|
|
1821
|
-
inDegree.set(ref, inDegree.get(ref) + 1);
|
|
1822
|
-
}
|
|
1823
|
-
}
|
|
1824
|
-
for (const ref of job.after) {
|
|
1825
|
-
if (jobMap.has(ref)) {
|
|
1826
|
-
adjacency.get(ref).push(job.id);
|
|
1827
|
-
inDegree.set(job.id, inDegree.get(job.id) + 1);
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
}
|
|
1831
|
-
const queue = [];
|
|
1832
|
-
for (const job of jobs) {
|
|
1833
|
-
if (inDegree.get(job.id) === 0) {
|
|
1834
|
-
queue.push(job);
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1837
|
-
queue.sort((a, b) => {
|
|
1838
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
1839
|
-
return a.index - b.index;
|
|
1840
|
-
});
|
|
1841
|
-
const result = [];
|
|
1842
|
-
while (queue.length > 0) {
|
|
1843
|
-
const job = queue.shift();
|
|
1844
|
-
result.push(job);
|
|
1845
|
-
const neighbors = adjacency.get(job.id) || [];
|
|
1846
|
-
for (const neighborId of neighbors) {
|
|
1847
|
-
const newDegree = inDegree.get(neighborId) - 1;
|
|
1848
|
-
inDegree.set(neighborId, newDegree);
|
|
1849
|
-
if (newDegree === 0) {
|
|
1850
|
-
const neighbor = jobMap.get(neighborId);
|
|
1851
|
-
insertSorted(queue, neighbor);
|
|
1852
|
-
}
|
|
1853
|
-
}
|
|
1854
|
-
}
|
|
1855
|
-
if (result.length !== n) {
|
|
1856
|
-
console.warn("[useFrame] Circular dependency detected in job constraints");
|
|
1857
|
-
const resultIds = new Set(result.map((j) => j.id));
|
|
1858
|
-
for (const job of jobs) {
|
|
1859
|
-
if (!resultIds.has(job.id)) result.push(job);
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
return result;
|
|
1863
|
-
}
|
|
1864
|
-
function insertSorted(arr, job) {
|
|
1865
|
-
let i = 0;
|
|
1866
|
-
while (i < arr.length) {
|
|
1867
|
-
const cmp = arr[i];
|
|
1868
|
-
if (job.priority > cmp.priority || job.priority === cmp.priority && job.index < cmp.index) {
|
|
1869
|
-
break;
|
|
1870
|
-
}
|
|
1871
|
-
i++;
|
|
1872
|
-
}
|
|
1873
|
-
arr.splice(i, 0, job);
|
|
1874
|
-
}
|
|
1875
|
-
|
|
1876
|
-
function shouldRun(job, now) {
|
|
1877
|
-
if (!job.enabled) return false;
|
|
1878
|
-
if (!job.fps) return true;
|
|
1879
|
-
const minInterval = 1e3 / job.fps;
|
|
1880
|
-
const lastRun = job.lastRun ?? 0;
|
|
1881
|
-
const elapsed = now - lastRun;
|
|
1882
|
-
if (elapsed < minInterval - 1) return false;
|
|
1883
|
-
if (job.drop) {
|
|
1884
|
-
job.lastRun = now;
|
|
1885
|
-
} else {
|
|
1886
|
-
const steps = Math.floor(elapsed / minInterval);
|
|
1887
|
-
job.lastRun = lastRun + steps * minInterval;
|
|
1888
|
-
if (job.lastRun < now - minInterval) {
|
|
1889
|
-
job.lastRun = now;
|
|
1890
|
-
}
|
|
1891
|
-
}
|
|
1892
|
-
return true;
|
|
1893
|
-
}
|
|
1894
|
-
function resetJobTiming(job) {
|
|
1895
|
-
job.lastRun = void 0;
|
|
1896
|
-
}
|
|
1897
|
-
|
|
1898
|
-
var __defProp = Object.defineProperty;
|
|
1899
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1900
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1901
|
-
const hmrData = (() => {
|
|
1902
|
-
if (typeof process !== "undefined" && process.env.NODE_ENV === "test") return void 0;
|
|
1903
|
-
if (typeof import_meta_hot !== "undefined") return import_meta_hot;
|
|
1904
|
-
try {
|
|
1905
|
-
return (0, eval)("import.meta.hot");
|
|
1906
|
-
} catch {
|
|
1907
|
-
return void 0;
|
|
1908
|
-
}
|
|
1909
|
-
})();
|
|
1910
|
-
const _Scheduler = class _Scheduler {
|
|
1911
|
-
//* Constructor ================================
|
|
1912
|
-
constructor() {
|
|
1913
|
-
//* Critical State ================================
|
|
1914
|
-
__publicField(this, "roots", /* @__PURE__ */ new Map());
|
|
1915
|
-
__publicField(this, "phaseGraph");
|
|
1916
|
-
__publicField(this, "loopState", {
|
|
1917
|
-
running: false,
|
|
1918
|
-
rafHandle: null,
|
|
1919
|
-
lastTime: null,
|
|
1920
|
-
// null = uninitialized, 0+ = valid timestamp
|
|
1921
|
-
frameCount: 0,
|
|
1922
|
-
elapsedTime: 0,
|
|
1923
|
-
createdAt: performance.now()
|
|
1924
|
-
});
|
|
1925
|
-
__publicField(this, "stoppedTime", 0);
|
|
1926
|
-
//* Private State ================================
|
|
1927
|
-
__publicField(this, "nextRootIndex", 0);
|
|
1928
|
-
__publicField(this, "globalBeforeJobs", /* @__PURE__ */ new Map());
|
|
1929
|
-
__publicField(this, "globalAfterJobs", /* @__PURE__ */ new Map());
|
|
1930
|
-
__publicField(this, "nextGlobalIndex", 0);
|
|
1931
|
-
__publicField(this, "idleCallbacks", /* @__PURE__ */ new Set());
|
|
1932
|
-
__publicField(this, "nextJobIndex", 0);
|
|
1933
|
-
__publicField(this, "jobStateListeners", /* @__PURE__ */ new Map());
|
|
1934
|
-
__publicField(this, "pendingFrames", 0);
|
|
1935
|
-
__publicField(this, "_frameloop", "always");
|
|
1936
|
-
//* Independent Mode & Error Handling State ================================
|
|
1937
|
-
__publicField(this, "_independent", false);
|
|
1938
|
-
__publicField(this, "errorHandler", null);
|
|
1939
|
-
__publicField(this, "rootReadyCallbacks", /* @__PURE__ */ new Set());
|
|
1940
|
-
//* Core Loop Execution Methods ================================
|
|
1941
|
-
/**
|
|
1942
|
-
* Main RAF loop callback.
|
|
1943
|
-
* Executes frame, handles demand mode, and schedules next frame.
|
|
1944
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
1945
|
-
* @returns {void}
|
|
1946
|
-
* @private
|
|
1947
|
-
*/
|
|
1948
|
-
__publicField(this, "loop", (timestamp) => {
|
|
1949
|
-
if (!this.loopState.running) return;
|
|
1950
|
-
this.executeFrame(timestamp);
|
|
1951
|
-
if (this._frameloop === "demand") {
|
|
1952
|
-
this.pendingFrames = Math.max(0, this.pendingFrames - 1);
|
|
1953
|
-
if (this.pendingFrames === 0) {
|
|
1954
|
-
this.notifyIdle(timestamp);
|
|
1955
|
-
return this.stop();
|
|
1956
|
-
}
|
|
1957
|
-
}
|
|
1958
|
-
this.loopState.rafHandle = requestAnimationFrame(this.loop);
|
|
1959
|
-
});
|
|
1960
|
-
this.phaseGraph = new PhaseGraph();
|
|
1961
|
-
}
|
|
1962
|
-
static get instance() {
|
|
1963
|
-
return globalThis[_Scheduler.INSTANCE_KEY] ?? null;
|
|
1964
|
-
}
|
|
1965
|
-
static set instance(value) {
|
|
1966
|
-
globalThis[_Scheduler.INSTANCE_KEY] = value;
|
|
1967
|
-
}
|
|
1968
|
-
/**
|
|
1969
|
-
* Get the global scheduler instance (creates if doesn't exist).
|
|
1970
|
-
* Uses HMR data to preserve instance across hot reloads.
|
|
1971
|
-
* @returns {Scheduler} The singleton scheduler instance
|
|
1972
|
-
*/
|
|
1973
|
-
static get() {
|
|
1974
|
-
if (!_Scheduler.instance && hmrData?.data?.scheduler) {
|
|
1975
|
-
_Scheduler.instance = hmrData.data.scheduler;
|
|
1976
|
-
}
|
|
1977
|
-
if (!_Scheduler.instance) {
|
|
1978
|
-
_Scheduler.instance = new _Scheduler();
|
|
1979
|
-
if (hmrData?.data) {
|
|
1980
|
-
hmrData.data.scheduler = _Scheduler.instance;
|
|
1981
|
-
}
|
|
1982
|
-
}
|
|
1983
|
-
return _Scheduler.instance;
|
|
1984
|
-
}
|
|
1985
|
-
/**
|
|
1986
|
-
* Reset the singleton instance. Stops the loop and clears all state.
|
|
1987
|
-
* Primarily used for testing to ensure clean state between tests.
|
|
1988
|
-
* @returns {void}
|
|
1989
|
-
*/
|
|
1990
|
-
static reset() {
|
|
1991
|
-
if (_Scheduler.instance) {
|
|
1992
|
-
_Scheduler.instance.stop();
|
|
1993
|
-
_Scheduler.instance = null;
|
|
1994
|
-
}
|
|
1995
|
-
if (hmrData?.data) {
|
|
1996
|
-
hmrData.data.scheduler = null;
|
|
1997
|
-
}
|
|
1998
|
-
}
|
|
1999
|
-
//* Getters & Setters ================================
|
|
2000
|
-
get phases() {
|
|
2001
|
-
return this.phaseGraph.getOrderedPhases();
|
|
2002
|
-
}
|
|
2003
|
-
get frameloop() {
|
|
2004
|
-
return this._frameloop;
|
|
2005
|
-
}
|
|
2006
|
-
set frameloop(mode) {
|
|
2007
|
-
if (this._frameloop === mode) return;
|
|
2008
|
-
const wasAlways = this._frameloop === "always";
|
|
2009
|
-
this._frameloop = mode;
|
|
2010
|
-
if (mode === "always" && !this.loopState.running && this.roots.size > 0) this.start();
|
|
2011
|
-
else if (mode !== "always" && wasAlways) this.stop();
|
|
2012
|
-
}
|
|
2013
|
-
get isRunning() {
|
|
2014
|
-
return this.loopState.running;
|
|
2015
|
-
}
|
|
2016
|
-
get isReady() {
|
|
2017
|
-
return this.roots.size > 0;
|
|
2018
|
-
}
|
|
2019
|
-
get independent() {
|
|
2020
|
-
return this._independent;
|
|
2021
|
-
}
|
|
2022
|
-
set independent(value) {
|
|
2023
|
-
this._independent = value;
|
|
2024
|
-
if (value) this.ensureDefaultRoot();
|
|
2025
|
-
}
|
|
2026
|
-
//* Root Management Methods ================================
|
|
2027
|
-
/**
|
|
2028
|
-
* Register a root (Canvas) with the scheduler.
|
|
2029
|
-
* The first root to register starts the RAF loop (if frameloop='always').
|
|
2030
|
-
* @param {string} id - Unique identifier for this root
|
|
2031
|
-
* @param {RootOptions} [options] - Optional configuration with getState and onError callbacks
|
|
2032
|
-
* @returns {() => void} Unsubscribe function to remove this root
|
|
2033
|
-
*/
|
|
2034
|
-
registerRoot(id, options = {}) {
|
|
2035
|
-
if (this.roots.has(id)) {
|
|
2036
|
-
console.warn(`[Scheduler] Root "${id}" already registered`);
|
|
2037
|
-
return () => this.unregisterRoot(id);
|
|
2038
|
-
}
|
|
2039
|
-
const entry = {
|
|
2040
|
-
id,
|
|
2041
|
-
getState: options.getState ?? (() => ({})),
|
|
2042
|
-
jobs: /* @__PURE__ */ new Map(),
|
|
2043
|
-
sortedJobs: [],
|
|
2044
|
-
needsRebuild: false
|
|
2045
|
-
};
|
|
2046
|
-
if (options.onError) {
|
|
2047
|
-
this.errorHandler = options.onError;
|
|
2048
|
-
}
|
|
2049
|
-
this.roots.set(id, entry);
|
|
2050
|
-
if (this.roots.size === 1) {
|
|
2051
|
-
this.notifyRootReady();
|
|
2052
|
-
if (this._frameloop === "always") this.start();
|
|
2053
|
-
}
|
|
2054
|
-
return () => this.unregisterRoot(id);
|
|
2055
|
-
}
|
|
2056
|
-
/**
|
|
2057
|
-
* Unregister a root from the scheduler.
|
|
2058
|
-
* Cleans up all job state listeners for this root's jobs.
|
|
2059
|
-
* The last root to unregister stops the RAF loop.
|
|
2060
|
-
* @param {string} id - The root ID to unregister
|
|
2061
|
-
* @returns {void}
|
|
2062
|
-
*/
|
|
2063
|
-
unregisterRoot(id) {
|
|
2064
|
-
const root = this.roots.get(id);
|
|
2065
|
-
if (!root) return;
|
|
2066
|
-
for (const jobId of root.jobs.keys()) {
|
|
2067
|
-
this.jobStateListeners.delete(jobId);
|
|
2068
|
-
}
|
|
2069
|
-
this.roots.delete(id);
|
|
2070
|
-
if (this.roots.size === 0) {
|
|
2071
|
-
this.stop();
|
|
2072
|
-
this.errorHandler = null;
|
|
2073
|
-
}
|
|
2074
|
-
}
|
|
2075
|
-
/**
|
|
2076
|
-
* Subscribe to be notified when a root becomes available.
|
|
2077
|
-
* Fires immediately if a root already exists.
|
|
2078
|
-
* @param {() => void} callback - Function called when first root registers
|
|
2079
|
-
* @returns {() => void} Unsubscribe function
|
|
2080
|
-
*/
|
|
2081
|
-
onRootReady(callback) {
|
|
2082
|
-
if (this.roots.size > 0) {
|
|
2083
|
-
callback();
|
|
2084
|
-
return () => {
|
|
2085
|
-
};
|
|
2086
|
-
}
|
|
2087
|
-
this.rootReadyCallbacks.add(callback);
|
|
2088
|
-
return () => this.rootReadyCallbacks.delete(callback);
|
|
2089
|
-
}
|
|
2090
|
-
/**
|
|
2091
|
-
* Notify all registered root-ready callbacks.
|
|
2092
|
-
* Called when the first root registers.
|
|
2093
|
-
* @returns {void}
|
|
2094
|
-
* @private
|
|
2095
|
-
*/
|
|
2096
|
-
notifyRootReady() {
|
|
2097
|
-
for (const cb of this.rootReadyCallbacks) {
|
|
2098
|
-
try {
|
|
2099
|
-
cb();
|
|
2100
|
-
} catch (error) {
|
|
2101
|
-
console.error("[Scheduler] Error in root-ready callback:", error);
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
this.rootReadyCallbacks.clear();
|
|
2105
|
-
}
|
|
2106
|
-
/**
|
|
2107
|
-
* Ensure a default root exists for independent mode.
|
|
2108
|
-
* Creates a minimal root with no state provider.
|
|
2109
|
-
* @returns {void}
|
|
2110
|
-
* @private
|
|
2111
|
-
*/
|
|
2112
|
-
ensureDefaultRoot() {
|
|
2113
|
-
if (!this.roots.has("__default__")) {
|
|
2114
|
-
this.registerRoot("__default__");
|
|
2115
|
-
}
|
|
2116
|
-
}
|
|
2117
|
-
/**
|
|
2118
|
-
* Trigger error handling for job errors.
|
|
2119
|
-
* Uses the bound error handler if available, otherwise logs to console.
|
|
2120
|
-
* @param {Error} error - The error to handle
|
|
2121
|
-
* @returns {void}
|
|
2122
|
-
*/
|
|
2123
|
-
triggerError(error) {
|
|
2124
|
-
if (this.errorHandler) this.errorHandler(error);
|
|
2125
|
-
else console.error("[Scheduler]", error);
|
|
2126
|
-
}
|
|
2127
|
-
//* Phase Management Methods ================================
|
|
2128
|
-
/**
|
|
2129
|
-
* Add a named phase to the scheduler's execution order.
|
|
2130
|
-
* Marks all roots for rebuild to incorporate the new phase.
|
|
2131
|
-
* @param {string} name - The phase name (e.g., 'physics', 'postprocess')
|
|
2132
|
-
* @param {AddPhaseOptions} [options] - Positioning options (before/after other phases)
|
|
2133
|
-
* @returns {void}
|
|
2134
|
-
* @example
|
|
2135
|
-
* scheduler.addPhase('physics', { before: 'update' });
|
|
2136
|
-
* scheduler.addPhase('postprocess', { after: 'render' });
|
|
2137
|
-
*/
|
|
2138
|
-
addPhase(name, options) {
|
|
2139
|
-
this.phaseGraph.addPhase(name, options);
|
|
2140
|
-
for (const root of this.roots.values()) {
|
|
2141
|
-
root.needsRebuild = true;
|
|
2142
|
-
}
|
|
2143
|
-
}
|
|
2144
|
-
/**
|
|
2145
|
-
* Check if a phase exists in the scheduler.
|
|
2146
|
-
* @param {string} name - The phase name to check
|
|
2147
|
-
* @returns {boolean} True if the phase exists
|
|
2148
|
-
*/
|
|
2149
|
-
hasPhase(name) {
|
|
2150
|
-
return this.phaseGraph.hasPhase(name);
|
|
2151
|
-
}
|
|
2152
|
-
//* Global Job Registration Methods (Deprecated APIs) ================================
|
|
2153
|
-
/**
|
|
2154
|
-
* Register a global job that runs once per frame (not per-root).
|
|
2155
|
-
* Used internally by deprecated addEffect/addAfterEffect APIs.
|
|
2156
|
-
* @param {'before' | 'after'} phase - When to run: 'before' all roots or 'after' all roots
|
|
2157
|
-
* @param {string} id - Unique identifier for this global job
|
|
2158
|
-
* @param {(timestamp: number) => void} callback - Function called each frame with RAF timestamp
|
|
2159
|
-
* @returns {() => void} Unsubscribe function to remove this global job
|
|
2160
|
-
* @deprecated Use useFrame with phases instead
|
|
2161
|
-
*/
|
|
2162
|
-
registerGlobal(phase, id, callback) {
|
|
2163
|
-
const job = { id, callback };
|
|
2164
|
-
if (phase === "before") {
|
|
2165
|
-
this.globalBeforeJobs.set(id, job);
|
|
2166
|
-
} else {
|
|
2167
|
-
this.globalAfterJobs.set(id, job);
|
|
2168
|
-
}
|
|
2169
|
-
return () => {
|
|
2170
|
-
if (phase === "before") this.globalBeforeJobs.delete(id);
|
|
2171
|
-
else this.globalAfterJobs.delete(id);
|
|
2172
|
-
};
|
|
2173
|
-
}
|
|
2174
|
-
//* Idle Callback Methods (Deprecated API) ================================
|
|
2175
|
-
/**
|
|
2176
|
-
* Register an idle callback that fires when the loop stops.
|
|
2177
|
-
* Used internally by deprecated addTail API.
|
|
2178
|
-
* @param {(timestamp: number) => void} callback - Function called when loop becomes idle
|
|
2179
|
-
* @returns {() => void} Unsubscribe function to remove this idle callback
|
|
2180
|
-
* @deprecated Use demand mode with invalidate() instead
|
|
2181
|
-
*/
|
|
2182
|
-
onIdle(callback) {
|
|
2183
|
-
this.idleCallbacks.add(callback);
|
|
2184
|
-
return () => this.idleCallbacks.delete(callback);
|
|
2185
|
-
}
|
|
2186
|
-
/**
|
|
2187
|
-
* Notify all registered idle callbacks.
|
|
2188
|
-
* Called when the loop stops in demand mode.
|
|
2189
|
-
* @param {number} timestamp - The RAF timestamp when idle occurred
|
|
2190
|
-
* @returns {void}
|
|
2191
|
-
* @private
|
|
2192
|
-
*/
|
|
2193
|
-
notifyIdle(timestamp) {
|
|
2194
|
-
for (const cb of this.idleCallbacks) {
|
|
2195
|
-
try {
|
|
2196
|
-
cb(timestamp);
|
|
2197
|
-
} catch (error) {
|
|
2198
|
-
console.error("[Scheduler] Error in idle callback:", error);
|
|
2199
|
-
}
|
|
2200
|
-
}
|
|
2201
|
-
}
|
|
2202
|
-
//* Job Registration & Management Methods ================================
|
|
2203
|
-
/**
|
|
2204
|
-
* Register a job (frame callback) with a specific root.
|
|
2205
|
-
* This is the core registration method used by useFrame internally.
|
|
2206
|
-
* @param {FrameNextCallback} callback - The function to call each frame
|
|
2207
|
-
* @param {JobOptions & { rootId?: string; system?: boolean }} [options] - Job configuration
|
|
2208
|
-
* @param {string} [options.rootId] - Target root ID (defaults to first registered root)
|
|
2209
|
-
* @param {string} [options.id] - Unique job ID (auto-generated if not provided)
|
|
2210
|
-
* @param {string} [options.phase] - Execution phase (defaults to 'update')
|
|
2211
|
-
* @param {number} [options.priority] - Priority within phase (higher = earlier, default 0)
|
|
2212
|
-
* @param {number} [options.fps] - FPS throttle limit
|
|
2213
|
-
* @param {boolean} [options.drop] - Drop frames when behind (default true)
|
|
2214
|
-
* @param {boolean} [options.enabled] - Whether job is active (default true)
|
|
2215
|
-
* @param {boolean} [options.system] - Internal flag for system jobs (not user-facing)
|
|
2216
|
-
* @returns {() => void} Unsubscribe function to remove this job
|
|
2217
|
-
*/
|
|
2218
|
-
register(callback, options = {}) {
|
|
2219
|
-
const rootId = options.rootId;
|
|
2220
|
-
const root = rootId ? this.roots.get(rootId) : this.roots.values().next().value;
|
|
2221
|
-
if (!root) {
|
|
2222
|
-
console.warn("[Scheduler] No root registered. Is this inside a Canvas?");
|
|
2223
|
-
return () => {
|
|
2224
|
-
};
|
|
2225
|
-
}
|
|
2226
|
-
const id = options.id ?? this.generateJobId();
|
|
2227
|
-
let phase = options.phase ?? "update";
|
|
2228
|
-
if (!options.phase && (options.before || options.after)) {
|
|
2229
|
-
phase = this.phaseGraph.resolveConstraintPhase(options.before, options.after);
|
|
2230
|
-
}
|
|
2231
|
-
const before = this.normalizeConstraints(options.before);
|
|
2232
|
-
const after = this.normalizeConstraints(options.after);
|
|
2233
|
-
const job = {
|
|
2234
|
-
id,
|
|
2235
|
-
callback,
|
|
2236
|
-
phase,
|
|
2237
|
-
before,
|
|
2238
|
-
after,
|
|
2239
|
-
priority: options.priority ?? 0,
|
|
2240
|
-
index: this.nextJobIndex++,
|
|
2241
|
-
fps: options.fps,
|
|
2242
|
-
drop: options.drop ?? true,
|
|
2243
|
-
enabled: options.enabled ?? true,
|
|
2244
|
-
system: options.system ?? false
|
|
2245
|
-
};
|
|
2246
|
-
if (root.jobs.has(id)) {
|
|
2247
|
-
console.warn(`[useFrame] Job with id "${id}" already exists, replacing`);
|
|
2248
|
-
}
|
|
2249
|
-
root.jobs.set(id, job);
|
|
2250
|
-
root.needsRebuild = true;
|
|
2251
|
-
return () => this.unregister(id, root.id);
|
|
2252
|
-
}
|
|
2253
|
-
/**
|
|
2254
|
-
* Unregister a job by its ID.
|
|
2255
|
-
* Searches all roots if rootId is not provided.
|
|
2256
|
-
* @param {string} id - The job ID to unregister
|
|
2257
|
-
* @param {string} [rootId] - Optional root ID to search (searches all if not provided)
|
|
2258
|
-
* @returns {void}
|
|
2259
|
-
*/
|
|
2260
|
-
unregister(id, rootId) {
|
|
2261
|
-
const root = rootId ? this.roots.get(rootId) : Array.from(this.roots.values()).find((r) => r.jobs.has(id));
|
|
2262
|
-
if (root?.jobs.delete(id)) {
|
|
2263
|
-
root.needsRebuild = true;
|
|
2264
|
-
this.jobStateListeners.delete(id);
|
|
2265
|
-
}
|
|
2266
|
-
}
|
|
2267
|
-
/**
|
|
2268
|
-
* Update a job's options dynamically.
|
|
2269
|
-
* Searches all roots to find the job by ID.
|
|
2270
|
-
* Phase/constraint changes trigger a rebuild of the sorted job list.
|
|
2271
|
-
* @param {string} id - The job ID to update
|
|
2272
|
-
* @param {Partial<JobOptions>} options - The options to update
|
|
2273
|
-
* @returns {void}
|
|
2274
|
-
*/
|
|
2275
|
-
updateJob(id, options) {
|
|
2276
|
-
let job;
|
|
2277
|
-
let root;
|
|
2278
|
-
for (const r of this.roots.values()) {
|
|
2279
|
-
job = r.jobs.get(id);
|
|
2280
|
-
if (job) {
|
|
2281
|
-
root = r;
|
|
2282
|
-
break;
|
|
2283
|
-
}
|
|
2284
|
-
}
|
|
2285
|
-
if (!job || !root) return;
|
|
2286
|
-
if (options.priority !== void 0) job.priority = options.priority;
|
|
2287
|
-
if (options.fps !== void 0) job.fps = options.fps;
|
|
2288
|
-
if (options.drop !== void 0) job.drop = options.drop;
|
|
2289
|
-
if (options.enabled !== void 0) {
|
|
2290
|
-
const wasEnabled = job.enabled;
|
|
2291
|
-
job.enabled = options.enabled;
|
|
2292
|
-
if (!wasEnabled && job.enabled) resetJobTiming(job);
|
|
2293
|
-
if (wasEnabled !== job.enabled) root.needsRebuild = true;
|
|
2294
|
-
}
|
|
2295
|
-
if (options.phase !== void 0 || options.before !== void 0 || options.after !== void 0) {
|
|
2296
|
-
if (options.phase) job.phase = options.phase;
|
|
2297
|
-
if (options.before !== void 0) job.before = this.normalizeConstraints(options.before);
|
|
2298
|
-
if (options.after !== void 0) job.after = this.normalizeConstraints(options.after);
|
|
2299
|
-
root.needsRebuild = true;
|
|
2300
|
-
}
|
|
2301
|
-
}
|
|
2302
|
-
//* Job State Management Methods ================================
|
|
2303
|
-
/**
|
|
2304
|
-
* Check if a job is currently paused (disabled).
|
|
2305
|
-
* @param {string} id - The job ID to check
|
|
2306
|
-
* @returns {boolean} True if the job exists and is paused
|
|
2307
|
-
*/
|
|
2308
|
-
isJobPaused(id) {
|
|
2309
|
-
for (const root of this.roots.values()) {
|
|
2310
|
-
const job = root.jobs.get(id);
|
|
2311
|
-
if (job) return !job.enabled;
|
|
2312
|
-
}
|
|
2313
|
-
return false;
|
|
2314
|
-
}
|
|
2315
|
-
/**
|
|
2316
|
-
* Subscribe to state changes for a specific job.
|
|
2317
|
-
* Listener is called when job is paused or resumed.
|
|
2318
|
-
* @param {string} id - The job ID to subscribe to
|
|
2319
|
-
* @param {() => void} listener - Callback invoked on state changes
|
|
2320
|
-
* @returns {() => void} Unsubscribe function
|
|
2321
|
-
*/
|
|
2322
|
-
subscribeJobState(id, listener) {
|
|
2323
|
-
if (!this.jobStateListeners.has(id)) {
|
|
2324
|
-
this.jobStateListeners.set(id, /* @__PURE__ */ new Set());
|
|
2325
|
-
}
|
|
2326
|
-
this.jobStateListeners.get(id).add(listener);
|
|
2327
|
-
return () => {
|
|
2328
|
-
this.jobStateListeners.get(id)?.delete(listener);
|
|
2329
|
-
if (this.jobStateListeners.get(id)?.size === 0) {
|
|
2330
|
-
this.jobStateListeners.delete(id);
|
|
2331
|
-
}
|
|
2332
|
-
};
|
|
2333
|
-
}
|
|
2334
|
-
/**
|
|
2335
|
-
* Notify all listeners that a job's state has changed.
|
|
2336
|
-
* @param {string} id - The job ID that changed
|
|
2337
|
-
* @returns {void}
|
|
2338
|
-
* @private
|
|
2339
|
-
*/
|
|
2340
|
-
notifyJobStateChange(id) {
|
|
2341
|
-
this.jobStateListeners.get(id)?.forEach((listener) => listener());
|
|
2342
|
-
}
|
|
2343
|
-
/**
|
|
2344
|
-
* Pause a job by ID (sets enabled=false).
|
|
2345
|
-
* Notifies any subscribed state listeners.
|
|
2346
|
-
* @param {string} id - The job ID to pause
|
|
2347
|
-
* @returns {void}
|
|
2348
|
-
*/
|
|
2349
|
-
pauseJob(id) {
|
|
2350
|
-
this.updateJob(id, { enabled: false });
|
|
2351
|
-
this.notifyJobStateChange(id);
|
|
2352
|
-
}
|
|
2353
|
-
/**
|
|
2354
|
-
* Resume a paused job by ID (sets enabled=true).
|
|
2355
|
-
* Resets job timing to prevent frame accumulation.
|
|
2356
|
-
* Notifies any subscribed state listeners.
|
|
2357
|
-
* @param {string} id - The job ID to resume
|
|
2358
|
-
* @returns {void}
|
|
2359
|
-
*/
|
|
2360
|
-
resumeJob(id) {
|
|
2361
|
-
this.updateJob(id, { enabled: true });
|
|
2362
|
-
this.notifyJobStateChange(id);
|
|
2363
|
-
}
|
|
2364
|
-
//* Frame Loop Control Methods ================================
|
|
2365
|
-
/**
|
|
2366
|
-
* Start the requestAnimationFrame loop.
|
|
2367
|
-
* Resets timing state (elapsedTime, frameCount) on start.
|
|
2368
|
-
* No-op if already running.
|
|
2369
|
-
* @returns {void}
|
|
2370
|
-
*/
|
|
2371
|
-
start() {
|
|
2372
|
-
if (this.loopState.running) return;
|
|
2373
|
-
const { elapsedTime, createdAt } = this.loopState;
|
|
2374
|
-
let adjustedCreated = 0;
|
|
2375
|
-
if (this.stoppedTime > 0) {
|
|
2376
|
-
adjustedCreated = createdAt - (performance.now() - this.stoppedTime);
|
|
2377
|
-
this.stoppedTime = 0;
|
|
2378
|
-
}
|
|
2379
|
-
Object.assign(this.loopState, {
|
|
2380
|
-
running: true,
|
|
2381
|
-
elapsedTime: elapsedTime ?? 0,
|
|
2382
|
-
lastTime: performance.now(),
|
|
2383
|
-
createdAt: adjustedCreated > 0 ? adjustedCreated : performance.now(),
|
|
2384
|
-
frameCount: 0,
|
|
2385
|
-
rafHandle: requestAnimationFrame(this.loop)
|
|
2386
|
-
});
|
|
2387
|
-
}
|
|
2388
|
-
/**
|
|
2389
|
-
* Stop the requestAnimationFrame loop.
|
|
2390
|
-
* Cancels any pending RAF callback.
|
|
2391
|
-
* No-op if not running.
|
|
2392
|
-
* @returns {void}
|
|
2393
|
-
*/
|
|
2394
|
-
stop() {
|
|
2395
|
-
if (!this.loopState.running) return;
|
|
2396
|
-
this.loopState.running = false;
|
|
2397
|
-
if (this.loopState.rafHandle !== null) {
|
|
2398
|
-
cancelAnimationFrame(this.loopState.rafHandle);
|
|
2399
|
-
this.loopState.rafHandle = null;
|
|
2400
|
-
}
|
|
2401
|
-
this.stoppedTime = performance.now();
|
|
2402
|
-
}
|
|
2403
|
-
/**
|
|
2404
|
-
* Request frames to be rendered in demand mode.
|
|
2405
|
-
* Accumulates pending frames (capped at 60) and starts the loop if not running.
|
|
2406
|
-
* No-op if frameloop is not 'demand'.
|
|
2407
|
-
* @param {number} [frames=1] - Number of frames to request
|
|
2408
|
-
* @param {boolean} [stackFrames=false] - Whether to add frames to existing pending count
|
|
2409
|
-
* - `false` (default): Sets pending frames to the specified value (replaces existing count)
|
|
2410
|
-
* - `true`: Adds frames to existing pending count (useful for accumulating invalidations)
|
|
2411
|
-
* @returns {void}
|
|
2412
|
-
* @example
|
|
2413
|
-
* // Request a single frame render
|
|
2414
|
-
* scheduler.invalidate();
|
|
2415
|
-
*
|
|
2416
|
-
* @example
|
|
2417
|
-
* // Request 5 frames (e.g., for animations)
|
|
2418
|
-
* scheduler.invalidate(5);
|
|
2419
|
-
*
|
|
2420
|
-
* @example
|
|
2421
|
-
* // Set pending frames to exactly 3 (don't stack with existing)
|
|
2422
|
-
* scheduler.invalidate(3, false);
|
|
2423
|
-
*
|
|
2424
|
-
* @example
|
|
2425
|
-
* // Add 2 more frames to existing pending count
|
|
2426
|
-
* scheduler.invalidate(2, true);
|
|
2427
|
-
*/
|
|
2428
|
-
invalidate(frames = 1, stackFrames = false) {
|
|
2429
|
-
if (this._frameloop !== "demand") return;
|
|
2430
|
-
const baseFrames = stackFrames ? this.pendingFrames : 0;
|
|
2431
|
-
this.pendingFrames = Math.min(60, baseFrames + frames);
|
|
2432
|
-
if (!this.loopState.running && this.pendingFrames > 0) this.start();
|
|
2433
|
-
}
|
|
2434
|
-
/**
|
|
2435
|
-
* Reset timing state for deterministic testing.
|
|
2436
|
-
* Preserves jobs and roots but resets lastTime, frameCount, elapsedTime, etc.
|
|
2437
|
-
* @returns {void}
|
|
2438
|
-
*/
|
|
2439
|
-
resetTiming() {
|
|
2440
|
-
this.loopState.lastTime = null;
|
|
2441
|
-
this.loopState.frameCount = 0;
|
|
2442
|
-
this.loopState.elapsedTime = 0;
|
|
2443
|
-
this.loopState.createdAt = performance.now();
|
|
2444
|
-
}
|
|
2445
|
-
//* Manual Stepping Methods ================================
|
|
2446
|
-
/**
|
|
2447
|
-
* Manually execute a single frame for all roots.
|
|
2448
|
-
* Useful for frameloop='never' mode or testing scenarios.
|
|
2449
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2450
|
-
* @returns {void}
|
|
2451
|
-
* @example
|
|
2452
|
-
* // Manual control mode
|
|
2453
|
-
* scheduler.frameloop = 'never';
|
|
2454
|
-
* scheduler.step(); // Execute one frame
|
|
2455
|
-
*/
|
|
2456
|
-
step(timestamp) {
|
|
2457
|
-
const now = timestamp ?? performance.now();
|
|
2458
|
-
this.executeFrame(now);
|
|
2459
|
-
}
|
|
2460
|
-
/**
|
|
2461
|
-
* Manually execute a single job by its ID.
|
|
2462
|
-
* Useful for testing individual job callbacks in isolation.
|
|
2463
|
-
* @param {string} id - The job ID to step
|
|
2464
|
-
* @param {number} [timestamp] - Optional timestamp (defaults to performance.now())
|
|
2465
|
-
* @returns {void}
|
|
2466
|
-
*/
|
|
2467
|
-
stepJob(id, timestamp) {
|
|
2468
|
-
let job;
|
|
2469
|
-
let root;
|
|
2470
|
-
for (const r of this.roots.values()) {
|
|
2471
|
-
job = r.jobs.get(id);
|
|
2472
|
-
if (job) {
|
|
2473
|
-
root = r;
|
|
2474
|
-
break;
|
|
2475
|
-
}
|
|
2476
|
-
}
|
|
2477
|
-
if (!job || !root) {
|
|
2478
|
-
console.warn(`[Scheduler] Job "${id}" not found`);
|
|
2479
|
-
return;
|
|
2480
|
-
}
|
|
2481
|
-
const now = timestamp ?? performance.now();
|
|
2482
|
-
const deltaMs = this.loopState.lastTime !== null ? now - this.loopState.lastTime : 0;
|
|
2483
|
-
const delta = deltaMs / 1e3;
|
|
2484
|
-
const elapsed = now - this.loopState.createdAt;
|
|
2485
|
-
const providedState = root.getState?.() ?? {};
|
|
2486
|
-
const frameState = {
|
|
2487
|
-
...providedState,
|
|
2488
|
-
time: now,
|
|
2489
|
-
delta,
|
|
2490
|
-
elapsed,
|
|
2491
|
-
frame: this.loopState.frameCount
|
|
2492
|
-
};
|
|
2493
|
-
try {
|
|
2494
|
-
job.callback(frameState, delta);
|
|
2495
|
-
} catch (error) {
|
|
2496
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2497
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2498
|
-
}
|
|
2499
|
-
}
|
|
2500
|
-
/**
|
|
2501
|
-
* Execute a single frame across all roots.
|
|
2502
|
-
* Order: globalBefore → each root's jobs → globalAfter
|
|
2503
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2504
|
-
* @returns {void}
|
|
2505
|
-
* @private
|
|
2506
|
-
*/
|
|
2507
|
-
executeFrame(timestamp) {
|
|
2508
|
-
const deltaMs = this.loopState.lastTime !== null ? timestamp - this.loopState.lastTime : 0;
|
|
2509
|
-
const delta = deltaMs / 1e3;
|
|
2510
|
-
this.loopState.lastTime = timestamp;
|
|
2511
|
-
this.loopState.frameCount++;
|
|
2512
|
-
this.loopState.elapsedTime += deltaMs;
|
|
2513
|
-
this.runGlobalJobs(this.globalBeforeJobs, timestamp);
|
|
2514
|
-
for (const root of this.roots.values()) {
|
|
2515
|
-
this.tickRoot(root, timestamp, delta);
|
|
2516
|
-
}
|
|
2517
|
-
this.runGlobalJobs(this.globalAfterJobs, timestamp);
|
|
2518
|
-
}
|
|
2519
|
-
/**
|
|
2520
|
-
* Run all global jobs from a job map.
|
|
2521
|
-
* Catches and logs errors without stopping execution.
|
|
2522
|
-
* @param {Map<string, GlobalJob>} jobs - The global jobs map to execute
|
|
2523
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2524
|
-
* @returns {void}
|
|
2525
|
-
* @private
|
|
2526
|
-
*/
|
|
2527
|
-
runGlobalJobs(jobs, timestamp) {
|
|
2528
|
-
for (const job of jobs.values()) {
|
|
2529
|
-
try {
|
|
2530
|
-
job.callback(timestamp);
|
|
2531
|
-
} catch (error) {
|
|
2532
|
-
console.error(`[Scheduler] Error in global job "${job.id}":`, error);
|
|
2533
|
-
}
|
|
2534
|
-
}
|
|
2535
|
-
}
|
|
2536
|
-
/**
|
|
2537
|
-
* Execute all jobs for a single root in sorted order.
|
|
2538
|
-
* Rebuilds sorted job list if needed, then dispatches each job.
|
|
2539
|
-
* Errors are caught and propagated via triggerError.
|
|
2540
|
-
* @param {RootEntry} root - The root entry to tick
|
|
2541
|
-
* @param {number} timestamp - RAF timestamp in milliseconds
|
|
2542
|
-
* @param {number} delta - Time since last frame in seconds
|
|
2543
|
-
* @returns {void}
|
|
2544
|
-
* @private
|
|
2545
|
-
*/
|
|
2546
|
-
tickRoot(root, timestamp, delta) {
|
|
2547
|
-
if (root.needsRebuild) {
|
|
2548
|
-
root.sortedJobs = rebuildSortedJobs(root.jobs, this.phaseGraph);
|
|
2549
|
-
root.needsRebuild = false;
|
|
2550
|
-
}
|
|
2551
|
-
const providedState = root.getState?.() ?? {};
|
|
2552
|
-
const frameState = {
|
|
2553
|
-
...providedState,
|
|
2554
|
-
time: timestamp,
|
|
2555
|
-
delta,
|
|
2556
|
-
elapsed: this.loopState.elapsedTime / 1e3,
|
|
2557
|
-
// Convert ms to seconds
|
|
2558
|
-
frame: this.loopState.frameCount
|
|
2559
|
-
};
|
|
2560
|
-
for (const job of root.sortedJobs) {
|
|
2561
|
-
if (!shouldRun(job, timestamp)) continue;
|
|
2562
|
-
try {
|
|
2563
|
-
job.callback(frameState, delta);
|
|
2564
|
-
} catch (error) {
|
|
2565
|
-
console.error(`[Scheduler] Error in job "${job.id}":`, error);
|
|
2566
|
-
this.triggerError(error instanceof Error ? error : new Error(String(error)));
|
|
2567
|
-
}
|
|
2568
|
-
}
|
|
2569
|
-
}
|
|
2570
|
-
//* Debug & Inspection Methods ================================
|
|
2571
|
-
/**
|
|
2572
|
-
* Get the total number of registered jobs across all roots.
|
|
2573
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2574
|
-
* @returns {number} Total job count
|
|
2575
|
-
*/
|
|
2576
|
-
getJobCount() {
|
|
2577
|
-
let count = 0;
|
|
2578
|
-
for (const root of this.roots.values()) {
|
|
2579
|
-
count += root.jobs.size;
|
|
2580
|
-
}
|
|
2581
|
-
return count + this.globalBeforeJobs.size + this.globalAfterJobs.size;
|
|
2582
|
-
}
|
|
2583
|
-
/**
|
|
2584
|
-
* Get all registered job IDs across all roots.
|
|
2585
|
-
* Includes both per-root jobs and global before/after jobs.
|
|
2586
|
-
* @returns {string[]} Array of all job IDs
|
|
2587
|
-
*/
|
|
2588
|
-
getJobIds() {
|
|
2589
|
-
const ids = [];
|
|
2590
|
-
for (const root of this.roots.values()) {
|
|
2591
|
-
ids.push(...root.jobs.keys());
|
|
2592
|
-
}
|
|
2593
|
-
ids.push(...this.globalBeforeJobs.keys());
|
|
2594
|
-
ids.push(...this.globalAfterJobs.keys());
|
|
2595
|
-
return ids;
|
|
2596
|
-
}
|
|
2597
|
-
/**
|
|
2598
|
-
* Get the number of registered roots (Canvas instances).
|
|
2599
|
-
* @returns {number} Number of registered roots
|
|
2600
|
-
*/
|
|
2601
|
-
getRootCount() {
|
|
2602
|
-
return this.roots.size;
|
|
2603
|
-
}
|
|
2604
|
-
/**
|
|
2605
|
-
* Check if any user (non-system) jobs are registered in a specific phase.
|
|
2606
|
-
* Used by the default render job to know if a user has taken over rendering.
|
|
2607
|
-
*
|
|
2608
|
-
* @param phase The phase to check
|
|
2609
|
-
* @param rootId Optional root ID to check (checks all roots if not provided)
|
|
2610
|
-
* @returns true if any user jobs exist in the phase
|
|
2611
|
-
*/
|
|
2612
|
-
hasUserJobsInPhase(phase, rootId) {
|
|
2613
|
-
const rootsToCheck = rootId ? [this.roots.get(rootId)].filter(Boolean) : Array.from(this.roots.values());
|
|
2614
|
-
return rootsToCheck.some((root) => {
|
|
2615
|
-
if (!root) return false;
|
|
2616
|
-
for (const job of root.jobs.values()) {
|
|
2617
|
-
if (job.phase === phase && !job.system && job.enabled) return true;
|
|
2618
|
-
}
|
|
2619
|
-
return false;
|
|
2620
|
-
});
|
|
2621
|
-
}
|
|
2622
|
-
//* Utility Methods ================================
|
|
2623
|
-
/**
|
|
2624
|
-
* Generate a unique root ID for automatic root registration.
|
|
2625
|
-
* @returns {string} A unique root ID in the format 'root_N'
|
|
2626
|
-
*/
|
|
2627
|
-
generateRootId() {
|
|
2628
|
-
return `root_${this.nextRootIndex++}`;
|
|
2629
|
-
}
|
|
2630
|
-
/**
|
|
2631
|
-
* Generate a unique job ID.
|
|
2632
|
-
* @returns {string} A unique job ID in the format 'job_N'
|
|
2633
|
-
* @private
|
|
2634
|
-
*/
|
|
2635
|
-
generateJobId() {
|
|
2636
|
-
return `job_${this.nextJobIndex}`;
|
|
2637
|
-
}
|
|
2638
|
-
/**
|
|
2639
|
-
* Normalize before/after constraints to a Set.
|
|
2640
|
-
* Handles undefined, single string, or array inputs.
|
|
2641
|
-
* @param {string | string[] | undefined} value - The constraint value(s)
|
|
2642
|
-
* @returns {Set<string>} Normalized Set of constraint strings
|
|
2643
|
-
* @private
|
|
2644
|
-
*/
|
|
2645
|
-
normalizeConstraints(value) {
|
|
2646
|
-
if (!value) return /* @__PURE__ */ new Set();
|
|
2647
|
-
if (Array.isArray(value)) return new Set(value);
|
|
2648
|
-
return /* @__PURE__ */ new Set([value]);
|
|
2649
|
-
}
|
|
2650
|
-
};
|
|
2651
|
-
//* Static State & Methods (Singleton Usage) ================================
|
|
2652
|
-
//* Cross-Bundle Singleton Key ==============================
|
|
2653
|
-
// Use Symbol.for() to ensure scheduler is shared across bundle boundaries
|
|
2654
|
-
// This prevents issues when mixing imports from @react-three/fiber and @react-three/fiber/webgpu
|
|
2655
|
-
__publicField(_Scheduler, "INSTANCE_KEY", Symbol.for("@react-three/fiber.scheduler"));
|
|
2656
|
-
let Scheduler = _Scheduler;
|
|
2657
|
-
const getScheduler = () => Scheduler.get();
|
|
2658
|
-
if (hmrData) {
|
|
2659
|
-
hmrData.accept?.();
|
|
1645
|
+
const INTERNAL_LIB_MARKERS = ["zustand", "setState", "Object.assign"];
|
|
1646
|
+
const INTERNAL_FIBER_MARKERS = ["packages/fiber/src", "packages/fiber/dist", "@react-three/fiber", "@react-three_fiber"];
|
|
1647
|
+
function isInternalRendererAccess(stack, callerDepth = 2) {
|
|
1648
|
+
if (!stack) return false;
|
|
1649
|
+
const caller = stack.split("\n")[callerDepth] ?? "";
|
|
1650
|
+
return INTERNAL_LIB_MARKERS.some((m) => caller.includes(m)) || INTERNAL_FIBER_MARKERS.some((m) => caller.includes(m));
|
|
2660
1651
|
}
|
|
2661
1652
|
|
|
2662
1653
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
@@ -2858,8 +1849,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2858
1849
|
const currentState = rootStore.getState();
|
|
2859
1850
|
if (!currentState.isLegacy && currentState.internal.actualRenderer) {
|
|
2860
1851
|
const stack = new Error().stack || "";
|
|
2861
|
-
|
|
2862
|
-
if (!isInternalAccess) {
|
|
1852
|
+
if (!isInternalRendererAccess(stack)) {
|
|
2863
1853
|
const cleanedStack = stack.split("\n").slice(2).join("\n") || "Stack trace unavailable";
|
|
2864
1854
|
notifyDepreciated({
|
|
2865
1855
|
heading: "Accessing state.gl in WebGPU mode",
|
|
@@ -2972,19 +1962,25 @@ function loadingFn(extensions, onProgress) {
|
|
|
2972
1962
|
);
|
|
2973
1963
|
};
|
|
2974
1964
|
}
|
|
2975
|
-
function useLoader(loader, input, extensions, onProgress) {
|
|
2976
|
-
const
|
|
1965
|
+
function useLoader(loader, input, extensions, onProgress, cacheKey) {
|
|
1966
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1967
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
2977
1968
|
const fn = loadingFn(extensions, onProgress);
|
|
2978
|
-
const results =
|
|
1969
|
+
const results = cacheKeys.map(
|
|
1970
|
+
(key, index) => suspend(() => fn(loader, inputs[index]), [loader, key], { equal: is.equ })
|
|
1971
|
+
);
|
|
2979
1972
|
return Array.isArray(input) ? results : results[0];
|
|
2980
1973
|
}
|
|
2981
|
-
useLoader.preload = function(loader, input, extensions, onProgress) {
|
|
2982
|
-
const
|
|
2983
|
-
|
|
1974
|
+
useLoader.preload = function(loader, input, extensions, onProgress, cacheKey) {
|
|
1975
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
1976
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1977
|
+
cacheKeys.forEach(
|
|
1978
|
+
(key, index) => preload(() => loadingFn(extensions, onProgress)(loader, inputs[index]), [loader, key])
|
|
1979
|
+
);
|
|
2984
1980
|
};
|
|
2985
|
-
useLoader.clear = function(loader, input) {
|
|
2986
|
-
const
|
|
2987
|
-
|
|
1981
|
+
useLoader.clear = function(loader, input, cacheKey) {
|
|
1982
|
+
const cacheKeys = Array.isArray(cacheKey ?? input) ? cacheKey ?? input : [cacheKey ?? input];
|
|
1983
|
+
cacheKeys.forEach((key) => clear([loader, key]));
|
|
2988
1984
|
};
|
|
2989
1985
|
useLoader.loader = getLoader;
|
|
2990
1986
|
|
|
@@ -3060,20 +2056,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
3060
2056
|
}
|
|
3061
2057
|
};
|
|
3062
2058
|
} else {
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
unregisterJob = registerOutside();
|
|
3072
|
-
});
|
|
3073
|
-
return () => {
|
|
3074
|
-
unsubReady();
|
|
3075
|
-
unregisterJob?.();
|
|
3076
|
-
};
|
|
2059
|
+
return scheduler.register(
|
|
2060
|
+
(state, delta) => {
|
|
2061
|
+
const frameState = state;
|
|
2062
|
+
if (!frameState.renderer) return;
|
|
2063
|
+
callbackRef.current?.(frameState, delta);
|
|
2064
|
+
},
|
|
2065
|
+
{ id, ...options }
|
|
2066
|
+
);
|
|
3077
2067
|
}
|
|
3078
2068
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
3079
2069
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -14851,6 +13841,7 @@ function swapInstances() {
|
|
|
14851
13841
|
}
|
|
14852
13842
|
}
|
|
14853
13843
|
if (instance.isHidden) unhideInstance(instance);
|
|
13844
|
+
if (isObject3D(instance.object)) removeInteractivity(findInitialRoot(instance), instance.object);
|
|
14854
13845
|
if (instance.object.__r3f) delete instance.object.__r3f;
|
|
14855
13846
|
if (instance.type !== "primitive") disposeOnIdle(instance.object);
|
|
14856
13847
|
}
|
|
@@ -14863,6 +13854,8 @@ function swapInstances() {
|
|
|
14863
13854
|
instance.object.__r3f = instance;
|
|
14864
13855
|
setFiberRef(fiber, instance.object);
|
|
14865
13856
|
delete instance.appliedOnce;
|
|
13857
|
+
instance.eventCount = 0;
|
|
13858
|
+
instance.handlers = {};
|
|
14866
13859
|
applyProps(instance.object, instance.props);
|
|
14867
13860
|
if (instance.props.attach) {
|
|
14868
13861
|
attach(parent, instance);
|
|
@@ -15167,6 +14160,7 @@ function createRoot(canvas) {
|
|
|
15167
14160
|
const lastConfiguredProps = {};
|
|
15168
14161
|
let configured = false;
|
|
15169
14162
|
let pending = null;
|
|
14163
|
+
let rendererSetup = null;
|
|
15170
14164
|
return {
|
|
15171
14165
|
async configure(props = {}) {
|
|
15172
14166
|
let resolve;
|
|
@@ -15197,7 +14191,7 @@ function createRoot(canvas) {
|
|
|
15197
14191
|
forceEven
|
|
15198
14192
|
} = props;
|
|
15199
14193
|
const textureColorSpace = is.obj(glConfig) && !is.fun(glConfig) && !isRenderer(glConfig) && glConfig.textureColorSpace || is.obj(rendererConfig) && !is.fun(rendererConfig) && !isRenderer(rendererConfig) && rendererConfig.textureColorSpace || SRGBColorSpace;
|
|
15200
|
-
|
|
14194
|
+
let state = store.getState();
|
|
15201
14195
|
const defaultGLProps = {
|
|
15202
14196
|
canvas,
|
|
15203
14197
|
powerPreference: "high-performance",
|
|
@@ -15225,56 +14219,68 @@ function createRoot(canvas) {
|
|
|
15225
14219
|
"The `primaryCanvas` prop for multi-canvas rendering cannot be used with WebGL. Remove the `gl` prop or use WebGPU."
|
|
15226
14220
|
);
|
|
15227
14221
|
}
|
|
15228
|
-
if (
|
|
15229
|
-
|
|
15230
|
-
|
|
15231
|
-
|
|
15232
|
-
|
|
15233
|
-
|
|
15234
|
-
|
|
15235
|
-
|
|
15236
|
-
|
|
15237
|
-
|
|
15238
|
-
|
|
15239
|
-
|
|
15240
|
-
|
|
15241
|
-
|
|
15242
|
-
|
|
15243
|
-
|
|
15244
|
-
|
|
15245
|
-
|
|
15246
|
-
|
|
15247
|
-
|
|
15248
|
-
|
|
15249
|
-
|
|
15250
|
-
|
|
15251
|
-
|
|
15252
|
-
|
|
15253
|
-
|
|
15254
|
-
|
|
15255
|
-
|
|
15256
|
-
|
|
15257
|
-
|
|
15258
|
-
|
|
15259
|
-
|
|
15260
|
-
|
|
15261
|
-
|
|
15262
|
-
|
|
15263
|
-
|
|
15264
|
-
|
|
15265
|
-
|
|
15266
|
-
|
|
15267
|
-
|
|
15268
|
-
|
|
15269
|
-
|
|
15270
|
-
|
|
15271
|
-
|
|
15272
|
-
|
|
15273
|
-
|
|
15274
|
-
|
|
14222
|
+
if (!state.internal.actualRenderer) {
|
|
14223
|
+
if (!rendererSetup) {
|
|
14224
|
+
rendererSetup = (async () => {
|
|
14225
|
+
if (wantsGL) {
|
|
14226
|
+
renderer = await resolveRenderer(glConfig, defaultGLProps, WebGLRenderer);
|
|
14227
|
+
state.internal.actualRenderer = renderer;
|
|
14228
|
+
state.set({ isLegacy: true, gl: renderer, renderer, primaryStore: store });
|
|
14229
|
+
} else if (!wantsGL && primaryCanvas) {
|
|
14230
|
+
const primary = await waitForPrimary(primaryCanvas);
|
|
14231
|
+
renderer = primary.renderer;
|
|
14232
|
+
state.internal.actualRenderer = renderer;
|
|
14233
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14234
|
+
primary.store.setState((prev) => ({
|
|
14235
|
+
internal: { ...prev.internal, isMultiCanvas: true }
|
|
14236
|
+
}));
|
|
14237
|
+
state.set((prev) => ({
|
|
14238
|
+
webGPUSupported: primary.store.getState().webGPUSupported,
|
|
14239
|
+
renderer,
|
|
14240
|
+
primaryStore: primary.store,
|
|
14241
|
+
internal: {
|
|
14242
|
+
...prev.internal,
|
|
14243
|
+
canvasTarget,
|
|
14244
|
+
isMultiCanvas: true,
|
|
14245
|
+
isSecondary: true,
|
|
14246
|
+
targetId: primaryCanvas
|
|
14247
|
+
}
|
|
14248
|
+
}));
|
|
14249
|
+
} else if (!wantsGL) {
|
|
14250
|
+
renderer = await resolveRenderer(rendererConfig, defaultGPUProps, WebGPURenderer);
|
|
14251
|
+
if (!renderer.hasInitialized?.()) {
|
|
14252
|
+
const size2 = computeInitialSize(canvas, propsSize);
|
|
14253
|
+
if (size2.width > 0 && size2.height > 0) {
|
|
14254
|
+
const pixelRatio = calculateDpr(dpr);
|
|
14255
|
+
canvas.width = size2.width * pixelRatio;
|
|
14256
|
+
canvas.height = size2.height * pixelRatio;
|
|
14257
|
+
}
|
|
14258
|
+
await renderer.init();
|
|
14259
|
+
}
|
|
14260
|
+
const backend = renderer.backend;
|
|
14261
|
+
const isWebGPUBackend = backend && "isWebGPUBackend" in backend;
|
|
14262
|
+
state.internal.actualRenderer = renderer;
|
|
14263
|
+
state.set({ webGPUSupported: isWebGPUBackend, renderer, primaryStore: store });
|
|
14264
|
+
if (canvasId && !state.internal.isSecondary) {
|
|
14265
|
+
const canvasTarget = new CanvasTarget(canvas);
|
|
14266
|
+
const unregisterPrimary = registerPrimary(canvasId, renderer, store);
|
|
14267
|
+
state.set((prev) => ({
|
|
14268
|
+
internal: {
|
|
14269
|
+
...prev.internal,
|
|
14270
|
+
canvasTarget,
|
|
14271
|
+
unregisterPrimary
|
|
14272
|
+
}
|
|
14273
|
+
}));
|
|
14274
|
+
}
|
|
15275
14275
|
}
|
|
15276
|
-
}))
|
|
14276
|
+
})().catch((err) => {
|
|
14277
|
+
rendererSetup = null;
|
|
14278
|
+
throw err;
|
|
14279
|
+
});
|
|
15277
14280
|
}
|
|
14281
|
+
await rendererSetup;
|
|
14282
|
+
state = store.getState();
|
|
14283
|
+
renderer = state.internal.actualRenderer;
|
|
15278
14284
|
}
|
|
15279
14285
|
let raycaster = state.raycaster;
|
|
15280
14286
|
if (!raycaster) state.set({ raycaster: raycaster = new Raycaster() });
|
|
@@ -15499,7 +14505,7 @@ function createRoot(canvas) {
|
|
|
15499
14505
|
{
|
|
15500
14506
|
id: `${newRootId}_frustum`,
|
|
15501
14507
|
rootId: newRootId,
|
|
15502
|
-
|
|
14508
|
+
before: "render",
|
|
15503
14509
|
system: true
|
|
15504
14510
|
}
|
|
15505
14511
|
);
|
|
@@ -15511,7 +14517,7 @@ function createRoot(canvas) {
|
|
|
15511
14517
|
{
|
|
15512
14518
|
id: `${newRootId}_visibility`,
|
|
15513
14519
|
rootId: newRootId,
|
|
15514
|
-
|
|
14520
|
+
before: "render",
|
|
15515
14521
|
system: true,
|
|
15516
14522
|
after: `${newRootId}_frustum`
|
|
15517
14523
|
}
|
|
@@ -15687,6 +14693,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15687
14693
|
viewport = rootState.viewport.getCurrentViewport(camera, new Vector3(), resolvedSize);
|
|
15688
14694
|
if (camera !== rootState.camera) updateCamera(camera, resolvedSize);
|
|
15689
14695
|
}
|
|
14696
|
+
const set = injectState.set;
|
|
15690
14697
|
return {
|
|
15691
14698
|
// The intersect consists of the previous root state
|
|
15692
14699
|
...rootState,
|
|
@@ -15705,7 +14712,7 @@ function PortalInner({ state = {}, children, container }) {
|
|
|
15705
14712
|
size: resolvedSize,
|
|
15706
14713
|
viewport: { ...rootState.viewport, ...viewport },
|
|
15707
14714
|
// Layers are allowed to override events
|
|
15708
|
-
setEvents: (events2) =>
|
|
14715
|
+
setEvents: (events2) => set((state2) => ({ ...state2, events: { ...state2.events, ...events2 } })),
|
|
15709
14716
|
// Container for child attachment - the portalScene (injected or container itself)
|
|
15710
14717
|
internal: { ...rootState.internal, ...injectState.internal, container: portalScene }
|
|
15711
14718
|
};
|
|
@@ -15734,6 +14741,59 @@ function flushSync(fn) {
|
|
|
15734
14741
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15735
14742
|
}
|
|
15736
14743
|
|
|
14744
|
+
function parseBackground(background) {
|
|
14745
|
+
if (!background) return null;
|
|
14746
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14747
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14748
|
+
return {
|
|
14749
|
+
...rest,
|
|
14750
|
+
preset,
|
|
14751
|
+
files: envMap || files,
|
|
14752
|
+
backgroundFiles: backgroundMap,
|
|
14753
|
+
background: true
|
|
14754
|
+
};
|
|
14755
|
+
}
|
|
14756
|
+
if (typeof background === "number") {
|
|
14757
|
+
return { color: background, background: true };
|
|
14758
|
+
}
|
|
14759
|
+
if (typeof background === "string") {
|
|
14760
|
+
if (background in presetsObj) {
|
|
14761
|
+
return { preset: background, background: true };
|
|
14762
|
+
}
|
|
14763
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14764
|
+
return { files: background, background: true };
|
|
14765
|
+
}
|
|
14766
|
+
return { color: background, background: true };
|
|
14767
|
+
}
|
|
14768
|
+
if (background.isColor) {
|
|
14769
|
+
return { color: background, background: true };
|
|
14770
|
+
}
|
|
14771
|
+
return null;
|
|
14772
|
+
}
|
|
14773
|
+
|
|
14774
|
+
function parseRendererConfig(rendererProp) {
|
|
14775
|
+
const isRendererConfig = typeof rendererProp === "object" && rendererProp !== null && !("render" in rendererProp) && ("primaryCanvas" in rendererProp || "scheduler" in rendererProp);
|
|
14776
|
+
if (isRendererConfig) {
|
|
14777
|
+
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
14778
|
+
return {
|
|
14779
|
+
primaryCanvas: pc,
|
|
14780
|
+
scheduler: sc,
|
|
14781
|
+
renderer: Object.keys(rest).length > 0 ? rest : rendererProp
|
|
14782
|
+
};
|
|
14783
|
+
}
|
|
14784
|
+
return { primaryCanvas: void 0, scheduler: void 0, renderer: rendererProp };
|
|
14785
|
+
}
|
|
14786
|
+
|
|
14787
|
+
function clearHmrCaches(store) {
|
|
14788
|
+
store.setState((state) => ({
|
|
14789
|
+
nodes: {},
|
|
14790
|
+
uniforms: {},
|
|
14791
|
+
buffers: {},
|
|
14792
|
+
gpuStorage: {},
|
|
14793
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14794
|
+
}));
|
|
14795
|
+
}
|
|
14796
|
+
|
|
15737
14797
|
function CanvasImpl({
|
|
15738
14798
|
ref,
|
|
15739
14799
|
children,
|
|
@@ -15754,6 +14814,8 @@ function CanvasImpl({
|
|
|
15754
14814
|
raycaster,
|
|
15755
14815
|
camera,
|
|
15756
14816
|
scene,
|
|
14817
|
+
autoUpdateFrustum,
|
|
14818
|
+
occlusion,
|
|
15757
14819
|
onPointerMissed,
|
|
15758
14820
|
onDragOverMissed,
|
|
15759
14821
|
onDropMissed,
|
|
@@ -15765,49 +14827,10 @@ function CanvasImpl({
|
|
|
15765
14827
|
forceEven,
|
|
15766
14828
|
...props
|
|
15767
14829
|
}) {
|
|
15768
|
-
const
|
|
15769
|
-
let primaryCanvas;
|
|
15770
|
-
let scheduler;
|
|
15771
|
-
let renderer;
|
|
15772
|
-
if (isRendererConfig) {
|
|
15773
|
-
const { primaryCanvas: pc, scheduler: sc, ...rest } = rendererProp;
|
|
15774
|
-
primaryCanvas = pc;
|
|
15775
|
-
scheduler = sc;
|
|
15776
|
-
renderer = Object.keys(rest).length > 0 ? rest : rendererProp;
|
|
15777
|
-
} else {
|
|
15778
|
-
renderer = rendererProp;
|
|
15779
|
-
}
|
|
14830
|
+
const { primaryCanvas, scheduler, renderer } = parseRendererConfig(rendererProp);
|
|
15780
14831
|
React.useMemo(() => extend(THREE), []);
|
|
15781
14832
|
const Bridge = useBridge();
|
|
15782
|
-
const backgroundProps = React.useMemo(() =>
|
|
15783
|
-
if (!background) return null;
|
|
15784
|
-
if (typeof background === "object" && !background.isColor) {
|
|
15785
|
-
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
15786
|
-
return {
|
|
15787
|
-
...rest,
|
|
15788
|
-
preset,
|
|
15789
|
-
files: envMap || files,
|
|
15790
|
-
backgroundFiles: backgroundMap,
|
|
15791
|
-
background: true
|
|
15792
|
-
};
|
|
15793
|
-
}
|
|
15794
|
-
if (typeof background === "number") {
|
|
15795
|
-
return { color: background, background: true };
|
|
15796
|
-
}
|
|
15797
|
-
if (typeof background === "string") {
|
|
15798
|
-
if (background in presetsObj) {
|
|
15799
|
-
return { preset: background, background: true };
|
|
15800
|
-
}
|
|
15801
|
-
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
15802
|
-
return { files: background, background: true };
|
|
15803
|
-
}
|
|
15804
|
-
return { color: background, background: true };
|
|
15805
|
-
}
|
|
15806
|
-
if (background.isColor) {
|
|
15807
|
-
return { color: background, background: true };
|
|
15808
|
-
}
|
|
15809
|
-
return null;
|
|
15810
|
-
}, [background]);
|
|
14833
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15811
14834
|
const hasInitialSizeRef = React.useRef(false);
|
|
15812
14835
|
const measureConfig = React.useMemo(() => {
|
|
15813
14836
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15849,6 +14872,7 @@ function CanvasImpl({
|
|
|
15849
14872
|
const handleDropMissed = useMutableCallback(onDropMissed);
|
|
15850
14873
|
const [block, setBlock] = React.useState(false);
|
|
15851
14874
|
const [error, setError] = React.useState(false);
|
|
14875
|
+
const [fallbackVisible, setFallbackVisible] = React.useState(false);
|
|
15852
14876
|
if (block) throw block;
|
|
15853
14877
|
if (error) throw error;
|
|
15854
14878
|
const root = React.useRef(null);
|
|
@@ -15856,6 +14880,7 @@ function CanvasImpl({
|
|
|
15856
14880
|
const unsubscribeErrorRef = React.useRef(null);
|
|
15857
14881
|
useIsomorphicLayoutEffect(() => {
|
|
15858
14882
|
effectActiveRef.current = true;
|
|
14883
|
+
if (fallbackVisible) return;
|
|
15859
14884
|
const canvas = canvasRef.current;
|
|
15860
14885
|
if (effectiveSize.width > 0 && effectiveSize.height > 0 && canvas) {
|
|
15861
14886
|
if (!root.current) {
|
|
@@ -15876,7 +14901,7 @@ function CanvasImpl({
|
|
|
15876
14901
|
}
|
|
15877
14902
|
async function run() {
|
|
15878
14903
|
if (!effectActiveRef.current || !root.current) return;
|
|
15879
|
-
await root.current.configure({
|
|
14904
|
+
const configured = await root.current.configure({
|
|
15880
14905
|
id,
|
|
15881
14906
|
primaryCanvas,
|
|
15882
14907
|
scheduler,
|
|
@@ -15891,6 +14916,8 @@ function CanvasImpl({
|
|
|
15891
14916
|
performance,
|
|
15892
14917
|
raycaster,
|
|
15893
14918
|
camera,
|
|
14919
|
+
autoUpdateFrustum,
|
|
14920
|
+
occlusion,
|
|
15894
14921
|
size: effectiveSize,
|
|
15895
14922
|
// Store size props for reset functionality
|
|
15896
14923
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15915,8 +14942,20 @@ function CanvasImpl({
|
|
|
15915
14942
|
}
|
|
15916
14943
|
onCreated?.(state);
|
|
15917
14944
|
}
|
|
15918
|
-
})
|
|
15919
|
-
|
|
14945
|
+
}).then(
|
|
14946
|
+
() => true,
|
|
14947
|
+
// Renderer setup failed (e.g. no WebGL/WebGPU support). The `fallback` prop lives
|
|
14948
|
+
// inside <canvas>, which browsers don't display, so surface it as visible DOM
|
|
14949
|
+
// instead; with no fallback, rethrow to an external error boundary. (#3757)
|
|
14950
|
+
(setupError) => {
|
|
14951
|
+
if (effectActiveRef.current) {
|
|
14952
|
+
if (fallback != null) setFallbackVisible(true);
|
|
14953
|
+
else setError(setupError);
|
|
14954
|
+
}
|
|
14955
|
+
return false;
|
|
14956
|
+
}
|
|
14957
|
+
);
|
|
14958
|
+
if (!configured || !effectActiveRef.current || !root.current) return;
|
|
15920
14959
|
root.current.render(
|
|
15921
14960
|
/* @__PURE__ */ jsx(Bridge, { children: /* @__PURE__ */ jsx(ErrorBoundary, { set: setError, children: /* @__PURE__ */ jsxs(React.Suspense, { fallback: /* @__PURE__ */ jsx(Block, { set: setBlock }), children: [
|
|
15922
14961
|
backgroundProps && /* @__PURE__ */ jsx(Environment, { ...backgroundProps }),
|
|
@@ -15950,14 +14989,7 @@ function CanvasImpl({
|
|
|
15950
14989
|
const handleHMR = () => {
|
|
15951
14990
|
queueMicrotask(() => {
|
|
15952
14991
|
const rootEntry = _roots.get(canvas);
|
|
15953
|
-
if (rootEntry?.store)
|
|
15954
|
-
console.log("[R3F] HMR detected \u2014 rebuilding nodes/uniforms");
|
|
15955
|
-
rootEntry.store.setState((state) => ({
|
|
15956
|
-
nodes: {},
|
|
15957
|
-
uniforms: {},
|
|
15958
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15959
|
-
}));
|
|
15960
|
-
}
|
|
14992
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15961
14993
|
});
|
|
15962
14994
|
};
|
|
15963
14995
|
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
@@ -15986,7 +15018,11 @@ function CanvasImpl({
|
|
|
15986
15018
|
...style
|
|
15987
15019
|
},
|
|
15988
15020
|
...props,
|
|
15989
|
-
children:
|
|
15021
|
+
children: fallbackVisible ? (
|
|
15022
|
+
// Renderer setup failed: render the fallback as visible DOM. Inside <canvas> (below)
|
|
15023
|
+
// it exists in the tree but browsers never paint it, which is the whole bug (#3757).
|
|
15024
|
+
fallback
|
|
15025
|
+
) : /* @__PURE__ */ jsx("div", { ref: containerRef, className: "r3f-canvas-container", style: { width: "100%", height: "100%" }, children: /* @__PURE__ */ jsx(
|
|
15990
15026
|
"canvas",
|
|
15991
15027
|
{
|
|
15992
15028
|
ref: canvasRef,
|
|
@@ -16005,4 +15041,4 @@ function Canvas(props) {
|
|
|
16005
15041
|
|
|
16006
15042
|
extend(THREE);
|
|
16007
15043
|
|
|
16008
|
-
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
15044
|
+
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS, Texture, _roots, act, addAfterEffect, addEffect, addTail, advance, applyProps, attach, buildGraph, calculateDpr, context, createEvents, createPointerEvents, createPortal, createRoot, createStore, detach, diffProps, dispose, createPointerEvents as events, extend, findInitialRoot, flushSync, fromRef, getInstanceProps, getPrimary, getPrimaryIds, getRootState, getUuidPrefix, hasConstructor, hasPrimary, invalidate, invalidateInstance, is, isColorRepresentation, isCopyable, isFromRef, isObject3D, isOnce, isOrthographicCamera, isRef, isRenderer, isTexture, isVectorLike, once, prepare, presetsObj, reconciler, registerPrimary, removeInteractivity, resolve, unmountComponentAtNode, unregisterPrimary, updateCamera, updateFrustum, useBridge, useEnvironment, useFrame, useGraph, useInstanceHandle, useIsomorphicLayoutEffect, useLoader, useMutableCallback, useRenderTarget, useStore, useTexture, useTextures, useThree, waitForPrimary };
|