babylon.volumetricLightScatteringPostProcess.js 11 KB

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