babylon.volumetricLightScatteringPostProcess.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 GodRaysPostProcess = (function (_super) {
  10. __extends(GodRaysPostProcess, _super);
  11. function GodRaysPostProcess(name, ratio, camera, samplingMode, engine, reusable) {
  12. var _this = this;
  13. _super.call(this, name, "volumetricLightScattering", ["lightPositionOnScreen"], ["lightScatteringSampler"], ratio, camera, samplingMode, engine, reusable);
  14. this._screenCoordinates = BABYLON.Vector2.Zero();
  15. this.invert = true;
  16. var scene = camera.getScene();
  17. this._viewPort = new BABYLON.Viewport(0, 0, 1, 1).toGlobal(scene.getEngine());
  18. // Create billboard
  19. this.mesh = BABYLON.Mesh.CreatePlane("VolumetricLightScatteringMesh", 2, scene);
  20. this.mesh.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_ALL;
  21. this.mesh.material = new BABYLON.StandardMaterial('VolumetricLightScatteringMaterial', scene);
  22. // Configure
  23. this._createPass(scene);
  24. this.onApply = function (effect) {
  25. _this._updateScreenCoordinates(scene);
  26. effect.setTexture("lightScatteringSampler", _this._godRaysRTT);
  27. effect.setVector2("lightPositionOnScreen", _this._screenCoordinates);
  28. };
  29. }
  30. GodRaysPostProcess.prototype.isReady = function (subMesh, useInstances) {
  31. var mesh = subMesh.getMesh();
  32. var scene = mesh.getScene();
  33. var defines = [];
  34. var attribs = [BABYLON.VertexBuffer.PositionKind];
  35. var material = subMesh.getMaterial();
  36. // Render this.mesh as default
  37. if (mesh === this.mesh)
  38. defines.push("#define BASIC_RENDER");
  39. // Alpha test
  40. if (material) {
  41. if (material.needAlphaTesting() || mesh === this.mesh)
  42. defines.push("#define ALPHATEST");
  43. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  44. attribs.push(BABYLON.VertexBuffer.UVKind);
  45. defines.push("#define UV1");
  46. }
  47. if (mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  48. attribs.push(BABYLON.VertexBuffer.UV2Kind);
  49. defines.push("#define UV2");
  50. }
  51. }
  52. // Bones
  53. if (mesh.useBones) {
  54. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  55. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  56. defines.push("#define BONES");
  57. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  58. }
  59. // Instances
  60. if (useInstances) {
  61. defines.push("#define INSTANCES");
  62. attribs.push("world0");
  63. attribs.push("world1");
  64. attribs.push("world2");
  65. attribs.push("world3");
  66. }
  67. // Get correct effect
  68. var join = defines.join("\n");
  69. if (this._cachedDefines !== join) {
  70. this._cachedDefines = join;
  71. this._godRaysPass = mesh.getScene().getEngine().createEffect({ vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" }, attribs, ["world", "mBones", "viewProjection", "diffuseMatrix", "far"], ["diffuseSampler"], join);
  72. }
  73. return this._godRaysPass.isReady();
  74. };
  75. GodRaysPostProcess.prototype.dispose = function (camera) {
  76. this._godRaysRTT.dispose();
  77. _super.prototype.dispose.call(this, camera);
  78. };
  79. GodRaysPostProcess.prototype.getPass = function () {
  80. return this._godRaysRTT;
  81. };
  82. // Private methods
  83. GodRaysPostProcess.prototype._createPass = function (scene) {
  84. var _this = this;
  85. var engine = scene.getEngine();
  86. this._godRaysRTT = new BABYLON.RenderTargetTexture("volumetricLightScatteringMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, scene, false, true, BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT);
  87. this._godRaysRTT.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  88. this._godRaysRTT.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  89. this._godRaysRTT.renderList = null;
  90. scene.customRenderTargets.push(this._godRaysRTT);
  91. // Custom render function for submeshes
  92. var renderSubMesh = function (subMesh) {
  93. var mesh = subMesh.getRenderingMesh();
  94. var scene = mesh.getScene();
  95. var engine = scene.getEngine();
  96. // Culling
  97. engine.setState(subMesh.getMaterial().backFaceCulling);
  98. // Managing instances
  99. var batch = mesh._getInstancesRenderList(subMesh._id);
  100. if (batch.mustReturn) {
  101. return;
  102. }
  103. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  104. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  105. engine.enableEffect(_this._godRaysPass);
  106. mesh._bind(subMesh, _this._godRaysPass, BABYLON.Material.TriangleFillMode);
  107. var material = subMesh.getMaterial();
  108. _this._godRaysPass.setMatrix("viewProjection", scene.getTransformMatrix());
  109. // Alpha test
  110. if (material && (mesh === _this.mesh || material.needAlphaTesting())) {
  111. var alphaTexture = material.getAlphaTestTexture();
  112. _this._godRaysPass.setTexture("diffuseSampler", alphaTexture);
  113. _this._godRaysPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  114. }
  115. // Bones
  116. if (mesh.useBones) {
  117. _this._godRaysPass.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  118. }
  119. // Draw
  120. mesh._processRendering(subMesh, _this._godRaysPass, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._godRaysPass.setMatrix("world", world); });
  121. }
  122. };
  123. // Render target texture callbacks
  124. var savedSceneClearColor = null;
  125. var sceneClearColor = new BABYLON.Color4(0.0, 0.0, 0.0, 1.0);
  126. this._godRaysRTT.onBeforeRender = function () {
  127. savedSceneClearColor = scene.clearColor;
  128. scene.clearColor = sceneClearColor;
  129. };
  130. this._godRaysRTT.onAfterRender = function () {
  131. scene.clearColor = savedSceneClearColor;
  132. };
  133. this._godRaysRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  134. var index;
  135. for (index = 0; index < opaqueSubMeshes.length; index++) {
  136. renderSubMesh(opaqueSubMeshes.data[index]);
  137. }
  138. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  139. renderSubMesh(alphaTestSubMeshes.data[index]);
  140. }
  141. };
  142. };
  143. GodRaysPostProcess.prototype._updateScreenCoordinates = function (scene) {
  144. var transform = scene.getTransformMatrix();
  145. var pos = BABYLON.Vector3.Project(this.mesh.position, BABYLON.Matrix.Identity(), transform, this._viewPort);
  146. this._screenCoordinates.x = pos.x / this._viewPort.width;
  147. this._screenCoordinates.y = pos.y / this._viewPort.height;
  148. if (this.invert)
  149. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  150. };
  151. return GodRaysPostProcess;
  152. })(BABYLON.PostProcess);
  153. BABYLON.GodRaysPostProcess = GodRaysPostProcess;
  154. })(BABYLON || (BABYLON = {}));
  155. //# sourceMappingURL=babylon.volumetricLightScatteringPostProcess.js.map