babylon.lensFlareSystem.js 8.0 KB

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