postProcessRenderEffect.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Tools } from "../../Misc/tools.js";
  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/features/featuresDeepDive/postProcesses/postProcessRenderPipeline
  6. */
  7. export class PostProcessRenderEffect {
  8. /**
  9. * Instantiates a post process render effect.
  10. * A post process can be used to apply a shader to a texture after it is rendered.
  11. * @param engine The engine the effect is tied to
  12. * @param name The name of the effect
  13. * @param getPostProcesses A function that returns a set of post processes which the effect will run in order to be run.
  14. * @param singleInstance False if this post process can be run on multiple cameras. (default: true)
  15. */
  16. constructor(engine, name, getPostProcesses, singleInstance) {
  17. this._name = name;
  18. this._singleInstance = singleInstance || true;
  19. this._getPostProcesses = getPostProcesses;
  20. this._cameras = {};
  21. this._indicesForCamera = {};
  22. this._postProcesses = {};
  23. }
  24. /**
  25. * Checks if all the post processes in the effect are supported.
  26. */
  27. get isSupported() {
  28. for (const index in this._postProcesses) {
  29. if (Object.prototype.hasOwnProperty.call(this._postProcesses, index)) {
  30. const pps = this._postProcesses[index];
  31. for (let ppIndex = 0; ppIndex < pps.length; ppIndex++) {
  32. if (!pps[ppIndex].isSupported) {
  33. return false;
  34. }
  35. }
  36. }
  37. }
  38. return true;
  39. }
  40. /**
  41. * Updates the current state of the effect
  42. * @internal
  43. */
  44. _update() { }
  45. /**
  46. * Attaches the effect on cameras
  47. * @param cameras The camera to attach to.
  48. * @internal
  49. */
  50. _attachCameras(cameras) {
  51. let cameraKey;
  52. const cams = Tools.MakeArray(cameras || this._cameras);
  53. if (!cams) {
  54. return;
  55. }
  56. for (let i = 0; i < cams.length; i++) {
  57. const camera = cams[i];
  58. if (!camera) {
  59. continue;
  60. }
  61. const cameraName = camera.name;
  62. if (this._singleInstance) {
  63. cameraKey = 0;
  64. }
  65. else {
  66. cameraKey = cameraName;
  67. }
  68. if (!this._postProcesses[cameraKey]) {
  69. const postProcess = this._getPostProcesses();
  70. if (postProcess) {
  71. this._postProcesses[cameraKey] = Array.isArray(postProcess) ? postProcess : [postProcess];
  72. }
  73. }
  74. if (!this._indicesForCamera[cameraName]) {
  75. this._indicesForCamera[cameraName] = [];
  76. }
  77. this._postProcesses[cameraKey].forEach((postProcess) => {
  78. const index = camera.attachPostProcess(postProcess);
  79. this._indicesForCamera[cameraName].push(index);
  80. });
  81. if (!this._cameras[cameraName]) {
  82. this._cameras[cameraName] = camera;
  83. }
  84. }
  85. }
  86. /**
  87. * Detaches the effect on cameras
  88. * @param cameras The camera to detach from.
  89. * @internal
  90. */
  91. _detachCameras(cameras) {
  92. const cams = Tools.MakeArray(cameras || this._cameras);
  93. if (!cams) {
  94. return;
  95. }
  96. for (let i = 0; i < cams.length; i++) {
  97. const camera = cams[i];
  98. const cameraName = camera.name;
  99. const postProcesses = this._postProcesses[this._singleInstance ? 0 : cameraName];
  100. if (postProcesses) {
  101. postProcesses.forEach((postProcess) => {
  102. camera.detachPostProcess(postProcess);
  103. });
  104. }
  105. if (this._cameras[cameraName]) {
  106. this._cameras[cameraName] = null;
  107. }
  108. delete this._indicesForCamera[cameraName];
  109. }
  110. }
  111. /**
  112. * Enables the effect on given cameras
  113. * @param cameras The camera to enable.
  114. * @internal
  115. */
  116. _enable(cameras) {
  117. const cams = Tools.MakeArray(cameras || this._cameras);
  118. if (!cams) {
  119. return;
  120. }
  121. for (let i = 0; i < cams.length; i++) {
  122. const camera = cams[i];
  123. const cameraName = camera.name;
  124. const cameraKey = this._singleInstance ? 0 : cameraName;
  125. for (let j = 0; j < this._indicesForCamera[cameraName].length; j++) {
  126. const index = this._indicesForCamera[cameraName][j];
  127. const postProcess = camera._postProcesses[index];
  128. if (postProcess === undefined || postProcess === null) {
  129. cams[i].attachPostProcess(this._postProcesses[cameraKey][j], index);
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Disables the effect on the given cameras
  136. * @param cameras The camera to disable.
  137. * @internal
  138. */
  139. _disable(cameras) {
  140. const cams = Tools.MakeArray(cameras || this._cameras);
  141. if (!cams) {
  142. return;
  143. }
  144. for (let i = 0; i < cams.length; i++) {
  145. const camera = cams[i];
  146. const cameraName = camera.name;
  147. this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess) => {
  148. camera.detachPostProcess(postProcess);
  149. });
  150. }
  151. }
  152. /**
  153. * Gets a list of the post processes contained in the effect.
  154. * @param camera The camera to get the post processes on.
  155. * @returns The list of the post processes in the effect.
  156. */
  157. getPostProcesses(camera) {
  158. if (this._singleInstance) {
  159. return this._postProcesses[0];
  160. }
  161. else {
  162. if (!camera) {
  163. return null;
  164. }
  165. return this._postProcesses[camera.name];
  166. }
  167. }
  168. }
  169. //# sourceMappingURL=postProcessRenderEffect.js.map