babylon.lensFlareSystem.js 10 KB

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