babylon.lensFlareSystem.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.checkCollisions && ((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 getEmitterPosition(): Vector3 {
  56. return this._emitter.getAbsolutePosition ? this._emitter.getAbsolutePosition() : this._emitter.position;
  57. }
  58. public computeEffectivePosition(globalViewport: Viewport): boolean {
  59. var position = this.getEmitterPosition();
  60. position = BABYLON.Vector3.Project(position, BABYLON.Matrix.Identity(), this._scene.getTransformMatrix(), globalViewport);
  61. this._positionX = position.x;
  62. this._positionY = position.y;
  63. position = BABYLON.Vector3.TransformCoordinates(this.getEmitterPosition(), this._scene.getViewMatrix());
  64. if (position.z > 0) {
  65. if ((this._positionX > globalViewport.x) && (this._positionX < globalViewport.x + globalViewport.width)) {
  66. if ((this._positionY > globalViewport.y) && (this._positionY < globalViewport.y + globalViewport.height))
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. public _isVisible(): boolean {
  73. if (!this._isEnabled) {
  74. return false;
  75. }
  76. var emitterPosition = this.getEmitterPosition();
  77. var direction = emitterPosition.subtract(this._scene.activeCamera.position);
  78. var distance = direction.length();
  79. direction.normalize();
  80. var ray = new BABYLON.Ray(this._scene.activeCamera.position, direction);
  81. var pickInfo = this._scene.pickWithRay(ray, this.meshesSelectionPredicate, true);
  82. return !pickInfo.hit || pickInfo.distance > distance;
  83. }
  84. public render(): boolean {
  85. if (!this._effect.isReady())
  86. return false;
  87. var engine = this._scene.getEngine();
  88. var viewport = this._scene.activeCamera.viewport;
  89. var globalViewport = viewport.toGlobal(engine);
  90. // Position
  91. if (!this.computeEffectivePosition(globalViewport)) {
  92. return false;
  93. }
  94. // Visibility
  95. if (!this._isVisible()) {
  96. return false;
  97. }
  98. // Intensity
  99. var awayX;
  100. var awayY;
  101. if (this._positionX < this.borderLimit + globalViewport.x) {
  102. awayX = this.borderLimit + globalViewport.x - this._positionX;
  103. } else if (this._positionX > globalViewport.x + globalViewport.width - this.borderLimit) {
  104. awayX = this._positionX - globalViewport.x - globalViewport.width + this.borderLimit;
  105. } else {
  106. awayX = 0;
  107. }
  108. if (this._positionY < this.borderLimit + globalViewport.y) {
  109. awayY = this.borderLimit + globalViewport.y - this._positionY;
  110. } else if (this._positionY > globalViewport.y + globalViewport.height - this.borderLimit) {
  111. awayY = this._positionY - globalViewport.y - globalViewport.height + this.borderLimit;
  112. } else {
  113. awayY = 0;
  114. }
  115. var away = (awayX > awayY) ? awayX : awayY;
  116. if (away > this.borderLimit) {
  117. away = this.borderLimit;
  118. }
  119. var intensity = 1.0 - (away / this.borderLimit);
  120. if (intensity < 0) {
  121. return false;
  122. }
  123. if (intensity > 1.0) {
  124. intensity = 1.0;
  125. }
  126. // Position
  127. var centerX = globalViewport.x + globalViewport.width / 2;
  128. var centerY = globalViewport.y + globalViewport.height / 2;
  129. var distX = centerX - this._positionX;
  130. var distY = centerY - this._positionY;
  131. // Effects
  132. engine.enableEffect(this._effect);
  133. engine.setState(false);
  134. engine.setDepthBuffer(false);
  135. engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD);
  136. // VBOs
  137. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  138. // Flares
  139. for (var index = 0; index < this.lensFlares.length; index++) {
  140. var flare = this.lensFlares[index];
  141. var x = centerX - (distX * flare.position);
  142. var y = centerY - (distY * flare.position);
  143. var cw = flare.size;
  144. var ch = flare.size * engine.getAspectRatio(this._scene.activeCamera);
  145. var cx = 2 * (x / globalViewport.width) - 1.0;
  146. var cy = 1.0 - 2 * (y / globalViewport.height);
  147. var viewportMatrix = BABYLON.Matrix.FromValues(
  148. cw / 2, 0, 0, 0,
  149. 0, ch / 2, 0, 0,
  150. 0, 0, 1, 0,
  151. cx, cy, 0, 1);
  152. this._effect.setMatrix("viewportMatrix", viewportMatrix);
  153. // Texture
  154. this._effect.setTexture("textureSampler", flare.texture);
  155. // Color
  156. this._effect.setFloat4("color", flare.color.r * intensity, flare.color.g * intensity, flare.color.b * intensity, 1.0);
  157. // Draw order
  158. engine.draw(true, 0, 6);
  159. }
  160. engine.setDepthBuffer(true);
  161. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  162. return true;
  163. }
  164. public dispose(): void {
  165. if (this._vertexBuffer) {
  166. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  167. this._vertexBuffer = null;
  168. }
  169. if (this._indexBuffer) {
  170. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  171. this._indexBuffer = null;
  172. }
  173. while (this.lensFlares.length) {
  174. this.lensFlares[0].dispose();
  175. }
  176. // Remove from scene
  177. var index = this._scene.lensFlareSystems.indexOf(this);
  178. this._scene.lensFlareSystems.splice(index, 1);
  179. }
  180. }
  181. }