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._vertexDeclaration = [2];
  7. this._vertexStrideSize = 2 * 4;
  8. this.texture = imgUrl ? new BABYLON.Texture(imgUrl, scene, true) : null;
  9. this.isBackground = isBackground === undefined ? true : isBackground;
  10. this.color = color === undefined ? new BABYLON.Color4(1, 1, 1, 1) : color;
  11. this._scene = scene;
  12. this._scene.layers.push(this);
  13. // VBO
  14. var vertices = [];
  15. vertices.push(1, 1);
  16. vertices.push(-1, 1);
  17. vertices.push(-1, -1);
  18. vertices.push(1, -1);
  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", ["position"], ["textureMatrix", "color"], ["textureSampler"], "");
  31. }
  32. Layer.prototype.render = function () {
  33. // Check
  34. if (!this._effect.isReady() || !this.texture || !this.texture.isReady())
  35. return;
  36. var engine = this._scene.getEngine();
  37. // Render
  38. engine.enableEffect(this._effect);
  39. engine.setState(false);
  40. // Texture
  41. this._effect.setTexture("textureSampler", this.texture);
  42. this._effect.setMatrix("textureMatrix", this.texture.getTextureMatrix());
  43. // Color
  44. this._effect.setFloat4("color", this.color.r, this.color.g, this.color.b, this.color.a);
  45. // VBOs
  46. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  47. // Draw order
  48. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  49. engine.draw(true, 0, 6);
  50. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  51. };
  52. Layer.prototype.dispose = function () {
  53. if (this._vertexBuffer) {
  54. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  55. this._vertexBuffer = null;
  56. }
  57. if (this._indexBuffer) {
  58. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  59. this._indexBuffer = null;
  60. }
  61. if (this.texture) {
  62. this.texture.dispose();
  63. this.texture = null;
  64. }
  65. // Remove from scene
  66. var index = this._scene.layers.indexOf(this);
  67. this._scene.layers.splice(index, 1);
  68. // Callback
  69. if (this.onDispose) {
  70. this.onDispose();
  71. }
  72. };
  73. return Layer;
  74. })();
  75. BABYLON.Layer = Layer;
  76. })(BABYLON || (BABYLON = {}));
  77. //# sourceMappingURL=babylon.layer.js.map