volumetricLightScatteringPostProcess.ts 18 KB

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