babylon.sceneOptimizer.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. module BABYLON {
  2. // Standard optimizations
  3. export class SceneOptimization {
  4. public apply = (scene: Scene): boolean => {
  5. return true; // Return true if everything that can be done was applied
  6. };
  7. constructor(public priority: number = 0) {
  8. }
  9. }
  10. export class TextureOptimization extends SceneOptimization {
  11. constructor(public priority: number = 0, public maximumSize: number = 1024) {
  12. super(priority);
  13. }
  14. public apply = (scene: Scene): boolean => {
  15. var allDone = true;
  16. for (var index = 0; index < scene.textures.length; index++) {
  17. var texture = scene.textures[index];
  18. if (!texture.canRescale) {
  19. continue;
  20. }
  21. var currentSize = texture.getSize();
  22. var maxDimension = Math.max(currentSize.width, currentSize.height);
  23. if (maxDimension > this.maximumSize) {
  24. texture.scale(0.5);
  25. allDone = false;
  26. }
  27. }
  28. return allDone;
  29. }
  30. }
  31. export class HardwareScalingOptimization extends SceneOptimization {
  32. private _currentScale = 1;
  33. constructor(public priority: number = 0, public maximumScale: number = 2) {
  34. super(priority);
  35. }
  36. public apply = (scene: Scene): boolean => {
  37. this._currentScale++;
  38. scene.getEngine().setHardwareScalingLevel(this._currentScale);
  39. return this._currentScale >= this.maximumScale;
  40. };
  41. }
  42. export class ShadowsOptimization extends SceneOptimization {
  43. public apply = (scene: Scene): boolean => {
  44. scene.shadowsEnabled = false;
  45. return true;
  46. };
  47. }
  48. export class PostProcessesOptimization extends SceneOptimization {
  49. public apply = (scene: Scene): boolean => {
  50. scene.postProcessesEnabled = false;
  51. return true;
  52. };
  53. }
  54. export class LensFlaresOptimization extends SceneOptimization {
  55. public apply = (scene: Scene): boolean => {
  56. scene.lensFlaresEnabled = false;
  57. return true;
  58. };
  59. }
  60. export class ParticlesOptimization extends SceneOptimization {
  61. public apply = (scene: Scene): boolean => {
  62. scene.particlesEnabled = false;
  63. return true;
  64. };
  65. }
  66. export class RenderTargetsOptimization extends SceneOptimization {
  67. public apply = (scene: Scene): boolean => {
  68. scene.renderTargetsEnabled = false;
  69. return true;
  70. };
  71. }
  72. export class MergeMeshesOptimization extends SceneOptimization {
  73. static _UpdateSelectionTree = false;
  74. public static get UpdateSelectionTree(): boolean {
  75. return MergeMeshesOptimization._UpdateSelectionTree;
  76. }
  77. public static set UpdateSelectionTree(value: boolean) {
  78. MergeMeshesOptimization._UpdateSelectionTree = value;
  79. }
  80. private _canBeMerged = (abstractMesh: AbstractMesh): boolean => {
  81. if (!(abstractMesh instanceof Mesh)) {
  82. return false;
  83. }
  84. var mesh = <Mesh>abstractMesh;
  85. if (!mesh.isVisible || !mesh.isEnabled()) {
  86. return false;
  87. }
  88. if (mesh.instances.length > 0) {
  89. return false;
  90. }
  91. if (mesh.skeleton || mesh.hasLODLevels) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. public apply = (scene: Scene, updateSelectionTree?: boolean): boolean => {
  97. var globalPool = scene.meshes.slice(0);
  98. var globalLength = globalPool.length;
  99. for (var index = 0; index < globalLength; index++) {
  100. var currentPool = new Array<Mesh>();
  101. var current = globalPool[index];
  102. // Checks
  103. if (!this._canBeMerged(current)) {
  104. continue;
  105. }
  106. currentPool.push(<Mesh>current);
  107. // Find compatible meshes
  108. for (var subIndex = index + 1; subIndex < globalLength; subIndex++) {
  109. var otherMesh = globalPool[subIndex];
  110. if (!this._canBeMerged(otherMesh)) {
  111. continue;
  112. }
  113. if (otherMesh.material !== current.material) {
  114. continue;
  115. }
  116. if (otherMesh.checkCollisions !== current.checkCollisions) {
  117. continue;
  118. }
  119. currentPool.push(<Mesh>otherMesh);
  120. globalLength--;
  121. globalPool.splice(subIndex, 1);
  122. subIndex--;
  123. }
  124. if (currentPool.length < 2) {
  125. continue;
  126. }
  127. // Merge meshes
  128. Mesh.MergeMeshes(currentPool);
  129. }
  130. if (updateSelectionTree != undefined) {
  131. if (updateSelectionTree) {
  132. scene.createOrUpdateSelectionOctree();
  133. }
  134. }
  135. else if (MergeMeshesOptimization.UpdateSelectionTree) {
  136. scene.createOrUpdateSelectionOctree();
  137. }
  138. return true;
  139. };
  140. }
  141. // Options
  142. export class SceneOptimizerOptions {
  143. public optimizations = new Array<SceneOptimization>();
  144. constructor(public targetFrameRate: number = 60, public trackerDuration: number = 2000) {
  145. }
  146. public static LowDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions {
  147. var result = new SceneOptimizerOptions(targetFrameRate);
  148. var priority = 0;
  149. result.optimizations.push(new MergeMeshesOptimization(priority));
  150. result.optimizations.push(new ShadowsOptimization(priority));
  151. result.optimizations.push(new LensFlaresOptimization(priority));
  152. // Next priority
  153. priority++;
  154. result.optimizations.push(new PostProcessesOptimization(priority));
  155. result.optimizations.push(new ParticlesOptimization(priority));
  156. // Next priority
  157. priority++;
  158. result.optimizations.push(new TextureOptimization(priority, 1024));
  159. return result;
  160. }
  161. public static ModerateDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions {
  162. var result = new SceneOptimizerOptions(targetFrameRate);
  163. var priority = 0;
  164. result.optimizations.push(new MergeMeshesOptimization(priority));
  165. result.optimizations.push(new ShadowsOptimization(priority));
  166. result.optimizations.push(new LensFlaresOptimization(priority));
  167. // Next priority
  168. priority++;
  169. result.optimizations.push(new PostProcessesOptimization(priority));
  170. result.optimizations.push(new ParticlesOptimization(priority));
  171. // Next priority
  172. priority++;
  173. result.optimizations.push(new TextureOptimization(priority, 512));
  174. // Next priority
  175. priority++;
  176. result.optimizations.push(new RenderTargetsOptimization(priority));
  177. // Next priority
  178. priority++;
  179. result.optimizations.push(new HardwareScalingOptimization(priority, 2));
  180. return result;
  181. }
  182. public static HighDegradationAllowed(targetFrameRate?: number): SceneOptimizerOptions {
  183. var result = new SceneOptimizerOptions(targetFrameRate);
  184. var priority = 0;
  185. result.optimizations.push(new MergeMeshesOptimization(priority));
  186. result.optimizations.push(new ShadowsOptimization(priority));
  187. result.optimizations.push(new LensFlaresOptimization(priority));
  188. // Next priority
  189. priority++;
  190. result.optimizations.push(new PostProcessesOptimization(priority));
  191. result.optimizations.push(new ParticlesOptimization(priority));
  192. // Next priority
  193. priority++;
  194. result.optimizations.push(new TextureOptimization(priority, 256));
  195. // Next priority
  196. priority++;
  197. result.optimizations.push(new RenderTargetsOptimization(priority));
  198. // Next priority
  199. priority++;
  200. result.optimizations.push(new HardwareScalingOptimization(priority, 4));
  201. return result;
  202. }
  203. }
  204. // Scene optimizer tool
  205. export class SceneOptimizer {
  206. static _CheckCurrentState(scene: Scene, options: SceneOptimizerOptions, currentPriorityLevel: number, onSuccess?: () => void, onFailure?: () => void) {
  207. // TODO: add an epsilon
  208. if (scene.getEngine().getFps() >= options.targetFrameRate) {
  209. if (onSuccess) {
  210. onSuccess();
  211. }
  212. return;
  213. }
  214. // Apply current level of optimizations
  215. var allDone = true;
  216. var noOptimizationApplied = true;
  217. for (var index = 0; index < options.optimizations.length; index++) {
  218. var optimization = options.optimizations[index];
  219. if (optimization.priority === currentPriorityLevel) {
  220. noOptimizationApplied = false;
  221. allDone = allDone && optimization.apply(scene);
  222. }
  223. }
  224. // If no optimization was applied, this is a failure :(
  225. if (noOptimizationApplied) {
  226. if (onFailure) {
  227. onFailure();
  228. }
  229. return;
  230. }
  231. // If all optimizations were done, move to next level
  232. if (allDone) {
  233. currentPriorityLevel++;
  234. }
  235. // Let's the system running for a specific amount of time before checking FPS
  236. scene.executeWhenReady(() => {
  237. setTimeout(() => {
  238. SceneOptimizer._CheckCurrentState(scene, options, currentPriorityLevel, onSuccess, onFailure);
  239. }, options.trackerDuration);
  240. });
  241. }
  242. public static OptimizeAsync(scene: Scene, options?: SceneOptimizerOptions, onSuccess?: () => void, onFailure?: () => void): void {
  243. if (!options) {
  244. options = SceneOptimizerOptions.ModerateDegradationAllowed();
  245. }
  246. // Let's the system running for a specific amount of time before checking FPS
  247. scene.executeWhenReady(() => {
  248. setTimeout(() => {
  249. SceneOptimizer._CheckCurrentState(scene, options, 0, onSuccess, onFailure);
  250. }, options.trackerDuration);
  251. });
  252. }
  253. }
  254. }