@react-three/drei 9.24.0 → 9.24.1
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/README.md +26 -9
- package/core/PerformanceMonitor.d.ts +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1917,6 +1917,10 @@ useBVH(mesh)
|
|
|
1917
1917
|
|
|
1918
1918
|
#### PerformanceMonitor
|
|
1919
1919
|
|
|
1920
|
+
This component will collect the average fps (frames per second) over time. If after a couple of iterations the averages are below or above a threshold it will trigger onIncline and onDecline callbacks that allow you to respond. Typically you would reduce the quality of your scene, the resolution, effects, the amount of stuff to render, or, increase it if you have enough framerate to fill.
|
|
1921
|
+
|
|
1922
|
+
Since this would normally cause ping-ponging between the two callbacks you define upper and lower framerate bounds, as long as you stay within that margin nothing will trigger. Ideally your app should find its way into that margin by gradually altering quality.
|
|
1923
|
+
|
|
1920
1924
|
```tsx
|
|
1921
1925
|
type PerformanceMonitorProps = {
|
|
1922
1926
|
/** How much time in milliseconds to collect an average fps, 250 */
|
|
@@ -1946,18 +1950,31 @@ type PerformanceMonitorProps = {
|
|
|
1946
1950
|
}
|
|
1947
1951
|
```
|
|
1948
1952
|
|
|
1949
|
-
|
|
1953
|
+
All callbacks give you the following data:
|
|
1950
1954
|
|
|
1951
|
-
|
|
1955
|
+
```tsx
|
|
1956
|
+
type PerformanceMonitorApi = {
|
|
1957
|
+
/** Current fps */
|
|
1958
|
+
fps: number
|
|
1959
|
+
/** Current performance factor, between 0 and 1 */
|
|
1960
|
+
factor: number
|
|
1961
|
+
/** Current highest fps, you can use this to determine device refresh rate */
|
|
1962
|
+
refreshrate: number
|
|
1963
|
+
/** Fps samples taken over time */
|
|
1964
|
+
frames: number[]
|
|
1965
|
+
/** Averages of frames taken over n iterations */
|
|
1966
|
+
averages: number[]
|
|
1967
|
+
}
|
|
1968
|
+
```
|
|
1952
1969
|
|
|
1953
1970
|
A simple example for regulating the resolution. It starts out with 1.5, if the system falls below the bounds it goes to 1, if it's fast enough it goes to 2.
|
|
1954
1971
|
|
|
1955
1972
|
```jsx
|
|
1956
1973
|
function App() {
|
|
1957
|
-
const [dpr,
|
|
1974
|
+
const [dpr, setDpr] = useState(1.5)
|
|
1958
1975
|
return (
|
|
1959
1976
|
<Canvas dpr={dpr}>
|
|
1960
|
-
<PerformanceMonitor onIncline={() =>
|
|
1977
|
+
<PerformanceMonitor onIncline={() => setDpr(2)} onDecline={() => setDpr(1)} >
|
|
1961
1978
|
```
|
|
1962
1979
|
|
|
1963
1980
|
You can also use the `onChange` callback to get notified when the average changes in whichever direction. This allows you to make gradual changes. It gives you a factor between 0 and 1, which is increased by incline and decreased by decline. The factor is initially 0.5 by default.
|
|
@@ -1968,23 +1985,23 @@ import round from 'lodash/round'
|
|
|
1968
1985
|
const [dpr, set] = useState(1)
|
|
1969
1986
|
return (
|
|
1970
1987
|
<Canvas dpr={dpr}>
|
|
1971
|
-
<PerformanceMonitor onChange={({ factor }) =>
|
|
1988
|
+
<PerformanceMonitor onChange={({ factor }) => setDpr(round(0.5 + 1.5 * factor, 1))} >
|
|
1972
1989
|
```
|
|
1973
1990
|
|
|
1974
1991
|
If you still experience flip flops despite the bounds you can define a limit of `flipflops`. If it is met `onFallback` will be triggered which typically sets a lowest possible baseline for the app. After the fallback has been called PerformanceMonitor will shut down.
|
|
1975
1992
|
|
|
1976
1993
|
```jsx
|
|
1977
|
-
<PerformanceMonitor flipflops={3} onFallback={() =>
|
|
1994
|
+
<PerformanceMonitor flipflops={3} onFallback={() => setDpr(1)}>
|
|
1978
1995
|
```
|
|
1979
1996
|
|
|
1980
|
-
PerformanceMonitor can also have children, if you wrap your
|
|
1997
|
+
PerformanceMonitor can also have children, if you wrap your app in it you get to use `usePerformanceMonitor` which allows individual components down the nested tree to respond to performance changes on their own.
|
|
1981
1998
|
|
|
1982
1999
|
```jsx
|
|
1983
2000
|
;<PerformanceMonitor>
|
|
1984
|
-
<
|
|
2001
|
+
<Effects />
|
|
1985
2002
|
</PerformanceMonitor>
|
|
1986
2003
|
|
|
1987
|
-
function
|
|
2004
|
+
function Effects() {
|
|
1988
2005
|
usePerformanceMonitor({ onIncline, onDecline, onFallback, onChange })
|
|
1989
2006
|
// ...
|
|
1990
2007
|
}
|
|
@@ -7,13 +7,13 @@ declare type PerformanceMonitorHookApi = {
|
|
|
7
7
|
};
|
|
8
8
|
declare type PerformanceMonitorApi = {
|
|
9
9
|
fps: number;
|
|
10
|
-
index: number;
|
|
11
10
|
factor: number;
|
|
12
|
-
flipped: number;
|
|
13
11
|
refreshrate: number;
|
|
14
|
-
fallback: boolean;
|
|
15
12
|
frames: number[];
|
|
16
13
|
averages: number[];
|
|
14
|
+
index: number;
|
|
15
|
+
flipped: number;
|
|
16
|
+
fallback: boolean;
|
|
17
17
|
subscriptions: Map<Symbol, PerformanceMonitorHookApi>;
|
|
18
18
|
subscribe: (ref: React.MutableRefObject<PerformanceMonitorHookApi>) => () => void;
|
|
19
19
|
};
|
|
@@ -21,7 +21,7 @@ declare type PerformanceMonitorProps = {
|
|
|
21
21
|
ms?: number;
|
|
22
22
|
iterations?: number;
|
|
23
23
|
threshold?: number;
|
|
24
|
-
bounds
|
|
24
|
+
bounds?: (refreshrate: number) => [lower: number, upper: number];
|
|
25
25
|
flipflops?: number;
|
|
26
26
|
factor?: number;
|
|
27
27
|
step?: number;
|