babylon.volumetricLightScatteringPostProcess.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. module BABYLON {
  2. export class GodRaysPostProcess extends PostProcess {
  3. // Members
  4. private _godRaysPass: Effect;
  5. private _godRaysRTT: RenderTargetTexture;
  6. private _viewPort: Viewport;
  7. private _screenCoordinates: Vector2 = Vector2.Zero();
  8. private _cachedDefines: string;
  9. public invert: boolean = true;
  10. public mesh: Mesh;
  11. constructor(name: string, ratio: number, camera: Camera, samplingMode?: number, engine?: Engine, reusable?: boolean) {
  12. super(name, "volumetricLightScattering", ["lightPositionOnScreen"], ["lightScatteringSampler"], ratio, camera, samplingMode, engine, reusable);
  13. var scene = camera.getScene();
  14. this._viewPort = new Viewport(0, 0, 1, 1).toGlobal(scene.getEngine());
  15. // Create billboard
  16. this.mesh = BABYLON.Mesh.CreatePlane("VolumetricLightScatteringMesh", 2, scene);
  17. this.mesh.billboardMode = BABYLON.AbstractMesh.BILLBOARDMODE_ALL;
  18. this.mesh.material = new StandardMaterial('VolumetricLightScatteringMaterial', scene);
  19. // Configure
  20. this._createPass(scene);
  21. this.onApply = (effect: Effect) => {
  22. this._updateScreenCoordinates(scene);
  23. effect.setTexture("lightScatteringSampler", this._godRaysRTT);
  24. effect.setVector2("lightPositionOnScreen", this._screenCoordinates);
  25. };
  26. }
  27. public isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  28. var mesh = subMesh.getMesh();
  29. var scene = mesh.getScene();
  30. var defines = [];
  31. var attribs = [VertexBuffer.PositionKind];
  32. var material = subMesh.getMaterial();
  33. // Render this.mesh as default
  34. if (mesh === this.mesh)
  35. defines.push("#define BASIC_RENDER");
  36. // Alpha test
  37. if (material) {
  38. if (material.needAlphaTesting() || mesh === this.mesh)
  39. defines.push("#define ALPHATEST");
  40. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  41. attribs.push(VertexBuffer.UVKind);
  42. defines.push("#define UV1");
  43. }
  44. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  45. attribs.push(VertexBuffer.UV2Kind);
  46. defines.push("#define UV2");
  47. }
  48. }
  49. // Bones
  50. if (mesh.useBones) {
  51. attribs.push(VertexBuffer.MatricesIndicesKind);
  52. attribs.push(VertexBuffer.MatricesWeightsKind);
  53. defines.push("#define BONES");
  54. defines.push("#define BonesPerMesh " + (mesh.skeleton.bones.length + 1));
  55. }
  56. // Instances
  57. if (useInstances) {
  58. defines.push("#define INSTANCES");
  59. attribs.push("world0");
  60. attribs.push("world1");
  61. attribs.push("world2");
  62. attribs.push("world3");
  63. }
  64. // Get correct effect
  65. var join = defines.join("\n");
  66. if (this._cachedDefines !== join) {
  67. this._cachedDefines = join;
  68. this._godRaysPass = mesh.getScene().getEngine().createEffect(
  69. { vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" },
  70. attribs,
  71. ["world", "mBones", "viewProjection", "diffuseMatrix", "far"],
  72. ["diffuseSampler"], join);
  73. }
  74. return this._godRaysPass.isReady();
  75. }
  76. public dispose(camera: Camera): void {
  77. this._godRaysRTT.dispose();
  78. super.dispose(camera);
  79. }
  80. public getPass(): RenderTargetTexture {
  81. return this._godRaysRTT;
  82. }
  83. // Private methods
  84. private _createPass(scene: Scene): void {
  85. var engine = scene.getEngine();
  86. this._godRaysRTT = new RenderTargetTexture("volumetricLightScatteringMap", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, scene, false, true, Engine.TEXTURETYPE_UNSIGNED_INT);
  87. this._godRaysRTT.wrapU = Texture.CLAMP_ADDRESSMODE;
  88. this._godRaysRTT.wrapV = 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 = (subMesh: SubMesh): void => {
  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, 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, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  122. (isInstance, world) => this._godRaysPass.setMatrix("world", world));
  123. }
  124. };
  125. // Render target texture callbacks
  126. var savedSceneClearColor: Color3;
  127. var sceneClearColor = new Color3(0.0, 0.0, 0.0);
  128. this._godRaysRTT.onBeforeRender = (): void => {
  129. savedSceneClearColor = scene.clearColor;
  130. scene.clearColor = sceneClearColor;
  131. };
  132. this._godRaysRTT.onAfterRender = (): void => {
  133. scene.clearColor = savedSceneClearColor;
  134. };
  135. this._godRaysRTT.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>): void => {
  136. var index;
  137. for (index = 0; index < opaqueSubMeshes.length; index++) {
  138. renderSubMesh(opaqueSubMeshes.data[index]);
  139. }
  140. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  141. renderSubMesh(alphaTestSubMeshes.data[index]);
  142. }
  143. };
  144. }
  145. private _updateScreenCoordinates(scene: Scene): void {
  146. var transform = scene.getTransformMatrix();
  147. var pos = Vector3.Project(this.mesh.position, Matrix.Identity(), transform, this._viewPort);
  148. this._screenCoordinates.x = pos.x / this._viewPort.width;
  149. this._screenCoordinates.y = pos.y / this._viewPort.height;
  150. if (this.invert)
  151. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  152. }
  153. }
  154. }