babylon.postProcessRenderEffect.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. module BABYLON {
  2. /**
  3. * This represents a set of one or more post processes in Babylon.
  4. * A post process can be used to apply a shader to a texture after it is rendered.
  5. * @example https://doc.babylonjs.com/how_to/how_to_use_postprocessrenderpipeline
  6. */
  7. export class PostProcessRenderEffect {
  8. private _postProcesses: { [Key: string]: Array<PostProcess> };
  9. private _getPostProcesses: () => Nullable<PostProcess | Array<PostProcess>>;
  10. private _singleInstance: boolean;
  11. private _cameras: { [key: string]: Nullable<Camera> };
  12. private _indicesForCamera: { [key: string]: number[] };
  13. /**
  14. * Name of the effect
  15. * @hidden
  16. */
  17. public _name: string;
  18. /**
  19. * Instantiates a post process render effect.
  20. * A post process can be used to apply a shader to a texture after it is rendered.
  21. * @param engine The engine the effect is tied to
  22. * @param name The name of the effect
  23. * @param getPostProcesses A function that returns a set of post processes which the effect will run in order to be run.
  24. * @param singleInstance False if this post process can be run on multiple cameras. (default: true)
  25. */
  26. constructor(engine: Engine, name: string, getPostProcesses: () => Nullable<PostProcess | Array<PostProcess>>, singleInstance?: boolean) {
  27. this._name = name;
  28. this._singleInstance = singleInstance || true;
  29. this._getPostProcesses = getPostProcesses;
  30. this._cameras = {};
  31. this._indicesForCamera = {};
  32. this._postProcesses = {};
  33. }
  34. /**
  35. * Checks if all the post processes in the effect are supported.
  36. */
  37. public get isSupported(): boolean {
  38. for (var index in this._postProcesses) {
  39. if (this._postProcesses.hasOwnProperty(index)) {
  40. let pps = this._postProcesses[index];
  41. for (var ppIndex = 0; ppIndex < pps.length; ppIndex++) {
  42. if (!pps[ppIndex].isSupported) {
  43. return false;
  44. }
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. /**
  51. * Updates the current state of the effect
  52. * @hidden
  53. */
  54. public _update(): void {
  55. }
  56. /**
  57. * Attaches the effect on cameras
  58. * @param cameras The camera to attach to.
  59. * @hidden
  60. */
  61. public _attachCameras(cameras: Camera): void;
  62. /**
  63. * Attaches the effect on cameras
  64. * @param cameras The camera to attach to.
  65. * @hidden
  66. */
  67. public _attachCameras(cameras: Camera[]): void;
  68. /**
  69. * Attaches the effect on cameras
  70. * @param cameras The camera to attach to.
  71. * @hidden
  72. */
  73. public _attachCameras(cameras: any): void {
  74. var cameraKey;
  75. var cams = Tools.MakeArray(cameras || this._cameras);
  76. if (!cams) {
  77. return;
  78. }
  79. for (var i = 0; i < cams.length; i++) {
  80. var camera = cams[i];
  81. var cameraName = camera.name;
  82. if (this._singleInstance) {
  83. cameraKey = 0;
  84. }
  85. else {
  86. cameraKey = cameraName;
  87. }
  88. if (!this._postProcesses[cameraKey]) {
  89. var postProcess = this._getPostProcesses();
  90. if (postProcess) {
  91. this._postProcesses[cameraKey] = Array.isArray(postProcess) ? postProcess : [postProcess];
  92. }
  93. }
  94. if (!this._indicesForCamera[cameraName]) {
  95. this._indicesForCamera[cameraName] = [];
  96. }
  97. this._postProcesses[cameraKey].forEach((postProcess: PostProcess) => {
  98. var index = camera.attachPostProcess(postProcess);
  99. this._indicesForCamera[cameraName].push(index);
  100. });
  101. if (!this._cameras[cameraName]) {
  102. this._cameras[cameraName] = camera;
  103. }
  104. }
  105. }
  106. /**
  107. * Detatches the effect on cameras
  108. * @param cameras The camera to detatch from.
  109. * @hidden
  110. */
  111. public _detachCameras(cameras: Camera): void;
  112. /**
  113. * Detatches the effect on cameras
  114. * @param cameras The camera to detatch from.
  115. * @hidden
  116. */
  117. public _detachCameras(cameras: Camera[]): void;
  118. /**
  119. * Detatches the effect on cameras
  120. * @param cameras The camera to detatch from.
  121. * @hidden
  122. */
  123. public _detachCameras(cameras: any): void {
  124. var cams = Tools.MakeArray(cameras || this._cameras);
  125. if (!cams) {
  126. return;
  127. }
  128. for (var i = 0; i < cams.length; i++) {
  129. var camera: Camera = cams[i];
  130. var cameraName: string = camera.name;
  131. this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess: PostProcess) => {
  132. camera.detachPostProcess(postProcess);
  133. })
  134. if (this._cameras[cameraName]) {
  135. //this._indicesForCamera.splice(index, 1);
  136. this._cameras[cameraName] = null;
  137. }
  138. }
  139. }
  140. /**
  141. * Enables the effect on given cameras
  142. * @param cameras The camera to enable.
  143. * @hidden
  144. */
  145. public _enable(cameras: Camera): void;
  146. /**
  147. * Enables the effect on given cameras
  148. * @param cameras The camera to enable.
  149. * @hidden
  150. */
  151. public _enable(cameras: Nullable<Camera[]>): void;
  152. /**
  153. * Enables the effect on given cameras
  154. * @param cameras The camera to enable.
  155. * @hidden
  156. */
  157. public _enable(cameras: any): void {
  158. var cams: Nullable<Array<Camera>> = Tools.MakeArray(cameras || this._cameras);
  159. if (!cams) {
  160. return;
  161. }
  162. for (var i = 0; i < cams.length; i++) {
  163. var camera = cams[i];
  164. var cameraName = camera.name;
  165. for (var j = 0; j < this._indicesForCamera[cameraName].length; j++) {
  166. if (camera._postProcesses[this._indicesForCamera[cameraName][j]] === undefined || camera._postProcesses[this._indicesForCamera[cameraName][j]] === null) {
  167. this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess) => {
  168. cams![i].attachPostProcess(postProcess, this._indicesForCamera[cameraName][j]);
  169. });
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. * Disables the effect on the given cameras
  176. * @param cameras The camera to disable.
  177. * @hidden
  178. */
  179. public _disable(cameras: Camera): void;
  180. /**
  181. * Disables the effect on the given cameras
  182. * @param cameras The camera to disable.
  183. * @hidden
  184. */
  185. public _disable(cameras: Nullable<Camera[]>): void;
  186. /**
  187. * Disables the effect on the given cameras
  188. * @param cameras The camera to disable.
  189. * @hidden
  190. */
  191. public _disable(cameras: any): void {
  192. var cams: Nullable<Array<Camera>> = Tools.MakeArray(cameras || this._cameras);
  193. if (!cams) {
  194. return;
  195. }
  196. for (var i = 0; i < cams.length; i++) {
  197. var camera = cams[i];
  198. var cameraName = camera.name;
  199. this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess) => {
  200. camera.detachPostProcess(postProcess);
  201. });
  202. }
  203. }
  204. /**
  205. * Gets a list of the post processes contained in the effect.
  206. * @param camera The camera to get the post processes on.
  207. * @returns The list of the post processes in the effect.
  208. */
  209. public getPostProcesses(camera?: Camera): Nullable<Array<PostProcess>> {
  210. if (this._singleInstance) {
  211. return this._postProcesses[0];
  212. }
  213. else {
  214. if (!camera) {
  215. return null;
  216. }
  217. return this._postProcesses[camera.name];
  218. }
  219. }
  220. }
  221. }