babylon.volumetricLightScatteringPostProcess.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. // Inspired by http://http.developer.nvidia.com/GPUGems3/gpugems3_ch13.html
  10. var VolumetricLightScatteringPostProcess = (function (_super) {
  11. __extends(VolumetricLightScatteringPostProcess, _super);
  12. /**
  13. * @constructor
  14. * @param {string} name - The post-process name
  15. * @param {any} ratio - The size of the post-process and/or internal pass (0.5 means that your postprocess will have a width = canvas.width 0.5 and a height = canvas.height 0.5)
  16. * @param {BABYLON.Camera} camera - The camera that the post-process will be attached to
  17. * @param {BABYLON.Mesh} mesh - The mesh used to create the light scattering
  18. * @param {number} samples - The post-process quality, default 100
  19. * @param {number} samplingMode - The post-process filtering mode
  20. * @param {BABYLON.Engine} engine - The babylon engine
  21. * @param {boolean} reusable - If the post-process is reusable
  22. */
  23. function VolumetricLightScatteringPostProcess(name, ratio, camera, mesh, samples, samplingMode, engine, reusable) {
  24. var _this = this;
  25. if (samples === void 0) { samples = 100; }
  26. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.BILINEAR_SAMPLINGMODE; }
  27. _super.call(this, name, "volumetricLightScattering", ["decay", "exposure", "weight", "meshPositionOnScreen", "density"], ["lightScatteringSampler"], ratio.postProcessRatio || ratio, camera, samplingMode, engine, reusable, "#define NUM_SAMPLES " + samples);
  28. this._screenCoordinates = BABYLON.Vector2.Zero();
  29. /**
  30. * Set if the post-process should use a custom position for the light source (true) or the internal mesh position (false)
  31. * @type {boolean}
  32. */
  33. this.useCustomMeshPosition = false;
  34. /**
  35. * If the post-process should inverse the light scattering direction
  36. * @type {boolean}
  37. */
  38. this.invert = true;
  39. /**
  40. * Array containing the excluded meshes not rendered in the internal pass
  41. */
  42. this.excludedMeshes = new Array();
  43. this.exposure = 0.3;
  44. this.decay = 0.96815;
  45. this.weight = 0.58767;
  46. this.density = 0.926;
  47. var scene = camera.getScene();
  48. this._viewPort = new BABYLON.Viewport(0, 0, 1, 1).toGlobal(scene.getEngine());
  49. // Configure mesh
  50. this.mesh = (mesh !== null) ? mesh : VolumetricLightScatteringPostProcess.CreateDefaultMesh("VolumetricLightScatteringMesh", scene);
  51. // Configure
  52. this._createPass(scene, ratio.passRatio || ratio);
  53. this.onApply = function (effect) {
  54. _this._updateMeshScreenCoordinates(scene);
  55. effect.setTexture("lightScatteringSampler", _this._volumetricLightScatteringRTT);
  56. effect.setFloat("exposure", _this.exposure);
  57. effect.setFloat("decay", _this.decay);
  58. effect.setFloat("weight", _this.weight);
  59. effect.setFloat("density", _this.density);
  60. effect.setVector2("meshPositionOnScreen", _this._screenCoordinates);
  61. };
  62. }
  63. VolumetricLightScatteringPostProcess.prototype.isReady = function (subMesh, useInstances) {
  64. var mesh = subMesh.getMesh();
  65. var defines = [];
  66. var attribs = [BABYLON.VertexBuffer.PositionKind];
  67. var material = subMesh.getMaterial();
  68. var needUV = false;
  69. // Render this.mesh as default
  70. if (mesh === this.mesh) {
  71. defines.push("#define BASIC_RENDER");
  72. defines.push("#define NEED_UV");
  73. needUV = true;
  74. }
  75. // Alpha test
  76. if (material) {
  77. if (material.needAlphaTesting() || mesh === this.mesh)
  78. defines.push("#define ALPHATEST");
  79. if (material.opacityTexture !== undefined) {
  80. defines.push("#define OPACITY");
  81. if (!needUV)
  82. defines.push("#define NEED_UV");
  83. }
  84. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  85. attribs.push(BABYLON.VertexBuffer.UVKind);
  86. defines.push("#define UV1");
  87. }
  88. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  89. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  90. defines.push("#define UV2");
  91. }
  92. }
  93. // Bones
  94. if (mesh.useBones) {
  95. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  96. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  97. defines.push("#define BONES");
  98. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  99. }
  100. // Instances
  101. if (useInstances) {
  102. defines.push("#define INSTANCES");
  103. attribs.push("world0");
  104. attribs.push("world1");
  105. attribs.push("world2");
  106. attribs.push("world3");
  107. }
  108. // Get correct effect
  109. var join = defines.join("\n");
  110. if (this._cachedDefines !== join) {
  111. this._cachedDefines = join;
  112. this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect({ vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" }, attribs, ["world", "mBones", "viewProjection", "diffuseMatrix"], ["diffuseSampler", "opacitySampler"], join);
  113. }
  114. return this._volumetricLightScatteringPass.isReady();
  115. };
  116. /**
  117. * Sets the new light position for light scattering effect
  118. * @param {BABYLON.Vector3} The new custom light position
  119. */
  120. VolumetricLightScatteringPostProcess.prototype.setCustomMeshPosition = function (position) {
  121. this._customMeshPosition = position;
  122. };
  123. /**
  124. * Returns the light position for light scattering effect
  125. * @return {BABYLON.Vector3} The custom light position
  126. */
  127. VolumetricLightScatteringPostProcess.prototype.getCustomMeshPosition = function () {
  128. return this._customMeshPosition;
  129. };
  130. /**
  131. * Disposes the internal assets and detaches the post-process from the camera
  132. */
  133. VolumetricLightScatteringPostProcess.prototype.dispose = function (camera) {
  134. var rttIndex = camera.getScene().customRenderTargets.indexOf(this._volumetricLightScatteringRTT);
  135. if (rttIndex !== -1) {
  136. camera.getScene().customRenderTargets.splice(rttIndex, 1);
  137. }
  138. this._volumetricLightScatteringRTT.dispose();
  139. _super.prototype.dispose.call(this, camera);
  140. };
  141. /**
  142. * Returns the render target texture used by the post-process
  143. * @return {BABYLON.RenderTargetTexture} The render target texture used by the post-process
  144. */
  145. VolumetricLightScatteringPostProcess.prototype.getPass = function () {
  146. return this._volumetricLightScatteringRTT;
  147. };
  148. // Private methods
  149. VolumetricLightScatteringPostProcess.prototype._meshExcluded = function (mesh) {
  150. if (this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  151. return true;
  152. }
  153. return false;
  154. };
  155. VolumetricLightScatteringPostProcess.prototype._createPass = function (scene, ratio) {
  156. var _this = this;
  157. var engine = scene.getEngine();
  158. this._volumetricLightScatteringRTT = new BABYLON.RenderTargetTexture("volumetricLightScatteringMap", { width: engine.getRenderWidth() * ratio, height: engine.getRenderHeight() * ratio }, scene, false, true, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT);
  159. this._volumetricLightScatteringRTT.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  160. this._volumetricLightScatteringRTT.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  161. this._volumetricLightScatteringRTT.renderList = null;
  162. this._volumetricLightScatteringRTT.renderParticles = false;
  163. scene.customRenderTargets.push(this._volumetricLightScatteringRTT);
  164. // Custom render function for submeshes
  165. var renderSubMesh = function (subMesh) {
  166. var mesh = subMesh.getRenderingMesh();
  167. if (_this._meshExcluded(mesh)) {
  168. return;
  169. }
  170. var scene = mesh.getScene();
  171. var engine = scene.getEngine();
  172. // Culling
  173. engine.setState(subMesh.getMaterial().backFaceCulling);
  174. // Managing instances
  175. var batch = mesh._getInstancesRenderList(subMesh._id);
  176. if (batch.mustReturn) {
  177. return;
  178. }
  179. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  180. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  181. engine.enableEffect(_this._volumetricLightScatteringPass);
  182. mesh._bind(subMesh, _this._volumetricLightScatteringPass, BABYLON.Material.TriangleFillMode);
  183. var material = subMesh.getMaterial();
  184. _this._volumetricLightScatteringPass.setMatrix("viewProjection", scene.getTransformMatrix());
  185. // Alpha test
  186. if (material && (mesh === _this.mesh || material.needAlphaTesting() || material.opacityTexture !== undefined)) {
  187. var alphaTexture = material.getAlphaTestTexture();
  188. _this._volumetricLightScatteringPass.setTexture("diffuseSampler", alphaTexture);
  189. if (alphaTexture) {
  190. _this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  191. }
  192. if (material.opacityTexture !== undefined)
  193. _this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
  194. }
  195. // Bones
  196. if (mesh.useBones) {
  197. _this._volumetricLightScatteringPass.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  198. }
  199. // Draw
  200. mesh._processRendering(subMesh, _this._volumetricLightScatteringPass, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._volumetricLightScatteringPass.setMatrix("world", world); });
  201. }
  202. };
  203. // Render target texture callbacks
  204. var savedSceneClearColor;
  205. var sceneClearColor = new BABYLON.Color3(0.0, 0.0, 0.0);
  206. this._volumetricLightScatteringRTT.onBeforeRender = function () {
  207. savedSceneClearColor = scene.clearColor;
  208. scene.clearColor = sceneClearColor;
  209. };
  210. this._volumetricLightScatteringRTT.onAfterRender = function () {
  211. scene.clearColor = savedSceneClearColor;
  212. };
  213. this._volumetricLightScatteringRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  214. var index;
  215. for (index = 0; index < opaqueSubMeshes.length; index++) {
  216. renderSubMesh(opaqueSubMeshes.data[index]);
  217. }
  218. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  219. renderSubMesh(alphaTestSubMeshes.data[index]);
  220. }
  221. for (index = 0; index < transparentSubMeshes.length; index++) {
  222. renderSubMesh(transparentSubMeshes.data[index]);
  223. }
  224. };
  225. };
  226. VolumetricLightScatteringPostProcess.prototype._updateMeshScreenCoordinates = function (scene) {
  227. var transform = scene.getTransformMatrix();
  228. var pos = BABYLON.Vector3.Project(this.useCustomMeshPosition ? this._customMeshPosition : this.mesh.position, BABYLON.Matrix.Identity(), transform, this._viewPort);
  229. this._screenCoordinates.x = pos.x / this._viewPort.width;
  230. this._screenCoordinates.y = pos.y / this._viewPort.height;
  231. if (this.invert)
  232. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  233. };
  234. // Static methods
  235. /**
  236. * Creates a default mesh for the Volumeric Light Scattering post-process
  237. * @param {string} The mesh name
  238. * @param {BABYLON.Scene} The scene where to create the mesh
  239. * @return {BABYLON.Mesh} the default mesh
  240. */
  241. VolumetricLightScatteringPostProcess.CreateDefaultMesh = function (name, scene) {
  242. var mesh = BABYLON.Mesh.CreatePlane(name, 1, scene);
  243. mesh.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_ALL;
  244. mesh.material = new BABYLON.StandardMaterial(name + "Material", scene);
  245. return mesh;
  246. };
  247. return VolumetricLightScatteringPostProcess;
  248. })(BABYLON.PostProcess);
  249. BABYLON.VolumetricLightScatteringPostProcess = VolumetricLightScatteringPostProcess;
  250. })(BABYLON || (BABYLON = {}));
  251. //# sourceMappingURL=babylon.volumetricLightScatteringPostProcess.js.map