babylon.lensFlareSystem.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. module BABYLON {
  2. export class LensFlareSystem {
  3. public lensFlares = new Array<LensFlare>();
  4. public borderLimit = 300;
  5. public meshesSelectionPredicate: (mesh: Mesh) => boolean;
  6. private _scene: Scene;
  7. private _emitter: any;
  8. private _vertexDeclaration = [2];
  9. private _vertexStrideSize = 2 * 4;
  10. private _vertexBuffer: WebGLBuffer;
  11. private _indexBuffer: WebGLBuffer;
  12. private _effect: Effect;
  13. private _positionX: number;
  14. private _positionY: number;
  15. constructor(public name: string, emitter: any, scene: Scene) {
  16. this._scene = scene;
  17. this._emitter = emitter;
  18. scene.lensFlareSystems.push(this);
  19. this.meshesSelectionPredicate = m => m.material && m.isVisible && m.isEnabled() && m.checkCollisions;
  20. // VBO
  21. var vertices = [];
  22. vertices.push(1, 1);
  23. vertices.push(-1, 1);
  24. vertices.push(-1, -1);
  25. vertices.push(1, -1);
  26. this._vertexBuffer = scene.getEngine().createVertexBuffer(vertices);
  27. // Indices
  28. var indices = [];
  29. indices.push(0);
  30. indices.push(1);
  31. indices.push(2);
  32. indices.push(0);
  33. indices.push(2);
  34. indices.push(3);
  35. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  36. // Effects
  37. this._effect = this._scene.getEngine().createEffect("lensFlare",
  38. ["position"],
  39. ["color", "viewportMatrix"],
  40. ["textureSampler"], "");
  41. }
  42. public getScene(): Scene {
  43. return this._scene;
  44. }
  45. public getEmitter(): any {
  46. return this._emitter;
  47. }
  48. public getEmitterPosition(): Vector3 {
  49. return this._emitter.getAbsolutePosition ? this._emitter.getAbsolutePosition() : this._emitter.position;
  50. }
  51. public computeEffectivePosition(globalViewport: Viewport): boolean {
  52. var position = this.getEmitterPosition();
  53. position = BABYLON.Vector3.Project(position, BABYLON.Matrix.Identity(), this._scene.getTransformMatrix(), globalViewport);
  54. this._positionX = position.x;
  55. this._positionY = position.y;
  56. position = BABYLON.Vector3.TransformCoordinates(this.getEmitterPosition(), this._scene.getViewMatrix());
  57. if (position.z > 0) {
  58. if ((this._positionX > globalViewport.x) && (this._positionX < globalViewport.x + globalViewport.width)) {
  59. if ((this._positionY > globalViewport.y) && (this._positionY < globalViewport.y + globalViewport.height))
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. public _isVisible(): boolean {
  66. var emitterPosition = this.getEmitterPosition();
  67. var direction = emitterPosition.subtract(this._scene.activeCamera.position);
  68. var distance = direction.length();
  69. direction.normalize();
  70. var ray = new BABYLON.Ray(this._scene.activeCamera.position, direction);
  71. var pickInfo = this._scene.pickWithRay(ray, this.meshesSelectionPredicate, true);
  72. return !pickInfo.hit || pickInfo.distance > distance;
  73. }
  74. public render(): boolean {
  75. if (!this._effect.isReady())
  76. return false;
  77. var engine = this._scene.getEngine();
  78. var viewport = this._scene.activeCamera.viewport;
  79. var globalViewport = viewport.toGlobal(engine);
  80. // Position
  81. if (!this.computeEffectivePosition(globalViewport)) {
  82. return false;
  83. }
  84. // Visibility
  85. if (!this._isVisible()) {
  86. return false;
  87. }
  88. // Intensity
  89. var awayX;
  90. var awayY;
  91. if (this._positionX < this.borderLimit + globalViewport.x) {
  92. awayX = this.borderLimit + globalViewport.x - this._positionX;
  93. } else if (this._positionX > globalViewport.x + globalViewport.width - this.borderLimit) {
  94. awayX = this._positionX - globalViewport.x - globalViewport.width + this.borderLimit;
  95. } else {
  96. awayX = 0;
  97. }
  98. if (this._positionY < this.borderLimit + globalViewport.y) {
  99. awayY = this.borderLimit + globalViewport.y - this._positionY;
  100. } else if (this._positionY > globalViewport.y + globalViewport.height - this.borderLimit) {
  101. awayY = this._positionY - globalViewport.y - globalViewport.height + this.borderLimit;
  102. } else {
  103. awayY = 0;
  104. }
  105. var away = (awayX > awayY) ? awayX : awayY;
  106. if (away > this.borderLimit) {
  107. away = this.borderLimit;
  108. }
  109. var intensity = 1.0 - (away / this.borderLimit);
  110. if (intensity < 0) {
  111. return false;
  112. }
  113. if (intensity > 1.0) {
  114. intensity = 1.0;
  115. }
  116. // Position
  117. var centerX = globalViewport.x + globalViewport.width / 2;
  118. var centerY = globalViewport.y + globalViewport.height / 2;
  119. var distX = centerX - this._positionX;
  120. var distY = centerY - this._positionY;
  121. // Effects
  122. engine.enableEffect(this._effect);
  123. engine.setState(false);
  124. engine.setDepthBuffer(false);
  125. engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD);
  126. // VBOs
  127. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  128. // Flares
  129. for (var index = 0; index < this.lensFlares.length; index++) {
  130. var flare = this.lensFlares[index];
  131. var x = centerX - (distX * flare.position);
  132. var y = centerY - (distY * flare.position);
  133. var cw = flare.size;
  134. var ch = flare.size * engine.getAspectRatio(this._scene.activeCamera);
  135. var cx = 2 * (x / globalViewport.width) - 1.0;
  136. var cy = 1.0 - 2 * (y / globalViewport.height);
  137. var viewportMatrix = BABYLON.Matrix.FromValues(
  138. cw / 2, 0, 0, 0,
  139. 0, ch / 2, 0, 0,
  140. 0, 0, 1, 0,
  141. cx, cy, 0, 1);
  142. this._effect.setMatrix("viewportMatrix", viewportMatrix);
  143. // Texture
  144. this._effect.setTexture("textureSampler", flare.texture);
  145. // Color
  146. this._effect.setFloat4("color", flare.color.r * intensity, flare.color.g * intensity, flare.color.b * intensity, 1.0);
  147. // Draw order
  148. engine.draw(true, 0, 6);
  149. }
  150. engine.setDepthBuffer(true);
  151. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  152. return true;
  153. }
  154. public dispose(): void {
  155. if (this._vertexBuffer) {
  156. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  157. this._vertexBuffer = null;
  158. }
  159. if (this._indexBuffer) {
  160. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  161. this._indexBuffer = null;
  162. }
  163. while (this.lensFlares.length) {
  164. this.lensFlares[0].dispose();
  165. }
  166. // Remove from scene
  167. var index = this._scene.lensFlareSystems.indexOf(this);
  168. this._scene.lensFlareSystems.splice(index, 1);
  169. }
  170. }
  171. }