babylon.layer.ts 3.3 KB

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