|
@@ -1,25 +1,67 @@
|
|
|
module BABYLON {
|
|
|
export type PostProcessOptions = { width: number, height: number };
|
|
|
|
|
|
+ /**
|
|
|
+ * PostProcess can be used to apply a shader to a texture after it has been rendered
|
|
|
+ * See https://doc.babylonjs.com/how_to/how_to_use_postprocesses
|
|
|
+ */
|
|
|
export class PostProcess {
|
|
|
+ /**
|
|
|
+ * Width of the texture to apply the post process on
|
|
|
+ */
|
|
|
public width = -1;
|
|
|
+ /**
|
|
|
+ * Height of the texture to apply the post process on
|
|
|
+ */
|
|
|
public height = -1;
|
|
|
+ /**
|
|
|
+ * Sampling mode used by the shader
|
|
|
+ * See https://doc.babylonjs.com/classes/3.1/texture
|
|
|
+ */
|
|
|
public renderTargetSamplingMode: number;
|
|
|
+ /**
|
|
|
+ * Clear color to use when screen clearing
|
|
|
+ */
|
|
|
public clearColor: Color4;
|
|
|
+ /**
|
|
|
+ * If the buffer needs to be cleared before applying the post process. (default: true)
|
|
|
+ * Should be set to false if shader will overwrite all previous pixels.
|
|
|
+ */
|
|
|
public autoClear = true;
|
|
|
+ /**
|
|
|
+ * Type of alpha mode to use when performing the post process (default: Engine.ALPHA_DISABLE)
|
|
|
+ */
|
|
|
public alphaMode = Engine.ALPHA_DISABLE;
|
|
|
+ /**
|
|
|
+ * Sets the setAlphaBlendConstants of the babylon engine
|
|
|
+ */
|
|
|
public alphaConstants: Color4;
|
|
|
+ /**
|
|
|
+ * Animations to be used for the post processing
|
|
|
+ */
|
|
|
public animations = new Array<Animation>();
|
|
|
|
|
|
- /*
|
|
|
- Enable Pixel Perfect mode where texture is not scaled to be power of 2.
|
|
|
- Can only be used on a single postprocess or on the last one of a chain.
|
|
|
- */
|
|
|
+ /**
|
|
|
+ * Enable Pixel Perfect mode where texture is not scaled to be power of 2.
|
|
|
+ * Can only be used on a single postprocess or on the last one of a chain. (default: false)
|
|
|
+ */
|
|
|
public enablePixelPerfectMode = false;
|
|
|
|
|
|
+ /**
|
|
|
+ * Scale mode for the post process (default: Engine.SCALEMODE_FLOOR)
|
|
|
+ */
|
|
|
public scaleMode = Engine.SCALEMODE_FLOOR;
|
|
|
+ /**
|
|
|
+ * Force textures to be a power of two (default: false)
|
|
|
+ */
|
|
|
public alwaysForcePOT = false;
|
|
|
+ /**
|
|
|
+ * Number of sample textures (default: 1)
|
|
|
+ */
|
|
|
public samples = 1;
|
|
|
+ /**
|
|
|
+ * Modify the scale of the post process to be the same as the viewport (default: false)
|
|
|
+ */
|
|
|
public adaptScaleToCurrentViewport = false;
|
|
|
|
|
|
private _camera: Camera;
|
|
@@ -28,7 +70,13 @@
|
|
|
private _options: number | PostProcessOptions;
|
|
|
private _reusable = false;
|
|
|
private _textureType: number;
|
|
|
+ /**
|
|
|
+ * Smart array of input and output textures for the post process.
|
|
|
+ */
|
|
|
public _textures = new SmartArray<InternalTexture>(2);
|
|
|
+ /**
|
|
|
+ * The index in _textures that corresponds to the output texture.
|
|
|
+ */
|
|
|
public _currentRenderTextureInd = 0;
|
|
|
private _effect: Effect;
|
|
|
private _samplers: string[];
|
|
@@ -50,6 +98,9 @@
|
|
|
public onActivateObservable = new Observable<Camera>();
|
|
|
|
|
|
private _onActivateObserver: Nullable<Observer<Camera>>;
|
|
|
+ /**
|
|
|
+ * A function that is added to the onActivateObservable
|
|
|
+ */
|
|
|
public set onActivate(callback: Nullable<(camera: Camera) => void>) {
|
|
|
if (this._onActivateObserver) {
|
|
|
this.onActivateObservable.remove(this._onActivateObserver);
|
|
@@ -66,6 +117,9 @@
|
|
|
public onSizeChangedObservable = new Observable<PostProcess>();
|
|
|
|
|
|
private _onSizeChangedObserver: Nullable<Observer<PostProcess>>;
|
|
|
+ /**
|
|
|
+ * A function that is added to the onSizeChangedObservable
|
|
|
+ */
|
|
|
public set onSizeChanged(callback: (postProcess: PostProcess) => void) {
|
|
|
if (this._onSizeChangedObserver) {
|
|
|
this.onSizeChangedObservable.remove(this._onSizeChangedObserver);
|
|
@@ -80,6 +134,9 @@
|
|
|
public onApplyObservable = new Observable<Effect>();
|
|
|
|
|
|
private _onApplyObserver: Nullable<Observer<Effect>>;
|
|
|
+ /**
|
|
|
+ * A function that is added to the onApplyObservable
|
|
|
+ */
|
|
|
public set onApply(callback: (effect: Effect) => void) {
|
|
|
if (this._onApplyObserver) {
|
|
|
this.onApplyObservable.remove(this._onApplyObserver);
|
|
@@ -94,6 +151,9 @@
|
|
|
public onBeforeRenderObservable = new Observable<Effect>();
|
|
|
|
|
|
private _onBeforeRenderObserver: Nullable<Observer<Effect>>;
|
|
|
+ /**
|
|
|
+ * A function that is added to the onBeforeRenderObservable
|
|
|
+ */
|
|
|
public set onBeforeRender(callback: (effect: Effect) => void) {
|
|
|
if (this._onBeforeRenderObserver) {
|
|
|
this.onBeforeRenderObservable.remove(this._onBeforeRenderObserver);
|
|
@@ -108,6 +168,9 @@
|
|
|
public onAfterRenderObservable = new Observable<Effect>();
|
|
|
|
|
|
private _onAfterRenderObserver: Nullable<Observer<Effect>>;
|
|
|
+ /**
|
|
|
+ * A function that is added to the onAfterRenderObservable
|
|
|
+ */
|
|
|
public set onAfterRender(callback: (efect: Effect) => void) {
|
|
|
if (this._onAfterRenderObserver) {
|
|
|
this.onAfterRenderObservable.remove(this._onAfterRenderObserver);
|
|
@@ -115,6 +178,9 @@
|
|
|
this._onAfterRenderObserver = this.onAfterRenderObservable.add(callback);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The resulting output of the post process.
|
|
|
+ */
|
|
|
public get outputTexture(): InternalTexture {
|
|
|
return this._textures.data[this._currentRenderTextureInd];
|
|
|
}
|
|
@@ -123,10 +189,18 @@
|
|
|
this._forcedOutputTexture = value;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the camera which post process is applied to.
|
|
|
+ * @returns The camera the post process is applied to.
|
|
|
+ */
|
|
|
public getCamera(): Camera {
|
|
|
return this._camera;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the texel size of the postprocess.
|
|
|
+ * See https://en.wikipedia.org/wiki/Texel_(graphics)
|
|
|
+ */
|
|
|
public get texelSize(): Vector2 {
|
|
|
if (this._shareOutputWithPostProcess) {
|
|
|
return this._shareOutputWithPostProcess.texelSize;
|
|
@@ -139,7 +213,24 @@
|
|
|
return this._texelSize;
|
|
|
}
|
|
|
|
|
|
- constructor(public name: string, fragmentUrl: string, parameters: Nullable<string[]>, samplers: Nullable<string[]>, options: number | PostProcessOptions, camera: Nullable<Camera>,
|
|
|
+ /**
|
|
|
+ * Creates a new instance of @see PostProcess
|
|
|
+ * @param name The name of the PostProcess.
|
|
|
+ * @param fragmentUrl The url of the fragment shader to be used.
|
|
|
+ * @param parameters Array of the names of uniform non-sampler2D variables that will be passed to the shader.
|
|
|
+ * @param samplers Array of the names of uniform sampler2D variables that will be passed to the shader.
|
|
|
+ * @param options The required width/height ratio to downsize to before computing the render pass. (Use 1.0 for full size)
|
|
|
+ * @param camera The camera to apply the render pass to.
|
|
|
+ * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
|
|
|
+ * @param engine The engine which the post process will be applied. (default: current engine)
|
|
|
+ * @param reusable If the post process can be reused on the same frame. (default: false)
|
|
|
+ * @param defines String of defines that will be set when running the fragment shader. (default: null)
|
|
|
+ * @param textureType Type of textures used when performing the post process. (default: 0)
|
|
|
+ * @param vertexUrl The url of the vertex shader to be used. (default: "postprocess")
|
|
|
+ * @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
|
|
|
+ * @param blockCompilation If the shader should be compiled imediatly. (default: false)
|
|
|
+ */
|
|
|
+ constructor(/** Name of the PostProcess. */public name: string, fragmentUrl: string, parameters: Nullable<string[]>, samplers: Nullable<string[]>, options: number | PostProcessOptions, camera: Nullable<Camera>,
|
|
|
samplingMode: number = Texture.NEAREST_SAMPLINGMODE, engine?: Engine, reusable?: boolean, defines: Nullable<string> = null, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, vertexUrl: string = "postprocess", indexParameters?: any, blockCompilation = false) {
|
|
|
if (camera != null) {
|
|
|
this._camera = camera;
|
|
@@ -175,14 +266,27 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the engine which this post process belongs to.
|
|
|
+ * @returns The engine the post process was enabled with.
|
|
|
+ */
|
|
|
public getEngine(): Engine {
|
|
|
return this._engine;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The effect that is created when initializing the post process.
|
|
|
+ * @returns The created effect corrisponding the the postprocess.
|
|
|
+ */
|
|
|
public getEffect(): Effect {
|
|
|
return this._effect;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * To avoid multiple redundant textures for multiple post process, the output the output texture for this post process can be shared with another.
|
|
|
+ * @param postProcess The post process to share the output with.
|
|
|
+ * @returns This post process.
|
|
|
+ */
|
|
|
public shareOutputWith(postProcess: PostProcess): PostProcess {
|
|
|
this._disposeTextures();
|
|
|
|
|
@@ -191,6 +295,15 @@
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Updates the effect with the current post process compile time values and recompiles the shader.
|
|
|
+ * @param defines Define statements that should be added at the beginning of the shader. (default: null)
|
|
|
+ * @param uniforms Set of uniform variables that will be passed to the shader. (default: null)
|
|
|
+ * @param samplers Set of Texture2D variables that will be passed to the shader. (default: null)
|
|
|
+ * @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
|
|
|
+ * @param onCompiled Called when the shader has been compiled.
|
|
|
+ * @param onError Called if there is an error when compiling a shader.
|
|
|
+ */
|
|
|
public updateEffect(defines: Nullable<string> = null, uniforms: Nullable<string[]> = null, samplers: Nullable<string[]> = null, indexParameters?: any,
|
|
|
onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void) {
|
|
|
this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl },
|
|
@@ -205,6 +318,10 @@
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The post process is reusable if it can be used multiple times within one frame.
|
|
|
+ * @returns If the post process is reusable
|
|
|
+ */
|
|
|
public isReusable(): boolean {
|
|
|
return this._reusable;
|
|
|
}
|
|
@@ -214,6 +331,12 @@
|
|
|
this.width = -1;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Activates the post process by intializing the textures to be used when executed. Notifies onActivateObservable.
|
|
|
+ * @param camera The camera that will be used in the post process. This camera will be used when calling onActivateObservable.
|
|
|
+ * @param sourceTexture The source texture to be inspected to get the width and height if not specified in the post process constructor. (default: null)
|
|
|
+ * @param forceDepthStencil If true, a depth and stencil buffer will be generated. (default: false)
|
|
|
+ */
|
|
|
public activate(camera: Nullable<Camera>, sourceTexture: Nullable<InternalTexture> = null, forceDepthStencil?: boolean): void {
|
|
|
camera = camera || this._camera;
|
|
|
|
|
@@ -319,10 +442,17 @@
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * If the post process is supported.
|
|
|
+ */
|
|
|
public get isSupported(): boolean {
|
|
|
return this._effect.isSupported;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The aspect ratio of the output texture.
|
|
|
+ */
|
|
|
public get aspectRatio(): number {
|
|
|
if (this._shareOutputWithPostProcess) {
|
|
|
return this._shareOutputWithPostProcess.aspectRatio;
|
|
@@ -342,6 +472,10 @@
|
|
|
return this._effect && this._effect.isReady();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Binds all textures and uniforms to the shader, this will be run on every pass.
|
|
|
+ * @returns the effect corrisponding to this post process. Null if not compiled or not ready.
|
|
|
+ */
|
|
|
public apply(): Nullable<Effect> {
|
|
|
// Check
|
|
|
if (!this._effect || !this._effect.isReady())
|
|
@@ -390,6 +524,10 @@
|
|
|
this._textures.dispose();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Disposes the post process.
|
|
|
+ * @param camera The camera to dispose the post process on.
|
|
|
+ */
|
|
|
public dispose(camera?: Camera): void {
|
|
|
camera = camera || this._camera;
|
|
|
|