"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.createSkyBoxProgram = createSkyBoxProgram;
const AttributeMap_1 = require("../AttributeMap");
const Matrix_1 = require("../Matrix");
const RenderFlags_1 = require("../RenderFlags");
const ShaderBuilder_1 = require("../ShaderBuilder");
const computeBaseColor = `return vec4(0, 0, 0, 0);`;
const assignFragData = `FragColor = TEXTURE_CUBE(s_cube, v_texDir);`;
const computePosition = `vec3 pos = u_rot * vec3(rawPos.x, rawPos.z, -rawPos.y); return pos.xyzz;`; // rawPos swizzling accounts for iModel rotation.
const computeTexDir = `v_texDir = rawPosition.xyz;`;
const scratchRotMatrix = new Matrix_1.Matrix3();
/** @internal */
function createSkyBoxProgram(context) {
    const prog = new ShaderBuilder_1.ProgramBuilder(AttributeMap_1.AttributeMap.findAttributeMap(undefined, false));
    prog.frag.set(1 /* FragmentShaderComponent.ComputeBaseColor */, computeBaseColor);
    prog.frag.set(17 /* FragmentShaderComponent.AssignFragData */, assignFragData);
    prog.vert.set(10 /* VertexShaderComponent.ComputePosition */, computePosition);
    prog.vert.addUniform("u_rot", 6 /* VariableType.Mat3 */, (prg) => {
        prg.addGraphicUniform("u_rot", (uniform, params) => {
            const rot = params.target.uniforms.frustum.viewMatrix.matrix;
            const mat3 = scratchRotMatrix;
            mat3.m00 = -rot.at(0, 0);
            mat3.m01 = -rot.at(0, 1);
            mat3.m02 = -rot.at(0, 2);
            mat3.m10 = -rot.at(1, 0);
            mat3.m11 = -rot.at(1, 1);
            mat3.m12 = -rot.at(1, 2);
            mat3.m20 = rot.at(2, 0);
            mat3.m21 = rot.at(2, 1);
            mat3.m22 = rot.at(2, 2);
            uniform.setMatrix3(mat3);
        });
    });
    prog.frag.addUniform("s_cube", 9 /* VariableType.SamplerCube */, (prg) => {
        prg.addGraphicUniform("s_cube", (uniform, params) => {
            const geom = params.geometry;
            geom.cube.texture.bindSampler(uniform, RenderFlags_1.TextureUnit.Zero);
        });
    });
    prog.addInlineComputedVarying("v_texDir", 4 /* VariableType.Vec3 */, computeTexDir);
    prog.vert.headerComment = "//!V! SkyBox";
    prog.frag.headerComment = "//!F! SkyBox";
    return prog.buildProgram(context);
}
//# sourceMappingURL=SkyBox.js.map