babylon.lensFlareSystem.js 8.0 KB

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