babylon.layer.js 2.8 KB

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