babylon.proceduralTextureSceneComponent.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. module BABYLON {
  2. export interface AbstractScene {
  3. /**
  4. * The list of procedural textures added to the scene
  5. * @see http://doc.babylonjs.com/how_to/how_to_use_procedural_textures
  6. */
  7. proceduralTextures: Array<ProceduralTexture>;
  8. }
  9. /**
  10. * Defines the Procedural Texture scene component responsible to manage any Procedural Texture
  11. * in a given scene.
  12. */
  13. export class ProceduralTextureSceneComponent implements ISceneComponent {
  14. /**
  15. * The component name helpfull to identify the component in the list of scene components.
  16. */
  17. public readonly name = SceneComponentConstants.NAME_PROCEDURALTEXTURE;
  18. /**
  19. * The scene the component belongs to.
  20. */
  21. public scene: Scene;
  22. /**
  23. * Creates a new instance of the component for the given scene
  24. * @param scene Defines the scene to register the component in
  25. */
  26. constructor(scene: Scene) {
  27. this.scene = scene;
  28. this.scene.proceduralTextures = new Array<ProceduralTexture>();
  29. scene.layers = new Array<Layer>();
  30. }
  31. /**
  32. * Registers the component in a given scene
  33. */
  34. public register(): void {
  35. this.scene._beforeClearStage.registerStep(SceneComponentConstants.STEP_BEFORECLEAR_PROCEDURALTEXTURE, this, this._beforeClear);
  36. }
  37. /**
  38. * Rebuilds the elements related to this component in case of
  39. * context lost for instance.
  40. */
  41. public rebuild(): void {
  42. // Nothing to do here.
  43. }
  44. /**
  45. * Disposes the component and the associated ressources.
  46. */
  47. public dispose(): void {
  48. // Nothing to do here.
  49. }
  50. private _beforeClear(): void {
  51. if (this.scene.proceduralTexturesEnabled) {
  52. Tools.StartPerformanceCounter("Procedural textures", this.scene.proceduralTextures.length > 0);
  53. for (var proceduralIndex = 0; proceduralIndex < this.scene.proceduralTextures.length; proceduralIndex++) {
  54. var proceduralTexture = this.scene.proceduralTextures[proceduralIndex];
  55. if (proceduralTexture._shouldRender()) {
  56. proceduralTexture.render();
  57. }
  58. }
  59. Tools.EndPerformanceCounter("Procedural textures", this.scene.proceduralTextures.length > 0);
  60. }
  61. }
  62. }
  63. }