@react-three/fiber 10.0.0-canary.1b98c17 → 10.0.0-canary.2c50459
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 +169 -1217
- package/dist/index.d.cts +102 -770
- package/dist/index.d.mts +102 -770
- package/dist/index.d.ts +102 -770
- package/dist/index.mjs +141 -1188
- package/dist/legacy.cjs +169 -1217
- package/dist/legacy.d.cts +102 -770
- package/dist/legacy.d.mts +102 -770
- package/dist/legacy.d.ts +102 -770
- package/dist/legacy.mjs +141 -1188
- package/dist/webgpu/index.cjs +204 -1234
- package/dist/webgpu/index.d.cts +102 -770
- package/dist/webgpu/index.d.mts +102 -770
- package/dist/webgpu/index.d.ts +102 -770
- package/dist/webgpu/index.mjs +176 -1205
- package/package.json +2 -1
- package/readme.md +2 -2
package/dist/index.mjs
CHANGED
|
@@ -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 = {
|
|
@@ -1627,1038 +1629,6 @@ function notifyAlpha({ message, link }) {
|
|
|
1627
1629
|
}
|
|
1628
1630
|
}
|
|
1629
1631
|
|
|
1630
|
-
var __defProp$1 = Object.defineProperty;
|
|
1631
|
-
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
1632
|
-
var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1633
|
-
const DEFAULT_PHASES = ["start", "input", "physics", "update", "render", "finish"];
|
|
1634
|
-
class PhaseGraph {
|
|
1635
|
-
constructor() {
|
|
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?.();
|
|
2660
|
-
}
|
|
2661
|
-
|
|
2662
1632
|
const R3F_CONTEXT = Symbol.for("@react-three/fiber.context");
|
|
2663
1633
|
const context = globalThis[R3F_CONTEXT] ?? (globalThis[R3F_CONTEXT] = React.createContext(null));
|
|
2664
1634
|
const createStore = (invalidate, advance) => {
|
|
@@ -2800,6 +1770,7 @@ const createStore = (invalidate, advance) => {
|
|
|
2800
1770
|
buffers: {},
|
|
2801
1771
|
gpuStorage: {},
|
|
2802
1772
|
textures: /* @__PURE__ */ new Map(),
|
|
1773
|
+
_textureRefs: /* @__PURE__ */ new Map(),
|
|
2803
1774
|
renderPipeline: null,
|
|
2804
1775
|
passes: {},
|
|
2805
1776
|
_hmrVersion: 0,
|
|
@@ -3059,20 +2030,14 @@ function useFrame(callback, priorityOrOptions) {
|
|
|
3059
2030
|
}
|
|
3060
2031
|
};
|
|
3061
2032
|
} else {
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
unregisterJob = registerOutside();
|
|
3071
|
-
});
|
|
3072
|
-
return () => {
|
|
3073
|
-
unsubReady();
|
|
3074
|
-
unregisterJob?.();
|
|
3075
|
-
};
|
|
2033
|
+
return scheduler.register(
|
|
2034
|
+
(state, delta) => {
|
|
2035
|
+
const frameState = state;
|
|
2036
|
+
if (!frameState.renderer) return;
|
|
2037
|
+
callbackRef.current?.(frameState, delta);
|
|
2038
|
+
},
|
|
2039
|
+
{ id, ...options }
|
|
2040
|
+
);
|
|
3076
2041
|
}
|
|
3077
2042
|
}, [store, scheduler, id, optionsKey, isLegacyPriority, isInsideCanvas]);
|
|
3078
2043
|
const isPaused = React.useSyncExternalStore(
|
|
@@ -3163,18 +2128,18 @@ function buildFromCache(input, textureCache) {
|
|
|
3163
2128
|
function useTexture(input, optionsOrOnLoad) {
|
|
3164
2129
|
const renderer = useThree((state) => state.internal.actualRenderer);
|
|
3165
2130
|
const store = useStore();
|
|
3166
|
-
const textureCache = useThree((state) => state.textures);
|
|
3167
2131
|
const options = typeof optionsOrOnLoad === "function" ? { onLoad: optionsOrOnLoad } : optionsOrOnLoad ?? {};
|
|
3168
|
-
const { onLoad, cache =
|
|
2132
|
+
const { onLoad, cache = true } = options;
|
|
3169
2133
|
const onLoadRef = useRef(onLoad);
|
|
3170
2134
|
onLoadRef.current = onLoad;
|
|
3171
2135
|
const onLoadCalledForRef = useRef(null);
|
|
3172
2136
|
const urls = useMemo(() => getUrls(input), [input]);
|
|
3173
2137
|
const cachedResult = useMemo(() => {
|
|
3174
2138
|
if (!cache) return null;
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
2139
|
+
const textures = store.getState().textures;
|
|
2140
|
+
if (!allUrlsCached(urls, textures)) return null;
|
|
2141
|
+
return buildFromCache(input, textures);
|
|
2142
|
+
}, [cache, urls, input, store]);
|
|
3178
2143
|
const loadedTextures = useLoader(
|
|
3179
2144
|
TextureLoader,
|
|
3180
2145
|
IsObject(input) ? Object.values(input) : input
|
|
@@ -3218,8 +2183,6 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3218
2183
|
}, [input, loadedTextures, cachedResult]);
|
|
3219
2184
|
useEffect(() => {
|
|
3220
2185
|
if (!cache) return;
|
|
3221
|
-
if (cachedResult) return;
|
|
3222
|
-
const set = store.setState;
|
|
3223
2186
|
const urlTextureMap = [];
|
|
3224
2187
|
if (typeof input === "string") {
|
|
3225
2188
|
urlTextureMap.push([input, mappedTextures]);
|
|
@@ -3233,18 +2196,32 @@ function useTexture(input, optionsOrOnLoad) {
|
|
|
3233
2196
|
urlTextureMap.push([url, textureRecord[key]]);
|
|
3234
2197
|
}
|
|
3235
2198
|
}
|
|
3236
|
-
|
|
3237
|
-
const
|
|
3238
|
-
let
|
|
2199
|
+
store.setState((state) => {
|
|
2200
|
+
const refs = new Map(state._textureRefs);
|
|
2201
|
+
let textures = state.textures;
|
|
2202
|
+
let added = false;
|
|
3239
2203
|
for (const [url, texture] of urlTextureMap) {
|
|
3240
|
-
if (!
|
|
3241
|
-
|
|
3242
|
-
|
|
2204
|
+
if (!textures.has(url)) {
|
|
2205
|
+
if (!added) {
|
|
2206
|
+
textures = new Map(textures);
|
|
2207
|
+
added = true;
|
|
2208
|
+
}
|
|
2209
|
+
textures.set(url, texture);
|
|
3243
2210
|
}
|
|
2211
|
+
refs.set(url, (refs.get(url) ?? 0) + 1);
|
|
3244
2212
|
}
|
|
3245
|
-
return
|
|
2213
|
+
return added ? { textures, _textureRefs: refs } : { _textureRefs: refs };
|
|
3246
2214
|
});
|
|
3247
|
-
|
|
2215
|
+
return () => store.setState((state) => {
|
|
2216
|
+
const refs = new Map(state._textureRefs);
|
|
2217
|
+
for (const [url] of urlTextureMap) {
|
|
2218
|
+
const next = (refs.get(url) ?? 0) - 1;
|
|
2219
|
+
if (next <= 0) refs.delete(url);
|
|
2220
|
+
else refs.set(url, next);
|
|
2221
|
+
}
|
|
2222
|
+
return { _textureRefs: refs };
|
|
2223
|
+
});
|
|
2224
|
+
}, [cache, input, mappedTextures, store]);
|
|
3248
2225
|
return mappedTextures;
|
|
3249
2226
|
}
|
|
3250
2227
|
useTexture.preload = (url) => useLoader.preload(TextureLoader, url);
|
|
@@ -3260,96 +2237,63 @@ const Texture = ({
|
|
|
3260
2237
|
return /* @__PURE__ */ jsx(Fragment, { children: children?.(ret) });
|
|
3261
2238
|
};
|
|
3262
2239
|
|
|
3263
|
-
function
|
|
3264
|
-
if (entry instanceof Texture$1) return entry;
|
|
3265
|
-
if (entry && typeof entry === "object" && "value" in entry && entry.value instanceof Texture$1) {
|
|
3266
|
-
return entry.value;
|
|
3267
|
-
}
|
|
3268
|
-
return null;
|
|
3269
|
-
}
|
|
3270
|
-
function useTextures() {
|
|
2240
|
+
function useTextures(selector) {
|
|
3271
2241
|
const store = useStore();
|
|
3272
|
-
|
|
3273
|
-
const set = store.setState;
|
|
2242
|
+
const registry = useMemo(() => {
|
|
3274
2243
|
const getState = store.getState;
|
|
3275
|
-
const
|
|
3276
|
-
|
|
3277
|
-
const newMap = new Map(state.textures);
|
|
3278
|
-
newMap.set(key, value);
|
|
3279
|
-
return { textures: newMap };
|
|
3280
|
-
});
|
|
3281
|
-
};
|
|
3282
|
-
const addMultiple = (items) => {
|
|
3283
|
-
set((state) => {
|
|
3284
|
-
const newMap = new Map(state.textures);
|
|
3285
|
-
const entries = items instanceof Map ? items.entries() : Object.entries(items);
|
|
3286
|
-
for (const [key, value] of entries) {
|
|
3287
|
-
newMap.set(key, value);
|
|
3288
|
-
}
|
|
3289
|
-
return { textures: newMap };
|
|
3290
|
-
});
|
|
3291
|
-
};
|
|
3292
|
-
const remove = (key) => {
|
|
3293
|
-
set((state) => {
|
|
3294
|
-
const newMap = new Map(state.textures);
|
|
3295
|
-
newMap.delete(key);
|
|
3296
|
-
return { textures: newMap };
|
|
3297
|
-
});
|
|
3298
|
-
};
|
|
3299
|
-
const removeMultiple = (keys) => {
|
|
3300
|
-
set((state) => {
|
|
3301
|
-
const newMap = new Map(state.textures);
|
|
3302
|
-
for (const key of keys) newMap.delete(key);
|
|
3303
|
-
return { textures: newMap };
|
|
3304
|
-
});
|
|
3305
|
-
};
|
|
3306
|
-
const dispose = (key) => {
|
|
3307
|
-
const entry = getState().textures.get(key);
|
|
3308
|
-
if (entry) {
|
|
3309
|
-
const tex = getTextureValue(entry);
|
|
3310
|
-
tex?.dispose();
|
|
3311
|
-
}
|
|
3312
|
-
remove(key);
|
|
3313
|
-
};
|
|
3314
|
-
const disposeMultiple = (keys) => {
|
|
3315
|
-
const textures = getState().textures;
|
|
3316
|
-
for (const key of keys) {
|
|
3317
|
-
const entry = textures.get(key);
|
|
3318
|
-
if (entry) {
|
|
3319
|
-
const tex = getTextureValue(entry);
|
|
3320
|
-
tex?.dispose();
|
|
3321
|
-
}
|
|
3322
|
-
}
|
|
3323
|
-
removeMultiple(keys);
|
|
3324
|
-
};
|
|
3325
|
-
const disposeAll = () => {
|
|
3326
|
-
const textures = getState().textures;
|
|
3327
|
-
for (const entry of textures.values()) {
|
|
3328
|
-
const tex = getTextureValue(entry);
|
|
3329
|
-
tex?.dispose();
|
|
3330
|
-
}
|
|
3331
|
-
set({ textures: /* @__PURE__ */ new Map() });
|
|
3332
|
-
};
|
|
2244
|
+
const setState = store.setState;
|
|
2245
|
+
const getOne = (key) => getState().textures.get(key);
|
|
3333
2246
|
return {
|
|
3334
|
-
|
|
3335
|
-
get textures() {
|
|
2247
|
+
get all() {
|
|
3336
2248
|
return getState().textures;
|
|
3337
2249
|
},
|
|
3338
|
-
|
|
3339
|
-
|
|
2250
|
+
get(input) {
|
|
2251
|
+
if (typeof input === "string") return getOne(input);
|
|
2252
|
+
if (Array.isArray(input)) return input.map(getOne);
|
|
2253
|
+
const out = {};
|
|
2254
|
+
for (const name in input) out[name] = getOne(input[name]);
|
|
2255
|
+
return out;
|
|
2256
|
+
},
|
|
3340
2257
|
has: (key) => getState().textures.has(key),
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
2258
|
+
add(keyOrRecord, texture) {
|
|
2259
|
+
setState((state) => {
|
|
2260
|
+
const textures = new Map(state.textures);
|
|
2261
|
+
if (typeof keyOrRecord === "string") {
|
|
2262
|
+
textures.set(keyOrRecord, texture);
|
|
2263
|
+
} else {
|
|
2264
|
+
for (const key in keyOrRecord) textures.set(key, keyOrRecord[key]);
|
|
2265
|
+
}
|
|
2266
|
+
return { textures };
|
|
2267
|
+
});
|
|
2268
|
+
},
|
|
2269
|
+
dispose(key, options) {
|
|
2270
|
+
const state = getState();
|
|
2271
|
+
const refs = state._textureRefs.get(key) ?? 0;
|
|
2272
|
+
if (refs > 0 && !options?.force) {
|
|
2273
|
+
console.warn(
|
|
2274
|
+
`[useTextures] "${key}" still has ${refs} active reference(s); skipping dispose. Pass { force: true } to override.`
|
|
2275
|
+
);
|
|
2276
|
+
return false;
|
|
2277
|
+
}
|
|
2278
|
+
state.textures.get(key)?.dispose();
|
|
2279
|
+
setState((s) => {
|
|
2280
|
+
const textures = new Map(s.textures);
|
|
2281
|
+
textures.delete(key);
|
|
2282
|
+
const nextRefs = new Map(s._textureRefs);
|
|
2283
|
+
nextRefs.delete(key);
|
|
2284
|
+
return { textures, _textureRefs: nextRefs };
|
|
2285
|
+
});
|
|
2286
|
+
return true;
|
|
2287
|
+
},
|
|
2288
|
+
disposeAll() {
|
|
2289
|
+
for (const texture of getState().textures.values()) texture.dispose();
|
|
2290
|
+
setState({ textures: /* @__PURE__ */ new Map(), _textureRefs: /* @__PURE__ */ new Map() });
|
|
2291
|
+
}
|
|
3351
2292
|
};
|
|
3352
2293
|
}, [store]);
|
|
2294
|
+
const subscribe = selector ? () => selector(registry) : (state) => state.textures;
|
|
2295
|
+
const selected = useThree(subscribe);
|
|
2296
|
+
return selector ? selected : registry;
|
|
3353
2297
|
}
|
|
3354
2298
|
|
|
3355
2299
|
function useRenderTarget(widthOrOptions, heightOrOptions, options) {
|
|
@@ -15519,7 +14463,7 @@ function createRoot(canvas) {
|
|
|
15519
14463
|
{
|
|
15520
14464
|
id: `${newRootId}_frustum`,
|
|
15521
14465
|
rootId: newRootId,
|
|
15522
|
-
|
|
14466
|
+
before: "render",
|
|
15523
14467
|
system: true
|
|
15524
14468
|
}
|
|
15525
14469
|
);
|
|
@@ -15531,7 +14475,7 @@ function createRoot(canvas) {
|
|
|
15531
14475
|
{
|
|
15532
14476
|
id: `${newRootId}_visibility`,
|
|
15533
14477
|
rootId: newRootId,
|
|
15534
|
-
|
|
14478
|
+
before: "render",
|
|
15535
14479
|
system: true,
|
|
15536
14480
|
after: `${newRootId}_frustum`
|
|
15537
14481
|
}
|
|
@@ -15754,6 +14698,46 @@ function flushSync(fn) {
|
|
|
15754
14698
|
return reconciler.flushSyncFromReconciler(fn);
|
|
15755
14699
|
}
|
|
15756
14700
|
|
|
14701
|
+
function parseBackground(background) {
|
|
14702
|
+
if (!background) return null;
|
|
14703
|
+
if (typeof background === "object" && !background.isColor) {
|
|
14704
|
+
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
14705
|
+
return {
|
|
14706
|
+
...rest,
|
|
14707
|
+
preset,
|
|
14708
|
+
files: envMap || files,
|
|
14709
|
+
backgroundFiles: backgroundMap,
|
|
14710
|
+
background: true
|
|
14711
|
+
};
|
|
14712
|
+
}
|
|
14713
|
+
if (typeof background === "number") {
|
|
14714
|
+
return { color: background, background: true };
|
|
14715
|
+
}
|
|
14716
|
+
if (typeof background === "string") {
|
|
14717
|
+
if (background in presetsObj) {
|
|
14718
|
+
return { preset: background, background: true };
|
|
14719
|
+
}
|
|
14720
|
+
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
14721
|
+
return { files: background, background: true };
|
|
14722
|
+
}
|
|
14723
|
+
return { color: background, background: true };
|
|
14724
|
+
}
|
|
14725
|
+
if (background.isColor) {
|
|
14726
|
+
return { color: background, background: true };
|
|
14727
|
+
}
|
|
14728
|
+
return null;
|
|
14729
|
+
}
|
|
14730
|
+
|
|
14731
|
+
function clearHmrCaches(store) {
|
|
14732
|
+
store.setState((state) => ({
|
|
14733
|
+
nodes: {},
|
|
14734
|
+
uniforms: {},
|
|
14735
|
+
buffers: {},
|
|
14736
|
+
gpuStorage: {},
|
|
14737
|
+
_hmrVersion: state._hmrVersion + 1
|
|
14738
|
+
}));
|
|
14739
|
+
}
|
|
14740
|
+
|
|
15757
14741
|
function CanvasImpl({
|
|
15758
14742
|
ref,
|
|
15759
14743
|
children,
|
|
@@ -15774,6 +14758,8 @@ function CanvasImpl({
|
|
|
15774
14758
|
raycaster,
|
|
15775
14759
|
camera,
|
|
15776
14760
|
scene,
|
|
14761
|
+
autoUpdateFrustum,
|
|
14762
|
+
occlusion,
|
|
15777
14763
|
onPointerMissed,
|
|
15778
14764
|
onDragOverMissed,
|
|
15779
14765
|
onDropMissed,
|
|
@@ -15799,35 +14785,7 @@ function CanvasImpl({
|
|
|
15799
14785
|
}
|
|
15800
14786
|
React.useMemo(() => extend(THREE), []);
|
|
15801
14787
|
const Bridge = useBridge();
|
|
15802
|
-
const backgroundProps = React.useMemo(() =>
|
|
15803
|
-
if (!background) return null;
|
|
15804
|
-
if (typeof background === "object" && !background.isColor) {
|
|
15805
|
-
const { backgroundMap, envMap, files, preset, ...rest } = background;
|
|
15806
|
-
return {
|
|
15807
|
-
...rest,
|
|
15808
|
-
preset,
|
|
15809
|
-
files: envMap || files,
|
|
15810
|
-
backgroundFiles: backgroundMap,
|
|
15811
|
-
background: true
|
|
15812
|
-
};
|
|
15813
|
-
}
|
|
15814
|
-
if (typeof background === "number") {
|
|
15815
|
-
return { color: background, background: true };
|
|
15816
|
-
}
|
|
15817
|
-
if (typeof background === "string") {
|
|
15818
|
-
if (background in presetsObj) {
|
|
15819
|
-
return { preset: background, background: true };
|
|
15820
|
-
}
|
|
15821
|
-
if (/^(https?:\/\/|\/|\.\/|\.\.\/)|\\.(hdr|exr|jpg|jpeg|png|webp|gif)$/i.test(background)) {
|
|
15822
|
-
return { files: background, background: true };
|
|
15823
|
-
}
|
|
15824
|
-
return { color: background, background: true };
|
|
15825
|
-
}
|
|
15826
|
-
if (background.isColor) {
|
|
15827
|
-
return { color: background, background: true };
|
|
15828
|
-
}
|
|
15829
|
-
return null;
|
|
15830
|
-
}, [background]);
|
|
14788
|
+
const backgroundProps = React.useMemo(() => parseBackground(background), [background]);
|
|
15831
14789
|
const hasInitialSizeRef = React.useRef(false);
|
|
15832
14790
|
const measureConfig = React.useMemo(() => {
|
|
15833
14791
|
if (!hasInitialSizeRef.current) {
|
|
@@ -15911,6 +14869,8 @@ function CanvasImpl({
|
|
|
15911
14869
|
performance,
|
|
15912
14870
|
raycaster,
|
|
15913
14871
|
camera,
|
|
14872
|
+
autoUpdateFrustum,
|
|
14873
|
+
occlusion,
|
|
15914
14874
|
size: effectiveSize,
|
|
15915
14875
|
// Store size props for reset functionality
|
|
15916
14876
|
_sizeProps: width !== void 0 || height !== void 0 ? { width, height } : null,
|
|
@@ -15970,14 +14930,7 @@ function CanvasImpl({
|
|
|
15970
14930
|
const handleHMR = () => {
|
|
15971
14931
|
queueMicrotask(() => {
|
|
15972
14932
|
const rootEntry = _roots.get(canvas);
|
|
15973
|
-
if (rootEntry?.store)
|
|
15974
|
-
console.log("[R3F] HMR detected \u2014 rebuilding nodes/uniforms");
|
|
15975
|
-
rootEntry.store.setState((state) => ({
|
|
15976
|
-
nodes: {},
|
|
15977
|
-
uniforms: {},
|
|
15978
|
-
_hmrVersion: state._hmrVersion + 1
|
|
15979
|
-
}));
|
|
15980
|
-
}
|
|
14933
|
+
if (rootEntry?.store) clearHmrCaches(rootEntry.store);
|
|
15981
14934
|
});
|
|
15982
14935
|
};
|
|
15983
14936
|
if (typeof import.meta !== "undefined" && import.meta.hot) {
|
|
@@ -16025,4 +14978,4 @@ function Canvas(props) {
|
|
|
16025
14978
|
|
|
16026
14979
|
extend(THREE);
|
|
16027
14980
|
|
|
16028
|
-
export { Block, Canvas, Environment, EnvironmentCube, EnvironmentMap, EnvironmentPortal, ErrorBoundary, FROM_REF, IsObject, ONCE, Portal, R3F_BUILD_LEGACY, R3F_BUILD_WEBGPU, REACT_INTERNAL_PROPS, RESERVED_PROPS,
|
|
14981
|
+
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 };
|