"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module WebGL
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.LightingUniforms = void 0;
const core_common_1 = require("@itwin/core-common");
const FloatRGBA_1 = require("./FloatRGBA");
const Sync_1 = require("./Sync");
/** Maintains state of uniforms associated with a DisplayStyle3dState's LightSettings.
 * A single float array stored as:
 *  0  float solar intensity
 *  1  vec3 ambient color
 *  4  float ambient intensity
 *  5  vec3 hemi lower color
 *  8  vec3 hemi upper color
 *  11 float hemi intensity
 *  12 float portrait intensity
 *  13 float specular intensity
 *  14 float num cels
 *  15 fresnel intensity (negative if fresnel is to be inverted)
 * Note solar direction is handled separately in TargetUniforms.
 * @internal
 */
class LightingUniforms {
    syncKey = 0;
    // CPU state
    _settings = core_common_1.LightSettings.fromJSON();
    _initialized = false;
    // GPU state.
    _data = new Float32Array(16);
    // Working state
    _rgb = new FloatRGBA_1.FloatRgb();
    setRgb(rgb, index) {
        this._rgb.setTbgr(core_common_1.ColorDef.computeTbgrFromComponents(rgb.r, rgb.g, rgb.b));
        this._data[index + 0] = this._rgb.red;
        this._data[index + 1] = this._rgb.green;
        this._data[index + 2] = this._rgb.blue;
    }
    update(settings) {
        if (this._initialized && this._settings.equals(settings))
            return;
        this._initialized = true;
        this._settings = settings;
        (0, Sync_1.desync)(this);
        const data = this._data;
        data[0] = settings.solar.intensity;
        this.setRgb(settings.ambient.color, 1);
        data[4] = settings.ambient.intensity;
        this.setRgb(settings.hemisphere.lowerColor, 5);
        this.setRgb(settings.hemisphere.upperColor, 8);
        data[11] = settings.hemisphere.intensity;
        data[12] = settings.portraitIntensity;
        data[13] = settings.specularIntensity;
        data[14] = settings.numCels;
        const fresnel = settings.fresnel.intensity;
        data[15] = settings.fresnel.invert ? -fresnel : fresnel;
    }
    bind(uniform) {
        if (!(0, Sync_1.sync)(this, uniform))
            uniform.setUniform1fv(this._data);
    }
}
exports.LightingUniforms = LightingUniforms;
//# sourceMappingURL=LightingUniforms.js.map