babylon.volumetricLightScatteringPostProcess.js 13 KB

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