babylon.layer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Layer = (function () {
  4. function Layer(name, imgUrl, scene, isBackground, color) {
  5. this.name = name;
  6. this.scale = new BABYLON.Vector2(1, 1);
  7. this.offset = new BABYLON.Vector2(0, 0);
  8. this.alphaBlendingMode = BABYLON.Engine.ALPHA_COMBINE;
  9. this._vertexDeclaration = [2];
  10. this._vertexStrideSize = 2 * 4;
  11. // Events
  12. /**
  13. * An event triggered when the layer is disposed.
  14. * @type {BABYLON.Observable}
  15. */
  16. this.onDisposeObservable = new BABYLON.Observable();
  17. /**
  18. * An event triggered before rendering the scene
  19. * @type {BABYLON.Observable}
  20. */
  21. this.onBeforeRenderObservable = new BABYLON.Observable();
  22. /**
  23. * An event triggered after rendering the scene
  24. * @type {BABYLON.Observable}
  25. */
  26. this.onAfterRenderObservable = new BABYLON.Observable();
  27. this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
  28. this.isBackground = isBackground === undefined ? true : isBackground;
  29. this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
  30. this._scene = scene;
  31. this._scene.layers.push(this);
  32. // VBO
  33. var vertices = [];
  34. vertices.push(1, 1);
  35. vertices.push(-1, 1);
  36. vertices.push(-1, -1);
  37. vertices.push(1, -1);
  38. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  39. // Indices
  40. var indices = [];
  41. indices.push(0);
  42. indices.push(1);
  43. indices.push(2);
  44. indices.push(0);
  45. indices.push(2);
  46. indices.push(3);
  47. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  48. // Effects
  49. this._effect = this._scene.getEngine().createEffect("layer", ["position"], ["textureMatrix", "color", "scale", "offset"], ["textureSampler"], "");
  50. this._alphaTestEffect = this._scene.getEngine().createEffect("layer", ["position"], ["textureMatrix", "color", "scale", "offset"], ["textureSampler"], "#define ALPHATEST");
  51. }
  52. Object.defineProperty(Layer.prototype, "onDispose", {
  53. set: function (callback) {
  54. if (this._onDisposeObserver) {
  55. this.onDisposeObservable.remove(this._onDisposeObserver);
  56. }
  57. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  58. },
  59. enumerable: true,
  60. configurable: true
  61. });
  62. Object.defineProperty(Layer.prototype, "onBeforeRender", {
  63. set: function (callback) {
  64. if (this._onBeforeRenderObserver) {
  65. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  66. }
  67. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  68. },
  69. enumerable: true,
  70. configurable: true
  71. });
  72. Object.defineProperty(Layer.prototype, "onAfterRender", {
  73. set: function (callback) {
  74. if (this._onAfterRenderObserver) {
  75. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  76. }
  77. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82. Layer.prototype.render = function () {
  83. var currentEffect = this.alphaTest ? this._alphaTestEffect : this._effect;
  84. // Check
  85. if (!currentEffect.isReady() || !this.texture || !this.texture.isReady())
  86. return;
  87. var engine = this._scene.getEngine();
  88. this.onBeforeRenderObservable.notifyObservers(this);
  89. // Render
  90. engine.enableEffect(currentEffect);
  91. engine.setState(false);
  92. // Texture
  93. currentEffect.setTexture("textureSampler", this.texture);
  94. currentEffect.setMatrix("textureMatrix", this.texture.getTextureMatrix());
  95. // Color
  96. currentEffect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  97. // Scale / offset
  98. currentEffect.setVector2("offset", this.offset);
  99. currentEffect.setVector2("scale", this.scale);
  100. // VBOs
  101. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, currentEffect);
  102. // Draw order
  103. if (!this._alphaTestEffect) {
  104. engine.setAlphaMode(this.alphaBlendingMode);
  105. engine.draw(true, 0, 6);
  106. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  107. }
  108. else {
  109. engine.draw(true, 0, 6);
  110. }
  111. this.onAfterRenderObservable.notifyObservers(this);
  112. };
  113. Layer.prototype.dispose = function () {
  114. if (this._vertexBuffer) {
  115. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  116. this._vertexBuffer = null;
  117. }
  118. if (this._indexBuffer) {
  119. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  120. this._indexBuffer = null;
  121. }
  122. if (this.texture) {
  123. this.texture.dispose();
  124. this.texture = null;
  125. }
  126. // Remove from scene
  127. var index = this._scene.layers.indexOf(this);
  128. this._scene.layers.splice(index, 1);
  129. // Callback
  130. this.onDisposeObservable.notifyObservers(this);
  131. this.onDisposeObservable.clear();
  132. this.onAfterRenderObservable.clear();
  133. this.onBeforeRenderObservable.clear();
  134. };
  135. return Layer;
  136. }());
  137. BABYLON.Layer = Layer;
  138. })(BABYLON || (BABYLON = {}));