volumetricLightScatteringPostProcess.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import { SmartArray, Tools, serializeAsVector3, serialize, serializeAsMeshReference } from "Tools";
  2. import { Color4, Color3, Vector2, Vector3, Matrix, Viewport } from "Math";
  3. import { Mesh, SubMesh, AbstractMesh, Scene, VertexBuffer } from "Mesh";
  4. import { Camera } from "Cameras";
  5. import { Material, StandardMaterial, RenderTargetTexture, Effect, Texture } from "Materials";
  6. import { PostProcess } from "PostProcess";
  7. import { Engine } from "Engine";
  8. /**
  9. * Inspired by http://http.developer.nvidia.com/GPUGems3/gpugems3_ch13.html
  10. */
  11. export class VolumetricLightScatteringPostProcess extends PostProcess {
  12. // Members
  13. private _volumetricLightScatteringPass: Effect;
  14. private _volumetricLightScatteringRTT: RenderTargetTexture;
  15. private _viewPort: Viewport;
  16. private _screenCoordinates: Vector2 = Vector2.Zero();
  17. private _cachedDefines: string;
  18. /**
  19. * If not undefined, the mesh position is computed from the attached node position
  20. */
  21. public attachedNode: { position: Vector3 };
  22. /**
  23. * Custom position of the mesh. Used if "useCustomMeshPosition" is set to "true"
  24. */
  25. @serializeAsVector3()
  26. public customMeshPosition: Vector3 = Vector3.Zero();
  27. /**
  28. * Set if the post-process should use a custom position for the light source (true) or the internal mesh position (false)
  29. */
  30. @serialize()
  31. public useCustomMeshPosition: boolean = false;
  32. /**
  33. * If the post-process should inverse the light scattering direction
  34. */
  35. @serialize()
  36. public invert: boolean = true;
  37. /**
  38. * The internal mesh used by the post-process
  39. */
  40. @serializeAsMeshReference()
  41. public mesh: Mesh;
  42. /**
  43. * @hidden
  44. * VolumetricLightScatteringPostProcess.useDiffuseColor is no longer used, use the mesh material directly instead
  45. */
  46. public get useDiffuseColor(): boolean {
  47. Tools.Warn("VolumetricLightScatteringPostProcess.useDiffuseColor is no longer used, use the mesh material directly instead");
  48. return false;
  49. }
  50. public set useDiffuseColor(useDiffuseColor: boolean) {
  51. Tools.Warn("VolumetricLightScatteringPostProcess.useDiffuseColor is no longer used, use the mesh material directly instead");
  52. }
  53. /**
  54. * Array containing the excluded meshes not rendered in the internal pass
  55. */
  56. @serialize()
  57. public excludedMeshes = new Array<AbstractMesh>();
  58. /**
  59. * Controls the overall intensity of the post-process
  60. */
  61. @serialize()
  62. public exposure = 0.3;
  63. /**
  64. * Dissipates each sample's contribution in range [0, 1]
  65. */
  66. @serialize()
  67. public decay = 0.96815;
  68. /**
  69. * Controls the overall intensity of each sample
  70. */
  71. @serialize()
  72. public weight = 0.58767;
  73. /**
  74. * Controls the density of each sample
  75. */
  76. @serialize()
  77. public density = 0.926;
  78. /**
  79. * @constructor
  80. * @param name The post-process name
  81. * @param ratio The size of the post-process and/or internal pass (0.5 means that your postprocess will have a width = canvas.width 0.5 and a height = canvas.height 0.5)
  82. * @param camera The camera that the post-process will be attached to
  83. * @param mesh The mesh used to create the light scattering
  84. * @param samples The post-process quality, default 100
  85. * @param samplingModeThe post-process filtering mode
  86. * @param engine The babylon engine
  87. * @param reusable If the post-process is reusable
  88. * @param scene The constructor needs a scene reference to initialize internal components. If "camera" is null a "scene" must be provided
  89. */
  90. constructor(name: string, ratio: any, camera: Camera, mesh?: Mesh, samples: number = 100, samplingMode: number = Texture.BILINEAR_SAMPLINGMODE, engine?: Engine, reusable?: boolean, scene?: Scene) {
  91. super(name, "volumetricLightScattering", ["decay", "exposure", "weight", "meshPositionOnScreen", "density"], ["lightScatteringSampler"], ratio.postProcessRatio || ratio, camera, samplingMode, engine, reusable, "#define NUM_SAMPLES " + samples);
  92. scene = <Scene>((camera === null) ? scene : camera.getScene()); // parameter "scene" can be null.
  93. engine = scene.getEngine();
  94. this._viewPort = new Viewport(0, 0, 1, 1).toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  95. // Configure mesh
  96. this.mesh = (<Mesh>((mesh !== null) ? mesh : VolumetricLightScatteringPostProcess.CreateDefaultMesh("VolumetricLightScatteringMesh", scene)));
  97. // Configure
  98. this._createPass(scene, ratio.passRatio || ratio);
  99. this.onActivate = (camera: Camera) => {
  100. if (!this.isSupported) {
  101. this.dispose(camera);
  102. }
  103. this.onActivate = null;
  104. };
  105. this.onApplyObservable.add((effect: Effect) => {
  106. this._updateMeshScreenCoordinates(<Scene>scene);
  107. effect.setTexture("lightScatteringSampler", this._volumetricLightScatteringRTT);
  108. effect.setFloat("exposure", this.exposure);
  109. effect.setFloat("decay", this.decay);
  110. effect.setFloat("weight", this.weight);
  111. effect.setFloat("density", this.density);
  112. effect.setVector2("meshPositionOnScreen", this._screenCoordinates);
  113. });
  114. }
  115. /**
  116. * Returns the string "VolumetricLightScatteringPostProcess"
  117. * @returns "VolumetricLightScatteringPostProcess"
  118. */
  119. public getClassName(): string {
  120. return "VolumetricLightScatteringPostProcess";
  121. }
  122. private _isReady(subMesh: SubMesh, useInstances: boolean): boolean {
  123. var mesh = subMesh.getMesh();
  124. // Render this.mesh as default
  125. if (mesh === this.mesh && mesh.material) {
  126. return mesh.material.isReady(mesh);
  127. }
  128. var defines = [];
  129. var attribs = [VertexBuffer.PositionKind];
  130. var material: any = subMesh.getMaterial();
  131. // Alpha test
  132. if (material) {
  133. if (material.needAlphaTesting()) {
  134. defines.push("#define ALPHATEST");
  135. }
  136. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  137. attribs.push(VertexBuffer.UVKind);
  138. defines.push("#define UV1");
  139. }
  140. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  141. attribs.push(VertexBuffer.UV2Kind);
  142. defines.push("#define UV2");
  143. }
  144. }
  145. // Bones
  146. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  147. attribs.push(VertexBuffer.MatricesIndicesKind);
  148. attribs.push(VertexBuffer.MatricesWeightsKind);
  149. defines.push("#define NUM_BONE_INFLUENCERS " + mesh.numBoneInfluencers);
  150. defines.push("#define BonesPerMesh " + (mesh.skeleton ? (mesh.skeleton.bones.length + 1) : 0));
  151. } else {
  152. defines.push("#define NUM_BONE_INFLUENCERS 0");
  153. }
  154. // Instances
  155. if (useInstances) {
  156. defines.push("#define INSTANCES");
  157. attribs.push("world0");
  158. attribs.push("world1");
  159. attribs.push("world2");
  160. attribs.push("world3");
  161. }
  162. // Get correct effect
  163. var join = defines.join("\n");
  164. if (this._cachedDefines !== join) {
  165. this._cachedDefines = join;
  166. this._volumetricLightScatteringPass = mesh.getScene().getEngine().createEffect(
  167. { vertexElement: "depth", fragmentElement: "volumetricLightScatteringPass" },
  168. attribs,
  169. ["world", "mBones", "viewProjection", "diffuseMatrix"],
  170. ["diffuseSampler"], join);
  171. }
  172. return this._volumetricLightScatteringPass.isReady();
  173. }
  174. /**
  175. * Sets the new light position for light scattering effect
  176. * @param position The new custom light position
  177. */
  178. public setCustomMeshPosition(position: Vector3): void {
  179. this.customMeshPosition = position;
  180. }
  181. /**
  182. * Returns the light position for light scattering effect
  183. * @return Vector3 The custom light position
  184. */
  185. public getCustomMeshPosition(): Vector3 {
  186. return this.customMeshPosition;
  187. }
  188. /**
  189. * Disposes the internal assets and detaches the post-process from the camera
  190. */
  191. public dispose(camera: Camera): void {
  192. var rttIndex = camera.getScene().customRenderTargets.indexOf(this._volumetricLightScatteringRTT);
  193. if (rttIndex !== -1) {
  194. camera.getScene().customRenderTargets.splice(rttIndex, 1);
  195. }
  196. this._volumetricLightScatteringRTT.dispose();
  197. super.dispose(camera);
  198. }
  199. /**
  200. * Returns the render target texture used by the post-process
  201. * @return the render target texture used by the post-process
  202. */
  203. public getPass(): RenderTargetTexture {
  204. return this._volumetricLightScatteringRTT;
  205. }
  206. // Private methods
  207. private _meshExcluded(mesh: AbstractMesh) {
  208. if (this.excludedMeshes.length > 0 && this.excludedMeshes.indexOf(mesh) !== -1) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. private _createPass(scene: Scene, ratio: number): void {
  214. var engine = scene.getEngine();
  215. this._volumetricLightScatteringRTT = new RenderTargetTexture("volumetricLightScatteringMap", { width: engine.getRenderWidth() * ratio, height: engine.getRenderHeight() * ratio }, scene, false, true, Engine.TEXTURETYPE_UNSIGNED_INT);
  216. this._volumetricLightScatteringRTT.wrapU = Texture.CLAMP_ADDRESSMODE;
  217. this._volumetricLightScatteringRTT.wrapV = Texture.CLAMP_ADDRESSMODE;
  218. this._volumetricLightScatteringRTT.renderList = null;
  219. this._volumetricLightScatteringRTT.renderParticles = false;
  220. this._volumetricLightScatteringRTT.ignoreCameraViewport = true;
  221. var camera = this.getCamera();
  222. if (camera) {
  223. camera.customRenderTargets.push(this._volumetricLightScatteringRTT);
  224. } else {
  225. scene.customRenderTargets.push(this._volumetricLightScatteringRTT);
  226. }
  227. // Custom render function for submeshes
  228. var renderSubMesh = (subMesh: SubMesh): void => {
  229. var mesh = subMesh.getRenderingMesh();
  230. if (this._meshExcluded(mesh)) {
  231. return;
  232. }
  233. let material = subMesh.getMaterial();
  234. if (!material) {
  235. return;
  236. }
  237. var scene = mesh.getScene();
  238. var engine = scene.getEngine();
  239. // Culling
  240. engine.setState(material.backFaceCulling);
  241. // Managing instances
  242. var batch = mesh._getInstancesRenderList(subMesh._id);
  243. if (batch.mustReturn) {
  244. return;
  245. }
  246. var hardwareInstancedRendering = (engine.getCaps().instancedArrays) && (batch.visibleInstances[subMesh._id] !== null);
  247. if (this._isReady(subMesh, hardwareInstancedRendering)) {
  248. var effect: Effect = this._volumetricLightScatteringPass;
  249. if (mesh === this.mesh) {
  250. if (subMesh.effect) {
  251. effect = subMesh.effect;
  252. } else {
  253. effect = <Effect>material.getEffect();
  254. }
  255. }
  256. engine.enableEffect(effect);
  257. mesh._bind(subMesh, effect, Material.TriangleFillMode);
  258. if (mesh === this.mesh) {
  259. material.bind(mesh.getWorldMatrix(), mesh);
  260. }
  261. else {
  262. this._volumetricLightScatteringPass.setMatrix("viewProjection", scene.getTransformMatrix());
  263. // Alpha test
  264. if (material && material.needAlphaTesting()) {
  265. var alphaTexture = material.getAlphaTestTexture();
  266. this._volumetricLightScatteringPass.setTexture("diffuseSampler", alphaTexture);
  267. if (alphaTexture) {
  268. this._volumetricLightScatteringPass.setMatrix("diffuseMatrix", alphaTexture.getTextureMatrix());
  269. }
  270. }
  271. // Bones
  272. if (mesh.useBones && mesh.computeBonesUsingShaders && mesh.skeleton) {
  273. this._volumetricLightScatteringPass.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  274. }
  275. }
  276. // Draw
  277. mesh._processRendering(subMesh, this._volumetricLightScatteringPass, Material.TriangleFillMode, batch, hardwareInstancedRendering,
  278. (isInstance, world) => effect.setMatrix("world", world));
  279. }
  280. };
  281. // Render target texture callbacks
  282. var savedSceneClearColor: Color4;
  283. var sceneClearColor = new Color4(0.0, 0.0, 0.0, 1.0);
  284. this._volumetricLightScatteringRTT.onBeforeRenderObservable.add((): void => {
  285. savedSceneClearColor = scene.clearColor;
  286. scene.clearColor = sceneClearColor;
  287. });
  288. this._volumetricLightScatteringRTT.onAfterRenderObservable.add((): void => {
  289. scene.clearColor = savedSceneClearColor;
  290. });
  291. this._volumetricLightScatteringRTT.customRenderFunction = (opaqueSubMeshes: SmartArray<SubMesh>, alphaTestSubMeshes: SmartArray<SubMesh>, transparentSubMeshes: SmartArray<SubMesh>, depthOnlySubMeshes: SmartArray<SubMesh>): void => {
  292. var engine = scene.getEngine();
  293. var index: number;
  294. if (depthOnlySubMeshes.length) {
  295. engine.setColorWrite(false);
  296. for (index = 0; index < depthOnlySubMeshes.length; index++) {
  297. renderSubMesh(depthOnlySubMeshes.data[index]);
  298. }
  299. engine.setColorWrite(true);
  300. }
  301. for (index = 0; index < opaqueSubMeshes.length; index++) {
  302. renderSubMesh(opaqueSubMeshes.data[index]);
  303. }
  304. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  305. renderSubMesh(alphaTestSubMeshes.data[index]);
  306. }
  307. if (transparentSubMeshes.length) {
  308. // Sort sub meshes
  309. for (index = 0; index < transparentSubMeshes.length; index++) {
  310. var submesh = transparentSubMeshes.data[index];
  311. let boundingInfo = submesh.getBoundingInfo();
  312. if (boundingInfo && scene.activeCamera) {
  313. submesh._alphaIndex = submesh.getMesh().alphaIndex;
  314. submesh._distanceToCamera = boundingInfo.boundingSphere.centerWorld.subtract(scene.activeCamera.position).length();
  315. }
  316. }
  317. var sortedArray = transparentSubMeshes.data.slice(0, transparentSubMeshes.length);
  318. sortedArray.sort((a, b) => {
  319. // Alpha index first
  320. if (a._alphaIndex > b._alphaIndex) {
  321. return 1;
  322. }
  323. if (a._alphaIndex < b._alphaIndex) {
  324. return -1;
  325. }
  326. // Then distance to camera
  327. if (a._distanceToCamera < b._distanceToCamera) {
  328. return 1;
  329. }
  330. if (a._distanceToCamera > b._distanceToCamera) {
  331. return -1;
  332. }
  333. return 0;
  334. });
  335. // Render sub meshes
  336. engine.setAlphaMode(Engine.ALPHA_COMBINE);
  337. for (index = 0; index < sortedArray.length; index++) {
  338. renderSubMesh(sortedArray[index]);
  339. }
  340. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  341. }
  342. };
  343. }
  344. private _updateMeshScreenCoordinates(scene: Scene): void {
  345. var transform = scene.getTransformMatrix();
  346. var meshPosition: Vector3;
  347. if (this.useCustomMeshPosition) {
  348. meshPosition = this.customMeshPosition;
  349. }
  350. else if (this.attachedNode) {
  351. meshPosition = this.attachedNode.position;
  352. }
  353. else {
  354. meshPosition = this.mesh.parent ? this.mesh.getAbsolutePosition() : this.mesh.position;
  355. }
  356. var pos = Vector3.Project(meshPosition, Matrix.Identity(), transform, this._viewPort);
  357. this._screenCoordinates.x = pos.x / this._viewPort.width;
  358. this._screenCoordinates.y = pos.y / this._viewPort.height;
  359. if (this.invert) {
  360. this._screenCoordinates.y = 1.0 - this._screenCoordinates.y;
  361. }
  362. }
  363. // Static methods
  364. /**
  365. * Creates a default mesh for the Volumeric Light Scattering post-process
  366. * @param name The mesh name
  367. * @param scene The scene where to create the mesh
  368. * @return the default mesh
  369. */
  370. public static CreateDefaultMesh(name: string, scene: Scene): Mesh {
  371. var mesh = Mesh.CreatePlane(name, 1, scene);
  372. mesh.billboardMode = AbstractMesh.BILLBOARDMODE_ALL;
  373. var material = new StandardMaterial(name + "Material", scene);
  374. material.emissiveColor = new Color3(1, 1, 1);
  375. mesh.material = material;
  376. return mesh;
  377. }
  378. }