meshSimplificationSceneComponent.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Scene } from "../scene";
  2. import { Mesh } from "./mesh";
  3. import { SimplificationQueue, ISimplificationSettings, SimplificationType } from "./meshSimplification";
  4. import { SceneComponentConstants, ISceneComponent } from "../sceneComponent";
  5. declare module "../scene" {
  6. export interface Scene {
  7. /** @hidden (Backing field) */
  8. _simplificationQueue: SimplificationQueue;
  9. /**
  10. * Gets or sets the simplification queue attached to the scene
  11. * @see http://doc.babylonjs.com/how_to/in-browser_mesh_simplification
  12. */
  13. simplificationQueue: SimplificationQueue;
  14. }
  15. }
  16. Object.defineProperty(Scene.prototype, "simplificationQueue", {
  17. get: function(this: Scene) {
  18. if (!this._simplificationQueue) {
  19. this._simplificationQueue = new SimplificationQueue();
  20. let component = this._getComponent(SceneComponentConstants.NAME_SIMPLIFICATIONQUEUE) as SimplicationQueueSceneComponent;
  21. if (!component) {
  22. component = new SimplicationQueueSceneComponent(this);
  23. this._addComponent(component);
  24. }
  25. }
  26. return this._simplificationQueue;
  27. },
  28. set: function(this: Scene, value: SimplificationQueue) {
  29. this._simplificationQueue = value;
  30. },
  31. enumerable: true,
  32. configurable: true
  33. });
  34. declare module "../Meshes/mesh" {
  35. export interface Mesh {
  36. /**
  37. * Simplify the mesh according to the given array of settings.
  38. * Function will return immediately and will simplify async
  39. * @param settings a collection of simplification settings
  40. * @param parallelProcessing should all levels calculate parallel or one after the other
  41. * @param simplificationType the type of simplification to run
  42. * @param successCallback optional success callback to be called after the simplification finished processing all settings
  43. * @returns the current mesh
  44. */
  45. simplify(settings: Array<ISimplificationSettings>, parallelProcessing?: boolean, simplificationType?: SimplificationType, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh;
  46. }
  47. }
  48. Mesh.prototype.simplify = function(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, simplificationType: SimplificationType = SimplificationType.QUADRATIC, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh {
  49. this.getScene().simplificationQueue.addTask({
  50. settings: settings,
  51. parallelProcessing: parallelProcessing,
  52. mesh: this,
  53. simplificationType: simplificationType,
  54. successCallback: successCallback
  55. });
  56. return this;
  57. };
  58. /**
  59. * Defines the simplification queue scene component responsible to help scheduling the various simplification task
  60. * created in a scene
  61. */
  62. export class SimplicationQueueSceneComponent implements ISceneComponent {
  63. /**
  64. * The component name helpfull to identify the component in the list of scene components.
  65. */
  66. public readonly name = SceneComponentConstants.NAME_SIMPLIFICATIONQUEUE;
  67. /**
  68. * The scene the component belongs to.
  69. */
  70. public scene: Scene;
  71. /**
  72. * Creates a new instance of the component for the given scene
  73. * @param scene Defines the scene to register the component in
  74. */
  75. constructor(scene: Scene) {
  76. this.scene = scene;
  77. }
  78. /**
  79. * Registers the component in a given scene
  80. */
  81. public register(): void {
  82. this.scene._beforeCameraUpdateStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERAUPDATE_SIMPLIFICATIONQUEUE, this, this._beforeCameraUpdate);
  83. }
  84. /**
  85. * Rebuilds the elements related to this component in case of
  86. * context lost for instance.
  87. */
  88. public rebuild(): void {
  89. // Nothing to do for this component
  90. }
  91. /**
  92. * Disposes the component and the associated ressources
  93. */
  94. public dispose(): void {
  95. // Nothing to do for this component
  96. }
  97. private _beforeCameraUpdate(): void {
  98. if (this.scene._simplificationQueue && !this.scene._simplificationQueue.running) {
  99. this.scene._simplificationQueue.executeNext();
  100. }
  101. }
  102. }