babylon.volumetricLightScatteringPostProcess.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. scene.customRenderTargets.push(this._godRaysRTT);
  91. // Custom render function for submeshes
  92. var renderSubMesh = (subMesh: SubMesh): void => {
  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, 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, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  121. (isInstance, world) => this._godRaysPass.setMatrix("world", world));
  122. }
  123. };
  124. // Render target texture callbacks
  125. var savedSceneClearColor: Color4 = null;
  126. var sceneClearColor = new Color4(0.0, 0.0, 0.0, 1.0);
  127. this._godRaysRTT.onBeforeRender = (): void => {
  128. savedSceneClearColor = scene.clearColor;
  129. scene.clearColor = sceneClearColor;
  130. };
  131. this._godRaysRTT.onAfterRender = (): void => {
  132. scene.clearColor = savedSceneClearColor;
  133. };
  134. this._godRaysRTT.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>): void => {
  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. private _updateScreenCoordinates(scene: Scene): void {
  145. var transform = scene.getTransformMatrix();
  146. var pos = Vector3.Project(this.mesh.position, 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. }
  153. }