babylon.lensFlareSystem.js 7.1 KB

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