babylon.lensFlareSystem.js 7.6 KB

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