Setting the file. One moment.
Adaptive Resolution · Frost Sequence Camera Orbit · heygen-com/hyperframes · Skills Docs
ContentsBack to the top of the page Three Mesh BVH LICENSE
(opens in a new tab)
source/src/core/ AdaptiveResolution.ts
TypeScript · 87 lines · 3 KB
10
11 update (
12 now : number ,
13 elapsedMs : number ,
14 deviceRatio : number ,
15 cap : number ,
16 enabled : boolean ,
17 targetFps : number ,
18 minimum : number ,
19 ) {
20 const maximum = Math. max ( 0.5 , Math. min (deviceRatio || 1 , cap));
21 const minRatio = Math. min (maximum, Math. max ( 0.5 , minimum));
22 if ( ! this .ratio || ! enabled) this .ratio = maximum;
23 this .ratio = Math. min (maximum, Math. max (minRatio, this .ratio));
24
25 // Clip one-off shader compilation/rebuild stalls before feeding the controller.
26 const sample = Math. min ( 50 , Math. max ( 1 , elapsedMs));
27 this .frameMs = this .frames === 0 ? sample : this .frameMs + (sample - this .frameMs) * 0.08 ;
28 this .frames ++ ;
29 if ( ! enabled || this .frames < 45 || now - this .lastAdjust < 0.5 ) return this .ratio;
30
31 const budget = 1000 / Math. max ( 30 , targetFps);
32 let next = this .ratio;
33 if ( this .frameMs > budget * 1.08 && this .ratio > minRatio) {
34 const ideal = this .ratio * Math. sqrt (budget / this .frameMs) * 0.98 ;
35 next = Math. min ( this .ratio - 0.05 , ideal);
36 next = Math. floor (next * 20 ) / 20 ;
37 } else if ( this .frameMs < budget * 0.82 && this .ratio < maximum) {
38 next = Math. ceil (( this .ratio + 0.05 ) * 20 ) / 20 ;
39 }
40 next = Math. min (maximum, Math. max (minRatio, next));
41 if (next !== this .ratio) {
42 this .ratio = next;
43 // Give the resized render targets time to establish their new steady-state cost.
44 this .frameMs = budget;
45 this .lastAdjust = now;
46 }
47 return this .ratio;
48 }
49
50 /**
51 * Plans a split-resolution frame. `sourceRatio` is the DPR paid by the scene pass; `outputRatio` is the
52 * canvas/final-post DPR. With split output disabled they are identical (whole-frame scaling fallback).
53 */
54 updateScene (
55 now : number ,
56 elapsedMs : number ,
57 deviceRatio : number ,
58 outputCap : number ,
59 enabled : boolean ,
60 targetFps : number ,
61 minPixelRatio : number ,
62 maxSceneScale : number ,
63 minSceneScale : number ,
64 splitOutput : boolean ,
65 ) {
66 const nativeRatio = Math. max ( 0.5 , Math. min (deviceRatio || 1 , outputCap));
67 const maxScale = Math. max ( 0.25 , Math. min ( 1 , maxSceneScale));
68 const sourceMaximum = Math. max ( 0.5 , nativeRatio * maxScale);
69 const relativeMinimum = nativeRatio * Math. max ( 0.25 , Math. min (maxScale, minSceneScale));
70 const sourceMinimum = Math. min (sourceMaximum, Math. max ( 0.5 , minPixelRatio, relativeMinimum));
71 const sourceRatio = this . update (
72 now,
73 elapsedMs,
74 sourceMaximum,
75 sourceMaximum,
76 enabled,
77 targetFps,
78 sourceMinimum,
79 );
80 const outputRatio = splitOutput ? nativeRatio : sourceRatio;
81 return {
82 sourceRatio,
83 outputRatio,
84 sceneScale: splitOutput ? sourceRatio / nativeRatio : 1 ,
85 };
86 }
87 }