babylon.layer.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
  12. this.isBackground = isBackground === undefined ? true : isBackground;
  13. this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
  14. this._scene = scene;
  15. this._scene.layers.push(this);
  16. // VBO
  17. var vertices = [];
  18. vertices.push(1, 1);
  19. vertices.push(-1, 1);
  20. vertices.push(-1, -1);
  21. vertices.push(1, -1);
  22. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  23. // Indices
  24. var indices = [];
  25. indices.push(0);
  26. indices.push(1);
  27. indices.push(2);
  28. indices.push(0);
  29. indices.push(2);
  30. indices.push(3);
  31. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  32. // Effects
  33. this._effect = this._scene.getEngine().createEffect("layer", ["position"], ["textureMatrix", "color", "scale", "offset"], ["textureSampler"], "");
  34. this._alphaTestEffect = this._scene.getEngine().createEffect("layer", ["position"], ["textureMatrix", "color", "scale", "offset"], ["textureSampler"], "#define ALPHATEST");
  35. }
  36. Layer.prototype.render = function () {
  37. var currentEffect = this.alphaTest ? this._alphaTestEffect : this._effect;
  38. // Check
  39. if (!currentEffect.isReady() || !this.texture || !this.texture.isReady())
  40. return;
  41. var engine = this._scene.getEngine();
  42. if (this.onBeforeRender) {
  43. this.onBeforeRender();
  44. }
  45. // Render
  46. engine.enableEffect(currentEffect);
  47. engine.setState(false);
  48. // Texture
  49. currentEffect.setTexture("textureSampler", this.texture);
  50. currentEffect.setMatrix("textureMatrix", this.texture.getTextureMatrix());
  51. // Color
  52. currentEffect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  53. // Scale / offset
  54. currentEffect.setVector2("offset", this.offset);
  55. currentEffect.setVector2("scale", this.scale);
  56. // VBOs
  57. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, currentEffect);
  58. // Draw order
  59. if (!this._alphaTestEffect) {
  60. engine.setAlphaMode(this.alphaBlendingMode);
  61. engine.draw(true, 0, 6);
  62. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  63. }
  64. else {
  65. engine.draw(true, 0, 6);
  66. }
  67. if (this.onAfterRender) {
  68. this.onAfterRender();
  69. }
  70. };
  71. Layer.prototype.dispose = function () {
  72. if (this._vertexBuffer) {
  73. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  74. this._vertexBuffer = null;
  75. }
  76. if (this._indexBuffer) {
  77. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  78. this._indexBuffer = null;
  79. }
  80. if (this.texture) {
  81. this.texture.dispose();
  82. this.texture = null;
  83. }
  84. // Remove from scene
  85. var index = this._scene.layers.indexOf(this);
  86. this._scene.layers.splice(index, 1);
  87. // Callback
  88. if (this.onDispose) {
  89. this.onDispose();
  90. }
  91. };
  92. return Layer;
  93. }());
  94. BABYLON.Layer = Layer;
  95. })(BABYLON || (BABYLON = {}));