babylon.lensFlareSystem.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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.viewportBorder = 0;
  9. this.layerMask = 0x0FFFFFFF;
  10. this._vertexBuffers = {};
  11. this._isEnabled = true;
  12. this._scene = scene;
  13. this._emitter = emitter;
  14. this.id = name;
  15. scene.lensFlareSystems.push(this);
  16. this.meshesSelectionPredicate = function (m) { return m.material && m.isVisible && m.isEnabled() && m.isBlocker && ((m.layerMask & scene.activeCamera.layerMask) != 0); };
  17. var engine = scene.getEngine();
  18. // VBO
  19. var vertices = [];
  20. vertices.push(1, 1);
  21. vertices.push(-1, 1);
  22. vertices.push(-1, -1);
  23. vertices.push(1, -1);
  24. this._vertexBuffers[BABYLON.VertexBuffer.PositionKind] = new BABYLON.VertexBuffer(engine, vertices, BABYLON.VertexBuffer.PositionKind, false, false, 2);
  25. // Indices
  26. var indices = [];
  27. indices.push(0);
  28. indices.push(1);
  29. indices.push(2);
  30. indices.push(0);
  31. indices.push(2);
  32. indices.push(3);
  33. this._indexBuffer = engine.createIndexBuffer(indices);
  34. // Effects
  35. this._effect = engine.createEffect("lensFlare", [BABYLON.VertexBuffer.PositionKind], ["color", "viewportMatrix"], ["textureSampler"], "");
  36. }
  37. Object.defineProperty(LensFlareSystem.prototype, "isEnabled", {
  38. get: function () {
  39. return this._isEnabled;
  40. },
  41. set: function (value) {
  42. this._isEnabled = value;
  43. },
  44. enumerable: true,
  45. configurable: true
  46. });
  47. LensFlareSystem.prototype.getScene = function () {
  48. return this._scene;
  49. };
  50. LensFlareSystem.prototype.getEmitter = function () {
  51. return this._emitter;
  52. };
  53. LensFlareSystem.prototype.setEmitter = function (newEmitter) {
  54. this._emitter = newEmitter;
  55. };
  56. LensFlareSystem.prototype.getEmitterPosition = function () {
  57. return this._emitter.getAbsolutePosition ? this._emitter.getAbsolutePosition() : this._emitter.position;
  58. };
  59. LensFlareSystem.prototype.computeEffectivePosition = function (globalViewport) {
  60. var position = this.getEmitterPosition();
  61. position = BABYLON.Vector3.Project(position, BABYLON.Matrix.Identity(), this._scene.getTransformMatrix(), globalViewport);
  62. this._positionX = position.x;
  63. this._positionY = position.y;
  64. position = BABYLON.Vector3.TransformCoordinates(this.getEmitterPosition(), this._scene.getViewMatrix());
  65. if (this.viewportBorder > 0) {
  66. globalViewport.x -= this.viewportBorder;
  67. globalViewport.y -= this.viewportBorder;
  68. globalViewport.width += this.viewportBorder * 2;
  69. globalViewport.height += this.viewportBorder * 2;
  70. position.x += this.viewportBorder;
  71. position.y += this.viewportBorder;
  72. this._positionX += this.viewportBorder;
  73. this._positionY += this.viewportBorder;
  74. }
  75. if (position.z > 0) {
  76. if ((this._positionX > globalViewport.x) && (this._positionX < globalViewport.x + globalViewport.width)) {
  77. if ((this._positionY > globalViewport.y) && (this._positionY < globalViewport.y + globalViewport.height))
  78. return true;
  79. }
  80. return true;
  81. }
  82. return false;
  83. };
  84. LensFlareSystem.prototype._isVisible = function () {
  85. if (!this._isEnabled) {
  86. return false;
  87. }
  88. var emitterPosition = this.getEmitterPosition();
  89. var direction = emitterPosition.subtract(this._scene.activeCamera.globalPosition);
  90. var distance = direction.length();
  91. direction.normalize();
  92. var ray = new BABYLON.Ray(this._scene.activeCamera.globalPosition, direction);
  93. var pickInfo = this._scene.pickWithRay(ray, this.meshesSelectionPredicate, true);
  94. return !pickInfo.hit || pickInfo.distance > distance;
  95. };
  96. LensFlareSystem.prototype.render = function () {
  97. if (!this._effect.isReady())
  98. return false;
  99. var engine = this._scene.getEngine();
  100. var viewport = this._scene.activeCamera.viewport;
  101. var globalViewport = viewport.toGlobal(engine.getRenderWidth(true), engine.getRenderHeight(true));
  102. // Position
  103. if (!this.computeEffectivePosition(globalViewport)) {
  104. return false;
  105. }
  106. // Visibility
  107. if (!this._isVisible()) {
  108. return false;
  109. }
  110. // Intensity
  111. var awayX;
  112. var awayY;
  113. if (this._positionX < this.borderLimit + globalViewport.x) {
  114. awayX = this.borderLimit + globalViewport.x - this._positionX;
  115. }
  116. else if (this._positionX > globalViewport.x + globalViewport.width - this.borderLimit) {
  117. awayX = this._positionX - globalViewport.x - globalViewport.width + this.borderLimit;
  118. }
  119. else {
  120. awayX = 0;
  121. }
  122. if (this._positionY < this.borderLimit + globalViewport.y) {
  123. awayY = this.borderLimit + globalViewport.y - this._positionY;
  124. }
  125. else if (this._positionY > globalViewport.y + globalViewport.height - this.borderLimit) {
  126. awayY = this._positionY - globalViewport.y - globalViewport.height + this.borderLimit;
  127. }
  128. else {
  129. awayY = 0;
  130. }
  131. var away = (awayX > awayY) ? awayX : awayY;
  132. away -= this.viewportBorder;
  133. if (away > this.borderLimit) {
  134. away = this.borderLimit;
  135. }
  136. var intensity = 1.0 - (away / this.borderLimit);
  137. if (intensity < 0) {
  138. return false;
  139. }
  140. if (intensity > 1.0) {
  141. intensity = 1.0;
  142. }
  143. if (this.viewportBorder > 0) {
  144. globalViewport.x += this.viewportBorder;
  145. globalViewport.y += this.viewportBorder;
  146. globalViewport.width -= this.viewportBorder * 2;
  147. globalViewport.height -= this.viewportBorder * 2;
  148. this._positionX -= this.viewportBorder;
  149. this._positionY -= this.viewportBorder;
  150. }
  151. // Position
  152. var centerX = globalViewport.x + globalViewport.width / 2;
  153. var centerY = globalViewport.y + globalViewport.height / 2;
  154. var distX = centerX - this._positionX;
  155. var distY = centerY - this._positionY;
  156. // Effects
  157. engine.enableEffect(this._effect);
  158. engine.setState(false);
  159. engine.setDepthBuffer(false);
  160. // VBOs
  161. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, this._effect);
  162. // Flares
  163. for (var index = 0; index < this.lensFlares.length; index++) {
  164. var flare = this.lensFlares[index];
  165. engine.setAlphaMode(flare.alphaMode);
  166. var x = centerX - (distX * flare.position);
  167. var y = centerY - (distY * flare.position);
  168. var cw = flare.size;
  169. var ch = flare.size * engine.getAspectRatio(this._scene.activeCamera, true);
  170. var cx = 2 * (x / (globalViewport.width + globalViewport.x * 2)) - 1.0;
  171. var cy = 1.0 - 2 * (y / (globalViewport.height + globalViewport.y * 2));
  172. var viewportMatrix = BABYLON.Matrix.FromValues(cw / 2, 0, 0, 0, 0, ch / 2, 0, 0, 0, 0, 1, 0, cx, cy, 0, 1);
  173. this._effect.setMatrix("viewportMatrix", viewportMatrix);
  174. // Texture
  175. this._effect.setTexture("textureSampler", flare.texture);
  176. // Color
  177. this._effect.setFloat4("color", flare.color.r * intensity, flare.color.g * intensity, flare.color.b * intensity, 1.0);
  178. // Draw order
  179. engine.draw(true, 0, 6);
  180. }
  181. engine.setDepthBuffer(true);
  182. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  183. return true;
  184. };
  185. LensFlareSystem.prototype.dispose = function () {
  186. var vertexBuffer = this._vertexBuffers[BABYLON.VertexBuffer.PositionKind];
  187. if (vertexBuffer) {
  188. vertexBuffer.dispose();
  189. this._vertexBuffers[BABYLON.VertexBuffer.PositionKind] = null;
  190. }
  191. if (this._indexBuffer) {
  192. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  193. this._indexBuffer = null;
  194. }
  195. while (this.lensFlares.length) {
  196. this.lensFlares[0].dispose();
  197. }
  198. // Remove from scene
  199. var index = this._scene.lensFlareSystems.indexOf(this);
  200. this._scene.lensFlareSystems.splice(index, 1);
  201. };
  202. LensFlareSystem.Parse = function (parsedLensFlareSystem, scene, rootUrl) {
  203. var emitter = scene.getLastEntryByID(parsedLensFlareSystem.emitterId);
  204. var name = parsedLensFlareSystem.name || "lensFlareSystem#" + parsedLensFlareSystem.emitterId;
  205. var lensFlareSystem = new LensFlareSystem(name, emitter, scene);
  206. lensFlareSystem.id = parsedLensFlareSystem.id || name;
  207. lensFlareSystem.borderLimit = parsedLensFlareSystem.borderLimit;
  208. for (var index = 0; index < parsedLensFlareSystem.flares.length; index++) {
  209. var parsedFlare = parsedLensFlareSystem.flares[index];
  210. var flare = new BABYLON.LensFlare(parsedFlare.size, parsedFlare.position, BABYLON.Color3.FromArray(parsedFlare.color), rootUrl + parsedFlare.textureName, lensFlareSystem);
  211. }
  212. return lensFlareSystem;
  213. };
  214. LensFlareSystem.prototype.serialize = function () {
  215. var serializationObject = {};
  216. serializationObject.id = this.id;
  217. serializationObject.name = this.name;
  218. serializationObject.emitterId = this.getEmitter().id;
  219. serializationObject.borderLimit = this.borderLimit;
  220. serializationObject.flares = [];
  221. for (var index = 0; index < this.lensFlares.length; index++) {
  222. var flare = this.lensFlares[index];
  223. serializationObject.flares.push({
  224. size: flare.size,
  225. position: flare.position,
  226. color: flare.color.asArray(),
  227. textureName: BABYLON.Tools.GetFilename(flare.texture.name)
  228. });
  229. }
  230. return serializationObject;
  231. };
  232. return LensFlareSystem;
  233. }());
  234. BABYLON.LensFlareSystem = LensFlareSystem;
  235. })(BABYLON || (BABYLON = {}));