babylon.volumetricLightScatteringPostProcess.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. this._godRaysRTT.renderParticles = false;
  91. scene.customRenderTargets.push(this._godRaysRTT);
  92. // Custom render function for submeshes
  93. var renderSubMesh = function (subMesh) {
  94. var mesh = subMesh.getRenderingMesh();
  95. var scene = mesh.getScene();
  96. var engine = scene.getEngine();
  97. // Culling
  98. engine.setState(subMesh.getMaterial().backFaceCulling);
  99. // Managing instances
  100. var batch = mesh._getInstancesRenderList(subMesh._id);
  101. if (batch.mustReturn) {
  102. return;
  103. }
  104. var hardwareInstancedRendering = (engine.getCaps().instancedArrays !== null) && (batch.visibleInstances[subMesh._id] !== null);
  105. if (_this.isReady(subMesh, hardwareInstancedRendering)) {
  106. engine.enableEffect(_this._godRaysPass);
  107. mesh._bind(subMesh, _this._godRaysPass, BABYLON.Material.TriangleFillMode);
  108. var material = subMesh.getMaterial();
  109. _this._godRaysPass.setMatrix("viewProjection", scene.getTransformMatrix());
  110. // Alpha test
  111. if (material && (mesh === _this.mesh || material.needAlphaTesting())) {
  112. var alphaTexture = material.getAlphaTestTexture();
  113. _this._godRaysPass.setTexture("diffuseSampler", alphaTexture);
  114. _this._godRaysPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  115. }
  116. // Bones
  117. if (mesh.useBones) {
  118. _this._godRaysPass.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  119. }
  120. // Draw
  121. mesh._processRendering(subMesh, _this._godRaysPass, BABYLON.Material.TriangleFillMode, batch, hardwareInstancedRendering, function (isInstance, world) { return _this._godRaysPass.setMatrix("world", world); });
  122. }
  123. };
  124. // Render target texture callbacks
  125. var savedSceneClearColor;
  126. var sceneClearColor = new BABYLON.Color3(0.0, 0.0, 0.0);
  127. this._godRaysRTT.onBeforeRender = function () {
  128. savedSceneClearColor = scene.clearColor;
  129. scene.clearColor = sceneClearColor;
  130. };
  131. this._godRaysRTT.onAfterRender = function () {
  132. scene.clearColor = savedSceneClearColor;
  133. };
  134. this._godRaysRTT.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes) {
  135. var index;
  136. for (index = 0; index < opaqueSubMeshes.length; index++) {
  137. renderSubMesh(opaqueSubMeshes.data[index]);
  138. }
  139. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  140. renderSubMesh(alphaTestSubMeshes.data[index]);
  141. }
  142. };
  143. };
  144. GodRaysPostProcess.prototype._updateScreenCoordinates = function (scene) {
  145. var transform = scene.getTransformMatrix();
  146. var pos = BABYLON.Vector3.Project(this.mesh.position, BABYLON.Matrix.Identity(), transform, this._viewPort);
  147. this._screenCoordinates.x = pos.x / this._viewPort.width;
  148. this._screenCoordinates.y = pos.y / this._viewPort.height;
  149. if (this.invert)
  150. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  151. };
  152. return GodRaysPostProcess;
  153. })(BABYLON.PostProcess);
  154. BABYLON.GodRaysPostProcess = GodRaysPostProcess;
  155. })(BABYLON || (BABYLON = {}));
  156. //# sourceMappingURL=babylon.volumetricLightScatteringPostProcess.js.map