babylon.postProcessRenderEffect.ts 8.3 KB

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