babylon.lensFlareSystem.js 7.1 KB

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