sceneOptimizer.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import type { Scene, IDisposable } from "../scene";
  2. import { Observable } from "./observable";
  3. /**
  4. * Defines the root class used to create scene optimization to use with SceneOptimizer
  5. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  6. */
  7. export declare class SceneOptimization {
  8. /**
  9. * Defines the priority of this optimization (0 by default which means first in the list)
  10. */
  11. priority: number;
  12. /**
  13. * Gets a string describing the action executed by the current optimization
  14. * @returns description string
  15. */
  16. getDescription(): string;
  17. /**
  18. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  19. * @param scene defines the current scene where to apply this optimization
  20. * @param optimizer defines the current optimizer
  21. * @returns true if everything that can be done was applied
  22. */
  23. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  24. /**
  25. * Creates the SceneOptimization object
  26. * @param priority defines the priority of this optimization (0 by default which means first in the list)
  27. */
  28. constructor(
  29. /**
  30. * Defines the priority of this optimization (0 by default which means first in the list)
  31. */
  32. priority?: number);
  33. }
  34. /**
  35. * Defines an optimization used to reduce the size of render target textures
  36. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  37. */
  38. export declare class TextureOptimization extends SceneOptimization {
  39. /**
  40. * Defines the priority of this optimization (0 by default which means first in the list)
  41. */
  42. priority: number;
  43. /**
  44. * Defines the maximum sized allowed for textures (1024 is the default value). If a texture is bigger, it will be scaled down using a factor defined by the step parameter
  45. */
  46. maximumSize: number;
  47. /**
  48. * Defines the factor (0.5 by default) used to scale down textures bigger than maximum sized allowed.
  49. */
  50. step: number;
  51. /**
  52. * Gets a string describing the action executed by the current optimization
  53. * @returns description string
  54. */
  55. getDescription(): string;
  56. /**
  57. * Creates the TextureOptimization object
  58. * @param priority defines the priority of this optimization (0 by default which means first in the list)
  59. * @param maximumSize defines the maximum sized allowed for textures (1024 is the default value). If a texture is bigger, it will be scaled down using a factor defined by the step parameter
  60. * @param step defines the factor (0.5 by default) used to scale down textures bigger than maximum sized allowed.
  61. */
  62. constructor(
  63. /**
  64. * Defines the priority of this optimization (0 by default which means first in the list)
  65. */
  66. priority?: number,
  67. /**
  68. * Defines the maximum sized allowed for textures (1024 is the default value). If a texture is bigger, it will be scaled down using a factor defined by the step parameter
  69. */
  70. maximumSize?: number,
  71. /**
  72. * Defines the factor (0.5 by default) used to scale down textures bigger than maximum sized allowed.
  73. */
  74. step?: number);
  75. /**
  76. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  77. * @param scene defines the current scene where to apply this optimization
  78. * @param optimizer defines the current optimizer
  79. * @returns true if everything that can be done was applied
  80. */
  81. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  82. }
  83. /**
  84. * Defines an optimization used to increase or decrease the rendering resolution
  85. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  86. */
  87. export declare class HardwareScalingOptimization extends SceneOptimization {
  88. /**
  89. * Defines the priority of this optimization (0 by default which means first in the list)
  90. */
  91. priority: number;
  92. /**
  93. * Defines the maximum scale to use (2 by default)
  94. */
  95. maximumScale: number;
  96. /**
  97. * Defines the step to use between two passes (0.5 by default)
  98. */
  99. step: number;
  100. private _currentScale;
  101. private _directionOffset;
  102. /**
  103. * Gets a string describing the action executed by the current optimization
  104. * @returns description string
  105. */
  106. getDescription(): string;
  107. /**
  108. * Creates the HardwareScalingOptimization object
  109. * @param priority defines the priority of this optimization (0 by default which means first in the list)
  110. * @param maximumScale defines the maximum scale to use (2 by default)
  111. * @param step defines the step to use between two passes (0.5 by default)
  112. */
  113. constructor(
  114. /**
  115. * Defines the priority of this optimization (0 by default which means first in the list)
  116. */
  117. priority?: number,
  118. /**
  119. * Defines the maximum scale to use (2 by default)
  120. */
  121. maximumScale?: number,
  122. /**
  123. * Defines the step to use between two passes (0.5 by default)
  124. */
  125. step?: number);
  126. /**
  127. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  128. * @param scene defines the current scene where to apply this optimization
  129. * @param optimizer defines the current optimizer
  130. * @returns true if everything that can be done was applied
  131. */
  132. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  133. }
  134. /**
  135. * Defines an optimization used to remove shadows
  136. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  137. */
  138. export declare class ShadowsOptimization extends SceneOptimization {
  139. /**
  140. * Gets a string describing the action executed by the current optimization
  141. * @returns description string
  142. */
  143. getDescription(): string;
  144. /**
  145. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  146. * @param scene defines the current scene where to apply this optimization
  147. * @param optimizer defines the current optimizer
  148. * @returns true if everything that can be done was applied
  149. */
  150. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  151. }
  152. /**
  153. * Defines an optimization used to turn post-processes off
  154. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  155. */
  156. export declare class PostProcessesOptimization extends SceneOptimization {
  157. /**
  158. * Gets a string describing the action executed by the current optimization
  159. * @returns description string
  160. */
  161. getDescription(): string;
  162. /**
  163. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  164. * @param scene defines the current scene where to apply this optimization
  165. * @param optimizer defines the current optimizer
  166. * @returns true if everything that can be done was applied
  167. */
  168. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  169. }
  170. /**
  171. * Defines an optimization used to turn lens flares off
  172. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  173. */
  174. export declare class LensFlaresOptimization extends SceneOptimization {
  175. /**
  176. * Gets a string describing the action executed by the current optimization
  177. * @returns description string
  178. */
  179. getDescription(): string;
  180. /**
  181. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  182. * @param scene defines the current scene where to apply this optimization
  183. * @param optimizer defines the current optimizer
  184. * @returns true if everything that can be done was applied
  185. */
  186. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  187. }
  188. /**
  189. * Defines an optimization based on user defined callback.
  190. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  191. */
  192. export declare class CustomOptimization extends SceneOptimization {
  193. /**
  194. * Callback called to apply the custom optimization.
  195. */
  196. onApply: (scene: Scene, optimizer: SceneOptimizer) => boolean;
  197. /**
  198. * Callback called to get custom description
  199. */
  200. onGetDescription: () => string;
  201. /**
  202. * Gets a string describing the action executed by the current optimization
  203. * @returns description string
  204. */
  205. getDescription(): string;
  206. /**
  207. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  208. * @param scene defines the current scene where to apply this optimization
  209. * @param optimizer defines the current optimizer
  210. * @returns true if everything that can be done was applied
  211. */
  212. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  213. }
  214. /**
  215. * Defines an optimization used to turn particles off
  216. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  217. */
  218. export declare class ParticlesOptimization extends SceneOptimization {
  219. /**
  220. * Gets a string describing the action executed by the current optimization
  221. * @returns description string
  222. */
  223. getDescription(): string;
  224. /**
  225. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  226. * @param scene defines the current scene where to apply this optimization
  227. * @param optimizer defines the current optimizer
  228. * @returns true if everything that can be done was applied
  229. */
  230. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  231. }
  232. /**
  233. * Defines an optimization used to turn render targets off
  234. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  235. */
  236. export declare class RenderTargetsOptimization extends SceneOptimization {
  237. /**
  238. * Gets a string describing the action executed by the current optimization
  239. * @returns description string
  240. */
  241. getDescription(): string;
  242. /**
  243. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  244. * @param scene defines the current scene where to apply this optimization
  245. * @param optimizer defines the current optimizer
  246. * @returns true if everything that can be done was applied
  247. */
  248. apply(scene: Scene, optimizer: SceneOptimizer): boolean;
  249. }
  250. /**
  251. * Defines an optimization used to merge meshes with compatible materials
  252. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  253. */
  254. export declare class MergeMeshesOptimization extends SceneOptimization {
  255. private static _UpdateSelectionTree;
  256. /**
  257. * Gets or sets a boolean which defines if optimization octree has to be updated
  258. */
  259. static get UpdateSelectionTree(): boolean;
  260. /**
  261. * Gets or sets a boolean which defines if optimization octree has to be updated
  262. */
  263. static set UpdateSelectionTree(value: boolean);
  264. /**
  265. * Gets a string describing the action executed by the current optimization
  266. * @returns description string
  267. */
  268. getDescription(): string;
  269. private _canBeMerged;
  270. /**
  271. * This function will be called by the SceneOptimizer when its priority is reached in order to apply the change required by the current optimization
  272. * @param scene defines the current scene where to apply this optimization
  273. * @param optimizer defines the current optimizer
  274. * @param updateSelectionTree defines that the selection octree has to be updated (false by default)
  275. * @returns true if everything that can be done was applied
  276. */
  277. apply(scene: Scene, optimizer: SceneOptimizer, updateSelectionTree?: boolean): boolean;
  278. }
  279. /**
  280. * Defines a list of options used by SceneOptimizer
  281. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  282. */
  283. export declare class SceneOptimizerOptions {
  284. /**
  285. * Defines the target frame rate to reach (60 by default)
  286. */
  287. targetFrameRate: number;
  288. /**
  289. * Defines the interval between two checks (2000ms by default)
  290. */
  291. trackerDuration: number;
  292. /**
  293. * Gets the list of optimizations to apply
  294. */
  295. optimizations: SceneOptimization[];
  296. /**
  297. * Creates a new list of options used by SceneOptimizer
  298. * @param targetFrameRate defines the target frame rate to reach (60 by default)
  299. * @param trackerDuration defines the interval between two checks (2000ms by default)
  300. */
  301. constructor(
  302. /**
  303. * Defines the target frame rate to reach (60 by default)
  304. */
  305. targetFrameRate?: number,
  306. /**
  307. * Defines the interval between two checks (2000ms by default)
  308. */
  309. trackerDuration?: number);
  310. /**
  311. * Add a new optimization
  312. * @param optimization defines the SceneOptimization to add to the list of active optimizations
  313. * @returns the current SceneOptimizerOptions
  314. */
  315. addOptimization(optimization: SceneOptimization): SceneOptimizerOptions;
  316. /**
  317. * Add a new custom optimization
  318. * @param onApply defines the callback called to apply the custom optimization (true if everything that can be done was applied)
  319. * @param onGetDescription defines the callback called to get the description attached with the optimization.
  320. * @param priority defines the priority of this optimization (0 by default which means first in the list)
  321. * @returns the current SceneOptimizerOptions
  322. */
  323. addCustomOptimization(onApply: (scene: Scene, optimizer: SceneOptimizer) => boolean, onGetDescription: () => string, priority?: number): SceneOptimizerOptions;
  324. /**
  325. * Creates a list of pre-defined optimizations aimed to reduce the visual impact on the scene
  326. * @param targetFrameRate defines the target frame rate (60 by default)
  327. * @returns a SceneOptimizerOptions object
  328. */
  329. static LowDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions;
  330. /**
  331. * Creates a list of pre-defined optimizations aimed to have a moderate impact on the scene visual
  332. * @param targetFrameRate defines the target frame rate (60 by default)
  333. * @returns a SceneOptimizerOptions object
  334. */
  335. static ModerateDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions;
  336. /**
  337. * Creates a list of pre-defined optimizations aimed to have a big impact on the scene visual
  338. * @param targetFrameRate defines the target frame rate (60 by default)
  339. * @returns a SceneOptimizerOptions object
  340. */
  341. static HighDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions;
  342. }
  343. /**
  344. * Class used to run optimizations in order to reach a target frame rate
  345. * @description More details at https://doc.babylonjs.com/features/featuresDeepDive/scene/sceneOptimizer
  346. */
  347. export declare class SceneOptimizer implements IDisposable {
  348. private _isRunning;
  349. private _options;
  350. private _scene;
  351. private _currentPriorityLevel;
  352. private _targetFrameRate;
  353. private _trackerDuration;
  354. private _currentFrameRate;
  355. private _sceneDisposeObserver;
  356. private _improvementMode;
  357. /**
  358. * Defines an observable called when the optimizer reaches the target frame rate
  359. */
  360. onSuccessObservable: Observable<SceneOptimizer>;
  361. /**
  362. * Defines an observable called when the optimizer enables an optimization
  363. */
  364. onNewOptimizationAppliedObservable: Observable<SceneOptimization>;
  365. /**
  366. * Defines an observable called when the optimizer is not able to reach the target frame rate
  367. */
  368. onFailureObservable: Observable<SceneOptimizer>;
  369. /**
  370. * Gets or sets a boolean indicating if the optimizer is in improvement mode
  371. */
  372. get isInImprovementMode(): boolean;
  373. set isInImprovementMode(value: boolean);
  374. /**
  375. * Gets the current priority level (0 at start)
  376. */
  377. get currentPriorityLevel(): number;
  378. /**
  379. * Gets the current frame rate checked by the SceneOptimizer
  380. */
  381. get currentFrameRate(): number;
  382. /**
  383. * Gets or sets the current target frame rate (60 by default)
  384. */
  385. get targetFrameRate(): number;
  386. /**
  387. * Gets or sets the current target frame rate (60 by default)
  388. */
  389. set targetFrameRate(value: number);
  390. /**
  391. * Gets or sets the current interval between two checks (every 2000ms by default)
  392. */
  393. get trackerDuration(): number;
  394. /**
  395. * Gets or sets the current interval between two checks (every 2000ms by default)
  396. */
  397. set trackerDuration(value: number);
  398. /**
  399. * Gets the list of active optimizations
  400. */
  401. get optimizations(): SceneOptimization[];
  402. /**
  403. * Creates a new SceneOptimizer
  404. * @param scene defines the scene to work on
  405. * @param options defines the options to use with the SceneOptimizer
  406. * @param autoGeneratePriorities defines if priorities must be generated and not read from SceneOptimization property (true by default)
  407. * @param improvementMode defines if the scene optimizer must run the maximum optimization while staying over a target frame instead of trying to reach the target framerate (false by default)
  408. */
  409. constructor(scene: Scene, options?: SceneOptimizerOptions, autoGeneratePriorities?: boolean, improvementMode?: boolean);
  410. /**
  411. * Stops the current optimizer
  412. */
  413. stop(): void;
  414. /**
  415. * Reset the optimizer to initial step (current priority level = 0)
  416. */
  417. reset(): void;
  418. /**
  419. * Start the optimizer. By default it will try to reach a specific framerate
  420. * but if the optimizer is set with improvementMode === true then it will run all optimization while frame rate is above the target frame rate
  421. */
  422. start(): void;
  423. private _checkCurrentState;
  424. /**
  425. * Release all resources
  426. */
  427. dispose(): void;
  428. /**
  429. * Helper function to create a SceneOptimizer with one single line of code
  430. * @param scene defines the scene to work on
  431. * @param options defines the options to use with the SceneOptimizer
  432. * @param onSuccess defines a callback to call on success
  433. * @param onFailure defines a callback to call on failure
  434. * @returns the new SceneOptimizer object
  435. */
  436. static OptimizeAsync(scene: Scene, options?: SceneOptimizerOptions, onSuccess?: () => void, onFailure?: () => void): SceneOptimizer;
  437. }