babylon.postProcess.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var PostProcess = (function () {
  4. function PostProcess(name, fragmentUrl, parameters, samplers, options, camera, samplingMode, engine, reusable, defines, textureType) {
  5. if (samplingMode === void 0) { samplingMode = BABYLON.Texture.NEAREST_SAMPLINGMODE; }
  6. if (textureType === void 0) { textureType = BABYLON.Engine.TEXTURETYPE_UNSIGNED_INT; }
  7. this.name = name;
  8. this.width = -1;
  9. this.height = -1;
  10. /*
  11. Enable Pixel Perfect mode where texture is not scaled to be power of 2.
  12. Can only be used on a single postprocess or on the last one of a chain.
  13. */
  14. this.enablePixelPerfectMode = false;
  15. this._reusable = false;
  16. this._textures = new BABYLON.SmartArray(2);
  17. this._currentRenderTextureInd = 0;
  18. this._scaleRatio = new BABYLON.Vector2(1, 1);
  19. // Events
  20. /**
  21. * An event triggered when the postprocess is activated.
  22. * @type {BABYLON.Observable}
  23. */
  24. this.onActivateObservable = new BABYLON.Observable();
  25. /**
  26. * An event triggered when the postprocess changes its size.
  27. * @type {BABYLON.Observable}
  28. */
  29. this.onSizeChangedObservable = new BABYLON.Observable();
  30. /**
  31. * An event triggered when the postprocess applies its effect.
  32. * @type {BABYLON.Observable}
  33. */
  34. this.onApplyObservable = new BABYLON.Observable();
  35. /**
  36. * An event triggered before rendering the postprocess
  37. * @type {BABYLON.Observable}
  38. */
  39. this.onBeforeRenderObservable = new BABYLON.Observable();
  40. /**
  41. * An event triggered after rendering the postprocess
  42. * @type {BABYLON.Observable}
  43. */
  44. this.onAfterRenderObservable = new BABYLON.Observable();
  45. if (camera != null) {
  46. this._camera = camera;
  47. this._scene = camera.getScene();
  48. camera.attachPostProcess(this);
  49. this._engine = this._scene.getEngine();
  50. }
  51. else {
  52. this._engine = engine;
  53. }
  54. this._options = options;
  55. this.renderTargetSamplingMode = samplingMode ? samplingMode : BABYLON.Texture.NEAREST_SAMPLINGMODE;
  56. this._reusable = reusable || false;
  57. this._textureType = textureType;
  58. this._samplers = samplers || [];
  59. this._samplers.push("textureSampler");
  60. this._fragmentUrl = fragmentUrl;
  61. this._parameters = parameters || [];
  62. this._parameters.push("scale");
  63. this.updateEffect(defines);
  64. }
  65. Object.defineProperty(PostProcess.prototype, "onActivate", {
  66. set: function (callback) {
  67. if (this._onActivateObserver) {
  68. this.onActivateObservable.remove(this._onActivateObserver);
  69. }
  70. this._onActivateObserver = this.onActivateObservable.add(callback);
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(PostProcess.prototype, "onSizeChanged", {
  76. set: function (callback) {
  77. if (this._onSizeChangedObserver) {
  78. this.onSizeChangedObservable.remove(this._onSizeChangedObserver);
  79. }
  80. this._onSizeChangedObserver = this.onSizeChangedObservable.add(callback);
  81. },
  82. enumerable: true,
  83. configurable: true
  84. });
  85. Object.defineProperty(PostProcess.prototype, "onApply", {
  86. set: function (callback) {
  87. if (this._onApplyObserver) {
  88. this.onApplyObservable.remove(this._onApplyObserver);
  89. }
  90. this._onApplyObserver = this.onApplyObservable.add(callback);
  91. },
  92. enumerable: true,
  93. configurable: true
  94. });
  95. Object.defineProperty(PostProcess.prototype, "onBeforeRender", {
  96. set: function (callback) {
  97. if (this._onBeforeRenderObserver) {
  98. this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
  99. }
  100. this._onBeforeRenderObserver = this.onBeforeRenderObservable.add(callback);
  101. },
  102. enumerable: true,
  103. configurable: true
  104. });
  105. Object.defineProperty(PostProcess.prototype, "onAfterRender", {
  106. set: function (callback) {
  107. if (this._onAfterRenderObserver) {
  108. this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
  109. }
  110. this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
  111. },
  112. enumerable: true,
  113. configurable: true
  114. });
  115. PostProcess.prototype.updateEffect = function (defines) {
  116. this._effect = this._engine.createEffect({ vertex: "postprocess", fragment: this._fragmentUrl }, ["position"], this._parameters, this._samplers, defines !== undefined ? defines : "");
  117. };
  118. PostProcess.prototype.isReusable = function () {
  119. return this._reusable;
  120. };
  121. /** invalidate frameBuffer to hint the postprocess to create a depth buffer */
  122. PostProcess.prototype.markTextureDirty = function () {
  123. this.width = -1;
  124. };
  125. PostProcess.prototype.activate = function (camera, sourceTexture) {
  126. camera = camera || this._camera;
  127. var scene = camera.getScene();
  128. var maxSize = camera.getEngine().getCaps().maxTextureSize;
  129. var requiredWidth = ((sourceTexture ? sourceTexture._width : this._engine.getRenderingCanvas().width) * this._options) | 0;
  130. var requiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._options) | 0;
  131. var desiredWidth = this._options.width || requiredWidth;
  132. var desiredHeight = this._options.height || requiredHeight;
  133. if (this.renderTargetSamplingMode !== BABYLON.Texture.NEAREST_SAMPLINGMODE) {
  134. if (!this._options.width) {
  135. desiredWidth = BABYLON.Tools.GetExponentOfTwo(desiredWidth, maxSize);
  136. }
  137. if (!this._options.height) {
  138. desiredHeight = BABYLON.Tools.GetExponentOfTwo(desiredHeight, maxSize);
  139. }
  140. }
  141. if (this.width !== desiredWidth || this.height !== desiredHeight) {
  142. if (this._textures.length > 0) {
  143. for (var i = 0; i < this._textures.length; i++) {
  144. this._engine._releaseTexture(this._textures.data[i]);
  145. }
  146. this._textures.reset();
  147. }
  148. this.width = desiredWidth;
  149. this.height = desiredHeight;
  150. this._textures.push(this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: camera._postProcesses.indexOf(this) === 0, samplingMode: this.renderTargetSamplingMode, type: this._textureType }));
  151. if (this._reusable) {
  152. this._textures.push(this._engine.createRenderTargetTexture({ width: this.width, height: this.height }, { generateMipMaps: false, generateDepthBuffer: camera._postProcesses.indexOf(this) === 0, samplingMode: this.renderTargetSamplingMode, type: this._textureType }));
  153. }
  154. this.onSizeChangedObservable.notifyObservers(this);
  155. }
  156. if (this.enablePixelPerfectMode) {
  157. this._scaleRatio.copyFromFloats(requiredWidth / desiredWidth, requiredHeight / desiredHeight);
  158. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd], 0, requiredWidth, requiredHeight);
  159. }
  160. else {
  161. this._scaleRatio.copyFromFloats(1, 1);
  162. this._engine.bindFramebuffer(this._textures.data[this._currentRenderTextureInd]);
  163. }
  164. this.onActivateObservable.notifyObservers(camera);
  165. // Clear
  166. if (this.clearColor) {
  167. this._engine.clear(this.clearColor, true, true);
  168. }
  169. else {
  170. this._engine.clear(scene.clearColor, scene.autoClear || scene.forceWireframe, true);
  171. }
  172. if (this._reusable) {
  173. this._currentRenderTextureInd = (this._currentRenderTextureInd + 1) % 2;
  174. }
  175. };
  176. Object.defineProperty(PostProcess.prototype, "isSupported", {
  177. get: function () {
  178. return this._effect.isSupported;
  179. },
  180. enumerable: true,
  181. configurable: true
  182. });
  183. PostProcess.prototype.apply = function () {
  184. // Check
  185. if (!this._effect.isReady())
  186. return null;
  187. // States
  188. this._engine.enableEffect(this._effect);
  189. this._engine.setState(false);
  190. this._engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  191. this._engine.setDepthBuffer(false);
  192. this._engine.setDepthWrite(false);
  193. // Texture
  194. this._effect._bindTexture("textureSampler", this._textures.data[this._currentRenderTextureInd]);
  195. // Parameters
  196. this._effect.setVector2("scale", this._scaleRatio);
  197. this.onApplyObservable.notifyObservers(this._effect);
  198. return this._effect;
  199. };
  200. PostProcess.prototype.dispose = function (camera) {
  201. camera = camera || this._camera;
  202. if (this._textures.length > 0) {
  203. for (var i = 0; i < this._textures.length; i++) {
  204. this._engine._releaseTexture(this._textures.data[i]);
  205. }
  206. this._textures.reset();
  207. }
  208. if (!camera) {
  209. return;
  210. }
  211. camera.detachPostProcess(this);
  212. var index = camera._postProcesses.indexOf(this);
  213. if (index === 0 && camera._postProcesses.length > 0) {
  214. this._camera._postProcesses[0].markTextureDirty();
  215. }
  216. this.onActivateObservable.clear();
  217. this.onAfterRenderObservable.clear();
  218. this.onApplyObservable.clear();
  219. this.onBeforeRenderObservable.clear();
  220. this.onSizeChangedObservable.clear();
  221. };
  222. return PostProcess;
  223. })();
  224. BABYLON.PostProcess = PostProcess;
  225. })(BABYLON || (BABYLON = {}));