import { Vector2 } from '../babylonjs-import';
import { DITHER_SAMPLER, DITHER_SIZE, DITHERING, RESOLUTION, DITHER_DISTANCE_FADE } from '../ShaderConstants';
import { CullingMaterial } from './CullingMaterial';
export class DitheringMaterial extends CullingMaterial {
    ditherTexture;
    _ditherSize;
    _isDitherModeEnabled = true;
    constructor(name, scene, renderingInfo, options, ditherTexture, writeColorBlending, excludeIgnoreDepthWriteCuttingPlanes) {
        super(name, scene, renderingInfo, options, writeColorBlending, excludeIgnoreDepthWriteCuttingPlanes);
        this.ditherTexture = ditherTexture;
        this._ditherSize = ditherTexture.getTexture(scene).getSize().width;
        this.ditherTexture = ditherTexture;
        this.visualSettings.ifc.transparency.onPropertyChanged.add((change) => {
            if (change.property === 'ditheringOptions') {
                // Updates the dither texture to calculate the new frames, no need to mark this material as dirty since all materials listen to changes in visualsettings
                this.ditherTexture.currentMode = this.visualSettings.ifc.transparency.ditheringOptions.mode;
                this.ditherTexture.animatedNoiseTextures =
                    this.visualSettings.ifc.transparency.ditheringOptions.animatedFrames;
            }
        });
    }
    getUniformsAndDefinesAndSamplers() {
        const defines = [];
        const uniforms = [];
        const samplers = [];
        const uniformsAndDefines = super.getUniformsAndDefinesAndSamplers();
        if (this._isDitherModeEnabled) {
            defines.push(DITHERING);
            uniforms.push(...[RESOLUTION, DITHER_SIZE]);
            samplers.push(DITHER_SAMPLER);
        }
        return {
            uniforms: [...uniforms, ...uniformsAndDefines.uniforms],
            defines: [...defines, ...uniformsAndDefines.defines],
            samplers: [...samplers, ...uniformsAndDefines.samplers]
        };
    }
    bindFunction(mesh) {
        super.bindFunction(mesh);
        const effect = this.getEffect();
        effect.setTexture(DITHER_SAMPLER, this.ditherTexture.getTexture(mesh.getScene()));
        if (this._isDitherModeEnabled) {
            const scene = this.getScene();
            effect.setVector2(DITHER_SIZE, new Vector2(scene.getEngine().getRenderWidth() / this._ditherSize, scene.getEngine().getRenderHeight() / this._ditherSize));
            effect.setFloat(DITHER_DISTANCE_FADE, this.visualSettings.ifc.transparency.ditheringOptions.distanceFade ? 1.0 : 0.0);
            this.getEffect().setVector2(RESOLUTION, new Vector2(scene.getEngine().getRenderWidth(), scene.getEngine().getRenderHeight()));
        }
    }
}
//# sourceMappingURL=DitheringMaterial.js.map