"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.createPointStringBuilder = exports.createPointStringHiliter = void 0;
const AttributeMap_1 = require("../AttributeMap");
const ShaderBuilder_1 = require("../ShaderBuilder");
const Color_1 = require("./Color");
const Common_1 = require("./Common");
const FeatureSymbology_1 = require("./FeatureSymbology");
const Fragment_1 = require("./Fragment");
const Vertex_1 = require("./Vertex");
const computePosition = `
  float lineWeight = computeLineWeight();
  lineWeight += 0.5 * float(lineWeight > 4.0); // fudge factor for rounding fat points...
  gl_PointSize = lineWeight;
  return MAT_MVP * rawPos;
`;
// gl_PointSize specifies coordinates of this fragment within the point in range [0,1].
// This should be the most precise of the many approaches we've tried, but it still yields some asymmetry...
// Discarding if it meets radius precisely seems to reduce that slightly...
// ###TODO try point sprites?
const roundCorners = `
  const vec2 center = vec2(0.5, 0.5);
  vec2 vt = gl_PointCoord - center;
  return dot(vt, vt) * v_roundCorners >= 0.25; // meets or exceeds radius of circle
`;
const computeRoundCorners = "  v_roundCorners = gl_PointSize > 4.0 ? 1.0 : 0.0;";
function createBase(instanced, positionType) {
    const attrMap = AttributeMap_1.AttributeMap.findAttributeMap(3 /* TechniqueId.PointString */, 1 /* IsInstanced.Yes */ === instanced);
    const builder = new ShaderBuilder_1.ProgramBuilder(attrMap, { positionType, instanced: 1 /* IsInstanced.Yes */ === instanced });
    const vert = builder.vert;
    vert.set(10 /* VertexShaderComponent.ComputePosition */, computePosition);
    (0, Vertex_1.addModelViewProjectionMatrix)(vert);
    (0, Vertex_1.addLineWeight)(vert);
    builder.addInlineComputedVarying("v_roundCorners", 2 /* VariableType.Float */, computeRoundCorners);
    builder.frag.set(0 /* FragmentShaderComponent.CheckForEarlyDiscard */, roundCorners);
    return builder;
}
/** @internal */
function createPointStringHiliter(instanced, posType) {
    const builder = createBase(instanced, posType);
    (0, FeatureSymbology_1.addHiliter)(builder, true);
    return builder;
}
exports.createPointStringHiliter = createPointStringHiliter;
/** @internal */
function createPointStringBuilder(instanced, posType) {
    const builder = createBase(instanced, posType);
    (0, Common_1.addShaderFlags)(builder);
    (0, Color_1.addColor)(builder);
    (0, Fragment_1.addWhiteOnWhiteReversal)(builder.frag);
    return builder;
}
exports.createPointStringBuilder = createPointStringBuilder;
//# sourceMappingURL=PointString.js.map