babylon.layer.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Layer = (function () {
  4. function Layer(name, imgUrl, scene, isBackground, color) {
  5. this.name = name;
  6. this.alphaBlendingMode = BABYLON.Engine.ALPHA_COMBINE;
  7. this._vertexDeclaration = [2];
  8. this._vertexStrideSize = 2 * 4;
  9. this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
  10. this.isBackground = isBackground === undefined ? true : isBackground;
  11. this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
  12. this._scene = scene;
  13. this._scene.layers.push(this);
  14. // VBO
  15. var vertices = [];
  16. vertices.push(1, 1);
  17. vertices.push(-1, 1);
  18. vertices.push(-1, -1);
  19. vertices.push(1, -1);
  20. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  21. // Indices
  22. var indices = [];
  23. indices.push(0);
  24. indices.push(1);
  25. indices.push(2);
  26. indices.push(0);
  27. indices.push(2);
  28. indices.push(3);
  29. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  30. // Effects
  31. this._effect = this._scene.getEngine().createEffect("layer", ["position"], ["textureMatrix", "color"], ["textureSampler"], "");
  32. }
  33. Layer.prototype.render = function () {
  34. // Check
  35. if (!this._effect.isReady() || !this.texture || !this.texture.isReady())
  36. return;
  37. var engine = this._scene.getEngine();
  38. // Render
  39. engine.enableEffect(this._effect);
  40. engine.setState(false);
  41. // Texture
  42. this._effect.setTexture("textureSampler", this.texture);
  43. this._effect.setMatrix("textureMatrix", this.texture.getTextureMatrix());
  44. // Color
  45. this._effect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  46. // VBOs
  47. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  48. // Draw order
  49. engine.setAlphaMode(this.alphaBlendingMode);
  50. engine.draw(true, 0, 6);
  51. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  52. };
  53. 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. return Layer;
  75. })();
  76. BABYLON.Layer = Layer;
  77. })(BABYLON || (BABYLON = {}));