123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- import { Nullable } from "../types";
- import { Observer } from "../Misc/observable";
- import { serialize } from "../Misc/decorators";
- import { Color4 } from "../Maths/math.color";
- import { Camera } from "../Cameras/camera";
- import { BaseTexture } from "../Materials/Textures/baseTexture";
- import { Effect } from "../Materials/effect";
- import { ColorCurves } from "../Materials/colorCurves";
- import { ImageProcessingConfiguration, IImageProcessingConfigurationDefines } from "../Materials/imageProcessingConfiguration";
- import { PostProcess, PostProcessOptions } from "./postProcess";
- import { Engine } from "../Engines/engine";
- import { EngineStore } from "../Engines/engineStore";
- import { Constants } from "../Engines/constants";
- import "../Shaders/imageProcessing.fragment";
- import "../Shaders/postprocess.vertex";
- /**
- * ImageProcessingPostProcess
- * @see https://doc.babylonjs.com/how_to/how_to_use_postprocesses#imageprocessing
- */
- export class ImageProcessingPostProcess extends PostProcess {
- /**
- * Default configuration related to image processing available in the PBR Material.
- */
- protected _imageProcessingConfiguration: ImageProcessingConfiguration;
- /**
- * Gets the image processing configuration used either in this material.
- */
- public get imageProcessingConfiguration(): ImageProcessingConfiguration {
- return this._imageProcessingConfiguration;
- }
- /**
- * Sets the Default image processing configuration used either in the this material.
- *
- * If sets to null, the scene one is in use.
- */
- public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
- // We are almost sure it is applied by post process as
- // We are in the post process :-)
- value.applyByPostProcess = true;
- this._attachImageProcessingConfiguration(value);
- }
- /**
- * Keep track of the image processing observer to allow dispose and replace.
- */
- private _imageProcessingObserver: Nullable<Observer<ImageProcessingConfiguration>>;
- /**
- * Attaches a new image processing configuration to the PBR Material.
- * @param configuration
- */
- protected _attachImageProcessingConfiguration(configuration: Nullable<ImageProcessingConfiguration>, doNotBuild = false): void {
- if (configuration === this._imageProcessingConfiguration) {
- return;
- }
- // Detaches observer.
- if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
- this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
- }
- // Pick the scene configuration if needed.
- if (!configuration) {
- var scene = null;
- var engine = this.getEngine();
- var camera = this.getCamera();
- if (camera) {
- scene = camera.getScene();
- }
- else if (engine && engine.scenes) {
- var scenes = engine.scenes;
- scene = scenes[scenes.length - 1];
- }
- else {
- scene = EngineStore.LastCreatedScene;
- }
- if (scene) {
- this._imageProcessingConfiguration = scene.imageProcessingConfiguration;
- }
- else {
- this._imageProcessingConfiguration = new ImageProcessingConfiguration();
- }
- }
- else {
- this._imageProcessingConfiguration = configuration;
- }
- // Attaches observer.
- if (this._imageProcessingConfiguration) {
- this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(() => {
- this._updateParameters();
- });
- }
- // Ensure the effect will be rebuilt.
- if (!doNotBuild) {
- this._updateParameters();
- }
- }
- /**
- * If the post process is supported.
- */
- public get isSupported(): boolean {
- const effect = this.getEffect();
- return !effect || effect.isSupported;
- }
- /**
- * Gets Color curves setup used in the effect if colorCurvesEnabled is set to true .
- */
- public get colorCurves(): Nullable<ColorCurves> {
- return this.imageProcessingConfiguration.colorCurves;
- }
- /**
- * Sets Color curves setup used in the effect if colorCurvesEnabled is set to true .
- */
- public set colorCurves(value: Nullable<ColorCurves>) {
- this.imageProcessingConfiguration.colorCurves = value;
- }
- /**
- * Gets wether the color curves effect is enabled.
- */
- public get colorCurvesEnabled(): boolean {
- return this.imageProcessingConfiguration.colorCurvesEnabled;
- }
- /**
- * Sets wether the color curves effect is enabled.
- */
- public set colorCurvesEnabled(value: boolean) {
- this.imageProcessingConfiguration.colorCurvesEnabled = value;
- }
- /**
- * Gets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
- */
- public get colorGradingTexture(): Nullable<BaseTexture> {
- return this.imageProcessingConfiguration.colorGradingTexture;
- }
- /**
- * Sets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
- */
- public set colorGradingTexture(value: Nullable<BaseTexture>) {
- this.imageProcessingConfiguration.colorGradingTexture = value;
- }
- /**
- * Gets wether the color grading effect is enabled.
- */
- public get colorGradingEnabled(): boolean {
- return this.imageProcessingConfiguration.colorGradingEnabled;
- }
- /**
- * Gets wether the color grading effect is enabled.
- */
- public set colorGradingEnabled(value: boolean) {
- this.imageProcessingConfiguration.colorGradingEnabled = value;
- }
- /**
- * Gets exposure used in the effect.
- */
- public get exposure(): number {
- return this.imageProcessingConfiguration.exposure;
- }
- /**
- * Sets exposure used in the effect.
- */
- public set exposure(value: number) {
- this.imageProcessingConfiguration.exposure = value;
- }
- /**
- * Gets wether tonemapping is enabled or not.
- */
- public get toneMappingEnabled(): boolean {
- return this._imageProcessingConfiguration.toneMappingEnabled;
- }
- /**
- * Sets wether tonemapping is enabled or not
- */
- public set toneMappingEnabled(value: boolean) {
- this._imageProcessingConfiguration.toneMappingEnabled = value;
- }
- /**
- * Gets the type of tone mapping effect.
- */
- public get toneMappingType(): number {
- return this._imageProcessingConfiguration.toneMappingType;
- }
- /**
- * Sets the type of tone mapping effect.
- */
- public set toneMappingType(value: number) {
- this._imageProcessingConfiguration.toneMappingType = value;
- }
- /**
- * Gets contrast used in the effect.
- */
- public get contrast(): number {
- return this.imageProcessingConfiguration.contrast;
- }
- /**
- * Sets contrast used in the effect.
- */
- public set contrast(value: number) {
- this.imageProcessingConfiguration.contrast = value;
- }
- /**
- * Gets Vignette stretch size.
- */
- public get vignetteStretch(): number {
- return this.imageProcessingConfiguration.vignetteStretch;
- }
- /**
- * Sets Vignette stretch size.
- */
- public set vignetteStretch(value: number) {
- this.imageProcessingConfiguration.vignetteStretch = value;
- }
- /**
- * Gets Vignette centre X Offset.
- */
- public get vignetteCentreX(): number {
- return this.imageProcessingConfiguration.vignetteCentreX;
- }
- /**
- * Sets Vignette centre X Offset.
- */
- public set vignetteCentreX(value: number) {
- this.imageProcessingConfiguration.vignetteCentreX = value;
- }
- /**
- * Gets Vignette centre Y Offset.
- */
- public get vignetteCentreY(): number {
- return this.imageProcessingConfiguration.vignetteCentreY;
- }
- /**
- * Sets Vignette centre Y Offset.
- */
- public set vignetteCentreY(value: number) {
- this.imageProcessingConfiguration.vignetteCentreY = value;
- }
- /**
- * Gets Vignette weight or intensity of the vignette effect.
- */
- public get vignetteWeight(): number {
- return this.imageProcessingConfiguration.vignetteWeight;
- }
- /**
- * Sets Vignette weight or intensity of the vignette effect.
- */
- public set vignetteWeight(value: number) {
- this.imageProcessingConfiguration.vignetteWeight = value;
- }
- /**
- * Gets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
- * if vignetteEnabled is set to true.
- */
- public get vignetteColor(): Color4 {
- return this.imageProcessingConfiguration.vignetteColor;
- }
- /**
- * Sets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
- * if vignetteEnabled is set to true.
- */
- public set vignetteColor(value: Color4) {
- this.imageProcessingConfiguration.vignetteColor = value;
- }
- /**
- * Gets Camera field of view used by the Vignette effect.
- */
- public get vignetteCameraFov(): number {
- return this.imageProcessingConfiguration.vignetteCameraFov;
- }
- /**
- * Sets Camera field of view used by the Vignette effect.
- */
- public set vignetteCameraFov(value: number) {
- this.imageProcessingConfiguration.vignetteCameraFov = value;
- }
- /**
- * Gets the vignette blend mode allowing different kind of effect.
- */
- public get vignetteBlendMode(): number {
- return this.imageProcessingConfiguration.vignetteBlendMode;
- }
- /**
- * Sets the vignette blend mode allowing different kind of effect.
- */
- public set vignetteBlendMode(value: number) {
- this.imageProcessingConfiguration.vignetteBlendMode = value;
- }
- /**
- * Gets wether the vignette effect is enabled.
- */
- public get vignetteEnabled(): boolean {
- return this.imageProcessingConfiguration.vignetteEnabled;
- }
- /**
- * Sets wether the vignette effect is enabled.
- */
- public set vignetteEnabled(value: boolean) {
- this.imageProcessingConfiguration.vignetteEnabled = value;
- }
- @serialize()
- private _fromLinearSpace = true;
- /**
- * Gets wether the input of the processing is in Gamma or Linear Space.
- */
- public get fromLinearSpace(): boolean {
- return this._fromLinearSpace;
- }
- /**
- * Sets wether the input of the processing is in Gamma or Linear Space.
- */
- public set fromLinearSpace(value: boolean) {
- if (this._fromLinearSpace === value) {
- return;
- }
- this._fromLinearSpace = value;
- this._updateParameters();
- }
- /**
- * Defines cache preventing GC.
- */
- private _defines: IImageProcessingConfigurationDefines & { FROMLINEARSPACE: boolean } = {
- IMAGEPROCESSING: false,
- VIGNETTE: false,
- VIGNETTEBLENDMODEMULTIPLY: false,
- VIGNETTEBLENDMODEOPAQUE: false,
- TONEMAPPING: false,
- TONEMAPPING_ACES: false,
- CONTRAST: false,
- COLORCURVES: false,
- COLORGRADING: false,
- COLORGRADING3D: false,
- FROMLINEARSPACE: false,
- SAMPLER3DGREENDEPTH: false,
- SAMPLER3DBGRMAP: false,
- IMAGEPROCESSINGPOSTPROCESS: false,
- EXPOSURE: false,
- };
- constructor(name: string, options: number | PostProcessOptions, camera: Nullable<Camera> = null, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, imageProcessingConfiguration?: ImageProcessingConfiguration) {
- super(name, "imageProcessing", [], [], options, camera, samplingMode, engine, reusable,
- null, textureType, "postprocess", null, true);
- // Setup the configuration as forced by the constructor. This would then not force the
- // scene materials output in linear space and let untouched the default forward pass.
- if (imageProcessingConfiguration) {
- imageProcessingConfiguration.applyByPostProcess = true;
- this._attachImageProcessingConfiguration(imageProcessingConfiguration, true);
- // This will cause the shader to be compiled
- this.fromLinearSpace = false;
- }
- // Setup the default processing configuration to the scene.
- else {
- this._attachImageProcessingConfiguration(null, true);
- this.imageProcessingConfiguration.applyByPostProcess = true;
- }
- this.onApply = (effect: Effect) => {
- this.imageProcessingConfiguration.bind(effect, this.aspectRatio);
- };
- }
- /**
- * "ImageProcessingPostProcess"
- * @returns "ImageProcessingPostProcess"
- */
- public getClassName(): string {
- return "ImageProcessingPostProcess";
- }
- protected _updateParameters(): void {
- this._defines.FROMLINEARSPACE = this._fromLinearSpace;
- this.imageProcessingConfiguration.prepareDefines(this._defines, true);
- var defines = "";
- for (const define in this._defines) {
- if ((<any>this._defines)[define]) {
- defines += `#define ${define};\r\n`;
- }
- }
- var samplers = ["textureSampler"];
- var uniforms = ["scale"];
- if (ImageProcessingConfiguration) {
- ImageProcessingConfiguration.PrepareSamplers(samplers, this._defines);
- ImageProcessingConfiguration.PrepareUniforms(uniforms, this._defines);
- }
- this.updateEffect(defines, uniforms, samplers);
- }
- public dispose(camera?: Camera): void {
- super.dispose(camera);
- if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
- this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
- }
- if (this._imageProcessingConfiguration) {
- this.imageProcessingConfiguration.applyByPostProcess = false;
- }
- }
- }
|