babylon.lensFlareSystem.js 7.4 KB

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