babylon.layer.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }
  35. Layer.prototype.render = function () {
  36. // Check
  37. if (!this._effect.isReady() || !this.texture || !this.texture.isReady())
  38. return;
  39. var engine = this._scene.getEngine();
  40. // Render
  41. engine.enableEffect(this._effect);
  42. engine.setState(false);
  43. // Texture
  44. this._effect.setTexture("textureSampler", this.texture);
  45. this._effect.setMatrix("textureMatrix", this.texture.getTextureMatrix());
  46. // Color
  47. this._effect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  48. // Scale / offset
  49. this._effect.setVector2("offset", this.offset);
  50. this._effect.setVector2("scale", this.scale);
  51. // VBOs
  52. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  53. // Draw order
  54. engine.setAlphaMode(this.alphaBlendingMode);
  55. engine.draw(true, 0, 6);
  56. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  57. };
  58. 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. return Layer;
  80. })();
  81. BABYLON.Layer = Layer;
  82. })(BABYLON || (BABYLON = {}));
  83. //# sourceMappingURL=babylon.layer.js.map