babylon.layer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Layer = function (name, imgUrl, scene, isBackground, color) {
  5. this.name = name;
  6. this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
  7. this.isBackground = isBackground === undefined ? true : isBackground;
  8. this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
  9. this._scene = scene;
  10. this._scene.layers.push(this);
  11. // VBO
  12. var vertices = [];
  13. vertices.push(1, 1);
  14. vertices.push(-1, 1);
  15. vertices.push(-1, -1);
  16. vertices.push(1, -1);
  17. this._vertexDeclaration = [2];
  18. this._vertexStrideSize = 2 * 4;
  19. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  20. // Indices
  21. var indices = [];
  22. indices.push(0);
  23. indices.push(1);
  24. indices.push(2);
  25. indices.push(0);
  26. indices.push(2);
  27. indices.push(3);
  28. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  29. // Effects
  30. this._effect = this._scene.getEngine().createEffect("layer",
  31. ["position"],
  32. ["textureMatrix", "color"],
  33. ["textureSampler"], "");
  34. };
  35. // Members
  36. BABYLON.Layer.prototype.onDispose = null;
  37. // Methods
  38. BABYLON.Layer.prototype.render = function () {
  39. // Check
  40. if (!this._effect.isReady() || !this.texture || !this.texture.isReady())
  41. return;
  42. var engine = this._scene.getEngine();
  43. // Render
  44. engine.enableEffect(this._effect);
  45. engine.setState(false);
  46. // Texture
  47. this._effect.setTexture("textureSampler", this.texture);
  48. this._effect.setMatrix("textureMatrix", this.texture._computeTextureMatrix());
  49. // Color
  50. this._effect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  51. // VBOs
  52. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  53. // Draw order
  54. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  55. engine.draw(true, 0, 6);
  56. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  57. };
  58. BABYLON.Layer.prototype.dispose = function () {
  59. if (this._vertexBuffer) {
  60. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  61. this._vertexBuffer = null;
  62. }
  63. if (this._indexBuffer) {
  64. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  65. this._indexBuffer = null;
  66. }
  67. if (this.texture) {
  68. this.texture.dispose();
  69. this.texture = null;
  70. }
  71. // Remove from scene
  72. var index = this._scene.layers.indexOf(this);
  73. this._scene.layers.splice(index, 1);
  74. // Callback
  75. if (this.onDispose) {
  76. this.onDispose();
  77. }
  78. };
  79. })();