"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 Rendering
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderPlanEllipsoid = void 0;
exports.createEmptyRenderPlan = createEmptyRenderPlan;
exports.createRenderPlanFromViewport = createRenderPlanFromViewport;
const core_common_1 = require("@itwin/core-common");
const core_geometry_1 = require("@itwin/core-geometry");
const FlashSettings_1 = require("../FlashSettings");
const scratchPoint3a = new core_geometry_1.Point3d();
const scratchPoint3b = new core_geometry_1.Point3d();
const scratchPoint3c = new core_geometry_1.Point3d();
/**
 * @internal
 */
class RenderPlanEllipsoid {
    constructor(ellipsoidCenter, ellipsoidRotation, ellipsoidRadii) {
        this.ellipsoidCenter = ellipsoidCenter;
        this.ellipsoidRotation = ellipsoidRotation;
        this.ellipsoidRadii = ellipsoidRadii;
    }
    equals(other) {
        if (this.ellipsoidCenter.isAlmostEqual(other.ellipsoidCenter))
            return false;
        if (this.ellipsoidRotation.isAlmostEqual(other.ellipsoidRotation))
            return false;
        if (this.ellipsoidRadii.isAlmostEqual(other.ellipsoidRadii))
            return false;
        return true;
    }
}
exports.RenderPlanEllipsoid = RenderPlanEllipsoid;
/** @internal */
function createEmptyRenderPlan() {
    return {
        is3d: true,
        viewFlags: new core_common_1.ViewFlags(),
        bgColor: core_common_1.ColorDef.white,
        monoColor: core_common_1.ColorDef.white,
        monochromeMode: core_common_1.MonochromeMode.Scaled,
        hiliteSettings: new core_common_1.Hilite.Settings(),
        emphasisSettings: new core_common_1.Hilite.Settings(),
        flashSettings: new FlashSettings_1.FlashSettings(),
        clipStyle: core_common_1.ClipStyle.defaults,
        frustum: new core_common_1.Frustum(),
        fraction: 0,
        isFadeOutActive: false,
        globalViewTransition: 0,
        isGlobeMode3D: false,
        backgroundMapOn: false,
        upVector: core_geometry_1.Vector3d.unitZ(),
        whiteOnWhiteReversal: core_common_1.WhiteOnWhiteReversalSettings.fromJSON(),
    };
}
/** @internal */
function createRenderPlanFromViewport(vp) {
    const view = vp.view;
    const style = view.displayStyle;
    const is3d = view.is3d();
    const globalViewTransition = view.is3d() ? view.globalViewTransition() : 0.0;
    const isGlobeMode3D = core_common_1.GlobeMode.Ellipsoid === view.globeMode;
    const backgroundMapOn = view.displayStyle.viewFlags.backgroundMap;
    const frustum = vp.viewingSpace.getFrustum();
    const fraction = vp.viewingSpace.frustFraction;
    const viewFlags = style.viewFlags;
    const bgColor = view.backgroundColor;
    const monoColor = style.monochromeColor;
    const monochromeMode = style.settings.monochromeMode;
    const hiliteSettings = vp.hilite;
    const emphasisSettings = vp.emphasisSettings;
    const flashSettings = vp.flashSettings;
    const lights = vp.lightSettings;
    const isFadeOutActive = vp.isFadeOutActive;
    const clip = view.getViewClip();
    const clipStyle = view.displayStyle.settings.clipStyle;
    const hline = style.is3d() ? style.settings.hiddenLineSettings : undefined;
    const ao = style.is3d() ? style.settings.ambientOcclusionSettings : undefined;
    const analysisStyle = style.settings.analysisStyle;
    const thematic = (style.is3d() && view.displayStyle.viewFlags.thematicDisplay) ? style.settings.thematic : undefined;
    const contours = (style.is3d() && style.settings.contours.groups.length > 0) ? style.settings.contours : undefined;
    const shouldDisplayAtmosphere = (style.is3d() && core_common_1.GlobeMode.Ellipsoid === view.globeMode && vp.iModel.isGeoLocated && style.viewFlags.backgroundMap) ? vp.view.getDisplayStyle3d().environment.displayAtmosphere : false;
    const atmosphere = shouldDisplayAtmosphere ? vp.view.getDisplayStyle3d().environment.atmosphere : undefined;
    let upVector;
    if (core_common_1.GlobeMode.Ellipsoid === view.globeMode) {
        const lb = frustum.getCorner(core_common_1.Npc.LeftBottomRear).interpolate(0.5, frustum.getCorner(core_common_1.Npc.LeftBottomFront), scratchPoint3a);
        const rt = frustum.getCorner(core_common_1.Npc.RightTopRear).interpolate(0.5, frustum.getCorner(core_common_1.Npc.RightTopFront), scratchPoint3b);
        const cntr = lb.interpolate(0.5, rt, scratchPoint3c);
        upVector = view.getUpVector(cntr);
    }
    else {
        upVector = core_geometry_1.Vector3d.unitZ();
    }
    let analysisTexture;
    if (analysisStyle?.thematic)
        analysisTexture = vp.target.renderSystem.getGradientTexture(analysisStyle.thematic.gradient, vp.iModel);
    let ellipsoid;
    if (core_common_1.GlobeMode.Ellipsoid === view.globeMode) {
        const mapEcefToDb = view.iModel.getMapEcefToDb(0);
        ellipsoid = new RenderPlanEllipsoid(core_geometry_1.Point3d.fromJSON(mapEcefToDb.origin), mapEcefToDb.matrix, core_geometry_1.Point3d.fromJSON({ x: core_geometry_1.Constant.earthRadiusWGS84.equator, y: core_geometry_1.Constant.earthRadiusWGS84.equator, z: core_geometry_1.Constant.earthRadiusWGS84.polar }));
    }
    return {
        is3d,
        viewFlags,
        bgColor,
        monoColor,
        monochromeMode,
        hiliteSettings,
        emphasisSettings,
        flashSettings,
        clip,
        clipStyle,
        hline,
        analysisStyle,
        ao,
        thematic,
        contours,
        atmosphere,
        isFadeOutActive,
        analysisTexture,
        frustum,
        fraction,
        globalViewTransition,
        isGlobeMode3D,
        backgroundMapOn,
        upVector,
        lights,
        whiteOnWhiteReversal: vp.displayStyle.settings.whiteOnWhiteReversal,
        ellipsoid,
    };
}
//# sourceMappingURL=RenderPlan.js.map