babylon.postProcessRenderEffect.ts 8.5 KB

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