babylon.lensFlareSystem.ts 8.0 KB

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