babylon.volumetricLightScatteringPostProcess.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. // Render this.mesh as default
  69. if (mesh === this.mesh) {
  70. defines.push("#define BASIC_RENDER");
  71. }
  72. // Alpha test
  73. if (material) {
  74. if (material.needAlphaTesting() || mesh === this.mesh)
  75. defines.push("#define ALPHATEST");
  76. if (material.opacityTexture !== undefined)
  77. defines.push("#define OPACITY");
  78. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  79. attribs.push(BABYLON.VertexBuffer.UVKind);
  80. defines.push("#define UV1");
  81. }
  82. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  83. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  84. defines.push("#define UV2");
  85. }
  86. }
  87. // Bones
  88. if (mesh.useBones) {
  89. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  90. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  91. defines.push("#define BONES");
  92. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  93. }
  94. // Instances
  95. if (useInstances) {
  96. defines.push("#define INSTANCES");
  97. attribs.push("world0");
  98. attribs.push("world1");
  99. attribs.push("world2");
  100. attribs.push("world3");
  101. }
  102. // Get correct effect
  103. var join = defines.join("\n");
  104. if (this._cachedDefines !== join) {
  105. this._cachedDefines = join;
  106. this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect({ vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" }, attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler", "opacitySampler"], join);
  107. }
  108. return this._volumetricLightScatteringPass.isReady();
  109. };
  110. /**
  111. * Sets the new light position for light scattering effect
  112. * @param {BABYLON.Vector3} The new custom light position
  113. */
  114. VolumetricLightScatteringPostProcess.prototype.setCustomMeshPosition = function (position) {
  115. this._customMeshPosition = position;
  116. };
  117. /**
  118. * Returns the light position for light scattering effect
  119. * @return {BABYLON.Vector3} The custom light position
  120. */
  121. VolumetricLightScatteringPostProcess.prototype.getCustomMeshPosition = function () {
  122. return this._customMeshPosition;
  123. };
  124. /**
  125. * Disposes the internal assets and detaches the post-process from the camera
  126. */
  127. VolumetricLightScatteringPostProcess.prototype.dispose = function (camera) {
  128. var rttIndex = camera.getScene().customRenderTargets.indexOf(this._volumetricLightScatteringRTT);
  129. if (rttIndex !== -1) {
  130. camera.getScene().customRenderTargets.splice(rttIndex, 1);
  131. }
  132. this._volumetricLightScatteringRTT.dispose();
  133. _super.prototype.dispose.call(this, camera);
  134. };
  135. /**
  136. * Returns the render target texture used by the post-process
  137. * @return {BABYLON.RenderTargetTexture} The render target texture used by the post-process
  138. */
  139. VolumetricLightScatteringPostProcess.prototype.getPass = function () {
  140. return this._volumetricLightScatteringRTT;
  141. };
  142. // Private methods
  143. VolumetricLightScatteringPostProcess.prototype._meshExcluded = function (mesh) {
  144. if (this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  145. return true;
  146. }
  147. return false;
  148. };
  149. VolumetricLightScatteringPostProcess.prototype._createPass = function (scene, ratio) {
  150. var _this = this;
  151. var engine = scene.getEngine();
  152. this._volumetricLightScatteringRTT = new BABYLON.RenderTargetTexture("volumetricLightScatteringMap", { width: engine.getRenderWidth() * ratio, height: engine.getRenderHeight() * ratio }, scene, false, true, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT);
  153. this._volumetricLightScatteringRTT.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  154. this._volumetricLightScatteringRTT.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  155. this._volumetricLightScatteringRTT.renderList = null;
  156. this._volumetricLightScatteringRTT.renderParticles = false;
  157. scene.customRenderTargets.push(this._volumetricLightScatteringRTT);
  158. // Custom render function for submeshes
  159. var renderSubMesh = function (subMesh) {
  160. var mesh = subMesh.getRenderingMesh();
  161. if (_this._meshExcluded(mesh)) {
  162. return;
  163. }
  164. var scene = mesh.getScene();
  165. var engine = scene.getEngine();
  166. // Culling
  167. engine.setState(subMesh.getMaterial().backFaceCulling);
  168. // Managing instances
  169. var batch = mesh._getInstancesRenderList(subMesh._id);
  170. if (batch.mustReturn) {
  171. return;
  172. }
  173. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  174. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  175. engine.enableEffect(_this._volumetricLightScatteringPass);
  176. mesh._bind(subMesh, _this._volumetricLightScatteringPass, BABYLON.Material.TriangleFillMode);
  177. var material = subMesh.getMaterial();
  178. _this._volumetricLightScatteringPass.setMatrix("viewProjection", scene.getTransformMatrix());
  179. // Alpha test
  180. if (material && (mesh === _this.mesh || material.needAlphaTesting() || material.opacityTexture !== undefined)) {
  181. var alphaTexture = material.getAlphaTestTexture();
  182. _this._volumetricLightScatteringPass.setTexture("diffuseSampler", alphaTexture);
  183. if (_this.mesh.material && alphaTexture)
  184. _this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  185. if (material.opacityTexture !== undefined)
  186. _this._volumetricLightScatteringPass.setTexture("opacitySampler", material.opacityTexture);
  187. }
  188. // Bones
  189. if (mesh.useBones) {
  190. _this._volumetricLightScatteringPass.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  191. }
  192. // Draw
  193. mesh._processRendering(subMesh, _this._volumetricLightScatteringPass, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._volumetricLightScatteringPass.setMatrix("world", world); });
  194. }
  195. };
  196. // Render target texture callbacks
  197. var savedSceneClearColor;
  198. var sceneClearColor = new BABYLON.Color3(0.0, 0.0, 0.0);
  199. this._volumetricLightScatteringRTT.onBeforeRender = function () {
  200. savedSceneClearColor = scene.clearColor;
  201. scene.clearColor = sceneClearColor;
  202. };
  203. this._volumetricLightScatteringRTT.onAfterRender = function () {
  204. scene.clearColor = savedSceneClearColor;
  205. };
  206. this._volumetricLightScatteringRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  207. var index;
  208. for (index = 0; index < opaqueSubMeshes.length; index++) {
  209. renderSubMesh(opaqueSubMeshes.data[index]);
  210. }
  211. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  212. renderSubMesh(alphaTestSubMeshes.data[index]);
  213. }
  214. for (index = 0; index < transparentSubMeshes.length; index++) {
  215. renderSubMesh(transparentSubMeshes.data[index]);
  216. }
  217. };
  218. };
  219. VolumetricLightScatteringPostProcess.prototype._updateMeshScreenCoordinates = function (scene) {
  220. var transform = scene.getTransformMatrix();
  221. var pos = BABYLON.Vector3.Project(this.useCustomMeshPosition ? this._customMeshPosition : this.mesh.position, BABYLON.Matrix.Identity(), transform, this._viewPort);
  222. this._screenCoordinates.x = pos.x / this._viewPort.width;
  223. this._screenCoordinates.y = pos.y / this._viewPort.height;
  224. if (this.invert)
  225. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  226. };
  227. // Static methods
  228. /**
  229. * Creates a default mesh for the Volumeric Light Scattering post-process
  230. * @param {string} The mesh name
  231. * @param {BABYLON.Scene} The scene where to create the mesh
  232. * @return {BABYLON.Mesh} the default mesh
  233. */
  234. VolumetricLightScatteringPostProcess.CreateDefaultMesh = function (name, scene) {
  235. var mesh = BABYLON.Mesh.CreatePlane(name, 1, scene);
  236. mesh.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_ALL;
  237. mesh.material = new BABYLON.StandardMaterial(name + "Material", scene);
  238. return mesh;
  239. };
  240. return VolumetricLightScatteringPostProcess;
  241. })(BABYLON.PostProcess);
  242. BABYLON.VolumetricLightScatteringPostProcess = VolumetricLightScatteringPostProcess;
  243. })(BABYLON || (BABYLON = {}));
  244. //# sourceMappingURL=../PostProcess/babylon.volumetricLightScatteringPostProcess.js.map