/* Mapbox GL JS is Copyright © 2020 Mapbox and subject to the Mapbox Terms of Service ((https://www.mapbox.com/legal/tos/). */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.mapboxgl = factory());
})(this, (function () { 'use strict';
/* eslint-disable */
var shared, worker, mapboxgl;
// define gets called three times: one for each chunk. we rely on the order
// they're imported to know which is which
function define(_, chunk) {
if (!shared) {
shared = chunk;
} else if (!worker) {
worker = chunk;
} else {
var workerBundleString = "self.onerror = function() { console.error('An error occurred while parsing the WebWorker bundle. This is most likely due to improper transpilation by Babel; please see https://docs.mapbox.com/mapbox-gl-js/guides/install/#transpiling'); }; var sharedChunk = {}; (" + shared + ")(sharedChunk); (" + worker + ")(sharedChunk); self.onerror = null;"
var sharedChunk = {};
shared(sharedChunk);
mapboxgl = chunk(sharedChunk);
if (typeof window !== 'undefined' && window && window.URL && window.URL.createObjectURL) {
mapboxgl.workerUrl = window.URL.createObjectURL(new Blob([workerBundleString], { type: 'text/javascript' }));
}
}
}
define(['exports'], (function (exports$1) { 'use strict';
/**
* Common utilities
* @module glMatrix
*/
// Configuration Constants
var EPSILON = 0.000001;
var ARRAY_TYPE = typeof Float32Array !== "undefined" ? Float32Array : Array;
var RANDOM = Math.random;
var ANGLE_ORDER = "zyx";
/**
* Symmetric round
* see https://www.npmjs.com/package/round-half-up-symmetric#user-content-detailed-background
*
* @param {Number} a value to round
*/
function round$4(a) {
if (a >= 0) return Math.round(a);
return a % 0.5 === 0 ? Math.floor(a) : Math.round(a);
}
/**
* Sets the type of array used when creating new vectors and matrices
*
* @param {Float32ArrayConstructor | ArrayConstructor} type Array type, such as Float32Array or Array
*/
function setMatrixArrayType(type) {
ARRAY_TYPE = type;
}
var degree = Math.PI / 180;
var radian = 180 / Math.PI;
/**
* Convert Degree To Radian
*
* @param {Number} a Angle in Degrees
*/
function toRadian(a) {
return a * degree;
}
/**
* Convert Radian To Degree
*
* @param {Number} a Angle in Radians
*/
function toDegree(a) {
return a * radian;
}
/**
* Tests whether or not the arguments have approximately the same value, within an absolute
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
* than or equal to 1.0, and a relative tolerance is used for larger values)
*
* @param {Number} a The first number to test.
* @param {Number} b The second number to test.
* @param {Number} tolerance Absolute or relative tolerance (default glMatrix.EPSILON)
* @returns {Boolean} True if the numbers are approximately equal, false otherwise.
*/
function equals$b(a, b) {
var tolerance = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EPSILON;
return Math.abs(a - b) <= tolerance * Math.max(1, Math.abs(a), Math.abs(b));
}
var common = /*#__PURE__*/Object.freeze({
__proto__: null,
ANGLE_ORDER: ANGLE_ORDER,
get ARRAY_TYPE () { return ARRAY_TYPE; },
EPSILON: EPSILON,
RANDOM: RANDOM,
equals: equals$b,
round: round$4,
setMatrixArrayType: setMatrixArrayType,
toDegree: toDegree,
toRadian: toRadian
});
/**
* 2x2 Matrix
* @module mat2
*/
/**
* Creates a new identity mat2
*
* @returns {mat2} a new 2x2 matrix
*/
function create$8() {
var out = new ARRAY_TYPE(4);
if (ARRAY_TYPE != Float32Array) {
out[1] = 0;
out[2] = 0;
}
out[0] = 1;
out[3] = 1;
return out;
}
/**
* Creates a new mat2 initialized with values from an existing matrix
*
* @param {ReadonlyMat2} a matrix to clone
* @returns {mat2} a new 2x2 matrix
*/
function clone$9(a) {
var out = new ARRAY_TYPE(4);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
}
/**
* Copy the values from one mat2 to another
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the source matrix
* @returns {mat2} out
*/
function copy$8(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
}
/**
* Set a mat2 to the identity matrix
*
* @param {mat2} out the receiving matrix
* @returns {mat2} out
*/
function identity$6(out) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 1;
return out;
}
/**
* Create a new mat2 with the given values
*
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m10 Component in column 1, row 0 position (index 2)
* @param {Number} m11 Component in column 1, row 1 position (index 3)
* @returns {mat2} out A new 2x2 matrix
*/
function fromValues$8(m00, m01, m10, m11) {
var out = new ARRAY_TYPE(4);
out[0] = m00;
out[1] = m01;
out[2] = m10;
out[3] = m11;
return out;
}
/**
* Set the components of a mat2 to the given values
*
* @param {mat2} out the receiving matrix
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m10 Component in column 1, row 0 position (index 2)
* @param {Number} m11 Component in column 1, row 1 position (index 3)
* @returns {mat2} out
*/
function set$8(out, m00, m01, m10, m11) {
out[0] = m00;
out[1] = m01;
out[2] = m10;
out[3] = m11;
return out;
}
/**
* Transpose the values of a mat2
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the source matrix
* @returns {mat2} out
*/
function transpose$2(out, a) {
// If we are transposing ourselves we can skip a few steps but have to cache
// some values
if (out === a) {
var a1 = a[1];
out[1] = a[2];
out[2] = a1;
} else {
out[0] = a[0];
out[1] = a[2];
out[2] = a[1];
out[3] = a[3];
}
return out;
}
/**
* Inverts a mat2
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the source matrix
* @returns {mat2 | null} out, or null if source matrix is not invertible
*/
function invert$5(out, a) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
// Calculate the determinant
var det = a0 * a3 - a2 * a1;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = a3 * det;
out[1] = -a1 * det;
out[2] = -a2 * det;
out[3] = a0 * det;
return out;
}
/**
* Calculates the adjugate of a mat2
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the source matrix
* @returns {mat2} out
*/
function adjoint$2(out, a) {
// Caching this value is necessary if out == a
var a0 = a[0];
out[0] = a[3];
out[1] = -a[1];
out[2] = -a[2];
out[3] = a0;
return out;
}
/**
* Calculates the determinant of a mat2
*
* @param {ReadonlyMat2} a the source matrix
* @returns {Number} determinant of a
*/
function determinant$3(a) {
return a[0] * a[3] - a[2] * a[1];
}
/**
* Multiplies two mat2's
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the first operand
* @param {ReadonlyMat2} b the second operand
* @returns {mat2} out
*/
function multiply$8(out, a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
out[0] = a0 * b0 + a2 * b1;
out[1] = a1 * b0 + a3 * b1;
out[2] = a0 * b2 + a2 * b3;
out[3] = a1 * b2 + a3 * b3;
return out;
}
/**
* Rotates a mat2 by the given angle
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2} out
*/
function rotate$5(out, a, rad) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var s = Math.sin(rad);
var c = Math.cos(rad);
out[0] = a0 * c + a2 * s;
out[1] = a1 * c + a3 * s;
out[2] = a0 * -s + a2 * c;
out[3] = a1 * -s + a3 * c;
return out;
}
/**
* Scales the mat2 by the dimensions in the given vec2
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the matrix to rotate
* @param {ReadonlyVec2} v the vec2 to scale the matrix by
* @returns {mat2} out
**/
function scale$8(out, a, v) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var v0 = v[0],
v1 = v[1];
out[0] = a0 * v0;
out[1] = a1 * v0;
out[2] = a2 * v1;
out[3] = a3 * v1;
return out;
}
/**
* Creates a matrix from a given angle
* This is equivalent to (but much faster than):
*
* mat2.identity(dest);
* mat2.rotate(dest, dest, rad);
*
* @param {mat2} out mat2 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2} out
*/
function fromRotation$4(out, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
out[0] = c;
out[1] = s;
out[2] = -s;
out[3] = c;
return out;
}
/**
* Creates a matrix from a vector scaling
* This is equivalent to (but much faster than):
*
* mat2.identity(dest);
* mat2.scale(dest, dest, vec);
*
* @param {mat2} out mat2 receiving operation result
* @param {ReadonlyVec2} v Scaling vector
* @returns {mat2} out
*/
function fromScaling$3(out, v) {
out[0] = v[0];
out[1] = 0;
out[2] = 0;
out[3] = v[1];
return out;
}
/**
* Returns a string representation of a mat2
*
* @param {ReadonlyMat2} a matrix to represent as a string
* @returns {String} string representation of the matrix
*/
function str$8(a) {
return "mat2(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
}
/**
* Returns Frobenius norm of a mat2
*
* @param {ReadonlyMat2} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function frob$3(a) {
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]);
}
/**
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
* @param {ReadonlyMat2} L the lower triangular matrix
* @param {ReadonlyMat2} D the diagonal matrix
* @param {ReadonlyMat2} U the upper triangular matrix
* @param {ReadonlyMat2} a the input matrix to factorize
*/
function LDU(L, D, U, a) {
L[2] = a[2] / a[0];
U[0] = a[0];
U[1] = a[1];
U[3] = a[3] - L[2] * U[1];
return [L, D, U];
}
/**
* Adds two mat2's
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the first operand
* @param {ReadonlyMat2} b the second operand
* @returns {mat2} out
*/
function add$8(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
return out;
}
/**
* Subtracts matrix b from matrix a
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the first operand
* @param {ReadonlyMat2} b the second operand
* @returns {mat2} out
*/
function subtract$6(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
return out;
}
/**
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyMat2} a The first matrix.
* @param {ReadonlyMat2} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function exactEquals$8(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
}
/**
* Returns whether or not the matrices have approximately the same elements in the same position.
*
* @param {ReadonlyMat2} a The first matrix.
* @param {ReadonlyMat2} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function equals$a(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
}
/**
* Multiply each element of the matrix by a scalar.
*
* @param {mat2} out the receiving matrix
* @param {ReadonlyMat2} a the matrix to scale
* @param {Number} b amount to scale the matrix's elements by
* @returns {mat2} out
*/
function multiplyScalar$3(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
return out;
}
/**
* Adds two mat2's after multiplying each element of the second operand by a scalar value.
*
* @param {mat2} out the receiving vector
* @param {ReadonlyMat2} a the first operand
* @param {ReadonlyMat2} b the second operand
* @param {Number} scale the amount to scale b's elements by before adding
* @returns {mat2} out
*/
function multiplyScalarAndAdd$3(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
out[3] = a[3] + b[3] * scale;
return out;
}
/**
* Alias for {@link mat2.multiply}
* @function
*/
var mul$8 = multiply$8;
/**
* Alias for {@link mat2.subtract}
* @function
*/
var sub$6 = subtract$6;
var mat2 = /*#__PURE__*/Object.freeze({
__proto__: null,
LDU: LDU,
add: add$8,
adjoint: adjoint$2,
clone: clone$9,
copy: copy$8,
create: create$8,
determinant: determinant$3,
equals: equals$a,
exactEquals: exactEquals$8,
frob: frob$3,
fromRotation: fromRotation$4,
fromScaling: fromScaling$3,
fromValues: fromValues$8,
identity: identity$6,
invert: invert$5,
mul: mul$8,
multiply: multiply$8,
multiplyScalar: multiplyScalar$3,
multiplyScalarAndAdd: multiplyScalarAndAdd$3,
rotate: rotate$5,
scale: scale$8,
set: set$8,
str: str$8,
sub: sub$6,
subtract: subtract$6,
transpose: transpose$2
});
/**
* 2x3 Matrix
* @module mat2d
* @description
* A mat2d contains six elements defined as:
*
* [a, b,
* c, d,
* tx, ty]
*
* This is a short form for the 3x3 matrix:
*
* [a, b, 0,
* c, d, 0,
* tx, ty, 1]
*
* The last column is ignored so the array is shorter and operations are faster.
*/
/**
* Creates a new identity mat2d
*
* @returns {mat2d} a new 2x3 matrix
*/
function create$7() {
var out = new ARRAY_TYPE(6);
if (ARRAY_TYPE != Float32Array) {
out[1] = 0;
out[2] = 0;
out[4] = 0;
out[5] = 0;
}
out[0] = 1;
out[3] = 1;
return out;
}
/**
* Creates a new mat2d initialized with values from an existing matrix
*
* @param {ReadonlyMat2d} a matrix to clone
* @returns {mat2d} a new 2x3 matrix
*/
function clone$8(a) {
var out = new ARRAY_TYPE(6);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
return out;
}
/**
* Copy the values from one mat2d to another
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the source matrix
* @returns {mat2d} out
*/
function copy$7(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
return out;
}
/**
* Set a mat2d to the identity matrix
*
* @param {mat2d} out the receiving matrix
* @returns {mat2d} out
*/
function identity$5(out) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 1;
out[4] = 0;
out[5] = 0;
return out;
}
/**
* Create a new mat2d with the given values
*
* @param {Number} a Component A (index 0)
* @param {Number} b Component B (index 1)
* @param {Number} c Component C (index 2)
* @param {Number} d Component D (index 3)
* @param {Number} tx Component TX (index 4)
* @param {Number} ty Component TY (index 5)
* @returns {mat2d} A new mat2d
*/
function fromValues$7(a, b, c, d, tx, ty) {
var out = new ARRAY_TYPE(6);
out[0] = a;
out[1] = b;
out[2] = c;
out[3] = d;
out[4] = tx;
out[5] = ty;
return out;
}
/**
* Set the components of a mat2d to the given values
*
* @param {mat2d} out the receiving matrix
* @param {Number} a Component A (index 0)
* @param {Number} b Component B (index 1)
* @param {Number} c Component C (index 2)
* @param {Number} d Component D (index 3)
* @param {Number} tx Component TX (index 4)
* @param {Number} ty Component TY (index 5)
* @returns {mat2d} out
*/
function set$7(out, a, b, c, d, tx, ty) {
out[0] = a;
out[1] = b;
out[2] = c;
out[3] = d;
out[4] = tx;
out[5] = ty;
return out;
}
/**
* Inverts a mat2d
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the source matrix
* @returns {mat2d | null} out, or null if source matrix is not invertible
*/
function invert$4(out, a) {
var aa = a[0],
ab = a[1],
ac = a[2],
ad = a[3];
var atx = a[4],
aty = a[5];
var det = aa * ad - ab * ac;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = ad * det;
out[1] = -ab * det;
out[2] = -ac * det;
out[3] = aa * det;
out[4] = (ac * aty - ad * atx) * det;
out[5] = (ab * atx - aa * aty) * det;
return out;
}
/**
* Calculates the determinant of a mat2d
*
* @param {ReadonlyMat2d} a the source matrix
* @returns {Number} determinant of a
*/
function determinant$2(a) {
return a[0] * a[3] - a[1] * a[2];
}
/**
* Multiplies two mat2d's
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the first operand
* @param {ReadonlyMat2d} b the second operand
* @returns {mat2d} out
*/
function multiply$7(out, a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3],
b4 = b[4],
b5 = b[5];
out[0] = a0 * b0 + a2 * b1;
out[1] = a1 * b0 + a3 * b1;
out[2] = a0 * b2 + a2 * b3;
out[3] = a1 * b2 + a3 * b3;
out[4] = a0 * b4 + a2 * b5 + a4;
out[5] = a1 * b4 + a3 * b5 + a5;
return out;
}
/**
* Rotates a mat2d by the given angle
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2d} out
*/
function rotate$4(out, a, rad) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5];
var s = Math.sin(rad);
var c = Math.cos(rad);
out[0] = a0 * c + a2 * s;
out[1] = a1 * c + a3 * s;
out[2] = a0 * -s + a2 * c;
out[3] = a1 * -s + a3 * c;
out[4] = a4;
out[5] = a5;
return out;
}
/**
* Scales the mat2d by the dimensions in the given vec2
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the matrix to translate
* @param {ReadonlyVec2} v the vec2 to scale the matrix by
* @returns {mat2d} out
**/
function scale$7(out, a, v) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5];
var v0 = v[0],
v1 = v[1];
out[0] = a0 * v0;
out[1] = a1 * v0;
out[2] = a2 * v1;
out[3] = a3 * v1;
out[4] = a4;
out[5] = a5;
return out;
}
/**
* Translates the mat2d by the dimensions in the given vec2
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the matrix to translate
* @param {ReadonlyVec2} v the vec2 to translate the matrix by
* @returns {mat2d} out
**/
function translate$4(out, a, v) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5];
var v0 = v[0],
v1 = v[1];
out[0] = a0;
out[1] = a1;
out[2] = a2;
out[3] = a3;
out[4] = a0 * v0 + a2 * v1 + a4;
out[5] = a1 * v0 + a3 * v1 + a5;
return out;
}
/**
* Creates a matrix from a given angle
* This is equivalent to (but much faster than):
*
* mat2d.identity(dest);
* mat2d.rotate(dest, dest, rad);
*
* @param {mat2d} out mat2d receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2d} out
*/
function fromRotation$3(out, rad) {
var s = Math.sin(rad),
c = Math.cos(rad);
out[0] = c;
out[1] = s;
out[2] = -s;
out[3] = c;
out[4] = 0;
out[5] = 0;
return out;
}
/**
* Creates a matrix from a vector scaling
* This is equivalent to (but much faster than):
*
* mat2d.identity(dest);
* mat2d.scale(dest, dest, vec);
*
* @param {mat2d} out mat2d receiving operation result
* @param {ReadonlyVec2} v Scaling vector
* @returns {mat2d} out
*/
function fromScaling$2(out, v) {
out[0] = v[0];
out[1] = 0;
out[2] = 0;
out[3] = v[1];
out[4] = 0;
out[5] = 0;
return out;
}
/**
* Creates a matrix from a vector translation
* This is equivalent to (but much faster than):
*
* mat2d.identity(dest);
* mat2d.translate(dest, dest, vec);
*
* @param {mat2d} out mat2d receiving operation result
* @param {ReadonlyVec2} v Translation vector
* @returns {mat2d} out
*/
function fromTranslation$3(out, v) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 1;
out[4] = v[0];
out[5] = v[1];
return out;
}
/**
* Returns a string representation of a mat2d
*
* @param {ReadonlyMat2d} a matrix to represent as a string
* @returns {String} string representation of the matrix
*/
function str$7(a) {
return "mat2d(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ")";
}
/**
* Returns Frobenius norm of a mat2d
*
* @param {ReadonlyMat2d} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function frob$2(a) {
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + 1);
}
/**
* Adds two mat2d's
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the first operand
* @param {ReadonlyMat2d} b the second operand
* @returns {mat2d} out
*/
function add$7(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
out[5] = a[5] + b[5];
return out;
}
/**
* Subtracts matrix b from matrix a
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the first operand
* @param {ReadonlyMat2d} b the second operand
* @returns {mat2d} out
*/
function subtract$5(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
out[4] = a[4] - b[4];
out[5] = a[5] - b[5];
return out;
}
/**
* Multiply each element of the matrix by a scalar.
*
* @param {mat2d} out the receiving matrix
* @param {ReadonlyMat2d} a the matrix to scale
* @param {Number} b amount to scale the matrix's elements by
* @returns {mat2d} out
*/
function multiplyScalar$2(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
out[4] = a[4] * b;
out[5] = a[5] * b;
return out;
}
/**
* Adds two mat2d's after multiplying each element of the second operand by a scalar value.
*
* @param {mat2d} out the receiving vector
* @param {ReadonlyMat2d} a the first operand
* @param {ReadonlyMat2d} b the second operand
* @param {Number} scale the amount to scale b's elements by before adding
* @returns {mat2d} out
*/
function multiplyScalarAndAdd$2(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
out[3] = a[3] + b[3] * scale;
out[4] = a[4] + b[4] * scale;
out[5] = a[5] + b[5] * scale;
return out;
}
/**
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyMat2d} a The first matrix.
* @param {ReadonlyMat2d} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function exactEquals$7(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5];
}
/**
* Returns whether or not the matrices have approximately the same elements in the same position.
*
* @param {ReadonlyMat2d} a The first matrix.
* @param {ReadonlyMat2d} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function equals$9(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3],
b4 = b[4],
b5 = b[5];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5));
}
/**
* Alias for {@link mat2d.multiply}
* @function
*/
var mul$7 = multiply$7;
/**
* Alias for {@link mat2d.subtract}
* @function
*/
var sub$5 = subtract$5;
var mat2d = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$7,
clone: clone$8,
copy: copy$7,
create: create$7,
determinant: determinant$2,
equals: equals$9,
exactEquals: exactEquals$7,
frob: frob$2,
fromRotation: fromRotation$3,
fromScaling: fromScaling$2,
fromTranslation: fromTranslation$3,
fromValues: fromValues$7,
identity: identity$5,
invert: invert$4,
mul: mul$7,
multiply: multiply$7,
multiplyScalar: multiplyScalar$2,
multiplyScalarAndAdd: multiplyScalarAndAdd$2,
rotate: rotate$4,
scale: scale$7,
set: set$7,
str: str$7,
sub: sub$5,
subtract: subtract$5,
translate: translate$4
});
/**
* 3x3 Matrix
* @module mat3
*/
/**
* Creates a new identity mat3
*
* @returns {mat3} a new 3x3 matrix
*/
function create$6() {
var out = new ARRAY_TYPE(9);
if (ARRAY_TYPE != Float32Array) {
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[5] = 0;
out[6] = 0;
out[7] = 0;
}
out[0] = 1;
out[4] = 1;
out[8] = 1;
return out;
}
/**
* Copies the upper-left 3x3 values into the given mat3.
*
* @param {mat3} out the receiving 3x3 matrix
* @param {ReadonlyMat4} a the source 4x4 matrix
* @returns {mat3} out
*/
function fromMat4$1(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[4];
out[4] = a[5];
out[5] = a[6];
out[6] = a[8];
out[7] = a[9];
out[8] = a[10];
return out;
}
/**
* Creates a new mat3 initialized with values from an existing matrix
*
* @param {ReadonlyMat3} a matrix to clone
* @returns {mat3} a new 3x3 matrix
*/
function clone$7(a) {
var out = new ARRAY_TYPE(9);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
return out;
}
/**
* Copy the values from one mat3 to another
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the source matrix
* @returns {mat3} out
*/
function copy$6(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
return out;
}
/**
* Create a new mat3 with the given values
*
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m02 Component in column 0, row 2 position (index 2)
* @param {Number} m10 Component in column 1, row 0 position (index 3)
* @param {Number} m11 Component in column 1, row 1 position (index 4)
* @param {Number} m12 Component in column 1, row 2 position (index 5)
* @param {Number} m20 Component in column 2, row 0 position (index 6)
* @param {Number} m21 Component in column 2, row 1 position (index 7)
* @param {Number} m22 Component in column 2, row 2 position (index 8)
* @returns {mat3} A new mat3
*/
function fromValues$6(m00, m01, m02, m10, m11, m12, m20, m21, m22) {
var out = new ARRAY_TYPE(9);
out[0] = m00;
out[1] = m01;
out[2] = m02;
out[3] = m10;
out[4] = m11;
out[5] = m12;
out[6] = m20;
out[7] = m21;
out[8] = m22;
return out;
}
/**
* Set the components of a mat3 to the given values
*
* @param {mat3} out the receiving matrix
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m02 Component in column 0, row 2 position (index 2)
* @param {Number} m10 Component in column 1, row 0 position (index 3)
* @param {Number} m11 Component in column 1, row 1 position (index 4)
* @param {Number} m12 Component in column 1, row 2 position (index 5)
* @param {Number} m20 Component in column 2, row 0 position (index 6)
* @param {Number} m21 Component in column 2, row 1 position (index 7)
* @param {Number} m22 Component in column 2, row 2 position (index 8)
* @returns {mat3} out
*/
function set$6(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
out[0] = m00;
out[1] = m01;
out[2] = m02;
out[3] = m10;
out[4] = m11;
out[5] = m12;
out[6] = m20;
out[7] = m21;
out[8] = m22;
return out;
}
/**
* Set a mat3 to the identity matrix
*
* @param {mat3} out the receiving matrix
* @returns {mat3} out
*/
function identity$4(out) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 1;
out[5] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 1;
return out;
}
/**
* Transpose the values of a mat3
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the source matrix
* @returns {mat3} out
*/
function transpose$1(out, a) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (out === a) {
var a01 = a[1],
a02 = a[2],
a12 = a[5];
out[1] = a[3];
out[2] = a[6];
out[3] = a01;
out[5] = a[7];
out[6] = a02;
out[7] = a12;
} else {
out[0] = a[0];
out[1] = a[3];
out[2] = a[6];
out[3] = a[1];
out[4] = a[4];
out[5] = a[7];
out[6] = a[2];
out[7] = a[5];
out[8] = a[8];
}
return out;
}
/**
* Inverts a mat3
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the source matrix
* @returns {mat3 | null} out, or null if source matrix is not invertible
*/
function invert$3(out, a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2];
var a10 = a[3],
a11 = a[4],
a12 = a[5];
var a20 = a[6],
a21 = a[7],
a22 = a[8];
var b01 = a22 * a11 - a12 * a21;
var b11 = -a22 * a10 + a12 * a20;
var b21 = a21 * a10 - a11 * a20;
// Calculate the determinant
var det = a00 * b01 + a01 * b11 + a02 * b21;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = b01 * det;
out[1] = (-a22 * a01 + a02 * a21) * det;
out[2] = (a12 * a01 - a02 * a11) * det;
out[3] = b11 * det;
out[4] = (a22 * a00 - a02 * a20) * det;
out[5] = (-a12 * a00 + a02 * a10) * det;
out[6] = b21 * det;
out[7] = (-a21 * a00 + a01 * a20) * det;
out[8] = (a11 * a00 - a01 * a10) * det;
return out;
}
/**
* Calculates the adjugate of a mat3
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the source matrix
* @returns {mat3} out
*/
function adjoint$1(out, a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2];
var a10 = a[3],
a11 = a[4],
a12 = a[5];
var a20 = a[6],
a21 = a[7],
a22 = a[8];
out[0] = a11 * a22 - a12 * a21;
out[1] = a02 * a21 - a01 * a22;
out[2] = a01 * a12 - a02 * a11;
out[3] = a12 * a20 - a10 * a22;
out[4] = a00 * a22 - a02 * a20;
out[5] = a02 * a10 - a00 * a12;
out[6] = a10 * a21 - a11 * a20;
out[7] = a01 * a20 - a00 * a21;
out[8] = a00 * a11 - a01 * a10;
return out;
}
/**
* Calculates the determinant of a mat3
*
* @param {ReadonlyMat3} a the source matrix
* @returns {Number} determinant of a
*/
function determinant$1(a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2];
var a10 = a[3],
a11 = a[4],
a12 = a[5];
var a20 = a[6],
a21 = a[7],
a22 = a[8];
return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
}
/**
* Multiplies two mat3's
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the first operand
* @param {ReadonlyMat3} b the second operand
* @returns {mat3} out
*/
function multiply$6(out, a, b) {
var a00 = a[0],
a01 = a[1],
a02 = a[2];
var a10 = a[3],
a11 = a[4],
a12 = a[5];
var a20 = a[6],
a21 = a[7],
a22 = a[8];
var b00 = b[0],
b01 = b[1],
b02 = b[2];
var b10 = b[3],
b11 = b[4],
b12 = b[5];
var b20 = b[6],
b21 = b[7],
b22 = b[8];
out[0] = b00 * a00 + b01 * a10 + b02 * a20;
out[1] = b00 * a01 + b01 * a11 + b02 * a21;
out[2] = b00 * a02 + b01 * a12 + b02 * a22;
out[3] = b10 * a00 + b11 * a10 + b12 * a20;
out[4] = b10 * a01 + b11 * a11 + b12 * a21;
out[5] = b10 * a02 + b11 * a12 + b12 * a22;
out[6] = b20 * a00 + b21 * a10 + b22 * a20;
out[7] = b20 * a01 + b21 * a11 + b22 * a21;
out[8] = b20 * a02 + b21 * a12 + b22 * a22;
return out;
}
/**
* Translate a mat3 by the given vector
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the matrix to translate
* @param {ReadonlyVec2} v vector to translate by
* @returns {mat3} out
*/
function translate$3(out, a, v) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a10 = a[3],
a11 = a[4],
a12 = a[5],
a20 = a[6],
a21 = a[7],
a22 = a[8],
x = v[0],
y = v[1];
out[0] = a00;
out[1] = a01;
out[2] = a02;
out[3] = a10;
out[4] = a11;
out[5] = a12;
out[6] = x * a00 + y * a10 + a20;
out[7] = x * a01 + y * a11 + a21;
out[8] = x * a02 + y * a12 + a22;
return out;
}
/**
* Rotates a mat3 by the given angle
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat3} out
*/
function rotate$3(out, a, rad) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a10 = a[3],
a11 = a[4],
a12 = a[5],
a20 = a[6],
a21 = a[7],
a22 = a[8],
s = Math.sin(rad),
c = Math.cos(rad);
out[0] = c * a00 + s * a10;
out[1] = c * a01 + s * a11;
out[2] = c * a02 + s * a12;
out[3] = c * a10 - s * a00;
out[4] = c * a11 - s * a01;
out[5] = c * a12 - s * a02;
out[6] = a20;
out[7] = a21;
out[8] = a22;
return out;
}
/**
* Scales the mat3 by the dimensions in the given vec2
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the matrix to scale
* @param {ReadonlyVec2} v the vec2 to scale the matrix by
* @returns {mat3} out
**/
function scale$6(out, a, v) {
var x = v[0],
y = v[1];
out[0] = x * a[0];
out[1] = x * a[1];
out[2] = x * a[2];
out[3] = y * a[3];
out[4] = y * a[4];
out[5] = y * a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
return out;
}
/**
* Creates a matrix from a vector translation
* This is equivalent to (but much faster than):
*
* mat3.identity(dest);
* mat3.translate(dest, dest, vec);
*
* @param {mat3} out mat3 receiving operation result
* @param {ReadonlyVec2} v Translation vector
* @returns {mat3} out
*/
function fromTranslation$2(out, v) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 1;
out[5] = 0;
out[6] = v[0];
out[7] = v[1];
out[8] = 1;
return out;
}
/**
* Creates a matrix from a given angle
* This is equivalent to (but much faster than):
*
* mat3.identity(dest);
* mat3.rotate(dest, dest, rad);
*
* @param {mat3} out mat3 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat3} out
*/
function fromRotation$2(out, rad) {
var s = Math.sin(rad),
c = Math.cos(rad);
out[0] = c;
out[1] = s;
out[2] = 0;
out[3] = -s;
out[4] = c;
out[5] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 1;
return out;
}
/**
* Creates a matrix from a vector scaling
* This is equivalent to (but much faster than):
*
* mat3.identity(dest);
* mat3.scale(dest, dest, vec);
*
* @param {mat3} out mat3 receiving operation result
* @param {ReadonlyVec2} v Scaling vector
* @returns {mat3} out
*/
function fromScaling$1(out, v) {
out[0] = v[0];
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = v[1];
out[5] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 1;
return out;
}
/**
* Copies the values from a mat2d into a mat3
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat2d} a the matrix to copy
* @returns {mat3} out
**/
function fromMat2d(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = 0;
out[3] = a[2];
out[4] = a[3];
out[5] = 0;
out[6] = a[4];
out[7] = a[5];
out[8] = 1;
return out;
}
/**
* Calculates a 3x3 matrix from the given quaternion
*
* @param {mat3} out mat3 receiving operation result
* @param {ReadonlyQuat} q Quaternion to create matrix from
*
* @returns {mat3} out
*/
function fromQuat$1(out, q) {
var x = q[0],
y = q[1],
z = q[2],
w = q[3];
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var yx = y * x2;
var yy = y * y2;
var zx = z * x2;
var zy = z * y2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
out[0] = 1 - yy - zz;
out[3] = yx - wz;
out[6] = zx + wy;
out[1] = yx + wz;
out[4] = 1 - xx - zz;
out[7] = zy - wx;
out[2] = zx - wy;
out[5] = zy + wx;
out[8] = 1 - xx - yy;
return out;
}
/**
* Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
*
* @param {mat3} out mat3 receiving operation result
* @param {ReadonlyMat4} a Mat4 to derive the normal matrix from
*
* @returns {mat3} out
*/
function normalFromMat4(out, a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
// Calculate the determinant
var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
return out;
}
/**
* Generates a 2D projection matrix with the given bounds
*
* @param {mat3} out mat3 frustum matrix will be written into
* @param {number} width Width of your gl context
* @param {number} height Height of gl context
* @returns {mat3} out
*/
function projection(out, width, height) {
out[0] = 2 / width;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = -2 / height;
out[5] = 0;
out[6] = -1;
out[7] = 1;
out[8] = 1;
return out;
}
/**
* Returns a string representation of a mat3
*
* @param {ReadonlyMat3} a matrix to represent as a string
* @returns {String} string representation of the matrix
*/
function str$6(a) {
return "mat3(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ")";
}
/**
* Returns Frobenius norm of a mat3
*
* @param {ReadonlyMat3} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function frob$1(a) {
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + a[6] * a[6] + a[7] * a[7] + a[8] * a[8]);
}
/**
* Adds two mat3's
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the first operand
* @param {ReadonlyMat3} b the second operand
* @returns {mat3} out
*/
function add$6(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
out[5] = a[5] + b[5];
out[6] = a[6] + b[6];
out[7] = a[7] + b[7];
out[8] = a[8] + b[8];
return out;
}
/**
* Subtracts matrix b from matrix a
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the first operand
* @param {ReadonlyMat3} b the second operand
* @returns {mat3} out
*/
function subtract$4(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
out[4] = a[4] - b[4];
out[5] = a[5] - b[5];
out[6] = a[6] - b[6];
out[7] = a[7] - b[7];
out[8] = a[8] - b[8];
return out;
}
/**
* Multiply each element of the matrix by a scalar.
*
* @param {mat3} out the receiving matrix
* @param {ReadonlyMat3} a the matrix to scale
* @param {Number} b amount to scale the matrix's elements by
* @returns {mat3} out
*/
function multiplyScalar$1(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
out[4] = a[4] * b;
out[5] = a[5] * b;
out[6] = a[6] * b;
out[7] = a[7] * b;
out[8] = a[8] * b;
return out;
}
/**
* Adds two mat3's after multiplying each element of the second operand by a scalar value.
*
* @param {mat3} out the receiving vector
* @param {ReadonlyMat3} a the first operand
* @param {ReadonlyMat3} b the second operand
* @param {Number} scale the amount to scale b's elements by before adding
* @returns {mat3} out
*/
function multiplyScalarAndAdd$1(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
out[3] = a[3] + b[3] * scale;
out[4] = a[4] + b[4] * scale;
out[5] = a[5] + b[5] * scale;
out[6] = a[6] + b[6] * scale;
out[7] = a[7] + b[7] * scale;
out[8] = a[8] + b[8] * scale;
return out;
}
/**
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyMat3} a The first matrix.
* @param {ReadonlyMat3} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function exactEquals$6(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8];
}
/**
* Returns whether or not the matrices have approximately the same elements in the same position.
*
* @param {ReadonlyMat3} a The first matrix.
* @param {ReadonlyMat3} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function equals$8(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5],
a6 = a[6],
a7 = a[7],
a8 = a[8];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3],
b4 = b[4],
b5 = b[5],
b6 = b[6],
b7 = b[7],
b8 = b[8];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8));
}
/**
* Alias for {@link mat3.multiply}
* @function
*/
var mul$6 = multiply$6;
/**
* Alias for {@link mat3.subtract}
* @function
*/
var sub$4 = subtract$4;
var mat3 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$6,
adjoint: adjoint$1,
clone: clone$7,
copy: copy$6,
create: create$6,
determinant: determinant$1,
equals: equals$8,
exactEquals: exactEquals$6,
frob: frob$1,
fromMat2d: fromMat2d,
fromMat4: fromMat4$1,
fromQuat: fromQuat$1,
fromRotation: fromRotation$2,
fromScaling: fromScaling$1,
fromTranslation: fromTranslation$2,
fromValues: fromValues$6,
identity: identity$4,
invert: invert$3,
mul: mul$6,
multiply: multiply$6,
multiplyScalar: multiplyScalar$1,
multiplyScalarAndAdd: multiplyScalarAndAdd$1,
normalFromMat4: normalFromMat4,
projection: projection,
rotate: rotate$3,
scale: scale$6,
set: set$6,
str: str$6,
sub: sub$4,
subtract: subtract$4,
translate: translate$3,
transpose: transpose$1
});
/**
* 4x4 Matrix
Format: column-major, when typed out it looks like row-major
The matrices are being post multiplied.
* @module mat4
*/
/**
* Creates a new identity mat4
*
* @returns {mat4} a new 4x4 matrix
*/
function create$5() {
var out = new ARRAY_TYPE(16);
if (ARRAY_TYPE != Float32Array) {
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
}
out[0] = 1;
out[5] = 1;
out[10] = 1;
out[15] = 1;
return out;
}
/**
* Creates a new mat4 initialized with values from an existing matrix
*
* @param {ReadonlyMat4} a matrix to clone
* @returns {mat4} a new 4x4 matrix
*/
function clone$6(a) {
var out = new ARRAY_TYPE(16);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
out[9] = a[9];
out[10] = a[10];
out[11] = a[11];
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
return out;
}
/**
* Copy the values from one mat4 to another
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the source matrix
* @returns {mat4} out
*/
function copy$5(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[8] = a[8];
out[9] = a[9];
out[10] = a[10];
out[11] = a[11];
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
return out;
}
/**
* Create a new mat4 with the given values
*
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m02 Component in column 0, row 2 position (index 2)
* @param {Number} m03 Component in column 0, row 3 position (index 3)
* @param {Number} m10 Component in column 1, row 0 position (index 4)
* @param {Number} m11 Component in column 1, row 1 position (index 5)
* @param {Number} m12 Component in column 1, row 2 position (index 6)
* @param {Number} m13 Component in column 1, row 3 position (index 7)
* @param {Number} m20 Component in column 2, row 0 position (index 8)
* @param {Number} m21 Component in column 2, row 1 position (index 9)
* @param {Number} m22 Component in column 2, row 2 position (index 10)
* @param {Number} m23 Component in column 2, row 3 position (index 11)
* @param {Number} m30 Component in column 3, row 0 position (index 12)
* @param {Number} m31 Component in column 3, row 1 position (index 13)
* @param {Number} m32 Component in column 3, row 2 position (index 14)
* @param {Number} m33 Component in column 3, row 3 position (index 15)
* @returns {mat4} A new mat4
*/
function fromValues$5(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
var out = new ARRAY_TYPE(16);
out[0] = m00;
out[1] = m01;
out[2] = m02;
out[3] = m03;
out[4] = m10;
out[5] = m11;
out[6] = m12;
out[7] = m13;
out[8] = m20;
out[9] = m21;
out[10] = m22;
out[11] = m23;
out[12] = m30;
out[13] = m31;
out[14] = m32;
out[15] = m33;
return out;
}
/**
* Set the components of a mat4 to the given values
*
* @param {mat4} out the receiving matrix
* @param {Number} m00 Component in column 0, row 0 position (index 0)
* @param {Number} m01 Component in column 0, row 1 position (index 1)
* @param {Number} m02 Component in column 0, row 2 position (index 2)
* @param {Number} m03 Component in column 0, row 3 position (index 3)
* @param {Number} m10 Component in column 1, row 0 position (index 4)
* @param {Number} m11 Component in column 1, row 1 position (index 5)
* @param {Number} m12 Component in column 1, row 2 position (index 6)
* @param {Number} m13 Component in column 1, row 3 position (index 7)
* @param {Number} m20 Component in column 2, row 0 position (index 8)
* @param {Number} m21 Component in column 2, row 1 position (index 9)
* @param {Number} m22 Component in column 2, row 2 position (index 10)
* @param {Number} m23 Component in column 2, row 3 position (index 11)
* @param {Number} m30 Component in column 3, row 0 position (index 12)
* @param {Number} m31 Component in column 3, row 1 position (index 13)
* @param {Number} m32 Component in column 3, row 2 position (index 14)
* @param {Number} m33 Component in column 3, row 3 position (index 15)
* @returns {mat4} out
*/
function set$5(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
out[0] = m00;
out[1] = m01;
out[2] = m02;
out[3] = m03;
out[4] = m10;
out[5] = m11;
out[6] = m12;
out[7] = m13;
out[8] = m20;
out[9] = m21;
out[10] = m22;
out[11] = m23;
out[12] = m30;
out[13] = m31;
out[14] = m32;
out[15] = m33;
return out;
}
/**
* Set a mat4 to the identity matrix
*
* @param {mat4} out the receiving matrix
* @returns {mat4} out
*/
function identity$3(out) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Transpose the values of a mat4
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the source matrix
* @returns {mat4} out
*/
function transpose(out, a) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (out === a) {
var a01 = a[1],
a02 = a[2],
a03 = a[3];
var a12 = a[6],
a13 = a[7];
var a23 = a[11];
out[1] = a[4];
out[2] = a[8];
out[3] = a[12];
out[4] = a01;
out[6] = a[9];
out[7] = a[13];
out[8] = a02;
out[9] = a12;
out[11] = a[14];
out[12] = a03;
out[13] = a13;
out[14] = a23;
} else {
out[0] = a[0];
out[1] = a[4];
out[2] = a[8];
out[3] = a[12];
out[4] = a[1];
out[5] = a[5];
out[6] = a[9];
out[7] = a[13];
out[8] = a[2];
out[9] = a[6];
out[10] = a[10];
out[11] = a[14];
out[12] = a[3];
out[13] = a[7];
out[14] = a[11];
out[15] = a[15];
}
return out;
}
/**
* Inverts a mat4
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the source matrix
* @returns {mat4 | null} out, or null if source matrix is not invertible
*/
function invert$2(out, a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
// Calculate the determinant
var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
if (!det) {
return null;
}
det = 1.0 / det;
out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
return out;
}
/**
* Calculates the adjugate of a mat4
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the source matrix
* @returns {mat4} out
*/
function adjoint(out, a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
var b00 = a00 * a11 - a01 * a10;
var b01 = a00 * a12 - a02 * a10;
var b02 = a00 * a13 - a03 * a10;
var b03 = a01 * a12 - a02 * a11;
var b04 = a01 * a13 - a03 * a11;
var b05 = a02 * a13 - a03 * a12;
var b06 = a20 * a31 - a21 * a30;
var b07 = a20 * a32 - a22 * a30;
var b08 = a20 * a33 - a23 * a30;
var b09 = a21 * a32 - a22 * a31;
var b10 = a21 * a33 - a23 * a31;
var b11 = a22 * a33 - a23 * a32;
out[0] = a11 * b11 - a12 * b10 + a13 * b09;
out[1] = a02 * b10 - a01 * b11 - a03 * b09;
out[2] = a31 * b05 - a32 * b04 + a33 * b03;
out[3] = a22 * b04 - a21 * b05 - a23 * b03;
out[4] = a12 * b08 - a10 * b11 - a13 * b07;
out[5] = a00 * b11 - a02 * b08 + a03 * b07;
out[6] = a32 * b02 - a30 * b05 - a33 * b01;
out[7] = a20 * b05 - a22 * b02 + a23 * b01;
out[8] = a10 * b10 - a11 * b08 + a13 * b06;
out[9] = a01 * b08 - a00 * b10 - a03 * b06;
out[10] = a30 * b04 - a31 * b02 + a33 * b00;
out[11] = a21 * b02 - a20 * b04 - a23 * b00;
out[12] = a11 * b07 - a10 * b09 - a12 * b06;
out[13] = a00 * b09 - a01 * b07 + a02 * b06;
out[14] = a31 * b01 - a30 * b03 - a32 * b00;
out[15] = a20 * b03 - a21 * b01 + a22 * b00;
return out;
}
/**
* Calculates the determinant of a mat4
*
* @param {ReadonlyMat4} a the source matrix
* @returns {Number} determinant of a
*/
function determinant(a) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
var b0 = a00 * a11 - a01 * a10;
var b1 = a00 * a12 - a02 * a10;
var b2 = a01 * a12 - a02 * a11;
var b3 = a20 * a31 - a21 * a30;
var b4 = a20 * a32 - a22 * a30;
var b5 = a21 * a32 - a22 * a31;
var b6 = a00 * b5 - a01 * b4 + a02 * b3;
var b7 = a10 * b5 - a11 * b4 + a12 * b3;
var b8 = a20 * b2 - a21 * b1 + a22 * b0;
var b9 = a30 * b2 - a31 * b1 + a32 * b0;
// Calculate the determinant
return a13 * b6 - a03 * b7 + a33 * b8 - a23 * b9;
}
/**
* Multiplies two mat4s
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the first operand
* @param {ReadonlyMat4} b the second operand
* @returns {mat4} out
*/
function multiply$5(out, a, b) {
var a00 = a[0],
a01 = a[1],
a02 = a[2],
a03 = a[3];
var a10 = a[4],
a11 = a[5],
a12 = a[6],
a13 = a[7];
var a20 = a[8],
a21 = a[9],
a22 = a[10],
a23 = a[11];
var a30 = a[12],
a31 = a[13],
a32 = a[14],
a33 = a[15];
// Cache only the current line of the second matrix
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[4];
b1 = b[5];
b2 = b[6];
b3 = b[7];
out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[8];
b1 = b[9];
b2 = b[10];
b3 = b[11];
out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
b0 = b[12];
b1 = b[13];
b2 = b[14];
b3 = b[15];
out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
return out;
}
/**
* Translate a mat4 by the given vector
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to translate
* @param {ReadonlyVec3} v vector to translate by
* @returns {mat4} out
*/
function translate$2(out, a, v) {
var x = v[0],
y = v[1],
z = v[2];
var a00, a01, a02, a03;
var a10, a11, a12, a13;
var a20, a21, a22, a23;
if (a === out) {
out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
} else {
a00 = a[0];
a01 = a[1];
a02 = a[2];
a03 = a[3];
a10 = a[4];
a11 = a[5];
a12 = a[6];
a13 = a[7];
a20 = a[8];
a21 = a[9];
a22 = a[10];
a23 = a[11];
out[0] = a00;
out[1] = a01;
out[2] = a02;
out[3] = a03;
out[4] = a10;
out[5] = a11;
out[6] = a12;
out[7] = a13;
out[8] = a20;
out[9] = a21;
out[10] = a22;
out[11] = a23;
out[12] = a00 * x + a10 * y + a20 * z + a[12];
out[13] = a01 * x + a11 * y + a21 * z + a[13];
out[14] = a02 * x + a12 * y + a22 * z + a[14];
out[15] = a03 * x + a13 * y + a23 * z + a[15];
}
return out;
}
/**
* Scales the mat4 by the dimensions in the given vec3 not using vectorization
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to scale
* @param {ReadonlyVec3} v the vec3 to scale the matrix by
* @returns {mat4} out
**/
function scale$5(out, a, v) {
var x = v[0],
y = v[1],
z = v[2];
out[0] = a[0] * x;
out[1] = a[1] * x;
out[2] = a[2] * x;
out[3] = a[3] * x;
out[4] = a[4] * y;
out[5] = a[5] * y;
out[6] = a[6] * y;
out[7] = a[7] * y;
out[8] = a[8] * z;
out[9] = a[9] * z;
out[10] = a[10] * z;
out[11] = a[11] * z;
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
return out;
}
/**
* Rotates a mat4 by the given angle around the given axis
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @param {ReadonlyVec3} axis the axis to rotate around
* @returns {mat4} out
*/
function rotate$2(out, a, rad, axis) {
var x = axis[0],
y = axis[1],
z = axis[2];
var len = Math.sqrt(x * x + y * y + z * z);
var s, c, t;
var a00, a01, a02, a03;
var a10, a11, a12, a13;
var a20, a21, a22, a23;
var b00, b01, b02;
var b10, b11, b12;
var b20, b21, b22;
if (len < EPSILON) {
return null;
}
len = 1 / len;
x *= len;
y *= len;
z *= len;
s = Math.sin(rad);
c = Math.cos(rad);
t = 1 - c;
a00 = a[0];
a01 = a[1];
a02 = a[2];
a03 = a[3];
a10 = a[4];
a11 = a[5];
a12 = a[6];
a13 = a[7];
a20 = a[8];
a21 = a[9];
a22 = a[10];
a23 = a[11];
// Construct the elements of the rotation matrix
b00 = x * x * t + c;
b01 = y * x * t + z * s;
b02 = z * x * t - y * s;
b10 = x * y * t - z * s;
b11 = y * y * t + c;
b12 = z * y * t + x * s;
b20 = x * z * t + y * s;
b21 = y * z * t - x * s;
b22 = z * z * t + c;
// Perform rotation-specific matrix multiplication
out[0] = a00 * b00 + a10 * b01 + a20 * b02;
out[1] = a01 * b00 + a11 * b01 + a21 * b02;
out[2] = a02 * b00 + a12 * b01 + a22 * b02;
out[3] = a03 * b00 + a13 * b01 + a23 * b02;
out[4] = a00 * b10 + a10 * b11 + a20 * b12;
out[5] = a01 * b10 + a11 * b11 + a21 * b12;
out[6] = a02 * b10 + a12 * b11 + a22 * b12;
out[7] = a03 * b10 + a13 * b11 + a23 * b12;
out[8] = a00 * b20 + a10 * b21 + a20 * b22;
out[9] = a01 * b20 + a11 * b21 + a21 * b22;
out[10] = a02 * b20 + a12 * b21 + a22 * b22;
out[11] = a03 * b20 + a13 * b21 + a23 * b22;
if (a !== out) {
// If the source and destination differ, copy the unchanged last row
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
}
return out;
}
/**
* Rotates a matrix by the given angle around the X axis
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function rotateX$3(out, a, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
var a10 = a[4];
var a11 = a[5];
var a12 = a[6];
var a13 = a[7];
var a20 = a[8];
var a21 = a[9];
var a22 = a[10];
var a23 = a[11];
if (a !== out) {
// If the source and destination differ, copy the unchanged rows
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
}
// Perform axis-specific matrix multiplication
out[4] = a10 * c + a20 * s;
out[5] = a11 * c + a21 * s;
out[6] = a12 * c + a22 * s;
out[7] = a13 * c + a23 * s;
out[8] = a20 * c - a10 * s;
out[9] = a21 * c - a11 * s;
out[10] = a22 * c - a12 * s;
out[11] = a23 * c - a13 * s;
return out;
}
/**
* Rotates a matrix by the given angle around the Y axis
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function rotateY$3(out, a, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a03 = a[3];
var a20 = a[8];
var a21 = a[9];
var a22 = a[10];
var a23 = a[11];
if (a !== out) {
// If the source and destination differ, copy the unchanged rows
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
}
// Perform axis-specific matrix multiplication
out[0] = a00 * c - a20 * s;
out[1] = a01 * c - a21 * s;
out[2] = a02 * c - a22 * s;
out[3] = a03 * c - a23 * s;
out[8] = a00 * s + a20 * c;
out[9] = a01 * s + a21 * c;
out[10] = a02 * s + a22 * c;
out[11] = a03 * s + a23 * c;
return out;
}
/**
* Rotates a matrix by the given angle around the Z axis
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function rotateZ$3(out, a, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
var a00 = a[0];
var a01 = a[1];
var a02 = a[2];
var a03 = a[3];
var a10 = a[4];
var a11 = a[5];
var a12 = a[6];
var a13 = a[7];
if (a !== out) {
// If the source and destination differ, copy the unchanged last row
out[8] = a[8];
out[9] = a[9];
out[10] = a[10];
out[11] = a[11];
out[12] = a[12];
out[13] = a[13];
out[14] = a[14];
out[15] = a[15];
}
// Perform axis-specific matrix multiplication
out[0] = a00 * c + a10 * s;
out[1] = a01 * c + a11 * s;
out[2] = a02 * c + a12 * s;
out[3] = a03 * c + a13 * s;
out[4] = a10 * c - a00 * s;
out[5] = a11 * c - a01 * s;
out[6] = a12 * c - a02 * s;
out[7] = a13 * c - a03 * s;
return out;
}
/**
* Creates a matrix from a vector translation
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, dest, vec);
*
* @param {mat4} out mat4 receiving operation result
* @param {ReadonlyVec3} v Translation vector
* @returns {mat4} out
*/
function fromTranslation$1(out, v) {
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = v[0];
out[13] = v[1];
out[14] = v[2];
out[15] = 1;
return out;
}
/**
* Creates a matrix from a vector scaling
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.scale(dest, dest, vec);
*
* @param {mat4} out mat4 receiving operation result
* @param {ReadonlyVec3} v Scaling vector
* @returns {mat4} out
*/
function fromScaling(out, v) {
out[0] = v[0];
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = v[1];
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = v[2];
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Creates a matrix from a given angle around a given axis
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.rotate(dest, dest, rad, axis);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @param {ReadonlyVec3} axis the axis to rotate around
* @returns {mat4} out
*/
function fromRotation$1(out, rad, axis) {
var x = axis[0],
y = axis[1],
z = axis[2];
var len = Math.sqrt(x * x + y * y + z * z);
var s, c, t;
if (len < EPSILON) {
return null;
}
len = 1 / len;
x *= len;
y *= len;
z *= len;
s = Math.sin(rad);
c = Math.cos(rad);
t = 1 - c;
// Perform rotation-specific matrix multiplication
out[0] = x * x * t + c;
out[1] = y * x * t + z * s;
out[2] = z * x * t - y * s;
out[3] = 0;
out[4] = x * y * t - z * s;
out[5] = y * y * t + c;
out[6] = z * y * t + x * s;
out[7] = 0;
out[8] = x * z * t + y * s;
out[9] = y * z * t - x * s;
out[10] = z * z * t + c;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Creates a matrix from the given angle around the X axis
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.rotateX(dest, dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function fromXRotation(out, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
// Perform axis-specific matrix multiplication
out[0] = 1;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = c;
out[6] = s;
out[7] = 0;
out[8] = 0;
out[9] = -s;
out[10] = c;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Creates a matrix from the given angle around the Y axis
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.rotateY(dest, dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function fromYRotation(out, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
// Perform axis-specific matrix multiplication
out[0] = c;
out[1] = 0;
out[2] = -s;
out[3] = 0;
out[4] = 0;
out[5] = 1;
out[6] = 0;
out[7] = 0;
out[8] = s;
out[9] = 0;
out[10] = c;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Creates a matrix from the given angle around the Z axis
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.rotateZ(dest, dest, rad);
*
* @param {mat4} out mat4 receiving operation result
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat4} out
*/
function fromZRotation(out, rad) {
var s = Math.sin(rad);
var c = Math.cos(rad);
// Perform axis-specific matrix multiplication
out[0] = c;
out[1] = s;
out[2] = 0;
out[3] = 0;
out[4] = -s;
out[5] = c;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 1;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Creates a matrix from a quaternion rotation and vector translation
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, dest, vec);
* let quatMat = mat4.create();
* mat4.fromQuat(quatMat, quat);
* mat4.multiply(dest, dest, quatMat);
*
* @param {mat4} out mat4 receiving operation result
* @param {quat} q Rotation quaternion
* @param {ReadonlyVec3} v Translation vector
* @returns {mat4} out
*/
function fromRotationTranslation$1(out, q, v) {
// Quaternion math
var x = q[0],
y = q[1],
z = q[2],
w = q[3];
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var xy = x * y2;
var xz = x * z2;
var yy = y * y2;
var yz = y * z2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
out[0] = 1 - (yy + zz);
out[1] = xy + wz;
out[2] = xz - wy;
out[3] = 0;
out[4] = xy - wz;
out[5] = 1 - (xx + zz);
out[6] = yz + wx;
out[7] = 0;
out[8] = xz + wy;
out[9] = yz - wx;
out[10] = 1 - (xx + yy);
out[11] = 0;
out[12] = v[0];
out[13] = v[1];
out[14] = v[2];
out[15] = 1;
return out;
}
/**
* Creates a new mat4 from a dual quat.
*
* @param {mat4} out Matrix
* @param {ReadonlyQuat2} a Dual Quaternion
* @returns {mat4} mat4 receiving operation result
*/
function fromQuat2(out, a) {
var translation = new ARRAY_TYPE(3);
var bx = -a[0],
by = -a[1],
bz = -a[2],
bw = a[3],
ax = a[4],
ay = a[5],
az = a[6],
aw = a[7];
var magnitude = bx * bx + by * by + bz * bz + bw * bw;
//Only scale if it makes sense
if (magnitude > 0) {
translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2 / magnitude;
translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2 / magnitude;
translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2 / magnitude;
} else {
translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
}
fromRotationTranslation$1(out, a, translation);
return out;
}
/**
* Returns the translation vector component of a transformation
* matrix. If a matrix is built with fromRotationTranslation,
* the returned vector will be the same as the translation vector
* originally supplied.
* @param {vec3} out Vector to receive translation component
* @param {ReadonlyMat4} mat Matrix to be decomposed (input)
* @return {vec3} out
*/
function getTranslation$1(out, mat) {
out[0] = mat[12];
out[1] = mat[13];
out[2] = mat[14];
return out;
}
/**
* Returns the scaling factor component of a transformation
* matrix. If a matrix is built with fromRotationTranslationScale
* with a normalized Quaternion parameter, the returned vector will be
* the same as the scaling vector
* originally supplied.
* @param {vec3} out Vector to receive scaling factor component
* @param {ReadonlyMat4} mat Matrix to be decomposed (input)
* @return {vec3} out
*/
function getScaling(out, mat) {
var m11 = mat[0];
var m12 = mat[1];
var m13 = mat[2];
var m21 = mat[4];
var m22 = mat[5];
var m23 = mat[6];
var m31 = mat[8];
var m32 = mat[9];
var m33 = mat[10];
out[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13);
out[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23);
out[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33);
return out;
}
/**
* Returns a quaternion representing the rotational component
* of a transformation matrix. If a matrix is built with
* fromRotationTranslation, the returned quaternion will be the
* same as the quaternion originally supplied.
* @param {quat} out Quaternion to receive the rotation component
* @param {ReadonlyMat4} mat Matrix to be decomposed (input)
* @return {quat} out
*/
function getRotation(out, mat) {
var scaling = new ARRAY_TYPE(3);
getScaling(scaling, mat);
var is1 = 1 / scaling[0];
var is2 = 1 / scaling[1];
var is3 = 1 / scaling[2];
var sm11 = mat[0] * is1;
var sm12 = mat[1] * is2;
var sm13 = mat[2] * is3;
var sm21 = mat[4] * is1;
var sm22 = mat[5] * is2;
var sm23 = mat[6] * is3;
var sm31 = mat[8] * is1;
var sm32 = mat[9] * is2;
var sm33 = mat[10] * is3;
var trace = sm11 + sm22 + sm33;
var S = 0;
if (trace > 0) {
S = Math.sqrt(trace + 1.0) * 2;
out[3] = 0.25 * S;
out[0] = (sm23 - sm32) / S;
out[1] = (sm31 - sm13) / S;
out[2] = (sm12 - sm21) / S;
} else if (sm11 > sm22 && sm11 > sm33) {
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
out[3] = (sm23 - sm32) / S;
out[0] = 0.25 * S;
out[1] = (sm12 + sm21) / S;
out[2] = (sm31 + sm13) / S;
} else if (sm22 > sm33) {
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
out[3] = (sm31 - sm13) / S;
out[0] = (sm12 + sm21) / S;
out[1] = 0.25 * S;
out[2] = (sm23 + sm32) / S;
} else {
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
out[3] = (sm12 - sm21) / S;
out[0] = (sm31 + sm13) / S;
out[1] = (sm23 + sm32) / S;
out[2] = 0.25 * S;
}
return out;
}
/**
* Decomposes a transformation matrix into its rotation, translation
* and scale components. Returns only the rotation component
* @param {quat} out_r Quaternion to receive the rotation component
* @param {vec3} out_t Vector to receive the translation vector
* @param {vec3} out_s Vector to receive the scaling factor
* @param {ReadonlyMat4} mat Matrix to be decomposed (input)
* @returns {quat} out_r
*/
function decompose(out_r, out_t, out_s, mat) {
out_t[0] = mat[12];
out_t[1] = mat[13];
out_t[2] = mat[14];
var m11 = mat[0];
var m12 = mat[1];
var m13 = mat[2];
var m21 = mat[4];
var m22 = mat[5];
var m23 = mat[6];
var m31 = mat[8];
var m32 = mat[9];
var m33 = mat[10];
out_s[0] = Math.sqrt(m11 * m11 + m12 * m12 + m13 * m13);
out_s[1] = Math.sqrt(m21 * m21 + m22 * m22 + m23 * m23);
out_s[2] = Math.sqrt(m31 * m31 + m32 * m32 + m33 * m33);
var is1 = 1 / out_s[0];
var is2 = 1 / out_s[1];
var is3 = 1 / out_s[2];
var sm11 = m11 * is1;
var sm12 = m12 * is2;
var sm13 = m13 * is3;
var sm21 = m21 * is1;
var sm22 = m22 * is2;
var sm23 = m23 * is3;
var sm31 = m31 * is1;
var sm32 = m32 * is2;
var sm33 = m33 * is3;
var trace = sm11 + sm22 + sm33;
var S = 0;
if (trace > 0) {
S = Math.sqrt(trace + 1.0) * 2;
out_r[3] = 0.25 * S;
out_r[0] = (sm23 - sm32) / S;
out_r[1] = (sm31 - sm13) / S;
out_r[2] = (sm12 - sm21) / S;
} else if (sm11 > sm22 && sm11 > sm33) {
S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
out_r[3] = (sm23 - sm32) / S;
out_r[0] = 0.25 * S;
out_r[1] = (sm12 + sm21) / S;
out_r[2] = (sm31 + sm13) / S;
} else if (sm22 > sm33) {
S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
out_r[3] = (sm31 - sm13) / S;
out_r[0] = (sm12 + sm21) / S;
out_r[1] = 0.25 * S;
out_r[2] = (sm23 + sm32) / S;
} else {
S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
out_r[3] = (sm12 - sm21) / S;
out_r[0] = (sm31 + sm13) / S;
out_r[1] = (sm23 + sm32) / S;
out_r[2] = 0.25 * S;
}
return out_r;
}
/**
* Creates a matrix from a quaternion rotation, vector translation and vector scale
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, dest, vec);
* let quatMat = mat4.create();
* mat4.fromQuat(quatMat, quat);
* mat4.multiply(dest, dest, quatMat);
* mat4.scale(dest, dest, scale)
*
* @param {mat4} out mat4 receiving operation result
* @param {quat} q Rotation quaternion
* @param {ReadonlyVec3} v Translation vector
* @param {ReadonlyVec3} s Scaling vector
* @returns {mat4} out
*/
function fromRotationTranslationScale(out, q, v, s) {
// Quaternion math
var x = q[0],
y = q[1],
z = q[2],
w = q[3];
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var xy = x * y2;
var xz = x * z2;
var yy = y * y2;
var yz = y * z2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
var sx = s[0];
var sy = s[1];
var sz = s[2];
out[0] = (1 - (yy + zz)) * sx;
out[1] = (xy + wz) * sx;
out[2] = (xz - wy) * sx;
out[3] = 0;
out[4] = (xy - wz) * sy;
out[5] = (1 - (xx + zz)) * sy;
out[6] = (yz + wx) * sy;
out[7] = 0;
out[8] = (xz + wy) * sz;
out[9] = (yz - wx) * sz;
out[10] = (1 - (xx + yy)) * sz;
out[11] = 0;
out[12] = v[0];
out[13] = v[1];
out[14] = v[2];
out[15] = 1;
return out;
}
/**
* Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
* This is equivalent to (but much faster than):
*
* mat4.identity(dest);
* mat4.translate(dest, dest, vec);
* mat4.translate(dest, dest, origin);
* let quatMat = mat4.create();
* mat4.fromQuat(quatMat, quat);
* mat4.multiply(dest, dest, quatMat);
* mat4.scale(dest, dest, scale)
* mat4.translate(dest, dest, negativeOrigin);
*
* @param {mat4} out mat4 receiving operation result
* @param {quat} q Rotation quaternion
* @param {ReadonlyVec3} v Translation vector
* @param {ReadonlyVec3} s Scaling vector
* @param {ReadonlyVec3} o The origin vector around which to scale and rotate
* @returns {mat4} out
*/
function fromRotationTranslationScaleOrigin(out, q, v, s, o) {
// Quaternion math
var x = q[0],
y = q[1],
z = q[2],
w = q[3];
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var xy = x * y2;
var xz = x * z2;
var yy = y * y2;
var yz = y * z2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
var sx = s[0];
var sy = s[1];
var sz = s[2];
var ox = o[0];
var oy = o[1];
var oz = o[2];
var out0 = (1 - (yy + zz)) * sx;
var out1 = (xy + wz) * sx;
var out2 = (xz - wy) * sx;
var out4 = (xy - wz) * sy;
var out5 = (1 - (xx + zz)) * sy;
var out6 = (yz + wx) * sy;
var out8 = (xz + wy) * sz;
var out9 = (yz - wx) * sz;
var out10 = (1 - (xx + yy)) * sz;
out[0] = out0;
out[1] = out1;
out[2] = out2;
out[3] = 0;
out[4] = out4;
out[5] = out5;
out[6] = out6;
out[7] = 0;
out[8] = out8;
out[9] = out9;
out[10] = out10;
out[11] = 0;
out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
out[15] = 1;
return out;
}
/**
* Calculates a 4x4 matrix from the given quaternion
*
* @param {mat4} out mat4 receiving operation result
* @param {ReadonlyQuat} q Quaternion to create matrix from
*
* @returns {mat4} out
*/
function fromQuat(out, q) {
var x = q[0],
y = q[1],
z = q[2],
w = q[3];
var x2 = x + x;
var y2 = y + y;
var z2 = z + z;
var xx = x * x2;
var yx = y * x2;
var yy = y * y2;
var zx = z * x2;
var zy = z * y2;
var zz = z * z2;
var wx = w * x2;
var wy = w * y2;
var wz = w * z2;
out[0] = 1 - yy - zz;
out[1] = yx + wz;
out[2] = zx - wy;
out[3] = 0;
out[4] = yx - wz;
out[5] = 1 - xx - zz;
out[6] = zy + wx;
out[7] = 0;
out[8] = zx + wy;
out[9] = zy - wx;
out[10] = 1 - xx - yy;
out[11] = 0;
out[12] = 0;
out[13] = 0;
out[14] = 0;
out[15] = 1;
return out;
}
/**
* Generates a frustum matrix with the given bounds
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {Number} left Left bound of the frustum
* @param {Number} right Right bound of the frustum
* @param {Number} bottom Bottom bound of the frustum
* @param {Number} top Top bound of the frustum
* @param {Number} near Near bound of the frustum
* @param {Number} far Far bound of the frustum
* @returns {mat4} out
*/
function frustum(out, left, right, bottom, top, near, far) {
var rl = 1 / (right - left);
var tb = 1 / (top - bottom);
var nf = 1 / (near - far);
out[0] = near * 2 * rl;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = near * 2 * tb;
out[6] = 0;
out[7] = 0;
out[8] = (right + left) * rl;
out[9] = (top + bottom) * tb;
out[10] = (far + near) * nf;
out[11] = -1;
out[12] = 0;
out[13] = 0;
out[14] = far * near * 2 * nf;
out[15] = 0;
return out;
}
/**
* Generates a perspective projection matrix with the given bounds.
* The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
* which matches WebGL/OpenGL's clip volume.
* Passing null/undefined/no value for far will generate infinite projection matrix.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} fovy Vertical field of view in radians
* @param {number} aspect Aspect ratio. typically viewport width/height
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum, can be null or Infinity
* @returns {mat4} out
*/
function perspectiveNO(out, fovy, aspect, near, far) {
var f = 1.0 / Math.tan(fovy / 2);
out[0] = f / aspect;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = f;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[11] = -1;
out[12] = 0;
out[13] = 0;
out[15] = 0;
if (far != null && far !== Infinity) {
var nf = 1 / (near - far);
out[10] = (far + near) * nf;
out[14] = 2 * far * near * nf;
} else {
out[10] = -1;
out[14] = -2 * near;
}
return out;
}
/**
* Alias for {@link mat4.perspectiveNO}
* @function
*/
var perspective = perspectiveNO;
/**
* Generates a perspective projection matrix suitable for WebGPU with the given bounds.
* The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
* which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
* Passing null/undefined/no value for far will generate infinite projection matrix.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} fovy Vertical field of view in radians
* @param {number} aspect Aspect ratio. typically viewport width/height
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum, can be null or Infinity
* @returns {mat4} out
*/
function perspectiveZO(out, fovy, aspect, near, far) {
var f = 1.0 / Math.tan(fovy / 2);
out[0] = f / aspect;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = f;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[11] = -1;
out[12] = 0;
out[13] = 0;
out[15] = 0;
if (far != null && far !== Infinity) {
var nf = 1 / (near - far);
out[10] = far * nf;
out[14] = far * near * nf;
} else {
out[10] = -1;
out[14] = -near;
}
return out;
}
/**
* Generates a perspective projection matrix with the given field of view.
* This is primarily useful for generating projection matrices to be used
* with the still experiemental WebVR API.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum
* @returns {mat4} out
*/
function perspectiveFromFieldOfView(out, fov, near, far) {
var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
var xScale = 2.0 / (leftTan + rightTan);
var yScale = 2.0 / (upTan + downTan);
out[0] = xScale;
out[1] = 0.0;
out[2] = 0.0;
out[3] = 0.0;
out[4] = 0.0;
out[5] = yScale;
out[6] = 0.0;
out[7] = 0.0;
out[8] = -((leftTan - rightTan) * xScale * 0.5);
out[9] = (upTan - downTan) * yScale * 0.5;
out[10] = far / (near - far);
out[11] = -1.0;
out[12] = 0.0;
out[13] = 0.0;
out[14] = far * near / (near - far);
out[15] = 0.0;
return out;
}
/**
* Generates a orthogonal projection matrix with the given bounds.
* The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
* which matches WebGL/OpenGL's clip volume.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} left Left bound of the frustum
* @param {number} right Right bound of the frustum
* @param {number} bottom Bottom bound of the frustum
* @param {number} top Top bound of the frustum
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum
* @returns {mat4} out
*/
function orthoNO(out, left, right, bottom, top, near, far) {
var lr = 1 / (left - right);
var bt = 1 / (bottom - top);
var nf = 1 / (near - far);
out[0] = -2 * lr;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = -2 * bt;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = 2 * nf;
out[11] = 0;
out[12] = (left + right) * lr;
out[13] = (top + bottom) * bt;
out[14] = (far + near) * nf;
out[15] = 1;
return out;
}
/**
* Alias for {@link mat4.orthoNO}
* @function
*/
var ortho = orthoNO;
/**
* Generates a orthogonal projection matrix with the given bounds.
* The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
* which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {number} left Left bound of the frustum
* @param {number} right Right bound of the frustum
* @param {number} bottom Bottom bound of the frustum
* @param {number} top Top bound of the frustum
* @param {number} near Near bound of the frustum
* @param {number} far Far bound of the frustum
* @returns {mat4} out
*/
function orthoZO(out, left, right, bottom, top, near, far) {
var lr = 1 / (left - right);
var bt = 1 / (bottom - top);
var nf = 1 / (near - far);
out[0] = -2 * lr;
out[1] = 0;
out[2] = 0;
out[3] = 0;
out[4] = 0;
out[5] = -2 * bt;
out[6] = 0;
out[7] = 0;
out[8] = 0;
out[9] = 0;
out[10] = nf;
out[11] = 0;
out[12] = (left + right) * lr;
out[13] = (top + bottom) * bt;
out[14] = near * nf;
out[15] = 1;
return out;
}
/**
* Generates a look-at matrix with the given eye position, focal point, and up axis.
* If you want a matrix that actually makes an object look at another object, you should use targetTo instead.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {ReadonlyVec3} eye Position of the viewer
* @param {ReadonlyVec3} center Point the viewer is looking at
* @param {ReadonlyVec3} up vec3 pointing up
* @returns {mat4} out
*/
function lookAt(out, eye, center, up) {
var x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
var eyex = eye[0];
var eyey = eye[1];
var eyez = eye[2];
var upx = up[0];
var upy = up[1];
var upz = up[2];
var centerx = center[0];
var centery = center[1];
var centerz = center[2];
if (Math.abs(eyex - centerx) < EPSILON && Math.abs(eyey - centery) < EPSILON && Math.abs(eyez - centerz) < EPSILON) {
return identity$3(out);
}
z0 = eyex - centerx;
z1 = eyey - centery;
z2 = eyez - centerz;
len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
z0 *= len;
z1 *= len;
z2 *= len;
x0 = upy * z2 - upz * z1;
x1 = upz * z0 - upx * z2;
x2 = upx * z1 - upy * z0;
len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
if (!len) {
x0 = 0;
x1 = 0;
x2 = 0;
} else {
len = 1 / len;
x0 *= len;
x1 *= len;
x2 *= len;
}
y0 = z1 * x2 - z2 * x1;
y1 = z2 * x0 - z0 * x2;
y2 = z0 * x1 - z1 * x0;
len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
if (!len) {
y0 = 0;
y1 = 0;
y2 = 0;
} else {
len = 1 / len;
y0 *= len;
y1 *= len;
y2 *= len;
}
out[0] = x0;
out[1] = y0;
out[2] = z0;
out[3] = 0;
out[4] = x1;
out[5] = y1;
out[6] = z1;
out[7] = 0;
out[8] = x2;
out[9] = y2;
out[10] = z2;
out[11] = 0;
out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
out[15] = 1;
return out;
}
/**
* Generates a matrix that makes something look at something else.
*
* @param {mat4} out mat4 frustum matrix will be written into
* @param {ReadonlyVec3} eye Position of the viewer
* @param {ReadonlyVec3} target Point the viewer is looking at
* @param {ReadonlyVec3} up vec3 pointing up
* @returns {mat4} out
*/
function targetTo(out, eye, target, up) {
var eyex = eye[0],
eyey = eye[1],
eyez = eye[2],
upx = up[0],
upy = up[1],
upz = up[2];
var z0 = eyex - target[0],
z1 = eyey - target[1],
z2 = eyez - target[2];
var len = z0 * z0 + z1 * z1 + z2 * z2;
if (len > 0) {
len = 1 / Math.sqrt(len);
z0 *= len;
z1 *= len;
z2 *= len;
}
var x0 = upy * z2 - upz * z1,
x1 = upz * z0 - upx * z2,
x2 = upx * z1 - upy * z0;
len = x0 * x0 + x1 * x1 + x2 * x2;
if (len > 0) {
len = 1 / Math.sqrt(len);
x0 *= len;
x1 *= len;
x2 *= len;
}
out[0] = x0;
out[1] = x1;
out[2] = x2;
out[3] = 0;
out[4] = z1 * x2 - z2 * x1;
out[5] = z2 * x0 - z0 * x2;
out[6] = z0 * x1 - z1 * x0;
out[7] = 0;
out[8] = z0;
out[9] = z1;
out[10] = z2;
out[11] = 0;
out[12] = eyex;
out[13] = eyey;
out[14] = eyez;
out[15] = 1;
return out;
}
/**
* Returns a string representation of a mat4
*
* @param {ReadonlyMat4} a matrix to represent as a string
* @returns {String} string representation of the matrix
*/
function str$5(a) {
return "mat4(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ", " + a[9] + ", " + a[10] + ", " + a[11] + ", " + a[12] + ", " + a[13] + ", " + a[14] + ", " + a[15] + ")";
}
/**
* Returns Frobenius norm of a mat4
*
* @param {ReadonlyMat4} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function frob(a) {
return Math.sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3] + a[4] * a[4] + a[5] * a[5] + a[6] * a[6] + a[7] * a[7] + a[8] * a[8] + a[9] * a[9] + a[10] * a[10] + a[11] * a[11] + a[12] * a[12] + a[13] * a[13] + a[14] * a[14] + a[15] * a[15]);
}
/**
* Adds two mat4's
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the first operand
* @param {ReadonlyMat4} b the second operand
* @returns {mat4} out
*/
function add$5(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
out[5] = a[5] + b[5];
out[6] = a[6] + b[6];
out[7] = a[7] + b[7];
out[8] = a[8] + b[8];
out[9] = a[9] + b[9];
out[10] = a[10] + b[10];
out[11] = a[11] + b[11];
out[12] = a[12] + b[12];
out[13] = a[13] + b[13];
out[14] = a[14] + b[14];
out[15] = a[15] + b[15];
return out;
}
/**
* Subtracts matrix b from matrix a
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the first operand
* @param {ReadonlyMat4} b the second operand
* @returns {mat4} out
*/
function subtract$3(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
out[4] = a[4] - b[4];
out[5] = a[5] - b[5];
out[6] = a[6] - b[6];
out[7] = a[7] - b[7];
out[8] = a[8] - b[8];
out[9] = a[9] - b[9];
out[10] = a[10] - b[10];
out[11] = a[11] - b[11];
out[12] = a[12] - b[12];
out[13] = a[13] - b[13];
out[14] = a[14] - b[14];
out[15] = a[15] - b[15];
return out;
}
/**
* Multiply each element of the matrix by a scalar.
*
* @param {mat4} out the receiving matrix
* @param {ReadonlyMat4} a the matrix to scale
* @param {Number} b amount to scale the matrix's elements by
* @returns {mat4} out
*/
function multiplyScalar(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
out[4] = a[4] * b;
out[5] = a[5] * b;
out[6] = a[6] * b;
out[7] = a[7] * b;
out[8] = a[8] * b;
out[9] = a[9] * b;
out[10] = a[10] * b;
out[11] = a[11] * b;
out[12] = a[12] * b;
out[13] = a[13] * b;
out[14] = a[14] * b;
out[15] = a[15] * b;
return out;
}
/**
* Adds two mat4's after multiplying each element of the second operand by a scalar value.
*
* @param {mat4} out the receiving vector
* @param {ReadonlyMat4} a the first operand
* @param {ReadonlyMat4} b the second operand
* @param {Number} scale the amount to scale b's elements by before adding
* @returns {mat4} out
*/
function multiplyScalarAndAdd(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
out[3] = a[3] + b[3] * scale;
out[4] = a[4] + b[4] * scale;
out[5] = a[5] + b[5] * scale;
out[6] = a[6] + b[6] * scale;
out[7] = a[7] + b[7] * scale;
out[8] = a[8] + b[8] * scale;
out[9] = a[9] + b[9] * scale;
out[10] = a[10] + b[10] * scale;
out[11] = a[11] + b[11] * scale;
out[12] = a[12] + b[12] * scale;
out[13] = a[13] + b[13] * scale;
out[14] = a[14] + b[14] * scale;
out[15] = a[15] + b[15] * scale;
return out;
}
/**
* Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyMat4} a The first matrix.
* @param {ReadonlyMat4} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function exactEquals$5(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15];
}
/**
* Returns whether or not the matrices have approximately the same elements in the same position.
*
* @param {ReadonlyMat4} a The first matrix.
* @param {ReadonlyMat4} b The second matrix.
* @returns {Boolean} True if the matrices are equal, false otherwise.
*/
function equals$7(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var a4 = a[4],
a5 = a[5],
a6 = a[6],
a7 = a[7];
var a8 = a[8],
a9 = a[9],
a10 = a[10],
a11 = a[11];
var a12 = a[12],
a13 = a[13],
a14 = a[14],
a15 = a[15];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
var b4 = b[4],
b5 = b[5],
b6 = b[6],
b7 = b[7];
var b8 = b[8],
b9 = b[9],
b10 = b[10],
b11 = b[11];
var b12 = b[12],
b13 = b[13],
b14 = b[14],
b15 = b[15];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && Math.abs(a9 - b9) <= EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && Math.abs(a10 - b10) <= EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && Math.abs(a11 - b11) <= EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && Math.abs(a12 - b12) <= EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && Math.abs(a13 - b13) <= EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && Math.abs(a14 - b14) <= EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15));
}
/**
* Alias for {@link mat4.multiply}
* @function
*/
var mul$5 = multiply$5;
/**
* Alias for {@link mat4.subtract}
* @function
*/
var sub$3 = subtract$3;
var mat4 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$5,
adjoint: adjoint,
clone: clone$6,
copy: copy$5,
create: create$5,
decompose: decompose,
determinant: determinant,
equals: equals$7,
exactEquals: exactEquals$5,
frob: frob,
fromQuat: fromQuat,
fromQuat2: fromQuat2,
fromRotation: fromRotation$1,
fromRotationTranslation: fromRotationTranslation$1,
fromRotationTranslationScale: fromRotationTranslationScale,
fromRotationTranslationScaleOrigin: fromRotationTranslationScaleOrigin,
fromScaling: fromScaling,
fromTranslation: fromTranslation$1,
fromValues: fromValues$5,
fromXRotation: fromXRotation,
fromYRotation: fromYRotation,
fromZRotation: fromZRotation,
frustum: frustum,
getRotation: getRotation,
getScaling: getScaling,
getTranslation: getTranslation$1,
identity: identity$3,
invert: invert$2,
lookAt: lookAt,
mul: mul$5,
multiply: multiply$5,
multiplyScalar: multiplyScalar,
multiplyScalarAndAdd: multiplyScalarAndAdd,
ortho: ortho,
orthoNO: orthoNO,
orthoZO: orthoZO,
perspective: perspective,
perspectiveFromFieldOfView: perspectiveFromFieldOfView,
perspectiveNO: perspectiveNO,
perspectiveZO: perspectiveZO,
rotate: rotate$2,
rotateX: rotateX$3,
rotateY: rotateY$3,
rotateZ: rotateZ$3,
scale: scale$5,
set: set$5,
str: str$5,
sub: sub$3,
subtract: subtract$3,
targetTo: targetTo,
translate: translate$2,
transpose: transpose
});
/**
* 3 Dimensional Vector
* @module vec3
*/
/**
* Creates a new, empty vec3
*
* @returns {vec3} a new 3D vector
*/
function create$4() {
var out = new ARRAY_TYPE(3);
if (ARRAY_TYPE != Float32Array) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
}
return out;
}
/**
* Creates a new vec3 initialized with values from an existing vector
*
* @param {ReadonlyVec3} a vector to clone
* @returns {vec3} a new 3D vector
*/
function clone$5(a) {
var out = new ARRAY_TYPE(3);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
return out;
}
/**
* Calculates the length of a vec3
*
* @param {ReadonlyVec3} a vector to calculate length of
* @returns {Number} length of a
*/
function length$4(a) {
var x = a[0];
var y = a[1];
var z = a[2];
return Math.sqrt(x * x + y * y + z * z);
}
/**
* Creates a new vec3 initialized with the given values
*
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @returns {vec3} a new 3D vector
*/
function fromValues$4(x, y, z) {
var out = new ARRAY_TYPE(3);
out[0] = x;
out[1] = y;
out[2] = z;
return out;
}
/**
* Copy the values from one vec3 to another
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the source vector
* @returns {vec3} out
*/
function copy$4(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
return out;
}
/**
* Set the components of a vec3 to the given values
*
* @param {vec3} out the receiving vector
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @returns {vec3} out
*/
function set$4(out, x, y, z) {
out[0] = x;
out[1] = y;
out[2] = z;
return out;
}
/**
* Adds two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function add$4(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
return out;
}
/**
* Subtracts vector b from vector a
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function subtract$2(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
return out;
}
/**
* Multiplies two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function multiply$4(out, a, b) {
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
out[2] = a[2] * b[2];
return out;
}
/**
* Divides two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function divide$2(out, a, b) {
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
out[2] = a[2] / b[2];
return out;
}
/**
* Math.ceil the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to ceil
* @returns {vec3} out
*/
function ceil$2(out, a) {
out[0] = Math.ceil(a[0]);
out[1] = Math.ceil(a[1]);
out[2] = Math.ceil(a[2]);
return out;
}
/**
* Math.floor the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to floor
* @returns {vec3} out
*/
function floor$3(out, a) {
out[0] = Math.floor(a[0]);
out[1] = Math.floor(a[1]);
out[2] = Math.floor(a[2]);
return out;
}
/**
* Returns the minimum of two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function min$3(out, a, b) {
out[0] = Math.min(a[0], b[0]);
out[1] = Math.min(a[1], b[1]);
out[2] = Math.min(a[2], b[2]);
return out;
}
/**
* Returns the maximum of two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function max$4(out, a, b) {
out[0] = Math.max(a[0], b[0]);
out[1] = Math.max(a[1], b[1]);
out[2] = Math.max(a[2], b[2]);
return out;
}
/**
* symmetric round the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to round
* @returns {vec3} out
*/
function round$3(out, a) {
out[0] = round$4(a[0]);
out[1] = round$4(a[1]);
out[2] = round$4(a[2]);
return out;
}
/**
* Scales a vec3 by a scalar number
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {vec3} out
*/
function scale$4(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
return out;
}
/**
* Adds two vec3's after scaling the second operand by a scalar value
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @param {Number} scale the amount to scale b by before adding
* @returns {vec3} out
*/
function scaleAndAdd$2(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
return out;
}
/**
* Calculates the euclidian distance between two vec3's
*
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {Number} distance between a and b
*/
function distance$2(a, b) {
var x = b[0] - a[0];
var y = b[1] - a[1];
var z = b[2] - a[2];
return Math.sqrt(x * x + y * y + z * z);
}
/**
* Calculates the squared euclidian distance between two vec3's
*
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {Number} squared distance between a and b
*/
function squaredDistance$2(a, b) {
var x = b[0] - a[0];
var y = b[1] - a[1];
var z = b[2] - a[2];
return x * x + y * y + z * z;
}
/**
* Calculates the squared length of a vec3
*
* @param {ReadonlyVec3} a vector to calculate squared length of
* @returns {Number} squared length of a
*/
function squaredLength$4(a) {
var x = a[0];
var y = a[1];
var z = a[2];
return x * x + y * y + z * z;
}
/**
* Negates the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to negate
* @returns {vec3} out
*/
function negate$2(out, a) {
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
return out;
}
/**
* Returns the inverse of the components of a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to invert
* @returns {vec3} out
*/
function inverse$2(out, a) {
out[0] = 1.0 / a[0];
out[1] = 1.0 / a[1];
out[2] = 1.0 / a[2];
return out;
}
/**
* Normalize a vec3
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a vector to normalize
* @returns {vec3} out
*/
function normalize$4(out, a) {
var x = a[0];
var y = a[1];
var z = a[2];
var len = x * x + y * y + z * z;
if (len > 0) {
//TODO: evaluate use of glm_invsqrt here?
len = 1 / Math.sqrt(len);
}
out[0] = a[0] * len;
out[1] = a[1] * len;
out[2] = a[2] * len;
return out;
}
/**
* Calculates the dot product of two vec3's
*
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {Number} dot product of a and b
*/
function dot$5(a, b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
/**
* Computes the cross product of two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @returns {vec3} out
*/
function cross$2(out, a, b) {
var ax = a[0],
ay = a[1],
az = a[2];
var bx = b[0],
by = b[1],
bz = b[2];
out[0] = ay * bz - az * by;
out[1] = az * bx - ax * bz;
out[2] = ax * by - ay * bx;
return out;
}
/**
* Performs a linear interpolation between two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec3} out
*/
function lerp$5(out, a, b, t) {
var ax = a[0];
var ay = a[1];
var az = a[2];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
out[2] = az + t * (b[2] - az);
return out;
}
/**
* Performs a spherical linear interpolation between two vec3's
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec3} out
*/
function slerp$2(out, a, b, t) {
var angle = Math.acos(Math.min(Math.max(dot$5(a, b), -1), 1));
var sinTotal = Math.sin(angle);
var ratioA = Math.sin((1 - t) * angle) / sinTotal;
var ratioB = Math.sin(t * angle) / sinTotal;
out[0] = ratioA * a[0] + ratioB * b[0];
out[1] = ratioA * a[1] + ratioB * b[1];
out[2] = ratioA * a[2] + ratioB * b[2];
return out;
}
/**
* Performs a hermite interpolation with two control points
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @param {ReadonlyVec3} c the third operand
* @param {ReadonlyVec3} d the fourth operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec3} out
*/
function hermite(out, a, b, c, d, t) {
var factorTimes2 = t * t;
var factor1 = factorTimes2 * (2 * t - 3) + 1;
var factor2 = factorTimes2 * (t - 2) + t;
var factor3 = factorTimes2 * (t - 1);
var factor4 = factorTimes2 * (3 - 2 * t);
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
return out;
}
/**
* Performs a bezier interpolation with two control points
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the first operand
* @param {ReadonlyVec3} b the second operand
* @param {ReadonlyVec3} c the third operand
* @param {ReadonlyVec3} d the fourth operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec3} out
*/
function bezier$1(out, a, b, c, d, t) {
var inverseFactor = 1 - t;
var inverseFactorTimesTwo = inverseFactor * inverseFactor;
var factorTimes2 = t * t;
var factor1 = inverseFactorTimesTwo * inverseFactor;
var factor2 = 3 * t * inverseFactorTimesTwo;
var factor3 = 3 * factorTimes2 * inverseFactor;
var factor4 = factorTimes2 * t;
out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
return out;
}
/**
* Generates a random vector with the given scale
*
* @param {vec3} out the receiving vector
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned
* @returns {vec3} out
*/
function random$3(out, scale) {
scale = scale === undefined ? 1.0 : scale;
var r = RANDOM() * 2.0 * Math.PI;
var z = RANDOM() * 2.0 - 1.0;
var zScale = Math.sqrt(1.0 - z * z) * scale;
out[0] = Math.cos(r) * zScale;
out[1] = Math.sin(r) * zScale;
out[2] = z * scale;
return out;
}
/**
* Transforms the vec3 with a mat4.
* 4th vector component is implicitly '1'
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the vector to transform
* @param {ReadonlyMat4} m matrix to transform with
* @returns {vec3} out
*/
function transformMat4$2(out, a, m) {
var x = a[0],
y = a[1],
z = a[2];
var w = m[3] * x + m[7] * y + m[11] * z + m[15];
w = w || 1.0;
out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
return out;
}
/**
* Transforms the vec3 with a mat3.
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the vector to transform
* @param {ReadonlyMat3} m the 3x3 matrix to transform with
* @returns {vec3} out
*/
function transformMat3$1(out, a, m) {
var x = a[0],
y = a[1],
z = a[2];
out[0] = x * m[0] + y * m[3] + z * m[6];
out[1] = x * m[1] + y * m[4] + z * m[7];
out[2] = x * m[2] + y * m[5] + z * m[8];
return out;
}
/**
* Transforms the vec3 with a quat
* Can also be used for dual quaternions. (Multiply it with the real part)
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec3} a the vector to transform
* @param {ReadonlyQuat} q normalized quaternion to transform with
* @returns {vec3} out
*/
function transformQuat$1(out, a, q) {
// Fast Vector Rotation using Quaternions by Robert Eisele
// https://raw.org/proof/vector-rotation-using-quaternions/
var qx = q[0],
qy = q[1],
qz = q[2],
qw = q[3];
var vx = a[0],
vy = a[1],
vz = a[2];
// t = q x v
var tx = qy * vz - qz * vy;
var ty = qz * vx - qx * vz;
var tz = qx * vy - qy * vx;
// t = 2t
tx = tx + tx;
ty = ty + ty;
tz = tz + tz;
// v + w t + q x t
out[0] = vx + qw * tx + qy * tz - qz * ty;
out[1] = vy + qw * ty + qz * tx - qx * tz;
out[2] = vz + qw * tz + qx * ty - qy * tx;
return out;
}
/**
* Rotate a 3D vector around the x-axis
* @param {vec3} out The receiving vec3
* @param {ReadonlyVec3} a The vec3 point to rotate
* @param {ReadonlyVec3} b The origin of the rotation
* @param {Number} rad The angle of rotation in radians
* @returns {vec3} out
*/
function rotateX$2(out, a, b, rad) {
var p = [],
r = [];
//Translate point to the origin
p[0] = a[0] - b[0];
p[1] = a[1] - b[1];
p[2] = a[2] - b[2];
//perform rotation
r[0] = p[0];
r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad);
r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad);
//translate to correct position
out[0] = r[0] + b[0];
out[1] = r[1] + b[1];
out[2] = r[2] + b[2];
return out;
}
/**
* Rotate a 3D vector around the y-axis
* @param {vec3} out The receiving vec3
* @param {ReadonlyVec3} a The vec3 point to rotate
* @param {ReadonlyVec3} b The origin of the rotation
* @param {Number} rad The angle of rotation in radians
* @returns {vec3} out
*/
function rotateY$2(out, a, b, rad) {
var p = [],
r = [];
//Translate point to the origin
p[0] = a[0] - b[0];
p[1] = a[1] - b[1];
p[2] = a[2] - b[2];
//perform rotation
r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad);
r[1] = p[1];
r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad);
//translate to correct position
out[0] = r[0] + b[0];
out[1] = r[1] + b[1];
out[2] = r[2] + b[2];
return out;
}
/**
* Rotate a 3D vector around the z-axis
* @param {vec3} out The receiving vec3
* @param {ReadonlyVec3} a The vec3 point to rotate
* @param {ReadonlyVec3} b The origin of the rotation
* @param {Number} rad The angle of rotation in radians
* @returns {vec3} out
*/
function rotateZ$2(out, a, b, rad) {
var p = [],
r = [];
//Translate point to the origin
p[0] = a[0] - b[0];
p[1] = a[1] - b[1];
p[2] = a[2] - b[2];
//perform rotation
r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad);
r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad);
r[2] = p[2];
//translate to correct position
out[0] = r[0] + b[0];
out[1] = r[1] + b[1];
out[2] = r[2] + b[2];
return out;
}
/**
* Get the angle between two 3D vectors
* @param {ReadonlyVec3} a The first operand
* @param {ReadonlyVec3} b The second operand
* @returns {Number} The angle in radians
*/
function angle$1(a, b) {
var ax = a[0],
ay = a[1],
az = a[2],
bx = b[0],
by = b[1],
bz = b[2],
mag = Math.sqrt((ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz)),
cosine = mag && dot$5(a, b) / mag;
return Math.acos(Math.min(Math.max(cosine, -1), 1));
}
/**
* Set the components of a vec3 to zero
*
* @param {vec3} out the receiving vector
* @returns {vec3} out
*/
function zero$2(out) {
out[0] = 0.0;
out[1] = 0.0;
out[2] = 0.0;
return out;
}
/**
* Returns a string representation of a vector
*
* @param {ReadonlyVec3} a vector to represent as a string
* @returns {String} string representation of the vector
*/
function str$4(a) {
return "vec3(" + a[0] + ", " + a[1] + ", " + a[2] + ")";
}
/**
* Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyVec3} a The first vector.
* @param {ReadonlyVec3} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function exactEquals$4(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
}
/**
* Returns whether or not the vectors have approximately the same elements in the same position.
*
* @param {ReadonlyVec3} a The first vector.
* @param {ReadonlyVec3} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function equals$6(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2];
var b0 = b[0],
b1 = b[1],
b2 = b[2];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2));
}
/**
* Alias for {@link vec3.subtract}
* @function
*/
var sub$2 = subtract$2;
/**
* Alias for {@link vec3.multiply}
* @function
*/
var mul$4 = multiply$4;
/**
* Alias for {@link vec3.divide}
* @function
*/
var div$2 = divide$2;
/**
* Alias for {@link vec3.distance}
* @function
*/
var dist$2 = distance$2;
/**
* Alias for {@link vec3.squaredDistance}
* @function
*/
var sqrDist$2 = squaredDistance$2;
/**
* Alias for {@link vec3.length}
* @function
*/
var len$4 = length$4;
/**
* Alias for {@link vec3.squaredLength}
* @function
*/
var sqrLen$4 = squaredLength$4;
/**
* Perform some operation over an array of vec3s.
*
* @param {Array} a the array of vectors to iterate over
* @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
* @param {Number} offset Number of elements to skip at the beginning of the array
* @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
* @param {Function} fn Function to call for each vector in the array
* @param {Object} [arg] additional argument to pass to fn
* @returns {Array} a
* @function
*/
var forEach$2 = function () {
var vec = create$4();
return function (a, stride, offset, count, fn, arg) {
var i, l;
if (!stride) {
stride = 3;
}
if (!offset) {
offset = 0;
}
if (count) {
l = Math.min(count * stride + offset, a.length);
} else {
l = a.length;
}
for (i = offset; i < l; i += stride) {
vec[0] = a[i];
vec[1] = a[i + 1];
vec[2] = a[i + 2];
fn(vec, vec, arg);
a[i] = vec[0];
a[i + 1] = vec[1];
a[i + 2] = vec[2];
}
return a;
};
}();
var vec3 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$4,
angle: angle$1,
bezier: bezier$1,
ceil: ceil$2,
clone: clone$5,
copy: copy$4,
create: create$4,
cross: cross$2,
dist: dist$2,
distance: distance$2,
div: div$2,
divide: divide$2,
dot: dot$5,
equals: equals$6,
exactEquals: exactEquals$4,
floor: floor$3,
forEach: forEach$2,
fromValues: fromValues$4,
hermite: hermite,
inverse: inverse$2,
len: len$4,
length: length$4,
lerp: lerp$5,
max: max$4,
min: min$3,
mul: mul$4,
multiply: multiply$4,
negate: negate$2,
normalize: normalize$4,
random: random$3,
rotateX: rotateX$2,
rotateY: rotateY$2,
rotateZ: rotateZ$2,
round: round$3,
scale: scale$4,
scaleAndAdd: scaleAndAdd$2,
set: set$4,
slerp: slerp$2,
sqrDist: sqrDist$2,
sqrLen: sqrLen$4,
squaredDistance: squaredDistance$2,
squaredLength: squaredLength$4,
str: str$4,
sub: sub$2,
subtract: subtract$2,
transformMat3: transformMat3$1,
transformMat4: transformMat4$2,
transformQuat: transformQuat$1,
zero: zero$2
});
/**
* 4 Dimensional Vector
* @module vec4
*/
/**
* Creates a new, empty vec4
*
* @returns {vec4} a new 4D vector
*/
function create$3() {
var out = new ARRAY_TYPE(4);
if (ARRAY_TYPE != Float32Array) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 0;
}
return out;
}
/**
* Creates a new vec4 initialized with values from an existing vector
*
* @param {ReadonlyVec4} a vector to clone
* @returns {vec4} a new 4D vector
*/
function clone$4(a) {
var out = new ARRAY_TYPE(4);
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
}
/**
* Creates a new vec4 initialized with the given values
*
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @param {Number} w W component
* @returns {vec4} a new 4D vector
*/
function fromValues$3(x, y, z, w) {
var out = new ARRAY_TYPE(4);
out[0] = x;
out[1] = y;
out[2] = z;
out[3] = w;
return out;
}
/**
* Copy the values from one vec4 to another
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the source vector
* @returns {vec4} out
*/
function copy$3(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
return out;
}
/**
* Set the components of a vec4 to the given values
*
* @param {vec4} out the receiving vector
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @param {Number} w W component
* @returns {vec4} out
*/
function set$3(out, x, y, z, w) {
out[0] = x;
out[1] = y;
out[2] = z;
out[3] = w;
return out;
}
/**
* Adds two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function add$3(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
return out;
}
/**
* Subtracts vector b from vector a
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function subtract$1(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
out[2] = a[2] - b[2];
out[3] = a[3] - b[3];
return out;
}
/**
* Multiplies two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function multiply$3(out, a, b) {
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
out[2] = a[2] * b[2];
out[3] = a[3] * b[3];
return out;
}
/**
* Divides two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function divide$1(out, a, b) {
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
out[2] = a[2] / b[2];
out[3] = a[3] / b[3];
return out;
}
/**
* Math.ceil the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to ceil
* @returns {vec4} out
*/
function ceil$1(out, a) {
out[0] = Math.ceil(a[0]);
out[1] = Math.ceil(a[1]);
out[2] = Math.ceil(a[2]);
out[3] = Math.ceil(a[3]);
return out;
}
/**
* Math.floor the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to floor
* @returns {vec4} out
*/
function floor$2(out, a) {
out[0] = Math.floor(a[0]);
out[1] = Math.floor(a[1]);
out[2] = Math.floor(a[2]);
out[3] = Math.floor(a[3]);
return out;
}
/**
* Returns the minimum of two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function min$2(out, a, b) {
out[0] = Math.min(a[0], b[0]);
out[1] = Math.min(a[1], b[1]);
out[2] = Math.min(a[2], b[2]);
out[3] = Math.min(a[3], b[3]);
return out;
}
/**
* Returns the maximum of two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {vec4} out
*/
function max$3(out, a, b) {
out[0] = Math.max(a[0], b[0]);
out[1] = Math.max(a[1], b[1]);
out[2] = Math.max(a[2], b[2]);
out[3] = Math.max(a[3], b[3]);
return out;
}
/**
* symmetric round the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to round
* @returns {vec4} out
*/
function round$2(out, a) {
out[0] = round$4(a[0]);
out[1] = round$4(a[1]);
out[2] = round$4(a[2]);
out[3] = round$4(a[3]);
return out;
}
/**
* Scales a vec4 by a scalar number
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {vec4} out
*/
function scale$3(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
return out;
}
/**
* Adds two vec4's after scaling the second operand by a scalar value
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @param {Number} scale the amount to scale b by before adding
* @returns {vec4} out
*/
function scaleAndAdd$1(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
out[2] = a[2] + b[2] * scale;
out[3] = a[3] + b[3] * scale;
return out;
}
/**
* Calculates the euclidian distance between two vec4's
*
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {Number} distance between a and b
*/
function distance$1(a, b) {
var x = b[0] - a[0];
var y = b[1] - a[1];
var z = b[2] - a[2];
var w = b[3] - a[3];
return Math.sqrt(x * x + y * y + z * z + w * w);
}
/**
* Calculates the squared euclidian distance between two vec4's
*
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {Number} squared distance between a and b
*/
function squaredDistance$1(a, b) {
var x = b[0] - a[0];
var y = b[1] - a[1];
var z = b[2] - a[2];
var w = b[3] - a[3];
return x * x + y * y + z * z + w * w;
}
/**
* Calculates the length of a vec4
*
* @param {ReadonlyVec4} a vector to calculate length of
* @returns {Number} length of a
*/
function length$3(a) {
var x = a[0];
var y = a[1];
var z = a[2];
var w = a[3];
return Math.sqrt(x * x + y * y + z * z + w * w);
}
/**
* Calculates the squared length of a vec4
*
* @param {ReadonlyVec4} a vector to calculate squared length of
* @returns {Number} squared length of a
*/
function squaredLength$3(a) {
var x = a[0];
var y = a[1];
var z = a[2];
var w = a[3];
return x * x + y * y + z * z + w * w;
}
/**
* Negates the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to negate
* @returns {vec4} out
*/
function negate$1(out, a) {
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
out[3] = -a[3];
return out;
}
/**
* Returns the inverse of the components of a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to invert
* @returns {vec4} out
*/
function inverse$1(out, a) {
out[0] = 1.0 / a[0];
out[1] = 1.0 / a[1];
out[2] = 1.0 / a[2];
out[3] = 1.0 / a[3];
return out;
}
/**
* Normalize a vec4
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a vector to normalize
* @returns {vec4} out
*/
function normalize$3(out, a) {
var x = a[0];
var y = a[1];
var z = a[2];
var w = a[3];
var len = x * x + y * y + z * z + w * w;
if (len > 0) {
len = 1 / Math.sqrt(len);
}
out[0] = x * len;
out[1] = y * len;
out[2] = z * len;
out[3] = w * len;
return out;
}
/**
* Calculates the dot product of two vec4's
*
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @returns {Number} dot product of a and b
*/
function dot$4(a, b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
}
/**
* Returns the cross-product of three vectors in a 4-dimensional space
*
* @param {ReadonlyVec4} out the receiving vector
* @param {ReadonlyVec4} u the first vector
* @param {ReadonlyVec4} v the second vector
* @param {ReadonlyVec4} w the third vector
* @returns {vec4} result
*/
function cross$1(out, u, v, w) {
var A = v[0] * w[1] - v[1] * w[0],
B = v[0] * w[2] - v[2] * w[0],
C = v[0] * w[3] - v[3] * w[0],
D = v[1] * w[2] - v[2] * w[1],
E = v[1] * w[3] - v[3] * w[1],
F = v[2] * w[3] - v[3] * w[2];
var G = u[0];
var H = u[1];
var I = u[2];
var J = u[3];
out[0] = H * F - I * E + J * D;
out[1] = -(G * F) + I * C - J * B;
out[2] = G * E - H * C + J * A;
out[3] = -(G * D) + H * B - I * A;
return out;
}
/**
* Performs a linear interpolation between two vec4's
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the first operand
* @param {ReadonlyVec4} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec4} out
*/
function lerp$4(out, a, b, t) {
var ax = a[0];
var ay = a[1];
var az = a[2];
var aw = a[3];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
out[2] = az + t * (b[2] - az);
out[3] = aw + t * (b[3] - aw);
return out;
}
/**
* Generates a random vector with the given scale
*
* @param {vec4} out the receiving vector
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned
* @returns {vec4} out
*/
function random$2(out, scale) {
scale = scale === undefined ? 1.0 : scale;
// Marsaglia, George. Choosing a Point from the Surface of a
// Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646.
// http://projecteuclid.org/euclid.aoms/1177692644;
var v1, v2, v3, v4;
var s1, s2;
var rand;
rand = RANDOM();
v1 = rand * 2 - 1;
v2 = (4 * RANDOM() - 2) * Math.sqrt(rand * -rand + rand);
s1 = v1 * v1 + v2 * v2;
rand = RANDOM();
v3 = rand * 2 - 1;
v4 = (4 * RANDOM() - 2) * Math.sqrt(rand * -rand + rand);
s2 = v3 * v3 + v4 * v4;
var d = Math.sqrt((1 - s1) / s2);
out[0] = scale * v1;
out[1] = scale * v2;
out[2] = scale * v3 * d;
out[3] = scale * v4 * d;
return out;
}
/**
* Transforms the vec4 with a mat4.
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the vector to transform
* @param {ReadonlyMat4} m matrix to transform with
* @returns {vec4} out
*/
function transformMat4$1(out, a, m) {
var x = a[0],
y = a[1],
z = a[2],
w = a[3];
out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return out;
}
/**
* Transforms the vec4 with a quat
*
* @param {vec4} out the receiving vector
* @param {ReadonlyVec4} a the vector to transform
* @param {ReadonlyQuat} q normalized quaternion to transform with
* @returns {vec4} out
*/
function transformQuat(out, a, q) {
// Fast Vector Rotation using Quaternions by Robert Eisele
// https://raw.org/proof/vector-rotation-using-quaternions/
var qx = q[0],
qy = q[1],
qz = q[2],
qw = q[3];
var vx = a[0],
vy = a[1],
vz = a[2];
// t = q x v
var tx = qy * vz - qz * vy;
var ty = qz * vx - qx * vz;
var tz = qx * vy - qy * vx;
// t = 2t
tx = tx + tx;
ty = ty + ty;
tz = tz + tz;
// v + w t + q x t
out[0] = vx + qw * tx + qy * tz - qz * ty;
out[1] = vy + qw * ty + qz * tx - qx * tz;
out[2] = vz + qw * tz + qx * ty - qy * tx;
out[3] = a[3];
return out;
}
/**
* Set the components of a vec4 to zero
*
* @param {vec4} out the receiving vector
* @returns {vec4} out
*/
function zero$1(out) {
out[0] = 0.0;
out[1] = 0.0;
out[2] = 0.0;
out[3] = 0.0;
return out;
}
/**
* Returns a string representation of a vector
*
* @param {ReadonlyVec4} a vector to represent as a string
* @returns {String} string representation of the vector
*/
function str$3(a) {
return "vec4(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
}
/**
* Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyVec4} a The first vector.
* @param {ReadonlyVec4} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function exactEquals$3(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
}
/**
* Returns whether or not the vectors have approximately the same elements in the same position.
*
* @param {ReadonlyVec4} a The first vector.
* @param {ReadonlyVec4} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function equals$5(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
}
/**
* Alias for {@link vec4.subtract}
* @function
*/
var sub$1 = subtract$1;
/**
* Alias for {@link vec4.multiply}
* @function
*/
var mul$3 = multiply$3;
/**
* Alias for {@link vec4.divide}
* @function
*/
var div$1 = divide$1;
/**
* Alias for {@link vec4.distance}
* @function
*/
var dist$1 = distance$1;
/**
* Alias for {@link vec4.squaredDistance}
* @function
*/
var sqrDist$1 = squaredDistance$1;
/**
* Alias for {@link vec4.length}
* @function
*/
var len$3 = length$3;
/**
* Alias for {@link vec4.squaredLength}
* @function
*/
var sqrLen$3 = squaredLength$3;
/**
* Perform some operation over an array of vec4s.
*
* @param {Array} a the array of vectors to iterate over
* @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
* @param {Number} offset Number of elements to skip at the beginning of the array
* @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
* @param {Function} fn Function to call for each vector in the array
* @param {Object} [arg] additional argument to pass to fn
* @returns {Array} a
* @function
*/
var forEach$1 = function () {
var vec = create$3();
return function (a, stride, offset, count, fn, arg) {
var i, l;
if (!stride) {
stride = 4;
}
if (!offset) {
offset = 0;
}
if (count) {
l = Math.min(count * stride + offset, a.length);
} else {
l = a.length;
}
for (i = offset; i < l; i += stride) {
vec[0] = a[i];
vec[1] = a[i + 1];
vec[2] = a[i + 2];
vec[3] = a[i + 3];
fn(vec, vec, arg);
a[i] = vec[0];
a[i + 1] = vec[1];
a[i + 2] = vec[2];
a[i + 3] = vec[3];
}
return a;
};
}();
var vec4 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$3,
ceil: ceil$1,
clone: clone$4,
copy: copy$3,
create: create$3,
cross: cross$1,
dist: dist$1,
distance: distance$1,
div: div$1,
divide: divide$1,
dot: dot$4,
equals: equals$5,
exactEquals: exactEquals$3,
floor: floor$2,
forEach: forEach$1,
fromValues: fromValues$3,
inverse: inverse$1,
len: len$3,
length: length$3,
lerp: lerp$4,
max: max$3,
min: min$2,
mul: mul$3,
multiply: multiply$3,
negate: negate$1,
normalize: normalize$3,
random: random$2,
round: round$2,
scale: scale$3,
scaleAndAdd: scaleAndAdd$1,
set: set$3,
sqrDist: sqrDist$1,
sqrLen: sqrLen$3,
squaredDistance: squaredDistance$1,
squaredLength: squaredLength$3,
str: str$3,
sub: sub$1,
subtract: subtract$1,
transformMat4: transformMat4$1,
transformQuat: transformQuat,
zero: zero$1
});
/**
* Quaternion in the format XYZW
* @module quat
*/
/**
* Creates a new identity quat
*
* @returns {quat} a new quaternion
*/
function create$2() {
var out = new ARRAY_TYPE(4);
if (ARRAY_TYPE != Float32Array) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
}
out[3] = 1;
return out;
}
/**
* Set a quat to the identity quaternion
*
* @param {quat} out the receiving quaternion
* @returns {quat} out
*/
function identity$2(out) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
return out;
}
/**
* Sets a quat from the given angle and rotation axis,
* then returns it.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyVec3} axis the axis around which to rotate
* @param {Number} rad the angle in radians
* @returns {quat} out
**/
function setAxisAngle(out, axis, rad) {
rad = rad * 0.5;
var s = Math.sin(rad);
out[0] = s * axis[0];
out[1] = s * axis[1];
out[2] = s * axis[2];
out[3] = Math.cos(rad);
return out;
}
/**
* Gets the rotation axis and angle for a given
* quaternion. If a quaternion is created with
* setAxisAngle, this method will return the same
* values as providied in the original parameter list
* OR functionally equivalent values.
* Example: The quaternion formed by axis [0, 0, 1] and
* angle -90 is the same as the quaternion formed by
* [0, 0, 1] and 270. This method favors the latter.
* @param {vec3} out_axis Vector receiving the axis of rotation
* @param {ReadonlyQuat} q Quaternion to be decomposed
* @return {Number} Angle, in radians, of the rotation
*/
function getAxisAngle(out_axis, q) {
var rad = Math.acos(q[3]) * 2.0;
var s = Math.sin(rad / 2.0);
if (s > EPSILON) {
out_axis[0] = q[0] / s;
out_axis[1] = q[1] / s;
out_axis[2] = q[2] / s;
} else {
// If s is zero, return any axis (no rotation - axis does not matter)
out_axis[0] = 1;
out_axis[1] = 0;
out_axis[2] = 0;
}
return rad;
}
/**
* Gets the angular distance between two unit quaternions
*
* @param {ReadonlyQuat} a Origin unit quaternion
* @param {ReadonlyQuat} b Destination unit quaternion
* @return {Number} Angle, in radians, between the two quaternions
*/
function getAngle(a, b) {
var dotproduct = dot$3(a, b);
return Math.acos(2 * dotproduct * dotproduct - 1);
}
/**
* Multiplies two quat's
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @returns {quat} out
*/
function multiply$2(out, a, b) {
var ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
var bx = b[0],
by = b[1],
bz = b[2],
bw = b[3];
out[0] = ax * bw + aw * bx + ay * bz - az * by;
out[1] = ay * bw + aw * by + az * bx - ax * bz;
out[2] = az * bw + aw * bz + ax * by - ay * bx;
out[3] = aw * bw - ax * bx - ay * by - az * bz;
return out;
}
/**
* Rotates a quaternion by the given angle about the X axis
*
* @param {quat} out quat receiving operation result
* @param {ReadonlyQuat} a quat to rotate
* @param {number} rad angle (in radians) to rotate
* @returns {quat} out
*/
function rotateX$1(out, a, rad) {
rad *= 0.5;
var ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
var bx = Math.sin(rad),
bw = Math.cos(rad);
out[0] = ax * bw + aw * bx;
out[1] = ay * bw + az * bx;
out[2] = az * bw - ay * bx;
out[3] = aw * bw - ax * bx;
return out;
}
/**
* Rotates a quaternion by the given angle about the Y axis
*
* @param {quat} out quat receiving operation result
* @param {ReadonlyQuat} a quat to rotate
* @param {number} rad angle (in radians) to rotate
* @returns {quat} out
*/
function rotateY$1(out, a, rad) {
rad *= 0.5;
var ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
var by = Math.sin(rad),
bw = Math.cos(rad);
out[0] = ax * bw - az * by;
out[1] = ay * bw + aw * by;
out[2] = az * bw + ax * by;
out[3] = aw * bw - ay * by;
return out;
}
/**
* Rotates a quaternion by the given angle about the Z axis
*
* @param {quat} out quat receiving operation result
* @param {ReadonlyQuat} a quat to rotate
* @param {number} rad angle (in radians) to rotate
* @returns {quat} out
*/
function rotateZ$1(out, a, rad) {
rad *= 0.5;
var ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
var bz = Math.sin(rad),
bw = Math.cos(rad);
out[0] = ax * bw + ay * bz;
out[1] = ay * bw - ax * bz;
out[2] = az * bw + aw * bz;
out[3] = aw * bw - az * bz;
return out;
}
/**
* Calculates the W component of a quat from the X, Y, and Z components.
* Assumes that quaternion is 1 unit in length.
* Any existing W component will be ignored.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate W component of
* @returns {quat} out
*/
function calculateW(out, a) {
var x = a[0],
y = a[1],
z = a[2];
out[0] = x;
out[1] = y;
out[2] = z;
out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
return out;
}
/**
* Calculate the exponential of a unit quaternion.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate the exponential of
* @returns {quat} out
*/
function exp(out, a) {
var x = a[0],
y = a[1],
z = a[2],
w = a[3];
var r = Math.sqrt(x * x + y * y + z * z);
var et = Math.exp(w);
var s = r > 0 ? et * Math.sin(r) / r : 0;
out[0] = x * s;
out[1] = y * s;
out[2] = z * s;
out[3] = et * Math.cos(r);
return out;
}
/**
* Calculate the natural logarithm of a unit quaternion.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate the exponential of
* @returns {quat} out
*/
function ln(out, a) {
var x = a[0],
y = a[1],
z = a[2],
w = a[3];
var r = Math.sqrt(x * x + y * y + z * z);
var t = r > 0 ? Math.atan2(r, w) / r : 0;
out[0] = x * t;
out[1] = y * t;
out[2] = z * t;
out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
return out;
}
/**
* Calculate the scalar power of a unit quaternion.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate the exponential of
* @param {Number} b amount to scale the quaternion by
* @returns {quat} out
*/
function pow$1(out, a, b) {
ln(out, a);
scale$2(out, out, b);
exp(out, out);
return out;
}
/**
* Performs a spherical linear interpolation between two quat
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {quat} out
*/
function slerp$1(out, a, b, t) {
// benchmarks:
// http://jsperf.com/quaternion-slerp-implementations
var ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
var bx = b[0],
by = b[1],
bz = b[2],
bw = b[3];
var omega, cosom, sinom, scale0, scale1;
// calc cosine
cosom = ax * bx + ay * by + az * bz + aw * bw;
// adjust signs (if necessary)
if (cosom < 0.0) {
cosom = -cosom;
bx = -bx;
by = -by;
bz = -bz;
bw = -bw;
}
// calculate coefficients
if (1.0 - cosom > EPSILON) {
// standard case (slerp)
omega = Math.acos(cosom);
sinom = Math.sin(omega);
scale0 = Math.sin((1.0 - t) * omega) / sinom;
scale1 = Math.sin(t * omega) / sinom;
} else {
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
scale0 = 1.0 - t;
scale1 = t;
}
// calculate final values
out[0] = scale0 * ax + scale1 * bx;
out[1] = scale0 * ay + scale1 * by;
out[2] = scale0 * az + scale1 * bz;
out[3] = scale0 * aw + scale1 * bw;
return out;
}
/**
* Generates a random unit quaternion
*
* @param {quat} out the receiving quaternion
* @returns {quat} out
*/
function random$1(out) {
// Implementation of http://planning.cs.uiuc.edu/node198.html
// TODO: Calling random 3 times is probably not the fastest solution
var u1 = RANDOM();
var u2 = RANDOM();
var u3 = RANDOM();
var sqrt1MinusU1 = Math.sqrt(1 - u1);
var sqrtU1 = Math.sqrt(u1);
out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
return out;
}
/**
* Calculates the inverse of a quat
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate inverse of
* @returns {quat} out
*/
function invert$1(out, a) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3];
var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
var invDot = dot ? 1.0 / dot : 0;
// TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
out[0] = -a0 * invDot;
out[1] = -a1 * invDot;
out[2] = -a2 * invDot;
out[3] = a3 * invDot;
return out;
}
/**
* Calculates the conjugate of a quat
* If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quat to calculate conjugate of
* @returns {quat} out
*/
function conjugate$1(out, a) {
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
out[3] = a[3];
return out;
}
/**
* Creates a quaternion from the given 3x3 rotation matrix.
*
* NOTE: The resultant quaternion is not normalized, so you should be sure
* to renormalize the quaternion yourself where necessary.
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyMat3} m rotation matrix
* @returns {quat} out
* @function
*/
function fromMat3(out, m) {
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
var fTrace = m[0] + m[4] + m[8];
var fRoot;
if (fTrace > 0.0) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
out[3] = 0.5 * fRoot;
fRoot = 0.5 / fRoot; // 1/(4w)
out[0] = (m[5] - m[7]) * fRoot;
out[1] = (m[6] - m[2]) * fRoot;
out[2] = (m[1] - m[3]) * fRoot;
} else {
// |w| <= 1/2
var i = 0;
if (m[4] > m[0]) i = 1;
if (m[8] > m[i * 3 + i]) i = 2;
var j = (i + 1) % 3;
var k = (i + 2) % 3;
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
out[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
}
return out;
}
/**
* Creates a quaternion from the given euler angle x, y, z using the provided intrinsic order for the conversion.
*
* @param {quat} out the receiving quaternion
* @param {Number} x Angle to rotate around X axis in degrees.
* @param {Number} y Angle to rotate around Y axis in degrees.
* @param {Number} z Angle to rotate around Z axis in degrees.
* @param {'xyz'|'xzy'|'yxz'|'yzx'|'zxy'|'zyx'} order Intrinsic order for conversion, default is zyx.
* @returns {quat} out
* @function
*/
function fromEuler(out, x, y, z) {
var order = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : ANGLE_ORDER;
var halfToRad = Math.PI / 360;
x *= halfToRad;
z *= halfToRad;
y *= halfToRad;
var sx = Math.sin(x);
var cx = Math.cos(x);
var sy = Math.sin(y);
var cy = Math.cos(y);
var sz = Math.sin(z);
var cz = Math.cos(z);
switch (order) {
case "xyz":
out[0] = sx * cy * cz + cx * sy * sz;
out[1] = cx * sy * cz - sx * cy * sz;
out[2] = cx * cy * sz + sx * sy * cz;
out[3] = cx * cy * cz - sx * sy * sz;
break;
case "xzy":
out[0] = sx * cy * cz - cx * sy * sz;
out[1] = cx * sy * cz - sx * cy * sz;
out[2] = cx * cy * sz + sx * sy * cz;
out[3] = cx * cy * cz + sx * sy * sz;
break;
case "yxz":
out[0] = sx * cy * cz + cx * sy * sz;
out[1] = cx * sy * cz - sx * cy * sz;
out[2] = cx * cy * sz - sx * sy * cz;
out[3] = cx * cy * cz + sx * sy * sz;
break;
case "yzx":
out[0] = sx * cy * cz + cx * sy * sz;
out[1] = cx * sy * cz + sx * cy * sz;
out[2] = cx * cy * sz - sx * sy * cz;
out[3] = cx * cy * cz - sx * sy * sz;
break;
case "zxy":
out[0] = sx * cy * cz - cx * sy * sz;
out[1] = cx * sy * cz + sx * cy * sz;
out[2] = cx * cy * sz + sx * sy * cz;
out[3] = cx * cy * cz - sx * sy * sz;
break;
case "zyx":
out[0] = sx * cy * cz - cx * sy * sz;
out[1] = cx * sy * cz + sx * cy * sz;
out[2] = cx * cy * sz - sx * sy * cz;
out[3] = cx * cy * cz + sx * sy * sz;
break;
default:
throw new Error('Unknown angle order ' + order);
}
return out;
}
/**
* Returns a string representation of a quaternion
*
* @param {ReadonlyQuat} a vector to represent as a string
* @returns {String} string representation of the vector
*/
function str$2(a) {
return "quat(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
}
/**
* Creates a new quat initialized with values from an existing quaternion
*
* @param {ReadonlyQuat} a quaternion to clone
* @returns {quat} a new quaternion
* @function
*/
var clone$3 = clone$4;
/**
* Creates a new quat initialized with the given values
*
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @param {Number} w W component
* @returns {quat} a new quaternion
* @function
*/
var fromValues$2 = fromValues$3;
/**
* Copy the values from one quat to another
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the source quaternion
* @returns {quat} out
* @function
*/
var copy$2 = copy$3;
/**
* Set the components of a quat to the given values
*
* @param {quat} out the receiving quaternion
* @param {Number} x X component
* @param {Number} y Y component
* @param {Number} z Z component
* @param {Number} w W component
* @returns {quat} out
* @function
*/
var set$2 = set$3;
/**
* Adds two quat's
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @returns {quat} out
* @function
*/
var add$2 = add$3;
/**
* Alias for {@link quat.multiply}
* @function
*/
var mul$2 = multiply$2;
/**
* Scales a quat by a scalar number
*
* @param {quat} out the receiving vector
* @param {ReadonlyQuat} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {quat} out
* @function
*/
var scale$2 = scale$3;
/**
* Calculates the dot product of two quat's
*
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @returns {Number} dot product of a and b
* @function
*/
var dot$3 = dot$4;
/**
* Performs a linear interpolation between two quat's
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {quat} out
* @function
*/
var lerp$3 = lerp$4;
/**
* Calculates the length of a quat
*
* @param {ReadonlyQuat} a vector to calculate length of
* @returns {Number} length of a
*/
var length$2 = length$3;
/**
* Alias for {@link quat.length}
* @function
*/
var len$2 = length$2;
/**
* Calculates the squared length of a quat
*
* @param {ReadonlyQuat} a vector to calculate squared length of
* @returns {Number} squared length of a
* @function
*/
var squaredLength$2 = squaredLength$3;
/**
* Alias for {@link quat.squaredLength}
* @function
*/
var sqrLen$2 = squaredLength$2;
/**
* Normalize a quat
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a quaternion to normalize
* @returns {quat} out
* @function
*/
var normalize$2 = normalize$3;
/**
* Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyQuat} a The first quaternion.
* @param {ReadonlyQuat} b The second quaternion.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
var exactEquals$2 = exactEquals$3;
/**
* Returns whether or not the quaternions point approximately to the same direction.
*
* Both quaternions are assumed to be unit length.
*
* @param {ReadonlyQuat} a The first unit quaternion.
* @param {ReadonlyQuat} b The second unit quaternion.
* @returns {Boolean} True if the quaternions are equal, false otherwise.
*/
function equals$4(a, b) {
return Math.abs(dot$4(a, b)) >= 1 - EPSILON;
}
/**
* Sets a quaternion to represent the shortest rotation from one
* vector to another.
*
* Both vectors are assumed to be unit length.
*
* @param {quat} out the receiving quaternion.
* @param {ReadonlyVec3} a the initial vector
* @param {ReadonlyVec3} b the destination vector
* @returns {quat} out
*/
var rotationTo = function () {
var tmpvec3 = create$4();
var xUnitVec3 = fromValues$4(1, 0, 0);
var yUnitVec3 = fromValues$4(0, 1, 0);
return function (out, a, b) {
var dot = dot$5(a, b);
if (dot < -0.999999) {
cross$2(tmpvec3, xUnitVec3, a);
if (len$4(tmpvec3) < 0.000001) cross$2(tmpvec3, yUnitVec3, a);
normalize$4(tmpvec3, tmpvec3);
setAxisAngle(out, tmpvec3, Math.PI);
return out;
} else if (dot > 0.999999) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
return out;
} else {
cross$2(tmpvec3, a, b);
out[0] = tmpvec3[0];
out[1] = tmpvec3[1];
out[2] = tmpvec3[2];
out[3] = 1 + dot;
return normalize$2(out, out);
}
};
}();
/**
* Performs a spherical linear interpolation with two control points
*
* @param {quat} out the receiving quaternion
* @param {ReadonlyQuat} a the first operand
* @param {ReadonlyQuat} b the second operand
* @param {ReadonlyQuat} c the third operand
* @param {ReadonlyQuat} d the fourth operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {quat} out
*/
var sqlerp = function () {
var temp1 = create$2();
var temp2 = create$2();
return function (out, a, b, c, d, t) {
slerp$1(temp1, a, d, t);
slerp$1(temp2, b, c, t);
slerp$1(out, temp1, temp2, 2 * t * (1 - t));
return out;
};
}();
/**
* Sets the specified quaternion with values corresponding to the given
* axes. Each axis is a vec3 and is expected to be unit length and
* perpendicular to all other specified axes.
*
* @param {ReadonlyVec3} view the vector representing the viewing direction
* @param {ReadonlyVec3} right the vector representing the local "right" direction
* @param {ReadonlyVec3} up the vector representing the local "up" direction
* @returns {quat} out
*/
var setAxes = function () {
var matr = create$6();
return function (out, view, right, up) {
matr[0] = right[0];
matr[3] = right[1];
matr[6] = right[2];
matr[1] = up[0];
matr[4] = up[1];
matr[7] = up[2];
matr[2] = -view[0];
matr[5] = -view[1];
matr[8] = -view[2];
return normalize$2(out, fromMat3(out, matr));
};
}();
var quat = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$2,
calculateW: calculateW,
clone: clone$3,
conjugate: conjugate$1,
copy: copy$2,
create: create$2,
dot: dot$3,
equals: equals$4,
exactEquals: exactEquals$2,
exp: exp,
fromEuler: fromEuler,
fromMat3: fromMat3,
fromValues: fromValues$2,
getAngle: getAngle,
getAxisAngle: getAxisAngle,
identity: identity$2,
invert: invert$1,
len: len$2,
length: length$2,
lerp: lerp$3,
ln: ln,
mul: mul$2,
multiply: multiply$2,
normalize: normalize$2,
pow: pow$1,
random: random$1,
rotateX: rotateX$1,
rotateY: rotateY$1,
rotateZ: rotateZ$1,
rotationTo: rotationTo,
scale: scale$2,
set: set$2,
setAxes: setAxes,
setAxisAngle: setAxisAngle,
slerp: slerp$1,
sqlerp: sqlerp,
sqrLen: sqrLen$2,
squaredLength: squaredLength$2,
str: str$2
});
/**
* Dual Quaternion
* Format: [real, dual]
* Quaternion format: XYZW
* Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.
* @module quat2
*/
/**
* Creates a new identity dual quat
*
* @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation]
*/
function create$1() {
var dq = new ARRAY_TYPE(8);
if (ARRAY_TYPE != Float32Array) {
dq[0] = 0;
dq[1] = 0;
dq[2] = 0;
dq[4] = 0;
dq[5] = 0;
dq[6] = 0;
dq[7] = 0;
}
dq[3] = 1;
return dq;
}
/**
* Creates a new quat initialized with values from an existing quaternion
*
* @param {ReadonlyQuat2} a dual quaternion to clone
* @returns {quat2} new dual quaternion
* @function
*/
function clone$2(a) {
var dq = new ARRAY_TYPE(8);
dq[0] = a[0];
dq[1] = a[1];
dq[2] = a[2];
dq[3] = a[3];
dq[4] = a[4];
dq[5] = a[5];
dq[6] = a[6];
dq[7] = a[7];
return dq;
}
/**
* Creates a new dual quat initialized with the given values
*
* @param {Number} x1 X component
* @param {Number} y1 Y component
* @param {Number} z1 Z component
* @param {Number} w1 W component
* @param {Number} x2 X component
* @param {Number} y2 Y component
* @param {Number} z2 Z component
* @param {Number} w2 W component
* @returns {quat2} new dual quaternion
* @function
*/
function fromValues$1(x1, y1, z1, w1, x2, y2, z2, w2) {
var dq = new ARRAY_TYPE(8);
dq[0] = x1;
dq[1] = y1;
dq[2] = z1;
dq[3] = w1;
dq[4] = x2;
dq[5] = y2;
dq[6] = z2;
dq[7] = w2;
return dq;
}
/**
* Creates a new dual quat from the given values (quat and translation)
*
* @param {Number} x1 X component
* @param {Number} y1 Y component
* @param {Number} z1 Z component
* @param {Number} w1 W component
* @param {Number} x2 X component (translation)
* @param {Number} y2 Y component (translation)
* @param {Number} z2 Z component (translation)
* @returns {quat2} new dual quaternion
* @function
*/
function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) {
var dq = new ARRAY_TYPE(8);
dq[0] = x1;
dq[1] = y1;
dq[2] = z1;
dq[3] = w1;
var ax = x2 * 0.5,
ay = y2 * 0.5,
az = z2 * 0.5;
dq[4] = ax * w1 + ay * z1 - az * y1;
dq[5] = ay * w1 + az * x1 - ax * z1;
dq[6] = az * w1 + ax * y1 - ay * x1;
dq[7] = -ax * x1 - ay * y1 - az * z1;
return dq;
}
/**
* Creates a dual quat from a quaternion and a translation
*
* @param {ReadonlyQuat2} dual quaternion receiving operation result
* @param {ReadonlyQuat} q a normalized quaternion
* @param {ReadonlyVec3} t translation vector
* @returns {quat2} dual quaternion receiving operation result
* @function
*/
function fromRotationTranslation(out, q, t) {
var ax = t[0] * 0.5,
ay = t[1] * 0.5,
az = t[2] * 0.5,
bx = q[0],
by = q[1],
bz = q[2],
bw = q[3];
out[0] = bx;
out[1] = by;
out[2] = bz;
out[3] = bw;
out[4] = ax * bw + ay * bz - az * by;
out[5] = ay * bw + az * bx - ax * bz;
out[6] = az * bw + ax * by - ay * bx;
out[7] = -ax * bx - ay * by - az * bz;
return out;
}
/**
* Creates a dual quat from a translation
*
* @param {ReadonlyQuat2} dual quaternion receiving operation result
* @param {ReadonlyVec3} t translation vector
* @returns {quat2} dual quaternion receiving operation result
* @function
*/
function fromTranslation(out, t) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
out[4] = t[0] * 0.5;
out[5] = t[1] * 0.5;
out[6] = t[2] * 0.5;
out[7] = 0;
return out;
}
/**
* Creates a dual quat from a quaternion
*
* @param {ReadonlyQuat2} dual quaternion receiving operation result
* @param {ReadonlyQuat} q the quaternion
* @returns {quat2} dual quaternion receiving operation result
* @function
*/
function fromRotation(out, q) {
out[0] = q[0];
out[1] = q[1];
out[2] = q[2];
out[3] = q[3];
out[4] = 0;
out[5] = 0;
out[6] = 0;
out[7] = 0;
return out;
}
/**
* Creates a new dual quat from a matrix (4x4)
*
* @param {quat2} out the dual quaternion
* @param {ReadonlyMat4} a the matrix
* @returns {quat2} dual quat receiving operation result
* @function
*/
function fromMat4(out, a) {
//TODO Optimize this
var outer = create$2();
getRotation(outer, a);
var t = new ARRAY_TYPE(3);
getTranslation$1(t, a);
fromRotationTranslation(out, outer, t);
return out;
}
/**
* Copy the values from one dual quat to another
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the source dual quaternion
* @returns {quat2} out
* @function
*/
function copy$1(out, a) {
out[0] = a[0];
out[1] = a[1];
out[2] = a[2];
out[3] = a[3];
out[4] = a[4];
out[5] = a[5];
out[6] = a[6];
out[7] = a[7];
return out;
}
/**
* Set a dual quat to the identity dual quaternion
*
* @param {quat2} out the receiving quaternion
* @returns {quat2} out
*/
function identity$1(out) {
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 1;
out[4] = 0;
out[5] = 0;
out[6] = 0;
out[7] = 0;
return out;
}
/**
* Set the components of a dual quat to the given values
*
* @param {quat2} out the receiving quaternion
* @param {Number} x1 X component
* @param {Number} y1 Y component
* @param {Number} z1 Z component
* @param {Number} w1 W component
* @param {Number} x2 X component
* @param {Number} y2 Y component
* @param {Number} z2 Z component
* @param {Number} w2 W component
* @returns {quat2} out
* @function
*/
function set$1(out, x1, y1, z1, w1, x2, y2, z2, w2) {
out[0] = x1;
out[1] = y1;
out[2] = z1;
out[3] = w1;
out[4] = x2;
out[5] = y2;
out[6] = z2;
out[7] = w2;
return out;
}
/**
* Gets the real part of a dual quat
* @param {quat} out real part
* @param {ReadonlyQuat2} a Dual Quaternion
* @return {quat} real part
*/
var getReal = copy$2;
/**
* Gets the dual part of a dual quat
* @param {quat} out dual part
* @param {ReadonlyQuat2} a Dual Quaternion
* @return {quat} dual part
*/
function getDual(out, a) {
out[0] = a[4];
out[1] = a[5];
out[2] = a[6];
out[3] = a[7];
return out;
}
/**
* Set the real component of a dual quat to the given quaternion
*
* @param {quat2} out the receiving quaternion
* @param {ReadonlyQuat} q a quaternion representing the real part
* @returns {quat2} out
* @function
*/
var setReal = copy$2;
/**
* Set the dual component of a dual quat to the given quaternion
*
* @param {quat2} out the receiving quaternion
* @param {ReadonlyQuat} q a quaternion representing the dual part
* @returns {quat2} out
* @function
*/
function setDual(out, q) {
out[4] = q[0];
out[5] = q[1];
out[6] = q[2];
out[7] = q[3];
return out;
}
/**
* Gets the translation of a normalized dual quat
* @param {vec3} out translation
* @param {ReadonlyQuat2} a Dual Quaternion to be decomposed
* @return {vec3} translation
*/
function getTranslation(out, a) {
var ax = a[4],
ay = a[5],
az = a[6],
aw = a[7],
bx = -a[0],
by = -a[1],
bz = -a[2],
bw = a[3];
out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
return out;
}
/**
* Translates a dual quat by the given vector
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to translate
* @param {ReadonlyVec3} v vector to translate by
* @returns {quat2} out
*/
function translate$1(out, a, v) {
var ax1 = a[0],
ay1 = a[1],
az1 = a[2],
aw1 = a[3],
bx1 = v[0] * 0.5,
by1 = v[1] * 0.5,
bz1 = v[2] * 0.5,
ax2 = a[4],
ay2 = a[5],
az2 = a[6],
aw2 = a[7];
out[0] = ax1;
out[1] = ay1;
out[2] = az1;
out[3] = aw1;
out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
return out;
}
/**
* Rotates a dual quat around the X axis
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @param {number} rad how far should the rotation be
* @returns {quat2} out
*/
function rotateX(out, a, rad) {
var bx = -a[0],
by = -a[1],
bz = -a[2],
bw = a[3],
ax = a[4],
ay = a[5],
az = a[6],
aw = a[7],
ax1 = ax * bw + aw * bx + ay * bz - az * by,
ay1 = ay * bw + aw * by + az * bx - ax * bz,
az1 = az * bw + aw * bz + ax * by - ay * bx,
aw1 = aw * bw - ax * bx - ay * by - az * bz;
rotateX$1(out, a, rad);
bx = out[0];
by = out[1];
bz = out[2];
bw = out[3];
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
return out;
}
/**
* Rotates a dual quat around the Y axis
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @param {number} rad how far should the rotation be
* @returns {quat2} out
*/
function rotateY(out, a, rad) {
var bx = -a[0],
by = -a[1],
bz = -a[2],
bw = a[3],
ax = a[4],
ay = a[5],
az = a[6],
aw = a[7],
ax1 = ax * bw + aw * bx + ay * bz - az * by,
ay1 = ay * bw + aw * by + az * bx - ax * bz,
az1 = az * bw + aw * bz + ax * by - ay * bx,
aw1 = aw * bw - ax * bx - ay * by - az * bz;
rotateY$1(out, a, rad);
bx = out[0];
by = out[1];
bz = out[2];
bw = out[3];
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
return out;
}
/**
* Rotates a dual quat around the Z axis
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @param {number} rad how far should the rotation be
* @returns {quat2} out
*/
function rotateZ(out, a, rad) {
var bx = -a[0],
by = -a[1],
bz = -a[2],
bw = a[3],
ax = a[4],
ay = a[5],
az = a[6],
aw = a[7],
ax1 = ax * bw + aw * bx + ay * bz - az * by,
ay1 = ay * bw + aw * by + az * bx - ax * bz,
az1 = az * bw + aw * bz + ax * by - ay * bx,
aw1 = aw * bw - ax * bx - ay * by - az * bz;
rotateZ$1(out, a, rad);
bx = out[0];
by = out[1];
bz = out[2];
bw = out[3];
out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
return out;
}
/**
* Rotates a dual quat by a given quaternion (a * q)
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @param {ReadonlyQuat} q quaternion to rotate by
* @returns {quat2} out
*/
function rotateByQuatAppend(out, a, q) {
var qx = q[0],
qy = q[1],
qz = q[2],
qw = q[3],
ax = a[0],
ay = a[1],
az = a[2],
aw = a[3];
out[0] = ax * qw + aw * qx + ay * qz - az * qy;
out[1] = ay * qw + aw * qy + az * qx - ax * qz;
out[2] = az * qw + aw * qz + ax * qy - ay * qx;
out[3] = aw * qw - ax * qx - ay * qy - az * qz;
ax = a[4];
ay = a[5];
az = a[6];
aw = a[7];
out[4] = ax * qw + aw * qx + ay * qz - az * qy;
out[5] = ay * qw + aw * qy + az * qx - ax * qz;
out[6] = az * qw + aw * qz + ax * qy - ay * qx;
out[7] = aw * qw - ax * qx - ay * qy - az * qz;
return out;
}
/**
* Rotates a dual quat by a given quaternion (q * a)
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat} q quaternion to rotate by
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @returns {quat2} out
*/
function rotateByQuatPrepend(out, q, a) {
var qx = q[0],
qy = q[1],
qz = q[2],
qw = q[3],
bx = a[0],
by = a[1],
bz = a[2],
bw = a[3];
out[0] = qx * bw + qw * bx + qy * bz - qz * by;
out[1] = qy * bw + qw * by + qz * bx - qx * bz;
out[2] = qz * bw + qw * bz + qx * by - qy * bx;
out[3] = qw * bw - qx * bx - qy * by - qz * bz;
bx = a[4];
by = a[5];
bz = a[6];
bw = a[7];
out[4] = qx * bw + qw * bx + qy * bz - qz * by;
out[5] = qy * bw + qw * by + qz * bx - qx * bz;
out[6] = qz * bw + qw * bz + qx * by - qy * bx;
out[7] = qw * bw - qx * bx - qy * by - qz * bz;
return out;
}
/**
* Rotates a dual quat around a given axis. Does the normalisation automatically
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the dual quaternion to rotate
* @param {ReadonlyVec3} axis the axis to rotate around
* @param {Number} rad how far the rotation should be
* @returns {quat2} out
*/
function rotateAroundAxis(out, a, axis, rad) {
//Special case for rad = 0
if (Math.abs(rad) < EPSILON) {
return copy$1(out, a);
}
var axisLength = Math.sqrt(axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2]);
rad = rad * 0.5;
var s = Math.sin(rad);
var bx = s * axis[0] / axisLength;
var by = s * axis[1] / axisLength;
var bz = s * axis[2] / axisLength;
var bw = Math.cos(rad);
var ax1 = a[0],
ay1 = a[1],
az1 = a[2],
aw1 = a[3];
out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
var ax = a[4],
ay = a[5],
az = a[6],
aw = a[7];
out[4] = ax * bw + aw * bx + ay * bz - az * by;
out[5] = ay * bw + aw * by + az * bx - ax * bz;
out[6] = az * bw + aw * bz + ax * by - ay * bx;
out[7] = aw * bw - ax * bx - ay * by - az * bz;
return out;
}
/**
* Adds two dual quat's
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the first operand
* @param {ReadonlyQuat2} b the second operand
* @returns {quat2} out
* @function
*/
function add$1(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
out[5] = a[5] + b[5];
out[6] = a[6] + b[6];
out[7] = a[7] + b[7];
return out;
}
/**
* Multiplies two dual quat's
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a the first operand
* @param {ReadonlyQuat2} b the second operand
* @returns {quat2} out
*/
function multiply$1(out, a, b) {
var ax0 = a[0],
ay0 = a[1],
az0 = a[2],
aw0 = a[3],
bx1 = b[4],
by1 = b[5],
bz1 = b[6],
bw1 = b[7],
ax1 = a[4],
ay1 = a[5],
az1 = a[6],
aw1 = a[7],
bx0 = b[0],
by0 = b[1],
bz0 = b[2],
bw0 = b[3];
out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0;
out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0;
out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0;
out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0;
return out;
}
/**
* Alias for {@link quat2.multiply}
* @function
*/
var mul$1 = multiply$1;
/**
* Scales a dual quat by a scalar number
*
* @param {quat2} out the receiving dual quat
* @param {ReadonlyQuat2} a the dual quat to scale
* @param {Number} b amount to scale the dual quat by
* @returns {quat2} out
* @function
*/
function scale$1(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
out[2] = a[2] * b;
out[3] = a[3] * b;
out[4] = a[4] * b;
out[5] = a[5] * b;
out[6] = a[6] * b;
out[7] = a[7] * b;
return out;
}
/**
* Calculates the dot product of two dual quat's (The dot product of the real parts)
*
* @param {ReadonlyQuat2} a the first operand
* @param {ReadonlyQuat2} b the second operand
* @returns {Number} dot product of a and b
* @function
*/
var dot$2 = dot$3;
/**
* Performs a linear interpolation between two dual quats's
* NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
*
* @param {quat2} out the receiving dual quat
* @param {ReadonlyQuat2} a the first operand
* @param {ReadonlyQuat2} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {quat2} out
*/
function lerp$2(out, a, b, t) {
var mt = 1 - t;
if (dot$2(a, b) < 0) t = -t;
out[0] = a[0] * mt + b[0] * t;
out[1] = a[1] * mt + b[1] * t;
out[2] = a[2] * mt + b[2] * t;
out[3] = a[3] * mt + b[3] * t;
out[4] = a[4] * mt + b[4] * t;
out[5] = a[5] * mt + b[5] * t;
out[6] = a[6] * mt + b[6] * t;
out[7] = a[7] * mt + b[7] * t;
return out;
}
/**
* Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a dual quat to calculate inverse of
* @returns {quat2} out
*/
function invert(out, a) {
var sqlen = squaredLength$1(a);
out[0] = -a[0] / sqlen;
out[1] = -a[1] / sqlen;
out[2] = -a[2] / sqlen;
out[3] = a[3] / sqlen;
out[4] = -a[4] / sqlen;
out[5] = -a[5] / sqlen;
out[6] = -a[6] / sqlen;
out[7] = a[7] / sqlen;
return out;
}
/**
* Calculates the conjugate of a dual quat
* If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
*
* @param {quat2} out the receiving quaternion
* @param {ReadonlyQuat2} a quat to calculate conjugate of
* @returns {quat2} out
*/
function conjugate(out, a) {
out[0] = -a[0];
out[1] = -a[1];
out[2] = -a[2];
out[3] = a[3];
out[4] = -a[4];
out[5] = -a[5];
out[6] = -a[6];
out[7] = a[7];
return out;
}
/**
* Calculates the length of a dual quat
*
* @param {ReadonlyQuat2} a dual quat to calculate length of
* @returns {Number} length of a
* @function
*/
var length$1 = length$2;
/**
* Alias for {@link quat2.length}
* @function
*/
var len$1 = length$1;
/**
* Calculates the squared length of a dual quat
*
* @param {ReadonlyQuat2} a dual quat to calculate squared length of
* @returns {Number} squared length of a
* @function
*/
var squaredLength$1 = squaredLength$2;
/**
* Alias for {@link quat2.squaredLength}
* @function
*/
var sqrLen$1 = squaredLength$1;
/**
* Normalize a dual quat
*
* @param {quat2} out the receiving dual quaternion
* @param {ReadonlyQuat2} a dual quaternion to normalize
* @returns {quat2} out
* @function
*/
function normalize$1(out, a) {
var magnitude = squaredLength$1(a);
if (magnitude > 0) {
magnitude = Math.sqrt(magnitude);
var a0 = a[0] / magnitude;
var a1 = a[1] / magnitude;
var a2 = a[2] / magnitude;
var a3 = a[3] / magnitude;
var b0 = a[4];
var b1 = a[5];
var b2 = a[6];
var b3 = a[7];
var a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
out[0] = a0;
out[1] = a1;
out[2] = a2;
out[3] = a3;
out[4] = (b0 - a0 * a_dot_b) / magnitude;
out[5] = (b1 - a1 * a_dot_b) / magnitude;
out[6] = (b2 - a2 * a_dot_b) / magnitude;
out[7] = (b3 - a3 * a_dot_b) / magnitude;
}
return out;
}
/**
* Returns a string representation of a dual quaternion
*
* @param {ReadonlyQuat2} a dual quaternion to represent as a string
* @returns {String} string representation of the dual quat
*/
function str$1(a) {
return "quat2(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ")";
}
/**
* Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
*
* @param {ReadonlyQuat2} a the first dual quaternion.
* @param {ReadonlyQuat2} b the second dual quaternion.
* @returns {Boolean} true if the dual quaternions are equal, false otherwise.
*/
function exactEquals$1(a, b) {
return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7];
}
/**
* Returns whether or not the dual quaternions have approximately the same elements in the same position.
*
* @param {ReadonlyQuat2} a the first dual quat.
* @param {ReadonlyQuat2} b the second dual quat.
* @returns {Boolean} true if the dual quats are equal, false otherwise.
*/
function equals$3(a, b) {
var a0 = a[0],
a1 = a[1],
a2 = a[2],
a3 = a[3],
a4 = a[4],
a5 = a[5],
a6 = a[6],
a7 = a[7];
var b0 = b[0],
b1 = b[1],
b2 = b[2],
b3 = b[3],
b4 = b[4],
b5 = b[5],
b6 = b[6],
b7 = b[7];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7));
}
var quat2 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add$1,
clone: clone$2,
conjugate: conjugate,
copy: copy$1,
create: create$1,
dot: dot$2,
equals: equals$3,
exactEquals: exactEquals$1,
fromMat4: fromMat4,
fromRotation: fromRotation,
fromRotationTranslation: fromRotationTranslation,
fromRotationTranslationValues: fromRotationTranslationValues,
fromTranslation: fromTranslation,
fromValues: fromValues$1,
getDual: getDual,
getReal: getReal,
getTranslation: getTranslation,
identity: identity$1,
invert: invert,
len: len$1,
length: length$1,
lerp: lerp$2,
mul: mul$1,
multiply: multiply$1,
normalize: normalize$1,
rotateAroundAxis: rotateAroundAxis,
rotateByQuatAppend: rotateByQuatAppend,
rotateByQuatPrepend: rotateByQuatPrepend,
rotateX: rotateX,
rotateY: rotateY,
rotateZ: rotateZ,
scale: scale$1,
set: set$1,
setDual: setDual,
setReal: setReal,
sqrLen: sqrLen$1,
squaredLength: squaredLength$1,
str: str$1,
translate: translate$1
});
/**
* 2 Dimensional Vector
* @module vec2
*/
/**
* Creates a new, empty vec2
*
* @returns {vec2} a new 2D vector
*/
function create() {
var out = new ARRAY_TYPE(2);
if (ARRAY_TYPE != Float32Array) {
out[0] = 0;
out[1] = 0;
}
return out;
}
/**
* Creates a new vec2 initialized with values from an existing vector
*
* @param {ReadonlyVec2} a vector to clone
* @returns {vec2} a new 2D vector
*/
function clone$1(a) {
var out = new ARRAY_TYPE(2);
out[0] = a[0];
out[1] = a[1];
return out;
}
/**
* Creates a new vec2 initialized with the given values
*
* @param {Number} x X component
* @param {Number} y Y component
* @returns {vec2} a new 2D vector
*/
function fromValues(x, y) {
var out = new ARRAY_TYPE(2);
out[0] = x;
out[1] = y;
return out;
}
/**
* Copy the values from one vec2 to another
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the source vector
* @returns {vec2} out
*/
function copy(out, a) {
out[0] = a[0];
out[1] = a[1];
return out;
}
/**
* Set the components of a vec2 to the given values
*
* @param {vec2} out the receiving vector
* @param {Number} x X component
* @param {Number} y Y component
* @returns {vec2} out
*/
function set(out, x, y) {
out[0] = x;
out[1] = y;
return out;
}
/**
* Adds two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function add(out, a, b) {
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
return out;
}
/**
* Subtracts vector b from vector a
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function subtract(out, a, b) {
out[0] = a[0] - b[0];
out[1] = a[1] - b[1];
return out;
}
/**
* Multiplies two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function multiply(out, a, b) {
out[0] = a[0] * b[0];
out[1] = a[1] * b[1];
return out;
}
/**
* Divides two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function divide(out, a, b) {
out[0] = a[0] / b[0];
out[1] = a[1] / b[1];
return out;
}
/**
* Math.ceil the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to ceil
* @returns {vec2} out
*/
function ceil(out, a) {
out[0] = Math.ceil(a[0]);
out[1] = Math.ceil(a[1]);
return out;
}
/**
* Math.floor the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to floor
* @returns {vec2} out
*/
function floor$1(out, a) {
out[0] = Math.floor(a[0]);
out[1] = Math.floor(a[1]);
return out;
}
/**
* Returns the minimum of two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function min$1(out, a, b) {
out[0] = Math.min(a[0], b[0]);
out[1] = Math.min(a[1], b[1]);
return out;
}
/**
* Returns the maximum of two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec2} out
*/
function max$2(out, a, b) {
out[0] = Math.max(a[0], b[0]);
out[1] = Math.max(a[1], b[1]);
return out;
}
/**
* symmetric round the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to round
* @returns {vec2} out
*/
function round$1(out, a) {
out[0] = round$4(a[0]);
out[1] = round$4(a[1]);
return out;
}
/**
* Scales a vec2 by a scalar number
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the vector to scale
* @param {Number} b amount to scale the vector by
* @returns {vec2} out
*/
function scale(out, a, b) {
out[0] = a[0] * b;
out[1] = a[1] * b;
return out;
}
/**
* Adds two vec2's after scaling the second operand by a scalar value
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @param {Number} scale the amount to scale b by before adding
* @returns {vec2} out
*/
function scaleAndAdd(out, a, b, scale) {
out[0] = a[0] + b[0] * scale;
out[1] = a[1] + b[1] * scale;
return out;
}
/**
* Calculates the euclidian distance between two vec2's
*
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {Number} distance between a and b
*/
function distance(a, b) {
var x = b[0] - a[0],
y = b[1] - a[1];
return Math.sqrt(x * x + y * y);
}
/**
* Calculates the squared euclidian distance between two vec2's
*
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {Number} squared distance between a and b
*/
function squaredDistance(a, b) {
var x = b[0] - a[0],
y = b[1] - a[1];
return x * x + y * y;
}
/**
* Calculates the length of a vec2
*
* @param {ReadonlyVec2} a vector to calculate length of
* @returns {Number} length of a
*/
function length(a) {
var x = a[0],
y = a[1];
return Math.sqrt(x * x + y * y);
}
/**
* Calculates the squared length of a vec2
*
* @param {ReadonlyVec2} a vector to calculate squared length of
* @returns {Number} squared length of a
*/
function squaredLength(a) {
var x = a[0],
y = a[1];
return x * x + y * y;
}
/**
* Negates the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to negate
* @returns {vec2} out
*/
function negate(out, a) {
out[0] = -a[0];
out[1] = -a[1];
return out;
}
/**
* Returns the inverse of the components of a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to invert
* @returns {vec2} out
*/
function inverse(out, a) {
out[0] = 1.0 / a[0];
out[1] = 1.0 / a[1];
return out;
}
/**
* Normalize a vec2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a vector to normalize
* @returns {vec2} out
*/
function normalize(out, a) {
var x = a[0],
y = a[1];
var len = x * x + y * y;
if (len > 0) {
//TODO: evaluate use of glm_invsqrt here?
len = 1 / Math.sqrt(len);
}
out[0] = a[0] * len;
out[1] = a[1] * len;
return out;
}
/**
* Calculates the dot product of two vec2's
*
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {Number} dot product of a and b
*/
function dot$1(a, b) {
return a[0] * b[0] + a[1] * b[1];
}
/**
* Computes the cross product of two vec2's
* Note that the cross product must by definition produce a 3D vector
*
* @param {vec3} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @returns {vec3} out
*/
function cross(out, a, b) {
var z = a[0] * b[1] - a[1] * b[0];
out[0] = out[1] = 0;
out[2] = z;
return out;
}
/**
* Performs a linear interpolation between two vec2's
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the first operand
* @param {ReadonlyVec2} b the second operand
* @param {Number} t interpolation amount, in the range [0-1], between the two inputs
* @returns {vec2} out
*/
function lerp$1(out, a, b, t) {
var ax = a[0],
ay = a[1];
out[0] = ax + t * (b[0] - ax);
out[1] = ay + t * (b[1] - ay);
return out;
}
/**
* Generates a random vector with the given scale
*
* @param {vec2} out the receiving vector
* @param {Number} [scale] Length of the resulting vector. If omitted, a unit vector will be returned
* @returns {vec2} out
*/
function random(out, scale) {
scale = scale === undefined ? 1.0 : scale;
var r = RANDOM() * 2.0 * Math.PI;
out[0] = Math.cos(r) * scale;
out[1] = Math.sin(r) * scale;
return out;
}
/**
* Transforms the vec2 with a mat2
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the vector to transform
* @param {ReadonlyMat2} m matrix to transform with
* @returns {vec2} out
*/
function transformMat2(out, a, m) {
var x = a[0],
y = a[1];
out[0] = m[0] * x + m[2] * y;
out[1] = m[1] * x + m[3] * y;
return out;
}
/**
* Transforms the vec2 with a mat2d
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the vector to transform
* @param {ReadonlyMat2d} m matrix to transform with
* @returns {vec2} out
*/
function transformMat2d(out, a, m) {
var x = a[0],
y = a[1];
out[0] = m[0] * x + m[2] * y + m[4];
out[1] = m[1] * x + m[3] * y + m[5];
return out;
}
/**
* Transforms the vec2 with a mat3
* 3rd vector component is implicitly '1'
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the vector to transform
* @param {ReadonlyMat3} m matrix to transform with
* @returns {vec2} out
*/
function transformMat3(out, a, m) {
var x = a[0],
y = a[1];
out[0] = m[0] * x + m[3] * y + m[6];
out[1] = m[1] * x + m[4] * y + m[7];
return out;
}
/**
* Transforms the vec2 with a mat4
* 3rd vector component is implicitly '0'
* 4th vector component is implicitly '1'
*
* @param {vec2} out the receiving vector
* @param {ReadonlyVec2} a the vector to transform
* @param {ReadonlyMat4} m matrix to transform with
* @returns {vec2} out
*/
function transformMat4(out, a, m) {
var x = a[0];
var y = a[1];
out[0] = m[0] * x + m[4] * y + m[12];
out[1] = m[1] * x + m[5] * y + m[13];
return out;
}
/**
* Rotate a 2D vector
* @param {vec2} out The receiving vec2
* @param {ReadonlyVec2} a The vec2 point to rotate
* @param {ReadonlyVec2} b The origin of the rotation
* @param {Number} rad The angle of rotation in radians
* @returns {vec2} out
*/
function rotate$1(out, a, b, rad) {
//Translate point to the origin
var p0 = a[0] - b[0],
p1 = a[1] - b[1],
sinC = Math.sin(rad),
cosC = Math.cos(rad);
//perform rotation and translate to correct position
out[0] = p0 * cosC - p1 * sinC + b[0];
out[1] = p0 * sinC + p1 * cosC + b[1];
return out;
}
/**
* Get the smallest angle between two 2D vectors
* @param {ReadonlyVec2} a The first operand
* @param {ReadonlyVec2} b The second operand
* @returns {Number} The angle in radians
*/
function angle(a, b) {
var ax = a[0],
ay = a[1],
bx = b[0],
by = b[1];
return Math.abs(Math.atan2(ay * bx - ax * by, ax * bx + ay * by));
}
/**
* Get the signed angle in the interval [-pi,pi] between two 2D vectors (positive if `a` is to the right of `b`)
*
* @param {ReadonlyVec2} a The first vector
* @param {ReadonlyVec2} b The second vector
* @returns {number} The signed angle in radians
*/
function signedAngle(a, b) {
var ax = a[0],
ay = a[1],
bx = b[0],
by = b[1];
return Math.atan2(ax * by - ay * bx, ax * bx + ay * by);
}
/**
* Set the components of a vec2 to zero
*
* @param {vec2} out the receiving vector
* @returns {vec2} out
*/
function zero(out) {
out[0] = 0.0;
out[1] = 0.0;
return out;
}
/**
* Returns a string representation of a vector
*
* @param {ReadonlyVec2} a vector to represent as a string
* @returns {String} string representation of the vector
*/
function str(a) {
return "vec2(" + a[0] + ", " + a[1] + ")";
}
/**
* Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
*
* @param {ReadonlyVec2} a The first vector.
* @param {ReadonlyVec2} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function exactEquals(a, b) {
return a[0] === b[0] && a[1] === b[1];
}
/**
* Returns whether or not the vectors have approximately the same elements in the same position.
*
* @param {ReadonlyVec2} a The first vector.
* @param {ReadonlyVec2} b The second vector.
* @returns {Boolean} True if the vectors are equal, false otherwise.
*/
function equals$2(a, b) {
var a0 = a[0],
a1 = a[1];
var b0 = b[0],
b1 = b[1];
return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1));
}
/**
* Alias for {@link vec2.length}
* @function
*/
var len = length;
/**
* Alias for {@link vec2.subtract}
* @function
*/
var sub = subtract;
/**
* Alias for {@link vec2.multiply}
* @function
*/
var mul = multiply;
/**
* Alias for {@link vec2.divide}
* @function
*/
var div = divide;
/**
* Alias for {@link vec2.distance}
* @function
*/
var dist = distance;
/**
* Alias for {@link vec2.squaredDistance}
* @function
*/
var sqrDist = squaredDistance;
/**
* Alias for {@link vec2.squaredLength}
* @function
*/
var sqrLen = squaredLength;
/**
* Perform some operation over an array of vec2s.
*
* @param {Array} a the array of vectors to iterate over
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
* @param {Number} offset Number of elements to skip at the beginning of the array
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
* @param {Function} fn Function to call for each vector in the array
* @param {Object} [arg] additional argument to pass to fn
* @returns {Array} a
* @function
*/
var forEach = function () {
var vec = create();
return function (a, stride, offset, count, fn, arg) {
var i, l;
if (!stride) {
stride = 2;
}
if (!offset) {
offset = 0;
}
if (count) {
l = Math.min(count * stride + offset, a.length);
} else {
l = a.length;
}
for (i = offset; i < l; i += stride) {
vec[0] = a[i];
vec[1] = a[i + 1];
fn(vec, vec, arg);
a[i] = vec[0];
a[i + 1] = vec[1];
}
return a;
};
}();
var vec2 = /*#__PURE__*/Object.freeze({
__proto__: null,
add: add,
angle: angle,
ceil: ceil,
clone: clone$1,
copy: copy,
create: create,
cross: cross,
dist: dist,
distance: distance,
div: div,
divide: divide,
dot: dot$1,
equals: equals$2,
exactEquals: exactEquals,
floor: floor$1,
forEach: forEach,
fromValues: fromValues,
inverse: inverse,
len: len,
length: length,
lerp: lerp$1,
max: max$2,
min: min$1,
mul: mul,
multiply: multiply,
negate: negate,
normalize: normalize,
random: random,
rotate: rotate$1,
round: round$1,
scale: scale,
scaleAndAdd: scaleAndAdd,
set: set,
signedAngle: signedAngle,
sqrDist: sqrDist,
sqrLen: sqrLen,
squaredDistance: squaredDistance,
squaredLength: squaredLength,
str: str,
sub: sub,
subtract: subtract,
transformMat2: transformMat2,
transformMat2d: transformMat2d,
transformMat3: transformMat3,
transformMat4: transformMat4,
zero: zero
});
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
function getDefaultExportFromNamespaceIfPresent (n) {
return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
}
function getDefaultExportFromNamespaceIfNotNamed (n) {
return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
}
function getAugmentedNamespace(n) {
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;
var f = n.default;
if (typeof f == "function") {
var a = function a () {
var isInstance = false;
try {
isInstance = this instanceof a;
} catch {}
if (isInstance) {
return Reflect.construct(f, arguments, this.constructor);
}
return f.apply(this, arguments);
};
a.prototype = f.prototype;
} else a = {};
Object.defineProperty(a, '__esModule', {value: true});
Object.keys(n).forEach(function (k) {
var d = Object.getOwnPropertyDescriptor(n, k);
Object.defineProperty(a, k, d.get ? d : {
enumerable: true,
get: function () {
return n[k];
}
});
});
return a;
}
var unitbezier;
var hasRequiredUnitbezier;
function requireUnitbezier () {
if (hasRequiredUnitbezier) return unitbezier;
hasRequiredUnitbezier = 1;
'use strict';
unitbezier = UnitBezier;
function UnitBezier(p1x, p1y, p2x, p2y) {
// Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1).
this.cx = 3.0 * p1x;
this.bx = 3.0 * (p2x - p1x) - this.cx;
this.ax = 1.0 - this.cx - this.bx;
this.cy = 3.0 * p1y;
this.by = 3.0 * (p2y - p1y) - this.cy;
this.ay = 1.0 - this.cy - this.by;
this.p1x = p1x;
this.p1y = p1y;
this.p2x = p2x;
this.p2y = p2y;
}
UnitBezier.prototype = {
sampleCurveX: function (t) {
// `ax t^3 + bx t^2 + cx t' expanded using Horner's rule.
return ((this.ax * t + this.bx) * t + this.cx) * t;
},
sampleCurveY: function (t) {
return ((this.ay * t + this.by) * t + this.cy) * t;
},
sampleCurveDerivativeX: function (t) {
return (3.0 * this.ax * t + 2.0 * this.bx) * t + this.cx;
},
solveCurveX: function (x, epsilon) {
if (epsilon === undefined) epsilon = 1e-6;
if (x < 0.0) return 0.0;
if (x > 1.0) return 1.0;
var t = x;
// First try a few iterations of Newton's method - normally very fast.
for (var i = 0; i < 8; i++) {
var x2 = this.sampleCurveX(t) - x;
if (Math.abs(x2) < epsilon) return t;
var d2 = this.sampleCurveDerivativeX(t);
if (Math.abs(d2) < 1e-6) break;
t = t - x2 / d2;
}
// Fall back to the bisection method for reliability.
var t0 = 0.0;
var t1 = 1.0;
t = x;
for (i = 0; i < 20; i++) {
x2 = this.sampleCurveX(t);
if (Math.abs(x2 - x) < epsilon) break;
if (x > x2) {
t0 = t;
} else {
t1 = t;
}
t = (t1 - t0) * 0.5 + t0;
}
return t;
},
solve: function (x, epsilon) {
return this.sampleCurveY(this.solveCurveX(x, epsilon));
}
};
return unitbezier;
}
var unitbezierExports = requireUnitbezier();
var UnitBezier = /*@__PURE__*/getDefaultExportFromCjs(unitbezierExports);
/**
* A standalone point geometry with useful accessor, comparison, and
* modification methods.
*
* @class
* @param {number} x the x-coordinate. This could be longitude or screen pixels, or any other sort of unit.
* @param {number} y the y-coordinate. This could be latitude or screen pixels, or any other sort of unit.
*
* @example
* const point = new Point(-77, 38);
*/
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype = {
/**
* Clone this point, returning a new point that can be modified
* without affecting the old one.
* @return {Point} the clone
*/
clone() { return new Point(this.x, this.y); },
/**
* Add this point's x & y coordinates to another point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
add(p) { return this.clone()._add(p); },
/**
* Subtract this point's x & y coordinates to from point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
sub(p) { return this.clone()._sub(p); },
/**
* Multiply this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
multByPoint(p) { return this.clone()._multByPoint(p); },
/**
* Divide this point's x & y coordinates by point,
* yielding a new point.
* @param {Point} p the other point
* @return {Point} output point
*/
divByPoint(p) { return this.clone()._divByPoint(p); },
/**
* Multiply this point's x & y coordinates by a factor,
* yielding a new point.
* @param {number} k factor
* @return {Point} output point
*/
mult(k) { return this.clone()._mult(k); },
/**
* Divide this point's x & y coordinates by a factor,
* yielding a new point.
* @param {number} k factor
* @return {Point} output point
*/
div(k) { return this.clone()._div(k); },
/**
* Rotate this point around the 0, 0 origin by an angle a,
* given in radians
* @param {number} a angle to rotate around, in radians
* @return {Point} output point
*/
rotate(a) { return this.clone()._rotate(a); },
/**
* Rotate this point around p point by an angle a,
* given in radians
* @param {number} a angle to rotate around, in radians
* @param {Point} p Point to rotate around
* @return {Point} output point
*/
rotateAround(a, p) { return this.clone()._rotateAround(a, p); },
/**
* Multiply this point by a 4x1 transformation matrix
* @param {[number, number, number, number]} m transformation matrix
* @return {Point} output point
*/
matMult(m) { return this.clone()._matMult(m); },
/**
* Calculate this point but as a unit vector from 0, 0, meaning
* that the distance from the resulting point to the 0, 0
* coordinate will be equal to 1 and the angle from the resulting
* point to the 0, 0 coordinate will be the same as before.
* @return {Point} unit vector point
*/
unit() { return this.clone()._unit(); },
/**
* Compute a perpendicular point, where the new y coordinate
* is the old x coordinate and the new x coordinate is the old y
* coordinate multiplied by -1
* @return {Point} perpendicular point
*/
perp() { return this.clone()._perp(); },
/**
* Return a version of this point with the x & y coordinates
* rounded to integers.
* @return {Point} rounded point
*/
round() { return this.clone()._round(); },
/**
* Return the magnitude of this point: this is the Euclidean
* distance from the 0, 0 coordinate to this point's x and y
* coordinates.
* @return {number} magnitude
*/
mag() {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
/**
* Judge whether this point is equal to another point, returning
* true or false.
* @param {Point} other the other point
* @return {boolean} whether the points are equal
*/
equals(other) {
return this.x === other.x &&
this.y === other.y;
},
/**
* Calculate the distance from this point to another point
* @param {Point} p the other point
* @return {number} distance
*/
dist(p) {
return Math.sqrt(this.distSqr(p));
},
/**
* Calculate the distance from this point to another point,
* without the square root step. Useful if you're comparing
* relative distances.
* @param {Point} p the other point
* @return {number} distance
*/
distSqr(p) {
const dx = p.x - this.x,
dy = p.y - this.y;
return dx * dx + dy * dy;
},
/**
* Get the angle from the 0, 0 coordinate to this point, in radians
* coordinates.
* @return {number} angle
*/
angle() {
return Math.atan2(this.y, this.x);
},
/**
* Get the angle from this point to another point, in radians
* @param {Point} b the other point
* @return {number} angle
*/
angleTo(b) {
return Math.atan2(this.y - b.y, this.x - b.x);
},
/**
* Get the angle between this point and another point, in radians
* @param {Point} b the other point
* @return {number} angle
*/
angleWith(b) {
return this.angleWithSep(b.x, b.y);
},
/**
* Find the angle of the two vectors, solving the formula for
* the cross product a x b = |a||b|sin(θ) for θ.
* @param {number} x the x-coordinate
* @param {number} y the y-coordinate
* @return {number} the angle in radians
*/
angleWithSep(x, y) {
return Math.atan2(
this.x * y - this.y * x,
this.x * x + this.y * y);
},
/** @param {[number, number, number, number]} m */
_matMult(m) {
const x = m[0] * this.x + m[1] * this.y,
y = m[2] * this.x + m[3] * this.y;
this.x = x;
this.y = y;
return this;
},
/** @param {Point} p */
_add(p) {
this.x += p.x;
this.y += p.y;
return this;
},
/** @param {Point} p */
_sub(p) {
this.x -= p.x;
this.y -= p.y;
return this;
},
/** @param {number} k */
_mult(k) {
this.x *= k;
this.y *= k;
return this;
},
/** @param {number} k */
_div(k) {
this.x /= k;
this.y /= k;
return this;
},
/** @param {Point} p */
_multByPoint(p) {
this.x *= p.x;
this.y *= p.y;
return this;
},
/** @param {Point} p */
_divByPoint(p) {
this.x /= p.x;
this.y /= p.y;
return this;
},
_unit() {
this._div(this.mag());
return this;
},
_perp() {
const y = this.y;
this.y = this.x;
this.x = -y;
return this;
},
/** @param {number} angle */
_rotate(angle) {
const cos = Math.cos(angle),
sin = Math.sin(angle),
x = cos * this.x - sin * this.y,
y = sin * this.x + cos * this.y;
this.x = x;
this.y = y;
return this;
},
/**
* @param {number} angle
* @param {Point} p
*/
_rotateAround(angle, p) {
const cos = Math.cos(angle),
sin = Math.sin(angle),
x = p.x + cos * (this.x - p.x) - sin * (this.y - p.y),
y = p.y + sin * (this.x - p.x) + cos * (this.y - p.y);
this.x = x;
this.y = y;
return this;
},
_round() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
return this;
},
constructor: Point
};
/**
* Construct a point from an array if necessary, otherwise if the input
* is already a Point, return it unchanged.
* @param {Point | [number, number] | {x: number, y: number}} p input value
* @return {Point} constructed point.
* @example
* // this
* var point = Point.convert([0, 1]);
* // is equivalent to
* var point = new Point(0, 1);
*/
Point.convert = function (p) {
if (p instanceof Point) {
return /** @type {Point} */ (p);
}
if (Array.isArray(p)) {
return new Point(+p[0], +p[1]);
}
if (p.x !== undefined && p.y !== undefined) {
return new Point(+p.x, +p.y);
}
throw new Error('Expected [x, y] or {x, y} point format');
};
var assert$2 = {exports: {}};
var isArguments;
var hasRequiredIsArguments;
function requireIsArguments () {
if (hasRequiredIsArguments) return isArguments;
hasRequiredIsArguments = 1;
'use strict';
var toStr = Object.prototype.toString;
isArguments = function isArguments(value) {
var str = toStr.call(value);
var isArgs = str === '[object Arguments]';
if (!isArgs) {
isArgs = str !== '[object Array]' &&
value !== null &&
typeof value === 'object' &&
typeof value.length === 'number' &&
value.length >= 0 &&
toStr.call(value.callee) === '[object Function]';
}
return isArgs;
};
return isArguments;
}
var implementation$2;
var hasRequiredImplementation$2;
function requireImplementation$2 () {
if (hasRequiredImplementation$2) return implementation$2;
hasRequiredImplementation$2 = 1;
'use strict';
var keysShim;
if (!Object.keys) {
// modified from https://github.com/es-shims/es5-shim
var has = Object.prototype.hasOwnProperty;
var toStr = Object.prototype.toString;
var isArgs = requireIsArguments(); // eslint-disable-line global-require
var isEnumerable = Object.prototype.propertyIsEnumerable;
var hasDontEnumBug = !isEnumerable.call({ toString: null }, 'toString');
var hasProtoEnumBug = isEnumerable.call(function () {}, 'prototype');
var dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
];
var equalsConstructorPrototype = function (o) {
var ctor = o.constructor;
return ctor && ctor.prototype === o;
};
var excludedKeys = {
$applicationCache: true,
$console: true,
$external: true,
$frame: true,
$frameElement: true,
$frames: true,
$innerHeight: true,
$innerWidth: true,
$onmozfullscreenchange: true,
$onmozfullscreenerror: true,
$outerHeight: true,
$outerWidth: true,
$pageXOffset: true,
$pageYOffset: true,
$parent: true,
$scrollLeft: true,
$scrollTop: true,
$scrollX: true,
$scrollY: true,
$self: true,
$webkitIndexedDB: true,
$webkitStorageInfo: true,
$window: true
};
var hasAutomationEqualityBug = (function () {
/* global window */
if (typeof window === 'undefined') { return false; }
for (var k in window) {
try {
if (!excludedKeys['$' + k] && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
equalsConstructorPrototype(window[k]);
} catch (e) {
return true;
}
}
} catch (e) {
return true;
}
}
return false;
}());
var equalsConstructorPrototypeIfNotBuggy = function (o) {
/* global window */
if (typeof window === 'undefined' || !hasAutomationEqualityBug) {
return equalsConstructorPrototype(o);
}
try {
return equalsConstructorPrototype(o);
} catch (e) {
return false;
}
};
keysShim = function keys(object) {
var isObject = object !== null && typeof object === 'object';
var isFunction = toStr.call(object) === '[object Function]';
var isArguments = isArgs(object);
var isString = isObject && toStr.call(object) === '[object String]';
var theKeys = [];
if (!isObject && !isFunction && !isArguments) {
throw new TypeError('Object.keys called on a non-object');
}
var skipProto = hasProtoEnumBug && isFunction;
if (isString && object.length > 0 && !has.call(object, 0)) {
for (var i = 0; i < object.length; ++i) {
theKeys.push(String(i));
}
}
if (isArguments && object.length > 0) {
for (var j = 0; j < object.length; ++j) {
theKeys.push(String(j));
}
} else {
for (var name in object) {
if (!(skipProto && name === 'prototype') && has.call(object, name)) {
theKeys.push(String(name));
}
}
}
if (hasDontEnumBug) {
var skipConstructor = equalsConstructorPrototypeIfNotBuggy(object);
for (var k = 0; k < dontEnums.length; ++k) {
if (!(skipConstructor && dontEnums[k] === 'constructor') && has.call(object, dontEnums[k])) {
theKeys.push(dontEnums[k]);
}
}
}
return theKeys;
};
}
implementation$2 = keysShim;
return implementation$2;
}
var objectKeys;
var hasRequiredObjectKeys;
function requireObjectKeys () {
if (hasRequiredObjectKeys) return objectKeys;
hasRequiredObjectKeys = 1;
'use strict';
var slice = Array.prototype.slice;
var isArgs = requireIsArguments();
var origKeys = Object.keys;
var keysShim = origKeys ? function keys(o) { return origKeys(o); } : requireImplementation$2();
var originalKeys = Object.keys;
keysShim.shim = function shimObjectKeys() {
if (Object.keys) {
var keysWorksWithArguments = (function () {
// Safari 5.0 bug
var args = Object.keys(arguments);
return args && args.length === arguments.length;
}(1, 2));
if (!keysWorksWithArguments) {
Object.keys = function keys(object) { // eslint-disable-line func-name-matching
if (isArgs(object)) {
return originalKeys(slice.call(object));
}
return originalKeys(object);
};
}
} else {
Object.keys = keysShim;
}
return Object.keys || keysShim;
};
objectKeys = keysShim;
return objectKeys;
}
var shams;
var hasRequiredShams;
function requireShams () {
if (hasRequiredShams) return shams;
hasRequiredShams = 1;
'use strict';
/** @type {import('./shams')} */
/* eslint complexity: [2, 18], max-statements: [2, 33] */
shams = function hasSymbols() {
if (typeof Symbol !== 'function' || typeof Object.getOwnPropertySymbols !== 'function') { return false; }
if (typeof Symbol.iterator === 'symbol') { return true; }
/** @type {{ [k in symbol]?: unknown }} */
var obj = {};
var sym = Symbol('test');
var symObj = Object(sym);
if (typeof sym === 'string') { return false; }
if (Object.prototype.toString.call(sym) !== '[object Symbol]') { return false; }
if (Object.prototype.toString.call(symObj) !== '[object Symbol]') { return false; }
// temp disabled per https://github.com/ljharb/object.assign/issues/17
// if (sym instanceof Symbol) { return false; }
// temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4
// if (!(symObj instanceof Symbol)) { return false; }
// if (typeof Symbol.prototype.toString !== 'function') { return false; }
// if (String(sym) !== Symbol.prototype.toString.call(sym)) { return false; }
var symVal = 42;
obj[sym] = symVal;
for (var _ in obj) { return false; } // eslint-disable-line no-restricted-syntax, no-unreachable-loop
if (typeof Object.keys === 'function' && Object.keys(obj).length !== 0) { return false; }
if (typeof Object.getOwnPropertyNames === 'function' && Object.getOwnPropertyNames(obj).length !== 0) { return false; }
var syms = Object.getOwnPropertySymbols(obj);
if (syms.length !== 1 || syms[0] !== sym) { return false; }
if (!Object.prototype.propertyIsEnumerable.call(obj, sym)) { return false; }
if (typeof Object.getOwnPropertyDescriptor === 'function') {
// eslint-disable-next-line no-extra-parens
var descriptor = /** @type {PropertyDescriptor} */ (Object.getOwnPropertyDescriptor(obj, sym));
if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; }
}
return true;
};
return shams;
}
var esObjectAtoms;
var hasRequiredEsObjectAtoms;
function requireEsObjectAtoms () {
if (hasRequiredEsObjectAtoms) return esObjectAtoms;
hasRequiredEsObjectAtoms = 1;
'use strict';
/** @type {import('.')} */
esObjectAtoms = Object;
return esObjectAtoms;
}
var esErrors;
var hasRequiredEsErrors;
function requireEsErrors () {
if (hasRequiredEsErrors) return esErrors;
hasRequiredEsErrors = 1;
'use strict';
/** @type {import('.')} */
esErrors = Error;
return esErrors;
}
var _eval;
var hasRequired_eval;
function require_eval () {
if (hasRequired_eval) return _eval;
hasRequired_eval = 1;
'use strict';
/** @type {import('./eval')} */
_eval = EvalError;
return _eval;
}
var range;
var hasRequiredRange;
function requireRange () {
if (hasRequiredRange) return range;
hasRequiredRange = 1;
'use strict';
/** @type {import('./range')} */
range = RangeError;
return range;
}
var ref;
var hasRequiredRef;
function requireRef () {
if (hasRequiredRef) return ref;
hasRequiredRef = 1;
'use strict';
/** @type {import('./ref')} */
ref = ReferenceError;
return ref;
}
var syntax;
var hasRequiredSyntax;
function requireSyntax () {
if (hasRequiredSyntax) return syntax;
hasRequiredSyntax = 1;
'use strict';
/** @type {import('./syntax')} */
syntax = SyntaxError;
return syntax;
}
var type$1;
var hasRequiredType;
function requireType () {
if (hasRequiredType) return type$1;
hasRequiredType = 1;
'use strict';
/** @type {import('./type')} */
type$1 = TypeError;
return type$1;
}
var uri;
var hasRequiredUri;
function requireUri () {
if (hasRequiredUri) return uri;
hasRequiredUri = 1;
'use strict';
/** @type {import('./uri')} */
uri = URIError;
return uri;
}
var abs;
var hasRequiredAbs;
function requireAbs () {
if (hasRequiredAbs) return abs;
hasRequiredAbs = 1;
'use strict';
/** @type {import('./abs')} */
abs = Math.abs;
return abs;
}
var floor;
var hasRequiredFloor;
function requireFloor () {
if (hasRequiredFloor) return floor;
hasRequiredFloor = 1;
'use strict';
/** @type {import('./floor')} */
floor = Math.floor;
return floor;
}
var max$1;
var hasRequiredMax;
function requireMax () {
if (hasRequiredMax) return max$1;
hasRequiredMax = 1;
'use strict';
/** @type {import('./max')} */
max$1 = Math.max;
return max$1;
}
var min;
var hasRequiredMin;
function requireMin () {
if (hasRequiredMin) return min;
hasRequiredMin = 1;
'use strict';
/** @type {import('./min')} */
min = Math.min;
return min;
}
var pow;
var hasRequiredPow;
function requirePow () {
if (hasRequiredPow) return pow;
hasRequiredPow = 1;
'use strict';
/** @type {import('./pow')} */
pow = Math.pow;
return pow;
}
var round;
var hasRequiredRound;
function requireRound () {
if (hasRequiredRound) return round;
hasRequiredRound = 1;
'use strict';
/** @type {import('./round')} */
round = Math.round;
return round;
}
var _isNaN;
var hasRequired_isNaN;
function require_isNaN () {
if (hasRequired_isNaN) return _isNaN;
hasRequired_isNaN = 1;
'use strict';
/** @type {import('./isNaN')} */
_isNaN = Number.isNaN || function isNaN(a) {
return a !== a;
};
return _isNaN;
}
var sign$1;
var hasRequiredSign;
function requireSign () {
if (hasRequiredSign) return sign$1;
hasRequiredSign = 1;
'use strict';
var $isNaN = require_isNaN();
/** @type {import('./sign')} */
sign$1 = function sign(number) {
if ($isNaN(number) || number === 0) {
return number;
}
return number < 0 ? -1 : +1;
};
return sign$1;
}
var gOPD;
var hasRequiredGOPD;
function requireGOPD () {
if (hasRequiredGOPD) return gOPD;
hasRequiredGOPD = 1;
'use strict';
/** @type {import('./gOPD')} */
gOPD = Object.getOwnPropertyDescriptor;
return gOPD;
}
var gopd;
var hasRequiredGopd;
function requireGopd () {
if (hasRequiredGopd) return gopd;
hasRequiredGopd = 1;
'use strict';
/** @type {import('.')} */
var $gOPD = requireGOPD();
if ($gOPD) {
try {
$gOPD([], 'length');
} catch (e) {
// IE 8 has a broken gOPD
$gOPD = null;
}
}
gopd = $gOPD;
return gopd;
}
var esDefineProperty;
var hasRequiredEsDefineProperty;
function requireEsDefineProperty () {
if (hasRequiredEsDefineProperty) return esDefineProperty;
hasRequiredEsDefineProperty = 1;
'use strict';
/** @type {import('.')} */
var $defineProperty = Object.defineProperty || false;
if ($defineProperty) {
try {
$defineProperty({}, 'a', { value: 1 });
} catch (e) {
// IE 8 has a broken defineProperty
$defineProperty = false;
}
}
esDefineProperty = $defineProperty;
return esDefineProperty;
}
var hasSymbols;
var hasRequiredHasSymbols;
function requireHasSymbols () {
if (hasRequiredHasSymbols) return hasSymbols;
hasRequiredHasSymbols = 1;
'use strict';
var origSymbol = typeof Symbol !== 'undefined' && Symbol;
var hasSymbolSham = requireShams();
/** @type {import('.')} */
hasSymbols = function hasNativeSymbols() {
if (typeof origSymbol !== 'function') { return false; }
if (typeof Symbol !== 'function') { return false; }
if (typeof origSymbol('foo') !== 'symbol') { return false; }
if (typeof Symbol('bar') !== 'symbol') { return false; }
return hasSymbolSham();
};
return hasSymbols;
}
var Reflect_getPrototypeOf;
var hasRequiredReflect_getPrototypeOf;
function requireReflect_getPrototypeOf () {
if (hasRequiredReflect_getPrototypeOf) return Reflect_getPrototypeOf;
hasRequiredReflect_getPrototypeOf = 1;
'use strict';
/** @type {import('./Reflect.getPrototypeOf')} */
Reflect_getPrototypeOf = (typeof Reflect !== 'undefined' && Reflect.getPrototypeOf) || null;
return Reflect_getPrototypeOf;
}
var Object_getPrototypeOf;
var hasRequiredObject_getPrototypeOf;
function requireObject_getPrototypeOf () {
if (hasRequiredObject_getPrototypeOf) return Object_getPrototypeOf;
hasRequiredObject_getPrototypeOf = 1;
'use strict';
var $Object = /*@__PURE__*/ requireEsObjectAtoms();
/** @type {import('./Object.getPrototypeOf')} */
Object_getPrototypeOf = $Object.getPrototypeOf || null;
return Object_getPrototypeOf;
}
var implementation$1;
var hasRequiredImplementation$1;
function requireImplementation$1 () {
if (hasRequiredImplementation$1) return implementation$1;
hasRequiredImplementation$1 = 1;
'use strict';
/* eslint no-invalid-this: 1 */
var ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ';
var toStr = Object.prototype.toString;
var max = Math.max;
var funcType = '[object Function]';
var concatty = function concatty(a, b) {
var arr = [];
for (var i = 0; i < a.length; i += 1) {
arr[i] = a[i];
}
for (var j = 0; j < b.length; j += 1) {
arr[j + a.length] = b[j];
}
return arr;
};
var slicy = function slicy(arrLike, offset) {
var arr = [];
for (var i = offset || 0, j = 0; i < arrLike.length; i += 1, j += 1) {
arr[j] = arrLike[i];
}
return arr;
};
var joiny = function (arr, joiner) {
var str = '';
for (var i = 0; i < arr.length; i += 1) {
str += arr[i];
if (i + 1 < arr.length) {
str += joiner;
}
}
return str;
};
implementation$1 = function bind(that) {
var target = this;
if (typeof target !== 'function' || toStr.apply(target) !== funcType) {
throw new TypeError(ERROR_MESSAGE + target);
}
var args = slicy(arguments, 1);
var bound;
var binder = function () {
if (this instanceof bound) {
var result = target.apply(
this,
concatty(args, arguments)
);
if (Object(result) === result) {
return result;
}
return this;
}
return target.apply(
that,
concatty(args, arguments)
);
};
var boundLength = max(0, target.length - args.length);
var boundArgs = [];
for (var i = 0; i < boundLength; i++) {
boundArgs[i] = '$' + i;
}
bound = Function('binder', 'return function (' + joiny(boundArgs, ',') + '){ return binder.apply(this,arguments); }')(binder);
if (target.prototype) {
var Empty = function Empty() {};
Empty.prototype = target.prototype;
bound.prototype = new Empty();
Empty.prototype = null;
}
return bound;
};
return implementation$1;
}
var functionBind;
var hasRequiredFunctionBind;
function requireFunctionBind () {
if (hasRequiredFunctionBind) return functionBind;
hasRequiredFunctionBind = 1;
'use strict';
var implementation = requireImplementation$1();
functionBind = Function.prototype.bind || implementation;
return functionBind;
}
var functionCall;
var hasRequiredFunctionCall;
function requireFunctionCall () {
if (hasRequiredFunctionCall) return functionCall;
hasRequiredFunctionCall = 1;
'use strict';
/** @type {import('./functionCall')} */
functionCall = Function.prototype.call;
return functionCall;
}
var functionApply;
var hasRequiredFunctionApply;
function requireFunctionApply () {
if (hasRequiredFunctionApply) return functionApply;
hasRequiredFunctionApply = 1;
'use strict';
/** @type {import('./functionApply')} */
functionApply = Function.prototype.apply;
return functionApply;
}
var reflectApply;
var hasRequiredReflectApply;
function requireReflectApply () {
if (hasRequiredReflectApply) return reflectApply;
hasRequiredReflectApply = 1;
'use strict';
/** @type {import('./reflectApply')} */
reflectApply = typeof Reflect !== 'undefined' && Reflect && Reflect.apply;
return reflectApply;
}
var actualApply;
var hasRequiredActualApply;
function requireActualApply () {
if (hasRequiredActualApply) return actualApply;
hasRequiredActualApply = 1;
'use strict';
var bind = requireFunctionBind();
var $apply = requireFunctionApply();
var $call = requireFunctionCall();
var $reflectApply = requireReflectApply();
/** @type {import('./actualApply')} */
actualApply = $reflectApply || bind.call($call, $apply);
return actualApply;
}
var callBindApplyHelpers;
var hasRequiredCallBindApplyHelpers;
function requireCallBindApplyHelpers () {
if (hasRequiredCallBindApplyHelpers) return callBindApplyHelpers;
hasRequiredCallBindApplyHelpers = 1;
'use strict';
var bind = requireFunctionBind();
var $TypeError = /*@__PURE__*/ requireType();
var $call = requireFunctionCall();
var $actualApply = requireActualApply();
/** @type {(args: [Function, thisArg?: unknown, ...args: unknown[]]) => Function} TODO FIXME, find a way to use import('.') */
callBindApplyHelpers = function callBindBasic(args) {
if (args.length < 1 || typeof args[0] !== 'function') {
throw new $TypeError('a function is required');
}
return $actualApply(bind, $call, args);
};
return callBindApplyHelpers;
}
var get$1;
var hasRequiredGet;
function requireGet () {
if (hasRequiredGet) return get$1;
hasRequiredGet = 1;
'use strict';
var callBind = requireCallBindApplyHelpers();
var gOPD = /*@__PURE__*/ requireGopd();
var hasProtoAccessor;
try {
// eslint-disable-next-line no-extra-parens, no-proto
hasProtoAccessor = /** @type {{ __proto__?: typeof Array.prototype }} */ ([]).__proto__ === Array.prototype;
} catch (e) {
if (!e || typeof e !== 'object' || !('code' in e) || e.code !== 'ERR_PROTO_ACCESS') {
throw e;
}
}
// eslint-disable-next-line no-extra-parens
var desc = !!hasProtoAccessor && gOPD && gOPD(Object.prototype, /** @type {keyof typeof Object.prototype} */ ('__proto__'));
var $Object = Object;
var $getPrototypeOf = $Object.getPrototypeOf;
/** @type {import('./get')} */
get$1 = desc && typeof desc.get === 'function'
? callBind([desc.get])
: typeof $getPrototypeOf === 'function'
? /** @type {import('./get')} */ function getDunder(value) {
// eslint-disable-next-line eqeqeq
return $getPrototypeOf(value == null ? value : $Object(value));
}
: false;
return get$1;
}
var getProto;
var hasRequiredGetProto;
function requireGetProto () {
if (hasRequiredGetProto) return getProto;
hasRequiredGetProto = 1;
'use strict';
var reflectGetProto = requireReflect_getPrototypeOf();
var originalGetProto = requireObject_getPrototypeOf();
var getDunderProto = /*@__PURE__*/ requireGet();
/** @type {import('.')} */
getProto = reflectGetProto
? function getProto(O) {
// @ts-expect-error TS can't narrow inside a closure, for some reason
return reflectGetProto(O);
}
: originalGetProto
? function getProto(O) {
if (!O || (typeof O !== 'object' && typeof O !== 'function')) {
throw new TypeError('getProto: not an object');
}
// @ts-expect-error TS can't narrow inside a closure, for some reason
return originalGetProto(O);
}
: getDunderProto
? function getProto(O) {
// @ts-expect-error TS can't narrow inside a closure, for some reason
return getDunderProto(O);
}
: null;
return getProto;
}
var hasown;
var hasRequiredHasown;
function requireHasown () {
if (hasRequiredHasown) return hasown;
hasRequiredHasown = 1;
'use strict';
var call = Function.prototype.call;
var $hasOwn = Object.prototype.hasOwnProperty;
var bind = requireFunctionBind();
/** @type {import('.')} */
hasown = bind.call(call, $hasOwn);
return hasown;
}
var getIntrinsic;
var hasRequiredGetIntrinsic;
function requireGetIntrinsic () {
if (hasRequiredGetIntrinsic) return getIntrinsic;
hasRequiredGetIntrinsic = 1;
'use strict';
var undefined$1;
var $Object = /*@__PURE__*/ requireEsObjectAtoms();
var $Error = /*@__PURE__*/ requireEsErrors();
var $EvalError = /*@__PURE__*/ require_eval();
var $RangeError = /*@__PURE__*/ requireRange();
var $ReferenceError = /*@__PURE__*/ requireRef();
var $SyntaxError = /*@__PURE__*/ requireSyntax();
var $TypeError = /*@__PURE__*/ requireType();
var $URIError = /*@__PURE__*/ requireUri();
var abs = /*@__PURE__*/ requireAbs();
var floor = /*@__PURE__*/ requireFloor();
var max = /*@__PURE__*/ requireMax();
var min = /*@__PURE__*/ requireMin();
var pow = /*@__PURE__*/ requirePow();
var round = /*@__PURE__*/ requireRound();
var sign = /*@__PURE__*/ requireSign();
var $Function = Function;
// eslint-disable-next-line consistent-return
var getEvalledConstructor = function (expressionSyntax) {
try {
return $Function('"use strict"; return (' + expressionSyntax + ').constructor;')();
} catch (e) {}
};
var $gOPD = /*@__PURE__*/ requireGopd();
var $defineProperty = /*@__PURE__*/ requireEsDefineProperty();
var throwTypeError = function () {
throw new $TypeError();
};
var ThrowTypeError = $gOPD
? (function () {
try {
// eslint-disable-next-line no-unused-expressions, no-caller, no-restricted-properties
arguments.callee; // IE 8 does not throw here
return throwTypeError;
} catch (calleeThrows) {
try {
// IE 8 throws on Object.getOwnPropertyDescriptor(arguments, '')
return $gOPD(arguments, 'callee').get;
} catch (gOPDthrows) {
return throwTypeError;
}
}
}())
: throwTypeError;
var hasSymbols = requireHasSymbols()();
var getProto = requireGetProto();
var $ObjectGPO = requireObject_getPrototypeOf();
var $ReflectGPO = requireReflect_getPrototypeOf();
var $apply = requireFunctionApply();
var $call = requireFunctionCall();
var needsEval = {};
var TypedArray = typeof Uint8Array === 'undefined' || !getProto ? undefined$1 : getProto(Uint8Array);
var INTRINSICS = {
__proto__: null,
'%AggregateError%': typeof AggregateError === 'undefined' ? undefined$1 : AggregateError,
'%Array%': Array,
'%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined$1 : ArrayBuffer,
'%ArrayIteratorPrototype%': hasSymbols && getProto ? getProto([][Symbol.iterator]()) : undefined$1,
'%AsyncFromSyncIteratorPrototype%': undefined$1,
'%AsyncFunction%': needsEval,
'%AsyncGenerator%': needsEval,
'%AsyncGeneratorFunction%': needsEval,
'%AsyncIteratorPrototype%': needsEval,
'%Atomics%': typeof Atomics === 'undefined' ? undefined$1 : Atomics,
'%BigInt%': typeof BigInt === 'undefined' ? undefined$1 : BigInt,
'%BigInt64Array%': typeof BigInt64Array === 'undefined' ? undefined$1 : BigInt64Array,
'%BigUint64Array%': typeof BigUint64Array === 'undefined' ? undefined$1 : BigUint64Array,
'%Boolean%': Boolean,
'%DataView%': typeof DataView === 'undefined' ? undefined$1 : DataView,
'%Date%': Date,
'%decodeURI%': decodeURI,
'%decodeURIComponent%': decodeURIComponent,
'%encodeURI%': encodeURI,
'%encodeURIComponent%': encodeURIComponent,
'%Error%': $Error,
'%eval%': eval, // eslint-disable-line no-eval
'%EvalError%': $EvalError,
'%Float16Array%': typeof Float16Array === 'undefined' ? undefined$1 : Float16Array,
'%Float32Array%': typeof Float32Array === 'undefined' ? undefined$1 : Float32Array,
'%Float64Array%': typeof Float64Array === 'undefined' ? undefined$1 : Float64Array,
'%FinalizationRegistry%': typeof FinalizationRegistry === 'undefined' ? undefined$1 : FinalizationRegistry,
'%Function%': $Function,
'%GeneratorFunction%': needsEval,
'%Int8Array%': typeof Int8Array === 'undefined' ? undefined$1 : Int8Array,
'%Int16Array%': typeof Int16Array === 'undefined' ? undefined$1 : Int16Array,
'%Int32Array%': typeof Int32Array === 'undefined' ? undefined$1 : Int32Array,
'%isFinite%': isFinite,
'%isNaN%': isNaN,
'%IteratorPrototype%': hasSymbols && getProto ? getProto(getProto([][Symbol.iterator]())) : undefined$1,
'%JSON%': typeof JSON === 'object' ? JSON : undefined$1,
'%Map%': typeof Map === 'undefined' ? undefined$1 : Map,
'%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols || !getProto ? undefined$1 : getProto(new Map()[Symbol.iterator]()),
'%Math%': Math,
'%Number%': Number,
'%Object%': $Object,
'%Object.getOwnPropertyDescriptor%': $gOPD,
'%parseFloat%': parseFloat,
'%parseInt%': parseInt,
'%Promise%': typeof Promise === 'undefined' ? undefined$1 : Promise,
'%Proxy%': typeof Proxy === 'undefined' ? undefined$1 : Proxy,
'%RangeError%': $RangeError,
'%ReferenceError%': $ReferenceError,
'%Reflect%': typeof Reflect === 'undefined' ? undefined$1 : Reflect,
'%RegExp%': RegExp,
'%Set%': typeof Set === 'undefined' ? undefined$1 : Set,
'%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols || !getProto ? undefined$1 : getProto(new Set()[Symbol.iterator]()),
'%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined$1 : SharedArrayBuffer,
'%String%': String,
'%StringIteratorPrototype%': hasSymbols && getProto ? getProto(''[Symbol.iterator]()) : undefined$1,
'%Symbol%': hasSymbols ? Symbol : undefined$1,
'%SyntaxError%': $SyntaxError,
'%ThrowTypeError%': ThrowTypeError,
'%TypedArray%': TypedArray,
'%TypeError%': $TypeError,
'%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined$1 : Uint8Array,
'%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined$1 : Uint8ClampedArray,
'%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined$1 : Uint16Array,
'%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined$1 : Uint32Array,
'%URIError%': $URIError,
'%WeakMap%': typeof WeakMap === 'undefined' ? undefined$1 : WeakMap,
'%WeakRef%': typeof WeakRef === 'undefined' ? undefined$1 : WeakRef,
'%WeakSet%': typeof WeakSet === 'undefined' ? undefined$1 : WeakSet,
'%Function.prototype.call%': $call,
'%Function.prototype.apply%': $apply,
'%Object.defineProperty%': $defineProperty,
'%Object.getPrototypeOf%': $ObjectGPO,
'%Math.abs%': abs,
'%Math.floor%': floor,
'%Math.max%': max,
'%Math.min%': min,
'%Math.pow%': pow,
'%Math.round%': round,
'%Math.sign%': sign,
'%Reflect.getPrototypeOf%': $ReflectGPO
};
if (getProto) {
try {
null.error; // eslint-disable-line no-unused-expressions
} catch (e) {
// https://github.com/tc39/proposal-shadowrealm/pull/384#issuecomment-1364264229
var errorProto = getProto(getProto(e));
INTRINSICS['%Error.prototype%'] = errorProto;
}
}
var doEval = function doEval(name) {
var value;
if (name === '%AsyncFunction%') {
value = getEvalledConstructor('async function () {}');
} else if (name === '%GeneratorFunction%') {
value = getEvalledConstructor('function* () {}');
} else if (name === '%AsyncGeneratorFunction%') {
value = getEvalledConstructor('async function* () {}');
} else if (name === '%AsyncGenerator%') {
var fn = doEval('%AsyncGeneratorFunction%');
if (fn) {
value = fn.prototype;
}
} else if (name === '%AsyncIteratorPrototype%') {
var gen = doEval('%AsyncGenerator%');
if (gen && getProto) {
value = getProto(gen.prototype);
}
}
INTRINSICS[name] = value;
return value;
};
var LEGACY_ALIASES = {
__proto__: null,
'%ArrayBufferPrototype%': ['ArrayBuffer', 'prototype'],
'%ArrayPrototype%': ['Array', 'prototype'],
'%ArrayProto_entries%': ['Array', 'prototype', 'entries'],
'%ArrayProto_forEach%': ['Array', 'prototype', 'forEach'],
'%ArrayProto_keys%': ['Array', 'prototype', 'keys'],
'%ArrayProto_values%': ['Array', 'prototype', 'values'],
'%AsyncFunctionPrototype%': ['AsyncFunction', 'prototype'],
'%AsyncGenerator%': ['AsyncGeneratorFunction', 'prototype'],
'%AsyncGeneratorPrototype%': ['AsyncGeneratorFunction', 'prototype', 'prototype'],
'%BooleanPrototype%': ['Boolean', 'prototype'],
'%DataViewPrototype%': ['DataView', 'prototype'],
'%DatePrototype%': ['Date', 'prototype'],
'%ErrorPrototype%': ['Error', 'prototype'],
'%EvalErrorPrototype%': ['EvalError', 'prototype'],
'%Float32ArrayPrototype%': ['Float32Array', 'prototype'],
'%Float64ArrayPrototype%': ['Float64Array', 'prototype'],
'%FunctionPrototype%': ['Function', 'prototype'],
'%Generator%': ['GeneratorFunction', 'prototype'],
'%GeneratorPrototype%': ['GeneratorFunction', 'prototype', 'prototype'],
'%Int8ArrayPrototype%': ['Int8Array', 'prototype'],
'%Int16ArrayPrototype%': ['Int16Array', 'prototype'],
'%Int32ArrayPrototype%': ['Int32Array', 'prototype'],
'%JSONParse%': ['JSON', 'parse'],
'%JSONStringify%': ['JSON', 'stringify'],
'%MapPrototype%': ['Map', 'prototype'],
'%NumberPrototype%': ['Number', 'prototype'],
'%ObjectPrototype%': ['Object', 'prototype'],
'%ObjProto_toString%': ['Object', 'prototype', 'toString'],
'%ObjProto_valueOf%': ['Object', 'prototype', 'valueOf'],
'%PromisePrototype%': ['Promise', 'prototype'],
'%PromiseProto_then%': ['Promise', 'prototype', 'then'],
'%Promise_all%': ['Promise', 'all'],
'%Promise_reject%': ['Promise', 'reject'],
'%Promise_resolve%': ['Promise', 'resolve'],
'%RangeErrorPrototype%': ['RangeError', 'prototype'],
'%ReferenceErrorPrototype%': ['ReferenceError', 'prototype'],
'%RegExpPrototype%': ['RegExp', 'prototype'],
'%SetPrototype%': ['Set', 'prototype'],
'%SharedArrayBufferPrototype%': ['SharedArrayBuffer', 'prototype'],
'%StringPrototype%': ['String', 'prototype'],
'%SymbolPrototype%': ['Symbol', 'prototype'],
'%SyntaxErrorPrototype%': ['SyntaxError', 'prototype'],
'%TypedArrayPrototype%': ['TypedArray', 'prototype'],
'%TypeErrorPrototype%': ['TypeError', 'prototype'],
'%Uint8ArrayPrototype%': ['Uint8Array', 'prototype'],
'%Uint8ClampedArrayPrototype%': ['Uint8ClampedArray', 'prototype'],
'%Uint16ArrayPrototype%': ['Uint16Array', 'prototype'],
'%Uint32ArrayPrototype%': ['Uint32Array', 'prototype'],
'%URIErrorPrototype%': ['URIError', 'prototype'],
'%WeakMapPrototype%': ['WeakMap', 'prototype'],
'%WeakSetPrototype%': ['WeakSet', 'prototype']
};
var bind = requireFunctionBind();
var hasOwn = /*@__PURE__*/ requireHasown();
var $concat = bind.call($call, Array.prototype.concat);
var $spliceApply = bind.call($apply, Array.prototype.splice);
var $replace = bind.call($call, String.prototype.replace);
var $strSlice = bind.call($call, String.prototype.slice);
var $exec = bind.call($call, RegExp.prototype.exec);
/* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */
var rePropName = /[^%.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|%$))/g;
var reEscapeChar = /\\(\\)?/g; /** Used to match backslashes in property paths. */
var stringToPath = function stringToPath(string) {
var first = $strSlice(string, 0, 1);
var last = $strSlice(string, -1);
if (first === '%' && last !== '%') {
throw new $SyntaxError('invalid intrinsic syntax, expected closing `%`');
} else if (last === '%' && first !== '%') {
throw new $SyntaxError('invalid intrinsic syntax, expected opening `%`');
}
var result = [];
$replace(string, rePropName, function (match, number, quote, subString) {
result[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match;
});
return result;
};
/* end adaptation */
var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {
var intrinsicName = name;
var alias;
if (hasOwn(LEGACY_ALIASES, intrinsicName)) {
alias = LEGACY_ALIASES[intrinsicName];
intrinsicName = '%' + alias[0] + '%';
}
if (hasOwn(INTRINSICS, intrinsicName)) {
var value = INTRINSICS[intrinsicName];
if (value === needsEval) {
value = doEval(intrinsicName);
}
if (typeof value === 'undefined' && !allowMissing) {
throw new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');
}
return {
alias: alias,
name: intrinsicName,
value: value
};
}
throw new $SyntaxError('intrinsic ' + name + ' does not exist!');
};
getIntrinsic = function GetIntrinsic(name, allowMissing) {
if (typeof name !== 'string' || name.length === 0) {
throw new $TypeError('intrinsic name must be a non-empty string');
}
if (arguments.length > 1 && typeof allowMissing !== 'boolean') {
throw new $TypeError('"allowMissing" argument must be a boolean');
}
if ($exec(/^%?[^%]*%?$/, name) === null) {
throw new $SyntaxError('`%` may not be present anywhere but at the beginning and end of the intrinsic name');
}
var parts = stringToPath(name);
var intrinsicBaseName = parts.length > 0 ? parts[0] : '';
var intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing);
var intrinsicRealName = intrinsic.name;
var value = intrinsic.value;
var skipFurtherCaching = false;
var alias = intrinsic.alias;
if (alias) {
intrinsicBaseName = alias[0];
$spliceApply(parts, $concat([0, 1], alias));
}
for (var i = 1, isOwn = true; i < parts.length; i += 1) {
var part = parts[i];
var first = $strSlice(part, 0, 1);
var last = $strSlice(part, -1);
if (
(
(first === '"' || first === "'" || first === '`')
|| (last === '"' || last === "'" || last === '`')
)
&& first !== last
) {
throw new $SyntaxError('property names with quotes must have matching quotes');
}
if (part === 'constructor' || !isOwn) {
skipFurtherCaching = true;
}
intrinsicBaseName += '.' + part;
intrinsicRealName = '%' + intrinsicBaseName + '%';
if (hasOwn(INTRINSICS, intrinsicRealName)) {
value = INTRINSICS[intrinsicRealName];
} else if (value != null) {
if (!(part in value)) {
if (!allowMissing) {
throw new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.');
}
return void undefined$1;
}
if ($gOPD && (i + 1) >= parts.length) {
var desc = $gOPD(value, part);
isOwn = !!desc;
// By convention, when a data property is converted to an accessor
// property to emulate a data property that does not suffer from
// the override mistake, that accessor's getter is marked with
// an `originalValue` property. Here, when we detect this, we
// uphold the illusion by pretending to see that original data
// property, i.e., returning the value rather than the getter
// itself.
if (isOwn && 'get' in desc && !('originalValue' in desc.get)) {
value = desc.get;
} else {
value = value[part];
}
} else {
isOwn = hasOwn(value, part);
value = value[part];
}
if (isOwn && !skipFurtherCaching) {
INTRINSICS[intrinsicRealName] = value;
}
}
}
return value;
};
return getIntrinsic;
}
var callBound;
var hasRequiredCallBound;
function requireCallBound () {
if (hasRequiredCallBound) return callBound;
hasRequiredCallBound = 1;
'use strict';
var GetIntrinsic = /*@__PURE__*/ requireGetIntrinsic();
var callBindBasic = requireCallBindApplyHelpers();
/** @type {(thisArg: string, searchString: string, position?: number) => number} */
var $indexOf = callBindBasic([GetIntrinsic('%String.prototype.indexOf%')]);
/** @type {import('.')} */
callBound = function callBoundIntrinsic(name, allowMissing) {
/* eslint no-extra-parens: 0 */
var intrinsic = /** @type {(this: unknown, ...args: unknown[]) => unknown} */ (GetIntrinsic(name, !!allowMissing));
if (typeof intrinsic === 'function' && $indexOf(name, '.prototype.') > -1) {
return callBindBasic(/** @type {const} */ ([intrinsic]));
}
return intrinsic;
};
return callBound;
}
var implementation;
var hasRequiredImplementation;
function requireImplementation () {
if (hasRequiredImplementation) return implementation;
hasRequiredImplementation = 1;
'use strict';
// modified from https://github.com/es-shims/es6-shim
var objectKeys = requireObjectKeys();
var hasSymbols = requireShams()();
var callBound = /*@__PURE__*/ requireCallBound();
var $Object = /*@__PURE__*/ requireEsObjectAtoms();
var $push = callBound('Array.prototype.push');
var $propIsEnumerable = callBound('Object.prototype.propertyIsEnumerable');
var originalGetSymbols = hasSymbols ? $Object.getOwnPropertySymbols : null;
// eslint-disable-next-line no-unused-vars
implementation = function assign(target, source1) {
if (target == null) { throw new TypeError('target must be an object'); }
var to = $Object(target); // step 1
if (arguments.length === 1) {
return to; // step 2
}
for (var s = 1; s < arguments.length; ++s) {
var from = $Object(arguments[s]); // step 3.a.i
// step 3.a.ii:
var keys = objectKeys(from);
var getSymbols = hasSymbols && ($Object.getOwnPropertySymbols || originalGetSymbols);
if (getSymbols) {
var syms = getSymbols(from);
for (var j = 0; j < syms.length; ++j) {
var key = syms[j];
if ($propIsEnumerable(from, key)) {
$push(keys, key);
}
}
}
// step 3.a.iii:
for (var i = 0; i < keys.length; ++i) {
var nextKey = keys[i];
if ($propIsEnumerable(from, nextKey)) { // step 3.a.iii.2
var propValue = from[nextKey]; // step 3.a.iii.2.a
to[nextKey] = propValue; // step 3.a.iii.2.b
}
}
}
return to; // step 4
};
return implementation;
}
var polyfill;
var hasRequiredPolyfill;
function requirePolyfill () {
if (hasRequiredPolyfill) return polyfill;
hasRequiredPolyfill = 1;
'use strict';
var implementation = requireImplementation();
var lacksProperEnumerationOrder = function () {
if (!Object.assign) {
return false;
}
/*
* v8, specifically in node 4.x, has a bug with incorrect property enumeration order
* note: this does not detect the bug unless there's 20 characters
*/
var str = 'abcdefghijklmnopqrst';
var letters = str.split('');
var map = {};
for (var i = 0; i < letters.length; ++i) {
map[letters[i]] = letters[i];
}
var obj = Object.assign({}, map);
var actual = '';
for (var k in obj) {
actual += k;
}
return str !== actual;
};
var assignHasPendingExceptions = function () {
if (!Object.assign || !Object.preventExtensions) {
return false;
}
/*
* Firefox 37 still has "pending exception" logic in its Object.assign implementation,
* which is 72% slower than our shim, and Firefox 40's native implementation.
*/
var thrower = Object.preventExtensions({ 1: 2 });
try {
Object.assign(thrower, 'xy');
} catch (e) {
return thrower[1] === 'y';
}
return false;
};
polyfill = function getPolyfill() {
if (!Object.assign) {
return implementation;
}
if (lacksProperEnumerationOrder()) {
return implementation;
}
if (assignHasPendingExceptions()) {
return implementation;
}
return Object.assign;
};
return polyfill;
}
var util = {};
var isBuffer;
var hasRequiredIsBuffer;
function requireIsBuffer () {
if (hasRequiredIsBuffer) return isBuffer;
hasRequiredIsBuffer = 1;
isBuffer = function isBuffer(arg) {
return arg instanceof Buffer;
};
return isBuffer;
}
var inherits_browser$1 = {exports: {}};
var inherits_browser = inherits_browser$1.exports;
var hasRequiredInherits_browser;
function requireInherits_browser () {
if (hasRequiredInherits_browser) return inherits_browser$1.exports;
hasRequiredInherits_browser = 1;
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
inherits_browser$1.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
inherits_browser$1.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
var TempCtor = function () {};
TempCtor.prototype = superCtor.prototype;
ctor.prototype = new TempCtor();
ctor.prototype.constructor = ctor;
};
}
return inherits_browser$1.exports;
}
var hasRequiredUtil;
function requireUtil () {
if (hasRequiredUtil) return util;
hasRequiredUtil = 1;
(function (exports$1) {
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var formatRegExp = /%[sdj%]/g;
exports$1.format = function(f) {
if (!isString(f)) {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(inspect(arguments[i]));
}
return objects.join(' ');
}
var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j':
try {
return JSON.stringify(args[i++]);
} catch (_) {
return '[Circular]';
}
default:
return x;
}
});
for (var x = args[i]; i < len; x = args[++i]) {
if (isNull(x) || !isObject(x)) {
str += ' ' + x;
} else {
str += ' ' + inspect(x);
}
}
return str;
};
// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports$1.deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (isUndefined(global.process)) {
return function() {
return exports$1.deprecate(fn, msg).apply(this, arguments);
};
}
if (process.noDeprecation === true) {
return fn;
}
var warned = false;
function deprecated() {
if (!warned) {
if (process.throwDeprecation) {
throw new Error(msg);
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
}
warned = true;
}
return fn.apply(this, arguments);
}
return deprecated;
};
var debugs = {};
var debugEnviron;
exports$1.debuglog = function(set) {
if (isUndefined(debugEnviron))
debugEnviron = process.env.NODE_DEBUG || '';
set = set.toUpperCase();
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports$1.format.apply(exports$1, arguments);
console.error('%s %d: %s', set, pid, msg);
};
} else {
debugs[set] = function() {};
}
}
return debugs[set];
};
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
*
* @param {Object} obj The object to print out.
* @param {Object} opts Optional options object that alters the output.
*/
/* legacy: obj, showHidden, depth, colors*/
function inspect(obj, opts) {
// default options
var ctx = {
seen: [],
stylize: stylizeNoColor
};
// legacy...
if (arguments.length >= 3) ctx.depth = arguments[2];
if (arguments.length >= 4) ctx.colors = arguments[3];
if (isBoolean(opts)) {
// legacy...
ctx.showHidden = opts;
} else if (opts) {
// got an "options" object
exports$1._extend(ctx, opts);
}
// set default options
if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
if (isUndefined(ctx.depth)) ctx.depth = 2;
if (isUndefined(ctx.colors)) ctx.colors = false;
if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
if (ctx.colors) ctx.stylize = stylizeWithColor;
return formatValue(ctx, obj, ctx.depth);
}
exports$1.inspect = inspect;
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39]
};
// Don't use 'blue' not visible on cmd.exe
inspect.styles = {
'special': 'cyan',
'number': 'yellow',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'date': 'magenta',
// "name": intentionally not styling
'regexp': 'red'
};
function stylizeWithColor(str, styleType) {
var style = inspect.styles[styleType];
if (style) {
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
}
function stylizeNoColor(str, styleType) {
return str;
}
function arrayToHash(array) {
var hash = {};
array.forEach(function(val, idx) {
hash[val] = true;
});
return hash;
}
function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports$1.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
var ret = value.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
}
return ret;
}
// Primitive types cannot have properties
var primitive = formatPrimitive(ctx, value);
if (primitive) {
return primitive;
}
// Look up the keys of the object.
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value);
}
// IE doesn't make error fields non-enumerable
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
if (isError(value)
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
return formatError(value);
}
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (isFunction(value)) {
var name = value.name ? ': ' + value.name : '';
return ctx.stylize('[Function' + name + ']', 'special');
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), 'date');
}
if (isError(value)) {
return formatError(value);
}
}
var base = '', array = false, braces = ['{', '}'];
// Make Array say that they are Array
if (isArray(value)) {
array = true;
braces = ['[', ']'];
}
// Make functions say that they are functions
if (isFunction(value)) {
var n = value.name ? ': ' + value.name : '';
base = ' [Function' + n + ']';
}
// Make RegExps say that they are RegExps
if (isRegExp(value)) {
base = ' ' + RegExp.prototype.toString.call(value);
}
// Make dates with properties first say the date
if (isDate(value)) {
base = ' ' + Date.prototype.toUTCString.call(value);
}
// Make error with message first say the error
if (isError(value)) {
base = ' ' + formatError(value);
}
if (keys.length === 0 && (!array || value.length == 0)) {
return braces[0] + base + braces[1];
}
if (recurseTimes < 0) {
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
} else {
return ctx.stylize('[Object]', 'special');
}
}
ctx.seen.push(value);
var output;
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
});
}
ctx.seen.pop();
return reduceToSingleString(output, base, braces);
}
function formatPrimitive(ctx, value) {
if (isUndefined(value))
return ctx.stylize('undefined', 'undefined');
if (isString(value)) {
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
return ctx.stylize(simple, 'string');
}
if (isNumber(value))
return ctx.stylize('' + value, 'number');
if (isBoolean(value))
return ctx.stylize('' + value, 'boolean');
// For some reason typeof null is "object", so special case here.
if (isNull(value))
return ctx.stylize('null', 'null');
}
function formatError(value) {
return '[' + Error.prototype.toString.call(value) + ']';
}
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
}
}
keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
}
});
return output;
}
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
var name, str, desc;
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
if (desc.get) {
if (desc.set) {
str = ctx.stylize('[Getter/Setter]', 'special');
} else {
str = ctx.stylize('[Getter]', 'special');
}
} else {
if (desc.set) {
str = ctx.stylize('[Setter]', 'special');
}
}
if (!hasOwnProperty(visibleKeys, key)) {
name = '[' + key + ']';
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
if (isNull(recurseTimes)) {
str = formatValue(ctx, desc.value, null);
} else {
str = formatValue(ctx, desc.value, recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (array) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
} else {
str = '\n' + str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n');
}
}
} else {
str = ctx.stylize('[Circular]', 'special');
}
}
if (isUndefined(name)) {
if (array && key.match(/^\d+$/)) {
return str;
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name');
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
name = ctx.stylize(name, 'string');
}
}
return name + ': ' + str;
}
function reduceToSingleString(output, base, braces) {
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) {
numLinesEst++;
if (cur.indexOf('\n') >= 0) numLinesEst++;
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
}, 0);
if (length > 60) {
return braces[0] +
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
}
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
}
// NOTE: These type checking functions intentionally don't use `instanceof`
// because it is fragile and can be easily faked with `Object.create()`.
function isArray(ar) {
return Array.isArray(ar);
}
exports$1.isArray = isArray;
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports$1.isBoolean = isBoolean;
function isNull(arg) {
return arg === null;
}
exports$1.isNull = isNull;
function isNullOrUndefined(arg) {
return arg == null;
}
exports$1.isNullOrUndefined = isNullOrUndefined;
function isNumber(arg) {
return typeof arg === 'number';
}
exports$1.isNumber = isNumber;
function isString(arg) {
return typeof arg === 'string';
}
exports$1.isString = isString;
function isSymbol(arg) {
return typeof arg === 'symbol';
}
exports$1.isSymbol = isSymbol;
function isUndefined(arg) {
return arg === void 0;
}
exports$1.isUndefined = isUndefined;
function isRegExp(re) {
return isObject(re) && objectToString(re) === '[object RegExp]';
}
exports$1.isRegExp = isRegExp;
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
exports$1.isObject = isObject;
function isDate(d) {
return isObject(d) && objectToString(d) === '[object Date]';
}
exports$1.isDate = isDate;
function isError(e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error);
}
exports$1.isError = isError;
function isFunction(arg) {
return typeof arg === 'function';
}
exports$1.isFunction = isFunction;
function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
}
exports$1.isPrimitive = isPrimitive;
exports$1.isBuffer = requireIsBuffer();
function objectToString(o) {
return Object.prototype.toString.call(o);
}
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}
// log is just a thin wrapper to console.log that prepends a timestamp
exports$1.log = function() {
console.log('%s - %s', timestamp(), exports$1.format.apply(exports$1, arguments));
};
/**
* Inherit the prototype methods from one constructor into another.
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be rewritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
*/
exports$1.inherits = requireInherits_browser();
exports$1._extend = function(origin, add) {
// Don't do anything if add isn't an object
if (!add || !isObject(add)) return origin;
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
origin[keys[i]] = add[keys[i]];
}
return origin;
};
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
} (util));
return util;
}
var assert_1 = assert$2.exports;
var hasRequiredAssert;
function requireAssert () {
if (hasRequiredAssert) return assert$2.exports;
hasRequiredAssert = 1;
'use strict';
var objectAssign = requirePolyfill()();
// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js
// original notice:
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh
* @license MIT
*/
function compare(a, b) {
if (a === b) {
return 0;
}
var x = a.length;
var y = b.length;
for (var i = 0, len = Math.min(x, y); i < len; ++i) {
if (a[i] !== b[i]) {
x = a[i];
y = b[i];
break;
}
}
if (x < y) {
return -1;
}
if (y < x) {
return 1;
}
return 0;
}
function isBuffer(b) {
if (global.Buffer && typeof global.Buffer.isBuffer === 'function') {
return global.Buffer.isBuffer(b);
}
return !!(b != null && b._isBuffer);
}
// based on node assert, original notice:
// NB: The URL to the CommonJS spec is kept just for tradition.
// node-assert has evolved a lot since then, both in API and behavior.
// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8!
//
// Originally from narwhal.js (http://narwhaljs.org)
// Copyright (c) 2009 Thomas Robinson <280north.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var util = requireUtil();
var hasOwn = Object.prototype.hasOwnProperty;
var pSlice = Array.prototype.slice;
var functionsHaveNames = (function () {
return function foo() {}.name === 'foo';
}());
function pToString (obj) {
return Object.prototype.toString.call(obj);
}
function isView(arrbuf) {
if (isBuffer(arrbuf)) {
return false;
}
if (typeof global.ArrayBuffer !== 'function') {
return false;
}
if (typeof ArrayBuffer.isView === 'function') {
return ArrayBuffer.isView(arrbuf);
}
if (!arrbuf) {
return false;
}
if (arrbuf instanceof DataView) {
return true;
}
if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) {
return true;
}
return false;
}
// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.
var assert = assert$2.exports = ok;
// 2. The AssertionError is defined in assert.
// new assert.AssertionError({ message: message,
// actual: actual,
// expected: expected })
var regex = /\s*function\s+([^\(\s]*)\s*/;
// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js
function getName(func) {
if (!util.isFunction(func)) {
return;
}
if (functionsHaveNames) {
return func.name;
}
var str = func.toString();
var match = str.match(regex);
return match && match[1];
}
assert.AssertionError = function AssertionError(options) {
this.name = 'AssertionError';
this.actual = options.actual;
this.expected = options.expected;
this.operator = options.operator;
if (options.message) {
this.message = options.message;
this.generatedMessage = false;
} else {
this.message = getMessage(this);
this.generatedMessage = true;
}
var stackStartFunction = options.stackStartFunction || fail;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, stackStartFunction);
} else {
// non v8 browsers so we can have a stacktrace
var err = new Error();
if (err.stack) {
var out = err.stack;
// try to strip useless frames
var fn_name = getName(stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
// we need to strip out everything before it (and its line)
var next_line = out.indexOf('\n', idx + 1);
out = out.substring(next_line + 1);
}
this.stack = out;
}
}
};
// assert.AssertionError instanceof Error
util.inherits(assert.AssertionError, Error);
function truncate(s, n) {
if (typeof s === 'string') {
return s.length < n ? s : s.slice(0, n);
} else {
return s;
}
}
function inspect(something) {
if (functionsHaveNames || !util.isFunction(something)) {
return util.inspect(something);
}
var rawname = getName(something);
var name = rawname ? ': ' + rawname : '';
return '[Function' + name + ']';
}
function getMessage(self) {
return truncate(inspect(self.actual), 128) + ' ' +
self.operator + ' ' +
truncate(inspect(self.expected), 128);
}
// At present only the three keys mentioned above are used and
// understood by the spec. Implementations or sub modules can pass
// other keys to the AssertionError's constructor - they will be
// ignored.
// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
// may be undefined if not provided. All assertion methods provide
// both the actual and expected values to the assertion error for
// display purposes.
function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
// EXTENSION! allows for well behaved errors defined elsewhere.
assert.fail = fail;
// 4. Pure assertion tests whether a value is truthy, as determined
// by !!guard.
// assert.ok(guard, message_opt);
// This statement is equivalent to assert.equal(true, !!guard,
// message_opt);. To test strictly for the value true, use
// assert.strictEqual(true, guard, message_opt);.
function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};
// 6. The non-equality assertion tests for whether two objects are not equal
// with != assert.notEqual(actual, expected, message_opt);
assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual);
}
};
// 7. The equivalence assertion tests a deep equality relation.
// assert.deepEqual(actual, expected, message_opt);
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected, false)) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) {
if (!_deepEqual(actual, expected, true)) {
fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual);
}
};
function _deepEqual(actual, expected, strict, memos) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if (isBuffer(actual) && isBuffer(expected)) {
return compare(actual, expected) === 0;
// 7.2. If the expected value is a Date object, the actual value is
// equivalent if it is also a Date object that refers to the same time.
} else if (util.isDate(actual) && util.isDate(expected)) {
return actual.getTime() === expected.getTime();
// 7.3 If the expected value is a RegExp object, the actual value is
// equivalent if it is also a RegExp object with the same source and
// properties (`global`, `multiline`, `lastIndex`, `ignoreCase`).
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase;
// 7.4. Other pairs that do not both pass typeof value == 'object',
// equivalence is determined by ==.
} else if ((actual === null || typeof actual !== 'object') &&
(expected === null || typeof expected !== 'object')) {
return strict ? actual === expected : actual == expected;
// If both values are instances of typed arrays, wrap their underlying
// ArrayBuffers in a Buffer each to increase performance
// This optimization requires the arrays to have the same type as checked by
// Object.prototype.toString (aka pToString). Never perform binary
// comparisons for Float*Arrays, though, since e.g. +0 === -0 but their
// bit patterns are not identical.
} else if (isView(actual) && isView(expected) &&
pToString(actual) === pToString(expected) &&
!(actual instanceof Float32Array ||
actual instanceof Float64Array)) {
return compare(new Uint8Array(actual.buffer),
new Uint8Array(expected.buffer)) === 0;
// 7.5 For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical 'prototype' property. Note: this
// accounts for both named and indexed properties on Arrays.
} else if (isBuffer(actual) !== isBuffer(expected)) {
return false;
} else {
memos = memos || {actual: [], expected: []};
var actualIndex = memos.actual.indexOf(actual);
if (actualIndex !== -1) {
if (actualIndex === memos.expected.indexOf(expected)) {
return true;
}
}
memos.actual.push(actual);
memos.expected.push(expected);
return objEquiv(actual, expected, strict, memos);
}
}
function isArguments(object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
function objEquiv(a, b, strict, actualVisitedObjects) {
if (a === null || a === undefined || b === null || b === undefined)
return false;
// if one is a primitive, the other must be same
if (util.isPrimitive(a) || util.isPrimitive(b))
return a === b;
if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b))
return false;
var aIsArgs = isArguments(a);
var bIsArgs = isArguments(b);
if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs))
return false;
if (aIsArgs) {
a = pSlice.call(a);
b = pSlice.call(b);
return _deepEqual(a, b, strict);
}
var ka = objectKeys(a);
var kb = objectKeys(b);
var key, i;
// having the same number of owned properties (keys incorporates
// hasOwnProperty)
if (ka.length !== kb.length)
return false;
//the same set of keys (although not necessarily the same order),
ka.sort();
kb.sort();
//~~~cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] !== kb[i])
return false;
}
//equivalent values for every corresponding key, and
//~~~possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects))
return false;
}
return true;
}
// 8. The non-equivalence assertion tests for any deep inequality.
// assert.notDeepEqual(actual, expected, message_opt);
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
if (_deepEqual(actual, expected, false)) {
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
}
};
assert.notDeepStrictEqual = notDeepStrictEqual;
function notDeepStrictEqual(actual, expected, message) {
if (_deepEqual(actual, expected, true)) {
fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
}
}
// 9. The strict equality assertion tests strict equality, as determined by ===.
// assert.strictEqual(actual, expected, message_opt);
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
fail(actual, expected, message, '===', assert.strictEqual);
}
};
// 10. The strict non-equality assertion tests for strict inequality, as
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
fail(actual, expected, message, '!==', assert.notStrictEqual);
}
};
function expectedException(actual, expected) {
if (!actual || !expected) {
return false;
}
if (Object.prototype.toString.call(expected) == '[object RegExp]') {
return expected.test(actual);
}
try {
if (actual instanceof expected) {
return true;
}
} catch (e) {
// Ignore. The instanceof check doesn't work for arrow functions.
}
if (Error.isPrototypeOf(expected)) {
return false;
}
return expected.call({}, actual) === true;
}
function _tryBlock(block) {
var error;
try {
block();
} catch (e) {
error = e;
}
return error;
}
function _throws(shouldThrow, block, expected, message) {
var actual;
if (typeof block !== 'function') {
throw new TypeError('"block" argument must be a function');
}
if (typeof expected === 'string') {
message = expected;
expected = null;
}
actual = _tryBlock(block);
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
(message ? ' ' + message : '.');
if (shouldThrow && !actual) {
fail(actual, expected, 'Missing expected exception' + message);
}
var userProvidedMessage = typeof message === 'string';
var isUnwantedException = !shouldThrow && util.isError(actual);
var isUnexpectedException = !shouldThrow && actual && !expected;
if ((isUnwantedException &&
userProvidedMessage &&
expectedException(actual, expected)) ||
isUnexpectedException) {
fail(actual, expected, 'Got unwanted exception' + message);
}
if ((shouldThrow && actual && expected &&
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
throw actual;
}
}
// 11. Expected to throw an error:
// assert.throws(block, Error_opt, message_opt);
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws(true, block, error, message);
};
// EXTENSION! This is annoying to write outside this module.
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
_throws(false, block, error, message);
};
assert.ifError = function(err) { if (err) throw err; };
// Expose a strict only variant of assert
function strict(value, message) {
if (!value) fail(value, true, message, '==', strict);
}
assert.strict = objectAssign(strict, assert, {
equal: assert.strictEqual,
deepEqual: assert.deepStrictEqual,
notEqual: assert.notStrictEqual,
notDeepEqual: assert.notDeepStrictEqual
});
assert.strict.strict = assert.strict;
var objectKeys = Object.keys || function (obj) {
var keys = [];
for (var key in obj) {
if (hasOwn.call(obj, key)) keys.push(key);
}
return keys;
};
return assert$2.exports;
}
var assertExports = requireAssert();
var assert$1 = /*@__PURE__*/getDefaultExportFromCjs(assertExports);
function deepEqual(a, b) {
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
}
if (typeof a === "object" && a !== null && b !== null) {
if (!(typeof b === "object")) return false;
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
for (const key in a) {
if (!deepEqual(a[key], b[key])) return false;
}
return true;
}
return a === b;
}
const DEG_TO_RAD = Math.PI / 180;
const RAD_TO_DEG = 180 / Math.PI;
function degToRad(a) {
return a * DEG_TO_RAD;
}
function radToDeg(a) {
return a * RAD_TO_DEG;
}
const TILE_CORNERS = [[0, 0], [1, 0], [1, 1], [0, 1]];
function furthestTileCorner(bearing) {
const alignedBearing = (bearing + 45 + 360) % 360;
const cornerIdx = Math.round(alignedBearing / 90) % 4;
return TILE_CORNERS[cornerIdx];
}
function easeCubicInOut(t) {
if (t <= 0) return 0;
if (t >= 1) return 1;
const t2 = t * t, t3 = t2 * t;
return 4 * (t < 0.5 ? t3 : 3 * (t - t2) + t3 - 0.75);
}
function getBounds(points) {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const p of points) {
minX = Math.min(minX, p.x);
minY = Math.min(minY, p.y);
maxX = Math.max(maxX, p.x);
maxY = Math.max(maxY, p.y);
}
return {
min: new Point(minX, minY),
max: new Point(maxX, maxY)
};
}
function getAABBPointSquareDist(min, max, point) {
let sqDist = 0;
for (let i = 0; i < 2; ++i) {
const v = point ? point[i] : 0;
assert$1(min[i] < max[i], "Invalid aabb min and max inputs, min[i] must be < max[i].");
if (min[i] > v) sqDist += (min[i] - v) * (min[i] - v);
if (max[i] < v) sqDist += (v - max[i]) * (v - max[i]);
}
return sqDist;
}
function polygonizeBounds(min, max, buffer = 0, close = true) {
const offset = new Point(buffer, buffer);
const minBuf = min.sub(offset);
const maxBuf = max.add(offset);
const polygon = [minBuf, new Point(maxBuf.x, minBuf.y), maxBuf, new Point(minBuf.x, maxBuf.y)];
if (close) {
polygon.push(minBuf.clone());
}
return polygon;
}
function bufferConvexPolygon(ring, buffer) {
assert$1(ring.length > 2, "bufferConvexPolygon requires the ring to have atleast 3 points");
const output = [];
for (let currIdx = 0; currIdx < ring.length; currIdx++) {
const prevIdx = wrap$1(currIdx - 1, -1, ring.length - 1);
const nextIdx = wrap$1(currIdx + 1, -1, ring.length - 1);
const prev = ring[prevIdx];
const curr = ring[currIdx];
const next = ring[nextIdx];
const p1 = prev.sub(curr).unit();
const p2 = next.sub(curr).unit();
const interiorAngle = p2.angleWithSep(p1.x, p1.y);
const offset = p1.add(p2).unit().mult(-1 * buffer / Math.sin(interiorAngle / 2));
output.push(curr.add(offset));
}
return output;
}
function bezier(p1x, p1y, p2x, p2y) {
const bezier2 = new UnitBezier(p1x, p1y, p2x, p2y);
return function(t) {
return bezier2.solve(t);
};
}
const ease = bezier(0.25, 0.1, 0.25, 1);
function clamp(n, min, max) {
return Math.min(max, Math.max(min, n));
}
function smoothstep(e0, e1, x) {
x = clamp((x - e0) / (e1 - e0), 0, 1);
return x * x * (3 - 2 * x);
}
function wrap$1(n, min, max) {
const d = max - min;
const w = ((n - min) % d + d) % d + min;
return w === min ? max : w;
}
function shortestAngle(a, b) {
const diff = (b - a + 180) % 360 - 180;
return diff < -180 ? diff + 360 : diff;
}
function asyncAll(array, fn, callback) {
if (!array.length) {
return callback(null, []);
}
let remaining = array.length;
const results = new Array(array.length);
let error = null;
array.forEach((item, i) => {
fn(item, (err, result) => {
if (err) error = err;
results[i] = result;
if (--remaining === 0) callback(error, results);
});
});
}
function keysDifference(obj, other) {
const difference = [];
for (const i in obj) {
if (!(i in other)) {
difference.push(i);
}
}
return difference;
}
function pick(src, properties) {
const result = {};
for (let i = 0; i < properties.length; i++) {
const k = properties[i];
if (k in src) {
result[k] = src[k];
}
}
return result;
}
let id = 1;
function uniqueId() {
return id++;
}
function uuid() {
function b(a) {
return a ? (a ^ Math.random() * (16 >> a / 4)).toString(16) : (
// @ts-expect-error - TS2365 - Operator '+' cannot be applied to types 'number[]' and 'number'.
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands, @typescript-eslint/no-unsafe-unary-minus, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
([1e7] + -[1e3] + -4e3 + -8e3 + -1e11).replace(/[018]/g, b)
);
}
return b();
}
function isPowerOfTwo(value) {
return Math.log2(value) % 1 === 0;
}
function nextPowerOfTwo(value) {
if (value <= 1) return 1;
return Math.pow(2, Math.ceil(Math.log2(value)));
}
function prevPowerOfTwo(value) {
if (value <= 1) return 1;
return Math.pow(2, Math.floor(Math.log2(value)));
}
function validateUuid(str) {
return str ? /^[0-9a-f]{8}-[0-9a-f]{4}-[4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(str) : false;
}
function bindAll(fns, context) {
fns.forEach((fn) => {
if (!context[fn]) {
return;
}
context[fn] = context[fn].bind(context);
});
}
function mapObject(input, iterator, context) {
const output = {};
for (const key in input) {
output[key] = iterator.call(context || this, input[key], key, input);
}
return output;
}
function filterObject(input, iterator, context) {
const output = {};
for (const key in input) {
if (iterator.call(context || this, input[key], key, input)) {
output[key] = input[key];
}
}
return output;
}
function clone(input) {
if (Array.isArray(input)) {
return input.map(clone);
} else if (typeof input === "object" && input) {
return mapObject(input, clone);
} else {
return input;
}
}
function mapValue(value, min, max, outMin, outMax) {
return clamp((value - min) / (max - min) * (outMax - outMin) + outMin, outMin, outMax);
}
function arraysIntersect(a, b) {
for (let l = 0; l < a.length; l++) {
if (b.indexOf(a[l]) >= 0) return true;
}
return false;
}
const warnOnceHistory = {};
function warnOnce(message) {
if (!warnOnceHistory[message]) {
if (typeof console !== "undefined") console.warn(message);
warnOnceHistory[message] = true;
}
}
function isCounterClockwise(a, b, c) {
return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x);
}
function calculateSignedArea$1(ring) {
let sum = 0;
for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
p1 = ring[i];
p2 = ring[j];
sum += (p2.x - p1.x) * (p1.y + p2.y);
}
return sum;
}
function sphericalPositionToCartesian([r, azimuthal, polar]) {
const a = degToRad(azimuthal + 90), p = degToRad(polar);
return {
x: r * Math.cos(a) * Math.sin(p),
y: r * Math.sin(a) * Math.sin(p),
z: r * Math.cos(p),
azimuthal,
polar
};
}
function sphericalDirectionToCartesian([azimuthal, polar]) {
const position = sphericalPositionToCartesian([1, azimuthal, polar]);
return {
x: position.x,
y: position.y,
z: position.z
};
}
function cartesianPositionToSpherical(x, y, z) {
const radial = Math.sqrt(x * x + y * y + z * z);
const polar = radial > 0 ? Math.acos(z / radial) * RAD_TO_DEG : 0;
let azimuthal = x !== 0 || y !== 0 ? Math.atan2(-y, -x) * RAD_TO_DEG + 90 : 0;
if (azimuthal < 0) {
azimuthal += 360;
}
return [radial, azimuthal, polar];
}
function isWorker(scope) {
if (typeof self === "undefined" && scope === void 0) {
return false;
}
if (typeof WorkerGlobalScope === "undefined") {
return false;
}
const contextToCheck = scope !== void 0 ? scope : self;
return contextToCheck instanceof WorkerGlobalScope;
}
function parseCacheControl(cacheControl) {
const re = /(?:^|(?:\s*\,\s*))([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)(?:\=(?:([^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)|(?:\"((?:[^"\\]|\\.)*)\")))?/g;
const header = {};
cacheControl.replace(re, ($0, $1, $2, $3) => {
const value = $2 || $3;
header[$1] = value ? value.toLowerCase() : true;
return "";
});
if (header["max-age"]) {
const maxAge = parseInt(header["max-age"], 10);
if (isNaN(maxAge)) delete header["max-age"];
else header["max-age"] = maxAge;
}
return header;
}
function getExpiryDataFromHeaders(responseHeaders) {
if (!responseHeaders) return { cacheControl: void 0, expires: void 0 };
const cacheControl = responseHeaders.get("Cache-Control");
const expires = responseHeaders.get("Expires");
return { cacheControl, expires };
}
let _isSafari = null;
function _resetSafariCheckForTest() {
_isSafari = null;
}
function isSafari(scope) {
if (_isSafari == null) {
const userAgent = scope.navigator ? scope.navigator.userAgent : null;
_isSafari = !!scope.safari || !!(userAgent && (/\b(iPad|iPhone|iPod)\b/.test(userAgent) || !!userAgent.match("Safari") && !userAgent.match("Chrome")));
}
return _isSafari;
}
function isSafariWithAntialiasingBug(scope) {
const userAgent = scope.navigator ? scope.navigator.userAgent : null;
if (!isSafari(scope)) return false;
return !!(userAgent && (userAgent.match("Version/15.4") || userAgent.match("Version/15.5") || userAgent.match(/CPU (OS|iPhone OS) (15_4|15_5) like Mac OS X/)));
}
function isFullscreen() {
return !!document.fullscreenElement || !!document.webkitFullscreenElement;
}
function storageAvailable(type) {
try {
const storage = self[type];
storage.setItem("_mapbox_test_", 1);
storage.removeItem("_mapbox_test_");
return true;
} catch (e) {
return false;
}
}
function b64EncodeUnicode(str) {
return btoa(
encodeURIComponent(str).replace(
/%([0-9A-F]{2})/g,
(match, p1) => {
return String.fromCharCode(Number("0x" + p1));
}
)
);
}
function b64DecodeUnicode(str) {
return decodeURIComponent(atob(str).split("").map((c) => {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
}).join(""));
}
function base64DecToArr(sBase64) {
const str = atob(sBase64);
const arr = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) arr[i] = str.codePointAt(i);
return arr;
}
function getColumn(matrix, col) {
return [matrix[col * 4], matrix[col * 4 + 1], matrix[col * 4 + 2], matrix[col * 4 + 3]];
}
function setColumn(matrix, col, values) {
matrix[col * 4 + 0] = values[0];
matrix[col * 4 + 1] = values[1];
matrix[col * 4 + 2] = values[2];
matrix[col * 4 + 3] = values[3];
}
function sRGBToLinearAndScale(v, s) {
return [
Math.pow(v[0], 2.2) * s,
Math.pow(v[1], 2.2) * s,
Math.pow(v[2], 2.2) * s
];
}
function linearVec3TosRGB(v) {
return [
Math.pow(v[0], 1 / 2.2),
Math.pow(v[1], 1 / 2.2),
Math.pow(v[2], 1 / 2.2)
];
}
function lowerBound(array, startIndex, finishIndex, target) {
while (startIndex < finishIndex) {
const middleIndex = startIndex + finishIndex >> 1;
if (array[middleIndex] < target) {
startIndex = middleIndex + 1;
} else {
finishIndex = middleIndex;
}
}
return startIndex;
}
function upperBound(array, startIndex, finishIndex, target) {
while (startIndex < finishIndex) {
const middleIndex = startIndex + finishIndex >> 1;
if (array[middleIndex] <= target) {
startIndex = middleIndex + 1;
} else {
finishIndex = middleIndex;
}
}
return startIndex;
}
function contrastFactor(contrast) {
return contrast > 0 ? 1 / (1.001 - contrast) : 1 + contrast;
}
function saturationFactor(saturation) {
return saturation > 0 ? 1 - 1 / (1.001 - saturation) : -saturation;
}
function computeColorAdjustmentMatrix(saturation, contrast, brightnessMin, brightnessMax) {
saturation = saturationFactor(saturation);
contrast = contrastFactor(contrast);
const m = create$5();
const sa = saturation / 3;
const sb = 1 - 2 * sa;
const saturationMatrix = [
sb,
sa,
sa,
0,
sa,
sb,
sa,
0,
sa,
sa,
sb,
0,
0,
0,
0,
1
];
const cs = 0.5 - 0.5 * contrast;
const contrastMatrix = [
contrast,
0,
0,
0,
0,
contrast,
0,
0,
0,
0,
contrast,
0,
cs,
cs,
cs,
1
];
const hl = brightnessMax - brightnessMin;
const brightnessMatrix = [
hl,
0,
0,
0,
0,
hl,
0,
0,
0,
0,
hl,
0,
brightnessMin,
brightnessMin,
brightnessMin,
1
];
multiply$5(m, brightnessMatrix, contrastMatrix);
multiply$5(m, m, saturationMatrix);
return m;
}
function mapRangeValue(value, from, to) {
return (value - from.min) * (to.max - to.min) / (from.max - from.min) + to.min;
}
function mapRange(range, from, to) {
return { min: mapRangeValue(range.min, from, to), max: mapRangeValue(range.max, from, to) };
}
function easeIn(x) {
return x * x * x * x * x;
}
var name = "mapbox-gl";
var description = "A WebGL interactive maps library";
var version = "3.17.0";
var main = "dist/mapbox-gl.js";
var style = "dist/mapbox-gl.css";
var types$2 = "dist/mapbox-gl.d.ts";
var license = "SEE LICENSE IN LICENSE.txt";
var type = "module";
var repository = {
type: "git",
url: "git://github.com/mapbox/mapbox-gl-js.git"
};
var workspaces = [
"src/style-spec",
"test/build/vite",
"test/build/webpack",
"test/build/typings"
];
var dependencies = {
"@mapbox/jsonlint-lines-primitives": "^2.0.2",
"@mapbox/mapbox-gl-supported": "^3.0.0",
"@mapbox/point-geometry": "^1.1.0",
"@mapbox/tiny-sdf": "^2.0.6",
"@mapbox/unitbezier": "^0.0.1",
"@mapbox/vector-tile": "^2.0.4",
"@mapbox/whoots-js": "^3.1.0",
"@types/geojson": "^7946.0.16",
"@types/geojson-vt": "^3.2.5",
"@types/mapbox__point-geometry": "^0.1.4",
"@types/pbf": "^3.0.5",
"@types/supercluster": "^7.1.3",
"cheap-ruler": "^4.0.0",
csscolorparser: "~1.0.3",
earcut: "^3.0.1",
"geojson-vt": "^4.0.2",
"gl-matrix": "^3.4.4",
"grid-index": "^1.1.0",
kdbush: "^4.0.2",
"martinez-polygon-clipping": "^0.7.4",
"murmurhash-js": "^1.0.0",
pbf: "^4.0.1",
potpack: "^2.0.0",
quickselect: "^3.0.0",
supercluster: "^8.0.1",
tinyqueue: "^3.0.0"
};
var devDependencies = {
"@eslint/compat": "^2.0.0",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.30.1",
"@mapbox/mvt-fixtures": "^3.10.0",
"@octokit/rest": "^22.0.0",
"@rollup/plugin-commonjs": "^29.0.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-strip": "^3.0.4",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-virtual": "^3.0.2",
"@size-limit/file": "^11.2.0",
"@tweakpane/core": "^2.0.5",
"@types/murmurhash-js": "^1.0.6",
"@types/node": "^24.0.10",
"@types/offscreencanvas": "^2019.7.3",
"@typescript-eslint/eslint-plugin": "^8.44.1",
"@typescript-eslint/parser": "^8.44.1",
"@vitest/browser": "^4.0.6",
"@vitest/browser-playwright": "^4.0.6",
"@vitest/ui": "^4.0.6",
address: "^2.0.3",
browserify: "^17.0.1",
"browserslist-to-esbuild": "^2.1.1",
chokidar: "^4.0.3",
cssnano: "^7.0.7",
"d3-queue": "^3.0.7",
diff: "^8.0.2",
"dts-bundle-generator": "^9.5.1",
ejs: "^3.1.10",
envify: "^4.1.0",
esbuild: "^0.27.0",
eslint: "^9.30.1",
"eslint-config-mourner": "^4.0.0",
"eslint-import-resolver-typescript": "^4.4.4",
"eslint-plugin-html": "^8.1.3",
"eslint-plugin-import-x": "^4.16.1",
"eslint-plugin-jsdoc": "^61.1.1",
express: "^5.1.0",
"json-stringify-pretty-compact": "^4.0.0",
lodash: "^4.17.21",
"mapbox-gl-styles": "^2.0.2",
minimist: "^1.2.6",
"mock-geolocation": "^1.0.11",
"node-notifier": "^10.0.1",
"npm-font-open-sans": "^1.1.0",
"npm-run-all2": "^8.0.4",
pixelmatch: "^6.0.0",
playwright: "^1.53.2",
postcss: "^8.5.6",
"postcss-cli": "^11.0.1",
"postcss-inline-svg": "^6.0.0",
"qrcode-terminal": "^0.12.0",
rollup: "^4.44.2",
"rollup-plugin-esbuild": "^6.2.1",
"rollup-plugin-unassert": "^0.6.0",
"serve-static": "^2.2.0",
"shuffle-seed": "^1.1.6",
"size-limit": "^11.2.0",
st: "^3.0.2",
stylelint: "^16.21.1",
"stylelint-config-standard": "^39.0.0",
tsx: "^4.20.3",
tweakpane: "^4.0.5",
typescript: "^5.9.2",
"typescript-eslint": "^8.44.1",
"vite-plugin-arraybuffer": "^0.1.0",
vitest: "^4.0.6"
};
var overrides = {
"type-fest": "^4.40.1"
};
var scripts = {
"build-dev": "rollup -c --environment BUILD:dev",
"watch-dev": "rollup -c --environment BUILD:dev --watch",
"watch-esm": "rollup -c rollup.config.esm.js --environment BUILD:dev --watch",
"build-umd-bench": "rollup -c --environment BUILD:bench,MINIFY:true",
"build-esm-bench": "rollup -c rollup.config.esm.js --environment BUILD:bench,MINIFY:true",
"build-bench": "run-p build-umd-bench build-esm-bench",
"build-prod": "rollup -c --environment BUILD:production",
"build-prod-min": "rollup -c --environment BUILD:production,MINIFY:true",
"build-esm-prod-min": "rollup -c rollup.config.esm.js --environment BUILD:production,MINIFY:true",
"build-csp": "rollup -c rollup.config.csp.js",
"build-css": "postcss -o dist/mapbox-gl.css src/css/mapbox-gl.css",
"build-style-spec": "npm run build --workspace src/style-spec && mkdir -p dist/style-spec && cp src/style-spec/dist/* dist/style-spec",
"build-dts": "dts-bundle-generator --no-banner --export-referenced-types=false --umd-module-name=mapboxgl -o ./dist/mapbox-gl.d.ts ./src/index.ts",
"watch-css": "postcss --watch -o dist/mapbox-gl.css src/css/mapbox-gl.css",
"build-token": "node build/generate-access-token-script.js",
"start-server": "st --no-cache -H 0.0.0.0 --port 9966 --index index.html .",
"start-range-server": "node build/range-request-server.js",
start: "run-p build-token watch-css watch-dev start-server",
"start-esm": "run-p build-token watch-css watch-esm start-server",
"start-debug": "run-p build-token watch-css watch-dev start-server",
"prepare-release-pages": "ln -sfn $PWD/dist test/release/dist && ln -sfn $PWD/debug test/release/debug && cp debug/access_token_generated.js test/release/",
"start-release": "run-s build-token build-prod-min build-esm-prod-min build-css print-release-url prepare-release-pages start-server",
lint: "eslint --cache src 3d-style test",
"lint-css": "stylelint 'src/css/mapbox-gl.css'",
test: "run-s lint lint-css test-typings test-unit",
"test-suite": "run-s test-render test-query test-expressions",
"test-suite-clean": "find test/integration/{render,query, expressions}-tests -mindepth 2 -type d -exec test -e \"{}/actual.png\" \\; -not \\( -exec test -e \"{}/style.json\" \\; \\) -print | xargs -t rm -r",
"watch-unit": "vitest --config vitest.config.unit.ts",
"test-unit": "vitest --config vitest.config.unit.ts --run",
"test-usvg": "vitest --config vitest.config.usvg.ts --run",
"start-usvg": "esbuild src/data/usvg/usvg_pb_renderer.ts src/data/usvg/usvg_pb_decoder.ts --outdir=src/data/usvg/ --bundle --format=esm --watch --serve=9966 --servedir=.",
"test-build": "node --test --test-timeout 10000 test/build/*.test.js",
"test-esm": "npx vitest run --config vitest.config.esm.ts",
"test-csp": "npm run build-token && vitest --config vitest.config.csp.ts --run",
"watch-render": "npx vitest watch --config vitest.config.render.chromium.ts",
"pretest-render": "npm run build-dev",
"test-render": "npx vitest run --config vitest.config.render.chromium.ts",
"test-render-chromium": "npx vitest run --config vitest.config.render.chromium.ts",
"test-render-firefox": "npx vitest run --config vitest.config.render.firefox.ts",
"test-render-safari": "npx vitest run --config vitest.config.render.safari.ts",
"pretest-render-prod": "npm run build-prod-min",
"test-render-prod": "npx vitest run --config vitest.config.render.chromium.prod.ts",
"test-render-chromium-prod": "npx vitest run --config vitest.config.render.chromium.prod.ts",
"pretest-render-csp": "npm run build-csp",
"test-render-csp": "npx vitest run --config vitest.config.render.chromium.csp.ts",
"test-render-chromium-csp": "npx vitest run --config vitest.config.render.chromium.csp.ts",
"watch-query": "npx vitest watch --config vitest.config.query.ts",
"test-query": "npx vitest run --config vitest.config.query.ts",
"test-expressions": "tsx ./test/expression.test.ts",
"test-typings": "tsx ./build/generate-typed-style-spec.ts && npm run tsc",
"test-style-spec": "npm test --workspace src/style-spec",
prepublishOnly: "run-s build-dev build-prod-min build-esm-prod-min build-prod build-csp build-css build-style-spec build-dts",
"print-release-url": "node build/print-release-url.js",
size: "size-limit",
"check-size": "tsx build/check-size.ts",
"check-ts-suppressions": "tsx build/check-ts-suppressions.ts",
codegen: "tsx ./build/generate-style-code.ts && tsx ./build/generate-struct-arrays.ts && tsx ./build/generate-typed-style-spec.ts",
tsc: "tsc --project tsconfig.json",
"publish-alpha": "./build/publish_alpha.sh"
};
var files = [
"dist/mapbox-gl*",
"dist/style-spec/",
"dist/esm-min/",
"dist/package.json",
"LICENSE.txt"
];
var _package = {
name: name,
description: description,
version: version,
main: main,
style: style,
types: types$2,
license: license,
type: type,
repository: repository,
workspaces: workspaces,
dependencies: dependencies,
devDependencies: devDependencies,
overrides: overrides,
scripts: scripts,
files: files,
"size-limit": [
{
limit: "450 kb",
gzip: true,
path: "dist/mapbox-gl.js"
},
{
limit: "450 kb",
gzip: true,
path: [
"dist/esm-min/mapbox-gl.js",
"dist/esm-min/shared.js",
"dist/esm-min/worker.js"
]
},
{
limit: "5.4 kb",
gzip: true,
path: "dist/mapbox-gl.css"
}
]
};
const config = {
API_URL: "https://api.mapbox.com",
get API_URL_REGEX() {
return /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/|\?|$)/i;
},
get API_TILEJSON_REGEX() {
return /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/v[0-9]*\/.*\.json.*$)/i;
},
get API_SPRITE_REGEX() {
return /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/styles\/v[0-9]*\/)(.*\/sprite.*\..*$)/i;
},
get API_FONTS_REGEX() {
return /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/fonts\/v[0-9]*\/)(.*\.pbf.*$)/i;
},
get API_STYLE_REGEX() {
return /^((https?:)?\/\/)?([^\/]+\.)?mapbox\.c(n|om)(\/styles\/v[0-9]*\/)(.*$)/i;
},
get API_CDN_URL_REGEX() {
return /^((https?:)?\/\/)?api\.mapbox\.c(n|om)(\/mapbox-gl-js\/)(.*$)/i;
},
get EVENTS_URL() {
if (!config.API_URL) {
return null;
}
try {
const url = new URL(config.API_URL);
if (url.hostname === "api.mapbox.cn") {
return "https://events.mapbox.cn/events/v2";
} else if (url.hostname === "api.mapbox.com") {
return "https://events.mapbox.com/events/v2";
} else {
return null;
}
} catch (e) {
return null;
}
},
SESSION_PATH: "/map-sessions/v1",
FEEDBACK_URL: "https://apps.mapbox.com/feedback",
TILE_URL_VERSION: "v4",
RASTER_URL_PREFIX: "raster/v1",
RASTERARRAYS_URL_PREFIX: "rasterarrays/v1",
REQUIRE_ACCESS_TOKEN: true,
ACCESS_TOKEN: null,
DEFAULT_STYLE: "mapbox://styles/mapbox/standard",
MAX_PARALLEL_IMAGE_REQUESTS: 16,
DRACO_URL: "https://api.mapbox.com/mapbox-gl-js/draco_decoder_gltf_v1.5.6.wasm",
MESHOPT_URL: "https://api.mapbox.com/mapbox-gl-js/meshopt_base_v0.20.wasm",
MESHOPT_SIMD_URL: "https://api.mapbox.com/mapbox-gl-js/meshopt_simd_v0.20.wasm",
BUILDING_GEN_URL: "https://api.mapbox.com/mapbox-gl-js/building-gen/building_gen_v1.2.4.wasm",
GLYPHS_URL: "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
TILES3D_URL_PREFIX: "3dtiles/v1"
};
function isMapboxHTTPURL(url) {
return config.API_URL_REGEX.test(url);
}
function isMapboxURL(url) {
return url.indexOf("mapbox:") === 0;
}
function isMapboxHTTPCDNURL(url) {
return config.API_CDN_URL_REGEX.test(url);
}
function isMapboxHTTPSpriteURL(url) {
return config.API_SPRITE_REGEX.test(url);
}
function isMapboxHTTPStyleURL(url) {
return config.API_STYLE_REGEX.test(url) && !isMapboxHTTPSpriteURL(url);
}
function isMapboxHTTPTileJSONURL(url) {
return config.API_TILEJSON_REGEX.test(url);
}
function isMapboxHTTPFontsURL(url) {
return config.API_FONTS_REGEX.test(url);
}
function hasCacheDefeatingSku(url) {
return url.indexOf("sku=") > 0 && isMapboxHTTPURL(url);
}
const LivePerformanceMarkers = {
create: "create",
load: "load",
fullLoad: "fullLoad"
};
const LivePerformanceUtils = {
mark(marker) {
performance.mark(marker);
},
measure(name, begin, end) {
performance.measure(name, begin, end);
}
};
function categorize(arr, fn) {
const obj = {};
if (arr) {
for (const item of arr) {
const category = fn(item);
if (obj[category] === void 0) {
obj[category] = [];
}
obj[category].push(item);
}
}
return obj;
}
function getCountersPerResourceType(resourceTimers) {
const obj = {};
if (resourceTimers) {
for (const category in resourceTimers) {
if (category !== "other") {
for (const timer of resourceTimers[category]) {
const min = `${category}ResolveRangeMin`;
const max = `${category}ResolveRangeMax`;
const reqCount = `${category}RequestCount`;
const reqCachedCount = `${category}RequestCachedCount`;
obj[min] = Math.min(obj[min] || Infinity, timer.startTime);
obj[max] = Math.max(obj[max] || -Infinity, timer.responseEnd);
const increment = (key) => {
if (obj[key] === void 0) {
obj[key] = 0;
}
++obj[key];
};
const transferSizeSupported = timer.transferSize !== void 0;
if (transferSizeSupported) {
const resourceFetchedFromCache = timer.transferSize === 0;
if (resourceFetchedFromCache) {
increment(reqCachedCount);
}
}
increment(reqCount);
}
}
}
}
return obj;
}
function getResourceCategory(entry) {
const url = entry.name.split("?")[0];
if (isMapboxHTTPCDNURL(url) && url.includes("mapbox-gl.js")) return "javascript";
if (isMapboxHTTPCDNURL(url) && url.includes("mapbox-gl.css")) return "css";
if (isMapboxHTTPFontsURL(url)) return "fontRange";
if (isMapboxHTTPSpriteURL(url)) return "sprite";
if (isMapboxHTTPStyleURL(url)) return "style";
if (isMapboxHTTPTileJSONURL(url)) return "tilejson";
return "other";
}
function getStyle(resourceTimers) {
if (resourceTimers) {
for (const timer of resourceTimers) {
const url = timer.name.split("?")[0];
if (isMapboxHTTPStyleURL(url)) {
const split = url.split("/").slice(-2);
if (split.length === 2) {
return `mapbox://styles/${split[0]}/${split[1]}`;
}
}
}
}
}
function getLivePerformanceMetrics(data) {
const resourceTimers = performance.getEntriesByType("resource");
const markerTimers = performance.getEntriesByType("mark");
const resourcesByType = categorize(resourceTimers, getResourceCategory);
const counters = getCountersPerResourceType(resourcesByType);
const devicePixelRatio = window.devicePixelRatio;
const connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
const effectiveType = connection ? connection.effectiveType : void 0;
const metrics = { counters: [], metadata: [], attributes: [] };
const addMetric = (arr, name, value) => {
if (value !== void 0 && value !== null) {
arr.push({ name, value: value.toString() });
}
};
for (const counter in counters) {
addMetric(metrics.counters, counter, counters[counter]);
}
if (data.interactionRange[0] !== Infinity && data.interactionRange[1] !== -Infinity) {
addMetric(metrics.counters, "interactionRangeMin", data.interactionRange[0]);
addMetric(metrics.counters, "interactionRangeMax", data.interactionRange[1]);
}
if (markerTimers) {
for (const markerName of Object.values(LivePerformanceMarkers)) {
const markerTimer = markerTimers.find((entry) => entry.name === markerName);
if (markerTimer) {
addMetric(metrics.counters, markerName, markerTimer.startTime);
}
}
}
addMetric(metrics.counters, "visibilityHidden", data.visibilityHidden);
addMetric(metrics.attributes, "style", getStyle(resourceTimers));
addMetric(metrics.attributes, "terrainEnabled", data.terrainEnabled ? "true" : "false");
addMetric(metrics.attributes, "fogEnabled", data.fogEnabled ? "true" : "false");
addMetric(metrics.attributes, "projection", data.projection);
addMetric(metrics.attributes, "zoom", data.zoom);
addMetric(metrics.metadata, "devicePixelRatio", devicePixelRatio);
addMetric(metrics.metadata, "connectionEffectiveType", effectiveType);
addMetric(metrics.metadata, "navigatorUserAgent", navigator.userAgent);
addMetric(metrics.metadata, "screenWidth", window.screen.width);
addMetric(metrics.metadata, "screenHeight", window.screen.height);
addMetric(metrics.metadata, "windowWidth", window.innerWidth);
addMetric(metrics.metadata, "windowHeight", window.innerHeight);
addMetric(metrics.metadata, "mapWidth", data.width / devicePixelRatio);
addMetric(metrics.metadata, "mapHeight", data.height / devicePixelRatio);
addMetric(metrics.metadata, "webglRenderer", data.renderer);
addMetric(metrics.metadata, "webglVendor", data.vendor);
addMetric(metrics.metadata, "sdkVersion", version);
addMetric(metrics.metadata, "sdkIdentifier", "mapbox-gl-js");
return metrics;
}
const PerformanceMarkers = {
libraryEvaluate: "library-evaluate",
frameGPU: "frame-gpu",
frame: "frame"
};
let fullLoadFinished = false;
let placementTime = 0;
function trackNameOrDefault() {
return isWorker(self) && self.name ? `${self.name}` : "Main";
}
let performanceUtilsGroupsMask = 0;
const PerformanceUtils = {
GROUP_NONE: 0,
GROUP_COMMON: 1 << 1,
GROUP_RENDERING: 1 << 2,
GROUP_RENDERING_DETAILED: 1 << 3,
now() {
return performance.now();
},
mark(marker, markOptions) {
performance.mark(marker, markOptions);
if (marker === LivePerformanceMarkers.fullLoad) {
fullLoadFinished = true;
}
},
// Bitmask to enable profiling groups
// e.g. PerformanceUtils.GROUP_COMMON | PerformanceUtils.GROUP_RENDERING
setEnabledGroupsMask(mask) {
performanceUtilsGroupsMask = mask;
},
enabledGroupsMask() {
return performanceUtilsGroupsMask;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
measureWithDetails(grpMask, name, track, startTime, properties, color) {
if ((grpMask & performanceUtilsGroupsMask) === 0) return;
performance.measure(name, { start: startTime, detail: {
devtools: {
trackGroup: track,
track: trackNameOrDefault(),
properties,
color
}
} });
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
markWithDetails(grpMask, name, properties, color) {
if ((grpMask & performanceUtilsGroupsMask) === 0) return;
performance.mark(name, {
detail: {
devtools: {
dataType: "marker",
color,
properties
}
}
});
},
// Based on console.timeStamp()
// Records timing measures to DevTools performance panel only
// Low overhead, but not recorded on Chrome timeline.
measureLowOverhead(grpMask, label, start, end, trackName) {
if ((grpMask & performanceUtilsGroupsMask) === 0) return;
console.timeStamp(label, start, end !== void 0 ? end : performance.now(), trackNameOrDefault());
},
measure(name, begin, end) {
performance.measure(name, begin, end);
},
beginMeasure(name) {
const mark = name;
performance.mark(mark);
return {
mark,
name
};
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
endMeasure(m, properties) {
performance.measure(m.name, {
start: m.mark,
detail: {
devtools: {
track: trackNameOrDefault(),
properties
}
}
});
},
recordPlacementTime(time) {
if (!fullLoadFinished) {
return;
}
placementTime += time;
},
frame(timestamp, isRenderFrame) {
performance.mark(PerformanceMarkers.frame, {
detail: {
timestamp,
isRenderFrame
}
});
},
clearMetrics() {
placementTime = 0;
fullLoadFinished = false;
performance.clearMeasures("loadTime");
performance.clearMeasures("fullLoadTime");
for (const marker in LivePerformanceMarkers) {
performance.clearMarks(LivePerformanceMarkers[marker]);
}
},
getPerformanceMetrics() {
const metrics = {};
performance.measure("loadTime", LivePerformanceMarkers.create, LivePerformanceMarkers.load);
performance.measure("fullLoadTime", LivePerformanceMarkers.create, LivePerformanceMarkers.fullLoad);
const measures = performance.getEntriesByType("measure");
for (const measure of measures) {
metrics[measure.name] = (metrics[measure.name] || 0) + measure.duration;
}
metrics.placementTime = placementTime;
return metrics;
},
getWorkerPerformanceMetrics() {
const entries = performance.getEntries().map((entry) => {
const result = entry.toJSON();
if (entry.detail) Object.assign(result, { detail: entry.detail });
return result;
});
return {
scope: isWorker(self) ? "Worker" : "Window",
timeOrigin: performance.timeOrigin,
entries
};
}
};
PerformanceUtils.mark(PerformanceMarkers.libraryEvaluate);
function getPerformanceMeasurement(request) {
const url = request ? request.url.toString() : void 0;
if (!url) {
return [];
}
return performance.getEntriesByName(url);
}
var performance$1 = performance;
let supportsOffscreenCanvas;
function offscreenCanvasSupported() {
if (supportsOffscreenCanvas == null) {
supportsOffscreenCanvas = self.OffscreenCanvas && new OffscreenCanvas(1, 1).getContext("2d") && typeof self.createImageBitmap === "function";
}
return supportsOffscreenCanvas;
}
let linkEl;
let reducedMotionQuery;
let stubTime;
let canvas$1;
let hasCanvasFingerprintNoise;
const exported$1 = {
/**
* Returns either performance.now() or a value set by setNow.
* @returns {number} Time value in milliseconds.
*/
now() {
if (stubTime !== void 0) {
return stubTime;
}
return performance.now();
},
setNow(time) {
stubTime = time;
},
restoreNow() {
stubTime = void 0;
},
frame(fn) {
const frame = requestAnimationFrame(fn);
return { cancel: () => cancelAnimationFrame(frame) };
},
getImageData(img, padding = 0) {
const { width, height } = img;
if (!canvas$1) {
canvas$1 = document.createElement("canvas");
}
const context = canvas$1.getContext("2d", { willReadFrequently: true });
if (!context) {
throw new Error("failed to create canvas 2d context");
}
if (width > canvas$1.width || height > canvas$1.height) {
canvas$1.width = width;
canvas$1.height = height;
}
context.clearRect(-padding, -padding, width + 2 * padding, height + 2 * padding);
context.drawImage(img, 0, 0, width, height);
return context.getImageData(-padding, -padding, width + 2 * padding, height + 2 * padding);
},
resolveURL(path) {
if (!linkEl) linkEl = document.createElement("a");
linkEl.href = path;
return linkEl.href;
},
get devicePixelRatio() {
return window.devicePixelRatio;
},
get prefersReducedMotion() {
if (!window.matchMedia) return false;
if (reducedMotionQuery == null) {
reducedMotionQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
}
return reducedMotionQuery.matches;
},
/**
* Returns true if the browser has OffscreenCanvas support and
* adds noise to Canvas2D operations used for image decoding to prevent fingerprinting.
*/
hasCanvasFingerprintNoise() {
if (hasCanvasFingerprintNoise !== void 0) {
return hasCanvasFingerprintNoise;
}
if (!offscreenCanvasSupported()) {
hasCanvasFingerprintNoise = false;
return false;
}
assert$1(self.OffscreenCanvas, "OffscreenCanvas is not supported");
const offscreenCanvas = new OffscreenCanvas(255 / 3, 1);
const offscreenCanvasContext = offscreenCanvas.getContext("2d", { willReadFrequently: true });
let inc = 0;
for (let i = 0; i < offscreenCanvas.width; ++i) {
offscreenCanvasContext.fillStyle = `rgba(${inc++},${inc++},${inc++}, 255)`;
offscreenCanvasContext.fillRect(i, 0, 1, 1);
}
const readData = offscreenCanvasContext.getImageData(0, 0, offscreenCanvas.width, offscreenCanvas.height);
inc = 0;
for (let i = 0; i < readData.data.length; ++i) {
if (i % 4 !== 3 && inc++ !== readData.data[i]) {
hasCanvasFingerprintNoise = true;
return true;
}
}
hasCanvasFingerprintNoise = false;
return false;
}
};
function setQueryParameters(url, params) {
const paramStart = url.indexOf("?");
if (paramStart < 0) return `${url}?${new URLSearchParams(params).toString()}`;
const searchParams = new URLSearchParams(url.slice(paramStart));
for (const key in params) {
searchParams.set(key, params[key]);
}
return `${url.slice(0, paramStart)}?${searchParams.toString()}`;
}
function stripQueryParameters(url, params = { persistentParams: [] }) {
const paramStart = url.indexOf("?");
if (paramStart < 0) return url;
const nextParams = new URLSearchParams();
const searchParams = new URLSearchParams(url.slice(paramStart));
for (const param of params.persistentParams) {
const value = searchParams.get(param);
if (value) nextParams.set(param, value);
}
const nextParamsString = nextParams.toString();
return `${url.slice(0, paramStart)}${nextParamsString.length > 0 ? `?${nextParamsString}` : ""}`;
}
const CACHE_NAME = "mapbox-tiles";
let cacheLimit = 500;
let cacheCheckThreshold = 50;
const MIN_TIME_UNTIL_EXPIRY = 1e3 * 60 * 7;
const PERSISTENT_PARAMS = ["language", "worldview", "jobid"];
let sharedCache;
function getCaches() {
try {
return caches;
} catch (e) {
}
}
function cacheOpen() {
const caches2 = getCaches();
if (caches2 && sharedCache == null) {
sharedCache = caches2.open(CACHE_NAME);
}
}
function cacheClose() {
sharedCache = void 0;
}
let responseConstructorSupportsReadableStream;
function prepareBody(response, callback) {
if (responseConstructorSupportsReadableStream === void 0) {
try {
new Response(new ReadableStream());
responseConstructorSupportsReadableStream = true;
} catch (e) {
responseConstructorSupportsReadableStream = false;
}
}
if (responseConstructorSupportsReadableStream) {
callback(response.body);
} else {
response.blob().then(callback).catch((e) => warnOnce(e.message));
}
}
function isNullBodyStatus(status) {
if (status === 200 || status === 404) {
return false;
}
return [101, 103, 204, 205, 304].includes(status);
}
function cachePut(request, response, requestTime) {
cacheOpen();
if (sharedCache == null) return;
const cacheControl = parseCacheControl(response.headers.get("Cache-Control") || "");
if (cacheControl["no-store"]) return;
const options = {
status: response.status,
statusText: response.statusText,
headers: new Headers()
};
response.headers.forEach((v, k) => options.headers.set(k, v));
if (cacheControl["max-age"]) {
options.headers.set("Expires", new Date(requestTime + cacheControl["max-age"] * 1e3).toUTCString());
}
const expires = options.headers.get("Expires");
if (!expires) return;
const timeUntilExpiry = new Date(expires).getTime() - requestTime;
if (timeUntilExpiry < MIN_TIME_UNTIL_EXPIRY) return;
let strippedURL = stripQueryParameters(request.url, { persistentParams: PERSISTENT_PARAMS });
if (response.status === 206) {
const range = request.headers.get("Range");
if (!range) return;
options.status = 200;
strippedURL = setQueryParameters(strippedURL, { range });
}
prepareBody(response, (body) => {
const clonedResponse = new Response(isNullBodyStatus(response.status) ? null : body, options);
cacheOpen();
if (sharedCache == null) return;
sharedCache.then((cache) => cache.put(strippedURL, clonedResponse)).catch((e) => warnOnce(e.message));
});
}
function cacheGet(request, callback) {
cacheOpen();
if (sharedCache == null) return callback(null);
sharedCache.then((cache) => {
let strippedURL = stripQueryParameters(request.url, { persistentParams: PERSISTENT_PARAMS });
const range = request.headers.get("Range");
if (range) strippedURL = setQueryParameters(strippedURL, { range });
cache.match(strippedURL).then((response) => {
const fresh = isFresh(response);
cache.delete(strippedURL).catch(callback);
if (fresh) {
cache.put(strippedURL, response.clone()).catch(callback);
}
callback(null, response, fresh);
}).catch(callback);
}).catch(callback);
}
function isFresh(response) {
if (!response) return false;
const expires = new Date(response.headers.get("Expires") || 0);
const cacheControl = parseCacheControl(response.headers.get("Cache-Control") || "");
return Number(expires) > Date.now() && !cacheControl["no-cache"];
}
let globalEntryCounter = Infinity;
function cacheEntryPossiblyAdded(dispatcher) {
globalEntryCounter++;
if (globalEntryCounter > cacheCheckThreshold) {
dispatcher.getActor().send("enforceCacheSizeLimit", cacheLimit);
globalEntryCounter = 0;
}
}
function enforceCacheSizeLimit(limit) {
cacheOpen();
if (sharedCache == null) return;
sharedCache.then((cache) => {
cache.keys().then((keys) => {
for (let i = 0; i < keys.length - limit; i++) {
cache.delete(keys[i]).catch((e) => warnOnce(e.message));
}
}).catch((e) => warnOnce(e.message));
}).catch((e) => warnOnce(e.message));
}
function clearTileCache(callback) {
const caches2 = getCaches();
if (!caches2) return;
const promise = caches2.delete(CACHE_NAME);
if (callback) {
promise.then(() => callback()).catch(callback);
}
}
function setCacheLimits(limit, checkThreshold) {
cacheLimit = limit;
cacheCheckThreshold = checkThreshold;
}
const exported = {
supported: false,
testSupport
};
let glForTesting;
let webpCheckComplete = false;
let webpImgTest;
let webpImgTestOnloadComplete = false;
const window$1 = typeof self !== "undefined" ? self : {};
if (window$1.document) {
webpImgTest = window$1.document.createElement("img");
webpImgTest.onload = function() {
if (glForTesting) testWebpTextureUpload(glForTesting);
glForTesting = null;
webpImgTestOnloadComplete = true;
};
webpImgTest.onerror = function() {
webpCheckComplete = true;
glForTesting = null;
};
webpImgTest.src = "data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA=";
}
function testSupport(gl) {
if (webpCheckComplete || !webpImgTest) return;
if (webpImgTestOnloadComplete) {
testWebpTextureUpload(gl);
} else {
glForTesting = gl;
}
}
function testWebpTextureUpload(gl) {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
try {
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, webpImgTest);
if (gl.isContextLost()) return;
exported.supported = true;
} catch (e) {
}
gl.deleteTexture(texture);
webpCheckComplete = true;
}
const ResourceType = {
Unknown: "Unknown",
Style: "Style",
Source: "Source",
Tile: "Tile",
Glyphs: "Glyphs",
SpriteImage: "SpriteImage",
SpriteJSON: "SpriteJSON",
Iconset: "Iconset",
Image: "Image",
Model: "Model"
};
if (typeof Object.freeze == "function") {
Object.freeze(ResourceType);
}
class AJAXError extends Error {
constructor(message, status, url) {
if (status === 401 && isMapboxHTTPURL(url)) {
message += ": you may have provided an invalid Mapbox access token. See https://docs.mapbox.com/api/overview/#access-tokens-and-token-scopes";
}
super(message);
this.status = status;
this.url = url;
}
toString() {
return `${this.name}: ${this.message} (${this.status}): ${this.url}`;
}
}
const getReferrer = isWorker() ? () => self.worker.referrer : () => (location.protocol === "blob:" ? parent : self).location.href;
const isFileURL = (url) => /^file:/.test(url) || /^file:/.test(getReferrer()) && !/^\w+:/.test(url);
function makeFetchRequest(requestParameters, callback) {
const controller = new AbortController();
const request = new Request(requestParameters.url, {
method: requestParameters.method || "GET",
body: requestParameters.body,
credentials: requestParameters.credentials,
headers: requestParameters.headers,
referrer: getReferrer(),
referrerPolicy: requestParameters.referrerPolicy,
signal: controller.signal
});
let complete = false;
let aborted = false;
const cacheIgnoringSearch = hasCacheDefeatingSku(request.url);
if (requestParameters.type === "json") {
request.headers.set("Accept", "application/json");
}
const validateOrFetch = (err, cachedResponse, responseIsFresh) => {
if (aborted) return;
if (err) {
if (err.message !== "SecurityError") {
warnOnce(err.toString());
}
}
if (cachedResponse && responseIsFresh) {
return finishRequest(cachedResponse);
}
if (cachedResponse) {
}
const requestTime = Date.now();
fetch(request).then((response) => {
if (response.ok) {
const cacheableResponse = cacheIgnoringSearch ? response.clone() : null;
return finishRequest(response, cacheableResponse, requestTime);
} else {
return callback(new AJAXError(response.statusText, response.status, requestParameters.url));
}
}).catch((error) => {
if (error.name === "AbortError") {
return;
}
callback(new Error(`${error.message} ${requestParameters.url}`));
});
};
const finishRequest = (response, cacheableResponse, requestTime) => {
(requestParameters.type === "arrayBuffer" ? response.arrayBuffer() : requestParameters.type === "json" ? response.json() : response.text()).then((result) => {
if (aborted) return;
if (cacheableResponse && requestTime) {
cachePut(request, cacheableResponse, requestTime);
}
complete = true;
callback(null, result, response.headers);
}).catch((err) => {
if (!aborted) callback(new Error(err.message));
});
};
if (cacheIgnoringSearch) {
cacheGet(request, validateOrFetch);
} else {
validateOrFetch(null, null);
}
return { cancel: () => {
aborted = true;
if (!complete) controller.abort();
} };
}
function makeXMLHttpRequest(requestParameters, callback) {
const xhr = new XMLHttpRequest();
xhr.open(requestParameters.method || "GET", requestParameters.url, true);
if (requestParameters.type === "arrayBuffer") {
xhr.responseType = "arraybuffer";
}
for (const k in requestParameters.headers) {
xhr.setRequestHeader(k, requestParameters.headers[k]);
}
if (requestParameters.type === "json") {
xhr.responseType = "text";
xhr.setRequestHeader("Accept", "application/json");
}
xhr.withCredentials = requestParameters.credentials === "include";
xhr.onerror = () => {
callback(new Error(xhr.statusText));
};
xhr.onload = () => {
if ((xhr.status >= 200 && xhr.status < 300 || xhr.status === 0) && xhr.response !== null) {
let data = xhr.response;
if (requestParameters.type === "json") {
try {
data = JSON.parse(xhr.response);
} catch (err) {
return callback(err);
}
}
const headersObject = new Headers();
const headers = xhr.getAllResponseHeaders();
headers.trim().split(/[\r\n]+/).forEach((line) => {
const parts = line.split(": ");
const header = parts.shift();
const value = parts.join(": ");
headersObject.append(header, value);
});
callback(null, data, headersObject);
} else {
callback(new AJAXError(xhr.statusText, xhr.status, requestParameters.url));
}
};
xhr.send(requestParameters.body);
return { cancel: () => xhr.abort() };
}
const makeRequest = function(requestParameters, callback) {
if (!isFileURL(requestParameters.url)) {
if (self.fetch && self.Request && self.AbortController && Request.prototype.hasOwnProperty("signal")) {
return makeFetchRequest(requestParameters, callback);
}
if (isWorker(self) && self.worker.actor) {
const queueOnMainThread = true;
return self.worker.actor.send("getResource", requestParameters, callback, void 0, queueOnMainThread);
}
}
return makeXMLHttpRequest(requestParameters, callback);
};
const getJSON = function(requestParameters, callback) {
return makeRequest(Object.assign(requestParameters, { type: "json" }), callback);
};
const getArrayBuffer = function(requestParameters, callback) {
return makeRequest(Object.assign(requestParameters, { type: "arrayBuffer" }), callback);
};
const postData = function(requestParameters, callback) {
return makeRequest(Object.assign(requestParameters, { method: "POST" }), callback);
};
const getData = function(requestParameters, callback) {
return makeRequest(Object.assign(requestParameters, { method: "GET" }), callback);
};
function sameOrigin(url) {
const a = document.createElement("a");
a.href = url;
return a.protocol === location.protocol && a.host === location.host;
}
const transparentPngUrl = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=";
function arrayBufferToImage(data, callback) {
const img = new Image();
img.onload = () => {
callback(null, img);
URL.revokeObjectURL(img.src);
img.onload = null;
requestAnimationFrame(() => {
img.src = transparentPngUrl;
});
};
img.onerror = () => callback(new Error("Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported."));
const blob = new Blob([new Uint8Array(data)], { type: "image/png" });
img.src = data.byteLength ? URL.createObjectURL(blob) : transparentPngUrl;
}
function arrayBufferToImageBitmap(data, callback) {
const blob = new Blob([new Uint8Array(data)], { type: "image/png" });
createImageBitmap(blob).then((imgBitmap) => {
callback(null, imgBitmap);
}).catch((e) => {
callback(new Error(`Could not load image because of ${e.message}. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.`));
});
}
let imageQueue, numImageRequests;
const resetImageRequestQueue = () => {
imageQueue = [];
numImageRequests = 0;
};
resetImageRequestQueue();
const getImage = function(requestParameters, callback) {
if (exported.supported) {
if (!requestParameters.headers) {
requestParameters.headers = {};
}
requestParameters.headers["accept"] = "image/webp,*/*";
}
if (numImageRequests >= config.MAX_PARALLEL_IMAGE_REQUESTS) {
const queued = {
requestParameters,
callback,
cancelled: false,
cancel() {
this.cancelled = true;
}
};
imageQueue.push(queued);
return queued;
}
numImageRequests++;
let advanced = false;
const advanceImageRequestQueue = () => {
if (advanced) return;
advanced = true;
numImageRequests--;
assert$1(numImageRequests >= 0);
while (imageQueue.length && numImageRequests < config.MAX_PARALLEL_IMAGE_REQUESTS) {
const request2 = imageQueue.shift();
const { requestParameters: requestParameters2, callback: callback2, cancelled } = request2;
if (!cancelled) {
request2.cancel = getImage(requestParameters2, callback2).cancel;
}
}
};
const request = getArrayBuffer(requestParameters, (err, data, headers) => {
advanceImageRequestQueue();
if (err) {
callback(err);
} else if (data) {
if (self.createImageBitmap) {
arrayBufferToImageBitmap(data, (err2, imgBitmap) => callback(err2, imgBitmap, headers));
} else {
arrayBufferToImage(data, (err2, img) => callback(err2, img, headers));
}
}
});
return {
cancel: () => {
request.cancel();
advanceImageRequestQueue();
}
};
};
const getVideo = function(urls, callback) {
const video = document.createElement("video");
video.muted = true;
video.onloadstart = function() {
callback(null, video);
};
for (let i = 0; i < urls.length; i++) {
const s = document.createElement("source");
if (!sameOrigin(urls[i])) {
video.crossOrigin = "Anonymous";
}
s.src = urls[i];
video.appendChild(s);
}
return { cancel: () => {
} };
};
var murmurhashJs$1 = {exports: {}};
var murmurhash3_gc$1 = {exports: {}};
/**
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011)
*
* @author Gary Court
* @see http://github.com/garycourt/murmurhash-js
* @author Austin Appleby
* @see http://sites.google.com/site/murmurhash/
*
* @param {string} key ASCII only
* @param {number} seed Positive integer only
* @return {number} 32-bit positive integer hash
*/
var murmurhash3_gc = murmurhash3_gc$1.exports;
var hasRequiredMurmurhash3_gc;
function requireMurmurhash3_gc () {
if (hasRequiredMurmurhash3_gc) return murmurhash3_gc$1.exports;
hasRequiredMurmurhash3_gc = 1;
(function (module) {
function murmurhash3_32_gc(key, seed) {
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
remainder = key.length & 3; // key.length % 4
bytes = key.length - remainder;
h1 = seed;
c1 = 0xcc9e2d51;
c2 = 0x1b873593;
i = 0;
while (i < bytes) {
k1 =
((key.charCodeAt(i) & 0xff)) |
((key.charCodeAt(++i) & 0xff) << 8) |
((key.charCodeAt(++i) & 0xff) << 16) |
((key.charCodeAt(++i) & 0xff) << 24);
++i;
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
}
k1 = 0;
switch (remainder) {
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1: k1 ^= (key.charCodeAt(i) & 0xff);
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
h1 ^= key.length;
h1 ^= h1 >>> 16;
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
h1 ^= h1 >>> 13;
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;
}
if('object' !== "undefined") {
module.exports = murmurhash3_32_gc;
}
} (murmurhash3_gc$1));
return murmurhash3_gc$1.exports;
}
var murmurhash2_gc$1 = {exports: {}};
/**
* JS Implementation of MurmurHash2
*
* @author Gary Court
* @see http://github.com/garycourt/murmurhash-js
* @author Austin Appleby
* @see http://sites.google.com/site/murmurhash/
*
* @param {string} str ASCII only
* @param {number} seed Positive integer only
* @return {number} 32-bit positive integer hash
*/
var murmurhash2_gc = murmurhash2_gc$1.exports;
var hasRequiredMurmurhash2_gc;
function requireMurmurhash2_gc () {
if (hasRequiredMurmurhash2_gc) return murmurhash2_gc$1.exports;
hasRequiredMurmurhash2_gc = 1;
(function (module) {
function murmurhash2_32_gc(str, seed) {
var
l = str.length,
h = seed ^ l,
i = 0,
k;
while (l >= 4) {
k =
((str.charCodeAt(i) & 0xff)) |
((str.charCodeAt(++i) & 0xff) << 8) |
((str.charCodeAt(++i) & 0xff) << 16) |
((str.charCodeAt(++i) & 0xff) << 24);
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
k ^= k >>> 24;
k = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;
l -= 4;
++i;
}
switch (l) {
case 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
case 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
case 1: h ^= (str.charCodeAt(i) & 0xff);
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
}
h ^= h >>> 13;
h = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));
h ^= h >>> 15;
return h >>> 0;
}
if('object' !== undefined) {
module.exports = murmurhash2_32_gc;
}
} (murmurhash2_gc$1));
return murmurhash2_gc$1.exports;
}
var murmurhashJs = murmurhashJs$1.exports;
var hasRequiredMurmurhashJs;
function requireMurmurhashJs () {
if (hasRequiredMurmurhashJs) return murmurhashJs$1.exports;
hasRequiredMurmurhashJs = 1;
var murmur3 = requireMurmurhash3_gc();
var murmur2 = requireMurmurhash2_gc();
murmurhashJs$1.exports = murmur3;
murmurhashJs$1.exports.murmur3 = murmur3;
murmurhashJs$1.exports.murmur2 = murmur2;
return murmurhashJs$1.exports;
}
var murmurhashJsExports = requireMurmurhashJs();
var murmur3 = /*@__PURE__*/getDefaultExportFromCjs(murmurhashJsExports);
class Event {
constructor(type, ...eventData) {
Object.assign(this, eventData[0] || {});
this.type = type;
}
}
class ErrorEvent extends Event {
constructor(error, data = {}) {
super("error", Object.assign({ error }, data));
}
}
function _addEventListener(type, listener, listenerList) {
const listenerExists = listenerList[type] && listenerList[type].indexOf(listener) !== -1;
if (!listenerExists) {
listenerList[type] = listenerList[type] || [];
listenerList[type].push(listener);
}
}
function _removeEventListener(type, listener, listenerList) {
if (listenerList && listenerList[type]) {
const index = listenerList[type].indexOf(listener);
if (index !== -1) {
listenerList[type].splice(index, 1);
}
}
}
class Evented {
/**
* Adds a listener to a specified event type.
*
* @param {string} type The event type to add a listen for.
* @param {Function} listener The function to be called when the event is fired.
* The listener function is called with the data object passed to `fire`,
* extended with `target` and `type` properties.
* @returns {Object} Returns itself to allow for method chaining.
*/
on(type, listener) {
this._listeners = this._listeners || {};
_addEventListener(type, listener, this._listeners);
return this;
}
/**
* Removes a previously registered event listener.
*
* @param {string} type The event type to remove listeners for.
* @param {Function} listener The listener function to remove.
* @returns {Object} Returns itself to allow for method chaining.
*/
off(type, listener) {
_removeEventListener(type, listener, this._listeners);
_removeEventListener(type, listener, this._oneTimeListeners);
return this;
}
once(type, listener) {
if (!listener) {
return new Promise((resolve) => {
this.once(type, resolve);
});
}
this._oneTimeListeners = this._oneTimeListeners || {};
_addEventListener(type, listener, this._oneTimeListeners);
return this;
}
fire(e, eventData) {
const event = typeof e === "string" ? new Event(e, eventData) : e;
const type = event.type;
if (this.listens(type)) {
event.target = this;
const listeners = this._listeners && this._listeners[type] ? this._listeners[type].slice() : [];
for (const listener of listeners) {
listener.call(this, event);
}
const oneTimeListeners = this._oneTimeListeners && this._oneTimeListeners[type] ? this._oneTimeListeners[type].slice() : [];
for (const listener of oneTimeListeners) {
_removeEventListener(type, listener, this._oneTimeListeners);
listener.call(this, event);
}
const parent = this._eventedParent;
if (parent) {
const eventedParentData = typeof this._eventedParentData === "function" ? this._eventedParentData() : this._eventedParentData;
Object.assign(event, eventedParentData);
parent.fire(event);
}
} else if (event instanceof ErrorEvent) {
console.error(event.error);
}
return this;
}
/**
* Returns true if this instance of Evented or any forwarded instances of Evented have a listener for the specified type.
*
* @param {string} type The event type.
* @returns {boolean} Returns `true` if there is at least one registered listener for specified event type, `false` otherwise.
* @private
*/
listens(type) {
return !!(this._listeners && this._listeners[type] && this._listeners[type].length > 0 || this._oneTimeListeners && this._oneTimeListeners[type] && this._oneTimeListeners[type].length > 0 || this._eventedParent && this._eventedParent.listens(type));
}
/**
* Bubble all events fired by this instance of Evented to this parent instance of Evented.
*
* @returns {Object} `this`
* @private
*/
setEventedParent(parent, data) {
this._eventedParent = parent;
this._eventedParentData = data;
return this;
}
}
const separator = "";
class ImageId {
constructor(id) {
if (typeof id === "string") {
this.name = id;
} else {
this.name = id.name;
this.iconsetId = id.iconsetId;
}
}
static from(id) {
return new ImageId(id);
}
static toString(id) {
return id.iconsetId ? `${id.name}${separator}${id.iconsetId}` : id.name;
}
static parse(str) {
const [name, iconsetId] = str.split(separator);
return new ImageId({ name, iconsetId });
}
static isEqual(a, b) {
return a.name === b.name && a.iconsetId === b.iconsetId;
}
toString() {
return ImageId.toString(this);
}
serialize() {
return { name: this.name, iconsetId: this.iconsetId };
}
}
var csscolorparser$1 = {};
var hasRequiredCsscolorparser;
function requireCsscolorparser () {
if (hasRequiredCsscolorparser) return csscolorparser$1;
hasRequiredCsscolorparser = 1;
// (c) Dean McNamee , 2012.
//
// https://github.com/deanm/css-color-parser-js
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// http://www.w3.org/TR/css3-color/
var kCSSColorTable = {
"transparent": [0,0,0,0], "aliceblue": [240,248,255,1],
"antiquewhite": [250,235,215,1], "aqua": [0,255,255,1],
"aquamarine": [127,255,212,1], "azure": [240,255,255,1],
"beige": [245,245,220,1], "bisque": [255,228,196,1],
"black": [0,0,0,1], "blanchedalmond": [255,235,205,1],
"blue": [0,0,255,1], "blueviolet": [138,43,226,1],
"brown": [165,42,42,1], "burlywood": [222,184,135,1],
"cadetblue": [95,158,160,1], "chartreuse": [127,255,0,1],
"chocolate": [210,105,30,1], "coral": [255,127,80,1],
"cornflowerblue": [100,149,237,1], "cornsilk": [255,248,220,1],
"crimson": [220,20,60,1], "cyan": [0,255,255,1],
"darkblue": [0,0,139,1], "darkcyan": [0,139,139,1],
"darkgoldenrod": [184,134,11,1], "darkgray": [169,169,169,1],
"darkgreen": [0,100,0,1], "darkgrey": [169,169,169,1],
"darkkhaki": [189,183,107,1], "darkmagenta": [139,0,139,1],
"darkolivegreen": [85,107,47,1], "darkorange": [255,140,0,1],
"darkorchid": [153,50,204,1], "darkred": [139,0,0,1],
"darksalmon": [233,150,122,1], "darkseagreen": [143,188,143,1],
"darkslateblue": [72,61,139,1], "darkslategray": [47,79,79,1],
"darkslategrey": [47,79,79,1], "darkturquoise": [0,206,209,1],
"darkviolet": [148,0,211,1], "deeppink": [255,20,147,1],
"deepskyblue": [0,191,255,1], "dimgray": [105,105,105,1],
"dimgrey": [105,105,105,1], "dodgerblue": [30,144,255,1],
"firebrick": [178,34,34,1], "floralwhite": [255,250,240,1],
"forestgreen": [34,139,34,1], "fuchsia": [255,0,255,1],
"gainsboro": [220,220,220,1], "ghostwhite": [248,248,255,1],
"gold": [255,215,0,1], "goldenrod": [218,165,32,1],
"gray": [128,128,128,1], "green": [0,128,0,1],
"greenyellow": [173,255,47,1], "grey": [128,128,128,1],
"honeydew": [240,255,240,1], "hotpink": [255,105,180,1],
"indianred": [205,92,92,1], "indigo": [75,0,130,1],
"ivory": [255,255,240,1], "khaki": [240,230,140,1],
"lavender": [230,230,250,1], "lavenderblush": [255,240,245,1],
"lawngreen": [124,252,0,1], "lemonchiffon": [255,250,205,1],
"lightblue": [173,216,230,1], "lightcoral": [240,128,128,1],
"lightcyan": [224,255,255,1], "lightgoldenrodyellow": [250,250,210,1],
"lightgray": [211,211,211,1], "lightgreen": [144,238,144,1],
"lightgrey": [211,211,211,1], "lightpink": [255,182,193,1],
"lightsalmon": [255,160,122,1], "lightseagreen": [32,178,170,1],
"lightskyblue": [135,206,250,1], "lightslategray": [119,136,153,1],
"lightslategrey": [119,136,153,1], "lightsteelblue": [176,196,222,1],
"lightyellow": [255,255,224,1], "lime": [0,255,0,1],
"limegreen": [50,205,50,1], "linen": [250,240,230,1],
"magenta": [255,0,255,1], "maroon": [128,0,0,1],
"mediumaquamarine": [102,205,170,1], "mediumblue": [0,0,205,1],
"mediumorchid": [186,85,211,1], "mediumpurple": [147,112,219,1],
"mediumseagreen": [60,179,113,1], "mediumslateblue": [123,104,238,1],
"mediumspringgreen": [0,250,154,1], "mediumturquoise": [72,209,204,1],
"mediumvioletred": [199,21,133,1], "midnightblue": [25,25,112,1],
"mintcream": [245,255,250,1], "mistyrose": [255,228,225,1],
"moccasin": [255,228,181,1], "navajowhite": [255,222,173,1],
"navy": [0,0,128,1], "oldlace": [253,245,230,1],
"olive": [128,128,0,1], "olivedrab": [107,142,35,1],
"orange": [255,165,0,1], "orangered": [255,69,0,1],
"orchid": [218,112,214,1], "palegoldenrod": [238,232,170,1],
"palegreen": [152,251,152,1], "paleturquoise": [175,238,238,1],
"palevioletred": [219,112,147,1], "papayawhip": [255,239,213,1],
"peachpuff": [255,218,185,1], "peru": [205,133,63,1],
"pink": [255,192,203,1], "plum": [221,160,221,1],
"powderblue": [176,224,230,1], "purple": [128,0,128,1],
"rebeccapurple": [102,51,153,1],
"red": [255,0,0,1], "rosybrown": [188,143,143,1],
"royalblue": [65,105,225,1], "saddlebrown": [139,69,19,1],
"salmon": [250,128,114,1], "sandybrown": [244,164,96,1],
"seagreen": [46,139,87,1], "seashell": [255,245,238,1],
"sienna": [160,82,45,1], "silver": [192,192,192,1],
"skyblue": [135,206,235,1], "slateblue": [106,90,205,1],
"slategray": [112,128,144,1], "slategrey": [112,128,144,1],
"snow": [255,250,250,1], "springgreen": [0,255,127,1],
"steelblue": [70,130,180,1], "tan": [210,180,140,1],
"teal": [0,128,128,1], "thistle": [216,191,216,1],
"tomato": [255,99,71,1], "turquoise": [64,224,208,1],
"violet": [238,130,238,1], "wheat": [245,222,179,1],
"white": [255,255,255,1], "whitesmoke": [245,245,245,1],
"yellow": [255,255,0,1], "yellowgreen": [154,205,50,1]};
function clamp_css_byte(i) { // Clamp to integer 0 .. 255.
i = Math.round(i); // Seems to be what Chrome does (vs truncation).
return i < 0 ? 0 : i > 255 ? 255 : i;
}
function clamp_css_float(f) { // Clamp to float 0.0 .. 1.0.
return f < 0 ? 0 : f > 1 ? 1 : f;
}
function parse_css_int(str) { // int or percentage.
if (str[str.length - 1] === '%')
return clamp_css_byte(parseFloat(str) / 100 * 255);
return clamp_css_byte(parseInt(str));
}
function parse_css_float(str) { // float or percentage.
if (str[str.length - 1] === '%')
return clamp_css_float(parseFloat(str) / 100);
return clamp_css_float(parseFloat(str));
}
function css_hue_to_rgb(m1, m2, h) {
if (h < 0) h += 1;
else if (h > 1) h -= 1;
if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
if (h * 2 < 1) return m2;
if (h * 3 < 2) return m1 + (m2 - m1) * (2/3 - h) * 6;
return m1;
}
function parseCSSColor(css_str) {
// Remove all whitespace, not compliant, but should just be more accepting.
var str = css_str.replace(/ /g, '').toLowerCase();
// Color keywords (and transparent) lookup.
if (str in kCSSColorTable) return kCSSColorTable[str].slice(); // dup.
// #abc and #abc123 syntax.
if (str[0] === '#') {
if (str.length === 4) {
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
if (!(iv >= 0 && iv <= 0xfff)) return null; // Covers NaN.
return [((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8),
(iv & 0xf0) | ((iv & 0xf0) >> 4),
(iv & 0xf) | ((iv & 0xf) << 4),
1];
} else if (str.length === 7) {
var iv = parseInt(str.substr(1), 16); // TODO(deanm): Stricter parsing.
if (!(iv >= 0 && iv <= 0xffffff)) return null; // Covers NaN.
return [(iv & 0xff0000) >> 16,
(iv & 0xff00) >> 8,
iv & 0xff,
1];
}
return null;
}
var op = str.indexOf('('), ep = str.indexOf(')');
if (op !== -1 && ep + 1 === str.length) {
var fname = str.substr(0, op);
var params = str.substr(op+1, ep-(op+1)).split(',');
var alpha = 1; // To allow case fallthrough.
switch (fname) {
case 'rgba':
if (params.length !== 4) return null;
alpha = parse_css_float(params.pop());
// Fall through.
case 'rgb':
if (params.length !== 3) return null;
return [parse_css_int(params[0]),
parse_css_int(params[1]),
parse_css_int(params[2]),
alpha];
case 'hsla':
if (params.length !== 4) return null;
alpha = parse_css_float(params.pop());
// Fall through.
case 'hsl':
if (params.length !== 3) return null;
var h = (((parseFloat(params[0]) % 360) + 360) % 360) / 360; // 0 .. 1
// NOTE(deanm): According to the CSS spec s/l should only be
// percentages, but we don't bother and let float or percentage.
var s = parse_css_float(params[1]);
var l = parse_css_float(params[2]);
var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s;
var m1 = l * 2 - m2;
return [clamp_css_byte(css_hue_to_rgb(m1, m2, h+1/3) * 255),
clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255),
clamp_css_byte(css_hue_to_rgb(m1, m2, h-1/3) * 255),
alpha];
default:
return null;
}
}
return null;
}
try { csscolorparser$1.parseCSSColor = parseCSSColor; } catch(e) { }
return csscolorparser$1;
}
var csscolorparserExports = requireCsscolorparser();
var csscolorparser = /*@__PURE__*/getDefaultExportFromCjs(csscolorparserExports);
class Color {
constructor(r, g, b, a = 1) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
/**
* Parses valid CSS color strings and returns a `Color` instance.
* @returns A `Color` instance, or `undefined` if the input is not a valid color string.
*/
static parse(input) {
if (!input) {
return void 0;
}
if (input instanceof Color) {
return input;
}
if (typeof input !== "string") {
return void 0;
}
const rgba = csscolorparserExports.parseCSSColor(input);
if (!rgba) {
return void 0;
}
return new Color(
rgba[0] / 255,
rgba[1] / 255,
rgba[2] / 255,
rgba[3]
);
}
/**
* Returns an RGBA string representing the color value.
*
* @returns An RGBA string.
* @example
* var purple = new Color.parse('purple');
* purple.toString; // = "rgba(128,0,128,1)"
* var translucentGreen = new Color.parse('rgba(26, 207, 26, .73)');
* translucentGreen.toString(); // = "rgba(26,207,26,0.73)"
*/
toString() {
const [r, g, b, a] = [
this.r,
this.g,
this.b,
this.a
];
return `rgba(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)},${a})`;
}
toNonPremultipliedRenderColor(lut) {
const { r, g, b, a } = this;
return new NonPremultipliedRenderColor(lut, r, g, b, a);
}
toPremultipliedRenderColor(lut) {
const { r, g, b, a } = this;
return new PremultipliedRenderColor(lut, r * a, g * a, b * a, a);
}
clone() {
return new Color(this.r, this.g, this.b, this.a);
}
}
class RenderColor {
constructor(lut, r, g, b, a, premultiplied = false) {
this.premultiplied = false;
this.premultiplied = premultiplied;
if (!lut) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
} else {
const N = lut.image.height;
const N2 = N * N;
if (this.premultiplied) {
r = a === 0 ? 0 : r / a * (N - 1);
g = a === 0 ? 0 : g / a * (N - 1);
b = a === 0 ? 0 : b / a * (N - 1);
} else {
r = r * (N - 1);
g = g * (N - 1);
b = b * (N - 1);
}
const r0 = Math.floor(r);
const g0 = Math.floor(g);
const b0 = Math.floor(b);
const r1 = Math.ceil(r);
const g1 = Math.ceil(g);
const b1 = Math.ceil(b);
const rw = r - r0;
const gw = g - g0;
const bw = b - b0;
const data = lut.image.data;
const i0 = (r0 + g0 * N2 + b0 * N) * 4;
const i1 = (r0 + g0 * N2 + b1 * N) * 4;
const i2 = (r0 + g1 * N2 + b0 * N) * 4;
const i3 = (r0 + g1 * N2 + b1 * N) * 4;
const i4 = (r1 + g0 * N2 + b0 * N) * 4;
const i5 = (r1 + g0 * N2 + b1 * N) * 4;
const i6 = (r1 + g1 * N2 + b0 * N) * 4;
const i7 = (r1 + g1 * N2 + b1 * N) * 4;
if (i0 < 0 || i7 >= data.length) {
throw new Error("out of range");
}
this.r = number(
number(
number(data[i0], data[i1], bw),
number(data[i2], data[i3], bw),
gw
),
number(
number(data[i4], data[i5], bw),
number(data[i6], data[i7], bw),
gw
),
rw
) / 255 * (this.premultiplied ? a : 1);
this.g = number(
number(
number(data[i0 + 1], data[i1 + 1], bw),
number(data[i2 + 1], data[i3 + 1], bw),
gw
),
number(
number(data[i4 + 1], data[i5 + 1], bw),
number(data[i6 + 1], data[i7 + 1], bw),
gw
),
rw
) / 255 * (this.premultiplied ? a : 1);
this.b = number(
number(
number(data[i0 + 2], data[i1 + 2], bw),
number(data[i2 + 2], data[i3 + 2], bw),
gw
),
number(
number(data[i4 + 2], data[i5 + 2], bw),
number(data[i6 + 2], data[i7 + 2], bw),
gw
),
rw
) / 255 * (this.premultiplied ? a : 1);
this.a = a;
}
}
/**
* Returns an RGBA array of values representing the color.
* @returns An array of RGBA color values in the range [0, 255].
*/
toArray() {
const { r, g, b, a } = this;
return [
r * 255,
g * 255,
b * 255,
a
];
}
/**
* Returns an HSLA array of values representing the color, unpremultiplied by A.
* @returns An array of HSLA color values.
*/
toHslaArray() {
let { r, g, b, a } = this;
if (this.premultiplied) {
if (a === 0) return [0, 0, 0, 0];
const invA = 1 / a;
r *= invA;
g *= invA;
b *= invA;
}
const red = Math.min(Math.max(r, 0), 1);
const green = Math.min(Math.max(g, 0), 1);
const blue = Math.min(Math.max(b, 0), 1);
const min = Math.min(red, green, blue);
const max = Math.max(red, green, blue);
const delta = max - min;
const l = (min + max) * 0.5;
if (delta === 0) {
return [0, 0, l * 100, a];
}
const s = l > 0.5 ? delta / (2 - max - min) : delta / (max + min);
let h;
switch (max) {
case red:
h = ((green - blue) / delta + (green < blue ? 6 : 0)) * 60;
break;
case green:
h = ((blue - red) / delta + 2) * 60;
break;
default:
h = ((red - green) / delta + 4) * 60;
}
return [h, s * 100, l * 100, a];
}
/**
* Returns a RGBA array of float values representing the color.
*
* @returns An array of RGBA color values in the range [0, 1].
*/
toArray01() {
const { r, g, b, a } = this;
return [
r,
g,
b,
a
];
}
/**
* Returns an RGB array of values representing the color, unpremultiplied by A and multiplied by a scalar.
*
* @param {number} scale A scale to apply to the unpremultiplied-alpha values.
* @returns An array of RGB color values in the range [0, 1].
*/
toArray01Scaled(scale) {
const { r, g, b } = this;
return [
r * scale,
g * scale,
b * scale
];
}
/**
* Returns an RGBA array of values representing the color converted to linear color space.
* The color is defined by sRGB primaries, but the sRGB transfer function
* is reversed to obtain linear energy.
* @returns An array of RGBA color values in the range [0, 1].
*/
toArray01Linear() {
const { r, g, b, a } = this;
return [
Math.pow(r, 2.2),
Math.pow(g, 2.2),
Math.pow(b, 2.2),
a
];
}
}
class NonPremultipliedRenderColor extends RenderColor {
constructor(lut, r, g, b, a) {
super(lut, r, g, b, a, false);
}
}
class PremultipliedRenderColor extends RenderColor {
constructor(lut, r, g, b, a) {
super(lut, r, g, b, a, true);
}
}
Color.black = new Color(0, 0, 0, 1);
Color.white = new Color(1, 1, 1, 1);
Color.transparent = new Color(0, 0, 0, 0);
Color.red = new Color(1, 0, 0, 1);
Color.blue = new Color(0, 0, 1, 1);
function number(a, b, t) {
return a * (1 - t) + b * t;
}
function color(from, to, t) {
return new Color(
number(from.r, to.r, t),
number(from.g, to.g, t),
number(from.b, to.b, t),
number(from.a, to.a, t)
);
}
function array$1(from, to, t) {
return from.map((d, i) => {
return number(d, to[i], t);
});
}
var interpolate$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
array: array$1,
color: color,
number: number
});
class ParsingError extends Error {
constructor(key, message) {
super(message);
this.message = message;
this.key = key;
}
}
class Scope {
constructor(parent, bindings = []) {
this.parent = parent;
this.bindings = {};
for (const [name, expression] of bindings) {
this.bindings[name] = expression;
}
}
concat(bindings) {
return new Scope(this, bindings);
}
get(name) {
if (this.bindings[name]) {
return this.bindings[name];
}
if (this.parent) {
return this.parent.get(name);
}
throw new Error(`${name} not found in scope.`);
}
has(name) {
if (this.bindings[name]) return true;
return this.parent ? this.parent.has(name) : false;
}
}
const NullType = { kind: "null" };
const NumberType = { kind: "number" };
const StringType = { kind: "string" };
const BooleanType = { kind: "boolean" };
const ColorType = { kind: "color" };
const ObjectType = { kind: "object" };
const ValueType = { kind: "value" };
const ErrorType = { kind: "error" };
const CollatorType = { kind: "collator" };
const FormattedType = { kind: "formatted" };
const ResolvedImageType = { kind: "resolvedImage" };
function array(itemType, N) {
return {
kind: "array",
itemType,
N
};
}
function toString$1(type) {
if (type.kind === "array") {
const itemType = toString$1(type.itemType);
return typeof type.N === "number" ? `array<${itemType}, ${type.N}>` : type.itemType.kind === "value" ? "array" : `array<${itemType}>`;
} else {
return type.kind;
}
}
const valueMemberTypes = [
NullType,
NumberType,
StringType,
BooleanType,
ColorType,
FormattedType,
ObjectType,
array(ValueType),
ResolvedImageType
];
function checkSubtype(expected, t) {
if (t.kind === "error") {
return null;
} else if (expected.kind === "array") {
if (t.kind === "array" && (t.N === 0 && t.itemType.kind === "value" || !checkSubtype(expected.itemType, t.itemType)) && (typeof expected.N !== "number" || expected.N === t.N)) {
return null;
}
} else if (expected.kind === t.kind) {
return null;
} else if (expected.kind === "value") {
for (const memberType of valueMemberTypes) {
if (!checkSubtype(memberType, t)) {
return null;
}
}
}
return `Expected ${toString$1(expected)} but found ${toString$1(t)} instead.`;
}
function isValidType(provided, allowedTypes) {
return allowedTypes.some((t) => t.kind === provided.kind);
}
function isValidNativeType(provided, allowedTypes) {
return allowedTypes.some((t) => {
if (t === "null") {
return provided === null;
} else if (t === "array") {
return Array.isArray(provided);
} else if (t === "object") {
return provided && !Array.isArray(provided) && typeof provided === "object";
} else {
return t === typeof provided;
}
});
}
function typeEquals(a, b) {
if (a.kind === "array" && b.kind === "array") {
return a.N === b.N && typeEquals(a.itemType, b.itemType);
} else {
return a.kind === b.kind;
}
}
class Collator {
constructor(caseSensitive, diacriticSensitive, locale) {
if (caseSensitive)
this.sensitivity = diacriticSensitive ? "variant" : "case";
else
this.sensitivity = diacriticSensitive ? "accent" : "base";
this.locale = locale;
this.collator = new Intl.Collator(
this.locale ? this.locale : [],
{ sensitivity: this.sensitivity, usage: "search" }
);
}
compare(lhs, rhs) {
return this.collator.compare(lhs, rhs);
}
resolvedLocale() {
return new Intl.Collator(this.locale ? this.locale : []).resolvedOptions().locale;
}
}
class FormattedSection {
constructor(text, image, scale, fontStack, textColor) {
this.text = text.normalize ? text.normalize() : text;
this.image = image;
this.scale = scale;
this.fontStack = fontStack;
this.textColor = textColor;
}
}
class Formatted {
constructor(sections) {
this.sections = sections;
}
static fromString(unformatted) {
return new Formatted([new FormattedSection(unformatted, null, null, null, null)]);
}
isEmpty() {
if (this.sections.length === 0) return true;
return !this.sections.some((section) => {
if (section.text.length !== 0) return true;
if (!section.image) return false;
return section.image.hasPrimary();
});
}
static factory(text) {
if (text instanceof Formatted) {
return text;
} else {
return Formatted.fromString(text);
}
}
toString() {
if (this.sections.length === 0) return "";
return this.sections.map((section) => section.text).join("");
}
serialize() {
const serialized = ["format"];
for (const section of this.sections) {
if (section.image) {
const primaryId = section.image.getPrimary().id.toString();
serialized.push(["image", primaryId]);
continue;
}
serialized.push(section.text);
const options = {};
if (section.fontStack) {
options["text-font"] = ["literal", section.fontStack.split(",")];
}
if (section.scale) {
options["font-scale"] = section.scale;
}
if (section.textColor) {
options["text-color"] = ["rgba"].concat(section.textColor.toNonPremultipliedRenderColor(null).toArray());
}
serialized.push(options);
}
return serialized;
}
}
class ImageVariant {
constructor(id, options = {}) {
this.id = ImageId.from(id);
this.params = options.params;
this.sx = options.sx || 1;
this.sy = options.sy || 1;
}
toString() {
return JSON.stringify(this);
}
static parse(str) {
let id, params, sx, sy;
try {
({ id, params, sx, sy } = JSON.parse(str) || {});
} catch (e) {
return null;
}
if (!id) return null;
return new ImageVariant(id, { params, sx, sy });
}
scaleSelf(factor, yFactor = factor) {
this.sx *= factor;
this.sy *= yFactor;
return this;
}
}
class ResolvedImage {
constructor(primaryId, primaryOptions, secondaryId, secondaryOptions, available = false) {
this.primaryId = ImageId.from(primaryId);
this.primaryOptions = primaryOptions;
if (secondaryId) this.secondaryId = ImageId.from(secondaryId);
this.secondaryOptions = secondaryOptions;
this.available = available;
}
toString() {
if (this.primaryId && this.secondaryId) {
const primaryName = this.primaryId.name;
const secondaryName = this.secondaryId.name;
return `[${primaryName},${secondaryName}]`;
}
return this.primaryId.name;
}
hasPrimary() {
return !!this.primaryId;
}
getPrimary() {
return new ImageVariant(this.primaryId, this.primaryOptions);
}
hasSecondary() {
return !!this.secondaryId;
}
getSecondary() {
if (!this.secondaryId) {
return null;
}
return new ImageVariant(this.secondaryId, this.secondaryOptions);
}
static from(image) {
return typeof image === "string" ? ResolvedImage.build({ name: image }) : image;
}
static build(primaryId, secondaryId, primaryOptions, secondaryOptions) {
if (!primaryId || typeof primaryId === "object" && !("name" in primaryId)) return null;
return new ResolvedImage(primaryId, primaryOptions, secondaryId, secondaryOptions);
}
}
function validateRGBA(r, g, b, a) {
if (!(typeof r === "number" && r >= 0 && r <= 255 && typeof g === "number" && g >= 0 && g <= 255 && typeof b === "number" && b >= 0 && b <= 255)) {
const value = typeof a === "number" ? [r, g, b, a] : [r, g, b];
return `Invalid rgba value [${value.join(", ")}]: 'r', 'g', and 'b' must be between 0 and 255.`;
}
if (!(typeof a === "undefined" || typeof a === "number" && a >= 0 && a <= 1)) {
return `Invalid rgba value [${[r, g, b, a].join(", ")}]: 'a' must be between 0 and 1.`;
}
return null;
}
function validateHSLA(h, s, l, a) {
if (!(typeof h === "number" && h >= 0 && h <= 360)) {
const value = typeof a === "number" ? [h, s, l, a] : [h, s, l];
return `Invalid hsla value [${value.join(", ")}]: 'h' must be between 0 and 360.`;
}
if (!(typeof s === "number" && s >= 0 && s <= 100 && typeof l === "number" && l >= 0 && l <= 100)) {
const value = typeof a === "number" ? [h, s, l, a] : [h, s, l];
return `Invalid hsla value [${value.join(", ")}]: 's', and 'l' must be between 0 and 100.`;
}
if (!(typeof a === "undefined" || typeof a === "number" && a >= 0 && a <= 1)) {
return `Invalid hsla value [${[h, s, l, a].join(", ")}]: 'a' must be between 0 and 1.`;
}
return null;
}
function isValue(mixed) {
if (mixed === null) {
return true;
} else if (typeof mixed === "string") {
return true;
} else if (typeof mixed === "boolean") {
return true;
} else if (typeof mixed === "number") {
return true;
} else if (mixed instanceof Color) {
return true;
} else if (mixed instanceof Collator) {
return true;
} else if (mixed instanceof Formatted) {
return true;
} else if (mixed instanceof ResolvedImage) {
return true;
} else if (Array.isArray(mixed)) {
for (const item of mixed) {
if (!isValue(item)) {
return false;
}
}
return true;
} else if (typeof mixed === "object") {
for (const key in mixed) {
if (!isValue(mixed[key])) {
return false;
}
}
return true;
} else {
return false;
}
}
function typeOf(value) {
if (value === null) {
return NullType;
} else if (typeof value === "string") {
return StringType;
} else if (typeof value === "boolean") {
return BooleanType;
} else if (typeof value === "number") {
return NumberType;
} else if (value instanceof Color) {
return ColorType;
} else if (value instanceof Collator) {
return CollatorType;
} else if (value instanceof Formatted) {
return FormattedType;
} else if (value instanceof ResolvedImage) {
return ResolvedImageType;
} else if (Array.isArray(value)) {
const length = value.length;
let itemType;
for (const item of value) {
const t = typeOf(item);
if (!itemType) {
itemType = t;
} else if (itemType === t) {
continue;
} else {
itemType = ValueType;
break;
}
}
return array(itemType || ValueType, length);
} else {
assert$1(typeof value === "object");
return ObjectType;
}
}
function toString(value) {
const type = typeof value;
if (value === null) {
return "";
} else if (type === "string" || type === "number" || type === "boolean") {
return String(value);
} else if (value instanceof Formatted || value instanceof ResolvedImage || value instanceof Color) {
return value.toString();
} else {
return JSON.stringify(value);
}
}
class Literal {
constructor(type, value) {
this.type = type;
this.value = value;
}
static parse(args, context) {
if (args.length !== 2)
return context.error(`'literal' expression requires exactly one argument, but found ${args.length - 1} instead.`);
if (!isValue(args[1]))
return context.error(`invalid value`);
const value = args[1];
let type = typeOf(value);
const expected = context.expectedType;
if (type.kind === "array" && type.N === 0 && expected && expected.kind === "array" && (typeof expected.N !== "number" || expected.N === 0)) {
type = expected;
}
return new Literal(type, value);
}
evaluate() {
return this.value;
}
eachChild() {
}
outputDefined() {
return true;
}
serialize() {
if (this.type.kind === "array" || this.type.kind === "object") {
return ["literal", this.value];
} else if (this.value instanceof Color) {
return ["rgba"].concat(this.value.toNonPremultipliedRenderColor(null).toArray());
} else if (this.value instanceof Formatted) {
return this.value.serialize();
} else {
assert$1(this.value === null || typeof this.value === "string" || typeof this.value === "number" || typeof this.value === "boolean");
return this.value;
}
}
}
class RuntimeError {
constructor(message) {
this.name = "ExpressionEvaluationError";
this.message = message;
}
toJSON() {
return this.message;
}
}
const types$1 = {
string: StringType,
number: NumberType,
boolean: BooleanType,
object: ObjectType
};
class Assertion {
constructor(type, args) {
this.type = type;
this.args = args;
}
static parse(args, context) {
if (args.length < 2)
return context.error(`Expected at least one argument.`);
let i = 1;
let type;
const name = args[0];
if (name === "array") {
let itemType;
if (args.length > 2) {
const type2 = args[1];
if (typeof type2 !== "string" || !(type2 in types$1) || type2 === "object")
return context.error('The item type argument of "array" must be one of string, number, boolean', 1);
itemType = types$1[type2];
i++;
} else {
itemType = ValueType;
}
let N;
if (args.length > 3) {
if (args[2] !== null && (typeof args[2] !== "number" || args[2] < 0 || args[2] !== Math.floor(args[2]))) {
return context.error('The length argument to "array" must be a positive integer literal', 2);
}
N = args[2];
i++;
}
type = array(itemType, N);
} else {
assert$1(types$1[name], name);
type = types$1[name];
}
const parsed = [];
for (; i < args.length; i++) {
const input = context.parse(args[i], i, ValueType);
if (!input) return null;
parsed.push(input);
}
return new Assertion(type, parsed);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
for (let i = 0; i < this.args.length; i++) {
const value = this.args[i].evaluate(ctx);
const error = checkSubtype(this.type, typeOf(value));
if (!error) {
return value;
} else if (i === this.args.length - 1) {
throw new RuntimeError(`The expression ${JSON.stringify(this.args[i].serialize())} evaluated to ${toString$1(typeOf(value))} but was expected to be of type ${toString$1(this.type)}.`);
}
}
assert$1(false);
return null;
}
eachChild(fn) {
this.args.forEach(fn);
}
outputDefined() {
return this.args.every((arg) => arg.outputDefined());
}
serialize() {
const type = this.type;
const serialized = [type.kind];
if (type.kind === "array") {
const itemType = type.itemType;
if (itemType.kind === "string" || itemType.kind === "number" || itemType.kind === "boolean") {
serialized.push(itemType.kind);
const N = type.N;
if (typeof N === "number" || this.args.length > 1) {
serialized.push(N);
}
}
}
return serialized.concat(this.args.map((arg) => arg.serialize()));
}
}
class FormatExpression {
constructor(sections) {
this.type = FormattedType;
this.sections = sections;
}
static parse(args, context) {
if (args.length < 2) {
return context.error(`Expected at least one argument.`);
}
const firstArg = args[1];
if (!Array.isArray(firstArg) && typeof firstArg === "object") {
return context.error(`First argument must be an image or text section.`);
}
const sections = [];
let nextTokenMayBeObject = false;
for (let i = 1; i <= args.length - 1; ++i) {
const arg = args[i];
if (nextTokenMayBeObject && typeof arg === "object" && !Array.isArray(arg)) {
nextTokenMayBeObject = false;
let scale = null;
if (arg["font-scale"]) {
scale = context.parseObjectValue(arg["font-scale"], i, "font-scale", NumberType);
if (!scale) return null;
}
let font = null;
if (arg["text-font"]) {
font = context.parseObjectValue(arg["text-font"], i, "text-font", array(StringType));
if (!font) return null;
}
let textColor = null;
if (arg["text-color"]) {
textColor = context.parseObjectValue(arg["text-color"], i, "text-color", ColorType);
if (!textColor) return null;
}
const lastExpression = sections[sections.length - 1];
lastExpression.scale = scale;
lastExpression.font = font;
lastExpression.textColor = textColor;
} else {
const content = context.parse(args[i], i, ValueType);
if (!content) return null;
const kind = content.type.kind;
if (kind !== "string" && kind !== "value" && kind !== "null" && kind !== "resolvedImage")
return context.error(`Formatted text type must be 'string', 'value', 'image' or 'null'.`);
nextTokenMayBeObject = true;
sections.push({ content, scale: null, font: null, textColor: null });
}
}
return new FormatExpression(sections);
}
evaluate(ctx) {
const evaluateSection = (section) => {
const evaluatedContent = section.content.evaluate(ctx);
if (typeEquals(typeOf(evaluatedContent), ResolvedImageType)) {
return new FormattedSection("", evaluatedContent, null, null, null);
}
return new FormattedSection(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
toString(evaluatedContent),
null,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
section.scale ? section.scale.evaluate(ctx) : null,
section.font ? section.font.evaluate(ctx).join(",") : null,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
section.textColor ? section.textColor.evaluate(ctx) : null
);
};
return new Formatted(this.sections.map(evaluateSection));
}
eachChild(fn) {
for (const section of this.sections) {
fn(section.content);
if (section.scale) {
fn(section.scale);
}
if (section.font) {
fn(section.font);
}
if (section.textColor) {
fn(section.textColor);
}
}
}
outputDefined() {
return false;
}
serialize() {
const serialized = ["format"];
for (const section of this.sections) {
serialized.push(section.content.serialize());
const options = {};
if (section.scale) {
options["font-scale"] = section.scale.serialize();
}
if (section.font) {
options["text-font"] = section.font.serialize();
}
if (section.textColor) {
options["text-color"] = section.textColor.serialize();
}
serialized.push(options);
}
return serialized;
}
}
function isImageOptions(value) {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
class ImageExpression {
constructor(inputPrimary, inputSecondary, inputPrimaryOptions, inputSecondaryOptions) {
this._imageWarnHistory = {};
this.type = ResolvedImageType;
this.namePrimary = inputPrimary;
this.nameSecondary = inputSecondary;
if (inputPrimaryOptions) {
this.paramsPrimary = inputPrimaryOptions.params;
this.iconsetIdPrimary = inputPrimaryOptions.iconset ? inputPrimaryOptions.iconset.id : void 0;
}
if (inputSecondaryOptions) {
this.paramsSecondary = inputSecondaryOptions.params;
this.iconsetIdSecondary = inputSecondaryOptions.iconset ? inputSecondaryOptions.iconset.id : void 0;
}
}
static parse(args, context) {
if (args.length < 2) {
return context.error(`Expected two or more arguments.`);
}
let nextArgId = 1;
const imageExpression = [];
function tryParseImage() {
if (nextArgId < args.length) {
const imageName = context.parse(args[nextArgId], nextArgId++, StringType);
if (!imageName) {
context.error(imageExpression.length ? `Secondary image variant is not a string.` : `No image name provided.`);
return false;
}
imageExpression.push({ image: imageName, options: {} });
return true;
}
return true;
}
function tryParseOptions() {
if (nextArgId < args.length) {
const options = args[nextArgId];
if (!isImageOptions(options)) {
return true;
}
const params = options.params;
const iconset = options.iconset;
const optionsContext = context.concat(nextArgId);
if (!params && !iconset) {
nextArgId++;
return true;
}
if (params) {
if (typeof params !== "object" || params.constructor !== Object) {
optionsContext.error(`Image options "params" should be an object`);
return false;
}
const parsedParams = {};
const childContext = optionsContext.concat(void 0, "params");
for (const key in params) {
if (!key) {
childContext.error(`Image parameter name should be non-empty`);
return false;
}
const value = childContext.concat(void 0, key).parse(params[key], void 0, ColorType, void 0, { typeAnnotation: "coerce" });
if (!value) {
return false;
}
parsedParams[key] = value;
}
imageExpression[imageExpression.length - 1].options.params = parsedParams;
}
if (iconset) {
if (typeof iconset !== "object" || iconset.constructor !== Object) {
optionsContext.error(`Image options "iconset" should be an object`);
return false;
}
if (!iconset.id) {
optionsContext.error(`Image options "iconset" should have an "id" property`);
return false;
}
imageExpression[imageExpression.length - 1].options.iconset = iconset;
}
nextArgId++;
return true;
}
return true;
}
for (let i = 0; i < 2; i++) {
if (!tryParseImage() || !tryParseOptions()) {
return;
}
}
return new ImageExpression(
imageExpression[0].image,
imageExpression[1] ? imageExpression[1].image : void 0,
imageExpression[0].options,
imageExpression[1] ? imageExpression[1].options : void 0
);
}
evaluateParams(ctx, params) {
const result = {};
if (params) {
for (const key in params) {
if (params[key]) {
try {
result[key] = params[key].evaluate(ctx);
} catch (err) {
continue;
}
}
}
} else {
return void 0;
}
if (Object.keys(result).length === 0) {
return void 0;
}
return { params: result };
}
evaluate(ctx) {
const primaryId = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
name: this.namePrimary.evaluate(ctx),
iconsetId: this.iconsetIdPrimary
};
const secondaryId = this.nameSecondary ? {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
name: this.nameSecondary.evaluate(ctx),
iconsetId: this.iconsetIdSecondary
} : void 0;
const value = ResolvedImage.build(
primaryId,
secondaryId,
this.paramsPrimary ? this.evaluateParams(ctx, this.paramsPrimary) : void 0,
this.paramsSecondary ? this.evaluateParams(ctx, this.paramsSecondary) : void 0
);
if (value && ctx.availableImages) {
const primaryId2 = value.getPrimary().id;
value.available = ctx.availableImages.some((id) => ImageId.isEqual(id, primaryId2));
if (value.available) {
const secondaryId2 = value.getSecondary() ? value.getSecondary().id : null;
if (secondaryId2) value.available = ctx.availableImages.some((id) => ImageId.isEqual(id, secondaryId2));
}
}
return value;
}
eachChild(fn) {
fn(this.namePrimary);
if (this.paramsPrimary) {
for (const key in this.paramsPrimary) {
if (this.paramsPrimary[key]) {
fn(this.paramsPrimary[key]);
}
}
}
if (this.nameSecondary) {
fn(this.nameSecondary);
if (this.paramsSecondary) {
for (const key in this.paramsSecondary) {
if (this.paramsSecondary[key]) {
fn(this.paramsSecondary[key]);
}
}
}
}
}
outputDefined() {
return false;
}
serializeOptions(params, iconsetId) {
const result = {};
if (iconsetId) {
result.iconset = { id: iconsetId };
}
if (params) {
result.params = {};
for (const key in params) {
if (params[key]) {
result.params[key] = params[key].serialize();
}
}
}
return Object.keys(result).length > 0 ? result : void 0;
}
serialize() {
const serialized = ["image", this.namePrimary.serialize()];
if (this.paramsPrimary || this.iconsetIdPrimary) {
const options = this.serializeOptions(this.paramsPrimary, this.iconsetIdPrimary);
if (options) serialized.push(options);
}
if (this.nameSecondary) {
serialized.push(this.nameSecondary.serialize());
if (this.paramsSecondary || this.iconsetIdSecondary) {
const options = this.serializeOptions(this.paramsSecondary, this.iconsetIdSecondary);
if (options) serialized.push(options);
}
}
return serialized;
}
}
function getType(val) {
if (isString(val)) return "string";
if (isNumber(val)) return "number";
if (isBoolean(val)) return "boolean";
if (Array.isArray(val)) return "array";
if (val === null) return "null";
if (isObject(val)) return "object";
return typeof val;
}
function isObject(value) {
if (value === null || value === void 0) return false;
if (Array.isArray(value)) return false;
if (typeof value === "function") return false;
if (value instanceof String || value instanceof Number || value instanceof Boolean) {
return false;
}
return typeof value === "object";
}
function isString(value) {
return typeof value === "string" || value instanceof String;
}
function isNumber(value) {
return typeof value === "number" || value instanceof Number;
}
function isBoolean(value) {
return typeof value === "boolean" || value instanceof Boolean;
}
const types = {
"to-boolean": BooleanType,
"to-color": ColorType,
"to-number": NumberType,
"to-string": StringType
};
class Coercion {
constructor(type, args) {
this.type = type;
this.args = args;
}
static parse(args, context) {
if (args.length < 2)
return context.error(`Expected at least one argument.`);
const name = args[0];
const parsed = [];
let type = NullType;
if (name === "to-array") {
if (!Array.isArray(args[1])) {
return null;
}
const arrayLength = args[1].length;
if (context.expectedType) {
if (context.expectedType.kind === "array") {
type = array(context.expectedType.itemType, arrayLength);
} else {
return context.error(`Expected ${context.expectedType.kind} but found array.`);
}
} else if (arrayLength > 0 && isValue(args[1][0])) {
const value = args[1][0];
type = array(typeOf(value), arrayLength);
} else {
return null;
}
for (let i = 0; i < arrayLength; i++) {
const member = args[1][i];
let parsedMember;
if (Array.isArray(member)) {
parsedMember = context.parse(member, void 0, type.itemType);
} else {
const memberType = getType(member);
if (memberType !== type.itemType.kind) {
return context.error(`Expected ${type.itemType.kind} but found ${memberType}.`);
}
parsedMember = context.registry["literal"].parse(["literal", member === void 0 ? null : member], context);
}
if (!parsedMember) return null;
parsed.push(parsedMember);
}
} else {
assert$1(types[name], name);
if ((name === "to-boolean" || name === "to-string") && args.length !== 2)
return context.error(`Expected one argument.`);
type = types[name];
for (let i = 1; i < args.length; i++) {
const input = context.parse(args[i], i, ValueType);
if (!input) return null;
parsed.push(input);
}
}
return new Coercion(type, parsed);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
if (this.type.kind === "boolean") {
return Boolean(this.args[0].evaluate(ctx));
} else if (this.type.kind === "color") {
let input;
let error;
for (const arg of this.args) {
input = arg.evaluate(ctx);
error = null;
if (input instanceof Color) {
return input;
} else if (typeof input === "string") {
const c = ctx.parseColor(input);
if (c) return c;
} else if (Array.isArray(input)) {
if (input.length < 3 || input.length > 4) {
error = `Invalid rbga value ${JSON.stringify(input)}: expected an array containing either three or four numeric values.`;
} else {
error = validateRGBA(input[0], input[1], input[2], input[3]);
}
if (!error) {
return new Color(input[0] / 255, input[1] / 255, input[2] / 255, input[3]);
}
}
}
throw new RuntimeError(error || `Could not parse color from value '${typeof input === "string" ? input : String(JSON.stringify(input))}'`);
} else if (this.type.kind === "number") {
let value = null;
for (const arg of this.args) {
value = arg.evaluate(ctx);
if (value === null) return 0;
const num = Number(value);
if (isNaN(num)) continue;
return num;
}
throw new RuntimeError(`Could not convert ${JSON.stringify(value)} to number.`);
} else if (this.type.kind === "formatted") {
return Formatted.fromString(toString(this.args[0].evaluate(ctx)));
} else if (this.type.kind === "resolvedImage") {
return ResolvedImage.build(toString(this.args[0].evaluate(ctx)));
} else if (this.type.kind === "array") {
return this.args.map((arg) => {
return arg.evaluate(ctx);
});
} else {
return toString(this.args[0].evaluate(ctx));
}
}
eachChild(fn) {
this.args.forEach(fn);
}
outputDefined() {
return this.args.every((arg) => arg.outputDefined());
}
serialize() {
if (this.type.kind === "formatted") {
return new FormatExpression([{ content: this.args[0], scale: null, font: null, textColor: null }]).serialize();
}
if (this.type.kind === "resolvedImage") {
return new ImageExpression(this.args[0]).serialize();
}
const serialized = this.type.kind === "array" ? [] : [`to-${this.type.kind}`];
this.eachChild((child) => {
serialized.push(child.serialize());
});
return serialized;
}
}
const geometryTypes = ["Unknown", "Point", "LineString", "Polygon"];
class EvaluationContext {
constructor(scope, options, iconImageUseTheme) {
this.globals = null;
this.feature = null;
this.featureState = null;
this.formattedSection = null;
this._parseColorCache = {};
this.availableImages = null;
this.canonical = null;
this.featureTileCoord = null;
this.featureDistanceData = null;
this.scope = scope;
this.options = options;
this.iconImageUseTheme = iconImageUseTheme;
}
id() {
return this.feature && this.feature.id !== void 0 ? this.feature.id : null;
}
geometryType() {
return this.feature ? typeof this.feature.type === "number" ? geometryTypes[this.feature.type] : this.feature.type : null;
}
geometry() {
return this.feature && "geometry" in this.feature ? this.feature.geometry : null;
}
canonicalID() {
return this.canonical;
}
properties() {
return this.feature && this.feature.properties || {};
}
measureLight(_) {
return this.globals.brightness || 0;
}
distanceFromCenter() {
if (this.featureTileCoord && this.featureDistanceData) {
const c = this.featureDistanceData.center;
const scale = this.featureDistanceData.scale;
const { x, y } = this.featureTileCoord;
const dX = x * scale - c[0];
const dY = y * scale - c[1];
const bX = this.featureDistanceData.bearing[0];
const bY = this.featureDistanceData.bearing[1];
const dist = bX * dX + bY * dY;
return dist;
}
return 0;
}
parseColor(input) {
let cached = this._parseColorCache[input];
if (!cached) {
cached = this._parseColorCache[input] = Color.parse(input);
}
return cached;
}
getConfig(id) {
return this.options ? this.options.get(id) : null;
}
}
class CompoundExpression {
constructor(name, type, evaluate, args, overloadIndex) {
this.name = name;
this.type = type;
this._evaluate = evaluate;
this.args = args;
this._overloadIndex = overloadIndex;
}
evaluate(ctx) {
if (!this._evaluate) {
const definition = CompoundExpression.definitions[this.name];
this._evaluate = Array.isArray(definition) ? definition[2] : definition.overloads[this._overloadIndex][1];
}
return this._evaluate(ctx, this.args);
}
eachChild(fn) {
this.args.forEach(fn);
}
outputDefined() {
return false;
}
serialize() {
return [this.name].concat(this.args.map((arg) => arg.serialize()));
}
static parse(args, context) {
const op = args[0];
const definition = CompoundExpression.definitions[op];
if (!definition) {
return context.error(`Unknown expression "${op}". If you wanted a literal array, use ["literal", [...]].`, 0);
}
const type = Array.isArray(definition) ? definition[0] : definition.type;
const availableOverloads = Array.isArray(definition) ? [[definition[1], definition[2]]] : definition.overloads;
const overloadParams = [];
let signatureContext = null;
let overloadIndex = -1;
for (const [params, evaluate] of availableOverloads) {
if (Array.isArray(params) && params.length !== args.length - 1) continue;
overloadParams.push(params);
overloadIndex++;
signatureContext = new ParsingContext(context.registry, context.path, null, context.scope, void 0, context._scope, context.options, context.iconImageUseTheme);
const parsedArgs = [];
let argParseFailed = false;
for (let i = 1; i < args.length; i++) {
const arg = args[i];
const expectedType = Array.isArray(params) ? params[i - 1] : (
// @ts-expect-error - TS2339 - Property 'type' does not exist on type 'Varargs | Evaluate'.
params.type
);
const parsed = signatureContext.parse(arg, 1 + parsedArgs.length, expectedType);
if (!parsed) {
argParseFailed = true;
break;
}
parsedArgs.push(parsed);
}
if (argParseFailed) {
continue;
}
if (Array.isArray(params)) {
if (params.length !== parsedArgs.length) {
signatureContext.error(`Expected ${params.length} arguments, but found ${parsedArgs.length} instead.`);
continue;
}
}
for (let i = 0; i < parsedArgs.length; i++) {
const expected = Array.isArray(params) ? params[i] : params.type;
const arg = parsedArgs[i];
signatureContext.concat(i + 1).checkSubtype(expected, arg.type);
}
if (signatureContext.errors.length === 0) {
return new CompoundExpression(op, type, evaluate, parsedArgs, overloadIndex);
}
}
assert$1(!signatureContext || signatureContext.errors.length > 0);
if (overloadParams.length === 1) {
context.errors.push(...signatureContext.errors);
} else {
const expected = overloadParams.length ? overloadParams : availableOverloads.map(([params]) => params);
const signatures = expected.map(stringifySignature).join(" | ");
const actualTypes = [];
for (let i = 1; i < args.length; i++) {
const parsed = context.parse(args[i], 1 + actualTypes.length);
if (!parsed) return null;
actualTypes.push(toString$1(parsed.type));
}
context.error(`Expected arguments of type ${signatures}, but found (${actualTypes.join(", ")}) instead.`);
}
return null;
}
static register(registry, definitions) {
assert$1(!CompoundExpression.definitions);
CompoundExpression.definitions = definitions;
for (const name in definitions) {
registry[name] = CompoundExpression;
}
}
}
function stringifySignature(signature) {
if (Array.isArray(signature)) {
return `(${signature.map(toString$1).join(", ")})`;
} else {
return `(${toString$1(signature.type)}...)`;
}
}
class CollatorExpression {
constructor(caseSensitive, diacriticSensitive, locale) {
this.type = CollatorType;
this.locale = locale;
this.caseSensitive = caseSensitive;
this.diacriticSensitive = diacriticSensitive;
}
static parse(args, context) {
if (args.length !== 2)
return context.error(`Expected one argument.`);
const options = args[1];
if (typeof options !== "object" || Array.isArray(options))
return context.error(`Collator options argument must be an object.`);
const caseSensitive = options["case-sensitive"] === void 0 ? context.parse(false, 1, BooleanType) : context.parseObjectValue(options["case-sensitive"], 1, "case-sensitive", BooleanType);
if (!caseSensitive) return null;
const diacriticSensitive = options["diacritic-sensitive"] === void 0 ? context.parse(false, 1, BooleanType) : context.parseObjectValue(options["diacritic-sensitive"], 1, "diacritic-sensitive", BooleanType);
if (!diacriticSensitive) return null;
let locale = null;
if (options["locale"]) {
locale = context.parseObjectValue(options["locale"], 1, "locale", StringType);
if (!locale) return null;
}
return new CollatorExpression(caseSensitive, diacriticSensitive, locale);
}
evaluate(ctx) {
return new Collator(
this.caseSensitive.evaluate(ctx),
this.diacriticSensitive.evaluate(ctx),
this.locale ? this.locale.evaluate(ctx) : null
);
}
eachChild(fn) {
fn(this.caseSensitive);
fn(this.diacriticSensitive);
if (this.locale) {
fn(this.locale);
}
}
outputDefined() {
return false;
}
serialize() {
const options = {};
options["case-sensitive"] = this.caseSensitive.serialize();
options["diacritic-sensitive"] = this.diacriticSensitive.serialize();
if (this.locale) {
options["locale"] = this.locale.serialize();
}
return ["collator", options];
}
}
/**
* Rearranges items so that all items in the [left, k] are the smallest.
* The k-th element will have the (k - left + 1)-th smallest value in [left, right].
*
* @template T
* @param {T[]} arr the array to partially sort (in place)
* @param {number} k middle index for partial sorting (as defined above)
* @param {number} [left=0] left index of the range to sort
* @param {number} [right=arr.length-1] right index
* @param {(a: T, b: T) => number} [compare = (a, b) => a - b] compare function
*/
function quickselect(arr, k, left = 0, right = arr.length - 1, compare = defaultCompare) {
while (right > left) {
if (right - left > 600) {
const n = right - left + 1;
const m = k - left + 1;
const z = Math.log(n);
const s = 0.5 * Math.exp(2 * z / 3);
const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
quickselect(arr, k, newLeft, newRight, compare);
}
const t = arr[k];
let i = left;
/** @type {number} */
let j = right;
swap$2(arr, left, k);
if (compare(arr[right], t) > 0) swap$2(arr, left, right);
while (i < j) {
swap$2(arr, i, j);
i++;
j--;
while (compare(arr[i], t) < 0) i++;
while (compare(arr[j], t) > 0) j--;
}
if (compare(arr[left], t) === 0) swap$2(arr, left, j);
else {
j++;
swap$2(arr, j, right);
}
if (j <= k) left = j + 1;
if (k <= j) right = j - 1;
}
}
/**
* @template T
* @param {T[]} arr
* @param {number} i
* @param {number} j
*/
function swap$2(arr, i, j) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/**
* @template T
* @param {T} a
* @param {T} b
* @returns {number}
*/
function defaultCompare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
function calculateSignedArea(ring) {
let sum = 0;
for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
p1 = ring[i];
p2 = ring[j];
sum += (p2.x - p1.x) * (p1.y + p2.y);
}
return sum;
}
function compareAreas$1(a, b) {
return b.area - a.area;
}
function classifyRings$2(rings, maxRings) {
const len = rings.length;
if (len <= 1) return [rings];
const polygons = [];
let polygon, ccw;
for (let i = 0; i < len; i++) {
const area = calculateSignedArea(rings[i]);
if (area === 0) continue;
rings[i].area = Math.abs(area);
if (ccw === void 0) ccw = area < 0;
if (ccw === area < 0) {
if (polygon) polygons.push(polygon);
polygon = [rings[i]];
} else {
polygon.push(rings[i]);
}
}
if (polygon) polygons.push(polygon);
if (maxRings > 1) {
for (let j = 0; j < polygons.length; j++) {
if (polygons[j].length <= maxRings) continue;
quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas$1);
polygons[j] = polygons[j].slice(0, maxRings);
}
}
return polygons;
}
function updateBBox(bbox, coord) {
bbox[0] = Math.min(bbox[0], coord[0]);
bbox[1] = Math.min(bbox[1], coord[1]);
bbox[2] = Math.max(bbox[2], coord[0]);
bbox[3] = Math.max(bbox[3], coord[1]);
}
function boxWithinBox(bbox1, bbox2) {
if (bbox1[0] <= bbox2[0]) return false;
if (bbox1[2] >= bbox2[2]) return false;
if (bbox1[1] <= bbox2[1]) return false;
if (bbox1[3] >= bbox2[3]) return false;
return true;
}
function onBoundary(p, p1, p2) {
const x1 = p[0] - p1[0];
const y1 = p[1] - p1[1];
const x2 = p[0] - p2[0];
const y2 = p[1] - p2[1];
return x1 * y2 - x2 * y1 === 0 && x1 * x2 <= 0 && y1 * y2 <= 0;
}
function rayIntersect(p, p1, p2) {
return p1[1] > p[1] !== p2[1] > p[1] && p[0] < (p2[0] - p1[0]) * (p[1] - p1[1]) / (p2[1] - p1[1]) + p1[0];
}
function pointWithinPolygon(point, rings, trueOnBoundary = false) {
let inside = false;
for (let i = 0, len = rings.length; i < len; i++) {
const ring = rings[i];
for (let j = 0, len2 = ring.length, k = len2 - 1; j < len2; k = j++) {
const q1 = ring[k];
const q2 = ring[j];
if (onBoundary(point, q1, q2)) return trueOnBoundary;
if (rayIntersect(point, q1, q2)) inside = !inside;
}
}
return inside;
}
function perp(v1, v2) {
return v1[0] * v2[1] - v1[1] * v2[0];
}
function twoSided(p1, p2, q1, q2) {
const x1 = p1[0] - q1[0];
const y1 = p1[1] - q1[1];
const x2 = p2[0] - q1[0];
const y2 = p2[1] - q1[1];
const x3 = q2[0] - q1[0];
const y3 = q2[1] - q1[1];
const det1 = x1 * y3 - x3 * y1;
const det2 = x2 * y3 - x3 * y2;
if (det1 > 0 && det2 < 0 || det1 < 0 && det2 > 0) return true;
return false;
}
function segmentIntersectSegment(a, b, c, d) {
const vectorP = [b[0] - a[0], b[1] - a[1]];
const vectorQ = [d[0] - c[0], d[1] - c[1]];
if (perp(vectorQ, vectorP) === 0) return false;
if (twoSided(a, b, c, d) && twoSided(c, d, a, b)) return true;
return false;
}
function computeBounds(points) {
const min = new Point(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY);
const max = new Point(Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY);
for (const point of points[0]) {
if (min.x > point.x) min.x = point.x;
if (min.y > point.y) min.y = point.y;
if (max.x < point.x) max.x = point.x;
if (max.y < point.y) max.y = point.y;
}
return { min, max };
}
const EXTENT$1 = 8192;
function mercatorXfromLng$1(lng) {
return (180 + lng) / 360;
}
function mercatorYfromLat$1(lat) {
return (180 - 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360))) / 360;
}
function getTileCoordinates(p, canonical) {
const x = mercatorXfromLng$1(p[0]);
const y = mercatorYfromLat$1(p[1]);
const tilesAtZoom = Math.pow(2, canonical.z);
return [Math.round(x * tilesAtZoom * EXTENT$1), Math.round(y * tilesAtZoom * EXTENT$1)];
}
function pointWithinPolygons(point, polygons) {
for (let i = 0; i < polygons.length; i++) {
if (pointWithinPolygon(point, polygons[i])) return true;
}
return false;
}
function lineIntersectPolygon(p1, p2, polygon) {
for (const ring of polygon) {
for (let j = 0, len = ring.length, k = len - 1; j < len; k = j++) {
const q1 = ring[k];
const q2 = ring[j];
if (segmentIntersectSegment(p1, p2, q1, q2)) {
return true;
}
}
}
return false;
}
function lineStringWithinPolygon(line, polygon) {
for (let i = 0; i < line.length; ++i) {
if (!pointWithinPolygon(line[i], polygon)) {
return false;
}
}
for (let i = 0; i < line.length - 1; ++i) {
if (lineIntersectPolygon(line[i], line[i + 1], polygon)) {
return false;
}
}
return true;
}
function lineStringWithinPolygons(line, polygons) {
for (let i = 0; i < polygons.length; i++) {
if (lineStringWithinPolygon(line, polygons[i])) return true;
}
return false;
}
function getTilePolygon(coordinates, bbox, canonical) {
const polygon = [];
for (let i = 0; i < coordinates.length; i++) {
const ring = [];
for (let j = 0; j < coordinates[i].length; j++) {
const coord = getTileCoordinates(coordinates[i][j], canonical);
updateBBox(bbox, coord);
ring.push(coord);
}
polygon.push(ring);
}
return polygon;
}
function getTilePolygons(coordinates, bbox, canonical) {
const polygons = [];
for (let i = 0; i < coordinates.length; i++) {
const polygon = getTilePolygon(coordinates[i], bbox, canonical);
polygons.push(polygon);
}
return polygons;
}
function updatePoint(p, bbox, polyBBox, worldSize) {
if (p[0] < polyBBox[0] || p[0] > polyBBox[2]) {
const halfWorldSize = worldSize * 0.5;
let shift = p[0] - polyBBox[0] > halfWorldSize ? -worldSize : polyBBox[0] - p[0] > halfWorldSize ? worldSize : 0;
if (shift === 0) {
shift = p[0] - polyBBox[2] > halfWorldSize ? -worldSize : polyBBox[2] - p[0] > halfWorldSize ? worldSize : 0;
}
p[0] += shift;
}
updateBBox(bbox, p);
}
function resetBBox(bbox) {
bbox[0] = bbox[1] = Infinity;
bbox[2] = bbox[3] = -Infinity;
}
function getTilePoints(geometry, pointBBox, polyBBox, canonical) {
const worldSize = Math.pow(2, canonical.z) * EXTENT$1;
const shifts = [canonical.x * EXTENT$1, canonical.y * EXTENT$1];
const tilePoints = [];
if (!geometry) return tilePoints;
for (const points of geometry) {
for (const point of points) {
const p = [point.x + shifts[0], point.y + shifts[1]];
updatePoint(p, pointBBox, polyBBox, worldSize);
tilePoints.push(p);
}
}
return tilePoints;
}
function getTileLines(geometry, lineBBox, polyBBox, canonical) {
const worldSize = Math.pow(2, canonical.z) * EXTENT$1;
const shifts = [canonical.x * EXTENT$1, canonical.y * EXTENT$1];
const tileLines = [];
if (!geometry) return tileLines;
for (const line of geometry) {
const tileLine = [];
for (const point of line) {
const p = [point.x + shifts[0], point.y + shifts[1]];
updateBBox(lineBBox, p);
tileLine.push(p);
}
tileLines.push(tileLine);
}
if (lineBBox[2] - lineBBox[0] <= worldSize / 2) {
resetBBox(lineBBox);
for (const line of tileLines) {
for (const p of line) {
updatePoint(p, lineBBox, polyBBox, worldSize);
}
}
}
return tileLines;
}
function pointsWithinPolygons(ctx, polygonGeometry) {
const pointBBox = [Infinity, Infinity, -Infinity, -Infinity];
const polyBBox = [Infinity, Infinity, -Infinity, -Infinity];
const canonical = ctx.canonicalID();
if (!canonical) {
return false;
}
if (polygonGeometry.type === "Polygon") {
const tilePolygon = getTilePolygon(polygonGeometry.coordinates, polyBBox, canonical);
const tilePoints = getTilePoints(ctx.geometry(), pointBBox, polyBBox, canonical);
if (!boxWithinBox(pointBBox, polyBBox)) return false;
for (const point of tilePoints) {
if (!pointWithinPolygon(point, tilePolygon)) return false;
}
}
if (polygonGeometry.type === "MultiPolygon") {
const tilePolygons = getTilePolygons(polygonGeometry.coordinates, polyBBox, canonical);
const tilePoints = getTilePoints(ctx.geometry(), pointBBox, polyBBox, canonical);
if (!boxWithinBox(pointBBox, polyBBox)) return false;
for (const point of tilePoints) {
if (!pointWithinPolygons(point, tilePolygons)) return false;
}
}
return true;
}
function linesWithinPolygons(ctx, polygonGeometry) {
const lineBBox = [Infinity, Infinity, -Infinity, -Infinity];
const polyBBox = [Infinity, Infinity, -Infinity, -Infinity];
const canonical = ctx.canonicalID();
if (!canonical) {
return false;
}
if (polygonGeometry.type === "Polygon") {
const tilePolygon = getTilePolygon(polygonGeometry.coordinates, polyBBox, canonical);
const tileLines = getTileLines(ctx.geometry(), lineBBox, polyBBox, canonical);
if (!boxWithinBox(lineBBox, polyBBox)) return false;
for (const line of tileLines) {
if (!lineStringWithinPolygon(line, tilePolygon)) return false;
}
}
if (polygonGeometry.type === "MultiPolygon") {
const tilePolygons = getTilePolygons(polygonGeometry.coordinates, polyBBox, canonical);
const tileLines = getTileLines(ctx.geometry(), lineBBox, polyBBox, canonical);
if (!boxWithinBox(lineBBox, polyBBox)) return false;
for (const line of tileLines) {
if (!lineStringWithinPolygons(line, tilePolygons)) return false;
}
}
return true;
}
class Within {
constructor(geojson, geometries) {
this.type = BooleanType;
this.geojson = geojson;
this.geometries = geometries;
}
static parse(args, context) {
if (args.length !== 2)
return context.error(`'within' expression requires exactly one argument, but found ${args.length - 1} instead.`);
if (isValue(args[1])) {
const geojson = args[1];
if (geojson.type === "FeatureCollection") {
for (let i = 0; i < geojson.features.length; ++i) {
const type = geojson.features[i].geometry.type;
if (type === "Polygon" || type === "MultiPolygon") {
return new Within(geojson, geojson.features[i].geometry);
}
}
} else if (geojson.type === "Feature") {
const type = geojson.geometry.type;
if (type === "Polygon" || type === "MultiPolygon") {
return new Within(geojson, geojson.geometry);
}
} else if (geojson.type === "Polygon" || geojson.type === "MultiPolygon") {
return new Within(geojson, geojson);
}
}
return context.error(`'within' expression requires valid geojson object that contains polygon geometry type.`);
}
evaluate(ctx) {
if (ctx.geometry() != null && ctx.canonicalID() != null) {
if (ctx.geometryType() === "Point") {
return pointsWithinPolygons(ctx, this.geometries);
} else if (ctx.geometryType() === "LineString") {
return linesWithinPolygons(ctx, this.geometries);
}
}
return false;
}
eachChild() {
}
outputDefined() {
return true;
}
serialize() {
return ["within", this.geojson];
}
}
const factors = {
kilometers: 1,
miles: 1000 / 1609.344,
nauticalmiles: 1000 / 1852,
meters: 1000,
metres: 1000,
yards: 1000 / 0.9144,
feet: 1000 / 0.3048,
inches: 1000 / 0.0254
};
// Values that define WGS84 ellipsoid model of the Earth
const RE = 6378.137; // equatorial radius
const FE = 1 / 298.257223563; // flattening
const E2 = FE * (2 - FE);
const RAD = Math.PI / 180;
/**
* A collection of very fast approximations to common geodesic measurements. Useful for performance-sensitive code that measures things on a city scale.
*/
class CheapRuler {
/**
* Creates a ruler object from tile coordinates (y and z).
*
* @param {number} y
* @param {number} z
* @param {keyof typeof factors} [units='kilometers']
* @returns {CheapRuler}
* @example
* const ruler = cheapRuler.fromTile(1567, 12);
* //=ruler
*/
static fromTile(y, z, units) {
const n = Math.PI * (1 - 2 * (y + 0.5) / Math.pow(2, z));
const lat = Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))) / RAD;
return new CheapRuler(lat, units);
}
/**
* Multipliers for converting between units.
*
* @example
* // convert 50 meters to yards
* 50 * CheapRuler.units.yards / CheapRuler.units.meters;
*/
static get units() {
return factors;
}
/**
* Creates a ruler instance for very fast approximations to common geodesic measurements around a certain latitude.
*
* @param {number} lat latitude
* @param {keyof typeof factors} [units='kilometers']
* @example
* const ruler = cheapRuler(35.05, 'miles');
* //=ruler
*/
constructor(lat, units) {
if (lat === undefined) throw new Error('No latitude given.');
if (units && !factors[units]) throw new Error(`Unknown unit ${ units }. Use one of: ${ Object.keys(factors).join(', ')}`);
// Curvature formulas from https://en.wikipedia.org/wiki/Earth_radius#Meridional
const m = RAD * RE * (units ? factors[units] : 1);
const coslat = Math.cos(lat * RAD);
const w2 = 1 / (1 - E2 * (1 - coslat * coslat));
const w = Math.sqrt(w2);
// multipliers for converting longitude and latitude degrees into distance
this.kx = m * w * coslat; // based on normal radius of curvature
this.ky = m * w * w2 * (1 - E2); // based on meridonal radius of curvature
}
/**
* Given two points of the form [longitude, latitude], returns the distance.
*
* @param {[number, number]} a point [longitude, latitude]
* @param {[number, number]} b point [longitude, latitude]
* @returns {number} distance
* @example
* const distance = ruler.distance([30.5, 50.5], [30.51, 50.49]);
* //=distance
*/
distance(a, b) {
const dx = wrap(a[0] - b[0]) * this.kx;
const dy = (a[1] - b[1]) * this.ky;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Returns the bearing between two points in angles.
*
* @param {[number, number]} a point [longitude, latitude]
* @param {[number, number]} b point [longitude, latitude]
* @returns {number} bearing
* @example
* const bearing = ruler.bearing([30.5, 50.5], [30.51, 50.49]);
* //=bearing
*/
bearing(a, b) {
const dx = wrap(b[0] - a[0]) * this.kx;
const dy = (b[1] - a[1]) * this.ky;
return Math.atan2(dx, dy) / RAD;
}
/**
* Returns a new point given distance and bearing from the starting point.
*
* @param {[number, number]} p point [longitude, latitude]
* @param {number} dist distance
* @param {number} bearing
* @returns {[number, number]} point [longitude, latitude]
* @example
* const point = ruler.destination([30.5, 50.5], 0.1, 90);
* //=point
*/
destination(p, dist, bearing) {
const a = bearing * RAD;
return this.offset(p,
Math.sin(a) * dist,
Math.cos(a) * dist);
}
/**
* Returns a new point given easting and northing offsets (in ruler units) from the starting point.
*
* @param {[number, number]} p point [longitude, latitude]
* @param {number} dx easting
* @param {number} dy northing
* @returns {[number, number]} point [longitude, latitude]
* @example
* const point = ruler.offset([30.5, 50.5], 10, 10);
* //=point
*/
offset(p, dx, dy) {
return [
p[0] + dx / this.kx,
p[1] + dy / this.ky
];
}
/**
* Given a line (an array of points), returns the total line distance.
*
* @param {[number, number][]} points [longitude, latitude]
* @returns {number} total line distance
* @example
* const length = ruler.lineDistance([
* [-67.031, 50.458], [-67.031, 50.534],
* [-66.929, 50.534], [-66.929, 50.458]
* ]);
* //=length
*/
lineDistance(points) {
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
total += this.distance(points[i], points[i + 1]);
}
return total;
}
/**
* Given a polygon (an array of rings, where each ring is an array of points), returns the area.
*
* @param {[number, number][][]} polygon
* @returns {number} area value in the specified units (square kilometers by default)
* @example
* const area = ruler.area([[
* [-67.031, 50.458], [-67.031, 50.534], [-66.929, 50.534],
* [-66.929, 50.458], [-67.031, 50.458]
* ]]);
* //=area
*/
area(polygon) {
let sum = 0;
for (let i = 0; i < polygon.length; i++) {
const ring = polygon[i];
for (let j = 0, len = ring.length, k = len - 1; j < len; k = j++) {
sum += wrap(ring[j][0] - ring[k][0]) * (ring[j][1] + ring[k][1]) * (i ? -1 : 1);
}
}
return (Math.abs(sum) / 2) * this.kx * this.ky;
}
/**
* Returns the point at a specified distance along the line.
*
* @param {[number, number][]} line
* @param {number} dist distance
* @returns {[number, number]} point [longitude, latitude]
* @example
* const point = ruler.along(line, 2.5);
* //=point
*/
along(line, dist) {
let sum = 0;
if (dist <= 0) return line[0];
for (let i = 0; i < line.length - 1; i++) {
const p0 = line[i];
const p1 = line[i + 1];
const d = this.distance(p0, p1);
sum += d;
if (sum > dist) return interpolate(p0, p1, (dist - (sum - d)) / d);
}
return line[line.length - 1];
}
/**
* Returns the distance from a point `p` to a line segment `a` to `b`.
*
* @pointToSegmentDistance
* @param {[number, number]} p point [longitude, latitude]
* @param {[number, number]} a segment point 1 [longitude, latitude]
* @param {[number, number]} b segment point 2 [longitude, latitude]
* @returns {number} distance
* @example
* const distance = ruler.pointToSegmentDistance([-67.04, 50.5], [-67.05, 50.57], [-67.03, 50.54]);
* //=distance
*/
pointToSegmentDistance(p, a, b) {
let [x, y] = a;
let dx = wrap(b[0] - x) * this.kx;
let dy = (b[1] - y) * this.ky;
if (dx !== 0 || dy !== 0) {
const t = (wrap(p[0] - x) * this.kx * dx + (p[1] - y) * this.ky * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = b[0];
y = b[1];
} else if (t > 0) {
x += (dx / this.kx) * t;
y += (dy / this.ky) * t;
}
}
dx = wrap(p[0] - x) * this.kx;
dy = (p[1] - y) * this.ky;
return Math.sqrt(dx * dx + dy * dy);
}
/**
* Returns an object of the form {point, index, t}, where point is closest point on the line
* from the given point, index is the start index of the segment with the closest point,
* and t is a parameter from 0 to 1 that indicates where the closest point is on that segment.
*
* @param {[number, number][]} line
* @param {[number, number]} p point [longitude, latitude]
* @returns {{point: [number, number], index: number, t: number}} {point, index, t}
* @example
* const point = ruler.pointOnLine(line, [-67.04, 50.5]).point;
* //=point
*/
pointOnLine(line, p) {
let minDist = Infinity;
let minX = line[0][0];
let minY = line[0][1];
let minI = 0;
let minT = 0;
for (let i = 0; i < line.length - 1; i++) {
let x = line[i][0];
let y = line[i][1];
let dx = wrap(line[i + 1][0] - x) * this.kx;
let dy = (line[i + 1][1] - y) * this.ky;
let t = 0;
if (dx !== 0 || dy !== 0) {
t = (wrap(p[0] - x) * this.kx * dx + (p[1] - y) * this.ky * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = line[i + 1][0];
y = line[i + 1][1];
} else if (t > 0) {
x += (dx / this.kx) * t;
y += (dy / this.ky) * t;
}
}
dx = wrap(p[0] - x) * this.kx;
dy = (p[1] - y) * this.ky;
const sqDist = dx * dx + dy * dy;
if (sqDist < minDist) {
minDist = sqDist;
minX = x;
minY = y;
minI = i;
minT = t;
}
}
return {
point: [minX, minY],
index: minI,
t: Math.max(0, Math.min(1, minT))
};
}
/**
* Returns a part of the given line between the start and the stop points (or their closest points on the line).
*
* @param {[number, number]} start point [longitude, latitude]
* @param {[number, number]} stop point [longitude, latitude]
* @param {[number, number][]} line
* @returns {[number, number][]} line part of a line
* @example
* const line2 = ruler.lineSlice([-67.04, 50.5], [-67.05, 50.56], line1);
* //=line2
*/
lineSlice(start, stop, line) {
let p1 = this.pointOnLine(line, start);
let p2 = this.pointOnLine(line, stop);
if (p1.index > p2.index || (p1.index === p2.index && p1.t > p2.t)) {
const tmp = p1;
p1 = p2;
p2 = tmp;
}
const slice = [p1.point];
const l = p1.index + 1;
const r = p2.index;
if (!equals$1(line[l], slice[0]) && l <= r)
slice.push(line[l]);
for (let i = l + 1; i <= r; i++) {
slice.push(line[i]);
}
if (!equals$1(line[r], p2.point))
slice.push(p2.point);
return slice;
}
/**
* Returns a part of the given line between the start and the stop points indicated by distance along the line.
*
* @param {number} start start distance
* @param {number} stop stop distance
* @param {[number, number][]} line
* @returns {[number, number][]} part of a line
* @example
* const line2 = ruler.lineSliceAlong(10, 20, line1);
* //=line2
*/
lineSliceAlong(start, stop, line) {
let sum = 0;
const slice = [];
for (let i = 0; i < line.length - 1; i++) {
const p0 = line[i];
const p1 = line[i + 1];
const d = this.distance(p0, p1);
sum += d;
if (sum > start && slice.length === 0) {
slice.push(interpolate(p0, p1, (start - (sum - d)) / d));
}
if (sum >= stop) {
slice.push(interpolate(p0, p1, (stop - (sum - d)) / d));
return slice;
}
if (sum > start) slice.push(p1);
}
return slice;
}
/**
* Given a point, returns a bounding box object ([w, s, e, n]) created from the given point buffered by a given distance.
*
* @param {[number, number]} p point [longitude, latitude]
* @param {number} buffer
* @returns {[number, number, number, number]} bbox ([w, s, e, n])
* @example
* const bbox = ruler.bufferPoint([30.5, 50.5], 0.01);
* //=bbox
*/
bufferPoint(p, buffer) {
const v = buffer / this.ky;
const h = buffer / this.kx;
return [
p[0] - h,
p[1] - v,
p[0] + h,
p[1] + v
];
}
/**
* Given a bounding box, returns the box buffered by a given distance.
*
* @param {[number, number, number, number]} bbox ([w, s, e, n])
* @param {number} buffer
* @returns {[number, number, number, number]} bbox ([w, s, e, n])
* @example
* const bbox = ruler.bufferBBox([30.5, 50.5, 31, 51], 0.2);
* //=bbox
*/
bufferBBox(bbox, buffer) {
const v = buffer / this.ky;
const h = buffer / this.kx;
return [
bbox[0] - h,
bbox[1] - v,
bbox[2] + h,
bbox[3] + v
];
}
/**
* Returns true if the given point is inside in the given bounding box, otherwise false.
*
* @param {[number, number]} p point [longitude, latitude]
* @param {[number, number, number, number]} bbox ([w, s, e, n])
* @returns {boolean}
* @example
* const inside = ruler.insideBBox([30.5, 50.5], [30, 50, 31, 51]);
* //=inside
*/
insideBBox(p, bbox) { // eslint-disable-line
return wrap(p[0] - bbox[0]) >= 0 &&
wrap(p[0] - bbox[2]) <= 0 &&
p[1] >= bbox[1] &&
p[1] <= bbox[3];
}
}
/**
* @param {[number, number]} a
* @param {[number, number]} b
*/
function equals$1(a, b) {
return a[0] === b[0] && a[1] === b[1];
}
/**
* @param {[number, number]} a
* @param {[number, number]} b
* @param {number} t
* @returns {[number, number]}
*/
function interpolate(a, b, t) {
const dx = wrap(b[0] - a[0]);
const dy = b[1] - a[1];
return [
a[0] + dx * t,
a[1] + dy * t
];
}
/**
* normalize a degree value into [-180..180] range
* @param {number} deg
*/
function wrap(deg) {
while (deg < -180) deg += 360;
while (deg > 180) deg -= 360;
return deg;
}
class TinyQueue {
constructor(data = [], compare = (a, b) => (a < b ? -1 : a > b ? 1 : 0)) {
this.data = data;
this.length = this.data.length;
this.compare = compare;
if (this.length > 0) {
for (let i = (this.length >> 1) - 1; i >= 0; i--) this._down(i);
}
}
push(item) {
this.data.push(item);
this._up(this.length++);
}
pop() {
if (this.length === 0) return undefined;
const top = this.data[0];
const bottom = this.data.pop();
if (--this.length > 0) {
this.data[0] = bottom;
this._down(0);
}
return top;
}
peek() {
return this.data[0];
}
_up(pos) {
const {data, compare} = this;
const item = data[pos];
while (pos > 0) {
const parent = (pos - 1) >> 1;
const current = data[parent];
if (compare(item, current) >= 0) break;
data[pos] = current;
pos = parent;
}
data[pos] = item;
}
_down(pos) {
const {data, compare} = this;
const halfLength = this.length >> 1;
const item = data[pos];
while (pos < halfLength) {
let bestChild = (pos << 1) + 1; // initially it is the left child
const right = bestChild + 1;
if (right < this.length && compare(data[right], data[bestChild]) < 0) {
bestChild = right;
}
if (compare(data[bestChild], item) >= 0) break;
data[pos] = data[bestChild];
pos = bestChild;
}
data[pos] = item;
}
}
var EXTENT = 8192;
function compareMax$1(a, b) {
return b.dist - a.dist;
}
const MIN_POINT_SIZE = 100;
const MIN_LINE_POINT_SIZE = 50;
function isDefaultBBOX(bbox) {
const defualtBBox = [Infinity, Infinity, -Infinity, -Infinity];
if (defualtBBox.length !== bbox.length) {
return false;
}
for (let i = 0; i < defualtBBox.length; i++) {
if (defualtBBox[i] !== bbox[i]) {
return false;
}
}
return true;
}
function getRangeSize(range) {
return range[1] - range[0] + 1;
}
function isRangeSafe(range, threshold) {
const ret = range[1] >= range[0] && range[1] < threshold;
if (!ret) {
console.warn("Distance Expression: Index is out of range");
}
return ret;
}
function splitRange(range, isLine) {
if (range[0] > range[1]) return [null, null];
const size = getRangeSize(range);
if (isLine) {
if (size === 2) {
return [range, null];
}
const size1 = Math.floor(size / 2);
const range1 = [range[0], range[0] + size1];
const range2 = [range[0] + size1, range[1]];
return [range1, range2];
} else {
if (size === 1) {
return [range, null];
}
const size1 = Math.floor(size / 2) - 1;
const range1 = [range[0], range[0] + size1];
const range2 = [range[0] + size1 + 1, range[1]];
return [range1, range2];
}
}
function getBBox(pointSets, range) {
const bbox = [Infinity, Infinity, -Infinity, -Infinity];
if (!isRangeSafe(range, pointSets.length)) return bbox;
for (let i = range[0]; i <= range[1]; ++i) {
updateBBox(bbox, pointSets[i]);
}
return bbox;
}
function getPolygonBBox(polygon) {
const bbox = [Infinity, Infinity, -Infinity, -Infinity];
for (let i = 0; i < polygon.length; ++i) {
for (let j = 0; j < polygon[i].length; ++j) {
updateBBox(bbox, polygon[i][j]);
}
}
return bbox;
}
function bboxToBBoxDistance(bbox1, bbox2, ruler) {
if (isDefaultBBOX(bbox1) || isDefaultBBOX(bbox2)) {
return NaN;
}
let dx = 0;
let dy = 0;
if (bbox1[2] < bbox2[0]) {
dx = bbox2[0] - bbox1[2];
}
if (bbox1[0] > bbox2[2]) {
dx = bbox1[0] - bbox2[2];
}
if (bbox1[1] > bbox2[3]) {
dy = bbox1[1] - bbox2[3];
}
if (bbox1[3] < bbox2[1]) {
dy = bbox2[1] - bbox1[3];
}
return ruler.distance([0, 0], [dx, dy]);
}
function lngFromMercatorX$1(x) {
return x * 360 - 180;
}
function latFromMercatorY$1(y) {
const y2 = 180 - y * 360;
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}
function getLngLatPoint(coord, canonical) {
const tilesAtZoom = Math.pow(2, canonical.z);
const x = (coord.x / EXTENT + canonical.x) / tilesAtZoom;
const y = (coord.y / EXTENT + canonical.y) / tilesAtZoom;
return [lngFromMercatorX$1(x), latFromMercatorY$1(y)];
}
function getLngLatPoints(coordinates, canonical) {
const coords = [];
for (let i = 0; i < coordinates.length; ++i) {
coords.push(getLngLatPoint(coordinates[i], canonical));
}
return coords;
}
function pointToLineDistance(point, line, ruler) {
const nearestPoint = ruler.pointOnLine(line, point).point;
return ruler.distance(point, nearestPoint);
}
function pointsToLineDistance(points, rangeA, line, rangeB, ruler) {
const subLine = line.slice(rangeB[0], rangeB[1] + 1);
let dist = Infinity;
for (let i = rangeA[0]; i <= rangeA[1]; ++i) {
if ((dist = Math.min(dist, pointToLineDistance(points[i], subLine, ruler))) === 0) return 0;
}
return dist;
}
function segmentToSegmentDistance(p1, p2, q1, q2, ruler) {
const dist1 = Math.min(
ruler.pointToSegmentDistance(p1, q1, q2),
ruler.pointToSegmentDistance(p2, q1, q2)
);
const dist2 = Math.min(
ruler.pointToSegmentDistance(q1, p1, p2),
ruler.pointToSegmentDistance(q2, p1, p2)
);
return Math.min(dist1, dist2);
}
function lineToLineDistance(line1, range1, line2, range2, ruler) {
if (!isRangeSafe(range1, line1.length) || !isRangeSafe(range2, line2.length)) {
return NaN;
}
let dist = Infinity;
for (let i = range1[0]; i < range1[1]; ++i) {
for (let j = range2[0]; j < range2[1]; ++j) {
if (segmentIntersectSegment(line1[i], line1[i + 1], line2[j], line2[j + 1])) return 0;
dist = Math.min(dist, segmentToSegmentDistance(line1[i], line1[i + 1], line2[j], line2[j + 1], ruler));
}
}
return dist;
}
function pointsToPointsDistance(pointSet1, range1, pointSet2, range2, ruler) {
if (!isRangeSafe(range1, pointSet1.length) || !isRangeSafe(range2, pointSet2.length)) {
return NaN;
}
let dist = Infinity;
for (let i = range1[0]; i <= range1[1]; ++i) {
for (let j = range2[0]; j <= range2[1]; ++j) {
if ((dist = Math.min(dist, ruler.distance(pointSet1[i], pointSet2[j]))) === 0) return dist;
}
}
return dist;
}
function pointToPolygonDistance(point, polygon, ruler) {
if (pointWithinPolygon(
point,
polygon,
true
/*trueOnBoundary*/
)) return 0;
let dist = Infinity;
for (const ring of polygon) {
const ringLen = ring.length;
if (ringLen < 2) {
console.warn("Distance Expression: Invalid polygon!");
return NaN;
}
if (ring[0] !== ring[ringLen - 1]) {
if ((dist = Math.min(dist, ruler.pointToSegmentDistance(point, ring[ringLen - 1], ring[0]))) === 0) return dist;
}
if ((dist = Math.min(dist, pointToLineDistance(point, ring, ruler))) === 0) return dist;
}
return dist;
}
function lineToPolygonDistance(line, range, polygon, ruler) {
if (!isRangeSafe(range, line.length)) {
return NaN;
}
for (let i = range[0]; i <= range[1]; ++i) {
if (pointWithinPolygon(
line[i],
polygon,
true
/*trueOnBoundary*/
)) return 0;
}
let dist = Infinity;
for (let i = range[0]; i < range[1]; ++i) {
for (const ring of polygon) {
for (let j = 0, len = ring.length, k = len - 1; j < len; k = j++) {
if (segmentIntersectSegment(line[i], line[i + 1], ring[k], ring[j])) return 0;
dist = Math.min(dist, segmentToSegmentDistance(line[i], line[i + 1], ring[k], ring[j], ruler));
}
}
}
return dist;
}
function polygonIntersect(polygon1, polygon2) {
for (const ring of polygon1) {
for (let i = 0; i <= ring.length - 1; ++i) {
if (pointWithinPolygon(
ring[i],
polygon2,
true
/*trueOnBoundary*/
)) return true;
}
}
return false;
}
function polygonToPolygonDistance(polygon1, polygon2, ruler, currentMiniDist = Infinity) {
const bbox1 = getPolygonBBox(polygon1);
const bbox2 = getPolygonBBox(polygon2);
if (currentMiniDist !== Infinity && bboxToBBoxDistance(bbox1, bbox2, ruler) >= currentMiniDist) {
return currentMiniDist;
}
if (boxWithinBox(bbox1, bbox2)) {
if (polygonIntersect(polygon1, polygon2)) return 0;
} else if (polygonIntersect(polygon2, polygon1)) {
return 0;
}
let dist = currentMiniDist;
for (const ring1 of polygon1) {
for (let i = 0, len1 = ring1.length, l = len1 - 1; i < len1; l = i++) {
for (const ring2 of polygon2) {
for (let j = 0, len2 = ring2.length, k = len2 - 1; j < len2; k = j++) {
if (segmentIntersectSegment(ring1[l], ring1[i], ring2[k], ring2[j])) return 0;
dist = Math.min(dist, segmentToSegmentDistance(ring1[l], ring1[i], ring2[k], ring2[j], ruler));
}
}
}
}
return dist;
}
function updateQueue(distQueue, miniDist, ruler, pointSet1, pointSet2, r1, r2) {
if (r1 === null || r2 === null) return;
const tempDist = bboxToBBoxDistance(getBBox(pointSet1, r1), getBBox(pointSet2, r2), ruler);
if (tempDist < miniDist) distQueue.push({ dist: tempDist, range1: r1, range2: r2 });
}
function pointSetToPolygonDistance(pointSets, isLine, polygon, ruler, currentMiniDist = Infinity) {
let miniDist = Math.min(ruler.distance(pointSets[0], polygon[0][0]), currentMiniDist);
if (miniDist === 0) return miniDist;
const initialDistPair = {
dist: 0,
range1: [0, pointSets.length - 1],
range2: [0, 0]
};
const distQueue = new TinyQueue([initialDistPair], compareMax$1);
const setThreshold = isLine ? MIN_LINE_POINT_SIZE : MIN_POINT_SIZE;
const polyBBox = getPolygonBBox(polygon);
while (distQueue.length) {
const distPair = distQueue.pop();
if (distPair.dist >= miniDist) continue;
const range = distPair.range1;
if (getRangeSize(range) <= setThreshold) {
if (!isRangeSafe(range, pointSets.length)) return NaN;
if (isLine) {
const tempDist = lineToPolygonDistance(pointSets, range, polygon, ruler);
if ((miniDist = Math.min(miniDist, tempDist)) === 0) return miniDist;
} else {
for (let i = range[0]; i <= range[1]; ++i) {
const tempDist = pointToPolygonDistance(pointSets[i], polygon, ruler);
if ((miniDist = Math.min(miniDist, tempDist)) === 0) return miniDist;
}
}
} else {
const newRanges = splitRange(range, isLine);
if (newRanges[0] !== null) {
const tempDist = bboxToBBoxDistance(getBBox(pointSets, newRanges[0]), polyBBox, ruler);
if (tempDist < miniDist) distQueue.push({ dist: tempDist, range1: newRanges[0], range2: [0, 0] });
}
if (newRanges[1] !== null) {
const tempDist = bboxToBBoxDistance(getBBox(pointSets, newRanges[1]), polyBBox, ruler);
if (tempDist < miniDist) distQueue.push({ dist: tempDist, range1: newRanges[1], range2: [0, 0] });
}
}
}
return miniDist;
}
function pointSetsDistance(pointSet1, isLine1, pointSet2, isLine2, ruler, currentMiniDist = Infinity) {
let miniDist = Math.min(currentMiniDist, ruler.distance(pointSet1[0], pointSet2[0]));
if (miniDist === 0) return miniDist;
const initialDistPair = {
dist: 0,
range1: [0, pointSet1.length - 1],
range2: [0, pointSet2.length - 1]
};
const distQueue = new TinyQueue([initialDistPair], compareMax$1);
const set1Threshold = isLine1 ? MIN_LINE_POINT_SIZE : MIN_POINT_SIZE;
const set2Threshold = isLine2 ? MIN_LINE_POINT_SIZE : MIN_POINT_SIZE;
while (distQueue.length) {
const distPair = distQueue.pop();
if (distPair.dist >= miniDist) continue;
const rangeA = distPair.range1;
const rangeB = distPair.range2;
if (getRangeSize(rangeA) <= set1Threshold && getRangeSize(rangeB) <= set2Threshold) {
if (!isRangeSafe(rangeA, pointSet1.length) || !isRangeSafe(rangeB, pointSet2.length)) {
return NaN;
}
if (isLine1 && isLine2) {
miniDist = Math.min(miniDist, lineToLineDistance(pointSet1, rangeA, pointSet2, rangeB, ruler));
} else if (!isLine1 && !isLine2) {
miniDist = Math.min(miniDist, pointsToPointsDistance(pointSet1, rangeA, pointSet2, rangeB, ruler));
} else if (isLine1 && !isLine2) {
miniDist = Math.min(miniDist, pointsToLineDistance(pointSet2, rangeB, pointSet1, rangeA, ruler));
} else if (!isLine1 && isLine2) {
miniDist = Math.min(miniDist, pointsToLineDistance(pointSet1, rangeA, pointSet2, rangeB, ruler));
}
if (miniDist === 0) return miniDist;
} else {
const newRangesA = splitRange(rangeA, isLine1);
const newRangesB = splitRange(rangeB, isLine2);
updateQueue(distQueue, miniDist, ruler, pointSet1, pointSet2, newRangesA[0], newRangesB[0]);
updateQueue(distQueue, miniDist, ruler, pointSet1, pointSet2, newRangesA[0], newRangesB[1]);
updateQueue(distQueue, miniDist, ruler, pointSet1, pointSet2, newRangesA[1], newRangesB[0]);
updateQueue(distQueue, miniDist, ruler, pointSet1, pointSet2, newRangesA[1], newRangesB[1]);
}
}
return miniDist;
}
function pointSetToLinesDistance(pointSet, isLine, lines, ruler, currentMiniDist = Infinity) {
let dist = currentMiniDist;
const bbox1 = getBBox(pointSet, [0, pointSet.length - 1]);
for (const line of lines) {
if (dist !== Infinity && bboxToBBoxDistance(bbox1, getBBox(line, [0, line.length - 1]), ruler) >= dist) continue;
dist = Math.min(dist, pointSetsDistance(pointSet, isLine, line, true, ruler, dist));
if (dist === 0) return dist;
}
return dist;
}
function pointSetToPolygonsDistance(points, isLine, polygons, ruler, currentMiniDist = Infinity) {
let dist = currentMiniDist;
const bbox1 = getBBox(points, [0, points.length - 1]);
for (const polygon of polygons) {
if (dist !== Infinity && bboxToBBoxDistance(bbox1, getPolygonBBox(polygon), ruler) >= dist) continue;
const tempDist = pointSetToPolygonDistance(points, isLine, polygon, ruler, dist);
if (isNaN(tempDist)) return tempDist;
if ((dist = Math.min(dist, tempDist)) === 0) return dist;
}
return dist;
}
function polygonsToPolygonsDistance(polygons1, polygons2, ruler) {
let dist = Infinity;
for (const polygon1 of polygons1) {
for (const polygon2 of polygons2) {
const tempDist = polygonToPolygonDistance(polygon1, polygon2, ruler, dist);
if (isNaN(tempDist)) return tempDist;
if ((dist = Math.min(dist, tempDist)) === 0) return dist;
}
}
return dist;
}
function pointsToGeometryDistance(originGeometry, canonical, geometry) {
const lngLatPoints = [];
for (const points of originGeometry) {
for (const point of points) {
lngLatPoints.push(getLngLatPoint(point, canonical));
}
}
const ruler = new CheapRuler(lngLatPoints[0][1], "meters");
if (geometry.type === "Point" || geometry.type === "MultiPoint" || geometry.type === "LineString") {
return pointSetsDistance(
lngLatPoints,
false,
geometry.type === "Point" ? [geometry.coordinates] : geometry.coordinates,
geometry.type === "LineString",
ruler
);
}
if (geometry.type === "MultiLineString") {
return pointSetToLinesDistance(lngLatPoints, false, geometry.coordinates, ruler);
}
if (geometry.type === "Polygon" || geometry.type === "MultiPolygon") {
return pointSetToPolygonsDistance(
lngLatPoints,
false,
geometry.type === "Polygon" ? [geometry.coordinates] : geometry.coordinates,
ruler
);
}
return null;
}
function linesToGeometryDistance(originGeometry, canonical, geometry) {
const lngLatLines = [];
for (const line of originGeometry) {
const lngLatLine = [];
for (const point of line) {
lngLatLine.push(getLngLatPoint(point, canonical));
}
lngLatLines.push(lngLatLine);
}
const ruler = new CheapRuler(lngLatLines[0][0][1], "meters");
if (geometry.type === "Point" || geometry.type === "MultiPoint" || geometry.type === "LineString") {
return pointSetToLinesDistance(
geometry.type === "Point" ? [geometry.coordinates] : geometry.coordinates,
geometry.type === "LineString",
lngLatLines,
ruler
);
}
if (geometry.type === "MultiLineString") {
let dist = Infinity;
for (let i = 0; i < geometry.coordinates.length; i++) {
const tempDist = pointSetToLinesDistance(geometry.coordinates[i], true, lngLatLines, ruler, dist);
if (isNaN(tempDist)) return tempDist;
if ((dist = Math.min(dist, tempDist)) === 0) return dist;
}
return dist;
}
if (geometry.type === "Polygon" || geometry.type === "MultiPolygon") {
let dist = Infinity;
for (let i = 0; i < lngLatLines.length; i++) {
const tempDist = pointSetToPolygonsDistance(
lngLatLines[i],
true,
geometry.type === "Polygon" ? [geometry.coordinates] : geometry.coordinates,
ruler,
dist
);
if (isNaN(tempDist)) return tempDist;
if ((dist = Math.min(dist, tempDist)) === 0) return dist;
}
return dist;
}
return null;
}
function polygonsToGeometryDistance(originGeometry, canonical, geometry) {
const lngLatPolygons = [];
for (const polygon of classifyRings$2(originGeometry, 0)) {
const lngLatPolygon = [];
for (let i = 0; i < polygon.length; ++i) {
lngLatPolygon.push(getLngLatPoints(polygon[i], canonical));
}
lngLatPolygons.push(lngLatPolygon);
}
const ruler = new CheapRuler(lngLatPolygons[0][0][0][1], "meters");
if (geometry.type === "Point" || geometry.type === "MultiPoint" || geometry.type === "LineString") {
return pointSetToPolygonsDistance(
geometry.type === "Point" ? [geometry.coordinates] : geometry.coordinates,
geometry.type === "LineString",
lngLatPolygons,
ruler
);
}
if (geometry.type === "MultiLineString") {
let dist = Infinity;
for (let i = 0; i < geometry.coordinates.length; i++) {
const tempDist = pointSetToPolygonsDistance(geometry.coordinates[i], true, lngLatPolygons, ruler, dist);
if (isNaN(tempDist)) return tempDist;
if ((dist = Math.min(dist, tempDist)) === 0) return dist;
}
return dist;
}
if (geometry.type === "Polygon" || geometry.type === "MultiPolygon") {
return polygonsToPolygonsDistance(
geometry.type === "Polygon" ? [geometry.coordinates] : geometry.coordinates,
lngLatPolygons,
ruler
);
}
return null;
}
function isTypeValid(type) {
return type === "Point" || type === "MultiPoint" || type === "LineString" || type === "MultiLineString" || type === "Polygon" || type === "MultiPolygon";
}
class Distance {
constructor(geojson, geometries) {
this.type = NumberType;
this.geojson = geojson;
this.geometries = geometries;
}
static parse(args, context) {
if (args.length !== 2) {
return context.error(`'distance' expression requires either one argument, but found ' ${args.length - 1} instead.`);
}
if (isValue(args[1])) {
const geojson = args[1];
if (geojson.type === "FeatureCollection") {
for (let i = 0; i < geojson.features.length; ++i) {
if (isTypeValid(geojson.features[i].geometry.type)) {
return new Distance(geojson, geojson.features[i].geometry);
}
}
} else if (geojson.type === "Feature") {
if (isTypeValid(geojson.geometry.type)) {
return new Distance(geojson, geojson.geometry);
}
} else if (isTypeValid(geojson.type)) {
return new Distance(geojson, geojson);
}
}
return context.error(
"'distance' expression needs to be an array with format ['Distance', GeoJSONObj]."
);
}
evaluate(ctx) {
const geometry = ctx.geometry();
const canonical = ctx.canonicalID();
if (geometry != null && canonical != null) {
if (ctx.geometryType() === "Point") {
return pointsToGeometryDistance(geometry, canonical, this.geometries);
}
if (ctx.geometryType() === "LineString") {
return linesToGeometryDistance(geometry, canonical, this.geometries);
}
if (ctx.geometryType() === "Polygon") {
return polygonsToGeometryDistance(geometry, canonical, this.geometries);
}
console.warn("Distance Expression: currently only evaluates valid Point/LineString/Polygon geometries.");
} else {
console.warn("Distance Expression: requirs valid feature and canonical information.");
}
return null;
}
eachChild() {
}
outputDefined() {
return true;
}
serialize() {
return ["distance", this.geojson];
}
}
function isFeatureConstant(e) {
if (e instanceof CompoundExpression) {
if (e.name === "get" && e.args.length === 1) {
return false;
} else if (e.name === "feature-state") {
return false;
} else if (e.name === "has" && e.args.length === 1) {
return false;
} else if (e.name === "properties" || e.name === "geometry-type" || e.name === "id") {
return false;
} else if (/^filter-/.test(e.name)) {
return false;
}
}
if (e instanceof Within) {
return false;
}
if (e instanceof Distance) {
return false;
}
if (e instanceof Config) {
return e.featureConstant;
}
let result = true;
e.eachChild((arg) => {
if (result && !isFeatureConstant(arg)) {
result = false;
}
});
return result;
}
function isStateConstant(e) {
if (e instanceof CompoundExpression) {
if (e.name === "feature-state") {
return false;
}
}
let result = true;
e.eachChild((arg) => {
if (result && !isStateConstant(arg)) {
result = false;
}
});
return result;
}
function isGlobalPropertyConstant(e, properties) {
if (e instanceof CompoundExpression && properties.indexOf(e.name) >= 0) {
return false;
}
let result = true;
e.eachChild((arg) => {
if (result && !isGlobalPropertyConstant(arg, properties)) {
result = false;
}
});
return result;
}
const FQIDSeparator$1 = "";
function makeConfigFQID(id, ownScope, contextScope) {
return [id, ownScope, contextScope].filter(Boolean).join(FQIDSeparator$1);
}
function coerceValue(type, value) {
switch (type) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
case "string":
return toString(value);
case "number":
return +value;
case "boolean":
return !!value;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
case "color":
return Color.parse(value);
case "formatted": {
return Formatted.fromString(toString(value));
}
case "resolvedImage": {
return ResolvedImage.build(toString(value));
}
}
return value;
}
function clampToAllowedNumber(value, min, max, step) {
if (step !== void 0) {
value = step * Math.round(value / step);
}
if (min !== void 0 && value < min) {
value = min;
}
if (max !== void 0 && value > max) {
value = max;
}
return value;
}
class Config {
constructor(type, key, scope, featureConstant = false) {
this.type = type;
this.key = key;
this.scope = scope;
this.featureConstant = featureConstant;
}
static parse(args, context) {
let type = context.expectedType;
if (type === null || type === void 0) {
type = ValueType;
}
if (args.length < 2 || args.length > 3) {
return context.error(`Invalid number of arguments for 'config' expression.`);
}
const configKey = context.parse(args[1], 1);
if (!(configKey instanceof Literal)) {
return context.error(`Key name of 'config' expression must be a string literal.`);
}
let featureConstant = true;
let configScopeValue;
const configKeyValue = toString(configKey.value);
if (args.length >= 3) {
const configScope = context.parse(args[2], 2);
if (!(configScope instanceof Literal)) {
return context.error(`Scope of 'config' expression must be a string literal.`);
}
configScopeValue = toString(configScope.value);
}
if (context.options) {
const fqid = makeConfigFQID(configKeyValue, configScopeValue, context._scope);
const config = context.options.get(fqid);
if (config) {
featureConstant = isFeatureConstant(config.value || config.default);
}
}
return new Config(type, configKeyValue, configScopeValue, featureConstant);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
const fqid = makeConfigFQID(this.key, this.scope, ctx.scope);
const config = ctx.getConfig(fqid);
if (!config) return null;
const { type, value, values, minValue, maxValue, stepValue } = config;
const defaultValue = config.default.evaluate(ctx);
let result = defaultValue;
if (value) {
const originalScope = ctx.scope;
ctx.scope = (originalScope || "").split(FQIDSeparator$1).slice(1).join(FQIDSeparator$1);
result = value.evaluate(ctx);
ctx.scope = originalScope;
}
if (type) {
result = coerceValue(type, result);
}
if (result !== void 0 && (minValue !== void 0 || maxValue !== void 0 || stepValue !== void 0)) {
if (typeof result === "number") {
result = clampToAllowedNumber(result, minValue, maxValue, stepValue);
} else if (Array.isArray(result)) {
result = result.map((item) => typeof item === "number" ? clampToAllowedNumber(item, minValue, maxValue, stepValue) : item);
}
}
if (value !== void 0 && result !== void 0 && values && !values.includes(result)) {
result = defaultValue;
if (type) {
result = coerceValue(type, result);
}
}
if (type && type !== this.type || result !== void 0 && !typeEquals(typeOf(result), this.type)) {
result = coerceValue(this.type.kind, result);
}
return result;
}
eachChild() {
}
outputDefined() {
return false;
}
serialize() {
const res = ["config", this.key];
if (this.scope) {
res.concat(this.scope);
}
return res;
}
}
class Var {
constructor(name, boundExpression) {
this.type = boundExpression.type;
this.name = name;
this.boundExpression = boundExpression;
}
static parse(args, context) {
if (args.length !== 2 || typeof args[1] !== "string")
return context.error(`'var' expression requires exactly one string literal argument.`);
const name = args[1];
if (!context.scope.has(name)) {
return context.error(`Unknown variable "${name}". Make sure "${name}" has been bound in an enclosing "let" expression before using it.`, 1);
}
return new Var(name, context.scope.get(name));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
return this.boundExpression.evaluate(ctx);
}
eachChild() {
}
outputDefined() {
return false;
}
serialize() {
return ["var", this.name];
}
}
class ParsingContext {
constructor(registry, path = [], expectedType, scope = new Scope(), errors = [], _scope, options, iconImageUseTheme) {
this.registry = registry;
this.path = path;
this.key = path.map((part) => {
if (typeof part === "string") {
return `['${part}']`;
}
return `[${part}]`;
}).join("");
this.scope = scope;
this.errors = errors;
this.expectedType = expectedType;
this._scope = _scope;
this.options = options;
this.iconImageUseTheme = iconImageUseTheme;
}
/**
* @param expr the JSON expression to parse
* @param index the optional argument index if this expression is an argument of a parent expression that's being parsed
* @param options
* @param options.omitTypeAnnotations set true to omit inferred type annotations. Caller beware: with this option set, the parsed expression's type will NOT satisfy `expectedType` if it would normally be wrapped in an inferred annotation.
* @private
*/
parse(expr, index, expectedType, bindings, options = {}) {
if (index || expectedType) {
return this.concat(index, null, expectedType, bindings)._parse(expr, options);
}
return this._parse(expr, options);
}
/**
* @param expr the JSON expression to parse
* @param index the optional argument index if parent object being is an argument of another expression
* @param key key of parent object being parsed
* @param options
* @param options.omitTypeAnnotations set true to omit inferred type annotations. Caller beware: with this option set, the parsed expression's type will NOT satisfy `expectedType` if it would normally be wrapped in an inferred annotation.
* @private
*/
parseObjectValue(expr, index, key, expectedType, bindings, options = {}) {
return this.concat(index, key, expectedType, bindings)._parse(expr, options);
}
_parse(expr, options) {
if (expr === null || typeof expr === "string" || typeof expr === "boolean" || typeof expr === "number") {
expr = ["literal", expr];
}
function annotate(parsed, type, typeAnnotation) {
if (typeAnnotation === "assert") {
return new Assertion(type, [parsed]);
} else if (typeAnnotation === "coerce") {
return new Coercion(type, [parsed]);
} else {
return parsed;
}
}
if (Array.isArray(expr)) {
if (expr.length === 0) {
return this.error(`Expected an array with at least one element. If you wanted a literal array, use ["literal", []].`);
}
const Expr = typeof expr[0] === "string" ? this.registry[expr[0]] : void 0;
if (Expr) {
let parsed = Expr.parse(expr, this);
if (!parsed) return null;
if (this.expectedType) {
const expected = this.expectedType;
const actual = parsed.type;
if ((expected.kind === "string" || expected.kind === "number" || expected.kind === "boolean" || expected.kind === "object" || expected.kind === "array") && actual.kind === "value") {
parsed = annotate(parsed, expected, options.typeAnnotation || "assert");
} else if ((expected.kind === "color" || expected.kind === "formatted" || expected.kind === "resolvedImage") && (actual.kind === "value" || actual.kind === "string")) {
parsed = annotate(parsed, expected, options.typeAnnotation || "coerce");
} else if (this.checkSubtype(expected, actual)) {
return null;
}
}
if (!(parsed instanceof Literal) && parsed.type.kind !== "resolvedImage" && isConstant(parsed)) {
const ec = new EvaluationContext(this._scope, this.options, this.iconImageUseTheme);
try {
parsed = new Literal(parsed.type, parsed.evaluate(ec));
} catch (e) {
this.error(e.message);
return null;
}
}
return parsed;
}
return Coercion.parse(["to-array", expr], this);
} else if (typeof expr === "undefined") {
return this.error(`'undefined' value invalid. Use null instead.`);
} else if (typeof expr === "object") {
return this.error(`Bare objects invalid. Use ["literal", {...}] instead.`);
} else {
return this.error(`Expected an array, but found ${typeof expr} instead.`);
}
}
/**
* Returns a copy of this context suitable for parsing the subexpression at
* index `index`, optionally appending to 'let' binding map.
*
* Note that `errors` property, intended for collecting errors while
* parsing, is copied by reference rather than cloned.
* @private
*/
concat(index, key, expectedType, bindings) {
let path = typeof index === "number" ? this.path.concat(index) : this.path;
path = typeof key === "string" ? path.concat(key) : path;
const scope = bindings ? this.scope.concat(bindings) : this.scope;
return new ParsingContext(
this.registry,
path,
expectedType || null,
scope,
this.errors,
this._scope,
this.options,
this.iconImageUseTheme
);
}
/**
* Push a parsing (or type checking) error into the `this.errors`
* @param error The message
* @param keys Optionally specify the source of the error at a child
* of the current expression at `this.key`.
* @private
*/
error(error, ...keys) {
const key = `${this.key}${keys.map((k) => `[${k}]`).join("")}`;
this.errors.push(new ParsingError(key, error));
}
/**
* Returns null if `t` is a subtype of `expected`; otherwise returns an
* error message and also pushes it to `this.errors`.
*/
checkSubtype(expected, t) {
const error = checkSubtype(expected, t);
if (error) this.error(error);
return error;
}
}
function isConstant(expression) {
if (expression instanceof Var) {
return isConstant(expression.boundExpression);
} else if (expression instanceof CompoundExpression && expression.name === "error") {
return false;
} else if (expression instanceof CollatorExpression) {
return false;
} else if (expression instanceof Within) {
return false;
} else if (expression instanceof Distance) {
return false;
} else if (expression instanceof Config) {
return false;
}
const isTypeAnnotation = expression instanceof Coercion || expression instanceof Assertion;
let childrenConstant = true;
expression.eachChild((child) => {
if (isTypeAnnotation) {
childrenConstant = childrenConstant && isConstant(child);
} else {
childrenConstant = childrenConstant && child instanceof Literal;
}
});
if (!childrenConstant) {
return false;
}
return isFeatureConstant(expression) && isGlobalPropertyConstant(expression, ["zoom", "heatmap-density", "worldview", "line-progress", "raster-value", "sky-radial-progress", "accumulated", "is-supported-script", "pitch", "distance-from-center", "measure-light", "raster-particle-speed"]);
}
function findStopLessThanOrEqualTo(stops, input) {
const lastIndex = stops.length - 1;
let lowerIndex = 0;
let upperIndex = lastIndex;
let currentIndex = 0;
let currentValue, nextValue;
while (lowerIndex <= upperIndex) {
currentIndex = Math.floor((lowerIndex + upperIndex) / 2);
currentValue = stops[currentIndex];
nextValue = stops[currentIndex + 1];
if (currentValue <= input) {
if (currentIndex === lastIndex || input < nextValue) {
return currentIndex;
}
lowerIndex = currentIndex + 1;
} else if (currentValue > input) {
upperIndex = currentIndex - 1;
} else {
throw new RuntimeError("Input is not a number.");
}
}
return 0;
}
class Step {
constructor(type, input, stops) {
this.type = type;
this.input = input;
this.labels = [];
this.outputs = [];
for (const [label, expression] of stops) {
this.labels.push(label);
this.outputs.push(expression);
}
}
static parse(args, context) {
if (args.length - 1 < 4) {
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
}
if ((args.length - 1) % 2 !== 0) {
return context.error(`Expected an even number of arguments.`);
}
const input = context.parse(args[1], 1, NumberType);
if (!input) return null;
const stops = [];
let outputType = null;
if (context.expectedType && context.expectedType.kind !== "value") {
outputType = context.expectedType;
}
for (let i = 1; i < args.length; i += 2) {
const label = i === 1 ? -Infinity : args[i];
const value = args[i + 1];
const labelKey = i;
const valueKey = i + 1;
if (typeof label !== "number") {
return context.error('Input/output pairs for "step" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey);
}
if (stops.length && stops[stops.length - 1][0] >= label) {
return context.error('Input/output pairs for "step" expressions must be arranged with input values in strictly ascending order.', labelKey);
}
const parsed = context.parse(value, valueKey, outputType);
if (!parsed) return null;
outputType = outputType || parsed.type;
stops.push([label, parsed]);
}
return new Step(outputType, input, stops);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
const labels = this.labels;
const outputs = this.outputs;
if (labels.length === 1) {
return outputs[0].evaluate(ctx);
}
const value = this.input.evaluate(ctx);
if (value <= labels[0]) {
return outputs[0].evaluate(ctx);
}
const stopCount = labels.length;
if (value >= labels[stopCount - 1]) {
return outputs[stopCount - 1].evaluate(ctx);
}
const index = findStopLessThanOrEqualTo(labels, value);
return outputs[index].evaluate(ctx);
}
eachChild(fn) {
fn(this.input);
for (const expression of this.outputs) {
fn(expression);
}
}
outputDefined() {
return this.outputs.every((out) => out.outputDefined());
}
serialize() {
const serialized = ["step", this.input.serialize()];
for (let i = 0; i < this.labels.length; i++) {
if (i > 0) {
serialized.push(this.labels[i]);
}
serialized.push(this.outputs[i].serialize());
}
return serialized;
}
}
const Xn = 0.95047, Yn = 1, Zn = 1.08883, t0 = 4 / 29, t1 = 6 / 29, t2 = 3 * t1 * t1, t3 = t1 * t1 * t1, deg2rad = Math.PI / 180, rad2deg = 180 / Math.PI;
function xyz2lab(t) {
return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
}
function lab2xyz(t) {
return t > t1 ? t * t * t : t2 * (t - t0);
}
function xyz2rgb(x) {
return 255 * (x <= 31308e-7 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055);
}
function rgb2xyz(x) {
x /= 255;
return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
function rgbToLab(rgbColor) {
const b = rgb2xyz(rgbColor.r), a = rgb2xyz(rgbColor.g), l = rgb2xyz(rgbColor.b), x = xyz2lab((0.4124564 * b + 0.3575761 * a + 0.1804375 * l) / Xn), y = xyz2lab((0.2126729 * b + 0.7151522 * a + 0.072175 * l) / Yn), z = xyz2lab((0.0193339 * b + 0.119192 * a + 0.9503041 * l) / Zn);
return {
l: 116 * y - 16,
a: 500 * (x - y),
b: 200 * (y - z),
alpha: rgbColor.a
};
}
function labToRgb(labColor) {
let y = (labColor.l + 16) / 116, x = isNaN(labColor.a) ? y : y + labColor.a / 500, z = isNaN(labColor.b) ? y : y - labColor.b / 200;
y = Yn * lab2xyz(y);
x = Xn * lab2xyz(x);
z = Zn * lab2xyz(z);
return new Color(
xyz2rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z),
// D65 -> sRGB
xyz2rgb(-0.969266 * x + 1.8760108 * y + 0.041556 * z),
xyz2rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z),
labColor.alpha
);
}
function interpolateLab(from, to, t) {
return {
l: number(from.l, to.l, t),
a: number(from.a, to.a, t),
b: number(from.b, to.b, t),
alpha: number(from.alpha, to.alpha, t)
};
}
function rgbToHcl(rgbColor) {
const { l, a, b } = rgbToLab(rgbColor);
const h = Math.atan2(b, a) * rad2deg;
return {
h: h < 0 ? h + 360 : h,
c: Math.sqrt(a * a + b * b),
l,
alpha: rgbColor.a
};
}
function hclToRgb(hclColor) {
const h = hclColor.h * deg2rad, c = hclColor.c, l = hclColor.l;
return labToRgb({
l,
a: Math.cos(h) * c,
b: Math.sin(h) * c,
alpha: hclColor.alpha
});
}
function interpolateHue(a, b, t) {
const d = b - a;
return a + t * (d > 180 || d < -180 ? d - 360 * Math.round(d / 360) : d);
}
function interpolateHcl(from, to, t) {
return {
h: interpolateHue(from.h, to.h, t),
c: number(from.c, to.c, t),
l: number(from.l, to.l, t),
alpha: number(from.alpha, to.alpha, t)
};
}
const lab = {
forward: rgbToLab,
reverse: labToRgb,
interpolate: interpolateLab
};
const hcl = {
forward: rgbToHcl,
reverse: hclToRgb,
interpolate: interpolateHcl
};
var colorSpaces = /*#__PURE__*/Object.freeze({
__proto__: null,
hcl: hcl,
lab: lab
});
class Interpolate {
constructor(type, operator, interpolation, input, stops) {
this.type = type;
this.operator = operator;
this.interpolation = interpolation;
this.input = input;
this.labels = [];
this.outputs = [];
for (const [label, expression] of stops) {
this.labels.push(label);
this.outputs.push(expression);
}
}
static interpolationFactor(interpolation, input, lower, upper) {
let t = 0;
if (interpolation.name === "exponential") {
t = exponentialInterpolation(input, interpolation.base, lower, upper);
} else if (interpolation.name === "linear") {
t = exponentialInterpolation(input, 1, lower, upper);
} else if (interpolation.name === "cubic-bezier") {
const c = interpolation.controlPoints;
const ub = new UnitBezier(c[0], c[1], c[2], c[3]);
t = ub.solve(exponentialInterpolation(input, 1, lower, upper));
}
return t;
}
static parse(args, context) {
let [operator, interpolation, input, ...rest] = args;
if (!Array.isArray(interpolation) || interpolation.length === 0) {
return context.error(`Expected an interpolation type expression.`, 1);
}
if (interpolation[0] === "linear") {
interpolation = { name: "linear" };
} else if (interpolation[0] === "exponential") {
const base = interpolation[1];
if (typeof base !== "number")
return context.error(`Exponential interpolation requires a numeric base.`, 1, 1);
interpolation = {
name: "exponential",
base
};
} else if (interpolation[0] === "cubic-bezier") {
const controlPoints = interpolation.slice(1);
if (controlPoints.length !== 4 || controlPoints.some((t) => typeof t !== "number" || t < 0 || t > 1)) {
return context.error("Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.", 1);
}
interpolation = {
name: "cubic-bezier",
controlPoints
};
} else {
return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0);
}
if (args.length - 1 < 4) {
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
}
if (args.length - 1 > 3 && (args.length - 1) % 2 !== 0) {
return context.error(`Expected an even number of arguments.`);
}
input = context.parse(input, 2, NumberType);
if (!input) return null;
const stops = [];
let outputType = null;
if (operator === "interpolate-hcl" || operator === "interpolate-lab") {
outputType = ColorType;
} else if (context.expectedType && context.expectedType.kind !== "value") {
outputType = context.expectedType;
}
for (let i = 0; i < rest.length; i += 2) {
const label = rest[i];
const value = rest[i + 1];
const labelKey = i + 3;
const valueKey = i + 4;
if (typeof label !== "number") {
return context.error('Input/output pairs for "interpolate" expressions must be defined using literal numeric values (not computed expressions) for the input values.', labelKey);
}
if (stops.length && stops[stops.length - 1][0] >= label) {
return context.error('Input/output pairs for "interpolate" expressions must be arranged with input values in strictly ascending order.', labelKey);
}
const parsed = context.parse(value, valueKey, outputType);
if (!parsed) return null;
outputType = outputType || parsed.type;
stops.push([label, parsed]);
}
if (outputType.kind !== "number" && outputType.kind !== "color" && !(outputType.kind === "array" && outputType.itemType.kind === "number" && typeof outputType.N === "number")) {
return context.error(`Type ${toString$1(outputType)} is not interpolatable.`);
}
return new Interpolate(outputType, operator, interpolation, input, stops);
}
evaluate(ctx) {
const labels = this.labels;
const outputs = this.outputs;
if (labels.length === 1) {
return outputs[0].evaluate(ctx);
}
const value = this.input.evaluate(ctx);
if (value <= labels[0]) {
return outputs[0].evaluate(ctx);
}
const stopCount = labels.length;
if (value >= labels[stopCount - 1]) {
return outputs[stopCount - 1].evaluate(ctx);
}
const index = findStopLessThanOrEqualTo(labels, value);
const lower = labels[index];
const upper = labels[index + 1];
const t = Interpolate.interpolationFactor(this.interpolation, value, lower, upper);
const outputLower = outputs[index].evaluate(ctx);
const outputUpper = outputs[index + 1].evaluate(ctx);
if (this.operator === "interpolate") {
return interpolate$1[this.type.kind.toLowerCase()](outputLower, outputUpper, t);
} else if (this.operator === "interpolate-hcl") {
return hcl.reverse(hcl.interpolate(hcl.forward(outputLower), hcl.forward(outputUpper), t));
} else {
return lab.reverse(lab.interpolate(lab.forward(outputLower), lab.forward(outputUpper), t));
}
}
eachChild(fn) {
fn(this.input);
for (const expression of this.outputs) {
fn(expression);
}
}
outputDefined() {
return this.outputs.every((out) => out.outputDefined());
}
serialize() {
let interpolation;
if (this.interpolation.name === "linear") {
interpolation = ["linear"];
} else if (this.interpolation.name === "exponential") {
if (this.interpolation.base === 1) {
interpolation = ["linear"];
} else {
interpolation = ["exponential", this.interpolation.base];
}
} else {
interpolation = ["cubic-bezier", ...this.interpolation.controlPoints];
}
const serialized = [this.operator, interpolation, this.input.serialize()];
for (let i = 0; i < this.labels.length; i++) {
serialized.push(
this.labels[i],
this.outputs[i].serialize()
);
}
return serialized;
}
}
function exponentialInterpolation(input, base, lowerValue, upperValue) {
const difference = upperValue - lowerValue;
const progress = input - lowerValue;
if (difference === 0) {
return 0;
} else if (base === 1) {
return progress / difference;
} else {
return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
}
}
class Coalesce {
constructor(type, args) {
this.type = type;
this.args = args;
}
static parse(args, context) {
if (args.length < 2) {
return context.error("Expectected at least one argument.");
}
let outputType = null;
const expectedType = context.expectedType;
if (expectedType && expectedType.kind !== "value") {
outputType = expectedType;
}
const parsedArgs = [];
for (const arg of args.slice(1)) {
const parsed = context.parse(arg, 1 + parsedArgs.length, outputType, void 0, { typeAnnotation: "omit" });
if (!parsed) return null;
outputType = outputType || parsed.type;
parsedArgs.push(parsed);
}
assert$1(outputType);
const needsAnnotation = expectedType && parsedArgs.some((arg) => checkSubtype(expectedType, arg.type));
return needsAnnotation ? new Coalesce(ValueType, parsedArgs) : new Coalesce(outputType, parsedArgs);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
let result = null;
let argCount = 0;
let firstImage;
for (const arg of this.args) {
argCount++;
result = arg.evaluate(ctx);
if (result && result instanceof ResolvedImage && !result.available) {
if (!firstImage) {
firstImage = result;
}
result = null;
if (argCount === this.args.length) {
return firstImage;
}
}
if (result !== null) break;
}
return result;
}
eachChild(fn) {
this.args.forEach(fn);
}
outputDefined() {
return this.args.every((arg) => arg.outputDefined());
}
serialize() {
const serialized = ["coalesce"];
this.eachChild((child) => {
serialized.push(child.serialize());
});
return serialized;
}
}
class Let {
constructor(bindings, result) {
this.type = result.type;
this.bindings = [].concat(bindings);
this.result = result;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
return this.result.evaluate(ctx);
}
eachChild(fn) {
for (const binding of this.bindings) {
fn(binding[1]);
}
fn(this.result);
}
static parse(args, context) {
if (args.length < 4)
return context.error(`Expected at least 3 arguments, but found ${args.length - 1} instead.`);
const bindings = [];
for (let i = 1; i < args.length - 1; i += 2) {
const name = args[i];
if (typeof name !== "string") {
return context.error(`Expected string, but found ${typeof name} instead.`, i);
}
if (/[^a-zA-Z0-9_]/.test(name)) {
return context.error(`Variable names must contain only alphanumeric characters or '_'.`, i);
}
const value = context.parse(args[i + 1], i + 1);
if (!value) return null;
bindings.push([name, value]);
}
const result = context.parse(args[args.length - 1], args.length - 1, context.expectedType, bindings);
if (!result) return null;
return new Let(bindings, result);
}
outputDefined() {
return this.result.outputDefined();
}
serialize() {
const serialized = ["let"];
for (const [name, expr] of this.bindings) {
serialized.push(name, expr.serialize());
}
serialized.push(this.result.serialize());
return serialized;
}
}
class At {
constructor(type, index, input) {
this.type = type;
this.index = index;
this.input = input;
}
static parse(args, context) {
if (args.length !== 3)
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
const index = context.parse(args[1], 1, NumberType);
const input = context.parse(args[2], 2, array(context.expectedType || ValueType));
if (!index || !input) return null;
const t = input.type;
return new At(t.itemType, index, input);
}
evaluate(ctx) {
const index = this.index.evaluate(ctx);
const array2 = this.input.evaluate(ctx);
if (index < 0) {
throw new RuntimeError(`Array index out of bounds: negative index`);
}
if (index >= array2.length) {
throw new RuntimeError(`Array index out of bounds: index exceeds array size`);
}
if (index !== Math.floor(index)) {
throw new RuntimeError(`Array index must be an integer. Use at-interpolated for fractional indices`);
}
return array2[index];
}
eachChild(fn) {
fn(this.index);
fn(this.input);
}
outputDefined() {
return false;
}
serialize() {
return ["at", this.index.serialize(), this.input.serialize()];
}
}
class AtInterpolated {
constructor(type, index, input) {
this.type = type;
this.index = index;
this.input = input;
}
static parse(args, context) {
if (args.length !== 3)
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
const index = context.parse(args[1], 1, NumberType);
const input = context.parse(args[2], 2, array(context.expectedType || ValueType));
if (!index || !input) return null;
const t = input.type;
return new AtInterpolated(t.itemType, index, input);
}
evaluate(ctx) {
const index = this.index.evaluate(ctx);
const array2 = this.input.evaluate(ctx);
if (index < 0) {
throw new RuntimeError(`Array index out of bounds: ${index} < 0.`);
}
if (index > array2.length - 1) {
throw new RuntimeError(`Array index out of bounds: ${index} > ${array2.length - 1}.`);
}
if (index === Math.floor(index)) {
return array2[index];
}
const lowerIndex = Math.floor(index);
const upperIndex = Math.ceil(index);
const lowerValue = array2[lowerIndex];
const upperValue = array2[upperIndex];
if (typeof lowerValue !== "number" || typeof upperValue !== "number") {
throw new RuntimeError(`Cannot interpolate between non-number values at index ${index}.`);
}
const fraction = index - lowerIndex;
return lowerValue * (1 - fraction) + upperValue * fraction;
}
eachChild(fn) {
fn(this.index);
fn(this.input);
}
outputDefined() {
return false;
}
serialize() {
return ["at-interpolated", this.index.serialize(), this.input.serialize()];
}
}
class In {
constructor(needle, haystack) {
this.type = BooleanType;
this.needle = needle;
this.haystack = haystack;
}
static parse(args, context) {
if (args.length !== 3) {
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
}
const needle = context.parse(args[1], 1, ValueType);
const haystack = context.parse(args[2], 2, ValueType);
if (!needle || !haystack) return null;
if (!isValidType(needle.type, [BooleanType, StringType, NumberType, NullType, ValueType])) {
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString$1(needle.type)} instead`);
}
return new In(needle, haystack);
}
evaluate(ctx) {
const needle = this.needle.evaluate(ctx);
const haystack = this.haystack.evaluate(ctx);
if (haystack == null) return false;
if (!isValidNativeType(needle, ["boolean", "string", "number", "null"])) {
throw new RuntimeError(`Expected first argument to be of type boolean, string, number or null, but found ${toString$1(typeOf(needle))} instead.`);
}
if (!isValidNativeType(haystack, ["string", "array"])) {
throw new RuntimeError(`Expected second argument to be of type array or string, but found ${toString$1(typeOf(haystack))} instead.`);
}
return haystack.indexOf(needle) >= 0;
}
eachChild(fn) {
fn(this.needle);
fn(this.haystack);
}
outputDefined() {
return true;
}
serialize() {
return ["in", this.needle.serialize(), this.haystack.serialize()];
}
}
class IndexOf {
constructor(needle, haystack, fromIndex) {
this.type = NumberType;
this.needle = needle;
this.haystack = haystack;
this.fromIndex = fromIndex;
}
static parse(args, context) {
if (args.length <= 2 || args.length >= 5) {
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`);
}
const needle = context.parse(args[1], 1, ValueType);
const haystack = context.parse(args[2], 2, ValueType);
if (!needle || !haystack) return null;
if (!isValidType(needle.type, [BooleanType, StringType, NumberType, NullType, ValueType])) {
return context.error(`Expected first argument to be of type boolean, string, number or null, but found ${toString$1(needle.type)} instead`);
}
if (args.length === 4) {
const fromIndex = context.parse(args[3], 3, NumberType);
if (!fromIndex) return null;
return new IndexOf(needle, haystack, fromIndex);
} else {
return new IndexOf(needle, haystack);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
const needle = this.needle.evaluate(ctx);
const haystack = this.haystack.evaluate(ctx);
if (!isValidNativeType(needle, ["boolean", "string", "number", "null"])) {
throw new RuntimeError(`Expected first argument to be of type boolean, string, number or null, but found ${toString$1(typeOf(needle))} instead.`);
}
if (!isValidNativeType(haystack, ["string", "array"])) {
throw new RuntimeError(`Expected second argument to be of type array or string, but found ${toString$1(typeOf(haystack))} instead.`);
}
if (this.fromIndex) {
const fromIndex = this.fromIndex.evaluate(ctx);
return haystack.indexOf(needle, fromIndex);
}
return haystack.indexOf(needle);
}
eachChild(fn) {
fn(this.needle);
fn(this.haystack);
if (this.fromIndex) {
fn(this.fromIndex);
}
}
outputDefined() {
return false;
}
serialize() {
if (this.fromIndex != null && this.fromIndex !== void 0) {
const fromIndex = this.fromIndex.serialize();
return ["index-of", this.needle.serialize(), this.haystack.serialize(), fromIndex];
}
return ["index-of", this.needle.serialize(), this.haystack.serialize()];
}
}
class Match {
constructor(inputType, outputType, input, cases, outputs, otherwise) {
this.inputType = inputType;
this.type = outputType;
this.input = input;
this.cases = cases;
this.outputs = outputs;
this.otherwise = otherwise;
}
static parse(args, context) {
if (args.length < 5)
return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
if (args.length % 2 !== 1)
return context.error(`Expected an even number of arguments.`);
let inputType;
let outputType;
if (context.expectedType && context.expectedType.kind !== "value") {
outputType = context.expectedType;
}
const cases = {};
const outputs = [];
for (let i = 2; i < args.length - 1; i += 2) {
let labels = args[i];
const value = args[i + 1];
if (!Array.isArray(labels)) {
labels = [labels];
}
const labelContext = context.concat(i);
if (labels.length === 0) {
return labelContext.error("Expected at least one branch label.");
}
for (const label of labels) {
if (typeof label !== "number" && typeof label !== "string") {
return labelContext.error(`Branch labels must be numbers or strings.`);
} else if (typeof label === "number" && Math.abs(label) > Number.MAX_SAFE_INTEGER) {
return labelContext.error(`Branch labels must be integers no larger than ${Number.MAX_SAFE_INTEGER}.`);
} else if (typeof label === "number" && Math.floor(label) !== label) {
return labelContext.error(`Numeric branch labels must be integer values.`);
} else if (!inputType) {
inputType = typeOf(label);
} else if (labelContext.checkSubtype(inputType, typeOf(label))) {
return null;
}
if (typeof cases[String(label)] !== "undefined") {
return labelContext.error("Branch labels must be unique.");
}
cases[String(label)] = outputs.length;
}
const result = context.parse(value, i, outputType);
if (!result) return null;
outputType = outputType || result.type;
outputs.push(result);
}
const input = context.parse(args[1], 1, ValueType);
if (!input) return null;
const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
if (!otherwise) return null;
assert$1(inputType && outputType);
if (input.type.kind !== "value" && context.concat(1).checkSubtype(inputType, input.type)) {
return null;
}
return new Match(inputType, outputType, input, cases, outputs, otherwise);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
const input = this.input.evaluate(ctx);
const output = typeEquals(typeOf(input), this.inputType) && this.outputs[this.cases[input]] || this.otherwise;
return output.evaluate(ctx);
}
eachChild(fn) {
fn(this.input);
this.outputs.forEach(fn);
fn(this.otherwise);
}
outputDefined() {
return this.outputs.every((out) => out.outputDefined()) && this.otherwise.outputDefined();
}
serialize() {
const serialized = ["match", this.input.serialize()];
const sortedLabels = Object.keys(this.cases).sort();
const groupedByOutput = [];
const outputLookup = {};
for (const label of sortedLabels) {
const outputIndex = outputLookup[this.cases[label]];
if (outputIndex === void 0) {
outputLookup[this.cases[label]] = groupedByOutput.length;
groupedByOutput.push([this.cases[label], [label]]);
} else {
groupedByOutput[outputIndex][1].push(label);
}
}
const coerceLabel = (label) => this.inputType.kind === "number" ? Number(label) : label;
for (const [outputIndex, labels] of groupedByOutput) {
if (labels.length === 1) {
serialized.push(coerceLabel(labels[0]));
} else {
serialized.push(labels.map(coerceLabel));
}
serialized.push(this.outputs[outputIndex].serialize());
}
serialized.push(this.otherwise.serialize());
return serialized;
}
}
class Case {
constructor(type, branches, otherwise) {
this.type = type;
this.branches = branches;
this.otherwise = otherwise;
}
static parse(args, context) {
if (args.length < 4)
return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
if (args.length % 2 !== 0)
return context.error(`Expected an odd number of arguments.`);
let outputType;
if (context.expectedType && context.expectedType.kind !== "value") {
outputType = context.expectedType;
}
const branches = [];
for (let i = 1; i < args.length - 1; i += 2) {
const test = context.parse(args[i], i, BooleanType);
if (!test) return null;
const result = context.parse(args[i + 1], i + 1, outputType);
if (!result) return null;
branches.push([test, result]);
outputType = outputType || result.type;
}
const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
if (!otherwise) return null;
assert$1(outputType);
return new Case(outputType, branches, otherwise);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
for (const [test, expression] of this.branches) {
if (test.evaluate(ctx)) {
return expression.evaluate(ctx);
}
}
return this.otherwise.evaluate(ctx);
}
eachChild(fn) {
for (const [test, expression] of this.branches) {
fn(test);
fn(expression);
}
fn(this.otherwise);
}
outputDefined() {
return this.branches.every(([_, out]) => out.outputDefined()) && this.otherwise.outputDefined();
}
serialize() {
const serialized = ["case"];
this.eachChild((child) => {
serialized.push(child.serialize());
});
return serialized;
}
}
class Slice {
constructor(type, input, beginIndex, endIndex) {
this.type = type;
this.input = input;
this.beginIndex = beginIndex;
this.endIndex = endIndex;
}
static parse(args, context) {
if (args.length <= 2 || args.length >= 5) {
return context.error(`Expected 3 or 4 arguments, but found ${args.length - 1} instead.`);
}
const input = context.parse(args[1], 1, ValueType);
const beginIndex = context.parse(args[2], 2, NumberType);
if (!input || !beginIndex) return null;
if (!isValidType(input.type, [array(ValueType), StringType, ValueType])) {
return context.error(`Expected first argument to be of type array or string, but found ${toString$1(input.type)} instead`);
}
if (args.length === 4) {
const endIndex = context.parse(args[3], 3, NumberType);
if (!endIndex) return null;
return new Slice(input.type, input, beginIndex, endIndex);
} else {
return new Slice(input.type, input, beginIndex);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(ctx) {
const input = this.input.evaluate(ctx);
const beginIndex = this.beginIndex.evaluate(ctx);
if (!isValidNativeType(input, ["string", "array"])) {
throw new RuntimeError(`Expected first argument to be of type array or string, but found ${toString$1(typeOf(input))} instead.`);
}
if (this.endIndex) {
const endIndex = this.endIndex.evaluate(ctx);
return input.slice(beginIndex, endIndex);
}
return input.slice(beginIndex);
}
eachChild(fn) {
fn(this.input);
fn(this.beginIndex);
if (this.endIndex) {
fn(this.endIndex);
}
}
outputDefined() {
return false;
}
serialize() {
if (this.endIndex != null && this.endIndex !== void 0) {
const endIndex = this.endIndex.serialize();
return ["slice", this.input.serialize(), this.beginIndex.serialize(), endIndex];
}
return ["slice", this.input.serialize(), this.beginIndex.serialize()];
}
}
class Split {
constructor(str, delimiter) {
this.type = array(StringType);
this.str = str;
this.delimiter = delimiter;
}
static parse(args, context) {
if (args.length !== 3) {
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`);
}
const str = context.parse(args[1], 1, StringType);
const delimiter = context.parse(args[2], 2, StringType);
if (!str || !delimiter) return;
return new Split(str, delimiter);
}
evaluate(ctx) {
const str = this.str.evaluate(ctx);
const delimiter = this.delimiter.evaluate(ctx);
return str.split(delimiter);
}
eachChild(fn) {
fn(this.str);
fn(this.delimiter);
}
outputDefined() {
return false;
}
serialize() {
return ["split", this.str.serialize(), this.delimiter.serialize()];
}
}
function isComparableType(op, type) {
if (op === "==" || op === "!=") {
return type.kind === "boolean" || type.kind === "string" || type.kind === "number" || type.kind === "null" || type.kind === "value";
} else {
return type.kind === "string" || type.kind === "number" || type.kind === "value";
}
}
function eq(ctx, a, b) {
return a === b;
}
function neq(ctx, a, b) {
return a !== b;
}
function lt(ctx, a, b) {
return a < b;
}
function gt(ctx, a, b) {
return a > b;
}
function lteq(ctx, a, b) {
return a <= b;
}
function gteq(ctx, a, b) {
return a >= b;
}
function eqCollate(ctx, a, b, c) {
return c.compare(a, b) === 0;
}
function neqCollate(ctx, a, b, c) {
return !eqCollate(ctx, a, b, c);
}
function ltCollate(ctx, a, b, c) {
return c.compare(a, b) < 0;
}
function gtCollate(ctx, a, b, c) {
return c.compare(a, b) > 0;
}
function lteqCollate(ctx, a, b, c) {
return c.compare(a, b) <= 0;
}
function gteqCollate(ctx, a, b, c) {
return c.compare(a, b) >= 0;
}
function makeComparison(op, compareBasic, compareWithCollator) {
const isOrderComparison = op !== "==" && op !== "!=";
return class Comparison {
constructor(lhs, rhs, collator) {
this.type = BooleanType;
this.lhs = lhs;
this.rhs = rhs;
this.collator = collator;
this.hasUntypedArgument = lhs.type.kind === "value" || rhs.type.kind === "value";
}
static parse(args, context) {
if (args.length !== 3 && args.length !== 4)
return context.error(`Expected two or three arguments.`);
const op2 = args[0];
let lhs = context.parse(args[1], 1, ValueType);
if (!lhs) return null;
if (!isComparableType(op2, lhs.type)) {
return context.concat(1).error(`"${op2}" comparisons are not supported for type '${toString$1(lhs.type)}'.`);
}
let rhs = context.parse(args[2], 2, ValueType);
if (!rhs) return null;
if (!isComparableType(op2, rhs.type)) {
return context.concat(2).error(`"${op2}" comparisons are not supported for type '${toString$1(rhs.type)}'.`);
}
if (lhs.type.kind !== rhs.type.kind && lhs.type.kind !== "value" && rhs.type.kind !== "value") {
return context.error(`Cannot compare types '${toString$1(lhs.type)}' and '${toString$1(rhs.type)}'.`);
}
if (isOrderComparison) {
if (lhs.type.kind === "value" && rhs.type.kind !== "value") {
lhs = new Assertion(rhs.type, [lhs]);
} else if (lhs.type.kind !== "value" && rhs.type.kind === "value") {
rhs = new Assertion(lhs.type, [rhs]);
}
}
let collator = null;
if (args.length === 4) {
if (lhs.type.kind !== "string" && rhs.type.kind !== "string" && lhs.type.kind !== "value" && rhs.type.kind !== "value") {
return context.error(`Cannot use collator to compare non-string types.`);
}
collator = context.parse(args[3], 3, CollatorType);
if (!collator) return null;
}
return new Comparison(lhs, rhs, collator);
}
evaluate(ctx) {
const lhs = this.lhs.evaluate(ctx);
const rhs = this.rhs.evaluate(ctx);
if (isOrderComparison && this.hasUntypedArgument) {
const lt2 = typeOf(lhs);
const rt = typeOf(rhs);
if (lt2.kind !== rt.kind || !(lt2.kind === "string" || lt2.kind === "number")) {
throw new RuntimeError(`Expected arguments for "${op}" to be (string, string) or (number, number), but found (${lt2.kind}, ${rt.kind}) instead.`);
}
}
if (this.collator && !isOrderComparison && this.hasUntypedArgument) {
const lt2 = typeOf(lhs);
const rt = typeOf(rhs);
if (lt2.kind !== "string" || rt.kind !== "string") {
return compareBasic(ctx, lhs, rhs);
}
}
return this.collator ? compareWithCollator(ctx, lhs, rhs, this.collator.evaluate(ctx)) : compareBasic(ctx, lhs, rhs);
}
eachChild(fn) {
fn(this.lhs);
fn(this.rhs);
if (this.collator) {
fn(this.collator);
}
}
outputDefined() {
return true;
}
serialize() {
const serialized = [op];
this.eachChild((child) => {
serialized.push(child.serialize());
});
return serialized;
}
};
}
const Equals = makeComparison("==", eq, eqCollate);
const NotEquals = makeComparison("!=", neq, neqCollate);
const LessThan = makeComparison("<", lt, ltCollate);
const GreaterThan = makeComparison(">", gt, gtCollate);
const LessThanOrEqual = makeComparison("<=", lteq, lteqCollate);
const GreaterThanOrEqual = makeComparison(">=", gteq, gteqCollate);
class NumberFormat {
// Default 3
constructor(number, locale, currency, unit, minFractionDigits, maxFractionDigits) {
this.type = StringType;
this.number = number;
this.locale = locale;
this.currency = currency;
this.unit = unit;
this.minFractionDigits = minFractionDigits;
this.maxFractionDigits = maxFractionDigits;
}
static parse(args, context) {
if (args.length !== 3)
return context.error(`Expected two arguments.`);
const number = context.parse(args[1], 1, NumberType);
if (!number) return null;
const options = args[2];
if (typeof options !== "object" || Array.isArray(options))
return context.error(`NumberFormat options argument must be an object.`);
let locale = null;
if (options["locale"]) {
locale = context.parseObjectValue(options["locale"], 2, "locale", StringType);
if (!locale) return null;
}
let currency = null;
if (options["currency"]) {
currency = context.parseObjectValue(options["currency"], 2, "currency", StringType);
if (!currency) return null;
}
let unit = null;
if (options["unit"]) {
unit = context.parseObjectValue(options["unit"], 2, "unit", StringType);
if (!unit) return null;
}
let minFractionDigits = null;
if (options["min-fraction-digits"]) {
minFractionDigits = context.parseObjectValue(options["min-fraction-digits"], 2, "min-fraction-digits", NumberType);
if (!minFractionDigits) return null;
}
let maxFractionDigits = null;
if (options["max-fraction-digits"]) {
maxFractionDigits = context.parseObjectValue(options["max-fraction-digits"], 2, "max-fraction-digits", NumberType);
if (!maxFractionDigits) return null;
}
return new NumberFormat(number, locale, currency, unit, minFractionDigits, maxFractionDigits);
}
evaluate(ctx) {
return new Intl.NumberFormat(
this.locale ? this.locale.evaluate(ctx) : [],
{
style: this.currency && "currency" || this.unit && "unit" || "decimal",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
currency: this.currency ? this.currency.evaluate(ctx) : void 0,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
unit: this.unit ? this.unit.evaluate(ctx) : void 0,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
minimumFractionDigits: this.minFractionDigits ? this.minFractionDigits.evaluate(ctx) : void 0,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
maximumFractionDigits: this.maxFractionDigits ? this.maxFractionDigits.evaluate(ctx) : void 0
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
}
).format(this.number.evaluate(ctx));
}
eachChild(fn) {
fn(this.number);
if (this.locale) {
fn(this.locale);
}
if (this.currency) {
fn(this.currency);
}
if (this.unit) {
fn(this.unit);
}
if (this.minFractionDigits) {
fn(this.minFractionDigits);
}
if (this.maxFractionDigits) {
fn(this.maxFractionDigits);
}
}
outputDefined() {
return false;
}
serialize() {
const options = {};
if (this.locale) {
options["locale"] = this.locale.serialize();
}
if (this.currency) {
options["currency"] = this.currency.serialize();
}
if (this.unit) {
options["unit"] = this.unit.serialize();
}
if (this.minFractionDigits) {
options["min-fraction-digits"] = this.minFractionDigits.serialize();
}
if (this.maxFractionDigits) {
options["max-fraction-digits"] = this.maxFractionDigits.serialize();
}
return ["number-format", this.number.serialize(), options];
}
}
class Length {
constructor(input) {
this.type = NumberType;
this.input = input;
}
static parse(args, context) {
if (args.length !== 2)
return context.error(`Expected 1 argument, but found ${args.length - 1} instead.`);
const input = context.parse(args[1], 1);
if (!input) return null;
if (input.type.kind !== "array" && input.type.kind !== "string" && input.type.kind !== "value")
return context.error(`Expected argument of type string or array, but found ${toString$1(input.type)} instead.`);
return new Length(input);
}
evaluate(ctx) {
const input = this.input.evaluate(ctx);
if (typeof input === "string") {
return input.length;
} else if (Array.isArray(input)) {
return input.length;
} else {
throw new RuntimeError(`Expected value to be of type string or array, but found ${toString$1(typeOf(input))} instead.`);
}
}
eachChild(fn) {
fn(this.input);
}
outputDefined() {
return false;
}
serialize() {
const serialized = ["length"];
this.eachChild((child) => {
serialized.push(child.serialize());
});
return serialized;
}
}
function mulberry32(a) {
return function() {
a |= 0;
a = a + 1831565813 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const expressions = {
// special forms
"==": Equals,
"!=": NotEquals,
">": GreaterThan,
"<": LessThan,
">=": GreaterThanOrEqual,
"<=": LessThanOrEqual,
"array": Assertion,
"at": At,
"at-interpolated": AtInterpolated,
"boolean": Assertion,
"case": Case,
"coalesce": Coalesce,
"collator": CollatorExpression,
"format": FormatExpression,
"image": ImageExpression,
"in": In,
"index-of": IndexOf,
"interpolate": Interpolate,
"interpolate-hcl": Interpolate,
"interpolate-lab": Interpolate,
"length": Length,
"let": Let,
"literal": Literal,
"match": Match,
"number": Assertion,
"number-format": NumberFormat,
"object": Assertion,
"slice": Slice,
"step": Step,
"string": Assertion,
"to-boolean": Coercion,
"to-color": Coercion,
"to-number": Coercion,
"to-string": Coercion,
"var": Var,
"within": Within,
"distance": Distance,
"config": Config,
"split": Split
};
function rgba(ctx, [r, g, b, a]) {
r = r.evaluate(ctx);
g = g.evaluate(ctx);
b = b.evaluate(ctx);
const alpha = a ? a.evaluate(ctx) : 1;
const error = validateRGBA(r, g, b, alpha);
if (error) throw new RuntimeError(error);
return new Color(r / 255, g / 255, b / 255, alpha);
}
function hsla(ctx, [h, s, l, a]) {
h = h.evaluate(ctx);
s = s.evaluate(ctx);
l = l.evaluate(ctx);
const alpha = a ? a.evaluate(ctx) : 1;
const error = validateHSLA(h, s, l, alpha);
if (error) throw new RuntimeError(error);
const colorFunction = `hsla(${h}, ${s}%, ${l}%, ${alpha})`;
const color = Color.parse(colorFunction);
if (!color) throw new RuntimeError(`Failed to parse HSLA color: ${colorFunction}`);
return color;
}
function has(key, obj) {
return key in obj;
}
function get(key, obj) {
const v = obj[key];
return typeof v === "undefined" ? null : v;
}
function binarySearch(v, a, i, j) {
while (i <= j) {
const m = i + j >> 1;
if (a[m] === v)
return true;
if (a[m] > v)
j = m - 1;
else
i = m + 1;
}
return false;
}
function varargs(type) {
return { type };
}
function hashString(str) {
let hash = 0;
if (str.length === 0) {
return hash;
}
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return hash;
}
CompoundExpression.register(expressions, {
"error": [
ErrorType,
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [v]) => {
throw new RuntimeError(v.evaluate(ctx));
}
],
"typeof": [
StringType,
[ValueType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [v]) => toString$1(typeOf(v.evaluate(ctx)))
],
"to-rgba": [
array(NumberType, 4),
[ColorType],
(ctx, [v]) => {
return v.evaluate(ctx).toNonPremultipliedRenderColor(null).toArray();
}
],
"to-hsla": [
array(NumberType, 4),
[ColorType],
(ctx, [v]) => {
return v.evaluate(ctx).toNonPremultipliedRenderColor(null).toHslaArray();
}
],
"rgb": [
ColorType,
[NumberType, NumberType, NumberType],
rgba
],
"rgba": [
ColorType,
[NumberType, NumberType, NumberType, NumberType],
rgba
],
"hsl": [
ColorType,
[NumberType, NumberType, NumberType],
hsla
],
"hsla": [
ColorType,
[NumberType, NumberType, NumberType, NumberType],
hsla
],
"has": {
type: BooleanType,
overloads: [
[
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [key]) => has(key.evaluate(ctx), ctx.properties())
],
[
[StringType, ObjectType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [key, obj]) => has(key.evaluate(ctx), obj.evaluate(ctx))
]
]
},
"get": {
type: ValueType,
overloads: [
[
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [key]) => get(key.evaluate(ctx), ctx.properties())
],
[
[StringType, ObjectType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument
(ctx, [key, obj]) => get(key.evaluate(ctx), obj.evaluate(ctx))
]
]
},
"feature-state": [
ValueType,
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [key]) => get(key.evaluate(ctx), ctx.featureState || {})
],
"properties": [
ObjectType,
[],
(ctx) => ctx.properties()
],
"geometry-type": [
StringType,
[],
(ctx) => ctx.geometryType()
],
"worldview": [
StringType,
[],
(ctx) => ctx.globals.worldview || ""
],
"is-active-floor": [
BooleanType,
varargs(StringType),
(ctx, args) => {
const hasActiveFloors = ctx.globals.activeFloors && ctx.globals.activeFloors.size > 0;
if (!hasActiveFloors) {
return false;
}
const floorIds = ctx.globals.activeFloors;
return args.some((arg) => {
const value = arg.evaluate(ctx);
return floorIds.has(value);
});
}
],
"id": [
ValueType,
[],
(ctx) => ctx.id()
],
"zoom": [
NumberType,
[],
(ctx) => ctx.globals.zoom
],
"pitch": [
NumberType,
[],
(ctx) => ctx.globals.pitch || 0
],
"distance-from-center": [
NumberType,
[],
(ctx) => ctx.distanceFromCenter()
],
"measure-light": [
NumberType,
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [s]) => ctx.measureLight(s.evaluate(ctx))
],
"heatmap-density": [
NumberType,
[],
(ctx) => ctx.globals.heatmapDensity || 0
],
"line-progress": [
NumberType,
[],
(ctx) => ctx.globals.lineProgress || 0
],
"raster-value": [
NumberType,
[],
(ctx) => ctx.globals.rasterValue || 0
],
"raster-particle-speed": [
NumberType,
[],
(ctx) => ctx.globals.rasterParticleSpeed || 0
],
"sky-radial-progress": [
NumberType,
[],
(ctx) => ctx.globals.skyRadialProgress || 0
],
"accumulated": [
ValueType,
[],
(ctx) => ctx.globals.accumulated === void 0 ? null : ctx.globals.accumulated
],
"+": [
NumberType,
varargs(NumberType),
(ctx, args) => {
let result = 0;
for (const arg of args) {
result += arg.evaluate(ctx);
}
return result;
}
],
"*": [
NumberType,
varargs(NumberType),
(ctx, args) => {
let result = 1;
for (const arg of args) {
result *= arg.evaluate(ctx);
}
return result;
}
],
"-": {
type: NumberType,
overloads: [
[
[NumberType, NumberType],
(ctx, [a, b]) => a.evaluate(ctx) - b.evaluate(ctx)
],
[
[NumberType],
(ctx, [a]) => -a.evaluate(ctx)
]
]
},
"/": [
NumberType,
[NumberType, NumberType],
(ctx, [a, b]) => a.evaluate(ctx) / b.evaluate(ctx)
],
"%": [
NumberType,
[NumberType, NumberType],
(ctx, [a, b]) => a.evaluate(ctx) % b.evaluate(ctx)
],
"ln2": [
NumberType,
[],
() => Math.LN2
],
"pi": [
NumberType,
[],
() => Math.PI
],
"e": [
NumberType,
[],
() => Math.E
],
"^": [
NumberType,
[NumberType, NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [b, e]) => Math.pow(b.evaluate(ctx), e.evaluate(ctx))
],
"sqrt": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [x]) => Math.sqrt(x.evaluate(ctx))
],
"log10": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.log(n.evaluate(ctx)) / Math.LN10
],
"ln": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.log(n.evaluate(ctx))
],
"log2": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.log2(n.evaluate(ctx))
],
"sin": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.sin(n.evaluate(ctx))
],
"cos": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.cos(n.evaluate(ctx))
],
"tan": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.tan(n.evaluate(ctx))
],
"asin": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.asin(n.evaluate(ctx))
],
"acos": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.acos(n.evaluate(ctx))
],
"atan": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.atan(n.evaluate(ctx))
],
"min": [
NumberType,
varargs(NumberType),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument
(ctx, args) => Math.min(...args.map((arg) => arg.evaluate(ctx)))
],
"max": [
NumberType,
varargs(NumberType),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-argument
(ctx, args) => Math.max(...args.map((arg) => arg.evaluate(ctx)))
],
"abs": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.abs(n.evaluate(ctx))
],
"round": [
NumberType,
[NumberType],
(ctx, [n]) => {
const v = n.evaluate(ctx);
return v < 0 ? -Math.round(-v) : Math.round(v);
}
],
"floor": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.floor(n.evaluate(ctx))
],
"ceil": [
NumberType,
[NumberType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, [n]) => Math.ceil(n.evaluate(ctx))
],
"filter-==": [
BooleanType,
[StringType, ValueType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
(ctx, [k, v]) => ctx.properties()[k.value] === v.value
],
"filter-id-==": [
BooleanType,
[ValueType],
(ctx, [v]) => ctx.id() === v.value
],
"filter-type-==": [
BooleanType,
[StringType],
(ctx, [v]) => ctx.geometryType() === v.value
],
"filter-<": [
BooleanType,
[StringType, ValueType],
(ctx, [k, v]) => {
const a = ctx.properties()[k.value];
const b = v.value;
return typeof a === typeof b && a < b;
}
],
"filter-id-<": [
BooleanType,
[ValueType],
(ctx, [v]) => {
const a = ctx.id();
const b = v.value;
return typeof a === typeof b && a < b;
}
],
"filter->": [
BooleanType,
[StringType, ValueType],
(ctx, [k, v]) => {
const a = ctx.properties()[k.value];
const b = v.value;
return typeof a === typeof b && a > b;
}
],
"filter-id->": [
BooleanType,
[ValueType],
(ctx, [v]) => {
const a = ctx.id();
const b = v.value;
return typeof a === typeof b && a > b;
}
],
"filter-<=": [
BooleanType,
[StringType, ValueType],
(ctx, [k, v]) => {
const a = ctx.properties()[k.value];
const b = v.value;
return typeof a === typeof b && a <= b;
}
],
"filter-id-<=": [
BooleanType,
[ValueType],
(ctx, [v]) => {
const a = ctx.id();
const b = v.value;
return typeof a === typeof b && a <= b;
}
],
"filter->=": [
BooleanType,
[StringType, ValueType],
(ctx, [k, v]) => {
const a = ctx.properties()[k.value];
const b = v.value;
return typeof a === typeof b && a >= b;
}
],
"filter-id->=": [
BooleanType,
[ValueType],
(ctx, [v]) => {
const a = ctx.id();
const b = v.value;
return typeof a === typeof b && a >= b;
}
],
"filter-has": [
BooleanType,
[ValueType],
(ctx, [k]) => k.value in ctx.properties()
],
"filter-has-id": [
BooleanType,
[],
(ctx) => ctx.id() !== null && ctx.id() !== void 0
],
"filter-type-in": [
BooleanType,
[array(StringType)],
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [v]) => v.value.indexOf(ctx.geometryType()) >= 0
],
"filter-id-in": [
BooleanType,
[array(ValueType)],
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [v]) => v.value.indexOf(ctx.id()) >= 0
],
"filter-in-small": [
BooleanType,
[StringType, array(ValueType)],
// assumes v is an array literal
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [k, v]) => v.value.indexOf(ctx.properties()[k.value]) >= 0
],
"filter-in-large": [
BooleanType,
[StringType, array(ValueType)],
// assumes v is a array literal with values sorted in ascending order and of a single type
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
(ctx, [k, v]) => binarySearch(ctx.properties()[k.value], v.value, 0, v.value.length - 1)
],
"all": {
type: BooleanType,
overloads: [
[
[BooleanType, BooleanType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
(ctx, [a, b]) => a.evaluate(ctx) && b.evaluate(ctx)
],
[
varargs(BooleanType),
(ctx, args) => {
for (const arg of args) {
if (!arg.evaluate(ctx))
return false;
}
return true;
}
]
]
},
"any": {
type: BooleanType,
overloads: [
[
[BooleanType, BooleanType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
(ctx, [a, b]) => a.evaluate(ctx) || b.evaluate(ctx)
],
[
varargs(BooleanType),
(ctx, args) => {
for (const arg of args) {
if (arg.evaluate(ctx))
return true;
}
return false;
}
]
]
},
"!": [
BooleanType,
[BooleanType],
(ctx, [b]) => !b.evaluate(ctx)
],
"is-supported-script": [
BooleanType,
[StringType],
// At parse time this will always return true, so we need to exclude this expression with isGlobalPropertyConstant
(ctx, [s]) => {
const isSupportedScript = ctx.globals && ctx.globals.isSupportedScript;
if (isSupportedScript) {
return isSupportedScript(s.evaluate(ctx));
}
return true;
}
],
"upcase": [
StringType,
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [s]) => s.evaluate(ctx).toUpperCase()
],
"downcase": [
StringType,
[StringType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [s]) => s.evaluate(ctx).toLowerCase()
],
"concat": [
StringType,
varargs(ValueType),
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
(ctx, args) => args.map((arg) => toString(arg.evaluate(ctx))).join("")
],
"resolved-locale": [
StringType,
[CollatorType],
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
(ctx, [collator]) => collator.evaluate(ctx).resolvedLocale()
],
"random": [
NumberType,
[NumberType, NumberType, ValueType],
(ctx, args) => {
const [min, max, seed] = args.map((arg) => arg.evaluate(ctx));
if (min > max) {
return min;
}
if (min === max) {
return min;
}
let seedVal;
if (typeof seed === "string") {
seedVal = hashString(seed);
} else if (typeof seed === "number") {
seedVal = seed;
} else {
throw new RuntimeError(`Invalid seed input: ${seed}`);
}
const random = mulberry32(seedVal)();
return min + random * (max - min);
}
]
});
function getConfigDependencies(e) {
if (e instanceof Config) {
const singleConfig = /* @__PURE__ */ new Set([e.key]);
return singleConfig;
}
let result = /* @__PURE__ */ new Set();
e.eachChild((arg) => {
result = /* @__PURE__ */ new Set([...result, ...getConfigDependencies(arg)]);
});
return result;
}
function isIndoorDependent(e) {
if (e instanceof CompoundExpression && e.name === "is-active-floor") {
return true;
}
let result = false;
e.eachChild((arg) => {
if (!result && isIndoorDependent(arg)) {
result = true;
}
});
return result;
}
function success(value) {
return { result: "success", value };
}
function error(value) {
return { result: "error", value };
}
function expressionHasParameter(expression, parameter) {
return !!expression && !!expression.parameters && expression.parameters.indexOf(parameter) > -1;
}
function supportsPropertyExpression(spec) {
return spec["property-type"] === "data-driven";
}
function supportsLightExpression(spec) {
return expressionHasParameter(spec.expression, "measure-light");
}
function supportsZoomExpression(spec) {
return expressionHasParameter(spec.expression, "zoom");
}
function supportsLineProgressExpression(spec) {
return expressionHasParameter(spec.expression, "line-progress");
}
function supportsInterpolation(spec) {
return !!spec.expression && spec.expression.interpolated;
}
function isFunction(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function identityFunction(x) {
return x;
}
function createFunction(parameters, propertySpec) {
const isColor = propertySpec.type === "color";
const zoomAndFeatureDependent = parameters.stops && typeof parameters.stops[0][0] === "object";
const featureDependent = zoomAndFeatureDependent || parameters.property !== void 0;
const zoomDependent = zoomAndFeatureDependent || !featureDependent;
const type = parameters.type || (supportsInterpolation(propertySpec) ? "exponential" : "interval");
if (isColor) {
parameters = Object.assign({}, parameters);
if (parameters.stops) {
parameters.stops = parameters.stops.map((stop) => {
return [stop[0], Color.parse(stop[1])];
});
}
if (parameters.default) {
parameters.default = Color.parse(parameters.default);
} else {
parameters.default = Color.parse(propertySpec.default);
}
}
if (parameters.colorSpace && parameters.colorSpace !== "rgb" && !colorSpaces[parameters.colorSpace]) {
throw new Error(`Unknown color space: ${parameters.colorSpace}`);
}
let innerFun;
let hashedStops;
let categoricalKeyType;
if (type === "exponential") {
innerFun = evaluateExponentialFunction;
} else if (type === "interval") {
innerFun = evaluateIntervalFunction;
} else if (type === "categorical") {
innerFun = evaluateCategoricalFunction;
hashedStops = /* @__PURE__ */ Object.create(null);
for (const stop of parameters.stops) {
hashedStops[stop[0]] = stop[1];
}
categoricalKeyType = typeof parameters.stops[0][0];
} else if (type === "identity") {
innerFun = evaluateIdentityFunction;
} else {
throw new Error(`Unknown function type "${type}"`);
}
if (zoomAndFeatureDependent) {
const featureFunctions = {};
const zoomStops = [];
for (let s = 0; s < parameters.stops.length; s++) {
const stop = parameters.stops[s];
const zoom = stop[0].zoom;
if (featureFunctions[zoom] === void 0) {
featureFunctions[zoom] = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
type: parameters.type,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
property: parameters.property,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
default: parameters.default,
stops: []
};
zoomStops.push(zoom);
}
featureFunctions[zoom].stops.push([stop[0].value, stop[1]]);
}
const featureFunctionStops = [];
for (const z of zoomStops) {
featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z], propertySpec)]);
}
const interpolationType = { name: "linear" };
return {
kind: "composite",
interpolationType,
interpolationFactor: Interpolate.interpolationFactor.bind(void 0, interpolationType),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access
zoomStops: featureFunctionStops.map((s) => s[0]),
evaluate({ zoom }, properties) {
return evaluateExponentialFunction({
stops: featureFunctionStops,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
base: parameters.base
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
}, propertySpec, zoom).evaluate(zoom, properties);
}
};
} else if (zoomDependent) {
const interpolationType = type === "exponential" ? (
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
{ name: "exponential", base: parameters.base !== void 0 ? parameters.base : 1 }
) : null;
return {
kind: "camera",
interpolationType,
interpolationFactor: Interpolate.interpolationFactor.bind(void 0, interpolationType),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
zoomStops: parameters.stops.map((s) => s[0]),
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
evaluate: ({ zoom }) => innerFun(parameters, propertySpec, zoom, hashedStops, categoricalKeyType)
};
} else {
return {
kind: "source",
evaluate(_, feature) {
const value = feature && feature.properties ? feature.properties[parameters.property] : void 0;
if (value === void 0) {
return coalesce(parameters.default, propertySpec.default);
}
return innerFun(parameters, propertySpec, value, hashedStops, categoricalKeyType);
}
};
}
}
function coalesce(a, b, c) {
if (a !== void 0) return a;
if (b !== void 0) return b;
if (c !== void 0) return c;
}
function evaluateCategoricalFunction(parameters, propertySpec, input, hashedStops, keyType) {
const evaluated = typeof input === keyType ? hashedStops[input] : void 0;
return coalesce(evaluated, parameters.default, propertySpec.default);
}
function evaluateIntervalFunction(parameters, propertySpec, input) {
if (!isNumber(input)) return coalesce(parameters.default, propertySpec.default);
const n = parameters.stops.length;
if (n === 1) return parameters.stops[0][1];
if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
if (input >= parameters.stops[n - 1][0]) return parameters.stops[n - 1][1];
const index = findStopLessThanOrEqualTo(parameters.stops.map((stop) => stop[0]), input);
return parameters.stops[index][1];
}
function evaluateExponentialFunction(parameters, propertySpec, input) {
const base = parameters.base !== void 0 ? parameters.base : 1;
if (!isNumber(input)) return coalesce(parameters.default, propertySpec.default);
const n = parameters.stops.length;
if (n === 1) return parameters.stops[0][1];
if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
if (input >= parameters.stops[n - 1][0]) return parameters.stops[n - 1][1];
const index = findStopLessThanOrEqualTo(parameters.stops.map((stop) => stop[0]), input);
const t = interpolationFactor(
input,
base,
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
parameters.stops[index][0],
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
parameters.stops[index + 1][0]
);
const outputLower = parameters.stops[index][1];
const outputUpper = parameters.stops[index + 1][1];
let interp = interpolate$1[propertySpec.type] || identityFunction;
if (parameters.colorSpace && parameters.colorSpace !== "rgb") {
const colorspace = colorSpaces[parameters.colorSpace];
interp = (a, b) => colorspace.reverse(colorspace.interpolate(colorspace.forward(a), colorspace.forward(b), t));
}
if (typeof outputLower.evaluate === "function") {
return {
evaluate(...args) {
const evaluatedLower = outputLower.evaluate.apply(void 0, args);
const evaluatedUpper = outputUpper.evaluate.apply(void 0, args);
if (evaluatedLower === void 0 || evaluatedUpper === void 0) {
return void 0;
}
return interp(evaluatedLower, evaluatedUpper, t);
}
};
}
return interp(outputLower, outputUpper, t);
}
function evaluateIdentityFunction(parameters, propertySpec, input) {
if (propertySpec.type === "color") {
input = Color.parse(input);
} else if (propertySpec.type === "formatted") {
input = Formatted.fromString(input.toString());
} else if (propertySpec.type === "resolvedImage") {
input = ResolvedImage.build(input.toString());
} else if (getType(input) !== propertySpec.type && (propertySpec.type !== "enum" || !propertySpec.values[input])) {
input = void 0;
}
return coalesce(input, parameters.default, propertySpec.default);
}
function interpolationFactor(input, base, lowerValue, upperValue) {
const difference = upperValue - lowerValue;
const progress = input - lowerValue;
if (difference === 0) {
return 0;
} else if (base === 1) {
return progress / difference;
} else {
return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
}
}
class StyleExpression {
constructor(expression, propertySpec, scope, options, iconImageUseTheme) {
this.expression = expression;
this._warningHistory = {};
this._scope = scope;
this._options = options;
this._iconImageUseTheme = iconImageUseTheme;
this._evaluator = new EvaluationContext(scope, options, iconImageUseTheme);
this._defaultValue = propertySpec ? getDefaultValue(propertySpec) : null;
this._enumValues = propertySpec && propertySpec.type === "enum" ? propertySpec.values : null;
this.configDependencies = getConfigDependencies(expression);
this.isIndoorDependent = isIndoorDependent(expression);
}
evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection, featureTileCoord, featureDistanceData) {
this._evaluator.globals = globals;
this._evaluator.feature = feature;
this._evaluator.featureState = featureState;
this._evaluator.canonical = canonical || null;
this._evaluator.availableImages = availableImages || null;
this._evaluator.formattedSection = formattedSection;
this._evaluator.featureTileCoord = featureTileCoord || null;
this._evaluator.featureDistanceData = featureDistanceData || null;
return this.expression.evaluate(this._evaluator);
}
evaluate(globals, feature, featureState, canonical, availableImages, formattedSection, featureTileCoord, featureDistanceData, iconImageUseTheme) {
if (!this._evaluator) {
this._evaluator = new EvaluationContext(this._scope, this._options, this._iconImageUseTheme);
}
this._evaluator.globals = globals;
this._evaluator.feature = feature || null;
this._evaluator.featureState = featureState || null;
this._evaluator.canonical = canonical || null;
this._evaluator.availableImages = availableImages || null;
this._evaluator.formattedSection = formattedSection || null;
this._evaluator.featureTileCoord = featureTileCoord || null;
this._evaluator.featureDistanceData = featureDistanceData || null;
this._evaluator.iconImageUseTheme = iconImageUseTheme || null;
try {
const val = this.expression.evaluate(this._evaluator);
if (val === null || val === void 0 || typeof val === "number" && val !== val) {
return this._defaultValue;
}
if (this._enumValues && !(val in this._enumValues)) {
throw new RuntimeError(`Expected value to be one of ${Object.keys(this._enumValues).map((v) => JSON.stringify(v)).join(", ")}, but found ${JSON.stringify(val)} instead.`);
}
return val;
} catch (e) {
const error2 = e;
if (!this._warningHistory[error2.message]) {
this._warningHistory[error2.message] = true;
if (typeof console !== "undefined") {
console.warn(`Failed to evaluate expression "${JSON.stringify(this.expression.serialize())}". ${error2.message}`);
}
}
return this._defaultValue;
}
}
}
function isExpression(expression) {
return Array.isArray(expression) && expression.length > 0 && typeof expression[0] === "string" && expression[0] in expressions;
}
function createExpression(expression, propertySpec, scope, options, iconImageUseTheme) {
const parser = new ParsingContext(expressions, [], propertySpec ? getExpectedType(propertySpec) : void 0, void 0, void 0, scope, options, iconImageUseTheme);
const parsed = parser.parse(
expression,
void 0,
void 0,
void 0,
propertySpec && propertySpec.type === "string" ? { typeAnnotation: "coerce" } : void 0
);
if (!parsed) {
assert$1(parser.errors.length > 0);
return error(parser.errors);
}
return success(new StyleExpression(parsed, propertySpec, scope, options, iconImageUseTheme));
}
class ZoomConstantExpression {
constructor(kind, expression, isLightConstant, isLineProgressConstant) {
this.kind = kind;
this._styleExpression = expression;
this.isLightConstant = isLightConstant;
this.isLineProgressConstant = isLineProgressConstant;
this.isStateDependent = kind !== "constant" && !isStateConstant(expression.expression);
this.configDependencies = getConfigDependencies(expression.expression);
this.isIndoorDependent = isIndoorDependent(expression.expression);
}
evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection) {
return this._styleExpression.evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection);
}
evaluate(globals, feature, featureState, canonical, availableImages, formattedSection, iconImageUseTheme) {
return this._styleExpression.evaluate(globals, feature, featureState, canonical, availableImages, formattedSection, void 0, void 0, iconImageUseTheme);
}
}
class ZoomDependentExpression {
constructor(kind, expression, zoomStops, interpolationType, isLightConstant, isLineProgressConstant) {
this.kind = kind;
this.zoomStops = zoomStops;
this._styleExpression = expression;
this.isStateDependent = kind !== "camera" && !isStateConstant(expression.expression);
this.isIndoorDependent = isIndoorDependent(expression.expression);
this.isLightConstant = isLightConstant;
this.isLineProgressConstant = isLineProgressConstant;
this.configDependencies = getConfigDependencies(expression.expression);
this.interpolationType = interpolationType;
}
evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection) {
return this._styleExpression.evaluateWithoutErrorHandling(globals, feature, featureState, canonical, availableImages, formattedSection);
}
evaluate(globals, feature, featureState, canonical, availableImages, formattedSection) {
return this._styleExpression.evaluate(globals, feature, featureState, canonical, availableImages, formattedSection);
}
interpolationFactor(input, lower, upper) {
if (this.interpolationType) {
return Interpolate.interpolationFactor(this.interpolationType, input, lower, upper);
} else {
return 0;
}
}
}
function createPropertyExpression(expression, propertySpec, scope, options, iconImageUseTheme) {
expression = createExpression(expression, propertySpec, scope, options, iconImageUseTheme);
if (expression.result === "error") {
return expression;
}
const parsed = expression.value.expression;
const isFeatureConstant$1 = isFeatureConstant(parsed);
if (!isFeatureConstant$1 && !supportsPropertyExpression(propertySpec)) {
return error([new ParsingError("", "data expressions not supported")]);
}
const isZoomConstant = isGlobalPropertyConstant(parsed, ["zoom", "pitch", "distance-from-center"]);
if (!isZoomConstant && !supportsZoomExpression(propertySpec)) {
return error([new ParsingError("", "zoom expressions not supported")]);
}
const isLightConstant = isGlobalPropertyConstant(parsed, ["measure-light"]);
if (!isLightConstant && !supportsLightExpression(propertySpec)) {
return error([new ParsingError("", "measure-light expression not supported")]);
}
const isLineProgressConstant = isGlobalPropertyConstant(parsed, ["line-progress"]);
if (!isLineProgressConstant && !supportsLineProgressExpression(propertySpec)) {
return error([new ParsingError("", "line-progress expression not supported")]);
}
const canRelaxZoomRestriction = propertySpec.expression && propertySpec.expression.relaxZoomRestriction;
const zoomCurve = findZoomCurve(parsed);
if (!zoomCurve && !isZoomConstant && !canRelaxZoomRestriction) {
return error([new ParsingError("", '"zoom" expression may only be used as input to a top-level "step" or "interpolate" expression, or in the properties of atmosphere.')]);
} else if (zoomCurve instanceof ParsingError) {
return error([zoomCurve]);
} else if (zoomCurve instanceof Interpolate && !supportsInterpolation(propertySpec)) {
return error([new ParsingError("", '"interpolate" expressions cannot be used with this property')]);
}
if (!zoomCurve) {
return success(isFeatureConstant$1 && isLineProgressConstant ? (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
new ZoomConstantExpression("constant", expression.value, isLightConstant, isLineProgressConstant)
) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
new ZoomConstantExpression("source", expression.value, isLightConstant, isLineProgressConstant)
));
}
const interpolationType = zoomCurve instanceof Interpolate ? zoomCurve.interpolation : void 0;
return success(isFeatureConstant$1 && isLineProgressConstant ? (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
new ZoomDependentExpression("camera", expression.value, zoomCurve.labels, interpolationType, isLightConstant, isLineProgressConstant)
) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
new ZoomDependentExpression("composite", expression.value, zoomCurve.labels, interpolationType, isLightConstant, isLineProgressConstant)
));
}
class StylePropertyFunction {
constructor(parameters, specification) {
this._parameters = parameters;
this._specification = specification;
Object.assign(this, createFunction(this._parameters, this._specification));
}
static deserialize(serialized) {
return new StylePropertyFunction(serialized._parameters, serialized._specification);
}
static serialize(input) {
return {
_parameters: input._parameters,
_specification: input._specification
};
}
}
function normalizePropertyExpression(value, specification, scope, options, iconImageUseTheme) {
if (isFunction(value)) {
return new StylePropertyFunction(value, specification);
} else if (isExpression(value) || Array.isArray(value) && value.length > 0) {
const expression = createPropertyExpression(value, specification, scope, options, iconImageUseTheme);
if (expression.result === "error") {
throw new Error(expression.value.map((err) => `${err.key}: ${err.message}`).join(", "));
}
return expression.value;
} else {
let constant = value;
if (typeof value === "string" && specification.type === "color") {
constant = Color.parse(value);
}
return {
kind: "constant",
configDependencies: /* @__PURE__ */ new Set(),
isIndoorDependent: false,
evaluate: () => constant
};
}
}
function findZoomCurve(expression) {
let result = null;
if (expression instanceof Let) {
result = findZoomCurve(expression.result);
} else if (expression instanceof Coalesce) {
for (const arg of expression.args) {
result = findZoomCurve(arg);
if (result) {
break;
}
}
} else if ((expression instanceof Step || expression instanceof Interpolate) && expression.input instanceof CompoundExpression && expression.input.name === "zoom") {
result = expression;
}
if (result instanceof ParsingError) {
return result;
}
expression.eachChild((child) => {
const childResult = findZoomCurve(child);
if (childResult instanceof ParsingError) {
result = childResult;
} else if (result && childResult && result !== childResult) {
result = new ParsingError("", 'Only one zoom-based "step" or "interpolate" subexpression may be used in an expression.');
}
});
return result;
}
function getExpectedType(spec) {
const types = {
color: ColorType,
string: StringType,
number: NumberType,
enum: StringType,
boolean: BooleanType,
formatted: FormattedType,
resolvedImage: ResolvedImageType
};
if (spec.type === "array") {
return array(types[spec.value] || ValueType, spec.length);
}
return types[spec.type];
}
function getDefaultValue(spec) {
if (spec.type === "color" && (isFunction(spec.default) || Array.isArray(spec.default))) {
return new Color(0, 0, 0, 0);
} else if (spec.type === "color") {
return Color.parse(spec.default) || null;
} else if (spec.default === void 0) {
return null;
} else {
return spec.default;
}
}
var gridIndex;
var hasRequiredGridIndex;
function requireGridIndex () {
if (hasRequiredGridIndex) return gridIndex;
hasRequiredGridIndex = 1;
'use strict';
gridIndex = GridIndex;
var NUM_PARAMS = 3;
function GridIndex(extent, n, padding) {
var cells = this.cells = [];
if (extent instanceof ArrayBuffer) {
this.arrayBuffer = extent;
var array = new Int32Array(this.arrayBuffer);
extent = array[0];
n = array[1];
padding = array[2];
this.d = n + 2 * padding;
for (var k = 0; k < this.d * this.d; k++) {
var start = array[NUM_PARAMS + k];
var end = array[NUM_PARAMS + k + 1];
cells.push(start === end ?
null :
array.subarray(start, end));
}
var keysOffset = array[NUM_PARAMS + cells.length];
var bboxesOffset = array[NUM_PARAMS + cells.length + 1];
this.keys = array.subarray(keysOffset, bboxesOffset);
this.bboxes = array.subarray(bboxesOffset);
this.insert = this._insertReadonly;
} else {
this.d = n + 2 * padding;
for (var i = 0; i < this.d * this.d; i++) {
cells.push([]);
}
this.keys = [];
this.bboxes = [];
}
this.n = n;
this.extent = extent;
this.padding = padding;
this.scale = n / extent;
this.uid = 0;
var p = (padding / n) * extent;
this.min = -p;
this.max = extent + p;
}
GridIndex.prototype.insert = function(key, x1, y1, x2, y2) {
this._forEachCell(x1, y1, x2, y2, this._insertCell, this.uid++);
this.keys.push(key);
this.bboxes.push(x1);
this.bboxes.push(y1);
this.bboxes.push(x2);
this.bboxes.push(y2);
};
GridIndex.prototype._insertReadonly = function() {
throw 'Cannot insert into a GridIndex created from an ArrayBuffer.';
};
GridIndex.prototype._insertCell = function(x1, y1, x2, y2, cellIndex, uid) {
this.cells[cellIndex].push(uid);
};
GridIndex.prototype.query = function(x1, y1, x2, y2, intersectionTest) {
var min = this.min;
var max = this.max;
if (x1 <= min && y1 <= min && max <= x2 && max <= y2 && !intersectionTest) {
// We use `Array#slice` because `this.keys` may be a `Int32Array` and
// some browsers (Safari and IE) do not support `TypedArray#slice`
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice#Browser_compatibility
return Array.prototype.slice.call(this.keys);
} else {
var result = [];
var seenUids = {};
this._forEachCell(x1, y1, x2, y2, this._queryCell, result, seenUids, intersectionTest);
return result;
}
};
GridIndex.prototype._queryCell = function(x1, y1, x2, y2, cellIndex, result, seenUids, intersectionTest) {
var cell = this.cells[cellIndex];
if (cell !== null) {
var keys = this.keys;
var bboxes = this.bboxes;
for (var u = 0; u < cell.length; u++) {
var uid = cell[u];
if (seenUids[uid] === undefined) {
var offset = uid * 4;
if (intersectionTest ?
intersectionTest(bboxes[offset + 0], bboxes[offset + 1], bboxes[offset + 2], bboxes[offset + 3]) :
((x1 <= bboxes[offset + 2]) &&
(y1 <= bboxes[offset + 3]) &&
(x2 >= bboxes[offset + 0]) &&
(y2 >= bboxes[offset + 1]))) {
seenUids[uid] = true;
result.push(keys[uid]);
} else {
seenUids[uid] = false;
}
}
}
}
};
GridIndex.prototype._forEachCell = function(x1, y1, x2, y2, fn, arg1, arg2, intersectionTest) {
var cx1 = this._convertToCellCoord(x1);
var cy1 = this._convertToCellCoord(y1);
var cx2 = this._convertToCellCoord(x2);
var cy2 = this._convertToCellCoord(y2);
for (var x = cx1; x <= cx2; x++) {
for (var y = cy1; y <= cy2; y++) {
var cellIndex = this.d * y + x;
if (intersectionTest && !intersectionTest(
this._convertFromCellCoord(x),
this._convertFromCellCoord(y),
this._convertFromCellCoord(x + 1),
this._convertFromCellCoord(y + 1))) continue;
if (fn.call(this, x1, y1, x2, y2, cellIndex, arg1, arg2, intersectionTest)) return;
}
}
};
GridIndex.prototype._convertFromCellCoord = function(x) {
return (x - this.padding) / this.scale;
};
GridIndex.prototype._convertToCellCoord = function(x) {
return Math.max(0, Math.min(this.d - 1, Math.floor(x * this.scale) + this.padding));
};
GridIndex.prototype.toArrayBuffer = function() {
if (this.arrayBuffer) return this.arrayBuffer;
var cells = this.cells;
var metadataLength = NUM_PARAMS + this.cells.length + 1 + 1;
var totalCellLength = 0;
for (var i = 0; i < this.cells.length; i++) {
totalCellLength += this.cells[i].length;
}
var array = new Int32Array(metadataLength + totalCellLength + this.keys.length + this.bboxes.length);
array[0] = this.extent;
array[1] = this.n;
array[2] = this.padding;
var offset = metadataLength;
for (var k = 0; k < cells.length; k++) {
var cell = cells[k];
array[NUM_PARAMS + k] = offset;
array.set(cell, offset);
offset += cell.length;
}
array[NUM_PARAMS + cells.length] = offset;
array.set(this.keys, offset);
offset += this.keys.length;
array[NUM_PARAMS + cells.length + 1] = offset;
array.set(this.bboxes, offset);
offset += this.bboxes.length;
return array.buffer;
};
return gridIndex;
}
var gridIndexExports = requireGridIndex();
var Grid = /*@__PURE__*/getDefaultExportFromCjs(gridIndexExports);
const registry = {};
function register(klass, name, options = {}) {
assert$1(name, "Can't register a class without a name.");
assert$1(!registry[name], `Class "${name}" is already registered.`);
Object.defineProperty(klass, "_classRegistryKey", {
value: name,
writable: false
});
registry[name] = {
klass,
omit: options.omit || []
};
}
register(Object, "Object");
Grid.serialize = function serialize2(grid, transferables) {
const buffer = grid.toArrayBuffer();
if (transferables) {
transferables.add(buffer);
}
return { buffer };
};
Grid.deserialize = function deserialize2(serialized) {
return new Grid(serialized.buffer);
};
Object.defineProperty(Grid, "name", { value: "Grid" });
register(Grid, "Grid");
delete Point.prototype.constructor;
register(Color, "Color");
register(Error, "Error");
register(Formatted, "Formatted");
register(FormattedSection, "FormattedSection");
register(AJAXError, "AJAXError");
register(ResolvedImage, "ResolvedImage");
register(StylePropertyFunction, "StylePropertyFunction");
register(StyleExpression, "StyleExpression", { omit: ["_evaluator"] });
register(ImageId, "ImageId");
register(ImageVariant, "ImageVariant");
register(ZoomDependentExpression, "ZoomDependentExpression");
register(ZoomConstantExpression, "ZoomConstantExpression");
register(CompoundExpression, "CompoundExpression", { omit: ["_evaluate"] });
for (const name in expressions) {
if (!registry[expressions[name]._classRegistryKey]) register(expressions[name], `Expression${name}`);
}
function isArrayBuffer(val) {
return val && (val instanceof ArrayBuffer || val.constructor && val.constructor.name === "ArrayBuffer");
}
function serialize(input, transferables) {
if (input === null || input === void 0 || typeof input === "boolean" || typeof input === "number" || typeof input === "string" || input instanceof Boolean || input instanceof Number || input instanceof String || input instanceof Date || input instanceof RegExp) {
return input;
}
if (isArrayBuffer(input) || input instanceof ImageBitmap) {
if (transferables) {
transferables.add(input);
}
return input;
}
if (ArrayBuffer.isView(input)) {
if (transferables) {
transferables.add(input.buffer);
}
return input;
}
if (input instanceof ImageData) {
if (transferables) {
transferables.add(input.data.buffer);
}
return input;
}
if (Array.isArray(input)) {
const serialized = [];
for (const item of input) {
serialized.push(serialize(item, transferables));
}
return serialized;
}
if (input instanceof Map) {
const properties = { "$name": "Map", entries: [] };
for (const [key, value] of input.entries()) {
properties.entries.push(serialize(key), serialize(value, transferables));
}
return properties;
}
if (input instanceof Set) {
const properties = { "$name": "Set" };
let idx = 0;
for (const value of input.values()) {
properties[++idx] = serialize(value);
}
return properties;
}
if (typeof input === "bigint") {
return { $name: "BigInt", value: input.toString() };
}
if (typeof input === "object") {
const klass = input.constructor;
const name = klass._classRegistryKey;
if (!name) {
throw new Error(`Can't serialize object of unregistered class "${klass.name}".`);
}
assert$1(registry[name]);
const properties = klass.serialize ? (
// (Temporary workaround) allow a class to provide static
// `serialize()` and `deserialize()` methods to bypass the generic
// approach.
// This temporary workaround lets us use the generic serialization
// approach for objects whose members include instances of dynamic
// StructArray types. Once we refactor StructArray to be static,
// we can remove this complexity.
klass.serialize(input, transferables)
) : {};
if (!klass.serialize) {
for (const key in input) {
if (!input.hasOwnProperty(key)) continue;
if (registry[name].omit.indexOf(key) >= 0) continue;
const property = input[key];
properties[key] = serialize(property, transferables);
}
if (input instanceof Error) {
properties["message"] = input.message;
}
} else {
assert$1(!transferables || !transferables.has(properties));
}
if (properties["$name"]) {
throw new Error("$name property is reserved for worker serialization logic.");
}
if (name !== "Object") {
properties["$name"] = name;
}
return properties;
}
throw new Error(`can't serialize object of type ${typeof input}`);
}
function deserialize(input) {
if (input === null || input === void 0 || typeof input === "boolean" || typeof input === "number" || typeof input === "string" || input instanceof Boolean || input instanceof Number || input instanceof String || input instanceof Date || input instanceof RegExp || isArrayBuffer(input) || input instanceof ImageBitmap || ArrayBuffer.isView(input) || input instanceof ImageData) {
return input;
}
if (Array.isArray(input)) {
return input.map(deserialize);
}
if (typeof input === "object") {
const name = input.$name || "Object";
if (name === "Map") {
const entries = input.entries || [];
const map = /* @__PURE__ */ new Map();
for (let i = 0; i < entries.length; i += 2) {
map.set(deserialize(entries[i]), deserialize(entries[i + 1]));
}
return map;
}
if (name === "Set") {
const set = /* @__PURE__ */ new Set();
for (const key of Object.keys(input)) {
if (key === "$name")
continue;
const value = input[key];
set.add(deserialize(value));
}
return set;
}
if (name === "BigInt") {
return BigInt(input.value);
}
const { klass } = registry[name];
if (!klass) {
throw new Error(`Can't deserialize unregistered class "${name}".`);
}
if (klass.deserialize) {
return klass.deserialize(input);
}
const result = Object.create(klass.prototype);
for (const key of Object.keys(input)) {
if (key === "$name")
continue;
const value = input[key];
result[key] = deserialize(value);
}
return result;
}
throw new Error(`can't deserialize object of type ${typeof input}`);
}
const unicodeBlockLookup = {
// 'Basic Latin': (char) => char >= 0x0000 && char <= 0x007F,
"Latin-1 Supplement": (char) => char >= 128 && char <= 255,
// 'Latin Extended-A': (char) => char >= 0x0100 && char <= 0x017F,
// 'Latin Extended-B': (char) => char >= 0x0180 && char <= 0x024F,
// 'IPA Extensions': (char) => char >= 0x0250 && char <= 0x02AF,
// 'Spacing Modifier Letters': (char) => char >= 0x02B0 && char <= 0x02FF,
// 'Combining Diacritical Marks': (char) => char >= 0x0300 && char <= 0x036F,
// 'Greek and Coptic': (char) => char >= 0x0370 && char <= 0x03FF,
// 'Cyrillic': (char) => char >= 0x0400 && char <= 0x04FF,
// 'Cyrillic Supplement': (char) => char >= 0x0500 && char <= 0x052F,
// 'Armenian': (char) => char >= 0x0530 && char <= 0x058F,
//'Hebrew': (char) => char >= 0x0590 && char <= 0x05FF,
"Arabic": (char) => char >= 1536 && char <= 1791,
//'Syriac': (char) => char >= 0x0700 && char <= 0x074F,
"Arabic Supplement": (char) => char >= 1872 && char <= 1919,
// 'Thaana': (char) => char >= 0x0780 && char <= 0x07BF,
// 'NKo': (char) => char >= 0x07C0 && char <= 0x07FF,
// 'Samaritan': (char) => char >= 0x0800 && char <= 0x083F,
// 'Mandaic': (char) => char >= 0x0840 && char <= 0x085F,
// 'Syriac Supplement': (char) => char >= 0x0860 && char <= 0x086F,
"Arabic Extended-A": (char) => char >= 2208 && char <= 2303,
// 'Devanagari': (char) => char >= 0x0900 && char <= 0x097F,
// 'Bengali': (char) => char >= 0x0980 && char <= 0x09FF,
// 'Gurmukhi': (char) => char >= 0x0A00 && char <= 0x0A7F,
// 'Gujarati': (char) => char >= 0x0A80 && char <= 0x0AFF,
// 'Oriya': (char) => char >= 0x0B00 && char <= 0x0B7F,
// 'Tamil': (char) => char >= 0x0B80 && char <= 0x0BFF,
// 'Telugu': (char) => char >= 0x0C00 && char <= 0x0C7F,
// 'Kannada': (char) => char >= 0x0C80 && char <= 0x0CFF,
// 'Malayalam': (char) => char >= 0x0D00 && char <= 0x0D7F,
// 'Sinhala': (char) => char >= 0x0D80 && char <= 0x0DFF,
// 'Thai': (char) => char >= 0x0E00 && char <= 0x0E7F,
// 'Lao': (char) => char >= 0x0E80 && char <= 0x0EFF,
// 'Tibetan': (char) => char >= 0x0F00 && char <= 0x0FFF,
// 'Myanmar': (char) => char >= 0x1000 && char <= 0x109F,
// 'Georgian': (char) => char >= 0x10A0 && char <= 0x10FF,
"Hangul Jamo": (char) => char >= 4352 && char <= 4607,
// 'Ethiopic': (char) => char >= 0x1200 && char <= 0x137F,
// 'Ethiopic Supplement': (char) => char >= 0x1380 && char <= 0x139F,
// 'Cherokee': (char) => char >= 0x13A0 && char <= 0x13FF,
"Unified Canadian Aboriginal Syllabics": (char) => char >= 5120 && char <= 5759,
// 'Ogham': (char) => char >= 0x1680 && char <= 0x169F,
// 'Runic': (char) => char >= 0x16A0 && char <= 0x16FF,
// 'Tagalog': (char) => char >= 0x1700 && char <= 0x171F,
// 'Hanunoo': (char) => char >= 0x1720 && char <= 0x173F,
// 'Buhid': (char) => char >= 0x1740 && char <= 0x175F,
// 'Tagbanwa': (char) => char >= 0x1760 && char <= 0x177F,
"Khmer": (char) => char >= 6016 && char <= 6143,
// 'Mongolian': (char) => char >= 0x1800 && char <= 0x18AF,
"Unified Canadian Aboriginal Syllabics Extended": (char) => char >= 6320 && char <= 6399,
// 'Limbu': (char) => char >= 0x1900 && char <= 0x194F,
// 'Tai Le': (char) => char >= 0x1950 && char <= 0x197F,
// 'New Tai Lue': (char) => char >= 0x1980 && char <= 0x19DF,
// 'Khmer Symbols': (char) => char >= 0x19E0 && char <= 0x19FF,
// 'Buginese': (char) => char >= 0x1A00 && char <= 0x1A1F,
// 'Tai Tham': (char) => char >= 0x1A20 && char <= 0x1AAF,
// 'Combining Diacritical Marks Extended': (char) => char >= 0x1AB0 && char <= 0x1AFF,
// 'Balinese': (char) => char >= 0x1B00 && char <= 0x1B7F,
// 'Sundanese': (char) => char >= 0x1B80 && char <= 0x1BBF,
// 'Batak': (char) => char >= 0x1BC0 && char <= 0x1BFF,
// 'Lepcha': (char) => char >= 0x1C00 && char <= 0x1C4F,
// 'Ol Chiki': (char) => char >= 0x1C50 && char <= 0x1C7F,
// 'Cyrillic Extended-C': (char) => char >= 0x1C80 && char <= 0x1C8F,
// 'Georgian Extended': (char) => char >= 0x1C90 && char <= 0x1CBF,
// 'Sundanese Supplement': (char) => char >= 0x1CC0 && char <= 0x1CCF,
// 'Vedic Extensions': (char) => char >= 0x1CD0 && char <= 0x1CFF,
// 'Phonetic Extensions': (char) => char >= 0x1D00 && char <= 0x1D7F,
// 'Phonetic Extensions Supplement': (char) => char >= 0x1D80 && char <= 0x1DBF,
// 'Combining Diacritical Marks Supplement': (char) => char >= 0x1DC0 && char <= 0x1DFF,
// 'Latin Extended Additional': (char) => char >= 0x1E00 && char <= 0x1EFF,
// 'Greek Extended': (char) => char >= 0x1F00 && char <= 0x1FFF,
"General Punctuation": (char) => char >= 8192 && char <= 8303,
// 'Superscripts and Subscripts': (char) => char >= 0x2070 && char <= 0x209F,
// 'Currency Symbols': (char) => char >= 0x20A0 && char <= 0x20CF,
// 'Combining Diacritical Marks for Symbols': (char) => char >= 0x20D0 && char <= 0x20FF,
"Letterlike Symbols": (char) => char >= 8448 && char <= 8527,
"Number Forms": (char) => char >= 8528 && char <= 8591,
// 'Arrows': (char) => char >= 0x2190 && char <= 0x21FF,
// 'Mathematical Operators': (char) => char >= 0x2200 && char <= 0x22FF,
"Miscellaneous Technical": (char) => char >= 8960 && char <= 9215,
"Control Pictures": (char) => char >= 9216 && char <= 9279,
"Optical Character Recognition": (char) => char >= 9280 && char <= 9311,
"Enclosed Alphanumerics": (char) => char >= 9312 && char <= 9471,
// 'Box Drawing': (char) => char >= 0x2500 && char <= 0x257F,
// 'Block Elements': (char) => char >= 0x2580 && char <= 0x259F,
"Geometric Shapes": (char) => char >= 9632 && char <= 9727,
"Miscellaneous Symbols": (char) => char >= 9728 && char <= 9983,
// 'Dingbats': (char) => char >= 0x2700 && char <= 0x27BF,
// 'Miscellaneous Mathematical Symbols-A': (char) => char >= 0x27C0 && char <= 0x27EF,
// 'Supplemental Arrows-A': (char) => char >= 0x27F0 && char <= 0x27FF,
// 'Braille Patterns': (char) => char >= 0x2800 && char <= 0x28FF,
// 'Supplemental Arrows-B': (char) => char >= 0x2900 && char <= 0x297F,
// 'Miscellaneous Mathematical Symbols-B': (char) => char >= 0x2980 && char <= 0x29FF,
// 'Supplemental Mathematical Operators': (char) => char >= 0x2A00 && char <= 0x2AFF,
"Miscellaneous Symbols and Arrows": (char) => char >= 11008 && char <= 11263,
// 'Glagolitic': (char) => char >= 0x2C00 && char <= 0x2C5F,
// 'Latin Extended-C': (char) => char >= 0x2C60 && char <= 0x2C7F,
// 'Coptic': (char) => char >= 0x2C80 && char <= 0x2CFF,
// 'Georgian Supplement': (char) => char >= 0x2D00 && char <= 0x2D2F,
// 'Tifinagh': (char) => char >= 0x2D30 && char <= 0x2D7F,
// 'Ethiopic Extended': (char) => char >= 0x2D80 && char <= 0x2DDF,
// 'Cyrillic Extended-A': (char) => char >= 0x2DE0 && char <= 0x2DFF,
// 'Supplemental Punctuation': (char) => char >= 0x2E00 && char <= 0x2E7F,
"CJK Radicals Supplement": (char) => char >= 11904 && char <= 12031,
"Kangxi Radicals": (char) => char >= 12032 && char <= 12255,
"Ideographic Description Characters": (char) => char >= 12272 && char <= 12287,
"CJK Symbols and Punctuation": (char) => char >= 12288 && char <= 12351,
"Hiragana": (char) => char >= 12352 && char <= 12447,
"Katakana": (char) => char >= 12448 && char <= 12543,
"Bopomofo": (char) => char >= 12544 && char <= 12591,
"Hangul Compatibility Jamo": (char) => char >= 12592 && char <= 12687,
"Kanbun": (char) => char >= 12688 && char <= 12703,
"Bopomofo Extended": (char) => char >= 12704 && char <= 12735,
"CJK Strokes": (char) => char >= 12736 && char <= 12783,
"Katakana Phonetic Extensions": (char) => char >= 12784 && char <= 12799,
"Enclosed CJK Letters and Months": (char) => char >= 12800 && char <= 13055,
"CJK Compatibility": (char) => char >= 13056 && char <= 13311,
"CJK Unified Ideographs Extension A": (char) => char >= 13312 && char <= 19903,
"Yijing Hexagram Symbols": (char) => char >= 19904 && char <= 19967,
"CJK Unified Ideographs": (char) => char >= 19968 && char <= 40959,
"Yi Syllables": (char) => char >= 40960 && char <= 42127,
"Yi Radicals": (char) => char >= 42128 && char <= 42191,
// 'Lisu': (char) => char >= 0xA4D0 && char <= 0xA4FF,
// 'Vai': (char) => char >= 0xA500 && char <= 0xA63F,
// 'Cyrillic Extended-B': (char) => char >= 0xA640 && char <= 0xA69F,
// 'Bamum': (char) => char >= 0xA6A0 && char <= 0xA6FF,
// 'Modifier Tone Letters': (char) => char >= 0xA700 && char <= 0xA71F,
// 'Latin Extended-D': (char) => char >= 0xA720 && char <= 0xA7FF,
// 'Syloti Nagri': (char) => char >= 0xA800 && char <= 0xA82F,
// 'Common Indic Number Forms': (char) => char >= 0xA830 && char <= 0xA83F,
// 'Phags-pa': (char) => char >= 0xA840 && char <= 0xA87F,
// 'Saurashtra': (char) => char >= 0xA880 && char <= 0xA8DF,
// 'Devanagari Extended': (char) => char >= 0xA8E0 && char <= 0xA8FF,
// 'Kayah Li': (char) => char >= 0xA900 && char <= 0xA92F,
// 'Rejang': (char) => char >= 0xA930 && char <= 0xA95F,
"Hangul Jamo Extended-A": (char) => char >= 43360 && char <= 43391,
// 'Javanese': (char) => char >= 0xA980 && char <= 0xA9DF,
// 'Myanmar Extended-B': (char) => char >= 0xA9E0 && char <= 0xA9FF,
// 'Cham': (char) => char >= 0xAA00 && char <= 0xAA5F,
// 'Myanmar Extended-A': (char) => char >= 0xAA60 && char <= 0xAA7F,
// 'Tai Viet': (char) => char >= 0xAA80 && char <= 0xAADF,
// 'Meetei Mayek Extensions': (char) => char >= 0xAAE0 && char <= 0xAAFF,
// 'Ethiopic Extended-A': (char) => char >= 0xAB00 && char <= 0xAB2F,
// 'Latin Extended-E': (char) => char >= 0xAB30 && char <= 0xAB6F,
// 'Cherokee Supplement': (char) => char >= 0xAB70 && char <= 0xABBF,
// 'Meetei Mayek': (char) => char >= 0xABC0 && char <= 0xABFF,
"Hangul Syllables": (char) => char >= 44032 && char <= 55215,
"Hangul Jamo Extended-B": (char) => char >= 55216 && char <= 55295,
// 'High Surrogates': (char) => char >= 0xD800 && char <= 0xDB7F,
// 'High Private Use Surrogates': (char) => char >= 0xDB80 && char <= 0xDBFF,
// 'Low Surrogates': (char) => char >= 0xDC00 && char <= 0xDFFF,
"Private Use Area": (char) => char >= 57344 && char <= 63743,
"CJK Compatibility Ideographs": (char) => char >= 63744 && char <= 64255,
// 'Alphabetic Presentation Forms': (char) => char >= 0xFB00 && char <= 0xFB4F,
"Arabic Presentation Forms-A": (char) => char >= 64336 && char <= 65023,
// 'Variation Selectors': (char) => char >= 0xFE00 && char <= 0xFE0F,
"Vertical Forms": (char) => char >= 65040 && char <= 65055,
// 'Combining Half Marks': (char) => char >= 0xFE20 && char <= 0xFE2F,
"CJK Compatibility Forms": (char) => char >= 65072 && char <= 65103,
"Small Form Variants": (char) => char >= 65104 && char <= 65135,
"Arabic Presentation Forms-B": (char) => char >= 65136 && char <= 65279,
"Halfwidth and Fullwidth Forms": (char) => char >= 65280 && char <= 65519,
// 'Specials': (char) => char >= 0xFFF0 && char <= 0xFFFF,
// 'Linear B Syllabary': (char) => char >= 0x10000 && char <= 0x1007F,
// 'Linear B Ideograms': (char) => char >= 0x10080 && char <= 0x100FF,
// 'Aegean Numbers': (char) => char >= 0x10100 && char <= 0x1013F,
// 'Ancient Greek Numbers': (char) => char >= 0x10140 && char <= 0x1018F,
// 'Ancient Symbols': (char) => char >= 0x10190 && char <= 0x101CF,
// 'Phaistos Disc': (char) => char >= 0x101D0 && char <= 0x101FF,
// 'Lycian': (char) => char >= 0x10280 && char <= 0x1029F,
// 'Carian': (char) => char >= 0x102A0 && char <= 0x102DF,
// 'Coptic Epact Numbers': (char) => char >= 0x102E0 && char <= 0x102FF,
// 'Old Italic': (char) => char >= 0x10300 && char <= 0x1032F,
// 'Gothic': (char) => char >= 0x10330 && char <= 0x1034F,
// 'Old Permic': (char) => char >= 0x10350 && char <= 0x1037F,
// 'Ugaritic': (char) => char >= 0x10380 && char <= 0x1039F,
// 'Old Persian': (char) => char >= 0x103A0 && char <= 0x103DF,
// 'Deseret': (char) => char >= 0x10400 && char <= 0x1044F,
// 'Shavian': (char) => char >= 0x10450 && char <= 0x1047F,
// 'Osmanya': (char) => char >= 0x10480 && char <= 0x104AF,
"Osage": (char) => char >= 66736 && char <= 66815,
// 'Elbasan': (char) => char >= 0x10500 && char <= 0x1052F,
// 'Caucasian Albanian': (char) => char >= 0x10530 && char <= 0x1056F,
// 'Linear A': (char) => char >= 0x10600 && char <= 0x1077F,
// 'Cypriot Syllabary': (char) => char >= 0x10800 && char <= 0x1083F,
// 'Imperial Aramaic': (char) => char >= 0x10840 && char <= 0x1085F,
// 'Palmyrene': (char) => char >= 0x10860 && char <= 0x1087F,
// 'Nabataean': (char) => char >= 0x10880 && char <= 0x108AF,
// 'Hatran': (char) => char >= 0x108E0 && char <= 0x108FF,
// 'Phoenician': (char) => char >= 0x10900 && char <= 0x1091F,
// 'Lydian': (char) => char >= 0x10920 && char <= 0x1093F,
// 'Meroitic Hieroglyphs': (char) => char >= 0x10980 && char <= 0x1099F,
// 'Meroitic Cursive': (char) => char >= 0x109A0 && char <= 0x109FF,
// 'Kharoshthi': (char) => char >= 0x10A00 && char <= 0x10A5F,
// 'Old South Arabian': (char) => char >= 0x10A60 && char <= 0x10A7F,
// 'Old North Arabian': (char) => char >= 0x10A80 && char <= 0x10A9F,
// 'Manichaean': (char) => char >= 0x10AC0 && char <= 0x10AFF,
// 'Avestan': (char) => char >= 0x10B00 && char <= 0x10B3F,
// 'Inscriptional Parthian': (char) => char >= 0x10B40 && char <= 0x10B5F,
// 'Inscriptional Pahlavi': (char) => char >= 0x10B60 && char <= 0x10B7F,
// 'Psalter Pahlavi': (char) => char >= 0x10B80 && char <= 0x10BAF,
// 'Old Turkic': (char) => char >= 0x10C00 && char <= 0x10C4F,
// 'Old Hungarian': (char) => char >= 0x10C80 && char <= 0x10CFF,
// 'Hanifi Rohingya': (char) => char >= 0x10D00 && char <= 0x10D3F,
// 'Rumi Numeral Symbols': (char) => char >= 0x10E60 && char <= 0x10E7F,
// 'Old Sogdian': (char) => char >= 0x10F00 && char <= 0x10F2F,
// 'Sogdian': (char) => char >= 0x10F30 && char <= 0x10F6F,
// 'Elymaic': (char) => char >= 0x10FE0 && char <= 0x10FFF,
// 'Brahmi': (char) => char >= 0x11000 && char <= 0x1107F,
// 'Kaithi': (char) => char >= 0x11080 && char <= 0x110CF,
// 'Sora Sompeng': (char) => char >= 0x110D0 && char <= 0x110FF,
// 'Chakma': (char) => char >= 0x11100 && char <= 0x1114F,
// 'Mahajani': (char) => char >= 0x11150 && char <= 0x1117F,
// 'Sharada': (char) => char >= 0x11180 && char <= 0x111DF,
// 'Sinhala Archaic Numbers': (char) => char >= 0x111E0 && char <= 0x111FF,
// 'Khojki': (char) => char >= 0x11200 && char <= 0x1124F,
// 'Multani': (char) => char >= 0x11280 && char <= 0x112AF,
// 'Khudawadi': (char) => char >= 0x112B0 && char <= 0x112FF,
// 'Grantha': (char) => char >= 0x11300 && char <= 0x1137F,
// 'Newa': (char) => char >= 0x11400 && char <= 0x1147F,
// 'Tirhuta': (char) => char >= 0x11480 && char <= 0x114DF,
// 'Siddham': (char) => char >= 0x11580 && char <= 0x115FF,
// 'Modi': (char) => char >= 0x11600 && char <= 0x1165F,
// 'Mongolian Supplement': (char) => char >= 0x11660 && char <= 0x1167F,
// 'Takri': (char) => char >= 0x11680 && char <= 0x116CF,
// 'Ahom': (char) => char >= 0x11700 && char <= 0x1173F,
// 'Dogra': (char) => char >= 0x11800 && char <= 0x1184F,
// 'Warang Citi': (char) => char >= 0x118A0 && char <= 0x118FF,
// 'Nandinagari': (char) => char >= 0x119A0 && char <= 0x119FF,
// 'Zanabazar Square': (char) => char >= 0x11A00 && char <= 0x11A4F,
// 'Soyombo': (char) => char >= 0x11A50 && char <= 0x11AAF,
// 'Pau Cin Hau': (char) => char >= 0x11AC0 && char <= 0x11AFF,
// 'Bhaiksuki': (char) => char >= 0x11C00 && char <= 0x11C6F,
// 'Marchen': (char) => char >= 0x11C70 && char <= 0x11CBF,
// 'Masaram Gondi': (char) => char >= 0x11D00 && char <= 0x11D5F,
// 'Gunjala Gondi': (char) => char >= 0x11D60 && char <= 0x11DAF,
// 'Makasar': (char) => char >= 0x11EE0 && char <= 0x11EFF,
// 'Tamil Supplement': (char) => char >= 0x11FC0 && char <= 0x11FFF,
// 'Cuneiform': (char) => char >= 0x12000 && char <= 0x123FF,
// 'Cuneiform Numbers and Punctuation': (char) => char >= 0x12400 && char <= 0x1247F,
// 'Early Dynastic Cuneiform': (char) => char >= 0x12480 && char <= 0x1254F,
// 'Egyptian Hieroglyphs': (char) => char >= 0x13000 && char <= 0x1342F,
// 'Egyptian Hieroglyph Format Controls': (char) => char >= 0x13430 && char <= 0x1343F,
// 'Anatolian Hieroglyphs': (char) => char >= 0x14400 && char <= 0x1467F,
// 'Bamum Supplement': (char) => char >= 0x16800 && char <= 0x16A3F,
// 'Mro': (char) => char >= 0x16A40 && char <= 0x16A6F,
// 'Bassa Vah': (char) => char >= 0x16AD0 && char <= 0x16AFF,
// 'Pahawh Hmong': (char) => char >= 0x16B00 && char <= 0x16B8F,
// 'Medefaidrin': (char) => char >= 0x16E40 && char <= 0x16E9F,
// 'Miao': (char) => char >= 0x16F00 && char <= 0x16F9F,
// 'Ideographic Symbols and Punctuation': (char) => char >= 0x16FE0 && char <= 0x16FFF,
// 'Tangut': (char) => char >= 0x17000 && char <= 0x187FF,
// 'Tangut Components': (char) => char >= 0x18800 && char <= 0x18AFF,
// 'Kana Supplement': (char) => char >= 0x1B000 && char <= 0x1B0FF,
// 'Kana Extended-A': (char) => char >= 0x1B100 && char <= 0x1B12F,
// 'Small Kana Extension': (char) => char >= 0x1B130 && char <= 0x1B16F,
// 'Nushu': (char) => char >= 0x1B170 && char <= 0x1B2FF,
// 'Duployan': (char) => char >= 0x1BC00 && char <= 0x1BC9F,
// 'Shorthand Format Controls': (char) => char >= 0x1BCA0 && char <= 0x1BCAF,
// 'Byzantine Musical Symbols': (char) => char >= 0x1D000 && char <= 0x1D0FF,
// 'Musical Symbols': (char) => char >= 0x1D100 && char <= 0x1D1FF,
// 'Ancient Greek Musical Notation': (char) => char >= 0x1D200 && char <= 0x1D24F,
// 'Mayan Numerals': (char) => char >= 0x1D2E0 && char <= 0x1D2FF,
// 'Tai Xuan Jing Symbols': (char) => char >= 0x1D300 && char <= 0x1D35F,
// 'Counting Rod Numerals': (char) => char >= 0x1D360 && char <= 0x1D37F,
// 'Mathematical Alphanumeric Symbols': (char) => char >= 0x1D400 && char <= 0x1D7FF,
// 'Sutton SignWriting': (char) => char >= 0x1D800 && char <= 0x1DAAF,
// 'Glagolitic Supplement': (char) => char >= 0x1E000 && char <= 0x1E02F,
// 'Nyiakeng Puachue Hmong': (char) => char >= 0x1E100 && char <= 0x1E14F,
// 'Wancho': (char) => char >= 0x1E2C0 && char <= 0x1E2FF,
// 'Mende Kikakui': (char) => char >= 0x1E800 && char <= 0x1E8DF,
// 'Adlam': (char) => char >= 0x1E900 && char <= 0x1E95F,
// 'Indic Siyaq Numbers': (char) => char >= 0x1EC70 && char <= 0x1ECBF,
// 'Ottoman Siyaq Numbers': (char) => char >= 0x1ED00 && char <= 0x1ED4F,
// 'Arabic Mathematical Alphabetic Symbols': (char) => char >= 0x1EE00 && char <= 0x1EEFF,
// 'Mahjong Tiles': (char) => char >= 0x1F000 && char <= 0x1F02F,
// 'Domino Tiles': (char) => char >= 0x1F030 && char <= 0x1F09F,
// 'Playing Cards': (char) => char >= 0x1F0A0 && char <= 0x1F0FF,
// 'Enclosed Alphanumeric Supplement': (char) => char >= 0x1F100 && char <= 0x1F1FF,
// 'Enclosed Ideographic Supplement': (char) => char >= 0x1F200 && char <= 0x1F2FF,
// 'Miscellaneous Symbols and Pictographs': (char) => char >= 0x1F300 && char <= 0x1F5FF,
// 'Emoticons': (char) => char >= 0x1F600 && char <= 0x1F64F,
// 'Ornamental Dingbats': (char) => char >= 0x1F650 && char <= 0x1F67F,
// 'Transport and Map Symbols': (char) => char >= 0x1F680 && char <= 0x1F6FF,
// 'Alchemical Symbols': (char) => char >= 0x1F700 && char <= 0x1F77F,
// 'Geometric Shapes Extended': (char) => char >= 0x1F780 && char <= 0x1F7FF,
// 'Supplemental Arrows-C': (char) => char >= 0x1F800 && char <= 0x1F8FF,
// 'Supplemental Symbols and Pictographs': (char) => char >= 0x1F900 && char <= 0x1F9FF,
// 'Chess Symbols': (char) => char >= 0x1FA00 && char <= 0x1FA6F,
// 'Symbols and Pictographs Extended-A': (char) => char >= 0x1FA70 && char <= 0x1FAFF,
"CJK Unified Ideographs Extension B": (char) => char >= 131072 && char <= 173791
// 'CJK Unified Ideographs Extension C': (char) => char >= 0x2A700 && char <= 0x2B73F,
// 'CJK Unified Ideographs Extension D': (char) => char >= 0x2B740 && char <= 0x2B81F,
// 'CJK Unified Ideographs Extension E': (char) => char >= 0x2B820 && char <= 0x2CEAF,
// 'CJK Unified Ideographs Extension F': (char) => char >= 0x2CEB0 && char <= 0x2EBEF,
// 'CJK Compatibility Ideographs Supplement': (char) => char >= 0x2F800 && char <= 0x2FA1F,
// 'Tags': (char) => char >= 0xE0000 && char <= 0xE007F,
// 'Variation Selectors Supplement': (char) => char >= 0xE0100 && char <= 0xE01EF,
// 'Supplementary Private Use Area-A': (char) => char >= 0xF0000 && char <= 0xFFFFF,
// 'Supplementary Private Use Area-B': (char) => char >= 0x100000 && char <= 0x10FFFF,
};
function allowsIdeographicBreaking(chars) {
for (const char of chars) {
if (!charAllowsIdeographicBreaking(char.charCodeAt(0))) return false;
}
return true;
}
function allowsVerticalWritingMode(chars) {
for (const char of chars) {
if (charHasUprightVerticalOrientation(char.charCodeAt(0))) return true;
}
return false;
}
function allowsLetterSpacing(chars) {
for (const char of chars) {
if (!charAllowsLetterSpacing(char.charCodeAt(0))) return false;
}
return true;
}
function charAllowsLetterSpacing(char) {
if (unicodeBlockLookup["Arabic"](char)) return false;
if (unicodeBlockLookup["Arabic Supplement"](char)) return false;
if (unicodeBlockLookup["Arabic Extended-A"](char)) return false;
if (unicodeBlockLookup["Arabic Presentation Forms-A"](char)) return false;
if (unicodeBlockLookup["Arabic Presentation Forms-B"](char)) return false;
return true;
}
function charAllowsIdeographicBreaking(char) {
if (char < 11904) return false;
if (unicodeBlockLookup["Bopomofo Extended"](char)) return true;
if (unicodeBlockLookup["Bopomofo"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility Forms"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility Ideographs"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility"](char)) return true;
if (unicodeBlockLookup["CJK Radicals Supplement"](char)) return true;
if (unicodeBlockLookup["CJK Strokes"](char)) return true;
if (unicodeBlockLookup["CJK Symbols and Punctuation"](char)) return true;
if (unicodeBlockLookup["CJK Unified Ideographs Extension A"](char)) return true;
if (unicodeBlockLookup["CJK Unified Ideographs"](char)) return true;
if (unicodeBlockLookup["Enclosed CJK Letters and Months"](char)) return true;
if (unicodeBlockLookup["Halfwidth and Fullwidth Forms"](char)) return true;
if (unicodeBlockLookup["Hiragana"](char)) return true;
if (unicodeBlockLookup["Ideographic Description Characters"](char)) return true;
if (unicodeBlockLookup["Kangxi Radicals"](char)) return true;
if (unicodeBlockLookup["Katakana Phonetic Extensions"](char)) return true;
if (unicodeBlockLookup["Katakana"](char)) return true;
if (unicodeBlockLookup["Vertical Forms"](char)) return true;
if (unicodeBlockLookup["Yi Radicals"](char)) return true;
if (unicodeBlockLookup["Yi Syllables"](char)) return true;
return false;
}
function charHasUprightVerticalOrientation(char) {
if (char === 746 || char === 747) {
return true;
}
if (char < 4352) return false;
if (unicodeBlockLookup["Bopomofo Extended"](char)) return true;
if (unicodeBlockLookup["Bopomofo"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility Forms"](char)) {
if (!(char >= 65097 && char <= 65103)) {
return true;
}
}
if (unicodeBlockLookup["CJK Compatibility Ideographs"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility"](char)) return true;
if (unicodeBlockLookup["CJK Radicals Supplement"](char)) return true;
if (unicodeBlockLookup["CJK Strokes"](char)) return true;
if (unicodeBlockLookup["CJK Symbols and Punctuation"](char)) {
if (!(char >= 12296 && char <= 12305) && !(char >= 12308 && char <= 12319) && char !== 12336) {
return true;
}
}
if (unicodeBlockLookup["CJK Unified Ideographs Extension A"](char)) return true;
if (unicodeBlockLookup["CJK Unified Ideographs"](char)) return true;
if (unicodeBlockLookup["Enclosed CJK Letters and Months"](char)) return true;
if (unicodeBlockLookup["Hangul Compatibility Jamo"](char)) return true;
if (unicodeBlockLookup["Hangul Jamo Extended-A"](char)) return true;
if (unicodeBlockLookup["Hangul Jamo Extended-B"](char)) return true;
if (unicodeBlockLookup["Hangul Jamo"](char)) return true;
if (unicodeBlockLookup["Hangul Syllables"](char)) return true;
if (unicodeBlockLookup["Hiragana"](char)) return true;
if (unicodeBlockLookup["Ideographic Description Characters"](char)) return true;
if (unicodeBlockLookup["Kanbun"](char)) return true;
if (unicodeBlockLookup["Kangxi Radicals"](char)) return true;
if (unicodeBlockLookup["Katakana Phonetic Extensions"](char)) return true;
if (unicodeBlockLookup["Katakana"](char)) {
if (char !== 12540) {
return true;
}
}
if (unicodeBlockLookup["Halfwidth and Fullwidth Forms"](char)) {
if (char !== 65288 && char !== 65289 && char !== 65293 && !(char >= 65306 && char <= 65310) && char !== 65339 && char !== 65341 && char !== 65343 && !(char >= 65371 && char <= 65503) && char !== 65507 && !(char >= 65512 && char <= 65519)) {
return true;
}
}
if (unicodeBlockLookup["Small Form Variants"](char)) {
if (!(char >= 65112 && char <= 65118) && !(char >= 65123 && char <= 65126)) {
return true;
}
}
if (unicodeBlockLookup["Unified Canadian Aboriginal Syllabics"](char)) return true;
if (unicodeBlockLookup["Unified Canadian Aboriginal Syllabics Extended"](char)) return true;
if (unicodeBlockLookup["Vertical Forms"](char)) return true;
if (unicodeBlockLookup["Yijing Hexagram Symbols"](char)) return true;
if (unicodeBlockLookup["Yi Syllables"](char)) return true;
if (unicodeBlockLookup["Yi Radicals"](char)) return true;
return false;
}
function charHasNeutralVerticalOrientation(char) {
if (unicodeBlockLookup["Latin-1 Supplement"](char)) {
if (char === 167 || char === 169 || char === 174 || char === 177 || char === 188 || char === 189 || char === 190 || char === 215 || char === 247) {
return true;
}
}
if (unicodeBlockLookup["General Punctuation"](char)) {
if (char === 8214 || char === 8224 || char === 8225 || char === 8240 || char === 8241 || char === 8251 || char === 8252 || char === 8258 || char === 8263 || char === 8264 || char === 8265 || char === 8273) {
return true;
}
}
if (unicodeBlockLookup["Letterlike Symbols"](char)) return true;
if (unicodeBlockLookup["Number Forms"](char)) return true;
if (unicodeBlockLookup["Miscellaneous Technical"](char)) {
if (char >= 8960 && char <= 8967 || char >= 8972 && char <= 8991 || char >= 8996 && char <= 9e3 || char === 9003 || char >= 9085 && char <= 9114 || char >= 9150 && char <= 9165 || char === 9167 || char >= 9169 && char <= 9179 || char >= 9186 && char <= 9215) {
return true;
}
}
if (unicodeBlockLookup["Control Pictures"](char) && char !== 9251) return true;
if (unicodeBlockLookup["Optical Character Recognition"](char)) return true;
if (unicodeBlockLookup["Enclosed Alphanumerics"](char)) return true;
if (unicodeBlockLookup["Geometric Shapes"](char)) return true;
if (unicodeBlockLookup["Miscellaneous Symbols"](char)) {
if (!(char >= 9754 && char <= 9759)) {
return true;
}
}
if (unicodeBlockLookup["Miscellaneous Symbols and Arrows"](char)) {
if (char >= 11026 && char <= 11055 || char >= 11088 && char <= 11097 || char >= 11192 && char <= 11243) {
return true;
}
}
if (unicodeBlockLookup["CJK Symbols and Punctuation"](char)) return true;
if (unicodeBlockLookup["Katakana"](char)) return true;
if (unicodeBlockLookup["Private Use Area"](char)) return true;
if (unicodeBlockLookup["CJK Compatibility Forms"](char)) return true;
if (unicodeBlockLookup["Small Form Variants"](char)) return true;
if (unicodeBlockLookup["Halfwidth and Fullwidth Forms"](char)) return true;
if (char === 8734 || char === 8756 || char === 8757 || char >= 9984 && char <= 10087 || char >= 10102 && char <= 10131 || char === 65532 || char === 65533) {
return true;
}
return false;
}
function needsRotationInVerticalMode(char) {
if (char === 12312 || char === 12313 || char === 12316) {
return true;
}
if (char === 12540 || char === 12448) {
return true;
}
return false;
}
function charHasRotatedVerticalOrientation(char) {
return !(charHasUprightVerticalOrientation(char) || charHasNeutralVerticalOrientation(char));
}
function charInComplexShapingScript(char) {
return unicodeBlockLookup["Arabic"](char) || unicodeBlockLookup["Arabic Supplement"](char) || unicodeBlockLookup["Arabic Extended-A"](char) || unicodeBlockLookup["Arabic Presentation Forms-A"](char) || unicodeBlockLookup["Arabic Presentation Forms-B"](char);
}
function charInRTLScript(char) {
return char >= 1424 && char <= 2303 || unicodeBlockLookup["Arabic Presentation Forms-A"](char) || unicodeBlockLookup["Arabic Presentation Forms-B"](char);
}
function charInSupportedScript(char, canRenderRTL) {
if (!canRenderRTL && charInRTLScript(char)) {
return false;
}
if (char >= 2304 && char <= 3583 || // Main blocks for Indic scripts and Sinhala
char >= 3840 && char <= 4255 || // Main blocks for Tibetan and Myanmar
unicodeBlockLookup["Khmer"](char)) {
return false;
}
return true;
}
function stringContainsRTLText(chars) {
for (const char of chars) {
if (charInRTLScript(char.charCodeAt(0))) {
return true;
}
}
return false;
}
function isStringInSupportedScript(chars, canRenderRTL) {
for (const char of chars) {
if (!charInSupportedScript(char.charCodeAt(0), canRenderRTL)) {
return false;
}
}
return true;
}
const rtlPluginStatus = {
unavailable: "unavailable",
// Not loaded
deferred: "deferred",
// The plugin URL has been specified, but loading has been deferred
loading: "loading",
// request in-flight
parsing: "parsing",
parsed: "parsed",
loaded: "loaded",
error: "error"
};
let _completionCallback = null;
let pluginStatus = rtlPluginStatus.unavailable;
let pluginURL = null;
const triggerPluginCompletionEvent = function(error) {
if (error && typeof error === "string" && error.indexOf("NetworkError") > -1) {
pluginStatus = rtlPluginStatus.error;
}
if (_completionCallback) {
_completionCallback(error);
}
};
function sendPluginStateToWorker() {
evented.fire(new Event("pluginStateChange", { pluginStatus, pluginURL }));
}
const evented = new Evented();
const getRTLTextPluginStatus = function() {
return pluginStatus;
};
const registerForPluginStateChange = function(callback) {
callback({ pluginStatus, pluginURL });
evented.on("pluginStateChange", callback);
return callback;
};
const clearRTLTextPlugin = function() {
pluginStatus = rtlPluginStatus.unavailable;
pluginURL = null;
};
const setRTLTextPlugin = function(url, callback, deferred = false) {
if (pluginStatus === rtlPluginStatus.deferred || pluginStatus === rtlPluginStatus.loading || pluginStatus === rtlPluginStatus.loaded) {
throw new Error("setRTLTextPlugin cannot be called multiple times.");
}
pluginURL = exported$1.resolveURL(url);
pluginStatus = rtlPluginStatus.deferred;
_completionCallback = callback;
sendPluginStateToWorker();
if (!deferred) {
downloadRTLTextPlugin();
}
};
const downloadRTLTextPlugin = function() {
if (pluginStatus !== rtlPluginStatus.deferred || !pluginURL) {
throw new Error("rtl-text-plugin cannot be downloaded unless a pluginURL is specified");
}
pluginStatus = rtlPluginStatus.loading;
sendPluginStateToWorker();
if (pluginURL) {
getArrayBuffer({ url: pluginURL }, (error) => {
if (error) {
triggerPluginCompletionEvent(error);
} else {
pluginStatus = rtlPluginStatus.loaded;
sendPluginStateToWorker();
}
});
}
};
const plugin = {
applyArabicShaping: null,
processBidirectionalText: null,
processStyledBidirectionalText: null,
isLoaded() {
return pluginStatus === rtlPluginStatus.loaded || // Main Thread: loaded if the completion callback returned successfully
plugin.applyArabicShaping != null;
},
isLoading() {
return pluginStatus === rtlPluginStatus.loading;
},
setState(state) {
assert$1(isWorker(self), "Cannot set the state of the rtl-text-plugin when not in the web-worker context");
pluginStatus = state.pluginStatus;
pluginURL = state.pluginURL;
},
isParsing() {
assert$1(isWorker(self), "rtl-text-plugin is only parsed on the worker-threads");
return pluginStatus === rtlPluginStatus.parsing;
},
isParsed() {
assert$1(isWorker(self), "rtl-text-plugin is only parsed on the worker-threads");
return pluginStatus === rtlPluginStatus.parsed;
},
getPluginURL() {
assert$1(isWorker(self), "rtl-text-plugin url can only be queried from the worker threads");
return pluginURL;
}
};
const lazyLoadRTLTextPlugin = function() {
if (!plugin.isLoading() && !plugin.isLoaded() && getRTLTextPluginStatus() === "deferred") {
downloadRTLTextPlugin();
}
};
class EvaluationParameters {
constructor(zoom, options) {
this.zoom = zoom;
if (options) {
this.now = options.now;
this.fadeDuration = options.fadeDuration;
this.transition = options.transition;
this.pitch = options.pitch;
this.brightness = options.brightness;
this.worldview = options.worldview;
this.activeFloors = options.activeFloors;
} else {
this.now = 0;
this.fadeDuration = 0;
this.transition = {};
this.pitch = 0;
this.brightness = 0;
}
}
isSupportedScript(str) {
return isStringInSupportedScript(str, plugin.isLoaded());
}
}
class PropertyValue {
constructor(property, value, scope, options, iconImageUseTheme) {
this.property = property;
this.value = value;
this.expression = normalizePropertyExpression(value === void 0 ? property.specification.default : value, property.specification, scope, options, iconImageUseTheme);
}
isIndoorDependent() {
return this.expression.isIndoorDependent;
}
isDataDriven() {
return this.expression.kind === "source" || this.expression.kind === "composite";
}
possiblyEvaluate(parameters, canonical, availableImages, iconImageUseTheme) {
return this.property.possiblyEvaluate(this, parameters, canonical, availableImages, iconImageUseTheme);
}
}
class TransitionablePropertyValue {
constructor(property, scope, options, iconImageUseTheme) {
this.property = property;
this.value = new PropertyValue(property, void 0, scope, options, iconImageUseTheme);
}
transitioned(parameters, prior) {
return new TransitioningPropertyValue(
this.property,
this.value,
prior,
Object.assign({}, parameters.transition, this.transition),
parameters.now
);
}
untransitioned() {
return new TransitioningPropertyValue(this.property, this.value, null, {}, 0);
}
}
class Transitionable {
constructor(properties, scope, options, iconImageUseTheme) {
this._properties = properties;
this._values = Object.create(properties.defaultTransitionablePropertyValues);
this._scope = scope;
this._options = options;
this._iconImageUseTheme = iconImageUseTheme;
this._isIndoorDependent = false;
this.configDependencies = /* @__PURE__ */ new Set();
}
getValue(name) {
return clone(this._values[name].value.value);
}
setValue(name, value) {
if (!this._values.hasOwnProperty(name)) {
this._values[name] = new TransitionablePropertyValue(this._values[name].property, this._scope, this._options, this._iconImageUseTheme);
}
this._values[name].value = new PropertyValue(this._values[name].property, value === null ? void 0 : clone(value), this._scope, this._options, this._iconImageUseTheme);
if (this._values[name].value.expression.configDependencies) {
this.configDependencies = /* @__PURE__ */ new Set([...this.configDependencies, ...this._values[name].value.expression.configDependencies]);
this._isIndoorDependent = this._isIndoorDependent || this._values[name].value.isIndoorDependent();
}
}
setTransitionOrValue(properties, options) {
if (options) this._options = options;
const specProperties = this._properties.properties;
if (properties) {
for (const name in properties) {
const value = properties[name];
if (name.endsWith("-transition")) {
const propName = name.slice(0, -"-transition".length);
if (specProperties[propName]) {
this.setTransition(propName, value);
}
} else if (specProperties.hasOwnProperty(name)) {
this.setValue(name, value);
}
}
}
}
getTransition(name) {
return clone(this._values[name].transition);
}
setTransition(name, value) {
if (!this._values.hasOwnProperty(name)) {
this._values[name] = new TransitionablePropertyValue(this._values[name].property);
}
this._values[name].transition = clone(value) || void 0;
}
serialize() {
const result = {};
for (const property of Object.keys(this._values)) {
const value = this.getValue(property);
if (value !== void 0) {
result[property] = value;
}
const transition = this.getTransition(property);
if (transition !== void 0) {
result[`${property}-transition`] = transition;
}
}
return result;
}
transitioned(parameters, prior) {
const result = new Transitioning(this._properties);
for (const property of Object.keys(this._values)) {
result._values[property] = this._values[property].transitioned(parameters, prior._values[property]);
}
return result;
}
untransitioned() {
const result = new Transitioning(this._properties);
for (const property of Object.keys(this._values)) {
result._values[property] = this._values[property].untransitioned();
}
return result;
}
isIndoorDependent() {
return this._isIndoorDependent;
}
}
class TransitioningPropertyValue {
constructor(property, value, prior, transition, now) {
const delay = transition.delay || 0;
const duration = transition.duration || 0;
now = now || 0;
this.property = property;
this.value = value;
this.begin = now + delay;
this.end = this.begin + duration;
if (property.specification.transition && (transition.delay || transition.duration)) {
this.prior = prior;
}
}
possiblyEvaluate(parameters, canonical, availableImages) {
const now = parameters.now || 0;
const finalValue = this.value.possiblyEvaluate(parameters, canonical, availableImages);
const prior = this.prior;
if (!prior) {
return finalValue;
} else if (now > this.end) {
this.prior = null;
return finalValue;
} else if (this.value.isDataDriven()) {
this.prior = null;
return finalValue;
} else if (now < this.begin) {
return prior.possiblyEvaluate(parameters, canonical, availableImages);
} else {
const t = (now - this.begin) / (this.end - this.begin);
return this.property.interpolate(prior.possiblyEvaluate(parameters, canonical, availableImages), finalValue, easeCubicInOut(t));
}
}
}
class Transitioning {
constructor(properties) {
this._properties = properties;
this._values = Object.create(properties.defaultTransitioningPropertyValues);
}
possiblyEvaluate(parameters, canonical, availableImages) {
const result = new PossiblyEvaluated(this._properties);
for (const property of Object.keys(this._values)) {
result._values[property] = this._values[property].possiblyEvaluate(parameters, canonical, availableImages);
}
return result;
}
hasTransition() {
for (const property of Object.keys(this._values)) {
if (this._values[property].prior) {
return true;
}
}
return false;
}
}
class Layout {
constructor(properties, scope, options, iconImageUseTheme) {
this._properties = properties;
this._values = Object.create(properties.defaultPropertyValues);
this._scope = scope;
this._options = options;
this._iconImageUseTheme = iconImageUseTheme;
this._isIndoorDependent = false;
this.configDependencies = /* @__PURE__ */ new Set();
}
getValue(name) {
return clone(this._values[name].value);
}
setValue(name, value) {
this._values[name] = new PropertyValue(this._values[name].property, value === null ? void 0 : clone(value), this._scope, this._options, this._iconImageUseTheme);
if (this._values[name].expression.configDependencies) {
this.configDependencies = /* @__PURE__ */ new Set([...this.configDependencies, ...this._values[name].expression.configDependencies]);
this._isIndoorDependent = this._isIndoorDependent || this._values[name].isIndoorDependent();
}
}
serialize() {
const result = {};
for (const property of Object.keys(this._values)) {
const value = this.getValue(property);
if (value !== void 0) {
result[property] = value;
}
}
return result;
}
possiblyEvaluate(parameters, canonical, availableImages, iconImageUseTheme) {
const result = new PossiblyEvaluated(this._properties);
for (const property of Object.keys(this._values)) {
result._values[property] = this._values[property].possiblyEvaluate(parameters, canonical, availableImages, iconImageUseTheme);
}
return result;
}
isIndoorDependent() {
return this._isIndoorDependent;
}
}
class PossiblyEvaluatedPropertyValue {
constructor(property, value, parameters, iconImageUseTheme) {
this.property = property;
this.value = value;
this.parameters = parameters;
this.iconImageUseTheme = iconImageUseTheme;
}
isConstant() {
return this.value.kind === "constant";
}
constantOr(value) {
if (this.value.kind === "constant") {
return this.value.value;
} else {
return value;
}
}
evaluate(feature, featureState, canonical, availableImages) {
return this.property.evaluate(this.value, this.parameters, feature, featureState, canonical, availableImages, this.iconImageUseTheme);
}
}
class PossiblyEvaluated {
constructor(properties) {
this._properties = properties;
this._values = Object.create(properties.defaultPossiblyEvaluatedValues);
}
get(name) {
return this._values[name];
}
}
class DataConstantProperty {
constructor(specification) {
this.specification = specification;
}
possiblyEvaluate(value, parameters) {
assert$1(!value.isDataDriven());
return value.expression.evaluate(parameters);
}
interpolate(a, b, t) {
const interp = interpolate$1[this.specification.type];
if (interp) {
return interp(a, b, t);
} else {
return a;
}
}
}
class DataDrivenProperty {
constructor(specification, overrides) {
this.specification = specification;
this.overrides = overrides;
}
possiblyEvaluate(value, parameters, canonical, availableImages, iconImageUseTheme) {
if (value.expression.kind === "constant" || value.expression.kind === "camera") {
return new PossiblyEvaluatedPropertyValue(this, { kind: "constant", value: value.expression.evaluate(parameters, null, {}, canonical, availableImages, void 0, iconImageUseTheme) }, parameters);
} else {
return new PossiblyEvaluatedPropertyValue(this, value.expression, parameters, iconImageUseTheme);
}
}
interpolate(a, b, t) {
if (a.value.kind !== "constant" || b.value.kind !== "constant") {
return a;
}
if (a.value.value === void 0 || b.value.value === void 0) {
return new PossiblyEvaluatedPropertyValue(this, { kind: "constant", value: void 0 }, a.parameters);
}
const interp = interpolate$1[this.specification.type];
if (interp) {
return new PossiblyEvaluatedPropertyValue(this, { kind: "constant", value: interp(a.value.value, b.value.value, t) }, a.parameters);
} else {
return a;
}
}
evaluate(value, parameters, feature, featureState, canonical, availableImages, iconImageUseTheme) {
if (value.kind === "constant") {
return value.value;
} else {
return value.evaluate(parameters, feature, featureState, canonical, availableImages, void 0, iconImageUseTheme);
}
}
}
class ColorRampProperty {
constructor(specification) {
this.specification = specification;
}
possiblyEvaluate(value, parameters, canonical, availableImages) {
return !!value.expression.evaluate(parameters, null, {}, canonical, availableImages);
}
interpolate() {
return false;
}
}
class DirectionProperty {
constructor(specification) {
this.specification = specification;
}
possiblyEvaluate(value, parameters) {
return sphericalDirectionToCartesian(value.expression.evaluate(parameters));
}
interpolate(a, b, t) {
return {
x: number(a.x, b.x, t),
y: number(a.y, b.y, t),
z: number(a.z, b.z, t)
};
}
}
class PositionProperty {
constructor(specification) {
this.specification = specification;
}
possiblyEvaluate(value, parameters) {
return sphericalPositionToCartesian(value.expression.evaluate(parameters));
}
interpolate(a, b, t) {
return {
x: number(a.x, b.x, t),
y: number(a.y, b.y, t),
z: number(a.z, b.z, t),
azimuthal: number(a.azimuthal, b.azimuthal, t),
polar: number(a.polar, b.polar, t)
};
}
}
class Properties {
constructor(properties) {
this.properties = properties;
this.defaultPropertyValues = {};
this.defaultTransitionablePropertyValues = {};
this.defaultTransitioningPropertyValues = {};
this.defaultPossiblyEvaluatedValues = {};
this.overridableProperties = [];
const defaultParameters = new EvaluationParameters(0, {});
for (const property in properties) {
const prop = properties[property];
if (prop.specification.overridable) {
this.overridableProperties.push(property);
}
const defaultPropertyValue = this.defaultPropertyValues[property] = new PropertyValue(prop, void 0);
const defaultTransitionablePropertyValue = this.defaultTransitionablePropertyValues[property] = new TransitionablePropertyValue(prop);
this.defaultTransitioningPropertyValues[property] = defaultTransitionablePropertyValue.untransitioned();
this.defaultPossiblyEvaluatedValues[property] = defaultPropertyValue.possiblyEvaluate(defaultParameters);
}
}
}
register(DataDrivenProperty, "DataDrivenProperty");
register(DataConstantProperty, "DataConstantProperty");
register(ColorRampProperty, "ColorRampProperty");
var spec = JSON.parse('{"$version":8,"$root":{"version":{"type":"enum","values":[8]},"fragment":{"type":"boolean"},"name":{"type":"string"},"metadata":{"type":"*"},"center":{"type":"array","value":"number"},"zoom":{"type":"number"},"bearing":{"type":"number","default":0,"period":360},"pitch":{"type":"number","default":0},"light":{"type":"light"},"lights":{"type":"array","value":"light-3d"},"terrain":{"type":"terrain","optional":true},"fog":{"type":"fog"},"snow":{"type":"snow"},"rain":{"type":"rain"},"camera":{"type":"camera"},"color-theme":{"type":"colorTheme"},"indoor":{"type":"indoor"},"imports":{"type":"array","value":"import"},"iconsets":{"type":"iconsets"},"schema":{"type":"schema"},"sources":{"type":"sources"},"sprite":{"type":"string"},"glyphs":{"type":"string","default":"mapbox://fonts/mapbox/{fontstack}/{range}.pbf"},"transition":{"type":"transition"},"projection":{"type":"projection"},"layers":{"type":"array","value":"layer"},"models":{"type":"models"},"featuresets":{"type":"featuresets"}},"featuresets":{"*":{"type":"featureset"}},"featureset":{"metadata":{"type":"*"},"selectors":{"type":"array","value":"selector"}},"selector":{"layer":{"type":"string"},"properties":{"type":"selectorProperty"},"featureNamespace":{"type":"string"},"_uniqueFeatureID":{"type":"boolean"}},"selectorProperty":{"*":{"type":"*"}},"model":{"type":"string"},"import":{"id":{"type":"string"},"url":{"type":"string"},"config":{"type":"config"},"data":{"type":"$root"},"color-theme":{"type":"colorTheme","optional":true}},"config":{"*":{"type":"*"}},"schema":{"*":{"type":"option"}},"option":{"default":{"type":"*","expression":{}},"type":{"type":"enum","values":{"string":1,"number":1,"boolean":1,"color":1}},"array":{"type":"boolean"},"minValue":{"type":"number"},"maxValue":{"type":"number"},"stepValue":{"type":"number"},"values":{"type":"array","value":"*"},"metadata":{"type":"*"}},"models":{"*":{"type":"model"}},"light-3d":{"id":{"type":"string"},"properties":{"type":"properties"},"type":{"type":"enum","values":{"ambient":{},"directional":{},"flat":{}}}},"properties":["properties_light_directional","properties_light_ambient","properties_light_flat"],"properties_light_directional":{"direction":{"type":"array","default":[210,30],"minimum":[0,0],"maximum":[360,90],"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom"]},"use-theme":true,"transition":true},"intensity":{"type":"number","default":0.5,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"cast-shadows":{"type":"boolean","default":false},"shadow-quality":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"parameters":["zoom"]}},"shadow-intensity":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"properties_light_ambient":{"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom"]},"use-theme":true,"transition":true},"intensity":{"type":"number","default":0.5,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"properties_light_flat":{"anchor":{"type":"enum","default":"viewport","values":{"map":1,"viewport":1},"expression":{"parameters":["zoom"]}},"position":{"type":"array","default":[1.15,210,30],"length":3,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom"]},"use-theme":true,"transition":true},"intensity":{"type":"number","default":0.5,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"iconsets":{"*":{"type":"iconset"}},"iconset":["iconset_sprite","iconset_source"],"iconset_sprite":{"type":{"type":"enum","values":{"sprite":1}},"url":{"type":"string"}},"iconset_source":{"type":{"type":"enum","values":{"source":1}},"source":{"type":"string"}},"sources":{"*":{"type":"source"}},"source":["source_vector","source_raster","source_raster_dem","source_raster_array","source_geojson","source_video","source_image","source_model"],"source_vector":{"type":{"type":"enum","values":{"vector":1}},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"bounds":{"type":"array","value":"number","length":4,"default":[-180,-85.051129,180,85.051129]},"extra_bounds":{"type":"array","value":{"type":"array","value":"number","length":4}},"scheme":{"type":"enum","values":{"xyz":1,"tms":1},"default":"xyz"},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"attribution":{"type":"string"},"promoteId":{"type":"promoteId"},"volatile":{"type":"boolean","default":false},"*":{"type":"*"}},"source_raster":{"type":{"type":"enum","values":{"raster":1}},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"bounds":{"type":"array","value":"number","length":4,"default":[-180,-85.051129,180,85.051129]},"extra_bounds":{"type":"array","value":{"type":"array","value":"number","length":4}},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"tileSize":{"type":"number","default":512},"scheme":{"type":"enum","values":{"xyz":1,"tms":1},"default":"xyz"},"attribution":{"type":"string"},"volatile":{"type":"boolean","default":false},"*":{"type":"*"}},"source_raster_dem":{"type":{"type":"enum","values":{"raster-dem":1}},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"bounds":{"type":"array","value":"number","length":4,"default":[-180,-85.051129,180,85.051129]},"extra_bounds":{"type":"array","value":{"type":"array","value":"number","length":4}},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"tileSize":{"type":"number","default":512},"attribution":{"type":"string"},"encoding":{"type":"enum","values":{"terrarium":1,"mapbox":1},"default":"mapbox"},"volatile":{"type":"boolean","default":false},"*":{"type":"*"}},"source_raster_array":{"type":{"type":"enum","values":{"raster-array":1}},"url":{"type":"string"},"tiles":{"type":"array","value":"string"},"bounds":{"type":"array","value":"number","length":4,"default":[-180,-85.051129,180,85.051129]},"extra_bounds":{"type":"array","value":{"type":"array","value":"number","length":4}},"minzoom":{"type":"number","default":0},"maxzoom":{"type":"number","default":22},"tileSize":{"type":"number","default":512},"attribution":{"type":"string"},"rasterLayers":{"type":"*"},"volatile":{"type":"boolean","default":false},"*":{"type":"*"}},"source_geojson":{"type":{"type":"enum","values":{"geojson":1}},"data":{"type":"*"},"maxzoom":{"type":"number","default":18},"minzoom":{"type":"number","default":0},"attribution":{"type":"string"},"buffer":{"type":"number","default":128,"maximum":512,"minimum":0},"filter":{"type":"*"},"tolerance":{"type":"number","default":0.375},"cluster":{"type":"boolean","default":false},"clusterRadius":{"type":"number","default":50,"minimum":0},"clusterMaxZoom":{"type":"number"},"clusterMinPoints":{"type":"number"},"clusterProperties":{"type":"*"},"lineMetrics":{"type":"boolean","default":false},"generateId":{"type":"boolean","default":false},"promoteId":{"type":"promoteId"},"dynamic":{"type":"boolean","default":false}},"source_video":{"type":{"type":"enum","values":{"video":1}},"urls":{"type":"array","value":"string"},"coordinates":{"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"source_image":{"type":{"type":"enum","values":{"image":1}},"url":{"type":"string"},"coordinates":{"type":"array","length":4,"value":{"type":"array","length":2,"value":"number"}}},"modelNodeOverride":{"orientation":{"type":"array","value":"number","length":3,"default":[0,0,0],"period":360}},"modelNodeOverrides":{"*":{"type":"modelNodeOverride"}},"modelMaterialOverride":{"model-color":{"type":"color"},"model-color-mix-intensity":{"type":"number"},"model-opacity":{"type":"number"},"model-emissive-strength":{"type":"number"}},"modelMaterialOverrides":{"*":{"type":"modelMaterialOverride"}},"modelSourceModel":{"uri":{"type":"string"},"position":{"type":"array","value":"number","length":2,"default":[0,0],"minimum":[-180,-90],"maximum":[180,90]},"orientation":{"type":"array","value":"number","length":3,"default":[0,0,0],"period":360},"nodeOverrides":{"type":"modelNodeOverrides"},"materialOverrides":{"type":"modelMaterialOverrides"},"nodeOverrideNames":{"type":"array","value":"string"},"materialOverrideNames":{"type":"array","value":"string"},"featureProperties":{"type":"*"}},"modelSourceModels":{"*":{"type":"modelSourceModel"}},"source_model":{"type":{"type":"enum","values":{"model":1,"batched-model":1}},"maxzoom":{"type":"number","default":18},"minzoom":{"type":"number","default":0},"tiles":{"type":"array","value":"string"},"models":{"type":"modelSourceModels"}},"layer":{"id":{"type":"string"},"type":{"type":"enum","values":{"fill":{},"line":{},"symbol":{},"circle":{},"heatmap":{},"fill-extrusion":{},"building":{},"raster":{},"raster-particle":{},"hillshade":{},"model":{},"background":{},"sky":{},"slot":{},"clip":{}}},"metadata":{"type":"*"},"source":{"type":"string"},"source-layer":{"type":"string"},"slot":{"type":"string"},"minzoom":{"type":"number","minimum":0,"maximum":24},"maxzoom":{"type":"number","minimum":0,"maximum":24},"filter":{"type":"filter"},"layout":{"type":"layout"},"paint":{"type":"paint"},"appearances":{"type":"array","value":"appearance","supported-layer-types":["symbol"]}},"appearance":{"condition":{"type":"boolean","expression":{"interpolated":true,"parameters":["zoom","pitch","feature","feature-state","measure-light","distance-from-center"]},"property-type":"data-driven"},"name":{"type":"string"},"properties":{"type":"*"}},"layout":["layout_clip","layout_fill","layout_line","layout_circle","layout_heatmap","layout_fill-extrusion","layout_building","layout_symbol","layout_raster","layout_raster-particle","layout_hillshade","layout_background","layout_sky","layout_model"],"layout_background":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_sky":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_model":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}},"model-id":{"type":"string","default":"","property-type":"data-driven","expression":{"parameters":["zoom","feature"]}}},"layout_clip":{"clip-layer-types":{"type":"array","value":"enum","values":{"model":1,"symbol":1},"default":[],"expression":{}},"clip-layer-scope":{"type":"array","value":"string","default":[],"expression":{}}},"layout_fill":{"fill-sort-key":{"type":"number","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}},"fill-elevation-reference":{"type":"enum","values":{"none":1,"hd-road-base":1,"hd-road-markup":1},"default":"none","expression":{}},"fill-construct-bridge-guard-rail":{"type":"boolean","default":"true","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"}},"layout_circle":{"circle-sort-key":{"type":"number","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"circle-elevation-reference":{"type":"enum","values":{"none":1,"hd-road-markup":1},"default":"none","expression":{}},"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_heatmap":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_fill-extrusion":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}},"fill-extrusion-edge-radius":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{}}},"layout_building":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}},"building-facade":{"type":"boolean","default":false,"expression":{"parameters":["feature"]},"property-type":"data-driven"},"building-facade-floors":{"type":"number","minimum":1,"maximum":200,"default":3,"property-type":"data-driven","expression":{"parameters":["feature"]}},"building-facade-unit-width":{"type":"number","minimum":1,"maximum":20,"default":3.1,"property-type":"data-driven","expression":{"parameters":["feature"]}},"building-facade-window":{"type":"array","length":2,"value":"number","minimum":0.1,"maximum":1,"default":[0.9,0.9],"property-type":"data-driven","expression":{"parameters":["feature"]}},"building-roof-shape":{"type":"enum","values":{"flat":1,"hipped":1,"gabled":1,"parapet":1,"mansard":1,"skillion":1,"pyramidal":1},"default":"flat","expression":{"parameters":["feature"]},"property-type":"data-driven"},"building-height":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{},"property-type":"data-driven"},"building-base":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{},"property-type":"data-driven"},"building-flood-light-wall-radius":{"property-type":"data-driven","type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state"]}},"building-flood-light-ground-radius":{"property-type":"data-driven","type":"number","default":0,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state"]}},"building-flip-roof-orientation":{"property-type":"data-driven","type":"boolean","default":false,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state"]}}},"layout_line":{"line-cap":{"type":"enum","values":{"butt":1,"round":1,"square":1},"default":"butt","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"line-join":{"type":"enum","values":{"bevel":1,"round":1,"miter":1,"none":1},"default":"miter","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"line-miter-limit":{"type":"number","default":2,"expression":{"interpolated":true,"parameters":["zoom"]}},"line-round-limit":{"type":"number","default":1.05,"expression":{"interpolated":true,"parameters":["zoom"]}},"line-sort-key":{"type":"number","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"line-z-offset":{"type":"number","default":0,"expression":{"parameters":["zoom","feature","line-progress"]},"property-type":"data-driven"},"line-elevation-reference":{"type":"enum","values":{"none":1,"sea":1,"ground":1,"hd-road-markup":1},"default":"none","expression":{}},"line-cross-slope":{"type":"number","expression":{}},"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}},"line-width-unit":{"type":"enum","values":{"pixels":1,"meters":1},"default":"pixels","expression":{"parameters":["zoom"]}}},"layout_symbol":{"symbol-placement":{"type":"enum","values":{"point":1,"line":1,"line-center":1},"default":"point","expression":{"parameters":["zoom"]}},"symbol-spacing":{"type":"number","default":250,"minimum":1,"expression":{"interpolated":true,"parameters":["zoom"]}},"symbol-avoid-edges":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"symbol-sort-key":{"type":"number","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"symbol-z-order":{"type":"enum","values":{"auto":1,"viewport-y":1,"source":1},"default":"auto","expression":{"parameters":["zoom"]}},"symbol-z-elevate":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"symbol-elevation-reference":{"type":"enum","values":{"sea":1,"ground":1,"hd-road-markup":1},"default":"ground","expression":{"parameters":["zoom"]}},"icon-allow-overlap":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"icon-ignore-placement":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"icon-optional":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"icon-rotation-alignment":{"type":"enum","values":{"map":1,"viewport":1,"auto":1},"default":"auto","expression":{"parameters":["zoom"]}},"icon-size":{"type":"number","default":1,"minimum":0,"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-size-scale-range":{"type":"array","value":"number","length":2,"default":[0.8,2],"minimum":[0.1,0.1],"maximum":[10,10],"expression":{}},"icon-text-fit":{"type":"enum","values":{"none":1,"width":1,"height":1,"both":1},"default":"none","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-text-fit-padding":{"type":"array","value":"number","length":4,"default":[0,0,0,0],"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-image":{"type":"resolvedImage","tokens":true,"appearance":true,"use-theme":true,"expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-rotate":{"type":"number","default":0,"period":360,"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-padding":{"type":"number","default":2,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]}},"icon-keep-upright":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"icon-offset":{"type":"array","value":"number","length":2,"default":[0,0],"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-anchor":{"type":"enum","values":{"center":1,"left":1,"right":1,"top":1,"bottom":1,"top-left":1,"top-right":1,"bottom-left":1,"bottom-right":1},"default":"center","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"icon-pitch-alignment":{"type":"enum","values":{"map":1,"viewport":1,"auto":1},"default":"auto","expression":{"parameters":["zoom"]}},"text-pitch-alignment":{"type":"enum","values":{"map":1,"viewport":1,"auto":1},"default":"auto","expression":{"parameters":["zoom"]}},"text-rotation-alignment":{"type":"enum","values":{"map":1,"viewport":1,"auto":1},"default":"auto","expression":{"parameters":["zoom"]}},"text-field":{"type":"formatted","default":"","tokens":true,"expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-font":{"type":"array","value":"string","default":["Open Sans Regular","Arial Unicode MS Regular"],"expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-size":{"type":"number","default":16,"minimum":0,"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-size-scale-range":{"type":"array","value":"number","length":2,"default":[0.8,2],"minimum":[0.1,0.1],"maximum":[10,10],"expression":{}},"text-max-width":{"type":"number","default":10,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-line-height":{"type":"number","default":1.2,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-letter-spacing":{"type":"number","default":0,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-justify":{"type":"enum","values":{"auto":1,"left":1,"center":1,"right":1},"default":"center","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-radial-offset":{"type":"number","default":0,"property-type":"data-driven","expression":{"interpolated":true,"parameters":["zoom","feature"]}},"text-variable-anchor":{"type":"array","value":"enum","values":{"center":1,"left":1,"right":1,"top":1,"bottom":1,"top-left":1,"top-right":1,"bottom-left":1,"bottom-right":1},"expression":{"parameters":["zoom"]}},"text-anchor":{"type":"enum","values":{"center":1,"left":1,"right":1,"top":1,"bottom":1,"top-left":1,"top-right":1,"bottom-left":1,"bottom-right":1},"default":"center","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-max-angle":{"type":"number","default":45,"expression":{"interpolated":true,"parameters":["zoom"]}},"text-writing-mode":{"type":"array","value":"enum","values":{"horizontal":1,"vertical":1},"expression":{"parameters":["zoom"]}},"text-rotate":{"type":"number","default":0,"period":360,"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-padding":{"type":"number","default":2,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]}},"text-keep-upright":{"type":"boolean","default":true,"expression":{"parameters":["zoom"]}},"text-transform":{"type":"enum","values":{"none":1,"uppercase":1,"lowercase":1},"default":"none","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-offset":{"type":"array","value":"number","length":2,"default":[0,0],"appearance":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"text-allow-overlap":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"text-ignore-placement":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"text-optional":{"type":"boolean","default":false,"expression":{"parameters":["zoom"]}},"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_raster":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_raster-particle":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"layout_hillshade":{"visibility":{"type":"enum","values":{"visible":1,"none":1},"default":"visible","expression":{}}},"filter":{"type":"array","value":"*"},"filter_symbol":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature","pitch","distance-from-center"]}},"filter_fill":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_hillshade":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_raster":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_raster-particle":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_clip":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_model":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_line":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_circle":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_fill-extrusion":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_building":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_heatmap":{"type":"boolean","default":false,"property-type":"data-driven","expression":{"parameters":["zoom","feature"]}},"filter_operator":{"type":"enum","values":{"==":1,"!=":1,">":1,">=":1,"<":1,"<=":1,"in":1,"!in":1,"all":1,"any":1,"none":1,"has":1,"!has":1}},"geometry_type":{"type":"enum","values":{"Point":1,"LineString":1,"Polygon":1}},"function":{"expression":{"type":"expression"},"stops":{"type":"array","value":"function_stop"},"base":{"type":"number","default":1,"minimum":0},"property":{"type":"string","default":"$zoom"},"type":{"type":"enum","values":{"identity":1,"exponential":1,"interval":1,"categorical":1},"default":"exponential"},"colorSpace":{"type":"enum","values":{"rgb":1,"lab":1,"hcl":1},"default":"rgb"},"default":{"type":"*"}},"function_stop":{"type":"array","minimum":0,"maximum":24,"value":["number","color"],"length":2},"expression":{"type":"array","value":"*","minimum":1},"fog":{"range":{"type":"array","default":[0.5,10],"minimum":-20,"maximum":20,"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true}},"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"high-color":{"type":"color","default":"#245cdf","expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"space-color":{"type":"color","default":["interpolate",["linear"],["zoom"],4,"#010b19",7,"#367ab9"],"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"horizon-blend":{"type":"number","default":["interpolate",["linear"],["zoom"],4,0.2,7,0.1],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"star-intensity":{"type":"number","default":["interpolate",["linear"],["zoom"],5,0.35,6,0],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"vertical-range":{"type":"array","default":[0,0],"minimum":0,"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true}}},"snow":{"density":{"type":"number","default":["interpolate",["linear"],["zoom"],11,0,13,0.85],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"intensity":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"vignette":{"type":"number","default":["interpolate",["linear"],["zoom"],11,0,13,0.3],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"vignette-color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"center-thinning":{"type":"number","default":0.4,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"direction":{"type":"array","default":[0,50],"minimum":0,"maximum":360,"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true}},"flake-size":{"type":"number","default":0.71,"minimum":0,"maximum":5,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true}},"rain":{"density":{"type":"number","default":["interpolate",["linear"],["zoom"],11,0,13,0.5],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"intensity":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"color":{"type":"color","default":["interpolate",["linear"],["measure-light","brightness"],0,"#03113d",0.3,"#a8adbc"],"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"opacity":{"type":"number","default":["interpolate",["linear"],["measure-light","brightness"],0,0.88,1,0.7],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"vignette":{"type":"number","default":["interpolate",["linear"],["zoom"],11,0,13,1],"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"vignette-color":{"type":"color","default":["interpolate",["linear"],["measure-light","brightness"],0,"#001736",0.3,"#464646"],"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"use-theme":true,"transition":true},"center-thinning":{"type":"number","default":0.57,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true},"direction":{"type":"array","default":[0,80],"minimum":0,"maximum":360,"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true}},"droplet-size":{"type":"array","default":[2.6,18.2],"minimum":0,"maximum":50,"length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true}},"distortion-strength":{"type":"number","default":0.7,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"],"relaxZoomRestriction":true},"transition":true}},"camera":{"camera-projection":{"type":"enum","values":{"perspective":1,"orthographic":1},"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]},"default":"perspective"}},"colorTheme":{"data":{"type":"string","expression":{}}},"indoor_source":{"sourceId":{"type":"string"},"sourceLayers":{"type":"array","value":"string"}},"indoor":{"*":{"type":"indoor_source"}},"light":{"anchor":{"type":"enum","default":"viewport","values":{"map":1,"viewport":1},"expression":{"parameters":["zoom"]}},"position":{"type":"array","default":[1.15,210,30],"length":3,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"color":{"type":"color","default":"#ffffff","expression":{"interpolated":true,"parameters":["zoom"]},"use-theme":true,"transition":true},"intensity":{"type":"number","default":0.5,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"projection":{"name":{"type":"enum","values":{"albers":1,"equalEarth":1,"equirectangular":1,"lambertConformalConic":1,"mercator":1,"naturalEarth":1,"winkelTripel":1,"globe":1},"default":"mercator"},"center":{"type":"array","length":2,"value":"number","minimum":[-180,-90],"maximum":[180,90]},"parallels":{"type":"array","length":2,"value":"number","minimum":[-90,-90],"maximum":[90,90]}},"terrain":{"source":{"type":"string"},"exaggeration":{"type":"number","default":1,"minimum":0,"maximum":1000,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"paint":["paint_fill","paint_line","paint_circle","paint_heatmap","paint_fill-extrusion","paint_building","paint_symbol","paint_raster","paint_raster-particle","paint_hillshade","paint_background","paint_sky","paint_model"],"paint_fill":{"fill-antialias":{"type":"boolean","default":true,"expression":{"parameters":["zoom"]}},"fill-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"fill-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"fill-outline-color":{"type":"color","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"fill-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"fill-pattern":{"type":"resolvedImage","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"fill-pattern-cross-fade":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"fill-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"fill-z-offset":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"},"fill-bridge-guard-rail-color":{"type":"color","default":"rgba(241, 236, 225, 255)","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light","feature"]},"property-type":"data-driven"},"fill-tunnel-structure-color":{"type":"color","default":"rgba(241, 236, 225, 255)","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light","feature"]},"property-type":"data-driven"}},"paint_fill-extrusion":{"fill-extrusion-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-extrusion-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"fill-extrusion-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-extrusion-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"fill-extrusion-pattern":{"type":"resolvedImage","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"fill-extrusion-pattern-cross-fade":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"fill-extrusion-height":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state"]},"property-type":"data-driven"},"fill-extrusion-base":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state"]},"property-type":"data-driven"},"fill-extrusion-height-alignment":{"type":"enum","values":{"terrain":1,"flat":1},"default":"flat"},"fill-extrusion-base-alignment":{"type":"enum","values":{"terrain":1,"flat":1},"default":"terrain"},"fill-extrusion-vertical-gradient":{"type":"boolean","default":true,"expression":{"parameters":["zoom"]}},"fill-extrusion-ambient-occlusion-intensity":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"fill-extrusion-ambient-occlusion-radius":{"type":"number","default":3,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"fill-extrusion-ambient-occlusion-wall-radius":{"type":"number","default":3,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"fill-extrusion-ambient-occlusion-ground-radius":{"type":"number","default":3,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"fill-extrusion-ambient-occlusion-ground-attenuation":{"type":"number","default":0.69,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-extrusion-flood-light-color":{"type":"color","default":"#ffffff","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"fill-extrusion-flood-light-intensity":{"type":"number","default":0,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"fill-extrusion-flood-light-wall-radius":{"property-type":"data-driven","type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state"]}},"fill-extrusion-flood-light-ground-radius":{"property-type":"data-driven","type":"number","default":0,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state"]}},"fill-extrusion-flood-light-ground-attenuation":{"type":"number","default":0.69,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-extrusion-vertical-scale":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"fill-extrusion-rounded-roof":{"type":"boolean","default":true,"expression":{"parameters":["zoom"]}},"fill-extrusion-cutoff-fade-range":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{}},"fill-extrusion-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light","feature-state"]},"property-type":"data-driven"},"fill-extrusion-line-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"fill-extrusion-cast-shadows":{"type":"boolean","default":true}},"paint_building":{"building-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"building-ambient-occlusion-intensity":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"parameters":[]},"transition":true},"building-ambient-occlusion-ground-intensity":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"building-ambient-occlusion-ground-radius":{"type":"number","default":3,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"building-ambient-occlusion-ground-attenuation":{"type":"number","default":0.69,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"building-vertical-scale":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"building-cast-shadows":{"type":"boolean","default":true},"building-color":{"type":"color","default":"rgba(193, 154, 127, 1)","use-theme":true,"expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light"]},"property-type":"data-driven"},"building-emissive-strength":{"type":"number","default":0,"minimum":0,"maximum":5,"expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light"]},"property-type":"data-driven"},"building-facade-emissive-chance":{"type":"number","default":0.35,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["measure-light","zoom"]}},"building-cutoff-fade-range":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{}},"building-flood-light-color":{"type":"color","default":"#ffffff","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"building-flood-light-intensity":{"type":"number","default":0,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"building-flood-light-ground-attenuation":{"type":"number","default":0.69,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}}},"paint_line":{"line-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"line-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"line-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"line-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"line-width":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light","line-progress"]},"property-type":"data-driven"},"line-gap-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"line-offset":{"type":"number","default":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"line-blur":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"line-dasharray":{"type":"array","value":"number","minimum":0,"expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"line-pattern":{"type":"resolvedImage","expression":{"parameters":["zoom","feature"]},"property-type":"data-driven"},"line-pattern-cross-fade":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"line-gradient":{"type":"color","use-theme":true,"expression":{"interpolated":true,"parameters":["line-progress"]}},"line-trim-offset":{"type":"array","value":"number","length":2,"default":[0,0],"minimum":[0,0],"maximum":[1,1]},"line-trim-fade-range":{"type":"array","value":"number","length":2,"default":[0,0],"minimum":[0,0],"maximum":[1,1],"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"line-trim-color":{"type":"color","default":"transparent","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"line-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]},"property-type":"data-driven"},"line-border-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state"]},"property-type":"data-driven"},"line-border-color":{"type":"color","default":"rgba(0, 0, 0, 0)","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state"]},"property-type":"data-driven"},"line-occlusion-opacity":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true}},"paint_circle":{"circle-radius":{"type":"number","default":5,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-blur":{"type":"number","default":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"circle-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"circle-pitch-scale":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"circle-pitch-alignment":{"type":"enum","values":{"map":1,"viewport":1},"default":"viewport","expression":{"parameters":["zoom"]}},"circle-stroke-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-stroke-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-stroke-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"circle-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}}},"paint_heatmap":{"heatmap-radius":{"type":"number","default":30,"minimum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"heatmap-weight":{"type":"number","default":1,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"heatmap-intensity":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"heatmap-color":{"type":"color","default":["interpolate",["linear"],["heatmap-density"],0,"rgba(0, 0, 255, 0)",0.1,"royalblue",0.3,"cyan",0.5,"lime",0.7,"yellow",1,"red"],"use-theme":true,"expression":{"interpolated":true,"parameters":["heatmap-density"]}},"heatmap-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}}},"paint_symbol":{"icon-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-occlusion-opacity":{"type":"number","minimum":0,"maximum":1,"default":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-emissive-strength":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light","feature-state"]},"property-type":"data-driven"},"text-emissive-strength":{"type":"number","default":1,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light","feature-state"]},"property-type":"data-driven"},"icon-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-halo-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-halo-blur":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"icon-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"icon-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"icon-image-cross-fade":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"text-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-occlusion-opacity":{"type":"number","minimum":0,"maximum":1,"default":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"overridable":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-halo-color":{"type":"color","default":"rgba(0, 0, 0, 0)","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-halo-width":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-halo-blur":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature","feature-state","measure-light"]},"property-type":"data-driven"},"text-translate":{"type":"array","value":"number","length":2,"default":[0,0],"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"text-translate-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":["zoom"]}},"icon-color-saturation":{"type":"number","default":0,"minimum":-1,"maximum":1,"expression":{}},"icon-color-contrast":{"type":"number","default":0,"minimum":-1,"maximum":1,"expression":{}},"icon-color-brightness-min":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{}},"icon-color-brightness-max":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{}},"symbol-z-offset":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","feature"]},"property-type":"data-driven"}},"paint_raster":{"raster-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-color":{"type":"color","use-theme":true,"expression":{"interpolated":true,"parameters":["raster-value"]}},"raster-color-mix":{"type":"array","default":[0.2126,0.7152,0.0722,0],"length":4,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-color-range":{"type":"array","length":2,"value":"number","transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-hue-rotate":{"type":"number","default":0,"period":360,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-brightness-min":{"type":"number","default":0,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-brightness-max":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-saturation":{"type":"number","default":0,"minimum":-1,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-contrast":{"type":"number","default":0,"minimum":-1,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-resampling":{"type":"enum","values":{"linear":1,"nearest":1},"default":"linear","expression":{"parameters":["zoom"]}},"raster-fade-duration":{"type":"number","default":300,"minimum":0,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"raster-array-band":{"type":"string"},"raster-elevation":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}}},"paint_raster-particle":{"raster-particle-array-band":{"type":"string"},"raster-particle-count":{"type":"number","default":512,"minimum":1},"raster-particle-color":{"type":"color","use-theme":true,"expression":{"interpolated":true,"parameters":["raster-particle-speed"]}},"raster-particle-max-speed":{"type":"number","default":1,"minimum":1},"raster-particle-speed-factor":{"type":"number","default":0.2,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-particle-fade-opacity-factor":{"type":"number","default":0.98,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"raster-particle-reset-rate-factor":{"type":"number","default":0.8,"minimum":0,"maximum":1},"raster-particle-elevation":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}}},"paint_hillshade":{"hillshade-illumination-direction":{"type":"number","default":335,"minimum":0,"maximum":359,"expression":{"interpolated":true,"parameters":["zoom"]}},"hillshade-illumination-anchor":{"type":"enum","values":{"map":1,"viewport":1},"default":"viewport","expression":{"parameters":["zoom"]}},"hillshade-exaggeration":{"type":"number","default":0.5,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"hillshade-shadow-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"hillshade-highlight-color":{"type":"color","default":"#FFFFFF","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"hillshade-accent-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"hillshade-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}}},"paint_background":{"background-pitch-alignment":{"type":"enum","values":{"map":1,"viewport":1},"default":"map","expression":{"parameters":[]}},"background-color":{"type":"color","default":"#000000","use-theme":true,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}},"background-pattern":{"type":"resolvedImage","expression":{"parameters":["zoom"]}},"background-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}},"background-emissive-strength":{"type":"number","default":0,"minimum":0,"transition":true,"expression":{"interpolated":true,"parameters":["zoom","measure-light"]}}},"paint_sky":{"sky-type":{"type":"enum","values":{"gradient":1,"atmosphere":1},"default":"atmosphere","expression":{"parameters":["zoom"]}},"sky-atmosphere-sun":{"type":"array","value":"number","length":2,"minimum":[0,0],"maximum":[360,180],"expression":{"parameters":["zoom"]}},"sky-atmosphere-sun-intensity":{"type":"number","default":10,"minimum":0,"maximum":100},"sky-gradient-center":{"type":"array","value":"number","default":[0,0],"length":2,"minimum":[0,0],"maximum":[360,180],"expression":{"parameters":["zoom"]}},"sky-gradient-radius":{"type":"number","default":90,"minimum":0,"maximum":180,"expression":{"parameters":["zoom"]}},"sky-gradient":{"type":"color","default":["interpolate",["linear"],["sky-radial-progress"],0.8,"#87ceeb",1,"white"],"use-theme":true,"expression":{"interpolated":true,"parameters":["sky-radial-progress"]}},"sky-atmosphere-halo-color":{"type":"color","default":"white","use-theme":true},"sky-atmosphere-color":{"type":"color","default":"white","use-theme":true},"sky-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["zoom"]}}},"paint_model":{"model-opacity":{"type":"number","default":1,"minimum":0,"maximum":1,"transition":true,"expression":{"interpolated":true,"parameters":["feature","feature-state","zoom"]},"property-type":"data-driven"},"model-rotation":{"type":"array","value":"number","length":3,"default":[0,0,0],"period":360,"property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state","zoom"]},"transition":true},"model-scale":{"type":"array","value":"number","length":3,"default":[1,1,1],"property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state","zoom"]},"transition":true},"model-translation":{"type":"array","value":"number","length":3,"default":[0,0,0],"property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state","zoom"]},"transition":true},"model-color":{"type":"color","default":"#ffffff","property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light","zoom"]},"use-theme":true,"transition":true},"model-color-mix-intensity":{"type":"number","property-type":"data-driven","default":0,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light"]},"transition":true},"model-type":{"type":"enum","values":{"common-3d":1,"location-indicator":1},"default":"common-3d"},"model-cast-shadows":{"type":"boolean","default":true},"model-receive-shadows":{"type":"boolean","default":true},"model-ambient-occlusion-intensity":{"type":"number","default":1,"minimum":0,"maximum":1,"expression":{"interpolated":true,"parameters":["zoom"]},"transition":true},"model-emissive-strength":{"type":"number","property-type":"data-driven","default":0,"minimum":0,"maximum":5,"expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light"]},"transition":true},"model-roughness":{"type":"number","default":1,"minimum":0,"maximum":1,"property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state"]},"transition":true},"model-height-based-emissive-strength-multiplier":{"type":"array","default":[1,1,1,1,0],"length":5,"value":"number","property-type":"data-driven","expression":{"interpolated":true,"parameters":["feature","feature-state","measure-light"]},"transition":true},"model-cutoff-fade-range":{"type":"number","default":0,"minimum":0,"maximum":1,"expression":{}},"model-front-cutoff":{"type":"array","value":"number","expression":{"interpolated":true,"parameters":["zoom"]},"length":3,"default":[0,0,1],"minimum":[0,0,0],"maximum":[1,1,1]},"model-elevation-reference":{"type":"enum","values":{"sea":1,"ground":1,"hd-road-markup":1},"default":"ground","expression":{}}},"transition":{"duration":{"type":"number","default":300,"minimum":0},"delay":{"type":"number","default":0,"minimum":0}},"promoteId":{"*":{"type":"*"}}}');
function unbundle(value) {
if (value instanceof Number || value instanceof String || value instanceof Boolean) {
return value.valueOf();
} else {
return value;
}
}
function deepUnbundle(value) {
if (Array.isArray(value)) {
return value.map(deepUnbundle);
} else if (value instanceof Object && !(value instanceof Number || value instanceof String || value instanceof Boolean)) {
const unbundledValue = {};
for (const key in value) {
unbundledValue[key] = deepUnbundle(value[key]);
}
return unbundledValue;
}
return unbundle(value);
}
function isExpressionFilter(filter) {
if (filter === true || filter === false) {
return true;
}
if (!Array.isArray(filter) || filter.length === 0) {
return false;
}
switch (filter[0]) {
case "has":
return filter.length >= 2 && filter[1] !== "$id" && filter[1] !== "$type";
case "in":
return filter.length >= 3 && (typeof filter[1] !== "string" || Array.isArray(filter[2]));
case "!in":
case "!has":
case "none":
return false;
case "==":
case "!=":
case ">":
case ">=":
case "<":
case "<=":
return filter.length !== 3 || (Array.isArray(filter[1]) || Array.isArray(filter[2]));
case "any":
case "all":
for (const f of filter.slice(1)) {
if (!isExpressionFilter(f) && typeof f !== "boolean") {
return false;
}
}
return true;
default:
return true;
}
}
function createFilter(filter, scope = "", options = null, layerType = "fill") {
if (filter === null || filter === void 0) {
return { filter: () => true, needGeometry: false, needFeature: false };
}
if (!isExpressionFilter(filter)) {
filter = convertFilter(filter);
}
const filterExp = filter;
let staticFilter = true;
try {
staticFilter = extractStaticFilter(filterExp);
} catch (e) {
console.warn(
`Failed to extract static filter. Filter will continue working, but at higher memory usage and slower framerate.
This is most likely a bug, please report this via https://github.com/mapbox/mapbox-gl-js/issues/new?assignees=&labels=&template=Bug_report.md
and paste the contents of this message in the report.
Thank you!
Filter Expression:
${JSON.stringify(filterExp, null, 2)}
`
);
}
let filterFunc = null;
let filterSpec = null;
if (layerType !== "background" && layerType !== "sky" && layerType !== "slot") {
filterSpec = spec[`filter_${layerType}`];
assert$1(filterSpec);
const compiledStaticFilter = createExpression(staticFilter, filterSpec, scope, options);
if (compiledStaticFilter.result === "error") {
throw new Error(compiledStaticFilter.value.map((err) => `${err.key}: ${err.message}`).join(", "));
} else {
filterFunc = (globalProperties, feature, canonical) => compiledStaticFilter.value.evaluate(globalProperties, feature, {}, canonical);
}
}
let dynamicFilterFunc = null;
let needFeature = null;
if (staticFilter !== filterExp) {
const compiledDynamicFilter = createExpression(filterExp, filterSpec, scope, options);
if (compiledDynamicFilter.result === "error") {
throw new Error(compiledDynamicFilter.value.map((err) => `${err.key}: ${err.message}`).join(", "));
} else {
dynamicFilterFunc = (globalProperties, feature, canonical, featureTileCoord, featureDistanceData) => compiledDynamicFilter.value.evaluate(globalProperties, feature, {}, canonical, void 0, void 0, featureTileCoord, featureDistanceData);
needFeature = !isFeatureConstant(compiledDynamicFilter.value.expression);
}
}
filterFunc = filterFunc;
const needGeometry = geometryNeeded(staticFilter);
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
filter: filterFunc,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
dynamicFilter: dynamicFilterFunc ? dynamicFilterFunc : void 0,
needGeometry,
needFeature: !!needFeature
};
}
function extractStaticFilter(filter) {
if (!isDynamicFilter(filter)) {
return filter;
}
let result = deepUnbundle(filter);
unionDynamicBranches(result);
result = collapseDynamicBooleanExpressions(result);
return result;
}
function collapseDynamicBooleanExpressions(expression) {
if (!Array.isArray(expression)) {
return expression;
}
const collapsed = collapsedExpression(expression);
if (collapsed === true) {
return collapsed;
} else {
return collapsed.map((subExpression) => collapseDynamicBooleanExpressions(subExpression));
}
}
function unionDynamicBranches(filter) {
let isBranchingDynamically = false;
const branches = [];
if (filter[0] === "case") {
for (let i = 1; i < filter.length - 1; i += 2) {
isBranchingDynamically = isBranchingDynamically || isDynamicFilter(filter[i]);
branches.push(filter[i + 1]);
}
branches.push(filter[filter.length - 1]);
} else if (filter[0] === "match") {
isBranchingDynamically = isBranchingDynamically || isDynamicFilter(filter[1]);
for (let i = 2; i < filter.length - 1; i += 2) {
branches.push(filter[i + 1]);
}
branches.push(filter[filter.length - 1]);
} else if (filter[0] === "step") {
isBranchingDynamically = isBranchingDynamically || isDynamicFilter(filter[1]);
for (let i = 1; i < filter.length - 1; i += 2) {
branches.push(filter[i + 1]);
}
}
if (isBranchingDynamically) {
filter.length = 0;
filter.push("any", ...branches);
}
for (let i = 1; i < filter.length; i++) {
unionDynamicBranches(filter[i]);
}
}
function isDynamicFilter(filter) {
if (!Array.isArray(filter)) {
return false;
}
if (isRootExpressionDynamic(filter[0])) {
return true;
}
for (let i = 1; i < filter.length; i++) {
const child = filter[i];
if (isDynamicFilter(child)) {
return true;
}
}
return false;
}
function isRootExpressionDynamic(expression) {
return expression === "pitch" || expression === "distance-from-center";
}
const dynamicConditionExpressions = /* @__PURE__ */ new Set([
"in",
"==",
"!=",
">",
">=",
"<",
"<=",
"to-boolean"
]);
function collapsedExpression(expression) {
if (dynamicConditionExpressions.has(expression[0])) {
for (let i = 1; i < expression.length; i++) {
const param = expression[i];
if (isDynamicFilter(param)) {
return true;
}
}
}
return expression;
}
function compare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
function geometryNeeded(filter) {
if (!Array.isArray(filter)) return false;
if (filter[0] === "within" || filter[0] === "distance") return true;
for (let index = 1; index < filter.length; index++) {
if (geometryNeeded(filter[index])) return true;
}
return false;
}
function convertFilter(filter) {
if (!filter) return true;
const op = filter[0];
if (filter.length <= 1) return op !== "any";
const converted = (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "==" ? convertComparisonOp(filter[1], filter[2], "==") : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "!=" ? convertNegation(convertComparisonOp(filter[1], filter[2], "==")) : op === "<" || op === ">" || op === "<=" || // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === ">=" ? convertComparisonOp(filter[1], filter[2], op) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "any" ? convertDisjunctionOp(filter.slice(1)) : (
// @ts-expect-error - TS2769 - No overload matches this call.
op === "all" ? ["all"].concat(filter.slice(1).map(convertFilter)) : (
// @ts-expect-error - TS2769 - No overload matches this call.
op === "none" ? ["all"].concat(filter.slice(1).map(convertFilter).map(convertNegation)) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "in" ? convertInOp(filter[1], filter.slice(2)) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "!in" ? convertNegation(convertInOp(filter[1], filter.slice(2))) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "has" ? convertHasOp(filter[1]) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
op === "!has" ? convertNegation(convertHasOp(filter[1])) : true
)
)
)
)
)
)
)
)
);
return converted;
}
function convertComparisonOp(property, value, op) {
switch (property) {
case "$type":
return [`filter-type-${op}`, value];
case "$id":
return [`filter-id-${op}`, value];
default:
return [`filter-${op}`, property, value];
}
}
function convertDisjunctionOp(filters) {
return ["any"].concat(filters.map(convertFilter));
}
function convertInOp(property, values) {
if (values.length === 0) {
return false;
}
switch (property) {
case "$type":
return [`filter-type-in`, ["literal", values]];
case "$id":
return [`filter-id-in`, ["literal", values]];
default:
if (values.length > 200 && !values.some((v) => typeof v !== typeof values[0])) {
return ["filter-in-large", property, ["literal", values.sort(compare)]];
} else {
return ["filter-in-small", property, ["literal", values]];
}
}
}
function convertHasOp(property) {
switch (property) {
case "$type":
return true;
case "$id":
return [`filter-has-id`];
default:
return [`filter-has`, property];
}
}
function convertNegation(filter) {
return ["!", filter];
}
const FQIDSeparator = "";
function isFQID(id) {
return id.indexOf(FQIDSeparator) >= 0;
}
function makeFQID(name, scope) {
if (!scope) return name;
return `${name}${FQIDSeparator}${scope}`;
}
function getNameFromFQID(fqid) {
const sep = fqid.indexOf(FQIDSeparator);
return sep >= 0 ? fqid.slice(0, sep) : fqid;
}
function getInnerScopeFromFQID(fqid) {
const sep = fqid.lastIndexOf(FQIDSeparator);
return sep >= 0 ? fqid.slice(sep + 1) : "";
}
function getOuterScopeFromFQID(fqid) {
const sep = fqid.indexOf(FQIDSeparator);
return sep >= 0 ? fqid.slice(sep + 1) : "";
}
let properties$2;
const getAppearanceProperties = () => properties$2 || (properties$2 = new Properties({
"icon-size": new DataDrivenProperty(spec["layout_symbol"]["icon-size"]),
"icon-image": new DataDrivenProperty(spec["layout_symbol"]["icon-image"]),
"icon-rotate": new DataDrivenProperty(spec["layout_symbol"]["icon-rotate"]),
"icon-offset": new DataDrivenProperty(spec["layout_symbol"]["icon-offset"]),
"text-size": new DataDrivenProperty(spec["layout_symbol"]["text-size"]),
"text-rotate": new DataDrivenProperty(spec["layout_symbol"]["text-rotate"]),
"text-offset": new DataDrivenProperty(spec["layout_symbol"]["text-offset"])
}));
class SymbolAppearance {
constructor(condition, name, properties, scope, options, iconImageUseTheme) {
const conditionSpec = spec["appearance"]["condition"];
const compiledExpression = createExpression(condition, conditionSpec);
if (compiledExpression.result === "success") {
this.condition = compiledExpression.value;
}
this.name = name;
if (properties) {
this.properties = new PossiblyEvaluated(getAppearanceProperties());
this.unevaluatedLayout = new Layout(getAppearanceProperties(), scope, options, iconImageUseTheme);
for (const property in properties) {
this.unevaluatedLayout.setValue(property, properties[property]);
}
}
}
isActive(context) {
if (!this.condition && context.isHidden && this.name === "hidden") return true;
return this.condition.evaluate(context.globals, context.feature, context.featureState, context.canonical);
}
getCondition() {
return this.condition;
}
getName() {
return this.name;
}
getProperty(name) {
return this.properties.get(name);
}
getUnevaluatedProperties() {
return this.unevaluatedLayout;
}
getUnevaluatedProperty(name) {
return this.unevaluatedLayout._values[name];
}
recalculate(parameters, availableImages, iconImageUseTheme) {
if (this.unevaluatedLayout) {
this.properties = this.unevaluatedLayout.possiblyEvaluate(parameters, void 0, availableImages, iconImageUseTheme);
}
}
serialize() {
const result = {};
result["condition"] = this.condition.expression.serialize();
if (this.name) result["name"] = this.name;
if (this.unevaluatedLayout) {
result["properties"] = this.unevaluatedLayout.serialize();
}
return result;
}
hasIconProperties() {
const iconImageProperty = this.hasProperty("icon-image");
const iconSizeProperty = this.hasProperty("icon-size");
const iconOffsetProperty = this.hasProperty("icon-offset");
const iconRotateProperty = this.hasProperty("icon-rotate");
return iconImageProperty || iconSizeProperty || iconOffsetProperty || iconRotateProperty;
}
hasTextProperties() {
const textSizeProperty = this.hasProperty("text-size");
const textOffsetProperty = this.hasProperty("text-offset");
const textRotateProperty = this.hasProperty("text-rotate");
return textSizeProperty || textOffsetProperty || textRotateProperty;
}
hasProperty(name) {
return this.getUnevaluatedProperty(name).value !== void 0;
}
}
const TRANSITION_SUFFIX = "-transition";
const drapedLayers = /* @__PURE__ */ new Set(["fill", "line", "background", "hillshade", "raster"]);
class StyleLayer extends Evented {
constructor(layer, properties, scope, lut, options, iconImageUseTheme) {
super();
this.id = layer.id;
this.fqid = makeFQID(this.id, scope);
this.type = layer.type;
this.scope = scope;
this.lut = lut;
this.options = options;
this.iconImageUseTheme = iconImageUseTheme;
this.appearances = new Array();
this._featureFilter = { filter: () => true, needGeometry: false, needFeature: false };
this._filterCompiled = false;
this.expressionDependencies = {
isIndoorDependent: false,
configDependencies: /* @__PURE__ */ new Set()
};
if (layer.type === "custom") return;
this.metadata = layer.metadata;
this.minzoom = layer.minzoom;
this.maxzoom = layer.maxzoom;
if (layer.type && layer.type !== "background" && layer.type !== "sky" && layer.type !== "slot") {
this.source = layer.source;
this.sourceLayer = layer["source-layer"];
this.filter = layer.filter;
const filterSpec = spec[`filter_${layer.type}`];
assert$1(filterSpec);
const compiledStaticFilter = createExpression(this.filter, filterSpec);
if (compiledStaticFilter.result !== "error") {
this.expressionDependencies.configDependencies = /* @__PURE__ */ new Set([...this.expressionDependencies.configDependencies, ...compiledStaticFilter.value.configDependencies]);
this.expressionDependencies.isIndoorDependent = this.expressionDependencies.isIndoorDependent || compiledStaticFilter.value.isIndoorDependent;
}
}
if (layer.slot) this.slot = layer.slot;
if (layer.appearances) {
this.setAppearances(layer.appearances);
}
if (properties.layout) {
this._unevaluatedLayout = new Layout(properties.layout, this.scope, options, this.iconImageUseTheme);
this.expressionDependencies.configDependencies = /* @__PURE__ */ new Set([...this.expressionDependencies.configDependencies, ...this._unevaluatedLayout.configDependencies]);
this.expressionDependencies.isIndoorDependent = this.expressionDependencies.isIndoorDependent || this._unevaluatedLayout.isIndoorDependent();
}
if (properties.paint) {
this._transitionablePaint = new Transitionable(properties.paint, this.scope, options);
for (const property in layer.paint) {
this.setPaintProperty(property, layer.paint[property]);
}
for (const property in layer.layout) {
this.setLayoutProperty(property, layer.layout[property]);
}
this.expressionDependencies.configDependencies = /* @__PURE__ */ new Set([...this.expressionDependencies.configDependencies, ...this._transitionablePaint.configDependencies]);
this.expressionDependencies.isIndoorDependent = this.expressionDependencies.isIndoorDependent || this._transitionablePaint.isIndoorDependent();
this._transitioningPaint = this._transitionablePaint.untransitioned();
this.paint = new PossiblyEvaluated(properties.paint);
}
}
// No-op in the StyleLayer class, must be implemented by each concrete StyleLayer
onAdd(_map) {
}
// No-op in the StyleLayer class, must be implemented by each concrete StyleLayer
onRemove(_map) {
}
isDraped(_sourceCache) {
return !this.is3D(true) && drapedLayers.has(this.type);
}
getLayoutProperty(name) {
if (name === "visibility") {
return this.visibility;
}
return this._unevaluatedLayout.getValue(name);
}
setLayoutProperty(name, value) {
if (this.type === "custom" && name === "visibility") {
this.visibility = value;
return;
}
const layout = this._unevaluatedLayout;
const specProps = layout._properties.properties;
if (!specProps[name]) return;
layout.setValue(name, value);
this.expressionDependencies.configDependencies = /* @__PURE__ */ new Set([...this.expressionDependencies.configDependencies, ...layout.configDependencies]);
this.expressionDependencies.isIndoorDependent = this.expressionDependencies.isIndoorDependent || layout.isIndoorDependent();
if (name === "visibility") {
this.possiblyEvaluateVisibility();
}
}
setAppearances(appearances) {
this.appearances = [];
appearances.forEach((a) => {
this.appearances.push(new SymbolAppearance(a.condition, a.name, a.properties, this.scope, this.options, this.iconImageUseTheme));
});
}
possiblyEvaluateVisibility() {
if (!this._unevaluatedLayout._values.visibility) {
return;
}
this.visibility = this._unevaluatedLayout._values.visibility.possiblyEvaluate({ zoom: 0 });
}
getPaintProperty(name) {
if (name.endsWith(TRANSITION_SUFFIX)) {
return this._transitionablePaint.getTransition(name.slice(0, -TRANSITION_SUFFIX.length));
} else {
return this._transitionablePaint.getValue(name);
}
}
isPaintProperty(name) {
const paint = this._transitionablePaint;
const specProps = paint._properties.properties;
return !!specProps[name];
}
setPaintProperty(name, value) {
const paint = this._transitionablePaint;
const specProps = paint._properties.properties;
if (name.endsWith(TRANSITION_SUFFIX)) {
const propName = name.slice(0, -TRANSITION_SUFFIX.length);
if (specProps[propName]) {
paint.setTransition(propName, value || void 0);
}
return false;
}
if (!specProps[name]) return false;
const transitionable = paint._values[name];
const wasDataDriven = transitionable.value.isDataDriven();
const oldValue = transitionable.value;
paint.setValue(name, value);
this.expressionDependencies.configDependencies = /* @__PURE__ */ new Set([...this.expressionDependencies.configDependencies, ...paint.configDependencies]);
this.expressionDependencies.isIndoorDependent = this.expressionDependencies.isIndoorDependent || paint.isIndoorDependent();
this._handleSpecialPaintPropertyUpdate(name);
const newValue = paint._values[name].value;
const isDataDriven = newValue.isDataDriven();
const isPattern = name.endsWith("pattern") || name === "line-dasharray";
return isDataDriven || wasDataDriven || isPattern || this._handleOverridablePaintPropertyUpdate(name, oldValue, newValue);
}
_handleSpecialPaintPropertyUpdate(_) {
}
getProgramIds() {
return null;
}
getDefaultProgramParams(name, zoom, lut) {
return null;
}
_handleOverridablePaintPropertyUpdate(name, oldValue, newValue) {
return false;
}
isHidden(zoom) {
if (this.minzoom && zoom < this.minzoom) return true;
if (this.maxzoom && zoom >= this.maxzoom) return true;
return this.visibility === "none";
}
updateTransitions(parameters) {
this._transitioningPaint = this._transitionablePaint.transitioned(parameters, this._transitioningPaint);
}
hasTransition() {
return this._transitioningPaint.hasTransition();
}
recalculate(parameters, availableImages) {
if (this._unevaluatedLayout) {
this.layout = this._unevaluatedLayout.possiblyEvaluate(parameters, void 0, availableImages, this.iconImageUseTheme);
}
this.paint = this._transitioningPaint.possiblyEvaluate(parameters, void 0, availableImages);
}
serialize() {
assert$1(this.type !== "custom", "Custom layers cannot be serialized");
const output = {
"id": this.id,
"type": this.type,
"slot": this.slot,
"source": this.source,
"source-layer": this.sourceLayer,
"metadata": this.metadata,
"minzoom": this.minzoom,
"maxzoom": this.maxzoom,
"filter": this.filter,
"layout": this._unevaluatedLayout && this._unevaluatedLayout.serialize(),
"paint": this._transitionablePaint && this._transitionablePaint.serialize()
};
if (this.appearances.length !== 0) {
output["appearances"] = this.appearances.map((a) => a.serialize());
}
return filterObject(output, (value, key) => {
return value !== void 0 && !(key === "layout" && !Object.keys(value).length) && !(key === "paint" && !Object.keys(value).length);
});
}
// Determines if the layer is 3D based on whether terrain is enabled.
// If 'terrainEnabled' parameter is not provided, then the function
// should return true if the layer is potentially 3D.
is3D(terrainEnabled) {
return false;
}
hasElevation() {
return false;
}
isSky() {
return false;
}
isTileClipped() {
return false;
}
hasOffscreenPass() {
return false;
}
hasShadowPass() {
return false;
}
canCastShadows() {
return false;
}
hasLightBeamPass() {
return false;
}
cutoffRange() {
return 0;
}
tileCoverLift() {
return 0;
}
resize() {
}
_clear() {
}
isStateDependent() {
for (const property in this.paint._values) {
const value = this.paint.get(property);
if (!(value instanceof PossiblyEvaluatedPropertyValue) || !supportsPropertyExpression(value.property.specification)) {
continue;
}
if ((value.value.kind === "source" || value.value.kind === "composite") && value.value.isStateDependent) {
return true;
}
}
for (const appearance of this.appearances) {
if (!isStateConstant(appearance.condition.expression)) {
return true;
}
}
return false;
}
compileFilter(options) {
if (!this._filterCompiled) {
this._featureFilter = createFilter(this.filter, this.scope, options);
this._filterCompiled = true;
}
}
invalidateCompiledFilter() {
this._filterCompiled = false;
}
dynamicFilter() {
return this._featureFilter.dynamicFilter;
}
dynamicFilterNeedsFeature() {
return this._featureFilter.needFeature;
}
getLayerRenderingStats() {
return this._stats;
}
resetLayerRenderingStats(painter) {
if (this._stats) {
if (painter.renderPass === "shadow") {
this._stats.numRenderedVerticesInShadowPass = 0;
} else {
this._stats.numRenderedVerticesInTransparentPass = 0;
}
}
}
getAppearances() {
return this.appearances;
}
queryRenderedFeatures(queryGeometry, sourceCache, transform) {
return {};
}
// @ts-expect-error - TS2355 - A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
queryRadius(_bucket) {
}
queryIntersectsFeature(_queryGeometry, _feature, _featureState, _geometry, _zoom, _transform, _pixelPosMatrix, _elevationHelper, _layoutVertexArrayOffset) {
}
}
const viewTypes = {
"Int8": Int8Array,
"Uint8": Uint8Array,
"Int16": Int16Array,
"Uint16": Uint16Array,
"Int32": Int32Array,
"Uint32": Uint32Array,
"Float32": Float32Array
};
class Struct {
/**
* @param {StructArray} structArray The StructArray the struct is stored in
* @param {number} index The index of the struct in the StructArray.
* @private
*/
constructor(structArray, index) {
this._structArray = structArray;
this._pos1 = index * this.size;
this._pos2 = this._pos1 / 2;
this._pos4 = this._pos1 / 4;
this._pos8 = this._pos1 / 8;
}
}
const DEFAULT_CAPACITY = 128;
const RESIZE_MULTIPLIER = 5;
const EMPTY_BUFFER = new ArrayBuffer(0);
class StructArray {
constructor() {
this._reallocCount = 0;
this.capacity = 0;
this.length = 0;
}
/**
* Serialize a StructArray instance. Serializes both the raw data and the
* metadata needed to reconstruct the StructArray base class during
* deserialization.
* @private
*/
static serialize(array, transferables) {
array._trim();
if (transferables && array.arrayBuffer) {
transferables.add(array.arrayBuffer);
}
return {
length: array.length,
arrayBuffer: array.arrayBuffer
};
}
static deserialize(input) {
const structArray = Object.create(this.prototype);
structArray.arrayBuffer = input.arrayBuffer;
structArray.length = input.length;
if (input.arrayBuffer) {
structArray.capacity = input.arrayBuffer.byteLength / structArray.bytesPerElement;
} else {
structArray.capacity = 0;
structArray.arrayBuffer = EMPTY_BUFFER;
}
structArray._refreshViews();
return structArray;
}
/**
* Resize the array to discard unused capacity.
*/
_trim() {
if (this.length !== this.capacity) {
this.capacity = this.length;
this.arrayBuffer = this.arrayBuffer.slice(0, this.length * this.bytesPerElement);
this._refreshViews();
}
}
/**
* Resets the the length of the array to 0 without de-allocating capacity.
*/
clear() {
this.length = 0;
}
/**
* Resize the array.
* If `n` is greater than the current length then additional elements with undefined values are added.
* If `n` is less than the current length then the array will be reduced to the first `n` elements.
* @param {number} n The new size of the array.
*/
resize(n) {
this.reserve(n);
this.length = n;
}
/**
* Indicate a planned increase in size, so that any necessary allocation may
* be done once, ahead of time.
* @param {number} n The expected size of the array.
*/
reserve(n) {
if (n > this.capacity) {
this._reallocCount++;
this.capacity = Math.max(n, Math.floor(this.capacity * RESIZE_MULTIPLIER), DEFAULT_CAPACITY);
this.arrayBuffer = new ArrayBuffer(this.capacity * this.bytesPerElement);
const oldUint8Array = this.uint8;
this._refreshViews();
if (oldUint8Array) this.uint8.set(oldUint8Array);
}
}
/**
* Indicate a planned increase in size, so that any necessary allocation may
* be done once, ahead of time.
* @param {number} n The expected number of additional elements added to the array.
*/
reserveForAdditional(n) {
this.reserve(this.length + n);
}
/**
* Create TypedArray views for the current ArrayBuffer.
*/
_refreshViews() {
throw new Error("StructArray#_refreshViews() must be implemented by each concrete StructArray layout");
}
emplace(..._) {
throw new Error("StructArray#emplace() must be implemented by each concrete StructArray layout");
}
emplaceBack(..._) {
throw new Error("StructArray#emplaceBack() must be implemented by each concrete StructArray layout");
}
destroy() {
this.int8 = this.uint8 = this.int16 = this.uint16 = this.int32 = this.uint32 = this.float32 = null;
this.arrayBuffer = null;
}
}
function createLayout(members, alignment = 1) {
let offset = 0;
let maxSize = 0;
const layoutMembers = members.map((member) => {
assert$1(member.name.length);
const typeSize = sizeOf(member.type);
const memberOffset = offset = align$1(offset, Math.max(alignment, typeSize));
const components = member.components || 1;
maxSize = Math.max(maxSize, typeSize);
offset += typeSize * components;
return {
name: member.name,
type: member.type,
components,
offset: memberOffset
};
});
const size = align$1(offset, Math.max(maxSize, alignment));
return {
members: layoutMembers,
size,
alignment
};
}
function sizeOf(type) {
return viewTypes[type].BYTES_PER_ELEMENT;
}
function align$1(offset, size) {
return Math.ceil(offset / size) * size;
}
class StructArrayLayout2i4 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1);
}
emplace(i, v0, v1) {
const o2 = i * 2;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
return i;
}
}
StructArrayLayout2i4.prototype.bytesPerElement = 4;
register(StructArrayLayout2i4, "StructArrayLayout2i4");
class StructArrayLayout3i6 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2);
}
emplace(i, v0, v1, v2) {
const o2 = i * 3;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
return i;
}
}
StructArrayLayout3i6.prototype.bytesPerElement = 6;
register(StructArrayLayout3i6, "StructArrayLayout3i6");
class StructArrayLayout4i8 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3);
}
emplace(i, v0, v1, v2, v3) {
const o2 = i * 4;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 3] = v3;
return i;
}
}
StructArrayLayout4i8.prototype.bytesPerElement = 8;
register(StructArrayLayout4i8, "StructArrayLayout4i8");
class StructArrayLayout1f4 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0);
}
emplace(i, v0) {
const o4 = i * 1;
this.float32[o4 + 0] = v0;
return i;
}
}
StructArrayLayout1f4.prototype.bytesPerElement = 4;
register(StructArrayLayout1f4, "StructArrayLayout1f4");
class StructArrayLayout2i1f8 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2);
}
emplace(i, v0, v1, v2) {
const o2 = i * 4;
const o4 = i * 2;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.float32[o4 + 1] = v2;
return i;
}
}
StructArrayLayout2i1f8.prototype.bytesPerElement = 8;
register(StructArrayLayout2i1f8, "StructArrayLayout2i1f8");
class StructArrayLayout3i8 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2);
}
emplace(i, v0, v1, v2) {
const o2 = i * 4;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
return i;
}
}
StructArrayLayout3i8.prototype.bytesPerElement = 8;
register(StructArrayLayout3i8, "StructArrayLayout3i8");
class StructArrayLayout5i10 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4);
}
emplace(i, v0, v1, v2, v3, v4) {
const o2 = i * 5;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 3] = v3;
this.int16[o2 + 4] = v4;
return i;
}
}
StructArrayLayout5i10.prototype.bytesPerElement = 10;
register(StructArrayLayout5i10, "StructArrayLayout5i10");
class StructArrayLayout2i4ub1f12 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6) {
const o2 = i * 6;
const o1 = i * 12;
const o4 = i * 3;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.uint8[o1 + 4] = v2;
this.uint8[o1 + 5] = v3;
this.uint8[o1 + 6] = v4;
this.uint8[o1 + 7] = v5;
this.float32[o4 + 2] = v6;
return i;
}
}
StructArrayLayout2i4ub1f12.prototype.bytesPerElement = 12;
register(StructArrayLayout2i4ub1f12, "StructArrayLayout2i4ub1f12");
class StructArrayLayout3f12 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2);
}
emplace(i, v0, v1, v2) {
const o4 = i * 3;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
return i;
}
}
StructArrayLayout3f12.prototype.bytesPerElement = 12;
register(StructArrayLayout3f12, "StructArrayLayout3f12");
class StructArrayLayout4ui1f12 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4);
}
emplace(i, v0, v1, v2, v3, v4) {
const o2 = i * 6;
const o4 = i * 3;
this.uint16[o2 + 0] = v0;
this.uint16[o2 + 1] = v1;
this.uint16[o2 + 2] = v2;
this.uint16[o2 + 3] = v3;
this.float32[o4 + 2] = v4;
return i;
}
}
StructArrayLayout4ui1f12.prototype.bytesPerElement = 12;
register(StructArrayLayout4ui1f12, "StructArrayLayout4ui1f12");
class StructArrayLayout4ui8 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3);
}
emplace(i, v0, v1, v2, v3) {
const o2 = i * 4;
this.uint16[o2 + 0] = v0;
this.uint16[o2 + 1] = v1;
this.uint16[o2 + 2] = v2;
this.uint16[o2 + 3] = v3;
return i;
}
}
StructArrayLayout4ui8.prototype.bytesPerElement = 8;
register(StructArrayLayout4ui8, "StructArrayLayout4ui8");
class StructArrayLayout6i12 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5);
}
emplace(i, v0, v1, v2, v3, v4, v5) {
const o2 = i * 6;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 3] = v3;
this.int16[o2 + 4] = v4;
this.int16[o2 + 5] = v5;
return i;
}
}
StructArrayLayout6i12.prototype.bytesPerElement = 12;
register(StructArrayLayout6i12, "StructArrayLayout6i12");
class StructArrayLayout4i4ui4i24 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) {
const o2 = i * 12;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 3] = v3;
this.uint16[o2 + 4] = v4;
this.uint16[o2 + 5] = v5;
this.uint16[o2 + 6] = v6;
this.uint16[o2 + 7] = v7;
this.int16[o2 + 8] = v8;
this.int16[o2 + 9] = v9;
this.int16[o2 + 10] = v10;
this.int16[o2 + 11] = v11;
return i;
}
}
StructArrayLayout4i4ui4i24.prototype.bytesPerElement = 24;
register(StructArrayLayout4i4ui4i24, "StructArrayLayout4i4ui4i24");
class StructArrayLayout3i3f20 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5);
}
emplace(i, v0, v1, v2, v3, v4, v5) {
const o2 = i * 10;
const o4 = i * 5;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.float32[o4 + 2] = v3;
this.float32[o4 + 3] = v4;
this.float32[o4 + 4] = v5;
return i;
}
}
StructArrayLayout3i3f20.prototype.bytesPerElement = 20;
register(StructArrayLayout3i3f20, "StructArrayLayout3i3f20");
class StructArrayLayout4f16 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3);
}
emplace(i, v0, v1, v2, v3) {
const o4 = i * 4;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
return i;
}
}
StructArrayLayout4f16.prototype.bytesPerElement = 16;
register(StructArrayLayout4f16, "StructArrayLayout4f16");
class StructArrayLayout1ul4 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint32 = new Uint32Array(this.arrayBuffer);
}
emplaceBack(v0) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0);
}
emplace(i, v0) {
const o4 = i * 1;
this.uint32[o4 + 0] = v0;
return i;
}
}
StructArrayLayout1ul4.prototype.bytesPerElement = 4;
register(StructArrayLayout1ul4, "StructArrayLayout1ul4");
class StructArrayLayout2ui4 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1);
}
emplace(i, v0, v1) {
const o2 = i * 2;
this.uint16[o2 + 0] = v0;
this.uint16[o2 + 1] = v1;
return i;
}
}
StructArrayLayout2ui4.prototype.bytesPerElement = 4;
register(StructArrayLayout2ui4, "StructArrayLayout2ui4");
class StructArrayLayout5i4f1i1ul2ui40 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
this.uint32 = new Uint32Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) {
const o2 = i * 20;
const o4 = i * 10;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 3] = v3;
this.int16[o2 + 4] = v4;
this.float32[o4 + 3] = v5;
this.float32[o4 + 4] = v6;
this.float32[o4 + 5] = v7;
this.float32[o4 + 6] = v8;
this.int16[o2 + 14] = v9;
this.uint32[o4 + 8] = v10;
this.uint16[o2 + 18] = v11;
this.uint16[o2 + 19] = v12;
return i;
}
}
StructArrayLayout5i4f1i1ul2ui40.prototype.bytesPerElement = 40;
register(StructArrayLayout5i4f1i1ul2ui40, "StructArrayLayout5i4f1i1ul2ui40");
class StructArrayLayout3i2i2i16 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6) {
const o2 = i * 8;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.int16[o2 + 4] = v3;
this.int16[o2 + 5] = v4;
this.int16[o2 + 6] = v5;
this.int16[o2 + 7] = v6;
return i;
}
}
StructArrayLayout3i2i2i16.prototype.bytesPerElement = 16;
register(StructArrayLayout3i2i2i16, "StructArrayLayout3i2i2i16");
class StructArrayLayout2f1f2i16 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4);
}
emplace(i, v0, v1, v2, v3, v4) {
const o4 = i * 4;
const o2 = i * 8;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.int16[o2 + 6] = v3;
this.int16[o2 + 7] = v4;
return i;
}
}
StructArrayLayout2f1f2i16.prototype.bytesPerElement = 16;
register(StructArrayLayout2f1f2i16, "StructArrayLayout2f1f2i16");
class StructArrayLayout2ub4f20 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5);
}
emplace(i, v0, v1, v2, v3, v4, v5) {
const o1 = i * 20;
const o4 = i * 5;
this.uint8[o1 + 0] = v0;
this.uint8[o1 + 1] = v1;
this.float32[o4 + 1] = v2;
this.float32[o4 + 2] = v3;
this.float32[o4 + 3] = v4;
this.float32[o4 + 4] = v5;
return i;
}
}
StructArrayLayout2ub4f20.prototype.bytesPerElement = 20;
register(StructArrayLayout2ub4f20, "StructArrayLayout2ub4f20");
class StructArrayLayout3ui6 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2);
}
emplace(i, v0, v1, v2) {
const o2 = i * 3;
this.uint16[o2 + 0] = v0;
this.uint16[o2 + 1] = v1;
this.uint16[o2 + 2] = v2;
return i;
}
}
StructArrayLayout3ui6.prototype.bytesPerElement = 6;
register(StructArrayLayout3ui6, "StructArrayLayout3ui6");
class StructArrayLayout3i2f2ui3ul3ui2f3ub1ul1i1ub60 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
this.uint32 = new Uint32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) {
const o2 = i * 30;
const o4 = i * 15;
const o1 = i * 60;
this.int16[o2 + 0] = v0;
this.int16[o2 + 1] = v1;
this.int16[o2 + 2] = v2;
this.float32[o4 + 2] = v3;
this.float32[o4 + 3] = v4;
this.uint16[o2 + 8] = v5;
this.uint16[o2 + 9] = v6;
this.uint32[o4 + 5] = v7;
this.uint32[o4 + 6] = v8;
this.uint32[o4 + 7] = v9;
this.uint16[o2 + 16] = v10;
this.uint16[o2 + 17] = v11;
this.uint16[o2 + 18] = v12;
this.float32[o4 + 10] = v13;
this.float32[o4 + 11] = v14;
this.uint8[o1 + 48] = v15;
this.uint8[o1 + 49] = v16;
this.uint8[o1 + 50] = v17;
this.uint32[o4 + 13] = v18;
this.int16[o2 + 28] = v19;
this.uint8[o1 + 58] = v20;
return i;
}
}
StructArrayLayout3i2f2ui3ul3ui2f3ub1ul1i1ub60.prototype.bytesPerElement = 60;
register(StructArrayLayout3i2f2ui3ul3ui2f3ub1ul1i1ub60, "StructArrayLayout3i2f2ui3ul3ui2f3ub1ul1i1ub60");
class StructArrayLayout2f9i15ui1ul4f1ub1ui80 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
this.uint32 = new Uint32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) {
const o4 = i * 20;
const o2 = i * 40;
const o1 = i * 80;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.int16[o2 + 4] = v2;
this.int16[o2 + 5] = v3;
this.int16[o2 + 6] = v4;
this.int16[o2 + 7] = v5;
this.int16[o2 + 8] = v6;
this.int16[o2 + 9] = v7;
this.int16[o2 + 10] = v8;
this.int16[o2 + 11] = v9;
this.int16[o2 + 12] = v10;
this.uint16[o2 + 13] = v11;
this.uint16[o2 + 14] = v12;
this.uint16[o2 + 15] = v13;
this.uint16[o2 + 16] = v14;
this.uint16[o2 + 17] = v15;
this.uint16[o2 + 18] = v16;
this.uint16[o2 + 19] = v17;
this.uint16[o2 + 20] = v18;
this.uint16[o2 + 21] = v19;
this.uint16[o2 + 22] = v20;
this.uint16[o2 + 23] = v21;
this.uint16[o2 + 24] = v22;
this.uint16[o2 + 25] = v23;
this.uint16[o2 + 26] = v24;
this.uint16[o2 + 27] = v25;
this.uint32[o4 + 14] = v26;
this.float32[o4 + 15] = v27;
this.float32[o4 + 16] = v28;
this.float32[o4 + 17] = v29;
this.float32[o4 + 18] = v30;
this.uint8[o1 + 76] = v31;
this.uint16[o2 + 39] = v32;
return i;
}
}
StructArrayLayout2f9i15ui1ul4f1ub1ui80.prototype.bytesPerElement = 80;
register(StructArrayLayout2f9i15ui1ul4f1ub1ui80, "StructArrayLayout2f9i15ui1ul4f1ub1ui80");
class StructArrayLayout6f24 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5);
}
emplace(i, v0, v1, v2, v3, v4, v5) {
const o4 = i * 6;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
this.float32[o4 + 5] = v5;
return i;
}
}
StructArrayLayout6f24.prototype.bytesPerElement = 24;
register(StructArrayLayout6f24, "StructArrayLayout6f24");
class StructArrayLayout5f20 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4);
}
emplace(i, v0, v1, v2, v3, v4) {
const o4 = i * 5;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
return i;
}
}
StructArrayLayout5f20.prototype.bytesPerElement = 20;
register(StructArrayLayout5f20, "StructArrayLayout5f20");
class StructArrayLayout7f28 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6) {
const o4 = i * 7;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
this.float32[o4 + 5] = v5;
this.float32[o4 + 6] = v6;
return i;
}
}
StructArrayLayout7f28.prototype.bytesPerElement = 28;
register(StructArrayLayout7f28, "StructArrayLayout7f28");
class StructArrayLayout11f44 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) {
const o4 = i * 11;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
this.float32[o4 + 5] = v5;
this.float32[o4 + 6] = v6;
this.float32[o4 + 7] = v7;
this.float32[o4 + 8] = v8;
this.float32[o4 + 9] = v9;
this.float32[o4 + 10] = v10;
return i;
}
}
StructArrayLayout11f44.prototype.bytesPerElement = 44;
register(StructArrayLayout11f44, "StructArrayLayout11f44");
class StructArrayLayout9f36 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8) {
const o4 = i * 9;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
this.float32[o4 + 5] = v5;
this.float32[o4 + 6] = v6;
this.float32[o4 + 7] = v7;
this.float32[o4 + 8] = v8;
return i;
}
}
StructArrayLayout9f36.prototype.bytesPerElement = 36;
register(StructArrayLayout9f36, "StructArrayLayout9f36");
class StructArrayLayout2f8 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1);
}
emplace(i, v0, v1) {
const o4 = i * 2;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
return i;
}
}
StructArrayLayout2f8.prototype.bytesPerElement = 8;
register(StructArrayLayout2f8, "StructArrayLayout2f8");
class StructArrayLayout1ul3ui12 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint32 = new Uint32Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3);
}
emplace(i, v0, v1, v2, v3) {
const o4 = i * 3;
const o2 = i * 6;
this.uint32[o4 + 0] = v0;
this.uint16[o2 + 2] = v1;
this.uint16[o2 + 3] = v2;
this.uint16[o2 + 4] = v3;
return i;
}
}
StructArrayLayout1ul3ui12.prototype.bytesPerElement = 12;
register(StructArrayLayout1ul3ui12, "StructArrayLayout1ul3ui12");
class StructArrayLayout1ui2 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
}
emplaceBack(v0) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0);
}
emplace(i, v0) {
const o2 = i * 1;
this.uint16[o2 + 0] = v0;
return i;
}
}
StructArrayLayout1ui2.prototype.bytesPerElement = 2;
register(StructArrayLayout1ui2, "StructArrayLayout1ui2");
class StructArrayLayout16f64 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
const o4 = i * 16;
this.float32[o4 + 0] = v0;
this.float32[o4 + 1] = v1;
this.float32[o4 + 2] = v2;
this.float32[o4 + 3] = v3;
this.float32[o4 + 4] = v4;
this.float32[o4 + 5] = v5;
this.float32[o4 + 6] = v6;
this.float32[o4 + 7] = v7;
this.float32[o4 + 8] = v8;
this.float32[o4 + 9] = v9;
this.float32[o4 + 10] = v10;
this.float32[o4 + 11] = v11;
this.float32[o4 + 12] = v12;
this.float32[o4 + 13] = v13;
this.float32[o4 + 14] = v14;
this.float32[o4 + 15] = v15;
return i;
}
}
StructArrayLayout16f64.prototype.bytesPerElement = 64;
register(StructArrayLayout16f64, "StructArrayLayout16f64");
class StructArrayLayout4ui3f20 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.uint16 = new Uint16Array(this.arrayBuffer);
this.float32 = new Float32Array(this.arrayBuffer);
}
emplaceBack(v0, v1, v2, v3, v4, v5, v6) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0, v1, v2, v3, v4, v5, v6);
}
emplace(i, v0, v1, v2, v3, v4, v5, v6) {
const o2 = i * 10;
const o4 = i * 5;
this.uint16[o2 + 0] = v0;
this.uint16[o2 + 1] = v1;
this.uint16[o2 + 2] = v2;
this.uint16[o2 + 3] = v3;
this.float32[o4 + 2] = v4;
this.float32[o4 + 3] = v5;
this.float32[o4 + 4] = v6;
return i;
}
}
StructArrayLayout4ui3f20.prototype.bytesPerElement = 20;
register(StructArrayLayout4ui3f20, "StructArrayLayout4ui3f20");
class StructArrayLayout1i2 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
this.int16 = new Int16Array(this.arrayBuffer);
}
emplaceBack(v0) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0);
}
emplace(i, v0) {
const o2 = i * 1;
this.int16[o2 + 0] = v0;
return i;
}
}
StructArrayLayout1i2.prototype.bytesPerElement = 2;
register(StructArrayLayout1i2, "StructArrayLayout1i2");
class StructArrayLayout1ub1 extends StructArray {
_refreshViews() {
this.uint8 = new Uint8Array(this.arrayBuffer);
}
emplaceBack(v0) {
const i = this.length;
this.resize(i + 1);
return this.emplace(i, v0);
}
emplace(i, v0) {
const o1 = i * 1;
this.uint8[o1 + 0] = v0;
return i;
}
}
StructArrayLayout1ub1.prototype.bytesPerElement = 1;
register(StructArrayLayout1ub1, "StructArrayLayout1ub1");
class CollisionBoxStruct extends Struct {
get projectedAnchorX() {
return this._structArray.int16[this._pos2 + 0];
}
get projectedAnchorY() {
return this._structArray.int16[this._pos2 + 1];
}
get projectedAnchorZ() {
return this._structArray.int16[this._pos2 + 2];
}
get tileAnchorX() {
return this._structArray.int16[this._pos2 + 3];
}
get tileAnchorY() {
return this._structArray.int16[this._pos2 + 4];
}
get x1() {
return this._structArray.float32[this._pos4 + 3];
}
get y1() {
return this._structArray.float32[this._pos4 + 4];
}
get x2() {
return this._structArray.float32[this._pos4 + 5];
}
get y2() {
return this._structArray.float32[this._pos4 + 6];
}
get padding() {
return this._structArray.int16[this._pos2 + 14];
}
get featureIndex() {
return this._structArray.uint32[this._pos4 + 8];
}
get sourceLayerIndex() {
return this._structArray.uint16[this._pos2 + 18];
}
get bucketIndex() {
return this._structArray.uint16[this._pos2 + 19];
}
}
CollisionBoxStruct.prototype.size = 40;
class CollisionBoxArray extends StructArrayLayout5i4f1i1ul2ui40 {
/**
* Return the CollisionBoxStruct at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index) {
assert$1(index >= 0);
assert$1(index < this.length);
return new CollisionBoxStruct(this, index);
}
}
register(CollisionBoxArray, "CollisionBoxArray");
class PlacedSymbolStruct extends Struct {
get projectedAnchorX() {
return this._structArray.int16[this._pos2 + 0];
}
get projectedAnchorY() {
return this._structArray.int16[this._pos2 + 1];
}
get projectedAnchorZ() {
return this._structArray.int16[this._pos2 + 2];
}
get tileAnchorX() {
return this._structArray.float32[this._pos4 + 2];
}
get tileAnchorY() {
return this._structArray.float32[this._pos4 + 3];
}
get glyphStartIndex() {
return this._structArray.uint16[this._pos2 + 8];
}
get numGlyphs() {
return this._structArray.uint16[this._pos2 + 9];
}
get vertexStartIndex() {
return this._structArray.uint32[this._pos4 + 5];
}
get lineStartIndex() {
return this._structArray.uint32[this._pos4 + 6];
}
get lineLength() {
return this._structArray.uint32[this._pos4 + 7];
}
get segment() {
return this._structArray.uint16[this._pos2 + 16];
}
get lowerSize() {
return this._structArray.uint16[this._pos2 + 17];
}
get upperSize() {
return this._structArray.uint16[this._pos2 + 18];
}
get lineOffsetX() {
return this._structArray.float32[this._pos4 + 10];
}
get lineOffsetY() {
return this._structArray.float32[this._pos4 + 11];
}
get writingMode() {
return this._structArray.uint8[this._pos1 + 48];
}
get placedOrientation() {
return this._structArray.uint8[this._pos1 + 49];
}
set placedOrientation(x) {
this._structArray.uint8[this._pos1 + 49] = x;
}
get hidden() {
return this._structArray.uint8[this._pos1 + 50];
}
set hidden(x) {
this._structArray.uint8[this._pos1 + 50] = x;
}
get crossTileID() {
return this._structArray.uint32[this._pos4 + 13];
}
set crossTileID(x) {
this._structArray.uint32[this._pos4 + 13] = x;
}
get associatedIconIndex() {
return this._structArray.int16[this._pos2 + 28];
}
get flipState() {
return this._structArray.uint8[this._pos1 + 58];
}
set flipState(x) {
this._structArray.uint8[this._pos1 + 58] = x;
}
}
PlacedSymbolStruct.prototype.size = 60;
class PlacedSymbolArray extends StructArrayLayout3i2f2ui3ul3ui2f3ub1ul1i1ub60 {
/**
* Return the PlacedSymbolStruct at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index) {
assert$1(index >= 0);
assert$1(index < this.length);
return new PlacedSymbolStruct(this, index);
}
}
register(PlacedSymbolArray, "PlacedSymbolArray");
class SymbolInstanceStruct extends Struct {
get tileAnchorX() {
return this._structArray.float32[this._pos4 + 0];
}
get tileAnchorY() {
return this._structArray.float32[this._pos4 + 1];
}
get projectedAnchorX() {
return this._structArray.int16[this._pos2 + 4];
}
get projectedAnchorY() {
return this._structArray.int16[this._pos2 + 5];
}
get projectedAnchorZ() {
return this._structArray.int16[this._pos2 + 6];
}
get rightJustifiedTextSymbolIndex() {
return this._structArray.int16[this._pos2 + 7];
}
get centerJustifiedTextSymbolIndex() {
return this._structArray.int16[this._pos2 + 8];
}
get leftJustifiedTextSymbolIndex() {
return this._structArray.int16[this._pos2 + 9];
}
get verticalPlacedTextSymbolIndex() {
return this._structArray.int16[this._pos2 + 10];
}
get placedIconSymbolIndex() {
return this._structArray.int16[this._pos2 + 11];
}
get verticalPlacedIconSymbolIndex() {
return this._structArray.int16[this._pos2 + 12];
}
get key() {
return this._structArray.uint16[this._pos2 + 13];
}
get textBoxStartIndex() {
return this._structArray.uint16[this._pos2 + 14];
}
get textBoxEndIndex() {
return this._structArray.uint16[this._pos2 + 15];
}
get verticalTextBoxStartIndex() {
return this._structArray.uint16[this._pos2 + 16];
}
get verticalTextBoxEndIndex() {
return this._structArray.uint16[this._pos2 + 17];
}
get iconBoxStartIndex() {
return this._structArray.uint16[this._pos2 + 18];
}
get iconBoxEndIndex() {
return this._structArray.uint16[this._pos2 + 19];
}
get verticalIconBoxStartIndex() {
return this._structArray.uint16[this._pos2 + 20];
}
get verticalIconBoxEndIndex() {
return this._structArray.uint16[this._pos2 + 21];
}
get featureIndex() {
return this._structArray.uint16[this._pos2 + 22];
}
get numHorizontalGlyphVertices() {
return this._structArray.uint16[this._pos2 + 23];
}
get numVerticalGlyphVertices() {
return this._structArray.uint16[this._pos2 + 24];
}
get numIconVertices() {
return this._structArray.uint16[this._pos2 + 25];
}
get numVerticalIconVertices() {
return this._structArray.uint16[this._pos2 + 26];
}
get useRuntimeCollisionCircles() {
return this._structArray.uint16[this._pos2 + 27];
}
get crossTileID() {
return this._structArray.uint32[this._pos4 + 14];
}
set crossTileID(x) {
this._structArray.uint32[this._pos4 + 14] = x;
}
get textOffset0() {
return this._structArray.float32[this._pos4 + 15];
}
get textOffset1() {
return this._structArray.float32[this._pos4 + 16];
}
get collisionCircleDiameter() {
return this._structArray.float32[this._pos4 + 17];
}
get zOffset() {
return this._structArray.float32[this._pos4 + 18];
}
set zOffset(x) {
this._structArray.float32[this._pos4 + 18] = x;
}
get hasIconTextFit() {
return this._structArray.uint8[this._pos1 + 76];
}
get elevationFeatureIndex() {
return this._structArray.uint16[this._pos2 + 39];
}
}
SymbolInstanceStruct.prototype.size = 80;
class SymbolInstanceArray extends StructArrayLayout2f9i15ui1ul4f1ub1ui80 {
/**
* Return the SymbolInstanceStruct at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index) {
assert$1(index >= 0);
assert$1(index < this.length);
return new SymbolInstanceStruct(this, index);
}
}
register(SymbolInstanceArray, "SymbolInstanceArray");
class GlyphOffsetArray extends StructArrayLayout1f4 {
getoffsetX(index) {
return this.float32[index * 1 + 0];
}
}
register(GlyphOffsetArray, "GlyphOffsetArray");
class SymbolLineVertexArray extends StructArrayLayout2i4 {
getx(index) {
return this.int16[index * 2 + 0];
}
gety(index) {
return this.int16[index * 2 + 1];
}
}
register(SymbolLineVertexArray, "SymbolLineVertexArray");
class FeatureIndexStruct extends Struct {
get featureIndex() {
return this._structArray.uint32[this._pos4 + 0];
}
get sourceLayerIndex() {
return this._structArray.uint16[this._pos2 + 2];
}
get bucketIndex() {
return this._structArray.uint16[this._pos2 + 3];
}
get layoutVertexArrayOffset() {
return this._structArray.uint16[this._pos2 + 4];
}
}
FeatureIndexStruct.prototype.size = 12;
class FeatureIndexArray extends StructArrayLayout1ul3ui12 {
/**
* Return the FeatureIndexStruct at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index) {
assert$1(index >= 0);
assert$1(index < this.length);
return new FeatureIndexStruct(this, index);
}
}
register(FeatureIndexArray, "FeatureIndexArray");
class FillExtrusionCentroidArray extends StructArrayLayout2ui4 {
geta_centroid_pos0(index) {
return this.uint16[index * 2 + 0];
}
geta_centroid_pos1(index) {
return this.uint16[index * 2 + 1];
}
}
register(FillExtrusionCentroidArray, "FillExtrusionCentroidArray");
class FillExtrusionWallStruct extends Struct {
get a_join_normal_inside0() {
return this._structArray.int16[this._pos2 + 0];
}
get a_join_normal_inside1() {
return this._structArray.int16[this._pos2 + 1];
}
get a_join_normal_inside2() {
return this._structArray.int16[this._pos2 + 2];
}
}
FillExtrusionWallStruct.prototype.size = 6;
class FillExtrusionWallArray extends StructArrayLayout3i6 {
/**
* Return the FillExtrusionWallStruct at the given location in the array.
* @param {number} index The index of the element.
* @private
*/
get(index) {
assert$1(index >= 0);
assert$1(index < this.length);
return new FillExtrusionWallStruct(this, index);
}
}
register(FillExtrusionWallArray, "FillExtrusionWallArray");
const circleAttributes = createLayout([
{ name: "a_pos", components: 2, type: "Int16" }
], 4);
const circleAttributesExt = createLayout([
{ name: "a_circle_z_offset", components: 1, type: "Float32" }
], 4);
const circleGlobeAttributesExt = createLayout([
{ name: "a_pos_3", components: 3, type: "Int16" },
{ name: "a_pos_normal_3", components: 3, type: "Int16" }
]);
const { members: members$8, size: size$8, alignment: alignment$8 } = circleAttributes;
class SegmentVector {
constructor(segments = []) {
this.segments = segments;
}
_prepareSegment(numVertices, vertexArrayLength, indexArrayLength, sortKey) {
let segment = this.segments[this.segments.length - 1];
if (numVertices > SegmentVector.MAX_VERTEX_ARRAY_LENGTH) warnOnce(`Max vertices per segment is ${SegmentVector.MAX_VERTEX_ARRAY_LENGTH}: bucket requested ${numVertices}`);
if (!segment || segment.vertexLength + numVertices > SegmentVector.MAX_VERTEX_ARRAY_LENGTH || segment.sortKey !== sortKey) {
segment = {
vertexOffset: vertexArrayLength,
primitiveOffset: indexArrayLength,
vertexLength: 0,
primitiveLength: 0
};
if (sortKey !== void 0) segment.sortKey = sortKey;
this.segments.push(segment);
}
return segment;
}
prepareSegment(numVertices, layoutVertexArray, indexArray, sortKey) {
return this._prepareSegment(numVertices, layoutVertexArray.length, indexArray.length, sortKey);
}
get() {
return this.segments;
}
destroy() {
for (const segment of this.segments) {
for (const k in segment.vaos) {
segment.vaos[k].destroy();
}
}
}
static simpleSegment(vertexOffset, primitiveOffset, vertexLength, primitiveLength) {
return new SegmentVector([{
vertexOffset,
primitiveOffset,
vertexLength,
primitiveLength,
vaos: {},
sortKey: 0
}]);
}
}
SegmentVector.MAX_VERTEX_ARRAY_LENGTH = Math.pow(2, 16) - 1;
register(SegmentVector, "SegmentVector");
function packUint8ToFloat(a, b) {
a = clamp(Math.floor(a), 0, 255);
b = clamp(Math.floor(b), 0, 255);
return 256 * a + b;
}
const patternAttributes = createLayout([
// [tl.x, tl.y, br.x, br.y]
{ name: "a_pattern", components: 4, type: "Uint16" },
{ name: "a_pixel_ratio", components: 1, type: "Float32" }
]);
const patternTransitionAttributes = createLayout([
// [tl.x, tl.y, br.x, br.y]
{ name: "a_pattern_b", components: 4, type: "Uint16" }
]);
const dashAttributes = createLayout([
{ name: "a_dash", components: 4, type: "Uint16" }
// [x, y, width, unused]
]);
class FeaturePositionMap {
constructor() {
this.ids = [];
this.uniqueIds = [];
this.positions = [];
this.indexed = false;
}
add(id, index, start, end) {
this.ids.push(getNumericId(id));
this.positions.push(index, start, end);
}
eachPosition(id, fn) {
assert$1(this.indexed);
const intId = getNumericId(id);
let i = 0;
let j = this.ids.length - 1;
while (i < j) {
const m = i + j >> 1;
if (this.ids[m] >= intId) {
j = m;
} else {
i = m + 1;
}
}
while (this.ids[i] === intId) {
const index = this.positions[3 * i];
const start = this.positions[3 * i + 1];
const end = this.positions[3 * i + 2];
fn(index, start, end);
i++;
}
}
static serialize(map, transferables) {
const ids = new Float64Array(map.ids);
const positions = new Uint32Array(map.positions);
sort$1(ids, positions, 0, ids.length - 1);
if (transferables) {
transferables.add(ids.buffer);
transferables.add(positions.buffer);
}
return { ids, positions };
}
static deserialize(obj) {
const map = new FeaturePositionMap();
map.ids = obj.ids;
map.positions = obj.positions;
let prev;
for (const id of map.ids) {
if (id !== prev) map.uniqueIds.push(id);
prev = id;
}
map.indexed = true;
return map;
}
}
function getNumericId(value) {
const numValue = +value;
if (Number.isSafeInteger(numValue)) {
return numValue;
}
return murmur3(String(value));
}
function sort$1(ids, positions, left, right) {
while (left < right) {
const pivot = ids[left + right >> 1];
let i = left - 1;
let j = right + 1;
while (true) {
do
i++;
while (ids[i] < pivot);
do
j--;
while (ids[j] > pivot);
if (i >= j) break;
swap$1(ids, i, j);
swap$1(positions, 3 * i, 3 * j);
swap$1(positions, 3 * i + 1, 3 * j + 1);
swap$1(positions, 3 * i + 2, 3 * j + 2);
}
if (j - left < right - j) {
sort$1(ids, positions, left, j);
left = j + 1;
} else {
sort$1(ids, positions, j + 1, right);
right = j;
}
}
}
function swap$1(arr, i, j) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
register(FeaturePositionMap, "FeaturePositionMap");
class Uniform {
constructor(context) {
this.gl = context.gl;
this.initialized = false;
}
fetchUniformLocation(program, name) {
if (!this.location && !this.initialized) {
this.location = this.gl.getUniformLocation(program, name);
this.initialized = true;
}
return !!this.location;
}
set(_program, _name, _v) {
throw new Error("Uniform#set() must be implemented by each concrete Uniform");
}
}
class Uniform1i extends Uniform {
constructor(context) {
super(context);
this.current = 0;
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (this.current !== v) {
this.current = v;
this.gl.uniform1i(this.location, v);
}
}
}
class Uniform1f extends Uniform {
constructor(context) {
super(context);
this.current = 0;
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (this.current !== v) {
this.current = v;
this.gl.uniform1f(this.location, v);
}
}
}
class Uniform2f extends Uniform {
constructor(context) {
super(context);
this.current = [0, 0];
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (v[0] !== this.current[0] || v[1] !== this.current[1]) {
this.current = v;
this.gl.uniform2f(this.location, v[0], v[1]);
}
}
}
class Uniform3f extends Uniform {
constructor(context) {
super(context);
this.current = [0, 0, 0];
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (v[0] !== this.current[0] || v[1] !== this.current[1] || v[2] !== this.current[2]) {
this.current = v;
this.gl.uniform3f(this.location, v[0], v[1], v[2]);
}
}
}
class Uniform4f extends Uniform {
constructor(context) {
super(context);
this.current = [0, 0, 0, 0];
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (v[0] !== this.current[0] || v[1] !== this.current[1] || v[2] !== this.current[2] || v[3] !== this.current[3]) {
this.current = v;
this.gl.uniform4f(this.location, v[0], v[1], v[2], v[3]);
}
}
}
class UniformColor extends Uniform {
constructor(context) {
super(context);
this.current = Color.transparent.toPremultipliedRenderColor(null);
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (v.r !== this.current.r || v.g !== this.current.g || v.b !== this.current.b || v.a !== this.current.a) {
this.current = v;
this.gl.uniform4f(this.location, v.r, v.g, v.b, v.a);
}
}
}
const emptyMat4 = new Float32Array(16);
class UniformMatrix4f extends Uniform {
constructor(context) {
super(context);
this.current = emptyMat4;
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
if (v[12] !== this.current[12] || v[0] !== this.current[0]) {
this.current = v;
this.gl.uniformMatrix4fv(this.location, false, v);
return;
}
for (let i = 1; i < 16; i++) {
if (v[i] !== this.current[i]) {
this.current = v;
this.gl.uniformMatrix4fv(this.location, false, v);
break;
}
}
}
}
const emptyMat3 = new Float32Array(9);
class UniformMatrix3f extends Uniform {
constructor(context) {
super(context);
this.current = emptyMat3;
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
for (let i = 0; i < 9; i++) {
if (v[i] !== this.current[i]) {
this.current = v;
this.gl.uniformMatrix3fv(this.location, false, v);
break;
}
}
}
}
const emptyMat2 = new Float32Array(4);
class UniformMatrix2f extends Uniform {
constructor(context) {
super(context);
this.current = emptyMat2;
}
set(program, name, v) {
if (!this.fetchUniformLocation(program, name)) return;
for (let i = 0; i < 4; i++) {
if (v[i] !== this.current[i]) {
this.current = v;
this.gl.uniformMatrix2fv(this.location, false, v);
break;
}
}
}
}
function packColor(color) {
return [
packUint8ToFloat(255 * color.r, 255 * color.g),
packUint8ToFloat(255 * color.b, 255 * color.a)
];
}
function shouldIgnoreLut(lutExpression, feature, featureState, availableImages, canonical, brightness, formattedSection, worldview) {
if (!lutExpression) return false;
if (lutExpression.kind === "composite" || lutExpression.kind === "source") {
const value = lutExpression.evaluate(new EvaluationParameters(0, { brightness, worldview }), feature, featureState, canonical, availableImages, formattedSection);
return value === "none";
}
return lutExpression.value === "none";
}
class ConstantBinder {
constructor(value, names, type, context) {
this.value = value;
this.uniformNames = names.map((name) => `u_${name}`);
this.type = type;
this.context = context;
}
setUniform(program, uniform, globals, currentValue, uniformName) {
const value = currentValue.constantOr(this.value);
if (value instanceof Color) {
const lut = this.lutExpression && this.lutExpression.kind === "constant" && this.lutExpression.value === "none" ? null : this.context.lut;
uniform.set(program, uniformName, value.toPremultipliedRenderColor(lut));
} else {
uniform.set(program, uniformName, value);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getBinding(context, _) {
return this.type === "color" ? new UniformColor(context) : new Uniform1f(context);
}
}
class PatternConstantBinder {
constructor(value, names) {
this.uniformNames = names.map((name) => `u_${name}`);
this.pattern = null;
this.patternTransition = null;
this.pixelRatio = 1;
}
setConstantPatternPositions(primaryPosTo, secondaryPosTo) {
this.pixelRatio = primaryPosTo.pixelRatio || 1;
this.pattern = primaryPosTo.tl.concat(primaryPosTo.br);
this.patternTransition = secondaryPosTo ? secondaryPosTo.tl.concat(secondaryPosTo.br) : this.pattern;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setUniform(program, uniform, globals, currentValue, uniformName) {
let pos = null;
if (uniformName === "u_pattern" || uniformName === "u_dash") {
pos = this.pattern;
}
if (uniformName === "u_pattern_b") {
pos = this.patternTransition;
}
if (uniformName === "u_pixel_ratio") {
pos = this.pixelRatio;
}
if (pos) uniform.set(program, uniformName, pos);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getBinding(context, name) {
return name === "u_pattern" || name === "u_pattern_b" || name === "u_dash" ? new Uniform4f(context) : new Uniform1f(context);
}
}
class SourceExpressionBinder {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(expression, names, type, PaintVertexArray) {
this.expression = expression;
this.type = type;
this.maxValue = 0;
this.paintVertexAttributes = names.map((name) => ({
name: `a_${name}`,
type: "Float32",
components: type === "color" ? 2 : 1,
offset: 0
}));
this.paintVertexArray = new PaintVertexArray();
}
populatePaintArray(newLength, feature, imagePositions, availableImages, canonical, brightness, formattedSection, worldview) {
const start = this.paintVertexArray.length;
assert$1(Array.isArray(availableImages));
const value = this.expression.kind === "composite" || this.expression.kind === "source" ? this.expression.evaluate(new EvaluationParameters(0, { brightness, worldview }), feature, {}, canonical, availableImages, formattedSection) : this.expression.kind === "constant" && this.expression.value;
const ignoreLut = shouldIgnoreLut(this.lutExpression, feature, {}, availableImages, canonical, brightness, formattedSection, worldview);
this.paintVertexArray.resize(newLength);
this._setPaintValue(start, newLength, value, ignoreLut ? null : this.context.lut);
}
updatePaintArray(start, end, feature, featureState, availableImages, spritePositions, brightness, worldview) {
const value = this.expression.kind === "composite" || this.expression.kind === "source" ? this.expression.evaluate({ zoom: 0, brightness, worldview }, feature, featureState, void 0, availableImages) : this.expression.kind === "constant" && this.expression.value;
const ignoreLut = shouldIgnoreLut(this.lutExpression, feature, featureState, availableImages, void 0, brightness, void 0, worldview);
this._setPaintValue(start, end, value, ignoreLut ? null : this.context.lut);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_setPaintValue(start, end, value, lut) {
if (this.type === "color") {
const color = packColor(value.toPremultipliedRenderColor(lut));
for (let i = start; i < end; i++) {
this.paintVertexArray.emplace(i, color[0], color[1]);
}
} else {
for (let i = start; i < end; i++) {
this.paintVertexArray.emplace(i, value);
}
this.maxValue = Math.max(this.maxValue, Math.abs(value));
}
}
upload(context) {
if (this.paintVertexArray && this.paintVertexArray.arrayBuffer) {
if (this.paintVertexBuffer && this.paintVertexBuffer.buffer) {
this.paintVertexBuffer.updateData(this.paintVertexArray);
} else {
const dynamicDraw = this.lutExpression && this.lutExpression.kind !== "constant" && (this.lutExpression.isStateDependent || !this.lutExpression.isLightConstant) || this.expression.kind !== "constant" && (this.expression.isStateDependent || !this.expression.isLightConstant);
this.paintVertexBuffer = context.createVertexBuffer(this.paintVertexArray, this.paintVertexAttributes, dynamicDraw);
}
}
}
destroy() {
if (this.paintVertexBuffer) {
this.paintVertexBuffer.destroy();
}
}
}
class CompositeExpressionBinder {
constructor(expression, names, type, useIntegerZoom, context, PaintVertexArray) {
this.expression = expression;
this.uniformNames = names.map((name) => `u_${name}_t`);
this.type = type;
this.useIntegerZoom = useIntegerZoom;
this.context = context;
this.maxValue = 0;
this.paintVertexAttributes = names.map((name) => ({
name: `a_${name}`,
type: "Float32",
components: type === "color" ? 4 : 2,
offset: 0
}));
this.paintVertexArray = new PaintVertexArray();
}
populatePaintArray(newLength, feature, imagePositions, availableImages, canonical, brightness, formattedSection, worldview) {
const min = this.expression.evaluate(new EvaluationParameters(this.context.zoom, { brightness, worldview }), feature, {}, canonical, availableImages, formattedSection);
const max = this.expression.evaluate(new EvaluationParameters(this.context.zoom + 1, { brightness, worldview }), feature, {}, canonical, availableImages, formattedSection);
const ignoreLut = shouldIgnoreLut(this.lutExpression, feature, {}, availableImages, canonical, brightness, formattedSection, worldview);
const start = this.paintVertexArray.length;
this.paintVertexArray.resize(newLength);
this._setPaintValue(start, newLength, min, max, ignoreLut ? null : this.context.lut);
}
updatePaintArray(start, end, feature, featureState, availableImages, spritePositions, brightness, worldview) {
const min = this.expression.evaluate({ zoom: this.context.zoom, brightness, worldview }, feature, featureState, void 0, availableImages);
const max = this.expression.evaluate({ zoom: this.context.zoom + 1, brightness, worldview }, feature, featureState, void 0, availableImages);
const ignoreLut = shouldIgnoreLut(this.lutExpression, feature, featureState, availableImages, void 0, brightness, void 0, worldview);
this._setPaintValue(start, end, min, max, ignoreLut ? null : this.context.lut);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_setPaintValue(start, end, min, max, lut) {
if (this.type === "color") {
const minColor = packColor(min.toPremultipliedRenderColor(lut));
const maxColor = packColor(min.toPremultipliedRenderColor(lut));
for (let i = start; i < end; i++) {
this.paintVertexArray.emplace(i, minColor[0], minColor[1], maxColor[0], maxColor[1]);
}
} else {
for (let i = start; i < end; i++) {
this.paintVertexArray.emplace(i, min, max);
}
this.maxValue = Math.max(this.maxValue, Math.abs(min), Math.abs(max));
}
}
upload(context) {
if (this.paintVertexArray && this.paintVertexArray.arrayBuffer) {
if (this.paintVertexBuffer && this.paintVertexBuffer.buffer) {
this.paintVertexBuffer.updateData(this.paintVertexArray);
} else {
this.paintVertexBuffer = context.createVertexBuffer(this.paintVertexArray, this.paintVertexAttributes, this.expression.isStateDependent || !this.expression.isLightConstant);
}
}
}
destroy() {
if (this.paintVertexBuffer) {
this.paintVertexBuffer.destroy();
}
}
setUniform(program, uniform, globals, _, uniformName) {
const currentZoom = this.useIntegerZoom ? Math.floor(globals.zoom) : globals.zoom;
const factor = clamp(this.expression.interpolationFactor(currentZoom, this.context.zoom, this.context.zoom + 1), 0, 1);
uniform.set(program, uniformName, factor);
}
getBinding(context, _) {
return new Uniform1f(context);
}
}
class PatternCompositeBinder {
constructor(expression, names, type, PaintVertexArray, layerId) {
this.expression = expression;
this.layerId = layerId;
this.paintVertexAttributes = (type === "array" ? dashAttributes : patternAttributes).members;
for (let i = 0; i < names.length; ++i) {
assert$1(!this.paintVertexAttributes[i] || `a_${names[i]}` === this.paintVertexAttributes[i].name);
}
this.paintVertexArray = new PaintVertexArray();
this.paintTransitionVertexArray = new StructArrayLayout4ui8();
}
populatePaintArray(length, feature, imagePositions, _availableImages) {
const start = this.paintVertexArray.length;
this.paintVertexArray.resize(length);
this._setPaintValues(start, length, feature.patterns && feature.patterns[this.layerId], imagePositions);
}
updatePaintArray(start, end, feature, featureState, availableImages, imagePositions, _) {
this._setPaintValues(start, end, feature.patterns && feature.patterns[this.layerId], imagePositions);
}
_setPaintValues(start, end, patterns, positions) {
if (!positions || !patterns) return;
const primaryPos = positions[patterns[0]];
const secondaryPos = positions[patterns[1]];
if (!primaryPos) return;
if (primaryPos) {
const { tl, br, pixelRatio } = primaryPos;
for (let i = start; i < end; i++) {
this.paintVertexArray.emplace(i, tl[0], tl[1], br[0], br[1], pixelRatio);
}
}
if (secondaryPos) {
this.paintTransitionVertexArray.resize(this.paintVertexArray.length);
const { tl, br } = secondaryPos;
for (let i = start; i < end; i++) {
this.paintTransitionVertexArray.emplace(i, tl[0], tl[1], br[0], br[1]);
}
}
}
upload(context) {
const isDynamicDraw = this.expression.isStateDependent || !this.expression.isLightConstant;
if (this.paintVertexArray && this.paintVertexArray.arrayBuffer) {
this.paintVertexBuffer = context.createVertexBuffer(this.paintVertexArray, this.paintVertexAttributes, isDynamicDraw);
}
if (this.paintTransitionVertexArray && this.paintTransitionVertexArray.length) {
this.paintTransitionVertexBuffer = context.createVertexBuffer(this.paintTransitionVertexArray, patternTransitionAttributes.members, isDynamicDraw);
}
}
destroy() {
if (this.paintVertexBuffer) this.paintVertexBuffer.destroy();
if (this.paintTransitionVertexBuffer) this.paintTransitionVertexBuffer.destroy();
}
}
class ProgramConfiguration {
constructor(layer, context, filterProperties = () => true) {
this.binders = {};
this._buffers = [];
this.context = context;
const keys = [];
for (const property in layer.paint._values) {
const value = layer.paint.get(property);
if (property.endsWith("-use-theme")) continue;
if (!filterProperties(property)) continue;
if (!(value instanceof PossiblyEvaluatedPropertyValue) || !supportsPropertyExpression(value.property.specification)) {
continue;
}
const names = paintAttributeNames(property, layer.type);
const expression = value.value;
const type = value.property.specification.type;
const useIntegerZoom = !!value.property.useIntegerZoom;
const isPattern = property === "line-dasharray" || property.endsWith("pattern");
const valueUseTheme = layer.paint.get(`${property}-use-theme`);
const sourceException = property === "line-dasharray" && layer.layout.get("line-cap").value.kind !== "constant" || valueUseTheme && valueUseTheme.value.kind !== "constant";
if (expression.kind === "constant" && !sourceException) {
this.binders[property] = isPattern ? new PatternConstantBinder(expression.value, names) : new ConstantBinder(expression.value, names, type, context);
keys.push(`/u_${property}`);
} else if (expression.kind === "source" || sourceException || isPattern) {
const StructArrayLayout = layoutType(property, type, "source");
this.binders[property] = isPattern ? (
// @ts-expect-error - TS2345 - Argument of type 'PossiblyEvaluatedValue' is not assignable to parameter of type 'CompositeExpression'.
new PatternCompositeBinder(expression, names, type, StructArrayLayout, layer.id)
) : new SourceExpressionBinder(expression, names, type, StructArrayLayout);
keys.push(`/a_${property}`);
} else {
const StructArrayLayout = layoutType(property, type, "composite");
this.binders[property] = new CompositeExpressionBinder(expression, names, type, useIntegerZoom, context, StructArrayLayout);
keys.push(`/z_${property}`);
}
if (valueUseTheme) {
this.binders[property].lutExpression = valueUseTheme.value;
}
}
this.cacheKey = keys.sort().join("");
}
getMaxValue(property) {
const binder = this.binders[property];
return binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder ? binder.maxValue : 0;
}
populatePaintArrays(newLength, feature, imagePositions, availableImages, canonical, brightness, formattedSection, worldview) {
for (const property in this.binders) {
const binder = this.binders[property];
binder.context = this.context;
if (binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder || binder instanceof PatternCompositeBinder)
binder.populatePaintArray(newLength, feature, imagePositions, availableImages, canonical, brightness, formattedSection, worldview);
}
}
setConstantPatternPositions(primaryPosTo, secondaryPosTo) {
for (const property in this.binders) {
const binder = this.binders[property];
if (binder instanceof PatternConstantBinder)
binder.setConstantPatternPositions(primaryPosTo, secondaryPosTo);
}
}
getPatternTransitionVertexBuffer(property) {
const binder = this.binders[property];
if (binder instanceof PatternCompositeBinder) {
return binder.paintTransitionVertexBuffer;
}
return null;
}
updatePaintArrays(featureStates, featureMap, featureMapWithoutIds, vtLayer, layer, availableImages, imagePositions, isBrightnessChanged, brightness, worldview) {
let dirty = false;
const keys = Object.keys(featureStates);
const featureStateUpdate = keys.length !== 0 && !isBrightnessChanged;
const ids = featureStateUpdate ? keys : featureMap.uniqueIds;
this.context.lut = layer.lut;
for (const property in this.binders) {
const binder = this.binders[property];
binder.context = this.context;
const isExpressionNotConst = binder.expression && binder.expression.kind && binder.expression.kind !== "constant";
if ((binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder || // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
binder instanceof PatternCompositeBinder) && isExpressionNotConst && (binder.expression.isStateDependent === true || binder.expression.isLightConstant === false)) {
const value = layer.paint.get(property);
binder.expression = value.value;
for (const id of ids) {
const state = featureStates[id.toString()];
featureMap.eachPosition(id, (index, start, end) => {
const feature = vtLayer.feature(index);
binder.updatePaintArray(start, end, feature, state, availableImages, imagePositions, brightness, worldview);
});
}
if (!featureStateUpdate) {
for (const id of featureMapWithoutIds.uniqueIds) {
const state = featureStates[id.toString()];
featureMapWithoutIds.eachPosition(id, (index, start, end) => {
const feature = vtLayer.feature(index);
binder.updatePaintArray(start, end, feature, state, availableImages, imagePositions, brightness, worldview);
});
}
}
dirty = true;
}
}
return dirty;
}
defines() {
const result = [];
for (const property in this.binders) {
const binder = this.binders[property];
if (binder instanceof ConstantBinder || binder instanceof PatternConstantBinder) {
result.push(...binder.uniformNames.map((name) => `#define HAS_UNIFORM_${name}`));
}
}
return result;
}
getPaintVertexBuffers() {
assert$1(this._buffers);
return this._buffers;
}
getUniforms(context) {
const uniforms = [];
for (const property in this.binders) {
const binder = this.binders[property];
if (binder instanceof ConstantBinder || binder instanceof PatternConstantBinder || binder instanceof CompositeExpressionBinder) {
for (const name of binder.uniformNames) {
uniforms.push({ name, property, binding: binder.getBinding(context, name) });
}
}
}
return uniforms;
}
setUniforms(program, context, binderUniforms, properties, globals) {
for (const { name, property, binding } of binderUniforms) {
this.binders[property].setUniform(program, binding, globals, properties.get(property), name);
}
}
updatePaintBuffers() {
this._buffers = [];
for (const property in this.binders) {
const binder = this.binders[property];
if ((binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder || binder instanceof PatternCompositeBinder) && binder.paintVertexBuffer) {
this._buffers.push(binder.paintVertexBuffer);
}
if (binder instanceof PatternCompositeBinder && binder.paintTransitionVertexBuffer) {
this._buffers.push(binder.paintTransitionVertexBuffer);
}
}
}
upload(context) {
for (const property in this.binders) {
const binder = this.binders[property];
if (binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder || binder instanceof PatternCompositeBinder)
binder.upload(context);
}
this.updatePaintBuffers();
}
destroy() {
for (const property in this.binders) {
const binder = this.binders[property];
if (binder instanceof SourceExpressionBinder || binder instanceof CompositeExpressionBinder || binder instanceof PatternCompositeBinder)
binder.destroy();
}
}
}
class ProgramConfigurationSet {
constructor(layers, context, filterProperties = () => true) {
this.programConfigurations = {};
for (const layer of layers) {
this.programConfigurations[layer.id] = new ProgramConfiguration(layer, context, filterProperties);
}
this.needsUpload = false;
this._featureMap = new FeaturePositionMap();
this._featureMapWithoutIds = new FeaturePositionMap();
this._bufferOffset = 0;
this._idlessCounter = 0;
}
populatePaintArrays(length, feature, index, imagePositions, availableImages, canonical, brightness, formattedSection, worldview) {
for (const key in this.programConfigurations) {
this.programConfigurations[key].populatePaintArrays(length, feature, imagePositions, availableImages, canonical, brightness, formattedSection, worldview);
}
if (feature.id !== void 0) {
this._featureMap.add(feature.id, index, this._bufferOffset, length);
} else {
this._featureMapWithoutIds.add(this._idlessCounter, index, this._bufferOffset, length);
this._idlessCounter += 1;
}
this._bufferOffset = length;
this.needsUpload = true;
}
updatePaintArrays(featureStates, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview) {
for (const layer of layers) {
this.needsUpload = this.programConfigurations[layer.id].updatePaintArrays(featureStates, this._featureMap, this._featureMapWithoutIds, vtLayer, layer, availableImages, imagePositions, isBrightnessChanged, brightness || 0, worldview) || this.needsUpload;
}
}
get(layerId) {
return this.programConfigurations[layerId];
}
upload(context) {
if (!this.needsUpload) return;
for (const layerId in this.programConfigurations) {
this.programConfigurations[layerId].upload(context);
}
this.needsUpload = false;
}
destroy() {
for (const layerId in this.programConfigurations) {
this.programConfigurations[layerId].destroy();
}
}
}
const attributeNameExceptions = {
"text-opacity": ["opacity"],
"icon-opacity": ["opacity"],
"text-occlusion-opacity": ["occlusion_opacity"],
"icon-occlusion-opacity": ["occlusion_opacity"],
"text-color": ["fill_color"],
"icon-color": ["fill_color"],
"text-emissive-strength": ["emissive_strength"],
"icon-emissive-strength": ["emissive_strength"],
"text-halo-color": ["halo_color"],
"icon-halo-color": ["halo_color"],
"text-halo-blur": ["halo_blur"],
"icon-halo-blur": ["halo_blur"],
"text-halo-width": ["halo_width"],
"icon-halo-width": ["halo_width"],
"symbol-z-offset": ["z_offset"],
"line-gap-width": ["gapwidth"],
"line-pattern": ["pattern", "pixel_ratio", "pattern_b"],
"fill-pattern": ["pattern", "pixel_ratio", "pattern_b"],
"fill-extrusion-pattern": ["pattern", "pixel_ratio", "pattern_b"],
"line-dasharray": ["dash"],
"fill-bridge-guard-rail-color": ["structure_color"],
"fill-tunnel-structure-color": ["structure_color"]
};
function paintAttributeNames(property, type) {
return attributeNameExceptions[property] || [property.replace(`${type}-`, "").replace(/-/g, "_")];
}
const propertyExceptions = {
"line-pattern": {
"source": StructArrayLayout4ui1f12,
"composite": StructArrayLayout4ui1f12
},
"fill-pattern": {
"source": StructArrayLayout4ui1f12,
"composite": StructArrayLayout4ui1f12
},
"fill-extrusion-pattern": {
"source": StructArrayLayout4ui1f12,
"composite": StructArrayLayout4ui1f12
},
"line-dasharray": {
// temporary layout
"source": StructArrayLayout4ui8,
"composite": StructArrayLayout4ui8
}
};
const defaultLayouts = {
"color": {
"source": StructArrayLayout2f8,
"composite": StructArrayLayout4f16
},
"number": {
"source": StructArrayLayout1f4,
"composite": StructArrayLayout2f8
}
};
function layoutType(property, type, binderType) {
const layoutException = propertyExceptions[property];
return layoutException && layoutException[binderType] || defaultLayouts[type][binderType];
}
register(ConstantBinder, "ConstantBinder");
register(PatternConstantBinder, "PatternConstantBinder");
register(SourceExpressionBinder, "SourceExpressionBinder");
register(PatternCompositeBinder, "PatternCompositeBinder");
register(CompositeExpressionBinder, "CompositeExpressionBinder");
register(ProgramConfiguration, "ProgramConfiguration", { omit: ["_buffers"] });
register(ProgramConfigurationSet, "ProgramConfigurationSet");
const GLOBE_RADIUS = EXTENT / Math.PI / 2;
const GLOBE_ZOOM_THRESHOLD_MIN = 5;
const GLOBE_ZOOM_THRESHOLD_MAX = 6;
const GLOBE_SCALE_MATCH_LATITUDE = 45;
const GLOBE_NORMALIZATION_BIT_RANGE = 15;
const GLOBE_NORMALIZATION_MASK = (1 << GLOBE_NORMALIZATION_BIT_RANGE - 1) - 1;
const GLOBE_VERTEX_GRID_SIZE = 64;
const GLOBE_LATITUDINAL_GRID_LOD_TABLE = [GLOBE_VERTEX_GRID_SIZE, GLOBE_VERTEX_GRID_SIZE / 2, GLOBE_VERTEX_GRID_SIZE / 4];
const TILE_SIZE = 512;
const GLOBE_MIN = -GLOBE_RADIUS;
const GLOBE_MAX = GLOBE_RADIUS;
function csLatLngToECEF(cosLat, sinLat, lng, radius = GLOBE_RADIUS) {
lng = degToRad(lng);
const sx = cosLat * Math.sin(lng) * radius;
const sy = -sinLat * radius;
const sz = cosLat * Math.cos(lng) * radius;
return [sx, sy, sz];
}
function ecefToLatLng([x, y, z]) {
const radius = Math.hypot(x, y, z);
const lng = Math.atan2(x, z);
const lat = Math.PI * 0.5 - Math.acos(-y / radius);
return new LngLat(radToDeg(lng), radToDeg(lat));
}
function latLngToECEF(lat, lng, radius) {
assert$1(lat <= 90 && lat >= -90, "Lattitude must be between -90 and 90");
return csLatLngToECEF(Math.cos(degToRad(lat)), Math.sin(degToRad(lat)), lng, radius);
}
const earthRadius = 63710088e-1;
const earthCircumference = 2 * Math.PI * earthRadius;
class LngLat {
constructor(lng, lat) {
if (isNaN(lng) || isNaN(lat)) {
throw new Error(`Invalid LngLat object: (${lng}, ${lat})`);
}
this.lng = +lng;
this.lat = +lat;
if (this.lat > 90 || this.lat < -90) {
throw new Error("Invalid LngLat latitude value: must be between -90 and 90");
}
}
/**
* Returns a new `LngLat` object whose longitude is wrapped to the range (-180, 180).
*
* @returns {LngLat} The wrapped `LngLat` object.
* @example
* const ll = new mapboxgl.LngLat(286.0251, 40.7736);
* const wrapped = ll.wrap();
* console.log(wrapped.lng); // = -73.9749
*/
wrap() {
return new LngLat(wrap$1(this.lng, -180, 180), this.lat);
}
/**
* Returns the coordinates represented as an array of two numbers.
*
* @returns {Array} The coordinates represeted as an array of longitude and latitude.
* @example
* const ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toArray(); // = [-73.9749, 40.7736]
*/
toArray() {
return [this.lng, this.lat];
}
/**
* Returns the coordinates represent as a string.
*
* @returns {string} The coordinates represented as a string of the format `'LngLat(lng, lat)'`.
* @example
* const ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toString(); // = "LngLat(-73.9749, 40.7736)"
*/
toString() {
return `LngLat(${this.lng}, ${this.lat})`;
}
/**
* Returns the approximate distance between a pair of coordinates in meters.
* Uses the Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine", Sky and Telescope, vol. 68, no. 2, 1984, p. 159).
*
* @param {LngLat} lngLat Coordinates to compute the distance to.
* @returns {number} Distance in meters between the two coordinates.
* @example
* const newYork = new mapboxgl.LngLat(-74.0060, 40.7128);
* const losAngeles = new mapboxgl.LngLat(-118.2437, 34.0522);
* newYork.distanceTo(losAngeles); // = 3935751.690893987, "true distance" using a non-spherical approximation is ~3966km
*/
distanceTo(lngLat) {
const rad = Math.PI / 180;
const lat1 = this.lat * rad;
const lat2 = lngLat.lat * rad;
const a = Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos((lngLat.lng - this.lng) * rad);
const maxMeters = earthRadius * Math.acos(Math.min(a, 1));
return maxMeters;
}
/**
* Returns a `LngLatBounds` from the coordinates extended by a given `radius`. The returned `LngLatBounds` completely contains the `radius`.
*
* @param {number} [radius=0] Distance in meters from the coordinates to extend the bounds.
* @returns {LngLatBounds} A new `LngLatBounds` object representing the coordinates extended by the `radius`.
* @example
* const ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toBounds(100).toArray(); // = [[-73.97501862141328, 40.77351016847229], [-73.97478137858673, 40.77368983152771]]
*/
toBounds(radius = 0) {
const earthCircumferenceInMetersAtEquator = 40075017;
const latAccuracy = 360 * radius / earthCircumferenceInMetersAtEquator, lngAccuracy = latAccuracy / Math.cos(Math.PI / 180 * this.lat);
return new LngLatBounds(
{ lng: this.lng - lngAccuracy, lat: this.lat - latAccuracy },
{ lng: this.lng + lngAccuracy, lat: this.lat + latAccuracy }
);
}
toEcef(altitude) {
const altInEcef = altitude * GLOBE_RADIUS / earthRadius;
const radius = GLOBE_RADIUS + altInEcef;
return latLngToECEF(this.lat, this.lng, radius);
}
/**
* Converts an array of two numbers or an object with `lng` and `lat` or `lon` and `lat` properties
* to a `LngLat` object.
*
* If a `LngLat` object is passed in, the function returns it unchanged.
*
* @param {LngLatLike} input An array of two numbers or object to convert, or a `LngLat` object to return.
* @returns {LngLat} A new `LngLat` object, if a conversion occurred, or the original `LngLat` object.
* @example
* const arr = [-73.9749, 40.7736];
* const ll = mapboxgl.LngLat.convert(arr);
* console.log(ll); // = LngLat {lng: -73.9749, lat: 40.7736}
*/
static convert(input) {
if (input instanceof LngLat) {
return input;
}
if (Array.isArray(input) && (input.length === 2 || input.length === 3)) {
return new LngLat(Number(input[0]), Number(input[1]));
}
if (!Array.isArray(input) && typeof input === "object" && input !== null) {
return new LngLat(
Number("lng" in input ? input.lng : input.lon),
Number(input.lat)
);
}
throw new Error("`LngLatLike` argument must be specified as a LngLat instance, an object {lng: , lat: }, an object {lon: , lat: }, or an array of [, ]");
}
}
class LngLatBounds {
constructor(sw, ne) {
if (!sw) {
} else if (ne) {
this.setSouthWest(sw).setNorthEast(ne);
} else if (Array.isArray(sw) && sw.length === 4) {
this.setSouthWest([sw[0], sw[1]]).setNorthEast([sw[2], sw[3]]);
} else {
this.setSouthWest(sw[0]).setNorthEast(sw[1]);
}
}
/**
* Set the northeast corner of the bounding box.
*
* @param {LngLatLike} ne A {@link LngLatLike} object describing the northeast corner of the bounding box.
* @returns {LngLatBounds} Returns itself to allow for method chaining.
* @example
* const sw = new mapboxgl.LngLat(-73.9876, 40.7661);
* const ne = new mapboxgl.LngLat(-73.9397, 40.8002);
* const llb = new mapboxgl.LngLatBounds(sw, ne);
* llb.setNorthEast([-73.9397, 42.8002]);
*/
setNorthEast(ne) {
this._ne = ne instanceof LngLat ? new LngLat(ne.lng, ne.lat) : LngLat.convert(ne);
return this;
}
/**
* Set the southwest corner of the bounding box.
*
* @param {LngLatLike} sw A {@link LngLatLike} object describing the southwest corner of the bounding box.
* @returns {LngLatBounds} Returns itself to allow for method chaining.
* @example
* const sw = new mapboxgl.LngLat(-73.9876, 40.7661);
* const ne = new mapboxgl.LngLat(-73.9397, 40.8002);
* const llb = new mapboxgl.LngLatBounds(sw, ne);
* llb.setSouthWest([-73.9876, 40.2661]);
*/
setSouthWest(sw) {
this._sw = sw instanceof LngLat ? new LngLat(sw.lng, sw.lat) : LngLat.convert(sw);
return this;
}
/**
* Extend the bounds to include a given LngLatLike or LngLatBoundsLike.
*
* @param {LngLatLike|LngLatBoundsLike} obj Object to extend to.
* @returns {LngLatBounds} Returns itself to allow for method chaining.
* @example
* const sw = new mapboxgl.LngLat(-73.9876, 40.7661);
* const ne = new mapboxgl.LngLat(-73.9397, 40.8002);
* const llb = new mapboxgl.LngLatBounds(sw, ne);
* llb.extend([-72.9876, 42.2661]);
*/
extend(obj) {
const sw = this._sw, ne = this._ne;
let sw2;
let ne2;
if (obj instanceof LngLat) {
sw2 = obj;
ne2 = obj;
} else if (obj instanceof LngLatBounds) {
sw2 = obj._sw;
ne2 = obj._ne;
if (!sw2 || !ne2) return this;
} else if (Array.isArray(obj)) {
if (obj.length === 4 || obj.every(Array.isArray)) {
const lngLatBoundsObj = obj;
return this.extend(LngLatBounds.convert(lngLatBoundsObj));
} else {
const lngLatObj = obj;
return this.extend(LngLat.convert(lngLatObj));
}
} else if (typeof obj === "object" && obj !== null && obj.hasOwnProperty("lat") && (obj.hasOwnProperty("lon") || obj.hasOwnProperty("lng"))) {
return this.extend(LngLat.convert(obj));
} else {
return this;
}
if (!sw && !ne) {
this._sw = new LngLat(sw2.lng, sw2.lat);
this._ne = new LngLat(ne2.lng, ne2.lat);
} else {
sw.lng = Math.min(sw2.lng, sw.lng);
sw.lat = Math.min(sw2.lat, sw.lat);
ne.lng = Math.max(ne2.lng, ne.lng);
ne.lat = Math.max(ne2.lat, ne.lat);
}
return this;
}
/**
* Returns the geographical coordinate equidistant from the bounding box's corners.
*
* @returns {LngLat} The bounding box's center.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getCenter(); // = LngLat {lng: -73.96365, lat: 40.78315}
*/
getCenter() {
return new LngLat((this._sw.lng + this._ne.lng) / 2, (this._sw.lat + this._ne.lat) / 2);
}
/**
* Returns the southwest corner of the bounding box.
*
* @returns {LngLat} The southwest corner of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getSouthWest(); // LngLat {lng: -73.9876, lat: 40.7661}
*/
getSouthWest() {
return this._sw;
}
/**
* Returns the northeast corner of the bounding box.
*
* @returns {LngLat} The northeast corner of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getNorthEast(); // LngLat {lng: -73.9397, lat: 40.8002}
*/
getNorthEast() {
return this._ne;
}
/**
* Returns the northwest corner of the bounding box.
*
* @returns {LngLat} The northwest corner of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getNorthWest(); // LngLat {lng: -73.9876, lat: 40.8002}
*/
getNorthWest() {
return new LngLat(this.getWest(), this.getNorth());
}
/**
* Returns the southeast corner of the bounding box.
*
* @returns {LngLat} The southeast corner of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getSouthEast(); // LngLat {lng: -73.9397, lat: 40.7661}
*/
getSouthEast() {
return new LngLat(this.getEast(), this.getSouth());
}
/**
* Returns the west edge of the bounding box.
*
* @returns {number} The west edge of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getWest(); // -73.9876
*/
getWest() {
return this._sw.lng;
}
/**
* Returns the south edge of the bounding box.
*
* @returns {number} The south edge of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getSouth(); // 40.7661
*/
getSouth() {
return this._sw.lat;
}
/**
* Returns the east edge of the bounding box.
*
* @returns {number} The east edge of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getEast(); // -73.9397
*/
getEast() {
return this._ne.lng;
}
/**
* Returns the north edge of the bounding box.
*
* @returns {number} The north edge of the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getNorth(); // 40.8002
*/
getNorth() {
return this._ne.lat;
}
/**
* Returns the bounding box represented as an array.
*
* @returns {Array>} The bounding box represented as an array, consisting of the
* southwest and northeast coordinates of the bounding represented as arrays of numbers.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]]
*/
toArray() {
return [this._sw.toArray(), this._ne.toArray()];
}
/**
* Return the bounding box represented as a string.
*
* @returns {string} The bounding box represents as a string of the format
* `'LngLatBounds(LngLat(lng, lat), LngLat(lng, lat))'`.
* @example
* const llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.toString(); // = "LngLatBounds(LngLat(-73.9876, 40.7661), LngLat(-73.9397, 40.8002))"
*/
toString() {
return `LngLatBounds(${this._sw.toString()}, ${this._ne.toString()})`;
}
/**
* Check if the bounding box is an empty/`null`-type box.
*
* @returns {boolean} True if bounds have been defined, otherwise false.
* @example
* const llb = new mapboxgl.LngLatBounds();
* llb.isEmpty(); // true
* llb.setNorthEast([-73.9876, 40.7661]);
* llb.setSouthWest([-73.9397, 40.8002]);
* llb.isEmpty(); // false
*/
isEmpty() {
return !(this._sw && this._ne);
}
/**
* Check if the point is within the bounding box.
*
* @param {LngLatLike} lnglat Geographic point to check against.
* @returns {boolean} True if the point is within the bounding box.
* @example
* const llb = new mapboxgl.LngLatBounds(
* new mapboxgl.LngLat(-73.9876, 40.7661),
* new mapboxgl.LngLat(-73.9397, 40.8002)
* );
*
* const ll = new mapboxgl.LngLat(-73.9567, 40.7789);
*
* console.log(llb.contains(ll)); // = true
*/
contains(lnglat) {
const { lng, lat } = LngLat.convert(lnglat);
const containsLatitude = this._sw.lat <= lat && lat <= this._ne.lat;
let containsLongitude = this._sw.lng <= lng && lng <= this._ne.lng;
if (this._sw.lng > this._ne.lng) {
containsLongitude = this._sw.lng >= lng && lng >= this._ne.lng;
}
return containsLatitude && containsLongitude;
}
/**
* Converts an array to a `LngLatBounds` object.
*
* If a `LngLatBounds` object is passed in, the function returns it unchanged.
*
* Internally, the function calls `LngLat#convert` to convert arrays to `LngLat` values.
*
* @param {LngLatBoundsLike} input An array of two coordinates to convert, or a `LngLatBounds` object to return.
* @returns {LngLatBounds | void} A new `LngLatBounds` object, if a conversion occurred, or the original `LngLatBounds` object.
* @example
* const arr = [[-73.9876, 40.7661], [-73.9397, 40.8002]];
* const llb = mapboxgl.LngLatBounds.convert(arr);
* console.log(llb); // = LngLatBounds {_sw: LngLat {lng: -73.9876, lat: 40.7661}, _ne: LngLat {lng: -73.9397, lat: 40.8002}}
*/
static convert(input) {
if (!input) return;
if (input instanceof LngLatBounds) return input;
return new LngLatBounds(input);
}
}
const DEFAULT_MIN_ZOOM = 0;
const DEFAULT_MAX_ZOOM = 25.5;
function circumferenceAtLatitude(latitude) {
return earthCircumference * Math.cos(latitude * Math.PI / 180);
}
function mercatorXfromLng(lng) {
return (180 + lng) / 360;
}
function mercatorYfromLat(lat) {
return (180 - 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360))) / 360;
}
function mercatorZfromAltitude(altitude, lat) {
return altitude / circumferenceAtLatitude(lat);
}
function lngFromMercatorX(x) {
return x * 360 - 180;
}
function latFromMercatorY(y) {
const y2 = 180 - y * 360;
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}
function altitudeFromMercatorZ(z, y) {
return z * circumferenceAtLatitude(latFromMercatorY(y));
}
const MAX_MERCATOR_LATITUDE = 85.051129;
function getLatitudeScale(lat) {
return Math.cos(degToRad(clamp(lat, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE)));
}
function getMetersPerPixelAtLatitude(lat, zoom) {
const constrainedZoom = clamp(zoom, DEFAULT_MIN_ZOOM, DEFAULT_MAX_ZOOM);
const constrainedScale = Math.pow(2, constrainedZoom);
return getLatitudeScale(lat) * earthCircumference / (constrainedScale * 512);
}
function mercatorScale(lat) {
return 1 / Math.cos(lat * Math.PI / 180);
}
function tileToMeter(canonical, tileYCoordinate = 0) {
const circumferenceAtEquator = 40075017;
const mercatorY = (canonical.y + tileYCoordinate / EXTENT) / (1 << canonical.z);
const exp = Math.exp(Math.PI * (1 - 2 * mercatorY));
return circumferenceAtEquator * 2 * exp / (exp * exp + 1) / EXTENT / (1 << canonical.z);
}
class MercatorCoordinate {
constructor(x, y, z = 0) {
this.x = +x;
this.y = +y;
this.z = +z;
}
/**
* Project a `LngLat` to a `MercatorCoordinate`.
*
* @param {LngLatLike} lngLatLike The location to project.
* @param {number} altitude The altitude in meters of the position.
* @returns {MercatorCoordinate} The projected mercator coordinate.
* @example
* const coord = mapboxgl.MercatorCoordinate.fromLngLat({lng: 0, lat: 0}, 0);
* console.log(coord); // MercatorCoordinate(0.5, 0.5, 0)
*/
static fromLngLat(lngLatLike, altitude = 0) {
const lngLat = LngLat.convert(lngLatLike);
return new MercatorCoordinate(
mercatorXfromLng(lngLat.lng),
mercatorYfromLat(lngLat.lat),
mercatorZfromAltitude(altitude, lngLat.lat)
);
}
/**
* Returns the `LngLat` for the coordinate.
*
* @returns {LngLat} The `LngLat` object.
* @example
* const coord = new mapboxgl.MercatorCoordinate(0.5, 0.5, 0);
* const lngLat = coord.toLngLat(); // LngLat(0, 0)
*/
toLngLat() {
return new LngLat(
lngFromMercatorX(this.x),
latFromMercatorY(this.y)
);
}
/**
* Returns the altitude in meters of the coordinate.
*
* @returns {number} The altitude in meters.
* @example
* const coord = new mapboxgl.MercatorCoordinate(0, 0, 0.02);
* coord.toAltitude(); // 6914.281956295339
*/
toAltitude() {
return altitudeFromMercatorZ(this.z, this.y);
}
/**
* Returns the distance of 1 meter in `MercatorCoordinate` units at this latitude.
*
* For coordinates in real world units using meters, this naturally provides the scale
* to transform into `MercatorCoordinate`s.
*
* @returns {number} Distance of 1 meter in `MercatorCoordinate` units.
* @example
* // Calculate a new MercatorCoordinate that is 150 meters west of the other coord.
* const coord = new mapboxgl.MercatorCoordinate(0.5, 0.25, 0);
* const offsetInMeters = 150;
* const offsetInMercatorCoordinateUnits = offsetInMeters * coord.meterInMercatorCoordinateUnits();
* const westCoord = new mapboxgl.MercatorCoordinate(coord.x - offsetInMercatorCoordinateUnits, coord.y, coord.z);
*/
meterInMercatorCoordinateUnits() {
return 1 / earthCircumference * mercatorScale(latFromMercatorY(this.y));
}
}
function pointToLineDist(px, py, ax, ay, bx, by) {
const dx = ax - bx;
const dy = ay - by;
return Math.abs((ay - py) * dx - (ax - px) * dy) / Math.hypot(dx, dy);
}
function addResampled(resampled, mx0, my0, mx2, my2, start, end, reproject, tolerance) {
const mx1 = (mx0 + mx2) / 2;
const my1 = (my0 + my2) / 2;
const mid = new Point(mx1, my1);
reproject(mid);
const err = pointToLineDist(mid.x, mid.y, start.x, start.y, end.x, end.y);
if (err >= tolerance) {
addResampled(resampled, mx0, my0, mx1, my1, start, mid, reproject, tolerance);
addResampled(resampled, mx1, my1, mx2, my2, mid, end, reproject, tolerance);
} else {
resampled.push(end);
}
}
function resample$1(line, reproject, tolerance) {
let prev = line[0];
let mx0 = prev.x;
let my0 = prev.y;
reproject(prev);
const resampled = [prev];
for (let i = 1; i < line.length; i++) {
const point = line[i];
const { x, y } = point;
reproject(point);
addResampled(resampled, mx0, my0, x, y, prev, point, reproject, tolerance);
mx0 = x;
my0 = y;
prev = point;
}
return resampled;
}
function addResampledPred(resampled, a, b, pred) {
const split = pred(a, b);
if (split) {
const mid = a.add(b)._mult(0.5);
addResampledPred(resampled, a, mid, pred);
addResampledPred(resampled, mid, b, pred);
} else {
resampled.push(b);
}
}
function resamplePred(line, predicate) {
let prev = line[0];
const resampled = [prev];
for (let i = 1; i < line.length; i++) {
const point = line[i];
addResampledPred(resampled, prev, point, predicate);
prev = point;
}
return resampled;
}
const BITS = 15;
const MAX = Math.pow(2, BITS - 1) - 1;
const MIN = -MAX - 1;
function preparePoint(point, scale) {
const x = Math.round(point.x * scale);
const y = Math.round(point.y * scale);
point.x = clamp(x, MIN, MAX);
point.y = clamp(y, MIN, MAX);
if (x < point.x || x > point.x + 1 || y < point.y || y > point.y + 1) {
warnOnce("Geometry exceeds allowed extent, reduce your vector tile buffer size");
}
return point;
}
function loadGeometry(feature, canonical, tileTransform) {
const geometry = feature.loadGeometry();
const extent = feature.extent;
const extentScale = EXTENT / extent;
if (canonical && tileTransform && tileTransform.projection.isReprojectedInTileSpace) {
const z2 = 1 << canonical.z;
const { scale, x, y, projection } = tileTransform;
const reproject = (p) => {
const lng = lngFromMercatorX((canonical.x + p.x / extent) / z2);
const lat = latFromMercatorY((canonical.y + p.y / extent) / z2);
const p2 = projection.project(lng, lat);
p.x = (p2.x * scale - x) * extent;
p.y = (p2.y * scale - y) * extent;
};
for (let i = 0; i < geometry.length; i++) {
if (feature.type !== 1) {
geometry[i] = resample$1(geometry[i], reproject, 1);
} else {
const line = [];
for (const p of geometry[i]) {
if (p.x < 0 || p.x >= extent || p.y < 0 || p.y >= extent) continue;
reproject(p);
line.push(p);
}
geometry[i] = line;
}
}
}
for (const line of geometry) {
for (const p of line) {
preparePoint(p, extentScale);
}
}
return geometry;
}
function toEvaluationFeature(feature, needGeometry) {
return {
type: feature.type,
id: feature.id,
properties: feature.properties,
geometry: needGeometry ? loadGeometry(feature) : []
};
}
/** @import Pbf from 'pbf' */
/** @import {Feature} from 'geojson' */
class VectorTileFeature {
/**
* @param {Pbf} pbf
* @param {number} end
* @param {number} extent
* @param {string[]} keys
* @param {(number | string | boolean)[]} values
*/
constructor(pbf, end, extent, keys, values) {
// Public
/** @type {Record} */
this.properties = {};
this.extent = extent;
/** @type {0 | 1 | 2 | 3} */
this.type = 0;
/** @type {number | undefined} */
this.id = undefined;
/** @private */
this._pbf = pbf;
/** @private */
this._geometry = -1;
/** @private */
this._keys = keys;
/** @private */
this._values = values;
pbf.readFields(readFeature, this, end);
}
loadGeometry() {
const pbf = this._pbf;
pbf.pos = this._geometry;
const end = pbf.readVarint() + pbf.pos;
/** @type Point[][] */
const lines = [];
/** @type Point[] | undefined */
let line;
let cmd = 1;
let length = 0;
let x = 0;
let y = 0;
while (pbf.pos < end) {
if (length <= 0) {
const cmdLen = pbf.readVarint();
cmd = cmdLen & 0x7;
length = cmdLen >> 3;
}
length--;
if (cmd === 1 || cmd === 2) {
x += pbf.readSVarint();
y += pbf.readSVarint();
if (cmd === 1) { // moveTo
if (line) lines.push(line);
line = [];
}
if (line) line.push(new Point(x, y));
} else if (cmd === 7) {
// Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
if (line) {
line.push(line[0].clone()); // closePolygon
}
} else {
throw new Error(`unknown command ${cmd}`);
}
}
if (line) lines.push(line);
return lines;
}
bbox() {
const pbf = this._pbf;
pbf.pos = this._geometry;
const end = pbf.readVarint() + pbf.pos;
let cmd = 1,
length = 0,
x = 0,
y = 0,
x1 = Infinity,
x2 = -Infinity,
y1 = Infinity,
y2 = -Infinity;
while (pbf.pos < end) {
if (length <= 0) {
const cmdLen = pbf.readVarint();
cmd = cmdLen & 0x7;
length = cmdLen >> 3;
}
length--;
if (cmd === 1 || cmd === 2) {
x += pbf.readSVarint();
y += pbf.readSVarint();
if (x < x1) x1 = x;
if (x > x2) x2 = x;
if (y < y1) y1 = y;
if (y > y2) y2 = y;
} else if (cmd !== 7) {
throw new Error(`unknown command ${cmd}`);
}
}
return [x1, y1, x2, y2];
}
/**
* @param {number} x
* @param {number} y
* @param {number} z
* @return {Feature}
*/
toGeoJSON(x, y, z) {
const size = this.extent * Math.pow(2, z),
x0 = this.extent * x,
y0 = this.extent * y,
vtCoords = this.loadGeometry();
/** @param {Point} p */
function projectPoint(p) {
return [
(p.x + x0) * 360 / size - 180,
360 / Math.PI * Math.atan(Math.exp((1 - (p.y + y0) * 2 / size) * Math.PI)) - 90
];
}
/** @param {Point[]} line */
function projectLine(line) {
return line.map(projectPoint);
}
/** @type {Feature["geometry"]} */
let geometry;
if (this.type === 1) {
const points = [];
for (const line of vtCoords) {
points.push(line[0]);
}
const coordinates = projectLine(points);
geometry = points.length === 1 ?
{type: 'Point', coordinates: coordinates[0]} :
{type: 'MultiPoint', coordinates};
} else if (this.type === 2) {
const coordinates = vtCoords.map(projectLine);
geometry = coordinates.length === 1 ?
{type: 'LineString', coordinates: coordinates[0]} :
{type: 'MultiLineString', coordinates};
} else if (this.type === 3) {
const polygons = classifyRings$1(vtCoords);
const coordinates = [];
for (const polygon of polygons) {
coordinates.push(polygon.map(projectLine));
}
geometry = coordinates.length === 1 ?
{type: 'Polygon', coordinates: coordinates[0]} :
{type: 'MultiPolygon', coordinates};
} else {
throw new Error('unknown feature type');
}
/** @type {Feature} */
const result = {
type: 'Feature',
geometry,
properties: this.properties
};
if (this.id != null) {
result.id = this.id;
}
return result;
}
}
/** @type {['Unknown', 'Point', 'LineString', 'Polygon']} */
VectorTileFeature.types = ['Unknown', 'Point', 'LineString', 'Polygon'];
/**
* @param {number} tag
* @param {VectorTileFeature} feature
* @param {Pbf} pbf
*/
function readFeature(tag, feature, pbf) {
if (tag === 1) feature.id = pbf.readVarint();
else if (tag === 2) readTag(pbf, feature);
else if (tag === 3) feature.type = /** @type {0 | 1 | 2 | 3} */ (pbf.readVarint());
// @ts-expect-error TS2341 deliberately accessing a private property
else if (tag === 4) feature._geometry = pbf.pos;
}
/**
* @param {Pbf} pbf
* @param {VectorTileFeature} feature
*/
function readTag(pbf, feature) {
const end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
// @ts-expect-error TS2341 deliberately accessing a private property
const key = feature._keys[pbf.readVarint()];
// @ts-expect-error TS2341 deliberately accessing a private property
const value = feature._values[pbf.readVarint()];
feature.properties[key] = value;
}
}
/** classifies an array of rings into polygons with outer rings and holes
* @param {Point[][]} rings
*/
function classifyRings$1(rings) {
const len = rings.length;
if (len <= 1) return [rings];
const polygons = [];
let polygon, ccw;
for (let i = 0; i < len; i++) {
const area = signedArea$1(rings[i]);
if (area === 0) continue;
if (ccw === undefined) ccw = area < 0;
if (ccw === area < 0) {
if (polygon) polygons.push(polygon);
polygon = [rings[i]];
} else if (polygon) {
polygon.push(rings[i]);
}
}
if (polygon) polygons.push(polygon);
return polygons;
}
/** @param {Point[]} ring */
function signedArea$1(ring) {
let sum = 0;
for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
p1 = ring[i];
p2 = ring[j];
sum += (p2.x - p1.x) * (p1.y + p2.y);
}
return sum;
}
class VectorTileLayer {
/**
* @param {Pbf} pbf
* @param {number} [end]
*/
constructor(pbf, end) {
// Public
this.version = 1;
this.name = '';
this.extent = 4096;
this.length = 0;
/** @private */
this._pbf = pbf;
/** @private
* @type {string[]} */
this._keys = [];
/** @private
* @type {(number | string | boolean)[]} */
this._values = [];
/** @private
* @type {number[]} */
this._features = [];
pbf.readFields(readLayer$1, this, end);
this.length = this._features.length;
}
/** return feature `i` from this layer as a `VectorTileFeature`
* @param {number} i
*/
feature(i) {
if (i < 0 || i >= this._features.length) throw new Error('feature index out of bounds');
this._pbf.pos = this._features[i];
const end = this._pbf.readVarint() + this._pbf.pos;
return new VectorTileFeature(this._pbf, end, this.extent, this._keys, this._values);
}
}
/**
* @param {number} tag
* @param {VectorTileLayer} layer
* @param {Pbf} pbf
*/
function readLayer$1(tag, layer, pbf) {
if (tag === 15) layer.version = pbf.readVarint();
else if (tag === 1) layer.name = pbf.readString();
else if (tag === 5) layer.extent = pbf.readVarint();
// @ts-expect-error TS2341 deliberately accessing a private property
else if (tag === 2) layer._features.push(pbf.pos);
// @ts-expect-error TS2341 deliberately accessing a private property
else if (tag === 3) layer._keys.push(pbf.readString());
// @ts-expect-error TS2341 deliberately accessing a private property
else if (tag === 4) layer._values.push(readValueMessage(pbf));
}
/**
* @param {Pbf} pbf
*/
function readValueMessage(pbf) {
let value = null;
const end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
const tag = pbf.readVarint() >> 3;
value = tag === 1 ? pbf.readString() :
tag === 2 ? pbf.readFloat() :
tag === 3 ? pbf.readDouble() :
tag === 4 ? pbf.readVarint64() :
tag === 5 ? pbf.readVarint() :
tag === 6 ? pbf.readSVarint() :
tag === 7 ? pbf.readBoolean() : null;
}
if (value == null) {
throw new Error('unknown feature value');
}
return value;
}
class VectorTile {
/**
* @param {Pbf} pbf
* @param {number} [end]
*/
constructor(pbf, end) {
/** @type {Record} */
this.layers = pbf.readFields(readTile, {}, end);
}
}
/**
* @param {number} tag
* @param {Record} layers
* @param {Pbf} pbf
*/
function readTile(tag, layers, pbf) {
if (tag === 3) {
const layer = new VectorTileLayer(pbf, pbf.readVarint() + pbf.pos);
if (layer.length) layers[layer.name] = layer;
}
}
const PROPERTY_ELEVATION_ID = "3d_elevation_id";
const PROPERTY_ELEVATION_ROAD_MARKUP_Z_LEVEL = "zLevel";
const PROPERTY_ELEVATION_ROAD_BASE_Z_LEVEL = "level";
const HD_ELEVATION_SOURCE_LAYER = "hd_road_elevation";
const ELEVATION_CLIP_MARGIN = 1;
const MARKUP_ELEVATION_BIAS = 0.05;
class PropertyParser {
constructor() {
this._valid = false;
}
reset(feature) {
this.feature = feature;
this._valid = true;
this._geometry = feature.loadGeometry();
if (this._geometry.length === 0 || this._geometry[0].length === 0) {
this._valid = false;
}
return this;
}
geometry(setter, convert) {
if (this._valid) {
setter(convert(this._geometry));
}
return this;
}
require(name, setter, convert) {
return this.get(name, true, setter, convert);
}
optional(name, setter, convert) {
return this.get(name, false, setter, convert);
}
success() {
return this._valid;
}
get(name, required, setter, convert) {
const value = this.feature.properties.hasOwnProperty(name) ? +this.feature.properties[name] : void 0;
if (this._valid && value !== void 0 && !Number.isNaN(value)) {
if (convert) {
setter(convert(value));
} else {
setter(value);
}
} else if (required) {
this._valid = false;
}
return this;
}
}
class VersionSchema {
constructor(feature, vertex) {
this.featureFunc = feature;
this.vertexFunc = vertex;
}
parseFeature(parser, feature, out) {
assert$1(this.featureFunc);
return this.featureFunc(parser, feature, out);
}
parseVertex(parser, feature, out) {
assert$1(this.vertexFunc);
return this.vertexFunc(parser, feature, out);
}
}
const schemaV100 = new VersionSchema(
(parser, feature, out) => {
return parser.reset(feature).require(PROPERTY_ELEVATION_ID, (value) => {
out.id = value;
}).optional("fixed_height_relative", (value) => {
out.constantHeight = value;
}, ElevationFeatureParser.decodeRelativeHeight).geometry((value) => {
out.bounds = value;
}, computeBounds).success();
},
(parser, feature, out) => {
return parser.reset(feature).require(PROPERTY_ELEVATION_ID, (value) => {
out.id = value;
}).require("elevation_idx", (value) => {
out.idx = value;
}).require("extent", (value) => {
out.extent = value;
}).require("height_relative", (value) => {
out.height = value;
}, ElevationFeatureParser.decodeRelativeHeight).geometry((value) => {
out.position = value;
}, ElevationFeatureParser.getPoint).success();
}
);
const schemaV101 = new VersionSchema(
(parser, feature, out) => {
return parser.reset(feature).require(PROPERTY_ELEVATION_ID, (value) => {
out.id = value;
}).optional("fixed_height", (value) => {
out.constantHeight = value;
}, ElevationFeatureParser.decodeMetricHeight).geometry((value) => {
out.bounds = value;
}, computeBounds).success();
},
(parser, feature, out) => {
return parser.reset(feature).require(PROPERTY_ELEVATION_ID, (value) => {
out.id = value;
}).require("elevation_idx", (value) => {
out.idx = value;
}).require("extent", (value) => {
out.extent = value;
}).require("height", (value) => {
out.height = value;
}, ElevationFeatureParser.decodeMetricHeight).geometry((value) => {
out.position = value;
}, ElevationFeatureParser.getPoint).success();
}
);
class ElevationFeatureParser {
static getPoint(points) {
return fromValues(points[0][0].x, points[0][0].y);
}
static decodeRelativeHeight(height) {
const RELATIVE_ELEVATION_TO_METERS = 5;
const scaler = 1 / 1e4;
return height * scaler * RELATIVE_ELEVATION_TO_METERS;
}
static decodeMetricHeight(height) {
const scaler = 1 / 1e4;
return height * scaler;
}
static getVersionSchema(version) {
if (!version) {
return schemaV100;
}
if (version === "1.0.1") {
return schemaV101;
}
return void 0;
}
static parse(data) {
const vertices = [];
const features = [];
const featureCount = data.length;
const parser = new PropertyParser();
for (let index = 0; index < featureCount; index++) {
const feature = data.feature(index);
const version = feature.properties.version;
const schema = ElevationFeatureParser.getVersionSchema(version);
if (schema === void 0) {
warnOnce(`Unknown elevation feature version number ${version || "(unknown)"}`);
continue;
}
const type = feature.properties["type"];
if (!type) {
continue;
}
const featureType = VectorTileFeature.types[feature.type];
if (featureType === "Point" && type === "curve_point") {
const out = {};
if (schema.parseVertex(parser, feature, out)) {
vertices.push(out);
}
} else if (featureType === "Polygon" && type === "curve_meta") {
const out = {};
if (schema.parseFeature(parser, feature, out)) {
features.push(out);
}
}
}
return { vertices, features };
}
}
class Ray2D {
constructor(pos_, dir_) {
this.pos = pos_;
this.dir = dir_;
}
intersectsPlane(pt, normal, out) {
const D = dot$1(normal, this.dir);
if (Math.abs(D) < 1e-6) {
return false;
}
const t = ((pt[0] - this.pos[0]) * normal[0] + (pt[1] - this.pos[1]) * normal[1]) / D;
out[0] = this.pos[0] + this.dir[0] * t;
out[1] = this.pos[1] + this.dir[1] * t;
return true;
}
}
class Ray {
constructor(pos_, dir_) {
this.pos = pos_;
this.dir = dir_;
}
intersectsPlane(pt, normal, out) {
const D = dot$5(normal, this.dir);
if (Math.abs(D) < 1e-6) {
return false;
}
const t = ((pt[0] - this.pos[0]) * normal[0] + (pt[1] - this.pos[1]) * normal[1] + (pt[2] - this.pos[2]) * normal[2]) / D;
out[0] = this.pos[0] + this.dir[0] * t;
out[1] = this.pos[1] + this.dir[1] * t;
out[2] = this.pos[2] + this.dir[2] * t;
return true;
}
closestPointOnSphere(center, r, out) {
assert$1(squaredLength$4(this.dir) > 0 && r >= 0);
if (equals$6(this.pos, center) || r === 0) {
out[0] = out[1] = out[2] = 0;
return false;
}
const [dx, dy, dz] = this.dir;
const px = this.pos[0] - center[0];
const py = this.pos[1] - center[1];
const pz = this.pos[2] - center[2];
const a = dx * dx + dy * dy + dz * dz;
const b = 2 * (px * dx + py * dy + pz * dz);
const c = px * px + py * py + pz * pz - r * r;
const d = b * b - 4 * a * c;
if (d < 0) {
const t = Math.max(-b / 2, 0);
const gx = px + dx * t;
const gy = py + dy * t;
const gz = pz + dz * t;
const glen = Math.hypot(gx, gy, gz);
out[0] = gx * r / glen;
out[1] = gy * r / glen;
out[2] = gz * r / glen;
return false;
} else {
assert$1(a > 0);
const t = (-b - Math.sqrt(d)) / (2 * a);
if (t < 0) {
const plen = Math.hypot(px, py, pz);
out[0] = px * r / plen;
out[1] = py * r / plen;
out[2] = pz * r / plen;
return false;
} else {
out[0] = px + dx * t;
out[1] = py + dy * t;
out[2] = pz + dz * t;
return true;
}
}
}
}
class FrustumCorners {
constructor(TL_, TR_, BR_, BL_, horizon_) {
this.TL = TL_;
this.TR = TR_;
this.BR = BR_;
this.BL = BL_;
this.horizon = horizon_;
}
static fromInvProjectionMatrix(invProj, horizonFromTop, viewportHeight) {
const TLClip = [-1, 1, 1];
const TRClip = [1, 1, 1];
const BRClip = [1, -1, 1];
const BLClip = [-1, -1, 1];
const TL = transformMat4$2(TLClip, TLClip, invProj);
const TR = transformMat4$2(TRClip, TRClip, invProj);
const BR = transformMat4$2(BRClip, BRClip, invProj);
const BL = transformMat4$2(BLClip, BLClip, invProj);
return new FrustumCorners(TL, TR, BR, BL, horizonFromTop / viewportHeight);
}
}
function projectPoints(points, origin, axis) {
let min = Infinity;
let max = -Infinity;
const vec = [];
for (const point of points) {
sub$2(vec, point, origin);
const projection = dot$5(vec, axis);
min = Math.min(min, projection);
max = Math.max(max, projection);
}
return [min, max];
}
function intersectsFrustum(frustum, aabbPoints) {
let fullyInside = true;
for (let p = 0; p < frustum.planes.length; p++) {
const plane = frustum.planes[p];
let pointsInside = 0;
for (let i = 0; i < aabbPoints.length; i++) {
pointsInside += +(dot$5(plane, aabbPoints[i]) + plane[3] >= 0);
}
if (pointsInside === 0)
return 0;
if (pointsInside !== aabbPoints.length)
fullyInside = false;
}
return fullyInside ? 2 : 1;
}
function intersectsFrustumPrecise(frustum, aabbPoints) {
for (const proj of frustum.projections) {
const projectedAabb = projectPoints(aabbPoints, frustum.points[0], proj.axis);
if (proj.projection[1] < projectedAabb[0] || proj.projection[0] > projectedAabb[1]) {
return 0;
}
}
return 1;
}
const NEAR_TL = 0;
const NEAR_TR = 1;
const NEAR_BR = 2;
const NEAR_BL = 3;
const FAR_TL = 4;
const FAR_TR = 5;
const FAR_BR = 6;
const FAR_BL = 7;
function pointsInsideOfPlane(points, plane) {
let pointsInside = 0;
const p = [0, 0, 0, 0];
for (let i = 0; i < points.length; i++) {
p[0] = points[i][0];
p[1] = points[i][1];
p[2] = points[i][2];
p[3] = 1;
if (dot$4(p, plane) >= 0) {
pointsInside++;
}
}
return pointsInside;
}
class Frustum {
constructor(points_, planes_) {
this.points = points_ || new Array(8).fill([0, 0, 0]);
this.planes = planes_ || new Array(6).fill([0, 0, 0, 0]);
this.bounds = Aabb.fromPoints(this.points);
this.projections = [];
this.frustumEdges = [
sub$2([], this.points[NEAR_BR], this.points[NEAR_BL]),
sub$2([], this.points[NEAR_TL], this.points[NEAR_BL]),
sub$2([], this.points[FAR_TL], this.points[NEAR_TL]),
sub$2([], this.points[FAR_TR], this.points[NEAR_TR]),
sub$2([], this.points[FAR_BR], this.points[NEAR_BR]),
sub$2([], this.points[FAR_BL], this.points[NEAR_BL])
];
for (const edge of this.frustumEdges) {
const axis0 = [0, -edge[2], edge[1]];
const axis1 = [edge[2], 0, -edge[0]];
this.projections.push({
axis: axis0,
projection: projectPoints(this.points, this.points[0], axis0)
});
this.projections.push({
axis: axis1,
projection: projectPoints(this.points, this.points[0], axis1)
});
}
}
static fromInvProjectionMatrix(invProj, worldSize, zoom, zInMeters) {
const clipSpaceCorners = [
[-1, 1, -1, 1],
[1, 1, -1, 1],
[1, -1, -1, 1],
[-1, -1, -1, 1],
[-1, 1, 1, 1],
[1, 1, 1, 1],
[1, -1, 1, 1],
[-1, -1, 1, 1]
];
const scale = Math.pow(2, zoom);
const frustumCoords = clipSpaceCorners.map((v) => {
const s = transformMat4$1([], v, invProj);
const k = 1 / s[3] / worldSize * scale;
return mul$3(s, s, [k, k, zInMeters ? 1 / s[3] : k, k]);
});
const frustumPlanePointIndices = [
[NEAR_TL, NEAR_TR, NEAR_BR],
// near
[FAR_BR, FAR_TR, FAR_TL],
// far
[NEAR_TL, NEAR_BL, FAR_BL],
// left
[NEAR_BR, NEAR_TR, FAR_TR],
// right
[NEAR_BL, NEAR_BR, FAR_BR],
// bottom
[NEAR_TL, FAR_TL, FAR_TR]
// top
];
const frustumPlanes = frustumPlanePointIndices.map((p) => {
const a = sub$2([], frustumCoords[p[0]], frustumCoords[p[1]]);
const b = sub$2([], frustumCoords[p[2]], frustumCoords[p[1]]);
const n = normalize$4([], cross$2([], a, b));
const d = -dot$5(n, frustumCoords[p[1]]);
return n.concat(d);
});
const frustumPoints = [];
for (let i = 0; i < frustumCoords.length; i++) {
frustumPoints.push([frustumCoords[i][0], frustumCoords[i][1], frustumCoords[i][2]]);
}
return new Frustum(frustumPoints, frustumPlanes);
}
// Performs precise intersection test between the frustum and the provided convex hull.
// The hull consits of vertices, faces (defined as planes) and a list of edges.
// Intersection test is performed using separating axis theoreom.
intersectsPrecise(vertices, faces, edges) {
for (let i = 0; i < faces.length; i++) {
if (!pointsInsideOfPlane(vertices, faces[i])) {
return 0;
}
}
for (let i = 0; i < this.planes.length; i++) {
if (!pointsInsideOfPlane(vertices, this.planes[i])) {
return 0;
}
}
for (const edge of edges) {
for (const frustumEdge of this.frustumEdges) {
const axis = cross$2([], edge, frustumEdge);
const len = length$4(axis);
if (len === 0) {
continue;
}
scale$4(axis, axis, 1 / len);
const projA = projectPoints(this.points, this.points[0], axis);
const projB = projectPoints(vertices, this.points[0], axis);
if (projA[0] > projB[1] || projB[0] > projA[1]) {
return 0;
}
}
}
return 1;
}
containsPoint(point) {
for (const plane of this.planes) {
const normal = [plane[0], plane[1], plane[2]];
const distance = plane[3];
if (dot$5(normal, point) + distance < 0) {
return false;
}
}
return true;
}
}
class Aabb {
static fromPoints(points) {
const min = [Infinity, Infinity, Infinity];
const max = [-Infinity, -Infinity, -Infinity];
for (const p of points) {
min$3(min, min, p);
max$4(max, max, p);
}
return new Aabb(min, max);
}
static fromTileIdAndHeight(id, minHeight, maxHeight) {
const tiles = 1 << id.canonical.z;
const x = id.canonical.x;
const y = id.canonical.y;
return new Aabb([x / tiles, y / tiles, minHeight], [(x + 1) / tiles, (y + 1) / tiles, maxHeight]);
}
static applyTransform(aabb, transform) {
const corners = aabb.getCorners();
for (let i = 0; i < corners.length; ++i) {
transformMat4$2(corners[i], corners[i], transform);
}
return Aabb.fromPoints(corners);
}
// A fast version of applyTransform. Note that it breaks down for non-uniform
// scale and complex projection matrices.
static applyTransformFast(aabb, transform) {
const min = [transform[12], transform[13], transform[14]];
const max = [...min];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
const value = transform[j * 4 + i];
const a = value * aabb.min[j];
const b = value * aabb.max[j];
min[i] += Math.min(a, b);
max[i] += Math.max(a, b);
}
}
return new Aabb(min, max);
}
static projectAabbCorners(aabb, transform) {
const corners = aabb.getCorners();
for (let i = 0; i < corners.length; ++i) {
transformMat4$2(corners[i], corners[i], transform);
}
return corners;
}
constructor(min_, max_) {
this.min = min_;
this.max = max_;
this.center = scale$4([], add$4([], this.min, this.max), 0.5);
}
quadrant(index) {
const split = [index % 2 === 0, index < 2];
const qMin = clone$5(this.min);
const qMax = clone$5(this.max);
for (let axis = 0; axis < split.length; axis++) {
qMin[axis] = split[axis] ? this.min[axis] : this.center[axis];
qMax[axis] = split[axis] ? this.center[axis] : this.max[axis];
}
qMax[2] = this.max[2];
return new Aabb(qMin, qMax);
}
distanceX(point) {
const pointOnAabb = Math.max(Math.min(this.max[0], point[0]), this.min[0]);
return pointOnAabb - point[0];
}
distanceY(point) {
const pointOnAabb = Math.max(Math.min(this.max[1], point[1]), this.min[1]);
return pointOnAabb - point[1];
}
distanceZ(point) {
const pointOnAabb = Math.max(Math.min(this.max[2], point[2]), this.min[2]);
return pointOnAabb - point[2];
}
getCorners() {
const mn = this.min;
const mx = this.max;
return [
[mn[0], mn[1], mn[2]],
[mx[0], mn[1], mn[2]],
[mx[0], mx[1], mn[2]],
[mn[0], mx[1], mn[2]],
[mn[0], mn[1], mx[2]],
[mx[0], mn[1], mx[2]],
[mx[0], mx[1], mx[2]],
[mn[0], mx[1], mx[2]]
];
}
// Performs conservative intersection test using separating axis theorem.
// Some accuracy is traded for better performance. False positive rate is < 1%.
// Flat intersection test checks only x and y dimensions of the aabb.
// Returns 0 if there's no intersection, 1 if shapes are intersecting and
// 2 if the aabb if fully inside the frustum.
intersects(frustum) {
if (!this.intersectsAabb(frustum.bounds)) {
return 0;
}
return intersectsFrustum(frustum, this.getCorners());
}
intersectsFlat(frustum) {
if (!this.intersectsAabb(frustum.bounds)) {
return 0;
}
const aabbPoints = [
[this.min[0], this.min[1], 0],
[this.max[0], this.min[1], 0],
[this.max[0], this.max[1], 0],
[this.min[0], this.max[1], 0]
];
return intersectsFrustum(frustum, aabbPoints);
}
// Performs precise intersection test using separating axis theorem.
// It is possible run only edge cases that were not covered in intersects().
// Flat intersection test checks only x and y dimensions of the aabb.
intersectsPrecise(frustum, edgeCasesOnly) {
if (!edgeCasesOnly) {
const intersects = this.intersects(frustum);
if (!intersects) {
return 0;
}
}
return intersectsFrustumPrecise(frustum, this.getCorners());
}
intersectsPreciseFlat(frustum, edgeCasesOnly) {
if (!edgeCasesOnly) {
const intersects = this.intersectsFlat(frustum);
if (!intersects) {
return 0;
}
}
const aabbPoints = [
[this.min[0], this.min[1], 0],
[this.max[0], this.min[1], 0],
[this.max[0], this.max[1], 0],
[this.min[0], this.max[1], 0]
];
return intersectsFrustumPrecise(frustum, aabbPoints);
}
intersectsAabb(aabb) {
for (let axis = 0; axis < 3; ++axis) {
if (this.min[axis] > aabb.max[axis] || aabb.min[axis] > this.max[axis]) {
return false;
}
}
return true;
}
intersectsAabbXY(aabb) {
if (this.min[0] > aabb.max[0] || aabb.min[0] > this.max[0]) {
return false;
}
if (this.min[1] > aabb.max[1] || aabb.min[1] > this.max[1]) {
return false;
}
return true;
}
encapsulate(aabb) {
for (let i = 0; i < 3; i++) {
this.min[i] = Math.min(this.min[i], aabb.min[i]);
this.max[i] = Math.max(this.max[i], aabb.max[i]);
}
}
encapsulatePoint(point) {
for (let i = 0; i < 3; i++) {
this.min[i] = Math.min(this.min[i], point[i]);
this.max[i] = Math.max(this.max[i], point[i]);
}
}
closestPoint(point) {
return [
Math.max(Math.min(this.max[0], point[0]), this.min[0]),
Math.max(Math.min(this.max[1], point[1]), this.min[1]),
Math.max(Math.min(this.max[2], point[2]), this.min[2])
];
}
}
register(Aabb, "Aabb");
const TUNNEL_THRESHOLD_METERS = 5;
class EdgeIterator {
constructor(feature, metersToTile) {
this.feature = feature;
this.metersToTile = metersToTile;
this.index = 0;
}
get() {
assert$1(this.index < this.feature.vertices.length);
const vertex = this.feature.vertices[this.index];
const dir = this.feature.vertexProps[this.index].dir;
const perpX = dir[1];
const perpY = -dir[0];
const dist = (vertex.extent + 1) * this.metersToTile;
const a = new Point(Math.trunc(vertex.position[0] + perpX * dist), Math.trunc(vertex.position[1] + perpY * dist));
const b = new Point(Math.trunc(vertex.position[0] - perpX * dist), Math.trunc(vertex.position[1] - perpY * dist));
return [a, b];
}
next() {
this.index++;
}
valid() {
return this.index < this.feature.vertices.length;
}
}
class ElevationFeature {
constructor(id, safeArea, constantHeight, vertices, edges, metersToTile) {
this.vertices = new Array();
this.vertexProps = new Array();
this.edges = new Array();
this.edgeProps = new Array();
this._tmpVec2 = [create(), create(), create(), create(), create(), create(), create()];
assert$1(constantHeight == null ? vertices != null && edges != null && metersToTile != null : true);
this.id = id;
this.heightRange = { min: constantHeight, max: constantHeight };
this.safeArea = safeArea;
this.constantHeight = constantHeight;
if (this.constantHeight != null || this.constantHeight == null && vertices.length === 0) return;
this.vertices = vertices;
this.edges = edges;
this.edges = this.edges.filter(
(edge) => edge.a < this.vertices.length && edge.b < this.vertices.length && !exactEquals(this.vertices[edge.a].position, this.vertices[edge.b].position)
);
this.heightRange = { min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY };
for (const vertex of this.vertices) {
this.vertexProps.push({ dir: fromValues(0, 0) });
this.heightRange.min = Math.min(this.heightRange.min, vertex.height);
this.heightRange.max = Math.max(this.heightRange.max, vertex.height);
}
for (const edge of this.edges) {
const a = this.vertices[edge.a].position;
const b = this.vertices[edge.b].position;
const vec = subtract(create(), b, a);
const len = length(vec);
const dir = scale(create(), vec, 1 / len);
this.edgeProps.push({ vec, dir, len });
const dirA = this.vertexProps[edge.a].dir;
const dirB = this.vertexProps[edge.b].dir;
add(dirA, dirA, dir);
add(dirB, dirB, dir);
}
for (const props of this.vertexProps) {
if (props.dir[0] !== 0 || props.dir[1] !== 0) {
normalize(props.dir, props.dir);
}
}
this.tessellate(metersToTile);
}
// Sample point at the provided location
pointElevation(point) {
if (this.constantHeight != null) {
return this.constantHeight;
}
const closestEdge = this.getClosestEdge(point);
if (closestEdge == null) {
return 0;
}
const [idx, t] = closestEdge;
const aIdx = this.edges[idx].a;
const bIdx = this.edges[idx].b;
return number(this.vertices[aIdx].height, this.vertices[bIdx].height, t);
}
computeSlopeNormal(point, metersToTile) {
const closestEdge = this.getClosestEdge(point);
if (!closestEdge) return fromValues$4(0, 0, 1);
const edgeIdx = closestEdge[0];
const edge = this.edges[edgeIdx];
const a = this.vertices[edge.a];
const b = this.vertices[edge.b];
const vec = this.edgeProps[edgeIdx].vec;
const edgeVec = fromValues$4(vec[0], vec[1], (b.height - a.height) * metersToTile);
const norm = fromValues$4(edgeVec[1], -edgeVec[0], 0);
cross$2(norm, norm, edgeVec);
const len = length$4(norm);
return len > 0 ? scale$4(norm, norm, 1 / len) : set$4(norm, 0, 0, 1);
}
// Safe area describes original tile boundaries of the elevation curve scaled to the current zoom level.
// This is useful in cases where e.g. a continuous bridge that's been initially split into adjacent tiles,
// and hence into two different elevation features, is cojoined into one on a lower zoom level tile.
getSafeArea() {
return this.safeArea;
}
// Returns true whether this elevation feature describes a tunnel
isTunnel() {
return this.heightRange.max <= -TUNNEL_THRESHOLD_METERS;
}
getClosestEdge(point) {
if (this.edges.length === 0) {
return void 0;
}
let closestIdx = 0;
let closestDist = Number.POSITIVE_INFINITY;
let closestT = 0;
const [pa, pb, papb, paPoint, aPoint, perpDir, pointVec] = this._tmpVec2;
set(pointVec, point.x, point.y);
const ray = new Ray2D(pointVec, null);
for (let i = 0; i < this.edges.length; i++) {
const edge = this.edges[i];
const edgeDir = this.edgeProps[i].dir;
ray.dir = edgeDir;
const a = this.vertices[edge.a].position;
const b = this.vertices[edge.b].position;
const paResult = ray.intersectsPlane(a, this.vertexProps[edge.a].dir, pa);
const pbResult = ray.intersectsPlane(b, this.vertexProps[edge.b].dir, pb);
if (!paResult || !pbResult) {
continue;
}
subtract(papb, pb, pa);
subtract(paPoint, pointVec, pa);
const papbLen = dot$1(papb, papb);
const t = papbLen > 0 ? dot$1(paPoint, papb) / papbLen : 0;
const clampedT = clamp(t, 0, 1);
const distAlongLine = Math.abs((t - clampedT) * this.edgeProps[i].len);
subtract(aPoint, pointVec, a);
set(perpDir, edgeDir[1], -edgeDir[0]);
const perpDist = Math.abs(dot$1(aPoint, perpDir));
const dist = distAlongLine + perpDist;
if (dist < closestDist) {
closestIdx = i;
closestDist = dist;
closestT = clampedT;
}
}
return [closestIdx, closestT];
}
tessellate(metersToTile) {
const splitDistanceThreshold = MARKUP_ELEVATION_BIAS;
const aPos = create$4();
const bPos = create$4();
const aPerp = create$4();
const bPerp = create$4();
for (let i = this.edges.length - 1; i >= 0; --i) {
const a = this.edges[i].a;
const b = this.edges[i].b;
const { position: aTilePos, height: aHeight, extent: aExtent } = this.vertices[a];
const { position: bTilePos, height: bHeight, extent: bExtent } = this.vertices[b];
const aDir = this.vertexProps[a].dir;
const bDir = this.vertexProps[b].dir;
set$4(aPos, aTilePos[0] / metersToTile, aTilePos[1] / metersToTile, aHeight);
set$4(bPos, bTilePos[0] / metersToTile, bTilePos[1] / metersToTile, bHeight);
set$4(aPerp, aDir[1], -aDir[0], 0);
scale$4(aPerp, aPerp, aExtent);
set$4(bPerp, bDir[1], -bDir[0], 0);
scale$4(bPerp, bPerp, bExtent);
const lineDistSq = this.distSqLines(
fromValues$4(aPos[0] + 0.5 * aPerp[0], aPos[1] + 0.5 * aPerp[1], aPos[2] + 0.5 * aPerp[2]),
fromValues$4(bPos[0] - 0.5 * bPerp[0], bPos[1] - 0.5 * bPerp[1], bPos[2] - 0.5 * bPerp[2]),
fromValues$4(aPos[0] - 0.5 * aPerp[0], aPos[1] - 0.5 * aPerp[1], aPos[2] - 0.5 * aPerp[2]),
fromValues$4(bPos[0] + 0.5 * bPerp[0], bPos[1] + 0.5 * bPerp[1], bPos[2] + 0.5 * bPerp[2])
);
if (lineDistSq <= splitDistanceThreshold * splitDistanceThreshold) {
continue;
}
const mid = this.vertices.length;
const pos = add(create(), aTilePos, bTilePos);
this.vertices.push({
position: scale(pos, pos, 0.5),
height: 0.5 * (aHeight + bHeight),
extent: 0.5 * (aExtent + bExtent)
});
const dir = add(create(), aDir, bDir);
this.vertexProps.push({ dir: normalize(dir, dir) });
this.edges.splice(i, 1);
this.edgeProps.splice(i, 1);
this.edges.push({ a, b: mid });
this.edges.push({ a: mid, b });
const edgeVec = subtract(create(), this.vertices[mid].position, aTilePos);
const edgeLen = length(edgeVec);
const edgeDir = scale(create(), edgeVec, 1 / edgeLen);
const props = {
vec: edgeVec,
dir: edgeDir,
len: edgeLen
};
this.edgeProps.push(props);
this.edgeProps.push(props);
assert$1(this.edgeProps.length === this.edges.length);
assert$1(this.vertexProps.length === this.vertices.length);
assert$1(this.edges.every((e) => e.a < this.vertices.length && e.b < this.vertices.length));
}
}
distSqLines(aStart, aEnd, bStart, bEnd) {
const aVec = subtract$2(create$4(), aEnd, aStart);
const bVec = subtract$2(create$4(), bEnd, bStart);
const abVec = subtract$2(create$4(), aStart, bStart);
const a = dot$5(aVec, aVec);
const b = dot$5(aVec, bVec);
const c = dot$5(aVec, abVec);
const d = dot$5(bVec, bVec);
const e = dot$5(bVec, abVec);
const det = a * d - b * b;
if (det === 0) {
const t2 = dot$5(abVec, bVec) / dot$5(bVec, bVec);
const vec = lerp$5(aVec, bStart, bEnd, t2);
return squaredDistance$2(vec, aStart);
}
const s = (b * e - c * d) / det;
const t = (a * e - b * c) / det;
const vecA = lerp$5(aVec, aStart, aEnd, s);
const vecB = lerp$5(bVec, bStart, bEnd, t);
return squaredDistance$2(vecA, vecB);
}
}
class ElevationFeatures {
static parseFrom(data, tileID) {
const parsedFeatures = ElevationFeatureParser.parse(data);
if (!parsedFeatures) {
return [];
}
let { vertices, features } = parsedFeatures;
const metersToTile = 1 / tileToMeter(tileID);
features.sort((a, b) => a.id - b.id);
vertices.sort((a, b) => a.id - b.id || a.idx - b.idx);
vertices = vertices.filter(
(value, index, self) => index === self.findIndex((t) => t.id === value.id && t.idx === value.idx)
);
const elevationFeatures = new Array();
let vCurrent = 0;
const vEnd = vertices.length;
for (const feature of features) {
if (feature.constantHeight) {
elevationFeatures.push(new ElevationFeature(feature.id, feature.bounds, feature.constantHeight));
continue;
}
while (vCurrent !== vEnd && vertices[vCurrent].id < feature.id) {
vCurrent++;
}
if (vCurrent === vEnd || vertices[vCurrent].id !== feature.id) {
continue;
}
const outVertices = new Array();
const outEdges = new Array();
const vFirst = vCurrent;
while (vCurrent !== vEnd && vertices[vCurrent].id === feature.id) {
const vertex = vertices[vCurrent];
outVertices.push({ position: vertex.position, height: vertex.height, extent: vertex.extent });
if (vCurrent !== vFirst && vertices[vCurrent - 1].idx === vertex.idx - 1) {
const idx = vCurrent - vFirst;
outEdges.push({ a: idx - 1, b: idx });
}
vCurrent++;
}
elevationFeatures.push(new ElevationFeature(
feature.id,
feature.bounds,
void 0,
outVertices,
outEdges,
metersToTile
));
}
assert$1(elevationFeatures.every((feature, index, array) => index === 0 || array[index - 1].id <= feature.id));
return elevationFeatures;
}
static getElevationFeature(feature, elevationFeatures) {
if (!elevationFeatures) return void 0;
const value = +feature.properties[PROPERTY_ELEVATION_ID];
if (Number.isNaN(value)) return void 0;
return elevationFeatures.find((f) => f.id === value);
}
}
class ElevationFeatureSampler {
constructor(sampleTileId, elevationTileId) {
this.zScale = 1;
this.xOffset = 0;
this.yOffset = 0;
if (sampleTileId.equals(elevationTileId)) return;
this.zScale = Math.pow(2, elevationTileId.z - sampleTileId.z);
this.xOffset = (sampleTileId.x * this.zScale - elevationTileId.x) * EXTENT;
this.yOffset = (sampleTileId.y * this.zScale - elevationTileId.y) * EXTENT;
}
constantElevation(elevation, bias) {
if (elevation.constantHeight == null) return void 0;
return this.computeBiasedHeight(elevation.constantHeight, bias);
}
pointElevation(point, elevation, bias) {
const constantHeight = this.constantElevation(elevation, bias);
if (constantHeight != null) {
return constantHeight;
}
point.x = point.x * this.zScale + this.xOffset;
point.y = point.y * this.zScale + this.yOffset;
return this.computeBiasedHeight(elevation.pointElevation(point), bias);
}
computeBiasedHeight(height, bias) {
if (bias <= 0) return height;
const stepHeight = height >= 0 ? height : Math.abs(0.5 * height);
return height + bias * smoothstep(0, bias, stepHeight);
}
}
register(ElevationFeature, "ElevationFeature");
class CircleBucket {
constructor(options) {
this.zoom = options.zoom;
this.overscaling = options.overscaling;
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.hasPattern = false;
this.projection = options.projection;
this.layoutVertexArray = new StructArrayLayout2i4();
this.indexArray = new StructArrayLayout3ui6();
this.segments = new SegmentVector();
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut });
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.elevationMode = this.layers[0].layout.get("circle-elevation-reference");
this.hasElevation = false;
if (this.elevationMode !== "none") {
this.elevatedLayoutVertexArray = new StructArrayLayout1f4();
}
this.worldview = options.worldview;
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
const styleLayer = this.layers[0];
const bucketFeatures = [];
let circleSortKey = null;
if (styleLayer.type === "circle") {
circleSortKey = styleLayer.layout.get("circle-sort-key");
}
for (const { feature, id, index, sourceLayerIndex } of features) {
const needGeometry = this.layers[0]._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const sortKey = circleSortKey ? (
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
circleSortKey.evaluate(evaluationFeature, {}, canonical)
) : void 0;
const bucketFeature = {
id,
properties: feature.properties,
type: feature.type,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
patterns: {},
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sortKey
};
bucketFeatures.push(bucketFeature);
}
if (circleSortKey) {
bucketFeatures.sort((a, b) => {
return a.sortKey - b.sortKey;
});
}
let globeProjection = null;
if (tileTransform.projection.name === "globe") {
this.globeExtVertexArray = new StructArrayLayout6i12();
globeProjection = tileTransform.projection;
}
for (const bucketFeature of bucketFeatures) {
const { geometry, index, sourceLayerIndex } = bucketFeature;
const feature = features[index].feature;
this.addFeature(bucketFeature, geometry, index, options.availableImages, canonical, globeProjection, options.brightness, options.elevationFeatures);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
}
if (!this.hasElevation) {
this.elevatedLayoutVertexArray = void 0;
}
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness) {
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, this.worldview);
}
isEmpty() {
return this.layoutVertexArray.length === 0;
}
uploadPending() {
return !this.uploaded || this.programConfigurations.needsUpload;
}
upload(context) {
if (!this.uploaded) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, circleAttributes.members);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
if (this.globeExtVertexArray) {
this.globeExtVertexBuffer = context.createVertexBuffer(this.globeExtVertexArray, circleGlobeAttributesExt.members);
}
if (this.elevatedLayoutVertexArray) {
assert$1(this.layoutVertexArray.length === this.elevatedLayoutVertexArray.length);
this.elevatedLayoutVertexBuffer = context.createVertexBuffer(this.elevatedLayoutVertexArray, circleAttributesExt.members);
}
}
this.programConfigurations.upload(context);
this.uploaded = true;
}
destroy() {
if (!this.layoutVertexBuffer) return;
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.programConfigurations.destroy();
this.segments.destroy();
if (this.globeExtVertexBuffer) {
this.globeExtVertexBuffer.destroy();
}
if (this.elevatedLayoutVertexBuffer) {
this.elevatedLayoutVertexBuffer.destroy();
}
}
addFeature(feature, geometry, index, availableImages, canonical, projection, brightness, elevationFeatures) {
let tiledElevation;
if (this.elevationMode !== "none") {
tiledElevation = ElevationFeatures.getElevationFeature(feature, elevationFeatures);
}
for (const ring of geometry) {
for (const point of ring) {
const x = point.x;
const y = point.y;
if (x < 0 || x >= EXTENT || y < 0 || y >= EXTENT) continue;
if (projection) {
const projectedPoint = projection.projectTilePoint(x, y, canonical);
const normal = projection.upVector(canonical, x, y);
this.addGlobeExtVertex(projectedPoint, normal);
this.addGlobeExtVertex(projectedPoint, normal);
this.addGlobeExtVertex(projectedPoint, normal);
this.addGlobeExtVertex(projectedPoint, normal);
}
const segment = this.segments.prepareSegment(4, this.layoutVertexArray, this.indexArray, feature.sortKey);
const index2 = segment.vertexLength;
this.addCircleVertex(x, y, -1, -1);
this.addCircleVertex(x, y, 1, -1);
this.addCircleVertex(x, y, 1, 1);
this.addCircleVertex(x, y, -1, 1);
if (this.elevationMode !== "none") {
const z = tiledElevation ? tiledElevation.pointElevation(new Point(x, y)) : 0;
this.hasElevation = this.hasElevation || z !== 0;
for (let i = 0; i < 4; i++) {
this.elevatedLayoutVertexArray.emplaceBack(z);
}
}
this.indexArray.emplaceBack(index2, index2 + 1, index2 + 2);
this.indexArray.emplaceBack(index2, index2 + 2, index2 + 3);
segment.vertexLength += 4;
segment.primitiveLength += 2;
}
}
this.programConfigurations.populatePaintArrays(this.layoutVertexArray.length, feature, index, {}, availableImages, canonical, brightness, void 0, this.worldview);
}
addCircleVertex(x, y, extrudeX, extrudeY) {
const circleX = x * 2 + (extrudeX + 1) / 2;
const circleY = y * 2 + (extrudeY + 1) / 2;
this.layoutVertexArray.emplaceBack(circleX, circleY);
}
addGlobeExtVertex(pos, normal) {
const encode = 1 << 14;
this.globeExtVertexArray.emplaceBack(
pos.x,
pos.y,
pos.z,
normal[0] * encode,
normal[1] * encode,
normal[2] * encode
);
}
}
register(CircleBucket, "CircleBucket", { omit: ["layers"] });
function polygonIntersectsPolygon(polygonA, polygonB) {
for (let i = 0; i < polygonA.length; i++) {
if (polygonContainsPoint(polygonB, polygonA[i])) return true;
}
for (let i = 0; i < polygonB.length; i++) {
if (polygonContainsPoint(polygonA, polygonB[i])) return true;
}
if (lineIntersectsLine(polygonA, polygonB)) return true;
return false;
}
function polygonIntersectsBufferedPoint(polygon, point, radius) {
if (polygonContainsPoint(polygon, point)) return true;
if (pointIntersectsBufferedLine(point, polygon, radius)) return true;
return false;
}
function polygonIntersectsMultiPolygon(polygon, multiPolygon) {
if (polygon.length === 1) {
return multiPolygonContainsPoint(multiPolygon, polygon[0]);
}
for (let m = 0; m < multiPolygon.length; m++) {
const ring = multiPolygon[m];
for (let n = 0; n < ring.length; n++) {
if (polygonContainsPoint(polygon, ring[n])) return true;
}
}
for (let i = 0; i < polygon.length; i++) {
if (multiPolygonContainsPoint(multiPolygon, polygon[i])) return true;
}
for (let k = 0; k < multiPolygon.length; k++) {
if (lineIntersectsLine(polygon, multiPolygon[k])) return true;
}
return false;
}
function polygonIntersectsBufferedMultiLine(polygon, multiLine, radius) {
for (let i = 0; i < multiLine.length; i++) {
const line = multiLine[i];
if (polygon.length >= 3) {
for (let k = 0; k < line.length; k++) {
if (polygonContainsPoint(polygon, line[k])) return true;
}
}
if (lineIntersectsBufferedLine(polygon, line, radius)) return true;
}
return false;
}
function lineIntersectsBufferedLine(lineA, lineB, radius) {
if (lineA.length > 1) {
if (lineIntersectsLine(lineA, lineB)) return true;
for (let j = 0; j < lineB.length; j++) {
if (pointIntersectsBufferedLine(lineB[j], lineA, radius)) return true;
}
}
for (let k = 0; k < lineA.length; k++) {
if (pointIntersectsBufferedLine(lineA[k], lineB, radius)) return true;
}
return false;
}
function lineIntersectsLine(lineA, lineB) {
if (lineA.length === 0 || lineB.length === 0) return false;
for (let i = 0; i < lineA.length - 1; i++) {
const a0 = lineA[i];
const a1 = lineA[i + 1];
for (let j = 0; j < lineB.length - 1; j++) {
const b0 = lineB[j];
const b1 = lineB[j + 1];
if (lineSegmentIntersectsLineSegment(a0, a1, b0, b1)) return true;
}
}
return false;
}
function lineSegmentIntersectsLineSegment(a0, a1, b0, b1) {
return isCounterClockwise(a0, b0, b1) !== isCounterClockwise(a1, b0, b1) && isCounterClockwise(a0, a1, b0) !== isCounterClockwise(a0, a1, b1);
}
function signedAreaTriangle(a, b, c) {
return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);
}
function segmentSegmentIntersection(a0, a1, b0, b1) {
const area0 = signedAreaTriangle(a0, a1, b1);
const area1 = signedAreaTriangle(a0, a1, b0);
if (Math.sign(area0) === Math.sign(area1)) {
return void 0;
}
const area2 = signedAreaTriangle(b0, b1, a0);
const area3 = area2 + area1 - area0;
if (Math.sign(area2) === Math.sign(area3)) {
return void 0;
}
return [area2 / (area2 - area3), area1 / (area1 - area0)];
}
function pointIntersectsBufferedLine(p, line, radius) {
const radiusSquared = radius * radius;
if (line.length === 1) return p.distSqr(line[0]) < radiusSquared;
for (let i = 1; i < line.length; i++) {
const v = line[i - 1], w = line[i];
if (distToSegmentSquared(p, v, w) < radiusSquared) return true;
}
return false;
}
function distToSegmentSquared(p, v, w) {
const l2 = v.distSqr(w);
if (l2 === 0) return p.distSqr(v);
const t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2;
if (t < 0) return p.distSqr(v);
if (t > 1) return p.distSqr(w);
return p.distSqr(w.sub(v)._mult(t)._add(v));
}
function multiPolygonContainsPoint(rings, p) {
let c = false;
let ring;
let p1;
let p2;
for (let k = 0; k < rings.length; k++) {
ring = rings[k];
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
p1 = ring[i];
p2 = ring[j];
if (p1.y > p.y !== p2.y > p.y && p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x) {
c = !c;
}
}
}
return c;
}
function polygonContainsPoint(ring, p) {
let c = false;
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const p1 = ring[i];
const p2 = ring[j];
if (p1.y > p.y !== p2.y > p.y && p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x) {
c = !c;
}
}
return c;
}
function polygonIntersectsBox(ring, boxX1, boxY1, boxX2, boxY2) {
for (const p of ring) {
if (boxX1 <= p.x && boxY1 <= p.y && boxX2 >= p.x && boxY2 >= p.y) return true;
}
const corners = [
new Point(boxX1, boxY1),
new Point(boxX1, boxY2),
new Point(boxX2, boxY2),
new Point(boxX2, boxY1)
];
if (ring.length > 2) {
for (const corner of corners) {
if (polygonContainsPoint(ring, corner)) return true;
}
}
for (let i = 0; i < ring.length - 1; i++) {
const p1 = ring[i];
const p2 = ring[i + 1];
if (edgeIntersectsBox(p1, p2, corners)) return true;
}
return false;
}
function edgeIntersectsBox(e1, e2, corners) {
const tl = corners[0];
const br = corners[2];
if (e1.x < tl.x && e2.x < tl.x || e1.x > br.x && e2.x > br.x || e1.y < tl.y && e2.y < tl.y || e1.y > br.y && e2.y > br.y) return false;
const dir = isCounterClockwise(e1, e2, corners[0]);
return dir !== isCounterClockwise(e1, e2, corners[1]) || dir !== isCounterClockwise(e1, e2, corners[2]) || dir !== isCounterClockwise(e1, e2, corners[3]);
}
function triangleLeftSideOfEdge(a, b, p0, p1, p2, padding) {
let nx = b.y - a.y;
let ny = a.x - b.x;
padding = padding || 0;
if (padding) {
const nLenSq = nx * nx + ny * ny;
if (nLenSq === 0) {
return true;
}
const len = Math.sqrt(nLenSq);
nx /= len;
ny /= len;
}
if ((p0.x - a.x) * nx + (p0.y - a.y) * ny - padding < 0) {
return false;
} else if ((p1.x - a.x) * nx + (p1.y - a.y) * ny - padding < 0) {
return false;
} else if ((p2.x - a.x) * nx + (p2.y - a.y) * ny - padding < 0) {
return false;
}
return true;
}
function triangleIntersectsTriangle(a0, b0, c0, a1, b1, c1, padding) {
if (triangleLeftSideOfEdge(a0, b0, a1, b1, c1, padding)) {
return false;
} else if (triangleLeftSideOfEdge(b0, c0, a1, b1, c1, padding)) {
return false;
} else if (triangleLeftSideOfEdge(c0, a0, a1, b1, c1, padding)) {
return false;
} else if (triangleLeftSideOfEdge(a1, b1, a0, b0, c0, padding)) {
return false;
} else if (triangleLeftSideOfEdge(b1, c1, a0, b0, c0, padding)) {
return false;
} else if (triangleLeftSideOfEdge(c1, a1, a0, b0, c0, padding)) {
return false;
}
return true;
}
function getMaximumPaintValue(property, layer, bucket) {
const value = layer.paint.get(property).value;
if (value.kind === "constant") {
return value.value;
} else {
return bucket.programConfigurations.get(layer.id).getMaxValue(property);
}
}
function translateDistance(translate2) {
return Math.sqrt(translate2[0] * translate2[0] + translate2[1] * translate2[1]);
}
function translate(queryGeometry, translate2, translateAnchor, bearing, pixelsToTileUnits) {
if (!translate2[0] && !translate2[1]) {
return queryGeometry;
}
const pt = Point.convert(translate2)._mult(pixelsToTileUnits);
if (translateAnchor === "viewport") {
pt._rotate(-bearing);
}
const translated = [];
for (let i = 0; i < queryGeometry.length; i++) {
const point = queryGeometry[i];
translated.push(point.sub(pt));
}
return translated;
}
function tilespaceTranslate(translate2, translateAnchor, bearing, pixelsToTileUnits) {
const pt = Point.convert(translate2)._mult(pixelsToTileUnits);
if (translateAnchor === "viewport") {
pt._rotate(-bearing);
}
return pt;
}
let layout$e;
const getLayoutProperties$e = () => layout$e || (layout$e = new Properties({
"circle-sort-key": new DataDrivenProperty(spec["layout_circle"]["circle-sort-key"]),
"circle-elevation-reference": new DataConstantProperty(spec["layout_circle"]["circle-elevation-reference"]),
"visibility": new DataConstantProperty(spec["layout_circle"]["visibility"])
}));
let paint$e;
const getPaintProperties$e = () => paint$e || (paint$e = new Properties({
"circle-radius": new DataDrivenProperty(spec["paint_circle"]["circle-radius"]),
"circle-color": new DataDrivenProperty(spec["paint_circle"]["circle-color"]),
"circle-blur": new DataDrivenProperty(spec["paint_circle"]["circle-blur"]),
"circle-opacity": new DataDrivenProperty(spec["paint_circle"]["circle-opacity"]),
"circle-translate": new DataConstantProperty(spec["paint_circle"]["circle-translate"]),
"circle-translate-anchor": new DataConstantProperty(spec["paint_circle"]["circle-translate-anchor"]),
"circle-pitch-scale": new DataConstantProperty(spec["paint_circle"]["circle-pitch-scale"]),
"circle-pitch-alignment": new DataConstantProperty(spec["paint_circle"]["circle-pitch-alignment"]),
"circle-stroke-width": new DataDrivenProperty(spec["paint_circle"]["circle-stroke-width"]),
"circle-stroke-color": new DataDrivenProperty(spec["paint_circle"]["circle-stroke-color"]),
"circle-stroke-opacity": new DataDrivenProperty(spec["paint_circle"]["circle-stroke-opacity"]),
"circle-emissive-strength": new DataConstantProperty(spec["paint_circle"]["circle-emissive-strength"]),
"circle-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"circle-stroke-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
/**
* getURL
*
* @param {String} baseUrl Base url of the WMS server
* @param {String} layer Layer name
* @param {Number} x Tile coordinate x
* @param {Number} y Tile coordinate y
* @param {Number} z Tile zoom
* @param {Object} [options]
* @param {String} [options.format='image/png']
* @param {String} [options.service='WMS']
* @param {String} [options.version='1.1.1']
* @param {String} [options.request='GetMap']
* @param {String} [options.srs='EPSG:3857']
* @param {Number} [options.width='256']
* @param {Number} [options.height='256']
* @returns {String} url
* @example
* var baseUrl = 'http://geodata.state.nj.us/imagerywms/Natural2015';
* var layer = 'Natural2015';
* var url = whoots.getURL(baseUrl, layer, 154308, 197167, 19);
*/
function getURL(baseUrl, layer, x, y, z, options) {
options = options || {};
var url = baseUrl + '?' + [
'bbox=' + getTileBBox(x, y, z),
'format=' + (options.format || 'image/png'),
'service=' + (options.service || 'WMS'),
'version=' + (options.version || '1.1.1'),
'request=' + (options.request || 'GetMap'),
'srs=' + (options.srs || 'EPSG:3857'),
'width=' + (options.width || 256),
'height=' + (options.height || 256),
'layers=' + layer
].join('&');
return url;
}
/**
* getTileBBox
*
* @param {Number} x Tile coordinate x
* @param {Number} y Tile coordinate y
* @param {Number} z Tile zoom
* @returns {String} String of the bounding box
*/
function getTileBBox(x, y, z) {
// for Google/OSM tile scheme we need to alter the y
y = (Math.pow(2, z) - y - 1);
var min = getMercCoords(x * 256, y * 256, z),
max = getMercCoords((x + 1) * 256, (y + 1) * 256, z);
return min[0] + ',' + min[1] + ',' + max[0] + ',' + max[1];
}
/**
* getMercCoords
*
* @param {Number} x Pixel coordinate x
* @param {Number} y Pixel coordinate y
* @param {Number} z Tile zoom
* @returns {Array} [x, y]
*/
function getMercCoords(x, y, z) {
var resolution = (2 * Math.PI * 6378137 / 256) / Math.pow(2, z),
merc_x = (x * resolution - 2 * Math.PI * 6378137 / 2.0),
merc_y = (y * resolution - 2 * Math.PI * 6378137 / 2.0);
return [merc_x, merc_y];
}
class CanonicalTileID {
constructor(z, x, y) {
assert$1(z >= 0 && z <= 25);
assert$1(x >= 0 && x < Math.pow(2, z));
assert$1(y >= 0 && y < Math.pow(2, z));
this.z = z;
this.x = x;
this.y = y;
this.key = calculateKey(0, z, z, x, y);
}
equals(id) {
return this.z === id.z && this.x === id.x && this.y === id.y;
}
isChildOf(parent) {
const zDifference = this.z - parent.z;
return parent.z === 0 || parent.z < this.z && parent.x === this.x >> zDifference && parent.y === this.y >> zDifference;
}
// given a list of urls, choose a url template and return a tile URL
url(urls, scheme) {
const bbox = getTileBBox(this.x, this.y, this.z);
const quadkey = getQuadkey(this.z, this.x, this.y);
return urls[(this.x + this.y) % urls.length].replace("{prefix}", (this.x % 16).toString(16) + (this.y % 16).toString(16)).replace(/{z}/g, String(this.z)).replace(/{x}/g, String(this.x)).replace(/{y}/g, String(scheme === "tms" ? Math.pow(2, this.z) - this.y - 1 : this.y)).replace("{quadkey}", quadkey).replace("{bbox-epsg-3857}", bbox);
}
toString() {
return `${this.z}/${this.x}/${this.y}`;
}
}
class UnwrappedTileID {
constructor(wrap, canonical) {
this.wrap = wrap;
this.canonical = canonical;
this.key = calculateKey(wrap, canonical.z, canonical.z, canonical.x, canonical.y);
}
}
class OverscaledTileID {
constructor(overscaledZ, wrap, z, x, y) {
assert$1(overscaledZ >= z);
this.overscaledZ = overscaledZ;
this.wrap = wrap;
this.canonical = new CanonicalTileID(z, +x, +y);
this.key = wrap === 0 && overscaledZ === z ? this.canonical.key : calculateKey(wrap, overscaledZ, z, x, y);
}
equals(id) {
return this.overscaledZ === id.overscaledZ && this.wrap === id.wrap && this.canonical.equals(id.canonical);
}
scaledTo(targetZ) {
assert$1(targetZ <= this.overscaledZ);
const zDifference = this.canonical.z - targetZ;
if (targetZ > this.canonical.z) {
return new OverscaledTileID(targetZ, this.wrap, this.canonical.z, this.canonical.x, this.canonical.y);
} else {
return new OverscaledTileID(targetZ, this.wrap, targetZ, this.canonical.x >> zDifference, this.canonical.y >> zDifference);
}
}
/*
* calculateScaledKey is an optimization:
* when withWrap == true, implements the same as this.scaledTo(z).key,
* when withWrap == false, implements the same as this.scaledTo(z).wrapped().key.
*/
calculateScaledKey(targetZ, withWrap = true) {
if (this.overscaledZ === targetZ && withWrap) return this.key;
if (targetZ > this.canonical.z) {
return calculateKey(this.wrap * +withWrap, targetZ, this.canonical.z, this.canonical.x, this.canonical.y);
} else {
const zDifference = this.canonical.z - targetZ;
return calculateKey(this.wrap * +withWrap, targetZ, targetZ, this.canonical.x >> zDifference, this.canonical.y >> zDifference);
}
}
isChildOf(parent) {
if (parent.wrap !== this.wrap) {
return false;
}
const zDifference = this.canonical.z - parent.canonical.z;
return parent.overscaledZ === 0 || parent.overscaledZ < this.overscaledZ && parent.canonical.z < this.canonical.z && parent.canonical.x === this.canonical.x >> zDifference && parent.canonical.y === this.canonical.y >> zDifference;
}
children(sourceMaxZoom) {
if (this.overscaledZ >= sourceMaxZoom) {
return [new OverscaledTileID(this.overscaledZ + 1, this.wrap, this.canonical.z, this.canonical.x, this.canonical.y)];
}
const z = this.canonical.z + 1;
const x = this.canonical.x * 2;
const y = this.canonical.y * 2;
return [
new OverscaledTileID(z, this.wrap, z, x, y),
new OverscaledTileID(z, this.wrap, z, x + 1, y),
new OverscaledTileID(z, this.wrap, z, x, y + 1),
new OverscaledTileID(z, this.wrap, z, x + 1, y + 1)
];
}
isLessThan(rhs) {
if (this.wrap < rhs.wrap) return true;
if (this.wrap > rhs.wrap) return false;
if (this.overscaledZ < rhs.overscaledZ) return true;
if (this.overscaledZ > rhs.overscaledZ) return false;
if (this.canonical.x < rhs.canonical.x) return true;
if (this.canonical.x > rhs.canonical.x) return false;
if (this.canonical.y < rhs.canonical.y) return true;
return false;
}
wrapped() {
return new OverscaledTileID(this.overscaledZ, 0, this.canonical.z, this.canonical.x, this.canonical.y);
}
unwrapTo(wrap) {
return new OverscaledTileID(this.overscaledZ, wrap, this.canonical.z, this.canonical.x, this.canonical.y);
}
overscaleFactor() {
return Math.pow(2, this.overscaledZ - this.canonical.z);
}
toUnwrapped() {
return new UnwrappedTileID(this.wrap, this.canonical);
}
toString() {
return `${this.overscaledZ}/${this.canonical.x}/${this.canonical.y}`;
}
}
function calculateKey(wrap, overscaledZ, z, x, y) {
const dim = 1 << Math.min(z, 22);
let xy = dim * (y % dim) + x % dim;
if (wrap && z < 22) {
const bitsAvailable = 2 * (22 - z);
xy += dim * dim * ((wrap < 0 ? -2 * wrap - 1 : 2 * wrap) % (1 << bitsAvailable));
}
const key = (xy * 32 + z) * 16 + (overscaledZ - z);
assert$1(key >= 0 && key <= Number.MAX_SAFE_INTEGER);
return key;
}
function getQuadkey(z, x, y) {
let quadkey = "", mask;
for (let i = z; i > 0; i--) {
mask = 1 << i - 1;
quadkey += (x & mask ? 1 : 0) + (y & mask ? 2 : 0);
}
return quadkey;
}
const neighborCoord = [
(coord) => {
let x = coord.canonical.x - 1;
let w = coord.wrap;
if (x < 0) {
x = (1 << coord.canonical.z) - 1;
w--;
}
return new OverscaledTileID(coord.overscaledZ, w, coord.canonical.z, x, coord.canonical.y);
},
(coord) => {
let x = coord.canonical.x + 1;
let w = coord.wrap;
if (x === 1 << coord.canonical.z) {
x = 0;
w++;
}
return new OverscaledTileID(coord.overscaledZ, w, coord.canonical.z, x, coord.canonical.y);
},
(coord) => new OverscaledTileID(
coord.overscaledZ,
coord.wrap,
coord.canonical.z,
coord.canonical.x,
(coord.canonical.y === 0 ? 1 << coord.canonical.z : coord.canonical.y) - 1
),
(coord) => new OverscaledTileID(
coord.overscaledZ,
coord.wrap,
coord.canonical.z,
coord.canonical.x,
coord.canonical.y === (1 << coord.canonical.z) - 1 ? 0 : coord.canonical.y + 1
)
];
register(CanonicalTileID, "CanonicalTileID");
register(OverscaledTileID, "OverscaledTileID", { omit: ["projMatrix", "expandedProjMatrix"] });
const layout$d = createLayout([
{ type: "Float32", name: "a_globe_pos", components: 3 },
{ type: "Float32", name: "a_uv", components: 2 }
]);
const { members: members$7, size: size$7, alignment: alignment$7 } = layout$d;
const posAttributesGlobeExt = createLayout([
{ name: "a_pos_3", components: 3, type: "Int16" }
]);
var posAttributes = createLayout([
{ name: "a_pos", type: "Int16", components: 2 }
]);
function globeMetersToEcef(d) {
return d * GLOBE_RADIUS / earthRadius;
}
const GLOBE_LOW_ZOOM_TILE_AABBS = [
// z == 0
new Aabb([GLOBE_MIN, GLOBE_MIN, GLOBE_MIN], [GLOBE_MAX, GLOBE_MAX, GLOBE_MAX]),
// z == 1
new Aabb([GLOBE_MIN, GLOBE_MIN, GLOBE_MIN], [0, 0, GLOBE_MAX]),
// x=0, y=0
new Aabb([0, GLOBE_MIN, GLOBE_MIN], [GLOBE_MAX, 0, GLOBE_MAX]),
// x=1, y=0
new Aabb([GLOBE_MIN, 0, GLOBE_MIN], [0, GLOBE_MAX, GLOBE_MAX]),
// x=0, y=1
new Aabb([0, 0, GLOBE_MIN], [GLOBE_MAX, GLOBE_MAX, GLOBE_MAX])
// x=1, y=1
];
function globePointCoordinate(tr, x, y, clampToHorizon = true) {
const point0 = scale$4([], tr._camera.position, tr.worldSize);
const point1 = [x, y, 1, 1];
transformMat4$1(point1, point1, tr.pixelMatrixInverse);
scale$3(point1, point1, 1 / point1[3]);
const p0p1 = sub$2([], point1, point0);
const dir = normalize$4([], p0p1);
const m = tr.globeMatrix;
const globeCenter = [m[12], m[13], m[14]];
const p0toCenter = sub$2([], globeCenter, point0);
const p0toCenterDist = length$4(p0toCenter);
const centerDir = normalize$4([], p0toCenter);
const radius = tr.worldSize / (2 * Math.PI);
const cosAngle = dot$5(centerDir, dir);
const origoTangentAngle = Math.asin(radius / p0toCenterDist);
const origoDirAngle = Math.acos(cosAngle);
if (origoTangentAngle < origoDirAngle) {
if (!clampToHorizon) return null;
const clampedP1 = [];
const origoToP1 = [];
scale$4(clampedP1, dir, p0toCenterDist / cosAngle);
normalize$4(origoToP1, sub$2(origoToP1, clampedP1, p0toCenter));
normalize$4(dir, add$4(dir, p0toCenter, scale$4(dir, origoToP1, Math.tan(origoTangentAngle) * p0toCenterDist)));
}
const pointOnGlobe = [];
const ray = new Ray(point0, dir);
ray.closestPointOnSphere(globeCenter, radius, pointOnGlobe);
const xa = normalize$4([], getColumn(m, 0));
const ya = normalize$4([], getColumn(m, 1));
const za = normalize$4([], getColumn(m, 2));
const xp = dot$5(xa, pointOnGlobe);
const yp = dot$5(ya, pointOnGlobe);
const zp = dot$5(za, pointOnGlobe);
const lat = radToDeg(Math.asin(-yp / radius));
let lng = radToDeg(Math.atan2(xp, zp));
lng = tr.center.lng + shortestAngle(tr.center.lng, lng);
const mx = mercatorXfromLng(lng);
const my = clamp(mercatorYfromLat(lat), 0, 1);
return new MercatorCoordinate(mx, my);
}
class Arc {
constructor(p0, p1, center) {
this.a = sub$2([], p0, center);
this.b = sub$2([], p1, center);
this.center = center;
const an = normalize$4([], this.a);
const bn = normalize$4([], this.b);
this.angle = Math.acos(dot$5(an, bn));
}
}
function slerp(a, b, angle, t) {
const sina = Math.sin(angle);
return a * (Math.sin((1 - t) * angle) / sina) + b * (Math.sin(t * angle) / sina);
}
function localExtremum(arc, dim) {
if (arc.angle === 0) {
return null;
}
let t;
if (arc.a[dim] === 0) {
t = 1 / arc.angle * 0.5 * Math.PI;
} else {
t = 1 / arc.angle * Math.atan(arc.b[dim] / arc.a[dim] / Math.sin(arc.angle) - 1 / Math.tan(arc.angle));
}
if (t < 0 || t > 1) {
return null;
}
return slerp(arc.a[dim], arc.b[dim], arc.angle, clamp(t, 0, 1)) + arc.center[dim];
}
function globeTileBounds(id) {
if (id.z <= 1) {
return GLOBE_LOW_ZOOM_TILE_AABBS[id.z + id.y * 2 + id.x];
}
const bounds = tileCornersToBounds(id);
const corners = boundsToECEF(bounds);
return Aabb.fromPoints(corners);
}
function interpolateVec3(from, to, phase) {
scale$4(from, from, 1 - phase);
return scaleAndAdd$2(from, from, to, phase);
}
function transitionTileAABBinECEF(id, tr) {
const phase = globeToMercatorTransition(tr.zoom);
if (phase === 0) {
return globeTileBounds(id);
}
const bounds = tileCornersToBounds(id);
const corners = boundsToECEF(bounds);
const w = mercatorXfromLng(bounds.getWest()) * tr.worldSize;
const e = mercatorXfromLng(bounds.getEast()) * tr.worldSize;
const n = mercatorYfromLat(bounds.getNorth()) * tr.worldSize;
const s = mercatorYfromLat(bounds.getSouth()) * tr.worldSize;
const nw = [w, n, 0];
const ne = [e, n, 0];
const sw = [w, s, 0];
const se = [e, s, 0];
const worldToECEFMatrix = invert$2([], tr.globeMatrix);
transformMat4$2(nw, nw, worldToECEFMatrix);
transformMat4$2(ne, ne, worldToECEFMatrix);
transformMat4$2(sw, sw, worldToECEFMatrix);
transformMat4$2(se, se, worldToECEFMatrix);
corners[0] = interpolateVec3(corners[0], sw, phase);
corners[1] = interpolateVec3(corners[1], se, phase);
corners[2] = interpolateVec3(corners[2], ne, phase);
corners[3] = interpolateVec3(corners[3], nw, phase);
return Aabb.fromPoints(corners);
}
function transformPoints(corners, globeMatrix, scale) {
for (const corner of corners) {
transformMat4$2(corner, corner, globeMatrix);
scale$4(corner, corner, scale);
}
}
function aabbForTileOnGlobe(tr, numTiles, tileId, extendToPoles) {
const scale = numTiles / tr.worldSize;
const m = tr.globeMatrix;
if (tileId.z <= 1) {
const corners2 = globeTileBounds(tileId).getCorners();
transformPoints(corners2, m, scale);
return Aabb.fromPoints(corners2);
}
const bounds = tileCornersToBounds(tileId, extendToPoles);
const corners = boundsToECEF(bounds, GLOBE_RADIUS + globeMetersToEcef(tr._tileCoverLift));
transformPoints(corners, m, scale);
const mx = Number.MAX_VALUE;
const cornerMax = [-mx, -mx, -mx];
const cornerMin = [mx, mx, mx];
if (bounds.contains(tr.center)) {
for (const corner of corners) {
min$3(cornerMin, cornerMin, corner);
max$4(cornerMax, cornerMax, corner);
}
cornerMax[2] = 0;
const point = tr.point;
const center = [point.x * scale, point.y * scale, 0];
min$3(cornerMin, cornerMin, center);
max$4(cornerMax, cornerMax, center);
return new Aabb(cornerMin, cornerMax);
}
if (tr._tileCoverLift > 0) {
for (const corner of corners) {
min$3(cornerMin, cornerMin, corner);
max$4(cornerMax, cornerMax, corner);
}
return new Aabb(cornerMin, cornerMax);
}
const arcCenter = [m[12] * scale, m[13] * scale, m[14] * scale];
const tileCenter = bounds.getCenter();
const centerLat = clamp(tr.center.lat, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
const tileCenterLat = clamp(tileCenter.lat, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
const camX = mercatorXfromLng(tr.center.lng);
const camY = mercatorYfromLat(centerLat);
let dx = camX - mercatorXfromLng(tileCenter.lng);
const dy = camY - mercatorYfromLat(tileCenterLat);
if (dx > 0.5) {
dx -= 1;
} else if (dx < -0.5) {
dx += 1;
}
let closestArcIdx = 0;
if (Math.abs(dx) > Math.abs(dy)) {
closestArcIdx = dx >= 0 ? 1 : 3;
} else {
closestArcIdx = dy >= 0 ? 0 : 2;
const yAxis = [m[4] * scale, m[5] * scale, m[6] * scale];
const shift = -Math.sin(degToRad(dy >= 0 ? bounds.getSouth() : bounds.getNorth())) * GLOBE_RADIUS;
scaleAndAdd$2(arcCenter, arcCenter, yAxis, shift);
}
const arcStart = corners[closestArcIdx];
const arcEnd = corners[(closestArcIdx + 1) % 4];
const closestArc = new Arc(arcStart, arcEnd, arcCenter);
const arcExtremum = [
localExtremum(closestArc, 0) || arcStart[0],
localExtremum(closestArc, 1) || arcStart[1],
localExtremum(closestArc, 2) || arcStart[2]
];
const phase = globeToMercatorTransition(tr.zoom);
if (phase > 0) {
const mercatorCorners = mercatorTileCornersInCameraSpace(tileId, numTiles, tr._pixelsPerMercatorPixel, camX, camY);
for (let i = 0; i < corners.length; i++) {
interpolateVec3(corners[i], mercatorCorners[i], phase);
}
const mercatorMidpoint = add$4([], mercatorCorners[closestArcIdx], mercatorCorners[(closestArcIdx + 1) % 4]);
scale$4(mercatorMidpoint, mercatorMidpoint, 0.5);
interpolateVec3(arcExtremum, mercatorMidpoint, phase);
}
for (const corner of corners) {
min$3(cornerMin, cornerMin, corner);
max$4(cornerMax, cornerMax, corner);
}
cornerMin[2] = Math.min(arcStart[2], arcEnd[2]);
min$3(cornerMin, cornerMin, arcExtremum);
max$4(cornerMax, cornerMax, arcExtremum);
return new Aabb(cornerMin, cornerMax);
}
function tileCornersToBounds({
x,
y,
z
}, extendToPoles = false) {
const s = 1 / (1 << z);
const sw = new LngLat(lngFromMercatorX(x * s), y === (1 << z) - 1 && extendToPoles ? -90 : latFromMercatorY((y + 1) * s));
const ne = new LngLat(lngFromMercatorX((x + 1) * s), y === 0 && extendToPoles ? 90 : latFromMercatorY(y * s));
return new LngLatBounds(sw, ne);
}
function mercatorTileCornersInCameraSpace({
x,
y,
z
}, numTiles, mercatorScale, camX, camY) {
const tileScale = 1 / (1 << z);
let w = x * tileScale;
let e = w + tileScale;
let n = y * tileScale;
let s = n + tileScale;
let wrap = 0;
const tileCenterXFromCamera = (w + e) / 2 - camX;
if (tileCenterXFromCamera > 0.5) {
wrap = -1;
} else if (tileCenterXFromCamera < -0.5) {
wrap = 1;
}
camX *= numTiles;
camY *= numTiles;
w = ((w + wrap) * numTiles - camX) * mercatorScale + camX;
e = ((e + wrap) * numTiles - camX) * mercatorScale + camX;
n = (n * numTiles - camY) * mercatorScale + camY;
s = (s * numTiles - camY) * mercatorScale + camY;
return [
[w, s, 0],
[e, s, 0],
[e, n, 0],
[w, n, 0]
];
}
function boundsToECEF(bounds, radius = GLOBE_RADIUS) {
const ny = degToRad(bounds.getNorth());
const sy = degToRad(bounds.getSouth());
const cosN = Math.cos(ny);
const cosS = Math.cos(sy);
const sinN = Math.sin(ny);
const sinS = Math.sin(sy);
const w = bounds.getWest();
const e = bounds.getEast();
return [
csLatLngToECEF(cosS, sinS, w, radius),
csLatLngToECEF(cosS, sinS, e, radius),
csLatLngToECEF(cosN, sinN, e, radius),
csLatLngToECEF(cosN, sinN, w, radius)
];
}
function tileCoordToECEF(x, y, id, radius) {
const tileCount = 1 << id.z;
const mercatorX = (x / EXTENT + id.x) / tileCount;
const mercatorY = (y / EXTENT + id.y) / tileCount;
const lat = latFromMercatorY(mercatorY);
const lng = lngFromMercatorX(mercatorX);
const pos = latLngToECEF(lat, lng, radius);
return pos;
}
function globeECEFOrigin(tileMatrix, id) {
const origin = [0, 0, 0];
const bounds = globeTileBounds(id.canonical);
const normalizationMatrix = globeNormalizeECEF(bounds);
transformMat4$2(origin, origin, normalizationMatrix);
transformMat4$2(origin, origin, tileMatrix);
return origin;
}
function globeECEFNormalizationScale({
min,
max
}) {
return GLOBE_NORMALIZATION_MASK / Math.max(max[0] - min[0], max[1] - min[1], max[2] - min[2]);
}
const tempMatrix = new Float64Array(16);
function globeNormalizeECEF(bounds) {
const scale = globeECEFNormalizationScale(bounds);
const m = fromScaling(tempMatrix, [scale, scale, scale]);
return translate$2(m, m, negate$2([], bounds.min));
}
function globeDenormalizeECEF(bounds) {
const m = fromTranslation$1(tempMatrix, bounds.min);
const scale = 1 / globeECEFNormalizationScale(bounds);
return scale$5(m, m, [scale, scale, scale]);
}
function globeECEFUnitsToPixelScale(worldSize) {
const localRadius = EXTENT / (2 * Math.PI);
const wsRadius = worldSize / (2 * Math.PI);
return wsRadius / localRadius;
}
function globePixelsToTileUnits(zoom, id) {
const ecefPerPixel = EXTENT / (TILE_SIZE * Math.pow(2, zoom));
const normCoeff = globeECEFNormalizationScale(globeTileBounds(id));
return ecefPerPixel * normCoeff;
}
function calculateGlobePosMatrix(x, y, worldSize, lng, lat) {
const scale = globeECEFUnitsToPixelScale(worldSize);
const offset = [x, y, -worldSize / (2 * Math.PI)];
const m = identity$3(new Float64Array(16));
translate$2(m, m, offset);
scale$5(m, m, [scale, scale, scale]);
rotateX$3(m, m, degToRad(-lat));
rotateY$3(m, m, degToRad(-lng));
return m;
}
function calculateGlobeMatrix(tr) {
const { x, y } = tr.point;
const { lng, lat } = tr._center;
return calculateGlobePosMatrix(x, y, tr.worldSize, lng, lat);
}
function calculateGlobeLabelMatrix(tr, id) {
const { x, y } = tr.point;
const m = calculateGlobePosMatrix(x, y, tr.worldSize / tr._pixelsPerMercatorPixel, 0, 0);
return multiply$5(m, m, globeDenormalizeECEF(globeTileBounds(id)));
}
function calculateGlobeMercatorMatrix(tr) {
const zScale = tr.pixelsPerMeter;
const ws = zScale / mercatorZfromAltitude(1, tr.center.lat);
const posMatrix = identity$3(new Float64Array(16));
translate$2(posMatrix, posMatrix, [tr.point.x, tr.point.y, 0]);
scale$5(posMatrix, posMatrix, [ws, ws, zScale]);
return Float32Array.from(posMatrix);
}
function globeToMercatorTransition(zoom) {
return smoothstep(GLOBE_ZOOM_THRESHOLD_MIN, GLOBE_ZOOM_THRESHOLD_MAX, zoom);
}
function globeMatrixForTile(id, globeMatrix) {
const decode = globeDenormalizeECEF(globeTileBounds(id));
return mul$5(create$5(), globeMatrix, decode);
}
function globePoleMatrixForTile(z, x, tr) {
const poleMatrix = identity$3(new Float64Array(16));
const numTiles = 1 << z;
const xOffsetAngle = (x / numTiles - 0.5) * Math.PI * 2;
rotateY$3(poleMatrix, tr.globeMatrix, xOffsetAngle);
return Float32Array.from(poleMatrix);
}
function globeUseCustomAntiAliasing(painter, context, transform) {
const transitionT = globeToMercatorTransition(transform.zoom);
const useContextAA = painter.style.map._antialias;
const disabled = painter.terrain && painter.terrain.exaggeration() > 0;
return transitionT === 0 && !useContextAA && !disabled;
}
function getGridMatrix(id, bounds, latitudinalLod, worldSize) {
const n = bounds.getNorth();
const s = bounds.getSouth();
const w = bounds.getWest();
const e = bounds.getEast();
const tiles = 1 << id.z;
const tileWidth = e - w;
const tileHeight = n - s;
const tileToLng = tileWidth / GLOBE_VERTEX_GRID_SIZE;
const tileToLat = -tileHeight / GLOBE_LATITUDINAL_GRID_LOD_TABLE[latitudinalLod];
const matrix = [0, tileToLng, 0, tileToLat, 0, 0, n, w, 0];
if (id.z > 0) {
const pixelPadding = 0.5;
const padding = pixelPadding * 360 / worldSize;
const xScale = padding / tileWidth + 1;
const yScale = padding / tileHeight + 1;
const padMatrix = [xScale, 0, 0, 0, yScale, 0, -0.5 * padding / tileToLng, 0.5 * padding / tileToLat, 1];
multiply$6(matrix, matrix, padMatrix);
}
matrix[2] = tiles;
matrix[5] = id.x;
matrix[8] = id.y;
return matrix;
}
function getLatitudinalLod(lat) {
const UPPER_LATITUDE = MAX_MERCATOR_LATITUDE - 5;
lat = clamp(lat, -UPPER_LATITUDE, UPPER_LATITUDE) / UPPER_LATITUDE * 90;
const t = Math.pow(Math.abs(Math.sin(degToRad(lat))), 3);
const lod = Math.round(t * (GLOBE_LATITUDINAL_GRID_LOD_TABLE.length - 1));
return lod;
}
function globeCenterToScreenPoint(tr) {
const pos = [0, 0, 0];
const matrix = identity$3(new Float64Array(16));
multiply$5(matrix, tr.pixelMatrix, tr.globeMatrix);
transformMat4$2(pos, pos, matrix);
return new Point(pos[0], pos[1]);
}
function cameraPositionInECEF(tr) {
const centerToPivot = latLngToECEF(tr._center.lat, tr._center.lng);
const south = fromValues$4(0, 1, 0);
let axis = cross$2([], south, centerToPivot);
const rotation = fromRotation$1([], -tr.angle, centerToPivot);
axis = transformMat4$2(axis, axis, rotation);
fromRotation$1(rotation, -tr._pitch, axis);
const pivotToCamera = normalize$4([], centerToPivot);
scale$4(pivotToCamera, pivotToCamera, globeMetersToEcef(tr.cameraToCenterDistance / tr.pixelsPerMeter));
transformMat4$2(pivotToCamera, pivotToCamera, rotation);
return add$4([], centerToPivot, pivotToCamera);
}
function globeTiltAtLngLat(tr, lngLat) {
const centerToPoint = latLngToECEF(lngLat.lat, lngLat.lng);
const centerToCamera = cameraPositionInECEF(tr);
const pointToCamera = subtract$2([], centerToCamera, centerToPoint);
return angle$1(pointToCamera, centerToPoint);
}
function isLngLatBehindGlobe(tr, lngLat) {
return globeTiltAtLngLat(tr, lngLat) > Math.PI / 2 * 1.01;
}
function polesInViewport(tr) {
const ecefToScreenMatrix = identity$3(new Float64Array(16));
multiply$5(ecefToScreenMatrix, tr.pixelMatrix, tr.globeMatrix);
const north = [0, GLOBE_MIN, 0];
const south = [0, GLOBE_MAX, 0];
transformMat4$2(north, north, ecefToScreenMatrix);
transformMat4$2(south, south, ecefToScreenMatrix);
const northInViewport = north[0] > 0 && north[0] <= tr.width && north[1] > 0 && north[1] <= tr.height && !isLngLatBehindGlobe(tr, new LngLat(tr.center.lat, 90));
const southInViewport = south[0] > 0 && south[0] <= tr.width && south[1] > 0 && south[1] <= tr.height && !isLngLatBehindGlobe(tr, new LngLat(tr.center.lat, -90));
return [northInViewport, southInViewport];
}
const POLE_RAD = degToRad(85);
const POLE_COS = Math.cos(POLE_RAD);
const POLE_SIN = Math.sin(POLE_RAD);
const EMBED_SKIRTS = true;
class GlobeSharedBuffers {
constructor(context) {
this._createGrid(context);
this._createPoles(context);
}
destroy() {
this._poleIndexBuffer.destroy();
this._gridBuffer.destroy();
this._gridIndexBuffer.destroy();
this._poleNorthVertexBuffer.destroy();
this._poleSouthVertexBuffer.destroy();
for (const segments of this._poleSegments) segments.destroy();
for (const segments of this._gridSegments) {
segments.withSkirts.destroy();
segments.withoutSkirts.destroy();
}
}
// Generate terrain grid vertices and indices for all LOD's
//
// Grid vertices memory layout:
//
// First line Skirt
// ┌───────────────┐
// │┌─────────────┐│
// Left ││┼┼┼┼┼┼┼┼┼┼┼┼┼││ Right
// Border ││┼┼┼┼┼┼┼┼┼┼┼┼┼││ Border
// Skirt │├─────────────┤│ Skirt
// ││ Main Grid ││
// │├─────────────┤│
// ││┼┼┼┼┼┼┼┼┼┼┼┼┼││
// ││┼┼┼┼┼┼┼┼┼┼┼┼┼││
// │└─────────────┘│
// ├───────────────┤
// ├───────────────┤
// └───────────────┘
// Bottom Skirt = Number of LOD's
//
_fillGridMeshWithLods(longitudinalCellsCount, latitudinalLods) {
const vertices = new StructArrayLayout2i4();
const indices = new StructArrayLayout3ui6();
const segments = [];
const xVertices = longitudinalCellsCount + 1 + 2 * (EMBED_SKIRTS ? 1 : 0);
const yVerticesHighLodNoStrip = latitudinalLods[0] + 1;
const yVerticesHighLodWithStrip = latitudinalLods[0] + 1 + (EMBED_SKIRTS ? 1 + latitudinalLods.length : 0);
const prepareVertex = (x, y, isSkirt) => {
if (!EMBED_SKIRTS) return [x, y];
let adjustedX = (() => {
if (x === xVertices - 1) {
return x - 2;
} else if (x === 0) {
return x;
} else {
return x - 1;
}
})();
const skirtOffset = 24575;
adjustedX += isSkirt ? skirtOffset : 0;
return [adjustedX, y];
};
if (EMBED_SKIRTS) {
for (let x = 0; x < xVertices; ++x) {
vertices.emplaceBack(...prepareVertex(x, 0, true));
}
}
for (let y = 0; y < yVerticesHighLodNoStrip; ++y) {
for (let x = 0; x < xVertices; ++x) {
const isSideBorder = x === 0 || x === xVertices - 1;
vertices.emplaceBack(...prepareVertex(x, y, isSideBorder && EMBED_SKIRTS));
}
}
if (EMBED_SKIRTS) {
for (let lodIdx = 0; lodIdx < latitudinalLods.length; ++lodIdx) {
const lastYRowForLod = latitudinalLods[lodIdx];
for (let x = 0; x < xVertices; ++x) {
vertices.emplaceBack(...prepareVertex(x, lastYRowForLod, true));
}
}
}
for (let lodIdx = 0; lodIdx < latitudinalLods.length; ++lodIdx) {
const indexOffset = indices.length;
const yVerticesLod = latitudinalLods[lodIdx] + 1 + 2 * (EMBED_SKIRTS ? 1 : 0);
const skirtsOnlyIndices = new StructArrayLayout3ui6();
for (let y = 0; y < yVerticesLod - 1; y++) {
const isLastLine = y === yVerticesLod - 2;
const offsetToNextRow = isLastLine && EMBED_SKIRTS ? xVertices * (yVerticesHighLodWithStrip - latitudinalLods.length + lodIdx - y) : xVertices;
for (let x = 0; x < xVertices - 1; x++) {
const idx = y * xVertices + x;
const isSkirt = EMBED_SKIRTS && (y === 0 || isLastLine || x === 0 || x === xVertices - 2);
if (isSkirt) {
skirtsOnlyIndices.emplaceBack(idx + 1, idx, idx + offsetToNextRow);
skirtsOnlyIndices.emplaceBack(idx + offsetToNextRow, idx + offsetToNextRow + 1, idx + 1);
} else {
indices.emplaceBack(idx + 1, idx, idx + offsetToNextRow);
indices.emplaceBack(idx + offsetToNextRow, idx + offsetToNextRow + 1, idx + 1);
}
}
}
const withoutSkirts = SegmentVector.simpleSegment(0, indexOffset, vertices.length, indices.length - indexOffset);
for (let i = 0; i < skirtsOnlyIndices.uint16.length; i += 3) {
indices.emplaceBack(skirtsOnlyIndices.uint16[i], skirtsOnlyIndices.uint16[i + 1], skirtsOnlyIndices.uint16[i + 2]);
}
const withSkirts = SegmentVector.simpleSegment(0, indexOffset, vertices.length, indices.length - indexOffset);
segments.push({ withoutSkirts, withSkirts });
}
return { vertices, indices, segments };
}
_createGrid(context) {
const gridWithLods = this._fillGridMeshWithLods(GLOBE_VERTEX_GRID_SIZE, GLOBE_LATITUDINAL_GRID_LOD_TABLE);
this._gridSegments = gridWithLods.segments;
this._gridBuffer = context.createVertexBuffer(gridWithLods.vertices, posAttributes.members);
this._gridIndexBuffer = context.createIndexBuffer(gridWithLods.indices, true);
}
_createPoles(context) {
const poleIndices = new StructArrayLayout3ui6();
for (let i = 0; i <= GLOBE_VERTEX_GRID_SIZE; i++) {
poleIndices.emplaceBack(0, i + 1, i + 2);
}
this._poleIndexBuffer = context.createIndexBuffer(poleIndices, true);
const northVertices = new StructArrayLayout5f20();
const southVertices = new StructArrayLayout5f20();
const texturedNorthVertices = new StructArrayLayout5f20();
const texturedSouthVertices = new StructArrayLayout5f20();
const polePrimitives = GLOBE_VERTEX_GRID_SIZE;
const poleVertices = GLOBE_VERTEX_GRID_SIZE + 2;
this._poleSegments = [];
for (let zoom = 0, offset = 0; zoom < GLOBE_ZOOM_THRESHOLD_MIN; zoom++) {
const tiles = 1 << zoom;
const endAngle = 360 / tiles;
northVertices.emplaceBack(0, -GLOBE_RADIUS, 0, 0.5, 0);
southVertices.emplaceBack(0, -GLOBE_RADIUS, 0, 0.5, 1);
texturedNorthVertices.emplaceBack(0, -GLOBE_RADIUS, 0, 0.5, 0.5);
texturedSouthVertices.emplaceBack(0, -GLOBE_RADIUS, 0, 0.5, 0.5);
for (let i = 0; i <= GLOBE_VERTEX_GRID_SIZE; i++) {
let uvX = i / GLOBE_VERTEX_GRID_SIZE;
let uvY = 0;
const angle = number(0, endAngle, uvX);
const [gx, gy, gz] = csLatLngToECEF(POLE_COS, POLE_SIN, angle, GLOBE_RADIUS);
northVertices.emplaceBack(gx, gy, gz, uvX, uvY);
southVertices.emplaceBack(gx, gy, gz, uvX, 1 - uvY);
const rad = degToRad(angle);
uvX = 0.5 + 0.5 * Math.sin(rad);
uvY = 0.5 + 0.5 * Math.cos(rad);
texturedNorthVertices.emplaceBack(gx, gy, gz, uvX, uvY);
texturedSouthVertices.emplaceBack(gx, gy, gz, uvX, 1 - uvY);
}
this._poleSegments.push(SegmentVector.simpleSegment(offset, 0, poleVertices, polePrimitives));
offset += poleVertices;
}
this._poleNorthVertexBuffer = context.createVertexBuffer(northVertices, members$7, false);
this._poleSouthVertexBuffer = context.createVertexBuffer(southVertices, members$7, false);
this._texturedPoleNorthVertexBuffer = context.createVertexBuffer(texturedNorthVertices, members$7, false);
this._texturedPoleSouthVertexBuffer = context.createVertexBuffer(texturedSouthVertices, members$7, false);
}
getGridBuffers(latitudinalLod, withSkirts) {
return [this._gridBuffer, this._gridIndexBuffer, withSkirts ? this._gridSegments[latitudinalLod].withSkirts : this._gridSegments[latitudinalLod].withoutSkirts];
}
getPoleBuffers(z, textured) {
return [
textured ? this._texturedPoleNorthVertexBuffer : this._poleNorthVertexBuffer,
textured ? this._texturedPoleSouthVertexBuffer : this._poleSouthVertexBuffer,
this._poleIndexBuffer,
this._poleSegments[z]
];
}
}
const circleUniforms = (context) => ({
"u_camera_to_center_distance": new Uniform1f(context),
"u_extrude_scale": new UniformMatrix2f(context),
"u_device_pixel_ratio": new Uniform1f(context),
"u_matrix": new UniformMatrix4f(context),
"u_inv_rot_matrix": new UniformMatrix4f(context),
"u_merc_center": new Uniform2f(context),
"u_tile_id": new Uniform3f(context),
"u_zoom_transition": new Uniform1f(context),
"u_up_dir": new Uniform3f(context),
"u_emissive_strength": new Uniform1f(context)
});
const identityMatrix = create$5();
const circleUniformValues = (painter, coord, tile, invMatrix, mercatorCenter, layer) => {
const transform = painter.transform;
const isGlobe = transform.projection.name === "globe";
let extrudeScale;
if (layer.paint.get("circle-pitch-alignment") === "map") {
if (isGlobe) {
const s = globePixelsToTileUnits(transform.zoom, coord.canonical) * transform._pixelsPerMercatorPixel;
extrudeScale = Float32Array.from([s, 0, 0, s]);
} else {
extrudeScale = transform.calculatePixelsToTileUnitsMatrix(tile);
}
} else {
extrudeScale = new Float32Array([
transform.pixelsToGLUnits[0],
0,
0,
transform.pixelsToGLUnits[1]
]);
}
const values = {
"u_camera_to_center_distance": painter.transform.getCameraToCenterDistance(transform.projection),
"u_matrix": painter.translatePosMatrix(
coord.projMatrix,
tile,
layer.paint.get("circle-translate"),
layer.paint.get("circle-translate-anchor")
),
"u_device_pixel_ratio": exported$1.devicePixelRatio,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"u_extrude_scale": extrudeScale,
"u_inv_rot_matrix": identityMatrix,
"u_merc_center": [0, 0],
"u_tile_id": [0, 0, 0],
"u_zoom_transition": 0,
"u_up_dir": [0, 0, 0],
"u_emissive_strength": layer.paint.get("circle-emissive-strength")
};
if (isGlobe) {
values["u_inv_rot_matrix"] = invMatrix;
values["u_merc_center"] = mercatorCenter;
values["u_tile_id"] = [coord.canonical.x, coord.canonical.y, 1 << coord.canonical.z];
values["u_zoom_transition"] = globeToMercatorTransition(transform.zoom);
const x = mercatorCenter[0] * EXTENT;
const y = mercatorCenter[1] * EXTENT;
values["u_up_dir"] = transform.projection.upVector(new CanonicalTileID(0, 0, 0), x, y);
}
return values;
};
const circleDefinesValues = (layer) => {
const values = [];
if (layer.paint.get("circle-pitch-alignment") === "map") values.push("PITCH_WITH_MAP");
if (layer.paint.get("circle-pitch-scale") === "map") values.push("SCALE_WITH_MAP");
return values;
};
class CircleStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$e(),
paint: getPaintProperties$e()
};
super(layer, properties, scope, lut, options);
}
createBucket(parameters) {
return new CircleBucket(parameters);
}
queryRadius(bucket) {
const circleBucket = bucket;
return getMaximumPaintValue("circle-radius", this, circleBucket) + getMaximumPaintValue("circle-stroke-width", this, circleBucket) + translateDistance(this.paint.get("circle-translate"));
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform, pixelPosMatrix, elevationHelper) {
const translation = tilespaceTranslate(
this.paint.get("circle-translate"),
this.paint.get("circle-translate-anchor"),
transform.angle,
queryGeometry.pixelToTileUnitsFactor
);
const size = this.paint.get("circle-radius").evaluate(feature, featureState) + this.paint.get("circle-stroke-width").evaluate(feature, featureState);
return queryIntersectsCircle(
queryGeometry,
geometry,
transform,
pixelPosMatrix,
elevationHelper,
this.paint.get("circle-pitch-alignment") === "map",
this.paint.get("circle-pitch-scale") === "map",
translation,
size
);
}
getProgramIds() {
return ["circle"];
}
getDefaultProgramParams(_, zoom, lut) {
const definesValues = circleDefinesValues(this);
return {
config: new ProgramConfiguration(this, { zoom, lut }),
defines: definesValues,
overrideFog: false
};
}
is3D(terrainEnabled) {
if (terrainEnabled) return false;
return !!this.layout && this.layout.get("circle-elevation-reference") !== "none";
}
hasElevation() {
return this.layout && this.layout.get("circle-elevation-reference") !== "none";
}
}
function queryIntersectsCircle(queryGeometry, geometry, transform, pixelPosMatrix, elevationHelper, alignWithMap, scaleWithMap, translation, size) {
if (alignWithMap && queryGeometry.queryGeometry.isAboveHorizon) return false;
if (alignWithMap) size *= queryGeometry.pixelToTileUnitsFactor;
const tileId = queryGeometry.tileID.canonical;
const elevationScale = transform.projection.upVectorScale(tileId, transform.center.lat, transform.worldSize).metersToTile;
for (const ring of geometry) {
for (const point of ring) {
const translatedPoint = point.add(translation);
const z = elevationHelper && transform.elevation ? transform.elevation.exaggeration() * elevationHelper.getElevationAt(translatedPoint.x, translatedPoint.y, true) : 0;
const reproj = transform.projection.projectTilePoint(translatedPoint.x, translatedPoint.y, tileId);
if (z > 0) {
const dir = transform.projection.upVector(tileId, translatedPoint.x, translatedPoint.y);
reproj.x += dir[0] * elevationScale * z;
reproj.y += dir[1] * elevationScale * z;
reproj.z += dir[2] * elevationScale * z;
}
const transformedPoint = alignWithMap ? translatedPoint : projectPoint(reproj.x, reproj.y, reproj.z, pixelPosMatrix);
const transformedPolygon = alignWithMap ? queryGeometry.tilespaceRays.map((r) => intersectAtHeight(r, z)) : queryGeometry.queryGeometry.screenGeometry;
const projectedCenter = transformMat4$1([], [reproj.x, reproj.y, reproj.z, 1], pixelPosMatrix);
if (!scaleWithMap && alignWithMap) {
size *= projectedCenter[3] / transform.cameraToCenterDistance;
} else if (scaleWithMap && !alignWithMap) {
size *= transform.cameraToCenterDistance / projectedCenter[3];
}
if (alignWithMap) {
const lat = latFromMercatorY((point.y / EXTENT + tileId.y) / (1 << tileId.z));
const scale = transform.projection.pixelsPerMeter(lat, 1) / mercatorZfromAltitude(1, lat);
size /= scale;
}
if (polygonIntersectsBufferedPoint(transformedPolygon, transformedPoint, size)) return true;
}
}
return false;
}
function projectPoint(x, y, z, pixelPosMatrix) {
const point = transformMat4$1([], [x, y, z, 1], pixelPosMatrix);
return new Point(point[0] / point[3], point[1] / point[3]);
}
const origin = fromValues$4(0, 0, 0);
const up = fromValues$4(0, 0, 1);
function intersectAtHeight(r, z) {
const intersectionPt = create$4();
origin[2] = z;
const intersects = r.intersectsPlane(origin, up, intersectionPt);
assert$1(intersects, "tilespacePoint should always be below horizon, and since camera cannot have pitch >90, ray should always intersect");
return new Point(intersectionPt[0], intersectionPt[1]);
}
class HeatmapBucket extends CircleBucket {
}
register(HeatmapBucket, "HeatmapBucket", { omit: ["layers"] });
let layout$c;
const getLayoutProperties$d = () => layout$c || (layout$c = new Properties({
"visibility": new DataConstantProperty(spec["layout_heatmap"]["visibility"])
}));
let paint$d;
const getPaintProperties$d = () => paint$d || (paint$d = new Properties({
"heatmap-radius": new DataDrivenProperty(spec["paint_heatmap"]["heatmap-radius"]),
"heatmap-weight": new DataDrivenProperty(spec["paint_heatmap"]["heatmap-weight"]),
"heatmap-intensity": new DataConstantProperty(spec["paint_heatmap"]["heatmap-intensity"]),
"heatmap-color": new ColorRampProperty(spec["paint_heatmap"]["heatmap-color"]),
"heatmap-opacity": new DataConstantProperty(spec["paint_heatmap"]["heatmap-opacity"]),
"heatmap-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
function createImage(image, {
width,
height
}, channels, data) {
if (!data) {
data = new Uint8Array(width * height * channels);
} else if (data instanceof Uint8ClampedArray) {
data = new Uint8Array(data.buffer);
} else if (data.length !== width * height * channels) {
throw new RangeError("mismatched image size");
}
image.width = width;
image.height = height;
image.data = data;
return image;
}
function resizeImage(image, newImage, channels) {
const { width, height } = newImage;
if (width === image.width && height === image.height) {
return;
}
copyImage(image, newImage, { x: 0, y: 0 }, { x: 0, y: 0 }, {
width: Math.min(image.width, width),
height: Math.min(image.height, height)
}, channels, null);
image.width = width;
image.height = height;
image.data = newImage.data;
}
function copyImage(srcImg, dstImg, srcPt, dstPt, size, channels, lut, overrideRGBWithWhite) {
if (size.width === 0 || size.height === 0) {
return dstImg;
}
if (size.width > srcImg.width || size.height > srcImg.height || srcPt.x > srcImg.width - size.width || srcPt.y > srcImg.height - size.height) {
throw new RangeError("out of range source coordinates for image copy");
}
if (size.width > dstImg.width || size.height > dstImg.height || dstPt.x > dstImg.width - size.width || dstPt.y > dstImg.height - size.height) {
throw new RangeError("out of range destination coordinates for image copy");
}
const srcData = srcImg.data;
const dstData = dstImg.data;
const overrideRGB = channels === 4 && overrideRGBWithWhite;
assert$1(srcData !== dstData);
for (let y = 0; y < size.height; y++) {
const srcOffset = ((srcPt.y + y) * srcImg.width + srcPt.x) * channels;
const dstOffset = ((dstPt.y + y) * dstImg.width + dstPt.x) * channels;
if (overrideRGB) {
for (let i = 0; i < size.width; i++) {
const srcByteOffset = srcOffset + i * channels + 3;
const dstPixelOffset = dstOffset + i * channels;
dstData[dstPixelOffset + 0] = 255;
dstData[dstPixelOffset + 1] = 255;
dstData[dstPixelOffset + 2] = 255;
dstData[dstPixelOffset + 3] = srcData[srcByteOffset];
}
} else if (lut) {
for (let i = 0; i < size.width; i++) {
const srcByteOffset = srcOffset + i * channels;
const dstPixelOffset = dstOffset + i * channels;
const alpha = srcData[srcByteOffset + 3];
const color = new Color(srcData[srcByteOffset + 0] / 255, srcData[srcByteOffset + 1] / 255, srcData[srcByteOffset + 2] / 255, alpha);
const shifted = color.toNonPremultipliedRenderColor(lut).toArray();
dstData[dstPixelOffset + 0] = shifted[0];
dstData[dstPixelOffset + 1] = shifted[1];
dstData[dstPixelOffset + 2] = shifted[2];
dstData[dstPixelOffset + 3] = shifted[3];
}
} else {
for (let i = 0; i < size.width * channels; i++) {
const srcByte = srcOffset + i;
dstData[dstOffset + i] = srcData[srcByte];
}
}
}
return dstImg;
}
class AlphaImage {
constructor(size, data) {
createImage(this, size, 1, data);
}
resize(size) {
resizeImage(this, new AlphaImage(size), 1);
}
clone() {
return new AlphaImage({ width: this.width, height: this.height }, new Uint8Array(this.data));
}
static copy(srcImg, dstImg, srcPt, dstPt, size) {
copyImage(srcImg, dstImg, srcPt, dstPt, size, 1, null);
}
}
class RGBAImage {
constructor(size, data) {
createImage(this, size, 4, data);
}
resize(size) {
resizeImage(this, new RGBAImage(size), 4);
}
replace(data, copy) {
if (copy) {
this.data.set(data);
} else if (data instanceof Uint8ClampedArray) {
this.data = new Uint8Array(data.buffer);
} else {
this.data = data;
}
}
clone() {
return new RGBAImage({ width: this.width, height: this.height }, new Uint8Array(this.data));
}
static copy(srcImg, dstImg, srcPt, dstPt, size, lut, overrideRGBWithWhite) {
copyImage(srcImg, dstImg, srcPt, dstPt, size, 4, lut, overrideRGBWithWhite);
}
}
class Float32Image {
constructor(size, data) {
this.width = size.width;
this.height = size.height;
if (data instanceof Uint8Array) {
this.data = new Float32Array(data.buffer);
} else {
this.data = data;
}
}
}
register(AlphaImage, "AlphaImage");
register(RGBAImage, "RGBAImage");
function renderColorRamp(params) {
const evaluationGlobals = {};
const width = params.resolution || 256;
const height = params.clips ? params.clips.length : 1;
const image = params.image || new RGBAImage({ width, height });
assert$1(isPowerOfTwo(width));
const renderPixel = (stride, index, progress) => {
evaluationGlobals[params.evaluationKey] = progress;
const color = params.expression.evaluate(evaluationGlobals);
const pxColor = color ? color.toNonPremultipliedRenderColor(null) : null;
if (!pxColor) return;
image.data[stride + index + 0] = Math.floor(pxColor.r * 255);
image.data[stride + index + 1] = Math.floor(pxColor.g * 255);
image.data[stride + index + 2] = Math.floor(pxColor.b * 255);
image.data[stride + index + 3] = Math.floor(pxColor.a * 255);
};
if (!params.clips) {
for (let i = 0, j = 0; i < width; i++, j += 4) {
const progress = i / (width - 1);
renderPixel(0, j, progress);
}
} else {
for (let clip = 0, stride = 0; clip < height; ++clip, stride += width * 4) {
for (let i = 0, j = 0; i < width; i++, j += 4) {
const progress = i / (width - 1);
const { start, end } = params.clips[clip];
const evaluationProgress = start * (1 - progress) + end * progress;
renderPixel(stride, j, evaluationProgress);
}
}
}
return image;
}
class HeatmapStyleLayer extends StyleLayer {
createBucket(parameters) {
return new HeatmapBucket(parameters);
}
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$d(),
paint: getPaintProperties$d()
};
super(layer, properties, scope, lut, options);
this._updateColorRamp();
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "heatmap-color") {
this._updateColorRamp();
}
}
_updateColorRamp() {
const expression = this._transitionablePaint._values["heatmap-color"].value.expression;
this.colorRamp = renderColorRamp({
expression,
evaluationKey: "heatmapDensity",
image: this.colorRamp
});
this.colorRampTexture = null;
}
resize() {
if (this.heatmapFbo) {
this.heatmapFbo.destroy();
this.heatmapFbo = null;
}
}
_clear() {
if (this.heatmapFbo) {
this.heatmapFbo.destroy();
this.heatmapFbo = null;
}
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
this.colorRampTexture = null;
}
}
queryRadius(bucket) {
return getMaximumPaintValue("heatmap-radius", this, bucket);
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform, pixelPosMatrix, elevationHelper) {
const size = this.paint.get("heatmap-radius").evaluate(feature, featureState);
return queryIntersectsCircle(
queryGeometry,
geometry,
transform,
pixelPosMatrix,
elevationHelper,
true,
true,
new Point(0, 0),
size
);
}
hasOffscreenPass() {
return this.paint.get("heatmap-opacity") !== 0 && this.visibility !== "none";
}
getProgramIds() {
return ["heatmap", "heatmapTexture"];
}
getDefaultProgramParams(name, zoom, lut) {
if (name === "heatmap") {
return {
config: new ProgramConfiguration(this, { zoom, lut }),
overrideFog: false
};
}
return {};
}
}
let layout$b;
const getLayoutProperties$c = () => layout$b || (layout$b = new Properties({
"visibility": new DataConstantProperty(spec["layout_hillshade"]["visibility"])
}));
let paint$c;
const getPaintProperties$c = () => paint$c || (paint$c = new Properties({
"hillshade-illumination-direction": new DataConstantProperty(spec["paint_hillshade"]["hillshade-illumination-direction"]),
"hillshade-illumination-anchor": new DataConstantProperty(spec["paint_hillshade"]["hillshade-illumination-anchor"]),
"hillshade-exaggeration": new DataConstantProperty(spec["paint_hillshade"]["hillshade-exaggeration"]),
"hillshade-shadow-color": new DataConstantProperty(spec["paint_hillshade"]["hillshade-shadow-color"]),
"hillshade-highlight-color": new DataConstantProperty(spec["paint_hillshade"]["hillshade-highlight-color"]),
"hillshade-accent-color": new DataConstantProperty(spec["paint_hillshade"]["hillshade-accent-color"]),
"hillshade-emissive-strength": new DataConstantProperty(spec["paint_hillshade"]["hillshade-emissive-strength"]),
"hillshade-shadow-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"hillshade-highlight-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"hillshade-accent-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class HillshadeStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$c(),
paint: getPaintProperties$c()
};
super(layer, properties, scope, lut, options);
}
shouldRedrape() {
return this.hasOffscreenPass() && this.paint.get("hillshade-illumination-anchor") === "viewport";
}
hasOffscreenPass() {
return this.paint.get("hillshade-exaggeration") !== 0 && this.visibility !== "none";
}
getProgramIds() {
return ["hillshade", "hillshadePrepare"];
}
getDefaultProgramParams(name, zoom, lut) {
return {
overrideFog: false
};
}
}
const fillLayoutAttributes = createLayout([
{ name: "a_pos", components: 2, type: "Int16" }
], 4);
const fillLayoutAttributesExt = createLayout([
{ name: "a_road_z_offset", components: 1, type: "Float32" }
], 4);
const intersectionsAttributes = createLayout([
{ name: "a_pos", components: 2, type: "Int16" },
{ name: "a_height", components: 1, type: "Float32" }
], 4);
const intersectionNormalAttributes = createLayout([
{ name: "a_pos_normal_3", components: 3, type: "Int16" }
], 4);
const { members: members$6, size: size$6, alignment: alignment$6 } = fillLayoutAttributes;
function earcut(data, holeIndices, dim = 2) {
const hasHoles = holeIndices && holeIndices.length;
const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
let outerNode = linkedList(data, 0, outerLen, dim, true);
const triangles = [];
if (!outerNode || outerNode.next === outerNode.prev) return triangles;
let minX, minY, invSize;
if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
// if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
if (data.length > 80 * dim) {
minX = data[0];
minY = data[1];
let maxX = minX;
let maxY = minY;
for (let i = dim; i < outerLen; i += dim) {
const x = data[i];
const y = data[i + 1];
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
}
// minX, minY and invSize are later used to transform coords into integers for z-order calculation
invSize = Math.max(maxX - minX, maxY - minY);
invSize = invSize !== 0 ? 32767 / invSize : 0;
}
earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
return triangles;
}
// create a circular doubly linked list from polygon points in the specified winding order
function linkedList(data, start, end, dim, clockwise) {
let last;
if (clockwise === (signedArea(data, start, end, dim) > 0)) {
for (let i = start; i < end; i += dim) last = insertNode(i / dim | 0, data[i], data[i + 1], last);
} else {
for (let i = end - dim; i >= start; i -= dim) last = insertNode(i / dim | 0, data[i], data[i + 1], last);
}
if (last && equals(last, last.next)) {
removeNode(last);
last = last.next;
}
return last;
}
// eliminate colinear or duplicate points
function filterPoints(start, end) {
if (!start) return start;
if (!end) end = start;
let p = start,
again;
do {
again = false;
if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
removeNode(p);
p = end = p.prev;
if (p === p.next) break;
again = true;
} else {
p = p.next;
}
} while (again || p !== end);
return end;
}
// main ear slicing loop which triangulates a polygon (given as a linked list)
function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
if (!ear) return;
// interlink polygon nodes in z-order
if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
let stop = ear;
// iterate through ears, slicing them one by one
while (ear.prev !== ear.next) {
const prev = ear.prev;
const next = ear.next;
if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
triangles.push(prev.i, ear.i, next.i); // cut off the triangle
removeNode(ear);
// skipping the next vertex leads to less sliver triangles
ear = next.next;
stop = next.next;
continue;
}
ear = next;
// if we looped through the whole remaining polygon and can't find any more ears
if (ear === stop) {
// try filtering points and slicing again
if (!pass) {
earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
// if this didn't work, try curing all small self-intersections locally
} else if (pass === 1) {
ear = cureLocalIntersections(filterPoints(ear), triangles);
earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
// as a last resort, try splitting the remaining polygon into two
} else if (pass === 2) {
splitEarcut(ear, triangles, dim, minX, minY, invSize);
}
break;
}
}
}
// check whether a polygon node forms a valid ear with adjacent nodes
function isEar(ear) {
const a = ear.prev,
b = ear,
c = ear.next;
if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
// now make sure we don't have other points inside the potential ear
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
// triangle bbox
const x0 = Math.min(ax, bx, cx),
y0 = Math.min(ay, by, cy),
x1 = Math.max(ax, bx, cx),
y1 = Math.max(ay, by, cy);
let p = c.next;
while (p !== a) {
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) &&
area(p.prev, p, p.next) >= 0) return false;
p = p.next;
}
return true;
}
function isEarHashed(ear, minX, minY, invSize) {
const a = ear.prev,
b = ear,
c = ear.next;
if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
// triangle bbox
const x0 = Math.min(ax, bx, cx),
y0 = Math.min(ay, by, cy),
x1 = Math.max(ax, bx, cx),
y1 = Math.max(ay, by, cy);
// z-order range for the current triangle bbox;
const minZ = zOrder(x0, y0, minX, minY, invSize),
maxZ = zOrder(x1, y1, minX, minY, invSize);
let p = ear.prevZ,
n = ear.nextZ;
// look for points inside the triangle in both directions
while (p && p.z >= minZ && n && n.z <= maxZ) {
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
p = p.prevZ;
if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
n = n.nextZ;
}
// look for remaining points in decreasing z-order
while (p && p.z >= minZ) {
if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
p = p.prevZ;
}
// look for remaining points in increasing z-order
while (n && n.z <= maxZ) {
if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
n = n.nextZ;
}
return true;
}
// go through all polygon nodes and cure small local self-intersections
function cureLocalIntersections(start, triangles) {
let p = start;
do {
const a = p.prev,
b = p.next.next;
if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
triangles.push(a.i, p.i, b.i);
// remove two nodes involved
removeNode(p);
removeNode(p.next);
p = start = b;
}
p = p.next;
} while (p !== start);
return filterPoints(p);
}
// try splitting polygon into two and triangulate them independently
function splitEarcut(start, triangles, dim, minX, minY, invSize) {
// look for a valid diagonal that divides the polygon into two
let a = start;
do {
let b = a.next.next;
while (b !== a.prev) {
if (a.i !== b.i && isValidDiagonal(a, b)) {
// split the polygon in two by the diagonal
let c = splitPolygon(a, b);
// filter colinear points around the cuts
a = filterPoints(a, a.next);
c = filterPoints(c, c.next);
// run earcut on each half
earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
return;
}
b = b.next;
}
a = a.next;
} while (a !== start);
}
// link every hole into the outer loop, producing a single-ring polygon without holes
function eliminateHoles(data, holeIndices, outerNode, dim) {
const queue = [];
for (let i = 0, len = holeIndices.length; i < len; i++) {
const start = holeIndices[i] * dim;
const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
const list = linkedList(data, start, end, dim, false);
if (list === list.next) list.steiner = true;
queue.push(getLeftmost(list));
}
queue.sort(compareXYSlope);
// process holes from left to right
for (let i = 0; i < queue.length; i++) {
outerNode = eliminateHole(queue[i], outerNode);
}
return outerNode;
}
function compareXYSlope(a, b) {
let result = a.x - b.x;
// when the left-most point of 2 holes meet at a vertex, sort the holes counterclockwise so that when we find
// the bridge to the outer shell is always the point that they meet at.
if (result === 0) {
result = a.y - b.y;
if (result === 0) {
const aSlope = (a.next.y - a.y) / (a.next.x - a.x);
const bSlope = (b.next.y - b.y) / (b.next.x - b.x);
result = aSlope - bSlope;
}
}
return result;
}
// find a bridge between vertices that connects hole with an outer ring and link it
function eliminateHole(hole, outerNode) {
const bridge = findHoleBridge(hole, outerNode);
if (!bridge) {
return outerNode;
}
const bridgeReverse = splitPolygon(bridge, hole);
// filter collinear points around the cuts
filterPoints(bridgeReverse, bridgeReverse.next);
return filterPoints(bridge, bridge.next);
}
// David Eberly's algorithm for finding a bridge between hole and outer polygon
function findHoleBridge(hole, outerNode) {
let p = outerNode;
const hx = hole.x;
const hy = hole.y;
let qx = -Infinity;
let m;
// find a segment intersected by a ray from the hole's leftmost point to the left;
// segment's endpoint with lesser x will be potential connection point
// unless they intersect at a vertex, then choose the vertex
if (equals(hole, p)) return p;
do {
if (equals(hole, p.next)) return p.next;
else if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
const x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
if (x <= hx && x > qx) {
qx = x;
m = p.x < p.next.x ? p : p.next;
if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
}
}
p = p.next;
} while (p !== outerNode);
if (!m) return null;
// look for points inside the triangle of hole point, segment intersection and endpoint;
// if there are no points found, we have a valid connection;
// otherwise choose the point of the minimum angle with the ray as connection point
const stop = m;
const mx = m.x;
const my = m.y;
let tanMin = Infinity;
p = m;
do {
if (hx >= p.x && p.x >= mx && hx !== p.x &&
pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
const tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
if (locallyInside(p, hole) &&
(tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
m = p;
tanMin = tan;
}
}
p = p.next;
} while (p !== stop);
return m;
}
// whether sector in vertex m contains sector in vertex p in the same coordinates
function sectorContainsSector(m, p) {
return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
}
// interlink polygon nodes in z-order
function indexCurve(start, minX, minY, invSize) {
let p = start;
do {
if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
p.prevZ = p.prev;
p.nextZ = p.next;
p = p.next;
} while (p !== start);
p.prevZ.nextZ = null;
p.prevZ = null;
sortLinked(p);
}
// Simon Tatham's linked list merge sort algorithm
// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
function sortLinked(list) {
let numMerges;
let inSize = 1;
do {
let p = list;
let e;
list = null;
let tail = null;
numMerges = 0;
while (p) {
numMerges++;
let q = p;
let pSize = 0;
for (let i = 0; i < inSize; i++) {
pSize++;
q = q.nextZ;
if (!q) break;
}
let qSize = inSize;
while (pSize > 0 || (qSize > 0 && q)) {
if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
e = p;
p = p.nextZ;
pSize--;
} else {
e = q;
q = q.nextZ;
qSize--;
}
if (tail) tail.nextZ = e;
else list = e;
e.prevZ = tail;
tail = e;
}
p = q;
}
tail.nextZ = null;
inSize *= 2;
} while (numMerges > 1);
return list;
}
// z-order of a point given coords and inverse of the longer side of data bbox
function zOrder(x, y, minX, minY, invSize) {
// coords are transformed into non-negative 15-bit integer range
x = (x - minX) * invSize | 0;
y = (y - minY) * invSize | 0;
x = (x | (x << 8)) & 0x00FF00FF;
x = (x | (x << 4)) & 0x0F0F0F0F;
x = (x | (x << 2)) & 0x33333333;
x = (x | (x << 1)) & 0x55555555;
y = (y | (y << 8)) & 0x00FF00FF;
y = (y | (y << 4)) & 0x0F0F0F0F;
y = (y | (y << 2)) & 0x33333333;
y = (y | (y << 1)) & 0x55555555;
return x | (y << 1);
}
// find the leftmost node of a polygon ring
function getLeftmost(start) {
let p = start,
leftmost = start;
do {
if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
p = p.next;
} while (p !== start);
return leftmost;
}
// check if a point lies within a convex triangle
function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
(ax - px) * (by - py) >= (bx - px) * (ay - py) &&
(bx - px) * (cy - py) >= (cx - px) * (by - py);
}
// check if a point lies within a convex triangle but false if its equal to the first point of the triangle
function pointInTriangleExceptFirst(ax, ay, bx, by, cx, cy, px, py) {
return !(ax === px && ay === py) && pointInTriangle(ax, ay, bx, by, cx, cy, px, py);
}
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
function isValidDiagonal(a, b) {
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // doesn't intersect other edges
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
}
// signed area of a triangle
function area(p, q, r) {
return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
}
// check if two points are equal
function equals(p1, p2) {
return p1.x === p2.x && p1.y === p2.y;
}
// check if two segments intersect
function intersects(p1, q1, p2, q2) {
const o1 = sign(area(p1, q1, p2));
const o2 = sign(area(p1, q1, q2));
const o3 = sign(area(p2, q2, p1));
const o4 = sign(area(p2, q2, q1));
if (o1 !== o2 && o3 !== o4) return true; // general case
if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
return false;
}
// for collinear points p, q, r, check if point q lies on segment pr
function onSegment(p, q, r) {
return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
}
function sign(num) {
return num > 0 ? 1 : num < 0 ? -1 : 0;
}
// check if a polygon diagonal intersects any polygon segments
function intersectsPolygon(a, b) {
let p = a;
do {
if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
intersects(p, p.next, a, b)) return true;
p = p.next;
} while (p !== a);
return false;
}
// check if a polygon diagonal is locally inside the polygon
function locallyInside(a, b) {
return area(a.prev, a, a.next) < 0 ?
area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
}
// check if the middle point of a polygon diagonal is inside the polygon
function middleInside(a, b) {
let p = a;
let inside = false;
const px = (a.x + b.x) / 2;
const py = (a.y + b.y) / 2;
do {
if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
(px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
inside = !inside;
p = p.next;
} while (p !== a);
return inside;
}
// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
// if one belongs to the outer ring and another to a hole, it merges it into a single ring
function splitPolygon(a, b) {
const a2 = createNode(a.i, a.x, a.y),
b2 = createNode(b.i, b.x, b.y),
an = a.next,
bp = b.prev;
a.next = b;
b.prev = a;
a2.next = an;
an.prev = a2;
b2.next = a2;
a2.prev = b2;
bp.next = b2;
b2.prev = bp;
return b2;
}
// create a node and optionally link it with previous one (in a circular doubly linked list)
function insertNode(i, x, y, last) {
const p = createNode(i, x, y);
if (!last) {
p.prev = p;
p.next = p;
} else {
p.next = last.next;
p.prev = last;
last.next.prev = p;
last.next = p;
}
return p;
}
function removeNode(p) {
p.next.prev = p.prev;
p.prev.next = p.next;
if (p.prevZ) p.prevZ.nextZ = p.nextZ;
if (p.nextZ) p.nextZ.prevZ = p.prevZ;
}
function createNode(i, x, y) {
return {
i, // vertex index in coordinates array
x, y, // vertex coordinates
prev: null, // previous and next vertex nodes in a polygon ring
next: null,
z: 0, // z-order curve value
prevZ: null, // previous and next nodes in z-order
nextZ: null,
steiner: false // indicates whether this is a steiner point
};
}
// return a percentage difference between the polygon area and its triangulation area;
// used to verify correctness of triangulation
function deviation(data, holeIndices, dim, triangles) {
const hasHoles = holeIndices && holeIndices.length;
const outerLen = hasHoles ? holeIndices[0] * dim : data.length;
let polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
if (hasHoles) {
for (let i = 0, len = holeIndices.length; i < len; i++) {
const start = holeIndices[i] * dim;
const end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
polygonArea -= Math.abs(signedArea(data, start, end, dim));
}
}
let trianglesArea = 0;
for (let i = 0; i < triangles.length; i += 3) {
const a = triangles[i] * dim;
const b = triangles[i + 1] * dim;
const c = triangles[i + 2] * dim;
trianglesArea += Math.abs(
(data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
(data[a] - data[b]) * (data[c + 1] - data[a + 1]));
}
return polygonArea === 0 && trianglesArea === 0 ? 0 :
Math.abs((trianglesArea - polygonArea) / polygonArea);
}
function signedArea(data, start, end, dim) {
let sum = 0;
for (let i = start, j = end - dim; i < end; i += dim) {
sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
j = i;
}
return sum;
}
// turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
function flatten(data) {
const vertices = [];
const holes = [];
const dimensions = data[0][0].length;
let holeIndex = 0;
let prevLen = 0;
for (const ring of data) {
for (const p of ring) {
for (let d = 0; d < dimensions; d++) vertices.push(p[d]);
}
if (prevLen) {
holeIndex += prevLen;
holes.push(holeIndex);
}
prevLen = ring.length;
}
return {vertices, holes, dimensions};
}
function classifyRings(rings, maxRings) {
const len = rings.length;
if (len <= 1) return [rings];
const polygons = [];
let polygon;
let ccw;
for (let i = 0; i < len; i++) {
const area = calculateSignedArea$1(rings[i]);
if (area === 0) continue;
rings[i].area = Math.abs(area);
if (ccw === void 0) ccw = area < 0;
if (ccw === area < 0) {
if (polygon) polygons.push(polygon);
polygon = [rings[i]];
} else {
polygon.push(rings[i]);
}
}
if (polygon) polygons.push(polygon);
if (maxRings > 1) {
for (let j = 0; j < polygons.length; j++) {
if (polygons[j].length <= maxRings) continue;
quickselect(polygons[j], maxRings, 1, polygons[j].length - 1, compareAreas);
polygons[j] = polygons[j].slice(0, maxRings);
}
}
return polygons;
}
function compareAreas(a, b) {
return b.area - a.area;
}
function addPattern(pattern, patterns, pixelRatio = 1) {
if (!pattern) {
return null;
}
const patternPrimary = typeof pattern === "string" ? ResolvedImage.from(pattern).getPrimary() : pattern.getPrimary();
const patternSecondary = typeof pattern === "string" ? null : pattern.getSecondary();
for (const pattern2 of [patternPrimary, patternSecondary]) {
if (!pattern2) {
continue;
}
const id = pattern2.id.toString();
if (!patterns.has(id)) {
patterns.set(id, []);
}
pattern2.scaleSelf(pixelRatio);
patterns.get(id).push(pattern2);
}
return {
primary: patternPrimary.toString(),
secondary: patternSecondary ? patternSecondary.toString() : null
};
}
function hasPattern(type, layers, pixelRatio, options) {
const patterns = options.patternDependencies;
let hasPattern2 = false;
for (const layer of layers) {
const patternProperty = layer.paint.get(`${type}-pattern`);
if (!patternProperty.isConstant()) {
hasPattern2 = true;
}
const constantPattern = patternProperty.constantOr(null);
if (addPattern(constantPattern, patterns, pixelRatio)) {
hasPattern2 = true;
}
}
return hasPattern2;
}
function addPatternDependencies(type, layers, patternFeature, zoom, pixelRatio, options) {
const patterns = options.patternDependencies;
for (const layer of layers) {
const patternProperty = layer.paint.get(`${type}-pattern`);
const patternPropertyValue = patternProperty.value;
if (patternPropertyValue.kind !== "constant") {
let pattern = patternPropertyValue.evaluate({ zoom }, patternFeature, {}, options.availableImages);
pattern = pattern && pattern.name ? pattern.name : pattern;
const patternResult = addPattern(pattern, patterns, pixelRatio);
if (!patternResult) {
continue;
}
const {
primary: primarySerialized,
secondary: secondarySerialized
} = patternResult;
if (primarySerialized) {
patternFeature.patterns[layer.id] = [primarySerialized, secondarySerialized].filter(Boolean);
}
}
}
return patternFeature;
}
class ElevationPolygons {
constructor() {
this.polygons = /* @__PURE__ */ new Map();
}
add(key, ...values) {
const poly = this.polygons.get(key);
if (!poly) {
this.polygons.set(key, values);
} else {
poly.push(...values);
}
}
merge(elevationPolygons) {
for (const [key, value] of elevationPolygons.polygons) {
this.add(key, ...value);
}
}
}
class ElevationPortalGraph {
constructor() {
this.portals = [];
}
static isOnBorder(a, b) {
return a <= 0 && b <= 0 || a >= EXTENT && b >= EXTENT;
}
// Constructs a single graph by combining portals of multiple graphs
static evaluate(unevaluatedPortals) {
if (unevaluatedPortals.length === 0) return new ElevationPortalGraph();
let portals = [];
for (const unevalGraph of unevaluatedPortals) {
portals.push(...unevalGraph.portals);
}
if (portals.length === 0) return new ElevationPortalGraph();
for (const portal of portals) {
const a = portal.va;
const b = portal.vb;
if (ElevationPortalGraph.isOnBorder(a.x, b.x) || ElevationPortalGraph.isOnBorder(a.y, b.y)) {
portal.type = "border";
}
}
const evaluatedGroup = portals.filter((p) => p.type !== "unevaluated");
const unevaluatedGroup = portals.filter((p) => p.type === "unevaluated");
if (unevaluatedGroup.length === 0) return new ElevationPortalGraph();
unevaluatedGroup.sort((a, b) => a.hash === b.hash ? a.isTunnel === b.isTunnel ? 0 : a.isTunnel ? -1 : 1 : a.hash < b.hash ? 1 : -1);
portals = evaluatedGroup.concat(unevaluatedGroup);
let begin = evaluatedGroup.length;
let end = begin;
let out = begin;
assert$1(begin < portals.length);
do {
end++;
if (end === portals.length || portals[begin].hash !== portals[end].hash) {
assert$1(end - begin <= 2);
if (end - begin === 2) {
if (out < begin) {
portals[out] = portals[begin];
portals[begin] = null;
}
const outPortal = portals[out];
const endPortal = portals[end - 1];
outPortal.type = outPortal.isTunnel !== endPortal.isTunnel ? "tunnel" : "polygon";
outPortal.connection = { a: outPortal.connection.a, b: endPortal.connection.a };
out++;
}
begin = end;
}
} while (begin !== portals.length);
portals.splice(out);
portals.sort((a, b) => a.hash < b.hash ? 1 : -1);
assert$1(portals.every((p) => p.type !== "unevaluated"));
return { portals };
}
}
register(ElevationPortalGraph, "ElevationPortalGraph");
register(ElevationPolygons, "ElevationPolygons");
const TUNNEL_ENTERANCE_HEIGHT = 4;
class MeshBuilder {
constructor(vertices, normals, indices) {
this.outPositions = vertices;
this.outNormals = normals;
this.outIndices = indices;
this.vertexLookup = /* @__PURE__ */ new Map();
}
addVertex(vertex, normal, tileToMeter2) {
let height = vertex[2];
if (tileToMeter2 != null) {
height *= tileToMeter2;
}
const lookup = `${vertex[0]},${vertex[1]},${vertex[2]},${normal[0]},${normal[1]},${normal[2]}`;
const result = this.vertexLookup.get(lookup);
if (result != null) {
return result;
}
const offset = this.outPositions.length;
this.vertexLookup.set(lookup, offset);
const normX = Math.trunc(normal[0] * (1 << 14));
const normY = Math.trunc(normal[1] * (1 << 14));
const normZ = Math.trunc(normal[2] * (1 << 14));
this.outPositions.emplaceBack(vertex[0], vertex[1], height);
this.outNormals.emplaceBack(normX, normY, normZ);
return offset;
}
addTriangle(i1, i2, i3) {
assert$1(i1 < this.outPositions.length && i2 < this.outPositions.length && i3 < this.outPositions.length);
this.outIndices.emplaceBack(i1, i2, i3);
}
addTriangles(indices, vertices, heights) {
if (indices.length === 0) return;
assert$1(indices.length % 3 === 0);
assert$1(vertices.length === heights.length || heights.length === 1);
const constantHeight = heights.length === 1;
const tmpVec = create$4();
const normal = create$4();
for (let i = 0; i < indices.length; i += 3) {
const v0 = vertices[indices[i + 0]];
const v1 = vertices[indices[i + 1]];
const v2 = vertices[indices[i + 2]];
const h0 = constantHeight ? heights[0] : heights[indices[i + 0]];
const h1 = constantHeight ? heights[0] : heights[indices[i + 1]];
const h2 = constantHeight ? heights[0] : heights[indices[i + 2]];
set$4(tmpVec, v0.x, v0.y, h0);
const i0 = this.addVertex(tmpVec, normal);
set$4(tmpVec, v1.x, v1.y, h1);
const i1 = this.addVertex(tmpVec, normal);
set$4(tmpVec, v2.x, v2.y, h2);
const i2 = this.addVertex(tmpVec, normal);
this.outIndices.emplaceBack(i0, i1, i2);
}
}
addQuad(p1, p2, p3, p4, normal, tileToMeters) {
const a = this.addVertex(p1, normal, tileToMeters);
const b = this.addVertex(p2, normal, tileToMeters);
const c = this.addVertex(p3, normal, tileToMeters);
const d = this.addVertex(p4, normal, tileToMeters);
this.addTriangle(a, b, c);
this.addTriangle(c, d, a);
}
getVertexCount() {
return this.outPositions.length;
}
clearVertexLookup() {
this.vertexLookup.clear();
}
}
class ElevatedStructures {
constructor(tileID, layers, zoom, lut) {
this.unevaluatedPortals = new ElevationPortalGraph();
this.portalPolygons = new ElevationPolygons();
// Tracks the rail/tunnel mesh same-feature vertex sections
// (within ElevatedStructure::vertexPositions).
// To be used for later populating the PaintPropertyBinder vertex vector
this.bridgeFeatureSections = [];
this.tunnelFeatureSections = [];
this.vertexHashLookup = /* @__PURE__ */ new Map();
this.unevalVertices = [];
this.unevalHeights = [];
this.unevalTriangles = [];
this.unevalTunnelTriangles = [];
this.unevalEdges = [];
this.vertexPositions = new StructArrayLayout2i1f8();
this.vertexNormals = new StructArrayLayout3i8();
this.indexArray = new StructArrayLayout3ui6();
this.tileToMeters = tileToMeter(tileID);
this.bridgeProgramConfigurations = new ProgramConfigurationSet(layers, { zoom, lut }, (name) => name !== "fill-tunnel-structure-color");
this.tunnelProgramConfigurations = new ProgramConfigurationSet(layers, { zoom, lut }, (name) => name !== "fill-bridge-guard-rail-color");
}
addVertices(vertices, heights) {
assert$1(this.unevalVertices.length === this.unevalHeights.length);
assert$1(vertices.length > 0 && vertices.length === heights.length);
const offset = this.unevalVertices.length;
for (let i = 0; i < vertices.length; i++) {
this.unevalVertices.push(vertices[i]);
this.unevalHeights.push(heights[i]);
}
return offset;
}
addTriangles(indices, offset, isTunnel) {
assert$1(indices.length > 0);
assert$1(offset >= 0);
const outTriangles = isTunnel ? this.unevalTunnelTriangles : this.unevalTriangles;
for (const i of indices) {
const idx = i + offset;
assert$1(idx < this.unevalVertices.length);
outTriangles.push(idx);
}
}
addRenderableRing(polygonIdx, vertexOffset, count, isTunnel, area, featureInfo) {
assert$1(vertexOffset + count <= this.unevalVertices.length);
const corners = [
new Point(area.min.x, area.min.y),
new Point(area.max.x, area.min.y),
new Point(area.max.x, area.max.y),
new Point(area.min.x, area.max.y)
];
for (let i = 0; i < count - 1; i++) {
const ai = vertexOffset + i;
const bi = ai + 1;
const va = this.unevalVertices[ai];
const vb = this.unevalVertices[bi];
const insideBounds = va.x >= area.min.x && va.x <= area.max.x && va.y >= area.min.y && va.y <= area.max.y || vb.x >= area.min.x && vb.x <= area.max.x && vb.y >= area.min.y && vb.y <= area.max.y;
if (!insideBounds && !edgeIntersectsBox(va, vb, corners)) {
continue;
}
if (this.isOnBorder(va.x, vb.x) || this.isOnBorder(va.y, vb.y)) {
continue;
}
const edgeHash = ElevatedStructures.computeEdgeHash(this.unevalVertices[ai], this.unevalVertices[bi]);
let portalHash;
let lookup = this.vertexHashLookup.get(ElevatedStructures.computePosHash(va));
if (lookup != null) {
portalHash = lookup.next;
} else {
lookup = this.vertexHashLookup.get(ElevatedStructures.computePosHash(vb));
portalHash = lookup != null ? lookup.prev : edgeHash;
}
this.unevalEdges.push({ polygonIdx, a: ai, b: bi, hash: edgeHash, portalHash, isTunnel, type: "unevaluated", featureInfo });
}
}
addPortalCandidates(id, polygon, isTunnel, elevation, zLevel) {
if (polygon.length === 0) return;
const leveledPoly = { geometry: polygon, zLevel };
this.portalPolygons.add(id, leveledPoly);
const pointsEqual = (a, b) => a.x === b.x && a.y === b.y;
const exterior = polygon[0];
assert$1(exterior.length > 1 && pointsEqual(exterior[0], exterior[exterior.length - 1]));
this.vertexHashLookup.clear();
let prevEdgeHash = ElevatedStructures.computeEdgeHash(exterior[exterior.length - 2], exterior[exterior.length - 1]);
for (let i = 0; i < exterior.length - 1; i++) {
const a = exterior[i + 0];
const b = exterior[i + 1];
const vavb = fromValues(b.x - a.x, b.y - a.y);
const length$1 = length(vavb);
if (length$1 === 0) continue;
let type = "unevaluated";
const ha = elevation.pointElevation(a);
const hb = elevation.pointElevation(b);
const onGround = Math.abs(ha) < 0.01 && Math.abs(hb) < 0.01;
if (onGround) {
type = "entrance";
} else {
if (this.isOnBorder(a.x, b.x) || this.isOnBorder(a.y, b.y)) {
type = "border";
}
}
const edgeHash = ElevatedStructures.computeEdgeHash(a, b);
this.unevaluatedPortals.portals.push({
connection: { a: id, b: void 0 },
va: a,
vb: b,
vab: vavb,
length: length$1,
hash: edgeHash,
isTunnel,
type
});
const posHash = ElevatedStructures.computePosHash(a);
assert$1(!this.vertexHashLookup.has(posHash));
this.vertexHashLookup.set(posHash, { prev: prevEdgeHash, next: edgeHash });
prevEdgeHash = edgeHash;
}
}
construct(evaluatedPortals) {
if (this.unevalVertices.length === 0) return;
assert$1(this.vertexPositions.length === 0 && this.vertexNormals.length === 0 && this.indexArray.length === 0);
const beginSegment = () => ({ vertexOffset: 0, primitiveOffset: this.indexArray.length });
const endSegment = (segment) => {
segment.primitiveLength = this.indexArray.length - segment.primitiveOffset;
};
const builder = new MeshBuilder(this.vertexPositions, this.vertexNormals, this.indexArray);
this.prepareEdges(evaluatedPortals.portals, this.unevalEdges);
const shadowCasterSegment = beginSegment();
const depthSegment = beginSegment();
const renderableBridgeSegment = beginSegment();
const partition = (edges, type) => {
edges.sort((a, b) => {
if (a.type === type && b.type !== type) return -1;
else if (a.type !== type && b.type === type) return 1;
return 0;
});
const idx = edges.findIndex((e) => e.type !== type);
return idx >= 0 ? idx : edges.length;
};
let wallEndIdx = 0;
if (this.unevalEdges.length > 0) {
wallEndIdx = partition(this.unevalEdges, "none");
assert$1(wallEndIdx >= 0);
this.constructBridgeStructures(
builder,
this.unevalVertices,
this.unevalHeights,
this.unevalEdges,
{ min: 0, max: wallEndIdx },
this.tileToMeters
);
}
endSegment(renderableBridgeSegment);
const renderableTunnelSegment = beginSegment();
const maskSegment = beginSegment();
if (this.unevalEdges.length > 0) {
const afterWallEnd = this.unevalEdges.splice(wallEndIdx);
const tunnelEndIdx = partition(afterWallEnd, "tunnel") + wallEndIdx;
this.unevalEdges.push(...afterWallEnd);
assert$1(wallEndIdx <= tunnelEndIdx && tunnelEndIdx >= 0 && tunnelEndIdx <= this.unevalEdges.length);
this.constructTunnelStructures(
builder,
this.unevalVertices,
this.unevalHeights,
this.unevalEdges,
{ min: 0, max: wallEndIdx },
{ min: wallEndIdx, max: tunnelEndIdx }
);
}
endSegment(renderableTunnelSegment);
builder.addTriangles(this.unevalTriangles, this.unevalVertices, this.unevalHeights);
endSegment(maskSegment);
builder.addTriangles(this.unevalTunnelTriangles, this.unevalVertices, this.unevalHeights);
endSegment(depthSegment);
builder.addTriangles(this.unevalTunnelTriangles, this.unevalVertices, [-0.1]);
endSegment(shadowCasterSegment);
this.maskSegments = SegmentVector.simpleSegment(0, maskSegment.primitiveOffset, 0, maskSegment.primitiveLength);
this.depthSegments = SegmentVector.simpleSegment(0, depthSegment.primitiveOffset, 0, depthSegment.primitiveLength);
this.renderableBridgeSegments = SegmentVector.simpleSegment(0, renderableBridgeSegment.primitiveOffset, 0, renderableBridgeSegment.primitiveLength);
this.renderableTunnelSegments = SegmentVector.simpleSegment(0, renderableTunnelSegment.primitiveOffset, 0, renderableTunnelSegment.primitiveLength);
this.shadowCasterSegments = SegmentVector.simpleSegment(0, shadowCasterSegment.primitiveOffset, 0, shadowCasterSegment.primitiveLength);
assert$1(this.vertexPositions.length === this.vertexNormals.length);
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, worldview) {
this.bridgeProgramConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview);
this.tunnelProgramConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview);
}
upload(context) {
if (this.vertexBuffer || this.vertexPositions.length === 0 || this.vertexNormals.length === 0 || this.indexArray.length === 0) {
return;
}
this.vertexBuffer = context.createVertexBuffer(this.vertexPositions, intersectionsAttributes.members);
this.vertexBufferNormal = context.createVertexBuffer(this.vertexNormals, intersectionNormalAttributes.members);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
this.bridgeProgramConfigurations.upload(context);
this.tunnelProgramConfigurations.upload(context);
}
destroy() {
if (this.vertexBuffer) {
this.vertexBuffer.destroy();
this.vertexBufferNormal.destroy();
this.indexBuffer.destroy();
}
if (this.maskSegments) {
this.maskSegments.destroy();
this.depthSegments.destroy();
this.renderableBridgeSegments.destroy();
this.renderableTunnelSegments.destroy();
this.shadowCasterSegments.destroy();
}
this.bridgeProgramConfigurations.destroy();
this.tunnelProgramConfigurations.destroy();
}
populatePaintArrays(vtLayer, canonical, availableImages, brightness, worldview) {
const populate = (programConfigurations, sections) => {
for (let i = 0; i < sections.length - 1; i++) {
const featureIndex = sections[i].featureIndex;
const length = sections[i + 1].vertexStart;
assert$1(Number.isFinite(featureIndex));
const feature = vtLayer.feature(featureIndex);
assert$1(feature);
programConfigurations.populatePaintArrays(length, feature, featureIndex, {}, availableImages, canonical, brightness, void 0, worldview);
}
};
populate(this.bridgeProgramConfigurations, this.bridgeFeatureSections);
populate(this.tunnelProgramConfigurations, this.tunnelFeatureSections);
}
computeVertexConnections(vertices, heights, edges, startEdge, endEdge) {
assert$1(endEdge <= edges.length);
const map = /* @__PURE__ */ new Map();
for (let i = startEdge; i < endEdge; i++) {
const edge = edges[i];
const a = edge.a;
const b = edge.b;
const aHash = ElevatedStructures.computePosHash(vertices[a]);
const bHash = ElevatedStructures.computePosHash(vertices[b]);
let pA = map.get(aHash);
if (!pA) {
pA = {};
map.set(aHash, pA);
}
let pB = map.get(bHash);
if (!pB) {
pB = {};
map.set(bHash, pB);
}
if (heights[a] <= 0 && heights[b] <= 0) {
continue;
}
pA.to = b;
pB.from = a;
}
return map;
}
isTerminalVertex(vertexIdx, connectivity) {
const posHash = ElevatedStructures.computePosHash(this.unevalVertices[vertexIdx]);
const conn = connectivity.get(posHash);
return !conn || !conn.from || !conn.to;
}
constructBridgeStructures(builder, vertices, heights, edges, edgeRange, tileToMeters) {
builder.clearVertexLookup();
const vertexConnectivity = this.computeVertexConnections(vertices, heights, edges, edgeRange.min, edgeRange.max);
const metersToTile = 1 / tileToMeters;
const scale = 0.5 * metersToTile;
const toTileVec = (v, vIdx) => set$4(v, vertices[vIdx].x, vertices[vIdx].y, heights[vIdx] * metersToTile);
const fromVec = create$4();
const midVec = create$4();
const toVec = create$4();
const fwd = create$4();
const sub = create$4();
const computeFwd = (out, vIdx) => {
const connectivity = vertexConnectivity.get(ElevatedStructures.computePosHash(vertices[vIdx]));
assert$1(connectivity);
const from = connectivity.from;
const to = connectivity.to;
if (!from || !to) return void 0;
toTileVec(fromVec, from);
toTileVec(midVec, vIdx);
toTileVec(toVec, to);
zero$2(fwd);
if (!exactEquals$4(fromVec, midVec)) {
sub$2(sub, midVec, fromVec);
normalize$4(fwd, sub);
}
if (!exactEquals$4(toVec, midVec)) {
sub$2(sub, toVec, midVec);
add$4(fwd, fwd, normalize$4(sub, sub));
}
const len = len$4(fwd);
return len > 0 ? scale$4(out, fwd, 1 / len) : void 0;
};
let lastFeatureIndex = Number.POSITIVE_INFINITY;
this.sortSubarray(edges, edgeRange.min, edgeRange.max, (a, b) => a.featureInfo.featureIndex - b.featureInfo.featureIndex);
const va = create$4();
const vb = create$4();
const dir = create$4();
const aLeft = create$4();
const bLeft = create$4();
const aUp = create$4();
const bUp = create$4();
const tmpVec1 = create$4();
const tmpVec2 = create$4();
const aVertices = [create$4(), create$4(), create$4(), create$4()];
const bVertices = [create$4(), create$4(), create$4(), create$4()];
const bridgeEdge = [{ coord: new Point(0, 0), height: 0 }, { coord: new Point(0, 0), height: 0 }];
const compare = (a, b) => a > b;
for (let i = edgeRange.min; i < edgeRange.max; i++) {
const edge = edges[i];
if (!edge.featureInfo.guardRailEnabled) continue;
const result = this.prepareEdgePoints(bridgeEdge, vertices, heights, edge, compare);
if (!result) continue;
const [pa, pb] = bridgeEdge;
set$4(va, pa.coord.x, pa.coord.y, metersToTile * pa.height);
set$4(vb, pb.coord.x, pb.coord.y, metersToTile * pb.height);
if (exactEquals$4(va, vb)) continue;
sub$2(dir, vb, va);
normalize$4(dir, dir);
const aFwd = computeFwd(tmpVec1, edge.a) || dir;
const bFwd = computeFwd(tmpVec2, edge.b) || dir;
set$4(aLeft, aFwd[1], -aFwd[0], 0);
normalize$4(aLeft, aLeft);
set$4(bLeft, bFwd[1], -bFwd[0], 0);
normalize$4(bLeft, bLeft);
cross$2(tmpVec1, aLeft, aFwd);
normalize$4(aUp, tmpVec1);
cross$2(tmpVec1, bLeft, bFwd);
normalize$4(bUp, tmpVec1);
add$4(aVertices[0], va, scale$4(tmpVec1, sub$2(tmpVec1, aLeft, aUp), scale));
add$4(aVertices[1], va, scale$4(tmpVec1, add$4(tmpVec1, aLeft, aUp), scale));
add$4(aVertices[2], va, scale$4(tmpVec1, aUp, scale));
aVertices[3] = va;
add$4(bVertices[0], vb, scale$4(tmpVec1, sub$2(tmpVec1, bLeft, bUp), scale));
add$4(bVertices[1], vb, scale$4(tmpVec1, add$4(tmpVec1, bLeft, bUp), scale));
add$4(bVertices[2], vb, scale$4(tmpVec1, bUp, scale));
bVertices[3] = vb;
lastFeatureIndex = this.addFeatureSection(edge.featureInfo.featureIndex, lastFeatureIndex, this.bridgeFeatureSections, builder);
const ao0 = builder.addVertex(aVertices[0], aLeft, tileToMeters);
const ao1 = builder.addVertex(aVertices[1], aLeft, tileToMeters);
const bo0 = builder.addVertex(bVertices[0], bLeft, tileToMeters);
const bo1 = builder.addVertex(bVertices[1], bLeft, tileToMeters);
builder.addTriangle(ao0, ao1, bo0);
builder.addTriangle(ao1, bo1, bo0);
const at0 = builder.addVertex(aVertices[1], aUp, tileToMeters);
const at1 = builder.addVertex(aVertices[2], aUp, tileToMeters);
const bt0 = builder.addVertex(bVertices[1], bUp, tileToMeters);
const bt1 = builder.addVertex(bVertices[2], bUp, tileToMeters);
builder.addTriangle(at0, at1, bt0);
builder.addTriangle(at1, bt1, bt0);
negate$2(aLeft, aLeft);
negate$2(bLeft, bLeft);
const ai0 = builder.addVertex(aVertices[2], aLeft, tileToMeters);
const ai1 = builder.addVertex(aVertices[3], aLeft, tileToMeters);
const bi0 = builder.addVertex(bVertices[2], bLeft, tileToMeters);
const bi1 = builder.addVertex(bVertices[3], bLeft, tileToMeters);
builder.addTriangle(ai0, ai1, bi0);
builder.addTriangle(ai1, bi1, bi0);
const aIsTerminal = this.isTerminalVertex(edge.a, vertexConnectivity);
const bIsTerminal = this.isTerminalVertex(edge.b, vertexConnectivity);
if (pa.height < 0.01 && aIsTerminal) {
builder.addQuad(aVertices[3], aVertices[2], aVertices[1], aVertices[0], negate$2(aFwd, aFwd), tileToMeters);
}
if (pb.height < 0.01 && bIsTerminal) {
builder.addQuad(bVertices[0], bVertices[1], bVertices[2], bVertices[3], bFwd, tileToMeters);
}
}
this.bridgeFeatureSections.push({ featureIndex: Number.POSITIVE_INFINITY, vertexStart: builder.getVertexCount() });
assert$1(this.bridgeFeatureSections.every((sec, i) => {
return i === 0 || this.bridgeFeatureSections[i - 1].vertexStart <= sec.vertexStart;
}));
}
constructTunnelStructures(builder, vertices, heights, edges, wallRange, entranceRange) {
builder.clearVertexLookup();
const tunnelEntranceHeight = TUNNEL_ENTERANCE_HEIGHT;
let lastFeatureIndex = Number.POSITIVE_INFINITY;
const sortFn = (a, b) => a.featureInfo.featureIndex - b.featureInfo.featureIndex;
this.sortSubarray(edges, wallRange.min, wallRange.max, sortFn);
this.sortSubarray(edges, entranceRange.min, entranceRange.max, sortFn);
const normalize = (v) => normalize$4(v, v);
const tunnelEdge = [{ coord: new Point(0, 0), height: 0 }, { coord: new Point(0, 0), height: 0 }];
const compare = (a, b) => a < b;
const v1 = create$4();
const v2 = create$4();
const v3 = create$4();
const v4 = create$4();
const tmpVec = create$4();
for (let i = wallRange.min; i < wallRange.max; i++) {
const result = this.prepareEdgePoints(tunnelEdge, vertices, heights, edges[i], compare);
if (!result) continue;
const [a, b] = tunnelEdge;
const norm = normalize(set$4(tmpVec, -(b.coord.y - a.coord.y), b.coord.x - a.coord.x, 0));
lastFeatureIndex = this.addFeatureSection(edges[i].featureInfo.featureIndex, lastFeatureIndex, this.tunnelFeatureSections, builder);
builder.addQuad(
set$4(v1, a.coord.x, a.coord.y, a.height),
set$4(v2, b.coord.x, b.coord.y, b.height),
set$4(v3, b.coord.x, b.coord.y, edges[i].isTunnel ? -0.1 : 0),
set$4(v4, a.coord.x, a.coord.y, edges[i].isTunnel ? -0.1 : 0),
norm
);
}
for (let i = entranceRange.min; i < entranceRange.max; i++) {
const edge = edges[i];
if (edge.isTunnel) {
[edge.a, edge.b] = [edge.b, edge.a];
}
const a = vertices[edge.a];
const b = vertices[edge.b];
const norm = normalize(set$4(tmpVec, -(b.y - a.y), b.x - a.x, 0));
lastFeatureIndex = this.addFeatureSection(edge.featureInfo.featureIndex, lastFeatureIndex, this.tunnelFeatureSections, builder);
builder.addQuad(
set$4(v1, b.x, b.y, 0),
set$4(v2, a.x, a.y, 0),
set$4(v3, a.x, a.y, heights[edge.a] + tunnelEntranceHeight),
set$4(v4, b.x, b.y, heights[edge.b] + tunnelEntranceHeight),
norm
);
builder.addQuad(
set$4(v1, a.x, a.y, 0),
set$4(v2, b.x, b.y, 0),
set$4(v3, b.x, b.y, heights[edge.b] + tunnelEntranceHeight),
set$4(v4, a.x, a.y, heights[edge.a] + tunnelEntranceHeight),
norm
);
}
this.tunnelFeatureSections.push({ featureIndex: Number.POSITIVE_INFINITY, vertexStart: builder.getVertexCount() });
assert$1(this.tunnelFeatureSections.every((sec, i) => {
return i === 0 || this.tunnelFeatureSections[i - 1].vertexStart <= sec.vertexStart;
}));
}
setElevatedPoint(out, x, y, h) {
assert$1(out);
out.coord.x = x;
out.coord.y = y;
out.height = h;
}
prepareEdgePoints(out, vertices, heights, edge, comp) {
assert$1(out.length === 2);
let vax = vertices[edge.a].x;
let vay = vertices[edge.a].y;
let vbx = vertices[edge.b].x;
let vby = vertices[edge.b].y;
let ha = heights[edge.a];
let hb = heights[edge.b];
const aPass = comp(ha, 0);
const bPass = comp(hb, 0);
if (aPass && bPass) {
this.setElevatedPoint(out[0], vax, vay, ha);
this.setElevatedPoint(out[1], vbx, vby, hb);
return true;
} else if (!aPass && !bPass) {
return false;
}
if (!aPass) {
const t = ha / (ha - hb);
vax = number(vax, vbx, t);
vay = number(vay, vby, t);
ha = number(ha, hb, t);
} else if (!bPass) {
const t = hb / (hb - ha);
vbx = number(vbx, vax, t);
vby = number(vby, vay, t);
hb = number(hb, ha, t);
}
this.setElevatedPoint(out[0], vax, vay, ha);
this.setElevatedPoint(out[1], vbx, vby, hb);
return true;
}
prepareEdges(portals, edges) {
if (edges.length === 0) return;
edges.sort((a, b) => a.hash === b.hash ? b.polygonIdx - a.polygonIdx : b.hash > a.hash ? 1 : -1);
let begin = 0;
let end = 0;
let out = 0;
let polygonIdx = edges[begin].polygonIdx;
do {
end++;
if (end === edges.length || edges[begin].hash !== edges[end].hash) {
const occurrences = end - begin;
const differentOwner = edges[end - 1].polygonIdx !== polygonIdx;
if (occurrences === 1 || differentOwner) {
if (out < begin) {
edges[out] = edges[begin];
edges[begin] = null;
}
edges[out].type = "none";
out++;
}
begin = end;
if (begin !== edges.length) {
polygonIdx = edges[begin].polygonIdx;
}
}
} while (begin !== edges.length);
edges.splice(out);
assert$1(edges.every((e) => e != null));
if (edges.length !== 0 && portals.length !== 0) {
assert$1(portals.every((portal, index) => {
return index === 0 || portals[index - 1].hash >= portal.hash;
}));
edges.sort((a, b) => a.portalHash < b.portalHash ? 1 : -1);
let eIndex = 0;
let pIndex = 0;
while (eIndex !== edges.length && pIndex !== portals.length) {
const edge = edges[eIndex];
const portal = portals[pIndex];
if (edge.portalHash > portal.hash) {
eIndex++;
} else if (portal.hash > edge.portalHash) {
pIndex++;
} else {
edge.type = portal.type;
eIndex++;
}
}
}
}
isOnBorder(a, b) {
return a <= 0 && b <= 0 || a >= EXTENT && b >= EXTENT;
}
addFeatureSection(featureIndex, lastFeatureIndex, sections, builder) {
if (featureIndex !== lastFeatureIndex) {
lastFeatureIndex = featureIndex;
sections.push({ featureIndex, vertexStart: builder.getVertexCount() });
builder.clearVertexLookup();
}
return lastFeatureIndex;
}
sortSubarray(array, start, end, fn) {
const sub = array.slice(start, end);
sub.sort(fn);
array.splice(start, sub.length, ...sub);
}
static computeEdgeHash(pa, pb) {
if (pa.y === pb.y && pa.x > pb.x || pa.y > pb.y) {
[pa, pb] = [pb, pa];
}
const aHash = BigInt(ElevatedStructures.computePosHash(pa));
const bHash = BigInt(ElevatedStructures.computePosHash(pb));
return aHash << 32n | bHash;
}
static computePosHash(p) {
const x = p.x & 65535;
const y = p.y & 65535;
return (x << 16 | y) >>> 0;
}
}
var martinez_umd$2 = {exports: {}};
/**
* martinez v0.7.4
* Martinez polygon clipping algorithm, does boolean operation on polygons (multipolygons, polygons with holes etc): intersection, union, difference, xor
*
* @author Alex Milevski
* @license MIT
* @preserve
*/
var martinez_umd$1 = martinez_umd$2.exports;
var hasRequiredMartinez_umd;
function requireMartinez_umd () {
if (hasRequiredMartinez_umd) return martinez_umd$2.exports;
hasRequiredMartinez_umd = 1;
(function (module, exports$1) {
(function (global, factory) {
'object' === 'object' && 'object' !== 'undefined' ? factory(exports$1) :
typeof undefined === 'function' && undefined.amd ? undefined(['exports'], factory) :
(global = global || self, factory(global.martinez = {}));
}(this, (function (exports$1) { 'use strict';
function DEFAULT_COMPARE (a, b) { return a > b ? 1 : a < b ? -1 : 0; }
var SplayTree = function SplayTree(compare, noDuplicates) {
if ( compare === void 0 ) compare = DEFAULT_COMPARE;
if ( noDuplicates === void 0 ) noDuplicates = false;
this._compare = compare;
this._root = null;
this._size = 0;
this._noDuplicates = !!noDuplicates;
};
var prototypeAccessors = { size: { configurable: true } };
SplayTree.prototype.rotateLeft = function rotateLeft (x) {
var y = x.right;
if (y) {
x.right = y.left;
if (y.left) { y.left.parent = x; }
y.parent = x.parent;
}
if (!x.parent) { this._root = y; }
else if (x === x.parent.left) { x.parent.left = y; }
else { x.parent.right = y; }
if (y) { y.left = x; }
x.parent = y;
};
SplayTree.prototype.rotateRight = function rotateRight (x) {
var y = x.left;
if (y) {
x.left = y.right;
if (y.right) { y.right.parent = x; }
y.parent = x.parent;
}
if (!x.parent) { this._root = y; }
else if(x === x.parent.left) { x.parent.left = y; }
else { x.parent.right = y; }
if (y) { y.right = x; }
x.parent = y;
};
SplayTree.prototype._splay = function _splay (x) {
while (x.parent) {
var p = x.parent;
if (!p.parent) {
if (p.left === x) { this.rotateRight(p); }
else { this.rotateLeft(p); }
} else if (p.left === x && p.parent.left === p) {
this.rotateRight(p.parent);
this.rotateRight(p);
} else if (p.right === x && p.parent.right === p) {
this.rotateLeft(p.parent);
this.rotateLeft(p);
} else if (p.left === x && p.parent.right === p) {
this.rotateRight(p);
this.rotateLeft(p);
} else {
this.rotateLeft(p);
this.rotateRight(p);
}
}
};
SplayTree.prototype.splay = function splay (x) {
var p, gp, ggp, l, r;
while (x.parent) {
p = x.parent;
gp = p.parent;
if (gp && gp.parent) {
ggp = gp.parent;
if (ggp.left === gp) { ggp.left= x; }
else { ggp.right = x; }
x.parent = ggp;
} else {
x.parent = null;
this._root = x;
}
l = x.left; r = x.right;
if (x === p.left) { // left
if (gp) {
if (gp.left === p) {
/* zig-zig */
if (p.right) {
gp.left = p.right;
gp.left.parent = gp;
} else { gp.left = null; }
p.right = gp;
gp.parent = p;
} else {
/* zig-zag */
if (l) {
gp.right = l;
l.parent = gp;
} else { gp.right = null; }
x.left = gp;
gp.parent = x;
}
}
if (r) {
p.left = r;
r.parent = p;
} else { p.left = null; }
x.right= p;
p.parent = x;
} else { // right
if (gp) {
if (gp.right === p) {
/* zig-zig */
if (p.left) {
gp.right = p.left;
gp.right.parent = gp;
} else { gp.right = null; }
p.left = gp;
gp.parent = p;
} else {
/* zig-zag */
if (r) {
gp.left = r;
r.parent = gp;
} else { gp.left = null; }
x.right = gp;
gp.parent = x;
}
}
if (l) {
p.right = l;
l.parent = p;
} else { p.right = null; }
x.left = p;
p.parent = x;
}
}
};
SplayTree.prototype.replace = function replace (u, v) {
if (!u.parent) { this._root = v; }
else if (u === u.parent.left) { u.parent.left = v; }
else { u.parent.right = v; }
if (v) { v.parent = u.parent; }
};
SplayTree.prototype.minNode = function minNode (u) {
if ( u === void 0 ) u = this._root;
if (u) { while (u.left) { u = u.left; } }
return u;
};
SplayTree.prototype.maxNode = function maxNode (u) {
if ( u === void 0 ) u = this._root;
if (u) { while (u.right) { u = u.right; } }
return u;
};
SplayTree.prototype.insert = function insert (key, data) {
var z = this._root;
var p = null;
var comp = this._compare;
var cmp;
if (this._noDuplicates) {
while (z) {
p = z;
cmp = comp(z.key, key);
if (cmp === 0) { return; }
else if (comp(z.key, key) < 0) { z = z.right; }
else { z = z.left; }
}
} else {
while (z) {
p = z;
if (comp(z.key, key) < 0) { z = z.right; }
else { z = z.left; }
}
}
z = { key: key, data: data, left: null, right: null, parent: p };
if (!p) { this._root = z; }
else if (comp(p.key, z.key) < 0) { p.right = z; }
else { p.left= z; }
this.splay(z);
this._size++;
return z;
};
SplayTree.prototype.find = function find (key) {
var z = this._root;
var comp = this._compare;
while (z) {
var cmp = comp(z.key, key);
if (cmp < 0) { z = z.right; }
else if (cmp > 0) { z = z.left; }
else { return z; }
}
return null;
};
/**
* Whether the tree contains a node with the given key
* @param{Key} key
* @return {boolean} true/false
*/
SplayTree.prototype.contains = function contains (key) {
var node = this._root;
var comparator = this._compare;
while (node){
var cmp = comparator(key, node.key);
if (cmp === 0) { return true; }
else if (cmp < 0) { node = node.left; }
else { node = node.right; }
}
return false;
};
SplayTree.prototype.remove = function remove (key) {
var z = this.find(key);
if (!z) { return false; }
this.splay(z);
if (!z.left) { this.replace(z, z.right); }
else if (!z.right) { this.replace(z, z.left); }
else {
var y = this.minNode(z.right);
if (y.parent !== z) {
this.replace(y, y.right);
y.right = z.right;
y.right.parent = y;
}
this.replace(z, y);
y.left = z.left;
y.left.parent = y;
}
this._size--;
return true;
};
SplayTree.prototype.removeNode = function removeNode (z) {
if (!z) { return false; }
this.splay(z);
if (!z.left) { this.replace(z, z.right); }
else if (!z.right) { this.replace(z, z.left); }
else {
var y = this.minNode(z.right);
if (y.parent !== z) {
this.replace(y, y.right);
y.right = z.right;
y.right.parent = y;
}
this.replace(z, y);
y.left = z.left;
y.left.parent = y;
}
this._size--;
return true;
};
SplayTree.prototype.erase = function erase (key) {
var z = this.find(key);
if (!z) { return; }
this.splay(z);
var s = z.left;
var t = z.right;
var sMax = null;
if (s) {
s.parent = null;
sMax = this.maxNode(s);
this.splay(sMax);
this._root = sMax;
}
if (t) {
if (s) { sMax.right = t; }
else { this._root = t; }
t.parent = sMax;
}
this._size--;
};
/**
* Removes and returns the node with smallest key
* @return {?Node}
*/
SplayTree.prototype.pop = function pop () {
var node = this._root, returnValue = null;
if (node) {
while (node.left) { node = node.left; }
returnValue = { key: node.key, data: node.data };
this.remove(node.key);
}
return returnValue;
};
/* eslint-disable class-methods-use-this */
/**
* Successor node
* @param{Node} node
* @return {?Node}
*/
SplayTree.prototype.next = function next (node) {
var successor = node;
if (successor) {
if (successor.right) {
successor = successor.right;
while (successor && successor.left) { successor = successor.left; }
} else {
successor = node.parent;
while (successor && successor.right === node) {
node = successor; successor = successor.parent;
}
}
}
return successor;
};
/**
* Predecessor node
* @param{Node} node
* @return {?Node}
*/
SplayTree.prototype.prev = function prev (node) {
var predecessor = node;
if (predecessor) {
if (predecessor.left) {
predecessor = predecessor.left;
while (predecessor && predecessor.right) { predecessor = predecessor.right; }
} else {
predecessor = node.parent;
while (predecessor && predecessor.left === node) {
node = predecessor;
predecessor = predecessor.parent;
}
}
}
return predecessor;
};
/* eslint-enable class-methods-use-this */
/**
* @param{forEachCallback} callback
* @return {SplayTree}
*/
SplayTree.prototype.forEach = function forEach (callback) {
var current = this._root;
var s = [], done = false, i = 0;
while (!done) {
// Reach the left most Node of the current Node
if (current) {
// Place pointer to a tree node on the stack
// before traversing the node's left subtree
s.push(current);
current = current.left;
} else {
// BackTrack from the empty subtree and visit the Node
// at the top of the stack; however, if the stack is
// empty you are done
if (s.length > 0) {
current = s.pop();
callback(current, i++);
// We have visited the node and its left
// subtree. Now, it's right subtree's turn
current = current.right;
} else { done = true; }
}
}
return this;
};
/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
* @param{Key} low
* @param{Key} high
* @param{Function} fn
* @param{*?} ctx
* @return {SplayTree}
*/
SplayTree.prototype.range = function range (low, high, fn, ctx) {
var Q = [];
var compare = this._compare;
var node = this._root, cmp;
while (Q.length !== 0 || node) {
if (node) {
Q.push(node);
node = node.left;
} else {
node = Q.pop();
cmp = compare(node.key, high);
if (cmp > 0) {
break;
} else if (compare(node.key, low) >= 0) {
if (fn.call(ctx, node)) { return this; } // stop if smth is returned
}
node = node.right;
}
}
return this;
};
/**
* Returns all keys in order
* @return {Array}
*/
SplayTree.prototype.keys = function keys () {
var current = this._root;
var s = [], r = [], done = false;
while (!done) {
if (current) {
s.push(current);
current = current.left;
} else {
if (s.length > 0) {
current = s.pop();
r.push(current.key);
current = current.right;
} else { done = true; }
}
}
return r;
};
/**
* Returns `data` fields of all nodes in order.
* @return {Array}
*/
SplayTree.prototype.values = function values () {
var current = this._root;
var s = [], r = [], done = false;
while (!done) {
if (current) {
s.push(current);
current = current.left;
} else {
if (s.length > 0) {
current = s.pop();
r.push(current.data);
current = current.right;
} else { done = true; }
}
}
return r;
};
/**
* Returns node at given index
* @param{number} index
* @return {?Node}
*/
SplayTree.prototype.at = function at (index) {
// removed after a consideration, more misleading than useful
// index = index % this.size;
// if (index < 0) index = this.size - index;
var current = this._root;
var s = [], done = false, i = 0;
while (!done) {
if (current) {
s.push(current);
current = current.left;
} else {
if (s.length > 0) {
current = s.pop();
if (i === index) { return current; }
i++;
current = current.right;
} else { done = true; }
}
}
return null;
};
/**
* Bulk-load items. Both array have to be same size
* @param{Array} keys
* @param{Array}[values]
* @param{Boolean} [presort=false] Pre-sort keys and values, using
* tree's comparator. Sorting is done
* in-place
* @return {AVLTree}
*/
SplayTree.prototype.load = function load (keys, values, presort) {
if ( keys === void 0 ) keys = [];
if ( values === void 0 ) values = [];
if ( presort === void 0 ) presort = false;
if (this._size !== 0) { throw new Error('bulk-load: tree is not empty'); }
var size = keys.length;
if (presort) { sort(keys, values, 0, size - 1, this._compare); }
this._root = loadRecursive(null, keys, values, 0, size);
this._size = size;
return this;
};
SplayTree.prototype.min = function min () {
var node = this.minNode(this._root);
if (node) { return node.key; }
else { return null; }
};
SplayTree.prototype.max = function max () {
var node = this.maxNode(this._root);
if (node) { return node.key; }
else { return null; }
};
SplayTree.prototype.isEmpty = function isEmpty () { return this._root === null; };
prototypeAccessors.size.get = function () { return this._size; };
/**
* Create a tree and load it with items
* @param{Array} keys
* @param{Array?} [values]
* @param{Function?} [comparator]
* @param{Boolean?} [presort=false] Pre-sort keys and values, using
* tree's comparator. Sorting is done
* in-place
* @param{Boolean?} [noDuplicates=false] Allow duplicates
* @return {SplayTree}
*/
SplayTree.createTree = function createTree (keys, values, comparator, presort, noDuplicates) {
return new SplayTree(comparator, noDuplicates).load(keys, values, presort);
};
Object.defineProperties( SplayTree.prototype, prototypeAccessors );
function loadRecursive (parent, keys, values, start, end) {
var size = end - start;
if (size > 0) {
var middle = start + Math.floor(size / 2);
var key = keys[middle];
var data = values[middle];
var node = { key: key, data: data, parent: parent };
node.left = loadRecursive(node, keys, values, start, middle);
node.right = loadRecursive(node, keys, values, middle + 1, end);
return node;
}
return null;
}
function sort(keys, values, left, right, compare) {
if (left >= right) { return; }
var pivot = keys[(left + right) >> 1];
var i = left - 1;
var j = right + 1;
while (true) {
do { i++; } while (compare(keys[i], pivot) < 0);
do { j--; } while (compare(keys[j], pivot) > 0);
if (i >= j) { break; }
var tmp = keys[i];
keys[i] = keys[j];
keys[j] = tmp;
tmp = values[i];
values[i] = values[j];
values[j] = tmp;
}
sort(keys, values, left, j, compare);
sort(keys, values, j + 1, right, compare);
}
var NORMAL = 0;
var NON_CONTRIBUTING = 1;
var SAME_TRANSITION = 2;
var DIFFERENT_TRANSITION = 3;
var INTERSECTION = 0;
var UNION = 1;
var DIFFERENCE = 2;
var XOR = 3;
/**
* @param {SweepEvent} event
* @param {SweepEvent} prev
* @param {Operation} operation
*/
function computeFields (event, prev, operation) {
// compute inOut and otherInOut fields
if (prev === null) {
event.inOut = false;
event.otherInOut = true;
// previous line segment in sweepline belongs to the same polygon
} else {
if (event.isSubject === prev.isSubject) {
event.inOut = !prev.inOut;
event.otherInOut = prev.otherInOut;
// previous line segment in sweepline belongs to the clipping polygon
} else {
event.inOut = !prev.otherInOut;
event.otherInOut = prev.isVertical() ? !prev.inOut : prev.inOut;
}
// compute prevInResult field
if (prev) {
event.prevInResult = (!inResult(prev, operation) || prev.isVertical())
? prev.prevInResult : prev;
}
}
// check if the line segment belongs to the Boolean operation
var isInResult = inResult(event, operation);
if (isInResult) {
event.resultTransition = determineResultTransition(event, operation);
} else {
event.resultTransition = 0;
}
}
/* eslint-disable indent */
function inResult(event, operation) {
switch (event.type) {
case NORMAL:
switch (operation) {
case INTERSECTION:
return !event.otherInOut;
case UNION:
return event.otherInOut;
case DIFFERENCE:
// return (event.isSubject && !event.otherInOut) ||
// (!event.isSubject && event.otherInOut);
return (event.isSubject && event.otherInOut) ||
(!event.isSubject && !event.otherInOut);
case XOR:
return true;
}
break;
case SAME_TRANSITION:
return operation === INTERSECTION || operation === UNION;
case DIFFERENT_TRANSITION:
return operation === DIFFERENCE;
case NON_CONTRIBUTING:
return false;
}
return false;
}
/* eslint-enable indent */
function determineResultTransition(event, operation) {
var thisIn = !event.inOut;
var thatIn = !event.otherInOut;
var isIn;
switch (operation) {
case INTERSECTION:
isIn = thisIn && thatIn; break;
case UNION:
isIn = thisIn || thatIn; break;
case XOR:
isIn = thisIn ^ thatIn; break;
case DIFFERENCE:
if (event.isSubject) {
isIn = thisIn && !thatIn;
} else {
isIn = thatIn && !thisIn;
}
break;
}
return isIn ? +1 : -1;
}
var SweepEvent = function SweepEvent (point, left, otherEvent, isSubject, edgeType) {
/**
* Is left endpoint?
* @type {Boolean}
*/
this.left = left;
/**
* @type {Array.}
*/
this.point = point;
/**
* Other edge reference
* @type {SweepEvent}
*/
this.otherEvent = otherEvent;
/**
* Belongs to source or clipping polygon
* @type {Boolean}
*/
this.isSubject = isSubject;
/**
* Edge contribution type
* @type {Number}
*/
this.type = edgeType || NORMAL;
/**
* In-out transition for the sweepline crossing polygon
* @type {Boolean}
*/
this.inOut = false;
/**
* @type {Boolean}
*/
this.otherInOut = false;
/**
* Previous event in result?
* @type {SweepEvent}
*/
this.prevInResult = null;
/**
* Type of result transition (0 = not in result, +1 = out-in, -1, in-out)
* @type {Number}
*/
this.resultTransition = 0;
// connection step
/**
* @type {Number}
*/
this.otherPos = -1;
/**
* @type {Number}
*/
this.outputContourId = -1;
this.isExteriorRing = true; // TODO: Looks unused, remove?
};
var prototypeAccessors$1 = { inResult: { configurable: true } };
/**
* @param{Array.}p
* @return {Boolean}
*/
SweepEvent.prototype.isBelow = function isBelow (p) {
var p0 = this.point, p1 = this.otherEvent.point;
return this.left
? (p0[0] - p[0]) * (p1[1] - p[1]) - (p1[0] - p[0]) * (p0[1] - p[1]) > 0
// signedArea(this.point, this.otherEvent.point, p) > 0 :
: (p1[0] - p[0]) * (p0[1] - p[1]) - (p0[0] - p[0]) * (p1[1] - p[1]) > 0;
//signedArea(this.otherEvent.point, this.point, p) > 0;
};
/**
* @param{Array.}p
* @return {Boolean}
*/
SweepEvent.prototype.isAbove = function isAbove (p) {
return !this.isBelow(p);
};
/**
* @return {Boolean}
*/
SweepEvent.prototype.isVertical = function isVertical () {
return this.point[0] === this.otherEvent.point[0];
};
/**
* Does event belong to result?
* @return {Boolean}
*/
prototypeAccessors$1.inResult.get = function () {
return this.resultTransition !== 0;
};
SweepEvent.prototype.clone = function clone () {
var copy = new SweepEvent(
this.point, this.left, this.otherEvent, this.isSubject, this.type);
copy.contourId = this.contourId;
copy.resultTransition = this.resultTransition;
copy.prevInResult = this.prevInResult;
copy.isExteriorRing = this.isExteriorRing;
copy.inOut = this.inOut;
copy.otherInOut = this.otherInOut;
return copy;
};
Object.defineProperties( SweepEvent.prototype, prototypeAccessors$1 );
function equals(p1, p2) {
if (p1[0] === p2[0]) {
if (p1[1] === p2[1]) {
return true;
} else {
return false;
}
}
return false;
}
// const EPSILON = 1e-9;
// const abs = Math.abs;
// TODO https://github.com/w8r/martinez/issues/6#issuecomment-262847164
// Precision problem.
//
// module.exports = function equals(p1, p2) {
// return abs(p1[0] - p2[0]) <= EPSILON && abs(p1[1] - p2[1]) <= EPSILON;
// };
var epsilon = 1.1102230246251565e-16;
var splitter = 134217729;
var resulterrbound = (3 + 8 * epsilon) * epsilon;
// fast_expansion_sum_zeroelim routine from oritinal code
function sum(elen, e, flen, f, h) {
var Q, Qnew, hh, bvirt;
var enow = e[0];
var fnow = f[0];
var eindex = 0;
var findex = 0;
if ((fnow > enow) === (fnow > -enow)) {
Q = enow;
enow = e[++eindex];
} else {
Q = fnow;
fnow = f[++findex];
}
var hindex = 0;
if (eindex < elen && findex < flen) {
if ((fnow > enow) === (fnow > -enow)) {
Qnew = enow + Q;
hh = Q - (Qnew - enow);
enow = e[++eindex];
} else {
Qnew = fnow + Q;
hh = Q - (Qnew - fnow);
fnow = f[++findex];
}
Q = Qnew;
if (hh !== 0) {
h[hindex++] = hh;
}
while (eindex < elen && findex < flen) {
if ((fnow > enow) === (fnow > -enow)) {
Qnew = Q + enow;
bvirt = Qnew - Q;
hh = Q - (Qnew - bvirt) + (enow - bvirt);
enow = e[++eindex];
} else {
Qnew = Q + fnow;
bvirt = Qnew - Q;
hh = Q - (Qnew - bvirt) + (fnow - bvirt);
fnow = f[++findex];
}
Q = Qnew;
if (hh !== 0) {
h[hindex++] = hh;
}
}
}
while (eindex < elen) {
Qnew = Q + enow;
bvirt = Qnew - Q;
hh = Q - (Qnew - bvirt) + (enow - bvirt);
enow = e[++eindex];
Q = Qnew;
if (hh !== 0) {
h[hindex++] = hh;
}
}
while (findex < flen) {
Qnew = Q + fnow;
bvirt = Qnew - Q;
hh = Q - (Qnew - bvirt) + (fnow - bvirt);
fnow = f[++findex];
Q = Qnew;
if (hh !== 0) {
h[hindex++] = hh;
}
}
if (Q !== 0 || hindex === 0) {
h[hindex++] = Q;
}
return hindex;
}
function estimate(elen, e) {
var Q = e[0];
for (var i = 1; i < elen; i++) { Q += e[i]; }
return Q;
}
function vec(n) {
return new Float64Array(n);
}
var ccwerrboundA = (3 + 16 * epsilon) * epsilon;
var ccwerrboundB = (2 + 12 * epsilon) * epsilon;
var ccwerrboundC = (9 + 64 * epsilon) * epsilon * epsilon;
var B = vec(4);
var C1 = vec(8);
var C2 = vec(12);
var D = vec(16);
var u = vec(4);
function orient2dadapt(ax, ay, bx, by, cx, cy, detsum) {
var acxtail, acytail, bcxtail, bcytail;
var bvirt, c, ahi, alo, bhi, blo, _i, _j, _0, s1, s0, t1, t0, u3;
var acx = ax - cx;
var bcx = bx - cx;
var acy = ay - cy;
var bcy = by - cy;
s1 = acx * bcy;
c = splitter * acx;
ahi = c - (c - acx);
alo = acx - ahi;
c = splitter * bcy;
bhi = c - (c - bcy);
blo = bcy - bhi;
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
t1 = acy * bcx;
c = splitter * acy;
ahi = c - (c - acy);
alo = acy - ahi;
c = splitter * bcx;
bhi = c - (c - bcx);
blo = bcx - bhi;
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
_i = s0 - t0;
bvirt = s0 - _i;
B[0] = s0 - (_i + bvirt) + (bvirt - t0);
_j = s1 + _i;
bvirt = _j - s1;
_0 = s1 - (_j - bvirt) + (_i - bvirt);
_i = _0 - t1;
bvirt = _0 - _i;
B[1] = _0 - (_i + bvirt) + (bvirt - t1);
u3 = _j + _i;
bvirt = u3 - _j;
B[2] = _j - (u3 - bvirt) + (_i - bvirt);
B[3] = u3;
var det = estimate(4, B);
var errbound = ccwerrboundB * detsum;
if (det >= errbound || -det >= errbound) {
return det;
}
bvirt = ax - acx;
acxtail = ax - (acx + bvirt) + (bvirt - cx);
bvirt = bx - bcx;
bcxtail = bx - (bcx + bvirt) + (bvirt - cx);
bvirt = ay - acy;
acytail = ay - (acy + bvirt) + (bvirt - cy);
bvirt = by - bcy;
bcytail = by - (bcy + bvirt) + (bvirt - cy);
if (acxtail === 0 && acytail === 0 && bcxtail === 0 && bcytail === 0) {
return det;
}
errbound = ccwerrboundC * detsum + resulterrbound * Math.abs(det);
det += (acx * bcytail + bcy * acxtail) - (acy * bcxtail + bcx * acytail);
if (det >= errbound || -det >= errbound) { return det; }
s1 = acxtail * bcy;
c = splitter * acxtail;
ahi = c - (c - acxtail);
alo = acxtail - ahi;
c = splitter * bcy;
bhi = c - (c - bcy);
blo = bcy - bhi;
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
t1 = acytail * bcx;
c = splitter * acytail;
ahi = c - (c - acytail);
alo = acytail - ahi;
c = splitter * bcx;
bhi = c - (c - bcx);
blo = bcx - bhi;
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
_i = s0 - t0;
bvirt = s0 - _i;
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
_j = s1 + _i;
bvirt = _j - s1;
_0 = s1 - (_j - bvirt) + (_i - bvirt);
_i = _0 - t1;
bvirt = _0 - _i;
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
u3 = _j + _i;
bvirt = u3 - _j;
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
u[3] = u3;
var C1len = sum(4, B, 4, u, C1);
s1 = acx * bcytail;
c = splitter * acx;
ahi = c - (c - acx);
alo = acx - ahi;
c = splitter * bcytail;
bhi = c - (c - bcytail);
blo = bcytail - bhi;
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
t1 = acy * bcxtail;
c = splitter * acy;
ahi = c - (c - acy);
alo = acy - ahi;
c = splitter * bcxtail;
bhi = c - (c - bcxtail);
blo = bcxtail - bhi;
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
_i = s0 - t0;
bvirt = s0 - _i;
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
_j = s1 + _i;
bvirt = _j - s1;
_0 = s1 - (_j - bvirt) + (_i - bvirt);
_i = _0 - t1;
bvirt = _0 - _i;
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
u3 = _j + _i;
bvirt = u3 - _j;
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
u[3] = u3;
var C2len = sum(C1len, C1, 4, u, C2);
s1 = acxtail * bcytail;
c = splitter * acxtail;
ahi = c - (c - acxtail);
alo = acxtail - ahi;
c = splitter * bcytail;
bhi = c - (c - bcytail);
blo = bcytail - bhi;
s0 = alo * blo - (s1 - ahi * bhi - alo * bhi - ahi * blo);
t1 = acytail * bcxtail;
c = splitter * acytail;
ahi = c - (c - acytail);
alo = acytail - ahi;
c = splitter * bcxtail;
bhi = c - (c - bcxtail);
blo = bcxtail - bhi;
t0 = alo * blo - (t1 - ahi * bhi - alo * bhi - ahi * blo);
_i = s0 - t0;
bvirt = s0 - _i;
u[0] = s0 - (_i + bvirt) + (bvirt - t0);
_j = s1 + _i;
bvirt = _j - s1;
_0 = s1 - (_j - bvirt) + (_i - bvirt);
_i = _0 - t1;
bvirt = _0 - _i;
u[1] = _0 - (_i + bvirt) + (bvirt - t1);
u3 = _j + _i;
bvirt = u3 - _j;
u[2] = _j - (u3 - bvirt) + (_i - bvirt);
u[3] = u3;
var Dlen = sum(C2len, C2, 4, u, D);
return D[Dlen - 1];
}
function orient2d(ax, ay, bx, by, cx, cy) {
var detleft = (ay - cy) * (bx - cx);
var detright = (ax - cx) * (by - cy);
var det = detleft - detright;
if (detleft === 0 || detright === 0 || (detleft > 0) !== (detright > 0)) { return det; }
var detsum = Math.abs(detleft + detright);
if (Math.abs(det) >= ccwerrboundA * detsum) { return det; }
return -orient2dadapt(ax, ay, bx, by, cx, cy, detsum);
}
/**
* Signed area of the triangle (p0, p1, p2)
* @param {Array.} p0
* @param {Array.} p1
* @param {Array.} p2
* @return {Number}
*/
function signedArea(p0, p1, p2) {
var res = orient2d(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]);
if (res > 0) { return -1; }
if (res < 0) { return 1; }
return 0;
}
/**
* @param {SweepEvent} e1
* @param {SweepEvent} e2
* @return {Number}
*/
function compareEvents(e1, e2) {
var p1 = e1.point;
var p2 = e2.point;
// Different x-coordinate
if (p1[0] > p2[0]) { return 1; }
if (p1[0] < p2[0]) { return -1; }
// Different points, but same x-coordinate
// Event with lower y-coordinate is processed first
if (p1[1] !== p2[1]) { return p1[1] > p2[1] ? 1 : -1; }
return specialCases(e1, e2, p1);
}
/* eslint-disable no-unused-vars */
function specialCases(e1, e2, p1, p2) {
// Same coordinates, but one is a left endpoint and the other is
// a right endpoint. The right endpoint is processed first
if (e1.left !== e2.left)
{ return e1.left ? 1 : -1; }
// const p2 = e1.otherEvent.point, p3 = e2.otherEvent.point;
// const sa = (p1[0] - p3[0]) * (p2[1] - p3[1]) - (p2[0] - p3[0]) * (p1[1] - p3[1])
// Same coordinates, both events
// are left endpoints or right endpoints.
// not collinear
if (signedArea(p1, e1.otherEvent.point, e2.otherEvent.point) !== 0) {
// the event associate to the bottom segment is processed first
return (!e1.isBelow(e2.otherEvent.point)) ? 1 : -1;
}
return (!e1.isSubject && e2.isSubject) ? 1 : -1;
}
/* eslint-enable no-unused-vars */
/**
* @param {SweepEvent} se
* @param {Array.} p
* @param {Queue} queue
* @return {Queue}
*/
function divideSegment(se, p, queue) {
var r = new SweepEvent(p, false, se, se.isSubject);
var l = new SweepEvent(p, true, se.otherEvent, se.isSubject);
/* eslint-disable no-console */
if (equals(se.point, se.otherEvent.point)) {
console.warn('what is that, a collapsed segment?', se);
}
/* eslint-enable no-console */
r.contourId = l.contourId = se.contourId;
// avoid a rounding error. The left event would be processed after the right event
if (compareEvents(l, se.otherEvent) > 0) {
se.otherEvent.left = true;
l.left = false;
}
// avoid a rounding error. The left event would be processed after the right event
// if (compareEvents(se, r) > 0) {}
se.otherEvent.otherEvent = l;
se.otherEvent = r;
queue.push(l);
queue.push(r);
return queue;
}
//const EPS = 1e-9;
/**
* Finds the magnitude of the cross product of two vectors (if we pretend
* they're in three dimensions)
*
* @param {Object} a First vector
* @param {Object} b Second vector
* @private
* @returns {Number} The magnitude of the cross product
*/
function crossProduct(a, b) {
return (a[0] * b[1]) - (a[1] * b[0]);
}
/**
* Finds the dot product of two vectors.
*
* @param {Object} a First vector
* @param {Object} b Second vector
* @private
* @returns {Number} The dot product
*/
function dotProduct(a, b) {
return (a[0] * b[0]) + (a[1] * b[1]);
}
/**
* Finds the intersection (if any) between two line segments a and b, given the
* line segments' end points a1, a2 and b1, b2.
*
* This algorithm is based on Schneider and Eberly.
* http://www.cimec.org.ar/~ncalvo/Schneider_Eberly.pdf
* Page 244.
*
* @param {Array.} a1 point of first line
* @param {Array.} a2 point of first line
* @param {Array.} b1 point of second line
* @param {Array.} b2 point of second line
* @param {Boolean=} noEndpointTouch whether to skip single touchpoints
* (meaning connected segments) as
* intersections
* @returns {Array.>|Null} If the lines intersect, the point of
* intersection. If they overlap, the two end points of the overlapping segment.
* Otherwise, null.
*/
function intersection (a1, a2, b1, b2, noEndpointTouch) {
// The algorithm expects our lines in the form P + sd, where P is a point,
// s is on the interval [0, 1], and d is a vector.
// We are passed two points. P can be the first point of each pair. The
// vector, then, could be thought of as the distance (in x and y components)
// from the first point to the second point.
// So first, let's make our vectors:
var va = [a2[0] - a1[0], a2[1] - a1[1]];
var vb = [b2[0] - b1[0], b2[1] - b1[1]];
// We also define a function to convert back to regular point form:
/* eslint-disable arrow-body-style */
function toPoint(p, s, d) {
return [
p[0] + s * d[0],
p[1] + s * d[1]
];
}
/* eslint-enable arrow-body-style */
// The rest is pretty much a straight port of the algorithm.
var e = [b1[0] - a1[0], b1[1] - a1[1]];
var kross = crossProduct(va, vb);
var sqrKross = kross * kross;
var sqrLenA = dotProduct(va, va);
//const sqrLenB = dotProduct(vb, vb);
// Check for line intersection. This works because of the properties of the
// cross product -- specifically, two vectors are parallel if and only if the
// cross product is the 0 vector. The full calculation involves relative error
// to account for possible very small line segments. See Schneider & Eberly
// for details.
if (sqrKross > 0/* EPS * sqrLenB * sqLenA */) {
// If they're not parallel, then (because these are line segments) they
// still might not actually intersect. This code checks that the
// intersection point of the lines is actually on both line segments.
var s = crossProduct(e, vb) / kross;
if (s < 0 || s > 1) {
// not on line segment a
return null;
}
var t = crossProduct(e, va) / kross;
if (t < 0 || t > 1) {
// not on line segment b
return null;
}
if (s === 0 || s === 1) {
// on an endpoint of line segment a
return noEndpointTouch ? null : [toPoint(a1, s, va)];
}
if (t === 0 || t === 1) {
// on an endpoint of line segment b
return noEndpointTouch ? null : [toPoint(b1, t, vb)];
}
return [toPoint(a1, s, va)];
}
// If we've reached this point, then the lines are either parallel or the
// same, but the segments could overlap partially or fully, or not at all.
// So we need to find the overlap, if any. To do that, we can use e, which is
// the (vector) difference between the two initial points. If this is parallel
// with the line itself, then the two lines are the same line, and there will
// be overlap.
//const sqrLenE = dotProduct(e, e);
kross = crossProduct(e, va);
sqrKross = kross * kross;
if (sqrKross > 0 /* EPS * sqLenB * sqLenE */) {
// Lines are just parallel, not the same. No overlap.
return null;
}
var sa = dotProduct(va, e) / sqrLenA;
var sb = sa + dotProduct(va, vb) / sqrLenA;
var smin = Math.min(sa, sb);
var smax = Math.max(sa, sb);
// this is, essentially, the FindIntersection acting on floats from
// Schneider & Eberly, just inlined into this function.
if (smin <= 1 && smax >= 0) {
// overlap on an end point
if (smin === 1) {
return noEndpointTouch ? null : [toPoint(a1, smin > 0 ? smin : 0, va)];
}
if (smax === 0) {
return noEndpointTouch ? null : [toPoint(a1, smax < 1 ? smax : 1, va)];
}
if (noEndpointTouch && smin === 0 && smax === 1) { return null; }
// There's overlap on a segment -- two points of intersection. Return both.
return [
toPoint(a1, smin > 0 ? smin : 0, va),
toPoint(a1, smax < 1 ? smax : 1, va)
];
}
return null;
}
/**
* @param {SweepEvent} se1
* @param {SweepEvent} se2
* @param {Queue} queue
* @return {Number}
*/
function possibleIntersection (se1, se2, queue) {
// that disallows self-intersecting polygons,
// did cost us half a day, so I'll leave it
// out of respect
// if (se1.isSubject === se2.isSubject) return;
var inter = intersection(
se1.point, se1.otherEvent.point,
se2.point, se2.otherEvent.point
);
var nintersections = inter ? inter.length : 0;
if (nintersections === 0) { return 0; } // no intersection
// the line segments intersect at an endpoint of both line segments
if ((nintersections === 1) &&
(equals(se1.point, se2.point) ||
equals(se1.otherEvent.point, se2.otherEvent.point))) {
return 0;
}
if (nintersections === 2 && se1.isSubject === se2.isSubject) {
// if(se1.contourId === se2.contourId){
// console.warn('Edges of the same polygon overlap',
// se1.point, se1.otherEvent.point, se2.point, se2.otherEvent.point);
// }
//throw new Error('Edges of the same polygon overlap');
return 0;
}
// The line segments associated to se1 and se2 intersect
if (nintersections === 1) {
// if the intersection point is not an endpoint of se1
if (!equals(se1.point, inter[0]) && !equals(se1.otherEvent.point, inter[0])) {
divideSegment(se1, inter[0], queue);
}
// if the intersection point is not an endpoint of se2
if (!equals(se2.point, inter[0]) && !equals(se2.otherEvent.point, inter[0])) {
divideSegment(se2, inter[0], queue);
}
return 1;
}
// The line segments associated to se1 and se2 overlap
var events = [];
var leftCoincide = false;
var rightCoincide = false;
if (equals(se1.point, se2.point)) {
leftCoincide = true; // linked
} else if (compareEvents(se1, se2) === 1) {
events.push(se2, se1);
} else {
events.push(se1, se2);
}
if (equals(se1.otherEvent.point, se2.otherEvent.point)) {
rightCoincide = true;
} else if (compareEvents(se1.otherEvent, se2.otherEvent) === 1) {
events.push(se2.otherEvent, se1.otherEvent);
} else {
events.push(se1.otherEvent, se2.otherEvent);
}
if ((leftCoincide && rightCoincide) || leftCoincide) {
// both line segments are equal or share the left endpoint
se2.type = NON_CONTRIBUTING;
se1.type = (se2.inOut === se1.inOut)
? SAME_TRANSITION : DIFFERENT_TRANSITION;
if (leftCoincide && !rightCoincide) {
// honestly no idea, but changing events selection from [2, 1]
// to [0, 1] fixes the overlapping self-intersecting polygons issue
divideSegment(events[1].otherEvent, events[0].point, queue);
}
return 2;
}
// the line segments share the right endpoint
if (rightCoincide) {
divideSegment(events[0], events[1].point, queue);
return 3;
}
// no line segment includes totally the other one
if (events[0] !== events[3].otherEvent) {
divideSegment(events[0], events[1].point, queue);
divideSegment(events[1], events[2].point, queue);
return 3;
}
// one line segment includes the other one
divideSegment(events[0], events[1].point, queue);
divideSegment(events[3].otherEvent, events[2].point, queue);
return 3;
}
/**
* @param {SweepEvent} le1
* @param {SweepEvent} le2
* @return {Number}
*/
function compareSegments(le1, le2) {
if (le1 === le2) { return 0; }
// Segments are not collinear
if (signedArea(le1.point, le1.otherEvent.point, le2.point) !== 0 ||
signedArea(le1.point, le1.otherEvent.point, le2.otherEvent.point) !== 0) {
// If they share their left endpoint use the right endpoint to sort
if (equals(le1.point, le2.point)) { return le1.isBelow(le2.otherEvent.point) ? -1 : 1; }
// Different left endpoint: use the left endpoint to sort
if (le1.point[0] === le2.point[0]) { return le1.point[1] < le2.point[1] ? -1 : 1; }
// has the line segment associated to e1 been inserted
// into S after the line segment associated to e2 ?
if (compareEvents(le1, le2) === 1) { return le2.isAbove(le1.point) ? -1 : 1; }
// The line segment associated to e2 has been inserted
// into S after the line segment associated to e1
return le1.isBelow(le2.point) ? -1 : 1;
}
if (le1.isSubject === le2.isSubject) { // same polygon
var p1 = le1.point, p2 = le2.point;
if (p1[0] === p2[0] && p1[1] === p2[1]/*equals(le1.point, le2.point)*/) {
p1 = le1.otherEvent.point; p2 = le2.otherEvent.point;
if (p1[0] === p2[0] && p1[1] === p2[1]) { return 0; }
else { return le1.contourId > le2.contourId ? 1 : -1; }
}
} else { // Segments are collinear, but belong to separate polygons
return le1.isSubject ? -1 : 1;
}
return compareEvents(le1, le2) === 1 ? 1 : -1;
}
function subdivide(eventQueue, subject, clipping, sbbox, cbbox, operation) {
var sweepLine = new SplayTree(compareSegments);
var sortedEvents = [];
var rightbound = Math.min(sbbox[2], cbbox[2]);
var prev, next, begin;
while (eventQueue.length !== 0) {
var event = eventQueue.pop();
sortedEvents.push(event);
// optimization by bboxes for intersection and difference goes here
if ((operation === INTERSECTION && event.point[0] > rightbound) ||
(operation === DIFFERENCE && event.point[0] > sbbox[2])) {
break;
}
if (event.left) {
next = prev = sweepLine.insert(event);
begin = sweepLine.minNode();
if (prev !== begin) { prev = sweepLine.prev(prev); }
else { prev = null; }
next = sweepLine.next(next);
var prevEvent = prev ? prev.key : null;
var prevprevEvent = (void 0);
computeFields(event, prevEvent, operation);
if (next) {
if (possibleIntersection(event, next.key, eventQueue) === 2) {
computeFields(event, prevEvent, operation);
computeFields(next.key, event, operation);
}
}
if (prev) {
if (possibleIntersection(prev.key, event, eventQueue) === 2) {
var prevprev = prev;
if (prevprev !== begin) { prevprev = sweepLine.prev(prevprev); }
else { prevprev = null; }
prevprevEvent = prevprev ? prevprev.key : null;
computeFields(prevEvent, prevprevEvent, operation);
computeFields(event, prevEvent, operation);
}
}
} else {
event = event.otherEvent;
next = prev = sweepLine.find(event);
if (prev && next) {
if (prev !== begin) { prev = sweepLine.prev(prev); }
else { prev = null; }
next = sweepLine.next(next);
sweepLine.remove(event);
if (next && prev) {
possibleIntersection(prev.key, next.key, eventQueue);
}
}
}
}
return sortedEvents;
}
var Contour = function Contour() {
this.points = [];
this.holeIds = [];
this.holeOf = null;
this.depth = null;
};
Contour.prototype.isExterior = function isExterior () {
return this.holeOf == null;
};
/**
* @param {Array.} sortedEvents
* @return {Array.}
*/
function orderEvents(sortedEvents) {
var event, i, len, tmp;
var resultEvents = [];
for (i = 0, len = sortedEvents.length; i < len; i++) {
event = sortedEvents[i];
if ((event.left && event.inResult) ||
(!event.left && event.otherEvent.inResult)) {
resultEvents.push(event);
}
}
// Due to overlapping edges the resultEvents array can be not wholly sorted
var sorted = false;
while (!sorted) {
sorted = true;
for (i = 0, len = resultEvents.length; i < len; i++) {
if ((i + 1) < len &&
compareEvents(resultEvents[i], resultEvents[i + 1]) === 1) {
tmp = resultEvents[i];
resultEvents[i] = resultEvents[i + 1];
resultEvents[i + 1] = tmp;
sorted = false;
}
}
}
for (i = 0, len = resultEvents.length; i < len; i++) {
event = resultEvents[i];
event.otherPos = i;
}
// imagine, the right event is found in the beginning of the queue,
// when his left counterpart is not marked yet
for (i = 0, len = resultEvents.length; i < len; i++) {
event = resultEvents[i];
if (!event.left) {
tmp = event.otherPos;
event.otherPos = event.otherEvent.otherPos;
event.otherEvent.otherPos = tmp;
}
}
return resultEvents;
}
/**
* @param {Number} pos
* @param {Array.} resultEvents
* @param {Object>} processed
* @return {Number}
*/
function nextPos(pos, resultEvents, processed, origPos) {
var newPos = pos + 1,
p = resultEvents[pos].point,
p1;
var length = resultEvents.length;
if (newPos < length)
{ p1 = resultEvents[newPos].point; }
while (newPos < length && p1[0] === p[0] && p1[1] === p[1]) {
if (!processed[newPos]) {
return newPos;
} else {
newPos++;
}
if (newPos < length) {
p1 = resultEvents[newPos].point;
}
}
newPos = pos - 1;
while (processed[newPos] && newPos > origPos) {
newPos--;
}
return newPos;
}
function initializeContourFromContext(event, contours, contourId) {
var contour = new Contour();
if (event.prevInResult != null) {
var prevInResult = event.prevInResult;
// Note that it is valid to query the "previous in result" for its output contour id,
// because we must have already processed it (i.e., assigned an output contour id)
// in an earlier iteration, otherwise it wouldn't be possible that it is "previous in
// result".
var lowerContourId = prevInResult.outputContourId;
var lowerResultTransition = prevInResult.resultTransition;
if (lowerResultTransition > 0) {
// We are inside. Now we have to check if the thing below us is another hole or
// an exterior contour.
var lowerContour = contours[lowerContourId];
if (lowerContour.holeOf != null) {
// The lower contour is a hole => Connect the new contour as a hole to its parent,
// and use same depth.
var parentContourId = lowerContour.holeOf;
contours[parentContourId].holeIds.push(contourId);
contour.holeOf = parentContourId;
contour.depth = contours[lowerContourId].depth;
} else {
// The lower contour is an exterior contour => Connect the new contour as a hole,
// and increment depth.
contours[lowerContourId].holeIds.push(contourId);
contour.holeOf = lowerContourId;
contour.depth = contours[lowerContourId].depth + 1;
}
} else {
// We are outside => this contour is an exterior contour of same depth.
contour.holeOf = null;
contour.depth = contours[lowerContourId].depth;
}
} else {
// There is no lower/previous contour => this contour is an exterior contour of depth 0.
contour.holeOf = null;
contour.depth = 0;
}
return contour;
}
/**
* @param {Array.} sortedEvents
* @return {Array.<*>} polygons
*/
function connectEdges(sortedEvents) {
var i, len;
var resultEvents = orderEvents(sortedEvents);
// "false"-filled array
var processed = {};
var contours = [];
var loop = function ( ) {
if (processed[i]) {
return;
}
var contourId = contours.length;
var contour = initializeContourFromContext(resultEvents[i], contours, contourId);
// Helper function that combines marking an event as processed with assigning its output contour ID
var markAsProcessed = function (pos) {
processed[pos] = true;
if (pos < resultEvents.length && resultEvents[pos]) {
resultEvents[pos].outputContourId = contourId;
}
};
var pos = i;
var origPos = i;
var initial = resultEvents[i].point;
contour.points.push(initial);
/* eslint no-constant-condition: "off" */
while (true) {
markAsProcessed(pos);
pos = resultEvents[pos].otherPos;
markAsProcessed(pos);
contour.points.push(resultEvents[pos].point);
pos = nextPos(pos, resultEvents, processed, origPos);
if (pos == origPos || pos >= resultEvents.length || !resultEvents[pos]) {
break;
}
}
contours.push(contour);
};
for (i = 0, len = resultEvents.length; i < len; i++) loop( );
return contours;
}
var tinyqueue = TinyQueue;
var default_1 = TinyQueue;
function TinyQueue(data, compare) {
if (!(this instanceof TinyQueue)) { return new TinyQueue(data, compare); }
this.data = data || [];
this.length = this.data.length;
this.compare = compare || defaultCompare;
if (this.length > 0) {
for (var i = (this.length >> 1) - 1; i >= 0; i--) { this._down(i); }
}
}
function defaultCompare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
TinyQueue.prototype = {
push: function (item) {
this.data.push(item);
this.length++;
this._up(this.length - 1);
},
pop: function () {
if (this.length === 0) { return undefined; }
var top = this.data[0];
this.length--;
if (this.length > 0) {
this.data[0] = this.data[this.length];
this._down(0);
}
this.data.pop();
return top;
},
peek: function () {
return this.data[0];
},
_up: function (pos) {
var data = this.data;
var compare = this.compare;
var item = data[pos];
while (pos > 0) {
var parent = (pos - 1) >> 1;
var current = data[parent];
if (compare(item, current) >= 0) { break; }
data[pos] = current;
pos = parent;
}
data[pos] = item;
},
_down: function (pos) {
var data = this.data;
var compare = this.compare;
var halfLength = this.length >> 1;
var item = data[pos];
while (pos < halfLength) {
var left = (pos << 1) + 1;
var right = left + 1;
var best = data[left];
if (right < this.length && compare(data[right], best) < 0) {
left = right;
best = data[right];
}
if (compare(best, item) >= 0) { break; }
data[pos] = best;
pos = left;
}
data[pos] = item;
}
};
tinyqueue.default = default_1;
var max = Math.max;
var min = Math.min;
var contourId = 0;
function processPolygon(contourOrHole, isSubject, depth, Q, bbox, isExteriorRing) {
var i, len, s1, s2, e1, e2;
for (i = 0, len = contourOrHole.length - 1; i < len; i++) {
s1 = contourOrHole[i];
s2 = contourOrHole[i + 1];
e1 = new SweepEvent(s1, false, undefined, isSubject);
e2 = new SweepEvent(s2, false, e1, isSubject);
e1.otherEvent = e2;
if (s1[0] === s2[0] && s1[1] === s2[1]) {
continue; // skip collapsed edges, or it breaks
}
e1.contourId = e2.contourId = depth;
if (!isExteriorRing) {
e1.isExteriorRing = false;
e2.isExteriorRing = false;
}
if (compareEvents(e1, e2) > 0) {
e2.left = true;
} else {
e1.left = true;
}
var x = s1[0], y = s1[1];
bbox[0] = min(bbox[0], x);
bbox[1] = min(bbox[1], y);
bbox[2] = max(bbox[2], x);
bbox[3] = max(bbox[3], y);
// Pushing it so the queue is sorted from left to right,
// with object on the left having the highest priority.
Q.push(e1);
Q.push(e2);
}
}
function fillQueue(subject, clipping, sbbox, cbbox, operation) {
var eventQueue = new tinyqueue(null, compareEvents);
var polygonSet, isExteriorRing, i, ii, j, jj; //, k, kk;
for (i = 0, ii = subject.length; i < ii; i++) {
polygonSet = subject[i];
for (j = 0, jj = polygonSet.length; j < jj; j++) {
isExteriorRing = j === 0;
if (isExteriorRing) { contourId++; }
processPolygon(polygonSet[j], true, contourId, eventQueue, sbbox, isExteriorRing);
}
}
for (i = 0, ii = clipping.length; i < ii; i++) {
polygonSet = clipping[i];
for (j = 0, jj = polygonSet.length; j < jj; j++) {
isExteriorRing = j === 0;
if (operation === DIFFERENCE) { isExteriorRing = false; }
if (isExteriorRing) { contourId++; }
processPolygon(polygonSet[j], false, contourId, eventQueue, cbbox, isExteriorRing);
}
}
return eventQueue;
}
var EMPTY = [];
function trivialOperation(subject, clipping, operation) {
var result = null;
if (subject.length * clipping.length === 0) {
if (operation === INTERSECTION) {
result = EMPTY;
} else if (operation === DIFFERENCE) {
result = subject;
} else if (operation === UNION ||
operation === XOR) {
result = (subject.length === 0) ? clipping : subject;
}
}
return result;
}
function compareBBoxes(subject, clipping, sbbox, cbbox, operation) {
var result = null;
if (sbbox[0] > cbbox[2] ||
cbbox[0] > sbbox[2] ||
sbbox[1] > cbbox[3] ||
cbbox[1] > sbbox[3]) {
if (operation === INTERSECTION) {
result = EMPTY;
} else if (operation === DIFFERENCE) {
result = subject;
} else if (operation === UNION ||
operation === XOR) {
result = subject.concat(clipping);
}
}
return result;
}
function boolean(subject, clipping, operation) {
if (typeof subject[0][0][0] === 'number') {
subject = [subject];
}
if (typeof clipping[0][0][0] === 'number') {
clipping = [clipping];
}
var trivial = trivialOperation(subject, clipping, operation);
if (trivial) {
return trivial === EMPTY ? null : trivial;
}
var sbbox = [Infinity, Infinity, -Infinity, -Infinity];
var cbbox = [Infinity, Infinity, -Infinity, -Infinity];
// console.time('fill queue');
var eventQueue = fillQueue(subject, clipping, sbbox, cbbox, operation);
//console.timeEnd('fill queue');
trivial = compareBBoxes(subject, clipping, sbbox, cbbox, operation);
if (trivial) {
return trivial === EMPTY ? null : trivial;
}
// console.time('subdivide edges');
var sortedEvents = subdivide(eventQueue, subject, clipping, sbbox, cbbox, operation);
//console.timeEnd('subdivide edges');
// console.time('connect vertices');
var contours = connectEdges(sortedEvents);
//console.timeEnd('connect vertices');
// Convert contours to polygons
var polygons = [];
for (var i = 0; i < contours.length; i++) {
var contour = contours[i];
if (contour.isExterior()) {
// The exterior ring goes first
var rings = [contour.points];
// Followed by holes if any
for (var j = 0; j < contour.holeIds.length; j++) {
var holeId = contour.holeIds[j];
rings.push(contours[holeId].points);
}
polygons.push(rings);
}
}
return polygons;
}
function union (subject, clipping) {
return boolean(subject, clipping, UNION);
}
function diff (subject, clipping) {
return boolean(subject, clipping, DIFFERENCE);
}
function xor (subject, clipping) {
return boolean(subject, clipping, XOR);
}
function intersection$1 (subject, clipping) {
return boolean(subject, clipping, INTERSECTION);
}
/**
* @enum {Number}
*/
var operations = { UNION: UNION, DIFFERENCE: DIFFERENCE, INTERSECTION: INTERSECTION, XOR: XOR };
exports$1.diff = diff;
exports$1.intersection = intersection$1;
exports$1.operations = operations;
exports$1.union = union;
exports$1.xor = xor;
Object.defineProperty(exports$1, '__esModule', { value: true });
})));
} (martinez_umd$2, martinez_umd$2.exports));
return martinez_umd$2.exports;
}
var martinez_umdExports = requireMartinez_umd();
var martinez_umd = /*@__PURE__*/getDefaultExportFromCjs(martinez_umdExports);
function clipPolygon(polygons, clipAxis1, clipAxis2, axis) {
const intersectX = (ring, ax, ay, bx, by, x) => {
ring.push(new Point(x, ay + (by - ay) * ((x - ax) / (bx - ax))));
};
const intersectY = (ring, ax, ay, bx, by, y) => {
ring.push(new Point(ax + (bx - ax) * ((y - ay) / (by - ay)), y));
};
const polygonsClipped = [];
const intersect = axis === 0 ? intersectX : intersectY;
for (const polygon of polygons) {
const polygonClipped = [];
for (const ring of polygon) {
if (ring.length <= 2) {
continue;
}
const clipped = [];
for (let i = 0; i < ring.length - 1; i++) {
const ax = ring[i].x;
const ay = ring[i].y;
const bx = ring[i + 1].x;
const by = ring[i + 1].y;
const a2 = axis === 0 ? ax : ay;
const b = axis === 0 ? bx : by;
if (a2 < clipAxis1) {
if (b > clipAxis1) {
intersect(clipped, ax, ay, bx, by, clipAxis1);
}
} else if (a2 > clipAxis2) {
if (b < clipAxis2) {
intersect(clipped, ax, ay, bx, by, clipAxis2);
}
} else {
clipped.push(ring[i]);
}
if (b < clipAxis1 && a2 >= clipAxis1) {
intersect(clipped, ax, ay, bx, by, clipAxis1);
}
if (b > clipAxis2 && a2 <= clipAxis2) {
intersect(clipped, ax, ay, bx, by, clipAxis2);
}
}
let last = ring[ring.length - 1];
const a = axis === 0 ? last.x : last.y;
if (a >= clipAxis1 && a <= clipAxis2) {
clipped.push(last);
}
if (clipped.length) {
last = clipped[clipped.length - 1];
if (clipped[0].x !== last.x || clipped[0].y !== last.y) {
clipped.push(clipped[0]);
}
polygonClipped.push(clipped);
}
}
if (polygonClipped.length) {
polygonsClipped.push(polygonClipped);
}
}
return polygonsClipped;
}
function gridSubdivision(polygons, bounds, gridSizeX, gridSizeY, padding = 0, splitFn) {
const outPolygons = [];
if (!polygons.length || !gridSizeX || !gridSizeY) {
return outPolygons;
}
const addResult = (clipped, bounds2) => {
for (const polygon of clipped) {
outPolygons.push({ polygon, bounds: bounds2 });
}
};
const hSplits = Math.ceil(Math.log2(gridSizeX));
const vSplits = Math.ceil(Math.log2(gridSizeY));
const initialSplits = hSplits - vSplits;
const splits = [];
for (let i = 0; i < Math.abs(initialSplits); i++) {
splits.push(initialSplits > 0 ? 0 : 1);
}
for (let i = 0; i < Math.min(hSplits, vSplits); i++) {
splits.push(0);
splits.push(1);
}
let split = polygons;
split = clipPolygon(split, bounds[0].y - padding, bounds[1].y + padding, 1);
split = clipPolygon(split, bounds[0].x - padding, bounds[1].x + padding, 0);
if (!split.length) {
return outPolygons;
}
const stack = [];
if (splits.length) {
stack.push({ polygons: split, bounds, depth: 0 });
} else {
addResult(split, bounds);
}
while (stack.length) {
const frame = stack.pop();
assert$1(frame.polygons.length > 0);
const depth = frame.depth;
const axis = splits[depth];
const bboxMin = frame.bounds[0];
const bboxMax = frame.bounds[1];
const splitMin = axis === 0 ? bboxMin.x : bboxMin.y;
const splitMax = axis === 0 ? bboxMax.x : bboxMax.y;
const splitMid = splitFn ? splitFn(axis, splitMin, splitMax) : 0.5 * (splitMin + splitMax);
const lclip = clipPolygon(frame.polygons, splitMin - padding, splitMid + padding, axis);
const rclip = clipPolygon(frame.polygons, splitMid - padding, splitMax + padding, axis);
if (lclip.length) {
const bbMaxX = axis === 0 ? splitMid : bboxMax.x;
const bbMaxY = axis === 1 ? splitMid : bboxMax.y;
const bbMax = new Point(bbMaxX, bbMaxY);
const lclipBounds = [bboxMin, bbMax];
if (splits.length > depth + 1) {
stack.push({ polygons: lclip, bounds: lclipBounds, depth: depth + 1 });
} else {
addResult(lclip, lclipBounds);
}
}
if (rclip.length) {
const bbMinX = axis === 0 ? splitMid : bboxMin.x;
const bbMinY = axis === 1 ? splitMid : bboxMin.y;
const bbMin = new Point(bbMinX, bbMinY);
const rclipBounds = [bbMin, bboxMax];
if (splits.length > depth + 1) {
stack.push({ polygons: rclip, bounds: rclipBounds, depth: depth + 1 });
} else {
addResult(rclip, rclipBounds);
}
}
}
return outPolygons;
}
function clip(subjectPolygon, clipRing) {
const geom = toMultiPolygon(subjectPolygon);
const clipGeom = toMultiPolygon([clipRing]);
const polygons = martinez_umdExports.intersection(geom, clipGeom);
if (polygons == null) return [];
return fromMultiPolygon(polygons);
}
function polygonSubdivision(subjectPolygon, subdivisionEdges) {
const scale = 1 << 16;
let polygons = toMultiPolygon(subjectPolygon, scale);
const clipGeometry = [];
for (; subdivisionEdges.valid(); subdivisionEdges.next()) {
const [a, b] = subdivisionEdges.get();
const ax = a.x * scale;
const ay = a.y * scale;
const bx = b.x * scale;
const by = b.y * scale;
const dx = bx - ax;
const dy = by - ay;
const len = Math.hypot(dx, dy);
if (len === 0) continue;
const shiftX = Math.trunc(dy / len * 3);
const shiftY = -Math.trunc(dx / len * 3);
clipGeometry.push([
[
[ax, ay],
[bx, by],
[bx + shiftX, by + shiftY],
[ax + shiftX, ay + shiftY],
[ax, ay]
]
]);
}
if (clipGeometry.length > 0) {
polygons = martinez_umdExports.diff(polygons, clipGeometry);
}
return fromMultiPolygon(polygons, 1 / scale);
}
function toMultiPolygon(polygon, scale = 1) {
return [polygon.map((ring) => ring.map((p) => [p.x * scale, p.y * scale]))];
}
function fromMultiPolygon(geometry, scale = 1) {
return geometry.map((poly) => poly.map((ring, index) => {
const r = ring.map((p) => new Point(p[0] * scale, p[1] * scale).round());
if (index > 0) {
r.reverse();
}
return r;
}));
}
const EARCUT_MAX_RINGS$1 = 500;
class FillBufferData {
constructor(options, elevated) {
this.layoutVertexArray = new StructArrayLayout2i4();
this.indexArray = new StructArrayLayout3ui6();
this.lineIndexArray = new StructArrayLayout2ui4();
this.triangleSegments = new SegmentVector();
this.lineSegments = new SegmentVector();
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut });
this.uploaded = false;
if (elevated) {
this.elevatedLayoutVertexArray = new StructArrayLayout1f4();
}
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, worldview) {
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview);
}
isEmpty() {
return this.layoutVertexArray.length === 0;
}
needsUpload() {
return this.programConfigurations.needsUpload;
}
upload(context) {
if (!this.uploaded) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, fillLayoutAttributes.members);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
this.lineIndexBuffer = context.createIndexBuffer(this.lineIndexArray);
if (this.elevatedLayoutVertexArray && this.elevatedLayoutVertexArray.length > 0) {
assert$1(this.layoutVertexArray.length === this.elevatedLayoutVertexArray.length);
this.elevatedLayoutVertexBuffer = context.createVertexBuffer(this.elevatedLayoutVertexArray, fillLayoutAttributesExt.members);
}
}
this.programConfigurations.upload(context);
this.uploaded = true;
}
destroy() {
if (!this.layoutVertexBuffer) return;
if (this.elevatedLayoutVertexBuffer) {
this.elevatedLayoutVertexBuffer.destroy();
}
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.lineIndexBuffer.destroy();
this.programConfigurations.destroy();
this.triangleSegments.destroy();
this.lineSegments.destroy();
}
populatePaintArrays(feature, index, imagePositions, availableImages, canonical, brightness, worldview) {
this.programConfigurations.populatePaintArrays(this.layoutVertexArray.length, feature, index, imagePositions, availableImages, canonical, brightness, void 0, worldview);
}
}
class FillBucket {
constructor(options) {
this.zoom = options.zoom;
this.pixelRatio = options.pixelRatio;
this.overscaling = options.overscaling;
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.hasPattern = false;
this.patternFeatures = [];
this.lut = options.lut;
this.bufferData = new FillBufferData(options, false);
this.elevationBufferData = new FillBufferData(options, true);
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.projection = options.projection;
this.elevationMode = this.layers[0].layout.get("fill-elevation-reference");
this.sourceLayerIndex = options.sourceLayerIndex;
this.worldview = options.worldview;
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
this.hasPattern = hasPattern("fill", this.layers, this.pixelRatio, options);
const fillSortKey = this.layers[0].layout.get("fill-sort-key");
const bucketFeatures = [];
for (const { feature, id, index, sourceLayerIndex } of features) {
const needGeometry = this.layers[0]._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const sortKey = fillSortKey ? fillSortKey.evaluate(evaluationFeature, {}, canonical, options.availableImages) : void 0;
const bucketFeature = {
id,
properties: feature.properties,
type: feature.type,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
patterns: {},
sortKey
};
bucketFeatures.push(bucketFeature);
}
if (fillSortKey) {
bucketFeatures.sort((a, b) => {
return a.sortKey - b.sortKey;
});
}
for (const bucketFeature of bucketFeatures) {
const { geometry, index, sourceLayerIndex } = bucketFeature;
if (this.hasPattern) {
const patternFeature = addPatternDependencies("fill", this.layers, bucketFeature, this.zoom, this.pixelRatio, options);
this.patternFeatures.push(patternFeature);
} else {
this.addFeature(bucketFeature, geometry, index, canonical, {}, options.availableImages, options.brightness, options.elevationFeatures);
}
const feature = features[index].feature;
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
}
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness) {
this.bufferData.update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, this.worldview);
this.elevationBufferData.update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, this.worldview);
if (this.elevatedStructures) {
this.elevatedStructures.update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, this.worldview);
}
}
addFeatures(options, canonical, imagePositions, availableImages, _, brightness) {
for (const feature of this.patternFeatures) {
this.addFeature(feature, feature.geometry, feature.index, canonical, imagePositions, availableImages, brightness, options.elevationFeatures);
}
}
isEmpty() {
return this.bufferData.isEmpty() && this.elevationBufferData.isEmpty();
}
uploadPending() {
return !this.uploaded || this.bufferData.needsUpload() || this.elevationBufferData.needsUpload();
}
upload(context) {
this.bufferData.upload(context);
this.elevationBufferData.upload(context);
if (this.elevatedStructures) {
this.elevatedStructures.upload(context);
}
}
destroy() {
this.bufferData.destroy();
this.elevationBufferData.destroy();
if (this.elevatedStructures) {
this.elevatedStructures.destroy();
}
}
addFeature(feature, geometry, index, canonical, imagePositions, availableImages = [], brightness, elevationFeatures) {
const polygons = classifyRings(geometry, EARCUT_MAX_RINGS$1);
if (this.elevationMode !== "none") {
this.addElevatedRoadFeature(feature, polygons, canonical, index, elevationFeatures);
} else {
this.addGeometry(polygons, this.bufferData);
}
this.bufferData.populatePaintArrays(feature, index, imagePositions, availableImages, canonical, brightness, this.worldview);
this.elevationBufferData.populatePaintArrays(feature, index, imagePositions, availableImages, canonical, brightness, this.worldview);
}
getUnevaluatedPortalGraph() {
return this.elevatedStructures ? this.elevatedStructures.unevaluatedPortals : void 0;
}
getElevationPolygons() {
return this.elevatedStructures ? this.elevatedStructures.portalPolygons : void 0;
}
setEvaluatedPortalGraph(graph, vtLayer, canonical, availableImages, brightness) {
if (this.elevatedStructures) {
this.elevatedStructures.construct(graph);
this.elevatedStructures.populatePaintArrays(vtLayer, canonical, availableImages, brightness, this.worldview);
}
}
addElevatedRoadFeature(feature, polygons, canonical, index, elevationFeatures) {
const elevatedGeometry = new Array();
const tiledElevation = ElevationFeatures.getElevationFeature(feature, elevationFeatures);
if (tiledElevation) {
const clipped = this.clipPolygonsToTile(polygons, ELEVATION_CLIP_MARGIN);
if (clipped.length > 0) {
elevatedGeometry.push({ polygons: clipped, elevationFeature: tiledElevation, elevationTileID: canonical });
}
} else {
this.addGeometry(polygons, this.bufferData);
return;
}
const constructBridgeGuardRail = this.layers[0].layout.get("fill-construct-bridge-guard-rail").evaluate(feature, {}, canonical);
const featureInfo = { guardRailEnabled: constructBridgeGuardRail, featureIndex: index };
for (const elevated of elevatedGeometry) {
if (elevated.elevationFeature) {
if (this.elevationMode === "hd-road-base") {
if (!this.elevatedStructures) {
this.elevatedStructures = new ElevatedStructures(elevated.elevationTileID, this.layers, this.zoom, this.lut);
}
const isTunnel = elevated.elevationFeature.isTunnel();
let zLevel = 0;
if (feature.properties.hasOwnProperty(PROPERTY_ELEVATION_ROAD_BASE_Z_LEVEL)) {
zLevel = +feature.properties[PROPERTY_ELEVATION_ROAD_BASE_Z_LEVEL];
}
for (const polygon of elevated.polygons) {
this.elevatedStructures.addPortalCandidates(
elevated.elevationFeature.id,
polygon,
isTunnel,
elevated.elevationFeature,
zLevel
);
}
}
if (elevated.elevationFeature.constantHeight == null) {
elevated.polygons = this.prepareElevatedPolygons(elevated.polygons, elevated.elevationFeature, elevated.elevationTileID);
}
const elevationSampler = new ElevationFeatureSampler(canonical, elevated.elevationTileID);
if (this.elevationMode === "hd-road-base") {
this.addElevatedGeometry(elevated.polygons, elevationSampler, elevated.elevationFeature, 0, index, featureInfo);
} else {
this.addElevatedGeometry(elevated.polygons, elevationSampler, elevated.elevationFeature, MARKUP_ELEVATION_BIAS, index, featureInfo);
}
}
}
}
addElevatedGeometry(polygons, elevationSampler, elevation, bias, index, featureInfo) {
const elevationParams = { elevation, elevationSampler, bias, index, featureInfo };
const [min, max] = this.addGeometry(polygons, this.elevationBufferData, elevationParams);
if (this.elevationBufferData.heightRange == null) {
this.elevationBufferData.heightRange = { min, max };
} else {
this.elevationBufferData.heightRange.min = Math.min(this.elevationBufferData.heightRange.min, min);
this.elevationBufferData.heightRange.max = Math.max(this.elevationBufferData.heightRange.max, max);
}
}
addGeometry(polygons, bufferData, elevationParams) {
let min = Number.POSITIVE_INFINITY;
let max = Number.NEGATIVE_INFINITY;
let constantHeight = null;
if (elevationParams) {
constantHeight = elevationParams.elevationSampler.constantElevation(elevationParams.elevation, elevationParams.bias);
if (constantHeight != null) {
min = constantHeight;
max = constantHeight;
}
}
const addElevatedVertex = (point, points, heights) => {
if (elevationParams == null) return;
points.push(point);
if (constantHeight != null) {
bufferData.elevatedLayoutVertexArray.emplaceBack(constantHeight);
heights.push(constantHeight);
} else {
const height = elevationParams.elevationSampler.pointElevation(point, elevationParams.elevation, elevationParams.bias);
bufferData.elevatedLayoutVertexArray.emplaceBack(height);
heights.push(height);
min = Math.min(min, height);
max = Math.max(max, height);
}
};
for (const polygon of polygons) {
let numVertices = 0;
for (const ring of polygon) {
numVertices += ring.length;
}
const triangleSegment = bufferData.triangleSegments.prepareSegment(numVertices, bufferData.layoutVertexArray, bufferData.indexArray);
const triangleIndex = triangleSegment.vertexLength;
const flattened = [];
const holeIndices = [];
const points = [];
const heights = [];
const ringVertexOffsets = [];
const vOffset = bufferData.layoutVertexArray.length;
for (const ring of polygon) {
if (ring.length === 0) {
continue;
}
if (ring !== polygon[0]) {
holeIndices.push(flattened.length / 2);
}
const lineSegment = bufferData.lineSegments.prepareSegment(ring.length, bufferData.layoutVertexArray, bufferData.lineIndexArray);
const lineIndex = lineSegment.vertexLength;
if (elevationParams) {
ringVertexOffsets.push(bufferData.layoutVertexArray.length - vOffset);
}
addElevatedVertex(ring[0], points, heights);
bufferData.layoutVertexArray.emplaceBack(ring[0].x, ring[0].y);
bufferData.lineIndexArray.emplaceBack(lineIndex + ring.length - 1, lineIndex);
flattened.push(ring[0].x);
flattened.push(ring[0].y);
for (let i = 1; i < ring.length; i++) {
addElevatedVertex(ring[i], points, heights);
bufferData.layoutVertexArray.emplaceBack(ring[i].x, ring[i].y);
bufferData.lineIndexArray.emplaceBack(lineIndex + i - 1, lineIndex + i);
flattened.push(ring[i].x);
flattened.push(ring[i].y);
}
lineSegment.vertexLength += ring.length;
lineSegment.primitiveLength += ring.length;
}
const indices = earcut(flattened, holeIndices);
assert$1(indices.length % 3 === 0);
for (let i = 0; i < indices.length; i += 3) {
bufferData.indexArray.emplaceBack(
triangleIndex + indices[i],
triangleIndex + indices[i + 1],
triangleIndex + indices[i + 2]
);
}
if (indices.length > 0 && elevationParams && this.elevationMode === "hd-road-base") {
const isTunnel = elevationParams.elevation.isTunnel();
const safeArea = elevationParams.elevation.safeArea;
const vOffset2 = this.elevatedStructures.addVertices(points, heights);
this.elevatedStructures.addTriangles(indices, vOffset2, isTunnel);
const ringCount = ringVertexOffsets.length;
if (ringCount > 0) {
for (let i = 0; i < ringCount - 1; i++) {
this.elevatedStructures.addRenderableRing(
elevationParams.index,
ringVertexOffsets[i] + vOffset2,
ringVertexOffsets[i + 1] - ringVertexOffsets[i],
isTunnel,
safeArea,
elevationParams.featureInfo
);
}
this.elevatedStructures.addRenderableRing(
elevationParams.index,
ringVertexOffsets[ringCount - 1] + vOffset2,
points.length - ringVertexOffsets[ringCount - 1],
isTunnel,
safeArea,
elevationParams.featureInfo
);
}
}
triangleSegment.vertexLength += numVertices;
triangleSegment.primitiveLength += indices.length / 3;
}
return [min, max];
}
prepareElevatedPolygons(polygons, elevation, tileID) {
const metersToTile = 1 / tileToMeter(tileID);
const clippedPolygons = [];
for (const poly of polygons) {
const clippedPoly = polygonSubdivision(poly, new EdgeIterator(elevation, metersToTile));
clippedPolygons.push(...clippedPoly);
}
return clippedPolygons;
}
clipPolygonsToTile(polygons, margin) {
const minX = -margin;
const minY = -margin;
const maxX = EXTENT + margin;
const maxY = EXTENT + margin;
let clipIdx = 0;
const noClippingGroup = [];
const clippingGroup = [];
for (; clipIdx < polygons.length; clipIdx++) {
const polygon = polygons[clipIdx];
assert$1(polygon.length > 0);
const bounds = computeBounds(polygon);
const noClipping = bounds.min.x >= minX && bounds.max.x <= maxX && bounds.min.y >= minY && bounds.max.y <= maxY;
const dst = noClipping ? noClippingGroup : clippingGroup;
dst.push(polygon);
}
if (noClippingGroup.length === polygons.length) return polygons;
const clipPoly = [
new Point(minX, minY),
new Point(maxX, minY),
new Point(maxX, maxY),
new Point(minX, maxY),
new Point(minX, minY)
];
const clippedPolygons = noClippingGroup;
for (const poly of clippingGroup) {
clippedPolygons.push(...clip(poly, clipPoly));
}
return clippedPolygons;
}
}
register(FillBucket, "FillBucket", { omit: ["layers", "patternFeatures"] });
register(FillBufferData, "FillBufferData");
register(ElevatedStructures, "ElevatedStructures");
let layout$a;
const getLayoutProperties$b = () => layout$a || (layout$a = new Properties({
"fill-sort-key": new DataDrivenProperty(spec["layout_fill"]["fill-sort-key"]),
"visibility": new DataConstantProperty(spec["layout_fill"]["visibility"]),
"fill-elevation-reference": new DataConstantProperty(spec["layout_fill"]["fill-elevation-reference"]),
"fill-construct-bridge-guard-rail": new DataDrivenProperty(spec["layout_fill"]["fill-construct-bridge-guard-rail"])
}));
let paint$b;
const getPaintProperties$b = () => paint$b || (paint$b = new Properties({
"fill-antialias": new DataConstantProperty(spec["paint_fill"]["fill-antialias"]),
"fill-opacity": new DataDrivenProperty(spec["paint_fill"]["fill-opacity"]),
"fill-color": new DataDrivenProperty(spec["paint_fill"]["fill-color"]),
"fill-outline-color": new DataDrivenProperty(spec["paint_fill"]["fill-outline-color"]),
"fill-translate": new DataConstantProperty(spec["paint_fill"]["fill-translate"]),
"fill-translate-anchor": new DataConstantProperty(spec["paint_fill"]["fill-translate-anchor"]),
"fill-pattern": new DataDrivenProperty(spec["paint_fill"]["fill-pattern"]),
"fill-pattern-cross-fade": new DataConstantProperty(spec["paint_fill"]["fill-pattern-cross-fade"]),
"fill-emissive-strength": new DataConstantProperty(spec["paint_fill"]["fill-emissive-strength"]),
"fill-z-offset": new DataDrivenProperty(spec["paint_fill"]["fill-z-offset"]),
"fill-bridge-guard-rail-color": new DataDrivenProperty(spec["paint_fill"]["fill-bridge-guard-rail-color"]),
"fill-tunnel-structure-color": new DataDrivenProperty(spec["paint_fill"]["fill-tunnel-structure-color"]),
"fill-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"fill-outline-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"fill-bridge-guard-rail-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"fill-tunnel-structure-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class FillStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$b(),
paint: getPaintProperties$b()
};
super(layer, properties, scope, lut, options);
}
getProgramIds() {
const pattern = this.paint.get("fill-pattern");
const image = pattern && pattern.constantOr(1);
const ids = [image ? "fillPattern" : "fill"];
if (this.paint.get("fill-antialias")) {
ids.push(image && !this.getPaintProperty("fill-outline-color") ? "fillOutlinePattern" : "fillOutline");
}
return ids;
}
getDefaultProgramParams(name, zoom, lut) {
return {
config: new ProgramConfiguration(this, { zoom, lut }),
overrideFog: false
};
}
recalculate(parameters, availableImages) {
super.recalculate(parameters, availableImages);
const outlineColor = this.paint._values["fill-outline-color"];
if (outlineColor.value.kind === "constant" && outlineColor.value.value === void 0) {
this.paint._values["fill-outline-color"] = this.paint._values["fill-color"];
}
}
createBucket(parameters) {
return new FillBucket(parameters);
}
queryRadius() {
return translateDistance(this.paint.get("fill-translate"));
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform) {
if (queryGeometry.queryGeometry.isAboveHorizon) return false;
const translatedPolygon = translate(
queryGeometry.tilespaceGeometry,
this.paint.get("fill-translate"),
this.paint.get("fill-translate-anchor"),
transform.angle,
queryGeometry.pixelToTileUnitsFactor
);
return polygonIntersectsMultiPolygon(translatedPolygon, geometry);
}
isTileClipped() {
return this.paint.get("fill-z-offset").constantOr(1) === 0;
}
is3D(terrainEnabled) {
if (this.paint.get("fill-z-offset").constantOr(1) !== 0) return true;
const potentially3D = this.layout && this.layout.get("fill-elevation-reference") !== "none";
return terrainEnabled != null ? potentially3D && !terrainEnabled : potentially3D;
}
hasElevation() {
return this.layout && this.layout.get("fill-elevation-reference") !== "none";
}
hasShadowPass() {
return this.layout && this.layout.get("fill-elevation-reference") !== "none";
}
}
class TriangleGridIndex {
constructor(vertices, indices, cellCount, maxCellSize) {
this.triangleCount = indices.length / 3;
this.min = new Point(0, 0);
this.max = new Point(0, 0);
this.xScale = 0;
this.yScale = 0;
this.cellsX = 0;
this.cellsY = 0;
this.cells = [];
this.payload = [];
if (this.triangleCount === 0 || vertices.length === 0) {
return;
}
const [min, max] = [vertices[0].clone(), vertices[0].clone()];
for (let i = 1; i < vertices.length; ++i) {
const v = vertices[i];
min.x = Math.min(min.x, v.x);
min.y = Math.min(min.y, v.y);
max.x = Math.max(max.x, v.x);
max.y = Math.max(max.y, v.y);
}
if (maxCellSize) {
const optimalCellCount = Math.ceil(Math.max(max.x - min.x, max.y - min.y) / maxCellSize);
cellCount = Math.max(cellCount, optimalCellCount);
}
if (cellCount === 0) {
return;
}
this.min = min;
this.max = max;
const size = this.max.sub(this.min);
size.x = Math.max(size.x, 1);
size.y = Math.max(size.y, 1);
const maxExt = Math.max(size.x, size.y);
const cellSize = maxExt / cellCount;
this.cellsX = Math.max(1, Math.ceil(size.x / cellSize));
this.cellsY = Math.max(1, Math.ceil(size.y / cellSize));
this.xScale = 1 / cellSize;
this.yScale = 1 / cellSize;
const associatedTriangles = [];
for (let t = 0; t < this.triangleCount; t++) {
const v0 = vertices[indices[t * 3 + 0]].sub(this.min);
const v1 = vertices[indices[t * 3 + 1]].sub(this.min);
const v2 = vertices[indices[t * 3 + 2]].sub(this.min);
const minx = toCellIdx(Math.floor(Math.min(v0.x, v1.x, v2.x)), this.xScale, this.cellsX);
const maxx = toCellIdx(Math.floor(Math.max(v0.x, v1.x, v2.x)), this.xScale, this.cellsX);
const miny = toCellIdx(Math.floor(Math.min(v0.y, v1.y, v2.y)), this.yScale, this.cellsY);
const maxy = toCellIdx(Math.floor(Math.max(v0.y, v1.y, v2.y)), this.yScale, this.cellsY);
const c00 = new Point(0, 0);
const c10 = new Point(0, 0);
const c01 = new Point(0, 0);
const c11 = new Point(0, 0);
for (let y = miny; y <= maxy; ++y) {
c00.y = c10.y = y * cellSize;
c01.y = c11.y = (y + 1) * cellSize;
for (let x = minx; x <= maxx; ++x) {
c00.x = c01.x = x * cellSize;
c10.x = c11.x = (x + 1) * cellSize;
if (!triangleIntersectsTriangle(v0, v1, v2, c00, c10, c11) && !triangleIntersectsTriangle(v0, v1, v2, c00, c11, c01)) {
continue;
}
associatedTriangles.push({ cellIdx: y * this.cellsX + x, triIdx: t });
}
}
}
if (associatedTriangles.length === 0) {
return;
}
associatedTriangles.sort((a, b) => a.cellIdx - b.cellIdx || a.triIdx - b.triIdx);
let idx = 0;
while (idx < associatedTriangles.length) {
const cellIdx = associatedTriangles[idx].cellIdx;
const cell = { start: this.payload.length, len: 0 };
while (idx < associatedTriangles.length && associatedTriangles[idx].cellIdx === cellIdx) {
++cell.len;
this.payload.push(associatedTriangles[idx++].triIdx);
}
this.cells[cellIdx] = cell;
}
}
_lazyInitLookup() {
if (!this.lookup) {
this.lookup = new Uint8Array(Math.ceil(this.triangleCount / 8));
}
this.lookup.fill(0);
}
queryPoint(p, out) {
if (this.triangleCount === 0 || this.cells.length === 0) {
return;
}
if (p.x > this.max.x || this.min.x > p.x || p.y > this.max.y || this.min.y > p.y) {
return;
}
const x = toCellIdx(p.x - this.min.x, this.xScale, this.cellsX);
const y = toCellIdx(p.y - this.min.y, this.yScale, this.cellsY);
const cell = this.cells[y * this.cellsX + x];
if (!cell) {
return;
}
this._lazyInitLookup();
for (let i = 0; i < cell.len; i++) {
const triIdx = this.payload[cell.start + i];
const byte = Math.floor(triIdx / 8);
const bit = 1 << triIdx % 8;
if (this.lookup[byte] & bit) {
continue;
}
this.lookup[byte] |= bit;
out.push(triIdx);
if (out.length === this.triangleCount) {
return;
}
}
}
query(bbMin, bbMax, out) {
if (this.triangleCount === 0 || this.cells.length === 0) {
return;
}
if (bbMin.x > this.max.x || this.min.x > bbMax.x) {
return;
} else if (bbMin.y > this.max.y || this.min.y > bbMax.y) {
return;
}
this._lazyInitLookup();
const mnx = toCellIdx(bbMin.x - this.min.x, this.xScale, this.cellsX);
const mxx = toCellIdx(bbMax.x - this.min.x, this.xScale, this.cellsX);
const mny = toCellIdx(bbMin.y - this.min.y, this.yScale, this.cellsY);
const mxy = toCellIdx(bbMax.y - this.min.y, this.yScale, this.cellsY);
for (let y = mny; y <= mxy; y++) {
for (let x = mnx; x <= mxx; x++) {
const cell = this.cells[y * this.cellsX + x];
if (!cell) {
continue;
}
for (let i = 0; i < cell.len; i++) {
const triIdx = this.payload[cell.start + i];
const byte = Math.floor(triIdx / 8);
const bit = 1 << triIdx % 8;
if (this.lookup[byte] & bit) {
continue;
}
this.lookup[byte] |= bit;
out.push(triIdx);
if (out.length === this.triangleCount) {
return;
}
}
}
}
}
}
function toCellIdx(p, scale, cells) {
return Math.max(0, Math.min(cells - 1, Math.floor(p * scale)));
}
register(TriangleGridIndex, "TriangleGridIndex");
class ClipBucket {
constructor(options) {
this.zoom = options.zoom;
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.hasPattern = false;
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.footprints = [];
this.worldview = options.worldview;
this.hasAppearances = null;
}
updateFootprints(id, footprints) {
for (const footprint of this.footprints) {
footprints.push({
footprint,
id
});
}
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
const bucketFeatures = [];
for (const { feature, id, index, sourceLayerIndex } of features) {
const needGeometry = this.layers[0]._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const bucketFeature = {
id,
properties: feature.properties,
type: feature.type,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
patterns: {}
};
bucketFeatures.push(bucketFeature);
}
for (const bucketFeature of bucketFeatures) {
const { geometry, index, sourceLayerIndex } = bucketFeature;
this.addFeature(bucketFeature, geometry, index, canonical, {}, options.availableImages, options.brightness);
const feature = features[index].feature;
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
}
}
isEmpty() {
return this.footprints.length === 0;
}
uploadPending() {
return false;
}
upload(_context) {
}
update(_states, _vtLayer, _availableImages, _imagePositions, _layers, _isBrightnessChanged, _brightness) {
}
destroy() {
}
addFeature(feature, geometry, index, canonical, imagePositions, _availableImages = [], _brightness) {
for (const polygon of classifyRings(geometry, 2)) {
const points = [];
const flattened = [];
const holeIndices = [];
const min = new Point(Infinity, Infinity);
const max = new Point(-Infinity, -Infinity);
for (const ring of polygon) {
if (ring.length === 0) {
continue;
}
if (ring !== polygon[0]) {
holeIndices.push(flattened.length / 2);
}
for (let i = 0; i < ring.length; i++) {
flattened.push(ring[i].x);
flattened.push(ring[i].y);
points.push(ring[i]);
min.x = Math.min(min.x, ring[i].x);
min.y = Math.min(min.y, ring[i].y);
max.x = Math.max(max.x, ring[i].x);
max.y = Math.max(max.y, ring[i].y);
}
}
const indices = earcut(flattened, holeIndices);
assert$1(indices.length % 3 === 0);
const grid = new TriangleGridIndex(points, indices, 8, 256);
this.footprints.push({
vertices: points,
indices,
grid,
min,
max
});
}
}
}
register(ClipBucket, "ClipBucket", { omit: ["layers"] });
let layout$9;
const getLayoutProperties$a = () => layout$9 || (layout$9 = new Properties({
"clip-layer-types": new DataConstantProperty(spec["layout_clip"]["clip-layer-types"]),
"clip-layer-scope": new DataConstantProperty(spec["layout_clip"]["clip-layer-scope"])
}));
let paint$a;
const getPaintProperties$a = () => paint$a || (paint$a = new Properties({}));
class ClipStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$a(),
paint: getPaintProperties$a()
};
super(layer, properties, scope, lut, options);
}
recalculate(parameters, availableImages) {
super.recalculate(parameters, availableImages);
}
createBucket(parameters) {
return new ClipBucket(parameters);
}
is3D(terrainEnabled) {
return true;
}
}
const fillExtrusionAttributes = createLayout([
{ name: "a_pos_normal_ed", components: 4, type: "Int16" }
]);
const fillExtrusionGroundAttributes = createLayout([
{ name: "a_pos_end", components: 4, type: "Int16" },
{ name: "a_angular_offset_factor", components: 1, type: "Int16" }
]);
const fillExtrusionGroundRadiusAttributes = createLayout([
{ name: "a_flood_light_ground_radius", components: 1, type: "Float32" }
]);
const centroidAttributes = createLayout([
{ name: "a_centroid_pos", components: 2, type: "Uint16" }
]);
const wallAttributes = createLayout([
{ name: "a_join_normal_inside", components: 3, type: "Int16" }
]);
const hiddenByLandmarkAttributes = createLayout([
{ name: "a_hidden_by_landmark", components: 1, type: "Uint8" }
]);
const fillExtrusionAttributesExt = createLayout([
{ name: "a_pos_3", components: 3, type: "Int16" },
{ name: "a_pos_normal_3", components: 3, type: "Int16" }
]);
const { members: members$5, size: size$5, alignment: alignment$5 } = fillExtrusionAttributes;
const ReplacementOrderLandmark = Number.MAX_SAFE_INTEGER;
const ReplacementOrderBuilding = ReplacementOrderLandmark - 1;
function scopeSkipsClipping(scope, scopes) {
return scopes.length !== 0 && scopes.find((el) => {
return el === scope;
}) === void 0;
}
function skipClipping(region, layerIndex, mask, scope) {
return region.order < layerIndex || region.order === ReplacementOrderLandmark || !(region.clipMask & mask) || scopeSkipsClipping(scope, region.clipScope);
}
class ReplacementSource {
constructor() {
this._updateTime = 0;
this._sourceIds = [];
this._activeRegions = [];
this._prevRegions = [];
this._globalClipBounds = { min: new Point(Infinity, Infinity), max: new Point(-Infinity, -Infinity) };
}
clear() {
if (this._activeRegions.length > 0) {
++this._updateTime;
}
this._activeRegions = [];
this._prevRegions = [];
}
get updateTime() {
return this._updateTime;
}
getReplacementRegionsForTile(id, checkAgainstGlobalClipBounds = false) {
const tileBounds = transformAabbToMerc(new Point(0, 0), new Point(EXTENT, EXTENT), id);
const result = [];
if (checkAgainstGlobalClipBounds) {
if (!regionsOverlap(tileBounds, this._globalClipBounds))
return result;
}
for (const region of this._activeRegions) {
if (region.hiddenByOverlap) {
continue;
}
if (!regionsOverlap(tileBounds, region)) {
continue;
}
const bounds = transformAabbToTile(region.min, region.max, id);
result.push({
min: bounds.min,
max: bounds.max,
sourceId: this._sourceIds[region.priority],
footprint: region.footprint,
footprintTileId: region.tileId,
order: region.order,
clipMask: region.clipMask,
clipScope: region.clipScope
});
}
return result;
}
setSources(sources) {
this._setSources(sources.map((source) => {
return {
getSourceId: () => {
return source.cache.id;
},
getFootprints: () => {
const footprints = [];
for (const id of source.cache.getVisibleCoordinates()) {
const tile = source.cache.getTile(id);
const bucket = tile.buckets[source.layer];
if (bucket) {
bucket.updateFootprints(id.toUnwrapped(), footprints);
}
}
return footprints;
},
getOrder: () => {
return source.order;
},
getClipMask: () => {
return source.clipMask;
},
getClipScope: () => {
return source.clipScope;
}
};
}));
}
_addSource(source) {
const footprints = source.getFootprints();
if (footprints.length === 0) {
return;
}
const order = source.getOrder();
const clipMask = source.getClipMask();
const clipScope = source.getClipScope();
for (const fp of footprints) {
if (!fp.footprint) {
continue;
}
const bounds = transformAabbToMerc(fp.footprint.min, fp.footprint.max, fp.id);
this._activeRegions.push({
min: bounds.min,
max: bounds.max,
hiddenByOverlap: false,
priority: this._sourceIds.length,
tileId: fp.id,
footprint: fp.footprint,
order,
clipMask,
clipScope
});
}
this._sourceIds.push(source.getSourceId());
}
_computeReplacement() {
this._activeRegions.sort((a, b) => {
return a.priority - b.priority || comparePoint(a.min, b.min) || comparePoint(a.max, b.max) || a.order - b.order || a.clipMask - b.clipMask || compareClipScopes(a.clipScope, b.clipScope);
});
let regionsChanged = this._activeRegions.length !== this._prevRegions.length;
if (!regionsChanged) {
let idx = 0;
while (!regionsChanged && idx !== this._activeRegions.length) {
const curr = this._activeRegions[idx];
const prev = this._prevRegions[idx];
regionsChanged = curr.priority !== prev.priority || !boundsEquals(curr, prev) || curr.order !== prev.order || (curr.clipMask !== prev.clipMask || !deepEqual(curr.clipScope, prev.clipScope));
this._activeRegions[idx].hiddenByOverlap = prev.hiddenByOverlap;
++idx;
}
}
if (regionsChanged) {
++this._updateTime;
for (const region of this._activeRegions) {
if (region.order !== ReplacementOrderLandmark) {
this._globalClipBounds.min.x = Math.min(this._globalClipBounds.min.x, region.min.x);
this._globalClipBounds.min.y = Math.min(this._globalClipBounds.min.y, region.min.y);
this._globalClipBounds.max.x = Math.max(this._globalClipBounds.max.x, region.max.x);
this._globalClipBounds.max.y = Math.max(this._globalClipBounds.max.y, region.max.y);
}
}
const firstRegionOfNextPriority = (idx) => {
const regs = this._activeRegions;
if (idx >= regs.length) {
return idx;
}
const priority = regs[idx].priority;
while (idx < regs.length && regs[idx].priority === priority) {
++idx;
}
return idx;
};
if (this._sourceIds.length > 1) {
let rangeBegin = 0;
let rangeEnd = firstRegionOfNextPriority(rangeBegin);
while (rangeBegin !== rangeEnd) {
let idx = rangeBegin;
const prevRangeEnd = rangeBegin;
while (idx !== rangeEnd) {
const active = this._activeRegions[idx];
active.hiddenByOverlap = false;
for (let prevIdx = 0; prevIdx < prevRangeEnd; prevIdx++) {
const prev = this._activeRegions[prevIdx];
if (prev.hiddenByOverlap) {
continue;
}
if (active.order !== ReplacementOrderLandmark) {
continue;
}
if (regionsOverlap(active, prev)) {
active.hiddenByOverlap = footprintsIntersect(active.footprint, active.tileId, prev.footprint, prev.tileId);
if (active.hiddenByOverlap) {
break;
}
}
}
++idx;
}
rangeBegin = rangeEnd;
rangeEnd = firstRegionOfNextPriority(rangeBegin);
}
}
}
}
_setSources(sources) {
[this._prevRegions, this._activeRegions] = [this._activeRegions, []];
this._sourceIds = [];
for (let i = sources.length - 1; i >= 0; i--) {
this._addSource(sources[i]);
}
this._computeReplacement();
}
}
function comparePoint(a, b) {
return a.x - b.x || a.y - b.y;
}
function compareClipScopes(a, b) {
const concat = (t, n) => {
return t + n;
};
return a.length - b.length || a.reduce(concat, "").localeCompare(b.reduce(concat, ""));
}
function boundsEquals(a, b) {
return comparePoint(a.min, b.min) === 0 && comparePoint(a.max, b.max) === 0;
}
function regionsOverlap(a, b) {
if (a.min.x > b.max.x || a.max.x < b.min.x)
return false;
else if (a.min.y > b.max.y || a.max.y < b.min.y)
return false;
return true;
}
function regionsEquals(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (a[i].sourceId !== b[i].sourceId || !boundsEquals(a[i], b[i]) || a[i].order !== b[i].order || a[i].clipMask !== b[i].clipMask || !deepEqual(a[i].clipScope, b[i].clipScope)) {
return false;
}
}
return true;
}
function transformAabbToMerc(min, max, id) {
const invExtent = 1 / EXTENT;
const invTiles = 1 / (1 << id.canonical.z);
const minx = (min.x * invExtent + id.canonical.x) * invTiles + id.wrap;
const maxx = (max.x * invExtent + id.canonical.x) * invTiles + id.wrap;
const miny = (min.y * invExtent + id.canonical.y) * invTiles;
const maxy = (max.y * invExtent + id.canonical.y) * invTiles;
return {
min: new Point(minx, miny),
max: new Point(maxx, maxy)
};
}
function transformAabbToTile(min, max, id) {
const tiles = 1 << id.canonical.z;
const minx = ((min.x - id.wrap) * tiles - id.canonical.x) * EXTENT;
const maxx = ((max.x - id.wrap) * tiles - id.canonical.x) * EXTENT;
const miny = (min.y * tiles - id.canonical.y) * EXTENT;
const maxy = (max.y * tiles - id.canonical.y) * EXTENT;
return {
min: new Point(minx, miny),
max: new Point(maxx, maxy)
};
}
function footprintTrianglesIntersect(footprint, vertices, indices, indexOffset, indexCount, baseVertex, padding) {
const fpIndices = footprint.indices;
const fpVertices = footprint.vertices;
const candidateTriangles = [];
for (let i = indexOffset; i < indexOffset + indexCount; i += 3) {
const a = vertices[indices[i + 0] + baseVertex];
const b = vertices[indices[i + 1] + baseVertex];
const c = vertices[indices[i + 2] + baseVertex];
const mnx = Math.min(a.x, b.x, c.x);
const mxx = Math.max(a.x, b.x, c.x);
const mny = Math.min(a.y, b.y, c.y);
const mxy = Math.max(a.y, b.y, c.y);
candidateTriangles.length = 0;
footprint.grid.query(new Point(mnx, mny), new Point(mxx, mxy), candidateTriangles);
for (let j = 0; j < candidateTriangles.length; j++) {
const triIdx = candidateTriangles[j];
const v0 = fpVertices[fpIndices[triIdx * 3 + 0]];
const v1 = fpVertices[fpIndices[triIdx * 3 + 1]];
const v2 = fpVertices[fpIndices[triIdx * 3 + 2]];
if (triangleIntersectsTriangle(v0, v1, v2, a, b, c, padding)) {
return true;
}
}
}
return false;
}
function footprintsIntersect(a, aTile, b, bTile) {
if (!a || !b) {
return false;
}
let queryVertices = a.vertices;
if (!aTile.canonical.equals(bTile.canonical) || aTile.wrap !== bTile.wrap) {
if (b.vertices.length < a.vertices.length) {
return footprintsIntersect(b, bTile, a, aTile);
}
const srcId = aTile.canonical;
const dstId = bTile.canonical;
const zDiff = Math.pow(2, dstId.z - srcId.z);
queryVertices = a.vertices.map((v) => {
const x = (v.x + srcId.x * EXTENT) * zDiff - dstId.x * EXTENT;
const y = (v.y + srcId.y * EXTENT) * zDiff - dstId.y * EXTENT;
return new Point(x, y);
});
}
return footprintTrianglesIntersect(b, queryVertices, a.indices, 0, a.indices.length, 0, 0);
}
function transformPointToTile(x, y, src, dst) {
const zDiff = Math.pow(2, dst.z - src.z);
const xf = (x + src.x * EXTENT) * zDiff - dst.x * EXTENT;
const yf = (y + src.y * EXTENT) * zDiff - dst.y * EXTENT;
return new Point(xf, yf);
}
function pointInFootprint(p, footprint) {
const candidateTriangles = [];
footprint.grid.queryPoint(p, candidateTriangles);
const fpIndices = footprint.indices;
const fpVertices = footprint.vertices;
for (let j = 0; j < candidateTriangles.length; j++) {
const triIdx = candidateTriangles[j];
const triangle = [
fpVertices[fpIndices[triIdx * 3 + 0]],
fpVertices[fpIndices[triIdx * 3 + 1]],
fpVertices[fpIndices[triIdx * 3 + 2]]
];
if (polygonContainsPoint(triangle, p)) {
return true;
}
}
return false;
}
function isClockWise(vertices) {
let signedArea = 0;
const n = vertices.length;
for (let i = 0; i < n; i++) {
const x1 = vertices[i].x;
const y1 = vertices[i].y;
const x2 = vertices[(i + 1) % n].x;
const y2 = vertices[(i + 1) % n].y;
signedArea += (x2 - x1) * (y2 + y1);
}
return signedArea >= 0;
}
function createLineWallGeometry(vertices) {
const isPolygon = vertices[0].x === vertices[vertices.length - 1].x && vertices[0].y === vertices[vertices.length - 1].y;
const isCW = isClockWise(vertices);
if (!isCW) {
vertices = vertices.reverse();
}
const wallGeometry = {
geometry: [],
joinNormals: [],
indices: []
};
const innerWall = [];
const outerWall = [];
const joinNormals = [];
let len = vertices.length;
while (len >= 2 && vertices[len - 1].equals(vertices[len - 2])) {
len--;
}
if (len < (isPolygon ? 3 : 2)) return wallGeometry;
let first = 0;
while (first < len - 1 && vertices[first].equals(vertices[first + 1])) {
first++;
}
let currentVertex;
let prevVertex;
let nextVertex;
let prevNormal;
let nextNormal;
if (isPolygon) {
currentVertex = vertices[len - 2];
nextNormal = vertices[first].sub(currentVertex)._unit()._perp();
}
for (let i = first; i < len; i++) {
nextVertex = i === len - 1 ? isPolygon ? vertices[first + 1] : void 0 : (
// if it's a polygon, treat the last vertex like the first
vertices[i + 1]
);
if (nextVertex && vertices[i].equals(nextVertex)) continue;
if (nextNormal) prevNormal = nextNormal;
if (currentVertex) prevVertex = currentVertex;
currentVertex = vertices[i];
nextNormal = nextVertex ? nextVertex.sub(currentVertex)._unit()._perp() : prevNormal;
prevNormal = prevNormal || nextNormal;
let joinNormal = prevNormal.add(nextNormal);
if (joinNormal.x !== 0 || joinNormal.y !== 0) {
joinNormal._unit();
}
const cosHalfAngle = joinNormal.x * nextNormal.x + joinNormal.y * nextNormal.y;
const miterLength = cosHalfAngle !== 0 ? 1 / cosHalfAngle : Infinity;
const lineTurnsLeft = prevNormal.x * nextNormal.y - prevNormal.y * nextNormal.x > 0;
let currentJoin = "miter";
const miterLimit = 2;
if (currentJoin === "miter" && miterLength > miterLimit) {
currentJoin = "bevel";
}
if (currentJoin === "bevel") {
if (miterLength > 100) currentJoin = "flipbevel";
if (miterLength < miterLimit) currentJoin = "miter";
}
const addWallJoin = (vert, normal, outerOffset, innerOffset) => {
const innerPoint = new Point(vert.x, vert.y);
const outerPoint = new Point(vert.x, vert.y);
innerPoint.x += normal.x * innerOffset;
innerPoint.y += normal.y * innerOffset;
outerPoint.x -= normal.x * Math.max(outerOffset, 1);
outerPoint.y -= normal.y * Math.max(outerOffset, 1);
joinNormals.push(normal);
innerWall.push(innerPoint);
outerWall.push(outerPoint);
};
if (currentJoin === "miter") {
joinNormal._mult(miterLength);
addWallJoin(currentVertex, joinNormal, 0, 0);
} else if (currentJoin === "flipbevel") {
joinNormal = nextNormal.mult(-1);
addWallJoin(currentVertex, joinNormal, 0, 0);
addWallJoin(currentVertex, joinNormal.mult(-1), 0, 0);
} else {
const offset = -Math.sqrt(miterLength * miterLength - 1);
const offsetA = lineTurnsLeft ? offset : 0;
const offsetB = lineTurnsLeft ? 0 : offset;
if (prevVertex) {
addWallJoin(currentVertex, prevNormal, offsetA, offsetB);
}
if (nextVertex) {
addWallJoin(currentVertex, nextNormal, offsetA, offsetB);
}
}
}
wallGeometry.geometry = [...innerWall, ...outerWall.reverse(), innerWall[0]];
wallGeometry.joinNormals = [...joinNormals, ...joinNormals.reverse(), joinNormals[joinNormals.length - 1]];
const numPoints = wallGeometry.geometry.length - 1;
for (let i = 0; i < numPoints / 2; i++) {
if (i + 1 < numPoints / 2) {
let indexA = i;
let indexB = i + 1;
let indexC = numPoints - 1 - i;
let indexD = numPoints - 2 - i;
indexA = indexA === 0 ? numPoints - 1 : indexA - 1;
indexB = indexB === 0 ? numPoints - 1 : indexB - 1;
indexC = indexC === 0 ? numPoints - 1 : indexC - 1;
indexD = indexD === 0 ? numPoints - 1 : indexD - 1;
wallGeometry.indices.push(indexC);
wallGeometry.indices.push(indexB);
wallGeometry.indices.push(indexA);
wallGeometry.indices.push(indexC);
wallGeometry.indices.push(indexD);
wallGeometry.indices.push(indexB);
}
}
return wallGeometry;
}
const tileCorners = [
new Point(0, 0),
new Point(EXTENT, 0),
new Point(EXTENT, EXTENT),
new Point(0, EXTENT)
];
function dropBufferConnectionLines(polygon, isPolygon) {
const lineSegments = [];
let lineSegment = [];
if (!isPolygon || polygon.length < 2) {
return [polygon];
} else if (polygon.length === 2) {
if (edgeIntersectsBox(polygon[0], polygon[1], tileCorners)) {
return [polygon];
}
return [];
} else {
for (let i = 0; i < polygon.length + 2; i++) {
const p0 = i === 0 ? polygon[polygon.length - 1] : polygon[(i - 1) % polygon.length];
const p1 = polygon[i % polygon.length];
const p2 = polygon[(i + 1) % polygon.length];
const intersectsPrev = edgeIntersectsBox(p0, p1, tileCorners);
const intersectsNext = edgeIntersectsBox(p1, p2, tileCorners);
const addPoint = intersectsPrev || intersectsNext;
if (addPoint) {
lineSegment.push(p1);
}
if (!addPoint || !intersectsNext) {
if (lineSegment.length > 0) {
if (lineSegment.length > 1) {
lineSegments.push(lineSegment);
}
lineSegment = [];
}
}
}
}
if (lineSegment.length > 1) {
lineSegments.push(lineSegment);
}
return lineSegments;
}
const vectorTileFeatureTypes$3 = VectorTileFeature.types;
const EARCUT_MAX_RINGS = 500;
const fillExtrusionDefaultDataDrivenProperties = [
"fill-extrusion-base",
"fill-extrusion-height",
"fill-extrusion-color",
"fill-extrusion-pattern",
"fill-extrusion-flood-light-wall-radius",
"fill-extrusion-line-width",
"fill-extrusion-emissive-strength"
];
const fillExtrusionGroundDataDrivenProperties = [
"fill-extrusion-flood-light-ground-radius"
];
const FACTOR = Math.pow(2, 13);
const TANGENT_CUTOFF = 4;
const NORM = Math.pow(2, 15) - 1;
const QUAD_VERTS = 4;
const QUAD_TRIS = 2;
const TILE_REGIONS = 4;
const HIDDEN_CENTROID = new Point(0, 1);
const HIDDEN_BY_REPLACEMENT = 2147483648;
const ELEVATION_SCALE = 7;
const ELEVATION_OFFSET = 450;
function addVertex$1(vertexArray, x, y, nxRatio, nySign, normalUp, top, e) {
vertexArray.emplaceBack(
// a_pos_normal_ed:
// Encode top and side/up normal using the least significant bits
(x << 1) + top,
(y << 1) + normalUp,
// dxdy is signed, encode quadrant info using the least significant bit
(Math.floor(nxRatio * FACTOR) << 1) + nySign,
// edgedistance (used for wrapping patterns around extrusion sides)
Math.round(e)
);
}
function addWallVertex(vertexArray, joinNormal, inside) {
vertexArray.emplaceBack(
// a_join_normal_inside:
joinNormal.x * EXTENT,
joinNormal.y * EXTENT,
inside ? 1 : 0
);
}
function addGroundVertex(vertexArray, p, q, start, bottom, angle) {
vertexArray.emplaceBack(
p.x,
p.y,
(q.x << 1) + start,
(q.y << 1) + bottom,
angle
);
}
function addGlobeExtVertex(vertexArray, pos, normal) {
const encode = 1 << 14;
vertexArray.emplaceBack(
pos.x,
pos.y,
pos.z,
normal[0] * encode,
normal[1] * encode,
normal[2] * encode
);
}
class FootprintSegment {
constructor() {
this.vertexOffset = 0;
this.vertexCount = 0;
this.indexOffset = 0;
this.indexCount = 0;
}
}
class PartData {
constructor() {
this.centroidXY = new Point(0, 0);
this.vertexArrayOffset = 0;
this.vertexCount = 0;
this.groundVertexArrayOffset = 0;
this.groundVertexCount = 0;
this.flags = 0;
this.footprintSegIdx = -1;
this.footprintSegLen = 0;
this.polygonSegIdx = -1;
this.polygonSegLen = 0;
this.min = new Point(Number.MAX_VALUE, Number.MAX_VALUE);
this.max = new Point(-Number.MAX_VALUE, -Number.MAX_VALUE);
this.height = 0;
this.buildingId = 0;
}
span() {
return new Point(this.max.x - this.min.x, this.max.y - this.min.y);
}
}
class BorderCentroidData {
constructor() {
this.acc = new Point(0, 0);
this.accCount = 0;
this.centroidDataIndex = 0;
}
startRing(data, p) {
if (data.min.x === Number.MAX_VALUE) {
data.min.x = data.max.x = p.x;
data.min.y = data.max.y = p.y;
}
}
appendEdge(data, p, prev) {
assert$1(data.min.x !== Number.MAX_VALUE);
this.accCount++;
this.acc._add(p);
let checkBorders = !!this.borders;
if (p.x < data.min.x) {
data.min.x = p.x;
checkBorders = true;
} else if (p.x > data.max.x) {
data.max.x = p.x;
checkBorders = true;
}
if (p.y < data.min.y) {
data.min.y = p.y;
checkBorders = true;
} else if (p.y > data.max.y) {
data.max.y = p.y;
checkBorders = true;
}
if (((p.x === 0 || p.x === EXTENT) && p.x === prev.x) !== ((p.y === 0 || p.y === EXTENT) && p.y === prev.y)) {
this.processBorderOverlap(p, prev);
}
if (checkBorders) {
this.checkBorderIntersection(p, prev);
}
}
checkBorderIntersection(p, prev) {
if (prev.x < 0 !== p.x < 0) {
this.addBorderIntersection(0, number(prev.y, p.y, (0 - prev.x) / (p.x - prev.x)));
}
if (prev.x > EXTENT !== p.x > EXTENT) {
this.addBorderIntersection(1, number(prev.y, p.y, (EXTENT - prev.x) / (p.x - prev.x)));
}
if (prev.y < 0 !== p.y < 0) {
this.addBorderIntersection(2, number(prev.x, p.x, (0 - prev.y) / (p.y - prev.y)));
}
if (prev.y > EXTENT !== p.y > EXTENT) {
this.addBorderIntersection(3, number(prev.x, p.x, (EXTENT - prev.y) / (p.y - prev.y)));
}
}
addBorderIntersection(index, i) {
if (!this.borders) {
this.borders = [
[Number.MAX_VALUE, -Number.MAX_VALUE],
[Number.MAX_VALUE, -Number.MAX_VALUE],
[Number.MAX_VALUE, -Number.MAX_VALUE],
[Number.MAX_VALUE, -Number.MAX_VALUE]
];
}
const b = this.borders[index];
if (i < b[0]) b[0] = i;
if (i > b[1]) b[1] = i;
}
processBorderOverlap(p, prev) {
if (p.x === prev.x) {
if (p.y === prev.y) return;
const index = p.x === 0 ? 0 : 1;
this.addBorderIntersection(index, prev.y);
this.addBorderIntersection(index, p.y);
} else {
assert$1(p.y === prev.y);
const index = p.y === 0 ? 2 : 3;
this.addBorderIntersection(index, prev.x);
this.addBorderIntersection(index, p.x);
}
}
centroid() {
if (this.accCount === 0) {
return new Point(0, 0);
}
return new Point(
Math.floor(Math.max(0, this.acc.x) / this.accCount),
Math.floor(Math.max(0, this.acc.y) / this.accCount)
);
}
intersectsCount() {
if (!this.borders) {
return 0;
}
return this.borders.reduce((acc, p) => acc + +(p[0] !== Number.MAX_VALUE), 0);
}
}
function concavity(a, b) {
return a.x * b.y - a.y * b.x < 0 ? -1 : 1;
}
function tanAngleClamped(angle) {
return Math.min(TANGENT_CUTOFF, Math.max(-TANGENT_CUTOFF, Math.tan(angle))) / TANGENT_CUTOFF * NORM;
}
function getAngularOffsetFactor(na, nb) {
const nm = na.add(nb)._unit();
const cosHalfAngle = clamp(na.x * nm.x + na.y * nm.y, -1, 1);
const factor = tanAngleClamped(Math.acos(cosHalfAngle)) * concavity(na, nb);
return factor;
}
const borderCheck = [
(a) => {
return a.x < 0;
},
// left
(a) => {
return a.x > EXTENT;
},
// right
(a) => {
return a.y < 0;
},
// top
(a) => {
return a.y > EXTENT;
}
// bottom
];
function getTileRegions(pa, pb, na, maxRadius) {
const regions = [4];
if (maxRadius === 0) return regions;
na._mult(maxRadius);
const c = pa.sub(na);
const d = pb.sub(na);
const points = [pa, pb, c, d];
for (let i = 0; i < TILE_REGIONS; i++) {
for (const point of points) {
if (borderCheck[i](point)) {
regions.push(i);
break;
}
}
}
return regions;
}
class GroundEffect {
constructor(options) {
this.groundRadiusArray = null;
this.groundRadiusBuffer = null;
this.vertexArray = new StructArrayLayout5i10();
this.indexArray = new StructArrayLayout3ui6();
const filtered = (property) => {
return fillExtrusionGroundDataDrivenProperties.includes(property);
};
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut }, filtered);
this._segments = new SegmentVector();
this.hiddenByLandmarkVertexArray = new StructArrayLayout1ub1();
this._segmentToGroundQuads = {};
this._segmentToGroundQuads[0] = [];
this._segmentToRegionTriCounts = {};
this._segmentToRegionTriCounts[0] = [0, 0, 0, 0, 0];
this.regionSegments = {};
this.regionSegments[4] = new SegmentVector();
}
getDefaultSegment() {
return this.regionSegments[4];
}
hasData() {
return this.vertexArray.length !== 0;
}
addData(polyline, bounds, maxRadius, roundedEdges = false) {
const n = polyline.length;
if (n > 2) {
let sid = Math.max(0, this._segments.get().length - 1);
const numNewVerts = n * 4;
const numExistingVerts = this.vertexArray.length;
const numExistingTris = this._segmentToGroundQuads[sid].length * QUAD_TRIS;
const segment = this._segments._prepareSegment(numNewVerts, numExistingVerts, numExistingTris);
const newSegmentAdded = sid !== this._segments.get().length - 1;
if (newSegmentAdded) {
sid++;
this._segmentToGroundQuads[sid] = [];
this._segmentToRegionTriCounts[sid] = [0, 0, 0, 0, 0];
}
let prevFactor;
{
const pa = polyline[n - 1];
const pb = polyline[0];
const pc = polyline[1];
const na = pb.sub(pa)._perp()._unit();
const nb = pc.sub(pb)._perp()._unit();
prevFactor = getAngularOffsetFactor(na, nb);
}
for (let i = 0; i < n; i++) {
const j = i === n - 1 ? 0 : i + 1;
const k = j === n - 1 ? 0 : j + 1;
const pa = polyline[i];
const pb = polyline[j];
const pc = polyline[k];
const na = pb.sub(pa)._perp()._unit();
const nb = pc.sub(pb)._perp()._unit();
const factor = getAngularOffsetFactor(na, nb);
const a0 = prevFactor;
const a1 = factor;
if (isEdgeOutsideBounds(pa, pb, bounds) || roundedEdges && pointOutsideBounds$1(pa, bounds) && pointOutsideBounds$1(pb, bounds)) {
prevFactor = factor;
continue;
}
const idx = segment.vertexLength;
addGroundVertex(this.vertexArray, pa, pb, 1, 1, a0);
addGroundVertex(this.vertexArray, pa, pb, 1, 0, a0);
addGroundVertex(this.vertexArray, pa, pb, 0, 1, a1);
addGroundVertex(this.vertexArray, pa, pb, 0, 0, a1);
segment.vertexLength += QUAD_VERTS;
const regions = getTileRegions(pa, pb, na, maxRadius);
for (const rid of regions) {
this._segmentToGroundQuads[sid].push({
id: idx,
region: rid
});
this._segmentToRegionTriCounts[sid][rid] += QUAD_TRIS;
segment.primitiveLength += QUAD_TRIS;
}
prevFactor = factor;
}
}
}
prepareBorderSegments() {
if (!this.hasData()) return;
assert$1(this._segments && this._segmentToGroundQuads && this._segmentToRegionTriCounts);
assert$1(!this.indexArray.length);
const segments = this._segments.get();
const numSegments = segments.length;
for (let i = 0; i < numSegments; i++) {
const groundQuads = this._segmentToGroundQuads[i];
groundQuads.sort((a, b) => {
return a.region - b.region;
});
}
for (let i = 0; i < numSegments; i++) {
const groundQuads = this._segmentToGroundQuads[i];
const segment = segments[i];
const regionTriCounts = this._segmentToRegionTriCounts[i];
const totalTriCount = regionTriCounts.reduce((acc, a) => {
return acc + a;
}, 0);
assert$1(segment.primitiveLength === totalTriCount);
let regionTriCountOffset = 0;
for (let k = 0; k <= TILE_REGIONS; k++) {
const triCount = regionTriCounts[k];
assert$1(triCount % QUAD_TRIS === 0);
if (triCount !== 0) {
let segmentVector = this.regionSegments[k];
if (!segmentVector) {
segmentVector = this.regionSegments[k] = new SegmentVector();
}
const nSegment = {
vertexOffset: segment.vertexOffset,
primitiveOffset: segment.primitiveOffset + regionTriCountOffset,
vertexLength: segment.vertexLength,
primitiveLength: triCount
};
segmentVector.get().push(nSegment);
}
regionTriCountOffset += triCount;
}
for (let j = 0; j < groundQuads.length; j++) {
const groundQuad = groundQuads[j];
const idx = groundQuad.id;
this.indexArray.emplaceBack(idx, idx + 1, idx + 3);
this.indexArray.emplaceBack(idx, idx + 3, idx + 2);
}
}
this._segmentToGroundQuads = null;
this._segmentToRegionTriCounts = null;
this._segments.destroy();
this._segments = null;
}
addPaintPropertiesData(feature, index, imagePositions, availableImages, canonical, brightness, worldview) {
if (!this.hasData()) return;
this.programConfigurations.populatePaintArrays(this.vertexArray.length, feature, index, imagePositions, availableImages, canonical, brightness, void 0, worldview);
}
upload(context) {
if (!this.hasData()) return;
this.vertexBuffer = context.createVertexBuffer(this.vertexArray, fillExtrusionGroundAttributes.members);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
if (this.groundRadiusArray != null) {
this.groundRadiusBuffer = context.createVertexBuffer(this.groundRadiusArray, fillExtrusionGroundRadiusAttributes.members);
}
}
uploadPaintProperties(context) {
if (!this.hasData()) return;
this.programConfigurations.upload(context);
}
update(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview) {
if (!this.hasData()) return;
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview);
}
updateHiddenByLandmark(data) {
const hide = !!(data.flags & HIDDEN_BY_REPLACEMENT);
this.updateHiddenByLandmarkRange(data.groundVertexArrayOffset, data.groundVertexCount, hide);
}
updateHiddenByLandmarkRange(groundVertexArrayOffset, groundVertexCount, hide) {
if (!this.hasData()) return;
const offset = groundVertexArrayOffset;
const vertexArrayBounds = groundVertexCount + groundVertexArrayOffset;
assert$1(vertexArrayBounds <= this.hiddenByLandmarkVertexArray.length);
assert$1(this.hiddenByLandmarkVertexArray.length === this.vertexArray.length);
if (groundVertexCount === 0) return;
for (let i = offset; i < vertexArrayBounds; ++i) {
this.hiddenByLandmarkVertexArray.emplace(i, hide ? 1 : 0);
}
this._needsHiddenByLandmarkUpdate = true;
}
uploadHiddenByLandmark(context) {
if (!this.hasData() || !this._needsHiddenByLandmarkUpdate) {
return;
}
if (!this.hiddenByLandmarkVertexBuffer && this.hiddenByLandmarkVertexArray.length > 0) {
this.hiddenByLandmarkVertexBuffer = context.createVertexBuffer(this.hiddenByLandmarkVertexArray, hiddenByLandmarkAttributes.members, true);
} else if (this.hiddenByLandmarkVertexBuffer) {
this.hiddenByLandmarkVertexBuffer.updateData(this.hiddenByLandmarkVertexArray);
}
this._needsHiddenByLandmarkUpdate = false;
}
destroy() {
if (!this.vertexBuffer) return;
this.vertexBuffer.destroy();
this.indexBuffer.destroy();
if (this.hiddenByLandmarkVertexBuffer) {
this.hiddenByLandmarkVertexBuffer.destroy();
}
if (this.groundRadiusBuffer) {
this.groundRadiusBuffer.destroy();
}
if (this._segments) this._segments.destroy();
this.programConfigurations.destroy();
for (let i = 0; i <= TILE_REGIONS; i++) {
const segments = this.regionSegments[i];
if (segments) {
segments.destroy();
}
}
}
}
class FillExtrusionBucket {
constructor(options) {
this.zoom = options.zoom;
this.canonical = options.canonical;
this.overscaling = options.overscaling;
this.layers = options.layers;
this.pixelRatio = options.pixelRatio;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.hasPattern = false;
this.edgeRadius = 0;
this.projection = options.projection;
this.activeReplacements = [];
this.replacementUpdateTime = 0;
this.centroidData = [];
this.footprintIndices = new StructArrayLayout3ui6();
this.footprintVertices = new StructArrayLayout2i4();
this.footprintSegments = [];
this.layoutVertexArray = new StructArrayLayout4i8();
this.centroidVertexArray = new FillExtrusionCentroidArray();
this.wallVertexArray = new FillExtrusionWallArray();
this.indexArray = new StructArrayLayout3ui6();
const filtered = (property) => {
return fillExtrusionDefaultDataDrivenProperties.includes(property);
};
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut }, filtered);
this.segments = new SegmentVector();
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.groundEffect = new GroundEffect(options);
this.maxHeight = 0;
this.partLookup = {};
this.triangleSubSegments = [];
this.polygonSegments = [];
this.worldview = options.worldview;
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
this.features = [];
this.hasPattern = hasPattern("fill-extrusion", this.layers, this.pixelRatio, options);
this.featuresOnBorder = [];
this.borderFeatureIndices = [[], [], [], []];
this.borderDoneWithNeighborZ = [-1, -1, -1, -1];
this.selfDEMTileTimestamp = Number.MAX_VALUE;
this.borderDEMTileTimestamp = [Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE];
this.tileToMeter = tileToMeter(canonical);
this.edgeRadius = this.layers[0].layout.get("fill-extrusion-edge-radius") / this.tileToMeter;
this.wallMode = this.layers[0].paint.get("fill-extrusion-line-width").constantOr(1) !== 0;
for (const { feature, id, index, sourceLayerIndex } of features) {
const needGeometry = this.layers[0]._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const bucketFeature = {
id,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
properties: feature.properties,
type: feature.type,
patterns: {}
};
const vertexArrayOffset = this.layoutVertexArray.length;
const featureIsPolygon = vectorTileFeatureTypes$3[bucketFeature.type] === "Polygon";
if (this.hasPattern) {
this.features.push({ featureId: feature.id, feature: addPatternDependencies("fill-extrusion", this.layers, bucketFeature, this.zoom, this.pixelRatio, options) });
} else {
if (this.wallMode) {
for (const polygon of bucketFeature.geometry) {
for (const line of dropBufferConnectionLines(polygon, featureIsPolygon)) {
this.addFeature(feature.id, bucketFeature, [line], index, canonical, {}, options.availableImages, tileTransform, options.brightness);
}
}
} else {
this.addFeature(feature.id, bucketFeature, bucketFeature.geometry, index, canonical, {}, options.availableImages, tileTransform, options.brightness);
}
}
options.featureIndex.insert(feature, bucketFeature.geometry, index, sourceLayerIndex, this.index, vertexArrayOffset);
}
this.sortBorders();
if (this.projection.name === "mercator") {
this.splitToSubtiles();
}
this.groundEffect.prepareBorderSegments();
this.polygonSegments.length = 0;
}
addFeatures(options, canonical, imagePositions, availableImages, tileTransform, brightness) {
for (const { featureId, feature } of this.features) {
const featureIsPolygon = vectorTileFeatureTypes$3[feature.type] === "Polygon";
const { geometry } = feature;
if (this.wallMode) {
for (const polygon of geometry) {
for (const line of dropBufferConnectionLines(polygon, featureIsPolygon)) {
this.addFeature(featureId, feature, [line], feature.index, canonical, imagePositions, availableImages, tileTransform, brightness);
}
}
} else {
this.addFeature(featureId, feature, geometry, feature.index, canonical, imagePositions, availableImages, tileTransform, brightness);
}
}
this.sortBorders();
if (this.projection.name === "mercator") {
this.splitToSubtiles();
}
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness) {
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, this.worldview);
this.groundEffect.update(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, this.worldview);
}
isEmpty() {
return this.layoutVertexArray.length === 0;
}
uploadPending() {
return !this.uploaded || this.programConfigurations.needsUpload || this.groundEffect.programConfigurations.needsUpload;
}
upload(context) {
if (!this.uploaded) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, members$5);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
this.wallVertexBuffer = context.createVertexBuffer(this.wallVertexArray, wallAttributes.members);
if (this.layoutVertexExtArray) {
this.layoutVertexExtBuffer = context.createVertexBuffer(this.layoutVertexExtArray, fillExtrusionAttributesExt.members, true);
}
this.groundEffect.upload(context);
}
this.groundEffect.uploadPaintProperties(context);
this.programConfigurations.upload(context);
this.uploaded = true;
}
uploadCentroid(context) {
this.groundEffect.uploadHiddenByLandmark(context);
if (!this.needsCentroidUpdate) {
return;
}
if (!this.centroidVertexBuffer && this.centroidVertexArray.length > 0) {
this.centroidVertexBuffer = context.createVertexBuffer(this.centroidVertexArray, centroidAttributes.members, true);
} else if (this.centroidVertexBuffer) {
this.centroidVertexBuffer.updateData(this.centroidVertexArray);
}
this.needsCentroidUpdate = false;
}
destroy() {
if (!this.layoutVertexBuffer) return;
this.layoutVertexBuffer.destroy();
if (this.centroidVertexBuffer) {
this.centroidVertexBuffer.destroy();
}
if (this.layoutVertexExtBuffer) {
this.layoutVertexExtBuffer.destroy();
}
this.groundEffect.destroy();
this.indexBuffer.destroy();
this.programConfigurations.destroy();
this.segments.destroy();
}
addFeature(featureId, feature, geometry, index, canonical, imagePositions, availableImages, tileTransform, brightness) {
const floodLightRadius = this.layers[0].paint.get("fill-extrusion-flood-light-ground-radius").evaluate(feature, {});
const maxRadius = floodLightRadius / this.tileToMeter;
const tileBounds = [new Point(0, 0), new Point(EXTENT, EXTENT)];
const projection = tileTransform.projection;
const isGlobe = projection.name === "globe";
const isPolygon = this.wallMode || vectorTileFeatureTypes$3[feature.type] === "Polygon";
const borderCentroidData = new BorderCentroidData();
borderCentroidData.centroidDataIndex = this.centroidData.length;
const centroid = new PartData();
centroid.buildingId = featureId;
if (feature.properties && feature.properties.hasOwnProperty("building_id")) {
centroid.buildingId = feature.properties["building_id"];
}
const base = this.layers[0].paint.get("fill-extrusion-base").evaluate(feature, {}, canonical);
const onGround = base <= 0;
const height = this.layers[0].paint.get("fill-extrusion-height").evaluate(feature, {}, canonical);
centroid.height = height;
centroid.vertexArrayOffset = this.layoutVertexArray.length;
centroid.groundVertexArrayOffset = this.groundEffect.vertexArray.length;
if (isGlobe && !this.layoutVertexExtArray) {
this.layoutVertexExtArray = new StructArrayLayout6i12();
}
let wallGeometry;
if (this.wallMode) {
if (isGlobe) {
warnOnce("Non zero fill-extrusion-line-width is not yet supported on globe.");
return;
}
if (geometry.length !== 1) {
assert$1(false);
return;
}
wallGeometry = createLineWallGeometry(geometry[0]);
geometry = [wallGeometry.geometry];
}
const isPointOnInnerWall = (index2, polygon) => {
return index2 < (polygon.length - 1) / 2 || index2 === polygon.length - 1;
};
const polygons = this.wallMode ? [geometry] : classifyRings(geometry, EARCUT_MAX_RINGS);
for (let i = polygons.length - 1; i >= 0; i--) {
const polygon = polygons[i];
if (polygon.length === 0 || isEntirelyOutside(polygon[0])) {
polygons.splice(i, 1);
}
}
let clippedPolygons;
if (isGlobe) {
clippedPolygons = resampleFillExtrusionPolygonsForGlobe(polygons, tileBounds, canonical);
} else {
clippedPolygons = [];
for (const polygon of polygons) {
clippedPolygons.push({ polygon, bounds: tileBounds });
}
}
const edgeRadius = isPolygon ? this.edgeRadius : 0;
const optimiseGround = edgeRadius > 0 && this.zoom < 17;
const isDuplicate = (coords, a) => {
if (coords.length === 0) return false;
const b = coords[coords.length - 1];
return a.x === b.x && a.y === b.y;
};
for (const { polygon, bounds } of clippedPolygons) {
let topIndex = 0;
let numVertices = 0;
for (const ring of polygon) {
if (isPolygon && !ring[0].equals(ring[ring.length - 1])) ring.push(ring[0]);
numVertices += isPolygon ? ring.length - 1 : ring.length;
}
const segment = this.segments.prepareSegment((isPolygon ? 5 : 4) * numVertices, this.layoutVertexArray, this.indexArray);
if (centroid.footprintSegIdx < 0) {
centroid.footprintSegIdx = this.footprintSegments.length;
}
if (centroid.polygonSegIdx < 0) {
centroid.polygonSegIdx = this.polygonSegments.length;
}
const polygonSeg = { triangleArrayOffset: this.indexArray.length, triangleCount: 0, triangleSegIdx: this.segments.segments.length - 1 };
const fpSegment = new FootprintSegment();
fpSegment.vertexOffset = this.footprintVertices.length;
fpSegment.indexOffset = this.footprintIndices.length * 3;
fpSegment.ringIndices = [];
if (isPolygon) {
const flattened = [];
const holeIndices = [];
topIndex = segment.vertexLength;
for (let r = 0; r < polygon.length; r++) {
const ring = polygon[r];
if (ring.length && r !== 0) {
holeIndices.push(flattened.length / 2);
}
const groundPolyline = [];
let na;
let nb;
{
const p0 = ring[0];
const p1 = ring[1];
na = p1.sub(p0)._perp()._unit();
}
fpSegment.ringIndices.push(ring.length - 1);
for (let i = 1; i < ring.length; i++) {
const p1 = ring[i];
const p2 = ring[i === ring.length - 1 ? 1 : i + 1];
const q = p1.clone();
if (edgeRadius) {
nb = p2.sub(p1)._perp()._unit();
const nm = na.add(nb)._unit();
const cosHalfAngle = na.x * nm.x + na.y * nm.y;
const offset = edgeRadius * Math.min(4, 1 / cosHalfAngle);
q.x += offset * nm.x;
q.y += offset * nm.y;
q.x = Math.round(q.x);
q.y = Math.round(q.y);
na = nb;
}
if (onGround && (edgeRadius === 0 || optimiseGround) && !isDuplicate(groundPolyline, q)) {
groundPolyline.push(q);
}
addVertex$1(this.layoutVertexArray, q.x, q.y, 0, 0, 1, 1, 0);
if (this.wallMode) {
const isInside = isPointOnInnerWall(i, ring);
const joinNormal = wallGeometry.joinNormals[i];
addWallVertex(this.wallVertexArray, joinNormal, !isInside);
}
segment.vertexLength++;
this.footprintVertices.emplaceBack(p1.x, p1.y);
flattened.push(p1.x, p1.y);
if (isGlobe) {
const array = this.layoutVertexExtArray;
const projectedP = projection.projectTilePoint(q.x, q.y, canonical);
const n = projection.upVector(canonical, q.x, q.y);
addGlobeExtVertex(array, projectedP, n);
}
}
if (onGround && (edgeRadius === 0 || optimiseGround)) {
if (groundPolyline.length !== 0 && isDuplicate(groundPolyline, groundPolyline[0])) {
groundPolyline.pop();
}
this.groundEffect.addData(groundPolyline, bounds, maxRadius);
}
}
const indices = this.wallMode ? wallGeometry.indices : earcut(flattened, holeIndices);
assert$1(indices.length % 3 === 0);
for (let j = 0; j < indices.length; j += 3) {
this.footprintIndices.emplaceBack(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
fpSegment.vertexOffset + indices[j + 0],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
fpSegment.vertexOffset + indices[j + 1],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
fpSegment.vertexOffset + indices[j + 2]
);
this.indexArray.emplaceBack(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
topIndex + indices[j],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
topIndex + indices[j + 2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
topIndex + indices[j + 1]
);
segment.primitiveLength++;
}
fpSegment.indexCount += indices.length;
fpSegment.vertexCount += this.footprintVertices.length - fpSegment.vertexOffset;
}
for (let r = 0; r < polygon.length; r++) {
const ring = polygon[r];
borderCentroidData.startRing(centroid, ring[0]);
let isPrevCornerConcave = ring.length > 4 && isAOConcaveAngle(ring[ring.length - 2], ring[0], ring[1]);
let offsetPrev = edgeRadius ? getRoundedEdgeOffset(ring[ring.length - 2], ring[0], ring[1], edgeRadius) : 0;
const groundPolyline = [];
let kFirst;
let na;
let nb;
{
const p0 = ring[0];
const p1 = ring[1];
na = p1.sub(p0)._perp()._unit();
}
let cap = true;
for (let i = 1, edgeDistance = 0; i < ring.length; i++) {
let p0 = ring[i - 1];
let p1 = ring[i];
const p2 = ring[i === ring.length - 1 ? 1 : i + 1];
borderCentroidData.appendEdge(centroid, p1, p0);
if (isEdgeOutsideBounds(p1, p0, bounds)) {
if (edgeRadius) {
na = p2.sub(p1)._perp()._unit();
cap = !cap;
}
continue;
}
const d = p1.sub(p0)._perp();
const nxRatio = d.x / (Math.abs(d.x) + Math.abs(d.y));
const nySign = d.y > 0 ? 1 : 0;
const dist = p0.dist(p1);
if (edgeDistance + dist > 32768) edgeDistance = 0;
if (edgeRadius) {
nb = p2.sub(p1)._perp()._unit();
const cosHalfAngle = getCosHalfAngle(na, nb);
let offsetNext = _getRoundedEdgeOffset(p0, p1, p2, cosHalfAngle, edgeRadius);
if (isNaN(offsetNext)) offsetNext = 0;
const nEdge = p1.sub(p0)._unit();
p0 = p0.add(nEdge.mult(offsetPrev))._round();
p1 = p1.add(nEdge.mult(-offsetNext))._round();
offsetPrev = offsetNext;
na = nb;
if (onGround && this.zoom >= 17) {
if (!isDuplicate(groundPolyline, p0)) groundPolyline.push(p0);
if (!isDuplicate(groundPolyline, p1)) groundPolyline.push(p1);
}
}
const k = segment.vertexLength;
const isConcaveCorner = ring.length > 4 && isAOConcaveAngle(p0, p1, p2);
let encodedEdgeDistance = encodeAOToEdgeDistance(edgeDistance, isPrevCornerConcave, cap);
addVertex$1(this.layoutVertexArray, p0.x, p0.y, nxRatio, nySign, 0, 0, encodedEdgeDistance);
addVertex$1(this.layoutVertexArray, p0.x, p0.y, nxRatio, nySign, 0, 1, encodedEdgeDistance);
if (this.wallMode) {
const isInside = isPointOnInnerWall(i - 1, ring);
const joinNormal = wallGeometry.joinNormals[i - 1];
addWallVertex(this.wallVertexArray, joinNormal, isInside);
addWallVertex(this.wallVertexArray, joinNormal, isInside);
}
edgeDistance += dist;
encodedEdgeDistance = encodeAOToEdgeDistance(edgeDistance, isConcaveCorner, !cap);
isPrevCornerConcave = isConcaveCorner;
addVertex$1(this.layoutVertexArray, p1.x, p1.y, nxRatio, nySign, 0, 0, encodedEdgeDistance);
addVertex$1(this.layoutVertexArray, p1.x, p1.y, nxRatio, nySign, 0, 1, encodedEdgeDistance);
if (this.wallMode) {
const isInside = isPointOnInnerWall(i, ring);
const joinNormal = wallGeometry.joinNormals[i];
addWallVertex(this.wallVertexArray, joinNormal, isInside);
addWallVertex(this.wallVertexArray, joinNormal, isInside);
}
segment.vertexLength += 4;
this.indexArray.emplaceBack(k + 0, k + 1, k + 2);
this.indexArray.emplaceBack(k + 1, k + 3, k + 2);
segment.primitiveLength += 2;
if (edgeRadius) {
const t0 = topIndex + (i === 1 ? ring.length - 2 : i - 2);
const t1 = i === 1 ? topIndex : t0 + 1;
this.indexArray.emplaceBack(k + 1, t0, k + 3);
this.indexArray.emplaceBack(t0, t1, k + 3);
segment.primitiveLength += 2;
if (kFirst === void 0) {
kFirst = k;
}
if (!isEdgeOutsideBounds(p2, ring[i], bounds)) {
const l = i === ring.length - 1 ? kFirst : segment.vertexLength;
this.indexArray.emplaceBack(k + 2, k + 3, l);
this.indexArray.emplaceBack(k + 3, l + 1, l);
this.indexArray.emplaceBack(k + 3, t1, l + 1);
segment.primitiveLength += 3;
}
cap = !cap;
}
if (isGlobe) {
const array = this.layoutVertexExtArray;
const projectedP0 = projection.projectTilePoint(p0.x, p0.y, canonical);
const projectedP1 = projection.projectTilePoint(p1.x, p1.y, canonical);
const n0 = projection.upVector(canonical, p0.x, p0.y);
const n1 = projection.upVector(canonical, p1.x, p1.y);
addGlobeExtVertex(array, projectedP0, n0);
addGlobeExtVertex(array, projectedP0, n0);
addGlobeExtVertex(array, projectedP1, n1);
addGlobeExtVertex(array, projectedP1, n1);
}
}
if (isPolygon) topIndex += ring.length - 1;
if (onGround && edgeRadius && this.zoom >= 17) {
if (groundPolyline.length !== 0 && isDuplicate(groundPolyline, groundPolyline[0])) {
groundPolyline.pop();
}
this.groundEffect.addData(groundPolyline, bounds, maxRadius, edgeRadius > 0);
}
}
this.footprintSegments.push(fpSegment);
polygonSeg.triangleCount = this.indexArray.length - polygonSeg.triangleArrayOffset;
this.polygonSegments.push(polygonSeg);
++centroid.footprintSegLen;
++centroid.polygonSegLen;
}
assert$1(!isGlobe || this.layoutVertexExtArray && this.layoutVertexExtArray.length === this.layoutVertexArray.length);
centroid.vertexCount = this.layoutVertexArray.length - centroid.vertexArrayOffset;
centroid.groundVertexCount = this.groundEffect.vertexArray.length - centroid.groundVertexArrayOffset;
if (centroid.vertexCount === 0) {
return;
}
centroid.centroidXY = borderCentroidData.borders ? HIDDEN_CENTROID : this.encodeCentroid(borderCentroidData, centroid);
this.centroidData.push(centroid);
if (borderCentroidData.borders) {
assert$1(borderCentroidData.centroidDataIndex === this.centroidData.length - 1);
this.featuresOnBorder.push(borderCentroidData);
const borderIndex = this.featuresOnBorder.length - 1;
for (let i = 0; i < borderCentroidData.borders.length; i++) {
if (borderCentroidData.borders[i][0] !== Number.MAX_VALUE) {
this.borderFeatureIndices[i].push(borderIndex);
}
}
}
this.programConfigurations.populatePaintArrays(this.layoutVertexArray.length, feature, index, imagePositions, availableImages, canonical, brightness, void 0, this.worldview);
this.groundEffect.addPaintPropertiesData(feature, index, imagePositions, availableImages, canonical, brightness, this.worldview);
this.maxHeight = Math.max(this.maxHeight, height);
}
sortBorders() {
for (let i = 0; i < this.borderFeatureIndices.length; i++) {
const borders = this.borderFeatureIndices[i];
borders.sort((a, b) => this.featuresOnBorder[a].borders[i][0] - this.featuresOnBorder[b].borders[i][0]);
}
}
splitToSubtiles() {
const segmentedFeatures = [];
for (let centroidIdx = 0; centroidIdx < this.centroidData.length; centroidIdx++) {
const part = this.centroidData[centroidIdx];
const right = +(part.min.x + part.max.x > EXTENT);
const bottom = +(part.min.y + part.max.y > EXTENT);
const subtile = bottom * 2 + (right ^ bottom);
for (let i = 0; i < part.polygonSegLen; i++) {
const polySegIdx = part.polygonSegIdx + i;
segmentedFeatures.push({ centroidIdx, subtile, polygonSegmentIdx: polySegIdx, triangleSegmentIdx: this.polygonSegments[polySegIdx].triangleSegIdx });
}
}
const sortedTriangles = new StructArrayLayout3ui6();
segmentedFeatures.sort((a, b) => a.triangleSegmentIdx === b.triangleSegmentIdx ? a.subtile - b.subtile : a.triangleSegmentIdx - b.triangleSegmentIdx);
let segmentIdx = 0;
let segmentBeginIndex = 0;
let segmentEndIndex = 0;
for (const segmentedFeature of segmentedFeatures) {
if (segmentedFeature.triangleSegmentIdx !== segmentIdx) {
break;
}
segmentEndIndex++;
}
const segmentedFeaturesEndIndex = segmentedFeatures.length;
while (segmentBeginIndex !== segmentedFeatures.length) {
segmentIdx = segmentedFeatures[segmentBeginIndex].triangleSegmentIdx;
let subTileIdx = 0;
let featuresBeginIndex = segmentBeginIndex;
let featuresEndIndex = segmentBeginIndex;
for (let seg = featuresBeginIndex; seg < segmentEndIndex; seg++) {
if (segmentedFeatures[seg].subtile !== subTileIdx) {
break;
}
featuresEndIndex++;
}
while (featuresBeginIndex !== segmentEndIndex) {
const featuresBegin = segmentedFeatures[featuresBeginIndex];
subTileIdx = featuresBegin.subtile;
const subtileMin = this.centroidData[featuresBegin.centroidIdx].min.clone();
const subtileMax = this.centroidData[featuresBegin.centroidIdx].max.clone();
const segment = {
vertexOffset: this.segments.segments[segmentIdx].vertexOffset,
primitiveOffset: sortedTriangles.length,
vertexLength: this.segments.segments[segmentIdx].vertexLength,
primitiveLength: 0,
sortKey: void 0,
vaos: {}
};
for (let featureIdx = featuresBeginIndex; featureIdx < featuresEndIndex; featureIdx++) {
const feature = segmentedFeatures[featureIdx];
const data = this.polygonSegments[feature.polygonSegmentIdx];
const centroidMin = this.centroidData[feature.centroidIdx].min;
const centroidMax = this.centroidData[feature.centroidIdx].max;
const iArray = this.indexArray.uint16;
for (let i = data.triangleArrayOffset; i < data.triangleArrayOffset + data.triangleCount; i++) {
sortedTriangles.emplaceBack(iArray[i * 3], iArray[i * 3 + 1], iArray[i * 3 + 2]);
}
segment.primitiveLength += data.triangleCount;
subtileMin.x = Math.min(subtileMin.x, centroidMin.x);
subtileMin.y = Math.min(subtileMin.y, centroidMin.y);
subtileMax.x = Math.max(subtileMax.x, centroidMax.x);
subtileMax.y = Math.max(subtileMax.y, centroidMax.y);
}
if (segment.primitiveLength > 0) {
this.triangleSubSegments.push({ segment, min: subtileMin, max: subtileMax });
}
featuresBeginIndex = featuresEndIndex;
for (let seg = featuresBeginIndex; seg < segmentEndIndex; seg++) {
if (segmentedFeatures[seg].subtile !== segmentedFeatures[featuresBeginIndex].subtile) {
break;
}
featuresEndIndex++;
}
}
segmentBeginIndex = segmentEndIndex;
for (let seg = segmentBeginIndex; seg < segmentedFeaturesEndIndex; seg++) {
if (segmentedFeatures[seg].triangleSegmentIdx !== segmentedFeatures[segmentBeginIndex].triangleSegmentIdx) {
break;
}
segmentEndIndex++;
}
}
sortedTriangles._trim();
this.indexArray = sortedTriangles;
}
getVisibleSegments(renderId, elevation, frustum) {
const outSegments = new SegmentVector();
if (this.wallMode) {
for (const subSegment of this.triangleSubSegments) {
outSegments.segments.push(subSegment.segment);
}
return outSegments;
}
let minZ = 0;
let maxZ = 0;
const tiles = 1 << renderId.canonical.z;
if (elevation) {
const minmax = elevation.getMinMaxForTile(renderId);
if (minmax) {
minZ = minmax.min;
maxZ = minmax.max;
}
}
maxZ += this.maxHeight;
const id = renderId.toUnwrapped();
let activeSegment;
const tileMin = [id.canonical.x / tiles + id.wrap, id.canonical.y / tiles];
const tileMax = [(id.canonical.x + 1) / tiles + id.wrap, (id.canonical.y + 1) / tiles];
const mix = (a, b, c) => {
return [a[0] * (1 - c[0]) + b[0] * c[0], a[1] * (1 - c[1]) + b[1] * c[1]];
};
const fracMin = [];
const fracMax = [];
for (const subSegment of this.triangleSubSegments) {
fracMin[0] = subSegment.min.x / EXTENT;
fracMin[1] = subSegment.min.y / EXTENT;
fracMax[0] = subSegment.max.x / EXTENT;
fracMax[1] = subSegment.max.y / EXTENT;
const aabbMin = mix(tileMin, tileMax, fracMin);
const aabbMax = mix(tileMin, tileMax, fracMax);
const aabb = new Aabb([aabbMin[0], aabbMin[1], minZ], [aabbMax[0], aabbMax[1], maxZ]);
if (aabb.intersectsPrecise(frustum) === 0) {
if (activeSegment) {
outSegments.segments.push(activeSegment);
activeSegment = void 0;
}
continue;
}
const renderSegment = subSegment.segment;
if (activeSegment && activeSegment.vertexOffset !== renderSegment.vertexOffset) {
outSegments.segments.push(activeSegment);
activeSegment = void 0;
}
if (!activeSegment) {
activeSegment = {
vertexOffset: renderSegment.vertexOffset,
primitiveLength: renderSegment.primitiveLength,
vertexLength: renderSegment.vertexLength,
primitiveOffset: renderSegment.primitiveOffset,
sortKey: void 0,
vaos: {}
};
} else {
activeSegment.vertexLength += renderSegment.vertexLength;
activeSegment.primitiveLength += renderSegment.primitiveLength;
}
}
if (activeSegment) {
outSegments.segments.push(activeSegment);
}
return outSegments;
}
// Encoded centroid x and y:
// x y
// ---------------------------------------------
// 0 0 Default, no flat roof.
// 0 1 Hide, used to hide parts of buildings on border while expecting the other side to get loaded
// >0 0 Elevation encoded to uint16 word
// >0 >0 Encoded centroid position and x & y span
encodeCentroid(borderCentroidData, data) {
const c = borderCentroidData.centroid();
const span = data.span();
const spanX = Math.min(7, Math.round(span.x * this.tileToMeter / 10));
const spanY = Math.min(7, Math.round(span.y * this.tileToMeter / 10));
return new Point(clamp(c.x, 1, EXTENT - 1) << 3 | spanX, clamp(c.y, 1, EXTENT - 1) << 3 | spanY);
}
// Border centroid data is unreliable for elevating parts split on tile borders.
// It is used only for synchronous lowering of splits as the centroid (not the size information in split parts) is consistent.
encodeBorderCentroid(borderCentroidData) {
assert$1(borderCentroidData.borders);
if (!borderCentroidData.borders) return new Point(0, 0);
const b = borderCentroidData.borders;
const notOnBorder = Number.MAX_VALUE;
const span = 6;
assert$1(borderCentroidData.intersectsCount() === 1);
if (b[0][0] !== notOnBorder || b[1][0] !== notOnBorder) {
const x = (b[0][0] !== notOnBorder ? 0 : 8191 << 3) | span;
const index = b[0][0] !== notOnBorder ? 0 : 1;
return new Point(x, ((b[index][0] + b[index][1]) / 2 | 0) << 3 | span);
} else {
assert$1(b[2][0] !== notOnBorder || b[3][0] !== notOnBorder);
const y = (b[2][0] !== notOnBorder ? 0 : 8191 << 3) | span;
const index = b[2][0] !== notOnBorder ? 2 : 3;
return new Point(((b[index][0] + b[index][1]) / 2 | 0) << 3 | span, y);
}
}
showCentroid(borderCentroidData) {
const c = this.centroidData[borderCentroidData.centroidDataIndex];
c.flags &= ~HIDDEN_BY_REPLACEMENT;
c.centroidXY.x = 0;
c.centroidXY.y = 0;
this.writeCentroidToBuffer(c);
}
writeCentroidToBuffer(data) {
this.groundEffect.updateHiddenByLandmark(data);
const offset = data.vertexArrayOffset;
const vertexArrayBounds = data.vertexCount + data.vertexArrayOffset;
assert$1(vertexArrayBounds <= this.centroidVertexArray.length);
assert$1(this.centroidVertexArray.length === this.layoutVertexArray.length);
const c = data.flags & HIDDEN_BY_REPLACEMENT ? HIDDEN_CENTROID : data.centroidXY;
const firstX = this.centroidVertexArray.geta_centroid_pos0(offset);
const firstY = this.centroidVertexArray.geta_centroid_pos1(offset);
if (firstY === c.y && firstX === c.x) {
return;
}
for (let i = offset; i < vertexArrayBounds; ++i) {
this.centroidVertexArray.emplace(i, c.x, c.y);
}
this.needsCentroidUpdate = true;
}
createCentroidsBuffer() {
assert$1(this.centroidVertexArray.length === 0);
assert$1(this.groundEffect.hiddenByLandmarkVertexArray.length === 0);
this.centroidVertexArray.resize(this.layoutVertexArray.length);
this.groundEffect.hiddenByLandmarkVertexArray.resize(this.groundEffect.vertexArray.length);
for (const centroid of this.centroidData) {
this.writeCentroidToBuffer(centroid);
}
}
updateReplacement(coord, source, layerIndex) {
const perfStartTime = PerformanceUtils.now();
if (source.updateTime === this.replacementUpdateTime) {
return;
}
this.replacementUpdateTime = source.updateTime;
const newReplacements = source.getReplacementRegionsForTile(coord.toUnwrapped());
if (regionsEquals(this.activeReplacements, newReplacements)) {
return;
}
this.activeReplacements = newReplacements;
if (this.centroidVertexArray.length === 0) {
this.createCentroidsBuffer();
} else {
for (const centroid of this.centroidData) {
centroid.flags &= ~HIDDEN_BY_REPLACEMENT;
}
}
const transformedVertices = [];
for (const region of this.activeReplacements) {
if (region.order < layerIndex) continue;
const padding = Math.max(1, Math.pow(2, region.footprintTileId.canonical.z - coord.canonical.z));
if (region.footprint.buildingIds) {
for (const centroid of this.centroidData) {
if (centroid.flags & HIDDEN_BY_REPLACEMENT) {
continue;
}
if (region.min.x > centroid.max.x || centroid.min.x > region.max.x) {
continue;
} else if (region.min.y > centroid.max.y || centroid.min.y > region.max.y) {
continue;
}
if (region.footprint.buildingIds.has(centroid.buildingId)) {
centroid.flags |= HIDDEN_BY_REPLACEMENT;
}
}
} else {
for (const centroid of this.centroidData) {
if (centroid.flags & HIDDEN_BY_REPLACEMENT) {
continue;
}
if (region.min.x > centroid.max.x || centroid.min.x > region.max.x) {
continue;
} else if (region.min.y > centroid.max.y || centroid.min.y > region.max.y) {
continue;
}
for (let i = 0; i < centroid.footprintSegLen; i++) {
const seg = this.footprintSegments[centroid.footprintSegIdx + i];
transformedVertices.length = 0;
transformFootprintVertices(
this.footprintVertices,
seg.vertexOffset,
seg.vertexCount,
region.footprintTileId.canonical,
coord.canonical,
transformedVertices
);
if (footprintTrianglesIntersect(
region.footprint,
transformedVertices,
this.footprintIndices.uint16,
seg.indexOffset,
seg.indexCount,
-seg.vertexOffset,
-padding
)) {
centroid.flags |= HIDDEN_BY_REPLACEMENT;
break;
}
}
}
}
}
for (const centroid of this.centroidData) {
this.writeCentroidToBuffer(centroid);
}
this.borderDoneWithNeighborZ = [-1, -1, -1, -1];
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "FillExtrusionBucket.updateReplacement", "FillExtrusion", perfStartTime);
}
footprintContainsPoint(x, y, centroid) {
let c = false;
for (let s = 0; s < centroid.footprintSegLen; s++) {
const seg = this.footprintSegments[centroid.footprintSegIdx + s];
let startRing = 0;
for (const endRing of seg.ringIndices) {
for (let i = startRing, j = endRing + startRing - 1; i < endRing + startRing; j = i++) {
const x1 = this.footprintVertices.int16[(i + seg.vertexOffset) * 2 + 0];
const y1 = this.footprintVertices.int16[(i + seg.vertexOffset) * 2 + 1];
const x2 = this.footprintVertices.int16[(j + seg.vertexOffset) * 2 + 0];
const y2 = this.footprintVertices.int16[(j + seg.vertexOffset) * 2 + 1];
if (y1 > y !== y2 > y && x < (x2 - x1) * (y - y1) / (y2 - y1) + x1) {
c = !c;
}
}
startRing = endRing;
}
}
return c;
}
getHeightAtTileCoord(x, y) {
let height = Number.NEGATIVE_INFINITY;
let hidden = true;
assert$1(x > -EXTENT && y > -EXTENT && x < 2 * EXTENT && y < 2 * EXTENT);
const lookupKey = (x + EXTENT) * 4 * EXTENT + (y + EXTENT);
if (this.partLookup.hasOwnProperty(lookupKey)) {
const centroid = this.partLookup[lookupKey];
return centroid ? { height: centroid.height, hidden: !!(centroid.flags & HIDDEN_BY_REPLACEMENT) } : void 0;
}
for (const centroid of this.centroidData) {
if (x > centroid.max.x || centroid.min.x > x || y > centroid.max.y || centroid.min.y > y) {
continue;
}
if (centroid.height <= height) {
continue;
}
if (this.footprintContainsPoint(x, y, centroid)) {
height = centroid.height;
this.partLookup[lookupKey] = centroid;
hidden = !!(centroid.flags & HIDDEN_BY_REPLACEMENT);
}
}
if (height === Number.NEGATIVE_INFINITY) {
this.partLookup[lookupKey] = void 0;
return;
}
return { height, hidden };
}
}
function getCosHalfAngle(na, nb) {
const nm = na.add(nb)._unit();
const cosHalfAngle = na.x * nm.x + na.y * nm.y;
return cosHalfAngle;
}
function getRoundedEdgeOffset(p0, p1, p2, edgeRadius) {
const na = p1.sub(p0)._perp()._unit();
const nb = p2.sub(p1)._perp()._unit();
const cosHalfAngle = getCosHalfAngle(na, nb);
return _getRoundedEdgeOffset(p0, p1, p2, cosHalfAngle, edgeRadius);
}
function _getRoundedEdgeOffset(p0, p1, p2, cosHalfAngle, edgeRadius) {
const sinHalfAngle = Math.sqrt(1 - cosHalfAngle * cosHalfAngle);
return Math.min(p0.dist(p1) / 3, p1.dist(p2) / 3, edgeRadius * sinHalfAngle / cosHalfAngle);
}
register(FillExtrusionBucket, "FillExtrusionBucket", { omit: ["layers", "features"] });
register(PartData, "PartData");
register(FootprintSegment, "FootprintSegment");
register(BorderCentroidData, "BorderCentroidData");
register(GroundEffect, "GroundEffect");
function isEdgeOutsideBounds(p1, p2, bounds) {
return p1.x < bounds[0].x && p2.x < bounds[0].x || p1.x > bounds[1].x && p2.x > bounds[1].x || p1.y < bounds[0].y && p2.y < bounds[0].y || p1.y > bounds[1].y && p2.y > bounds[1].y;
}
function pointOutsideBounds$1(p, bounds) {
return p.x < bounds[0].x || p.x > bounds[1].x || p.y < bounds[0].y || p.y > bounds[1].y;
}
function isEntirelyOutside(ring) {
return ring.every((p) => p.x <= 0) || ring.every((p) => p.x >= EXTENT) || ring.every((p) => p.y <= 0) || ring.every((p) => p.y >= EXTENT);
}
function isAOConcaveAngle(p2, p1, p3) {
if (p2.x < 0 || p2.x >= EXTENT || p1.x < 0 || p1.x >= EXTENT || p3.x < 0 || p3.x >= EXTENT) {
return false;
}
const a = p3.sub(p1);
const an = a.perp();
const b = p2.sub(p1);
const ab = a.x * b.x + a.y * b.y;
const cosAB = ab / Math.sqrt((a.x * a.x + a.y * a.y) * (b.x * b.x + b.y * b.y));
const dotProductWithNormal = an.x * b.x + an.y * b.y;
return cosAB > -0.866 && dotProductWithNormal < 0;
}
function encodeAOToEdgeDistance(edgeDistance, isConcaveCorner, edgeStart) {
const encodedEdgeDistance = isConcaveCorner ? edgeDistance | 2 : edgeDistance & ~2;
return edgeStart ? encodedEdgeDistance | 1 : encodedEdgeDistance & ~1;
}
function fillExtrusionHeightLift() {
const angle = Math.PI / 32;
const tanAngle = Math.tan(angle);
const r = earthRadius;
return r * Math.sqrt(1 + 2 * tanAngle * tanAngle) - r;
}
function resampleFillExtrusionPolygonsForGlobe(polygons, tileBounds, tileID) {
const cellCount = 360 / 32;
const tiles = 1 << tileID.z;
const leftLng = lngFromMercatorX(tileID.x / tiles);
const rightLng = lngFromMercatorX((tileID.x + 1) / tiles);
const topLat = latFromMercatorY(tileID.y / tiles);
const bottomLat = latFromMercatorY((tileID.y + 1) / tiles);
const cellCountOnXAxis = Math.ceil((rightLng - leftLng) / cellCount);
const cellCountOnYAxis = Math.ceil((topLat - bottomLat) / cellCount);
const splitFn = (axis, min, max) => {
if (axis === 0) {
return 0.5 * (min + max);
} else {
const maxLat = latFromMercatorY((tileID.y + min / EXTENT) / tiles);
const minLat = latFromMercatorY((tileID.y + max / EXTENT) / tiles);
const midLat = 0.5 * (minLat + maxLat);
return (mercatorYfromLat(midLat) * tiles - tileID.y) * EXTENT;
}
};
return gridSubdivision(polygons, tileBounds, cellCountOnXAxis, cellCountOnYAxis, 1, splitFn);
}
function transformFootprintVertices(vertices, offset, count, footprintId, centroidId, out) {
const zDiff = Math.pow(2, footprintId.z - centroidId.z);
for (let i = 0; i < count; i++) {
let x = vertices.int16[(i + offset) * 2 + 0];
let y = vertices.int16[(i + offset) * 2 + 1];
x = (x + centroidId.x * EXTENT) * zDiff - footprintId.x * EXTENT;
y = (y + centroidId.y * EXTENT) * zDiff - footprintId.y * EXTENT;
out.push(new Point(x, y));
}
}
let layout$8;
const getLayoutProperties$9 = () => layout$8 || (layout$8 = new Properties({
"visibility": new DataConstantProperty(spec["layout_fill-extrusion"]["visibility"]),
"fill-extrusion-edge-radius": new DataConstantProperty(spec["layout_fill-extrusion"]["fill-extrusion-edge-radius"])
}));
let paint$9;
const getPaintProperties$9 = () => paint$9 || (paint$9 = new Properties({
"fill-extrusion-opacity": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-opacity"]),
"fill-extrusion-color": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-color"]),
"fill-extrusion-translate": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-translate"]),
"fill-extrusion-translate-anchor": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-translate-anchor"]),
"fill-extrusion-pattern": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-pattern"]),
"fill-extrusion-pattern-cross-fade": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-pattern-cross-fade"]),
"fill-extrusion-height": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-height"]),
"fill-extrusion-base": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-base"]),
"fill-extrusion-height-alignment": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-height-alignment"]),
"fill-extrusion-base-alignment": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-base-alignment"]),
"fill-extrusion-vertical-gradient": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-vertical-gradient"]),
"fill-extrusion-ambient-occlusion-intensity": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-ambient-occlusion-intensity"]),
"fill-extrusion-ambient-occlusion-radius": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-ambient-occlusion-radius"]),
"fill-extrusion-ambient-occlusion-wall-radius": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-ambient-occlusion-wall-radius"]),
"fill-extrusion-ambient-occlusion-ground-radius": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-ambient-occlusion-ground-radius"]),
"fill-extrusion-ambient-occlusion-ground-attenuation": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-ambient-occlusion-ground-attenuation"]),
"fill-extrusion-flood-light-color": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-flood-light-color"]),
"fill-extrusion-flood-light-intensity": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-flood-light-intensity"]),
"fill-extrusion-flood-light-wall-radius": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-flood-light-wall-radius"]),
"fill-extrusion-flood-light-ground-radius": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-flood-light-ground-radius"]),
"fill-extrusion-flood-light-ground-attenuation": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-flood-light-ground-attenuation"]),
"fill-extrusion-vertical-scale": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-vertical-scale"]),
"fill-extrusion-rounded-roof": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-rounded-roof"]),
"fill-extrusion-cutoff-fade-range": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-cutoff-fade-range"]),
"fill-extrusion-emissive-strength": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-emissive-strength"]),
"fill-extrusion-line-width": new DataDrivenProperty(spec["paint_fill-extrusion"]["fill-extrusion-line-width"]),
"fill-extrusion-cast-shadows": new DataConstantProperty(spec["paint_fill-extrusion"]["fill-extrusion-cast-shadows"]),
"fill-extrusion-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"fill-extrusion-flood-light-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class Point3D extends Point {
constructor(x, y, z) {
super(x, y);
this.z = z;
}
}
class Point4D extends Point3D {
// used for line progress and interpolated on clipping
constructor(x, y, z, w) {
super(x, y, z);
this.w = w;
}
}
function clipFirst(a, b, axis, clip) {
const axis1 = axis === "x" ? "y" : "x";
const ratio = (clip - a[axis]) / (b[axis] - a[axis]);
a[axis1] = Math.round(a[axis1] + (b[axis1] - a[axis1]) * ratio);
a[axis] = clip;
if (a.hasOwnProperty("z")) {
a["z"] = number(a["z"], b["z"], ratio);
}
if (a.hasOwnProperty("w")) {
a["w"] = number(a["w"], b["w"], ratio);
}
}
function clipLine(p0, p1, boundsMin, boundsMax) {
const clipAxis1 = boundsMin;
const clipAxis2 = boundsMax;
for (const axis of ["x", "y"]) {
let a = p0;
let b = p1;
if (a[axis] >= b[axis]) {
a = p1;
b = p0;
}
if (a[axis] < clipAxis1 && b[axis] > clipAxis1) {
clipFirst(a, b, axis, clipAxis1);
}
if (a[axis] < clipAxis2 && b[axis] > clipAxis2) {
clipFirst(b, a, axis, clipAxis2);
}
}
}
function clipLines(lines, x1, y1, x2, y2, linesInfo) {
const clippedLines = [];
for (let l = 0; l < lines.length; l++) {
const line = lines[l];
let clippedLine;
const startOffset = clippedLines.length;
let lineTotalLength = 0;
for (let i = 0; i < line.length - 1; i++) {
let p0 = line[i];
let p1 = line[i + 1];
let segLen = 0;
const lenSoFar = lineTotalLength;
let pStart;
let pEnd;
if (linesInfo) {
segLen = Math.hypot(p1.x - p0.x, p1.y - p0.y);
lineTotalLength += segLen;
pStart = p0;
pEnd = p1;
}
if (p0.x < x1 && p1.x < x1) {
continue;
} else if (p0.x < x1) {
p0 = new Point(x1, p0.y + (p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x)))._round();
} else if (p1.x < x1) {
p1 = new Point(x1, p0.y + (p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x)))._round();
}
if (p0.y < y1 && p1.y < y1) {
continue;
} else if (p0.y < y1) {
p0 = new Point(p0.x + (p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y)), y1)._round();
} else if (p1.y < y1) {
p1 = new Point(p0.x + (p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y)), y1)._round();
}
if (p0.x >= x2 && p1.x >= x2) {
continue;
} else if (p0.x >= x2) {
p0 = new Point(x2, p0.y + (p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x)))._round();
} else if (p1.x >= x2) {
p1 = new Point(x2, p0.y + (p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x)))._round();
}
if (p0.y >= y2 && p1.y >= y2) {
continue;
} else if (p0.y >= y2) {
p0 = new Point(p0.x + (p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y)), y2)._round();
} else if (p1.y >= y2) {
p1 = new Point(p0.x + (p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y)), y2)._round();
}
if (!clippedLine || !p0.equals(clippedLine[clippedLine.length - 1])) {
clippedLine = [p0];
clippedLines.push(clippedLine);
if (linesInfo) {
linesInfo.push({
progress: { min: lenSoFar + computeT(pStart, pEnd, p0) * segLen, max: 1 },
parentIndex: l,
prevPoint: pStart,
nextPoint: pEnd
});
}
}
clippedLine.push(p1);
if (linesInfo) {
linesInfo[linesInfo.length - 1].progress.max = lenSoFar + computeT(pStart, pEnd, p1) * segLen;
linesInfo[linesInfo.length - 1].nextPoint = pEnd;
}
}
if (linesInfo) {
if (lineTotalLength > 0) {
for (let i = startOffset; i < clippedLines.length; i++) {
linesInfo[i].progress.min /= lineTotalLength;
linesInfo[i].progress.max /= lineTotalLength;
}
}
}
}
assert$1(!linesInfo || clippedLines.length === linesInfo.length);
return clippedLines;
}
function lineSubdivision(subjectLine, edgeIterator, separateSegments, linesOut, progressOut) {
if (subjectLine.length < 2) {
linesOut.push(subjectLine);
if (progressOut) {
progressOut.push({ min: 0, max: 1 });
}
return;
}
const intersections = [];
while (edgeIterator.valid()) {
const [c, d] = edgeIterator.get();
for (let i = 0; i < subjectLine.length - 1; i++) {
const a = subjectLine[i];
const b = subjectLine[i + 1];
const intersection = segmentSegmentIntersection(a, b, c, d);
if (intersection) {
const [t] = intersection;
const point = new Point(number(a.x, b.x, t), number(a.y, b.y, t));
intersections.push({ t: i + t, distance: 0, point });
}
}
edgeIterator.next();
}
if (intersections.length === 0) {
linesOut.push(subjectLine);
if (progressOut) {
progressOut.push({ min: 0, max: 1 });
}
return;
}
intersections.sort((a, b) => a.t - b.t);
const pointDistances = [];
if (progressOut) {
pointDistances.push(0);
for (let i = 1; i < subjectLine.length; i++) {
const a = subjectLine[i - 1];
const b = subjectLine[i];
pointDistances.push(pointDistances[i - 1] + Math.hypot(a.x - b.x, a.y - b.y));
}
}
let subjIdx = 0;
let intrIdx = 0;
let output = [];
linesOut.push(output);
if (progressOut) {
progressOut.push({ min: 0, max: 1 });
}
while (subjIdx !== subjectLine.length) {
if (intrIdx === intersections.length) {
while (subjIdx !== subjectLine.length) {
if (output.length === 0 || !output[output.length - 1].equals(subjectLine[subjIdx])) {
output.push(subjectLine[subjIdx]);
}
subjIdx++;
}
break;
}
if (intersections[intrIdx].t <= subjIdx) {
if (output.length === 0 || !output[output.length - 1].equals(intersections[intrIdx].point)) {
output.push(intersections[intrIdx].point);
}
const pointIdx = Math.trunc(intersections[intrIdx].t);
if (separateSegments && output.length > 1 && pointIdx < subjectLine.length - 1) {
output = [intersections[intrIdx].point];
linesOut.push(output);
if (progressOut) {
const fract = intersections[intrIdx].t - pointIdx;
const distanceAtT = number(pointDistances[pointIdx], pointDistances[pointIdx + 1], fract);
const normT = distanceAtT / pointDistances[pointDistances.length - 1];
progressOut[progressOut.length - 1].max = normT;
progressOut.push({ min: normT, max: 1 });
}
}
intrIdx++;
} else {
if (output.length === 0 || !output[output.length - 1].equals(subjectLine[subjIdx])) {
output.push(subjectLine[subjIdx]);
}
subjIdx++;
}
}
assert$1(intrIdx === intersections.length);
}
function computeT(a, b, p) {
if (a.x !== b.x) {
return (p.x - a.x) / (b.x - a.x);
} else if (a.y !== b.y) {
return (p.y - a.y) / (b.y - a.y);
}
return 0;
}
class FillExtrusionStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$9(),
paint: getPaintProperties$9()
};
super(layer, properties, scope, lut, options);
this._stats = { numRenderedVerticesInShadowPass: 0, numRenderedVerticesInTransparentPass: 0 };
}
createBucket(parameters) {
return new FillExtrusionBucket(parameters);
}
queryRadius() {
return translateDistance(this.paint.get("fill-extrusion-translate"));
}
is3D(terrainEnabled) {
return true;
}
hasShadowPass() {
return this.paint.get("fill-extrusion-cast-shadows");
}
cutoffRange() {
return this.paint.get("fill-extrusion-cutoff-fade-range");
}
canCastShadows() {
return true;
}
getProgramIds() {
const patternProperty = this.paint.get("fill-extrusion-pattern");
const image = patternProperty.constantOr(1);
return [image ? "fillExtrusionPattern" : "fillExtrusion"];
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform, pixelPosMatrix, elevationHelper, layoutVertexArrayOffset) {
const translation = tilespaceTranslate(
this.paint.get("fill-extrusion-translate"),
this.paint.get("fill-extrusion-translate-anchor"),
transform.angle,
queryGeometry.pixelToTileUnitsFactor
);
const height = this.paint.get("fill-extrusion-height").evaluate(feature, featureState);
const base = this.paint.get("fill-extrusion-base").evaluate(feature, featureState);
const centroid = [0, 0];
const terrainVisible = elevationHelper && transform.elevation;
const exaggeration = transform.elevation ? transform.elevation.exaggeration() : 1;
const bucket = queryGeometry.tile.getBucket(this);
if (terrainVisible && bucket instanceof FillExtrusionBucket) {
const centroidVertexArray = bucket.centroidVertexArray;
const centroidOffset = layoutVertexArrayOffset + 1;
if (centroidOffset < centroidVertexArray.length) {
centroid[0] = centroidVertexArray.geta_centroid_pos0(centroidOffset);
centroid[1] = centroidVertexArray.geta_centroid_pos1(centroidOffset);
}
}
const isHidden = centroid[0] === 0 && centroid[1] === 1;
if (isHidden) return false;
if (transform.projection.name === "globe") {
const bounds = [new Point(0, 0), new Point(EXTENT, EXTENT)];
const resampledGeometry = resampleFillExtrusionPolygonsForGlobe([geometry], bounds, queryGeometry.tileID.canonical);
geometry = resampledGeometry.map((clipped) => clipped.polygon).flat();
}
const demSampler = terrainVisible ? elevationHelper : null;
const [projectedBase, projectedTop] = projectExtrusion(transform, geometry, base, height, translation, pixelPosMatrix, demSampler, centroid, exaggeration, transform.center.lat, queryGeometry.tileID.canonical);
const screenQuery = queryGeometry.queryGeometry;
const projectedQueryGeometry = screenQuery.isPointQuery() ? screenQuery.screenBounds : screenQuery.screenGeometry;
return checkIntersection(projectedBase, projectedTop, projectedQueryGeometry);
}
}
function dot(a, b) {
return a.x * b.x + a.y * b.y;
}
function getIntersectionDistance(projectedQueryGeometry, projectedFace) {
if (projectedQueryGeometry.length === 1) {
let i = 0;
const a = projectedFace[i++];
let b;
while (!b || a.equals(b)) {
b = projectedFace[i++];
if (!b) return Infinity;
}
for (; i < projectedFace.length; i++) {
const c = projectedFace[i];
const p = projectedQueryGeometry[0];
const ab = b.sub(a);
const ac = c.sub(a);
const ap = p.sub(a);
const dotABAB = dot(ab, ab);
const dotABAC = dot(ab, ac);
const dotACAC = dot(ac, ac);
const dotAPAB = dot(ap, ab);
const dotAPAC = dot(ap, ac);
const denom = dotABAB * dotACAC - dotABAC * dotABAC;
const v = (dotACAC * dotAPAB - dotABAC * dotAPAC) / denom;
const w = (dotABAB * dotAPAC - dotABAC * dotAPAB) / denom;
const u = 1 - v - w;
const distance = a.z * u + b.z * v + c.z * w;
if (isFinite(distance)) return distance;
}
return Infinity;
} else {
let closestDistance = Infinity;
for (const p of projectedFace) {
closestDistance = Math.min(closestDistance, p.z);
}
return closestDistance;
}
}
function checkIntersection(projectedBase, projectedTop, projectedQueryGeometry) {
let closestDistance = Infinity;
if (polygonIntersectsMultiPolygon(projectedQueryGeometry, projectedTop)) {
closestDistance = getIntersectionDistance(projectedQueryGeometry, projectedTop[0]);
}
for (let r = 0; r < projectedTop.length; r++) {
const ringTop = projectedTop[r];
const ringBase = projectedBase[r];
for (let p = 0; p < ringTop.length - 1; p++) {
const topA = ringTop[p];
const topB = ringTop[p + 1];
const baseA = ringBase[p];
const baseB = ringBase[p + 1];
const face = [topA, topB, baseB, baseA, topA];
if (polygonIntersectsPolygon(projectedQueryGeometry, face)) {
closestDistance = Math.min(closestDistance, getIntersectionDistance(projectedQueryGeometry, face));
}
}
}
return closestDistance === Infinity ? false : closestDistance;
}
function projectExtrusion(tr, geometry, zBase, zTop, translation, m, demSampler, centroid, exaggeration, lat, tileID) {
if (tr.projection.name === "globe") {
return projectExtrusionGlobe(tr, geometry, zBase, zTop, translation, m, demSampler, centroid, exaggeration, lat, tileID);
} else {
if (demSampler) {
return projectExtrusion3D(geometry, zBase, zTop, translation, m, demSampler, centroid, exaggeration, lat);
} else {
return projectExtrusion2D(geometry, zBase, zTop, translation, m);
}
}
}
function projectExtrusionGlobe(tr, geometry, zBase, zTop, translation, m, demSampler, centroid, exaggeration, lat, tileID) {
const projectedBase = [];
const projectedTop = [];
const elevationScale = tr.projection.upVectorScale(tileID, tr.center.lat, tr.worldSize).metersToTile;
const basePoint = [0, 0, 0, 1];
const topPoint = [0, 0, 0, 1];
const setPoint = (point, x, y, z) => {
point[0] = x;
point[1] = y;
point[2] = z;
point[3] = 1;
};
const lift = fillExtrusionHeightLift();
if (zBase > 0) {
zBase += lift;
}
zTop += lift;
for (const r of geometry) {
const ringBase = [];
const ringTop = [];
for (const p of r) {
const x = p.x + translation.x;
const y = p.y + translation.y;
const reproj = tr.projection.projectTilePoint(x, y, tileID);
const dir = tr.projection.upVector(tileID, p.x, p.y);
let zBasePoint = zBase;
let zTopPoint = zTop;
if (demSampler) {
const offset = getTerrainHeightOffset(x, y, zBase, zTop, demSampler, centroid, exaggeration, lat);
zBasePoint += offset.base;
zTopPoint += offset.top;
}
if (zBase !== 0) {
setPoint(
basePoint,
reproj.x + dir[0] * elevationScale * zBasePoint,
reproj.y + dir[1] * elevationScale * zBasePoint,
reproj.z + dir[2] * elevationScale * zBasePoint
);
} else {
setPoint(basePoint, reproj.x, reproj.y, reproj.z);
}
setPoint(
topPoint,
reproj.x + dir[0] * elevationScale * zTopPoint,
reproj.y + dir[1] * elevationScale * zTopPoint,
reproj.z + dir[2] * elevationScale * zTopPoint
);
transformMat4$2(basePoint, basePoint, m);
transformMat4$2(topPoint, topPoint, m);
ringBase.push(new Point3D(basePoint[0], basePoint[1], basePoint[2]));
ringTop.push(new Point3D(topPoint[0], topPoint[1], topPoint[2]));
}
projectedBase.push(ringBase);
projectedTop.push(ringTop);
}
return [projectedBase, projectedTop];
}
function projectExtrusion2D(geometry, zBase, zTop, translation, m) {
const projectedBase = [];
const projectedTop = [];
const baseXZ = m[8] * zBase;
const baseYZ = m[9] * zBase;
const baseZZ = m[10] * zBase;
const baseWZ = m[11] * zBase;
const topXZ = m[8] * zTop;
const topYZ = m[9] * zTop;
const topZZ = m[10] * zTop;
const topWZ = m[11] * zTop;
for (const r of geometry) {
const ringBase = [];
const ringTop = [];
for (const p of r) {
const x = p.x + translation.x;
const y = p.y + translation.y;
const sX = m[0] * x + m[4] * y + m[12];
const sY = m[1] * x + m[5] * y + m[13];
const sZ = m[2] * x + m[6] * y + m[14];
const sW = m[3] * x + m[7] * y + m[15];
const baseX = sX + baseXZ;
const baseY = sY + baseYZ;
const baseZ = sZ + baseZZ;
const baseW = Math.max(sW + baseWZ, 1e-5);
const topX = sX + topXZ;
const topY = sY + topYZ;
const topZ = sZ + topZZ;
const topW = Math.max(sW + topWZ, 1e-5);
ringBase.push(new Point3D(baseX / baseW, baseY / baseW, baseZ / baseW));
ringTop.push(new Point3D(topX / topW, topY / topW, topZ / topW));
}
projectedBase.push(ringBase);
projectedTop.push(ringTop);
}
return [projectedBase, projectedTop];
}
function projectExtrusion3D(geometry, zBase, zTop, translation, m, demSampler, centroid, exaggeration, lat) {
const projectedBase = [];
const projectedTop = [];
const v = [0, 0, 0, 1];
for (const r of geometry) {
const ringBase = [];
const ringTop = [];
for (const p of r) {
const x = p.x + translation.x;
const y = p.y + translation.y;
const heightOffset = getTerrainHeightOffset(x, y, zBase, zTop, demSampler, centroid, exaggeration, lat);
v[0] = x;
v[1] = y;
v[2] = heightOffset.base;
v[3] = 1;
transformMat4$1(v, v, m);
v[3] = Math.max(v[3], 1e-5);
const base = new Point3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]);
v[0] = x;
v[1] = y;
v[2] = heightOffset.top;
v[3] = 1;
transformMat4$1(v, v, m);
v[3] = Math.max(v[3], 1e-5);
const top = new Point3D(v[0] / v[3], v[1] / v[3], v[2] / v[3]);
ringBase.push(base);
ringTop.push(top);
}
projectedBase.push(ringBase);
projectedTop.push(ringTop);
}
return [projectedBase, projectedTop];
}
function getTerrainHeightOffset(x, y, zBase, zTop, demSampler, centroid, exaggeration, lat) {
const ele = exaggeration * demSampler.getElevationAt(x, y, true, true);
const flatRoof = centroid[0] !== 0;
const centroidElevation = flatRoof ? centroid[1] === 0 ? exaggeration * elevationFromUint16(centroid[0]) : exaggeration * flatElevation(demSampler, centroid, lat) : ele;
return {
base: ele + (zBase === 0 ? -1 : zBase),
// Use -1 instead of -5 in shader to prevent picking underground
top: flatRoof ? Math.max(centroidElevation + zTop, ele + zBase + 2) : ele + zTop
};
}
function elevationFromUint16(n) {
return n / ELEVATION_SCALE - ELEVATION_OFFSET;
}
function flatElevation(demSampler, centroid, lat) {
const posX = Math.floor(centroid[0] / 8);
const posY = Math.floor(centroid[1] / 8);
const spanX = 10 * (centroid[0] - posX * 8);
const spanY = 10 * (centroid[1] - posY * 8);
const z = demSampler.getElevationAt(posX, posY, true, true);
const meterToDEM = demSampler.getMeterToDEM(lat);
const wX = Math.floor(0.5 * (spanX * meterToDEM - 1));
const wY = Math.floor(0.5 * (spanY * meterToDEM - 1));
const posPx = demSampler.tileCoordToPixel(posX, posY);
const offsetX = 2 * wX + 1;
const offsetY = 2 * wY + 1;
const corners = fourSample(demSampler, posPx.x - wX, posPx.y - wY, offsetX, offsetY);
const diffX = Math.abs(corners[0] - corners[1]);
const diffY = Math.abs(corners[2] - corners[3]);
const diffZ = Math.abs(corners[0] - corners[2]);
const diffW = Math.abs(corners[1] - corners[3]);
const diffSumX = diffX + diffY;
const diffSumY = diffZ + diffW;
const slopeX = Math.min(0.25, meterToDEM * 0.5 * diffSumX / offsetX);
const slopeY = Math.min(0.25, meterToDEM * 0.5 * diffSumY / offsetY);
return z + Math.max(slopeX * spanX, slopeY * spanY);
}
function fourSample(demSampler, posX, posY, offsetX, offsetY) {
return [
demSampler.getElevationAtPixel(posX, posY, true),
demSampler.getElevationAtPixel(posX + offsetY, posY, true),
demSampler.getElevationAtPixel(posX, posY + offsetY, true),
demSampler.getElevationAtPixel(posX + offsetX, posY + offsetY, true)
];
}
class ThrottledInvoker {
constructor(callback) {
this._callback = callback;
this._triggered = false;
if (typeof MessageChannel !== "undefined") {
this._channel = new MessageChannel();
this._channel.port2.onmessage = () => {
this._triggered = false;
this._callback();
};
}
}
trigger() {
if (!this._triggered) {
this._triggered = true;
if (this._channel) {
this._channel.port1.postMessage(true);
} else {
setTimeout(() => {
this._triggered = false;
this._callback();
}, 0);
}
}
}
remove() {
this._channel = void 0;
this._callback = () => {
};
}
}
class Scheduler {
constructor() {
this.tasks = {};
this.taskQueue = [];
bindAll(["process"], this);
this.invoker = new ThrottledInvoker(this.process);
this.nextId = 0;
}
add(fn, metadata) {
const id = this.nextId++;
const priority = getPriority(metadata);
if (priority === 0) {
const m = isWorker(self) ? PerformanceUtils.beginMeasure("workerTask") : void 0;
try {
fn();
} finally {
if (m) PerformanceUtils.endMeasure(m);
}
return null;
}
this.tasks[id] = { fn, metadata, priority, id };
this.taskQueue.push(id);
this.invoker.trigger();
return {
cancel: () => {
delete this.tasks[id];
}
};
}
process() {
const m = isWorker(self) ? PerformanceUtils.beginMeasure("workerTask") : void 0;
try {
this.taskQueue = this.taskQueue.filter((id2) => !!this.tasks[id2]);
if (!this.taskQueue.length) {
return;
}
const id = this.pick();
if (id === null) return;
const task = this.tasks[id];
delete this.tasks[id];
if (this.taskQueue.length) {
this.invoker.trigger();
}
if (!task) {
return;
}
task.fn();
} finally {
if (m) PerformanceUtils.endMeasure(m);
}
}
pick() {
let minIndex = null;
let minPriority = Infinity;
for (let i = 0; i < this.taskQueue.length; i++) {
const id2 = this.taskQueue[i];
const task = this.tasks[id2];
if (task.priority < minPriority) {
minPriority = task.priority;
minIndex = i;
}
}
if (minIndex === null) return null;
const id = this.taskQueue[minIndex];
this.taskQueue.splice(minIndex, 1);
return id;
}
remove() {
this.invoker.remove();
}
}
function getPriority({ type, isSymbolTile, zoom }) {
zoom = zoom || 0;
if (type === "message") return 0;
if (type === "maybePrepare" && !isSymbolTile) return 100 - zoom;
if (type === "parseTile" && !isSymbolTile) return 200 - zoom;
if (type === "parseTile" && isSymbolTile) return 300 - zoom;
if (type === "maybePrepare" && isSymbolTile) return 400 - zoom;
return 500;
}
class Actor {
constructor(target, parent, mapId) {
this.target = target;
this.parent = parent;
this.mapId = mapId;
this.callbacks = {};
this.cancelCallbacks = {};
bindAll(["receive"], this);
this.target.addEventListener("message", this.receive, false);
this.scheduler = new Scheduler();
}
/**
* Sends a message from a main-thread map to a Worker or from a Worker back to
* a main-thread map instance.
*
* @param type The name of the target method to invoke or '[source-type].[source-name].name' for a method on a WorkerSource.
* @param targetMapId A particular mapId to which to send this message.
* @private
*/
send(type, data, callback, targetMapId, mustQueue = false, callbackMetadata) {
const id = Math.round(Math.random() * 1e18).toString(36).substring(0, 10);
if (callback) {
callback.metadata = callbackMetadata;
this.callbacks[id] = callback;
}
const buffers = /* @__PURE__ */ new Set();
this.target.postMessage({
id,
type,
hasCallback: !!callback,
targetMapId,
mustQueue,
sourceMapId: this.mapId,
data: serialize(data, buffers)
}, buffers);
return {
cancel: () => {
if (callback) {
delete this.callbacks[id];
}
this.target.postMessage({
id,
type: "",
targetMapId,
sourceMapId: this.mapId
});
}
};
}
receive(message) {
const data = message.data;
if (!data) return;
const id = data.id;
if (!id) return;
if (data.targetMapId && this.mapId !== data.targetMapId) {
return;
}
if (data.type === "") {
const cancel = this.cancelCallbacks[id];
delete this.cancelCallbacks[id];
if (cancel) {
cancel.cancel();
}
} else {
if (data.mustQueue || isWorker(self)) {
const callback = this.callbacks[id];
const metadata = callback && callback.metadata || { type: "message" };
const cancel = this.scheduler.add(() => this.processTask(id, data), metadata);
if (cancel) this.cancelCallbacks[id] = cancel;
} else {
this.processTask(id, data);
}
}
}
processTask(id, task) {
delete this.cancelCallbacks[id];
if (task.type === "") {
const callback = this.callbacks[id];
delete this.callbacks[id];
if (callback) {
if (task.error) {
callback(deserialize(task.error));
} else {
callback(null, deserialize(task.data));
}
}
} else {
const buffers = /* @__PURE__ */ new Set();
const done = task.hasCallback ? (err, data) => {
this.target.postMessage({
id,
type: "",
sourceMapId: this.mapId,
error: err ? serialize(err) : null,
data: serialize(data, buffers)
}, buffers);
} : () => {
};
const params = deserialize(task.data);
if (this.parent[task.type]) {
this.parent[task.type](task.sourceMapId, params, done);
} else if (this.parent.getWorkerSource) {
const keys = task.type.split(".");
const { source, scope } = params;
const workerSource = this.parent.getWorkerSource(task.sourceMapId, keys[0], source, scope);
workerSource[keys[1]](params, done);
} else {
done(new Error(`Could not find function ${task.type}`));
}
}
}
remove() {
this.scheduler.remove();
this.target.removeEventListener("message", this.receive, false);
}
}
var WorkerClass = {
workerUrl: "",
workerClass: null,
workerParams: void 0
};
function createWorker(name) {
return WorkerClass.workerClass != null ? new WorkerClass.workerClass() : new self.Worker(WorkerClass.workerUrl, Object.assign({ name }, WorkerClass.workerParams));
}
const PRELOAD_POOL_ID = "mapboxgl_preloaded_worker_pool";
class WorkerPool {
constructor(name) {
this.active = {};
this.name = name;
}
acquire(mapId, count = WorkerPool.workerCount) {
if (!this.workers) {
this.workers = [];
while (this.workers.length < count) {
const w = createWorker(`${this.name || ""}WorkerPool: ${mapId}-${this.workers.length}`);
this.workers.push(w);
}
}
this.active[mapId] = true;
return this.workers.slice();
}
release(mapId) {
delete this.active[mapId];
if (this.workers && this.numActive() === 0) {
this.workers.forEach((w) => {
w.terminate();
});
this.workers = null;
}
}
isPreloaded() {
return !!this.active[PRELOAD_POOL_ID];
}
numActive() {
return Object.keys(this.active).length;
}
}
WorkerPool.workerCount = 2;
class Dispatcher {
constructor(workerPool, parent, name = "Worker", count = WorkerPool.workerCount) {
this.workerPool = workerPool;
this.actors = [];
this.currentActor = 0;
this.id = uniqueId();
const workers = this.workerPool.acquire(this.id, count);
for (let i = 0; i < workers.length; i++) {
const worker = workers[i];
const actor = new Dispatcher.Actor(worker, parent, this.id);
actor.name = `${name} ${i}`;
this.actors.push(actor);
}
assert$1(this.actors.length);
this.ready = false;
this.broadcast("checkIfReady", null, () => {
this.ready = true;
});
}
/**
* Broadcast a message to all Workers.
* @private
*/
broadcast(type, data, cb) {
assert$1(this.actors.length);
cb = cb || function() {
};
asyncAll(this.actors, (actor, done) => {
actor.send(type, data, done);
}, cb);
}
/**
* Acquires an actor to dispatch messages to. The actors are distributed in round-robin fashion.
* @returns {Actor} An actor object backed by a web worker for processing messages.
*/
getActor() {
assert$1(this.actors.length);
this.currentActor = (this.currentActor + 1) % this.actors.length;
return this.actors[this.currentActor];
}
remove() {
this.actors.forEach((actor) => {
actor.remove();
});
this.actors = [];
this.workerPool.release(this.id);
}
}
Dispatcher.Actor = Actor;
let globalWorkerPool;
let imageRasterizerWorkerPool;
function getGlobalWorkerPool() {
if (!globalWorkerPool) {
globalWorkerPool = new WorkerPool();
}
return globalWorkerPool;
}
function getImageRasterizerWorkerPool() {
if (!imageRasterizerWorkerPool) {
imageRasterizerWorkerPool = new WorkerPool("ImageRasterizer");
}
return imageRasterizerWorkerPool;
}
function prewarm() {
const workerPool = getGlobalWorkerPool();
workerPool.acquire(PRELOAD_POOL_ID);
}
function clearPrewarmedResources() {
const pool = globalWorkerPool;
if (pool) {
if (pool.isPreloaded() && pool.numActive() === 1) {
pool.release(PRELOAD_POOL_ID);
globalWorkerPool = null;
} else {
console.warn("Could not clear WebWorkers since there are active Map instances that still reference it. The pre-warmed WebWorker pool can only be cleared when all map instances have been removed with map.remove()");
}
}
}
const ROOF_TYPE_PARAPET = 0;
const ROOF_TYPE_HIPPED = 1;
const ROOF_TYPE_GABLED = 2;
const ROOF_TYPE_FLAT = 3;
const ROOF_TYPE_MANSARD = 4;
const ROOF_TYPE_SKILLION = 5;
const ROOF_TYPE_PYRAMIDAL = 6;
const BUILDING_PART_WALL = 0;
const BUILDING_PART_ROOF = 1;
const BUILDING_PART_FACADE_GLAZING = 2;
const BUILDING_PART_ENTRANCE = 3;
const MEMORY_STACK_SIZE = 1024 * 4;
class BuildingGen {
constructor(module) {
this.module = module;
this.memoryStack = this.module.malloc(MEMORY_STACK_SIZE);
this.memoryStackNextFree = this.memoryStack;
}
createIntArray(data) {
const positionsPtr = this.memoryStackNextFree;
this.memoryStackNextFree += data.length * Int32Array.BYTES_PER_ELEMENT;
if (this.memoryStackNextFree - this.memoryStack > MEMORY_STACK_SIZE) {
return -1;
}
const positionsArray = new Int32Array(this.module.heap32.buffer, positionsPtr, data.length);
positionsArray.set(data);
return positionsPtr;
}
createFloatArray(data) {
const positionsPtr = this.memoryStackNextFree;
this.memoryStackNextFree += data.length * Float32Array.BYTES_PER_ELEMENT;
if (this.memoryStackNextFree - this.memoryStack > MEMORY_STACK_SIZE) {
return -1;
}
const positionsArray = new Float32Array(this.module.heapF32.buffer, positionsPtr, data.length);
positionsArray.set(data);
return positionsPtr;
}
readStringBuffer(ptr) {
let str = "";
while (this.module.heapU8[ptr] !== 0) {
str += String.fromCharCode(this.module.heapU8[ptr]);
++ptr;
}
return str;
}
setStyle(style) {
const normalScale = style.normalScale;
this.module.setStyle(
normalScale[0],
normalScale[1],
normalScale[2],
style.tileToMeters
);
}
setAOOptions(bakeToVertices, parapetOcclusionDistance) {
this.module.setAOOptions(bakeToVertices ? 1 : 0, parapetOcclusionDistance);
}
setMetricOptions(convertToMeters, tileZoom) {
this.module.setMetricOptions(convertToMeters ? 1 : 0, tileZoom);
}
setStructuralOptions(simplifyInput) {
this.module.setStructuralOptions(simplifyInput ? 1 : 0);
}
setFacadeOptions(facadeHeight, createEaves) {
this.module.setFacadeOptions(facadeHeight, createEaves ? 1 : 0);
}
setFauxFacadeOptions(hasFacade, useUvXModifier, uvXModifier) {
this.module.setFauxFacadeOptions(hasFacade ? 1 : 0, useUvXModifier ? 1 : 0, uvXModifier);
}
setFacadeClassifierOptions(classificationDistance) {
this.module.setFacadeClassifierOptions(classificationDistance);
}
generateMesh(features, facades) {
this.memoryStackNextFree = this.memoryStack;
for (const feature of features) {
const ringIndexPointer = this.createIntArray(feature.ringIndices);
const coordinatesPointer = this.createFloatArray(feature.coordinates);
if (ringIndexPointer === -1 || coordinatesPointer === -1) {
return `building_gen: Out of stack memory: ${this.memoryStackNextFree - this.memoryStack}/${MEMORY_STACK_SIZE}`;
}
this.module.addFeature(
feature.id,
feature.sourceId,
feature.minHeight,
feature.height,
feature.roofType,
coordinatesPointer,
ringIndexPointer,
feature.ringIndices.length - 1
);
}
for (const facade of facades) {
let entrances;
if (facade.entrances) {
entrances = JSON.parse(facade.entrances);
} else {
entrances = [];
}
const entrancesPointer = this.createFloatArray(entrances);
const coordinatesPointer = this.createFloatArray(facade.coordinates);
if (entrancesPointer === -1 || coordinatesPointer === -1) {
return `building_gen: Out of stack memory: ${this.memoryStackNextFree - this.memoryStack}/${MEMORY_STACK_SIZE}`;
}
this.module.addFacade(
facade.sourceId,
facade.crossPerc,
facade.distanceToRoad,
entrancesPointer,
entrances.length,
coordinatesPointer,
facade.coordinates.length
);
}
const success = this.module.generateMesh();
if (!success) {
const errorPtr = this.module.getLastError();
return this.readStringBuffer(errorPtr);
}
const meshCount = this.module.getMeshCount();
const meshes = new Array(meshCount);
for (let i = 0; i < meshCount; i++) {
const positionsPtr = this.module.getPositionsPtr(i);
const positionsLength = this.module.getPositionsLength(i);
const positionsArray = new Float32Array(this.module.heapF32.buffer, positionsPtr, positionsLength);
const normalsPtr = this.module.getNormalsPtr(i);
const normalsLength = this.module.getNormalsLength(i);
const normalsArray = new Float32Array(this.module.heapF32.buffer, normalsPtr, normalsLength);
const aoPtr = this.module.getAOPtr(i);
const aoLength = this.module.getAOLength(i);
const aoArray = new Float32Array(this.module.heapF32.buffer, aoPtr, aoLength);
const uvPtr = this.module.getUVPtr(i);
const uvLength = this.module.getUVLength(i);
const uvArray = new Float32Array(this.module.heapF32.buffer, uvPtr, uvLength);
const fauxFacadePtr = this.module.getFauxFacadePtr(i);
const fauxFacadeLength = this.module.getFauxFacadeLength(i);
const isFauxFacadeArray = new Uint8Array(this.module.heapU8.buffer, fauxFacadePtr, fauxFacadeLength);
const indicesPtr = this.module.getIndicesPtr(i);
const indicesLength = this.module.getIndicesLength(i);
const indicesArray = new Int16Array(this.module.heap16.buffer, indicesPtr, indicesLength);
const buildingPart = this.module.getBuildingPart(i);
meshes[i] = {
positions: positionsArray,
normals: normalsArray,
ao: aoArray,
uv: uvArray,
isFauxFacade: isFauxFacadeArray,
indices: indicesArray,
buildingPart
};
}
const ringCount = this.module.getRingCount();
const modifiedPolygonRings = [];
for (let i = 0; i < ringCount; i++) {
const ringPtr = this.module.getRingPtr(i);
const ringLength = this.module.getRingLength(i);
const ringArray = new Float32Array(this.module.heapF32.buffer, ringPtr, ringLength);
modifiedPolygonRings.push(ringArray);
}
const outerRingLength = this.module.getOuterRingLength();
return { meshes, outerRingLength, modifiedPolygonRings };
}
}
function loadBuildingGen(wasmPromise) {
let heapU8;
let heap16;
let heap32;
let heapF32;
let wasmMemory;
function updateMemoryViews() {
heapU8 = new Uint8Array(wasmMemory.buffer);
heap16 = new Int16Array(wasmMemory.buffer);
heap32 = new Int32Array(wasmMemory.buffer);
heapF32 = new Float32Array(wasmMemory.buffer);
}
function abort() {
throw new Error("Unexpected BuildingGen error.");
}
function resizeHeap(requestedSize) {
const oldSize = heapU8.length;
const newSize = Math.max(requestedSize >>> 0, Math.ceil(oldSize * 1.2));
const pages = Math.ceil((newSize - oldSize) / 65536);
try {
wasmMemory.grow(pages);
updateMemoryViews();
return true;
} catch (_) {
return false;
}
}
const wasmVoid = () => {
};
const wasmImports = {
a: {
a: abort,
f: resizeHeap,
g: abort,
// These are a regression in emscripten and mostly don't appear to
// be used/important.
// https://github.com/emscripten-core/emscripten/issues/22534
b: wasmVoid,
c: wasmVoid,
d: wasmVoid,
e: wasmVoid
}
};
const instantiateWasm = WebAssembly.instantiateStreaming ? WebAssembly.instantiateStreaming(wasmPromise, wasmImports) : wasmPromise.then((wasm) => wasm.arrayBuffer()).then((buffer) => WebAssembly.instantiate(buffer, wasmImports));
return instantiateWasm.then((output) => {
const exports$1 = output.instance.exports;
const initialiseRuntime = exports$1.g;
initialiseRuntime();
wasmMemory = exports$1.f;
updateMemoryViews();
return new BuildingGen({
setStyle: exports$1.h,
setAOOptions: exports$1.i,
setMetricOptions: exports$1.j,
setStructuralOptions: exports$1.k,
setFacadeOptions: exports$1.l,
setFauxFacadeOptions: exports$1.m,
setFacadeClassifierOptions: exports$1.n,
addFeature: exports$1.o,
addFacade: exports$1.p,
generateMesh: exports$1.q,
getLastError: exports$1.r,
getOuterRingLength: exports$1.s,
getMeshCount: exports$1.t,
getPositionsPtr: exports$1.u,
getPositionsLength: exports$1.v,
getNormalsPtr: exports$1.w,
getNormalsLength: exports$1.x,
getAOPtr: exports$1.y,
getAOLength: exports$1.z,
getUVPtr: exports$1.A,
getUVLength: exports$1.B,
getFauxFacadePtr: exports$1.C,
getFauxFacadeLength: exports$1.D,
getIndicesPtr: exports$1.E,
getIndicesLength: exports$1.F,
getBuildingPart: exports$1.G,
getRingCount: exports$1.H,
getRingPtr: exports$1.I,
getRingLength: exports$1.J,
malloc: exports$1.K,
free: exports$1.L,
heapU8,
heap16,
heap32,
heapF32
});
});
}
function DracoDecoderModule(wasmPromise) {
let HEAPU8, wasmMemory = null;
function updateMemoryViews() {
HEAPU8 = new Uint8Array(wasmMemory.buffer);
}
function abort() {
throw new Error("Unexpected Draco error.");
}
function memcpyBig(dest, src, num) {
return HEAPU8.copyWithin(dest, src, src + num);
}
function resizeHeap(requestedSize) {
const oldSize = HEAPU8.length;
const newSize = Math.max(requestedSize >>> 0, Math.ceil(oldSize * 1.2));
const pages = Math.ceil((newSize - oldSize) / 65536);
try {
wasmMemory.grow(pages);
updateMemoryViews();
return true;
} catch (e) {
return false;
}
}
const wasmImports = {
a: {
a: abort,
d: memcpyBig,
c: resizeHeap,
b: abort
}
};
const instantiateWasm = WebAssembly.instantiateStreaming ? (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
WebAssembly.instantiateStreaming(wasmPromise, wasmImports)
) : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
wasmPromise.then((wasm) => wasm.arrayBuffer()).then((buffer) => WebAssembly.instantiate(buffer, wasmImports))
);
return instantiateWasm.then((output) => {
const {
Rb: _free,
Qb: _malloc,
P: _Mesh,
T: _MeshDestroy,
X: _StatusOK,
Ja: _Decoder,
La: _DecoderDecodeArrayToMesh,
Qa: _DecoderGetAttributeByUniqueId,
Va: _DecoderGetTrianglesUInt16Array,
Wa: _DecoderGetTrianglesUInt32Array,
eb: _DecoderGetAttributeDataArrayForAllPoints,
jb: _DecoderDestroy,
f: initRuntime,
e: memory,
yb: getINT8,
zb: getUINT8,
Ab: getINT16,
Bb: getUINT16,
Db: getUINT32,
Gb: getFLOAT32
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
} = output.instance.exports;
wasmMemory = memory;
const ensureCache = /* @__PURE__ */ (() => {
let buffer = 0;
let size = 0;
let needed = 0;
let temp = 0;
return (array) => {
if (needed) {
_free(temp);
_free(buffer);
size += needed;
needed = buffer = 0;
}
if (!buffer) {
size += 128;
buffer = _malloc(size);
}
const len = array.length + 7 & -8;
let offset = buffer;
if (len >= size) {
needed = len;
offset = temp = _malloc(len);
}
for (let i = 0; i < array.length; i++) {
HEAPU8[offset + i] = array[i];
}
return offset;
};
})();
class Mesh {
constructor() {
this.ptr = _Mesh();
}
destroy() {
_MeshDestroy(this.ptr);
}
}
class Decoder {
constructor() {
this.ptr = _Decoder();
}
destroy() {
_DecoderDestroy(this.ptr);
}
DecodeArrayToMesh(data, dataSize, outMesh) {
const offset = ensureCache(data);
const status = _DecoderDecodeArrayToMesh(this.ptr, offset, dataSize, outMesh.ptr);
return !!_StatusOK(status);
}
GetAttributeByUniqueId(pc, id) {
return { ptr: _DecoderGetAttributeByUniqueId(this.ptr, pc.ptr, id) };
}
GetTrianglesUInt16Array(m, outSize, outValues) {
_DecoderGetTrianglesUInt16Array(this.ptr, m.ptr, outSize, outValues);
}
GetTrianglesUInt32Array(m, outSize, outValues) {
_DecoderGetTrianglesUInt32Array(this.ptr, m.ptr, outSize, outValues);
}
GetAttributeDataArrayForAllPoints(pc, pa, dataType, outSize, outValues) {
_DecoderGetAttributeDataArrayForAllPoints(this.ptr, pc.ptr, pa.ptr, dataType, outSize, outValues);
}
}
updateMemoryViews();
initRuntime();
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
memory,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
_free,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
_malloc,
Mesh,
Decoder,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_INT8: getINT8(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_UINT8: getUINT8(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_INT16: getINT16(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_UINT16: getUINT16(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_UINT32: getUINT32(),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
DT_FLOAT32: getFLOAT32()
};
});
}
function MeshoptDecoder(wasmPromise) {
let instance;
const ready = (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
WebAssembly.instantiateStreaming(wasmPromise, {}).then((result) => {
instance = result.instance;
instance.exports.__wasm_call_ctors();
})
);
function decode(instance2, fun, target, count, size, source, filter) {
const sbrk = instance2.exports.sbrk;
const count4 = count + 3 & ~3;
const tp = sbrk(count4 * size);
const sp = sbrk(source.length);
const heap = new Uint8Array(instance2.exports.memory.buffer);
heap.set(source, sp);
const res = fun(tp, count, size, sp, source.length);
if (res === 0 && filter) {
filter(tp, count4, size);
}
target.set(heap.subarray(tp, tp + count * size));
sbrk(tp - sbrk(0));
if (res !== 0) {
throw new Error(`Malformed buffer data: ${res}`);
}
}
const filters = {
NONE: "",
OCTAHEDRAL: "meshopt_decodeFilterOct",
QUATERNION: "meshopt_decodeFilterQuat",
EXPONENTIAL: "meshopt_decodeFilterExp"
};
const decoders = {
ATTRIBUTES: "meshopt_decodeVertexBuffer",
TRIANGLES: "meshopt_decodeIndexBuffer",
INDICES: "meshopt_decodeIndexSequence"
};
return {
ready,
supported: true,
decodeGltfBuffer(target, count, size, source, mode, filter) {
decode(instance, instance.exports[decoders[mode]], target, count, size, source, instance.exports[filters[filter]]);
}
};
}
let dispatcher = null;
let dracoLoading;
let dracoUrl;
let draco;
let meshoptUrl;
let meshopt;
let buildingGenLoading = null;
let buildingGenError = null;
let buildingGen = null;
function getDracoUrl() {
if (isWorker(self) && self.worker.dracoUrl) {
return self.worker.dracoUrl;
}
return dracoUrl ? dracoUrl : config.DRACO_URL;
}
function setDracoUrl(url) {
dracoUrl = exported$1.resolveURL(url);
if (!dispatcher) {
dispatcher = new Dispatcher(getGlobalWorkerPool(), new Evented());
}
dispatcher.broadcast("setDracoUrl", dracoUrl);
}
function waitForDraco() {
if (draco) return;
if (dracoLoading != null) return dracoLoading;
const startTime = PerformanceUtils.now();
dracoLoading = DracoDecoderModule(fetch(getDracoUrl()));
return dracoLoading.then((module) => {
draco = module;
dracoLoading = void 0;
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "waitForDraco", "Models", startTime);
});
}
function getMeshoptUrl() {
if (isWorker(self) && self.worker.meshoptUrl) {
return self.worker.meshoptUrl;
}
if (meshoptUrl) return meshoptUrl;
const detector = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 3, 2, 0, 0, 5, 3, 1, 0, 1, 12, 1, 0, 10, 22, 2, 12, 0, 65, 0, 65, 0, 65, 0, 252, 10, 0, 0, 11, 7, 0, 65, 0, 253, 15, 26, 11]);
if (typeof WebAssembly !== "object") {
throw new Error("WebAssembly not supported, cannot instantiate meshoptimizer");
}
meshoptUrl = WebAssembly.validate(detector) ? config.MESHOPT_SIMD_URL : config.MESHOPT_URL;
return meshoptUrl;
}
function setMeshoptUrl(url) {
meshoptUrl = exported$1.resolveURL(url);
if (!dispatcher) {
dispatcher = new Dispatcher(getGlobalWorkerPool(), new Evented());
}
dispatcher.broadcast("setMeshoptUrl", meshoptUrl);
}
function waitForMeshopt() {
if (meshopt) return;
const startTime = PerformanceUtils.now();
const decoder = MeshoptDecoder(fetch(getMeshoptUrl()));
return decoder.ready.then(() => {
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "waitForMeshopt", "Models", startTime);
meshopt = decoder;
});
}
function waitForBuildingGen() {
if (buildingGen != null || buildingGenError != null) return null;
if (buildingGenLoading != null) return buildingGenLoading;
const m = PerformanceUtils.now();
const wasmData = fetch(config.BUILDING_GEN_URL);
buildingGenLoading = loadBuildingGen(wasmData).then((instance) => {
buildingGenLoading = null;
buildingGen = instance;
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "waitForBuildingGen", "BuildingBucket", m);
return buildingGen;
}).catch((error) => {
warnOnce("Could not load building-gen");
buildingGenLoading = null;
buildingGenError = error;
});
return buildingGenLoading;
}
function getBuildingGen() {
return buildingGen;
}
const GLTF_BYTE = 5120;
const GLTF_UBYTE = 5121;
const GLTF_SHORT = 5122;
const GLTF_USHORT = 5123;
const GLTF_UINT = 5125;
const GLTF_FLOAT = 5126;
const GLTF_TO_ARRAY_TYPE = {
[GLTF_BYTE]: Int8Array,
[GLTF_UBYTE]: Uint8Array,
[GLTF_SHORT]: Int16Array,
[GLTF_USHORT]: Uint16Array,
[GLTF_UINT]: Uint32Array,
[GLTF_FLOAT]: Float32Array
};
const GLTF_TO_DRACO_TYPE = {
[GLTF_BYTE]: "DT_INT8",
[GLTF_UBYTE]: "DT_UINT8",
[GLTF_SHORT]: "DT_INT16",
[GLTF_USHORT]: "DT_UINT16",
[GLTF_UINT]: "DT_UINT32",
[GLTF_FLOAT]: "DT_FLOAT32"
};
const GLTF_COMPONENTS = {
SCALAR: 1,
VEC2: 2,
VEC3: 3,
VEC4: 4,
MAT2: 4,
MAT3: 9,
MAT4: 16
};
function setAccessorBuffer(buffer, accessor, gltf) {
const bufferViewIndex = gltf.json.bufferViews.length;
const bufferIndex = gltf.buffers.length;
accessor.bufferView = bufferViewIndex;
gltf.json.bufferViews[bufferViewIndex] = {
buffer: bufferIndex,
byteLength: buffer.byteLength
};
gltf.buffers[bufferIndex] = buffer;
}
const DRACO_EXT = "KHR_draco_mesh_compression";
function loadDracoMesh(primitive, gltf) {
const config2 = primitive.extensions && primitive.extensions[DRACO_EXT];
if (!config2) return;
const decoder = new draco.Decoder();
const bytes = getGLTFBytes(gltf, config2.bufferView);
const mesh = new draco.Mesh();
const ok = decoder.DecodeArrayToMesh(bytes, bytes.byteLength, mesh);
if (!ok) throw new Error("Failed to decode Draco mesh");
const indexAccessor = gltf.json.accessors[primitive.indices];
const IndexArrayType = GLTF_TO_ARRAY_TYPE[indexAccessor.componentType];
const indicesSize = indexAccessor.count * IndexArrayType.BYTES_PER_ELEMENT;
const ptr = draco._malloc(indicesSize);
if (IndexArrayType === Uint16Array) {
decoder.GetTrianglesUInt16Array(mesh, indicesSize, ptr);
} else {
decoder.GetTrianglesUInt32Array(mesh, indicesSize, ptr);
}
const indicesBuffer = draco.memory.buffer.slice(ptr, ptr + indicesSize);
setAccessorBuffer(indicesBuffer, indexAccessor, gltf);
draco._free(ptr);
for (const attributeId of Object.keys(config2.attributes)) {
const attribute = decoder.GetAttributeByUniqueId(mesh, config2.attributes[attributeId]);
const accessor = gltf.json.accessors[primitive.attributes[attributeId]];
const ArrayType = GLTF_TO_ARRAY_TYPE[accessor.componentType];
const dracoTypeName = GLTF_TO_DRACO_TYPE[accessor.componentType];
const numComponents = GLTF_COMPONENTS[accessor.type];
const numValues = accessor.count * numComponents;
const dataSize = numValues * ArrayType.BYTES_PER_ELEMENT;
const ptr2 = draco._malloc(dataSize);
decoder.GetAttributeDataArrayForAllPoints(mesh, attribute, draco[dracoTypeName], dataSize, ptr2);
const buffer = draco.memory.buffer.slice(ptr2, ptr2 + dataSize);
setAccessorBuffer(buffer, accessor, gltf);
draco._free(ptr2);
}
decoder.destroy();
mesh.destroy();
delete primitive.extensions[DRACO_EXT];
}
const MESHOPT_EXT = "EXT_meshopt_compression";
function loadMeshoptBuffer(bufferView, gltf) {
if (!(bufferView.extensions && bufferView.extensions[MESHOPT_EXT])) return;
const config2 = bufferView.extensions[MESHOPT_EXT];
const byteOffset = config2.byteOffset || 0;
const byteLength = config2.byteLength || 0;
const buffer = gltf.buffers[config2.buffer];
const source = new Uint8Array(buffer, byteOffset, byteLength);
const target = new Uint8Array(config2.count * config2.byteStride);
meshopt.decodeGltfBuffer(target, config2.count, config2.byteStride, source, config2.mode, config2.filter);
bufferView.buffer = gltf.buffers.length;
bufferView.byteOffset = 0;
gltf.buffers[bufferView.buffer] = target.buffer;
delete bufferView.extensions[MESHOPT_EXT];
}
const MAGIC_GLTF = 1179937895;
const GLB_CHUNK_TYPE_JSON = 1313821514;
const GLB_CHUNK_TYPE_BIN = 5130562;
const textDecoder = new TextDecoder("utf8");
function resolveUrl(url, baseUrl) {
return new URL(url, baseUrl).href;
}
function loadBuffer(buffer, gltf, index, baseUrl) {
return fetch(resolveUrl(buffer.uri, baseUrl)).then((response) => response.arrayBuffer()).then((arrayBuffer) => {
assert$1(arrayBuffer.byteLength >= buffer.byteLength);
gltf.buffers[index] = arrayBuffer;
});
}
function getGLTFBytes(gltf, bufferViewIndex) {
const bufferView = gltf.json.bufferViews[bufferViewIndex];
const buffer = gltf.buffers[bufferView.buffer];
return new Uint8Array(buffer, bufferView.byteOffset || 0, bufferView.byteLength);
}
function loadImage(img, gltf, index, baseUrl) {
if (img.uri) {
const uri = resolveUrl(img.uri, baseUrl);
return fetch(uri).then((response) => response.blob()).then((blob) => createImageBitmap(blob)).then((imageBitmap) => {
gltf.images[index] = imageBitmap;
});
} else if (img.bufferView !== void 0) {
const bytes = getGLTFBytes(gltf, img.bufferView);
const blob = new Blob([bytes], { type: img.mimeType });
return createImageBitmap(blob).then((imageBitmap) => {
gltf.images[index] = imageBitmap;
});
}
}
function decodeGLTF(arrayBuffer, byteOffset = 0, baseUrl) {
const startTime = PerformanceUtils.now();
const gltf = { json: null, images: [], buffers: [] };
if (new Uint32Array(arrayBuffer, byteOffset, 1)[0] === MAGIC_GLTF) {
const view = new Uint32Array(arrayBuffer, byteOffset);
assert$1(view[1] === 2);
let pos = 2;
const glbLen = (view[pos++] >> 2) - 3;
const jsonLen = view[pos++] >> 2;
const jsonType = view[pos++];
assert$1(jsonType === GLB_CHUNK_TYPE_JSON);
gltf.json = JSON.parse(textDecoder.decode(view.subarray(pos, pos + jsonLen)));
pos += jsonLen;
if (pos < glbLen) {
const byteLength = view[pos++];
const binType = view[pos++];
assert$1(binType === GLB_CHUNK_TYPE_BIN);
const start = byteOffset + (pos << 2);
gltf.buffers[0] = arrayBuffer.slice(start, start + byteLength);
}
} else {
gltf.json = JSON.parse(textDecoder.decode(new Uint8Array(arrayBuffer, byteOffset)));
}
const { buffers, images, meshes, extensionsUsed, bufferViews } = gltf.json;
let bufferLoadsPromise = Promise.resolve();
if (buffers) {
const bufferLoads = [];
for (let i = 0; i < buffers.length; i++) {
const buffer = buffers[i];
if (buffer.uri) {
bufferLoads.push(loadBuffer(buffer, gltf, i, baseUrl));
} else if (!gltf.buffers[i]) {
gltf.buffers[i] = null;
}
}
bufferLoadsPromise = Promise.all(bufferLoads);
}
return bufferLoadsPromise.then(() => {
const assetLoads = [];
const dracoUsed = extensionsUsed && extensionsUsed.includes(DRACO_EXT);
const meshoptUsed = extensionsUsed && extensionsUsed.includes(MESHOPT_EXT);
if (dracoUsed) {
assetLoads.push(waitForDraco());
}
if (meshoptUsed) {
assetLoads.push(waitForMeshopt());
}
if (images) {
for (let i = 0; i < images.length; i++) {
assetLoads.push(loadImage(images[i], gltf, i, baseUrl));
}
}
const assetLoadsPromise = assetLoads.length ? Promise.all(assetLoads) : Promise.resolve();
return assetLoadsPromise.then(() => {
if (dracoUsed && meshes) {
for (const { primitives } of meshes) {
for (const primitive of primitives) {
loadDracoMesh(primitive, gltf);
}
}
}
if (meshoptUsed && meshes && bufferViews) {
for (const bufferView of bufferViews) {
loadMeshoptBuffer(bufferView, gltf);
}
}
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "decodeGLTF", "Models", startTime);
return gltf;
});
});
}
function loadGLTF(url) {
return fetch(url).then((response) => response.arrayBuffer()).then((buffer) => decodeGLTF(buffer, 0, url));
}
function load3DTile(data) {
const magic = new Uint32Array(data, 0, 1)[0];
let gltfOffset = 0;
if (magic !== MAGIC_GLTF) {
const header = new Uint32Array(data, 0, 7);
const [
/*magic*/
,
/*version*/
,
byteLen,
featureTableJsonLen,
featureTableBinLen,
batchTableJsonLen
/*, batchTableBinLen*/
] = header;
gltfOffset = header.byteLength + featureTableJsonLen + featureTableBinLen + batchTableJsonLen + featureTableBinLen;
if (byteLen !== data.byteLength || gltfOffset >= data.byteLength) {
warnOnce("Invalid b3dm header information.");
}
}
return decodeGLTF(data, gltfOffset);
}
function _getLegacyFormat(format) {
switch (format) {
case WebGL2RenderingContext["RGBA8"]:
return WebGL2RenderingContext["RGBA"];
case WebGL2RenderingContext["DEPTH_COMPONENT16"]:
return WebGL2RenderingContext["DEPTH_COMPONENT"];
case WebGL2RenderingContext["DEPTH24_STENCIL8"]:
return WebGL2RenderingContext["DEPTH_STENCIL"];
case WebGL2RenderingContext["R8"]:
return WebGL2RenderingContext["RED"];
case WebGL2RenderingContext["R32F"]:
return WebGL2RenderingContext["RED"];
}
}
function _getType(format) {
switch (format) {
case WebGL2RenderingContext["RGBA8"]:
return WebGL2RenderingContext["UNSIGNED_BYTE"];
case WebGL2RenderingContext["DEPTH_COMPONENT16"]:
return WebGL2RenderingContext["UNSIGNED_SHORT"];
case WebGL2RenderingContext["DEPTH24_STENCIL8"]:
return WebGL2RenderingContext["UNSIGNED_INT_24_8"];
case WebGL2RenderingContext["R8"]:
return WebGL2RenderingContext["UNSIGNED_BYTE"];
case WebGL2RenderingContext["R32F"]:
return WebGL2RenderingContext["FLOAT"];
}
}
class Texture {
constructor(context, image, format, options) {
this.context = context;
this.format = format;
this.useMipmap = options && options.useMipmap;
this.texture = context.gl.createTexture();
this.update(image, { premultiply: options && options.premultiply });
}
update(image, options) {
const srcWidth = image && image instanceof HTMLVideoElement && image.width === 0 ? image.videoWidth : image.width;
const srcHeight = image && image instanceof HTMLVideoElement && image.height === 0 ? image.videoHeight : image.height;
const { context } = this;
const { gl } = context;
const { x, y } = options && options.position ? options.position : { x: 0, y: 0 };
const width = x + srcWidth;
const height = y + srcHeight;
if (this.size && (this.size[0] !== width || this.size[1] !== height)) {
gl.bindTexture(gl.TEXTURE_2D, null);
gl.deleteTexture(this.texture);
this.texture = gl.createTexture();
this.size = null;
}
gl.bindTexture(gl.TEXTURE_2D, this.texture);
context.pixelStoreUnpackFlipY.set(false);
context.pixelStoreUnpack.set(1);
context.pixelStoreUnpackPremultiplyAlpha.set(this.format === gl.RGBA8 && (!options || options.premultiply !== false));
const externalImage = image instanceof HTMLImageElement || image instanceof HTMLCanvasElement || image instanceof HTMLVideoElement || image instanceof ImageData || ImageBitmap && image instanceof ImageBitmap;
assert$1(!externalImage || this.format === gl.R8 || this.format === gl.RGBA8, "Texture format needs to be RGBA8 when using external source");
if (!this.size && width > 0 && height > 0) {
const numLevels = this.useMipmap ? Math.floor(Math.log2(Math.max(width, height))) + 1 : 1;
gl.texStorage2D(gl.TEXTURE_2D, numLevels, this.format, width, height);
this.size = [width, height];
}
if (this.size) {
if (externalImage) {
gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, _getLegacyFormat(this.format), _getType(this.format), image);
} else if ("data" in image && image.data) {
gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, srcWidth, srcHeight, _getLegacyFormat(this.format), _getType(this.format), image.data);
}
}
if (this.useMipmap) {
gl.generateMipmap(gl.TEXTURE_2D);
}
}
bind(filter, wrap, ignoreMipMap = false) {
const { context } = this;
const { gl } = context;
gl.bindTexture(gl.TEXTURE_2D, this.texture);
if (filter !== this.minFilter) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
this.useMipmap && !ignoreMipMap ? filter === gl.NEAREST ? gl.NEAREST_MIPMAP_NEAREST : gl.LINEAR_MIPMAP_LINEAR : filter
);
this.minFilter = filter;
}
if (wrap !== this.wrapS) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
this.wrapS = wrap;
}
}
bindExtraParam(minFilter, magFilter, wrapS, wrapT, compareMode) {
const { context } = this;
const { gl } = context;
gl.bindTexture(gl.TEXTURE_2D, this.texture);
if (magFilter !== this.magFilter) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
this.magFilter = magFilter;
}
if (minFilter !== this.minFilter) {
gl.texParameteri(
gl.TEXTURE_2D,
gl.TEXTURE_MIN_FILTER,
this.useMipmap ? minFilter === gl.NEAREST ? gl.NEAREST_MIPMAP_NEAREST : gl.LINEAR_MIPMAP_LINEAR : minFilter
);
this.minFilter = minFilter;
}
if (wrapS !== this.wrapS) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
this.wrapS = wrapS;
}
if (wrapT !== this.wrapT) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
this.wrapT = wrapT;
}
if (compareMode !== this.compareMode) {
if (compareMode) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.COMPARE_REF_TO_TEXTURE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_FUNC, compareMode);
} else {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_COMPARE_MODE, gl.NONE);
}
this.compareMode = compareMode;
}
}
destroy() {
const { gl } = this.context;
gl.deleteTexture(this.texture);
this.texture = null;
}
}
class Texture3D {
constructor(context, image, size, format) {
this.context = context;
this.format = format;
this.size = size;
this.texture = context.gl.createTexture();
const [width, height, depth] = this.size;
const { gl } = context;
gl.bindTexture(gl.TEXTURE_3D, this.texture);
context.pixelStoreUnpackFlipY.set(false);
context.pixelStoreUnpack.set(1);
context.pixelStoreUnpackPremultiplyAlpha.set(false);
assert$1(this.format !== gl.R32F || image instanceof Float32Image);
assert$1(image.width === image.height * image.height);
assert$1(image.height === height);
assert$1(image.width === width * depth);
if ("data" in image && image.data) {
gl.texImage3D(gl.TEXTURE_3D, 0, this.format, width, height, depth, 0, _getLegacyFormat(this.format), _getType(this.format), image.data);
}
}
bind(filter, wrap) {
const { context } = this;
const { gl } = context;
gl.bindTexture(gl.TEXTURE_3D, this.texture);
if (filter !== this.minFilter) {
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, filter);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, filter);
this.minFilter = filter;
}
if (wrap !== this.wrapS) {
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, wrap);
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, wrap);
this.wrapS = wrap;
}
}
destroy() {
const { gl } = this.context;
gl.deleteTexture(this.texture);
this.texture = null;
}
}
class UserManagedTexture {
constructor(context, texture) {
this.context = context;
this.texture = texture;
}
bind(filter, wrap) {
const { context } = this;
const { gl } = context;
gl.bindTexture(gl.TEXTURE_2D, this.texture);
if (filter !== this.minFilter) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
this.minFilter = filter;
}
if (wrap !== this.wrapS) {
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
this.wrapS = wrap;
}
}
}
const modelAttributes = createLayout([
{ name: "a_pos_3f", components: 3, type: "Float32" }
]);
const color3fAttributes = createLayout([
{ name: "a_color_3f", components: 3, type: "Float32" }
]);
const color4fAttributes = createLayout([
{ name: "a_color_4f", components: 4, type: "Float32" }
]);
const texcoordAttributes = createLayout([
{ name: "a_uv_2f", components: 2, type: "Float32" }
]);
const normalAttributes = createLayout([
{ name: "a_normal_3f", components: 3, type: "Float32" }
]);
const instanceAttributes = createLayout([
{ name: "a_normal_matrix0", components: 4, type: "Float32" },
{ name: "a_normal_matrix1", components: 4, type: "Float32" },
{ name: "a_normal_matrix2", components: 4, type: "Float32" },
{ name: "a_normal_matrix3", components: 4, type: "Float32" }
]);
const featureAttributes = createLayout([
// pbr encoding: | color.rgba (4 bytes) | emissivity (a byte) | roughness (a nibble) | metallic (a nibble)
{ name: "a_pbr", components: 4, type: "Uint16" },
{ name: "a_heightBasedEmissiveStrength", components: 3, type: "Float32" }
]);
const { members: members$4, size: size$4, alignment: alignment$4 } = modelAttributes;
function getProjectionAdjustments(transform, withoutRotation) {
const interpT = getProjectionInterpolationT(transform.projection, transform.zoom, transform.width, transform.height);
const matrix = getShearAdjustment(transform.projection, transform.zoom, transform.center, interpT, withoutRotation);
const scaleAdjustment = getScaleAdjustment(transform);
scale$5(matrix, matrix, [scaleAdjustment, scaleAdjustment, 1]);
return matrix;
}
function getScaleAdjustment(transform) {
const projection = transform.projection;
const interpT = getProjectionInterpolationT(transform.projection, transform.zoom, transform.width, transform.height);
const zoomAdjustment = getZoomAdjustment(projection, transform.center);
const zoomAdjustmentOrigin = getZoomAdjustment(projection, LngLat.convert(projection.center));
const scaleAdjustment = Math.pow(2, zoomAdjustment * interpT + (1 - interpT) * zoomAdjustmentOrigin);
return scaleAdjustment;
}
function getProjectionAdjustmentInverted(transform) {
const m = getProjectionAdjustments(transform, true);
return invert$5(
[],
[
m[0],
m[1],
m[4],
m[5]
]
);
}
function getProjectionInterpolationT(projection, zoom, width, height, maxSize = Infinity) {
const range = projection.range;
if (!range) return 0;
const size = Math.min(maxSize, Math.max(width, height));
const rangeAdjustment = Math.log2(size / 1024);
const zoomA = range[0] + rangeAdjustment;
const zoomB = range[1] + rangeAdjustment;
const t = smoothstep(zoomA, zoomB, zoom);
return t;
}
const offset = 1 / 4e4;
function getZoomAdjustment(projection, loc) {
const lat = clamp(loc.lat, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
const loc1 = new LngLat(loc.lng - 180 * offset, lat);
const loc2 = new LngLat(loc.lng + 180 * offset, lat);
const p1 = projection.project(loc1.lng, lat);
const p2 = projection.project(loc2.lng, lat);
const m1 = MercatorCoordinate.fromLngLat(loc1);
const m2 = MercatorCoordinate.fromLngLat(loc2);
const pdx = p2.x - p1.x;
const pdy = p2.y - p1.y;
const mdx = m2.x - m1.x;
const mdy = m2.y - m1.y;
const scale = Math.sqrt((mdx * mdx + mdy * mdy) / (pdx * pdx + pdy * pdy));
return Math.log2(scale);
}
function getShearAdjustment(projection, zoom, loc, interpT, withoutRotation) {
const locw = new LngLat(loc.lng - 180 * offset, loc.lat);
const loce = new LngLat(loc.lng + 180 * offset, loc.lat);
const pw = projection.project(locw.lng, locw.lat);
const pe = projection.project(loce.lng, loce.lat);
const pdx = pe.x - pw.x;
const pdy = pe.y - pw.y;
const angleAdjust = -Math.atan2(pdy, pdx);
const mc2 = MercatorCoordinate.fromLngLat(loc);
mc2.y = clamp(mc2.y, -1 + offset, 1 - offset);
const loc2 = mc2.toLngLat();
const p2 = projection.project(loc2.lng, loc2.lat);
const mc3 = MercatorCoordinate.fromLngLat(loc2);
mc3.x += offset;
const loc3 = mc3.toLngLat();
const p3 = projection.project(loc3.lng, loc3.lat);
const pdx3 = p3.x - p2.x;
const pdy3 = p3.y - p2.y;
const delta3 = rotate(pdx3, pdy3, angleAdjust);
const mc4 = MercatorCoordinate.fromLngLat(loc2);
mc4.y += offset;
const loc4 = mc4.toLngLat();
const p4 = projection.project(loc4.lng, loc4.lat);
const pdx4 = p4.x - p2.x;
const pdy4 = p4.y - p2.y;
const delta4 = rotate(pdx4, pdy4, angleAdjust);
const scale = Math.abs(delta3.x) / Math.abs(delta4.y);
const unrotate = identity$3([]);
rotateZ$3(unrotate, unrotate, -angleAdjust * (1 - (withoutRotation ? 0 : interpT)));
const shear = identity$3([]);
scale$5(shear, shear, [1, 1 - (1 - scale) * interpT, 1]);
shear[4] = -delta4.x / delta4.y * interpT;
rotateZ$3(shear, shear, angleAdjust);
multiply$5(shear, unrotate, shear);
return shear;
}
function rotate(x, y, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
return {
x: x * cos - y * sin,
y: x * sin + y * cos
};
}
function rotationScaleYZFlipMatrix(out, rotation, scale) {
identity$3(out);
rotateZ$3(out, out, degToRad(rotation[2]));
rotateX$3(out, out, degToRad(rotation[0]));
rotateY$3(out, out, degToRad(rotation[1]));
scale$5(out, out, scale);
const coordSpaceTransform = [
1,
0,
0,
0,
0,
0,
1,
0,
0,
1,
0,
0,
0,
0,
0,
1
];
multiply$5(out, out, coordSpaceTransform);
}
function getBoxBottomFace(corners, meterToMercator) {
const zUp = [0, 0, 1];
const boxFaces = [
{ corners: [0, 1, 3, 2], dotProductWithUp: 0 },
{ corners: [1, 5, 2, 6], dotProductWithUp: 0 },
{ corners: [0, 4, 1, 5], dotProductWithUp: 0 },
{ corners: [2, 6, 3, 7], dotProductWithUp: 0 },
{ corners: [4, 7, 5, 6], dotProductWithUp: 0 },
{ corners: [0, 3, 4, 7], dotProductWithUp: 0 }
];
for (const face of boxFaces) {
const p0 = corners[face.corners[0]];
const p1 = corners[face.corners[1]];
const p2 = corners[face.corners[2]];
const a = [p1[0] - p0[0], p1[1] - p0[1], meterToMercator * (p1[2] - p0[2])];
const b = [p2[0] - p0[0], p2[1] - p0[1], meterToMercator * (p2[2] - p0[2])];
const normal = cross$2(a, a, b);
normalize$4(normal, normal);
face.dotProductWithUp = dot$5(normal, zUp);
}
boxFaces.sort((a, b) => {
return a.dotProductWithUp - b.dotProductWithUp;
});
return boxFaces[0].corners;
}
function rotationFor3Points(out, p0, p1, p2, h0, h1, h2, meterToMercator) {
const p0p1 = [p1[0] - p0[0], p1[1] - p0[1], 0];
const p0p2 = [p2[0] - p0[0], p2[1] - p0[1], 0];
if (length$4(p0p1) < 1e-12 || length$4(p0p2) < 1e-12) {
return identity$2(out);
}
const from = cross$2([], p0p1, p0p2);
normalize$4(from, from);
subtract$2(p0p2, p2, p0);
p0p1[2] = (h1 - h0) * meterToMercator;
p0p2[2] = (h2 - h0) * meterToMercator;
const to = p0p1;
cross$2(to, p0p1, p0p2);
normalize$4(to, to);
return rotationTo(out, from, to);
}
function coordinateFrameAtEcef(ecef) {
const zAxis = [ecef[0], ecef[1], ecef[2]];
let yAxis = [0, 1, 0];
const xAxis = cross$2([], yAxis, zAxis);
cross$2(yAxis, zAxis, xAxis);
if (squaredLength$4(yAxis) === 0) {
yAxis = [0, 1, 0];
cross$2(xAxis, zAxis, yAxis);
assert$1(squaredLength$4(xAxis) > 0);
}
normalize$4(xAxis, xAxis);
normalize$4(yAxis, yAxis);
normalize$4(zAxis, zAxis);
return [
xAxis[0],
xAxis[1],
xAxis[2],
0,
yAxis[0],
yAxis[1],
yAxis[2],
0,
zAxis[0],
zAxis[1],
zAxis[2],
0,
ecef[0],
ecef[1],
ecef[2],
1
];
}
function convertModelMatrix(matrix, transform, scaleWithViewport) {
const worldSize = transform.worldSize;
const position = [matrix[12], matrix[13], matrix[14]];
const lat = latFromMercatorY(position[1] / worldSize);
const lng = lngFromMercatorX(position[0] / worldSize);
const mercToEcef = identity$3([]);
const sourcePixelsPerMeter = mercatorZfromAltitude(1, lat) * worldSize;
const pixelsPerMeterConversion = mercatorZfromAltitude(1, 0) * worldSize * getMetersPerPixelAtLatitude(lat, transform.zoom);
const pixelsToEcef = 1 / globeECEFUnitsToPixelScale(worldSize);
let scale = pixelsPerMeterConversion * pixelsToEcef;
if (scaleWithViewport) {
const t = getProjectionInterpolationT(transform.projection, transform.zoom, transform.width, transform.height, 1024);
const projectionScaler = transform.projection.pixelSpaceConversion(transform.center.lat, worldSize, t);
scale = pixelsToEcef * projectionScaler;
}
const ecefCoord = latLngToECEF(lat, lng);
add$4(ecefCoord, ecefCoord, scale$4([], normalize$4([], ecefCoord), sourcePixelsPerMeter * scale * position[2]));
const ecefFrame = coordinateFrameAtEcef(ecefCoord);
scale$5(mercToEcef, mercToEcef, [scale, scale, scale * sourcePixelsPerMeter]);
translate$2(mercToEcef, mercToEcef, [-position[0], -position[1], -position[2]]);
const result = multiply$5([], transform.globeMatrix, ecefFrame);
multiply$5(result, result, mercToEcef);
multiply$5(result, result, matrix);
return result;
}
function mercatorToGlobeMatrix(matrix, transform) {
const worldSize = transform.worldSize;
const pixelsPerMeterConversion = mercatorZfromAltitude(1, 0) * worldSize * getMetersPerPixelAtLatitude(transform.center.lat, transform.zoom);
const pixelsToEcef = pixelsPerMeterConversion / globeECEFUnitsToPixelScale(worldSize);
const pixelsPerMeter = mercatorZfromAltitude(1, transform.center.lat) * worldSize;
const m = identity$3([]);
rotateY$3(m, m, degToRad(transform.center.lng));
rotateX$3(m, m, degToRad(transform.center.lat));
translate$2(m, m, [0, 0, GLOBE_RADIUS]);
scale$5(m, m, [pixelsToEcef, pixelsToEcef, pixelsToEcef * pixelsPerMeter]);
const transformPoint = transform.point;
translate$2(m, m, [-transformPoint.x, -transformPoint.y, 0]);
multiply$5(m, m, matrix);
return multiply$5(m, transform.globeMatrix, m);
}
function affineMatrixLerp(a, b, t) {
const lerpAxis = (ax, bx, t2) => {
const axLen = length$4(ax);
const bxLen = length$4(bx);
const c = interpolateVec3(ax, bx, t2);
return scale$4(c, c, 1 / length$4(c) * number(axLen, bxLen, t2));
};
const xAxis = lerpAxis([a[0], a[1], a[2]], [b[0], b[1], b[2]], t);
const yAxis = lerpAxis([a[4], a[5], a[6]], [b[4], b[5], b[6]], t);
const zAxis = lerpAxis([a[8], a[9], a[10]], [b[8], b[9], b[10]], t);
const pos = interpolateVec3([a[12], a[13], a[14]], [b[12], b[13], b[14]], t);
return [
xAxis[0],
xAxis[1],
xAxis[2],
0,
yAxis[0],
yAxis[1],
yAxis[2],
0,
zAxis[0],
zAxis[1],
zAxis[2],
0,
pos[0],
pos[1],
pos[2],
1
];
}
function convertModelMatrixForGlobe(matrix, transform, scaleWithViewport = false) {
const t = globeToMercatorTransition(transform.zoom);
const modelMatrix = convertModelMatrix(matrix, transform, scaleWithViewport);
if (t > 0) {
const mercatorMatrix = mercatorToGlobeMatrix(matrix, transform);
return affineMatrixLerp(modelMatrix, mercatorMatrix, t);
}
return modelMatrix;
}
function convexPolygonFromPoints(points) {
assert$1(points.length >= 3, "Polygon must have at least 3 points");
const ring = [];
let leftmostIdx = 0;
for (let i = 1; i < points.length; i++) {
if (points[i][0] < points[leftmostIdx][0] || points[i][0] === points[leftmostIdx][0] && points[i][1] < points[leftmostIdx][1]) {
leftmostIdx = i;
}
}
let current = leftmostIdx;
let next;
const visitedMap = new Uint8Array(points.length);
do {
if (visitedMap[current]) {
break;
}
ring.push(new Point(points[current][0], points[current][1]));
visitedMap[current] = 1;
next = (current + 1) % points.length;
for (let i = 0; i < points.length; i++) {
if (points[i][0] === points[next][0] && points[i][1] === points[next][1] || points[i][0] === points[current][0] && points[i][1] === points[current][1]) {
continue;
}
const a = [points[i][0] - points[current][0], points[i][1] - points[current][1]];
const b = [points[next][0] - points[current][0], points[next][1] - points[current][1]];
const det = a[0] * b[1] - a[1] * b[0];
const dir = a[0] * b[0] + a[1] * b[1];
if (det > 0 || det === 0 && dir >= 0 && a[0] * a[0] + a[1] * a[1] > b[0] * b[0] + b[1] * b[1]) {
next = i;
}
}
current = next;
} while (current !== leftmostIdx);
if (ring.length > 0) {
ring.push(ring[0]);
}
return ring;
}
function queryGeometryIntersectsProjectedAabb(queryGeometry, transform, worldViewProjection, aabb) {
const corners = Aabb.projectAabbCorners(aabb, worldViewProjection);
let minDepth = Number.MAX_VALUE;
for (let c = 0; c < corners.length; ++c) {
const corner = corners[c];
corner[0] = (0.5 * corner[0] + 0.5) * transform.width;
corner[1] = (0.5 - 0.5 * corner[1]) * transform.height;
if (corner[2] < minDepth) {
minDepth = corner[2];
}
}
const convexPolygon = convexPolygonFromPoints(corners);
if (polygonIntersectsPolygon(queryGeometry, convexPolygon)) {
return minDepth;
}
}
const HEIGHTMAP_DIM = 64;
const ModelTraits = {
CoordinateSpaceTile: 1,
CoordinateSpaceYUp: 2,
// not used yet.
HasMapboxMeshFeatures: 1 << 2,
HasMeshoptCompression: 1 << 3
};
const DefaultModelScale = [1, 1, 1];
function positionModelOnTerrain(rotationOnTerrain, transform, aabb, matrix, position) {
const elevation = transform.elevation;
if (!elevation) {
return 0;
}
const corners = Aabb.projectAabbCorners(aabb, matrix);
const meterToMercator = mercatorZfromAltitude(1, position.lat) * transform.worldSize;
const bottomFace = getBoxBottomFace(corners, meterToMercator);
const b0 = corners[bottomFace[0]];
const b1 = corners[bottomFace[1]];
const b2 = corners[bottomFace[2]];
const b3 = corners[bottomFace[3]];
const e0 = elevation.getAtPointOrZero(new MercatorCoordinate(b0[0] / transform.worldSize, b0[1] / transform.worldSize), 0);
const e1 = elevation.getAtPointOrZero(new MercatorCoordinate(b1[0] / transform.worldSize, b1[1] / transform.worldSize), 0);
const e2 = elevation.getAtPointOrZero(new MercatorCoordinate(b2[0] / transform.worldSize, b2[1] / transform.worldSize), 0);
const e3 = elevation.getAtPointOrZero(new MercatorCoordinate(b3[0] / transform.worldSize, b3[1] / transform.worldSize), 0);
const d03 = (e0 + e3) / 2;
const d12 = (e1 + e2) / 2;
if (d03 > d12) {
if (e1 < e2) {
rotationFor3Points(rotationOnTerrain, b1, b3, b0, e1, e3, e0, meterToMercator);
} else {
rotationFor3Points(rotationOnTerrain, b2, b0, b3, e2, e0, e3, meterToMercator);
}
} else {
if (e0 < e3) {
rotationFor3Points(rotationOnTerrain, b0, b1, b2, e0, e1, e2, meterToMercator);
} else {
rotationFor3Points(rotationOnTerrain, b3, b2, b1, e3, e2, e1, meterToMercator);
}
}
return Math.max(d03, d12);
}
function calculateModelMatrix(matrix, model, state, position, rotation, scale, translation, applyElevation, followTerrainSlope, viewportScale = false) {
const zoom = state.zoom;
const projectedPoint = state.project(position);
const modelMetersPerPixel = getMetersPerPixelAtLatitude(position.lat, zoom);
const modelPixelsPerMeter = 1 / modelMetersPerPixel;
identity$3(matrix);
const offset = [projectedPoint.x + translation[0] * modelPixelsPerMeter, projectedPoint.y + translation[1] * modelPixelsPerMeter, translation[2]];
translate$2(matrix, matrix, offset);
let scaleXY = 1;
let scaleZ = 1;
const worldSize = state.worldSize;
if (viewportScale) {
if (state.projection.name === "mercator") {
let elevation = 0;
if (state.elevation) {
elevation = state.elevation.getAtPointOrZero(new MercatorCoordinate(projectedPoint.x / worldSize, projectedPoint.y / worldSize), 0);
}
const mercProjPos = transformMat4$1([], [projectedPoint.x, projectedPoint.y, elevation, 1], state.projMatrix);
const mercProjectionScale = mercProjPos[3] / state.cameraToCenterDistance;
const viewMetersPerPixel = getMetersPerPixelAtLatitude(state.center.lat, zoom);
scaleXY = mercProjectionScale;
scaleZ = mercProjectionScale * viewMetersPerPixel;
} else if (state.projection.name === "globe") {
const globeMatrix = convertModelMatrixForGlobe(matrix, state);
const worldViewProjection = multiply$5([], state.projMatrix, globeMatrix);
const globeProjPos = [0, 0, 0, 1];
transformMat4$1(globeProjPos, globeProjPos, worldViewProjection);
const globeProjectionScale = globeProjPos[3] / state.cameraToCenterDistance;
const transition = globeToMercatorTransition(zoom);
const modelPixelConv = state.projection.pixelsPerMeter(position.lat, worldSize) * getMetersPerPixelAtLatitude(position.lat, zoom);
const viewPixelConv = state.projection.pixelsPerMeter(state.center.lat, worldSize) * getMetersPerPixelAtLatitude(state.center.lat, zoom);
const viewLatScale = getLatitudeScale(state.center.lat);
scaleXY = globeProjectionScale / number(modelPixelConv, viewLatScale, transition);
scaleZ = globeProjectionScale * modelMetersPerPixel / modelPixelConv;
scaleXY *= viewPixelConv;
scaleZ *= viewPixelConv;
}
} else {
scaleXY = modelPixelsPerMeter;
}
scale$5(matrix, matrix, [scaleXY, scaleXY, scaleZ]);
const modelMatrixBeforeRotationScaleYZFlip = [...matrix];
const orientation = model.orientation;
const rotationScaleYZFlip = [];
rotationScaleYZFlipMatrix(
rotationScaleYZFlip,
[
orientation[0] + (rotation ? rotation[0] : 0),
orientation[1] + (rotation ? rotation[1] : 0),
orientation[2] + (rotation ? rotation[2] : 0)
],
scale
);
multiply$5(matrix, modelMatrixBeforeRotationScaleYZFlip, rotationScaleYZFlip);
if (applyElevation && state.elevation) {
let elevate = 0;
const rotateOnTerrain = [];
if (followTerrainSlope && state.elevation) {
elevate = positionModelOnTerrain(rotateOnTerrain, state, model.aabb, matrix, position);
const rotationOnTerrain = fromQuat([], rotateOnTerrain);
const appendRotation = multiply$5([], rotationOnTerrain, rotationScaleYZFlip);
multiply$5(matrix, modelMatrixBeforeRotationScaleYZFlip, appendRotation);
} else {
elevate = state.elevation.getAtPointOrZero(new MercatorCoordinate(projectedPoint.x / worldSize, projectedPoint.y / worldSize), 0);
}
if (elevate !== 0) {
matrix[14] += elevate;
}
}
}
function rotationYZX(out, rotation) {
identity$3(out);
rotateY$3(out, out, degToRad(rotation[1]));
rotateZ$3(out, out, degToRad(rotation[2]));
rotateX$3(out, out, degToRad(rotation[0]));
}
class Model {
constructor(id, uri, position, orientation, nodes) {
this.materialOverrides = /* @__PURE__ */ new Map();
this.nodeOverrides = /* @__PURE__ */ new Map();
this.materialOverrideNames = [];
this.nodeOverrideNames = [];
this.featureProperties = {};
this.id = id;
this.uri = uri;
this.position = position != null ? new LngLat(position[0], position[1]) : new LngLat(0, 0);
this.orientation = orientation != null ? orientation : [0, 0, 0];
this.nodes = nodes;
this.uploaded = false;
this.aabb = new Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
this.matrix = [];
}
_applyTransformations(node, parentMatrix) {
multiply$5(node.globalMatrix, parentMatrix, node.localMatrix);
const nodeOverride = this.nodeOverrides.get(node.name);
if (nodeOverride !== void 0) {
const m = [];
rotationYZX(m, nodeOverride.orientation);
multiply$5(node.globalMatrix, node.globalMatrix, m);
}
if (node.meshes) {
for (const mesh of node.meshes) {
const enclosingBounds = Aabb.applyTransformFast(mesh.aabb, node.globalMatrix);
this.aabb.encapsulate(enclosingBounds);
}
}
if (node.children) {
for (const child of node.children) {
this._applyTransformations(child, node.globalMatrix);
}
}
}
computeBoundsAndApplyParent() {
const localMatrix = identity$3([]);
this.aabb = new Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
for (const node of this.nodes) {
this._applyTransformations(node, localMatrix);
}
}
computeModelMatrix(painter, rotation, scale, translation, applyElevation, followTerrainSlope, viewportScale = false) {
calculateModelMatrix(this.matrix, this, painter.transform, this.position, rotation, scale, translation, applyElevation, followTerrainSlope, viewportScale);
}
upload(context) {
if (this.uploaded) return;
for (const node of this.nodes) {
uploadNode(node, context);
}
for (const node of this.nodes) {
destroyNodeArrays(node);
}
this.uploaded = true;
}
destroy() {
for (const node of this.nodes) {
destroyBuffers(node);
}
}
}
function uploadTexture(texture, context, useSingleChannelTexture = false) {
const textureFormat = useSingleChannelTexture ? context.gl.R8 : context.gl.RGBA8;
if (!texture.uploaded) {
const useMipmap = texture.sampler.minFilter >= context.gl.NEAREST_MIPMAP_NEAREST;
texture.gfxTexture = new Texture(context, texture.image, textureFormat, { useMipmap });
texture.uploaded = true;
texture.image = null;
}
}
function uploadMesh(mesh, context, useSingleChannelOcclusionTexture) {
mesh.indexBuffer = context.createIndexBuffer(mesh.indexArray, false, true);
mesh.vertexBuffer = context.createVertexBuffer(mesh.vertexArray, modelAttributes.members, false, true);
if (mesh.normalArray) {
mesh.normalBuffer = context.createVertexBuffer(mesh.normalArray, normalAttributes.members, false, true);
}
if (mesh.texcoordArray) {
mesh.texcoordBuffer = context.createVertexBuffer(mesh.texcoordArray, texcoordAttributes.members, false, true);
}
if (mesh.colorArray) {
const colorAttributes = mesh.colorArray.bytesPerElement === 12 ? color3fAttributes : color4fAttributes;
mesh.colorBuffer = context.createVertexBuffer(mesh.colorArray, colorAttributes.members, false, true);
}
if (mesh.featureArray) {
mesh.pbrBuffer = context.createVertexBuffer(mesh.featureArray, featureAttributes.members, true);
}
mesh.segments = SegmentVector.simpleSegment(0, 0, mesh.vertexArray.length, mesh.indexArray.length);
const material = mesh.material;
if (material.pbrMetallicRoughness.baseColorTexture) {
uploadTexture(material.pbrMetallicRoughness.baseColorTexture, context);
}
if (material.pbrMetallicRoughness.metallicRoughnessTexture) {
uploadTexture(material.pbrMetallicRoughness.metallicRoughnessTexture, context);
}
if (material.normalTexture) {
uploadTexture(material.normalTexture, context);
}
if (material.occlusionTexture) {
uploadTexture(material.occlusionTexture, context, useSingleChannelOcclusionTexture);
}
if (material.emissionTexture) {
uploadTexture(material.emissionTexture, context);
}
}
function uploadNode(node, context, useSingleChannelOcclusionTexture) {
if (node.meshes) {
for (const mesh of node.meshes) {
uploadMesh(mesh, context, useSingleChannelOcclusionTexture);
}
}
if (node.children) {
for (const child of node.children) {
uploadNode(child, context, useSingleChannelOcclusionTexture);
}
}
}
function destroyNodeArrays(node) {
if (node.meshes) {
for (const mesh of node.meshes) {
mesh.indexArray.destroy();
mesh.vertexArray.destroy();
if (mesh.colorArray) mesh.colorArray.destroy();
if (mesh.normalArray) mesh.normalArray.destroy();
if (mesh.texcoordArray) mesh.texcoordArray.destroy();
if (mesh.featureArray) {
mesh.featureArray.destroy();
}
}
}
if (node.children) {
for (const child of node.children) {
destroyNodeArrays(child);
}
}
}
function destroyTextures(material) {
if (material.pbrMetallicRoughness.baseColorTexture && material.pbrMetallicRoughness.baseColorTexture.gfxTexture) {
material.pbrMetallicRoughness.baseColorTexture.gfxTexture.destroy();
}
if (material.pbrMetallicRoughness.metallicRoughnessTexture && material.pbrMetallicRoughness.metallicRoughnessTexture.gfxTexture) {
material.pbrMetallicRoughness.metallicRoughnessTexture.gfxTexture.destroy();
}
if (material.normalTexture && material.normalTexture.gfxTexture) {
material.normalTexture.gfxTexture.destroy();
}
if (material.emissionTexture && material.emissionTexture.gfxTexture) {
material.emissionTexture.gfxTexture.destroy();
}
if (material.occlusionTexture && material.occlusionTexture.gfxTexture) {
material.occlusionTexture.gfxTexture.destroy();
}
}
function destroyBuffers(node) {
if (node.meshes) {
for (const mesh of node.meshes) {
if (!mesh.vertexBuffer) continue;
mesh.vertexBuffer.destroy();
mesh.indexBuffer.destroy();
if (mesh.normalBuffer) {
mesh.normalBuffer.destroy();
}
if (mesh.texcoordBuffer) {
mesh.texcoordBuffer.destroy();
}
if (mesh.colorBuffer) {
mesh.colorBuffer.destroy();
}
if (mesh.pbrBuffer) {
mesh.pbrBuffer.destroy();
}
mesh.segments.destroy();
if (mesh.material) {
destroyTextures(mesh.material);
}
}
}
if (node.children) {
for (const child of node.children) {
destroyBuffers(child);
}
}
}
function convertTextures(gltf, images) {
const textures = [];
const gl = WebGL2RenderingContext;
if (gltf.json.textures) {
for (const textureDesc of gltf.json.textures) {
const sampler = {
magFilter: gl.LINEAR,
minFilter: gl.NEAREST,
wrapS: gl.REPEAT,
wrapT: gl.REPEAT
};
if (textureDesc.sampler !== void 0) Object.assign(sampler, gltf.json.samplers[textureDesc.sampler]);
textures.push({
image: images[textureDesc.source],
sampler,
uploaded: false
});
}
}
return textures;
}
function convertMaterial(materialDesc, textures) {
const {
emissiveFactor = [0, 0, 0],
alphaMode = "OPAQUE",
alphaCutoff = 0.5,
normalTexture,
occlusionTexture,
emissiveTexture,
doubleSided,
name
} = materialDesc;
const {
baseColorFactor = [1, 1, 1, 1],
metallicFactor = 1,
roughnessFactor = 1,
baseColorTexture,
metallicRoughnessTexture
} = materialDesc.pbrMetallicRoughness || {};
const modelOcclusionTexture = occlusionTexture ? textures[occlusionTexture.index] : void 0;
if (occlusionTexture && occlusionTexture.extensions && occlusionTexture.extensions["KHR_texture_transform"] && modelOcclusionTexture) {
const transform = occlusionTexture.extensions["KHR_texture_transform"];
modelOcclusionTexture.offsetScale = [transform.offset[0], transform.offset[1], transform.scale[0], transform.scale[1]];
}
return {
name,
pbrMetallicRoughness: {
baseColorFactor: new Color(...baseColorFactor),
metallicFactor,
roughnessFactor,
baseColorTexture: baseColorTexture ? textures[baseColorTexture.index] : void 0,
metallicRoughnessTexture: metallicRoughnessTexture ? textures[metallicRoughnessTexture.index] : void 0
},
doubleSided,
emissiveFactor: new Color(...emissiveFactor),
alphaMode,
alphaCutoff,
normalTexture: normalTexture ? textures[normalTexture.index] : void 0,
occlusionTexture: modelOcclusionTexture,
emissionTexture: emissiveTexture ? textures[emissiveTexture.index] : void 0,
defined: materialDesc.defined === void 0
// just to make the rendertests the same than native
};
}
function computeCentroid(indexArray, vertexArray) {
const out = [0, 0, 0];
const indexSize = indexArray.length;
if (indexSize > 0) {
for (let i = 0; i < indexSize; i++) {
const index = indexArray[i] * 3;
out[0] += vertexArray[index];
out[1] += vertexArray[index + 1];
out[2] += vertexArray[index + 2];
}
out[0] /= indexSize;
out[1] /= indexSize;
out[2] /= indexSize;
}
return out;
}
function getNormalizedScale(arrayType) {
switch (arrayType) {
case Int8Array:
return 1 / 127;
case Uint8Array:
return 1 / 255;
case Int16Array:
return 1 / 32767;
case Uint16Array:
return 1 / 65535;
default:
return 1;
}
}
function getBufferData(gltf, accessor) {
const bufferView = gltf.json.bufferViews[accessor.bufferView];
const buffer = gltf.buffers[bufferView.buffer];
const offset = (accessor.byteOffset || 0) + (bufferView.byteOffset || 0);
const ArrayType = GLTF_TO_ARRAY_TYPE[accessor.componentType];
const itemBytes = GLTF_COMPONENTS[accessor.type] * ArrayType.BYTES_PER_ELEMENT;
const stride = bufferView.byteStride && bufferView.byteStride !== itemBytes ? bufferView.byteStride / ArrayType.BYTES_PER_ELEMENT : GLTF_COMPONENTS[accessor.type];
const bufferData = new ArrayType(buffer, offset, accessor.count * stride);
return bufferData;
}
function setArrayData(gltf, accessor, array, buffer) {
const ArrayType = GLTF_TO_ARRAY_TYPE[accessor.componentType];
const norm = getNormalizedScale(ArrayType);
const bufferView = gltf.json.bufferViews[accessor.bufferView];
const numElements = bufferView.byteStride ? bufferView.byteStride / ArrayType.BYTES_PER_ELEMENT : GLTF_COMPONENTS[accessor.type];
const float32Array = array.float32;
const components = float32Array.length / array.capacity;
for (let i = 0, count = 0; i < accessor.count * numElements; i += numElements, count += components) {
for (let j = 0; j < components; j++) {
float32Array[count + j] = buffer[i + j] * norm;
}
}
array._trim();
}
function convertPrimitive(primitive, gltf, textures) {
const indicesIdx = primitive.indices;
const attributeMap = primitive.attributes;
const mesh = {};
mesh.indexArray = new StructArrayLayout3ui6();
const indexAccessor = gltf.json.accessors[indicesIdx];
const numTriangles = indexAccessor.count / 3;
mesh.indexArray.reserve(numTriangles);
const indexArrayBuffer = getBufferData(gltf, indexAccessor);
for (let i = 0; i < numTriangles; i++) {
mesh.indexArray.emplaceBack(indexArrayBuffer[i * 3], indexArrayBuffer[i * 3 + 1], indexArrayBuffer[i * 3 + 2]);
}
mesh.indexArray._trim();
mesh.vertexArray = new StructArrayLayout3f12();
const positionAccessor = gltf.json.accessors[attributeMap.POSITION];
mesh.vertexArray.reserve(positionAccessor.count);
const vertexArrayBuffer = getBufferData(gltf, positionAccessor);
for (let i = 0; i < positionAccessor.count; i++) {
mesh.vertexArray.emplaceBack(vertexArrayBuffer[i * 3], vertexArrayBuffer[i * 3 + 1], vertexArrayBuffer[i * 3 + 2]);
}
mesh.vertexArray._trim();
mesh.aabb = new Aabb(positionAccessor.min, positionAccessor.max);
mesh.centroid = computeCentroid(indexArrayBuffer, vertexArrayBuffer);
if (attributeMap.COLOR_0 !== void 0) {
const colorAccessor = gltf.json.accessors[attributeMap.COLOR_0];
const numElements = GLTF_COMPONENTS[colorAccessor.type];
const colorArrayBuffer = getBufferData(gltf, colorAccessor);
mesh.colorArray = numElements === 3 ? new StructArrayLayout3f12() : new StructArrayLayout4f16();
mesh.colorArray.resize(colorAccessor.count);
setArrayData(gltf, colorAccessor, mesh.colorArray, colorArrayBuffer);
}
if (attributeMap.NORMAL !== void 0) {
mesh.normalArray = new StructArrayLayout3f12();
const normalAccessor = gltf.json.accessors[attributeMap.NORMAL];
mesh.normalArray.resize(normalAccessor.count);
const normalArrayBuffer = getBufferData(gltf, normalAccessor);
setArrayData(gltf, normalAccessor, mesh.normalArray, normalArrayBuffer);
}
if (attributeMap.TEXCOORD_0 !== void 0 && textures.length > 0) {
mesh.texcoordArray = new StructArrayLayout2f8();
const texcoordAccessor = gltf.json.accessors[attributeMap.TEXCOORD_0];
mesh.texcoordArray.resize(texcoordAccessor.count);
const texcoordArrayBuffer = getBufferData(gltf, texcoordAccessor);
setArrayData(gltf, texcoordAccessor, mesh.texcoordArray, texcoordArrayBuffer);
}
if (attributeMap._FEATURE_ID_RGBA4444 !== void 0) {
const featureAccesor = gltf.json.accessors[attributeMap._FEATURE_ID_RGBA4444];
if (gltf.json.extensionsUsed && gltf.json.extensionsUsed.includes("EXT_meshopt_compression")) {
mesh.featureData = getBufferData(gltf, featureAccesor);
}
}
if (attributeMap._FEATURE_RGBA4444 !== void 0) {
const featureAccesor = gltf.json.accessors[attributeMap._FEATURE_RGBA4444];
mesh.featureData = new Uint32Array(getBufferData(gltf, featureAccesor).buffer);
}
const materialIdx = primitive.material;
const materialDesc = materialIdx !== void 0 ? gltf.json.materials[materialIdx] : { defined: false };
mesh.material = convertMaterial(materialDesc, textures);
return mesh;
}
function convertMeshes(gltf, textures) {
const meshes = [];
for (const meshDesc of gltf.json.meshes) {
const primitives = [];
for (const primitive of meshDesc.primitives) {
primitives.push(convertPrimitive(primitive, gltf, textures));
}
meshes.push(primitives);
}
return meshes;
}
function convertNode(nodeDesc, gltf, meshes) {
const { matrix, rotation, translation, scale, mesh, extras, children, name } = nodeDesc;
const node = {};
node.name = name;
node.localMatrix = matrix || fromRotationTranslationScale([], rotation || [0, 0, 0, 1], translation || [0, 0, 0], scale || [1, 1, 1]);
node.globalMatrix = clone$6(node.localMatrix);
if (mesh !== void 0) {
node.meshes = meshes[mesh];
const anchor = node.anchor = [0, 0];
for (const mesh2 of node.meshes) {
const { min, max } = mesh2.aabb;
anchor[0] += min[0] + max[0];
anchor[1] += min[1] + max[1];
}
anchor[0] = Math.floor(anchor[0] / node.meshes.length / 2);
anchor[1] = Math.floor(anchor[1] / node.meshes.length / 2);
}
if (extras) {
if (extras.id) {
node.id = extras.id;
}
if (extras.lights) {
node.lights = decodeLights(extras.lights);
}
if (extras["MAPBOX_geometry_bloom"]) {
node.isGeometryBloom = extras["MAPBOX_geometry_bloom"];
}
}
if (children) {
const converted = [];
for (const childNodeIdx of children) {
converted.push(convertNode(gltf.json.nodes[childNodeIdx], gltf, meshes));
}
node.children = converted;
}
return node;
}
function convertFootprint(mesh) {
if (mesh.vertices.length === 0 || mesh.indices.length === 0) {
return null;
}
const grid = new TriangleGridIndex(mesh.vertices, mesh.indices, 8, 256);
const [min, max] = [grid.min.clone(), grid.max.clone()];
return {
vertices: mesh.vertices,
indices: mesh.indices,
grid,
min,
max
};
}
function parseLegacyFootprintMesh(gltfNode) {
if (!gltfNode.extras || !gltfNode.extras.ground) {
return null;
}
const groundContainer = gltfNode.extras.ground;
if (!groundContainer || !Array.isArray(groundContainer) || groundContainer.length === 0) {
return null;
}
const ground = groundContainer[0];
if (!ground || !Array.isArray(ground) || ground.length === 0) {
return null;
}
const vertices = [];
for (const point of ground) {
if (!Array.isArray(point) || point.length !== 2) {
continue;
}
const x = point[0];
const y = point[1];
if (typeof x !== "number" || typeof y !== "number") {
continue;
}
vertices.push(new Point(x, y));
}
if (vertices.length < 3) {
return null;
}
if (vertices.length > 1 && vertices[vertices.length - 1].equals(vertices[0])) {
vertices.pop();
}
let cross = 0;
for (let i = 0; i < vertices.length; i++) {
const a = vertices[i];
const b = vertices[(i + 1) % vertices.length];
const c = vertices[(i + 2) % vertices.length];
cross += (a.x - b.x) * (c.y - b.y) - (c.x - b.x) * (a.y - b.y);
}
if (cross > 0) {
vertices.reverse();
}
const indices = earcut(vertices.flatMap((v) => [v.x, v.y]), []);
if (indices.length === 0) {
return null;
}
return { vertices, indices };
}
function parseNodeFootprintMesh(meshes, matrix) {
const vertices = [];
const indices = [];
let baseVertex = 0;
const tempVertex = [];
for (const mesh of meshes) {
baseVertex = vertices.length;
const vArray = mesh.vertexArray.float32;
const iArray = mesh.indexArray.uint16;
for (let i = 0; i < mesh.vertexArray.length; i++) {
tempVertex[0] = vArray[i * 3 + 0];
tempVertex[1] = vArray[i * 3 + 1];
tempVertex[2] = vArray[i * 3 + 2];
transformMat4$2(tempVertex, tempVertex, matrix);
vertices.push(new Point(tempVertex[0], tempVertex[1]));
}
for (let i = 0; i < mesh.indexArray.length * 3; i++) {
indices.push(iArray[i] + baseVertex);
}
}
if (indices.length % 3 !== 0) {
return null;
}
for (let i = 0; i < indices.length; i += 3) {
const a = vertices[indices[i + 0]];
const b = vertices[indices[i + 1]];
const c = vertices[indices[i + 2]];
if ((a.x - b.x) * (c.y - b.y) - (c.x - b.x) * (a.y - b.y) > 0) {
[indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
}
}
return { vertices, indices };
}
function convertFootprints(convertedNodes, sceneNodes, modelNodes) {
assert$1(convertedNodes.length === sceneNodes.length);
const nodeFootprintLookup = {};
const footprintNodeIndices = /* @__PURE__ */ new Set();
for (let i = 0; i < convertedNodes.length; i++) {
const gltfNode = modelNodes[sceneNodes[i]];
if (!gltfNode.extras) {
continue;
}
const fpVersion = gltfNode.extras["mapbox:footprint:version"];
const fpId = gltfNode.extras["mapbox:footprint:id"];
if (fpVersion || fpId) {
footprintNodeIndices.add(i);
}
if (fpVersion !== "1.0.0" || !fpId) {
continue;
}
nodeFootprintLookup[fpId] = i;
}
for (let i = 0; i < convertedNodes.length; i++) {
if (footprintNodeIndices.has(i)) {
continue;
}
const node = convertedNodes[i];
const gltfNode = modelNodes[sceneNodes[i]];
if (!gltfNode.extras) {
continue;
}
let fpMesh = null;
if (node.id in nodeFootprintLookup) {
fpMesh = parseNodeFootprintMesh(convertedNodes[nodeFootprintLookup[node.id]].meshes, node.localMatrix);
}
if (!fpMesh) {
fpMesh = parseLegacyFootprintMesh(gltfNode);
}
if (fpMesh) {
node.footprint = convertFootprint(fpMesh);
}
}
if (footprintNodeIndices.size > 0) {
const nodesToRemove = Array.from(footprintNodeIndices.values()).sort((a, b) => a - b);
for (let i = nodesToRemove.length - 1; i >= 0; i--) {
convertedNodes.splice(nodesToRemove[i], 1);
}
}
}
function convertModel(gltf) {
const textures = convertTextures(gltf, gltf.images);
const meshes = convertMeshes(gltf, textures);
const { scenes, scene, nodes } = gltf.json;
const sceneNodes = scenes ? scenes[scene || 0].nodes : [...nodes.keys()];
const resultNodes = [];
for (const nodeIdx of sceneNodes) {
resultNodes.push(convertNode(nodes[nodeIdx], gltf, meshes));
}
convertFootprints(resultNodes, sceneNodes, gltf.json.nodes);
return resultNodes;
}
function process3DTile(gltf, zScale) {
const nodes = convertModel(gltf);
for (const node of nodes) {
for (const mesh of node.meshes) {
parseHeightmap(mesh);
}
if (node.lights) {
node.lightMeshIndex = node.meshes.length;
node.meshes.push(createLightsMesh(node.lights, zScale));
}
}
return nodes;
}
function parseHeightmap(mesh) {
mesh.heightmap = new Float32Array(HEIGHTMAP_DIM * HEIGHTMAP_DIM);
mesh.heightmap.fill(-1);
const vertices = mesh.vertexArray.float32;
const xMin = mesh.aabb.min[0] - 1;
const yMin = mesh.aabb.min[1] - 1;
const xMax = mesh.aabb.max[0];
const yMax = mesh.aabb.max[1];
const xRange = xMax - xMin + 2;
const yRange = yMax - yMin + 2;
const xCellInv = HEIGHTMAP_DIM / xRange;
const yCellInv = HEIGHTMAP_DIM / yRange;
for (let i = 0; i < vertices.length; i += 3) {
const px = vertices[i + 0];
const py = vertices[i + 1];
const pz = vertices[i + 2];
const x = (px - xMin) * xCellInv | 0;
const y = (py - yMin) * yCellInv | 0;
assert$1(x >= 0 && x < HEIGHTMAP_DIM);
assert$1(y >= 0 && y < HEIGHTMAP_DIM);
if (pz > mesh.heightmap[y * HEIGHTMAP_DIM + x]) {
mesh.heightmap[y * HEIGHTMAP_DIM + x] = pz;
}
}
}
function calculateLightsMesh(lights, zScale, indexArray, vertexArray, colorArray) {
indexArray.reserve(indexArray.length + 4 * lights.length);
vertexArray.reserve(vertexArray.length + 10 * lights.length);
colorArray.reserve(colorArray.length + 10 * lights.length);
let currentVertex = vertexArray.length;
for (const light of lights) {
const fallOff = Math.min(10, Math.max(4, 1.3 * light.height)) * zScale;
const tangent = [-light.normal[1], light.normal[0], 0];
const horizontalSpread = Math.min(0.29, 0.1 * light.width / light.depth);
const width = light.width - 2 * light.depth * zScale * (horizontalSpread + 0.01);
const v1 = scaleAndAdd$2([], light.pos, tangent, width / 2);
const v2 = scaleAndAdd$2([], light.pos, tangent, -width / 2);
const v0 = [v1[0], v1[1], v1[2] + light.height];
const v3 = [v2[0], v2[1], v2[2] + light.height];
const v1extrusion = scaleAndAdd$2([], light.normal, tangent, horizontalSpread);
scale$4(v1extrusion, v1extrusion, fallOff);
const v2extrusion = scaleAndAdd$2([], light.normal, tangent, -horizontalSpread);
scale$4(v2extrusion, v2extrusion, fallOff);
add$4(v1extrusion, v1, v1extrusion);
add$4(v2extrusion, v2, v2extrusion);
v1[2] += 0.1;
v2[2] += 0.1;
vertexArray.emplaceBack(v1extrusion[0], v1extrusion[1], v1extrusion[2]);
vertexArray.emplaceBack(v2extrusion[0], v2extrusion[1], v2extrusion[2]);
vertexArray.emplaceBack(v1[0], v1[1], v1[2]);
vertexArray.emplaceBack(v2[0], v2[1], v2[2]);
vertexArray.emplaceBack(v0[0], v0[1], v0[2]);
vertexArray.emplaceBack(v3[0], v3[1], v3[2]);
vertexArray.emplaceBack(v1[0], v1[1], v1[2]);
vertexArray.emplaceBack(v2[0], v2[1], v2[2]);
vertexArray.emplaceBack(v1extrusion[0], v1extrusion[1], v1extrusion[2]);
vertexArray.emplaceBack(v2extrusion[0], v2extrusion[1], v2extrusion[2]);
const halfWidth = width / fallOff / 2;
colorArray.emplaceBack(-halfWidth - horizontalSpread, -1, halfWidth, 0.8);
colorArray.emplaceBack(halfWidth + horizontalSpread, -1, halfWidth, 0.8);
colorArray.emplaceBack(-halfWidth, 0, halfWidth, 1.3);
colorArray.emplaceBack(halfWidth, 0, halfWidth, 1.3);
colorArray.emplaceBack(halfWidth + horizontalSpread, -0.8, halfWidth, 0.7);
colorArray.emplaceBack(halfWidth + horizontalSpread, -0.8, halfWidth, 0.7);
colorArray.emplaceBack(0, 0, halfWidth, 1.3);
colorArray.emplaceBack(0, 0, halfWidth, 1.3);
colorArray.emplaceBack(halfWidth + horizontalSpread, -1.2, halfWidth, 0.8);
colorArray.emplaceBack(halfWidth + horizontalSpread, -1.2, halfWidth, 0.8);
indexArray.emplaceBack(6 + currentVertex, 4 + currentVertex, 8 + currentVertex);
indexArray.emplaceBack(7 + currentVertex, 9 + currentVertex, 5 + currentVertex);
indexArray.emplaceBack(0 + currentVertex, 1 + currentVertex, 2 + currentVertex);
indexArray.emplaceBack(1 + currentVertex, 3 + currentVertex, 2 + currentVertex);
currentVertex += 10;
}
}
function createLightsMesh(lights, zScale) {
const mesh = {};
mesh.indexArray = new StructArrayLayout3ui6();
mesh.vertexArray = new StructArrayLayout3f12();
mesh.colorArray = new StructArrayLayout4f16();
calculateLightsMesh(lights, zScale, mesh.indexArray, mesh.vertexArray, mesh.colorArray);
const material = {};
material.defined = true;
material.emissiveFactor = Color.black;
const pbrMetallicRoughness = {};
pbrMetallicRoughness.baseColorFactor = Color.white;
material.pbrMetallicRoughness = pbrMetallicRoughness;
mesh.material = material;
mesh.aabb = new Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
return mesh;
}
function decodeLights(base64) {
if (!base64.length) return [];
const decoded = base64DecToArr(base64);
const lights = [];
const lightCount = decoded.length / 24;
const lightData = new Uint16Array(decoded.buffer);
const lightDataFloat = new Float32Array(decoded.buffer);
const stride = 6;
for (let i = 0; i < lightCount; i++) {
const height = lightData[i * 2 * stride] / 30;
const elevation = lightData[i * 2 * stride + 1] / 30;
const depth = lightData[i * 2 * stride + 10] / 100;
const x0 = lightDataFloat[i * stride + 1];
const y0 = lightDataFloat[i * stride + 2];
const x1 = lightDataFloat[i * stride + 3];
const y1 = lightDataFloat[i * stride + 4];
const dx = x1 - x0;
const dy = y1 - y0;
const width = Math.hypot(dx, dy);
const normal = [dy / width, -dx / width, 0];
const pos = [x0 + dx * 0.5, y0 + dy * 0.5, elevation];
const points = [x0, y0, x1, y1];
lights.push({ pos, normal, width, height, depth, points });
}
return lights;
}
const buildingPositionAttributes = createLayout([
{ name: "a_pos_3f", components: 3, type: "Float32" }
]);
const buildingNormalAttributes = createLayout([
{ name: "a_normal_3", components: 3, type: "Int16" }
]);
const buildingCentroidAttributes = createLayout([
{ name: "a_centroid_3", components: 3, type: "Int16" }
]);
const buildingColorAttributes = createLayout([
{ name: "a_part_color_emissive", components: 2, type: "Uint16" }
]);
const buildingFacadePaintAttributes = createLayout([
{ name: "a_faux_facade_color_emissive", components: 2, type: "Uint16" }
]);
const buildingFacadeDataAttributes = createLayout([
{ name: "a_faux_facade_data", components: 4, type: "Uint16" }
]);
const buildingFacadeVerticalRangeAttributes = createLayout([
{ name: "a_faux_facade_vertical_range", components: 2, type: "Uint16" }
]);
const buildingBloomAttenuationAttributes = createLayout([
{ name: "a_bloom_attenuation", components: 4, type: "Float32" }
]);
const buildingFloodLightWallRadiusAttributes = createLayout([
{ name: "a_flood_light_wall_radius_1i16", components: 1, type: "Uint16" }
]);
const { members: members$3, size: size$3, alignment: alignment$3 } = buildingPositionAttributes;
const vectorTileFeatureTypes$2 = VectorTileFeature.types;
const BUILDING_VISIBLE = 0;
const BUILDING_HIDDEN_BY_REPLACEMENT = 1;
const BUILDING_HIDDEN_BY_TILE_BORDER_DEDUPLICATION = 2;
const BUILDING_HIDDEN_WITH_INCOMPLETE_PARTS = 4;
const MAX_INT_16 = 32767;
const BUILDING_TILE_PADDING = 163;
const FLOOD_LIGHT_MAX_RADIUS_METER = 2048;
function geometryFullyInsideTile(geometry, padding) {
const extentWithPadding = EXTENT + padding;
for (const polygon of geometry) {
for (const point of polygon) {
if (point.x < -padding || point.x > extentWithPadding || point.y < -padding || point.y > extentWithPadding) {
return false;
}
}
}
return true;
}
function translateRoofType(roofShape) {
switch (roofShape) {
case "flat":
return ROOF_TYPE_FLAT;
case "hipped":
return ROOF_TYPE_HIPPED;
case "gabled":
return ROOF_TYPE_GABLED;
case "parapet":
return ROOF_TYPE_PARAPET;
case "mansard":
return ROOF_TYPE_MANSARD;
case "skillion":
return ROOF_TYPE_SKILLION;
case "pyramidal":
return ROOF_TYPE_PYRAMIDAL;
default:
throw new Error(`Unknown roof shape: ${roofShape}`);
}
}
;
class BuildingBloomGeometry {
constructor() {
this.layoutVertexArray = new StructArrayLayout3f12();
this.layoutAttenuationArray = new StructArrayLayout4f16();
this.layoutColorArray = new StructArrayLayout2ui4();
this.indexArray = new StructArrayLayout3ui6();
this.indexArrayForConflation = new StructArrayLayout3ui6();
this.segmentsBucket = new SegmentVector();
}
}
class BuildingGeometry {
constructor(withFauxFacade) {
this.layoutFacadePaintArray = null;
this.layoutFacadeDataArray = null;
this.layoutFacadeVerticalRangeArray = null;
this.segmentsBucket = new SegmentVector();
this.entranceBloom = new BuildingBloomGeometry();
const initialVertexCapacity = 65 * 1024;
const initialIndexCapactiy = 65 * 1024;
this.layoutVertexArray = new StructArrayLayout3f12();
this.layoutVertexArray.reserve(initialVertexCapacity);
this.layoutNormalArray = new StructArrayLayout3i6();
this.layoutNormalArray.reserve(initialVertexCapacity);
this.layoutCentroidArray = new StructArrayLayout3i6();
this.layoutCentroidArray.reserve(initialVertexCapacity);
this.layoutColorArray = new StructArrayLayout2ui4();
this.layoutColorArray.reserve(initialVertexCapacity);
this.layoutFloodLightDataArray = new StructArrayLayout1ui2();
this.layoutFloodLightDataArray.reserve(initialVertexCapacity);
this.layoutAOArray = new StructArrayLayout1ub1();
this.layoutAOArray.reserve(initialVertexCapacity);
this.indexArray = new StructArrayLayout3ui6();
this.indexArray.reserve(initialIndexCapactiy);
this.indexArrayForConflation = new StructArrayLayout3ui6();
this.segmentsBucket = new SegmentVector();
this.entranceBloom = new BuildingBloomGeometry();
if (withFauxFacade) {
this.layoutFacadePaintArray = new StructArrayLayout2ui4();
this.layoutFacadeDataArray = new StructArrayLayout4ui8();
this.layoutFacadeVerticalRangeArray = new StructArrayLayout2ui4();
}
}
reserve(numVertices, numIndices, withFauxFacade) {
this.layoutVertexArray.reserveForAdditional(numVertices);
this.layoutCentroidArray.reserveForAdditional(numVertices);
this.layoutFloodLightDataArray.reserveForAdditional(numVertices);
this.layoutNormalArray.reserveForAdditional(numVertices);
this.layoutAOArray.reserveForAdditional(numVertices);
this.layoutColorArray.reserveForAdditional(numVertices);
this.indexArray.reserveForAdditional(numIndices);
if (withFauxFacade) {
this.layoutFacadePaintArray.reserveForAdditional(numVertices);
this.layoutFacadeDataArray.reserveForAdditional(numVertices);
this.layoutFacadeVerticalRangeArray.reserveForAdditional(numVertices);
}
}
}
class BuildingBucket {
constructor(options) {
this.colorBufferUploaded = false;
this.maxHeight = 0;
this.replacementUpdateTime = 0;
this.activeReplacements = [];
this.footprints = [];
this.footprintsVertices = new StructArrayLayout2f8();
this.footprintsIndices = new StructArrayLayout1ui2();
this.footprintsMin = new Point(Infinity, Infinity);
this.footprintsMax = new Point(-Infinity, -Infinity);
this.featuresOnBorder = [];
this.buildingWithoutFacade = new BuildingGeometry(false);
this.buildingWithFacade = new BuildingGeometry(true);
this.indexArrayForConflationUploaded = false;
this.featureFootprintLookup = /* @__PURE__ */ new Map();
this.buildingIds = /* @__PURE__ */ new Set();
this.footprintLookup = {};
this.zoom = options.zoom;
this.canonical = options.canonical;
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.hasPattern = false;
this.worldview = options.worldview;
this.lut = options.lut;
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut });
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.projection = options.projection;
this.groundEffect = new GroundEffect(options);
this.groundEffect.groundRadiusArray = new StructArrayLayout1f4();
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
const emptyGrid = new TriangleGridIndex([], [], 1);
const footprintForBucket = {
vertices: [],
indices: new Uint32Array(0),
grid: emptyGrid,
min: this.footprintsMin,
max: this.footprintsMax,
buildingIds: this.buildingIds
};
_footprints.push({
footprint: footprintForBucket,
id: _id
});
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
prepare() {
return waitForBuildingGen();
}
populate(features, options, canonical, tileTransform) {
const perfStartTime = PerformanceUtils.now();
let perfBuildingGenTime = 0;
let perfBuildingGenCount = 0;
let perfMeshLoopAccuTime = 0;
const buildingGen = getBuildingGen();
if (!buildingGen) {
return;
}
const tileToMeters = tileToMeter(canonical);
this.tileToMeter = tileToMeters;
this.brightness = options.brightness;
const style = {
normalScale: [1, -1, tileToMeters],
tileToMeters
};
buildingGen.setStyle(style);
buildingGen.setAOOptions(false, 0.3);
buildingGen.setMetricOptions(false, 16);
buildingGen.setStructuralOptions(true);
buildingGen.setFacadeClassifierOptions(3);
const featureSourceIdMap = /* @__PURE__ */ new Map();
const facadeDataForFeature = /* @__PURE__ */ new Map();
let featuresFacadesCount = 0;
for (const { feature } of features) {
const isFacade = vectorTileFeatureTypes$2[feature.type] === "LineString";
if (!isFacade) {
featureSourceIdMap.set(feature.id, feature.properties.source_id);
continue;
}
const needGeometry = this.layers[0]._featureFilter.needGeometry;
if (needGeometry && !this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom), feature, canonical))
continue;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!needGeometry && !this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom), evaluationFeature, canonical))
continue;
const geometry = needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform);
const coordinates = [];
for (const polygon of geometry) {
for (const point of polygon) {
coordinates.push(point.x);
coordinates.push(point.y);
}
}
const facadeProperties = {
coordinates,
crossPerc: feature.properties.cross_perc,
distanceToRoad: feature.properties.distance_to_road,
entrances: feature.properties.entrances,
sourceId: 0
};
const sourceId = feature.properties.source_id;
let facades = facadeDataForFeature.get(sourceId);
if (!facades) {
facades = [];
facadeDataForFeature.set(sourceId, facades);
}
facades.push(facadeProperties);
++featuresFacadesCount;
}
this.maxHeight = 0;
const disabledFootprintLookup = new Array();
const disabledBuildings = /* @__PURE__ */ new Set();
const disableBuilding = (buildingId) => {
if (buildingId != null) {
disabledBuildings.add(buildingId);
}
};
const addBuildingFootprint = (buildingId, footprintIndex) => {
if (buildingId == null) {
return;
}
disabledFootprintLookup.push({ buildingId, footprintIndex });
};
const estimatedVertexCapacity = (features.length - featuresFacadesCount) * 64;
const estimatedIndexCapacity = estimatedVertexCapacity / 2;
this.buildingWithFacade.reserve(estimatedVertexCapacity, estimatedIndexCapacity, true);
this.buildingWithoutFacade.reserve(estimatedVertexCapacity * 2, estimatedIndexCapacity * 2, false);
this.footprintsIndices.reserve((features.length - featuresFacadesCount) * 16);
this.footprintsVertices.reserve((features.length - featuresFacadesCount) * 8);
for (const { feature, id, index, sourceLayerIndex } of features) {
const isFacade = vectorTileFeatureTypes$2[feature.type] === "LineString";
if (isFacade) {
continue;
}
const needGeometry = this.layers[0]._featureFilter.needGeometry;
if (needGeometry && !this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom), feature, canonical))
continue;
let buildingId = null;
if (feature.properties && feature.properties.hasOwnProperty("building_id")) {
buildingId = feature.properties["building_id"];
if (disabledBuildings.has(buildingId)) {
continue;
}
}
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!needGeometry && !this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom), evaluationFeature, canonical))
continue;
const geometry = needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform);
const EARCUT_MAX_RINGS = 500;
const classifiedRings = classifyRings(geometry, EARCUT_MAX_RINGS);
let hasHoles = false;
for (const ring of classifiedRings) {
if (ring.length !== 1) {
hasHoles = true;
break;
}
}
if (hasHoles) {
disableBuilding(buildingId);
continue;
}
if (!geometryFullyInsideTile(geometry, BUILDING_TILE_PADDING)) {
disableBuilding(buildingId);
continue;
}
const layer = this.layers[0];
const roofShapeString = layer.layout.get("building-roof-shape").evaluate(feature, {}, canonical);
const roofType = translateRoofType(roofShapeString);
const base = layer.layout.get("building-base").evaluate(feature, {}, canonical);
const height = layer.layout.get("building-height").evaluate(feature, {}, canonical);
const floodLightGroundRadius = layer.layout.get("building-flood-light-ground-radius").evaluate(feature, {}, canonical);
const aoIntensity = layer.paint.get("building-ambient-occlusion-intensity");
const maxRadius = floodLightGroundRadius / this.tileToMeter;
feature.properties["building-part"] = "roof";
const roofColor = layer.paint.get("building-color").evaluate(feature, {}, this.canonical).toPremultipliedRenderColor(this.lut);
const roofEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, {}, this.canonical);
feature.properties["building-part"] = "wall";
const wallsColor = layer.paint.get("building-color").evaluate(feature, {}, this.canonical).toPremultipliedRenderColor(this.lut);
const wallsEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, {}, this.canonical);
feature.properties["building-part"] = "window";
const windowColor = layer.paint.get("building-color").evaluate(feature, {}, this.canonical).toPremultipliedRenderColor(this.lut);
const windowEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, {}, this.canonical);
feature.properties["building-part"] = "door";
const entranceColor = layer.paint.get("building-color").evaluate(feature, {}, this.canonical).toPremultipliedRenderColor(this.lut);
const entranceEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, {}, this.canonical);
let floodLightWallRadius = layer.layout.get("building-flood-light-wall-radius").evaluate(feature, {}, canonical);
floodLightWallRadius = clamp(floodLightWallRadius, 0, FLOOD_LIGHT_MAX_RADIUS_METER);
const floodLightWallRadiusNormalized = floodLightWallRadius / FLOOD_LIGHT_MAX_RADIUS_METER * MAX_INT_16;
const sourceId = featureSourceIdMap.get(id);
const facades = facadeDataForFeature.get(sourceId) || [];
const hasFauxFacade = facades.length !== 0 && layer.layout.get("building-facade").evaluate(feature, {}, canonical);
buildingGen.setFacadeOptions(4, true);
buildingGen.setFauxFacadeOptions(hasFauxFacade, false, 1);
let windowXPerc = 0;
let windowYPerc = 0;
let floorXTile = 0;
let floorYTile = 0;
let startPositionTile = 0;
let endPositionTile = 0;
let normalizedStartTile = 0;
let normalizedEndTile = 0;
let g1 = 0;
let b1 = 0;
let a1 = 0;
if (hasFauxFacade) {
let numFloors = Math.round(layer.layout.get("building-facade-floors").evaluate(feature, {}, canonical));
if (base === 0) {
const facadeHintAvailable = facades.length > 0;
numFloors = Math.max(1, numFloors - (facadeHintAvailable ? 1 : 0));
let facadeHeight = 4;
if (height > 100) {
const facadeHeights = [10, 13, 15];
facadeHeight = facadeHeights[feature.id ? feature.id % facadeHeights.length : 0];
} else if (height <= 10) {
facadeHeight = 3;
}
buildingGen.setFacadeOptions(facadeHeight, true);
startPositionTile = (height < 15 ? 1.3 : 1.61803) * facadeHeight / tileToMeters;
} else {
startPositionTile = base / tileToMeters;
}
endPositionTile = height / tileToMeters;
startPositionTile = Math.min(startPositionTile, endPositionTile);
const unitWidth = layer.layout.get("building-facade-unit-width").evaluate(feature, {}, canonical);
floorXTile = unitWidth / tileToMeters;
floorYTile = (endPositionTile - startPositionTile) / numFloors;
buildingGen.setFauxFacadeOptions(true, true, floorXTile);
const window = layer.layout.get("building-facade-window").evaluate(feature, {}, canonical);
windowXPerc = window[0];
windowYPerc = window[1];
normalizedStartTile = Math.floor(Math.min(1, startPositionTile / EXTENT) * 65535);
normalizedEndTile = Math.floor(Math.min(1, endPositionTile / EXTENT) * 65535);
const windowX = Math.floor(windowXPerc * 255);
const windowY = Math.floor(windowYPerc * 255);
g1 = windowX << 8 | windowY;
const normalizedFloorXTile = Math.floor(Math.min(1, floorXTile / EXTENT) * 65535);
b1 = normalizedFloorXTile;
const normalizedFloorYTile = Math.floor(Math.min(1, floorYTile / EXTENT) * 65535);
a1 = normalizedFloorYTile;
}
const buildingGenFeatures = Array(classifiedRings.length);
const bboxMin = { x: Infinity, y: Infinity };
const bboxMax = { x: -Infinity, y: -Infinity };
const centroid = { x: 0, y: 0 };
let pointCount = 0;
for (let p = 0; p < classifiedRings.length; p++) {
const polygon = classifiedRings[p];
if (polygon.length > 0) {
const coordinates = [];
const ringIndices = Array(polygon.length + 1);
ringIndices[0] = 0;
for (let r = 0; r < polygon.length; r++) {
const ring = polygon[r];
for (let i = 0; i < ring.length; i++) {
const point = ring[ring.length - i - 1];
bboxMin.x = Math.min(bboxMin.x, point.x);
bboxMin.y = Math.min(bboxMin.y, point.y);
bboxMax.x = Math.max(bboxMax.x, point.x);
bboxMax.y = Math.max(bboxMax.y, point.y);
centroid.x += point.x;
centroid.y += point.y;
pointCount++;
coordinates.push(point.x);
coordinates.push(point.y);
}
ringIndices[r + 1] = coordinates.length;
}
const featureInput = {
id: feature.id ? feature.id : 0,
height,
minHeight: base,
sourceId: 0,
roofType,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
coordinates,
ringIndices
};
buildingGenFeatures[p] = featureInput;
}
}
assert$1(pointCount > 0);
centroid.x /= pointCount || 1;
centroid.y /= pointCount || 1;
const perfBeforeGenMesh = PerformanceUtils.now();
const result = buildingGen.generateMesh(buildingGenFeatures, facades);
perfBuildingGenTime += PerformanceUtils.now() - perfBeforeGenMesh;
++perfBuildingGenCount;
if (typeof result === "string") {
warnOnce(`Unable to generate building ${feature.id}: ${result}`);
disableBuilding(buildingId);
continue;
}
if (result.meshes.length === 0 || result.modifiedPolygonRings.length === 0) {
disableBuilding(buildingId);
continue;
}
const building = hasFauxFacade ? this.buildingWithFacade : this.buildingWithoutFacade;
let vertexCount = 0;
for (const mesh of result.meshes) {
vertexCount += mesh.positions.length / 3;
}
const segment = building.segmentsBucket.prepareSegment(vertexCount, building.layoutVertexArray, building.indexArray);
const buildingParts = [];
let buildingBloom = null;
let bloomIndicesOffset = 0;
let bloomIndicesLength = -1;
const vertexArrayOffset = building.layoutVertexArray.length;
const newVertexCount = vertexArrayOffset + vertexCount;
building.layoutVertexArray.resize(newVertexCount);
building.layoutCentroidArray.resize(newVertexCount);
building.layoutNormalArray.resize(newVertexCount);
building.layoutAOArray.resize(newVertexCount);
building.layoutColorArray.resize(newVertexCount);
building.layoutFloodLightDataArray.resize(newVertexCount);
if (hasFauxFacade) {
building.layoutFacadePaintArray.resize(newVertexCount);
building.layoutFacadeDataArray.resize(newVertexCount);
building.layoutFacadeVerticalRangeArray.resize(newVertexCount);
}
const indexArrayRangeStartOffset = building.indexArray.length;
let footprintHeight = 0;
const perfMeshLoopStartTime = PerformanceUtils.now();
let partVertexOffset = vertexArrayOffset;
for (const mesh of result.meshes) {
let partColor;
let emissive;
if (mesh.buildingPart === BUILDING_PART_ROOF) {
partColor = roofColor;
emissive = roofEmissive;
} else if (mesh.buildingPart === BUILDING_PART_WALL) {
partColor = wallsColor;
emissive = wallsEmissive;
} else if (mesh.buildingPart === BUILDING_PART_FACADE_GLAZING) {
partColor = windowColor;
emissive = windowEmissive;
} else if (mesh.buildingPart === BUILDING_PART_ENTRANCE) {
partColor = entranceColor;
emissive = entranceEmissive;
} else {
continue;
}
emissive = clamp(emissive, 0, 1);
if (mesh.buildingPart === BUILDING_PART_ENTRANCE) {
const areaLights = new Array();
for (let i = 0; i < mesh.positions.length; i += 12) {
const x0 = mesh.positions[i + 0];
const y0 = mesh.positions[i + 1];
const x1 = mesh.positions[i + 3];
const y1 = mesh.positions[i + 4];
const elevation = mesh.positions[i + 2];
const height2 = mesh.positions[i + 8] - elevation;
const depth = 1;
const dx = x1 - x0;
const dy = y1 - y0;
const width = Math.hypot(dx, dy);
const normal = [dy / width, -dx / width, 0];
const pos = [x0 + dx * 0.5, y0 + dy * 0.5, elevation];
const points = [x0, y0, x1, y1];
areaLights.push({ pos, normal, width, height: height2, depth, points });
}
const expectedLightVertices = areaLights.length * 10;
const bloomSegment = building.entranceBloom.segmentsBucket.prepareSegment(
expectedLightVertices,
building.entranceBloom.layoutVertexArray,
building.entranceBloom.indexArray
);
const bloomVertexOffset = building.entranceBloom.layoutVertexArray.length;
bloomIndicesOffset = building.entranceBloom.indexArray.length;
calculateLightsMesh(areaLights, 0.5 / this.tileToMeter, building.entranceBloom.indexArray, building.entranceBloom.layoutVertexArray, building.entranceBloom.layoutAttenuationArray);
const bloomVertexLength = building.entranceBloom.layoutVertexArray.length - bloomVertexOffset;
bloomIndicesLength = building.entranceBloom.indexArray.length - bloomIndicesOffset;
for (let p = 0; p < bloomVertexLength; p++) {
const r = entranceColor.r * 255;
const g = entranceColor.g * 255;
const b = entranceColor.b * 255;
const e = entranceEmissive * 51;
const c1 = r << 8 | g;
const c2 = b << 8 | e;
building.entranceBloom.layoutColorArray.emplaceBack(c1, c2);
}
bloomSegment.vertexLength += bloomVertexLength;
bloomSegment.primitiveLength += bloomIndicesLength;
buildingBloom = {
part: mesh.buildingPart,
vertexOffset: bloomVertexOffset,
vertexLength: bloomVertexLength
};
}
building.layoutVertexArray.float32.set(mesh.positions, partVertexOffset * 3);
const partVertexCount = mesh.positions.length / 3;
for (let v = 0; v < partVertexCount; ++v) {
const vertIdx = v * 3;
footprintHeight = Math.max(footprintHeight, mesh.positions[vertIdx + 2]);
const nx = mesh.normals[vertIdx] * MAX_INT_16;
const ny = mesh.normals[vertIdx + 1] * MAX_INT_16;
const nz = mesh.normals[vertIdx + 2] * MAX_INT_16;
const nrmIdx = (partVertexOffset + v) * 3;
building.layoutNormalArray.int16[nrmIdx] = nx;
building.layoutNormalArray.int16[nrmIdx + 1] = ny;
building.layoutNormalArray.int16[nrmIdx + 2] = nz;
const ao = mesh.ao[v];
building.layoutAOArray.uint8[partVertexOffset + v] = ao * 255;
const colorFactor = 1 + (ao - 1) * aoIntensity;
const r = partColor.r * 255 * colorFactor;
const g = partColor.g * 255 * colorFactor;
const b = partColor.b * 255 * colorFactor;
const e = emissive * 255;
const c1 = r << 8 | g;
const c2 = b << 8 | e;
building.layoutColorArray.uint16[(partVertexOffset + v) * 2] = c1;
building.layoutColorArray.uint16[(partVertexOffset + v) * 2 + 1] = c2;
}
const cx = Math.floor(centroid.x);
const cy = Math.floor(centroid.y);
const ch = Math.floor(height);
for (let v = 0; v < partVertexCount; ++v) {
const idx = (partVertexOffset + v) * 3;
building.layoutCentroidArray.int16[idx] = cx;
building.layoutCentroidArray.int16[idx + 1] = cy;
building.layoutCentroidArray.int16[idx + 2] = ch;
}
if (mesh.buildingPart === BUILDING_PART_WALL) {
building.layoutFloodLightDataArray.uint16.fill(floodLightWallRadiusNormalized, partVertexOffset, partVertexOffset + partVertexCount);
} else {
building.layoutFloodLightDataArray.uint16.fill(0, partVertexOffset, partVertexOffset + partVertexCount);
}
if (hasFauxFacade) {
const r = windowColor.r * 255;
const g = windowColor.g * 255;
const b = windowColor.b * 255;
const e = windowEmissive * 255;
const c1 = r << 8 | g;
const c2 = b << 8 | e;
for (let v = 0; v < partVertexCount; ++v) {
const i = (partVertexOffset + v) * 2;
building.layoutFacadePaintArray.uint16[i] = c1;
building.layoutFacadePaintArray.uint16[i + 1] = c2;
}
for (let v = 0; v < partVertexCount; ++v) {
const uvIdx = v * 2;
if (mesh.isFauxFacade[v]) {
const edgeDistanceTile = mesh.uv[uvIdx] * result.outerRingLength;
const normalizedEdgeDistance = Math.min(65535, Math.floor(edgeDistanceTile));
const r1 = normalizedEdgeDistance | 1;
building.layoutFacadeDataArray.emplace(partVertexOffset + v, r1, g1, b1, a1);
building.layoutFacadeVerticalRangeArray.emplace(partVertexOffset + v, normalizedStartTile, normalizedEndTile);
} else {
building.layoutFacadeDataArray.emplace(partVertexOffset + v, 0, 0, 0, 0);
building.layoutFacadeVerticalRangeArray.emplace(partVertexOffset + v, 0, 0);
}
}
}
const triangleIndex = segment.vertexLength;
const indexCount = mesh.indices.length / 3;
const indexOffset = building.indexArray.length;
building.indexArray.resize(indexOffset + indexCount);
for (let i = 0; i < indexCount; ++i) {
const idx = i * 3;
const o = indexOffset * 3 + idx;
building.indexArray.uint16[o] = triangleIndex + mesh.indices[idx];
building.indexArray.uint16[o + 1] = triangleIndex + mesh.indices[idx + 1];
building.indexArray.uint16[o + 2] = triangleIndex + mesh.indices[idx + 2];
}
if (mesh.buildingPart === BUILDING_PART_ROOF || mesh.buildingPart === BUILDING_PART_WALL || mesh.buildingPart === BUILDING_PART_FACADE_GLAZING || mesh.buildingPart === BUILDING_PART_ENTRANCE) {
const buildingFeaturePart = {
part: mesh.buildingPart,
vertexOffset: partVertexOffset,
vertexLength: mesh.positions.length / 3
};
buildingParts.push(buildingFeaturePart);
}
partVertexOffset += partVertexCount;
segment.vertexLength += partVertexCount;
segment.primitiveLength += mesh.indices.length / 3;
}
perfMeshLoopAccuTime += PerformanceUtils.now() - perfMeshLoopStartTime;
this.maxHeight = Math.max(this.maxHeight, footprintHeight);
const indexArrayRangeLength = building.indexArray.length - indexArrayRangeStartOffset;
const footprintIndexOffset = this.footprintsIndices.length;
const footprintVertexOffset = this.footprintsVertices.length;
const footprintFlattened = [];
const footprintBoundsMin = new Point(Infinity, Infinity);
const footprintBoundsMax = new Point(-Infinity, -Infinity);
const groundEffectVertexOffset = this.groundEffect.vertexArray.length;
for (const ring of result.modifiedPolygonRings) {
const groundPolyline = [];
const boundsMin = new Point(Infinity, Infinity);
const boundsMax = new Point(-Infinity, -Infinity);
for (let i = 0; i < ring.length; i += 2) {
const reverseIdx = ring.length - i - 2;
boundsMin.x = Math.min(boundsMin.x, ring[reverseIdx]);
boundsMin.y = Math.min(boundsMin.y, ring[reverseIdx + 1]);
boundsMax.x = Math.max(boundsMax.x, ring[reverseIdx]);
boundsMax.y = Math.max(boundsMax.y, ring[reverseIdx + 1]);
const point = new Point(ring[reverseIdx], ring[reverseIdx + 1]);
groundPolyline.push(point);
footprintFlattened.push(point.x, point.y);
this.footprintsVertices.emplaceBack(point.x, point.y);
}
footprintBoundsMin.x = Math.min(footprintBoundsMin.x, boundsMin.x);
footprintBoundsMin.y = Math.min(footprintBoundsMin.y, boundsMin.y);
footprintBoundsMax.x = Math.max(footprintBoundsMax.x, boundsMax.x);
footprintBoundsMax.y = Math.max(footprintBoundsMax.y, boundsMax.y);
this.groundEffect.addData(groundPolyline, [boundsMin, boundsMax], maxRadius);
}
const groundEffectVertexLength = this.groundEffect.vertexArray.length - groundEffectVertexOffset;
this.groundEffect.groundRadiusArray.reserveForAdditional(groundEffectVertexLength);
for (let v = 0; v < groundEffectVertexLength; v++) {
this.groundEffect.groundRadiusArray.emplaceBack(floodLightGroundRadius);
}
if (bboxMin.x < 0 || bboxMax.x > EXTENT || bboxMin.y < 0 || bboxMax.y > EXTENT) {
this.featuresOnBorder.push({ featureId: feature.id, footprintIndex: this.footprints.length });
}
{
const indices = earcut(footprintFlattened, null, 2);
assert$1(indices.length % 3 === 0);
assert$1(footprintFlattened.length / 2 < 65535);
this.footprintsIndices.resize(this.footprintsIndices.length + indices.length);
this.footprintsIndices.uint16.set(indices, footprintIndexOffset);
const buildingOrFeatureId = buildingId != null ? buildingId : feature.id;
this.buildingIds.add(buildingOrFeatureId);
this.footprintsMin.x = Math.min(this.footprintsMin.x, footprintBoundsMin.x);
this.footprintsMin.y = Math.min(this.footprintsMin.y, footprintBoundsMin.y);
this.footprintsMax.x = Math.max(this.footprintsMax.x, footprintBoundsMax.x);
this.footprintsMax.y = Math.max(this.footprintsMax.y, footprintBoundsMax.y);
const footprint = {
footprintVertexOffset,
footprintVertexLength: this.footprintsVertices.length - footprintVertexOffset,
footprintIndexOffset,
footprintIndexLength: this.footprintsIndices.length - footprintIndexOffset,
min: footprintBoundsMin,
max: footprintBoundsMax,
hiddenFlags: BUILDING_VISIBLE,
indicesOffset: indexArrayRangeStartOffset,
indicesLength: indexArrayRangeLength,
bloomIndicesOffset,
bloomIndicesLength,
groundEffectVertexOffset,
groundEffectVertexLength,
hasFauxFacade,
height: footprintHeight,
promoteId: id,
feature: evaluationFeature,
parts: buildingParts,
buildingBloom
};
const footprintIndex = this.footprints.length;
if (feature.id !== void 0) {
this.featureFootprintLookup.set(feature.id, footprintIndex);
}
addBuildingFootprint(buildingId, footprintIndex);
this.footprints.push(footprint);
}
this.programConfigurations.populatePaintArrays(building.layoutVertexArray.length, feature, index, {}, options.availableImages, canonical, options.brightness);
this.groundEffect.addPaintPropertiesData(feature, index, {}, options.availableImages, canonical, options.brightness);
options.featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index, vertexArrayOffset);
}
disabledFootprintLookup.forEach(({ buildingId, footprintIndex }) => {
if (disabledBuildings.has(buildingId)) {
const footprint = this.footprints[footprintIndex];
footprint.hiddenFlags |= BUILDING_HIDDEN_WITH_INCOMPLETE_PARTS;
}
});
const filteredBuildingIds = /* @__PURE__ */ new Set();
this.buildingIds.forEach((buildingId, value2, set) => {
if (!disabledBuildings.has(buildingId)) {
filteredBuildingIds.add(buildingId);
}
});
this.buildingIds = filteredBuildingIds;
this.groundEffect.prepareBorderSegments();
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "BuildingBucket.populate", "BuildingBucket", perfStartTime, [
["buildingGen", perfBuildingGenTime],
["buildingGenCount", perfBuildingGenCount],
["buildingGenFeatPerMs", features.length / perfBuildingGenTime],
["meshLoopAccuTime", perfMeshLoopAccuTime]
], "primary-dark");
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness) {
const perfStartTime = PerformanceUtils.now();
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness);
this.groundEffect.update(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness);
this.evaluate(this.layers[0], states);
this.colorBufferUploaded = false;
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "BuildingBucket.update", "BuildingBucket", perfStartTime);
}
isEmpty() {
return this.buildingWithoutFacade.layoutVertexArray.length === 0 && this.buildingWithFacade.layoutVertexArray.length === 0;
}
uploadPending() {
return !this.uploaded || this.programConfigurations.needsUpload || this.groundEffect.programConfigurations.needsUpload;
}
upload(context) {
const perfStartTime = PerformanceUtils.now();
const uploadBuilding = (building) => {
building.layoutVertexBuffer = context.createVertexBuffer(building.layoutVertexArray, buildingPositionAttributes.members);
building.layoutNormalBuffer = context.createVertexBuffer(building.layoutNormalArray, buildingNormalAttributes.members);
building.layoutCentroidBuffer = context.createVertexBuffer(building.layoutCentroidArray, buildingCentroidAttributes.members);
building.layoutFloodLightDataBuffer = context.createVertexBuffer(building.layoutFloodLightDataArray, buildingFloodLightWallRadiusAttributes.members);
if (building.layoutFacadeDataArray && building.layoutFacadeDataArray.length) {
building.layoutFacadeDataBuffer = context.createVertexBuffer(building.layoutFacadeDataArray, buildingFacadeDataAttributes.members);
}
if (building.layoutFacadeVerticalRangeArray && building.layoutFacadeVerticalRangeArray.length) {
building.layoutFacadeVerticalRangeBuffer = context.createVertexBuffer(building.layoutFacadeVerticalRangeArray, buildingFacadeVerticalRangeAttributes.members);
}
if (building.entranceBloom.layoutVertexArray.length) {
building.entranceBloom.layoutVertexBuffer = context.createVertexBuffer(building.entranceBloom.layoutVertexArray, buildingPositionAttributes.members);
building.entranceBloom.layoutAttenuationBuffer = context.createVertexBuffer(building.entranceBloom.layoutAttenuationArray, buildingBloomAttenuationAttributes.members);
}
this.uploadUpdatedColorBuffer(context);
this.uploadUpdatedIndexBuffer(context);
};
if (!this.uploaded) {
uploadBuilding(this.buildingWithoutFacade);
uploadBuilding(this.buildingWithFacade);
this.groundEffect.upload(context);
}
this.groundEffect.uploadPaintProperties(context);
this.programConfigurations.upload(context);
this.uploaded = true;
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "BuildingBucket.upload", "BuildingBucket", perfStartTime);
}
destroy() {
const destroyBuilding = (building) => {
if (!building.layoutVertexBuffer) {
return;
}
building.layoutVertexBuffer.destroy();
building.layoutNormalBuffer.destroy();
building.layoutColorBuffer.destroy();
building.segmentsBucket.destroy();
if (building.indexBuffer) {
building.indexBuffer.destroy();
}
if (building.entranceBloom.layoutVertexBuffer) {
building.entranceBloom.layoutVertexBuffer.destroy();
building.entranceBloom.layoutColorBuffer.destroy();
building.entranceBloom.layoutAttenuationBuffer.destroy();
building.entranceBloom.indexBuffer.destroy();
building.entranceBloom.segmentsBucket.destroy();
}
};
destroyBuilding(this.buildingWithoutFacade);
destroyBuilding(this.buildingWithFacade);
this.groundEffect.destroy();
this.programConfigurations.destroy();
}
updateFootprintHiddenFlags(footprintIndices, hiddenFlags, operationSetFlag = true) {
let changed = false;
const orMask = operationSetFlag ? hiddenFlags : 0;
const andMask = (operationSetFlag ? ~0 : ~hiddenFlags) | 0;
if (this.groundEffect.hiddenByLandmarkVertexArray.length === 0) {
this.groundEffect.hiddenByLandmarkVertexArray.resize(this.groundEffect.vertexArray.length);
}
for (const footprintIndex of footprintIndices) {
const footprint = this.footprints[footprintIndex];
const newHiddenFlags = footprint.hiddenFlags & andMask | orMask;
if (footprint.hiddenFlags !== newHiddenFlags) {
footprint.hiddenFlags = newHiddenFlags;
changed = true;
this.groundEffect.updateHiddenByLandmarkRange(footprint.groundEffectVertexOffset, footprint.groundEffectVertexLength, footprint.hiddenFlags !== BUILDING_VISIBLE);
}
}
if (changed) {
this.indexArrayForConflationUploaded = false;
}
return changed;
}
uploadUpdatedIndexBuffer(context) {
this.groundEffect.uploadHiddenByLandmark(context);
if (this.indexArrayForConflationUploaded) {
return;
}
const clearBuildingIndexBuffer = (building) => {
if (building.indexArray.length === 0) {
return;
}
building.indexArrayForConflation.resize(building.indexArray.length);
building.indexArrayForConflation.uint16.set(building.indexArray.uint16);
building.entranceBloom.indexArrayForConflation.resize(building.entranceBloom.indexArray.length);
building.entranceBloom.indexArrayForConflation.uint16.set(building.entranceBloom.indexArray.uint16);
};
clearBuildingIndexBuffer(this.buildingWithoutFacade);
clearBuildingIndexBuffer(this.buildingWithFacade);
for (const footprint of this.footprints) {
const building = footprint.hasFauxFacade ? this.buildingWithFacade : this.buildingWithoutFacade;
const footprintIndicesEnd = footprint.indicesOffset + footprint.indicesLength;
const isVisible = footprint.hiddenFlags === BUILDING_VISIBLE;
if (!isVisible) {
for (let idx = footprint.indicesOffset; idx < footprintIndicesEnd; idx++) {
building.indexArrayForConflation.uint16[idx * 3 + 0] = 0;
building.indexArrayForConflation.uint16[idx * 3 + 1] = 0;
building.indexArrayForConflation.uint16[idx * 3 + 2] = 0;
}
const bloomIndicesEnd = footprint.bloomIndicesOffset + footprint.bloomIndicesLength;
for (let idx = footprint.bloomIndicesOffset; idx < bloomIndicesEnd; idx++) {
building.entranceBloom.indexArrayForConflation.uint16[idx * 3 + 0] = 0;
building.entranceBloom.indexArrayForConflation.uint16[idx * 3 + 1] = 0;
building.entranceBloom.indexArrayForConflation.uint16[idx * 3 + 2] = 0;
}
}
}
const uploadBuildingIndexBuffer = (building) => {
if (building.indexArray.length === 0) {
return;
}
if (!building.indexBuffer) {
building.indexBuffer = context.createIndexBuffer(building.indexArrayForConflation, true);
} else {
building.indexBuffer.updateData(building.indexArrayForConflation);
}
if (!building.entranceBloom.indexBuffer) {
building.entranceBloom.indexBuffer = context.createIndexBuffer(building.entranceBloom.indexArrayForConflation, true);
} else {
building.entranceBloom.indexBuffer.updateData(building.entranceBloom.indexArrayForConflation);
}
};
uploadBuildingIndexBuffer(this.buildingWithoutFacade);
uploadBuildingIndexBuffer(this.buildingWithFacade);
this.indexArrayForConflationUploaded = true;
}
uploadUpdatedColorBuffer(context) {
const uploadBuildingColorBuffer = (building) => {
if (!building.layoutColorBuffer) {
building.layoutColorBuffer = context.createVertexBuffer(building.layoutColorArray, buildingColorAttributes.members, true);
} else {
building.layoutColorBuffer.updateData(building.layoutColorArray);
}
if (building.layoutFacadePaintArray) {
if (!building.layoutFacadePaintBuffer) {
building.layoutFacadePaintBuffer = context.createVertexBuffer(building.layoutFacadePaintArray, buildingFacadePaintAttributes.members, true);
} else {
building.layoutFacadePaintBuffer.updateData(building.layoutFacadePaintArray);
}
}
if (!building.entranceBloom.layoutColorBuffer) {
building.entranceBloom.layoutColorBuffer = context.createVertexBuffer(building.entranceBloom.layoutColorArray, buildingColorAttributes.members, true);
} else {
building.entranceBloom.layoutColorBuffer.updateData(building.entranceBloom.layoutColorArray);
}
};
uploadBuildingColorBuffer(this.buildingWithoutFacade);
uploadBuildingColorBuffer(this.buildingWithFacade);
this.colorBufferUploaded = true;
}
evaluate(layer, featureState) {
const perfStartTime = PerformanceUtils.now();
const aoIntensity = layer.paint.get("building-ambient-occlusion-intensity");
for (const buildingFeature of this.footprints) {
if (buildingFeature.hiddenFlags & BUILDING_HIDDEN_WITH_INCOMPLETE_PARTS) {
continue;
}
const state = featureState[buildingFeature.promoteId];
const feature = buildingFeature.feature;
feature.properties["building-part"] = "roof";
const roofColor = layer.paint.get("building-color").evaluate(feature, state, this.canonical).toPremultipliedRenderColor(this.lut);
const roofEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, state, this.canonical);
feature.properties["building-part"] = "wall";
const wallsColor = layer.paint.get("building-color").evaluate(feature, state, this.canonical).toPremultipliedRenderColor(this.lut);
const wallsEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, state, this.canonical);
feature.properties["building-part"] = "window";
const windowColor = layer.paint.get("building-color").evaluate(feature, state, this.canonical).toPremultipliedRenderColor(this.lut);
const windowEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, state, this.canonical);
feature.properties["building-part"] = "door";
const entranceColor = layer.paint.get("building-color").evaluate(feature, state, this.canonical).toPremultipliedRenderColor(this.lut);
const entranceEmissive = layer.paint.get("building-emissive-strength").evaluate(feature, state, this.canonical);
const building = buildingFeature.hasFauxFacade ? this.buildingWithFacade : this.buildingWithoutFacade;
for (const buildingPart of buildingFeature.parts) {
let color = roofColor;
let emissive;
if (buildingPart.part === BUILDING_PART_ROOF) {
color = roofColor;
emissive = roofEmissive;
} else if (buildingPart.part === BUILDING_PART_WALL) {
color = wallsColor;
emissive = wallsEmissive;
} else if (buildingPart.part === BUILDING_PART_FACADE_GLAZING) {
color = windowColor;
emissive = windowEmissive;
} else if (buildingPart.part === BUILDING_PART_ENTRANCE) {
color = entranceColor;
emissive = entranceEmissive;
}
emissive = clamp(emissive, 0, 1);
for (let i = 0; i < buildingPart.vertexLength; i++) {
const vertexOffset = buildingPart.vertexOffset + i;
const colorFactor = 1 + (building.layoutAOArray.uint8[vertexOffset] / 255 - 1) * aoIntensity;
const r = color.r * colorFactor * 255;
const g = color.g * colorFactor * 255;
const b = color.b * colorFactor * 255;
const e = emissive * 255;
const c1 = r << 8 | g;
const c2 = b << 8 | e;
building.layoutColorArray.emplace(vertexOffset, c1, c2);
if (buildingFeature.hasFauxFacade) {
const r2 = windowColor.r * 255;
const g2 = windowColor.g * 255;
const b2 = windowColor.b * 255;
const e2 = windowEmissive * 255;
const c12 = r2 << 8 | g2;
const c22 = b2 << 8 | e2;
building.layoutFacadePaintArray.emplace(vertexOffset, c12, c22);
}
}
}
const buildingBloom = buildingFeature.buildingBloom;
if (buildingBloom) {
for (let i = 0; i < buildingBloom.vertexLength; i++) {
const vertexOffset = buildingBloom.vertexOffset + i;
const r = entranceColor.r * 255;
const g = entranceColor.g * 255;
const b = entranceColor.b * 255;
const e = entranceEmissive * 51;
const c1 = r << 8 | g;
const c2 = b << 8 | e;
building.entranceBloom.layoutColorArray.emplace(vertexOffset, c1, c2);
}
}
}
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "BuildingBucket.evaluate", "BuildingBucket", perfStartTime);
}
needsEvaluation() {
return !this.colorBufferUploaded;
}
updateReplacement(coord, source, layerIndex) {
const perfStartTime = PerformanceUtils.now();
if (source.updateTime === this.replacementUpdateTime) {
return;
}
this.replacementUpdateTime = source.updateTime;
const newReplacements = source.getReplacementRegionsForTile(coord.toUnwrapped());
if (regionsEquals(this.activeReplacements, newReplacements)) {
return;
}
this.activeReplacements = newReplacements;
for (const footprint of this.footprints) {
footprint.hiddenFlags &= ~BUILDING_HIDDEN_BY_REPLACEMENT;
}
const transformedVertices = [];
for (const region of this.activeReplacements) {
if (region.order <= ReplacementOrderBuilding) continue;
const padding = Math.max(1, Math.pow(2, region.footprintTileId.canonical.z - coord.canonical.z));
for (const footprint of this.footprints) {
if (footprint.min.x > region.max.x || footprint.max.x < region.min.x) {
continue;
} else if (footprint.min.y > region.max.y || footprint.max.y < region.min.y) {
continue;
}
transformedVertices.length = 0;
transformFootprintVerticesFloat32(
this.footprintsVertices,
footprint.footprintVertexOffset,
footprint.footprintVertexLength,
region.footprintTileId.canonical,
coord.canonical,
transformedVertices
);
if (footprintTrianglesIntersect(
region.footprint,
transformedVertices,
this.footprintsIndices.uint16,
footprint.footprintIndexOffset,
footprint.footprintIndexLength,
0,
-padding
)) {
footprint.hiddenFlags |= BUILDING_HIDDEN_BY_REPLACEMENT;
}
}
}
if (this.groundEffect.hiddenByLandmarkVertexArray.length === 0) {
this.groundEffect.hiddenByLandmarkVertexArray.resize(this.groundEffect.vertexArray.length);
}
for (const footprint of this.footprints) {
this.groundEffect.updateHiddenByLandmarkRange(footprint.groundEffectVertexOffset, footprint.groundEffectVertexLength, footprint.hiddenFlags !== BUILDING_VISIBLE);
}
this.indexArrayForConflationUploaded = false;
PerformanceUtils.measureWithDetails(PerformanceUtils.GROUP_COMMON, "BuildingBucket.updateReplacement", "BuildingBucket", perfStartTime);
}
getFootprint(feature) {
if (feature.id !== void 0) {
assert$1(this.featureFootprintLookup.has(feature.id));
const footprintIndex = this.featureFootprintLookup.get(feature.id);
return this.footprints[footprintIndex];
}
return null;
}
getHeightAtTileCoord(x, y) {
let height = Number.NEGATIVE_INFINITY;
let hidden = true;
assert$1(x > -EXTENT && y > -EXTENT && x < 2 * EXTENT && y < 2 * EXTENT);
const lookupKey = (x + EXTENT) * 4 * EXTENT + (y + EXTENT);
if (this.footprintLookup.hasOwnProperty(lookupKey)) {
const footprint = this.footprintLookup[lookupKey];
return footprint ? { height: footprint.height, hidden: footprint.hiddenFlags !== BUILDING_VISIBLE } : void 0;
}
const pt = new Point(x, y);
for (const footprint of this.footprints) {
if (x > footprint.max.x || footprint.min.x > x || y > footprint.max.y || footprint.min.y > y) {
continue;
}
if (footprint.height <= height) {
continue;
}
const footprintVertices = this.footprintsVertices.float32.subarray(footprint.footprintVertexOffset * 2, (footprint.footprintVertexOffset + footprint.footprintVertexLength) * 2);
const footprintIndices = this.footprintsIndices.uint16.subarray(footprint.footprintIndexOffset, footprint.footprintIndexOffset + footprint.footprintIndexLength);
if (pointInTriangleMesh(pt, footprintVertices, footprintIndices)) {
height = footprint.height;
this.footprintLookup[lookupKey] = footprint;
hidden = footprint.hiddenFlags !== BUILDING_VISIBLE;
}
}
if (height === Number.NEGATIVE_INFINITY) {
this.footprintLookup[lookupKey] = void 0;
return;
}
return { height, hidden };
}
}
function pointInTriangleMesh(pt, vertices, indices) {
for (let triIndex = 0; triIndex < indices.length; triIndex += 3) {
const i0 = indices[triIndex];
const i1 = indices[triIndex + 1];
const i2 = indices[triIndex + 2];
const p0X = vertices[i0 * 2 + 0];
const p0Y = vertices[i0 * 2 + 1];
const p1X = vertices[i1 * 2 + 0];
const p1Y = vertices[i1 * 2 + 1];
const p2X = vertices[i2 * 2 + 0];
const p2Y = vertices[i2 * 2 + 1];
const s = (p0X - p2X) * (pt.y - p2Y) - (p0Y - p2Y) * (pt.x - p2X);
const t = (p1X - p0X) * (pt.y - p0Y) - (p1Y - p0Y) * (pt.x - p0X);
if (s < 0 !== t < 0 && s !== 0 && t !== 0)
continue;
const d = (p2X - p1X) * (pt.y - p1Y) - (p2Y - p1Y) * (pt.x - p1X);
if (d === 0 || d < 0 === s + t <= 0) {
return true;
}
}
return false;
}
function transformFootprintVerticesFloat32(vertices, offset, count, footprintId, centroidId, out) {
const zDiff = Math.pow(2, footprintId.z - centroidId.z);
for (let i = 0; i < count; i++) {
let x = vertices.float32[(i + offset) * 2 + 0];
let y = vertices.float32[(i + offset) * 2 + 1];
x = (x + centroidId.x * EXTENT) * zDiff - footprintId.x * EXTENT;
y = (y + centroidId.y * EXTENT) * zDiff - footprintId.y * EXTENT;
out.push(new Point(x, y));
}
}
register(BuildingBucket, "BuildingBucket", { omit: ["layers"] });
register(BuildingGeometry, "BuildingGeometry");
register(BuildingBloomGeometry, "BuildingBloomGeometry");
let layout$7;
const getLayoutProperties$8 = () => layout$7 || (layout$7 = new Properties({
"visibility": new DataConstantProperty(spec["layout_building"]["visibility"]),
"building-facade": new DataDrivenProperty(spec["layout_building"]["building-facade"]),
"building-facade-floors": new DataDrivenProperty(spec["layout_building"]["building-facade-floors"]),
"building-facade-unit-width": new DataDrivenProperty(spec["layout_building"]["building-facade-unit-width"]),
"building-facade-window": new DataDrivenProperty(spec["layout_building"]["building-facade-window"]),
"building-roof-shape": new DataDrivenProperty(spec["layout_building"]["building-roof-shape"]),
"building-height": new DataDrivenProperty(spec["layout_building"]["building-height"]),
"building-base": new DataDrivenProperty(spec["layout_building"]["building-base"]),
"building-flood-light-wall-radius": new DataDrivenProperty(spec["layout_building"]["building-flood-light-wall-radius"]),
"building-flood-light-ground-radius": new DataDrivenProperty(spec["layout_building"]["building-flood-light-ground-radius"]),
"building-flip-roof-orientation": new DataDrivenProperty(spec["layout_building"]["building-flip-roof-orientation"])
}));
let paint$8;
const getPaintProperties$8 = () => paint$8 || (paint$8 = new Properties({
"building-opacity": new DataConstantProperty(spec["paint_building"]["building-opacity"]),
"building-ambient-occlusion-intensity": new DataConstantProperty(spec["paint_building"]["building-ambient-occlusion-intensity"]),
"building-ambient-occlusion-ground-intensity": new DataConstantProperty(spec["paint_building"]["building-ambient-occlusion-ground-intensity"]),
"building-ambient-occlusion-ground-radius": new DataConstantProperty(spec["paint_building"]["building-ambient-occlusion-ground-radius"]),
"building-ambient-occlusion-ground-attenuation": new DataConstantProperty(spec["paint_building"]["building-ambient-occlusion-ground-attenuation"]),
"building-vertical-scale": new DataConstantProperty(spec["paint_building"]["building-vertical-scale"]),
"building-cast-shadows": new DataConstantProperty(spec["paint_building"]["building-cast-shadows"]),
"building-color": new DataDrivenProperty(spec["paint_building"]["building-color"]),
"building-emissive-strength": new DataDrivenProperty(spec["paint_building"]["building-emissive-strength"]),
"building-facade-emissive-chance": new DataConstantProperty(spec["paint_building"]["building-facade-emissive-chance"]),
"building-cutoff-fade-range": new DataConstantProperty(spec["paint_building"]["building-cutoff-fade-range"]),
"building-flood-light-color": new DataConstantProperty(spec["paint_building"]["building-flood-light-color"]),
"building-flood-light-intensity": new DataConstantProperty(spec["paint_building"]["building-flood-light-intensity"]),
"building-flood-light-ground-attenuation": new DataConstantProperty(spec["paint_building"]["building-flood-light-ground-attenuation"]),
"building-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"building-flood-light-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class BuildingStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$8(),
paint: getPaintProperties$8()
};
super(layer, properties, scope, lut, options);
this._stats = { numRenderedVerticesInShadowPass: 0, numRenderedVerticesInTransparentPass: 0 };
}
createBucket(parameters) {
return new BuildingBucket(parameters);
}
cutoffRange() {
return this.paint.get("building-cutoff-fade-range");
}
hasShadowPass() {
return this.paint.get("building-cast-shadows");
}
hasLightBeamPass() {
return true;
}
canCastShadows() {
return true;
}
is3D(terrainEnabled) {
return true;
}
queryRadius(bucket) {
return 0;
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform, pixelPosMatrix, elevationHelper, layoutVertexArrayOffset) {
let height = this.layout.get("building-height").evaluate(feature, featureState);
const base = this.layout.get("building-base").evaluate(feature, featureState);
const bucket = queryGeometry.tile.getBucket(this);
assert$1(bucket instanceof BuildingBucket);
const footprint = bucket.getFootprint(feature);
if (footprint) {
if (footprint.hiddenFlags !== BUILDING_VISIBLE) {
return false;
}
height = footprint.height;
}
const demSampler = null;
const centroid = [0, 0];
const exaggeration = 1;
const [projectedBase, projectedTop] = projectExtrusion(transform, geometry, base, height, new Point(0, 0), pixelPosMatrix, demSampler, centroid, exaggeration, transform.center.lat, queryGeometry.tileID.canonical);
const screenQuery = queryGeometry.queryGeometry;
const projectedQueryGeometry = screenQuery.isPointQuery() ? screenQuery.screenBounds : screenQuery.screenGeometry;
return checkIntersection(projectedBase, projectedTop, projectedQueryGeometry);
}
}
const lineLayoutAttributes = createLayout([
{ name: "a_pos_normal", components: 2, type: "Int16" },
{ name: "a_data", components: 4, type: "Uint8" },
{ name: "a_linesofar", components: 1, type: "Float32" }
], 4);
const lineZOffsetAttributes = createLayout([
{ name: "a_z_offset_width", components: 3, type: "Float32" }
], 4);
const { members: members$2, size: size$2, alignment: alignment$2 } = lineLayoutAttributes;
const lineLayoutAttributesExt = createLayout([
{ name: "a_packed", components: 3, type: "Float32" }
]);
const { members: members$1, size: size$1, alignment: alignment$1 } = lineLayoutAttributesExt;
const lineLayoutAttributesPattern = createLayout([
{ name: "a_pattern_data", components: 3, type: "Float32" }
]);
const { members, size, alignment } = lineLayoutAttributesPattern;
class LineAtlas {
constructor(width, height) {
this.width = width;
this.height = height;
this.nextRow = 0;
this.image = new AlphaImage({ width, height });
this.positions = {};
this.uploaded = false;
}
/**
* Get a dash line pattern.
*
* @param {Array} dasharray
* @param {string} lineCap the type of line caps to be added to dashes
* @returns {Object} position of dash texture in { y, height, width }
* @private
*/
getDash(dasharray, lineCap) {
const key = this.getKey(dasharray, lineCap);
return this.positions[key];
}
trim() {
const width = this.width;
const height = this.height = nextPowerOfTwo(this.nextRow);
this.image.resize({ width, height });
}
getKey(dasharray, lineCap) {
return dasharray.join(",") + lineCap;
}
getDashRanges(dasharray, lineAtlasWidth, stretch) {
const oddDashArray = dasharray.length % 2 === 1;
const ranges = [];
let left = oddDashArray ? -dasharray[dasharray.length - 1] * stretch : 0;
let right = dasharray[0] * stretch;
let isDash = true;
ranges.push({ left, right, isDash, zeroLength: dasharray[0] === 0 });
let currentDashLength = dasharray[0];
for (let i = 1; i < dasharray.length; i++) {
isDash = !isDash;
const dashLength = dasharray[i];
left = currentDashLength * stretch;
currentDashLength += dashLength;
right = currentDashLength * stretch;
ranges.push({ left, right, isDash, zeroLength: dashLength === 0 });
}
return ranges;
}
addRoundDash(ranges, stretch, n) {
const halfStretch = stretch / 2;
for (let y = -n; y <= n; y++) {
const row = this.nextRow + n + y;
const index = this.width * row;
let currIndex = 0;
let range = ranges[currIndex];
for (let x = 0; x < this.width; x++) {
if (x / range.right > 1) {
range = ranges[++currIndex];
}
const distLeft = Math.abs(x - range.left);
const distRight = Math.abs(x - range.right);
const minDist = Math.min(distLeft, distRight);
let signedDistance;
const distMiddle = y / n * (halfStretch + 1);
if (range.isDash) {
const distEdge = halfStretch - Math.abs(distMiddle);
signedDistance = Math.sqrt(minDist * minDist + distEdge * distEdge);
} else {
signedDistance = halfStretch - Math.sqrt(minDist * minDist + distMiddle * distMiddle);
}
this.image.data[index + x] = Math.max(0, Math.min(255, signedDistance + 128));
}
}
}
addRegularDash(ranges, capLength) {
for (let i = ranges.length - 1; i >= 0; --i) {
const part = ranges[i];
const next = ranges[i + 1];
if (part.zeroLength) {
ranges.splice(i, 1);
} else if (next && next.isDash === part.isDash) {
next.left = part.left;
ranges.splice(i, 1);
}
}
const first = ranges[0];
const last = ranges[ranges.length - 1];
if (first.isDash === last.isDash) {
first.left = last.left - this.width;
last.right = first.right + this.width;
}
const index = this.width * this.nextRow;
let currIndex = 0;
let range = ranges[currIndex];
for (let x = 0; x < this.width; x++) {
if (x / range.right > 1) {
range = ranges[++currIndex];
}
const distLeft = Math.abs(x - range.left);
const distRight = Math.abs(x - range.right);
const minDist = Math.min(distLeft, distRight);
const signedDistance = (range.isDash ? minDist : -minDist) + capLength;
this.image.data[index + x] = Math.max(0, Math.min(255, signedDistance + 128));
}
}
addDash(dasharray, lineCap) {
const key = this.getKey(dasharray, lineCap);
if (this.positions[key]) return this.positions[key];
const round = lineCap === "round";
const n = round ? 7 : 0;
const height = 2 * n + 1;
if (this.nextRow + height > this.height) {
warnOnce("LineAtlas out of space");
return null;
}
if (dasharray.length === 0) {
dasharray.push(1);
}
let length = 0;
for (let i = 0; i < dasharray.length; i++) {
if (dasharray[i] < 0) {
warnOnce("Negative value is found in line dasharray, replacing values with 0");
dasharray[i] = 0;
}
length += dasharray[i];
}
if (length !== 0) {
const stretch = this.width / length;
const ranges = this.getDashRanges(dasharray, this.width, stretch);
if (round) {
this.addRoundDash(ranges, stretch, n);
} else {
const capLength = lineCap === "square" ? 0.5 * stretch : 0;
this.addRegularDash(ranges, capLength);
}
}
const y = this.nextRow + n;
this.nextRow += height;
const pos = {
tl: [y, n],
br: [length, 0]
};
this.positions[key] = pos;
return pos;
}
}
register(LineAtlas, "LineAtlas");
const vectorTileFeatureTypes$1 = VectorTileFeature.types;
const EXTRUDE_SCALE = 63;
const COS_HALF_SHARP_CORNER = Math.cos(75 / 2 * (Math.PI / 180));
const COS_STRAIGHT_CORNER = Math.cos(5 * (Math.PI / 180));
const DEG_PER_TRIANGLE = 20;
class LineBucket {
constructor(options) {
this.evaluationGlobals = { "zoom": 0, "lineProgress": void 0 };
this.elevationType = "none";
this.zoom = options.zoom;
this.evaluationGlobals.zoom = this.zoom;
this.overscaling = options.overscaling;
this.pixelRatio = options.pixelRatio;
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.index = options.index;
this.projection = options.projection;
this.hasPattern = false;
this.hasCrossSlope = false;
this.patternFeatures = [];
this.lineClipsArray = [];
this.gradients = {};
this.layers.forEach((layer) => {
this.gradients[layer.id] = {};
});
this.layoutVertexArray = new StructArrayLayout2i4ub1f12();
this.layoutVertexArray2 = new StructArrayLayout3f12();
this.patternVertexArray = new StructArrayLayout3f12();
this.indexArray = new StructArrayLayout3ui6();
this.programConfigurations = new ProgramConfigurationSet(options.layers, { zoom: options.zoom, lut: options.lut });
this.segments = new SegmentVector();
this.maxLineLength = 0;
this.zOffsetVertexArray = new StructArrayLayout3f12();
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.tessellationStep = options.tessellationStep ? options.tessellationStep : EXTENT / 64;
this.worldview = options.worldview;
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
this.hasPattern = hasPattern("line", this.layers, this.pixelRatio, options);
const lineSortKey = this.layers[0].layout.get("line-sort-key");
this.tileToMeter = tileToMeter(canonical);
const elevationReference = this.layers[0].layout.get("line-elevation-reference");
if (elevationReference === "hd-road-markup") {
this.elevationType = "road";
} else {
const zOffset = this.layers[0].layout.get("line-z-offset");
const zOffsetZero = zOffset.isConstant() && !zOffset.constantOr(0);
const seaOrGroundReference = elevationReference === "sea" || elevationReference === "ground";
this.elevationType = seaOrGroundReference || !zOffsetZero ? "offset" : "none";
if (this.elevationType === "offset" && elevationReference === "none") {
warnOnce(`line-elevation-reference: ground is used for the layer ${this.layerIds[0]} because non-zero line-z-offset value was found.`);
}
}
const crossSlope = this.layers[0].layout.get("line-cross-slope");
this.hasCrossSlope = this.elevationType === "offset" && crossSlope !== void 0;
const bucketFeatures = [];
for (const { feature, id, index, sourceLayerIndex } of features) {
const needGeometry = this.layers[0]._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const sortKey = lineSortKey ? lineSortKey.evaluate(evaluationFeature, {}, canonical) : void 0;
const bucketFeature = {
id,
properties: feature.properties,
type: feature.type,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
patterns: {},
sortKey
};
bucketFeatures.push(bucketFeature);
}
if (lineSortKey) {
bucketFeatures.sort((a, b) => {
return a.sortKey - b.sortKey;
});
}
const { lineAtlas, featureIndex } = options;
const hasFeatureDashes = this.addConstantDashes(lineAtlas);
for (const bucketFeature of bucketFeatures) {
const { geometry, index, sourceLayerIndex } = bucketFeature;
if (hasFeatureDashes) {
this.addFeatureDashes(bucketFeature, lineAtlas);
}
if (this.hasPattern) {
const patternBucketFeature = addPatternDependencies("line", this.layers, bucketFeature, this.zoom, this.pixelRatio, options);
this.patternFeatures.push(patternBucketFeature);
} else {
this.addFeature(bucketFeature, geometry, index, canonical, lineAtlas.positions, options.availableImages, options.brightness, options.elevationFeatures);
}
const feature = features[index].feature;
featureIndex.insert(feature, geometry, index, sourceLayerIndex, this.index);
}
}
addConstantDashes(lineAtlas) {
let hasFeatureDashes = false;
for (const layer of this.layers) {
const dashPropertyValue = layer.paint.get("line-dasharray").value;
const capPropertyValue = layer.layout.get("line-cap").value;
if (dashPropertyValue.kind !== "constant" || capPropertyValue.kind !== "constant") {
hasFeatureDashes = true;
} else {
const constCap = capPropertyValue.value;
const constDash = dashPropertyValue.value;
if (!constDash) continue;
lineAtlas.addDash(constDash, constCap);
}
}
return hasFeatureDashes;
}
addFeatureDashes(feature, lineAtlas) {
const zoom = this.zoom;
for (const layer of this.layers) {
const dashPropertyValue = layer.paint.get("line-dasharray").value;
const capPropertyValue = layer.layout.get("line-cap").value;
if (dashPropertyValue.kind === "constant" && capPropertyValue.kind === "constant") continue;
let dashArray, cap;
if (dashPropertyValue.kind === "constant") {
dashArray = dashPropertyValue.value;
if (!dashArray) continue;
} else {
dashArray = dashPropertyValue.evaluate({ zoom }, feature);
}
if (capPropertyValue.kind === "constant") {
cap = capPropertyValue.value;
} else {
cap = capPropertyValue.evaluate({ zoom }, feature);
}
lineAtlas.addDash(dashArray, cap);
feature.patterns[layer.id] = [lineAtlas.getKey(dashArray, cap)];
}
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness, worldview) {
this.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, worldview);
}
addFeatures(options, canonical, imagePositions, availableImages, _, brightness) {
for (const feature of this.patternFeatures) {
this.addFeature(feature, feature.geometry, feature.index, canonical, imagePositions, availableImages, brightness, options.elevationFeatures);
}
}
isEmpty() {
return this.layoutVertexArray.length === 0;
}
uploadPending() {
return !this.uploaded || this.programConfigurations.needsUpload;
}
upload(context) {
if (!this.uploaded) {
if (this.layoutVertexArray2.length !== 0) {
this.layoutVertexBuffer2 = context.createVertexBuffer(this.layoutVertexArray2, members$1);
}
if (this.patternVertexArray.length !== 0) {
this.patternVertexBuffer = context.createVertexBuffer(this.patternVertexArray, members);
}
if (!this.zOffsetVertexBuffer && this.zOffsetVertexArray.length > 0) {
this.zOffsetVertexBuffer = context.createVertexBuffer(this.zOffsetVertexArray, lineZOffsetAttributes.members, true);
}
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, members$2);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
}
this.programConfigurations.upload(context);
this.uploaded = true;
}
destroy() {
if (!this.layoutVertexBuffer) return;
if (this.zOffsetVertexBuffer) {
this.zOffsetVertexBuffer.destroy();
}
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.programConfigurations.destroy();
this.segments.destroy();
}
lineFeatureClips(feature, multiLineMetricsIndex) {
let startProp;
let endProp;
if (multiLineMetricsIndex && multiLineMetricsIndex > 0) {
startProp = `mapbox_clip_start_${multiLineMetricsIndex}`;
endProp = `mapbox_clip_end_${multiLineMetricsIndex}`;
} else {
startProp = "mapbox_clip_start";
endProp = "mapbox_clip_end";
}
if (!!feature.properties && feature.properties.hasOwnProperty(startProp) && feature.properties.hasOwnProperty(endProp)) {
const start = +feature.properties[startProp];
const end = +feature.properties[endProp];
return { start, end };
}
}
addFeature(feature, geometry, index, canonical, imagePositions, availableImages, brightness, elevationFeatures) {
const layout = this.layers[0].layout;
const join = layout.get("line-join").evaluate(feature, {});
const cap = layout.get("line-cap").evaluate(feature, {});
const miterLimit = layout.get("line-miter-limit");
const roundLimit = layout.get("line-round-limit");
this.lineClips = this.lineFeatureClips(feature);
this.lineFeature = feature;
const hasMapboxLineMetrics = !!feature.properties && feature.properties.hasOwnProperty("mapbox_line_metrics") ? feature.properties["mapbox_line_metrics"] : false;
this.zOffsetValue = layout.get("line-z-offset").value;
const paint = this.layers[0].paint;
const lineWidth = paint.get("line-width").value;
if (lineWidth.kind !== "constant" && lineWidth.isLineProgressConstant === false) {
this.variableWidthValue = lineWidth;
}
if (this.elevationType === "road") {
const vertexOffset = this.layoutVertexArray.length;
const added = this.addElevatedRoadFeature(feature, geometry, canonical, elevationFeatures, join, cap, miterLimit, roundLimit);
if (!added) {
const [clippedLines, linesInfo] = this.clipRuntimeLinesToTile(geometry, ELEVATION_CLIP_MARGIN);
for (let i = 0; i < clippedLines.length; i++) {
const line = clippedLines[i];
const info = linesInfo[i];
const subseg = {
progress: { min: info.progress.min, max: info.progress.max },
nextDir: this.computeSegNextDir(info, line),
prevDir: this.computeSegPrevDir(info, line)
};
const multiLineMetricsIndex = hasMapboxLineMetrics && info.parentIndex > 0 ? info.parentIndex : null;
this.addLine(line, feature, canonical, join, cap, miterLimit, roundLimit, subseg, multiLineMetricsIndex);
}
this.fillNonElevatedRoadSegment(vertexOffset);
}
} else {
for (let i = 0; i < geometry.length; i++) {
const line = geometry[i];
const multiLineMetricsIndex = hasMapboxLineMetrics && i > 0 ? i : null;
this.addLine(line, feature, canonical, join, cap, miterLimit, roundLimit, void 0, multiLineMetricsIndex);
}
}
this.programConfigurations.populatePaintArrays(this.layoutVertexArray.length, feature, index, imagePositions, availableImages, canonical, brightness, void 0, this.worldview);
}
computeSegNextDir(info, line) {
assert$1(line.length > 1);
return info.nextPoint.sub(line.at(-2)).unit();
}
computeSegPrevDir(info, line) {
assert$1(line.length > 1);
return line[1].sub(info.prevPoint).unit();
}
clipLinesToTile(lines, margin) {
return clipLines(lines, -margin, -margin, EXTENT + margin, EXTENT + margin);
}
clipRuntimeLinesToTile(lines, margin) {
const linesInfo = [];
const clipped = clipLines(lines, -margin, -margin, EXTENT + margin, EXTENT + margin, linesInfo);
return [clipped, linesInfo];
}
addElevatedRoadFeature(feature, geometry, canonical, elevationFeatures, join, cap, miterLimit, roundLimit) {
const elevatedGeometry = [];
const tiledElevation = ElevationFeatures.getElevationFeature(feature, elevationFeatures);
if (tiledElevation) {
const clippedLines = this.clipLinesToTile(geometry, ELEVATION_CLIP_MARGIN);
const preparedLines = this.prepareElevatedLines(clippedLines, tiledElevation, canonical);
for (const line of preparedLines) {
elevatedGeometry.push({
geometry: line,
elevation: tiledElevation,
elevationTileID: canonical,
segment: { progress: { min: 0, max: 1 }, nextDir: void 0, prevDir: void 0 }
});
}
}
if (elevatedGeometry.length === 0) return false;
for (const elevated of elevatedGeometry) {
const vertexOffset = this.layoutVertexArray.length;
this.addLine(elevated.geometry, feature, canonical, join, cap, miterLimit, roundLimit);
const sampler = new ElevationFeatureSampler(canonical, elevated.elevationTileID);
if (elevated.elevation) {
for (let i = vertexOffset; i < this.layoutVertexArray.length; i++) {
const point = new Point(this.layoutVertexArray.int16[i * 6] >> 1, this.layoutVertexArray.int16[i * 6 + 1] >> 1);
const height = sampler.pointElevation(point, elevated.elevation, MARKUP_ELEVATION_BIAS);
this.updateHeightRange(height);
this.zOffsetVertexArray.emplaceBack(height, 0, 0);
}
} else {
this.fillNonElevatedRoadSegment(vertexOffset);
}
assert$1(this.layoutVertexArray.length === this.zOffsetVertexArray.length);
}
return true;
}
prepareElevatedLines(lines, elevation, tileID) {
if (elevation.constantHeight != null) {
return lines;
}
const splitLines = [];
const metersToTile = 1 / tileToMeter(tileID);
for (const line of lines) {
lineSubdivision(line, new EdgeIterator(elevation, metersToTile), false, splitLines);
}
return splitLines;
}
fillNonElevatedRoadSegment(vertexOffset) {
for (let i = vertexOffset; i < this.layoutVertexArray.length; i++) {
this.zOffsetVertexArray.emplaceBack(0, 0, 0);
}
}
updateHeightRange(height) {
if (this.heightRange) {
this.heightRange.min = Math.min(this.heightRange.min, height);
this.heightRange.max = Math.max(this.heightRange.max, height);
} else {
this.heightRange = { min: height, max: height };
}
}
addLine(vertices, feature, canonical, join, cap, miterLimit, roundLimit, subsegment, multiLineMetricsIndex) {
this.distance = 0;
this.prevDistance = 0;
this.scaledDistance = 0;
this.totalDistance = 0;
this.totalFeatureLength = 0;
this.lineSoFar = 0;
this.currentVertex = void 0;
this.lineClips = multiLineMetricsIndex ? this.lineFeatureClips(feature, multiLineMetricsIndex) : this.lineClips;
const joinNone = join === "none";
this.patternJoinNone = this.hasPattern && joinNone;
this.segmentStart = 0;
this.segmentStartf32 = 0;
this.segmentPoints = [];
const intermediateStartPoint = subsegment && subsegment.progress.min > 0;
const intermediateEndPoint = subsegment && subsegment.progress.max < 1;
if (this.lineClips) {
let range = { min: this.lineClips.start, max: this.lineClips.end };
let clipRangeScale = 1;
if (subsegment) {
const clipRange = this.lineClips.end - this.lineClips.start;
range = mapRange(subsegment.progress, { min: 0, max: 1 }, range);
if (clipRange > 0) {
clipRangeScale = (range.max - range.min) / clipRange;
}
}
const featureLen = +feature.properties["mapbox_clip_feature_len"];
const segmentLen = +feature.properties["mapbox_clip_seg_len"];
if (!Number.isNaN(featureLen) && !Number.isNaN(segmentLen)) {
this.totalFeatureLength = featureLen;
this.distance = segmentLen * clipRangeScale;
this.lineClips.start = range.min;
this.lineClips.end = range.max;
this.maxLineLength = Math.max(this.maxLineLength, this.distance);
} else {
for (let i = 0; i < vertices.length - 1; i++) {
this.totalDistance += vertices[i].dist(vertices[i + 1]);
}
const featureLen2 = this.totalDistance / (range.max - range.min);
this.totalFeatureLength = Number.isFinite(featureLen2) ? featureLen2 : 0;
this.lineClips.start = range.min;
this.lineClips.end = range.max;
this.maxLineLength = Math.max(this.maxLineLength, this.totalDistance);
}
this.lineClipsArray.push(this.lineClips);
this.updateScaledDistance();
}
const isPolygon = vectorTileFeatureTypes$1[feature.type] === "Polygon";
let len = vertices.length;
while (len >= 2 && vertices[len - 1].equals(vertices[len - 2])) {
len--;
}
let first = 0;
while (first < len - 1 && vertices[first].equals(vertices[first + 1])) {
first++;
}
if (len < (isPolygon ? 3 : 2)) return;
if (join === "bevel") miterLimit = 1.05;
const segment = this.segments.prepareSegment(len * 10, this.layoutVertexArray, this.indexArray);
let currentVertex;
let prevVertex;
let nextVertex;
let prevNormal;
let nextNormal;
let startNormal;
let endNormal;
if (subsegment && subsegment.prevDir) {
startNormal = subsegment.prevDir.perp();
}
if (subsegment && subsegment.nextDir) {
endNormal = subsegment.nextDir.perp();
}
this.e1 = this.e2 = -1;
if (isPolygon) {
currentVertex = vertices[len - 2];
nextNormal = vertices[first].sub(currentVertex)._unit()._perp();
}
let lineProgressFeatures;
for (let i = first; i < len; i++) {
nextVertex = i === len - 1 ? isPolygon ? vertices[first + 1] : void 0 : (
// if it's a polygon, treat the last vertex like the first
vertices[i + 1]
);
if (nextVertex && vertices[i].equals(nextVertex)) continue;
if (nextNormal) prevNormal = nextNormal;
if (currentVertex) prevVertex = currentVertex;
currentVertex = vertices[i];
lineProgressFeatures = this.evaluateLineProgressFeatures(prevVertex ? prevVertex.dist(currentVertex) : 0);
nextNormal = nextVertex ? nextVertex.sub(currentVertex)._unit()._perp() : prevNormal;
prevNormal = prevNormal || nextNormal;
const middleVertex = prevVertex && nextVertex;
let currentJoin = middleVertex ? join : isPolygon || joinNone ? "butt" : cap;
const cosAngle = prevNormal.x * nextNormal.x + prevNormal.y * nextNormal.y;
if (joinNone) {
const endLineSegment = function(bucket) {
if (bucket.patternJoinNone) {
const pointCount = bucket.segmentPoints.length / 2;
const segmentLength = bucket.lineSoFar - bucket.segmentStart;
for (let idx = 0; idx < pointCount; ++idx) {
const pos = bucket.segmentPoints[idx * 2];
const offsetSign = bucket.segmentPoints[idx * 2 + 1];
const posAndOffset = Math.round(pos) + 0.5 + offsetSign * 0.25;
bucket.patternVertexArray.emplaceBack(posAndOffset, segmentLength, bucket.segmentStart);
bucket.patternVertexArray.emplaceBack(posAndOffset, segmentLength, bucket.segmentStart);
}
bucket.segmentPoints.length = 0;
assert$1(bucket.zOffsetVertexArray.length === bucket.patternVertexArray.length || bucket.elevationType !== "offset");
}
bucket.e1 = bucket.e2 = -1;
};
if (middleVertex && cosAngle < COS_STRAIGHT_CORNER) {
this.updateDistance(prevVertex, currentVertex);
this.addCurrentVertex(currentVertex, prevNormal, 1, 1, segment, lineProgressFeatures);
endLineSegment(this);
this.addCurrentVertex(currentVertex, nextNormal, -1, -1, segment, lineProgressFeatures);
continue;
} else if (prevVertex) {
if (!nextVertex) {
this.updateDistance(prevVertex, currentVertex);
this.addCurrentVertex(currentVertex, prevNormal, 1, 1, segment, lineProgressFeatures);
endLineSegment(this);
continue;
} else {
currentJoin = "miter";
}
}
}
let joinNormal = prevNormal.add(nextNormal);
if (joinNormal.x !== 0 || joinNormal.y !== 0) {
joinNormal._unit();
}
const cosHalfAngle = joinNormal.x * nextNormal.x + joinNormal.y * nextNormal.y;
const miterLength = cosHalfAngle !== 0 ? 1 / cosHalfAngle : Infinity;
const approxAngle = 2 * Math.sqrt(2 - 2 * cosHalfAngle);
const isSharpCorner = cosHalfAngle < COS_HALF_SHARP_CORNER && prevVertex && nextVertex;
const lineTurnsLeft = prevNormal.x * nextNormal.y - prevNormal.y * nextNormal.x > 0;
const SHARP_CORNER_OFFSET = 15;
const sharpCornerOffset = this.overscaling <= 16 ? SHARP_CORNER_OFFSET * EXTENT / (512 * this.overscaling) : 0;
if (middleVertex && currentJoin === "round") {
if (miterLength < roundLimit) {
currentJoin = "miter";
} else if (miterLength <= 2) {
const boundsMin = -10;
const boundsMax = EXTENT + 10;
const outside = pointOutsideBounds(currentVertex, boundsMin, boundsMax);
currentJoin = this.elevationType === "offset" && (outside || this.hasCrossSlope) ? "miter" : "fakeround";
}
}
if (currentJoin === "miter" && miterLength > miterLimit) {
currentJoin = "bevel";
}
if (currentJoin === "bevel") {
if (miterLength > 2) currentJoin = "flipbevel";
if (miterLength < miterLimit) currentJoin = "miter";
}
const sharpMiter = currentJoin === "miter" && isSharpCorner;
if (prevVertex && !sharpMiter) this.updateDistance(prevVertex, currentVertex);
if (currentJoin === "miter") {
if (isSharpCorner) {
const prevSegmentLength = currentVertex.dist(prevVertex);
if (prevSegmentLength > 2 * sharpCornerOffset) {
const newPrevVertex = currentVertex.sub(currentVertex.sub(prevVertex)._mult(sharpCornerOffset / prevSegmentLength)._round());
this.updateDistance(prevVertex, newPrevVertex);
this.addCurrentVertex(newPrevVertex, prevNormal, 0, 0, segment, lineProgressFeatures);
prevVertex = newPrevVertex;
}
this.updateDistance(prevVertex, currentVertex);
joinNormal._mult(miterLength);
this.addCurrentVertex(currentVertex, joinNormal, 0, 0, segment, lineProgressFeatures);
const nextSegmentLength = currentVertex.dist(nextVertex);
if (nextSegmentLength > 2 * sharpCornerOffset) {
const newCurrentVertex = currentVertex.add(nextVertex.sub(currentVertex)._mult(sharpCornerOffset / nextSegmentLength)._round());
this.updateDistance(currentVertex, newCurrentVertex);
this.addCurrentVertex(newCurrentVertex, nextNormal, 0, 0, segment, lineProgressFeatures);
currentVertex = newCurrentVertex;
}
} else {
joinNormal._mult(miterLength);
this.addCurrentVertex(currentVertex, joinNormal, 0, 0, segment, lineProgressFeatures);
}
} else if (currentJoin === "flipbevel") {
if (miterLength > 100) {
joinNormal = nextNormal.mult(-1);
} else {
const bevelLength = miterLength * prevNormal.add(nextNormal).mag() / prevNormal.sub(nextNormal).mag();
joinNormal._perp()._mult(bevelLength * (lineTurnsLeft ? -1 : 1));
}
this.addCurrentVertex(currentVertex, joinNormal, 0, 0, segment, lineProgressFeatures);
this.addCurrentVertex(currentVertex, joinNormal.mult(-1), 0, 0, segment, lineProgressFeatures);
} else if (currentJoin === "bevel" || currentJoin === "fakeround") {
if (lineProgressFeatures != null && prevVertex) {
this.addCurrentVertex(currentVertex, endNormal ? endNormal : prevNormal, -1, -1, segment, lineProgressFeatures);
}
const dist = currentVertex.dist(prevVertex);
const skipStraightEdges = dist <= 2 * sharpCornerOffset && currentJoin !== "bevel";
const join2 = joinNormal.mult(lineTurnsLeft ? 1 : -1);
join2._mult(miterLength);
const next = nextNormal.mult(lineTurnsLeft ? -1 : 1);
const prev = prevNormal.mult(lineTurnsLeft ? -1 : 1);
const lpf = this.evaluateLineProgressFeatures(this.distance);
if (lineProgressFeatures == null) {
this.addHalfVertex(currentVertex, join2.x, join2.y, false, !lineTurnsLeft, 0, segment, lpf);
if (!skipStraightEdges) {
this.addHalfVertex(currentVertex, join2.x + prev.x * 2, join2.y + prev.y * 2, false, lineTurnsLeft, 0, segment, lpf);
}
}
if (currentJoin === "fakeround") {
const n = Math.round(approxAngle * 180 / Math.PI / DEG_PER_TRIANGLE);
this.addHalfVertex(currentVertex, prev.x, prev.y, false, lineTurnsLeft, 0, segment, lpf);
for (let m = 0; m < n; m++) {
let t = m / n;
if (t !== 0.5) {
const t2 = t - 0.5;
const A = 1.0904 + cosAngle * (-3.2452 + cosAngle * (3.55645 - cosAngle * 1.43519));
const B = 0.848013 + cosAngle * (-1.06021 + cosAngle * 0.215638);
t = t + t * t2 * (t - 1) * (A * t2 * t2 + B);
}
const extrude = next.sub(prev)._mult(t)._add(prev)._unit();
this.addHalfVertex(currentVertex, extrude.x, extrude.y, false, lineTurnsLeft, 0, segment, lpf);
}
this.addHalfVertex(currentVertex, next.x, next.y, false, lineTurnsLeft, 0, segment, lpf);
}
if (!skipStraightEdges && lineProgressFeatures == null) {
this.addHalfVertex(currentVertex, join2.x + next.x * 2, join2.y + next.y * 2, false, lineTurnsLeft, 0, segment, lpf);
}
if (lineProgressFeatures != null && nextVertex) {
this.addCurrentVertex(currentVertex, startNormal ? startNormal : nextNormal, 1, 1, segment, lineProgressFeatures);
}
} else if (currentJoin === "butt") {
this.addCurrentVertex(currentVertex, joinNormal, 0, 0, segment, lineProgressFeatures);
} else if (currentJoin === "square") {
if (!prevVertex) {
const capExt = intermediateStartPoint ? 0 : -1;
this.addCurrentVertex(currentVertex, joinNormal, capExt, capExt, segment, lineProgressFeatures);
}
this.addCurrentVertex(currentVertex, joinNormal, 0, 0, segment, lineProgressFeatures);
if (prevVertex) {
const capExt = intermediateEndPoint ? 0 : 1;
this.addCurrentVertex(currentVertex, joinNormal, capExt, capExt, segment, lineProgressFeatures);
}
} else if (currentJoin === "round") {
if (prevVertex) {
const vertexNormal = !middleVertex && endNormal ? endNormal : prevNormal;
this.addCurrentVertex(currentVertex, vertexNormal, 0, 0, segment, lineProgressFeatures);
if (middleVertex || !intermediateEndPoint) {
this.addCurrentVertex(currentVertex, vertexNormal, 1, 1, segment, lineProgressFeatures, true);
}
}
if (nextVertex) {
const vertexNormal = !middleVertex && startNormal ? startNormal : nextNormal;
if (middleVertex || !intermediateStartPoint) {
this.addCurrentVertex(currentVertex, vertexNormal, -1, -1, segment, lineProgressFeatures, true);
}
this.addCurrentVertex(currentVertex, vertexNormal, 0, 0, segment, lineProgressFeatures);
}
}
}
}
addVerticesTo(from, to, leftX, leftY, rightX, rightY, endLeft, endRight, segment, round) {
const STEP = this.tessellationStep;
const steps = (to.w - from.w) / STEP | 0;
let stepsDistance = 0;
const scaledDistance = this.scaledDistance;
if (steps > 1) {
this.lineSoFar = from.w;
const stepX = (to.x - from.x) / steps;
const stepY = (to.y - from.y) / steps;
const stepZ = (to.z - from.z) / steps;
const stepW = (to.w - from.w) / steps;
for (let i = 1; i < steps; ++i) {
from.x += stepX;
from.y += stepY;
from.z += stepZ;
this.lineSoFar += stepW;
stepsDistance += stepW;
const lpf2 = this.evaluateLineProgressFeatures(this.prevDistance + stepsDistance);
this.scaledDistance = (this.prevDistance + stepsDistance) / this.totalDistance;
this.addHalfVertex(from, leftX, leftY, round, false, endLeft, segment, lpf2);
this.addHalfVertex(from, rightX, rightY, round, true, -endRight, segment, lpf2);
}
}
this.lineSoFar = to.w;
this.scaledDistance = scaledDistance;
const lpf = this.evaluateLineProgressFeatures(this.distance);
this.addHalfVertex(to, leftX, leftY, round, false, endLeft, segment, lpf);
this.addHalfVertex(to, rightX, rightY, round, true, -endRight, segment, lpf);
}
evaluateLineProgressFeatures(distance) {
assert$1(distance >= 0);
if (!this.variableWidthValue && this.elevationType !== "offset") {
return null;
}
this.evaluationGlobals.lineProgress = 0;
if (this.lineClips) {
this.evaluationGlobals.lineProgress = Math.min(1, (this.totalFeatureLength * this.lineClips.start + distance) / this.totalFeatureLength);
} else {
warnOnce(`line-progress evaluation for ${this.layerIds[0]} requires enabling 'lineMetrics' for the source.`);
}
let variableWidth = 0;
if (this.variableWidthValue && this.variableWidthValue.kind !== "constant") {
variableWidth = this.variableWidthValue.evaluate(this.evaluationGlobals, this.lineFeature) || 0;
}
if (this.elevationType !== "offset") {
return { zOffset: 0, variableWidth };
}
if (this.zOffsetValue.kind === "constant") {
return { zOffset: this.zOffsetValue.value, variableWidth };
}
const zOffset = this.zOffsetValue.evaluate(this.evaluationGlobals, this.lineFeature) || 0;
return { zOffset, variableWidth };
}
/**
* Add two vertices to the buffers.
*
* @param p the line vertex to add buffer vertices for
* @param normal vertex normal
* @param endLeft extrude to shift the left vertex along the line
* @param endRight extrude to shift the left vertex along the line
* @param segment the segment object to add the vertex to
* @param round whether this is a round cap
* @private
*/
addCurrentVertex(p, normal, endLeft, endRight, segment, lineProgressFeatures, round = false) {
const leftX = normal.x + normal.y * endLeft;
const leftY = normal.y - normal.x * endLeft;
const rightX = -normal.x + normal.y * endRight;
const rightY = -normal.y - normal.x * endRight;
if (lineProgressFeatures != null) {
const dropOutOfBounds = this.elevationType === "offset";
const boundsMin = -10;
const boundsMax = EXTENT + 10;
const zOffset = lineProgressFeatures.zOffset;
const vertex = new Point4D(p.x, p.y, zOffset, this.lineSoFar);
const outside = dropOutOfBounds ? pointOutsideBounds(p, boundsMin, boundsMax) : false;
const lineSoFar = this.lineSoFar;
const distance = this.distance;
if (!this.currentVertex) {
if (!outside) {
this.addHalfVertex(p, leftX, leftY, round, false, endLeft, segment, lineProgressFeatures);
this.addHalfVertex(p, rightX, rightY, round, true, -endRight, segment, lineProgressFeatures);
}
} else if (outside) {
const prevOutside = this.currentVertexIsOutside;
const prev = this.currentVertex;
const next = new Point4D(p.x, p.y, zOffset, this.lineSoFar);
clipLine(prev, next, boundsMin, boundsMax);
if (!pointOutsideBounds(next, boundsMin, boundsMax)) {
if (prevOutside) {
this.e1 = this.e2 = -1;
this.distance -= prev.dist(vertex);
this.lineSoFar = prev.w;
const lpf = this.evaluateLineProgressFeatures(prev.w - this.totalFeatureLength * (this.lineClips ? this.lineClips.start : 0));
this.addHalfVertex(prev, leftX, leftY, round, false, endLeft, segment, lpf);
this.addHalfVertex(prev, rightX, rightY, round, true, -endRight, segment, lpf);
this.prevDistance = this.distance;
}
this.distance = this.prevDistance + prev.dist(next);
this.scaledDistance = this.distance / this.totalDistance;
this.addVerticesTo(prev, next, leftX, leftY, rightX, rightY, endLeft, endRight, segment, round);
this.distance = distance;
this.scaledDistance = this.distance / this.totalDistance;
}
} else {
const prevOutside = this.currentVertexIsOutside;
const prev = this.currentVertex;
if (prevOutside) {
clipLine(prev, vertex, boundsMin, boundsMax);
assert$1(vertex.x === p.x && vertex.y === p.y);
this.e1 = this.e2 = -1;
this.distance -= prev.dist(vertex);
this.scaledDistance = this.distance / this.totalDistance;
this.lineSoFar = prev.w;
const lpf = this.evaluateLineProgressFeatures(prev.w - this.totalFeatureLength * (this.lineClips ? this.lineClips.start : 0));
this.addHalfVertex(prev, leftX, leftY, round, false, endLeft, segment, lpf);
this.addHalfVertex(prev, rightX, rightY, round, true, -endRight, segment, lpf);
this.prevDistance = this.distance;
this.distance = distance;
this.scaledDistance = this.distance / this.totalDistance;
}
this.addVerticesTo(prev, vertex, leftX, leftY, rightX, rightY, endLeft, endRight, segment, round);
}
this.currentVertex = vertex;
this.currentVertexIsOutside = outside;
this.lineSoFar = lineSoFar;
} else {
this.addHalfVertex(p, leftX, leftY, round, false, endLeft, segment, lineProgressFeatures);
this.addHalfVertex(p, rightX, rightY, round, true, -endRight, segment, lineProgressFeatures);
}
}
addHalfVertex({
x,
y
}, extrudeX, extrudeY, round, up, dir, segment, lineProgressFeatures) {
if (this.patternJoinNone) {
if (this.segmentPoints.length === 0) {
this.segmentStart = this.lineSoFar;
this.segmentStartf32 = Math.fround(this.lineSoFar);
}
if (!up) {
this.segmentPoints.push(this.lineSoFar - this.segmentStart, dir);
}
}
this.layoutVertexArray.emplaceBack(
// a_pos_normal
// Encode round/up the least significant bits
(x << 1) + (round ? 1 : 0),
(y << 1) + (up ? 1 : 0),
// a_data
// add 128 to store a byte in an unsigned byte
Math.round(EXTRUDE_SCALE * extrudeX) + 128,
Math.round(EXTRUDE_SCALE * extrudeY) + 128,
(dir === 0 ? 0 : dir < 0 ? -1 : 1) + 1,
0,
// unused
// a_linesofar
this.lineSoFar - this.segmentStartf32
);
if (this.lineClips) {
const lineProgress = number(this.lineClips.start, this.lineClips.end, this.scaledDistance);
this.layoutVertexArray2.emplaceBack(this.scaledDistance, this.lineClipsArray.length, lineProgress);
}
const e = segment.vertexLength++;
if (this.e1 >= 0 && this.e2 >= 0) {
this.indexArray.emplaceBack(this.e1, this.e2, e);
segment.primitiveLength++;
}
if (up) {
this.e2 = e;
} else {
this.e1 = e;
}
if (lineProgressFeatures != null) {
this.zOffsetVertexArray.emplaceBack(
lineProgressFeatures.zOffset,
lineProgressFeatures.variableWidth,
lineProgressFeatures.variableWidth
);
}
assert$1(this.zOffsetVertexArray.length === this.layoutVertexArray.length || this.elevationType !== "offset");
}
updateScaledDistance() {
if (this.lineClips) {
this.scaledDistance = this.distance / this.totalDistance;
this.lineSoFar = this.totalFeatureLength * this.lineClips.start + this.distance;
} else {
this.lineSoFar = this.distance;
}
}
updateDistance(prev, next) {
this.prevDistance = this.distance;
this.distance += prev.dist(next);
this.updateScaledDistance();
}
}
function pointOutsideBounds(p, min, max) {
return p.x < min || p.x > max || p.y < min || p.y > max;
}
register(LineBucket, "LineBucket", { omit: ["layers", "patternFeatures", "currentVertex", "currentVertexIsOutside"] });
let layout$6;
const getLayoutProperties$7 = () => layout$6 || (layout$6 = new Properties({
"line-cap": new DataDrivenProperty(spec["layout_line"]["line-cap"]),
"line-join": new DataDrivenProperty(spec["layout_line"]["line-join"]),
"line-miter-limit": new DataConstantProperty(spec["layout_line"]["line-miter-limit"]),
"line-round-limit": new DataConstantProperty(spec["layout_line"]["line-round-limit"]),
"line-sort-key": new DataDrivenProperty(spec["layout_line"]["line-sort-key"]),
"line-z-offset": new DataDrivenProperty(spec["layout_line"]["line-z-offset"]),
"line-elevation-reference": new DataConstantProperty(spec["layout_line"]["line-elevation-reference"]),
"line-cross-slope": new DataConstantProperty(spec["layout_line"]["line-cross-slope"]),
"visibility": new DataConstantProperty(spec["layout_line"]["visibility"]),
"line-width-unit": new DataConstantProperty(spec["layout_line"]["line-width-unit"])
}));
let paint$7;
const getPaintProperties$7 = () => paint$7 || (paint$7 = new Properties({
"line-opacity": new DataDrivenProperty(spec["paint_line"]["line-opacity"]),
"line-color": new DataDrivenProperty(spec["paint_line"]["line-color"]),
"line-translate": new DataConstantProperty(spec["paint_line"]["line-translate"]),
"line-translate-anchor": new DataConstantProperty(spec["paint_line"]["line-translate-anchor"]),
"line-width": new DataDrivenProperty(spec["paint_line"]["line-width"]),
"line-gap-width": new DataDrivenProperty(spec["paint_line"]["line-gap-width"]),
"line-offset": new DataDrivenProperty(spec["paint_line"]["line-offset"]),
"line-blur": new DataDrivenProperty(spec["paint_line"]["line-blur"]),
"line-dasharray": new DataDrivenProperty(spec["paint_line"]["line-dasharray"]),
"line-pattern": new DataDrivenProperty(spec["paint_line"]["line-pattern"]),
"line-pattern-cross-fade": new DataConstantProperty(spec["paint_line"]["line-pattern-cross-fade"]),
"line-gradient": new ColorRampProperty(spec["paint_line"]["line-gradient"]),
"line-trim-offset": new DataConstantProperty(spec["paint_line"]["line-trim-offset"]),
"line-trim-fade-range": new DataConstantProperty(spec["paint_line"]["line-trim-fade-range"]),
"line-trim-color": new DataConstantProperty(spec["paint_line"]["line-trim-color"]),
"line-emissive-strength": new DataDrivenProperty(spec["paint_line"]["line-emissive-strength"]),
"line-border-width": new DataDrivenProperty(spec["paint_line"]["line-border-width"]),
"line-border-color": new DataDrivenProperty(spec["paint_line"]["line-border-color"]),
"line-occlusion-opacity": new DataConstantProperty(spec["paint_line"]["line-occlusion-opacity"]),
"line-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"line-gradient-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"line-trim-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"line-border-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
function pixelsToTileUnits(tile, pixelValue, z) {
return pixelValue * (EXTENT / (tile.tileSize * Math.pow(2, z - tile.tileID.overscaledZ)));
}
function getPixelsToTileUnitsMatrix(tile, transform) {
const { scale } = tile.tileTransform;
const s = scale * EXTENT / (tile.tileSize * Math.pow(2, transform.zoom - tile.tileID.overscaledZ + tile.tileID.canonical.z));
return scale$8(new Float32Array(4), transform.inverseAdjustmentMatrix, [s, s]);
}
const lineUniforms = (context) => ({
"u_matrix": new UniformMatrix4f(context),
"u_pixels_to_tile_units": new UniformMatrix2f(context),
"u_device_pixel_ratio": new Uniform1f(context),
"u_width_scale": new Uniform1f(context),
"u_floor_width_scale": new Uniform1f(context),
"u_units_to_pixels": new Uniform2f(context),
"u_dash_image": new Uniform1i(context),
"u_gradient_image": new Uniform1i(context),
"u_image_height": new Uniform1f(context),
"u_texsize": new Uniform2f(context),
"u_tile_units_to_pixels": new Uniform1f(context),
"u_alpha_discard_threshold": new Uniform1f(context),
"u_trim_offset": new Uniform2f(context),
"u_trim_fade_range": new Uniform2f(context),
"u_trim_color": new Uniform4f(context),
"u_zbias_factor": new Uniform1f(context),
"u_tile_to_meter": new Uniform1f(context),
"u_ground_shadow_factor": new Uniform3f(context)
});
const linePatternUniforms = (context) => ({
"u_matrix": new UniformMatrix4f(context),
"u_texsize": new Uniform2f(context),
"u_pixels_to_tile_units": new UniformMatrix2f(context),
"u_device_pixel_ratio": new Uniform1f(context),
"u_width_scale": new Uniform1f(context),
"u_floor_width_scale": new Uniform1f(context),
"u_image": new Uniform1i(context),
"u_units_to_pixels": new Uniform2f(context),
"u_tile_units_to_pixels": new Uniform1f(context),
"u_alpha_discard_threshold": new Uniform1f(context),
"u_trim_offset": new Uniform2f(context),
"u_trim_fade_range": new Uniform2f(context),
"u_trim_color": new Uniform4f(context),
"u_zbias_factor": new Uniform1f(context),
"u_tile_to_meter": new Uniform1f(context),
"u_ground_shadow_factor": new Uniform3f(context),
"u_pattern_transition": new Uniform1f(context)
});
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const lineUniformValues = (painter, tile, layer, matrix, imageHeight, pixelRatio, widthScale, floorWidthScale, trimOffset, groundShadowFactor) => {
const transform = painter.transform;
const pixelsToTileUnits2 = transform.calculatePixelsToTileUnitsMatrix(tile);
const ignoreLut = layer.paint.get("line-trim-color-use-theme").constantOr("default") === "none";
const zbiasFactor = transform.pitch < 15 ? lerp(0.07, 0.7, clamp((14 - transform.zoom) / (14 - 9), 0, 1)) : 0.07;
return {
"u_matrix": calculateMatrix(painter, tile, layer, matrix),
"u_pixels_to_tile_units": pixelsToTileUnits2,
"u_device_pixel_ratio": pixelRatio,
"u_width_scale": widthScale,
"u_floor_width_scale": floorWidthScale,
"u_units_to_pixels": [
1 / transform.pixelsToGLUnits[0],
1 / transform.pixelsToGLUnits[1]
],
"u_dash_image": 0,
"u_gradient_image": 1,
"u_image_height": imageHeight,
"u_texsize": hasDash(layer) && tile.lineAtlasTexture ? tile.lineAtlasTexture.size : [0, 0],
"u_tile_units_to_pixels": calculateTileRatio(tile, painter.transform),
"u_alpha_discard_threshold": 0,
"u_trim_offset": trimOffset,
"u_trim_fade_range": layer.paint.get("line-trim-fade-range"),
"u_trim_color": layer.paint.get("line-trim-color").toPremultipliedRenderColor(ignoreLut ? null : layer.lut).toArray01(),
"u_zbias_factor": zbiasFactor,
"u_tile_to_meter": tileToMeter(tile.tileID.canonical, 0),
"u_ground_shadow_factor": groundShadowFactor
};
};
const linePatternUniformValues = (painter, tile, layer, matrix, pixelRatio, widthScale, floorWidthScale, trimOffset, groundShadowFactor, transition) => {
const transform = painter.transform;
const zbiasFactor = transform.pitch < 15 ? lerp(0.07, 0.7, clamp((14 - transform.zoom) / (14 - 9), 0, 1)) : 0.07;
const ignoreLut = layer.paint.get("line-trim-color-use-theme").constantOr("default") === "none";
return {
"u_matrix": calculateMatrix(painter, tile, layer, matrix),
"u_texsize": tile.imageAtlasTexture ? tile.imageAtlasTexture.size : [0, 0],
// camera zoom ratio
"u_pixels_to_tile_units": transform.calculatePixelsToTileUnitsMatrix(tile),
"u_device_pixel_ratio": pixelRatio,
"u_width_scale": widthScale,
"u_floor_width_scale": floorWidthScale,
"u_image": 0,
"u_tile_units_to_pixels": calculateTileRatio(tile, transform),
"u_units_to_pixels": [
1 / transform.pixelsToGLUnits[0],
1 / transform.pixelsToGLUnits[1]
],
"u_alpha_discard_threshold": 0,
"u_trim_offset": trimOffset,
"u_trim_fade_range": layer.paint.get("line-trim-fade-range"),
"u_trim_color": layer.paint.get("line-trim-color").toPremultipliedRenderColor(ignoreLut ? null : layer.lut).toArray01(),
"u_zbias_factor": zbiasFactor,
"u_tile_to_meter": tileToMeter(tile.tileID.canonical, 0),
"u_ground_shadow_factor": groundShadowFactor,
"u_pattern_transition": transition
};
};
function calculateTileRatio(tile, transform) {
return 1 / pixelsToTileUnits(tile, 1, transform.tileZoom);
}
function calculateMatrix(painter, tile, layer, matrix) {
return painter.translatePosMatrix(
matrix ? matrix : tile.tileID.projMatrix,
tile,
layer.paint.get("line-translate"),
layer.paint.get("line-translate-anchor")
);
}
const lineDefinesValues = (layer) => {
const values = [];
if (hasDash(layer)) values.push("RENDER_LINE_DASH");
if (layer.paint.get("line-gradient")) values.push("RENDER_LINE_GRADIENT");
const trimOffset = layer.paint.get("line-trim-offset");
if (trimOffset[0] !== 0 || trimOffset[1] !== 0) {
values.push("RENDER_LINE_TRIM_OFFSET");
}
const hasBorder = layer.paint.get("line-border-width").constantOr(1) !== 0;
if (hasBorder) values.push("RENDER_LINE_BORDER");
const hasJoinNone = layer.layout.get("line-join").constantOr("miter") === "none";
const hasPattern = !!layer.paint.get("line-pattern").constantOr(1);
if (hasJoinNone && hasPattern) {
values.push("LINE_JOIN_NONE");
}
return values;
};
function hasDash(layer) {
const dashPropertyValue = layer.paint.get("line-dasharray").value;
return dashPropertyValue.kind !== "constant" || dashPropertyValue.value;
}
let properties$1;
const getProperties$1 = () => {
if (properties$1) {
return properties$1;
}
properties$1 = {
layout: getLayoutProperties$7(),
paint: getPaintProperties$7()
};
return properties$1;
};
class LineFloorwidthProperty extends DataDrivenProperty {
possiblyEvaluate(value, parameters) {
parameters = new EvaluationParameters(Math.floor(parameters.zoom), {
now: parameters.now,
fadeDuration: parameters.fadeDuration,
transition: parameters.transition,
worldview: parameters.worldview
});
return super.possiblyEvaluate(value, parameters);
}
evaluate(value, globals, feature, featureState) {
globals = Object.assign({}, globals, { zoom: Math.floor(globals.zoom) });
return super.evaluate(value, globals, feature, featureState);
}
}
let lineFloorwidthProperty;
const getLineFloorwidthProperty = () => {
if (lineFloorwidthProperty) {
return lineFloorwidthProperty;
}
const properties2 = getProperties$1();
lineFloorwidthProperty = new LineFloorwidthProperty(properties2.paint.properties["line-width"].specification);
lineFloorwidthProperty.useIntegerZoom = true;
return lineFloorwidthProperty;
};
class LineStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties2 = getProperties$1();
super(layer, properties2, scope, lut, options);
if (properties2.layout) {
this.layout = new PossiblyEvaluated(properties2.layout);
}
this.gradientVersion = 0;
this.hasElevatedBuckets = false;
this.hasNonElevatedBuckets = false;
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "line-gradient") {
const expression = this._transitionablePaint._values["line-gradient"].value.expression;
this.stepInterpolant = expression._styleExpression && expression._styleExpression.expression instanceof Step;
this.gradientVersion = (this.gradientVersion + 1) % Number.MAX_SAFE_INTEGER;
}
}
gradientExpression() {
return this._transitionablePaint._values["line-gradient"].value.expression;
}
widthExpression() {
return this._transitionablePaint._values["line-width"].value.expression;
}
emissiveStrengthExpression() {
return this._transitionablePaint._values["line-emissive-strength"].value.expression;
}
recalculate(parameters, availableImages) {
super.recalculate(parameters, availableImages);
this.paint._values["line-floorwidth"] = getLineFloorwidthProperty().possiblyEvaluate(this._transitioningPaint._values["line-width"].value, parameters);
}
createBucket(parameters) {
return new LineBucket(parameters);
}
getProgramIds() {
const patternProperty = this.paint.get("line-pattern");
const image = patternProperty.constantOr(1);
const programId = image ? "linePattern" : "line";
return [programId];
}
getDefaultProgramParams(name, zoom, lut) {
const definesValues = lineDefinesValues(this);
return {
config: new ProgramConfiguration(this, { zoom, lut }),
defines: definesValues,
overrideFog: false
};
}
queryRadius(bucket) {
const lineBucket = bucket;
const width = getLineWidth(
getMaximumPaintValue("line-width", this, lineBucket),
getMaximumPaintValue("line-gap-width", this, lineBucket)
);
const offset = getMaximumPaintValue("line-offset", this, lineBucket);
return width / 2 + Math.abs(offset) + translateDistance(this.paint.get("line-translate"));
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform) {
if (queryGeometry.queryGeometry.isAboveHorizon) return false;
const translatedPolygon = translate(
queryGeometry.tilespaceGeometry,
this.paint.get("line-translate"),
this.paint.get("line-translate-anchor"),
transform.angle,
queryGeometry.pixelToTileUnitsFactor
);
const halfWidth = queryGeometry.pixelToTileUnitsFactor / 2 * getLineWidth(
this.paint.get("line-width").evaluate(feature, featureState),
this.paint.get("line-gap-width").evaluate(feature, featureState)
);
const lineOffset = this.paint.get("line-offset").evaluate(feature, featureState);
if (lineOffset) {
geometry = offsetLine(geometry, lineOffset * queryGeometry.pixelToTileUnitsFactor);
}
return polygonIntersectsBufferedMultiLine(translatedPolygon, geometry, halfWidth);
}
isTileClipped() {
return this.hasNonElevatedBuckets;
}
isDraped(_) {
return !this.hasElevatedBuckets || this.layout && this.layout.get("line-elevation-reference") === "hd-road-markup";
}
hasElevation() {
return this.layout && this.layout.get("line-elevation-reference") !== "none";
}
}
function getLineWidth(lineWidth, lineGapWidth) {
if (lineGapWidth > 0) {
return lineGapWidth + 2 * lineWidth;
} else {
return lineWidth;
}
}
function offsetLine(rings, offset) {
const newRings = [];
const zero = new Point(0, 0);
for (let k = 0; k < rings.length; k++) {
const ring = rings[k];
const newRing = [];
for (let i = 0; i < ring.length; i++) {
const a = ring[i - 1];
const b = ring[i];
const c = ring[i + 1];
const aToB = i === 0 ? zero : b.sub(a)._unit()._perp();
const bToC = i === ring.length - 1 ? zero : c.sub(b)._unit()._perp();
const extrude = aToB._add(bToC)._unit();
const cosHalfAngle = extrude.x * bToC.x + extrude.y * bToC.y;
extrude._mult(1 / cosHalfAngle);
newRing.push(extrude._mult(offset)._add(b));
}
newRings.push(newRing);
}
return newRings;
}
const symbolLayoutAttributes = createLayout([
{ name: "a_pos_offset", components: 4, type: "Int16" },
{ name: "a_tex_size", components: 4, type: "Uint16" },
{ name: "a_pixeloffset", components: 4, type: "Int16" }
], 4);
const symbolGlobeExtAttributes = createLayout([
{ name: "a_globe_anchor", components: 3, type: "Int16" },
{ name: "a_globe_normal", components: 3, type: "Float32" }
], 4);
const dynamicLayoutAttributes = createLayout([
{ name: "a_projected_pos", components: 4, type: "Float32" }
], 4);
const placementOpacityAttributes = createLayout([
{ name: "a_fade_opacity", components: 1, type: "Uint32" }
], 4);
const zOffsetAttributes = createLayout([
{ name: "a_auto_z_offset", components: 1, type: "Float32" }
], 4);
const orientationAttributes = createLayout([
{ name: "a_x_axis", components: 3, type: "Float32" },
{ name: "a_y_axis", components: 3, type: "Float32" }
]);
const iconTransitioningAttributes = createLayout([
{ name: "a_texb", components: 2, type: "Uint16" }
]);
const collisionVertexAttributes = createLayout([
{ name: "a_placed", components: 2, type: "Uint8" },
{ name: "a_shift", components: 2, type: "Float32" },
{ name: "a_elevation_from_sea", components: 2, type: "Float32" }
]);
const collisionVertexAttributesExt = createLayout([
{ name: "a_size_scale", components: 1, type: "Float32" },
{ name: "a_padding", components: 2, type: "Float32" },
{ name: "a_auto_z_offset", components: 1, type: "Float32" }
]);
const collisionBox = createLayout([
// the box is centered around the anchor point
{ type: "Int16", name: "projectedAnchorX" },
{ type: "Int16", name: "projectedAnchorY" },
{ type: "Int16", name: "projectedAnchorZ" },
{ type: "Int16", name: "tileAnchorX" },
{ type: "Int16", name: "tileAnchorY" },
// distances to the edges from the anchor
{ type: "Float32", name: "x1" },
{ type: "Float32", name: "y1" },
{ type: "Float32", name: "x2" },
{ type: "Float32", name: "y2" },
{ type: "Int16", name: "padding" },
// the index of the feature in the original vectortile
{ type: "Uint32", name: "featureIndex" },
// the source layer the feature appears in
{ type: "Uint16", name: "sourceLayerIndex" },
// the bucket the feature appears in
{ type: "Uint16", name: "bucketIndex" }
]);
const collisionBoxLayout = createLayout([
// used to render collision boxes for debugging purposes
{ name: "a_pos", components: 3, type: "Int16" },
{ name: "a_anchor_pos", components: 2, type: "Int16" },
{ name: "a_extrude", components: 2, type: "Int16" }
], 4);
const collisionCircleLayout = createLayout([
// used to render collision circles for debugging purposes
{ name: "a_pos_2f", components: 2, type: "Float32" },
{ name: "a_radius", components: 1, type: "Float32" },
{ name: "a_flags", components: 2, type: "Int16" }
], 4);
const quadTriangle = createLayout([
{ name: "triangle", components: 3, type: "Uint16" }
]);
const placement = createLayout([
{ type: "Int16", name: "projectedAnchorX" },
{ type: "Int16", name: "projectedAnchorY" },
{ type: "Int16", name: "projectedAnchorZ" },
{ type: "Float32", name: "tileAnchorX" },
{ type: "Float32", name: "tileAnchorY" },
{ type: "Uint16", name: "glyphStartIndex" },
{ type: "Uint16", name: "numGlyphs" },
{ type: "Uint32", name: "vertexStartIndex" },
{ type: "Uint32", name: "lineStartIndex" },
{ type: "Uint32", name: "lineLength" },
{ type: "Uint16", name: "segment" },
{ type: "Uint16", name: "lowerSize" },
{ type: "Uint16", name: "upperSize" },
{ type: "Float32", name: "lineOffsetX" },
{ type: "Float32", name: "lineOffsetY" },
{ type: "Uint8", name: "writingMode" },
{ type: "Uint8", name: "placedOrientation" },
{ type: "Uint8", name: "hidden" },
{ type: "Uint32", name: "crossTileID" },
{ type: "Int16", name: "associatedIconIndex" },
{ type: "Uint8", name: "flipState" }
]);
const symbolInstance = createLayout([
{ type: "Float32", name: "tileAnchorX" },
{ type: "Float32", name: "tileAnchorY" },
{ type: "Int16", name: "projectedAnchorX" },
{ type: "Int16", name: "projectedAnchorY" },
{ type: "Int16", name: "projectedAnchorZ" },
{ type: "Int16", name: "rightJustifiedTextSymbolIndex" },
{ type: "Int16", name: "centerJustifiedTextSymbolIndex" },
{ type: "Int16", name: "leftJustifiedTextSymbolIndex" },
{ type: "Int16", name: "verticalPlacedTextSymbolIndex" },
{ type: "Int16", name: "placedIconSymbolIndex" },
{ type: "Int16", name: "verticalPlacedIconSymbolIndex" },
{ type: "Uint16", name: "key" },
{ type: "Uint16", name: "textBoxStartIndex" },
{ type: "Uint16", name: "textBoxEndIndex" },
{ type: "Uint16", name: "verticalTextBoxStartIndex" },
{ type: "Uint16", name: "verticalTextBoxEndIndex" },
{ type: "Uint16", name: "iconBoxStartIndex" },
{ type: "Uint16", name: "iconBoxEndIndex" },
{ type: "Uint16", name: "verticalIconBoxStartIndex" },
{ type: "Uint16", name: "verticalIconBoxEndIndex" },
{ type: "Uint16", name: "featureIndex" },
{ type: "Uint16", name: "numHorizontalGlyphVertices" },
{ type: "Uint16", name: "numVerticalGlyphVertices" },
{ type: "Uint16", name: "numIconVertices" },
{ type: "Uint16", name: "numVerticalIconVertices" },
{ type: "Uint16", name: "useRuntimeCollisionCircles" },
{ type: "Uint32", name: "crossTileID" },
{ type: "Float32", components: 2, name: "textOffset" },
{ type: "Float32", name: "collisionCircleDiameter" },
{ type: "Float32", name: "zOffset" },
{ type: "Uint8", name: "hasIconTextFit" },
{ type: "Uint16", name: "elevationFeatureIndex" }
]);
const glyphOffset = createLayout([
{ type: "Float32", name: "offsetX" }
]);
const lineVertex = createLayout([
{ type: "Int16", name: "x" },
{ type: "Int16", name: "y" }
]);
var ONE_EM = 24;
function transformText(text, layer, feature) {
const transform = layer.layout.get("text-transform").evaluate(feature, {});
if (transform === "uppercase") {
text = text.toLocaleUpperCase();
} else if (transform === "lowercase") {
text = text.toLocaleLowerCase();
}
if (plugin.applyArabicShaping) {
text = plugin.applyArabicShaping(text);
}
return text;
}
function transformText$1(text, layer, feature) {
text.sections.forEach((section) => {
section.text = transformText(section.text, layer, feature);
});
return text;
}
function mergeLines(features) {
const leftIndex = {};
const rightIndex = {};
const mergedFeatures = [];
let mergedIndex = 0;
function add(k) {
mergedFeatures.push(features[k]);
mergedIndex++;
}
function mergeFromRight(leftKey, rightKey, geom) {
const i = rightIndex[leftKey];
delete rightIndex[leftKey];
rightIndex[rightKey] = i;
mergedFeatures[i].geometry[0].pop();
mergedFeatures[i].geometry[0] = mergedFeatures[i].geometry[0].concat(geom[0]);
return i;
}
function mergeFromLeft(leftKey, rightKey, geom) {
const i = leftIndex[rightKey];
delete leftIndex[rightKey];
leftIndex[leftKey] = i;
mergedFeatures[i].geometry[0].shift();
mergedFeatures[i].geometry[0] = geom[0].concat(mergedFeatures[i].geometry[0]);
return i;
}
function getKey(text, geom, onRight) {
const point = onRight ? geom[0][geom[0].length - 1] : geom[0][0];
return `${text}:${point.x}:${point.y}`;
}
for (let k = 0; k < features.length; k++) {
const feature = features[k];
const geom = feature.geometry;
const text = feature.text ? feature.text.toString() : null;
if (!text) {
add(k);
continue;
}
const leftKey = getKey(text, geom), rightKey = getKey(text, geom, true);
if (leftKey in rightIndex && rightKey in leftIndex && rightIndex[leftKey] !== leftIndex[rightKey]) {
const j = mergeFromLeft(leftKey, rightKey, geom);
const i = mergeFromRight(leftKey, rightKey, mergedFeatures[j].geometry);
delete leftIndex[leftKey];
delete rightIndex[rightKey];
rightIndex[getKey(text, mergedFeatures[i].geometry, true)] = i;
mergedFeatures[j].geometry = null;
} else if (leftKey in rightIndex) {
mergeFromRight(leftKey, rightKey, geom);
} else if (rightKey in leftIndex) {
mergeFromLeft(leftKey, rightKey, geom);
} else {
add(k);
leftIndex[leftKey] = mergedIndex - 1;
rightIndex[rightKey] = mergedIndex - 1;
}
}
return mergedFeatures.filter((f) => f.geometry);
}
const verticalizedCharacterMap = {
"!": "\uFE15",
"#": "\uFF03",
"$": "\uFF04",
"%": "\uFF05",
"&": "\uFF06",
"(": "\uFE35",
")": "\uFE36",
"*": "\uFF0A",
"+": "\uFF0B",
",": "\uFE10",
"-": "\uFE32",
".": "\u30FB",
"/": "\uFF0F",
":": "\uFE13",
";": "\uFE14",
"<": "\uFE3F",
"=": "\uFF1D",
">": "\uFE40",
"?": "\uFE16",
"@": "\uFF20",
"[": "\uFE47",
"\\": "\uFF3C",
"]": "\uFE48",
"^": "\uFF3E",
"_": "\uFE33",
"`": "\uFF40",
"{": "\uFE37",
"|": "\u2015",
"}": "\uFE38",
"~": "\uFF5E",
"\xA2": "\uFFE0",
"\xA3": "\uFFE1",
"\xA5": "\uFFE5",
"\xA6": "\uFFE4",
"\xAC": "\uFFE2",
"\xAF": "\uFFE3",
"\u2013": "\uFE32",
"\u2014": "\uFE31",
"\u2018": "\uFE43",
"\u2019": "\uFE44",
"\u201C": "\uFE41",
"\u201D": "\uFE42",
"\u2026": "\uFE19",
"\u2027": "\u30FB",
"\u20A9": "\uFFE6",
"\u3001": "\uFE11",
"\u3002": "\uFE12",
"\u3008": "\uFE3F",
"\u3009": "\uFE40",
"\u300A": "\uFE3D",
"\u300B": "\uFE3E",
"\u300C": "\uFE41",
"\u300D": "\uFE42",
"\u300E": "\uFE43",
"\u300F": "\uFE44",
"\u3010": "\uFE3B",
"\u3011": "\uFE3C",
"\u3014": "\uFE39",
"\u3015": "\uFE3A",
"\u3016": "\uFE17",
"\u3017": "\uFE18",
"\uFF01": "\uFE15",
"\uFF08": "\uFE35",
"\uFF09": "\uFE36",
"\uFF0C": "\uFE10",
"\uFF0D": "\uFE32",
"\uFF0E": "\u30FB",
"\uFF1A": "\uFE13",
"\uFF1B": "\uFE14",
"\uFF1C": "\uFE3F",
"\uFF1E": "\uFE40",
"\uFF1F": "\uFE16",
"\uFF3B": "\uFE47",
"\uFF3D": "\uFE48",
"\uFF3F": "\uFE33",
"\uFF5B": "\uFE37",
"\uFF5C": "\u2015",
"\uFF5D": "\uFE38",
"\uFF5F": "\uFE35",
"\uFF60": "\uFE36",
"\uFF61": "\uFE12",
"\uFF62": "\uFE41",
"\uFF63": "\uFE42",
"\u2190": "\u2191",
"\u2192": "\u2193"
};
function verticalizePunctuation(input, skipContextChecking) {
let output = "";
for (let i = 0; i < input.length; i++) {
const nextCharCode = input.charCodeAt(i + 1) || null;
const prevCharCode = input.charCodeAt(i - 1) || null;
const canReplacePunctuation = skipContextChecking || (!nextCharCode || !charHasRotatedVerticalOrientation(nextCharCode) || verticalizedCharacterMap[input[i + 1]]) && (!prevCharCode || !charHasRotatedVerticalOrientation(prevCharCode) || verticalizedCharacterMap[input[i - 1]]);
if (canReplacePunctuation && verticalizedCharacterMap[input[i]]) {
output += verticalizedCharacterMap[input[i]];
} else {
output += input[i];
}
}
return output;
}
function isVerticalClosePunctuation(chr) {
return chr === "\uFE36" || chr === "\uFE48" || chr === "\uFE38" || chr === "\uFE44" || chr === "\uFE42" || chr === "\uFE3E" || chr === "\uFE3C" || chr === "\uFE3A" || chr === "\uFE18" || chr === "\uFE40" || chr === "\uFE10" || chr === "\uFE13" || chr === "\uFE14" || chr === "\uFF40" || chr === "\uFFE3" || chr === "\uFE11" || chr === "\uFE12";
}
function isVerticalOpenPunctuation(chr) {
return chr === "\uFE35" || chr === "\uFE47" || chr === "\uFE37" || chr === "\uFE43" || chr === "\uFE41" || chr === "\uFE3D" || chr === "\uFE3B" || chr === "\uFE39" || chr === "\uFE17" || chr === "\uFE3F";
}
const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;
// Threshold chosen based on both benchmarking and knowledge about browser string
// data structures (which currently switch structure types at 12 bytes or more)
const TEXT_DECODER_MIN_LENGTH = 12;
const utf8TextDecoder = typeof TextDecoder === 'undefined' ? null : new TextDecoder('utf-8');
const PBF_VARINT = 0; // varint: int32, int64, uint32, uint64, sint32, sint64, bool, enum
const PBF_FIXED64 = 1; // 64-bit: double, fixed64, sfixed64
const PBF_BYTES = 2; // length-delimited: string, bytes, embedded messages, packed repeated fields
const PBF_FIXED32 = 5; // 32-bit: float, fixed32, sfixed32
let Pbf$1 = class Pbf {
/**
* @param {Uint8Array | ArrayBuffer} [buf]
*/
constructor(buf = new Uint8Array(16)) {
this.buf = ArrayBuffer.isView(buf) ? buf : new Uint8Array(buf);
this.dataView = new DataView(this.buf.buffer);
this.pos = 0;
this.type = 0;
this.length = this.buf.length;
}
// === READING =================================================================
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
* @param {number} [end]
*/
readFields(readField, result, end = this.length) {
while (this.pos < end) {
const val = this.readVarint(),
tag = val >> 3,
startPos = this.pos;
this.type = val & 0x7;
readField(tag, result, this);
if (this.pos === startPos) this.skip(val);
}
return result;
}
/**
* @template T
* @param {(tag: number, result: T, pbf: Pbf) => void} readField
* @param {T} result
*/
readMessage(readField, result) {
return this.readFields(readField, result, this.readVarint() + this.pos);
}
readFixed32() {
const val = this.dataView.getUint32(this.pos, true);
this.pos += 4;
return val;
}
readSFixed32() {
const val = this.dataView.getInt32(this.pos, true);
this.pos += 4;
return val;
}
// 64-bit int handling is based on github.com/dpw/node-buffer-more-ints (MIT-licensed)
readFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getUint32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readSFixed64() {
const val = this.dataView.getUint32(this.pos, true) + this.dataView.getInt32(this.pos + 4, true) * SHIFT_LEFT_32;
this.pos += 8;
return val;
}
readFloat() {
const val = this.dataView.getFloat32(this.pos, true);
this.pos += 4;
return val;
}
readDouble() {
const val = this.dataView.getFloat64(this.pos, true);
this.pos += 8;
return val;
}
/**
* @param {boolean} [isSigned]
*/
readVarint(isSigned) {
const buf = this.buf;
let val, b;
b = buf[this.pos++]; val = b & 0x7f; if (b < 0x80) return val;
b = buf[this.pos++]; val |= (b & 0x7f) << 7; if (b < 0x80) return val;
b = buf[this.pos++]; val |= (b & 0x7f) << 14; if (b < 0x80) return val;
b = buf[this.pos++]; val |= (b & 0x7f) << 21; if (b < 0x80) return val;
b = buf[this.pos]; val |= (b & 0x0f) << 28;
return readVarintRemainder(val, isSigned, this);
}
readVarint64() { // for compatibility with v2.0.1
return this.readVarint(true);
}
readSVarint() {
const num = this.readVarint();
return num % 2 === 1 ? (num + 1) / -2 : num / 2; // zigzag encoding
}
readBoolean() {
return Boolean(this.readVarint());
}
readString() {
const end = this.readVarint() + this.pos;
const pos = this.pos;
this.pos = end;
if (end - pos >= TEXT_DECODER_MIN_LENGTH && utf8TextDecoder) {
// longer strings are fast with the built-in browser TextDecoder API
return utf8TextDecoder.decode(this.buf.subarray(pos, end));
}
// short strings are fast with our custom implementation
return readUtf8(this.buf, pos, end);
}
readBytes() {
const end = this.readVarint() + this.pos,
buffer = this.buf.subarray(this.pos, end);
this.pos = end;
return buffer;
}
// verbose for performance reasons; doesn't affect gzipped size
/**
* @param {number[]} [arr]
* @param {boolean} [isSigned]
*/
readPackedVarint(arr = [], isSigned) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readVarint(isSigned));
return arr;
}
/** @param {number[]} [arr] */
readPackedSVarint(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSVarint());
return arr;
}
/** @param {boolean[]} [arr] */
readPackedBoolean(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readBoolean());
return arr;
}
/** @param {number[]} [arr] */
readPackedFloat(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFloat());
return arr;
}
/** @param {number[]} [arr] */
readPackedDouble(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readDouble());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed32(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed32());
return arr;
}
/** @param {number[]} [arr] */
readPackedFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readFixed64());
return arr;
}
/** @param {number[]} [arr] */
readPackedSFixed64(arr = []) {
const end = this.readPackedEnd();
while (this.pos < end) arr.push(this.readSFixed64());
return arr;
}
readPackedEnd() {
return this.type === PBF_BYTES ? this.readVarint() + this.pos : this.pos + 1;
}
/** @param {number} val */
skip(val) {
const type = val & 0x7;
if (type === PBF_VARINT) while (this.buf[this.pos++] > 0x7f) {}
else if (type === PBF_BYTES) this.pos = this.readVarint() + this.pos;
else if (type === PBF_FIXED32) this.pos += 4;
else if (type === PBF_FIXED64) this.pos += 8;
else throw new Error(`Unimplemented type: ${type}`);
}
// === WRITING =================================================================
/**
* @param {number} tag
* @param {number} type
*/
writeTag(tag, type) {
this.writeVarint((tag << 3) | type);
}
/** @param {number} min */
realloc(min) {
let length = this.length || 16;
while (length < this.pos + min) length *= 2;
if (length !== this.length) {
const buf = new Uint8Array(length);
buf.set(this.buf);
this.buf = buf;
this.dataView = new DataView(buf.buffer);
this.length = length;
}
}
finish() {
this.length = this.pos;
this.pos = 0;
return this.buf.subarray(0, this.length);
}
/** @param {number} val */
writeFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeSFixed32(val) {
this.realloc(4);
this.dataView.setInt32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeSFixed64(val) {
this.realloc(8);
this.dataView.setInt32(this.pos, val & -1, true);
this.dataView.setInt32(this.pos + 4, Math.floor(val * SHIFT_RIGHT_32), true);
this.pos += 8;
}
/** @param {number} val */
writeVarint(val) {
val = +val || 0;
if (val > 0xfffffff || val < 0) {
writeBigVarint(val, this);
return;
}
this.realloc(4);
this.buf[this.pos++] = val & 0x7f | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
this.buf[this.pos++] = ((val >>>= 7) & 0x7f) | (val > 0x7f ? 0x80 : 0); if (val <= 0x7f) return;
this.buf[this.pos++] = (val >>> 7) & 0x7f;
}
/** @param {number} val */
writeSVarint(val) {
this.writeVarint(val < 0 ? -val * 2 - 1 : val * 2);
}
/** @param {boolean} val */
writeBoolean(val) {
this.writeVarint(+val);
}
/** @param {string} str */
writeString(str) {
str = String(str);
this.realloc(str.length * 4);
this.pos++; // reserve 1 byte for short string length
const startPos = this.pos;
// write the string directly to the buffer and see how much was written
this.pos = writeUtf8(this.buf, str, this.pos);
const len = this.pos - startPos;
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
// finally, write the message length in the reserved place and restore the position
this.pos = startPos - 1;
this.writeVarint(len);
this.pos += len;
}
/** @param {number} val */
writeFloat(val) {
this.realloc(4);
this.dataView.setFloat32(this.pos, val, true);
this.pos += 4;
}
/** @param {number} val */
writeDouble(val) {
this.realloc(8);
this.dataView.setFloat64(this.pos, val, true);
this.pos += 8;
}
/** @param {Uint8Array} buffer */
writeBytes(buffer) {
const len = buffer.length;
this.writeVarint(len);
this.realloc(len);
for (let i = 0; i < len; i++) this.buf[this.pos++] = buffer[i];
}
/**
* @template T
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeRawMessage(fn, obj) {
this.pos++; // reserve 1 byte for short message length
// write the message directly to the buffer and see how much was written
const startPos = this.pos;
fn(obj, this);
const len = this.pos - startPos;
if (len >= 0x80) makeRoomForExtraLength(startPos, len, this);
// finally, write the message length in the reserved place and restore the position
this.pos = startPos - 1;
this.writeVarint(len);
this.pos += len;
}
/**
* @template T
* @param {number} tag
* @param {(obj: T, pbf: Pbf) => void} fn
* @param {T} obj
*/
writeMessage(tag, fn, obj) {
this.writeTag(tag, PBF_BYTES);
this.writeRawMessage(fn, obj);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedVarint, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSVarint(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSVarint, arr);
}
/**
* @param {number} tag
* @param {boolean[]} arr
*/
writePackedBoolean(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedBoolean, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFloat(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFloat, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedDouble(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedDouble, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed32(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed32, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedFixed64, arr);
}
/**
* @param {number} tag
* @param {number[]} arr
*/
writePackedSFixed64(tag, arr) {
if (arr.length) this.writeMessage(tag, writePackedSFixed64, arr);
}
/**
* @param {number} tag
* @param {Uint8Array} buffer
*/
writeBytesField(tag, buffer) {
this.writeTag(tag, PBF_BYTES);
this.writeBytes(buffer);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed32Field(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeSFixed32(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSFixed64Field(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeSFixed64(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeVarint(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeSVarintField(tag, val) {
this.writeTag(tag, PBF_VARINT);
this.writeSVarint(val);
}
/**
* @param {number} tag
* @param {string} str
*/
writeStringField(tag, str) {
this.writeTag(tag, PBF_BYTES);
this.writeString(str);
}
/**
* @param {number} tag
* @param {number} val
*/
writeFloatField(tag, val) {
this.writeTag(tag, PBF_FIXED32);
this.writeFloat(val);
}
/**
* @param {number} tag
* @param {number} val
*/
writeDoubleField(tag, val) {
this.writeTag(tag, PBF_FIXED64);
this.writeDouble(val);
}
/**
* @param {number} tag
* @param {boolean} val
*/
writeBooleanField(tag, val) {
this.writeVarintField(tag, +val);
}
};;
/**
* @param {number} l
* @param {boolean | undefined} s
* @param {Pbf} p
*/
function readVarintRemainder(l, s, p) {
const buf = p.buf;
let h, b;
b = buf[p.pos++]; h = (b & 0x70) >> 4; if (b < 0x80) return toNum(l, h, s);
b = buf[p.pos++]; h |= (b & 0x7f) << 3; if (b < 0x80) return toNum(l, h, s);
b = buf[p.pos++]; h |= (b & 0x7f) << 10; if (b < 0x80) return toNum(l, h, s);
b = buf[p.pos++]; h |= (b & 0x7f) << 17; if (b < 0x80) return toNum(l, h, s);
b = buf[p.pos++]; h |= (b & 0x7f) << 24; if (b < 0x80) return toNum(l, h, s);
b = buf[p.pos++]; h |= (b & 0x01) << 31; if (b < 0x80) return toNum(l, h, s);
throw new Error('Expected varint not more than 10 bytes');
}
/**
* @param {number} low
* @param {number} high
* @param {boolean} [isSigned]
*/
function toNum(low, high, isSigned) {
return isSigned ? high * 0x100000000 + (low >>> 0) : ((high >>> 0) * 0x100000000) + (low >>> 0);
}
/**
* @param {number} val
* @param {Pbf} pbf
*/
function writeBigVarint(val, pbf) {
let low, high;
if (val >= 0) {
low = (val % 0x100000000) | 0;
high = (val / 0x100000000) | 0;
} else {
low = ~(-val % 0x100000000);
high = ~(-val / 0x100000000);
if (low ^ 0xffffffff) {
low = (low + 1) | 0;
} else {
low = 0;
high = (high + 1) | 0;
}
}
if (val >= 0x10000000000000000 || val < -0x10000000000000000) {
throw new Error('Given varint doesn\'t fit into 10 bytes');
}
pbf.realloc(10);
writeBigVarintLow(low, high, pbf);
writeBigVarintHigh(high, pbf);
}
/**
* @param {number} high
* @param {number} low
* @param {Pbf} pbf
*/
function writeBigVarintLow(low, high, pbf) {
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
pbf.buf[pbf.pos++] = low & 0x7f | 0x80; low >>>= 7;
pbf.buf[pbf.pos] = low & 0x7f;
}
/**
* @param {number} high
* @param {Pbf} pbf
*/
function writeBigVarintHigh(high, pbf) {
const lsb = (high & 0x07) << 4;
pbf.buf[pbf.pos++] |= lsb | ((high >>>= 3) ? 0x80 : 0); if (!high) return;
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
pbf.buf[pbf.pos++] = high & 0x7f | ((high >>>= 7) ? 0x80 : 0); if (!high) return;
pbf.buf[pbf.pos++] = high & 0x7f;
}
/**
* @param {number} startPos
* @param {number} len
* @param {Pbf} pbf
*/
function makeRoomForExtraLength(startPos, len, pbf) {
const extraLen =
len <= 0x3fff ? 1 :
len <= 0x1fffff ? 2 :
len <= 0xfffffff ? 3 : Math.floor(Math.log(len) / (Math.LN2 * 7));
// if 1 byte isn't enough for encoding message length, shift the data to the right
pbf.realloc(extraLen);
for (let i = pbf.pos - 1; i >= startPos; i--) pbf.buf[i + extraLen] = pbf.buf[i];
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSVarint(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSVarint(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFloat(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFloat(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedDouble(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeDouble(arr[i]);
}
/**
* @param {boolean[]} arr
* @param {Pbf} pbf
*/
function writePackedBoolean(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeBoolean(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed32(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed32(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeFixed64(arr[i]);
}
/**
* @param {number[]} arr
* @param {Pbf} pbf
*/
function writePackedSFixed64(arr, pbf) {
for (let i = 0; i < arr.length; i++) pbf.writeSFixed64(arr[i]);
}
// Buffer code below from https://github.com/feross/buffer, MIT-licensed
/**
* @param {Uint8Array} buf
* @param {number} pos
* @param {number} end
*/
function readUtf8(buf, pos, end) {
let str = '';
let i = pos;
while (i < end) {
const b0 = buf[i];
let c = null; // codepoint
let bytesPerSequence =
b0 > 0xEF ? 4 :
b0 > 0xDF ? 3 :
b0 > 0xBF ? 2 : 1;
if (i + bytesPerSequence > end) break;
let b1, b2, b3;
if (bytesPerSequence === 1) {
if (b0 < 0x80) {
c = b0;
}
} else if (bytesPerSequence === 2) {
b1 = buf[i + 1];
if ((b1 & 0xC0) === 0x80) {
c = (b0 & 0x1F) << 0x6 | (b1 & 0x3F);
if (c <= 0x7F) {
c = null;
}
}
} else if (bytesPerSequence === 3) {
b1 = buf[i + 1];
b2 = buf[i + 2];
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80) {
c = (b0 & 0xF) << 0xC | (b1 & 0x3F) << 0x6 | (b2 & 0x3F);
if (c <= 0x7FF || (c >= 0xD800 && c <= 0xDFFF)) {
c = null;
}
}
} else if (bytesPerSequence === 4) {
b1 = buf[i + 1];
b2 = buf[i + 2];
b3 = buf[i + 3];
if ((b1 & 0xC0) === 0x80 && (b2 & 0xC0) === 0x80 && (b3 & 0xC0) === 0x80) {
c = (b0 & 0xF) << 0x12 | (b1 & 0x3F) << 0xC | (b2 & 0x3F) << 0x6 | (b3 & 0x3F);
if (c <= 0xFFFF || c >= 0x110000) {
c = null;
}
}
}
if (c === null) {
c = 0xFFFD;
bytesPerSequence = 1;
} else if (c > 0xFFFF) {
c -= 0x10000;
str += String.fromCharCode(c >>> 10 & 0x3FF | 0xD800);
c = 0xDC00 | c & 0x3FF;
}
str += String.fromCharCode(c);
i += bytesPerSequence;
}
return str;
}
/**
* @param {Uint8Array} buf
* @param {string} str
* @param {number} pos
*/
function writeUtf8(buf, str, pos) {
for (let i = 0, c, lead; i < str.length; i++) {
c = str.charCodeAt(i); // code point
if (c > 0xD7FF && c < 0xE000) {
if (lead) {
if (c < 0xDC00) {
buf[pos++] = 0xEF;
buf[pos++] = 0xBF;
buf[pos++] = 0xBD;
lead = c;
continue;
} else {
c = lead - 0xD800 << 10 | c - 0xDC00 | 0x10000;
lead = null;
}
} else {
if (c > 0xDBFF || (i + 1 === str.length)) {
buf[pos++] = 0xEF;
buf[pos++] = 0xBF;
buf[pos++] = 0xBD;
} else {
lead = c;
}
continue;
}
} else if (lead) {
buf[pos++] = 0xEF;
buf[pos++] = 0xBF;
buf[pos++] = 0xBD;
lead = null;
}
if (c < 0x80) {
buf[pos++] = c;
} else {
if (c < 0x800) {
buf[pos++] = c >> 0x6 | 0xC0;
} else {
if (c < 0x10000) {
buf[pos++] = c >> 0xC | 0xE0;
} else {
buf[pos++] = c >> 0x12 | 0xF0;
buf[pos++] = c >> 0xC & 0x3F | 0x80;
}
buf[pos++] = c >> 0x6 & 0x3F | 0x80;
}
buf[pos++] = c & 0x3F | 0x80;
}
}
return pos;
}
const border$1 = 3;
function readFontstacks(tag, glyphData, pbf) {
glyphData.glyphs = [];
if (tag === 1) {
pbf.readMessage(readFontstack, glyphData);
}
}
function readFontstack(tag, glyphData, pbf) {
if (tag === 3) {
const { id, bitmap, width, height, left, top, advance } = pbf.readMessage(readGlyph, {});
glyphData.glyphs.push({
id,
bitmap: new AlphaImage({
width: width + 2 * border$1,
height: height + 2 * border$1
}, bitmap),
metrics: { width, height, left, top, advance }
});
} else if (tag === 4) {
glyphData.ascender = pbf.readSVarint();
} else if (tag === 5) {
glyphData.descender = pbf.readSVarint();
}
}
function readGlyph(tag, glyph, pbf) {
if (tag === 1) glyph.id = pbf.readVarint();
else if (tag === 2) glyph.bitmap = pbf.readBytes();
else if (tag === 3) glyph.width = pbf.readVarint();
else if (tag === 4) glyph.height = pbf.readVarint();
else if (tag === 5) glyph.left = pbf.readSVarint();
else if (tag === 6) glyph.top = pbf.readSVarint();
else if (tag === 7) glyph.advance = pbf.readVarint();
}
function parseGlyphPbf(data) {
return new Pbf$1(data).readFields(readFontstacks, {});
}
const GLYPH_PBF_BORDER = border$1;
const WritingMode = {
horizontal: 1,
vertical: 2,
horizontalOnly: 3
};
const SHAPING_DEFAULT_OFFSET = -17;
function isEmpty(positionedLines) {
for (const line of positionedLines) {
if (line.positionedGlyphs.length !== 0) {
return false;
}
}
return true;
}
const PUAbegin = 57344;
const PUAend = 63743;
class SectionOptions {
constructor() {
this.scale = 1;
this.fontStack = "";
this.image = null;
}
static forText(scale, fontStack) {
const textOptions = new SectionOptions();
textOptions.scale = scale || 1;
textOptions.fontStack = fontStack;
return textOptions;
}
static forImage(image) {
const imageOptions = new SectionOptions();
imageOptions.image = image;
return imageOptions;
}
}
class TaggedString {
constructor() {
this.text = "";
this.sectionIndex = [];
this.sections = [];
this.imageSectionID = null;
}
static fromFeature(text, defaultFontStack, pixelRatio) {
const result = new TaggedString();
for (let i = 0; i < text.sections.length; i++) {
const section = text.sections[i];
if (!section.image) {
result.addTextSection(section, defaultFontStack);
} else {
result.addImageSection(section, pixelRatio);
}
}
return result;
}
length() {
return this.text.length;
}
getSection(index) {
return this.sections[this.sectionIndex[index]];
}
getSections() {
return this.sections;
}
getSectionIndex(index) {
return this.sectionIndex[index];
}
getCodePoint(index) {
return this.text.codePointAt(index);
}
verticalizePunctuation(skipContextChecking) {
this.text = verticalizePunctuation(this.text, skipContextChecking);
}
trim() {
let beginningWhitespace = 0;
for (let i = 0; i < this.text.length && whitespace[this.text.charCodeAt(i)]; i++) {
beginningWhitespace++;
}
let trailingWhitespace = this.text.length;
for (let i = this.text.length - 1; i >= 0 && i >= beginningWhitespace && whitespace[this.text.charCodeAt(i)]; i--) {
trailingWhitespace--;
}
this.text = this.text.substring(beginningWhitespace, trailingWhitespace);
this.sectionIndex = this.sectionIndex.slice(beginningWhitespace, trailingWhitespace);
}
substring(start, end) {
const substring = new TaggedString();
substring.text = this.text.substring(start, end);
substring.sectionIndex = this.sectionIndex.slice(start, end);
substring.sections = this.sections;
return substring;
}
toString() {
return this.text;
}
getMaxScale() {
return this.sectionIndex.reduce((max, index) => Math.max(max, this.sections[index].scale), 0);
}
addTextSection(section, defaultFontStack) {
this.text += section.text;
this.sections.push(SectionOptions.forText(section.scale, section.fontStack || defaultFontStack));
const index = this.sections.length - 1;
for (let i = 0; i < section.text.length; ++i) {
this.sectionIndex.push(index);
}
}
addImageSection(section, pixelRatio) {
const image = section.image ? section.image.getPrimary() : null;
if (!image) {
warnOnce(`Can't add FormattedSection with an empty image.`);
return;
}
image.scaleSelf(pixelRatio);
const nextImageSectionCharCode = this.getNextImageSectionCharCode();
if (!nextImageSectionCharCode) {
warnOnce(`Reached maximum number of images ${PUAend - PUAbegin + 2}`);
return;
}
this.text += String.fromCodePoint(nextImageSectionCharCode);
this.sections.push(SectionOptions.forImage(image));
this.sectionIndex.push(this.sections.length - 1);
}
getNextImageSectionCharCode() {
if (!this.imageSectionID) {
this.imageSectionID = PUAbegin;
return this.imageSectionID;
}
if (this.imageSectionID >= PUAend) return null;
return ++this.imageSectionID;
}
}
function breakLines(input, lineBreakPoints) {
const lines = [];
const text = input.text;
let start = 0;
for (const lineBreak of lineBreakPoints) {
lines.push(input.substring(start, lineBreak));
start = lineBreak;
}
if (start < text.length) {
lines.push(input.substring(start, text.length));
}
return lines;
}
function shapeText(text, glyphMap, glyphPositions, imagePositions, defaultFontStack, maxWidth, lineHeight, textAnchor, textJustify, spacing, translate, writingMode, allowVerticalPlacement, layoutTextSize, layoutTextSizeThisZoom, pixelRatio = 1) {
const logicalInput = TaggedString.fromFeature(text, defaultFontStack, pixelRatio);
if (writingMode === WritingMode.vertical) {
logicalInput.verticalizePunctuation(allowVerticalPlacement);
}
let lines = [];
const lineBreaks = determineLineBreaks(logicalInput, spacing, maxWidth, glyphMap, imagePositions, layoutTextSize);
const { processBidirectionalText, processStyledBidirectionalText } = plugin;
if (processBidirectionalText && logicalInput.sections.length === 1) {
const untaggedLines = processBidirectionalText(logicalInput.toString(), lineBreaks);
for (const line of untaggedLines) {
const taggedLine = new TaggedString();
taggedLine.text = line;
taggedLine.sections = logicalInput.sections;
for (let i = 0; i < line.length; i++) {
taggedLine.sectionIndex.push(0);
}
lines.push(taggedLine);
}
} else if (processStyledBidirectionalText) {
const processedLines = processStyledBidirectionalText(logicalInput.text, logicalInput.sectionIndex, lineBreaks);
for (const line of processedLines) {
const taggedLine = new TaggedString();
taggedLine.text = line[0];
taggedLine.sectionIndex = line[1];
taggedLine.sections = logicalInput.sections;
lines.push(taggedLine);
}
} else {
lines = breakLines(logicalInput, lineBreaks);
}
const positionedLines = [];
const shaping = {
positionedLines,
text: logicalInput.toString(),
top: translate[1],
bottom: translate[1],
left: translate[0],
right: translate[0],
writingMode,
iconsInText: false,
verticalizable: false,
hasBaseline: false
};
shapeLines(shaping, glyphMap, glyphPositions, imagePositions, lines, lineHeight, textAnchor, textJustify, writingMode, spacing, allowVerticalPlacement, layoutTextSizeThisZoom);
if (isEmpty(positionedLines)) return void 0;
return shaping;
}
const whitespace = {
[9]: true,
// tab
[10]: true,
// newline
[11]: true,
// vertical tab
[12]: true,
// form feed
[13]: true,
// carriage return
[32]: true
// space
};
const breakable = {
[10]: true,
// newline
[32]: true,
// space
[38]: true,
// ampersand
[40]: true,
// left parenthesis
[41]: true,
// right parenthesis
[43]: true,
// plus sign
[45]: true,
// hyphen-minus
[47]: true,
// solidus
[173]: true,
// soft hyphen
[183]: true,
// middle dot
[8203]: true,
// zero-width space
[8208]: true,
// hyphen
[8211]: true,
// en dash
[8231]: true
// interpunct
// Many other characters may be reasonable breakpoints
// Consider "neutral orientation" characters at scriptDetection.charHasNeutralVerticalOrientation
// See https://github.com/mapbox/mapbox-gl-js/issues/3658
};
function getGlyphAdvance(codePoint, section, glyphMap, imagePositions, spacing, layoutTextSize) {
if (!section.image) {
const positions = glyphMap[section.fontStack];
const glyph = positions && positions.glyphs[codePoint];
if (!glyph) return 0;
return glyph.metrics.advance * section.scale + spacing;
} else {
const imagePosition = imagePositions.get(section.image.toString());
if (!imagePosition) return 0;
return imagePosition.displaySize[0] * section.scale * ONE_EM / layoutTextSize + spacing;
}
}
function determineAverageLineWidth(logicalInput, spacing, maxWidth, glyphMap, imagePositions, layoutTextSize) {
let totalWidth = 0;
for (let index = 0; index < logicalInput.length(); index++) {
const section = logicalInput.getSection(index);
totalWidth += getGlyphAdvance(logicalInput.getCodePoint(index), section, glyphMap, imagePositions, spacing, layoutTextSize);
}
const lineCount = Math.max(1, Math.ceil(totalWidth / maxWidth));
return totalWidth / lineCount;
}
function calculateBadness(lineWidth, targetWidth, penalty, isLastBreak) {
const raggedness = Math.pow(lineWidth - targetWidth, 2);
if (isLastBreak) {
if (lineWidth < targetWidth) {
return raggedness / 2;
} else {
return raggedness * 2;
}
}
return raggedness + Math.abs(penalty) * penalty;
}
function calculatePenalty(codePoint, nextCodePoint, penalizableIdeographicBreak) {
let penalty = 0;
if (codePoint === 10) {
penalty -= 1e4;
}
if (penalizableIdeographicBreak) {
penalty += 150;
}
if (codePoint === 40 || codePoint === 65288) {
penalty += 50;
}
if (nextCodePoint === 41 || nextCodePoint === 65289) {
penalty += 50;
}
return penalty;
}
function evaluateBreak(breakIndex, breakX, targetWidth, potentialBreaks, penalty, isLastBreak) {
let bestPriorBreak = null;
let bestBreakBadness = calculateBadness(breakX, targetWidth, penalty, isLastBreak);
for (const potentialBreak of potentialBreaks) {
const lineWidth = breakX - potentialBreak.x;
const breakBadness = calculateBadness(lineWidth, targetWidth, penalty, isLastBreak) + potentialBreak.badness;
if (breakBadness <= bestBreakBadness) {
bestPriorBreak = potentialBreak;
bestBreakBadness = breakBadness;
}
}
return {
index: breakIndex,
x: breakX,
priorBreak: bestPriorBreak,
badness: bestBreakBadness
};
}
function leastBadBreaks(lastLineBreak) {
if (!lastLineBreak) {
return [];
}
return leastBadBreaks(lastLineBreak.priorBreak).concat(lastLineBreak.index);
}
function determineLineBreaks(logicalInput, spacing, maxWidth, glyphMap, imagePositions, layoutTextSize) {
if (!logicalInput)
return [];
const potentialLineBreaks = [];
const targetWidth = determineAverageLineWidth(logicalInput, spacing, maxWidth, glyphMap, imagePositions, layoutTextSize);
const hasServerSuggestedBreakpoints = logicalInput.text.indexOf("\u200B") >= 0;
let currentX = 0;
for (let i = 0; i < logicalInput.length(); i++) {
const section = logicalInput.getSection(i);
const codePoint = logicalInput.getCodePoint(i);
if (!whitespace[codePoint]) currentX += getGlyphAdvance(codePoint, section, glyphMap, imagePositions, spacing, layoutTextSize);
if (i < logicalInput.length() - 1) {
const ideographicBreak = charAllowsIdeographicBreaking(codePoint);
if (breakable[codePoint] || ideographicBreak || section.image) {
potentialLineBreaks.push(
evaluateBreak(
i + 1,
currentX,
targetWidth,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
potentialLineBreaks,
calculatePenalty(codePoint, logicalInput.getCodePoint(i + 1), ideographicBreak && hasServerSuggestedBreakpoints),
false
)
);
}
}
}
return leastBadBreaks(
evaluateBreak(
logicalInput.length(),
currentX,
targetWidth,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
potentialLineBreaks,
0,
true
)
);
}
function getAnchorAlignment(anchor) {
let horizontalAlign = 0.5, verticalAlign = 0.5;
switch (anchor) {
case "right":
case "top-right":
case "bottom-right":
horizontalAlign = 1;
break;
case "left":
case "top-left":
case "bottom-left":
horizontalAlign = 0;
break;
}
switch (anchor) {
case "bottom":
case "bottom-right":
case "bottom-left":
verticalAlign = 1;
break;
case "top":
case "top-right":
case "top-left":
verticalAlign = 0;
break;
}
return { horizontalAlign, verticalAlign };
}
function shapeLines(shaping, glyphMap, glyphPositions, imagePositions, lines, lineHeight, textAnchor, textJustify, writingMode, spacing, allowVerticalPlacement, layoutTextSizeThisZoom) {
let x = 0;
let y = 0;
let maxLineLength = 0;
let maxLineHeight = 0;
const justify = textJustify === "right" ? 1 : textJustify === "left" ? 0 : 0.5;
let hasBaseline = false;
for (const line of lines) {
const sections = line.getSections();
for (const section of sections) {
if (section.image) continue;
const glyphData = glyphMap[section.fontStack];
if (!glyphData) continue;
hasBaseline = glyphData.ascender !== void 0 && glyphData.descender !== void 0;
if (!hasBaseline) break;
}
if (!hasBaseline) break;
}
let lineIndex = 0;
for (const line of lines) {
line.trim();
const lineMaxScale = line.getMaxScale();
const maxLineOffset = (lineMaxScale - 1) * ONE_EM;
const positionedLine = { positionedGlyphs: [], lineOffset: 0 };
shaping.positionedLines[lineIndex] = positionedLine;
const positionedGlyphs = positionedLine.positionedGlyphs;
let lineOffset = 0;
if (!line.length()) {
y += lineHeight;
++lineIndex;
continue;
}
let biggestHeight = 0;
let baselineOffset = 0;
for (let i = 0; i < line.length(); i++) {
const section = line.getSection(i);
const sectionIndex = line.getSectionIndex(i);
const codePoint = line.getCodePoint(i);
let sectionScale = section.scale;
let metrics = null;
let rect = null;
let image = null;
let verticalAdvance = ONE_EM;
let glyphOffset = 0;
let glyphWritingMode = writingMode;
if (glyphWritingMode === WritingMode.vertical && needsRotationInVerticalMode(codePoint)) {
glyphWritingMode = WritingMode.horizontal;
}
const vertical = !(glyphWritingMode === WritingMode.horizontal || // Don't verticalize glyphs that have no upright orientation if vertical placement is disabled.
!allowVerticalPlacement && !charHasUprightVerticalOrientation(codePoint) || // If vertical placement is enabled, don't verticalize glyphs that
// are from complex text layout script, or whitespaces.
allowVerticalPlacement && (whitespace[codePoint] || charInComplexShapingScript(codePoint)));
if (!section.image) {
const glyphPositionData = glyphPositions[section.fontStack];
if (!glyphPositionData) continue;
if (glyphPositionData[codePoint]) {
rect = glyphPositionData[codePoint];
}
const glyphData = glyphMap[section.fontStack];
if (!glyphData) continue;
const glyph = glyphData.glyphs[codePoint];
if (!glyph) continue;
metrics = glyph.metrics;
verticalAdvance = codePoint !== 8203 ? ONE_EM : 0;
if (hasBaseline) {
const ascender = glyphData.ascender !== void 0 ? Math.abs(glyphData.ascender) : 0;
const descender = glyphData.descender !== void 0 ? Math.abs(glyphData.descender) : 0;
const value = (ascender + descender) * sectionScale;
if (biggestHeight < value) {
biggestHeight = value;
baselineOffset = (ascender - descender) / 2 * sectionScale;
}
glyphOffset = -ascender * sectionScale;
} else {
glyphOffset = SHAPING_DEFAULT_OFFSET + (lineMaxScale - sectionScale) * ONE_EM;
}
} else {
const imagePosition = imagePositions.get(section.image.toString());
if (!imagePosition) continue;
image = section.image;
shaping.iconsInText = shaping.iconsInText || true;
rect = imagePosition.paddedRect;
const size = imagePosition.displaySize;
sectionScale = sectionScale * ONE_EM / layoutTextSizeThisZoom;
metrics = {
width: size[0],
height: size[1],
left: 0,
top: -GLYPH_PBF_BORDER,
advance: vertical ? size[1] : size[0],
localGlyph: false
};
if (!hasBaseline) {
glyphOffset = SHAPING_DEFAULT_OFFSET + lineMaxScale * ONE_EM - size[1] * sectionScale;
} else {
const imageAscender = metrics.height;
glyphOffset = -imageAscender * sectionScale;
}
verticalAdvance = metrics.advance;
const offset = (vertical ? size[0] : size[1]) * sectionScale - ONE_EM * lineMaxScale;
if (offset > 0 && offset > lineOffset) {
lineOffset = offset;
}
}
if (!vertical) {
positionedGlyphs.push({ glyph: codePoint, image, x, y: y + glyphOffset, vertical, scale: sectionScale, localGlyph: metrics.localGlyph, fontStack: section.fontStack, sectionIndex, metrics, rect });
x += metrics.advance * sectionScale + spacing;
} else {
shaping.verticalizable = true;
positionedGlyphs.push({ glyph: codePoint, image, x, y: y + glyphOffset, vertical, scale: sectionScale, localGlyph: metrics.localGlyph, fontStack: section.fontStack, sectionIndex, metrics, rect });
x += verticalAdvance * sectionScale + spacing;
}
}
if (positionedGlyphs.length !== 0) {
const lineLength = x - spacing;
maxLineLength = Math.max(lineLength, maxLineLength);
if (hasBaseline) {
justifyLine(positionedGlyphs, justify, lineOffset, baselineOffset, lineHeight * lineMaxScale / 2);
} else {
justifyLine(positionedGlyphs, justify, lineOffset, 0, lineHeight / 2);
}
}
x = 0;
const currentLineHeight = lineHeight * lineMaxScale + lineOffset;
positionedLine.lineOffset = Math.max(lineOffset, maxLineOffset);
y += currentLineHeight;
maxLineHeight = Math.max(currentLineHeight, maxLineHeight);
++lineIndex;
}
const height = y;
const { horizontalAlign, verticalAlign } = getAnchorAlignment(textAnchor);
align(shaping.positionedLines, justify, horizontalAlign, verticalAlign, maxLineLength, height);
shaping.top += -verticalAlign * height;
shaping.bottom = shaping.top + height;
shaping.left += -horizontalAlign * maxLineLength;
shaping.right = shaping.left + maxLineLength;
shaping.hasBaseline = hasBaseline;
}
function justifyLine(positionedGlyphs, justify, lineOffset, baselineOffset, halfLineHeight) {
if (!justify && !lineOffset && !baselineOffset && !halfLineHeight) {
return;
}
const end = positionedGlyphs.length - 1;
const lastGlyph = positionedGlyphs[end];
const lastAdvance = lastGlyph.metrics.advance * lastGlyph.scale;
const lineIndent = (lastGlyph.x + lastAdvance) * justify;
for (let j = 0; j <= end; j++) {
positionedGlyphs[j].x -= lineIndent;
positionedGlyphs[j].y += lineOffset + baselineOffset + halfLineHeight;
}
}
function align(positionedLines, justify, horizontalAlign, verticalAlign, maxLineLength, blockHeight) {
const shiftX = (justify - horizontalAlign) * maxLineLength;
const shiftY = -blockHeight * verticalAlign;
for (const line of positionedLines) {
for (const positionedGlyph of line.positionedGlyphs) {
positionedGlyph.x += shiftX;
positionedGlyph.y += shiftY;
}
}
}
function isPositionedIcon(icon) {
return icon["imagePrimary"] !== void 0 && icon["top"] !== void 0 && icon["bottom"] !== void 0 && icon["left"] !== void 0 && icon["right"] !== void 0;
}
function shapeIcon(imagePrimary, imageSecondary, iconOffset, iconAnchor) {
const { horizontalAlign, verticalAlign } = getAnchorAlignment(iconAnchor);
const dx = iconOffset[0];
const dy = iconOffset[1];
const x1 = dx - imagePrimary.displaySize[0] * horizontalAlign;
const x2 = x1 + imagePrimary.displaySize[0];
const y1 = dy - imagePrimary.displaySize[1] * verticalAlign;
const y2 = y1 + imagePrimary.displaySize[1];
return { imagePrimary, imageSecondary, top: y1, bottom: y2, left: x1, right: x2 };
}
function fitIconToText(shapedIcon, shapedText, textFit, padding, iconOffset, fontScale) {
assert$1(textFit !== "none");
assert$1(Array.isArray(padding) && padding.length === 4);
assert$1(Array.isArray(iconOffset) && iconOffset.length === 2);
const image = shapedIcon.imagePrimary;
let collisionPadding;
if (image.content) {
const content = image.content;
const pixelRatio = image.pixelRatio || 1;
collisionPadding = [
content[0] / pixelRatio,
content[1] / pixelRatio,
image.displaySize[0] - content[2] / pixelRatio,
image.displaySize[1] - content[3] / pixelRatio
];
}
const textLeft = shapedText.left * fontScale;
const textRight = shapedText.right * fontScale;
let top;
let right;
let bottom;
let left;
if (textFit === "width" || textFit === "both") {
left = iconOffset[0] + textLeft - padding[3];
right = iconOffset[0] + textRight + padding[1];
} else {
left = iconOffset[0] + (textLeft + textRight - image.displaySize[0]) / 2;
right = left + image.displaySize[0];
}
const textTop = shapedText.top * fontScale;
const textBottom = shapedText.bottom * fontScale;
if (textFit === "height" || textFit === "both") {
top = iconOffset[1] + textTop - padding[0];
bottom = iconOffset[1] + textBottom + padding[2];
} else {
top = iconOffset[1] + (textTop + textBottom - image.displaySize[1]) / 2;
bottom = top + image.displaySize[1];
}
return { imagePrimary: image, imageSecondary: void 0, top, right, bottom, left, collisionPadding };
}
function isFullyStretchableX(icon) {
const imagePrimary = icon.imagePrimary;
return !imagePrimary.stretchX;
}
function isFullyStretchableY(icon) {
const imagePrimary = icon.imagePrimary;
return !imagePrimary.stretchY;
}
function getPositionedIconSize(icon) {
const width = icon.right - icon.left;
const height = icon.bottom - icon.top;
return { width, height };
}
const SIZE_PACK_FACTOR = 128;
function getRasterizedIconSize(sizeData, unevaluatedIconSize, canonical, zoom, feature, worldview) {
if (sizeData.kind === "camera") {
return sizeData.maxSize;
}
if (sizeData.kind === "composite") {
const maxZoomSize = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(sizeData.maxZoom, { worldview }), canonical).evaluate(feature, {}, canonical);
const minZoomSize = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(sizeData.minZoom, { worldview }), canonical).evaluate(feature, {}, canonical);
return Math.max(maxZoomSize, minZoomSize);
}
return unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(zoom, { worldview })).evaluate(feature, {}, canonical);
}
function getSizeData(tileZoom, value, worldview) {
const { expression } = value;
if (expression.kind === "constant") {
const layoutSize = expression.evaluate(new EvaluationParameters(tileZoom + 1, { worldview }));
return { kind: "constant", layoutSize };
} else if (expression.kind === "source") {
return { kind: "source" };
} else {
const { zoomStops, interpolationType } = expression;
let lower = 0;
while (lower < zoomStops.length && zoomStops[lower] <= tileZoom) lower++;
lower = Math.max(0, lower - 1);
let upper = lower;
while (upper < zoomStops.length && zoomStops[upper] < tileZoom + 1) upper++;
upper = Math.min(zoomStops.length - 1, upper);
const minZoom = zoomStops[lower];
const maxZoom = zoomStops[upper];
if (expression.kind === "composite") {
return { kind: "composite", minZoom, maxZoom, interpolationType };
}
const minSize = expression.evaluate(new EvaluationParameters(minZoom, { worldview }));
const maxSize = expression.evaluate(new EvaluationParameters(maxZoom, { worldview }));
return { kind: "camera", minZoom, maxZoom, minSize, maxSize, interpolationType };
}
}
function evaluateSizeForFeature(sizeData, {
uSize,
uSizeT
}, {
lowerSize,
upperSize
}) {
if (sizeData.kind === "source") {
return lowerSize / SIZE_PACK_FACTOR;
} else if (sizeData.kind === "composite") {
return number(lowerSize / SIZE_PACK_FACTOR, upperSize / SIZE_PACK_FACTOR, uSizeT);
}
return uSize;
}
function evaluateSizeForZoom(sizeData, zoom, scaleFactor = 1) {
let uSizeT = 0;
let uSize = 0;
if (sizeData.kind === "constant") {
uSize = sizeData.layoutSize * scaleFactor;
} else if (sizeData.kind !== "source") {
const { interpolationType, minZoom, maxZoom } = sizeData;
const t = !interpolationType ? 0 : clamp(
Interpolate.interpolationFactor(interpolationType, zoom, minZoom, maxZoom),
0,
1
);
if (sizeData.kind === "camera") {
uSize = number(sizeData.minSize, sizeData.maxSize, t) * scaleFactor;
} else {
uSizeT = t * scaleFactor;
}
}
return { uSizeT, uSize };
}
class Anchor extends Point {
constructor(x, y, z, angle, segment) {
super(x, y);
this.angle = angle;
this.z = z;
if (segment !== void 0) {
this.segment = segment;
}
}
clone() {
return new Anchor(this.x, this.y, this.z, this.angle, this.segment);
}
}
register(Anchor, "Anchor");
function checkMaxAngle(line, anchor, labelLength, windowSize, maxAngle) {
if (anchor.segment === void 0) return true;
let p = anchor;
let index = anchor.segment + 1;
let anchorDistance = 0;
while (anchorDistance > -labelLength / 2) {
index--;
if (index < 0) return false;
anchorDistance -= line[index].dist(p);
p = line[index];
}
anchorDistance += line[index].dist(line[index + 1]);
index++;
const recentCorners = [];
let recentAngleDelta = 0;
while (anchorDistance < labelLength / 2) {
const prev = line[index - 1];
const current = line[index];
const next = line[index + 1];
if (!next) return false;
let angleDelta = prev.angleTo(current) - current.angleTo(next);
angleDelta = Math.abs((angleDelta + 3 * Math.PI) % (Math.PI * 2) - Math.PI);
recentCorners.push({
distance: anchorDistance,
angleDelta
});
recentAngleDelta += angleDelta;
while (anchorDistance - recentCorners[0].distance > windowSize) {
recentAngleDelta -= recentCorners.shift().angleDelta;
}
if (recentAngleDelta > maxAngle) return false;
index++;
anchorDistance += current.dist(next);
}
return true;
}
function getLineLength(line) {
let lineLength = 0;
for (let k = 0; k < line.length - 1; k++) {
lineLength += line[k].dist(line[k + 1]);
}
return lineLength;
}
function getAngleWindowSize(shapedText, glyphSize, boxScale) {
return shapedText ? 3 / 5 * glyphSize * boxScale : 0;
}
function getShapedLabelLength(shapedText, shapedIcon) {
return Math.max(
shapedText ? shapedText.right - shapedText.left : 0,
shapedIcon ? shapedIcon.right - shapedIcon.left : 0
);
}
function getCenterAnchor(line, maxAngle, shapedText, shapedIcon, glyphSize, boxScale) {
const angleWindowSize = getAngleWindowSize(shapedText, glyphSize, boxScale);
const labelLength = getShapedLabelLength(shapedText, shapedIcon) * boxScale;
let prevDistance = 0;
const centerDistance = getLineLength(line) / 2;
for (let i = 0; i < line.length - 1; i++) {
const a = line[i], b = line[i + 1];
const segmentDistance = a.dist(b);
if (prevDistance + segmentDistance > centerDistance) {
const t = (centerDistance - prevDistance) / segmentDistance, x = number(a.x, b.x, t), y = number(a.y, b.y, t);
const anchor = new Anchor(x, y, 0, b.angleTo(a), i);
if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) {
return anchor;
} else {
return;
}
}
prevDistance += segmentDistance;
}
}
function getAnchors(line, spacing, maxAngle, shapedText, shapedIcon, glyphSize, boxScale, overscaling, tileExtent) {
const angleWindowSize = getAngleWindowSize(shapedText, glyphSize, boxScale);
const shapedLabelLength = getShapedLabelLength(shapedText, shapedIcon);
const labelLength = shapedLabelLength * boxScale;
const isLineContinued = line[0].x === 0 || line[0].x === tileExtent || line[0].y === 0 || line[0].y === tileExtent;
if (spacing - labelLength < spacing / 4) {
spacing = labelLength + spacing / 4;
}
const fixedExtraOffset = glyphSize * 2;
const offset = !isLineContinued ? (shapedLabelLength / 2 + fixedExtraOffset) * boxScale * overscaling % spacing : spacing / 2 * overscaling % spacing;
return resample(line, offset, spacing, angleWindowSize, maxAngle, labelLength, isLineContinued, false, tileExtent);
}
function resample(line, offset, spacing, angleWindowSize, maxAngle, labelLength, isLineContinued, placeAtMiddle, tileExtent) {
const halfLabelLength = labelLength / 2;
const lineLength = getLineLength(line);
let distance = 0, markedDistance = offset - spacing;
let anchors = [];
for (let i = 0; i < line.length - 1; i++) {
const a = line[i], b = line[i + 1];
const segmentDist = a.dist(b), angle = b.angleTo(a);
while (markedDistance + spacing < distance + segmentDist) {
markedDistance += spacing;
const t = (markedDistance - distance) / segmentDist, x = number(a.x, b.x, t), y = number(a.y, b.y, t);
if (x >= 0 && x < tileExtent && y >= 0 && y < tileExtent && markedDistance - halfLabelLength >= 0 && markedDistance + halfLabelLength <= lineLength) {
const anchor = new Anchor(x, y, 0, angle, i);
if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) {
anchors.push(anchor);
}
}
}
distance += segmentDist;
}
if (!placeAtMiddle && !anchors.length && !isLineContinued) {
anchors = resample(line, distance / 2, spacing, angleWindowSize, maxAngle, labelLength, isLineContinued, true, tileExtent);
}
return anchors;
}
/**
* @typedef {Object} PotpackBox
* @property {number} w Box width.
* @property {number} h Box height.
* @property {number} [x] X coordinate in the resulting container.
* @property {number} [y] Y coordinate in the resulting container.
*/
/**
* @typedef {Object} PotpackStats
* @property {number} w Width of the resulting container.
* @property {number} h Height of the resulting container.
* @property {number} fill The space utilization value (0 to 1). Higher is better.
*/
/**
* Packs 2D rectangles into a near-square container.
*
* Mutates the {@link boxes} array: it's sorted (by height/width),
* and box objects are augmented with `x`, `y` coordinates.
*
* @param {PotpackBox[]} boxes
* @return {PotpackStats}
*/
function potpack(boxes) {
// calculate total box area and maximum box width
let area = 0;
let maxWidth = 0;
for (const box of boxes) {
area += box.w * box.h;
maxWidth = Math.max(maxWidth, box.w);
}
// sort the boxes for insertion by height, descending
boxes.sort((a, b) => b.h - a.h);
// aim for a squarish resulting container,
// slightly adjusted for sub-100% space utilization
const startWidth = Math.max(Math.ceil(Math.sqrt(area / 0.95)), maxWidth);
// start with a single empty space, unbounded at the bottom
const spaces = [{x: 0, y: 0, w: startWidth, h: Infinity}];
let width = 0;
let height = 0;
for (const box of boxes) {
// look through spaces backwards so that we check smaller spaces first
for (let i = spaces.length - 1; i >= 0; i--) {
const space = spaces[i];
// look for empty spaces that can accommodate the current box
if (box.w > space.w || box.h > space.h) continue;
// found the space; add the box to its top-left corner
// |-------|-------|
// | box | |
// |_______| |
// | space |
// |_______________|
box.x = space.x;
box.y = space.y;
height = Math.max(height, box.y + box.h);
width = Math.max(width, box.x + box.w);
if (box.w === space.w && box.h === space.h) {
// space matches the box exactly; remove it
const last = spaces.pop();
if (last && i < spaces.length) spaces[i] = last;
} else if (box.h === space.h) {
// space matches the box height; update it accordingly
// |-------|---------------|
// | box | updated space |
// |_______|_______________|
space.x += box.w;
space.w -= box.w;
} else if (box.w === space.w) {
// space matches the box width; update it accordingly
// |---------------|
// | box |
// |_______________|
// | updated space |
// |_______________|
space.y += box.h;
space.h -= box.h;
} else {
// otherwise the box splits the space into two spaces
// |-------|-----------|
// | box | new space |
// |_______|___________|
// | updated space |
// |___________________|
spaces.push({
x: space.x + box.w,
y: space.y,
w: space.w - box.w,
h: box.h
});
space.y += box.h;
space.h -= box.h;
}
break;
}
}
return {
w: width, // container width
h: height, // container height
fill: (area / (width * height)) || 0 // space utilization
};
}
const ICON_PADDING = 1;
const PATTERN_PADDING = 2;
class ImagePosition {
static getImagePositionScale(imageVariant, usvg, pixelRatio) {
if (usvg && imageVariant) {
const { sx, sy } = imageVariant;
return {
x: sx,
y: sy
};
} else {
return {
x: pixelRatio,
y: pixelRatio
};
}
}
constructor(paddedRect, image, padding, imageVariant) {
this.paddedRect = paddedRect;
const {
pixelRatio,
version,
stretchX,
stretchY,
content,
sdf,
usvg
} = image;
this.pixelRatio = pixelRatio;
this.stretchX = stretchX;
this.stretchY = stretchY;
this.content = content;
this.version = version;
this.padding = padding;
this.sdf = sdf;
this.usvg = usvg;
this.scale = ImagePosition.getImagePositionScale(imageVariant, usvg, pixelRatio);
}
get tl() {
return [
this.paddedRect.x + this.padding,
this.paddedRect.y + this.padding
];
}
get br() {
return [
this.paddedRect.x + this.paddedRect.w - this.padding,
this.paddedRect.y + this.paddedRect.h - this.padding
];
}
get displaySize() {
return [
(this.paddedRect.w - this.padding * 2) / this.scale.x,
(this.paddedRect.h - this.padding * 2) / this.scale.y
];
}
}
function getImageBin(image, padding, scale = [1, 1]) {
const imageWidth = image.data ? image.data.width : image.width * scale[0];
const imageHeight = image.data ? image.data.height : image.height * scale[1];
return {
x: 0,
y: 0,
w: imageWidth + 2 * padding,
h: imageHeight + 2 * padding
};
}
function getImagePosition(id, src, padding) {
const imageVariant = ImageVariant.parse(id);
const bin = getImageBin(src, padding, [imageVariant.sx, imageVariant.sy]);
return { bin, imagePosition: new ImagePosition(bin, src, padding, imageVariant), imageVariant };
}
class ImageAtlas {
constructor(icons, patterns, lut) {
const iconPositions = /* @__PURE__ */ new Map();
const patternPositions = /* @__PURE__ */ new Map();
this.haveRenderCallbacks = [];
const bins = [];
this.addImages(icons, iconPositions, ICON_PADDING, bins);
this.addImages(patterns, patternPositions, PATTERN_PADDING, bins);
const { w, h } = potpack(bins);
const image = new RGBAImage({ width: w || 1, height: h || 1 });
for (const [id, src] of icons.entries()) {
const bin = iconPositions.get(id).paddedRect;
const overrideRGB = src.sdf;
RGBAImage.copy(src.data, image, { x: 0, y: 0 }, { x: bin.x + ICON_PADDING, y: bin.y + ICON_PADDING }, src.data, null, overrideRGB);
}
for (const [id, src] of patterns.entries()) {
const patternPosition = patternPositions.get(id);
const bin = patternPosition.paddedRect;
let padding = patternPosition.padding;
const x = bin.x + padding, y = bin.y + padding, w2 = src.data.width, h2 = src.data.height;
assert$1(padding > 1);
padding = padding > 1 ? padding - 1 : padding;
RGBAImage.copy(src.data, image, { x: 0, y: 0 }, { x, y }, src.data, lut);
RGBAImage.copy(src.data, image, { x: 0, y: h2 - padding }, { x, y: y - padding }, { width: w2, height: padding }, lut);
RGBAImage.copy(src.data, image, { x: 0, y: 0 }, { x, y: y + h2 }, { width: w2, height: padding }, lut);
RGBAImage.copy(src.data, image, { x: w2 - padding, y: 0 }, { x: x - padding, y }, { width: padding, height: h2 }, lut);
RGBAImage.copy(src.data, image, { x: 0, y: 0 }, { x: x + w2, y }, { width: padding, height: h2 }, lut);
RGBAImage.copy(src.data, image, { x: w2 - padding, y: h2 - padding }, { x: x - padding, y: y - padding }, { width: padding, height: padding }, lut);
RGBAImage.copy(src.data, image, { x: 0, y: h2 - padding }, { x: x + w2, y: y - padding }, { width: padding, height: padding }, lut);
RGBAImage.copy(src.data, image, { x: 0, y: 0 }, { x: x + w2, y: y + h2 }, { width: padding, height: padding }, lut);
RGBAImage.copy(src.data, image, { x: w2 - padding, y: 0 }, { x: x - padding, y: y + h2 }, { width: padding, height: padding }, lut);
}
this.lut = lut;
this.image = image;
this.iconPositions = iconPositions;
this.patternPositions = patternPositions;
}
addImages(images, positions, padding, bins) {
for (const [id, src] of images.entries()) {
const { bin, imagePosition, imageVariant } = getImagePosition(id, src, padding);
positions.set(id, imagePosition);
bins.push(bin);
if (src.hasRenderCallback) {
this.haveRenderCallbacks.push(imageVariant.id);
}
}
}
patchUpdatedImages(imageManager, texture, scope) {
this.haveRenderCallbacks = this.haveRenderCallbacks.filter((id) => imageManager.hasImage(id, scope));
imageManager.dispatchRenderCallbacks(this.haveRenderCallbacks, scope);
for (const imageId of imageManager.getUpdatedImages(scope)) {
for (const id of this.iconPositions.keys()) {
const imageVariant = ImageVariant.parse(id);
if (ImageId.isEqual(imageVariant.id, imageId)) {
const image = imageManager.getImage(imageId, scope);
this.patchUpdatedImage(this.iconPositions.get(id), image, texture, null);
}
}
for (const id of this.patternPositions.keys()) {
const imageVariant = ImageVariant.parse(id);
if (ImageId.isEqual(imageVariant.id, imageId)) {
const image = imageManager.getImage(imageId, scope);
this.patchUpdatedImage(this.patternPositions.get(id), image, texture, this.lut);
}
}
}
}
patchUpdatedImage(position, image, texture, lut = null) {
if (!position || !image) return;
if (position.version === image.version) return;
position.version = image.version;
const [x, y] = position.tl;
const overrideRGBWithWhite = position.sdf;
if (this.lut || overrideRGBWithWhite) {
const size = { width: image.data.width, height: image.data.height };
const imageToUpload = new RGBAImage(size);
RGBAImage.copy(image.data, imageToUpload, { x: 0, y: 0 }, { x: 0, y: 0 }, size, lut, overrideRGBWithWhite);
texture.update(imageToUpload, { position: { x, y } });
} else {
texture.update(image.data, { position: { x, y } });
}
}
}
register(ImagePosition, "ImagePosition");
register(ImageAtlas, "ImageAtlas");
function loadGlyphRange(fontstack, range, urlTemplate, requestManager, callback) {
const begin = range * 256;
const end = begin + 255;
const request = requestManager.transformRequest(
requestManager.normalizeGlyphsURL(urlTemplate).replace("{fontstack}", fontstack).replace("{range}", `${begin}-${end}`),
ResourceType.Glyphs
);
getArrayBuffer(request, (err, data) => {
if (err) {
callback(err);
} else if (data) {
const glyphs = {};
const glyphData = parseGlyphPbf(data);
for (const glyph of glyphData.glyphs) {
glyphs[glyph.id] = glyph;
}
callback(null, { glyphs, ascender: glyphData.ascender, descender: glyphData.descender });
}
});
}
const INF = 1e20;
class TinySDF {
constructor({
fontSize = 24,
buffer = 3,
radius = 8,
cutoff = 0.25,
fontFamily = 'sans-serif',
fontWeight = 'normal',
fontStyle = 'normal',
lang = null
} = {}) {
this.buffer = buffer;
this.cutoff = cutoff;
this.radius = radius;
this.lang = lang;
// make the canvas size big enough to both have the specified buffer around the glyph
// for "halo", and account for some glyphs possibly being larger than their font size
const size = this.size = fontSize + buffer * 4;
const canvas = this._createCanvas(size);
const ctx = this.ctx = canvas.getContext('2d', {willReadFrequently: true});
ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
ctx.textBaseline = 'alphabetic';
ctx.textAlign = 'left'; // Necessary so that RTL text doesn't have different alignment
ctx.fillStyle = 'black';
// temporary arrays for the distance transform
this.gridOuter = new Float64Array(size * size);
this.gridInner = new Float64Array(size * size);
this.f = new Float64Array(size);
this.z = new Float64Array(size + 1);
this.v = new Uint16Array(size);
}
_createCanvas(size) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = size;
return canvas;
}
draw(char) {
const {
width: glyphAdvance,
actualBoundingBoxAscent,
actualBoundingBoxDescent,
actualBoundingBoxLeft,
actualBoundingBoxRight
} = this.ctx.measureText(char);
// The integer/pixel part of the top alignment is encoded in metrics.glyphTop
// The remainder is implicitly encoded in the rasterization
const glyphTop = Math.ceil(actualBoundingBoxAscent);
const glyphLeft = 0;
// If the glyph overflows the canvas size, it will be clipped at the bottom/right
const glyphWidth = Math.max(0, Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft)));
const glyphHeight = Math.min(this.size - this.buffer, glyphTop + Math.ceil(actualBoundingBoxDescent));
const width = glyphWidth + 2 * this.buffer;
const height = glyphHeight + 2 * this.buffer;
const len = Math.max(width * height, 0);
const data = new Uint8ClampedArray(len);
const glyph = {data, width, height, glyphWidth, glyphHeight, glyphTop, glyphLeft, glyphAdvance};
if (glyphWidth === 0 || glyphHeight === 0) return glyph;
const {ctx, buffer, gridInner, gridOuter} = this;
if (this.lang) ctx.lang = this.lang;
ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight);
ctx.fillText(char, buffer, buffer + glyphTop);
const imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight);
// Initialize grids outside the glyph range to alpha 0
gridOuter.fill(INF, 0, len);
gridInner.fill(0, 0, len);
for (let y = 0; y < glyphHeight; y++) {
for (let x = 0; x < glyphWidth; x++) {
const a = imgData.data[4 * (y * glyphWidth + x) + 3] / 255; // alpha value
if (a === 0) continue; // empty pixels
const j = (y + buffer) * width + x + buffer;
if (a === 1) { // fully drawn pixels
gridOuter[j] = 0;
gridInner[j] = INF;
} else { // aliased pixels
const d = 0.5 - a;
gridOuter[j] = d > 0 ? d * d : 0;
gridInner[j] = d < 0 ? d * d : 0;
}
}
}
edt(gridOuter, 0, 0, width, height, width, this.f, this.v, this.z);
edt(gridInner, buffer, buffer, glyphWidth, glyphHeight, width, this.f, this.v, this.z);
for (let i = 0; i < len; i++) {
const d = Math.sqrt(gridOuter[i]) - Math.sqrt(gridInner[i]);
data[i] = Math.round(255 - 255 * (d / this.radius + this.cutoff));
}
return glyph;
}
}
// 2D Euclidean squared distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/papers/dt-final.pdf
function edt(data, x0, y0, width, height, gridSize, f, v, z) {
for (let x = x0; x < x0 + width; x++) edt1d(data, y0 * gridSize + x, gridSize, height, f, v, z);
for (let y = y0; y < y0 + height; y++) edt1d(data, y * gridSize + x0, 1, width, f, v, z);
}
// 1D squared distance transform
function edt1d(grid, offset, stride, length, f, v, z) {
v[0] = 0;
z[0] = -INF;
z[1] = INF;
f[0] = grid[offset];
for (let q = 1, k = 0, s = 0; q < length; q++) {
f[q] = grid[offset + q * stride];
const q2 = q * q;
do {
const r = v[k];
s = (f[q] - f[r] + q2 - r * r) / (q - r) / 2;
} while (s <= z[k] && --k > -1);
k++;
v[k] = q;
z[k] = s;
z[k + 1] = INF;
}
for (let q = 0, k = 0; q < length; q++) {
while (z[k + 1] < q) k++;
const r = v[k];
const qr = q - r;
grid[offset + q * stride] = f[r] + qr * qr;
}
}
const SDF_SCALE = 2;
const LocalGlyphMode = {
none: 0,
ideographs: 1,
all: 2
};
class GlyphManager {
constructor(requestManager, localGlyphMode, localFontFamily) {
this.requestManager = requestManager;
this.localGlyphMode = localGlyphMode;
this.localFontFamily = localFontFamily;
this.url = "";
this.entries = {};
this.localGlyphs = {
"200": {},
"400": {},
"500": {},
"900": {}
};
}
setURL(url) {
this.url = url;
}
getGlyphs(glyphs, callback) {
const all = [];
const url = this.url || config.GLYPHS_URL;
for (const stack in glyphs) {
for (const id of glyphs[stack]) {
all.push({ stack, id });
}
}
asyncAll(all, ({ stack, id }, fnCallback) => {
let entry = this.entries[stack];
if (!entry) {
entry = this.entries[stack] = {
glyphs: {},
requests: {},
ranges: {},
ascender: void 0,
descender: void 0
};
}
let glyph = entry.glyphs[id];
if (glyph !== void 0) {
fnCallback(null, { stack, id, glyph });
return;
}
glyph = this._tinySDF(entry, stack, id);
if (glyph) {
entry.glyphs[id] = glyph;
fnCallback(null, { stack, id, glyph });
return;
}
const range = Math.floor(id / 256);
if (range * 256 > 65535) {
warnOnce("glyphs > 65535 not supported");
fnCallback(null, { stack, id, glyph });
return;
}
if (entry.ranges[range]) {
fnCallback(null, { stack, id, glyph });
return;
}
let requests = entry.requests[range];
if (!requests) {
requests = entry.requests[range] = [];
GlyphManager.loadGlyphRange(
stack,
range,
url,
this.requestManager,
(err, response) => {
if (response) {
entry.ascender = response.ascender;
entry.descender = response.descender;
for (const id2 in response.glyphs) {
if (!this._doesCharSupportLocalGlyph(+id2)) {
entry.glyphs[+id2] = response.glyphs[+id2];
}
}
entry.ranges[range] = true;
}
for (const cb of requests) {
cb(err, response);
}
delete entry.requests[range];
}
);
}
requests.push((err, result) => {
if (err) {
fnCallback(err);
} else if (result) {
fnCallback(null, { stack, id, glyph: result.glyphs[id] || null });
}
});
}, (err, glyphs2) => {
if (err) {
callback(err);
} else if (glyphs2) {
const result = {};
for (const { stack, id, glyph } of glyphs2) {
if (result[stack] === void 0) result[stack] = {};
if (result[stack].glyphs === void 0) result[stack].glyphs = {};
result[stack].glyphs[id] = glyph && {
id: glyph.id,
bitmap: glyph.bitmap.clone(),
metrics: glyph.metrics
};
result[stack].ascender = this.entries[stack].ascender;
result[stack].descender = this.entries[stack].descender;
}
callback(null, result);
}
});
}
_doesCharSupportLocalGlyph(id) {
if (this.localGlyphMode === LocalGlyphMode.none) {
return false;
} else if (this.localGlyphMode === LocalGlyphMode.all) {
return !!this.localFontFamily;
} else {
return !!this.localFontFamily && (unicodeBlockLookup["CJK Unified Ideographs"](id) || unicodeBlockLookup["Hangul Syllables"](id) || unicodeBlockLookup["Hiragana"](id) || unicodeBlockLookup["Katakana"](id) || // gl-native parity: Extend Ideographs rasterization range to include CJK symbols and punctuations
unicodeBlockLookup["CJK Symbols and Punctuation"](id) || unicodeBlockLookup["CJK Unified Ideographs Extension A"](id) || unicodeBlockLookup["CJK Unified Ideographs Extension B"](id) || // very rare surrogate characters
unicodeBlockLookup["Osage"](id));
}
}
_tinySDF(entry, stack, id) {
const fontFamily = this.localFontFamily;
if (!fontFamily || !this._doesCharSupportLocalGlyph(id)) return;
let tinySDF = entry.tinySDF;
if (!tinySDF) {
let fontWeight = "400";
if (/bold/i.test(stack)) {
fontWeight = "900";
} else if (/medium/i.test(stack)) {
fontWeight = "500";
} else if (/light/i.test(stack)) {
fontWeight = "200";
}
const fontSize = 24 * SDF_SCALE;
const buffer = 3 * SDF_SCALE;
const radius = 8 * SDF_SCALE;
tinySDF = entry.tinySDF = new GlyphManager.TinySDF({ fontFamily, fontWeight, fontSize, buffer, radius });
tinySDF.fontWeight = fontWeight;
}
if (this.localGlyphs[tinySDF.fontWeight][id]) {
return this.localGlyphs[tinySDF.fontWeight][id];
}
const char = String.fromCodePoint(id);
const { data, width, height, glyphWidth, glyphHeight, glyphLeft, glyphTop, glyphAdvance } = tinySDF.draw(char);
const baselineAdjustment = 27;
const glyph = this.localGlyphs[tinySDF.fontWeight][id] = {
id,
bitmap: new AlphaImage({ width, height }, data),
metrics: {
width: glyphWidth / SDF_SCALE,
height: glyphHeight / SDF_SCALE,
left: glyphLeft / SDF_SCALE,
top: glyphTop / SDF_SCALE - baselineAdjustment,
advance: glyphAdvance / SDF_SCALE,
localGlyph: true
}
};
return glyph;
}
}
GlyphManager.loadGlyphRange = loadGlyphRange;
GlyphManager.TinySDF = TinySDF;
const border = ICON_PADDING;
function reduceRanges(sum, range) {
return sum + range[1] - range[0];
}
function getIconQuads(shapedIcon, iconRotate, isSDFIcon, hasIconTextFit, iconScale = 1) {
const quads = [];
const image = shapedIcon.imagePrimary;
const pixelRatio = image.pixelRatio;
const imageWidth = image.paddedRect.w - 2 * border;
const imageHeight = image.paddedRect.h - 2 * border;
const iconWidth = (shapedIcon.right - shapedIcon.left) * iconScale;
const iconHeight = (shapedIcon.bottom - shapedIcon.top) * iconScale;
const stretchX = image.stretchX || [[0, imageWidth]];
const stretchY = image.stretchY || [[0, imageHeight]];
const stretchWidth = stretchX.reduce(reduceRanges, 0);
const stretchHeight = stretchY.reduce(reduceRanges, 0);
const fixedWidth = imageWidth - stretchWidth;
const fixedHeight = imageHeight - stretchHeight;
let stretchOffsetX = 0;
let stretchContentWidth = stretchWidth;
let stretchOffsetY = 0;
let stretchContentHeight = stretchHeight;
let fixedOffsetX = 0;
let fixedContentWidth = fixedWidth;
let fixedOffsetY = 0;
let fixedContentHeight = fixedHeight;
if (image.content && hasIconTextFit) {
const content = image.content;
stretchOffsetX = sumWithinRange(stretchX, 0, content[0]);
stretchOffsetY = sumWithinRange(stretchY, 0, content[1]);
stretchContentWidth = sumWithinRange(stretchX, content[0], content[2]);
stretchContentHeight = sumWithinRange(stretchY, content[1], content[3]);
fixedOffsetX = content[0] - stretchOffsetX;
fixedOffsetY = content[1] - stretchOffsetY;
fixedContentWidth = content[2] - content[0] - stretchContentWidth;
fixedContentHeight = content[3] - content[1] - stretchContentHeight;
}
const makeBox = (left, top, right, bottom) => {
const leftEm = getEmOffset(left.stretch - stretchOffsetX, stretchContentWidth, iconWidth, shapedIcon.left * iconScale);
const leftPx = getPxOffset(left.fixed - fixedOffsetX, fixedContentWidth, left.stretch, stretchWidth);
const topEm = getEmOffset(top.stretch - stretchOffsetY, stretchContentHeight, iconHeight, shapedIcon.top * iconScale);
const topPx = getPxOffset(top.fixed - fixedOffsetY, fixedContentHeight, top.stretch, stretchHeight);
const rightEm = getEmOffset(right.stretch - stretchOffsetX, stretchContentWidth, iconWidth, shapedIcon.left * iconScale);
const rightPx = getPxOffset(right.fixed - fixedOffsetX, fixedContentWidth, right.stretch, stretchWidth);
const bottomEm = getEmOffset(bottom.stretch - stretchOffsetY, stretchContentHeight, iconHeight, shapedIcon.top * iconScale);
const bottomPx = getPxOffset(bottom.fixed - fixedOffsetY, fixedContentHeight, bottom.stretch, stretchHeight);
const tl = new Point(leftEm, topEm);
const tr = new Point(rightEm, topEm);
const br = new Point(rightEm, bottomEm);
const bl = new Point(leftEm, bottomEm);
const pixelOffsetTL = new Point(leftPx / pixelRatio, topPx / pixelRatio);
const pixelOffsetBR = new Point(rightPx / pixelRatio, bottomPx / pixelRatio);
const angle = iconRotate * Math.PI / 180;
if (angle) {
const sin = Math.sin(angle), cos = Math.cos(angle), matrix = [cos, -sin, sin, cos];
tl._matMult(matrix);
tr._matMult(matrix);
bl._matMult(matrix);
br._matMult(matrix);
}
const x1 = left.stretch + left.fixed;
const x2 = right.stretch + right.fixed;
const y1 = top.stretch + top.fixed;
const y2 = bottom.stretch + bottom.fixed;
const subRect = {
x: image.paddedRect.x + border + x1,
y: image.paddedRect.y + border + y1,
w: x2 - x1,
h: y2 - y1
};
const imageSecondary = shapedIcon.imageSecondary;
const subRectB = imageSecondary ? {
x: imageSecondary.paddedRect.x + border + x1,
y: imageSecondary.paddedRect.y + border + y1,
w: x2 - x1,
h: y2 - y1
} : void 0;
const minFontScaleX = fixedContentWidth / pixelRatio / iconWidth;
const minFontScaleY = fixedContentHeight / pixelRatio / iconHeight;
return { tl, tr, bl, br, texPrimary: subRect, texSecondary: subRectB, writingMode: void 0, glyphOffset: [0, 0], sectionIndex: 0, pixelOffsetTL, pixelOffsetBR, minFontScaleX, minFontScaleY, isSDF: isSDFIcon };
};
if (!hasIconTextFit || !image.stretchX && !image.stretchY) {
quads.push(makeBox(
{ fixed: 0, stretch: -1 },
{ fixed: 0, stretch: -1 },
{ fixed: 0, stretch: imageWidth + 1 },
{ fixed: 0, stretch: imageHeight + 1 }
));
} else {
const xCuts = stretchZonesToCuts(stretchX, fixedWidth, stretchWidth);
const yCuts = stretchZonesToCuts(stretchY, fixedHeight, stretchHeight);
for (let xi = 0; xi < xCuts.length - 1; xi++) {
const x1 = xCuts[xi];
const x2 = xCuts[xi + 1];
for (let yi = 0; yi < yCuts.length - 1; yi++) {
const y1 = yCuts[yi];
const y2 = yCuts[yi + 1];
quads.push(makeBox(x1, y1, x2, y2));
}
}
}
return quads;
}
function getIconQuadsNumber(image, hasIconTextFit) {
const imageWidth = image.paddedRect.w - 2 * border;
const imageHeight = image.paddedRect.h - 2 * border;
const stretchX = image.stretchX || [[0, imageWidth]];
const stretchY = image.stretchY || [[0, imageHeight]];
if (!hasIconTextFit || !image.stretchX && !image.stretchY) {
return 1;
}
const xCuts = stretchZonesNumber(stretchX);
const yCuts = stretchZonesNumber(stretchY);
return xCuts * yCuts;
}
function sumWithinRange(ranges, min, max) {
let sum = 0;
for (const range of ranges) {
sum += Math.max(min, Math.min(max, range[1])) - Math.max(min, Math.min(max, range[0]));
}
return sum;
}
function stretchZonesToCuts(stretchZones, fixedSize, stretchSize) {
const cuts = [{ fixed: -border, stretch: 0 }];
for (const [c1, c2] of stretchZones) {
const last = cuts[cuts.length - 1];
cuts.push({
fixed: c1 - last.stretch,
stretch: last.stretch
});
cuts.push({
fixed: c1 - last.stretch,
stretch: last.stretch + (c2 - c1)
});
}
cuts.push({
fixed: fixedSize + border,
stretch: stretchSize
});
return cuts;
}
function stretchZonesNumber(stretchZones) {
return 2 * stretchZones.length + 1;
}
function getEmOffset(stretchOffset, stretchSize, iconSize, iconOffset) {
return stretchOffset / stretchSize * iconSize + iconOffset;
}
function getPxOffset(fixedOffset, fixedSize, stretchOffset, stretchSize) {
return fixedOffset - fixedSize * stretchOffset / stretchSize;
}
function getRotateOffset(textOffset) {
const x = textOffset[0], y = textOffset[1];
const product = x * y;
if (product > 0) {
return [x, -y];
} else if (product < 0) {
return [-x, y];
} else if (x === 0) {
return [y, x];
} else {
return [y, -x];
}
}
function getMidlineOffset(shaping, lineHeight, previousOffset, lineIndex) {
const currentLineHeight = lineHeight + shaping.positionedLines[lineIndex].lineOffset;
if (lineIndex === 0) {
return previousOffset + currentLineHeight / 2;
}
const aboveLineHeight = lineHeight + shaping.positionedLines[lineIndex - 1].lineOffset;
return previousOffset + (currentLineHeight + aboveLineHeight) / 2;
}
function getGlyphQuads(anchor, shaping, textOffset, layer, alongLine, feature, imageMap, allowVerticalPlacement) {
const quads = [];
if (shaping.positionedLines.length === 0) return quads;
const textRotate = layer.layout.get("text-rotate").evaluate(feature, {}) * Math.PI / 180;
const rotateOffset = getRotateOffset(textOffset);
let shapingHeight = Math.abs(shaping.top - shaping.bottom);
for (const line of shaping.positionedLines) {
shapingHeight -= line.lineOffset;
}
const lineCounts = shaping.positionedLines.length;
const lineHeight = shapingHeight / lineCounts;
let currentOffset = shaping.top - textOffset[1];
for (let lineIndex = 0; lineIndex < lineCounts; ++lineIndex) {
const line = shaping.positionedLines[lineIndex];
currentOffset = getMidlineOffset(shaping, lineHeight, currentOffset, lineIndex);
for (const positionedGlyph of line.positionedGlyphs) {
if (!positionedGlyph.rect) continue;
const textureRect = positionedGlyph.rect || {};
const glyphPadding = 1;
let rectBuffer = GLYPH_PBF_BORDER + glyphPadding;
let isSDF = true;
let pixelRatio = 1;
let lineOffset = 0;
if (positionedGlyph.image) {
const image = imageMap.get(positionedGlyph.image.toString());
if (!image) continue;
if (image.sdf) {
warnOnce("SDF images are not supported in formatted text and will be ignored.");
continue;
}
isSDF = false;
pixelRatio = image.pixelRatio;
rectBuffer = ICON_PADDING / pixelRatio;
}
const rotateVerticalGlyph = (alongLine || allowVerticalPlacement) && positionedGlyph.vertical;
const halfAdvance = positionedGlyph.metrics.advance * positionedGlyph.scale / 2;
const metrics = positionedGlyph.metrics;
const rect = positionedGlyph.rect;
if (rect === null) continue;
if (allowVerticalPlacement && shaping.verticalizable) {
lineOffset = positionedGlyph.image ? halfAdvance - positionedGlyph.metrics.width * positionedGlyph.scale / 2 : 0;
}
const glyphOffset = alongLine ? [positionedGlyph.x + halfAdvance, positionedGlyph.y] : [0, 0];
let builtInOffset = [0, 0];
let verticalizedLabelOffset = [0, 0];
let useRotateOffset = false;
if (!alongLine) {
if (rotateVerticalGlyph) {
verticalizedLabelOffset = [positionedGlyph.x + halfAdvance + rotateOffset[0], positionedGlyph.y + rotateOffset[1] - lineOffset];
useRotateOffset = true;
} else {
builtInOffset = [positionedGlyph.x + halfAdvance + textOffset[0], positionedGlyph.y + textOffset[1] - lineOffset];
}
}
const paddedWidth = rect.w * positionedGlyph.scale / (pixelRatio * (positionedGlyph.localGlyph ? SDF_SCALE : 1));
const paddedHeight = rect.h * positionedGlyph.scale / (pixelRatio * (positionedGlyph.localGlyph ? SDF_SCALE : 1));
let tl;
let tr;
let bl;
let br;
if (!rotateVerticalGlyph) {
const x1 = (metrics.left - rectBuffer) * positionedGlyph.scale - halfAdvance + builtInOffset[0];
const y1 = (-metrics.top - rectBuffer) * positionedGlyph.scale + builtInOffset[1];
const x2 = x1 + paddedWidth;
const y2 = y1 + paddedHeight;
tl = new Point(x1, y1);
tr = new Point(x2, y1);
bl = new Point(x1, y2);
br = new Point(x2, y2);
} else {
const yShift = positionedGlyph.y - currentOffset;
const center = new Point(-halfAdvance, halfAdvance - yShift);
const verticalRotation = -Math.PI / 2;
const verticalOffsetCorrection = new Point(...verticalizedLabelOffset);
tl = new Point(-halfAdvance + builtInOffset[0], builtInOffset[1]);
tl._rotateAround(verticalRotation, center)._add(verticalOffsetCorrection);
tl.x += -yShift + halfAdvance;
tl.y -= (metrics.left - rectBuffer) * positionedGlyph.scale;
const verticalAdvance = positionedGlyph.image ? metrics.advance * positionedGlyph.scale : ONE_EM * positionedGlyph.scale;
const chr = String.fromCodePoint(positionedGlyph.glyph);
if (isVerticalClosePunctuation(chr)) {
tl.x += (-rectBuffer + 1) * positionedGlyph.scale;
} else if (isVerticalOpenPunctuation(chr)) {
const xOffset = verticalAdvance - metrics.height * positionedGlyph.scale;
tl.x += xOffset + (-rectBuffer - 1) * positionedGlyph.scale;
} else if (!positionedGlyph.image && (metrics.width + rectBuffer * 2 !== rect.w || metrics.height + rectBuffer * 2 !== rect.h)) {
const perfectPaddedHeight = (metrics.height + rectBuffer * 2) * positionedGlyph.scale;
const delta = verticalAdvance - perfectPaddedHeight;
tl.x += delta / 2;
} else {
const delta = verticalAdvance - paddedHeight;
tl.x += delta / 2;
}
tr = new Point(tl.x, tl.y - paddedWidth);
bl = new Point(tl.x + paddedHeight, tl.y);
br = new Point(tl.x + paddedHeight, tl.y - paddedWidth);
}
if (textRotate) {
let center;
if (!alongLine) {
if (useRotateOffset) {
center = new Point(rotateOffset[0], rotateOffset[1]);
} else {
center = new Point(textOffset[0], textOffset[1]);
}
} else {
center = new Point(0, 0);
}
tl._rotateAround(textRotate, center);
tr._rotateAround(textRotate, center);
bl._rotateAround(textRotate, center);
br._rotateAround(textRotate, center);
}
const pixelOffsetTL = new Point(0, 0);
const pixelOffsetBR = new Point(0, 0);
const minFontScaleX = 0;
const minFontScaleY = 0;
quads.push({ tl, tr, bl, br, texPrimary: textureRect, texSecondary: void 0, writingMode: shaping.writingMode, glyphOffset, sectionIndex: positionedGlyph.sectionIndex, isSDF, pixelOffsetTL, pixelOffsetBR, minFontScaleX, minFontScaleY });
}
}
return quads;
}
function findPoleOfInaccessibility(polygonRings, precision = 1, debug = false) {
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
const outerRing = polygonRings[0];
for (let i = 0; i < outerRing.length; i++) {
const p = outerRing[i];
if (!i || p.x < minX) minX = p.x;
if (!i || p.y < minY) minY = p.y;
if (!i || p.x > maxX) maxX = p.x;
if (!i || p.y > maxY) maxY = p.y;
}
const width = maxX - minX;
const height = maxY - minY;
const cellSize = Math.min(width, height);
let h = cellSize / 2;
const cellQueue = new TinyQueue([], compareMax);
if (cellSize === 0) return new Point(minX, minY);
for (let x = minX; x < maxX; x += cellSize) {
for (let y = minY; y < maxY; y += cellSize) {
cellQueue.push(new Cell(x + h, y + h, h, polygonRings));
}
}
let bestCell = getCentroidCell(polygonRings);
let numProbes = cellQueue.length;
while (cellQueue.length) {
const cell = cellQueue.pop();
if (cell.d > bestCell.d || !bestCell.d) {
bestCell = cell;
if (debug) console.log("found best %d after %d probes", Math.round(1e4 * cell.d) / 1e4, numProbes);
}
if (cell.max - bestCell.d <= precision) continue;
h = cell.h / 2;
cellQueue.push(new Cell(cell.p.x - h, cell.p.y - h, h, polygonRings));
cellQueue.push(new Cell(cell.p.x + h, cell.p.y - h, h, polygonRings));
cellQueue.push(new Cell(cell.p.x - h, cell.p.y + h, h, polygonRings));
cellQueue.push(new Cell(cell.p.x + h, cell.p.y + h, h, polygonRings));
numProbes += 4;
}
if (debug) {
console.log(`num probes: ${numProbes}`);
console.log(`best distance: ${bestCell.d}`);
}
return bestCell.p;
}
function compareMax(a, b) {
return b.max - a.max;
}
class Cell {
constructor(x, y, h, polygon) {
this.p = new Point(x, y);
this.h = h;
this.d = pointToPolygonDist(this.p, polygon);
this.max = this.d + this.h * Math.SQRT2;
}
}
function pointToPolygonDist(p, polygon) {
let inside = false;
let minDistSq = Infinity;
for (let k = 0; k < polygon.length; k++) {
const ring = polygon[k];
for (let i = 0, len = ring.length, j = len - 1; i < len; j = i++) {
const a = ring[i];
const b = ring[j];
if (a.y > p.y !== b.y > p.y && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x) inside = !inside;
minDistSq = Math.min(minDistSq, distToSegmentSquared(p, a, b));
}
}
return (inside ? 1 : -1) * Math.sqrt(minDistSq);
}
function getCentroidCell(polygon) {
let area = 0;
let x = 0;
let y = 0;
const points = polygon[0];
for (let i = 0, len = points.length, j = len - 1; i < len; j = i++) {
const a = points[i];
const b = points[j];
const f = a.x * b.y - b.x * a.y;
x += (a.x + b.x) * f;
y += (a.y + b.y) * f;
area += f * 3;
}
return new Cell(x / area, y / area, 0, polygon);
}
const keys = Object.keys;
const baselineOffset = 7;
const INVALID_TEXT_OFFSET = Number.POSITIVE_INFINITY;
const sqrt2 = Math.sqrt(2);
const SymbolBucketConstants = {
// this constant is based on the size of StructArray indexes used in a symbol
// bucket--namely, glyphOffsetArrayStart
// eg the max valid UInt16 is 65,535
// See https://github.com/mapbox/mapbox-gl-js/issues/2907 for motivation
// lineStartIndex and textBoxStartIndex could potentially be concerns
// but we expect there to be many fewer boxes/lines than glyphs
MAX_GLYPHS: 65535
};
function scaleBoundingBox(bbox, scale) {
bbox.top *= scale;
bbox.bottom *= scale;
bbox.left *= scale;
bbox.right *= scale;
bbox.scaled = true;
}
function rotateBoundingBox(bbox, rotateDegrees) {
if (!rotateDegrees) return;
const rotateRadians = degToRad(rotateDegrees);
const tl = new Point(bbox.left, bbox.top);
const tr = new Point(bbox.right, bbox.top);
const bl = new Point(bbox.left, bbox.bottom);
const br = new Point(bbox.right, bbox.bottom);
const center = new Point(0, 0);
tl._rotateAround(rotateRadians, center);
tr._rotateAround(rotateRadians, center);
bl._rotateAround(rotateRadians, center);
br._rotateAround(rotateRadians, center);
bbox.left = Math.min(tl.x, tr.x, bl.x, br.x);
bbox.right = Math.max(tl.x, tr.x, bl.x, br.x);
bbox.top = Math.min(tl.y, tr.y, bl.y, br.y);
bbox.bottom = Math.max(tl.y, tr.y, bl.y, br.y);
}
function getUpdateSymbolBoundingBox(shaped, existingBBox, rotate, scale, textOffset) {
const collisionPadding = isPositionedIcon(shaped) && shaped.collisionPadding ? shaped.collisionPadding : [0, 0, 0, 0];
const top = shaped.top - collisionPadding[1];
const bottom = shaped.bottom + collisionPadding[3];
const left = shaped.left - collisionPadding[0];
const right = shaped.right + collisionPadding[2];
const newBBox = {
top,
bottom,
left,
right,
scaled: false
};
if (scale !== void 0) {
scaleBoundingBox(newBBox, scale);
}
if (rotate) {
rotateBoundingBox(newBBox, rotate);
}
if (textOffset) {
newBBox.left += textOffset[0];
newBBox.right += textOffset[0];
newBBox.top += textOffset[1];
newBBox.bottom += textOffset[1];
}
if (!existingBBox) {
return newBBox;
} else {
return {
top: Math.min(existingBBox.top, newBBox.top),
bottom: Math.max(existingBBox.bottom, newBBox.bottom),
left: Math.min(existingBBox.left, newBBox.left),
right: Math.max(existingBBox.right, newBBox.right),
scaled: existingBBox.scaled || newBBox.scaled
};
}
}
function evaluateVariableOffset(anchor, [offsetX, offsetY]) {
let x = 0, y = 0;
if (offsetY === INVALID_TEXT_OFFSET) {
if (offsetX < 0) offsetX = 0;
const hypotenuse = offsetX / sqrt2;
switch (anchor) {
case "top-right":
case "top-left":
y = hypotenuse - baselineOffset;
break;
case "bottom-right":
case "bottom-left":
y = -hypotenuse + baselineOffset;
break;
case "bottom":
y = -offsetX + baselineOffset;
break;
case "top":
y = offsetX - baselineOffset;
break;
}
switch (anchor) {
case "top-right":
case "bottom-right":
x = -hypotenuse;
break;
case "top-left":
case "bottom-left":
x = hypotenuse;
break;
case "left":
x = offsetX;
break;
case "right":
x = -offsetX;
break;
}
} else {
offsetX = Math.abs(offsetX);
offsetY = Math.abs(offsetY);
switch (anchor) {
case "top-right":
case "top-left":
case "top":
y = offsetY - baselineOffset;
break;
case "bottom-right":
case "bottom-left":
case "bottom":
y = -offsetY + baselineOffset;
break;
}
switch (anchor) {
case "top-right":
case "bottom-right":
case "right":
x = -offsetX;
break;
case "top-left":
case "bottom-left":
case "left":
x = offsetX;
break;
}
}
return [x, y];
}
function performSymbolLayout(bucket, glyphMap, glyphPositions, imageMap, imagePositions, canonical, tileZoom, scaleFactor = 1, pixelRatio, imageRasterizationTasks, worldview) {
bucket.createArrays();
const tileSize = 512 * bucket.overscaling;
bucket.tilePixelRatio = EXTENT / tileSize;
bucket.compareText = {};
bucket.iconsNeedLinear = false;
const layout = bucket.layers[0].layout;
const unevaluatedLayoutValues = bucket.layers[0]._unevaluatedLayout._values;
const sizes = {};
sizes.scaleFactor = scaleFactor;
sizes.textSizeScaleRange = layout.get("text-size-scale-range");
sizes.iconSizeScaleRange = layout.get("icon-size-scale-range");
const [textSizeScaleRangeMin, textSizeScaleRangeMax] = sizes.textSizeScaleRange;
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = sizes.iconSizeScaleRange;
sizes.textScaleFactor = clamp(sizes.scaleFactor, textSizeScaleRangeMin, textSizeScaleRangeMax);
sizes.iconScaleFactor = clamp(sizes.scaleFactor, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
const unevaluatedTextSize = unevaluatedLayoutValues["text-size"];
const unevaluatedIconSize = unevaluatedLayoutValues["icon-size"];
if (bucket.textSizeData.kind === "composite") {
const { minZoom, maxZoom } = bucket.textSizeData;
sizes.compositeTextSizes = [
unevaluatedTextSize.possiblyEvaluate(new EvaluationParameters(minZoom, { worldview }), canonical),
unevaluatedTextSize.possiblyEvaluate(new EvaluationParameters(maxZoom, { worldview }), canonical)
];
}
if (bucket.iconSizeData.kind === "composite") {
const { minZoom, maxZoom } = bucket.iconSizeData;
sizes.compositeIconSizes = [
unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(minZoom, { worldview }), canonical),
unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(maxZoom, { worldview }), canonical)
];
}
sizes.layoutTextSize = unevaluatedTextSize.possiblyEvaluate(new EvaluationParameters(tileZoom + 1, { worldview }), canonical);
sizes.layoutIconSize = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(tileZoom + 1, { worldview }), canonical);
sizes.textMaxSize = unevaluatedTextSize.possiblyEvaluate(new EvaluationParameters(18, { worldview }), canonical);
const symbolPlacement = layout.get("symbol-placement");
const textAlongLine = layout.get("text-rotation-alignment") === "map" && symbolPlacement !== "point";
const textSize = layout.get("text-size");
let hasAnySecondaryIcon = false;
const featureData = [];
for (const feature of bucket.features) {
const fontstack = layout.get("text-font").evaluate(feature, {}, canonical).join(",");
const layoutTextSizeThisZoom = textSize.evaluate(feature, {}, canonical) * sizes.textScaleFactor;
const layoutTextSize = sizes.layoutTextSize.evaluate(feature, {}, canonical) * sizes.textScaleFactor;
const layoutIconSize = sizes.layoutIconSize.evaluate(feature, {}, canonical) * sizes.iconScaleFactor;
const shapedTextOrientations = {
horizontal: {},
vertical: void 0
};
const text = feature.text;
let textOffset = [0, 0];
if (text) {
const unformattedText = text.toString();
const spacing = layout.get("text-letter-spacing").evaluate(feature, {}, canonical) * ONE_EM;
const lineHeight = layout.get("text-line-height").evaluate(feature, {}, canonical) * ONE_EM;
const spacingIfAllowed = allowsLetterSpacing(unformattedText) ? spacing : 0;
const textAnchor = layout.get("text-anchor").evaluate(feature, {}, canonical);
const variableTextAnchor = layout.get("text-variable-anchor");
if (!variableTextAnchor) {
const radialOffset = layout.get("text-radial-offset").evaluate(feature, {}, canonical);
if (radialOffset) {
textOffset = evaluateVariableOffset(textAnchor, [radialOffset * ONE_EM, INVALID_TEXT_OFFSET]);
} else {
const evaluatedTextOffset = layout.get("text-offset").evaluate(feature, {}, canonical);
textOffset = [evaluatedTextOffset[0] * ONE_EM, evaluatedTextOffset[1] * ONE_EM];
}
}
let textJustify = textAlongLine ? "center" : layout.get("text-justify").evaluate(feature, {}, canonical);
const isPointPlacement = symbolPlacement === "point";
const maxWidth = isPointPlacement ? layout.get("text-max-width").evaluate(feature, {}, canonical) * ONE_EM : Infinity;
const addVerticalShapingIfNeeded = (textJustify2) => {
if (bucket.allowVerticalPlacement && allowsVerticalWritingMode(unformattedText)) {
shapedTextOrientations.vertical = shapeText(
text,
glyphMap,
glyphPositions,
imagePositions,
fontstack,
maxWidth,
lineHeight,
textAnchor,
textJustify2,
spacingIfAllowed,
textOffset,
WritingMode.vertical,
true,
layoutTextSize,
layoutTextSizeThisZoom,
pixelRatio
);
}
};
if (!textAlongLine && variableTextAnchor) {
const justifications = textJustify === "auto" ? variableTextAnchor.map((a) => getAnchorJustification(a)) : [textJustify];
let singleLine = false;
for (let i = 0; i < justifications.length; i++) {
const justification = justifications[i];
if (shapedTextOrientations.horizontal[justification]) continue;
if (singleLine) {
shapedTextOrientations.horizontal[justification] = shapedTextOrientations.horizontal[0];
} else {
const shaping = shapeText(
text,
glyphMap,
glyphPositions,
imagePositions,
fontstack,
maxWidth,
lineHeight,
"center",
justification,
spacingIfAllowed,
textOffset,
WritingMode.horizontal,
false,
layoutTextSize,
layoutTextSizeThisZoom,
pixelRatio
);
if (shaping) {
shapedTextOrientations.horizontal[justification] = shaping;
singleLine = shaping.positionedLines.length === 1;
}
}
}
addVerticalShapingIfNeeded("left");
} else {
if (textJustify === "auto") {
textJustify = getAnchorJustification(textAnchor);
}
if (isPointPlacement || (layout.get("text-writing-mode").indexOf("horizontal") >= 0 || !allowsVerticalWritingMode(unformattedText))) {
const shaping = shapeText(
text,
glyphMap,
glyphPositions,
imagePositions,
fontstack,
maxWidth,
lineHeight,
textAnchor,
textJustify,
spacingIfAllowed,
textOffset,
WritingMode.horizontal,
false,
layoutTextSize,
layoutTextSizeThisZoom,
pixelRatio
);
if (shaping) shapedTextOrientations.horizontal[textJustify] = shaping;
}
addVerticalShapingIfNeeded(isPointPlacement ? "left" : textJustify);
}
}
let shapedIcon;
let isSDFIcon = false;
let iconPrimary;
let primaryImage;
let iconSecondary;
let secondaryImage;
let iconOffset;
let iconAnchor;
const iconTextFit = layout.get("icon-text-fit").evaluate(feature, {}, canonical);
if (feature.icon && feature.icon.hasPrimary()) {
const icons = getScaledImageVariant(feature.icon, bucket.iconSizeData, unevaluatedLayoutValues["icon-size"], canonical, bucket.zoom, feature, pixelRatio, sizes.iconScaleFactor, worldview);
iconPrimary = icons.iconPrimary;
iconSecondary = icons.iconSecondary;
const primaryImageSerialized = iconPrimary.toString();
primaryImage = imageMap.get(primaryImageSerialized);
if (primaryImage) {
iconOffset = layout.get("icon-offset").evaluate(feature, {}, canonical);
iconAnchor = layout.get("icon-anchor").evaluate(feature, {}, canonical);
shapedIcon = shapeIcon(
imagePositions.get(primaryImageSerialized),
iconSecondary ? imagePositions.get(iconSecondary.toString()) : void 0,
iconOffset,
iconAnchor
);
isSDFIcon = primaryImage.sdf;
if (bucket.sdfIcons === void 0) {
bucket.sdfIcons = primaryImage.sdf;
} else if (bucket.sdfIcons !== primaryImage.sdf) {
warnOnce("Style sheet warning: Cannot mix SDF and non-SDF icons in one buffer");
}
if (primaryImage.pixelRatio !== bucket.pixelRatio) {
bucket.iconsNeedLinear = true;
} else if (layout.get("icon-rotate").constantOr(1) !== 0) {
bucket.iconsNeedLinear = true;
}
}
if (iconSecondary) {
const secondaryImageSerialized = iconSecondary.toString();
secondaryImage = imageMap.get(secondaryImageSerialized);
}
}
hasAnySecondaryIcon = hasAnySecondaryIcon || !!(feature.icon && feature.icon.hasSecondary());
const shapedText = getDefaultHorizontalShaping(shapedTextOrientations.horizontal) || shapedTextOrientations.vertical;
if (!bucket.iconsInText) {
bucket.iconsInText = shapedText ? shapedText.iconsInText : false;
}
const glyphSize = ONE_EM, fontScale = layoutTextSize * sizes.textScaleFactor / glyphSize;
const { defaultShapedIcon, verticallyShapedIcon } = fitIconsToText(bucket, shapedIcon, layout, feature, canonical, shapedTextOrientations, fontScale, iconOffset, iconTextFit);
if (iconTextFit !== "none" && shapedIcon && (isFullyStretchableX(shapedIcon) || isFullyStretchableY(shapedIcon))) {
scaleShapedIconImage(defaultShapedIcon.imagePrimary, primaryImage, iconPrimary, shapedIcon, defaultShapedIcon, iconTextFit, imageRasterizationTasks, imageMap, imagePositions);
scaleShapedIconImage(defaultShapedIcon.imageSecondary, secondaryImage, iconSecondary, shapedIcon, defaultShapedIcon, iconTextFit, imageRasterizationTasks, imageMap, imagePositions);
if (verticallyShapedIcon) {
scaleShapedIconImage(verticallyShapedIcon.imagePrimary, primaryImage, iconPrimary, shapedIcon, verticallyShapedIcon, iconTextFit, imageRasterizationTasks, imageMap, imagePositions);
scaleShapedIconImage(verticallyShapedIcon.imageSecondary, secondaryImage, iconSecondary, shapedIcon, verticallyShapedIcon, iconTextFit, imageRasterizationTasks, imageMap, imagePositions);
}
}
shapedIcon = defaultShapedIcon;
const { iconBBox, iconVerticalBBox, textBBox, textVerticalBBox } = mergeAppearancesBboxes(
bucket,
shapedIcon,
verticallyShapedIcon,
layout,
feature,
canonical,
layoutIconSize,
iconOffset,
sizes,
imagePositions,
iconAnchor,
shapedTextOrientations,
layoutTextSize,
textOffset
);
featureData.push({
feature,
shapedTextOrientations,
shapedText,
shapedIcon,
iconPrimary,
iconSecondary,
iconOffset,
iconAnchor,
verticallyShapedIcon,
layoutTextSize,
layoutIconSize,
textOffset,
isSDFIcon,
iconTextFit,
iconCollisionBounds: iconBBox,
iconVerticalCollisionBounds: iconVerticalBBox,
textCollisionBounds: textBBox,
textVerticalCollisionBounds: textVerticalBBox
});
}
return { featureData, sizes, hasAnySecondaryIcon, textAlongLine, symbolPlacement };
}
function getLayoutProperties$6(layout, feature, canonical) {
const baseIconRotate = layout.get("icon-rotate").evaluate(feature, {}, canonical);
const baseTextRotate = layout.get("text-rotate").evaluate(feature, {}, canonical);
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const iconScaleFactor = clamp(1, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
return { baseIconRotate, baseTextRotate, iconScaleFactor };
}
function mergeAppearancesBboxes(bucket, shapedIcon, verticallyShapedIcon, layout, feature, canonical, layoutIconSize, iconOffset, sizes, imagePositions, iconAnchor, shapedTextOrientations, layoutTextSize, textOffset) {
const symbolLayer = bucket.layers[0];
const appearances = symbolLayer.appearances;
if (appearances.length === 0) {
return { iconBBox: null, iconVerticalBBox: null, textBBox: null, textVerticalBBox: null };
}
const iconBBoxes = {
iconBBox: null,
iconVerticalBBox: null
};
const textBBoxes = {
textBBox: null,
textVerticalBBox: null
};
const { baseIconRotate, baseTextRotate, iconScaleFactor } = getLayoutProperties$6(layout, feature, canonical);
if (shapedIcon) {
iconBBoxes.iconBBox = getUpdateSymbolBoundingBox(shapedIcon, iconBBoxes.iconBBox, baseIconRotate, layoutIconSize);
if (verticallyShapedIcon) {
const verticalIconRotate = baseIconRotate + 90;
iconBBoxes.iconVerticalBBox = getUpdateSymbolBoundingBox(verticallyShapedIcon, iconBBoxes.iconVerticalBBox, verticalIconRotate, layoutIconSize);
}
}
const defaultHorizontalShaping = getDefaultHorizontalShaping(shapedTextOrientations.horizontal);
if (defaultHorizontalShaping) {
textBBoxes.textBBox = getUpdateSymbolBoundingBox(defaultHorizontalShaping, textBBoxes.textBBox, baseTextRotate, 1, textOffset);
}
if (shapedTextOrientations.vertical) {
const verticalTextRotate = baseTextRotate + 90;
textBBoxes.textVerticalBBox = getUpdateSymbolBoundingBox(shapedTextOrientations.vertical, textBBoxes.textVerticalBBox, verticalTextRotate, 1, textOffset);
}
for (const appearance of appearances) {
if (appearance.hasIconProperties()) {
updateIconBoundingBoxes(
iconBBoxes,
bucket,
symbolLayer,
appearance,
feature,
canonical,
iconOffset,
baseIconRotate,
layoutIconSize,
sizes,
shapedIcon,
imagePositions,
iconScaleFactor,
iconAnchor
);
}
if (appearance.hasTextProperties()) {
updateTextBoundingBoxes(
textBBoxes,
symbolLayer,
appearance,
feature,
canonical,
textOffset,
baseTextRotate,
layoutTextSize,
defaultHorizontalShaping,
shapedTextOrientations.vertical
);
}
}
return { iconBBox: iconBBoxes.iconBBox, iconVerticalBBox: iconBBoxes.iconVerticalBBox, textBBox: textBBoxes.textBBox, textVerticalBBox: textBBoxes.textVerticalBBox };
}
function updateIconBoundingBoxes(input, bucket, symbolLayer, appearance, feature, canonical, iconOffset, baseIconRotate, layoutIconSize, sizes, shapedIcon, imagePositions, iconScaleFactor, iconAnchor) {
const { appearanceIconOffset, appearanceIconRotate, appearanceIconSize } = getAppearanceIconValues(appearance, symbolLayer, feature, canonical, iconOffset, baseIconRotate, layoutIconSize, sizes);
let appearanceShapedIcon = null;
let appearanceVerticallyShapedIcon = null;
let imagePositionToUse = null;
if (appearance.hasProperty("icon-image")) {
imagePositionToUse = getAppearanceImagePosition(bucket, symbolLayer, appearance, feature, canonical, imagePositions, iconScaleFactor);
} else if (shapedIcon) {
imagePositionToUse = shapedIcon.imagePrimary;
}
if (imagePositionToUse) {
appearanceShapedIcon = shapeIcon(imagePositionToUse, null, appearanceIconOffset, iconAnchor);
if (bucket.allowVerticalPlacement) {
appearanceVerticallyShapedIcon = shapeIcon(imagePositionToUse, null, appearanceIconOffset, iconAnchor);
}
}
if (appearanceShapedIcon) {
input.iconBBox = getUpdateSymbolBoundingBox(appearanceShapedIcon, input.iconBBox, appearanceIconRotate, appearanceIconSize);
}
if (appearanceVerticallyShapedIcon) {
const verticalAppearanceIconRotate = appearanceIconRotate + 90;
input.iconVerticalBBox = getUpdateSymbolBoundingBox(appearanceVerticallyShapedIcon, input.iconVerticalBBox, verticalAppearanceIconRotate, appearanceIconSize);
}
}
function getAppearanceIconValues(appearance, symbolLayer, feature, canonical, iconOffset, baseIconRotate, layoutIconSize, sizes) {
const appearanceIconOffsetValue = appearance.hasProperty("icon-offset") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-offset", feature, canonical, []) : null;
const appearanceIconOffset = appearanceIconOffsetValue && Array.isArray(appearanceIconOffsetValue) ? appearanceIconOffsetValue : iconOffset;
const appearanceIconRotateValue = appearance.hasProperty("icon-rotate") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-rotate", feature, canonical, []) : null;
const appearanceIconRotate = typeof appearanceIconRotateValue === "number" ? appearanceIconRotateValue : baseIconRotate;
const appearanceIconSizeValue = appearance.hasProperty("icon-size") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-size", feature, canonical, []) : null;
const appearanceIconSize = typeof appearanceIconSizeValue === "number" ? appearanceIconSizeValue * sizes.iconScaleFactor : layoutIconSize;
return { appearanceIconOffset, appearanceIconRotate, appearanceIconSize };
}
function getAppearanceImagePosition(bucket, symbolLayer, appearance, feature, canonical, imagePositions, iconScaleFactor) {
let imagePositionToUse = null;
const appearanceIconImage = symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-image", feature, canonical, []);
if (appearanceIconImage) {
const icon = bucket.getResolvedImageFromTokens(appearanceIconImage);
const unevaluatedIconSize = appearance.getUnevaluatedProperty("icon-size");
const iconSizeData = getSizeData(bucket.zoom, unevaluatedIconSize, bucket.worldview);
const imageVariant = getScaledImageVariant(icon, iconSizeData, unevaluatedIconSize, canonical, bucket.zoom, feature, bucket.pixelRatio, iconScaleFactor, bucket.worldview);
imagePositionToUse = imagePositions.get(imageVariant.iconPrimary.toString());
}
return imagePositionToUse;
}
function updateTextBoundingBoxes(input, symbolLayer, appearance, feature, canonical, textOffset, baseTextRotate, layoutTextSize, defaultHorizontalShaping, defaultVerticalShaping) {
const { appearanceTextOffset, appearanceTextRotate, appearanceTextSize } = getAppearanceTextValues(appearance, symbolLayer, feature, canonical, textOffset, baseTextRotate, layoutTextSize);
const textSizeScale = appearanceTextSize / layoutTextSize;
if (defaultHorizontalShaping) {
input.textBBox = getUpdateSymbolBoundingBox(defaultHorizontalShaping, input.textBBox, appearanceTextRotate, textSizeScale, appearanceTextOffset);
}
if (defaultVerticalShaping) {
const verticalAppearanceTextRotate = appearanceTextRotate + 90;
input.textVerticalBBox = getUpdateSymbolBoundingBox(defaultVerticalShaping, input.textVerticalBBox, verticalAppearanceTextRotate, textSizeScale, appearanceTextOffset);
}
}
function getAppearanceTextValues(appearance, symbolLayer, feature, canonical, textOffset, baseTextRotate, layoutTextSize) {
const appearanceTextOffsetValue = appearance.hasProperty("text-offset") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "text-offset", feature, canonical, []) : null;
const appearanceTextOffset = appearanceTextOffsetValue && Array.isArray(appearanceTextOffsetValue) ? [appearanceTextOffsetValue[0] * ONE_EM, appearanceTextOffsetValue[1] * ONE_EM] : textOffset;
const appearanceTextRotateValue = appearance.hasProperty("text-rotate") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "text-rotate", feature, canonical, []) : null;
const appearanceTextRotate = typeof appearanceTextRotateValue === "number" ? appearanceTextRotateValue : baseTextRotate;
const appearanceTextSizeValue = appearance.hasProperty("text-size") ? symbolLayer.getAppearanceValueAndResolveTokens(appearance, "text-size", feature, canonical, []) : null;
const appearanceTextSize = typeof appearanceTextSizeValue === "number" ? appearanceTextSizeValue : layoutTextSize;
return { appearanceTextOffset, appearanceTextRotate, appearanceTextSize };
}
function scaleShapedIconImage(outImagePosition, image, variant, beforeFitIcon, afterFitIcon, iconTextFit, imageRasterizationTasks, imageMap, imagePositions) {
if (!image || !image.usvg) return;
const beforeFitIconSize = getPositionedIconSize(beforeFitIcon);
const afterFitIconSize = getPositionedIconSize(afterFitIcon);
const widthMultiplier = (iconTextFit === "both" || iconTextFit === "width") && isFullyStretchableX(beforeFitIcon) ? afterFitIconSize.width / beforeFitIconSize.width : 1;
const heightMultiplier = (iconTextFit === "both" || iconTextFit === "height") && isFullyStretchableY(beforeFitIcon) ? afterFitIconSize.height / beforeFitIconSize.height : 1;
variant.scaleSelf(widthMultiplier, heightMultiplier);
const scaledIconId = variant.toString();
imageRasterizationTasks.set(scaledIconId, variant);
imageMap.set(scaledIconId, image);
const { imagePosition } = getImagePosition(scaledIconId, image, ICON_PADDING);
imagePositions.set(scaledIconId, imagePosition);
outImagePosition = imagePosition;
}
function scaleImageVariant(image, iconSizeData, iconSize, tileID, zoom, feature, pixelRatio, iconScaleFactor, worldview) {
if (!image) return void 0;
const iconSizeFactor = getRasterizedIconSize(iconSizeData, iconSize, tileID, zoom, feature, worldview);
const scaleFactor = iconSizeFactor * iconScaleFactor * pixelRatio;
return image.scaleSelf(scaleFactor);
}
function getScaledImageVariant(icon, iconSizeData, iconSize, tileID, zoom, feature, pixelRatio, iconScaleFactor, worldview) {
const iconPrimary = scaleImageVariant(icon.getPrimary(), iconSizeData, iconSize, tileID, zoom, feature, pixelRatio, iconScaleFactor, worldview);
const iconSecondary = scaleImageVariant(icon.getSecondary(), iconSizeData, iconSize, tileID, zoom, feature, pixelRatio, iconScaleFactor, worldview);
return { iconPrimary, iconSecondary };
}
function checkCrossFadeImagePositions(primary, secondary, iconPositions) {
if (!secondary) return;
const primaryPosition = iconPositions.get(primary.toString());
const secondaryPosition = iconPositions.get(secondary.toString());
if (!primaryPosition || !secondaryPosition) {
return;
}
if (primaryPosition.paddedRect.w !== secondaryPosition.paddedRect.w || primaryPosition.paddedRect.h !== secondaryPosition.paddedRect.h) {
warnOnce(`Mismatch in icon variant sizes: ${primary.toString()} and ${secondary.toString()}`);
}
if (primaryPosition.usvg !== secondaryPosition.usvg) {
warnOnce(`Mismatch in icon variant image types: ${primary.id} and ${secondary.id}`);
}
}
function postRasterizationSymbolLayout(bucket, bucketData, showCollisionBoxes, availableImages, canonical, tileZoom, projection, brightness, imageMap, imageAtlas) {
bucket.iconAtlasPositions = imageAtlas.iconPositions;
const { featureData, hasAnySecondaryIcon, sizes, textAlongLine, symbolPlacement } = bucketData;
for (const data of featureData) {
const {
shapedIcon,
verticallyShapedIcon,
feature,
shapedTextOrientations,
shapedText,
layoutTextSize,
layoutIconSize,
textOffset,
isSDFIcon,
iconPrimary,
iconSecondary,
iconTextFit,
iconOffset,
iconCollisionBounds,
iconVerticalCollisionBounds,
textCollisionBounds,
textVerticalCollisionBounds
} = data;
reconcileImagePosition(shapedIcon, imageAtlas.iconPositions, iconPrimary, iconSecondary);
reconcileImagePosition(verticallyShapedIcon, imageAtlas.iconPositions, iconPrimary, iconSecondary);
reconcileTextImagePositions(shapedTextOrientations, imageAtlas.iconPositions);
checkCrossFadeImagePositions(iconPrimary, iconSecondary, imageAtlas.iconPositions);
if (shapedText || shapedIcon) {
addFeature(
bucket,
feature,
shapedTextOrientations,
shapedIcon,
verticallyShapedIcon,
imageMap,
sizes,
layoutTextSize,
layoutIconSize,
textOffset,
isSDFIcon,
availableImages,
canonical,
projection,
brightness,
hasAnySecondaryIcon,
iconTextFit,
iconOffset,
textAlongLine,
symbolPlacement,
iconCollisionBounds,
iconVerticalCollisionBounds,
textCollisionBounds,
textVerticalCollisionBounds
);
}
}
if (showCollisionBoxes) {
bucket.generateCollisionDebugBuffers(tileZoom, bucket.collisionBoxArray, sizes.textScaleFactor);
}
}
function reconcileImagePosition(shapedIcon, atlasIconPositions, iconPrimary, iconSecondary) {
if (!shapedIcon) return;
const primaryImagePosition = atlasIconPositions.get(iconPrimary.toString());
shapedIcon.imagePrimary = primaryImagePosition;
if (iconSecondary) {
const secondaryImagePosition = atlasIconPositions.get(iconSecondary.toString());
shapedIcon.imageSecondary = secondaryImagePosition;
}
}
function reconcileTextImagePositions(shapedTextOrientations, atlasIconPositions) {
for (const orientation in shapedTextOrientations.horizontal) {
reconcileTextOrientationImagePositions(shapedTextOrientations.horizontal[orientation], atlasIconPositions);
}
reconcileTextOrientationImagePositions(shapedTextOrientations.vertical, atlasIconPositions);
}
function reconcileTextOrientationImagePositions(shapedText, atlasIconPositions) {
if (!shapedText) return;
for (const line of shapedText.positionedLines) {
for (const glyph of line.positionedGlyphs) {
if (glyph.image !== null) {
const imageId = glyph.image.toString();
glyph.rect = atlasIconPositions.get(imageId).paddedRect;
}
}
}
}
function getAnchorJustification(anchor) {
switch (anchor) {
case "right":
case "top-right":
case "bottom-right":
return "right";
case "left":
case "top-left":
case "bottom-left":
return "left";
}
return "center";
}
function tilePixelRatioForSymbolSpacing(overscaleFactor, overscaledZ) {
if (overscaledZ > 18 && overscaleFactor > 2) {
overscaleFactor >>= 1;
}
const tilePixelRatio = EXTENT / (512 * overscaleFactor);
return Math.max(tilePixelRatio, 1);
}
function fitIconsToText(bucket, shapedIcon, layout, feature, canonical, shapedTextOrientations, fontScale, iconOffset, iconTextFit) {
const defaultShaping = getDefaultHorizontalShaping(shapedTextOrientations.horizontal) || shapedTextOrientations.vertical;
const iconTextFitPadding = layout.get("icon-text-fit-padding").evaluate(feature, {}, canonical);
const hasIconTextFit = iconTextFit !== "none";
let defaultShapedIcon = shapedIcon;
let verticallyShapedIcon;
if (shapedIcon && hasIconTextFit) {
if (bucket.allowVerticalPlacement && shapedTextOrientations.vertical) {
verticallyShapedIcon = fitIconToText(
shapedIcon,
shapedTextOrientations.vertical,
iconTextFit,
iconTextFitPadding,
iconOffset,
fontScale
);
}
if (defaultShaping) {
defaultShapedIcon = fitIconToText(
shapedIcon,
defaultShaping,
iconTextFit,
iconTextFitPadding,
iconOffset,
fontScale
);
}
}
return { defaultShapedIcon, verticallyShapedIcon };
}
function addFeature(bucket, feature, shapedTextOrientations, shapedIcon, verticallyShapedIcon, imageMap, sizes, layoutTextSize, layoutIconSize, textOffset, isSDFIcon, availableImages, canonical, projection, brightness, hasAnySecondaryIcon, iconTextFit, iconOffset, textAlongLine, symbolPlacement, iconCollisionBounds, iconVerticalCollisionBounds, textCollisionBounds, textVerticalCollisionBounds) {
let textMaxSize = sizes.textMaxSize.evaluate(feature, {}, canonical);
if (textMaxSize === void 0) {
textMaxSize = layoutTextSize * sizes.textScaleFactor;
} else {
textMaxSize *= sizes.textScaleFactor;
}
const layout = bucket.layers[0].layout;
const glyphSize = ONE_EM;
const fontScale = layoutTextSize * sizes.textScaleFactor / glyphSize;
const defaultShaping = getDefaultHorizontalShaping(shapedTextOrientations.horizontal) || shapedTextOrientations.vertical;
if (iconTextFit !== "none" && bucket.appearanceFeatureData && feature.index < bucket.appearanceFeatureData.length) {
const featureData = bucket.appearanceFeatureData[feature.index];
if (featureData) {
featureData.textShaping = defaultShaping;
featureData.iconTextFitPadding = layout.get("icon-text-fit-padding").evaluate(feature, {}, canonical);
featureData.fontScale = fontScale;
}
}
const isGlobe = projection.name === "globe";
const textMaxBoxScale = bucket.tilePixelRatio * textMaxSize / glyphSize, iconBoxScale = bucket.tilePixelRatio * layoutIconSize, symbolMinDistance = tilePixelRatioForSymbolSpacing(bucket.overscaling, bucket.zoom) * layout.get("symbol-spacing"), textPadding = layout.get("text-padding") * bucket.tilePixelRatio, iconPadding = layout.get("icon-padding") * bucket.tilePixelRatio, textMaxAngle = degToRad(layout.get("text-max-angle")), iconAlongLine = layout.get("icon-rotation-alignment") === "map" && symbolPlacement !== "point", textRepeatDistance = symbolMinDistance / 2;
const hasIconTextFit = iconTextFit !== "none";
if (bucket.hasAnyIconTextFit === false && hasIconTextFit) {
bucket.hasAnyIconTextFit = true;
}
const elevationFeatureId = feature.properties ? +feature.properties[PROPERTY_ELEVATION_ID] : null;
const elevationFeatureIndex = elevationFeatureId && bucket.elevationFeatureIdToIndex ? bucket.elevationFeatureIdToIndex.get(elevationFeatureId) : 65535;
const addSymbolAtAnchor = (line, anchor, canonicalId) => {
if (anchor.x < 0 || anchor.x >= EXTENT || anchor.y < 0 || anchor.y >= EXTENT) {
return;
}
let globe = null;
if (isGlobe) {
const { x, y, z } = projection.projectTilePoint(anchor.x, anchor.y, canonicalId);
globe = {
anchor: new Anchor(x, y, z, 0, void 0),
up: projection.upVector(canonicalId, anchor.x, anchor.y)
};
}
addSymbol(
bucket,
anchor,
globe,
line,
shapedTextOrientations,
shapedIcon,
imageMap,
verticallyShapedIcon,
bucket.layers[0],
bucket.collisionBoxArray,
feature.index,
feature.sourceLayerIndex,
bucket.index,
textPadding,
textAlongLine,
textOffset,
iconBoxScale,
iconPadding,
iconAlongLine,
iconOffset,
feature,
sizes,
isSDFIcon,
availableImages,
canonical,
brightness,
hasAnySecondaryIcon,
iconTextFit,
elevationFeatureIndex,
iconCollisionBounds,
iconVerticalCollisionBounds,
textCollisionBounds,
textVerticalCollisionBounds
);
};
if (symbolPlacement === "line") {
for (const line of clipLines(feature.geometry, 0, 0, EXTENT, EXTENT)) {
const anchors = getAnchors(
line,
symbolMinDistance,
textMaxAngle,
shapedTextOrientations.vertical || defaultShaping,
shapedIcon,
glyphSize,
textMaxBoxScale,
bucket.overscaling,
EXTENT
);
for (const anchor of anchors) {
const shapedText = defaultShaping;
if (!shapedText || !anchorIsTooClose(bucket, shapedText.text, textRepeatDistance, anchor)) {
addSymbolAtAnchor(line, anchor, canonical);
}
}
}
} else if (symbolPlacement === "line-center") {
for (const line of feature.geometry) {
if (line.length > 1) {
const anchor = getCenterAnchor(
line,
textMaxAngle,
shapedTextOrientations.vertical || defaultShaping,
shapedIcon,
glyphSize,
textMaxBoxScale
);
if (anchor) {
addSymbolAtAnchor(line, anchor, canonical);
}
}
}
} else if (feature.type === "Polygon") {
for (const polygon of classifyRings(feature.geometry, 0)) {
const poi = findPoleOfInaccessibility(polygon, 16);
addSymbolAtAnchor(polygon[0], new Anchor(poi.x, poi.y, 0, 0, void 0), canonical);
}
} else if (feature.type === "LineString") {
for (const line of feature.geometry) {
addSymbolAtAnchor(line, new Anchor(line[0].x, line[0].y, 0, 0, void 0), canonical);
}
} else if (feature.type === "Point") {
for (const points of feature.geometry) {
for (const point of points) {
addSymbolAtAnchor([point], new Anchor(point.x, point.y, 0, 0, void 0), canonical);
}
}
}
}
const MAX_GLYPH_ICON_SIZE = 255;
const MAX_PACKED_SIZE = MAX_GLYPH_ICON_SIZE * SIZE_PACK_FACTOR;
function addTextVertices(bucket, globe, tileAnchor, shapedText, imageMap, layer, textAlongLine, feature, textOffset, lineArray, writingMode, placementTypes, placedTextSymbolIndices, placedIconIndex, sizes, availableImages, canonical, symbolInstanceIndex, brightness) {
const glyphQuads = getGlyphQuads(
tileAnchor,
shapedText,
textOffset,
layer,
textAlongLine,
feature,
imageMap,
bucket.allowVerticalPlacement
);
const sizeData = bucket.textSizeData;
let textSizeData = null;
if (sizeData.kind === "source") {
textSizeData = [
SIZE_PACK_FACTOR * layer.layout.get("text-size").evaluate(feature, {}, canonical) * sizes.textScaleFactor
];
if (textSizeData[0] > MAX_PACKED_SIZE) {
warnOnce(`${bucket.layerIds[0]}: Value for "text-size" is >= ${MAX_GLYPH_ICON_SIZE}. Reduce your "text-size".`);
}
} else if (sizeData.kind === "composite") {
textSizeData = [
SIZE_PACK_FACTOR * sizes.compositeTextSizes[0].evaluate(feature, {}, canonical) * sizes.textScaleFactor,
SIZE_PACK_FACTOR * sizes.compositeTextSizes[1].evaluate(feature, {}, canonical) * sizes.textScaleFactor
];
if (textSizeData[0] > MAX_PACKED_SIZE || textSizeData[1] > MAX_PACKED_SIZE) {
warnOnce(`${bucket.layerIds[0]}: Value for "text-size" is >= ${MAX_GLYPH_ICON_SIZE}. Reduce your "text-size".`);
}
}
bucket.addSymbols(
bucket.text,
glyphQuads,
textSizeData,
textOffset,
textAlongLine,
feature,
writingMode,
globe,
tileAnchor,
lineArray.lineStartIndex,
lineArray.lineLength,
placedIconIndex,
availableImages,
canonical,
brightness,
false,
symbolInstanceIndex,
glyphQuads.length
);
for (const placementType of placementTypes) {
placedTextSymbolIndices[placementType] = bucket.text.placedSymbolArray.length - 1;
}
return glyphQuads.length * 4;
}
function getDefaultHorizontalShaping(horizontalShaping) {
for (const justification in horizontalShaping) {
return horizontalShaping[justification];
}
return null;
}
function evaluateBoxCollisionFeature(collisionBoxArray, projectedAnchor, tileAnchor, featureIndex, sourceLayerIndex, bucketIndex, shaped, padding, rotate, textOffset, collisionBounds) {
let y1, y2, x1, x2;
y1 = collisionBounds ? collisionBounds.top : shaped.top;
y2 = collisionBounds ? collisionBounds.bottom : shaped.bottom;
x1 = collisionBounds ? collisionBounds.left : shaped.left;
x2 = collisionBounds ? collisionBounds.right : shaped.right;
if (isPositionedIcon(shaped) && shaped.collisionPadding) {
const collisionPadding = shaped.collisionPadding;
x1 -= collisionPadding[0];
y1 -= collisionPadding[1];
x2 += collisionPadding[2];
y2 += collisionPadding[3];
}
if (rotate) {
const tl = new Point(x1, y1);
const tr = new Point(x2, y1);
const bl = new Point(x1, y2);
const br = new Point(x2, y2);
const rotateRadians = degToRad(rotate);
let rotateCenter = new Point(0, 0);
if (textOffset) {
rotateCenter = new Point(textOffset[0], textOffset[1]);
}
tl._rotateAround(rotateRadians, rotateCenter);
tr._rotateAround(rotateRadians, rotateCenter);
bl._rotateAround(rotateRadians, rotateCenter);
br._rotateAround(rotateRadians, rotateCenter);
x1 = Math.min(tl.x, tr.x, bl.x, br.x);
x2 = Math.max(tl.x, tr.x, bl.x, br.x);
y1 = Math.min(tl.y, tr.y, bl.y, br.y);
y2 = Math.max(tl.y, tr.y, bl.y, br.y);
}
collisionBoxArray.emplaceBack(projectedAnchor.x, projectedAnchor.y, projectedAnchor.z, tileAnchor.x, tileAnchor.y, x1, y1, x2, y2, padding, featureIndex, sourceLayerIndex, bucketIndex);
return collisionBoxArray.length - 1;
}
function evaluateCircleCollisionFeature(shaped) {
if (isPositionedIcon(shaped) && shaped.collisionPadding) {
shaped.top -= shaped.collisionPadding[1];
shaped.bottom += shaped.collisionPadding[3];
}
const height = shaped.bottom - shaped.top;
return height > 0 ? Math.max(10, height) : null;
}
function addSymbol(bucket, anchor, globe, line, shapedTextOrientations, shapedIcon, imageMap, verticallyShapedIcon, layer, collisionBoxArray, featureIndex, sourceLayerIndex, bucketIndex, textPadding, textAlongLine, textOffset, iconBoxScale, iconPadding, iconAlongLine, iconOffset, feature, sizes, isSDFIcon, availableImages, canonical, brightness, hasAnySecondaryIcon, iconTextFit, elevationFeatureIndex, iconCollisionBounds, iconVerticalCollisionBounds, textCollisionBounds, textVerticalCollisionBounds) {
const lineArray = bucket.addToLineVertexArray(anchor, line);
let textBoxIndex, iconBoxIndex, verticalTextBoxIndex, verticalIconBoxIndex;
let textCircle, verticalTextCircle, verticalIconCircle;
let numIconVertices = 0;
let numVerticalIconVertices = 0;
let numHorizontalGlyphVertices = 0;
let numVerticalGlyphVertices = 0;
let placedIconSymbolIndex = -1;
let verticalPlacedIconSymbolIndex = -1;
const placedTextSymbolIndices = {};
let key = murmur3("");
const collisionFeatureAnchor = globe ? globe.anchor : anchor;
const hasIconTextFit = iconTextFit !== "none";
let textOffset0 = 0;
let textOffset1 = 0;
if (layer._unevaluatedLayout.getValue("text-radial-offset") === void 0) {
const evaluatedTextOffset = layer.layout.get("text-offset").evaluate(feature, {}, canonical);
textOffset0 = evaluatedTextOffset[0] * ONE_EM;
textOffset1 = evaluatedTextOffset[1] * ONE_EM;
} else {
textOffset0 = layer.layout.get("text-radial-offset").evaluate(feature, {}, canonical) * ONE_EM;
textOffset1 = INVALID_TEXT_OFFSET;
}
if (bucket.allowVerticalPlacement && shapedTextOrientations.vertical) {
const verticalShaping = shapedTextOrientations.vertical;
if (textAlongLine) {
verticalTextCircle = evaluateCircleCollisionFeature(verticalShaping);
if (verticallyShapedIcon) {
verticalIconCircle = evaluateCircleCollisionFeature(verticallyShapedIcon);
}
} else {
const textRotation = layer.layout.get("text-rotate").evaluate(feature, {}, canonical);
const verticalTextRotation = textRotation + 90;
verticalTextBoxIndex = evaluateBoxCollisionFeature(collisionBoxArray, collisionFeatureAnchor, anchor, featureIndex, sourceLayerIndex, bucketIndex, verticalShaping, textPadding, verticalTextRotation, textOffset, textCollisionBounds);
if (verticallyShapedIcon) {
verticalIconBoxIndex = evaluateBoxCollisionFeature(collisionBoxArray, collisionFeatureAnchor, anchor, featureIndex, sourceLayerIndex, bucketIndex, verticallyShapedIcon, iconPadding, verticalTextRotation, null, iconVerticalCollisionBounds);
}
}
}
if (shapedIcon) {
const sizeData = bucket.iconSizeData;
const iconRotate = layer.layout.get("icon-rotate").evaluate(feature, {}, canonical);
const iconQuads = getIconQuads(shapedIcon, iconRotate, isSDFIcon, hasIconTextFit, sizes.iconScaleFactor);
const verticalIconQuads = verticallyShapedIcon ? getIconQuads(verticallyShapedIcon, iconRotate, isSDFIcon, hasIconTextFit, sizes.iconScaleFactor) : void 0;
iconBoxIndex = evaluateBoxCollisionFeature(collisionBoxArray, collisionFeatureAnchor, anchor, featureIndex, sourceLayerIndex, bucketIndex, shapedIcon, iconPadding, iconRotate, null, iconCollisionBounds);
const maxQuadCount = calculateMaxIconQuadCount(
bucket,
iconQuads,
verticalIconQuads,
layer.layout,
feature,
canonical,
bucket.iconAtlasPositions,
hasIconTextFit
);
numIconVertices = maxQuadCount * 4;
let iconSizeData = null;
if (sizeData.kind === "source") {
iconSizeData = [
SIZE_PACK_FACTOR * layer.layout.get("icon-size").evaluate(feature, {}, canonical) * sizes.iconScaleFactor
];
if (iconSizeData[0] > MAX_PACKED_SIZE) {
warnOnce(`${bucket.layerIds[0]}: Value for "icon-size" is >= ${MAX_GLYPH_ICON_SIZE}. Reduce your "icon-size".`);
}
} else if (sizeData.kind === "composite") {
iconSizeData = [
SIZE_PACK_FACTOR * sizes.compositeIconSizes[0].evaluate(feature, {}, canonical) * sizes.iconScaleFactor,
SIZE_PACK_FACTOR * sizes.compositeIconSizes[1].evaluate(feature, {}, canonical) * sizes.iconScaleFactor
];
if (iconSizeData[0] > MAX_PACKED_SIZE || iconSizeData[1] > MAX_PACKED_SIZE) {
warnOnce(`${bucket.layerIds[0]}: Value for "icon-size" is >= ${MAX_GLYPH_ICON_SIZE}. Reduce your "icon-size".`);
}
}
bucket.addSymbols(
bucket.icon,
iconQuads,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
iconSizeData,
iconOffset,
iconAlongLine,
feature,
void 0,
globe,
anchor,
lineArray.lineStartIndex,
lineArray.lineLength,
// The icon itself does not have an associated symbol since the text isnt placed yet
-1,
availableImages,
canonical,
brightness,
hasAnySecondaryIcon,
bucket.symbolInstances.length,
maxQuadCount
);
placedIconSymbolIndex = bucket.icon.placedSymbolArray.length - 1;
if (verticalIconQuads) {
numVerticalIconVertices = maxQuadCount * 4;
bucket.addSymbols(
bucket.icon,
verticalIconQuads,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
iconSizeData,
iconOffset,
iconAlongLine,
feature,
WritingMode.vertical,
globe,
anchor,
lineArray.lineStartIndex,
lineArray.lineLength,
// The icon itself does not have an associated symbol since the text isnt placed yet
-1,
availableImages,
canonical,
brightness,
hasAnySecondaryIcon,
bucket.symbolInstances.length,
maxQuadCount
);
verticalPlacedIconSymbolIndex = bucket.icon.placedSymbolArray.length - 1;
}
}
for (const placement in shapedTextOrientations.horizontal) {
const justification = placement;
const shaping = shapedTextOrientations.horizontal[justification];
if (!textBoxIndex) {
key = murmur3(shaping.text);
if (textAlongLine) {
textCircle = evaluateCircleCollisionFeature(shaping);
} else {
const textRotate = layer.layout.get("text-rotate").evaluate(feature, {}, canonical);
textBoxIndex = evaluateBoxCollisionFeature(collisionBoxArray, collisionFeatureAnchor, anchor, featureIndex, sourceLayerIndex, bucketIndex, shaping, textPadding, textRotate, textOffset, textCollisionBounds);
}
}
const singleLine = shaping.positionedLines.length === 1;
numHorizontalGlyphVertices += addTextVertices(
bucket,
globe,
anchor,
shaping,
imageMap,
layer,
textAlongLine,
feature,
textOffset,
lineArray,
shapedTextOrientations.vertical ? WritingMode.horizontal : WritingMode.horizontalOnly,
singleLine ? keys(shapedTextOrientations.horizontal) : [justification],
placedTextSymbolIndices,
placedIconSymbolIndex,
sizes,
availableImages,
canonical,
bucket.symbolInstances.length,
brightness
);
if (singleLine) {
break;
}
}
if (shapedTextOrientations.vertical) {
numVerticalGlyphVertices += addTextVertices(
bucket,
globe,
anchor,
shapedTextOrientations.vertical,
imageMap,
layer,
textAlongLine,
feature,
textOffset,
lineArray,
WritingMode.vertical,
["vertical"],
placedTextSymbolIndices,
verticalPlacedIconSymbolIndex,
sizes,
availableImages,
canonical,
bucket.symbolInstances.length,
brightness
);
}
let collisionCircleDiameter = -1;
const getCollisionCircleHeight = (diameter, prevHeight) => {
return diameter ? Math.max(diameter, prevHeight) : prevHeight;
};
collisionCircleDiameter = getCollisionCircleHeight(textCircle, collisionCircleDiameter);
collisionCircleDiameter = getCollisionCircleHeight(verticalTextCircle, collisionCircleDiameter);
collisionCircleDiameter = getCollisionCircleHeight(verticalIconCircle, collisionCircleDiameter);
const useRuntimeCollisionCircles = collisionCircleDiameter > -1 ? 1 : 0;
if (bucket.glyphOffsetArray.length >= SymbolBucketConstants.MAX_GLYPHS) warnOnce(
"Too many glyphs being rendered in a tile. See https://github.com/mapbox/mapbox-gl-js/issues/2907"
);
if (feature.sortKey !== void 0) {
bucket.addToSortKeyRanges(bucket.symbolInstances.length, feature.sortKey);
}
const projectedAnchor = collisionFeatureAnchor;
bucket.symbolInstances.emplaceBack(
anchor.x,
anchor.y,
projectedAnchor.x,
projectedAnchor.y,
projectedAnchor.z,
placedTextSymbolIndices.right >= 0 ? placedTextSymbolIndices.right : -1,
placedTextSymbolIndices.center >= 0 ? placedTextSymbolIndices.center : -1,
placedTextSymbolIndices.left >= 0 ? placedTextSymbolIndices.left : -1,
placedTextSymbolIndices.vertical >= 0 ? placedTextSymbolIndices.vertical : -1,
placedIconSymbolIndex,
verticalPlacedIconSymbolIndex,
key,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
textBoxIndex !== void 0 ? textBoxIndex : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
textBoxIndex !== void 0 ? textBoxIndex + 1 : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
verticalTextBoxIndex !== void 0 ? verticalTextBoxIndex : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
verticalTextBoxIndex !== void 0 ? verticalTextBoxIndex + 1 : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
iconBoxIndex !== void 0 ? iconBoxIndex : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
iconBoxIndex !== void 0 ? iconBoxIndex + 1 : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
verticalIconBoxIndex ? verticalIconBoxIndex : bucket.collisionBoxArray.length,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
verticalIconBoxIndex ? verticalIconBoxIndex + 1 : bucket.collisionBoxArray.length,
featureIndex,
numHorizontalGlyphVertices,
numVerticalGlyphVertices,
numIconVertices,
numVerticalIconVertices,
useRuntimeCollisionCircles,
0,
textOffset0,
textOffset1,
collisionCircleDiameter,
0,
hasIconTextFit ? 1 : 0,
elevationFeatureIndex
);
}
function anchorIsTooClose(bucket, text, repeatDistance, anchor) {
const compareText = bucket.compareText;
if (!(text in compareText)) {
compareText[text] = [];
} else {
const otherAnchors = compareText[text];
for (let k = otherAnchors.length - 1; k >= 0; k--) {
if (anchor.dist(otherAnchors[k]) < repeatDistance) {
return true;
}
}
}
compareText[text].push(anchor);
return false;
}
function calculateMaxIconQuadCount(bucket, iconQuads, verticalIconQuads, layout, feature, canonical, imagePositions, hasIconTextFit) {
const symbolLayer = bucket.layers[0];
const appearances = symbolLayer.appearances;
let maxQuadCount = iconQuads.length;
if (verticalIconQuads) {
maxQuadCount = Math.max(maxQuadCount, verticalIconQuads.length);
}
if (appearances.length === 0) {
return maxQuadCount;
}
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const iconScaleFactor = clamp(1, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
for (const appearance of appearances) {
const unevaluatedProperties = appearance.getUnevaluatedProperties();
const iconImageProperty = unevaluatedProperties._values["icon-image"].value !== void 0;
if (iconImageProperty) {
const appearanceIconImage = symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-image", feature, canonical, []);
if (appearanceIconImage) {
const icon = bucket.getResolvedImageFromTokens(appearanceIconImage);
if (icon) {
const unevaluatedIconSize = unevaluatedProperties._values["icon-size"];
const iconSizeData = getSizeData(bucket.zoom, unevaluatedIconSize, bucket.worldview);
const imageVariant = getScaledImageVariant(icon, iconSizeData, unevaluatedIconSize, canonical, bucket.zoom, feature, bucket.pixelRatio, iconScaleFactor, bucket.worldview);
const imagePosition = imagePositions.get(imageVariant.iconPrimary.toString());
maxQuadCount = Math.max(maxQuadCount, getIconQuadsNumber(imagePosition, hasIconTextFit));
}
}
}
}
return maxQuadCount;
}
function farthestPixelDistanceOnPlane(tr, pixelsPerMeter) {
const fovAboveCenter = tr.fovAboveCenter;
const minElevationInPixels = tr.elevation ? tr.elevation.getMinElevationBelowMSL() * pixelsPerMeter : 0;
const cameraToSeaLevelDistance = (tr._camera.position[2] * tr.worldSize - minElevationInPixels) / Math.cos(tr._pitch);
const topHalfSurfaceDistance = Math.sin(fovAboveCenter) * cameraToSeaLevelDistance / Math.sin(Math.max(Math.PI / 2 - tr._pitch - fovAboveCenter, 0.01));
let furthestDistance = Math.sin(tr._pitch) * topHalfSurfaceDistance + cameraToSeaLevelDistance;
const horizonDistance = cameraToSeaLevelDistance * (1 / tr._horizonShift);
if (!tr.elevation || tr.elevation.exaggeration() === 0) {
let factor = Math.max(tr.zoom - 17, 0);
if (tr.isOrthographic) {
factor /= 10;
}
furthestDistance *= 1 + factor;
}
return Math.min(furthestDistance * 1.01, horizonDistance);
}
function farthestPixelDistanceOnSphere(tr, pixelsPerMeter) {
const cameraDistance = tr.cameraToCenterDistance;
const centerPixelAltitude = tr._centerAltitude * pixelsPerMeter;
const camera = tr._camera;
const forward = tr._camera.forward();
const cameraPosition = add$4([], scale$4([], forward, -cameraDistance), [0, 0, centerPixelAltitude]);
const globeRadius = tr.worldSize / (2 * Math.PI);
const globeCenter = [0, 0, -globeRadius];
const aspectRatio = tr.width / tr.height;
const tanFovAboveCenter = Math.tan(tr.fovAboveCenter);
const up = scale$4([], camera.up(), tanFovAboveCenter);
const right = scale$4([], camera.right(), tanFovAboveCenter * aspectRatio);
const dir = normalize$4([], add$4([], add$4([], forward, up), right));
const pointOnGlobe = [];
const ray = new Ray(cameraPosition, dir);
let pixelDistance;
if (ray.closestPointOnSphere(globeCenter, globeRadius, pointOnGlobe)) {
const p0 = add$4([], pointOnGlobe, globeCenter);
const p1 = sub$2([], p0, cameraPosition);
pixelDistance = Math.cos(tr.fovAboveCenter) * length$4(p1);
} else {
const globeCenterToCamera = sub$2([], cameraPosition, globeCenter);
const cameraToGlobe = sub$2([], globeCenter, cameraPosition);
normalize$4(cameraToGlobe, cameraToGlobe);
const cameraHeight = length$4(globeCenterToCamera) - globeRadius;
pixelDistance = Math.sqrt(cameraHeight * (cameraHeight + 2 * globeRadius));
const angle = Math.acos(pixelDistance / (globeRadius + cameraHeight)) - Math.acos(dot$5(forward, cameraToGlobe));
pixelDistance *= Math.cos(angle);
}
return pixelDistance * 1.01;
}
function tileTransform(id, projection) {
if (!projection.isReprojectedInTileSpace) {
return { scale: 1 << id.z, x: id.x, y: id.y, x2: id.x + 1, y2: id.y + 1, projection };
}
const s = Math.pow(2, -id.z);
const x1 = id.x * s;
const x2 = (id.x + 1) * s;
const y1 = id.y * s;
const y2 = (id.y + 1) * s;
const lng1 = lngFromMercatorX(x1);
const lng2 = lngFromMercatorX(x2);
const lat1 = latFromMercatorY(y1);
const lat2 = latFromMercatorY(y2);
const p0 = projection.project(lng1, lat1);
const p1 = projection.project(lng2, lat1);
const p2 = projection.project(lng2, lat2);
const p3 = projection.project(lng1, lat2);
let minX = Math.min(p0.x, p1.x, p2.x, p3.x);
let minY = Math.min(p0.y, p1.y, p2.y, p3.y);
let maxX = Math.max(p0.x, p1.x, p2.x, p3.x);
let maxY = Math.max(p0.y, p1.y, p2.y, p3.y);
const maxErr = s / 16;
function processSegment(pa, pb, ax, ay, bx, by) {
const mx = (ax + bx) / 2;
const my = (ay + by) / 2;
const pm = projection.project(lngFromMercatorX(mx), latFromMercatorY(my));
const err = Math.max(0, minX - pm.x, minY - pm.y, pm.x - maxX, pm.y - maxY);
minX = Math.min(minX, pm.x);
maxX = Math.max(maxX, pm.x);
minY = Math.min(minY, pm.y);
maxY = Math.max(maxY, pm.y);
if (err > maxErr) {
processSegment(pa, pm, ax, ay, mx, my);
processSegment(pm, pb, mx, my, bx, by);
}
}
processSegment(p0, p1, x1, y1, x2, y1);
processSegment(p1, p2, x2, y1, x2, y2);
processSegment(p2, p3, x2, y2, x1, y2);
processSegment(p3, p0, x1, y2, x1, y1);
minX -= maxErr;
minY -= maxErr;
maxX += maxErr;
maxY += maxErr;
const max = Math.max(maxX - minX, maxY - minY);
const scale = 1 / max;
return {
scale,
x: minX * scale,
y: minY * scale,
x2: maxX * scale,
y2: maxY * scale,
projection
};
}
function tileAABB(tr, numTiles, z, x, y, wrap, min, max, projection) {
if (projection.name === "globe") {
const tileId = new CanonicalTileID(z, x, y);
return aabbForTileOnGlobe(tr, numTiles, tileId, false);
}
const tt = tileTransform({ z, x, y }, projection);
const tx = tt.x / tt.scale;
const ty = tt.y / tt.scale;
const tx2 = tt.x2 / tt.scale;
const ty2 = tt.y2 / tt.scale;
if (isNaN(tx) || isNaN(tx2) || isNaN(ty) || isNaN(ty2)) {
assert$1(false);
}
return new Aabb(
[(wrap + tx) * numTiles, numTiles * ty, min],
[(wrap + tx2) * numTiles, numTiles * ty2, max]
);
}
function getTilePoint(tileTransform2, {
x,
y
}, wrap = 0) {
return new Point(
((x - wrap) * tileTransform2.scale - tileTransform2.x) * EXTENT,
(y * tileTransform2.scale - tileTransform2.y) * EXTENT
);
}
function getTileVec3(tileTransform2, coord, wrap = 0) {
const x = ((coord.x - wrap) * tileTransform2.scale - tileTransform2.x) * EXTENT;
const y = (coord.y * tileTransform2.scale - tileTransform2.y) * EXTENT;
return fromValues$4(x, y, altitudeFromMercatorZ(coord.z, coord.y));
}
const identity = identity$3(new Float32Array(16));
class Projection {
constructor(options) {
this.spec = options;
this.name = options.name;
this.wrap = false;
this.requiresDraping = false;
this.supportsWorldCopies = false;
this.supportsTerrain = false;
this.supportsFog = false;
this.supportsFreeCamera = false;
this.zAxisUnit = "meters";
this.isReprojectedInTileSpace = true;
this.unsupportedLayers = ["custom"];
this.center = [0, 0];
this.range = [3.5, 7];
}
project(lng, lat) {
return { x: 0, y: 0, z: 0 };
}
unproject(x, y) {
return new LngLat(0, 0);
}
projectTilePoint(x, y, _) {
return { x, y, z: 0 };
}
locationPoint(tr, lngLat, altitude, terrain = true) {
return tr._coordinatePoint(tr.locationCoordinate(lngLat, altitude), terrain);
}
pixelsPerMeter(lat, worldSize) {
return mercatorZfromAltitude(1, lat) * worldSize;
}
// pixels-per-meter is used to describe relation between real world and pixel distances.
// `pixelSpaceConversion` can be used to convert the ratio from mercator projection to
// the currently active projection.
//
// `pixelSpaceConversion` is useful for converting between pixel spaces where some logic
// expects mercator pixels, such as raycasting where the scale is expected to be in
// mercator pixels.
pixelSpaceConversion(lat, worldSize, interpolationT) {
return 1;
}
farthestPixelDistance(tr) {
return farthestPixelDistanceOnPlane(tr, tr.pixelsPerMeter);
}
pointCoordinate(tr, x, y, z) {
const horizonOffset = tr.horizonLineFromTop(false);
const clamped = new Point(x, Math.max(horizonOffset, y));
return tr.rayIntersectionCoordinate(tr.pointRayIntersection(clamped, z));
}
pointCoordinate3D(tr, x, y) {
const p = new Point(x, y);
if (tr.elevation) {
return tr.elevation.pointCoordinate(p);
} else {
const mc = this.pointCoordinate(tr, p.x, p.y, 0);
return [mc.x, mc.y, mc.z];
}
}
isPointAboveHorizon(tr, p) {
if (tr.elevation && tr.elevation.visibleDemTiles.length) {
const raycastOnTerrain = this.pointCoordinate3D(tr, p.x, p.y);
return !raycastOnTerrain;
}
const horizon = tr.horizonLineFromTop();
return p.y < horizon;
}
createInversionMatrix(tr, id) {
return identity;
}
createTileMatrix(tr, worldSize, id) {
let scale, scaledX, scaledY;
const canonical = id.canonical;
const posMatrix = identity$3(new Float64Array(16));
if (this.isReprojectedInTileSpace) {
const cs = tileTransform(canonical, this);
scale = 1;
scaledX = cs.x + id.wrap * cs.scale;
scaledY = cs.y;
scale$5(posMatrix, posMatrix, [scale / cs.scale, scale / cs.scale, tr.pixelsPerMeter / worldSize]);
} else {
scale = worldSize / tr.zoomScale(canonical.z);
const unwrappedX = canonical.x + Math.pow(2, canonical.z) * id.wrap;
scaledX = unwrappedX * scale;
scaledY = canonical.y * scale;
}
translate$2(posMatrix, posMatrix, [scaledX, scaledY, 0]);
scale$5(posMatrix, posMatrix, [scale / EXTENT, scale / EXTENT, 1]);
return posMatrix;
}
upVector(id, x, y) {
return [0, 0, 1];
}
upVectorScale(id, latitude, worldSize) {
return { metersToTile: 1 };
}
}
class Albers extends Projection {
constructor(options) {
super(options);
this.range = [4, 7];
this.center = options.center || [-96, 37.5];
const [lat0, lat1] = this.parallels = options.parallels || [29.5, 45.5];
const sy0 = Math.sin(degToRad(lat0));
this.n = (sy0 + Math.sin(degToRad(lat1))) / 2;
this.c = 1 + sy0 * (2 * this.n - sy0);
this.r0 = Math.sqrt(this.c) / this.n;
}
project(lng, lat) {
const { n, c, r0 } = this;
const lambda = degToRad(lng - this.center[0]);
const phi = degToRad(lat);
const r = Math.sqrt(c - 2 * n * Math.sin(phi)) / n;
const x = r * Math.sin(lambda * n);
const y = r * Math.cos(lambda * n) - r0;
return { x, y, z: 0 };
}
unproject(x, y) {
const { n, c, r0 } = this;
const r0y = r0 + y;
let l = Math.atan2(x, Math.abs(r0y)) * Math.sign(r0y);
if (r0y * n < 0) {
l -= Math.PI * Math.sign(x) * Math.sign(r0y);
}
const dt = degToRad(this.center[0]) * n;
l = wrap$1(l, -Math.PI - dt, Math.PI - dt);
const lng = clamp(radToDeg(l / n) + this.center[0], -180, 180);
const phi = Math.asin(clamp((c - (x * x + r0y * r0y) * n * n) / (2 * n), -1, 1));
const lat = clamp(radToDeg(phi), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, lat);
}
}
const a1 = 1.340264;
const a2 = -0.081106;
const a3 = 893e-6;
const a4 = 3796e-6;
const M = Math.sqrt(3) / 2;
class EqualEarth extends Projection {
project(lng, lat) {
lat = lat / 180 * Math.PI;
lng = lng / 180 * Math.PI;
const theta = Math.asin(M * Math.sin(lat));
const theta2 = theta * theta;
const theta6 = theta2 * theta2 * theta2;
const x = lng * Math.cos(theta) / (M * (a1 + 3 * a2 * theta2 + theta6 * (7 * a3 + 9 * a4 * theta2)));
const y = theta * (a1 + a2 * theta2 + theta6 * (a3 + a4 * theta2));
return {
x: (x / Math.PI + 0.5) * 0.5,
y: 1 - (y / Math.PI + 1) * 0.5,
z: 0
};
}
unproject(x, y) {
x = (2 * x - 0.5) * Math.PI;
y = (2 * (1 - y) - 1) * Math.PI;
let theta = y;
let theta2 = theta * theta;
let theta6 = theta2 * theta2 * theta2;
for (let i = 0, delta, fy, fpy; i < 12; ++i) {
fy = theta * (a1 + a2 * theta2 + theta6 * (a3 + a4 * theta2)) - y;
fpy = a1 + 3 * a2 * theta2 + theta6 * (7 * a3 + 9 * a4 * theta2);
delta = fy / fpy;
theta = clamp(theta - delta, -Math.PI / 3, Math.PI / 3);
theta2 = theta * theta;
theta6 = theta2 * theta2 * theta2;
if (Math.abs(delta) < 1e-12) break;
}
const lambda = M * x * (a1 + 3 * a2 * theta2 + theta6 * (7 * a3 + 9 * a4 * theta2)) / Math.cos(theta);
const phi = Math.asin(Math.sin(theta) / M);
const lng = clamp(lambda * 180 / Math.PI, -180, 180);
const lat = clamp(phi * 180 / Math.PI, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, lat);
}
}
class Equirectangular extends Projection {
constructor(options) {
super(options);
this.wrap = true;
this.supportsWorldCopies = true;
}
project(lng, lat) {
const x = 0.5 + lng / 360;
const y = 0.5 - lat / 360;
return { x, y, z: 0 };
}
unproject(x, y) {
const lng = (x - 0.5) * 360;
const lat = clamp((0.5 - y) * 360, -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, lat);
}
}
const halfPi = Math.PI / 2;
function tany(y) {
return Math.tan((halfPi + y) / 2);
}
class LambertConformalConic extends Projection {
constructor(options) {
super(options);
this.center = options.center || [0, 30];
const [lat0, lat1] = this.parallels = options.parallels || [30, 30];
let y0 = degToRad(lat0);
let y1 = degToRad(lat1);
this.southernCenter = y0 + y1 < 0;
if (this.southernCenter) {
y0 = -y0;
y1 = -y1;
}
const cy0 = Math.cos(y0);
const tany0 = tany(y0);
this.n = y0 === y1 ? Math.sin(y0) : Math.log(cy0 / Math.cos(y1)) / Math.log(tany(y1) / tany0);
this.f = cy0 * Math.pow(tany(y0), this.n) / this.n;
}
project(lng, lat) {
lat = degToRad(lat);
if (this.southernCenter) lat = -lat;
lng = degToRad(lng - this.center[0]);
const epsilon = 1e-6;
const { n, f } = this;
if (f > 0) {
if (lat < -halfPi + epsilon) lat = -halfPi + epsilon;
} else {
if (lat > halfPi - epsilon) lat = halfPi - epsilon;
}
const r = f / Math.pow(tany(lat), n);
let x = r * Math.sin(n * lng);
let y = f - r * Math.cos(n * lng);
x = (x / Math.PI + 0.5) * 0.5;
y = (y / Math.PI + 0.5) * 0.5;
return {
x,
y: this.southernCenter ? y : 1 - y,
z: 0
};
}
unproject(x, y) {
x = (2 * x - 0.5) * Math.PI;
if (this.southernCenter) y = 1 - y;
y = (2 * (1 - y) - 0.5) * Math.PI;
const { n, f } = this;
const fy = f - y;
const signFy = Math.sign(fy);
const r = Math.sign(n) * Math.sqrt(x * x + fy * fy);
let l = Math.atan2(x, Math.abs(fy)) * signFy;
if (fy * n < 0) l -= Math.PI * Math.sign(x) * signFy;
const lng = clamp(radToDeg(l / n) + this.center[0], -180, 180);
const phi = 2 * Math.atan(Math.pow(f / r, 1 / n)) - halfPi;
const lat = clamp(radToDeg(phi), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, this.southernCenter ? -lat : lat);
}
}
class Mercator extends Projection {
constructor(options) {
super(options);
this.wrap = true;
this.supportsWorldCopies = true;
this.supportsTerrain = true;
this.supportsFog = true;
this.supportsFreeCamera = true;
this.isReprojectedInTileSpace = false;
this.unsupportedLayers = [];
this.range = null;
}
project(lng, lat) {
const x = mercatorXfromLng(lng);
const y = mercatorYfromLat(lat);
return { x, y, z: 0 };
}
unproject(x, y) {
const lng = lngFromMercatorX(x);
const lat = latFromMercatorY(y);
return new LngLat(lng, lat);
}
}
const maxPhi$1 = degToRad(MAX_MERCATOR_LATITUDE);
class NaturalEarth extends Projection {
project(lng, lat) {
lat = degToRad(lat);
lng = degToRad(lng);
const phi2 = lat * lat;
const phi4 = phi2 * phi2;
const x = lng * (0.8707 - 0.131979 * phi2 + phi4 * (-0.013791 + phi4 * (3971e-6 * phi2 - 1529e-6 * phi4)));
const y = lat * (1.007226 + phi2 * (0.015085 + phi4 * (-0.044475 + 0.028874 * phi2 - 5916e-6 * phi4)));
return {
x: (x / Math.PI + 0.5) * 0.5,
y: 1 - (y / Math.PI + 1) * 0.5,
z: 0
};
}
unproject(x, y) {
x = (2 * x - 0.5) * Math.PI;
y = (2 * (1 - y) - 1) * Math.PI;
const epsilon = 1e-6;
let phi = y;
let i = 25;
let delta = 0;
let phi2 = phi * phi;
do {
phi2 = phi * phi;
const phi4 = phi2 * phi2;
delta = (phi * (1.007226 + phi2 * (0.015085 + phi4 * (-0.044475 + 0.028874 * phi2 - 5916e-6 * phi4))) - y) / (1.007226 + phi2 * (0.015085 * 3 + phi4 * (-0.044475 * 7 + 0.028874 * 9 * phi2 - 5916e-6 * 11 * phi4)));
phi = clamp(phi - delta, -maxPhi$1, maxPhi$1);
} while (Math.abs(delta) > epsilon && --i > 0);
phi2 = phi * phi;
const lambda = x / (0.8707 + phi2 * (-0.131979 + phi2 * (-0.013791 + phi2 * phi2 * phi2 * (3971e-6 - 1529e-6 * phi2))));
const lng = clamp(radToDeg(lambda), -180, 180);
const lat = radToDeg(phi);
return new LngLat(lng, lat);
}
}
const maxPhi = degToRad(MAX_MERCATOR_LATITUDE);
class WinkelTripel extends Projection {
project(lng, lat) {
lat = degToRad(lat);
lng = degToRad(lng);
const cosLat = Math.cos(lat);
const twoOverPi = 2 / Math.PI;
const alpha = Math.acos(cosLat * Math.cos(lng / 2));
const sinAlphaOverAlpha = Math.sin(alpha) / alpha;
const x = 0.5 * (lng * twoOverPi + 2 * cosLat * Math.sin(lng / 2) / sinAlphaOverAlpha) || 0;
const y = 0.5 * (lat + Math.sin(lat) / sinAlphaOverAlpha) || 0;
return {
x: (x / Math.PI + 0.5) * 0.5,
y: 1 - (y / Math.PI + 1) * 0.5,
z: 0
};
}
unproject(x, y) {
x = (2 * x - 0.5) * Math.PI;
y = (2 * (1 - y) - 1) * Math.PI;
let lambda = x;
let phi = y;
let i = 25;
const epsilon = 1e-6;
let dlambda = 0, dphi = 0;
do {
const cosphi = Math.cos(phi), sinphi = Math.sin(phi), sinphi2 = 2 * sinphi * cosphi, sin2phi = sinphi * sinphi, cos2phi = cosphi * cosphi, coslambda2 = Math.cos(lambda / 2), sinlambda2 = Math.sin(lambda / 2), sinlambda = 2 * coslambda2 * sinlambda2, sin2lambda2 = sinlambda2 * sinlambda2, C = 1 - cos2phi * coslambda2 * coslambda2, F = C ? 1 / C : 0, E = C ? Math.acos(cosphi * coslambda2) * Math.sqrt(1 / C) : 0, fx = 0.5 * (2 * E * cosphi * sinlambda2 + lambda * 2 / Math.PI) - x, fy = 0.5 * (E * sinphi + phi) - y, dxdlambda = 0.5 * F * (cos2phi * sin2lambda2 + E * cosphi * coslambda2 * sin2phi) + 1 / Math.PI, dxdphi = F * (sinlambda * sinphi2 / 4 - E * sinphi * sinlambda2), dydlambda = 0.125 * F * (sinphi2 * sinlambda2 - E * sinphi * cos2phi * sinlambda), dydphi = 0.5 * F * (sin2phi * coslambda2 + E * sin2lambda2 * cosphi) + 0.5, denominator = dxdphi * dydlambda - dydphi * dxdlambda;
dlambda = (fy * dxdphi - fx * dydphi) / denominator;
dphi = (fx * dydlambda - fy * dxdlambda) / denominator;
lambda = clamp(lambda - dlambda, -Math.PI, Math.PI);
phi = clamp(phi - dphi, -maxPhi, maxPhi);
} while ((Math.abs(dlambda) > epsilon || Math.abs(dphi) > epsilon) && --i > 0);
return new LngLat(radToDeg(lambda), radToDeg(phi));
}
}
class CylindricalEqualArea extends Projection {
constructor(options) {
super(options);
this.center = options.center || [0, 0];
this.parallels = options.parallels || [0, 0];
this.cosPhi = Math.max(0.01, Math.cos(degToRad(this.parallels[0])));
this.scale = 1 / (2 * Math.max(Math.PI * this.cosPhi, 1 / this.cosPhi));
this.wrap = true;
this.supportsWorldCopies = true;
}
project(lng, lat) {
const { scale, cosPhi } = this;
const x = degToRad(lng) * cosPhi;
const y = Math.sin(degToRad(lat)) / cosPhi;
return {
x: x * scale + 0.5,
y: -y * scale + 0.5,
z: 0
};
}
unproject(x, y) {
const { scale, cosPhi } = this;
const x_ = (x - 0.5) / scale;
const y_ = -(y - 0.5) / scale;
const lng = clamp(radToDeg(x_) / cosPhi, -180, 180);
const y2 = y_ * cosPhi;
const y3 = Math.asin(clamp(y2, -1, 1));
const lat = clamp(radToDeg(y3), -MAX_MERCATOR_LATITUDE, MAX_MERCATOR_LATITUDE);
return new LngLat(lng, lat);
}
}
class Globe extends Mercator {
constructor(options) {
super(options);
this.requiresDraping = true;
this.supportsWorldCopies = false;
this.supportsFog = true;
this.zAxisUnit = "pixels";
this.unsupportedLayers = ["debug"];
this.range = [3, 5];
}
projectTilePoint(x, y, id) {
const pos = tileCoordToECEF(x, y, id);
const bounds = globeTileBounds(id);
const normalizationMatrix = globeNormalizeECEF(bounds);
transformMat4$2(pos, pos, normalizationMatrix);
return { x: pos[0], y: pos[1], z: pos[2] };
}
locationPoint(tr, lngLat, altitude) {
const pos = latLngToECEF(lngLat.lat, lngLat.lng);
const up = normalize$4([], pos);
const elevation = altitude ? tr._centerAltitude + altitude : tr.elevation ? tr.elevation.getAtPointOrZero(tr.locationCoordinate(lngLat), tr._centerAltitude) : tr._centerAltitude;
const upScale = mercatorZfromAltitude(1, 0) * EXTENT * elevation;
scaleAndAdd$2(pos, pos, up, upScale);
const matrix = identity$3(new Float64Array(16));
multiply$5(matrix, tr.pixelMatrix, tr.globeMatrix);
transformMat4$2(pos, pos, matrix);
return new Point(pos[0], pos[1]);
}
pixelsPerMeter(lat, worldSize) {
return mercatorZfromAltitude(1, 0) * worldSize;
}
pixelSpaceConversion(lat, worldSize, interpolationT) {
const centerScale = mercatorZfromAltitude(1, lat) * worldSize;
const referenceScale = mercatorZfromAltitude(1, GLOBE_SCALE_MATCH_LATITUDE) * worldSize;
const combinedScale = number(referenceScale, centerScale, interpolationT);
return this.pixelsPerMeter(lat, worldSize) / combinedScale;
}
createTileMatrix(tr, worldSize, id) {
const decode = globeDenormalizeECEF(globeTileBounds(id.canonical));
return multiply$5(new Float64Array(16), tr.globeMatrix, decode);
}
createInversionMatrix(tr, id) {
const { center } = tr;
const matrix = globeNormalizeECEF(globeTileBounds(id));
rotateY$3(matrix, matrix, degToRad(center.lng));
rotateX$3(matrix, matrix, degToRad(center.lat));
scale$5(matrix, matrix, [tr._pixelsPerMercatorPixel, tr._pixelsPerMercatorPixel, 1]);
return Float32Array.from(matrix);
}
pointCoordinate(tr, x, y, _) {
const coord = globePointCoordinate(tr, x, y, true);
if (!coord) {
return new MercatorCoordinate(0, 0);
}
return coord;
}
pointCoordinate3D(tr, x, y) {
const coord = this.pointCoordinate(tr, x, y, 0);
return [coord.x, coord.y, coord.z];
}
isPointAboveHorizon(tr, p) {
const raycastOnGlobe = globePointCoordinate(tr, p.x, p.y, false);
return !raycastOnGlobe;
}
farthestPixelDistance(tr) {
const pixelsPerMeter = this.pixelsPerMeter(tr.center.lat, tr.worldSize);
const globePixelDistance = farthestPixelDistanceOnSphere(tr, pixelsPerMeter);
const t = globeToMercatorTransition(tr.zoom);
if (t > 0) {
const mercatorPixelsPerMeter = mercatorZfromAltitude(1, tr.center.lat) * tr.worldSize;
const mercatorPixelDistance = farthestPixelDistanceOnPlane(tr, mercatorPixelsPerMeter);
const pixelRadius = tr.worldSize / (2 * Math.PI);
const approxTileArcHalfAngle = Math.max(tr.width, tr.height) / tr.worldSize * Math.PI;
const padding = pixelRadius * (1 - Math.cos(approxTileArcHalfAngle));
return number(globePixelDistance, mercatorPixelDistance + padding, Math.pow(t, 10));
}
return globePixelDistance;
}
upVector(id, x, y) {
return tileCoordToECEF(x, y, id, 1);
}
upVectorScale(id) {
return { metersToTile: globeMetersToEcef(globeECEFNormalizationScale(globeTileBounds(id))) };
}
}
function getProjection(config) {
const parallels = config.parallels;
const isDegenerateConic = parallels ? Math.abs(parallels[0] + parallels[1]) < 0.01 : false;
switch (config.name) {
case "mercator":
return new Mercator(config);
case "equirectangular":
return new Equirectangular(config);
case "naturalEarth":
return new NaturalEarth(config);
case "equalEarth":
return new EqualEarth(config);
case "winkelTripel":
return new WinkelTripel(config);
case "albers":
return isDegenerateConic ? new CylindricalEqualArea(config) : new Albers(config);
case "lambertConformalConic":
return isDegenerateConic ? new CylindricalEqualArea(config) : new LambertConformalConic(config);
case "globe":
return new Globe(config);
}
throw new Error(`Invalid projection name: ${config.name}`);
}
const vectorTileFeatureTypes = VectorTileFeature.types;
const shaderOpacityAttributes = [
{ name: "a_fade_opacity", components: 1, type: "Uint8", offset: 0 }
];
function addVertex(array, tileAnchorX, tileAnchorY, ox, oy, tx, ty, sizeVertex, isSDF, pixelOffsetX, pixelOffsetY, minFontScaleX, minFontScaleY) {
const aSizeX = sizeVertex ? Math.min(MAX_PACKED_SIZE, Math.round(sizeVertex[0])) : 0;
const aSizeY = sizeVertex ? Math.min(MAX_PACKED_SIZE, Math.round(sizeVertex[1])) : 0;
array.emplaceBack(
// a_pos_offset
tileAnchorX,
tileAnchorY,
Math.round(ox * 32),
Math.round(oy * 32),
// a_data
tx,
// x coordinate of symbol on atlas texture
ty,
// y coordinate of symbol on atlas texture
(aSizeX << 1) + (isSDF ? 1 : 0),
(aSizeY << 1) + 0,
// pack isAppearanceIcon flag in last bit, 0 for non-appearance icon
pixelOffsetX * 16,
pixelOffsetY * 16,
minFontScaleX * 256,
minFontScaleY * 256
);
}
function addTransitioningVertex(array, tx, ty) {
array.emplaceBack(tx, ty);
}
function addGlobeVertex(array, projAnchorX, projAnchorY, projAnchorZ, normX, normY, normZ) {
array.emplaceBack(
// a_globe_anchor
projAnchorX,
projAnchorY,
projAnchorZ,
// a_globe_normal
normX,
normY,
normZ
);
}
function updateGlobeVertexNormal(array, vertexIdx, normX, normY, normZ) {
const offset = vertexIdx * 5 + 2;
array.float32[offset + 0] = normX;
array.float32[offset + 1] = normY;
array.float32[offset + 2] = normZ;
}
const addOrientationVertex = (orientationArray, numVertices, orientedXAxis, orientedYAxis) => {
for (let i = 0; i < numVertices; i++) {
orientationArray.emplaceBack(orientedXAxis[0], orientedXAxis[1], orientedXAxis[2], orientedYAxis[0], orientedYAxis[1], orientedYAxis[2]);
}
};
function addDynamicAttributes(dynamicLayoutVertexArray, x, y, z, angle) {
dynamicLayoutVertexArray.emplaceBack(x, y, z, angle);
dynamicLayoutVertexArray.emplaceBack(x, y, z, angle);
dynamicLayoutVertexArray.emplaceBack(x, y, z, angle);
dynamicLayoutVertexArray.emplaceBack(x, y, z, angle);
}
function containsRTLText(formattedText) {
for (const section of formattedText.sections) {
if (stringContainsRTLText(section.text)) {
return true;
}
}
return false;
}
class SymbolBuffers {
constructor(programConfigurations) {
this.layoutVertexArray = new StructArrayLayout4i4ui4i24();
this.indexArray = new StructArrayLayout3ui6();
this.programConfigurations = programConfigurations;
this.segments = new SegmentVector();
this.dynamicLayoutVertexArray = new StructArrayLayout4f16();
this.opacityVertexArray = new StructArrayLayout1ul4();
this.placedSymbolArray = new PlacedSymbolArray();
this.iconTransitioningVertexArray = new StructArrayLayout2ui4();
this.globeExtVertexArray = new StructArrayLayout3i3f20();
this.zOffsetVertexArray = new StructArrayLayout1f4();
this.orientationVertexArray = new StructArrayLayout6f24();
this.symbolInstanceIndices = [];
}
isEmpty() {
return this.layoutVertexArray.length === 0 && this.indexArray.length === 0 && this.dynamicLayoutVertexArray.length === 0 && this.opacityVertexArray.length === 0 && this.iconTransitioningVertexArray.length === 0;
}
getIconVertexData(offset, numVertices) {
const data = [];
assert$1(offset >= 0 && offset < this.layoutVertexArray.length, "Invalid vertex offset");
const uint16Array = this.layoutVertexArray.uint16;
for (let i = 0; i < numVertices; ++i) {
const baseOffset = (offset + i) * 12;
data.push(...uint16Array.slice(baseOffset, baseOffset + 12));
}
return data;
}
updateIconVertexData(vertexIndex, anchorX, anchorY, newOx, newOy, newTx, newTy, newSizeX, newSizeY, pixelOffsetX, pixelOffsetY, minFontScaleX, minFontScaleY) {
assert$1(vertexIndex >= 0 && vertexIndex < this.layoutVertexArray.length, "Invalid vertex start index");
const uint16Array = this.layoutVertexArray.uint16;
const baseOffset = vertexIndex * 12;
uint16Array[baseOffset] = anchorX;
uint16Array[baseOffset + 1] = anchorY;
uint16Array[baseOffset + 2] = newOx;
uint16Array[baseOffset + 3] = newOy;
uint16Array[baseOffset + 4] = newTx;
uint16Array[baseOffset + 5] = newTy;
uint16Array[baseOffset + 6] = newSizeX;
uint16Array[baseOffset + 7] = newSizeY;
uint16Array[baseOffset + 8] = pixelOffsetX;
uint16Array[baseOffset + 9] = pixelOffsetY;
uint16Array[baseOffset + 10] = minFontScaleX;
uint16Array[baseOffset + 11] = minFontScaleY;
}
upload(context, dynamicIndexBuffer, upload, update, createZOffsetBuffer, hasAppearances) {
if (this.isEmpty()) {
return;
}
if (upload) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, symbolLayoutAttributes.members, !!hasAppearances);
this.indexBuffer = context.createIndexBuffer(this.indexArray, dynamicIndexBuffer);
this.dynamicLayoutVertexBuffer = context.createVertexBuffer(this.dynamicLayoutVertexArray, dynamicLayoutAttributes.members, true);
this.opacityVertexBuffer = context.createVertexBuffer(this.opacityVertexArray, shaderOpacityAttributes, true);
if (this.iconTransitioningVertexArray.length > 0) {
this.iconTransitioningVertexBuffer = context.createVertexBuffer(this.iconTransitioningVertexArray, iconTransitioningAttributes.members, true);
}
if (this.globeExtVertexArray.length > 0) {
this.globeExtVertexBuffer = context.createVertexBuffer(this.globeExtVertexArray, symbolGlobeExtAttributes.members, true);
}
if (!this.zOffsetVertexBuffer && (this.zOffsetVertexArray.length > 0 || !!createZOffsetBuffer)) {
this.zOffsetVertexBuffer = context.createVertexBuffer(this.zOffsetVertexArray, zOffsetAttributes.members, true);
}
if (!this.orientationVertexBuffer && this.orientationVertexArray && this.orientationVertexArray.length > 0) {
this.orientationVertexBuffer = context.createVertexBuffer(this.orientationVertexArray, orientationAttributes.members, true);
assert$1(this.orientationVertexBuffer.length === this.layoutVertexBuffer.length);
}
this.opacityVertexBuffer.itemSize = 1;
}
if (upload || update) {
this.programConfigurations.upload(context);
}
}
destroy() {
if (!this.layoutVertexBuffer) return;
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.programConfigurations.destroy();
this.segments.destroy();
this.dynamicLayoutVertexBuffer.destroy();
this.opacityVertexBuffer.destroy();
if (this.iconTransitioningVertexBuffer) {
this.iconTransitioningVertexBuffer.destroy();
}
if (this.globeExtVertexBuffer) {
this.globeExtVertexBuffer.destroy();
}
if (this.zOffsetVertexBuffer) {
this.zOffsetVertexBuffer.destroy();
}
if (this.orientationVertexBuffer) {
this.orientationVertexBuffer.destroy();
}
}
}
register(SymbolBuffers, "SymbolBuffers");
class CollisionBuffers {
constructor(LayoutArray, layoutAttributes, IndexArray) {
this.layoutVertexArray = new LayoutArray();
this.layoutAttributes = layoutAttributes;
this.indexArray = new IndexArray();
this.segments = new SegmentVector();
this.collisionVertexArray = new StructArrayLayout2ub4f20();
this.collisionVertexArrayExt = new StructArrayLayout4f16();
}
upload(context) {
this.layoutVertexBuffer = context.createVertexBuffer(this.layoutVertexArray, this.layoutAttributes);
this.indexBuffer = context.createIndexBuffer(this.indexArray);
this.collisionVertexBuffer = context.createVertexBuffer(this.collisionVertexArray, collisionVertexAttributes.members, true);
this.collisionVertexBufferExt = context.createVertexBuffer(this.collisionVertexArrayExt, collisionVertexAttributesExt.members, true);
}
destroy() {
if (!this.layoutVertexBuffer) return;
this.layoutVertexBuffer.destroy();
this.indexBuffer.destroy();
this.segments.destroy();
this.collisionVertexBuffer.destroy();
this.collisionVertexBufferExt.destroy();
}
}
register(CollisionBuffers, "CollisionBuffers");
class SymbolBucket {
constructor(options) {
this.collisionBoxArray = options.collisionBoxArray;
this.zoom = options.zoom;
this.overscaling = options.overscaling;
this.layers = options.layers;
this.layerIds = this.layers.map((layer2) => layer2.fqid);
this.index = options.index;
this.pixelRatio = options.pixelRatio;
this.sourceLayerIndex = options.sourceLayerIndex;
this.hasPattern = false;
this.hasRTLText = false;
this.fullyClipped = false;
this.hasAnyIconTextFit = false;
this.sortKeyRanges = [];
this.collisionCircleArray = [];
this.placementInvProjMatrix = identity$3([]);
this.placementViewportMatrix = identity$3([]);
const layer = this.layers[0];
const unevaluatedLayoutValues = layer._unevaluatedLayout._values;
this.worldview = options.worldview;
this.localizable = options.localizable;
this.textSizeData = getSizeData(this.zoom, unevaluatedLayoutValues["text-size"], this.worldview);
this.iconSizeData = getSizeData(this.zoom, unevaluatedLayoutValues["icon-size"], this.worldview);
const layout = this.layers[0].layout;
const sortKey = layout.get("symbol-sort-key");
const zOrder = layout.get("symbol-z-order");
this.lut = options.lut;
this.canOverlap = layout.get("text-allow-overlap") || layout.get("icon-allow-overlap") || layout.get("text-ignore-placement") || layout.get("icon-ignore-placement");
this.sortFeaturesByKey = zOrder !== "viewport-y" && sortKey.constantOr(1) !== void 0;
const zOrderByViewportY = zOrder === "viewport-y" || zOrder === "auto" && !this.sortFeaturesByKey;
this.sortFeaturesByY = zOrderByViewportY && this.canOverlap;
this.writingModes = layout.get("text-writing-mode").map((wm) => WritingMode[wm]);
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.sourceID = options.sourceID;
this.projection = options.projection;
this.hasAnyZOffset = false;
this.zOffsetSortDirty = false;
this.zOffsetBuffersNeedUpload = false;
this.elevationType = "none";
this.elevationStateComplete = false;
this.activeReplacements = [];
this.replacementUpdateTime = 0;
this.hasAnySecondaryIcon = false;
this.hasAppearances = null;
this.lastActiveApperance = null;
this.featureToAppearanceIndex = {};
}
hasAnyAppearanceProperty(propertyName) {
const layer = this.layers[0];
const appearances = layer.getAppearances();
if (!appearances || appearances.length === 0) {
return false;
}
return appearances.some((appearance) => {
const property = appearance.getProperty(propertyName);
return property !== void 0 && property !== null;
});
}
createArrays() {
this.text = new SymbolBuffers(new ProgramConfigurationSet(this.layers, { zoom: this.zoom, lut: this.lut }, (property) => {
return property.startsWith("text") || property.startsWith("symbol");
}));
this.icon = new SymbolBuffers(new ProgramConfigurationSet(this.layers, { zoom: this.zoom, lut: this.lut }, (property) => {
return property.startsWith("icon") || property.startsWith("symbol");
}));
this.glyphOffsetArray = new GlyphOffsetArray();
this.lineVertexArray = new SymbolLineVertexArray();
this.symbolInstances = new SymbolInstanceArray();
}
calculateGlyphDependencies(text, stack, textAlongLine, allowVerticalPlacement, doesAllowVerticalWritingMode) {
for (const char of text) {
const codePoint = char.codePointAt(0);
if (codePoint === void 0) break;
stack[codePoint] = true;
if (allowVerticalPlacement && doesAllowVerticalWritingMode && codePoint <= 65535) {
const verticalChar = verticalizedCharacterMap[char];
if (verticalChar) {
stack[verticalChar.charCodeAt(0)] = true;
}
}
}
}
calculateEffectiveAppearanceIconSize(activeAppearance, currentZoom, evaluationFeature, canonical, availableImages, iconScaleFactor) {
let effectiveIconSize = 1;
const unevaluatedIconSize = activeAppearance.getUnevaluatedProperties()._values["icon-size"];
const iconSizeData = getSizeData(this.zoom, unevaluatedIconSize, this.worldview);
const sizeValue = evaluateSizeForZoom(iconSizeData, currentZoom);
if (iconSizeData.kind === "constant" || iconSizeData.kind === "camera") {
effectiveIconSize = sizeValue.uSize;
}
if (iconSizeData.kind === "composite") {
const { minZoom, maxZoom } = iconSizeData;
const possibleSizeMin = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(minZoom, { worldview: this.worldview }), canonical);
const possibleSizeMax = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(maxZoom, { worldview: this.worldview }), canonical);
const sizeMin = possibleSizeMin.evaluate(evaluationFeature, {}, canonical, availableImages);
const sizeMax = possibleSizeMax.evaluate(evaluationFeature, {}, canonical, availableImages);
effectiveIconSize = sizeMin + (sizeMax - sizeMin) * sizeValue.uSizeT;
}
if (iconSizeData.kind === "source") {
const possibleSize = unevaluatedIconSize.possiblyEvaluate(new EvaluationParameters(this.zoom, { worldview: this.worldview }), canonical);
effectiveIconSize = possibleSize.evaluate(evaluationFeature, {}, canonical, availableImages);
}
return effectiveIconSize * iconScaleFactor;
}
updateFootprints(_id, _footprints) {
}
updateReplacement(coord, source) {
if (source.updateTime === this.replacementUpdateTime) {
return false;
}
this.replacementUpdateTime = source.updateTime;
const newReplacements = source.getReplacementRegionsForTile(coord.toUnwrapped(), true);
if (regionsEquals(this.activeReplacements, newReplacements)) {
return false;
}
this.activeReplacements = newReplacements;
return true;
}
getResolvedImageFromTokens(tokens) {
if (typeof tokens === "string") {
return ResolvedImage.build(tokens);
}
return tokens;
}
populate(features, options, canonical, tileTransform) {
const layer = this.layers[0];
const layout = layer.layout;
const isGlobe = this.projection.name === "globe";
const textFont = layout.get("text-font");
const textField = layout.get("text-field");
const iconImage = layout.get("icon-image");
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const iconScaleFactor = clamp(options.scaleFactor || 1, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
const hasText = (textField.value.kind !== "constant" || textField.value.value instanceof Formatted && !textField.value.value.isEmpty() || textField.value.value.toString().length > 0) && (textFont.value.kind !== "constant" || textFont.value.value.length > 0);
const hasIcon = iconImage.value.kind !== "constant" || !!iconImage.value.value || Object.keys(iconImage.parameters).length > 0;
const hasAppearanceIcons = this.hasAnyAppearanceProperty("icon-image");
const symbolSortKey = layout.get("symbol-sort-key");
this.features = [];
this.appearanceFeatureData = [];
if (!hasText && !hasIcon && !hasAppearanceIcons) {
return;
}
const icons = options.iconDependencies;
const stacks = options.glyphDependencies;
const availableImages = options.availableImages;
const globalProperties = new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors });
const addImageVariantToIcons = (variant) => {
const variantId = variant.id.toString();
if (icons.has(variantId)) {
icons.get(variantId).push(variant);
} else {
icons.set(variantId, [variant]);
}
};
for (const indexedFeature of features) {
const { feature, id, index, sourceLayerIndex } = indexedFeature;
const needGeometry = layer._featureFilter.needGeometry;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!layer._featureFilter.filter(globalProperties, evaluationFeature, canonical)) {
continue;
}
if (!needGeometry) evaluationFeature.geometry = loadGeometry(feature, canonical, tileTransform);
if (isGlobe && feature.type !== 1 && canonical.z <= 5) {
const geom = evaluationFeature.geometry;
const cosAngleThreshold = 0.98078528056;
const predicate = (a, b) => {
const v0 = tileCoordToECEF(a.x, a.y, canonical, 1);
const v1 = tileCoordToECEF(b.x, b.y, canonical, 1);
return dot$5(v0, v1) < cosAngleThreshold;
};
for (let i = 0; i < geom.length; i++) {
geom[i] = resamplePred(geom[i], predicate);
}
}
let text;
if (hasText) {
const resolvedTokens = layer.getValueAndResolveTokens("text-field", evaluationFeature, canonical, availableImages);
const formattedText = Formatted.factory(resolvedTokens);
if (containsRTLText(formattedText)) {
this.hasRTLText = true;
}
if (!this.hasRTLText || // non-rtl text so can proceed safely
getRTLTextPluginStatus() === "unavailable" || // We don't intend to lazy-load the rtl text plugin, so proceed with incorrect shaping
this.hasRTLText && plugin.isParsed()) {
text = transformText$1(formattedText, layer, evaluationFeature);
}
}
let icon;
if (hasIcon) {
const resolvedTokens = layer.getValueAndResolveTokens("icon-image", evaluationFeature, canonical, availableImages);
icon = this.getResolvedImageFromTokens(resolvedTokens);
}
const symbolLayer = this.layers[0];
let usesAppearanceIconAsFallback = false;
if (!icon && hasAppearanceIcons) {
const appearances2 = symbolLayer.getAppearances();
for (const appearance of appearances2) {
const iconImage2 = appearance.getProperty("icon-image");
if (iconImage2) {
const resolvedTokens = symbolLayer.getAppearanceValueAndResolveTokens(appearance, "icon-image", evaluationFeature, canonical, availableImages);
if (resolvedTokens) {
icon = this.getResolvedImageFromTokens(resolvedTokens);
usesAppearanceIconAsFallback = true;
break;
}
}
}
}
if (!text && !icon) {
continue;
}
const sortKey = this.sortFeaturesByKey ? symbolSortKey.evaluate(evaluationFeature, {}, canonical) : void 0;
const symbolFeature = {
id,
text,
icon,
index,
sourceLayerIndex,
geometry: evaluationFeature.geometry,
properties: feature.properties,
type: vectorTileFeatureTypes[feature.type],
sortKey
};
this.features.push(symbolFeature);
this.featureToAppearanceIndex[index] = this.appearanceFeatureData.length;
this.appearanceFeatureData.push({
id,
// This is already the promoted ID from IndexedFeature
properties: feature.properties,
usesAppearanceIconAsPlaceholder: usesAppearanceIconAsFallback,
isUsingAppearanceVertexData: false,
layoutBasedVertexData: [],
activeAppearance: null
});
if (icon) {
const unevaluatedLayoutValues = symbolLayer._unevaluatedLayout._values;
const { iconPrimary, iconSecondary } = getScaledImageVariant(icon, this.iconSizeData, unevaluatedLayoutValues["icon-size"], canonical, this.zoom, symbolFeature, this.pixelRatio, iconScaleFactor, this.worldview);
addImageVariantToIcons(iconPrimary);
if (iconSecondary) {
this.hasAnySecondaryIcon = true;
addImageVariantToIcons(iconSecondary);
}
}
const appearances = symbolLayer.getAppearances();
if (appearances.length !== 0) {
appearances.forEach((a) => {
const iconImage2 = a.getProperty("icon-image");
if (!iconImage2) return;
const iconPrimary = this.getCombinedIconPrimary(a, symbolLayer, evaluationFeature, canonical, availableImages, symbolFeature, iconScaleFactor);
if (!iconPrimary) return;
addImageVariantToIcons(iconPrimary);
});
}
if (text) {
const fontStack = textFont.evaluate(evaluationFeature, {}, canonical).join(",");
const textAlongLine = layout.get("text-rotation-alignment") === "map" && layout.get("symbol-placement") !== "point";
this.allowVerticalPlacement = this.writingModes && this.writingModes.indexOf(WritingMode.vertical) >= 0;
for (const section of text.sections) {
if (!section.image) {
const doesAllowVerticalWritingMode = allowsVerticalWritingMode(text.toString());
const sectionFont = section.fontStack || fontStack;
const sectionStack = stacks[sectionFont] = stacks[sectionFont] || {};
this.calculateGlyphDependencies(section.text, sectionStack, textAlongLine, this.allowVerticalPlacement, doesAllowVerticalWritingMode);
} else {
const imagePrimary = section.image.getPrimary().scaleSelf(this.pixelRatio);
const imagePrimaryId = imagePrimary.id.toString();
const primaryIcons = icons.get(imagePrimaryId) || [];
primaryIcons.push(imagePrimary);
icons.set(imagePrimaryId, primaryIcons);
}
}
}
}
if (layout.get("symbol-placement") === "line") {
this.features = mergeLines(this.features);
}
if (layout.get("symbol-elevation-reference") === "hd-road-markup") {
this.elevationType = "road";
if (options.elevationFeatures) {
if (!this.elevationFeatures && options.elevationFeatures.length > 0) {
this.elevationFeatures = [];
this.elevationFeatureIdToIndex = /* @__PURE__ */ new Map();
}
for (const elevationFeature of options.elevationFeatures) {
this.elevationFeatureIdToIndex.set(elevationFeature.id, this.elevationFeatures.length);
this.elevationFeatures.push(elevationFeature);
}
}
} else if (layout.get("symbol-z-elevate")) {
this.elevationType = "offset";
}
if (this.elevationType !== "none") {
this.zOffsetBuffersNeedUpload = true;
}
if (this.sortFeaturesByKey) {
this.features.sort((a, b) => {
return a.sortKey - b.sortKey;
});
}
}
getCombinedIconPrimary(appearance, layer, evaluationFeature, canonical, availableImages, symbolFeature, iconScaleFactor) {
let icon;
let iconPrimary;
const unevaluatedProperties = appearance.getUnevaluatedProperties();
const hasIconImage = unevaluatedProperties._values["icon-image"].value !== void 0;
if (hasIconImage) {
const resolvedTokens = layer.getAppearanceValueAndResolveTokens(appearance, "icon-image", evaluationFeature, canonical, availableImages);
icon = this.getResolvedImageFromTokens(resolvedTokens);
} else {
const resolvedTokens = layer.getValueAndResolveTokens("icon-image", evaluationFeature, canonical, availableImages);
icon = this.getResolvedImageFromTokens(resolvedTokens);
}
if (icon) {
const unevaluatedIconSize = unevaluatedProperties._values["icon-size"] || layer._unevaluatedLayout._values["icon-size"];
const iconSizeData = getSizeData(this.zoom, unevaluatedIconSize, this.worldview);
const imageVariant = getScaledImageVariant(icon, iconSizeData, unevaluatedIconSize, canonical, this.zoom, symbolFeature, this.pixelRatio, iconScaleFactor, this.worldview);
iconPrimary = imageVariant.iconPrimary;
}
return iconPrimary;
}
updateAppearanceBasedIconTextures(canonical, featureState, availableImages, globalProperties) {
if (!this.appearanceFeatureData) return false;
if (!this.icon.layoutVertexArray || this.icon.layoutVertexArray.length === 0) {
return false;
}
const layer = this.layers[0];
let reuploadBuffer = false;
let vertexOffset = 0;
const layout = layer.layout;
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const iconScaleFactor = clamp(1, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
for (let s = 0; s < this.symbolInstances.length; s++) {
const symbolInstance = this.symbolInstances.get(s);
const appearanceFeatureDataIndex = this.featureToAppearanceIndex[symbolInstance.featureIndex];
const featureData = appearanceFeatureDataIndex !== void 0 ? this.appearanceFeatureData[appearanceFeatureDataIndex] : void 0;
if (featureData && symbolInstance.placedIconSymbolIndex >= 0) {
const featureId = featureData.id;
const featureStateForThis = featureState && featureId !== void 0 ? featureState[String(featureId)] : void 0;
const evaluationFeature = {
type: "Point",
// Type doesn't matter
id: featureData.id,
properties: featureData.properties,
geometry: []
// Geometry doesn't matter
};
const activeAppearance = this.layers[0].appearances && this.layers[0].appearances.find((a) => a.isActive({ globals: globalProperties, feature: evaluationFeature, canonical, featureState: featureStateForThis }));
if (featureData.activeAppearance === activeAppearance) {
vertexOffset += symbolInstance.numIconVertices;
continue;
}
if (activeAppearance) {
featureData.activeAppearance = activeAppearance;
const minimalFeature = {
sortKey: void 0,
text: void 0,
icon: null,
// Will be resolved in getAppearanceIconPrimary
index: symbolInstance.featureIndex,
sourceLayerIndex: symbolInstance.featureIndex,
geometry: [],
properties: featureData.properties,
type: "Point",
id: featureData.id
};
const iconPrimary = this.getCombinedIconPrimary(activeAppearance, layer, evaluationFeature, canonical, availableImages, minimalFeature, iconScaleFactor);
if (!iconPrimary) continue;
const primaryImageSerialized = iconPrimary.toString();
const position = this.iconAtlasPositions && this.iconAtlasPositions.get(primaryImageSerialized);
if (position) {
const iconOffsetValue = layer.getAppearanceValueAndResolveTokens(activeAppearance, "icon-offset", evaluationFeature, canonical, availableImages);
const iconOffset = iconOffsetValue && Array.isArray(iconOffsetValue) ? iconOffsetValue : [0, 0];
const iconAnchor = layer.layout.get("icon-anchor").evaluate(evaluationFeature, {}, canonical);
let shapedIcon = shapeIcon(position, void 0, iconOffset, iconAnchor);
const iconRotateValue = layer.getAppearanceValueAndResolveTokens(activeAppearance, "icon-rotate", evaluationFeature, canonical, availableImages);
const iconRotate = typeof iconRotateValue === "number" ? iconRotateValue : 0;
const isSDFIcon = position.sdf;
const iconTextFit = layer.layout.get("icon-text-fit").constantOr("none");
if (iconTextFit !== "none" && featureData.textShaping && featureData.iconTextFitPadding && featureData.fontScale) {
shapedIcon = fitIconToText(
shapedIcon,
featureData.textShaping,
iconTextFit,
featureData.iconTextFitPadding,
iconOffset,
featureData.fontScale
);
}
const effectiveIconSize = this.calculateEffectiveAppearanceIconSize(
activeAppearance,
globalProperties.zoom,
evaluationFeature,
canonical,
availableImages,
iconScaleFactor
);
const newSizeX = 0;
const newSizeY = (Math.min(MAX_PACKED_SIZE, Math.round(effectiveIconSize * SIZE_PACK_FACTOR)) << 1) + 1;
const iconQuads = getIconQuads(shapedIcon, iconRotate, isSDFIcon, iconTextFit !== "none", iconScaleFactor);
if (!featureData.isUsingAppearanceVertexData) {
featureData.isUsingAppearanceVertexData = true;
featureData.layoutBasedVertexData = this.icon.getIconVertexData(vertexOffset, symbolInstance.numIconVertices);
}
for (let j = 0; j < iconQuads.length; ++j) {
const quad = iconQuads[j];
const anchorX = featureData.layoutBasedVertexData[0] || symbolInstance.tileAnchorX;
const anchorY = featureData.layoutBasedVertexData[1] || symbolInstance.tileAnchorY;
const pixelOffsetTLX = quad.pixelOffsetTL.x * 16;
const pixelOffsetTLY = quad.pixelOffsetTL.y * 16;
const pixelOffsetBRX = quad.pixelOffsetBR.x * 16;
const pixelOffsetBRY = quad.pixelOffsetBR.y * 16;
const minFontScaleX = quad.minFontScaleX * 16;
const minFontScaleY = quad.minFontScaleY * 16;
this.icon.updateIconVertexData(vertexOffset, anchorX, anchorY, Math.round(quad.tl.x * 32), Math.round(quad.tl.y * 32), quad.texPrimary.x, quad.texPrimary.y, newSizeX, newSizeY, pixelOffsetTLX, pixelOffsetTLY, minFontScaleX, minFontScaleY);
this.icon.updateIconVertexData(vertexOffset + 1, anchorX, anchorY, Math.round(quad.tr.x * 32), Math.round(quad.tr.y * 32), quad.texPrimary.x + quad.texPrimary.w, quad.texPrimary.y, newSizeX, newSizeY, pixelOffsetBRX, pixelOffsetTLY, minFontScaleX, minFontScaleY);
this.icon.updateIconVertexData(vertexOffset + 2, anchorX, anchorY, Math.round(quad.bl.x * 32), Math.round(quad.bl.y * 32), quad.texPrimary.x, quad.texPrimary.y + quad.texPrimary.h, newSizeX, newSizeY, pixelOffsetTLX, pixelOffsetBRY, minFontScaleX, minFontScaleY);
this.icon.updateIconVertexData(vertexOffset + 3, anchorX, anchorY, Math.round(quad.br.x * 32), Math.round(quad.br.y * 32), quad.texPrimary.x + quad.texPrimary.w, quad.texPrimary.y + quad.texPrimary.h, newSizeX, newSizeY, pixelOffsetBRX, pixelOffsetBRY, minFontScaleX, minFontScaleY);
vertexOffset += 4;
}
const verticesUsed = iconQuads.length * 4;
const remainingVertices = symbolInstance.numIconVertices - verticesUsed;
for (let k = 0; k < remainingVertices; ++k) {
this.icon.updateIconVertexData(vertexOffset + k, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
reuploadBuffer = true;
} else {
vertexOffset += symbolInstance.numIconVertices;
}
} else if (featureData.usesAppearanceIconAsPlaceholder) {
if (this.layers[0].appearances && this.layers[0].appearances.length > 0) {
this.icon.updateIconVertexData(vertexOffset, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
this.icon.updateIconVertexData(vertexOffset + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
this.icon.updateIconVertexData(vertexOffset + 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
this.icon.updateIconVertexData(vertexOffset + 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
reuploadBuffer = true;
}
vertexOffset += symbolInstance.numIconVertices;
featureData.activeAppearance = null;
} else if (featureData.isUsingAppearanceVertexData) {
const numItemsPerVertex = 12;
const layoutNumVertices = featureData.layoutBasedVertexData.length / numItemsPerVertex;
for (let i = 0; i < layoutNumVertices; ++i) {
const base = i * numItemsPerVertex;
this.icon.updateIconVertexData(
vertexOffset + i,
featureData.layoutBasedVertexData[base + 0],
featureData.layoutBasedVertexData[base + 1],
featureData.layoutBasedVertexData[base + 2],
featureData.layoutBasedVertexData[base + 3],
featureData.layoutBasedVertexData[base + 4],
featureData.layoutBasedVertexData[base + 5],
featureData.layoutBasedVertexData[base + 6],
featureData.layoutBasedVertexData[base + 7],
featureData.layoutBasedVertexData[base + 8],
featureData.layoutBasedVertexData[base + 9],
featureData.layoutBasedVertexData[base + 10],
featureData.layoutBasedVertexData[base + 11]
);
}
vertexOffset += layoutNumVertices;
featureData.isUsingAppearanceVertexData = false;
featureData.activeAppearance = null;
reuploadBuffer = true;
} else {
vertexOffset += symbolInstance.numIconVertices;
featureData.activeAppearance = null;
}
}
}
return reuploadBuffer;
}
update(states, vtLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness) {
this.text.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, this.worldview);
this.icon.programConfigurations.updatePaintArrays(states, vtLayer, layers, availableImages, imagePositions, isBrightnessChanged, brightness, this.worldview);
}
updateRoadElevation(canonical) {
if (this.elevationType !== "road" || !this.elevationFeatures) {
return;
}
if (this.elevationStateComplete) {
return;
}
this.elevationStateComplete = true;
this.hasAnyZOffset = false;
let dataChanged = false;
const tileToMeters = tileToMeter(canonical);
const metersToTile = 1 / tileToMeters;
let hasTextOrientation = false;
let hasIconOrientation = false;
for (let s = 0; s < this.symbolInstances.length; s++) {
const symbolInstance = this.symbolInstances.get(s);
const orientedXAxis = fromValues$4(1, 0, 0);
const orientedYAxis = fromValues$4(0, 1, 0);
const {
numHorizontalGlyphVertices,
numVerticalGlyphVertices,
numIconVertices,
numVerticalIconVertices
} = symbolInstance;
const hasText = numHorizontalGlyphVertices > 0 || numVerticalGlyphVertices > 0;
const hasIcon = numIconVertices > 0;
const elevationFeature = this.elevationFeatures[symbolInstance.elevationFeatureIndex];
if (elevationFeature) {
const anchor = new Point(symbolInstance.tileAnchorX, symbolInstance.tileAnchorY);
const newZOffset = 0.075 + elevationFeature.pointElevation(anchor);
if (symbolInstance.zOffset !== newZOffset) {
dataChanged = true;
symbolInstance.zOffset = newZOffset;
}
if (newZOffset !== 0) {
this.hasAnyZOffset = true;
}
const slopeNormal = elevationFeature.computeSlopeNormal(anchor, metersToTile);
const rotation = rotationTo(create$2(), fromValues$4(0, 0, 1), slopeNormal);
transformQuat$1(orientedXAxis, orientedXAxis, rotation);
transformQuat$1(orientedYAxis, orientedYAxis, rotation);
orientedXAxis[2] *= tileToMeters;
orientedYAxis[2] *= tileToMeters;
if (orientedXAxis[0] !== 1 || orientedXAxis[1] !== 0 || orientedXAxis[2] !== 0 || orientedYAxis[0] !== 0 || orientedYAxis[1] !== 1 || orientedYAxis[2] !== 0) {
hasTextOrientation = hasTextOrientation || hasText;
hasIconOrientation = hasIconOrientation || hasIcon;
}
}
if (hasText) {
addOrientationVertex(this.text.orientationVertexArray, numHorizontalGlyphVertices, orientedXAxis, orientedYAxis);
addOrientationVertex(this.text.orientationVertexArray, numVerticalGlyphVertices, orientedXAxis, orientedYAxis);
}
if (hasIcon) {
const { placedIconSymbolIndex, verticalPlacedIconSymbolIndex } = symbolInstance;
if (placedIconSymbolIndex >= 0) {
addOrientationVertex(this.icon.orientationVertexArray, numIconVertices, orientedXAxis, orientedYAxis);
}
if (verticalPlacedIconSymbolIndex >= 0) {
addOrientationVertex(this.icon.orientationVertexArray, numVerticalIconVertices, orientedXAxis, orientedYAxis);
}
}
}
if (!hasTextOrientation) {
this.text.orientationVertexArray = void 0;
}
if (!hasIconOrientation) {
this.icon.orientationVertexArray = void 0;
}
if (dataChanged) {
this.zOffsetBuffersNeedUpload = true;
this.zOffsetSortDirty = true;
}
}
updateZOffset() {
const addZOffsetTextVertex = (array, numVertices, value) => {
currentTextZOffsetVertex += numVertices;
if (currentTextZOffsetVertex > array.length) {
array.resize(currentTextZOffsetVertex);
}
for (let i = -numVertices; i < 0; i++) {
array.emplace(i + currentTextZOffsetVertex, value);
}
};
const addZOffsetIconVertex = (array, numVertices, value) => {
currentIconZOffsetVertex += numVertices;
if (currentIconZOffsetVertex > array.length) {
array.resize(currentIconZOffsetVertex);
}
for (let i = -numVertices; i < 0; i++) {
array.emplace(i + currentIconZOffsetVertex, value);
}
};
const updateZOffset = this.zOffsetBuffersNeedUpload;
if (!updateZOffset) return;
this.zOffsetBuffersNeedUpload = false;
let currentTextZOffsetVertex = 0;
let currentIconZOffsetVertex = 0;
for (let s = 0; s < this.symbolInstances.length; s++) {
const symbolInstance = this.symbolInstances.get(s);
const {
numHorizontalGlyphVertices,
numVerticalGlyphVertices,
numIconVertices
} = symbolInstance;
const zOffset = symbolInstance.zOffset;
const hasText = numHorizontalGlyphVertices > 0 || numVerticalGlyphVertices > 0;
const hasIcon = numIconVertices > 0;
if (hasText) {
addZOffsetTextVertex(this.text.zOffsetVertexArray, numHorizontalGlyphVertices, zOffset);
addZOffsetTextVertex(this.text.zOffsetVertexArray, numVerticalGlyphVertices, zOffset);
}
if (hasIcon) {
const { placedIconSymbolIndex, verticalPlacedIconSymbolIndex } = symbolInstance;
if (placedIconSymbolIndex >= 0) {
addZOffsetIconVertex(this.icon.zOffsetVertexArray, numIconVertices, zOffset);
}
if (verticalPlacedIconSymbolIndex >= 0) {
addZOffsetIconVertex(this.icon.zOffsetVertexArray, symbolInstance.numVerticalIconVertices, zOffset);
}
}
}
if (this.text.zOffsetVertexBuffer) {
this.text.zOffsetVertexBuffer.updateData(this.text.zOffsetVertexArray);
assert$1(this.text.zOffsetVertexBuffer.length === this.text.layoutVertexArray.length);
}
if (this.icon.zOffsetVertexBuffer) {
this.icon.zOffsetVertexBuffer.updateData(this.icon.zOffsetVertexArray);
assert$1(this.icon.zOffsetVertexBuffer.length === this.icon.layoutVertexArray.length);
}
}
isEmpty() {
return this.symbolInstances.length === 0 && !this.hasRTLText;
}
uploadPending() {
return !this.uploaded || this.text.programConfigurations.needsUpload || this.icon.programConfigurations.needsUpload;
}
upload(context, canonical, featureState, availableImages, globalProperties) {
if (!this.uploaded && this.hasDebugData()) {
this.textCollisionBox.upload(context);
this.iconCollisionBox.upload(context);
}
this.text.upload(context, this.sortFeaturesByY, !this.uploaded, this.text.programConfigurations.needsUpload, this.zOffsetBuffersNeedUpload, false);
if (this.hasAppearances === null) {
this.hasAppearances = this.layers.some((layer) => layer.appearances && layer.appearances.length > 0);
}
this.icon.upload(context, this.sortFeaturesByY, !this.uploaded, this.icon.programConfigurations.needsUpload, this.zOffsetBuffersNeedUpload, this.hasAppearances);
this.uploaded = true;
}
updateAppearances(canonical, featureState, availableImages, globalProperties) {
if (!canonical || !featureState || !availableImages) {
return false;
}
if (!this.icon.layoutVertexArray || this.icon.layoutVertexArray.length === 0) {
return false;
}
if (!this.icon.layoutVertexArray.arrayBuffer) {
return false;
}
const hasChanges = this.updateAppearanceBasedIconTextures(canonical, featureState, availableImages, globalProperties);
if (hasChanges && this.icon.layoutVertexBuffer && this.icon.layoutVertexArray.arrayBuffer !== null) {
if (this.icon.layoutVertexArray.length === this.icon.layoutVertexBuffer.length) {
this.icon.layoutVertexBuffer.updateData(this.icon.layoutVertexArray);
}
}
}
destroyDebugData() {
this.textCollisionBox.destroy();
this.iconCollisionBox.destroy();
}
getProjection() {
if (!this.projectionInstance) {
this.projectionInstance = getProjection(this.projection);
}
return this.projectionInstance;
}
destroy() {
this.text.destroy();
this.icon.destroy();
if (this.hasDebugData()) {
this.destroyDebugData();
}
}
addToLineVertexArray(anchor, line) {
const lineStartIndex = this.lineVertexArray.length;
if (anchor.segment !== void 0) {
for (const { x, y } of line) {
this.lineVertexArray.emplaceBack(x, y);
}
}
return {
lineStartIndex,
lineLength: this.lineVertexArray.length - lineStartIndex
};
}
addSymbols(arrays, quads, sizeVertex, lineOffset, alongLine, feature, writingMode, globe, tileAnchor, lineStartIndex, lineLength, associatedIconIndex, availableImages, canonical, brightness, hasAnySecondaryIcon, symbolInstanceIndex, maxQuadCount) {
const indexArray = arrays.indexArray;
const layoutVertexArray = arrays.layoutVertexArray;
const globeExtVertexArray = arrays.globeExtVertexArray;
const quadCountForSegment = maxQuadCount;
const segment = arrays.segments.prepareSegment(4 * quadCountForSegment, layoutVertexArray, indexArray, this.canOverlap ? feature.sortKey : void 0);
const glyphOffsetArrayStart = this.glyphOffsetArray.length;
const vertexStartIndex = segment.vertexLength;
const angle = this.allowVerticalPlacement && writingMode === WritingMode.vertical ? Math.PI / 2 : 0;
const sections = feature.text && feature.text.sections;
for (let i = 0; i < quads.length; i++) {
const { tl, tr, bl, br, texPrimary, texSecondary, pixelOffsetTL, pixelOffsetBR, minFontScaleX, minFontScaleY, glyphOffset, isSDF, sectionIndex } = quads[i];
const index = segment.vertexLength;
const y = glyphOffset[1];
addVertex(layoutVertexArray, tileAnchor.x, tileAnchor.y, tl.x, y + tl.y, texPrimary.x, texPrimary.y, sizeVertex, isSDF, pixelOffsetTL.x, pixelOffsetTL.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, tileAnchor.x, tileAnchor.y, tr.x, y + tr.y, texPrimary.x + texPrimary.w, texPrimary.y, sizeVertex, isSDF, pixelOffsetBR.x, pixelOffsetTL.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, tileAnchor.x, tileAnchor.y, bl.x, y + bl.y, texPrimary.x, texPrimary.y + texPrimary.h, sizeVertex, isSDF, pixelOffsetTL.x, pixelOffsetBR.y, minFontScaleX, minFontScaleY);
addVertex(layoutVertexArray, tileAnchor.x, tileAnchor.y, br.x, y + br.y, texPrimary.x + texPrimary.w, texPrimary.y + texPrimary.h, sizeVertex, isSDF, pixelOffsetBR.x, pixelOffsetBR.y, minFontScaleX, minFontScaleY);
if (globe) {
const { x, y: y2, z } = globe.anchor;
const [ux, uy, uz] = globe.up;
addGlobeVertex(globeExtVertexArray, x, y2, z, ux, uy, uz);
addGlobeVertex(globeExtVertexArray, x, y2, z, ux, uy, uz);
addGlobeVertex(globeExtVertexArray, x, y2, z, ux, uy, uz);
addGlobeVertex(globeExtVertexArray, x, y2, z, ux, uy, uz);
addDynamicAttributes(arrays.dynamicLayoutVertexArray, x, y2, z, angle);
} else {
addDynamicAttributes(arrays.dynamicLayoutVertexArray, tileAnchor.x, tileAnchor.y, tileAnchor.z, angle);
}
if (hasAnySecondaryIcon) {
const tex = texSecondary ? texSecondary : texPrimary;
addTransitioningVertex(arrays.iconTransitioningVertexArray, tex.x, tex.y);
addTransitioningVertex(arrays.iconTransitioningVertexArray, tex.x + tex.w, tex.y);
addTransitioningVertex(arrays.iconTransitioningVertexArray, tex.x, tex.y + tex.h);
addTransitioningVertex(arrays.iconTransitioningVertexArray, tex.x + tex.w, tex.y + tex.h);
}
indexArray.emplaceBack(index, index + 1, index + 2);
indexArray.emplaceBack(index + 1, index + 2, index + 3);
segment.vertexLength += 4;
segment.primitiveLength += 2;
this.glyphOffsetArray.emplaceBack(glyphOffset[0]);
if (i === quads.length - 1 || sectionIndex !== quads[i + 1].sectionIndex) {
arrays.programConfigurations.populatePaintArrays(layoutVertexArray.length, feature, feature.index, {}, availableImages, canonical, brightness, sections && sections[sectionIndex], this.worldview);
}
}
const remainingQuads = maxQuadCount - quads.length;
if (remainingQuads !== 0) {
this._addNullVertices(remainingQuads, layoutVertexArray, sizeVertex, globe, globeExtVertexArray, arrays, hasAnySecondaryIcon, segment, indexArray);
}
const projectedAnchor = globe ? globe.anchor : tileAnchor;
arrays.placedSymbolArray.emplaceBack(
projectedAnchor.x,
projectedAnchor.y,
projectedAnchor.z,
tileAnchor.x,
tileAnchor.y,
glyphOffsetArrayStart,
this.glyphOffsetArray.length - glyphOffsetArrayStart,
vertexStartIndex,
lineStartIndex,
lineLength,
tileAnchor.segment,
sizeVertex ? sizeVertex[0] : 0,
sizeVertex ? sizeVertex[1] : 0,
lineOffset[0],
lineOffset[1],
writingMode,
// placedOrientation is null initially; will be updated to horizontal(1)/vertical(2) if placed
0,
0,
// The crossTileID is only filled/used on the foreground for dynamic text anchors
0,
associatedIconIndex,
// flipState is unknown initially; will be updated to flipRequired(1)/flipNotRequired(2) during line label reprojection
0
);
arrays.symbolInstanceIndices.push(symbolInstanceIndex);
}
_addNullVertices(num, layoutVertexArray, sizeVertex, globe, globeExtVertexArray, arrays, hasAnySecondaryIcon, segment, indexArray) {
for (let i = 0; i < num; i++) {
for (let j = 0; j < 4; j++) {
addVertex(layoutVertexArray, 0, 0, 0, 0, 0, 0, sizeVertex, false, 0, 0, 0, 0);
if (globe) {
addGlobeVertex(globeExtVertexArray, 0, 0, 0, 0, 0, 0);
addDynamicAttributes(arrays.dynamicLayoutVertexArray, 0, 0, 0, 0);
} else {
addDynamicAttributes(arrays.dynamicLayoutVertexArray, 0, 0, 0, 0);
}
if (hasAnySecondaryIcon) {
addTransitioningVertex(arrays.iconTransitioningVertexArray, 0, 0);
}
}
const quadStartIndex = segment.vertexLength;
indexArray.emplaceBack(quadStartIndex, quadStartIndex + 1, quadStartIndex + 2);
indexArray.emplaceBack(quadStartIndex + 1, quadStartIndex + 2, quadStartIndex + 3);
segment.vertexLength += 4;
segment.primitiveLength += 2;
this.glyphOffsetArray.emplaceBack(0);
}
}
_commitLayoutVertex(array, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, tileAnchorX, tileAnchorY, extrude) {
array.emplaceBack(
// pos
boxTileAnchorX,
boxTileAnchorY,
boxTileAnchorZ,
// a_anchor_pos
tileAnchorX,
tileAnchorY,
// extrude
Math.round(extrude.x),
Math.round(extrude.y)
);
}
_addCollisionDebugVertices(box, scale, arrays, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, symbolInstance) {
const segment = arrays.segments.prepareSegment(4, arrays.layoutVertexArray, arrays.indexArray);
const index = segment.vertexLength;
const symbolTileAnchorX = symbolInstance.tileAnchorX;
const symbolTileAnchorY = symbolInstance.tileAnchorY;
for (let i = 0; i < 4; i++) {
arrays.collisionVertexArray.emplaceBack(0, 0, 0, 0, 0, 0);
}
this._commitDebugCollisionVertexUpdate(arrays.collisionVertexArrayExt, scale, box.padding, symbolInstance.zOffset);
this._commitLayoutVertex(arrays.layoutVertexArray, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, symbolTileAnchorX, symbolTileAnchorY, new Point(box.x1, box.y1));
this._commitLayoutVertex(arrays.layoutVertexArray, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, symbolTileAnchorX, symbolTileAnchorY, new Point(box.x2, box.y1));
this._commitLayoutVertex(arrays.layoutVertexArray, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, symbolTileAnchorX, symbolTileAnchorY, new Point(box.x2, box.y2));
this._commitLayoutVertex(arrays.layoutVertexArray, boxTileAnchorX, boxTileAnchorY, boxTileAnchorZ, symbolTileAnchorX, symbolTileAnchorY, new Point(box.x1, box.y2));
segment.vertexLength += 4;
const indexArray = arrays.indexArray;
indexArray.emplaceBack(index, index + 1);
indexArray.emplaceBack(index + 1, index + 2);
indexArray.emplaceBack(index + 2, index + 3);
indexArray.emplaceBack(index + 3, index);
segment.primitiveLength += 4;
}
_addTextDebugCollisionBoxes(size, zoom, collisionBoxArray, startIndex, endIndex, instance) {
for (let b = startIndex; b < endIndex; b++) {
const box = collisionBoxArray.get(b);
const scale = this.getSymbolInstanceTextSize(size, instance, zoom, b);
this._addCollisionDebugVertices(box, scale, this.textCollisionBox, box.projectedAnchorX, box.projectedAnchorY, box.projectedAnchorZ, instance);
}
}
_addIconDebugCollisionBoxes(size, zoom, collisionBoxArray, startIndex, endIndex, instance) {
for (let b = startIndex; b < endIndex; b++) {
const box = collisionBoxArray.get(b);
const scale = this.getSymbolInstanceIconSize(size, zoom, instance.placedIconSymbolIndex);
this._addCollisionDebugVertices(box, scale, this.iconCollisionBox, box.projectedAnchorX, box.projectedAnchorY, box.projectedAnchorZ, instance);
}
}
generateCollisionDebugBuffers(zoom, collisionBoxArray, textScaleFactor) {
if (this.hasDebugData()) {
this.destroyDebugData();
}
this.textCollisionBox = new CollisionBuffers(StructArrayLayout3i2i2i16, collisionBoxLayout.members, StructArrayLayout2ui4);
this.iconCollisionBox = new CollisionBuffers(StructArrayLayout3i2i2i16, collisionBoxLayout.members, StructArrayLayout2ui4);
const iconSize = evaluateSizeForZoom(this.iconSizeData, zoom);
const textSize = evaluateSizeForZoom(this.textSizeData, zoom, textScaleFactor);
for (let i = 0; i < this.symbolInstances.length; i++) {
const symbolInstance = this.symbolInstances.get(i);
this._addTextDebugCollisionBoxes(textSize, zoom, collisionBoxArray, symbolInstance.textBoxStartIndex, symbolInstance.textBoxEndIndex, symbolInstance);
this._addTextDebugCollisionBoxes(textSize, zoom, collisionBoxArray, symbolInstance.verticalTextBoxStartIndex, symbolInstance.verticalTextBoxEndIndex, symbolInstance);
this._addIconDebugCollisionBoxes(iconSize, zoom, collisionBoxArray, symbolInstance.iconBoxStartIndex, symbolInstance.iconBoxEndIndex, symbolInstance);
this._addIconDebugCollisionBoxes(iconSize, zoom, collisionBoxArray, symbolInstance.verticalIconBoxStartIndex, symbolInstance.verticalIconBoxEndIndex, symbolInstance);
}
}
getSymbolInstanceTextSize(textSize, instance, zoom, boxIndex) {
const symbolIndex = instance.rightJustifiedTextSymbolIndex >= 0 ? instance.rightJustifiedTextSymbolIndex : instance.centerJustifiedTextSymbolIndex >= 0 ? instance.centerJustifiedTextSymbolIndex : instance.leftJustifiedTextSymbolIndex >= 0 ? instance.leftJustifiedTextSymbolIndex : instance.verticalPlacedTextSymbolIndex >= 0 ? instance.verticalPlacedTextSymbolIndex : boxIndex;
const symbol = this.text.placedSymbolArray.get(symbolIndex);
const featureSize = evaluateSizeForFeature(this.textSizeData, textSize, symbol) / ONE_EM;
return this.tilePixelRatio * featureSize;
}
getSymbolInstanceIconSize(iconSize, zoom, iconIndex) {
const symbol = this.icon.placedSymbolArray.get(iconIndex);
const featureSize = evaluateSizeForFeature(this.iconSizeData, iconSize, symbol);
return this.tilePixelRatio * featureSize;
}
_commitDebugCollisionVertexUpdate(array, scale, padding, zOffset) {
array.emplaceBack(scale, -padding, -padding, zOffset);
array.emplaceBack(scale, padding, -padding, zOffset);
array.emplaceBack(scale, padding, padding, zOffset);
array.emplaceBack(scale, -padding, padding, zOffset);
}
_updateTextDebugCollisionBoxes(size, zoom, collisionBoxArray, startIndex, endIndex, instance, scaleFactor) {
for (let b = startIndex; b < endIndex; b++) {
const box = collisionBoxArray.get(b);
const scale = this.getSymbolInstanceTextSize(size, instance, zoom, b);
const array = this.textCollisionBox.collisionVertexArrayExt;
this._commitDebugCollisionVertexUpdate(array, scale, box.padding, instance.zOffset);
}
}
_updateIconDebugCollisionBoxes(size, zoom, collisionBoxArray, startIndex, endIndex, instance, iconScaleFactor) {
for (let b = startIndex; b < endIndex; b++) {
const box = collisionBoxArray.get(b);
const scale = this.getSymbolInstanceIconSize(size, zoom, instance.placedIconSymbolIndex);
const array = this.iconCollisionBox.collisionVertexArrayExt;
this._commitDebugCollisionVertexUpdate(array, scale, box.padding, instance.zOffset);
}
}
updateCollisionDebugBuffers(zoom, collisionBoxArray, textScaleFactor, iconScaleFactor) {
if (!this.hasDebugData()) {
return;
}
if (this.hasTextCollisionBoxData()) this.textCollisionBox.collisionVertexArrayExt.clear();
if (this.hasIconCollisionBoxData()) this.iconCollisionBox.collisionVertexArrayExt.clear();
const iconSize = evaluateSizeForZoom(this.iconSizeData, zoom, iconScaleFactor);
const textSize = evaluateSizeForZoom(this.textSizeData, zoom, textScaleFactor);
for (let i = 0; i < this.symbolInstances.length; i++) {
const symbolInstance = this.symbolInstances.get(i);
this._updateTextDebugCollisionBoxes(textSize, zoom, collisionBoxArray, symbolInstance.textBoxStartIndex, symbolInstance.textBoxEndIndex, symbolInstance, textScaleFactor);
this._updateTextDebugCollisionBoxes(textSize, zoom, collisionBoxArray, symbolInstance.verticalTextBoxStartIndex, symbolInstance.verticalTextBoxEndIndex, symbolInstance, textScaleFactor);
this._updateIconDebugCollisionBoxes(iconSize, zoom, collisionBoxArray, symbolInstance.iconBoxStartIndex, symbolInstance.iconBoxEndIndex, symbolInstance, iconScaleFactor);
this._updateIconDebugCollisionBoxes(iconSize, zoom, collisionBoxArray, symbolInstance.verticalIconBoxStartIndex, symbolInstance.verticalIconBoxEndIndex, symbolInstance, iconScaleFactor);
}
if (this.hasTextCollisionBoxData() && this.textCollisionBox.collisionVertexBufferExt) {
this.textCollisionBox.collisionVertexBufferExt.updateData(this.textCollisionBox.collisionVertexArrayExt);
}
if (this.hasIconCollisionBoxData() && this.iconCollisionBox.collisionVertexBufferExt) {
this.iconCollisionBox.collisionVertexBufferExt.updateData(this.iconCollisionBox.collisionVertexArrayExt);
}
}
// These flat arrays are meant to be quicker to iterate over than the source
// CollisionBoxArray
_deserializeCollisionBoxesForSymbol(collisionBoxArray, textStartIndex, textEndIndex, verticalTextStartIndex, verticalTextEndIndex, iconStartIndex, iconEndIndex, verticalIconStartIndex, verticalIconEndIndex) {
const collisionArrays = {};
if (textStartIndex < textEndIndex) {
const { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY, featureIndex } = collisionBoxArray.get(textStartIndex);
collisionArrays.textBox = { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY };
collisionArrays.textFeatureIndex = featureIndex;
}
if (verticalTextStartIndex < verticalTextEndIndex) {
const { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY, featureIndex } = collisionBoxArray.get(verticalTextStartIndex);
collisionArrays.verticalTextBox = { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY };
collisionArrays.verticalTextFeatureIndex = featureIndex;
}
if (iconStartIndex < iconEndIndex) {
const { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY, featureIndex } = collisionBoxArray.get(iconStartIndex);
collisionArrays.iconBox = { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY };
collisionArrays.iconFeatureIndex = featureIndex;
}
if (verticalIconStartIndex < verticalIconEndIndex) {
const { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY, featureIndex } = collisionBoxArray.get(verticalIconStartIndex);
collisionArrays.verticalIconBox = { x1, y1, x2, y2, padding, projectedAnchorX, projectedAnchorY, projectedAnchorZ, tileAnchorX, tileAnchorY };
collisionArrays.verticalIconFeatureIndex = featureIndex;
}
return collisionArrays;
}
deserializeCollisionBoxes(collisionBoxArray) {
this.collisionArrays = [];
for (let i = 0; i < this.symbolInstances.length; i++) {
const symbolInstance = this.symbolInstances.get(i);
this.collisionArrays.push(this._deserializeCollisionBoxesForSymbol(
collisionBoxArray,
symbolInstance.textBoxStartIndex,
symbolInstance.textBoxEndIndex,
symbolInstance.verticalTextBoxStartIndex,
symbolInstance.verticalTextBoxEndIndex,
symbolInstance.iconBoxStartIndex,
symbolInstance.iconBoxEndIndex,
symbolInstance.verticalIconBoxStartIndex,
symbolInstance.verticalIconBoxEndIndex
));
}
}
hasTextData() {
return this.text.segments.get().length > 0;
}
hasIconData() {
return this.icon.segments.get().length > 0;
}
hasDebugData() {
return this.textCollisionBox && this.iconCollisionBox;
}
hasTextCollisionBoxData() {
return this.hasDebugData() && this.textCollisionBox.segments.get().length > 0;
}
hasIconCollisionBoxData() {
return this.hasDebugData() && this.iconCollisionBox.segments.get().length > 0;
}
hasIconTextFit() {
return this.hasAnyIconTextFit;
}
addIndicesForPlacedSymbol(iconOrText, placedSymbolIndex) {
const placedSymbol = iconOrText.placedSymbolArray.get(placedSymbolIndex);
const endIndex = placedSymbol.vertexStartIndex + placedSymbol.numGlyphs * 4;
for (let vertexIndex = placedSymbol.vertexStartIndex; vertexIndex < endIndex; vertexIndex += 4) {
iconOrText.indexArray.emplaceBack(vertexIndex, vertexIndex + 1, vertexIndex + 2);
iconOrText.indexArray.emplaceBack(vertexIndex + 1, vertexIndex + 2, vertexIndex + 3);
}
}
getSortedSymbolIndexes(angle) {
if (this.sortedAngle === angle && this.symbolInstanceIndexes !== void 0) {
return this.symbolInstanceIndexes;
}
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const rotatedYs = [];
const featureIndexes = [];
const result = [];
for (let i = 0; i < this.symbolInstances.length; ++i) {
result.push(i);
const symbolInstance = this.symbolInstances.get(i);
rotatedYs.push(Math.round(sin * symbolInstance.tileAnchorX + cos * symbolInstance.tileAnchorY) | 0);
featureIndexes.push(symbolInstance.featureIndex);
}
result.sort((aIndex, bIndex) => rotatedYs[aIndex] - rotatedYs[bIndex] || featureIndexes[bIndex] - featureIndexes[aIndex]);
return result;
}
getSortedIndexesByZOffset() {
if (!this.zOffsetSortDirty) {
assert$1(this.symbolInstanceIndexesSortedZOffset.length === this.symbolInstances.length);
return this.symbolInstanceIndexesSortedZOffset;
}
if (!this.symbolInstanceIndexesSortedZOffset) {
this.symbolInstanceIndexesSortedZOffset = [];
for (let i = 0; i < this.symbolInstances.length; ++i) {
this.symbolInstanceIndexesSortedZOffset.push(i);
}
}
this.zOffsetSortDirty = false;
return this.symbolInstanceIndexesSortedZOffset.sort((aIndex, bIndex) => this.symbolInstances.get(bIndex).zOffset - this.symbolInstances.get(aIndex).zOffset);
}
addToSortKeyRanges(symbolInstanceIndex, sortKey) {
const last = this.sortKeyRanges[this.sortKeyRanges.length - 1];
if (last && last.sortKey === sortKey) {
last.symbolInstanceEnd = symbolInstanceIndex + 1;
} else {
this.sortKeyRanges.push({
sortKey,
symbolInstanceStart: symbolInstanceIndex,
symbolInstanceEnd: symbolInstanceIndex + 1
});
}
}
sortFeatures(angle) {
if (!this.sortFeaturesByY) return;
if (this.sortedAngle === angle) return;
if (this.text.segments.get().length > 1 || this.icon.segments.get().length > 1) return;
this.symbolInstanceIndexes = this.getSortedSymbolIndexes(angle);
this.sortedAngle = angle;
this.text.indexArray.clear();
this.icon.indexArray.clear();
this.featureSortOrder = [];
for (const i of this.symbolInstanceIndexes) {
const symbol = this.symbolInstances.get(i);
this.featureSortOrder.push(symbol.featureIndex);
const {
rightJustifiedTextSymbolIndex: right,
centerJustifiedTextSymbolIndex: center,
leftJustifiedTextSymbolIndex: left,
verticalPlacedTextSymbolIndex: vertical,
placedIconSymbolIndex: icon,
verticalPlacedIconSymbolIndex: iconVertical
} = symbol;
if (right >= 0) this.addIndicesForPlacedSymbol(this.text, right);
if (center >= 0 && center !== right) this.addIndicesForPlacedSymbol(this.text, center);
if (left >= 0 && left !== center && left !== right) this.addIndicesForPlacedSymbol(this.text, left);
if (vertical >= 0) this.addIndicesForPlacedSymbol(this.text, vertical);
if (icon >= 0) this.addIndicesForPlacedSymbol(this.icon, icon);
if (iconVertical >= 0) this.addIndicesForPlacedSymbol(this.icon, iconVertical);
}
if (this.text.indexBuffer) this.text.indexBuffer.updateData(this.text.indexArray);
if (this.icon.indexBuffer) this.icon.indexBuffer.updateData(this.icon.indexArray);
}
getElevationFeatureForText(placedSymbolIdx) {
const placedSymbols = this.text.placedSymbolArray;
assert$1(this.text.symbolInstanceIndices.length === placedSymbols.length);
const symbolInstanceIndex = this.text.symbolInstanceIndices[placedSymbolIdx];
const symbolInstance = this.symbolInstances.get(symbolInstanceIndex);
assert$1(symbolInstance);
const elevationFeatureIndex = symbolInstance.elevationFeatureIndex;
assert$1(elevationFeatureIndex === 65535 || elevationFeatureIndex < this.elevationFeatures.length);
let elevationFeature;
if (this.elevationFeatures && elevationFeatureIndex < this.elevationFeatures.length) {
elevationFeature = this.elevationFeatures[elevationFeatureIndex];
}
return elevationFeature;
}
}
register(SymbolBucket, "SymbolBucket", {
omit: ["layers", "collisionBoxArray", "compareText", "features"]
});
SymbolBucket.addDynamicAttributes = addDynamicAttributes;
function resolveTokens(properties, text) {
return text.replace(/{([^{}]+)}/g, (match, key) => {
return key in properties ? String(properties[key]) : "";
});
}
let layout$5;
const getLayoutProperties$5 = () => layout$5 || (layout$5 = new Properties({
"symbol-placement": new DataConstantProperty(spec["layout_symbol"]["symbol-placement"]),
"symbol-spacing": new DataConstantProperty(spec["layout_symbol"]["symbol-spacing"]),
"symbol-avoid-edges": new DataConstantProperty(spec["layout_symbol"]["symbol-avoid-edges"]),
"symbol-sort-key": new DataDrivenProperty(spec["layout_symbol"]["symbol-sort-key"]),
"symbol-z-order": new DataConstantProperty(spec["layout_symbol"]["symbol-z-order"]),
"symbol-z-elevate": new DataConstantProperty(spec["layout_symbol"]["symbol-z-elevate"]),
"symbol-elevation-reference": new DataConstantProperty(spec["layout_symbol"]["symbol-elevation-reference"]),
"icon-allow-overlap": new DataConstantProperty(spec["layout_symbol"]["icon-allow-overlap"]),
"icon-ignore-placement": new DataConstantProperty(spec["layout_symbol"]["icon-ignore-placement"]),
"icon-optional": new DataConstantProperty(spec["layout_symbol"]["icon-optional"]),
"icon-rotation-alignment": new DataConstantProperty(spec["layout_symbol"]["icon-rotation-alignment"]),
"icon-size": new DataDrivenProperty(spec["layout_symbol"]["icon-size"]),
"icon-size-scale-range": new DataConstantProperty(spec["layout_symbol"]["icon-size-scale-range"]),
"icon-text-fit": new DataDrivenProperty(spec["layout_symbol"]["icon-text-fit"]),
"icon-text-fit-padding": new DataDrivenProperty(spec["layout_symbol"]["icon-text-fit-padding"]),
"icon-image": new DataDrivenProperty(spec["layout_symbol"]["icon-image"]),
"icon-image-use-theme": new DataConstantProperty({ "type": "string", "default": "default", "property-type": "data-constant" }),
"icon-rotate": new DataDrivenProperty(spec["layout_symbol"]["icon-rotate"]),
"icon-padding": new DataConstantProperty(spec["layout_symbol"]["icon-padding"]),
"icon-keep-upright": new DataConstantProperty(spec["layout_symbol"]["icon-keep-upright"]),
"icon-offset": new DataDrivenProperty(spec["layout_symbol"]["icon-offset"]),
"icon-anchor": new DataDrivenProperty(spec["layout_symbol"]["icon-anchor"]),
"icon-pitch-alignment": new DataConstantProperty(spec["layout_symbol"]["icon-pitch-alignment"]),
"text-pitch-alignment": new DataConstantProperty(spec["layout_symbol"]["text-pitch-alignment"]),
"text-rotation-alignment": new DataConstantProperty(spec["layout_symbol"]["text-rotation-alignment"]),
"text-field": new DataDrivenProperty(spec["layout_symbol"]["text-field"]),
"text-font": new DataDrivenProperty(spec["layout_symbol"]["text-font"]),
"text-size": new DataDrivenProperty(spec["layout_symbol"]["text-size"]),
"text-size-scale-range": new DataConstantProperty(spec["layout_symbol"]["text-size-scale-range"]),
"text-max-width": new DataDrivenProperty(spec["layout_symbol"]["text-max-width"]),
"text-line-height": new DataDrivenProperty(spec["layout_symbol"]["text-line-height"]),
"text-letter-spacing": new DataDrivenProperty(spec["layout_symbol"]["text-letter-spacing"]),
"text-justify": new DataDrivenProperty(spec["layout_symbol"]["text-justify"]),
"text-radial-offset": new DataDrivenProperty(spec["layout_symbol"]["text-radial-offset"]),
"text-variable-anchor": new DataConstantProperty(spec["layout_symbol"]["text-variable-anchor"]),
"text-anchor": new DataDrivenProperty(spec["layout_symbol"]["text-anchor"]),
"text-max-angle": new DataConstantProperty(spec["layout_symbol"]["text-max-angle"]),
"text-writing-mode": new DataConstantProperty(spec["layout_symbol"]["text-writing-mode"]),
"text-rotate": new DataDrivenProperty(spec["layout_symbol"]["text-rotate"]),
"text-padding": new DataConstantProperty(spec["layout_symbol"]["text-padding"]),
"text-keep-upright": new DataConstantProperty(spec["layout_symbol"]["text-keep-upright"]),
"text-transform": new DataDrivenProperty(spec["layout_symbol"]["text-transform"]),
"text-offset": new DataDrivenProperty(spec["layout_symbol"]["text-offset"]),
"text-allow-overlap": new DataConstantProperty(spec["layout_symbol"]["text-allow-overlap"]),
"text-ignore-placement": new DataConstantProperty(spec["layout_symbol"]["text-ignore-placement"]),
"text-optional": new DataConstantProperty(spec["layout_symbol"]["text-optional"]),
"visibility": new DataConstantProperty(spec["layout_symbol"]["visibility"])
}));
let paint$6;
const getPaintProperties$6 = () => paint$6 || (paint$6 = new Properties({
"icon-opacity": new DataDrivenProperty(spec["paint_symbol"]["icon-opacity"]),
"icon-occlusion-opacity": new DataDrivenProperty(spec["paint_symbol"]["icon-occlusion-opacity"]),
"icon-emissive-strength": new DataDrivenProperty(spec["paint_symbol"]["icon-emissive-strength"]),
"text-emissive-strength": new DataDrivenProperty(spec["paint_symbol"]["text-emissive-strength"]),
"icon-color": new DataDrivenProperty(spec["paint_symbol"]["icon-color"]),
"icon-halo-color": new DataDrivenProperty(spec["paint_symbol"]["icon-halo-color"]),
"icon-halo-width": new DataDrivenProperty(spec["paint_symbol"]["icon-halo-width"]),
"icon-halo-blur": new DataDrivenProperty(spec["paint_symbol"]["icon-halo-blur"]),
"icon-translate": new DataConstantProperty(spec["paint_symbol"]["icon-translate"]),
"icon-translate-anchor": new DataConstantProperty(spec["paint_symbol"]["icon-translate-anchor"]),
"icon-image-cross-fade": new DataConstantProperty(spec["paint_symbol"]["icon-image-cross-fade"]),
"text-opacity": new DataDrivenProperty(spec["paint_symbol"]["text-opacity"]),
"text-occlusion-opacity": new DataDrivenProperty(spec["paint_symbol"]["text-occlusion-opacity"]),
"text-color": new DataDrivenProperty(spec["paint_symbol"]["text-color"], { runtimeType: ColorType, getOverride: (o) => o.textColor, hasOverride: (o) => !!o.textColor }),
"text-halo-color": new DataDrivenProperty(spec["paint_symbol"]["text-halo-color"]),
"text-halo-width": new DataDrivenProperty(spec["paint_symbol"]["text-halo-width"]),
"text-halo-blur": new DataDrivenProperty(spec["paint_symbol"]["text-halo-blur"]),
"text-translate": new DataConstantProperty(spec["paint_symbol"]["text-translate"]),
"text-translate-anchor": new DataConstantProperty(spec["paint_symbol"]["text-translate-anchor"]),
"icon-color-saturation": new DataConstantProperty(spec["paint_symbol"]["icon-color-saturation"]),
"icon-color-contrast": new DataConstantProperty(spec["paint_symbol"]["icon-color-contrast"]),
"icon-color-brightness-min": new DataConstantProperty(spec["paint_symbol"]["icon-color-brightness-min"]),
"icon-color-brightness-max": new DataConstantProperty(spec["paint_symbol"]["icon-color-brightness-max"]),
"symbol-z-offset": new DataDrivenProperty(spec["paint_symbol"]["symbol-z-offset"]),
"icon-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"icon-halo-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"text-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"text-halo-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class FormatSectionOverride {
constructor(defaultValue) {
assert$1(defaultValue.property.overrides !== void 0);
this.type = defaultValue.property.overrides ? defaultValue.property.overrides.runtimeType : NullType;
this.defaultValue = defaultValue;
}
evaluate(ctx) {
if (ctx.formattedSection) {
const overrides = this.defaultValue.property.overrides;
if (overrides && overrides.hasOverride(ctx.formattedSection)) {
return overrides.getOverride(ctx.formattedSection);
}
}
if (ctx.feature && ctx.featureState) {
return this.defaultValue.evaluate(ctx.feature, ctx.featureState);
}
return this.defaultValue.property.specification.default;
}
eachChild(fn) {
if (!this.defaultValue.isConstant()) {
const expr = this.defaultValue.value;
fn(expr._styleExpression.expression);
}
}
// Cannot be statically evaluated, as the output depends on the evaluation context.
outputDefined() {
return false;
}
serialize() {
return null;
}
}
register(FormatSectionOverride, "FormatSectionOverride", { omit: ["defaultValue"] });
let properties;
const getProperties = () => {
if (properties) {
return properties;
}
properties = {
layout: getLayoutProperties$5(),
paint: getPaintProperties$6()
};
return properties;
};
class SymbolStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
super(layer, getProperties(), scope, lut, options, layer.layout ? layer.layout["icon-image-use-theme"] : null);
this._colorAdjustmentMatrix = identity$3([]);
this.hasOcclusionOpacityProperties = layer.paint !== void 0 && ("icon-occlusion-opacity" in layer.paint || "text-occlusion-opacity" in layer.paint);
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "icon-occlusion-opacity" || name === "text-occlusion-opacity") {
this.hasOcclusionOpacityProperties = true;
}
}
recalculate(parameters, availableImages) {
super.recalculate(parameters, availableImages);
if (this.appearances) {
this.appearances.forEach((a) => {
a.recalculate(parameters, availableImages, this.iconImageUseTheme);
});
}
if (this.layout.get("icon-rotation-alignment") === "auto") {
if (this.layout.get("symbol-placement") !== "point") {
this.layout._values["icon-rotation-alignment"] = "map";
} else {
this.layout._values["icon-rotation-alignment"] = "viewport";
}
}
if (this.layout.get("text-rotation-alignment") === "auto") {
if (this.layout.get("symbol-placement") !== "point") {
this.layout._values["text-rotation-alignment"] = "map";
} else {
this.layout._values["text-rotation-alignment"] = "viewport";
}
}
if (this.layout.get("text-pitch-alignment") === "auto") {
this.layout._values["text-pitch-alignment"] = this.layout.get("text-rotation-alignment");
}
if (this.layout.get("icon-pitch-alignment") === "auto") {
this.layout._values["icon-pitch-alignment"] = this.layout.get("icon-rotation-alignment");
}
const writingModes = this.layout.get("text-writing-mode");
if (writingModes) {
const deduped = [];
for (const m of writingModes) {
if (deduped.indexOf(m) < 0) deduped.push(m);
}
this.layout._values["text-writing-mode"] = deduped;
} else if (this.layout.get("symbol-placement") === "point") {
this.layout._values["text-writing-mode"] = ["horizontal"];
} else {
this.layout._values["text-writing-mode"] = ["horizontal", "vertical"];
}
this._setPaintOverrides();
}
getColorAdjustmentMatrix(saturation, contrast, brightnessMin, brightnessMax) {
if (this._saturation !== saturation || this._contrast !== contrast || this._brightnessMin !== brightnessMin || this._brightnessMax !== brightnessMax) {
this._colorAdjustmentMatrix = computeColorAdjustmentMatrix(saturation, contrast, brightnessMin, brightnessMax);
this._saturation = saturation;
this._contrast = contrast;
this._brightnessMin = brightnessMin;
this._brightnessMax = brightnessMax;
}
return this._colorAdjustmentMatrix;
}
getValueAndResolveTokens(name, feature, canonical, availableImages) {
const property = this.layout.get(name);
const value = property.evaluate(feature, {}, canonical, availableImages);
const unevaluated = this._unevaluatedLayout._values[name];
if (!unevaluated.isDataDriven() && !isExpression(unevaluated.value) && value) {
return resolveTokens(feature.properties, value);
}
return value;
}
getAppearanceValueAndResolveTokens(appearance, name, feature, canonical, availableImages) {
const property = appearance.getProperty(name);
if (!property) return;
const value = property.evaluate(feature, {}, canonical, availableImages);
const unevaluated = appearance.getUnevaluatedProperties()._values[name];
if (!unevaluated.isDataDriven() && !isExpression(unevaluated.value) && value && typeof value === "string") {
return resolveTokens(feature.properties, value);
}
return value;
}
createBucket(parameters) {
return new SymbolBucket(parameters);
}
queryRadius() {
return 0;
}
queryIntersectsFeature() {
assert$1(false);
return false;
}
_setPaintOverrides() {
for (const overridable of getProperties().paint.overridableProperties) {
if (!SymbolStyleLayer.hasPaintOverride(this.layout, overridable)) {
continue;
}
const overriden = this.paint.get(overridable);
const override = new FormatSectionOverride(overriden);
const styleExpression = new StyleExpression(override, overriden.property.specification, this.scope, this.options, this.layout.get("icon-image-use-theme"));
let expression = null;
if (overriden.value.kind === "constant" || overriden.value.kind === "source") {
expression = new ZoomConstantExpression("source", styleExpression);
} else {
expression = new ZoomDependentExpression(
"composite",
styleExpression,
overriden.value.zoomStops,
overriden.value.interpolationType
);
}
this.paint._values[overridable] = new PossiblyEvaluatedPropertyValue(
overriden.property,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
expression,
overriden.parameters
);
}
}
_handleOverridablePaintPropertyUpdate(name, oldValue, newValue) {
if (!this.layout || oldValue.isDataDriven() || newValue.isDataDriven()) {
return false;
}
return SymbolStyleLayer.hasPaintOverride(this.layout, name);
}
static hasPaintOverride(layout, propertyName) {
const textField = layout.get("text-field");
const property = getProperties().paint.properties[propertyName];
let hasOverrides = false;
const checkSections = (sections) => {
for (const section of sections) {
if (property.overrides && property.overrides.hasOverride(section)) {
hasOverrides = true;
return;
}
}
};
if (textField.value.kind === "constant" && textField.value.value instanceof Formatted) {
checkSections(textField.value.value.sections);
} else if (textField.value.kind === "source") {
const checkExpression = (expression) => {
if (hasOverrides) return;
if (expression instanceof Literal && typeOf(expression.value) === FormattedType) {
const formatted = expression.value;
checkSections(formatted.sections);
} else if (expression instanceof FormatExpression) {
checkSections(expression.sections);
} else {
expression.eachChild(checkExpression);
}
};
const expr = textField.value;
if (expr._styleExpression) {
checkExpression(expr._styleExpression.expression);
}
}
return hasOverrides;
}
getProgramIds() {
return ["symbol"];
}
getDefaultProgramParams(name, zoom, lut) {
return {
config: new ProgramConfiguration(this, { zoom, lut }),
overrideFog: false
};
}
hasElevation() {
return this.layout && this.layout.get("symbol-elevation-reference") === "hd-road-markup";
}
}
let layout$4;
const getLayoutProperties$4 = () => layout$4 || (layout$4 = new Properties({
"visibility": new DataConstantProperty(spec["layout_background"]["visibility"])
}));
let paint$5;
const getPaintProperties$5 = () => paint$5 || (paint$5 = new Properties({
"background-pitch-alignment": new DataConstantProperty(spec["paint_background"]["background-pitch-alignment"]),
"background-color": new DataConstantProperty(spec["paint_background"]["background-color"]),
"background-pattern": new DataConstantProperty(spec["paint_background"]["background-pattern"]),
"background-opacity": new DataConstantProperty(spec["paint_background"]["background-opacity"]),
"background-emissive-strength": new DataConstantProperty(spec["paint_background"]["background-emissive-strength"]),
"background-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class BackgroundStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$4(),
paint: getPaintProperties$5()
};
super(layer, properties, scope, lut, options);
}
getProgramIds() {
const image = this.paint.get("background-pattern");
return [image ? "backgroundPattern" : "background"];
}
getDefaultProgramParams(name, zoom, lut) {
return {
overrideFog: false
};
}
is3D(terrainEnabled) {
return this.paint.get("background-pitch-alignment") === "viewport";
}
}
let layout$3;
const getLayoutProperties$3 = () => layout$3 || (layout$3 = new Properties({
"visibility": new DataConstantProperty(spec["layout_raster"]["visibility"])
}));
let paint$4;
const getPaintProperties$4 = () => paint$4 || (paint$4 = new Properties({
"raster-opacity": new DataConstantProperty(spec["paint_raster"]["raster-opacity"]),
"raster-color": new ColorRampProperty(spec["paint_raster"]["raster-color"]),
"raster-color-mix": new DataConstantProperty(spec["paint_raster"]["raster-color-mix"]),
"raster-color-range": new DataConstantProperty(spec["paint_raster"]["raster-color-range"]),
"raster-hue-rotate": new DataConstantProperty(spec["paint_raster"]["raster-hue-rotate"]),
"raster-brightness-min": new DataConstantProperty(spec["paint_raster"]["raster-brightness-min"]),
"raster-brightness-max": new DataConstantProperty(spec["paint_raster"]["raster-brightness-max"]),
"raster-saturation": new DataConstantProperty(spec["paint_raster"]["raster-saturation"]),
"raster-contrast": new DataConstantProperty(spec["paint_raster"]["raster-contrast"]),
"raster-resampling": new DataConstantProperty(spec["paint_raster"]["raster-resampling"]),
"raster-fade-duration": new DataConstantProperty(spec["paint_raster"]["raster-fade-duration"]),
"raster-emissive-strength": new DataConstantProperty(spec["paint_raster"]["raster-emissive-strength"]),
"raster-array-band": new DataConstantProperty(spec["paint_raster"]["raster-array-band"]),
"raster-elevation": new DataConstantProperty(spec["paint_raster"]["raster-elevation"]),
"raster-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
var boundsAttributes = createLayout([
{ name: "a_pos", type: "Int16", components: 2 },
{ name: "a_texture_pos", type: "Int16", components: 2 }
]);
function basisToPoints(x1, y1, x2, y2, x3, y3, x4, y4) {
const m = [x1, y1, 1, x2, y2, 1, x3, y3, 1];
const s = [x4, y4, 1];
const ma = adjoint$1([], m);
const [sx, sy, sz] = transformMat3$1(s, s, ma);
return multiply$6(m, m, [sx, 0, 0, 0, sy, 0, 0, 0, sz]);
}
function getTileToTextureTransformMatrix(x1, y1, x2, y2, x3, y3, x4, y4) {
const a = basisToPoints(0, 0, 1, 0, 1, 1, 0, 1);
const b = basisToPoints(x1, y1, x2, y2, x3, y3, x4, y4);
const adjB = adjoint$1([], b);
return multiply$6(a, a, adjB);
}
function getTextureToTileTransformMatrix(x1, y1, x2, y2, x3, y3, x4, y4) {
const a = basisToPoints(0, 0, 1, 0, 1, 1, 0, 1);
const b = basisToPoints(x1, y1, x2, y2, x3, y3, x4, y4);
const adjA = adjoint$1([], a);
return multiply$6(b, b, adjA);
}
function getPerspectiveTransform(x1, y1, x2, y2, x3, y3, x4, y4) {
const m = getTextureToTileTransformMatrix(x1, y1, x2, y2, x3, y3, x4, y4);
return [
m[2] / m[8] / EXTENT,
m[5] / m[8] / EXTENT
];
}
function isConvex(coords) {
const dx1 = coords[1].x - coords[0].x;
const dy1 = coords[1].y - coords[0].y;
const dx2 = coords[2].x - coords[1].x;
const dy2 = coords[2].y - coords[1].y;
const dx3 = coords[3].x - coords[2].x;
const dy3 = coords[3].y - coords[2].y;
const dx4 = coords[0].x - coords[3].x;
const dy4 = coords[0].y - coords[3].y;
const crossProduct1 = dx1 * dy2 - dx2 * dy1;
const crossProduct2 = dx2 * dy3 - dx3 * dy2;
const crossProduct3 = dx3 * dy4 - dx4 * dy3;
const crossProduct4 = dx4 * dy1 - dx1 * dy4;
return crossProduct1 > 0 && crossProduct2 > 0 && crossProduct3 > 0 && crossProduct4 > 0 || crossProduct1 < 0 && crossProduct2 < 0 && crossProduct3 < 0 && crossProduct4 < 0;
}
function constrainCoordinates(coords) {
return [coords[0], Math.min(Math.max(coords[1], -MAX_MERCATOR_LATITUDE), MAX_MERCATOR_LATITUDE)];
}
function constrain(coords) {
return [
constrainCoordinates(coords[0]),
constrainCoordinates(coords[1]),
constrainCoordinates(coords[2]),
constrainCoordinates(coords[3])
];
}
function calculateMinAndSize(coords) {
let minX = coords[0][0];
let maxX = minX;
let minY = coords[0][1];
let maxY = minY;
for (let i = 1; i < coords.length; i++) {
if (coords[i][0] < minX) {
minX = coords[i][0];
} else if (coords[i][0] > maxX) {
maxX = coords[i][0];
}
if (coords[i][1] < minY) {
minY = coords[i][1];
} else if (coords[i][1] > maxY) {
maxY = coords[i][1];
}
}
return [minX, minY, maxX - minX, maxY - minY];
}
function calculateMinAndSizeForPoints(coords) {
let minX = coords[0].x;
let maxX = minX;
let minY = coords[0].y;
let maxY = minY;
for (let i = 1; i < coords.length; i++) {
if (coords[i].x < minX) {
minX = coords[i].x;
} else if (coords[i].x > maxX) {
maxX = coords[i].x;
}
if (coords[i].y < minY) {
minY = coords[i].y;
} else if (coords[i].y > maxY) {
maxY = coords[i].y;
}
}
return [minX, minY, maxX - minX, maxY - minY];
}
function sortTriangles(centerLatitudes, indices) {
const triangleCount = centerLatitudes.length;
assert$1(indices.length === triangleCount);
const triangleIndexes = Array.from({ length: triangleCount }, (v, i) => i);
triangleIndexes.sort((idx1, idx2) => {
return centerLatitudes[idx1] - centerLatitudes[idx2];
});
const sortedCenterLatitudes = [];
const sortedIndices = new StructArrayLayout3ui6();
for (let i = 0; i < triangleIndexes.length; i++) {
const idx = triangleIndexes[i];
sortedCenterLatitudes.push(centerLatitudes[idx]);
const i0 = idx * 3;
const i1 = i0 + 1;
const i2 = i1 + 1;
sortedIndices.emplaceBack(indices.uint16[i0], indices.uint16[i1], indices.uint16[i2]);
}
return [sortedCenterLatitudes, sortedIndices];
}
class ImageSource extends Evented {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.coordinates = options.coordinates;
this.type = "image";
this.minzoom = 0;
this.maxzoom = 22;
this.tileSize = 512;
this.tiles = {};
this._loaded = false;
this.onNorthPole = false;
this.onSouthPole = false;
this.setEventedParent(eventedParent);
this.options = options;
this._dirty = false;
}
load(newCoordinates, loaded) {
this._loaded = loaded || false;
this.fire(new Event("dataloading", { dataType: "source" }));
this.url = this.options.url;
if (!this.url) {
if (newCoordinates) {
this.coordinates = newCoordinates;
}
this._loaded = true;
this._finishLoading();
return;
}
this._imageRequest = getImage(this.map._requestManager.transformRequest(this.url, ResourceType.Image), (err, image) => {
this._imageRequest = null;
this._loaded = true;
if (err) {
this.fire(new ErrorEvent(err));
} else if (image) {
if (image instanceof HTMLImageElement) {
this.image = exported$1.getImageData(image);
} else {
this.image = image;
}
this._dirty = true;
this.width = this.image.width;
this.height = this.image.height;
if (newCoordinates) {
this.coordinates = newCoordinates;
}
this._finishLoading();
}
});
}
loaded() {
return this._loaded;
}
/**
* Updates the image URL and, optionally, the coordinates. To avoid having the image flash after changing,
* set the `raster-fade-duration` paint property on the raster layer to 0.
*
* @param {Object} options Options object.
* @param {string} [options.url] Required image URL.
* @param {Array>} [options.coordinates] Four geographical coordinates,
* represented as arrays of longitude and latitude numbers, which define the corners of the image.
* The coordinates start at the top left corner of the image and proceed in clockwise order.
* They do not have to represent a rectangle.
* @returns {ImageSource} Returns itself to allow for method chaining.
* @example
* // Add to an image source to the map with some initial URL and coordinates
* map.addSource('image_source_id', {
* type: 'image',
* url: 'https://www.mapbox.com/images/foo.png',
* coordinates: [
* [-76.54, 39.18],
* [-76.52, 39.18],
* [-76.52, 39.17],
* [-76.54, 39.17]
* ]
* });
* // Then update the image URL and coordinates
* imageSource.updateImage({
* url: 'https://www.mapbox.com/images/bar.png',
* coordinates: [
* [-76.5433, 39.1857],
* [-76.5280, 39.1838],
* [-76.5295, 39.1768],
* [-76.5452, 39.1787]
* ]
* });
*/
updateImage(options) {
if (!options.url) {
return this;
}
if (this._imageRequest && options.url !== this.options.url) {
this._imageRequest.cancel();
this._imageRequest = null;
}
this.options.url = options.url;
this.load(options.coordinates, this._loaded);
return this;
}
setTexture(texture) {
if (!(texture.handle instanceof WebGLTexture)) {
throw new Error(`The provided handle is not a WebGLTexture instance`);
}
const context = this.map.painter.context;
this.texture = new UserManagedTexture(context, texture.handle);
this.width = texture.dimensions[0];
this.height = texture.dimensions[1];
this._dirty = false;
this._loaded = true;
this._finishLoading();
return this;
}
_finishLoading() {
if (this.map) {
this.setCoordinates(this.coordinates);
this.fire(new Event("data", { dataType: "source", sourceDataType: "metadata" }));
}
}
onAdd(map) {
this.map = map;
this.load();
}
onRemove(_) {
if (this._imageRequest) {
this._imageRequest.cancel();
this._imageRequest = null;
}
if (this.texture && !(this.texture instanceof UserManagedTexture)) this.texture.destroy();
if (this.boundsBuffer) {
this.boundsBuffer.destroy();
if (this.elevatedGlobeVertexBuffer) {
this.elevatedGlobeVertexBuffer.destroy();
}
if (this.elevatedGlobeIndexBuffer) {
this.elevatedGlobeIndexBuffer.destroy();
}
}
}
/**
* Sets the image's coordinates and re-renders the map.
*
* @param {Array>} coordinates Four geographical coordinates,
* represented as arrays of longitude and latitude numbers, which define the corners of the image.
* The coordinates start at the top left corner of the image and proceed in clockwise order.
* They do not have to represent a rectangle.
* @returns {ImageSource} Returns itself to allow for method chaining.
* @example
* // Add an image source to the map with some initial coordinates
* map.addSource('image_source_id', {
* type: 'image',
* url: 'https://www.mapbox.com/images/foo.png',
* coordinates: [
* [-76.54, 39.18],
* [-76.52, 39.18],
* [-76.52, 39.17],
* [-76.54, 39.17]
* ]
* });
* // Then update the image coordinates
* imageSource.setCoordinates([
* [-76.5433, 39.1857],
* [-76.5280, 39.1838],
* [-76.5295, 39.1768],
* [-76.5452, 39.1787]
* ]);
*/
setCoordinates(coordinates) {
this.coordinates = coordinates;
this._boundsArray = void 0;
this._unsupportedCoords = false;
if (!coordinates.length) {
assert$1(false);
return this;
}
this.onNorthPole = false;
this.onSouthPole = false;
let minLat = coordinates[0][1];
let maxLat = coordinates[0][1];
for (const coord of coordinates) {
if (coord[1] > maxLat) {
maxLat = coord[1];
}
if (coord[1] < minLat) {
minLat = coord[1];
}
}
const midLat = (maxLat + minLat) / 2;
if (midLat > MAX_MERCATOR_LATITUDE) {
this.onNorthPole = true;
} else if (midLat < -MAX_MERCATOR_LATITUDE) {
this.onSouthPole = true;
}
if (!this.onNorthPole && !this.onSouthPole) {
const cornerCoords = coordinates.map(MercatorCoordinate.fromLngLat);
this.tileID = getCoordinatesCenterTileID(cornerCoords);
this.minzoom = this.maxzoom = this.tileID.z;
}
this.fire(new Event("data", { dataType: "source", sourceDataType: "content" }));
return this;
}
_clear() {
if (this.texture && !(this.texture instanceof UserManagedTexture)) {
this.texture.destroy();
this._dirty = true;
}
this.texture = null;
this._boundsArray = void 0;
this._unsupportedCoords = false;
}
_prepareData(context) {
for (const w in this.tiles) {
const tile = this.tiles[w];
if (tile.state !== "loaded") {
tile.state = "loaded";
tile.texture = this.texture;
}
}
if (this._boundsArray || this.onNorthPole || this.onSouthPole || this._unsupportedCoords) return;
const globalTileTr = tileTransform(new CanonicalTileID(0, 0, 0), this.map.transform.projection);
const globalTileCoords = [
globalTileTr.projection.project(this.coordinates[0][0], this.coordinates[0][1]),
globalTileTr.projection.project(this.coordinates[1][0], this.coordinates[1][1]),
globalTileTr.projection.project(this.coordinates[2][0], this.coordinates[2][1]),
globalTileTr.projection.project(this.coordinates[3][0], this.coordinates[3][1])
];
if (!isConvex(globalTileCoords)) {
console.warn("Image source coordinates are defining non-convex area in the Mercator projection");
this._unsupportedCoords = true;
return;
}
const tileTr = tileTransform(this.tileID, this.map.transform.projection);
const [tl, tr, br, bl] = this.coordinates.map((coord) => {
const projectedCoord = tileTr.projection.project(coord[0], coord[1]);
return getTilePoint(tileTr, projectedCoord)._round();
});
this.perspectiveTransform = getPerspectiveTransform(tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
const boundsArray = this._boundsArray = new StructArrayLayout4i8();
boundsArray.emplaceBack(tl.x, tl.y, 0, 0);
boundsArray.emplaceBack(tr.x, tr.y, EXTENT, 0);
boundsArray.emplaceBack(bl.x, bl.y, 0, EXTENT);
boundsArray.emplaceBack(br.x, br.y, EXTENT, EXTENT);
if (this.boundsBuffer) {
this.boundsBuffer.destroy();
if (this.elevatedGlobeVertexBuffer) {
this.elevatedGlobeVertexBuffer.destroy();
}
if (this.elevatedGlobeIndexBuffer) {
this.elevatedGlobeIndexBuffer.destroy();
}
}
this.boundsBuffer = context.createVertexBuffer(boundsArray, boundsAttributes.members);
this.boundsSegments = SegmentVector.simpleSegment(0, 0, 4, 2);
const cellCount = GLOBE_VERTEX_GRID_SIZE;
const lineSize = cellCount + 1;
const linesCount = cellCount + 1;
const vertexCount = lineSize * linesCount;
const triangleCount = cellCount * cellCount * 2;
const verticesLongitudes = [];
const constrainedCoordinates = constrain(this.coordinates);
const [minLng, minLat, lngDiff, latDiff] = calculateMinAndSize(constrainedCoordinates);
{
const elevatedGlobeVertexArray = new StructArrayLayout4i8();
const [minX, minY, dx, dy] = calculateMinAndSizeForPoints(globalTileCoords);
const transformToImagePoint = (coord) => {
return [(coord.x - minX) / dx, (coord.y - minY) / dy];
};
const [p0, p1, p2, p3] = globalTileCoords.map(transformToImagePoint);
const toUV = getTileToTextureTransformMatrix(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]);
this.elevatedGlobePerspectiveTransform = getPerspectiveTransform(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]);
const addVertex = (point, tilePoint) => {
verticesLongitudes.push(point.lng);
const x = Math.round((point.lng - minLng) / lngDiff * EXTENT);
const y = Math.round((point.lat - minLat) / latDiff * EXTENT);
const imagePoint = transformToImagePoint(tilePoint);
const uv = transformMat3$1([], [imagePoint[0], imagePoint[1], 1], toUV);
const u = Math.round(uv[0] / uv[2] * EXTENT);
const v = Math.round(uv[1] / uv[2] * EXTENT);
elevatedGlobeVertexArray.emplaceBack(x, y, u, v);
};
const leftDx = globalTileCoords[3].x - globalTileCoords[0].x;
const leftDy = globalTileCoords[3].y - globalTileCoords[0].y;
const rightDx = globalTileCoords[2].x - globalTileCoords[1].x;
const rightDy = globalTileCoords[2].y - globalTileCoords[1].y;
for (let i = 0; i < linesCount; i++) {
const linesPart = i / cellCount;
const startLinePoint = [globalTileCoords[0].x + linesPart * leftDx, globalTileCoords[0].y + linesPart * leftDy];
const endLinePoint = [globalTileCoords[1].x + linesPart * rightDx, globalTileCoords[1].y + linesPart * rightDy];
const lineDx = endLinePoint[0] - startLinePoint[0];
const lineDy = endLinePoint[1] - startLinePoint[1];
for (let j = 0; j < lineSize; j++) {
const linePart = j / cellCount;
const point = { x: startLinePoint[0] + lineDx * linePart, y: startLinePoint[1] + lineDy * linePart, z: 0 };
addVertex(globalTileTr.projection.unproject(point.x, point.y), point);
}
}
this.elevatedGlobeVertexBuffer = context.createVertexBuffer(elevatedGlobeVertexArray, boundsAttributes.members);
}
{
this.maxLongitudeTriangleSize = 0;
let elevatedGlobeTrianglesCenterLongitudes = [];
let indices = new StructArrayLayout3ui6();
const processTriangle = (i0, i1, i2) => {
indices.emplaceBack(i0, i1, i2);
const l0 = verticesLongitudes[i0];
const l1 = verticesLongitudes[i1];
const l2 = verticesLongitudes[i2];
const minLongitude = Math.min(Math.min(l0, l1), l2);
const maxLongitude = Math.max(Math.max(l0, l1), l2);
const diff = maxLongitude - minLongitude;
if (diff > this.maxLongitudeTriangleSize) {
this.maxLongitudeTriangleSize = diff;
}
elevatedGlobeTrianglesCenterLongitudes.push(minLongitude + diff / 2);
};
for (let i = 0; i < cellCount; i++) {
for (let j = 0; j < cellCount; j++) {
const i0 = i * lineSize + j;
const i1 = i0 + 1;
const i2 = i0 + lineSize;
const i3 = i2 + 1;
processTriangle(i0, i2, i1);
processTriangle(i1, i2, i3);
}
}
[elevatedGlobeTrianglesCenterLongitudes, indices] = sortTriangles(elevatedGlobeTrianglesCenterLongitudes, indices);
this.elevatedGlobeTrianglesCenterLongitudes = elevatedGlobeTrianglesCenterLongitudes;
this.elevatedGlobeIndexBuffer = context.createIndexBuffer(indices);
}
this.elevatedGlobeSegments = SegmentVector.simpleSegment(0, 0, vertexCount, triangleCount);
this.elevatedGlobeGridMatrix = new Float32Array([0, lngDiff / EXTENT, 0, latDiff / EXTENT, 0, 0, minLat, minLng, 0]);
}
prepare() {
const hasTiles = Object.keys(this.tiles).length !== 0;
if (this.tileID && !hasTiles) return;
const context = this.map.painter.context;
const gl = context.gl;
if (this._dirty && !(this.texture instanceof UserManagedTexture)) {
if (!this.texture) {
this.texture = new Texture(context, this.image, gl.RGBA8);
this.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
} else {
this.texture.update(this.image);
}
this._dirty = false;
}
if (!hasTiles) return;
this._prepareData(context);
}
loadTile(tile, callback) {
if (this.tileID && this.tileID.equals(tile.tileID.canonical)) {
this.tiles[String(tile.tileID.wrap)] = tile;
tile.buckets = {};
callback(null);
} else {
tile.state = "errored";
callback(null);
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
serialize() {
return {
type: "image",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
url: this.options.url,
coordinates: this.coordinates
};
}
hasTransition() {
return false;
}
getSegmentsForLongitude(longitude) {
const segments = this.elevatedGlobeSegments;
if (!this.elevatedGlobeTrianglesCenterLongitudes || !segments) {
return null;
}
const longitudes = this.elevatedGlobeTrianglesCenterLongitudes;
assert$1(longitudes.length !== 0);
const normalizeLongitudeTo = (longitude2, desiredLongitude) => {
const diff = Math.round((desiredLongitude - longitude2) / 360);
return longitude2 + diff * 360;
};
let gapLongitude = normalizeLongitudeTo(longitude + 180, longitudes[0]);
const ret = new SegmentVector();
const addTriangleRange = (triangleOffset, triangleCount) => {
ret.segments.push(
{
vertexOffset: 0,
primitiveOffset: triangleOffset,
vertexLength: segments.segments[0].vertexLength,
primitiveLength: triangleCount,
sortKey: void 0,
vaos: {}
}
);
};
const distanceToDrop = 0.51 * this.maxLongitudeTriangleSize;
assert$1(distanceToDrop > 0);
assert$1(distanceToDrop < 180);
if (Math.abs(longitudes[0] - gapLongitude) <= distanceToDrop) {
const minIdx2 = upperBound(longitudes, 0, longitudes.length, gapLongitude + distanceToDrop);
if (minIdx2 === longitudes.length) {
return ret;
}
const maxIdx2 = lowerBound(longitudes, minIdx2 + 1, longitudes.length, gapLongitude + 360 - distanceToDrop);
const count = maxIdx2 - minIdx2;
addTriangleRange(minIdx2, count);
return ret;
}
if (gapLongitude < longitudes[0]) {
gapLongitude += 360;
}
const minIdx = lowerBound(longitudes, 0, longitudes.length, gapLongitude - distanceToDrop);
if (minIdx === longitudes.length) {
addTriangleRange(0, longitudes.length);
return ret;
}
addTriangleRange(0, minIdx - 0);
const maxIdx = upperBound(longitudes, minIdx + 1, longitudes.length, gapLongitude + distanceToDrop);
if (maxIdx !== longitudes.length) {
addTriangleRange(maxIdx, longitudes.length - maxIdx);
}
return ret;
}
}
function getCoordinatesCenterTileID(coords) {
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const coord of coords) {
minX = Math.min(minX, coord.x);
minY = Math.min(minY, coord.y);
maxX = Math.max(maxX, coord.x);
maxY = Math.max(maxY, coord.y);
}
const dx = maxX - minX;
const dy = maxY - minY;
const dMax = Math.max(dx, dy);
const zoom = Math.max(0, Math.floor(-Math.log2(dMax)));
const tilesAtZoom = Math.pow(2, zoom);
let x = Math.floor((minX + maxX) / 2 * tilesAtZoom);
if (x > 1) {
x -= 1;
}
return new CanonicalTileID(
zoom,
x,
Math.floor((minY + maxY) / 2 * tilesAtZoom)
);
}
const COLOR_RAMP_RES$1 = 256;
const COLOR_MIX_FACTOR = (Math.pow(COLOR_RAMP_RES$1, 2) - 1) / (255 * COLOR_RAMP_RES$1 * (COLOR_RAMP_RES$1 + 3));
class RasterStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$3(),
paint: getPaintProperties$4()
};
super(layer, properties, scope, lut, options);
this.updateColorRamp();
this._curRampRange = [NaN, NaN];
}
getProgramIds() {
return ["raster"];
}
hasColorMap() {
const expr = this._transitionablePaint._values["raster-color"].value;
return !!expr.value;
}
tileCoverLift() {
return this.paint.get("raster-elevation");
}
isDraped(sourceCache) {
if (sourceCache && sourceCache._source instanceof ImageSource) {
if (sourceCache._source.onNorthPole || sourceCache._source.onSouthPole) {
return false;
}
}
return this.paint.get("raster-elevation") === 0;
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "raster-color" || name === "raster-color-range") {
this._curRampRange = [NaN, NaN];
this.updateColorRamp();
}
}
_clear() {
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
this.colorRampTexture = null;
}
}
updateColorRamp(overrideRange) {
if (!this.hasColorMap()) return;
if (!this._curRampRange) return;
const expression = this._transitionablePaint._values["raster-color"].value.expression;
const [start, end] = overrideRange || this._transitionablePaint._values["raster-color-range"].value.expression.evaluate({ zoom: 0 }) || [NaN, NaN];
if (isNaN(start) && isNaN(end)) return;
if (start === this._curRampRange[0] && end === this._curRampRange[1]) return;
this.colorRamp = renderColorRamp({
expression,
evaluationKey: "rasterValue",
image: this.colorRamp,
clips: [{ start, end }],
resolution: COLOR_RAMP_RES$1
});
this.colorRampTexture = null;
this._curRampRange = [start, end];
}
}
let layout$2;
const getLayoutProperties$2 = () => layout$2 || (layout$2 = new Properties({
"visibility": new DataConstantProperty(spec["layout_raster-particle"]["visibility"])
}));
let paint$3;
const getPaintProperties$3 = () => paint$3 || (paint$3 = new Properties({
"raster-particle-array-band": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-array-band"]),
"raster-particle-count": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-count"]),
"raster-particle-color": new ColorRampProperty(spec["paint_raster-particle"]["raster-particle-color"]),
"raster-particle-max-speed": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-max-speed"]),
"raster-particle-speed-factor": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-speed-factor"]),
"raster-particle-fade-opacity-factor": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-fade-opacity-factor"]),
"raster-particle-reset-rate-factor": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-reset-rate-factor"]),
"raster-particle-elevation": new DataConstantProperty(spec["paint_raster-particle"]["raster-particle-elevation"]),
"raster-particle-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
const COLOR_RAMP_RES = 256;
class RasterParticleStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$2(),
paint: getPaintProperties$3()
};
super(layer, properties, scope, lut, options);
this._updateColorRamp();
this.lastInvalidatedAt = exported$1.now();
}
_clear() {
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
this.colorRampTexture = null;
}
if (this.tileFramebuffer) {
this.tileFramebuffer.destroy();
this.tileFramebuffer = null;
}
if (this.particleFramebuffer) {
this.particleFramebuffer.destroy();
this.particleFramebuffer = null;
}
}
onRemove(_) {
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
}
if (this.tileFramebuffer) {
this.tileFramebuffer.destroy();
}
if (this.particleFramebuffer) {
this.particleFramebuffer.destroy();
}
}
hasColorMap() {
const expr = this._transitionablePaint._values["raster-particle-color"].value;
return !!expr.value;
}
getProgramIds() {
return ["rasterParticle"];
}
hasOffscreenPass() {
return this.visibility !== "none";
}
isDraped(_) {
return false;
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "raster-particle-color" || name === "raster-particle-max-speed") {
this._updateColorRamp();
this._invalidateAnimationState();
}
if (name === "raster-particle-count") {
this._invalidateAnimationState();
}
}
_updateColorRamp() {
if (!this.hasColorMap()) return;
const expression = this._transitionablePaint._values["raster-particle-color"].value.expression;
const end = this._transitionablePaint._values["raster-particle-max-speed"].value.expression.evaluate({ zoom: 0 });
this.colorRamp = renderColorRamp({
expression,
evaluationKey: "rasterParticleSpeed",
image: this.colorRamp,
clips: [{ start: 0, end }],
resolution: COLOR_RAMP_RES
});
this.colorRampTexture = null;
}
_invalidateAnimationState() {
this.lastInvalidatedAt = exported$1.now();
}
tileCoverLift() {
return this.paint.get("raster-particle-elevation");
}
}
function validateCustomStyleLayer(layerObject) {
const errors = [];
const id = layerObject.id;
if (id === void 0) {
errors.push({
message: `layers.${id}: missing required property "id"`
});
}
if (layerObject.render === void 0) {
errors.push({
message: `layers.${id}: missing required method "render"`
});
}
if (layerObject.renderingMode && layerObject.renderingMode !== "2d" && layerObject.renderingMode !== "3d") {
errors.push({
message: `layers.${id}: property "renderingMode" must be either "2d" or "3d"`
});
}
return errors;
}
class CustomStyleLayer extends StyleLayer {
constructor(implementation, scope) {
super(implementation, {}, scope, null);
this.implementation = implementation;
if (implementation.slot) this.slot = implementation.slot;
}
is3D(terrainEnabled) {
return this.implementation.renderingMode === "3d";
}
hasOffscreenPass() {
return this.implementation.prerender !== void 0;
}
isDraped(_) {
return this.implementation.renderToTile !== void 0;
}
shouldRedrape() {
return !!this.implementation.shouldRerenderTiles && this.implementation.shouldRerenderTiles();
}
recalculate() {
}
updateTransitions() {
}
hasTransition() {
return false;
}
serialize() {
assert$1(false, "Custom layers cannot be serialized");
}
onAdd(map) {
if (this.implementation.onAdd) {
this.implementation.onAdd(map, map.painter.context.gl);
}
}
onRemove(map) {
if (this.implementation.onRemove) {
this.implementation.onRemove(map, map.painter.context.gl);
}
}
}
let layout$1;
const getLayoutProperties$1 = () => layout$1 || (layout$1 = new Properties({
"visibility": new DataConstantProperty(spec["layout_sky"]["visibility"])
}));
let paint$2;
const getPaintProperties$2 = () => paint$2 || (paint$2 = new Properties({
"sky-type": new DataConstantProperty(spec["paint_sky"]["sky-type"]),
"sky-atmosphere-sun": new DataConstantProperty(spec["paint_sky"]["sky-atmosphere-sun"]),
"sky-atmosphere-sun-intensity": new DataConstantProperty(spec["paint_sky"]["sky-atmosphere-sun-intensity"]),
"sky-gradient-center": new DataConstantProperty(spec["paint_sky"]["sky-gradient-center"]),
"sky-gradient-radius": new DataConstantProperty(spec["paint_sky"]["sky-gradient-radius"]),
"sky-gradient": new ColorRampProperty(spec["paint_sky"]["sky-gradient"]),
"sky-atmosphere-halo-color": new DataConstantProperty(spec["paint_sky"]["sky-atmosphere-halo-color"]),
"sky-atmosphere-color": new DataConstantProperty(spec["paint_sky"]["sky-atmosphere-color"]),
"sky-opacity": new DataConstantProperty(spec["paint_sky"]["sky-opacity"]),
"sky-gradient-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"sky-atmosphere-halo-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" }),
"sky-atmosphere-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
function getCelestialDirection(azimuth, altitude, leftHanded) {
const up = [0, 0, 1];
const rotation = identity$2([]);
rotateY$1(rotation, rotation, leftHanded ? -degToRad(azimuth) + Math.PI : degToRad(azimuth));
rotateX$1(rotation, rotation, -degToRad(altitude));
transformQuat$1(up, up, rotation);
return normalize$4(up, up);
}
class SkyLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties$1(),
paint: getPaintProperties$2()
};
super(layer, properties, scope, lut, options);
this._updateColorRamp();
}
_clear() {
if (this.skyboxFbo) {
this.skyboxFbo.destroy();
this.skyboxFbo = null;
}
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
this.colorRampTexture = null;
}
this._skyboxInvalidated = true;
}
_handleSpecialPaintPropertyUpdate(name) {
if (name === "sky-gradient") {
this._updateColorRamp();
} else if (name === "sky-atmosphere-sun" || name === "sky-atmosphere-halo-color" || name === "sky-atmosphere-color" || name === "sky-atmosphere-sun-intensity") {
this._skyboxInvalidated = true;
}
}
_updateColorRamp() {
const expression = this._transitionablePaint._values["sky-gradient"].value.expression;
this.colorRamp = renderColorRamp({
expression,
evaluationKey: "skyRadialProgress"
});
if (this.colorRampTexture) {
this.colorRampTexture.destroy();
this.colorRampTexture = null;
}
}
needsSkyboxCapture(painter) {
if (!!this._skyboxInvalidated || !this.skyboxTexture || !this.skyboxGeometry) {
return true;
}
if (!this.paint.get("sky-atmosphere-sun")) {
const lightPosition = painter.style.light.properties.get("position");
return this._lightPosition.azimuthal !== lightPosition.azimuthal || this._lightPosition.polar !== lightPosition.polar;
}
return false;
}
getCenter(painter, leftHanded) {
const type = this.paint.get("sky-type");
if (type === "atmosphere") {
const sunPosition = this.paint.get("sky-atmosphere-sun");
const useLightPosition = !sunPosition;
const light = painter.style.light;
const lightPosition = light.properties.get("position");
if (useLightPosition && light.properties.get("anchor") === "viewport") {
warnOnce("The sun direction is attached to a light with viewport anchor, lighting may behave unexpectedly.");
}
return useLightPosition ? getCelestialDirection(lightPosition.azimuthal, -lightPosition.polar + 90, leftHanded) : getCelestialDirection(sunPosition[0], -sunPosition[1] + 90, leftHanded);
}
assert$1(type === "gradient");
const direction = this.paint.get("sky-gradient-center");
return getCelestialDirection(direction[0], -direction[1] + 90, leftHanded);
}
isSky() {
return true;
}
markSkyboxValid(painter) {
this._skyboxInvalidated = false;
this._lightPosition = painter.style.light.properties.get("position");
}
hasOffscreenPass() {
return true;
}
getProgramIds() {
const type = this.paint.get("sky-type");
if (type === "atmosphere") {
return ["skyboxCapture", "skybox"];
} else if (type === "gradient") {
return ["skyboxGradient"];
}
return null;
}
}
let paint$1;
const getPaintProperties$1 = () => paint$1 || (paint$1 = new Properties({}));
class SlotStyleLayer extends StyleLayer {
constructor(layer, scope, _lut, _) {
const properties = {
paint: getPaintProperties$1()
};
super(layer, properties, scope, null);
}
}
const LayerTypeMask = {
None: 0,
Model: 1,
Symbol: 2,
FillExtrusion: 4,
All: 7
};
class ValidationError {
constructor(key, value, message, identifier) {
this.message = (key ? `${key}: ` : "") + message;
if (identifier) this.identifier = identifier;
if (value !== null && value !== void 0 && value.__line__) {
this.line = value.__line__;
}
}
}
class ValidationWarning extends ValidationError {
}
function isValidUrl(str, allowRelativeUrls) {
const isRelative = str.indexOf("://") === -1;
try {
new URL(str, isRelative && allowRelativeUrls ? "http://example.com" : void 0);
return true;
} catch (_) {
return false;
}
}
function validateModel(options) {
const url = options.value;
if (!url) {
return [];
}
if (!isString(url)) {
return [new ValidationError(options.key, url, `string expected, "${getType(url)}" found`)];
}
if (!isValidUrl(url, true)) {
return [new ValidationError(options.key, url, `invalid url "${url}"`)];
}
return [];
}
class ModelFeature {
constructor(feature, offset) {
this.feature = feature;
this.instancedDataOffset = offset;
this.instancedDataCount = 0;
this.rotation = [0, 0, 0];
this.scale = [1, 1, 1];
this.translation = [0, 0, 0];
}
}
class PerModelAttributes {
constructor() {
// via this.features, enable lookup instancedDataArray based on feature ID.
this.maxScale = 1;
this.maxXYTranslationDistance = 0;
this.instancedDataArray = new StructArrayLayout16f64();
this.instancesEvaluatedElevation = [];
this.features = [];
this.idToFeaturesIndex = {};
}
colorForInstance(index) {
const offset = index * 16;
const va = this.instancedDataArray.float32;
let a = Math.floor(va[offset + 2]);
const b = (va[offset + 2] - a) * 1.05;
a /= 100;
const r = va[offset] % 1 * 1.05;
const g = va[offset + 1] % 1 * 1.05;
return [r, g, b, a];
}
tileCoordinatesForInstance(index) {
const offset = index * 16;
const va = this.instancedDataArray.float32;
let x_ = va[offset + 0];
const wasHidden = x_ > EXTENT;
x_ = wasHidden ? x_ - EXTENT : x_;
const x = Math.trunc(x_);
const y = Math.trunc(va[offset + 1]);
return new Point(x, y);
}
translationForInstance(index) {
const offset = index * 16;
const va = this.instancedDataArray.float32;
return [va[offset + 4], va[offset + 5], va[offset + 6]];
}
rotationScaleForInstance(index) {
const offset = index * 16;
const va = this.instancedDataArray.float32;
return [
va[offset + 7],
va[offset + 8],
va[offset + 9],
va[offset + 10],
va[offset + 11],
va[offset + 12],
va[offset + 13],
va[offset + 14],
va[offset + 15]
];
}
transformForInstance(index) {
const offset = index * 16;
const va = this.instancedDataArray.float32;
return [
va[offset + 7],
va[offset + 8],
va[offset + 9],
va[offset + 4],
va[offset + 10],
va[offset + 11],
va[offset + 12],
va[offset + 5],
va[offset + 13],
va[offset + 14],
va[offset + 15],
va[offset + 6],
0,
0,
0,
1
];
}
}
class ModelBucket {
constructor(options) {
this.zoom = options.zoom;
this.canonical = options.canonical;
this.overscaledZ = this.canonical.z + Math.log2(options.overscaling);
this.layers = options.layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.projection = options.projection;
this.index = options.index;
this.worldview = options.worldview;
this.hasZoomDependentProperties = this.layers[0].isZoomDependent();
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.hasPattern = false;
this.instancesPerModel = {};
this.validForExaggeration = 0;
this.maxVerticalOffset = 0;
this.maxScale = 0;
this.maxHeight = 0;
this.lookupDim = this.zoom > this.canonical.z + 1 ? 0 : this.zoom > this.canonical.z ? 256 : this.zoom > 15 ? 75 : 100;
this.instanceCount = 0;
this.terrainElevationMin = 0;
this.terrainElevationMax = 0;
this.validForDEMTile = { id: null, timestamp: 0 };
this.modelUris = [];
this.modelsRequested = false;
this.activeReplacements = [];
this.replacementUpdateTime = 0;
this.styleDefinedModelURLs = options.styleDefinedModelURLs;
this.hasAppearances = null;
}
updateFootprints(_id, _footprints) {
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
populate(features, options, canonical, tileTransform) {
this.tileToMeter = tileToMeter(canonical);
const needGeometry = this.layers[0]._featureFilter.needGeometry;
this.lookup = new Uint8Array(this.lookupDim * this.lookupDim);
for (const { feature, id, index, sourceLayerIndex } of features) {
const featureId = id != null ? id : feature.properties && feature.properties.hasOwnProperty("id") ? feature.properties["id"] : void 0;
const evaluationFeature = toEvaluationFeature(feature, needGeometry);
if (!this.layers[0]._featureFilter.filter(new EvaluationParameters(this.zoom, { worldview: this.worldview, activeFloors: options.activeFloors }), evaluationFeature, canonical))
continue;
const bucketFeature = {
id: featureId,
sourceLayerIndex,
index,
geometry: needGeometry ? evaluationFeature.geometry : loadGeometry(feature, canonical, tileTransform),
properties: feature.properties,
type: feature.type,
patterns: {}
};
const modelId = this.addFeature(bucketFeature, bucketFeature.geometry, evaluationFeature);
if (modelId) {
options.featureIndex.insert(feature, bucketFeature.geometry, index, sourceLayerIndex, this.index, this.instancesPerModel[modelId].instancedDataArray.length, EXTENT / 32);
}
}
this.lookup = null;
}
evaluateQueryRenderedFeaturePadding() {
const modelManager = this.layers[0].modelManager;
const scope = this.layers[0].scope;
let maxQueryRadius = 0;
for (const modelId of this.modelUris) {
const model = modelManager.getModel(modelId, scope);
if (!model) {
continue;
}
const modelInstances = this.instancesPerModel[modelId];
if (modelInstances) {
const radius = distance$2(model.aabb.max, model.aabb.min) * 0.5 * modelInstances.maxScale + modelInstances.maxXYTranslationDistance;
const radiusInTileUnits = Math.min(EXTENT, Math.max(radius / this.tileToMeter, EXTENT / 32));
maxQueryRadius = Math.max(radiusInTileUnits, maxQueryRadius);
}
}
return maxQueryRadius;
}
update(states, vtLayer, availableImages, imagePositions) {
for (const modelId in this.instancesPerModel) {
const instances = this.instancesPerModel[modelId];
for (const id in states) {
if (instances.idToFeaturesIndex.hasOwnProperty(id)) {
const feature = instances.features[instances.idToFeaturesIndex[id]];
this.evaluate(feature, states[id], instances, true);
this.uploaded = false;
}
}
}
this.maxHeight = 0;
}
updateZoomBasedPaintProperties() {
if (!this.hasZoomDependentProperties) {
return false;
}
let reuploadNeeded = false;
for (const modelId in this.instancesPerModel) {
const instances = this.instancesPerModel[modelId];
for (const feature of instances.features) {
const layer = this.layers[0];
const evaluationFeature = feature.feature;
const canonical = this.canonical;
const rotation = layer.paint.get("model-rotation").evaluate(evaluationFeature, {}, canonical);
const scale = layer.paint.get("model-scale").evaluate(evaluationFeature, {}, canonical);
const translation = layer.paint.get("model-translation").evaluate(evaluationFeature, {}, canonical);
if (!exactEquals$4(feature.rotation, rotation) || !exactEquals$4(feature.scale, scale) || !exactEquals$4(feature.translation, translation)) {
this.evaluate(feature, feature.featureStates, instances, true);
reuploadNeeded = true;
}
}
}
return reuploadNeeded;
}
updateReplacement(coord, source, layerIndex, scope) {
if (source.updateTime === this.replacementUpdateTime) {
return false;
}
this.replacementUpdateTime = source.updateTime;
const newReplacements = source.getReplacementRegionsForTile(coord.toUnwrapped(), true);
if (regionsEquals(this.activeReplacements, newReplacements)) {
return false;
}
this.activeReplacements = newReplacements;
let reuploadNeeded = false;
for (const modelId in this.instancesPerModel) {
const perModelVertexArray = this.instancesPerModel[modelId];
const va = perModelVertexArray.instancedDataArray;
for (const feature of perModelVertexArray.features) {
const offset = feature.instancedDataOffset;
const count = feature.instancedDataCount;
for (let i = 0; i < count; i++) {
const i16 = (i + offset) * 16;
let x_ = va.float32[i16 + 0];
const wasHidden = x_ > EXTENT;
x_ = wasHidden ? x_ - EXTENT : x_;
const x = Math.floor(x_);
const y = Math.floor(va.float32[i16 + 1]);
let hidden = false;
for (const region of this.activeReplacements) {
if (skipClipping(region, layerIndex, LayerTypeMask.Model, scope)) continue;
if (region.min.x > x || x > region.max.x || region.min.y > y || y > region.max.y) {
continue;
}
const p = transformPointToTile(x, y, coord.canonical, region.footprintTileId.canonical);
hidden = pointInFootprint(p, region.footprint);
if (hidden) break;
}
va.float32[i16] = hidden ? x_ + EXTENT : x_;
reuploadNeeded = reuploadNeeded || hidden !== wasHidden;
}
}
}
return reuploadNeeded;
}
isEmpty() {
for (const modelId in this.instancesPerModel) {
const perModelAttributes = this.instancesPerModel[modelId];
if (perModelAttributes.instancedDataArray.length !== 0) return false;
}
return true;
}
uploadPending() {
return !this.uploaded;
}
upload(context) {
const useInstancingThreshold = 0;
if (!this.uploaded) {
for (const modelId in this.instancesPerModel) {
const perModelAttributes = this.instancesPerModel[modelId];
if (perModelAttributes.instancedDataArray.length < useInstancingThreshold || perModelAttributes.instancedDataArray.length === 0) continue;
if (!perModelAttributes.instancedDataBuffer) {
perModelAttributes.instancedDataBuffer = context.createVertexBuffer(perModelAttributes.instancedDataArray, instanceAttributes.members, true, void 0, this.instanceCount);
} else {
perModelAttributes.instancedDataBuffer.updateData(perModelAttributes.instancedDataArray);
}
}
}
this.uploaded = true;
}
destroy(reload) {
for (const modelId in this.instancesPerModel) {
const perModelAttributes = this.instancesPerModel[modelId];
if (perModelAttributes.instancedDataArray.length === 0) continue;
if (perModelAttributes.instancedDataBuffer) {
perModelAttributes.instancedDataBuffer.destroy();
}
}
const modelManager = this.layers[0].modelManager;
if (reload && modelManager && this.modelUris && this.modelsRequested) {
for (const modelUri of this.modelUris) {
modelManager.removeModel(modelUri, "", true);
}
}
}
addFeature(feature, geometry, evaluationFeature) {
const layer = this.layers[0];
const modelIdProperty = layer.layout.get("model-id");
assert$1(modelIdProperty);
const modelId = modelIdProperty.evaluate(evaluationFeature, {}, this.canonical);
if (!modelId) {
warnOnce(`modelId is not evaluated for layer ${layer.id} and it is not going to get rendered.`);
return modelId;
}
if (isValidUrl(modelId, false)) {
if (!this.modelUris.includes(modelId)) {
this.modelUris.push(modelId);
}
} else {
if (this.styleDefinedModelURLs[modelId] !== void 0) {
if (!this.modelUris.includes(modelId)) {
this.modelUris.push(modelId);
}
}
}
if (!this.instancesPerModel[modelId]) {
this.instancesPerModel[modelId] = new PerModelAttributes();
}
const perModelVertexArray = this.instancesPerModel[modelId];
const instancedDataArray = perModelVertexArray.instancedDataArray;
const modelFeature = new ModelFeature(evaluationFeature, instancedDataArray.length);
for (const geometries of geometry) {
for (const point of geometries) {
if (point.x < 0 || point.x >= EXTENT || point.y < 0 || point.y >= EXTENT) {
continue;
}
if (this.lookupDim !== 0) {
const tileToLookup = (this.lookupDim - 1) / EXTENT;
const lookupIndex = this.lookupDim * (point.y * tileToLookup | 0) + point.x * tileToLookup | 0;
if (this.lookup) {
if (this.lookup[lookupIndex] !== 0) {
continue;
}
this.lookup[lookupIndex] = 1;
}
}
this.instanceCount++;
const i = instancedDataArray.length;
instancedDataArray.resize(i + 1);
perModelVertexArray.instancesEvaluatedElevation.push(0);
instancedDataArray.float32[i * 16] = point.x;
instancedDataArray.float32[i * 16 + 1] = point.y;
}
}
modelFeature.instancedDataCount = perModelVertexArray.instancedDataArray.length - modelFeature.instancedDataOffset;
if (modelFeature.instancedDataCount > 0) {
if (feature.id) {
perModelVertexArray.idToFeaturesIndex[feature.id] = perModelVertexArray.features.length;
}
perModelVertexArray.features.push(modelFeature);
this.evaluate(modelFeature, {}, perModelVertexArray, false);
}
return modelId;
}
getModelUris() {
return this.modelUris;
}
evaluate(feature, featureState, perModelVertexArray, update) {
const layer = this.layers[0];
const evaluationFeature = feature.feature;
const canonical = this.canonical;
const rotation = feature.rotation = layer.paint.get("model-rotation").evaluate(evaluationFeature, featureState, canonical);
const scale = feature.scale = layer.paint.get("model-scale").evaluate(evaluationFeature, featureState, canonical);
const translation = feature.translation = layer.paint.get("model-translation").evaluate(evaluationFeature, featureState, canonical);
const color = Object.assign({}, layer.paint.get("model-color").evaluate(evaluationFeature, featureState, canonical));
color.a = layer.paint.get("model-color-mix-intensity").evaluate(evaluationFeature, featureState, canonical);
const rotationScaleYZFlip = [];
if (this.maxVerticalOffset < translation[2]) this.maxVerticalOffset = translation[2];
const translationDistanceXYSq = translation[0] * translation[0] + translation[1] * translation[1];
const translationDistanceXY = translationDistanceXYSq > 0 ? Math.sqrt(translationDistanceXYSq) : 0;
perModelVertexArray.maxScale = Math.max(Math.max(perModelVertexArray.maxScale, scale[0]), Math.max(scale[1], scale[2]));
perModelVertexArray.maxXYTranslationDistance = Math.max(perModelVertexArray.maxXYTranslationDistance, translationDistanceXY);
this.maxScale = Math.max(Math.max(this.maxScale, scale[0]), Math.max(scale[1], scale[2]));
rotationScaleYZFlipMatrix(rotationScaleYZFlip, rotation, scale);
const constantTileToMeterAcrossTile = 10;
assert$1(perModelVertexArray.instancedDataArray.bytesPerElement === 64);
const vaOffset2 = Math.round(100 * color.a) + color.b / 1.05;
for (let i = 0; i < feature.instancedDataCount; ++i) {
const instanceOffset = feature.instancedDataOffset + i;
const offset = instanceOffset * 16;
const va = perModelVertexArray.instancedDataArray.float32;
let terrainElevationContribution = 0;
if (update) {
terrainElevationContribution = va[offset + 6] - perModelVertexArray.instancesEvaluatedElevation[instanceOffset];
}
const pointY = va[offset + 1] | 0;
va[offset] = (va[offset] | 0) + color.r / 1.05;
va[offset + 1] = pointY + color.g / 1.05;
va[offset + 2] = vaOffset2;
va[offset + 3] = 1 / (canonical.z > constantTileToMeterAcrossTile ? this.tileToMeter : tileToMeter(canonical, pointY));
va[offset + 4] = translation[0];
va[offset + 5] = translation[1];
va[offset + 6] = translation[2] + terrainElevationContribution;
va[offset + 7] = rotationScaleYZFlip[0];
va[offset + 8] = rotationScaleYZFlip[1];
va[offset + 9] = rotationScaleYZFlip[2];
va[offset + 10] = rotationScaleYZFlip[4];
va[offset + 11] = rotationScaleYZFlip[5];
va[offset + 12] = rotationScaleYZFlip[6];
va[offset + 13] = rotationScaleYZFlip[8];
va[offset + 14] = rotationScaleYZFlip[9];
va[offset + 15] = rotationScaleYZFlip[10];
perModelVertexArray.instancesEvaluatedElevation[instanceOffset] = translation[2];
}
}
}
register(ModelBucket, "ModelBucket", { omit: ["layers"] });
register(PerModelAttributes, "PerModelAttributes");
register(ModelFeature, "ModelFeature");
let layout;
const getLayoutProperties = () => layout || (layout = new Properties({
"visibility": new DataConstantProperty(spec["layout_model"]["visibility"]),
"model-id": new DataDrivenProperty(spec["layout_model"]["model-id"])
}));
let paint;
const getPaintProperties = () => paint || (paint = new Properties({
"model-opacity": new DataDrivenProperty(spec["paint_model"]["model-opacity"]),
"model-rotation": new DataDrivenProperty(spec["paint_model"]["model-rotation"]),
"model-scale": new DataDrivenProperty(spec["paint_model"]["model-scale"]),
"model-translation": new DataDrivenProperty(spec["paint_model"]["model-translation"]),
"model-color": new DataDrivenProperty(spec["paint_model"]["model-color"]),
"model-color-mix-intensity": new DataDrivenProperty(spec["paint_model"]["model-color-mix-intensity"]),
"model-type": new DataConstantProperty(spec["paint_model"]["model-type"]),
"model-cast-shadows": new DataConstantProperty(spec["paint_model"]["model-cast-shadows"]),
"model-receive-shadows": new DataConstantProperty(spec["paint_model"]["model-receive-shadows"]),
"model-ambient-occlusion-intensity": new DataConstantProperty(spec["paint_model"]["model-ambient-occlusion-intensity"]),
"model-emissive-strength": new DataDrivenProperty(spec["paint_model"]["model-emissive-strength"]),
"model-roughness": new DataDrivenProperty(spec["paint_model"]["model-roughness"]),
"model-height-based-emissive-strength-multiplier": new DataDrivenProperty(spec["paint_model"]["model-height-based-emissive-strength-multiplier"]),
"model-cutoff-fade-range": new DataConstantProperty(spec["paint_model"]["model-cutoff-fade-range"]),
"model-front-cutoff": new DataConstantProperty(spec["paint_model"]["model-front-cutoff"]),
"model-elevation-reference": new DataConstantProperty(spec["paint_model"]["model-elevation-reference"]),
"model-color-use-theme": new DataDrivenProperty({ "type": "string", "default": "default", "property-type": "data-driven" })
}));
class Elevation {
/**
* Helper that checks whether DEM data is available at a given mercator coordinate.
* @param {MercatorCoordinate} point Mercator coordinate of the point to check against.
* @returns {boolean} `true` indicating whether the data is available at `point`, and `false` otherwise.
*/
isDataAvailableAtPoint(point) {
const sourceCache = this._source();
if (this.isUsingMockSource() || !sourceCache || point.y < 0 || point.y > 1) {
return false;
}
const cache = sourceCache;
const z = cache.getSource().maxzoom;
const tiles = 1 << z;
const wrap = Math.floor(point.x);
const px = point.x - wrap;
const x = Math.floor(px * tiles);
const y = Math.floor(point.y * tiles);
const demTile = this.findDEMTileFor(new OverscaledTileID(z, wrap, z, x, y));
return !!(demTile && demTile.dem);
}
/**
* Helper around `getAtPoint` that guarantees that a numeric value is returned.
* @param {MercatorCoordinate} point Mercator coordinate of the point.
* @param {number} defaultIfNotLoaded Value that is returned if the dem tile of the provided point is not loaded.
* @returns {number} Altitude in meters.
*/
getAtPointOrZero(point, defaultIfNotLoaded = 0) {
return this.getAtPoint(point, defaultIfNotLoaded) || 0;
}
/**
* Altitude above sea level in meters at specified point.
* @param {MercatorCoordinate} point Mercator coordinate of the point.
* @param {number} defaultIfNotLoaded Value that is returned if the DEM tile of the provided point is not loaded.
* @param {boolean} exaggerated `true` if styling exaggeration should be applied to the resulting elevation.
* @returns {number} Altitude in meters.
* If there is no loaded tile that carries information for the requested
* point elevation, returns `defaultIfNotLoaded`.
* Doesn't invoke network request to fetch the data.
*/
getAtPoint(point, defaultIfNotLoaded, exaggerated = true) {
if (this.isUsingMockSource()) {
return null;
}
if (defaultIfNotLoaded == null) defaultIfNotLoaded = null;
const src = this._source();
if (!src) return defaultIfNotLoaded;
if (point.y < 0 || point.y > 1) {
return defaultIfNotLoaded;
}
const cache = src;
const z = cache.getSource().maxzoom;
const tiles = 1 << z;
const wrap = Math.floor(point.x);
const px = point.x - wrap;
const tileID = new OverscaledTileID(z, wrap, z, Math.floor(px * tiles), Math.floor(point.y * tiles));
const demTile = this.findDEMTileFor(tileID);
if (!(demTile && demTile.dem)) {
return defaultIfNotLoaded;
}
const dem = demTile.dem;
const tilesAtTileZoom = 1 << demTile.tileID.canonical.z;
const x = (px * tilesAtTileZoom - demTile.tileID.canonical.x) * dem.dim;
const y = (point.y * tilesAtTileZoom - demTile.tileID.canonical.y) * dem.dim;
const i = Math.floor(x);
const j = Math.floor(y);
const exaggeration = exaggerated ? this.exaggeration() : 1;
return exaggeration * number(
number(dem.get(i, j), dem.get(i, j + 1), y - j),
number(dem.get(i + 1, j), dem.get(i + 1, j + 1), y - j),
x - i
);
}
/*
* point.x and point.y are offset within tile, in 0 .. EXTENT coordinate space.
*/
static getAtTileOffset(tileID, point, elevation, elevationFeature) {
const tilesAtTileZoom = 1 << tileID.canonical.z;
if (elevationFeature) {
return elevationFeature.pointElevation(point);
} else if (elevation) {
return elevation.getAtPointOrZero(new MercatorCoordinate(
tileID.wrap + (tileID.canonical.x + point.x / EXTENT) / tilesAtTileZoom,
(tileID.canonical.y + point.y / EXTENT) / tilesAtTileZoom
));
} else {
return 0;
}
}
static getAtTileOffsetFunc(tileID, lat, worldSize, projection) {
return ((p, elevation, elevationFeature) => {
assert$1(p);
const z = this.getAtTileOffset(tileID, p, elevation, elevationFeature);
const upVector = projection.upVector(tileID.canonical, p.x, p.y);
const upVectorScale = projection.upVectorScale(tileID.canonical, lat, worldSize).metersToTile;
scale$4(upVector, upVector, z * upVectorScale);
return upVector;
});
}
/*
* Batch fetch for multiple tile points: points holds input and return value:
* vec3's items on index 0 and 1 define x and y offset within tile, in [0 .. EXTENT]
* range, respectively. vec3 item at index 2 is output value, in meters.
* If a DEM tile that covers tileID is loaded, true is returned, otherwise false.
* Nearest filter sampling on dem data is done (no interpolation).
*/
getForTilePoints(tileID, points, interpolated, useDemTile) {
if (this.isUsingMockSource()) {
return false;
}
const helper = DEMSampler.create(this, tileID, useDemTile);
if (!helper) {
return false;
}
points.forEach((p) => {
p[2] = this.exaggeration() * helper.getElevationAt(p[0], p[1], interpolated);
});
return true;
}
/**
* Get elevation minimum and maximum for tile identified by `tileID`.
* @param {OverscaledTileID} tileID The `tileId` is a sub tile (or covers the same space) of the DEM tile we read the information from.
* @returns {?{min: number, max: number}} The min and max elevation.
*/
getMinMaxForTile(tileID) {
if (this.isUsingMockSource()) {
return null;
}
const demTile = this.findDEMTileFor(tileID);
if (!(demTile && demTile.dem)) {
return null;
}
const dem = demTile.dem;
const tree = dem.tree;
const demTileID = demTile.tileID;
const scale = 1 << tileID.canonical.z - demTileID.canonical.z;
let xOffset = tileID.canonical.x / scale - demTileID.canonical.x;
let yOffset = tileID.canonical.y / scale - demTileID.canonical.y;
let index = 0;
for (let i = 0; i < tileID.canonical.z - demTileID.canonical.z; i++) {
if (tree.leaves[index]) break;
xOffset *= 2;
yOffset *= 2;
const childOffset = 2 * Math.floor(yOffset) + Math.floor(xOffset);
index = tree.childOffsets[index] + childOffset;
xOffset = xOffset % 1;
yOffset = yOffset % 1;
}
return { min: this.exaggeration() * tree.minimums[index], max: this.exaggeration() * tree.maximums[index] };
}
/**
* Get elevation minimum below MSL for the visible tiles. This function accounts
* for terrain exaggeration and is conservative based on the maximum DEM error,
* do not expect accurate values from this function.
* If no negative elevation is visible, this function returns 0.
* @returns {number} The min elevation below sea level of all visible tiles.
*/
getMinElevationBelowMSL() {
throw new Error("Pure virtual method called.");
}
/**
* Performs raycast against visible DEM tiles on the screen and returns the distance travelled along the ray.
* `x` & `y` components of the position are expected to be in normalized mercator coordinates [0, 1] and z in meters.
* @param {vec3} position The ray origin.
* @param {vec3} dir The ray direction.
* @param {number} exaggeration The terrain exaggeration.
*/
raycast(_position, _dir, _exaggeration) {
throw new Error("Pure virtual method called.");
}
/**
* Given a point on screen, returns 3D MercatorCoordinate on terrain.
* Helper function that wraps `raycast`.
*
* @param {Point} screenPoint Screen point in pixels in top-left origin coordinate system.
* @returns {vec4} If there is intersection with terrain, returns vec4(x, y, z, e), a
* 3D MercatorCoordinate's of intersection in its first 3 components, and elevation in meter in its 4th coordinate.
* Otherwise returns null.
*/
pointCoordinate(_screenPoint) {
throw new Error("Pure virtual method called.");
}
/*
* Implementation provides SourceCache of raster-dem source type cache, in
* order to access already loaded cached tiles.
*/
_source() {
throw new Error("Pure virtual method called.");
}
/*
* Whether the SourceCache instance is a mock source cache.
* This mock source cache is used solely for the Globe projection and with terrain disabled,
* where we only want to leverage the draping rendering pipeline without incurring DEM-tile
* download overhead. This function is useful to skip DEM processing as the mock data source
* placeholder contains only 0 height.
*/
isUsingMockSource() {
throw new Error("Pure virtual method called.");
}
/*
* A multiplier defined by style as terrain exaggeration. Elevation provided
* by getXXXX methods is multiplied by this.
*/
exaggeration() {
throw new Error("Pure virtual method called.");
}
/**
* Lookup DEM tile that corresponds to (covers) tileID.
* @private
*/
findDEMTileFor(_) {
throw new Error("Pure virtual method called.");
}
/**
* Get list of DEM tiles used to render current frame.
* @private
*/
get visibleDemTiles() {
throw new Error("Getter must be implemented in subclass.");
}
/**
* Get elevation minimum and maximum for tiles which are visible on the current frame.
*/
getMinMaxForVisibleTiles() {
const visibleTiles = this.visibleDemTiles;
if (visibleTiles.length === 0) {
return null;
}
let found = false;
let min = Number.MAX_VALUE;
let max = Number.MIN_VALUE;
for (const tile of visibleTiles) {
const minmax = this.getMinMaxForTile(tile.tileID);
if (!minmax) {
continue;
}
min = Math.min(min, minmax.min);
max = Math.max(max, minmax.max);
found = true;
}
if (!found) {
return null;
}
return { min, max };
}
}
class DEMSampler {
constructor(demTile, scale, offset) {
this._demTile = demTile;
this._dem = this._demTile.dem;
this._scale = scale;
this._offset = offset;
}
static create(elevation, tileID, useDemTile) {
const demTile = useDemTile || elevation.findDEMTileFor(tileID);
if (!(demTile && demTile.dem)) {
return;
}
const dem = demTile.dem;
const demTileID = demTile.tileID;
const scale = 1 << tileID.canonical.z - demTileID.canonical.z;
const xOffset = (tileID.canonical.x / scale - demTileID.canonical.x) * dem.dim;
const yOffset = (tileID.canonical.y / scale - demTileID.canonical.y) * dem.dim;
const k = dem.dim / EXTENT / scale;
return new DEMSampler(demTile, k, [xOffset, yOffset]);
}
tileCoordToPixel(x, y) {
const px = x * this._scale + this._offset[0];
const py = y * this._scale + this._offset[1];
const i = Math.floor(px);
const j = Math.floor(py);
return new Point(i, j);
}
getElevationAt(x, y, interpolated, clampToEdge) {
const px = x * this._scale + this._offset[0];
const py = y * this._scale + this._offset[1];
const i = Math.floor(px);
const j = Math.floor(py);
const dem = this._dem;
clampToEdge = !!clampToEdge;
return interpolated ? number(
number(dem.get(i, j, clampToEdge), dem.get(i, j + 1, clampToEdge), py - j),
number(dem.get(i + 1, j, clampToEdge), dem.get(i + 1, j + 1, clampToEdge), py - j),
px - i
) : dem.get(i, j, clampToEdge);
}
getElevationAtPixel(x, y, clampToEdge) {
return this._dem.get(x, y, !!clampToEdge);
}
getMeterToDEM(lat) {
return (1 << this._demTile.tileID.canonical.z) * mercatorZfromAltitude(1, lat) * this._dem.stride;
}
}
const lookup = new Float32Array(512 * 512);
const passLookup = new Uint8Array(512 * 512);
function getNodeHeight(node) {
let height = 0;
if (node.meshes) {
for (const mesh of node.meshes) {
height = Math.max(height, mesh.aabb.max[2]);
}
}
if (node.children) {
for (const child of node.children) {
height = Math.max(height, getNodeHeight(child));
}
}
return height;
}
function addAABBsToGridIndex(node, key, grid) {
if (node.meshes) {
for (const mesh of node.meshes) {
if (mesh.aabb.min[0] === Infinity) continue;
const meshAabb = Aabb.applyTransform(mesh.aabb, node.globalMatrix);
grid.insert(key, meshAabb.min[0], meshAabb.min[1], meshAabb.max[0], meshAabb.max[1]);
}
}
if (node.children) {
for (const child of node.children) {
addAABBsToGridIndex(child, key, grid);
}
}
}
const PartIndices = {
wall: 1,
door: 2,
roof: 3,
window: 4,
lamp: 5,
logo: 6
};
const PartNames = ["", "wall", "door", "roof", "window", "lamp", "logo"];
class Tiled3dModelFeature {
constructor(node) {
this.node = node;
this.evaluatedRMEA = [
[1, 0, 0, 1],
[1, 0, 0, 1],
// wall
[1, 0, 0, 1],
// door
[1, 0, 0, 1],
// roof
[0.4, 1, 0, 1],
// window
[1, 0, 0, 1],
// lamp
[1, 0, 0, 1]
];
this.hiddenByReplacement = false;
this.evaluatedTranslation = [0, 0, 0];
this.evaluatedScale = [1, 1, 1];
this.evaluatedColor = [];
this.emissionHeightBasedParams = [];
this.cameraCollisionOpacity = 1;
this.feature = { type: "Point", id: node.id, geometry: [], properties: { "height": getNodeHeight(node) } };
this.aabb = this._getLocalBounds();
this.state = null;
}
_getLocalBounds() {
if (!this.node.meshes) {
return new Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
}
if (!this.aabb) {
let i = 0;
const aabb = new Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
for (const mesh of this.node.meshes) {
if (this.node.lightMeshIndex !== i) {
mesh.transformedAabb = Aabb.applyTransformFast(mesh.aabb, this.node.globalMatrix);
aabb.encapsulate(mesh.transformedAabb);
}
i++;
}
this.aabb = aabb;
}
return this.aabb;
}
}
class Tiled3dModelBucket {
constructor(layers, nodes, id, hasMbxMeshFeatures, hasMeshoptCompression, brightness, featureIndex, worldview) {
this.id = id;
this.layers = layers;
this.layerIds = this.layers.map((layer) => layer.fqid);
this.stateDependentLayerIds = this.layers.filter((l) => l.isStateDependent()).map((l) => l.id);
this.modelTraits |= ModelTraits.CoordinateSpaceTile;
this.uploaded = false;
this.hasPattern = false;
if (hasMbxMeshFeatures) {
this.modelTraits |= ModelTraits.HasMapboxMeshFeatures;
}
if (hasMeshoptCompression) {
this.modelTraits |= ModelTraits.HasMeshoptCompression;
}
this.zoom = -1;
this.terrainExaggeration = 1;
this.projection = { name: "mercator" };
this.replacementUpdateTime = 0;
this.elevationReadFromZ = 255;
this.brightness = brightness;
this.worldview = worldview;
this.dirty = true;
this.needsUpload = false;
this.filter = null;
this.nodesInfo = [];
for (const node of nodes) {
this.nodesInfo.push(new Tiled3dModelFeature(node));
addAABBsToGridIndex(node, featureIndex.featureIndexArray.length, featureIndex.grid);
featureIndex.featureIndexArray.emplaceBack(this.nodesInfo.length - 1, 0, featureIndex.bucketLayerIDs.length - 1, 0);
}
this.states = {};
this.hasAppearances = null;
}
updateFootprints(id, footprints) {
for (const nodeInfo of this.getNodesInfo()) {
const node = nodeInfo.node;
if (!node.footprint) {
continue;
}
footprints.push({
footprint: node.footprint,
id
});
}
}
updateAppearances(_canonical, _featureState, _availableImages, _globalProperties) {
}
update(states) {
const withStateUpdates = Object.keys(states).length !== 0;
if (withStateUpdates && !this.stateDependentLayers.length) return;
const layers = withStateUpdates ? this.stateDependentLayers : this.layers;
if (!deepEqual(states, this.states)) {
for (const layer of layers) {
this.evaluate(layer, states);
}
}
this.states = structuredClone(states);
}
populate() {
console.log("populate 3D model bucket");
}
uploadPending() {
return !this.uploaded || this.needsUpload;
}
upload(context) {
if (!this.needsUpload) return;
const nodesInfo = this.getNodesInfo();
for (const nodeInfo of nodesInfo) {
const node = nodeInfo.node;
if (this.uploaded) {
this.updatePbrBuffer(node);
continue;
}
uploadNode(node, context, true);
}
for (const nodeInfo of nodesInfo) {
destroyNodeArrays(nodeInfo.node);
}
this.uploaded = true;
this.needsUpload = false;
}
updatePbrBuffer(node) {
let result = false;
if (!node.meshes) return result;
for (const mesh of node.meshes) {
if (mesh.pbrBuffer) {
mesh.pbrBuffer.updateData(mesh.featureArray);
result = true;
}
}
return result;
}
needsReEvaluation(painter, zoom, layer) {
const projection = painter.transform.projectionOptions;
const calculatedBrightness = painter.style.getBrightness();
const brightnessChanged = this.brightness !== calculatedBrightness;
if (!this.uploaded || this.dirty || projection.name !== this.projection.name || expressionRequiresReevaluation(layer.paint.get("model-color").value, brightnessChanged) || expressionRequiresReevaluation(layer.paint.get("model-color-mix-intensity").value, brightnessChanged) || expressionRequiresReevaluation(layer.paint.get("model-roughness").value, brightnessChanged) || expressionRequiresReevaluation(layer.paint.get("model-emissive-strength").value, brightnessChanged) || expressionRequiresReevaluation(layer.paint.get("model-height-based-emissive-strength-multiplier").value, brightnessChanged)) {
this.projection = projection;
this.brightness = calculatedBrightness;
const nodesInfo = this.getNodesInfo();
for (const nodeInfo of nodesInfo) {
nodeInfo.state = null;
}
return true;
}
return false;
}
evaluateTransform(painter, layer) {
if (painter.transform.zoom === this.zoom) return;
this.zoom = painter.transform.zoom;
const nodesInfo = this.getNodesInfo();
const canonical = this.id.canonical;
for (const nodeInfo of nodesInfo) {
const evaluationFeature = nodeInfo.feature;
nodeInfo.evaluatedTranslation = layer.paint.get("model-translation").evaluate(evaluationFeature, {}, canonical);
nodeInfo.evaluatedScale = layer.paint.get("model-scale").evaluate(evaluationFeature, {}, canonical);
}
}
evaluate(layer, states) {
const nodesInfo = this.getNodesInfo();
for (const nodeInfo of nodesInfo) {
if (!nodeInfo.node.meshes) continue;
const evaluationFeature = nodeInfo.feature;
const state = states && states[evaluationFeature.id];
if (deepEqual(state, nodeInfo.state)) continue;
nodeInfo.state = structuredClone(state);
const hasFeatures = nodeInfo.node.meshes && nodeInfo.node.meshes[0].featureData;
const previousDoorColor = nodeInfo.evaluatedColor[PartIndices.door];
const previousDoorRMEA = nodeInfo.evaluatedRMEA[PartIndices.door];
const canonical = this.id.canonical;
nodeInfo.hasTranslucentParts = false;
if (hasFeatures) {
for (let i = 0; i < PartNames.length; i++) {
const part = PartNames[i];
if (part.length) {
evaluationFeature.properties["part"] = part;
}
const color = layer.paint.get("model-color").evaluate(evaluationFeature, state, canonical).toPremultipliedRenderColor(null);
const colorMixIntensity = layer.paint.get("model-color-mix-intensity").evaluate(evaluationFeature, state, canonical);
nodeInfo.evaluatedColor[i] = [color.r, color.g, color.b, colorMixIntensity];
nodeInfo.evaluatedRMEA[i][0] = layer.paint.get("model-roughness").evaluate(evaluationFeature, state, canonical);
nodeInfo.evaluatedRMEA[i][2] = layer.paint.get("model-emissive-strength").evaluate(evaluationFeature, state, canonical);
nodeInfo.evaluatedRMEA[i][3] = color.a;
nodeInfo.emissionHeightBasedParams[i] = layer.paint.get("model-height-based-emissive-strength-multiplier").evaluate(evaluationFeature, state, canonical);
if (!nodeInfo.hasTranslucentParts && color.a < 1) {
nodeInfo.hasTranslucentParts = true;
}
}
delete evaluationFeature.properties["part"];
const doorLightChanged = previousDoorColor !== nodeInfo.evaluatedColor[PartIndices.door] || previousDoorRMEA !== nodeInfo.evaluatedRMEA[PartIndices.door];
updateNodeFeatureVertices(nodeInfo, doorLightChanged, this.modelTraits);
} else {
nodeInfo.evaluatedRMEA[0][2] = layer.paint.get("model-emissive-strength").evaluate(evaluationFeature, state, canonical);
}
nodeInfo.evaluatedTranslation = layer.paint.get("model-translation").evaluate(evaluationFeature, state, canonical);
nodeInfo.evaluatedScale = layer.paint.get("model-scale").evaluate(evaluationFeature, state, canonical);
if (!this.updatePbrBuffer(nodeInfo.node)) {
this.needsUpload = true;
}
}
this.dirty = false;
}
elevationUpdate(terrain, exaggeration, coord, source) {
assert$1(terrain);
const demTile = terrain.findDEMTileFor(coord);
if (!demTile) return;
if (demTile.tileID.canonical === this.terrainTile && exaggeration === this.terrainExaggeration) return;
if (demTile.dem && demTile.tileID.overscaledZ !== this.elevationReadFromZ) {
this.elevationReadFromZ = demTile.tileID.overscaledZ;
const dem = DEMSampler.create(terrain, coord, demTile);
if (!dem) return;
if (this.modelTraits & ModelTraits.HasMapboxMeshFeatures) {
this.updateDEM(terrain, dem, coord, source);
}
for (const nodeInfo of this.getNodesInfo()) {
const node = nodeInfo.node;
if (!node.footprint || !node.footprint.vertices || !node.footprint.vertices.length) {
continue;
}
const vertices = node.footprint.vertices;
let elevation = dem.getElevationAt(vertices[0].x, vertices[0].y, true, true);
for (let i = 1; i < vertices.length; i++) {
elevation = Math.min(elevation, dem.getElevationAt(vertices[i].x, vertices[i].y, true, true));
}
node.elevation = elevation;
}
}
this.terrainTile = demTile.tileID.canonical;
this.terrainExaggeration = exaggeration;
}
updateDEM(terrain, dem, coord, source) {
let tiles = dem._dem._modifiedForSources[source];
if (tiles === void 0) {
dem._dem._modifiedForSources[source] = [];
tiles = dem._dem._modifiedForSources[source];
}
if (tiles.includes(coord.canonical)) {
return;
}
const demRes = dem._dem.dim;
tiles.push(coord.canonical);
assert$1(lookup.length <= demRes * demRes);
let changed = false;
for (const nodeInfo of this.getNodesInfo()) {
const node = nodeInfo.node;
if (!node.footprint || !node.footprint.grid) {
continue;
}
const grid = node.footprint.grid;
const minDem = dem.tileCoordToPixel(grid.min.x, grid.min.y);
const maxDem = dem.tileCoordToPixel(grid.max.x, grid.max.y);
const distanceToBorder = Math.min(Math.min(demRes - maxDem.y, minDem.x), Math.min(minDem.y, demRes - maxDem.x));
if (distanceToBorder < 0) {
continue;
}
const demAtt = clamp(distanceToBorder, 2, 5);
let minx = Math.max(0, minDem.x - demAtt);
let miny = Math.max(0, minDem.y - demAtt);
let maxx = Math.min(maxDem.x + demAtt, demRes - 1);
let maxy = Math.min(maxDem.y + demAtt, demRes - 1);
for (let y = miny; y <= maxy; ++y) {
for (let x = minx; x <= maxx; ++x) {
passLookup[y * demRes + x] = 255;
}
}
let heightAcc = 0;
let count = 0;
for (let celly = 0; celly < grid.cellsY; ++celly) {
for (let cellx = 0; cellx < grid.cellsX; ++cellx) {
const cell = grid.cells[celly * grid.cellsX + cellx];
if (!cell) {
continue;
}
const demP = dem.tileCoordToPixel(grid.min.x + cellx / grid.xScale, grid.min.y + celly / grid.yScale);
const demPMax = dem.tileCoordToPixel(grid.min.x + (cellx + 1) / grid.xScale, grid.min.y + (celly + 1) / grid.yScale);
for (let y = demP.y; y <= Math.min(demPMax.y + 1, demRes - 1); ++y) {
for (let x = demP.x; x <= Math.min(demPMax.x + 1, demRes - 1); ++x) {
if (passLookup[y * demRes + x] === 255) {
passLookup[y * demRes + x] = 0;
const height = dem.getElevationAtPixel(x, y);
heightAcc += height;
count++;
}
}
}
}
}
assert$1(count);
const avgHeight = heightAcc / count;
minx = Math.max(1, minDem.x - demAtt);
miny = Math.max(1, minDem.y - demAtt);
maxx = Math.min(maxDem.x + demAtt, demRes - 2);
maxy = Math.min(maxDem.y + demAtt, demRes - 2);
changed = true;
for (let y = miny; y <= maxy; ++y) {
for (let x = minx; x <= maxx; ++x) {
if (passLookup[y * demRes + x] === 0) {
lookup[y * demRes + x] = dem._dem.set(x, y, avgHeight);
}
}
}
for (let p = 1; p < demAtt; ++p) {
minx = Math.max(1, minDem.x - p);
miny = Math.max(1, minDem.y - p);
maxx = Math.min(maxDem.x + p, demRes - 2);
maxy = Math.min(maxDem.y + p, demRes - 2);
for (let y = miny; y <= maxy; ++y) {
for (let x = minx; x <= maxx; ++x) {
const indexThis = y * demRes + x;
if (passLookup[indexThis] === 255) {
let maxDiff = 0;
let maxDiffAbs = 0;
let xoffset = -1;
let yoffset = -1;
for (let j = -1; j <= 1; ++j) {
for (let i = -1; i <= 1; ++i) {
const index = (y + j) * demRes + x + i;
if (passLookup[index] >= p) {
continue;
}
const diff = lookup[index];
const diffAbs = Math.abs(diff);
if (diffAbs > maxDiffAbs) {
maxDiff = diff;
maxDiffAbs = diffAbs;
xoffset = i;
yoffset = j;
}
}
}
if (maxDiffAbs > 0.1) {
const diagonalAttenuation = Math.abs(xoffset * yoffset) * 0.5;
const attenuation = 1 - (p + diagonalAttenuation) / demAtt;
assert$1(attenuation > 0);
const prev = dem._dem.get(x, y);
let next = prev + maxDiff * attenuation;
const parent = dem._dem.get(x + xoffset, y + yoffset);
const child = dem._dem.get(x - xoffset, y - yoffset, true);
if ((next - parent) * (next - child) > 0) {
next = (parent + child) / 2;
}
lookup[indexThis] = dem._dem.set(x, y, next);
passLookup[indexThis] = p;
}
}
}
}
}
}
if (changed) {
dem._demTile.needsDEMTextureUpload = true;
dem._dem._timestamp = exported$1.now();
}
}
setFilter(filterSpec) {
this.filter = filterSpec ? createFilter(filterSpec) : null;
}
getNodesInfo() {
if (this.filter) {
return this.nodesInfo.filter((node) => {
return this.filter.filter(new EvaluationParameters(this.id.overscaledZ, { worldview: this.worldview }), node.feature, this.id.canonical);
});
}
return this.nodesInfo;
}
destroy() {
const nodesInfo = this.getNodesInfo();
for (const nodeInfo of nodesInfo) {
destroyNodeArrays(nodeInfo.node);
destroyBuffers(nodeInfo.node);
}
}
isEmpty() {
return !this.nodesInfo.length;
}
updateReplacement(coord, source) {
if (source.updateTime === this.replacementUpdateTime) {
return;
}
this.replacementUpdateTime = source.updateTime;
const activeReplacements = source.getReplacementRegionsForTile(coord.toUnwrapped());
for (const nodeInfo of this.getNodesInfo()) {
const footprint = nodeInfo.node.footprint;
nodeInfo.hiddenByReplacement = !!footprint && !activeReplacements.find((region) => region.footprint === footprint);
}
}
getHeightAtTileCoord(x, y) {
const candidates = [];
const tmpVertex = [0, 0, 0];
const nodeInverse = identity$3([]);
for (const nodeInfo of this.getNodesInfo()) {
assert$1(nodeInfo.node.meshes.length > 0);
const mesh = nodeInfo.node.meshes[0];
const meshAabb = mesh.transformedAabb;
if (x < meshAabb.min[0] || y < meshAabb.min[1] || x > meshAabb.max[0] || y > meshAabb.max[1]) continue;
if (nodeInfo.node.hidden === true) return { height: Infinity, maxHeight: nodeInfo.feature.properties["height"], hidden: false, verticalScale: nodeInfo.evaluatedScale[2] };
assert$1(mesh.heightmap);
invert$2(nodeInverse, nodeInfo.node.globalMatrix);
tmpVertex[0] = x;
tmpVertex[1] = y;
transformMat4$2(tmpVertex, tmpVertex, nodeInverse);
const xCell = (tmpVertex[0] - mesh.aabb.min[0]) / (mesh.aabb.max[0] - mesh.aabb.min[0]) * HEIGHTMAP_DIM | 0;
const yCell = (tmpVertex[1] - mesh.aabb.min[1]) / (mesh.aabb.max[1] - mesh.aabb.min[1]) * HEIGHTMAP_DIM | 0;
const heightmapIndex = Math.min(HEIGHTMAP_DIM - 1, yCell) * HEIGHTMAP_DIM + Math.min(HEIGHTMAP_DIM - 1, xCell);
const heightValue = mesh.heightmap[heightmapIndex];
if (heightValue < 0 && nodeInfo.node.footprint) {
nodeInfo.node.footprint.grid.query(new Point(x, y), new Point(x, y), candidates);
if (candidates.length > 0) {
return { height: void 0, maxHeight: nodeInfo.feature.properties["height"], hidden: nodeInfo.hiddenByReplacement, verticalScale: nodeInfo.evaluatedScale[2] };
}
continue;
}
if (nodeInfo.hiddenByReplacement) return;
return { height: heightValue, maxHeight: nodeInfo.feature.properties["height"], hidden: false, verticalScale: nodeInfo.evaluatedScale[2] };
}
}
}
function expressionRequiresReevaluation(e, brightnessChanged) {
assert$1(e.kind === "constant" || e instanceof ZoomConstantExpression);
if (e instanceof ZoomConstantExpression) {
return !e.isLightConstant && brightnessChanged;
}
return false;
}
function encodeEmissionToByte(emission) {
const clampedEmission = clamp(emission, 0, 2);
return Math.min(Math.round(0.5 * clampedEmission * 255), 255);
}
function addPBRVertex(vertexArray, color, colorMix, rmea, heightBasedEmissionMultiplierParams, zMin, zMax, lightsFeatureArray) {
let r = (color & 61440 | (color & 61440) >> 4) >> 8;
let g = (color & 3840 | (color & 3840) >> 4) >> 4;
let b = color & 240 | (color & 240) >> 4;
if (colorMix[3] > 0) {
r = number(r, 255 * colorMix[0], colorMix[3]);
g = number(g, 255 * colorMix[1], colorMix[3]);
b = number(b, 255 * colorMix[2], colorMix[3]);
}
const a0 = r << 8 | g;
const a1 = b << 8 | Math.floor(rmea[3] * 255);
const a2 = encodeEmissionToByte(rmea[2]) << 8 | rmea[0] * 15 << 4 | rmea[1] * 15;
const emissionMultiplierStart = clamp(heightBasedEmissionMultiplierParams[0], 0, 1);
const emissionMultiplierFinish = clamp(heightBasedEmissionMultiplierParams[1], 0, 1);
const emissionMultiplierValueStart = clamp(heightBasedEmissionMultiplierParams[2], 0, 1);
const emissionMultiplierValueFinish = clamp(heightBasedEmissionMultiplierParams[3], 0, 1);
let a3, b0, b1, b2;
if (emissionMultiplierStart !== emissionMultiplierFinish && zMax !== zMin && emissionMultiplierFinish !== emissionMultiplierStart) {
const zRange = zMax - zMin;
b0 = 1 / (zRange * (emissionMultiplierFinish - emissionMultiplierStart));
b1 = -(zMin + zRange * emissionMultiplierStart) / (zRange * (emissionMultiplierFinish - emissionMultiplierStart));
const power = clamp(heightBasedEmissionMultiplierParams[4], -1, 1);
b2 = Math.pow(10, power);
a3 = emissionMultiplierValueStart * 255 << 8 | emissionMultiplierValueFinish * 255;
} else {
a3 = 255 << 8 | 255;
b0 = 0;
b1 = 1;
b2 = 1;
}
vertexArray.emplaceBack(a0, a1, a2, a3, b0, b1, b2);
if (lightsFeatureArray) {
const size = lightsFeatureArray.length;
lightsFeatureArray.clear();
for (let j = 0; j < size; j++) {
lightsFeatureArray.emplaceBack(a0, a1, a2, a3, b0, b1, b2);
}
}
}
function updateNodeFeatureVertices(nodeInfo, doorLightChanged, modelTraits) {
const node = nodeInfo.node;
let i = 0;
const isV2Tile = modelTraits & ModelTraits.HasMeshoptCompression;
for (const mesh of node.meshes) {
if (node.lights && node.lightMeshIndex === i) continue;
if (!mesh.featureData) continue;
mesh.featureArray = new StructArrayLayout4ui3f20();
mesh.featureArray.reserve(mesh.featureData.length);
let pendingDoorLightUpdate = doorLightChanged;
for (const feature of mesh.featureData) {
const featureColor = isV2Tile ? feature & 65535 : feature >> 16 & 65535;
const id = isV2Tile ? feature >> 16 & 65535 : feature & 65535;
const partId = (id & 15) < 8 ? id & 15 : 0;
const rmea = nodeInfo.evaluatedRMEA[partId];
const evaluatedColor = nodeInfo.evaluatedColor[partId];
const emissionParams = nodeInfo.emissionHeightBasedParams[partId];
let lightsFeatureArray;
if (pendingDoorLightUpdate && partId === PartIndices.door && node.lights) {
lightsFeatureArray = new StructArrayLayout4ui3f20();
lightsFeatureArray.resize(node.lights.length * 10);
}
addPBRVertex(mesh.featureArray, featureColor, evaluatedColor, rmea, emissionParams, mesh.aabb.min[2], mesh.aabb.max[2], lightsFeatureArray);
if (lightsFeatureArray && pendingDoorLightUpdate) {
pendingDoorLightUpdate = false;
const lightsMesh = node.meshes[node.lightMeshIndex];
lightsMesh.featureArray = lightsFeatureArray;
lightsMesh.featureArray._trim();
}
}
mesh.featureArray._trim();
i++;
}
}
register(Tiled3dModelBucket, "Tiled3dModelBucket", { omit: ["layers"] });
register(Tiled3dModelFeature, "Tiled3dModelFeature");
const customProps = ["id", "tile", "layer", "source", "sourceLayer", "state"];
class Feature {
constructor(vectorTileFeature, z, x, y, id) {
this.type = "Feature";
this._vectorTileFeature = vectorTileFeature;
this._z = z;
this._x = x;
this._y = y;
this.properties = vectorTileFeature ? vectorTileFeature.properties : {};
this.id = id;
}
clone() {
const feature = new Feature(this._vectorTileFeature, this._z, this._x, this._y, this.id);
if (this.state) feature.state = Object.assign({}, this.state);
if (this.layer) feature.layer = Object.assign({}, this.layer);
if (this.source) feature.source = this.source;
if (this.sourceLayer) feature.sourceLayer = this.sourceLayer;
return feature;
}
get geometry() {
if (this._geometry === void 0 && this._vectorTileFeature) {
this._geometry = this._vectorTileFeature.toGeoJSON(this._x, this._y, this._z).geometry;
}
return this._geometry;
}
set geometry(g) {
this._geometry = g;
}
toJSON() {
const json = {
type: "Feature",
state: void 0,
geometry: this.geometry,
properties: this.properties
};
for (const key of customProps) {
if (this[key] !== void 0) json[key] = this[key];
}
return json;
}
}
class TargetFeature extends Feature {
/**
* @private
*/
constructor(feature, variant) {
super(feature._vectorTileFeature, feature._z, feature._x, feature._y, feature.id);
if (feature.state) this.state = Object.assign({}, feature.state);
this.target = variant.target;
this.namespace = variant.namespace;
if (variant.properties) this.properties = variant.properties;
if (this.target && ("featuresetId" in this.target && !this.target.importId || "layerId" in this.target)) {
this.source = feature.source;
this.sourceLayer = feature.sourceLayer;
this.layer = feature.layer;
}
}
toJSON() {
const json = super.toJSON();
json.target = this.target;
json.namespace = this.namespace;
return json;
}
}
class ModelSource extends Evented {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super();
this.id = id;
this.type = "model";
this.models = [];
this._loaded = false;
this._options = options;
this._modelsInfo = /* @__PURE__ */ new Map();
}
load() {
const modelPromises = [];
for (const modelId in this._options.models) {
const modelSpec = this._options.models[modelId];
const modelInfo = this._modelsInfo.get(modelId);
if (modelInfo) {
const model = modelInfo.model;
model.position = modelSpec.position != null ? new LngLat(modelSpec.position[0], modelSpec.position[1]) : new LngLat(0, 0);
model.orientation = modelSpec.orientation != null ? modelSpec.orientation : [0, 0, 0];
modelInfo.modelSpec = modelSpec;
ModelSource.applyModelSpecification(model, modelSpec);
model.computeBoundsAndApplyParent();
this.models.push(model);
} else {
const modelPromise = loadGLTF(this.map._requestManager.transformRequest(modelSpec.uri, ResourceType.Model).url).then((gltf) => {
if (!gltf) return;
const nodes = convertModel(gltf);
const model = new Model(modelId, modelSpec.uri, modelSpec.position, modelSpec.orientation, nodes);
ModelSource.applyModelSpecification(model, modelSpec);
model.computeBoundsAndApplyParent();
this.models.push(model);
this._modelsInfo.set(modelId, {
modelSpec,
model
});
}).catch((err) => {
this.fire(new ErrorEvent(new Error(`Could not load model ${modelId} from ${modelSpec.uri}: ${err.message}`)));
});
modelPromises.push(modelPromise);
}
}
Promise.allSettled(modelPromises).then(() => {
this._loaded = true;
this.fire(new Event("data", { dataType: "source", sourceDataType: "metadata" }));
}).catch((err) => {
this._loaded = true;
this.fire(new ErrorEvent(new Error(`Could not load models: ${err.message}`)));
});
}
static applyModelSpecification(model, modelSpec) {
if (modelSpec.nodeOverrides) {
ModelSource.convertNodeOverrides(model, modelSpec.nodeOverrides);
}
if (modelSpec.materialOverrides) {
ModelSource.convertMaterialOverrides(model, modelSpec.materialOverrides);
}
if (modelSpec.nodeOverrideNames) {
model.nodeOverrideNames = [...modelSpec.nodeOverrideNames];
}
if (modelSpec.materialOverrideNames) {
model.materialOverrideNames = [...modelSpec.materialOverrideNames];
}
if (modelSpec.featureProperties) {
model.featureProperties = modelSpec.featureProperties;
}
}
static convertNodeOverrides(model, overrides) {
if (Array.isArray(overrides) && overrides.every((item) => typeof item === "string")) {
model.nodeOverrideNames = [];
for (const nodeName of overrides) {
model.nodeOverrideNames.push(nodeName);
}
return;
}
Object.entries(overrides).forEach(([key, value]) => {
const nodeOverride = {
orientation: [0, 0, 0]
};
if (value.hasOwnProperty("orientation")) {
const orientation = value["orientation"];
if (orientation) {
nodeOverride.orientation = orientation;
}
}
model.nodeOverrides.set(key, nodeOverride);
});
}
static convertMaterialOverrides(model, overrides) {
if (Array.isArray(overrides) && overrides.every((item) => typeof item === "string")) {
model.materialOverrideNames = [];
for (const materialName of overrides) {
model.materialOverrideNames.push(materialName);
}
return;
}
Object.entries(overrides).forEach(([key, value]) => {
const materialOverride = {
color: new Color(1, 1, 1),
colorMix: 0,
emissionStrength: 0,
opacity: 1
};
const modelColor = value["model-color"];
if (modelColor !== void 0) {
materialOverride.color.r = modelColor[0];
materialOverride.color.g = modelColor[1];
materialOverride.color.b = modelColor[2];
}
const modelColorMixIntensity = value["model-color-mix-intensity"];
if (modelColorMixIntensity !== void 0) {
materialOverride.colorMix = modelColorMixIntensity;
}
const modelEmissiveStrength = value["model-emissive-strength"];
if (modelEmissiveStrength !== void 0) {
materialOverride.emissionStrength = modelEmissiveStrength;
}
const modelOpacity = value["model-opacity"];
if (modelOpacity !== void 0) {
materialOverride.opacity = modelOpacity;
}
model.materialOverrides.set(key, materialOverride);
});
}
onAdd(map) {
this.map = map;
this.load();
}
hasTransition() {
return false;
}
loaded() {
return this._loaded;
}
getModels() {
return this.models;
}
loadTile(tile, callback) {
}
serialize() {
return this._options;
}
setProperty(property, value) {
return false;
}
reload() {
const fqid = makeFQID(this.id, this.scope);
this.map.style.clearSource(fqid);
this.models = [];
this._modelsInfo.clear();
this._loaded = false;
this.load();
}
/**
* Sets the list of models along with their properties.
*
* Updates are efficient as long as the model URIs remain unchanged.
* @param {ModelSourceModelsSpecification} modelSpecs Model specifications according to [Style Specification](https://www.mapbox.com/mapbox-gl-style-spec/#sources-model).
* @example
* map.getSource('some id').setModels({
* "model-1" : {
* "uri": "model_1.glb",
* "position": [-74.0135, 40.7153],
* "orientation": [0, 0, 0]
* }
* });
*/
setModels(modelSpecs) {
this.models = [];
const updatedModelsInfo = /* @__PURE__ */ new Map();
for (const modelId in modelSpecs) {
const modelSpec = modelSpecs[modelId];
if (this._modelsInfo.has(modelId)) {
const entry = this._modelsInfo.get(modelId);
if (entry && entry.modelSpec.uri === modelSpec.uri) {
updatedModelsInfo.set(modelId, entry);
}
}
}
this._modelsInfo = updatedModelsInfo;
this._options.models = modelSpecs;
this._loaded = false;
this.load();
}
}
class ModelStyleLayer extends StyleLayer {
constructor(layer, scope, lut, options) {
const properties = {
layout: getLayoutProperties(),
paint: getPaintProperties()
};
super(layer, properties, scope, lut, options);
this.layer = layer;
this._stats = { numRenderedVerticesInShadowPass: 0, numRenderedVerticesInTransparentPass: 0 };
}
createBucket(parameters) {
return new ModelBucket(parameters);
}
getProgramIds() {
return ["model"];
}
is3D(terrainEnabled) {
return true;
}
hasShadowPass() {
return true;
}
canCastShadows() {
return true;
}
hasLightBeamPass() {
return true;
}
cutoffRange() {
return this.paint.get("model-cutoff-fade-range");
}
queryRadius(bucket) {
return bucket instanceof Tiled3dModelBucket ? EXTENT - 1 : 0;
}
queryRenderedFeatures(queryGeometry, sourceCache, transform) {
const source = sourceCache.getSource();
if (!source || !(source instanceof ModelSource)) return {};
const modelSource = source;
const result = {};
result[this.id] = [];
const layerResult = result[this.id];
let modelFeatureIndex = 0;
for (const model of modelSource.models) {
const modelFeatureState = sourceCache.getFeatureState(this.sourceLayer, model.id);
const modelFeatureForEval = {
type: "Unknown",
id: model.id,
properties: model.featureProperties
};
const rotation = this.paint.get("model-rotation").evaluate(modelFeatureForEval, modelFeatureState);
const scale = this.paint.get("model-scale").evaluate(modelFeatureForEval, modelFeatureState);
const translation = this.paint.get("model-translation").evaluate(modelFeatureForEval, modelFeatureState);
const elevationReference = this.paint.get("model-elevation-reference");
const shouldFollowTerrainSlope = elevationReference === "ground";
const shouldApplyElevation = elevationReference === "ground";
let matrix = [];
calculateModelMatrix(
matrix,
model,
transform,
model.position,
rotation,
scale,
translation,
shouldApplyElevation,
shouldFollowTerrainSlope,
false
);
if (transform.projection.name === "globe") {
matrix = convertModelMatrixForGlobe(matrix, transform);
}
const worldViewProjection = multiply$5([], transform.projMatrix, matrix);
const projectedQueryGeometry = queryGeometry.isPointQuery() ? queryGeometry.screenBounds : queryGeometry.screenGeometry;
const depth = queryGeometryIntersectsProjectedAabb(projectedQueryGeometry, transform, worldViewProjection, model.aabb);
if (depth != null) {
const modelFeature = new Feature(void 0, 0, 0, 0, model.id);
modelFeature.layer = this.layer;
modelFeature.properties = structuredClone(model.featureProperties);
modelFeature.properties["layer"] = this.id;
modelFeature.properties["uri"] = model.uri;
modelFeature.properties["orientation"] = model.orientation;
modelFeature.sourceLayer = this.sourceLayer;
modelFeature.geometry = {
type: "Point",
coordinates: [model.position.lng, model.position.lat]
};
modelFeature.state = modelFeatureState;
modelFeature.source = this.source;
layerResult.push({ featureIndex: modelFeatureIndex, feature: modelFeature, intersectionZ: depth });
}
++modelFeatureIndex;
}
return result;
}
queryIntersectsFeature(queryGeometry, feature, featureState, geometry, zoom, transform) {
if (!this.modelManager) return false;
const modelManager = this.modelManager;
const bucket = queryGeometry.tile.getBucket(this);
if (!bucket || !(bucket instanceof ModelBucket)) return false;
for (const modelId in bucket.instancesPerModel) {
const instances = bucket.instancesPerModel[modelId];
const featureId = feature.id !== void 0 ? feature.id : feature.properties && feature.properties.hasOwnProperty("id") ? feature.properties["id"] : void 0;
if (instances.idToFeaturesIndex.hasOwnProperty(featureId)) {
const modelFeature = instances.features[instances.idToFeaturesIndex[featureId]];
const model = modelManager.getModel(modelId, this.scope);
if (!model) return false;
let matrix = [];
const position = new LngLat(0, 0);
const id = bucket.canonical;
let minDepth = Number.MAX_VALUE;
for (let i = 0; i < modelFeature.instancedDataCount; ++i) {
const instanceOffset = modelFeature.instancedDataOffset + i;
const offset = instanceOffset * 16;
const va = instances.instancedDataArray.float32;
const translation = [va[offset + 4], va[offset + 5], va[offset + 6]];
const pointX = Math.floor(va[offset]);
const pointY = Math.floor(va[offset + 1]);
tileToLngLat(id, position, pointX, pointY);
calculateModelMatrix(
matrix,
model,
transform,
position,
modelFeature.rotation,
modelFeature.scale,
translation,
false,
false,
false
);
if (transform.projection.name === "globe") {
matrix = convertModelMatrixForGlobe(matrix, transform);
}
const worldViewProjection = multiply$5([], transform.projMatrix, matrix);
const screenQuery = queryGeometry.queryGeometry;
const projectedQueryGeometry = screenQuery.isPointQuery() ? screenQuery.screenBounds : screenQuery.screenGeometry;
const depth = queryGeometryIntersectsProjectedAabb(projectedQueryGeometry, transform, worldViewProjection, model.aabb);
if (depth != null) {
minDepth = Math.min(depth, minDepth);
}
}
if (minDepth !== Number.MAX_VALUE) {
return minDepth;
}
return false;
}
}
return false;
}
_handleOverridablePaintPropertyUpdate(name, oldValue, newValue) {
if (!this.layout || oldValue.isDataDriven() || newValue.isDataDriven()) {
return false;
}
return name === "model-color" || name === "model-color-mix-intensity" || name === "model-rotation" || name === "model-scale" || name === "model-translation" || name === "model-emissive-strength";
}
_isPropertyZoomDependent(name) {
const prop = this._transitionablePaint._values[name];
return prop != null && prop.value != null && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
prop.value.expression != null && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
prop.value.expression instanceof ZoomDependentExpression;
}
isZoomDependent() {
return this._isPropertyZoomDependent("model-scale") || this._isPropertyZoomDependent("model-rotation") || this._isPropertyZoomDependent("model-translation");
}
}
function tileToLngLat(id, position, pointX, pointY) {
const tileCount = 1 << id.z;
position.lat = latFromMercatorY((pointY / EXTENT + id.y) / tileCount);
position.lng = lngFromMercatorX((pointX / EXTENT + id.x) / tileCount);
}
function loadMatchingModelFeature(bucket, featureIndex, tilespaceGeometry, transform) {
const nodeInfo = bucket.getNodesInfo()[featureIndex];
if (!nodeInfo || nodeInfo.hiddenByReplacement || !nodeInfo.node.meshes) return;
let intersectionZ = Number.MAX_VALUE;
const node = nodeInfo.node;
const tile = tilespaceGeometry.tile;
const tileMatrix = transform.calculatePosMatrix(tile.tileID.toUnwrapped(), transform.worldSize);
const modelMatrix = tileMatrix;
const scale = nodeInfo.evaluatedScale;
let elevation = 0;
if (transform.elevation && node.elevation) {
elevation = node.elevation * transform.elevation.exaggeration();
}
const anchorX = node.anchor ? node.anchor[0] : 0;
const anchorY = node.anchor ? node.anchor[1] : 0;
translate$2(modelMatrix, modelMatrix, [anchorX * (scale[0] - 1), anchorY * (scale[1] - 1), elevation]);
scale$5(modelMatrix, modelMatrix, scale);
const screenQuery = tilespaceGeometry.queryGeometry;
const projectedQueryGeometry = screenQuery.isPointQuery() ? screenQuery.screenBounds : screenQuery.screenGeometry;
const checkNode = function(n) {
const worldViewProjectionForNode = multiply$5([], modelMatrix, n.globalMatrix);
multiply$5(worldViewProjectionForNode, transform.expandedFarZProjMatrix, worldViewProjectionForNode);
for (let i = 0; i < n.meshes.length; ++i) {
const mesh = n.meshes[i];
if (i === n.lightMeshIndex) {
continue;
}
const depth = queryGeometryIntersectsProjectedAabb(projectedQueryGeometry, transform, worldViewProjectionForNode, mesh.aabb);
if (depth != null) {
intersectionZ = Math.min(depth, intersectionZ);
}
}
if (n.children) {
for (const child of n.children) {
checkNode(child);
}
}
};
checkNode(node);
if (intersectionZ === Number.MAX_VALUE) return;
const position = new LngLat(0, 0);
tileToLngLat(tile.tileID.canonical, position, nodeInfo.node.anchor[0], nodeInfo.node.anchor[1]);
return { intersectionZ, position, feature: nodeInfo.feature };
}
const subclasses = {
circle: CircleStyleLayer,
heatmap: HeatmapStyleLayer,
hillshade: HillshadeStyleLayer,
fill: FillStyleLayer,
"fill-extrusion": FillExtrusionStyleLayer,
building: BuildingStyleLayer,
line: LineStyleLayer,
symbol: SymbolStyleLayer,
background: BackgroundStyleLayer,
raster: RasterStyleLayer,
"raster-particle": RasterParticleStyleLayer,
sky: SkyLayer,
slot: SlotStyleLayer,
model: ModelStyleLayer,
clip: ClipStyleLayer
};
function createStyleLayer(layer, scope, lut, options) {
if (layer.type === "custom") {
return new CustomStyleLayer(layer, scope);
} else {
return new subclasses[layer.type](layer, scope, lut, options);
}
}
const defaultColor = new Color(0, 0, 0);
const PathRule = {
PATH_RULE_UNSPECIFIED: 0,
PATH_RULE_NON_ZERO: 1,
PATH_RULE_EVEN_ODD: 2
};
const LineCap = {
LINE_CAP_UNSPECIFIED: 0,
LINE_CAP_BUTT: 1,
LINE_CAP_ROUND: 2,
LINE_CAP_SQUARE: 3
};
const LineJoin = {
LINE_JOIN_UNSPECIFIED: 0,
LINE_JOIN_MITER: 1,
LINE_JOIN_MITER_CLIP: 2,
LINE_JOIN_ROUND: 3,
LINE_JOIN_BEVEL: 4
};
const PaintOrder = {
PAINT_ORDER_UNSPECIFIED: 0,
PAINT_ORDER_FILL_AND_STROKE: 1,
PAINT_ORDER_STROKE_AND_FILL: 2
};
const PathCommand = {
PATH_COMMAND_UNSPECIFIED: 0,
PATH_COMMAND_MOVE: 1,
PATH_COMMAND_LINE: 2,
PATH_COMMAND_QUAD: 3,
PATH_COMMAND_CUBIC: 4,
PATH_COMMAND_CLOSE: 5
};
const SpreadMethod = {
SPREAD_METHOD_UNSPECIFIED: 0,
SPREAD_METHOD_PAD: 1,
SPREAD_METHOD_REFLECT: 2,
SPREAD_METHOD_REPEAT: 3
};
const MaskType = {
MASK_TYPE_UNSPECIFIED: 0,
MASK_TYPE_LUMINANCE: 1,
MASK_TYPE_ALPHA: 2
};
function readIconSet(pbf, end) {
return pbf.readFields(readIconSetField, { icons: [] }, end);
}
function readIconSetField(tag, obj, pbf) {
if (tag === 1) obj.icons.push(readIcon(pbf, pbf.readVarint() + pbf.pos));
}
function buildStretchedAreas(metadata, axis) {
const areas = [];
const stretch = metadata[`stretch_${axis}`];
let left = null;
for (let i = 0; i < stretch.length; i++) {
if (left === null) {
if (areas.length === 0) {
left = stretch[0];
} else {
left = areas[areas.length - 1][1] + stretch[i];
}
} else {
const right = left + stretch[i];
areas.push([left, right]);
left = null;
}
}
metadata[`stretch_${axis}_areas`] = areas;
}
function postProcessIcon(icon) {
if (!icon.usvg_tree.height) {
icon.usvg_tree.height = icon.usvg_tree.width;
}
if (!icon.metadata) {
return icon;
}
const { metadata } = icon;
if (metadata.content_area) {
const { content_area: contentArea } = metadata;
if (contentArea.left == null) {
contentArea.left = 0;
}
if (contentArea.top == null) {
contentArea.top = contentArea.left;
}
if (contentArea.width == null) {
contentArea.width = icon.usvg_tree.width;
}
if (contentArea.height == null) {
contentArea.height = contentArea.width;
}
}
if (metadata.text_placeholder) {
const { text_placeholder: textPlaceholder } = metadata;
if (textPlaceholder.top == null) {
textPlaceholder.top = textPlaceholder.left;
}
if (textPlaceholder.height == null) {
textPlaceholder.height = textPlaceholder.width;
}
}
if (metadata.stretch_x && metadata.stretch_x.length) {
buildStretchedAreas(metadata, "x");
}
if (metadata.stretch_y && metadata.stretch_y.length) {
buildStretchedAreas(metadata, "y");
}
return icon;
}
function readIcon(pbf, end) {
return postProcessIcon(pbf.readFields(readIconField, { name: void 0 }, end));
}
function readIconField(tag, obj, pbf) {
if (tag === 1) obj.name = pbf.readString();
else if (tag === 2) obj.metadata = readIconMetadata(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 3) {
obj.usvg_tree = readUsvgTree(pbf, pbf.readVarint() + pbf.pos);
obj.data = "usvg_tree";
}
}
function readIconMetadata(pbf, end) {
return pbf.readFields(readIconMetadataField, {
stretch_x: null,
stretch_y: null,
stretch_x_areas: null,
stretch_y_areas: null,
variables: []
}, end);
}
function readIconMetadataField(tag, obj, pbf) {
if (tag === 1) obj.stretch_x = pbf.readPackedVarint();
else if (tag === 2) obj.stretch_y = pbf.readPackedVarint();
else if (tag === 3) obj.content_area = readNonEmptyArea(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 4) obj.variables.push(readVariable(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 5) obj.text_placeholder = readNonEmptyArea(pbf, pbf.readVarint() + pbf.pos);
}
function readNonEmptyArea(pbf, end) {
return pbf.readFields(readNonEmptyAreaField, {}, end);
}
function readNonEmptyAreaField(tag, obj, pbf) {
if (tag === 1) obj.left = pbf.readVarint();
else if (tag === 2) obj.width = pbf.readVarint();
else if (tag === 3) obj.top = pbf.readVarint();
else if (tag === 4) obj.height = pbf.readVarint();
}
function readVariable(pbf, end) {
return pbf.readFields(readVariableField, { name: void 0 }, end);
}
function readVariableField(tag, obj, pbf) {
if (tag === 1) obj.name = pbf.readString();
else if (tag === 2) {
obj.rgb_color = readColor(pbf.readVarint());
obj.value = "rgb_color";
}
}
function readUsvgTree(pbf, end) {
return pbf.readFields(readUsvgTreeField, { width: 20, children: [], linear_gradients: [], radial_gradients: [], clip_paths: [], masks: [] }, end);
}
function readUsvgTreeField(tag, obj, pbf) {
if (tag === 1) obj.width = obj.height = pbf.readVarint();
else if (tag === 2) obj.height = pbf.readVarint();
else if (tag === 3) obj.children.push(readNode(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 4) obj.linear_gradients.push(readLinearGradient(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 5) obj.radial_gradients.push(readRadialGradient(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 7) obj.clip_paths.push(readClipPath(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 8) obj.masks.push(readMask(pbf, pbf.readVarint() + pbf.pos));
}
function readNode(pbf, end) {
return pbf.readFields(readNodeField, {}, end);
}
function readNodeField(tag, obj, pbf) {
if (tag === 1) {
obj.group = readGroup(pbf, pbf.readVarint() + pbf.pos);
obj.node = "group";
} else if (tag === 2) {
obj.path = readPath(pbf, pbf.readVarint() + pbf.pos);
obj.node = "path";
}
}
function readGroup(pbf, end) {
return pbf.readFields(readGroupField, { opacity: 255, children: [] }, end);
}
function readGroupField(tag, obj, pbf) {
if (tag === 1) obj.transform = readTransform(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 2) obj.opacity = pbf.readVarint();
else if (tag === 5) obj.clip_path_idx = pbf.readVarint();
else if (tag === 6) obj.mask_idx = pbf.readVarint();
else if (tag === 7) obj.children.push(readNode(pbf, pbf.readVarint() + pbf.pos));
}
function readTransform(pbf, end) {
return pbf.readFields(readTransformField, { sx: 1, ky: 0, kx: 0, sy: 1, tx: 0, ty: 0 }, end);
}
function readTransformField(tag, obj, pbf) {
if (tag === 1) obj.sx = pbf.readFloat();
else if (tag === 2) obj.ky = pbf.readFloat();
else if (tag === 3) obj.kx = pbf.readFloat();
else if (tag === 4) obj.sy = pbf.readFloat();
else if (tag === 5) obj.tx = pbf.readFloat();
else if (tag === 6) obj.ty = pbf.readFloat();
}
function readPath(pbf, end) {
return pbf.readFields(readPathField, { paint_order: 1, commands: [], step: 1, diffs: [], rule: PathRule.PATH_RULE_NON_ZERO }, end);
}
function readPathField(tag, obj, pbf) {
if (tag === 1) obj.fill = readFill(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 2) obj.stroke = readStroke(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 3) obj.paint_order = pbf.readVarint();
else if (tag === 5) pbf.readPackedVarint(obj.commands);
else if (tag === 6) obj.step = pbf.readFloat();
else if (tag === 7) pbf.readPackedSVarint(obj.diffs);
else if (tag === 8) obj.rule = pbf.readVarint();
}
function readFill(pbf, end) {
return pbf.readFields(readFillField, { rgb_color: defaultColor, paint: "rgb_color", opacity: 255 }, end);
}
function readFillField(tag, obj, pbf) {
if (tag === 1) {
obj.rgb_color = readColor(pbf.readVarint());
obj.paint = "rgb_color";
} else if (tag === 2) {
obj.linear_gradient_idx = pbf.readVarint();
obj.paint = "linear_gradient_idx";
} else if (tag === 3) {
obj.radial_gradient_idx = pbf.readVarint();
obj.paint = "radial_gradient_idx";
} else if (tag === 5) obj.opacity = pbf.readVarint();
}
function readStroke(pbf, end) {
return pbf.readFields(readStrokeField, { rgb_color: defaultColor, paint: "rgb_color", dasharray: [], dashoffset: 0, miterlimit: 4, opacity: 255, width: 1, linecap: 1, linejoin: 1 }, end);
}
function readColor(number) {
return new Color((number >> 16 & 255) / 255, (number >> 8 & 255) / 255, (number & 255) / 255, 1);
}
function readStrokeField(tag, obj, pbf) {
if (tag === 1) {
obj.rgb_color = readColor(pbf.readVarint());
obj.paint = "rgb_color";
} else if (tag === 2) {
obj.linear_gradient_idx = pbf.readVarint();
obj.paint = "linear_gradient_idx";
} else if (tag === 3) {
obj.radial_gradient_idx = pbf.readVarint();
obj.paint = "radial_gradient_idx";
} else if (tag === 5) pbf.readPackedFloat(obj.dasharray);
else if (tag === 6) obj.dashoffset = pbf.readFloat();
else if (tag === 7) obj.miterlimit = pbf.readFloat();
else if (tag === 8) obj.opacity = pbf.readVarint();
else if (tag === 9) obj.width = pbf.readFloat();
else if (tag === 10) obj.linecap = pbf.readVarint();
else if (tag === 11) obj.linejoin = pbf.readVarint();
}
function readLinearGradient(pbf, end) {
return pbf.readFields(readLinearGradientField, { spread_method: 1, stops: [], x1: 0, y1: 0, x2: 1, y2: 0 }, end);
}
function readLinearGradientField(tag, obj, pbf) {
if (tag === 1) obj.transform = readTransform(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 2) obj.spread_method = pbf.readVarint();
else if (tag === 3) obj.stops.push(readStop(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 4) obj.x1 = pbf.readFloat();
else if (tag === 5) obj.y1 = pbf.readFloat();
else if (tag === 6) obj.x2 = pbf.readFloat();
else if (tag === 7) obj.y2 = pbf.readFloat();
}
function readStop(pbf, end) {
return pbf.readFields(readStopField, { offset: 0, opacity: 255, rgb_color: defaultColor }, end);
}
function readStopField(tag, obj, pbf) {
if (tag === 1) obj.offset = pbf.readFloat();
else if (tag === 2) obj.opacity = pbf.readVarint();
else if (tag === 3) obj.rgb_color = readColor(pbf.readVarint());
}
function readRadialGradient(pbf, end) {
return pbf.readFields(readRadialGradientField, { spread_method: 1, stops: [], cx: 0.5, cy: 0.5, r: 0.5, fx: 0.5, fy: 0.5, fr: 0 }, end);
}
function readRadialGradientField(tag, obj, pbf) {
if (tag === 1) obj.transform = readTransform(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 2) obj.spread_method = pbf.readVarint();
else if (tag === 3) obj.stops.push(readStop(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 4) obj.cx = pbf.readFloat();
else if (tag === 5) obj.cy = pbf.readFloat();
else if (tag === 6) obj.r = pbf.readFloat();
else if (tag === 7) obj.fx = pbf.readFloat();
else if (tag === 8) obj.fy = pbf.readFloat();
else if (tag === 9) obj.fr = pbf.readFloat();
}
function readClipPath(pbf, end) {
return pbf.readFields(readClipPathField, { children: [] }, end);
}
function readClipPathField(tag, obj, pbf) {
if (tag === 1) obj.transform = readTransform(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 2) obj.clip_path_idx = pbf.readVarint();
else if (tag === 3) obj.children.push(readNode(pbf, pbf.readVarint() + pbf.pos));
}
function readMask(pbf, end) {
const mask = pbf.readFields(readMaskField, { left: 0, width: 20, mask_type: MaskType.MASK_TYPE_LUMINANCE, children: [] }, end);
if (mask.height == null) {
mask.height = mask.width;
}
if (mask.top == null) {
mask.top = mask.left;
}
return mask;
}
function readMaskField(tag, obj, pbf) {
if (tag === 1) obj.left = obj.top = pbf.readFloat();
else if (tag === 2) obj.width = obj.height = pbf.readFloat();
else if (tag === 3) obj.top = pbf.readFloat();
else if (tag === 4) obj.height = pbf.readFloat();
else if (tag === 5) obj.mask_type = pbf.readVarint();
else if (tag === 6) obj.mask_idx = pbf.readVarint();
else if (tag === 7) obj.children.push(readNode(pbf, pbf.readVarint() + pbf.pos));
}
class ColorReplacements {
static calculate(params = {}, variables = []) {
const replacements = /* @__PURE__ */ new Map();
const variablesMap = /* @__PURE__ */ new Map();
if (Object.keys(params).length === 0) {
return replacements;
}
variables.forEach((variable) => {
variablesMap.set(variable.name, variable.rgb_color || new Color(0, 0, 0));
});
for (const [key, value] of Object.entries(params)) {
if (variablesMap.has(key)) {
replacements.set(variablesMap.get(key).toString(), value);
} else {
console.warn(`Ignoring unknown image variable "${key}"`);
}
}
return replacements;
}
}
function getStyleColor(iconColor, opacity = 255, colorReplacements) {
const alpha = opacity / 255;
const serializedColor = iconColor.toString();
const color = colorReplacements.has(serializedColor) ? colorReplacements.get(serializedColor).clone() : iconColor.clone();
color.a *= alpha;
return color.toString();
}
function getCanvas(width, height) {
if (!offscreenCanvasSupported()) {
const canvas2 = document.createElement("canvas");
canvas2.width = width;
canvas2.height = height;
return canvas2;
}
return new OffscreenCanvas(width, height);
}
let canvas = null;
let context;
function renderIcon(icon, options) {
const colorReplacements = ColorReplacements.calculate(options.params, icon.metadata ? icon.metadata.variables : []);
const tree = icon.usvg_tree;
const naturalWidth = tree.width;
const naturalHeight = tree.height;
const renderedWidth = Math.max(1, Math.round(naturalWidth * options.sx));
const renderedHeight = Math.max(1, Math.round(naturalHeight * options.sy));
const finalTr = new DOMMatrix([
renderedWidth / naturalWidth,
0,
0,
renderedHeight / naturalHeight,
0,
0
]);
if (canvas === null) {
canvas = getCanvas(10, 10);
context = canvas.getContext("2d", { willReadFrequently: true });
}
canvas.width = renderedWidth;
canvas.height = renderedHeight;
renderNodes(context, finalTr, tree, tree, colorReplacements);
return context.getImageData(0, 0, renderedWidth, renderedHeight);
}
function renderNodes(context2, transform, tree, parent, colorReplacements) {
for (const node of parent.children) {
renderNode(context2, transform, tree, node, colorReplacements);
}
}
function renderNode(context2, transform, tree, node, colorReplacements) {
if (node.group) {
context2.save();
renderGroup(context2, transform, tree, node.group, colorReplacements);
context2.restore();
} else if (node.path) {
context2.save();
renderPath(context2, transform, tree, node.path, colorReplacements);
context2.restore();
} else {
assert(false, "Not implemented");
}
}
function shouldIsolate(group, hasClipPath, hasMask) {
return group.opacity !== 255 || hasClipPath || hasMask;
}
function renderGroup(context2, transform, tree, group, colorReplacements) {
const mask = group.mask_idx != null ? tree.masks[group.mask_idx] : null;
const clipPath = group.clip_path_idx != null ? tree.clip_paths[group.clip_path_idx] : null;
if (group.transform) {
transform = makeTransform(group.transform).preMultiplySelf(transform);
}
if (!shouldIsolate(group, clipPath != null, mask != null)) {
renderNodes(context2, transform, tree, group, colorReplacements);
return;
}
const groupCanvas = getCanvas(context2.canvas.width, context2.canvas.height);
const groupContext = groupCanvas.getContext("2d");
renderNodes(groupContext, transform, tree, group, colorReplacements);
if (clipPath) {
applyClipPath(groupContext, transform, tree, clipPath);
}
if (mask) {
applyMask(groupContext, transform, tree, mask, colorReplacements);
}
context2.globalAlpha = group.opacity / 255;
context2.drawImage(groupCanvas, 0, 0);
}
function renderPath(context2, transform, tree, path, colorReplacements) {
context2.setTransform(transform);
if (path.paint_order === PaintOrder.PAINT_ORDER_FILL_AND_STROKE) {
fillPath(context2, tree, path, colorReplacements);
strokePath(context2, tree, path, colorReplacements);
} else {
strokePath(context2, tree, path, colorReplacements);
fillPath(context2, tree, path, colorReplacements);
}
}
function fillPath(context2, tree, path, colorReplacements) {
const fill = path.fill;
if (!fill) return;
const alpha = fill.opacity / 255;
context2.save();
context2.beginPath();
buildPath(path, context2);
switch (fill.paint) {
case "rgb_color": {
context2.fillStyle = getStyleColor(fill.rgb_color, fill.opacity, colorReplacements);
break;
}
case "linear_gradient_idx": {
const linearGradient = tree.linear_gradients[fill.linear_gradient_idx];
if (linearGradient.transform) {
context2.setTransform(makeTransform(linearGradient.transform).preMultiplySelf(context2.getTransform()));
}
context2.fillStyle = convertLinearGradient(context2, linearGradient, alpha, colorReplacements);
break;
}
case "radial_gradient_idx": {
const radialGradient = tree.radial_gradients[fill.radial_gradient_idx];
if (radialGradient.transform) {
context2.setTransform(makeTransform(radialGradient.transform).preMultiplySelf(context2.getTransform()));
}
context2.fillStyle = convertRadialGradient(context2, radialGradient, alpha, colorReplacements);
}
}
context2.fill(getFillRule(path));
context2.restore();
}
function getFillRule(path) {
return path.rule === PathRule.PATH_RULE_NON_ZERO ? "nonzero" : path.rule === PathRule.PATH_RULE_EVEN_ODD ? "evenodd" : void 0;
}
function strokePath(context2, tree, path, colorReplacements) {
const stroke = path.stroke;
if (!stroke) return;
const path2d = makePath2d(path);
context2.lineWidth = stroke.width;
context2.miterLimit = stroke.miterlimit;
context2.setLineDash(stroke.dasharray);
context2.lineDashOffset = stroke.dashoffset;
const alpha = stroke.opacity / 255;
switch (stroke.paint) {
case "rgb_color": {
context2.strokeStyle = getStyleColor(stroke.rgb_color, stroke.opacity, colorReplacements);
break;
}
case "linear_gradient_idx":
context2.strokeStyle = convertLinearGradient(context2, tree.linear_gradients[stroke.linear_gradient_idx], alpha, colorReplacements, true);
break;
case "radial_gradient_idx":
context2.strokeStyle = convertRadialGradient(context2, tree.radial_gradients[stroke.radial_gradient_idx], alpha, colorReplacements, true);
}
switch (stroke.linejoin) {
case LineJoin.LINE_JOIN_MITER_CLIP:
case LineJoin.LINE_JOIN_MITER:
context2.lineJoin = "miter";
break;
case LineJoin.LINE_JOIN_ROUND:
context2.lineJoin = "round";
break;
case LineJoin.LINE_JOIN_BEVEL:
context2.lineJoin = "bevel";
}
switch (stroke.linecap) {
case LineCap.LINE_CAP_BUTT:
context2.lineCap = "butt";
break;
case LineCap.LINE_CAP_ROUND:
context2.lineCap = "round";
break;
case LineCap.LINE_CAP_SQUARE:
context2.lineCap = "square";
}
context2.stroke(path2d);
}
function convertLinearGradient(context2, gradient, alpha, colorReplacements, transformGradient = false) {
if (gradient.stops.length === 1) {
const stop = gradient.stops[0];
return getStyleColor(stop.rgb_color, stop.opacity * alpha, colorReplacements);
}
const { x1, y1, x2, y2 } = gradient;
let start = new DOMPoint(x1, y1);
let end = new DOMPoint(x2, y2);
if (transformGradient) {
const tr = makeTransform(gradient.transform);
start = tr.transformPoint(start);
end = tr.transformPoint(end);
}
const linearGradient = context2.createLinearGradient(start.x, start.y, end.x, end.y);
for (const stop of gradient.stops) {
linearGradient.addColorStop(stop.offset, getStyleColor(stop.rgb_color, stop.opacity * alpha, colorReplacements));
}
return linearGradient;
}
function convertRadialGradient(context2, gradient, alpha, colorReplacements, transformGradient = false) {
if (gradient.stops.length === 1) {
const stop = gradient.stops[0];
return getStyleColor(stop.rgb_color, stop.opacity * alpha, colorReplacements);
}
const tr = makeTransform(gradient.transform);
const { fx, fy, fr, cx, cy, r } = gradient;
let start = new DOMPoint(fx, fy);
let end = new DOMPoint(cx, cy);
let r1 = fr;
let r2 = r;
if (transformGradient) {
start = tr.transformPoint(start);
end = tr.transformPoint(end);
const uniformScale = (tr.a + tr.d) / 2;
r1 = fr * uniformScale;
r2 = gradient.r * uniformScale;
}
const radialGradient = context2.createRadialGradient(start.x, start.y, r1, end.x, end.y, r2);
for (const stop of gradient.stops) {
radialGradient.addColorStop(stop.offset, getStyleColor(stop.rgb_color, stop.opacity * alpha, colorReplacements));
}
return radialGradient;
}
function renderClipPath(context2, transform, tree, clipPath) {
const tr = clipPath.transform ? makeTransform(clipPath.transform).preMultiplySelf(transform) : transform;
const groupCanvas = getCanvas(context2.canvas.width, context2.canvas.height);
const groupContext = groupCanvas.getContext("2d");
for (const node of clipPath.children) {
if (node.group) {
renderClipPath(groupContext, tr, tree, node.group);
} else if (node.path) {
const path = node.path;
const path2d = new Path2D();
path2d.addPath(makePath2d(path), tr);
groupContext.fill(path2d, getFillRule(path));
}
}
const selfClipPath = clipPath.clip_path_idx != null ? tree.clip_paths[clipPath.clip_path_idx] : null;
if (selfClipPath) {
applyClipPath(groupContext, tr, tree, selfClipPath);
}
context2.globalCompositeOperation = "source-over";
context2.drawImage(groupCanvas, 0, 0);
}
function applyClipPath(context2, transform, tree, clipPath) {
const maskCanvas = getCanvas(context2.canvas.width, context2.canvas.height);
const maskContext = maskCanvas.getContext("2d");
renderClipPath(maskContext, transform, tree, clipPath);
context2.globalCompositeOperation = "destination-in";
context2.drawImage(maskCanvas, 0, 0);
}
function applyMask(context2, transform, tree, mask, colorReplacements) {
if (mask.children.length === 0) {
return;
}
const childMask = mask.mask_idx != null ? tree.masks[mask.mask_idx] : null;
if (childMask) {
applyMask(context2, transform, tree, childMask, colorReplacements);
}
const width = context2.canvas.width;
const height = context2.canvas.height;
const maskCanvas = getCanvas(width, height);
const maskContext = maskCanvas.getContext("2d");
const maskWidth = mask.width;
const maskHeight = mask.height;
const maskLeft = mask.left;
const maskTop = mask.top;
const clipPath = new Path2D();
const rect = new Path2D();
rect.rect(maskLeft, maskTop, maskWidth, maskHeight);
clipPath.addPath(rect, transform);
maskContext.clip(clipPath);
for (const node of mask.children) {
renderNode(maskContext, transform, tree, node, colorReplacements);
}
const maskImageData = maskContext.getImageData(0, 0, width, height);
const maskData = maskImageData.data;
if (mask.mask_type === MaskType.MASK_TYPE_LUMINANCE) {
for (let i = 0; i < maskData.length; i += 4) {
const r = maskData[i];
const g = maskData[i + 1];
const b = maskData[i + 2];
const a = maskData[i + 3] / 255;
const luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
maskData[i + 3] = luminance * a;
}
}
maskContext.putImageData(maskImageData, 0, 0);
context2.globalCompositeOperation = "destination-in";
context2.drawImage(maskCanvas, 0, 0);
}
function makeTransform(transform) {
return transform ? new DOMMatrix([transform.sx, transform.ky, transform.kx, transform.sy, transform.tx, transform.ty]) : new DOMMatrix();
}
function buildPath(path, path2d) {
const step = path.step;
let x = path.diffs[0] * step;
let y = path.diffs[1] * step;
path2d.moveTo(x, y);
for (let i = 0, j = 2; i < path.commands.length; i++) {
switch (path.commands[i]) {
case PathCommand.PATH_COMMAND_MOVE: {
x += path.diffs[j++] * step;
y += path.diffs[j++] * step;
path2d.moveTo(x, y);
break;
}
case PathCommand.PATH_COMMAND_LINE: {
x += path.diffs[j++] * step;
y += path.diffs[j++] * step;
path2d.lineTo(x, y);
break;
}
case PathCommand.PATH_COMMAND_QUAD: {
const cpx = x + path.diffs[j++] * step;
const cpy = y + path.diffs[j++] * step;
x = cpx + path.diffs[j++] * step;
y = cpy + path.diffs[j++] * step;
path2d.quadraticCurveTo(cpx, cpy, x, y);
break;
}
case PathCommand.PATH_COMMAND_CUBIC: {
const cp1x = x + path.diffs[j++] * step;
const cp1y = y + path.diffs[j++] * step;
const cp2x = cp1x + path.diffs[j++] * step;
const cp2y = cp1y + path.diffs[j++] * step;
x = cp2x + path.diffs[j++] * step;
y = cp2y + path.diffs[j++] * step;
path2d.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
break;
}
case PathCommand.PATH_COMMAND_CLOSE: {
path2d.closePath();
break;
}
default:
assert(false, `Unknown path command "${path.commands[i]}"`);
}
}
return path2d;
}
function makePath2d(path) {
return buildPath(path, new Path2D());
}
function assert(condition, message) {
console.assert(condition, message);
}
class LRUCache {
/**
* @param {number} capacity - max size of cache
*/
constructor(capacity) {
this.capacity = capacity;
this.cache = /* @__PURE__ */ new Map();
}
get(key) {
if (!this.cache.has(key)) return void 0;
const value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
put(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key);
} else if (this.cache.size === this.capacity) {
this.cache.delete(this.cache.keys().next().value);
}
this.cache.set(key, value);
}
delete(key) {
this.cache.delete(key);
}
}
register(LRUCache, "LRUCache");
const MAX_CACHE_SIZE = 150;
class ImageRasterizer {
constructor() {
this.cacheMap = /* @__PURE__ */ new Map();
this.cacheDependenciesMap = /* @__PURE__ */ new Map();
}
static _getImage(imageData) {
return new RGBAImage(imageData, imageData.data);
}
getFromCache(imageVariant, scope, mapId) {
if (!this.cacheMap.has(mapId)) {
this.cacheMap.set(mapId, new LRUCache(MAX_CACHE_SIZE));
}
return this.cacheMap.get(mapId).get(makeFQID(imageVariant.toString(), scope));
}
setInCache(imageVariant, image, scope, mapId) {
if (!this.cacheDependenciesMap.has(mapId)) {
this.cacheDependenciesMap.set(mapId, /* @__PURE__ */ new Map());
}
if (!this.cacheMap.has(mapId)) {
this.cacheMap.set(mapId, new LRUCache(MAX_CACHE_SIZE));
}
const cacheDependencies = this.cacheDependenciesMap.get(mapId);
const fqid = makeFQID(imageVariant.id.toString(), scope);
if (!cacheDependencies.get(fqid)) {
cacheDependencies.set(fqid, /* @__PURE__ */ new Set());
}
const cache = this.cacheMap.get(mapId);
const serializedId = imageVariant.toString();
cacheDependencies.get(fqid).add(serializedId);
cache.put(makeFQID(imageVariant.toString(), scope), image);
}
removeImagesFromCacheByIds(ids, scope, mapId = 0) {
if (!this.cacheMap.has(mapId) || !this.cacheDependenciesMap.has(mapId)) {
return;
}
const cache = this.cacheMap.get(mapId);
const cacheDependencies = this.cacheDependenciesMap.get(mapId);
for (const id of ids) {
const fqid = makeFQID(id.toString(), scope);
if (cacheDependencies.has(fqid)) {
for (const dependency of cacheDependencies.get(fqid)) {
cache.delete(dependency);
}
cacheDependencies.delete(fqid);
}
}
}
rasterize(imageVariant, image, scope, mapId) {
const cachedImage = this.getFromCache(imageVariant, scope, mapId);
if (cachedImage) {
return cachedImage.clone();
}
const imageData = renderIcon(image.icon, imageVariant);
const imageResult = ImageRasterizer._getImage(imageData);
this.setInCache(imageVariant, imageResult, scope, mapId);
return imageResult.clone();
}
}
class DedupedRequest {
constructor(scheduler) {
this.entries = {};
this.scheduler = scheduler;
}
request(key, metadata, request, callback) {
const entry = this.entries[key] = this.entries[key] || { callbacks: [] };
if (entry.result) {
const [err, result] = entry.result;
if (this.scheduler) {
this.scheduler.add(() => {
callback(err, result);
}, metadata);
} else {
callback(err, result);
}
return () => {
};
}
entry.callbacks.push(callback);
if (!entry.cancel) {
entry.cancel = request((err, result) => {
entry.result = [err, result];
for (const cb of entry.callbacks) {
if (this.scheduler) {
this.scheduler.add(() => {
cb(err, result);
}, metadata);
} else {
cb(err, result);
}
}
setTimeout(() => delete this.entries[key], 1e3 * 3);
});
}
return () => {
if (entry.result) return;
entry.callbacks = entry.callbacks.filter((cb) => cb !== callback);
if (!entry.callbacks.length) {
entry.cancel();
delete this.entries[key];
}
};
}
}
function loadVectorTile(params, callback, skipParse) {
const key = JSON.stringify(params.request);
const makeRequest = (callback2) => {
const request = getArrayBuffer(params.request, (err, data, responseHeaders) => {
if (err) {
callback2(err);
} else if (data) {
callback2(null, {
rawData: data,
vectorTile: skipParse ? void 0 : new VectorTile(new Pbf$1(data)),
responseHeaders: new Map(responseHeaders.entries())
});
}
});
return () => {
request.cancel();
callback2();
};
};
if (params.data) {
this.deduped.entries[key] = { result: [null, params.data] };
}
const callbackMetadata = { type: "parseTile", isSymbolTile: params.isSymbolTile, zoom: params.tileZoom };
return this.deduped.request(key, callbackMetadata, makeRequest, callback);
}
class MipLevel {
constructor(size_) {
this.size = size_;
this.minimums = [];
this.maximums = [];
this.leaves = [];
}
getElevation(x, y) {
const idx = this.toIdx(x, y);
return {
min: this.minimums[idx],
max: this.maximums[idx]
};
}
isLeaf(x, y) {
return this.leaves[this.toIdx(x, y)];
}
toIdx(x, y) {
return y * this.size + x;
}
}
function aabbRayIntersect(min, max, pos, dir) {
let tMin = 0;
let tMax = Number.MAX_VALUE;
const epsilon = 1e-15;
for (let i = 0; i < 3; i++) {
if (Math.abs(dir[i]) < epsilon) {
if (pos[i] < min[i] || pos[i] > max[i])
return null;
} else {
const ood = 1 / dir[i];
let t1 = (min[i] - pos[i]) * ood;
let t2 = (max[i] - pos[i]) * ood;
if (t1 > t2) {
const temp = t1;
t1 = t2;
t2 = temp;
}
if (t1 > tMin)
tMin = t1;
if (t2 < tMax)
tMax = t2;
if (tMin > tMax)
return null;
}
}
return tMin;
}
function triangleRayIntersect(ax, ay, az, bx, by, bz, cx, cy, cz, pos, dir) {
const abX = bx - ax;
const abY = by - ay;
const abZ = bz - az;
const acX = cx - ax;
const acY = cy - ay;
const acZ = cz - az;
const pvecX = dir[1] * acZ - dir[2] * acY;
const pvecY = dir[2] * acX - dir[0] * acZ;
const pvecZ = dir[0] * acY - dir[1] * acX;
const det = abX * pvecX + abY * pvecY + abZ * pvecZ;
if (Math.abs(det) < 1e-15)
return null;
const invDet = 1 / det;
const tvecX = pos[0] - ax;
const tvecY = pos[1] - ay;
const tvecZ = pos[2] - az;
const u = (tvecX * pvecX + tvecY * pvecY + tvecZ * pvecZ) * invDet;
if (u < 0 || u > 1)
return null;
const qvecX = tvecY * abZ - tvecZ * abY;
const qvecY = tvecZ * abX - tvecX * abZ;
const qvecZ = tvecX * abY - tvecY * abX;
const v = (dir[0] * qvecX + dir[1] * qvecY + dir[2] * qvecZ) * invDet;
if (v < 0 || u + v > 1)
return null;
return (acX * qvecX + acY * qvecY + acZ * qvecZ) * invDet;
}
function frac(v, lo, hi) {
return (v - lo) / (hi - lo);
}
function decodeBounds(x, y, depth, boundsMinx, boundsMiny, boundsMaxx, boundsMaxy, outMin, outMax) {
const scale = 1 << depth;
const rangex = boundsMaxx - boundsMinx;
const rangey = boundsMaxy - boundsMiny;
const minX = (x + 0) / scale * rangex + boundsMinx;
const maxX = (x + 1) / scale * rangex + boundsMinx;
const minY = (y + 0) / scale * rangey + boundsMiny;
const maxY = (y + 1) / scale * rangey + boundsMiny;
outMin[0] = minX;
outMin[1] = minY;
outMax[0] = maxX;
outMax[1] = maxY;
}
const aabbSkirtPadding = 100;
class DemMinMaxQuadTree {
constructor(dem_) {
this.maximums = [];
this.minimums = [];
this.leaves = [];
this.childOffsets = [];
this.nodeCount = 0;
this.dem = dem_;
this._siblingOffset = [
[0, 0],
[1, 0],
[0, 1],
[1, 1]
];
if (!this.dem)
return;
const mips = buildDemMipmap(this.dem);
const maxLvl = mips.length - 1;
const rootMip = mips[maxLvl];
const min = rootMip.minimums;
const max = rootMip.maximums;
const leaves = rootMip.leaves;
this._addNode(min[0], max[0], leaves[0]);
this._construct(mips, 0, 0, maxLvl, 0);
}
// Performs raycast against the tree root only. Min and max coordinates defines the size of the root node
raycastRoot(minx, miny, maxx, maxy, p, d, exaggeration = 1) {
const min = [minx, miny, -aabbSkirtPadding];
const max = [maxx, maxy, this.maximums[0] * exaggeration];
return aabbRayIntersect(min, max, p, d);
}
raycast(rootMinx, rootMiny, rootMaxx, rootMaxy, p, d, exaggeration = 1) {
if (!this.nodeCount)
return null;
const t = this.raycastRoot(rootMinx, rootMiny, rootMaxx, rootMaxy, p, d, exaggeration);
if (t == null)
return null;
const tHits = [];
const sortedHits = [];
const boundsMin = [];
const boundsMax = [];
const stack = [{
idx: 0,
t,
nodex: 0,
nodey: 0,
depth: 0
}];
while (stack.length > 0) {
const { idx, t: t2, nodex, nodey, depth } = stack.pop();
if (this.leaves[idx]) {
decodeBounds(nodex, nodey, depth, rootMinx, rootMiny, rootMaxx, rootMaxy, boundsMin, boundsMax);
const scale = 1 << depth;
const minxUv = (nodex + 0) / scale;
const maxxUv = (nodex + 1) / scale;
const minyUv = (nodey + 0) / scale;
const maxyUv = (nodey + 1) / scale;
const az = sampleElevation(minxUv, minyUv, this.dem) * exaggeration;
const bz = sampleElevation(maxxUv, minyUv, this.dem) * exaggeration;
const cz = sampleElevation(maxxUv, maxyUv, this.dem) * exaggeration;
const dz = sampleElevation(minxUv, maxyUv, this.dem) * exaggeration;
const t0 = triangleRayIntersect(
boundsMin[0],
boundsMin[1],
az,
// A
boundsMax[0],
boundsMin[1],
bz,
// B
boundsMax[0],
boundsMax[1],
cz,
// C
p,
d
);
const t1 = triangleRayIntersect(
boundsMax[0],
boundsMax[1],
cz,
boundsMin[0],
boundsMax[1],
dz,
boundsMin[0],
boundsMin[1],
az,
p,
d
);
const tMin = Math.min(
t0 !== null ? t0 : Number.MAX_VALUE,
t1 !== null ? t1 : Number.MAX_VALUE
);
if (tMin === Number.MAX_VALUE) {
const hitPos = scaleAndAdd$2([], p, d, t2);
const fracx = frac(hitPos[0], boundsMin[0], boundsMax[0]);
const fracy = frac(hitPos[1], boundsMin[1], boundsMax[1]);
if (bilinearLerp(az, bz, dz, cz, fracx, fracy) >= hitPos[2])
return t2;
} else {
return tMin;
}
continue;
}
let hitCount = 0;
for (let i = 0; i < this._siblingOffset.length; i++) {
const childNodeX = (nodex << 1) + this._siblingOffset[i][0];
const childNodeY = (nodey << 1) + this._siblingOffset[i][1];
decodeBounds(childNodeX, childNodeY, depth + 1, rootMinx, rootMiny, rootMaxx, rootMaxy, boundsMin, boundsMax);
boundsMin[2] = -aabbSkirtPadding;
boundsMax[2] = this.maximums[this.childOffsets[idx] + i] * exaggeration;
const result = aabbRayIntersect(boundsMin, boundsMax, p, d);
if (result != null) {
const tHit = result;
tHits[i] = tHit;
let added = false;
for (let j = 0; j < hitCount && !added; j++) {
if (tHit >= tHits[sortedHits[j]]) {
sortedHits.splice(j, 0, i);
added = true;
}
}
if (!added)
sortedHits[hitCount] = i;
hitCount++;
}
}
for (let i = 0; i < hitCount; i++) {
const hitIdx = sortedHits[i];
stack.push({
idx: this.childOffsets[idx] + hitIdx,
t: tHits[hitIdx],
nodex: (nodex << 1) + this._siblingOffset[hitIdx][0],
nodey: (nodey << 1) + this._siblingOffset[hitIdx][1],
depth: depth + 1
});
}
}
return null;
}
_addNode(min, max, leaf) {
this.minimums.push(min);
this.maximums.push(max);
this.leaves.push(leaf);
this.childOffsets.push(0);
return this.nodeCount++;
}
_construct(mips, x, y, lvl, parentIdx) {
if (mips[lvl].isLeaf(x, y) === 1) {
return;
}
if (!this.childOffsets[parentIdx])
this.childOffsets[parentIdx] = this.nodeCount;
const childLvl = lvl - 1;
const childMip = mips[childLvl];
let leafMask = 0;
let firstNodeIdx = 0;
for (let i = 0; i < this._siblingOffset.length; i++) {
const childX = x * 2 + this._siblingOffset[i][0];
const childY = y * 2 + this._siblingOffset[i][1];
const elevation = childMip.getElevation(childX, childY);
const leaf = childMip.isLeaf(childX, childY);
const nodeIdx = this._addNode(elevation.min, elevation.max, leaf);
if (leaf)
leafMask |= 1 << i;
if (!firstNodeIdx)
firstNodeIdx = nodeIdx;
}
for (let i = 0; i < this._siblingOffset.length; i++) {
if (!(leafMask & 1 << i)) {
this._construct(mips, x * 2 + this._siblingOffset[i][0], y * 2 + this._siblingOffset[i][1], childLvl, firstNodeIdx + i);
}
}
}
}
function bilinearLerp(p00, p10, p01, p11, x, y) {
return number(
number(p00, p01, y),
number(p10, p11, y),
x
);
}
function sampleElevation(fx, fy, dem) {
const demSize = dem.dim;
const x = clamp(fx * demSize - 0.5, 0, demSize - 1);
const y = clamp(fy * demSize - 0.5, 0, demSize - 1);
const ixMin = Math.floor(x);
const iyMin = Math.floor(y);
const ixMax = Math.min(ixMin + 1, demSize - 1);
const iyMax = Math.min(iyMin + 1, demSize - 1);
const e00 = dem.get(ixMin, iyMin);
const e10 = dem.get(ixMax, iyMin);
const e01 = dem.get(ixMin, iyMax);
const e11 = dem.get(ixMax, iyMax);
return bilinearLerp(e00, e10, e01, e11, x - ixMin, y - iyMin);
}
function buildDemMipmap(dem) {
const demSize = dem.dim;
const elevationDiffThreshold = 5;
const texelSizeOfMip0 = 8;
const levelCount = Math.ceil(Math.log2(demSize / texelSizeOfMip0));
const mips = [];
let blockCount = Math.ceil(Math.pow(2, levelCount));
const blockSize = 1 / blockCount;
const blockSamples = (x, y, size, exclusive, outBounds) => {
const padding = exclusive ? 1 : 0;
const minx = x * size;
const maxx = (x + 1) * size - padding;
const miny = y * size;
const maxy = (y + 1) * size - padding;
outBounds[0] = minx;
outBounds[1] = miny;
outBounds[2] = maxx;
outBounds[3] = maxy;
};
let mip = new MipLevel(blockCount);
const blockBounds = [];
for (let idx = 0; idx < blockCount * blockCount; idx++) {
const y = Math.floor(idx / blockCount);
const x = idx % blockCount;
blockSamples(x, y, blockSize, false, blockBounds);
const e0 = sampleElevation(blockBounds[0], blockBounds[1], dem);
const e1 = sampleElevation(blockBounds[2], blockBounds[1], dem);
const e2 = sampleElevation(blockBounds[2], blockBounds[3], dem);
const e3 = sampleElevation(blockBounds[0], blockBounds[3], dem);
mip.minimums.push(Math.min(e0, e1, e2, e3));
mip.maximums.push(Math.max(e0, e1, e2, e3));
mip.leaves.push(1);
}
mips.push(mip);
for (blockCount /= 2; blockCount >= 1; blockCount /= 2) {
const prevMip = mips[mips.length - 1];
mip = new MipLevel(blockCount);
for (let idx = 0; idx < blockCount * blockCount; idx++) {
const y = Math.floor(idx / blockCount);
const x = idx % blockCount;
blockSamples(x, y, 2, true, blockBounds);
const e0 = prevMip.getElevation(blockBounds[0], blockBounds[1]);
const e1 = prevMip.getElevation(blockBounds[2], blockBounds[1]);
const e2 = prevMip.getElevation(blockBounds[2], blockBounds[3]);
const e3 = prevMip.getElevation(blockBounds[0], blockBounds[3]);
const l0 = prevMip.isLeaf(blockBounds[0], blockBounds[1]);
const l1 = prevMip.isLeaf(blockBounds[2], blockBounds[1]);
const l2 = prevMip.isLeaf(blockBounds[2], blockBounds[3]);
const l3 = prevMip.isLeaf(blockBounds[0], blockBounds[3]);
const minElevation = Math.min(e0.min, e1.min, e2.min, e3.min);
const maxElevation = Math.max(e0.max, e1.max, e2.max, e3.max);
const canConcatenate = l0 && l1 && l2 && l3;
mip.maximums.push(maxElevation);
mip.minimums.push(minElevation);
if (maxElevation - minElevation <= elevationDiffThreshold && canConcatenate) {
mip.leaves.push(1);
} else {
mip.leaves.push(0);
}
}
mips.push(mip);
}
return mips;
}
const unpackVectors = {
mapbox: [6553.6, 25.6, 0.1, 1e4],
terrarium: [256, 1, 1 / 256, 32768]
};
function unpackMapbox(r, g, b) {
return (r * 256 * 256 + g * 256 + b) / 10 - 1e4;
}
function unpackTerrarium(r, g, b) {
return r * 256 + g + b / 256 - 32768;
}
class DEMData {
get tree() {
if (!this._tree) this._buildQuadTree();
return this._tree;
}
// RGBAImage data has uniform 1px padding on all sides: square tile edge size defines stride
// and dim is calculated as stride - 2.
constructor(uid, data, sourceEncoding, borderReady = false) {
this.uid = uid;
if (data.height !== data.width) throw new RangeError("DEM tiles must be square");
if (sourceEncoding && sourceEncoding !== "mapbox" && sourceEncoding !== "terrarium") {
warnOnce(`"${sourceEncoding}" is not a valid encoding type. Valid types include "mapbox" and "terrarium".`);
return;
}
this.stride = data.height;
const dim = this.dim = data.height - 2;
const values = new Uint32Array(data.data.buffer);
this.pixels = new Uint8Array(data.data.buffer);
this.floatView = new Float32Array(data.data.buffer);
this.borderReady = borderReady;
this._modifiedForSources = {};
if (!borderReady) {
for (let x = 0; x < dim; x++) {
values[this._idx(-1, x)] = values[this._idx(0, x)];
values[this._idx(dim, x)] = values[this._idx(dim - 1, x)];
values[this._idx(x, -1)] = values[this._idx(x, 0)];
values[this._idx(x, dim)] = values[this._idx(x, dim - 1)];
}
values[this._idx(-1, -1)] = values[this._idx(0, 0)];
values[this._idx(dim, -1)] = values[this._idx(dim - 1, 0)];
values[this._idx(-1, dim)] = values[this._idx(0, dim - 1)];
values[this._idx(dim, dim)] = values[this._idx(dim - 1, dim - 1)];
}
const unpack = sourceEncoding === "terrarium" ? unpackTerrarium : unpackMapbox;
for (let i = 0; i < values.length; ++i) {
const byteIdx = i * 4;
this.floatView[i] = unpack(this.pixels[byteIdx], this.pixels[byteIdx + 1], this.pixels[byteIdx + 2]);
}
this._timestamp = exported$1.now();
}
_buildQuadTree() {
assert$1(!this._tree);
this._tree = new DemMinMaxQuadTree(this);
}
get(x, y, clampToEdge = false) {
if (clampToEdge) {
x = clamp(x, -1, this.dim);
y = clamp(y, -1, this.dim);
}
const idx = this._idx(x, y);
return this.floatView[idx];
}
set(x, y, v) {
const idx = this._idx(x, y);
const p = this.floatView[idx];
this.floatView[idx] = v;
return v - p;
}
static getUnpackVector(encoding) {
return unpackVectors[encoding];
}
_idx(x, y) {
if (x < -1 || x >= this.dim + 1 || y < -1 || y >= this.dim + 1) throw new RangeError("out of range source coordinates for DEM data");
return (y + 1) * this.stride + (x + 1);
}
static pack(altitude, encoding) {
const color = [0, 0, 0, 0];
const vector = DEMData.getUnpackVector(encoding);
let v = Math.floor((altitude + vector[3]) / vector[2]);
color[2] = v % 256;
v = Math.floor(v / 256);
color[1] = v % 256;
v = Math.floor(v / 256);
color[0] = v;
return color;
}
getPixels() {
return new Float32Image({ width: this.stride, height: this.stride }, this.pixels);
}
backfillBorder(borderTile, dx, dy) {
if (this.dim !== borderTile.dim) throw new Error("dem dimension mismatch");
let xMin = dx * this.dim, xMax = dx * this.dim + this.dim, yMin = dy * this.dim, yMax = dy * this.dim + this.dim;
switch (dx) {
case -1:
xMin = xMax - 1;
break;
case 1:
xMax = xMin + 1;
break;
}
switch (dy) {
case -1:
yMin = yMax - 1;
break;
case 1:
yMax = yMin + 1;
break;
}
const ox = -dx * this.dim;
const oy = -dy * this.dim;
for (let y = yMin; y < yMax; y++) {
for (let x = xMin; x < xMax; x++) {
const i = 4 * this._idx(x, y);
const j = 4 * this._idx(x + ox, y + oy);
this.pixels[i + 0] = borderTile.pixels[j + 0];
this.pixels[i + 1] = borderTile.pixels[j + 1];
this.pixels[i + 2] = borderTile.pixels[j + 2];
this.pixels[i + 3] = borderTile.pixels[j + 3];
}
}
}
onDeserialize() {
if (this._tree) this._tree.dem = this;
}
}
register(DEMData, "DEMData");
register(DemMinMaxQuadTree, "DemMinMaxQuadTree", { omit: ["dem"] });
function readTileHeader(pbf, end) {
return pbf.readFields(readTileHeaderTag, {
headerLength: 0,
x: 0,
y: 0,
z: 0,
layers: []
}, end);
}
function readTileHeaderTag(tag, obj, pbf) {
if (tag === 1) obj.headerLength = pbf.readFixed32();
else if (tag === 2) obj.x = pbf.readVarint();
else if (tag === 3) obj.y = pbf.readVarint();
else if (tag === 4) obj.z = pbf.readVarint();
else if (tag === 5) obj.layers.push(readLayer(pbf, pbf.readVarint() + pbf.pos));
}
function readFilter(pbf, end) {
return pbf.readFields(readFilterTag, {}, end);
}
function readFilterTag(tag, obj, pbf) {
if (tag === 1) {
obj.delta_filter = readFilterDelta(pbf, pbf.readVarint() + pbf.pos);
obj.filter = "delta_filter";
} else if (tag === 2) {
pbf.readVarint();
obj.filter = "zigzag_filter";
} else if (tag === 3) {
pbf.readVarint();
obj.filter = "bitshuffle_filter";
} else if (tag === 4) {
pbf.readVarint();
obj.filter = "byteshuffle_filter";
}
}
function readFilterDelta(pbf, end) {
return pbf.readFields(readFilterDeltaTag, {
blockSize: 0
}, end);
}
function readFilterDeltaTag(tag, obj, pbf) {
if (tag === 1) obj.blockSize = pbf.readVarint();
}
function readCodec(pbf, end) {
return pbf.readFields(readCodecTag, {}, end);
}
function readCodecTag(tag, obj, pbf) {
if (tag === 1) {
pbf.readVarint();
obj.codec = "gzip_data";
} else if (tag === 2) {
pbf.readVarint();
obj.codec = "jpeg_image";
} else if (tag === 3) {
pbf.readVarint();
obj.codec = "webp_image";
} else if (tag === 4) {
pbf.readVarint();
obj.codec = "png_image";
}
}
function readDataIndexEntry(pbf, end) {
return pbf.readFields(readDataIndexEntryTag, {
firstByte: 0,
lastByte: 0,
filters: [],
codec: null,
offset: 0,
scale: 0,
bands: []
}, end);
}
function readDataIndexEntryTag(tag, obj, pbf) {
let deprecated_scale = 0;
let deprecated_offset = 0;
if (tag === 1) obj.firstByte = pbf.readFixed64();
else if (tag === 2) obj.lastByte = pbf.readFixed64();
else if (tag === 3) obj.filters.push(readFilter(pbf, pbf.readVarint() + pbf.pos));
else if (tag === 4) obj.codec = readCodec(pbf, pbf.readVarint() + pbf.pos);
else if (tag === 5) deprecated_offset = pbf.readFloat();
else if (tag === 6) deprecated_scale = pbf.readFloat();
else if (tag === 7) obj.bands.push(pbf.readString());
else if (tag === 8) obj.offset = pbf.readDouble();
else if (tag === 9) obj.scale = pbf.readDouble();
if (obj.offset === 0) obj.offset = deprecated_offset;
if (obj.scale === 0) obj.scale = deprecated_scale;
}
function readLayer(pbf, end) {
return pbf.readFields(readLayerTag, {
version: 0,
name: "",
units: "",
tileSize: 0,
buffer: 0,
pixelFormat: 0,
dataIndex: []
}, end);
}
function readLayerTag(tag, obj, pbf) {
if (tag === 1) obj.version = pbf.readVarint();
else if (tag === 2) obj.name = pbf.readString();
else if (tag === 3) obj.units = pbf.readString();
else if (tag === 4) obj.tileSize = pbf.readVarint();
else if (tag === 5) obj.buffer = pbf.readVarint();
else if (tag === 6) obj.pixelFormat = pbf.readVarint();
else if (tag === 7) obj.dataIndex.push(readDataIndexEntry(pbf, pbf.readVarint() + pbf.pos));
}
function readNumericData(pbf, values) {
pbf.readFields(readNumericDataTag, values);
}
function readNumericDataTag(tag, values, pbf) {
if (tag === 2) {
readUint32Values(pbf, pbf.readVarint() + pbf.pos, values);
} else if (tag === 3) {
throw new Error("Not implemented");
}
}
function readUint32Values(pbf, end, values) {
return pbf.readFields(readUint32ValuesTag, values, end);
}
function readUint32ValuesTag(tag, values, pbf) {
if (tag === 1) {
let i = 0;
const end = pbf.readVarint() + pbf.pos;
while (pbf.pos < end) {
values[i++] = pbf.readVarint();
}
}
}
function deltaDecode(data, shape) {
if (shape.length !== 4) {
throw new Error(`Expected data of dimension 4 but got ${shape.length}.`);
}
let axisOffset = shape[3];
for (let axis = 2; axis >= 1; axis--) {
const start1 = axis === 1 ? 1 : 0;
const start2 = axis === 2 ? 1 : 0;
for (let i0 = 0; i0 < shape[0]; i0++) {
const offset0 = shape[1] * i0;
for (let i1 = start1; i1 < shape[1]; i1++) {
const offset1 = shape[2] * (i1 + offset0);
for (let i2 = start2; i2 < shape[2]; i2++) {
const offset2 = shape[3] * (i2 + offset1);
for (let i3 = 0; i3 < shape[3]; i3++) {
const offset3 = offset2 + i3;
data[offset3] += data[offset3 - axisOffset];
}
}
}
}
axisOffset *= shape[axis];
}
return data;
}
function zigzagDecode(data) {
for (let i = 0, n = data.length; i < n; i++) {
data[i] = data[i] >>> 1 ^ -(data[i] & 1);
}
return data;
}
function bitshuffleDecode(data, pixelFormat) {
switch (pixelFormat) {
case "uint32":
return data;
case "uint16":
for (let i = 0; i < data.length; i += 2) {
const a = data[i];
const b = data[i + 1];
data[i] = (a & 240) >> 4 | (a & 61440) >> 8 | (b & 240) << 4 | b & 61440;
data[i + 1] = a & 15 | (a & 3840) >> 4 | (b & 15) << 8 | (b & 3840) << 4;
}
return data;
case "uint8":
for (let i = 0; i < data.length; i += 4) {
const a = data[i];
const b = data[i + 1];
const c = data[i + 2];
const d = data[i + 3];
data[i + 0] = (a & 192) >> 6 | (b & 192) >> 4 | (c & 192) >> 2 | (d & 192) >> 0;
data[i + 1] = (a & 48) >> 4 | (b & 48) >> 2 | (c & 48) >> 0 | (d & 48) << 2;
data[i + 2] = (a & 12) >> 2 | (b & 12) >> 0 | (c & 12) << 2 | (d & 12) << 4;
data[i + 3] = (a & 3) >> 0 | (b & 3) << 2 | (c & 3) << 4 | (d & 3) << 6;
}
return data;
default:
throw new Error(`Invalid pixel format, "${pixelFormat}"`);
}
}
var u8 = Uint8Array, u16 = Uint16Array, i32 = Int32Array;
var fleb = new u8([
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
1,
1,
2,
2,
2,
2,
3,
3,
3,
3,
4,
4,
4,
4,
5,
5,
5,
5,
0,
/* unused */
0,
0,
/* impossible */
0
]);
var fdeb = new u8([
0,
0,
0,
0,
1,
1,
2,
2,
3,
3,
4,
4,
5,
5,
6,
6,
7,
7,
8,
8,
9,
9,
10,
10,
11,
11,
12,
12,
13,
13,
/* unused */
0,
0
]);
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
var freb = function(eb, start) {
var b = new u16(31);
for (var i = 0; i < 31; ++i) {
b[i] = start += 1 << eb[i - 1];
}
var r = new i32(b[30]);
for (var i = 1; i < 30; ++i) {
for (var j = b[i]; j < b[i + 1]; ++j) {
r[j] = j - b[i] << 5 | i;
}
}
return {
b,
r
};
};
var _a = freb(fleb, 2), fl = _a.b, revfl = _a.r;
fl[28] = 258, revfl[258] = 28;
var _b = freb(fdeb, 0), fd = _b.b;
var rev = new u16(32768);
for (var i = 0; i < 32768; ++i) {
var x = (i & 43690) >> 1 | (i & 21845) << 1;
x = (x & 52428) >> 2 | (x & 13107) << 2;
x = (x & 61680) >> 4 | (x & 3855) << 4;
rev[i] = ((x & 65280) >> 8 | (x & 255) << 8) >> 1;
}
var hMap = function(cd, mb, r) {
var s = cd.length;
var i = 0;
var l = new u16(mb);
for (; i < s; ++i) {
if (cd[i]) ++l[cd[i] - 1];
}
var le = new u16(mb);
for (i = 1; i < mb; ++i) {
le[i] = le[i - 1] + l[i - 1] << 1;
}
var co;
if (r) {
co = new u16(1 << mb);
var rvb = 15 - mb;
for (i = 0; i < s; ++i) {
if (cd[i]) {
var sv = i << 4 | cd[i];
var r_1 = mb - cd[i];
var v = le[cd[i] - 1]++ << r_1;
for (var m = v | (1 << r_1) - 1; v <= m; ++v) {
co[rev[v] >> rvb] = sv;
}
}
}
} else {
co = new u16(s);
for (i = 0; i < s; ++i) {
if (cd[i]) {
co[i] = rev[le[cd[i] - 1]++] >> 15 - cd[i];
}
}
}
return co;
};
var flt = new u8(288);
for (var i = 0; i < 144; ++i) flt[i] = 8;
for (var i = 144; i < 256; ++i) flt[i] = 9;
for (var i = 256; i < 280; ++i) flt[i] = 7;
for (var i = 280; i < 288; ++i) flt[i] = 8;
var fdt = new u8(32);
for (var i = 0; i < 32; ++i) fdt[i] = 5;
var flrm = /* @__PURE__ */ hMap(flt, 9, 1);
var fdrm = /* @__PURE__ */ hMap(fdt, 5, 1);
var max = function(a) {
var m = a[0];
for (var i = 1; i < a.length; ++i) {
if (a[i] > m) m = a[i];
}
return m;
};
var bits = function(d, p, m) {
var o = p / 8 | 0;
return (d[o] | d[o + 1] << 8) >> (p & 7) & m;
};
var bits16 = function(d, p) {
var o = p / 8 | 0;
return (d[o] | d[o + 1] << 8 | d[o + 2] << 16) >> (p & 7);
};
var shft = function(p) {
return (p + 7) / 8 | 0;
};
var slc = function(v, s, e) {
if (s == null || s < 0) s = 0;
if (e == null || e > v.length) e = v.length;
return new u8(v.subarray(s, e));
};
var ec = [
"unexpected EOF",
"invalid block type",
"invalid length/literal",
"invalid distance",
"stream finished",
"no stream handler",
,
"no callback",
"invalid UTF-8 data",
"extra field too long",
"date not in range 1980-2099",
"filename too long",
"stream finishing",
"invalid zip data"
// determined by unknown compression method
];
var err = function(ind, msg, nt) {
var e = new Error(msg || ec[ind]);
e.code = ind;
if (Error.captureStackTrace) Error.captureStackTrace(e, err);
if (!nt) throw e;
return e;
};
var inflt = function(dat, st, buf, dict) {
var sl = dat.length, dl = dict ? dict.length : 0;
if (!sl || st.f && !st.l) return buf || new u8(0);
var noBuf = !buf;
var resize = noBuf || st.i != 2;
var noSt = st.i;
if (noBuf) buf = new u8(sl * 3);
var cbuf = function(l2) {
var bl = buf.length;
if (l2 > bl) {
var nbuf = new u8(Math.max(bl * 2, l2));
nbuf.set(buf);
buf = nbuf;
}
};
var final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
var tbts = sl * 8;
do {
if (!lm) {
final = bits(dat, pos, 1);
var type = bits(dat, pos + 1, 3);
pos += 3;
if (!type) {
var s = shft(pos) + 4, l = dat[s - 4] | dat[s - 3] << 8, t = s + l;
if (t > sl) {
if (noSt) err(0);
break;
}
if (resize) cbuf(bt + l);
buf.set(dat.subarray(s, t), bt);
st.b = bt += l, st.p = pos = t * 8, st.f = final;
continue;
} else if (type == 1) lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
else if (type == 2) {
var hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
var tl = hLit + bits(dat, pos + 5, 31) + 1;
pos += 14;
var ldt = new u8(tl);
var clt = new u8(19);
for (var i = 0; i < hcLen; ++i) {
clt[clim[i]] = bits(dat, pos + i * 3, 7);
}
pos += hcLen * 3;
var clb = max(clt), clbmsk = (1 << clb) - 1;
var clm = hMap(clt, clb, 1);
for (var i = 0; i < tl; ) {
var r = clm[bits(dat, pos, clbmsk)];
pos += r & 15;
var s = r >> 4;
if (s < 16) {
ldt[i++] = s;
} else {
var c = 0, n = 0;
if (s == 16) n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];
else if (s == 17) n = 3 + bits(dat, pos, 7), pos += 3;
else if (s == 18) n = 11 + bits(dat, pos, 127), pos += 7;
while (n--) ldt[i++] = c;
}
}
var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
lbt = max(lt);
dbt = max(dt);
lm = hMap(lt, lbt, 1);
dm = hMap(dt, dbt, 1);
} else err(1);
if (pos > tbts) {
if (noSt) err(0);
break;
}
}
if (resize) cbuf(bt + 131072);
var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
var lpos = pos;
for (; ; lpos = pos) {
var c = lm[bits16(dat, pos) & lms], sym = c >> 4;
pos += c & 15;
if (pos > tbts) {
if (noSt) err(0);
break;
}
if (!c) err(2);
if (sym < 256) buf[bt++] = sym;
else if (sym == 256) {
lpos = pos, lm = null;
break;
} else {
var add = sym - 254;
if (sym > 264) {
var i = sym - 257, b = fleb[i];
add = bits(dat, pos, (1 << b) - 1) + fl[i];
pos += b;
}
var d = dm[bits16(dat, pos) & dms], dsym = d >> 4;
if (!d) err(3);
pos += d & 15;
var dt = fd[dsym];
if (dsym > 3) {
var b = fdeb[dsym];
dt += bits16(dat, pos) & (1 << b) - 1, pos += b;
}
if (pos > tbts) {
if (noSt) err(0);
break;
}
if (resize) cbuf(bt + 131072);
var end = bt + add;
if (bt < dt) {
var shift = dl - dt, dend = Math.min(dt, end);
if (shift + bt < 0) err(3);
for (; bt < dend; ++bt) buf[bt] = dict[shift + bt];
}
for (; bt < end; ++bt) buf[bt] = buf[bt - dt];
}
}
st.l = lm, st.p = lpos, st.b = bt, st.f = final;
if (lm) final = 1, st.m = lbt, st.d = dm, st.n = dbt;
} while (!final);
return bt != buf.length && noBuf ? slc(buf, 0, bt) : buf.subarray(0, bt);
};
var et = /* @__PURE__ */ new u8(0);
var gzs = function(d) {
if (d[0] != 31 || d[1] != 139 || d[2] != 8) err(6, "invalid gzip data");
var flg = d[3];
var st = 10;
if (flg & 4) st += (d[10] | d[11] << 8) + 2;
for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++]) ;
return st + (flg & 2);
};
var gzl = function(d) {
var l = d.length;
return (d[l - 4] | d[l - 3] << 8 | d[l - 2] << 16 | d[l - 1] << 24) >>> 0;
};
function gunzipSync(data, opts) {
var st = gzs(data);
if (st + 8 > data.length) err(6, "invalid gzip data");
return inflt(data.subarray(st, -8), {
i: 2
}, opts && opts.out || new u8(gzl(data)), opts && opts.dictionary);
}
var td = typeof TextDecoder != "undefined" && /* @__PURE__ */ new TextDecoder();
var tds = 0;
try {
td.decode(et, {
stream: true
});
tds = 1;
} catch (e) {
}
const DS_TYPES = {
gzip_data: "gzip"
};
function decompress(bytes, codec) {
if (!globalThis.DecompressionStream) {
switch (codec) {
case "gzip_data":
return Promise.resolve(gunzipSync(bytes));
}
}
const decompressionStreamType = DS_TYPES[codec];
if (!decompressionStreamType) {
throw new Error(`Unhandled codec: ${codec}`);
}
const ds = new globalThis.DecompressionStream(decompressionStreamType);
return new Response(new Blob([bytes]).stream().pipeThrough(ds)).arrayBuffer().then((buf) => new Uint8Array(buf));
}
class MRTError extends Error {
/**
* @param {string} message - error message
*/
constructor(message) {
super(message);
this.name = "MRTError";
}
}
const VERSION$1 = "2.0.1";
const MRT_VERSION = 1;
const PIXEL_FORMAT = {
0: "uint32",
1: "uint32",
2: "uint16",
3: "uint8"
};
const PIXEL_FORMAT_TO_DIM_LEN = {
uint32: 1,
uint16: 2,
uint8: 4
};
const PIXEL_FORMAT_TO_CTOR = {
uint32: Uint32Array,
uint16: Uint16Array,
uint8: Uint8Array
};
let Pbf;
class MapboxRasterTile {
/**
* @param {number} cacheSize - number of decoded data chunks cached
*/
constructor(cacheSize = 5) {
this.x = NaN;
this.y = NaN;
this.z = NaN;
this.layers = {};
this._cacheSize = cacheSize;
}
/**
* Get a layer instance by name
* @param {string} layerName - name of requested layer
* @return {MapboxRasterLayer} layer instance
*/
getLayer(layerName) {
const layer = this.layers[layerName];
if (!layer) throw new MRTError(`Layer '${layerName}' not found`);
return layer;
}
/**
* Get the length of the header from MRT bytes
* @param {ArrayBuffer} buf - data buffer
* @return {number} - length of header, in bytes
*/
getHeaderLength(buf) {
const bytes = new Uint8Array(buf);
const view = new DataView(buf);
if (bytes[0] !== 13) throw new MRTError("File is not a valid MRT.");
return view.getUint32(1, true);
}
/**
* @param {ArrayBuffer} buf - data buffer
* @return {MapboxRasterTile} raster tile instance
*/
parseHeader(buf) {
const bytes = new Uint8Array(buf);
const headerLength = this.getHeaderLength(buf);
if (bytes.length < headerLength) {
throw new MRTError(`Expected header with length >= ${headerLength} but got buffer of length ${bytes.length}`);
}
const pbf = new Pbf(bytes.subarray(0, headerLength));
const meta = readTileHeader(pbf);
if (!isNaN(this.x) && (this.x !== meta.x || this.y !== meta.y || this.z !== meta.z)) {
throw new MRTError(`Invalid attempt to parse header ${meta.z}/${meta.x}/${meta.y} for tile ${this.z}/${this.x}/${this.y}`);
}
this.x = meta.x;
this.y = meta.y;
this.z = meta.z;
for (const layer of meta.layers) {
this.layers[layer.name] = new MapboxRasterLayer(layer, { cacheSize: this._cacheSize });
}
return this;
}
/**
* Create a serializable representation of a data parsing task
* @param {TDataRange} range - range of fetched data
* @return {MRTDecodingBatch} processing task description
*/
createDecodingTask(range) {
const tasks = [];
const layer = this.getLayer(range.layerName);
for (let blockIndex of range.blockIndices) {
const block = layer.dataIndex[blockIndex];
const firstByte = block.firstByte - range.firstByte;
const lastByte = block.lastByte - range.firstByte;
if (layer._blocksInProgress.has(blockIndex)) continue;
const task = {
layerName: layer.name,
firstByte,
lastByte,
pixelFormat: layer.pixelFormat,
blockIndex,
blockShape: [block.bands.length].concat(layer.bandShape),
buffer: layer.buffer,
codec: block.codec.codec,
filters: block.filters.map((f) => f.filter)
};
layer._blocksInProgress.add(blockIndex);
tasks.push(task);
}
const onCancel = () => {
tasks.forEach((task) => layer._blocksInProgress.delete(task.blockIndex));
};
const onComplete = (err2, results) => {
tasks.forEach((task) => layer._blocksInProgress.delete(task.blockIndex));
if (err2) throw err2;
results.forEach((result) => {
this.getLayer(result.layerName).processDecodedData(result);
});
};
return new MRTDecodingBatch(tasks, onCancel, onComplete);
}
}
class MapboxRasterLayer {
/**
* @param {object} pbf - layer configuration
* @param {number} pbf.version - major version of MRT specification with which tile was encoded
* @param {string} pbf.name - layer name
* @param {string} pbf.units - layer units
* @param {number} pbf.tileSize - number of rows and columns in raster data
* @param {number} pbf.buffer - number of pixels around the edge of each tile
* @param {number} pbf.pixelFormat - encoded pixel format enum indicating uint32, uint16, or uint8
* @param {TPbfDataIndexEntry[]} pbf.dataIndex - index of data chunk byte offsets
* @param {TRasterLayerConfig} [config] - Additional configuration parameters
*/
constructor({
version,
name,
units,
tileSize,
pixelFormat,
buffer,
dataIndex
}, config) {
this.version = version;
if (this.version !== MRT_VERSION) {
throw new MRTError(`Cannot parse raster layer encoded with MRT version ${version}`);
}
this.name = name;
this.units = units;
this.tileSize = tileSize;
this.buffer = buffer;
this.pixelFormat = PIXEL_FORMAT[pixelFormat];
this.dataIndex = dataIndex;
this.bandShape = [tileSize + 2 * buffer, tileSize + 2 * buffer, PIXEL_FORMAT_TO_DIM_LEN[this.pixelFormat]];
const cacheSize = config ? config.cacheSize : 5;
this._decodedBlocks = new LRUCache(cacheSize);
this._blocksInProgress = /* @__PURE__ */ new Set();
}
/**
* Get the dimensionality of data based on pixelFormat
* @return {number} length of vector dimension
*/
get dimension() {
return PIXEL_FORMAT_TO_DIM_LEN[this.pixelFormat];
}
/**
* Return the layer cache size (readonly)
* @return {number} cache size
*/
get cacheSize() {
return this._decodedBlocks.capacity;
}
/**
* List all bands
* @return {Array} - list of bands
*/
getBandList() {
return this.dataIndex.map(({
bands
}) => bands).flat();
}
/**
* Assimilate results of data loading task
* @param {TDecodingResult} result - result of processing task
*/
processDecodedData(result) {
const key = result.blockIndex.toString();
if (this._decodedBlocks.get(key)) return;
this._decodedBlocks.put(key, result.data);
}
/**
* Find block for a band sequence index
* @param {string|number} band - label or integer index of desired band
* @return {TBlockReference} - index of block and index of band within block
*/
getBlockForBand(band) {
let blockBandStart = 0;
switch (typeof band) {
case "string":
for (const [blockIndex, block] of this.dataIndex.entries()) {
for (const [blockBandIndex, bandName] of block.bands.entries()) {
if (bandName !== band) continue;
return {
bandIndex: blockBandStart + blockBandIndex,
blockIndex,
blockBandIndex
};
}
blockBandStart += block.bands.length;
}
break;
case "number":
for (const [blockIndex, block] of this.dataIndex.entries()) {
if (band >= blockBandStart && band < blockBandStart + block.bands.length) {
return {
bandIndex: band,
blockIndex,
blockBandIndex: band - blockBandStart
};
}
blockBandStart += block.bands.length;
}
break;
default:
throw new MRTError(`Invalid band \`${JSON.stringify(band)}\`. Expected string or integer.`);
}
return { blockIndex: -1, blockBandIndex: -1 };
}
/**
* Get the byte range of a data slice, for performing a HTTP Range fetch
* @param {Array} bandList - list of slices to be covered
* @return {TDataRange} range of data
*/
getDataRange(bandList) {
let firstByte = Infinity;
let lastByte = -Infinity;
const blockIndices = [];
const allBlocks = /* @__PURE__ */ new Set();
for (const band of bandList) {
const {
blockIndex
} = this.getBlockForBand(band);
if (blockIndex < 0) {
throw new MRTError(`Invalid band: ${JSON.stringify(band)}`);
}
const block = this.dataIndex[blockIndex];
if (!blockIndices.includes(blockIndex)) {
blockIndices.push(blockIndex);
}
allBlocks.add(blockIndex);
firstByte = Math.min(firstByte, block.firstByte);
lastByte = Math.max(lastByte, block.lastByte);
}
if (allBlocks.size > this.cacheSize) {
throw new MRTError(`Number of blocks to decode (${allBlocks.size}) exceeds cache size (${this.cacheSize}).`);
}
return {
layerName: this.name,
firstByte,
lastByte,
blockIndices
};
}
/**
* Check if the specified band is valid
* @param {number | string} band - sequence band
* @return {boolean} - true if band exists in layer
*/
hasBand(band) {
const {
blockIndex
} = this.getBlockForBand(band);
return blockIndex >= 0;
}
/**
* Check if the layer has data for a given sequence band
* @param {number | string} band - sequence band
* @return {boolean} true if data is already available
*/
hasDataForBand(band) {
const {
blockIndex
} = this.getBlockForBand(band);
return blockIndex >= 0 && !!this._decodedBlocks.get(blockIndex.toString());
}
/**
* Get a typed array view of data
* @param {number | string} band - sequence band
* @return {TBandViewRGBA} view of raster layer
*/
getBandView(band) {
const {
blockIndex,
blockBandIndex
} = this.getBlockForBand(band);
if (blockIndex < 0) {
throw new MRTError(`Band not found: ${JSON.stringify(band)}`);
}
const blockData = this._decodedBlocks.get(blockIndex.toString());
if (!blockData) {
throw new MRTError(`Data for band ${JSON.stringify(band)} of layer "${this.name}" not decoded.`);
}
const block = this.dataIndex[blockIndex];
const bandDataLength = this.bandShape.reduce((a, b) => a * b, 1);
const start = blockBandIndex * bandDataLength;
const data = blockData.subarray(start, start + bandDataLength);
const bytes = new Uint8Array(data.buffer).subarray(data.byteOffset, data.byteOffset + data.byteLength);
return {
data,
bytes,
tileSize: this.tileSize,
buffer: this.buffer,
pixelFormat: this.pixelFormat,
dimension: this.dimension,
offset: block.offset,
scale: block.scale
};
}
}
MapboxRasterTile.setPbf = function(_Pbf) {
Pbf = _Pbf;
};
class MRTDecodingBatch {
/**
* @param {TProcessingTask[]} tasks - processing tasks
* @param {() => void} onCancel - callback invoked on cancel
* @param {(err: Error, results: TDecodingResult[]) => void} onComplete - callback invoked on completion
*/
constructor(tasks, onCancel, onComplete) {
this.tasks = tasks;
this._onCancel = onCancel;
this._onComplete = onComplete;
this._finalized = false;
}
/**
* Cancel a processing task
* return {void}
*/
cancel() {
if (this._finalized) return;
this._onCancel();
this._finalized = true;
}
/**
* Complete a processing task
* @param {Error} err - processing error, if encountered
* @param {TDecodingResult[]} result - result of processing
* return {void}
*/
complete(err2, result) {
if (this._finalized) return;
this._onComplete(err2, result);
this._finalized = true;
}
}
MapboxRasterTile.performDecoding = function(buf, decodingBatch) {
const bytes = new Uint8Array(buf);
return Promise.all(decodingBatch.tasks.map((task) => {
const {
layerName,
firstByte,
lastByte,
pixelFormat,
blockShape,
blockIndex,
filters,
codec
} = task;
const taskBuf = bytes.subarray(firstByte, lastByte + 1);
const dataLength = blockShape[0] * blockShape[1] * blockShape[2];
const values = new Uint32Array(dataLength);
let decoded;
switch (codec) {
case "gzip_data": {
decoded = decompress(taskBuf, codec).then((bytes2) => {
readNumericData(new Pbf(bytes2), values);
const Ctor = PIXEL_FORMAT_TO_CTOR[pixelFormat];
return new Ctor(values.buffer);
});
break;
}
default:
throw new MRTError(`Unhandled codec: ${codec}`);
}
return decoded.then((data) => {
for (let i = filters.length - 1; i >= 0; i--) {
switch (filters[i]) {
case "delta_filter":
deltaDecode(data, blockShape);
break;
case "zigzag_filter":
zigzagDecode(data);
break;
case "bitshuffle_filter":
bitshuffleDecode(data, pixelFormat);
break;
default:
throw new MRTError(`Unhandled filter "${filters[i]}"`);
}
}
return {
layerName,
blockIndex,
data
};
}).catch((err2) => {
throw err2;
});
}));
};
register(MRTDecodingBatch, "MRTDecodingBatch", { omit: ["_onCancel", "_onComplete"] });
register(MapboxRasterTile, "MapboxRasterTile");
register(MapboxRasterLayer, "MapboxRasterLayer", { omit: ["_blocksInProgress"] });
class DictionaryCoder {
constructor(strings) {
this._stringToNumber = {};
this._numberToString = [];
for (let i = 0; i < strings.length; i++) {
const string = strings[i];
this._stringToNumber[string] = i;
this._numberToString[i] = string;
}
}
encode(string) {
assert$1(string in this._stringToNumber);
return this._stringToNumber[string];
}
decode(n) {
assert$1(n < this._numberToString.length);
return this._numberToString[n];
}
}
class FeatureIndex {
constructor(tileID, promoteId) {
this.tileID = tileID;
this.x = tileID.canonical.x;
this.y = tileID.canonical.y;
this.z = tileID.canonical.z;
this.grid = new Grid(EXTENT, 16, 0);
this.featureIndexArray = new FeatureIndexArray();
this.promoteId = promoteId;
this.is3DTile = false;
this.serializedLayersCache = /* @__PURE__ */ new Map();
}
insert(feature, geometry, featureIndex, sourceLayerIndex, bucketIndex, layoutVertexArrayOffset = 0, envelopePadding = 0) {
const key = this.featureIndexArray.length;
this.featureIndexArray.emplaceBack(featureIndex, sourceLayerIndex, bucketIndex, layoutVertexArrayOffset);
const grid = this.grid;
for (let r = 0; r < geometry.length; r++) {
const ring = geometry[r];
const bbox = [Infinity, Infinity, -Infinity, -Infinity];
for (let i = 0; i < ring.length; i++) {
const p = ring[i];
bbox[0] = Math.min(bbox[0], p.x);
bbox[1] = Math.min(bbox[1], p.y);
bbox[2] = Math.max(bbox[2], p.x);
bbox[3] = Math.max(bbox[3], p.y);
}
if (envelopePadding !== 0) {
bbox[0] -= envelopePadding;
bbox[1] -= envelopePadding;
bbox[2] += envelopePadding;
bbox[3] += envelopePadding;
}
if (bbox[0] < EXTENT && bbox[1] < EXTENT && bbox[2] >= 0 && bbox[3] >= 0) {
grid.insert(key, bbox[0], bbox[1], bbox[2], bbox[3]);
}
}
}
loadVTLayers() {
if (!this.vtLayers) {
this.vtLayers = new VectorTile(new Pbf$1(this.rawTileData)).layers;
this.sourceLayerCoder = new DictionaryCoder(this.vtLayers ? Object.keys(this.vtLayers).sort() : ["_geojsonTileLayer"]);
this.vtFeatures = {};
for (const layer in this.vtLayers) {
this.vtFeatures[layer] = [];
}
}
return this.vtLayers;
}
// Finds non-symbol features in this tile at a particular position.
query(query, params) {
const { tilespaceGeometry, transform, tileTransform, pixelPosMatrix, availableImages, worldview } = params;
this.loadVTLayers();
this.serializedLayersCache.clear();
const queryRadius = params.queryRadius ? params.queryRadius : 0;
const bounds = tilespaceGeometry.bufferedTilespaceBounds;
const queryPredicate = (bx1, by1, bx2, by2) => {
const isects = polygonIntersectsBox(tilespaceGeometry.bufferedTilespaceGeometry, bx1 - queryRadius, by1 - queryRadius, bx2 + queryRadius, by2 + queryRadius);
return isects;
};
const matching = this.grid.query(bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y, queryPredicate);
matching.sort(topDownFeatureComparator);
let elevationHelper = null;
if (transform.elevation && matching.length > 0) {
elevationHelper = DEMSampler.create(transform.elevation, this.tileID);
}
const result = {};
let previousIndex;
for (let k = 0; k < matching.length; k++) {
const index = matching[k];
if (index === previousIndex) continue;
previousIndex = index;
const match = this.featureIndexArray.get(index);
let featureGeometry = null;
if (this.is3DTile) {
this.loadMatchingModelFeature(result, match, query, tilespaceGeometry, transform, worldview);
continue;
}
const intersectionTest = (feature, styleLayer, featureState, layoutVertexArrayOffset = 0) => {
if (!featureGeometry) {
featureGeometry = loadGeometry(feature, this.tileID.canonical, tileTransform);
}
return styleLayer.queryIntersectsFeature(tilespaceGeometry, feature, featureState, featureGeometry, this.z, transform, pixelPosMatrix, elevationHelper, layoutVertexArrayOffset);
};
this.loadMatchingFeature(
result,
match,
query,
availableImages,
worldview,
intersectionTest
);
}
return result;
}
loadMatchingFeature(result, featureIndexData, query, availableImages, worldview, intersectionTest) {
const { featureIndex, bucketIndex, sourceLayerIndex, layoutVertexArrayOffset } = featureIndexData;
const layerIDs = this.bucketLayerIDs[bucketIndex];
const queryLayers = query.layers;
const queryLayerIDs = Object.keys(queryLayers);
if (queryLayerIDs.length && !arraysIntersect(queryLayerIDs, layerIDs)) {
return;
}
const querySourceCache = query.sourceCache;
const sourceLayerName = this.sourceLayerCoder.decode(sourceLayerIndex);
const sourceLayer = this.vtLayers[sourceLayerName];
const feature = sourceLayer.feature(featureIndex);
const id = this.getId(feature, sourceLayerName);
for (let l = 0; l < layerIDs.length; l++) {
const layerId = layerIDs[l];
if (!queryLayers[layerId]) continue;
const { styleLayer, targets } = queryLayers[layerId];
let featureState = {};
if (id !== void 0) {
featureState = querySourceCache.getFeatureState(styleLayer.sourceLayer, id);
}
const intersectionZ = !intersectionTest || intersectionTest(feature, styleLayer, featureState, layoutVertexArrayOffset);
if (!intersectionZ) {
continue;
}
const geojsonFeature = new Feature(feature, this.z, this.x, this.y, id);
geojsonFeature.tile = this.tileID.canonical;
geojsonFeature.state = featureState;
let serializedLayer = this.serializedLayersCache.get(layerId);
if (!serializedLayer) {
serializedLayer = styleLayer.serialize();
serializedLayer.id = layerId;
this.serializedLayersCache.set(layerId, serializedLayer);
}
geojsonFeature.source = serializedLayer.source;
geojsonFeature.sourceLayer = serializedLayer["source-layer"];
geojsonFeature.layer = Object.assign({}, serializedLayer);
geojsonFeature.layer.paint = evaluateProperties(serializedLayer.paint, styleLayer.paint, feature, featureState, availableImages);
geojsonFeature.layer.layout = evaluateProperties(serializedLayer.layout, styleLayer.layout, feature, featureState, availableImages);
let shouldInclude = false;
for (const target of targets) {
this.updateFeatureProperties(geojsonFeature, target);
const { filter } = target;
if (filter) {
feature.properties = geojsonFeature.properties;
if (filter.needGeometry) {
const evaluationFeature = toEvaluationFeature(feature, true);
if (!filter.filter(new EvaluationParameters(this.tileID.overscaledZ, { worldview }), evaluationFeature, this.tileID.canonical)) {
continue;
}
} else if (!filter.filter(new EvaluationParameters(this.tileID.overscaledZ, { worldview }), feature)) {
continue;
}
}
shouldInclude = true;
if (target.targetId) {
this.addFeatureVariant(geojsonFeature, target);
}
}
if (shouldInclude) {
this.appendToResult(result, layerId, featureIndex, geojsonFeature, intersectionZ);
}
}
}
loadMatchingModelFeature(result, featureIndexData, query, tilespaceGeometry, transform, worldview) {
const { featureIndex, bucketIndex } = featureIndexData;
const layerIDs = this.bucketLayerIDs[bucketIndex];
const queryLayers = query.layers;
const queryLayerIDs = Object.keys(queryLayers);
if (queryLayerIDs.length && !arraysIntersect(queryLayerIDs, layerIDs)) {
return;
}
for (let l = 0; l < layerIDs.length; l++) {
const layerId = layerIDs[l];
const { styleLayer, targets } = queryLayers[layerId];
if (styleLayer.type !== "model") continue;
const tile = tilespaceGeometry.tile;
const bucket = tile.getBucket(styleLayer);
if (!bucket || !(bucket instanceof Tiled3dModelBucket)) continue;
const model = loadMatchingModelFeature(bucket, featureIndex, tilespaceGeometry, transform);
if (!model) continue;
const { z, x, y } = tile.tileID.canonical;
const { feature, intersectionZ, position } = model;
let featureState = {};
if (feature.id !== void 0) {
featureState = query.sourceCache.getFeatureState(styleLayer.sourceLayer, feature.id);
}
const geojsonFeature = new Feature({}, z, x, y, feature.id);
geojsonFeature.tile = this.tileID.canonical;
geojsonFeature.state = featureState;
geojsonFeature.properties = feature.properties;
geojsonFeature.geometry = { type: "Point", coordinates: [position.lng, position.lat] };
let serializedLayer = this.serializedLayersCache.get(layerId);
if (!serializedLayer) {
serializedLayer = styleLayer.serialize();
serializedLayer.id = layerId;
this.serializedLayersCache.set(layerId, serializedLayer);
}
geojsonFeature.source = serializedLayer.source;
geojsonFeature.sourceLayer = serializedLayer["source-layer"];
geojsonFeature.layer = Object.assign({}, serializedLayer);
let shouldInclude = false;
for (const target of targets) {
this.updateFeatureProperties(geojsonFeature, target);
const { filter } = target;
if (filter) {
feature.properties = geojsonFeature.properties;
if (filter.needGeometry) {
if (!filter.filter(new EvaluationParameters(this.tileID.overscaledZ, { worldview }), feature, this.tileID.canonical)) {
continue;
}
} else if (!filter.filter(new EvaluationParameters(this.tileID.overscaledZ, { worldview }), feature)) {
continue;
}
}
shouldInclude = true;
if (target.targetId) {
this.addFeatureVariant(geojsonFeature, target);
}
}
if (shouldInclude) {
this.appendToResult(result, layerId, featureIndex, geojsonFeature, intersectionZ);
}
}
}
updateFeatureProperties(feature, target, availableImages) {
if (target.properties) {
const transformedProperties = {};
for (const name in target.properties) {
const expression = target.properties[name];
const value = expression.evaluate(
{ zoom: this.z },
feature._vectorTileFeature,
feature.state,
feature.tile,
availableImages
);
if (value != null) transformedProperties[name] = value;
}
feature.properties = transformedProperties;
}
}
/**
* Create a feature variant for a query target and add it to the original feature.
*
* @param {Feature} feature The original feature.
* @param {QrfTarget} target The target to derive the feature for.
* @returns {Feature} The derived feature.
*/
addFeatureVariant(feature, target, availableImages) {
const variant = {
target: target.target,
namespace: target.namespace,
uniqueFeatureID: target.uniqueFeatureID
};
if (target.properties) {
variant.properties = feature.properties;
}
feature.variants = feature.variants || {};
feature.variants[target.targetId] = feature.variants[target.targetId] || [];
feature.variants[target.targetId].push(variant);
}
appendToResult(result, layerID, featureIndex, geojsonFeature, intersectionZ) {
let layerResult = result[layerID];
if (layerResult === void 0) {
layerResult = result[layerID] = [];
}
layerResult.push({ featureIndex, feature: geojsonFeature, intersectionZ });
}
// Given a set of symbol indexes that have already been looked up,
// return a matching set of GeoJSONFeatures
lookupSymbolFeatures(symbolFeatureIndexes, bucketIndex, sourceLayerIndex, query, availableImages, worldview) {
const result = {};
this.loadVTLayers();
for (const symbolFeatureIndex of symbolFeatureIndexes) {
const featureIndexData = { bucketIndex, sourceLayerIndex, featureIndex: symbolFeatureIndex, layoutVertexArrayOffset: 0 };
this.loadMatchingFeature(result, featureIndexData, query, availableImages, worldview);
}
return result;
}
loadFeature(featureIndexData) {
const { featureIndex, sourceLayerIndex } = featureIndexData;
this.loadVTLayers();
const sourceLayerName = this.sourceLayerCoder.decode(sourceLayerIndex);
const featureCache = this.vtFeatures[sourceLayerName];
if (featureCache[featureIndex]) {
return featureCache[featureIndex];
}
const sourceLayer = this.vtLayers[sourceLayerName];
const feature = sourceLayer.feature(featureIndex);
featureCache[featureIndex] = feature;
return feature;
}
hasLayer(id) {
for (const layerIDs of this.bucketLayerIDs) {
for (const layerID of layerIDs) {
if (id === layerID) return true;
}
}
return false;
}
getId(feature, sourceLayerId) {
let id = feature.id;
if (this.promoteId) {
const propName = !Array.isArray(this.promoteId) && typeof this.promoteId === "object" ? this.promoteId[sourceLayerId] : this.promoteId;
if (propName != null) {
if (Array.isArray(propName)) {
if (!this.promoteIdExpression) {
const expression = createExpression(propName);
if (expression.result === "success") {
this.promoteIdExpression = expression.value;
} else {
const error = expression.value.map((err) => `${err.key}: ${err.message}`).join(", ");
warnOnce(`Failed to create expression for promoteId: ${error}`);
return void 0;
}
}
id = this.promoteIdExpression.evaluate({ zoom: 0 }, feature);
} else {
id = feature.properties[propName];
}
}
if (typeof id === "boolean") id = Number(id);
}
return id;
}
}
register(FeatureIndex, "FeatureIndex", { omit: ["rawTileData", "sourceLayerCoder"] });
function evaluateProperties(serializedProperties, styleLayerProperties, feature, featureState, availableImages) {
return mapObject(serializedProperties, (_, key) => {
const prop = styleLayerProperties instanceof PossiblyEvaluated ? styleLayerProperties.get(key) : null;
return prop && prop.evaluate ? prop.evaluate(feature, featureState, void 0, availableImages) : prop;
});
}
function topDownFeatureComparator(a, b) {
return b - a;
}
var refProperties = ["type", "source", "source-layer", "minzoom", "maxzoom", "filter", "layout"];
const ARRAY_TYPES = [
Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array,
Int32Array, Uint32Array, Float32Array, Float64Array
];
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor} TypedArrayConstructor */
const VERSION = 1; // serialized format version
const HEADER_SIZE = 8;
class KDBush {
/**
* Creates an index from raw `ArrayBuffer` data.
* @param {ArrayBuffer} data
*/
static from(data) {
if (!(data instanceof ArrayBuffer)) {
throw new Error('Data must be an instance of ArrayBuffer.');
}
const [magic, versionAndType] = new Uint8Array(data, 0, 2);
if (magic !== 0xdb) {
throw new Error('Data does not appear to be in a KDBush format.');
}
const version = versionAndType >> 4;
if (version !== VERSION) {
throw new Error(`Got v${version} data when expected v${VERSION}.`);
}
const ArrayType = ARRAY_TYPES[versionAndType & 0x0f];
if (!ArrayType) {
throw new Error('Unrecognized array type.');
}
const [nodeSize] = new Uint16Array(data, 2, 1);
const [numItems] = new Uint32Array(data, 4, 1);
return new KDBush(numItems, nodeSize, ArrayType, data);
}
/**
* Creates an index that will hold a given number of items.
* @param {number} numItems
* @param {number} [nodeSize=64] Size of the KD-tree node (64 by default).
* @param {TypedArrayConstructor} [ArrayType=Float64Array] The array type used for coordinates storage (`Float64Array` by default).
* @param {ArrayBuffer} [data] (For internal use only)
*/
constructor(numItems, nodeSize = 64, ArrayType = Float64Array, data) {
if (isNaN(numItems) || numItems < 0) throw new Error(`Unpexpected numItems value: ${numItems}.`);
this.numItems = +numItems;
this.nodeSize = Math.min(Math.max(+nodeSize, 2), 65535);
this.ArrayType = ArrayType;
this.IndexArrayType = numItems < 65536 ? Uint16Array : Uint32Array;
const arrayTypeIndex = ARRAY_TYPES.indexOf(this.ArrayType);
const coordsByteSize = numItems * 2 * this.ArrayType.BYTES_PER_ELEMENT;
const idsByteSize = numItems * this.IndexArrayType.BYTES_PER_ELEMENT;
const padCoords = (8 - idsByteSize % 8) % 8;
if (arrayTypeIndex < 0) {
throw new Error(`Unexpected typed array class: ${ArrayType}.`);
}
if (data && (data instanceof ArrayBuffer)) { // reconstruct an index from a buffer
this.data = data;
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
this._pos = numItems * 2;
this._finished = true;
} else { // initialize a new index
this.data = new ArrayBuffer(HEADER_SIZE + coordsByteSize + idsByteSize + padCoords);
this.ids = new this.IndexArrayType(this.data, HEADER_SIZE, numItems);
this.coords = new this.ArrayType(this.data, HEADER_SIZE + idsByteSize + padCoords, numItems * 2);
this._pos = 0;
this._finished = false;
// set header
new Uint8Array(this.data, 0, 2).set([0xdb, (VERSION << 4) + arrayTypeIndex]);
new Uint16Array(this.data, 2, 1)[0] = nodeSize;
new Uint32Array(this.data, 4, 1)[0] = numItems;
}
}
/**
* Add a point to the index.
* @param {number} x
* @param {number} y
* @returns {number} An incremental index associated with the added item (starting from `0`).
*/
add(x, y) {
const index = this._pos >> 1;
this.ids[index] = index;
this.coords[this._pos++] = x;
this.coords[this._pos++] = y;
return index;
}
/**
* Perform indexing of the added points.
*/
finish() {
const numAdded = this._pos >> 1;
if (numAdded !== this.numItems) {
throw new Error(`Added ${numAdded} items when expected ${this.numItems}.`);
}
// kd-sort both arrays for efficient search
sort(this.ids, this.coords, this.nodeSize, 0, this.numItems - 1, 0);
this._finished = true;
return this;
}
/**
* Search the index for items within a given bounding box.
* @param {number} minX
* @param {number} minY
* @param {number} maxX
* @param {number} maxY
* @returns {number[]} An array of indices correponding to the found items.
*/
range(minX, minY, maxX, maxY) {
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
const {ids, coords, nodeSize} = this;
const stack = [0, ids.length - 1, 0];
const result = [];
// recursively search for items in range in the kd-sorted arrays
while (stack.length) {
const axis = stack.pop() || 0;
const right = stack.pop() || 0;
const left = stack.pop() || 0;
// if we reached "tree node", search linearly
if (right - left <= nodeSize) {
for (let i = left; i <= right; i++) {
const x = coords[2 * i];
const y = coords[2 * i + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[i]);
}
continue;
}
// otherwise find the middle index
const m = (left + right) >> 1;
// include the middle item if it's in range
const x = coords[2 * m];
const y = coords[2 * m + 1];
if (x >= minX && x <= maxX && y >= minY && y <= maxY) result.push(ids[m]);
// queue search in halves that intersect the query
if (axis === 0 ? minX <= x : minY <= y) {
stack.push(left);
stack.push(m - 1);
stack.push(1 - axis);
}
if (axis === 0 ? maxX >= x : maxY >= y) {
stack.push(m + 1);
stack.push(right);
stack.push(1 - axis);
}
}
return result;
}
/**
* Search the index for items within a given radius.
* @param {number} qx
* @param {number} qy
* @param {number} r Query radius.
* @returns {number[]} An array of indices correponding to the found items.
*/
within(qx, qy, r) {
if (!this._finished) throw new Error('Data not yet indexed - call index.finish().');
const {ids, coords, nodeSize} = this;
const stack = [0, ids.length - 1, 0];
const result = [];
const r2 = r * r;
// recursively search for items within radius in the kd-sorted arrays
while (stack.length) {
const axis = stack.pop() || 0;
const right = stack.pop() || 0;
const left = stack.pop() || 0;
// if we reached "tree node", search linearly
if (right - left <= nodeSize) {
for (let i = left; i <= right; i++) {
if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]);
}
continue;
}
// otherwise find the middle index
const m = (left + right) >> 1;
// include the middle item if it's in range
const x = coords[2 * m];
const y = coords[2 * m + 1];
if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]);
// queue search in halves that intersect the query
if (axis === 0 ? qx - r <= x : qy - r <= y) {
stack.push(left);
stack.push(m - 1);
stack.push(1 - axis);
}
if (axis === 0 ? qx + r >= x : qy + r >= y) {
stack.push(m + 1);
stack.push(right);
stack.push(1 - axis);
}
}
return result;
}
}
/**
* @param {Uint16Array | Uint32Array} ids
* @param {InstanceType} coords
* @param {number} nodeSize
* @param {number} left
* @param {number} right
* @param {number} axis
*/
function sort(ids, coords, nodeSize, left, right, axis) {
if (right - left <= nodeSize) return;
const m = (left + right) >> 1; // middle index
// sort ids and coords around the middle index so that the halves lie
// either left/right or top/bottom correspondingly (taking turns)
select(ids, coords, m, left, right, axis);
// recursively kd-sort first half and second half on the opposite axis
sort(ids, coords, nodeSize, left, m - 1, 1 - axis);
sort(ids, coords, nodeSize, m + 1, right, 1 - axis);
}
/**
* Custom Floyd-Rivest selection algorithm: sort ids and coords so that
* [left..k-1] items are smaller than k-th item (on either x or y axis)
* @param {Uint16Array | Uint32Array} ids
* @param {InstanceType} coords
* @param {number} k
* @param {number} left
* @param {number} right
* @param {number} axis
*/
function select(ids, coords, k, left, right, axis) {
while (right > left) {
if (right - left > 600) {
const n = right - left + 1;
const m = k - left + 1;
const z = Math.log(n);
const s = 0.5 * Math.exp(2 * z / 3);
const sd = 0.5 * Math.sqrt(z * s * (n - s) / n) * (m - n / 2 < 0 ? -1 : 1);
const newLeft = Math.max(left, Math.floor(k - m * s / n + sd));
const newRight = Math.min(right, Math.floor(k + (n - m) * s / n + sd));
select(ids, coords, k, newLeft, newRight, axis);
}
const t = coords[2 * k + axis];
let i = left;
let j = right;
swapItem(ids, coords, left, k);
if (coords[2 * right + axis] > t) swapItem(ids, coords, left, right);
while (i < j) {
swapItem(ids, coords, i, j);
i++;
j--;
while (coords[2 * i + axis] < t) i++;
while (coords[2 * j + axis] > t) j--;
}
if (coords[2 * left + axis] === t) swapItem(ids, coords, left, j);
else {
j++;
swapItem(ids, coords, j, right);
}
if (j <= k) left = j + 1;
if (k <= j) right = j - 1;
}
}
/**
* @param {Uint16Array | Uint32Array} ids
* @param {InstanceType} coords
* @param {number} i
* @param {number} j
*/
function swapItem(ids, coords, i, j) {
swap(ids, i, j);
swap(coords, 2 * i, 2 * j);
swap(coords, 2 * i + 1, 2 * j + 1);
}
/**
* @param {InstanceType} arr
* @param {number} i
* @param {number} j
*/
function swap(arr, i, j) {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/**
* @param {number} ax
* @param {number} ay
* @param {number} bx
* @param {number} by
*/
function sqDist(ax, ay, bx, by) {
const dx = ax - bx;
const dy = ay - by;
return dx * dx + dy * dy;
}
exports$1.AJAXError = AJAXError;
exports$1.Aabb = Aabb;
exports$1.Actor = Actor;
exports$1.AlphaImage = AlphaImage;
exports$1.BUILDING_HIDDEN_BY_TILE_BORDER_DEDUPLICATION = BUILDING_HIDDEN_BY_TILE_BORDER_DEDUPLICATION;
exports$1.COLOR_MIX_FACTOR = COLOR_MIX_FACTOR;
exports$1.COLOR_RAMP_RES = COLOR_RAMP_RES$1;
exports$1.COLOR_RAMP_RES$1 = COLOR_RAMP_RES;
exports$1.CanonicalTileID = CanonicalTileID;
exports$1.CollisionBoxArray = CollisionBoxArray;
exports$1.Color = Color;
exports$1.CompoundExpression = CompoundExpression;
exports$1.DEMData = DEMData;
exports$1.DEMSampler = DEMSampler;
exports$1.DataConstantProperty = DataConstantProperty;
exports$1.DedupedRequest = DedupedRequest;
exports$1.DefaultModelScale = DefaultModelScale;
exports$1.DictionaryCoder = DictionaryCoder;
exports$1.DirectionProperty = DirectionProperty;
exports$1.Dispatcher = Dispatcher;
exports$1.ELEVATION_OFFSET = ELEVATION_OFFSET;
exports$1.ELEVATION_SCALE = ELEVATION_SCALE;
exports$1.EXTENT = EXTENT;
exports$1.Elevation = Elevation;
exports$1.ElevationFeatures = ElevationFeatures;
exports$1.ElevationPortalGraph = ElevationPortalGraph;
exports$1.ErrorEvent = ErrorEvent;
exports$1.EvaluationParameters = EvaluationParameters;
exports$1.Event = Event;
exports$1.Evented = Evented;
exports$1.FAR_BL = FAR_BL;
exports$1.FAR_BR = FAR_BR;
exports$1.Feature = Feature;
exports$1.FeatureIndex = FeatureIndex;
exports$1.FillBucket = FillBucket;
exports$1.FillExtrusionBucket = FillExtrusionBucket;
exports$1.Float32Image = Float32Image;
exports$1.Frustum = Frustum;
exports$1.FrustumCorners = FrustumCorners;
exports$1.GLOBE_RADIUS = GLOBE_RADIUS;
exports$1.GLOBE_SCALE_MATCH_LATITUDE = GLOBE_SCALE_MATCH_LATITUDE;
exports$1.GLOBE_ZOOM_THRESHOLD_MAX = GLOBE_ZOOM_THRESHOLD_MAX;
exports$1.GLOBE_ZOOM_THRESHOLD_MIN = GLOBE_ZOOM_THRESHOLD_MIN;
exports$1.GlobeSharedBuffers = GlobeSharedBuffers;
exports$1.GlyphManager = GlyphManager;
exports$1.HD_ELEVATION_SOURCE_LAYER = HD_ELEVATION_SOURCE_LAYER;
exports$1.HIDDEN_BY_REPLACEMENT = HIDDEN_BY_REPLACEMENT;
exports$1.ICON_PADDING = ICON_PADDING;
exports$1.ImageAtlas = ImageAtlas;
exports$1.ImageId = ImageId;
exports$1.ImagePosition = ImagePosition;
exports$1.ImageRasterizer = ImageRasterizer;
exports$1.ImageSource = ImageSource;
exports$1.ImageVariant = ImageVariant;
exports$1.KDBush = KDBush;
exports$1.LayerTypeMask = LayerTypeMask;
exports$1.LineAtlas = LineAtlas;
exports$1.LineBucket = LineBucket;
exports$1.LivePerformanceMarkers = LivePerformanceMarkers;
exports$1.LivePerformanceUtils = LivePerformanceUtils;
exports$1.LngLat = LngLat;
exports$1.LngLatBounds = LngLatBounds;
exports$1.LocalGlyphMode = LocalGlyphMode;
exports$1.MAX_MERCATOR_LATITUDE = MAX_MERCATOR_LATITUDE;
exports$1.MapboxRasterTile = MapboxRasterTile;
exports$1.MercatorCoordinate = MercatorCoordinate;
exports$1.Model = Model;
exports$1.ModelSource = ModelSource;
exports$1.ModelTraits = ModelTraits;
exports$1.NEAR_BL = NEAR_BL;
exports$1.NEAR_BR = NEAR_BR;
exports$1.ONE_EM = ONE_EM;
exports$1.OverscaledTileID = OverscaledTileID;
exports$1.PATTERN_PADDING = PATTERN_PADDING;
exports$1.PROPERTY_ELEVATION_ID = PROPERTY_ELEVATION_ID;
exports$1.Pbf = Pbf$1;
exports$1.PerformanceMarkers = PerformanceMarkers;
exports$1.PerformanceUtils = PerformanceUtils;
exports$1.Point = Point;
exports$1.PositionProperty = PositionProperty;
exports$1.PossiblyEvaluated = PossiblyEvaluated;
exports$1.Properties = Properties;
exports$1.RGBAImage = RGBAImage;
exports$1.RasterParticleStyleLayer = RasterParticleStyleLayer;
exports$1.RasterStyleLayer = RasterStyleLayer;
exports$1.Ray = Ray;
exports$1.ReplacementOrderBuilding = ReplacementOrderBuilding;
exports$1.ReplacementOrderLandmark = ReplacementOrderLandmark;
exports$1.ReplacementSource = ReplacementSource;
exports$1.ResolvedImage = ResolvedImage;
exports$1.ResourceType = ResourceType;
exports$1.SDF_SCALE = SDF_SCALE;
exports$1.SegmentVector = SegmentVector;
exports$1.StructArrayLayout11f44 = StructArrayLayout11f44;
exports$1.StructArrayLayout1i2 = StructArrayLayout1i2;
exports$1.StructArrayLayout1ui2 = StructArrayLayout1ui2;
exports$1.StructArrayLayout2f1f2i16 = StructArrayLayout2f1f2i16;
exports$1.StructArrayLayout2f8 = StructArrayLayout2f8;
exports$1.StructArrayLayout2i4 = StructArrayLayout2i4;
exports$1.StructArrayLayout2ui4 = StructArrayLayout2ui4;
exports$1.StructArrayLayout3f12 = StructArrayLayout3f12;
exports$1.StructArrayLayout3i6 = StructArrayLayout3i6;
exports$1.StructArrayLayout3ui6 = StructArrayLayout3ui6;
exports$1.StructArrayLayout4i8 = StructArrayLayout4i8;
exports$1.StructArrayLayout5f20 = StructArrayLayout5f20;
exports$1.StructArrayLayout7f28 = StructArrayLayout7f28;
exports$1.StructArrayLayout9f36 = StructArrayLayout9f36;
exports$1.SymbolBucket = SymbolBucket;
exports$1.TargetFeature = TargetFeature;
exports$1.Texture = Texture;
exports$1.Texture3D = Texture3D;
exports$1.Tiled3dModelBucket = Tiled3dModelBucket;
exports$1.Transitionable = Transitionable;
exports$1.Uniform1f = Uniform1f;
exports$1.Uniform1i = Uniform1i;
exports$1.Uniform2f = Uniform2f;
exports$1.Uniform3f = Uniform3f;
exports$1.Uniform4f = Uniform4f;
exports$1.UniformColor = UniformColor;
exports$1.UniformMatrix3f = UniformMatrix3f;
exports$1.UniformMatrix4f = UniformMatrix4f;
exports$1.UnwrappedTileID = UnwrappedTileID;
exports$1.UserManagedTexture = UserManagedTexture;
exports$1.ValidationError = ValidationError;
exports$1.ValidationWarning = ValidationWarning;
exports$1.VectorTile = VectorTile;
exports$1.VectorTileFeature = VectorTileFeature;
exports$1.WorkerClass = WorkerClass;
exports$1.WorkerPool = WorkerPool;
exports$1.WritingMode = WritingMode;
exports$1.ZoomDependentExpression = ZoomDependentExpression;
exports$1.aabbForTileOnGlobe = aabbForTileOnGlobe;
exports$1.add = add;
exports$1.add$1 = add$4;
exports$1.addDynamicAttributes = addDynamicAttributes;
exports$1.altitudeFromMercatorZ = altitudeFromMercatorZ;
exports$1.array = array$1;
exports$1.assert = assert$1;
exports$1.asyncAll = asyncAll;
exports$1.b64DecodeUnicode = b64DecodeUnicode;
exports$1.b64EncodeUnicode = b64EncodeUnicode;
exports$1.bezier = bezier;
exports$1.bindAll = bindAll;
exports$1.boundsAttributes = boundsAttributes;
exports$1.bufferConvexPolygon = bufferConvexPolygon;
exports$1.cacheEntryPossiblyAdded = cacheEntryPossiblyAdded;
exports$1.calculateGlobeLabelMatrix = calculateGlobeLabelMatrix;
exports$1.calculateGlobeMatrix = calculateGlobeMatrix;
exports$1.calculateGlobeMercatorMatrix = calculateGlobeMercatorMatrix;
exports$1.calculateKey = calculateKey;
exports$1.calculateModelMatrix = calculateModelMatrix;
exports$1.cartesianPositionToSpherical = cartesianPositionToSpherical;
exports$1.circleDefinesValues = circleDefinesValues;
exports$1.circleUniformValues = circleUniformValues;
exports$1.circleUniforms = circleUniforms;
exports$1.circumferenceAtLatitude = circumferenceAtLatitude;
exports$1.clamp = clamp;
exports$1.clearPrewarmedResources = clearPrewarmedResources;
exports$1.clearTileCache = clearTileCache;
exports$1.clipLines = clipLines;
exports$1.clone = clone$6;
exports$1.clone$1 = clone;
exports$1.collisionCircleLayout = collisionCircleLayout;
exports$1.config = config;
exports$1.conjugate = conjugate$1;
exports$1.contrastFactor = contrastFactor;
exports$1.convertModel = convertModel;
exports$1.convertModelMatrixForGlobe = convertModelMatrixForGlobe;
exports$1.create = create$5;
exports$1.create$1 = create$8;
exports$1.create$2 = create$4;
exports$1.create$3 = create$6;
exports$1.create$4 = create$2;
exports$1.create$5 = create$3;
exports$1.createExpression = createExpression;
exports$1.createFilter = createFilter;
exports$1.createLayout = createLayout;
exports$1.createPropertyExpression = createPropertyExpression;
exports$1.createStyleLayer = createStyleLayer;
exports$1.cross = cross$2;
exports$1.csscolorparserExports = csscolorparserExports;
exports$1.deepEqual = deepEqual;
exports$1.deepUnbundle = deepUnbundle;
exports$1.degToRad = degToRad;
exports$1.distance = distance$2;
exports$1.div = div$2;
exports$1.dot = dot$5;
exports$1.earcut = earcut;
exports$1.earthRadius = earthRadius;
exports$1.ease = ease;
exports$1.easeCubicInOut = easeCubicInOut;
exports$1.easeIn = easeIn;
exports$1.ecefToLatLng = ecefToLatLng;
exports$1.edgeIntersectsBox = edgeIntersectsBox;
exports$1.enforceCacheSizeLimit = enforceCacheSizeLimit;
exports$1.evaluateSizeForFeature = evaluateSizeForFeature;
exports$1.evaluateSizeForZoom = evaluateSizeForZoom;
exports$1.evaluateVariableOffset = evaluateVariableOffset;
exports$1.evented = evented;
exports$1.exactEquals = exactEquals$2;
exports$1.exactEquals$1 = exactEquals$4;
exports$1.exported = exported;
exports$1.exported$1 = exported$1;
exports$1.fillExtrusionHeightLift = fillExtrusionHeightLift;
exports$1.filterObject = filterObject;
exports$1.fromMat4 = fromMat4$1;
exports$1.fromQuat = fromQuat;
exports$1.fromRotation = fromRotation$2;
exports$1.fromScaling = fromScaling;
exports$1.fromValues = fromValues;
exports$1.fromValues$1 = fromValues$3;
exports$1.fromValues$2 = fromValues$4;
exports$1.fromValues$3 = fromValues$5;
exports$1.furthestTileCorner = furthestTileCorner;
exports$1.getAABBPointSquareDist = getAABBPointSquareDist;
exports$1.getAnchorAlignment = getAnchorAlignment;
exports$1.getAnchorJustification = getAnchorJustification;
exports$1.getArrayBuffer = getArrayBuffer;
exports$1.getAxisAngle = getAxisAngle;
exports$1.getBounds = getBounds;
exports$1.getColumn = getColumn;
exports$1.getData = getData;
exports$1.getDefaultExportFromCjs = getDefaultExportFromCjs;
exports$1.getDracoUrl = getDracoUrl;
exports$1.getExpiryDataFromHeaders = getExpiryDataFromHeaders;
exports$1.getGlobalWorkerPool = getGlobalWorkerPool;
exports$1.getGridMatrix = getGridMatrix;
exports$1.getImage = getImage;
exports$1.getImagePosition = getImagePosition;
exports$1.getImageRasterizerWorkerPool = getImageRasterizerWorkerPool;
exports$1.getInnerScopeFromFQID = getInnerScopeFromFQID;
exports$1.getJSON = getJSON;
exports$1.getLatitudinalLod = getLatitudinalLod;
exports$1.getLivePerformanceMetrics = getLivePerformanceMetrics;
exports$1.getMeshoptUrl = getMeshoptUrl;
exports$1.getMetersPerPixelAtLatitude = getMetersPerPixelAtLatitude;
exports$1.getNameFromFQID = getNameFromFQID;
exports$1.getOuterScopeFromFQID = getOuterScopeFromFQID;
exports$1.getPerformanceMeasurement = getPerformanceMeasurement;
exports$1.getPixelsToTileUnitsMatrix = getPixelsToTileUnitsMatrix;
exports$1.getProjection = getProjection;
exports$1.getProjectionAdjustmentInverted = getProjectionAdjustmentInverted;
exports$1.getProjectionAdjustments = getProjectionAdjustments;
exports$1.getProjectionInterpolationT = getProjectionInterpolationT;
exports$1.getRTLTextPluginStatus = getRTLTextPluginStatus;
exports$1.getReferrer = getReferrer;
exports$1.getRotation = getRotation;
exports$1.getScaleAdjustment = getScaleAdjustment;
exports$1.getScaling = getScaling;
exports$1.getTilePoint = getTilePoint;
exports$1.getTileVec3 = getTileVec3;
exports$1.getType = getType;
exports$1.getVideo = getVideo;
exports$1.getZoomAdjustment = getZoomAdjustment;
exports$1.globeCenterToScreenPoint = globeCenterToScreenPoint;
exports$1.globeDenormalizeECEF = globeDenormalizeECEF;
exports$1.globeECEFOrigin = globeECEFOrigin;
exports$1.globeMetersToEcef = globeMetersToEcef;
exports$1.globeNormalizeECEF = globeNormalizeECEF;
exports$1.globePixelsToTileUnits = globePixelsToTileUnits;
exports$1.globePoleMatrixForTile = globePoleMatrixForTile;
exports$1.globeTileBounds = globeTileBounds;
exports$1.globeTiltAtLngLat = globeTiltAtLngLat;
exports$1.globeToMercatorTransition = globeToMercatorTransition;
exports$1.globeUseCustomAntiAliasing = globeUseCustomAntiAliasing;
exports$1.identity = identity$3;
exports$1.identity$1 = identity$2;
exports$1.interpolateVec3 = interpolateVec3;
exports$1.invert = invert$2;
exports$1.invert$1 = invert$5;
exports$1.invert$2 = invert$3;
exports$1.isBoolean = isBoolean;
exports$1.isExpression = isExpression;
exports$1.isExpressionFilter = isExpressionFilter;
exports$1.isFQID = isFQID;
exports$1.isFeatureConstant = isFeatureConstant;
exports$1.isFullscreen = isFullscreen;
exports$1.isFunction = isFunction;
exports$1.isGlobalPropertyConstant = isGlobalPropertyConstant;
exports$1.isLngLatBehindGlobe = isLngLatBehindGlobe;
exports$1.isMapboxHTTPURL = isMapboxHTTPURL;
exports$1.isMapboxURL = isMapboxURL;
exports$1.isNumber = isNumber;
exports$1.isObject = isObject;
exports$1.isSafari = isSafari;
exports$1.isSafariWithAntialiasingBug = isSafariWithAntialiasingBug;
exports$1.isStateConstant = isStateConstant;
exports$1.isString = isString;
exports$1.isValidUrl = isValidUrl;
exports$1.isWorker = isWorker;
exports$1.keysDifference = keysDifference;
exports$1.latFromMercatorY = latFromMercatorY;
exports$1.latLngToECEF = latLngToECEF;
exports$1.lazyLoadRTLTextPlugin = lazyLoadRTLTextPlugin;
exports$1.len = len$4;
exports$1.length = length$4;
exports$1.length$1 = length$2;
exports$1.lerp = lerp$5;
exports$1.lerp$1 = lerp$4;
exports$1.lineDefinesValues = lineDefinesValues;
exports$1.linePatternUniformValues = linePatternUniformValues;
exports$1.linePatternUniforms = linePatternUniforms;
exports$1.lineUniformValues = lineUniformValues;
exports$1.lineUniforms = lineUniforms;
exports$1.linearVec3TosRGB = linearVec3TosRGB;
exports$1.lngFromMercatorX = lngFromMercatorX;
exports$1.load3DTile = load3DTile;
exports$1.loadGLTF = loadGLTF;
exports$1.loadGeometry = loadGeometry;
exports$1.loadVectorTile = loadVectorTile;
exports$1.makeFQID = makeFQID;
exports$1.makeRequest = makeRequest;
exports$1.mapObject = mapObject;
exports$1.mapValue = mapValue;
exports$1.max = max$3;
exports$1.mercatorScale = mercatorScale;
exports$1.mercatorXfromLng = mercatorXfromLng;
exports$1.mercatorYfromLat = mercatorYfromLat;
exports$1.mercatorZfromAltitude = mercatorZfromAltitude;
exports$1.min = min$2;
exports$1.mul = mul$5;
exports$1.mul$1 = mul$4;
exports$1.mulberry32 = mulberry32;
exports$1.multiply = multiply$5;
exports$1.multiply$1 = multiply$4;
exports$1.murmur3 = murmur3;
exports$1.negate = negate$2;
exports$1.neighborCoord = neighborCoord;
exports$1.nextPowerOfTwo = nextPowerOfTwo;
exports$1.normalize = normalize$4;
exports$1.normalize$1 = normalize$2;
exports$1.normalize$2 = normalize$3;
exports$1.number = number;
exports$1.offscreenCanvasSupported = offscreenCanvasSupported;
exports$1.ortho = ortho;
exports$1.parseCacheControl = parseCacheControl;
exports$1.performSymbolLayout = performSymbolLayout;
exports$1.perspective = perspective;
exports$1.pick = pick;
exports$1.pixelsToTileUnits = pixelsToTileUnits;
exports$1.plugin = plugin;
exports$1.pointInFootprint = pointInFootprint;
exports$1.polesInViewport = polesInViewport;
exports$1.polygonContainsPoint = polygonContainsPoint;
exports$1.polygonIntersectsBox = polygonIntersectsBox;
exports$1.polygonIntersectsPolygon = polygonIntersectsPolygon;
exports$1.polygonizeBounds = polygonizeBounds;
exports$1.posAttributes = posAttributes;
exports$1.posAttributesGlobeExt = posAttributesGlobeExt;
exports$1.postData = postData;
exports$1.postRasterizationSymbolLayout = postRasterizationSymbolLayout;
exports$1.potpack = potpack;
exports$1.prevPowerOfTwo = prevPowerOfTwo;
exports$1.prewarm = prewarm;
exports$1.process3DTile = process3DTile;
exports$1.radToDeg = radToDeg;
exports$1.readIconSet = readIconSet;
exports$1.refProperties = refProperties;
exports$1.register = register;
exports$1.registerForPluginStateChange = registerForPluginStateChange;
exports$1.renderColorRamp = renderColorRamp;
exports$1.resample = resample$1;
exports$1.rotate = rotate$5;
exports$1.rotateX = rotateX$1;
exports$1.rotateX$1 = rotateX$3;
exports$1.rotateY = rotateY$3;
exports$1.rotateY$1 = rotateY$1;
exports$1.rotateZ = rotateZ$3;
exports$1.rotateZ$1 = rotateZ$1;
exports$1.rtlPluginStatus = rtlPluginStatus;
exports$1.sRGBToLinearAndScale = sRGBToLinearAndScale;
exports$1.saturationFactor = saturationFactor;
exports$1.scale = scale;
exports$1.scale$1 = scale$4;
exports$1.scale$2 = scale$3;
exports$1.scale$3 = scale$5;
exports$1.scaleAndAdd = scaleAndAdd$2;
exports$1.set = set$4;
exports$1.setCacheLimits = setCacheLimits;
exports$1.setColumn = setColumn;
exports$1.setDracoUrl = setDracoUrl;
exports$1.setMeshoptUrl = setMeshoptUrl;
exports$1.setRTLTextPlugin = setRTLTextPlugin;
exports$1.skipClipping = skipClipping;
exports$1.smoothstep = smoothstep;
exports$1.spec = spec;
exports$1.sphericalPositionToCartesian = sphericalPositionToCartesian;
exports$1.squaredLength = squaredLength$4;
exports$1.storageAvailable = storageAvailable;
exports$1.stripQueryParameters = stripQueryParameters;
exports$1.sub = sub$2;
exports$1.sub$1 = sub;
exports$1.subtract = subtract$2;
exports$1.supportsInterpolation = supportsInterpolation;
exports$1.supportsLightExpression = supportsLightExpression;
exports$1.supportsPropertyExpression = supportsPropertyExpression;
exports$1.supportsZoomExpression = supportsZoomExpression;
exports$1.tileAABB = tileAABB;
exports$1.tileCoordToECEF = tileCoordToECEF;
exports$1.tileCornersToBounds = tileCornersToBounds;
exports$1.tileToLngLat = tileToLngLat;
exports$1.tileToMeter = tileToMeter;
exports$1.tileTransform = tileTransform;
exports$1.toEvaluationFeature = toEvaluationFeature;
exports$1.transformMat3 = transformMat3$1;
exports$1.transformMat4 = transformMat4$2;
exports$1.transformMat4$1 = transformMat4$1;
exports$1.transformPointToTile = transformPointToTile;
exports$1.transformQuat = transformQuat$1;
exports$1.transitionTileAABBinECEF = transitionTileAABBinECEF;
exports$1.translate = translate$2;
exports$1.transpose = transpose;
exports$1.triggerPluginCompletionEvent = triggerPluginCompletionEvent;
exports$1.unbundle = unbundle;
exports$1.uniqueId = uniqueId;
exports$1.updateGlobeVertexNormal = updateGlobeVertexNormal;
exports$1.uuid = uuid;
exports$1.validateCustomStyleLayer = validateCustomStyleLayer;
exports$1.validateModel = validateModel;
exports$1.validateUuid = validateUuid;
exports$1.version = version;
exports$1.warnOnce = warnOnce;
exports$1.wrap = wrap$1;
exports$1.zero = zero$2;
}));
define(['./shared'], (function (index) { 'use strict';
function stringify(obj) {
if (typeof obj === "number" || typeof obj === "boolean" || typeof obj === "string" || obj === void 0 || obj === null)
return JSON.stringify(obj);
if (Array.isArray(obj)) {
let str2 = "[";
for (const val of obj) {
str2 += `${stringify(val)},`;
}
return `${str2}]`;
}
let str = "{";
for (const key of Object.keys(obj).sort()) {
str += `${key}:${stringify(obj[key])},`;
}
return `${str}}`;
}
function getKey(layer) {
let key = "";
for (const k of index.refProperties) {
key += `/${stringify(layer[k])}`;
}
return key;
}
function containsKey(obj, key) {
function recursiveSearch(item) {
if (typeof item === "string" && item === key) {
return true;
}
if (Array.isArray(item)) {
return item.some(recursiveSearch);
}
if (item && typeof item === "object") {
return Object.values(item).some(recursiveSearch);
}
return false;
}
return recursiveSearch(obj);
}
function groupByLayout(layers, cachedKeys) {
const groups = {};
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
let k = cachedKeys && cachedKeys[layer.id];
if (!k) {
if (layer.type === "symbol") {
k = layer.id;
} else {
k = getKey(layer);
if (layer.type === "line" && layer["paint"]) {
const lineWidth = layer["paint"]["line-width"];
if (containsKey(lineWidth, "line-progress")) {
k += `/${stringify(layer["paint"]["line-width"])}`;
}
}
}
}
if (cachedKeys)
cachedKeys[layer.id] = k;
let group = groups[k];
if (!group) {
group = groups[k] = [];
}
group.push(layer);
}
const result = [];
for (const k in groups) {
result.push(groups[k]);
}
return result;
}
class StyleLayerIndex {
constructor(layerConfigs) {
this.keyCache = {};
this._layers = {};
this._layerConfigs = {};
if (layerConfigs) {
this.replace(layerConfigs);
}
}
replace(layerConfigs, options) {
this._layerConfigs = {};
this._layers = {};
this.update(layerConfigs, [], options);
}
update(layerConfigs, removedIds, options) {
this._options = options;
for (const layerConfig of layerConfigs) {
this._layerConfigs[layerConfig.id] = layerConfig;
const layer = this._layers[layerConfig.id] = index.createStyleLayer(layerConfig, this.scope, null, this._options);
layer.compileFilter(options);
if (this.keyCache[layerConfig.id])
delete this.keyCache[layerConfig.id];
}
for (const id of removedIds) {
delete this.keyCache[id];
delete this._layerConfigs[id];
delete this._layers[id];
}
this.familiesBySource = {};
const groups = groupByLayout(Object.values(this._layerConfigs), this.keyCache);
for (const layerConfigs2 of groups) {
const layers = layerConfigs2.map((layerConfig) => this._layers[layerConfig.id]);
const layer = layers[0];
if (layer.visibility === "none") {
continue;
}
const sourceId = layer.source || "";
let sourceGroup = this.familiesBySource[sourceId];
if (!sourceGroup) {
sourceGroup = this.familiesBySource[sourceId] = {};
}
const sourceLayerId = layer.sourceLayer || "_geojsonTileLayer";
let sourceLayerFamilies = sourceGroup[sourceLayerId];
if (!sourceLayerFamilies) {
sourceLayerFamilies = sourceGroup[sourceLayerId] = [];
}
sourceLayerFamilies.push(layers);
}
}
}
const glyphPadding = 1;
const localGlyphPadding = glyphPadding * index.SDF_SCALE;
class GlyphAtlas {
constructor(stacks) {
const positions = {};
const bins = [];
for (const stack in stacks) {
const glyphData = stacks[stack];
const glyphPositionMap = positions[stack] = {};
for (const id in glyphData.glyphs) {
const src = glyphData.glyphs[+id];
if (!src || src.bitmap.width === 0 || src.bitmap.height === 0) continue;
const padding = src.metrics.localGlyph ? localGlyphPadding : glyphPadding;
const bin = {
x: 0,
y: 0,
w: src.bitmap.width + 2 * padding,
h: src.bitmap.height + 2 * padding
};
bins.push(bin);
glyphPositionMap[id] = bin;
}
}
const { w, h } = index.potpack(bins);
const image = new index.AlphaImage({ width: w || 1, height: h || 1 });
for (const stack in stacks) {
const glyphData = stacks[stack];
for (const id in glyphData.glyphs) {
const src = glyphData.glyphs[+id];
if (!src || src.bitmap.width === 0 || src.bitmap.height === 0) continue;
const bin = positions[stack][id];
const padding = src.metrics.localGlyph ? localGlyphPadding : glyphPadding;
index.AlphaImage.copy(src.bitmap, image, { x: 0, y: 0 }, { x: bin.x + padding, y: bin.y + padding }, src.bitmap);
}
}
this.image = image;
this.positions = positions;
}
}
index.register(GlyphAtlas, "GlyphAtlas");
function parseIndoorData(data, indoorTileOptions, actor) {
const sourceLayers = calculateIndoorSourceLayers(indoorTileOptions.sourceLayers, new Set(Object.keys(data.layers)));
const indoorState = indoorTileOptions.indoorState;
const indoorData = parseData(data, sourceLayers, indoorState.lastActiveFloors, indoorState.selectedFloorId);
actor.send("setIndoorData", indoorData);
return indoorData;
}
function parseData(data, sourceLayers, lastActiveFloors, selectedFloorId) {
const newActiveFloors = /* @__PURE__ */ new Set();
const allFloors = /* @__PURE__ */ new Set();
const allDefaultFloors = /* @__PURE__ */ new Set();
const floorIdToConflicts = /* @__PURE__ */ new Map();
const buildings = {};
const conflictsWithActive = (candidateId) => {
const candidateConflicts = floorIdToConflicts.get(candidateId) || /* @__PURE__ */ new Set();
for (const activeId of newActiveFloors) {
const activeConflicts = floorIdToConflicts.get(activeId) || /* @__PURE__ */ new Set();
if (activeConflicts.has(candidateId) || candidateConflicts.has(activeId)) return true;
}
return false;
};
for (const layerId of sourceLayers) {
const sourceLayer = data.layers[layerId];
if (!sourceLayer) {
index.warnOnce(`indoor source layer not found: ${layerId}`);
continue;
}
for (let index = 0; index < sourceLayer.length; index++) {
const feature = sourceLayer.feature(index);
if (isValidBuildingFeature(feature)) {
const { id, center } = parseBuilding(feature);
upsertBuilding(buildings, id, center);
newActiveFloors.add(id);
continue;
}
if (isValidFloorFeature(feature)) {
const { id, isDefault, connections, conflicts, buildings: buildingIds, name, zIndex } = parseFloor(feature);
assignFloorToBuildings(buildings, buildingIds, id, { name, zIndex });
floorIdToConflicts.set(id, conflicts);
const isActiveFloor = id === selectedFloorId || connections.has(selectedFloorId);
if (isActiveFloor) {
newActiveFloors.add(id);
}
allFloors.add(id);
if (isDefault) {
allDefaultFloors.add(id);
}
}
}
}
if (lastActiveFloors) {
for (const lastActiveFloorId of lastActiveFloors) {
if (!allFloors.has(lastActiveFloorId)) continue;
if (!conflictsWithActive(lastActiveFloorId)) {
newActiveFloors.add(lastActiveFloorId);
}
}
}
for (const defaultFloorId of allDefaultFloors) {
if (newActiveFloors.has(defaultFloorId)) continue;
if (!conflictsWithActive(defaultFloorId)) {
newActiveFloors.add(defaultFloorId);
}
}
return {
buildings,
activeFloors: newActiveFloors
};
}
function upsertBuilding(buildings, buildingId, center) {
if (!buildings[buildingId]) {
buildings[buildingId] = {
floorIds: /* @__PURE__ */ new Set(),
center: center ? center : [0, 0],
floors: {}
};
} else if (center) {
buildings[buildingId].center = center;
}
}
function assignFloorToBuildings(buildings, buildingIds, floorId, floor) {
for (const buildingId of buildingIds) {
upsertBuilding(buildings, buildingId);
buildings[buildingId].floors[floorId] = floor;
buildings[buildingId].floorIds.add(floorId);
}
}
function parseBuilding(feature) {
const id = feature.properties.id.toString();
const center = feature.properties.center.toString().split(";").map(Number);
return { id, center };
}
function parseFloor(feature) {
const id = feature.properties.id.toString();
const isDefault = feature.properties.is_default ? feature.properties.is_default : false;
const connections = feature.properties.connected_floor_ids ? new Set(feature.properties.connected_floor_ids.toString().split(";")) : /* @__PURE__ */ new Set();
const conflicts = feature.properties.conflicted_floor_ids ? new Set(feature.properties.conflicted_floor_ids.toString().split(";")) : /* @__PURE__ */ new Set();
const buildings = feature.properties.building_ids ? new Set(feature.properties.building_ids.toString().split(";")) : /* @__PURE__ */ new Set();
const name = feature.properties.name.toString();
const zIndex = feature.properties.z_index;
return { id, isDefault, connections, conflicts, buildings, name, zIndex };
}
function hasRequiredProperties(feature, requiredProps) {
return requiredProps.every((prop) => feature.properties && feature.properties[prop] !== void 0 && feature.properties[prop] !== null);
}
function isValidBuildingFeature(feature) {
return hasRequiredProperties(feature, ["type", "id", "name"]) && feature.properties.type === "building";
}
function isValidFloorFeature(feature) {
return hasRequiredProperties(feature, ["type", "id", "name", "z_index"]) && feature.properties.type === "floor";
}
function calculateIndoorSourceLayers(sourceLayers, actualSourceLayers) {
if (!sourceLayers) {
index.warnOnce("No source layers defined in indoor specification");
return actualSourceLayers;
}
if (sourceLayers.size === 0) {
return actualSourceLayers;
}
const missingSourceLayers = sourceLayers.difference(actualSourceLayers);
for (const missingSourceLayer of missingSourceLayers) {
index.warnOnce(`Missing source layer required in indoor specification: ${missingSourceLayer}`);
}
return actualSourceLayers.intersection(actualSourceLayers);
}
class WorkerTile {
constructor(params) {
this.tileID = new index.OverscaledTileID(params.tileID.overscaledZ, params.tileID.wrap, params.tileID.canonical.z, params.tileID.canonical.x, params.tileID.canonical.y);
this.tileZoom = params.tileZoom;
this.uid = params.uid;
this.zoom = params.zoom;
this.lut = params.lut;
this.canonical = params.tileID.canonical;
this.pixelRatio = params.pixelRatio;
this.tileSize = params.tileSize;
this.source = params.source;
this.scope = params.scope;
this.overscaling = this.tileID.overscaleFactor();
this.showCollisionBoxes = params.showCollisionBoxes;
this.collectResourceTiming = params.request ? params.request.collectResourceTiming : false;
this.promoteId = params.promoteId;
this.isSymbolTile = params.isSymbolTile;
this.tileTransform = index.tileTransform(params.tileID.canonical, params.projection);
this.projection = params.projection;
this.worldview = params.worldview;
this.localizableLayerIds = params.localizableLayerIds;
this.brightness = params.brightness;
this.extraShadowCaster = !!params.extraShadowCaster;
this.tessellationStep = params.tessellationStep;
this.scaleFactor = params.scaleFactor;
this.worldview = params.worldview;
this.indoor = params.indoor;
}
parse(data, layerIndex, availableImages, availableModels, actor, callback) {
const m = index.PerformanceUtils.beginMeasure("parseTile1");
this.status = "parsing";
this.data = data;
this.collisionBoxArray = new index.CollisionBoxArray();
const sourceLayerCoder = new index.DictionaryCoder(Object.keys(data.layers).sort());
const featureIndex = new index.FeatureIndex(this.tileID, this.promoteId);
featureIndex.bucketLayerIDs = [];
const buckets = {};
const lineAtlas = new index.LineAtlas(256, 256);
const options = {
featureIndex,
iconDependencies: /* @__PURE__ */ new Map(),
patternDependencies: /* @__PURE__ */ new Map(),
glyphDependencies: {},
lineAtlas,
availableImages,
brightness: this.brightness,
scaleFactor: this.scaleFactor,
elevationFeatures: void 0,
activeFloors: void 0
};
if (this.indoor) {
const activeFloorsVisible = this.indoor.indoorState.activeFloorsVisible;
const indoorData = parseIndoorData(data, this.indoor, actor);
options.activeFloors = activeFloorsVisible ? indoorData.activeFloors : void 0;
}
const asyncBucketLoads = [];
const layerFamilies = layerIndex.familiesBySource[this.source];
for (const sourceLayerId in layerFamilies) {
const sourceLayer = data.layers[sourceLayerId];
if (!sourceLayer) {
continue;
}
let anySymbolLayers = false;
let anyOtherLayers = false;
let any3DLayer = false;
for (const family of layerFamilies[sourceLayerId]) {
if (family[0].type === "symbol") {
anySymbolLayers = true;
} else {
anyOtherLayers = true;
}
if (family[0].is3D() && family[0].type !== "model") {
any3DLayer = true;
}
}
if (this.extraShadowCaster && !any3DLayer) {
continue;
}
if (this.isSymbolTile === true && !anySymbolLayers) {
continue;
} else if (this.isSymbolTile === false && !anyOtherLayers) {
continue;
}
if (sourceLayer.version === 1) {
index.warnOnce(`Vector tile source "${this.source}" layer "${sourceLayerId}" does not use vector tile spec v2 and therefore may have some rendering errors.`);
}
const sourceLayerIndex = sourceLayerCoder.encode(sourceLayerId);
const features = [];
const localizable = this.localizableLayerIds && this.localizableLayerIds.has(sourceLayerId);
let elevationDependency = false;
for (let index$1 = 0, currentFeatureIndex = 0; index$1 < sourceLayer.length; index$1++) {
const feature = sourceLayer.feature(index$1);
const id = featureIndex.getId(feature, sourceLayerId);
const worldview = feature.properties ? feature.properties.worldview : null;
if (localizable && this.worldview && typeof worldview === "string") {
if (worldview === "all") {
feature.properties["$localized"] = true;
} else if (worldview.split(",").includes(this.worldview)) {
feature.properties["$localized"] = true;
feature.properties["worldview"] = this.worldview;
} else {
continue;
}
}
if (!elevationDependency && feature.properties && feature.properties.hasOwnProperty(index.PROPERTY_ELEVATION_ID)) {
elevationDependency = true;
}
features.push({ feature, id, index: currentFeatureIndex, sourceLayerIndex });
currentFeatureIndex++;
}
if (elevationDependency && !options.elevationFeatures && data.layers.hasOwnProperty(index.HD_ELEVATION_SOURCE_LAYER)) {
options.elevationFeatures = index.ElevationFeatures.parseFrom(data.layers[index.HD_ELEVATION_SOURCE_LAYER], this.canonical);
}
for (const family of layerFamilies[sourceLayerId]) {
const layer = family[0];
if (this.extraShadowCaster && (!layer.is3D() || layer.type === "model")) {
continue;
}
if (this.isSymbolTile !== void 0 && layer.type === "symbol" !== this.isSymbolTile) continue;
index.assert(layer.source === this.source);
if (layer.minzoom && this.zoom < Math.floor(layer.minzoom)) continue;
if (layer.maxzoom && this.zoom >= layer.maxzoom) continue;
if (layer.visibility === "none") continue;
recalculateLayers(family, this.zoom, options.brightness, availableImages, this.worldview);
const bucket = buckets[layer.id] = layer.createBucket({
index: featureIndex.bucketLayerIDs.length,
layers: family,
zoom: this.zoom,
lut: this.lut,
canonical: this.canonical,
pixelRatio: this.pixelRatio,
overscaling: this.overscaling,
collisionBoxArray: this.collisionBoxArray,
sourceLayerIndex,
sourceID: this.source,
projection: this.projection.spec,
tessellationStep: this.tessellationStep,
styleDefinedModelURLs: availableModels,
worldview: this.worldview,
localizable
});
index.assert(this.tileTransform.projection.name === this.projection.name);
featureIndex.bucketLayerIDs.push(family.map((l) => index.makeFQID(l.id, l.scope)));
let bucketPromise = bucket.prepare ? bucket.prepare() : null;
if (bucketPromise != null) {
bucketPromise = bucketPromise.then(() => bucket.populate(features, options, this.tileID.canonical, this.tileTransform));
asyncBucketLoads.push(bucketPromise);
} else {
bucket.populate(features, options, this.tileID.canonical, this.tileTransform);
}
}
}
const prepareTile = () => {
lineAtlas.trim();
let error;
let glyphMap;
let iconMap;
let patternMap;
let iconRasterizationTasks;
let patternRasterizationTasks;
const taskMetadata = { type: "maybePrepare", isSymbolTile: this.isSymbolTile, zoom: this.zoom };
const maybePrepare = () => {
if (error) {
this.status = "done";
return callback(error);
} else if (this.extraShadowCaster) {
const m2 = index.PerformanceUtils.beginMeasure("parseTile2");
this.status = "done";
callback(null, {
buckets: Object.values(buckets).filter((b) => !b.isEmpty()),
featureIndex,
collisionBoxArray: null,
glyphAtlasImage: null,
lineAtlas: null,
imageAtlas: null,
brightness: options.brightness,
// Only used for benchmarking:
glyphMap: null,
iconMap: null,
glyphPositions: null
});
index.PerformanceUtils.endMeasure(m2, [["tileID", this.tileID.toString()], ["source", this.source]]);
} else if (glyphMap && iconMap && patternMap) {
const m2 = index.PerformanceUtils.beginMeasure("parseTile2");
const glyphAtlas = new GlyphAtlas(glyphMap);
const iconPositions = /* @__PURE__ */ new Map();
for (const [id, icon] of iconMap.entries()) {
const { imagePosition } = index.getImagePosition(id, icon, index.ICON_PADDING);
iconPositions.set(id, imagePosition);
}
const symbolLayoutData = {};
for (const key in buckets) {
const bucket = buckets[key];
if (bucket instanceof index.SymbolBucket) {
recalculateLayers(bucket.layers, this.zoom, options.brightness, availableImages, this.worldview);
symbolLayoutData[key] = index.performSymbolLayout(
bucket,
glyphMap,
glyphAtlas.positions,
iconMap,
iconPositions,
this.tileID.canonical,
this.tileZoom,
this.scaleFactor,
this.pixelRatio,
iconRasterizationTasks,
this.worldview
);
}
}
const rasterizationStatus = { iconsPending: true, patternsPending: true };
this.rasterizeIfNeeded(actor, iconMap, iconRasterizationTasks, () => {
rasterizationStatus.iconsPending = false;
postRasterizationLayout(symbolLayoutData, glyphAtlas, rasterizationStatus, m2);
});
this.rasterizeIfNeeded(actor, patternMap, patternRasterizationTasks, () => {
rasterizationStatus.patternsPending = false;
postRasterizationLayout(symbolLayoutData, glyphAtlas, rasterizationStatus, m2);
});
}
};
const postRasterizationLayout = (symbolLayoutData, glyphAtlas, rasterizationStatus, m2) => {
if (rasterizationStatus.iconsPending || rasterizationStatus.patternsPending) return;
const imageAtlas = new index.ImageAtlas(iconMap, patternMap, this.lut);
for (const key in buckets) {
const bucket = buckets[key];
if (key in symbolLayoutData) {
index.postRasterizationSymbolLayout(bucket, symbolLayoutData[key], this.showCollisionBoxes, availableImages, this.tileID.canonical, this.tileZoom, this.projection, this.brightness, iconMap, imageAtlas);
} else if (bucket.hasPattern && (bucket instanceof index.LineBucket || bucket instanceof index.FillBucket || bucket instanceof index.FillExtrusionBucket)) {
recalculateLayers(bucket.layers, this.zoom, options.brightness, availableImages, this.worldview);
const imagePositions = Object.fromEntries(imageAtlas.patternPositions);
bucket.addFeatures(options, this.tileID.canonical, imagePositions, availableImages, this.tileTransform, this.brightness);
}
}
this.status = "done";
callback(null, {
buckets: Object.values(buckets).filter((b) => !b.isEmpty()),
featureIndex,
collisionBoxArray: this.collisionBoxArray,
glyphAtlasImage: glyphAtlas.image,
lineAtlas,
imageAtlas,
brightness: options.brightness
});
index.PerformanceUtils.endMeasure(m2, [["tileID", this.tileID.toString()], ["source", this.source]]);
};
if (!this.extraShadowCaster) {
const stacks = index.mapObject(options.glyphDependencies, (glyphs) => Object.keys(glyphs).map(Number));
if (Object.keys(stacks).length) {
actor.send("getGlyphs", { uid: this.uid, stacks }, (err, result) => {
if (!error) {
error = err;
glyphMap = result;
maybePrepare();
}
}, void 0, false, taskMetadata);
} else {
glyphMap = {};
}
const images = Array.from(options.iconDependencies.keys()).map((id) => index.ImageId.parse(id));
if (images.length) {
const params = { images, source: this.source, scope: this.scope, tileID: this.tileID, type: "icons" };
actor.send("getImages", params, (err, result) => {
if (error) {
return;
}
error = err;
iconMap = /* @__PURE__ */ new Map();
iconRasterizationTasks = this.updateImageMapAndGetImageTaskQueue(iconMap, result, options.iconDependencies);
maybePrepare();
}, void 0, false, taskMetadata);
} else {
iconMap = /* @__PURE__ */ new Map();
iconRasterizationTasks = /* @__PURE__ */ new Map();
}
const patterns = Array.from(options.patternDependencies.keys()).map((id) => index.ImageId.parse(id));
if (patterns.length) {
const params = { images: patterns, source: this.source, scope: this.scope, tileID: this.tileID, type: "patterns" };
actor.send("getImages", params, (err, result) => {
if (error) {
return;
}
error = err;
patternMap = /* @__PURE__ */ new Map();
patternRasterizationTasks = this.updateImageMapAndGetImageTaskQueue(patternMap, result, options.patternDependencies);
maybePrepare();
}, void 0, false, taskMetadata);
} else {
patternMap = /* @__PURE__ */ new Map();
patternRasterizationTasks = /* @__PURE__ */ new Map();
}
}
if (options.elevationFeatures && options.elevationFeatures.length > 0) {
const unevaluatedPortals = [];
for (const bucket of Object.values(buckets)) {
if (bucket instanceof index.FillBucket) {
const graph = bucket.getUnevaluatedPortalGraph();
if (graph) {
unevaluatedPortals.push(graph);
}
}
}
const evaluatedPortals = index.ElevationPortalGraph.evaluate(unevaluatedPortals);
for (const bucket of Object.values(buckets)) {
if (bucket instanceof index.FillBucket) {
const vtLayer = data.layers[sourceLayerCoder.decode(bucket.sourceLayerIndex)];
index.assert(vtLayer);
bucket.setEvaluatedPortalGraph(evaluatedPortals, vtLayer, this.tileID.canonical, options.availableImages, options.brightness);
}
}
}
index.PerformanceUtils.endMeasure(m, [["tileID", this.tileID.toString()], ["source", this.source]]);
maybePrepare();
};
if (asyncBucketLoads.length > 0) {
Promise.allSettled(asyncBucketLoads).then(prepareTile).catch(callback);
} else {
prepareTile();
}
}
updateParameters(params) {
this.scaleFactor = params.scaleFactor;
this.showCollisionBoxes = params.showCollisionBoxes;
this.projection = params.projection;
this.brightness = params.brightness;
this.tileTransform = index.tileTransform(params.tileID.canonical, params.projection);
this.extraShadowCaster = params.extraShadowCaster;
this.lut = params.lut;
this.worldview = params.worldview;
this.indoor = params.indoor;
}
rasterizeIfNeeded(actor, outputMap, tasks, callback) {
const needRasterization = Array.from(outputMap.values()).some((image) => image.usvg);
if (needRasterization) {
this.rasterize(actor, outputMap, tasks, callback);
} else {
callback();
}
}
updateImageMapAndGetImageTaskQueue(imageMap, images, imageDependencies) {
const imageRasterizationTasks = /* @__PURE__ */ new Map();
for (const imageName of images.keys()) {
const requiredImageVariants = imageDependencies.get(imageName) || [];
for (const imageVariant of requiredImageVariants) {
const imageVariantStr = imageVariant.toString();
const image = images.get(imageVariant.id.toString());
if (!image.usvg) {
imageMap.set(imageVariantStr, image);
} else if (!imageRasterizationTasks.has(imageVariantStr)) {
imageRasterizationTasks.set(imageVariantStr, imageVariant);
imageMap.set(imageVariantStr, Object.assign({}, image));
}
}
}
return imageRasterizationTasks;
}
rasterize(actor, imageMap, tasks, callback) {
this.rasterizeTask = actor.send("rasterizeImages", { scope: this.scope, tasks }, (err, rasterizedImages) => {
if (!err) {
for (const [id, data] of rasterizedImages.entries()) {
const image = Object.assign(imageMap.get(id), { data });
imageMap.set(id, image);
}
}
callback();
});
}
cancelRasterize() {
if (this.rasterizeTask) {
this.rasterizeTask.cancel();
}
}
}
function recalculateLayers(layers, zoom, brightness, availableImages, worldview) {
const parameters = new index.EvaluationParameters(zoom, { brightness, worldview });
for (const layer of layers) {
layer.recalculate(parameters, availableImages);
}
}
class VectorTileWorkerSource extends index.Evented {
/**
* @param [loadVectorData] Optional method for custom loading of a VectorTile
* object based on parameters passed from the main-thread Source. See
* {@link VectorTileWorkerSource#loadTile}. The default implementation simply
* loads the pbf at `params.url`.
* @private
*/
constructor(actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadVectorData, brightness) {
super();
this.actor = actor;
this.layerIndex = layerIndex;
this.availableImages = availableImages;
this.availableModels = availableModels;
this.loadVectorData = loadVectorData || index.loadVectorTile;
this.loading = {};
this.loaded = {};
this.deduped = new index.DedupedRequest(actor.scheduler);
this.isSpriteLoaded = isSpriteLoaded;
this.scheduler = actor.scheduler;
this.brightness = brightness;
}
/**
* Implements {@link WorkerSource#loadTile}. Delegates to
* {@link VectorTileWorkerSource#loadVectorData} (which by default expects
* a `params.url` property) for fetching and producing a VectorTile object.
* @private
*/
loadTile(params, callback) {
const uid = params.uid;
const requestParam = params && params.request;
const perf = requestParam && requestParam.collectResourceTiming;
const workerTile = this.loading[uid] = new WorkerTile(params);
workerTile.abort = this.loadVectorData(params, (err, response) => {
const aborted = !this.loading[uid];
delete this.loading[uid];
workerTile.cancelRasterize();
if (aborted || err || !response) {
workerTile.status = "done";
if (!aborted) this.loaded[uid] = workerTile;
return callback(err);
}
const rawTileData = response.rawData;
const cacheControl = {};
const expiryData = index.getExpiryDataFromHeaders(response.responseHeaders);
if (expiryData && expiryData.expires) cacheControl.expires = expiryData.expires;
if (expiryData && expiryData.cacheControl) cacheControl.cacheControl = expiryData.cacheControl;
workerTile.vectorTile = response.vectorTile || new index.VectorTile(new index.Pbf(rawTileData));
const parseTile = () => {
const WorkerSourceVectorTileCallback = (err2, result) => {
if (err2 || !result) return callback(err2);
const resourceTiming = {};
if (perf) {
const resourceTimingData = index.getPerformanceMeasurement(requestParam);
if (resourceTimingData.length > 0) {
resourceTiming.resourceTiming = JSON.parse(JSON.stringify(resourceTimingData));
}
}
callback(null, Object.assign({ rawTileData: rawTileData.slice(0), responseHeaders: response.responseHeaders }, result, cacheControl, resourceTiming));
};
workerTile.parse(workerTile.vectorTile, this.layerIndex, this.availableImages, this.availableModels, this.actor, WorkerSourceVectorTileCallback);
};
if (this.isSpriteLoaded) {
parseTile();
} else {
this.once("isSpriteLoaded", () => {
if (this.scheduler) {
const metadata = { type: "parseTile", isSymbolTile: params.isSymbolTile, zoom: params.tileZoom };
this.scheduler.add(parseTile, metadata);
} else {
parseTile();
}
});
}
this.loaded = this.loaded || {};
this.loaded[uid] = workerTile;
});
}
/**
* Implements {@link WorkerSource#reloadTile}.
* @private
*/
reloadTile(params, callback) {
const loaded = this.loaded, uid = params.uid;
if (loaded && loaded[uid]) {
const workerTile = loaded[uid];
workerTile.updateParameters(params);
const done = (err, data) => {
const reloadCallback = workerTile.reloadCallback;
if (reloadCallback) {
delete workerTile.reloadCallback;
workerTile.parse(workerTile.vectorTile, this.layerIndex, this.availableImages, this.availableModels, this.actor, reloadCallback);
}
callback(err, data);
};
if (workerTile.status === "parsing") {
workerTile.reloadCallback = done;
} else if (workerTile.status === "done") {
if (workerTile.vectorTile) {
workerTile.parse(workerTile.vectorTile, this.layerIndex, this.availableImages, this.availableModels, this.actor, done);
} else {
done();
}
}
} else {
callback(null, void 0);
}
}
/**
* Implements {@link WorkerSource#abortTile}.
* @private
*/
abortTile(params, callback) {
const uid = params.uid;
const tile = this.loading[uid];
if (tile) {
if (tile.abort) tile.abort();
delete this.loading[uid];
}
callback();
}
/**
* Implements {@link WorkerSource#removeTile}.
* @private
*/
removeTile(params, callback) {
const loaded = this.loaded, uid = params.uid;
if (loaded && loaded[uid]) {
delete loaded[uid];
}
callback();
}
}
class RasterDEMTileWorkerSource {
loadTile(params, callback) {
const { uid, encoding, rawImageData, padding } = params;
const imagePixels = ImageBitmap && rawImageData instanceof ImageBitmap ? this.getImageData(rawImageData, padding) : rawImageData;
const dem = new index.DEMData(uid, imagePixels, encoding, padding < 1);
callback(null, dem);
}
reloadTile(params, callback) {
callback(null, null);
}
abortTile(params, callback) {
callback();
}
removeTile(params, callback) {
callback();
}
getImageData(imgBitmap, padding) {
if (!this.offscreenCanvas || !this.offscreenCanvasContext) {
this.offscreenCanvas = new OffscreenCanvas(imgBitmap.width, imgBitmap.height);
this.offscreenCanvasContext = this.offscreenCanvas.getContext("2d", { willReadFrequently: true });
}
this.offscreenCanvas.width = imgBitmap.width;
this.offscreenCanvas.height = imgBitmap.height;
this.offscreenCanvasContext.drawImage(imgBitmap, 0, 0, imgBitmap.width, imgBitmap.height);
const imgData = this.offscreenCanvasContext.getImageData(-padding, -padding, imgBitmap.width + 2 * padding, imgBitmap.height + 2 * padding);
this.offscreenCanvasContext.clearRect(0, 0, this.offscreenCanvas.width, this.offscreenCanvas.height);
return imgData;
}
}
index.MapboxRasterTile.setPbf(index.Pbf);
const MRT_DECODED_BAND_CACHE_SIZE = 30;
class RasterArrayWorkerTile {
constructor(params) {
const cacheSize = params.partial ? MRT_DECODED_BAND_CACHE_SIZE : Infinity;
this._mrt = new index.MapboxRasterTile(cacheSize);
this._isHeaderLoaded = false;
this.uid = params.uid;
this.tileID = params.tileID;
this.source = params.source;
}
parse(buffer, callback) {
const mrt = this._mrt;
this.status = "parsing";
this._entireBuffer = buffer;
try {
mrt.parseHeader(buffer);
this._isHeaderLoaded = true;
const decodingTasks = [];
for (const layerId in mrt.layers) {
const layer = mrt.getLayer(layerId);
const range = layer.getDataRange(layer.getBandList());
const task = mrt.createDecodingTask(range);
const bufferSlice = buffer.slice(range.firstByte, range.lastByte + 1);
const decodingTask = index.MapboxRasterTile.performDecoding(bufferSlice, task).then((result) => task.complete(null, result)).catch((error) => task.complete(error, null));
decodingTasks.push(decodingTask);
}
Promise.allSettled(decodingTasks).then(() => callback(null, mrt)).catch((error) => callback(error));
} catch (error) {
callback(error);
}
}
}
class RasterArrayTileWorkerSource {
constructor(actor) {
this.actor = actor;
this.loading = {};
this.loaded = {};
}
loadTile(params, callback) {
const uid = params.uid;
const requestParam = params.request;
const workerTile = this.loading[uid] = new RasterArrayWorkerTile(params);
const { cancel } = index.getArrayBuffer(requestParam, (error, buffer, headers) => {
const aborted = !this.loading[uid];
delete this.loading[uid];
if (aborted || error || !buffer) {
workerTile.status = "done";
if (!aborted) this.loaded[uid] = workerTile;
return callback(error);
}
workerTile.parse(buffer, (error2, mrt) => {
if (error2 || !mrt) return callback(error2);
callback(null, mrt, headers);
});
this.loaded[uid] = workerTile;
});
workerTile.abort = cancel;
}
reloadTile(params, callback) {
callback(null, void 0);
}
abortTile(params, callback) {
const uid = params.uid;
const workerTile = this.loading[uid];
if (workerTile) {
if (workerTile.abort) workerTile.abort();
delete this.loading[uid];
}
callback();
}
removeTile(params, callback) {
const uid = params.uid;
if (this.loaded[uid]) {
delete this.loaded[uid];
}
callback();
}
decodeRasterArray(params, callback) {
index.MapboxRasterTile.performDecoding(params.buffer, params.task).then((result) => callback(null, result)).catch((error) => callback(error));
}
}
const toGeoJSON = index.VectorTileFeature.prototype.toGeoJSON;
class FeatureWrapper {
constructor(feature) {
this._feature = feature;
this.extent = index.EXTENT;
this.type = feature.type;
this.properties = feature.tags;
if ("id" in feature && !isNaN(feature.id)) {
this.id = parseInt(feature.id, 10);
}
}
loadGeometry() {
if (this._feature.type === 1) {
const geometry = [];
for (const point of this._feature.geometry) {
geometry.push([new index.Point(point[0], point[1])]);
}
return geometry;
} else {
const geometry = [];
for (const ring of this._feature.geometry) {
const newRing = [];
for (const point of ring) {
newRing.push(new index.Point(point[0], point[1]));
}
geometry.push(newRing);
}
return geometry;
}
}
toGeoJSON(x, y, z) {
return toGeoJSON.call(this, x, y, z);
}
}
class LayerWrapper {
constructor(name, features) {
this.name = name;
this.extent = index.EXTENT;
this.length = features.length;
this._jsonFeatures = features;
}
feature(i) {
return new FeatureWrapper(this._jsonFeatures[i]);
}
}
class GeoJSONWrapper {
constructor(layers) {
this.layers = {};
this.extent = index.EXTENT;
for (const name of Object.keys(layers)) {
this.layers[name] = new LayerWrapper(name, layers[name]);
}
}
}
const PAD = 64 / 4096;
const PAD_PX = 128;
class GeoJSONRT {
constructor() {
this.features = /* @__PURE__ */ new Map();
}
clear() {
this.features.clear();
}
load(features = [], cache) {
for (const feature of features) {
const id = feature.id;
if (id == null) continue;
let updated = this.features.get(id);
if (updated) this.updateCache(updated, cache);
if (!feature.geometry) {
this.features.delete(id);
} else {
updated = convertFeature$1(feature);
this.updateCache(updated, cache);
this.features.set(id, updated);
}
this.updateCache(updated, cache);
}
}
// clear all tiles that contain a given feature from the tile cache
updateCache(feature, cache) {
for (const { canonical, uid } of Object.values(cache)) {
const { z, x, y } = canonical;
const z2 = Math.pow(2, z);
if (intersectsTile(feature, z2, x, y)) {
delete cache[uid];
}
}
}
// return all features that fit in the tile (plus a small padding) by bbox; since dynamic mode is
// for "small data, frequently updated" case, linear loop through all features should be fast enough
getTile(z, tx, ty) {
const z2 = Math.pow(2, z);
const features = [];
for (const feature of this.features.values()) {
if (intersectsTile(feature, z2, tx, ty)) {
features.push(outputFeature(feature, z2, tx, ty));
}
}
return { features };
}
getFeatures() {
return [...this.features.values()];
}
}
function intersectsTile({ minX, minY, maxX, maxY }, z2, tx, ty) {
const x0 = (tx - PAD) / z2;
const y0 = (ty - PAD) / z2;
const x1 = (tx + 1 + PAD) / z2;
const y1 = (ty + 1 + PAD) / z2;
return minX < x1 && minY < y1 && maxX > x0 && maxY > y0;
}
function convertFeature$1(originalFeature) {
const { id, geometry, properties } = originalFeature;
if (!geometry) return;
if (geometry.type === "GeometryCollection") {
throw new Error("GeometryCollection not supported in dynamic mode.");
}
const { type, coordinates } = geometry;
const feature = {
id,
type: 1,
geometry: [],
tags: properties,
minX: Infinity,
minY: Infinity,
maxX: -Infinity,
maxY: -Infinity
};
const geom = feature.geometry;
if (type === "Point") {
convertPoint$1(coordinates, geom, feature);
} else if (type === "MultiPoint") {
for (const p of coordinates) {
convertPoint$1(p, geom, feature);
}
} else if (type === "LineString") {
feature.type = 2;
convertLine$1(coordinates, geom, feature);
} else if (type === "MultiLineString") {
feature.type = 2;
convertLines$1(coordinates, geom, feature);
} else if (type === "Polygon") {
feature.type = 3;
convertLines$1(coordinates, geom, feature, true);
} else if (type === "MultiPolygon") {
feature.type = 3;
for (const polygon of coordinates) {
convertLines$1(polygon, geom, feature, true);
}
} else {
throw new Error("Input data is not a valid GeoJSON object.");
}
return feature;
}
function convertPoint$1([lng, lat], out, bbox) {
const x = index.mercatorXfromLng(lng);
let y = index.mercatorYfromLat(lat);
y = y < 0 ? 0 : y > 1 ? 1 : y;
out.push(x, y);
bbox.minX = Math.min(bbox.minX, x);
bbox.minY = Math.min(bbox.minY, y);
bbox.maxX = Math.max(bbox.maxX, x);
bbox.maxY = Math.max(bbox.maxY, y);
}
function convertLine$1(ring, out, bbox, isPolygon = false, isOuter = false) {
const newLine = [];
for (const p of ring) {
convertPoint$1(p, newLine, bbox);
}
out.push(newLine);
if (isPolygon) rewind$1(newLine, isOuter);
}
function convertLines$1(lines, out, bbox, isPolygon = false) {
for (let i = 0; i < lines.length; i++) {
convertLine$1(lines[i], out, bbox, isPolygon, i === 0);
}
}
function outputFeature(feature, z2, tx, ty) {
const { id, type, geometry, tags } = feature;
const tileGeom = [];
if (type === 1) {
transformPoints(geometry, z2, tx, ty, tileGeom);
} else if (type === 2) {
for (const ring of geometry) {
transformAndClipLine(ring, z2, tx, ty, tileGeom);
}
} else if (type === 3) {
for (const ring of geometry) {
transformAndClipPolygon(ring, z2, tx, ty, tileGeom);
}
}
return {
id,
type,
geometry: tileGeom,
tags
};
}
function transformPoints(line, z2, tx, ty, out) {
for (let i = 0; i < line.length; i += 2) {
const ox = Math.round(index.EXTENT * (line[i + 0] * z2 - tx));
const oy = Math.round(index.EXTENT * (line[i + 1] * z2 - ty));
out.push([ox, oy]);
}
}
function transformAndClipLine(line, z2, tx, ty, out) {
const min = -PAD_PX;
const max = index.EXTENT + PAD_PX;
let part;
for (let i = 0; i < line.length - 2; i += 2) {
let x0 = Math.round(index.EXTENT * (line[i + 0] * z2 - tx));
let y0 = Math.round(index.EXTENT * (line[i + 1] * z2 - ty));
let x1 = Math.round(index.EXTENT * (line[i + 2] * z2 - tx));
let y1 = Math.round(index.EXTENT * (line[i + 3] * z2 - ty));
const dx = x1 - x0;
const dy = y1 - y0;
if (x0 < min && x1 < min) {
continue;
} else if (x0 < min) {
y0 = y0 + Math.round(dy * ((min - x0) / dx));
x0 = min;
} else if (x1 < min) {
y1 = y0 + Math.round(dy * ((min - x0) / dx));
x1 = min;
}
if (y0 < min && y1 < min) {
continue;
} else if (y0 < min) {
x0 = x0 + Math.round(dx * ((min - y0) / dy));
y0 = min;
} else if (y1 < min) {
x1 = x0 + Math.round(dx * ((min - y0) / dy));
y1 = min;
}
if (x0 >= max && x1 >= max) {
continue;
} else if (x0 >= max) {
y0 = y0 + Math.round(dy * ((max - x0) / dx));
x0 = max;
} else if (x1 >= max) {
y1 = y0 + Math.round(dy * ((max - x0) / dx));
x1 = max;
}
if (y0 >= max && y1 >= max) {
continue;
} else if (y0 >= max) {
x0 = x0 + Math.round(dx * ((max - y0) / dy));
y0 = max;
} else if (y1 >= max) {
x1 = x0 + Math.round(dx * ((max - y0) / dy));
y1 = max;
}
if (!part || x0 !== part[part.length - 1][0] || y0 !== part[part.length - 1][1]) {
part = [[x0, y0]];
out.push(part);
}
part.push([x1, y1]);
}
}
function transformAndClipPolygon(input, z2, tx, ty, out) {
const minX = (tx - PAD) / z2;
const minY = (ty - PAD) / z2;
const maxX = (tx + 1 + PAD) / z2;
const maxY = (ty + 1 + PAD) / z2;
function bitCode(x, y) {
let code = 0;
if (x < minX) code |= 1;
else if (x > maxX) code |= 2;
if (y < minY) code |= 4;
else if (y > maxY) code |= 8;
return code;
}
let clipped = [];
for (let edge = 1; edge <= 8; edge *= 2) {
let x0 = input[input.length - 2];
let y0 = input[input.length - 1];
let prevInside = !(bitCode(x0, y0) & edge);
for (let i = 0; i < input.length; i += 2) {
const x1 = input[i];
const y1 = input[i + 1];
const inside = !(bitCode(x1, y1) & edge);
if (inside !== prevInside) {
if (edge & 8) clipped.push(x0 + (x1 - x0) * (maxY - y0) / (y1 - y0), maxY);
else if (edge & 4) clipped.push(x0 + (x1 - x0) * (minY - y0) / (y1 - y0), minY);
else if (edge & 2) clipped.push(maxX, y0 + (y1 - y0) * (maxX - x0) / (x1 - x0));
else if (edge & 1) clipped.push(minX, y0 + (y1 - y0) * (minX - x0) / (x1 - x0));
}
if (inside) clipped.push(x1, y1);
x0 = x1;
y0 = y1;
prevInside = inside;
}
input = clipped;
if (!input.length || edge === 8) break;
clipped = [];
}
const ring = [];
for (let i = 0; i < clipped.length; i += 2) ring.push([
Math.round(index.EXTENT * (clipped[i] * z2 - tx)),
Math.round(index.EXTENT * (clipped[i + 1] * z2 - ty))
]);
out.push(ring);
}
function rewind$1(ring, clockwise) {
let area = 0;
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
}
if (area > 0 === clockwise) {
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
const x = ring[i];
const y = ring[i + 1];
ring[i] = ring[len - 2 - i];
ring[i + 1] = ring[len - 1 - i];
ring[len - 2 - i] = x;
ring[len - 1 - i] = y;
}
}
}
function writeFeatures(layers) {
const pbf = new index.Pbf();
for (const name of Object.keys(layers)) {
const features = layers[name];
pbf.writeMessage(3, writeLayer, { name, features });
}
return pbf.finish();
}
function writeLayer({ name, features }, pbf) {
pbf.writeStringField(1, name);
pbf.writeVarintField(5, index.EXTENT);
const keys = /* @__PURE__ */ new Map();
const values = /* @__PURE__ */ new Map();
const context = {
keys,
values,
feature: null
};
for (const feature of features) {
context.feature = feature;
pbf.writeMessage(2, writeFeature, context);
}
for (const key of keys.keys()) {
pbf.writeStringField(3, key);
}
for (const value of values.keys()) {
pbf.writeMessage(4, writeValue, value);
}
}
function writeFeature(context, pbf) {
const feature = context.feature;
if (feature.id !== void 0 && Number.isSafeInteger(+feature.id)) {
pbf.writeVarintField(1, +feature.id);
}
if (feature.tags) pbf.writeMessage(2, writeProperties, context);
pbf.writeVarintField(3, feature.type);
pbf.writeMessage(4, writeGeometry, feature);
}
function writeProperties({ keys, values, feature }, pbf) {
for (const key of Object.keys(feature.tags)) {
let value = feature.tags[key];
if (value === null) continue;
let keyIndex = keys.get(key);
if (keyIndex === void 0) {
keyIndex = keys.size;
keys.set(key, keyIndex);
}
pbf.writeVarint(keyIndex);
const type = typeof value;
if (type !== "string" && type !== "boolean" && type !== "number") {
value = JSON.stringify(value);
}
let valueIndex = values.get(value);
if (valueIndex === void 0) {
valueIndex = values.size;
values.set(value, valueIndex);
}
pbf.writeVarint(valueIndex);
}
}
function command(cmd, length) {
return (length << 3) + (cmd & 7);
}
function zigzag(num) {
return num << 1 ^ num >> 31;
}
function writeGeometry(feature, pbf) {
const { geometry, type } = feature;
let x = 0;
let y = 0;
if (type === 1) {
pbf.writeVarint(command(1, geometry.length));
for (const p of geometry) {
const dx = p[0] - x;
const dy = p[1] - y;
pbf.writeVarint(zigzag(dx));
pbf.writeVarint(zigzag(dy));
x += dx;
y += dy;
}
} else {
for (const ring of geometry) {
pbf.writeVarint(command(1, 1));
const lineCount = ring.length - (type === 3 ? 1 : 0);
for (let i = 0; i < lineCount; i++) {
if (i === 1) pbf.writeVarint(command(2, lineCount - 1));
const dx = ring[i][0] - x;
const dy = ring[i][1] - y;
pbf.writeVarint(zigzag(dx));
pbf.writeVarint(zigzag(dy));
x += dx;
y += dy;
}
if (type === 3) {
pbf.writeVarint(command(7, 1));
}
}
}
}
function writeValue(value, pbf) {
const type = typeof value;
if (type === "string") {
pbf.writeStringField(1, value);
} else if (type === "boolean") {
pbf.writeBooleanField(7, value);
} else if (type === "number") {
if (value % 1 !== 0) {
pbf.writeDoubleField(3, value);
} else if (value < 0) {
pbf.writeSVarintField(6, value);
} else {
pbf.writeVarintField(5, value);
}
}
}
const defaultOptions$1 = {
minZoom: 0, // min zoom to generate clusters on
maxZoom: 16, // max zoom level to cluster the points on
minPoints: 2, // minimum points to form a cluster
radius: 40, // cluster radius in pixels
extent: 512, // tile extent (radius is calculated relative to it)
nodeSize: 64, // size of the KD-tree leaf node, affects performance
log: false, // whether to log timing info
// whether to generate numeric ids for input features (in vector tiles)
generateId: false,
// a reduce function for calculating custom cluster properties
reduce: null, // (accumulated, props) => { accumulated.sum += props.sum; }
// properties to use for individual points when running the reducer
map: props => props // props => ({sum: props.my_value})
};
const fround = Math.fround || (tmp => ((x) => { tmp[0] = +x; return tmp[0]; }))(new Float32Array(1));
const OFFSET_ZOOM = 2;
const OFFSET_ID = 3;
const OFFSET_PARENT = 4;
const OFFSET_NUM = 5;
const OFFSET_PROP = 6;
class Supercluster {
constructor(options) {
this.options = Object.assign(Object.create(defaultOptions$1), options);
this.trees = new Array(this.options.maxZoom + 1);
this.stride = this.options.reduce ? 7 : 6;
this.clusterProps = [];
}
load(points) {
const {log, minZoom, maxZoom} = this.options;
if (log) console.time('total time');
const timerId = `prepare ${ points.length } points`;
if (log) console.time(timerId);
this.points = points;
// generate a cluster object for each point and index input points into a KD-tree
const data = [];
for (let i = 0; i < points.length; i++) {
const p = points[i];
if (!p.geometry) continue;
const [lng, lat] = p.geometry.coordinates;
const x = fround(lngX(lng));
const y = fround(latY(lat));
// store internal point/cluster data in flat numeric arrays for performance
data.push(
x, y, // projected point coordinates
Infinity, // the last zoom the point was processed at
i, // index of the source feature in the original input array
-1, // parent cluster id
1 // number of points in a cluster
);
if (this.options.reduce) data.push(0); // noop
}
let tree = this.trees[maxZoom + 1] = this._createTree(data);
if (log) console.timeEnd(timerId);
// cluster points on max zoom, then cluster the results on previous zoom, etc.;
// results in a cluster hierarchy across zoom levels
for (let z = maxZoom; z >= minZoom; z--) {
const now = +Date.now();
// create a new set of clusters for the zoom and index them with a KD-tree
tree = this.trees[z] = this._createTree(this._cluster(tree, z));
if (log) console.log('z%d: %d clusters in %dms', z, tree.numItems, +Date.now() - now);
}
if (log) console.timeEnd('total time');
return this;
}
getClusters(bbox, zoom) {
let minLng = ((bbox[0] + 180) % 360 + 360) % 360 - 180;
const minLat = Math.max(-90, Math.min(90, bbox[1]));
let maxLng = bbox[2] === 180 ? 180 : ((bbox[2] + 180) % 360 + 360) % 360 - 180;
const maxLat = Math.max(-90, Math.min(90, bbox[3]));
if (bbox[2] - bbox[0] >= 360) {
minLng = -180;
maxLng = 180;
} else if (minLng > maxLng) {
const easternHem = this.getClusters([minLng, minLat, 180, maxLat], zoom);
const westernHem = this.getClusters([-180, minLat, maxLng, maxLat], zoom);
return easternHem.concat(westernHem);
}
const tree = this.trees[this._limitZoom(zoom)];
const ids = tree.range(lngX(minLng), latY(maxLat), lngX(maxLng), latY(minLat));
const data = tree.data;
const clusters = [];
for (const id of ids) {
const k = this.stride * id;
clusters.push(data[k + OFFSET_NUM] > 1 ? getClusterJSON(data, k, this.clusterProps) : this.points[data[k + OFFSET_ID]]);
}
return clusters;
}
getChildren(clusterId) {
const originId = this._getOriginId(clusterId);
const originZoom = this._getOriginZoom(clusterId);
const errorMsg = 'No cluster with the specified id.';
const tree = this.trees[originZoom];
if (!tree) throw new Error(errorMsg);
const data = tree.data;
if (originId * this.stride >= data.length) throw new Error(errorMsg);
const r = this.options.radius / (this.options.extent * Math.pow(2, originZoom - 1));
const x = data[originId * this.stride];
const y = data[originId * this.stride + 1];
const ids = tree.within(x, y, r);
const children = [];
for (const id of ids) {
const k = id * this.stride;
if (data[k + OFFSET_PARENT] === clusterId) {
children.push(data[k + OFFSET_NUM] > 1 ? getClusterJSON(data, k, this.clusterProps) : this.points[data[k + OFFSET_ID]]);
}
}
if (children.length === 0) throw new Error(errorMsg);
return children;
}
getLeaves(clusterId, limit, offset) {
limit = limit || 10;
offset = offset || 0;
const leaves = [];
this._appendLeaves(leaves, clusterId, limit, offset, 0);
return leaves;
}
getTile(z, x, y) {
const tree = this.trees[this._limitZoom(z)];
const z2 = Math.pow(2, z);
const {extent, radius} = this.options;
const p = radius / extent;
const top = (y - p) / z2;
const bottom = (y + 1 + p) / z2;
const tile = {
features: []
};
this._addTileFeatures(
tree.range((x - p) / z2, top, (x + 1 + p) / z2, bottom),
tree.data, x, y, z2, tile);
if (x === 0) {
this._addTileFeatures(
tree.range(1 - p / z2, top, 1, bottom),
tree.data, z2, y, z2, tile);
}
if (x === z2 - 1) {
this._addTileFeatures(
tree.range(0, top, p / z2, bottom),
tree.data, -1, y, z2, tile);
}
return tile.features.length ? tile : null;
}
getClusterExpansionZoom(clusterId) {
let expansionZoom = this._getOriginZoom(clusterId) - 1;
while (expansionZoom <= this.options.maxZoom) {
const children = this.getChildren(clusterId);
expansionZoom++;
if (children.length !== 1) break;
clusterId = children[0].properties.cluster_id;
}
return expansionZoom;
}
_appendLeaves(result, clusterId, limit, offset, skipped) {
const children = this.getChildren(clusterId);
for (const child of children) {
const props = child.properties;
if (props && props.cluster) {
if (skipped + props.point_count <= offset) {
// skip the whole cluster
skipped += props.point_count;
} else {
// enter the cluster
skipped = this._appendLeaves(result, props.cluster_id, limit, offset, skipped);
// exit the cluster
}
} else if (skipped < offset) {
// skip a single point
skipped++;
} else {
// add a single point
result.push(child);
}
if (result.length === limit) break;
}
return skipped;
}
_createTree(data) {
const tree = new index.KDBush(data.length / this.stride | 0, this.options.nodeSize, Float32Array);
for (let i = 0; i < data.length; i += this.stride) tree.add(data[i], data[i + 1]);
tree.finish();
tree.data = data;
return tree;
}
_addTileFeatures(ids, data, x, y, z2, tile) {
for (const i of ids) {
const k = i * this.stride;
const isCluster = data[k + OFFSET_NUM] > 1;
let tags, px, py;
if (isCluster) {
tags = getClusterProperties(data, k, this.clusterProps);
px = data[k];
py = data[k + 1];
} else {
const p = this.points[data[k + OFFSET_ID]];
tags = p.properties;
const [lng, lat] = p.geometry.coordinates;
px = lngX(lng);
py = latY(lat);
}
const f = {
type: 1,
geometry: [[
Math.round(this.options.extent * (px * z2 - x)),
Math.round(this.options.extent * (py * z2 - y))
]],
tags
};
// assign id
let id;
if (isCluster || this.options.generateId) {
// optionally generate id for points
id = data[k + OFFSET_ID];
} else {
// keep id if already assigned
id = this.points[data[k + OFFSET_ID]].id;
}
if (id !== undefined) f.id = id;
tile.features.push(f);
}
}
_limitZoom(z) {
return Math.max(this.options.minZoom, Math.min(Math.floor(+z), this.options.maxZoom + 1));
}
_cluster(tree, zoom) {
const {radius, extent, reduce, minPoints} = this.options;
const r = radius / (extent * Math.pow(2, zoom));
const data = tree.data;
const nextData = [];
const stride = this.stride;
// loop through each point
for (let i = 0; i < data.length; i += stride) {
// if we've already visited the point at this zoom level, skip it
if (data[i + OFFSET_ZOOM] <= zoom) continue;
data[i + OFFSET_ZOOM] = zoom;
// find all nearby points
const x = data[i];
const y = data[i + 1];
const neighborIds = tree.within(data[i], data[i + 1], r);
const numPointsOrigin = data[i + OFFSET_NUM];
let numPoints = numPointsOrigin;
// count the number of points in a potential cluster
for (const neighborId of neighborIds) {
const k = neighborId * stride;
// filter out neighbors that are already processed
if (data[k + OFFSET_ZOOM] > zoom) numPoints += data[k + OFFSET_NUM];
}
// if there were neighbors to merge, and there are enough points to form a cluster
if (numPoints > numPointsOrigin && numPoints >= minPoints) {
let wx = x * numPointsOrigin;
let wy = y * numPointsOrigin;
let clusterProperties;
let clusterPropIndex = -1;
// encode both zoom and point index on which the cluster originated -- offset by total length of features
const id = ((i / stride | 0) << 5) + (zoom + 1) + this.points.length;
for (const neighborId of neighborIds) {
const k = neighborId * stride;
if (data[k + OFFSET_ZOOM] <= zoom) continue;
data[k + OFFSET_ZOOM] = zoom; // save the zoom (so it doesn't get processed twice)
const numPoints2 = data[k + OFFSET_NUM];
wx += data[k] * numPoints2; // accumulate coordinates for calculating weighted center
wy += data[k + 1] * numPoints2;
data[k + OFFSET_PARENT] = id;
if (reduce) {
if (!clusterProperties) {
clusterProperties = this._map(data, i, true);
clusterPropIndex = this.clusterProps.length;
this.clusterProps.push(clusterProperties);
}
reduce(clusterProperties, this._map(data, k));
}
}
data[i + OFFSET_PARENT] = id;
nextData.push(wx / numPoints, wy / numPoints, Infinity, id, -1, numPoints);
if (reduce) nextData.push(clusterPropIndex);
} else { // left points as unclustered
for (let j = 0; j < stride; j++) nextData.push(data[i + j]);
if (numPoints > 1) {
for (const neighborId of neighborIds) {
const k = neighborId * stride;
if (data[k + OFFSET_ZOOM] <= zoom) continue;
data[k + OFFSET_ZOOM] = zoom;
for (let j = 0; j < stride; j++) nextData.push(data[k + j]);
}
}
}
}
return nextData;
}
// get index of the point from which the cluster originated
_getOriginId(clusterId) {
return (clusterId - this.points.length) >> 5;
}
// get zoom of the point from which the cluster originated
_getOriginZoom(clusterId) {
return (clusterId - this.points.length) % 32;
}
_map(data, i, clone) {
if (data[i + OFFSET_NUM] > 1) {
const props = this.clusterProps[data[i + OFFSET_PROP]];
return clone ? Object.assign({}, props) : props;
}
const original = this.points[data[i + OFFSET_ID]].properties;
const result = this.options.map(original);
return clone && result === original ? Object.assign({}, result) : result;
}
}
function getClusterJSON(data, i, clusterProps) {
return {
type: 'Feature',
id: data[i + OFFSET_ID],
properties: getClusterProperties(data, i, clusterProps),
geometry: {
type: 'Point',
coordinates: [xLng(data[i]), yLat(data[i + 1])]
}
};
}
function getClusterProperties(data, i, clusterProps) {
const count = data[i + OFFSET_NUM];
const abbrev =
count >= 10000 ? `${Math.round(count / 1000) }k` :
count >= 1000 ? `${Math.round(count / 100) / 10 }k` : count;
const propIndex = data[i + OFFSET_PROP];
const properties = propIndex === -1 ? {} : Object.assign({}, clusterProps[propIndex]);
return Object.assign(properties, {
cluster: true,
cluster_id: data[i + OFFSET_ID],
point_count: count,
point_count_abbreviated: abbrev
});
}
// longitude/latitude to spherical mercator in [0..1] range
function lngX(lng) {
return lng / 360 + 0.5;
}
function latY(lat) {
const sin = Math.sin(lat * Math.PI / 180);
const y = (0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI);
return y < 0 ? 0 : y > 1 ? 1 : y;
}
// spherical mercator to longitude/latitude
function xLng(x) {
return (x - 0.5) * 360;
}
function yLat(y) {
const y2 = (180 - y * 360) * Math.PI / 180;
return 360 * Math.atan(Math.exp(y2)) / Math.PI - 90;
}
// calculate simplification data using optimized Douglas-Peucker algorithm
function simplify(coords, first, last, sqTolerance) {
let maxSqDist = sqTolerance;
const mid = first + ((last - first) >> 1);
let minPosToMid = last - first;
let index;
const ax = coords[first];
const ay = coords[first + 1];
const bx = coords[last];
const by = coords[last + 1];
for (let i = first + 3; i < last; i += 3) {
const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
if (d > maxSqDist) {
index = i;
maxSqDist = d;
} else if (d === maxSqDist) {
// a workaround to ensure we choose a pivot close to the middle of the list,
// reducing recursion depth, for certain degenerate inputs
// https://github.com/mapbox/geojson-vt/issues/104
const posToMid = Math.abs(i - mid);
if (posToMid < minPosToMid) {
index = i;
minPosToMid = posToMid;
}
}
}
if (maxSqDist > sqTolerance) {
if (index - first > 3) simplify(coords, first, index, sqTolerance);
coords[index + 2] = maxSqDist;
if (last - index > 3) simplify(coords, index, last, sqTolerance);
}
}
// square distance from a point to a segment
function getSqSegDist(px, py, x, y, bx, by) {
let dx = bx - x;
let dy = by - y;
if (dx !== 0 || dy !== 0) {
const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
if (t > 1) {
x = bx;
y = by;
} else if (t > 0) {
x += dx * t;
y += dy * t;
}
}
dx = px - x;
dy = py - y;
return dx * dx + dy * dy;
}
function createFeature(id, type, geom, tags) {
const feature = {
id: id == null ? null : id,
type,
geometry: geom,
tags,
minX: Infinity,
minY: Infinity,
maxX: -Infinity,
maxY: -Infinity
};
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
calcLineBBox(feature, geom);
} else if (type === 'Polygon') {
// the outer ring (ie [0]) contains all inner rings
calcLineBBox(feature, geom[0]);
} else if (type === 'MultiLineString') {
for (const line of geom) {
calcLineBBox(feature, line);
}
} else if (type === 'MultiPolygon') {
for (const polygon of geom) {
// the outer ring (ie [0]) contains all inner rings
calcLineBBox(feature, polygon[0]);
}
}
return feature;
}
function calcLineBBox(feature, geom) {
for (let i = 0; i < geom.length; i += 3) {
feature.minX = Math.min(feature.minX, geom[i]);
feature.minY = Math.min(feature.minY, geom[i + 1]);
feature.maxX = Math.max(feature.maxX, geom[i]);
feature.maxY = Math.max(feature.maxY, geom[i + 1]);
}
}
// converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
function convert(data, options) {
const features = [];
if (data.type === 'FeatureCollection') {
for (let i = 0; i < data.features.length; i++) {
convertFeature(features, data.features[i], options, i);
}
} else if (data.type === 'Feature') {
convertFeature(features, data, options);
} else {
// single geometry or a geometry collection
convertFeature(features, {geometry: data}, options);
}
return features;
}
function convertFeature(features, geojson, options, index) {
if (!geojson.geometry) return;
const coords = geojson.geometry.coordinates;
if (coords && coords.length === 0) return;
const type = geojson.geometry.type;
const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
let geometry = [];
let id = geojson.id;
if (options.promoteId) {
id = geojson.properties[options.promoteId];
} else if (options.generateId) {
id = index || 0;
}
if (type === 'Point') {
convertPoint(coords, geometry);
} else if (type === 'MultiPoint') {
for (const p of coords) {
convertPoint(p, geometry);
}
} else if (type === 'LineString') {
convertLine(coords, geometry, tolerance, false);
} else if (type === 'MultiLineString') {
if (options.lineMetrics) {
// explode into linestrings to be able to track metrics
for (const line of coords) {
geometry = [];
convertLine(line, geometry, tolerance, false);
features.push(createFeature(id, 'LineString', geometry, geojson.properties));
}
return;
} else {
convertLines(coords, geometry, tolerance, false);
}
} else if (type === 'Polygon') {
convertLines(coords, geometry, tolerance, true);
} else if (type === 'MultiPolygon') {
for (const polygon of coords) {
const newPolygon = [];
convertLines(polygon, newPolygon, tolerance, true);
geometry.push(newPolygon);
}
} else if (type === 'GeometryCollection') {
for (const singleGeometry of geojson.geometry.geometries) {
convertFeature(features, {
id,
geometry: singleGeometry,
properties: geojson.properties
}, options, index);
}
return;
} else {
throw new Error('Input data is not a valid GeoJSON object.');
}
features.push(createFeature(id, type, geometry, geojson.properties));
}
function convertPoint(coords, out) {
out.push(projectX(coords[0]), projectY(coords[1]), 0);
}
function convertLine(ring, out, tolerance, isPolygon) {
let x0, y0;
let size = 0;
for (let j = 0; j < ring.length; j++) {
const x = projectX(ring[j][0]);
const y = projectY(ring[j][1]);
out.push(x, y, 0);
if (j > 0) {
if (isPolygon) {
size += (x0 * y - x * y0) / 2; // area
} else {
size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
}
}
x0 = x;
y0 = y;
}
const last = out.length - 3;
out[2] = 1;
simplify(out, 0, last, tolerance);
out[last + 2] = 1;
out.size = Math.abs(size);
out.start = 0;
out.end = out.size;
}
function convertLines(rings, out, tolerance, isPolygon) {
for (let i = 0; i < rings.length; i++) {
const geom = [];
convertLine(rings[i], geom, tolerance, isPolygon);
out.push(geom);
}
}
function projectX(x) {
return x / 360 + 0.5;
}
function projectY(y) {
const sin = Math.sin(y * Math.PI / 180);
const y2 = 0.5 - 0.25 * Math.log((1 + sin) / (1 - sin)) / Math.PI;
return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
}
/* clip features between two vertical or horizontal axis-parallel lines:
* | |
* ___|___ | /
* / | \____|____/
* | |
*
* k1 and k2 are the line coordinates
* axis: 0 for x, 1 for y
* minAll and maxAll: minimum and maximum coordinate value for all features
*/
function clip(features, scale, k1, k2, axis, minAll, maxAll, options) {
k1 /= scale;
k2 /= scale;
if (minAll >= k1 && maxAll < k2) return features; // trivial accept
else if (maxAll < k1 || minAll >= k2) return null; // trivial reject
const clipped = [];
for (const feature of features) {
const geometry = feature.geometry;
let type = feature.type;
const min = axis === 0 ? feature.minX : feature.minY;
const max = axis === 0 ? feature.maxX : feature.maxY;
if (min >= k1 && max < k2) { // trivial accept
clipped.push(feature);
continue;
} else if (max < k1 || min >= k2) { // trivial reject
continue;
}
let newGeometry = [];
if (type === 'Point' || type === 'MultiPoint') {
clipPoints(geometry, newGeometry, k1, k2, axis);
} else if (type === 'LineString') {
clipLine(geometry, newGeometry, k1, k2, axis, false, options.lineMetrics);
} else if (type === 'MultiLineString') {
clipLines(geometry, newGeometry, k1, k2, axis, false);
} else if (type === 'Polygon') {
clipLines(geometry, newGeometry, k1, k2, axis, true);
} else if (type === 'MultiPolygon') {
for (const polygon of geometry) {
const newPolygon = [];
clipLines(polygon, newPolygon, k1, k2, axis, true);
if (newPolygon.length) {
newGeometry.push(newPolygon);
}
}
}
if (newGeometry.length) {
if (options.lineMetrics && type === 'LineString') {
for (const line of newGeometry) {
clipped.push(createFeature(feature.id, type, line, feature.tags));
}
continue;
}
if (type === 'LineString' || type === 'MultiLineString') {
if (newGeometry.length === 1) {
type = 'LineString';
newGeometry = newGeometry[0];
} else {
type = 'MultiLineString';
}
}
if (type === 'Point' || type === 'MultiPoint') {
type = newGeometry.length === 3 ? 'Point' : 'MultiPoint';
}
clipped.push(createFeature(feature.id, type, newGeometry, feature.tags));
}
}
return clipped.length ? clipped : null;
}
function clipPoints(geom, newGeom, k1, k2, axis) {
for (let i = 0; i < geom.length; i += 3) {
const a = geom[i + axis];
if (a >= k1 && a <= k2) {
addPoint(newGeom, geom[i], geom[i + 1], geom[i + 2]);
}
}
}
function clipLine(geom, newGeom, k1, k2, axis, isPolygon, trackMetrics) {
let slice = newSlice(geom);
const intersect = axis === 0 ? intersectX : intersectY;
let len = geom.start;
let segLen, t;
for (let i = 0; i < geom.length - 3; i += 3) {
const ax = geom[i];
const ay = geom[i + 1];
const az = geom[i + 2];
const bx = geom[i + 3];
const by = geom[i + 4];
const a = axis === 0 ? ax : ay;
const b = axis === 0 ? bx : by;
let exited = false;
if (trackMetrics) segLen = Math.sqrt(Math.pow(ax - bx, 2) + Math.pow(ay - by, 2));
if (a < k1) {
// ---|--> | (line enters the clip region from the left)
if (b > k1) {
t = intersect(slice, ax, ay, bx, by, k1);
if (trackMetrics) slice.start = len + segLen * t;
}
} else if (a > k2) {
// | <--|--- (line enters the clip region from the right)
if (b < k2) {
t = intersect(slice, ax, ay, bx, by, k2);
if (trackMetrics) slice.start = len + segLen * t;
}
} else {
addPoint(slice, ax, ay, az);
}
if (b < k1 && a >= k1) {
// <--|--- | or <--|-----|--- (line exits the clip region on the left)
t = intersect(slice, ax, ay, bx, by, k1);
exited = true;
}
if (b > k2 && a <= k2) {
// | ---|--> or ---|-----|--> (line exits the clip region on the right)
t = intersect(slice, ax, ay, bx, by, k2);
exited = true;
}
if (!isPolygon && exited) {
if (trackMetrics) slice.end = len + segLen * t;
newGeom.push(slice);
slice = newSlice(geom);
}
if (trackMetrics) len += segLen;
}
// add the last point
let last = geom.length - 3;
const ax = geom[last];
const ay = geom[last + 1];
const az = geom[last + 2];
const a = axis === 0 ? ax : ay;
if (a >= k1 && a <= k2) addPoint(slice, ax, ay, az);
// close the polygon if its endpoints are not the same after clipping
last = slice.length - 3;
if (isPolygon && last >= 3 && (slice[last] !== slice[0] || slice[last + 1] !== slice[1])) {
addPoint(slice, slice[0], slice[1], slice[2]);
}
// add the final slice
if (slice.length) {
newGeom.push(slice);
}
}
function newSlice(line) {
const slice = [];
slice.size = line.size;
slice.start = line.start;
slice.end = line.end;
return slice;
}
function clipLines(geom, newGeom, k1, k2, axis, isPolygon) {
for (const line of geom) {
clipLine(line, newGeom, k1, k2, axis, isPolygon, false);
}
}
function addPoint(out, x, y, z) {
out.push(x, y, z);
}
function intersectX(out, ax, ay, bx, by, x) {
const t = (x - ax) / (bx - ax);
addPoint(out, x, ay + (by - ay) * t, 1);
return t;
}
function intersectY(out, ax, ay, bx, by, y) {
const t = (y - ay) / (by - ay);
addPoint(out, ax + (bx - ax) * t, y, 1);
return t;
}
function wrap(features, options) {
const buffer = options.buffer / options.extent;
let merged = features;
const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
if (left || right) {
merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
if (left) merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
if (right) merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
}
return merged;
}
function shiftFeatureCoords(features, offset) {
const newFeatures = [];
for (let i = 0; i < features.length; i++) {
const feature = features[i];
const type = feature.type;
let newGeometry;
if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
newGeometry = shiftCoords(feature.geometry, offset);
} else if (type === 'MultiLineString' || type === 'Polygon') {
newGeometry = [];
for (const line of feature.geometry) {
newGeometry.push(shiftCoords(line, offset));
}
} else if (type === 'MultiPolygon') {
newGeometry = [];
for (const polygon of feature.geometry) {
const newPolygon = [];
for (const line of polygon) {
newPolygon.push(shiftCoords(line, offset));
}
newGeometry.push(newPolygon);
}
}
newFeatures.push(createFeature(feature.id, type, newGeometry, feature.tags));
}
return newFeatures;
}
function shiftCoords(points, offset) {
const newPoints = [];
newPoints.size = points.size;
if (points.start !== undefined) {
newPoints.start = points.start;
newPoints.end = points.end;
}
for (let i = 0; i < points.length; i += 3) {
newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
}
return newPoints;
}
// Transforms the coordinates of each feature in the given tile from
// mercator-projected space into (extent x extent) tile space.
function transformTile(tile, extent) {
if (tile.transformed) return tile;
const z2 = 1 << tile.z;
const tx = tile.x;
const ty = tile.y;
for (const feature of tile.features) {
const geom = feature.geometry;
const type = feature.type;
feature.geometry = [];
if (type === 1) {
for (let j = 0; j < geom.length; j += 2) {
feature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty));
}
} else {
for (let j = 0; j < geom.length; j++) {
const ring = [];
for (let k = 0; k < geom[j].length; k += 2) {
ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty));
}
feature.geometry.push(ring);
}
}
}
tile.transformed = true;
return tile;
}
function transformPoint(x, y, extent, z2, tx, ty) {
return [
Math.round(extent * (x * z2 - tx)),
Math.round(extent * (y * z2 - ty))];
}
function createTile(features, z, tx, ty, options) {
const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
const tile = {
features: [],
numPoints: 0,
numSimplified: 0,
numFeatures: features.length,
source: null,
x: tx,
y: ty,
z,
transformed: false,
minX: 2,
minY: 1,
maxX: -1,
maxY: 0
};
for (const feature of features) {
addFeature(tile, feature, tolerance, options);
}
return tile;
}
function addFeature(tile, feature, tolerance, options) {
const geom = feature.geometry;
const type = feature.type;
const simplified = [];
tile.minX = Math.min(tile.minX, feature.minX);
tile.minY = Math.min(tile.minY, feature.minY);
tile.maxX = Math.max(tile.maxX, feature.maxX);
tile.maxY = Math.max(tile.maxY, feature.maxY);
if (type === 'Point' || type === 'MultiPoint') {
for (let i = 0; i < geom.length; i += 3) {
simplified.push(geom[i], geom[i + 1]);
tile.numPoints++;
tile.numSimplified++;
}
} else if (type === 'LineString') {
addLine(simplified, geom, tile, tolerance, false, false);
} else if (type === 'MultiLineString' || type === 'Polygon') {
for (let i = 0; i < geom.length; i++) {
addLine(simplified, geom[i], tile, tolerance, type === 'Polygon', i === 0);
}
} else if (type === 'MultiPolygon') {
for (let k = 0; k < geom.length; k++) {
const polygon = geom[k];
for (let i = 0; i < polygon.length; i++) {
addLine(simplified, polygon[i], tile, tolerance, true, i === 0);
}
}
}
if (simplified.length) {
let tags = feature.tags || null;
if (type === 'LineString' && options.lineMetrics) {
tags = {};
for (const key in feature.tags) tags[key] = feature.tags[key];
tags['mapbox_clip_start'] = geom.start / geom.size;
tags['mapbox_clip_end'] = geom.end / geom.size;
}
const tileFeature = {
geometry: simplified,
type: type === 'Polygon' || type === 'MultiPolygon' ? 3 :
(type === 'LineString' || type === 'MultiLineString' ? 2 : 1),
tags
};
if (feature.id !== null) {
tileFeature.id = feature.id;
}
tile.features.push(tileFeature);
}
}
function addLine(result, geom, tile, tolerance, isPolygon, isOuter) {
const sqTolerance = tolerance * tolerance;
if (tolerance > 0 && (geom.size < (isPolygon ? sqTolerance : tolerance))) {
tile.numPoints += geom.length / 3;
return;
}
const ring = [];
for (let i = 0; i < geom.length; i += 3) {
if (tolerance === 0 || geom[i + 2] > sqTolerance) {
tile.numSimplified++;
ring.push(geom[i], geom[i + 1]);
}
tile.numPoints++;
}
if (isPolygon) rewind(ring, isOuter);
result.push(ring);
}
function rewind(ring, clockwise) {
let area = 0;
for (let i = 0, len = ring.length, j = len - 2; i < len; j = i, i += 2) {
area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
}
if (area > 0 === clockwise) {
for (let i = 0, len = ring.length; i < len / 2; i += 2) {
const x = ring[i];
const y = ring[i + 1];
ring[i] = ring[len - 2 - i];
ring[i + 1] = ring[len - 1 - i];
ring[len - 2 - i] = x;
ring[len - 1 - i] = y;
}
}
}
const defaultOptions = {
maxZoom: 14, // max zoom to preserve detail on
indexMaxZoom: 5, // max zoom in the tile index
indexMaxPoints: 100000, // max number of points per tile in the tile index
tolerance: 3, // simplification tolerance (higher means simpler)
extent: 4096, // tile extent
buffer: 64, // tile buffer on each side
lineMetrics: false, // whether to calculate line metrics
promoteId: null, // name of a feature property to be promoted to feature.id
generateId: false, // whether to generate feature ids. Cannot be used with promoteId
debug: 0 // logging level (0, 1 or 2)
};
class GeoJSONVT {
constructor(data, options) {
options = this.options = extend(Object.create(defaultOptions), options);
const debug = options.debug;
if (debug) console.time('preprocess data');
if (options.maxZoom < 0 || options.maxZoom > 24) throw new Error('maxZoom should be in the 0-24 range');
if (options.promoteId && options.generateId) throw new Error('promoteId and generateId cannot be used together.');
// projects and adds simplification info
let features = convert(data, options);
// tiles and tileCoords are part of the public API
this.tiles = {};
this.tileCoords = [];
if (debug) {
console.timeEnd('preprocess data');
console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
console.time('generate tiles');
this.stats = {};
this.total = 0;
}
// wraps features (ie extreme west and extreme east)
features = wrap(features, options);
// start slicing from the top tile down
if (features.length) this.splitTile(features, 0, 0, 0);
if (debug) {
if (features.length) console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
console.timeEnd('generate tiles');
console.log('tiles generated:', this.total, JSON.stringify(this.stats));
}
}
// splits features from a parent tile to sub-tiles.
// z, x, and y are the coordinates of the parent tile
// cz, cx, and cy are the coordinates of the target tile
//
// If no target tile is specified, splitting stops when we reach the maximum
// zoom or the number of points is low as specified in the options.
splitTile(features, z, x, y, cz, cx, cy) {
const stack = [features, z, x, y];
const options = this.options;
const debug = options.debug;
// avoid recursion by using a processing queue
while (stack.length) {
y = stack.pop();
x = stack.pop();
z = stack.pop();
features = stack.pop();
const z2 = 1 << z;
const id = toID(z, x, y);
let tile = this.tiles[id];
if (!tile) {
if (debug > 1) console.time('creation');
tile = this.tiles[id] = createTile(features, z, x, y, options);
this.tileCoords.push({z, x, y});
if (debug) {
if (debug > 1) {
console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
console.timeEnd('creation');
}
const key = `z${ z}`;
this.stats[key] = (this.stats[key] || 0) + 1;
this.total++;
}
}
// save reference to original geometry in tile so that we can drill down later if we stop now
tile.source = features;
// if it's the first-pass tiling
if (cz == null) {
// stop tiling if we reached max zoom, or if the tile is too simple
if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
// if a drilldown to a specific tile
} else if (z === options.maxZoom || z === cz) {
// stop tiling if we reached base zoom or our target tile zoom
continue;
} else if (cz != null) {
// stop tiling if it's not an ancestor of the target tile
const zoomSteps = cz - z;
if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
}
// if we slice further down, no need to keep source geometry
tile.source = null;
if (features.length === 0) continue;
if (debug > 1) console.time('clipping');
// values we'll use for clipping
const k1 = 0.5 * options.buffer / options.extent;
const k2 = 0.5 - k1;
const k3 = 0.5 + k1;
const k4 = 1 + k1;
let tl = null;
let bl = null;
let tr = null;
let br = null;
let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
features = null;
if (left) {
tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
left = null;
}
if (right) {
tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
right = null;
}
if (debug > 1) console.timeEnd('clipping');
stack.push(tl || [], z + 1, x * 2, y * 2);
stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
}
}
getTile(z, x, y) {
z = +z;
x = +x;
y = +y;
const options = this.options;
const {extent, debug} = options;
if (z < 0 || z > 24) return null;
const z2 = 1 << z;
x = (x + z2) & (z2 - 1); // wrap tile x coordinate
const id = toID(z, x, y);
if (this.tiles[id]) return transformTile(this.tiles[id], extent);
if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
let z0 = z;
let x0 = x;
let y0 = y;
let parent;
while (!parent && z0 > 0) {
z0--;
x0 = x0 >> 1;
y0 = y0 >> 1;
parent = this.tiles[toID(z0, x0, y0)];
}
if (!parent || !parent.source) return null;
// if we found a parent tile containing the original geometry, we can drill down from it
if (debug > 1) {
console.log('found parent tile z%d-%d-%d', z0, x0, y0);
console.time('drilling down');
}
this.splitTile(parent.source, z0, x0, y0, z, x, y);
if (debug > 1) console.timeEnd('drilling down');
return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
}
}
function toID(z, x, y) {
return (((1 << z) * y + x) * 32) + z;
}
function extend(dest, src) {
for (const i in src) dest[i] = src[i];
return dest;
}
function geojsonvt(data, options) {
return new GeoJSONVT(data, options);
}
function loadGeoJSONTile(params, callback) {
const canonical = params.tileID.canonical;
if (!this._geoJSONIndex) {
callback(null, null);
return;
}
const geoJSONTile = this._geoJSONIndex.getTile(canonical.z, canonical.x, canonical.y);
if (!geoJSONTile) {
callback(null, null);
return;
}
const isElevationfeature = (f) => f.tags && "3d_elevation_id" in f.tags && "source" in f.tags && f.tags.source === "elevation";
const elevationFeatures = geoJSONTile.features.filter((f) => isElevationfeature(f));
let layers = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
_geojsonTileLayer: geoJSONTile.features
};
if (elevationFeatures.length > 0) {
const nonElevationFeatures = geoJSONTile.features.filter((f) => !isElevationfeature(f));
layers = {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
_geojsonTileLayer: nonElevationFeatures,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"hd_road_elevation": elevationFeatures
};
}
const vectorTile = new GeoJSONWrapper(layers);
const rawData = writeFeatures(layers).buffer;
callback(null, { vectorTile, rawData });
}
class GeoJSONWorkerSource extends VectorTileWorkerSource {
/**
* @param [loadGeoJSON] Optional method for custom loading/parsing of
* GeoJSON based on parameters passed from the main-thread Source.
* See {@link GeoJSONWorkerSource#loadGeoJSON}.
* @private
*/
constructor(actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadGeoJSON, brightness) {
super(actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadGeoJSONTile, brightness);
if (loadGeoJSON) {
this.loadGeoJSON = loadGeoJSON;
}
this._dynamicIndex = new GeoJSONRT();
}
/**
* Fetches (if appropriate), parses, and index geojson data into tiles. This
* preparatory method must be called before {@link GeoJSONWorkerSource#loadTile}
* can correctly serve up tiles.
*
* Defers to {@link GeoJSONWorkerSource#loadGeoJSON} for the fetching/parsing,
* expecting `callback(error, data)` to be called with either an error or a
* parsed GeoJSON object.
*
* When `loadData` requests come in faster than they can be processed,
* they are coalesced into a single request using the latest data.
* See {@link GeoJSONWorkerSource#coalesce}
*
* @private
*/
loadData(params, callback) {
const requestParam = params && params.request;
const perf = requestParam && requestParam.collectResourceTiming;
this._geoJSONIndex = null;
this.loadGeoJSON(params, (err, data) => {
if (err || !data) {
return callback(err);
} else if (typeof data !== "object") {
return callback(new Error(`Input data given to '${params.source}' is not a valid GeoJSON object.`));
} else {
try {
if (params.filter) {
const compiled = index.createExpression(params.filter, { type: "boolean", "property-type": "data-driven", overridable: false, transition: false });
if (compiled.result === "error")
throw new Error(compiled.value.map((err2) => `${err2.key}: ${err2.message}`).join(", "));
data.features = data.features.filter((feature) => compiled.value.evaluate({ zoom: 0 }, feature));
}
if (params.dynamic) {
if (data.type === "Feature") data = { type: "FeatureCollection", features: [data] };
if (!params.append) {
this._dynamicIndex.clear();
this.loaded = {};
}
this._dynamicIndex.load(data.features, this.loaded);
if (params.cluster) data.features = this._dynamicIndex.getFeatures();
} else {
this.loaded = {};
}
this._geoJSONIndex = // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
params.cluster ? new Supercluster(getSuperclusterOptions(params)).load(data.features) : params.dynamic ? this._dynamicIndex : (
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
geojsonvt(data, params.geojsonVtOptions)
);
} catch (err2) {
return callback(err2);
}
const result = {};
if (perf) {
const resourceTimingData = index.getPerformanceMeasurement(requestParam);
if (resourceTimingData) {
result.resourceTiming = {};
result.resourceTiming[params.source] = JSON.parse(JSON.stringify(resourceTimingData));
}
}
callback(null, result);
}
});
}
/**
* Implements {@link WorkerSource#reloadTile}.
*
* If the tile is loaded, uses the implementation in VectorTileWorkerSource.
* Otherwise, such as after a setData() call, we load the tile fresh.
*
* @private
*/
reloadTile(params, callback) {
const loaded = this.loaded, uid = params.uid;
if (loaded && loaded[uid]) {
if (params.partial) {
return callback(null, void 0);
}
return super.reloadTile(params, callback);
} else {
return this.loadTile(params, callback);
}
}
/**
* Fetch and parse GeoJSON according to the given params. Calls `callback`
* with `(err, data)`, where `data` is a parsed GeoJSON object.
*
* GeoJSON is loaded and parsed from `params.url` if it exists, or else
* expected as a literal (string or object) `params.data`.
*
* @param params
* @param [params.url] A URL to the remote GeoJSON data.
* @param [params.data] Literal GeoJSON data. Must be provided if `params.url` is not.
* @private
*/
loadGeoJSON(params, callback) {
if (params.request) {
index.getJSON(params.request, callback);
} else if (typeof params.data === "string") {
setTimeout(() => {
try {
return callback(null, JSON.parse(params.data));
} catch (e) {
return callback(new Error(`Input data given to '${params.source}' is not a valid GeoJSON object.`));
}
}, 0);
} else {
return callback(new Error(`Input data given to '${params.source}' is not a valid GeoJSON object.`));
}
}
getClusterExpansionZoom(params, callback) {
try {
callback(null, this._geoJSONIndex.getClusterExpansionZoom(params.clusterId));
} catch (e) {
callback(e);
}
}
getClusterChildren(params, callback) {
try {
callback(null, this._geoJSONIndex.getChildren(params.clusterId));
} catch (e) {
callback(e);
}
}
getClusterLeaves(params, callback) {
try {
callback(null, this._geoJSONIndex.getLeaves(params.clusterId, params.limit, params.offset));
} catch (e) {
callback(e);
}
}
}
function getSuperclusterOptions({
superclusterOptions,
clusterProperties
}) {
if (!clusterProperties || !superclusterOptions) return superclusterOptions;
const mapExpressions = {};
const reduceExpressions = {};
const globals = { accumulated: null, zoom: 0 };
const feature = { properties: null };
const propertyNames = Object.keys(clusterProperties);
for (const key of propertyNames) {
const [operator, mapExpression] = clusterProperties[key];
const mapExpressionParsed = index.createExpression(mapExpression);
const reduceExpressionParsed = index.createExpression(
typeof operator === "string" ? [operator, ["accumulated"], ["get", key]] : operator
);
index.assert(mapExpressionParsed.result === "success");
index.assert(reduceExpressionParsed.result === "success");
mapExpressions[key] = mapExpressionParsed.value;
reduceExpressions[key] = reduceExpressionParsed.value;
}
superclusterOptions.map = (pointProperties) => {
feature.properties = pointProperties;
const properties = {};
for (const key of propertyNames) {
properties[key] = mapExpressions[key].evaluate(globals, feature);
}
return properties;
};
superclusterOptions.reduce = (accumulated, clusterProperties2) => {
feature.properties = clusterProperties2;
for (const key of propertyNames) {
globals.accumulated = accumulated[key];
accumulated[key] = reduceExpressions[key].evaluate(globals, feature);
}
};
return superclusterOptions;
}
class Tiled3dWorkerTile {
constructor(params, brightness, worldview) {
this.tileID = new index.OverscaledTileID(params.tileID.overscaledZ, params.tileID.wrap, params.tileID.canonical.z, params.tileID.canonical.x, params.tileID.canonical.y);
this.tileZoom = params.tileZoom;
this.uid = params.uid;
this.zoom = params.zoom;
this.canonical = params.tileID.canonical;
this.pixelRatio = params.pixelRatio;
this.tileSize = params.tileSize;
this.source = params.source;
this.overscaling = this.tileID.overscaleFactor();
this.projection = params.projection;
this.brightness = brightness;
this.worldview = worldview;
}
parse(data, layerIndex, params, callback) {
this.status = "parsing";
const tileID = new index.OverscaledTileID(params.tileID.overscaledZ, params.tileID.wrap, params.tileID.canonical.z, params.tileID.canonical.x, params.tileID.canonical.y);
const buckets = [];
const layerFamilies = layerIndex.familiesBySource[params.source];
const featureIndex = new index.FeatureIndex(tileID, params.promoteId);
featureIndex.bucketLayerIDs = [];
featureIndex.is3DTile = true;
index.load3DTile(data).then((gltf) => {
if (!gltf) return callback(new Error("Could not parse tile"));
const hasMapboxMeshFeatures = gltf.json.extensionsUsed && gltf.json.extensionsUsed.includes("MAPBOX_mesh_features") || gltf.json.asset.extras && gltf.json.asset.extras["MAPBOX_mesh_features"];
const hasMeshoptCompression = gltf.json.extensionsUsed && gltf.json.extensionsUsed.includes("EXT_meshopt_compression");
const parameters = new index.EvaluationParameters(this.zoom, { brightness: this.brightness, worldview: this.worldview });
for (const sourceLayerId in layerFamilies) {
for (const family of layerFamilies[sourceLayerId]) {
const layer = family[0];
featureIndex.bucketLayerIDs.push(family.map((l) => index.makeFQID(l.id, l.scope)));
layer.recalculate(parameters, []);
const nodes = index.process3DTile(gltf, 1 / index.tileToMeter(params.tileID.canonical));
const bucket = new index.Tiled3dModelBucket(family, nodes, tileID, hasMapboxMeshFeatures, hasMeshoptCompression, this.brightness, featureIndex, this.worldview);
if (!hasMapboxMeshFeatures) bucket.needsUpload = true;
buckets.push(bucket);
bucket.evaluate(layer);
}
}
this.status = "done";
callback(null, {
buckets,
featureIndex,
collisionBoxArray: null,
glyphAtlasImage: null,
lineAtlas: null,
imageAtlas: null,
brightness: null
});
}).catch((err) => callback(new Error(err.message)));
}
}
class Tiled3dModelWorkerSource {
constructor(actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadVectorData, brightness, worldview) {
this.actor = actor;
this.layerIndex = layerIndex;
this.availableImages = availableImages;
this.availableModels = availableModels;
this.brightness = brightness;
this.loading = {};
this.loaded = {};
this.worldview = worldview;
}
/**
* Implements {@link WorkerSource#loadTile}.
* @private
*/
loadTile(params, callback) {
const uid = params.uid;
const workerTile = this.loading[uid] = new Tiled3dWorkerTile(params, this.brightness, this.worldview);
index.getArrayBuffer(params.request, (err, data) => {
const aborted = !this.loading[uid];
delete this.loading[uid];
if (aborted || err) {
workerTile.status = "done";
if (!aborted) this.loaded[uid] = workerTile;
return callback(err);
}
if (!data || data.byteLength === 0) {
workerTile.status = "done";
this.loaded[uid] = workerTile;
return callback();
}
const workerSourceVectorTileCallback = (err2, result) => {
workerTile.status = "done";
this.loaded = this.loaded || {};
this.loaded[uid] = workerTile;
if (err2 || !result) callback(err2);
else callback(null, result);
};
workerTile.parse(data, this.layerIndex, params, workerSourceVectorTileCallback);
});
}
/**
* Implements {@link WorkerSource#reloadTile}.
* Re-parses a tile that has already been loaded. Yields the same data as {@link WorkerSource#loadTile}.
* @private
*/
reloadTile(params, callback) {
const loaded = this.loaded;
const uid = params.uid;
if (loaded && loaded[uid]) {
const workerTile = loaded[uid];
workerTile.projection = params.projection;
workerTile.brightness = params.brightness;
const done = (err, data) => {
const reloadCallback = workerTile.reloadCallback;
if (reloadCallback) {
delete workerTile.reloadCallback;
this.loadTile(params, callback);
}
callback(err, data);
};
if (workerTile.status === "parsing") {
workerTile.reloadCallback = done;
} else if (workerTile.status === "done") {
this.loadTile(params, callback);
}
}
}
/**
* Implements {@link WorkerSource#abortTile}.
* Aborts loading a tile that is in progress.
* @private
*/
abortTile(params, callback) {
const uid = params.uid;
const tile = this.loading[uid];
if (tile) {
delete this.loading[uid];
}
callback();
}
/**
* Implements {@link WorkerSource#removeTile}.
* Removes this tile from any local caches.
* @private
*/
removeTile(params, callback) {
const loaded = this.loaded, uid = params.uid;
if (loaded && loaded[uid]) {
delete loaded[uid];
}
callback();
}
}
class MapWorker {
constructor(self2) {
index.PerformanceUtils.measure("workerEvaluateScript");
this.self = self2;
this.actor = new index.Actor(self2, this);
this.layerIndexes = {};
this.availableImages = {};
this.availableModels = {};
this.isSpriteLoaded = {};
this.imageRasterizer = new index.ImageRasterizer();
this.rtlPluginParsingListeners = [];
this.projections = {};
this.defaultProjection = index.getProjection({ name: "mercator" });
this.workerSourceTypes = {
"vector": VectorTileWorkerSource,
"geojson": GeoJSONWorkerSource,
"raster-dem": RasterDEMTileWorkerSource,
"raster-array": RasterArrayTileWorkerSource,
"batched-model": Tiled3dModelWorkerSource
};
this.workerSources = {};
this.self.registerWorkerSource = (name, WorkerSource) => {
if (this.workerSourceTypes[name]) {
throw new Error(`Worker source with name "${name}" already registered.`);
}
this.workerSourceTypes[name] = WorkerSource;
};
this.self.registerRTLTextPlugin = (rtlTextPlugin) => {
if (index.plugin.isParsed()) {
throw new Error("RTL text plugin already registered.");
}
index.plugin.setState({
pluginStatus: index.rtlPluginStatus.parsed,
pluginURL: index.plugin.getPluginURL()
});
index.plugin["applyArabicShaping"] = rtlTextPlugin.applyArabicShaping;
index.plugin["processBidirectionalText"] = rtlTextPlugin.processBidirectionalText;
index.plugin["processStyledBidirectionalText"] = rtlTextPlugin.processStyledBidirectionalText;
for (const callback of this.rtlPluginParsingListeners) {
callback(null, true);
}
this.rtlPluginParsingListeners = [];
};
}
clearCaches(mapId, params, callback) {
delete this.layerIndexes[mapId];
delete this.availableImages[mapId];
delete this.availableModels[mapId];
delete this.workerSources[mapId];
callback();
}
checkIfReady(mapID, params, callback) {
callback();
}
setReferrer(mapID, referrer) {
this.referrer = referrer;
}
spriteLoaded(mapId, params) {
if (!this.isSpriteLoaded[mapId])
this.isSpriteLoaded[mapId] = {};
const { scope, isLoaded } = params;
this.isSpriteLoaded[mapId][scope] = isLoaded;
if (!this.workerSources[mapId] || !this.workerSources[mapId][scope]) {
return;
}
for (const workerSource in this.workerSources[mapId][scope]) {
const ws = this.workerSources[mapId][scope][workerSource];
for (const source in ws) {
const workerSource2 = ws[source];
if (workerSource2 instanceof VectorTileWorkerSource) {
workerSource2.isSpriteLoaded = isLoaded;
workerSource2.fire(new index.Event("isSpriteLoaded"));
}
}
}
}
setImages(mapId, params, callback) {
if (!this.availableImages[mapId]) {
this.availableImages[mapId] = {};
}
const { scope, images } = params;
this.availableImages[mapId][scope] = images;
if (!this.workerSources[mapId] || !this.workerSources[mapId][scope]) {
callback();
return;
}
for (const workerSource in this.workerSources[mapId][scope]) {
const ws = this.workerSources[mapId][scope][workerSource];
for (const source in ws) {
ws[source].availableImages = images;
}
}
callback();
}
setModels(mapId, { scope, models }, callback) {
if (!this.availableModels[mapId]) {
this.availableModels[mapId] = {};
}
this.availableModels[mapId][scope] = models;
if (!this.workerSources[mapId] || !this.workerSources[mapId][scope]) {
callback();
return;
}
for (const workerSource in this.workerSources[mapId][scope]) {
const ws = this.workerSources[mapId][scope][workerSource];
for (const source in ws) {
ws[source].availableModels = models;
}
}
callback();
}
setProjection(mapId, config) {
this.projections[mapId] = index.getProjection(config);
}
setBrightness(mapId, brightness, callback) {
this.brightness = brightness;
callback();
}
setWorldview(mapId, worldview, callback) {
this.worldview = worldview;
callback();
}
setLayers(mapId, params, callback) {
this.getLayerIndex(mapId, params.scope).replace(params.layers, params.options);
callback();
}
updateLayers(mapId, params, callback) {
this.getLayerIndex(mapId, params.scope).update(params.layers, params.removedIds, params.options);
callback();
}
loadTile(mapId, params, callback) {
index.assert(params.type);
params.projection = this.projections[mapId] || this.defaultProjection;
this.getWorkerSource(mapId, params.type, params.source, params.scope).loadTile(params, callback);
}
decodeRasterArray(mapId, params, callback) {
this.getWorkerSource(mapId, params.type, params.source, params.scope).decodeRasterArray(params, callback);
}
reloadTile(mapId, params, callback) {
index.assert(params.type);
params.projection = this.projections[mapId] || this.defaultProjection;
this.getWorkerSource(mapId, params.type, params.source, params.scope).reloadTile(params, callback);
}
abortTile(mapId, params, callback) {
index.assert(params.type);
this.getWorkerSource(mapId, params.type, params.source, params.scope).abortTile(params, callback);
}
removeTile(mapId, params, callback) {
index.assert(params.type);
this.getWorkerSource(mapId, params.type, params.source, params.scope).removeTile(params, callback);
}
removeSource(mapId, params, callback) {
index.assert(params.type);
index.assert(params.scope);
index.assert(params.source);
if (!this.workerSources[mapId] || !this.workerSources[mapId][params.scope] || !this.workerSources[mapId][params.scope][params.type] || !this.workerSources[mapId][params.scope][params.type][params.source]) {
return;
}
const worker = this.workerSources[mapId][params.scope][params.type][params.source];
delete this.workerSources[mapId][params.scope][params.type][params.source];
if (worker.removeSource !== void 0) {
worker.removeSource(params, callback);
} else {
callback();
}
}
/**
* Load a {@link WorkerSource} script at params.url. The script is run
* (using importScripts) with `registerWorkerSource` in scope, which is a
* function taking `(name, workerSourceObject)`.
* @private
*/
loadWorkerSource(mapId, params, callback) {
try {
this.self.importScripts(params.url);
callback();
} catch (e) {
callback(e);
}
}
syncRTLPluginState(mapId, state, callback) {
if (index.plugin.isParsed()) {
callback(null, true);
return;
}
if (index.plugin.isParsing()) {
this.rtlPluginParsingListeners.push(callback);
return;
}
try {
index.plugin.setState(state);
const pluginURL = index.plugin.getPluginURL();
if (index.plugin.isLoaded() && !index.plugin.isParsed() && !index.plugin.isParsing() && pluginURL != null) {
index.plugin.setState({
pluginStatus: index.rtlPluginStatus.parsing,
pluginURL: index.plugin.getPluginURL()
});
this.self.importScripts(pluginURL);
if (index.plugin.isParsed()) {
callback(null, true);
} else {
this.rtlPluginParsingListeners.push(callback);
}
}
} catch (e) {
callback(e);
}
}
setDracoUrl(mapId, dracoUrl) {
this.dracoUrl = dracoUrl;
}
getAvailableImages(mapId, scope) {
if (!this.availableImages[mapId]) {
this.availableImages[mapId] = {};
}
let availableImages = this.availableImages[mapId][scope];
if (!availableImages) {
availableImages = [];
}
return availableImages;
}
getAvailableModels(mapId, scope) {
if (!this.availableModels[mapId]) {
this.availableModels[mapId] = {};
}
let availableModels = this.availableModels[mapId][scope];
if (!availableModels) {
availableModels = {};
}
return availableModels;
}
getLayerIndex(mapId, scope) {
if (!this.layerIndexes[mapId]) {
this.layerIndexes[mapId] = {};
}
let layerIndex = this.layerIndexes[mapId][scope];
if (!layerIndex) {
layerIndex = this.layerIndexes[mapId][scope] = new StyleLayerIndex();
layerIndex.scope = scope;
}
return layerIndex;
}
getWorkerSource(mapId, type, source, scope) {
const workerSources = this.workerSources;
if (!workerSources[mapId])
workerSources[mapId] = {};
if (!workerSources[mapId][scope])
workerSources[mapId][scope] = {};
if (!workerSources[mapId][scope][type])
workerSources[mapId][scope][type] = {};
if (!this.isSpriteLoaded[mapId])
this.isSpriteLoaded[mapId] = {};
if (!workerSources[mapId][scope][type][source]) {
const actor = {
send: (type2, data, callback, _targetMapId, mustQueue, metadata) => {
return this.actor.send(type2, data, callback, mapId, mustQueue, metadata);
},
scheduler: this.actor.scheduler
};
workerSources[mapId][scope][type][source] = new this.workerSourceTypes[type](
actor,
this.getLayerIndex(mapId, scope),
this.getAvailableImages(mapId, scope),
this.getAvailableModels(mapId, scope),
this.isSpriteLoaded[mapId][scope],
void 0,
this.brightness,
this.worldview
);
}
return workerSources[mapId][scope][type][source];
}
rasterizeImagesWorker(mapId, params, callback) {
const rasterizedImages = /* @__PURE__ */ new Map();
for (const [id, { image, imageVariant }] of params.tasks.entries()) {
const rasterizedImage = this.imageRasterizer.rasterize(imageVariant, image, params.scope, mapId);
rasterizedImages.set(id, rasterizedImage);
}
callback(void 0, rasterizedImages);
}
removeRasterizedImages(mapId, params, callback) {
this.imageRasterizer.removeImagesFromCacheByIds(params.imageIds, params.scope, mapId);
callback();
}
enforceCacheSizeLimit(mapId, limit) {
index.enforceCacheSizeLimit(limit);
}
getWorkerPerformanceMetrics(mapId, params, callback) {
callback(void 0, index.PerformanceUtils.getWorkerPerformanceMetrics());
}
}
if (index.isWorker(self)) {
self.worker = new MapWorker(self);
}
return MapWorker;
}));
define(['./shared'], (function (index$1) { 'use strict';
var mapboxGlSupported = {};
var hasRequiredMapboxGlSupported;
function requireMapboxGlSupported () {
if (hasRequiredMapboxGlSupported) return mapboxGlSupported;
hasRequiredMapboxGlSupported = 1;
'use strict';
mapboxGlSupported.supported = isSupported;
mapboxGlSupported.notSupportedReason = notSupportedReason;
/**
* Test whether the current browser supports Mapbox GL JS
* @param {Object} options
* @param {boolean} [options.failIfMajorPerformanceCaveat=false] Return `false`
* if the performance of Mapbox GL JS would be dramatically worse than
* expected (i.e. a software renderer is would be used)
* @return {boolean}
*/
function isSupported(options) {
return !notSupportedReason(options);
}
function notSupportedReason(options) {
if (!isBrowser()) return 'not a browser';
if (!isWorkerSupported()) return 'insufficient worker support';
if (!isCanvasGetImageDataSupported()) return 'insufficient Canvas/getImageData support';
if (!isWebGLSupportedCached(options && options.failIfMajorPerformanceCaveat)) return 'insufficient WebGL2 support';
if (!isNotIE()) return 'insufficient ECMAScript 6 support';
}
function isBrowser() {
return typeof window !== 'undefined' && typeof document !== 'undefined';
}
function isWorkerSupported() {
if (!('Worker' in window && 'Blob' in window && 'URL' in window)) {
return false;
}
var blob = new Blob([''], { type: 'text/javascript' });
var workerURL = URL.createObjectURL(blob);
var supported;
var worker;
try {
worker = new Worker(workerURL);
supported = true;
} catch (e) {
supported = false;
}
if (worker) {
worker.terminate();
}
URL.revokeObjectURL(workerURL);
return supported;
}
// Some browsers or browser extensions block access to canvas data to prevent fingerprinting.
// Mapbox GL uses this API to load sprites and images in general.
function isCanvasGetImageDataSupported() {
var canvas = document.createElement('canvas');
canvas.width = canvas.height = 1;
var context = canvas.getContext('2d');
if (!context) {
return false;
}
var imageData = context.getImageData(0, 0, 1, 1);
return imageData && imageData.width === canvas.width;
}
var isWebGLSupportedCache = {};
function isWebGLSupportedCached(failIfMajorPerformanceCaveat) {
if (isWebGLSupportedCache[failIfMajorPerformanceCaveat] === undefined) {
isWebGLSupportedCache[failIfMajorPerformanceCaveat] = isWebGLSupported(failIfMajorPerformanceCaveat);
}
return isWebGLSupportedCache[failIfMajorPerformanceCaveat];
}
isSupported.webGLContextAttributes = {
antialias: false,
alpha: true,
stencil: true,
depth: true
};
function getWebGLContext(failIfMajorPerformanceCaveat) {
var canvas = document.createElement('canvas');
var attributes = Object.create(isSupported.webGLContextAttributes);
attributes.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat;
return canvas.getContext('webgl2', attributes);
}
function isWebGLSupported(failIfMajorPerformanceCaveat) {
var gl = getWebGLContext(failIfMajorPerformanceCaveat);
if (!gl) {
return false;
}
// Try compiling a shader and get its compile status. Some browsers like Brave block this API
// to prevent fingerprinting. Unfortunately, this also means that Mapbox GL won't work.
var shader;
try {
shader = gl.createShader(gl.VERTEX_SHADER);
} catch (e) {
// some older browsers throw an exception that `createShader` is not defined
// so handle this separately from the case where browsers block `createShader`
// for security reasons
return false;
}
if (!shader || gl.isContextLost()) {
return false;
}
gl.shaderSource(shader, 'void main() {}');
gl.compileShader(shader);
return gl.getShaderParameter(shader, gl.COMPILE_STATUS) === true;
}
function isNotIE() {
return !document.documentMode;
}
return mapboxGlSupported;
}
var mapboxGlSupportedExports = requireMapboxGlSupported();
var index = /*@__PURE__*/index$1.getDefaultExportFromCjs(mapboxGlSupportedExports);
function create$1(tagName, className, container) {
const el = document.createElement(tagName);
if (className !== void 0 && className !== null) el.className = className;
if (container) container.appendChild(el);
return el;
}
function createSVG(tagName, attributes, container) {
const el = document.createElementNS("http://www.w3.org/2000/svg", tagName);
for (const name of Object.keys(attributes)) {
el.setAttributeNS(null, name, String(attributes[name]));
}
if (container) container.appendChild(el);
return el;
}
const docStyle = typeof document !== "undefined" ? document.documentElement && document.documentElement.style : null;
const selectProp = docStyle && docStyle.userSelect !== void 0 ? "userSelect" : "WebkitUserSelect";
let userSelect;
function disableDrag() {
if (docStyle && selectProp) {
userSelect = docStyle[selectProp];
docStyle[selectProp] = "none";
}
}
function enableDrag() {
if (docStyle && selectProp) {
docStyle[selectProp] = userSelect;
}
}
function suppressClickListener(e) {
e.preventDefault();
e.stopPropagation();
window.removeEventListener("click", suppressClickListener, true);
}
function suppressClick() {
window.addEventListener("click", suppressClickListener, true);
window.setTimeout(() => {
window.removeEventListener("click", suppressClickListener, true);
}, 0);
}
function mousePos(el, e) {
const rect = el.getBoundingClientRect();
return getScaledPoint(el, rect, e);
}
function touchPos(el, touches) {
const rect = el.getBoundingClientRect();
const points = [];
for (let i = 0; i < touches.length; i++) {
points.push(getScaledPoint(el, rect, touches[i]));
}
return points;
}
function mouseButton(e) {
index$1.assert(e.type === "mousedown" || e.type === "mouseup");
if (/firefox/i.test(navigator.userAgent) && /macintosh/i.test(navigator.userAgent) && e.button === 2 && e.ctrlKey) {
return 0;
}
return e.button;
}
function getScaledPoint(el, rect, e) {
const scaling = el.offsetWidth === rect.width ? 1 : el.offsetWidth / rect.width;
return new index$1.Point(
(e.clientX - rect.left) * scaling,
(e.clientY - rect.top) * scaling
);
}
const SKU_ID = "01";
function createSkuToken() {
const TOKEN_VERSION = "1";
const base62chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let sessionRandomizer = "";
for (let i = 0; i < 10; i++) {
sessionRandomizer += base62chars[Math.floor(Math.random() * 62)];
}
const expiration = 12 * 60 * 60 * 1e3;
const token = [TOKEN_VERSION, SKU_ID, sessionRandomizer].join("");
const tokenExpiresAt = Date.now() + expiration;
return { token, tokenExpiresAt };
}
var define_import_meta_env_default = { mode: "dev" };
const AUTH_ERR_MSG = "NO_ACCESS_TOKEN";
class RequestManager {
constructor(transformRequestFn, customAccessToken, silenceAuthErrors) {
this._transformRequestFn = transformRequestFn;
this._customAccessToken = customAccessToken;
this._silenceAuthErrors = !!silenceAuthErrors;
this._createSkuToken();
}
_createSkuToken() {
const skuToken = createSkuToken();
this._skuToken = skuToken.token;
this._skuTokenExpiresAt = skuToken.tokenExpiresAt;
}
_isSkuTokenExpired() {
return Date.now() > this._skuTokenExpiresAt;
}
transformRequest(url, type) {
if (this._transformRequestFn) {
return this._transformRequestFn(url, type) || { url };
}
return { url };
}
normalizeStyleURL(url, accessToken) {
if (!index$1.isMapboxURL(url)) return url;
const urlObject = parseUrl(url);
if (define_import_meta_env_default.mode !== "dev") {
urlObject.params.push(`sdk=js-${index$1.version}`);
}
urlObject.path = `/styles/v1${urlObject.path}`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeGlyphsURL(url, accessToken) {
if (!index$1.isMapboxURL(url)) return url;
const urlObject = parseUrl(url);
urlObject.path = `/fonts/v1${urlObject.path}`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeModelURL(url, accessToken) {
if (!index$1.isMapboxURL(url)) return url;
const urlObject = parseUrl(url);
urlObject.path = `/models/v1${urlObject.path}`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeSourceURL(url, accessToken, language, worldview) {
if (!index$1.isMapboxURL(url)) return url;
const urlObject = parseUrl(url);
urlObject.path = `/v4/${urlObject.authority}.json`;
urlObject.params.push("secure");
if (language) {
urlObject.params.push(`language=${language}`);
}
if (worldview) {
urlObject.params.push(`worldview=${worldview}`);
}
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeIconsetURL(url, accessToken) {
const urlObject = parseUrl(url);
if (!index$1.isMapboxURL(url)) {
return formatUrl(urlObject);
}
urlObject.path = `/styles/v1${urlObject.path}/iconset.pbf`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeSpriteURL(url, format, extension, accessToken) {
const urlObject = parseUrl(url);
if (!index$1.isMapboxURL(url)) {
urlObject.path += `${format}${extension}`;
return formatUrl(urlObject);
}
urlObject.path = `/styles/v1${urlObject.path}/sprite${format}${extension}`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
}
normalizeTileURL(tileURL, use2x, rasterTileSize) {
if (this._isSkuTokenExpired()) {
this._createSkuToken();
}
if (tileURL && !index$1.isMapboxURL(tileURL)) return tileURL;
const urlObject = parseUrl(tileURL);
const imageExtensionRe = /(\.(png|jpg)\d*)(?=$)/;
const extension = index$1.exported.supported ? ".webp" : "$1";
const use2xAs512 = rasterTileSize && urlObject.authority !== "raster" && rasterTileSize === 512;
const suffix = use2x || use2xAs512 ? "@2x" : "";
urlObject.path = urlObject.path.replace(imageExtensionRe, `${suffix}${extension}`);
if (urlObject.authority === "raster") {
urlObject.path = `/${index$1.config.RASTER_URL_PREFIX}${urlObject.path}`;
} else if (urlObject.authority === "rasterarrays") {
urlObject.path = `/${index$1.config.RASTERARRAYS_URL_PREFIX}${urlObject.path}`;
} else if (urlObject.authority === "3dtiles") {
urlObject.path = `/${index$1.config.TILES3D_URL_PREFIX}${urlObject.path}`;
} else {
const tileURLAPIPrefixRe = /^.+\/v4\//;
urlObject.path = urlObject.path.replace(tileURLAPIPrefixRe, "/");
urlObject.path = `/${index$1.config.TILE_URL_VERSION}${urlObject.path}`;
}
const accessToken = this._customAccessToken || getAccessToken(urlObject.params) || index$1.config.ACCESS_TOKEN;
if (index$1.config.REQUIRE_ACCESS_TOKEN && accessToken && this._skuToken) {
urlObject.params.push(`sku=${this._skuToken}`);
}
return this._makeAPIURL(urlObject, accessToken);
}
canonicalizeTileURL(url, removeAccessToken) {
const extensionRe = /\.[\w]+$/;
const urlObject = parseUrl(url);
if (!urlObject.path.match(/^(\/v4\/|\/(raster|rasterarrays)\/v1\/)/) || !urlObject.path.match(extensionRe)) {
return url;
}
let result = "mapbox://";
if (urlObject.path.match(/^\/raster\/v1\//)) {
const rasterPrefix = `/${index$1.config.RASTER_URL_PREFIX}/`;
result += `raster/${urlObject.path.replace(rasterPrefix, "")}`;
} else if (urlObject.path.match(/^\/rasterarrays\/v1\//)) {
const rasterPrefix = `/${index$1.config.RASTERARRAYS_URL_PREFIX}/`;
result += `rasterarrays/${urlObject.path.replace(rasterPrefix, "")}`;
} else {
const tilesPrefix = `/${index$1.config.TILE_URL_VERSION}/`;
result += `tiles/${urlObject.path.replace(tilesPrefix, "")}`;
}
let params = urlObject.params;
if (removeAccessToken) {
params = params.filter((p) => !p.match(/^access_token=/));
}
if (params.length) result += `?${params.join("&")}`;
return result;
}
canonicalizeTileset(tileJSON, sourceURL) {
const removeAccessToken = sourceURL ? index$1.isMapboxURL(sourceURL) : false;
const canonical = [];
for (const url of tileJSON.tiles || []) {
if (index$1.isMapboxHTTPURL(url)) {
canonical.push(this.canonicalizeTileURL(url, removeAccessToken));
} else {
canonical.push(url);
}
}
return canonical;
}
_makeAPIURL(urlObject, accessToken) {
const help = "See https://docs.mapbox.com/api/overview/#access-tokens-and-token-scopes";
const apiUrlObject = parseUrl(index$1.config.API_URL);
urlObject.protocol = apiUrlObject.protocol;
urlObject.authority = apiUrlObject.authority;
if (urlObject.protocol === "http") {
const i = urlObject.params.indexOf("secure");
if (i >= 0) urlObject.params.splice(i, 1);
}
if (apiUrlObject.path !== "/") {
urlObject.path = `${apiUrlObject.path}${urlObject.path}`;
}
if (!index$1.config.REQUIRE_ACCESS_TOKEN) return formatUrl(urlObject);
accessToken = accessToken || index$1.config.ACCESS_TOKEN;
if (!this._silenceAuthErrors) {
if (!accessToken)
throw new Error(`An API access token is required to use Mapbox GL. ${help}`);
if (accessToken[0] === "s")
throw new Error(`Use a public access token (pk.*) with Mapbox GL, not a secret access token (sk.*). ${help}`);
}
urlObject.params = urlObject.params.filter((d) => d.indexOf("access_token") === -1);
urlObject.params.push(`access_token=${accessToken || ""}`);
return formatUrl(urlObject);
}
}
function getAccessToken(params) {
for (const param of params) {
const match = param.match(/^access_token=(.*)$/);
if (match) {
return match[1];
}
}
return null;
}
const urlRe = /^(\w+):\/\/([^/?]*)(\/[^?]+)?\??(.+)?/;
function parseUrl(url) {
const parts = url.match(urlRe);
if (!parts) {
throw new Error("Unable to parse URL object");
}
return {
protocol: parts[1],
authority: parts[2],
path: parts[3] || "/",
params: parts[4] ? parts[4].split("&") : []
};
}
function formatUrl(obj) {
const params = obj.params.length ? `?${obj.params.join("&")}` : "";
return `${obj.protocol}://${obj.authority}${obj.path}${params}`;
}
const telemEventKey = "mapbox.eventData";
function parseAccessToken(accessToken) {
if (!accessToken) {
return null;
}
const parts = accessToken.split(".");
if (!parts || parts.length !== 3) {
return null;
}
try {
const jsonData = JSON.parse(index$1.b64DecodeUnicode(parts[1]));
return jsonData;
} catch (e) {
return null;
}
}
class TelemetryEvent {
constructor(type) {
this.type = type;
this.anonId = null;
this.anonIdTimestamp = null;
this.eventData = {};
this.queue = [];
this.pendingRequest = null;
}
getStorageKey(domain) {
const tokenData = parseAccessToken(index$1.config.ACCESS_TOKEN);
let u = "";
if (tokenData && tokenData["u"]) {
u = index$1.b64EncodeUnicode(tokenData["u"]);
} else {
u = index$1.config.ACCESS_TOKEN || "";
}
return domain ? `${telemEventKey}.${domain}:${u}` : `${telemEventKey}:${u}`;
}
fetchEventData() {
const isLocalStorageAvailable = index$1.storageAvailable("localStorage");
const storageKey = this.getStorageKey();
const uuidKey = this.getStorageKey("uuid");
const uuidTimestampKey = this.getStorageKey("uuidTimestamp");
if (isLocalStorageAvailable) {
try {
const data = localStorage.getItem(storageKey);
if (data) {
this.eventData = JSON.parse(data);
}
const currentUuid = localStorage.getItem(uuidKey);
if (currentUuid) {
this.anonId = currentUuid;
}
const uuidTimestamp = localStorage.getItem(uuidTimestampKey);
if (uuidTimestamp) {
this.anonIdTimestamp = Number(uuidTimestamp);
}
const twentyFourHoursAgo = Date.now() - 24 * 60 * 60 * 1e3;
if (!this.anonIdTimestamp || this.anonIdTimestamp < twentyFourHoursAgo) {
this.refreshUUID();
}
} catch (e) {
index$1.warnOnce("Unable to read from LocalStorage");
}
}
}
refreshUUID() {
this.anonId = index$1.uuid();
this.anonIdTimestamp = Date.now();
}
saveEventData() {
const isLocalStorageAvailable = index$1.storageAvailable("localStorage");
const storageKey = this.getStorageKey();
const uuidKey = this.getStorageKey("uuid");
const uuidTimestampKey = this.getStorageKey("uuidTimestamp");
const anonId = this.anonId;
const anonIdTimestamp = this.anonIdTimestamp;
if (isLocalStorageAvailable && anonId) {
try {
localStorage.setItem(uuidKey, anonId);
if (Object.keys(this.eventData).length >= 1) {
localStorage.setItem(storageKey, JSON.stringify(this.eventData));
}
if (anonIdTimestamp) {
localStorage.setItem(uuidTimestampKey, anonIdTimestamp.toString());
}
} catch (e) {
index$1.warnOnce("Unable to write to LocalStorage");
}
}
}
processRequests(_) {
}
/*
* If any event data should be persisted after the POST request, the callback should modify eventData`
* to the values that should be saved. For this reason, the callback should be invoked prior to the call
* to TelemetryEvent#saveData
*/
postEvent(timestamp, additionalPayload, callback, customAccessToken) {
if (!index$1.config.EVENTS_URL) return;
const eventsUrlObject = parseUrl(index$1.config.EVENTS_URL);
eventsUrlObject.params.push(`access_token=${customAccessToken || index$1.config.ACCESS_TOKEN || ""}`);
const payload = {
event: this.type,
created: new Date(timestamp).toISOString()
};
const finalPayload = additionalPayload ? Object.assign(payload, additionalPayload) : payload;
const request = {
url: formatUrl(eventsUrlObject),
headers: {
"Content-Type": "text/plain"
//Skip the pre-flight OPTIONS request
},
body: JSON.stringify([finalPayload])
};
this.pendingRequest = index$1.postData(request, (error) => {
this.pendingRequest = null;
callback(error);
this.saveEventData();
this.processRequests(customAccessToken);
});
}
queueRequest(event, customAccessToken) {
this.queue.push(event);
this.processRequests(customAccessToken);
}
}
class PerformanceEvent extends TelemetryEvent {
constructor() {
super("gljs.performance");
}
postPerformanceEvent(customAccessToken, performanceData) {
if (index$1.config.EVENTS_URL) {
if (customAccessToken || index$1.config.ACCESS_TOKEN) {
this.queueRequest({ timestamp: Date.now(), performanceData }, customAccessToken);
}
}
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) {
return;
}
const { timestamp, performanceData } = this.queue.shift();
const additionalPayload = index$1.getLivePerformanceMetrics(performanceData);
for (const metadata of additionalPayload.metadata) {
index$1.assert(typeof metadata.value === "string");
}
for (const counter of additionalPayload.counters) {
index$1.assert(typeof counter.value === "string");
}
for (const attribute of additionalPayload.attributes) {
index$1.assert(typeof attribute.value === "string");
}
this.postEvent(timestamp, additionalPayload, () => {
}, customAccessToken);
}
}
class MapLoadEvent extends TelemetryEvent {
constructor() {
super("map.load");
this.success = {};
this.skuToken = "";
}
postMapLoadEvent(mapId, skuToken, customAccessToken, callback) {
this.skuToken = skuToken;
this.errorCb = callback;
if (index$1.config.EVENTS_URL) {
if (customAccessToken || index$1.config.ACCESS_TOKEN) {
this.queueRequest({ id: mapId, timestamp: Date.now() }, customAccessToken);
} else {
this.errorCb(new Error(AUTH_ERR_MSG));
}
}
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) return;
const { id, timestamp } = this.queue.shift();
if (id && this.success[id]) return;
if (!this.anonId || !this.anonIdTimestamp) {
this.fetchEventData();
}
if (!index$1.validateUuid(this.anonId)) {
this.refreshUUID();
}
const additionalPayload = {
sdkIdentifier: "mapbox-gl-js",
sdkVersion: index$1.version,
skuId: SKU_ID,
skuToken: this.skuToken,
userId: this.anonId
};
this.postEvent(timestamp, additionalPayload, (err) => {
if (err) {
this.errorCb(err);
} else {
if (id) this.success[id] = true;
}
}, customAccessToken);
}
remove() {
this.errorCb = null;
}
}
class StyleLoadEvent extends TelemetryEvent {
constructor() {
super("style.load");
this.eventIdPerMapInstanceMap = /* @__PURE__ */ new Map();
this.mapInstanceIdMap = /* @__PURE__ */ new WeakMap();
}
getMapInstanceId(map) {
let instanceId = this.mapInstanceIdMap.get(map);
if (!instanceId) {
instanceId = index$1.uuid();
this.mapInstanceIdMap.set(map, instanceId);
}
return instanceId;
}
getEventId(mapInstanceId) {
const eventId = this.eventIdPerMapInstanceMap.get(mapInstanceId) || 0;
this.eventIdPerMapInstanceMap.set(mapInstanceId, eventId + 1);
return eventId;
}
postStyleLoadEvent(customAccessToken, input) {
const {
map,
style,
importedStyles
} = input;
if (!index$1.config.EVENTS_URL || !(customAccessToken || index$1.config.ACCESS_TOKEN)) {
return;
}
const mapInstanceId = this.getMapInstanceId(map);
const payload = {
mapInstanceId,
eventId: this.getEventId(mapInstanceId),
style
};
if (importedStyles.length) {
payload.importedStyles = importedStyles;
}
this.queueRequest({
timestamp: Date.now(),
payload
}, customAccessToken);
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) {
return;
}
const { timestamp, payload } = this.queue.shift();
this.postEvent(timestamp, payload, () => {
}, customAccessToken);
}
}
class MetricsEvent extends TelemetryEvent {
constructor(data) {
super("metrics");
if (data) this.data = data;
}
postMetricsEvent(customAccessToken) {
if (!index$1.config.EVENTS_URL || !(customAccessToken || index$1.config.ACCESS_TOKEN)) {
return;
}
if (!this.anonId) {
this.fetchEventData();
}
if (!index$1.validateUuid(this.anonId)) {
this.refreshUUID();
}
const payload = Object.assign({}, this.data, { sessionId: this.anonId });
this.queueRequest({
timestamp: Date.now(),
payload
}, customAccessToken);
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) {
return;
}
const { timestamp, payload } = this.queue.shift();
this.postEvent(timestamp, payload, () => {
}, customAccessToken);
}
}
class MapSessionAPI extends TelemetryEvent {
constructor() {
super("map.auth");
this.success = {};
this.skuToken = "";
}
getSession(timestamp, token, callback, customAccessToken) {
if (!index$1.config.API_URL || !index$1.config.SESSION_PATH) return;
const authUrlObject = parseUrl(index$1.config.API_URL + index$1.config.SESSION_PATH);
authUrlObject.params.push(`sku=${token || ""}`);
authUrlObject.params.push(`access_token=${customAccessToken || index$1.config.ACCESS_TOKEN || ""}`);
const request = {
url: formatUrl(authUrlObject),
headers: {
"Content-Type": "text/plain"
//Skip the pre-flight OPTIONS request
}
};
this.pendingRequest = index$1.getData(request, (error) => {
this.pendingRequest = null;
callback(error);
this.saveEventData();
this.processRequests(customAccessToken);
});
}
getSessionAPI(mapId, skuToken, customAccessToken, callback) {
this.skuToken = skuToken;
this.errorCb = callback;
if (index$1.config.SESSION_PATH && index$1.config.API_URL) {
if (customAccessToken || index$1.config.ACCESS_TOKEN) {
this.queueRequest({ id: mapId, timestamp: Date.now() }, customAccessToken);
} else {
this.errorCb(new Error(AUTH_ERR_MSG));
}
}
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) return;
const { id, timestamp } = this.queue.shift();
if (id && this.success[id]) return;
this.getSession(timestamp, this.skuToken, (err) => {
if (err) {
this.errorCb(err);
} else {
if (id) this.success[id] = true;
}
}, customAccessToken);
}
remove() {
this.errorCb = null;
}
}
class TurnstileEvent extends TelemetryEvent {
constructor(customAccessToken) {
super("appUserTurnstile");
this._customAccessToken = customAccessToken;
}
postTurnstileEvent(tileUrls, customAccessToken) {
if (index$1.config.EVENTS_URL && index$1.config.ACCESS_TOKEN && Array.isArray(tileUrls) && tileUrls.some((url) => index$1.isMapboxURL(url) || index$1.isMapboxHTTPURL(url))) {
this.queueRequest(Date.now(), customAccessToken);
}
}
processRequests(customAccessToken) {
if (this.pendingRequest || this.queue.length === 0) {
return;
}
if (!this.anonId || !this.anonIdTimestamp || !this.eventData.lastSuccess || !this.eventData.tokenU) {
this.fetchEventData();
}
const tokenData = parseAccessToken(index$1.config.ACCESS_TOKEN);
const tokenU = tokenData ? tokenData["u"] : index$1.config.ACCESS_TOKEN;
let dueForEvent = tokenU !== this.eventData.tokenU;
if (!index$1.validateUuid(this.anonId)) {
this.refreshUUID();
dueForEvent = true;
}
const nextUpdate = this.queue.shift();
if (this.eventData.lastSuccess) {
const lastUpdate = new Date(this.eventData.lastSuccess);
const nextDate = new Date(nextUpdate);
const daysElapsed = (nextUpdate - this.eventData.lastSuccess) / (24 * 60 * 60 * 1e3);
dueForEvent = dueForEvent || daysElapsed >= 1 || daysElapsed < -1 || lastUpdate.getDate() !== nextDate.getDate();
} else {
dueForEvent = true;
}
if (!dueForEvent) {
this.processRequests();
return;
}
const additionalPayload = {
sdkIdentifier: "mapbox-gl-js",
sdkVersion: index$1.version,
skuId: SKU_ID,
"enabled.telemetry": false,
userId: this.anonId
};
this.postEvent(nextUpdate, additionalPayload, (err) => {
if (!err) {
this.eventData.lastSuccess = nextUpdate;
this.eventData.tokenU = tokenU;
}
}, customAccessToken);
}
}
const turnstileEvent_ = new TurnstileEvent();
const postTurnstileEvent = turnstileEvent_.postTurnstileEvent.bind(turnstileEvent_);
const mapLoadEvent = new MapLoadEvent();
const postMapLoadEvent = mapLoadEvent.postMapLoadEvent.bind(mapLoadEvent);
const styleLoadEvent = new StyleLoadEvent();
const postStyleLoadEvent = styleLoadEvent.postStyleLoadEvent.bind(styleLoadEvent);
const styleWithAppearanceEvent = new MetricsEvent({ attributes: [{ name: "maps/js/layer-animations/style-with-appearances" }] });
const postStyleWithAppearanceEvent = styleWithAppearanceEvent.postMetricsEvent.bind(styleWithAppearanceEvent);
const addedAppearanceEvent = new MetricsEvent({ attributes: [{ name: "maps/js/layer-animations/runtime-appearances" }] });
const postAddedAppearanceEvent = addedAppearanceEvent.postMetricsEvent.bind(addedAppearanceEvent);
const performanceEvent_ = new PerformanceEvent();
const postPerformanceEvent = performanceEvent_.postPerformanceEvent.bind(performanceEvent_);
const mapSessionAPI = new MapSessionAPI();
const getMapSessionAPI = mapSessionAPI.getSessionAPI.bind(mapSessionAPI);
const authenticatedMaps = /* @__PURE__ */ new Set();
function storeAuthState(gl, state) {
if (state) {
authenticatedMaps.add(gl);
} else {
authenticatedMaps.delete(gl);
}
}
function isMapAuthenticated(gl) {
return authenticatedMaps.has(gl);
}
function removeAuthState(gl) {
authenticatedMaps.delete(gl);
}
class StyleChanges {
constructor() {
this._changed = false;
this._updatedLayers = {};
this._removedLayers = {};
this._updatedSourceCaches = {};
this._updatedPaintProps = /* @__PURE__ */ new Set();
this._updatedImages = {};
}
isDirty() {
return this._changed;
}
/**
* Mark changes as dirty.
*/
setDirty() {
this._changed = true;
}
getUpdatedSourceCaches() {
return this._updatedSourceCaches;
}
/**
* Mark that a source cache needs to be cleared or reloaded.
* @param {string} id
* @param {'clear' | 'reload'} action
*/
updateSourceCache(id, action) {
this._updatedSourceCaches[id] = action;
this.setDirty();
}
/**
* Discards updates to the source cache with the given id.
* @param {string} id
*/
discardSourceCacheUpdate(id) {
delete this._updatedSourceCaches[id];
}
/**
* Mark a layer as having changes and needs to be rerendered.
* @param {TypedStyleLayer} layer
*/
updateLayer(layer) {
const scope = layer.scope;
this._updatedLayers[scope] = this._updatedLayers[scope] || /* @__PURE__ */ new Set();
this._updatedLayers[scope].add(layer.id);
this.setDirty();
}
/**
* Mark a layer as having been removed and needing to be cleaned up.
* @param {TypedStyleLayer} layer
*/
removeLayer(layer) {
const scope = layer.scope;
this._removedLayers[scope] = this._removedLayers[scope] || {};
this._updatedLayers[scope] = this._updatedLayers[scope] || /* @__PURE__ */ new Set();
this._removedLayers[scope][layer.id] = layer;
this._updatedLayers[scope].delete(layer.id);
this._updatedPaintProps.delete(layer.fqid);
this.setDirty();
}
/**
* Returns StyleLayer if layer needs to be removed.
* @param {TypedStyleLayer} layer
*/
getRemovedLayer(layer) {
if (!this._removedLayers[layer.scope]) return null;
return this._removedLayers[layer.scope][layer.id];
}
/**
* Eliminate layer from the list of layers that need to be removed.
* @param {TypedStyleLayer} layer
*/
discardLayerRemoval(layer) {
if (!this._removedLayers[layer.scope]) return;
delete this._removedLayers[layer.scope][layer.id];
}
/**
* Returns a list of layer ids that have been updated or removed grouped by the scope.
* @returns {{[scope: string]: {updatedIds: Array, removedIds: Array}}}}
*/
getLayerUpdatesByScope() {
const updatesByScope = {};
for (const scope in this._updatedLayers) {
updatesByScope[scope] = updatesByScope[scope] || {};
updatesByScope[scope].updatedIds = Array.from(this._updatedLayers[scope].values());
}
for (const scope in this._removedLayers) {
updatesByScope[scope] = updatesByScope[scope] || {};
updatesByScope[scope].removedIds = Object.keys(this._removedLayers[scope]);
}
return updatesByScope;
}
getUpdatedPaintProperties() {
return this._updatedPaintProps;
}
/**
* Mark a layer as having a changed paint properties.
* @param {TypedStyleLayer} layer
*/
updatePaintProperties(layer) {
this._updatedPaintProps.add(layer.fqid);
this.setDirty();
}
getUpdatedImages(scope) {
return this._updatedImages[scope] ? Array.from(this._updatedImages[scope].values()) : [];
}
/**
* Mark an image as having changes.
* @param {ImageId} id
*/
updateImage(id, scope) {
this._updatedImages[scope] = this._updatedImages[scope] || /* @__PURE__ */ new Set();
this._updatedImages[scope].add(index$1.ImageId.toString(id));
this.setDirty();
}
resetUpdatedImages(scope) {
if (this._updatedImages[scope]) {
this._updatedImages[scope].clear();
}
}
/**
* Reset all style changes.
*/
reset() {
this._changed = false;
this._updatedLayers = {};
this._removedLayers = {};
this._updatedSourceCaches = {};
this._updatedPaintProps.clear();
this._updatedImages = {};
}
}
function loadSprite(baseURL, requestManager, callback) {
let json, image, error;
const format = index$1.exported$1.devicePixelRatio > 1 ? "@2x" : "";
let jsonRequest = index$1.getJSON(requestManager.transformRequest(requestManager.normalizeSpriteURL(baseURL, format, ".json"), index$1.ResourceType.SpriteJSON), (err, data) => {
jsonRequest = null;
if (!error) {
error = err;
json = data;
maybeComplete();
}
});
let imageRequest = index$1.getImage(requestManager.transformRequest(requestManager.normalizeSpriteURL(baseURL, format, ".png"), index$1.ResourceType.SpriteImage), (err, img) => {
imageRequest = null;
if (!error) {
error = err;
image = img;
maybeComplete();
}
});
function maybeComplete() {
if (error) {
callback(error);
} else if (json && image) {
const imageData = index$1.exported$1.getImageData(image);
const result = {};
for (const id in json) {
const { width, height, x, y, sdf, pixelRatio, stretchX, stretchY, content } = json[id];
const data = new index$1.RGBAImage({ width, height });
index$1.RGBAImage.copy(imageData, data, { x, y }, { x: 0, y: 0 }, { width, height }, null);
result[id] = {
data,
pixelRatio: pixelRatio !== void 0 ? pixelRatio : 1,
sdf: sdf !== void 0 ? sdf : false,
stretchX,
stretchY,
content,
usvg: false,
version: 0
};
}
callback(null, result);
}
}
return {
cancel() {
if (jsonRequest) {
jsonRequest.cancel();
jsonRequest = null;
}
if (imageRequest) {
imageRequest.cancel();
imageRequest = null;
}
}
};
}
function renderStyleImage(image) {
const { userImage } = image;
if (userImage && userImage.render) {
const updated = userImage.render();
if (updated) {
image.data.replace(new Uint8Array(userImage.data.buffer));
return true;
}
}
return false;
}
const IMAGE_RASTERIZER_WORKER_POOL_COUNT = 1;
class ImageManager extends index$1.Evented {
constructor(spriteFormat) {
super();
this.imageProviders = /* @__PURE__ */ new Map();
this.images = /* @__PURE__ */ new Map();
this.updatedImages = /* @__PURE__ */ new Map();
this.callbackDispatchedThisFrame = /* @__PURE__ */ new Map();
this.loaded = /* @__PURE__ */ new Map();
this.requestors = [];
this.patterns = /* @__PURE__ */ new Map();
this.patternsInFlight = /* @__PURE__ */ new Set();
this.atlasImage = /* @__PURE__ */ new Map();
this.atlasTexture = /* @__PURE__ */ new Map();
this.dirty = true;
this.spriteFormat = spriteFormat;
if (spriteFormat !== "raster" && index$1.offscreenCanvasSupported()) {
this.imageRasterizerDispatcher = new index$1.Dispatcher(
index$1.getImageRasterizerWorkerPool(),
this,
"Image Rasterizer Worker",
IMAGE_RASTERIZER_WORKER_POOL_COUNT
);
}
}
addScope(scope) {
this.loaded.set(scope, false);
this.imageProviders.set(scope, /* @__PURE__ */ new Map());
this.images.set(scope, /* @__PURE__ */ new Map());
this.updatedImages.set(scope, /* @__PURE__ */ new Set());
this.callbackDispatchedThisFrame.set(scope, /* @__PURE__ */ new Set());
this.patterns.set(scope, /* @__PURE__ */ new Map());
this.atlasImage.set(scope, new index$1.RGBAImage({ width: 1, height: 1 }));
}
removeScope(scope) {
this.loaded.delete(scope);
this.imageProviders.delete(scope);
this.images.delete(scope);
this.updatedImages.delete(scope);
this.callbackDispatchedThisFrame.delete(scope);
this.patterns.delete(scope);
this.atlasImage.delete(scope);
const atlasTexture = this.atlasTexture.get(scope);
if (atlasTexture) {
atlasTexture.destroy();
this.atlasTexture.delete(scope);
}
}
addImageProvider(imageProvider, scope) {
if (!this.imageProviders.has(scope)) {
this.imageProviders.set(scope, /* @__PURE__ */ new Map());
}
index$1.assert(!this.imageProviders.get(scope).has(imageProvider.id), "ImageProvider already exists");
this.imageProviders.get(scope).set(imageProvider.id, imageProvider);
}
removeImageProvider(imageProviderId, scope) {
if (this.imageProviders.has(scope)) {
this.imageProviders.get(scope).delete(imageProviderId);
}
}
getPendingImageProviders() {
const pendingImageProviders = [];
for (const imageProviders of this.imageProviders.values()) {
for (const imageProvider of imageProviders.values()) {
if (imageProvider.hasPendingRequests()) {
pendingImageProviders.push(imageProvider);
}
}
}
return pendingImageProviders;
}
get imageRasterizer() {
if (!this._imageRasterizer) {
this._imageRasterizer = new index$1.ImageRasterizer();
}
return this._imageRasterizer;
}
isLoaded() {
for (const scope of this.loaded.keys()) {
if (!this.loaded.get(scope)) return false;
}
return true;
}
setLoaded(loaded, scope) {
if (this.loaded.get(scope) === loaded) {
return;
}
this.loaded.set(scope, loaded);
if (loaded) {
for (const { ids, callback } of this.requestors) {
this._notify(ids, scope, callback);
}
this.requestors = [];
}
}
hasImage(id, scope) {
return !!this.getImage(id, scope);
}
getImage(id, scope) {
return this.images.get(scope).get(id.toString());
}
addImage(id, scope, image) {
index$1.assert(!this.images.get(scope).has(id.toString()), `Image "${id.toString()}" already exists in scope "${scope}"`);
if (this._validate(id, image)) {
this.images.get(scope).set(id.toString(), image);
}
}
_validate(id, image) {
let valid = true;
if (!this._validateStretch(image.stretchX, image.data && image.data.width)) {
this.fire(new index$1.ErrorEvent(new Error(`Image "${id.name}" has invalid "stretchX" value`)));
valid = false;
}
if (!this._validateStretch(image.stretchY, image.data && image.data.height)) {
this.fire(new index$1.ErrorEvent(new Error(`Image "${id.name}" has invalid "stretchY" value`)));
valid = false;
}
if (!this._validateContent(image.content, image)) {
this.fire(new index$1.ErrorEvent(new Error(`Image "${id.name}" has invalid "content" value`)));
valid = false;
}
return valid;
}
_validateStretch(stretch, size) {
if (!stretch) return true;
let last = 0;
for (const part of stretch) {
if (part[0] < last || part[1] < part[0] || size < part[1]) return false;
last = part[1];
}
return true;
}
_validateContent(content, image) {
if (!content) return true;
if (content.length !== 4) return false;
if (!image.usvg) {
if (content[0] < 0 || image.data.width < content[0]) return false;
if (content[1] < 0 || image.data.height < content[1]) return false;
if (content[2] < 0 || image.data.width < content[2]) return false;
if (content[3] < 0 || image.data.height < content[3]) return false;
}
if (content[2] < content[0]) return false;
if (content[3] < content[1]) return false;
return true;
}
updateImage(id, scope, image) {
const oldImage = this.images.get(scope).get(id.toString());
index$1.assert(oldImage, `Image "${id.toString()}" does not exist in scope "${scope}"`);
index$1.assert(oldImage.data.width === image.data.width && oldImage.data.height === image.data.height, `Image "${id.toString()}" dimensions mismatch`);
image.version = oldImage.version + 1;
this.images.get(scope).set(id.toString(), image);
this.updatedImages.get(scope).add(id);
this.removeFromImageRasterizerCache(id, scope);
}
clearUpdatedImages(scope) {
this.updatedImages.get(scope).clear();
}
removeFromImageRasterizerCache(id, scope) {
if (this.spriteFormat === "raster") {
return;
}
if (index$1.offscreenCanvasSupported()) {
this.imageRasterizerDispatcher.getActor().send("removeRasterizedImages", { imageIds: [id], scope });
} else {
this.imageRasterizer.removeImagesFromCacheByIds([id], scope);
}
}
removeImage(id, scope) {
const images = this.images.get(scope);
index$1.assert(images.has(id.toString()), `Image "${id.toString()}" does not exist in scope "${scope}"`);
const image = images.get(id.toString());
images.delete(id.toString());
this.patterns.get(scope).delete(id.toString());
this.removeFromImageRasterizerCache(id, scope);
if (image.userImage && image.userImage.onRemove) {
image.userImage.onRemove();
}
}
listImages(scope) {
return Array.from(this.images.get(scope).keys()).map((id) => index$1.ImageId.from(id));
}
getImages(ids, scope, callback) {
const images = [];
const resolvedImages = [];
const imageProviders = this.imageProviders.get(scope);
for (const id of ids) {
if (!id.iconsetId) {
images.push(id);
continue;
}
const imageProvider = imageProviders.get(id.iconsetId);
if (!imageProvider) continue;
const image = this.getImage(id, scope);
if (image) resolvedImages.push(id);
else imageProvider.addPendingRequest(id);
}
if (images.length === 0) {
this._notify(resolvedImages, scope, callback);
return;
}
let hasAllDependencies = true;
const isLoaded = !!this.loaded.get(scope);
const imagesInScope = this.images.get(scope);
if (!isLoaded) {
for (const id of images) {
if (!imagesInScope.has(id.toString())) {
hasAllDependencies = false;
}
}
}
if (isLoaded || hasAllDependencies) {
this._notify(images, scope, callback);
} else {
this.requestors.push({ ids: images, scope, callback });
}
}
rasterizeImages(params, callback) {
const imageWorkerTasks = /* @__PURE__ */ new Map();
const { tasks, scope } = params;
for (const [id, imageVariant] of tasks.entries()) {
const image = this.getImage(imageVariant.id, scope);
if (image) {
imageWorkerTasks.set(id, { image, imageVariant });
}
}
this._rasterizeImages(scope, imageWorkerTasks, callback);
}
_rasterizeImages(scope, tasks, callback) {
if (index$1.offscreenCanvasSupported()) {
this.imageRasterizerDispatcher.getActor().send("rasterizeImagesWorker", { tasks, scope }, callback);
} else {
const rasterizedImages = /* @__PURE__ */ new Map();
for (const [id, { image, imageVariant }] of tasks.entries()) {
rasterizedImages.set(id, this.imageRasterizer.rasterize(imageVariant, image, scope, 0));
}
callback(void 0, rasterizedImages);
}
}
getUpdatedImages(scope) {
return this.updatedImages.get(scope) || /* @__PURE__ */ new Set();
}
_notify(ids, scope, callback) {
const imagesInScope = this.images.get(scope);
const response = /* @__PURE__ */ new Map();
for (const id of ids) {
if (!imagesInScope.get(id.toString())) {
if (id.iconsetId) continue;
this.fire(new index$1.Event("styleimagemissing", { id: id.name }));
}
const image = imagesInScope.get(id.toString());
if (!image) {
index$1.warnOnce(`Image "${id.name}" could not be loaded. Please make sure you have added the image with map.addImage() or a "sprite" property in your style. You can provide missing images by listening for the "styleimagemissing" map event.`);
continue;
}
const styleImage = {
// Vector images will be rasterized on the worker thread
data: image.usvg ? null : image.data.clone(),
pixelRatio: image.pixelRatio,
sdf: image.sdf,
usvg: image.usvg,
version: image.version,
stretchX: image.stretchX,
stretchY: image.stretchY,
content: image.content,
hasRenderCallback: Boolean(image.userImage && image.userImage.render)
};
if (image.usvg) {
Object.assign(styleImage, {
width: image.icon.usvg_tree.width,
height: image.icon.usvg_tree.height
});
}
response.set(index$1.ImageId.toString(id), styleImage);
}
callback(null, response);
}
// Pattern stuff
getPixelSize(scope) {
const { width, height } = this.atlasImage.get(scope);
return { width, height };
}
getPattern(id, scope, lut) {
const strImageId = id.toString();
const patternsInScope = this.patterns.get(scope);
const pattern = patternsInScope.get(strImageId);
const image = this.getImage(id, scope);
if (!image) {
return null;
}
if (pattern) {
if (pattern.position.version === image.version) {
return pattern.position;
} else {
pattern.position.version = image.version;
}
} else {
if (image.usvg && !image.data) {
const patternInFlightId = this.getPatternInFlightId(strImageId, scope);
if (this.patternsInFlight.has(patternInFlightId)) {
return null;
}
this.patternsInFlight.add(patternInFlightId);
const imageVariant = new index$1.ImageVariant(id).scaleSelf(index$1.exported$1.devicePixelRatio);
const tasks = /* @__PURE__ */ new Map([[imageVariant.toString(), { image, imageVariant }]]);
this._rasterizeImages(scope, tasks, (_, rasterizedImages) => this.storePatternImage(imageVariant, scope, image, lut, rasterizedImages));
return null;
} else {
this.storePattern(id, scope, image);
}
}
this._updatePatternAtlas(scope, lut);
return patternsInScope.get(strImageId).position;
}
getPatternInFlightId(id, scope) {
return index$1.makeFQID(id, scope);
}
hasPatternsInFlight() {
return this.patternsInFlight.size !== 0;
}
storePatternImage(imageVariant, scope, image, lut, rasterizedImages) {
const id = imageVariant.toString();
const imageData = rasterizedImages ? rasterizedImages.get(id) : void 0;
if (!imageData) return;
image.data = imageData;
this.storePattern(imageVariant.id, scope, image);
this._updatePatternAtlas(scope, lut);
this.patternsInFlight.delete(this.getPatternInFlightId(imageVariant.id.toString(), scope));
}
storePattern(id, scope, image) {
const w = image.data.width + index$1.PATTERN_PADDING * 2;
const h = image.data.height + index$1.PATTERN_PADDING * 2;
const bin = { w, h, x: 0, y: 0 };
const position = new index$1.ImagePosition(bin, image, index$1.PATTERN_PADDING);
this.patterns.get(scope).set(id.toString(), { bin, position });
}
destroyAtlasTextures() {
for (const atlasTexture of this.atlasTexture.values()) {
if (atlasTexture) {
atlasTexture.destroy();
}
}
this.atlasTexture.clear();
}
bind(context, scope) {
const gl = context.gl;
let atlasTexture = this.atlasTexture.get(scope);
if (!atlasTexture) {
atlasTexture = new index$1.Texture(context, this.atlasImage.get(scope), gl.RGBA8);
this.atlasTexture.set(scope, atlasTexture);
} else if (this.dirty) {
atlasTexture.update(this.atlasImage.get(scope));
this.dirty = false;
}
atlasTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
_updatePatternAtlas(scope, lut) {
const patternsInScope = this.patterns.get(scope);
const bins = Array.from(patternsInScope.values()).map(({ bin }) => bin);
const { w, h } = index$1.potpack(bins);
const dst = this.atlasImage.get(scope);
dst.resize({ width: w || 1, height: h || 1 });
const imagesInScope = this.images.get(scope);
for (const [id, { bin, position }] of patternsInScope.entries()) {
let padding = position.padding;
const x = bin.x + padding;
const y = bin.y + padding;
const src = imagesInScope.get(id).data;
const w2 = src.width;
const h2 = src.height;
index$1.assert(padding > 1);
padding = padding > 1 ? padding - 1 : padding;
index$1.RGBAImage.copy(src, dst, { x: 0, y: 0 }, { x, y }, { width: w2, height: h2 }, lut);
index$1.RGBAImage.copy(src, dst, { x: 0, y: h2 - padding }, { x, y: y - padding }, { width: w2, height: padding }, lut);
index$1.RGBAImage.copy(src, dst, { x: 0, y: 0 }, { x, y: y + h2 }, { width: w2, height: padding }, lut);
index$1.RGBAImage.copy(src, dst, { x: w2 - padding, y: 0 }, { x: x - padding, y }, { width: padding, height: h2 }, lut);
index$1.RGBAImage.copy(src, dst, { x: 0, y: 0 }, { x: x + w2, y }, { width: padding, height: h2 }, lut);
index$1.RGBAImage.copy(src, dst, { x: w2 - padding, y: h2 - padding }, { x: x - padding, y: y - padding }, { width: padding, height: padding }, lut);
index$1.RGBAImage.copy(src, dst, { x: 0, y: h2 - padding }, { x: x + w2, y: y - padding }, { width: padding, height: padding }, lut);
index$1.RGBAImage.copy(src, dst, { x: 0, y: 0 }, { x: x + w2, y: y + h2 }, { width: padding, height: padding }, lut);
index$1.RGBAImage.copy(src, dst, { x: w2 - padding, y: 0 }, { x: x - padding, y: y + h2 }, { width: padding, height: padding }, lut);
}
this.dirty = true;
}
beginFrame() {
for (const scope of this.images.keys()) {
this.callbackDispatchedThisFrame.set(scope, /* @__PURE__ */ new Set());
}
}
dispatchRenderCallbacks(ids, scope) {
const imagesInScope = this.images.get(scope);
for (const id of ids) {
if (this.callbackDispatchedThisFrame.get(scope).has(id.toString())) continue;
this.callbackDispatchedThisFrame.get(scope).add(id.toString());
const image = imagesInScope.get(id.toString());
index$1.assert(image, `Image "${id.toString()}" does not exist in scope "${scope}"`);
const updated = renderStyleImage(image);
if (updated) {
this.updateImage(id, scope, image);
}
}
}
destroy() {
if (this.imageRasterizerDispatcher) this.imageRasterizerDispatcher.remove();
}
}
function validateImport(options) {
const key = options.key;
const { value, styleSpec } = options;
if (!index$1.isObject(value)) {
return [new index$1.ValidationError(key, value, `import must be an object`)];
}
const { data, ...importSpec } = value;
Object.defineProperty(importSpec, "__line__", {
value: value.__line__,
enumerable: false
});
let errors = validateObject(Object.assign({}, options, {
value: importSpec,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.import
}));
if (index$1.unbundle(importSpec.id) === "") {
const key2 = `${options.key}.id`;
errors.push(new index$1.ValidationError(key2, importSpec, `import id can't be an empty string`));
}
if (data) {
const key2 = `${options.key}.data`;
errors = errors.concat(validateStyle$1(data, styleSpec, { key: key2 }));
}
return errors;
}
function validateArray(options) {
const array = options.value;
const arraySpec = options.valueSpec;
const style = options.style;
const styleSpec = options.styleSpec;
const key = options.key;
const validateArrayElement = options.arrayElementValidator || validate;
if (!Array.isArray(array)) {
return [new index$1.ValidationError(key, array, `array expected, ${index$1.getType(array)} found`)];
}
if (arraySpec.length && array.length !== arraySpec.length) {
return [new index$1.ValidationError(key, array, `array length ${arraySpec.length} expected, length ${array.length} found`)];
}
if (arraySpec["min-length"] && array.length < arraySpec["min-length"]) {
return [new index$1.ValidationError(key, array, `array length at least ${arraySpec["min-length"]} expected, length ${array.length} found`)];
}
let arrayElementSpec = {
type: arraySpec.value,
values: arraySpec.values,
minimum: arraySpec.minimum,
maximum: arraySpec.maximum,
function: void 0
};
if (styleSpec.$version < 7) {
arrayElementSpec.function = arraySpec.function;
}
if (index$1.isObject(arraySpec.value)) {
arrayElementSpec = arraySpec.value;
}
let errors = [];
for (let i = 0; i < array.length; i++) {
errors = errors.concat(validateArrayElement({
array,
arrayIndex: i,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
value: array[i],
valueSpec: arrayElementSpec,
style,
styleSpec,
key: `${key}[${i}]`
}, true));
}
return errors;
}
function validateNumber(options) {
const key = options.key;
const value = options.value;
const valueSpec = options.valueSpec;
if (!index$1.isNumber(value)) {
return [new index$1.ValidationError(key, value, `number expected, ${index$1.getType(value)} found`)];
}
if (value !== value) {
return [new index$1.ValidationError(key, value, `number expected, NaN found`)];
}
if ("minimum" in valueSpec) {
let specMin = valueSpec.minimum;
if (Array.isArray(valueSpec.minimum)) {
const i = options.arrayIndex;
specMin = valueSpec.minimum[i];
}
if (value < specMin) {
return [new index$1.ValidationError(key, value, `${value} is less than the minimum value ${specMin}`)];
}
}
if ("maximum" in valueSpec) {
let specMax = valueSpec.maximum;
if (Array.isArray(valueSpec.maximum)) {
const i = options.arrayIndex;
specMax = valueSpec.maximum[i];
}
if (value > specMax) {
return [new index$1.ValidationError(key, value, `${value} is greater than the maximum value ${specMax}`)];
}
}
return [];
}
function hasObjectStops(value) {
const stops = value["stops"];
return Array.isArray(stops) && Array.isArray(stops[0]) && index$1.isObject(stops[0][0]);
}
function validateFunction(options) {
const key = options.key;
const value = options.value;
if (!index$1.isObject(value)) {
return [new index$1.ValidationError(key, value, `object expected, ${index$1.getType(value)} found`)];
}
const functionValueSpec = options.valueSpec;
const functionType = index$1.unbundle(value.type);
let stopKeyType;
let stopDomainValues = {};
let previousStopDomainValue;
let previousStopDomainZoom;
const isZoomFunction = functionType !== "categorical" && value.property === void 0;
const isPropertyFunction = !isZoomFunction;
const isZoomAndPropertyFunction = hasObjectStops(value);
const errors = validateObject({
key: options.key,
value: options.value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: options.styleSpec.function,
style: options.style,
styleSpec: options.styleSpec,
objectElementValidators: {
stops: validateFunctionStops,
default: validateFunctionDefault
}
});
if (functionType === "identity" && isZoomFunction) {
errors.push(new index$1.ValidationError(options.key, options.value, 'missing required property "property"'));
}
if (functionType !== "identity" && !value.stops) {
errors.push(new index$1.ValidationError(options.key, options.value, 'missing required property "stops"'));
}
if (functionType === "exponential" && functionValueSpec.expression && !index$1.supportsInterpolation(functionValueSpec)) {
errors.push(new index$1.ValidationError(options.key, options.value, "exponential functions not supported"));
}
if (options.styleSpec.$version >= 8) {
if (isPropertyFunction && !index$1.supportsPropertyExpression(functionValueSpec)) {
errors.push(new index$1.ValidationError(options.key, options.value, "property functions not supported"));
} else if (isZoomFunction && !index$1.supportsZoomExpression(functionValueSpec)) {
errors.push(new index$1.ValidationError(options.key, options.value, "zoom functions not supported"));
}
}
if ((functionType === "categorical" || isZoomAndPropertyFunction) && value.property === void 0) {
errors.push(new index$1.ValidationError(options.key, options.value, '"property" property is required'));
}
return errors;
function validateFunctionStops(options2) {
if (functionType === "identity") {
return [new index$1.ValidationError(options2.key, options2.value, 'identity function may not have a "stops" property')];
}
let errors2 = [];
const value2 = options2.value;
errors2 = errors2.concat(validateArray({
key: options2.key,
value: value2,
valueSpec: options2.valueSpec,
style: options2.style,
styleSpec: options2.styleSpec,
arrayElementValidator: validateFunctionStop
}));
if (Array.isArray(value2) && value2.length === 0) {
errors2.push(new index$1.ValidationError(options2.key, value2, "array must have at least one stop"));
}
return errors2;
}
function validateFunctionStop(options2) {
let errors2 = [];
const value2 = options2.value;
const key2 = options2.key;
if (!Array.isArray(value2)) {
return [new index$1.ValidationError(key2, value2, `array expected, ${index$1.getType(value2)} found`)];
}
if (value2.length !== 2) {
return [new index$1.ValidationError(key2, value2, `array length 2 expected, length ${value2.length} found`)];
}
if (isZoomAndPropertyFunction) {
if (!index$1.isObject(value2[0])) {
return [new index$1.ValidationError(key2, value2, `object expected, ${index$1.getType(value2[0])} found`)];
}
const stopKey = value2[0];
if (stopKey.zoom === void 0) {
return [new index$1.ValidationError(key2, value2, "object stop key must have zoom")];
}
if (stopKey.value === void 0) {
return [new index$1.ValidationError(key2, value2, "object stop key must have value")];
}
const nextStopDomainZoom = index$1.unbundle(stopKey.zoom);
if (typeof nextStopDomainZoom !== "number") {
return [new index$1.ValidationError(key2, stopKey.zoom, "stop zoom values must be numbers")];
}
if (previousStopDomainZoom && previousStopDomainZoom > nextStopDomainZoom) {
return [new index$1.ValidationError(key2, stopKey.zoom, "stop zoom values must appear in ascending order")];
}
if (nextStopDomainZoom !== previousStopDomainZoom) {
previousStopDomainZoom = nextStopDomainZoom;
previousStopDomainValue = void 0;
stopDomainValues = {};
}
errors2 = errors2.concat(validateObject({
key: `${key2}[0]`,
value: value2[0],
valueSpec: { zoom: {} },
style: options2.style,
styleSpec: options2.styleSpec,
objectElementValidators: { zoom: validateNumber, value: validateStopDomainValue }
}));
} else {
errors2 = errors2.concat(validateStopDomainValue({
key: `${key2}[0]`,
value: value2[0],
valueSpec: {},
style: options2.style,
styleSpec: options2.styleSpec
}, value2));
}
if (index$1.isExpression(index$1.deepUnbundle(value2[1]))) {
return errors2.concat([new index$1.ValidationError(`${key2}[1]`, value2[1], "expressions are not allowed in function stops.")]);
}
return errors2.concat(validate({
key: `${key2}[1]`,
value: value2[1],
valueSpec: functionValueSpec,
style: options2.style,
styleSpec: options2.styleSpec
}));
}
function validateStopDomainValue(options2, stop) {
const type = index$1.getType(options2.value);
const value2 = index$1.unbundle(options2.value);
const reportValue = options2.value !== null ? options2.value : stop;
if (!stopKeyType) {
stopKeyType = type;
} else if (type !== stopKeyType) {
return [new index$1.ValidationError(options2.key, reportValue, `${type} stop domain type must match previous stop domain type ${stopKeyType}`)];
}
if (type !== "number" && type !== "string" && type !== "boolean" && typeof value2 !== "number" && typeof value2 !== "string" && typeof value2 !== "boolean") {
return [new index$1.ValidationError(options2.key, reportValue, "stop domain value must be a number, string, or boolean")];
}
if (type !== "number" && functionType !== "categorical") {
let message = `number expected, ${type} found`;
if (index$1.supportsPropertyExpression(functionValueSpec) && functionType === void 0) {
message += '\nIf you intended to use a categorical function, specify `"type": "categorical"`.';
}
return [new index$1.ValidationError(options2.key, reportValue, message)];
}
if (functionType === "categorical" && type === "number" && (typeof value2 !== "number" || !isFinite(value2) || Math.floor(value2) !== value2)) {
return [new index$1.ValidationError(options2.key, reportValue, `integer expected, found ${String(value2)}`)];
}
if (functionType !== "categorical" && type === "number" && typeof value2 === "number" && typeof previousStopDomainValue === "number" && previousStopDomainValue !== void 0 && value2 < previousStopDomainValue) {
return [new index$1.ValidationError(options2.key, reportValue, "stop domain values must appear in ascending order")];
} else {
previousStopDomainValue = value2;
}
if (functionType === "categorical" && value2 in stopDomainValues) {
return [new index$1.ValidationError(options2.key, reportValue, "stop domain values must be unique")];
} else {
stopDomainValues[value2] = true;
}
return [];
}
function validateFunctionDefault(options2) {
return validate({
key: options2.key,
value: options2.value,
valueSpec: functionValueSpec,
style: options2.style,
styleSpec: options2.styleSpec
});
}
}
function validateExpression(options) {
const expression = (options.expressionContext === "property" ? index$1.createPropertyExpression : index$1.createExpression)(index$1.deepUnbundle(options.value), options.valueSpec);
if (expression.result === "error") {
return expression.value.map((error) => {
return new index$1.ValidationError(`${options.key}${error.key}`, options.value, error.message);
});
}
const expressionObj = expression.value.expression || expression.value._styleExpression.expression;
if (options.expressionContext === "property" && options.propertyKey === "text-font" && // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
!expressionObj.outputDefined()) {
return [new index$1.ValidationError(options.key, options.value, `Invalid data expression for "${options.propertyKey}". Output values must be contained as literals within the expression.`)];
}
if (options.expressionContext === "property" && options.propertyType === "layout" && // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
!index$1.isStateConstant(expressionObj)) {
return [new index$1.ValidationError(options.key, options.value, '"feature-state" data expressions are not supported with layout properties.')];
}
if (options.expressionContext === "filter") {
return disallowedFilterParameters(expressionObj, options);
}
if (options.expressionContext === "appearance") {
return checkDisallowedParameters(expressionObj, options);
}
if (options.expressionContext && options.expressionContext.indexOf("cluster") === 0) {
if (!index$1.isGlobalPropertyConstant(expressionObj, ["zoom", "feature-state"])) {
return [new index$1.ValidationError(options.key, options.value, '"zoom" and "feature-state" expressions are not supported with cluster properties.')];
}
if (options.expressionContext === "cluster-initial" && !index$1.isFeatureConstant(expressionObj)) {
return [new index$1.ValidationError(options.key, options.value, "Feature data expressions are not supported with initial expression part of cluster properties.")];
}
}
return [];
}
function disallowedFilterParameters(e, options) {
const disallowedParameters = /* @__PURE__ */ new Set([
"zoom",
"feature-state",
"pitch",
"distance-from-center"
]);
if (options.valueSpec && options.valueSpec.expression) {
for (const param of options.valueSpec.expression.parameters) {
disallowedParameters.delete(param);
}
}
if (disallowedParameters.size === 0) {
return [];
}
const errors = [];
if (e instanceof index$1.CompoundExpression) {
if (disallowedParameters.has(e.name)) {
return [new index$1.ValidationError(options.key, options.value, `["${e.name}"] expression is not supported in a filter for a ${options.object.type} layer with id: ${options.object.id}`)];
}
}
e.eachChild((arg) => {
errors.push(...disallowedFilterParameters(arg, options));
});
return errors;
}
function checkDisallowedParameters(e, options) {
const allowedParameters = /* @__PURE__ */ new Set();
if (options.valueSpec && options.valueSpec.expression) {
for (const param of options.valueSpec.expression.parameters) {
allowedParameters.add(param);
}
}
if (allowedParameters.size === 0) {
return [];
}
const errors = [];
if (e instanceof index$1.CompoundExpression) {
if (!allowedParameters.has(e.name)) {
return [new index$1.ValidationError(options.key, options.value, `["${e.name}"] is not an allowed parameter`)];
}
}
e.eachChild((arg) => {
errors.push(...checkDisallowedParameters(arg, options));
});
return errors;
}
function validateBoolean(options) {
const value = options.value;
const key = options.key;
if (!index$1.isBoolean(value)) {
return [new index$1.ValidationError(key, value, `boolean expected, ${index$1.getType(value)} found`)];
}
return [];
}
function validateColor({ key, value }) {
if (!index$1.isString(value)) {
return [new index$1.ValidationError(key, value, `color expected, ${index$1.getType(value)} found`)];
}
if (index$1.csscolorparserExports.parseCSSColor(value) === null) {
return [new index$1.ValidationError(key, value, `color expected, "${value}" found`)];
}
return [];
}
function validateEnum(options) {
const key = options.key;
const value = options.value;
const valueSpec = options.valueSpec;
const errors = [];
if (Array.isArray(valueSpec.values)) {
if (valueSpec.values.indexOf(index$1.unbundle(value)) === -1) {
errors.push(new index$1.ValidationError(key, value, `expected one of [${valueSpec.values.join(", ")}], ${JSON.stringify(value)} found`));
}
} else {
if (Object.keys(valueSpec.values).indexOf(index$1.unbundle(value)) === -1) {
errors.push(new index$1.ValidationError(key, value, `expected one of [${Object.keys(valueSpec.values).join(", ")}], ${JSON.stringify(value)} found`));
}
}
return errors;
}
function validateFilter$1(options) {
if (index$1.isExpressionFilter(index$1.deepUnbundle(options.value))) {
const layerType = options.layerType || "fill";
return validateExpression(Object.assign({}, options, {
expressionContext: "filter",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: options.styleSpec[`filter_${layerType}`]
}));
} else {
return validateNonExpressionFilter(options);
}
}
function validateNonExpressionFilter(options) {
const value = options.value;
const key = options.key;
if (!Array.isArray(value)) {
return [new index$1.ValidationError(key, value, `array expected, ${index$1.getType(value)} found`)];
}
if (value.length < 1) {
return [new index$1.ValidationError(key, value, "filter array must have at least 1 element")];
}
const styleSpec = options.styleSpec;
let errors = validateEnum({
key: `${key}[0]`,
value: value[0],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.filter_operator
});
const validate = () => {
if (value.length >= 2) {
if (!index$1.isString(value[1])) {
errors.push(new index$1.ValidationError(`${key}[1]`, value[1], `string expected, ${index$1.getType(value[1])} found`));
}
}
for (let i = 2; i < value.length; i++) {
if (index$1.unbundle(value[1]) === "$type") {
errors = errors.concat(validateEnum({
key: `${key}[${i}]`,
value: value[i],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.geometry_type
}));
} else if (!index$1.isString(value[i]) && !index$1.isNumber(value[i]) && !index$1.isBoolean(value[i])) {
errors.push(new index$1.ValidationError(`${key}[${i}]`, value[i], `string, number, or boolean expected, ${index$1.getType(value[i])} found.`));
}
}
};
switch (index$1.unbundle(value[0])) {
case "<":
case "<=":
case ">":
case ">=":
if (value.length >= 2 && index$1.unbundle(value[1]) === "$type") {
errors.push(new index$1.ValidationError(key, value, `"$type" cannot be use with operator "${value[0]}"`));
}
if (value.length !== 3) {
errors.push(new index$1.ValidationError(key, value, `filter array for operator "${value[0]}" must have 3 elements`));
}
validate();
break;
case "==":
case "!=":
if (value.length !== 3) {
errors.push(new index$1.ValidationError(key, value, `filter array for operator "${value[0]}" must have 3 elements`));
}
validate();
break;
case "in":
case "!in":
validate();
break;
case "any":
case "all":
case "none":
for (let i = 1; i < value.length; i++) {
errors = errors.concat(validateNonExpressionFilter({
key: `${key}[${i}]`,
value: value[i],
style: options.style,
styleSpec: options.styleSpec
}));
}
break;
case "has":
case "!has":
if (value.length !== 2) {
errors.push(new index$1.ValidationError(key, value, `filter array for "${value[0]}" operator must have 2 elements`));
} else if (!index$1.isString(value[1])) {
errors.push(new index$1.ValidationError(`${key}[1]`, value[1], `string expected, ${index$1.getType(value[1])} found`));
}
break;
}
return errors;
}
function validateProperty(options, propertyType) {
const key = options.key;
const style = options.style;
const layer = options.layer;
const styleSpec = options.styleSpec;
const value = options.value;
const propertyKey = options.objectKey;
const layerSpec = styleSpec[`${propertyType}_${options.layerType}`];
if (!layerSpec) return [];
const useThemeMatch = propertyKey.match(/^(.*)-use-theme$/);
if (useThemeMatch && layerSpec[useThemeMatch[1]]) {
if (index$1.isExpression(index$1.deepUnbundle(value))) {
const errors2 = [];
return errors2.concat(validate({
key,
value,
valueSpec: {
type: "string",
expression: {
interpolated: false,
parameters: ["zoom", "feature"]
},
"property-type": "data-driven"
},
style,
styleSpec,
expressionContext: "property",
propertyType,
propertyKey
}));
}
return validate({
key,
value,
valueSpec: { type: "string" },
style,
styleSpec
});
}
const transitionMatch = propertyKey.match(/^(.*)-transition$/);
if (propertyType === "paint" && transitionMatch && layerSpec[transitionMatch[1]] && layerSpec[transitionMatch[1]].transition) {
return validate({
key,
value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
});
}
const valueSpec = options.valueSpec || layerSpec[propertyKey];
if (!valueSpec) {
return [new index$1.ValidationWarning(key, value, `unknown property "${propertyKey}"`)];
}
let tokenMatch;
if (index$1.isString(value) && index$1.supportsPropertyExpression(valueSpec) && !valueSpec.tokens && (tokenMatch = /^{([^}]+)}$/.exec(value))) {
const example = `\`{ "type": "identity", "property": ${tokenMatch ? JSON.stringify(tokenMatch[1]) : '"_"'} }\``;
return [new index$1.ValidationError(
key,
value,
`"${propertyKey}" does not support interpolation syntax
Use an identity property function instead: ${example}.`
)];
}
const errors = [];
if (options.layerType === "symbol") {
if (propertyKey === "text-field" && style && !style.glyphs && !style.imports) {
errors.push(new index$1.ValidationError(key, value, 'use of "text-field" requires a style "glyphs" property'));
}
if (propertyKey === "text-font" && index$1.isFunction(index$1.deepUnbundle(value)) && index$1.unbundle(value.type) === "identity") {
errors.push(new index$1.ValidationError(key, value, '"text-font" does not support identity functions'));
}
} else if (options.layerType === "model" && propertyType === "paint" && layer && layer.layout && layer.layout.hasOwnProperty("model-id")) {
if (index$1.supportsPropertyExpression(valueSpec) && (index$1.supportsLightExpression(valueSpec) || index$1.supportsZoomExpression(valueSpec))) {
const expression = index$1.createPropertyExpression(index$1.deepUnbundle(value), valueSpec);
const expressionValue = expression.value;
const expressionObj = "expression" in expressionValue && expressionValue.expression || "_styleExpression" in expressionValue && expressionValue._styleExpression && expressionValue._styleExpression.expression;
if (expressionObj && !index$1.isGlobalPropertyConstant(expressionObj, ["measure-light"])) {
if (propertyKey !== "model-emissive-strength" || (!index$1.isFeatureConstant(expressionObj) || !index$1.isStateConstant(expressionObj))) {
errors.push(new index$1.ValidationError(key, value, `${propertyKey} does not support measure-light expressions when the model layer source is vector tile or GeoJSON.`));
}
}
}
}
return errors.concat(validate({
key: options.key,
value,
valueSpec,
style,
styleSpec,
expressionContext: "property",
propertyType,
propertyKey
}));
}
function validateAppearance(options) {
const { key, layer, layerType } = options;
const value = index$1.unbundle(options.value);
const name = index$1.unbundle(value.name);
const condition = index$1.unbundle(value.condition);
const errors = validateObject({
key,
value,
valueSpec: options.styleSpec.appearance,
style: options.style,
styleSpec: options.styleSpec,
objectElementValidators: {
condition: (options2) => validateCondition(Object.assign({ layer, layerType }, options2)),
properties: (options2) => validateProperties(Object.assign({ layer, layerType }, options2))
}
});
if (name !== "hidden" && condition === void 0) {
errors.push(new index$1.ValidationError(options.key, "name", `Appearance with name different than "hidden" must have a condition`));
}
return errors;
}
function validateProperties(options) {
const errors = [];
const { styleSpec, layer, layerType } = options;
const paintProperties = styleSpec[`paint_${layerType}`];
const layoutProperties = styleSpec[`layout_${layerType}`];
const properties = options.object[options.objectKey];
for (const propertyKey in properties) {
const propertyType = propertyKey in paintProperties ? "paint" : propertyKey in layoutProperties ? "layout" : void 0;
if (!propertyType) {
errors.push(new index$1.ValidationError(options.key, propertyKey, `unknown property "${propertyKey}" for layer type "${layerType}"`));
continue;
}
const propertyValidationOptions = Object.assign({}, options, {
key: `${options.key}.${propertyKey}`,
object: properties,
objectKey: propertyKey,
layer,
layerType,
value: properties[propertyKey],
valueSpec: propertyType === "paint" ? paintProperties[propertyKey] : layoutProperties[propertyKey]
});
errors.push(...validateProperty(propertyValidationOptions, propertyType));
}
return errors;
}
function validateCondition(options) {
const errors = [];
const appearance = options.object;
const condition = appearance.condition;
errors.push(...validateExpression({
key: options.key,
value: condition,
valueSpec: index$1.spec["appearance"]["condition"],
expressionContext: "appearance"
}));
return errors;
}
function validatePaintProperty$1(options) {
return validateProperty(options, "paint");
}
function validateLayoutProperty$1(options) {
return validateProperty(options, "layout");
}
function validateLayer$1(options) {
let errors = [];
const layer = options.value;
const key = options.key;
const style = options.style;
const styleSpec = options.styleSpec;
if (!index$1.isObject(layer)) {
return [new index$1.ValidationError(key, layer, `object expected`)];
}
if (!layer.type && !layer.ref) {
errors.push(new index$1.ValidationError(key, layer, 'either "type" or "ref" is required'));
}
let type = index$1.unbundle(layer.type);
const ref = index$1.unbundle(layer.ref);
if (layer.id) {
const layerId = index$1.unbundle(layer.id);
for (let i = 0; i < options.arrayIndex; i++) {
const otherLayer = style.layers[i];
if (index$1.unbundle(otherLayer.id) === layerId) {
errors.push(new index$1.ValidationError(key, layer.id, `duplicate layer id "${layerId}", previously used at line ${otherLayer.id.__line__}`));
}
}
}
if ("ref" in layer) {
["type", "source", "source-layer", "filter", "layout"].forEach((p) => {
if (p in layer) {
errors.push(new index$1.ValidationError(key, layer[p], `"${p}" is prohibited for ref layers`));
}
});
let parent;
style.layers.forEach((layer2) => {
if (index$1.unbundle(layer2.id) === ref) parent = layer2;
});
if (!parent) {
if (typeof ref === "string")
errors.push(new index$1.ValidationError(key, layer.ref, `ref layer "${ref}" not found`));
} else if (parent.ref) {
errors.push(new index$1.ValidationError(key, layer.ref, "ref cannot reference another ref layer"));
} else {
type = index$1.unbundle(parent.type);
}
} else if (!(type === "background" || type === "sky" || type === "slot")) {
if (!layer.source) {
errors.push(new index$1.ValidationError(key, layer, 'missing required property "source"'));
} else if (!index$1.isString(layer.source)) {
errors.push(new index$1.ValidationError(`${key}.source`, layer.source, '"source" must be a string'));
} else {
const source = style.sources && style.sources[layer.source];
const sourceType = source && index$1.unbundle(source.type);
if (!source) {
errors.push(new index$1.ValidationError(key, layer.source, `source "${layer.source}" not found`));
} else if (sourceType === "vector" && type === "raster") {
errors.push(new index$1.ValidationError(key, layer.source, `layer "${layer.id}" requires a raster source`));
} else if (sourceType === "raster" && type !== "raster") {
errors.push(new index$1.ValidationError(key, layer.source, `layer "${layer.id}" requires a vector source`));
} else if (sourceType === "vector" && !layer["source-layer"]) {
errors.push(new index$1.ValidationError(key, layer, `layer "${layer.id}" must specify a "source-layer"`));
} else if (sourceType === "raster-dem" && type !== "hillshade") {
errors.push(new index$1.ValidationError(key, layer.source, "raster-dem source can only be used with layer type 'hillshade'."));
} else if (sourceType === "raster-array" && !["raster", "raster-particle"].includes(type)) {
errors.push(new index$1.ValidationError(key, layer.source, `raster-array source can only be used with layer type 'raster'.`));
} else if (type === "line" && layer.paint && (layer.paint["line-gradient"] || layer.paint["line-trim-offset"]) && (sourceType === "geojson" && !source.lineMetrics)) {
errors.push(new index$1.ValidationError(key, layer, `layer "${layer.id}" specifies a line-gradient, which requires the GeoJSON source to have \`lineMetrics\` enabled.`));
} else if (type === "raster-particle" && sourceType !== "raster-array") {
errors.push(new index$1.ValidationError(key, layer.source, `layer "${layer.id}" requires a 'raster-array' source.`));
}
}
}
errors = errors.concat(validateObject({
key,
value: layer,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.layer,
style: options.style,
styleSpec: options.styleSpec,
objectElementValidators: {
"*"() {
return [];
},
// We don't want to enforce the spec's `"requires": true` for backward compatibility with refs;
// the actual requirement is validated above. See https://github.com/mapbox/mapbox-gl-js/issues/5772.
type() {
return validate({
key: `${key}.type`,
value: layer.type,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: styleSpec.layer.type,
style: options.style,
styleSpec: options.styleSpec,
object: layer,
objectKey: "type"
});
},
filter(options2) {
return validateFilter$1(Object.assign({ layerType: type }, options2));
},
layout(options2) {
return validateObject({
layer,
key: options2.key,
value: options2.value,
valueSpec: {},
style: options2.style,
styleSpec: options2.styleSpec,
objectElementValidators: {
"*"(options3) {
return validateLayoutProperty$1(Object.assign({ layerType: type }, options3));
}
}
});
},
paint(options2) {
return validateObject({
layer,
key: options2.key,
value: options2.value,
valueSpec: {},
style: options2.style,
styleSpec: options2.styleSpec,
objectElementValidators: {
"*"(options3) {
return validatePaintProperty$1(Object.assign({ layerType: type, layer }, options3));
}
}
});
},
appearances(options2) {
const validationErrors = validateArray({
key: options2.key,
value: options2.value,
valueSpec: options2.valueSpec,
style: options2.style,
styleSpec: options2.styleSpec,
arrayElementValidator: (options3) => validateAppearance(Object.assign({ layerType: type, layer }, options3))
});
const appearances = Array.isArray(options2.value) ? options2.value : [];
const dedupedNames = /* @__PURE__ */ new Set();
appearances.forEach((a, index) => {
const name = index$1.unbundle(a.name);
if (name) {
if (dedupedNames.has(name)) {
const layerId = index$1.unbundle(layer.id);
validationErrors.push(new index$1.ValidationError(options2.key, name, `Duplicated appearance name "${name}" for layer "${layerId}"`));
} else {
dedupedNames.add(name);
}
}
});
return validationErrors;
}
}
}));
return errors;
}
function validateString({ key, value }) {
if (index$1.isString(value)) {
return [];
}
return [new index$1.ValidationError(key, value, `string expected, ${index$1.getType(value)} found`)];
}
const objectElementValidators = {
promoteId: validatePromoteId
};
function validateSource$1(options) {
const value = options.value;
const key = options.key;
const styleSpec = options.styleSpec;
const style = options.style;
if (!index$1.isObject(value)) {
return [new index$1.ValidationError(key, value, `object expected, ${index$1.getType(value)} found`)];
}
if (!("type" in value)) {
return [new index$1.ValidationError(key, value, '"type" is required')];
}
const type = index$1.unbundle(value.type);
let errors = [];
if (["vector", "raster", "raster-dem", "raster-array"].includes(type)) {
if (!("url" in value) && !("tiles" in value)) {
errors.push(new index$1.ValidationWarning(key, value, 'Either "url" or "tiles" is required.'));
}
}
switch (type) {
case "vector":
case "raster":
case "raster-dem":
case "raster-array":
errors = errors.concat(validateObject({
key,
value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec[`source_${type.replace("-", "_")}`],
style: options.style,
styleSpec,
objectElementValidators
}));
return errors;
case "geojson":
errors = validateObject({
key,
value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.source_geojson,
style,
styleSpec,
objectElementValidators
});
if ("cluster" in value && "clusterProperties" in value) {
if (!index$1.isObject(value.clusterProperties)) {
return [new index$1.ValidationError(`${key}.clusterProperties`, value, `object expected, ${index$1.getType(value)} found`)];
}
for (const prop in value.clusterProperties) {
const propValue = value.clusterProperties[prop];
if (!Array.isArray(propValue)) {
return [new index$1.ValidationError(`${key}.clusterProperties.${prop}`, propValue, "array expected")];
}
const [operator, mapExpr] = propValue;
const reduceExpr = typeof operator === "string" ? [operator, ["accumulated"], ["get", prop]] : operator;
errors.push(...validateExpression({
key: `${key}.${prop}.map`,
value: mapExpr,
expressionContext: "cluster-map"
}));
errors.push(...validateExpression({
key: `${key}.${prop}.reduce`,
value: reduceExpr,
expressionContext: "cluster-reduce"
}));
}
}
return errors;
case "video":
return validateObject({
key,
value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.source_video,
style,
styleSpec
});
case "image":
return validateObject({
key,
value,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.source_image,
style,
styleSpec
});
case "canvas":
return [new index$1.ValidationError(key, null, `Please use runtime APIs to add canvas sources, rather than including them in stylesheets.`, "source.canvas")];
default:
return validateEnum({
key: `${key}.type`,
value: value.type,
valueSpec: { values: getSourceTypeValues(styleSpec) }
});
}
}
function getSourceTypeValues(styleSpec) {
const sourceArray = styleSpec.source;
return sourceArray.reduce((memo, source) => {
const sourceType = styleSpec[source];
if (sourceType.type.type === "enum") {
memo = memo.concat(Object.keys(sourceType.type.values || {}));
}
return memo;
}, []);
}
function validatePromoteId({ key, value }) {
if (index$1.isString(value)) {
return validateString({ key, value });
}
if (Array.isArray(value)) {
const errors2 = [];
const unbundledValue = index$1.deepUnbundle(value);
const expression = index$1.createExpression(unbundledValue);
if (expression.result === "error") {
expression.value.forEach((err) => {
errors2.push(new index$1.ValidationError(`${key}${err.key}`, null, `${err.message}`));
});
}
const parsed = expression.value.expression;
const onlyFeatureDependent = index$1.isGlobalPropertyConstant(parsed, ["zoom", "heatmap-density", "line-progress", "raster-value", "sky-radial-progress", "accumulated", "is-supported-script", "pitch", "distance-from-center", "measure-light", "raster-particle-speed"]);
if (!onlyFeatureDependent) {
errors2.push(new index$1.ValidationError(`${key}`, null, "promoteId expression should be only feature dependent"));
}
return errors2;
}
if (!index$1.isObject(value)) {
return [new index$1.ValidationError(key, value, `string, expression or object expected, "${index$1.getType(value)}" found`)];
}
const errors = [];
for (const prop in value) {
errors.push(...validatePromoteId({ key: `${key}.${prop}`, value: value[prop] }));
}
return errors;
}
function validateLight$1(options) {
const light = options.value;
const styleSpec = options.styleSpec;
const lightSpec = styleSpec.light;
const style = options.style;
if (light === void 0) {
return [];
}
if (!index$1.isObject(light)) {
return [new index$1.ValidationError("light", light, `object expected, ${index$1.getType(light)} found`)];
}
let errors = [];
for (const key in light) {
const transitionMatch = key.match(/^(.*)-transition$/);
const useThemeMatch = key.match(/^(.*)-use-theme$/);
if (useThemeMatch && lightSpec[useThemeMatch[1]]) {
errors = errors.concat(validate({
key,
value: light[key],
valueSpec: { type: "string" },
style,
styleSpec
}));
} else if (transitionMatch && lightSpec[transitionMatch[1]] && lightSpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key,
value: light[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (lightSpec[key]) {
errors = errors.concat(validate({
key,
value: light[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: lightSpec[key],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationError(key, light[key], `unknown property "${key}"`)]);
}
}
return errors;
}
function validateLights$1(options) {
const light = options.value;
if (!light) {
return [];
}
const key = options.key;
if (!index$1.isObject(light)) {
return [new index$1.ValidationError(key, light, `object expected, ${index$1.getType(light)} found`)];
}
let errors = [];
const styleSpec = options.styleSpec;
const lightSpec = styleSpec["light-3d"];
const style = options.style;
const lights = options.style.lights;
for (const prop of ["type", "id"]) {
if (!(prop in light)) {
errors = errors.concat([new index$1.ValidationError(key, light, `missing property "${prop}"`)]);
return errors;
}
}
if (!index$1.isString(light.type)) {
errors = errors.concat([new index$1.ValidationError(`${key}.type`, light.type, `string expected`)]);
return errors;
}
if (lights) {
for (let i = 0; i < options.arrayIndex; i++) {
const lightType2 = index$1.unbundle(light.type);
const otherLight = lights[i];
if (index$1.unbundle(otherLight.type) === lightType2) {
errors.push(new index$1.ValidationError(key, light.id, `duplicate light type "${light.type}", previously defined at line ${otherLight.id.__line__}`));
}
}
}
const lightType = `properties_light_${light.type}`;
if (!(lightType in styleSpec)) {
errors = errors.concat([new index$1.ValidationError(`${key}.type`, light, `Invalid light type ${light.type}`)]);
return errors;
}
const lightPropertySpec = styleSpec[lightType];
for (const key2 in light) {
if (key2 === "properties") {
const properties = light[key2];
if (!index$1.isObject(properties)) {
errors = errors.concat([new index$1.ValidationError("properties", properties, `object expected, ${index$1.getType(properties)} found`)]);
return errors;
}
for (const propertyKey in properties) {
const transitionMatch = propertyKey.match(/^(.*)-transition$/);
const useThemeMatch = propertyKey.match(/^(.*)-use-theme$/);
if (useThemeMatch && lightPropertySpec[useThemeMatch[1]]) {
errors = errors.concat(validate({
key: key2,
value: properties[propertyKey],
valueSpec: { type: "string" },
style,
styleSpec
}));
} else if (transitionMatch && lightPropertySpec[transitionMatch[1]] && lightPropertySpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key: key2,
value: light[key2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (!lightPropertySpec[propertyKey]) {
errors = errors.concat([new index$1.ValidationWarning(options.key, properties[propertyKey], `unknown property "${propertyKey}"`)]);
} else {
errors = errors.concat(validate({
key: propertyKey,
value: properties[propertyKey],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: lightPropertySpec[propertyKey],
style,
styleSpec
}));
}
}
} else {
if (lightSpec[key2]) {
errors = errors.concat(validate({
key: key2,
value: light[key2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: lightSpec[key2],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationWarning(key2, light[key2], `unknown property "${key2}"`)]);
}
}
}
return errors;
}
function validateTerrain$1(options) {
const terrain = options.value;
const key = options.key;
const style = options.style;
const styleSpec = options.styleSpec;
const terrainSpec = styleSpec.terrain;
if (terrain == null) {
return [];
}
if (!index$1.isObject(terrain)) {
return [new index$1.ValidationError("terrain", terrain, `object expected, ${index$1.getType(terrain)} found`)];
}
let errors = [];
for (const key2 in terrain) {
const transitionMatch = key2.match(/^(.*)-transition$/);
const useThemeMatch = key2.match(/^(.*)-use-theme$/);
if (useThemeMatch && terrainSpec[useThemeMatch[1]]) {
errors = errors.concat(validate({
key: key2,
value: terrain[key2],
valueSpec: { type: "string" },
style,
styleSpec
}));
} else if (transitionMatch && terrainSpec[transitionMatch[1]] && terrainSpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key: key2,
value: terrain[key2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (terrainSpec[key2]) {
errors = errors.concat(validate({
key: key2,
value: terrain[key2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: terrainSpec[key2],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationWarning(key2, terrain[key2], `unknown property "${key2}"`)]);
}
}
if (!terrain.source) {
errors.push(new index$1.ValidationError(key, terrain, `terrain is missing required property "source"`));
} else if (!index$1.isString(terrain.source)) {
errors.push(new index$1.ValidationError(`${key}.source`, terrain.source, `source must be a string`));
} else {
const source = style.sources && style.sources[terrain.source];
const sourceType = source && index$1.unbundle(source.type);
if (!source) {
errors.push(new index$1.ValidationError(`${key}.source`, terrain.source, `source "${terrain.source}" not found`));
} else if (sourceType !== "raster-dem") {
errors.push(new index$1.ValidationError(`${key}.source`, terrain.source, `terrain cannot be used with a source of type ${sourceType}, it only be used with a "raster-dem" source type`));
}
}
return errors;
}
function validateFog$1(options) {
const fog = options.value;
const style = options.style;
const styleSpec = options.styleSpec;
const fogSpec = styleSpec.fog;
if (fog === void 0) {
return [];
}
if (!index$1.isObject(fog)) {
return [new index$1.ValidationError("fog", fog, `object expected, ${index$1.getType(fog)} found`)];
}
let errors = [];
for (const key in fog) {
const transitionMatch = key.match(/^(.*)-transition$/);
const useThemeMatch = key.match(/^(.*)-use-theme$/);
if (useThemeMatch && fogSpec[useThemeMatch[1]]) {
errors = errors.concat(validate({
key,
value: fog[key],
valueSpec: { type: "string" },
style,
styleSpec
}));
} else if (transitionMatch && fogSpec[transitionMatch[1]] && fogSpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key,
value: fog[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (fogSpec[key]) {
errors = errors.concat(validate({
key,
value: fog[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
valueSpec: fogSpec[key],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationWarning(key, fog[key], `unknown property "${key}"`)]);
}
}
return errors;
}
function validateFormatted(options) {
const errors = validateString(options);
if (errors.length === 0) {
return [];
}
return validateExpression(options);
}
function validateImage(options) {
const errors = validateString(options);
if (errors.length === 0) {
return [];
}
return validateExpression(options);
}
function validateProjection(options) {
const projection = options.value;
const styleSpec = options.styleSpec;
const projectionSpec = styleSpec.projection;
const style = options.style;
if (index$1.isObject(projection)) {
let errors = [];
for (const key in projection) {
errors = errors.concat(validate({
key,
value: projection[key],
valueSpec: projectionSpec[key],
style,
styleSpec
}));
}
return errors;
}
if (!index$1.isString(projection)) {
return [new index$1.ValidationError("projection", projection, `object or string expected, ${index$1.getType(projection)} found`)];
}
return [];
}
function isSourceIconset(type, iconset) {
return !!(type === "source" && iconset.source);
}
function validateIconset(options) {
const iconset = options.value;
const key = options.key;
const styleSpec = options.styleSpec;
const style = options.style;
if (!index$1.isObject(iconset)) {
return [new index$1.ValidationError(key, iconset, "object expected")];
}
if (!iconset.type) {
return [new index$1.ValidationError(key, iconset, '"type" is required')];
}
const type = index$1.unbundle(iconset.type);
let errors = [];
errors = errors.concat(validateObject({
key,
value: iconset,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec[`iconset_${type}`],
style,
styleSpec
}));
if (isSourceIconset(type, iconset)) {
const source = style.sources && style.sources[iconset.source];
const sourceType = source && index$1.unbundle(source.type);
if (!source) {
errors.push(new index$1.ValidationError(key, iconset.source, `source "${iconset.source}" not found`));
} else if (sourceType !== "raster-array") {
errors.push(new index$1.ValidationError(key, iconset.source, `iconset cannot be used with a source of type ${String(sourceType)}, it only be used with a "raster-array" source type`));
}
}
return errors;
}
const VALIDATORS = {
"*": () => [],
"array": validateArray,
"boolean": validateBoolean,
"number": validateNumber,
"color": validateColor,
"enum": validateEnum,
"filter": validateFilter$1,
"function": validateFunction,
"layer": validateLayer$1,
"object": validateObject,
"source": validateSource$1,
"model": index$1.validateModel,
"light": validateLight$1,
"light-3d": validateLights$1,
"terrain": validateTerrain$1,
"fog": validateFog$1,
"string": validateString,
"formatted": validateFormatted,
"resolvedImage": validateImage,
"projection": validateProjection,
"import": validateImport,
"iconset": validateIconset
};
function validate(options, arrayAsExpression = false) {
const value = options.value;
const valueSpec = options.valueSpec;
const styleSpec = options.styleSpec;
if (valueSpec.expression) {
if (index$1.isFunction(index$1.unbundle(value))) return validateFunction(options);
if (index$1.isExpression(index$1.deepUnbundle(value))) return validateExpression(options);
}
if (valueSpec.type && VALIDATORS[valueSpec.type]) {
const errors2 = VALIDATORS[valueSpec.type](options);
if (arrayAsExpression === true && errors2.length > 0 && Array.isArray(options.value)) {
return validateExpression(options);
}
return errors2;
}
const errors = validateObject(Object.assign({}, options, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: valueSpec.type ? styleSpec[valueSpec.type] : valueSpec
}));
return errors;
}
function validateObject(options) {
const key = options.key;
const object = options.value;
const elementSpecs = options.valueSpec || {};
const elementValidators = options.objectElementValidators || {};
const style = options.style;
const styleSpec = options.styleSpec;
if (!index$1.isObject(object)) {
return [new index$1.ValidationError(key, object, `object expected, ${index$1.getType(object)} found`)];
}
let errors = [];
for (const objectKey in object) {
const elementSpecKey = objectKey.split(".")[0];
const elementSpec = elementSpecs[elementSpecKey] || elementSpecs["*"];
let validateElement;
if (elementValidators[elementSpecKey]) {
validateElement = elementValidators[elementSpecKey];
} else if (elementSpecs[elementSpecKey]) {
validateElement = validate;
} else if (elementValidators["*"]) {
validateElement = elementValidators["*"];
} else if (elementSpecs["*"]) {
validateElement = validate;
}
if (!validateElement) {
errors.push(new index$1.ValidationWarning(key, object[objectKey], `unknown property "${objectKey}"`));
continue;
}
errors = errors.concat(validateElement({
key: (key ? `${key}.` : key) + objectKey,
value: object[objectKey],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: elementSpec,
style,
styleSpec,
object,
objectKey
}, object));
}
for (const elementSpecKey in elementSpecs) {
if (elementValidators[elementSpecKey]) {
continue;
}
const elementSpec = elementSpecs[elementSpecKey];
if (elementSpec.required && elementSpec["default"] === void 0 && object[elementSpecKey] === void 0) {
errors.push(new index$1.ValidationError(key, object, `missing required property "${elementSpecKey}"`));
}
}
return errors;
}
function validateGlyphsUrl({ key, value }) {
const errors = validateString({ key, value });
if (errors.length) return errors;
const str = value;
if (str.indexOf("{fontstack}") === -1) {
errors.push(new index$1.ValidationError(key, value, '"glyphs" url must include a "{fontstack}" token'));
}
if (str.indexOf("{range}") === -1) {
errors.push(new index$1.ValidationError(key, value, '"glyphs" url must include a "{range}" token'));
}
return errors;
}
function validateStyle$1(style, styleSpec = index$1.spec, options = {}) {
const errors = validateObject({
key: options.key || "",
value: style,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: Object.assign(
styleSpec.$root,
// Skip validation of the root properties that are not defined in the style spec (e.g. 'owner').
{ "*": { type: "*" } }
),
styleSpec,
style,
objectElementValidators: {
glyphs: validateGlyphsUrl
}
});
return errors;
}
function validateSnow$1(options) {
const snow = options.value;
const style = options.style;
const styleSpec = options.styleSpec;
const snowSpec = styleSpec.snow;
if (snow === void 0) {
return [];
}
if (!index$1.isObject(snow)) {
return [new index$1.ValidationError("snow", snow, `object expected, ${index$1.getType(snow)} found`)];
}
let errors = [];
for (const key in snow) {
const transitionMatch = key.match(/^(.*)-transition$/);
if (transitionMatch && snowSpec[transitionMatch[1]] && snowSpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key,
value: snow[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (snowSpec[key]) {
errors = errors.concat(validate({
key,
value: snow[key],
valueSpec: snowSpec[key],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationWarning(key, snow[key], `unknown property "${key}"`)]);
}
}
return errors;
}
function validateRain$1(options) {
const rain = options.value;
const style = options.style;
const styleSpec = options.styleSpec;
const rainSpec = styleSpec.rain;
if (rain === void 0) {
return [];
}
if (!index$1.isObject(rain)) {
return [new index$1.ValidationError("rain", rain, `object expected, ${index$1.getType(rain)} found`)];
}
let errors = [];
for (const key in rain) {
const transitionMatch = key.match(/^(.*)-transition$/);
if (transitionMatch && rainSpec[transitionMatch[1]] && rainSpec[transitionMatch[1]].transition) {
errors = errors.concat(validate({
key,
value: rain[key],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
valueSpec: styleSpec.transition,
style,
styleSpec
}));
} else if (rainSpec[key]) {
errors = errors.concat(validate({
key,
value: rain[key],
valueSpec: rainSpec[key],
style,
styleSpec
}));
} else {
errors = errors.concat([new index$1.ValidationWarning(key, rain[key], `unknown property "${key}"`)]);
}
}
return errors;
}
function validateStyle(style, styleSpec = index$1.spec) {
const errors = validateStyle$1(style, styleSpec);
return sortErrors(errors);
}
const validateSource = (opts) => sortErrors(validateSource$1(opts));
const validateLight = (opts) => sortErrors(validateLight$1(opts));
const validateLights = (opts) => sortErrors(validateLights$1(opts));
const validateTerrain = (opts) => sortErrors(validateTerrain$1(opts));
const validateFog = (opts) => sortErrors(validateFog$1(opts));
const validateSnow = (opts) => sortErrors(validateSnow$1(opts));
const validateRain = (opts) => sortErrors(validateRain$1(opts));
const validateLayer = (opts) => sortErrors(validateLayer$1(opts));
const validateFilter = (opts) => sortErrors(validateFilter$1(opts));
const validatePaintProperty = (opts) => sortErrors(validatePaintProperty$1(opts));
const validateLayoutProperty = (opts) => sortErrors(validateLayoutProperty$1(opts));
const validateModel = (opts) => sortErrors(index$1.validateModel(opts));
function sortErrors(errors) {
return errors.slice().sort((a, b) => a.line && b.line ? a.line - b.line : 0);
}
function emitValidationErrors$1(emitter, errors) {
let hasErrors = false;
if (errors && errors.length) {
for (const error of errors) {
if (error instanceof index$1.ValidationWarning) {
index$1.warnOnce(error.message);
} else {
emitter.fire(new index$1.ErrorEvent(new Error(error.message)));
hasErrors = true;
}
}
}
return hasErrors;
}
const lightReference = index$1.spec.light;
let properties$4;
const getProperties$4 = () => properties$4 || (properties$4 = new index$1.Properties({
"anchor": new index$1.DataConstantProperty(lightReference.anchor),
"position": new index$1.PositionProperty(lightReference.position),
"color": new index$1.DataConstantProperty(lightReference.color),
"intensity": new index$1.DataConstantProperty(lightReference.intensity)
}));
class Light extends index$1.Evented {
constructor(lightOptions, id = "flat") {
super();
this._transitionable = new index$1.Transitionable(getProperties$4());
this.setLight(lightOptions, id);
this._transitioning = this._transitionable.untransitioned();
}
getLight() {
return this._transitionable.serialize();
}
setLight(light, id, options = {}) {
if (this._validate(validateLight, light, options)) {
return;
}
this._transitionable.setTransitionOrValue(light);
this.id = id;
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
_validate(validate, value, options) {
if (options && options.validate === false) {
return false;
}
return emitValidationErrors$1(this, validate.call(validateStyle, Object.assign({
value,
// Workaround for https://github.com/mapbox/mapbox-gl-js/issues/2407
style: { glyphs: true, sprite: true },
styleSpec: index$1.spec
})));
}
}
const terrainSpecification = index$1.spec.terrain;
const DrapeRenderMode = {
deferred: 0,
elevated: 1
};
let Terrain$1 = class Terrain extends index$1.Evented {
constructor(terrainOptions, drapeRenderMode, scope, configOptions, worldview) {
super();
this.scope = scope;
this._transitionable = new index$1.Transitionable(new index$1.Properties({
"source": new index$1.DataConstantProperty(terrainSpecification.source),
"exaggeration": new index$1.DataConstantProperty(terrainSpecification.exaggeration)
}), scope, configOptions);
this._transitionable.setTransitionOrValue(terrainOptions, configOptions);
this._transitioning = this._transitionable.untransitioned();
this.drapeRenderMode = drapeRenderMode;
this.worldview = worldview;
}
get() {
return this._transitionable.serialize();
}
set(terrain, configOptions) {
this._transitionable.setTransitionOrValue(terrain, configOptions);
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
getExaggeration(atZoom) {
return this._transitioning.possiblyEvaluate(new index$1.EvaluationParameters(atZoom, { worldview: this.worldview })).get("exaggeration");
}
// For dynamic terrain, this is the zoom range when the terrain flattening (disabling) starts
// and ends. At zoom above attenuationRange->max, terrain exaggeration is evaluated to 0.
// The exaggeration used in terrain is smoothened from this value, and as terrain gets disabled
// using smoothened curve, it effectivelly gets disabled at some value above attenuationRange->max
// but that value is capped at ceil(attenuationRange->max).
getAttenuationRange() {
if (!this.isZoomDependent()) {
return null;
}
const exaggeration = this._transitionable._values["exaggeration"];
if (!exaggeration) {
return null;
}
const expression = exaggeration.value.expression;
if (!expression) {
return null;
}
let zoomBeforeDrop = -1;
let fullyDisabledZoom = -1;
let theLastExaggeration = 1;
const zeroExaggerationCutoff = 0.01;
for (const zoom of expression.zoomStops) {
theLastExaggeration = expression.evaluate(new index$1.EvaluationParameters(zoom, { worldview: this.worldview }));
if (theLastExaggeration > zeroExaggerationCutoff) {
zoomBeforeDrop = zoom;
fullyDisabledZoom = -1;
} else {
fullyDisabledZoom = zoom;
}
}
if (theLastExaggeration < zeroExaggerationCutoff && zoomBeforeDrop > 0 && fullyDisabledZoom > zoomBeforeDrop) {
return [zoomBeforeDrop, fullyDisabledZoom];
}
return null;
}
isZoomDependent() {
const exaggeration = this._transitionable._values["exaggeration"];
return exaggeration != null && exaggeration.value != null && exaggeration.value.expression != null && exaggeration.value.expression instanceof index$1.ZoomDependentExpression;
}
};
const FOG_PITCH_START = 45;
const FOG_PITCH_END = 65;
const FOG_SYMBOL_CLIPPING_THRESHOLD = 0.9;
const FOG_OPACITY_THRESHOLD = 0.05;
function getFogOpacity(state, depth, pitch, fov) {
const fogPitchOpacity = index$1.smoothstep(FOG_PITCH_START, FOG_PITCH_END, pitch);
const [start, end] = getFovAdjustedFogRange(state, fov);
const decay = 6;
const fogRange = (depth - start) / (end - start);
let falloff = 1 - Math.min(1, Math.exp(-decay * fogRange));
falloff *= falloff * falloff;
falloff = Math.min(1, 1.00747 * falloff);
return falloff * fogPitchOpacity * state.alpha;
}
function getFovAdjustedFogRange(state, fov) {
const shift = 0.5 / Math.tan(fov * 0.5);
return [state.range[0] + shift, state.range[1] + shift];
}
function getFogOpacityAtTileCoord(state, x, y, z, tileId, transform) {
const mat = transform.calculateFogTileMatrix(tileId);
const pos = [x, y, z];
index$1.transformMat4(pos, pos, mat);
return getFogOpacity(state, index$1.length(pos), transform.pitch, transform._fov);
}
function getFogOpacityAtLngLat(state, lngLat, transform) {
const meters = index$1.MercatorCoordinate.fromLngLat(lngLat);
const elevation = transform.elevation ? transform.elevation.getAtPointOrZero(meters) : 0;
return getFogOpacityAtMercCoord(state, meters.x, meters.y, elevation, transform);
}
function getFogOpacityAtMercCoord(state, x, y, elevation, transform) {
const pos = index$1.transformMat4([], [x, y, elevation], transform.mercatorFogMatrix);
return getFogOpacity(state, index$1.length(pos), transform.pitch, transform._fov);
}
function getFogOpacityForBounds(state, matrix, x0, y0, x1, y1, transform) {
const points = [
[x0, y0, 0],
[x1, y0, 0],
[x1, y1, 0],
[x0, y1, 0]
];
let min = Number.MAX_VALUE;
let max = -Number.MAX_VALUE;
for (const point of points) {
const transformedPoint = index$1.transformMat4([], point, matrix);
const distance = index$1.length(transformedPoint);
min = Math.min(min, distance);
max = Math.max(max, distance);
}
return [getFogOpacity(state, min, transform.pitch, transform._fov), getFogOpacity(state, max, transform.pitch, transform._fov)];
}
const fogReference = index$1.spec.fog;
class Fog extends index$1.Evented {
constructor(fogOptions, transform, scope, configOptions) {
super();
const fogProperties = new index$1.Properties({
"range": new index$1.DataConstantProperty(fogReference.range),
"color": new index$1.DataConstantProperty(fogReference.color),
"color-use-theme": new index$1.DataConstantProperty({ "type": "string", "property-type": "data-constant", "default": "default" }),
"high-color": new index$1.DataConstantProperty(fogReference["high-color"]),
"high-color-use-theme": new index$1.DataConstantProperty({ "type": "string", "property-type": "data-constant", "default": "default" }),
"space-color": new index$1.DataConstantProperty(fogReference["space-color"]),
"space-color-use-theme": new index$1.DataConstantProperty({ "type": "string", "property-type": "data-constant", "default": "default" }),
"horizon-blend": new index$1.DataConstantProperty(fogReference["horizon-blend"]),
"star-intensity": new index$1.DataConstantProperty(fogReference["star-intensity"]),
"vertical-range": new index$1.DataConstantProperty(fogReference["vertical-range"])
});
this._transitionable = new index$1.Transitionable(fogProperties, scope, new Map(configOptions));
this.set(fogOptions, configOptions);
this._transitioning = this._transitionable.untransitioned();
this._transform = transform;
this.properties = new index$1.PossiblyEvaluated(fogProperties);
this.scope = scope;
}
get state() {
const tr = this._transform;
const isGlobe = tr.projection.name === "globe";
const transitionT = index$1.globeToMercatorTransition(tr.zoom);
const range = this.properties.get("range");
const globeFixedFogRange = [0.5, 3];
return {
range: isGlobe ? [
index$1.number(globeFixedFogRange[0], range[0], transitionT),
index$1.number(globeFixedFogRange[1], range[1], transitionT)
] : range,
horizonBlend: this.properties.get("horizon-blend"),
alpha: this.properties.get("color").a
};
}
get() {
return this._transitionable.serialize();
}
set(fog, configOptions, options = {}) {
if (this._validate(validateFog, fog, options)) {
return;
}
const properties = Object.assign({}, fog);
for (const name of Object.keys(fogReference)) {
if (properties[name] === void 0) {
properties[name] = fogReference[name].default;
}
}
this._options = properties;
this._transitionable.setTransitionOrValue(this._options, configOptions);
}
getOpacity(pitch) {
if (!this._transform.projection.supportsFog) return 0;
const fogColor = this.properties && this.properties.get("color") || 1;
const isGlobe = this._transform.projection.name === "globe";
const pitchFactor = isGlobe ? 1 : index$1.smoothstep(FOG_PITCH_START, FOG_PITCH_END, pitch);
return pitchFactor * fogColor.a;
}
getOpacityAtLatLng(lngLat, transform) {
if (!this._transform.projection.supportsFog) return 0;
return getFogOpacityAtLngLat(this.state, lngLat, transform);
}
getOpacityForTile(id) {
if (!this._transform.projection.supportsFog) return [1, 1];
const fogMatrix = this._transform.calculateFogTileMatrix(id.toUnwrapped());
return getFogOpacityForBounds(this.state, fogMatrix, 0, 0, index$1.EXTENT, index$1.EXTENT, this._transform);
}
getOpacityForBounds(matrix, x0, y0, x1, y1) {
if (!this._transform.projection.supportsFog) return [1, 1];
return getFogOpacityForBounds(this.state, matrix, x0, y0, x1, y1, this._transform);
}
getFovAdjustedRange(fov) {
if (!this._transform.projection.supportsFog) return [0, 1];
return getFovAdjustedFogRange(this.state, fov);
}
isVisibleOnFrustum(frustum) {
if (!this._transform.projection.supportsFog) return false;
const farPoints = [4, 5, 6, 7];
for (const pointIdx of farPoints) {
const farPoint = frustum.points[pointIdx];
let flatPoint;
if (farPoint[2] >= 0) {
flatPoint = farPoint;
} else {
const nearPoint = frustum.points[pointIdx - 4];
flatPoint = index$1.array(nearPoint, farPoint, nearPoint[2] / (nearPoint[2] - farPoint[2]));
}
if (getFogOpacityAtMercCoord(this.state, flatPoint[0], flatPoint[1], 0, this._transform) >= FOG_OPACITY_THRESHOLD) {
return true;
}
}
return false;
}
updateConfig(configOptions) {
this._transitionable.setTransitionOrValue(this._options, new Map(configOptions));
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
_validate(validate, value, options) {
if (options && options.validate === false) {
return false;
}
return emitValidationErrors$1(this, validate.call(validateStyle, Object.assign({
value,
style: { glyphs: true, sprite: true },
styleSpec: index$1.spec
})));
}
}
let properties$3;
const getProperties$3 = () => properties$3 || (properties$3 = new index$1.Properties({
"density": new index$1.DataConstantProperty(index$1.spec["snow"]["density"]),
"intensity": new index$1.DataConstantProperty(index$1.spec["snow"]["intensity"]),
"color": new index$1.DataConstantProperty(index$1.spec["snow"]["color"]),
"opacity": new index$1.DataConstantProperty(index$1.spec["snow"]["opacity"]),
"vignette": new index$1.DataConstantProperty(index$1.spec["snow"]["vignette"]),
"vignette-color": new index$1.DataConstantProperty(index$1.spec["snow"]["vignette-color"]),
"center-thinning": new index$1.DataConstantProperty(index$1.spec["snow"]["center-thinning"]),
"direction": new index$1.DataConstantProperty(index$1.spec["snow"]["direction"]),
"flake-size": new index$1.DataConstantProperty(index$1.spec["snow"]["flake-size"])
}));
let Snow$1 = class Snow extends index$1.Evented {
constructor(snowOptions, transform, scope, configOptions) {
super();
const snowProperties = getProperties$3();
this._transitionable = new index$1.Transitionable(snowProperties, scope, new Map(configOptions));
this.set(snowOptions, configOptions);
this._transitioning = this._transitionable.untransitioned();
this.properties = new index$1.PossiblyEvaluated(snowProperties);
this.scope = scope;
}
get state() {
const opacity = this.properties.get("opacity");
const color = this.properties.get("color");
const directionAngles = this.properties.get("direction");
const heading = index$1.degToRad(directionAngles[0]);
const pitch = -Math.max(index$1.degToRad(directionAngles[1]), 0.01);
const direction = [Math.cos(heading) * Math.cos(pitch), Math.sin(heading) * Math.cos(pitch), Math.sin(pitch)];
const vignetteIntensity = this.properties.get("vignette");
const vignetteColor = this.properties.get("vignette-color");
vignetteColor.a = vignetteIntensity;
return {
density: this.properties.get("density"),
intensity: this.properties.get("intensity"),
color: new index$1.Color(color.r, color.g, color.b, color.a * opacity),
direction,
centerThinning: this.properties.get("center-thinning"),
flakeSize: this.properties.get("flake-size"),
vignetteColor
};
}
get() {
return this._transitionable.serialize();
}
set(snow, configOptions, options = {}) {
if (this._validate(validateSnow, snow, options)) {
return;
}
const properties = Object.assign({}, snow);
const snowSpec = index$1.spec.snow;
for (const name of Object.keys(snowSpec)) {
if (properties[name] === void 0) {
properties[name] = snowSpec[name].default;
}
}
this._options = properties;
this._transitionable.setTransitionOrValue(this._options, configOptions);
}
updateConfig(configOptions) {
this._transitionable.setTransitionOrValue(this._options, new Map(configOptions));
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
_validate(validate, value, options) {
if (options && options.validate === false) {
return false;
}
return emitValidationErrors$1(this, validate.call(validateStyle, Object.assign({
value,
style: { glyphs: true, sprite: true },
styleSpec: index$1.spec
})));
}
};
let properties$2;
const getProperties$2 = () => properties$2 || (properties$2 = new index$1.Properties({
"density": new index$1.DataConstantProperty(index$1.spec["rain"]["density"]),
"intensity": new index$1.DataConstantProperty(index$1.spec["rain"]["intensity"]),
"color": new index$1.DataConstantProperty(index$1.spec["rain"]["color"]),
"opacity": new index$1.DataConstantProperty(index$1.spec["rain"]["opacity"]),
"vignette": new index$1.DataConstantProperty(index$1.spec["rain"]["vignette"]),
"vignette-color": new index$1.DataConstantProperty(index$1.spec["rain"]["vignette-color"]),
"center-thinning": new index$1.DataConstantProperty(index$1.spec["rain"]["center-thinning"]),
"direction": new index$1.DataConstantProperty(index$1.spec["rain"]["direction"]),
"droplet-size": new index$1.DataConstantProperty(index$1.spec["rain"]["droplet-size"]),
"distortion-strength": new index$1.DataConstantProperty(index$1.spec["rain"]["distortion-strength"])
}));
let Rain$1 = class Rain extends index$1.Evented {
constructor(rainOptions, transform, scope, configOptions) {
super();
const rainProperties = getProperties$2();
this._transitionable = new index$1.Transitionable(rainProperties, scope, new Map(configOptions));
this.set(rainOptions, configOptions);
this._transitioning = this._transitionable.untransitioned();
this.properties = new index$1.PossiblyEvaluated(rainProperties);
this.scope = scope;
}
get state() {
const opacity = this.properties.get("opacity");
const color = this.properties.get("color");
const directionAngles = this.properties.get("direction");
const heading = index$1.degToRad(directionAngles[0]);
const pitch = -Math.max(index$1.degToRad(directionAngles[1]), 0.01);
const direction = [Math.cos(heading) * Math.cos(pitch), Math.sin(heading) * Math.cos(pitch), Math.sin(pitch)];
const vignetteColor = this.properties.get("vignette-color");
vignetteColor.a = this.properties.get("vignette");
return {
density: this.properties.get("density"),
intensity: this.properties.get("intensity"),
color: new index$1.Color(color.r, color.g, color.b, color.a * opacity),
direction,
centerThinning: this.properties.get("center-thinning"),
dropletSize: this.properties.get("droplet-size"),
distortionStrength: this.properties.get("distortion-strength"),
vignetteColor
};
}
get() {
return this._transitionable.serialize();
}
set(rain, configOptions, options = {}) {
if (this._validate(validateRain, rain, options)) {
return;
}
const properties = Object.assign({}, rain);
const rainSpec = index$1.spec.rain;
for (const name of Object.keys(rainSpec)) {
if (properties[name] === void 0) {
properties[name] = rainSpec[name].default;
}
}
this._options = properties;
this._transitionable.setTransitionOrValue(this._options, configOptions);
}
updateConfig(configOptions) {
this._transitionable.setTransitionOrValue(this._options, new Map(configOptions));
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
_validate(validate, value, options) {
if (options && options.validate === false) {
return false;
}
return emitValidationErrors$1(this, validate.call(validateStyle, Object.assign({
value,
style: { glyphs: true, sprite: true },
styleSpec: index$1.spec
})));
}
};
class Lights extends index$1.Evented {
constructor(options, properties, scope, configOptions) {
super();
this.scope = scope;
this._options = options;
this.properties = new index$1.PossiblyEvaluated(properties);
this._transitionable = new index$1.Transitionable(properties, scope, new Map(configOptions));
this._transitionable.setTransitionOrValue(options.properties);
this._transitioning = this._transitionable.untransitioned();
}
updateConfig(configOptions) {
this._transitionable.setTransitionOrValue(this._options.properties, new Map(configOptions));
}
updateTransitions(parameters) {
this._transitioning = this._transitionable.transitioned(parameters, this._transitioning);
}
hasTransition() {
return this._transitioning.hasTransition();
}
recalculate(parameters) {
this.properties = this._transitioning.possiblyEvaluate(parameters);
}
get() {
this._options.properties = this._transitionable.serialize();
return this._options;
}
set(options, configOptions) {
this._options = options;
this._transitionable.setTransitionOrValue(options.properties, configOptions);
}
shadowsEnabled() {
if (!this.properties) return false;
return this.properties.get("cast-shadows") === true;
}
}
let properties$1;
const getProperties$1 = () => properties$1 || (properties$1 = new index$1.Properties({
"color": new index$1.DataConstantProperty(index$1.spec["properties_light_ambient"]["color"]),
"color-use-theme": new index$1.DataConstantProperty({ "type": "string", "default": "default", "property-type": "data-constant" }),
"intensity": new index$1.DataConstantProperty(index$1.spec["properties_light_ambient"]["intensity"])
}));
let properties;
const getProperties = () => properties || (properties = new index$1.Properties({
"direction": new index$1.DirectionProperty(index$1.spec["properties_light_directional"]["direction"]),
"color": new index$1.DataConstantProperty(index$1.spec["properties_light_directional"]["color"]),
"color-use-theme": new index$1.DataConstantProperty({ "type": "string", "default": "default", "property-type": "data-constant" }),
"intensity": new index$1.DataConstantProperty(index$1.spec["properties_light_directional"]["intensity"]),
"cast-shadows": new index$1.DataConstantProperty(index$1.spec["properties_light_directional"]["cast-shadows"]),
"shadow-quality": new index$1.DataConstantProperty(index$1.spec["properties_light_directional"]["shadow-quality"]),
"shadow-intensity": new index$1.DataConstantProperty(index$1.spec["properties_light_directional"]["shadow-intensity"])
}));
class QueryGeometry {
constructor(screenBounds, aboveHorizon, transform) {
this.screenBounds = screenBounds;
this.cameraPoint = transform.getCameraPoint();
this._screenRaycastCache = {};
this._cameraRaycastCache = {};
this.isAboveHorizon = aboveHorizon;
this.screenGeometry = this.bufferedScreenGeometry(0);
this.screenGeometryMercator = this._bufferedScreenMercator(0, transform);
}
/**
* Factory method to help contruct an instance while accounting for current map state.
*
* @static
* @param {(PointLike | [PointLike, PointLike])} geometry The query geometry.
* @param {Transform} transform The current map transform.
* @returns {QueryGeometry} An instance of the QueryGeometry class.
*/
static createFromScreenPoints(geometry, transform) {
let screenGeometry;
let aboveHorizon;
if (geometry instanceof index$1.Point || typeof geometry[0] === "number") {
const pt = index$1.Point.convert(geometry);
screenGeometry = [pt];
aboveHorizon = transform.isPointAboveHorizon(pt);
} else {
const tl = index$1.Point.convert(geometry[0]);
const br = index$1.Point.convert(geometry[1]);
const center = tl.add(br)._div(2);
screenGeometry = [tl, br];
aboveHorizon = index$1.polygonizeBounds(tl, br).every((p) => transform.isPointAboveHorizon(p)) && transform.isPointAboveHorizon(center);
}
return new QueryGeometry(screenGeometry, aboveHorizon, transform);
}
/**
* Returns true if the initial query by the user was a single point.
*
* @returns {boolean} Returns `true` if the initial query geometry was a single point.
*/
isPointQuery() {
return this.screenBounds.length === 1;
}
/**
* Due to data-driven styling features do not uniform size(eg `circle-radius`) and can be offset differntly
* from their original location(for example with `*-translate`). This means we have to expand our query region for
* each tile to account for variation in these properties.
* Each tile calculates a tile level max padding value (in screenspace pixels) when its parsed, this function
* lets us calculate a buffered version of the screenspace query geometry for each tile.
*
* @param {number} buffer The tile padding in screenspace pixels.
* @returns {Point[]} The buffered query geometry.
*/
bufferedScreenGeometry(buffer) {
return index$1.polygonizeBounds(
this.screenBounds[0],
this.screenBounds.length === 1 ? this.screenBounds[0] : this.screenBounds[1],
buffer
);
}
/**
* When the map is pitched, some of the 3D features that intersect a query will not intersect
* the query at the surface of the earth. Instead the feature may be closer and only intersect
* the query because it extrudes into the air.
*
* This returns a geometry that is a convex polygon that encompasses the query frustum and the point underneath the camera.
* Similar to `bufferedScreenGeometry`, buffering is added to account for variation in paint properties.
*
* Case 1: point underneath camera is exactly behind query volume
* +----------+
* | |
* | |
* | |
* + +
* X X
* X X
* X X
* X X
* XX.
*
* Case 2: point is behind and to the right
* +----------+
* | X
* | X
* | XX
* + X
* XXX XX
* XXXX X
* XXX XX
* XX X
* XXX.
*
* Case 3: point is behind and to the left
* +----------+
* X |
* X |
* XX |
* X +
* X XXXX
* XX XXX
* X XXXX
* X XXXX
* XXX.
*
* @param {number} buffer The tile padding in screenspace pixels.
* @returns {Point[]} The buffered query geometry.
*/
bufferedCameraGeometry(buffer) {
const min = this.screenBounds[0];
const max = this.screenBounds.length === 1 ? this.screenBounds[0].add(new index$1.Point(1, 1)) : this.screenBounds[1];
const cameraPolygon = index$1.polygonizeBounds(min, max, 0, false);
if (this.cameraPoint.y > max.y) {
if (this.cameraPoint.x > min.x && this.cameraPoint.x < max.x) {
cameraPolygon.splice(3, 0, this.cameraPoint);
} else if (this.cameraPoint.x >= max.x) {
cameraPolygon[2] = this.cameraPoint;
} else if (this.cameraPoint.x <= min.x) {
cameraPolygon[3] = this.cameraPoint;
}
}
return index$1.bufferConvexPolygon(cameraPolygon, buffer);
}
// Creates a convex polygon in screen coordinates that encompasses the query frustum and
// the camera location at globe's surface. Camera point can be at any side of the query polygon as
// opposed to `bufferedCameraGeometry` which restricts the location to underneath the polygon.
bufferedCameraGeometryGlobe(buffer) {
const min = this.screenBounds[0];
const max = this.screenBounds.length === 1 ? this.screenBounds[0].add(new index$1.Point(1, 1)) : this.screenBounds[1];
const cameraPolygon = index$1.polygonizeBounds(min, max, buffer);
const camPos = this.cameraPoint.clone();
const column = (camPos.x > min.x) + (camPos.x > max.x);
const row = (camPos.y > min.y) + (camPos.y > max.y);
const sector = row * 3 + column;
switch (sector) {
case 0:
cameraPolygon[0] = camPos;
cameraPolygon[4] = camPos.clone();
break;
case 1:
cameraPolygon.splice(1, 0, camPos);
break;
case 2:
cameraPolygon[1] = camPos;
break;
case 3:
cameraPolygon.splice(4, 0, camPos);
break;
case 5:
cameraPolygon.splice(2, 0, camPos);
break;
case 6:
cameraPolygon[3] = camPos;
break;
case 7:
cameraPolygon.splice(3, 0, camPos);
break;
case 8:
cameraPolygon[2] = camPos;
break;
}
return cameraPolygon;
}
/**
* Checks if a tile is contained within this query geometry.
*
* @param {Tile} tile The tile to check.
* @param {Transform} transform The current map transform.
* @param {boolean} use3D A boolean indicating whether to query 3D features.
* @param {number} cameraWrap A wrap value for offsetting the camera position.
* @returns {?TilespaceQueryGeometry} Returns `undefined` if the tile does not intersect.
*/
containsTile(tile, transform, use3D, cameraWrap = 0) {
const bias = 1;
const padding = Math.max(tile.queryPadding, tile.evaluateQueryRenderedFeaturePadding()) / transform._pixelsPerMercatorPixel + bias;
const cachedQuery = use3D ? this._bufferedCameraMercator(padding, transform) : this._bufferedScreenMercator(padding, transform);
let wrap2 = tile.tileID.wrap + (cachedQuery.unwrapped ? cameraWrap : 0);
const geometryForTileCheck = cachedQuery.polygon.map((p) => index$1.getTilePoint(tile.tileTransform, p, wrap2));
if (!index$1.polygonIntersectsBox(geometryForTileCheck, 0, 0, index$1.EXTENT, index$1.EXTENT)) {
return void 0;
}
wrap2 = tile.tileID.wrap + (this.screenGeometryMercator.unwrapped ? cameraWrap : 0);
const tilespaceVec3s = this.screenGeometryMercator.polygon.map((p) => index$1.getTileVec3(tile.tileTransform, p, wrap2));
const tilespaceGeometry = tilespaceVec3s.map((v) => new index$1.Point(v[0], v[1]));
const cameraMercator = transform.getFreeCameraOptions().position || new index$1.MercatorCoordinate(0, 0, 0);
const tilespaceCameraPosition = index$1.getTileVec3(tile.tileTransform, cameraMercator, wrap2);
const tilespaceRays = tilespaceVec3s.map((tileVec) => {
const dir = index$1.sub(tileVec, tileVec, tilespaceCameraPosition);
index$1.normalize(dir, dir);
return new index$1.Ray(tilespaceCameraPosition, dir);
});
const pixelToTileUnitsFactor = index$1.pixelsToTileUnits(tile, 1, transform.zoom) * transform._pixelsPerMercatorPixel;
return {
queryGeometry: this,
tilespaceGeometry,
tilespaceRays,
bufferedTilespaceGeometry: geometryForTileCheck,
bufferedTilespaceBounds: clampBoundsToTileExtents(index$1.getBounds(geometryForTileCheck)),
tile,
tileID: tile.tileID,
pixelToTileUnitsFactor
};
}
/**
* These methods add caching on top of the terrain raycasting provided by `Transform#pointCoordinate3d`.
* Tiles come with different values of padding, however its very likely that multiple tiles share the same value of padding
* based on the style. In that case we want to reuse the result from a previously computed terrain raycast.
*/
_bufferedScreenMercator(padding, transform) {
const key = cacheKey(padding);
if (this._screenRaycastCache[key]) {
return this._screenRaycastCache[key];
} else {
let poly;
if (transform.projection.name === "globe") {
poly = this._projectAndResample(this.bufferedScreenGeometry(padding), transform);
} else {
poly = {
polygon: this.bufferedScreenGeometry(padding).map((p) => transform.pointCoordinate3D(p)),
unwrapped: true
};
}
this._screenRaycastCache[key] = poly;
return poly;
}
}
_bufferedCameraMercator(padding, transform) {
const key = cacheKey(padding);
if (this._cameraRaycastCache[key]) {
return this._cameraRaycastCache[key];
} else {
let poly;
if (transform.projection.name === "globe") {
poly = this._projectAndResample(this.bufferedCameraGeometryGlobe(padding), transform);
} else {
poly = {
polygon: this.bufferedCameraGeometry(padding).map((p) => transform.pointCoordinate3D(p)),
unwrapped: true
};
}
this._cameraRaycastCache[key] = poly;
return poly;
}
}
_projectAndResample(polygon, transform) {
const polePolygon = projectPolygonCoveringPoles(polygon, transform);
if (polePolygon) {
return polePolygon;
}
const resampled = unwrapQueryPolygon(resamplePolygon(polygon, transform).map((p) => new index$1.Point(wrap(p.x), p.y)), transform);
return {
polygon: resampled.polygon.map((p) => new index$1.MercatorCoordinate(p.x, p.y)),
unwrapped: resampled.unwrapped
};
}
}
function unwrapQueryPolygon(polygon, tr) {
let unwrapped = false;
let maxX = -Infinity;
let startEdge = 0;
for (let e = 0; e < polygon.length - 1; e++) {
if (polygon[e].x > maxX) {
maxX = polygon[e].x;
startEdge = e;
}
}
for (let i = 0; i < polygon.length - 1; i++) {
const edge = (startEdge + i) % (polygon.length - 1);
const a = polygon[edge];
const b = polygon[edge + 1];
if (Math.abs(a.x - b.x) > 0.5) {
if (a.x < b.x) {
a.x += 1;
if (edge === 0) {
polygon[polygon.length - 1].x += 1;
}
} else {
b.x += 1;
if (edge + 1 === polygon.length - 1) {
polygon[0].x += 1;
}
}
unwrapped = true;
}
}
const cameraX = index$1.mercatorXfromLng(tr.center.lng);
if (unwrapped && cameraX < Math.abs(cameraX - 1)) {
polygon.forEach((p) => {
p.x -= 1;
});
}
return {
polygon,
unwrapped
};
}
function projectPolygonCoveringPoles(polygon, tr) {
const matrix = index$1.multiply([], tr.pixelMatrix, tr.globeMatrix);
const northPole = [0, -index$1.GLOBE_RADIUS, 0, 1];
const southPole = [0, index$1.GLOBE_RADIUS, 0, 1];
const center = [0, 0, 0, 1];
index$1.transformMat4$1(northPole, northPole, matrix);
index$1.transformMat4$1(southPole, southPole, matrix);
index$1.transformMat4$1(center, center, matrix);
const screenNp = new index$1.Point(northPole[0] / northPole[3], northPole[1] / northPole[3]);
const screenSp = new index$1.Point(southPole[0] / southPole[3], southPole[1] / southPole[3]);
const containsNp = index$1.polygonContainsPoint(polygon, screenNp) && northPole[3] < center[3];
const containsSp = index$1.polygonContainsPoint(polygon, screenSp) && southPole[3] < center[3];
if (!containsNp && !containsSp) {
return null;
}
const result = findEdgeCrossingAntimeridian(polygon, tr, containsNp ? -1 : 1);
if (!result) {
return null;
}
const { idx, t } = result;
let partA = idx > 1 ? resamplePolygon(polygon.slice(0, idx), tr) : [];
let partB = idx < polygon.length ? resamplePolygon(polygon.slice(idx), tr) : [];
partA = partA.map((p) => new index$1.Point(wrap(p.x), p.y));
partB = partB.map((p) => new index$1.Point(wrap(p.x), p.y));
const resampled = [...partA];
if (resampled.length === 0) {
resampled.push(partB[partB.length - 1]);
}
const a = resampled[resampled.length - 1];
const b = partB.length === 0 ? partA[0] : partB[0];
const intersectionY = index$1.number(a.y, b.y, t);
let mid;
if (containsNp) {
mid = [
new index$1.Point(0, intersectionY),
new index$1.Point(0, 0),
new index$1.Point(1, 0),
new index$1.Point(1, intersectionY)
];
} else {
mid = [
new index$1.Point(1, intersectionY),
new index$1.Point(1, 1),
new index$1.Point(0, 1),
new index$1.Point(0, intersectionY)
];
}
resampled.push(...mid);
if (partB.length === 0) {
resampled.push(partA[0]);
} else {
resampled.push(...partB);
}
return {
polygon: resampled.map((p) => new index$1.MercatorCoordinate(p.x, p.y)),
unwrapped: false
};
}
function resamplePolygon(polygon, transform) {
const tolerance = 1 / 256;
return index$1.resample(
polygon,
(p) => {
const mc = transform.pointCoordinate3D(p);
p.x = mc.x;
p.y = mc.y;
},
tolerance
);
}
function wrap(mercatorX) {
return mercatorX < 0 ? 1 + mercatorX % 1 : mercatorX % 1;
}
function findEdgeCrossingAntimeridian(polygon, tr, direction) {
for (let i = 1; i < polygon.length; i++) {
const a = wrap(tr.pointCoordinate3D(polygon[i - 1]).x);
const b = wrap(tr.pointCoordinate3D(polygon[i]).x);
if (direction < 0) {
if (a < b) {
return { idx: i, t: -a / (b - 1 - a) };
}
} else {
if (b < a) {
return { idx: i, t: (1 - a) / (b + 1 - a) };
}
}
}
return null;
}
function cacheKey(padding) {
return padding * 100 | 0;
}
function clampBoundsToTileExtents(bounds) {
bounds.min.x = index$1.clamp(bounds.min.x, 0, index$1.EXTENT);
bounds.min.y = index$1.clamp(bounds.min.y, 0, index$1.EXTENT);
bounds.max.x = index$1.clamp(bounds.max.x, 0, index$1.EXTENT);
bounds.max.y = index$1.clamp(bounds.max.y, 0, index$1.EXTENT);
return bounds;
}
function getInlinedTileJSON(data, language, worldview) {
if (!data) {
return null;
}
if (!language && !worldview) {
return data;
}
worldview = worldview || data.worldview_default;
const tileJSONLanguages = Object.values(data.language || {});
if (tileJSONLanguages.length === 0) {
return null;
}
const tileJSONWorldviews = Object.values(data.worldview || {});
if (tileJSONWorldviews.length === 0) {
return null;
}
const isLanguageMatched = tileJSONLanguages.every((lang) => lang === language);
const isWorldviewMatched = tileJSONWorldviews.every((vw) => vw === worldview);
if (isLanguageMatched && isWorldviewMatched) {
return data;
}
if (!(language in (data.language_options || {})) && !(worldview in (data.worldview_options || {}))) {
if (!data.language_options || !data.worldview_options) {
return null;
}
return data;
}
return null;
}
function loadTileJSON(options, requestManager, language, worldview, callback) {
const loaded = function(err, tileJSON) {
if (err) {
return callback(err);
} else if (tileJSON) {
if (options.url && tileJSON.tiles && options.tiles) delete options.tiles;
if (tileJSON.variants) {
if (!Array.isArray(tileJSON.variants)) {
return callback(new Error("variants must be an array"));
}
for (const variant of tileJSON.variants) {
if (variant == null || typeof variant !== "object" || variant.constructor !== Object) {
return callback(new Error("variant must be an object"));
}
if (!Array.isArray(variant.capabilities)) {
return callback(new Error("capabilities must be an array"));
}
if (variant.capabilities.length === 1 && variant.capabilities[0] === "meshopt") {
tileJSON = Object.assign(tileJSON, variant);
break;
}
}
}
const result = index$1.pick(
// explicit source options take precedence over TileJSON
Object.assign({}, tileJSON, options),
["tilejson", "tiles", "minzoom", "maxzoom", "attribution", "mapbox_logo", "bounds", "extra_bounds", "scheme", "tileSize", "encoding", "vector_layers", "raster_layers", "worldview_options", "worldview_default", "worldview"]
);
result.tiles = requestManager.canonicalizeTileset(result, options.url);
callback(null, result);
}
};
const inlinedTileJSON = getInlinedTileJSON(options.data, language, worldview);
if (inlinedTileJSON) {
return index$1.exported$1.frame(() => loaded(null, inlinedTileJSON));
}
if (options.url) {
return index$1.getJSON(requestManager.transformRequest(requestManager.normalizeSourceURL(options.url, null, language, worldview), index$1.ResourceType.Source), loaded);
} else {
return index$1.exported$1.frame(() => {
const { data, ...tileJSON } = options;
loaded(null, tileJSON);
});
}
}
function contains(bounds, tileID) {
const worldSize = Math.pow(2, tileID.z);
const minX = Math.floor(index$1.mercatorXfromLng(bounds.getWest()) * worldSize);
const minY = Math.floor(index$1.mercatorYfromLat(bounds.getNorth()) * worldSize);
const maxX = Math.ceil(index$1.mercatorXfromLng(bounds.getEast()) * worldSize);
const maxY = Math.ceil(index$1.mercatorYfromLat(bounds.getSouth()) * worldSize);
const hit = tileID.x >= minX && tileID.x < maxX && tileID.y >= minY && tileID.y < maxY;
return hit;
}
class TileBounds {
constructor(bounds, minzoom, maxzoom) {
this.bounds = bounds ? index$1.LngLatBounds.convert(this.validateBounds(bounds)) : null;
this.minzoom = minzoom || 0;
this.maxzoom = maxzoom || 24;
}
// left, bottom, right, top
validateBounds(bounds) {
if (!Array.isArray(bounds) || bounds.length !== 4) return [-180, -90, 180, 90];
return [Math.max(-180, bounds[0]), Math.max(-90, bounds[1]), Math.min(180, bounds[2]), Math.min(90, bounds[3])];
}
addExtraBounds(extraBounds) {
if (!extraBounds) return;
if (!this.extraBounds) this.extraBounds = [];
for (const bounds of extraBounds) {
this.extraBounds.push(index$1.LngLatBounds.convert(this.validateBounds(bounds)));
}
}
contains(tileID) {
if (tileID.z > this.maxzoom || tileID.z < this.minzoom) {
return false;
}
if (this.bounds && !contains(this.bounds, tileID)) {
return false;
}
if (!this.extraBounds) {
return true;
}
for (const bounds of this.extraBounds) {
if (contains(bounds, tileID)) {
return true;
}
}
return false;
}
static fromTileJSON(tileJSON) {
if (!tileJSON.bounds && !tileJSON.extra_bounds) return null;
const tileBounds = new TileBounds(tileJSON.bounds, tileJSON.minzoom, tileJSON.maxzoom);
tileBounds.addExtraBounds(tileJSON.extra_bounds);
return tileBounds;
}
}
class VectorTileSource extends index$1.Evented {
constructor(id, options, dispatcher, eventedParent) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.type = "vector";
this.minzoom = 0;
this.maxzoom = 22;
this.scheme = "xyz";
this.tileSize = 512;
this.reparseOverscaled = true;
this.isTileClipped = true;
this._loaded = false;
Object.assign(this, index$1.pick(options, ["url", "scheme", "tileSize", "promoteId"]));
this._options = Object.assign({ type: "vector" }, options);
this._collectResourceTiming = !!options.collectResourceTiming;
if (this.tileSize !== 512) {
throw new Error("vector tile sources must have a tileSize of 512");
}
this.setEventedParent(eventedParent);
this._tileWorkers = {};
this._deduped = new index$1.DedupedRequest();
}
load(callback) {
this._loaded = false;
this.fire(new index$1.Event("dataloading", { dataType: "source" }));
const language = Array.isArray(this.map._language) ? this.map._language.join() : this.map._language;
const worldview = this.map.getWorldview();
this._tileJSONRequest = loadTileJSON(this._options, this.map._requestManager, language, worldview, (err, tileJSON) => {
this._tileJSONRequest = null;
this._loaded = true;
if (err) {
if (language) console.warn(`Ensure that your requested language string is a valid BCP-47 code or list of codes. Found: ${language}`);
if (worldview) console.warn(`Requested worldview strings must be a valid ISO alpha-2 code. Found: ${worldview}`);
this.fire(new index$1.ErrorEvent(err));
} else if (tileJSON) {
Object.assign(this, tileJSON);
this.hasWorldviews = !!tileJSON.worldview_options;
if (tileJSON.worldview_default) {
this.worldviewDefault = tileJSON.worldview_default;
}
if (tileJSON.vector_layers) {
this.vectorLayers = tileJSON.vector_layers;
this.vectorLayerIds = [];
this.localizableLayerIds = /* @__PURE__ */ new Set();
for (const layer of tileJSON.vector_layers) {
this.vectorLayerIds.push(layer.id);
if (tileJSON.worldview && tileJSON.worldview[layer.source]) {
this.localizableLayerIds.add(layer.id);
}
}
}
this.tileBounds = TileBounds.fromTileJSON(tileJSON);
postTurnstileEvent(tileJSON.tiles, this.map._requestManager._customAccessToken);
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "metadata" }));
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "content" }));
}
if (callback) callback(err);
});
}
loaded() {
return this._loaded;
}
hasTile(tileID) {
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
onAdd(map) {
this.map = map;
this.load();
}
/**
* Reloads the source data and re-renders the map.
*
* @example
* map.getSource('source-id').reload();
*/
reload() {
this.cancelTileJSONRequest();
const fqid = index$1.makeFQID(this.id, this.scope);
this.load(() => this.map.style.clearSource(fqid));
}
/**
* Sets the source `tiles` property and re-renders the map.
*
* @param {string[]} tiles An array of one or more tile source URLs, as in the TileJSON spec.
* @returns {VectorTileSource} Returns itself to allow for method chaining.
* @example
* map.addSource('source-id', {
* type: 'vector',
* tiles: ['https://some_end_point.net/{z}/{x}/{y}.mvt'],
* minzoom: 6,
* maxzoom: 14
* });
*
* // Set the endpoint associated with a vector tile source.
* map.getSource('source-id').setTiles(['https://another_end_point.net/{z}/{x}/{y}.mvt']);
*/
setTiles(tiles) {
this._options.tiles = tiles;
this.reload();
return this;
}
/**
* Sets the source `url` property and re-renders the map.
*
* @param {string} url A URL to a TileJSON resource. Supported protocols are `http:`, `https:`, and `mapbox://`.
* @returns {VectorTileSource} Returns itself to allow for method chaining.
* @example
* map.addSource('source-id', {
* type: 'vector',
* url: 'mapbox://mapbox.mapbox-streets-v7'
* });
*
* // Update vector tile source to a new URL endpoint
* map.getSource('source-id').setUrl("mapbox://mapbox.mapbox-streets-v8");
*/
setUrl(url) {
this.url = url;
this._options.url = url;
this.reload();
return this;
}
onRemove(_) {
this.cancelTileJSONRequest();
}
serialize() {
return Object.assign({}, this._options);
}
loadTile(tile, callback) {
const tileUrl = tile.tileID.canonical.url(this.tiles, this.scheme);
const url = this.map._requestManager.normalizeTileURL(tileUrl);
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile);
const lutForScope = this.map.style ? this.map.style.getLut(this.scope) : null;
const lut = lutForScope ? { image: lutForScope.image.clone() } : null;
const params = {
request,
data: void 0,
uid: tile.uid,
tileID: tile.tileID,
tileZoom: tile.tileZoom,
zoom: tile.tileID.overscaledZ,
maxZoom: this.maxzoom,
lut,
tileSize: this.tileSize * tile.tileID.overscaleFactor(),
type: this.type,
source: this.id,
scope: this.scope,
pixelRatio: index$1.exported$1.devicePixelRatio,
showCollisionBoxes: this.map.showCollisionBoxes,
promoteId: this.promoteId,
isSymbolTile: tile.isSymbolTile,
brightness: this.map.style ? this.map.style.getBrightness() || 0 : 0,
extraShadowCaster: tile.isExtraShadowCaster,
tessellationStep: this.map._tessellationStep,
scaleFactor: this.map.getScaleFactor(),
worldview: this.map.getWorldview() || this.worldviewDefault,
indoor: this.map.indoor ? this.map.indoor.getIndoorTileOptions(this.id, this.scope) : null
};
if (this.hasWorldviews && index$1.isMapboxURL(tileUrl)) {
params.localizableLayerIds = this.localizableLayerIds;
}
params.request.collectResourceTiming = this._collectResourceTiming;
if (!tile.actor || tile.state === "expired") {
tile.actor = this._tileWorkers[url] = this._tileWorkers[url] || this.dispatcher.getActor();
if (!this.dispatcher.ready) {
const cancel = index$1.loadVectorTile.call({ deduped: this._deduped }, params, (err, data) => {
if (err || !data) {
done.call(this, err);
} else {
const expiryData = index$1.getExpiryDataFromHeaders(data.responseHeaders);
params.data = {
rawData: data.rawData.slice(0),
expires: expiryData.expires,
cacheControl: expiryData.cacheControl
};
if (tile.actor) tile.actor.send("loadTile", params, done.bind(this), void 0, true);
}
}, true);
tile.request = { cancel };
} else {
tile.request = tile.actor.send("loadTile", params, done.bind(this), void 0, true);
}
} else if (tile.state === "loading") {
tile.reloadCallback = callback;
} else {
tile.request = tile.actor.send("reloadTile", params, done.bind(this));
}
function done(err, data) {
delete tile.request;
if (tile.aborted)
return callback(null);
if (err && err instanceof index$1.AJAXError && err.status !== 404) {
return callback(err);
}
if (data && data.resourceTiming)
tile.resourceTiming = data.resourceTiming;
if (this.map._refreshExpiredTiles && data) tile.setExpiryData(data);
tile.loadVectorData(data, this.map.painter);
index$1.cacheEntryPossiblyAdded(this.dispatcher);
callback(null, data);
if (tile.reloadCallback) {
this.loadTile(tile, tile.reloadCallback);
tile.reloadCallback = null;
}
}
}
abortTile(tile) {
if (tile.request) {
tile.request.cancel();
delete tile.request;
}
if (tile.actor) {
tile.actor.send("abortTile", { uid: tile.uid, type: this.type, source: this.id, scope: this.scope });
}
}
unloadTile(tile, _) {
if (tile.actor) {
tile.actor.send("removeTile", { uid: tile.uid, type: this.type, source: this.id, scope: this.scope });
}
tile.destroy();
}
hasTransition() {
return false;
}
afterUpdate() {
this._tileWorkers = {};
}
cancelTileJSONRequest() {
if (!this._tileJSONRequest) return;
this._tileJSONRequest.cancel();
this._tileJSONRequest = null;
}
}
class RasterTileSource extends index$1.Evented {
constructor(id, options, dispatcher, eventedParent) {
super();
this.id = id;
this.dispatcher = dispatcher;
this.setEventedParent(eventedParent);
this.type = "raster";
this.minzoom = 0;
this.maxzoom = 22;
this.roundZoom = true;
this.scheme = "xyz";
this.tileSize = 512;
this._loaded = false;
this._options = Object.assign({ type: "raster" }, options);
Object.assign(this, index$1.pick(options, ["url", "scheme", "tileSize"]));
}
load(callback) {
this._loaded = false;
this.fire(new index$1.Event("dataloading", { dataType: "source" }));
const worldview = this.map.getWorldview();
this._tileJSONRequest = loadTileJSON(this._options, this.map._requestManager, null, worldview, (err, tileJSON) => {
this._tileJSONRequest = null;
this._loaded = true;
if (err) {
this.fire(new index$1.ErrorEvent(err));
} else if (tileJSON) {
Object.assign(this, tileJSON);
if (tileJSON.raster_layers) {
this.rasterLayers = tileJSON.raster_layers;
this.rasterLayerIds = this.rasterLayers.map((layer) => layer.id);
}
this.tileBounds = TileBounds.fromTileJSON(tileJSON);
postTurnstileEvent(tileJSON.tiles);
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "metadata" }));
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "content" }));
}
if (callback) callback(err);
});
}
loaded() {
return this._loaded;
}
onAdd(map) {
this.map = map;
this.load();
}
/**
* Reloads the source data and re-renders the map.
*
* @example
* map.getSource('source-id').reload();
*/
reload() {
this.cancelTileJSONRequest();
const fqid = index$1.makeFQID(this.id, this.scope);
this.load(() => this.map.style.clearSource(fqid));
}
/**
* Sets the source `tiles` property and re-renders the map.
*
* @param {string[]} tiles An array of one or more tile source URLs, as in the TileJSON spec.
* @returns {RasterTileSource} Returns itself to allow for method chaining.
* @example
* map.addSource('source-id', {
* type: 'raster',
* tiles: ['https://some_end_point.net/{z}/{x}/{y}.png'],
* tileSize: 256
* });
*
* // Set the endpoint associated with a raster tile source.
* map.getSource('source-id').setTiles(['https://another_end_point.net/{z}/{x}/{y}.png']);
*/
setTiles(tiles) {
this._options.tiles = tiles;
this.reload();
return this;
}
/**
* Sets the source `url` property and re-renders the map.
*
* @param {string} url A URL to a TileJSON resource. Supported protocols are `http:`, `https:`, and `mapbox://`.
* @returns {RasterTileSource} Returns itself to allow for method chaining.
* @example
* map.addSource('source-id', {
* type: 'raster',
* url: 'mapbox://mapbox.satellite'
* });
*
* // Update raster tile source to a new URL endpoint
* map.getSource('source-id').setUrl('mapbox://mapbox.satellite');
*/
setUrl(url) {
this.url = url;
this._options.url = url;
this.reload();
return this;
}
onRemove(_) {
this.cancelTileJSONRequest();
}
serialize() {
return Object.assign({}, this._options);
}
hasTile(tileID) {
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
loadTile(tile, callback) {
const use2x = index$1.exported$1.devicePixelRatio >= 2;
const url = this.map._requestManager.normalizeTileURL(tile.tileID.canonical.url(this.tiles, this.scheme), use2x, this.tileSize);
tile.request = index$1.getImage(this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile), (error, data, responseHeaders) => {
delete tile.request;
if (tile.aborted) {
tile.state = "unloaded";
return callback(null);
}
if (error) {
tile.state = "errored";
return callback(error);
}
if (!data) return callback(null);
const expiryData = index$1.getExpiryDataFromHeaders(responseHeaders);
if (this.map._refreshExpiredTiles) tile.setExpiryData(expiryData);
tile.setTexture(data, this.map.painter);
tile.state = "loaded";
index$1.cacheEntryPossiblyAdded(this.dispatcher);
callback(null);
});
}
abortTile(tile, callback) {
if (tile.request) {
tile.request.cancel();
delete tile.request;
}
if (callback) callback();
}
unloadTile(tile, callback) {
if (tile.texture && tile.texture instanceof index$1.Texture) {
tile.destroy(false);
if (tile.texture && tile.texture instanceof index$1.Texture) {
this.map.painter.saveTileTexture(tile.texture);
}
} else {
tile.destroy();
}
if (callback) callback();
}
hasTransition() {
return false;
}
cancelTileJSONRequest() {
if (!this._tileJSONRequest) return;
this._tileJSONRequest.cancel();
this._tileJSONRequest = null;
}
}
class RasterDEMTileSource extends RasterTileSource {
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
this.type = "raster-dem";
this.maxzoom = 22;
this._options = Object.assign({ type: "raster-dem" }, options);
this.encoding = options.encoding || "mapbox";
}
loadTile(tile, callback) {
const url = this.map._requestManager.normalizeTileURL(tile.tileID.canonical.url(this.tiles, this.scheme), false, this.tileSize);
tile.request = index$1.getImage(this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile), imageLoaded.bind(this));
function imageLoaded(err, img, responseHeaders) {
delete tile.request;
if (tile.aborted) {
tile.state = "unloaded";
callback(null);
} else if (err) {
tile.state = "errored";
callback(err);
} else if (img) {
const expiryData = index$1.getExpiryDataFromHeaders(responseHeaders);
if (this.map._refreshExpiredTiles) tile.setExpiryData(expiryData);
const transfer = ImageBitmap && img instanceof ImageBitmap && index$1.offscreenCanvasSupported();
const buffer = (img.width - index$1.prevPowerOfTwo(img.width)) / 2;
const padding = 1 - buffer;
const borderReady = padding < 1;
if (!borderReady && !tile.neighboringTiles) {
tile.neighboringTiles = this._getNeighboringTiles(tile.tileID);
}
const rawImageData = transfer ? img : index$1.exported$1.getImageData(img, padding);
const params = {
uid: tile.uid,
tileID: tile.tileID,
source: this.id,
type: this.type,
scope: this.scope,
rawImageData,
encoding: this.encoding,
padding
};
if (!tile.actor || tile.state === "expired") {
tile.actor = this.dispatcher.getActor();
tile.actor.send("loadTile", params, done.bind(this), void 0, true);
}
}
}
function done(err, dem) {
if (err) {
tile.state = "errored";
callback(err);
}
if (dem) {
tile.dem = dem;
tile.dem.onDeserialize();
tile.needsHillshadePrepare = true;
tile.needsDEMTextureUpload = true;
tile.state = "loaded";
callback(null);
}
}
}
_getNeighboringTiles(tileID) {
const canonical = tileID.canonical;
const dim = Math.pow(2, canonical.z);
const px = (canonical.x - 1 + dim) % dim;
const pxw = canonical.x === 0 ? tileID.wrap - 1 : tileID.wrap;
const nx = (canonical.x + 1 + dim) % dim;
const nxw = canonical.x + 1 === dim ? tileID.wrap + 1 : tileID.wrap;
const neighboringTiles = {};
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, pxw, canonical.z, px, canonical.y).key] = { backfilled: false };
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, nxw, canonical.z, nx, canonical.y).key] = { backfilled: false };
if (canonical.y > 0) {
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, pxw, canonical.z, px, canonical.y - 1).key] = { backfilled: false };
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, tileID.wrap, canonical.z, canonical.x, canonical.y - 1).key] = { backfilled: false };
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, nxw, canonical.z, nx, canonical.y - 1).key] = { backfilled: false };
}
if (canonical.y + 1 < dim) {
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, pxw, canonical.z, px, canonical.y + 1).key] = { backfilled: false };
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, tileID.wrap, canonical.z, canonical.x, canonical.y + 1).key] = { backfilled: false };
neighboringTiles[new index$1.OverscaledTileID(tileID.overscaledZ, nxw, canonical.z, nx, canonical.y + 1).key] = { backfilled: false };
}
return neighboringTiles;
}
}
const NO_DATA_VALUE = 4294967295;
function getPointXY([x, y], bandView, { scaled = true } = {}) {
if (!bandView) {
throw new Error("bandView is undefined");
}
const { data, tileSize, buffer, offset, scale, dimension } = bandView;
if (x < -buffer || x > tileSize + buffer || y < -buffer || y > tileSize + buffer) {
throw new Error(
`Point (${x}, ${y}) out of bounds for tileSize=${tileSize}, buffer=${buffer}`
);
}
const width = tileSize + 2 * buffer;
const pointIndex = (y + buffer) * width + (x + buffer);
const untypedData = new Uint32Array(data.buffer);
if (untypedData[pointIndex] === NO_DATA_VALUE) {
return null;
}
let output = [];
if (scaled) {
output = [];
} else {
const Ctor = bandView.data.constructor;
output = new Ctor(dimension);
}
for (let i = 0; i < dimension; i++) {
const rawValue = data[dimension * pointIndex + i];
const scaledValue = rawValue * scale + offset;
output[i] = Math.round(1e12 * scaledValue) / 1e12;
}
return output;
}
function getPointLonLat([lon, lat], tile, bandView, { scaled = true } = {}) {
const { tileSize, buffer } = bandView;
const { x, y, z } = tile;
if (!isFinite(x) || !isFinite(y) || !isFinite(z)) {
throw new Error("Invalid MRT header");
}
const z2 = 2 ** z;
const xMerc = z2 * index$1.mercatorXfromLng(lon);
const yMerc = z2 * index$1.mercatorYfromLat(lat);
const pixelX = Math.min(
Math.max(-buffer, Math.floor((xMerc - x) * tileSize)),
tileSize - 1 + buffer
);
const pixelY = Math.min(
Math.max(-buffer, Math.floor((yMerc - y) * tileSize)),
tileSize - 1 + buffer
);
return getPointXY([pixelX, pixelY], bandView, { scaled });
}
class RasterArrayTileSource extends RasterTileSource {
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
this.type = "raster-array";
this.maxzoom = 22;
this.partial = true;
this._loadTilePending = {};
this._loadTileLoaded = {};
this._options = Object.assign({ type: "raster-array" }, options);
}
triggerRepaint(tile) {
const terrain = this.map.painter._terrain;
const sourceCache = this.map.style.getSourceCache(this.id);
if (terrain && terrain.enabled && sourceCache) {
terrain._clearRenderCacheForTile(sourceCache.id, tile.tileID);
}
this.map.triggerRepaint();
}
loadTile(tile, callback) {
const url = this.map._requestManager.normalizeTileURL(tile.tileID.canonical.url(this.tiles, this.scheme), false, this.tileSize);
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile);
const params = {
request,
uid: tile.uid,
tileID: tile.tileID,
type: this.type,
source: this.id,
scope: this.scope,
partial: this.partial
};
tile.source = this.id;
tile.scope = this.scope;
tile.requestParams = request;
if (!tile.actor) tile.actor = this.dispatcher.getActor();
const done = (error, data, responseHeaders) => {
delete tile.request;
if (tile.aborted) {
tile.state = "unloaded";
return callback(null);
}
if (error) {
if (error.name === "AbortError") return;
tile.state = "errored";
return callback(error);
}
if (this.map._refreshExpiredTiles && data) {
const expiryData = index$1.getExpiryDataFromHeaders(responseHeaders);
tile.setExpiryData(expiryData);
}
if (this.partial && tile.state !== "expired") {
tile.state = "empty";
} else if (!this.partial) {
if (!data) return callback(null);
tile.state = "loaded";
tile._isHeaderLoaded = true;
tile._mrt = data;
}
callback(null);
};
if (this.partial) {
tile.request = tile.fetchHeader(void 0, done.bind(this));
} else {
tile.request = tile.actor.send("loadTile", params, done.bind(this), void 0, true);
}
}
abortTile(tile) {
if (tile.request) {
tile.request.cancel();
delete tile.request;
}
if (tile.actor) {
tile.actor.send("abortTile", { uid: tile.uid, type: this.type, source: this.id, scope: this.scope });
}
}
unloadTile(tile, _) {
const textures = tile.texturePerLayer;
tile.flushAllQueues();
if (textures.size) {
tile.destroy(false);
for (const texture of textures.values()) {
this.map.painter.saveTileTexture(texture);
}
} else {
tile.destroy();
}
}
/**
* Prepare RasterArrayTile for the rendering. If tile doesn't have data
* for the requested band, fetch and repaint once it's acquired.
* @private
*/
prepareTile(tile, sourceLayer, layerId, band) {
if (!tile._isHeaderLoaded) return;
if (tile.state !== "empty") tile.state = "reloading";
tile.fetchBandForRender(sourceLayer, layerId, band, (error, data) => {
if (error) {
tile.state = "errored";
this.fire(new index$1.ErrorEvent(error));
this.triggerRepaint(tile);
return;
}
if (data) {
tile._isHeaderLoaded = true;
tile.setTexturePerLayer(layerId, data, this.map.painter);
tile.state = "loaded";
this.triggerRepaint(tile);
}
});
}
/**
* Get the initial band for a source layer.
* @private
*/
getInitialBand(sourceLayer) {
if (!this.rasterLayers) return 0;
const rasterLayer = this.rasterLayers.find(({ id }) => id === sourceLayer);
const fields = rasterLayer && rasterLayer.fields;
const bands = fields && fields.bands && fields.bands;
return bands ? bands[0] : 0;
}
/**
* Get a texture descriptor for a source layer and a band.
* @private
* @param {RasterArrayTile} tile
* @param {RasterStyleLayer} layer
* @param {boolean} fallbackToPrevious If true, return previous texture even if update is needed
* @returns {TextureDescriptor} Texture descriptor with texture if available
*/
getTextureDescriptor(tile, layer, fallbackToPrevious) {
if (!tile) return;
const sourceLayer = layer.sourceLayer || this.rasterLayerIds && this.rasterLayerIds[0];
if (!sourceLayer) return;
let layerBand = null;
if (layer instanceof index$1.RasterStyleLayer) {
layerBand = layer.paint.get("raster-array-band");
} else if (layer instanceof index$1.RasterParticleStyleLayer) {
layerBand = layer.paint.get("raster-particle-array-band");
}
const band = layerBand || this.getInitialBand(sourceLayer);
if (band == null) return;
if (!tile.textureDescriptorPerLayer.get(layer.id)) {
this.prepareTile(tile, sourceLayer, layer.id, band);
return;
}
if (tile.updateNeeded(layer.id, band) && !fallbackToPrevious) return;
const textureDescriptor = tile.textureDescriptorPerLayer.get(layer.id);
return Object.assign({}, textureDescriptor, { texture: tile.texturePerLayer.get(layer.id) });
}
/**
* Creates style images from raster array tiles based on the requested image names.
* Used by `ImageProvider` to resolve pending image requests.
* @private
* @param {RasterArrayTile[]} tiles - Array of loaded raster array tiles to extract data from
* @param {string[]} imageNames - Array of image names in format "layerId/bandId" to extract
* @returns {StyleImageMap} Map of image names to StyleImage objects
*/
getImages(tiles, imageNames) {
const styleImages = /* @__PURE__ */ new Map();
for (const tile of tiles) {
for (const name of imageNames) {
const [layerId, bandId] = name.split("/");
const layer = tile.getLayer(layerId);
if (!layer) continue;
if (!layer.hasBand(bandId) || !layer.hasDataForBand(bandId)) continue;
const { bytes, tileSize, buffer } = layer.getBandView(bandId);
const size = tileSize + 2 * buffer;
const styleImage = {
data: new index$1.RGBAImage({ width: size, height: size }, bytes),
pixelRatio: 2,
sdf: false,
usvg: false,
version: 0
};
styleImages.set(name, styleImage);
}
}
return styleImages;
}
queryRasterArrayValueByBandId(lngLat, tile, params) {
const mrt = tile._mrt;
return new Promise((resolve) => {
const queryResult = {};
const fetchLayerBandsRequests = /* @__PURE__ */ new Set();
for (const [layerName, layer] of Object.entries(mrt.layers)) {
if (params.layerName && layerName !== params.layerName) continue;
const entry = {};
queryResult[layerName] = entry;
for (const { bands } of layer.dataIndex) {
for (const band of bands) {
if (params.bands && !params.bands.includes(band)) continue;
fetchLayerBandsRequests.add(index$1.makeFQID(layerName, band));
tile.fetchBand(layerName, null, band, (err) => {
index$1.exported$1.frame(() => {
if (err) {
entry[band] = null;
} else {
entry[band] = getPointLonLat([lngLat.lng, lngLat.lat], mrt, layer.getBandView(band));
}
fetchLayerBandsRequests.delete(index$1.makeFQID(layerName, band));
if (fetchLayerBandsRequests.size === 0) {
resolve(queryResult);
}
});
}, false);
}
}
}
if (fetchLayerBandsRequests.size === 0) {
resolve(queryResult);
}
});
}
_loadTileForQuery(tile, callback) {
if (this._loadTileLoaded[tile.uid]) {
callback(null, tile._mrt);
return;
}
if (this._loadTilePending[tile.uid]) {
this._loadTilePending[tile.uid].push(callback);
return;
}
this._loadTilePending[tile.uid] = [callback];
const url = this.map._requestManager.normalizeTileURL(tile.tileID.canonical.url(this.tiles, this.scheme), false, this.tileSize);
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile);
const requestParams = {
request,
uid: tile.uid,
tileID: tile.tileID,
type: this.type,
source: this.id,
scope: this.scope,
partial: false
};
tile.actor.send("loadTile", requestParams, (error, data, responseHeaders) => {
if (error) {
this._loadTilePending[tile.uid].forEach((cb) => cb(error, null));
delete this._loadTilePending[tile.uid];
return;
}
if (!data) {
this._loadTilePending[tile.uid].forEach((cb) => cb(null, null));
delete this._loadTilePending[tile.uid];
return;
}
if (this.map._refreshExpiredTiles && data) {
const expiryData = index$1.getExpiryDataFromHeaders(responseHeaders);
tile.setExpiryData(expiryData);
}
tile._mrt = data;
tile._isHeaderLoaded = true;
tile.state = "loaded";
this._loadTilePending[tile.uid].forEach((cb) => cb(null, data));
this._loadTileLoaded[tile.uid] = true;
delete this._loadTilePending[tile.uid];
}, void 0, true);
}
queryRasterArrayValueByAllBands(lngLat, tile, params) {
return new Promise((resolve, reject) => {
this._loadTileForQuery(tile, (error, data) => {
if (error) {
reject(error);
return;
}
if (!data) {
resolve(null);
return;
}
resolve(this.queryRasterArrayValueByBandId(lngLat, tile, params));
});
});
}
queryRasterArrayValue(lngLatLike, params) {
const lngLat = index$1.LngLat.convert(lngLatLike);
const tile = this.findLoadedParent(lngLat);
if (!tile) return Promise.resolve(null);
const mrt = tile._mrt;
if (!mrt) return Promise.resolve(null);
if (params.bands || !this.partial) {
return this.queryRasterArrayValueByBandId(lngLat, tile, params);
} else {
return this.queryRasterArrayValueByAllBands(lngLat, tile, params);
}
}
findLoadedParent(lngLat) {
const point = index$1.MercatorCoordinate.fromLngLat(lngLat, this.map.transform.tileSize);
const z = this.maxzoom + 1;
const tiles = 1 << z;
const wrap = Math.floor(point.x);
const px = point.x - wrap;
const x = Math.floor(px * tiles);
const y = Math.floor(point.y * tiles);
const sourceCache = this.map.style.getSourceCache(this.id);
const tileID = new index$1.OverscaledTileID(z, wrap, z, x, y);
return sourceCache.findLoadedParent(tileID, this.minzoom);
}
}
class GeoJSONSource extends index$1.Evented {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super();
this.id = id;
this.type = "geojson";
this.minzoom = 0;
this.maxzoom = 18;
this.tileSize = 512;
this.isTileClipped = true;
this.reparseOverscaled = true;
this._loaded = false;
this.actor = dispatcher.getActor();
this.setEventedParent(eventedParent);
this._data = options.data;
this._options = Object.assign({}, options);
this._collectResourceTiming = options.collectResourceTiming;
if (options.maxzoom !== void 0) this.maxzoom = options.maxzoom;
if (options.minzoom !== void 0) this.minzoom = options.minzoom;
if (options.type) this.type = options.type;
if (options.attribution) this.attribution = options.attribution;
this.promoteId = options.promoteId;
const scale = index$1.EXTENT / this.tileSize;
this.workerOptions = Object.assign({
source: this.id,
scope: this.scope,
cluster: options.cluster || false,
geojsonVtOptions: {
buffer: (options.buffer !== void 0 ? options.buffer : 128) * scale,
tolerance: (options.tolerance !== void 0 ? options.tolerance : 0.375) * scale,
extent: index$1.EXTENT,
maxZoom: this.maxzoom,
lineMetrics: options.lineMetrics || false,
generateId: options.generateId || false
},
superclusterOptions: {
maxZoom: options.clusterMaxZoom !== void 0 ? options.clusterMaxZoom : this.maxzoom - 1,
minPoints: Math.max(2, options.clusterMinPoints || 2),
extent: index$1.EXTENT,
radius: (options.clusterRadius !== void 0 ? options.clusterRadius : 50) * scale,
log: false,
generateId: options.generateId || false
},
clusterProperties: options.clusterProperties,
filter: options.filter,
dynamic: options.dynamic
}, options.workerOptions);
}
onAdd(map) {
this.map = map;
this.setData(this._data);
}
/**
* Sets the GeoJSON data and re-renders the map.
*
* @param {Object | string} data A GeoJSON data object or a URL to one. The latter is preferable in the case of large GeoJSON files.
* @returns {GeoJSONSource} Returns itself to allow for method chaining.
* @example
* map.addSource('source_id', {
* type: 'geojson',
* data: {
* type: 'FeatureCollection',
* features: []
* }
* });
* const geojsonSource = map.getSource('source_id');
* // Update the data after the GeoJSON source was created
* geojsonSource.setData({
* "type": "FeatureCollection",
* "features": [{
* "type": "Feature",
* "properties": {"name": "Null Island"},
* "geometry": {
* "type": "Point",
* "coordinates": [ 0, 0 ]
* }
* }]
* });
*/
setData(data) {
this._data = data;
this._updateWorkerData();
return this;
}
/**
* Updates the existing GeoJSON data with new features and re-renders the map.
* Can only be used on sources with `dynamic: true` in options.
* Updates features by their IDs:
*
* - If there's a feature with the same ID, overwrite it.
* - If there's a feature with the same ID but the new one's geometry is `null`, remove it
* - If there's no such ID in existing data, add it as a new feature.
*
* @param {Object | string} data A GeoJSON data object or a URL to one.
* @returns {GeoJSONSource} Returns itself to allow for method chaining.
* @example
* // Update the feature with ID=123 in the existing GeoJSON source
* map.getSource('source_id').updateData({
* "type": "FeatureCollection",
* "features": [{
* "id": 123,
* "type": "Feature",
* "properties": {"name": "Null Island"},
* "geometry": {
* "type": "Point",
* "coordinates": [ 0, 0 ]
* }
* }]
* });
*/
updateData(data) {
if (!this._options.dynamic) {
return this.fire(new index$1.ErrorEvent(new Error("Can't call updateData on a GeoJSON source with dynamic set to false.")));
}
if (typeof data !== "string") {
if (data.type === "Feature") {
data = { type: "FeatureCollection", features: [data] };
}
if (data.type !== "FeatureCollection") {
return this.fire(new index$1.ErrorEvent(new Error("Data to update should be a feature or a feature collection.")));
}
}
if (this._coalesce && typeof data !== "string" && typeof this._data !== "string" && this._data.type === "FeatureCollection") {
const featuresById = /* @__PURE__ */ new Map();
for (const feature of this._data.features) featuresById.set(feature.id, feature);
for (const feature of data.features) featuresById.set(feature.id, feature);
this._data.features = [...featuresById.values()];
} else {
this._data = data;
}
this._updateWorkerData(true);
return this;
}
/**
* For clustered sources, fetches the zoom at which the given cluster expands.
*
* @param {number} clusterId The value of the cluster's `cluster_id` property.
* @param {Function} callback A callback to be called when the zoom value is retrieved (`(error, zoom) => { ... }`).
* @returns {GeoJSONSource} Returns itself to allow for method chaining.
* @example
* // Assuming the map has a layer named 'clusters' and a source 'earthquakes'
* // The following creates a camera animation on cluster feature click
* // the clicked layer should be filtered to only include clusters, e.g. `filter: ['has', 'point_count']`
* map.on('click', 'clusters', (e) => {
* const features = map.queryRenderedFeatures(e.point, {
* layers: ['clusters']
* });
*
* const clusterId = features[0].properties.cluster_id;
*
* // Ease the camera to the next cluster expansion
* map.getSource('earthquakes').getClusterExpansionZoom(
* clusterId,
* (err, zoom) => {
* if (!err) {
* map.easeTo({
* center: features[0].geometry.coordinates,
* zoom
* });
* }
* }
* );
* });
*/
getClusterExpansionZoom(clusterId, callback) {
this.actor.send("geojson.getClusterExpansionZoom", { clusterId, source: this.id, scope: this.scope }, callback);
return this;
}
/**
* For clustered sources, fetches the children of the given cluster on the next zoom level (as an array of GeoJSON features).
*
* @param {number} clusterId The value of the cluster's `cluster_id` property.
* @param {Function} callback A callback to be called when the features are retrieved (`(error, features) => { ... }`).
* @returns {GeoJSONSource} Returns itself to allow for method chaining.
* @example
* // Retrieve cluster children on click
* // the clicked layer should be filtered to only include clusters, e.g. `filter: ['has', 'point_count']`
* map.on('click', 'clusters', (e) => {
* const features = map.queryRenderedFeatures(e.point, {
* layers: ['clusters']
* });
*
* const clusterId = features[0].properties.cluster_id;
*
* clusterSource.getClusterChildren(clusterId, (error, features) => {
* if (!error) {
* console.log('Cluster children:', features);
* }
* });
* });
*/
getClusterChildren(clusterId, callback) {
this.actor.send("geojson.getClusterChildren", { clusterId, source: this.id, scope: this.scope }, callback);
return this;
}
/**
* For clustered sources, fetches the original points that belong to the cluster (as an array of GeoJSON features).
*
* @param {number} clusterId The value of the cluster's `cluster_id` property.
* @param {number} limit The maximum number of features to return. Defaults to `10` if a falsy value is given.
* @param {number} offset The number of features to skip (for example, for pagination). Defaults to `0` if a falsy value is given.
* @param {Function} callback A callback to be called when the features are retrieved (`(error, features) => { ... }`).
* @returns {GeoJSONSource} Returns itself to allow for method chaining.
* @example
* // Retrieve cluster leaves on click
* // the clicked layer should be filtered to only include clusters, e.g. `filter: ['has', 'point_count']`
* map.on('click', 'clusters', (e) => {
* const features = map.queryRenderedFeatures(e.point, {
* layers: ['clusters']
* });
*
* const clusterId = features[0].properties.cluster_id;
* const pointCount = features[0].properties.point_count;
* const clusterSource = map.getSource('clusters');
*
* clusterSource.getClusterLeaves(clusterId, pointCount, 0, (error, features) => {
* // Print cluster leaves in the console
* console.log('Cluster leaves:', error, features);
* });
* });
*/
getClusterLeaves(clusterId, limit, offset, callback) {
this.actor.send("geojson.getClusterLeaves", {
source: this.id,
scope: this.scope,
clusterId,
limit,
offset
}, callback);
return this;
}
/*
* Responsible for invoking WorkerSource's geojson.loadData target, which
* handles loading the geojson data and preparing to serve it up as tiles,
* using geojson-vt or supercluster as appropriate.
*/
_updateWorkerData(append = false) {
if (this._pendingLoad) {
this._coalesce = true;
return;
}
this.fire(new index$1.Event("dataloading", { dataType: "source" }));
this._loaded = false;
const options = Object.assign({ append }, this.workerOptions);
options.scope = this.scope;
const data = this._data;
if (typeof data === "string") {
options.request = this.map._requestManager.transformRequest(index$1.exported$1.resolveURL(data), index$1.ResourceType.Source);
options.request.collectResourceTiming = this._collectResourceTiming;
} else {
options.data = JSON.stringify(data);
}
this._pendingLoad = this.actor.send(`${this.type}.loadData`, options, (err, result) => {
this._loaded = true;
this._pendingLoad = null;
if (err) {
this.fire(new index$1.ErrorEvent(err));
} else {
const data2 = { dataType: "source", sourceDataType: this._metadataFired ? "content" : "metadata" };
if (this._collectResourceTiming && result && result.resourceTiming && result.resourceTiming[this.id]) {
data2.resourceTiming = result.resourceTiming[this.id];
}
if (append) this._partialReload = true;
this.fire(new index$1.Event("data", data2));
this._partialReload = false;
this._metadataFired = true;
}
if (this._coalesce) {
this._updateWorkerData(append);
this._coalesce = false;
}
});
}
loaded() {
return this._loaded;
}
reload() {
const fqid = index$1.makeFQID(this.id, this.scope);
this.map.style.clearSource(fqid);
this._updateWorkerData();
}
loadTile(tile, callback) {
const message = !tile.actor ? "loadTile" : "reloadTile";
tile.actor = this.actor;
const lutForScope = this.map.style ? this.map.style.getLut(this.scope) : null;
const lut = lutForScope ? { image: lutForScope.image.clone() } : null;
const partial = this._partialReload;
const params = {
type: this.type,
uid: tile.uid,
tileID: tile.tileID,
tileZoom: tile.tileZoom,
zoom: tile.tileID.overscaledZ,
maxZoom: this.maxzoom,
tileSize: this.tileSize,
source: this.id,
lut,
scope: this.scope,
pixelRatio: index$1.exported$1.devicePixelRatio,
showCollisionBoxes: this.map.showCollisionBoxes,
promoteId: this.promoteId,
brightness: this.map.style ? this.map.style.getBrightness() || 0 : 0,
extraShadowCaster: tile.isExtraShadowCaster,
scaleFactor: this.map.getScaleFactor(),
partial,
worldview: this.map.getWorldview(),
indoor: this.map.indoor ? this.map.indoor.getIndoorTileOptions(this.id, this.scope) : null
};
tile.request = this.actor.send(message, params, (err, data) => {
if (partial && !data) {
tile.state = "loaded";
return callback(null);
}
delete tile.request;
tile.destroy(false);
if (tile.aborted) {
return callback(null);
}
if (err) {
return callback(err);
}
tile.loadVectorData(data, this.map.painter, message === "reloadTile");
return callback(null);
}, void 0, message === "loadTile");
}
abortTile(tile) {
if (tile.request) {
tile.request.cancel();
delete tile.request;
}
tile.aborted = true;
}
unloadTile(tile, _) {
this.actor.send("removeTile", { uid: tile.uid, type: this.type, source: this.id, scope: this.scope });
tile.destroy();
}
onRemove(_) {
if (this._pendingLoad) {
this._pendingLoad.cancel();
}
}
serialize() {
return Object.assign({}, this._options, {
type: this.type,
data: this._data
});
}
hasTransition() {
return false;
}
}
class VideoSource extends index$1.ImageSource {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
this.roundZoom = true;
this.type = "video";
this.options = options;
}
load() {
this._loaded = false;
const options = this.options;
this.urls = [];
for (const url of options.urls) {
this.urls.push(this.map._requestManager.transformRequest(url, index$1.ResourceType.Source).url);
}
index$1.getVideo(this.urls, (err, video) => {
this._loaded = true;
if (err) {
this.fire(new index$1.ErrorEvent(err));
} else if (video) {
this.video = video;
this.video.loop = true;
this.video.setAttribute("playsinline", "");
this.video.addEventListener("playing", () => {
this.map.triggerRepaint();
});
if (this.map) {
this.video.play();
}
this._finishLoading();
}
});
}
/**
* Pauses the video.
*
* @example
* // Assuming a video source identified by video_source_id was added to the map
* const videoSource = map.getSource('video_source_id');
*
* // Pauses the video
* videoSource.pause();
*/
pause() {
if (this.video) {
this.video.pause();
}
}
/**
* Plays the video.
*
* @example
* // Assuming a video source identified by video_source_id was added to the map
* const videoSource = map.getSource('video_source_id');
*
* // Starts the video
* videoSource.play();
*/
play() {
if (this.video) {
this.video.play();
}
}
/**
* Sets playback to a timestamp, in seconds.
* @private
*/
seek(seconds) {
if (this.video) {
const seekableRange = this.video.seekable;
if (seconds < seekableRange.start(0) || seconds > seekableRange.end(0)) {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${this.id}`, null, `Playback for this video can be set only between the ${seekableRange.start(0)} and ${seekableRange.end(0)}-second mark.`)));
} else this.video.currentTime = seconds;
}
}
/**
* Returns the HTML `video` element.
*
* @returns {HTMLVideoElement} The HTML `video` element.
* @example
* // Assuming a video source identified by video_source_id was added to the map
* const videoSource = map.getSource('video_source_id');
*
* videoSource.getVideo(); //
*/
getVideo() {
return this.video;
}
onAdd(map) {
if (this.map) return;
this.map = map;
this.load();
if (this.video) {
this.video.play();
this.setCoordinates(this.coordinates);
}
}
// eslint-disable-next-line jsdoc/require-returns-check
/**
* Sets the video's coordinates and re-renders the map.
*
* @method setCoordinates
* @instance
* @memberof VideoSource
* @returns {VideoSource} Returns itself to allow for method chaining.
* @example
* // Add a video source to the map to map
* map.addSource('video_source_id', {
* type: 'video',
* urls: [
* 'https://www.mapbox.com/blog/assets/baltimore-smoke.mp4',
* 'https://www.mapbox.com/blog/assets/baltimore-smoke.webm'
* ],
* coordinates: [
* [-76.54, 39.18],
* [-76.52, 39.18],
* [-76.52, 39.17],
* [-76.54, 39.17]
* ]
* });
*
* // Then update the video source coordinates by new coordinates
* const videoSource = map.getSource('video_source_id');
* videoSource.setCoordinates([
* [-76.5433, 39.1857],
* [-76.5280, 39.1838],
* [-76.5295, 39.1768],
* [-76.5452, 39.1787]
* ]);
*/
// setCoordinates inherited from ImageSource
prepare() {
if (Object.keys(this.tiles).length === 0 || this.video.readyState < 2) {
return;
}
const context = this.map.painter.context;
const gl = context.gl;
if (!this.texture) {
this.texture = new index$1.Texture(context, this.video, gl.RGBA8);
this.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
this.width = this.video.videoWidth;
this.height = this.video.videoHeight;
} else if (!this.video.paused) {
this.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, this.video);
}
this._prepareData(context);
}
serialize() {
return {
type: "video",
urls: this.urls,
coordinates: this.coordinates
};
}
hasTransition() {
return this.video && !this.video.paused;
}
}
class CanvasSource extends index$1.ImageSource {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super(id, options, dispatcher, eventedParent);
if (!options.coordinates) {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${id}`, null, 'missing required property "coordinates"')));
} else if (!Array.isArray(options.coordinates) || options.coordinates.length !== 4 || options.coordinates.some((c) => !Array.isArray(c) || c.length !== 2 || c.some((l) => typeof l !== "number"))) {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${id}`, null, '"coordinates" property must be an array of 4 longitude/latitude array pairs')));
}
if (options.animate && typeof options.animate !== "boolean") {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${id}`, null, 'optional "animate" property must be a boolean value')));
}
if (!options.canvas) {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${id}`, null, 'missing required property "canvas"')));
} else if (typeof options.canvas !== "string" && !(options.canvas instanceof HTMLCanvasElement)) {
this.fire(new index$1.ErrorEvent(new index$1.ValidationError(`sources.${id}`, null, '"canvas" must be either a string representing the ID of the canvas element from which to read, or an HTMLCanvasElement instance')));
}
this.options = options;
this.animate = options.animate !== void 0 ? options.animate : true;
}
/**
* Enables animation. The image will be copied from the canvas to the map on each frame.
*
* @method play
* @instance
* @memberof CanvasSource
*/
/**
* Disables animation. The map will display a static copy of the canvas image.
*
* @method pause
* @instance
* @memberof CanvasSource
*/
load() {
this._loaded = true;
if (!this.canvas) {
this.canvas = this.options.canvas instanceof HTMLCanvasElement ? this.options.canvas : document.getElementById(this.options.canvas);
}
this.width = this.canvas.width;
this.height = this.canvas.height;
if (this._hasInvalidDimensions()) {
this.fire(new index$1.ErrorEvent(new Error("Canvas dimensions cannot be less than or equal to zero.")));
return;
}
this.play = function() {
this._playing = true;
this.map.triggerRepaint();
};
this.pause = function() {
if (this._playing) {
this.prepare();
this._playing = false;
}
};
this._finishLoading();
}
/**
* Returns the HTML `canvas` element.
*
* @returns {HTMLCanvasElement} The HTML `canvas` element.
* @example
* // Assuming the following canvas is added to your page
* //
* map.addSource('canvas-source', {
* type: 'canvas',
* canvas: 'canvasID',
* coordinates: [
* [91.4461, 21.5006],
* [100.3541, 21.5006],
* [100.3541, 13.9706],
* [91.4461, 13.9706]
* ]
* });
* map.getSource('canvas-source').getCanvas(); //
*/
getCanvas() {
return this.canvas;
}
onAdd(map) {
this.map = map;
this.load();
if (this.canvas) {
if (this.animate) this.play();
}
}
onRemove(_) {
this.pause();
}
/**
* Sets the canvas's coordinates and re-renders the map.
*
* @method setCoordinates
* @instance
* @memberof CanvasSource
* @param {Array>} coordinates Four geographical coordinates,
* represented as arrays of longitude and latitude numbers, which define the corners of the canvas.
* The coordinates start at the top left corner of the canvas and proceed in clockwise order.
* They do not have to represent a rectangle.
* @returns {CanvasSource} Returns itself to allow for method chaining.
*/
// setCoordinates inherited from ImageSource
prepare() {
let resize = false;
if (this.canvas.width !== this.width) {
this.width = this.canvas.width;
resize = true;
}
if (this.canvas.height !== this.height) {
this.height = this.canvas.height;
resize = true;
}
if (this._hasInvalidDimensions()) return;
if (Object.keys(this.tiles).length === 0) return;
const context = this.map.painter.context;
if (!this.texture) {
this.texture = new index$1.Texture(context, this.canvas, context.gl.RGBA8, { premultiply: true });
} else if ((resize || this._playing) && !(this.texture instanceof index$1.UserManagedTexture)) {
this.texture.update(this.canvas, { premultiply: true });
}
this._prepareData(context);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
serialize() {
return {
type: "canvas",
coordinates: this.coordinates
};
}
hasTransition() {
return this._playing;
}
_hasInvalidDimensions() {
for (const x of [this.canvas.width, this.canvas.height]) {
if (isNaN(x) || x <= 0) return true;
}
return false;
}
}
function isRaster(data) {
return data instanceof ImageData || data instanceof HTMLCanvasElement || data instanceof ImageBitmap || data instanceof HTMLImageElement;
}
class CustomSource extends index$1.Evented {
constructor(id, implementation, dispatcher, eventedParent) {
super();
this.id = id;
this.type = "custom";
this._dataType = "raster";
this._dispatcher = dispatcher;
this._implementation = implementation;
this.setEventedParent(eventedParent);
this.scheme = "xyz";
this.minzoom = 0;
this.maxzoom = 22;
this.tileSize = 512;
this._loaded = false;
this.roundZoom = true;
if (!this._implementation) {
this.fire(new index$1.ErrorEvent(new Error(`Missing implementation for ${this.id} custom source`)));
}
if (!this._implementation.loadTile) {
this.fire(new index$1.ErrorEvent(new Error(`Missing loadTile implementation for ${this.id} custom source`)));
}
if (this._implementation.bounds) {
this.tileBounds = new TileBounds(this._implementation.bounds, this.minzoom, this.maxzoom);
}
implementation.update = this._update.bind(this);
implementation.clearTiles = this._clearTiles.bind(this);
implementation.coveringTiles = this._coveringTiles.bind(this);
Object.assign(this, index$1.pick(implementation, ["dataType", "scheme", "minzoom", "maxzoom", "tileSize", "attribution", "minTileCacheSize", "maxTileCacheSize"]));
}
serialize() {
return index$1.pick(this, ["type", "scheme", "minzoom", "maxzoom", "tileSize", "attribution"]);
}
load() {
this._loaded = true;
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "metadata" }));
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "content" }));
}
loaded() {
return this._loaded;
}
onAdd(map) {
this.map = map;
this._loaded = false;
this.fire(new index$1.Event("dataloading", { dataType: "source" }));
if (this._implementation.onAdd) this._implementation.onAdd(map);
this.load();
}
onRemove(map) {
if (this._implementation.onRemove) {
this._implementation.onRemove(map);
}
}
hasTile(tileID) {
if (this._implementation.hasTile) {
const { x, y, z } = tileID.canonical;
return this._implementation.hasTile({ x, y, z });
}
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
loadTile(tile, callback) {
const { x, y, z } = tile.tileID.canonical;
const controller = new AbortController();
const signal = controller.signal;
tile.request = Promise.resolve(this._implementation.loadTile({ x, y, z }, { signal })).then(tileLoaded.bind(this)).catch((error) => {
if (error.name === "AbortError") return;
tile.state = "errored";
callback(error);
});
tile.request.cancel = () => controller.abort();
function tileLoaded(data) {
delete tile.request;
if (tile.aborted) {
tile.state = "unloaded";
return callback(null);
}
if (data === void 0) {
tile.state = "errored";
return callback(null);
}
if (data === null) {
const emptyImage = { width: this.tileSize, height: this.tileSize, data: null };
this.loadTileData(tile, emptyImage);
tile.state = "loaded";
return callback(null);
}
if (!isRaster(data)) {
tile.state = "errored";
return callback(new Error(`Can't infer data type for ${this.id}, only raster data supported at the moment`));
}
this.loadTileData(tile, data);
tile.state = "loaded";
callback(null);
}
}
loadTileData(tile, data) {
tile.setTexture(data, this.map.painter);
}
unloadTile(tile, callback) {
if (tile.texture && tile.texture instanceof index$1.Texture) {
tile.destroy(false);
if (tile.texture && tile.texture instanceof index$1.Texture) {
this.map.painter.saveTileTexture(tile.texture);
}
} else {
tile.destroy();
}
if (this._implementation.unloadTile) {
const { x, y, z } = tile.tileID.canonical;
this._implementation.unloadTile({ x, y, z });
}
if (callback) callback();
}
abortTile(tile, callback) {
if (tile.request && tile.request.cancel) {
tile.request.cancel();
delete tile.request;
}
if (callback) callback();
}
hasTransition() {
return false;
}
_coveringTiles() {
const tileIDs = this.map.transform.coveringTiles({
tileSize: this.tileSize,
minzoom: this.minzoom,
maxzoom: this.maxzoom,
roundZoom: this.roundZoom
});
return tileIDs.map((tileID) => ({ x: tileID.canonical.x, y: tileID.canonical.y, z: tileID.canonical.z }));
}
_clearTiles() {
const fqid = index$1.makeFQID(this.id, this.scope);
this.map.style.clearSource(fqid);
}
_update() {
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "content" }));
}
}
class Tiled3DModelSource extends index$1.Evented {
/**
* @private
*/
constructor(id, options, dispatcher, eventedParent) {
super();
this.type = "batched-model";
this.id = id;
this.tileSize = 512;
this._options = options;
this.tiles = this._options.tiles;
this.maxzoom = options.maxzoom || 19;
this.minzoom = options.minzoom || 0;
this.roundZoom = true;
this.usedInConflation = true;
this.dispatcher = dispatcher;
this.reparseOverscaled = false;
this.scheme = "xyz";
this._loaded = false;
this.setEventedParent(eventedParent);
}
onAdd(map) {
this.map = map;
this.load();
}
reload() {
this.cancelTileJSONRequest();
const fqid = index$1.makeFQID(this.id, this.scope);
this.load(() => this.map.style.clearSource(fqid));
}
cancelTileJSONRequest() {
if (!this._tileJSONRequest) return;
this._tileJSONRequest.cancel();
this._tileJSONRequest = null;
}
load(callback) {
this._loaded = false;
this.fire(new index$1.Event("dataloading", { dataType: "source" }));
const language = Array.isArray(this.map._language) ? this.map._language.join() : this.map._language;
const worldview = this.map.getWorldview();
this._tileJSONRequest = loadTileJSON(this._options, this.map._requestManager, language, worldview, (err, tileJSON) => {
this._tileJSONRequest = null;
this._loaded = true;
if (err) {
if (language) console.warn(`Ensure that your requested language string is a valid BCP-47 code or list of codes. Found: ${language}`);
if (worldview && worldview.length !== 2) console.warn(`Requested worldview strings must be a valid ISO alpha-2 code. Found: ${worldview}`);
this.fire(new index$1.ErrorEvent(err));
} else if (tileJSON) {
Object.assign(this, tileJSON);
if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
postTurnstileEvent(tileJSON.tiles, this.map._requestManager._customAccessToken);
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "metadata" }));
this.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "content" }));
}
if (callback) callback(err);
});
}
hasTransition() {
return false;
}
hasTile(tileID) {
return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
}
loaded() {
return this._loaded;
}
loadTile(tile, callback) {
const url = this.map._requestManager.normalizeTileURL(tile.tileID.canonical.url(this.tiles, this.scheme));
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Tile);
const params = {
request,
data: void 0,
uid: tile.uid,
tileID: tile.tileID,
tileZoom: tile.tileZoom,
zoom: tile.tileID.overscaledZ,
tileSize: this.tileSize * tile.tileID.overscaleFactor(),
type: this.type,
source: this.id,
scope: this.scope,
showCollisionBoxes: this.map.showCollisionBoxes,
isSymbolTile: tile.isSymbolTile,
brightness: this.map.style ? this.map.style.getBrightness() || 0 : 0,
pixelRatio: index$1.exported$1.devicePixelRatio,
promoteId: this.promoteId
};
if (!tile.actor || tile.state === "expired") {
tile.actor = this.dispatcher.getActor();
tile.request = tile.actor.send("loadTile", params, done.bind(this), void 0, true);
} else if (tile.state === "loading") {
tile.reloadCallback = callback;
} else {
if (tile.buckets) {
const buckets = Object.values(tile.buckets);
for (const bucket of buckets) {
bucket.dirty = true;
}
tile.state = "loaded";
return;
}
tile.request = tile.actor.send("reloadTile", params, done.bind(this));
}
function done(err, data) {
if (tile.aborted) return callback(null);
if (err && err.status !== 404) {
return callback(err);
}
if (this.map._refreshExpiredTiles && data) tile.setExpiryData(data);
tile.loadModelData(data, this.map.painter);
tile.state = "loaded";
callback(null);
}
}
serialize() {
return Object.assign({}, this._options);
}
}
const sourceTypes = {
vector: VectorTileSource,
raster: RasterTileSource,
"raster-dem": RasterDEMTileSource,
"raster-array": RasterArrayTileSource,
geojson: GeoJSONSource,
video: VideoSource,
image: index$1.ImageSource,
model: index$1.ModelSource,
"batched-model": Tiled3DModelSource,
canvas: CanvasSource,
custom: CustomSource
};
const create = function(id, specification, dispatcher, eventedParent) {
const source = new sourceTypes[specification.type](id, specification, dispatcher, eventedParent);
if (source.id !== id) {
throw new Error(`Expected Source id to be ${id} instead of ${source.id}`);
}
index$1.bindAll(["load", "abort", "unload", "serialize", "prepare"], source);
return source;
};
const getType = function(name) {
return sourceTypes[name];
};
const setType = function(name, type) {
sourceTypes[name] = type;
};
function generateTargetKey(target) {
if ("layerId" in target) {
return `layer:${target.layerId}`;
} else {
const { featuresetId, importId } = target;
return `featureset:${featuresetId}${importId ? `:import:${importId}` : ""}`;
}
}
function getFeatureTargetKey(variant, feature, targetId = "") {
return `${targetId}:${feature.id || ""}:${feature.layer.id}:${generateTargetKey(variant.target)}`;
}
function shouldSkipFeatureVariant(variant, feature, uniqueFeatureSet, targetId = "") {
if (variant.uniqueFeatureID) {
const key = getFeatureTargetKey(variant, feature, targetId);
if (uniqueFeatureSet.has(key)) {
return true;
}
uniqueFeatureSet.add(key);
}
return false;
}
function queryRenderedFeatures(queryGeometry, query, availableImages, transform, visualizeQueryGeometry = false) {
const sourceCacheTransform = query.sourceCache.transform;
const tileResults = query.sourceCache.tilesIn(queryGeometry, query.has3DLayers, visualizeQueryGeometry);
tileResults.sort(sortTilesIn);
const renderedFeatureLayers = [];
for (const tileResult of tileResults) {
const queryResults = tileResult.tile.queryRenderedFeatures(
query,
tileResult,
availableImages,
transform,
sourceCacheTransform,
visualizeQueryGeometry
);
if (Object.keys(queryResults).length) {
renderedFeatureLayers.push({ wrappedTileID: tileResult.tile.tileID.wrapped().key, queryResults });
}
}
for (const layerId in query.layers) {
const layer = query.layers[layerId];
if (layer.styleLayer) {
const queryResults = layer.styleLayer.queryRenderedFeatures(queryGeometry, query.sourceCache, transform);
if (Object.keys(queryResults).length) {
renderedFeatureLayers.push({ wrappedTileID: 0, queryResults });
}
}
}
if (renderedFeatureLayers.length === 0) {
return {};
}
return mergeRenderedFeatureLayers(renderedFeatureLayers);
}
function queryRenderedSymbols(queryGeometry, query, availableImages, collisionIndex, retainedQueryData, worldview) {
const result = {};
const renderedSymbols = collisionIndex.queryRenderedSymbols(queryGeometry);
const bucketQueryData = [];
for (const bucketInstanceId of Object.keys(renderedSymbols).map(Number)) {
bucketQueryData.push(retainedQueryData[bucketInstanceId]);
}
bucketQueryData.sort(sortTilesIn);
for (const queryData of bucketQueryData) {
const bucketSymbols = queryData.featureIndex.lookupSymbolFeatures(
renderedSymbols[queryData.bucketInstanceId],
queryData.bucketIndex,
queryData.sourceLayerIndex,
query,
availableImages,
worldview
);
for (const layerID in bucketSymbols) {
const resultFeatures = result[layerID] = result[layerID] || [];
const layerSymbols = bucketSymbols[layerID];
layerSymbols.sort((a, b) => {
const featureSortOrder = queryData.featureSortOrder;
if (featureSortOrder) {
const sortedA = featureSortOrder.indexOf(a.featureIndex);
const sortedB = featureSortOrder.indexOf(b.featureIndex);
index$1.assert(sortedA >= 0);
index$1.assert(sortedB >= 0);
return sortedB - sortedA;
} else {
return b.featureIndex - a.featureIndex;
}
});
for (const symbolFeature of layerSymbols) {
resultFeatures.push(symbolFeature);
}
}
}
return result;
}
function querySourceFeatures(sourceCache, params) {
const tiles = sourceCache.getRenderableIds().map((id) => {
return sourceCache.getTileByID(id);
});
const result = [];
const dataTiles = {};
for (let i = 0; i < tiles.length; i++) {
const tile = tiles[i];
const dataID = tile.tileID.canonical.key;
if (!dataTiles[dataID]) {
dataTiles[dataID] = true;
tile.querySourceFeatures(result, params);
}
}
return result;
}
function sortTilesIn(a, b) {
const idA = a.tileID;
const idB = b.tileID;
return idA.overscaledZ - idB.overscaledZ || idA.canonical.y - idB.canonical.y || idA.wrap - idB.wrap || idA.canonical.x - idB.canonical.x;
}
function mergeRenderedFeatureLayers(tiles) {
const result = {};
const wrappedIDLayerMap = {};
for (const tile of tiles) {
const queryResults = tile.queryResults;
const wrappedID = tile.wrappedTileID;
const wrappedIDLayers = wrappedIDLayerMap[wrappedID] = wrappedIDLayerMap[wrappedID] || {};
for (const layerID in queryResults) {
const tileFeatures = queryResults[layerID];
const wrappedIDFeatures = wrappedIDLayers[layerID] = wrappedIDLayers[layerID] || {};
const resultFeatures = result[layerID] = result[layerID] || [];
for (const tileFeature of tileFeatures) {
if (!wrappedIDFeatures[tileFeature.featureIndex]) {
wrappedIDFeatures[tileFeature.featureIndex] = true;
resultFeatures.push(tileFeature);
}
}
}
}
return result;
}
function deserialize(input, style) {
const output = {};
if (!style) return output;
for (const bucket of input) {
const layers = bucket.layerIds.map((id) => style.getLayer(id)).filter(Boolean);
if (layers.length === 0) {
continue;
}
bucket.layers = layers;
if (bucket.stateDependentLayerIds) {
bucket.stateDependentLayers = bucket.stateDependentLayerIds.map((lId) => layers.filter((l) => l.id === lId)[0]);
}
for (const layer of layers) {
output[layer.fqid] = bucket;
}
}
return output;
}
const Debug = {
extend(dest, ...sources) {
return Object.assign(dest, ...sources);
},
run(fn) {
fn();
},
debugCanvas: null,
aabbCorners: [],
_initializeCanvas(tr) {
if (!Debug.debugCanvas) {
const canvas = Debug.debugCanvas = document.createElement("canvas");
if (document.body) document.body.appendChild(canvas);
canvas.style.position = "absolute";
canvas.style.left = "0";
canvas.style.top = "0";
canvas.style.pointerEvents = "none";
const resize = () => {
canvas.width = tr.width;
canvas.height = tr.height;
};
resize();
window.addEventListener("resize", resize);
}
return Debug.debugCanvas;
},
_drawLine(ctx, start, end) {
if (!start || !end) return;
ctx.moveTo(...start);
ctx.lineTo(...end);
},
_drawQuad(ctx, corners) {
Debug._drawLine(ctx, corners[0], corners[1]);
Debug._drawLine(ctx, corners[1], corners[2]);
Debug._drawLine(ctx, corners[2], corners[3]);
Debug._drawLine(ctx, corners[3], corners[0]);
},
_drawBox(ctx, corners) {
index$1.assert(corners.length === 8, `AABB needs 8 corners, found ${corners.length}`);
ctx.beginPath();
Debug._drawQuad(ctx, corners.slice(0, 4));
Debug._drawQuad(ctx, corners.slice(4));
Debug._drawLine(ctx, corners[0], corners[4]);
Debug._drawLine(ctx, corners[1], corners[5]);
Debug._drawLine(ctx, corners[2], corners[6]);
Debug._drawLine(ctx, corners[3], corners[7]);
ctx.stroke();
},
drawAabbs(painter, sourceCache, coords) {
const tr = painter.transform;
const worldToECEFMatrix = index$1.invert(new Float64Array(16), tr.globeMatrix);
const ecefToPixelMatrix = index$1.multiply([], tr.pixelMatrix, tr.globeMatrix);
const ecefToCameraMatrix = index$1.multiply([], tr._camera.getWorldToCamera(tr.worldSize, 1), tr.globeMatrix);
if (!tr.freezeTileCoverage) {
Debug.aabbCorners = coords.map((coord) => {
const aabb = index$1.aabbForTileOnGlobe(tr, tr.worldSize, coord.canonical, false);
const corners = aabb.getCorners();
for (const pos of corners) {
index$1.transformMat4(pos, pos, worldToECEFMatrix);
}
return corners;
});
}
const canvas = Debug._initializeCanvas(tr);
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
const tileCount = Debug.aabbCorners.length;
ctx.shadowColor = "#000";
ctx.shadowBlur = 2;
ctx.lineWidth = 1.5;
for (let i = 0; i < tileCount; i++) {
const pixelCorners = Debug.aabbCorners[i].map((ecef) => {
const cameraPos = index$1.transformMat4([], ecef, ecefToCameraMatrix);
if (cameraPos[2] > 0) {
return null;
}
return index$1.transformMat4([], ecef, ecefToPixelMatrix);
});
ctx.strokeStyle = `hsl(${360 * i / tileCount}, 100%, 50%)`;
Debug._drawBox(ctx, pixelCorners);
}
},
clearAabbs() {
if (!Debug.debugCanvas) return;
Debug.debugCanvas.getContext("2d").clearRect(0, 0, Debug.debugCanvas.width, Debug.debugCanvas.height);
Debug.aabbCorners = [];
}
};
class TileSpaceDebugBuffer {
constructor(tileSize, color = index$1.Color.red) {
this.vertices = new index$1.StructArrayLayout2i4();
this.indices = new index$1.StructArrayLayout1ui2();
this.tileSize = tileSize;
this.needsUpload = true;
this.color = color;
}
addPoints(points) {
this.clearPoints();
for (const point of points) {
this.addPoint(point);
}
this.addPoint(points[0]);
}
addPoint(p) {
const crosshairSize = 80;
const currLineLineLength = this.vertices.length;
this.vertices.emplaceBack(p.x, p.y);
this.vertices.emplaceBack(p.x + crosshairSize / 2, p.y);
this.vertices.emplaceBack(p.x, p.y - crosshairSize / 2);
this.vertices.emplaceBack(p.x, p.y + crosshairSize / 2);
this.vertices.emplaceBack(p.x - crosshairSize / 2, p.y);
this.indices.emplaceBack(currLineLineLength);
this.indices.emplaceBack(currLineLineLength + 1);
this.indices.emplaceBack(currLineLineLength + 2);
this.indices.emplaceBack(currLineLineLength + 3);
this.indices.emplaceBack(currLineLineLength + 4);
this.indices.emplaceBack(currLineLineLength);
this.needsUpload = true;
}
clearPoints() {
this.vertices.clear();
this.indices.clear();
this.needsUpload = true;
}
lazyUpload(context) {
if (this.needsUpload && this.hasVertices()) {
this.unload();
this.vertexBuffer = context.createVertexBuffer(this.vertices, index$1.posAttributes.members, true);
this.indexBuffer = context.createIndexBuffer(this.indices, true);
this.segments = index$1.SegmentVector.simpleSegment(0, 0, this.vertices.length, this.indices.length);
this.needsUpload = false;
}
}
hasVertices() {
return this.vertices.length > 1;
}
unload() {
if (this.vertexBuffer) {
this.vertexBuffer.destroy();
delete this.vertexBuffer;
}
if (this.indexBuffer) {
this.indexBuffer.destroy();
delete this.indexBuffer;
}
if (this.segments) {
this.segments.destroy();
delete this.segments;
}
}
}
const meshSize = 32;
const gridSize = meshSize + 1;
const numTriangles = meshSize * meshSize * 2 - 2;
const numParentTriangles = numTriangles - meshSize * meshSize;
const coords = new Uint16Array(numTriangles * 4);
for (let i = 0; i < numTriangles; i++) {
let id = i + 2;
let ax = 0, ay = 0, bx = 0, by = 0, cx = 0, cy = 0;
if (id & 1) {
bx = by = cx = meshSize;
} else {
ax = ay = cy = meshSize;
}
while ((id >>= 1) > 1) {
const mx = ax + bx >> 1;
const my = ay + by >> 1;
if (id & 1) {
bx = ax;
by = ay;
ax = cx;
ay = cy;
} else {
ax = bx;
ay = by;
bx = cx;
by = cy;
}
cx = mx;
cy = my;
}
const k = i * 4;
coords[k + 0] = ax;
coords[k + 1] = ay;
coords[k + 2] = bx;
coords[k + 3] = by;
}
const reprojectedCoords = new Uint16Array(gridSize * gridSize * 2);
const used = new Uint8Array(gridSize * gridSize);
const indexMap = new Uint16Array(gridSize * gridSize);
const commonRasterTileSize = 256;
const paddingSize = meshSize / commonRasterTileSize / 4;
function seamPadding(n) {
if (n === 0) return -paddingSize;
else if (n === gridSize - 1) return paddingSize;
else return 0;
}
function getTileMesh(canonical, projection) {
const cs = index$1.tileTransform(canonical, projection);
const z2 = Math.pow(2, canonical.z);
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
const lng = index$1.lngFromMercatorX((canonical.x + (x + seamPadding(x)) / meshSize) / z2);
const lat = index$1.latFromMercatorY((canonical.y + (y + seamPadding(y)) / meshSize) / z2);
const p = projection.project(lng, lat);
const k = y * gridSize + x;
reprojectedCoords[2 * k + 0] = Math.round((p.x * cs.scale - cs.x) * index$1.EXTENT);
reprojectedCoords[2 * k + 1] = Math.round((p.y * cs.scale - cs.y) * index$1.EXTENT);
}
}
used.fill(0);
indexMap.fill(0);
for (let i = numTriangles - 1; i >= 0; i--) {
const k = i * 4;
const ax = coords[k + 0];
const ay = coords[k + 1];
const bx = coords[k + 2];
const by = coords[k + 3];
const mx = ax + bx >> 1;
const my = ay + by >> 1;
const cx = mx + my - ay;
const cy = my + ax - mx;
const aIndex = ay * gridSize + ax;
const bIndex = by * gridSize + bx;
const mIndex = my * gridSize + mx;
const rax = reprojectedCoords[2 * aIndex + 0];
const ray = reprojectedCoords[2 * aIndex + 1];
const rbx = reprojectedCoords[2 * bIndex + 0];
const rby = reprojectedCoords[2 * bIndex + 1];
const rmx = reprojectedCoords[2 * mIndex + 0];
const rmy = reprojectedCoords[2 * mIndex + 1];
const isUsed = Math.hypot((rax + rbx) / 2 - rmx, (ray + rby) / 2 - rmy) >= 16;
used[mIndex] = used[mIndex] || (isUsed ? 1 : 0);
if (i < numParentTriangles) {
const leftChildIndex = (ay + cy >> 1) * gridSize + (ax + cx >> 1);
const rightChildIndex = (by + cy >> 1) * gridSize + (bx + cx >> 1);
used[mIndex] = used[mIndex] || used[leftChildIndex] || used[rightChildIndex];
}
}
const vertices = new index$1.StructArrayLayout4i8();
const indices = new index$1.StructArrayLayout3ui6();
let numVertices = 0;
function addVertex(x, y) {
const k = y * gridSize + x;
if (indexMap[k] === 0) {
vertices.emplaceBack(
reprojectedCoords[2 * k + 0],
reprojectedCoords[2 * k + 1],
x * index$1.EXTENT / meshSize,
y * index$1.EXTENT / meshSize
);
indexMap[k] = ++numVertices;
}
return indexMap[k] - 1;
}
function addTriangles(ax, ay, bx, by, cx, cy) {
const mx = ax + bx >> 1;
const my = ay + by >> 1;
if (Math.abs(ax - cx) + Math.abs(ay - cy) > 1 && used[my * gridSize + mx]) {
addTriangles(cx, cy, ax, ay, mx, my);
addTriangles(bx, by, cx, cy, mx, my);
} else {
const ai = addVertex(ax, ay);
const bi = addVertex(bx, by);
const ci = addVertex(cx, cy);
indices.emplaceBack(ai, bi, ci);
}
}
addTriangles(0, 0, meshSize, meshSize, meshSize, 0);
addTriangles(meshSize, meshSize, 0, 0, 0, meshSize);
return { vertices, indices };
}
const CLOCK_SKEW_RETRY_TIMEOUT = 3e4;
const BOUNDS_FEATURE = /* @__PURE__ */ (() => {
return {
type: 2,
extent: index$1.EXTENT,
loadGeometry() {
return [[
new index$1.Point(0, 0),
new index$1.Point(index$1.EXTENT + 1, 0),
new index$1.Point(index$1.EXTENT + 1, index$1.EXTENT + 1),
new index$1.Point(0, index$1.EXTENT + 1),
new index$1.Point(0, 0)
]];
}
};
})();
function getPixelPosMatrix(transform, tileID) {
const t = index$1.fromScaling([], [transform.width * 0.5, -transform.height * 0.5, 1]);
index$1.translate(t, t, [1, -1, 0]);
index$1.multiply(t, t, transform.calculateProjMatrix(tileID.toUnwrapped()));
return Float32Array.from(t);
}
class Tile {
/**
* @param {OverscaledTileID} tileID
* @param size
* @private
*/
constructor(tileID, size, tileZoom, painter, isRaster, worldview) {
this.tileID = tileID;
this.uid = index$1.uniqueId();
this.uses = 0;
this.tileSize = size;
this.tileZoom = tileZoom;
this.buckets = {};
this.expirationTime = null;
this.queryPadding = 0;
this.hasSymbolBuckets = false;
this.hasRTLText = false;
this.dependencies = {};
this.isRaster = isRaster;
if (painter && painter.style) {
this._lastUpdatedBrightness = painter.style.getBrightness();
}
this.expiredRequestCount = 0;
this.state = "loading";
if (painter && painter.transform) {
this.projection = painter.transform.projection;
}
this.worldview = worldview;
this._hasAppearances = null;
}
registerFadeDuration(duration) {
const fadeEndTime = duration + this.timeAdded;
if (fadeEndTime < index$1.exported$1.now()) return;
if (this.fadeEndTime && fadeEndTime < this.fadeEndTime) return;
this.fadeEndTime = fadeEndTime;
}
wasRequested() {
return this.state === "errored" || this.state === "loaded" || this.state === "reloading";
}
get tileTransform() {
if (!this._tileTransform) {
this._tileTransform = index$1.tileTransform(this.tileID.canonical, this.projection);
}
return this._tileTransform;
}
/**
* Given a data object with a 'buffers' property, load it into
* this tile's elementGroups and buffers properties and set loaded
* to true. If the data is null, like in the case of an empty
* GeoJSON tile, no-op but still set loaded to true.
* @param {Object} data
* @param painter
* @returns {undefined}
* @private
*/
loadVectorData(data, painter, justReloaded) {
this.unloadVectorData();
this.state = "loaded";
if (!data) {
this.collisionBoxArray = new index$1.CollisionBoxArray();
return;
}
if (data.featureIndex) {
this.latestFeatureIndex = data.featureIndex;
if (data.rawTileData) {
this.latestRawTileData = data.rawTileData;
this.latestFeatureIndex.rawTileData = data.rawTileData;
} else if (this.latestRawTileData) {
this.latestFeatureIndex.rawTileData = this.latestRawTileData;
}
}
this.collisionBoxArray = data.collisionBoxArray;
this.buckets = deserialize(data.buckets, painter.style);
this.hasSymbolBuckets = false;
for (const id in this.buckets) {
const bucket = this.buckets[id];
if (bucket instanceof index$1.SymbolBucket) {
this.hasSymbolBuckets = true;
if (justReloaded) {
bucket.justReloaded = true;
} else {
break;
}
}
}
this.hasRTLText = false;
if (this.hasSymbolBuckets) {
for (const id in this.buckets) {
const bucket = this.buckets[id];
if (bucket instanceof index$1.SymbolBucket) {
if (bucket.hasRTLText) {
this.hasRTLText = true;
index$1.lazyLoadRTLTextPlugin();
break;
}
}
}
}
this.queryPadding = 0;
for (const id in this.buckets) {
const bucket = this.buckets[id];
const layer = painter.style.getOwnLayer(id);
if (!layer) continue;
const queryRadius = layer.queryRadius(bucket);
this.queryPadding = Math.max(this.queryPadding, queryRadius);
}
if (data.imageAtlas) {
this.imageAtlas = data.imageAtlas;
}
if (data.glyphAtlasImage) {
this.glyphAtlasImage = data.glyphAtlasImage;
}
if (data.lineAtlas) {
this.lineAtlas = data.lineAtlas;
}
this._lastUpdatedBrightness = data.brightness;
}
/**
* Release any data or WebGL resources referenced by this tile.
* @returns {undefined}
* @private
*/
unloadVectorData() {
if (!this.hasData()) return;
for (const id in this.buckets) {
this.buckets[id].destroy();
}
this.buckets = {};
if (this.imageAtlas) {
this.imageAtlas = null;
}
if (this.lineAtlas) {
this.lineAtlas = null;
}
if (this.imageAtlasTexture) {
this.imageAtlasTexture.destroy();
}
if (this.glyphAtlasTexture) {
this.glyphAtlasTexture.destroy();
}
if (this.lineAtlasTexture) {
this.lineAtlasTexture.destroy();
}
if (this._tileBoundsBuffer) {
this._tileBoundsBuffer.destroy();
this._tileBoundsIndexBuffer.destroy();
this._tileBoundsSegments.destroy();
this._tileBoundsBuffer = null;
}
if (this._tileDebugBuffer) {
this._tileDebugBuffer.destroy();
this._tileDebugSegments.destroy();
this._tileDebugBuffer = null;
}
if (this._tileDebugIndexBuffer) {
this._tileDebugIndexBuffer.destroy();
this._tileDebugIndexBuffer = null;
}
if (this._globeTileDebugBorderBuffer) {
this._globeTileDebugBorderBuffer.destroy();
this._globeTileDebugBorderBuffer = null;
}
if (this._tileDebugTextBuffer) {
this._tileDebugTextBuffer.destroy();
this._tileDebugTextSegments.destroy();
this._tileDebugTextIndexBuffer.destroy();
this._tileDebugTextBuffer = null;
}
if (this._globeTileDebugTextBuffer) {
this._globeTileDebugTextBuffer.destroy();
this._globeTileDebugTextBuffer = null;
}
Debug.run(() => {
if (this.queryGeometryDebugViz) {
this.queryGeometryDebugViz.unload();
delete this.queryGeometryDebugViz;
}
if (this.queryBoundsDebugViz) {
this.queryBoundsDebugViz.unload();
delete this.queryBoundsDebugViz;
}
});
this.latestFeatureIndex = null;
this.state = "unloaded";
}
loadModelData(data, painter, justReloaded) {
if (!data) {
return;
}
if (data.resourceTiming) this.resourceTiming = data.resourceTiming;
this.buckets = Object.assign({}, this.buckets, deserialize(data.buckets, painter.style));
if (data.featureIndex) {
this.latestFeatureIndex = data.featureIndex;
}
}
getBucket(layer) {
return this.buckets[layer.fqid];
}
upload(context, painter) {
for (const id in this.buckets) {
const bucket = this.buckets[id];
if (bucket.uploadPending()) {
let featureStates = {};
let availableImages = [];
let globalProperties = {
zoom: 0,
pitch: 0,
brightness: 0,
worldview: ""
};
if (painter) {
if (painter.style) {
availableImages = painter.style.listImages();
const bucketLayer = bucket.layers[0];
const sourceLayerId = bucketLayer["sourceLayer"] || "_geojsonTileLayer";
const sourceCache = painter.style.getLayerSourceCache(bucketLayer);
if (sourceCache) {
featureStates = sourceCache._state.getState(sourceLayerId, void 0);
}
}
globalProperties = {
zoom: painter.transform.zoom || 0,
pitch: painter.transform.pitch || 0,
brightness: painter.style.getBrightness() || 0,
worldview: painter.worldview || ""
};
}
bucket.upload(context, this.tileID.canonical, featureStates, availableImages, globalProperties);
}
}
const gl = context.gl;
const atlas = this.imageAtlas;
if (atlas && !atlas.uploaded) {
const hasPattern = !!atlas.patternPositions.size;
this.imageAtlasTexture = new index$1.Texture(context, atlas.image, gl.RGBA8, { useMipmap: hasPattern });
this.imageAtlas.uploaded = true;
}
if (this.glyphAtlasImage) {
this.glyphAtlasTexture = new index$1.Texture(context, this.glyphAtlasImage, gl.R8);
this.glyphAtlasImage = null;
}
if (this.lineAtlas && !this.lineAtlas.uploaded) {
this.lineAtlasTexture = new index$1.Texture(context, this.lineAtlas.image, gl.R8);
this.lineAtlas.uploaded = true;
}
}
prepare(imageManager, painter, scope) {
if (this.imageAtlas && this.imageAtlasTexture) {
this.imageAtlas.patchUpdatedImages(imageManager, this.imageAtlasTexture, scope);
}
if (!painter || !this.latestFeatureIndex || !this.latestFeatureIndex.rawTileData) {
return;
}
const brightness = painter.style.getBrightness();
if (this._hasAppearances === null) {
this._hasAppearances = this.hasAppearances(painter);
}
if (!this._lastUpdatedBrightness && !brightness && !this._hasAppearances) {
return;
}
if (!this._hasAppearances && this._lastUpdatedBrightness && brightness && Math.abs(this._lastUpdatedBrightness - brightness) < 1e-3) {
return;
}
this.updateBuckets(painter, this._lastUpdatedBrightness !== brightness);
this._lastUpdatedBrightness = brightness;
}
// Evaluate maximum query padding required for all buckets of this tile
evaluateQueryRenderedFeaturePadding() {
let maxRadius = 0;
for (const bucketId in this.buckets) {
const bucket = this.buckets[bucketId];
if (bucket.evaluateQueryRenderedFeaturePadding) {
maxRadius = Math.max(maxRadius, bucket.evaluateQueryRenderedFeaturePadding());
}
}
return maxRadius;
}
// Queries non-symbol features rendered for this tile.
// Symbol features are queried globally
queryRenderedFeatures(query, tilespaceGeometry, availableImages, transform, sourceCacheTransform, visualizeQueryGeometry) {
Debug.run(() => {
if (visualizeQueryGeometry) {
let geometryViz = this.queryGeometryDebugViz;
let boundsViz = this.queryBoundsDebugViz;
if (!geometryViz) {
geometryViz = this.queryGeometryDebugViz = new TileSpaceDebugBuffer(this.tileSize);
}
if (!boundsViz) {
boundsViz = this.queryBoundsDebugViz = new TileSpaceDebugBuffer(this.tileSize, index$1.Color.blue);
}
geometryViz.addPoints(tilespaceGeometry.tilespaceGeometry);
boundsViz.addPoints(tilespaceGeometry.bufferedTilespaceGeometry);
}
});
if (!this.latestFeatureIndex || !(this.latestFeatureIndex.rawTileData || this.latestFeatureIndex.is3DTile)) {
return {};
}
const maxFeatureQueryRadius = this.evaluateQueryRenderedFeaturePadding();
const pixelPosMatrix = getPixelPosMatrix(sourceCacheTransform, this.tileID);
return this.latestFeatureIndex.query(
query,
{
tilespaceGeometry,
pixelPosMatrix,
transform,
availableImages,
tileTransform: this.tileTransform,
worldview: this.worldview,
queryRadius: maxFeatureQueryRadius
}
);
}
querySourceFeatures(result, params) {
const featureIndex = this.latestFeatureIndex;
if (!featureIndex || !featureIndex.rawTileData) return;
const vtLayers = featureIndex.loadVTLayers();
const sourceLayer = params ? params.sourceLayer : "";
const layer = vtLayers._geojsonTileLayer || vtLayers[sourceLayer];
if (!layer) return;
const filter = index$1.createFilter(params && params.filter);
const { z, x, y } = this.tileID.canonical;
const coord = { z, x, y };
for (let i = 0; i < layer.length; i++) {
const feature = layer.feature(i);
if (filter.needGeometry) {
const evaluationFeature = index$1.toEvaluationFeature(feature, true);
if (!filter.filter(new index$1.EvaluationParameters(this.tileID.overscaledZ, { worldview: this.worldview }), evaluationFeature, this.tileID.canonical))
continue;
} else if (!filter.filter(new index$1.EvaluationParameters(this.tileID.overscaledZ, { worldview: this.worldview }), feature)) {
continue;
}
const id = featureIndex.getId(feature, sourceLayer);
const geojsonFeature = new index$1.Feature(feature, z, x, y, id);
geojsonFeature.tile = coord;
result.push(geojsonFeature);
}
}
loaded() {
return this.state === "loaded" || this.state === "errored";
}
hasData() {
return this.state === "loaded" || this.state === "reloading" || this.state === "expired";
}
patternsLoaded() {
return !!this.imageAtlas && !!this.imageAtlas.patternPositions.size;
}
setExpiryData(data) {
const prior = this.expirationTime;
if (data.cacheControl) {
const parsedCC = index$1.parseCacheControl(data.cacheControl);
if (parsedCC["max-age"]) this.expirationTime = Date.now() + parsedCC["max-age"] * 1e3;
} else if (data.expires) {
this.expirationTime = new Date(data.expires).getTime();
}
if (this.expirationTime) {
const now = Date.now();
let isExpired = false;
if (this.expirationTime > now) {
isExpired = false;
} else if (!prior) {
isExpired = true;
} else if (this.expirationTime < prior) {
isExpired = true;
} else {
const delta = this.expirationTime - prior;
if (!delta) {
isExpired = true;
} else {
this.expirationTime = now + Math.max(delta, CLOCK_SKEW_RETRY_TIMEOUT);
}
}
if (isExpired) {
this.expiredRequestCount++;
this.state = "expired";
} else {
this.expiredRequestCount = 0;
}
}
}
getExpiryTimeout() {
if (this.expirationTime) {
if (this.expiredRequestCount) {
return 1e3 * (1 << Math.min(this.expiredRequestCount - 1, 31));
} else {
return Math.min(this.expirationTime - (/* @__PURE__ */ new Date()).getTime(), Math.pow(2, 31) - 1);
}
}
}
refreshFeatureState(painter) {
if (!this.latestFeatureIndex || !(this.latestFeatureIndex.rawTileData || this.latestFeatureIndex.is3DTile) || !painter) {
return;
}
this.updateBuckets(painter);
}
hasAppearances(painter) {
for (const id in this.buckets) {
if (!painter.style.hasLayer(id)) continue;
const bucket = this.buckets[id];
const hasAppearances = bucket.layers.some((layer) => layer.appearances && layer.appearances.length > 0);
if (hasAppearances) return true;
}
return false;
}
updateBuckets(painter, isBrightnessChanged) {
if (!this.latestFeatureIndex) return;
if (!painter.style) return;
const availableImages = painter.style.listImages();
const brightness = painter.style.getBrightness();
for (const id in this.buckets) {
if (!painter.style.hasLayer(id)) continue;
const bucket = this.buckets[id];
const bucketLayer = bucket.layers[0];
const sourceLayerId = bucketLayer["sourceLayer"] || "_geojsonTileLayer";
const sourceCache = painter.style.getLayerSourceCache(bucketLayer);
let sourceLayerStates = {};
if (sourceCache) {
sourceLayerStates = sourceCache._state.getState(sourceLayerId, void 0);
}
const imagePositions = this.imageAtlas ? Object.fromEntries(this.imageAtlas.patternPositions) : {};
const withStateUpdates = Object.keys(sourceLayerStates).length > 0 && !isBrightnessChanged;
bucket.hasAppearances = bucket.layers.some((layer2) => layer2.appearances && layer2.appearances.length > 0);
const layers = withStateUpdates ? bucket.stateDependentLayers : bucket.layers;
if (withStateUpdates && bucket.stateDependentLayers.length !== 0 || isBrightnessChanged) {
const vtLayers = this.latestFeatureIndex.loadVTLayers();
const sourceLayer = vtLayers[sourceLayerId];
bucket.update(sourceLayerStates, sourceLayer, availableImages, imagePositions, layers, isBrightnessChanged, brightness);
}
if (withStateUpdates && bucket.stateDependentLayers.length !== 0 || isBrightnessChanged || bucket.hasAppearances) {
const globalProperties = {
zoom: painter.transform.zoom,
pitch: painter.transform.pitch,
brightness: painter.style.getBrightness() || 0,
worldview: painter.worldview
};
bucket.updateAppearances(this.tileID.canonical, sourceLayerStates, availableImages, globalProperties);
}
if (bucket instanceof index$1.LineBucket || bucket instanceof index$1.FillBucket) {
if (painter._terrain && painter._terrain.enabled && sourceCache && bucket.uploadPending()) {
painter._terrain._clearRenderCacheForTile(sourceCache.id, this.tileID);
}
}
const layer = painter && painter.style && painter.style.getOwnLayer(id);
if (layer) {
this.queryPadding = Math.max(this.queryPadding, layer.queryRadius(bucket));
}
}
}
holdingForFade() {
return this.symbolFadeHoldUntil !== void 0;
}
symbolFadeFinished() {
return !this.symbolFadeHoldUntil || this.symbolFadeHoldUntil < index$1.exported$1.now();
}
clearFadeHold() {
this.symbolFadeHoldUntil = void 0;
}
setHoldDuration(duration) {
this.symbolFadeHoldUntil = index$1.exported$1.now() + duration;
}
setTexture(img, painter) {
const context = painter.context;
const gl = context.gl;
this.texture = this.texture || painter.getTileTexture(img.width);
if (this.texture && this.texture instanceof index$1.Texture) {
this.texture.update(img);
} else {
this.texture = new index$1.Texture(context, img, gl.RGBA8, { useMipmap: true });
this.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
}
setDependencies(namespace, dependencies) {
const index = {};
for (const dep of dependencies) {
index[dep] = true;
}
this.dependencies[namespace] = index;
}
hasDependency(namespaces, keys) {
for (const namespace of namespaces) {
const dependencies = this.dependencies[namespace];
if (dependencies) {
for (const key of keys) {
if (dependencies[key]) {
return true;
}
}
}
}
return false;
}
clearQueryDebugViz() {
Debug.run(() => {
if (this.queryGeometryDebugViz) {
this.queryGeometryDebugViz.clearPoints();
}
if (this.queryBoundsDebugViz) {
this.queryBoundsDebugViz.clearPoints();
}
});
}
_makeDebugTileBoundsBuffers(context, projection) {
if (!projection || projection.name === "mercator" || this._tileDebugBuffer) return;
const boundsLine = index$1.loadGeometry(BOUNDS_FEATURE, this.tileID.canonical, this.tileTransform)[0];
const debugVertices = new index$1.StructArrayLayout2i4();
const debugIndices = new index$1.StructArrayLayout1ui2();
for (let i = 0; i < boundsLine.length; i++) {
const { x, y } = boundsLine[i];
debugVertices.emplaceBack(x, y);
debugIndices.emplaceBack(i);
}
debugIndices.emplaceBack(0);
this._tileDebugIndexBuffer = context.createIndexBuffer(debugIndices);
this._tileDebugBuffer = context.createVertexBuffer(debugVertices, index$1.posAttributes.members);
this._tileDebugSegments = index$1.SegmentVector.simpleSegment(0, 0, debugVertices.length, debugIndices.length);
}
_makeTileBoundsBuffers(context, projection) {
if (this._tileBoundsBuffer || !projection || projection.name === "mercator") return;
const boundsLine = index$1.loadGeometry(BOUNDS_FEATURE, this.tileID.canonical, this.tileTransform)[0];
let boundsVertices, boundsIndices;
if (this.isRaster) {
const mesh = getTileMesh(this.tileID.canonical, projection);
boundsVertices = mesh.vertices;
boundsIndices = mesh.indices;
} else {
boundsVertices = new index$1.StructArrayLayout4i8();
boundsIndices = new index$1.StructArrayLayout3ui6();
for (const { x, y } of boundsLine) {
boundsVertices.emplaceBack(x, y, 0, 0);
}
const indices = index$1.earcut(boundsVertices.int16.subarray(0, boundsVertices.length * 4), void 0, 4);
for (let i = 0; i < indices.length; i += 3) {
boundsIndices.emplaceBack(indices[i], indices[i + 1], indices[i + 2]);
}
}
this._tileBoundsBuffer = context.createVertexBuffer(boundsVertices, index$1.boundsAttributes.members);
this._tileBoundsIndexBuffer = context.createIndexBuffer(boundsIndices);
this._tileBoundsSegments = index$1.SegmentVector.simpleSegment(0, 0, boundsVertices.length, boundsIndices.length);
}
_makeGlobeTileDebugBuffers(context, transform) {
const projection = transform.projection;
if (!projection || projection.name !== "globe" || transform.freezeTileCoverage) return;
const id = this.tileID.canonical;
const bounds = index$1.transitionTileAABBinECEF(id, transform);
const normalizationMatrix = index$1.globeNormalizeECEF(bounds);
const phase = index$1.globeToMercatorTransition(transform.zoom);
let worldToECEFMatrix;
if (phase > 0) {
worldToECEFMatrix = index$1.invert(new Float64Array(16), transform.globeMatrix);
}
this._makeGlobeTileDebugBorderBuffer(context, id, transform, normalizationMatrix, worldToECEFMatrix, phase);
this._makeGlobeTileDebugTextBuffer(context, id, transform, normalizationMatrix, worldToECEFMatrix, phase);
}
_globePoint(x, y, id, tr, normalizationMatrix, worldToECEFMatrix, phase) {
let ecef = index$1.tileCoordToECEF(x, y, id);
if (worldToECEFMatrix) {
const tileCount = 1 << id.z;
const camX = index$1.mercatorXfromLng(tr.center.lng);
const camY = index$1.mercatorYfromLat(tr.center.lat);
const tileCenterX = (id.x + 0.5) / tileCount;
const dx = tileCenterX - camX;
let wrap = 0;
if (dx > 0.5) {
wrap = -1;
} else if (dx < -0.5) {
wrap = 1;
}
let mercatorX = (x / index$1.EXTENT + id.x) / tileCount + wrap;
let mercatorY = (y / index$1.EXTENT + id.y) / tileCount;
mercatorX = (mercatorX - camX) * tr._pixelsPerMercatorPixel + camX;
mercatorY = (mercatorY - camY) * tr._pixelsPerMercatorPixel + camY;
const mercatorPos = [mercatorX * tr.worldSize, mercatorY * tr.worldSize, 0];
index$1.transformMat4(mercatorPos, mercatorPos, worldToECEFMatrix);
ecef = index$1.interpolateVec3(ecef, mercatorPos, phase);
}
const gp = index$1.transformMat4(ecef, ecef, normalizationMatrix);
return gp;
}
_makeGlobeTileDebugBorderBuffer(context, id, tr, normalizationMatrix, worldToECEFMatrix, phase) {
const vertices = new index$1.StructArrayLayout2i4();
const indices = new index$1.StructArrayLayout1ui2();
const extraGlobe = new index$1.StructArrayLayout3i6();
const addLine = (sx, sy, ex, ey, pointCount) => {
const stepX = (ex - sx) / (pointCount - 1);
const stepY = (ey - sy) / (pointCount - 1);
const vOffset = vertices.length;
for (let i = 0; i < pointCount; i++) {
const x = sx + i * stepX;
const y = sy + i * stepY;
vertices.emplaceBack(x, y);
const gp = this._globePoint(x, y, id, tr, normalizationMatrix, worldToECEFMatrix, phase);
extraGlobe.emplaceBack(gp[0], gp[1], gp[2]);
indices.emplaceBack(vOffset + i);
}
};
const e = index$1.EXTENT;
addLine(0, 0, e, 0, 16);
addLine(e, 0, e, e, 16);
addLine(e, e, 0, e, 16);
addLine(0, e, 0, 0, 16);
this._tileDebugIndexBuffer = context.createIndexBuffer(indices);
this._tileDebugBuffer = context.createVertexBuffer(vertices, index$1.posAttributes.members);
this._globeTileDebugBorderBuffer = context.createVertexBuffer(extraGlobe, index$1.posAttributesGlobeExt.members);
this._tileDebugSegments = index$1.SegmentVector.simpleSegment(0, 0, vertices.length, indices.length);
}
_makeGlobeTileDebugTextBuffer(context, id, tr, normalizationMatrix, worldToECEFMatrix, phase) {
const SEGMENTS = 4;
const numVertices = SEGMENTS + 1;
const step = index$1.EXTENT / SEGMENTS;
const vertices = new index$1.StructArrayLayout2i4();
const indices = new index$1.StructArrayLayout3ui6();
const extraGlobe = new index$1.StructArrayLayout3i6();
const totalVertices = numVertices * numVertices;
const totalTriangles = SEGMENTS * SEGMENTS * 2;
indices.reserve(totalTriangles);
vertices.reserve(totalVertices);
extraGlobe.reserve(totalVertices);
const toIndex = (j, i) => {
return totalVertices * j + i;
};
for (let j = 0; j < totalVertices; j++) {
const y = j * step;
for (let i = 0; i < totalVertices; i++) {
const x = i * step;
vertices.emplaceBack(x, y);
const gp = this._globePoint(x, y, id, tr, normalizationMatrix, worldToECEFMatrix, phase);
extraGlobe.emplaceBack(gp[0], gp[1], gp[2]);
}
}
for (let j = 0; j < SEGMENTS; j++) {
for (let i = 0; i < SEGMENTS; i++) {
const tl = toIndex(j, i);
const tr2 = toIndex(j, i + 1);
const bl = toIndex(j + 1, i);
const br = toIndex(j + 1, i + 1);
indices.emplaceBack(tl, tr2, bl);
indices.emplaceBack(bl, tr2, br);
}
}
this._tileDebugTextIndexBuffer = context.createIndexBuffer(indices);
this._tileDebugTextBuffer = context.createVertexBuffer(vertices, index$1.posAttributes.members);
this._globeTileDebugTextBuffer = context.createVertexBuffer(extraGlobe, index$1.posAttributesGlobeExt.members);
this._tileDebugTextSegments = index$1.SegmentVector.simpleSegment(0, 0, totalVertices, totalTriangles);
}
/**
* Release data and WebGL resources referenced by this tile.
* @returns {undefined}
* @private
*/
destroy(reload = true) {
for (const id in this.buckets) {
this.buckets[id].destroy(reload);
}
this.buckets = {};
if (this.imageAtlas) {
this.imageAtlas = null;
}
if (this.lineAtlas) {
this.lineAtlas = null;
}
if (this.imageAtlasTexture) {
this.imageAtlasTexture.destroy();
delete this.imageAtlasTexture;
}
if (this.glyphAtlasTexture) {
this.glyphAtlasTexture.destroy();
delete this.glyphAtlasTexture;
}
if (this.lineAtlasTexture) {
this.lineAtlasTexture.destroy();
delete this.lineAtlasTexture;
}
if (this._tileBoundsBuffer) {
this._tileBoundsBuffer.destroy();
this._tileBoundsIndexBuffer.destroy();
this._tileBoundsSegments.destroy();
this._tileBoundsBuffer = null;
}
if (this._tileDebugBuffer) {
this._tileDebugBuffer.destroy();
this._tileDebugSegments.destroy();
this._tileDebugBuffer = null;
}
if (this._tileDebugIndexBuffer) {
this._tileDebugIndexBuffer.destroy();
this._tileDebugIndexBuffer = null;
}
if (this._globeTileDebugBorderBuffer) {
this._globeTileDebugBorderBuffer.destroy();
this._globeTileDebugBorderBuffer = null;
}
if (this._tileDebugTextBuffer) {
this._tileDebugTextBuffer.destroy();
this._tileDebugTextSegments.destroy();
this._tileDebugTextIndexBuffer.destroy();
this._tileDebugTextBuffer = null;
}
if (this._globeTileDebugTextBuffer) {
this._globeTileDebugTextBuffer.destroy();
this._globeTileDebugTextBuffer = null;
}
if (reload && this.texture && this.texture instanceof index$1.Texture) {
this.texture.destroy();
delete this.texture;
}
if (this.emissiveTexture && this.emissiveTexture instanceof index$1.Texture) {
this.emissiveTexture.destroy();
delete this.emissiveTexture;
}
if (this.hillshadeFBO) {
this.hillshadeFBO.destroy();
delete this.hillshadeFBO;
}
if (this.dem) {
delete this.dem;
}
if (this.neighboringTiles) {
delete this.neighboringTiles;
}
if (this.demTexture) {
this.demTexture.destroy();
delete this.demTexture;
}
if (this.rasterParticleState) {
this.rasterParticleState.destroy();
delete this.rasterParticleState;
}
Debug.run(() => {
if (this.queryGeometryDebugViz) {
this.queryGeometryDebugViz.unload();
delete this.queryGeometryDebugViz;
}
if (this.queryBoundsDebugViz) {
this.queryBoundsDebugViz.unload();
delete this.queryBoundsDebugViz;
}
});
this.latestFeatureIndex = null;
this.state = "unloaded";
}
}
index$1.MapboxRasterTile.setPbf(index$1.Pbf);
const FIRST_TRY_HEADER_LENGTH = 16384;
const MRT_DECODED_BAND_CACHE_SIZE = 30;
class RasterArrayTile extends Tile {
constructor(tileID, size, tileZoom, painter, isRaster) {
super(tileID, size, tileZoom, painter, isRaster);
this._workQueuePerLayer = /* @__PURE__ */ new Map();
this._fetchQueuePerLayer = /* @__PURE__ */ new Map();
this._taskQueue = /* @__PURE__ */ new Map();
this._isHeaderLoaded = false;
this.textureDescriptorPerLayer = /* @__PURE__ */ new Map();
this.texturePerLayer = /* @__PURE__ */ new Map();
}
/**
* Returns a map of all layers in the raster array tile.
* @returns {Record}
* @private
*/
getLayers() {
return this._mrt ? Object.values(this._mrt.layers) : [];
}
/**
* Returns a layer in the raster array tile.
* @param {string} layerId
* @returns {MapboxRasterLayer | null | undefined}
* @private
*/
getLayer(layerId) {
return this._mrt && this._mrt.getLayer(layerId);
}
/**
* @private
*/
setTexturePerLayer(sourceLayer, img, painter) {
const context = painter.context;
const gl = context.gl;
let texture = this.texturePerLayer.get(sourceLayer) || painter.getTileTexture(img.width);
if (texture && texture instanceof index$1.Texture) {
texture.update(img, { premultiply: false });
} else {
texture = new index$1.Texture(context, img, gl.RGBA8, { premultiply: false });
}
if (!this.texturePerLayer.has(sourceLayer)) {
this.texturePerLayer.set(sourceLayer, texture);
}
}
/**
* Stops existing fetches
* @private
*/
flushQueues(sourceLayer) {
const workQueue = this._workQueuePerLayer.get(sourceLayer) || [];
const fetchQueue = this._fetchQueuePerLayer.get(sourceLayer) || [];
while (workQueue.length) {
workQueue.pop()();
}
while (fetchQueue.length) {
fetchQueue.pop()();
}
}
/**
* @private
*/
flushAllQueues() {
for (const sourceLayer of this._workQueuePerLayer.keys()) {
const workQueue = this._workQueuePerLayer.get(sourceLayer) || [];
while (workQueue.length) {
workQueue.pop()();
}
}
for (const sourceLayer of this._fetchQueuePerLayer.keys()) {
const fetchQueue = this._fetchQueuePerLayer.get(sourceLayer) || [];
while (fetchQueue.length) {
fetchQueue.pop()();
}
}
}
fetchHeader(fetchLength = FIRST_TRY_HEADER_LENGTH, callback) {
const mrt = this._mrt = new index$1.MapboxRasterTile(MRT_DECODED_BAND_CACHE_SIZE);
const headerRequestParams = Object.assign({}, this.requestParams, { headers: { Range: `bytes=0-${fetchLength - 1}` } });
this.entireBuffer = null;
this.request = index$1.getArrayBuffer(headerRequestParams, (error, dataBuffer, headers) => {
if (error) {
callback(error);
return;
}
try {
const headerLength = mrt.getHeaderLength(dataBuffer);
if (headerLength > fetchLength) {
this.request = this.fetchHeader(headerLength, callback);
return;
}
mrt.parseHeader(dataBuffer);
this._isHeaderLoaded = true;
let lastByte = 0;
for (const layer of Object.values(mrt.layers)) {
lastByte = Math.max(lastByte, layer.dataIndex[layer.dataIndex.length - 1].lastByte);
}
if (dataBuffer.byteLength >= lastByte) {
this.entireBuffer = dataBuffer;
}
callback(null, this.entireBuffer || dataBuffer, headers);
} catch (error2) {
callback(error2);
}
});
return this.request;
}
fetchBandForRender(sourceLayer, layerId, band, callback) {
this.fetchBand(sourceLayer, layerId, band, (err) => {
if (err) {
callback(err);
return;
}
this.updateTextureDescriptor(sourceLayer, layerId, band);
const textureDescriptor = this.textureDescriptorPerLayer.get(layerId);
callback(null, textureDescriptor ? textureDescriptor.img : null);
});
}
fetchBand(sourceLayer, layerId, band, callback, cancelable = true) {
const mrt = this._mrt;
if (!this._isHeaderLoaded || !mrt) {
callback(new Error("Tile header is not ready"));
return;
}
const actor = this.actor;
if (!actor) {
callback(new Error("Can't fetch tile band without an actor"));
return;
}
let task;
const taskQueueId = index$1.makeFQID(String(band), index$1.makeFQID(this.tileID.key, sourceLayer));
let taskInQueue = this._taskQueue.get(taskQueueId);
if (!taskInQueue) {
taskInQueue = /* @__PURE__ */ new Set();
taskInQueue.add(callback);
this._taskQueue.set(taskQueueId, taskInQueue);
} else {
taskInQueue.add(callback);
}
const onDataDecoded = (err, result) => {
task.complete(err, result);
if (err) {
callback(err);
return;
}
taskInQueue.forEach((cb) => cb(null, result));
this._taskQueue.delete(taskQueueId);
};
const onDataLoaded = (err, buffer) => {
if (err) return callback(err);
const params = {
type: "raster-array",
source: this.source,
scope: this.scope,
tileID: this.tileID,
uid: this.uid,
buffer,
task
};
const workerJob = actor.send("decodeRasterArray", params, onDataDecoded, void 0, true);
if (layerId !== null) {
const workQueue = this._workQueuePerLayer.get(layerId) || [];
workQueue.push(() => {
if (workerJob) workerJob.cancel();
task.cancel();
});
if (!this._workQueuePerLayer.has(layerId)) {
this._workQueuePerLayer.set(layerId, workQueue);
}
}
};
let mrtLayer;
try {
mrtLayer = mrt.getLayer(sourceLayer);
} catch (err) {
if (this.state === "reloading") {
return;
}
throw err;
}
if (!mrtLayer) {
callback(new Error(`Unknown sourceLayer "${sourceLayer}"`));
return;
}
if (mrtLayer.hasDataForBand(band)) {
taskInQueue.forEach((cb) => cb(null, null));
this._taskQueue.delete(taskQueueId);
return;
}
const range = mrtLayer.getDataRange([band]);
task = mrt.createDecodingTask(range);
if (task && !task.tasks.length) {
return;
}
if (layerId !== null) {
this.flushQueues(layerId);
}
if (this.entireBuffer) {
onDataLoaded(null, this.entireBuffer.slice(range.firstByte, range.lastByte + 1));
} else {
const rangeRequestParams = Object.assign({}, this.requestParams, { headers: { Range: `bytes=${range.firstByte}-${range.lastByte}` } });
const request = index$1.getArrayBuffer(rangeRequestParams, onDataLoaded);
if (layerId !== null) {
const fetchQueue = this._fetchQueuePerLayer.get(layerId) || [];
fetchQueue.push(() => {
request.cancel();
task.cancel();
});
if (!this._fetchQueuePerLayer.has(layerId)) {
this._fetchQueuePerLayer.set(layerId, fetchQueue);
}
}
}
}
updateNeeded(layerId, band) {
const textureUpdateNeeded = !this.textureDescriptorPerLayer.get(layerId) || this.textureDescriptorPerLayer.get(layerId).band !== band || this.refreshedUponExpiration;
return textureUpdateNeeded && this.state !== "errored";
}
updateTextureDescriptor(sourceLayer, layerId, band) {
if (!this._mrt) return;
const mrtLayer = this._mrt.getLayer(sourceLayer);
if (!mrtLayer || !mrtLayer.hasBand(band) || !mrtLayer.hasDataForBand(band)) return;
const { bytes, tileSize, buffer, offset, scale } = mrtLayer.getBandView(band);
const size = tileSize + 2 * buffer;
const img = new index$1.RGBAImage({ width: size, height: size }, bytes);
const texture = this.texturePerLayer.get(layerId);
if (texture && texture instanceof index$1.Texture) {
texture.update(img, { premultiply: false });
}
this.textureDescriptorPerLayer.set(layerId, {
layer: sourceLayer,
band,
img,
buffer,
offset,
tileSize,
format: mrtLayer.pixelFormat,
mix: [
scale,
scale * 256,
scale * 65536,
scale * 16777216
]
});
}
destroy(preserveTexture = false) {
super.destroy(preserveTexture);
delete this._mrt;
if (!preserveTexture) {
for (const texture of this.texturePerLayer.values()) {
if (texture && texture instanceof index$1.Texture) {
texture.destroy();
}
}
}
this.texturePerLayer.clear();
this.textureDescriptorPerLayer.clear();
if (this.fbo) {
this.fbo.destroy();
delete this.fbo;
}
delete this.request;
delete this.requestParams;
this._isHeaderLoaded = false;
}
}
class TileCache {
/**
* @param {number} max The max number of permitted values.
* @private
* @param {Function} onRemove The callback called with items when they expire.
*/
constructor(max, onRemove) {
this.max = max;
this.onRemove = onRemove;
this.reset();
}
/**
* Clear the cache.
*
* @returns {TileCache} Returns itself to allow for method chaining.
* @private
*/
reset() {
for (const key in this.data) {
for (const removedData of this.data[key]) {
if (removedData.timeout) clearTimeout(removedData.timeout);
this.onRemove(removedData.value);
}
}
this.data = {};
this.order = [];
return this;
}
/**
* Add a key, value combination to the cache, trimming its size if this pushes
* it over max length.
*
* @param {OverscaledTileID} tileID lookup key for the item
* @param {*} data any value
*
* @returns {TileCache} Returns itself to allow for method chaining.
* @private
*/
add(tileID, data, expiryTimeout) {
const key = tileID.wrapped().key;
if (this.data[key] === void 0) {
this.data[key] = [];
}
const dataWrapper = {
value: data,
timeout: void 0
};
if (expiryTimeout !== void 0) {
dataWrapper.timeout = setTimeout(() => {
this.remove(tileID, dataWrapper);
}, expiryTimeout);
}
this.data[key].push(dataWrapper);
this.order.push(key);
if (this.order.length > this.max) {
const removedData = this._getAndRemoveByKey(this.order[0]);
if (removedData) this.onRemove(removedData);
}
return this;
}
/**
* Determine whether the value attached to `key` is present
*
* @param {OverscaledTileID} tileID the key to be looked-up
* @returns {boolean} whether the cache has this value
* @private
*/
has(tileID) {
return tileID.wrapped().key in this.data;
}
/**
* Get the value attached to a specific key and remove data from cache.
* If the key is not found, returns `null`
*
* @param {OverscaledTileID} tileID the key to look up
* @returns {*} the data, or null if it isn't found
* @private
*/
getAndRemove(tileID) {
if (!this.has(tileID)) {
return null;
}
return this._getAndRemoveByKey(tileID.wrapped().key);
}
/*
* Get and remove the value with the specified key.
*/
_getAndRemoveByKey(key) {
const data = this.data[key].shift();
if (data.timeout) clearTimeout(data.timeout);
if (this.data[key].length === 0) {
delete this.data[key];
}
this.order.splice(this.order.indexOf(key), 1);
return data.value;
}
/*
* Get the value with the specified (wrapped tile) key.
*/
getByKey(key) {
const data = this.data[key];
return data ? data[0].value : null;
}
/**
* Get the value attached to a specific key without removing data
* from the cache. If the key is not found, returns `null`
*
* @param {OverscaledTileID} tileID the key to look up
* @returns {*} the data, or null if it isn't found
* @private
*/
get(tileID) {
if (!this.has(tileID)) {
return null;
}
const data = this.data[tileID.wrapped().key][0];
return data.value;
}
/**
* Remove a key/value combination from the cache.
*
* @param {OverscaledTileID} tileID the key for the pair to delete
* @param {Tile} value If a value is provided, remove that exact version of the value.
* @returns {TileCache} this cache
* @private
*/
remove(tileID, value) {
if (!this.has(tileID)) {
return this;
}
const key = tileID.wrapped().key;
const dataIndex = value === void 0 ? 0 : this.data[key].indexOf(value);
const data = this.data[key][dataIndex];
this.data[key].splice(dataIndex, 1);
if (data.timeout) clearTimeout(data.timeout);
if (this.data[key].length === 0) {
delete this.data[key];
}
this.onRemove(data.value);
this.order.splice(this.order.indexOf(key), 1);
return this;
}
/**
* Change the max size of the cache.
*
* @param {number} max the max size of the cache
* @returns {TileCache} this cache
* @private
*/
setMaxSize(max) {
this.max = max;
while (this.order.length > this.max) {
const removedData = this._getAndRemoveByKey(this.order[0]);
if (removedData) this.onRemove(removedData);
}
return this;
}
/**
* Remove entries that do not pass a filter function. Used for removing
* stale tiles from the cache.
*
* @private
* @param {function} filterFn Determines whether the tile is filtered. If the supplied function returns false, the tile will be filtered out.
*/
filter(filterFn) {
const removed = [];
for (const key in this.data) {
for (const entry of this.data[key]) {
if (!filterFn(entry.value)) {
removed.push(entry);
}
}
}
for (const r of removed) {
this.remove(r.value.tileID, r);
}
}
}
class SourceFeatureState {
constructor() {
this.state = {};
this.stateChanges = {};
this.deletedStates = {};
}
updateState(sourceLayer, featureId, newState) {
const feature = String(featureId);
this.stateChanges[sourceLayer] = this.stateChanges[sourceLayer] || {};
this.stateChanges[sourceLayer][feature] = this.stateChanges[sourceLayer][feature] || {};
Object.assign(this.stateChanges[sourceLayer][feature], newState);
if (this.deletedStates[sourceLayer] === null) {
this.deletedStates[sourceLayer] = {};
for (const ft in this.state[sourceLayer]) {
if (ft !== feature) this.deletedStates[sourceLayer][ft] = null;
}
} else {
const featureDeletionQueued = this.deletedStates[sourceLayer] && this.deletedStates[sourceLayer][feature] === null;
if (featureDeletionQueued) {
this.deletedStates[sourceLayer][feature] = {};
for (const prop in this.state[sourceLayer][feature]) {
if (!newState[prop]) this.deletedStates[sourceLayer][feature][prop] = null;
}
} else {
for (const key in newState) {
const deletionInQueue = this.deletedStates[sourceLayer] && this.deletedStates[sourceLayer][feature] && this.deletedStates[sourceLayer][feature][key] === null;
if (deletionInQueue) delete this.deletedStates[sourceLayer][feature][key];
}
}
}
}
removeFeatureState(sourceLayer, featureId, key) {
const sourceLayerDeleted = this.deletedStates[sourceLayer] === null;
if (sourceLayerDeleted) return;
const feature = String(featureId);
this.deletedStates[sourceLayer] = this.deletedStates[sourceLayer] || {};
if (key && featureId !== void 0) {
if (this.deletedStates[sourceLayer][feature] !== null) {
this.deletedStates[sourceLayer][feature] = this.deletedStates[sourceLayer][feature] || {};
this.deletedStates[sourceLayer][feature][key] = null;
}
} else if (featureId !== void 0) {
const updateInQueue = this.stateChanges[sourceLayer] && this.stateChanges[sourceLayer][feature];
if (updateInQueue) {
this.deletedStates[sourceLayer][feature] = {};
for (key in this.stateChanges[sourceLayer][feature]) this.deletedStates[sourceLayer][feature][key] = null;
} else {
this.deletedStates[sourceLayer][feature] = null;
}
} else {
this.deletedStates[sourceLayer] = null;
}
}
getState(sourceLayer, featureId) {
const base = this.state[sourceLayer] || {};
const changes = this.stateChanges[sourceLayer] || {};
const deletedStates = this.deletedStates[sourceLayer];
if (deletedStates === null) return {};
if (featureId !== void 0) {
const feature = String(featureId);
const reconciledState2 = Object.assign({}, base[feature], changes[feature]);
if (deletedStates) {
const featureDeletions = deletedStates[featureId];
if (featureDeletions === null) return {};
for (const prop in featureDeletions) delete reconciledState2[prop];
}
return reconciledState2;
}
const reconciledState = Object.assign({}, base, changes);
if (deletedStates) {
for (const feature in deletedStates) delete reconciledState[feature];
}
return reconciledState;
}
initializeTileState(tile, painter) {
tile.refreshFeatureState(painter);
}
coalesceChanges(tiles, painter) {
const featuresChanged = {};
for (const sourceLayer in this.stateChanges) {
this.state[sourceLayer] = this.state[sourceLayer] || {};
const layerStates = {};
for (const feature in this.stateChanges[sourceLayer]) {
if (!this.state[sourceLayer][feature]) this.state[sourceLayer][feature] = {};
Object.assign(this.state[sourceLayer][feature], this.stateChanges[sourceLayer][feature]);
layerStates[feature] = this.state[sourceLayer][feature];
}
featuresChanged[sourceLayer] = layerStates;
}
for (const sourceLayer in this.deletedStates) {
this.state[sourceLayer] = this.state[sourceLayer] || {};
const layerStates = {};
if (this.deletedStates[sourceLayer] === null) {
for (const ft in this.state[sourceLayer]) {
layerStates[ft] = {};
this.state[sourceLayer][ft] = {};
}
} else {
for (const feature in this.deletedStates[sourceLayer]) {
const deleteWholeFeatureState = this.deletedStates[sourceLayer][feature] === null;
if (deleteWholeFeatureState) this.state[sourceLayer][feature] = {};
else if (this.state[sourceLayer][feature]) {
for (const key of Object.keys(this.deletedStates[sourceLayer][feature])) {
delete this.state[sourceLayer][feature][key];
}
}
layerStates[feature] = this.state[sourceLayer][feature];
}
}
featuresChanged[sourceLayer] = featuresChanged[sourceLayer] || {};
Object.assign(featuresChanged[sourceLayer], layerStates);
}
this.stateChanges = {};
this.deletedStates = {};
if (Object.keys(featuresChanged).length === 0) return;
for (const id in tiles) {
const tile = tiles[id];
tile.refreshFeatureState(painter);
}
}
}
class SourceCache extends index$1.Evented {
constructor(id, source, onlySymbols) {
super();
this.id = id;
this._onlySymbols = onlySymbols;
source.on("data", (e) => {
if (e.dataType === "source" && e.sourceDataType === "metadata") this._sourceLoaded = true;
if (this._sourceLoaded && !this._paused && e.dataType === "source" && e.sourceDataType === "content") {
this.reload();
if (this.transform) {
this.update(this.transform);
}
}
});
source.on("error", () => {
this._sourceErrored = true;
});
this._source = source;
this._tiles = {};
this._cache = new TileCache(0, this._unloadTile.bind(this));
this._timers = {};
this._cacheTimers = {};
this._minTileCacheSize = source.minTileCacheSize;
this._maxTileCacheSize = source.maxTileCacheSize;
this._loadedParentTiles = {};
this.castsShadows = false;
this.tileCoverLift = 0;
this._coveredTiles = {};
this._shadowCasterTiles = {};
this._state = new SourceFeatureState();
this._isRaster = this._source.type === "raster" || this._source.type === "raster-dem" || this._source.type === "raster-array" || // @ts-expect-error - TS2339 - Property '_dataType' does not exist on type 'VideoSource | ImageSource | CanvasSource | CustomSource'.
this._source.type === "custom" && this._source._dataType === "raster";
}
onAdd(map) {
this.map = map;
this._minTileCacheSize = this._minTileCacheSize === void 0 && map ? map._minTileCacheSize : this._minTileCacheSize;
this._maxTileCacheSize = this._maxTileCacheSize === void 0 && map ? map._maxTileCacheSize : this._maxTileCacheSize;
}
/**
* Return true if no tile data is pending, tiles will not change unless
* an additional API call is received.
* @private
*/
loaded() {
if (this._sourceErrored) {
return true;
}
if (!this._sourceLoaded) {
return false;
}
if (!this._source.loaded()) {
return false;
}
for (const t in this._tiles) {
const tile = this._tiles[t];
if (!tile.loaded()) return false;
}
return true;
}
getSource() {
return this._source;
}
pause() {
this._paused = true;
}
resume() {
if (!this._paused) return;
const shouldReload = this._shouldReloadOnResume;
this._paused = false;
this._shouldReloadOnResume = false;
if (shouldReload) this.reload();
if (this.transform) this.update(this.transform);
}
_loadTile(tile, callback) {
tile.isSymbolTile = this._onlySymbols;
tile.isExtraShadowCaster = this._shadowCasterTiles[tile.tileID.key];
return this._source.loadTile(tile, callback);
}
_unloadTile(tile) {
if (this._source.unloadTile)
return this._source.unloadTile(tile);
}
_abortTile(tile) {
if (this._source.abortTile)
return this._source.abortTile(tile);
}
serialize() {
return this._source.serialize();
}
prepare(context) {
if (this._source.prepare) {
this._source.prepare();
}
this._state.coalesceChanges(this._tiles, this.map ? this.map.painter : null);
for (const i in this._tiles) {
const tile = this._tiles[i];
tile.upload(context, this.map ? this.map.painter : void 0);
tile.prepare(this.map.style.imageManager, this.map ? this.map.painter : null, this._source.scope);
}
}
/**
* Return all tile ids ordered with z-order, and cast to numbers
* @private
*/
getIds() {
return Object.values(this._tiles).map((tile) => tile.tileID).sort(compareTileId).map((id) => id.key);
}
getRenderableIds(symbolLayer, includeShadowCasters) {
const renderables = [];
for (const id in this._tiles) {
if (this._isIdRenderable(+id, symbolLayer, includeShadowCasters)) renderables.push(this._tiles[id]);
}
if (symbolLayer) {
return renderables.sort((a_, b_) => {
const a = a_.tileID;
const b = b_.tileID;
const rotatedA = new index$1.Point(a.canonical.x, a.canonical.y)._rotate(this.transform.angle);
const rotatedB = new index$1.Point(b.canonical.x, b.canonical.y)._rotate(this.transform.angle);
return a.overscaledZ - b.overscaledZ || rotatedB.y - rotatedA.y || rotatedB.x - rotatedA.x;
}).map((tile) => tile.tileID.key);
}
return renderables.map((tile) => tile.tileID).sort(compareTileId).map((id) => id.key);
}
hasRenderableParent(tileID) {
const parentTile = this.findLoadedParent(tileID, 0);
if (parentTile) {
return this._isIdRenderable(parentTile.tileID.key);
}
return false;
}
_isIdRenderable(id, symbolLayer, includeShadowCasters) {
return this._tiles[id] && this._tiles[id].hasData() && !this._coveredTiles[id] && (symbolLayer || !this._tiles[id].holdingForFade()) && (includeShadowCasters || !this._shadowCasterTiles[id]);
}
reload() {
if (this._paused) {
this._shouldReloadOnResume = true;
return;
}
this._cache.reset();
for (const i in this._tiles) {
if (this._tiles[i].state !== "errored") this._reloadTile(+i, "reloading");
}
}
_reloadTile(id, state) {
const tile = this._tiles[id];
if (!tile) return;
if (tile.state !== "loading") {
tile.state = state;
}
this._loadTile(tile, this._tileLoaded.bind(this, tile, id, state));
}
_tileLoaded(tile, id, previousState, err, data) {
if (err) {
tile.state = "errored";
if (err.status !== 404) this._source.fire(new index$1.ErrorEvent(err, { tile }));
else {
this._source.fire(new index$1.Event("data", { dataType: "source", sourceDataType: "error", sourceId: this._source.id, tile }));
const hasParent = tile.tileID.key in this._loadedParentTiles;
if (!hasParent) return;
const updateForTerrain = this._source.type === "raster-dem" && this.usedForTerrain;
if (updateForTerrain && this.map.painter.terrain) {
const terrain = this.map.painter.terrain;
this.update(this.transform, terrain.getScaledDemTileSize(), true);
terrain.resetTileLookupCache(this.id);
} else {
this.update(this.transform);
}
}
return;
}
tile.timeAdded = index$1.exported$1.now();
if (previousState === "expired") tile.refreshedUponExpiration = true;
this._setTileReloadTimer(id, tile);
if (this._source.type === "raster-dem" && tile.dem) this._backfillDEM(tile);
this._state.initializeTileState(tile, this.map ? this.map.painter : null);
let responseHeaders = /* @__PURE__ */ new Map();
if (data && data.responseHeaders) responseHeaders = data.responseHeaders;
this._source.fire(new index$1.Event("data", { dataType: "source", tile, coord: tile.tileID, "sourceCacheId": this.id, responseHeaders }));
}
/**
* For raster terrain source, backfill DEM to eliminate visible tile boundaries
* @private
*/
_backfillDEM(tile) {
const renderables = this.getRenderableIds();
for (let i = 0; i < renderables.length; i++) {
const borderId = renderables[i];
if (tile.neighboringTiles && tile.neighboringTiles[borderId]) {
const borderTile = this.getTileByID(borderId);
fillBorder(tile, borderTile);
fillBorder(borderTile, tile);
}
}
function fillBorder(tile2, borderTile) {
if (!tile2.dem || tile2.dem.borderReady) return;
tile2.needsHillshadePrepare = true;
tile2.needsDEMTextureUpload = true;
let dx = borderTile.tileID.canonical.x - tile2.tileID.canonical.x;
const dy = borderTile.tileID.canonical.y - tile2.tileID.canonical.y;
const dim = Math.pow(2, tile2.tileID.canonical.z);
const borderId = borderTile.tileID.key;
if (dx === 0 && dy === 0) return;
if (Math.abs(dy) > 1) {
return;
}
if (Math.abs(dx) > 1) {
if (Math.abs(dx + dim) === 1) {
dx += dim;
} else if (Math.abs(dx - dim) === 1) {
dx -= dim;
}
}
if (!borderTile.dem || !tile2.dem) return;
tile2.dem.backfillBorder(borderTile.dem, dx, dy);
if (tile2.neighboringTiles && tile2.neighboringTiles[borderId])
tile2.neighboringTiles[borderId].backfilled = true;
}
}
/**
* Get a specific tile by TileID
* @private
*/
getTile(tileID) {
return this.getTileByID(tileID.key);
}
/**
* Get a specific tile by id
* @private
*/
getTileByID(id) {
return this._tiles[id];
}
/**
* For a given set of tiles, retain children that are loaded and have a zoom
* between `zoom` (exclusive) and `maxCoveringZoom` (inclusive)
* @private
*/
_retainLoadedChildren(idealTiles, zoom, maxCoveringZoom, retain) {
for (const id in this._tiles) {
let tile = this._tiles[id];
if (retain[id] || !tile.hasData() || tile.tileID.overscaledZ <= zoom || tile.tileID.overscaledZ > maxCoveringZoom) continue;
let topmostLoadedID = tile.tileID;
while (tile && tile.tileID.overscaledZ > zoom + 1) {
const parentID = tile.tileID.scaledTo(tile.tileID.overscaledZ - 1);
tile = this._tiles[parentID.key];
if (tile && tile.hasData()) {
topmostLoadedID = parentID;
}
}
let tileID = topmostLoadedID;
while (tileID.overscaledZ > zoom) {
tileID = tileID.scaledTo(tileID.overscaledZ - 1);
if (idealTiles[tileID.key]) {
retain[topmostLoadedID.key] = topmostLoadedID;
break;
}
}
}
}
/**
* Find a loaded parent of the given tile (up to minCoveringZoom)
* @private
*/
findLoadedParent(tileID, minCoveringZoom) {
if (tileID.key in this._loadedParentTiles) {
const parent = this._loadedParentTiles[tileID.key];
if (parent && parent.tileID.overscaledZ >= minCoveringZoom) {
return parent;
} else {
return null;
}
}
for (let z = tileID.overscaledZ - 1; z >= minCoveringZoom; z--) {
const parentTileID = tileID.scaledTo(z);
const tile = this._getLoadedTile(parentTileID);
if (tile) {
return tile;
}
}
}
_getLoadedTile(tileID) {
const tile = this._tiles[tileID.key];
if (tile && tile.hasData()) {
return tile;
}
const cachedTile = this._cache.getByKey(this._source.reparseOverscaled ? tileID.wrapped().key : tileID.canonical.key);
return cachedTile;
}
/**
* Resizes the tile cache based on the current viewport's size
* or the minTileCacheSize and maxTileCacheSize options passed during map creation
*
* Larger viewports use more tiles and need larger caches. Larger viewports
* are more likely to be found on devices with more memory and on pages where
* the map is more important.
* @private
*/
updateCacheSize(transform, tileSize) {
tileSize = tileSize || this._source.tileSize;
const widthInTiles = Math.ceil(transform.width / tileSize) + 1;
const heightInTiles = Math.ceil(transform.height / tileSize) + 1;
const approxTilesInView = widthInTiles * heightInTiles;
const commonZoomRange = 5;
const viewDependentMaxSize = Math.floor(approxTilesInView * commonZoomRange);
const minSize = typeof this._minTileCacheSize === "number" ? Math.max(this._minTileCacheSize, viewDependentMaxSize) : viewDependentMaxSize;
const maxSize = typeof this._maxTileCacheSize === "number" ? Math.min(this._maxTileCacheSize, minSize) : minSize;
this._cache.setMaxSize(maxSize);
}
handleWrapJump(lng) {
const prevLng = this._prevLng === void 0 ? lng : this._prevLng;
const lngDifference = lng - prevLng;
const worldDifference = lngDifference / 360;
const wrapDelta = Math.round(worldDifference);
this._prevLng = lng;
if (wrapDelta) {
const tiles = {};
for (const key in this._tiles) {
const tile = this._tiles[key];
tile.tileID = tile.tileID.unwrapTo(tile.tileID.wrap + wrapDelta);
tiles[tile.tileID.key] = tile;
}
this._tiles = tiles;
for (const id in this._timers) {
clearTimeout(this._timers[id]);
delete this._timers[id];
}
for (const id in this._tiles) {
const tile = this._tiles[id];
this._setTileReloadTimer(+id, tile);
}
}
}
/**
* Removes tiles that are outside the viewport and adds new tiles that
* are inside the viewport.
* @private
* @param {boolean} updateForTerrain Signals to update tiles even if the
* source is not used (this.used) by layers: it is used for terrain.
* @param {tileSize} tileSize If needed to get lower resolution ideal cover,
* override source.tileSize used in tile cover calculation.
*/
update(transform, tileSize, updateForTerrain, directionalLight, elevatedLayers) {
this.transform = transform;
if (!this._sourceLoaded || this._paused || this.transform.freezeTileCoverage) {
return;
}
index$1.assert(!(updateForTerrain && !this.usedForTerrain));
if (this.usedForTerrain && !updateForTerrain) {
return;
}
this.updateCacheSize(transform, tileSize);
if (this.transform.projection.name !== "globe") {
this.handleWrapJump(this.transform.center.lng);
}
this._shadowCasterTiles = {};
this._coveredTiles = {};
const isBatchedModelType = this._source.type === "batched-model";
let idealTileIDs;
let maxZoom = this._source.maxzoom;
const terrain = this.map && this.map.painter ? this.map.painter._terrain : null;
const sourceUsedForTerrain = terrain && terrain.sourceCache === this;
if (sourceUsedForTerrain && terrain.attenuationRange()) {
const minAttenuationZoom = terrain.attenuationRange()[0];
const demMaxZoom = Math.floor(minAttenuationZoom) - Math.log2(terrain.getDemUpscale());
if (maxZoom > demMaxZoom) {
maxZoom = demMaxZoom;
}
}
if (!this.used && !this.usedForTerrain) {
idealTileIDs = [];
} else if (this._source.tileID) {
idealTileIDs = transform.getVisibleUnwrappedCoordinates(this._source.tileID).map((unwrapped) => new index$1.OverscaledTileID(unwrapped.canonical.z, unwrapped.wrap, unwrapped.canonical.z, unwrapped.canonical.x, unwrapped.canonical.y));
} else if (this.tileCoverLift !== 0) {
const modifiedTransform = transform.clone();
modifiedTransform.tileCoverLift = this.tileCoverLift;
idealTileIDs = modifiedTransform.coveringTiles({
tileSize: tileSize || this._source.tileSize,
minzoom: this._source.minzoom,
maxzoom: maxZoom,
roundZoom: this._source.roundZoom && !updateForTerrain,
reparseOverscaled: this._source.reparseOverscaled,
isTerrainDEM: this.usedForTerrain,
calculateQuadrantVisibility: isBatchedModelType
});
if (this._source.minzoom <= 1 && transform.projection.name === "globe") {
idealTileIDs.push(new index$1.OverscaledTileID(1, 0, 1, 0, 0));
idealTileIDs.push(new index$1.OverscaledTileID(1, 0, 1, 1, 0));
idealTileIDs.push(new index$1.OverscaledTileID(1, 0, 1, 0, 1));
idealTileIDs.push(new index$1.OverscaledTileID(1, 0, 1, 1, 1));
}
} else {
idealTileIDs = transform.coveringTiles({
tileSize: tileSize || this._source.tileSize,
minzoom: this._source.minzoom,
maxzoom: maxZoom,
roundZoom: this._source.roundZoom && !updateForTerrain,
reparseOverscaled: this._source.reparseOverscaled,
isTerrainDEM: this.usedForTerrain,
calculateQuadrantVisibility: isBatchedModelType
});
if (this._source.hasTile) {
const hasTile = this._source.hasTile.bind(this._source);
idealTileIDs = idealTileIDs.filter((coord) => hasTile(coord));
}
}
if (idealTileIDs.length > 0 && this.transform.projection.name !== "globe" && !this.usedForTerrain && !isRasterType(this._source.type)) {
const coveringZoom = transform.coveringZoomLevel({
tileSize: tileSize || this._source.tileSize,
roundZoom: this._source.roundZoom && !updateForTerrain
});
const idealZoom = Math.min(coveringZoom, this._source.maxzoom);
if (isBatchedModelType) {
const batchedModelTileIDs = transform.extendTileCover(idealTileIDs, idealZoom);
for (const id of batchedModelTileIDs) {
idealTileIDs.push(id);
}
} else if (elevatedLayers) {
const elevatedTileIDs = transform.extendTileCoverToNearPlane(idealTileIDs, this.transform.getFrustum(idealZoom), idealZoom);
for (const id of elevatedTileIDs) {
idealTileIDs.push(id);
}
} else if (this.castsShadows && directionalLight) {
const SHADOWS_MIN_ZOOM_EXTRA_TILES = 16;
const shadowCasterTileIDs = transform.extendTileCover(idealTileIDs, idealZoom, directionalLight, SHADOWS_MIN_ZOOM_EXTRA_TILES);
for (const id of shadowCasterTileIDs) {
this._shadowCasterTiles[id.key] = true;
idealTileIDs.push(id);
}
}
}
const retain = this._updateRetainedTiles(idealTileIDs);
if (isRasterType(this._source.type) && idealTileIDs.length !== 0) {
const parentsForFading = {};
const fadingTiles = {};
const ids = Object.keys(retain);
for (const id of ids) {
const tileID = retain[id];
index$1.assert(tileID.key === +id);
const tile = this._tiles[id];
if (!tile || tile.fadeEndTime && tile.fadeEndTime <= index$1.exported$1.now()) continue;
const parentTile = this.findLoadedParent(tileID, Math.max(tileID.overscaledZ - SourceCache.maxOverzooming, this._source.minzoom));
if (parentTile) {
this._addTile(parentTile.tileID);
parentsForFading[parentTile.tileID.key] = parentTile.tileID;
}
fadingTiles[id] = tileID;
}
const minZoom = idealTileIDs[idealTileIDs.length - 1].overscaledZ;
for (const id in this._tiles) {
const childTile = this._tiles[id];
if (retain[id] || !childTile.hasData()) {
continue;
}
let parentID = childTile.tileID;
while (parentID.overscaledZ > minZoom) {
parentID = parentID.scaledTo(parentID.overscaledZ - 1);
const tile = this._tiles[parentID.key];
if (tile && tile.hasData() && fadingTiles[parentID.key]) {
retain[id] = childTile.tileID;
break;
}
}
}
for (const id in parentsForFading) {
if (!retain[id]) {
this._coveredTiles[id] = true;
retain[id] = parentsForFading[id];
}
}
}
for (const retainedId in retain) {
this._tiles[retainedId].clearFadeHold();
}
const remove = index$1.keysDifference(this._tiles, retain);
for (const tileID of remove) {
const tile = this._tiles[tileID];
if (tile.hasSymbolBuckets && !tile.holdingForFade()) {
tile.setHoldDuration(this.map._fadeDuration);
} else if (!tile.hasSymbolBuckets || tile.symbolFadeFinished()) {
this._removeTile(+tileID);
}
}
this._updateLoadedParentTileCache();
if (this._onlySymbols && this._source.afterUpdate) {
this._source.afterUpdate();
}
}
releaseSymbolFadeTiles() {
for (const id in this._tiles) {
if (this._tiles[id].holdingForFade()) {
this._removeTile(+id);
}
}
}
_updateRetainedTiles(idealTileIDs) {
const retain = {};
if (idealTileIDs.length === 0) {
return retain;
}
const checked = {};
const minZoom = idealTileIDs.reduce((min, id) => Math.min(min, id.overscaledZ), Infinity);
const maxZoom = idealTileIDs[0].overscaledZ;
index$1.assert(minZoom <= maxZoom);
const minCoveringZoom = Math.max(maxZoom - SourceCache.maxOverzooming, this._source.minzoom);
const maxCoveringZoom = Math.max(maxZoom + SourceCache.maxUnderzooming, this._source.minzoom);
const missingTiles = {};
for (const tileID of idealTileIDs) {
const tile = this._addTile(tileID);
retain[tileID.key] = tileID;
if (tile.hasData()) continue;
if (minZoom < this._source.maxzoom) {
missingTiles[tileID.key] = tileID;
}
}
this._retainLoadedChildren(missingTiles, minZoom, maxCoveringZoom, retain);
for (const tileID of idealTileIDs) {
let tile = this._tiles[tileID.key];
if (tile.hasData()) continue;
if (tileID.canonical.z >= this._source.maxzoom) {
const childCoord = tileID.children(this._source.maxzoom)[0];
const childTile = this.getTile(childCoord);
if (!!childTile && childTile.hasData()) {
retain[childCoord.key] = childCoord;
continue;
}
} else {
const children = tileID.children(this._source.maxzoom);
if (retain[children[0].key] && retain[children[1].key] && retain[children[2].key] && retain[children[3].key]) continue;
}
let parentWasRequested = tile.wasRequested();
for (let overscaledZ = tileID.overscaledZ - 1; overscaledZ >= minCoveringZoom; --overscaledZ) {
const parentId = tileID.scaledTo(overscaledZ);
if (checked[parentId.key]) break;
checked[parentId.key] = true;
tile = this.getTile(parentId);
if (!tile && parentWasRequested) {
tile = this._addTile(parentId);
}
if (tile) {
retain[parentId.key] = parentId;
parentWasRequested = tile.wasRequested();
if (tile.hasData()) break;
}
}
}
return retain;
}
_updateLoadedParentTileCache() {
this._loadedParentTiles = {};
for (const tileKey in this._tiles) {
const path = [];
let parentTile;
let currentId = this._tiles[tileKey].tileID;
while (currentId.overscaledZ > 0) {
if (currentId.key in this._loadedParentTiles) {
parentTile = this._loadedParentTiles[currentId.key];
break;
}
path.push(currentId.key);
const parentId = currentId.scaledTo(currentId.overscaledZ - 1);
parentTile = this._getLoadedTile(parentId);
if (parentTile) {
break;
}
currentId = parentId;
}
for (const key of path) {
this._loadedParentTiles[key] = parentTile;
}
}
}
/**
* Add a tile, given its coordinate, to the pyramid.
* @private
*/
_addTile(tileID) {
let tile = this._tiles[tileID.key];
const isExtraShadowCaster = !!this._shadowCasterTiles[tileID.key];
if (tile) {
if (tile.isExtraShadowCaster === true && !isExtraShadowCaster) {
this._reloadTile(tileID.key, "reloading");
}
return tile;
}
tile = this._cache.getAndRemove(tileID);
if (tile) {
this._setTileReloadTimer(tileID.key, tile);
tile.tileID = tileID;
this._state.initializeTileState(tile, this.map ? this.map.painter : null);
if (this._cacheTimers[tileID.key]) {
clearTimeout(this._cacheTimers[tileID.key]);
delete this._cacheTimers[tileID.key];
this._setTileReloadTimer(tileID.key, tile);
}
}
const cached = Boolean(tile);
if (!cached) {
const painter = this.map ? this.map.painter : null;
const size = this._source.tileSize * tileID.overscaleFactor();
const isRasterArray = this._source.type === "raster-array";
tile = isRasterArray ? new RasterArrayTile(tileID, size, this.transform.tileZoom, painter, this._isRaster) : new Tile(tileID, size, this.transform.tileZoom, painter, this._isRaster, this._source.worldview);
this._loadTile(tile, this._tileLoaded.bind(this, tile, tileID.key, tile.state));
}
tile.uses++;
this._tiles[tileID.key] = tile;
if (!cached) this._source.fire(new index$1.Event("dataloading", { tile, coord: tile.tileID, dataType: "source" }));
return tile;
}
_setTileReloadTimer(id, tile) {
if (id in this._timers) {
clearTimeout(this._timers[id]);
delete this._timers[id];
}
const expiryTimeout = tile.getExpiryTimeout();
if (expiryTimeout) {
this._timers[id] = setTimeout(() => {
this._reloadTile(id, "expired");
delete this._timers[id];
}, expiryTimeout);
}
}
/**
* Remove a tile, given its id, from the pyramid
* @private
*/
_removeTile(id) {
const tile = this._tiles[id];
if (!tile)
return;
tile.uses--;
delete this._tiles[id];
if (this._timers[id]) {
clearTimeout(this._timers[id]);
delete this._timers[id];
}
if (tile.uses > 0)
return;
if (tile.hasData() && tile.state !== "reloading" || tile.state === "empty") {
this._cache.add(tile.tileID, tile, tile.getExpiryTimeout());
} else {
tile.aborted = true;
this._abortTile(tile);
this._unloadTile(tile);
}
}
/**
* Remove all tiles from this pyramid.
* @private
*/
clearTiles() {
this._shouldReloadOnResume = false;
this._paused = false;
for (const id in this._tiles)
this._removeTile(+id);
if (this._source._clear)
this._source._clear();
this._cache.reset();
if (this.map && this.usedForTerrain && this.map.painter.terrain) {
this.map.painter.terrain.resetTileLookupCache(this.id);
}
}
/**
* Search through our current tiles and attempt to find the tiles that cover the given `queryGeometry`.
*
* @param {QueryGeometry} queryGeometry
* @param {boolean} [visualizeQueryGeometry=false]
* @param {boolean} use3DQuery
* @returns
* @private
*/
tilesIn(queryGeometry, use3DQuery, visualizeQueryGeometry) {
const tileResults = [];
const transform = this.transform;
if (!transform) return tileResults;
const isGlobe = transform.projection.name === "globe";
const centerX = index$1.mercatorXfromLng(transform.center.lng);
for (const tileID in this._tiles) {
const tile = this._tiles[tileID];
if (visualizeQueryGeometry) {
tile.clearQueryDebugViz();
}
if (tile.holdingForFade()) {
continue;
}
let tilesToCheck;
if (isGlobe) {
const id = tile.tileID.canonical;
index$1.assert(tile.tileID.wrap === 0);
if (id.z === 0) {
const distances = [
Math.abs(index$1.clamp(centerX, ...tileBoundsX(id, -1)) - centerX),
Math.abs(index$1.clamp(centerX, ...tileBoundsX(id, 1)) - centerX)
];
tilesToCheck = [0, distances.indexOf(Math.min(...distances)) * 2 - 1];
} else {
const distances = [
Math.abs(index$1.clamp(centerX, ...tileBoundsX(id, -1)) - centerX),
Math.abs(index$1.clamp(centerX, ...tileBoundsX(id, 0)) - centerX),
Math.abs(index$1.clamp(centerX, ...tileBoundsX(id, 1)) - centerX)
];
tilesToCheck = [distances.indexOf(Math.min(...distances)) - 1];
}
} else {
tilesToCheck = [0];
}
for (const wrap of tilesToCheck) {
const tileResult = queryGeometry.containsTile(tile, transform, use3DQuery, wrap);
if (tileResult) {
tileResults.push(tileResult);
}
}
}
return tileResults;
}
getShadowCasterCoordinates() {
return this._getRenderableCoordinates(false, true);
}
getVisibleCoordinates(symbolLayer) {
return this._getRenderableCoordinates(symbolLayer);
}
_getRenderableCoordinates(symbolLayer, includeShadowCasters) {
const coords = this.getRenderableIds(symbolLayer, includeShadowCasters).map((id) => this._tiles[id].tileID);
const isGlobe = this.transform.projection.name === "globe";
for (const coord of coords) {
coord.projMatrix = this.transform.calculateProjMatrix(coord.toUnwrapped());
if (isGlobe) {
coord.expandedProjMatrix = this.transform.calculateProjMatrix(coord.toUnwrapped(), false, true);
} else {
coord.expandedProjMatrix = coord.projMatrix;
}
}
return coords;
}
sortCoordinatesByDistance(coords) {
const sortedCoords = coords.slice();
const camPos = this.transform._camera.position;
const camFwd = this.transform._camera.forward();
const precomputedDistances = {};
for (const id of sortedCoords) {
const invTiles = 1 / (1 << id.canonical.z);
const centerX = (id.canonical.x + 0.5) * invTiles + id.wrap;
const centerY = (id.canonical.y + 0.5) * invTiles;
precomputedDistances[id.key] = (centerX - camPos[0]) * camFwd[0] + (centerY - camPos[1]) * camFwd[1] - camPos[2] * camFwd[2];
}
sortedCoords.sort((a, b) => {
return precomputedDistances[a.key] - precomputedDistances[b.key];
});
return sortedCoords;
}
hasTransition() {
if (this._source.hasTransition()) {
return true;
}
if (isRasterType(this._source.type)) {
for (const id in this._tiles) {
const tile = this._tiles[id];
if (tile.fadeEndTime !== void 0 && tile.fadeEndTime >= index$1.exported$1.now()) {
return true;
}
}
}
return false;
}
/**
* Set the value of a particular state for a feature
* @private
*/
setFeatureState(sourceLayer, featureId, state) {
sourceLayer = sourceLayer || "_geojsonTileLayer";
this._state.updateState(sourceLayer, featureId, state);
}
/**
* Resets the value of a particular state key for a feature
* @private
*/
removeFeatureState(sourceLayer, featureId, key) {
sourceLayer = sourceLayer || "_geojsonTileLayer";
this._state.removeFeatureState(sourceLayer, featureId, key);
}
/**
* Get the entire state object for a feature
* @private
*/
getFeatureState(sourceLayer, featureId) {
sourceLayer = sourceLayer || "_geojsonTileLayer";
return this._state.getState(sourceLayer, featureId);
}
/**
* Sets the set of keys that the tile depends on. This allows tiles to
* be reloaded when their dependencies change.
* @private
*/
setDependencies(tileKey, namespace, dependencies) {
const tile = this._tiles[tileKey];
if (tile) {
tile.setDependencies(namespace, dependencies);
}
}
/**
* Reloads all tiles that depend on the given keys.
* @private
*/
reloadTilesForDependencies(namespaces, keys) {
for (const id in this._tiles) {
const tile = this._tiles[id];
if (tile.hasDependency(namespaces, keys)) {
this._reloadTile(+id, "reloading");
}
}
this._cache.filter((tile) => !tile.hasDependency(namespaces, keys));
}
/**
* Preloads all tiles that will be requested for one or a series of transformations
*
* @private
* @returns {Object} Returns `this` | Promise.
*/
_preloadTiles(transform, callback) {
if (!this._sourceLoaded) {
const waitUntilSourceLoaded = () => {
if (!this._sourceLoaded) return;
this._source.off("data", waitUntilSourceLoaded);
this._preloadTiles(transform, callback);
};
this._source.on("data", waitUntilSourceLoaded);
return;
}
const coveringTilesIDs = /* @__PURE__ */ new Map();
const transforms = Array.isArray(transform) ? transform : [transform];
const terrain = this.map.painter.terrain;
const tileSize = this.usedForTerrain && terrain ? terrain.getScaledDemTileSize() : this._source.tileSize;
for (const tr of transforms) {
const tileIDs2 = tr.coveringTiles({
tileSize,
minzoom: this._source.minzoom,
maxzoom: this._source.maxzoom,
roundZoom: this._source.roundZoom && !this.usedForTerrain,
reparseOverscaled: this._source.reparseOverscaled,
isTerrainDEM: this.usedForTerrain
});
for (const tileID of tileIDs2) {
coveringTilesIDs.set(tileID.key, tileID);
}
if (this.usedForTerrain) {
tr.updateElevation(false);
}
}
const tileIDs = Array.from(coveringTilesIDs.values());
index$1.asyncAll(tileIDs, (tileID, done) => {
const tile = new Tile(tileID, this._source.tileSize * tileID.overscaleFactor(), this.transform.tileZoom, this.map.painter, this._isRaster, this._source.worldview);
this._loadTile(tile, (err) => {
if (this._source.type === "raster-dem" && tile.dem) this._backfillDEM(tile);
done(err, tile);
});
}, callback);
}
}
SourceCache.maxOverzooming = 10;
SourceCache.maxUnderzooming = 3;
function compareTileId(a, b) {
const aWrap = Math.abs(a.wrap * 2) - +(a.wrap < 0);
const bWrap = Math.abs(b.wrap * 2) - +(b.wrap < 0);
return a.overscaledZ - b.overscaledZ || bWrap - aWrap || b.canonical.y - a.canonical.y || b.canonical.x - a.canonical.x;
}
function isRasterType(type) {
return type === "raster" || type === "image" || type === "video" || type === "custom";
}
function tileBoundsX(id, wrap) {
const tiles = 1 << id.z;
return [id.x / tiles + wrap, (id.x + 1) / tiles + wrap];
}
class BuildingIndex {
// when layer're hidden since the last frame, don't keep previous elevation, while loading tiles.
constructor(style) {
this.style = style;
this.layersGotHidden = false;
this.layers = [];
}
processLayersChanged() {
this.layers = [];
const visible = false, visibilityChanged = false;
for (const layerId in this.style._mergedLayers) {
const layer = this.style._mergedLayers[layerId];
if (layer.type === "fill-extrusion" || layer.type === "building") {
this.layers.push({ layer, visible, visibilityChanged });
} else if (layer.type === "model") {
const source = this.style.getLayerSource(layer);
if (source && source.type === "batched-model") {
this.layers.push({ layer, visible, visibilityChanged });
}
}
}
}
// Check if some of the building layers are disabled or with opacity evaluated to 0.
onNewFrame(zoom) {
this.layersGotHidden = false;
for (const l of this.layers) {
const layer = l.layer;
let visible = false;
if (layer.type === "fill-extrusion") {
visible = !layer.isHidden(zoom) && layer.paint.get("fill-extrusion-opacity") > 0;
} else if (layer.type === "building") {
visible = !layer.isHidden(zoom) && layer.paint.get("building-opacity") > 0;
} else if (layer.type === "model") {
visible = !layer.isHidden(zoom) && layer.paint.get("model-opacity").constantOr(1) > 0;
}
this.layersGotHidden = this.layersGotHidden || !visible && l.visible;
l.visible = visible;
}
}
updateZOffset(symbolBucket, tileID) {
this.currentBuildingBuckets = [];
for (const l of this.layers) {
const layer = l.layer;
const sourceCache = this.style.getLayerSourceCache(layer);
let verticalScale = 1;
if (layer.type === "fill-extrusion") {
verticalScale = l.visible ? layer.paint.get("fill-extrusion-vertical-scale") : 0;
} else if (layer.type === "building") {
verticalScale = l.visible ? layer.paint.get("building-vertical-scale") : 0;
}
let tile = sourceCache ? sourceCache.getTile(tileID) : null;
if (!tile && sourceCache) {
for (const cachedTileKey in sourceCache._tiles) {
const cachedTile = sourceCache._tiles[cachedTileKey];
if (tileID.canonical.isChildOf(cachedTile.tileID.canonical)) {
tile = cachedTile;
break;
}
}
}
this.currentBuildingBuckets.push({ bucket: tile ? tile.getBucket(layer) : null, tileID: tile ? tile.tileID : tileID, verticalScale });
}
symbolBucket.hasAnyZOffset = false;
let dataChanged = false;
for (let s = 0; s < symbolBucket.symbolInstances.length; s++) {
const symbolInstance = symbolBucket.symbolInstances.get(s);
const currentZOffset = symbolInstance.zOffset;
const newZOffset = this._getHeightAtTileOffset(tileID, symbolInstance.tileAnchorX, symbolInstance.tileAnchorY);
symbolInstance.zOffset = newZOffset !== Number.NEGATIVE_INFINITY ? newZOffset : currentZOffset;
if (!dataChanged && currentZOffset !== symbolInstance.zOffset) {
dataChanged = true;
}
if (!symbolBucket.hasAnyZOffset && symbolInstance.zOffset !== 0) {
symbolBucket.hasAnyZOffset = true;
}
}
if (dataChanged) {
symbolBucket.zOffsetBuffersNeedUpload = true;
symbolBucket.zOffsetSortDirty = true;
}
}
_mapCoordToOverlappingTile(tid, x, y, targetTileID) {
let tileX = x;
let tileY = y;
const tileID = targetTileID;
if (tid.canonical.z !== tileID.canonical.z) {
const id = tileID.canonical;
const zDiff = 1 / (1 << tid.canonical.z - id.z);
tileX = (x + tid.canonical.x * index$1.EXTENT) * zDiff - id.x * index$1.EXTENT | 0;
tileY = (y + tid.canonical.y * index$1.EXTENT) * zDiff - id.y * index$1.EXTENT | 0;
}
return { tileX, tileY };
}
_getHeightAtTileOffset(tid, x, y) {
let availableHeight;
let maxFillExtrusionHeight;
for (let i = 0; i < this.layers.length; ++i) {
const l = this.layers[i];
const layer = l.layer;
if (layer.type !== "fill-extrusion" && layer.type !== "building") continue;
const { bucket, tileID, verticalScale } = this.currentBuildingBuckets[i];
if (!bucket) continue;
const { tileX, tileY } = this._mapCoordToOverlappingTile(tid, x, y, tileID);
const b = bucket;
const heightData = b.getHeightAtTileCoord(tileX, tileY);
if (!heightData || heightData.height === void 0) continue;
if (heightData.hidden) {
availableHeight = heightData.height;
continue;
}
maxFillExtrusionHeight = Math.max(heightData.height * verticalScale, maxFillExtrusionHeight || 0);
}
if (maxFillExtrusionHeight !== void 0) {
return maxFillExtrusionHeight;
}
for (let i = 0; i < this.layers.length; ++i) {
const l = this.layers[i];
const layer = l.layer;
if (layer.type !== "model" || !l.visible) continue;
const { bucket, tileID } = this.currentBuildingBuckets[i];
if (!bucket) continue;
const { tileX, tileY } = this._mapCoordToOverlappingTile(tid, x, y, tileID);
const b = bucket;
const heightData = b.getHeightAtTileCoord(tileX, tileY);
if (!heightData || heightData.hidden) continue;
if (heightData.height === void 0 && availableHeight !== void 0) return Math.min(heightData.maxHeight, availableHeight) * heightData.verticalScale;
return heightData.height ? heightData.height * heightData.verticalScale : Number.NEGATIVE_INFINITY;
}
return this.layersGotHidden ? 0 : Number.NEGATIVE_INFINITY;
}
}
function deref(layer, parent) {
const result = {};
for (const k in layer) {
if (k !== "ref") {
result[k] = layer[k];
}
}
index$1.refProperties.forEach((k) => {
if (k in parent) {
result[k] = parent[k];
}
});
return result;
}
function derefLayers(layers) {
layers = layers.slice();
const map = /* @__PURE__ */ Object.create(null);
for (let i = 0; i < layers.length; i++) {
map[layers[i].id] = layers[i];
}
for (let i = 0; i < layers.length; i++) {
if ("ref" in layers[i]) {
layers[i] = deref(layers[i], map[layers[i].ref]);
}
}
return layers;
}
function emptyStyle() {
return {
version: 8,
layers: [],
sources: {}
};
}
const operations = {
/*
* { command: 'setStyle', args: [stylesheet] }
*/
setStyle: "setStyle",
/*
* { command: 'addLayer', args: [layer, 'beforeLayerId'] }
*/
addLayer: "addLayer",
/*
* { command: 'removeLayer', args: ['layerId'] }
*/
removeLayer: "removeLayer",
/*
* { command: 'setPaintProperty', args: ['layerId', 'prop', value] }
*/
setPaintProperty: "setPaintProperty",
/*
* { command: 'setLayoutProperty', args: ['layerId', 'prop', value] }
*/
setLayoutProperty: "setLayoutProperty",
/*
* { command: 'setSlot', args: ['layerId', slot] }
*/
setSlot: "setSlot",
/*
* { command: 'setFilter', args: ['layerId', filter] }
*/
setFilter: "setFilter",
/*
* { command: 'addSource', args: ['sourceId', source] }
*/
addSource: "addSource",
/*
* { command: 'removeSource', args: ['sourceId'] }
*/
removeSource: "removeSource",
/*
* { command: 'setGeoJSONSourceData', args: ['sourceId', data] }
*/
setGeoJSONSourceData: "setGeoJSONSourceData",
/*
* { command: 'setLayerZoomRange', args: ['layerId', 0, 22] }
*/
setLayerZoomRange: "setLayerZoomRange",
/*
* { command: 'setLayerProperty', args: ['layerId', 'prop', value] }
*/
setLayerProperty: "setLayerProperty",
/*
* { command: 'setCenter', args: [[lon, lat]] }
*/
setCenter: "setCenter",
/*
* { command: 'setZoom', args: [zoom] }
*/
setZoom: "setZoom",
/*
* { command: 'setBearing', args: [bearing] }
*/
setBearing: "setBearing",
/*
* { command: 'setPitch', args: [pitch] }
*/
setPitch: "setPitch",
/*
* { command: 'setSprite', args: ['spriteUrl'] }
*/
setSprite: "setSprite",
/*
* { command: 'setGlyphs', args: ['glyphsUrl'] }
*/
setGlyphs: "setGlyphs",
/*
* { command: 'setTransition', args: [transition] }
*/
setTransition: "setTransition",
/*
* { command: 'setLighting', args: [lightProperties] }
*/
setLight: "setLight",
/*
* { command: 'setTerrain', args: [terrainProperties] }
*/
setTerrain: "setTerrain",
/*
* { command: 'setFog', args: [fogProperties] }
*/
setFog: "setFog",
/*
* { command: 'setSnow', args: [snowProperties] }
*/
setSnow: "setSnow",
/*
* { command: 'setRain', args: [rainProperties] }
*/
setRain: "setRain",
/*
* { command: 'setCamera', args: [cameraProperties] }
*/
setCamera: "setCamera",
/*
* { command: 'setLights', args: [{light-3d},...] }
*/
setLights: "setLights",
/*
* { command: 'setProjection', args: [projectionProperties] }
*/
setProjection: "setProjection",
/*
* { command: 'addImport', args: [import] }
*/
addImport: "addImport",
/*
* { command: 'removeImport', args: [importId] }
*/
removeImport: "removeImport",
/**
* { command: 'updateImport', args: [importId, importSpecification | styleUrl] }
*/
updateImport: "updateImport",
/*
* { command: 'addIconset', args: [iconsetId, IconsetSpecification] }
*/
addIconset: "addIconset",
/*
* { command: 'removeIconset', args: [iconsetId] }
*/
removeIconset: "removeIconset"
};
function addSource(sourceId, after, commands) {
commands.push({ command: operations.addSource, args: [sourceId, after[sourceId]] });
}
function removeSource(sourceId, commands, sourcesRemoved) {
commands.push({ command: operations.removeSource, args: [sourceId] });
sourcesRemoved[sourceId] = true;
}
function updateSource(sourceId, after, commands, sourcesRemoved) {
removeSource(sourceId, commands, sourcesRemoved);
addSource(sourceId, after, commands);
}
function canUpdateGeoJSON(before, after, sourceId) {
let prop;
for (prop in before[sourceId]) {
if (!before[sourceId].hasOwnProperty(prop)) continue;
if (prop !== "data" && !index$1.deepEqual(before[sourceId][prop], after[sourceId][prop])) {
return false;
}
}
for (prop in after[sourceId]) {
if (!after[sourceId].hasOwnProperty(prop)) continue;
if (prop !== "data" && !index$1.deepEqual(before[sourceId][prop], after[sourceId][prop])) {
return false;
}
}
return true;
}
function diffSources(before, after, commands, sourcesRemoved) {
before = before || {};
after = after || {};
let sourceId;
for (sourceId in before) {
if (!before.hasOwnProperty(sourceId)) continue;
if (!after.hasOwnProperty(sourceId)) {
removeSource(sourceId, commands, sourcesRemoved);
}
}
for (sourceId in after) {
if (!after.hasOwnProperty(sourceId)) continue;
const source = after[sourceId];
if (!before.hasOwnProperty(sourceId)) {
addSource(sourceId, after, commands);
} else if (!index$1.deepEqual(before[sourceId], source)) {
if (before[sourceId].type === "geojson" && source.type === "geojson" && canUpdateGeoJSON(before, after, sourceId)) {
commands.push({ command: operations.setGeoJSONSourceData, args: [sourceId, source.data] });
} else {
updateSource(sourceId, after, commands, sourcesRemoved);
}
}
}
}
function diffLayerPropertyChanges(before, after, commands, layerId, klass, command) {
before = before || {};
after = after || {};
let prop;
for (prop in before) {
if (!before.hasOwnProperty(prop)) continue;
if (!index$1.deepEqual(before[prop], after[prop])) {
commands.push({ command, args: [layerId, prop, after[prop], klass] });
}
}
for (prop in after) {
if (!after.hasOwnProperty(prop) || before.hasOwnProperty(prop)) continue;
if (!index$1.deepEqual(before[prop], after[prop])) {
commands.push({ command, args: [layerId, prop, after[prop], klass] });
}
}
}
function pluckId(item) {
return item.id;
}
function indexById(group, item) {
group[item.id] = item;
return group;
}
function diffLayers(before, after, commands) {
before = before || [];
after = after || [];
const beforeOrder = before.map(pluckId);
const afterOrder = after.map(pluckId);
const beforeIndex = before.reduce(indexById, {});
const afterIndex = after.reduce(indexById, {});
const tracker = beforeOrder.slice();
const clean = /* @__PURE__ */ Object.create(null);
let i, d, layerId, beforeLayer, afterLayer, insertBeforeLayerId, prop;
for (i = 0, d = 0; i < beforeOrder.length; i++) {
layerId = beforeOrder[i];
if (!afterIndex.hasOwnProperty(layerId)) {
commands.push({ command: operations.removeLayer, args: [layerId] });
tracker.splice(tracker.indexOf(layerId, d), 1);
} else {
d++;
}
}
for (i = 0, d = 0; i < afterOrder.length; i++) {
layerId = afterOrder[afterOrder.length - 1 - i];
if (tracker[tracker.length - 1 - i] === layerId) continue;
if (beforeIndex.hasOwnProperty(layerId)) {
commands.push({ command: operations.removeLayer, args: [layerId] });
tracker.splice(tracker.lastIndexOf(layerId, tracker.length - d), 1);
} else {
d++;
}
insertBeforeLayerId = tracker[tracker.length - i];
commands.push({ command: operations.addLayer, args: [afterIndex[layerId], insertBeforeLayerId] });
tracker.splice(tracker.length - i, 0, layerId);
clean[layerId] = true;
}
for (i = 0; i < afterOrder.length; i++) {
layerId = afterOrder[i];
beforeLayer = beforeIndex[layerId];
afterLayer = afterIndex[layerId];
if (clean[layerId] || index$1.deepEqual(beforeLayer, afterLayer)) continue;
if (!index$1.deepEqual(beforeLayer.source, afterLayer.source) || !index$1.deepEqual(beforeLayer["source-layer"], afterLayer["source-layer"]) || !index$1.deepEqual(beforeLayer.type, afterLayer.type)) {
commands.push({ command: operations.removeLayer, args: [layerId] });
insertBeforeLayerId = tracker[tracker.lastIndexOf(layerId) + 1];
commands.push({ command: operations.addLayer, args: [afterLayer, insertBeforeLayerId] });
continue;
}
diffLayerPropertyChanges(beforeLayer.layout, afterLayer.layout, commands, layerId, null, operations.setLayoutProperty);
diffLayerPropertyChanges(beforeLayer.paint, afterLayer.paint, commands, layerId, null, operations.setPaintProperty);
if (!index$1.deepEqual(beforeLayer.slot, afterLayer.slot)) {
commands.push({ command: operations.setSlot, args: [layerId, afterLayer.slot] });
}
if (!index$1.deepEqual(beforeLayer.filter, afterLayer.filter)) {
commands.push({ command: operations.setFilter, args: [layerId, afterLayer.filter] });
}
if (!index$1.deepEqual(beforeLayer.minzoom, afterLayer.minzoom) || !index$1.deepEqual(beforeLayer.maxzoom, afterLayer.maxzoom)) {
commands.push({ command: operations.setLayerZoomRange, args: [layerId, afterLayer.minzoom, afterLayer.maxzoom] });
}
for (prop in beforeLayer) {
if (!beforeLayer.hasOwnProperty(prop)) continue;
if (prop === "layout" || prop === "paint" || prop === "filter" || prop === "metadata" || prop === "minzoom" || prop === "maxzoom" || prop === "slot") continue;
if (prop.indexOf("paint.") === 0) {
diffLayerPropertyChanges(beforeLayer[prop], afterLayer[prop], commands, layerId, prop.slice(6), operations.setPaintProperty);
} else if (!index$1.deepEqual(beforeLayer[prop], afterLayer[prop])) {
commands.push({ command: operations.setLayerProperty, args: [layerId, prop, afterLayer[prop]] });
}
}
for (prop in afterLayer) {
if (!afterLayer.hasOwnProperty(prop) || beforeLayer.hasOwnProperty(prop)) continue;
if (prop === "layout" || prop === "paint" || prop === "filter" || prop === "metadata" || prop === "minzoom" || prop === "maxzoom" || prop === "slot") continue;
if (prop.indexOf("paint.") === 0) {
diffLayerPropertyChanges(beforeLayer[prop], afterLayer[prop], commands, layerId, prop.slice(6), operations.setPaintProperty);
} else if (!index$1.deepEqual(beforeLayer[prop], afterLayer[prop])) {
commands.push({ command: operations.setLayerProperty, args: [layerId, prop, afterLayer[prop]] });
}
}
}
}
function diffImports(before = [], after = [], commands) {
before = before || [];
after = after || [];
const beforeOrder = before.map(pluckId);
const afterOrder = after.map(pluckId);
const beforeIndex = before.reduce(indexById, {});
const afterIndex = after.reduce(indexById, {});
const tracker = beforeOrder.slice();
let i, d, importId, insertBefore;
for (i = 0, d = 0; i < beforeOrder.length; i++) {
importId = beforeOrder[i];
if (!afterIndex.hasOwnProperty(importId)) {
commands.push({ command: operations.removeImport, args: [importId] });
tracker.splice(tracker.indexOf(importId, d), 1);
} else {
d++;
}
}
for (i = 0, d = 0; i < afterOrder.length; i++) {
importId = afterOrder[afterOrder.length - 1 - i];
if (tracker[tracker.length - 1 - i] === importId) continue;
if (beforeIndex.hasOwnProperty(importId)) {
commands.push({ command: operations.removeImport, args: [importId] });
tracker.splice(tracker.lastIndexOf(importId, tracker.length - d), 1);
} else {
d++;
}
insertBefore = tracker[tracker.length - i];
commands.push({ command: operations.addImport, args: [afterIndex[importId], insertBefore] });
tracker.splice(tracker.length - i, 0, importId);
}
for (const afterImport of after) {
const beforeImport = beforeIndex[afterImport.id];
if (!beforeImport) continue;
delete beforeImport.data;
if (index$1.deepEqual(beforeImport, afterImport)) continue;
commands.push({ command: operations.updateImport, args: [afterImport.id, afterImport] });
}
}
function diffIconsets(before, after, commands) {
before = before || {};
after = after || {};
let iconsetId;
for (iconsetId in before) {
if (!before.hasOwnProperty(iconsetId)) continue;
if (!after.hasOwnProperty(iconsetId)) {
commands.push({ command: operations.removeIconset, args: [iconsetId] });
}
}
for (iconsetId in after) {
if (!after.hasOwnProperty(iconsetId)) continue;
const iconset = after[iconsetId];
if (!before.hasOwnProperty(iconsetId)) {
commands.push({ command: operations.addIconset, args: [iconsetId, iconset] });
} else if (!index$1.deepEqual(before[iconsetId], iconset)) {
commands.push({ command: operations.removeIconset, args: [iconsetId] });
commands.push({ command: operations.addIconset, args: [iconsetId, iconset] });
}
}
}
function diffStyles(before, after) {
if (!before) return [{ command: operations.setStyle, args: [after] }];
let commands = [];
try {
if (!index$1.deepEqual(before.version, after.version)) {
return [{ command: operations.setStyle, args: [after] }];
}
if (!index$1.deepEqual(before.center, after.center)) {
commands.push({ command: operations.setCenter, args: [after.center] });
}
if (!index$1.deepEqual(before.zoom, after.zoom)) {
commands.push({ command: operations.setZoom, args: [after.zoom] });
}
if (!index$1.deepEqual(before.bearing, after.bearing)) {
commands.push({ command: operations.setBearing, args: [after.bearing] });
}
if (!index$1.deepEqual(before.pitch, after.pitch)) {
commands.push({ command: operations.setPitch, args: [after.pitch] });
}
if (!index$1.deepEqual(before.sprite, after.sprite)) {
commands.push({ command: operations.setSprite, args: [after.sprite] });
}
if (!index$1.deepEqual(before.glyphs, after.glyphs)) {
commands.push({ command: operations.setGlyphs, args: [after.glyphs] });
}
if (!index$1.deepEqual(before.imports, after.imports)) {
diffImports(before.imports, after.imports, commands);
}
if (!index$1.deepEqual(before.transition, after.transition)) {
commands.push({ command: operations.setTransition, args: [after.transition] });
}
if (!index$1.deepEqual(before.light, after.light)) {
commands.push({ command: operations.setLight, args: [after.light] });
}
if (!index$1.deepEqual(before.fog, after.fog)) {
commands.push({ command: operations.setFog, args: [after.fog] });
}
if (!index$1.deepEqual(before.snow, after.snow)) {
commands.push({ command: operations.setSnow, args: [after.snow] });
}
if (!index$1.deepEqual(before.rain, after.rain)) {
commands.push({ command: operations.setRain, args: [after.rain] });
}
if (!index$1.deepEqual(before.projection, after.projection)) {
commands.push({ command: operations.setProjection, args: [after.projection] });
}
if (!index$1.deepEqual(before.lights, after.lights)) {
commands.push({ command: operations.setLights, args: [after.lights] });
}
if (!index$1.deepEqual(before.camera, after.camera)) {
commands.push({ command: operations.setCamera, args: [after.camera] });
}
if (!index$1.deepEqual(before.iconsets, after.iconsets)) {
diffIconsets(before.iconsets, after.iconsets, commands);
}
if (!index$1.deepEqual(before["color-theme"], after["color-theme"])) {
return [{ command: operations.setStyle, args: [after] }];
}
const sourcesRemoved = {};
const removeOrAddSourceCommands = [];
diffSources(before.sources, after.sources, removeOrAddSourceCommands, sourcesRemoved);
const beforeLayers = [];
if (before.layers) {
before.layers.forEach((layer) => {
if (layer.source && sourcesRemoved[layer.source]) {
commands.push({ command: operations.removeLayer, args: [layer.id] });
} else {
beforeLayers.push(layer);
}
});
}
let beforeTerrain = before.terrain;
if (beforeTerrain) {
if (sourcesRemoved[beforeTerrain.source]) {
commands.push({ command: operations.setTerrain, args: [void 0] });
beforeTerrain = void 0;
}
}
commands = commands.concat(removeOrAddSourceCommands);
if (!index$1.deepEqual(beforeTerrain, after.terrain)) {
commands.push({ command: operations.setTerrain, args: [after.terrain] });
}
diffLayers(beforeLayers, after.layers, commands);
} catch (e) {
console.warn("Unable to compute style diff:", e);
commands = [{ command: operations.setStyle, args: [after] }];
}
return commands;
}
function reconstructTileMatrix(transform, projection, coord) {
const tileMatrix = projection.createTileMatrix(transform, transform.worldSize, coord.toUnwrapped());
return index$1.multiply(new Float32Array(16), transform.projMatrix, tileMatrix);
}
function getCollisionDebugTileProjectionMatrix(coord, bucket, transform) {
if (bucket.projection.name === transform.projection.name) {
index$1.assert(coord.projMatrix);
return coord.projMatrix;
}
const tr = transform.clone();
tr.setProjection(bucket.projection);
return reconstructTileMatrix(tr, bucket.getProjection(), coord);
}
function getSymbolTileProjectionMatrix(coord, bucketProjection, transform) {
if (bucketProjection.name === transform.projection.name) {
index$1.assert(coord.projMatrix);
return coord.projMatrix;
}
return reconstructTileMatrix(transform, bucketProjection, coord);
}
function getSymbolPlacementTileProjectionMatrix(coord, bucketProjection, transform, runtimeProjection) {
if (bucketProjection.name === runtimeProjection) {
return transform.calculateProjMatrix(coord.toUnwrapped());
}
index$1.assert(transform.projection.name === bucketProjection.name);
return reconstructTileMatrix(transform, bucketProjection, coord);
}
class PathInterpolator {
constructor(points_, padding_) {
this.reset(points_, padding_);
}
reset(points_, padding_) {
this.points = points_ || [];
this._distances = [0];
for (let i = 1; i < this.points.length; i++) {
this._distances[i] = this._distances[i - 1] + this.points[i].dist(this.points[i - 1]);
}
this.length = this._distances[this._distances.length - 1];
this.padding = Math.min(padding_ || 0, this.length * 0.5);
this.paddedLength = this.length - this.padding * 2;
}
lerp(t) {
index$1.assert(this.points.length > 0);
if (this.points.length === 1) {
return this.points[0];
}
t = index$1.clamp(t, 0, 1);
let currentIndex = 1;
let distOfCurrentIdx = this._distances[currentIndex];
const distToTarget = t * this.paddedLength + this.padding;
while (distOfCurrentIdx < distToTarget && currentIndex < this._distances.length) {
distOfCurrentIdx = this._distances[++currentIndex];
}
const idxOfPrevPoint = currentIndex - 1;
const distOfPrevIdx = this._distances[idxOfPrevPoint];
const segmentLength = distOfCurrentIdx - distOfPrevIdx;
const segmentT = segmentLength > 0 ? (distToTarget - distOfPrevIdx) / segmentLength : 0;
return this.points[idxOfPrevPoint].mult(1 - segmentT).add(this.points[currentIndex].mult(segmentT));
}
}
class GridIndex {
constructor(width, height, cellSize) {
const boxCells = this.boxCells = [];
const circleCells = this.circleCells = [];
this.xCellCount = Math.ceil(width / cellSize);
this.yCellCount = Math.ceil(height / cellSize);
for (let i = 0; i < this.xCellCount * this.yCellCount; i++) {
boxCells.push([]);
circleCells.push([]);
}
this.circleKeys = [];
this.boxKeys = [];
this.bboxes = [];
this.circles = [];
this.width = width;
this.height = height;
this.xScale = this.xCellCount / width;
this.yScale = this.yCellCount / height;
this.boxUid = 0;
this.circleUid = 0;
}
keysLength() {
return this.boxKeys.length + this.circleKeys.length;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
insert(key, x1, y1, x2, y2) {
this._forEachCell(x1, y1, x2, y2, this._insertBoxCell, this.boxUid++);
this.boxKeys.push(key);
this.bboxes.push(x1);
this.bboxes.push(y1);
this.bboxes.push(x2);
this.bboxes.push(y2);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
insertCircle(key, x, y, radius) {
this._forEachCell(x - radius, y - radius, x + radius, y + radius, this._insertCircleCell, this.circleUid++);
this.circleKeys.push(key);
this.circles.push(x);
this.circles.push(y);
this.circles.push(radius);
}
_insertBoxCell(x1, y1, x2, y2, cellIndex, uid) {
this.boxCells[cellIndex].push(uid);
}
_insertCircleCell(x1, y1, x2, y2, cellIndex, uid) {
this.circleCells[cellIndex].push(uid);
}
_query(x1, y1, x2, y2, hitTest, predicate) {
if (x2 < 0 || x1 > this.width || y2 < 0 || y1 > this.height) {
return hitTest ? false : [];
}
const result = [];
if (x1 <= 0 && y1 <= 0 && this.width <= x2 && this.height <= y2) {
if (hitTest) {
return true;
}
for (let boxUid = 0; boxUid < this.boxKeys.length; boxUid++) {
result.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
key: this.boxKeys[boxUid],
x1: this.bboxes[boxUid * 4],
y1: this.bboxes[boxUid * 4 + 1],
x2: this.bboxes[boxUid * 4 + 2],
y2: this.bboxes[boxUid * 4 + 3]
});
}
for (let circleUid = 0; circleUid < this.circleKeys.length; circleUid++) {
const x = this.circles[circleUid * 3];
const y = this.circles[circleUid * 3 + 1];
const radius = this.circles[circleUid * 3 + 2];
result.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
key: this.circleKeys[circleUid],
x1: x - radius,
y1: y - radius,
x2: x + radius,
y2: y + radius
});
}
return predicate ? result.filter(predicate) : result;
} else {
const queryArgs = {
hitTest,
seenUids: { box: {}, circle: {} }
};
this._forEachCell(x1, y1, x2, y2, this._queryCell, result, queryArgs, predicate);
return hitTest ? result.length > 0 : result;
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_queryCircle(x, y, radius, hitTest, predicate) {
const x1 = x - radius;
const x2 = x + radius;
const y1 = y - radius;
const y2 = y + radius;
if (x2 < 0 || x1 > this.width || y2 < 0 || y1 > this.height) {
return hitTest ? false : [];
}
const result = [];
const queryArgs = {
hitTest,
circle: { x, y, radius },
seenUids: { box: {}, circle: {} }
};
this._forEachCell(x1, y1, x2, y2, this._queryCellCircle, result, queryArgs, predicate);
return hitTest ? result.length > 0 : result;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
query(x1, y1, x2, y2, predicate) {
return this._query(x1, y1, x2, y2, false, predicate);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hitTest(x1, y1, x2, y2, predicate) {
return this._query(x1, y1, x2, y2, true, predicate);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hitTestCircle(x, y, radius, predicate) {
return this._queryCircle(x, y, radius, true, predicate);
}
_queryCell(x1, y1, x2, y2, cellIndex, result, queryArgs, predicate) {
const seenUids = queryArgs.seenUids;
const boxCell = this.boxCells[cellIndex];
if (boxCell !== null) {
const bboxes = this.bboxes;
for (const boxUid of boxCell) {
if (!seenUids.box[boxUid]) {
seenUids.box[boxUid] = true;
const offset = boxUid * 4;
if (x1 <= bboxes[offset + 2] && y1 <= bboxes[offset + 3] && x2 >= bboxes[offset + 0] && y2 >= bboxes[offset + 1] && // eslint-disable-next-line @typescript-eslint/no-unsafe-call
(!predicate || predicate(this.boxKeys[boxUid]))) {
if (queryArgs.hitTest) {
result.push(true);
return true;
} else {
result.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
key: this.boxKeys[boxUid],
x1: bboxes[offset],
y1: bboxes[offset + 1],
x2: bboxes[offset + 2],
y2: bboxes[offset + 3]
});
}
}
}
}
}
const circleCell = this.circleCells[cellIndex];
if (circleCell !== null) {
const circles = this.circles;
for (const circleUid of circleCell) {
if (!seenUids.circle[circleUid]) {
seenUids.circle[circleUid] = true;
const offset = circleUid * 3;
if (this._circleAndRectCollide(
circles[offset],
circles[offset + 1],
circles[offset + 2],
x1,
y1,
x2,
y2
) && // eslint-disable-next-line @typescript-eslint/no-unsafe-call
(!predicate || predicate(this.circleKeys[circleUid]))) {
if (queryArgs.hitTest) {
result.push(true);
return true;
} else {
const x = circles[offset];
const y = circles[offset + 1];
const radius = circles[offset + 2];
result.push({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
key: this.circleKeys[circleUid],
x1: x - radius,
y1: y - radius,
x2: x + radius,
y2: y + radius
});
}
}
}
}
}
}
_queryCellCircle(x1, y1, x2, y2, cellIndex, result, queryArgs, predicate) {
const circle = queryArgs.circle;
const seenUids = queryArgs.seenUids;
const boxCell = this.boxCells[cellIndex];
if (boxCell !== null) {
const bboxes = this.bboxes;
for (const boxUid of boxCell) {
if (!seenUids.box[boxUid]) {
seenUids.box[boxUid] = true;
const offset = boxUid * 4;
if (this._circleAndRectCollide(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.x,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.y,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.radius,
bboxes[offset + 0],
bboxes[offset + 1],
bboxes[offset + 2],
bboxes[offset + 3]
) && // eslint-disable-next-line @typescript-eslint/no-unsafe-call
(!predicate || predicate(this.boxKeys[boxUid]))) {
result.push(true);
return true;
}
}
}
}
const circleCell = this.circleCells[cellIndex];
if (circleCell !== null) {
const circles = this.circles;
for (const circleUid of circleCell) {
if (!seenUids.circle[circleUid]) {
seenUids.circle[circleUid] = true;
const offset = circleUid * 3;
if (this._circlesCollide(
circles[offset],
circles[offset + 1],
circles[offset + 2],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.x,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.y,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
circle.radius
) && // eslint-disable-next-line @typescript-eslint/no-unsafe-call
(!predicate || predicate(this.circleKeys[circleUid]))) {
result.push(true);
return true;
}
}
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
_forEachCell(x1, y1, x2, y2, fn, arg1, arg2, predicate) {
const cx1 = this._convertToXCellCoord(x1);
const cy1 = this._convertToYCellCoord(y1);
const cx2 = this._convertToXCellCoord(x2);
const cy2 = this._convertToYCellCoord(y2);
for (let x = cx1; x <= cx2; x++) {
for (let y = cy1; y <= cy2; y++) {
const cellIndex = this.xCellCount * y + x;
if (fn.call(this, x1, y1, x2, y2, cellIndex, arg1, arg2, predicate)) return;
}
}
}
_convertToXCellCoord(x) {
return Math.max(0, Math.min(this.xCellCount - 1, Math.floor(x * this.xScale)));
}
_convertToYCellCoord(y) {
return Math.max(0, Math.min(this.yCellCount - 1, Math.floor(y * this.yScale)));
}
_circlesCollide(x1, y1, r1, x2, y2, r2) {
const dx = x2 - x1;
const dy = y2 - y1;
const bothRadii = r1 + r2;
return bothRadii * bothRadii > dx * dx + dy * dy;
}
_circleAndRectCollide(circleX, circleY, radius, x1, y1, x2, y2) {
const halfRectWidth = (x2 - x1) / 2;
const distX = Math.abs(circleX - (x1 + halfRectWidth));
if (distX > halfRectWidth + radius) {
return false;
}
const halfRectHeight = (y2 - y1) / 2;
const distY = Math.abs(circleY - (y1 + halfRectHeight));
if (distY > halfRectHeight + radius) {
return false;
}
if (distX <= halfRectWidth || distY <= halfRectHeight) {
return true;
}
const dx = distX - halfRectWidth;
const dy = distY - halfRectHeight;
return dx * dx + dy * dy <= radius * radius;
}
}
const FlipState = {
unknown: 0,
flipRequired: 1,
flipNotRequired: 2
};
const maxTangent = Math.tan(85 * Math.PI / 180);
function getLabelPlaneMatrixForRendering(posMatrix, tileID, pitchWithMap, rotateWithMap, transform, projection, pixelsToTileUnits) {
const m = index$1.create();
if (pitchWithMap) {
if (projection.name === "globe") {
const lm = index$1.calculateGlobeLabelMatrix(transform, tileID);
index$1.multiply(m, m, lm);
} else {
const s = index$1.invert$1([], pixelsToTileUnits);
m[0] = s[0];
m[1] = s[1];
m[4] = s[2];
m[5] = s[3];
if (!rotateWithMap) {
index$1.rotateZ(m, m, transform.angle);
}
}
} else {
index$1.multiply(m, transform.labelPlaneMatrix, posMatrix);
}
return m;
}
function getLabelPlaneMatrixForPlacement(posMatrix, tileID, pitchWithMap, rotateWithMap, transform, projection, pixelsToTileUnits) {
const m = getLabelPlaneMatrixForRendering(posMatrix, tileID, pitchWithMap, rotateWithMap, transform, projection, pixelsToTileUnits);
if (projection.name !== "globe" || !pitchWithMap) {
m[2] = m[6] = m[10] = m[14] = 0;
}
return m;
}
function getGlCoordMatrix(posMatrix, tileID, pitchWithMap, rotateWithMap, transform, projection, pixelsToTileUnits) {
if (pitchWithMap) {
if (projection.name === "globe") {
const m = getLabelPlaneMatrixForRendering(posMatrix, tileID, pitchWithMap, rotateWithMap, transform, projection, pixelsToTileUnits);
index$1.invert(m, m);
index$1.multiply(m, posMatrix, m);
return m;
} else {
const m = index$1.clone(posMatrix);
const s = index$1.identity([]);
s[0] = pixelsToTileUnits[0];
s[1] = pixelsToTileUnits[1];
s[4] = pixelsToTileUnits[2];
s[5] = pixelsToTileUnits[3];
index$1.multiply(m, m, s);
if (!rotateWithMap) {
index$1.rotateZ(m, m, -transform.angle);
}
return m;
}
} else {
return transform.glCoordMatrix;
}
}
function project(x, y, z, matrix) {
const pos = [x, y, z, 1];
if (z) {
index$1.transformMat4$1(pos, pos, matrix);
} else {
xyTransformMat4(pos, pos, matrix);
}
const w = pos[3];
pos[0] /= w;
pos[1] /= w;
pos[2] /= w;
return pos;
}
function projectClamped([x, y, z], matrix) {
const pos = [x, y, z, 1];
index$1.transformMat4$1(pos, pos, matrix);
const w = pos[3] = Math.max(pos[3], 1e-6);
pos[0] /= w;
pos[1] /= w;
pos[2] /= w;
return pos;
}
function getPerspectiveRatio(cameraToCenterDistance, signedDistanceFromCamera) {
return Math.min(0.5 + 0.5 * (cameraToCenterDistance / signedDistanceFromCamera), 1.5);
}
function isVisible(anchorPos, clippingBuffer) {
const x = anchorPos[0] / anchorPos[3];
const y = anchorPos[1] / anchorPos[3];
const inPaddedViewport = x >= -clippingBuffer[0] && x <= clippingBuffer[0] && y >= -clippingBuffer[1] && y <= clippingBuffer[1];
return inPaddedViewport;
}
function updateLineLabels(bucket, posMatrix, painter, isText, labelPlaneMatrix, glCoordMatrix, pitchWithMap, keepUpright, getElevation, tileID, scaleFactor = 1) {
const tr = painter.transform;
const sizeData = isText ? bucket.textSizeData : bucket.iconSizeData;
const partiallyEvaluatedSize = index$1.evaluateSizeForZoom(sizeData, painter.transform.zoom, scaleFactor);
const isGlobe = tr.projection.name === "globe";
const clippingBuffer = [256 / painter.width * 2 + 1, 256 / painter.height * 2 + 1];
const dynamicLayoutVertexArray = isText ? bucket.text.dynamicLayoutVertexArray : bucket.icon.dynamicLayoutVertexArray;
dynamicLayoutVertexArray.clear();
let globeExtVertexArray = null;
if (isGlobe) {
globeExtVertexArray = isText ? bucket.text.globeExtVertexArray : bucket.icon.globeExtVertexArray;
}
const lineVertexArray = bucket.lineVertexArray;
const placedSymbols = isText ? bucket.text.placedSymbolArray : bucket.icon.placedSymbolArray;
const aspectRatio = painter.transform.width / painter.transform.height;
let useVertical = false;
let prevWritingMode;
for (let s = 0; s < placedSymbols.length; s++) {
const symbol = placedSymbols.get(s);
const { numGlyphs, writingMode } = symbol;
if (writingMode === index$1.WritingMode.vertical && !useVertical && prevWritingMode !== index$1.WritingMode.horizontal) {
useVertical = true;
}
prevWritingMode = writingMode;
if ((symbol.hidden || writingMode === index$1.WritingMode.vertical) && !useVertical) {
hideGlyphs(numGlyphs, dynamicLayoutVertexArray);
continue;
}
useVertical = false;
const tileAnchorPoint = new index$1.Point(symbol.tileAnchorX, symbol.tileAnchorY);
const renderElevatedRoads = bucket.elevationType === "road";
const hasElevation = !!tr.elevation || renderElevatedRoads;
let { x, y, z } = tr.projection.projectTilePoint(tileAnchorPoint.x, tileAnchorPoint.y, tileID.canonical);
let elevationParams = null;
if (hasElevation) {
const elevationFeature = renderElevatedRoads ? bucket.getElevationFeatureForText(s) : null;
elevationParams = {
getElevation,
elevation: tr.elevation,
elevationFeature
};
const [dx, dy, dz] = getElevation(tileAnchorPoint, tr.elevation, elevationFeature);
x += dx;
y += dy;
z += dz;
}
const anchorPos = [x, y, z, 1];
index$1.transformMat4$1(anchorPos, anchorPos, posMatrix);
if (!isVisible(anchorPos, clippingBuffer)) {
hideGlyphs(numGlyphs, dynamicLayoutVertexArray);
continue;
}
const cameraToAnchorDistance = anchorPos[3];
const perspectiveRatio = getPerspectiveRatio(painter.transform.getCameraToCenterDistance(tr.projection), cameraToAnchorDistance);
const fontSize = index$1.evaluateSizeForFeature(sizeData, partiallyEvaluatedSize, symbol);
const pitchScaledFontSize = pitchWithMap ? fontSize / perspectiveRatio : fontSize * perspectiveRatio;
const labelPlaneAnchorPoint = project(x, y, z, labelPlaneMatrix);
if (labelPlaneAnchorPoint[3] <= 0) {
hideGlyphs(numGlyphs, dynamicLayoutVertexArray);
continue;
}
let projectionCache = {};
const layout = bucket.layers[0].layout;
const textMaxAngle = index$1.degToRad(layout.get("text-max-angle"));
const textMaxAngleThreshold = Math.cos(textMaxAngle);
const elevationParamsForPlacement = pitchWithMap ? null : elevationParams;
const placeUnflipped = placeGlyphsAlongLine(
symbol,
pitchScaledFontSize,
false,
keepUpright,
posMatrix,
labelPlaneMatrix,
glCoordMatrix,
bucket.glyphOffsetArray,
lineVertexArray,
dynamicLayoutVertexArray,
globeExtVertexArray,
labelPlaneAnchorPoint,
tileAnchorPoint,
projectionCache,
aspectRatio,
elevationParamsForPlacement,
tr.projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
);
useVertical = placeUnflipped.useVertical;
if (elevationParamsForPlacement && placeUnflipped.needsFlipping) projectionCache = {};
if (placeUnflipped.notEnoughRoom || useVertical || placeUnflipped.needsFlipping && placeGlyphsAlongLine(
symbol,
pitchScaledFontSize,
true,
keepUpright,
posMatrix,
labelPlaneMatrix,
glCoordMatrix,
bucket.glyphOffsetArray,
lineVertexArray,
dynamicLayoutVertexArray,
globeExtVertexArray,
labelPlaneAnchorPoint,
tileAnchorPoint,
projectionCache,
aspectRatio,
elevationParamsForPlacement,
tr.projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
).notEnoughRoom) {
hideGlyphs(numGlyphs, dynamicLayoutVertexArray);
}
}
if (isText) {
bucket.text.dynamicLayoutVertexBuffer.updateData(dynamicLayoutVertexArray);
if (globeExtVertexArray && bucket.text.globeExtVertexBuffer) {
bucket.text.globeExtVertexBuffer.updateData(globeExtVertexArray);
}
} else {
bucket.icon.dynamicLayoutVertexBuffer.updateData(dynamicLayoutVertexArray);
if (globeExtVertexArray && bucket.icon.globeExtVertexBuffer) {
bucket.icon.globeExtVertexBuffer.updateData(globeExtVertexArray);
}
}
}
function placeFirstAndLastGlyph(fontScale, glyphOffsetArray, lineOffsetX, lineOffsetY, flip, anchorPoint, tileAnchorPoint, symbol, lineVertexArray, labelPlaneMatrix, projectionCache, elevationParams, returnPathInTileCoords, projection, tileID, pitchWithMap, textMaxAngleThreshold) {
const { lineStartIndex, glyphStartIndex, segment } = symbol;
const glyphEndIndex = glyphStartIndex + symbol.numGlyphs;
const lineEndIndex = lineStartIndex + symbol.lineLength;
const firstGlyphOffset = glyphOffsetArray.getoffsetX(glyphStartIndex);
const lastGlyphOffset = glyphOffsetArray.getoffsetX(glyphEndIndex - 1);
const firstPlacedGlyph = placeGlyphAlongLine(
fontScale * firstGlyphOffset,
lineOffsetX,
lineOffsetY,
flip,
anchorPoint,
tileAnchorPoint,
segment,
lineStartIndex,
lineEndIndex,
lineVertexArray,
labelPlaneMatrix,
projectionCache,
elevationParams,
returnPathInTileCoords,
true,
projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
);
if (!firstPlacedGlyph)
return null;
const lastPlacedGlyph = placeGlyphAlongLine(
fontScale * lastGlyphOffset,
lineOffsetX,
lineOffsetY,
flip,
anchorPoint,
tileAnchorPoint,
segment,
lineStartIndex,
lineEndIndex,
lineVertexArray,
labelPlaneMatrix,
projectionCache,
elevationParams,
returnPathInTileCoords,
true,
projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
);
if (!lastPlacedGlyph)
return null;
return { first: firstPlacedGlyph, last: lastPlacedGlyph };
}
function isInFlipRetainRange(dx, dy) {
return dx === 0 || Math.abs(dy / dx) > maxTangent;
}
function requiresOrientationChange(writingMode, flipState, dx, dy) {
if (writingMode === index$1.WritingMode.horizontal && Math.abs(dy) > Math.abs(dx)) {
return { useVertical: true };
}
if (writingMode === index$1.WritingMode.vertical) {
return dy > 0 ? { needsFlipping: true } : null;
}
if (flipState !== FlipState.unknown && isInFlipRetainRange(dx, dy)) {
return flipState === FlipState.flipRequired ? { needsFlipping: true } : null;
}
return dx < 0 ? { needsFlipping: true } : null;
}
function placeGlyphsAlongLine(symbol, fontSize, flip, keepUpright, posMatrix, labelPlaneMatrix, glCoordMatrix, glyphOffsetArray, lineVertexArray, dynamicLayoutVertexArray, globeExtVertexArray, anchorPoint, tileAnchorPoint, projectionCache, aspectRatio, elevationParams, projection, tileID, pitchWithMap, textMaxAngleThreshold) {
const fontScale = fontSize / 24;
const lineOffsetX = symbol.lineOffsetX * fontScale;
const lineOffsetY = symbol.lineOffsetY * fontScale;
const { lineStartIndex, glyphStartIndex, numGlyphs, segment, writingMode, flipState } = symbol;
const lineEndIndex = lineStartIndex + symbol.lineLength;
const addGlyph = (glyph) => {
if (globeExtVertexArray) {
const [ux, uy, uz] = glyph.up;
const offset = dynamicLayoutVertexArray.length;
index$1.updateGlobeVertexNormal(globeExtVertexArray, offset + 0, ux, uy, uz);
index$1.updateGlobeVertexNormal(globeExtVertexArray, offset + 1, ux, uy, uz);
index$1.updateGlobeVertexNormal(globeExtVertexArray, offset + 2, ux, uy, uz);
index$1.updateGlobeVertexNormal(globeExtVertexArray, offset + 3, ux, uy, uz);
}
const [x, y, z] = glyph.point;
index$1.addDynamicAttributes(dynamicLayoutVertexArray, x, y, z, glyph.angle);
};
if (numGlyphs > 1) {
const firstAndLastGlyph = placeFirstAndLastGlyph(fontScale, glyphOffsetArray, lineOffsetX, lineOffsetY, flip, anchorPoint, tileAnchorPoint, symbol, lineVertexArray, labelPlaneMatrix, projectionCache, elevationParams, false, projection, tileID, pitchWithMap, textMaxAngleThreshold);
if (!firstAndLastGlyph) {
return { notEnoughRoom: true };
}
if (keepUpright && !flip) {
let [x0, y0, z0] = firstAndLastGlyph.first.point;
let [x1, y1, z1] = firstAndLastGlyph.last.point;
[x0, y0] = project(x0, y0, z0, glCoordMatrix);
[x1, y1] = project(x1, y1, z1, glCoordMatrix);
const orientationChange = requiresOrientationChange(writingMode, flipState, (x1 - x0) * aspectRatio, y1 - y0);
symbol.flipState = orientationChange && orientationChange.needsFlipping ? FlipState.flipRequired : FlipState.flipNotRequired;
if (orientationChange) {
return orientationChange;
}
}
addGlyph(firstAndLastGlyph.first);
for (let glyphIndex = glyphStartIndex + 1; glyphIndex < glyphStartIndex + numGlyphs - 1; glyphIndex++) {
const glyph = placeGlyphAlongLine(
fontScale * glyphOffsetArray.getoffsetX(glyphIndex),
lineOffsetX,
lineOffsetY,
flip,
anchorPoint,
tileAnchorPoint,
segment,
lineStartIndex,
lineEndIndex,
lineVertexArray,
labelPlaneMatrix,
projectionCache,
elevationParams,
false,
false,
projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
);
if (!glyph) {
dynamicLayoutVertexArray.length -= 4 * (glyphIndex - glyphStartIndex);
return { notEnoughRoom: true };
}
addGlyph(glyph);
}
addGlyph(firstAndLastGlyph.last);
} else {
if (keepUpright && !flip) {
const a = project(tileAnchorPoint.x, tileAnchorPoint.y, 0, posMatrix);
const tileVertexIndex = lineStartIndex + segment + 1;
const tileSegmentEnd = new index$1.Point(lineVertexArray.getx(tileVertexIndex), lineVertexArray.gety(tileVertexIndex));
const projectedVertex = project(tileSegmentEnd.x, tileSegmentEnd.y, 0, posMatrix);
const b = projectedVertex[3] > 0 ? projectedVertex : projectTruncatedLineSegment(tileAnchorPoint, tileSegmentEnd, a, 1, posMatrix, void 0, projection, tileID.canonical);
const orientationChange = requiresOrientationChange(writingMode, flipState, (b[0] - a[0]) * aspectRatio, b[1] - a[1]);
symbol.flipState = orientationChange && orientationChange.needsFlipping ? FlipState.flipRequired : FlipState.flipNotRequired;
if (orientationChange) {
return orientationChange;
}
}
const singleGlyph = placeGlyphAlongLine(
fontScale * glyphOffsetArray.getoffsetX(glyphStartIndex),
lineOffsetX,
lineOffsetY,
flip,
anchorPoint,
tileAnchorPoint,
segment,
lineStartIndex,
lineEndIndex,
lineVertexArray,
labelPlaneMatrix,
projectionCache,
elevationParams,
false,
false,
projection,
tileID,
pitchWithMap,
textMaxAngleThreshold
);
if (!singleGlyph) {
return { notEnoughRoom: true };
}
addGlyph(singleGlyph);
}
return {};
}
function elevatePointAndProject(p, tileID, posMatrix, projection, elevationParams) {
const { x, y, z } = projection.projectTilePoint(p.x, p.y, tileID);
if (!elevationParams) {
return project(x, y, z, posMatrix);
}
const [dx, dy, dz] = elevationParams.getElevation(p, elevationParams.elevation, elevationParams.elevationFeature);
return project(x + dx, y + dy, z + dz, posMatrix);
}
function projectTruncatedLineSegment(previousTilePoint, currentTilePoint, previousProjectedPoint, minimumLength, projectionMatrix, elevationParams, projection, tileID) {
const unitVertex = previousTilePoint.sub(currentTilePoint)._unit()._add(previousTilePoint);
const projectedUnit = elevatePointAndProject(unitVertex, tileID, projectionMatrix, projection, elevationParams);
index$1.sub(projectedUnit, previousProjectedPoint, projectedUnit);
index$1.normalize(projectedUnit, projectedUnit);
return index$1.scaleAndAdd(projectedUnit, previousProjectedPoint, projectedUnit, minimumLength);
}
function placeGlyphAlongLine(offsetX, lineOffsetX, lineOffsetY, flip, anchorPoint, tileAnchorPoint, anchorSegment, lineStartIndex, lineEndIndex, lineVertexArray, labelPlaneMatrix, projectionCache, elevationParams, returnPathInTileCoords, endGlyph, reprojection, tileID, pitchWithMap, textMaxAngleThreshold) {
const combinedOffsetX = flip ? offsetX - lineOffsetX : offsetX + lineOffsetX;
let dir = combinedOffsetX > 0 ? 1 : -1;
let angle = 0;
if (flip) {
dir *= -1;
angle = Math.PI;
}
if (dir < 0) angle += Math.PI;
let currentIndex = lineStartIndex + anchorSegment + (dir > 0 ? 0 : 1) | 0;
let current = anchorPoint;
let prev = anchorPoint;
let distanceToPrev = 0;
let currentSegmentDistance = 0;
const absOffsetX = Math.abs(combinedOffsetX);
const pathVertices = [];
const tilePath = [];
let currentVertex = tileAnchorPoint;
let prevVertex = currentVertex;
let prevToCurrent = index$1.zero([]);
const getTruncatedLineSegment = () => {
return projectTruncatedLineSegment(prevVertex, currentVertex, prev, absOffsetX - distanceToPrev + 1, labelPlaneMatrix, elevationParams, reprojection, tileID.canonical);
};
while (distanceToPrev + currentSegmentDistance <= absOffsetX) {
currentIndex += dir;
if (currentIndex < lineStartIndex || currentIndex >= lineEndIndex)
return null;
prev = current;
prevVertex = currentVertex;
pathVertices.push(prev);
if (returnPathInTileCoords) tilePath.push(prevVertex);
currentVertex = new index$1.Point(lineVertexArray.getx(currentIndex), lineVertexArray.gety(currentIndex));
current = projectionCache[currentIndex];
if (!current) {
const projection = elevatePointAndProject(currentVertex, tileID.canonical, labelPlaneMatrix, reprojection, elevationParams);
if (projection[3] > 0) {
current = projectionCache[currentIndex] = projection;
} else {
current = getTruncatedLineSegment();
}
}
distanceToPrev += currentSegmentDistance;
const nextPrevToCurrent = index$1.sub([], current, prev);
const nextSegmentDistance = index$1.distance(prev, current);
if (lineOffsetY) {
if (nextSegmentDistance > 0 && currentSegmentDistance > 0) {
const cosTheta = index$1.dot(prevToCurrent, nextPrevToCurrent) / (currentSegmentDistance * nextSegmentDistance);
if (cosTheta < textMaxAngleThreshold) {
return null;
}
}
}
currentSegmentDistance = nextSegmentDistance;
prevToCurrent = nextPrevToCurrent;
}
if (endGlyph && elevationParams) {
if (projectionCache[currentIndex]) {
current = getTruncatedLineSegment();
currentSegmentDistance = index$1.distance(prev, current);
prevToCurrent = index$1.sub([], current, prev);
}
projectionCache[currentIndex] = current;
}
const segmentInterpolationT = (absOffsetX - distanceToPrev) / currentSegmentDistance;
const tilePoint = currentVertex.sub(prevVertex)._mult(segmentInterpolationT)._add(prevVertex);
const labelPlanePoint = index$1.scaleAndAdd([], prev, prevToCurrent, segmentInterpolationT);
let axisZ = [0, 0, 1];
let diffX = prevToCurrent[0];
let diffY = prevToCurrent[1];
if (pitchWithMap) {
axisZ = reprojection.upVector(tileID.canonical, tilePoint.x, tilePoint.y);
if (axisZ[0] !== 0 || axisZ[1] !== 0 || axisZ[2] !== 1) {
const axisX = [axisZ[2], 0, -axisZ[0]];
const axisY = index$1.cross([], axisZ, axisX);
index$1.normalize(axisX, axisX);
index$1.normalize(axisY, axisY);
diffX = index$1.dot(prevToCurrent, axisX);
diffY = index$1.dot(prevToCurrent, axisY);
}
}
if (lineOffsetY) {
const offsetDir = index$1.cross([], axisZ, prevToCurrent);
index$1.normalize(offsetDir, offsetDir);
index$1.scaleAndAdd(labelPlanePoint, labelPlanePoint, offsetDir, lineOffsetY * dir);
}
const segmentAngle = angle + Math.atan2(diffY, diffX);
pathVertices.push(labelPlanePoint);
if (returnPathInTileCoords) {
tilePath.push(tilePoint);
}
return {
point: labelPlanePoint,
angle: segmentAngle,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
path: pathVertices,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
tilePath,
up: axisZ
};
}
function hideGlyphs(num, dynamicLayoutVertexArray) {
const offset = dynamicLayoutVertexArray.length;
const end = offset + 4 * num;
dynamicLayoutVertexArray.resize(end);
dynamicLayoutVertexArray.float32.fill(-Infinity, offset * 4, end * 4);
}
function xyTransformMat4(out, a, m) {
const x = a[0], y = a[1];
out[0] = m[0] * x + m[4] * y + m[12];
out[1] = m[1] * x + m[5] * y + m[13];
out[3] = m[3] * x + m[7] * y + m[15];
return out;
}
const viewportPadding = 100;
class CollisionIndex {
constructor(transform, fogState, grid = new GridIndex(transform.width + 2 * viewportPadding, transform.height + 2 * viewportPadding, 25), ignoredGrid = new GridIndex(transform.width + 2 * viewportPadding, transform.height + 2 * viewportPadding, 25)) {
this.transform = transform;
this.grid = grid;
this.ignoredGrid = ignoredGrid;
this.pitchfactor = Math.cos(transform._pitch) * transform.cameraToCenterDistance;
this.screenRightBoundary = transform.width + viewportPadding;
this.screenBottomBoundary = transform.height + viewportPadding;
this.gridRightBoundary = transform.width + 2 * viewportPadding;
this.gridBottomBoundary = transform.height + 2 * viewportPadding;
this.fogState = fogState;
}
placeCollisionBox(bucket, scale, collisionBox, mercatorCenter, invMatrix, projectedPosOnLabelSpace, shift, allowOverlap, textPixelRatio, posMatrix, collisionGroupPredicate) {
index$1.assert(!this.transform.elevation || collisionBox.elevation !== void 0);
let projectedAnchorX = collisionBox.projectedAnchorX;
let projectedAnchorY = collisionBox.projectedAnchorY;
let projectedAnchorZ = collisionBox.projectedAnchorZ;
const anchorX = collisionBox.tileAnchorX;
const anchorY = collisionBox.tileAnchorY;
const elevation = collisionBox.elevation;
const tileID = collisionBox.tileID;
const projection = bucket.getProjection();
if (elevation && tileID) {
const [ux, uy, uz] = projection.upVector(tileID.canonical, collisionBox.tileAnchorX, collisionBox.tileAnchorY);
const upScale = projection.upVectorScale(tileID.canonical, this.transform.center.lat, this.transform.worldSize).metersToTile;
projectedAnchorX += ux * elevation * upScale;
projectedAnchorY += uy * elevation * upScale;
projectedAnchorZ += uz * elevation * upScale;
}
const bucketIsGlobeProjection = bucket.projection.name === "globe";
const globeToMercator = bucket.projection.name === "globe" ? index$1.globeToMercatorTransition(this.transform.zoom) : 0;
const isGlobeToMercatorTransition = globeToMercator < 1;
if (tileID && bucketIsGlobeProjection && isGlobeToMercatorTransition && !projectedPosOnLabelSpace) {
const tilesCount = 1 << tileID.canonical.z;
const mercator = index$1.fromValues(anchorX, anchorY);
index$1.scale(mercator, mercator, 1 / index$1.EXTENT);
index$1.add(mercator, mercator, index$1.fromValues(tileID.canonical.x, tileID.canonical.y));
index$1.scale(mercator, mercator, 1 / tilesCount);
index$1.sub$1(mercator, mercator, index$1.fromValues(mercatorCenter[0], mercatorCenter[1]));
mercator[0] = index$1.wrap(mercator[0], -0.5, 0.5);
index$1.scale(mercator, mercator, index$1.EXTENT);
const mercatorPosition = index$1.fromValues$1(mercator[0], mercator[1], index$1.EXTENT / (2 * Math.PI), 1);
index$1.transformMat4$1(mercatorPosition, mercatorPosition, invMatrix);
projectedAnchorX = index$1.number(projectedAnchorX, mercatorPosition[0], globeToMercator);
projectedAnchorY = index$1.number(projectedAnchorY, mercatorPosition[1], globeToMercator);
projectedAnchorZ = index$1.number(projectedAnchorZ, mercatorPosition[2], globeToMercator);
}
const checkOcclusion = projection.name === "globe" || !!elevation || this.transform.pitch > 0;
const projectedPoint = this.projectAndGetPerspectiveRatio(posMatrix, projectedAnchorX, projectedAnchorY, projectedAnchorZ, collisionBox.tileID, checkOcclusion, projection);
const tileToViewport = textPixelRatio * projectedPoint.perspectiveRatio;
const tlX = (collisionBox.x1 * scale + shift.x - collisionBox.padding) * tileToViewport + projectedPoint.point.x;
const tlY = (collisionBox.y1 * scale + shift.y - collisionBox.padding) * tileToViewport + projectedPoint.point.y;
const brX = (collisionBox.x2 * scale + shift.x + collisionBox.padding) * tileToViewport + projectedPoint.point.x;
const brY = (collisionBox.y2 * scale + shift.y + collisionBox.padding) * tileToViewport + projectedPoint.point.y;
const minPerspectiveRatio = 0.55;
const isClipped = projectedPoint.perspectiveRatio <= minPerspectiveRatio || projectedPoint.occluded;
if (!this.isInsideGrid(tlX, tlY, brX, brY) || !allowOverlap && this.grid.hitTest(tlX, tlY, brX, brY, collisionGroupPredicate) || isClipped) {
return {
box: [],
offscreen: false,
occluded: projectedPoint.occluded
};
}
return {
box: [tlX, tlY, brX, brY],
offscreen: this.isOffscreen(tlX, tlY, brX, brY),
occluded: false
};
}
placeCollisionCircles(bucket, allowOverlap, symbol, symbolIndex, lineVertexArray, glyphOffsetArray, fontSize, posMatrix, labelPlaneMatrix, labelToScreenMatrix, showCollisionCircles, pitchWithMap, collisionGroupPredicate, circlePixelDiameter, textPixelPadding, tileID) {
const placedCollisionCircles = [];
const elevation = this.transform.elevation;
const projection = bucket.getProjection();
const renderElevatedRoads = bucket.elevationType === "road";
const hasElevation = !!elevation || renderElevatedRoads;
const getElevation = index$1.Elevation.getAtTileOffsetFunc(tileID, this.transform.center.lat, this.transform.worldSize, projection);
const tileAnchorPoint = new index$1.Point(symbol.tileAnchorX, symbol.tileAnchorY);
const tileUnitAnchorPoint = new index$1.Point(symbol.tileAnchorX, symbol.tileAnchorY);
let { x: anchorX, y: anchorY, z: anchorZ } = projection.projectTilePoint(tileUnitAnchorPoint.x, tileUnitAnchorPoint.y, tileID.canonical);
let elevationParams = null;
if (hasElevation) {
const elevationFeature = renderElevatedRoads ? bucket.getElevationFeatureForText(symbolIndex) : null;
elevationParams = {
getElevation,
elevation,
elevationFeature
};
const [dx, dy, dz] = getElevation(tileAnchorPoint, elevation, elevationFeature);
anchorX += dx;
anchorY += dy;
anchorZ += dz;
}
const isGlobe = projection.name === "globe";
const checkOcclusion = isGlobe || !!elevation || this.transform.pitch > 0;
const screenAnchorPoint = this.projectAndGetPerspectiveRatio(posMatrix, anchorX, anchorY, anchorZ, tileID, checkOcclusion, projection);
const { perspectiveRatio } = screenAnchorPoint;
const labelPlaneFontScale = (pitchWithMap ? fontSize / perspectiveRatio : fontSize * perspectiveRatio) / index$1.ONE_EM;
const labelPlaneAnchorPoint = project(anchorX, anchorY, anchorZ, labelPlaneMatrix);
const projectionCache = {};
const lineOffsetX = symbol.lineOffsetX * labelPlaneFontScale;
const lineOffsetY = symbol.lineOffsetY * labelPlaneFontScale;
const layout = bucket.layers[0].layout;
const textMaxAngle = index$1.degToRad(layout.get("text-max-angle"));
const maxAngleCos = Math.cos(textMaxAngle);
const firstAndLastGlyph = screenAnchorPoint.signedDistanceFromCamera > 0 ? placeFirstAndLastGlyph(
labelPlaneFontScale,
glyphOffsetArray,
lineOffsetX,
lineOffsetY,
renderElevatedRoads && symbol.flipState === 1,
// FlipState.flipRequired
// @ts-expect-error - TS2345 - Argument of type 'vec4' is not assignable to parameter of type 'vec3'.
labelPlaneAnchorPoint,
tileUnitAnchorPoint,
symbol,
lineVertexArray,
labelPlaneMatrix,
projectionCache,
hasElevation && !pitchWithMap ? elevationParams : null,
// pitchWithMap: no need to sample elevation as it has no effect when projecting using scale/rotate to tile space labelPlaneMatrix.
pitchWithMap && hasElevation,
projection,
tileID,
pitchWithMap,
maxAngleCos
) : null;
let collisionDetected = false;
let inGrid = false;
let entirelyOffscreen = true;
if (firstAndLastGlyph && !screenAnchorPoint.occluded) {
const radius = circlePixelDiameter * 0.5 * perspectiveRatio + textPixelPadding;
const screenPlaneMin = new index$1.Point(-viewportPadding, -viewportPadding);
const screenPlaneMax = new index$1.Point(this.screenRightBoundary, this.screenBottomBoundary);
const interpolator = new PathInterpolator();
const { first, last } = firstAndLastGlyph;
const firstLen = first.path.length;
let projectedPath = [];
for (let i = firstLen - 1; i >= 1; i--) {
projectedPath.push(first.path[i]);
}
for (let i = 1; i < last.path.length; i++) {
projectedPath.push(last.path[i]);
}
index$1.assert(projectedPath.length >= 2);
const circleDist = radius * 2.5;
if (labelToScreenMatrix) {
index$1.assert(pitchWithMap);
projectedPath = projectedPath.map(([x, y, z], index) => {
if (hasElevation && !isGlobe) {
const point = index < firstLen - 1 ? first.tilePath[firstLen - 1 - index] : last.tilePath[index - firstLen + 2];
index$1.assert(point);
index$1.assert(elevationParams);
z = getElevation(point, elevation, elevationParams.elevationFeature)[2];
}
return project(x, y, z, labelToScreenMatrix);
});
if (projectedPath.some((point) => point[3] <= 0)) {
projectedPath = [];
}
}
let segments = [];
if (projectedPath.length > 0) {
let minx = Infinity;
let maxx = -Infinity;
let miny = Infinity;
let maxy = -Infinity;
for (const p of projectedPath) {
minx = Math.min(minx, p[0]);
miny = Math.min(miny, p[1]);
maxx = Math.max(maxx, p[0]);
maxy = Math.max(maxy, p[1]);
}
if (maxx >= screenPlaneMin.x && minx <= screenPlaneMax.x && maxy >= screenPlaneMin.y && miny <= screenPlaneMax.y) {
segments = [projectedPath.map((p) => new index$1.Point(p[0], p[1]))];
if (minx < screenPlaneMin.x || maxx > screenPlaneMax.x || miny < screenPlaneMin.y || maxy > screenPlaneMax.y) {
segments = index$1.clipLines(segments, screenPlaneMin.x, screenPlaneMin.y, screenPlaneMax.x, screenPlaneMax.y);
}
}
}
for (const seg of segments) {
index$1.assert(seg.length > 0);
interpolator.reset(seg, radius * 0.25);
let numCircles = 0;
if (interpolator.length <= 0.5 * radius) {
numCircles = 1;
} else {
numCircles = Math.ceil(interpolator.paddedLength / circleDist) + 1;
}
for (let i = 0; i < numCircles; i++) {
const t = i / Math.max(numCircles - 1, 1);
const circlePosition = interpolator.lerp(t);
const centerX = circlePosition.x + viewportPadding;
const centerY = circlePosition.y + viewportPadding;
placedCollisionCircles.push(centerX, centerY, radius, 0);
const x1 = centerX - radius;
const y1 = centerY - radius;
const x2 = centerX + radius;
const y2 = centerY + radius;
entirelyOffscreen = entirelyOffscreen && this.isOffscreen(x1, y1, x2, y2);
inGrid = inGrid || this.isInsideGrid(x1, y1, x2, y2);
if (!allowOverlap) {
if (this.grid.hitTestCircle(centerX, centerY, radius, collisionGroupPredicate)) {
collisionDetected = true;
if (!showCollisionCircles) {
return {
circles: [],
offscreen: false,
collisionDetected,
occluded: false
};
}
}
}
}
}
}
return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
circles: !showCollisionCircles && collisionDetected || !inGrid ? [] : placedCollisionCircles,
offscreen: entirelyOffscreen,
collisionDetected,
occluded: screenAnchorPoint.occluded
};
}
/**
* Because the geometries in the CollisionIndex are an approximation of the shape of
* symbols on the map, we use the CollisionIndex to look up the symbol part of
* `queryRenderedFeatures`.
*
* @private
*/
queryRenderedSymbols(viewportQueryGeometry) {
if (viewportQueryGeometry.length === 0 || this.grid.keysLength() === 0 && this.ignoredGrid.keysLength() === 0) {
return {};
}
const query = [];
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const point of viewportQueryGeometry) {
const gridPoint = new index$1.Point(point.x + viewportPadding, point.y + viewportPadding);
minX = Math.min(minX, gridPoint.x);
minY = Math.min(minY, gridPoint.y);
maxX = Math.max(maxX, gridPoint.x);
maxY = Math.max(maxY, gridPoint.y);
query.push(gridPoint);
}
const features = this.grid.query(minX, minY, maxX, maxY).concat(this.ignoredGrid.query(minX, minY, maxX, maxY));
const seenFeatures = {};
const result = {};
for (const feature of features) {
const featureKey = feature.key;
if (seenFeatures[featureKey.bucketInstanceId] === void 0) {
seenFeatures[featureKey.bucketInstanceId] = {};
}
if (seenFeatures[featureKey.bucketInstanceId][featureKey.featureIndex]) {
continue;
}
const bbox = [
new index$1.Point(feature.x1, feature.y1),
new index$1.Point(feature.x2, feature.y1),
new index$1.Point(feature.x2, feature.y2),
new index$1.Point(feature.x1, feature.y2)
];
if (!index$1.polygonIntersectsPolygon(query, bbox)) {
continue;
}
seenFeatures[featureKey.bucketInstanceId][featureKey.featureIndex] = true;
if (result[featureKey.bucketInstanceId] === void 0) {
result[featureKey.bucketInstanceId] = [];
}
result[featureKey.bucketInstanceId].push(featureKey.featureIndex);
}
return result;
}
insertCollisionBox(collisionBox, ignorePlacement, bucketInstanceId, featureIndex, collisionGroupID) {
const grid = ignorePlacement ? this.ignoredGrid : this.grid;
const key = { bucketInstanceId, featureIndex, collisionGroupID };
grid.insert(key, collisionBox[0], collisionBox[1], collisionBox[2], collisionBox[3]);
}
insertCollisionCircles(collisionCircles, ignorePlacement, bucketInstanceId, featureIndex, collisionGroupID) {
const grid = ignorePlacement ? this.ignoredGrid : this.grid;
const key = { bucketInstanceId, featureIndex, collisionGroupID };
for (let k = 0; k < collisionCircles.length; k += 4) {
grid.insertCircle(key, collisionCircles[k], collisionCircles[k + 1], collisionCircles[k + 2]);
}
}
projectAndGetPerspectiveRatio(posMatrix, x, y, z, tileID, checkOcclusion, bucketProjection) {
const p = [x, y, z, 1];
let behindFog = false;
if (z || this.transform.pitch > 0) {
index$1.transformMat4$1(p, p, posMatrix);
const isGlobe = bucketProjection.name === "globe";
if (this.fogState && tileID && !isGlobe) {
const fogOpacity = getFogOpacityAtTileCoord(this.fogState, x, y, z, tileID.toUnwrapped(), this.transform);
behindFog = fogOpacity > FOG_SYMBOL_CLIPPING_THRESHOLD;
}
} else {
xyTransformMat4(p, p, posMatrix);
}
const w = p[3];
const a = new index$1.Point(
(p[0] / w + 1) / 2 * this.transform.width + viewportPadding,
(-p[1] / w + 1) / 2 * this.transform.height + viewportPadding
);
return {
point: a,
// See perspective ratio comment in symbol_sdf.vertex
// We're doing collision detection in viewport space so we need
// to scale down boxes in the distance
perspectiveRatio: Math.min(0.5 + 0.5 * (this.transform.getCameraToCenterDistance(bucketProjection) / w), 1.5),
signedDistanceFromCamera: w,
occluded: checkOcclusion && p[2] > w || behindFog
// Occluded by the far plane
};
}
isOffscreen(x1, y1, x2, y2) {
return x2 < viewportPadding || x1 >= this.screenRightBoundary || y2 < viewportPadding || y1 > this.screenBottomBoundary;
}
isInsideGrid(x1, y1, x2, y2) {
return x2 >= 0 && x1 < this.gridRightBoundary && y2 >= 0 && y1 < this.gridBottomBoundary;
}
/*
* Returns a matrix for transforming collision shapes to viewport coordinate space.
* Use this function to render e.g. collision circles on the screen.
* example transformation: clipPos = glCoordMatrix * viewportMatrix * circle_pos
*/
getViewportMatrix() {
const m = index$1.identity([]);
index$1.translate(m, m, [-viewportPadding, -viewportPadding, 0]);
return m;
}
}
class OpacityState {
constructor(prevState, increment, placed, skipFade) {
if (prevState) {
this.opacity = Math.max(0, Math.min(1, prevState.opacity + (prevState.placed ? increment : -increment)));
} else {
this.opacity = skipFade && placed ? 1 : 0;
}
this.placed = placed;
}
isHidden() {
return this.opacity === 0 && !this.placed;
}
}
class JointOpacityState {
constructor(prevState, increment, placedText, placedIcon, skipFade, clipped = false) {
this.text = new OpacityState(prevState ? prevState.text : null, increment, placedText, skipFade);
this.icon = new OpacityState(prevState ? prevState.icon : null, increment, placedIcon, skipFade);
this.clipped = clipped;
}
isHidden() {
return this.text.isHidden() && this.icon.isHidden();
}
}
class JointPlacement {
constructor(text, icon, skipFade, clipped = false) {
this.text = text;
this.icon = icon;
this.skipFade = skipFade;
this.clipped = clipped;
}
}
class CollisionCircleArray {
constructor() {
this.invProjMatrix = index$1.create();
this.viewportMatrix = index$1.create();
this.circles = [];
}
}
class RetainedQueryData {
constructor(bucketInstanceId, featureIndex, sourceLayerIndex, bucketIndex, tileID) {
this.bucketInstanceId = bucketInstanceId;
this.featureIndex = featureIndex;
this.sourceLayerIndex = sourceLayerIndex;
this.bucketIndex = bucketIndex;
this.tileID = tileID;
}
}
class CollisionGroups {
constructor(crossSourceCollisions) {
this.crossSourceCollisions = crossSourceCollisions;
this.maxGroupID = 0;
this.collisionGroups = {};
}
get(sourceID) {
if (!this.crossSourceCollisions) {
if (!this.collisionGroups[sourceID]) {
const nextGroupID = ++this.maxGroupID;
this.collisionGroups[sourceID] = {
ID: nextGroupID,
predicate: (key) => {
return key.collisionGroupID === nextGroupID;
}
};
}
return this.collisionGroups[sourceID];
} else {
return { ID: 0, predicate: null };
}
}
}
function calculateVariableLayoutShift(anchor, width, height, textOffset, textScale) {
const { horizontalAlign, verticalAlign } = index$1.getAnchorAlignment(anchor);
const shiftX = -(horizontalAlign - 0.5) * width;
const shiftY = -(verticalAlign - 0.5) * height;
const offset = index$1.evaluateVariableOffset(anchor, textOffset);
return new index$1.Point(
shiftX + offset[0] * textScale,
shiftY + offset[1] * textScale
);
}
function offsetShift(shiftX, shiftY, rotateWithMap, pitchWithMap, angle) {
const shift = new index$1.Point(shiftX, shiftY);
if (rotateWithMap) {
shift._rotate(pitchWithMap ? angle : -angle);
}
return shift;
}
class Placement {
constructor(transform, fadeDuration, crossSourceCollisions, prevPlacement, fogState, buildingIndex) {
this.transform = transform.clone();
this.projection = transform.projection.name;
this.collisionIndex = new CollisionIndex(this.transform, fogState);
this.buildingIndex = buildingIndex;
this.placements = {};
this.opacities = {};
this.variableOffsets = {};
this.stale = false;
this.commitTime = 0;
this.fadeDuration = fadeDuration;
this.retainedQueryData = {};
this.collisionGroups = new CollisionGroups(crossSourceCollisions);
this.collisionCircleArrays = {};
this.prevPlacement = prevPlacement;
if (prevPlacement) {
prevPlacement.prevPlacement = void 0;
}
this.placedOrientations = {};
this.lastReplacementSourceUpdateTime = 0;
}
getBucketParts(results, styleLayer, tile, sortAcrossTiles, scaleFactor = 1) {
const symbolBucket = tile.getBucket(styleLayer);
const bucketFeatureIndex = tile.latestFeatureIndex;
if (!symbolBucket || !bucketFeatureIndex || styleLayer.fqid !== symbolBucket.layerIds[0])
return;
const layout = symbolBucket.layers[0].layout;
const paint = symbolBucket.layers[0].paint;
const collisionBoxArray = tile.collisionBoxArray;
const scale = Math.pow(2, this.transform.zoom - tile.tileID.overscaledZ);
const textPixelRatio = tile.tileSize / index$1.EXTENT;
const unwrappedTileID = tile.tileID.toUnwrapped();
this.transform.setProjection(symbolBucket.projection);
const posMatrix = getSymbolPlacementTileProjectionMatrix(tile.tileID, symbolBucket.getProjection(), this.transform, this.projection);
const pitchWithMap = layout.get("text-pitch-alignment") === "map";
const rotateWithMap = layout.get("text-rotation-alignment") === "map";
styleLayer.compileFilter(styleLayer.options);
const dynamicFilter = styleLayer.dynamicFilter();
const dynamicFilterNeedsFeature = styleLayer.dynamicFilterNeedsFeature();
const pixelsToTiles = this.transform.calculatePixelsToTileUnitsMatrix(tile);
const textLabelPlaneMatrix = getLabelPlaneMatrixForPlacement(
posMatrix,
tile.tileID.canonical,
pitchWithMap,
rotateWithMap,
this.transform,
symbolBucket.getProjection(),
pixelsToTiles
);
let labelToScreenMatrix = null;
const invMatrix = symbolBucket.getProjection().createInversionMatrix(this.transform, tile.tileID.canonical);
if (pitchWithMap) {
const glMatrix = getGlCoordMatrix(
posMatrix,
tile.tileID.canonical,
pitchWithMap,
rotateWithMap,
this.transform,
symbolBucket.getProjection(),
pixelsToTiles
);
labelToScreenMatrix = index$1.multiply([], this.transform.labelPlaneMatrix, glMatrix);
}
let clippingData = null;
index$1.assert(!!tile.latestFeatureIndex);
if (!!dynamicFilter && tile.latestFeatureIndex) {
clippingData = {
unwrappedTileID,
dynamicFilter,
dynamicFilterNeedsFeature
};
}
this.retainedQueryData[symbolBucket.bucketInstanceId] = new RetainedQueryData(
symbolBucket.bucketInstanceId,
bucketFeatureIndex,
symbolBucket.sourceLayerIndex,
symbolBucket.index,
tile.tileID
);
const [textSizeScaleRangeMin, textSizeScaleRangeMax] = symbolBucket.layers[0].layout.get("text-size-scale-range");
const textScaleFactor = index$1.clamp(scaleFactor, textSizeScaleRangeMin, textSizeScaleRangeMax);
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const iconScaleFactor = index$1.clamp(scaleFactor, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
const mercatorCenter = [
index$1.mercatorXfromLng(this.transform.center.lng),
index$1.mercatorYfromLat(this.transform.center.lat)
];
const parameters = {
bucket: symbolBucket,
layout,
paint,
posMatrix,
invMatrix,
mercatorCenter,
textLabelPlaneMatrix,
labelToScreenMatrix,
clippingData,
scale,
textPixelRatio,
holdingForFade: tile.holdingForFade(),
collisionBoxArray,
partiallyEvaluatedTextSize: index$1.evaluateSizeForZoom(symbolBucket.textSizeData, this.transform.zoom, textScaleFactor),
partiallyEvaluatedIconSize: index$1.evaluateSizeForZoom(symbolBucket.iconSizeData, this.transform.zoom, iconScaleFactor),
collisionGroup: this.collisionGroups.get(symbolBucket.sourceID),
latestFeatureIndex: tile.latestFeatureIndex
};
if (sortAcrossTiles) {
for (const range of symbolBucket.sortKeyRanges) {
const { sortKey, symbolInstanceStart, symbolInstanceEnd } = range;
results.push({ sortKey, symbolInstanceStart, symbolInstanceEnd, parameters });
}
} else {
results.push({
symbolInstanceStart: 0,
symbolInstanceEnd: symbolBucket.symbolInstances.length,
parameters
});
}
}
attemptAnchorPlacement(anchor, textBox, mercatorCenter, invMatrix, projectedPosOnLabelSpace, width, height, textScale, rotateWithMap, pitchWithMap, textPixelRatio, posMatrix, collisionGroup, textAllowOverlap, symbolInstance, boxIndex, bucket, orientation, iconBox, textSize, iconSize) {
const { textOffset0, textOffset1, crossTileID } = symbolInstance;
const textOffset = [textOffset0, textOffset1];
const shift = calculateVariableLayoutShift(anchor, width, height, textOffset, textScale);
const placedGlyphBoxes = this.collisionIndex.placeCollisionBox(
bucket,
textScale,
textBox,
mercatorCenter,
invMatrix,
projectedPosOnLabelSpace,
offsetShift(shift.x, shift.y, rotateWithMap, pitchWithMap, this.transform.angle),
textAllowOverlap,
textPixelRatio,
posMatrix,
collisionGroup.predicate
);
if (iconBox) {
const size = bucket.getSymbolInstanceIconSize(iconSize, this.transform.zoom, symbolInstance.placedIconSymbolIndex);
const placedIconBoxes = this.collisionIndex.placeCollisionBox(
bucket,
size,
iconBox,
mercatorCenter,
invMatrix,
projectedPosOnLabelSpace,
offsetShift(shift.x, shift.y, rotateWithMap, pitchWithMap, this.transform.angle),
textAllowOverlap,
textPixelRatio,
posMatrix,
collisionGroup.predicate
);
if (placedIconBoxes.box.length === 0) return;
}
if (placedGlyphBoxes.box.length > 0) {
let prevAnchor;
if (this.prevPlacement && this.prevPlacement.variableOffsets[crossTileID] && this.prevPlacement.placements[crossTileID] && this.prevPlacement.placements[crossTileID].text) {
prevAnchor = this.prevPlacement.variableOffsets[crossTileID].anchor;
}
index$1.assert(crossTileID !== 0);
this.variableOffsets[crossTileID] = {
textOffset,
width,
height,
anchor,
textScale,
prevAnchor
};
this.markUsedJustification(bucket, anchor, symbolInstance, orientation);
if (bucket.allowVerticalPlacement) {
this.markUsedOrientation(bucket, orientation, symbolInstance);
this.placedOrientations[crossTileID] = orientation;
}
return { shift, placedGlyphBoxes };
}
}
placeLayerBucketPart(bucketPart, seenCrossTileIDs, showCollisionBoxes, updateCollisionBoxIfNecessary, scaleFactor = 1) {
const {
bucket,
layout,
paint,
posMatrix,
textLabelPlaneMatrix,
labelToScreenMatrix,
clippingData,
textPixelRatio,
mercatorCenter,
invMatrix,
holdingForFade,
collisionBoxArray,
partiallyEvaluatedTextSize,
partiallyEvaluatedIconSize,
collisionGroup,
latestFeatureIndex
} = bucketPart.parameters;
const textOptional = layout.get("text-optional");
const iconOptional = layout.get("icon-optional");
const textAllowOverlap = layout.get("text-allow-overlap");
const iconAllowOverlap = layout.get("icon-allow-overlap");
const textRotateWithMap = layout.get("text-rotation-alignment") === "map";
const iconRotateWithMap = layout.get("icon-rotation-alignment") === "map";
const pitchWithMap = layout.get("text-pitch-alignment") === "map";
const symbolZOffset = paint.get("symbol-z-offset");
const elevationFromSea = layout.get("symbol-elevation-reference") === "sea";
const symbolPlacement = layout.get("symbol-placement");
const [textSizeScaleRangeMin, textSizeScaleRangeMax] = layout.get("text-size-scale-range");
const [iconSizeScaleRangeMin, iconSizeScaleRangeMax] = layout.get("icon-size-scale-range");
const textScaleFactor = index$1.clamp(scaleFactor, textSizeScaleRangeMin, textSizeScaleRangeMax);
const iconScaleFactor = index$1.clamp(scaleFactor, iconSizeScaleRangeMin, iconSizeScaleRangeMax);
const textVariableAnchor = layout.get("text-variable-anchor");
const isTextPlacedAlongLine = textRotateWithMap && symbolPlacement !== "point";
const isIconPlacedAlongLine = iconRotateWithMap && symbolPlacement !== "point";
const hasVariableAnchors = textVariableAnchor && bucket.hasTextData();
const updateTextFitIcon = bucket.hasIconTextFit() && hasVariableAnchors && bucket.hasIconData();
this.transform.setProjection(bucket.projection);
const textProjectedPosOnLabelSpace = hasVariableAnchors || isTextPlacedAlongLine;
const iconProjectedPosOnLabelSpace = isIconPlacedAlongLine || updateTextFitIcon;
let alwaysShowText = textAllowOverlap && (iconAllowOverlap || !bucket.hasIconData() || iconOptional);
let alwaysShowIcon = iconAllowOverlap && (textAllowOverlap || !bucket.hasTextData() || textOptional);
const needsFeatureForElevation = !symbolZOffset.isConstant();
if (!bucket.collisionArrays && collisionBoxArray) {
bucket.deserializeCollisionBoxes(collisionBoxArray);
}
if (showCollisionBoxes && updateCollisionBoxIfNecessary) {
bucket.updateCollisionDebugBuffers(this.transform.zoom, collisionBoxArray, textScaleFactor, iconScaleFactor);
}
const placeSymbol = (symbolInstance, boxIndex, collisionArrays) => {
const { crossTileID, numVerticalGlyphVertices } = symbolInstance;
let feature = null;
if (clippingData && clippingData.dynamicFilterNeedsFeature || needsFeatureForElevation) {
const retainedQueryData = this.retainedQueryData[bucket.bucketInstanceId];
feature = latestFeatureIndex.loadFeature({
featureIndex: symbolInstance.featureIndex,
bucketIndex: retainedQueryData.bucketIndex,
sourceLayerIndex: retainedQueryData.sourceLayerIndex,
layoutVertexArrayOffset: 0
});
const worldview = feature.properties ? feature.properties.worldview : null;
if (bucket.localizable && bucket.worldview && typeof worldview === "string") {
if (worldview === "all") {
feature.properties["$localized"] = true;
} else if (worldview.split(",").includes(bucket.worldview)) {
feature.properties["$localized"] = true;
feature.properties["worldview"] = bucket.worldview;
} else {
return;
}
}
}
if (clippingData) {
const globals = {
zoom: this.transform.zoom,
pitch: this.transform.pitch,
worldview: bucket.worldview
};
const canonicalTileId = this.retainedQueryData[bucket.bucketInstanceId].tileID.canonical;
const filterFunc = clippingData.dynamicFilter;
const shouldClip = !filterFunc(globals, feature, canonicalTileId, new index$1.Point(symbolInstance.tileAnchorX, symbolInstance.tileAnchorY), this.transform.calculateDistanceTileData(clippingData.unwrappedTileID));
if (shouldClip) {
this.placements[crossTileID] = new JointPlacement(false, false, false, true);
seenCrossTileIDs.add(crossTileID);
return;
}
}
const symbolZOffsetValue = symbolZOffset.evaluate(feature, {});
if (seenCrossTileIDs.has(crossTileID)) return;
if (holdingForFade) {
this.placements[crossTileID] = new JointPlacement(false, false, false);
return;
}
let placeText = false;
let placeIcon = false;
let offscreen = true;
let textOccluded = false;
let iconOccluded = false;
let shift = null;
let placed = { box: null, offscreen: null, occluded: null };
let placedVerticalText = { box: null, offscreen: null, occluded: null };
let placedGlyphBoxes = null;
let placedGlyphCircles = null;
let placedIconBoxes = null;
let textFeatureIndex = 0;
let verticalTextFeatureIndex = 0;
let iconFeatureIndex = 0;
if (collisionArrays.textFeatureIndex) {
textFeatureIndex = collisionArrays.textFeatureIndex;
} else if (symbolInstance.useRuntimeCollisionCircles) {
textFeatureIndex = symbolInstance.featureIndex;
}
if (collisionArrays.verticalTextFeatureIndex) {
verticalTextFeatureIndex = collisionArrays.verticalTextFeatureIndex;
}
const elevationFeature = bucket.elevationFeatures ? bucket.elevationFeatures[symbolInstance.elevationFeatureIndex] : void 0;
const updateBoxData = (box) => {
box.tileID = this.retainedQueryData[bucket.bucketInstanceId].tileID;
const elevation = this.transform.elevation;
box.elevation = elevationFromSea ? symbolZOffsetValue : symbolZOffsetValue + index$1.Elevation.getAtTileOffset(box.tileID, new index$1.Point(box.tileAnchorX, box.tileAnchorY), elevation, elevationFeature);
box.elevation += symbolInstance.zOffset;
};
const textBox = collisionArrays.textBox;
if (textBox) {
updateBoxData(textBox);
const updatePreviousOrientationIfNotPlaced = (isPlaced) => {
let previousOrientation = index$1.WritingMode.horizontal;
if (bucket.allowVerticalPlacement && !isPlaced && this.prevPlacement) {
const prevPlacedOrientation = this.prevPlacement.placedOrientations[crossTileID];
if (prevPlacedOrientation) {
this.placedOrientations[crossTileID] = prevPlacedOrientation;
previousOrientation = prevPlacedOrientation;
this.markUsedOrientation(bucket, previousOrientation, symbolInstance);
}
}
return previousOrientation;
};
const placeTextForPlacementModes = (placeHorizontalFn, placeVerticalFn) => {
if (bucket.allowVerticalPlacement && numVerticalGlyphVertices > 0 && collisionArrays.verticalTextBox) {
for (const placementMode of bucket.writingModes) {
if (placementMode === index$1.WritingMode.vertical) {
placed = placeVerticalFn();
placedVerticalText = placed;
} else {
placed = placeHorizontalFn();
}
if (placed && placed.box && placed.box.length) break;
}
} else {
placed = placeHorizontalFn();
}
};
if (!textVariableAnchor) {
const placeBox = (collisionTextBox, orientation) => {
const textScale = bucket.getSymbolInstanceTextSize(partiallyEvaluatedTextSize, symbolInstance, this.transform.zoom, boxIndex);
const placedFeature = this.collisionIndex.placeCollisionBox(
bucket,
textScale,
collisionTextBox,
mercatorCenter,
invMatrix,
textProjectedPosOnLabelSpace,
new index$1.Point(0, 0),
textAllowOverlap,
textPixelRatio,
posMatrix,
collisionGroup.predicate
);
if (placedFeature && placedFeature.box && placedFeature.box.length) {
this.markUsedOrientation(bucket, orientation, symbolInstance);
this.placedOrientations[crossTileID] = orientation;
}
return placedFeature;
};
const placeHorizontal = () => {
return placeBox(textBox, index$1.WritingMode.horizontal);
};
const placeVertical = () => {
const verticalTextBox = collisionArrays.verticalTextBox;
if (bucket.allowVerticalPlacement && numVerticalGlyphVertices > 0 && verticalTextBox) {
updateBoxData(verticalTextBox);
return placeBox(verticalTextBox, index$1.WritingMode.vertical);
}
return { box: null, offscreen: null, occluded: null };
};
placeTextForPlacementModes(
placeHorizontal,
placeVertical
);
const isPlaced = placed && placed.box && placed.box.length;
updatePreviousOrientationIfNotPlaced(!!isPlaced);
} else {
let anchors = textVariableAnchor;
if (this.prevPlacement && this.prevPlacement.variableOffsets[crossTileID]) {
const prevOffsets = this.prevPlacement.variableOffsets[crossTileID];
if (anchors.indexOf(prevOffsets.anchor) > 0) {
anchors = anchors.filter((anchor) => anchor !== prevOffsets.anchor);
anchors.unshift(prevOffsets.anchor);
}
}
const placeBoxForVariableAnchors = (collisionTextBox, collisionIconBox, orientation) => {
const textScale = bucket.getSymbolInstanceTextSize(partiallyEvaluatedTextSize, symbolInstance, this.transform.zoom, boxIndex);
const width = (collisionTextBox.x2 - collisionTextBox.x1) * textScale + 2 * collisionTextBox.padding;
const height = (collisionTextBox.y2 - collisionTextBox.y1) * textScale + 2 * collisionTextBox.padding;
const variableIconBox = symbolInstance.hasIconTextFit && !iconAllowOverlap ? collisionIconBox : null;
if (variableIconBox) updateBoxData(variableIconBox);
let placedBox = { box: [], offscreen: false, occluded: false };
const placementAttempts = textAllowOverlap ? anchors.length * 2 : anchors.length;
for (let i = 0; i < placementAttempts; ++i) {
const anchor = anchors[i % anchors.length];
const allowOverlap = i >= anchors.length;
const result = this.attemptAnchorPlacement(
anchor,
collisionTextBox,
mercatorCenter,
invMatrix,
textProjectedPosOnLabelSpace,
width,
height,
textScale,
textRotateWithMap,
pitchWithMap,
textPixelRatio,
posMatrix,
collisionGroup,
allowOverlap,
symbolInstance,
boxIndex,
bucket,
orientation,
variableIconBox,
partiallyEvaluatedTextSize,
partiallyEvaluatedIconSize
);
if (result) {
placedBox = result.placedGlyphBoxes;
if (placedBox && placedBox.box && placedBox.box.length) {
placeText = true;
shift = result.shift;
break;
}
}
}
return placedBox;
};
const placeHorizontal = () => {
return placeBoxForVariableAnchors(textBox, collisionArrays.iconBox, index$1.WritingMode.horizontal);
};
const placeVertical = () => {
const verticalTextBox = collisionArrays.verticalTextBox;
if (verticalTextBox) updateBoxData(verticalTextBox);
const wasPlaced = placed && placed.box && placed.box.length;
if (bucket.allowVerticalPlacement && !wasPlaced && numVerticalGlyphVertices > 0 && verticalTextBox) {
return placeBoxForVariableAnchors(verticalTextBox, collisionArrays.verticalIconBox, index$1.WritingMode.vertical);
}
return { box: null, offscreen: null, occluded: null };
};
placeTextForPlacementModes(placeHorizontal, placeVertical);
if (placed) {
placeText = placed.box;
offscreen = placed.offscreen;
textOccluded = placed.occluded;
}
const isPlaced = placed && placed.box;
const prevOrientation = updatePreviousOrientationIfNotPlaced(!!isPlaced);
if (!placeText && this.prevPlacement) {
const prevOffset = this.prevPlacement.variableOffsets[crossTileID];
if (prevOffset) {
this.variableOffsets[crossTileID] = prevOffset;
this.markUsedJustification(bucket, prevOffset.anchor, symbolInstance, prevOrientation);
}
}
}
}
placedGlyphBoxes = placed;
placeText = placedGlyphBoxes && placedGlyphBoxes.box && placedGlyphBoxes.box.length > 0;
offscreen = placedGlyphBoxes && placedGlyphBoxes.offscreen;
textOccluded = placedGlyphBoxes && placedGlyphBoxes.occluded;
if (symbolInstance.useRuntimeCollisionCircles) {
const placedSymbolIndex = symbolInstance.centerJustifiedTextSymbolIndex >= 0 ? symbolInstance.centerJustifiedTextSymbolIndex : symbolInstance.verticalPlacedTextSymbolIndex;
const placedSymbol = bucket.text.placedSymbolArray.get(placedSymbolIndex);
const fontSize = index$1.evaluateSizeForFeature(bucket.textSizeData, partiallyEvaluatedTextSize, placedSymbol);
const textPixelPadding = layout.get("text-padding");
const circlePixelDiameter = symbolInstance.collisionCircleDiameter * fontSize / index$1.ONE_EM;
placedGlyphCircles = this.collisionIndex.placeCollisionCircles(
bucket,
textAllowOverlap,
placedSymbol,
placedSymbolIndex,
bucket.lineVertexArray,
bucket.glyphOffsetArray,
fontSize,
posMatrix,
textLabelPlaneMatrix,
labelToScreenMatrix,
showCollisionBoxes,
pitchWithMap,
collisionGroup.predicate,
circlePixelDiameter,
textPixelPadding,
this.retainedQueryData[bucket.bucketInstanceId].tileID
);
index$1.assert(!placedGlyphCircles.circles.length || (!placedGlyphCircles.collisionDetected || showCollisionBoxes));
placeText = textAllowOverlap || placedGlyphCircles.circles.length > 0 && !placedGlyphCircles.collisionDetected;
offscreen = offscreen && placedGlyphCircles.offscreen;
textOccluded = placedGlyphCircles.occluded;
}
if (collisionArrays.iconFeatureIndex) {
iconFeatureIndex = collisionArrays.iconFeatureIndex;
}
if (collisionArrays.iconBox) {
const placeIconFeature = (iconBox) => {
updateBoxData(iconBox);
const shiftPoint = symbolInstance.hasIconTextFit && shift ? offsetShift(shift.x, shift.y, textRotateWithMap, pitchWithMap, this.transform.angle) : new index$1.Point(0, 0);
const iconScale = bucket.getSymbolInstanceIconSize(partiallyEvaluatedIconSize, this.transform.zoom, symbolInstance.placedIconSymbolIndex);
return this.collisionIndex.placeCollisionBox(
bucket,
iconScale,
iconBox,
mercatorCenter,
invMatrix,
iconProjectedPosOnLabelSpace,
shiftPoint,
iconAllowOverlap,
textPixelRatio,
posMatrix,
collisionGroup.predicate
);
};
if (placedVerticalText && placedVerticalText.box && placedVerticalText.box.length && collisionArrays.verticalIconBox) {
placedIconBoxes = placeIconFeature(collisionArrays.verticalIconBox);
placeIcon = placedIconBoxes.box.length > 0;
} else {
placedIconBoxes = placeIconFeature(collisionArrays.iconBox);
placeIcon = placedIconBoxes.box.length > 0;
}
offscreen = offscreen && placedIconBoxes.offscreen;
iconOccluded = placedIconBoxes.occluded;
}
const iconWithoutText = textOptional || symbolInstance.numHorizontalGlyphVertices === 0 && numVerticalGlyphVertices === 0;
const textWithoutIcon = iconOptional || symbolInstance.numIconVertices === 0;
if (!iconWithoutText && !textWithoutIcon) {
placeIcon = placeText = placeIcon && placeText;
} else if (!textWithoutIcon) {
placeText = placeIcon && placeText;
} else if (!iconWithoutText) {
placeIcon = placeIcon && placeText;
}
if (placeText && placedGlyphBoxes && placedGlyphBoxes.box) {
if (placedVerticalText && placedVerticalText.box && verticalTextFeatureIndex) {
this.collisionIndex.insertCollisionBox(
placedGlyphBoxes.box,
layout.get("text-ignore-placement"),
bucket.bucketInstanceId,
verticalTextFeatureIndex,
collisionGroup.ID
);
} else {
this.collisionIndex.insertCollisionBox(
placedGlyphBoxes.box,
layout.get("text-ignore-placement"),
bucket.bucketInstanceId,
textFeatureIndex,
collisionGroup.ID
);
}
}
if (placeIcon && placedIconBoxes) {
this.collisionIndex.insertCollisionBox(
placedIconBoxes.box,
layout.get("icon-ignore-placement"),
bucket.bucketInstanceId,
iconFeatureIndex,
collisionGroup.ID
);
}
if (placedGlyphCircles) {
if (placeText) {
this.collisionIndex.insertCollisionCircles(
placedGlyphCircles.circles,
layout.get("text-ignore-placement"),
bucket.bucketInstanceId,
textFeatureIndex,
collisionGroup.ID
);
}
if (showCollisionBoxes) {
const id = bucket.bucketInstanceId;
let circleArray = this.collisionCircleArrays[id];
if (circleArray === void 0)
circleArray = this.collisionCircleArrays[id] = new CollisionCircleArray();
for (let i = 0; i < placedGlyphCircles.circles.length; i += 4) {
circleArray.circles.push(placedGlyphCircles.circles[i + 0]);
circleArray.circles.push(placedGlyphCircles.circles[i + 1]);
circleArray.circles.push(placedGlyphCircles.circles[i + 2]);
circleArray.circles.push(placedGlyphCircles.collisionDetected ? 1 : 0);
}
}
}
index$1.assert(crossTileID !== 0);
index$1.assert(bucket.bucketInstanceId !== 0);
const notGlobe = bucket.projection.name !== "globe";
alwaysShowText = alwaysShowText && (notGlobe || !textOccluded);
alwaysShowIcon = alwaysShowIcon && (notGlobe || !iconOccluded);
this.placements[crossTileID] = new JointPlacement(placeText || alwaysShowText, placeIcon || alwaysShowIcon, offscreen || bucket.justReloaded);
seenCrossTileIDs.add(crossTileID);
};
const tileID = this.retainedQueryData[bucket.bucketInstanceId].tileID;
if (bucket.elevationType === "offset" && this.buildingIndex) {
this.buildingIndex.updateZOffset(bucket, tileID);
}
if (bucket.elevationType === "road") {
bucket.updateRoadElevation(tileID.canonical);
}
bucket.updateZOffset();
if (bucket.sortFeaturesByY) {
index$1.assert(bucketPart.symbolInstanceStart === 0);
const symbolIndexes = bucket.getSortedSymbolIndexes(this.transform.angle);
for (let i = symbolIndexes.length - 1; i >= 0; --i) {
const symbolIndex = symbolIndexes[i];
placeSymbol(bucket.symbolInstances.get(symbolIndex), symbolIndex, bucket.collisionArrays[symbolIndex]);
}
if (bucket.hasAnyZOffset) index$1.warnOnce(`${bucket.layerIds[0]} layer symbol-z-elevate: symbols are not sorted by elevation if symbol-z-order is evaluated to viewport-y`);
} else if (bucket.hasAnyZOffset) {
const indexes = bucket.getSortedIndexesByZOffset();
for (let i = 0; i < indexes.length; ++i) {
const symbolIndex = indexes[i];
placeSymbol(bucket.symbolInstances.get(symbolIndex), symbolIndex, bucket.collisionArrays[symbolIndex]);
}
} else {
for (let i = bucketPart.symbolInstanceStart; i < bucketPart.symbolInstanceEnd; i++) {
placeSymbol(bucket.symbolInstances.get(i), i, bucket.collisionArrays[i]);
}
}
if (showCollisionBoxes && bucket.bucketInstanceId in this.collisionCircleArrays) {
const circleArray = this.collisionCircleArrays[bucket.bucketInstanceId];
index$1.invert(circleArray.invProjMatrix, posMatrix);
circleArray.viewportMatrix = this.collisionIndex.getViewportMatrix();
}
bucket.justReloaded = false;
}
markUsedJustification(bucket, placedAnchor, symbolInstance, orientation) {
const {
leftJustifiedTextSymbolIndex: left,
centerJustifiedTextSymbolIndex: center,
rightJustifiedTextSymbolIndex: right,
verticalPlacedTextSymbolIndex: vertical,
crossTileID
} = symbolInstance;
const justification = index$1.getAnchorJustification(placedAnchor);
const autoIndex = orientation === index$1.WritingMode.vertical ? vertical : justification === "left" ? left : justification === "center" ? center : justification === "right" ? right : -1;
if (left >= 0) bucket.text.placedSymbolArray.get(left).crossTileID = autoIndex >= 0 && left !== autoIndex ? 0 : crossTileID;
if (center >= 0) bucket.text.placedSymbolArray.get(center).crossTileID = autoIndex >= 0 && center !== autoIndex ? 0 : crossTileID;
if (right >= 0) bucket.text.placedSymbolArray.get(right).crossTileID = autoIndex >= 0 && right !== autoIndex ? 0 : crossTileID;
if (vertical >= 0) bucket.text.placedSymbolArray.get(vertical).crossTileID = autoIndex >= 0 && vertical !== autoIndex ? 0 : crossTileID;
}
markUsedOrientation(bucket, orientation, symbolInstance) {
const horizontalOrientation = orientation === index$1.WritingMode.horizontal || orientation === index$1.WritingMode.horizontalOnly ? orientation : 0;
const verticalOrientation = orientation === index$1.WritingMode.vertical ? orientation : 0;
const {
leftJustifiedTextSymbolIndex: left,
centerJustifiedTextSymbolIndex: center,
rightJustifiedTextSymbolIndex: right,
verticalPlacedTextSymbolIndex: vertical
} = symbolInstance;
const array = bucket.text.placedSymbolArray;
if (left >= 0) array.get(left).placedOrientation = horizontalOrientation;
if (center >= 0) array.get(center).placedOrientation = horizontalOrientation;
if (right >= 0) array.get(right).placedOrientation = horizontalOrientation;
if (vertical >= 0) array.get(vertical).placedOrientation = verticalOrientation;
}
commit(now) {
this.commitTime = now;
this.zoomAtLastRecencyCheck = this.transform.zoom;
const prevPlacement = this.prevPlacement;
let placementChanged = false;
this.prevZoomAdjustment = prevPlacement ? prevPlacement.zoomAdjustment(this.transform.zoom) : 0;
const increment = prevPlacement ? prevPlacement.symbolFadeChange(now) : 1;
const prevOpacities = prevPlacement ? prevPlacement.opacities : {};
const prevOffsets = prevPlacement ? prevPlacement.variableOffsets : {};
const prevOrientations = prevPlacement ? prevPlacement.placedOrientations : {};
for (const crossTileID in this.placements) {
const jointPlacement = this.placements[crossTileID];
const prevOpacity = prevOpacities[crossTileID];
if (prevOpacity) {
this.opacities[crossTileID] = new JointOpacityState(prevOpacity, increment, jointPlacement.text, jointPlacement.icon, null, jointPlacement.clipped);
placementChanged = placementChanged || jointPlacement.text !== prevOpacity.text.placed || jointPlacement.icon !== prevOpacity.icon.placed;
} else {
this.opacities[crossTileID] = new JointOpacityState(null, increment, jointPlacement.text, jointPlacement.icon, jointPlacement.skipFade, jointPlacement.clipped);
placementChanged = placementChanged || jointPlacement.text || jointPlacement.icon;
}
}
for (const crossTileID in prevOpacities) {
const prevOpacity = prevOpacities[crossTileID];
if (!this.opacities[crossTileID]) {
const jointOpacity = new JointOpacityState(prevOpacity, increment, false, false);
if (!jointOpacity.isHidden()) {
this.opacities[crossTileID] = jointOpacity;
placementChanged = placementChanged || prevOpacity.text.placed || prevOpacity.icon.placed;
}
}
}
for (const crossTileID in prevOffsets) {
if (!this.variableOffsets[crossTileID] && this.opacities[crossTileID] && !this.opacities[crossTileID].isHidden()) {
this.variableOffsets[crossTileID] = prevOffsets[crossTileID];
}
}
for (const crossTileID in prevOrientations) {
if (!this.placedOrientations[crossTileID] && this.opacities[crossTileID] && !this.opacities[crossTileID].isHidden()) {
this.placedOrientations[crossTileID] = prevOrientations[crossTileID];
}
}
index$1.assert(!prevPlacement || prevPlacement.lastPlacementChangeTime !== void 0);
if (placementChanged) {
this.lastPlacementChangeTime = now;
} else if (typeof this.lastPlacementChangeTime !== "number") {
this.lastPlacementChangeTime = prevPlacement ? prevPlacement.lastPlacementChangeTime : now;
}
}
updateLayerOpacities(styleLayer, tiles, layerIndex, replacementSource) {
if (replacementSource) {
this.lastReplacementSourceUpdateTime = replacementSource.updateTime;
}
const seenCrossTileIDs = /* @__PURE__ */ new Set();
for (const tile of tiles) {
const symbolBucket = tile.getBucket(styleLayer);
if (symbolBucket && tile.latestFeatureIndex && styleLayer.fqid === symbolBucket.layerIds[0]) {
this.updateBucketOpacities(symbolBucket, seenCrossTileIDs, tile, tile.collisionBoxArray, layerIndex, replacementSource, tile.tileID, styleLayer.scope);
if (symbolBucket.elevationType === "offset" && this.buildingIndex) {
this.buildingIndex.updateZOffset(symbolBucket, tile.tileID);
}
if (symbolBucket.elevationType === "road") {
symbolBucket.updateRoadElevation(tile.tileID.canonical);
}
symbolBucket.updateZOffset();
}
}
}
updateBucketOpacities(bucket, seenCrossTileIDs, tile, collisionBoxArray, layerIndex, replacementSource, coord, scope) {
if (bucket.hasTextData()) bucket.text.opacityVertexArray.clear();
if (bucket.hasIconData()) bucket.icon.opacityVertexArray.clear();
if (bucket.hasIconCollisionBoxData()) bucket.iconCollisionBox.collisionVertexArray.clear();
if (bucket.hasTextCollisionBoxData()) bucket.textCollisionBox.collisionVertexArray.clear();
const layout = bucket.layers[0].layout;
const paint = bucket.layers[0].paint;
const hasClipping = !!bucket.layers[0].dynamicFilter();
const duplicateOpacityState = new JointOpacityState(null, 0, false, false, true);
const textAllowOverlap = layout.get("text-allow-overlap");
const iconAllowOverlap = layout.get("icon-allow-overlap");
const variablePlacement = layout.get("text-variable-anchor");
const rotateWithMap = layout.get("text-rotation-alignment") === "map";
const pitchWithMap = layout.get("text-pitch-alignment") === "map";
const symbolZOffset = paint.get("symbol-z-offset");
const elevationFromSea = layout.get("symbol-elevation-reference") === "sea";
const needsFeatureForElevation = !symbolZOffset.isConstant();
const defaultOpacityState = new JointOpacityState(
null,
0,
textAllowOverlap && (iconAllowOverlap || !bucket.hasIconData() || layout.get("icon-optional")),
iconAllowOverlap && (textAllowOverlap || !bucket.hasTextData() || layout.get("text-optional")),
true
);
if (!bucket.collisionArrays && collisionBoxArray && (bucket.hasIconCollisionBoxData() || bucket.hasTextCollisionBoxData())) {
bucket.deserializeCollisionBoxes(collisionBoxArray);
}
const addOpacities = (iconOrText, numVertices, opacity) => {
for (let i = 0; i < numVertices / 4; i++) {
iconOrText.opacityVertexArray.emplaceBack(opacity);
}
};
let visibleInstanceCount = 0;
if (replacementSource) {
bucket.updateReplacement(coord, replacementSource);
}
for (let s = 0; s < bucket.symbolInstances.length; s++) {
const symbolInstance = bucket.symbolInstances.get(s);
const {
numHorizontalGlyphVertices,
numVerticalGlyphVertices,
crossTileID,
numIconVertices,
tileAnchorX,
tileAnchorY
} = symbolInstance;
let feature = null;
const retainedQueryData = this.retainedQueryData[bucket.bucketInstanceId];
if (needsFeatureForElevation && symbolInstance && retainedQueryData) {
const featureIndex = tile.latestFeatureIndex;
feature = featureIndex.loadFeature({
featureIndex: symbolInstance.featureIndex,
bucketIndex: retainedQueryData.bucketIndex,
sourceLayerIndex: retainedQueryData.sourceLayerIndex,
layoutVertexArrayOffset: 0
});
}
const symbolZOffsetValue = symbolZOffset.evaluate(feature, {});
const isDuplicate = seenCrossTileIDs.has(crossTileID);
let opacityState = this.opacities[crossTileID];
if (isDuplicate) {
opacityState = duplicateOpacityState;
} else if (!opacityState) {
opacityState = defaultOpacityState;
this.opacities[crossTileID] = opacityState;
}
seenCrossTileIDs.add(crossTileID);
const hasText = numHorizontalGlyphVertices > 0 || numVerticalGlyphVertices > 0;
const hasIcon = numIconVertices > 0;
const placedOrientation = this.placedOrientations[crossTileID];
const horizontalHidden = placedOrientation === index$1.WritingMode.vertical;
const verticalHidden = placedOrientation === index$1.WritingMode.horizontal || placedOrientation === index$1.WritingMode.horizontalOnly;
if ((hasText || hasIcon) && !opacityState.isHidden()) visibleInstanceCount++;
let clippedSymbol = false;
if ((hasText || hasIcon) && replacementSource) {
for (const region of bucket.activeReplacements) {
if (index$1.skipClipping(region, layerIndex, index$1.LayerTypeMask.Symbol, scope)) continue;
if (region.min.x > tileAnchorX || tileAnchorX > region.max.x || region.min.y > tileAnchorY || tileAnchorY > region.max.y) {
continue;
}
const p = index$1.transformPointToTile(tileAnchorX, tileAnchorY, coord.canonical, region.footprintTileId.canonical);
clippedSymbol = index$1.pointInFootprint(p, region.footprint);
if (clippedSymbol) break;
}
}
if (hasText) {
const packedOpacity = clippedSymbol ? PACKED_HIDDEN_OPACITY : packOpacity(opacityState.text);
const horizontalOpacity = horizontalHidden ? PACKED_HIDDEN_OPACITY : packedOpacity;
addOpacities(bucket.text, numHorizontalGlyphVertices, horizontalOpacity);
const verticalOpacity = verticalHidden ? PACKED_HIDDEN_OPACITY : packedOpacity;
addOpacities(bucket.text, numVerticalGlyphVertices, verticalOpacity);
const symbolHidden = opacityState.text.isHidden();
const {
leftJustifiedTextSymbolIndex: left,
centerJustifiedTextSymbolIndex: center,
rightJustifiedTextSymbolIndex: right,
verticalPlacedTextSymbolIndex: vertical
} = symbolInstance;
const array = bucket.text.placedSymbolArray;
const horizontalHiddenValue = symbolHidden || horizontalHidden ? 1 : 0;
if (left >= 0) array.get(left).hidden = horizontalHiddenValue;
if (center >= 0) array.get(center).hidden = horizontalHiddenValue;
if (right >= 0) array.get(right).hidden = horizontalHiddenValue;
if (vertical >= 0) array.get(vertical).hidden = symbolHidden || verticalHidden ? 1 : 0;
const prevOffset = this.variableOffsets[crossTileID];
if (prevOffset) {
this.markUsedJustification(bucket, prevOffset.anchor, symbolInstance, placedOrientation);
}
const prevOrientation = this.placedOrientations[crossTileID];
if (prevOrientation) {
this.markUsedJustification(bucket, "left", symbolInstance, prevOrientation);
this.markUsedOrientation(bucket, prevOrientation, symbolInstance);
}
}
if (hasIcon) {
const packedOpacity = clippedSymbol ? PACKED_HIDDEN_OPACITY : packOpacity(opacityState.icon);
const { placedIconSymbolIndex, verticalPlacedIconSymbolIndex } = symbolInstance;
const array = bucket.icon.placedSymbolArray;
const iconHidden = opacityState.icon.isHidden() ? 1 : 0;
if (placedIconSymbolIndex >= 0) {
const horizontalOpacity = !horizontalHidden ? packedOpacity : PACKED_HIDDEN_OPACITY;
addOpacities(bucket.icon, numIconVertices, horizontalOpacity);
array.get(placedIconSymbolIndex).hidden = iconHidden;
}
if (verticalPlacedIconSymbolIndex >= 0) {
const verticalOpacity = !verticalHidden ? packedOpacity : PACKED_HIDDEN_OPACITY;
addOpacities(bucket.icon, symbolInstance.numVerticalIconVertices, verticalOpacity);
array.get(verticalPlacedIconSymbolIndex).hidden = iconHidden;
}
}
if (bucket.hasIconCollisionBoxData() || bucket.hasTextCollisionBoxData()) {
const collisionArrays = bucket.collisionArrays[s];
if (collisionArrays) {
let shift = new index$1.Point(0, 0);
let used = true;
if (collisionArrays.textBox || collisionArrays.verticalTextBox) {
if (variablePlacement) {
const variableOffset = this.variableOffsets[crossTileID];
if (variableOffset) {
shift = calculateVariableLayoutShift(
variableOffset.anchor,
variableOffset.width,
variableOffset.height,
variableOffset.textOffset,
variableOffset.textScale
);
if (rotateWithMap) {
shift._rotate(pitchWithMap ? this.transform.angle : -this.transform.angle);
}
} else {
used = false;
}
}
if (hasClipping) {
used = !opacityState.clipped;
}
if (collisionArrays.textBox) {
updateCollisionVertices(bucket.textCollisionBox.collisionVertexArray, opacityState.text.placed, !used || horizontalHidden, symbolZOffsetValue, elevationFromSea, shift.x, shift.y);
}
if (collisionArrays.verticalTextBox) {
updateCollisionVertices(bucket.textCollisionBox.collisionVertexArray, opacityState.text.placed, !used || verticalHidden, symbolZOffsetValue, elevationFromSea, shift.x, shift.y);
}
}
const verticalIconUsed = used && Boolean(!verticalHidden && collisionArrays.verticalIconBox);
if (collisionArrays.iconBox) {
updateCollisionVertices(
bucket.iconCollisionBox.collisionVertexArray,
opacityState.icon.placed,
verticalIconUsed,
symbolZOffsetValue,
elevationFromSea,
symbolInstance.hasIconTextFit ? shift.x : 0,
symbolInstance.hasIconTextFit ? shift.y : 0
);
}
if (collisionArrays.verticalIconBox) {
updateCollisionVertices(
bucket.iconCollisionBox.collisionVertexArray,
opacityState.icon.placed,
!verticalIconUsed,
symbolZOffsetValue,
elevationFromSea,
symbolInstance.hasIconTextFit ? shift.x : 0,
symbolInstance.hasIconTextFit ? shift.y : 0
);
}
}
}
}
bucket.fullyClipped = visibleInstanceCount === 0;
bucket.sortFeatures(this.transform.angle);
if (this.retainedQueryData[bucket.bucketInstanceId]) {
this.retainedQueryData[bucket.bucketInstanceId].featureSortOrder = bucket.featureSortOrder;
}
if (bucket.hasTextData() && bucket.text.opacityVertexBuffer) {
bucket.text.opacityVertexBuffer.updateData(bucket.text.opacityVertexArray);
}
if (bucket.hasIconData() && bucket.icon.opacityVertexBuffer) {
bucket.icon.opacityVertexBuffer.updateData(bucket.icon.opacityVertexArray);
}
if (bucket.hasIconCollisionBoxData() && bucket.iconCollisionBox.collisionVertexBuffer) {
bucket.iconCollisionBox.collisionVertexBuffer.updateData(bucket.iconCollisionBox.collisionVertexArray);
}
if (bucket.hasTextCollisionBoxData() && bucket.textCollisionBox.collisionVertexBuffer) {
bucket.textCollisionBox.collisionVertexBuffer.updateData(bucket.textCollisionBox.collisionVertexArray);
}
index$1.assert(bucket.text.opacityVertexArray.length === bucket.text.layoutVertexArray.length / 4);
index$1.assert(bucket.icon.opacityVertexArray.length === bucket.icon.layoutVertexArray.length / 4);
if (bucket.bucketInstanceId in this.collisionCircleArrays) {
const instance = this.collisionCircleArrays[bucket.bucketInstanceId];
bucket.placementInvProjMatrix = instance.invProjMatrix;
bucket.placementViewportMatrix = instance.viewportMatrix;
bucket.collisionCircleArray = instance.circles;
delete this.collisionCircleArrays[bucket.bucketInstanceId];
}
}
symbolFadeChange(now) {
return this.fadeDuration === 0 ? 1 : (now - this.commitTime) / this.fadeDuration + this.prevZoomAdjustment;
}
zoomAdjustment(zoom) {
return Math.max(0, (this.transform.zoom - zoom) / 1.5);
}
hasTransitions(now) {
return this.stale || now - this.lastPlacementChangeTime < this.fadeDuration;
}
stillRecent(now, zoom) {
const durationAdjustment = this.zoomAtLastRecencyCheck === zoom ? 1 - this.zoomAdjustment(zoom) : 1;
this.zoomAtLastRecencyCheck = zoom;
return this.commitTime + this.fadeDuration * durationAdjustment > now;
}
isStale() {
return this.stale;
}
setStale() {
this.stale = true;
}
}
function updateCollisionVertices(collisionVertexArray, placed, notUsed, elevation, elevationFromSea, shiftX, shiftY) {
collisionVertexArray.emplaceBack(placed ? 1 : 0, notUsed ? 1 : 0, shiftX || 0, shiftY || 0, elevation, elevationFromSea ? 1 : 0);
collisionVertexArray.emplaceBack(placed ? 1 : 0, notUsed ? 1 : 0, shiftX || 0, shiftY || 0, elevation, elevationFromSea ? 1 : 0);
collisionVertexArray.emplaceBack(placed ? 1 : 0, notUsed ? 1 : 0, shiftX || 0, shiftY || 0, elevation, elevationFromSea ? 1 : 0);
collisionVertexArray.emplaceBack(placed ? 1 : 0, notUsed ? 1 : 0, shiftX || 0, shiftY || 0, elevation, elevationFromSea ? 1 : 0);
}
const shift25 = Math.pow(2, 25);
const shift24 = Math.pow(2, 24);
const shift17 = Math.pow(2, 17);
const shift16 = Math.pow(2, 16);
const shift9 = Math.pow(2, 9);
const shift8 = Math.pow(2, 8);
const shift1 = Math.pow(2, 1);
function packOpacity(opacityState) {
if (opacityState.opacity === 0 && !opacityState.placed) {
return 0;
} else if (opacityState.opacity === 1 && opacityState.placed) {
return 4294967295;
}
const targetBit = opacityState.placed ? 1 : 0;
const opacityBits = Math.floor(opacityState.opacity * 127);
return opacityBits * shift25 + targetBit * shift24 + opacityBits * shift17 + targetBit * shift16 + opacityBits * shift9 + targetBit * shift8 + opacityBits * shift1 + targetBit;
}
const PACKED_HIDDEN_OPACITY = 0;
class LayerPlacement {
constructor(styleLayer) {
this._sortAcrossTiles = styleLayer.layout.get("symbol-z-order") !== "viewport-y" && styleLayer.layout.get("symbol-sort-key").constantOr(1) !== void 0;
this._currentTileIndex = 0;
this._currentPartIndex = 0;
this._seenCrossTileIDs = /* @__PURE__ */ new Set();
this._bucketParts = [];
}
continuePlacement(tiles, placement, showCollisionBoxes, styleLayer, shouldPausePlacement, scaleFactor) {
const bucketParts = this._bucketParts;
while (this._currentTileIndex < tiles.length) {
const tile = tiles[this._currentTileIndex];
placement.getBucketParts(bucketParts, styleLayer, tile, this._sortAcrossTiles, scaleFactor);
this._currentTileIndex++;
if (shouldPausePlacement()) {
return true;
}
}
if (this._sortAcrossTiles) {
this._sortAcrossTiles = false;
bucketParts.sort((a, b) => a.sortKey - b.sortKey);
}
while (this._currentPartIndex < bucketParts.length) {
const bucketPart = bucketParts[this._currentPartIndex];
placement.placeLayerBucketPart(bucketPart, this._seenCrossTileIDs, showCollisionBoxes, bucketPart.symbolInstanceStart === 0, scaleFactor);
this._currentPartIndex++;
if (shouldPausePlacement()) {
return true;
}
}
return false;
}
}
class PauseablePlacement {
constructor(transform, order, forceFullPlacement, showCollisionBoxes, fadeDuration, crossSourceCollisions, prevPlacement, fogState, buildingIndex) {
this.placement = new Placement(transform, fadeDuration, crossSourceCollisions, prevPlacement, fogState, buildingIndex);
this._currentPlacementIndex = order.length - 1;
this._forceFullPlacement = forceFullPlacement;
this._showCollisionBoxes = showCollisionBoxes;
this._done = false;
}
isDone() {
return this._done;
}
continuePlacement(order, layers, layerTiles, layerTilesInYOrder, scaleFactor) {
const startTime = index$1.exported$1.now();
const shouldPausePlacement = () => {
const elapsedTime = index$1.exported$1.now() - startTime;
return this._forceFullPlacement ? false : elapsedTime > 2;
};
while (this._currentPlacementIndex >= 0) {
const layerId = order[this._currentPlacementIndex];
const layer = layers[layerId];
const placementZoom = this.placement.collisionIndex.transform.zoom;
if (layer.type === "symbol" && layer.visibility !== "none" && (!layer.minzoom || layer.minzoom <= placementZoom) && (!layer.maxzoom || layer.maxzoom > placementZoom)) {
const symbolLayer = layer;
const zOffset = symbolLayer.layout.get("symbol-z-elevate");
const hasSymbolSortKey = symbolLayer.layout.get("symbol-sort-key").constantOr(1) !== void 0;
const symbolZOrder = symbolLayer.layout.get("symbol-z-order");
const sortSymbolByKey = symbolZOrder !== "viewport-y" && hasSymbolSortKey;
const zOrderByViewportY = symbolZOrder === "viewport-y" || symbolZOrder === "auto" && !sortSymbolByKey;
const canOverlap = symbolLayer.layout.get("text-allow-overlap") || symbolLayer.layout.get("icon-allow-overlap") || symbolLayer.layout.get("text-ignore-placement") || symbolLayer.layout.get("icon-ignore-placement");
const sortSymbolByViewportY = zOrderByViewportY && canOverlap;
const inProgressLayer = this._inProgressLayer = this._inProgressLayer || new LayerPlacement(symbolLayer);
const sourceId = index$1.makeFQID(layer.source, layer.scope);
const sortTileByY = zOffset || sortSymbolByViewportY;
const pausePlacement = inProgressLayer.continuePlacement(sortTileByY ? layerTilesInYOrder[sourceId] : layerTiles[sourceId], this.placement, this._showCollisionBoxes, layer, shouldPausePlacement, scaleFactor);
if (pausePlacement) {
index$1.PerformanceUtils.recordPlacementTime(index$1.exported$1.now() - startTime);
return;
}
delete this._inProgressLayer;
}
this._currentPlacementIndex--;
}
index$1.PerformanceUtils.recordPlacementTime(index$1.exported$1.now() - startTime);
this._done = true;
}
commit(now) {
this.placement.commit(now);
return this.placement;
}
}
const roundingFactor = 512 / index$1.EXTENT / 2;
class TileLayerIndex {
constructor(tileID, symbolInstances, bucketInstanceId) {
this.tileID = tileID;
this.bucketInstanceId = bucketInstanceId;
this.index = new index$1.KDBush(symbolInstances.length, 16, Int32Array);
this.keys = [];
this.crossTileIDs = [];
const tx = tileID.canonical.x * index$1.EXTENT;
const ty = tileID.canonical.y * index$1.EXTENT;
for (let i = 0; i < symbolInstances.length; i++) {
const { key, crossTileID, tileAnchorX, tileAnchorY } = symbolInstances.get(i);
const x = Math.floor((tx + tileAnchorX) * roundingFactor);
const y = Math.floor((ty + tileAnchorY) * roundingFactor);
this.index.add(x, y);
this.keys.push(key);
this.crossTileIDs.push(crossTileID);
}
this.index.finish();
}
findMatches(symbolInstances, newTileID, zoomCrossTileIDs) {
const tolerance = this.tileID.canonical.z < newTileID.canonical.z ? 1 : Math.pow(2, this.tileID.canonical.z - newTileID.canonical.z);
const scale = roundingFactor / Math.pow(2, newTileID.canonical.z - this.tileID.canonical.z);
const tx = newTileID.canonical.x * index$1.EXTENT;
const ty = newTileID.canonical.y * index$1.EXTENT;
for (let i = 0; i < symbolInstances.length; i++) {
const symbolInstance = symbolInstances.get(i);
if (symbolInstance.crossTileID) {
continue;
}
const { key, tileAnchorX, tileAnchorY } = symbolInstance;
const x = Math.floor((tx + tileAnchorX) * scale);
const y = Math.floor((ty + tileAnchorY) * scale);
const matchedIds = this.index.range(x - tolerance, y - tolerance, x + tolerance, y + tolerance).sort((a, b) => a - b);
for (const id of matchedIds) {
const crossTileID = this.crossTileIDs[id];
if (this.keys[id] === key && !zoomCrossTileIDs.has(crossTileID)) {
zoomCrossTileIDs.add(crossTileID);
symbolInstance.crossTileID = crossTileID;
break;
}
}
}
}
}
class CrossTileIDs {
constructor() {
this.maxCrossTileID = 0;
}
generate() {
return ++this.maxCrossTileID;
}
}
class CrossTileSymbolLayerIndex {
constructor() {
this.indexes = {};
this.usedCrossTileIDs = {};
this.lng = 0;
}
/*
* Sometimes when a user pans across the antimeridian the longitude value gets wrapped.
* To prevent labels from flashing out and in we adjust the tileID values in the indexes
* so that they match the new wrapped version of the map.
*/
handleWrapJump(lng) {
const wrapDelta = Math.round((lng - this.lng) / 360);
if (wrapDelta !== 0) {
for (const zoom in this.indexes) {
const zoomIndexes = this.indexes[zoom];
const newZoomIndex = {};
for (const key in zoomIndexes) {
const index = zoomIndexes[key];
index.tileID = index.tileID.unwrapTo(index.tileID.wrap + wrapDelta);
newZoomIndex[index.tileID.key] = index;
}
this.indexes[zoom] = newZoomIndex;
}
}
this.lng = lng;
}
addBucket(tileID, bucket, crossTileIDs) {
if (this.indexes[tileID.overscaledZ] && this.indexes[tileID.overscaledZ][tileID.key]) {
if (this.indexes[tileID.overscaledZ][tileID.key].bucketInstanceId === bucket.bucketInstanceId) {
return false;
} else {
this.removeBucketCrossTileIDs(
tileID.overscaledZ,
this.indexes[tileID.overscaledZ][tileID.key]
);
}
}
for (let i = 0; i < bucket.symbolInstances.length; i++) {
const symbolInstance = bucket.symbolInstances.get(i);
symbolInstance.crossTileID = 0;
}
if (!this.usedCrossTileIDs[tileID.overscaledZ]) {
this.usedCrossTileIDs[tileID.overscaledZ] = /* @__PURE__ */ new Set();
}
const zoomCrossTileIDs = this.usedCrossTileIDs[tileID.overscaledZ];
for (const zoom in this.indexes) {
const zoomIndexes = this.indexes[zoom];
if (Number(zoom) > tileID.overscaledZ) {
for (const id in zoomIndexes) {
const childIndex = zoomIndexes[id];
if (childIndex.tileID.isChildOf(tileID)) {
childIndex.findMatches(bucket.symbolInstances, tileID, zoomCrossTileIDs);
}
}
} else {
const parentCoord = tileID.scaledTo(Number(zoom));
const parentIndex = zoomIndexes[parentCoord.key];
if (parentIndex) {
parentIndex.findMatches(bucket.symbolInstances, tileID, zoomCrossTileIDs);
}
}
}
for (let i = 0; i < bucket.symbolInstances.length; i++) {
const symbolInstance = bucket.symbolInstances.get(i);
if (!symbolInstance.crossTileID) {
symbolInstance.crossTileID = crossTileIDs.generate();
zoomCrossTileIDs.add(symbolInstance.crossTileID);
}
}
if (this.indexes[tileID.overscaledZ] === void 0) {
this.indexes[tileID.overscaledZ] = {};
}
this.indexes[tileID.overscaledZ][tileID.key] = new TileLayerIndex(tileID, bucket.symbolInstances, bucket.bucketInstanceId);
return true;
}
removeBucketCrossTileIDs(zoom, removedBucket) {
for (const crossTileID of removedBucket.crossTileIDs) {
this.usedCrossTileIDs[zoom].delete(crossTileID);
}
}
removeStaleBuckets(currentIDs) {
let tilesChanged = false;
for (const z in this.indexes) {
const zoomIndexes = this.indexes[z];
for (const tileKey in zoomIndexes) {
if (!currentIDs[zoomIndexes[tileKey].bucketInstanceId]) {
this.removeBucketCrossTileIDs(z, zoomIndexes[tileKey]);
delete zoomIndexes[tileKey];
tilesChanged = true;
}
}
}
return tilesChanged;
}
}
class CrossTileSymbolIndex {
constructor() {
this.layerIndexes = {};
this.crossTileIDs = new CrossTileIDs();
this.maxBucketInstanceId = 0;
this.bucketsInCurrentPlacement = {};
}
addLayer(styleLayer, tiles, lng, projection) {
let layerIndex = this.layerIndexes[styleLayer.fqid];
if (layerIndex === void 0) {
layerIndex = this.layerIndexes[styleLayer.fqid] = new CrossTileSymbolLayerIndex();
}
let symbolBucketsChanged = false;
const currentBucketIDs = {};
if (projection.name !== "globe") {
layerIndex.handleWrapJump(lng);
}
for (const tile of tiles) {
const symbolBucket = tile.getBucket(styleLayer);
if (!symbolBucket || styleLayer.fqid !== symbolBucket.layerIds[0])
continue;
if (!symbolBucket.bucketInstanceId) {
symbolBucket.bucketInstanceId = ++this.maxBucketInstanceId;
}
if (layerIndex.addBucket(tile.tileID, symbolBucket, this.crossTileIDs)) {
symbolBucketsChanged = true;
}
currentBucketIDs[symbolBucket.bucketInstanceId] = true;
}
if (layerIndex.removeStaleBuckets(currentBucketIDs)) {
symbolBucketsChanged = true;
}
return symbolBucketsChanged;
}
pruneUnusedLayers(usedLayers) {
const usedLayerMap = {};
usedLayers.forEach((usedLayer) => {
usedLayerMap[usedLayer] = true;
});
for (const layerId in this.layerIndexes) {
if (!usedLayerMap[layerId]) {
delete this.layerIndexes[layerId];
}
}
}
}
const ZERO = 0;
const ONE = 1;
const SRC_ALPHA = 770;
const ONE_MINUS_SRC_ALPHA = 771;
const DST_COLOR = 774;
class ColorMode {
constructor(blendFunction, blendColor, mask, blendEquation) {
this.blendFunction = blendFunction;
this.blendColor = blendColor.toNonPremultipliedRenderColor(null);
this.mask = mask;
this.blendEquation = blendEquation;
}
}
ColorMode.Replace = [ONE, ZERO, ONE, ZERO];
ColorMode.disabled = new ColorMode(ColorMode.Replace, index$1.Color.transparent, [false, false, false, false]);
ColorMode.unblended = new ColorMode(ColorMode.Replace, index$1.Color.transparent, [true, true, true, true]);
ColorMode.alphaBlended = new ColorMode([ONE, ONE_MINUS_SRC_ALPHA, ONE, ONE_MINUS_SRC_ALPHA], index$1.Color.transparent, [true, true, true, true]);
ColorMode.alphaBlendedNonPremultiplied = new ColorMode([SRC_ALPHA, ONE_MINUS_SRC_ALPHA, SRC_ALPHA, ONE_MINUS_SRC_ALPHA], index$1.Color.transparent, [true, true, true, true]);
ColorMode.multiply = new ColorMode([DST_COLOR, ZERO, DST_COLOR, ZERO], index$1.Color.transparent, [true, true, true, true]);
ColorMode.additive = new ColorMode([ONE, ONE, ONE, ONE], index$1.Color.transparent, [true, true, true, true]);
const ALWAYS$1 = 519;
class DepthMode {
constructor(depthFunc, depthMask, depthRange) {
this.func = depthFunc;
this.mask = depthMask;
this.range = depthRange;
}
}
DepthMode.ReadOnly = false;
DepthMode.ReadWrite = true;
DepthMode.disabled = new DepthMode(ALWAYS$1, DepthMode.ReadOnly, [0, 1]);
const ALWAYS = 519;
const KEEP = 7680;
class StencilMode {
constructor(test, ref, mask, fail, depthFail, pass) {
this.test = test;
this.ref = ref;
this.mask = mask;
this.fail = fail;
this.depthFail = depthFail;
this.pass = pass;
}
}
StencilMode.disabled = new StencilMode({ func: ALWAYS, mask: 0 }, 0, 0, KEEP, KEEP, KEEP);
const BACK = 1029;
const FRONT = 1028;
const CCW = 2305;
const CW = 2304;
class CullFaceMode {
constructor(enable, mode, frontFace) {
this.enable = enable;
this.mode = mode;
this.frontFace = frontFace;
}
}
CullFaceMode.disabled = new CullFaceMode(false, BACK, CCW);
CullFaceMode.backCCW = new CullFaceMode(true, BACK, CCW);
CullFaceMode.backCW = new CullFaceMode(true, BACK, CW);
CullFaceMode.frontCW = new CullFaceMode(true, FRONT, CW);
CullFaceMode.frontCCW = new CullFaceMode(true, FRONT, CCW);
function updateTransformOrientation(matrix, orientation) {
const position = index$1.getColumn(matrix, 3);
index$1.fromQuat(matrix, orientation);
index$1.setColumn(matrix, 3, position);
}
function updateTransformPosition(matrix, position) {
index$1.setColumn(matrix, 3, [position[0], position[1], position[2], 1]);
}
function orientationFromPitchBearing(pitch, bearing) {
const orientation = index$1.identity$1([]);
index$1.rotateZ$1(orientation, orientation, -bearing);
index$1.rotateX(orientation, orientation, -pitch);
return orientation;
}
function orientationFromFrame(forward, up) {
const xyForward = [forward[0], forward[1], 0];
const xyUp = [up[0], up[1], 0];
const epsilon = 1e-15;
if (index$1.length(xyForward) >= epsilon) {
const xyDir = index$1.normalize([], xyForward);
index$1.scale$1(xyUp, xyDir, index$1.dot(xyUp, xyDir));
up[0] = xyUp[0];
up[1] = xyUp[1];
}
const right = index$1.cross([], up, forward);
if (index$1.len(right) < epsilon) {
return null;
}
const bearing = Math.atan2(-right[1], right[0]);
const pitch = Math.atan2(Math.sqrt(forward[0] * forward[0] + forward[1] * forward[1]), -forward[2]);
return orientationFromPitchBearing(pitch, bearing);
}
class FreeCameraOptions {
constructor(position, orientation) {
this.position = position;
this.orientation = orientation;
}
get position() {
return this._position;
}
set position(position) {
if (!position) {
this._position = null;
} else {
const mercatorCoordinate = position instanceof index$1.MercatorCoordinate ? position : new index$1.MercatorCoordinate(position[0], position[1], position[2]);
if (this._renderWorldCopies) {
mercatorCoordinate.x = index$1.wrap(mercatorCoordinate.x, 0, 1);
}
this._position = mercatorCoordinate;
}
}
/**
* Helper function for setting orientation of the camera by defining a focus point
* on the map.
*
* @param {LngLatLike} location Location of the focus point on the map.
* @param {vec3?} up Up vector of the camera is necessary in certain scenarios where bearing can't be deduced from the viewing direction.
* @param {number?} altitude Altitude of the focus point on the map. By default ground level is used.
* @example
* const camera = map.getFreeCameraOptions();
*
* const position = [138.72649, 35.33974];
* const altitude = 3000;
*
* camera.position = mapboxgl.MercatorCoordinate.fromLngLat(position, altitude);
* camera.lookAtPoint([138.73036, 35.36197]);
* // Apply camera changes
* map.setFreeCameraOptions(camera);
*/
lookAtPoint(location, up, altitude) {
this.orientation = null;
if (!this.position) {
return;
}
const pos = this.position;
const targetAltitude = altitude ? altitude : this._elevation ? this._elevation.getAtPointOrZero(index$1.MercatorCoordinate.fromLngLat(location)) : 0;
const target = index$1.MercatorCoordinate.fromLngLat(location, targetAltitude);
const forward = [target.x - pos.x, target.y - pos.y, target.z - pos.z];
if (!up)
up = [0, 0, 1];
up[2] = Math.abs(up[2]);
this.orientation = orientationFromFrame(forward, up);
}
/**
* Helper function for setting the orientation of the camera as a pitch and a bearing.
*
* @param {number} pitch Pitch angle in degrees.
* @param {number} bearing Bearing angle in degrees.
* @example
* const camera = map.getFreeCameraOptions();
*
* // Update camera pitch and bearing
* camera.setPitchBearing(80, 90);
* // Apply changes
* map.setFreeCameraOptions(camera);
*/
setPitchBearing(pitch, bearing) {
this.orientation = orientationFromPitchBearing(index$1.degToRad(pitch), index$1.degToRad(-bearing));
}
}
class FreeCamera {
constructor(position, orientation) {
this._transform = index$1.identity([]);
this.orientation = orientation;
this.position = position;
}
get mercatorPosition() {
const pos = this.position;
return new index$1.MercatorCoordinate(pos[0], pos[1], pos[2]);
}
get position() {
const col = index$1.getColumn(this._transform, 3);
return [col[0], col[1], col[2]];
}
set position(value) {
if (value) {
updateTransformPosition(this._transform, value);
}
}
get orientation() {
return this._orientation;
}
set orientation(value) {
this._orientation = value || index$1.identity$1([]);
if (value) {
updateTransformOrientation(this._transform, this._orientation);
}
}
getPitchBearing() {
const f = this.forward();
const r = this.right();
return {
bearing: Math.atan2(-r[1], r[0]),
pitch: Math.atan2(Math.sqrt(f[0] * f[0] + f[1] * f[1]), -f[2])
};
}
setPitchBearing(pitch, bearing) {
this._orientation = orientationFromPitchBearing(pitch, bearing);
updateTransformOrientation(this._transform, this._orientation);
}
forward() {
const col = index$1.getColumn(this._transform, 2);
return [-col[0], -col[1], -col[2]];
}
up() {
const col = index$1.getColumn(this._transform, 1);
return [-col[0], -col[1], -col[2]];
}
right() {
const col = index$1.getColumn(this._transform, 0);
return [col[0], col[1], col[2]];
}
getCameraToWorld(worldSize, pixelsPerMeter) {
const cameraToWorld = new Float64Array(16);
index$1.invert(cameraToWorld, this.getWorldToCamera(worldSize, pixelsPerMeter));
return cameraToWorld;
}
getCameraToWorldMercator() {
return this._transform;
}
getWorldToCameraPosition(worldSize, pixelsPerMeter, uniformScale) {
const invPosition = this.position;
index$1.scale$1(invPosition, invPosition, -worldSize);
const matrix = new Float64Array(16);
index$1.fromScaling(matrix, [uniformScale, uniformScale, uniformScale]);
index$1.translate(matrix, matrix, invPosition);
matrix[10] *= pixelsPerMeter;
return matrix;
}
getWorldToCamera(worldSize, pixelsPerMeter) {
const matrix = new Float64Array(16);
const invOrientation = new Float64Array(4);
const invPosition = this.position;
index$1.conjugate(invOrientation, this._orientation);
index$1.scale$1(invPosition, invPosition, -worldSize);
index$1.fromQuat(matrix, invOrientation);
index$1.translate(matrix, matrix, invPosition);
matrix[1] *= -1;
matrix[5] *= -1;
matrix[9] *= -1;
matrix[13] *= -1;
matrix[8] *= pixelsPerMeter;
matrix[9] *= pixelsPerMeter;
matrix[10] *= pixelsPerMeter;
matrix[11] *= pixelsPerMeter;
return matrix;
}
getCameraToClipPerspective(fovy, aspectRatio, nearZ, farZ) {
const matrix = new Float64Array(16);
index$1.perspective(matrix, fovy, aspectRatio, nearZ, farZ);
return matrix;
}
getCameraToClipOrthographic(left, right, bottom, top, nearZ, farZ) {
const matrix = new Float64Array(16);
index$1.ortho(matrix, left, right, bottom, top, nearZ, farZ);
return matrix;
}
// The additional parameter needs to be removed. This was introduced because originally
// the value returned by this function was incorrect. Fixing it would break the fog visuals and needs to be
// communicated carefully first. Also see transform.cameraWorldSizeForFog.
getDistanceToElevation(elevationMeters, convert = false) {
const z0 = elevationMeters === 0 ? 0 : index$1.mercatorZfromAltitude(elevationMeters, convert ? index$1.latFromMercatorY(this.position[1]) : this.position[1]);
const f = this.forward();
return (z0 - this.position[2]) / f[2];
}
clone() {
return new FreeCamera([...this.position], [...this.orientation]);
}
}
/*! Tweakpane 4.0.5 (c) 2016 cocopon, licensed under the MIT license. */
function forceCast(v) {
return v;
}
function isEmpty(value) {
return value === null || value === undefined;
}
function isObject$1(value) {
return value !== null && typeof value === 'object';
}
function isRecord(value) {
return value !== null && typeof value === 'object';
}
function deepEqualsArray(a1, a2) {
if (a1.length !== a2.length) {
return false;
}
for (let i = 0; i < a1.length; i++) {
if (a1[i] !== a2[i]) {
return false;
}
}
return true;
}
function deepMerge(r1, r2) {
const keys = Array.from(new Set([...Object.keys(r1), ...Object.keys(r2)]));
return keys.reduce((result, key) => {
const v1 = r1[key];
const v2 = r2[key];
return isRecord(v1) && isRecord(v2)
? Object.assign(Object.assign({}, result), { [key]: deepMerge(v1, v2) }) : Object.assign(Object.assign({}, result), { [key]: key in r2 ? v2 : v1 });
}, {});
}
function isBinding(value) {
if (!isObject$1(value)) {
return false;
}
return 'target' in value;
}
const CREATE_MESSAGE_MAP = {
alreadydisposed: () => 'View has been already disposed',
invalidparams: (context) => `Invalid parameters for '${context.name}'`,
nomatchingcontroller: (context) => `No matching controller for '${context.key}'`,
nomatchingview: (context) => `No matching view for '${JSON.stringify(context.params)}'`,
notbindable: () => `Value is not bindable`,
notcompatible: (context) => `Not compatible with plugin '${context.id}'`,
propertynotfound: (context) => `Property '${context.name}' not found`,
shouldneverhappen: () => 'This error should never happen',
};
class TpError {
static alreadyDisposed() {
return new TpError({ type: 'alreadydisposed' });
}
static notBindable() {
return new TpError({
type: 'notbindable',
});
}
static notCompatible(bundleId, id) {
return new TpError({
type: 'notcompatible',
context: {
id: `${bundleId}.${id}`,
},
});
}
static propertyNotFound(name) {
return new TpError({
type: 'propertynotfound',
context: {
name: name,
},
});
}
static shouldNeverHappen() {
return new TpError({ type: 'shouldneverhappen' });
}
constructor(config) {
var _a;
this.message =
(_a = CREATE_MESSAGE_MAP[config.type](forceCast(config.context))) !== null && _a !== void 0 ? _a : 'Unexpected error';
this.name = this.constructor.name;
this.stack = new Error(this.message).stack;
this.type = config.type;
}
toString() {
return this.message;
}
}
class BindingTarget {
constructor(obj, key) {
this.obj_ = obj;
this.key = key;
}
static isBindable(obj) {
if (obj === null) {
return false;
}
if (typeof obj !== 'object' && typeof obj !== 'function') {
return false;
}
return true;
}
read() {
return this.obj_[this.key];
}
write(value) {
this.obj_[this.key] = value;
}
writeProperty(name, value) {
const valueObj = this.read();
if (!BindingTarget.isBindable(valueObj)) {
throw TpError.notBindable();
}
if (!(name in valueObj)) {
throw TpError.propertyNotFound(name);
}
valueObj[name] = value;
}
}
class Emitter {
constructor() {
this.observers_ = {};
}
on(eventName, handler, opt_options) {
var _a;
let observers = this.observers_[eventName];
if (!observers) {
observers = this.observers_[eventName] = [];
}
observers.push({
handler: handler,
key: (_a = opt_options === null || opt_options === void 0 ? void 0 : opt_options.key) !== null && _a !== void 0 ? _a : handler,
});
return this;
}
off(eventName, key) {
const observers = this.observers_[eventName];
if (observers) {
this.observers_[eventName] = observers.filter((observer) => {
return observer.key !== key;
});
}
return this;
}
emit(eventName, event) {
const observers = this.observers_[eventName];
if (!observers) {
return;
}
observers.forEach((observer) => {
observer.handler(event);
});
}
}
class ComplexValue {
constructor(initialValue, config) {
var _a;
this.constraint_ = config === null || config === void 0 ? void 0 : config.constraint;
this.equals_ = (_a = config === null || config === void 0 ? void 0 : config.equals) !== null && _a !== void 0 ? _a : ((v1, v2) => v1 === v2);
this.emitter = new Emitter();
this.rawValue_ = initialValue;
}
get constraint() {
return this.constraint_;
}
get rawValue() {
return this.rawValue_;
}
set rawValue(rawValue) {
this.setRawValue(rawValue, {
forceEmit: false,
last: true,
});
}
setRawValue(rawValue, options) {
const opts = options !== null && options !== void 0 ? options : {
forceEmit: false,
last: true,
};
const constrainedValue = this.constraint_
? this.constraint_.constrain(rawValue)
: rawValue;
const prevValue = this.rawValue_;
const changed = !this.equals_(prevValue, constrainedValue);
if (!changed && !opts.forceEmit) {
return;
}
this.emitter.emit('beforechange', {
sender: this,
});
this.rawValue_ = constrainedValue;
this.emitter.emit('change', {
options: opts,
previousRawValue: prevValue,
rawValue: constrainedValue,
sender: this,
});
}
}
class PrimitiveValue {
constructor(initialValue) {
this.emitter = new Emitter();
this.value_ = initialValue;
}
get rawValue() {
return this.value_;
}
set rawValue(value) {
this.setRawValue(value, {
forceEmit: false,
last: true,
});
}
setRawValue(value, options) {
const opts = options !== null && options !== void 0 ? options : {
forceEmit: false,
last: true,
};
const prevValue = this.value_;
if (prevValue === value && !opts.forceEmit) {
return;
}
this.emitter.emit('beforechange', {
sender: this,
});
this.value_ = value;
this.emitter.emit('change', {
options: opts,
previousRawValue: prevValue,
rawValue: this.value_,
sender: this,
});
}
}
class ReadonlyPrimitiveValue {
constructor(value) {
this.emitter = new Emitter();
this.onValueBeforeChange_ = this.onValueBeforeChange_.bind(this);
this.onValueChange_ = this.onValueChange_.bind(this);
this.value_ = value;
this.value_.emitter.on('beforechange', this.onValueBeforeChange_);
this.value_.emitter.on('change', this.onValueChange_);
}
get rawValue() {
return this.value_.rawValue;
}
onValueBeforeChange_(ev) {
this.emitter.emit('beforechange', Object.assign(Object.assign({}, ev), { sender: this }));
}
onValueChange_(ev) {
this.emitter.emit('change', Object.assign(Object.assign({}, ev), { sender: this }));
}
}
function createValue(initialValue, config) {
const constraint = config === null || config === void 0 ? void 0 : config.constraint;
const equals = config === null || config === void 0 ? void 0 : config.equals;
if (!constraint && !equals) {
return new PrimitiveValue(initialValue);
}
return new ComplexValue(initialValue, config);
}
function createReadonlyValue(value) {
return [
new ReadonlyPrimitiveValue(value),
(rawValue, options) => {
value.setRawValue(rawValue, options);
},
];
}
class ValueMap {
constructor(valueMap) {
this.emitter = new Emitter();
this.valMap_ = valueMap;
for (const key in this.valMap_) {
const v = this.valMap_[key];
v.emitter.on('change', () => {
this.emitter.emit('change', {
key: key,
sender: this,
});
});
}
}
static createCore(initialValue) {
const keys = Object.keys(initialValue);
return keys.reduce((o, key) => {
return Object.assign(o, {
[key]: createValue(initialValue[key]),
});
}, {});
}
static fromObject(initialValue) {
const core = this.createCore(initialValue);
return new ValueMap(core);
}
get(key) {
return this.valMap_[key].rawValue;
}
set(key, value) {
this.valMap_[key].rawValue = value;
}
value(key) {
return this.valMap_[key];
}
}
class DefiniteRangeConstraint {
constructor(config) {
this.values = ValueMap.fromObject({
max: config.max,
min: config.min,
});
}
constrain(value) {
const max = this.values.get('max');
const min = this.values.get('min');
return Math.min(Math.max(value, min), max);
}
}
class RangeConstraint {
constructor(config) {
this.values = ValueMap.fromObject({
max: config.max,
min: config.min,
});
}
constrain(value) {
const max = this.values.get('max');
const min = this.values.get('min');
let result = value;
if (!isEmpty(min)) {
result = Math.max(result, min);
}
if (!isEmpty(max)) {
result = Math.min(result, max);
}
return result;
}
}
class StepConstraint {
constructor(step, origin = 0) {
this.step = step;
this.origin = origin;
}
constrain(value) {
const o = this.origin % this.step;
const r = Math.round((value - o) / this.step);
return o + r * this.step;
}
}
class NumberLiteralNode {
constructor(text) {
this.text = text;
}
evaluate() {
return Number(this.text);
}
toString() {
return this.text;
}
}
const BINARY_OPERATION_MAP = {
'**': (v1, v2) => Math.pow(v1, v2),
'*': (v1, v2) => v1 * v2,
'/': (v1, v2) => v1 / v2,
'%': (v1, v2) => v1 % v2,
'+': (v1, v2) => v1 + v2,
'-': (v1, v2) => v1 - v2,
'<<': (v1, v2) => v1 << v2,
'>>': (v1, v2) => v1 >> v2,
'>>>': (v1, v2) => v1 >>> v2,
'&': (v1, v2) => v1 & v2,
'^': (v1, v2) => v1 ^ v2,
'|': (v1, v2) => v1 | v2,
};
class BinaryOperationNode {
constructor(operator, left, right) {
this.left = left;
this.operator = operator;
this.right = right;
}
evaluate() {
const op = BINARY_OPERATION_MAP[this.operator];
if (!op) {
throw new Error(`unexpected binary operator: '${this.operator}`);
}
return op(this.left.evaluate(), this.right.evaluate());
}
toString() {
return [
'b(',
this.left.toString(),
this.operator,
this.right.toString(),
')',
].join(' ');
}
}
const UNARY_OPERATION_MAP = {
'+': (v) => v,
'-': (v) => -v,
'~': (v) => ~v,
};
class UnaryOperationNode {
constructor(operator, expr) {
this.operator = operator;
this.expression = expr;
}
evaluate() {
const op = UNARY_OPERATION_MAP[this.operator];
if (!op) {
throw new Error(`unexpected unary operator: '${this.operator}`);
}
return op(this.expression.evaluate());
}
toString() {
return ['u(', this.operator, this.expression.toString(), ')'].join(' ');
}
}
function combineReader(parsers) {
return (text, cursor) => {
for (let i = 0; i < parsers.length; i++) {
const result = parsers[i](text, cursor);
if (result !== '') {
return result;
}
}
return '';
};
}
function readWhitespace(text, cursor) {
var _a;
const m = text.substr(cursor).match(/^\s+/);
return (_a = (m && m[0])) !== null && _a !== void 0 ? _a : '';
}
function readNonZeroDigit(text, cursor) {
const ch = text.substr(cursor, 1);
return ch.match(/^[1-9]$/) ? ch : '';
}
function readDecimalDigits(text, cursor) {
var _a;
const m = text.substr(cursor).match(/^[0-9]+/);
return (_a = (m && m[0])) !== null && _a !== void 0 ? _a : '';
}
function readSignedInteger(text, cursor) {
const ds = readDecimalDigits(text, cursor);
if (ds !== '') {
return ds;
}
const sign = text.substr(cursor, 1);
cursor += 1;
if (sign !== '-' && sign !== '+') {
return '';
}
const sds = readDecimalDigits(text, cursor);
if (sds === '') {
return '';
}
return sign + sds;
}
function readExponentPart(text, cursor) {
const e = text.substr(cursor, 1);
cursor += 1;
if (e.toLowerCase() !== 'e') {
return '';
}
const si = readSignedInteger(text, cursor);
if (si === '') {
return '';
}
return e + si;
}
function readDecimalIntegerLiteral(text, cursor) {
const ch = text.substr(cursor, 1);
if (ch === '0') {
return ch;
}
const nzd = readNonZeroDigit(text, cursor);
cursor += nzd.length;
if (nzd === '') {
return '';
}
return nzd + readDecimalDigits(text, cursor);
}
function readDecimalLiteral1(text, cursor) {
const dil = readDecimalIntegerLiteral(text, cursor);
cursor += dil.length;
if (dil === '') {
return '';
}
const dot = text.substr(cursor, 1);
cursor += dot.length;
if (dot !== '.') {
return '';
}
const dds = readDecimalDigits(text, cursor);
cursor += dds.length;
return dil + dot + dds + readExponentPart(text, cursor);
}
function readDecimalLiteral2(text, cursor) {
const dot = text.substr(cursor, 1);
cursor += dot.length;
if (dot !== '.') {
return '';
}
const dds = readDecimalDigits(text, cursor);
cursor += dds.length;
if (dds === '') {
return '';
}
return dot + dds + readExponentPart(text, cursor);
}
function readDecimalLiteral3(text, cursor) {
const dil = readDecimalIntegerLiteral(text, cursor);
cursor += dil.length;
if (dil === '') {
return '';
}
return dil + readExponentPart(text, cursor);
}
const readDecimalLiteral = combineReader([
readDecimalLiteral1,
readDecimalLiteral2,
readDecimalLiteral3,
]);
function parseBinaryDigits(text, cursor) {
var _a;
const m = text.substr(cursor).match(/^[01]+/);
return (_a = (m && m[0])) !== null && _a !== void 0 ? _a : '';
}
function readBinaryIntegerLiteral(text, cursor) {
const prefix = text.substr(cursor, 2);
cursor += prefix.length;
if (prefix.toLowerCase() !== '0b') {
return '';
}
const bds = parseBinaryDigits(text, cursor);
if (bds === '') {
return '';
}
return prefix + bds;
}
function readOctalDigits(text, cursor) {
var _a;
const m = text.substr(cursor).match(/^[0-7]+/);
return (_a = (m && m[0])) !== null && _a !== void 0 ? _a : '';
}
function readOctalIntegerLiteral(text, cursor) {
const prefix = text.substr(cursor, 2);
cursor += prefix.length;
if (prefix.toLowerCase() !== '0o') {
return '';
}
const ods = readOctalDigits(text, cursor);
if (ods === '') {
return '';
}
return prefix + ods;
}
function readHexDigits(text, cursor) {
var _a;
const m = text.substr(cursor).match(/^[0-9a-f]+/i);
return (_a = (m && m[0])) !== null && _a !== void 0 ? _a : '';
}
function readHexIntegerLiteral(text, cursor) {
const prefix = text.substr(cursor, 2);
cursor += prefix.length;
if (prefix.toLowerCase() !== '0x') {
return '';
}
const hds = readHexDigits(text, cursor);
if (hds === '') {
return '';
}
return prefix + hds;
}
const readNonDecimalIntegerLiteral = combineReader([
readBinaryIntegerLiteral,
readOctalIntegerLiteral,
readHexIntegerLiteral,
]);
const readNumericLiteral = combineReader([
readNonDecimalIntegerLiteral,
readDecimalLiteral,
]);
function parseLiteral(text, cursor) {
const num = readNumericLiteral(text, cursor);
cursor += num.length;
if (num === '') {
return null;
}
return {
evaluable: new NumberLiteralNode(num),
cursor: cursor,
};
}
function parseParenthesizedExpression(text, cursor) {
const op = text.substr(cursor, 1);
cursor += op.length;
if (op !== '(') {
return null;
}
const expr = parseExpression(text, cursor);
if (!expr) {
return null;
}
cursor = expr.cursor;
cursor += readWhitespace(text, cursor).length;
const cl = text.substr(cursor, 1);
cursor += cl.length;
if (cl !== ')') {
return null;
}
return {
evaluable: expr.evaluable,
cursor: cursor,
};
}
function parsePrimaryExpression(text, cursor) {
var _a;
return ((_a = parseLiteral(text, cursor)) !== null && _a !== void 0 ? _a : parseParenthesizedExpression(text, cursor));
}
function parseUnaryExpression(text, cursor) {
const expr = parsePrimaryExpression(text, cursor);
if (expr) {
return expr;
}
const op = text.substr(cursor, 1);
cursor += op.length;
if (op !== '+' && op !== '-' && op !== '~') {
return null;
}
const num = parseUnaryExpression(text, cursor);
if (!num) {
return null;
}
cursor = num.cursor;
return {
cursor: cursor,
evaluable: new UnaryOperationNode(op, num.evaluable),
};
}
function readBinaryOperator(ops, text, cursor) {
cursor += readWhitespace(text, cursor).length;
const op = ops.filter((op) => text.startsWith(op, cursor))[0];
if (!op) {
return null;
}
cursor += op.length;
cursor += readWhitespace(text, cursor).length;
return {
cursor: cursor,
operator: op,
};
}
function createBinaryOperationExpressionParser(exprParser, ops) {
return (text, cursor) => {
const firstExpr = exprParser(text, cursor);
if (!firstExpr) {
return null;
}
cursor = firstExpr.cursor;
let expr = firstExpr.evaluable;
for (;;) {
const op = readBinaryOperator(ops, text, cursor);
if (!op) {
break;
}
cursor = op.cursor;
const nextExpr = exprParser(text, cursor);
if (!nextExpr) {
return null;
}
cursor = nextExpr.cursor;
expr = new BinaryOperationNode(op.operator, expr, nextExpr.evaluable);
}
return expr
? {
cursor: cursor,
evaluable: expr,
}
: null;
};
}
const parseBinaryOperationExpression = [
['**'],
['*', '/', '%'],
['+', '-'],
['<<', '>>>', '>>'],
['&'],
['^'],
['|'],
].reduce((parser, ops) => {
return createBinaryOperationExpressionParser(parser, ops);
}, parseUnaryExpression);
function parseExpression(text, cursor) {
cursor += readWhitespace(text, cursor).length;
return parseBinaryOperationExpression(text, cursor);
}
function parseEcmaNumberExpression(text) {
const expr = parseExpression(text, 0);
if (!expr) {
return null;
}
const cursor = expr.cursor + readWhitespace(text, expr.cursor).length;
if (cursor !== text.length) {
return null;
}
return expr.evaluable;
}
function parseNumber(text) {
var _a;
const r = parseEcmaNumberExpression(text);
return (_a = r === null || r === void 0 ? void 0 : r.evaluate()) !== null && _a !== void 0 ? _a : null;
}
function numberFromUnknown(value) {
if (typeof value === 'number') {
return value;
}
if (typeof value === 'string') {
const pv = parseNumber(value);
if (!isEmpty(pv)) {
return pv;
}
}
return 0;
}
function numberToString(value) {
return String(value);
}
function createNumberFormatter(digits) {
return (value) => {
return value.toFixed(Math.max(Math.min(digits, 20), 0));
};
}
function mapRange(value, start1, end1, start2, end2) {
const p = (value - start1) / (end1 - start1);
return start2 + p * (end2 - start2);
}
function getDecimalDigits(value) {
const text = String(value.toFixed(10));
const frac = text.split('.')[1];
return frac.replace(/0+$/, '').length;
}
function constrainRange(value, min, max) {
return Math.min(Math.max(value, min), max);
}
function loopRange(value, max) {
return ((value % max) + max) % max;
}
function getSuitableDecimalDigits(params, rawValue) {
return !isEmpty(params.step)
? getDecimalDigits(params.step)
: Math.max(getDecimalDigits(rawValue), 2);
}
function getSuitableKeyScale(params) {
var _a;
return (_a = params.step) !== null && _a !== void 0 ? _a : 1;
}
function getSuitablePointerScale(params, rawValue) {
var _a;
const base = Math.abs((_a = params.step) !== null && _a !== void 0 ? _a : rawValue);
return base === 0 ? 0.1 : Math.pow(10, Math.floor(Math.log10(base)) - 1);
}
function createStepConstraint(params, initialValue) {
if (!isEmpty(params.step)) {
return new StepConstraint(params.step, initialValue);
}
return null;
}
function createRangeConstraint(params) {
if (!isEmpty(params.max) && !isEmpty(params.min)) {
return new DefiniteRangeConstraint({
max: params.max,
min: params.min,
});
}
if (!isEmpty(params.max) || !isEmpty(params.min)) {
return new RangeConstraint({
max: params.max,
min: params.min,
});
}
return null;
}
function createNumberTextPropsObject(params, initialValue) {
var _a, _b, _c;
return {
formatter: (_a = params.format) !== null && _a !== void 0 ? _a : createNumberFormatter(getSuitableDecimalDigits(params, initialValue)),
keyScale: (_b = params.keyScale) !== null && _b !== void 0 ? _b : getSuitableKeyScale(params),
pointerScale: (_c = params.pointerScale) !== null && _c !== void 0 ? _c : getSuitablePointerScale(params, initialValue),
};
}
function createNumberTextInputParamsParser(p) {
return {
format: p.optional.function,
keyScale: p.optional.number,
max: p.optional.number,
min: p.optional.number,
pointerScale: p.optional.number,
step: p.optional.number,
};
}
function createPointAxis(config) {
return {
constraint: config.constraint,
textProps: ValueMap.fromObject(createNumberTextPropsObject(config.params, config.initialValue)),
};
}
class BladeApi {
constructor(controller) {
this.controller = controller;
}
get element() {
return this.controller.view.element;
}
get disabled() {
return this.controller.viewProps.get('disabled');
}
set disabled(disabled) {
this.controller.viewProps.set('disabled', disabled);
}
get hidden() {
return this.controller.viewProps.get('hidden');
}
set hidden(hidden) {
this.controller.viewProps.set('hidden', hidden);
}
dispose() {
this.controller.viewProps.set('disposed', true);
}
importState(state) {
return this.controller.importState(state);
}
exportState() {
return this.controller.exportState();
}
}
class TpEvent {
constructor(target) {
this.target = target;
}
}
class TpChangeEvent extends TpEvent {
constructor(target, value, last) {
super(target);
this.value = value;
this.last = last !== null && last !== void 0 ? last : true;
}
}
class TpFoldEvent extends TpEvent {
constructor(target, expanded) {
super(target);
this.expanded = expanded;
}
}
class TpTabSelectEvent extends TpEvent {
constructor(target, index) {
super(target);
this.index = index;
}
}
class TpMouseEvent extends TpEvent {
constructor(target, nativeEvent) {
super(target);
this.native = nativeEvent;
}
}
class BindingApi extends BladeApi {
constructor(controller) {
super(controller);
this.onValueChange_ = this.onValueChange_.bind(this);
this.emitter_ = new Emitter();
this.controller.value.emitter.on('change', this.onValueChange_);
}
get label() {
return this.controller.labelController.props.get('label');
}
set label(label) {
this.controller.labelController.props.set('label', label);
}
get key() {
return this.controller.value.binding.target.key;
}
get tag() {
return this.controller.tag;
}
set tag(tag) {
this.controller.tag = tag;
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
refresh() {
this.controller.value.fetch();
}
onValueChange_(ev) {
const value = this.controller.value;
this.emitter_.emit('change', new TpChangeEvent(this, forceCast(value.binding.target.read()), ev.options.last));
}
}
class InputBindingValue {
constructor(value, binding) {
this.onValueBeforeChange_ = this.onValueBeforeChange_.bind(this);
this.onValueChange_ = this.onValueChange_.bind(this);
this.binding = binding;
this.value_ = value;
this.value_.emitter.on('beforechange', this.onValueBeforeChange_);
this.value_.emitter.on('change', this.onValueChange_);
this.emitter = new Emitter();
}
get rawValue() {
return this.value_.rawValue;
}
set rawValue(rawValue) {
this.value_.rawValue = rawValue;
}
setRawValue(rawValue, options) {
this.value_.setRawValue(rawValue, options);
}
fetch() {
this.value_.rawValue = this.binding.read();
}
push() {
this.binding.write(this.value_.rawValue);
}
onValueBeforeChange_(ev) {
this.emitter.emit('beforechange', Object.assign(Object.assign({}, ev), { sender: this }));
}
onValueChange_(ev) {
this.push();
this.emitter.emit('change', Object.assign(Object.assign({}, ev), { sender: this }));
}
}
function isInputBindingValue(v) {
if (!('binding' in v)) {
return false;
}
const b = v['binding'];
return isBinding(b) && 'read' in b && 'write' in b;
}
function parseObject(value, keyToParserMap) {
const keys = Object.keys(keyToParserMap);
const result = keys.reduce((tmp, key) => {
if (tmp === undefined) {
return undefined;
}
const parser = keyToParserMap[key];
const result = parser(value[key]);
return result.succeeded
? Object.assign(Object.assign({}, tmp), { [key]: result.value }) : undefined;
}, {});
return forceCast(result);
}
function parseArray(value, parseItem) {
return value.reduce((tmp, item) => {
if (tmp === undefined) {
return undefined;
}
const result = parseItem(item);
if (!result.succeeded || result.value === undefined) {
return undefined;
}
return [...tmp, result.value];
}, []);
}
function isObject(value) {
if (value === null) {
return false;
}
return typeof value === 'object';
}
function createMicroParserBuilder(parse) {
return (optional) => (v) => {
if (!optional && v === undefined) {
return {
succeeded: false,
value: undefined,
};
}
if (optional && v === undefined) {
return {
succeeded: true,
value: undefined,
};
}
const result = parse(v);
return result !== undefined
? {
succeeded: true,
value: result,
}
: {
succeeded: false,
value: undefined,
};
};
}
function createMicroParserBuilders(optional) {
return {
custom: (parse) => createMicroParserBuilder(parse)(optional),
boolean: createMicroParserBuilder((v) => typeof v === 'boolean' ? v : undefined)(optional),
number: createMicroParserBuilder((v) => typeof v === 'number' ? v : undefined)(optional),
string: createMicroParserBuilder((v) => typeof v === 'string' ? v : undefined)(optional),
function: createMicroParserBuilder((v) =>
typeof v === 'function' ? v : undefined)(optional),
constant: (value) => createMicroParserBuilder((v) => (v === value ? value : undefined))(optional),
raw: createMicroParserBuilder((v) => v)(optional),
object: (keyToParserMap) => createMicroParserBuilder((v) => {
if (!isObject(v)) {
return undefined;
}
return parseObject(v, keyToParserMap);
})(optional),
array: (itemParser) => createMicroParserBuilder((v) => {
if (!Array.isArray(v)) {
return undefined;
}
return parseArray(v, itemParser);
})(optional),
};
}
const MicroParsers = {
optional: createMicroParserBuilders(true),
required: createMicroParserBuilders(false),
};
function parseRecord(value, keyToParserMap) {
const map = keyToParserMap(MicroParsers);
const result = MicroParsers.required.object(map)(value);
return result.succeeded ? result.value : undefined;
}
function importBladeState(state, superImport, parser, callback) {
if (superImport && !superImport(state)) {
return false;
}
const result = parseRecord(state, parser);
return result ? callback(result) : false;
}
function exportBladeState(superExport, thisState) {
var _a;
return deepMerge((_a = superExport === null || superExport === void 0 ? void 0 : superExport()) !== null && _a !== void 0 ? _a : {}, thisState);
}
function isValueBladeController(bc) {
return 'value' in bc;
}
function isBindingValue(v) {
if (!isObject$1(v) || !('binding' in v)) {
return false;
}
const b = v.binding;
return isBinding(b);
}
const SVG_NS = 'http://www.w3.org/2000/svg';
function forceReflow(element) {
element.offsetHeight;
}
function disableTransitionTemporarily(element, callback) {
const t = element.style.transition;
element.style.transition = 'none';
callback();
element.style.transition = t;
}
function supportsTouch(doc) {
return doc.ontouchstart !== undefined;
}
function getGlobalObject() {
return globalThis;
}
function getWindowDocument() {
const globalObj = forceCast(getGlobalObject());
return globalObj.document;
}
function getCanvasContext(canvasElement) {
const win = canvasElement.ownerDocument.defaultView;
if (!win) {
return null;
}
const isBrowser = 'document' in win;
return isBrowser
? canvasElement.getContext('2d', {
willReadFrequently: true,
})
: null;
}
const ICON_ID_TO_INNER_HTML_MAP = {
check: '',
dropdown: '',
p2dpad: '',
};
function createSvgIconElement(document, iconId) {
const elem = document.createElementNS(SVG_NS, 'svg');
elem.innerHTML = ICON_ID_TO_INNER_HTML_MAP[iconId];
return elem;
}
function insertElementAt(parentElement, element, index) {
parentElement.insertBefore(element, parentElement.children[index]);
}
function removeElement(element) {
if (element.parentElement) {
element.parentElement.removeChild(element);
}
}
function removeChildElements(element) {
while (element.children.length > 0) {
element.removeChild(element.children[0]);
}
}
function removeChildNodes(element) {
while (element.childNodes.length > 0) {
element.removeChild(element.childNodes[0]);
}
}
function findNextTarget(ev) {
if (ev.relatedTarget) {
return forceCast(ev.relatedTarget);
}
if ('explicitOriginalTarget' in ev) {
return ev.explicitOriginalTarget;
}
return null;
}
function bindValue(value, applyValue) {
value.emitter.on('change', (ev) => {
applyValue(ev.rawValue);
});
applyValue(value.rawValue);
}
function bindValueMap(valueMap, key, applyValue) {
bindValue(valueMap.value(key), applyValue);
}
const PREFIX = 'tp';
function ClassName(viewName) {
const fn = (opt_elementName, opt_modifier) => {
return [
PREFIX,
'-',
viewName,
'v',
opt_elementName ? `_${opt_elementName}` : '',
opt_modifier ? `-${opt_modifier}` : '',
].join('');
};
return fn;
}
const cn$r = ClassName('lbl');
function createLabelNode(doc, label) {
const frag = doc.createDocumentFragment();
const lineNodes = label.split('\n').map((line) => {
return doc.createTextNode(line);
});
lineNodes.forEach((lineNode, index) => {
if (index > 0) {
frag.appendChild(doc.createElement('br'));
}
frag.appendChild(lineNode);
});
return frag;
}
class LabelView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$r());
config.viewProps.bindClassModifiers(this.element);
const labelElem = doc.createElement('div');
labelElem.classList.add(cn$r('l'));
bindValueMap(config.props, 'label', (value) => {
if (isEmpty(value)) {
this.element.classList.add(cn$r(undefined, 'nol'));
}
else {
this.element.classList.remove(cn$r(undefined, 'nol'));
removeChildNodes(labelElem);
labelElem.appendChild(createLabelNode(doc, value));
}
});
this.element.appendChild(labelElem);
this.labelElement = labelElem;
const valueElem = doc.createElement('div');
valueElem.classList.add(cn$r('v'));
this.element.appendChild(valueElem);
this.valueElement = valueElem;
}
}
class LabelController {
constructor(doc, config) {
this.props = config.props;
this.valueController = config.valueController;
this.viewProps = config.valueController.viewProps;
this.view = new LabelView(doc, {
props: config.props,
viewProps: this.viewProps,
});
this.view.valueElement.appendChild(this.valueController.view.element);
}
importProps(state) {
return importBladeState(state, null, (p) => ({
label: p.optional.string,
}), (result) => {
this.props.set('label', result.label);
return true;
});
}
exportProps() {
return exportBladeState(null, {
label: this.props.get('label'),
});
}
}
function getAllBladePositions() {
return ['veryfirst', 'first', 'last', 'verylast'];
}
const cn$q = ClassName('');
const POS_TO_CLASS_NAME_MAP = {
veryfirst: 'vfst',
first: 'fst',
last: 'lst',
verylast: 'vlst',
};
class BladeController {
constructor(config) {
this.parent_ = null;
this.blade = config.blade;
this.view = config.view;
this.viewProps = config.viewProps;
const elem = this.view.element;
this.blade.value('positions').emitter.on('change', () => {
getAllBladePositions().forEach((pos) => {
elem.classList.remove(cn$q(undefined, POS_TO_CLASS_NAME_MAP[pos]));
});
this.blade.get('positions').forEach((pos) => {
elem.classList.add(cn$q(undefined, POS_TO_CLASS_NAME_MAP[pos]));
});
});
this.viewProps.handleDispose(() => {
removeElement(elem);
});
}
get parent() {
return this.parent_;
}
set parent(parent) {
this.parent_ = parent;
this.viewProps.set('parent', this.parent_ ? this.parent_.viewProps : null);
}
importState(state) {
return importBladeState(state, null, (p) => ({
disabled: p.required.boolean,
hidden: p.required.boolean,
}), (result) => {
this.viewProps.importState(result);
return true;
});
}
exportState() {
return exportBladeState(null, Object.assign({}, this.viewProps.exportState()));
}
}
class LabeledValueBladeController extends BladeController {
constructor(doc, config) {
if (config.value !== config.valueController.value) {
throw TpError.shouldNeverHappen();
}
const viewProps = config.valueController.viewProps;
const lc = new LabelController(doc, {
blade: config.blade,
props: config.props,
valueController: config.valueController,
});
super(Object.assign(Object.assign({}, config), { view: new LabelView(doc, {
props: config.props,
viewProps: viewProps,
}), viewProps: viewProps }));
this.labelController = lc;
this.value = config.value;
this.valueController = config.valueController;
this.view.valueElement.appendChild(this.valueController.view.element);
}
importState(state) {
return importBladeState(state, (s) => {
var _a, _b, _c;
return super.importState(s) &&
this.labelController.importProps(s) &&
((_c = (_b = (_a = this.valueController).importProps) === null || _b === void 0 ? void 0 : _b.call(_a, state)) !== null && _c !== void 0 ? _c : true);
}, (p) => ({
value: p.optional.raw,
}), (result) => {
if (result.value) {
this.value.rawValue = result.value;
}
return true;
});
}
exportState() {
var _a, _b, _c;
return exportBladeState(() => super.exportState(), Object.assign(Object.assign({ value: this.value.rawValue }, this.labelController.exportProps()), ((_c = (_b = (_a = this.valueController).exportProps) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : {})));
}
}
function excludeValue(state) {
const result = Object.assign({}, state);
delete result.value;
return result;
}
class BindingController extends LabeledValueBladeController {
constructor(doc, config) {
super(doc, config);
this.tag = config.tag;
}
importState(state) {
return importBladeState(state,
(_s) => super.importState(excludeValue(state)), (p) => ({
tag: p.optional.string,
}), (result) => {
this.tag = result.tag;
return true;
});
}
exportState() {
return exportBladeState(() => excludeValue(super.exportState()), {
binding: {
key: this.value.binding.target.key,
value: this.value.binding.target.read(),
},
tag: this.tag,
});
}
}
function isBindingController(bc) {
return isValueBladeController(bc) && isBindingValue(bc.value);
}
class InputBindingController extends BindingController {
importState(state) {
return importBladeState(state, (s) => super.importState(s), (p) => ({
binding: p.required.object({
value: p.required.raw,
}),
}), (result) => {
this.value.binding.inject(result.binding.value);
this.value.fetch();
return true;
});
}
}
function isInputBindingController(bc) {
return isValueBladeController(bc) && isInputBindingValue(bc.value);
}
function fillBuffer(buffer, bufferSize) {
while (buffer.length < bufferSize) {
buffer.push(undefined);
}
}
function initializeBuffer(bufferSize) {
const buffer = [];
fillBuffer(buffer, bufferSize);
return buffer;
}
function createTrimmedBuffer(buffer) {
const index = buffer.indexOf(undefined);
return forceCast(index < 0 ? buffer : buffer.slice(0, index));
}
function createPushedBuffer(buffer, newValue) {
const newBuffer = [...createTrimmedBuffer(buffer), newValue];
if (newBuffer.length > buffer.length) {
newBuffer.splice(0, newBuffer.length - buffer.length);
}
else {
fillBuffer(newBuffer, buffer.length);
}
return newBuffer;
}
class MonitorBindingValue {
constructor(config) {
this.emitter = new Emitter();
this.onTick_ = this.onTick_.bind(this);
this.onValueBeforeChange_ = this.onValueBeforeChange_.bind(this);
this.onValueChange_ = this.onValueChange_.bind(this);
this.binding = config.binding;
this.value_ = createValue(initializeBuffer(config.bufferSize));
this.value_.emitter.on('beforechange', this.onValueBeforeChange_);
this.value_.emitter.on('change', this.onValueChange_);
this.ticker = config.ticker;
this.ticker.emitter.on('tick', this.onTick_);
this.fetch();
}
get rawValue() {
return this.value_.rawValue;
}
set rawValue(rawValue) {
this.value_.rawValue = rawValue;
}
setRawValue(rawValue, options) {
this.value_.setRawValue(rawValue, options);
}
fetch() {
this.value_.rawValue = createPushedBuffer(this.value_.rawValue, this.binding.read());
}
onTick_() {
this.fetch();
}
onValueBeforeChange_(ev) {
this.emitter.emit('beforechange', Object.assign(Object.assign({}, ev), { sender: this }));
}
onValueChange_(ev) {
this.emitter.emit('change', Object.assign(Object.assign({}, ev), { sender: this }));
}
}
function isMonitorBindingValue(v) {
if (!('binding' in v)) {
return false;
}
const b = v['binding'];
return isBinding(b) && 'read' in b && !('write' in b);
}
class MonitorBindingController extends BindingController {
exportState() {
return exportBladeState(() => super.exportState(), {
binding: {
readonly: true,
},
});
}
}
function isMonitorBindingController(bc) {
return (isValueBladeController(bc) &&
isMonitorBindingValue(bc.value));
}
class ButtonApi extends BladeApi {
get label() {
return this.controller.labelController.props.get('label');
}
set label(label) {
this.controller.labelController.props.set('label', label);
}
get title() {
var _a;
return (_a = this.controller.buttonController.props.get('title')) !== null && _a !== void 0 ? _a : '';
}
set title(title) {
this.controller.buttonController.props.set('title', title);
}
on(eventName, handler) {
const bh = handler.bind(this);
const emitter = this.controller.buttonController.emitter;
emitter.on(eventName, (ev) => {
bh(new TpMouseEvent(this, ev.nativeEvent));
});
return this;
}
off(eventName, handler) {
const emitter = this.controller.buttonController.emitter;
emitter.off(eventName, handler);
return this;
}
}
function applyClass(elem, className, active) {
if (active) {
elem.classList.add(className);
}
else {
elem.classList.remove(className);
}
}
function valueToClassName(elem, className) {
return (value) => {
applyClass(elem, className, value);
};
}
function bindValueToTextContent(value, elem) {
bindValue(value, (text) => {
elem.textContent = text !== null && text !== void 0 ? text : '';
});
}
const cn$p = ClassName('btn');
class ButtonView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$p());
config.viewProps.bindClassModifiers(this.element);
const buttonElem = doc.createElement('button');
buttonElem.classList.add(cn$p('b'));
config.viewProps.bindDisabled(buttonElem);
this.element.appendChild(buttonElem);
this.buttonElement = buttonElem;
const titleElem = doc.createElement('div');
titleElem.classList.add(cn$p('t'));
bindValueToTextContent(config.props.value('title'), titleElem);
this.buttonElement.appendChild(titleElem);
}
}
class ButtonController {
constructor(doc, config) {
this.emitter = new Emitter();
this.onClick_ = this.onClick_.bind(this);
this.props = config.props;
this.viewProps = config.viewProps;
this.view = new ButtonView(doc, {
props: this.props,
viewProps: this.viewProps,
});
this.view.buttonElement.addEventListener('click', this.onClick_);
}
importProps(state) {
return importBladeState(state, null, (p) => ({
title: p.optional.string,
}), (result) => {
this.props.set('title', result.title);
return true;
});
}
exportProps() {
return exportBladeState(null, {
title: this.props.get('title'),
});
}
onClick_(ev) {
this.emitter.emit('click', {
nativeEvent: ev,
sender: this,
});
}
}
class ButtonBladeController extends BladeController {
constructor(doc, config) {
const bc = new ButtonController(doc, {
props: config.buttonProps,
viewProps: config.viewProps,
});
const lc = new LabelController(doc, {
blade: config.blade,
props: config.labelProps,
valueController: bc,
});
super({
blade: config.blade,
view: lc.view,
viewProps: config.viewProps,
});
this.buttonController = bc;
this.labelController = lc;
}
importState(state) {
return importBladeState(state, (s) => super.importState(s) &&
this.buttonController.importProps(s) &&
this.labelController.importProps(s), () => ({}), () => true);
}
exportState() {
return exportBladeState(() => super.exportState(), Object.assign(Object.assign({}, this.buttonController.exportProps()), this.labelController.exportProps()));
}
}
class Semver {
constructor(text) {
const [core, prerelease] = text.split('-');
const coreComps = core.split('.');
this.major = parseInt(coreComps[0], 10);
this.minor = parseInt(coreComps[1], 10);
this.patch = parseInt(coreComps[2], 10);
this.prerelease = prerelease !== null && prerelease !== void 0 ? prerelease : null;
}
toString() {
const core = [this.major, this.minor, this.patch].join('.');
return this.prerelease !== null ? [core, this.prerelease].join('-') : core;
}
}
const VERSION$1 = new Semver('2.0.5');
function createPlugin(plugin) {
return Object.assign({ core: VERSION$1 }, plugin);
}
const ButtonBladePlugin = createPlugin({
id: 'button',
type: 'blade',
accept(params) {
const result = parseRecord(params, (p) => ({
title: p.required.string,
view: p.required.constant('button'),
label: p.optional.string,
}));
return result ? { params: result } : null;
},
controller(args) {
return new ButtonBladeController(args.document, {
blade: args.blade,
buttonProps: ValueMap.fromObject({
title: args.params.title,
}),
labelProps: ValueMap.fromObject({
label: args.params.label,
}),
viewProps: args.viewProps,
});
},
api(args) {
if (args.controller instanceof ButtonBladeController) {
return new ButtonApi(args.controller);
}
return null;
},
});
function addButtonAsBlade(api, params) {
return api.addBlade(Object.assign(Object.assign({}, params), { view: 'button' }));
}
function addFolderAsBlade(api, params) {
return api.addBlade(Object.assign(Object.assign({}, params), { view: 'folder' }));
}
function addTabAsBlade(api, params) {
return api.addBlade(Object.assign(Object.assign({}, params), { view: 'tab' }));
}
function isRefreshable(value) {
if (!isObject$1(value)) {
return false;
}
return 'refresh' in value && typeof value.refresh === 'function';
}
function createBindingTarget(obj, key) {
if (!BindingTarget.isBindable(obj)) {
throw TpError.notBindable();
}
return new BindingTarget(obj, key);
}
class RackApi {
constructor(controller, pool) {
this.onRackValueChange_ = this.onRackValueChange_.bind(this);
this.controller_ = controller;
this.emitter_ = new Emitter();
this.pool_ = pool;
const rack = this.controller_.rack;
rack.emitter.on('valuechange', this.onRackValueChange_);
}
get children() {
return this.controller_.rack.children.map((bc) => this.pool_.createApi(bc));
}
addBinding(object, key, opt_params) {
const params = opt_params !== null && opt_params !== void 0 ? opt_params : {};
const doc = this.controller_.element.ownerDocument;
const bc = this.pool_.createBinding(doc, createBindingTarget(object, key), params);
const api = this.pool_.createBindingApi(bc);
return this.add(api, params.index);
}
addFolder(params) {
return addFolderAsBlade(this, params);
}
addButton(params) {
return addButtonAsBlade(this, params);
}
addTab(params) {
return addTabAsBlade(this, params);
}
add(api, opt_index) {
const bc = api.controller;
this.controller_.rack.add(bc, opt_index);
return api;
}
remove(api) {
this.controller_.rack.remove(api.controller);
}
addBlade(params) {
const doc = this.controller_.element.ownerDocument;
const bc = this.pool_.createBlade(doc, params);
const api = this.pool_.createApi(bc);
return this.add(api, params.index);
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
refresh() {
this.children.forEach((c) => {
if (isRefreshable(c)) {
c.refresh();
}
});
}
onRackValueChange_(ev) {
const bc = ev.bladeController;
const api = this.pool_.createApi(bc);
const binding = isBindingValue(bc.value) ? bc.value.binding : null;
this.emitter_.emit('change', new TpChangeEvent(api, binding ? binding.target.read() : bc.value.rawValue, ev.options.last));
}
}
class ContainerBladeApi extends BladeApi {
constructor(controller, pool) {
super(controller);
this.rackApi_ = new RackApi(controller.rackController, pool);
}
refresh() {
this.rackApi_.refresh();
}
}
class ContainerBladeController extends BladeController {
constructor(config) {
super({
blade: config.blade,
view: config.view,
viewProps: config.rackController.viewProps,
});
this.rackController = config.rackController;
}
importState(state) {
return importBladeState(state, (s) => super.importState(s), (p) => ({
children: p.required.array(p.required.raw),
}), (result) => {
return this.rackController.rack.children.every((c, index) => {
return c.importState(result.children[index]);
});
});
}
exportState() {
return exportBladeState(() => super.exportState(), {
children: this.rackController.rack.children.map((c) => c.exportState()),
});
}
}
function isContainerBladeController(bc) {
return 'rackController' in bc;
}
class NestedOrderedSet {
constructor(extract) {
this.emitter = new Emitter();
this.items_ = [];
this.cache_ = new Set();
this.onSubListAdd_ = this.onSubListAdd_.bind(this);
this.onSubListRemove_ = this.onSubListRemove_.bind(this);
this.extract_ = extract;
}
get items() {
return this.items_;
}
allItems() {
return Array.from(this.cache_);
}
find(callback) {
for (const item of this.allItems()) {
if (callback(item)) {
return item;
}
}
return null;
}
includes(item) {
return this.cache_.has(item);
}
add(item, opt_index) {
if (this.includes(item)) {
throw TpError.shouldNeverHappen();
}
const index = opt_index !== undefined ? opt_index : this.items_.length;
this.items_.splice(index, 0, item);
this.cache_.add(item);
const subList = this.extract_(item);
if (subList) {
subList.emitter.on('add', this.onSubListAdd_);
subList.emitter.on('remove', this.onSubListRemove_);
subList.allItems().forEach((i) => {
this.cache_.add(i);
});
}
this.emitter.emit('add', {
index: index,
item: item,
root: this,
target: this,
});
}
remove(item) {
const index = this.items_.indexOf(item);
if (index < 0) {
return;
}
this.items_.splice(index, 1);
this.cache_.delete(item);
const subList = this.extract_(item);
if (subList) {
subList.allItems().forEach((i) => {
this.cache_.delete(i);
});
subList.emitter.off('add', this.onSubListAdd_);
subList.emitter.off('remove', this.onSubListRemove_);
}
this.emitter.emit('remove', {
index: index,
item: item,
root: this,
target: this,
});
}
onSubListAdd_(ev) {
this.cache_.add(ev.item);
this.emitter.emit('add', {
index: ev.index,
item: ev.item,
root: this,
target: ev.target,
});
}
onSubListRemove_(ev) {
this.cache_.delete(ev.item);
this.emitter.emit('remove', {
index: ev.index,
item: ev.item,
root: this,
target: ev.target,
});
}
}
function findValueBladeController(bcs, v) {
for (let i = 0; i < bcs.length; i++) {
const bc = bcs[i];
if (isValueBladeController(bc) && bc.value === v) {
return bc;
}
}
return null;
}
function findSubBladeControllerSet(bc) {
return isContainerBladeController(bc)
? bc.rackController.rack['bcSet_']
: null;
}
class Rack {
constructor(config) {
var _a, _b;
this.emitter = new Emitter();
this.onBladePositionsChange_ = this.onBladePositionsChange_.bind(this);
this.onSetAdd_ = this.onSetAdd_.bind(this);
this.onSetRemove_ = this.onSetRemove_.bind(this);
this.onChildDispose_ = this.onChildDispose_.bind(this);
this.onChildPositionsChange_ = this.onChildPositionsChange_.bind(this);
this.onChildValueChange_ = this.onChildValueChange_.bind(this);
this.onChildViewPropsChange_ = this.onChildViewPropsChange_.bind(this);
this.onRackLayout_ = this.onRackLayout_.bind(this);
this.onRackValueChange_ = this.onRackValueChange_.bind(this);
this.blade_ = (_a = config.blade) !== null && _a !== void 0 ? _a : null;
(_b = this.blade_) === null || _b === void 0 ? void 0 : _b.value('positions').emitter.on('change', this.onBladePositionsChange_);
this.viewProps = config.viewProps;
this.bcSet_ = new NestedOrderedSet(findSubBladeControllerSet);
this.bcSet_.emitter.on('add', this.onSetAdd_);
this.bcSet_.emitter.on('remove', this.onSetRemove_);
}
get children() {
return this.bcSet_.items;
}
add(bc, opt_index) {
var _a;
(_a = bc.parent) === null || _a === void 0 ? void 0 : _a.remove(bc);
bc.parent = this;
this.bcSet_.add(bc, opt_index);
}
remove(bc) {
bc.parent = null;
this.bcSet_.remove(bc);
}
find(finder) {
return this.bcSet_.allItems().filter(finder);
}
onSetAdd_(ev) {
this.updatePositions_();
const root = ev.target === ev.root;
this.emitter.emit('add', {
bladeController: ev.item,
index: ev.index,
root: root,
sender: this,
});
if (!root) {
return;
}
const bc = ev.item;
bc.viewProps.emitter.on('change', this.onChildViewPropsChange_);
bc.blade
.value('positions')
.emitter.on('change', this.onChildPositionsChange_);
bc.viewProps.handleDispose(this.onChildDispose_);
if (isValueBladeController(bc)) {
bc.value.emitter.on('change', this.onChildValueChange_);
}
else if (isContainerBladeController(bc)) {
const rack = bc.rackController.rack;
if (rack) {
const emitter = rack.emitter;
emitter.on('layout', this.onRackLayout_);
emitter.on('valuechange', this.onRackValueChange_);
}
}
}
onSetRemove_(ev) {
this.updatePositions_();
const root = ev.target === ev.root;
this.emitter.emit('remove', {
bladeController: ev.item,
root: root,
sender: this,
});
if (!root) {
return;
}
const bc = ev.item;
if (isValueBladeController(bc)) {
bc.value.emitter.off('change', this.onChildValueChange_);
}
else if (isContainerBladeController(bc)) {
const rack = bc.rackController.rack;
if (rack) {
const emitter = rack.emitter;
emitter.off('layout', this.onRackLayout_);
emitter.off('valuechange', this.onRackValueChange_);
}
}
}
updatePositions_() {
const visibleItems = this.bcSet_.items.filter((bc) => !bc.viewProps.get('hidden'));
const firstVisibleItem = visibleItems[0];
const lastVisibleItem = visibleItems[visibleItems.length - 1];
this.bcSet_.items.forEach((bc) => {
const ps = [];
if (bc === firstVisibleItem) {
ps.push('first');
if (!this.blade_ ||
this.blade_.get('positions').includes('veryfirst')) {
ps.push('veryfirst');
}
}
if (bc === lastVisibleItem) {
ps.push('last');
if (!this.blade_ || this.blade_.get('positions').includes('verylast')) {
ps.push('verylast');
}
}
bc.blade.set('positions', ps);
});
}
onChildPositionsChange_() {
this.updatePositions_();
this.emitter.emit('layout', {
sender: this,
});
}
onChildViewPropsChange_(_ev) {
this.updatePositions_();
this.emitter.emit('layout', {
sender: this,
});
}
onChildDispose_() {
const disposedUcs = this.bcSet_.items.filter((bc) => {
return bc.viewProps.get('disposed');
});
disposedUcs.forEach((bc) => {
this.bcSet_.remove(bc);
});
}
onChildValueChange_(ev) {
const bc = findValueBladeController(this.find(isValueBladeController), ev.sender);
if (!bc) {
throw TpError.alreadyDisposed();
}
this.emitter.emit('valuechange', {
bladeController: bc,
options: ev.options,
sender: this,
});
}
onRackLayout_(_) {
this.updatePositions_();
this.emitter.emit('layout', {
sender: this,
});
}
onRackValueChange_(ev) {
this.emitter.emit('valuechange', {
bladeController: ev.bladeController,
options: ev.options,
sender: this,
});
}
onBladePositionsChange_() {
this.updatePositions_();
}
}
class RackController {
constructor(config) {
this.onRackAdd_ = this.onRackAdd_.bind(this);
this.onRackRemove_ = this.onRackRemove_.bind(this);
this.element = config.element;
this.viewProps = config.viewProps;
const rack = new Rack({
blade: config.root ? undefined : config.blade,
viewProps: config.viewProps,
});
rack.emitter.on('add', this.onRackAdd_);
rack.emitter.on('remove', this.onRackRemove_);
this.rack = rack;
this.viewProps.handleDispose(() => {
for (let i = this.rack.children.length - 1; i >= 0; i--) {
const bc = this.rack.children[i];
bc.viewProps.set('disposed', true);
}
});
}
onRackAdd_(ev) {
if (!ev.root) {
return;
}
insertElementAt(this.element, ev.bladeController.view.element, ev.index);
}
onRackRemove_(ev) {
if (!ev.root) {
return;
}
removeElement(ev.bladeController.view.element);
}
}
function createBlade() {
return new ValueMap({
positions: createValue([], {
equals: deepEqualsArray,
}),
});
}
class Foldable extends ValueMap {
constructor(valueMap) {
super(valueMap);
}
static create(expanded) {
const coreObj = {
completed: true,
expanded: expanded,
expandedHeight: null,
shouldFixHeight: false,
temporaryExpanded: null,
};
const core = ValueMap.createCore(coreObj);
return new Foldable(core);
}
get styleExpanded() {
var _a;
return (_a = this.get('temporaryExpanded')) !== null && _a !== void 0 ? _a : this.get('expanded');
}
get styleHeight() {
if (!this.styleExpanded) {
return '0';
}
const exHeight = this.get('expandedHeight');
if (this.get('shouldFixHeight') && !isEmpty(exHeight)) {
return `${exHeight}px`;
}
return 'auto';
}
bindExpandedClass(elem, expandedClassName) {
const onExpand = () => {
const expanded = this.styleExpanded;
if (expanded) {
elem.classList.add(expandedClassName);
}
else {
elem.classList.remove(expandedClassName);
}
};
bindValueMap(this, 'expanded', onExpand);
bindValueMap(this, 'temporaryExpanded', onExpand);
}
cleanUpTransition() {
this.set('shouldFixHeight', false);
this.set('expandedHeight', null);
this.set('completed', true);
}
}
function computeExpandedFolderHeight(folder, containerElement) {
let height = 0;
disableTransitionTemporarily(containerElement, () => {
folder.set('expandedHeight', null);
folder.set('temporaryExpanded', true);
forceReflow(containerElement);
height = containerElement.clientHeight;
folder.set('temporaryExpanded', null);
forceReflow(containerElement);
});
return height;
}
function applyHeight(foldable, elem) {
elem.style.height = foldable.styleHeight;
}
function bindFoldable(foldable, elem) {
foldable.value('expanded').emitter.on('beforechange', () => {
foldable.set('completed', false);
if (isEmpty(foldable.get('expandedHeight'))) {
const h = computeExpandedFolderHeight(foldable, elem);
if (h > 0) {
foldable.set('expandedHeight', h);
}
}
foldable.set('shouldFixHeight', true);
forceReflow(elem);
});
foldable.emitter.on('change', () => {
applyHeight(foldable, elem);
});
applyHeight(foldable, elem);
elem.addEventListener('transitionend', (ev) => {
if (ev.propertyName !== 'height') {
return;
}
foldable.cleanUpTransition();
});
}
class FolderApi extends ContainerBladeApi {
constructor(controller, pool) {
super(controller, pool);
this.emitter_ = new Emitter();
this.controller.foldable
.value('expanded')
.emitter.on('change', (ev) => {
this.emitter_.emit('fold', new TpFoldEvent(this, ev.sender.rawValue));
});
this.rackApi_.on('change', (ev) => {
this.emitter_.emit('change', ev);
});
}
get expanded() {
return this.controller.foldable.get('expanded');
}
set expanded(expanded) {
this.controller.foldable.set('expanded', expanded);
}
get title() {
return this.controller.props.get('title');
}
set title(title) {
this.controller.props.set('title', title);
}
get children() {
return this.rackApi_.children;
}
addBinding(object, key, opt_params) {
return this.rackApi_.addBinding(object, key, opt_params);
}
addFolder(params) {
return this.rackApi_.addFolder(params);
}
addButton(params) {
return this.rackApi_.addButton(params);
}
addTab(params) {
return this.rackApi_.addTab(params);
}
add(api, opt_index) {
return this.rackApi_.add(api, opt_index);
}
remove(api) {
this.rackApi_.remove(api);
}
addBlade(params) {
return this.rackApi_.addBlade(params);
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
}
const bladeContainerClassName = ClassName('cnt');
class FolderView {
constructor(doc, config) {
var _a;
this.className_ = ClassName((_a = config.viewName) !== null && _a !== void 0 ? _a : 'fld');
this.element = doc.createElement('div');
this.element.classList.add(this.className_(), bladeContainerClassName());
config.viewProps.bindClassModifiers(this.element);
this.foldable_ = config.foldable;
this.foldable_.bindExpandedClass(this.element, this.className_(undefined, 'expanded'));
bindValueMap(this.foldable_, 'completed', valueToClassName(this.element, this.className_(undefined, 'cpl')));
const buttonElem = doc.createElement('button');
buttonElem.classList.add(this.className_('b'));
bindValueMap(config.props, 'title', (title) => {
if (isEmpty(title)) {
this.element.classList.add(this.className_(undefined, 'not'));
}
else {
this.element.classList.remove(this.className_(undefined, 'not'));
}
});
config.viewProps.bindDisabled(buttonElem);
this.element.appendChild(buttonElem);
this.buttonElement = buttonElem;
const indentElem = doc.createElement('div');
indentElem.classList.add(this.className_('i'));
this.element.appendChild(indentElem);
const titleElem = doc.createElement('div');
titleElem.classList.add(this.className_('t'));
bindValueToTextContent(config.props.value('title'), titleElem);
this.buttonElement.appendChild(titleElem);
this.titleElement = titleElem;
const markElem = doc.createElement('div');
markElem.classList.add(this.className_('m'));
this.buttonElement.appendChild(markElem);
const containerElem = doc.createElement('div');
containerElem.classList.add(this.className_('c'));
this.element.appendChild(containerElem);
this.containerElement = containerElem;
}
}
class FolderController extends ContainerBladeController {
constructor(doc, config) {
var _a;
const foldable = Foldable.create((_a = config.expanded) !== null && _a !== void 0 ? _a : true);
const view = new FolderView(doc, {
foldable: foldable,
props: config.props,
viewName: config.root ? 'rot' : undefined,
viewProps: config.viewProps,
});
super(Object.assign(Object.assign({}, config), { rackController: new RackController({
blade: config.blade,
element: view.containerElement,
root: config.root,
viewProps: config.viewProps,
}), view: view }));
this.onTitleClick_ = this.onTitleClick_.bind(this);
this.props = config.props;
this.foldable = foldable;
bindFoldable(this.foldable, this.view.containerElement);
this.rackController.rack.emitter.on('add', () => {
this.foldable.cleanUpTransition();
});
this.rackController.rack.emitter.on('remove', () => {
this.foldable.cleanUpTransition();
});
this.view.buttonElement.addEventListener('click', this.onTitleClick_);
}
get document() {
return this.view.element.ownerDocument;
}
importState(state) {
return importBladeState(state, (s) => super.importState(s), (p) => ({
expanded: p.required.boolean,
title: p.optional.string,
}), (result) => {
this.foldable.set('expanded', result.expanded);
this.props.set('title', result.title);
return true;
});
}
exportState() {
return exportBladeState(() => super.exportState(), {
expanded: this.foldable.get('expanded'),
title: this.props.get('title'),
});
}
onTitleClick_() {
this.foldable.set('expanded', !this.foldable.get('expanded'));
}
}
const FolderBladePlugin = createPlugin({
id: 'folder',
type: 'blade',
accept(params) {
const result = parseRecord(params, (p) => ({
title: p.required.string,
view: p.required.constant('folder'),
expanded: p.optional.boolean,
}));
return result ? { params: result } : null;
},
controller(args) {
return new FolderController(args.document, {
blade: args.blade,
expanded: args.params.expanded,
props: ValueMap.fromObject({
title: args.params.title,
}),
viewProps: args.viewProps,
});
},
api(args) {
if (!(args.controller instanceof FolderController)) {
return null;
}
return new FolderApi(args.controller, args.pool);
},
});
const cn$o = ClassName('');
function valueToModifier(elem, modifier) {
return valueToClassName(elem, cn$o(undefined, modifier));
}
class ViewProps extends ValueMap {
constructor(valueMap) {
var _a;
super(valueMap);
this.onDisabledChange_ = this.onDisabledChange_.bind(this);
this.onParentChange_ = this.onParentChange_.bind(this);
this.onParentGlobalDisabledChange_ =
this.onParentGlobalDisabledChange_.bind(this);
[this.globalDisabled_, this.setGlobalDisabled_] = createReadonlyValue(createValue(this.getGlobalDisabled_()));
this.value('disabled').emitter.on('change', this.onDisabledChange_);
this.value('parent').emitter.on('change', this.onParentChange_);
(_a = this.get('parent')) === null || _a === void 0 ? void 0 : _a.globalDisabled.emitter.on('change', this.onParentGlobalDisabledChange_);
}
static create(opt_initialValue) {
var _a, _b, _c;
const initialValue = opt_initialValue !== null && opt_initialValue !== void 0 ? opt_initialValue : {};
return new ViewProps(ValueMap.createCore({
disabled: (_a = initialValue.disabled) !== null && _a !== void 0 ? _a : false,
disposed: false,
hidden: (_b = initialValue.hidden) !== null && _b !== void 0 ? _b : false,
parent: (_c = initialValue.parent) !== null && _c !== void 0 ? _c : null,
}));
}
get globalDisabled() {
return this.globalDisabled_;
}
bindClassModifiers(elem) {
bindValue(this.globalDisabled_, valueToModifier(elem, 'disabled'));
bindValueMap(this, 'hidden', valueToModifier(elem, 'hidden'));
}
bindDisabled(target) {
bindValue(this.globalDisabled_, (disabled) => {
target.disabled = disabled;
});
}
bindTabIndex(elem) {
bindValue(this.globalDisabled_, (disabled) => {
elem.tabIndex = disabled ? -1 : 0;
});
}
handleDispose(callback) {
this.value('disposed').emitter.on('change', (disposed) => {
if (disposed) {
callback();
}
});
}
importState(state) {
this.set('disabled', state.disabled);
this.set('hidden', state.hidden);
}
exportState() {
return {
disabled: this.get('disabled'),
hidden: this.get('hidden'),
};
}
getGlobalDisabled_() {
const parent = this.get('parent');
const parentDisabled = parent ? parent.globalDisabled.rawValue : false;
return parentDisabled || this.get('disabled');
}
updateGlobalDisabled_() {
this.setGlobalDisabled_(this.getGlobalDisabled_());
}
onDisabledChange_() {
this.updateGlobalDisabled_();
}
onParentGlobalDisabledChange_() {
this.updateGlobalDisabled_();
}
onParentChange_(ev) {
var _a;
const prevParent = ev.previousRawValue;
prevParent === null || prevParent === void 0 ? void 0 : prevParent.globalDisabled.emitter.off('change', this.onParentGlobalDisabledChange_);
(_a = this.get('parent')) === null || _a === void 0 ? void 0 : _a.globalDisabled.emitter.on('change', this.onParentGlobalDisabledChange_);
this.updateGlobalDisabled_();
}
}
const cn$n = ClassName('tbp');
class TabPageView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$n());
config.viewProps.bindClassModifiers(this.element);
const containerElem = doc.createElement('div');
containerElem.classList.add(cn$n('c'));
this.element.appendChild(containerElem);
this.containerElement = containerElem;
}
}
const cn$m = ClassName('tbi');
class TabItemView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$m());
config.viewProps.bindClassModifiers(this.element);
bindValueMap(config.props, 'selected', (selected) => {
if (selected) {
this.element.classList.add(cn$m(undefined, 'sel'));
}
else {
this.element.classList.remove(cn$m(undefined, 'sel'));
}
});
const buttonElem = doc.createElement('button');
buttonElem.classList.add(cn$m('b'));
config.viewProps.bindDisabled(buttonElem);
this.element.appendChild(buttonElem);
this.buttonElement = buttonElem;
const titleElem = doc.createElement('div');
titleElem.classList.add(cn$m('t'));
bindValueToTextContent(config.props.value('title'), titleElem);
this.buttonElement.appendChild(titleElem);
this.titleElement = titleElem;
}
}
class TabItemController {
constructor(doc, config) {
this.emitter = new Emitter();
this.onClick_ = this.onClick_.bind(this);
this.props = config.props;
this.viewProps = config.viewProps;
this.view = new TabItemView(doc, {
props: config.props,
viewProps: config.viewProps,
});
this.view.buttonElement.addEventListener('click', this.onClick_);
}
onClick_() {
this.emitter.emit('click', {
sender: this,
});
}
}
class TabPageController extends ContainerBladeController {
constructor(doc, config) {
const view = new TabPageView(doc, {
viewProps: config.viewProps,
});
super(Object.assign(Object.assign({}, config), { rackController: new RackController({
blade: config.blade,
element: view.containerElement,
viewProps: config.viewProps,
}), view: view }));
this.onItemClick_ = this.onItemClick_.bind(this);
this.ic_ = new TabItemController(doc, {
props: config.itemProps,
viewProps: ViewProps.create(),
});
this.ic_.emitter.on('click', this.onItemClick_);
this.props = config.props;
bindValueMap(this.props, 'selected', (selected) => {
this.itemController.props.set('selected', selected);
this.viewProps.set('hidden', !selected);
});
}
get itemController() {
return this.ic_;
}
importState(state) {
return importBladeState(state, (s) => super.importState(s), (p) => ({
selected: p.required.boolean,
title: p.required.string,
}), (result) => {
this.ic_.props.set('selected', result.selected);
this.ic_.props.set('title', result.title);
return true;
});
}
exportState() {
return exportBladeState(() => super.exportState(), {
selected: this.ic_.props.get('selected'),
title: this.ic_.props.get('title'),
});
}
onItemClick_() {
this.props.set('selected', true);
}
}
class TabApi extends ContainerBladeApi {
constructor(controller, pool) {
super(controller, pool);
this.emitter_ = new Emitter();
this.onSelect_ = this.onSelect_.bind(this);
this.pool_ = pool;
this.rackApi_.on('change', (ev) => {
this.emitter_.emit('change', ev);
});
this.controller.tab.selectedIndex.emitter.on('change', this.onSelect_);
}
get pages() {
return this.rackApi_.children;
}
addPage(params) {
const doc = this.controller.view.element.ownerDocument;
const pc = new TabPageController(doc, {
blade: createBlade(),
itemProps: ValueMap.fromObject({
selected: false,
title: params.title,
}),
props: ValueMap.fromObject({
selected: false,
}),
viewProps: ViewProps.create(),
});
const papi = this.pool_.createApi(pc);
return this.rackApi_.add(papi, params.index);
}
removePage(index) {
this.rackApi_.remove(this.rackApi_.children[index]);
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
onSelect_(ev) {
this.emitter_.emit('select', new TpTabSelectEvent(this, ev.rawValue));
}
}
class TabPageApi extends ContainerBladeApi {
get title() {
var _a;
return (_a = this.controller.itemController.props.get('title')) !== null && _a !== void 0 ? _a : '';
}
set title(title) {
this.controller.itemController.props.set('title', title);
}
get selected() {
return this.controller.props.get('selected');
}
set selected(selected) {
this.controller.props.set('selected', selected);
}
get children() {
return this.rackApi_.children;
}
addButton(params) {
return this.rackApi_.addButton(params);
}
addFolder(params) {
return this.rackApi_.addFolder(params);
}
addTab(params) {
return this.rackApi_.addTab(params);
}
add(api, opt_index) {
this.rackApi_.add(api, opt_index);
}
remove(api) {
this.rackApi_.remove(api);
}
addBinding(object, key, opt_params) {
return this.rackApi_.addBinding(object, key, opt_params);
}
addBlade(params) {
return this.rackApi_.addBlade(params);
}
}
const INDEX_NOT_SELECTED = -1;
class Tab {
constructor() {
this.onItemSelectedChange_ = this.onItemSelectedChange_.bind(this);
this.empty = createValue(true);
this.selectedIndex = createValue(INDEX_NOT_SELECTED);
this.items_ = [];
}
add(item, opt_index) {
const index = opt_index !== null && opt_index !== void 0 ? opt_index : this.items_.length;
this.items_.splice(index, 0, item);
item.emitter.on('change', this.onItemSelectedChange_);
this.keepSelection_();
}
remove(item) {
const index = this.items_.indexOf(item);
if (index < 0) {
return;
}
this.items_.splice(index, 1);
item.emitter.off('change', this.onItemSelectedChange_);
this.keepSelection_();
}
keepSelection_() {
if (this.items_.length === 0) {
this.selectedIndex.rawValue = INDEX_NOT_SELECTED;
this.empty.rawValue = true;
return;
}
const firstSelIndex = this.items_.findIndex((s) => s.rawValue);
if (firstSelIndex < 0) {
this.items_.forEach((s, i) => {
s.rawValue = i === 0;
});
this.selectedIndex.rawValue = 0;
}
else {
this.items_.forEach((s, i) => {
s.rawValue = i === firstSelIndex;
});
this.selectedIndex.rawValue = firstSelIndex;
}
this.empty.rawValue = false;
}
onItemSelectedChange_(ev) {
if (ev.rawValue) {
const index = this.items_.findIndex((s) => s === ev.sender);
this.items_.forEach((s, i) => {
s.rawValue = i === index;
});
this.selectedIndex.rawValue = index;
}
else {
this.keepSelection_();
}
}
}
const cn$l = ClassName('tab');
class TabView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$l(), bladeContainerClassName());
config.viewProps.bindClassModifiers(this.element);
bindValue(config.empty, valueToClassName(this.element, cn$l(undefined, 'nop')));
const titleElem = doc.createElement('div');
titleElem.classList.add(cn$l('t'));
this.element.appendChild(titleElem);
this.itemsElement = titleElem;
const indentElem = doc.createElement('div');
indentElem.classList.add(cn$l('i'));
this.element.appendChild(indentElem);
const contentsElem = doc.createElement('div');
contentsElem.classList.add(cn$l('c'));
this.element.appendChild(contentsElem);
this.contentsElement = contentsElem;
}
}
class TabController extends ContainerBladeController {
constructor(doc, config) {
const tab = new Tab();
const view = new TabView(doc, {
empty: tab.empty,
viewProps: config.viewProps,
});
super({
blade: config.blade,
rackController: new RackController({
blade: config.blade,
element: view.contentsElement,
viewProps: config.viewProps,
}),
view: view,
});
this.onRackAdd_ = this.onRackAdd_.bind(this);
this.onRackRemove_ = this.onRackRemove_.bind(this);
const rack = this.rackController.rack;
rack.emitter.on('add', this.onRackAdd_);
rack.emitter.on('remove', this.onRackRemove_);
this.tab = tab;
}
add(pc, opt_index) {
this.rackController.rack.add(pc, opt_index);
}
remove(index) {
this.rackController.rack.remove(this.rackController.rack.children[index]);
}
onRackAdd_(ev) {
if (!ev.root) {
return;
}
const pc = ev.bladeController;
insertElementAt(this.view.itemsElement, pc.itemController.view.element, ev.index);
pc.itemController.viewProps.set('parent', this.viewProps);
this.tab.add(pc.props.value('selected'));
}
onRackRemove_(ev) {
if (!ev.root) {
return;
}
const pc = ev.bladeController;
removeElement(pc.itemController.view.element);
pc.itemController.viewProps.set('parent', null);
this.tab.remove(pc.props.value('selected'));
}
}
const TabBladePlugin = createPlugin({
id: 'tab',
type: 'blade',
accept(params) {
const result = parseRecord(params, (p) => ({
pages: p.required.array(p.required.object({ title: p.required.string })),
view: p.required.constant('tab'),
}));
if (!result || result.pages.length === 0) {
return null;
}
return { params: result };
},
controller(args) {
const c = new TabController(args.document, {
blade: args.blade,
viewProps: args.viewProps,
});
args.params.pages.forEach((p) => {
const pc = new TabPageController(args.document, {
blade: createBlade(),
itemProps: ValueMap.fromObject({
selected: false,
title: p.title,
}),
props: ValueMap.fromObject({
selected: false,
}),
viewProps: ViewProps.create(),
});
c.add(pc);
});
return c;
},
api(args) {
if (args.controller instanceof TabController) {
return new TabApi(args.controller, args.pool);
}
if (args.controller instanceof TabPageController) {
return new TabPageApi(args.controller, args.pool);
}
return null;
},
});
function createBladeController(plugin, args) {
const ac = plugin.accept(args.params);
if (!ac) {
return null;
}
const params = parseRecord(args.params, (p) => ({
disabled: p.optional.boolean,
hidden: p.optional.boolean,
}));
return plugin.controller({
blade: createBlade(),
document: args.document,
params: forceCast(Object.assign(Object.assign({}, ac.params), { disabled: params === null || params === void 0 ? void 0 : params.disabled, hidden: params === null || params === void 0 ? void 0 : params.hidden })),
viewProps: ViewProps.create({
disabled: params === null || params === void 0 ? void 0 : params.disabled,
hidden: params === null || params === void 0 ? void 0 : params.hidden,
}),
});
}
class ListInputBindingApi extends BindingApi {
get options() {
return this.controller.valueController.props.get('options');
}
set options(options) {
this.controller.valueController.props.set('options', options);
}
}
class ManualTicker {
constructor() {
this.disabled = false;
this.emitter = new Emitter();
}
dispose() { }
tick() {
if (this.disabled) {
return;
}
this.emitter.emit('tick', {
sender: this,
});
}
}
class IntervalTicker {
constructor(doc, interval) {
this.disabled_ = false;
this.timerId_ = null;
this.onTick_ = this.onTick_.bind(this);
this.doc_ = doc;
this.emitter = new Emitter();
this.interval_ = interval;
this.setTimer_();
}
get disabled() {
return this.disabled_;
}
set disabled(inactive) {
this.disabled_ = inactive;
if (this.disabled_) {
this.clearTimer_();
}
else {
this.setTimer_();
}
}
dispose() {
this.clearTimer_();
}
clearTimer_() {
if (this.timerId_ === null) {
return;
}
const win = this.doc_.defaultView;
if (win) {
win.clearInterval(this.timerId_);
}
this.timerId_ = null;
}
setTimer_() {
this.clearTimer_();
if (this.interval_ <= 0) {
return;
}
const win = this.doc_.defaultView;
if (win) {
this.timerId_ = win.setInterval(this.onTick_, this.interval_);
}
}
onTick_() {
if (this.disabled_) {
return;
}
this.emitter.emit('tick', {
sender: this,
});
}
}
class CompositeConstraint {
constructor(constraints) {
this.constraints = constraints;
}
constrain(value) {
return this.constraints.reduce((result, c) => {
return c.constrain(result);
}, value);
}
}
function findConstraint(c, constraintClass) {
if (c instanceof constraintClass) {
return c;
}
if (c instanceof CompositeConstraint) {
const result = c.constraints.reduce((tmpResult, sc) => {
if (tmpResult) {
return tmpResult;
}
return sc instanceof constraintClass ? sc : null;
}, null);
if (result) {
return result;
}
}
return null;
}
class ListConstraint {
constructor(options) {
this.values = ValueMap.fromObject({
options: options,
});
}
constrain(value) {
const opts = this.values.get('options');
if (opts.length === 0) {
return value;
}
const matched = opts.filter((item) => {
return item.value === value;
}).length > 0;
return matched ? value : opts[0].value;
}
}
function parseListOptions(value) {
var _a;
const p = MicroParsers;
if (Array.isArray(value)) {
return (_a = parseRecord({ items: value }, (p) => ({
items: p.required.array(p.required.object({
text: p.required.string,
value: p.required.raw,
})),
}))) === null || _a === void 0 ? void 0 : _a.items;
}
if (typeof value === 'object') {
return p.required.raw(value)
.value;
}
return undefined;
}
function normalizeListOptions(options) {
if (Array.isArray(options)) {
return options;
}
const items = [];
Object.keys(options).forEach((text) => {
items.push({ text: text, value: options[text] });
});
return items;
}
function createListConstraint(options) {
return !isEmpty(options)
? new ListConstraint(normalizeListOptions(forceCast(options)))
: null;
}
const cn$k = ClassName('lst');
class ListView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
this.props_ = config.props;
this.element = doc.createElement('div');
this.element.classList.add(cn$k());
config.viewProps.bindClassModifiers(this.element);
const selectElem = doc.createElement('select');
selectElem.classList.add(cn$k('s'));
config.viewProps.bindDisabled(selectElem);
this.element.appendChild(selectElem);
this.selectElement = selectElem;
const markElem = doc.createElement('div');
markElem.classList.add(cn$k('m'));
markElem.appendChild(createSvgIconElement(doc, 'dropdown'));
this.element.appendChild(markElem);
config.value.emitter.on('change', this.onValueChange_);
this.value_ = config.value;
bindValueMap(this.props_, 'options', (opts) => {
removeChildElements(this.selectElement);
opts.forEach((item) => {
const optionElem = doc.createElement('option');
optionElem.textContent = item.text;
this.selectElement.appendChild(optionElem);
});
this.update_();
});
}
update_() {
const values = this.props_.get('options').map((o) => o.value);
this.selectElement.selectedIndex = values.indexOf(this.value_.rawValue);
}
onValueChange_() {
this.update_();
}
}
class ListController {
constructor(doc, config) {
this.onSelectChange_ = this.onSelectChange_.bind(this);
this.props = config.props;
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new ListView(doc, {
props: this.props,
value: this.value,
viewProps: this.viewProps,
});
this.view.selectElement.addEventListener('change', this.onSelectChange_);
}
onSelectChange_(e) {
const selectElem = forceCast(e.currentTarget);
this.value.rawValue =
this.props.get('options')[selectElem.selectedIndex].value;
}
importProps(state) {
return importBladeState(state, null, (p) => ({
options: p.required.custom(parseListOptions),
}), (result) => {
this.props.set('options', normalizeListOptions(result.options));
return true;
});
}
exportProps() {
return exportBladeState(null, {
options: this.props.get('options'),
});
}
}
const cn$j = ClassName('pop');
class PopupView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$j());
config.viewProps.bindClassModifiers(this.element);
bindValue(config.shows, valueToClassName(this.element, cn$j(undefined, 'v')));
}
}
class PopupController {
constructor(doc, config) {
this.shows = createValue(false);
this.viewProps = config.viewProps;
this.view = new PopupView(doc, {
shows: this.shows,
viewProps: this.viewProps,
});
}
}
const cn$i = ClassName('txt');
class TextView {
constructor(doc, config) {
this.onChange_ = this.onChange_.bind(this);
this.element = doc.createElement('div');
this.element.classList.add(cn$i());
config.viewProps.bindClassModifiers(this.element);
this.props_ = config.props;
this.props_.emitter.on('change', this.onChange_);
const inputElem = doc.createElement('input');
inputElem.classList.add(cn$i('i'));
inputElem.type = 'text';
config.viewProps.bindDisabled(inputElem);
this.element.appendChild(inputElem);
this.inputElement = inputElem;
config.value.emitter.on('change', this.onChange_);
this.value_ = config.value;
this.refresh();
}
refresh() {
const formatter = this.props_.get('formatter');
this.inputElement.value = formatter(this.value_.rawValue);
}
onChange_() {
this.refresh();
}
}
class TextController {
constructor(doc, config) {
this.onInputChange_ = this.onInputChange_.bind(this);
this.parser_ = config.parser;
this.props = config.props;
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new TextView(doc, {
props: config.props,
value: this.value,
viewProps: this.viewProps,
});
this.view.inputElement.addEventListener('change', this.onInputChange_);
}
onInputChange_(e) {
const inputElem = forceCast(e.currentTarget);
const value = inputElem.value;
const parsedValue = this.parser_(value);
if (!isEmpty(parsedValue)) {
this.value.rawValue = parsedValue;
}
this.view.refresh();
}
}
function boolToString(value) {
return String(value);
}
function boolFromUnknown(value) {
if (value === 'false') {
return false;
}
return !!value;
}
function BooleanFormatter(value) {
return boolToString(value);
}
function composeParsers(parsers) {
return (text) => {
return parsers.reduce((result, parser) => {
if (result !== null) {
return result;
}
return parser(text);
}, null);
};
}
const innerFormatter = createNumberFormatter(0);
function formatPercentage(value) {
return innerFormatter(value) + '%';
}
function stringFromUnknown(value) {
return String(value);
}
function formatString(value) {
return value;
}
function connectValues({ primary, secondary, forward, backward, }) {
let changing = false;
function preventFeedback(callback) {
if (changing) {
return;
}
changing = true;
callback();
changing = false;
}
primary.emitter.on('change', (ev) => {
preventFeedback(() => {
secondary.setRawValue(forward(primary.rawValue, secondary.rawValue), ev.options);
});
});
secondary.emitter.on('change', (ev) => {
preventFeedback(() => {
primary.setRawValue(backward(primary.rawValue, secondary.rawValue), ev.options);
});
preventFeedback(() => {
secondary.setRawValue(forward(primary.rawValue, secondary.rawValue), ev.options);
});
});
preventFeedback(() => {
secondary.setRawValue(forward(primary.rawValue, secondary.rawValue), {
forceEmit: false,
last: true,
});
});
}
function getStepForKey(keyScale, keys) {
const step = keyScale * (keys.altKey ? 0.1 : 1) * (keys.shiftKey ? 10 : 1);
if (keys.upKey) {
return +step;
}
else if (keys.downKey) {
return -step;
}
return 0;
}
function getVerticalStepKeys(ev) {
return {
altKey: ev.altKey,
downKey: ev.key === 'ArrowDown',
shiftKey: ev.shiftKey,
upKey: ev.key === 'ArrowUp',
};
}
function getHorizontalStepKeys(ev) {
return {
altKey: ev.altKey,
downKey: ev.key === 'ArrowLeft',
shiftKey: ev.shiftKey,
upKey: ev.key === 'ArrowRight',
};
}
function isVerticalArrowKey(key) {
return key === 'ArrowUp' || key === 'ArrowDown';
}
function isArrowKey(key) {
return isVerticalArrowKey(key) || key === 'ArrowLeft' || key === 'ArrowRight';
}
function computeOffset$1(ev, elem) {
var _a, _b;
const win = elem.ownerDocument.defaultView;
const rect = elem.getBoundingClientRect();
return {
x: ev.pageX - (((_a = (win && win.scrollX)) !== null && _a !== void 0 ? _a : 0) + rect.left),
y: ev.pageY - (((_b = (win && win.scrollY)) !== null && _b !== void 0 ? _b : 0) + rect.top),
};
}
class PointerHandler {
constructor(element) {
this.lastTouch_ = null;
this.onDocumentMouseMove_ = this.onDocumentMouseMove_.bind(this);
this.onDocumentMouseUp_ = this.onDocumentMouseUp_.bind(this);
this.onMouseDown_ = this.onMouseDown_.bind(this);
this.onTouchEnd_ = this.onTouchEnd_.bind(this);
this.onTouchMove_ = this.onTouchMove_.bind(this);
this.onTouchStart_ = this.onTouchStart_.bind(this);
this.elem_ = element;
this.emitter = new Emitter();
element.addEventListener('touchstart', this.onTouchStart_, {
passive: false,
});
element.addEventListener('touchmove', this.onTouchMove_, {
passive: true,
});
element.addEventListener('touchend', this.onTouchEnd_);
element.addEventListener('mousedown', this.onMouseDown_);
}
computePosition_(offset) {
const rect = this.elem_.getBoundingClientRect();
return {
bounds: {
width: rect.width,
height: rect.height,
},
point: offset
? {
x: offset.x,
y: offset.y,
}
: null,
};
}
onMouseDown_(ev) {
var _a;
ev.preventDefault();
(_a = ev.currentTarget) === null || _a === void 0 ? void 0 : _a.focus();
const doc = this.elem_.ownerDocument;
doc.addEventListener('mousemove', this.onDocumentMouseMove_);
doc.addEventListener('mouseup', this.onDocumentMouseUp_);
this.emitter.emit('down', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset$1(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onDocumentMouseMove_(ev) {
this.emitter.emit('move', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset$1(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onDocumentMouseUp_(ev) {
const doc = this.elem_.ownerDocument;
doc.removeEventListener('mousemove', this.onDocumentMouseMove_);
doc.removeEventListener('mouseup', this.onDocumentMouseUp_);
this.emitter.emit('up', {
altKey: ev.altKey,
data: this.computePosition_(computeOffset$1(ev, this.elem_)),
sender: this,
shiftKey: ev.shiftKey,
});
}
onTouchStart_(ev) {
ev.preventDefault();
const touch = ev.targetTouches.item(0);
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('down', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
this.lastTouch_ = touch;
}
onTouchMove_(ev) {
const touch = ev.targetTouches.item(0);
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('move', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
this.lastTouch_ = touch;
}
onTouchEnd_(ev) {
var _a;
const touch = (_a = ev.targetTouches.item(0)) !== null && _a !== void 0 ? _a : this.lastTouch_;
const rect = this.elem_.getBoundingClientRect();
this.emitter.emit('up', {
altKey: ev.altKey,
data: this.computePosition_(touch
? {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
}
: undefined),
sender: this,
shiftKey: ev.shiftKey,
});
}
}
const cn$h = ClassName('txt');
class NumberTextView {
constructor(doc, config) {
this.onChange_ = this.onChange_.bind(this);
this.props_ = config.props;
this.props_.emitter.on('change', this.onChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$h(), cn$h(undefined, 'num'));
if (config.arrayPosition) {
this.element.classList.add(cn$h(undefined, config.arrayPosition));
}
config.viewProps.bindClassModifiers(this.element);
const inputElem = doc.createElement('input');
inputElem.classList.add(cn$h('i'));
inputElem.type = 'text';
config.viewProps.bindDisabled(inputElem);
this.element.appendChild(inputElem);
this.inputElement = inputElem;
this.onDraggingChange_ = this.onDraggingChange_.bind(this);
this.dragging_ = config.dragging;
this.dragging_.emitter.on('change', this.onDraggingChange_);
this.element.classList.add(cn$h());
this.inputElement.classList.add(cn$h('i'));
const knobElem = doc.createElement('div');
knobElem.classList.add(cn$h('k'));
this.element.appendChild(knobElem);
this.knobElement = knobElem;
const guideElem = doc.createElementNS(SVG_NS, 'svg');
guideElem.classList.add(cn$h('g'));
this.knobElement.appendChild(guideElem);
const bodyElem = doc.createElementNS(SVG_NS, 'path');
bodyElem.classList.add(cn$h('gb'));
guideElem.appendChild(bodyElem);
this.guideBodyElem_ = bodyElem;
const headElem = doc.createElementNS(SVG_NS, 'path');
headElem.classList.add(cn$h('gh'));
guideElem.appendChild(headElem);
this.guideHeadElem_ = headElem;
const tooltipElem = doc.createElement('div');
tooltipElem.classList.add(ClassName('tt')());
this.knobElement.appendChild(tooltipElem);
this.tooltipElem_ = tooltipElem;
config.value.emitter.on('change', this.onChange_);
this.value = config.value;
this.refresh();
}
onDraggingChange_(ev) {
if (ev.rawValue === null) {
this.element.classList.remove(cn$h(undefined, 'drg'));
return;
}
this.element.classList.add(cn$h(undefined, 'drg'));
const x = ev.rawValue / this.props_.get('pointerScale');
const aox = x + (x > 0 ? -1 : x < 0 ? +1 : 0);
const adx = constrainRange(-aox, -4, +4);
this.guideHeadElem_.setAttributeNS(null, 'd', [`M ${aox + adx},0 L${aox},4 L${aox + adx},8`, `M ${x},-1 L${x},9`].join(' '));
this.guideBodyElem_.setAttributeNS(null, 'd', `M 0,4 L${x},4`);
const formatter = this.props_.get('formatter');
this.tooltipElem_.textContent = formatter(this.value.rawValue);
this.tooltipElem_.style.left = `${x}px`;
}
refresh() {
const formatter = this.props_.get('formatter');
this.inputElement.value = formatter(this.value.rawValue);
}
onChange_() {
this.refresh();
}
}
class NumberTextController {
constructor(doc, config) {
var _a;
this.originRawValue_ = 0;
this.onInputChange_ = this.onInputChange_.bind(this);
this.onInputKeyDown_ = this.onInputKeyDown_.bind(this);
this.onInputKeyUp_ = this.onInputKeyUp_.bind(this);
this.onPointerDown_ = this.onPointerDown_.bind(this);
this.onPointerMove_ = this.onPointerMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.parser_ = config.parser;
this.props = config.props;
this.sliderProps_ = (_a = config.sliderProps) !== null && _a !== void 0 ? _a : null;
this.value = config.value;
this.viewProps = config.viewProps;
this.dragging_ = createValue(null);
this.view = new NumberTextView(doc, {
arrayPosition: config.arrayPosition,
dragging: this.dragging_,
props: this.props,
value: this.value,
viewProps: this.viewProps,
});
this.view.inputElement.addEventListener('change', this.onInputChange_);
this.view.inputElement.addEventListener('keydown', this.onInputKeyDown_);
this.view.inputElement.addEventListener('keyup', this.onInputKeyUp_);
const ph = new PointerHandler(this.view.knobElement);
ph.emitter.on('down', this.onPointerDown_);
ph.emitter.on('move', this.onPointerMove_);
ph.emitter.on('up', this.onPointerUp_);
}
constrainValue_(value) {
var _a, _b;
const min = (_a = this.sliderProps_) === null || _a === void 0 ? void 0 : _a.get('min');
const max = (_b = this.sliderProps_) === null || _b === void 0 ? void 0 : _b.get('max');
let v = value;
if (min !== undefined) {
v = Math.max(v, min);
}
if (max !== undefined) {
v = Math.min(v, max);
}
return v;
}
onInputChange_(e) {
const inputElem = forceCast(e.currentTarget);
const value = inputElem.value;
const parsedValue = this.parser_(value);
if (!isEmpty(parsedValue)) {
this.value.rawValue = this.constrainValue_(parsedValue);
}
this.view.refresh();
}
onInputKeyDown_(ev) {
const step = getStepForKey(this.props.get('keyScale'), getVerticalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.constrainValue_(this.value.rawValue + step), {
forceEmit: false,
last: false,
});
}
onInputKeyUp_(ev) {
const step = getStepForKey(this.props.get('keyScale'), getVerticalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
onPointerDown_() {
this.originRawValue_ = this.value.rawValue;
this.dragging_.rawValue = 0;
}
computeDraggingValue_(data) {
if (!data.point) {
return null;
}
const dx = data.point.x - data.bounds.width / 2;
return this.constrainValue_(this.originRawValue_ + dx * this.props.get('pointerScale'));
}
onPointerMove_(ev) {
const v = this.computeDraggingValue_(ev.data);
if (v === null) {
return;
}
this.value.setRawValue(v, {
forceEmit: false,
last: false,
});
this.dragging_.rawValue = this.value.rawValue - this.originRawValue_;
}
onPointerUp_(ev) {
const v = this.computeDraggingValue_(ev.data);
if (v === null) {
return;
}
this.value.setRawValue(v, {
forceEmit: true,
last: true,
});
this.dragging_.rawValue = null;
}
}
const cn$g = ClassName('sld');
class SliderView {
constructor(doc, config) {
this.onChange_ = this.onChange_.bind(this);
this.props_ = config.props;
this.props_.emitter.on('change', this.onChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$g());
config.viewProps.bindClassModifiers(this.element);
const trackElem = doc.createElement('div');
trackElem.classList.add(cn$g('t'));
config.viewProps.bindTabIndex(trackElem);
this.element.appendChild(trackElem);
this.trackElement = trackElem;
const knobElem = doc.createElement('div');
knobElem.classList.add(cn$g('k'));
this.trackElement.appendChild(knobElem);
this.knobElement = knobElem;
config.value.emitter.on('change', this.onChange_);
this.value = config.value;
this.update_();
}
update_() {
const p = constrainRange(mapRange(this.value.rawValue, this.props_.get('min'), this.props_.get('max'), 0, 100), 0, 100);
this.knobElement.style.width = `${p}%`;
}
onChange_() {
this.update_();
}
}
class SliderController {
constructor(doc, config) {
this.onKeyDown_ = this.onKeyDown_.bind(this);
this.onKeyUp_ = this.onKeyUp_.bind(this);
this.onPointerDownOrMove_ = this.onPointerDownOrMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.props = config.props;
this.view = new SliderView(doc, {
props: this.props,
value: this.value,
viewProps: this.viewProps,
});
this.ptHandler_ = new PointerHandler(this.view.trackElement);
this.ptHandler_.emitter.on('down', this.onPointerDownOrMove_);
this.ptHandler_.emitter.on('move', this.onPointerDownOrMove_);
this.ptHandler_.emitter.on('up', this.onPointerUp_);
this.view.trackElement.addEventListener('keydown', this.onKeyDown_);
this.view.trackElement.addEventListener('keyup', this.onKeyUp_);
}
handlePointerEvent_(d, opts) {
if (!d.point) {
return;
}
this.value.setRawValue(mapRange(constrainRange(d.point.x, 0, d.bounds.width), 0, d.bounds.width, this.props.get('min'), this.props.get('max')), opts);
}
onPointerDownOrMove_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerUp_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: true,
last: true,
});
}
onKeyDown_(ev) {
const step = getStepForKey(this.props.get('keyScale'), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.value.rawValue + step, {
forceEmit: false,
last: false,
});
}
onKeyUp_(ev) {
const step = getStepForKey(this.props.get('keyScale'), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
}
const cn$f = ClassName('sldtxt');
class SliderTextView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$f());
const sliderElem = doc.createElement('div');
sliderElem.classList.add(cn$f('s'));
this.sliderView_ = config.sliderView;
sliderElem.appendChild(this.sliderView_.element);
this.element.appendChild(sliderElem);
const textElem = doc.createElement('div');
textElem.classList.add(cn$f('t'));
this.textView_ = config.textView;
textElem.appendChild(this.textView_.element);
this.element.appendChild(textElem);
}
}
class SliderTextController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.sliderC_ = new SliderController(doc, {
props: config.sliderProps,
value: config.value,
viewProps: this.viewProps,
});
this.textC_ = new NumberTextController(doc, {
parser: config.parser,
props: config.textProps,
sliderProps: config.sliderProps,
value: config.value,
viewProps: config.viewProps,
});
this.view = new SliderTextView(doc, {
sliderView: this.sliderC_.view,
textView: this.textC_.view,
});
}
get sliderController() {
return this.sliderC_;
}
get textController() {
return this.textC_;
}
importProps(state) {
return importBladeState(state, null, (p) => ({
max: p.required.number,
min: p.required.number,
}), (result) => {
const sliderProps = this.sliderC_.props;
sliderProps.set('max', result.max);
sliderProps.set('min', result.min);
return true;
});
}
exportProps() {
const sliderProps = this.sliderC_.props;
return exportBladeState(null, {
max: sliderProps.get('max'),
min: sliderProps.get('min'),
});
}
}
function createSliderTextProps(config) {
return {
sliderProps: new ValueMap({
keyScale: config.keyScale,
max: config.max,
min: config.min,
}),
textProps: new ValueMap({
formatter: createValue(config.formatter),
keyScale: config.keyScale,
pointerScale: createValue(config.pointerScale),
}),
};
}
const CSS_VAR_MAP = {
containerUnitSize: 'cnt-usz',
};
function getCssVar(key) {
return `--${CSS_VAR_MAP[key]}`;
}
function createPointDimensionParser(p) {
return createNumberTextInputParamsParser(p);
}
function parsePointDimensionParams(value) {
if (!isRecord(value)) {
return undefined;
}
return parseRecord(value, createPointDimensionParser);
}
function createDimensionConstraint(params, initialValue) {
if (!params) {
return undefined;
}
const constraints = [];
const cs = createStepConstraint(params, initialValue);
if (cs) {
constraints.push(cs);
}
const rs = createRangeConstraint(params);
if (rs) {
constraints.push(rs);
}
return new CompositeConstraint(constraints);
}
function isCompatible(ver) {
if (!ver) {
return false;
}
return ver.major === VERSION$1.major;
}
function parsePickerLayout(value) {
if (value === 'inline' || value === 'popup') {
return value;
}
return undefined;
}
function writePrimitive(target, value) {
target.write(value);
}
const cn$e = ClassName('ckb');
class CheckboxView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
this.element = doc.createElement('div');
this.element.classList.add(cn$e());
config.viewProps.bindClassModifiers(this.element);
const labelElem = doc.createElement('label');
labelElem.classList.add(cn$e('l'));
this.element.appendChild(labelElem);
this.labelElement = labelElem;
const inputElem = doc.createElement('input');
inputElem.classList.add(cn$e('i'));
inputElem.type = 'checkbox';
this.labelElement.appendChild(inputElem);
this.inputElement = inputElem;
config.viewProps.bindDisabled(this.inputElement);
const wrapperElem = doc.createElement('div');
wrapperElem.classList.add(cn$e('w'));
this.labelElement.appendChild(wrapperElem);
const markElem = createSvgIconElement(doc, 'check');
wrapperElem.appendChild(markElem);
config.value.emitter.on('change', this.onValueChange_);
this.value = config.value;
this.update_();
}
update_() {
this.inputElement.checked = this.value.rawValue;
}
onValueChange_() {
this.update_();
}
}
class CheckboxController {
constructor(doc, config) {
this.onInputChange_ = this.onInputChange_.bind(this);
this.onLabelMouseDown_ = this.onLabelMouseDown_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new CheckboxView(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.view.inputElement.addEventListener('change', this.onInputChange_);
this.view.labelElement.addEventListener('mousedown', this.onLabelMouseDown_);
}
onInputChange_(ev) {
const inputElem = forceCast(ev.currentTarget);
this.value.rawValue = inputElem.checked;
ev.preventDefault();
ev.stopPropagation();
}
onLabelMouseDown_(ev) {
ev.preventDefault();
}
}
function createConstraint$6(params) {
const constraints = [];
const lc = createListConstraint(params.options);
if (lc) {
constraints.push(lc);
}
return new CompositeConstraint(constraints);
}
const BooleanInputPlugin = createPlugin({
id: 'input-bool',
type: 'input',
accept: (value, params) => {
if (typeof value !== 'boolean') {
return null;
}
const result = parseRecord(params, (p) => ({
options: p.optional.custom(parseListOptions),
readonly: p.optional.constant(false),
}));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => boolFromUnknown,
constraint: (args) => createConstraint$6(args.params),
writer: (_args) => writePrimitive,
},
controller: (args) => {
const doc = args.document;
const value = args.value;
const c = args.constraint;
const lc = c && findConstraint(c, ListConstraint);
if (lc) {
return new ListController(doc, {
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
});
}
return new CheckboxController(doc, {
value: value,
viewProps: args.viewProps,
});
},
api(args) {
if (typeof args.controller.value.rawValue !== 'boolean') {
return null;
}
if (args.controller.valueController instanceof ListController) {
return new ListInputBindingApi(args.controller);
}
return null;
},
});
const cn$d = ClassName('col');
class ColorView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$d());
config.foldable.bindExpandedClass(this.element, cn$d(undefined, 'expanded'));
bindValueMap(config.foldable, 'completed', valueToClassName(this.element, cn$d(undefined, 'cpl')));
const headElem = doc.createElement('div');
headElem.classList.add(cn$d('h'));
this.element.appendChild(headElem);
const swatchElem = doc.createElement('div');
swatchElem.classList.add(cn$d('s'));
headElem.appendChild(swatchElem);
this.swatchElement = swatchElem;
const textElem = doc.createElement('div');
textElem.classList.add(cn$d('t'));
headElem.appendChild(textElem);
this.textElement = textElem;
if (config.pickerLayout === 'inline') {
const pickerElem = doc.createElement('div');
pickerElem.classList.add(cn$d('p'));
this.element.appendChild(pickerElem);
this.pickerElement = pickerElem;
}
else {
this.pickerElement = null;
}
}
}
function rgbToHslInt(r, g, b) {
const rp = constrainRange(r / 255, 0, 1);
const gp = constrainRange(g / 255, 0, 1);
const bp = constrainRange(b / 255, 0, 1);
const cmax = Math.max(rp, gp, bp);
const cmin = Math.min(rp, gp, bp);
const c = cmax - cmin;
let h = 0;
let s = 0;
const l = (cmin + cmax) / 2;
if (c !== 0) {
s = c / (1 - Math.abs(cmax + cmin - 1));
if (rp === cmax) {
h = (gp - bp) / c;
}
else if (gp === cmax) {
h = 2 + (bp - rp) / c;
}
else {
h = 4 + (rp - gp) / c;
}
h = h / 6 + (h < 0 ? 1 : 0);
}
return [h * 360, s * 100, l * 100];
}
function hslToRgbInt(h, s, l) {
const hp = ((h % 360) + 360) % 360;
const sp = constrainRange(s / 100, 0, 1);
const lp = constrainRange(l / 100, 0, 1);
const c = (1 - Math.abs(2 * lp - 1)) * sp;
const x = c * (1 - Math.abs(((hp / 60) % 2) - 1));
const m = lp - c / 2;
let rp, gp, bp;
if (hp >= 0 && hp < 60) {
[rp, gp, bp] = [c, x, 0];
}
else if (hp >= 60 && hp < 120) {
[rp, gp, bp] = [x, c, 0];
}
else if (hp >= 120 && hp < 180) {
[rp, gp, bp] = [0, c, x];
}
else if (hp >= 180 && hp < 240) {
[rp, gp, bp] = [0, x, c];
}
else if (hp >= 240 && hp < 300) {
[rp, gp, bp] = [x, 0, c];
}
else {
[rp, gp, bp] = [c, 0, x];
}
return [(rp + m) * 255, (gp + m) * 255, (bp + m) * 255];
}
function rgbToHsvInt(r, g, b) {
const rp = constrainRange(r / 255, 0, 1);
const gp = constrainRange(g / 255, 0, 1);
const bp = constrainRange(b / 255, 0, 1);
const cmax = Math.max(rp, gp, bp);
const cmin = Math.min(rp, gp, bp);
const d = cmax - cmin;
let h;
if (d === 0) {
h = 0;
}
else if (cmax === rp) {
h = 60 * (((((gp - bp) / d) % 6) + 6) % 6);
}
else if (cmax === gp) {
h = 60 * ((bp - rp) / d + 2);
}
else {
h = 60 * ((rp - gp) / d + 4);
}
const s = cmax === 0 ? 0 : d / cmax;
const v = cmax;
return [h, s * 100, v * 100];
}
function hsvToRgbInt(h, s, v) {
const hp = loopRange(h, 360);
const sp = constrainRange(s / 100, 0, 1);
const vp = constrainRange(v / 100, 0, 1);
const c = vp * sp;
const x = c * (1 - Math.abs(((hp / 60) % 2) - 1));
const m = vp - c;
let rp, gp, bp;
if (hp >= 0 && hp < 60) {
[rp, gp, bp] = [c, x, 0];
}
else if (hp >= 60 && hp < 120) {
[rp, gp, bp] = [x, c, 0];
}
else if (hp >= 120 && hp < 180) {
[rp, gp, bp] = [0, c, x];
}
else if (hp >= 180 && hp < 240) {
[rp, gp, bp] = [0, x, c];
}
else if (hp >= 240 && hp < 300) {
[rp, gp, bp] = [x, 0, c];
}
else {
[rp, gp, bp] = [c, 0, x];
}
return [(rp + m) * 255, (gp + m) * 255, (bp + m) * 255];
}
function hslToHsvInt(h, s, l) {
const sd = l + (s * (100 - Math.abs(2 * l - 100))) / (2 * 100);
return [
h,
sd !== 0 ? (s * (100 - Math.abs(2 * l - 100))) / sd : 0,
l + (s * (100 - Math.abs(2 * l - 100))) / (2 * 100),
];
}
function hsvToHslInt(h, s, v) {
const sd = 100 - Math.abs((v * (200 - s)) / 100 - 100);
return [h, sd !== 0 ? (s * v) / sd : 0, (v * (200 - s)) / (2 * 100)];
}
function removeAlphaComponent(comps) {
return [comps[0], comps[1], comps[2]];
}
function appendAlphaComponent(comps, alpha) {
return [comps[0], comps[1], comps[2], alpha];
}
const MODE_CONVERTER_MAP = {
hsl: {
hsl: (h, s, l) => [h, s, l],
hsv: hslToHsvInt,
rgb: hslToRgbInt,
},
hsv: {
hsl: hsvToHslInt,
hsv: (h, s, v) => [h, s, v],
rgb: hsvToRgbInt,
},
rgb: {
hsl: rgbToHslInt,
hsv: rgbToHsvInt,
rgb: (r, g, b) => [r, g, b],
},
};
function getColorMaxComponents(mode, type) {
return [
type === 'float' ? 1 : mode === 'rgb' ? 255 : 360,
type === 'float' ? 1 : mode === 'rgb' ? 255 : 100,
type === 'float' ? 1 : mode === 'rgb' ? 255 : 100,
];
}
function loopHueRange(hue, max) {
return hue === max ? max : loopRange(hue, max);
}
function constrainColorComponents(components, mode, type) {
var _a;
const ms = getColorMaxComponents(mode, type);
return [
mode === 'rgb'
? constrainRange(components[0], 0, ms[0])
: loopHueRange(components[0], ms[0]),
constrainRange(components[1], 0, ms[1]),
constrainRange(components[2], 0, ms[2]),
constrainRange((_a = components[3]) !== null && _a !== void 0 ? _a : 1, 0, 1),
];
}
function convertColorType(comps, mode, from, to) {
const fms = getColorMaxComponents(mode, from);
const tms = getColorMaxComponents(mode, to);
return comps.map((c, index) => (c / fms[index]) * tms[index]);
}
function convertColor(components, from, to) {
const intComps = convertColorType(components, from.mode, from.type, 'int');
const result = MODE_CONVERTER_MAP[from.mode][to.mode](...intComps);
return convertColorType(result, to.mode, 'int', to.type);
}
class IntColor {
static black() {
return new IntColor([0, 0, 0], 'rgb');
}
constructor(comps, mode) {
this.type = 'int';
this.mode = mode;
this.comps_ = constrainColorComponents(comps, mode, this.type);
}
getComponents(opt_mode) {
return appendAlphaComponent(convertColor(removeAlphaComponent(this.comps_), { mode: this.mode, type: this.type }, { mode: opt_mode !== null && opt_mode !== void 0 ? opt_mode : this.mode, type: this.type }), this.comps_[3]);
}
toRgbaObject() {
const rgbComps = this.getComponents('rgb');
return {
r: rgbComps[0],
g: rgbComps[1],
b: rgbComps[2],
a: rgbComps[3],
};
}
}
const cn$c = ClassName('colp');
class ColorPickerView {
constructor(doc, config) {
this.alphaViews_ = null;
this.element = doc.createElement('div');
this.element.classList.add(cn$c());
config.viewProps.bindClassModifiers(this.element);
const hsvElem = doc.createElement('div');
hsvElem.classList.add(cn$c('hsv'));
const svElem = doc.createElement('div');
svElem.classList.add(cn$c('sv'));
this.svPaletteView_ = config.svPaletteView;
svElem.appendChild(this.svPaletteView_.element);
hsvElem.appendChild(svElem);
const hElem = doc.createElement('div');
hElem.classList.add(cn$c('h'));
this.hPaletteView_ = config.hPaletteView;
hElem.appendChild(this.hPaletteView_.element);
hsvElem.appendChild(hElem);
this.element.appendChild(hsvElem);
const rgbElem = doc.createElement('div');
rgbElem.classList.add(cn$c('rgb'));
this.textsView_ = config.textsView;
rgbElem.appendChild(this.textsView_.element);
this.element.appendChild(rgbElem);
if (config.alphaViews) {
this.alphaViews_ = {
palette: config.alphaViews.palette,
text: config.alphaViews.text,
};
const aElem = doc.createElement('div');
aElem.classList.add(cn$c('a'));
const apElem = doc.createElement('div');
apElem.classList.add(cn$c('ap'));
apElem.appendChild(this.alphaViews_.palette.element);
aElem.appendChild(apElem);
const atElem = doc.createElement('div');
atElem.classList.add(cn$c('at'));
atElem.appendChild(this.alphaViews_.text.element);
aElem.appendChild(atElem);
this.element.appendChild(aElem);
}
}
get allFocusableElements() {
const elems = [
this.svPaletteView_.element,
this.hPaletteView_.element,
this.textsView_.modeSelectElement,
...this.textsView_.inputViews.map((v) => v.inputElement),
];
if (this.alphaViews_) {
elems.push(this.alphaViews_.palette.element, this.alphaViews_.text.inputElement);
}
return elems;
}
}
function parseColorType(value) {
return value === 'int' ? 'int' : value === 'float' ? 'float' : undefined;
}
function parseColorInputParams(params) {
return parseRecord(params, (p) => ({
color: p.optional.object({
alpha: p.optional.boolean,
type: p.optional.custom(parseColorType),
}),
expanded: p.optional.boolean,
picker: p.optional.custom(parsePickerLayout),
readonly: p.optional.constant(false),
}));
}
function getKeyScaleForColor(forAlpha) {
return forAlpha ? 0.1 : 1;
}
function extractColorType(params) {
var _a;
return (_a = params.color) === null || _a === void 0 ? void 0 : _a.type;
}
class FloatColor {
constructor(comps, mode) {
this.type = 'float';
this.mode = mode;
this.comps_ = constrainColorComponents(comps, mode, this.type);
}
getComponents(opt_mode) {
return appendAlphaComponent(convertColor(removeAlphaComponent(this.comps_), { mode: this.mode, type: this.type }, { mode: opt_mode !== null && opt_mode !== void 0 ? opt_mode : this.mode, type: this.type }), this.comps_[3]);
}
toRgbaObject() {
const rgbComps = this.getComponents('rgb');
return {
r: rgbComps[0],
g: rgbComps[1],
b: rgbComps[2],
a: rgbComps[3],
};
}
}
const TYPE_TO_CONSTRUCTOR_MAP = {
int: (comps, mode) => new IntColor(comps, mode),
float: (comps, mode) => new FloatColor(comps, mode),
};
function createColor(comps, mode, type) {
return TYPE_TO_CONSTRUCTOR_MAP[type](comps, mode);
}
function isFloatColor(c) {
return c.type === 'float';
}
function isIntColor(c) {
return c.type === 'int';
}
function convertFloatToInt(cf) {
const comps = cf.getComponents();
const ms = getColorMaxComponents(cf.mode, 'int');
return new IntColor([
Math.round(mapRange(comps[0], 0, 1, 0, ms[0])),
Math.round(mapRange(comps[1], 0, 1, 0, ms[1])),
Math.round(mapRange(comps[2], 0, 1, 0, ms[2])),
comps[3],
], cf.mode);
}
function convertIntToFloat(ci) {
const comps = ci.getComponents();
const ms = getColorMaxComponents(ci.mode, 'int');
return new FloatColor([
mapRange(comps[0], 0, ms[0], 0, 1),
mapRange(comps[1], 0, ms[1], 0, 1),
mapRange(comps[2], 0, ms[2], 0, 1),
comps[3],
], ci.mode);
}
function mapColorType(c, type) {
if (c.type === type) {
return c;
}
if (isIntColor(c) && type === 'float') {
return convertIntToFloat(c);
}
if (isFloatColor(c) && type === 'int') {
return convertFloatToInt(c);
}
throw TpError.shouldNeverHappen();
}
function equalsStringColorFormat(f1, f2) {
return (f1.alpha === f2.alpha &&
f1.mode === f2.mode &&
f1.notation === f2.notation &&
f1.type === f2.type);
}
function parseCssNumberOrPercentage(text, max) {
const m = text.match(/^(.+)%$/);
if (!m) {
return Math.min(parseFloat(text), max);
}
return Math.min(parseFloat(m[1]) * 0.01 * max, max);
}
const ANGLE_TO_DEG_MAP = {
deg: (angle) => angle,
grad: (angle) => (angle * 360) / 400,
rad: (angle) => (angle * 360) / (2 * Math.PI),
turn: (angle) => angle * 360,
};
function parseCssNumberOrAngle(text) {
const m = text.match(/^([0-9.]+?)(deg|grad|rad|turn)$/);
if (!m) {
return parseFloat(text);
}
const angle = parseFloat(m[1]);
const unit = m[2];
return ANGLE_TO_DEG_MAP[unit](angle);
}
function parseFunctionalRgbColorComponents(text) {
const m = text.match(/^rgb\(\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*\)$/);
if (!m) {
return null;
}
const comps = [
parseCssNumberOrPercentage(m[1], 255),
parseCssNumberOrPercentage(m[2], 255),
parseCssNumberOrPercentage(m[3], 255),
];
if (isNaN(comps[0]) || isNaN(comps[1]) || isNaN(comps[2])) {
return null;
}
return comps;
}
function parseFunctionalRgbColor(text) {
const comps = parseFunctionalRgbColorComponents(text);
return comps ? new IntColor(comps, 'rgb') : null;
}
function parseFunctionalRgbaColorComponents(text) {
const m = text.match(/^rgba\(\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*\)$/);
if (!m) {
return null;
}
const comps = [
parseCssNumberOrPercentage(m[1], 255),
parseCssNumberOrPercentage(m[2], 255),
parseCssNumberOrPercentage(m[3], 255),
parseCssNumberOrPercentage(m[4], 1),
];
if (isNaN(comps[0]) ||
isNaN(comps[1]) ||
isNaN(comps[2]) ||
isNaN(comps[3])) {
return null;
}
return comps;
}
function parseFunctionalRgbaColor(text) {
const comps = parseFunctionalRgbaColorComponents(text);
return comps ? new IntColor(comps, 'rgb') : null;
}
function parseFunctionalHslColorComponents(text) {
const m = text.match(/^hsl\(\s*([0-9A-Fa-f.]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*\)$/);
if (!m) {
return null;
}
const comps = [
parseCssNumberOrAngle(m[1]),
parseCssNumberOrPercentage(m[2], 100),
parseCssNumberOrPercentage(m[3], 100),
];
if (isNaN(comps[0]) || isNaN(comps[1]) || isNaN(comps[2])) {
return null;
}
return comps;
}
function parseFunctionalHslColor(text) {
const comps = parseFunctionalHslColorComponents(text);
return comps ? new IntColor(comps, 'hsl') : null;
}
function parseHslaColorComponents(text) {
const m = text.match(/^hsla\(\s*([0-9A-Fa-f.]+(?:deg|grad|rad|turn)?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*,\s*([0-9A-Fa-f.]+%?)\s*\)$/);
if (!m) {
return null;
}
const comps = [
parseCssNumberOrAngle(m[1]),
parseCssNumberOrPercentage(m[2], 100),
parseCssNumberOrPercentage(m[3], 100),
parseCssNumberOrPercentage(m[4], 1),
];
if (isNaN(comps[0]) ||
isNaN(comps[1]) ||
isNaN(comps[2]) ||
isNaN(comps[3])) {
return null;
}
return comps;
}
function parseFunctionalHslaColor(text) {
const comps = parseHslaColorComponents(text);
return comps ? new IntColor(comps, 'hsl') : null;
}
function parseHexRgbColorComponents(text) {
const mRgb = text.match(/^#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$/);
if (mRgb) {
return [
parseInt(mRgb[1] + mRgb[1], 16),
parseInt(mRgb[2] + mRgb[2], 16),
parseInt(mRgb[3] + mRgb[3], 16),
];
}
const mRrggbb = text.match(/^(?:#|0x)([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/);
if (mRrggbb) {
return [
parseInt(mRrggbb[1], 16),
parseInt(mRrggbb[2], 16),
parseInt(mRrggbb[3], 16),
];
}
return null;
}
function parseHexRgbColor(text) {
const comps = parseHexRgbColorComponents(text);
return comps ? new IntColor(comps, 'rgb') : null;
}
function parseHexRgbaColorComponents(text) {
const mRgb = text.match(/^#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$/);
if (mRgb) {
return [
parseInt(mRgb[1] + mRgb[1], 16),
parseInt(mRgb[2] + mRgb[2], 16),
parseInt(mRgb[3] + mRgb[3], 16),
mapRange(parseInt(mRgb[4] + mRgb[4], 16), 0, 255, 0, 1),
];
}
const mRrggbb = text.match(/^(?:#|0x)?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/);
if (mRrggbb) {
return [
parseInt(mRrggbb[1], 16),
parseInt(mRrggbb[2], 16),
parseInt(mRrggbb[3], 16),
mapRange(parseInt(mRrggbb[4], 16), 0, 255, 0, 1),
];
}
return null;
}
function parseHexRgbaColor(text) {
const comps = parseHexRgbaColorComponents(text);
return comps ? new IntColor(comps, 'rgb') : null;
}
function parseObjectRgbColorComponents(text) {
const m = text.match(/^\{\s*r\s*:\s*([0-9A-Fa-f.]+%?)\s*,\s*g\s*:\s*([0-9A-Fa-f.]+%?)\s*,\s*b\s*:\s*([0-9A-Fa-f.]+%?)\s*\}$/);
if (!m) {
return null;
}
const comps = [
parseFloat(m[1]),
parseFloat(m[2]),
parseFloat(m[3]),
];
if (isNaN(comps[0]) || isNaN(comps[1]) || isNaN(comps[2])) {
return null;
}
return comps;
}
function createObjectRgbColorParser(type) {
return (text) => {
const comps = parseObjectRgbColorComponents(text);
return comps ? createColor(comps, 'rgb', type) : null;
};
}
function parseObjectRgbaColorComponents(text) {
const m = text.match(/^\{\s*r\s*:\s*([0-9A-Fa-f.]+%?)\s*,\s*g\s*:\s*([0-9A-Fa-f.]+%?)\s*,\s*b\s*:\s*([0-9A-Fa-f.]+%?)\s*,\s*a\s*:\s*([0-9A-Fa-f.]+%?)\s*\}$/);
if (!m) {
return null;
}
const comps = [
parseFloat(m[1]),
parseFloat(m[2]),
parseFloat(m[3]),
parseFloat(m[4]),
];
if (isNaN(comps[0]) ||
isNaN(comps[1]) ||
isNaN(comps[2]) ||
isNaN(comps[3])) {
return null;
}
return comps;
}
function createObjectRgbaColorParser(type) {
return (text) => {
const comps = parseObjectRgbaColorComponents(text);
return comps ? createColor(comps, 'rgb', type) : null;
};
}
const PARSER_AND_RESULT = [
{
parser: parseHexRgbColorComponents,
result: {
alpha: false,
mode: 'rgb',
notation: 'hex',
},
},
{
parser: parseHexRgbaColorComponents,
result: {
alpha: true,
mode: 'rgb',
notation: 'hex',
},
},
{
parser: parseFunctionalRgbColorComponents,
result: {
alpha: false,
mode: 'rgb',
notation: 'func',
},
},
{
parser: parseFunctionalRgbaColorComponents,
result: {
alpha: true,
mode: 'rgb',
notation: 'func',
},
},
{
parser: parseFunctionalHslColorComponents,
result: {
alpha: false,
mode: 'hsl',
notation: 'func',
},
},
{
parser: parseHslaColorComponents,
result: {
alpha: true,
mode: 'hsl',
notation: 'func',
},
},
{
parser: parseObjectRgbColorComponents,
result: {
alpha: false,
mode: 'rgb',
notation: 'object',
},
},
{
parser: parseObjectRgbaColorComponents,
result: {
alpha: true,
mode: 'rgb',
notation: 'object',
},
},
];
function detectStringColor(text) {
return PARSER_AND_RESULT.reduce((prev, { parser, result: detection }) => {
if (prev) {
return prev;
}
return parser(text) ? detection : null;
}, null);
}
function detectStringColorFormat(text, type = 'int') {
const r = detectStringColor(text);
if (!r) {
return null;
}
if (r.notation === 'hex' && type !== 'float') {
return Object.assign(Object.assign({}, r), { type: 'int' });
}
if (r.notation === 'func') {
return Object.assign(Object.assign({}, r), { type: type });
}
return null;
}
function createColorStringParser(type) {
const parsers = [
parseHexRgbColor,
parseHexRgbaColor,
parseFunctionalRgbColor,
parseFunctionalRgbaColor,
parseFunctionalHslColor,
parseFunctionalHslaColor,
];
if (type === 'int') {
parsers.push(createObjectRgbColorParser('int'), createObjectRgbaColorParser('int'));
}
if (type === 'float') {
parsers.push(createObjectRgbColorParser('float'), createObjectRgbaColorParser('float'));
}
const parser = composeParsers(parsers);
return (text) => {
const result = parser(text);
return result ? mapColorType(result, type) : null;
};
}
function readIntColorString(value) {
const parser = createColorStringParser('int');
if (typeof value !== 'string') {
return IntColor.black();
}
const result = parser(value);
return result !== null && result !== void 0 ? result : IntColor.black();
}
function zerofill(comp) {
const hex = constrainRange(Math.floor(comp), 0, 255).toString(16);
return hex.length === 1 ? `0${hex}` : hex;
}
function colorToHexRgbString(value, prefix = '#') {
const hexes = removeAlphaComponent(value.getComponents('rgb'))
.map(zerofill)
.join('');
return `${prefix}${hexes}`;
}
function colorToHexRgbaString(value, prefix = '#') {
const rgbaComps = value.getComponents('rgb');
const hexes = [rgbaComps[0], rgbaComps[1], rgbaComps[2], rgbaComps[3] * 255]
.map(zerofill)
.join('');
return `${prefix}${hexes}`;
}
function colorToFunctionalRgbString(value) {
const formatter = createNumberFormatter(0);
const ci = mapColorType(value, 'int');
const comps = removeAlphaComponent(ci.getComponents('rgb')).map((comp) => formatter(comp));
return `rgb(${comps.join(', ')})`;
}
function colorToFunctionalRgbaString(value) {
const aFormatter = createNumberFormatter(2);
const rgbFormatter = createNumberFormatter(0);
const ci = mapColorType(value, 'int');
const comps = ci.getComponents('rgb').map((comp, index) => {
const formatter = index === 3 ? aFormatter : rgbFormatter;
return formatter(comp);
});
return `rgba(${comps.join(', ')})`;
}
function colorToFunctionalHslString(value) {
const formatters = [
createNumberFormatter(0),
formatPercentage,
formatPercentage,
];
const ci = mapColorType(value, 'int');
const comps = removeAlphaComponent(ci.getComponents('hsl')).map((comp, index) => formatters[index](comp));
return `hsl(${comps.join(', ')})`;
}
function colorToFunctionalHslaString(value) {
const formatters = [
createNumberFormatter(0),
formatPercentage,
formatPercentage,
createNumberFormatter(2),
];
const ci = mapColorType(value, 'int');
const comps = ci
.getComponents('hsl')
.map((comp, index) => formatters[index](comp));
return `hsla(${comps.join(', ')})`;
}
function colorToObjectRgbString(value, type) {
const formatter = createNumberFormatter(type === 'float' ? 2 : 0);
const names = ['r', 'g', 'b'];
const cc = mapColorType(value, type);
const comps = removeAlphaComponent(cc.getComponents('rgb')).map((comp, index) => `${names[index]}: ${formatter(comp)}`);
return `{${comps.join(', ')}}`;
}
function createObjectRgbColorFormatter(type) {
return (value) => colorToObjectRgbString(value, type);
}
function colorToObjectRgbaString(value, type) {
const aFormatter = createNumberFormatter(2);
const rgbFormatter = createNumberFormatter(type === 'float' ? 2 : 0);
const names = ['r', 'g', 'b', 'a'];
const cc = mapColorType(value, type);
const comps = cc.getComponents('rgb').map((comp, index) => {
const formatter = index === 3 ? aFormatter : rgbFormatter;
return `${names[index]}: ${formatter(comp)}`;
});
return `{${comps.join(', ')}}`;
}
function createObjectRgbaColorFormatter(type) {
return (value) => colorToObjectRgbaString(value, type);
}
const FORMAT_AND_STRINGIFIERS = [
{
format: {
alpha: false,
mode: 'rgb',
notation: 'hex',
type: 'int',
},
stringifier: colorToHexRgbString,
},
{
format: {
alpha: true,
mode: 'rgb',
notation: 'hex',
type: 'int',
},
stringifier: colorToHexRgbaString,
},
{
format: {
alpha: false,
mode: 'rgb',
notation: 'func',
type: 'int',
},
stringifier: colorToFunctionalRgbString,
},
{
format: {
alpha: true,
mode: 'rgb',
notation: 'func',
type: 'int',
},
stringifier: colorToFunctionalRgbaString,
},
{
format: {
alpha: false,
mode: 'hsl',
notation: 'func',
type: 'int',
},
stringifier: colorToFunctionalHslString,
},
{
format: {
alpha: true,
mode: 'hsl',
notation: 'func',
type: 'int',
},
stringifier: colorToFunctionalHslaString,
},
...['int', 'float'].reduce((prev, type) => {
return [
...prev,
{
format: {
alpha: false,
mode: 'rgb',
notation: 'object',
type: type,
},
stringifier: createObjectRgbColorFormatter(type),
},
{
format: {
alpha: true,
mode: 'rgb',
notation: 'object',
type: type,
},
stringifier: createObjectRgbaColorFormatter(type),
},
];
}, []),
];
function findColorStringifier(format) {
return FORMAT_AND_STRINGIFIERS.reduce((prev, fas) => {
if (prev) {
return prev;
}
return equalsStringColorFormat(fas.format, format)
? fas.stringifier
: null;
}, null);
}
const cn$b = ClassName('apl');
class APaletteView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
this.value = config.value;
this.value.emitter.on('change', this.onValueChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$b());
config.viewProps.bindClassModifiers(this.element);
config.viewProps.bindTabIndex(this.element);
const barElem = doc.createElement('div');
barElem.classList.add(cn$b('b'));
this.element.appendChild(barElem);
const colorElem = doc.createElement('div');
colorElem.classList.add(cn$b('c'));
barElem.appendChild(colorElem);
this.colorElem_ = colorElem;
const markerElem = doc.createElement('div');
markerElem.classList.add(cn$b('m'));
this.element.appendChild(markerElem);
this.markerElem_ = markerElem;
const previewElem = doc.createElement('div');
previewElem.classList.add(cn$b('p'));
this.markerElem_.appendChild(previewElem);
this.previewElem_ = previewElem;
this.update_();
}
update_() {
const c = this.value.rawValue;
const rgbaComps = c.getComponents('rgb');
const leftColor = new IntColor([rgbaComps[0], rgbaComps[1], rgbaComps[2], 0], 'rgb');
const rightColor = new IntColor([rgbaComps[0], rgbaComps[1], rgbaComps[2], 255], 'rgb');
const gradientComps = [
'to right',
colorToFunctionalRgbaString(leftColor),
colorToFunctionalRgbaString(rightColor),
];
this.colorElem_.style.background = `linear-gradient(${gradientComps.join(',')})`;
this.previewElem_.style.backgroundColor = colorToFunctionalRgbaString(c);
const left = mapRange(rgbaComps[3], 0, 1, 0, 100);
this.markerElem_.style.left = `${left}%`;
}
onValueChange_() {
this.update_();
}
}
class APaletteController {
constructor(doc, config) {
this.onKeyDown_ = this.onKeyDown_.bind(this);
this.onKeyUp_ = this.onKeyUp_.bind(this);
this.onPointerDown_ = this.onPointerDown_.bind(this);
this.onPointerMove_ = this.onPointerMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new APaletteView(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.ptHandler_ = new PointerHandler(this.view.element);
this.ptHandler_.emitter.on('down', this.onPointerDown_);
this.ptHandler_.emitter.on('move', this.onPointerMove_);
this.ptHandler_.emitter.on('up', this.onPointerUp_);
this.view.element.addEventListener('keydown', this.onKeyDown_);
this.view.element.addEventListener('keyup', this.onKeyUp_);
}
handlePointerEvent_(d, opts) {
if (!d.point) {
return;
}
const alpha = d.point.x / d.bounds.width;
const c = this.value.rawValue;
const [h, s, v] = c.getComponents('hsv');
this.value.setRawValue(new IntColor([h, s, v, alpha], 'hsv'), opts);
}
onPointerDown_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerMove_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerUp_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: true,
last: true,
});
}
onKeyDown_(ev) {
const step = getStepForKey(getKeyScaleForColor(true), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
const c = this.value.rawValue;
const [h, s, v, a] = c.getComponents('hsv');
this.value.setRawValue(new IntColor([h, s, v, a + step], 'hsv'), {
forceEmit: false,
last: false,
});
}
onKeyUp_(ev) {
const step = getStepForKey(getKeyScaleForColor(true), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
}
const cn$a = ClassName('coltxt');
function createModeSelectElement(doc) {
const selectElem = doc.createElement('select');
const items = [
{ text: 'RGB', value: 'rgb' },
{ text: 'HSL', value: 'hsl' },
{ text: 'HSV', value: 'hsv' },
{ text: 'HEX', value: 'hex' },
];
selectElem.appendChild(items.reduce((frag, item) => {
const optElem = doc.createElement('option');
optElem.textContent = item.text;
optElem.value = item.value;
frag.appendChild(optElem);
return frag;
}, doc.createDocumentFragment()));
return selectElem;
}
class ColorTextsView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$a());
config.viewProps.bindClassModifiers(this.element);
const modeElem = doc.createElement('div');
modeElem.classList.add(cn$a('m'));
this.modeElem_ = createModeSelectElement(doc);
this.modeElem_.classList.add(cn$a('ms'));
modeElem.appendChild(this.modeSelectElement);
config.viewProps.bindDisabled(this.modeElem_);
const modeMarkerElem = doc.createElement('div');
modeMarkerElem.classList.add(cn$a('mm'));
modeMarkerElem.appendChild(createSvgIconElement(doc, 'dropdown'));
modeElem.appendChild(modeMarkerElem);
this.element.appendChild(modeElem);
const inputsElem = doc.createElement('div');
inputsElem.classList.add(cn$a('w'));
this.element.appendChild(inputsElem);
this.inputsElem_ = inputsElem;
this.inputViews_ = config.inputViews;
this.applyInputViews_();
bindValue(config.mode, (mode) => {
this.modeElem_.value = mode;
});
}
get modeSelectElement() {
return this.modeElem_;
}
get inputViews() {
return this.inputViews_;
}
set inputViews(inputViews) {
this.inputViews_ = inputViews;
this.applyInputViews_();
}
applyInputViews_() {
removeChildElements(this.inputsElem_);
const doc = this.element.ownerDocument;
this.inputViews_.forEach((v) => {
const compElem = doc.createElement('div');
compElem.classList.add(cn$a('c'));
compElem.appendChild(v.element);
this.inputsElem_.appendChild(compElem);
});
}
}
function createFormatter$2(type) {
return createNumberFormatter(type === 'float' ? 2 : 0);
}
function createConstraint$5(mode, type, index) {
const max = getColorMaxComponents(mode, type)[index];
return new DefiniteRangeConstraint({
min: 0,
max: max,
});
}
function createComponentController(doc, config, index) {
return new NumberTextController(doc, {
arrayPosition: index === 0 ? 'fst' : index === 3 - 1 ? 'lst' : 'mid',
parser: config.parser,
props: ValueMap.fromObject({
formatter: createFormatter$2(config.colorType),
keyScale: getKeyScaleForColor(false),
pointerScale: config.colorType === 'float' ? 0.01 : 1,
}),
value: createValue(0, {
constraint: createConstraint$5(config.colorMode, config.colorType, index),
}),
viewProps: config.viewProps,
});
}
function createComponentControllers(doc, config) {
const cc = {
colorMode: config.colorMode,
colorType: config.colorType,
parser: parseNumber,
viewProps: config.viewProps,
};
return [0, 1, 2].map((i) => {
const c = createComponentController(doc, cc, i);
connectValues({
primary: config.value,
secondary: c.value,
forward(p) {
const mc = mapColorType(p, config.colorType);
return mc.getComponents(config.colorMode)[i];
},
backward(p, s) {
const pickedMode = config.colorMode;
const mc = mapColorType(p, config.colorType);
const comps = mc.getComponents(pickedMode);
comps[i] = s;
const c = createColor(appendAlphaComponent(removeAlphaComponent(comps), comps[3]), pickedMode, config.colorType);
return mapColorType(c, 'int');
},
});
return c;
});
}
function createHexController(doc, config) {
const c = new TextController(doc, {
parser: createColorStringParser('int'),
props: ValueMap.fromObject({
formatter: colorToHexRgbString,
}),
value: createValue(IntColor.black()),
viewProps: config.viewProps,
});
connectValues({
primary: config.value,
secondary: c.value,
forward: (p) => new IntColor(removeAlphaComponent(p.getComponents()), p.mode),
backward: (p, s) => new IntColor(appendAlphaComponent(removeAlphaComponent(s.getComponents(p.mode)), p.getComponents()[3]), p.mode),
});
return [c];
}
function isColorMode(mode) {
return mode !== 'hex';
}
class ColorTextsController {
constructor(doc, config) {
this.onModeSelectChange_ = this.onModeSelectChange_.bind(this);
this.colorType_ = config.colorType;
this.value = config.value;
this.viewProps = config.viewProps;
this.colorMode = createValue(this.value.rawValue.mode);
this.ccs_ = this.createComponentControllers_(doc);
this.view = new ColorTextsView(doc, {
mode: this.colorMode,
inputViews: [this.ccs_[0].view, this.ccs_[1].view, this.ccs_[2].view],
viewProps: this.viewProps,
});
this.view.modeSelectElement.addEventListener('change', this.onModeSelectChange_);
}
createComponentControllers_(doc) {
const mode = this.colorMode.rawValue;
if (isColorMode(mode)) {
return createComponentControllers(doc, {
colorMode: mode,
colorType: this.colorType_,
value: this.value,
viewProps: this.viewProps,
});
}
return createHexController(doc, {
value: this.value,
viewProps: this.viewProps,
});
}
onModeSelectChange_(ev) {
const selectElem = ev.currentTarget;
this.colorMode.rawValue = selectElem.value;
this.ccs_ = this.createComponentControllers_(this.view.element.ownerDocument);
this.view.inputViews = this.ccs_.map((cc) => cc.view);
}
}
const cn$9 = ClassName('hpl');
class HPaletteView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
this.value = config.value;
this.value.emitter.on('change', this.onValueChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$9());
config.viewProps.bindClassModifiers(this.element);
config.viewProps.bindTabIndex(this.element);
const colorElem = doc.createElement('div');
colorElem.classList.add(cn$9('c'));
this.element.appendChild(colorElem);
const markerElem = doc.createElement('div');
markerElem.classList.add(cn$9('m'));
this.element.appendChild(markerElem);
this.markerElem_ = markerElem;
this.update_();
}
update_() {
const c = this.value.rawValue;
const [h] = c.getComponents('hsv');
this.markerElem_.style.backgroundColor = colorToFunctionalRgbString(new IntColor([h, 100, 100], 'hsv'));
const left = mapRange(h, 0, 360, 0, 100);
this.markerElem_.style.left = `${left}%`;
}
onValueChange_() {
this.update_();
}
}
class HPaletteController {
constructor(doc, config) {
this.onKeyDown_ = this.onKeyDown_.bind(this);
this.onKeyUp_ = this.onKeyUp_.bind(this);
this.onPointerDown_ = this.onPointerDown_.bind(this);
this.onPointerMove_ = this.onPointerMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new HPaletteView(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.ptHandler_ = new PointerHandler(this.view.element);
this.ptHandler_.emitter.on('down', this.onPointerDown_);
this.ptHandler_.emitter.on('move', this.onPointerMove_);
this.ptHandler_.emitter.on('up', this.onPointerUp_);
this.view.element.addEventListener('keydown', this.onKeyDown_);
this.view.element.addEventListener('keyup', this.onKeyUp_);
}
handlePointerEvent_(d, opts) {
if (!d.point) {
return;
}
const hue = mapRange(constrainRange(d.point.x, 0, d.bounds.width), 0, d.bounds.width, 0, 360);
const c = this.value.rawValue;
const [, s, v, a] = c.getComponents('hsv');
this.value.setRawValue(new IntColor([hue, s, v, a], 'hsv'), opts);
}
onPointerDown_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerMove_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerUp_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: true,
last: true,
});
}
onKeyDown_(ev) {
const step = getStepForKey(getKeyScaleForColor(false), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
const c = this.value.rawValue;
const [h, s, v, a] = c.getComponents('hsv');
this.value.setRawValue(new IntColor([h + step, s, v, a], 'hsv'), {
forceEmit: false,
last: false,
});
}
onKeyUp_(ev) {
const step = getStepForKey(getKeyScaleForColor(false), getHorizontalStepKeys(ev));
if (step === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
}
const cn$8 = ClassName('svp');
const CANVAS_RESOL = 64;
class SvPaletteView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
this.value = config.value;
this.value.emitter.on('change', this.onValueChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$8());
config.viewProps.bindClassModifiers(this.element);
config.viewProps.bindTabIndex(this.element);
const canvasElem = doc.createElement('canvas');
canvasElem.height = CANVAS_RESOL;
canvasElem.width = CANVAS_RESOL;
canvasElem.classList.add(cn$8('c'));
this.element.appendChild(canvasElem);
this.canvasElement = canvasElem;
const markerElem = doc.createElement('div');
markerElem.classList.add(cn$8('m'));
this.element.appendChild(markerElem);
this.markerElem_ = markerElem;
this.update_();
}
update_() {
const ctx = getCanvasContext(this.canvasElement);
if (!ctx) {
return;
}
const c = this.value.rawValue;
const hsvComps = c.getComponents('hsv');
const width = this.canvasElement.width;
const height = this.canvasElement.height;
const imgData = ctx.getImageData(0, 0, width, height);
const data = imgData.data;
for (let iy = 0; iy < height; iy++) {
for (let ix = 0; ix < width; ix++) {
const s = mapRange(ix, 0, width, 0, 100);
const v = mapRange(iy, 0, height, 100, 0);
const rgbComps = hsvToRgbInt(hsvComps[0], s, v);
const i = (iy * width + ix) * 4;
data[i] = rgbComps[0];
data[i + 1] = rgbComps[1];
data[i + 2] = rgbComps[2];
data[i + 3] = 255;
}
}
ctx.putImageData(imgData, 0, 0);
const left = mapRange(hsvComps[1], 0, 100, 0, 100);
this.markerElem_.style.left = `${left}%`;
const top = mapRange(hsvComps[2], 0, 100, 100, 0);
this.markerElem_.style.top = `${top}%`;
}
onValueChange_() {
this.update_();
}
}
class SvPaletteController {
constructor(doc, config) {
this.onKeyDown_ = this.onKeyDown_.bind(this);
this.onKeyUp_ = this.onKeyUp_.bind(this);
this.onPointerDown_ = this.onPointerDown_.bind(this);
this.onPointerMove_ = this.onPointerMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new SvPaletteView(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.ptHandler_ = new PointerHandler(this.view.element);
this.ptHandler_.emitter.on('down', this.onPointerDown_);
this.ptHandler_.emitter.on('move', this.onPointerMove_);
this.ptHandler_.emitter.on('up', this.onPointerUp_);
this.view.element.addEventListener('keydown', this.onKeyDown_);
this.view.element.addEventListener('keyup', this.onKeyUp_);
}
handlePointerEvent_(d, opts) {
if (!d.point) {
return;
}
const saturation = mapRange(d.point.x, 0, d.bounds.width, 0, 100);
const value = mapRange(d.point.y, 0, d.bounds.height, 100, 0);
const [h, , , a] = this.value.rawValue.getComponents('hsv');
this.value.setRawValue(new IntColor([h, saturation, value, a], 'hsv'), opts);
}
onPointerDown_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerMove_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerUp_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: true,
last: true,
});
}
onKeyDown_(ev) {
if (isArrowKey(ev.key)) {
ev.preventDefault();
}
const [h, s, v, a] = this.value.rawValue.getComponents('hsv');
const keyScale = getKeyScaleForColor(false);
const ds = getStepForKey(keyScale, getHorizontalStepKeys(ev));
const dv = getStepForKey(keyScale, getVerticalStepKeys(ev));
if (ds === 0 && dv === 0) {
return;
}
this.value.setRawValue(new IntColor([h, s + ds, v + dv, a], 'hsv'), {
forceEmit: false,
last: false,
});
}
onKeyUp_(ev) {
const keyScale = getKeyScaleForColor(false);
const ds = getStepForKey(keyScale, getHorizontalStepKeys(ev));
const dv = getStepForKey(keyScale, getVerticalStepKeys(ev));
if (ds === 0 && dv === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
}
class ColorPickerController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.hPaletteC_ = new HPaletteController(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.svPaletteC_ = new SvPaletteController(doc, {
value: this.value,
viewProps: this.viewProps,
});
this.alphaIcs_ = config.supportsAlpha
? {
palette: new APaletteController(doc, {
value: this.value,
viewProps: this.viewProps,
}),
text: new NumberTextController(doc, {
parser: parseNumber,
props: ValueMap.fromObject({
pointerScale: 0.01,
keyScale: 0.1,
formatter: createNumberFormatter(2),
}),
value: createValue(0, {
constraint: new DefiniteRangeConstraint({ min: 0, max: 1 }),
}),
viewProps: this.viewProps,
}),
}
: null;
if (this.alphaIcs_) {
connectValues({
primary: this.value,
secondary: this.alphaIcs_.text.value,
forward: (p) => p.getComponents()[3],
backward: (p, s) => {
const comps = p.getComponents();
comps[3] = s;
return new IntColor(comps, p.mode);
},
});
}
this.textsC_ = new ColorTextsController(doc, {
colorType: config.colorType,
value: this.value,
viewProps: this.viewProps,
});
this.view = new ColorPickerView(doc, {
alphaViews: this.alphaIcs_
? {
palette: this.alphaIcs_.palette.view,
text: this.alphaIcs_.text.view,
}
: null,
hPaletteView: this.hPaletteC_.view,
supportsAlpha: config.supportsAlpha,
svPaletteView: this.svPaletteC_.view,
textsView: this.textsC_.view,
viewProps: this.viewProps,
});
}
get textsController() {
return this.textsC_;
}
}
const cn$7 = ClassName('colsw');
class ColorSwatchView {
constructor(doc, config) {
this.onValueChange_ = this.onValueChange_.bind(this);
config.value.emitter.on('change', this.onValueChange_);
this.value = config.value;
this.element = doc.createElement('div');
this.element.classList.add(cn$7());
config.viewProps.bindClassModifiers(this.element);
const swatchElem = doc.createElement('div');
swatchElem.classList.add(cn$7('sw'));
this.element.appendChild(swatchElem);
this.swatchElem_ = swatchElem;
const buttonElem = doc.createElement('button');
buttonElem.classList.add(cn$7('b'));
config.viewProps.bindDisabled(buttonElem);
this.element.appendChild(buttonElem);
this.buttonElement = buttonElem;
this.update_();
}
update_() {
const value = this.value.rawValue;
this.swatchElem_.style.backgroundColor = colorToHexRgbaString(value);
}
onValueChange_() {
this.update_();
}
}
class ColorSwatchController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new ColorSwatchView(doc, {
value: this.value,
viewProps: this.viewProps,
});
}
}
class ColorController {
constructor(doc, config) {
this.onButtonBlur_ = this.onButtonBlur_.bind(this);
this.onButtonClick_ = this.onButtonClick_.bind(this);
this.onPopupChildBlur_ = this.onPopupChildBlur_.bind(this);
this.onPopupChildKeydown_ = this.onPopupChildKeydown_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.foldable_ = Foldable.create(config.expanded);
this.swatchC_ = new ColorSwatchController(doc, {
value: this.value,
viewProps: this.viewProps,
});
const buttonElem = this.swatchC_.view.buttonElement;
buttonElem.addEventListener('blur', this.onButtonBlur_);
buttonElem.addEventListener('click', this.onButtonClick_);
this.textC_ = new TextController(doc, {
parser: config.parser,
props: ValueMap.fromObject({
formatter: config.formatter,
}),
value: this.value,
viewProps: this.viewProps,
});
this.view = new ColorView(doc, {
foldable: this.foldable_,
pickerLayout: config.pickerLayout,
});
this.view.swatchElement.appendChild(this.swatchC_.view.element);
this.view.textElement.appendChild(this.textC_.view.element);
this.popC_ =
config.pickerLayout === 'popup'
? new PopupController(doc, {
viewProps: this.viewProps,
})
: null;
const pickerC = new ColorPickerController(doc, {
colorType: config.colorType,
supportsAlpha: config.supportsAlpha,
value: this.value,
viewProps: this.viewProps,
});
pickerC.view.allFocusableElements.forEach((elem) => {
elem.addEventListener('blur', this.onPopupChildBlur_);
elem.addEventListener('keydown', this.onPopupChildKeydown_);
});
this.pickerC_ = pickerC;
if (this.popC_) {
this.view.element.appendChild(this.popC_.view.element);
this.popC_.view.element.appendChild(pickerC.view.element);
connectValues({
primary: this.foldable_.value('expanded'),
secondary: this.popC_.shows,
forward: (p) => p,
backward: (_, s) => s,
});
}
else if (this.view.pickerElement) {
this.view.pickerElement.appendChild(this.pickerC_.view.element);
bindFoldable(this.foldable_, this.view.pickerElement);
}
}
get textController() {
return this.textC_;
}
onButtonBlur_(e) {
if (!this.popC_) {
return;
}
const elem = this.view.element;
const nextTarget = forceCast(e.relatedTarget);
if (!nextTarget || !elem.contains(nextTarget)) {
this.popC_.shows.rawValue = false;
}
}
onButtonClick_() {
this.foldable_.set('expanded', !this.foldable_.get('expanded'));
if (this.foldable_.get('expanded')) {
this.pickerC_.view.allFocusableElements[0].focus();
}
}
onPopupChildBlur_(ev) {
if (!this.popC_) {
return;
}
const elem = this.popC_.view.element;
const nextTarget = findNextTarget(ev);
if (nextTarget && elem.contains(nextTarget)) {
return;
}
if (nextTarget &&
nextTarget === this.swatchC_.view.buttonElement &&
!supportsTouch(elem.ownerDocument)) {
return;
}
this.popC_.shows.rawValue = false;
}
onPopupChildKeydown_(ev) {
if (this.popC_) {
if (ev.key === 'Escape') {
this.popC_.shows.rawValue = false;
}
}
else if (this.view.pickerElement) {
if (ev.key === 'Escape') {
this.swatchC_.view.buttonElement.focus();
}
}
}
}
function colorToRgbNumber(value) {
return removeAlphaComponent(value.getComponents('rgb')).reduce((result, comp) => {
return (result << 8) | (Math.floor(comp) & 0xff);
}, 0);
}
function colorToRgbaNumber(value) {
return (value.getComponents('rgb').reduce((result, comp, index) => {
const hex = Math.floor(index === 3 ? comp * 255 : comp) & 0xff;
return (result << 8) | hex;
}, 0) >>> 0);
}
function numberToRgbColor(num) {
return new IntColor([(num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff], 'rgb');
}
function numberToRgbaColor(num) {
return new IntColor([
(num >> 24) & 0xff,
(num >> 16) & 0xff,
(num >> 8) & 0xff,
mapRange(num & 0xff, 0, 255, 0, 1),
], 'rgb');
}
function colorFromRgbNumber(value) {
if (typeof value !== 'number') {
return IntColor.black();
}
return numberToRgbColor(value);
}
function colorFromRgbaNumber(value) {
if (typeof value !== 'number') {
return IntColor.black();
}
return numberToRgbaColor(value);
}
function isRgbColorComponent(obj, key) {
if (typeof obj !== 'object' || isEmpty(obj)) {
return false;
}
return key in obj && typeof obj[key] === 'number';
}
function isRgbColorObject(obj) {
return (isRgbColorComponent(obj, 'r') &&
isRgbColorComponent(obj, 'g') &&
isRgbColorComponent(obj, 'b'));
}
function isRgbaColorObject(obj) {
return isRgbColorObject(obj) && isRgbColorComponent(obj, 'a');
}
function isColorObject(obj) {
return isRgbColorObject(obj);
}
function equalsColor(v1, v2) {
if (v1.mode !== v2.mode) {
return false;
}
if (v1.type !== v2.type) {
return false;
}
const comps1 = v1.getComponents();
const comps2 = v2.getComponents();
for (let i = 0; i < comps1.length; i++) {
if (comps1[i] !== comps2[i]) {
return false;
}
}
return true;
}
function createColorComponentsFromRgbObject(obj) {
return 'a' in obj ? [obj.r, obj.g, obj.b, obj.a] : [obj.r, obj.g, obj.b];
}
function createColorStringWriter(format) {
const stringify = findColorStringifier(format);
return stringify
? (target, value) => {
writePrimitive(target, stringify(value));
}
: null;
}
function createColorNumberWriter(supportsAlpha) {
const colorToNumber = supportsAlpha ? colorToRgbaNumber : colorToRgbNumber;
return (target, value) => {
writePrimitive(target, colorToNumber(value));
};
}
function writeRgbaColorObject(target, value, type) {
const cc = mapColorType(value, type);
const obj = cc.toRgbaObject();
target.writeProperty('r', obj.r);
target.writeProperty('g', obj.g);
target.writeProperty('b', obj.b);
target.writeProperty('a', obj.a);
}
function writeRgbColorObject(target, value, type) {
const cc = mapColorType(value, type);
const obj = cc.toRgbaObject();
target.writeProperty('r', obj.r);
target.writeProperty('g', obj.g);
target.writeProperty('b', obj.b);
}
function createColorObjectWriter(supportsAlpha, type) {
return (target, inValue) => {
if (supportsAlpha) {
writeRgbaColorObject(target, inValue, type);
}
else {
writeRgbColorObject(target, inValue, type);
}
};
}
function shouldSupportAlpha$1(inputParams) {
var _a;
if ((_a = inputParams === null || inputParams === void 0 ? void 0 : inputParams.color) === null || _a === void 0 ? void 0 : _a.alpha) {
return true;
}
return false;
}
function createFormatter$1(supportsAlpha) {
return supportsAlpha
? (v) => colorToHexRgbaString(v, '0x')
: (v) => colorToHexRgbString(v, '0x');
}
function isForColor(params) {
if ('color' in params) {
return true;
}
if (params.view === 'color') {
return true;
}
return false;
}
const NumberColorInputPlugin = createPlugin({
id: 'input-color-number',
type: 'input',
accept: (value, params) => {
if (typeof value !== 'number') {
return null;
}
if (!isForColor(params)) {
return null;
}
const result = parseColorInputParams(params);
return result
? {
initialValue: value,
params: Object.assign(Object.assign({}, result), { supportsAlpha: shouldSupportAlpha$1(params) }),
}
: null;
},
binding: {
reader: (args) => {
return args.params.supportsAlpha
? colorFromRgbaNumber
: colorFromRgbNumber;
},
equals: equalsColor,
writer: (args) => {
return createColorNumberWriter(args.params.supportsAlpha);
},
},
controller: (args) => {
var _a, _b;
return new ColorController(args.document, {
colorType: 'int',
expanded: (_a = args.params.expanded) !== null && _a !== void 0 ? _a : false,
formatter: createFormatter$1(args.params.supportsAlpha),
parser: createColorStringParser('int'),
pickerLayout: (_b = args.params.picker) !== null && _b !== void 0 ? _b : 'popup',
supportsAlpha: args.params.supportsAlpha,
value: args.value,
viewProps: args.viewProps,
});
},
});
function colorFromObject(value, type) {
if (!isColorObject(value)) {
return mapColorType(IntColor.black(), type);
}
if (type === 'int') {
const comps = createColorComponentsFromRgbObject(value);
return new IntColor(comps, 'rgb');
}
if (type === 'float') {
const comps = createColorComponentsFromRgbObject(value);
return new FloatColor(comps, 'rgb');
}
return mapColorType(IntColor.black(), 'int');
}
function shouldSupportAlpha(initialValue) {
return isRgbaColorObject(initialValue);
}
function createColorObjectBindingReader(type) {
return (value) => {
const c = colorFromObject(value, type);
return mapColorType(c, 'int');
};
}
function createColorObjectFormatter(supportsAlpha, type) {
return (value) => {
if (supportsAlpha) {
return colorToObjectRgbaString(value, type);
}
return colorToObjectRgbString(value, type);
};
}
const ObjectColorInputPlugin = createPlugin({
id: 'input-color-object',
type: 'input',
accept: (value, params) => {
var _a;
if (!isColorObject(value)) {
return null;
}
const result = parseColorInputParams(params);
return result
? {
initialValue: value,
params: Object.assign(Object.assign({}, result), { colorType: (_a = extractColorType(params)) !== null && _a !== void 0 ? _a : 'int' }),
}
: null;
},
binding: {
reader: (args) => createColorObjectBindingReader(args.params.colorType),
equals: equalsColor,
writer: (args) => createColorObjectWriter(shouldSupportAlpha(args.initialValue), args.params.colorType),
},
controller: (args) => {
var _a, _b;
const supportsAlpha = isRgbaColorObject(args.initialValue);
return new ColorController(args.document, {
colorType: args.params.colorType,
expanded: (_a = args.params.expanded) !== null && _a !== void 0 ? _a : false,
formatter: createColorObjectFormatter(supportsAlpha, args.params.colorType),
parser: createColorStringParser('int'),
pickerLayout: (_b = args.params.picker) !== null && _b !== void 0 ? _b : 'popup',
supportsAlpha: supportsAlpha,
value: args.value,
viewProps: args.viewProps,
});
},
});
const StringColorInputPlugin = createPlugin({
id: 'input-color-string',
type: 'input',
accept: (value, params) => {
if (typeof value !== 'string') {
return null;
}
if (params.view === 'text') {
return null;
}
const format = detectStringColorFormat(value, extractColorType(params));
if (!format) {
return null;
}
const stringifier = findColorStringifier(format);
if (!stringifier) {
return null;
}
const result = parseColorInputParams(params);
return result
? {
initialValue: value,
params: Object.assign(Object.assign({}, result), { format: format, stringifier: stringifier }),
}
: null;
},
binding: {
reader: () => readIntColorString,
equals: equalsColor,
writer: (args) => {
const writer = createColorStringWriter(args.params.format);
if (!writer) {
throw TpError.notBindable();
}
return writer;
},
},
controller: (args) => {
var _a, _b;
return new ColorController(args.document, {
colorType: args.params.format.type,
expanded: (_a = args.params.expanded) !== null && _a !== void 0 ? _a : false,
formatter: args.params.stringifier,
parser: createColorStringParser('int'),
pickerLayout: (_b = args.params.picker) !== null && _b !== void 0 ? _b : 'popup',
supportsAlpha: args.params.format.alpha,
value: args.value,
viewProps: args.viewProps,
});
},
});
class PointNdConstraint {
constructor(config) {
this.components = config.components;
this.asm_ = config.assembly;
}
constrain(value) {
const comps = this.asm_
.toComponents(value)
.map((comp, index) => { var _a, _b; return (_b = (_a = this.components[index]) === null || _a === void 0 ? void 0 : _a.constrain(comp)) !== null && _b !== void 0 ? _b : comp; });
return this.asm_.fromComponents(comps);
}
}
const cn$6 = ClassName('pndtxt');
class PointNdTextView {
constructor(doc, config) {
this.textViews = config.textViews;
this.element = doc.createElement('div');
this.element.classList.add(cn$6());
this.textViews.forEach((v) => {
const axisElem = doc.createElement('div');
axisElem.classList.add(cn$6('a'));
axisElem.appendChild(v.element);
this.element.appendChild(axisElem);
});
}
}
function createAxisController(doc, config, index) {
return new NumberTextController(doc, {
arrayPosition: index === 0 ? 'fst' : index === config.axes.length - 1 ? 'lst' : 'mid',
parser: config.parser,
props: config.axes[index].textProps,
value: createValue(0, {
constraint: config.axes[index].constraint,
}),
viewProps: config.viewProps,
});
}
class PointNdTextController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.acs_ = config.axes.map((_, index) => createAxisController(doc, config, index));
this.acs_.forEach((c, index) => {
connectValues({
primary: this.value,
secondary: c.value,
forward: (p) => config.assembly.toComponents(p)[index],
backward: (p, s) => {
const comps = config.assembly.toComponents(p);
comps[index] = s;
return config.assembly.fromComponents(comps);
},
});
});
this.view = new PointNdTextView(doc, {
textViews: this.acs_.map((ac) => ac.view),
});
}
get textControllers() {
return this.acs_;
}
}
class SliderInputBindingApi extends BindingApi {
get max() {
return this.controller.valueController.sliderController.props.get('max');
}
set max(max) {
this.controller.valueController.sliderController.props.set('max', max);
}
get min() {
return this.controller.valueController.sliderController.props.get('min');
}
set min(max) {
this.controller.valueController.sliderController.props.set('min', max);
}
}
function createConstraint$4(params, initialValue) {
const constraints = [];
const sc = createStepConstraint(params, initialValue);
if (sc) {
constraints.push(sc);
}
const rc = createRangeConstraint(params);
if (rc) {
constraints.push(rc);
}
const lc = createListConstraint(params.options);
if (lc) {
constraints.push(lc);
}
return new CompositeConstraint(constraints);
}
const NumberInputPlugin = createPlugin({
id: 'input-number',
type: 'input',
accept: (value, params) => {
if (typeof value !== 'number') {
return null;
}
const result = parseRecord(params, (p) => (Object.assign(Object.assign({}, createNumberTextInputParamsParser(p)), { options: p.optional.custom(parseListOptions), readonly: p.optional.constant(false) })));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => numberFromUnknown,
constraint: (args) => createConstraint$4(args.params, args.initialValue),
writer: (_args) => writePrimitive,
},
controller: (args) => {
const value = args.value;
const c = args.constraint;
const lc = c && findConstraint(c, ListConstraint);
if (lc) {
return new ListController(args.document, {
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
});
}
const textPropsObj = createNumberTextPropsObject(args.params, value.rawValue);
const drc = c && findConstraint(c, DefiniteRangeConstraint);
if (drc) {
return new SliderTextController(args.document, Object.assign(Object.assign({}, createSliderTextProps(Object.assign(Object.assign({}, textPropsObj), { keyScale: createValue(textPropsObj.keyScale), max: drc.values.value('max'), min: drc.values.value('min') }))), { parser: parseNumber, value: value, viewProps: args.viewProps }));
}
return new NumberTextController(args.document, {
parser: parseNumber,
props: ValueMap.fromObject(textPropsObj),
value: value,
viewProps: args.viewProps,
});
},
api(args) {
if (typeof args.controller.value.rawValue !== 'number') {
return null;
}
if (args.controller.valueController instanceof SliderTextController) {
return new SliderInputBindingApi(args.controller);
}
if (args.controller.valueController instanceof ListController) {
return new ListInputBindingApi(args.controller);
}
return null;
},
});
class Point2d {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
getComponents() {
return [this.x, this.y];
}
static isObject(obj) {
if (isEmpty(obj)) {
return false;
}
const x = obj.x;
const y = obj.y;
if (typeof x !== 'number' || typeof y !== 'number') {
return false;
}
return true;
}
static equals(v1, v2) {
return v1.x === v2.x && v1.y === v2.y;
}
toObject() {
return {
x: this.x,
y: this.y,
};
}
}
const Point2dAssembly = {
toComponents: (p) => p.getComponents(),
fromComponents: (comps) => new Point2d(...comps),
};
const cn$5 = ClassName('p2d');
class Point2dView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn$5());
config.viewProps.bindClassModifiers(this.element);
bindValue(config.expanded, valueToClassName(this.element, cn$5(undefined, 'expanded')));
const headElem = doc.createElement('div');
headElem.classList.add(cn$5('h'));
this.element.appendChild(headElem);
const buttonElem = doc.createElement('button');
buttonElem.classList.add(cn$5('b'));
buttonElem.appendChild(createSvgIconElement(doc, 'p2dpad'));
config.viewProps.bindDisabled(buttonElem);
headElem.appendChild(buttonElem);
this.buttonElement = buttonElem;
const textElem = doc.createElement('div');
textElem.classList.add(cn$5('t'));
headElem.appendChild(textElem);
this.textElement = textElem;
if (config.pickerLayout === 'inline') {
const pickerElem = doc.createElement('div');
pickerElem.classList.add(cn$5('p'));
this.element.appendChild(pickerElem);
this.pickerElement = pickerElem;
}
else {
this.pickerElement = null;
}
}
}
const cn$4 = ClassName('p2dp');
class Point2dPickerView {
constructor(doc, config) {
this.onFoldableChange_ = this.onFoldableChange_.bind(this);
this.onPropsChange_ = this.onPropsChange_.bind(this);
this.onValueChange_ = this.onValueChange_.bind(this);
this.props_ = config.props;
this.props_.emitter.on('change', this.onPropsChange_);
this.element = doc.createElement('div');
this.element.classList.add(cn$4());
if (config.layout === 'popup') {
this.element.classList.add(cn$4(undefined, 'p'));
}
config.viewProps.bindClassModifiers(this.element);
const padElem = doc.createElement('div');
padElem.classList.add(cn$4('p'));
config.viewProps.bindTabIndex(padElem);
this.element.appendChild(padElem);
this.padElement = padElem;
const svgElem = doc.createElementNS(SVG_NS, 'svg');
svgElem.classList.add(cn$4('g'));
this.padElement.appendChild(svgElem);
this.svgElem_ = svgElem;
const xAxisElem = doc.createElementNS(SVG_NS, 'line');
xAxisElem.classList.add(cn$4('ax'));
xAxisElem.setAttributeNS(null, 'x1', '0');
xAxisElem.setAttributeNS(null, 'y1', '50%');
xAxisElem.setAttributeNS(null, 'x2', '100%');
xAxisElem.setAttributeNS(null, 'y2', '50%');
this.svgElem_.appendChild(xAxisElem);
const yAxisElem = doc.createElementNS(SVG_NS, 'line');
yAxisElem.classList.add(cn$4('ax'));
yAxisElem.setAttributeNS(null, 'x1', '50%');
yAxisElem.setAttributeNS(null, 'y1', '0');
yAxisElem.setAttributeNS(null, 'x2', '50%');
yAxisElem.setAttributeNS(null, 'y2', '100%');
this.svgElem_.appendChild(yAxisElem);
const lineElem = doc.createElementNS(SVG_NS, 'line');
lineElem.classList.add(cn$4('l'));
lineElem.setAttributeNS(null, 'x1', '50%');
lineElem.setAttributeNS(null, 'y1', '50%');
this.svgElem_.appendChild(lineElem);
this.lineElem_ = lineElem;
const markerElem = doc.createElement('div');
markerElem.classList.add(cn$4('m'));
this.padElement.appendChild(markerElem);
this.markerElem_ = markerElem;
config.value.emitter.on('change', this.onValueChange_);
this.value = config.value;
this.update_();
}
get allFocusableElements() {
return [this.padElement];
}
update_() {
const [x, y] = this.value.rawValue.getComponents();
const max = this.props_.get('max');
const px = mapRange(x, -max, +max, 0, 100);
const py = mapRange(y, -max, +max, 0, 100);
const ipy = this.props_.get('invertsY') ? 100 - py : py;
this.lineElem_.setAttributeNS(null, 'x2', `${px}%`);
this.lineElem_.setAttributeNS(null, 'y2', `${ipy}%`);
this.markerElem_.style.left = `${px}%`;
this.markerElem_.style.top = `${ipy}%`;
}
onValueChange_() {
this.update_();
}
onPropsChange_() {
this.update_();
}
onFoldableChange_() {
this.update_();
}
}
function computeOffset(ev, keyScales, invertsY) {
return [
getStepForKey(keyScales[0], getHorizontalStepKeys(ev)),
getStepForKey(keyScales[1], getVerticalStepKeys(ev)) * (invertsY ? 1 : -1),
];
}
class Point2dPickerController {
constructor(doc, config) {
this.onPadKeyDown_ = this.onPadKeyDown_.bind(this);
this.onPadKeyUp_ = this.onPadKeyUp_.bind(this);
this.onPointerDown_ = this.onPointerDown_.bind(this);
this.onPointerMove_ = this.onPointerMove_.bind(this);
this.onPointerUp_ = this.onPointerUp_.bind(this);
this.props = config.props;
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new Point2dPickerView(doc, {
layout: config.layout,
props: this.props,
value: this.value,
viewProps: this.viewProps,
});
this.ptHandler_ = new PointerHandler(this.view.padElement);
this.ptHandler_.emitter.on('down', this.onPointerDown_);
this.ptHandler_.emitter.on('move', this.onPointerMove_);
this.ptHandler_.emitter.on('up', this.onPointerUp_);
this.view.padElement.addEventListener('keydown', this.onPadKeyDown_);
this.view.padElement.addEventListener('keyup', this.onPadKeyUp_);
}
handlePointerEvent_(d, opts) {
if (!d.point) {
return;
}
const max = this.props.get('max');
const px = mapRange(d.point.x, 0, d.bounds.width, -max, +max);
const py = mapRange(this.props.get('invertsY') ? d.bounds.height - d.point.y : d.point.y, 0, d.bounds.height, -max, +max);
this.value.setRawValue(new Point2d(px, py), opts);
}
onPointerDown_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerMove_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: false,
last: false,
});
}
onPointerUp_(ev) {
this.handlePointerEvent_(ev.data, {
forceEmit: true,
last: true,
});
}
onPadKeyDown_(ev) {
if (isArrowKey(ev.key)) {
ev.preventDefault();
}
const [dx, dy] = computeOffset(ev, [this.props.get('xKeyScale'), this.props.get('yKeyScale')], this.props.get('invertsY'));
if (dx === 0 && dy === 0) {
return;
}
this.value.setRawValue(new Point2d(this.value.rawValue.x + dx, this.value.rawValue.y + dy), {
forceEmit: false,
last: false,
});
}
onPadKeyUp_(ev) {
const [dx, dy] = computeOffset(ev, [this.props.get('xKeyScale'), this.props.get('yKeyScale')], this.props.get('invertsY'));
if (dx === 0 && dy === 0) {
return;
}
this.value.setRawValue(this.value.rawValue, {
forceEmit: true,
last: true,
});
}
}
class Point2dController {
constructor(doc, config) {
var _a, _b;
this.onPopupChildBlur_ = this.onPopupChildBlur_.bind(this);
this.onPopupChildKeydown_ = this.onPopupChildKeydown_.bind(this);
this.onPadButtonBlur_ = this.onPadButtonBlur_.bind(this);
this.onPadButtonClick_ = this.onPadButtonClick_.bind(this);
this.value = config.value;
this.viewProps = config.viewProps;
this.foldable_ = Foldable.create(config.expanded);
this.popC_ =
config.pickerLayout === 'popup'
? new PopupController(doc, {
viewProps: this.viewProps,
})
: null;
const padC = new Point2dPickerController(doc, {
layout: config.pickerLayout,
props: new ValueMap({
invertsY: createValue(config.invertsY),
max: createValue(config.max),
xKeyScale: config.axes[0].textProps.value('keyScale'),
yKeyScale: config.axes[1].textProps.value('keyScale'),
}),
value: this.value,
viewProps: this.viewProps,
});
padC.view.allFocusableElements.forEach((elem) => {
elem.addEventListener('blur', this.onPopupChildBlur_);
elem.addEventListener('keydown', this.onPopupChildKeydown_);
});
this.pickerC_ = padC;
this.textC_ = new PointNdTextController(doc, {
assembly: Point2dAssembly,
axes: config.axes,
parser: config.parser,
value: this.value,
viewProps: this.viewProps,
});
this.view = new Point2dView(doc, {
expanded: this.foldable_.value('expanded'),
pickerLayout: config.pickerLayout,
viewProps: this.viewProps,
});
this.view.textElement.appendChild(this.textC_.view.element);
(_a = this.view.buttonElement) === null || _a === void 0 ? void 0 : _a.addEventListener('blur', this.onPadButtonBlur_);
(_b = this.view.buttonElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.onPadButtonClick_);
if (this.popC_) {
this.view.element.appendChild(this.popC_.view.element);
this.popC_.view.element.appendChild(this.pickerC_.view.element);
connectValues({
primary: this.foldable_.value('expanded'),
secondary: this.popC_.shows,
forward: (p) => p,
backward: (_, s) => s,
});
}
else if (this.view.pickerElement) {
this.view.pickerElement.appendChild(this.pickerC_.view.element);
bindFoldable(this.foldable_, this.view.pickerElement);
}
}
get textController() {
return this.textC_;
}
onPadButtonBlur_(e) {
if (!this.popC_) {
return;
}
const elem = this.view.element;
const nextTarget = forceCast(e.relatedTarget);
if (!nextTarget || !elem.contains(nextTarget)) {
this.popC_.shows.rawValue = false;
}
}
onPadButtonClick_() {
this.foldable_.set('expanded', !this.foldable_.get('expanded'));
if (this.foldable_.get('expanded')) {
this.pickerC_.view.allFocusableElements[0].focus();
}
}
onPopupChildBlur_(ev) {
if (!this.popC_) {
return;
}
const elem = this.popC_.view.element;
const nextTarget = findNextTarget(ev);
if (nextTarget && elem.contains(nextTarget)) {
return;
}
if (nextTarget &&
nextTarget === this.view.buttonElement &&
!supportsTouch(elem.ownerDocument)) {
return;
}
this.popC_.shows.rawValue = false;
}
onPopupChildKeydown_(ev) {
if (this.popC_) {
if (ev.key === 'Escape') {
this.popC_.shows.rawValue = false;
}
}
else if (this.view.pickerElement) {
if (ev.key === 'Escape') {
this.view.buttonElement.focus();
}
}
}
}
function point2dFromUnknown(value) {
return Point2d.isObject(value)
? new Point2d(value.x, value.y)
: new Point2d();
}
function writePoint2d(target, value) {
target.writeProperty('x', value.x);
target.writeProperty('y', value.y);
}
function createConstraint$3(params, initialValue) {
return new PointNdConstraint({
assembly: Point2dAssembly,
components: [
createDimensionConstraint(Object.assign(Object.assign({}, params), params.x), initialValue.x),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.y), initialValue.y),
],
});
}
function getSuitableMaxDimensionValue(params, rawValue) {
var _a, _b;
if (!isEmpty(params.min) || !isEmpty(params.max)) {
return Math.max(Math.abs((_a = params.min) !== null && _a !== void 0 ? _a : 0), Math.abs((_b = params.max) !== null && _b !== void 0 ? _b : 0));
}
const step = getSuitableKeyScale(params);
return Math.max(Math.abs(step) * 10, Math.abs(rawValue) * 10);
}
function getSuitableMax(params, initialValue) {
var _a, _b;
const xr = getSuitableMaxDimensionValue(deepMerge(params, ((_a = params.x) !== null && _a !== void 0 ? _a : {})), initialValue.x);
const yr = getSuitableMaxDimensionValue(deepMerge(params, ((_b = params.y) !== null && _b !== void 0 ? _b : {})), initialValue.y);
return Math.max(xr, yr);
}
function shouldInvertY(params) {
if (!('y' in params)) {
return false;
}
const yParams = params.y;
if (!yParams) {
return false;
}
return 'inverted' in yParams ? !!yParams.inverted : false;
}
const Point2dInputPlugin = createPlugin({
id: 'input-point2d',
type: 'input',
accept: (value, params) => {
if (!Point2d.isObject(value)) {
return null;
}
const result = parseRecord(params, (p) => (Object.assign(Object.assign({}, createPointDimensionParser(p)), { expanded: p.optional.boolean, picker: p.optional.custom(parsePickerLayout), readonly: p.optional.constant(false), x: p.optional.custom(parsePointDimensionParams), y: p.optional.object(Object.assign(Object.assign({}, createPointDimensionParser(p)), { inverted: p.optional.boolean })) })));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: () => point2dFromUnknown,
constraint: (args) => createConstraint$3(args.params, args.initialValue),
equals: Point2d.equals,
writer: () => writePoint2d,
},
controller: (args) => {
var _a, _b;
const doc = args.document;
const value = args.value;
const c = args.constraint;
const dParams = [args.params.x, args.params.y];
return new Point2dController(doc, {
axes: value.rawValue.getComponents().map((comp, i) => {
var _a;
return createPointAxis({
constraint: c.components[i],
initialValue: comp,
params: deepMerge(args.params, ((_a = dParams[i]) !== null && _a !== void 0 ? _a : {})),
});
}),
expanded: (_a = args.params.expanded) !== null && _a !== void 0 ? _a : false,
invertsY: shouldInvertY(args.params),
max: getSuitableMax(args.params, value.rawValue),
parser: parseNumber,
pickerLayout: (_b = args.params.picker) !== null && _b !== void 0 ? _b : 'popup',
value: value,
viewProps: args.viewProps,
});
},
});
class Point3d {
constructor(x = 0, y = 0, z = 0) {
this.x = x;
this.y = y;
this.z = z;
}
getComponents() {
return [this.x, this.y, this.z];
}
static isObject(obj) {
if (isEmpty(obj)) {
return false;
}
const x = obj.x;
const y = obj.y;
const z = obj.z;
if (typeof x !== 'number' ||
typeof y !== 'number' ||
typeof z !== 'number') {
return false;
}
return true;
}
static equals(v1, v2) {
return v1.x === v2.x && v1.y === v2.y && v1.z === v2.z;
}
toObject() {
return {
x: this.x,
y: this.y,
z: this.z,
};
}
}
const Point3dAssembly = {
toComponents: (p) => p.getComponents(),
fromComponents: (comps) => new Point3d(...comps),
};
function point3dFromUnknown(value) {
return Point3d.isObject(value)
? new Point3d(value.x, value.y, value.z)
: new Point3d();
}
function writePoint3d(target, value) {
target.writeProperty('x', value.x);
target.writeProperty('y', value.y);
target.writeProperty('z', value.z);
}
function createConstraint$2(params, initialValue) {
return new PointNdConstraint({
assembly: Point3dAssembly,
components: [
createDimensionConstraint(Object.assign(Object.assign({}, params), params.x), initialValue.x),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.y), initialValue.y),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.z), initialValue.z),
],
});
}
const Point3dInputPlugin = createPlugin({
id: 'input-point3d',
type: 'input',
accept: (value, params) => {
if (!Point3d.isObject(value)) {
return null;
}
const result = parseRecord(params, (p) => (Object.assign(Object.assign({}, createPointDimensionParser(p)), { readonly: p.optional.constant(false), x: p.optional.custom(parsePointDimensionParams), y: p.optional.custom(parsePointDimensionParams), z: p.optional.custom(parsePointDimensionParams) })));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => point3dFromUnknown,
constraint: (args) => createConstraint$2(args.params, args.initialValue),
equals: Point3d.equals,
writer: (_args) => writePoint3d,
},
controller: (args) => {
const value = args.value;
const c = args.constraint;
const dParams = [args.params.x, args.params.y, args.params.z];
return new PointNdTextController(args.document, {
assembly: Point3dAssembly,
axes: value.rawValue.getComponents().map((comp, i) => {
var _a;
return createPointAxis({
constraint: c.components[i],
initialValue: comp,
params: deepMerge(args.params, ((_a = dParams[i]) !== null && _a !== void 0 ? _a : {})),
});
}),
parser: parseNumber,
value: value,
viewProps: args.viewProps,
});
},
});
class Point4d {
constructor(x = 0, y = 0, z = 0, w = 0) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
getComponents() {
return [this.x, this.y, this.z, this.w];
}
static isObject(obj) {
if (isEmpty(obj)) {
return false;
}
const x = obj.x;
const y = obj.y;
const z = obj.z;
const w = obj.w;
if (typeof x !== 'number' ||
typeof y !== 'number' ||
typeof z !== 'number' ||
typeof w !== 'number') {
return false;
}
return true;
}
static equals(v1, v2) {
return v1.x === v2.x && v1.y === v2.y && v1.z === v2.z && v1.w === v2.w;
}
toObject() {
return {
x: this.x,
y: this.y,
z: this.z,
w: this.w,
};
}
}
const Point4dAssembly = {
toComponents: (p) => p.getComponents(),
fromComponents: (comps) => new Point4d(...comps),
};
function point4dFromUnknown(value) {
return Point4d.isObject(value)
? new Point4d(value.x, value.y, value.z, value.w)
: new Point4d();
}
function writePoint4d(target, value) {
target.writeProperty('x', value.x);
target.writeProperty('y', value.y);
target.writeProperty('z', value.z);
target.writeProperty('w', value.w);
}
function createConstraint$1(params, initialValue) {
return new PointNdConstraint({
assembly: Point4dAssembly,
components: [
createDimensionConstraint(Object.assign(Object.assign({}, params), params.x), initialValue.x),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.y), initialValue.y),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.z), initialValue.z),
createDimensionConstraint(Object.assign(Object.assign({}, params), params.w), initialValue.w),
],
});
}
const Point4dInputPlugin = createPlugin({
id: 'input-point4d',
type: 'input',
accept: (value, params) => {
if (!Point4d.isObject(value)) {
return null;
}
const result = parseRecord(params, (p) => (Object.assign(Object.assign({}, createPointDimensionParser(p)), { readonly: p.optional.constant(false), w: p.optional.custom(parsePointDimensionParams), x: p.optional.custom(parsePointDimensionParams), y: p.optional.custom(parsePointDimensionParams), z: p.optional.custom(parsePointDimensionParams) })));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => point4dFromUnknown,
constraint: (args) => createConstraint$1(args.params, args.initialValue),
equals: Point4d.equals,
writer: (_args) => writePoint4d,
},
controller: (args) => {
const value = args.value;
const c = args.constraint;
const dParams = [
args.params.x,
args.params.y,
args.params.z,
args.params.w,
];
return new PointNdTextController(args.document, {
assembly: Point4dAssembly,
axes: value.rawValue.getComponents().map((comp, i) => {
var _a;
return createPointAxis({
constraint: c.components[i],
initialValue: comp,
params: deepMerge(args.params, ((_a = dParams[i]) !== null && _a !== void 0 ? _a : {})),
});
}),
parser: parseNumber,
value: value,
viewProps: args.viewProps,
});
},
});
function createConstraint(params) {
const constraints = [];
const lc = createListConstraint(params.options);
if (lc) {
constraints.push(lc);
}
return new CompositeConstraint(constraints);
}
const StringInputPlugin = createPlugin({
id: 'input-string',
type: 'input',
accept: (value, params) => {
if (typeof value !== 'string') {
return null;
}
const result = parseRecord(params, (p) => ({
readonly: p.optional.constant(false),
options: p.optional.custom(parseListOptions),
}));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => stringFromUnknown,
constraint: (args) => createConstraint(args.params),
writer: (_args) => writePrimitive,
},
controller: (args) => {
const doc = args.document;
const value = args.value;
const c = args.constraint;
const lc = c && findConstraint(c, ListConstraint);
if (lc) {
return new ListController(doc, {
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
});
}
return new TextController(doc, {
parser: (v) => v,
props: ValueMap.fromObject({
formatter: formatString,
}),
value: value,
viewProps: args.viewProps,
});
},
api(args) {
if (typeof args.controller.value.rawValue !== 'string') {
return null;
}
if (args.controller.valueController instanceof ListController) {
return new ListInputBindingApi(args.controller);
}
return null;
},
});
const Constants = {
monitor: {
defaultInterval: 200,
defaultRows: 3,
},
};
const cn$3 = ClassName('mll');
class MultiLogView {
constructor(doc, config) {
this.onValueUpdate_ = this.onValueUpdate_.bind(this);
this.formatter_ = config.formatter;
this.element = doc.createElement('div');
this.element.classList.add(cn$3());
config.viewProps.bindClassModifiers(this.element);
const textareaElem = doc.createElement('textarea');
textareaElem.classList.add(cn$3('i'));
textareaElem.style.height = `calc(var(${getCssVar('containerUnitSize')}) * ${config.rows})`;
textareaElem.readOnly = true;
config.viewProps.bindDisabled(textareaElem);
this.element.appendChild(textareaElem);
this.textareaElem_ = textareaElem;
config.value.emitter.on('change', this.onValueUpdate_);
this.value = config.value;
this.update_();
}
update_() {
const elem = this.textareaElem_;
const shouldScroll = elem.scrollTop === elem.scrollHeight - elem.clientHeight;
const lines = [];
this.value.rawValue.forEach((value) => {
if (value !== undefined) {
lines.push(this.formatter_(value));
}
});
elem.textContent = lines.join('\n');
if (shouldScroll) {
elem.scrollTop = elem.scrollHeight;
}
}
onValueUpdate_() {
this.update_();
}
}
class MultiLogController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new MultiLogView(doc, {
formatter: config.formatter,
rows: config.rows,
value: this.value,
viewProps: this.viewProps,
});
}
}
const cn$2 = ClassName('sgl');
class SingleLogView {
constructor(doc, config) {
this.onValueUpdate_ = this.onValueUpdate_.bind(this);
this.formatter_ = config.formatter;
this.element = doc.createElement('div');
this.element.classList.add(cn$2());
config.viewProps.bindClassModifiers(this.element);
const inputElem = doc.createElement('input');
inputElem.classList.add(cn$2('i'));
inputElem.readOnly = true;
inputElem.type = 'text';
config.viewProps.bindDisabled(inputElem);
this.element.appendChild(inputElem);
this.inputElement = inputElem;
config.value.emitter.on('change', this.onValueUpdate_);
this.value = config.value;
this.update_();
}
update_() {
const values = this.value.rawValue;
const lastValue = values[values.length - 1];
this.inputElement.value =
lastValue !== undefined ? this.formatter_(lastValue) : '';
}
onValueUpdate_() {
this.update_();
}
}
class SingleLogController {
constructor(doc, config) {
this.value = config.value;
this.viewProps = config.viewProps;
this.view = new SingleLogView(doc, {
formatter: config.formatter,
value: this.value,
viewProps: this.viewProps,
});
}
}
const BooleanMonitorPlugin = createPlugin({
id: 'monitor-bool',
type: 'monitor',
accept: (value, params) => {
if (typeof value !== 'boolean') {
return null;
}
const result = parseRecord(params, (p) => ({
readonly: p.required.constant(true),
rows: p.optional.number,
}));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => boolFromUnknown,
},
controller: (args) => {
var _a;
if (args.value.rawValue.length === 1) {
return new SingleLogController(args.document, {
formatter: BooleanFormatter,
value: args.value,
viewProps: args.viewProps,
});
}
return new MultiLogController(args.document, {
formatter: BooleanFormatter,
rows: (_a = args.params.rows) !== null && _a !== void 0 ? _a : Constants.monitor.defaultRows,
value: args.value,
viewProps: args.viewProps,
});
},
});
class GraphLogMonitorBindingApi extends BindingApi {
get max() {
return this.controller.valueController.props.get('max');
}
set max(max) {
this.controller.valueController.props.set('max', max);
}
get min() {
return this.controller.valueController.props.get('min');
}
set min(min) {
this.controller.valueController.props.set('min', min);
}
}
const cn$1 = ClassName('grl');
class GraphLogView {
constructor(doc, config) {
this.onCursorChange_ = this.onCursorChange_.bind(this);
this.onValueUpdate_ = this.onValueUpdate_.bind(this);
this.element = doc.createElement('div');
this.element.classList.add(cn$1());
config.viewProps.bindClassModifiers(this.element);
this.formatter_ = config.formatter;
this.props_ = config.props;
this.cursor_ = config.cursor;
this.cursor_.emitter.on('change', this.onCursorChange_);
const svgElem = doc.createElementNS(SVG_NS, 'svg');
svgElem.classList.add(cn$1('g'));
svgElem.style.height = `calc(var(${getCssVar('containerUnitSize')}) * ${config.rows})`;
this.element.appendChild(svgElem);
this.svgElem_ = svgElem;
const lineElem = doc.createElementNS(SVG_NS, 'polyline');
this.svgElem_.appendChild(lineElem);
this.lineElem_ = lineElem;
const tooltipElem = doc.createElement('div');
tooltipElem.classList.add(cn$1('t'), ClassName('tt')());
this.element.appendChild(tooltipElem);
this.tooltipElem_ = tooltipElem;
config.value.emitter.on('change', this.onValueUpdate_);
this.value = config.value;
this.update_();
}
get graphElement() {
return this.svgElem_;
}
update_() {
const { clientWidth: w, clientHeight: h } = this.element;
const maxIndex = this.value.rawValue.length - 1;
const min = this.props_.get('min');
const max = this.props_.get('max');
const points = [];
this.value.rawValue.forEach((v, index) => {
if (v === undefined) {
return;
}
const x = mapRange(index, 0, maxIndex, 0, w);
const y = mapRange(v, min, max, h, 0);
points.push([x, y].join(','));
});
this.lineElem_.setAttributeNS(null, 'points', points.join(' '));
const tooltipElem = this.tooltipElem_;
const value = this.value.rawValue[this.cursor_.rawValue];
if (value === undefined) {
tooltipElem.classList.remove(cn$1('t', 'a'));
return;
}
const tx = mapRange(this.cursor_.rawValue, 0, maxIndex, 0, w);
const ty = mapRange(value, min, max, h, 0);
tooltipElem.style.left = `${tx}px`;
tooltipElem.style.top = `${ty}px`;
tooltipElem.textContent = `${this.formatter_(value)}`;
if (!tooltipElem.classList.contains(cn$1('t', 'a'))) {
tooltipElem.classList.add(cn$1('t', 'a'), cn$1('t', 'in'));
forceReflow(tooltipElem);
tooltipElem.classList.remove(cn$1('t', 'in'));
}
}
onValueUpdate_() {
this.update_();
}
onCursorChange_() {
this.update_();
}
}
class GraphLogController {
constructor(doc, config) {
this.onGraphMouseMove_ = this.onGraphMouseMove_.bind(this);
this.onGraphMouseLeave_ = this.onGraphMouseLeave_.bind(this);
this.onGraphPointerDown_ = this.onGraphPointerDown_.bind(this);
this.onGraphPointerMove_ = this.onGraphPointerMove_.bind(this);
this.onGraphPointerUp_ = this.onGraphPointerUp_.bind(this);
this.props = config.props;
this.value = config.value;
this.viewProps = config.viewProps;
this.cursor_ = createValue(-1);
this.view = new GraphLogView(doc, {
cursor: this.cursor_,
formatter: config.formatter,
rows: config.rows,
props: this.props,
value: this.value,
viewProps: this.viewProps,
});
if (!supportsTouch(doc)) {
this.view.element.addEventListener('mousemove', this.onGraphMouseMove_);
this.view.element.addEventListener('mouseleave', this.onGraphMouseLeave_);
}
else {
const ph = new PointerHandler(this.view.element);
ph.emitter.on('down', this.onGraphPointerDown_);
ph.emitter.on('move', this.onGraphPointerMove_);
ph.emitter.on('up', this.onGraphPointerUp_);
}
}
importProps(state) {
return importBladeState(state, null, (p) => ({
max: p.required.number,
min: p.required.number,
}), (result) => {
this.props.set('max', result.max);
this.props.set('min', result.min);
return true;
});
}
exportProps() {
return exportBladeState(null, {
max: this.props.get('max'),
min: this.props.get('min'),
});
}
onGraphMouseLeave_() {
this.cursor_.rawValue = -1;
}
onGraphMouseMove_(ev) {
const { clientWidth: w } = this.view.element;
this.cursor_.rawValue = Math.floor(mapRange(ev.offsetX, 0, w, 0, this.value.rawValue.length));
}
onGraphPointerDown_(ev) {
this.onGraphPointerMove_(ev);
}
onGraphPointerMove_(ev) {
if (!ev.data.point) {
this.cursor_.rawValue = -1;
return;
}
this.cursor_.rawValue = Math.floor(mapRange(ev.data.point.x, 0, ev.data.bounds.width, 0, this.value.rawValue.length));
}
onGraphPointerUp_() {
this.cursor_.rawValue = -1;
}
}
function createFormatter(params) {
return !isEmpty(params.format) ? params.format : createNumberFormatter(2);
}
function createTextMonitor(args) {
var _a;
if (args.value.rawValue.length === 1) {
return new SingleLogController(args.document, {
formatter: createFormatter(args.params),
value: args.value,
viewProps: args.viewProps,
});
}
return new MultiLogController(args.document, {
formatter: createFormatter(args.params),
rows: (_a = args.params.rows) !== null && _a !== void 0 ? _a : Constants.monitor.defaultRows,
value: args.value,
viewProps: args.viewProps,
});
}
function createGraphMonitor(args) {
var _a, _b, _c;
return new GraphLogController(args.document, {
formatter: createFormatter(args.params),
rows: (_a = args.params.rows) !== null && _a !== void 0 ? _a : Constants.monitor.defaultRows,
props: ValueMap.fromObject({
max: (_b = args.params.max) !== null && _b !== void 0 ? _b : 100,
min: (_c = args.params.min) !== null && _c !== void 0 ? _c : 0,
}),
value: args.value,
viewProps: args.viewProps,
});
}
function shouldShowGraph(params) {
return params.view === 'graph';
}
const NumberMonitorPlugin = createPlugin({
id: 'monitor-number',
type: 'monitor',
accept: (value, params) => {
if (typeof value !== 'number') {
return null;
}
const result = parseRecord(params, (p) => ({
format: p.optional.function,
max: p.optional.number,
min: p.optional.number,
readonly: p.required.constant(true),
rows: p.optional.number,
view: p.optional.string,
}));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
defaultBufferSize: (params) => (shouldShowGraph(params) ? 64 : 1),
reader: (_args) => numberFromUnknown,
},
controller: (args) => {
if (shouldShowGraph(args.params)) {
return createGraphMonitor(args);
}
return createTextMonitor(args);
},
api: (args) => {
if (args.controller.valueController instanceof GraphLogController) {
return new GraphLogMonitorBindingApi(args.controller);
}
return null;
},
});
const StringMonitorPlugin = createPlugin({
id: 'monitor-string',
type: 'monitor',
accept: (value, params) => {
if (typeof value !== 'string') {
return null;
}
const result = parseRecord(params, (p) => ({
multiline: p.optional.boolean,
readonly: p.required.constant(true),
rows: p.optional.number,
}));
return result
? {
initialValue: value,
params: result,
}
: null;
},
binding: {
reader: (_args) => stringFromUnknown,
},
controller: (args) => {
var _a;
const value = args.value;
const multiline = value.rawValue.length > 1 || args.params.multiline;
if (multiline) {
return new MultiLogController(args.document, {
formatter: formatString,
rows: (_a = args.params.rows) !== null && _a !== void 0 ? _a : Constants.monitor.defaultRows,
value: value,
viewProps: args.viewProps,
});
}
return new SingleLogController(args.document, {
formatter: formatString,
value: value,
viewProps: args.viewProps,
});
},
});
class BladeApiCache {
constructor() {
this.map_ = new Map();
}
get(bc) {
var _a;
return (_a = this.map_.get(bc)) !== null && _a !== void 0 ? _a : null;
}
has(bc) {
return this.map_.has(bc);
}
add(bc, api) {
this.map_.set(bc, api);
bc.viewProps.handleDispose(() => {
this.map_.delete(bc);
});
return api;
}
}
class ReadWriteBinding {
constructor(config) {
this.target = config.target;
this.reader_ = config.reader;
this.writer_ = config.writer;
}
read() {
return this.reader_(this.target.read());
}
write(value) {
this.writer_(this.target, value);
}
inject(value) {
this.write(this.reader_(value));
}
}
function createInputBindingController(plugin, args) {
var _a;
const result = plugin.accept(args.target.read(), args.params);
if (isEmpty(result)) {
return null;
}
const valueArgs = {
target: args.target,
initialValue: result.initialValue,
params: result.params,
};
const params = parseRecord(args.params, (p) => ({
disabled: p.optional.boolean,
hidden: p.optional.boolean,
label: p.optional.string,
tag: p.optional.string,
}));
const reader = plugin.binding.reader(valueArgs);
const constraint = plugin.binding.constraint
? plugin.binding.constraint(valueArgs)
: undefined;
const binding = new ReadWriteBinding({
reader: reader,
target: args.target,
writer: plugin.binding.writer(valueArgs),
});
const value = new InputBindingValue(createValue(reader(result.initialValue), {
constraint: constraint,
equals: plugin.binding.equals,
}), binding);
const controller = plugin.controller({
constraint: constraint,
document: args.document,
initialValue: result.initialValue,
params: result.params,
value: value,
viewProps: ViewProps.create({
disabled: params === null || params === void 0 ? void 0 : params.disabled,
hidden: params === null || params === void 0 ? void 0 : params.hidden,
}),
});
return new InputBindingController(args.document, {
blade: createBlade(),
props: ValueMap.fromObject({
label: 'label' in args.params ? (_a = params === null || params === void 0 ? void 0 : params.label) !== null && _a !== void 0 ? _a : null : args.target.key,
}),
tag: params === null || params === void 0 ? void 0 : params.tag,
value: value,
valueController: controller,
});
}
class ReadonlyBinding {
constructor(config) {
this.target = config.target;
this.reader_ = config.reader;
}
read() {
return this.reader_(this.target.read());
}
}
function createTicker(document, interval) {
return interval === 0
? new ManualTicker()
: new IntervalTicker(document, interval !== null && interval !== void 0 ? interval : Constants.monitor.defaultInterval);
}
function createMonitorBindingController(plugin, args) {
var _a, _b, _c;
const result = plugin.accept(args.target.read(), args.params);
if (isEmpty(result)) {
return null;
}
const bindingArgs = {
target: args.target,
initialValue: result.initialValue,
params: result.params,
};
const params = parseRecord(args.params, (p) => ({
bufferSize: p.optional.number,
disabled: p.optional.boolean,
hidden: p.optional.boolean,
interval: p.optional.number,
label: p.optional.string,
}));
const reader = plugin.binding.reader(bindingArgs);
const bufferSize = (_b = (_a = params === null || params === void 0 ? void 0 : params.bufferSize) !== null && _a !== void 0 ? _a : (plugin.binding.defaultBufferSize &&
plugin.binding.defaultBufferSize(result.params))) !== null && _b !== void 0 ? _b : 1;
const value = new MonitorBindingValue({
binding: new ReadonlyBinding({
reader: reader,
target: args.target,
}),
bufferSize: bufferSize,
ticker: createTicker(args.document, params === null || params === void 0 ? void 0 : params.interval),
});
const controller = plugin.controller({
document: args.document,
params: result.params,
value: value,
viewProps: ViewProps.create({
disabled: params === null || params === void 0 ? void 0 : params.disabled,
hidden: params === null || params === void 0 ? void 0 : params.hidden,
}),
});
controller.viewProps.bindDisabled(value.ticker);
controller.viewProps.handleDispose(() => {
value.ticker.dispose();
});
return new MonitorBindingController(args.document, {
blade: createBlade(),
props: ValueMap.fromObject({
label: 'label' in args.params ? (_c = params === null || params === void 0 ? void 0 : params.label) !== null && _c !== void 0 ? _c : null : args.target.key,
}),
value: value,
valueController: controller,
});
}
class PluginPool {
constructor(apiCache) {
this.pluginsMap_ = {
blades: [],
inputs: [],
monitors: [],
};
this.apiCache_ = apiCache;
}
getAll() {
return [
...this.pluginsMap_.blades,
...this.pluginsMap_.inputs,
...this.pluginsMap_.monitors,
];
}
register(bundleId, r) {
if (!isCompatible(r.core)) {
throw TpError.notCompatible(bundleId, r.id);
}
if (r.type === 'blade') {
this.pluginsMap_.blades.unshift(r);
}
else if (r.type === 'input') {
this.pluginsMap_.inputs.unshift(r);
}
else if (r.type === 'monitor') {
this.pluginsMap_.monitors.unshift(r);
}
}
createInput_(document, target, params) {
return this.pluginsMap_.inputs.reduce((result, plugin) => result !== null && result !== void 0 ? result : createInputBindingController(plugin, {
document: document,
target: target,
params: params,
}), null);
}
createMonitor_(document, target, params) {
return this.pluginsMap_.monitors.reduce((result, plugin) => result !== null && result !== void 0 ? result : createMonitorBindingController(plugin, {
document: document,
params: params,
target: target,
}), null);
}
createBinding(doc, target, params) {
const initialValue = target.read();
if (isEmpty(initialValue)) {
throw new TpError({
context: {
key: target.key,
},
type: 'nomatchingcontroller',
});
}
const ic = this.createInput_(doc, target, params);
if (ic) {
return ic;
}
const mc = this.createMonitor_(doc, target, params);
if (mc) {
return mc;
}
throw new TpError({
context: {
key: target.key,
},
type: 'nomatchingcontroller',
});
}
createBlade(document, params) {
const bc = this.pluginsMap_.blades.reduce((result, plugin) => result !== null && result !== void 0 ? result : createBladeController(plugin, {
document: document,
params: params,
}), null);
if (!bc) {
throw new TpError({
type: 'nomatchingview',
context: {
params: params,
},
});
}
return bc;
}
createInputBindingApi_(bc) {
const api = this.pluginsMap_.inputs.reduce((result, plugin) => {
var _a, _b;
if (result) {
return result;
}
return ((_b = (_a = plugin.api) === null || _a === void 0 ? void 0 : _a.call(plugin, {
controller: bc,
})) !== null && _b !== void 0 ? _b : null);
}, null);
return this.apiCache_.add(bc, api !== null && api !== void 0 ? api : new BindingApi(bc));
}
createMonitorBindingApi_(bc) {
const api = this.pluginsMap_.monitors.reduce((result, plugin) => {
var _a, _b;
if (result) {
return result;
}
return ((_b = (_a = plugin.api) === null || _a === void 0 ? void 0 : _a.call(plugin, {
controller: bc,
})) !== null && _b !== void 0 ? _b : null);
}, null);
return this.apiCache_.add(bc, api !== null && api !== void 0 ? api : new BindingApi(bc));
}
createBindingApi(bc) {
if (this.apiCache_.has(bc)) {
return this.apiCache_.get(bc);
}
if (isInputBindingController(bc)) {
return this.createInputBindingApi_(bc);
}
if (isMonitorBindingController(bc)) {
return this.createMonitorBindingApi_(bc);
}
throw TpError.shouldNeverHappen();
}
createApi(bc) {
if (this.apiCache_.has(bc)) {
return this.apiCache_.get(bc);
}
if (isBindingController(bc)) {
return this.createBindingApi(bc);
}
const api = this.pluginsMap_.blades.reduce((result, plugin) => result !== null && result !== void 0 ? result : plugin.api({
controller: bc,
pool: this,
}), null);
if (!api) {
throw TpError.shouldNeverHappen();
}
return this.apiCache_.add(bc, api);
}
}
const sharedCache = new BladeApiCache();
function createDefaultPluginPool() {
const pool = new PluginPool(sharedCache);
[
Point2dInputPlugin,
Point3dInputPlugin,
Point4dInputPlugin,
StringInputPlugin,
NumberInputPlugin,
StringColorInputPlugin,
ObjectColorInputPlugin,
NumberColorInputPlugin,
BooleanInputPlugin,
BooleanMonitorPlugin,
StringMonitorPlugin,
NumberMonitorPlugin,
ButtonBladePlugin,
FolderBladePlugin,
TabBladePlugin,
].forEach((p) => {
pool.register('core', p);
});
return pool;
}
class ListBladeApi extends BladeApi {
/**
* @hidden
*/
constructor(controller) {
super(controller);
this.emitter_ = new Emitter();
this.controller.value.emitter.on('change', (ev) => {
this.emitter_.emit('change', new TpChangeEvent(this, ev.rawValue));
});
}
get label() {
return this.controller.labelController.props.get('label');
}
set label(label) {
this.controller.labelController.props.set('label', label);
}
get options() {
return this.controller.valueController.props.get('options');
}
set options(options) {
this.controller.valueController.props.set('options', options);
}
get value() {
return this.controller.value.rawValue;
}
set value(value) {
this.controller.value.rawValue = value;
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
}
class SeparatorBladeApi extends BladeApi {
}
class SliderBladeApi extends BladeApi {
/**
* @hidden
*/
constructor(controller) {
super(controller);
this.emitter_ = new Emitter();
this.controller.value.emitter.on('change', (ev) => {
this.emitter_.emit('change', new TpChangeEvent(this, ev.rawValue));
});
}
get label() {
return this.controller.labelController.props.get('label');
}
set label(label) {
this.controller.labelController.props.set('label', label);
}
get max() {
return this.controller.valueController.sliderController.props.get('max');
}
set max(max) {
this.controller.valueController.sliderController.props.set('max', max);
}
get min() {
return this.controller.valueController.sliderController.props.get('min');
}
set min(min) {
this.controller.valueController.sliderController.props.set('min', min);
}
get value() {
return this.controller.value.rawValue;
}
set value(value) {
this.controller.value.rawValue = value;
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
}
class TextBladeApi extends BladeApi {
/**
* @hidden
*/
constructor(controller) {
super(controller);
this.emitter_ = new Emitter();
this.controller.value.emitter.on('change', (ev) => {
this.emitter_.emit('change', new TpChangeEvent(this, ev.rawValue));
});
}
get label() {
return this.controller.labelController.props.get('label');
}
set label(label) {
this.controller.labelController.props.set('label', label);
}
get formatter() {
return this.controller.valueController.props.get('formatter');
}
set formatter(formatter) {
this.controller.valueController.props.set('formatter', formatter);
}
get value() {
return this.controller.value.rawValue;
}
set value(value) {
this.controller.value.rawValue = value;
}
on(eventName, handler) {
const bh = handler.bind(this);
this.emitter_.on(eventName, (ev) => {
bh(ev);
}, {
key: handler,
});
return this;
}
off(eventName, handler) {
this.emitter_.off(eventName, handler);
return this;
}
}
const ListBladePlugin = (function () {
return {
id: 'list',
type: 'blade',
core: VERSION$1,
accept(params) {
const result = parseRecord(params, (p) => ({
options: p.required.custom(parseListOptions),
value: p.required.raw,
view: p.required.constant('list'),
label: p.optional.string,
}));
return result ? { params: result } : null;
},
controller(args) {
const lc = new ListConstraint(normalizeListOptions(args.params.options));
const value = createValue(args.params.value, {
constraint: lc,
});
const ic = new ListController(args.document, {
props: new ValueMap({
options: lc.values.value('options'),
}),
value: value,
viewProps: args.viewProps,
});
return new LabeledValueBladeController(args.document, {
blade: args.blade,
props: ValueMap.fromObject({
label: args.params.label,
}),
value: value,
valueController: ic,
});
},
api(args) {
if (!(args.controller instanceof LabeledValueBladeController)) {
return null;
}
if (!(args.controller.valueController instanceof ListController)) {
return null;
}
return new ListBladeApi(args.controller);
},
};
})();
class RootApi extends FolderApi {
/**
* @hidden
*/
constructor(controller, pool) {
super(controller, pool);
}
get element() {
return this.controller.view.element;
}
}
/**
* @hidden
*/
class RootController extends FolderController {
constructor(doc, config) {
super(doc, {
expanded: config.expanded,
blade: config.blade,
props: config.props,
root: true,
viewProps: config.viewProps,
});
}
}
const cn = ClassName('spr');
/**
* @hidden
*/
class SeparatorView {
constructor(doc, config) {
this.element = doc.createElement('div');
this.element.classList.add(cn());
config.viewProps.bindClassModifiers(this.element);
const hrElem = doc.createElement('hr');
hrElem.classList.add(cn('r'));
this.element.appendChild(hrElem);
}
}
/**
* @hidden
*/
class SeparatorController extends BladeController {
/**
* @hidden
*/
constructor(doc, config) {
super(Object.assign(Object.assign({}, config), { view: new SeparatorView(doc, {
viewProps: config.viewProps,
}) }));
}
}
const SeparatorBladePlugin = {
id: 'separator',
type: 'blade',
core: VERSION$1,
accept(params) {
const result = parseRecord(params, (p) => ({
view: p.required.constant('separator'),
}));
return result ? { params: result } : null;
},
controller(args) {
return new SeparatorController(args.document, {
blade: args.blade,
viewProps: args.viewProps,
});
},
api(args) {
if (!(args.controller instanceof SeparatorController)) {
return null;
}
return new SeparatorBladeApi(args.controller);
},
};
const SliderBladePlugin = {
id: 'slider',
type: 'blade',
core: VERSION$1,
accept(params) {
const result = parseRecord(params, (p) => ({
max: p.required.number,
min: p.required.number,
view: p.required.constant('slider'),
format: p.optional.function,
label: p.optional.string,
value: p.optional.number,
}));
return result ? { params: result } : null;
},
controller(args) {
var _a, _b;
const initialValue = (_a = args.params.value) !== null && _a !== void 0 ? _a : 0;
const drc = new DefiniteRangeConstraint({
max: args.params.max,
min: args.params.min,
});
const v = createValue(initialValue, {
constraint: drc,
});
const vc = new SliderTextController(args.document, Object.assign(Object.assign({}, createSliderTextProps({
formatter: (_b = args.params.format) !== null && _b !== void 0 ? _b : numberToString,
keyScale: createValue(1),
max: drc.values.value('max'),
min: drc.values.value('min'),
pointerScale: getSuitablePointerScale(args.params, initialValue),
})), { parser: parseNumber, value: v, viewProps: args.viewProps }));
return new LabeledValueBladeController(args.document, {
blade: args.blade,
props: ValueMap.fromObject({
label: args.params.label,
}),
value: v,
valueController: vc,
});
},
api(args) {
if (!(args.controller instanceof LabeledValueBladeController)) {
return null;
}
if (!(args.controller.valueController instanceof SliderTextController)) {
return null;
}
return new SliderBladeApi(args.controller);
},
};
const TextBladePlugin = (function () {
return {
id: 'text',
type: 'blade',
core: VERSION$1,
accept(params) {
const result = parseRecord(params, (p) => ({
parse: p.required.function,
value: p.required.raw,
view: p.required.constant('text'),
format: p.optional.function,
label: p.optional.string,
}));
return result ? { params: result } : null;
},
controller(args) {
var _a;
const v = createValue(args.params.value);
const ic = new TextController(args.document, {
parser: args.params.parse,
props: ValueMap.fromObject({
formatter: (_a = args.params.format) !== null && _a !== void 0 ? _a : ((v) => String(v)),
}),
value: v,
viewProps: args.viewProps,
});
return new LabeledValueBladeController(args.document, {
blade: args.blade,
props: ValueMap.fromObject({
label: args.params.label,
}),
value: v,
valueController: ic,
});
},
api(args) {
if (!(args.controller instanceof LabeledValueBladeController)) {
return null;
}
if (!(args.controller.valueController instanceof TextController)) {
return null;
}
return new TextBladeApi(args.controller);
},
};
})();
function createDefaultWrapperElement(doc) {
const elem = doc.createElement('div');
elem.classList.add(ClassName('dfw')());
if (doc.body) {
doc.body.appendChild(elem);
}
return elem;
}
function embedStyle(doc, id, css) {
if (doc.querySelector(`style[data-tp-style=${id}]`)) {
return;
}
const styleElem = doc.createElement('style');
styleElem.dataset.tpStyle = id;
styleElem.textContent = css;
doc.head.appendChild(styleElem);
}
/**
* The root pane of Tweakpane.
*/
class Pane extends RootApi {
constructor(opt_config) {
var _a, _b;
const config = opt_config !== null && opt_config !== void 0 ? opt_config : {};
const doc = (_a = config.document) !== null && _a !== void 0 ? _a : getWindowDocument();
const pool = createDefaultPluginPool();
const rootController = new RootController(doc, {
expanded: config.expanded,
blade: createBlade(),
props: ValueMap.fromObject({
title: config.title,
}),
viewProps: ViewProps.create(),
});
super(rootController, pool);
this.pool_ = pool;
this.containerElem_ = (_b = config.container) !== null && _b !== void 0 ? _b : createDefaultWrapperElement(doc);
this.containerElem_.appendChild(this.element);
this.doc_ = doc;
this.usesDefaultWrapper_ = !config.container;
this.setUpDefaultPlugins_();
}
get document() {
if (!this.doc_) {
throw TpError.alreadyDisposed();
}
return this.doc_;
}
dispose() {
const containerElem = this.containerElem_;
if (!containerElem) {
throw TpError.alreadyDisposed();
}
if (this.usesDefaultWrapper_) {
const parentElem = containerElem.parentElement;
if (parentElem) {
parentElem.removeChild(containerElem);
}
}
this.containerElem_ = null;
this.doc_ = null;
super.dispose();
}
registerPlugin(bundle) {
if (bundle.css) {
embedStyle(this.document, `plugin-${bundle.id}`, bundle.css);
}
const plugins = 'plugin' in bundle
? [bundle.plugin]
: 'plugins' in bundle
? bundle.plugins
: [];
plugins.forEach((p) => {
this.pool_.register(bundle.id, p);
});
}
setUpDefaultPlugins_() {
this.registerPlugin({
id: 'default',
// NOTE: This string literal will be replaced with the default CSS by Rollup at the compilation time
css: '.tp-tbiv_b,.tp-coltxtv_ms,.tp-colswv_b,.tp-ckbv_i,.tp-sglv_i,.tp-mllv_i,.tp-grlv_g,.tp-txtv_i,.tp-p2dpv_p,.tp-colswv_sw,.tp-rotv_b,.tp-fldv_b,.tp-p2dv_b,.tp-btnv_b,.tp-lstv_s{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:rgba(0,0,0,0);border-width:0;font-family:inherit;font-size:inherit;font-weight:inherit;margin:0;outline:none;padding:0}.tp-p2dv_b,.tp-btnv_b,.tp-lstv_s{background-color:var(--btn-bg);border-radius:var(--bld-br);color:var(--btn-fg);cursor:pointer;display:block;font-weight:bold;height:var(--cnt-usz);line-height:var(--cnt-usz);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.tp-p2dv_b:hover,.tp-btnv_b:hover,.tp-lstv_s:hover{background-color:var(--btn-bg-h)}.tp-p2dv_b:focus,.tp-btnv_b:focus,.tp-lstv_s:focus{background-color:var(--btn-bg-f)}.tp-p2dv_b:active,.tp-btnv_b:active,.tp-lstv_s:active{background-color:var(--btn-bg-a)}.tp-p2dv_b:disabled,.tp-btnv_b:disabled,.tp-lstv_s:disabled{opacity:.5}.tp-rotv_c>.tp-cntv.tp-v-lst,.tp-tbpv_c>.tp-cntv.tp-v-lst,.tp-fldv_c>.tp-cntv.tp-v-lst{margin-bottom:calc(-1*var(--cnt-vp))}.tp-rotv_c>.tp-fldv.tp-v-lst .tp-fldv_c,.tp-tbpv_c>.tp-fldv.tp-v-lst .tp-fldv_c,.tp-fldv_c>.tp-fldv.tp-v-lst .tp-fldv_c{border-bottom-left-radius:0}.tp-rotv_c>.tp-fldv.tp-v-lst .tp-fldv_b,.tp-tbpv_c>.tp-fldv.tp-v-lst .tp-fldv_b,.tp-fldv_c>.tp-fldv.tp-v-lst .tp-fldv_b{border-bottom-left-radius:0}.tp-rotv_c>*:not(.tp-v-fst),.tp-tbpv_c>*:not(.tp-v-fst),.tp-fldv_c>*:not(.tp-v-fst){margin-top:var(--cnt-usp)}.tp-rotv_c>.tp-sprv:not(.tp-v-fst),.tp-tbpv_c>.tp-sprv:not(.tp-v-fst),.tp-fldv_c>.tp-sprv:not(.tp-v-fst),.tp-rotv_c>.tp-cntv:not(.tp-v-fst),.tp-tbpv_c>.tp-cntv:not(.tp-v-fst),.tp-fldv_c>.tp-cntv:not(.tp-v-fst){margin-top:var(--cnt-vp)}.tp-rotv_c>.tp-sprv+*:not(.tp-v-hidden),.tp-tbpv_c>.tp-sprv+*:not(.tp-v-hidden),.tp-fldv_c>.tp-sprv+*:not(.tp-v-hidden),.tp-rotv_c>.tp-cntv+*:not(.tp-v-hidden),.tp-tbpv_c>.tp-cntv+*:not(.tp-v-hidden),.tp-fldv_c>.tp-cntv+*:not(.tp-v-hidden){margin-top:var(--cnt-vp)}.tp-rotv_c>.tp-sprv:not(.tp-v-hidden)+.tp-sprv,.tp-tbpv_c>.tp-sprv:not(.tp-v-hidden)+.tp-sprv,.tp-fldv_c>.tp-sprv:not(.tp-v-hidden)+.tp-sprv,.tp-rotv_c>.tp-cntv:not(.tp-v-hidden)+.tp-cntv,.tp-tbpv_c>.tp-cntv:not(.tp-v-hidden)+.tp-cntv,.tp-fldv_c>.tp-cntv:not(.tp-v-hidden)+.tp-cntv{margin-top:0}.tp-tbpv_c>.tp-cntv,.tp-fldv_c>.tp-cntv{margin-left:4px}.tp-tbpv_c>.tp-fldv>.tp-fldv_b,.tp-fldv_c>.tp-fldv>.tp-fldv_b{border-top-left-radius:var(--bld-br);border-bottom-left-radius:var(--bld-br)}.tp-tbpv_c>.tp-fldv.tp-fldv-expanded>.tp-fldv_b,.tp-fldv_c>.tp-fldv.tp-fldv-expanded>.tp-fldv_b{border-bottom-left-radius:0}.tp-tbpv_c .tp-fldv>.tp-fldv_c,.tp-fldv_c .tp-fldv>.tp-fldv_c{border-bottom-left-radius:var(--bld-br)}.tp-tbpv_c>.tp-cntv+.tp-fldv>.tp-fldv_b,.tp-fldv_c>.tp-cntv+.tp-fldv>.tp-fldv_b{border-top-left-radius:0}.tp-tbpv_c>.tp-cntv+.tp-tabv>.tp-tabv_t,.tp-fldv_c>.tp-cntv+.tp-tabv>.tp-tabv_t{border-top-left-radius:0}.tp-tbpv_c>.tp-tabv>.tp-tabv_t,.tp-fldv_c>.tp-tabv>.tp-tabv_t{border-top-left-radius:var(--bld-br)}.tp-tbpv_c .tp-tabv>.tp-tabv_c,.tp-fldv_c .tp-tabv>.tp-tabv_c{border-bottom-left-radius:var(--bld-br)}.tp-rotv_b,.tp-fldv_b{background-color:var(--cnt-bg);color:var(--cnt-fg);cursor:pointer;display:block;height:calc(var(--cnt-usz) + 4px);line-height:calc(var(--cnt-usz) + 4px);overflow:hidden;padding-left:var(--cnt-hp);padding-right:calc(4px + var(--cnt-usz) + var(--cnt-hp));position:relative;text-align:left;text-overflow:ellipsis;white-space:nowrap;width:100%;transition:border-radius .2s ease-in-out .2s}.tp-rotv_b:hover,.tp-fldv_b:hover{background-color:var(--cnt-bg-h)}.tp-rotv_b:focus,.tp-fldv_b:focus{background-color:var(--cnt-bg-f)}.tp-rotv_b:active,.tp-fldv_b:active{background-color:var(--cnt-bg-a)}.tp-rotv_b:disabled,.tp-fldv_b:disabled{opacity:.5}.tp-rotv_m,.tp-fldv_m{background:linear-gradient(to left, var(--cnt-fg), var(--cnt-fg) 2px, transparent 2px, transparent 4px, var(--cnt-fg) 4px);border-radius:2px;bottom:0;content:"";display:block;height:6px;right:calc(var(--cnt-hp) + (var(--cnt-usz) + 4px - 6px)/2 - 2px);margin:auto;opacity:.5;position:absolute;top:0;transform:rotate(90deg);transition:transform .2s ease-in-out;width:6px}.tp-rotv.tp-rotv-expanded .tp-rotv_m,.tp-fldv.tp-fldv-expanded>.tp-fldv_b>.tp-fldv_m{transform:none}.tp-rotv_c,.tp-fldv_c{box-sizing:border-box;height:0;opacity:0;overflow:hidden;padding-bottom:0;padding-top:0;position:relative;transition:height .2s ease-in-out,opacity .2s linear,padding .2s ease-in-out}.tp-rotv.tp-rotv-cpl:not(.tp-rotv-expanded) .tp-rotv_c,.tp-fldv.tp-fldv-cpl:not(.tp-fldv-expanded)>.tp-fldv_c{display:none}.tp-rotv.tp-rotv-expanded .tp-rotv_c,.tp-fldv.tp-fldv-expanded>.tp-fldv_c{opacity:1;padding-bottom:var(--cnt-vp);padding-top:var(--cnt-vp);transform:none;overflow:visible;transition:height .2s ease-in-out,opacity .2s linear .2s,padding .2s ease-in-out}.tp-txtv_i,.tp-p2dpv_p,.tp-colswv_sw{background-color:var(--in-bg);border-radius:var(--bld-br);box-sizing:border-box;color:var(--in-fg);font-family:inherit;height:var(--cnt-usz);line-height:var(--cnt-usz);min-width:0;width:100%}.tp-txtv_i:hover,.tp-p2dpv_p:hover,.tp-colswv_sw:hover{background-color:var(--in-bg-h)}.tp-txtv_i:focus,.tp-p2dpv_p:focus,.tp-colswv_sw:focus{background-color:var(--in-bg-f)}.tp-txtv_i:active,.tp-p2dpv_p:active,.tp-colswv_sw:active{background-color:var(--in-bg-a)}.tp-txtv_i:disabled,.tp-p2dpv_p:disabled,.tp-colswv_sw:disabled{opacity:.5}.tp-lstv,.tp-coltxtv_m{position:relative}.tp-lstv_s{padding:0 20px 0 4px;width:100%}.tp-lstv_m,.tp-coltxtv_mm{bottom:0;margin:auto;pointer-events:none;position:absolute;right:2px;top:0}.tp-lstv_m svg,.tp-coltxtv_mm svg{bottom:0;height:16px;margin:auto;position:absolute;right:0;top:0;width:16px}.tp-lstv_m svg path,.tp-coltxtv_mm svg path{fill:currentColor}.tp-sglv_i,.tp-mllv_i,.tp-grlv_g{background-color:var(--mo-bg);border-radius:var(--bld-br);box-sizing:border-box;color:var(--mo-fg);height:var(--cnt-usz);scrollbar-color:currentColor rgba(0,0,0,0);scrollbar-width:thin;width:100%}.tp-sglv_i::-webkit-scrollbar,.tp-mllv_i::-webkit-scrollbar,.tp-grlv_g::-webkit-scrollbar{height:8px;width:8px}.tp-sglv_i::-webkit-scrollbar-corner,.tp-mllv_i::-webkit-scrollbar-corner,.tp-grlv_g::-webkit-scrollbar-corner{background-color:rgba(0,0,0,0)}.tp-sglv_i::-webkit-scrollbar-thumb,.tp-mllv_i::-webkit-scrollbar-thumb,.tp-grlv_g::-webkit-scrollbar-thumb{background-clip:padding-box;background-color:currentColor;border:rgba(0,0,0,0) solid 2px;border-radius:4px}.tp-pndtxtv,.tp-coltxtv_w{display:flex}.tp-pndtxtv_a,.tp-coltxtv_c{width:100%}.tp-pndtxtv_a+.tp-pndtxtv_a,.tp-coltxtv_c+.tp-pndtxtv_a,.tp-pndtxtv_a+.tp-coltxtv_c,.tp-coltxtv_c+.tp-coltxtv_c{margin-left:2px}.tp-rotv{--bs-bg: var(--tp-base-background-color, hsl(230, 7%, 17%));--bs-br: var(--tp-base-border-radius, 6px);--bs-ff: var(--tp-base-font-family, Roboto Mono, Source Code Pro, Menlo, Courier, monospace);--bs-sh: var(--tp-base-shadow-color, rgba(0, 0, 0, 0.2));--bld-br: var(--tp-blade-border-radius, 2px);--bld-hp: var(--tp-blade-horizontal-padding, 4px);--bld-vw: var(--tp-blade-value-width, 160px);--btn-bg: var(--tp-button-background-color, hsl(230, 7%, 70%));--btn-bg-a: var(--tp-button-background-color-active, #d6d7db);--btn-bg-f: var(--tp-button-background-color-focus, #c8cad0);--btn-bg-h: var(--tp-button-background-color-hover, #bbbcc4);--btn-fg: var(--tp-button-foreground-color, hsl(230, 7%, 17%));--cnt-bg: var(--tp-container-background-color, rgba(187, 188, 196, 0.1));--cnt-bg-a: var(--tp-container-background-color-active, rgba(187, 188, 196, 0.25));--cnt-bg-f: var(--tp-container-background-color-focus, rgba(187, 188, 196, 0.2));--cnt-bg-h: var(--tp-container-background-color-hover, rgba(187, 188, 196, 0.15));--cnt-fg: var(--tp-container-foreground-color, hsl(230, 7%, 75%));--cnt-hp: var(--tp-container-horizontal-padding, 4px);--cnt-vp: var(--tp-container-vertical-padding, 4px);--cnt-usp: var(--tp-container-unit-spacing, 4px);--cnt-usz: var(--tp-container-unit-size, 20px);--in-bg: var(--tp-input-background-color, rgba(187, 188, 196, 0.1));--in-bg-a: var(--tp-input-background-color-active, rgba(187, 188, 196, 0.25));--in-bg-f: var(--tp-input-background-color-focus, rgba(187, 188, 196, 0.2));--in-bg-h: var(--tp-input-background-color-hover, rgba(187, 188, 196, 0.15));--in-fg: var(--tp-input-foreground-color, hsl(230, 7%, 75%));--lbl-fg: var(--tp-label-foreground-color, rgba(187, 188, 196, 0.7));--mo-bg: var(--tp-monitor-background-color, rgba(0, 0, 0, 0.2));--mo-fg: var(--tp-monitor-foreground-color, rgba(187, 188, 196, 0.7));--grv-fg: var(--tp-groove-foreground-color, rgba(187, 188, 196, 0.1))}.tp-btnv_b{width:100%}.tp-btnv_t{text-align:center}.tp-ckbv_l{display:block;position:relative}.tp-ckbv_i{left:0;opacity:0;position:absolute;top:0}.tp-ckbv_w{background-color:var(--in-bg);border-radius:var(--bld-br);cursor:pointer;display:block;height:var(--cnt-usz);position:relative;width:var(--cnt-usz)}.tp-ckbv_w svg{display:block;height:16px;inset:0;margin:auto;opacity:0;position:absolute;width:16px}.tp-ckbv_w svg path{fill:none;stroke:var(--in-fg);stroke-width:2}.tp-ckbv_i:hover+.tp-ckbv_w{background-color:var(--in-bg-h)}.tp-ckbv_i:focus+.tp-ckbv_w{background-color:var(--in-bg-f)}.tp-ckbv_i:active+.tp-ckbv_w{background-color:var(--in-bg-a)}.tp-ckbv_i:checked+.tp-ckbv_w svg{opacity:1}.tp-ckbv.tp-v-disabled .tp-ckbv_w{opacity:.5}.tp-colv{position:relative}.tp-colv_h{display:flex}.tp-colv_s{flex-grow:0;flex-shrink:0;width:var(--cnt-usz)}.tp-colv_t{flex:1;margin-left:4px}.tp-colv_p{height:0;margin-top:0;opacity:0;overflow:hidden;transition:height .2s ease-in-out,opacity .2s linear,margin .2s ease-in-out}.tp-colv.tp-colv-expanded.tp-colv-cpl .tp-colv_p{overflow:visible}.tp-colv.tp-colv-expanded .tp-colv_p{margin-top:var(--cnt-usp);opacity:1}.tp-colv .tp-popv{left:calc(-1*var(--cnt-hp));right:calc(-1*var(--cnt-hp));top:var(--cnt-usz)}.tp-colpv_h,.tp-colpv_ap{margin-left:6px;margin-right:6px}.tp-colpv_h{margin-top:var(--cnt-usp)}.tp-colpv_rgb{display:flex;margin-top:var(--cnt-usp);width:100%}.tp-colpv_a{display:flex;margin-top:var(--cnt-vp);padding-top:calc(var(--cnt-vp) + 2px);position:relative}.tp-colpv_a::before{background-color:var(--grv-fg);content:"";height:2px;left:calc(-1*var(--cnt-hp));position:absolute;right:calc(-1*var(--cnt-hp));top:0}.tp-colpv.tp-v-disabled .tp-colpv_a::before{opacity:.5}.tp-colpv_ap{align-items:center;display:flex;flex:3}.tp-colpv_at{flex:1;margin-left:4px}.tp-svpv{border-radius:var(--bld-br);outline:none;overflow:hidden;position:relative}.tp-svpv.tp-v-disabled{opacity:.5}.tp-svpv_c{cursor:crosshair;display:block;height:calc(var(--cnt-usz)*4);width:100%}.tp-svpv_m{border-radius:100%;border:rgba(255,255,255,.75) solid 2px;box-sizing:border-box;filter:drop-shadow(0 0 1px rgba(0, 0, 0, 0.3));height:12px;margin-left:-6px;margin-top:-6px;pointer-events:none;position:absolute;width:12px}.tp-svpv:focus .tp-svpv_m{border-color:#fff}.tp-hplv{cursor:pointer;height:var(--cnt-usz);outline:none;position:relative}.tp-hplv.tp-v-disabled{opacity:.5}.tp-hplv_c{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAAABCAYAAABubagXAAAAQ0lEQVQoU2P8z8Dwn0GCgQEDi2OK/RBgYHjBgIpfovFh8j8YBIgzFGQxuqEgPhaDOT5gOhPkdCxOZeBg+IDFZZiGAgCaSSMYtcRHLgAAAABJRU5ErkJggg==);background-position:left top;background-repeat:no-repeat;background-size:100% 100%;border-radius:2px;display:block;height:4px;left:0;margin-top:-2px;position:absolute;top:50%;width:100%}.tp-hplv_m{border-radius:var(--bld-br);border:rgba(255,255,255,.75) solid 2px;box-shadow:0 0 2px rgba(0,0,0,.1);box-sizing:border-box;height:12px;left:50%;margin-left:-6px;margin-top:-6px;position:absolute;top:50%;width:12px}.tp-hplv:focus .tp-hplv_m{border-color:#fff}.tp-aplv{cursor:pointer;height:var(--cnt-usz);outline:none;position:relative;width:100%}.tp-aplv.tp-v-disabled{opacity:.5}.tp-aplv_b{background-color:#fff;background-image:linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%),linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%);background-size:4px 4px;background-position:0 0,2px 2px;border-radius:2px;display:block;height:4px;left:0;margin-top:-2px;overflow:hidden;position:absolute;top:50%;width:100%}.tp-aplv_c{inset:0;position:absolute}.tp-aplv_m{background-color:#fff;background-image:linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%),linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%);background-size:12px 12px;background-position:0 0,6px 6px;border-radius:var(--bld-br);box-shadow:0 0 2px rgba(0,0,0,.1);height:12px;left:50%;margin-left:-6px;margin-top:-6px;overflow:hidden;position:absolute;top:50%;width:12px}.tp-aplv_p{border-radius:var(--bld-br);border:rgba(255,255,255,.75) solid 2px;box-sizing:border-box;inset:0;position:absolute}.tp-aplv:focus .tp-aplv_p{border-color:#fff}.tp-colswv{background-color:#fff;background-image:linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%),linear-gradient(to top right, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%);background-size:10px 10px;background-position:0 0,5px 5px;border-radius:var(--bld-br);overflow:hidden}.tp-colswv.tp-v-disabled{opacity:.5}.tp-colswv_sw{border-radius:0}.tp-colswv_b{cursor:pointer;display:block;height:var(--cnt-usz);left:0;position:absolute;top:0;width:var(--cnt-usz)}.tp-colswv_b:focus::after{border:rgba(255,255,255,.75) solid 2px;border-radius:var(--bld-br);content:"";display:block;inset:0;position:absolute}.tp-coltxtv{display:flex;width:100%}.tp-coltxtv_m{margin-right:4px}.tp-coltxtv_ms{border-radius:var(--bld-br);color:var(--lbl-fg);cursor:pointer;height:var(--cnt-usz);line-height:var(--cnt-usz);padding:0 18px 0 4px}.tp-coltxtv_ms:hover{background-color:var(--in-bg-h)}.tp-coltxtv_ms:focus{background-color:var(--in-bg-f)}.tp-coltxtv_ms:active{background-color:var(--in-bg-a)}.tp-coltxtv_mm{color:var(--lbl-fg)}.tp-coltxtv.tp-v-disabled .tp-coltxtv_mm{opacity:.5}.tp-coltxtv_w{flex:1}.tp-dfwv{position:absolute;top:8px;right:8px;width:256px}.tp-fldv{position:relative}.tp-fldv_t{padding-left:4px}.tp-fldv_b:disabled .tp-fldv_m{display:none}.tp-fldv_c{padding-left:4px}.tp-fldv_i{bottom:0;color:var(--cnt-bg);left:0;overflow:hidden;position:absolute;top:calc(var(--cnt-usz) + 4px);width:max(var(--bs-br),4px)}.tp-fldv_i::before{background-color:currentColor;bottom:0;content:"";left:0;position:absolute;top:0;width:4px}.tp-fldv_b:hover+.tp-fldv_i{color:var(--cnt-bg-h)}.tp-fldv_b:focus+.tp-fldv_i{color:var(--cnt-bg-f)}.tp-fldv_b:active+.tp-fldv_i{color:var(--cnt-bg-a)}.tp-fldv.tp-v-disabled>.tp-fldv_i{opacity:.5}.tp-grlv{position:relative}.tp-grlv_g{display:block;height:calc(var(--cnt-usz)*3)}.tp-grlv_g polyline{fill:none;stroke:var(--mo-fg);stroke-linejoin:round}.tp-grlv_t{margin-top:-4px;transition:left .05s,top .05s;visibility:hidden}.tp-grlv_t.tp-grlv_t-a{visibility:visible}.tp-grlv_t.tp-grlv_t-in{transition:none}.tp-grlv.tp-v-disabled .tp-grlv_g{opacity:.5}.tp-grlv .tp-ttv{background-color:var(--mo-fg)}.tp-grlv .tp-ttv::before{border-top-color:var(--mo-fg)}.tp-lblv{align-items:center;display:flex;line-height:1.3;padding-left:var(--cnt-hp);padding-right:var(--cnt-hp)}.tp-lblv.tp-lblv-nol{display:block}.tp-lblv_l{color:var(--lbl-fg);flex:1;-webkit-hyphens:auto;hyphens:auto;overflow:hidden;padding-left:4px;padding-right:16px}.tp-lblv.tp-v-disabled .tp-lblv_l{opacity:.5}.tp-lblv.tp-lblv-nol .tp-lblv_l{display:none}.tp-lblv_v{align-self:flex-start;flex-grow:0;flex-shrink:0;width:var(--bld-vw)}.tp-lblv.tp-lblv-nol .tp-lblv_v{width:100%}.tp-lstv_s{padding:0 20px 0 var(--bld-hp);width:100%}.tp-lstv_m{color:var(--btn-fg)}.tp-sglv_i{padding-left:var(--bld-hp);padding-right:var(--bld-hp)}.tp-sglv.tp-v-disabled .tp-sglv_i{opacity:.5}.tp-mllv_i{display:block;height:calc(var(--cnt-usz)*3);line-height:var(--cnt-usz);padding-left:var(--bld-hp);padding-right:var(--bld-hp);resize:none;white-space:pre}.tp-mllv.tp-v-disabled .tp-mllv_i{opacity:.5}.tp-p2dv{position:relative}.tp-p2dv_h{display:flex}.tp-p2dv_b{height:var(--cnt-usz);margin-right:4px;position:relative;width:var(--cnt-usz)}.tp-p2dv_b svg{display:block;height:16px;left:50%;margin-left:-8px;margin-top:-8px;position:absolute;top:50%;width:16px}.tp-p2dv_b svg path{stroke:currentColor;stroke-width:2}.tp-p2dv_b svg circle{fill:currentColor}.tp-p2dv_t{flex:1}.tp-p2dv_p{height:0;margin-top:0;opacity:0;overflow:hidden;transition:height .2s ease-in-out,opacity .2s linear,margin .2s ease-in-out}.tp-p2dv.tp-p2dv-expanded .tp-p2dv_p{margin-top:var(--cnt-usp);opacity:1}.tp-p2dv .tp-popv{left:calc(-1*var(--cnt-hp));right:calc(-1*var(--cnt-hp));top:var(--cnt-usz)}.tp-p2dpv{padding-left:calc(var(--cnt-usz) + 4px)}.tp-p2dpv_p{cursor:crosshair;height:0;overflow:hidden;padding-bottom:100%;position:relative}.tp-p2dpv.tp-v-disabled .tp-p2dpv_p{opacity:.5}.tp-p2dpv_g{display:block;height:100%;left:0;pointer-events:none;position:absolute;top:0;width:100%}.tp-p2dpv_ax{opacity:.1;stroke:var(--in-fg);stroke-dasharray:1}.tp-p2dpv_l{opacity:.5;stroke:var(--in-fg);stroke-dasharray:1}.tp-p2dpv_m{border:var(--in-fg) solid 1px;border-radius:50%;box-sizing:border-box;height:4px;margin-left:-2px;margin-top:-2px;position:absolute;width:4px}.tp-p2dpv_p:focus .tp-p2dpv_m{background-color:var(--in-fg);border-width:0}.tp-popv{background-color:var(--bs-bg);border-radius:var(--bs-br);box-shadow:0 2px 4px var(--bs-sh);display:none;max-width:var(--bld-vw);padding:var(--cnt-vp) var(--cnt-hp);position:absolute;visibility:hidden;z-index:1000}.tp-popv.tp-popv-v{display:block;visibility:visible}.tp-sldv.tp-v-disabled{opacity:.5}.tp-sldv_t{box-sizing:border-box;cursor:pointer;height:var(--cnt-usz);margin:0 6px;outline:none;position:relative}.tp-sldv_t::before{background-color:var(--in-bg);border-radius:1px;content:"";display:block;height:2px;inset:0;margin:auto;position:absolute}.tp-sldv_k{height:100%;left:0;position:absolute;top:0}.tp-sldv_k::before{background-color:var(--in-fg);border-radius:1px;content:"";display:block;height:2px;inset:0;margin-bottom:auto;margin-top:auto;position:absolute}.tp-sldv_k::after{background-color:var(--btn-bg);border-radius:var(--bld-br);bottom:0;content:"";display:block;height:12px;margin-bottom:auto;margin-top:auto;position:absolute;right:-6px;top:0;width:12px}.tp-sldv_t:hover .tp-sldv_k::after{background-color:var(--btn-bg-h)}.tp-sldv_t:focus .tp-sldv_k::after{background-color:var(--btn-bg-f)}.tp-sldv_t:active .tp-sldv_k::after{background-color:var(--btn-bg-a)}.tp-sldtxtv{display:flex}.tp-sldtxtv_s{flex:2}.tp-sldtxtv_t{flex:1;margin-left:4px}.tp-tabv{position:relative}.tp-tabv_t{align-items:flex-end;color:var(--cnt-bg);display:flex;overflow:hidden;position:relative}.tp-tabv_t:hover{color:var(--cnt-bg-h)}.tp-tabv_t:has(*:focus){color:var(--cnt-bg-f)}.tp-tabv_t:has(*:active){color:var(--cnt-bg-a)}.tp-tabv_t::before{background-color:currentColor;bottom:0;content:"";height:2px;left:0;pointer-events:none;position:absolute;right:0}.tp-tabv.tp-v-disabled .tp-tabv_t::before{opacity:.5}.tp-tabv.tp-tabv-nop .tp-tabv_t{height:calc(var(--cnt-usz) + 4px);position:relative}.tp-tabv.tp-tabv-nop .tp-tabv_t::before{background-color:var(--cnt-bg);bottom:0;content:"";height:2px;left:0;position:absolute;right:0}.tp-tabv_i{bottom:0;color:var(--cnt-bg);left:0;overflow:hidden;position:absolute;top:calc(var(--cnt-usz) + 4px);width:max(var(--bs-br),4px)}.tp-tabv_i::before{background-color:currentColor;bottom:0;content:"";left:0;position:absolute;top:0;width:4px}.tp-tabv_t:hover+.tp-tabv_i{color:var(--cnt-bg-h)}.tp-tabv_t:has(*:focus)+.tp-tabv_i{color:var(--cnt-bg-f)}.tp-tabv_t:has(*:active)+.tp-tabv_i{color:var(--cnt-bg-a)}.tp-tabv.tp-v-disabled>.tp-tabv_i{opacity:.5}.tp-tbiv{flex:1;min-width:0;position:relative}.tp-tbiv+.tp-tbiv{margin-left:2px}.tp-tbiv+.tp-tbiv.tp-v-disabled::before{opacity:.5}.tp-tbiv_b{display:block;padding-left:calc(var(--cnt-hp) + 4px);padding-right:calc(var(--cnt-hp) + 4px);position:relative;width:100%}.tp-tbiv_b:disabled{opacity:.5}.tp-tbiv_b::before{background-color:var(--cnt-bg);content:"";inset:0 0 2px;pointer-events:none;position:absolute}.tp-tbiv_b:hover::before{background-color:var(--cnt-bg-h)}.tp-tbiv_b:focus::before{background-color:var(--cnt-bg-f)}.tp-tbiv_b:active::before{background-color:var(--cnt-bg-a)}.tp-tbiv_t{color:var(--cnt-fg);height:calc(var(--cnt-usz) + 4px);line-height:calc(var(--cnt-usz) + 4px);opacity:.5;overflow:hidden;position:relative;text-overflow:ellipsis}.tp-tbiv.tp-tbiv-sel .tp-tbiv_t{opacity:1}.tp-tbpv_c{padding-bottom:var(--cnt-vp);padding-left:4px;padding-top:var(--cnt-vp)}.tp-txtv{position:relative}.tp-txtv_i{padding-left:var(--bld-hp);padding-right:var(--bld-hp)}.tp-txtv.tp-txtv-fst .tp-txtv_i{border-bottom-right-radius:0;border-top-right-radius:0}.tp-txtv.tp-txtv-mid .tp-txtv_i{border-radius:0}.tp-txtv.tp-txtv-lst .tp-txtv_i{border-bottom-left-radius:0;border-top-left-radius:0}.tp-txtv.tp-txtv-num .tp-txtv_i{text-align:right}.tp-txtv.tp-txtv-drg .tp-txtv_i{opacity:.3}.tp-txtv_k{cursor:pointer;height:100%;left:calc(var(--bld-hp) - 5px);position:absolute;top:0;width:12px}.tp-txtv_k::before{background-color:var(--in-fg);border-radius:1px;bottom:0;content:"";height:calc(var(--cnt-usz) - 4px);left:50%;margin-bottom:auto;margin-left:-1px;margin-top:auto;opacity:.1;position:absolute;top:0;transition:border-radius .1s,height .1s,transform .1s,width .1s;width:2px}.tp-txtv_k:hover::before,.tp-txtv.tp-txtv-drg .tp-txtv_k::before{opacity:1}.tp-txtv.tp-txtv-drg .tp-txtv_k::before{border-radius:50%;height:4px;transform:translateX(-1px);width:4px}.tp-txtv_g{bottom:0;display:block;height:8px;left:50%;margin:auto;overflow:visible;pointer-events:none;position:absolute;top:0;visibility:hidden;width:100%}.tp-txtv.tp-txtv-drg .tp-txtv_g{visibility:visible}.tp-txtv_gb{fill:none;stroke:var(--in-fg);stroke-dasharray:1}.tp-txtv_gh{fill:none;stroke:var(--in-fg)}.tp-txtv .tp-ttv{margin-left:6px;visibility:hidden}.tp-txtv.tp-txtv-drg .tp-ttv{visibility:visible}.tp-ttv{background-color:var(--in-fg);border-radius:var(--bld-br);color:var(--bs-bg);padding:2px 4px;pointer-events:none;position:absolute;transform:translate(-50%, -100%)}.tp-ttv::before{border-color:var(--in-fg) rgba(0,0,0,0) rgba(0,0,0,0) rgba(0,0,0,0);border-style:solid;border-width:2px;box-sizing:border-box;content:"";font-size:.9em;height:4px;left:50%;margin-left:-2px;position:absolute;top:100%;width:4px}.tp-rotv{background-color:var(--bs-bg);border-radius:var(--bs-br);box-shadow:0 2px 4px var(--bs-sh);font-family:var(--bs-ff);font-size:11px;font-weight:500;line-height:1;text-align:left}.tp-rotv_b{border-bottom-left-radius:var(--bs-br);border-bottom-right-radius:var(--bs-br);border-top-left-radius:var(--bs-br);border-top-right-radius:var(--bs-br);padding-left:calc(4px + var(--cnt-usz) + var(--cnt-hp));text-align:center}.tp-rotv.tp-rotv-expanded .tp-rotv_b{border-bottom-left-radius:0;border-bottom-right-radius:0;transition-delay:0s;transition-duration:0s}.tp-rotv.tp-rotv-not>.tp-rotv_b{display:none}.tp-rotv_b:disabled .tp-rotv_m{display:none}.tp-rotv_c>.tp-fldv.tp-v-lst>.tp-fldv_c{border-bottom-left-radius:var(--bs-br);border-bottom-right-radius:var(--bs-br)}.tp-rotv_c>.tp-fldv.tp-v-lst>.tp-fldv_i{border-bottom-left-radius:var(--bs-br)}.tp-rotv_c>.tp-fldv.tp-v-lst:not(.tp-fldv-expanded)>.tp-fldv_b{border-bottom-left-radius:var(--bs-br);border-bottom-right-radius:var(--bs-br)}.tp-rotv_c>.tp-fldv.tp-v-lst.tp-fldv-expanded>.tp-fldv_b{transition-delay:0s;transition-duration:0s}.tp-rotv_c .tp-fldv.tp-v-vlst:not(.tp-fldv-expanded)>.tp-fldv_b{border-bottom-right-radius:var(--bs-br)}.tp-rotv.tp-rotv-not .tp-rotv_c>.tp-fldv.tp-v-fst{margin-top:calc(-1*var(--cnt-vp))}.tp-rotv.tp-rotv-not .tp-rotv_c>.tp-fldv.tp-v-fst>.tp-fldv_b{border-top-left-radius:var(--bs-br);border-top-right-radius:var(--bs-br)}.tp-rotv_c>.tp-tabv.tp-v-lst>.tp-tabv_c{border-bottom-left-radius:var(--bs-br);border-bottom-right-radius:var(--bs-br)}.tp-rotv_c>.tp-tabv.tp-v-lst>.tp-tabv_i{border-bottom-left-radius:var(--bs-br)}.tp-rotv.tp-rotv-not .tp-rotv_c>.tp-tabv.tp-v-fst{margin-top:calc(-1*var(--cnt-vp))}.tp-rotv.tp-rotv-not .tp-rotv_c>.tp-tabv.tp-v-fst>.tp-tabv_t{border-top-left-radius:var(--bs-br);border-top-right-radius:var(--bs-br)}.tp-rotv.tp-v-disabled,.tp-rotv .tp-v-disabled{pointer-events:none}.tp-rotv.tp-v-hidden,.tp-rotv .tp-v-hidden{display:none}.tp-sprv_r{background-color:var(--grv-fg);border-width:0;display:block;height:2px;margin:0;width:100%}.tp-sprv.tp-v-disabled .tp-sprv_r{opacity:.5}',
plugins: [
ListBladePlugin,
SeparatorBladePlugin,
SliderBladePlugin,
TabBladePlugin,
TextBladePlugin,
],
});
}
}
const VERSION = new Semver('4.0.5');
const DevTools = {
pane: null,
folders: /* @__PURE__ */ new Map(),
storageName: null,
filePickerOpts: {
types: [{
description: "Parameters file",
accept: { "text/plain": [".json"] }
}]
},
addTo(map) {
DevTools.storageName = `mapbox:devtools:${location.pathname}`;
const positionContainer = map._controlPositions["top-right"];
const container = create$1("div", "mapboxgl-ctrl mapbox-devTools", positionContainer);
container.style.maxHeight = "75vh";
container.style.overflowY = "auto";
const pane = new Pane({
title: "devtools",
container,
expanded: false
});
const saved = localStorage.getItem(DevTools.storageName);
if (saved) pane.importState(JSON.parse(saved));
pane.on("fold", () => DevTools.saveState());
pane.addButton({ title: "Reset" }).on("click", () => DevTools.reset());
pane.addButton({ title: "Fold all" }).on("click", () => DevTools.setExpanded(false));
pane.addButton({ title: "Unfold all" }).on("click", () => DevTools.setExpanded(true));
pane.addButton({ title: "Save to file" }).on("click", () => DevTools.saveParameters());
pane.addButton({ title: "Load from file" }).on("click", () => DevTools.loadParameters());
DevTools.pane = pane;
},
saveState() {
if (!DevTools.pane) return;
const state = DevTools.pane.exportState();
localStorage.setItem(DevTools.storageName, JSON.stringify(state));
},
setExpanded(expanded) {
DevTools.folders.forEach((folder) => {
folder.api.expanded = expanded;
folder.api.refresh();
});
DevTools.saveState();
},
reset() {
if (!DevTools.pane) return;
DevTools.folders.forEach((folder) => {
folder.bindings.forEach((metadata, key) => {
metadata.target[key] = structuredClone(metadata.defaultValue);
});
folder.api.refresh();
});
DevTools.updateIndicators();
DevTools.saveState();
},
updateIndicators() {
DevTools.folders.forEach((folder, folderName) => {
let isModified = false;
folder.bindings.forEach((metadata, key) => {
const currentValue = metadata.target[key];
const isDefault = JSON.stringify(metadata.defaultValue) === JSON.stringify(currentValue);
if (!isDefault) isModified = true;
const prefix = isDefault ? "\u25CB " : "\u25CF ";
metadata.binding.label = prefix + metadata.label;
});
folder.api.title = isModified ? `\u25CF ${folderName}` : folderName;
});
},
saveParameters() {
if (!DevTools.pane || !window.showSaveFilePicker) return;
const state = DevTools.pane.exportState();
const serialized = JSON.stringify(state, null, 2);
window.showSaveFilePicker(DevTools.filePickerOpts).then((fileHandle) => fileHandle.createWritable()).then((writable) => writable.write(serialized).then(() => writable)).then((writable) => writable.close().catch(() => {
})).catch((err) => console.warn("Failed to save parameters:", err));
},
loadParameters() {
if (!DevTools.pane || !window.showOpenFilePicker) return;
window.showOpenFilePicker(DevTools.filePickerOpts).then((fileHandles) => fileHandles[0].getFile()).then((file) => file.text()).then((fileData) => {
const state = JSON.parse(fileData);
DevTools.pane.importState(state);
DevTools.updateIndicators();
DevTools.folders.forEach((folder) => folder.api.refresh());
}).catch((err) => console.warn("Failed to load parameters:", err));
},
getOrCreateFolder(folderName) {
if (!DevTools.pane) return null;
let folder = DevTools.folders.get(folderName);
if (folder) return folder;
const api = DevTools.pane.addFolder({ title: folderName, expanded: true });
folder = { api, bindings: /* @__PURE__ */ new Map() };
DevTools.folders.set(folderName, folder);
api.on("fold", () => DevTools.saveState());
return folder;
},
addParameter(target, key, folderName, params, callback) {
if (!DevTools.pane) return;
const folder = DevTools.getOrCreateFolder(folderName);
if (folder.bindings.has(key)) console.warn(`Parameter "${folderName}/${String(key)}" already registered`);
const defaultValue = structuredClone(target[key]);
const binding = folder.api.addBinding(target, key, params);
const saved = localStorage.getItem(DevTools.storageName);
if (saved) DevTools.pane.importState(JSON.parse(saved));
binding.on("change", (ev) => {
DevTools.saveState();
DevTools.updateIndicators();
if (callback) callback(ev.value);
});
folder.bindings.set(key, {
defaultValue,
binding,
label: binding.label || key.toString(),
callback,
target
});
DevTools.updateIndicators();
},
addBinding(target, key, folderName, params) {
if (!DevTools.pane) return;
const folder = DevTools.getOrCreateFolder(folderName);
folder.api.addBinding(target, key, params);
},
addButton(folderName, buttonTitle, onClick) {
if (!DevTools.pane) return;
const folder = DevTools.getOrCreateFolder(folderName);
const button = folder.api.addButton({ title: buttonTitle });
button.on("click", () => onClick());
},
refresh() {
DevTools.folders.forEach((folder) => folder.api.refresh());
}
};
const shadowUniforms = (context) => ({
"u_light_matrix_0": new index$1.UniformMatrix4f(context),
"u_light_matrix_1": new index$1.UniformMatrix4f(context),
"u_fade_range": new index$1.Uniform2f(context),
"u_shadow_normal_offset": new index$1.Uniform3f(context),
"u_shadow_intensity": new index$1.Uniform1f(context),
"u_shadow_texel_size": new index$1.Uniform1f(context),
"u_shadow_map_resolution": new index$1.Uniform1f(context),
"u_shadow_direction": new index$1.Uniform3f(context),
"u_shadow_bias": new index$1.Uniform3f(context),
"u_shadowmap_0": new index$1.Uniform1i(context),
"u_shadowmap_1": new index$1.Uniform1i(context)
});
function defaultShadowUniformValues() {
return {
"u_light_matrix_0": new Float32Array(16),
"u_light_matrix_1": new Float32Array(16),
"u_shadow_intensity": 0,
"u_fade_range": [0, 0],
"u_shadow_normal_offset": [1, 1, 1],
"u_shadow_texel_size": 1,
"u_shadow_map_resolution": 1,
"u_shadow_direction": [0, 0, 1],
"u_shadow_bias": [36e-5, 12e-4, 0.012],
"u_shadowmap_0": 0,
"u_shadowmap_1": 0
};
}
const TextureSlots = {
BaseColor: 5,
MetallicRoughness: 6,
Normal: 7,
Occlusion: 8,
Emission: 9,
LUT: 10,
ShadowMap0: 11
};
const groundShadowUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context)
});
const groundShadowUniformValues = (matrix, shadowFactor) => ({
"u_matrix": matrix,
"u_ground_shadow_factor": shadowFactor
});
class EdgeInsets {
constructor(top = 0, bottom = 0, left = 0, right = 0) {
if (isNaN(top) || top < 0 || isNaN(bottom) || bottom < 0 || isNaN(left) || left < 0 || isNaN(right) || right < 0) {
throw new Error("Invalid value for edge-insets, top, bottom, left and right must all be numbers");
}
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}
/**
* Interpolates the inset in-place.
* This maintains the current inset value for any inset not present in `target`.
*
* @private
* @param {PaddingOptions | EdgeInsets} start The initial padding options.
* @param {PaddingOptions} target The target padding options.
* @param {number} t The interpolation variable.
* @returns {EdgeInsets} The interpolated edge insets.
* @memberof EdgeInsets
*/
interpolate(start, target, t) {
if (target.top != null && start.top != null) this.top = index$1.number(start.top, target.top, t);
if (target.bottom != null && start.bottom != null) this.bottom = index$1.number(start.bottom, target.bottom, t);
if (target.left != null && start.left != null) this.left = index$1.number(start.left, target.left, t);
if (target.right != null && start.right != null) this.right = index$1.number(start.right, target.right, t);
return this;
}
/**
* Utility method that computes the new apprent center or vanishing point after applying insets.
* This is in pixels and with the top left being (0.0) and +y being downwards.
*
* @private
* @param {number} width The width of the map in pixels.
* @param {number} height The height of the map in pixels.
* @returns {Point} The apparent center or vanishing point of the map.
* @memberof EdgeInsets
*/
getCenter(width, height) {
const x = index$1.clamp((this.left + width - this.right) / 2, 0, width);
const y = index$1.clamp((this.top + height - this.bottom) / 2, 0, height);
return new index$1.Point(x, y);
}
equals(other) {
return this.top === other.top && this.bottom === other.bottom && this.left === other.left && this.right === other.right;
}
clone() {
return new EdgeInsets(this.top, this.bottom, this.left, this.right);
}
/**
* Returns the current state as json, useful when you want to have a
* read-only representation of the inset.
*
* @private
* @returns {PaddingOptions} The current padding options.
* @memberof EdgeInsets
*/
toJSON() {
return {
top: this.top,
bottom: this.bottom,
left: this.left,
right: this.right
};
}
}
const NUM_WORLD_COPIES = 3;
const DEFAULT_MIN_ZOOM = 0;
const DEFAULT_MAX_ZOOM = 25.5;
const MIN_LOD_PITCH = 60;
const OrthographicPitchTranstionValue = 15;
const lerpMatrix = (out, a, b, value) => {
for (let i = 0; i < 16; i++) {
out[i] = index$1.number(a[i], b[i], value);
}
return out;
};
const QuadrantVisibility = {
None: 0,
TopLeft: 1,
TopRight: 2,
BottomLeft: 4,
BottomRight: 8,
All: 15
};
class Transform {
constructor(minZoom, maxZoom, minPitch, maxPitch, renderWorldCopies, projection, bounds) {
this.tileSize = 512;
this._renderWorldCopies = renderWorldCopies === void 0 ? true : renderWorldCopies;
this._minZoom = minZoom || DEFAULT_MIN_ZOOM;
this._maxZoom = maxZoom || 22;
this._minPitch = minPitch === void 0 || minPitch === null ? 0 : minPitch;
this._maxPitch = maxPitch === void 0 || maxPitch === null ? 60 : maxPitch;
this.setProjection(projection);
this.setMaxBounds(bounds);
this.width = 0;
this.height = 0;
this._center = new index$1.LngLat(0, 0);
this.zoom = 0;
this.angle = 0;
this._fov = 0.6435011087932844;
this._pitch = 0;
this._nearZ = 0;
this._farZ = 0;
this._unmodified = true;
this._edgeInsets = new EdgeInsets();
this._projMatrixCache = {};
this._alignedProjMatrixCache = {};
this._fogTileMatrixCache = {};
this._expandedProjMatrixCache = {};
this._distanceTileDataCache = {};
this._camera = new FreeCamera();
this._centerAltitude = 0;
this._averageElevation = 0;
this.cameraElevationReference = "ground";
this._pixelsPerMercatorPixel = 1;
this.globeRadius = 0;
this.globeCenterInViewSpace = [0, 0, 0];
this._tileCoverLift = 0;
this.freezeTileCoverage = false;
this._horizonShift = 0.1;
this._orthographicProjectionAtLowPitch = false;
this._allowWorldUnderZoom = false;
}
clone() {
const clone = new Transform(this._minZoom, this._maxZoom, this._minPitch, this.maxPitch, this._renderWorldCopies, this.getProjection(), this.maxBounds);
clone._elevation = this._elevation;
clone._centerAltitude = this._centerAltitude;
clone._centerAltitudeValidForExaggeration = this._centerAltitudeValidForExaggeration;
clone.tileSize = this.tileSize;
clone.mercatorFromTransition = this.mercatorFromTransition;
clone.width = this.width;
clone.height = this.height;
clone.cameraElevationReference = this.cameraElevationReference;
clone._center = this._center;
clone._setZoom(this.zoom);
clone._seaLevelZoom = this._seaLevelZoom;
clone.angle = this.angle;
clone._fov = this._fov;
clone._pitch = this._pitch;
clone._nearZ = this._nearZ;
clone._farZ = this._farZ;
clone._averageElevation = this._averageElevation;
clone._orthographicProjectionAtLowPitch = this._orthographicProjectionAtLowPitch;
clone._unmodified = this._unmodified;
clone._edgeInsets = this._edgeInsets.clone();
clone._camera = this._camera.clone();
clone._calcMatrices();
clone.freezeTileCoverage = this.freezeTileCoverage;
clone.frustumCorners = this.frustumCorners;
clone._allowWorldUnderZoom = this._allowWorldUnderZoom;
return clone;
}
get isOrthographic() {
return this.projection.name !== "globe" && this._orthographicProjectionAtLowPitch && this.pitch < OrthographicPitchTranstionValue;
}
get elevation() {
return this._elevation;
}
set elevation(elevation) {
if (this._elevation === elevation) return;
this._elevation = elevation;
this._updateCameraOnTerrain();
this._calcMatrices();
}
get depthOcclusionForSymbolsAndCircles() {
return this.projection.name !== "globe" && !this.isOrthographic;
}
updateElevation(constrainCameraOverTerrain, adaptCameraAltitude = false) {
const centerAltitudeChanged = this._elevation && this._elevation.exaggeration() !== this._centerAltitudeValidForExaggeration;
if (this._seaLevelZoom == null || centerAltitudeChanged) {
this._updateCameraOnTerrain();
}
if (constrainCameraOverTerrain || centerAltitudeChanged) {
this._constrainCamera(adaptCameraAltitude);
}
this._calcMatrices();
}
getProjection() {
return index$1.pick(this.projection, ["name", "center", "parallels"]);
}
// Returns whether the projection changes
setProjection(projection) {
this.projectionOptions = projection || { name: "mercator" };
const oldProjection = this.projection ? this.getProjection() : void 0;
this.projection = index$1.getProjection(this.projectionOptions);
const newProjection = this.getProjection();
const projectionHasChanged = !index$1.deepEqual(oldProjection, newProjection);
if (projectionHasChanged) {
this._calcMatrices();
}
this.mercatorFromTransition = false;
return projectionHasChanged;
}
// Returns whether the projection need to be reevaluated
setOrthographicProjectionAtLowPitch(enabled) {
if (this._orthographicProjectionAtLowPitch === enabled) {
return false;
}
this._orthographicProjectionAtLowPitch = enabled;
this._calcMatrices();
return true;
}
setMercatorFromTransition() {
const oldProjection = this.projection.name;
this.mercatorFromTransition = true;
this.projectionOptions = { name: "mercator" };
this.projection = index$1.getProjection({ name: "mercator" });
const projectionHasChanged = oldProjection !== this.projection.name;
if (projectionHasChanged) {
this._calcMatrices();
}
return projectionHasChanged;
}
get minZoom() {
return this._minZoom;
}
set minZoom(zoom) {
if (this._minZoom === zoom) return;
this._minZoom = zoom;
this.zoom = Math.max(this.zoom, zoom);
}
get maxZoom() {
return this._maxZoom;
}
set maxZoom(zoom) {
if (this._maxZoom === zoom) return;
this._maxZoom = zoom;
this.zoom = Math.min(this.zoom, zoom);
}
get minPitch() {
return this._minPitch;
}
set minPitch(pitch) {
if (this._minPitch === pitch) return;
this._minPitch = pitch;
this.pitch = Math.max(this.pitch, pitch);
}
get maxPitch() {
return this._maxPitch;
}
set maxPitch(pitch) {
if (this._maxPitch === pitch) return;
this._maxPitch = pitch;
this.pitch = Math.min(this.pitch, pitch);
}
get renderWorldCopies() {
return this._renderWorldCopies && this.projection.supportsWorldCopies === true;
}
set renderWorldCopies(renderWorldCopies) {
if (renderWorldCopies === void 0) {
renderWorldCopies = true;
} else if (renderWorldCopies === null) {
renderWorldCopies = false;
}
this._renderWorldCopies = renderWorldCopies;
}
get worldSize() {
return this.tileSize * this.scale;
}
// This getter returns an incorrect value.
// It should eventually be removed and cameraWorldSize be used instead.
// See free_camera.getDistanceToElevation for the rationale.
get cameraWorldSizeForFog() {
const distance = Math.max(this._camera.getDistanceToElevation(this._averageElevation), Number.EPSILON);
return this._worldSizeFromZoom(this._zoomFromMercatorZ(distance));
}
get cameraWorldSize() {
const distance = Math.max(this._camera.getDistanceToElevation(this._averageElevation, true), Number.EPSILON);
return this._worldSizeFromZoom(this._zoomFromMercatorZ(distance));
}
// `pixelsPerMeter` is used to describe relation between real world and pixel distances.
// In mercator projection it is dependant on latitude value meaning that one meter covers
// less pixels at the equator than near polar regions. Globe projection in other hand uses
// fixed ratio everywhere.
get pixelsPerMeter() {
return this.projection.pixelsPerMeter(this.center.lat, this.worldSize);
}
get cameraPixelsPerMeter() {
return index$1.mercatorZfromAltitude(1, this.center.lat) * this.cameraWorldSizeForFog;
}
get centerOffset() {
return this.centerPoint._sub(this.size._div(2));
}
get size() {
return new index$1.Point(this.width, this.height);
}
get bearing() {
return index$1.wrap(this.rotation, -180, 180);
}
set bearing(bearing) {
this.rotation = bearing;
}
get rotation() {
return -this.angle / Math.PI * 180;
}
set rotation(rotation) {
const b = -rotation * Math.PI / 180;
if (this.angle === b) return;
this._unmodified = false;
this.angle = b;
this._calcMatrices();
this.rotationMatrix = index$1.create$1();
index$1.rotate(this.rotationMatrix, this.rotationMatrix, this.angle);
}
get pitch() {
return this._pitch / Math.PI * 180;
}
set pitch(pitch) {
const p = index$1.clamp(pitch, this.minPitch, this.maxPitch) / 180 * Math.PI;
if (this._pitch === p) return;
this._unmodified = false;
this._pitch = p;
this._calcMatrices();
}
get aspect() {
return this.width / this.height;
}
get fov() {
return this._fov / Math.PI * 180;
}
set fov(fov) {
fov = Math.max(0.01, Math.min(60, fov));
if (this._fov === fov) return;
this._unmodified = false;
this._fov = index$1.degToRad(fov);
this._calcMatrices();
}
get fovX() {
return this._fov;
}
get fovY() {
const focalLength = 1 / Math.tan(this.fovX * 0.5);
return 2 * Math.atan(1 / this.aspect / focalLength);
}
get averageElevation() {
return this._averageElevation;
}
set averageElevation(averageElevation) {
this._averageElevation = averageElevation;
this._calcFogMatrices();
this._distanceTileDataCache = {};
}
get zoom() {
return this._zoom;
}
set zoom(zoom) {
const z = Math.min(Math.max(zoom, this.minZoom), this.maxZoom);
if (this._zoom === z) return;
this._unmodified = false;
this._setZoom(z);
this._updateSeaLevelZoom();
this._constrain();
this._calcMatrices();
}
_setZoom(z) {
this._zoom = z;
this.scale = this.zoomScale(z);
this.tileZoom = Math.floor(z);
this.zoomFraction = z - this.tileZoom;
}
get tileCoverLift() {
return this._tileCoverLift;
}
set tileCoverLift(lift) {
if (this._tileCoverLift === lift) return;
this._tileCoverLift = lift;
}
_updateCameraOnTerrain() {
const elevationAtCenter = this.elevation ? this.elevation.getAtPoint(this.locationCoordinate(this.center), Number.NEGATIVE_INFINITY) : Number.NEGATIVE_INFINITY;
const usePreviousCenter = this.elevation && elevationAtCenter === Number.NEGATIVE_INFINITY && this.elevation.visibleDemTiles.length > 0 && this.elevation.exaggeration() > 0 && this._centerAltitudeValidForExaggeration;
if (!this._elevation || elevationAtCenter === Number.NEGATIVE_INFINITY && !(usePreviousCenter && this._centerAltitude)) {
this._centerAltitude = 0;
this._seaLevelZoom = null;
this._centerAltitudeValidForExaggeration = void 0;
return;
}
const elevation = this._elevation;
if (usePreviousCenter || this._centerAltitude && this._centerAltitudeValidForExaggeration && elevation.exaggeration() && this._centerAltitudeValidForExaggeration !== elevation.exaggeration()) {
index$1.assert(this._centerAltitudeValidForExaggeration);
const previousExaggeration = this._centerAltitudeValidForExaggeration;
this._centerAltitude = this._centerAltitude / previousExaggeration * elevation.exaggeration();
this._centerAltitudeValidForExaggeration = elevation.exaggeration();
} else {
this._centerAltitude = elevationAtCenter || 0;
this._centerAltitudeValidForExaggeration = elevation.exaggeration();
}
this._updateSeaLevelZoom();
}
_updateSeaLevelZoom() {
if (this._centerAltitudeValidForExaggeration === void 0) {
return;
}
const height = this.cameraToCenterDistance;
const terrainElevation = this.pixelsPerMeter * this._centerAltitude;
const mercatorZ = Math.max(0, (terrainElevation + height) / this.worldSize);
this._seaLevelZoom = this._zoomFromMercatorZ(mercatorZ);
}
sampleAverageElevation() {
if (!this._elevation) return 0;
const elevation = this._elevation;
const elevationSamplePoints = [
[0.5, 0.2],
[0.3, 0.5],
[0.5, 0.5],
[0.7, 0.5],
[0.5, 0.8]
];
const horizon = this.horizonLineFromTop();
let elevationSum = 0;
let weightSum = 0;
for (let i = 0; i < elevationSamplePoints.length; i++) {
const pt = new index$1.Point(
elevationSamplePoints[i][0] * this.width,
horizon + elevationSamplePoints[i][1] * (this.height - horizon)
);
const hit = elevation.pointCoordinate(pt);
if (!hit) continue;
const distanceToHit = Math.hypot(hit[0] - this._camera.position[0], hit[1] - this._camera.position[1]);
const weight = 1 / distanceToHit;
elevationSum += hit[3] * weight;
weightSum += weight;
}
if (weightSum === 0) return NaN;
return elevationSum / weightSum;
}
get center() {
return this._center;
}
set center(center) {
if (center.lat === this._center.lat && center.lng === this._center.lng) return;
this._unmodified = false;
this._center = center;
if (this._terrainEnabled()) {
if (this.cameraElevationReference === "ground") {
this._updateCameraOnTerrain();
} else {
this._updateZoomFromElevation();
}
}
this._constrain();
this._calcMatrices();
}
_updateZoomFromElevation() {
if (this._seaLevelZoom == null || !this._elevation)
return;
const seaLevelZoom = this._seaLevelZoom;
const elevationAtCenter = this._elevation.getAtPointOrZero(this.locationCoordinate(this.center));
const mercatorElevation = this.pixelsPerMeter / this.worldSize * elevationAtCenter;
const altitude = this._mercatorZfromZoom(seaLevelZoom);
const minHeight = this._mercatorZfromZoom(this._maxZoom);
const height = Math.max(altitude - mercatorElevation, minHeight);
this._setZoom(this._zoomFromMercatorZ(height));
}
get padding() {
return this._edgeInsets.toJSON();
}
set padding(padding) {
if (this._edgeInsets.equals(padding)) return;
this._unmodified = false;
this._edgeInsets.interpolate(this._edgeInsets, padding, 1);
this._calcMatrices();
}
equals(transform) {
const lastElevation = this.elevation;
const newElevation = transform.elevation;
const elevationChanged = lastElevation != null !== (newElevation != null) || lastElevation && newElevation && lastElevation.exaggeration() !== newElevation.exaggeration();
return this.width === transform.width && this.height === transform.height && this.center.lng === transform.center.lng && this.center.lat === transform.center.lat && this.zoom === transform.zoom && this.bearing === transform.bearing && this.pitch === transform.pitch && this.fov === transform.fov && this.projection.name === transform.projection.name && this._edgeInsets.equals(transform.padding) && !elevationChanged;
}
/**
* Computes a zoom value relative to a map plane that goes through the provided mercator position.
*
* @param {MercatorCoordinate} position A position defining the altitude of the the map plane.
* @returns {number} The zoom value.
*/
computeZoomRelativeTo(position) {
const centerOnTargetAltitude = this.rayIntersectionCoordinate(this.pointRayIntersection(this.centerPoint, position.toAltitude()));
let targetPosition;
if (position.z < this._camera.position[2]) {
targetPosition = [centerOnTargetAltitude.x, centerOnTargetAltitude.y, centerOnTargetAltitude.z];
} else {
targetPosition = [position.x, position.y, position.z];
}
const distToTarget = index$1.length(index$1.sub([], this._camera.position, targetPosition));
return index$1.clamp(this._zoomFromMercatorZ(distToTarget), this._minZoom, this._maxZoom);
}
setFreeCameraOptions(options) {
if (!this.height)
return;
if (!options.position && !options.orientation)
return;
this._updateCameraState();
let changed = false;
if (options.orientation && !index$1.exactEquals(options.orientation, this._camera.orientation)) {
changed = this._setCameraOrientation(options.orientation);
}
if (options.position) {
const newPosition = [options.position.x, options.position.y, options.position.z];
if (!index$1.exactEquals$1(newPosition, this._camera.position)) {
this._setCameraPosition(newPosition);
changed = true;
}
}
if (changed) {
this._updateStateFromCamera();
this.recenterOnTerrain();
}
}
getFreeCameraOptions() {
this._updateCameraState();
const pos = this._camera.position;
const options = new FreeCameraOptions();
options.position = new index$1.MercatorCoordinate(pos[0], pos[1], pos[2]);
options.orientation = this._camera.orientation;
options._elevation = this.elevation;
options._renderWorldCopies = this.renderWorldCopies;
return options;
}
_setCameraOrientation(orientation) {
if (!index$1.length$1(orientation))
return false;
index$1.normalize$1(orientation, orientation);
const forward = index$1.transformQuat([], [0, 0, -1], orientation);
const up = index$1.transformQuat([], [0, -1, 0], orientation);
if (up[2] < 0)
return false;
const updatedOrientation = orientationFromFrame(forward, up);
if (!updatedOrientation)
return false;
this._camera.orientation = updatedOrientation;
return true;
}
_setCameraPosition(position) {
const minWorldSize = this.zoomScale(this.minZoom) * this.tileSize;
const maxWorldSize = this.zoomScale(this.maxZoom) * this.tileSize;
const distToCenter = this.cameraToCenterDistance;
position[2] = index$1.clamp(position[2], distToCenter / maxWorldSize, distToCenter / minWorldSize);
this._camera.position = position;
}
/**
* The center of the screen in pixels with the top-left corner being (0,0)
* and +y axis pointing downwards. This accounts for padding.
*
* @readonly
* @type {Point}
* @memberof Transform
*/
get centerPoint() {
return this._edgeInsets.getCenter(this.width, this.height);
}
/**
* Returns the vertical half-fov, accounting for padding, in radians.
*
* @readonly
* @type {number}
* @private
*/
get fovAboveCenter() {
return this._fov * (0.5 + this.centerOffset.y / this.height);
}
/**
* Returns true if the padding options are equal.
*
* @param {PaddingOptions} padding The padding options to compare.
* @returns {boolean} True if the padding options are equal.
* @memberof Transform
*/
isPaddingEqual(padding) {
return this._edgeInsets.equals(padding);
}
/**
* Helper method to update edge-insets inplace.
*
* @param {PaddingOptions} start The initial padding options.
* @param {PaddingOptions} target The target padding options.
* @param {number} t The interpolation variable.
* @memberof Transform
*/
interpolatePadding(start, target, t) {
this._unmodified = false;
this._edgeInsets.interpolate(start, target, t);
this._constrain();
this._calcMatrices();
}
/**
* Return the highest zoom level that fully includes all tiles within the transform's boundaries.
* @param {Object} options Options.
* @param {number} options.tileSize Tile size, expressed in screen pixels.
* @param {boolean} options.roundZoom Target zoom level. If true, the value will be rounded to the closest integer. Otherwise the value will be floored.
* @returns {number} An integer zoom level at which all tiles will be visible.
*/
coveringZoomLevel(options) {
const z = (options.roundZoom ? Math.round : Math.floor)(
this.zoom + this.scaleZoom(this.tileSize / options.tileSize)
);
return Math.max(0, z);
}
/**
* Return any "wrapped" copies of a given tile coordinate that are visible
* in the current view.
*
* @private
*/
getVisibleUnwrappedCoordinates(tileID) {
const result = [new index$1.UnwrappedTileID(0, tileID)];
if (this.renderWorldCopies) {
const utl = this.pointCoordinate(new index$1.Point(0, 0));
const utr = this.pointCoordinate(new index$1.Point(this.width, 0));
const ubl = this.pointCoordinate(new index$1.Point(this.width, this.height));
const ubr = this.pointCoordinate(new index$1.Point(0, this.height));
const w0 = Math.floor(Math.min(utl.x, utr.x, ubl.x, ubr.x));
const w1 = Math.floor(Math.max(utl.x, utr.x, ubl.x, ubr.x));
const extraWorldCopy = 1;
for (let w = w0 - extraWorldCopy; w <= w1 + extraWorldCopy; w++) {
if (w === 0) continue;
result.push(new index$1.UnwrappedTileID(w, tileID));
}
}
return result;
}
isLODDisabled(checkPitch) {
return (!checkPitch || this.pitch <= MIN_LOD_PITCH) && this._edgeInsets.top <= this._edgeInsets.bottom && !this._elevation && !this.projection.isReprojectedInTileSpace;
}
/**
* Extends tile coverage to include potential neighboring tiles using either a direction vector or quadrant visibility information.
* @param {Array} coveringTiles tile cover that is extended
* @param {number} maxZoom maximum zoom level
* @param {vec3} direction direction unit vector, if undefined quadrant visibility information is used
* @returns {Array} a set of extension tiles
*/
extendTileCover(coveringTiles, maxZoom, direction, minZoom) {
let out = [];
const extendDirection = direction != null;
const extendQuadrants = !extendDirection;
if (extendQuadrants && this.zoom < maxZoom) return out;
if (extendDirection && direction[0] === 0 && direction[1] === 0) return out;
const addedTiles = /* @__PURE__ */ new Set();
const addTileId = (overscaledZ, wrap2, z, x, y) => {
const key = index$1.calculateKey(wrap2, overscaledZ, z, x, y);
if (!addedTiles.has(key)) {
out.push(new index$1.OverscaledTileID(overscaledZ, wrap2, z, x, y));
addedTiles.add(key);
}
};
for (let i = 0; i < coveringTiles.length; i++) {
const id = coveringTiles[i];
if (extendQuadrants && id.canonical.z !== maxZoom) continue;
if (extendDirection && minZoom !== void 0 && minZoom > id.canonical.z) continue;
const tileId = id.canonical;
const overscaledZ = id.overscaledZ;
const tileWrap = id.wrap;
const tiles = 1 << tileId.z;
const xMaxInsideRange = tileId.x + 1 < tiles;
const xMinInsideRange = tileId.x > 0;
const yMaxInsideRange = tileId.y + 1 < tiles;
const yMinInsideRange = tileId.y > 0;
const leftWrap = id.wrap - (xMinInsideRange ? 0 : 1);
const rightWrap = id.wrap + (xMaxInsideRange ? 0 : 1);
const leftTileX = xMinInsideRange ? tileId.x - 1 : tiles - 1;
const rightTileX = xMaxInsideRange ? tileId.x + 1 : 0;
if (extendDirection) {
if (direction[0] < 0) {
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y);
if (direction[1] < 0 && yMaxInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y + 1);
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y + 1);
}
if (direction[1] > 0 && yMinInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y - 1);
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y - 1);
}
} else if (direction[0] > 0) {
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y);
if (direction[1] < 0 && yMaxInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y + 1);
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y + 1);
}
if (direction[1] > 0 && yMinInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y - 1);
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y - 1);
}
} else {
if (direction[1] < 0 && yMaxInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y + 1);
} else if (yMinInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y - 1);
}
}
} else {
const visibility = id.visibleQuadrants;
index$1.assert(visibility !== void 0);
if (visibility & QuadrantVisibility.TopLeft) {
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y);
if (yMinInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y - 1);
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y - 1);
}
}
if (visibility & QuadrantVisibility.TopRight) {
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y);
if (yMinInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y - 1);
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y - 1);
}
}
if (visibility & QuadrantVisibility.BottomLeft) {
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y);
if (yMaxInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y + 1);
addTileId(overscaledZ, leftWrap, tileId.z, leftTileX, tileId.y + 1);
}
}
if (visibility & QuadrantVisibility.BottomRight) {
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y);
if (yMaxInsideRange) {
addTileId(overscaledZ, tileWrap, tileId.z, tileId.x, tileId.y + 1);
addTileId(overscaledZ, rightWrap, tileId.z, rightTileX, tileId.y + 1);
}
}
}
}
const nonOverlappingIds = [];
for (const id of out) {
if (!out.some((ancestorCandidate) => id.isChildOf(ancestorCandidate))) {
nonOverlappingIds.push(id);
}
}
out = nonOverlappingIds.filter((newId) => !coveringTiles.some((oldId) => {
if (newId.overscaledZ < maxZoom && oldId.isChildOf(newId)) {
return true;
}
return newId.equals(oldId) || newId.isChildOf(oldId);
}));
if (extendQuadrants) {
const numTiles = 1 << maxZoom;
const isGlobe = this.projection.name === "globe";
const cameraCoord = isGlobe ? this._camera.mercatorPosition : this.pointCoordinate(this.getCameraPoint());
const cameraPoint = [numTiles * cameraCoord.x, numTiles * cameraCoord.y];
const limit = 4;
const limitSq = limit * limit;
out = out.filter((id) => {
const tileCenterX = id.canonical.x + 0.5;
const tileCenterY = id.canonical.y + 0.5;
const dx = tileCenterX - cameraPoint[0];
const dy = tileCenterY - cameraPoint[1];
const distSq = dx * dx + dy * dy;
return distSq < limitSq;
});
}
return out;
}
/**
* Extend tile coverage to include tiles that are below the view frustum.
* @param {Array} tiles tile cover that is extended
* @param {Frustum} frustum view frustum
* @param {number} maxZoom maximum zoom level
* @returns {Array} a set of extension tiles
*/
extendTileCoverToNearPlane(tiles, frustum, maxZoom) {
const out = [];
const addedTiles = /* @__PURE__ */ new Set();
for (const tile of tiles) {
addedTiles.add(tile.key);
}
const addTileId = (overscaledZ2, wrap2, z, x, y) => {
const key = index$1.calculateKey(wrap2, overscaledZ2, z, x, y);
if (!addedTiles.has(key)) {
out.push(new index$1.OverscaledTileID(overscaledZ2, wrap2, z, x, y));
addedTiles.add(key);
}
};
const overscaledZ = tiles.reduce((overscaledZ2, tile) => {
return Math.max(overscaledZ2, tile.overscaledZ);
}, maxZoom);
const numTiles = 1 << maxZoom;
const tileCorners = [
new index$1.Point(0, 0),
new index$1.Point(index$1.EXTENT, 0),
new index$1.Point(index$1.EXTENT, index$1.EXTENT),
new index$1.Point(0, index$1.EXTENT)
];
const p1 = new index$1.Point(0, 0);
const p2 = new index$1.Point(0, 0);
const findTileIntersections = (e1, e2) => {
const e1X = Math.floor(e1[0]);
const e1Y = Math.floor(e1[1]);
const e1TileX = (e1[0] - e1X) * index$1.EXTENT;
const e1TileY = (e1[1] - e1Y) * index$1.EXTENT;
const e2X = Math.floor(e2[0]);
const e2Y = Math.floor(e2[1]);
const e2TileX = (e2[0] - e2X) * index$1.EXTENT;
const e2TileY = (e2[1] - e2Y) * index$1.EXTENT;
for (let dx = -1; dx <= 1; dx++) {
const x = e1X + dx;
if (x < 0 || x >= numTiles) continue;
p1.x = e1TileX - dx * index$1.EXTENT;
p2.x = e2TileX - (x - e2X) * index$1.EXTENT;
for (let dy = -1; dy <= 1; dy++) {
const y = e1Y + dy;
p1.y = e1TileY - dy * index$1.EXTENT;
p2.y = e2TileY - (y - e2Y) * index$1.EXTENT;
if (index$1.edgeIntersectsBox(p1, p2, tileCorners)) {
addTileId(overscaledZ, 0, maxZoom, x, y);
}
}
}
};
const points = frustum.points;
const nearBl = points[index$1.NEAR_BL];
const nearBr = points[index$1.NEAR_BR];
const farBl = this._projectToGround(nearBl, points[index$1.FAR_BL]);
const farBr = this._projectToGround(nearBr, points[index$1.FAR_BR]);
findTileIntersections(nearBl, farBl);
findTileIntersections(nearBr, farBr);
return out;
}
_projectToGround(near, far) {
index$1.assert(far[2] < near[2]);
return index$1.lerp(index$1.create$2(), near, far, near[2] / (near[2] - far[2]));
}
/**
* Return all coordinates that could cover this transform for a covering
* zoom level.
* @param {Object} options
* @param {number} options.tileSize
* @param {number} options.minzoom
* @param {number} options.maxzoom
* @param {boolean} options.roundZoom
* @param {boolean} options.reparseOverscaled
* @returns {Array} OverscaledTileIDs
* @private
*/
coveringTiles(options) {
let z = this.coveringZoomLevel(options);
const actualZ = z;
const hasExaggeration = this.elevation && this.elevation.exaggeration();
const useElevationData = hasExaggeration && !options.isTerrainDEM;
const isMercator = this.projection.name === "mercator";
if (options.minzoom !== void 0 && z < options.minzoom) return [];
if (options.maxzoom !== void 0 && z > options.maxzoom) z = options.maxzoom;
const centerCoord = this.locationCoordinate(this.center);
const centerLatitude = this.center.lat;
const numTiles = 1 << z;
const centerPoint = [numTiles * centerCoord.x, numTiles * centerCoord.y, 0];
const isGlobe = this.projection.name === "globe";
const zInMeters = !isGlobe;
const cameraFrustum = index$1.Frustum.fromInvProjectionMatrix(this.invProjMatrix, this.worldSize, z, zInMeters);
const cameraCoord = isGlobe ? this._camera.mercatorPosition : this.pointCoordinate(this.getCameraPoint());
const meterToTile = numTiles * index$1.mercatorZfromAltitude(1, this.center.lat);
const cameraAltitude = this._camera.position[2] / index$1.mercatorZfromAltitude(1, this.center.lat);
const cameraPoint = [numTiles * cameraCoord.x, numTiles * cameraCoord.y, cameraAltitude * (zInMeters ? 1 : meterToTile)];
const verticalFrustumIntersect = isGlobe || hasExaggeration;
const zoomSplitDistance = this.cameraToCenterDistance / options.tileSize * (options.roundZoom ? 1 : 0.502);
const minZoom = this.isLODDisabled(true) ? z : 0;
let maxRange;
if (this._elevation && options.isTerrainDEM) {
maxRange = this._elevation.exaggeration() * 1e4;
} else if (this._elevation) {
const minMaxOpt = this._elevation.getMinMaxForVisibleTiles();
maxRange = minMaxOpt ? minMaxOpt.max : this._centerAltitude;
} else {
maxRange = this._centerAltitude;
}
const minRange = options.isTerrainDEM ? -maxRange : this._elevation ? this._elevation.getMinElevationBelowMSL() : 0;
const scaleAdjustment = this.projection.isReprojectedInTileSpace ? index$1.getScaleAdjustment(this) : 1;
const relativeScaleAtMercatorCoord = (mc) => {
const offset = 1 / 4e4;
const mcEast = new index$1.MercatorCoordinate(mc.x + offset, mc.y, mc.z);
const mcSouth = new index$1.MercatorCoordinate(mc.x, mc.y + offset, mc.z);
const ll = mc.toLngLat();
const llEast = mcEast.toLngLat();
const llSouth = mcSouth.toLngLat();
const p = this.locationCoordinate(ll);
const pEast = this.locationCoordinate(llEast);
const pSouth = this.locationCoordinate(llSouth);
const dx = Math.hypot(pEast.x - p.x, pEast.y - p.y);
const dy = Math.hypot(pSouth.x - p.x, pSouth.y - p.y);
return Math.sqrt(dx * dy) * scaleAdjustment / offset;
};
const newRootTile = (wrap2) => {
const max = maxRange;
const min = minRange;
return {
// With elevation, this._elevation provides z coordinate values. For 2D:
// All tiles are on zero elevation plane => z difference is zero
aabb: index$1.tileAABB(this, numTiles, 0, 0, 0, wrap2, min, max, this.projection),
zoom: 0,
x: 0,
y: 0,
minZ: min,
maxZ: max,
wrap: wrap2,
fullyVisible: false
};
};
const stack = [];
let result = [];
const maxZoom = z;
const overscaledZ = options.reparseOverscaled ? actualZ : z;
const cameraHeight = (cameraAltitude - this._centerAltitude) * meterToTile;
const getAABBFromElevation = (it) => {
index$1.assert(this._elevation);
if (!this._elevation || !it.tileID || !isMercator) return;
const minmax = this._elevation.getMinMaxForTile(it.tileID);
const aabb = it.aabb;
if (minmax) {
aabb.min[2] = minmax.min;
aabb.max[2] = minmax.max;
aabb.center[2] = (aabb.min[2] + aabb.max[2]) / 2;
} else {
it.shouldSplit = shouldSplit(it);
if (!it.shouldSplit) {
aabb.min[2] = aabb.max[2] = aabb.center[2] = this._centerAltitude;
}
}
};
const distToSplitScale = (dz, d) => {
const acuteAngleThresholdSin = 0.707;
const stretchTile = 1.1;
if (d * acuteAngleThresholdSin < dz) return 1;
const r = d / dz;
const k = r - 1 / acuteAngleThresholdSin;
return r / (1 / acuteAngleThresholdSin + (Math.pow(stretchTile, k + 1) - 1) / (stretchTile - 1) - 1);
};
const shouldSplit = (it) => {
if (it.zoom < minZoom) {
return true;
} else if (it.zoom === maxZoom) {
return false;
}
if (it.shouldSplit != null) {
return it.shouldSplit;
}
const dx = it.aabb.distanceX(cameraPoint);
const dy = it.aabb.distanceY(cameraPoint);
let dz = cameraHeight;
let tileScaleAdjustment = 1;
if (isGlobe) {
dz = it.aabb.distanceZ(cameraPoint);
const tilesAtZoom = Math.pow(2, it.zoom);
const minLat = index$1.latFromMercatorY((it.y + 1) / tilesAtZoom);
const maxLat = index$1.latFromMercatorY(it.y / tilesAtZoom);
const closestLat = Math.min(Math.max(centerLatitude, minLat), maxLat);
const relativeTileScale = index$1.circumferenceAtLatitude(closestLat) / index$1.circumferenceAtLatitude(centerLatitude);
if (closestLat === centerLatitude) {
const maxDivergence = 0.3;
tileScaleAdjustment = 1 / Math.max(1, this._mercatorScaleRatio - maxDivergence);
} else {
tileScaleAdjustment = Math.min(1, relativeTileScale / this._mercatorScaleRatio);
}
if (this.zoom <= index$1.GLOBE_ZOOM_THRESHOLD_MIN && it.zoom === maxZoom - 1 && relativeTileScale >= 0.9) {
return true;
}
} else {
index$1.assert(zInMeters);
if (useElevationData) {
dz = it.aabb.distanceZ(cameraPoint) * meterToTile;
}
if (this.projection.isReprojectedInTileSpace && actualZ <= 5) {
const numTiles2 = Math.pow(2, it.zoom);
const relativeScale = relativeScaleAtMercatorCoord(new index$1.MercatorCoordinate((it.x + 0.5) / numTiles2, (it.y + 0.5) / numTiles2));
tileScaleAdjustment = relativeScale > 0.85 ? 1 : relativeScale;
}
}
if (!(isMercator || isGlobe)) {
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz);
let distToSplit2 = (1 << maxZoom - it.zoom) * zoomSplitDistance * tileScaleAdjustment;
distToSplit2 = distToSplit2 * distToSplitScale(Math.max(dz, cameraHeight), distance);
return distance < distToSplit2;
}
let closestDistance = Number.MAX_VALUE;
let closestElevation = 0;
const corners = it.aabb.getCorners();
const distanceXyz = [];
for (const corner of corners) {
index$1.sub(distanceXyz, corner, cameraPoint);
if (!isGlobe) {
if (useElevationData) {
distanceXyz[2] *= meterToTile;
} else {
distanceXyz[2] = cameraHeight;
}
}
const dist = index$1.dot(distanceXyz, this._camera.forward());
if (dist < closestDistance) {
closestDistance = dist;
closestElevation = Math.abs(distanceXyz[2]);
}
}
let distToSplit = (1 << maxZoom - it.zoom) * zoomSplitDistance * tileScaleAdjustment;
distToSplit *= distToSplitScale(Math.max(closestElevation, cameraHeight), closestDistance);
if (closestDistance < distToSplit) {
return true;
}
const closestPointToCenter = it.aabb.closestPoint(centerPoint);
return closestPointToCenter[0] === centerPoint[0] && closestPointToCenter[1] === centerPoint[1];
};
if (this.renderWorldCopies) {
for (let i = 1; i <= NUM_WORLD_COPIES; i++) {
stack.push(newRootTile(-i));
stack.push(newRootTile(i));
}
}
stack.push(newRootTile(0));
while (stack.length > 0) {
const it = stack.pop();
const x = it.x;
const y = it.y;
let fullyVisible = it.fullyVisible;
const isPoleNeighbourAndGlobeProjection = () => {
return this.projection.name === "globe" && (it.y === 0 || it.y === (1 << it.zoom) - 1);
};
if (!fullyVisible) {
let intersectResult = verticalFrustumIntersect ? it.aabb.intersects(cameraFrustum) : it.aabb.intersectsFlat(cameraFrustum);
if (intersectResult === 0 && isPoleNeighbourAndGlobeProjection()) {
const tileId = new index$1.CanonicalTileID(it.zoom, x, y);
const poleAABB = index$1.aabbForTileOnGlobe(this, numTiles, tileId, true);
intersectResult = poleAABB.intersects(cameraFrustum);
}
if (intersectResult === 0) {
continue;
}
fullyVisible = intersectResult === 2;
}
if (it.zoom === maxZoom || !shouldSplit(it)) {
const tileZoom = it.zoom === maxZoom ? overscaledZ : it.zoom;
if (!!options.minzoom && options.minzoom > tileZoom) {
continue;
}
let visibility = QuadrantVisibility.None;
if (!fullyVisible) {
let intersectResult = verticalFrustumIntersect ? it.aabb.intersectsPrecise(cameraFrustum) : it.aabb.intersectsPreciseFlat(cameraFrustum);
if (intersectResult === 0 && isPoleNeighbourAndGlobeProjection()) {
const tileId = new index$1.CanonicalTileID(it.zoom, x, y);
const poleAABB = index$1.aabbForTileOnGlobe(this, numTiles, tileId, true);
intersectResult = poleAABB.intersectsPrecise(cameraFrustum);
}
if (intersectResult === 0) {
continue;
}
if (options.calculateQuadrantVisibility) {
if (cameraFrustum.containsPoint(it.aabb.center)) {
visibility = QuadrantVisibility.All;
} else {
for (let i = 0; i < 4; i++) {
const quadrantAabb = it.aabb.quadrant(i);
if (quadrantAabb.intersects(cameraFrustum) !== 0) {
visibility |= 1 << i;
}
}
}
}
}
const dx = centerPoint[0] - (0.5 + x + (it.wrap << it.zoom)) * (1 << z - it.zoom);
const dy = centerPoint[1] - 0.5 - y;
const id = it.tileID ? it.tileID : new index$1.OverscaledTileID(tileZoom, it.wrap, it.zoom, x, y);
if (options.calculateQuadrantVisibility) {
id.visibleQuadrants = visibility;
}
result.push({ tileID: id, distanceSq: dx * dx + dy * dy });
continue;
}
for (let i = 0; i < 4; i++) {
const childX = (x << 1) + i % 2;
const childY = (y << 1) + (i >> 1);
const aabb = isMercator ? it.aabb.quadrant(i) : index$1.tileAABB(this, numTiles, it.zoom + 1, childX, childY, it.wrap, it.minZ, it.maxZ, this.projection);
const child = { aabb, zoom: it.zoom + 1, x: childX, y: childY, wrap: it.wrap, fullyVisible, tileID: void 0, shouldSplit: void 0, minZ: it.minZ, maxZ: it.maxZ };
if (useElevationData && !isGlobe) {
child.tileID = new index$1.OverscaledTileID(it.zoom + 1 === maxZoom ? overscaledZ : it.zoom + 1, it.wrap, it.zoom + 1, childX, childY);
getAABBFromElevation(child);
}
stack.push(child);
}
}
if (this.fogCullDistSq) {
const fogCullDistSq = this.fogCullDistSq;
const horizonLineFromTop = this.horizonLineFromTop();
result = result.filter((entry) => {
const tl = [0, 0, 0, 1];
const br = [index$1.EXTENT, index$1.EXTENT, 0, 1];
const fogTileMatrix = this.calculateFogTileMatrix(entry.tileID.toUnwrapped());
index$1.transformMat4$1(tl, tl, fogTileMatrix);
index$1.transformMat4$1(br, br, fogTileMatrix);
const min = index$1.min([], tl, br);
const max = index$1.max([], tl, br);
const sqDist = index$1.getAABBPointSquareDist(min, max);
if (sqDist === 0) {
return true;
}
let overHorizonLine = false;
const elevation = this._elevation;
if (elevation && sqDist > fogCullDistSq && horizonLineFromTop !== 0) {
const projMatrix = this.calculateProjMatrix(entry.tileID.toUnwrapped());
let minmax;
if (!options.isTerrainDEM) {
minmax = elevation.getMinMaxForTile(entry.tileID);
}
if (!minmax) {
minmax = { min: minRange, max: maxRange };
}
const cornerFar = index$1.furthestTileCorner(this.rotation);
const farX = cornerFar[0] * index$1.EXTENT;
const farY = cornerFar[1] * index$1.EXTENT;
const worldFar = [farX, farY, minmax.max];
index$1.transformMat4(worldFar, worldFar, projMatrix);
const screenCoordY = (1 - worldFar[1]) * this.height * 0.5;
overHorizonLine = screenCoordY < horizonLineFromTop;
}
return sqDist < fogCullDistSq || overHorizonLine;
});
}
const cover = result.sort((a, b) => a.distanceSq - b.distanceSq).map((a) => a.tileID);
index$1.assert(!cover.length || this.elevation || cover[0].overscaledZ === overscaledZ || !isMercator);
return cover;
}
resize(width, height) {
this.width = width;
this.height = height;
this.pixelsToGLUnits = [2 / width, -2 / height];
this._constrain();
this._calcMatrices();
}
get unmodified() {
return this._unmodified;
}
zoomScale(zoom) {
return Math.pow(2, zoom);
}
scaleZoom(scale) {
return Math.log2(scale);
}
// Transform from LngLat to Point in world coordinates [-180, 180] x [90, -90] --> [0, this.worldSize] x [0, this.worldSize]
project(lnglat) {
const lat = index$1.clamp(lnglat.lat, -index$1.MAX_MERCATOR_LATITUDE, index$1.MAX_MERCATOR_LATITUDE);
const projectedLngLat = this.projection.project(lnglat.lng, lat);
return new index$1.Point(
projectedLngLat.x * this.worldSize,
projectedLngLat.y * this.worldSize
);
}
// Transform from Point in world coordinates to LngLat [0, this.worldSize] x [0, this.worldSize] --> [-180, 180] x [90, -90]
unproject(point) {
return this.projection.unproject(point.x / this.worldSize, point.y / this.worldSize);
}
// Point at center in world coordinates.
get point() {
return this.project(this.center);
}
// Point at center in Mercator coordinates.
get pointMerc() {
return this.point._div(this.worldSize);
}
// Ratio of pixelsPerMeter in the current projection to Mercator's.
get pixelsPerMeterRatio() {
return this.pixelsPerMeter / index$1.mercatorZfromAltitude(1, this.center.lat) / this.worldSize;
}
setLocationAtPoint(lnglat, point) {
let x, y;
const centerPoint = this.centerPoint;
if (this.projection.name === "globe") {
const worldSize = this.worldSize;
x = (point.x - centerPoint.x) / worldSize;
y = (point.y - centerPoint.y) / worldSize;
} else {
const a = this.pointCoordinate(point);
const b = this.pointCoordinate(centerPoint);
x = a.x - b.x;
y = a.y - b.y;
}
const loc = this.locationCoordinate(lnglat);
this.setLocation(new index$1.MercatorCoordinate(loc.x - x, loc.y - y));
}
setLocation(location) {
this.center = this.coordinateLocation(location);
if (this.projection.wrap) {
this.center = this.center.wrap();
}
}
/**
* Given a location, return the screen point that corresponds to it. In 3D mode
* (with terrain) this behaves the same as in 2D mode.
* This method is coupled with {@see pointLocation} in 3D mode to model map manipulation
* using flat plane approach to keep constant elevation above ground.
* @param {LngLat} lnglat location
* @param {number} altitude (optional) altitude above the map plane in meters.
* @returns {Point} screen point
* @private
*/
locationPoint(lnglat, altitude) {
return this.projection.locationPoint(this, lnglat, altitude);
}
/**
* Given a location, return the screen point that corresponds to it
* In 3D mode (when terrain is enabled) elevation is sampled for the point before
* projecting it. In 2D mode, behaves the same locationPoint.
* @param {LngLat} lnglat location
* @param {number} altitude (optional) altitude above the map plane in meters.
* @returns {Point} screen point
* @private
*/
locationPoint3D(lnglat, altitude) {
return this.projection.locationPoint(this, lnglat, altitude, true);
}
/**
* Given a point on screen, return its lnglat
* @param {Point} p screen point
* @returns {LngLat} lnglat location
* @private
*/
pointLocation(p) {
return this.coordinateLocation(this.pointCoordinate(p));
}
/**
* Given a point on screen, return its lnglat
* In 3D mode (map with terrain) returns location of terrain raycast point.
* In 2D mode, behaves the same as {@see pointLocation}.
* @param {Point} p screen point
* @param {number} altitude (optional) altitude above the map plane in meters.
* @returns {LngLat} lnglat location
* @private
*/
pointLocation3D(p, altitude) {
return this.coordinateLocation(this.pointCoordinate3D(p, altitude));
}
/**
* Given a geographical lngLat, return an unrounded
* coordinate that represents it at this transform's zoom level.
* @param {LngLat} lngLat
* @param {number} altitude (optional) altitude above the map plane in meters.
* @returns {Coordinate}
* @private
*/
locationCoordinate(lngLat, altitude) {
const z = altitude ? index$1.mercatorZfromAltitude(altitude, lngLat.lat) : void 0;
const projectedLngLat = this.projection.project(lngLat.lng, lngLat.lat);
return new index$1.MercatorCoordinate(
projectedLngLat.x,
projectedLngLat.y,
z
);
}
/**
* Given a Coordinate, return its geographical position.
* @param {Coordinate} coord
* @returns {LngLat} lngLat
* @private
*/
coordinateLocation(coord) {
return this.projection.unproject(coord.x, coord.y);
}
/**
* Casts a ray from a point on screen and returns the Ray,
* and the extent along it, at which it intersects the map plane.
*
* @param {Point} p Viewport pixel co-ordinates.
* @param {number} z Optional altitude of the map plane, defaulting to elevation at center.
* @returns {{ p0: Vec4, p1: Vec4, t: number }} p0,p1 are two points on the ray.
* t is the fractional extent along the ray at which the ray intersects the map plane.
* @private
*/
pointRayIntersection(p, z) {
const targetZ = z !== void 0 && z !== null ? z : this._centerAltitude;
const p0 = [p.x, p.y, 0, 1];
const p1 = [p.x, p.y, 1, 1];
index$1.transformMat4$1(p0, p0, this.pixelMatrixInverse);
index$1.transformMat4$1(p1, p1, this.pixelMatrixInverse);
const w0 = p0[3];
const w1 = p1[3];
index$1.scale$2(p0, p0, 1 / w0);
index$1.scale$2(p1, p1, 1 / w1);
const z0 = p0[2];
const z1 = p1[2];
const t = z0 === z1 ? 0 : (targetZ - z0) / (z1 - z0);
return { p0, p1, t };
}
screenPointToMercatorRay(p) {
const p0 = [p.x, p.y, 0, 1];
const p1 = [p.x, p.y, 1, 1];
index$1.transformMat4$1(p0, p0, this.pixelMatrixInverse);
index$1.transformMat4$1(p1, p1, this.pixelMatrixInverse);
index$1.scale$2(p0, p0, 1 / p0[3]);
index$1.scale$2(p1, p1, 1 / p1[3]);
p0[2] = index$1.mercatorZfromAltitude(p0[2], this._center.lat) * this.worldSize;
p1[2] = index$1.mercatorZfromAltitude(p1[2], this._center.lat) * this.worldSize;
index$1.scale$2(p0, p0, 1 / this.worldSize);
index$1.scale$2(p1, p1, 1 / this.worldSize);
return new index$1.Ray([p0[0], p0[1], p0[2]], index$1.normalize([], index$1.sub([], p1, p0)));
}
/**
* Helper method to convert the ray intersection with the map plane to MercatorCoordinate.
*
* @param {RayIntersectionResult} rayIntersection
* @returns {MercatorCoordinate}
* @private
*/
rayIntersectionCoordinate(rayIntersection) {
const { p0, p1, t } = rayIntersection;
const z0 = index$1.mercatorZfromAltitude(p0[2], this._center.lat);
const z1 = index$1.mercatorZfromAltitude(p1[2], this._center.lat);
return new index$1.MercatorCoordinate(
index$1.number(p0[0], p1[0], t) / this.worldSize,
index$1.number(p0[1], p1[1], t) / this.worldSize,
index$1.number(z0, z1, t)
);
}
/**
* Given a point on screen, returns MercatorCoordinate.
* @param {Point} p Top left origin screen point, in pixels.
* @param {number} z Optional altitude of the map plane, defaulting to elevation at center.
* @private
*/
pointCoordinate(p, z = this._centerAltitude) {
return this.projection.pointCoordinate(this, p.x, p.y, z);
}
/**
* Given a point on screen, returns MercatorCoordinate.
* In 3D mode, raycast to terrain. In 2D mode, behaves the same as {@see pointCoordinate}.
* For p above terrain, don't return point behind camera but clamp p.y at the top of terrain.
* @param {Point} p top left origin screen point, in pixels.
* @param {number} altitude (optional) altitude above the map plane in meters.
* @private
*/
pointCoordinate3D(p, altitude) {
if (!this.elevation) return this.pointCoordinate(p, altitude);
let raycast = this.projection.pointCoordinate3D(this, p.x, p.y);
if (raycast) return new index$1.MercatorCoordinate(raycast[0], raycast[1], raycast[2]);
let start = 0, end = this.horizonLineFromTop();
if (p.y > end) return this.pointCoordinate(p, altitude);
const samples = 10;
const threshold = 0.02 * end;
const r = p.clone();
for (let i = 0; i < samples && end - start > threshold; i++) {
r.y = index$1.number(start, end, 0.66);
const rCast = this.projection.pointCoordinate3D(this, r.x, r.y);
if (rCast) {
end = r.y;
raycast = rCast;
} else {
start = r.y;
}
}
return raycast ? new index$1.MercatorCoordinate(raycast[0], raycast[1], raycast[2]) : this.pointCoordinate(p);
}
/**
* Returns true if a screenspace Point p, is above the horizon.
* In non-globe projections, this approximates the map as an infinite plane and does not account for z0-z3
* wherein the map is small quad with whitespace above the north pole and below the south pole.
*
* @param {Point} p
* @returns {boolean}
* @private
*/
isPointAboveHorizon(p) {
return this.projection.isPointAboveHorizon(this, p);
}
/**
* Determines if the given point is located on a visible map surface.
*
* @param {Point} p
* @returns {boolean}
* @private
*/
isPointOnSurface(p) {
if (p.y < 0 || p.y > this.height || p.x < 0 || p.x > this.width) return false;
if (this.elevation || this.zoom >= index$1.GLOBE_ZOOM_THRESHOLD_MAX) return !this.isPointAboveHorizon(p);
const coord = this.pointCoordinate(p);
return coord.y >= 0 && coord.y <= 1;
}
/**
* Given a coordinate, return the screen point that corresponds to it
* @param {Coordinate} coord
* @param {boolean} sampleTerrainIn3D in 3D mode (terrain enabled), sample elevation for the point.
* If false, do the same as in 2D mode, assume flat camera elevation plane for all points.
* @returns {Point} screen point
* @private
*/
_coordinatePoint(coord, sampleTerrainIn3D) {
const elevation = sampleTerrainIn3D && this.elevation ? this.elevation.getAtPointOrZero(coord, this._centerAltitude) : this._centerAltitude;
const p = [coord.x * this.worldSize, coord.y * this.worldSize, elevation + coord.toAltitude(), 1];
index$1.transformMat4$1(p, p, this.pixelMatrix);
return p[3] > 0 ? new index$1.Point(p[0] / p[3], p[1] / p[3]) : new index$1.Point(Number.MAX_VALUE, Number.MAX_VALUE);
}
// In Globe, conic and thematic projections, Lng/Lat extremes are not always at corners.
// This function additionally checks each screen edge midpoint.
// While midpoints continue to be extremes, it recursively checks midpoints of smaller segments.
_getBoundsNonRectangular() {
index$1.assert(!this.projection.supportsWorldCopies, "Rectangular projections should use the simpler _getBoundsRectangular");
const { top, left } = this._edgeInsets;
const bottom = this.height - this._edgeInsets.bottom;
const right = this.width - this._edgeInsets.right;
const tl = this.pointLocation3D(new index$1.Point(left, top));
const tr = this.pointLocation3D(new index$1.Point(right, top));
const br = this.pointLocation3D(new index$1.Point(right, bottom));
const bl = this.pointLocation3D(new index$1.Point(left, bottom));
let west = Math.min(tl.lng, tr.lng, br.lng, bl.lng);
let east = Math.max(tl.lng, tr.lng, br.lng, bl.lng);
let south = Math.min(tl.lat, tr.lat, br.lat, bl.lat);
let north = Math.max(tl.lat, tr.lat, br.lat, bl.lat);
const s = Math.pow(2, -this.zoom);
const maxErr = s / 16 * 270;
const minRecursions = this.projection.name === "globe" ? 1 : 4;
const processSegment = (ax, ay, bx, by, depth) => {
const mx = (ax + bx) / 2;
const my = (ay + by) / 2;
const p = new index$1.Point(mx, my);
const { lng, lat } = this.pointLocation3D(p);
const err = Math.max(0, west - lng, south - lat, lng - east, lat - north);
west = Math.min(west, lng);
east = Math.max(east, lng);
south = Math.min(south, lat);
north = Math.max(north, lat);
if (depth < minRecursions || err > maxErr) {
processSegment(ax, ay, mx, my, depth + 1);
processSegment(mx, my, bx, by, depth + 1);
}
};
processSegment(left, top, right, top, 1);
processSegment(right, top, right, bottom, 1);
processSegment(right, bottom, left, bottom, 1);
processSegment(left, bottom, left, top, 1);
if (this.projection.name === "globe") {
const [northPoleIsVisible, southPoleIsVisible] = index$1.polesInViewport(this);
if (northPoleIsVisible) {
north = 90;
east = 180;
west = -180;
} else if (southPoleIsVisible) {
south = -90;
east = 180;
west = -180;
}
}
return new index$1.LngLatBounds(new index$1.LngLat(west, south), new index$1.LngLat(east, north));
}
_getBoundsRectangular(min, max) {
index$1.assert(this.projection.supportsWorldCopies, "_getBoundsRectangular only checks corners and works only on rectangular projections. Other projections should use _getBoundsNonRectangular");
const { top, left } = this._edgeInsets;
const bottom = this.height - this._edgeInsets.bottom;
const right = this.width - this._edgeInsets.right;
const topLeft = new index$1.Point(left, top);
const topRight = new index$1.Point(right, top);
const bottomRight = new index$1.Point(right, bottom);
const bottomLeft = new index$1.Point(left, bottom);
let tl = this.pointCoordinate(topLeft, min);
let tr = this.pointCoordinate(topRight, min);
const br = this.pointCoordinate(bottomRight, max);
const bl = this.pointCoordinate(bottomLeft, max);
const slope = (p1, p2) => (p2.y - p1.y) / (p2.x - p1.x);
if (tl.y > 1 && tr.y >= 0) tl = new index$1.MercatorCoordinate((1 - bl.y) / slope(bl, tl) + bl.x, 1);
else if (tl.y < 0 && tr.y <= 1) tl = new index$1.MercatorCoordinate(-bl.y / slope(bl, tl) + bl.x, 0);
if (tr.y > 1 && tl.y >= 0) tr = new index$1.MercatorCoordinate((1 - br.y) / slope(br, tr) + br.x, 1);
else if (tr.y < 0 && tl.y <= 1) tr = new index$1.MercatorCoordinate(-br.y / slope(br, tr) + br.x, 0);
return new index$1.LngLatBounds().extend(this.coordinateLocation(tl)).extend(this.coordinateLocation(tr)).extend(this.coordinateLocation(bl)).extend(this.coordinateLocation(br));
}
_getBoundsRectangularTerrain() {
index$1.assert(this.elevation);
const elevation = this.elevation;
if (!elevation.visibleDemTiles.length || elevation.isUsingMockSource()) {
return this._getBoundsRectangular(0, 0);
}
const minmax = elevation.visibleDemTiles.reduce((acc, t) => {
if (t.dem) {
const tree = t.dem.tree;
acc.min = Math.min(acc.min, tree.minimums[0]);
acc.max = Math.max(acc.max, tree.maximums[0]);
}
return acc;
}, { min: Number.MAX_VALUE, max: 0 });
index$1.assert(minmax.min !== Number.MAX_VALUE);
return this._getBoundsRectangular(minmax.min * elevation.exaggeration(), minmax.max * elevation.exaggeration());
}
/**
* Returns the map's geographical bounds. When the bearing or pitch is non-zero, the visible region is not
* an axis-aligned rectangle, and the result is the smallest bounds that encompasses the visible region.
*
* @returns {LngLatBounds} Returns a {@link LngLatBounds} object describing the map's geographical bounds.
*/
getBounds() {
if (this.projection.name === "mercator" || this.projection.name === "equirectangular") {
if (this._terrainEnabled()) return this._getBoundsRectangularTerrain();
return this._getBoundsRectangular(0, 0);
}
return this._getBoundsNonRectangular();
}
/**
* Returns position of horizon line from the top of the map in pixels.
* If horizon is not visible, returns 0 by default or a negative value if called with clampToTop = false.
* @private
*/
horizonLineFromTop(clampToTop = true) {
const h = this.height / 2 / Math.tan(this._fov / 2) / Math.tan(Math.max(this._pitch, 0.1)) - this.centerOffset.y;
const offset = this.height / 2 - h * (1 - this._horizonShift);
return clampToTop ? Math.max(0, offset) : offset;
}
/**
* Returns the maximum geographical bounds the map is constrained to, or `null` if none set.
* @returns {LngLatBounds} {@link LngLatBounds}.
*/
getMaxBounds() {
return this.maxBounds;
}
/**
* Sets or clears the map's geographical constraints.
*
* @param {LngLatBounds} bounds A {@link LngLatBounds} object describing the new geographic boundaries of the map.
*/
setMaxBounds(bounds) {
this.maxBounds = bounds;
this.minLat = -index$1.MAX_MERCATOR_LATITUDE;
this.maxLat = index$1.MAX_MERCATOR_LATITUDE;
this.minLng = -180;
this.maxLng = 180;
if (bounds) {
this.minLat = bounds.getSouth();
this.maxLat = bounds.getNorth();
this.minLng = bounds.getWest();
this.maxLng = bounds.getEast();
if (this.maxLng < this.minLng) this.maxLng += 360;
}
this.worldMinX = index$1.mercatorXfromLng(this.minLng) * this.tileSize;
this.worldMaxX = index$1.mercatorXfromLng(this.maxLng) * this.tileSize;
this.worldMinY = index$1.mercatorYfromLat(this.maxLat) * this.tileSize;
this.worldMaxY = index$1.mercatorYfromLat(this.minLat) * this.tileSize;
this._constrain();
}
calculatePosMatrix(unwrappedTileID, worldSize) {
return this.projection.createTileMatrix(this, worldSize, unwrappedTileID);
}
calculateDistanceTileData(unwrappedTileID) {
const distanceDataKey = unwrappedTileID.key;
const cache = this._distanceTileDataCache;
if (cache[distanceDataKey]) {
return cache[distanceDataKey];
}
const canonical = unwrappedTileID.canonical;
const windowScaleFactor = 1 / this.height;
const cws = this.cameraWorldSize;
const scale = cws / this.zoomScale(canonical.z);
const unwrappedX = canonical.x + Math.pow(2, canonical.z) * unwrappedTileID.wrap;
const tX = unwrappedX * scale;
const tY = canonical.y * scale;
const center = this.point;
center.x *= cws / this.worldSize;
center.y *= cws / this.worldSize;
const angle = this.angle;
const bX = Math.sin(-angle);
const bY = -Math.cos(-angle);
const cX = (center.x - tX) * windowScaleFactor;
const cY = (center.y - tY) * windowScaleFactor;
cache[distanceDataKey] = {
bearing: [bX, bY],
center: [cX, cY],
scale: scale / index$1.EXTENT * windowScaleFactor
};
return cache[distanceDataKey];
}
/**
* Calculate the fogTileMatrix that, given a tile coordinate, can be used to
* calculate its position relative to the camera in units of pixels divided
* by the map height. Used with fog for consistent computation of distance
* from camera.
*
* @param {UnwrappedTileID} unwrappedTileID;
* @private
*/
calculateFogTileMatrix(unwrappedTileID) {
const fogTileMatrixKey = unwrappedTileID.key;
const cache = this._fogTileMatrixCache;
if (cache[fogTileMatrixKey]) {
return cache[fogTileMatrixKey];
}
const posMatrix = this.projection.createTileMatrix(this, this.cameraWorldSizeForFog, unwrappedTileID);
index$1.multiply(posMatrix, this.worldToFogMatrix, posMatrix);
cache[fogTileMatrixKey] = new Float32Array(posMatrix);
return cache[fogTileMatrixKey];
}
/**
* Calculate the projMatrix that, given a tile coordinate, would be used to display the tile on the screen.
* @param {UnwrappedTileID} unwrappedTileID;
* @private
*/
calculateProjMatrix(unwrappedTileID, aligned = false, expanded = false) {
const projMatrixKey = unwrappedTileID.key;
let cache;
if (expanded) {
cache = this._expandedProjMatrixCache;
} else if (aligned) {
cache = this._alignedProjMatrixCache;
} else {
cache = this._projMatrixCache;
}
if (cache[projMatrixKey]) {
return cache[projMatrixKey];
}
const posMatrix = this.calculatePosMatrix(unwrappedTileID, this.worldSize);
let projMatrix;
if (this.projection.isReprojectedInTileSpace) {
projMatrix = this.mercatorMatrix;
} else if (expanded) {
index$1.assert(!aligned);
projMatrix = this.expandedFarZProjMatrix;
} else {
projMatrix = aligned ? this.alignedProjMatrix : this.projMatrix;
}
index$1.multiply(posMatrix, projMatrix, posMatrix);
cache[projMatrixKey] = new Float32Array(posMatrix);
return cache[projMatrixKey];
}
calculatePixelsToTileUnitsMatrix(tile) {
const key = tile.tileID.key;
const cache = this._pixelsToTileUnitsCache;
if (cache[key]) {
return cache[key];
}
const matrix = index$1.getPixelsToTileUnitsMatrix(tile, this);
cache[key] = matrix;
return cache[key];
}
customLayerMatrix() {
return this.mercatorMatrix.slice();
}
globeToMercatorMatrix() {
if (this.projection.name === "globe") {
const pixelsToMerc = 1 / this.worldSize;
const m = index$1.fromScaling([], [pixelsToMerc, pixelsToMerc, pixelsToMerc]);
index$1.multiply(m, m, this.globeMatrix);
return m;
}
return void 0;
}
recenterOnTerrain() {
if (!this._elevation || this.projection.name === "globe")
return;
const elevation = this._elevation;
this._updateCameraState();
const mercPixelsPerMeter = index$1.mercatorZfromAltitude(1, this._center.lat) * this.worldSize;
const start = this._computeCameraPosition(mercPixelsPerMeter);
const dir = this._camera.forward();
const metersToMerc = index$1.mercatorZfromAltitude(1, this._center.lat);
start[2] /= metersToMerc;
dir[2] /= metersToMerc;
index$1.normalize(dir, dir);
const t = elevation.raycast(start, dir, elevation.exaggeration());
if (t) {
const point = index$1.scaleAndAdd([], start, dir, t);
const newCenter = new index$1.MercatorCoordinate(point[0], point[1], index$1.mercatorZfromAltitude(point[2], index$1.latFromMercatorY(point[1])));
const camToNew = [newCenter.x - start[0], newCenter.y - start[1], newCenter.z - start[2] * metersToMerc];
const maxAltitude = (newCenter.z + index$1.length(camToNew)) * this._pixelsPerMercatorPixel;
this._seaLevelZoom = this._zoomFromMercatorZ(maxAltitude);
this._centerAltitude = newCenter.toAltitude();
this._center = this.coordinateLocation(newCenter);
this._updateZoomFromElevation();
this._constrain();
this._calcMatrices();
}
}
_constrainCamera(adaptCameraAltitude = false) {
if (!this._elevation)
return;
const elevation = this._elevation;
const mercPixelsPerMeter = index$1.mercatorZfromAltitude(1, this._center.lat) * this.worldSize;
const pos = this._computeCameraPosition(mercPixelsPerMeter);
const elevationAtCamera = elevation.getAtPointOrZero(new index$1.MercatorCoordinate(...pos));
const terrainElevation = this.pixelsPerMeter / this.worldSize * elevationAtCamera;
const minHeight = this._minimumHeightOverTerrain();
const cameraHeight = pos[2] - terrainElevation;
if (cameraHeight <= minHeight) {
if (cameraHeight < 0 || adaptCameraAltitude) {
const center = this.locationCoordinate(this._center, this._centerAltitude);
const cameraToCenter = [pos[0], pos[1], center.z - pos[2]];
const prevDistToCamera = index$1.length(cameraToCenter);
cameraToCenter[2] -= (minHeight - cameraHeight) / this._pixelsPerMercatorPixel;
const newDistToCamera = index$1.length(cameraToCenter);
if (newDistToCamera === 0)
return;
index$1.scale$1(cameraToCenter, cameraToCenter, prevDistToCamera / newDistToCamera * this._pixelsPerMercatorPixel);
this._camera.position = [pos[0], pos[1], center.z * this._pixelsPerMercatorPixel - cameraToCenter[2]];
this._updateStateFromCamera();
} else {
this._isCameraConstrained = true;
}
}
}
_constrain() {
if (!this.center || !this.width || !this.height || this._constraining) return;
this._constraining = true;
const isGlobe = this.projection.name === "globe" || this.mercatorFromTransition;
if (this.projection.isReprojectedInTileSpace || isGlobe) {
const center = this.center;
center.lat = index$1.clamp(center.lat, this.minLat, this.maxLat);
if (this.maxBounds || !(this.renderWorldCopies || isGlobe)) center.lng = index$1.clamp(center.lng, this.minLng, this.maxLng);
this.center = center;
this._constraining = false;
return;
}
const unmodified = this._unmodified;
const { x, y } = this.point;
let s = 0;
let x2 = x;
let y2 = y;
const w2 = this.width / 2;
const h2 = this.height / 2;
const minY = this.worldMinY * this.scale;
const maxY = this.worldMaxY * this.scale;
if (y - h2 < minY) y2 = minY + h2;
if (y + h2 > maxY) y2 = maxY - h2;
if (maxY - minY < this.height) {
s = Math.max(s, this.height / (maxY - minY));
y2 = (maxY + minY) / 2;
}
if (this.maxBounds || !this._renderWorldCopies || !this.projection.wrap) {
const minX = this.worldMinX * this.scale;
const maxX = this.worldMaxX * this.scale;
const shift = this.worldSize / 2 - (minX + maxX) / 2;
x2 = (x + shift + this.worldSize) % this.worldSize - shift;
if (x2 - w2 < minX) x2 = minX + w2;
if (x2 + w2 > maxX) x2 = maxX - w2;
if (maxX - minX < this.width) {
s = Math.max(s, this.width / (maxX - minX));
x2 = (maxX + minX) / 2;
}
}
if ((x2 !== x || y2 !== y) && !this._allowWorldUnderZoom) {
this.center = this.unproject(new index$1.Point(x2, y2));
}
if (s && !this._allowWorldUnderZoom) {
this.zoom += this.scaleZoom(s);
}
this._constrainCamera();
this._unmodified = unmodified;
this._constraining = false;
}
/**
* Returns the minimum zoom at which `this.width` can fit max longitude range
* and `this.height` can fit max latitude range.
*
* @returns {number} The zoom value.
*/
_minZoomForBounds() {
let minZoom = Math.max(0, this.scaleZoom(Math.max(0, this.height) / (this.worldMaxY - this.worldMinY)));
if (this.maxBounds) {
minZoom = Math.max(minZoom, this.scaleZoom(this.width / (this.worldMaxX - this.worldMinX)));
}
return minZoom;
}
/**
* Returns the maximum distance of the camera from the center of the bounds, such that
* `this.width` can fit max longitude range and `this.height` can fit max latitude range.
* In mercator units.
*
* @returns {number} The mercator z coordinate.
*/
_maxCameraBoundsDistance() {
return this._mercatorZfromZoom(this._minZoomForBounds());
}
_calcMatrices() {
if (!this.height) return;
const offset = this.centerOffset;
const isGlobe = this.projection.name === "globe";
const pixelsPerMeter = this.pixelsPerMeter;
if (this.projection.name === "globe") {
this._mercatorScaleRatio = index$1.mercatorZfromAltitude(1, this.center.lat) / index$1.mercatorZfromAltitude(1, index$1.GLOBE_SCALE_MATCH_LATITUDE);
}
const projectionT = index$1.getProjectionInterpolationT(this.projection, this.zoom, this.width, this.height, 1024);
this._pixelsPerMercatorPixel = this.projection.pixelSpaceConversion(this.center.lat, this.worldSize, projectionT);
this.cameraToCenterDistance = 0.5 / Math.tan(this._fov * 0.5) * this.height * this._pixelsPerMercatorPixel;
this._updateCameraState();
this._farZ = this.projection.farthestPixelDistance(this);
this._nearZ = this.height / 50;
const zUnit = this.projection.zAxisUnit === "meters" ? pixelsPerMeter : 1;
const worldToCamera = this._camera.getWorldToCamera(this.worldSize, zUnit);
let cameraToClip;
const cameraToClipPerspective = this._camera.getCameraToClipPerspective(this._fov, this.width / this.height, this._nearZ, this._farZ);
cameraToClipPerspective[8] = -offset.x * 2 / this.width;
cameraToClipPerspective[9] = offset.y * 2 / this.height;
if (this.isOrthographic) {
const cameraToCenterDistance = 0.5 * this.height / Math.tan(this._fov / 2) * 1;
let top = cameraToCenterDistance * Math.tan(this._fov * 0.5);
let right = top * this.aspect;
let left = -right;
let bottom = -top;
right -= offset.x;
left -= offset.x;
top += offset.y;
bottom += offset.y;
cameraToClip = this._camera.getCameraToClipOrthographic(left, right, bottom, top, this._nearZ, this._farZ);
const mixValue = this.pitch >= OrthographicPitchTranstionValue ? 1 : this.pitch / OrthographicPitchTranstionValue;
lerpMatrix(cameraToClip, cameraToClip, cameraToClipPerspective, index$1.easeIn(mixValue));
} else {
cameraToClip = cameraToClipPerspective;
}
const worldToClipPerspective = index$1.mul([], cameraToClipPerspective, worldToCamera);
let m = index$1.mul([], cameraToClip, worldToCamera);
if (this.projection.isReprojectedInTileSpace) {
const mc = this.locationCoordinate(this.center);
const adjustments = index$1.identity([]);
index$1.translate(adjustments, adjustments, [mc.x * this.worldSize, mc.y * this.worldSize, 0]);
index$1.multiply(adjustments, adjustments, index$1.getProjectionAdjustments(this));
index$1.translate(adjustments, adjustments, [-mc.x * this.worldSize, -mc.y * this.worldSize, 0]);
index$1.multiply(m, m, adjustments);
index$1.multiply(worldToClipPerspective, worldToClipPerspective, adjustments);
this.inverseAdjustmentMatrix = index$1.getProjectionAdjustmentInverted(this);
} else {
this.inverseAdjustmentMatrix = [1, 0, 0, 1];
}
this.mercatorMatrix = index$1.scale$3([], m, [this.worldSize, this.worldSize, this.worldSize / zUnit, 1]);
this.projMatrix = m;
this.invProjMatrix = index$1.invert(new Float64Array(16), this.projMatrix);
if (isGlobe) {
const expandedCameraToClipPerspective = this._camera.getCameraToClipPerspective(this._fov, this.width / this.height, this._nearZ, Infinity);
expandedCameraToClipPerspective[8] = -offset.x * 2 / this.width;
expandedCameraToClipPerspective[9] = offset.y * 2 / this.height;
this.expandedFarZProjMatrix = index$1.mul([], expandedCameraToClipPerspective, worldToCamera);
} else {
this.expandedFarZProjMatrix = this.projMatrix;
}
const clipToCamera = index$1.invert([], cameraToClip);
this.frustumCorners = index$1.FrustumCorners.fromInvProjectionMatrix(clipToCamera, this.horizonLineFromTop(), this.height);
this.cameraFrustum = index$1.Frustum.fromInvProjectionMatrix(this.invProjMatrix, this.worldSize, 0, !isGlobe);
const view = new Float32Array(16);
index$1.identity(view);
index$1.scale$3(view, view, [1, -1, 1]);
index$1.rotateX$1(view, view, this._pitch);
index$1.rotateZ(view, view, this.angle);
const projection = index$1.perspective(new Float32Array(16), this._fov, this.width / this.height, this._nearZ, this._farZ);
this.starsProjMatrix = index$1.clone(projection);
const skyboxHorizonShift = (Math.PI / 2 - this._pitch) * (this.height / this._fov) * this._horizonShift;
projection[8] = -offset.x * 2 / this.width;
projection[9] = (offset.y + skyboxHorizonShift) * 2 / this.height;
this.skyboxMatrix = index$1.multiply(view, projection, view);
const point = this.point;
const x = point.x, y = point.y;
const xShift = this.width % 2 / 2, yShift = this.height % 2 / 2, angleCos = Math.cos(this.angle), angleSin = Math.sin(this.angle), dx = x - Math.round(x) + angleCos * xShift + angleSin * yShift, dy = y - Math.round(y) + angleCos * yShift + angleSin * xShift;
const alignedM = new Float64Array(m);
index$1.translate(alignedM, alignedM, [dx > 0.5 ? dx - 1 : dx, dy > 0.5 ? dy - 1 : dy, 0]);
this.alignedProjMatrix = alignedM;
m = index$1.create();
index$1.scale$3(m, m, [this.width / 2, -this.height / 2, 1]);
index$1.translate(m, m, [1, -1, 0]);
this.labelPlaneMatrix = m;
m = index$1.create();
index$1.scale$3(m, m, [1, -1, 1]);
index$1.translate(m, m, [-1, -1, 0]);
index$1.scale$3(m, m, [2 / this.width, 2 / this.height, 1]);
this.glCoordMatrix = m;
this.pixelMatrix = index$1.multiply(new Float64Array(16), this.labelPlaneMatrix, worldToClipPerspective);
this._calcFogMatrices();
this._distanceTileDataCache = {};
m = index$1.invert(new Float64Array(16), this.pixelMatrix);
if (!m) throw new Error("failed to invert matrix");
this.pixelMatrixInverse = m;
if (this.projection.name === "globe" || this.mercatorFromTransition) {
this.globeMatrix = index$1.calculateGlobeMatrix(this);
const globeCenter = [this.globeMatrix[12], this.globeMatrix[13], this.globeMatrix[14]];
this.globeCenterInViewSpace = index$1.transformMat4(globeCenter, globeCenter, worldToCamera);
this.globeRadius = this.worldSize / 2 / Math.PI - 1;
} else {
this.globeMatrix = m;
}
this._projMatrixCache = {};
this._alignedProjMatrixCache = {};
this._pixelsToTileUnitsCache = {};
this._expandedProjMatrixCache = {};
}
_calcFogMatrices() {
this._fogTileMatrixCache = {};
const cameraWorldSizeForFog = this.cameraWorldSizeForFog;
const cameraPixelsPerMeter = this.cameraPixelsPerMeter;
const cameraPos = this._camera.position;
const windowScaleFactor = 1 / this.height / this._pixelsPerMercatorPixel;
const metersToPixel = [cameraWorldSizeForFog, cameraWorldSizeForFog, cameraPixelsPerMeter];
index$1.scale$1(metersToPixel, metersToPixel, windowScaleFactor);
index$1.scale$1(cameraPos, cameraPos, -1);
index$1.multiply$1(cameraPos, cameraPos, metersToPixel);
const m = index$1.create();
index$1.translate(m, m, cameraPos);
index$1.scale$3(m, m, metersToPixel);
this.mercatorFogMatrix = m;
this.worldToFogMatrix = this._camera.getWorldToCameraPosition(cameraWorldSizeForFog, cameraPixelsPerMeter, windowScaleFactor);
}
_computeCameraPosition(targetPixelsPerMeter) {
targetPixelsPerMeter = targetPixelsPerMeter || this.pixelsPerMeter;
const pixelSpaceConversion = targetPixelsPerMeter / this.pixelsPerMeter;
const dir = this._camera.forward();
const center = this.point;
const zoom = this._seaLevelZoom ? this._seaLevelZoom : this._zoom;
const altitude = this._mercatorZfromZoom(zoom) * pixelSpaceConversion;
const distance = altitude - targetPixelsPerMeter / this.worldSize * this._centerAltitude;
return [
center.x / this.worldSize - dir[0] * distance,
center.y / this.worldSize - dir[1] * distance,
targetPixelsPerMeter / this.worldSize * this._centerAltitude - dir[2] * distance
];
}
_updateCameraState() {
if (!this.height) return;
this._camera.setPitchBearing(this._pitch, this.angle);
this._camera.position = this._computeCameraPosition();
}
/**
* Apply a 3d translation to the camera position, but clamping it so that
* it respects the maximum longitude and latitude range set.
*
* @param {vec3} translation The translation vector.
*/
_translateCameraConstrained(translation) {
const maxDistance = this._maxCameraBoundsDistance();
const maxZ = maxDistance * Math.cos(this._pitch);
const z = this._camera.position[2];
const deltaZ = translation[2];
let t = 1;
if (this.projection.wrap) this.center = this.center.wrap();
if (deltaZ > 0) {
t = Math.min((maxZ - z) / deltaZ, 1);
}
this._camera.position = index$1.scaleAndAdd([], this._camera.position, translation, t);
this._updateStateFromCamera();
}
_updateStateFromCamera() {
const position = this._camera.position;
const dir = this._camera.forward();
const { pitch, bearing } = this._camera.getPitchBearing();
const centerAltitude = index$1.mercatorZfromAltitude(this._centerAltitude, this.center.lat) * this._pixelsPerMercatorPixel;
const minHeight = this._mercatorZfromZoom(this._maxZoom) * Math.cos(index$1.degToRad(this._maxPitch));
const height = Math.max((position[2] - centerAltitude) / Math.cos(pitch), minHeight);
const zoom = this._zoomFromMercatorZ(height);
index$1.scaleAndAdd(position, position, dir, height);
this._pitch = index$1.clamp(pitch, index$1.degToRad(this.minPitch), index$1.degToRad(this.maxPitch));
this.angle = index$1.wrap(bearing, -Math.PI, Math.PI);
this._setZoom(index$1.clamp(zoom, this._minZoom, this._maxZoom));
this._updateSeaLevelZoom();
this._center = this.coordinateLocation(new index$1.MercatorCoordinate(position[0], position[1], position[2]));
this._unmodified = false;
this._constrain();
this._calcMatrices();
}
_worldSizeFromZoom(zoom) {
return Math.pow(2, zoom) * this.tileSize;
}
_mercatorZfromZoom(zoom) {
return this.cameraToCenterDistance / this._worldSizeFromZoom(zoom);
}
_minimumHeightOverTerrain() {
const MAX_DRAPE_OVERZOOM = 4;
const zoom = Math.min(this._seaLevelZoom != null ? this._seaLevelZoom : this._zoom, this._maxZoom) + MAX_DRAPE_OVERZOOM;
return this._mercatorZfromZoom(zoom);
}
_zoomFromMercatorZ(z) {
return this.scaleZoom(this.cameraToCenterDistance / (Math.max(0, z) * this.tileSize));
}
// This function is helpful to approximate true zoom given a mercator height with varying ppm.
// With Globe, since we use a fixed reference latitude at lower zoom levels and transition between this
// latitude and the center's latitude as you zoom in, camera to center distance varies dynamically.
// As the cameraToCenterDistance is a function of zoom, we need to approximate the true zoom
// given a mercator meter value in order to eliminate the zoom/cameraToCenterDistance dependency.
zoomFromMercatorZAdjusted(mercatorZ) {
index$1.assert(this.projection.name === "globe");
index$1.assert(mercatorZ !== 0);
let zoomLow = 0;
let zoomHigh = index$1.GLOBE_ZOOM_THRESHOLD_MAX;
let zoom = 0;
let minZoomDiff = Infinity;
const epsilon = 1e-6;
while (zoomHigh - zoomLow > epsilon && zoomHigh > zoomLow) {
const zoomMid = zoomLow + (zoomHigh - zoomLow) * 0.5;
const worldSize = this.tileSize * Math.pow(2, zoomMid);
const d = this.getCameraToCenterDistance(this.projection, zoomMid, worldSize);
const newZoom = this.scaleZoom(d / (Math.max(0, mercatorZ) * this.tileSize));
const diff = Math.abs(zoomMid - newZoom);
if (diff < minZoomDiff) {
minZoomDiff = diff;
zoom = zoomMid;
}
if (zoomMid < newZoom) {
zoomLow = zoomMid;
} else {
zoomHigh = zoomMid;
}
}
return zoom;
}
_terrainEnabled() {
if (!this._elevation) return false;
if (!this.projection.supportsTerrain) {
index$1.warnOnce("Terrain is not yet supported with alternate projections. Use mercator or globe to enable terrain.");
return false;
}
return true;
}
// Check if any of the four corners are off the edge of the rendered map
// This function will return `false` for all non-mercator projection
anyCornerOffEdge(p0, p1) {
const minX = Math.min(p0.x, p1.x);
const maxX = Math.max(p0.x, p1.x);
const minY = Math.min(p0.y, p1.y);
const maxY = Math.max(p0.y, p1.y);
const horizon = this.horizonLineFromTop(false);
if (minY < horizon) return true;
if (this.projection.name !== "mercator") {
return false;
}
const min = new index$1.Point(minX, minY);
const max = new index$1.Point(maxX, maxY);
const corners = [
min,
max,
new index$1.Point(minX, maxY),
new index$1.Point(maxX, minY)
];
const minWX = this.renderWorldCopies ? -NUM_WORLD_COPIES : 0;
const maxWX = this.renderWorldCopies ? 1 + NUM_WORLD_COPIES : 1;
const minWY = 0;
const maxWY = 1;
for (const corner of corners) {
const rayIntersection = this.pointRayIntersection(corner);
if (rayIntersection.t < 0) {
return true;
}
const coordinate = this.rayIntersectionCoordinate(rayIntersection);
if (coordinate.x < minWX || coordinate.y < minWY || coordinate.x > maxWX || coordinate.y > maxWY) {
return true;
}
}
return false;
}
// Checks the four corners of the frustum to see if they lie in the map's quad.
//
isHorizonVisible() {
const horizonAngleEpsilon = 2;
if (this.pitch + index$1.radToDeg(this.fovAboveCenter) > 90 - horizonAngleEpsilon) {
return true;
}
return this.anyCornerOffEdge(new index$1.Point(0, 0), new index$1.Point(this.width, this.height));
}
/**
* Converts a zoom delta value into a physical distance travelled in web mercator coordinates.
*
* @param {vec3} center Destination mercator point of the movement.
* @param {number} zoomDelta Change in the zoom value.
* @returns {number} The distance in mercator coordinates.
*/
zoomDeltaToMovement(center, zoomDelta) {
const distance = index$1.length(index$1.sub([], this._camera.position, center));
const relativeZoom = this._zoomFromMercatorZ(distance) + zoomDelta;
return distance - this._mercatorZfromZoom(relativeZoom);
}
/*
* The camera looks at the map from a 3D (lng, lat, altitude) location. Let's use `cameraLocation`
* as the name for the location under the camera and on the surface of the earth (lng, lat, 0).
* `cameraPoint` is the projected position of the `cameraLocation`.
*
* This point is useful to us because only fill-extrusions that are between `cameraPoint` and
* the query point on the surface of the earth can extend and intersect the query.
*
* When the map is not pitched the `cameraPoint` is equivalent to the center of the map because
* the camera is right above the center of the map.
*/
getCameraPoint() {
if (this.projection.name === "globe") {
const center = [this.globeMatrix[12], this.globeMatrix[13], this.globeMatrix[14]];
const pos = projectClamped(center, this.pixelMatrix);
return new index$1.Point(pos[0], pos[1]);
} else {
const pitch = this._pitch;
const yOffset = Math.tan(pitch) * (this.cameraToCenterDistance || 1);
return this.centerPoint.add(new index$1.Point(0, yOffset));
}
}
getCameraToCenterDistance(projection, zoom = this.zoom, worldSize = this.worldSize) {
const t = index$1.getProjectionInterpolationT(projection, zoom, this.width, this.height, 1024);
const projectionScaler = projection.pixelSpaceConversion(this.center.lat, worldSize, t);
let distance = 0.5 / Math.tan(this._fov * 0.5) * this.height * projectionScaler;
if (this.isOrthographic) {
const mixValue = this.pitch >= OrthographicPitchTranstionValue ? 1 : this.pitch / OrthographicPitchTranstionValue;
distance = index$1.number(1, distance, index$1.easeIn(mixValue));
}
return distance;
}
getWorldToCameraMatrix() {
const zUnit = this.projection.zAxisUnit === "meters" ? this.pixelsPerMeter : 1;
const worldToCamera = this._camera.getWorldToCamera(this.worldSize, zUnit);
if (this.projection.name === "globe") {
index$1.multiply(worldToCamera, worldToCamera, this.globeMatrix);
}
return worldToCamera;
}
getFrustum(zoom) {
const zInMeters = this.projection.zAxisUnit === "meters";
return index$1.Frustum.fromInvProjectionMatrix(this.invProjMatrix, this.worldSize, zoom, zInMeters);
}
}
const cutoffUniforms = (context) => ({
"u_cutoff_params": new index$1.Uniform4f(context)
});
const getCutoffParams = (painter, cutoffFadeRange) => {
if (cutoffFadeRange > 0 && painter.terrain) {
index$1.warnOnce("Cutoff is currently disabled on terrain");
}
if (cutoffFadeRange <= 0 || painter.terrain) {
return {
shouldRenderCutoff: false,
uniformValues: {
"u_cutoff_params": [0, 0, 0, 1]
}
};
}
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const tr = painter.transform;
const zoomScale = Math.max(Math.abs(tr._zoom - (painter.minCutoffZoom - 1)), 1);
const pitchScale = tr.isLODDisabled(false) ? index$1.smoothstep(MIN_LOD_PITCH, MIN_LOD_PITCH - 15, tr.pitch) : index$1.smoothstep(30, 15, tr.pitch);
const zRange = tr._farZ - tr._nearZ;
const cameraToCenterDistance = tr.cameraToCenterDistance;
const fadeRangePixels = cutoffFadeRange * tr.height;
const cutoffDistance = lerp(cameraToCenterDistance, tr._farZ + fadeRangePixels, pitchScale) * zoomScale;
const relativeCutoffDistance = (cutoffDistance - tr._nearZ) / zRange;
const relativeCutoffFadeDistance = (cutoffDistance - fadeRangePixels - tr._nearZ) / zRange;
return {
shouldRenderCutoff: pitchScale < 1,
uniformValues: {
"u_cutoff_params": [
tr._nearZ,
tr._farZ,
relativeCutoffDistance,
relativeCutoffFadeDistance
]
}
};
};
const shadowParameters = {
cascadeCount: 2,
normalOffset: 3,
shadowMapResolution: 2048
};
function lerpClamp$1(x, x1, x2, y1, y2) {
const t = index$1.clamp((x - x1) / (x2 - x1), 0, 1);
return (1 - t) * y1 + t * y2;
}
class ShadowReceiver {
constructor(aabb, lastCascade) {
this.aabb = aabb;
this.lastCascade = lastCascade;
}
}
class ShadowReceivers {
add(tileId, aabb) {
const receiver = this.receivers[tileId.key];
if (receiver !== void 0) {
receiver.aabb.min[0] = Math.min(receiver.aabb.min[0], aabb.min[0]);
receiver.aabb.min[1] = Math.min(receiver.aabb.min[1], aabb.min[1]);
receiver.aabb.min[2] = Math.min(receiver.aabb.min[2], aabb.min[2]);
receiver.aabb.max[0] = Math.max(receiver.aabb.max[0], aabb.max[0]);
receiver.aabb.max[1] = Math.max(receiver.aabb.max[1], aabb.max[1]);
receiver.aabb.max[2] = Math.max(receiver.aabb.max[2], aabb.max[2]);
} else {
this.receivers[tileId.key] = new ShadowReceiver(aabb, null);
}
}
clear() {
this.receivers = {};
}
get(tileId) {
return this.receivers[tileId.key];
}
// Returns the number of cascades that need to be rendered based on visibility on screen.
// Cascades that need to be rendered always include the first cascade.
computeRequiredCascades(frustum, worldSize, cascades) {
const frustumAabb = index$1.Aabb.fromPoints(frustum.points);
let lastCascade = 0;
for (const receiverKey in this.receivers) {
const receiver = this.receivers[receiverKey];
if (!receiver) continue;
if (!frustumAabb.intersectsAabb(receiver.aabb)) continue;
receiver.aabb.min = frustumAabb.closestPoint(receiver.aabb.min);
receiver.aabb.max = frustumAabb.closestPoint(receiver.aabb.max);
const clampedTileAabbPoints = receiver.aabb.getCorners();
for (let i = 0; i < cascades.length; i++) {
let aabbInsideCascade = true;
for (const point of clampedTileAabbPoints) {
const p = [point[0] * worldSize, point[1] * worldSize, point[2]];
index$1.transformMat4(p, p, cascades[i].matrix);
if (p[0] < -1 || p[0] > 1 || p[1] < -1 || p[1] > 1) {
aabbInsideCascade = false;
break;
}
}
receiver.lastCascade = i;
lastCascade = Math.max(lastCascade, i);
if (aabbInsideCascade) {
break;
}
}
}
return lastCascade + 1;
}
}
class ShadowRenderer {
constructor(painter) {
this.painter = painter;
this._enabled = false;
this._shadowLayerCount = 0;
this._numCascadesToRender = 0;
this._cascades = [];
this._groundShadowTiles = [];
this._receivers = new ShadowReceivers();
this._depthMode = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadWrite, [0, 1]);
this._uniformValues = defaultShadowUniformValues();
this._forceDisable = false;
this.useNormalOffset = false;
DevTools.addParameter(this, "_forceDisable", "Shadows", { label: "forceDisable" }, () => {
this.painter.style.map.triggerRepaint();
});
DevTools.addParameter(shadowParameters, "cascadeCount", "Shadows", { min: 1, max: 2, step: 1 });
DevTools.addParameter(shadowParameters, "normalOffset", "Shadows", { min: 0, max: 10, step: 0.05 });
DevTools.addParameter(shadowParameters, "shadowMapResolution", "Shadows", { min: 32, max: 2048, step: 32 });
DevTools.addBinding(this, "_numCascadesToRender", "Shadows", { readonly: true, label: "numCascadesToRender" });
}
destroy() {
for (const cascade of this._cascades) {
cascade.texture.destroy();
cascade.framebuffer.destroy();
}
this._cascades = [];
}
updateShadowParameters(transform, directionalLight) {
const painter = this.painter;
this._enabled = false;
this._shadowLayerCount = 0;
this._receivers.clear();
if (!directionalLight || !directionalLight.properties) {
return;
}
const shadowIntensity = directionalLight.properties.get("shadow-intensity");
if (!directionalLight.shadowsEnabled() || shadowIntensity <= 0) {
return;
}
this._shadowLayerCount = painter.style.order.reduce(
(accumulator, layerId) => {
const layer = painter.style._mergedLayers[layerId];
return accumulator + (layer.hasShadowPass() && !layer.isHidden(transform.zoom) ? 1 : 0);
},
0
);
this._enabled = this._shadowLayerCount > 0;
if (!this.enabled) {
return;
}
const context = painter.context;
const width = shadowParameters.shadowMapResolution;
const height = shadowParameters.shadowMapResolution;
if (this._cascades.length === 0 || shadowParameters.shadowMapResolution !== this._cascades[0].texture.size[0]) {
this._cascades = [];
for (let i = 0; i < shadowParameters.cascadeCount; ++i) {
const useColor = painter._shadowMapDebug;
const gl = context.gl;
const fbo = context.createFramebuffer(width, height, useColor ? 1 : 0, "texture");
const depthTexture = new index$1.Texture(context, { width, height, data: null }, gl.DEPTH_COMPONENT16);
fbo.depthAttachment.set(depthTexture.texture);
if (useColor) {
const colorTexture = new index$1.Texture(context, { width, height, data: null }, gl.RGBA8);
fbo.colorAttachment0.set(colorTexture.texture);
}
this._cascades.push({
framebuffer: fbo,
texture: depthTexture,
matrix: [],
far: 0,
boundingSphereRadius: 0,
frustum: new index$1.Frustum(),
scale: 0
});
}
}
this.shadowDirection = shadowDirectionFromProperties(directionalLight);
let verticalRange = 0;
if (transform.elevation) {
const elevation2 = transform.elevation;
const range = [1e4, -1e4];
elevation2.visibleDemTiles.filter((tile) => tile.dem).forEach((tile) => {
const minMaxTree = tile.dem.tree;
range[0] = Math.min(range[0], minMaxTree.minimums[0]);
range[1] = Math.max(range[1], minMaxTree.maximums[0]);
});
if (range[0] !== 1e4) {
verticalRange = (range[1] - range[0]) * elevation2.exaggeration();
}
}
const cascadeSplitDist = transform.cameraToCenterDistance * 1.5;
const shadowCutoutDist = cascadeSplitDist * 3;
const cameraInvProj = new Float64Array(16);
for (let cascadeIndex = 0; cascadeIndex < this._cascades.length; ++cascadeIndex) {
const cascade = this._cascades[cascadeIndex];
let near = transform.height / 50;
let far = 1;
if (shadowParameters.cascadeCount === 1) {
far = shadowCutoutDist;
} else {
if (cascadeIndex === 0) {
far = cascadeSplitDist;
} else {
near = cascadeSplitDist;
far = shadowCutoutDist;
}
}
const [matrix, radius] = createLightMatrix(transform, this.shadowDirection, near, far, shadowParameters.shadowMapResolution, verticalRange);
cascade.scale = transform.scale;
cascade.matrix = matrix;
cascade.boundingSphereRadius = radius;
index$1.invert(cameraInvProj, cascade.matrix);
cascade.frustum = index$1.Frustum.fromInvProjectionMatrix(cameraInvProj, 1, 0, true);
cascade.far = far;
}
const fadeRangeIdx = this._cascades.length - 1;
this._uniformValues["u_fade_range"] = [this._cascades[fadeRangeIdx].far * 0.75, this._cascades[fadeRangeIdx].far];
this._uniformValues["u_shadow_intensity"] = shadowIntensity;
this._uniformValues["u_shadow_direction"] = [this.shadowDirection[0], this.shadowDirection[1], this.shadowDirection[2]];
this._uniformValues["u_shadow_texel_size"] = 1 / shadowParameters.shadowMapResolution;
this._uniformValues["u_shadow_map_resolution"] = shadowParameters.shadowMapResolution;
this._uniformValues["u_shadowmap_0"] = TextureSlots.ShadowMap0;
this._uniformValues["u_shadowmap_1"] = TextureSlots.ShadowMap0 + 1;
const tileCoverOptions = {
tileSize: 512,
renderWorldCopies: true
};
this._groundShadowTiles = painter.transform.coveringTiles(tileCoverOptions);
const elevation = painter.transform.elevation;
for (const tileId of this._groundShadowTiles) {
let tileHeight = { min: 0, max: 0 };
if (elevation) {
const minMax = elevation.getMinMaxForTile(tileId);
if (minMax) tileHeight = minMax;
}
this.addShadowReceiver(tileId.toUnwrapped(), tileHeight.min, tileHeight.max);
}
}
get enabled() {
return this._enabled && !this._forceDisable;
}
set enabled(enabled) {
this._enabled = enabled;
}
drawShadowPass(style, sourceCoords) {
if (!this.enabled) {
return;
}
const painter = this.painter;
const context = painter.context;
index$1.assert(painter.renderPass === "shadow");
this._numCascadesToRender = this._receivers.computeRequiredCascades(painter.transform.getFrustum(0), painter.transform.worldSize, this._cascades);
context.viewport.set([0, 0, shadowParameters.shadowMapResolution, shadowParameters.shadowMapResolution]);
for (let cascade = 0; cascade < this._numCascadesToRender; ++cascade) {
painter.currentShadowCascade = cascade;
context.bindFramebuffer.set(this._cascades[cascade].framebuffer.framebuffer);
context.clear({ color: index$1.Color.white, depth: 1 });
for (const layerId of style.order) {
const layer = style._mergedLayers[layerId];
if (!layer.hasShadowPass() || layer.isHidden(painter.transform.zoom)) continue;
const sourceCache = style.getLayerSourceCache(layer);
const coords = sourceCache ? sourceCoords[sourceCache.id] : void 0;
if (layer.type !== "model" && !(coords && coords.length)) continue;
painter.renderLayer(painter, sourceCache, layer, coords);
}
}
painter.currentShadowCascade = 0;
}
drawGroundShadows() {
if (!this.enabled) {
return;
}
const painter = this.painter;
const style = painter.style;
const context = painter.context;
const gl = context.gl;
const directionalLight = style.directionalLight;
const ambientLight = style.ambientLight;
if (!directionalLight || !ambientLight) {
return;
}
const baseDefines = [];
const cutoffParams = getCutoffParams(painter, painter.longestCutoffRange);
if (cutoffParams.shouldRenderCutoff) {
baseDefines.push("RENDER_CUTOFF");
}
baseDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE");
if (this.useNormalOffset) {
baseDefines.push("NORMAL_OFFSET");
}
const shadowColor = calculateGroundShadowFactor(style, directionalLight, ambientLight);
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const stencilMode = new StencilMode({ func: gl.EQUAL, mask: 255 }, 0, 255, gl.KEEP, gl.KEEP, gl.KEEP);
for (const id of this._groundShadowTiles) {
const unwrapped = id.toUnwrapped();
const affectedByFog = painter.isTileAffectedByFog(id);
const program = painter.getOrCreateProgram("groundShadow", { defines: baseDefines, overrideFog: affectedByFog });
this.setupShadows(unwrapped, program);
painter.uploadCommonUniforms(context, program, unwrapped, null, cutoffParams);
const uniformValues = groundShadowUniformValues(painter.transform.calculateProjMatrix(unwrapped), shadowColor);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
ColorMode.multiply,
CullFaceMode.disabled,
uniformValues,
"ground_shadow",
painter.tileExtentBuffer,
painter.quadTriangleIndexBuffer,
painter.tileExtentSegments,
null,
painter.transform.zoom,
null,
null
);
}
}
getShadowPassColorMode() {
return this.painter._shadowMapDebug ? ColorMode.unblended : ColorMode.disabled;
}
getShadowPassDepthMode() {
return this._depthMode;
}
getShadowCastingLayerCount() {
return this._shadowLayerCount;
}
calculateShadowPassMatrixFromTile(unwrappedId) {
const tr = this.painter.transform;
const tileMatrix = tr.calculatePosMatrix(unwrappedId, tr.worldSize);
const lightMatrix = this._cascades[this.painter.currentShadowCascade].matrix;
index$1.multiply(tileMatrix, lightMatrix, tileMatrix);
return Float32Array.from(tileMatrix);
}
calculateShadowPassMatrixFromMatrix(matrix) {
const result = index$1.clone(matrix);
const lightMatrix = this._cascades[this.painter.currentShadowCascade].matrix;
index$1.multiply(result, lightMatrix, matrix);
return result;
}
setupShadows(unwrappedTileID, program, normalOffsetMode) {
if (!this.enabled) {
return;
}
const transform = this.painter.transform;
const context = this.painter.context;
const gl = context.gl;
const uniforms = this._uniformValues;
const lightMatrix = new Float64Array(16);
const tileMatrix = transform.calculatePosMatrix(unwrappedTileID, transform.worldSize);
for (let i = 0; i < this._cascades.length; i++) {
index$1.multiply(lightMatrix, this._cascades[i].matrix, tileMatrix);
uniforms[i === 0 ? "u_light_matrix_0" : "u_light_matrix_1"] = Float32Array.from(lightMatrix);
context.activeTexture.set(gl.TEXTURE0 + TextureSlots.ShadowMap0 + i);
this._cascades[i].texture.bindExtraParam(gl.LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.GREATER);
}
this.useNormalOffset = !!normalOffsetMode;
if (this.useNormalOffset) {
const meterInTiles = index$1.tileToMeter(unwrappedTileID.canonical);
const texelScale = 2 / transform.tileSize * index$1.EXTENT / shadowParameters.shadowMapResolution;
const shadowTexelInTileCoords0 = texelScale * this._cascades[0].boundingSphereRadius;
const shadowTexelInTileCoords1 = texelScale * this._cascades[this._cascades.length - 1].boundingSphereRadius;
const tileTypeMultiplier = normalOffsetMode === "vector-tile" ? 1 : 3;
const scale = tileTypeMultiplier * lerpClamp$1(transform.zoom, 22, 0, 0.125, 4);
const offset0 = shadowTexelInTileCoords0 * scale;
const offset1 = shadowTexelInTileCoords1 * scale;
uniforms["u_shadow_normal_offset"] = [meterInTiles, offset0, offset1];
uniforms["u_shadow_bias"] = [1e-4, 12e-4, 0.012];
} else {
uniforms["u_shadow_bias"] = [36e-5, 12e-4, 0.012];
}
program.setShadowUniformValues(context, uniforms);
}
setupShadowsFromMatrix(worldMatrix, program, normalOffset = false) {
if (!this.enabled) {
return;
}
const context = this.painter.context;
const gl = context.gl;
const uniforms = this._uniformValues;
const lightMatrix = new Float64Array(16);
for (let i = 0; i < shadowParameters.cascadeCount; i++) {
index$1.multiply(lightMatrix, this._cascades[i].matrix, worldMatrix);
uniforms[i === 0 ? "u_light_matrix_0" : "u_light_matrix_1"] = Float32Array.from(lightMatrix);
context.activeTexture.set(gl.TEXTURE0 + TextureSlots.ShadowMap0 + i);
this._cascades[i].texture.bindExtraParam(gl.LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE, gl.GREATER);
}
this.useNormalOffset = normalOffset;
if (normalOffset) {
const scale = shadowParameters.normalOffset;
uniforms["u_shadow_normal_offset"] = [1, scale, scale];
uniforms["u_shadow_bias"] = [6e-5, 12e-4, 0.012];
} else {
uniforms["u_shadow_bias"] = [36e-5, 12e-4, 0.012];
}
program.setShadowUniformValues(context, uniforms);
}
// When the same uniform values are used multiple times on different programs, it is sufficient
// to call program.setShadowUniformValues(context, uniforms) instead of calling setupShadowsFromMatrix multiple times.
getShadowUniformValues() {
return this._uniformValues;
}
getCurrentCascadeFrustum() {
return this._cascades[this.painter.currentShadowCascade].frustum;
}
computeSimplifiedTileShadowVolume(id, height, worldSize, lightDir) {
if (lightDir[2] >= 0) {
return {};
}
const corners = tileAabb(id, height, worldSize).getCorners();
const t = height / -lightDir[2];
if (lightDir[0] < 0) {
index$1.add$1(corners[0], corners[0], [lightDir[0] * t, 0, 0]);
index$1.add$1(corners[3], corners[3], [lightDir[0] * t, 0, 0]);
} else if (lightDir[0] > 0) {
index$1.add$1(corners[1], corners[1], [lightDir[0] * t, 0, 0]);
index$1.add$1(corners[2], corners[2], [lightDir[0] * t, 0, 0]);
}
if (lightDir[1] < 0) {
index$1.add$1(corners[0], corners[0], [0, lightDir[1] * t, 0]);
index$1.add$1(corners[1], corners[1], [0, lightDir[1] * t, 0]);
} else if (lightDir[1] > 0) {
index$1.add$1(corners[2], corners[2], [0, lightDir[1] * t, 0]);
index$1.add$1(corners[3], corners[3], [0, lightDir[1] * t, 0]);
}
const tileShadowVolume = {};
tileShadowVolume.vertices = corners;
tileShadowVolume.planes = [
computePlane(corners[1], corners[0], corners[4]),
// top
computePlane(corners[2], corners[1], corners[5]),
// right
computePlane(corners[3], corners[2], corners[6]),
// bottom
computePlane(corners[0], corners[3], corners[7])
];
return tileShadowVolume;
}
addShadowReceiver(tileId, minHeight, maxHeight) {
this._receivers.add(tileId, index$1.Aabb.fromTileIdAndHeight(tileId, minHeight, maxHeight));
}
getMaxCascadeForTile(tileId) {
const receiver = this._receivers.get(tileId);
return !!receiver && !!receiver.lastCascade ? receiver.lastCascade : 0;
}
}
function tileAabb(id, height, worldSize) {
const tileToWorld = worldSize / (1 << id.canonical.z);
const minx = id.canonical.x * tileToWorld + id.wrap * worldSize;
const maxx = (id.canonical.x + 1) * tileToWorld + id.wrap * worldSize;
const miny = id.canonical.y * tileToWorld + id.wrap * worldSize;
const maxy = (id.canonical.y + 1) * tileToWorld + id.wrap * worldSize;
return new index$1.Aabb([minx, miny, 0], [maxx, maxy, height]);
}
function computePlane(a, b, c) {
const bc = index$1.sub([], c, b);
const ba = index$1.sub([], a, b);
const normal = index$1.cross([], bc, ba);
const len = index$1.length(normal);
if (len === 0) {
return [0, 0, 1, 0];
}
index$1.scale$1(normal, normal, 1 / len);
return [normal[0], normal[1], normal[2], -index$1.dot(normal, b)];
}
function shadowDirectionFromProperties(directionalLight) {
const direction = directionalLight.properties.get("direction");
const spherical = index$1.cartesianPositionToSpherical(direction.x, direction.y, direction.z);
const MaxPolarCoordinate = 75;
spherical[2] = index$1.clamp(spherical[2], 0, MaxPolarCoordinate);
const position = index$1.sphericalPositionToCartesian([spherical[0], spherical[1], spherical[2]]);
return index$1.fromValues$2(position.x, position.y, position.z);
}
function calculateGroundShadowFactor(style, directionalLight, ambientLight) {
const dirColorIgnoreLut = directionalLight.properties.get("color-use-theme") === "none";
const dirColor = directionalLight.properties.get("color");
const dirIntensity = directionalLight.properties.get("intensity");
const dirDirection = directionalLight.properties.get("direction");
const directionVec = [dirDirection.x, dirDirection.y, dirDirection.z];
const ambientColorIgnoreLut = ambientLight.properties.get("color-use-theme") === "none";
const ambientColor = ambientLight.properties.get("color");
const ambientIntensity = ambientLight.properties.get("intensity");
const groundNormal = [0, 0, 1];
const dirDirectionalFactor = Math.max(index$1.dot(groundNormal, directionVec), 0);
const ambStrength = [0, 0, 0];
index$1.scale$1(ambStrength, ambientColor.toPremultipliedRenderColor(ambientColorIgnoreLut ? null : style.getLut(directionalLight.scope)).toArray01Linear().slice(0, 3), ambientIntensity);
const dirStrength = [0, 0, 0];
index$1.scale$1(dirStrength, dirColor.toPremultipliedRenderColor(dirColorIgnoreLut ? null : style.getLut(ambientLight.scope)).toArray01Linear().slice(0, 3), dirDirectionalFactor * dirIntensity);
const shadow = [
ambStrength[0] > 0 ? ambStrength[0] / (ambStrength[0] + dirStrength[0]) : 0,
ambStrength[1] > 0 ? ambStrength[1] / (ambStrength[1] + dirStrength[1]) : 0,
ambStrength[2] > 0 ? ambStrength[2] / (ambStrength[2] + dirStrength[2]) : 0
];
return index$1.linearVec3TosRGB(shadow);
}
function createLightMatrix(transform, shadowDirection, near, far, resolution, verticalRange) {
const zoom = transform.zoom;
const scale = transform.scale;
const ws = transform.worldSize;
const wsInverse = 1 / ws;
const aspectRatio = transform.aspect;
const k = Math.sqrt(1 + aspectRatio * aspectRatio) * Math.tan(transform.fovX * 0.5);
const k2 = k * k;
const farMinusNear = far - near;
const farPlusNear = far + near;
let centerDepth;
let radius;
if (k2 > farMinusNear / farPlusNear) {
centerDepth = far;
radius = far * k;
} else {
centerDepth = 0.5 * farPlusNear * (1 + k2);
radius = 0.5 * Math.sqrt(farMinusNear * farMinusNear + 2 * (far * far + near * near) * k2 + farPlusNear * farPlusNear * k2 * k2);
}
const pixelsPerMeter = transform.projection.pixelsPerMeter(transform.center.lat, ws);
const cameraToWorldMerc = transform._camera.getCameraToWorldMercator();
const sphereCenter = [0, 0, -centerDepth * wsInverse];
index$1.transformMat4(sphereCenter, sphereCenter, cameraToWorldMerc);
let sphereRadius = radius * wsInverse;
const frustumPointToMercator = function(point) {
point[0] /= scale;
point[1] /= scale;
point[2] = index$1.mercatorZfromAltitude(point[2], transform._center.lat);
return point;
};
const padding = transform._edgeInsets;
if (padding.left !== 0 || padding.top !== 0 || padding.right !== 0 || padding.bottom !== 0) {
if (padding.left !== padding.right || padding.top !== padding.bottom) {
const zUnit = transform.projection.zAxisUnit === "meters" ? pixelsPerMeter : 1;
const worldToCamera = transform._camera.getWorldToCamera(transform.worldSize, zUnit);
const cameraToClip = transform._camera.getCameraToClipPerspective(transform._fov, transform.width / transform.height, near, far);
cameraToClip[8] = -transform.centerOffset.x * 2 / transform.width;
cameraToClip[9] = transform.centerOffset.y * 2 / transform.height;
const cameraProj = new Float64Array(16);
index$1.mul(cameraProj, cameraToClip, worldToCamera);
const cameraInvProj = new Float64Array(16);
index$1.invert(cameraInvProj, cameraProj);
const frustum = index$1.Frustum.fromInvProjectionMatrix(cameraInvProj, ws, zoom, true);
for (const p of frustum.points) {
const fp = frustumPointToMercator(p);
sphereRadius = Math.max(sphereRadius, index$1.len(index$1.subtract([], sphereCenter, fp)));
}
}
}
const roundingMarginFactor = resolution / (resolution - 1);
sphereRadius *= roundingMarginFactor;
const pitch = Math.acos(shadowDirection[2]);
const bearing = Math.atan2(-shadowDirection[0], -shadowDirection[1]);
const camera = new FreeCamera();
camera.position = sphereCenter;
camera.setPitchBearing(pitch, bearing);
const lightWorldToView = camera.getWorldToCamera(ws, pixelsPerMeter);
const radiusPx = sphereRadius * ws;
const lightMatrixNearZ = Math.min(transform._mercatorZfromZoom(17) * ws * -2, radiusPx * -2);
const lightMatrixFarZ = (radiusPx + verticalRange * pixelsPerMeter) / shadowDirection[2];
const lightViewToClip = camera.getCameraToClipOrthographic(-radiusPx, radiusPx, -radiusPx, radiusPx, lightMatrixNearZ, lightMatrixFarZ);
const lightWorldToClip = new Float64Array(16);
index$1.multiply(lightWorldToClip, lightViewToClip, lightWorldToView);
const alignedCenter = index$1.fromValues$2(Math.floor(sphereCenter[0] * 1e6) / 1e6 * ws, Math.floor(sphereCenter[1] * 1e6) / 1e6 * ws, 0);
const halfResolution = 0.5 * resolution;
const projectedPoint = [0, 0, 0];
index$1.transformMat4(projectedPoint, alignedCenter, lightWorldToClip);
index$1.scale$1(projectedPoint, projectedPoint, halfResolution);
const roundedPoint = [Math.floor(projectedPoint[0]), Math.floor(projectedPoint[1]), Math.floor(projectedPoint[2])];
const offsetVec = [0, 0, 0];
index$1.sub(offsetVec, projectedPoint, roundedPoint);
index$1.scale$1(offsetVec, offsetVec, -1 / halfResolution);
const truncMatrix = new Float64Array(16);
index$1.identity(truncMatrix);
index$1.translate(truncMatrix, truncMatrix, offsetVec);
index$1.multiply(lightWorldToClip, truncMatrix, lightWorldToClip);
return [lightWorldToClip, radiusPx];
}
class ModelManager extends index$1.Evented {
constructor(requestManager) {
super();
this.requestManager = requestManager;
this.models = { "": {} };
this.modelUris = { "": {} };
this.modelByURL = {};
this.numModelsLoading = {};
}
loadModel(id, url) {
return index$1.loadGLTF(this.requestManager.transformRequest(url, index$1.ResourceType.Model).url).then((gltf) => {
if (!gltf) return;
const nodes = index$1.convertModel(gltf);
const model = new index$1.Model(id, url, void 0, void 0, nodes);
model.computeBoundsAndApplyParent();
return model;
}).catch((err) => {
if (err && err.status === 404) {
return null;
}
this.fire(new index$1.ErrorEvent(new Error(`Could not load model ${id} from ${url}: ${err.message}`)));
});
}
load(modelUris, scope, options = { forceReload: false }) {
if (!this.models[scope]) this.models[scope] = {};
const modelIds = Object.keys(modelUris);
const modelLoads = [];
const idsToLoad = [];
for (const modelId of modelIds) {
const modelURI = modelUris[modelId];
if (!this.hasURLBeenRequested(modelURI) || options.forceReload) {
this.modelByURL[modelURI] = { modelId, scope };
modelLoads.push(this.loadModel(modelId, modelURI));
idsToLoad.push(modelId);
}
if (!this.models[scope][modelId]) {
this.models[scope][modelId] = { model: null, numReferences: 1 };
}
}
this.numModelsLoading[scope] = (this.numModelsLoading[scope] || 0) + idsToLoad.length;
Promise.allSettled(modelLoads).then((results) => {
for (let i = 0; i < results.length; i++) {
const { status } = results[i];
if (status === "rejected") continue;
const { value } = results[i];
if (!this.models[scope][idsToLoad[i]]) {
this.models[scope][idsToLoad[i]] = { model: null, numReferences: 1 };
}
this.models[scope][idsToLoad[i]].model = value;
}
this.numModelsLoading[scope] -= idsToLoad.length;
this.fire(new index$1.Event("data", { dataType: "style" }));
}).catch((err) => {
this.fire(new index$1.ErrorEvent(new Error(`Could not load models: ${err.message}`)));
});
}
isLoaded() {
for (const scope in this.numModelsLoading) {
if (this.numModelsLoading[scope] > 0) return false;
}
return true;
}
hasModel(id, scope, options = { exactIdMatch: false }) {
return !!(options.exactIdMatch ? this.getModel(id, scope) : this.getModelByURL(this.modelUris[scope][id]));
}
getModel(id, scope) {
if (!this.models[scope]) this.models[scope] = {};
return this.models[scope][id] ? this.models[scope][id].model : void 0;
}
getModelByURL(modelURL) {
if (!modelURL) return null;
const referencedModel = this.modelByURL[modelURL];
if (!referencedModel) return null;
return this.models[referencedModel.scope][referencedModel.modelId].model;
}
hasModelBeenAdded(id, scope) {
return this.models[scope] && this.models[scope][id] !== void 0;
}
getModelURIs(scope) {
return this.modelUris[scope] || {};
}
addModel(id, url, scope) {
if (!this.models[scope]) this.models[scope] = {};
if (!this.modelUris[scope]) this.modelUris[scope] = {};
const normalizedModelURL = this.requestManager.normalizeModelURL(url);
if ((this.hasModel(id, scope, { exactIdMatch: true }) || this.hasModelBeenAdded(id, scope)) && this.modelUris[scope][id] === normalizedModelURL) {
this.models[scope][id].numReferences++;
} else if (this.hasURLBeenRequested(normalizedModelURL)) {
const { scope: scope2, modelId } = this.modelByURL[normalizedModelURL];
this.models[scope2][modelId].numReferences++;
} else {
this.modelUris[scope][id] = normalizedModelURL;
this.load({ [id]: this.modelUris[scope][id] }, scope);
}
}
addModelURLs(modelsToAdd, scope) {
if (!this.models[scope]) this.models[scope] = {};
if (!this.modelUris[scope]) this.modelUris[scope] = {};
const modelUris = this.modelUris[scope];
for (const modelId in modelsToAdd) {
const modelUri = modelsToAdd[modelId];
modelUris[modelId] = this.requestManager.normalizeModelURL(modelUri);
}
}
reloadModels(scope) {
this.load(this.modelUris[scope], scope, { forceReload: true });
}
addModelsFromBucket(modelIds, scope) {
if (!this.models[scope]) this.models[scope] = {};
if (!this.modelUris[scope]) this.modelUris[scope] = {};
const modelsRequests = {};
for (const modelId of modelIds) {
if (this.hasModel(modelId, scope, { exactIdMatch: true }) || this.hasURLBeenRequested(modelId)) {
this.models[scope][modelId].numReferences++;
} else if (this.modelUris[scope][modelId] && !this.hasURLBeenRequested(modelId)) {
modelsRequests[modelId] = this.modelUris[scope][modelId];
} else if (!this.hasURLBeenRequested(modelId) && index$1.isValidUrl(modelId, false)) {
this.modelUris[scope][modelId] = this.requestManager.normalizeModelURL(modelId);
modelsRequests[modelId] = this.modelUris[scope][modelId];
}
}
this.load(modelsRequests, scope);
}
hasURLBeenRequested(url) {
return this.modelByURL[url] !== void 0;
}
removeModel(id, scope, keepModelURI = false, forceRemoval = false) {
if (!this.models[scope] || !this.models[scope][id]) return;
this.models[scope][id].numReferences--;
if (this.models[scope][id].numReferences === 0 || forceRemoval) {
const modelURI = this.modelUris[scope][id];
if (!keepModelURI) delete this.modelUris[scope][id];
delete this.modelByURL[modelURI];
const model = this.models[scope][id].model;
if (!model) return;
delete this.models[scope][id];
model.destroy();
}
}
destroy() {
for (const scope of Object.keys(this.models)) {
for (const modelId of Object.keys(this.models[scope])) {
const model = this.models[scope][modelId].model;
delete this.models[scope][modelId];
if (model) model.destroy();
}
}
this.models = { "": {} };
this.modelUris = { "": {} };
this.modelByURL = {};
this.numModelsLoading = {};
}
listModels(scope) {
if (!this.models[scope]) this.models[scope] = {};
return Object.keys(this.models[scope]);
}
upload(painter, scope) {
if (!this.models[scope]) this.models[scope] = {};
for (const modelId in this.models[scope]) {
if (this.models[scope][modelId].model) {
this.models[scope][modelId].model.upload(painter.context);
}
}
}
}
const colorThemeReference = index$1.spec.colorTheme;
const colorizationProperties = new index$1.Properties({
data: new index$1.DataConstantProperty(colorThemeReference.data)
});
function evaluateColorThemeProperties(scope, values, configOptions, worldview) {
const properties = Object.assign({}, values);
for (const name of Object.keys(colorThemeReference)) {
if (properties[name] === void 0) {
properties[name] = colorThemeReference[name].default;
}
}
const transitionable = new index$1.Transitionable(colorizationProperties, scope, new Map(configOptions));
transitionable.setTransitionOrValue(properties, configOptions);
const transitioning = transitionable.untransitioned();
return transitioning.possiblyEvaluate(new index$1.EvaluationParameters(0, { worldview }));
}
function getContentArea(icon) {
if (!icon.metadata || !icon.metadata.content_area) {
return void 0;
}
const dpr = index$1.exported$1.devicePixelRatio;
const { left, top, width, height } = icon.metadata.content_area;
const scaledLeft = left * dpr;
const scaledTop = top * dpr;
return [
scaledLeft,
scaledTop,
scaledLeft + width * dpr,
scaledTop + height * dpr
];
}
function getStretchArea(stretchArea) {
if (!stretchArea) {
return void 0;
}
return stretchArea.map(([l, r]) => [l * index$1.exported$1.devicePixelRatio, r * index$1.exported$1.devicePixelRatio]);
}
function loadIconset(loadURL, requestManager, callback) {
return index$1.getArrayBuffer(requestManager.transformRequest(requestManager.normalizeIconsetURL(loadURL), index$1.ResourceType.Iconset), (err, data) => {
if (err) {
callback(err);
return;
}
const result = {};
const iconSet = index$1.readIconSet(new index$1.Pbf(data));
for (const icon of iconSet.icons) {
const styleImage = {
version: 1,
pixelRatio: index$1.exported$1.devicePixelRatio,
content: getContentArea(icon),
stretchX: icon.metadata ? getStretchArea(icon.metadata.stretch_x_areas) : void 0,
stretchY: icon.metadata ? getStretchArea(icon.metadata.stretch_y_areas) : void 0,
sdf: false,
usvg: true,
icon
};
result[icon.name] = styleImage;
}
callback(null, result);
});
}
class ImageProvider {
constructor(id, scope, sourceCache) {
this.id = id;
this.scope = scope;
this.sourceCache = sourceCache;
this.pendingRequests = /* @__PURE__ */ new Set();
this.missingRequests = /* @__PURE__ */ new Set();
}
addPendingRequest(imageId) {
if (this.missingRequests.has(imageId.name)) return;
if (!this.pendingRequests.has(imageId.name)) {
this.pendingRequests.add(imageId.name);
}
}
hasPendingRequests() {
return this.pendingRequests.size > 0;
}
/**
* Resolves pending image requests by extracting image data from visible tiles.
* Called during the Map's render cycle to process image requests that were
* added through addPendingRequest(). Supports only `RasterArrayTileSource`.
* @returns {StyleImageMap} Map of resolved image requests
*/
resolvePendingRequests() {
const styleImages = /* @__PURE__ */ new Map();
if (!this.sourceCache.loaded()) return styleImages;
const tileIDs = this.sourceCache.getVisibleCoordinates();
if (tileIDs.length === 0) return styleImages;
const source = this.sourceCache.getSource();
if (!(source instanceof RasterArrayTileSource)) return styleImages;
const tiles = tileIDs.map((tileID) => this.sourceCache.getTile(tileID));
const images = source.getImages(tiles, Array.from(this.pendingRequests));
for (const [name, styleImage] of images) {
styleImages.set(index$1.ImageId.from({ name, iconsetId: this.id }), styleImage);
this.pendingRequests.delete(name);
}
for (const imageId of this.pendingRequests) {
this.missingRequests.add(imageId);
}
this.pendingRequests.clear();
return styleImages;
}
}
const emitValidationErrors = (evented, errors) => emitValidationErrors$1(evented, errors && errors.filter((error) => error.identifier !== "source.canvas"));
const supportedDiffOperations = index$1.pick(operations, [
"addLayer",
"removeLayer",
"setLights",
"setPaintProperty",
"setLayoutProperty",
"setLayerProperty",
"setSlot",
"setFilter",
"addSource",
"removeSource",
"setLayerZoomRange",
"setLight",
"setTransition",
"setGeoJSONSourceData",
"setTerrain",
"setFog",
"setSnow",
"setRain",
"setProjection",
"setCamera",
"addImport",
"removeImport",
"updateImport",
"addIconset",
"removeIconset"
// 'setGlyphs',
// 'setSprite',
]);
const ignoredDiffOperations = index$1.pick(operations, [
"setCenter",
"setZoom",
"setBearing",
"setPitch"
]);
const featurelessLayerTypes = /* @__PURE__ */ new Set(["background", "sky", "slot", "custom"]);
const empty = emptyStyle();
const MAX_IMPORT_DEPTH = 5;
const defaultTransition = { duration: 300, delay: 0 };
class Style extends index$1.Evented {
constructor(map, options = {}) {
super();
this.map = map;
this.scope = options.scope || "";
this.globalId = null;
this.fragments = [];
this.importDepth = options.importDepth || 0;
this.importsCache = options.importsCache || /* @__PURE__ */ new Map();
this.resolvedImports = options.resolvedImports || /* @__PURE__ */ new Set();
this.transition = Object.assign({}, defaultTransition);
this._buildingIndex = new BuildingIndex(this);
this.crossTileSymbolIndex = new CrossTileSymbolIndex();
this._mergedOrder = [];
this._drapedFirstOrder = [];
this._mergedLayers = {};
this._mergedIndoor = {};
this._mergedSourceCaches = {};
this._mergedOtherSourceCaches = {};
this._mergedSymbolSourceCaches = {};
this._clipLayerPresent = false;
this._hasAppearances = false;
this._has3DLayers = false;
this._hasCircleLayers = false;
this._hasSymbolLayers = false;
this._importedAsBasemap = false;
this._changes = options.styleChanges || new StyleChanges();
this._hasDataDrivenEmissive = false;
if (options.dispatcher) {
this.dispatcher = options.dispatcher;
} else {
this.dispatcher = new index$1.Dispatcher(index$1.getGlobalWorkerPool(), this);
}
if (options.imageManager) {
this.imageManager = options.imageManager;
} else {
this.imageManager = new ImageManager(this.map._spriteFormat);
this.imageManager.setEventedParent(this);
}
this.imageManager.addScope(this.scope);
if (options.glyphManager) {
this.glyphManager = options.glyphManager;
} else {
this.glyphManager = new index$1.GlyphManager(
map._requestManager,
options.localFontFamily ? index$1.LocalGlyphMode.all : options.localIdeographFontFamily ? index$1.LocalGlyphMode.ideographs : index$1.LocalGlyphMode.none,
options.localFontFamily || options.localIdeographFontFamily
);
}
if (options.modelManager) {
this.modelManager = options.modelManager;
} else {
this.modelManager = new ModelManager(map._requestManager);
this.modelManager.setEventedParent(this);
}
this._layers = {};
this._sourceCaches = {};
this._otherSourceCaches = {};
this._symbolSourceCaches = {};
this._loaded = false;
this._precompileDone = false;
this._shouldPrecompile = false;
this._availableImages = [];
this._availableModels = {};
this._order = [];
this._markersNeedUpdate = false;
this.options = options.configOptions ? options.configOptions : /* @__PURE__ */ new Map();
this._configDependentLayers = options.configDependentLayers ? options.configDependentLayers : /* @__PURE__ */ new Set();
this._indoorDependentLayers = options.indoorDependentLayers ? options.indoorDependentLayers : /* @__PURE__ */ new Set();
this._config = options.config;
this._styleColorTheme = {
lut: null,
lutLoading: false,
lutLoadingCorrelationID: 0,
colorTheme: null,
colorThemeOverride: options.colorThemeOverride
};
this._styleColorThemeForScope = {};
this._initialConfig = options.initialConfig;
this.dispatcher.broadcast("setReferrer", index$1.getReferrer());
const self = this;
this._rtlTextPluginCallback = Style.registerForPluginStateChange((event) => {
const state = {
pluginStatus: event.pluginStatus,
pluginURL: event.pluginURL
};
self.dispatcher.broadcast("syncRTLPluginState", state, (err, results) => {
index$1.triggerPluginCompletionEvent(err);
if (results) {
const allComplete = results.every((elem) => elem);
if (allComplete) {
for (const id in self._sourceCaches) {
const sourceCache = self._sourceCaches[id];
const sourceCacheType = sourceCache.getSource().type;
if (sourceCacheType === "vector" || sourceCacheType === "geojson") {
sourceCache.reload();
}
}
}
}
});
});
this.on("data", (event) => {
if (event.dataType !== "source" || event.sourceDataType !== "metadata") {
return;
}
const source = this.getOwnSource(event.sourceId);
if (!source || !source.vectorLayerIds) {
return;
}
for (const layerId in this._layers) {
const layer = this._layers[layerId];
if (layer.source === source.id) {
this._validateLayer(layer);
}
}
});
}
load(style) {
if (!style) {
return this;
}
if (typeof style === "string") {
this.loadURL(style);
} else {
this.loadJSON(style);
}
return this;
}
_getGlobalId(loadedStyle) {
if (!loadedStyle) {
return null;
}
if (typeof loadedStyle === "string") {
if (index$1.isMapboxURL(loadedStyle)) {
return loadedStyle;
}
const url = index$1.stripQueryParameters(loadedStyle);
if (!url.startsWith("http")) {
try {
return new URL(url, location.href).toString();
} catch (e) {
return url;
}
}
return url;
}
return `json://${index$1.murmur3(JSON.stringify(loadedStyle))}`;
}
_diffStyle(style, onStarted, onFinished) {
this.globalId = this._getGlobalId(style);
const handleStyle = (json, callback) => {
try {
callback(null, this.setState(json, onFinished));
} catch (e) {
callback(e, false);
}
};
if (typeof style === "string") {
const url = this.map._requestManager.normalizeStyleURL(style);
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Style);
index$1.getJSON(request, (error, json) => {
if (error) {
this.fire(new index$1.ErrorEvent(error));
} else if (json) {
handleStyle(json, onStarted);
}
});
} else if (typeof style === "object") {
handleStyle(style, onStarted);
}
}
loadURL(url, options = {}) {
this.fire(new index$1.Event("dataloading", { dataType: "style" }));
const validate = typeof options.validate === "boolean" ? options.validate : !index$1.isMapboxURL(url);
this.globalId = this._getGlobalId(url);
url = this.map._requestManager.normalizeStyleURL(url, options.accessToken);
this.resolvedImports.add(url);
const cachedImport = this.importsCache.get(url);
if (cachedImport) return this._load(cachedImport, validate);
const request = this.map._requestManager.transformRequest(url, index$1.ResourceType.Style);
this._request = index$1.getJSON(request, (error, json) => {
this._request = null;
if (error) {
this.fire(new index$1.ErrorEvent(error));
} else if (json) {
this.importsCache.set(url, json);
return this._load(json, validate);
}
});
}
loadJSON(json, options = {}) {
this.fire(new index$1.Event("dataloading", { dataType: "style" }));
this.globalId = this._getGlobalId(json);
this._request = index$1.exported$1.frame(() => {
this._request = null;
this._load(json, options.validate !== false);
});
}
loadEmpty() {
this.fire(new index$1.Event("dataloading", { dataType: "style" }));
this._load(empty, false);
}
_loadImports(imports, validate, beforeId) {
if (this.importDepth >= MAX_IMPORT_DEPTH - 1) {
index$1.warnOnce(`Style doesn't support nesting deeper than ${MAX_IMPORT_DEPTH}`);
return Promise.resolve();
}
const waitForStyles = [];
for (const importSpec of imports) {
const style = this._createFragmentStyle(importSpec);
const waitForStyle = new Promise((resolve) => {
style.once("style.import.load", resolve);
style.once("error", resolve);
}).then(() => this.mergeAll());
waitForStyles.push(waitForStyle);
if (this.resolvedImports.has(importSpec.url)) {
style.loadEmpty();
continue;
}
const json = importSpec.data || this.importsCache.get(importSpec.url);
if (json) {
style.loadJSON(json, { validate });
if (this._isInternalStyle(json)) {
style.globalId = null;
}
} else if (importSpec.url) {
style.loadURL(importSpec.url, { validate });
} else {
style.loadEmpty();
}
const fragment = {
style,
id: importSpec.id,
config: importSpec.config
};
if (beforeId) {
const beforeIndex = this.fragments.findIndex(({ id }) => id === beforeId);
index$1.assert(beforeIndex !== -1, `Import with id "${beforeId}" does not exist on this map`);
this.fragments = this.fragments.slice(0, beforeIndex).concat(fragment).concat(this.fragments.slice(beforeIndex));
} else {
this.fragments.push(fragment);
}
}
return Promise.allSettled(waitForStyles);
}
getImportGlobalIds(style = this, ids = /* @__PURE__ */ new Set()) {
for (const fragment of style.fragments) {
if (fragment.style.globalId) {
ids.add(fragment.style.globalId);
}
this.getImportGlobalIds(fragment.style, ids);
}
return [...ids.values()];
}
_createFragmentStyle(importSpec) {
const scope = this.scope ? index$1.makeFQID(importSpec.id, this.scope) : importSpec.id;
let config;
const initialConfig = this._initialConfig && this._initialConfig[scope];
if (importSpec.config || initialConfig) {
config = Object.assign({}, importSpec.config, initialConfig);
}
const style = new Style(this.map, {
scope,
styleChanges: this._changes,
importDepth: this.importDepth + 1,
importsCache: this.importsCache,
// Clone resolvedImports so it's not being shared between siblings
resolvedImports: new Set(this.resolvedImports),
// Use shared Dispatcher and assets Managers between Styles
dispatcher: this.dispatcher,
imageManager: this.imageManager,
glyphManager: this.glyphManager,
modelManager: this.modelManager,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
config,
configOptions: this.options,
colorThemeOverride: importSpec["color-theme"],
configDependentLayers: this._configDependentLayers,
indoorDependentLayers: this._indoorDependentLayers
});
style.setEventedParent(this.map, { style });
return style;
}
_reloadImports() {
this.mergeAll();
this._updateMapProjection();
this.updateConfigDependencies();
this._updateLayers(this._indoorDependentLayers);
this.map._triggerCameraUpdate(this.camera);
this.dispatcher.broadcast("setLayers", {
layers: this._serializeLayers(this._order),
scope: this.scope,
options: this.options
});
this._shouldPrecompile = this.map._precompilePrograms && this.isRootStyle();
}
_isInternalStyle(json) {
return this.isRootStyle() && (json.fragment || !!json.schema && json.fragment !== false);
}
_load(json, validate) {
if (this._isInternalStyle(json)) {
const basemap = { id: "basemap", data: json, url: "" };
const style = Object.assign(
{},
empty,
{ imports: [basemap] },
json.center ? { center: json.center } : {},
json.bearing ? { bearing: json.bearing } : {},
json.pitch ? { pitch: json.pitch } : {},
json.zoom ? { zoom: json.zoom } : {},
json.light ? { light: json.light } : {}
);
this._importedAsBasemap = true;
this._load(style, validate);
return;
}
this.updateConfig(this._config, json.schema);
if (validate && emitValidationErrors(this, validateStyle(json))) {
return;
}
this._loaded = true;
this.stylesheet = index$1.clone$1(json);
const proceedWithStyleLoad = () => {
for (const id in json.sources) {
this.addSource(id, json.sources[id], { validate: false, isInitialLoad: true });
}
if (json.iconsets) {
for (const id in json.iconsets) {
this.addIconset(id, json.iconsets[id]);
}
}
if (json.sprite) {
this._loadIconset(json.sprite);
} else {
this.imageManager.setLoaded(true, this.scope);
this.dispatcher.broadcast("spriteLoaded", { scope: this.scope, isLoaded: true });
}
if (!this.glyphManager.url && json.glyphs) this.glyphManager.setURL(json.glyphs);
const layers = derefLayers(this.stylesheet.layers);
this._order = layers.map((layer) => layer.id);
if (this.stylesheet.light) {
index$1.warnOnce("The `light` root property is deprecated, prefer using `lights` with `flat` light type instead.");
}
if (this.stylesheet.lights) {
if (this.stylesheet.lights.length === 1 && this.stylesheet.lights[0].type === "flat") {
const flatLight = this.stylesheet.lights[0];
this.light = new Light(flatLight.properties, flatLight.id);
} else {
this.setLights(this.stylesheet.lights);
}
}
if (!this.light) {
this.light = new Light(this.stylesheet.light);
}
this._layers = {};
for (const layer of layers) {
const styleLayer = index$1.createStyleLayer(layer, this.scope, this._styleColorTheme.lut, this.options);
if (styleLayer.expressionDependencies.configDependencies.size !== 0) this._configDependentLayers.add(styleLayer.fqid);
if (styleLayer.expressionDependencies.isIndoorDependent) this._indoorDependentLayers.add(styleLayer.fqid);
this._hasAppearances = this._hasAppearances || styleLayer.getAppearances().length !== 0;
styleLayer.setEventedParent(this, { layer: { id: styleLayer.id } });
this._layers[styleLayer.id] = styleLayer;
const sourceCache = this.getOwnLayerSourceCache(styleLayer);
const shadowsEnabled = !!this.directionalLight && this.directionalLight.shadowsEnabled();
if (sourceCache && styleLayer.canCastShadows() && shadowsEnabled) {
sourceCache.castsShadows = true;
}
}
if (this.stylesheet.featuresets) {
this.setFeaturesetSelectors(this.stylesheet.featuresets);
}
if (this.stylesheet.models) {
this.addModelURLs(this.stylesheet.models);
}
const terrain = this.stylesheet.terrain;
if (terrain) {
this.checkCanvasFingerprintNoise();
if (!this.disableElevatedTerrain && !this.terrainSetForDrapingOnly()) {
this._createTerrain(terrain, DrapeRenderMode.elevated);
}
}
if (this.stylesheet.fog) {
this._createFog(this.stylesheet.fog);
}
if (this.stylesheet.snow) {
this._createSnow(this.stylesheet.snow);
}
if (this.stylesheet.rain) {
this._createRain(this.stylesheet.rain);
}
if (this.stylesheet.transition) {
this.setTransition(this.stylesheet.transition);
}
this.fire(new index$1.Event("data", { dataType: "style" }));
const isRootStyle = this.isRootStyle();
if (json.imports) {
this._loadImports(json.imports, validate).then(() => {
this._reloadImports();
this.fire(new index$1.Event(isRootStyle ? "style.load" : "style.import.load"));
}).catch((e) => {
this.fire(new index$1.ErrorEvent(new Error("Failed to load imports", e)));
this.fire(new index$1.Event(isRootStyle ? "style.load" : "style.import.load"));
});
} else {
this._reloadImports();
this.fire(new index$1.Event(isRootStyle ? "style.load" : "style.import.load"));
}
};
this._styleColorTheme.colorTheme = this.stylesheet["color-theme"];
const colorTheme = this._styleColorTheme.colorThemeOverride ? this._styleColorTheme.colorThemeOverride : this._styleColorTheme.colorTheme;
if (colorTheme) {
const data = this._evaluateColorThemeData(colorTheme);
this._loadColorTheme(data).then(() => {
proceedWithStyleLoad();
}).catch((e) => {
index$1.warnOnce(`Couldn't load color theme from the stylesheet: ${e}`);
proceedWithStyleLoad();
});
} else {
this._styleColorTheme.lut = null;
proceedWithStyleLoad();
}
}
isRootStyle() {
return this.importDepth === 0;
}
hasAppearances() {
return this._hasAppearances || this.fragments.some(((f) => f.style.hasAppearances()));
}
mergeAll() {
let light;
let ambientLight;
let directionalLight;
let terrain;
let fog;
let snow;
let rain;
let projection;
let transition;
let camera;
const styleColorThemeForScope = {};
if (this.terrain && this.terrain.scope !== this.scope) {
delete this.terrain;
}
this.forEachFragmentStyle((style) => {
if (!style.stylesheet) return;
if (style.light != null)
light = style.light;
if (style.stylesheet.lights) {
for (const light2 of style.stylesheet.lights) {
if (light2.type === "ambient" && style.ambientLight != null)
ambientLight = style.ambientLight;
if (light2.type === "directional" && style.directionalLight != null)
directionalLight = style.directionalLight;
}
}
terrain = this._prioritizeTerrain(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
terrain,
style.terrain,
style.stylesheet.terrain
);
if (style.stylesheet.fog && style.fog != null)
fog = style.fog;
if (style.stylesheet.snow && style.snow != null)
snow = style.snow;
if (style.stylesheet.rain && style.rain != null)
rain = style.rain;
if (style.stylesheet.camera != null)
camera = style.stylesheet.camera;
if (style.stylesheet.projection != null)
projection = style.stylesheet.projection;
if (style.stylesheet.transition != null)
transition = style.stylesheet.transition;
styleColorThemeForScope[style.scope] = style._styleColorTheme;
});
this.light = light;
this.ambientLight = ambientLight;
this.directionalLight = directionalLight;
this.fog = fog;
this.snow = snow;
this.rain = rain;
this._styleColorThemeForScope = styleColorThemeForScope;
if (terrain === null) {
delete this.terrain;
} else {
this.terrain = terrain;
}
this.camera = camera || { "camera-projection": "perspective" };
this.projection = projection || { name: "mercator" };
this.transition = Object.assign({}, defaultTransition, transition);
this.mergeSources();
this.mergeLayers();
this.mergeIndoor();
}
forEachFragmentStyle(fn) {
const traverse = (style) => {
for (const fragment of style.fragments) {
traverse(fragment.style);
}
fn(style);
};
traverse(this);
}
_prioritizeTerrain(prevTerrain, nextTerrain, nextTerrainSpec) {
const prevIsDeffered = prevTerrain && prevTerrain.drapeRenderMode === DrapeRenderMode.deferred;
const nextIsDeffered = nextTerrain && nextTerrain.drapeRenderMode === DrapeRenderMode.deferred;
if (nextTerrainSpec === null) {
if (nextIsDeffered) return nextTerrain;
if (prevIsDeffered) return prevTerrain;
return null;
}
if (nextTerrain != null) {
const nextIsElevated = nextTerrain && nextTerrain.drapeRenderMode === DrapeRenderMode.elevated;
if (!prevTerrain || prevIsDeffered || nextIsElevated) return nextTerrain;
}
return prevTerrain;
}
mergeTerrain() {
let terrain;
if (this.terrain && this.terrain.scope !== this.scope) {
delete this.terrain;
}
this.forEachFragmentStyle((style) => {
terrain = this._prioritizeTerrain(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
terrain,
style.terrain,
style.stylesheet.terrain
);
});
if (terrain === null) {
delete this.terrain;
} else {
this.terrain = terrain;
}
}
mergeProjection() {
let projection;
this.forEachFragmentStyle((style) => {
if (style.stylesheet.projection != null)
projection = style.stylesheet.projection;
});
this.projection = projection || { name: "mercator" };
}
mergeSources() {
const mergedSourceCaches = {};
const mergedOtherSourceCaches = {};
const mergedSymbolSourceCaches = {};
this.forEachFragmentStyle((style) => {
for (const id in style._sourceCaches) {
const fqid = index$1.makeFQID(id, style.scope);
mergedSourceCaches[fqid] = style._sourceCaches[id];
}
for (const id in style._otherSourceCaches) {
const fqid = index$1.makeFQID(id, style.scope);
mergedOtherSourceCaches[fqid] = style._otherSourceCaches[id];
}
for (const id in style._symbolSourceCaches) {
const fqid = index$1.makeFQID(id, style.scope);
mergedSymbolSourceCaches[fqid] = style._symbolSourceCaches[id];
}
});
this._mergedSourceCaches = mergedSourceCaches;
this._mergedOtherSourceCaches = mergedOtherSourceCaches;
this._mergedSymbolSourceCaches = mergedSymbolSourceCaches;
}
mergeIndoor() {
this.forEachFragmentStyle((style) => {
if (style.stylesheet && style.stylesheet.indoor) {
for (const indoor of Object.values(style.stylesheet.indoor)) {
const indoorSpec = indoor;
const fqid = index$1.makeFQID(indoorSpec.sourceId, style.scope);
this._mergedIndoor[fqid] = new Set(indoorSpec.sourceLayers || []);
}
}
});
}
mergeLayers() {
const slots = {};
const mergedOrder = [];
const mergedLayers = {};
this._mergedSlots = [];
this._has3DLayers = false;
this._hasCircleLayers = false;
this._hasSymbolLayers = false;
this.forEachFragmentStyle((style) => {
for (const layerId of style._order) {
const layer = style._layers[layerId];
if (layer.type === "slot") {
const slotName = index$1.getNameFromFQID(layerId);
if (slots[slotName]) continue;
else slots[slotName] = [];
}
if (layer.slot && slots[layer.slot]) {
slots[layer.slot].push(layer);
continue;
}
mergedOrder.push(layer);
}
});
this._mergedOrder = [];
let last3DLayerIdx = -1;
const sort = (layers = []) => {
for (const layer of layers) {
if (layer.type === "slot") {
const slotName = index$1.getNameFromFQID(layer.id);
if (slots[slotName]) sort(slots[slotName]);
this._mergedSlots.push(slotName);
} else {
const fqid = index$1.makeFQID(layer.id, layer.scope);
this._mergedOrder.push(fqid);
mergedLayers[fqid] = layer;
if (layer.is3D(!!this.terrain)) {
this._has3DLayers = true;
last3DLayerIdx = this._mergedOrder.length - 1;
}
if (layer.type === "circle") this._hasCircleLayers = true;
if (layer.type === "symbol") this._hasSymbolLayers = true;
if (layer.type === "clip") this._clipLayerPresent = true;
}
}
};
sort(mergedOrder);
if (this._has3DLayers) {
const priorities = {};
for (let i = 0; i < this._mergedOrder.length; ++i) {
const layerName = this._mergedOrder[i];
const layer = mergedLayers[layerName];
if (i === last3DLayerIdx) {
priorities[layerName] = 1;
} else if (i < last3DLayerIdx) {
if (layer.hasOcclusionOpacityProperties) {
priorities[layerName] = 2;
} else {
priorities[layerName] = 0;
}
} else {
priorities[layerName] = 4;
}
}
this._mergedOrder.sort((layerName1, layerName2) => {
const p1 = priorities[layerName1];
const p2 = priorities[layerName2];
return p1 - p2;
});
}
this._mergedLayers = mergedLayers;
this.updateDrapeFirstLayers();
this._buildingIndex.processLayersChanged();
this._updateDataDrivenEmissiveStrength();
}
terrainSetForDrapingOnly() {
return !!this.terrain && this.terrain.drapeRenderMode === DrapeRenderMode.deferred;
}
getCamera() {
return this.stylesheet.camera;
}
setCamera(camera) {
this.stylesheet.camera = Object.assign({}, this.stylesheet.camera, camera);
this.camera = this.stylesheet.camera;
return this;
}
_evaluateColorThemeData(theme) {
if (!theme.data) {
return null;
}
const properties = evaluateColorThemeProperties(this.scope, theme, this.options);
return properties.get("data");
}
_loadColorTheme(inputData) {
this._styleColorTheme.lutLoading = true;
this._styleColorTheme.lutLoadingCorrelationID += 1;
const correlationID = this._styleColorTheme.lutLoadingCorrelationID;
return new Promise((resolve, reject) => {
const dataURLPrefix = "data:image/png;base64,";
if (!inputData || inputData.length === 0) {
this._styleColorTheme.lut = null;
this._styleColorTheme.lutLoading = false;
resolve();
return;
}
let colorThemeData = inputData;
if (!colorThemeData.startsWith(dataURLPrefix)) {
colorThemeData = dataURLPrefix + colorThemeData;
}
const styleLutName = index$1.ImageId.from("mapbox-reserved-lut");
const lutImage = new Image();
lutImage.src = colorThemeData;
lutImage.onerror = () => {
this._styleColorTheme.lutLoading = false;
reject(new Error("Failed to load image data"));
};
lutImage.onload = () => {
if (this._styleColorTheme.lutLoadingCorrelationID !== correlationID) {
resolve();
return;
}
this._styleColorTheme.lutLoading = false;
const { width, height, data } = index$1.exported$1.getImageData(lutImage);
if (height > 32) {
reject(new Error("The height of the image must be less than or equal to 32 pixels."));
return;
}
if (width !== height * height) {
reject(new Error("The width of the image must be equal to the height squared."));
return;
}
if (this.getImage(styleLutName)) {
this.removeImage(styleLutName);
}
this.addImage(styleLutName, { data: new index$1.RGBAImage({ width, height }, data), pixelRatio: 1, sdf: false, usvg: false, version: 0 });
const image = this.imageManager.getImage(styleLutName, this.scope);
if (!image) {
reject(new Error("Missing LUT image."));
} else {
this._styleColorTheme.lut = {
image: image.data,
data: inputData
};
resolve();
}
};
});
}
getLut(scope) {
const styleColorTheme = this._styleColorThemeForScope[scope];
return styleColorTheme ? styleColorTheme.lut : null;
}
setProjection(projection) {
if (projection) {
this.stylesheet.projection = projection;
} else {
delete this.stylesheet.projection;
}
this.mergeProjection();
this._updateMapProjection();
}
applyProjectionUpdate() {
if (!this._loaded) return;
this.dispatcher.broadcast("setProjection", this.map.transform.projectionOptions);
if (this.map.transform.projection.requiresDraping) {
const hasTerrain = (this.getTerrain() || this.stylesheet.terrain) && !this.disableElevatedTerrain;
if (!hasTerrain) {
this.setTerrainForDraping();
}
} else if (this.terrainSetForDrapingOnly()) {
this.setTerrain(null, DrapeRenderMode.deferred);
}
}
_updateMapProjection() {
if (!this.isRootStyle()) return;
if (!this.map._useExplicitProjection) {
this.map._prioritizeAndUpdateProjection(null, this.projection);
} else {
this.applyProjectionUpdate();
}
}
/**
* Loads a sprite from the given URL.
* @fires Map.event:data Fires `data` with `{dataType: 'style'}` to indicate that sprite loading is complete.
*/
_loadSprite(url) {
this._spriteRequest = loadSprite(url, this.map._requestManager, (err, images) => {
this._spriteRequest = null;
if (err) {
this.fire(new index$1.ErrorEvent(err));
} else if (images) {
const styleImageMap = /* @__PURE__ */ new Map();
for (const id in images) {
styleImageMap.set(index$1.ImageId.from(id), images[id]);
}
this.addImages(styleImageMap);
}
this.imageManager.setLoaded(true, this.scope);
this.dispatcher.broadcast("spriteLoaded", { scope: this.scope, isLoaded: true });
this.fire(new index$1.Event("data", { dataType: "style" }));
});
}
addIconset(iconsetId, iconset) {
if (iconset.type === "sprite") {
this._loadSprite(iconset.url);
return;
}
const sourceCache = this.getOwnSourceCache(iconset.source);
if (!sourceCache) {
this.fire(new index$1.ErrorEvent(new Error(`Source "${iconset.source}" as specified by iconset "${iconsetId}" does not exist and cannot be used as an iconset source`)));
return;
}
const source = sourceCache.getSource();
if (source.type !== "raster-array") {
this.fire(new index$1.ErrorEvent(new Error(`Source "${iconset.source}" as specified by iconset "${iconsetId}" is not a "raster-array" source and cannot be used as an iconset source`)));
return;
}
source.partial = false;
const imageProvider = new ImageProvider(iconsetId, this.scope, sourceCache);
this.imageManager.addImageProvider(imageProvider, this.scope);
}
removeIconset(iconsetId) {
this.imageManager.removeImageProvider(iconsetId, this.scope);
}
/**
* Loads an iconset from the given URL. If the sprite is not a Mapbox URL, it loads a raster sprite.
* @fires Map.event:data Fires `data` with `{dataType: 'style'}` to indicate that sprite loading is complete.
*/
_loadIconset(url) {
if (!index$1.isMapboxURL(url) && this.map._spriteFormat !== "icon_set" || this.map._spriteFormat === "raster") {
this._loadSprite(url);
return;
}
const isFallbackExists = this.map._spriteFormat === "auto";
this._spriteRequest = loadIconset(url, this.map._requestManager, (err, images) => {
this._spriteRequest = null;
if (err) {
if (isFallbackExists) {
this._loadSprite(url);
} else {
this.fire(new index$1.ErrorEvent(err));
}
} else if (images) {
const styleImageMap = /* @__PURE__ */ new Map();
for (const id in images) {
styleImageMap.set(index$1.ImageId.from(id), images[id]);
}
this.addImages(styleImageMap);
}
this.imageManager.setLoaded(true, this.scope);
this.dispatcher.broadcast("spriteLoaded", { scope: this.scope, isLoaded: true });
this.fire(new index$1.Event("data", { dataType: "style" }));
});
}
_validateLayer(layer) {
const source = this.getOwnSource(layer.source);
if (!source) {
return;
}
const sourceLayer = layer.sourceLayer;
if (!sourceLayer) {
return;
}
if (source.type === "geojson" || source.vectorLayerIds && source.vectorLayerIds.indexOf(sourceLayer) === -1) {
this.fire(new index$1.ErrorEvent(new Error(
`Source layer "${sourceLayer}" does not exist on source "${source.id}" as specified by style layer "${layer.id}"`
)));
}
}
loaded() {
if (!this._loaded)
return false;
if (Object.keys(this._changes.getUpdatedSourceCaches()).length)
return false;
for (const id in this._sourceCaches)
if (!this._sourceCaches[id].loaded())
return false;
if (!this.imageManager.isLoaded())
return false;
if (this.imageManager.hasPatternsInFlight())
return false;
if (!this.modelManager.isLoaded())
return false;
if (this._styleColorTheme.lutLoading)
return false;
for (const { style } of this.fragments) {
if (!style.loaded()) return false;
}
return true;
}
_serializeImports() {
if (!this.stylesheet.imports) return void 0;
return this.stylesheet.imports.map((importSpec, index) => {
const fragment = this.fragments[index];
if (fragment && fragment.style) {
importSpec.data = fragment.style.serialize();
}
return importSpec;
});
}
_serializeSources() {
const sources = {};
for (const cacheId in this._sourceCaches) {
const source = this._sourceCaches[cacheId].getSource();
if (!sources[source.id]) {
sources[source.id] = source.serialize();
}
}
return sources;
}
_serializeLayers(ids) {
const serializedLayers = [];
for (const id of ids) {
const layer = this._layers[id];
if (layer && layer.type !== "custom") {
serializedLayers.push(layer.serialize());
}
}
return serializedLayers;
}
hasLightTransitions() {
if (this.light && this.light.hasTransition()) {
return true;
}
if (this.ambientLight && this.ambientLight.hasTransition()) {
return true;
}
if (this.directionalLight && this.directionalLight.hasTransition()) {
return true;
}
return false;
}
hasFogTransition() {
if (!this.fog) return false;
return this.fog.hasTransition();
}
hasSnowTransition() {
if (!this.snow) return false;
return this.snow.hasTransition();
}
hasRainTransition() {
if (!this.rain) return false;
return this.rain.hasTransition();
}
hasTransitions() {
if (this.hasLightTransitions()) {
return true;
}
if (this.hasFogTransition()) {
return true;
}
if (this.hasSnowTransition()) {
return true;
}
if (this.hasRainTransition()) {
return true;
}
for (const id in this._sourceCaches) {
if (this._sourceCaches[id].hasTransition()) {
return true;
}
}
for (const layerId in this._layers) {
const layer = this._layers[layerId];
if (layer.hasTransition()) {
return true;
}
}
return false;
}
_updateDataDrivenEmissiveStrength() {
for (const layerId in this._mergedLayers) {
const layer = this._mergedLayers[layerId];
if (layer._transitionablePaint && layer._transitionablePaint._values) {
const transitionableValue = layer._transitionablePaint._values["line-emissive-strength"];
if (transitionableValue && transitionableValue.value && transitionableValue.value.isDataDriven()) {
this._hasDataDrivenEmissive = true;
return;
}
}
}
this._hasDataDrivenEmissive = false;
}
hasDataDrivenEmissiveStrength() {
return this._hasDataDrivenEmissive;
}
get order() {
if (this.terrain) {
index$1.assert(this._drapedFirstOrder.length === this._mergedOrder.length, "drapedFirstOrder doesn't match order");
return this._drapedFirstOrder;
}
return this._mergedOrder;
}
/**
* Returns active order for when terrain or globe are enabled (when draping is enabled).
* @param drapingEnabled {boolean} speficy if order is requested for draping enabled.
* @private
*/
_getOrder(drapingEnabled) {
return drapingEnabled ? this.order : this._mergedOrder;
}
isLayerDraped(layer) {
if (!this.terrain) return false;
return layer.isDraped(this.getLayerSourceCache(layer));
}
_checkLoaded() {
if (!this._loaded) {
throw new Error("Style is not done loading");
}
}
_checkLayer(layerId) {
const layer = this.getOwnLayer(layerId);
if (!layer) {
this.fire(new index$1.ErrorEvent(new Error(`The layer '${layerId}' does not exist in the map's style.`)));
return;
}
return layer;
}
_checkSource(sourceId) {
const source = this.getOwnSource(sourceId);
if (!source) {
this.fire(new index$1.ErrorEvent(new Error(`The source '${sourceId}' does not exist in the map's style.`)));
return;
}
return source;
}
precompilePrograms(layer, parameters) {
const painter = this.map.painter;
if (!painter) {
return;
}
for (let i = layer.minzoom || DEFAULT_MIN_ZOOM; i < (layer.maxzoom || DEFAULT_MAX_ZOOM); i++) {
const programIds = layer.getProgramIds();
if (!programIds) continue;
for (const programId of programIds) {
const params = layer.getDefaultProgramParams(programId, parameters.zoom, this._styleColorTheme.lut);
if (params) {
painter.style = this;
if (this.fog) {
painter._fogVisible = true;
params.overrideFog = true;
painter.getOrCreateProgram(programId, params);
}
painter._fogVisible = false;
params.overrideFog = false;
painter.getOrCreateProgram(programId, params);
if (this.stylesheet.terrain || this.stylesheet.projection && this.stylesheet.projection.name === "globe") {
params.overrideRtt = true;
painter.getOrCreateProgram(programId, params);
}
}
}
}
}
/**
* Apply queued style updates in a batch and recalculate zoom-dependent paint properties.
* @private
*/
update(parameters) {
if (!this._loaded) {
return;
}
if (this.ambientLight) {
this.ambientLight.recalculate(parameters);
}
if (this.directionalLight) {
this.directionalLight.recalculate(parameters);
}
const brightness = this.calculateLightsBrightness();
parameters.brightness = brightness || 0;
if (brightness !== this._brightness) {
this._brightness = brightness;
this.dispatcher.broadcast("setBrightness", brightness);
}
if (parameters.worldview !== this._worldview) {
this._worldview = parameters.worldview;
this.dispatcher.broadcast("setWorldview", this._worldview);
}
const changed = this._changes.isDirty();
let layersUpdated = false;
if (this._changes.isDirty()) {
const updatesByScope = this._changes.getLayerUpdatesByScope();
for (const scope in updatesByScope) {
const { updatedIds, removedIds } = updatesByScope[scope];
if (updatedIds || removedIds) {
this._updateWorkerLayers(scope, updatedIds, removedIds);
layersUpdated = true;
}
}
this.updateSourceCaches();
this._updateTilesForChangedImages();
this.updateLayers(parameters);
if (this.light) {
this.light.updateTransitions(parameters);
}
if (this.ambientLight) {
this.ambientLight.updateTransitions(parameters);
}
if (this.directionalLight) {
this.directionalLight.updateTransitions(parameters);
}
if (this.fog) {
this.fog.updateTransitions(parameters);
}
if (this.snow) {
this.snow.updateTransitions(parameters);
}
if (this.rain) {
this.rain.updateTransitions(parameters);
}
this._changes.reset();
}
const sourcesUsedBefore = {};
for (const sourceId in this._mergedSourceCaches) {
const sourceCache = this._mergedSourceCaches[sourceId];
sourcesUsedBefore[sourceId] = sourceCache.used;
sourceCache.used = false;
sourceCache.tileCoverLift = 0;
}
for (const layerId of this._mergedOrder) {
const layer = this._mergedLayers[layerId];
if (layer.visibility !== "none") layer.recalculate(parameters, this._availableImages);
if (!layer.isHidden(parameters.zoom)) {
const sourceCache = this.getLayerSourceCache(layer);
if (sourceCache) {
sourceCache.used = true;
sourceCache.tileCoverLift = Math.max(sourceCache.tileCoverLift, layer.tileCoverLift());
}
}
if (!this._precompileDone && this._shouldPrecompile) {
if ("requestIdleCallback" in window) {
requestIdleCallback(() => {
this.precompilePrograms(layer, parameters);
});
} else {
this.precompilePrograms(layer, parameters);
}
}
}
if (this._shouldPrecompile) {
this._precompileDone = true;
}
if (this.terrain && layersUpdated) {
this.mergeLayers();
}
const pendingImageProviders = this.imageManager.getPendingImageProviders();
for (const imageProvider of pendingImageProviders) {
imageProvider.sourceCache.used = true;
}
for (const sourceId in sourcesUsedBefore) {
const sourceCache = this._mergedSourceCaches[sourceId];
if (sourcesUsedBefore[sourceId] !== sourceCache.used) {
const source = sourceCache.getSource();
source.fire(new index$1.Event("data", { sourceDataType: "visibility", dataType: "source", sourceId: sourceCache.getSource().id }));
}
}
if (this.light) {
this.light.recalculate(parameters);
}
if (this.terrain) {
this.terrain.recalculate(parameters);
}
if (this.fog) {
this.fog.recalculate(parameters);
}
if (this.snow) {
this.snow.recalculate(parameters);
}
if (this.rain) {
this.rain.recalculate(parameters);
}
this.z = parameters.zoom;
if (this._markersNeedUpdate) {
this._updateMarkersOpacity();
this._markersNeedUpdate = false;
}
this.imageManager.clearUpdatedImages(this.scope);
if (changed) {
this.fire(new index$1.Event("data", { dataType: "style" }));
}
}
/**
* Resolves pending image requests from ImageProviders during the map render cycle.
* @private
*/
updateImageProviders() {
const pendingImageProviders = this.imageManager.getPendingImageProviders();
for (const imageProvider of pendingImageProviders) {
const images = imageProvider.resolvePendingRequests();
const fragmentStyle = this.getFragmentStyle(imageProvider.scope);
index$1.assert(fragmentStyle, "Fragment style not found for image provider");
if (!fragmentStyle) continue;
fragmentStyle.addImages(images);
}
}
/*
* Apply any queued image changes.
*/
_updateTilesForChangedImages() {
const updatedImages = {};
for (const name in this._mergedSourceCaches) {
const sourceCache = this._mergedSourceCaches[name];
const scope = sourceCache.getSource().scope;
updatedImages[scope] = updatedImages[scope] || this._changes.getUpdatedImages(scope);
if (updatedImages[scope].length === 0) continue;
this._mergedSourceCaches[name].reloadTilesForDependencies(["icons", "patterns"], updatedImages[scope]);
}
for (const scope in updatedImages) {
this._changes.resetUpdatedImages(scope);
}
}
_updateWorkerLayers(scope, updatedIds, removedIds) {
const fragmentStyle = this.getFragmentStyle(scope);
if (!fragmentStyle) return;
this.dispatcher.broadcast("updateLayers", {
layers: updatedIds ? fragmentStyle._serializeLayers(updatedIds) : [],
scope,
removedIds: removedIds || [],
options: fragmentStyle.options
});
}
/**
* Update this style's state to match the given style JSON, performing only
* the necessary mutations.
*
* May throw an Error ('Unimplemented: METHOD') if the mapbox-gl-style-spec
* diff algorithm produces an operation that is not supported.
*
* @returns {boolean} true if any changes were made; false otherwise
* @private
*/
setState(nextState, onFinish) {
this._checkLoaded();
if (emitValidationErrors(this, validateStyle(nextState))) return false;
nextState = index$1.clone$1(nextState);
nextState.layers = derefLayers(nextState.layers);
const changes = diffStyles(this.serialize(), nextState).filter((op) => !(op.command in ignoredDiffOperations));
if (changes.length === 0) {
return false;
}
const unimplementedOps = changes.filter((op) => !(op.command in supportedDiffOperations));
if (unimplementedOps.length > 0) {
throw new Error(`Unimplemented: ${unimplementedOps.map((op) => op.command).join(", ")}.`);
}
const changesPromises = [];
changes.forEach((op) => {
changesPromises.push(this[op.command](...op.args));
});
if (onFinish) {
Promise.all(changesPromises).then(onFinish).catch(onFinish);
}
this.stylesheet = nextState;
this.mergeAll();
this.dispatcher.broadcast("setLayers", {
layers: this._serializeLayers(this._order),
scope: this.scope,
options: this.options
});
return true;
}
/**
* Broadcast the current set of available images to the Workers.
* Note that this is a scoped method, so it will only update the images for the given scope.
*/
_updateWorkerImages() {
this._availableImages = this.imageManager.listImages(this.scope);
this.dispatcher.broadcast("setImages", { scope: this.scope, images: this._availableImages });
}
_updateWorkerModels() {
this._availableModels = this.modelManager.getModelURIs(this.scope);
const params = { scope: this.scope, models: this._availableModels };
this.dispatcher.broadcast("setModels", params);
}
/**
* Add a set of images to the style.
* @fires Map.event:data Fires `data` with `{dataType: 'style'}` to indicate that the set of available images has changed.
* @returns {Style}
*/
addImages(images) {
if (images.size === 0) {
return this;
}
for (const [id, image] of images.entries()) {
if (this.getImage(id)) {
return this.fire(new index$1.ErrorEvent(new Error(`An image with the name "${id.name}" already exists.`)));
}
this.imageManager.addImage(id, this.scope, image);
this._changes.updateImage(id, this.scope);
}
this._updateWorkerImages();
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
addImage(id, image) {
if (this.getImage(id)) {
return this.fire(new index$1.ErrorEvent(new Error(`An image with the name "${id.name}" already exists.`)));
}
this.imageManager.addImage(id, this.scope, image);
this._changes.updateImage(id, this.scope);
this._updateWorkerImages();
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
updateImage(id, image, performSymbolLayout = false) {
this.imageManager.updateImage(id, this.scope, image);
if (performSymbolLayout) {
this._changes.updateImage(id, this.scope);
this._updateWorkerImages();
this.fire(new index$1.Event("data", { dataType: "style" }));
}
}
getImage(id) {
return this.imageManager.getImage(id, this.scope);
}
removeImage(id) {
if (!this.getImage(id)) {
return this.fire(new index$1.ErrorEvent(new Error("No image with this name exists.")));
}
this.imageManager.removeImage(id, this.scope);
this._changes.updateImage(id, this.scope);
this._updateWorkerImages();
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
listImages() {
this._checkLoaded();
return this._availableImages.slice();
}
getActualScope() {
return this._importedAsBasemap ? "basemap" : this.scope;
}
addModelURLs(models) {
this.modelManager.addModelURLs(models, this.getActualScope());
this._updateWorkerModels();
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
addModel(id, url, options = {}) {
this._checkLoaded();
if (this._validate(validateModel, `models.${id}`, url, null, options)) return this;
this.modelManager.addModel(id, url, this.getActualScope());
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
hasModel(id) {
return this.modelManager.hasModel(id, this.getActualScope());
}
removeModel(id) {
if (!this.hasModel(id)) {
return this.fire(new index$1.ErrorEvent(new Error("No model with this ID exists.")));
}
const keepModelURI = false;
const forceRemoval = true;
this.modelManager.removeModel(id, this.getActualScope(), keepModelURI, forceRemoval);
this.fire(new index$1.Event("data", { dataType: "style" }));
return this;
}
listModels() {
this._checkLoaded();
return this.modelManager.listModels(this.getActualScope());
}
addSource(id, source, options = {}) {
this._checkLoaded();
if (this.getOwnSource(id) !== void 0) {
throw new Error(`There is already a source with ID "${id}".`);
}
if (!source.type) {
throw new Error(`The type property must be defined, but only the following properties were given: ${Object.keys(source).join(", ")}.`);
}
const builtIns = ["vector", "raster", "geojson", "video", "image"];
const shouldValidate = builtIns.indexOf(source.type) >= 0;
if (shouldValidate && this._validate(validateSource, `sources.${id}`, source, null, options)) return;
if (this.map && this.map._collectResourceTiming) source.collectResourceTiming = true;
const sourceInstance = create(id, source, this.dispatcher, this);
sourceInstance.scope = this.scope;
sourceInstance.setEventedParent(this, () => ({
isSourceLoaded: this._isSourceCacheLoaded(sourceInstance.id),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
source: sourceInstance.serialize(),
sourceId: sourceInstance.id
}));
const addSourceCache = (onlySymbols) => {
const sourceCacheId = (onlySymbols ? "symbol:" : "other:") + sourceInstance.id;
const sourceCacheFQID = index$1.makeFQID(sourceCacheId, this.scope);
const sourceCache = this._sourceCaches[sourceCacheId] = new SourceCache(sourceCacheFQID, sourceInstance, onlySymbols);
(onlySymbols ? this._symbolSourceCaches : this._otherSourceCaches)[sourceInstance.id] = sourceCache;
sourceCache.onAdd(this.map);
};
addSourceCache(false);
if (source.type === "vector" || source.type === "geojson") {
addSourceCache(true);
}
if (sourceInstance.onAdd)
sourceInstance.onAdd(this.map);
if (!options.isInitialLoad) {
this.mergeSources();
this._changes.setDirty();
}
}
/**
* Remove a source from this stylesheet, given its ID.
* @param {string} id ID of the source to remove.
* @throws {Error} If no source is found with the given ID.
* @returns {Map} The {@link Map} object.
*/
removeSource(id) {
this._checkLoaded();
const source = this.getOwnSource(id);
if (!source) {
throw new Error("There is no source with this ID");
}
for (const layerId in this._layers) {
if (this._layers[layerId].source === id) {
return this.fire(new index$1.ErrorEvent(new Error(`Source "${id}" cannot be removed while layer "${layerId}" is using it.`)));
}
}
if (this.terrain && this.terrain.scope === this.scope && this.terrain.get().source === id) {
return this.fire(new index$1.ErrorEvent(new Error(`Source "${id}" cannot be removed while terrain is using it.`)));
}
if (this.stylesheet.iconsets) {
const iconset = Object.entries(this.stylesheet.iconsets).find(([_, iconset2]) => iconset2.type === "source" ? iconset2.source === id : false);
if (iconset) return this.fire(new index$1.ErrorEvent(new Error(`Source "${id}" cannot be removed while iconset "${iconset[0]}" is using it.`)));
}
const sourceCaches = this.getOwnSourceCaches(id);
for (const sourceCache of sourceCaches) {
const id2 = index$1.getNameFromFQID(sourceCache.id);
delete this._sourceCaches[id2];
this._changes.discardSourceCacheUpdate(sourceCache.id);
sourceCache.fire(new index$1.Event("data", { sourceDataType: "metadata", dataType: "source", sourceId: sourceCache.getSource().id }));
sourceCache.setEventedParent(null);
sourceCache.clearTiles();
}
delete this._otherSourceCaches[id];
delete this._symbolSourceCaches[id];
this.mergeSources();
source.setEventedParent(null);
if (source.onRemove)
source.onRemove(this.map);
this._changes.setDirty();
return this;
}
/**
* Set the data of a GeoJSON source, given its ID.
* @param {string} id ID of the source.
* @param {GeoJSON|string} data GeoJSON source.
*/
setGeoJSONSourceData(id, data) {
this._checkLoaded();
index$1.assert(this.getOwnSource(id) !== void 0, "There is no source with this ID");
const geojsonSource = this.getOwnSource(id);
index$1.assert(geojsonSource.type === "geojson");
geojsonSource.setData(data);
this._changes.setDirty();
}
/**
* Get a source by ID.
* @param {string} id ID of the desired source.
* @returns {?Source} The source object.
*/
getOwnSource(id) {
const sourceCache = this.getOwnSourceCache(id);
return sourceCache && sourceCache.getSource();
}
getOwnSources() {
const sources = [];
for (const id in this._otherSourceCaches) {
const sourceCache = this.getOwnSourceCache(id);
if (sourceCache) sources.push(sourceCache.getSource());
}
return sources;
}
areTilesLoaded() {
const sources = this._mergedSourceCaches;
for (const id in sources) {
const source = sources[id];
const tiles = source._tiles;
for (const t in tiles) {
const tile = tiles[t];
if (!(tile.state === "loaded" || tile.state === "errored")) return false;
}
}
return true;
}
setLights(lights) {
this._checkLoaded();
if (!lights) {
delete this.ambientLight;
delete this.directionalLight;
return;
}
const transitionParameters = this._getTransitionParameters();
for (const light of lights) {
if (this._validate(validateLights, "lights", light)) {
return;
}
switch (light.type) {
case "ambient":
if (this.ambientLight) {
const ambientLight = this.ambientLight;
ambientLight.set(light);
ambientLight.updateTransitions(transitionParameters);
} else {
this.ambientLight = new Lights(light, getProperties$1(), this.scope, this.options);
}
break;
case "directional":
if (this.directionalLight) {
const directionalLight = this.directionalLight;
directionalLight.set(light);
directionalLight.updateTransitions(transitionParameters);
} else {
this.directionalLight = new Lights(light, getProperties(), this.scope, this.options);
}
break;
default:
index$1.assert(false, `Unknown light type: ${light.type}`);
}
}
const evaluationParametersOptions = Object.assign(transitionParameters, { worldview: this.map.getWorldview() });
const evaluationParameters = new index$1.EvaluationParameters(this.z || 0, evaluationParametersOptions);
if (this.ambientLight) {
this.ambientLight.recalculate(evaluationParameters);
}
if (this.directionalLight) {
this.directionalLight.recalculate(evaluationParameters);
}
this._brightness = this.calculateLightsBrightness();
this.dispatcher.broadcast("setBrightness", this._brightness);
}
calculateLightsBrightness() {
const directional = this.directionalLight;
const ambient = this.ambientLight;
if (!directional || !ambient) {
return;
}
const relativeLuminance = (color) => {
const r = color[0] <= 0.03928 ? color[0] / 12.92 : Math.pow((color[0] + 0.055) / 1.055, 2.4);
const g = color[1] <= 0.03928 ? color[1] / 12.92 : Math.pow((color[1] + 0.055) / 1.055, 2.4);
const b = color[2] <= 0.03928 ? color[2] / 12.92 : Math.pow((color[2] + 0.055) / 1.055, 2.4);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
const directionalColor = directional.properties.get("color").toNonPremultipliedRenderColor(null).toArray01();
const directionalIntensity = directional.properties.get("intensity");
const direction = directional.properties.get("direction");
const sphericalDirection = index$1.cartesianPositionToSpherical(direction.x, direction.y, direction.z);
const polarIntensity = 1 - sphericalDirection[2] / 90;
const directionalBrightness = relativeLuminance(directionalColor) * directionalIntensity * polarIntensity;
const ambientColor = ambient.properties.get("color").toNonPremultipliedRenderColor(null).toArray01();
const ambientIntensity = ambient.properties.get("intensity");
const ambientBrightness = relativeLuminance(ambientColor) * ambientIntensity;
const brightness = (directionalBrightness + ambientBrightness) / 2;
return Number(brightness.toFixed(6));
}
getBrightness() {
return this._brightness;
}
getLights() {
if (!this.enable3dLights()) return null;
const lights = [];
if (this.directionalLight) {
lights.push(this.directionalLight.get());
}
if (this.ambientLight) {
lights.push(this.ambientLight.get());
}
return lights;
}
enable3dLights() {
return !!this.ambientLight && !!this.directionalLight;
}
/**
* Returns nested fragment style associated with the provided fragmentId.
* If no fragmentId is provided, returns itself.
*/
getFragmentStyle(fragmentId) {
if (fragmentId == null || fragmentId === "" && this.isRootStyle()) return this;
if (index$1.isFQID(fragmentId)) {
const scope = index$1.getInnerScopeFromFQID(fragmentId);
const fragment = this.fragments.find(({ id }) => id === scope);
index$1.assert(fragment, `Fragment with id ${scope} not found in the style.`);
if (!fragment) return void 0;
const name = index$1.getNameFromFQID(fragmentId);
return fragment.style.getFragmentStyle(name);
} else {
const fragment = this.fragments.find(({ id }) => id === fragmentId);
return fragment ? fragment.style : void 0;
}
}
setFeaturesetSelectors(featuresets) {
if (!featuresets) return;
const sourceInfoMap = {};
const createKey = (sourceId, sourcelayerId = "") => `${sourceId}::${sourcelayerId}`;
this._featuresetSelectors = {};
for (const featuresetId in featuresets) {
const featuresetSelectors = this._featuresetSelectors[featuresetId] = [];
for (const selector of featuresets[featuresetId].selectors) {
if (selector.featureNamespace) {
const layer = this.getOwnLayer(selector.layer);
if (!layer) {
index$1.warnOnce(`Layer is undefined for selector: ${selector.layer}`);
continue;
}
const sourceKey = createKey(layer.source, layer.sourceLayer);
if (sourceKey in sourceInfoMap && sourceInfoMap[sourceKey] !== selector.featureNamespace) {
index$1.warnOnce(`"featureNamespace ${selector.featureNamespace} of featureset ${featuresetId}'s selector is not associated to the same source, skip this selector`);
continue;
}
sourceInfoMap[sourceKey] = selector.featureNamespace;
}
let properties;
if (selector.properties) {
for (const name in selector.properties) {
const expression = index$1.createExpression(selector.properties[name]);
if (expression.result === "success") {
properties = properties || {};
properties[name] = expression.value;
}
}
}
featuresetSelectors.push({ layerId: selector.layer, namespace: selector.featureNamespace, properties, uniqueFeatureID: selector._uniqueFeatureID });
}
}
}
/**
* Returns the featureset descriptors associated with a style fragment.
* If no fragmentId is provided, returns own featureset descriptors.
*/
getFeaturesetDescriptors(fragmentId) {
const style = this.getFragmentStyle(fragmentId);
if (!style || !style.stylesheet.featuresets) return [];
const featuresetDescriptors = [];
for (const id in style.stylesheet.featuresets) {
featuresetDescriptors.push({ featuresetId: id, importId: style.scope ? style.scope : void 0 });
}
return featuresetDescriptors;
}
/**
* Returns the layers associated with a featureset in the style fragment.
* If no fragmentId is provided, returns the layers associated with own featuresets.
*/
getFeaturesetLayers(featuresetId, fragmentId) {
const style = this.getFragmentStyle(fragmentId);
const featuresets = style.stylesheet.featuresets;
if (!featuresets || !featuresets[featuresetId]) {
this.fire(new index$1.ErrorEvent(new Error(`The featureset '${featuresetId}' does not exist in the map's style and cannot be queried.`)));
return [];
}
const layers = [];
for (const selector of featuresets[featuresetId].selectors) {
const layer = style.getOwnLayer(selector.layer);
if (layer) layers.push(layer);
}
return layers;
}
getConfigProperty(fragmentId, key) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return null;
const fqid = index$1.makeFQID(key, fragmentStyle.scope);
const expressions = fragmentStyle.options.get(fqid);
const expression = expressions ? expressions.value || expressions.default : null;
return expression ? expression.serialize() : null;
}
isIndoorEnabled() {
return Object.keys(this._mergedIndoor).length > 0;
}
getIndoorSourceLayers(sourceId, scope) {
const fqid = index$1.makeFQID(sourceId, scope);
return this._mergedIndoor[fqid];
}
setIndoorData(mapId, params) {
this.map.indoor.setIndoorData(params);
}
updateIndoorDependentLayers() {
this._updateLayers(this._indoorDependentLayers);
this.map._styleDirty = true;
this.map.triggerRepaint();
}
setConfigProperty(fragmentId, key, value) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return;
const schema = fragmentStyle.stylesheet.schema;
if (!schema || !schema[key]) return;
const expressionParsed = index$1.createExpression(value);
if (expressionParsed.result !== "success") {
emitValidationErrors(this, expressionParsed.value);
return;
}
const expression = expressionParsed.value.expression;
const fqid = index$1.makeFQID(key, fragmentStyle.scope);
const expressions = fragmentStyle.options.get(fqid);
if (!expressions) return;
let defaultExpression;
const { minValue, maxValue, stepValue, type, values } = schema[key];
const defaultExpressionParsed = index$1.createExpression(schema[key].default);
if (defaultExpressionParsed.result === "success") {
defaultExpression = defaultExpressionParsed.value.expression;
}
if (!defaultExpression) {
this.fire(new index$1.ErrorEvent(new Error(`No schema defined for the config option "${key}" in the "${fragmentId}" fragment.`)));
return;
}
this.options.set(fqid, Object.assign({}, expressions, {
value: expression,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
default: defaultExpression,
minValue,
maxValue,
stepValue,
type,
values
}));
this.updateConfigDependencies(key);
}
getConfig(fragmentId) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return null;
const schema = fragmentStyle.stylesheet.schema;
if (!schema) return null;
const config = {};
for (const key in schema) {
const fqid = index$1.makeFQID(key, fragmentStyle.scope);
const expressions = fragmentStyle.options.get(fqid);
const expression = expressions ? expressions.value || expressions.default : null;
config[key] = expression ? expression.serialize() : null;
}
return config;
}
setConfig(fragmentId, config) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return;
const schema = fragmentStyle.stylesheet.schema;
fragmentStyle.updateConfig(config, schema);
this.updateConfigDependencies();
}
getSchema(fragmentId) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return null;
return fragmentStyle.stylesheet.schema;
}
setSchema(fragmentId, schema) {
const fragmentStyle = this.getFragmentStyle(fragmentId);
if (!fragmentStyle) return;
fragmentStyle.stylesheet.schema = schema;
fragmentStyle.updateConfig(fragmentStyle._config, schema);
this.updateConfigDependencies();
}
updateConfig(config, schema) {
this._config = config;
if (!config && !schema) return;
if (!schema) {
this.fire(new index$1.ErrorEvent(new Error(`Attempting to set config for a style without schema.`)));
return;
}
for (const id in schema) {
let defaultExpression;
let configExpression;
const expression = schema[id].default;
const expressionParsed = index$1.createExpression(expression);
if (expressionParsed.result === "success") {
defaultExpression = expressionParsed.value.expression;
}
if (config && config[id] !== void 0) {
const expressionParsed2 = index$1.createExpression(config[id]);
if (expressionParsed2.result === "success") {
configExpression = expressionParsed2.value.expression;
}
}
const { minValue, maxValue, stepValue, type, values } = schema[id];
if (defaultExpression) {
const fqid = index$1.makeFQID(id, this.scope);
this.options.set(fqid, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
default: defaultExpression,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
value: configExpression,
minValue,
maxValue,
stepValue,
type,
values
});
} else {
this.fire(new index$1.ErrorEvent(new Error(`No schema defined for config option "${id}".`)));
}
}
}
_updateLayers(layerIds, condition = () => true) {
for (const id of layerIds) {
const layer = this.getLayer(id);
if (layer && condition(layer)) {
layer.possiblyEvaluateVisibility();
this._updateLayer(layer);
this._changes.setDirty();
}
}
}
updateConfigDependencies(configKey) {
this._updateLayers(this._configDependentLayers, (layer) => {
return configKey ? layer.expressionDependencies.configDependencies.has(configKey) : true;
});
if (this.ambientLight) {
this.ambientLight.updateConfig(this.options);
}
if (this.directionalLight) {
this.directionalLight.updateConfig(this.options);
}
if (this.fog) {
this.fog.updateConfig(this.options);
}
if (this.snow) {
this.snow.updateConfig(this.options);
}
if (this.rain) {
this.rain.updateConfig(this.options);
}
this.forEachFragmentStyle((style) => {
const colorTheme = style._styleColorTheme.colorThemeOverride ? style._styleColorTheme.colorThemeOverride : style._styleColorTheme.colorTheme;
if (colorTheme) {
const data = style._evaluateColorThemeData(colorTheme);
if (!style._styleColorTheme.lut && data !== "" || style._styleColorTheme.lut && data !== style._styleColorTheme.lut.data) {
style.setColorTheme(colorTheme);
}
}
});
this._changes.setDirty();
}
/**
* Add a layer to the map style. The layer will be inserted before the layer with
* ID `before`, or appended if `before` is omitted.
* @param {Object | CustomLayerInterface} layerObject The style layer to add.
* @param {string} [before] ID of an existing layer to insert before.
* @param {Object} options Style setter options.
* @returns {Map} The {@link Map} object.
*/
addLayer(layerObject, before, options = {}) {
this._checkLoaded();
const id = layerObject.id;
if (this._layers[id]) {
this.fire(new index$1.ErrorEvent(new Error(`Layer with id "${id}" already exists on this map`)));
return;
}
let layer;
if (layerObject.type === "custom") {
if (emitValidationErrors(this, index$1.validateCustomStyleLayer(layerObject))) return;
layer = index$1.createStyleLayer(layerObject, this.scope, this._styleColorTheme.lut, this.options);
} else {
if (typeof layerObject.source === "object") {
this.addSource(id, layerObject.source);
layerObject = index$1.clone$1(layerObject);
layerObject = Object.assign(layerObject, { source: id });
}
if (this._validate(
validateLayer,
`layers.${id}`,
layerObject,
{ arrayIndex: -1 },
options
)) return;
layer = index$1.createStyleLayer(layerObject, this.scope, this._styleColorTheme.lut, this.options);
this._validateLayer(layer);
layer.setEventedParent(this, { layer: { id } });
}
const fqid = index$1.makeFQID(layer.source, layer.scope);
if (layer.expressionDependencies.configDependencies.size !== 0) this._configDependentLayers.add(fqid);
if (layer.expressionDependencies.isIndoorDependent) this._indoorDependentLayers.add(fqid);
let index = this._order.length;
if (before) {
const beforeIndex = this._order.indexOf(before);
if (beforeIndex === -1) {
this.fire(new index$1.ErrorEvent(new Error(`Layer with id "${before}" does not exist on this map.`)));
return;
}
const beforeLayer = this._layers[before];
if (layer.slot === beforeLayer.slot) index = beforeIndex;
else index$1.warnOnce(`Layer with id "${before}" has a different slot. Layers can only be rearranged within the same slot.`);
}
this._order.splice(index, 0, id);
this._layerOrderChanged = true;
this._layers[id] = layer;
const sourceCache = this.getOwnLayerSourceCache(layer);
const shadowsEnabled = !!this.directionalLight && this.directionalLight.shadowsEnabled();
if (sourceCache && layer.canCastShadows() && shadowsEnabled) {
sourceCache.castsShadows = true;
}
const removedLayer = this._changes.getRemovedLayer(layer);
if (removedLayer && layer.source && sourceCache && layer.type !== "custom") {
this._changes.discardLayerRemoval(layer);
const fqid2 = index$1.makeFQID(layer.source, layer.scope);
if (removedLayer.type !== layer.type) {
this._changes.updateSourceCache(fqid2, "clear");
} else {
this._changes.updateSourceCache(fqid2, "reload");
sourceCache.pause();
}
}
this._updateLayer(layer);
if (layer.onAdd) {
layer.onAdd(this.map);
}
layer.scope = this.scope;
this.mergeLayers();
}
/**
* Moves a layer to a different z-position. The layer will be inserted before the layer with
* ID `before`, or appended if `before` is omitted.
* @param {string} id ID of the layer to move.
* @param {string} [before] ID of an existing layer to insert before.
*/
moveLayer(id, before) {
this._checkLoaded();
const layer = this._checkLayer(id);
if (!layer) return;
if (id === before) {
return;
}
const index = this._order.indexOf(id);
this._order.splice(index, 1);
let newIndex = this._order.length;
if (before) {
const beforeIndex = this._order.indexOf(before);
if (beforeIndex === -1) {
this.fire(new index$1.ErrorEvent(new Error(`Layer with id "${before}" does not exist on this map.`)));
return;
}
const beforeLayer = this._layers[before];
if (layer.slot === beforeLayer.slot) newIndex = beforeIndex;
else index$1.warnOnce(`Layer with id "${before}" has a different slot. Layers can only be rearranged within the same slot.`);
}
this._order.splice(newIndex, 0, id);
this._changes.setDirty();
this._layerOrderChanged = true;
this.mergeLayers();
}
/**
* Remove the layer with the given id from the style.
*
* If no such layer exists, an `error` event is fired.
*
* @param {string} id ID of the layer to remove.
* @fires Map.event:error
*/
removeLayer(id) {
this._checkLoaded();
const layer = this._checkLayer(id);
if (!layer) return;
layer.setEventedParent(null);
const index = this._order.indexOf(id);
this._order.splice(index, 1);
delete this._layers[id];
this._changes.setDirty();
this._layerOrderChanged = true;
this._configDependentLayers.delete(layer.fqid);
this._indoorDependentLayers.delete(layer.fqid);
this._changes.removeLayer(layer);
const sourceCache = this.getOwnLayerSourceCache(layer);
if (sourceCache && sourceCache.castsShadows) {
let shadowCastersLeft = false;
for (const key in this._layers) {
if (this._layers[key].source === layer.source && this._layers[key].canCastShadows()) {
shadowCastersLeft = true;
break;
}
}
sourceCache.castsShadows = shadowCastersLeft;
}
if (layer.onRemove) {
layer.onRemove(this.map);
}
this.mergeLayers();
}
/**
* Return the style layer object with the given `id`.
*
* @param {string} id ID of the desired layer.
* @returns {TypedStyleLayer} A layer, if one with the given `id` exists.
*/
getOwnLayer(id) {
return this._layers[id];
}
/**
* Checks if a specific layer is present within the style.
*
* @param {string} id ID of the desired layer.
* @returns {boolean} A boolean specifying if the given layer is present.
*/
hasLayer(id) {
return id in this._mergedLayers;
}
/**
* Checks if a specific layer type is present within the style.
*
* @param {string} type Type of the desired layer.
* @returns {boolean} A boolean specifying if the given layer type is present.
*/
hasLayerType(type) {
for (const layerId in this._layers) {
const layer = this._layers[layerId];
if (layer.type === type) {
return true;
}
}
return false;
}
setLayerZoomRange(layerId, minzoom, maxzoom) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (layer.minzoom === minzoom && layer.maxzoom === maxzoom) return;
if (minzoom != null) {
layer.minzoom = minzoom;
}
if (maxzoom != null) {
layer.maxzoom = maxzoom;
}
this._updateLayer(layer);
}
getSlots() {
this._checkLoaded();
return this._mergedSlots;
}
setSlot(layerId, slot) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (layer.slot === slot) {
return;
}
layer.slot = slot;
this._updateLayer(layer);
}
setFilter(layerId, filter, options = {}) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (index$1.deepEqual(layer.filter, filter)) {
return;
}
if (filter === null || filter === void 0) {
layer.filter = void 0;
this._updateLayer(layer);
return;
}
if (this._validate(validateFilter, `layers.${layer.id}.filter`, filter, { layerType: layer.type }, options)) {
return;
}
layer.filter = index$1.clone$1(filter);
this._updateLayer(layer);
}
/**
* Get a layer's filter object.
* @param {string} layerId The layer to inspect.
* @returns {*} The layer's filter, if any.
*/
getFilter(layerId) {
const layer = this._checkLayer(layerId);
if (!layer) return;
return index$1.clone$1(layer.filter);
}
setLayoutProperty(layerId, name, value, options = {}) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (index$1.deepEqual(layer.getLayoutProperty(name), value)) return;
if (value !== null && value !== void 0 && !(options && options.validate === false)) {
const key = `layers.${layerId}.layout.${name}`;
const errors = emitValidationErrors(layer, validateLayoutProperty.call(validateStyle, {
key,
layerType: layer.type,
objectKey: name,
value,
styleSpec: index$1.spec,
// Workaround for https://github.com/mapbox/mapbox-gl-js/issues/2407
style: { glyphs: true, sprite: true }
}));
if (errors) {
return;
}
}
layer.setLayoutProperty(name, value);
if (layer.expressionDependencies.configDependencies.size !== 0) this._configDependentLayers.add(layer.fqid);
if (layer.expressionDependencies.isIndoorDependent) this._indoorDependentLayers.add(layer.fqid);
this._updateLayer(layer);
}
setLayerProperty(layerId, name, value, options = {}) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (name === "appearances") {
layer.setAppearances(value);
this._changes.setDirty();
} else if (layer.isPaintProperty(name)) {
this.setPaintProperty(layerId, name, value, options);
} else {
this.setLayoutProperty(layerId, name, value, options);
}
}
/**
* Get a layout property's value from a given layer.
* @param {string} layerId The layer to inspect.
* @param {string} name The name of the layout property.
* @returns {*} The property value.
*/
getLayoutProperty(layerId, name) {
const layer = this._checkLayer(layerId);
if (!layer) return;
return layer.getLayoutProperty(name);
}
setPaintProperty(layerId, name, value, options = {}) {
this._checkLoaded();
const layer = this._checkLayer(layerId);
if (!layer) return;
if (index$1.deepEqual(layer.getPaintProperty(name), value)) return;
if (value !== null && value !== void 0 && !(options && options.validate === false)) {
const key = `layers.${layerId}.paint.${name}`;
const errors = emitValidationErrors(layer, validatePaintProperty.call(validateStyle, {
key,
layerType: layer.type,
objectKey: name,
value,
styleSpec: index$1.spec
}));
if (errors) {
return;
}
}
const requiresRelayout = layer.setPaintProperty(name, value);
if (layer.expressionDependencies.configDependencies.size !== 0) this._configDependentLayers.add(layer.fqid);
if (layer.expressionDependencies.isIndoorDependent) this._indoorDependentLayers.add(layer.fqid);
if (requiresRelayout) {
this._updateLayer(layer);
}
this._changes.updatePaintProperties(layer);
}
getPaintProperty(layerId, name) {
const layer = this._checkLayer(layerId);
if (!layer) return;
return layer.getPaintProperty(name);
}
setFeatureState(target, state) {
this._checkLoaded();
if ("target" in target) {
if ("featuresetId" in target.target) {
const { featuresetId, importId } = target.target;
const fragment = this.getFragmentStyle(importId);
const layers = fragment.getFeaturesetLayers(featuresetId);
for (const { source: source2, sourceLayer: sourceLayer2 } of layers) {
fragment.setFeatureState({ id: target.id, source: source2, sourceLayer: sourceLayer2 }, state);
}
} else if ("layerId" in target.target) {
const { layerId } = target.target;
const layer = this.getLayer(layerId);
this.setFeatureState({ id: target.id, source: layer.source, sourceLayer: layer.sourceLayer }, state);
}
return;
}
const sourceId = target.source;
const sourceLayer = target.sourceLayer;
const source = this._checkSource(sourceId);
if (!source) return;
const sourceType = source.type;
if (sourceType === "geojson" && sourceLayer) {
this.fire(new index$1.ErrorEvent(new Error(`GeoJSON sources cannot have a sourceLayer parameter.`)));
return;
}
if (sourceType === "vector" && !sourceLayer) {
this.fire(new index$1.ErrorEvent(new Error(`The sourceLayer parameter must be provided for vector source types.`)));
return;
}
if (target.id === void 0) {
this.fire(new index$1.ErrorEvent(new Error(`The feature id parameter must be provided.`)));
}
const sourceCaches = this.getOwnSourceCaches(sourceId);
for (const sourceCache of sourceCaches) {
sourceCache.setFeatureState(sourceLayer, target.id, state);
}
}
removeFeatureState(target, key) {
this._checkLoaded();
if ("target" in target) {
if ("featuresetId" in target.target) {
const { featuresetId, importId } = target.target;
const fragment = this.getFragmentStyle(importId);
const layers = fragment.getFeaturesetLayers(featuresetId);
for (const { source: source2, sourceLayer: sourceLayer2 } of layers) {
fragment.removeFeatureState({ id: target.id, source: source2, sourceLayer: sourceLayer2 }, key);
}
} else if ("layerId" in target.target) {
const { layerId } = target.target;
const layer = this.getLayer(layerId);
this.removeFeatureState({ id: target.id, source: layer.source, sourceLayer: layer.sourceLayer }, key);
}
return;
}
const sourceId = target.source;
const source = this._checkSource(sourceId);
if (!source) return;
const sourceType = source.type;
const sourceLayer = sourceType === "vector" ? target.sourceLayer : void 0;
if (sourceType === "vector" && !sourceLayer) {
this.fire(new index$1.ErrorEvent(new Error(`The sourceLayer parameter must be provided for vector source types.`)));
return;
}
if (key && (typeof target.id !== "string" && typeof target.id !== "number")) {
this.fire(new index$1.ErrorEvent(new Error(`A feature id is required to remove its specific state property.`)));
return;
}
const sourceCaches = this.getOwnSourceCaches(sourceId);
for (const sourceCache of sourceCaches) {
sourceCache.removeFeatureState(sourceLayer, target.id, key);
}
}
getFeatureState(target) {
this._checkLoaded();
if ("target" in target) {
let finalState;
if ("featuresetId" in target.target) {
const { featuresetId, importId } = target.target;
const fragment = this.getFragmentStyle(importId);
const layers = fragment.getFeaturesetLayers(featuresetId);
for (const { source: source2, sourceLayer: sourceLayer2 } of layers) {
const state = fragment.getFeatureState({ id: target.id, source: source2, sourceLayer: sourceLayer2 });
if (state && !finalState) {
finalState = state;
} else if (!index$1.deepEqual(finalState, state)) {
this.fire(new index$1.ErrorEvent(new Error(`The same feature id exists in multiple sources in the featureset, but their feature states are not consistent through the sources.`)));
return;
}
}
} else if ("layerId" in target.target) {
const { layerId } = target.target;
const layer = this.getLayer(layerId);
finalState = this.getFeatureState({ id: target.id, source: layer.source, sourceLayer: layer.sourceLayer });
}
return finalState;
}
const sourceId = target.source;
const sourceLayer = target.sourceLayer;
const source = this._checkSource(sourceId);
if (!source) return;
const sourceType = source.type;
if (sourceType === "vector" && !sourceLayer) {
this.fire(new index$1.ErrorEvent(new Error(`The sourceLayer parameter must be provided for vector source types.`)));
return;
}
if (target.id === void 0) {
this.fire(new index$1.ErrorEvent(new Error(`The feature id parameter must be provided.`)));
}
const sourceCaches = this.getOwnSourceCaches(sourceId);
return sourceCaches[0].getFeatureState(sourceLayer, target.id);
}
setTransition(transition) {
this.stylesheet.transition = Object.assign({}, this.stylesheet.transition, transition);
this.transition = this.stylesheet.transition;
return this;
}
getTransition() {
return Object.assign({}, this.stylesheet.transition);
}
serialize() {
this._checkLoaded();
const terrain = this.getTerrain();
const scopedTerrain = terrain && this.terrain && this.terrain.scope === this.scope ? terrain : this.stylesheet.terrain;
return index$1.filterObject({
version: this.stylesheet.version,
name: this.stylesheet.name,
metadata: this.stylesheet.metadata,
fragment: this.stylesheet.fragment,
iconsets: this.stylesheet.iconsets,
imports: this._serializeImports(),
schema: this.stylesheet.schema,
camera: this.stylesheet.camera,
light: this.stylesheet.light,
lights: this.stylesheet.lights,
terrain: scopedTerrain,
fog: this.stylesheet.fog,
snow: this.stylesheet.snow,
rain: this.stylesheet.rain,
center: this.stylesheet.center,
"color-theme": this.stylesheet["color-theme"],
zoom: this.stylesheet.zoom,
bearing: this.stylesheet.bearing,
pitch: this.stylesheet.pitch,
sprite: this.stylesheet.sprite,
glyphs: this.stylesheet.glyphs,
transition: this.stylesheet.transition,
projection: this.stylesheet.projection,
sources: this._serializeSources(),
layers: this._serializeLayers(this._order)
}, (value) => value !== void 0);
}
_updateFilteredLayers(filter) {
for (const layer of Object.values(this._mergedLayers)) {
if (filter(layer)) {
this._updateLayer(layer);
}
}
}
_updateLayer(layer) {
this._changes.updateLayer(layer);
const sourceCache = this.getLayerSourceCache(layer);
const fqid = index$1.makeFQID(layer.source, layer.scope);
const sourceCacheUpdates = this._changes.getUpdatedSourceCaches();
if (layer.source && !sourceCacheUpdates[fqid] && // Skip for raster layers (https://github.com/mapbox/mapbox-gl-js/issues/7865)
sourceCache && sourceCache.getSource().type !== "raster") {
this._changes.updateSourceCache(fqid, "reload");
sourceCache.pause();
}
layer.invalidateCompiledFilter();
}
_flattenAndSortRenderedFeatures(sourceResults) {
const isLayer3D = (layerId) => this._mergedLayers[layerId].is3D(!!this.terrain);
const order = this.order;
const layerIndex = {};
const features3D = [];
for (let l = order.length - 1; l >= 0; l--) {
const layerId = order[l];
if (isLayer3D(layerId)) {
layerIndex[layerId] = l;
for (const sourceResult of sourceResults) {
const layerFeatures = sourceResult[layerId];
if (layerFeatures) {
for (const featureWrapper of layerFeatures) {
features3D.push(featureWrapper);
}
}
}
}
}
features3D.sort((a, b) => {
return b.intersectionZ - a.intersectionZ;
});
const features = [];
for (let l = order.length - 1; l >= 0; l--) {
const layerId = order[l];
if (isLayer3D(layerId)) {
for (let i = features3D.length - 1; i >= 0; i--) {
const topmost3D = features3D[i].feature;
if (topmost3D.layer && layerIndex[topmost3D.layer.id] < l) break;
features.push(topmost3D);
features3D.pop();
}
} else {
for (const sourceResult of sourceResults) {
const layerFeatures = sourceResult[layerId];
if (layerFeatures) {
for (const featureWrapper of layerFeatures) {
features.push(featureWrapper.feature);
}
}
}
}
}
return features;
}
queryRasterValue(sourceId, lnglat, parameters) {
const source = this.getOwnSource(sourceId);
if (!source) {
this.fire(new index$1.ErrorEvent(new Error(`Source with id "${sourceId}" does not exist in the style.`)));
return Promise.resolve(null);
}
if (source.type !== "raster-array") {
this.fire(new index$1.ErrorEvent(new Error(`queryRasterValue support only "raster-array" sources.`)));
return Promise.resolve(null);
}
return source.queryRasterArrayValue(lnglat, parameters);
}
queryRenderedFeatures(queryGeometry, params, transform) {
let filter;
if (params && !Array.isArray(params) && params.filter) {
this._validate(validateFilter, "queryRenderedFeatures.filter", params.filter, null, params);
filter = index$1.createFilter(params.filter);
}
const queries = {};
const addLayerToQuery = (styleLayer) => {
if (featurelessLayerTypes.has(styleLayer.type)) return;
const sourceCache = this.getOwnLayerSourceCache(styleLayer);
index$1.assert(sourceCache, "queryable layers must have a source");
const querySourceCache = queries[sourceCache.id] = queries[sourceCache.id] || { sourceCache, layers: {}, has3DLayers: false };
if (styleLayer.is3D(!!this.terrain)) querySourceCache.has3DLayers = true;
querySourceCache.layers[styleLayer.fqid] = querySourceCache.layers[styleLayer.fqid] || { styleLayer, targets: [] };
querySourceCache.layers[styleLayer.fqid].targets.push({ filter });
};
if (params && params.layers) {
if (!Array.isArray(params.layers)) {
this.fire(new index$1.ErrorEvent(new Error("parameters.layers must be an Array.")));
return [];
}
for (const layerId of params.layers) {
const styleLayer = this._layers[layerId];
if (!styleLayer) {
this.fire(new index$1.ErrorEvent(new Error(`The layer '${layerId}' does not exist in the map's style and cannot be queried for features.`)));
return [];
}
addLayerToQuery(styleLayer);
}
} else {
for (const layerId in this._layers) {
addLayerToQuery(this._layers[layerId]);
}
}
const renderedFeatures = this._queryRenderedFeatures(queryGeometry, queries, transform);
const sortedFeatures = this._flattenAndSortRenderedFeatures(renderedFeatures);
const features = [];
for (const feature of sortedFeatures) {
const scope = index$1.getOuterScopeFromFQID(feature.layer.id);
if (scope === this.scope) features.push(feature);
}
return features;
}
queryRenderedFeatureset(queryGeometry, params, transform) {
let filter;
if (params && !Array.isArray(params) && params.filter) {
this._validate(validateFilter, "queryRenderedFeatures.filter", params.filter, null, params);
filter = index$1.createFilter(params.filter);
}
const targetId = "mock";
const targets = [];
if (params && params.target) {
targets.push(Object.assign({}, params, { targetId, filter }));
} else {
const featuresetDescriptors = this.getFeaturesetDescriptors();
for (const featureset of featuresetDescriptors) {
targets.push({ targetId, filter, target: featureset });
}
for (const { style } of this.fragments) {
const featuresetDescriptors2 = style.getFeaturesetDescriptors();
for (const featureset of featuresetDescriptors2) {
targets.push({ targetId, filter, target: featureset });
}
}
}
const features = this.queryRenderedTargets(queryGeometry, targets, transform);
const targetFeatures = [];
const uniqueFeatureSet = /* @__PURE__ */ new Set();
for (const feature of features) {
for (const variant of feature.variants[targetId]) {
if (shouldSkipFeatureVariant(variant, feature, uniqueFeatureSet)) {
continue;
}
targetFeatures.push(new index$1.TargetFeature(feature, variant));
}
}
return targetFeatures;
}
queryRenderedTargets(queryGeometry, targets, transform) {
const queries = {};
const addLayerToQuery = (styleLayer, sourceCache, target, selector) => {
index$1.assert(sourceCache, "queryable layers must have a source");
const querySourceCache = queries[sourceCache.id] = queries[sourceCache.id] || { sourceCache, layers: {}, has3DLayers: false };
querySourceCache.layers[styleLayer.fqid] = querySourceCache.layers[styleLayer.fqid] || { styleLayer, targets: [] };
if (styleLayer.is3D(!!this.terrain)) querySourceCache.has3DLayers = true;
if (!selector) {
target.uniqueFeatureID = false;
querySourceCache.layers[styleLayer.fqid].targets.push(target);
return;
}
querySourceCache.layers[styleLayer.fqid].targets.push(Object.assign({}, target, {
namespace: selector.namespace,
properties: selector.properties,
uniqueFeatureID: selector.uniqueFeatureID
}));
};
for (const target of targets) {
if ("featuresetId" in target.target) {
const { featuresetId, importId } = target.target;
const style = this.getFragmentStyle(importId);
if (!style || !style._featuresetSelectors) continue;
const selectors = style._featuresetSelectors[featuresetId];
if (!selectors) {
this.fire(new index$1.ErrorEvent(new Error(`The featureset '${featuresetId}' does not exist in the map's style and cannot be queried for features.`)));
continue;
}
for (const selector of selectors) {
const styleLayer = style.getOwnLayer(selector.layerId);
if (!styleLayer || featurelessLayerTypes.has(styleLayer.type)) continue;
const sourceCache = style.getOwnLayerSourceCache(styleLayer);
addLayerToQuery(styleLayer, sourceCache, target, selector);
}
} else if ("layerId" in target.target) {
const { layerId } = target.target;
const styleLayer = this.getLayer(layerId);
if (!styleLayer || featurelessLayerTypes.has(styleLayer.type)) continue;
const sourceCache = this.getLayerSourceCache(styleLayer);
addLayerToQuery(styleLayer, sourceCache, target);
}
}
const renderedFeatures = this._queryRenderedFeatures(queryGeometry, queries, transform);
const sortedFeatures = this._flattenAndSortRenderedFeatures(renderedFeatures);
return sortedFeatures;
}
_queryRenderedFeatures(queryGeometry, queries, transform) {
const queryResults = [];
const showQueryGeometry = !!this.map._showQueryGeometry;
const queryGeometryStruct = QueryGeometry.createFromScreenPoints(queryGeometry, transform);
for (const sourceCacheId in queries) {
const queryResult = queryRenderedFeatures(
queryGeometryStruct,
queries[sourceCacheId],
this._availableImages,
transform,
showQueryGeometry
);
if (Object.keys(queryResult).length) queryResults.push(queryResult);
}
if (this.placement) {
for (const sourceCacheId in queries) {
if (!queries[sourceCacheId].sourceCache._onlySymbols) continue;
const queryResult = queryRenderedSymbols(
queryGeometryStruct.screenGeometry,
queries[sourceCacheId],
this._availableImages,
this.placement.collisionIndex,
this.placement.retainedQueryData,
this.map.getWorldview()
);
if (Object.keys(queryResult).length) queryResults.push(queryResult);
}
}
return queryResults;
}
querySourceFeatures(sourceId, params) {
const filter = params && params.filter;
if (filter) {
this._validate(validateFilter, "querySourceFeatures.filter", filter, null, params);
}
let results = [];
const sourceCaches = this.getOwnSourceCaches(sourceId);
for (const sourceCache of sourceCaches) {
results = results.concat(querySourceFeatures(sourceCache, params));
}
return results;
}
addSourceType(name, SourceType, callback) {
if (Style.getSourceType(name)) {
return callback(new Error(`A source type called "${name}" already exists.`));
}
Style.setSourceType(name, SourceType);
if (!SourceType.workerSourceURL) {
return callback(null, null);
}
this.dispatcher.broadcast("loadWorkerSource", {
name,
url: SourceType.workerSourceURL
}, callback);
}
getFlatLight() {
return this.light.getLight();
}
setFlatLight(lightOptions, id, options = {}) {
this._checkLoaded();
const light = this.light.getLight();
let _update = false;
for (const key in lightOptions) {
if (!index$1.deepEqual(lightOptions[key], light[key])) {
_update = true;
break;
}
}
if (!_update) return;
const parameters = this._getTransitionParameters();
this.light.setLight(lightOptions, id, options);
this.light.updateTransitions(parameters);
}
getTerrain() {
return this.terrain && this.terrain.drapeRenderMode === DrapeRenderMode.elevated ? this.terrain.get() : null;
}
setTerrainForDraping() {
const mockTerrainOptions = { source: "", exaggeration: 0 };
this.setTerrain(mockTerrainOptions, DrapeRenderMode.deferred);
}
checkCanvasFingerprintNoise() {
if (this.disableElevatedTerrain === void 0) {
this.disableElevatedTerrain = index$1.exported$1.hasCanvasFingerprintNoise();
if (this.disableElevatedTerrain) index$1.warnOnce("Terrain and hillshade are disabled because of Canvas2D limitations when fingerprinting protection is enabled (e.g. in private browsing mode).");
}
}
// eslint-disable-next-line no-warning-comments
// TODO: generic approach for root level property: light, terrain, skybox.
// It is not done here to prevent rebasing issues.
setTerrain(terrainOptions, drapeRenderMode = DrapeRenderMode.elevated) {
this._checkLoaded();
if (!terrainOptions) {
if (!this.terrainSetForDrapingOnly()) {
delete this.terrain;
if (this.map.transform.projection.requiresDraping) {
this.setTerrainForDraping();
}
}
if (drapeRenderMode === DrapeRenderMode.deferred) {
delete this.terrain;
}
if (terrainOptions === null) {
this.stylesheet.terrain = null;
} else {
delete this.stylesheet.terrain;
}
this._force3DLayerUpdate();
this._markersNeedUpdate = true;
return;
}
this.checkCanvasFingerprintNoise();
let options = terrainOptions;
const isUpdating = !("source" in terrainOptions) || terrainOptions.source == null;
if (drapeRenderMode === DrapeRenderMode.elevated) {
if (this.disableElevatedTerrain) return;
if ("source" in options && typeof options.source === "object") {
const id = "terrain-dem-src";
this.addSource(id, options.source);
options = index$1.clone$1(options);
options = Object.assign(options, { source: id });
}
const validationOptions = Object.assign({}, options);
const validationProps = {};
if (this.terrain && isUpdating) {
validationOptions.source = this.terrain.get().source;
const fragmentStyle = this.terrain ? this.getFragmentStyle(this.terrain.scope) : null;
if (fragmentStyle) {
validationProps.style = fragmentStyle.serialize();
}
}
if (this._validate(validateTerrain, "terrain", validationOptions, validationProps)) {
return;
}
}
if (!this.terrain || this.terrain.scope !== this.scope && !isUpdating || this.terrain && drapeRenderMode !== this.terrain.drapeRenderMode) {
if (!options) return;
this._createTerrain(options, drapeRenderMode);
this.fire(new index$1.Event("data", { dataType: "style" }));
} else {
const terrain = this.terrain;
const currSpec = terrain.get();
for (const name of Object.keys(index$1.spec.terrain)) {
if (!options.hasOwnProperty(name) && !!index$1.spec.terrain[name].default) {
options[name] = index$1.spec.terrain[name].default;
}
}
for (const key in terrainOptions) {
if (!index$1.deepEqual(terrainOptions[key], currSpec[key])) {
terrain.set(terrainOptions, this.options);
this.stylesheet.terrain = terrainOptions;
const parameters = this._getTransitionParameters({ duration: 0 });
terrain.updateTransitions(parameters);
this.fire(new index$1.Event("data", { dataType: "style" }));
break;
}
}
}
this.mergeTerrain();
this.updateDrapeFirstLayers();
this._markersNeedUpdate = true;
}
_createFog(fogOptions) {
const fog = this.fog = new Fog(fogOptions, this.map.transform, this.scope, this.options);
this.stylesheet.fog = fog.get();
const parameters = this._getTransitionParameters({ duration: 0 });
fog.updateTransitions(parameters);
}
_createSnow(snowOptions) {
const snow = this.snow = new Snow$1(snowOptions, this.map.transform, this.scope, this.options);
this.stylesheet.snow = snow.get();
const parameters = this._getTransitionParameters({ duration: 0 });
snow.updateTransitions(parameters);
}
_createRain(rainOptions) {
const rain = this.rain = new Rain$1(rainOptions, this.map.transform, this.scope, this.options);
this.stylesheet.rain = rain.get();
const parameters = this._getTransitionParameters({ duration: 0 });
rain.updateTransitions(parameters);
}
_updateMarkersOpacity() {
if (this.map._markers.length === 0) {
return;
}
this.map._requestDomTask(() => {
for (const marker of this.map._markers) {
marker._evaluateOpacity();
}
});
}
getFog() {
return this.fog ? this.fog.get() : null;
}
setFog(fogOptions) {
this._checkLoaded();
if (!fogOptions) {
delete this.fog;
delete this.stylesheet.fog;
this._markersNeedUpdate = true;
return;
}
if (!this.fog) {
this._createFog(fogOptions);
} else {
const fog = this.fog;
if (!index$1.deepEqual(fog.get(), fogOptions)) {
fog.set(fogOptions, this.options);
this.stylesheet.fog = fog.get();
const parameters = this._getTransitionParameters({ duration: 0 });
fog.updateTransitions(parameters);
}
}
this._markersNeedUpdate = true;
}
getSnow() {
return this.snow ? this.snow.get() : null;
}
setSnow(snowOptions) {
this._checkLoaded();
if (!snowOptions) {
delete this.snow;
delete this.stylesheet.snow;
return;
}
if (!this.snow) {
this._createSnow(snowOptions);
} else {
const snow = this.snow;
if (!index$1.deepEqual(snow.get(), snowOptions)) {
snow.set(snowOptions, this.options);
this.stylesheet.snow = snow.get();
const parameters = this._getTransitionParameters({ duration: 0 });
snow.updateTransitions(parameters);
}
}
this._markersNeedUpdate = true;
}
getRain() {
return this.rain ? this.rain.get() : null;
}
setRain(rainOptions) {
this._checkLoaded();
if (!rainOptions) {
delete this.rain;
delete this.stylesheet.rain;
return;
}
if (!this.rain) {
this._createRain(rainOptions);
} else {
const rain = this.rain;
if (!index$1.deepEqual(rain.get(), rainOptions)) {
rain.set(rainOptions, this.options);
this.stylesheet.rain = rain.get();
const parameters = this._getTransitionParameters({ duration: 0 });
rain.updateTransitions(parameters);
}
}
this._markersNeedUpdate = true;
}
_reloadColorTheme() {
const updateStyle = () => {
for (const layerId in this._layers) {
const layer = this._layers[layerId];
layer.lut = this._styleColorTheme.lut;
}
for (const id in this._sourceCaches) {
this._sourceCaches[id].clearTiles();
}
};
const colorTheme = this._styleColorTheme.colorThemeOverride ? this._styleColorTheme.colorThemeOverride : this._styleColorTheme.colorTheme;
if (!colorTheme) {
this._styleColorTheme.lut = null;
updateStyle();
return;
}
const data = this._evaluateColorThemeData(colorTheme);
this._loadColorTheme(data).then(() => {
this.fire(new index$1.Event("colorthemeset"));
updateStyle();
}).catch((e) => {
index$1.warnOnce(`Couldn't set color theme: ${e}`);
});
}
setColorTheme(colorTheme) {
this._checkLoaded();
if (this._styleColorTheme.colorThemeOverride) {
index$1.warnOnce(`Note: setColorTheme is called on a style with a color-theme override, the passed color-theme won't be visible.`);
}
this._styleColorTheme.colorTheme = colorTheme;
this._reloadColorTheme();
}
setImportColorTheme(importId, colorTheme) {
const fragmentStyle = this.getFragmentStyle(importId);
if (!fragmentStyle) return;
fragmentStyle._styleColorTheme.colorThemeOverride = colorTheme;
fragmentStyle._reloadColorTheme();
}
_getTransitionParameters(transition) {
return {
now: index$1.exported$1.now(),
transition: Object.assign(this.transition, transition)
};
}
updateDrapeFirstLayers() {
if (!this.terrain) {
return;
}
const draped = [];
const nonDraped = [];
for (const layerId of this._mergedOrder) {
const layer = this._mergedLayers[layerId];
if (this.isLayerDraped(layer)) {
draped.push(layerId);
} else {
nonDraped.push(layerId);
}
}
this._drapedFirstOrder = [];
this._drapedFirstOrder.push(...draped);
this._drapedFirstOrder.push(...nonDraped);
}
_createTerrain(terrainOptions, drapeRenderMode) {
const terrain = this.terrain = new Terrain$1(terrainOptions, drapeRenderMode, this.scope, this.options, this.map.getWorldview());
if (drapeRenderMode === DrapeRenderMode.elevated) {
this.stylesheet.terrain = terrainOptions;
}
this.mergeTerrain();
this.updateDrapeFirstLayers();
this._force3DLayerUpdate();
const parameters = this._getTransitionParameters({ duration: 0 });
terrain.updateTransitions(parameters);
}
_force3DLayerUpdate() {
for (const layerId in this._layers) {
const layer = this._layers[layerId];
if (layer.type === "fill-extrusion") {
this._updateLayer(layer);
}
}
}
_forceSymbolLayerUpdate() {
for (const layerId in this._layers) {
const layer = this._layers[layerId];
if (layer.type === "symbol") {
this._updateLayer(layer);
}
}
}
_validate(validate, key, value, props, options = {}) {
if (options && options.validate === false) {
return false;
}
const style = Object.assign({}, this.serialize());
return emitValidationErrors(this, validate.call(validateStyle, Object.assign({
key,
style,
value,
styleSpec: index$1.spec
}, props)));
}
_remove() {
if (this._request) {
this._request.cancel();
this._request = null;
}
if (this._spriteRequest) {
this._spriteRequest.cancel();
this._spriteRequest = null;
}
index$1.evented.off("pluginStateChange", this._rtlTextPluginCallback);
for (const layerId in this._mergedLayers) {
const layer = this._mergedLayers[layerId];
layer.setEventedParent(null);
}
for (const id in this._mergedSourceCaches) {
this._mergedSourceCaches[id].clearTiles();
this._mergedSourceCaches[id].setEventedParent(null);
}
this.imageManager.removeScope(this.scope);
this.setEventedParent(null);
delete this.fog;
delete this.snow;
delete this.rain;
delete this.terrain;
delete this.ambientLight;
delete this.directionalLight;
if (this.isRootStyle()) {
this.imageManager.setEventedParent(null);
this.imageManager.destroy();
this.modelManager.setEventedParent(null);
this.modelManager.destroy();
this.dispatcher.remove();
}
}
clearSource(id) {
const sourceCaches = this.getSourceCaches(id);
for (const sourceCache of sourceCaches) {
sourceCache.clearTiles();
}
}
clearSources() {
for (const id in this._mergedSourceCaches) {
this._mergedSourceCaches[id].clearTiles();
}
}
clearLayers() {
for (const id in this._mergedLayers) {
const layer = this._mergedLayers[id];
if (layer._clear) {
layer._clear();
}
}
}
reloadSource(id) {
const sourceCaches = this.getSourceCaches(id);
for (const sourceCache of sourceCaches) {
sourceCache.resume();
sourceCache.reload();
}
}
reloadSources() {
for (const source of this.getSources()) {
if (source.reload)
source.reload();
}
}
reloadModels() {
this.modelManager.reloadModels("");
this.forEachFragmentStyle((style) => {
style.modelManager.reloadModels(style.scope);
});
}
updateSources(transform) {
let lightDirection;
if (this.directionalLight) {
lightDirection = shadowDirectionFromProperties(this.directionalLight);
}
const sourcesForBuildingLayers = /* @__PURE__ */ new Set();
const sourcesWithElevatedLayers = /* @__PURE__ */ new Set();
for (const id in this._mergedLayers) {
const layer = this._mergedLayers[id];
if (layer.type === "building") {
sourcesForBuildingLayers.add(layer.source);
}
if (layer.hasElevation() && !sourcesWithElevatedLayers.has(layer.source)) {
sourcesWithElevatedLayers.add(layer.source);
}
}
for (const id in this._mergedSourceCaches) {
const sourceCache = this._mergedSourceCaches[id];
const elevatedLayers = sourcesWithElevatedLayers.has(sourceCache._source.id);
if (sourcesForBuildingLayers.has(sourceCache._source.id)) {
sourceCache._source.reparseOverscaled = false;
}
sourceCache.update(transform, void 0, void 0, lightDirection, elevatedLayers);
}
}
_generateCollisionBoxes() {
for (const id in this._sourceCaches) {
const sourceCache = this._sourceCaches[id];
sourceCache.resume();
sourceCache.reload();
}
}
_updatePlacement(painter, transform, showCollisionBoxes, fadeDuration, crossSourceCollisions, replacementSource, forceFullPlacement = false) {
let symbolBucketsChanged = false;
let placementCommitted = false;
const layerTiles = {};
const layerTilesInYOrder = {};
for (const layerId of this._mergedOrder) {
const styleLayer = this._mergedLayers[layerId];
if (styleLayer.type !== "symbol") continue;
const sourceId = index$1.makeFQID(styleLayer.source, styleLayer.scope);
let sourceTiles = layerTiles[sourceId];
if (!sourceTiles) {
const sourceCache = this.getLayerSourceCache(styleLayer);
if (!sourceCache) continue;
const tiles = sourceCache.getRenderableIds(true).map((id) => sourceCache.getTileByID(id));
layerTilesInYOrder[sourceId] = tiles.slice();
sourceTiles = layerTiles[sourceId] = tiles.sort((a, b) => b.tileID.overscaledZ - a.tileID.overscaledZ || (a.tileID.isLessThan(b.tileID) ? -1 : 1));
}
const layerBucketsChanged = this.crossTileSymbolIndex.addLayer(styleLayer, sourceTiles, transform.center.lng, transform.projection);
symbolBucketsChanged = symbolBucketsChanged || layerBucketsChanged;
}
this.crossTileSymbolIndex.pruneUnusedLayers(this._mergedOrder);
forceFullPlacement = forceFullPlacement || this._layerOrderChanged;
if (this._layerOrderChanged) {
this.fire(new index$1.Event("neworder"));
}
const transformChanged = Boolean(this.placement && !transform.equals(this.placement.transform));
const replacementSourceChanged = Boolean(this.placement && (this.placement.lastReplacementSourceUpdateTime !== 0 && !replacementSource || this.placement.lastReplacementSourceUpdateTime !== replacementSource.updateTime));
const fullFrameUpdateRequired = (transformChanged || replacementSourceChanged || symbolBucketsChanged || this.placement && this.placement.isStale()) && fadeDuration === 0;
if (forceFullPlacement || !this.pauseablePlacement || fullFrameUpdateRequired || fadeDuration !== 0 && this.pauseablePlacement.isDone() && !this.placement.stillRecent(index$1.exported$1.now(), transform.zoom)) {
const fogState = this.fog && transform.projection.supportsFog ? this.fog.state : null;
this.pauseablePlacement = new PauseablePlacement(transform, this._mergedOrder, forceFullPlacement || fadeDuration === 0, showCollisionBoxes, fadeDuration, crossSourceCollisions, this.placement, fogState, this._buildingIndex);
this._layerOrderChanged = false;
}
if (this.pauseablePlacement.isDone()) {
this.placement.setStale();
} else {
this.pauseablePlacement.continuePlacement(this._mergedOrder, this._mergedLayers, layerTiles, layerTilesInYOrder, this.map.painter.scaleFactor);
if (this.pauseablePlacement.isDone()) {
this.placement = this.pauseablePlacement.commit(index$1.exported$1.now());
placementCommitted = true;
}
if (symbolBucketsChanged) {
this.pauseablePlacement.placement.setStale();
}
}
if (placementCommitted || symbolBucketsChanged) {
this._buildingIndex.onNewFrame(transform.zoom);
for (let i = 0; i < this._mergedOrder.length; i++) {
const layerId = this._mergedOrder[i];
const styleLayer = this._mergedLayers[layerId];
if (styleLayer.type !== "symbol") continue;
if (styleLayer.visibility === "none") continue;
const checkAgainstClipLayer = this.isLayerClipped(styleLayer);
this.placement.updateLayerOpacities(styleLayer, layerTiles[index$1.makeFQID(styleLayer.source, styleLayer.scope)], i, checkAgainstClipLayer ? replacementSource : null);
}
}
const needsRerender = !this.pauseablePlacement.isDone() || this.placement.hasTransitions(index$1.exported$1.now());
return { needsRerender };
}
_releaseSymbolFadeTiles() {
for (const id in this._sourceCaches) {
this._sourceCaches[id].releaseSymbolFadeTiles();
}
}
// Fragments and merging
addImport(importSpec, beforeId) {
this._checkLoaded();
const imports = this.stylesheet.imports = this.stylesheet.imports || [];
const index = imports.findIndex(({ id }) => id === importSpec.id);
if (index !== -1) {
this.fire(new index$1.ErrorEvent(new Error(`Import with id '${importSpec.id}' already exists in the map's style.`)));
return;
}
if (!beforeId) {
imports.push(importSpec);
return this._loadImports([importSpec], true);
}
const beforeIndex = imports.findIndex(({ id }) => id === beforeId);
if (beforeIndex === -1) {
this.fire(new index$1.ErrorEvent(new Error(`Import with id "${beforeId}" does not exist on this map.`)));
}
this.stylesheet.imports = imports.slice(0, beforeIndex).concat(importSpec).concat(imports.slice(beforeIndex));
return this._loadImports([importSpec], true, beforeId);
}
updateImport(importId, importSpecification) {
this._checkLoaded();
const imports = this.stylesheet.imports || [];
const index = this.getImportIndex(importId);
if (index === -1) return this;
if (typeof importSpecification === "string") {
this.setImportUrl(importId, importSpecification);
return this;
}
if (importSpecification.url && importSpecification.url !== imports[index].url) {
this.setImportUrl(importId, importSpecification.url);
}
if (!index$1.deepEqual(importSpecification.config, imports[index].config)) {
this.setImportConfig(importId, importSpecification.config, importSpecification.data.schema);
}
if (!index$1.deepEqual(importSpecification.data, imports[index].data)) {
this.setImportData(importId, importSpecification.data);
}
return this;
}
moveImport(importId, beforeId) {
this._checkLoaded();
let imports = this.stylesheet.imports || [];
const index = this.getImportIndex(importId);
if (index === -1) return this;
const beforeIndex = this.getImportIndex(beforeId);
if (beforeIndex === -1) return this;
const importSpec = imports[index];
const fragment = this.fragments[index];
imports = imports.filter(({ id }) => id !== importId);
this.fragments = this.fragments.filter(({ id }) => id !== importId);
this.stylesheet.imports = imports.slice(0, beforeIndex).concat(importSpec).concat(imports.slice(beforeIndex));
this.fragments = this.fragments.slice(0, beforeIndex).concat(fragment).concat(this.fragments.slice(beforeIndex));
this.mergeLayers();
return this;
}
setImportUrl(importId, url) {
this._checkLoaded();
const imports = this.stylesheet.imports || [];
const index = this.getImportIndex(importId);
if (index === -1) return this;
imports[index].url = url;
const fragment = this.fragments[index];
fragment.style = this._createFragmentStyle(imports[index]);
fragment.style.on("style.import.load", () => this.mergeAll());
fragment.style.loadURL(url);
return this;
}
setImportData(importId, stylesheet) {
this._checkLoaded();
const index = this.getImportIndex(importId);
const imports = this.stylesheet.imports || [];
if (index === -1) return this;
if (!stylesheet) {
delete imports[index].data;
return this.setImportUrl(importId, imports[index].url);
}
const fragment = this.fragments[index];
fragment.style.setState(stylesheet);
this._reloadImports();
return this;
}
setImportConfig(importId, config, importSchema) {
this._checkLoaded();
const index = this.getImportIndex(importId);
const imports = this.stylesheet.imports || [];
if (index === -1) return this;
if (config) {
imports[index].config = config;
} else {
delete imports[index].config;
}
const fragment = this.fragments[index];
if (importSchema && fragment.style.stylesheet) {
fragment.style.stylesheet.schema = importSchema;
}
const schema = fragment.style.stylesheet && fragment.style.stylesheet.schema;
fragment.config = config;
fragment.style.updateConfig(config, schema);
this.updateConfigDependencies();
return this;
}
removeImport(importId) {
this._checkLoaded();
const imports = this.stylesheet.imports || [];
const index = this.getImportIndex(importId);
if (index === -1) return;
imports.splice(index, 1);
const fragment = this.fragments[index];
fragment.style._remove();
this.fragments.splice(index, 1);
this._reloadImports();
}
getImportIndex(importId) {
const imports = this.stylesheet.imports || [];
const index = imports.findIndex((importSpec) => importSpec.id === importId);
if (index === -1) {
this.fire(new index$1.ErrorEvent(new Error(`Import '${importId}' does not exist in the map's style and cannot be updated.`)));
}
return index;
}
/**
* Return the style layer object with the given `id`.
*
* @param {string} id ID of the desired layer.
* @returns {TypedStyleLayer} A layer, if one with the given `id` exists.
*/
getLayer(id) {
return this._mergedLayers[id];
}
getSources() {
const sources = [];
for (const id in this._mergedOtherSourceCaches) {
const sourceCache = this._mergedOtherSourceCaches[id];
if (sourceCache) sources.push(sourceCache.getSource());
}
return sources;
}
/**
* Get a source by ID.
* @param {string} id ID of the desired source.
* @returns {?Source} The source object.
*/
getSource(id, scope) {
const sourceCache = this.getSourceCache(id, scope);
return sourceCache && sourceCache.getSource();
}
getLayerSource(layer) {
const sourceCache = this.getLayerSourceCache(layer);
return sourceCache && sourceCache.getSource();
}
getSourceCache(id, scope) {
const fqid = index$1.makeFQID(id, scope);
return this._mergedOtherSourceCaches[fqid];
}
getLayerSourceCache(layer) {
const fqid = index$1.makeFQID(layer.source, layer.scope);
return layer.type === "symbol" ? this._mergedSymbolSourceCaches[fqid] : this._mergedOtherSourceCaches[fqid];
}
/**
* Returns all source caches for a given style FQID.
* If no FQID is provided, returns all source caches,
* including source caches in imported styles.
* @param {string} fqid Style FQID.
* @returns {Array} List of source caches.
*/
getSourceCaches(fqid) {
if (fqid == null)
return Object.values(this._mergedSourceCaches);
const sourceCaches = [];
if (this._mergedOtherSourceCaches[fqid]) {
sourceCaches.push(this._mergedOtherSourceCaches[fqid]);
}
if (this._mergedSymbolSourceCaches[fqid]) {
sourceCaches.push(this._mergedSymbolSourceCaches[fqid]);
}
return sourceCaches;
}
updateSourceCaches() {
const updatedSourceCaches = this._changes.getUpdatedSourceCaches();
for (const fqid in updatedSourceCaches) {
const action = updatedSourceCaches[fqid];
index$1.assert(action === "reload" || action === "clear");
if (action === "reload") {
this.reloadSource(fqid);
} else if (action === "clear") {
this.clearSource(fqid);
}
}
}
updateLayers(parameters) {
const updatedPaintProps = this._changes.getUpdatedPaintProperties();
for (const id of updatedPaintProps) {
const layer = this.getLayer(id);
if (layer) layer.updateTransitions(parameters);
}
}
getGlyphsUrl() {
return this.stylesheet.glyphs;
}
setGlyphsUrl(url) {
this.stylesheet.glyphs = url;
this.glyphManager.setURL(url);
}
// Callbacks from web workers
getImages(mapId, params, callback) {
this.imageManager.getImages(params.images, params.scope, callback);
this._updateTilesForChangedImages();
const setDependencies = (sourceCache) => {
if (sourceCache) {
const dependencies = params.images.map((id) => index$1.ImageId.toString(id));
sourceCache.setDependencies(params.tileID.key, params.type, dependencies);
}
};
const fqid = index$1.makeFQID(params.source, params.scope);
setDependencies(this._mergedOtherSourceCaches[fqid]);
setDependencies(this._mergedSymbolSourceCaches[fqid]);
if (params.images.some((id) => id.iconsetId)) {
this.fire(new index$1.Event("data", { dataType: "style" }));
}
}
rasterizeImages(mapId, params, callback) {
this.imageManager.rasterizeImages(params, callback);
}
getGlyphs(mapId, params, callback) {
this.glyphManager.getGlyphs(params.stacks, callback);
}
getResource(mapId, params, callback) {
return index$1.makeRequest(params, callback);
}
getOwnSourceCache(source) {
return this._otherSourceCaches[source];
}
getOwnLayerSourceCache(layer) {
return layer.type === "symbol" ? this._symbolSourceCaches[layer.source] : this._otherSourceCaches[layer.source];
}
getOwnSourceCaches(source) {
const sourceCaches = [];
if (this._otherSourceCaches[source]) {
sourceCaches.push(this._otherSourceCaches[source]);
}
if (this._symbolSourceCaches[source]) {
sourceCaches.push(this._symbolSourceCaches[source]);
}
return sourceCaches;
}
_isSourceCacheLoaded(source) {
const sourceCaches = this.getOwnSourceCaches(source);
if (sourceCaches.length === 0) {
this.fire(new index$1.ErrorEvent(new Error(`There is no source with ID '${source}'`)));
return false;
}
return sourceCaches.every((sc) => sc.loaded());
}
has3DLayers() {
return this._has3DLayers;
}
hasSymbolLayers() {
return this._hasSymbolLayers;
}
hasCircleLayers() {
return this._hasCircleLayers;
}
isLayerClipped(layer, source) {
if (!this._clipLayerPresent && layer.type !== "fill-extrusion" && layer.type !== "building") return false;
const isFillExtrusion = layer.type === "fill-extrusion" && (layer.sourceLayer === "building" || layer.sourceLayer === "procedural_buildings");
const isBuilding = layer.type === "building";
if (layer.is3D(!!this.terrain)) {
if (isFillExtrusion || isBuilding || !!source && source.type === "batched-model") return true;
if (layer.type === "model") {
return true;
}
} else if (layer.type === "symbol") {
return true;
}
return false;
}
_clearWorkerCaches() {
this.dispatcher.broadcast("clearCaches");
}
destroy() {
this._clearWorkerCaches();
this.fragments.forEach((fragment) => {
fragment.style._remove();
});
if (this.terrainSetForDrapingOnly()) {
delete this.terrain;
delete this.stylesheet.terrain;
}
}
}
Style.getSourceType = getType;
Style.setSourceType = setType;
Style.registerForPluginStateChange = index$1.registerForPluginStateChange;
class IndoorManager extends index$1.Evented {
constructor(style) {
super();
this._style = style;
this._buildings = {};
this._activeFloors = /* @__PURE__ */ new Set();
this._activeFloorsVisible = true;
this._indoorState = { selectedFloorId: null, lastActiveFloors: null, activeFloorsVisible: true };
index$1.bindAll(["_updateUI"], this);
}
destroy() {
this._buildings = {};
this._activeFloors = /* @__PURE__ */ new Set();
this._indoorState = null;
}
selectFloor(floorId) {
if (floorId === this._selectedFloorId && this._activeFloorsVisible) {
return;
}
this._selectedFloorId = floorId;
this._activeFloorsVisible = true;
this._updateActiveFloors();
}
setActiveFloorsVisibility(activeFloorsVisible) {
this._activeFloorsVisible = activeFloorsVisible;
this._updateActiveFloors();
this._updateIndoorSelector();
}
/// Fan in data sent from different tiles and sources and merge it into the one state
/// Both buildings and active floors updates are additive-only
setIndoorData(indoorData) {
for (const [id, building] of Object.entries(indoorData.buildings)) {
if (this._buildings[id]) {
for (const floorId of building.floorIds) {
if (this._buildings[id].floors[floorId]) {
continue;
}
this._buildings[id].floors[floorId] = building.floors[floorId];
}
} else {
this._buildings[id] = building;
}
}
for (const floorId of indoorData.activeFloors) {
this._activeFloors.add(floorId);
}
this._updateIndoorSelector();
}
getIndoorTileOptions(sourceId, scope) {
const sourceLayers = this._style.getIndoorSourceLayers(sourceId, scope);
if (!sourceLayers || !this._indoorState) {
return null;
}
return {
sourceLayers,
indoorState: this._indoorState
};
}
_updateUI(zoom, mapCenter, mapBounds) {
const closestBuildingId = findClosestBuildingId(this._buildings, mapCenter, mapBounds, zoom);
if (closestBuildingId !== this._closestBuildingId) {
this._closestBuildingId = closestBuildingId;
this._updateIndoorSelector();
}
}
_updateIndoorSelector() {
const buildings = this._buildings;
const closestBuildingId = this._closestBuildingId;
const closestBuilding = closestBuildingId && buildings ? buildings[closestBuildingId] : void 0;
if (!closestBuilding) {
this.fire(new index$1.Event("selector-update", {
selectedFloorId: null,
activeFloorsVisible: this._activeFloorsVisible,
floors: []
}));
return;
}
let buildingActiveFloorId = null;
for (const floorId of closestBuilding.floorIds) {
if (this._activeFloors && this._activeFloors.has(floorId)) {
buildingActiveFloorId = floorId;
break;
}
}
const floors = Array.from(closestBuilding.floorIds).map((floorId) => ({
id: floorId,
name: closestBuilding.floors[floorId].name,
zIndex: closestBuilding.floors[floorId].zIndex
})).sort((a, b) => b.zIndex - a.zIndex);
this.fire(new index$1.Event("selector-update", {
selectedFloorId: buildingActiveFloorId,
activeFloorsVisible: this._activeFloorsVisible,
floors
}));
}
// Update previous state and pass it to tiles
// Resolve active floors to construct new set based on data from tiles
// Tiles will resolve individual subsets of activeFloors and send it in setIndoorData
_updateActiveFloors() {
const lastActiveFloors = this._activeFloors;
this._activeFloors = /* @__PURE__ */ new Set();
this._indoorState = { selectedFloorId: this._selectedFloorId, lastActiveFloors, activeFloorsVisible: this._activeFloorsVisible };
this._style.updateIndoorDependentLayers();
}
}
function findClosestBuildingId(buildings, mapCenter, mapBounds, zoom) {
const indoorMinimumZoom = 16;
let closestBuildingId = null;
let minDistance = Number.MAX_SAFE_INTEGER;
if (zoom < indoorMinimumZoom) {
return null;
}
for (const [id, building] of Object.entries(buildings)) {
const buildingCenter = building.center;
if (buildingCenter) {
const distance = mapCenter.distanceTo(index$1.LngLat.convert(buildingCenter));
if (distance < minDistance && mapBounds.contains(buildingCenter)) {
minDistance = distance;
closestBuildingId = id;
}
}
}
return closestBuildingId;
}
var preludeCommon = "\n#define EPSILON 0.0000001\n#define PI 3.141592653589793\n#ifdef RENDER_CUTOFF\nfloat cutoff_opacity(vec4 cutoff_params,float depth) {float near=cutoff_params.x;float far=cutoff_params.y;float cutoffStart=cutoff_params.z;float cutoffEnd=cutoff_params.w;float linearDepth=(depth-near)/(far-near);return clamp((linearDepth-cutoffStart)/(cutoffEnd-cutoffStart),0.0,1.0);}\n#endif";
var preludeFrag = "\n#ifdef DUAL_SOURCE_BLENDING\nlayout(location=0,index=0) out vec4 glFragColor;layout(location=0,index=1) out vec4 glFragColorSrc1;\n#else\nlayout(location=0) out vec4 glFragColor;\n#endif\n#ifdef USE_MRT1\nlayout(location=1) out vec4 out_Target1;\n#endif\nhighp float unpack_depth(highp vec4 rgba_depth)\n{const highp vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);return dot(rgba_depth,bit_shift)*2.0-1.0;}highp vec4 pack_depth(highp float ndc_z) {highp float depth=ndc_z*0.5+0.5;const highp vec4 bit_shift=vec4(255.0*255.0*255.0,255.0*255.0,255.0,1.0);const highp vec4 bit_mask =vec4(0.0,1.0/255.0,1.0/255.0,1.0/255.0);highp vec4 res=fract(depth*bit_shift);res-=res.xxyz*bit_mask;return res;}\n#ifdef INDICATOR_CUTOUT\nuniform vec3 u_indicator_cutout_centers;uniform vec4 u_indicator_cutout_params;\n#endif\nvec4 applyCutout(vec4 color,float height) {\n#ifdef INDICATOR_CUTOUT\nfloat verticalFadeRange=u_indicator_cutout_centers.z*0.25;float holeMinOpacity=mix(1.0,u_indicator_cutout_params.x,smoothstep(u_indicator_cutout_centers.z,u_indicator_cutout_centers.z+verticalFadeRange,height));float holeRadius=max(u_indicator_cutout_params.y,0.0);float holeAspectRatio=u_indicator_cutout_params.z;float fadeStart=u_indicator_cutout_params.w;float distA=distance(vec2(gl_FragCoord.x,gl_FragCoord.y*holeAspectRatio),vec2(u_indicator_cutout_centers[0],u_indicator_cutout_centers[1]*holeAspectRatio));return color*min(smoothstep(fadeStart,holeRadius,distA)+holeMinOpacity,1.0);\n#else\nreturn color;\n#endif\n}\n#ifdef DEBUG_WIREFRAME\n#define HANDLE_WIREFRAME_DEBUG \\\nglFragColor=vec4(0.7,0.0,0.0,0.7); \\\ngl_FragDepth=gl_FragCoord.z-0.0001;\n#else\n#define HANDLE_WIREFRAME_DEBUG\n#endif\n#ifdef RENDER_CUTOFF\nuniform highp vec4 u_cutoff_params;in float v_cutoff_opacity;\n#endif\nvec4 textureLodCustom(sampler2D image,highp vec2 pos,highp vec2 lod_coord) {highp vec2 size=vec2(textureSize(image,0));highp vec2 dx=dFdx(lod_coord.xy*size);highp vec2 dy=dFdy(lod_coord.xy*size);highp float delta_max_sqr=max(dot(dx,dx),dot(dy,dy));highp float lod=0.5*log2(delta_max_sqr);return textureLod(image,pos,lod);}vec4 applyLUT(highp sampler3D lut,vec4 col) {vec3 size=vec3(textureSize(lut,0));vec3 uvw=(col.rbg*float(size-1.0)+0.5)/size;return vec4(texture(lut,uvw).rgb*col.a,col.a);}vec3 applyLUT(highp sampler3D lut,vec3 col) {return applyLUT(lut,vec4(col,1.0)).rgb;}";
var preludeVert = "\n#define EXTENT 8192.0\n#define RAD_TO_DEG 180.0/PI\n#define DEG_TO_RAD PI/180.0\n#define GLOBE_RADIUS EXTENT/PI/2.0\nfloat wrap(float n,float min,float max) {float d=max-min;float w=mod(mod(n-min,d)+d,d)+min;return (w==min) ? max : w;}\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 mercator_tile_position(mat4 matrix,vec2 tile_anchor,vec3 tile_id,vec2 mercator_center) {\n#ifndef PROJECTED_POS_ON_VIEWPORT\nfloat tiles=tile_id.z;vec2 mercator=(tile_anchor/EXTENT+tile_id.xy)/tiles;mercator-=mercator_center;mercator.x=wrap(mercator.x,-0.5,0.5);vec4 mercator_tile=vec4(mercator.xy*EXTENT,EXTENT/(2.0*PI),1.0);mercator_tile=matrix*mercator_tile;return mercator_tile.xyz;\n#else\nreturn vec3(0.0);\n#endif\n}vec3 mix_globe_mercator(vec3 globe,vec3 mercator,float t) {return mix(globe,mercator,t);}mat3 globe_mercator_surface_vectors(vec3 pos_normal,vec3 up_dir,float zoom_transition) {vec3 normal=zoom_transition==0.0 ? pos_normal : normalize(mix(pos_normal,up_dir,zoom_transition));vec3 xAxis=normalize(vec3(normal.z,0.0,-normal.x));vec3 yAxis=normalize(cross(normal,xAxis));return mat3(xAxis,yAxis,normal);}\n#endif\nvec2 unpack_float(const float packedValue) {int packedIntValue=int(packedValue);int v0=packedIntValue/256;return vec2(v0,packedIntValue-v0*256);}vec2 unpack_opacity(const float packedOpacity) {int intOpacity=int(packedOpacity)/2;return vec2(float(intOpacity)/127.0,mod(packedOpacity,2.0));}vec4 decode_color(const vec2 encodedColor) {return vec4(\nunpack_float(encodedColor[0])/255.0,unpack_float(encodedColor[1])/255.0\n);}float unpack_mix_vec2(const vec2 packedValue,const float t) {return mix(packedValue[0],packedValue[1],t);}vec4 unpack_mix_color(const vec4 packedColors,const float t) {vec4 minColor=decode_color(vec2(packedColors[0],packedColors[1]));vec4 maxColor=decode_color(vec2(packedColors[2],packedColors[3]));return mix(minColor,maxColor,t);}vec2 get_pattern_pos(const vec2 pixel_coord_upper,const vec2 pixel_coord_lower,const vec2 pattern_size,const vec2 units_to_pixels,const vec2 pos) {vec2 offset=mod(mod(mod(pixel_coord_upper,pattern_size)*256.0,pattern_size)*256.0+pixel_coord_lower,pattern_size);return (units_to_pixels*pos+offset)/pattern_size;}vec2 get_pattern_pos(const vec2 pixel_coord_upper,const vec2 pixel_coord_lower,const vec2 pattern_size,const float tile_units_to_pixels,const vec2 pos) {return get_pattern_pos(pixel_coord_upper,pixel_coord_lower,pattern_size,vec2(tile_units_to_pixels),pos);}float mercatorXfromLng(float lng) {return (180.0+lng)/360.0;}float mercatorYfromLat(float lat) {return (180.0-(RAD_TO_DEG*log(tan(PI/4.0+lat/2.0*DEG_TO_RAD))))/360.0;}vec3 latLngToECEF(vec2 latLng) {latLng=DEG_TO_RAD*latLng;float cosLat=cos(latLng[0]);float sinLat=sin(latLng[0]);float cosLng=cos(latLng[1]);float sinLng=sin(latLng[1]);float sx=cosLat*sinLng*GLOBE_RADIUS;float sy=-sinLat*GLOBE_RADIUS;float sz=cosLat*cosLng*GLOBE_RADIUS;return vec3(sx,sy,sz);}\n#ifdef RENDER_CUTOFF\nuniform vec4 u_cutoff_params;out float v_cutoff_opacity;\n#endif\nconst vec4 AWAY=vec4(-1000.0,-1000.0,-1000.0,1);const float skirtOffset=24575.0;vec3 decomposeToPosAndSkirt(vec2 posWithComposedSkirt)\n{float skirt=float(posWithComposedSkirt.x >=skirtOffset);vec2 pos=posWithComposedSkirt-vec2(skirt*skirtOffset,0.0);return vec3(pos,skirt);}\n#ifndef HAS_SHADER_STORAGE_BLOCK_material_buffer\n#define GET_ATTRIBUTE_float(attrib,matInfo,attrib_id) attrib\n#define GET_ATTRIBUTE_vec4(attrib,matInfo,attrib_id) attrib\n#define GET_ATTRIBUTE_vec2(attrib,matInfo,attrib_id) attrib\n#define DECLARE_MATERIAL_TABLE_INFO\n#endif";
var backgroundFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform vec4 u_color;uniform float u_opacity;uniform mediump float u_emissive_strength;\n#ifdef LIGHTING_3D_MODE\nin vec4 v_color;\n#endif\nvoid main() {vec4 out_color;\n#ifdef LIGHTING_3D_MODE\nout_color=v_color;\n#else\nout_color=u_color;\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nglFragColor=out_color*u_opacity;\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var backgroundVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_lighting.glsl\"\nin vec2 a_pos;uniform mat4 u_matrix;uniform mediump float u_emissive_strength;\n#ifdef LIGHTING_3D_MODE\nuniform mediump vec4 u_color;out vec4 v_color;\n#endif\nvoid main() {gl_Position=u_matrix*vec4(a_pos,0,1);\n#ifdef LIGHTING_3D_MODE\nv_color=apply_lighting_with_emission_ground(u_color,u_emissive_strength);\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var backgroundPatternFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform vec2 u_pattern_tl;uniform vec2 u_pattern_br;uniform vec2 u_texsize;uniform float u_opacity;uniform float u_emissive_strength;uniform sampler2D u_image;in highp vec2 v_pos;void main() {highp vec2 imagecoord=mod(v_pos,1.0);highp vec2 pos=mix(u_pattern_tl/u_texsize,u_pattern_br/u_texsize,imagecoord);vec4 out_color=textureLodCustom(u_image,pos,v_pos);\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nglFragColor=out_color*u_opacity;\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var backgroundPatternVert = "#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;uniform vec2 u_pattern_size;uniform vec2 u_pixel_coord_upper;uniform vec2 u_pixel_coord_lower;uniform vec2 u_pattern_units_to_pixels;in vec2 a_pos;out highp vec2 v_pos;void main() {gl_Position=u_matrix*vec4(a_pos,0,1);v_pos=get_pattern_pos(u_pixel_coord_upper,u_pixel_coord_lower,u_pattern_size,u_pattern_units_to_pixels,a_pos);\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var buildingFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nconst float window_depth=0.5;const float ao_radius=0.2;in vec4 v_color;in highp vec3 v_normal;in highp vec3 v_pos;\n#ifdef BUILDING_FAUX_FACADE\nin lowp float v_faux_facade;in highp float v_faux_facade_ed;in highp vec2 v_faux_facade_window;in highp vec2 v_faux_facade_floor;in highp vec2 v_faux_facade_range;in highp float v_aspect;in highp vec3 v_tbn_0;in highp vec3 v_tbn_1;in highp vec3 v_tbn_2;in highp vec4 v_faux_color_emissive;uniform float u_faux_facade_ao_intensity;\n#endif\n#ifdef RENDER_SHADOWS\nin highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;\n#endif\n#ifdef FLOOD_LIGHT\nin highp float v_flood_radius;in float v_has_flood_light;\n#endif\nuniform lowp float u_opacity;uniform vec3 u_camera_pos;uniform highp float u_tile_to_meter;uniform float u_facade_emissive_chance;uniform vec3 u_flood_light_color;uniform float u_flood_light_intensity;vec3 linearTosRGB(in vec3 color) {return pow(color,vec3(1./2.2));}\n#ifdef BUILDING_FAUX_FACADE\nfloat hash12(in vec2 p) {vec3 p3 =fract(vec3(p.xyx)*0.1031);p3+=dot(p3,p3.yzx+33.33);return fract((p3.x+p3.y)*p3.z);}float min3(in vec3 v) {return min(min(v.x,v.y),v.z);}vec2 get_uv_mask_id(in vec2 q,out float mask,out vec2 id) {vec2 p=q;mask=step(v_faux_facade_range.x,p.y)*step(p.y,v_faux_facade_range.y);p.y=p.y-v_faux_facade_range.x;vec2 uv=modf(p/v_faux_facade_floor,id);vec4 d=(v_faux_facade_floor.xyxy+vec4(-v_faux_facade_window,v_faux_facade_window))*0.5;vec4 edge=d/v_faux_facade_floor.xyxy;vec2 m=step(edge.xy,uv)*step(uv,edge.zw);mask*=m.x*m.y;uv-=vec2(0.5);uv*=vec2(0.5)/(vec2(0.5)-edge.xy);uv+=vec2(0.5);return uv;}float ray_unit_box(in vec3 ray_o,in vec3 ray_d,in vec3 bmin,in vec3 bmax) {vec3 planes=mix(bmin,bmax,step(0.0,ray_d));vec3 t=(planes-ray_o)/ray_d;return min3(t);}float get_emissive(in vec2 id) {if (u_facade_emissive_chance > 0.0) {return (step(hash12(id),u_facade_emissive_chance)+0.05)*v_faux_color_emissive.a;}return 0.0;}vec3 get_shade_info(in vec3 v,in vec3 v_normalized,in vec3 color,in vec2 id,in mat3 tbn,inout vec3 out_normal,inout float out_emissive) {vec3 out_color=color;vec3 abs_v=abs(v_normalized);bool x_major=abs_v.x >=abs_v.y && abs_v.x >=abs_v.z;bool y_major=abs_v.y >=abs_v.x && abs_v.y >=abs_v.z;bool z_major=abs_v.z >=abs_v.x && abs_v.z >=abs_v.y;\n#if 0\nif (x_major) {out_color=v.x > 0.0 ? vec3(1.0,0.0,0.0) : vec3(0.0,1.0,1.0);} else if (y_major) {out_color=v.y > 0.0 ? vec3(0.0,1.0,0.0) : vec3(1.0,0.0,1.0);} else if (z_major) {out_color=v.z > 0.0 ? vec3(0.0,0.0,1.0) : vec3(1.0,1.0,0.0);}out_emissive=1.0;\n#else\nif (x_major) {out_normal=sign(v.x)*tbn[0];} else if (y_major) {out_normal=vec3(0.0,0.0,-sign(v.y));} else if (z_major) {out_color=v_faux_color_emissive.rgb;out_emissive=v.z <=0.0 ? get_emissive(id) : out_emissive;}float ao=1.0;if (u_faux_facade_ao_intensity > 0.0) {vec4 ao_range=v_faux_facade_window.xxyy*0.5-vec4(0,ao_radius,0,ao_radius);vec2 ao_range_z=vec2(window_depth*0.5)-vec2(0.0,ao_radius);if (x_major || y_major) {ao*=smoothstep(-ao_range_z.x,-ao_range_z.y,v.z);} else if (z_major) {ao*=smoothstep(-ao_range.x,-ao_range.y,v.x)*(1.0-smoothstep(ao_range.y,ao_range.x,v.x));ao*=smoothstep(-ao_range.z,-ao_range.w,v.y)*(1.0-smoothstep(ao_range.w,ao_range.z,v.y));}ao=mix(1.0,min(1.0,ao+0.25),u_faux_facade_ao_intensity);}out_color*=ao;\n#endif\nreturn out_color;}\n#endif\nvec3 apply_lighting_linear(in vec3 color,in vec3 normal,in float dir_factor) {float ambient_directional_factor=calculate_ambient_directional_factor(normal);vec3 ambient_contrib=ambient_directional_factor*u_lighting_ambient_color;vec3 directional_contrib=u_lighting_directional_color*dir_factor;return color*(ambient_contrib+directional_contrib);}void main() {vec3 normal=normalize(v_normal);vec3 base_color=v_color.rgb;float emissive=v_color.a;\n#ifdef BUILDING_FAUX_FACADE\nif (v_faux_facade > 0.0) {mat3 tbn=mat3(v_tbn_0,v_tbn_1,v_tbn_2);vec3 v=vec3(v_pos.xy,v_pos.z/u_tile_to_meter)-u_camera_pos;vec3 view_tangent=transpose(tbn)*v;vec2 q=vec2(v_faux_facade_ed,v_pos.z);float mask=0.0;vec2 id=vec2(0.0);vec2 uv=get_uv_mask_id(q,mask,id);uv*=v_faux_facade_window;vec3 bmin=vec3(0.0,0.0,-window_depth);vec3 bmax=bmin+vec3(v_faux_facade_window,window_depth);vec3 ray_o=vec3(uv,0.0);vec3 ray_d=normalize(view_tangent);float t_min=ray_unit_box(ray_o,ray_d,bmin,bmax);vec3 hit=ray_o+t_min*ray_d;vec3 r=vec3(v_faux_facade_window,-window_depth);hit-=r*0.5;vec3 normalized=hit/r;vec3 out_normal=normal;float out_emissive=emissive;vec3 room_color=get_shade_info(hit,normalized,base_color,id,tbn,out_normal,out_emissive);base_color=mix(base_color,room_color,mask);normal=mix(normal,out_normal,mask);emissive=mix(emissive,out_emissive,mask);}\n#endif\nvec4 color=vec4(base_color,1.0);vec3 xy_flipped_normal=vec3(-normal.xy,normal.z);float shadowed_lighting_factor=0.0;\n#ifdef RENDER_SHADOWS\n#ifdef RENDER_CUTOFF\nshadowed_lighting_factor=shadowed_light_factor_normal_opacity(xy_flipped_normal,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w,v_cutoff_opacity);if (v_cutoff_opacity==0.0) {discard;}\n#else\nshadowed_lighting_factor=shadowed_light_factor_normal(xy_flipped_normal,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w);\n#endif\n#else\nshadowed_lighting_factor=dot(xy_flipped_normal,u_lighting_directional_dir);\n#endif\ncolor.rgb=apply_lighting_linear(color.rgb,xy_flipped_normal,shadowed_lighting_factor);color.rgb=linearTosRGB(color.rgb);\n#ifdef FLOOD_LIGHT\nfloat flood_radiance=(1.0-min(v_pos.z/v_flood_radius,1.0))*u_flood_light_intensity*v_has_flood_light;color.rgb=mix(color.rgb,u_flood_light_color,flood_radiance);\n#endif\ncolor.rgb=mix(color.rgb,linearTosRGB(base_color.rgb),emissive);\n#ifdef FOG\ncolor=fog_dither(fog_apply_premultiplied(color,v_fog_pos,v_pos.z));\n#endif\ncolor*=u_opacity;\n#ifdef INDICATOR_CUTOUT\ncolor=applyCutout(color,v_pos.z);\n#endif\n#ifdef FEATURE_CUTOUT\ncolor=apply_feature_cutout(color,gl_FragCoord);\n#endif\nglFragColor=color; \n#ifdef DEBUG_SHOW_NORMALS\ncolor.rgb=xy_flipped_normal*0.5+vec3(0.5,0.5,0.5);color.a=1.0;glFragColor=color;\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var buildingVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nin vec3 a_pos_3f;in vec3 a_normal_3;in vec3 a_centroid_3;in float a_flood_light_wall_radius_1i16;in vec4 a_faux_facade_data;in vec2 a_faux_facade_vertical_range;uniform mat4 u_matrix;uniform mat4 u_normal_matrix;uniform highp float u_tile_to_meter;out vec4 v_color;out vec3 v_normal;out highp vec3 v_pos;\n#ifdef BUILDING_FAUX_FACADE\nout lowp float v_faux_facade;out highp float v_faux_facade_ed;out highp vec2 v_faux_facade_window;out highp vec2 v_faux_facade_floor;out highp vec2 v_faux_facade_range;out highp float v_aspect;out highp vec3 v_tbn_0;out highp vec3 v_tbn_1;out highp vec3 v_tbn_2;out highp vec4 v_faux_color_emissive;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;\n#endif\n#ifdef FLOOD_LIGHT\nout highp float v_flood_radius;out float v_has_flood_light;\n#endif\nconst float MAX_UINT_16=65535.0;const float MAX_INT_16=32767.0;const float MAX_UINT_8=255.0;const float TWO_POW_8=256.0;const float FLOOD_LIGHT_MAX_RADIUS_METER=2048.0;vec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}\n#ifdef BUILDING_FAUX_FACADE\nmat3 get_tbn(in vec3 normal) {const vec3 bitangent=vec3(0.0,0.0,1.0);vec3 tangent=normalize(vec3(normal.y,-normal.x,0.0));return mat3(tangent,bitangent,normal);}\n#endif\n#pragma mapbox: define-attribute-vertex-shader-only highp vec2 part_color_emissive\n#pragma mapbox: define-attribute-vertex-shader-only highp vec2 faux_facade_color_emissive\nvoid main() {\n#pragma mapbox: initialize-attribute-custom highp vec2 part_color_emissive\n#pragma mapbox: initialize-attribute-custom highp vec2 faux_facade_color_emissive\n#ifdef FLOOD_LIGHT\nv_flood_radius=(a_flood_light_wall_radius_1i16/MAX_INT_16*FLOOD_LIGHT_MAX_RADIUS_METER);v_has_flood_light=step(0.0,v_flood_radius);\n#endif\nvec4 color_emissive=decode_color(part_color_emissive);v_color=vec4(sRGBToLinear(color_emissive.rgb),color_emissive.a);vec3 a_normal_3f=a_normal_3/MAX_INT_16;v_normal=vec3(u_normal_matrix*vec4(a_normal_3f,0.0));float hidden=0.0;float depth_offset=0.0;\n#ifdef BUILDING_FAUX_FACADE\nv_faux_facade=a_faux_facade_data.x;if (v_faux_facade > 0.0) {v_faux_facade_ed=a_faux_facade_data.x *u_tile_to_meter;float window_x_perc=floor(a_faux_facade_data.y/TWO_POW_8);float window_y_perc=a_faux_facade_data.y-TWO_POW_8*window_x_perc;vec2 window_perc=vec2(window_x_perc,window_y_perc)/MAX_UINT_8;v_faux_facade_floor=(a_faux_facade_data.zw/MAX_UINT_16*EXTENT)*u_tile_to_meter;v_faux_facade_window=window_perc*v_faux_facade_floor;v_faux_facade_range=(a_faux_facade_vertical_range/MAX_UINT_16*EXTENT)*u_tile_to_meter;v_aspect=v_faux_facade_window.x/v_faux_facade_window.y;mat3 tbn=get_tbn(normalize(v_normal));v_tbn_0=tbn[0];v_tbn_1=tbn[1];v_tbn_2=tbn[2];v_faux_color_emissive=decode_color(faux_facade_color_emissive);v_faux_color_emissive.rgb=sRGBToLinear(v_faux_color_emissive.rgb);float height=a_centroid_3.z;depth_offset=min(1000.0,height)*0.0000002;}\n#endif\nv_pos=a_pos_3f;\n#ifdef RENDER_CUTOFF\nvec4 ground=u_matrix*vec4(a_centroid_3,1.0);v_cutoff_opacity=cutoff_opacity(u_cutoff_params,ground.z);hidden=float(v_cutoff_opacity==0.0);v_pos.z*=v_cutoff_opacity;\n#endif\n#ifdef RENDER_SHADOWS\nvec3 shadow_pos=v_pos;\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset_model(v_normal);shadow_pos+=offset*shadow_normal_offset_multiplier0();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shadow_pos,1.0);v_pos_light_view_1=u_light_matrix_1*vec4(shadow_pos,1.0);\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(v_pos);\n#endif\ngl_Position=mix(u_matrix*vec4(v_pos,1),AWAY,hidden);gl_Position.z-=depth_offset*gl_Position.w;}";
var buildingBloomFrag = "in vec4 v_color_emissive;\n#pragma mapbox: define-attribute highp vec4 bloom_attenuation\n#pragma mapbox: initialize-attribute highp vec4 bloom_attenuation\nfloat saturate(float val) {return clamp(val,0.0,1.0);}void main() {float emission=v_color_emissive.a;float opacity=1.0;\n#ifdef HAS_ATTRIBUTE_a_bloom_attenuation\nfloat distance=length(vec2(1.3*max(0.0,abs(bloom_attenuation.x)-bloom_attenuation.z),bloom_attenuation.y));distance+= mix(0.5,0.0,clamp(emission-1.0,0.0,1.0));opacity*=saturate(1.0-distance*distance);\n#endif\n#ifdef RENDER_CUTOFF\nopacity*=v_cutoff_opacity;\n#endif\nglFragColor=vec4(v_color_emissive.rgb,1.0)*opacity;}";
var buildingBloomVert = "in vec3 a_pos_3f;\n#pragma mapbox: define-attribute-vertex-shader-only highp vec2 part_color_emissive\n#pragma mapbox: define-attribute highp vec4 bloom_attenuation\nout vec4 v_color_emissive;uniform mat4 u_matrix;vec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}void main() {\n#pragma mapbox: initialize-attribute-custom highp vec2 part_color_emissive\n#pragma mapbox: initialize-attribute highp vec4 bloom_attenuation\n#ifdef HAS_ATTRIBUTE_a_part_color_emissive\nvec4 color_emissive=decode_color(part_color_emissive);float part_emissive=color_emissive.a*5.0;v_color_emissive=vec4(sRGBToLinear(color_emissive.rgb),part_emissive);\n#else\nv_color_emissive=vec4(1.0);\n#endif\ngl_Position=u_matrix*vec4(a_pos_3f,1.0);\n#ifdef RENDER_CUTOFF\nv_cutoff_opacity=cutoff_opacity(u_cutoff_params,gl_Position.z);\n#endif\n}";
var buildingDepthFrag = "in highp float v_depth;void main() {\n#ifndef DEPTH_TEXTURE\nglFragColor=pack_depth(v_depth);\n#endif\n}";
var buildingDepthVert = "in vec3 a_pos_3f;uniform mat4 u_matrix;out highp float v_depth;void main() {gl_Position=u_matrix*vec4(a_pos_3f,1.0);v_depth=gl_Position.z/gl_Position.w;}";
var circleFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nin vec3 v_data;in float v_visibility;\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define mediump float radius\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define highp vec4 stroke_color\n#pragma mapbox: define mediump float stroke_width\n#pragma mapbox: define lowp float stroke_opacity\nuniform float u_emissive_strength;void main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize mediump float radius\n#pragma mapbox: initialize lowp float blur\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize highp vec4 stroke_color\n#pragma mapbox: initialize mediump float stroke_width\n#pragma mapbox: initialize lowp float stroke_opacity\nvec2 extrude=v_data.xy;float blur_positive=blur < 0.0 ? 0.0 : 1.0;lowp float antialiasblur=v_data.z;float extrude_length=length(extrude)+antialiasblur*(1.0-blur_positive);float antialiased_blur=-max(abs(blur),antialiasblur);float antialiase_blur_opacity=smoothstep(0.0,antialiasblur,extrude_length-1.0);float opacity_t=blur_positive==1.0 ? \nsmoothstep(0.0,-antialiased_blur,1.0-extrude_length) : \nsmoothstep(antialiased_blur,0.0,extrude_length-1.0)-antialiase_blur_opacity;float color_t=stroke_width < 0.01 ? 0.0 : smoothstep(\nantialiased_blur,0.0,extrude_length-radius/(radius+stroke_width)\n);vec4 out_color=mix(color*opacity,stroke_color*stroke_opacity,color_t);\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#endif\n#ifdef FOG\nout_color=fog_apply_premultiplied(out_color,v_fog_pos);\n#endif\nglFragColor=out_color*(v_visibility*opacity_t);\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\n}";
var circleVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\n#define NUM_VISIBILITY_RINGS 2\n#define INV_SQRT2 0.70710678\n#define ELEVATION_BIAS 0.0001\n#define NUM_SAMPLES_PER_RING 16\nuniform mat4 u_matrix;uniform mat2 u_extrude_scale;uniform lowp float u_device_pixel_ratio;uniform highp float u_camera_to_center_distance;in vec2 a_pos;\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_pos_3;in vec3 a_pos_normal_3;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform vec3 u_tile_id;uniform float u_zoom_transition;uniform vec3 u_up_dir;\n#endif\n#ifdef ELEVATED_ROADS\nin float a_circle_z_offset;\n#endif\nout vec3 v_data;out float v_visibility;\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define mediump float radius\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define highp vec4 stroke_color\n#pragma mapbox: define mediump float stroke_width\n#pragma mapbox: define lowp float stroke_opacity\nvec2 calc_offset(vec2 extrusion,float radius,float stroke_width, float view_scale) {return extrusion*(radius+stroke_width)*u_extrude_scale*view_scale;}float cantilevered_elevation(vec2 pos,float radius,float stroke_width,float view_scale) {vec2 c1=pos+calc_offset(vec2(-1,-1),radius,stroke_width,view_scale);vec2 c2=pos+calc_offset(vec2(1,-1),radius,stroke_width,view_scale);vec2 c3=pos+calc_offset(vec2(1,1),radius,stroke_width,view_scale);vec2 c4=pos+calc_offset(vec2(-1,1),radius,stroke_width,view_scale);float h1=elevation(c1)+ELEVATION_BIAS;float h2=elevation(c2)+ELEVATION_BIAS;float h3=elevation(c3)+ELEVATION_BIAS;float h4=elevation(c4)+ELEVATION_BIAS;return max(h4,max(h3,max(h1,h2)));}float circle_elevation(vec2 pos) {\n#if defined(TERRAIN)\nreturn elevation(pos)+ELEVATION_BIAS;\n#else\nreturn 0.0;\n#endif\n}vec4 project_vertex(vec2 extrusion,vec4 world_center,vec4 projected_center,float radius,float stroke_width, float view_scale,mat3 surface_vectors) {vec2 sample_offset=calc_offset(extrusion,radius,stroke_width,view_scale);\n#ifdef PITCH_WITH_MAP\n#ifdef PROJECTION_GLOBE_VIEW\nreturn u_matrix*( world_center+vec4(sample_offset.x*surface_vectors[0]+sample_offset.y*surface_vectors[1],0) );\n#else\nreturn u_matrix*( world_center+vec4(sample_offset,0,0) );\n#endif\n#else\nreturn projected_center+vec4(sample_offset,0,0);\n#endif\n}float get_sample_step() {\n#ifdef PITCH_WITH_MAP\nreturn 2.0*PI/float(NUM_SAMPLES_PER_RING);\n#else\nreturn PI/float(NUM_SAMPLES_PER_RING);\n#endif\n}void main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize mediump float radius\n#pragma mapbox: initialize lowp float blur\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize highp vec4 stroke_color\n#pragma mapbox: initialize mediump float stroke_width\n#pragma mapbox: initialize lowp float stroke_opacity\nvec2 extrude=vec2(mod(a_pos,2.0)*2.0-1.0);vec2 circle_center=floor(a_pos*0.5);vec4 world_center;mat3 surface_vectors;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 pos_normal_3=a_pos_normal_3/16384.0;surface_vectors=globe_mercator_surface_vectors(pos_normal_3,u_up_dir,u_zoom_transition);vec3 surface_extrusion=extrude.x*surface_vectors[0]+extrude.y*surface_vectors[1];vec3 globe_elevation=elevationVector(circle_center)*circle_elevation(circle_center);vec3 globe_pos=a_pos_3+surface_extrusion+globe_elevation;vec3 mercator_elevation=u_up_dir*u_tile_up_scale*circle_elevation(circle_center);vec3 merc_pos=mercator_tile_position(u_inv_rot_matrix,circle_center,u_tile_id,u_merc_center)+surface_extrusion+mercator_elevation;vec3 pos=mix_globe_mercator(globe_pos,merc_pos,u_zoom_transition);world_center=vec4(pos,1);\n#else \nsurface_vectors=mat3(1.0);float height=circle_elevation(circle_center);world_center=vec4(circle_center,height,1);\n#endif\n#ifdef ELEVATED_ROADS\nworld_center.z+=a_circle_z_offset+ELEVATION_BIAS;\n#endif\nvec4 projected_center=u_matrix*world_center;float view_scale=0.0;\n#ifdef PITCH_WITH_MAP\n#ifdef SCALE_WITH_MAP\nview_scale=1.0;\n#else\nview_scale=projected_center.w/u_camera_to_center_distance;\n#endif\n#else\n#ifdef SCALE_WITH_MAP\nview_scale=u_camera_to_center_distance;\n#else\nview_scale=projected_center.w;\n#endif\n#endif\ngl_Position=project_vertex(extrude,world_center,projected_center,radius,stroke_width,view_scale,surface_vectors);float visibility=0.0;\n#ifdef TERRAIN\nfloat step=get_sample_step();vec4 occlusion_world_center;vec4 occlusion_projected_center;\n#ifdef PITCH_WITH_MAP\nfloat cantilevered_height=cantilevered_elevation(circle_center,radius,stroke_width,view_scale);occlusion_world_center=vec4(circle_center,cantilevered_height,1);occlusion_projected_center=u_matrix*occlusion_world_center;\n#else\nocclusion_world_center=world_center;occlusion_projected_center=projected_center;\n#endif\nfor(int ring=0; ring < NUM_VISIBILITY_RINGS; ring++) {float scale=(float(ring)+1.0)/float(NUM_VISIBILITY_RINGS);for(int i=0; i < NUM_SAMPLES_PER_RING; i++) {vec2 extrusion=vec2(cos(step*float(i)),-sin(step*float(i)))*scale;vec4 frag_pos=project_vertex(extrusion,occlusion_world_center,occlusion_projected_center,radius,stroke_width,view_scale,surface_vectors);visibility+=float(!isOccluded(frag_pos));}}visibility/=float(NUM_VISIBILITY_RINGS)*float(NUM_SAMPLES_PER_RING);\n#else\nvisibility=1.0;\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nvisibility=1.0;\n#endif\nv_visibility=visibility;lowp float antialiasblur=1.0/u_device_pixel_ratio/(radius+stroke_width);v_data=vec3(extrude.x,extrude.y,antialiasblur);\n#ifdef FOG\nv_fog_pos=fog_position(world_center.xyz);\n#endif\n}";
var clippingMaskFrag = "void main() {glFragColor=vec4(1.0);}";
var clippingMaskVert = "in vec2 a_pos;uniform mat4 u_matrix;void main() {gl_Position=u_matrix*vec4(a_pos,0,1);}";
var heatmapFrag = "#include \"_prelude_fog.fragment.glsl\"\nuniform highp float u_intensity;in vec2 v_extrude;\n#pragma mapbox: define highp float weight\n#define GAUSS_COEF 0.3989422804014327\nvoid main() {\n#pragma mapbox: initialize highp float weight\nfloat d=-0.5*3.0*3.0*dot(v_extrude,v_extrude);float val=weight*u_intensity*GAUSS_COEF*exp(d);glFragColor=vec4(val,1.0,1.0,1.0);\n#ifdef FOG\nif (u_is_globe==0) {glFragColor.r*=pow(1.0-fog_opacity(v_fog_pos),2.0);}\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var heatmapVert = "#include \"_prelude_terrain.vertex.glsl\"\n#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;uniform float u_extrude_scale;uniform float u_opacity;uniform float u_intensity;in vec2 a_pos;\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_pos_3;in vec3 a_pos_normal_3;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform vec3 u_tile_id;uniform float u_zoom_transition;uniform vec3 u_up_dir;\n#endif\nout vec2 v_extrude;\n#pragma mapbox: define highp float weight\n#pragma mapbox: define mediump float radius\nconst highp float ZERO=1.0/255.0/16.0;\n#define GAUSS_COEF 0.3989422804014327\nvoid main() {\n#pragma mapbox: initialize highp float weight\n#pragma mapbox: initialize mediump float radius\nvec2 unscaled_extrude=vec2(mod(a_pos,2.0)*2.0-1.0);float S=sqrt(-2.0*log(ZERO/weight/u_intensity/GAUSS_COEF))/3.0;v_extrude=S*unscaled_extrude;vec2 extrude=v_extrude*radius*u_extrude_scale;vec2 tilePos=floor(a_pos*0.5);vec3 pos;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 pos_normal_3=a_pos_normal_3/16384.0;mat3 surface_vectors=globe_mercator_surface_vectors(pos_normal_3,u_up_dir,u_zoom_transition);vec3 surface_extrusion=extrude.x*surface_vectors[0]+extrude.y*surface_vectors[1];vec3 globe_elevation=elevationVector(tilePos)*elevation(tilePos);vec3 globe_pos=a_pos_3+surface_extrusion+globe_elevation;vec3 mercator_elevation=u_up_dir*u_tile_up_scale*elevation(tilePos);vec3 merc_pos=mercator_tile_position(u_inv_rot_matrix,tilePos,u_tile_id,u_merc_center)+surface_extrusion+mercator_elevation;pos=mix_globe_mercator(globe_pos,merc_pos,u_zoom_transition);\n#else\npos=vec3(tilePos+extrude,elevation(tilePos));\n#endif\ngl_Position=u_matrix*vec4(pos,1);\n#ifdef FOG\nv_fog_pos=fog_position(pos);\n#endif\n}";
var heatmapTextureFrag = "uniform sampler2D u_image;uniform sampler2D u_color_ramp;uniform float u_opacity;in vec2 v_pos;void main() {float t=texture(u_image,v_pos).r;vec4 color=texture(u_color_ramp,vec2(t,0.5));glFragColor=color*u_opacity;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(0.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var heatmapTextureVert = "in vec2 a_pos;out vec2 v_pos;void main() {gl_Position=vec4(a_pos,0,1);v_pos=a_pos*0.5+0.5;}";
var collisionBoxFrag = "in float v_placed;in float v_notUsed;void main() {vec4 red =vec4(1.0,0.0,0.0,1.0);vec4 blue=vec4(0.0,0.0,1.0,0.5);glFragColor =mix(red,blue,step(0.5,v_placed))*0.5;glFragColor*=mix(1.0,0.1,step(0.5,v_notUsed));}";
var collisionBoxVert = "#include \"_prelude_terrain.vertex.glsl\"\nin vec3 a_pos;in vec2 a_anchor_pos;in vec2 a_extrude;in vec2 a_placed;in vec2 a_shift;in vec2 a_elevation_from_sea;in float a_size_scale;in vec2 a_padding;in float a_auto_z_offset;uniform mat4 u_matrix;uniform vec2 u_extrude_scale;uniform float u_camera_to_center_distance;\n#ifdef PROJECTION_GLOBE_VIEW\nuniform vec3 u_tile_id;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform float u_zoom_transition;\n#endif\nout float v_placed;out float v_notUsed;void main() {float feature_elevation=a_elevation_from_sea.x+a_auto_z_offset;float terrain_elevation=(a_elevation_from_sea.y==1.0 ? 0.0 : elevation(a_anchor_pos));vec3 proj_pos=a_pos+elevationVector(a_anchor_pos)*(feature_elevation+terrain_elevation);\n#ifdef PROJECTION_GLOBE_VIEW\n#ifndef PROJECTED_POS_ON_VIEWPORT\nvec3 globe_pos=proj_pos;vec3 mercator_pos=mercator_tile_position(u_inv_rot_matrix,a_anchor_pos,u_tile_id,u_merc_center);proj_pos=mix_globe_mercator(globe_pos,mercator_pos,u_zoom_transition);\n#endif\n#endif\nvec4 projectedPoint=u_matrix*vec4(proj_pos,1);highp float camera_to_anchor_distance=projectedPoint.w;highp float collision_perspective_ratio=clamp(\n0.5+0.5*(u_camera_to_center_distance/camera_to_anchor_distance),0.0,1.5);gl_Position=projectedPoint;gl_Position.xy+=(a_extrude*a_size_scale+a_shift+a_padding)*u_extrude_scale*gl_Position.w*collision_perspective_ratio;v_placed=a_placed.x;v_notUsed=a_placed.y;}";
var collisionCircleFrag = "in float v_radius;in vec2 v_extrude;in float v_perspective_ratio;in float v_collision;void main() {float alpha=0.5*min(v_perspective_ratio,1.0);float stroke_radius=0.9*max(v_perspective_ratio,1.0);float distance_to_center=length(v_extrude);float distance_to_edge=abs(distance_to_center-v_radius);float opacity_t=smoothstep(-stroke_radius,0.0,-distance_to_edge);vec4 color=mix(vec4(0.0,0.0,1.0,0.5),vec4(1.0,0.0,0.0,1.0),v_collision);glFragColor=color*alpha*opacity_t;}";
var collisionCircleVert = "in vec2 a_pos_2f;in float a_radius;in vec2 a_flags;uniform mat4 u_matrix;uniform mat4 u_inv_matrix;uniform vec2 u_viewport_size;uniform float u_camera_to_center_distance;out float v_radius;out vec2 v_extrude;out float v_perspective_ratio;out float v_collision;vec3 toTilePosition(vec2 screenPos) {vec4 rayStart=u_inv_matrix*vec4(screenPos,-1.0,1.0);vec4 rayEnd =u_inv_matrix*vec4(screenPos, 1.0,1.0);rayStart.xyz/=rayStart.w;rayEnd.xyz /=rayEnd.w;highp float t=(0.0-rayStart.z)/(rayEnd.z-rayStart.z);return mix(rayStart.xyz,rayEnd.xyz,t);}void main() {vec2 quadCenterPos=a_pos_2f;float radius=a_radius;float collision=a_flags.x;float vertexIdx=a_flags.y;vec2 quadVertexOffset=vec2(\nmix(-1.0,1.0,float(vertexIdx >=2.0)),mix(-1.0,1.0,float(vertexIdx >=1.0 && vertexIdx <=2.0)));vec2 quadVertexExtent=quadVertexOffset*radius;vec3 tilePos=toTilePosition(quadCenterPos);vec4 clipPos=u_matrix*vec4(tilePos,1.0);highp float camera_to_anchor_distance=clipPos.w;highp float collision_perspective_ratio=clamp(\n0.5+0.5*(u_camera_to_center_distance/camera_to_anchor_distance),0.0,4.0);float padding_factor=1.2;v_radius=radius;v_extrude=quadVertexExtent*padding_factor;v_perspective_ratio=collision_perspective_ratio;v_collision=collision;gl_Position=vec4(clipPos.xyz/clipPos.w,1.0)+vec4(quadVertexExtent*padding_factor/u_viewport_size*2.0,0.0,0.0);}";
var debugFrag = "uniform highp vec4 u_color;uniform sampler2D u_overlay;in vec2 v_uv;void main() {vec4 overlay_color=texture(u_overlay,v_uv);glFragColor=mix(u_color,overlay_color,overlay_color.a);}";
var debugVert = "#include \"_prelude_terrain.vertex.glsl\"\nin vec2 a_pos;\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_pos_3;\n#endif\nout vec2 v_uv;uniform mat4 u_matrix;uniform float u_overlay_scale;void main() {float h=elevation(a_pos);v_uv=a_pos/8192.0;\n#ifdef PROJECTION_GLOBE_VIEW\ngl_Position=u_matrix*vec4(a_pos_3+elevationVector(a_pos)*h,1);\n#else\ngl_Position=u_matrix*vec4(a_pos*u_overlay_scale,h,1);\n#endif\n}";
var fillFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define lowp float opacity\nuniform float u_emissive_strength;\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\n#ifdef ELEVATED_ROADS\nin highp float v_road_z_offset;\n#endif\n#ifdef INDICATOR_CUTOUT\nin highp float v_z_offset;\n#endif\nvoid main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize lowp float opacity\nvec4 out_color=color;\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);out_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nout_color*=opacity;\n#ifdef INDICATOR_CUTOUT\nif (v_z_offset >=0.0) {out_color=applyCutout(out_color,v_z_offset);}\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\nglFragColor=out_color;\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nin vec2 a_pos;\n#ifdef ELEVATED_ROADS\nin float a_road_z_offset;out highp float v_road_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\n#ifdef INDICATOR_CUTOUT\nout highp float v_z_offset;\n#endif\nuniform mat4 u_matrix;\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define highp float z_offset\nvoid main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize highp float z_offset\n#ifdef ELEVATED_ROADS\nz_offset+=a_road_z_offset;v_road_z_offset=z_offset;\n#endif\nfloat hidden=float(opacity==0.0);gl_Position=mix(u_matrix*vec4(a_pos,z_offset,1),AWAY,hidden);\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=vec3(a_pos,z_offset);vec3 shd_pos1=vec3(a_pos,z_offset);\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n#ifdef INDICATOR_CUTOUT\nv_z_offset=z_offset;\n#endif\n}";
var fillOutlineFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nin highp vec2 v_pos;uniform float u_emissive_strength;\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\n#pragma mapbox: define highp vec4 outline_color\n#pragma mapbox: define lowp float opacity\nvoid main() {\n#pragma mapbox: initialize highp vec4 outline_color\n#pragma mapbox: initialize lowp float opacity\nfloat dist=length(v_pos-gl_FragCoord.xy);float alpha=1.0-smoothstep(0.0,1.0,dist);vec4 out_color=outline_color;\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);out_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\nglFragColor=out_color*(alpha*opacity);\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillOutlineVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nin vec2 a_pos;\n#ifdef ELEVATED_ROADS\nin float a_road_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\nuniform mat4 u_matrix;uniform vec2 u_world;out highp vec2 v_pos;\n#pragma mapbox: define highp vec4 outline_color\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define highp float z_offset\nvoid main() {\n#pragma mapbox: initialize highp vec4 outline_color\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize highp float z_offset\n#ifdef ELEVATED_ROADS\nz_offset+=a_road_z_offset;\n#endif\nfloat hidden=float(opacity==0.0);gl_Position=mix(u_matrix*vec4(a_pos,z_offset,1),AWAY,hidden);\n#ifdef FLIP_Y\nv_pos=(vec2(gl_Position.x,-gl_Position.y)/gl_Position.w+1.0)/2.0*u_world;\n#else\nv_pos=(gl_Position.xy/gl_Position.w+1.0)/2.0*u_world;\n#endif\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=vec3(a_pos,z_offset);vec3 shd_pos1=vec3(a_pos,z_offset);\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var fillOutlinePatternFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nuniform vec2 u_texsize;uniform sampler2D u_image;\n#ifdef FILL_PATTERN_TRANSITION\nuniform float u_pattern_transition;\n#endif\nuniform float u_emissive_strength;\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\nin highp vec2 v_pos;in highp vec2 v_pos_world;\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\nvoid main() {\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;highp vec2 imagecoord=mod(v_pos,1.0);highp vec2 pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,imagecoord);highp vec2 lod_pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,v_pos);float dist=length(v_pos_world-gl_FragCoord.xy);float alpha=1.0-smoothstep(0.0,1.0,dist);vec4 out_color=textureLodCustom(u_image,pos,lod_pos);\n#ifdef APPLY_LUT_ON_GPU\nout_color=applyLUT(u_lutTexture,out_color);\n#endif\n#ifdef FILL_PATTERN_TRANSITION\nvec2 pattern_b_tl=pattern_b.xy;vec2 pattern_b_br=pattern_b.zw;highp vec2 pos_b=mix(pattern_b_tl/u_texsize,pattern_b_br/u_texsize,imagecoord);vec4 color_b=textureLodCustom(u_image,pos_b,lod_pos);out_color=out_color*(1.0-u_pattern_transition)+color_b*u_pattern_transition;\n#endif\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);out_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nglFragColor=out_color*(alpha*opacity);\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillOutlinePatternVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nuniform mat4 u_matrix;uniform vec2 u_world;uniform vec2 u_pixel_coord_upper;uniform vec2 u_pixel_coord_lower;uniform float u_tile_units_to_pixels;in vec2 a_pos;\n#ifdef ELEVATED_ROADS\nin float a_road_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\nout highp vec2 v_pos;out highp vec2 v_pos_world;\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define lowp float pixel_ratio\n#pragma mapbox: define highp float z_offset\nvoid main() {\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\n#pragma mapbox: initialize lowp float pixel_ratio\n#pragma mapbox: initialize highp float z_offset\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;\n#ifdef ELEVATED_ROADS\nz_offset+=a_road_z_offset;\n#endif\nfloat hidden=float(opacity==0.0);gl_Position=mix(u_matrix*vec4(a_pos,z_offset,1),AWAY,hidden);vec2 display_size=(pattern_br-pattern_tl)/pixel_ratio;v_pos=get_pattern_pos(u_pixel_coord_upper,u_pixel_coord_lower,display_size,u_tile_units_to_pixels,a_pos);\n#ifdef FLIP_Y\nv_pos_world=(vec2(gl_Position.x,-gl_Position.y)/gl_Position.w+1.0)/2.0*u_world;\n#else\nv_pos_world=(gl_Position.xy/gl_Position.w+1.0)/2.0*u_world;\n#endif\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=vec3(a_pos,z_offset);vec3 shd_pos1=vec3(a_pos,z_offset);\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var fillPatternFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nuniform vec2 u_texsize;uniform sampler2D u_image;\n#ifdef FILL_PATTERN_TRANSITION\nuniform float u_pattern_transition;\n#endif\nin highp vec2 v_pos;uniform float u_emissive_strength;\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\n#ifdef ELEVATED_ROADS\nin highp float v_road_z_offset;\n#endif\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\nvoid main() {\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;highp vec2 imagecoord=mod(v_pos,1.0);highp vec2 pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,imagecoord);highp vec2 lod_pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,v_pos);vec4 out_color=textureLodCustom(u_image,pos,lod_pos);\n#ifdef APPLY_LUT_ON_GPU\nout_color=applyLUT(u_lutTexture,out_color);\n#endif\n#ifdef FILL_PATTERN_TRANSITION\nvec2 pattern_b_tl=pattern_b.xy;vec2 pattern_b_br=pattern_b.zw;highp vec2 pos_b=mix(pattern_b_tl/u_texsize,pattern_b_br/u_texsize,imagecoord);vec4 color_b=textureLodCustom(u_image,pos_b,lod_pos);out_color=out_color*(1.0-u_pattern_transition)+color_b*u_pattern_transition;\n#endif\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,u_emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);\n#ifdef ELEVATED_ROADS\nout_color.rgb*=mix(v_road_z_offset !=0.0 ? u_ground_shadow_factor : vec3(1.0),vec3(1.0),light);\n#else\nout_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nglFragColor=out_color*opacity;\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillPatternVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nuniform mat4 u_matrix;uniform vec2 u_pixel_coord_upper;uniform vec2 u_pixel_coord_lower;uniform float u_tile_units_to_pixels;in vec2 a_pos;\n#ifdef ELEVATED_ROADS\nin float a_road_z_offset;out highp float v_road_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\nout highp vec2 v_pos;\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp vec4 pattern\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define lowp float pixel_ratio\n#pragma mapbox: define highp float z_offset\nvoid main() {\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump vec4 pattern\n#pragma mapbox: initialize lowp float pixel_ratio\n#pragma mapbox: initialize highp float z_offset\n#ifdef FILL_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;vec2 display_size=(pattern_br-pattern_tl)/pixel_ratio;\n#ifdef ELEVATED_ROADS\nz_offset+=a_road_z_offset;v_road_z_offset=z_offset;\n#endif\nfloat hidden=float(opacity==0.0);gl_Position=mix(u_matrix*vec4(a_pos,z_offset,1),AWAY,hidden);v_pos=get_pattern_pos(u_pixel_coord_upper,u_pixel_coord_lower,display_size,u_tile_units_to_pixels,a_pos);\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=vec3(a_pos,z_offset);vec3 shd_pos1=vec3(a_pos,z_offset);\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var fillExtrusionFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nin vec4 v_color;in vec4 v_flat;\n#ifdef RENDER_SHADOWS\nin highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;\n#endif\nuniform lowp float u_opacity;\n#ifdef FAUX_AO\nuniform lowp vec2 u_ao;in vec2 v_ao;\n#endif\n#if defined(ZERO_ROOF_RADIUS) && !defined(LIGHTING_3D_MODE)\nin vec4 v_roof_color;\n#endif\n#if defined(ZERO_ROOF_RADIUS) || defined(RENDER_SHADOWS) || defined(LIGHTING_3D_MODE)\nin highp vec3 v_normal;\n#endif\nuniform vec3 u_flood_light_color;uniform highp float u_vertical_scale;uniform float u_flood_light_intensity;uniform vec3 u_ground_shadow_factor;\n#if defined(LIGHTING_3D_MODE) && defined(FLOOD_LIGHT)\nin float v_flood_radius;in float v_has_floodlight;\n#endif\nin float v_height;\n#pragma mapbox: define highp float emissive_strength\nvoid main() {\n#pragma mapbox: initialize highp float emissive_strength\n#if defined(ZERO_ROOF_RADIUS) || defined(RENDER_SHADOWS) || defined(LIGHTING_3D_MODE)\nvec3 normal=normalize(v_normal);\n#endif\nfloat z;vec4 color=v_color;\n#ifdef ZERO_ROOF_RADIUS\nz=float(normal.z > 0.00001);\n#ifdef LIGHTING_3D_MODE\nnormal=mix(normal,vec3(0.0,0.0,1.0),z);\n#else\ncolor=mix(v_color,v_roof_color,z);\n#endif\n#endif\nfloat h=max(0.0,v_height);float ao_shade=1.0;\n#ifdef FAUX_AO\nfloat intensity=u_ao[0];float h_floors=h/(u_ao[1]*u_vertical_scale);float y_shade=1.0-0.9*intensity*min(v_ao.y,1.0);ao_shade=(1.0-0.08*intensity)*(y_shade+(1.0-y_shade)*(1.0-pow(1.0-min(h_floors/16.0,1.0),16.0)))+0.08*intensity*min(h_floors/160.0,1.0);float concave=v_ao.x*v_ao.x;\n#ifdef ZERO_ROOF_RADIUS\nconcave*=(1.0-z);\n#endif\nfloat x_shade=mix(1.0,mix(0.6,0.75,min(h_floors/30.0,1.0)),intensity)+0.1*intensity*min(h,1.0);ao_shade*=mix(1.0,x_shade*x_shade*x_shade,concave);\n#ifdef LIGHTING_3D_MODE\n#ifdef FLOOD_LIGHT\ncolor.rgb*=mix(ao_shade,1.0,v_has_floodlight);\n#else\ncolor.rgb*=ao_shade;\n#endif\n#else\ncolor.rgb*=ao_shade;\n#endif\n#endif\n#ifdef LIGHTING_3D_MODE\nfloat flood_radiance=0.0;\n#ifdef FLOOD_LIGHT\nflood_radiance=(1.0-min(h/v_flood_radius,1.0))*u_flood_light_intensity*v_has_floodlight;\n#endif\n#ifdef RENDER_SHADOWS\n#ifdef FLOOD_LIGHT\nfloat ndotl_unclamped=dot(normal,u_shadow_direction);float ndotl=max(0.0,ndotl_unclamped);float occlusion=ndotl_unclamped < 0.0 ? 1.0 : shadow_occlusion(ndotl,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w);vec3 litColor=apply_lighting(color.rgb,normal,(1.0-u_shadow_intensity*occlusion)*ndotl);vec3 floodLitColor=compute_flood_lighting(u_flood_light_color*u_opacity,1.0-u_shadow_intensity,occlusion,u_ground_shadow_factor);color.rgb=mix(litColor,floodLitColor,flood_radiance);\n#else\nfloat shadowed_lighting_factor;\n#ifdef RENDER_CUTOFF\nshadowed_lighting_factor=shadowed_light_factor_normal_opacity(normal,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w,v_cutoff_opacity);if (v_cutoff_opacity==0.0) {discard;}\n#else\nshadowed_lighting_factor=shadowed_light_factor_normal(normal,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w);\n#endif\ncolor.rgb=apply_lighting(color.rgb,normal,shadowed_lighting_factor);\n#endif\n#else\ncolor.rgb=apply_lighting(color.rgb,normal);\n#ifdef FLOOD_LIGHT\ncolor.rgb=mix(color.rgb,u_flood_light_color*u_opacity,flood_radiance);\n#endif\n#endif\ncolor.rgb=mix(color.rgb,v_flat.rgb,emissive_strength);color*=u_opacity;\n#endif\n#ifdef FOG\ncolor=fog_dither(fog_apply_premultiplied(color,v_fog_pos,h));\n#endif\n#ifdef INDICATOR_CUTOUT\ncolor=applyCutout(color,h);\n#endif\n#ifdef FEATURE_CUTOUT\ncolor=apply_feature_cutout(color,gl_FragCoord);\n#endif\nglFragColor=color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillExtrusionVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_material_table.vertex.glsl\"\nuniform mat4 u_matrix;uniform vec3 u_lightcolor;uniform lowp vec3 u_lightpos;uniform lowp float u_lightintensity;uniform float u_vertical_gradient;uniform lowp float u_opacity;uniform float u_edge_radius;uniform float u_width_scale;in vec4 a_pos_normal_ed;in vec2 a_centroid_pos;\n#ifdef RENDER_WALL_MODE\nin vec3 a_join_normal_inside;\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_pos_3;in vec3 a_pos_normal_3;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform vec3 u_tile_id;uniform float u_zoom_transition;uniform vec3 u_up_dir;uniform float u_height_lift;\n#endif\n#ifdef TERRAIN\nuniform int u_height_type;uniform int u_base_type;\n#endif\nuniform highp float u_vertical_scale;out vec4 v_color;out vec4 v_flat;\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;\n#endif\n#if defined(ZERO_ROOF_RADIUS) && !defined(LIGHTING_3D_MODE)\nout vec4 v_roof_color;\n#endif\n#if defined(ZERO_ROOF_RADIUS) || defined(RENDER_SHADOWS) || defined(LIGHTING_3D_MODE)\nout highp vec3 v_normal;\n#endif\n#ifdef FAUX_AO\nuniform lowp vec2 u_ao;out vec2 v_ao;\n#endif\n#if defined(LIGHTING_3D_MODE) && defined(FLOOD_LIGHT)\nout float v_flood_radius;out float v_has_floodlight;\n#endif\nout float v_height;vec3 linearTosRGB(vec3 color) {return pow(color,vec3(1./2.2));}vec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}\n#pragma mapbox: define highp float base\n#pragma mapbox: define highp float height\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define highp float flood_light_wall_radius\n#pragma mapbox: define highp float line_width\n#pragma mapbox: define highp float emissive_strength\nvoid main() {DECLARE_MATERIAL_TABLE_INFO\n#pragma mapbox: initialize highp float base\n#pragma mapbox: initialize highp float height\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize highp float flood_light_wall_radius\n#pragma mapbox: initialize highp float line_width\n#pragma mapbox: initialize highp float emissive_strength\nbase*=u_vertical_scale;height*=u_vertical_scale;vec4 pos_nx=floor(a_pos_normal_ed*0.5);vec4 top_up_ny_start=a_pos_normal_ed-2.0*pos_nx;vec3 top_up_ny=top_up_ny_start.xyz;float x_normal=pos_nx.z/8192.0;vec3 normal=top_up_ny.y==1.0 ? vec3(0.0,0.0,1.0) : normalize(vec3(x_normal,(2.0*top_up_ny.z-1.0)*(1.0-abs(x_normal)),0.0));\n#if defined(ZERO_ROOF_RADIUS) || defined(RENDER_SHADOWS) || defined(LIGHTING_3D_MODE)\nv_normal=normal;\n#endif\nbase=max(0.0,base);float attr_height=height;height=max(0.0,top_up_ny.y==0.0 && top_up_ny.x==1.0 ? height-u_edge_radius : height);float t=top_up_ny.x;vec2 centroid_pos=vec2(0.0);\n#if defined(HAS_CENTROID) || defined(TERRAIN)\ncentroid_pos=a_centroid_pos;\n#endif\nfloat ele=0.0;float h=0.0;float c_ele=0.0;vec3 pos;\n#ifdef TERRAIN\nbool is_flat_height=centroid_pos.x !=0.0 && u_height_type==1;bool is_flat_base=centroid_pos.x !=0.0 && u_base_type==1;ele=elevation(pos_nx.xy);c_ele=is_flat_height || is_flat_base ? (centroid_pos.y==0.0 ? elevationFromUint16(centroid_pos.x) : flatElevation(centroid_pos)) : ele;float h_height=is_flat_height ? max(c_ele+height,ele+base+2.0) : ele+height;float h_base=is_flat_base ? max(c_ele+base,ele+base) : ele+(base==0.0 ?-5.0 : base);h=t > 0.0 ? max(h_base,h_height) : h_base;pos=vec3(pos_nx.xy,h);\n#else\nh=t > 0.0 ? height : base;pos=vec3(pos_nx.xy,h);\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nfloat lift=float((t+base) > 0.0)*u_height_lift;h+=lift;vec3 globe_normal=normalize(mix(a_pos_normal_3/16384.0,u_up_dir,u_zoom_transition));vec3 globe_pos=a_pos_3+globe_normal*(u_tile_up_scale*h);vec3 merc_pos=mercator_tile_position(u_inv_rot_matrix,pos.xy,u_tile_id,u_merc_center)+u_up_dir*u_tile_up_scale*pos.z;pos=mix_globe_mercator(globe_pos,merc_pos,u_zoom_transition);\n#endif\nfloat cutoff=1.0;vec3 scaled_pos=pos;\n#ifdef RENDER_CUTOFF\nvec3 centroid_random=vec3(centroid_pos.xy,centroid_pos.x+centroid_pos.y+1.0);vec3 ground_pos=centroid_pos.x==0.0 ? pos.xyz : (centroid_random/8.0);vec4 ground=u_matrix*vec4(ground_pos.xy,ele,1.0);\n#ifdef CLIP_ZERO_TO_ONE\ncutoff=cutoff_opacity(u_cutoff_params,ground.z*2.0-ground.w);\n#else\ncutoff=cutoff_opacity(u_cutoff_params,ground.z);\n#endif\nif (centroid_pos.y !=0.0 && centroid_pos.x !=0.0) {vec3 g=floor(ground_pos);vec3 mod_=centroid_random-g*8.0;float seed=min(1.0,0.1*(min(3.5,max(mod_.x+mod_.y,0.2*attr_height))*0.35+mod_.z));if (cutoff < 0.8-seed) {cutoff=0.0;}}float cutoff_scale=cutoff;v_cutoff_opacity=cutoff;scaled_pos.z=mix(c_ele,h,cutoff_scale);\n#endif\nfloat hidden=float((centroid_pos.x==0.0 && centroid_pos.y==1.0) || (cutoff==0.0 && centroid_pos.x !=0.0) || (color.a==0.0));\n#ifdef RENDER_WALL_MODE\nvec2 wall_offset=u_width_scale*line_width*(a_join_normal_inside.xy/EXTENT);scaled_pos.xy+=(1.0-a_join_normal_inside.z)*wall_offset*0.5;scaled_pos.xy-=a_join_normal_inside.z*wall_offset*0.5;\n#endif\ngl_Position=mix(u_matrix*vec4(scaled_pos,1),AWAY,hidden);h=h-ele;v_height=h;\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=pos;vec3 shd_pos1=pos;\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(normal);shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);\n#endif\nfloat NdotL=0.0;float colorvalue=0.0;\n#ifndef LIGHTING_3D_MODE\ncolorvalue=color.r*0.2126+color.g*0.7152+color.b*0.0722;vec4 ambientlight=vec4(0.03,0.03,0.03,1.0);color+=ambientlight;NdotL=clamp(dot(normal,u_lightpos),0.0,1.0);NdotL=mix((1.0-u_lightintensity),max((1.0-colorvalue+u_lightintensity),1.0),NdotL);if (normal.y !=0.0) {float r=0.84;r=mix(0.7,0.98,1.0-u_lightintensity);NdotL*=(\n(1.0-u_vertical_gradient)+(u_vertical_gradient*clamp((t+base)*pow(height/150.0,0.5),r,1.0)));}\n#endif\n#ifdef FAUX_AO\nfloat concave=pos_nx.w-floor(pos_nx.w*0.5)*2.0;float start=top_up_ny_start.w;float y_ground=1.0-clamp(t+base,0.0,1.0);float top_height=height;\n#ifdef TERRAIN\ntop_height=mix(max(c_ele+height,ele+base+2.0),ele+height,float(centroid_pos.x==0.0))-ele;y_ground+=y_ground*5.0/max(3.0,top_height);\n#endif\nv_ao=vec2(mix(concave,-concave,start),y_ground);NdotL*=(1.0+0.05*(1.0-top_up_ny.y)*u_ao[0]);\n#ifdef PROJECTION_GLOBE_VIEW\ntop_height+=u_height_lift;\n#endif\ngl_Position.z-=(0.0000006*(min(top_height,500.)+2.0*min(base,500.0)+60.0*concave+3.0*start))*gl_Position.w;\n#endif\n#ifdef LIGHTING_3D_MODE\n#ifdef FLOOD_LIGHT\nfloat is_wall=1.0-float(t > 0.0 && top_up_ny.y > 0.0);v_has_floodlight=float(flood_light_wall_radius > 0.0 && is_wall > 0.0);v_flood_radius=flood_light_wall_radius*u_vertical_scale;\n#endif\nv_color=vec4(color.rgb,1.0);float ndotl=calculate_NdotL(normal);v_flat.rgb=sRGBToLinear(color.rgb);v_flat.rgb=v_flat.rgb*(ndotl+(1.0-min(ndotl*57.29,1.0))*emissive_strength);v_flat=vec4(linearTosRGB(v_flat.rgb),1.0);\n#else\nv_color=vec4(0.0,0.0,0.0,1.0);v_color.rgb+=clamp(color.rgb*NdotL*u_lightcolor,mix(vec3(0.0),vec3(0.3),1.0-u_lightcolor),vec3(1.0));v_color*=u_opacity;\n#endif\n#if defined(ZERO_ROOF_RADIUS) && !defined(LIGHTING_3D_MODE)\nfloat roofNdotL=clamp(u_lightpos.z,0.0,1.0);roofNdotL=mix((1.0-u_lightintensity),max((1.0-colorvalue+u_lightintensity),1.0),roofNdotL);v_roof_color=vec4(0.0,0.0,0.0,1.0);v_roof_color.rgb+=clamp(color.rgb*roofNdotL*u_lightcolor,mix(vec3(0.0),vec3(0.3),1.0-u_lightcolor),vec3(1.0));v_roof_color*=u_opacity;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(pos);\n#endif\n}";
var fillExtrusionPatternFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform vec2 u_texsize;uniform sampler2D u_image;\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\nuniform float u_pattern_transition;\n#endif\n#ifdef FAUX_AO\nuniform lowp vec2 u_ao;in vec3 v_ao;\n#endif\n#ifdef LIGHTING_3D_MODE\nin vec3 v_normal;\n#endif\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\nin highp vec2 v_pos;in vec4 v_lighting;uniform lowp float u_opacity;\n#pragma mapbox: define highp float base\n#pragma mapbox: define highp float height\n#pragma mapbox: define mediump vec4 pattern\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define highp float pixel_ratio\nvoid main() {\n#pragma mapbox: initialize highp float base\n#pragma mapbox: initialize highp float height\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\n#pragma mapbox: initialize highp float pixel_ratio\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;highp vec2 imagecoord=mod(v_pos,1.0);highp vec2 pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,imagecoord);highp vec2 lod_pos=mix(pattern_tl/u_texsize,pattern_br/u_texsize,v_pos);vec4 out_color=textureLodCustom(u_image,pos,lod_pos);\n#ifdef APPLY_LUT_ON_GPU\nout_color=applyLUT(u_lutTexture,out_color);\n#endif\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\nvec2 pattern_b_tl=pattern_b.xy;vec2 pattern_b_br=pattern_b.zw;highp vec2 pos_b=mix(pattern_b_tl/u_texsize,pattern_b_br/u_texsize,imagecoord);vec4 color_b=textureLodCustom(u_image,pos_b,lod_pos);out_color=out_color*(1.0-u_pattern_transition)+color_b*u_pattern_transition;\n#endif\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting(out_color,normalize(v_normal))*u_opacity;\n#else\nout_color=out_color*v_lighting;\n#endif\n#ifdef FAUX_AO\nfloat intensity=u_ao[0];float h=max(0.0,v_ao.z);float h_floors=h/u_ao[1];float y_shade=1.0-0.9*intensity*min(v_ao.y,1.0);float shade=(1.0-0.08*intensity)*(y_shade+(1.0-y_shade)*(1.0-pow(1.0-min(h_floors/16.0,1.0),16.0)))+0.08*intensity*min(h_floors/160.0,1.0);float concave=v_ao.x*v_ao.x;float x_shade=mix(1.0,mix(0.6,0.75,min(h_floors/30.0,1.0)),intensity)+0.1*intensity*min(h,1.0);shade*=mix(1.0,x_shade*x_shade*x_shade,concave);out_color.rgb=out_color.rgb*shade;\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\n#ifdef INDICATOR_CUTOUT\nout_color=applyCutout(out_color,height);\n#endif\nglFragColor=out_color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var fillExtrusionPatternVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_material_table.vertex.glsl\"\nuniform mat4 u_matrix;uniform vec2 u_pixel_coord_upper;uniform vec2 u_pixel_coord_lower;uniform float u_height_factor;uniform float u_tile_units_to_pixels;uniform float u_vertical_gradient;uniform lowp float u_opacity;uniform float u_width_scale;uniform vec3 u_lightcolor;uniform lowp vec3 u_lightpos;uniform lowp float u_lightintensity;in vec4 a_pos_normal_ed;in vec2 a_centroid_pos;\n#ifdef RENDER_WALL_MODE\nin vec3 a_join_normal_inside;\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_pos_3;in vec3 a_pos_normal_3;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform vec3 u_tile_id;uniform float u_zoom_transition;uniform vec3 u_up_dir;uniform float u_height_lift;\n#endif\n#ifdef TERRAIN\nuniform int u_height_type;uniform int u_base_type;\n#endif\nout highp vec2 v_pos;out vec4 v_lighting;\n#ifdef FAUX_AO\nuniform lowp vec2 u_ao;out vec3 v_ao;\n#endif\n#ifdef LIGHTING_3D_MODE\nout vec3 v_normal;\n#endif\n#pragma mapbox: define highp float base\n#pragma mapbox: define highp float height\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define mediump vec4 pattern\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define highp float pixel_ratio\n#pragma mapbox: define highp float line_width\nvoid main() {DECLARE_MATERIAL_TABLE_INFO\n#pragma mapbox: initialize highp float base\n#pragma mapbox: initialize highp float height\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef FILL_EXTRUSION_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\n#pragma mapbox: initialize highp float pixel_ratio\n#pragma mapbox: initialize highp float line_width\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;vec4 pos_nx=floor(a_pos_normal_ed*0.5);mediump vec4 top_up_ny_start=a_pos_normal_ed-2.0*pos_nx;mediump vec3 top_up_ny=top_up_ny_start.xyz;float x_normal=pos_nx.z/8192.0;vec3 normal=top_up_ny.y==1.0 ? vec3(0.0,0.0,1.0) : normalize(vec3(x_normal,(2.0*top_up_ny.z-1.0)*(1.0-abs(x_normal)),0.0));float edgedistance=a_pos_normal_ed.w;vec2 display_size=(pattern_br-pattern_tl)/pixel_ratio;base=max(0.0,base);height=max(0.0,height);float t=top_up_ny.x;float z=t > 0.0 ? height : base;vec2 centroid_pos=vec2(0.0);\n#if defined(HAS_CENTROID) || defined(TERRAIN)\ncentroid_pos=a_centroid_pos;\n#endif\nfloat ele=0.0;float h=z;vec3 p;float c_ele;\n#ifdef TERRAIN\nbool is_flat_height=centroid_pos.x !=0.0 && u_height_type==1;bool is_flat_base=centroid_pos.x !=0.0 && u_base_type==1;ele=elevation(pos_nx.xy);c_ele=is_flat_height || is_flat_base ? (centroid_pos.y==0.0 ? elevationFromUint16(centroid_pos.x) : flatElevation(centroid_pos)) : ele;float h_height=is_flat_height ? max(c_ele+height,ele+base+2.0) : ele+height;float h_base=is_flat_base ? max(c_ele+base,ele+base) : ele+(base==0.0 ?-5.0 : base);h=t > 0.0 ? max(h_base,h_height) : h_base;p=vec3(pos_nx.xy,h);\n#else\np=vec3(pos_nx.xy,z);\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nfloat lift=float((t+base) > 0.0)*u_height_lift;h+=lift;vec3 globe_normal=normalize(mix(a_pos_normal_3/16384.0,u_up_dir,u_zoom_transition));vec3 globe_pos=a_pos_3+globe_normal*(u_tile_up_scale*(p.z+lift));vec3 merc_pos=mercator_tile_position(u_inv_rot_matrix,p.xy,u_tile_id,u_merc_center)+u_up_dir*u_tile_up_scale*p.z;p=mix_globe_mercator(globe_pos,merc_pos,u_zoom_transition);\n#endif\n#ifdef RENDER_WALL_MODE\nvec2 wall_offset=u_width_scale*line_width*(a_join_normal_inside.xy/EXTENT);p.xy+=(1.0-a_join_normal_inside.z)*wall_offset*0.5;p.xy-=a_join_normal_inside.z*wall_offset*0.5;\n#endif\nfloat hidden=float((centroid_pos.x==0.0 && centroid_pos.y==1.0) || (color.a==0.0));gl_Position=mix(u_matrix*vec4(p,1),AWAY,hidden);vec2 pos=normal.z==1.0\n? pos_nx.xy\n: vec2(edgedistance,z*u_height_factor);v_pos=get_pattern_pos(u_pixel_coord_upper,u_pixel_coord_lower,display_size,u_tile_units_to_pixels,pos);v_lighting=vec4(0.0,0.0,0.0,1.0);float NdotL=0.0;\n#ifdef LIGHTING_3D_MODE\nNdotL=calculate_NdotL(normal);\n#else\nNdotL=clamp(dot(normal,u_lightpos),0.0,1.0);NdotL=mix((1.0-u_lightintensity),max((0.5+u_lightintensity),1.0),NdotL);\n#endif\nif (normal.y !=0.0) {float r=0.84;\n#ifndef LIGHTING_3D_MODE\nr=mix(0.7,0.98,1.0-u_lightintensity);\n#endif\nNdotL*=(\n(1.0-u_vertical_gradient)+(u_vertical_gradient*clamp((t+base)*pow(height/150.0,0.5),r,1.0)));}\n#ifdef FAUX_AO\nfloat concave=pos_nx.w-floor(pos_nx.w*0.5)*2.0;float start=top_up_ny_start.w;float y_ground=1.0-clamp(t+base,0.0,1.0);float top_height=height;\n#ifdef TERRAIN\ntop_height=mix(max(c_ele+height,ele+base+2.0),ele+height,float(centroid_pos.x==0.0))-ele;y_ground+=y_ground*5.0/max(3.0,top_height);\n#endif\nv_ao=vec3(mix(concave,-concave,start),y_ground,h-ele);NdotL*=(1.0+0.05*(1.0-top_up_ny.y)*u_ao[0]);\n#ifdef PROJECTION_GLOBE_VIEW\ntop_height+=u_height_lift;\n#endif\ngl_Position.z-=(0.0000006*(min(top_height,500.)+2.0*min(base,500.0)+60.0*concave+3.0*start))*gl_Position.w;\n#endif\n#ifdef LIGHTING_3D_MODE\nv_normal=normal;\n#else\nv_lighting.rgb+=clamp(NdotL*u_lightcolor,mix(vec3(0.0),vec3(0.3),1.0-u_lightcolor),vec3(1.0));v_lighting*=u_opacity;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(p);\n#endif\n}";
var hillshadePrepareFrag = "precision highp float;uniform highp sampler2D u_image;in vec2 v_pos;uniform vec2 u_dimension;uniform float u_zoom;float getElevation(vec2 coord) {return texture(u_image,coord).r/4.0;}void main() {vec2 epsilon=1.0/u_dimension;float a=getElevation(v_pos+vec2(-epsilon.x,-epsilon.y));float b=getElevation(v_pos+vec2(0,-epsilon.y));float c=getElevation(v_pos+vec2(epsilon.x,-epsilon.y));float d=getElevation(v_pos+vec2(-epsilon.x,0));float e=getElevation(v_pos+vec2(epsilon.x,0));float f=getElevation(v_pos+vec2(-epsilon.x,epsilon.y));float g=getElevation(v_pos+vec2(0,epsilon.y));float h=getElevation(v_pos+vec2(epsilon.x,epsilon.y));float exaggerationFactor=u_zoom < 2.0 ? 0.4 : u_zoom < 4.5 ? 0.35 : 0.3;float exaggeration=u_zoom < 15.0 ? (u_zoom-15.0)*exaggerationFactor : 0.0;vec2 deriv=vec2(\n(c+e+e+h)-(a+d+d+f),(f+g+g+h)-(a+b+b+c)\n)/pow(2.0,exaggeration+(19.2562-u_zoom));glFragColor=clamp(vec4(\nderiv.x/2.0+0.5,deriv.y/2.0+0.5,1.0,1.0),0.0,1.0);}";
var fillExtrusionGroundEffectFrag = "uniform highp float u_ao_pass;uniform highp float u_opacity;uniform highp float u_flood_light_intensity;uniform highp vec3 u_flood_light_color;uniform highp float u_attenuation;uniform sampler2D u_fb;uniform float u_fb_size;\n#ifdef SDF_SUBPASS\nin highp vec2 v_pos;in highp vec4 v_line_segment;in highp float v_flood_light_radius_tile;in highp vec2 v_ao;float line_df(highp vec2 a,highp vec2 b,highp vec2 p) {highp vec2 ba=b-a;highp vec2 pa=p-a;highp float r=clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);return length(pa-r*ba);}\n#ifdef FOG\nin highp float v_fog;\n#endif\n#endif\nvoid main() {\n#ifdef CLEAR_SUBPASS\nvec4 color=vec4(1.0);\n#ifdef CLEAR_FROM_TEXTURE\ncolor=texture(u_fb,gl_FragCoord.xy/vec2(u_fb_size));\n#endif\nglFragColor=color;\n#else\n#ifdef SDF_SUBPASS\nhighp float d=line_df(v_line_segment.xy,v_line_segment.zw,v_pos);highp float effect_radius=mix(v_flood_light_radius_tile,v_ao.y,u_ao_pass);d/=effect_radius;d=min(d,1.0);d=1.0-pow(1.0-d,u_attenuation);highp float effect_intensity=mix(u_flood_light_intensity,v_ao.x,u_ao_pass);highp float fog=1.0;\n#ifdef FOG\nfog=v_fog;\n#endif\n#ifdef RENDER_CUTOFF\nfog*=v_cutoff_opacity;\n#endif\nglFragColor=vec4(vec3(0.0),mix(1.0,d,effect_intensity*u_opacity*fog));\n#else\n#ifdef USE_MRT1\nout_Target1=vec4(1.0-texture(u_fb,gl_FragCoord.xy/vec2(u_fb_size)).a,0.0,0.0,0.0);\n#else\nvec4 color=mix(vec4(u_flood_light_color,1.0),vec4(vec3(0.0),1.0),u_ao_pass);\n#ifdef OVERDRAW_INSPECTOR\ncolor=vec4(1.0);\n#endif\nglFragColor=color;\n#endif\n#endif\nHANDLE_WIREFRAME_DEBUG;\n#endif\n}";
var fillExtrusionGroundEffectVert = "#include \"_prelude_fog.vertex.glsl\"\nin highp vec4 a_pos_end;in highp float a_angular_offset_factor;in highp float a_hidden_by_landmark;\n#ifdef SDF_SUBPASS\nout highp vec2 v_pos;out highp vec4 v_line_segment;out highp float v_flood_light_radius_tile;out highp vec2 v_ao;\n#ifdef FOG\nout highp float v_fog;\n#endif\n#endif\nuniform highp float u_flood_light_intensity;uniform highp mat4 u_matrix;uniform highp float u_ao_pass;uniform highp float u_meter_to_tile;uniform highp float u_edge_radius;uniform highp float u_dynamic_offset;uniform highp vec2 u_ao;\n#pragma mapbox: define highp float flood_light_ground_radius\nconst float TANGENT_CUTOFF=4.0;const float NORM=32767.0;void main() {\n#pragma mapbox: initialize highp float flood_light_ground_radius\nvec2 p=a_pos_end.xy;vec2 q=floor(a_pos_end.zw*0.5);vec2 start_bottom=a_pos_end.zw-q*2.0;float fl_ground_radius=flood_light_ground_radius;fl_ground_radius=abs(flood_light_ground_radius);float direction=flood_light_ground_radius < 0.0 ?-1.0 : 1.0;float flood_radius_tile=fl_ground_radius*u_meter_to_tile;vec2 v=normalize(q-p);float ao_radius=u_ao.y/3.5;float effect_radius=mix(flood_radius_tile,ao_radius,u_ao_pass)+u_edge_radius;float angular_offset_factor=a_angular_offset_factor/NORM*TANGENT_CUTOFF;float angular_offset=direction*angular_offset_factor*effect_radius;float top=1.0-start_bottom.y;float side=(0.5-start_bottom.x)*2.0;vec2 extrusion_parallel=v*side*mix(u_dynamic_offset,angular_offset,top);vec2 perp=vec2(v.y,-v.x);vec2 extrusion_perp=direction*perp*effect_radius*top;vec3 pos=vec3(mix(q,p,start_bottom.x),0.0);pos.xy+=extrusion_parallel+extrusion_perp;\n#ifdef SDF_SUBPASS\nv_pos=pos.xy;v_line_segment=vec4(p,q)+perp.xyxy*u_edge_radius;v_flood_light_radius_tile=flood_radius_tile;v_ao=vec2(u_ao.x,ao_radius);\n#ifdef FOG\nv_fog_pos=fog_position(pos);v_fog=1.0-fog(v_fog_pos);\n#endif\n#endif\nfloat hidden_by_landmark=0.0;\n#ifdef HAS_CENTROID\nhidden_by_landmark=a_hidden_by_landmark;\n#endif\nfloat isFloodlit=float(fl_ground_radius > 0.0 && u_flood_light_intensity > 0.0);float hidden=mix(1.0-isFloodlit,isFloodlit,u_ao_pass);hidden+=hidden_by_landmark;gl_Position=mix(u_matrix*vec4(pos,1.0),AWAY,float(hidden > 0.0));\n#ifdef RENDER_CUTOFF\nv_cutoff_opacity=cutoff_opacity(u_cutoff_params,gl_Position.z);\n#endif\n}";
var hillshadePrepareVert = "uniform mat4 u_matrix;uniform vec2 u_dimension;in vec2 a_pos;in vec2 a_texture_pos;out vec2 v_pos;void main() {gl_Position=u_matrix*vec4(a_pos,0,1);highp vec2 epsilon=1.0/u_dimension;float scale=(u_dimension.x-2.0)/u_dimension.x;v_pos=(a_texture_pos/8192.0)*scale+epsilon;}";
var hillshadeFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform sampler2D u_image;in vec2 v_pos;uniform vec2 u_latrange;uniform vec2 u_light;uniform vec4 u_shadow;uniform vec4 u_highlight;uniform vec4 u_accent;uniform float u_emissive_strength;void main() {vec4 pixel=texture(u_image,v_pos);vec2 deriv=((pixel.rg*2.0)-1.0);float scaleFactor=cos(radians((u_latrange[0]-u_latrange[1])*(1.0-v_pos.y)+u_latrange[1]));float slope=atan(1.25*length(deriv)/scaleFactor);float aspect=deriv.x !=0.0 ? atan(deriv.y,-deriv.x) : PI/2.0*(deriv.y > 0.0 ? 1.0 :-1.0);float intensity=u_light.x;float azimuth=u_light.y+PI;float base=1.875-intensity*1.75;float maxValue=0.5*PI;float scaledSlope=intensity !=0.5 ? ((pow(base,slope)-1.0)/(pow(base,maxValue)-1.0))*maxValue : slope;float accent=cos(scaledSlope);vec4 accent_color=(1.0-accent)*u_accent*clamp(intensity*2.0,0.0,1.0);float shade=abs(mod((aspect+azimuth)/PI+0.5,2.0)-1.0);vec4 shade_color=mix(u_shadow,u_highlight,shade)*sin(scaledSlope)*clamp(intensity*2.0,0.0,1.0);glFragColor=accent_color*(1.0-shade_color.a)+shade_color;\n#ifdef LIGHTING_3D_MODE\nglFragColor=apply_lighting_with_emission_ground(glFragColor,u_emissive_strength);\n#endif\n#ifdef FOG\nglFragColor=fog_dither(fog_apply_premultiplied(glFragColor,v_fog_pos));\n#endif\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var hillshadeVert = "#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;in vec2 a_pos;in vec2 a_texture_pos;out vec2 v_pos;void main() {gl_Position=u_matrix*vec4(a_pos,0,1);v_pos=a_texture_pos/8192.0;\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var lineFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nuniform lowp float u_device_pixel_ratio;uniform highp float u_width_scale;uniform highp float u_floor_width_scale;uniform float u_alpha_discard_threshold;uniform highp vec2 u_trim_offset;uniform highp vec2 u_trim_fade_range;uniform lowp vec4 u_trim_color;in vec2 v_width2;in vec2 v_normal;in float v_gamma_scale;in highp vec3 v_uv;\n#ifdef ELEVATED_ROADS\nin highp float v_road_z_offset;\n#endif\n#ifdef RENDER_LINE_DASH\nuniform sampler2D u_dash_image;in vec2 v_tex;\n#endif\n#ifdef RENDER_LINE_GRADIENT\nuniform sampler2D u_gradient_image;\n#endif\n#ifdef INDICATOR_CUTOUT\nin highp float v_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\nfloat luminance(vec3 c) {return (c.r+c.r+c.b+c.g+c.g+c.g)*0.1667;}\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define lowp float floorwidth\n#pragma mapbox: define lowp vec4 dash\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define mediump float side_z_offset\n#pragma mapbox: define lowp float border_width\n#pragma mapbox: define lowp vec4 border_color\n#pragma mapbox: define lowp float emissive_strength\nfloat linearstep(float edge0,float edge1,float x) {return clamp((x-edge0)/(edge1-edge0),0.0,1.0);}void main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize lowp float floorwidth\n#pragma mapbox: initialize lowp vec4 dash\n#pragma mapbox: initialize lowp float blur\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump float side_z_offset\n#pragma mapbox: initialize lowp float border_width\n#pragma mapbox: initialize lowp vec4 border_color\n#pragma mapbox: initialize lowp float emissive_strength\nfloat dist=length(v_normal)*v_width2.s;float blur2=(u_width_scale*blur+1.0/u_device_pixel_ratio)*v_gamma_scale;float alpha=clamp(min(dist-(v_width2.t-blur2),v_width2.s-dist)/blur2,0.0,1.0);alpha=side_z_offset > 0.0 ? 1.0-alpha : alpha;\n#ifdef RENDER_LINE_DASH\nfloat sdfdist=texture(u_dash_image,v_tex).r;float sdfgamma=1.0/(2.0*u_device_pixel_ratio)/dash.z;float scaled_floorwidth=(floorwidth*u_floor_width_scale);alpha*=linearstep(0.5-sdfgamma/scaled_floorwidth,0.5+sdfgamma/scaled_floorwidth,sdfdist);\n#endif\nhighp vec4 out_color;\n#ifdef RENDER_LINE_GRADIENT\nout_color=texture(u_gradient_image,v_uv.xy);\n#ifdef MULTIPLY_LINE_GRADIENT_COLOR\nout_color*=color;\n#endif\n#else\nout_color=color;\n#endif\nfloat trim_alpha=1.0;\n#ifdef RENDER_LINE_TRIM_OFFSET\nhighp float trim_start=u_trim_offset[0];highp float trim_end=u_trim_offset[1];highp float line_progress=v_uv[2];if (trim_end > trim_start) {highp float start_transition=max(0.0,min(1.0,(line_progress-trim_start)/max(u_trim_fade_range[0],1.0e-9)));highp float end_transition=max(0.0,min(1.0,(trim_end-line_progress)/max(u_trim_fade_range[1],1.0e-9)));highp float transition_factor=min(start_transition,end_transition);out_color=mix(out_color,u_trim_color,transition_factor);trim_alpha=1.0-transition_factor;}\n#endif\nif (u_alpha_discard_threshold !=0.0) {if (alpha < u_alpha_discard_threshold) {discard;}}\n#ifdef RENDER_LINE_BORDER\nfloat edgeBlur=((border_width*u_width_scale)+1.0/u_device_pixel_ratio);float alpha2=clamp(min(dist-(v_width2.t-edgeBlur),v_width2.s-dist)/edgeBlur,0.0,1.0);if (alpha2 < 1.) {float smoothAlpha=smoothstep(0.6,1.0,alpha2);if (border_color.a==0.0) {float Y=(out_color.a > 0.01) ? luminance(out_color.rgb/out_color.a) : 1.;float adjustment=(Y > 0.) ? 0.5/Y : 0.45;if (out_color.a > 0.25 && Y < 0.25) {vec3 borderColor=(Y > 0.) ? out_color.rgb : vec3(1,1,1)*out_color.a;out_color.rgb=out_color.rgb+borderColor*(adjustment*(1.0-smoothAlpha));} else {out_color.rgb*=(0.6 +0.4*smoothAlpha);}} else {out_color=mix(border_color*trim_alpha,out_color,smoothAlpha);}}\n#endif\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);\n#ifdef ELEVATED_ROADS\nout_color.rgb*=mix(v_road_z_offset !=0.0 ? u_ground_shadow_factor : vec3(1.0),vec3(1.0),light);\n#else\nout_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#endif\n#ifdef FOG\nout_color=fog_dither(fog_apply_premultiplied(out_color,v_fog_pos));\n#endif\nout_color*=(alpha*opacity);\n#ifdef INDICATOR_CUTOUT\nout_color=applyCutout(out_color,v_z_offset);\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\nglFragColor=out_color;\n#ifdef DUAL_SOURCE_BLENDING\nglFragColorSrc1=vec4(vec3(0.0),emissive_strength);\n#else\n#ifdef USE_MRT1\nout_Target1=vec4(emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var lineVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\n#define EXTRUDE_SCALE 0.015873016\nin vec2 a_pos_normal;in vec4 a_data;\n#if defined(ELEVATED) || defined(ELEVATED_ROADS) || defined(VARIABLE_LINE_WIDTH)\nin vec3 a_z_offset_width;\n#endif\n#if defined(RENDER_LINE_GRADIENT) || defined(RENDER_LINE_TRIM_OFFSET)\nin highp vec3 a_packed;\n#endif\n#ifdef RENDER_LINE_DASH\nin float a_linesofar;\n#endif\nuniform mat4 u_matrix;uniform mat2 u_pixels_to_tile_units;uniform vec2 u_units_to_pixels;uniform lowp float u_device_pixel_ratio;uniform float u_width_scale;uniform highp float u_floor_width_scale;\n#ifdef ELEVATED\nuniform lowp float u_zbias_factor;uniform lowp float u_tile_to_meter;float sample_elevation(vec2 apos) {\n#ifdef ELEVATION_REFERENCE_SEA\nreturn 0.0;\n#else\nreturn elevation(apos);\n#endif\n}\n#endif\nout vec2 v_normal;out vec2 v_width2;out float v_gamma_scale;out highp vec3 v_uv;\n#ifdef ELEVATED_ROADS\nout highp float v_road_z_offset;\n#endif\n#ifdef RENDER_LINE_DASH\nuniform vec2 u_texsize;uniform float u_tile_units_to_pixels;out vec2 v_tex;\n#endif\n#ifdef RENDER_LINE_GRADIENT\nuniform float u_image_height;\n#endif\n#ifdef INDICATOR_CUTOUT\nout highp float v_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\n#pragma mapbox: define highp vec4 color\n#pragma mapbox: define lowp float floorwidth\n#pragma mapbox: define lowp vec4 dash\n#pragma mapbox: define lowp float blur\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define mediump float gapwidth\n#pragma mapbox: define lowp float offset\n#pragma mapbox: define mediump float width\n#pragma mapbox: define mediump float side_z_offset\n#pragma mapbox: define lowp float border_width\n#pragma mapbox: define lowp vec4 border_color\n#pragma mapbox: define lowp float emissive_strength\nvoid main() {\n#pragma mapbox: initialize highp vec4 color\n#pragma mapbox: initialize lowp float floorwidth\n#pragma mapbox: initialize lowp vec4 dash\n#pragma mapbox: initialize lowp float blur\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize mediump float gapwidth\n#pragma mapbox: initialize lowp float offset\n#pragma mapbox: initialize mediump float width\n#pragma mapbox: initialize mediump float side_z_offset\n#pragma mapbox: initialize lowp float border_width\n#pragma mapbox: initialize lowp vec4 border_color\n#pragma mapbox: initialize lowp float emissive_strength\nfloat a_z_offset;\n#if defined(ELEVATED) || defined(ELEVATED_ROADS)\na_z_offset=a_z_offset_width.x;\n#endif\nfloat ANTIALIASING=1.0/u_device_pixel_ratio/2.0;vec2 a_extrude=a_data.xy-128.0;float a_direction=mod(a_data.z,4.0)-1.0;vec2 pos=floor(a_pos_normal*0.5);mediump vec2 normal=a_pos_normal-2.0*pos;normal.y=normal.y*2.0-1.0;v_normal=normal;gapwidth=gapwidth/2.0;float halfwidth;\n#ifdef VARIABLE_LINE_WIDTH\nbool left=normal.y==1.0;halfwidth=(u_width_scale*(left ? a_z_offset_width.y : a_z_offset_width.z))/2.0;a_z_offset+=left ? side_z_offset : 0.0;v_normal=side_z_offset > 0.0 && left ? vec2(0.0) : v_normal;\n#else\nhalfwidth=(u_width_scale*width)/2.0;\n#endif\noffset=-1.0*offset*u_width_scale;float inset=gapwidth+(gapwidth > 0.0 ? ANTIALIASING : 0.0);float outset=gapwidth+halfwidth*(gapwidth > 0.0 ? 2.0 : 1.0)+(halfwidth==0.0 ? 0.0 : ANTIALIASING);mediump vec2 dist=outset*a_extrude*EXTRUDE_SCALE;mediump float u=0.5*a_direction;mediump float t=1.0-abs(u);mediump vec2 offset2=offset*a_extrude*EXTRUDE_SCALE*normal.y*mat2(t,-u,u,t);float hidden=float(opacity==0.0);vec2 extrude=dist*u_pixels_to_tile_units;vec4 projected_extrude=u_matrix*vec4(extrude,0.0,0.0);vec2 projected_extrude_xy=projected_extrude.xy;\n#ifdef ELEVATED_ROADS\nv_road_z_offset=a_z_offset;gl_Position=u_matrix*vec4(pos+offset2*u_pixels_to_tile_units,a_z_offset,1.0)+projected_extrude;\n#else\n#ifdef ELEVATED\nvec2 offsetTile=offset2*u_pixels_to_tile_units;vec2 offset_pos=pos+offsetTile;float ele=0.0;\n#ifdef CROSS_SLOPE_VERTICAL\nfloat top=a_pos_normal.y-2.0*floor(a_pos_normal.y*0.5);float line_height=2.0*u_tile_to_meter*outset*top*u_pixels_to_tile_units[1][1]+a_z_offset;ele=sample_elevation(offset_pos)+line_height;projected_extrude=vec4(0);\n#else\n#ifdef CROSS_SLOPE_HORIZONTAL\nfloat ele0=sample_elevation(offset_pos);float ele1=max(sample_elevation(offset_pos+extrude),sample_elevation(offset_pos+extrude/2.0));float ele2=max(sample_elevation(offset_pos-extrude),sample_elevation(offset_pos-extrude/2.0));float ele_max=max(ele0,max(ele1,ele2));ele=ele_max+a_z_offset;\n#else\nfloat ele0=sample_elevation(offset_pos);float ele1=max(sample_elevation(offset_pos+extrude),sample_elevation(offset_pos+extrude/2.0));float ele2=max(sample_elevation(offset_pos-extrude),sample_elevation(offset_pos-extrude/2.0));float ele_max=max(ele0,0.5*(ele1+ele2));ele=ele_max-ele0+ele1+a_z_offset;\n#endif\n#endif\ngl_Position=u_matrix*vec4(offset_pos,ele,1.0)+projected_extrude;float z=clamp(gl_Position.z/gl_Position.w,0.5,1.0);float zbias=max(0.00005,(pow(z,0.8)-z)*u_zbias_factor*u_exaggeration);gl_Position.z-=(gl_Position.w*zbias);gl_Position=mix(gl_Position,AWAY,hidden);\n#else\ngl_Position=mix(u_matrix*vec4(pos+offset2*u_pixels_to_tile_units,0.0,1.0)+projected_extrude,AWAY,hidden);\n#endif\n#endif\n#ifdef ELEVATED_ROADS\n#ifdef RENDER_SHADOWS\nvec3 shd_pos=vec3(pos+(offset2+dist)*u_pixels_to_tile_units,a_z_offset);vec3 shd_pos0=shd_pos;vec3 shd_pos1=shd_pos;\n#ifdef NORMAL_OFFSET\nvec3 shd_pos_offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=shd_pos_offset*shadow_normal_offset_multiplier0();shd_pos1+=shd_pos_offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#endif\n#ifndef RENDER_TO_TEXTURE\nfloat epsilon=0.0001;float extrude_length_without_perspective=max(length(dist),epsilon);float extrude_length_with_perspective=max(length(projected_extrude_xy/gl_Position.w*u_units_to_pixels),epsilon);v_gamma_scale=mix(extrude_length_without_perspective/extrude_length_with_perspective,1.0,step(0.01,blur));\n#else\nv_gamma_scale=1.0;\n#endif\n#if defined(RENDER_LINE_GRADIENT) || defined(RENDER_LINE_TRIM_OFFSET)\nhighp float a_uv_x=a_packed[0];float a_split_index=a_packed[1];highp float line_progress=a_packed[2];\n#ifdef RENDER_LINE_GRADIENT\nhighp float texel_height=1.0/u_image_height;highp float half_texel_height=0.5*texel_height;v_uv=vec3(a_uv_x,a_split_index*texel_height-half_texel_height,line_progress);\n#else\nv_uv=vec3(a_uv_x,0.0,line_progress);\n#endif\n#endif\n#ifdef RENDER_LINE_DASH\nfloat scale=dash.z==0.0 ? 0.0 : u_tile_units_to_pixels/dash.z;float height=dash.y;v_tex=vec2(a_linesofar*scale/(floorwidth*u_floor_width_scale),(-normal.y*height+dash.x+0.5)/u_texsize.y);\n#endif\nv_width2=vec2(outset,inset);\n#ifdef FOG\nv_fog_pos=fog_position(pos);\n#endif\n#ifdef INDICATOR_CUTOUT\nv_z_offset=a_z_offset;\n#endif\n}";
var linePatternFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nuniform highp float u_device_pixel_ratio;uniform highp float u_width_scale;uniform highp float u_alpha_discard_threshold;uniform highp vec2 u_texsize;uniform highp float u_tile_units_to_pixels;uniform highp vec2 u_trim_offset;uniform highp vec2 u_trim_fade_range;uniform lowp vec4 u_trim_color;uniform sampler2D u_image;\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\n#ifdef LINE_PATTERN_TRANSITION\nuniform float u_pattern_transition;\n#endif\nin vec2 v_normal;in vec2 v_width2;in highp float v_linesofar;in float v_gamma_scale;in float v_width;\n#ifdef RENDER_LINE_TRIM_OFFSET\nin highp vec3 v_uv;\n#endif\n#ifdef ELEVATED_ROADS\nin highp float v_road_z_offset;\n#endif\n#ifdef LINE_JOIN_NONE\nin vec2 v_pattern_data;\n#endif\n#ifdef INDICATOR_CUTOUT\nin highp float v_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\n#pragma mapbox: define mediump vec4 pattern\n#ifdef LINE_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define mediump float pixel_ratio\n#pragma mapbox: define mediump float blur\n#pragma mapbox: define mediump float opacity\n#pragma mapbox: define lowp float emissive_strength\nvoid main() {\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef LINE_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\n#pragma mapbox: initialize mediump float pixel_ratio\n#pragma mapbox: initialize mediump float blur\n#pragma mapbox: initialize mediump float opacity\n#pragma mapbox: initialize lowp float emissive_strength\nvec2 pattern_tl=pattern.xy;vec2 pattern_br=pattern.zw;vec2 display_size=(pattern_br-pattern_tl)/pixel_ratio;highp float pattern_size=display_size.x/u_tile_units_to_pixels;float aspect=display_size.y/v_width;float dist=length(v_normal)*v_width2.s;float blur2=(u_width_scale*blur+1.0/u_device_pixel_ratio)*v_gamma_scale;float alpha=clamp(min(dist-(v_width2.t-blur2),v_width2.s-dist)/blur2,0.0,1.0);highp float pattern_x=v_linesofar/pattern_size*aspect;highp float x=mod(pattern_x,1.0);highp float y=0.5*v_normal.y+0.5;vec2 texel_size=1.0/u_texsize;highp vec2 pos=mix(pattern_tl*texel_size-texel_size,pattern_br*texel_size+texel_size,vec2(x,y));highp vec2 lod_pos=mix(pattern_tl*texel_size-texel_size,pattern_br*texel_size+texel_size,vec2(pattern_x,y));vec4 color=textureLodCustom(u_image,pos,lod_pos);\n#ifdef APPLY_LUT_ON_GPU\ncolor=applyLUT(u_lutTexture,color);\n#endif\n#ifdef LINE_PATTERN_TRANSITION\nvec2 pattern_b_tl=pattern_b.xy;vec2 pattern_b_br=pattern_b.zw;highp vec2 pos_b=mix(pattern_b_tl*texel_size-texel_size,pattern_b_br*texel_size+texel_size,vec2(x,y));vec4 color_b=textureLodCustom(u_image,pos_b,lod_pos);color=color*(1.0-u_pattern_transition)+color_b*u_pattern_transition;\n#endif\n#ifdef RENDER_LINE_TRIM_OFFSET\nhighp float trim_start=u_trim_offset[0];highp float trim_end=u_trim_offset[1];highp float line_progress=v_uv[2];if (trim_end > trim_start) {highp float start_transition=max(0.0,min(1.0,(line_progress-trim_start)/max(u_trim_fade_range[0],1.0e-9)));highp float end_transition=max(0.0,min(1.0,(trim_end-line_progress)/max(u_trim_fade_range[1],1.0e-9)));highp float transition_factor=min(start_transition,end_transition);color=mix(color,color.a*u_trim_color,transition_factor);}\n#endif\n#ifdef LINE_JOIN_NONE\nhighp float pattern_len=pattern_size/aspect;highp float segment_phase=pattern_len-mod(v_linesofar-v_pattern_data.x+pattern_len,pattern_len);highp float visible_start=segment_phase-step(pattern_len*0.5,segment_phase)*pattern_len;highp float visible_end=floor((v_pattern_data.y-segment_phase)/pattern_len)*pattern_len+segment_phase;visible_end+=step(pattern_len*0.5,v_pattern_data.y-visible_end)*pattern_len;if (v_pattern_data.x < visible_start || v_pattern_data.x >=visible_end) {color=vec4(0.0);}\n#endif\n#ifdef LIGHTING_3D_MODE\ncolor=apply_lighting_with_emission_ground(color,emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);\n#ifdef ELEVATED_ROADS\ncolor.rgb*=mix(v_road_z_offset !=0.0 ? u_ground_shadow_factor : vec3(1.0),vec3(1.0),light);\n#else\ncolor.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#endif\n#endif\n#endif\n#ifdef FOG\ncolor=fog_dither(fog_apply_premultiplied(color,v_fog_pos));\n#endif\ncolor*=(alpha*opacity);if (u_alpha_discard_threshold !=0.0) {if (color.a < u_alpha_discard_threshold) {discard;}}\n#ifdef INDICATOR_CUTOUT\ncolor=applyCutout(color,v_z_offset);\n#endif\nglFragColor=color;\n#ifdef DUAL_SOURCE_BLENDING\nglFragColorSrc1=vec4(vec3(0.0),emissive_strength);\n#else\n#ifdef USE_MRT1\nout_Target1=vec4(emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var linePatternVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\n#define scale 0.015873016\nin vec2 a_pos_normal;in vec4 a_data;\n#if defined(ELEVATED) || defined(ELEVATED_ROADS)\nin vec3 a_z_offset_width;\n#endif\n#ifdef RENDER_LINE_TRIM_OFFSET\nin highp vec3 a_packed;\n#endif\nin highp float a_linesofar;\n#ifdef LINE_JOIN_NONE\nin highp vec3 a_pattern_data;out vec2 v_pattern_data;\n#endif\n#ifdef INDICATOR_CUTOUT\nout highp float v_z_offset;\n#endif\nuniform mat4 u_matrix;uniform float u_tile_units_to_pixels;uniform vec2 u_units_to_pixels;uniform mat2 u_pixels_to_tile_units;uniform float u_device_pixel_ratio;uniform float u_width_scale;uniform float u_floor_width_scale;\n#ifdef ELEVATED\nuniform lowp float u_zbias_factor;uniform lowp float u_tile_to_meter;float sample_elevation(vec2 apos) {\n#ifdef ELEVATION_REFERENCE_SEA\nreturn 0.0;\n#else\nreturn elevation(apos);\n#endif\n}\n#endif\nout vec2 v_normal;out vec2 v_width2;out highp float v_linesofar;out float v_gamma_scale;out float v_width;\n#ifdef RENDER_LINE_TRIM_OFFSET\nout highp vec3 v_uv;\n#endif\n#ifdef ELEVATED_ROADS\nout highp float v_road_z_offset;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\n#pragma mapbox: define mediump float blur\n#pragma mapbox: define mediump float opacity\n#pragma mapbox: define mediump float offset\n#pragma mapbox: define mediump float gapwidth\n#pragma mapbox: define mediump float width\n#pragma mapbox: define mediump float floorwidth\n#pragma mapbox: define mediump vec4 pattern\n#ifdef LINE_PATTERN_TRANSITION\n#pragma mapbox: define mediump vec4 pattern_b\n#endif\n#pragma mapbox: define mediump float pixel_ratio\n#pragma mapbox: define lowp float emissive_strength\nvoid main() {\n#pragma mapbox: initialize mediump float blur\n#pragma mapbox: initialize mediump float opacity\n#pragma mapbox: initialize mediump float offset\n#pragma mapbox: initialize mediump float gapwidth\n#pragma mapbox: initialize mediump float width\n#pragma mapbox: initialize mediump float floorwidth\n#pragma mapbox: initialize mediump vec4 pattern\n#ifdef LINE_PATTERN_TRANSITION\n#pragma mapbox: initialize mediump vec4 pattern_b\n#endif\n#pragma mapbox: initialize mediump float pixel_ratio\n#pragma mapbox: initialize lowp float emissive_strength\nfloat a_z_offset;\n#if defined(ELEVATED) || defined(ELEVATED_ROADS)\na_z_offset=a_z_offset_width.x;\n#endif\nfloat ANTIALIASING=1.0/u_device_pixel_ratio/2.0;vec2 a_extrude=a_data.xy-128.0;float a_direction=mod(a_data.z,4.0)-1.0;vec2 pos=floor(a_pos_normal*0.5);vec2 normal=a_pos_normal-2.0*pos;normal.y=normal.y*2.0-1.0;v_normal=normal;gapwidth=gapwidth/2.0;float halfwidth=(u_width_scale*width)/2.0;offset=-1.0*offset*u_width_scale;float inset=gapwidth+(gapwidth > 0.0 ? ANTIALIASING : 0.0);float outset=gapwidth+halfwidth*(gapwidth > 0.0 ? 2.0 : 1.0)+(halfwidth==0.0 ? 0.0 : ANTIALIASING);vec2 dist=outset*a_extrude*scale;float u=0.5*a_direction;float t=1.0-abs(u);vec2 offset2=offset*a_extrude*scale*normal.y*mat2(t,-u,u,t);float hidden=float(opacity==0.0);vec2 extrude=dist*u_pixels_to_tile_units;vec4 projected_extrude=u_matrix*vec4(extrude,0.0,0.0);vec2 projected_extrude_xy=projected_extrude.xy;\n#ifdef ELEVATED_ROADS\nv_road_z_offset=a_z_offset;gl_Position=u_matrix*vec4(pos+offset2*u_pixels_to_tile_units,a_z_offset,1.0)+projected_extrude;\n#else\n#ifdef ELEVATED\nvec2 offsetTile=offset2*u_pixels_to_tile_units;vec2 offset_pos=pos+offsetTile;float ele=0.0;\n#ifdef CROSS_SLOPE_VERTICAL\nfloat top=a_pos_normal.y-2.0*floor(a_pos_normal.y*0.5);float line_height=2.0*u_tile_to_meter*outset*top*u_pixels_to_tile_units[1][1]+a_z_offset;ele=sample_elevation(offset_pos)+line_height;projected_extrude=vec4(0);\n#else\n#ifdef CROSS_SLOPE_HORIZONTAL\nfloat ele0=sample_elevation(offset_pos);float ele1=max(sample_elevation(offset_pos+extrude),sample_elevation(offset_pos+extrude/2.0));float ele2=max(sample_elevation(offset_pos-extrude),sample_elevation(offset_pos-extrude/2.0));float ele_max=max(ele0,max(ele1,ele2));ele=ele_max+a_z_offset;\n#else\nfloat ele0=sample_elevation(offset_pos);float ele1=max(sample_elevation(offset_pos+extrude),sample_elevation(offset_pos+extrude/2.0));float ele2=max(sample_elevation(offset_pos-extrude),sample_elevation(offset_pos-extrude/2.0));float ele_max=max(ele0,0.5*(ele1+ele2));ele=ele_max-ele0+ele1+a_z_offset;\n#endif\n#endif\ngl_Position=u_matrix*vec4(offset_pos,ele,1.0)+projected_extrude;float z=clamp(gl_Position.z/gl_Position.w,0.5,1.0);float zbias=max(0.00005,(pow(z,0.8)-z)*u_zbias_factor*u_exaggeration);gl_Position.z-=(gl_Position.w*zbias);gl_Position=mix(gl_Position,AWAY,hidden);\n#else\ngl_Position=mix(u_matrix*vec4(pos+offset2*u_pixels_to_tile_units,0.0,1.0)+projected_extrude,AWAY,hidden);\n#endif\n#endif\n#ifdef ELEVATED_ROADS\n#ifdef RENDER_SHADOWS\nvec3 shd_pos=vec3(pos+(offset2+dist)*u_pixels_to_tile_units,a_z_offset);vec3 shd_pos0=shd_pos;vec3 shd_pos1=shd_pos;\n#ifdef NORMAL_OFFSET\nvec3 shd_pos_offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=shd_pos_offset*shadow_normal_offset_multiplier0();shd_pos1+=shd_pos_offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#endif\n#ifndef RENDER_TO_TEXTURE\nfloat extrude_length_without_perspective=length(dist);float extrude_length_with_perspective=length(projected_extrude_xy/gl_Position.w*u_units_to_pixels);v_gamma_scale=mix(extrude_length_without_perspective/extrude_length_with_perspective,1.0,step(0.01,blur));\n#else\nv_gamma_scale=1.0;\n#endif\n#ifdef RENDER_LINE_TRIM_OFFSET\nhighp float a_uv_x=a_packed[0];highp float line_progress=a_packed[2];v_uv=vec3(a_uv_x,0.0,line_progress);\n#endif\nv_linesofar=a_linesofar;v_width2=vec2(outset,inset);v_width=(floorwidth*u_floor_width_scale);\n#ifdef LINE_JOIN_NONE\nv_width=(floorwidth*u_floor_width_scale)+ANTIALIASING;mediump float pixels_to_tile_units=1.0/u_tile_units_to_pixels;mediump float pixel_ratio_inverse=1.0/pixel_ratio;mediump float aspect=v_width/((pattern.w-pattern.y)*pixel_ratio_inverse);highp float subt_multiple=(pattern.z-pattern.x)*pixel_ratio_inverse*pixels_to_tile_units*aspect*32.0;highp float subt=floor(a_pattern_data.z/subt_multiple)*subt_multiple;float offset_sign=(fract(a_pattern_data.x)-0.5)*4.0;float line_progress_offset=offset_sign*v_width*0.5*pixels_to_tile_units;v_linesofar=(a_pattern_data.z-subt)+a_linesofar+line_progress_offset;v_pattern_data=vec2(a_pattern_data.x+line_progress_offset,a_pattern_data.y);\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(pos);\n#endif\n#ifdef INDICATOR_CUTOUT\nv_z_offset=a_z_offset;\n#endif\n}";
var rasterFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_raster_array.glsl\"\nuniform float u_fade_t;uniform float u_opacity;uniform highp float u_raster_elevation;uniform highp float u_zoom_transition;in vec2 v_pos0;in vec2 v_pos1;in float v_depth;\n#ifdef PROJECTION_GLOBE_VIEW\nin float v_split_fade;\n#endif\nuniform float u_brightness_low;uniform float u_brightness_high;uniform float u_saturation_factor;uniform float u_contrast_factor;uniform vec3 u_spin_weights;uniform float u_emissive_strength;\n#ifndef RASTER_ARRAY\nuniform highp sampler2D u_image0;uniform sampler2D u_image1;\n#endif\n#ifdef RASTER_COLOR\nuniform sampler2D u_color_ramp;uniform highp vec4 u_colorization_mix;uniform highp float u_colorization_offset;uniform vec2 u_texture_res;\n#endif\nvoid main() {vec4 color0,color1,color;vec2 value;\n#ifdef RASTER_COLOR\n#ifdef RASTER_ARRAY\n#ifdef RASTER_ARRAY_LINEAR\nvalue=mix(\nraTexture2D_image0_linear(v_pos0,u_texture_res,u_colorization_mix,u_colorization_offset),raTexture2D_image1_linear(v_pos1,u_texture_res,u_colorization_mix,u_colorization_offset),u_fade_t\n);\n#else\nvalue=mix(\nraTexture2D_image0_nearest(v_pos0,u_texture_res,u_colorization_mix,u_colorization_offset),raTexture2D_image1_nearest(v_pos1,u_texture_res,u_colorization_mix,u_colorization_offset),u_fade_t\n);\n#endif\nif (value.y > 0.0) value.x/=value.y;\n#else\ncolor=mix(texture(u_image0,v_pos0),texture(u_image1,v_pos1),u_fade_t);value=vec2(u_colorization_offset+dot(color.rgb,u_colorization_mix.rgb),color.a);\n#endif\ncolor=texture(u_color_ramp,vec2(value.x,0.5));if (color.a > 0.0) color.rgb/=color.a;color.a*=value.y;\n#else\ncolor0=texture(u_image0,v_pos0);color1=texture(u_image1,v_pos1);if (color0.a > 0.0) color0.rgb/=color0.a;if (color1.a > 0.0) color1.rgb/=color1.a;color=mix(color0,color1,u_fade_t);\n#endif\ncolor.a*=u_opacity;\n#ifdef GLOBE_POLES\ncolor.a*=1.0-smoothstep(0.0,0.05,u_zoom_transition);\n#endif\nvec3 rgb=color.rgb;rgb=vec3(\ndot(rgb,u_spin_weights.xyz),dot(rgb,u_spin_weights.zxy),dot(rgb,u_spin_weights.yzx));float average=(color.r+color.g+color.b)/3.0;rgb+=(average-rgb)*u_saturation_factor;rgb=(rgb-0.5)*u_contrast_factor+0.5;vec3 u_high_vec=vec3(u_brightness_low,u_brightness_low,u_brightness_low);vec3 u_low_vec=vec3(u_brightness_high,u_brightness_high,u_brightness_high);vec3 out_color=mix(u_high_vec,u_low_vec,rgb);\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(vec4(out_color,1.0),u_emissive_strength).rgb;\n#endif\n#ifdef FOG\nhighp float fog_limit_high_meters=1000000.0;highp float fog_limit_low_meters=600000.0;float fog_limit=1.0-smoothstep(fog_limit_low_meters,fog_limit_high_meters,u_raster_elevation);out_color=fog_dither(fog_apply(out_color,v_fog_pos,fog_limit));\n#endif\nglFragColor=vec4(out_color*color.a,color.a);\n#ifdef PROJECTION_GLOBE_VIEW\nglFragColor*=mix(1.0,1.0-smoothstep(0.0,0.05,u_zoom_transition),smoothstep(0.8,0.9,v_split_fade));\n#endif\n#ifdef RENDER_CUTOFF\nglFragColor=glFragColor*cutoff_opacity(u_cutoff_params,v_depth);\n#endif\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\n#ifdef USE_MRT1\nout_Target1=vec4(u_emissive_strength*glFragColor.a,0.0,0.0,glFragColor.a);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var rasterVert = "#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;uniform mat4 u_normalize_matrix;uniform mat4 u_globe_matrix;uniform mat4 u_merc_matrix;uniform mat3 u_grid_matrix;uniform vec2 u_tl_parent;uniform float u_scale_parent;uniform vec2 u_perspective_transform;uniform vec2 u_texture_offset;uniform float u_raster_elevation;uniform float u_zoom_transition;uniform vec2 u_merc_center;\n#define GLOBE_UPSCALE GLOBE_RADIUS/6371008.8\n#ifdef GLOBE_POLES\nin vec3 a_globe_pos;in vec2 a_uv;\n#else\nin vec2 a_pos;in vec2 a_texture_pos;\n#endif\nout vec2 v_pos0;out vec2 v_pos1;out float v_depth;\n#ifdef PROJECTION_GLOBE_VIEW\nout float v_split_fade;\n#endif\nvoid main() {vec2 uv;\n#ifdef GLOBE_POLES\nvec3 globe_pos=a_globe_pos;globe_pos+=normalize(globe_pos)*u_raster_elevation*GLOBE_UPSCALE;gl_Position=u_matrix*u_globe_matrix*vec4(globe_pos ,1.0);uv=a_uv;\n#ifdef FOG\nv_fog_pos=fog_position((u_normalize_matrix*vec4(a_globe_pos,1.0)).xyz);\n#endif\n#else\nfloat w=1.0+dot(a_texture_pos,u_perspective_transform);uv=a_texture_pos/8192.0;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 decomposed_pos_and_skirt=decomposeToPosAndSkirt(a_pos);vec3 latLng=u_grid_matrix*vec3(decomposed_pos_and_skirt.xy,1.0);vec3 globe_pos=latLngToECEF(latLng.xy);globe_pos+=normalize(globe_pos)*u_raster_elevation*GLOBE_UPSCALE;vec4 globe_world_pos=u_globe_matrix*vec4(globe_pos,1.0);vec4 merc_world_pos=vec4(0.0);float mercatorY=mercatorYfromLat(latLng[0]);float mercatorX=mercatorXfromLng(latLng[1]); \nv_split_fade=0.0;if (u_zoom_transition > 0.0) {vec2 merc_pos=vec2(mercatorX,mercatorY);merc_world_pos=vec4(merc_pos,u_raster_elevation,1.0);merc_world_pos.xy-=u_merc_center;merc_world_pos.x=wrap(merc_world_pos.x,-0.5,0.5);merc_world_pos=u_merc_matrix*merc_world_pos;float opposite_merc_center=mod(u_merc_center.x+0.5,1.0);float dist_from_poles=(abs(mercatorY-0.5)*2.0);float range=0.1;v_split_fade=abs(opposite_merc_center-mercatorX);v_split_fade=clamp(1.0-v_split_fade,0.0,1.0);v_split_fade=max(smoothstep(1.0-range,1.0,dist_from_poles),max(smoothstep(1.0-range,1.0,v_split_fade),smoothstep(1.0-range,1.0,1.0-v_split_fade)));}float tiles=u_grid_matrix[0][2];if (tiles > 0.0) {float idx=u_grid_matrix[1][2];float idy=u_grid_matrix[2][2];float uvY=mercatorY*tiles-idy;float uvX=mercatorX*tiles-idx;uv=vec2(uvX,uvY);}vec4 interpolated_pos=vec4(mix(globe_world_pos.xyz,merc_world_pos.xyz,u_zoom_transition)*w,w);gl_Position=u_matrix*interpolated_pos;\n#ifdef FOG\nv_fog_pos=fog_position((u_normalize_matrix*vec4(globe_pos,1.0)).xyz);\n#endif\n#else\ngl_Position=u_matrix*vec4(a_pos*w,u_raster_elevation*w,w);\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n#endif\n#endif\nv_pos0=uv;v_pos1=(v_pos0*u_scale_parent)+u_tl_parent;v_pos0=u_texture_offset.x+u_texture_offset.y*v_pos0;v_pos1=u_texture_offset.x+u_texture_offset.y*v_pos1;\n#ifdef RENDER_CUTOFF\nv_depth=gl_Position.z;\n#endif\n}";
var rasterParticleFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform float u_fade_t;uniform float u_opacity;uniform highp float u_raster_elevation;in vec2 v_pos0;in vec2 v_pos1;uniform sampler2D u_image0;uniform sampler2D u_image1;void main() {vec4 color0,color1,color;color0=texture(u_image0,v_pos0);color1=texture(u_image1,v_pos1);if (color0.a > 0.0) color0.rgb/=color0.a;if (color1.a > 0.0) color1.rgb/=color1.a;color=mix(color0,color1,u_fade_t);color.a*=u_opacity;vec3 out_color=color.rgb;\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(vec4(out_color,1.0),1.0).rgb;\n#endif\n#ifdef FOG\nhighp float fog_limit_high_meters=1000000.0;highp float fog_limit_low_meters=600000.0;float fog_limit=1.0-smoothstep(fog_limit_low_meters,fog_limit_high_meters,u_raster_elevation);out_color=fog_dither(fog_apply(out_color,v_fog_pos,fog_limit));\n#endif\nglFragColor=vec4(out_color*color.a,color.a);\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var rasterParticleVert = "#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;uniform mat4 u_normalize_matrix;uniform mat4 u_globe_matrix;uniform mat4 u_merc_matrix;uniform mat3 u_grid_matrix;uniform vec2 u_tl_parent;uniform float u_scale_parent;uniform float u_raster_elevation;uniform float u_zoom_transition;uniform vec2 u_merc_center;\n#define GLOBE_UPSCALE GLOBE_RADIUS/6371008.8\nin vec2 a_pos;in vec2 a_texture_pos;out vec2 v_pos0;out vec2 v_pos1;void main() {float w=1.0;vec2 uv;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 decomposed_pos_and_skirt=decomposeToPosAndSkirt(a_pos);vec3 latLng=u_grid_matrix*vec3(decomposed_pos_and_skirt.xy,1.0);float mercatorY=mercatorYfromLat(latLng[0]);float mercatorX=mercatorXfromLng(latLng[1]);float tiles=u_grid_matrix[0][2];float idx=u_grid_matrix[1][2];float idy=u_grid_matrix[2][2];float uvX=mercatorX*tiles-idx;float uvY=mercatorY*tiles-idy;uv=vec2(uvX,uvY);vec3 globe_pos=latLngToECEF(latLng.xy);globe_pos+=normalize(globe_pos)*u_raster_elevation*GLOBE_UPSCALE;vec4 globe_world_pos=u_globe_matrix*vec4(globe_pos,1.0);vec4 merc_world_pos=vec4(0.0);if (u_zoom_transition > 0.0) {vec2 merc_pos=vec2(mercatorX,mercatorY);merc_world_pos=vec4(merc_pos,u_raster_elevation,1.0);merc_world_pos.xy-=u_merc_center;merc_world_pos.x=wrap(merc_world_pos.x,-0.5,0.5);merc_world_pos=u_merc_matrix*merc_world_pos;}vec4 interpolated_pos=vec4(mix(globe_world_pos.xyz,merc_world_pos.xyz,u_zoom_transition)*w,w);gl_Position=u_matrix*interpolated_pos;\n#ifdef FOG\nv_fog_pos=fog_position((u_normalize_matrix*vec4(globe_pos,1.0)).xyz);\n#endif\n#else\nuv=a_texture_pos/8192.0;gl_Position=u_matrix*vec4(a_pos*w,u_raster_elevation*w,w);\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n#endif\nv_pos0=uv;v_pos1=(v_pos0*u_scale_parent)+u_tl_parent;}";
var rasterParticleDrawFrag = "uniform sampler2D u_color_ramp;in float v_particle_speed;void main() {glFragColor=texture(u_color_ramp,vec2(v_particle_speed,0.5));}";
var rasterParticleDrawVert = "#include \"_prelude_raster_particle.glsl\"\nin float a_index;uniform sampler2D u_particle_texture;uniform float u_particle_texture_side_len;uniform vec2 u_tile_offset;out float v_particle_speed;void main() {ivec2 pixel_coord=ivec2(\nmod(a_index,u_particle_texture_side_len),a_index/u_particle_texture_side_len);vec4 pixel=texelFetch(u_particle_texture,pixel_coord,0);vec2 pos=unpack_pos_from_rgba(pixel)+u_tile_offset;vec2 tex_coord=fract(pos);vec2 velocity=lookup_velocity(tex_coord);if (velocity==INVALID_VELOCITY) {gl_Position=AWAY;v_particle_speed=0.0;} else {gl_Position=vec4(2.0*pos-1.0,0,1);v_particle_speed=length(velocity);}gl_PointSize=1.0;}";
var rasterParticleTextureFrag = "uniform sampler2D u_texture;uniform float u_opacity;in vec2 v_tex_pos;void main() {vec4 color=texture(u_texture,v_tex_pos);glFragColor=vec4(floor(255.0*color*u_opacity)/255.0);}";
var rasterParticleTextureVert = "in vec2 a_pos;out vec2 v_tex_pos;void main() {vec2 uv=0.5*a_pos+vec2(0.5);v_tex_pos=uv;gl_Position=vec4(a_pos,0.0,1.0);}";
var rasterParticleUpdateFrag = "#include \"_prelude_raster_particle.glsl\"\nuniform sampler2D u_particle_texture;uniform mediump float u_particle_texture_side_len;uniform mediump float u_speed_factor;uniform highp float u_reset_rate;uniform highp float u_rand_seed;in highp vec2 v_tex_coord;vec2 linearstep(vec2 edge0,vec2 edge1,vec2 x) {return clamp((x-edge0)/(edge1-edge0),vec2(0),vec2(1));}const highp vec3 rand_constants=vec3(12.9898,78.233,4375.85453);highp float rand(const highp vec2 co) {highp float t=dot(rand_constants.xy,co);return fract(sin(t)*(rand_constants.z+t));}void main() {ivec2 pixel_coord=ivec2(v_tex_coord*u_particle_texture_side_len);highp vec4 pixel=texelFetch(u_particle_texture,pixel_coord,0);highp vec2 pos=unpack_pos_from_rgba(pixel);highp vec2 velocity=lookup_velocity(clamp(pos,0.0,1.0));highp vec2 dp=velocity==INVALID_VELOCITY ? vec2(0) : velocity*u_speed_factor;pos=pos+dp;highp vec2 seed=(pos+v_tex_coord)*u_rand_seed;highp vec2 random_pos=vec2(rand(seed+1.3),rand(seed+2.1));highp vec2 persist_rate=pow(\nlinearstep(vec2(-u_particle_pos_offset),vec2(0),pos)*linearstep(vec2(1.0+u_particle_pos_offset),vec2(1),pos),vec2(4)\n);highp vec2 per_frame_persist=pow(persist_rate,abs(dp)/u_particle_pos_offset);highp float drop_rate=1.0-per_frame_persist.x*per_frame_persist.y;drop_rate=any(greaterThanEqual(abs(pos-0.5),vec2(0.5+u_particle_pos_offset))) ? 1.0 : drop_rate;highp float drop=step(1.0-drop_rate-u_reset_rate,rand(seed));highp vec2 next_pos=mix(pos,random_pos,drop);glFragColor=pack_pos_to_rgba(next_pos);}";
var rasterParticleUpdateVert = "in vec2 a_pos;out vec2 v_tex_coord;void main() {v_tex_coord=0.5*(a_pos+vec2(1.0));gl_Position=vec4(a_pos,0.0,1.0);}";
var symbolFrag = "#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#define SDF_PX 8.0\n#define SDF 1.0\n#define ICON 0.0\nuniform sampler2D u_texture;uniform sampler2D u_texture_icon;uniform highp float u_gamma_scale;uniform lowp float u_device_pixel_ratio;uniform bool u_is_text;uniform bool u_is_halo;uniform lowp float u_scale_factor;\n#ifdef ICON_TRANSITION\nuniform float u_icon_transition;\n#endif\n#ifdef COLOR_ADJUSTMENT\nuniform mat4 u_color_adj_mat;\n#endif\n#ifdef INDICATOR_CUTOUT\nin highp float v_z_offset;\n#else\n#ifdef RENDER_SHADOWS\nin highp float v_z_offset;\n#endif\n#endif\nin vec2 v_tex_a;\n#ifdef ICON_TRANSITION\nin vec2 v_tex_b;\n#endif\nin float v_draw_halo;in vec3 v_gamma_scale_size_fade_opacity;\n#ifdef RENDER_TEXT_AND_SYMBOL\nin float is_sdf;in vec2 v_tex_a_icon;\n#endif\n#ifdef RENDER_SHADOWS\nuniform vec3 u_ground_shadow_factor;in highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in highp float v_depth;\n#endif\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\n#pragma mapbox: define highp vec4 fill_color\n#pragma mapbox: define highp vec4 halo_color\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp float halo_width\n#pragma mapbox: define lowp float halo_blur\n#pragma mapbox: define lowp float emissive_strength\nvoid main() {\n#pragma mapbox: initialize highp vec4 fill_color\n#pragma mapbox: initialize highp vec4 halo_color\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize lowp float halo_width\n#pragma mapbox: initialize lowp float halo_blur\n#pragma mapbox: initialize lowp float emissive_strength\nvec4 out_color;float fade_opacity=v_gamma_scale_size_fade_opacity[2];\n#ifdef RENDER_TEXT_AND_SYMBOL\nif (is_sdf==ICON) {vec2 tex_icon=v_tex_a_icon;lowp float alpha=opacity*fade_opacity;glFragColor=texture(u_texture_icon,tex_icon)*alpha;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nreturn;}\n#endif\n#ifdef RENDER_SDF\nfloat EDGE_GAMMA=0.105/u_device_pixel_ratio;float gamma_scale=v_gamma_scale_size_fade_opacity.x;float size=v_gamma_scale_size_fade_opacity.y;float fontScale=u_is_text ? size/24.0 : size;out_color=fill_color;highp float gamma=EDGE_GAMMA/(fontScale*u_gamma_scale);lowp float buff=(256.0-64.0)/256.0;bool draw_halo=v_draw_halo > 0.0;if (draw_halo) {out_color=halo_color;gamma=(halo_blur*u_scale_factor*1.19/SDF_PX+EDGE_GAMMA)/(fontScale*u_gamma_scale);buff=(6.0-halo_width*u_scale_factor/fontScale)/SDF_PX;}lowp float dist=texture(u_texture,v_tex_a).r;highp float gamma_scaled=gamma*gamma_scale;highp float alpha=smoothstep(buff-gamma_scaled,buff+gamma_scaled,dist);out_color*=alpha;\n#else\n#ifdef ICON_TRANSITION\nvec4 a=texture(u_texture,v_tex_a)*(1.0-u_icon_transition);vec4 b=texture(u_texture,v_tex_b)*u_icon_transition;out_color=(a+b);\n#else\nout_color=texture(u_texture,v_tex_a);\n#endif\n#ifdef APPLY_LUT_ON_GPU\nout_color=applyLUT(u_lutTexture,out_color);\n#endif\n#ifdef COLOR_ADJUSTMENT\nout_color=u_color_adj_mat*out_color;\n#endif\n#endif\nout_color*=opacity*fade_opacity;\n#ifdef LIGHTING_3D_MODE\nout_color=apply_lighting_with_emission_ground(out_color,emissive_strength);\n#ifdef RENDER_SHADOWS\nfloat light=shadowed_light_factor(v_pos_light_view_0,v_pos_light_view_1,v_depth);\n#ifdef TERRAIN\nout_color.rgb*=mix(u_ground_shadow_factor,vec3(1.0),light);\n#else\nout_color.rgb*=mix(v_z_offset !=0.0 ? u_ground_shadow_factor : vec3(1.0),vec3(1.0),light);\n#endif\n#endif\n#endif\n#ifdef INDICATOR_CUTOUT\nout_color=applyCutout(out_color,v_z_offset);\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\nglFragColor=out_color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var symbolVert = "#include \"_prelude_terrain.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\n#define APPEARANCE_ICON 1.0\nin vec4 a_pos_offset;in vec4 a_tex_size;in vec4 a_pixeloffset;in vec4 a_projected_pos;in float a_fade_opacity;\n#ifdef Z_OFFSET\nin float a_auto_z_offset;\n#endif\n#ifdef PROJECTION_GLOBE_VIEW\nin vec3 a_globe_anchor;in vec3 a_globe_normal;\n#endif\n#ifdef ICON_TRANSITION\nin vec2 a_texb;\n#endif\n#ifdef OCCLUSION_QUERIES\nin float a_occlusion_query_opacity;\n#endif\n#ifdef ELEVATED_ROADS\nin vec3 a_x_axis;in vec3 a_y_axis;uniform float u_normal_scale;\n#endif\n#ifdef INDICATOR_CUTOUT\nout highp float v_z_offset;\n#else\n#ifdef RENDER_SHADOWS\nout highp float v_z_offset;\n#endif\n#endif\nuniform bool u_is_size_zoom_constant;uniform bool u_is_size_feature_constant;uniform highp float u_size_t;uniform highp float u_size;uniform mat4 u_matrix;uniform mat4 u_inv_matrix;uniform mat4 u_label_plane_matrix;uniform mat4 u_coord_matrix;uniform bool u_is_text;uniform bool u_elevation_from_sea;uniform bool u_pitch_with_map;uniform bool u_rotate_symbol;uniform highp float u_aspect_ratio;uniform highp float u_camera_to_center_distance;uniform float u_fade_change;uniform vec2 u_texsize;uniform vec3 u_up_vector;uniform vec2 u_texsize_icon;uniform bool u_is_halo;\n#ifdef PROJECTION_GLOBE_VIEW\nuniform vec3 u_tile_id;uniform mat4 u_inv_rot_matrix;uniform vec2 u_merc_center;uniform vec3 u_camera_forward;uniform float u_zoom_transition;uniform vec3 u_ecef_origin;uniform mat4 u_tile_matrix;\n#endif\nout vec2 v_tex_a;\n#ifdef ICON_TRANSITION\nout vec2 v_tex_b;\n#endif\nout float v_draw_halo;out vec3 v_gamma_scale_size_fade_opacity;\n#ifdef RENDER_TEXT_AND_SYMBOL\nout float is_sdf;out vec2 v_tex_a_icon;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out highp float v_depth;\n#endif\n#pragma mapbox: define highp vec4 fill_color\n#pragma mapbox: define highp vec4 halo_color\n#pragma mapbox: define lowp float opacity\n#pragma mapbox: define lowp float halo_width\n#pragma mapbox: define lowp float halo_blur\n#pragma mapbox: define lowp float emissive_strength\n#pragma mapbox: define lowp float occlusion_opacity\n#pragma mapbox: define lowp float z_offset\nvoid main() {\n#pragma mapbox: initialize highp vec4 fill_color\n#pragma mapbox: initialize highp vec4 halo_color\n#pragma mapbox: initialize lowp float opacity\n#pragma mapbox: initialize lowp float halo_width\n#pragma mapbox: initialize lowp float halo_blur\n#pragma mapbox: initialize lowp float emissive_strength\n#pragma mapbox: initialize lowp float occlusion_opacity\n#pragma mapbox: initialize lowp float z_offset\nvec2 a_pos=a_pos_offset.xy;vec2 a_offset=a_pos_offset.zw;vec2 a_tex=a_tex_size.xy;vec2 a_size=a_tex_size.zw;float a_size_min=floor(a_size[0]*0.5);float a_size_max= floor(a_size[1]*0.5);float a_apperance_icon=a_size[1]-2.0*a_size_max;vec2 a_pxoffset=a_pixeloffset.xy;vec2 a_min_font_scale=a_pixeloffset.zw/256.0;highp float segment_angle=-a_projected_pos[3];float size;if (a_apperance_icon==APPEARANCE_ICON) {size=a_size_max/128.0;} else if (!u_is_size_zoom_constant && !u_is_size_feature_constant) {size=mix(a_size_min,a_size_max,u_size_t)/128.0;} else if (u_is_size_zoom_constant && !u_is_size_feature_constant) {size=a_size_min/128.0;} else {size=u_size;}vec2 tile_anchor=a_pos;float e=u_elevation_from_sea ? z_offset : z_offset+elevation(tile_anchor);\n#ifdef Z_OFFSET\ne+=a_auto_z_offset;\n#endif\nvec3 h=elevationVector(tile_anchor)*e;float globe_occlusion_fade;vec3 world_pos;vec3 mercator_pos;vec3 world_pos_globe;\n#ifdef PROJECTION_GLOBE_VIEW\nmercator_pos=mercator_tile_position(u_inv_rot_matrix,tile_anchor,u_tile_id,u_merc_center);world_pos_globe=a_globe_anchor+h;world_pos=mix_globe_mercator(world_pos_globe,mercator_pos,u_zoom_transition);vec4 ecef_point=u_tile_matrix*vec4(world_pos,1.0);vec3 origin_to_point=ecef_point.xyz-u_ecef_origin;globe_occlusion_fade=dot(origin_to_point,u_camera_forward) >=0.0 ? 0.0 : 1.0;\n#else\nworld_pos=vec3(tile_anchor,0)+h;globe_occlusion_fade=1.0;\n#endif\nvec4 projected_point=u_matrix*vec4(world_pos,1);highp float camera_to_anchor_distance=projected_point.w;highp float distance_ratio=u_pitch_with_map ?\ncamera_to_anchor_distance/u_camera_to_center_distance :\nu_camera_to_center_distance/camera_to_anchor_distance;highp float perspective_ratio=clamp(\n0.5+0.5*distance_ratio,0.0,1.5);size*=perspective_ratio;float font_scale=u_is_text ? size/24.0 : size;highp float symbol_rotation=0.0;if (u_rotate_symbol) {vec4 offsetprojected_point;vec2 a;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 displacement=vec3(a_globe_normal.z,0,-a_globe_normal.x);offsetprojected_point=u_matrix*vec4(a_globe_anchor+displacement,1);vec4 projected_point_globe=u_matrix*vec4(world_pos_globe,1);a=projected_point_globe.xy/projected_point_globe.w;\n#else\noffsetprojected_point=u_matrix*vec4(tile_anchor+vec2(1,0),0,1);a=projected_point.xy/projected_point.w;\n#endif\nvec2 b=offsetprojected_point.xy/offsetprojected_point.w;symbol_rotation=atan((b.y-a.y)/u_aspect_ratio,b.x-a.x);}vec4 projected_pos;\n#ifdef PROJECTION_GLOBE_VIEW\n#ifdef PROJECTED_POS_ON_VIEWPORT\nprojected_pos=u_label_plane_matrix*vec4(a_projected_pos.xyz+h,1.0);\n#else\nvec3 proj_pos=mix_globe_mercator(a_projected_pos.xyz,mercator_pos,u_zoom_transition)+h;projected_pos=u_label_plane_matrix*vec4(proj_pos,1.0); \n#endif\n#else\nprojected_pos=u_label_plane_matrix*vec4(a_projected_pos.xy,h.z,1.0);\n#endif\nhighp float angle_sin=sin(segment_angle+symbol_rotation);highp float angle_cos=cos(segment_angle+symbol_rotation);mat2 rotation_matrix=mat2(angle_cos,-1.0*angle_sin,angle_sin,angle_cos);float z=0.0;vec2 offset=rotation_matrix*(a_offset/32.0*max(a_min_font_scale,font_scale)+a_pxoffset/16.0);\n#ifdef TERRAIN\n#ifdef PITCH_WITH_MAP_TERRAIN\nvec4 tile_pos=u_label_plane_matrix_inv*vec4(a_projected_pos.xy+offset,0.0,1.0);z=elevation(tile_pos.xy);\n#endif\n#endif\n#ifdef Z_OFFSET\nz+=u_pitch_with_map ? a_auto_z_offset+z_offset : 0.0;\n#else\nz+=u_pitch_with_map ? z_offset : 0.0;\n#endif\nfloat occlusion_fade=globe_occlusion_fade;vec2 fade_opacity=unpack_opacity(a_fade_opacity);float fade_change=fade_opacity[1] > 0.5 ? u_fade_change :-u_fade_change;float out_fade_opacity=max(0.0,min(occlusion_fade,fade_opacity[0]+fade_change));\n#ifdef DEPTH_OCCLUSION\nfloat depth_occlusion=occlusionFadeMultiSample(projected_point);float depth_occlusion_multplier=mix(occlusion_opacity,1.0,depth_occlusion);out_fade_opacity*=depth_occlusion_multplier;\n#endif\n#ifdef OCCLUSION_QUERIES\nfloat occludedFadeMultiplier=mix(occlusion_opacity,1.0,a_occlusion_query_opacity);out_fade_opacity*=occludedFadeMultiplier;\n#endif\n#ifdef Z_TEST_OCCLUSION\nout_fade_opacity*=occlusion_opacity;\n#endif\nfloat alpha=opacity*out_fade_opacity;float hidden=float(alpha==0.0 || projected_point.w <=0.0 || occlusion_fade==0.0);vec3 pos;\n#ifdef PROJECTION_GLOBE_VIEW\nvec3 xAxis=u_pitch_with_map ? normalize(cross(a_globe_normal,u_up_vector)) : vec3(1,0,0);vec3 yAxis=u_pitch_with_map ? normalize(cross(a_globe_normal,xAxis)) : vec3(0,1,0);pos=projected_pos.xyz/projected_pos.w+xAxis*offset.x+yAxis*offset.y;\n#else\n#ifdef ELEVATED_ROADS\nvec3 xAxis=vec3(a_x_axis.xy,a_x_axis.z*u_normal_scale);vec3 yAxis=vec3(a_y_axis.xy,a_y_axis.z*u_normal_scale);pos=projected_pos.xyz/projected_pos.w+xAxis*offset.x+yAxis*offset.y;\n#else\npos=vec3(projected_pos.xy/projected_pos.w+offset,z);\n#endif\n#endif\ngl_Position=mix(u_coord_matrix*vec4(pos,1.0),AWAY,hidden);float gamma_scale=gl_Position.w;v_draw_halo=(u_is_halo && float(gl_InstanceID)==0.0) ? 1.0 : 0.0;v_gamma_scale_size_fade_opacity=vec3(gamma_scale,size,out_fade_opacity);v_tex_a=a_tex/u_texsize;\n#ifdef RENDER_TEXT_AND_SYMBOL\nis_sdf=a_size[0]-2.0*a_size_min;v_tex_a_icon=a_tex/u_texsize_icon;\n#endif\n#ifdef ICON_TRANSITION\nv_tex_b=a_texb/u_texsize;\n#endif\n#ifdef RENDER_SHADOWS\nvec4 shd_pos=u_inv_matrix*vec4(pos,1.0);vec3 shd_pos0=shd_pos.xyz;vec3 shd_pos1=shd_pos.xyz;\n#ifdef NORMAL_OFFSET\nvec3 shd_pos_offset=shadow_normal_offset(vec3(0.0,0.0,1.0));shd_pos0+=shd_pos_offset*shadow_normal_offset_multiplier0();shd_pos1+=shd_pos_offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef INDICATOR_CUTOUT\nv_z_offset=e;\n#else\n#ifdef RENDER_SHADOWS\nv_z_offset=e;\n#endif\n#endif\n}";
var skyboxFrag = "#include \"_prelude_fog.fragment.glsl\"\nin lowp vec3 v_uv;uniform lowp samplerCube u_cubemap;uniform lowp float u_opacity;uniform highp float u_temporal_offset;uniform highp vec3 u_sun_direction;float sun_disk(highp vec3 ray_direction,highp vec3 sun_direction) {highp float cos_angle=dot(normalize(ray_direction),sun_direction);const highp float cos_sun_angular_diameter=0.99996192306;const highp float smoothstep_delta=1e-5;return smoothstep(\ncos_sun_angular_diameter-smoothstep_delta,cos_sun_angular_diameter+smoothstep_delta,cos_angle);}float map(float value,float start,float end,float new_start,float new_end) {return ((value-start)*(new_end-new_start))/(end-start)+new_start;}void main() {vec3 uv=v_uv;const float y_bias=0.015;uv.y+=y_bias;uv.y=pow(abs(uv.y),1.0/5.0);uv.y=map(uv.y,0.0,1.0,-1.0,1.0);vec3 sky_color=texture(u_cubemap,uv).rgb;\n#ifdef FOG\nsky_color=fog_apply_sky_gradient(v_uv.xzy,sky_color);\n#endif\nsky_color+=0.1*sun_disk(v_uv,u_sun_direction);glFragColor=vec4(sky_color*u_opacity,u_opacity);\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\n}";
var skyboxGradientFrag = "#include \"_prelude_fog.fragment.glsl\"\nin highp vec3 v_uv;uniform lowp sampler2D u_color_ramp;uniform highp vec3 u_center_direction;uniform lowp float u_radius;uniform lowp float u_opacity;uniform highp float u_temporal_offset;void main() {float progress=acos(dot(normalize(v_uv),u_center_direction))/u_radius;vec4 color=texture(u_color_ramp,vec2(progress,0.5));\n#ifdef FOG\ncolor.rgb=fog_apply_sky_gradient(v_uv.xzy,color.rgb/color.a)*color.a;\n#endif\ncolor*=u_opacity;glFragColor=color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\n}";
var skyboxVert = "in highp vec3 a_pos_3f;uniform lowp mat4 u_matrix;out highp vec3 v_uv;void main() {const mat3 half_neg_pi_around_x=mat3(1.0,0.0, 0.0,0.0,0.0,-1.0,0.0,1.0, 0.0);v_uv=half_neg_pi_around_x*a_pos_3f;vec4 pos=u_matrix*vec4(a_pos_3f,1.0);gl_Position=pos.xyww;}";
var terrainRasterFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform sampler2D u_image0;\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nuniform sampler2D u_image1;uniform float u_emissive_texture_available;\n#endif\nin vec2 v_pos0;\n#ifdef FOG\nin float v_fog_opacity;\n#endif\n#ifdef RENDER_SHADOWS\nin vec4 v_pos_light_view_0;in vec4 v_pos_light_view_1;\n#endif\nuniform vec3 u_ground_shadow_factor;void main() {vec4 image_color=texture(u_image0,v_pos0);vec4 color;\n#ifdef LIGHTING_3D_MODE\nconst vec3 normal=vec3(0.0,0.0,1.0);\n#ifdef RENDER_SHADOWS\nfloat cutoffOpacity=1.0;\n#ifdef RENDER_CUTOFF\ncutoffOpacity=cutoff_opacity(u_cutoff_params,1.0/gl_FragCoord.w);\n#endif\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nfloat emissive_strength=u_emissive_texture_available > 0.5 ? texture(u_image1,v_pos0).r : image_color.a;vec3 unlit_base=image_color.rgb*(1.0-emissive_strength);vec3 emissive_base=image_color.rgb*emissive_strength;float ndotl=u_shadow_direction.z;float occlusion=ndotl < 0.0 ? 1.0 : shadow_occlusion(v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w,0.0);ndotl=max(0.0,ndotl);vec3 lit=apply_lighting(unlit_base,normal,mix(1.0,(1.0-(u_shadow_intensity*occlusion))*ndotl,cutoffOpacity));vec3 emissive=compute_emissive_draped(emissive_base,1.0-u_shadow_intensity,occlusion,u_ground_shadow_factor);color.rgb=lit+emissive;color.a=1.0;\n#else\nfloat lighting_factor=shadowed_light_factor_normal_unbiased(normal,v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w);color=apply_lighting(image_color,normal,mix(1.0,lighting_factor,cutoffOpacity));\n#endif\n#else\nfloat lighting_factor=u_lighting_directional_dir.z;color=apply_lighting(image_color,normal,lighting_factor);\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nfloat emissive_strength=u_emissive_texture_available > 0.5 ? texture(u_image1,v_pos0).r : image_color.a;color.rgb=mix(color.rgb,image_color.rgb,emissive_strength);color.a=1.0;\n#endif\n#endif\n#else\ncolor=image_color;\n#endif\n#ifdef FOG\n#ifdef ZERO_EXAGGERATION\ncolor=fog_dither(fog_apply_premultiplied(color,v_fog_pos));\n#else\ncolor=fog_dither(fog_apply_from_vert(color,v_fog_opacity));\n#endif\n#endif\nglFragColor=color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var terrainRasterVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\nuniform mat4 u_matrix;uniform float u_skirt_height;in vec2 a_pos;out vec2 v_pos0;\n#ifdef FOG\nout float v_fog_opacity;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out vec4 v_pos_light_view_0;out vec4 v_pos_light_view_1;\n#endif\nvoid main() {vec3 decomposedPosAndSkirt=decomposeToPosAndSkirt(a_pos);float skirt=decomposedPosAndSkirt.z;vec2 decodedPos=decomposedPosAndSkirt.xy;float elevation=elevation(decodedPos)-skirt*u_skirt_height;v_pos0=decodedPos/8192.0;gl_Position=u_matrix*vec4(decodedPos,elevation,1.0);\n#ifdef FOG\n#ifdef ZERO_EXAGGERATION\nv_fog_pos=fog_position(decodedPos);\n#else\nv_fog_opacity=fog(fog_position(vec3(decodedPos,elevation)));\n#endif\n#endif\n#ifdef RENDER_SHADOWS\nvec3 pos=vec3(decodedPos,elevation);v_pos_light_view_0=u_light_matrix_0*vec4(pos,1.);v_pos_light_view_1=u_light_matrix_1*vec4(pos,1.);\n#endif\n}";
var terrainDepthFrag = "precision highp float;in float v_depth;void main() {glFragColor=pack_depth(v_depth);}";
var terrainDepthVert = "#include \"_prelude_terrain.vertex.glsl\"\nuniform mat4 u_matrix;in vec2 a_pos;out float v_depth;void main() {float elevation=elevation(a_pos);gl_Position=u_matrix*vec4(a_pos,elevation,1.0);v_depth=gl_Position.z/gl_Position.w;}";
var preludeTerrainVert = "\n#define ELEVATION_SCALE 7.0\n#define ELEVATION_OFFSET 450.0\n#ifdef PROJECTION_GLOBE_VIEW\nuniform vec3 u_tile_tl_up;uniform vec3 u_tile_tr_up;uniform vec3 u_tile_br_up;uniform vec3 u_tile_bl_up;uniform float u_tile_up_scale;vec3 elevationVector(vec2 pos) {vec2 uv=pos/EXTENT;vec3 up=normalize(mix(\nmix(u_tile_tl_up,u_tile_tr_up,uv.xxx),mix(u_tile_bl_up,u_tile_br_up,uv.xxx),uv.yyy));return up*u_tile_up_scale;}\n#else\nvec3 elevationVector(vec2 pos) { return vec3(0,0,1); }\n#endif\n#ifdef TERRAIN\nuniform highp sampler2D u_dem;uniform highp sampler2D u_dem_prev;uniform vec2 u_dem_tl;uniform vec2 u_dem_tl_prev;uniform float u_dem_scale;uniform float u_dem_scale_prev;uniform float u_dem_size;uniform float u_dem_lerp;uniform float u_exaggeration;uniform float u_meter_to_dem;uniform mat4 u_label_plane_matrix_inv;vec4 tileUvToDemSample(vec2 uv,float dem_size,float dem_scale,vec2 dem_tl) {vec2 pos=dem_size*(uv*dem_scale+dem_tl)+1.0;vec2 f=fract(pos);return vec4((pos-f+0.5)/(dem_size+2.0),f);}float currentElevation(vec2 apos) {\n#ifdef TERRAIN_DEM_FLOAT_FORMAT\nvec2 pos=(u_dem_size*(apos/8192.0*u_dem_scale+u_dem_tl)+1.5)/(u_dem_size+2.0);return u_exaggeration*texture(u_dem,pos).r;\n#else\nfloat dd=1.0/(u_dem_size+2.0);vec4 r=tileUvToDemSample(apos/8192.0,u_dem_size,u_dem_scale,u_dem_tl);vec2 pos=r.xy;vec2 f=r.zw;float tl=texture(u_dem,pos).r;float tr=texture(u_dem,pos+vec2(dd,0)).r;float bl=texture(u_dem,pos+vec2(0,dd)).r;float br=texture(u_dem,pos+vec2(dd,dd)).r;return u_exaggeration*mix(mix(tl,tr,f.x),mix(bl,br,f.x),f.y);\n#endif\n}float prevElevation(vec2 apos) {\n#ifdef TERRAIN_DEM_FLOAT_FORMAT\nvec2 pos=(u_dem_size*(apos/8192.0*u_dem_scale_prev+u_dem_tl_prev)+1.5)/(u_dem_size+2.0);return u_exaggeration*texture(u_dem_prev,pos).r;\n#else\nfloat dd=1.0/(u_dem_size+2.0);vec4 r=tileUvToDemSample(apos/8192.0,u_dem_size,u_dem_scale_prev,u_dem_tl_prev);vec2 pos=r.xy;vec2 f=r.zw;float tl=texture(u_dem_prev,pos).r;float tr=texture(u_dem_prev,pos+vec2(dd,0)).r;float bl=texture(u_dem_prev,pos+vec2(0,dd)).r;float br=texture(u_dem_prev,pos+vec2(dd,dd)).r;return u_exaggeration*mix(mix(tl,tr,f.x),mix(bl,br,f.x),f.y);\n#endif\n}\n#ifdef TERRAIN_VERTEX_MORPHING\nfloat elevation(vec2 apos) {\n#ifdef ZERO_EXAGGERATION\nreturn 0.0;\n#endif\nfloat nextElevation=currentElevation(apos);float prevElevation=prevElevation(apos);return mix(prevElevation,nextElevation,u_dem_lerp);}\n#else\nfloat elevation(vec2 apos) {\n#ifdef ZERO_EXAGGERATION\nreturn 0.0;\n#endif\nreturn currentElevation(apos);}\n#endif\nvec4 fourSample(vec2 pos,vec2 off) {float tl=texture(u_dem,pos).r;float tr=texture(u_dem,pos+vec2(off.x,0.0)).r;float bl=texture(u_dem,pos+vec2(0.0,off.y)).r;float br=texture(u_dem,pos+off).r;return vec4(tl,tr,bl,br);}float flatElevation(vec2 pack) {vec2 apos=floor(pack/8.0);vec2 span=10.0*(pack-apos*8.0);vec2 uvTex=(apos-vec2(1.0,1.0))/8190.0;float size=u_dem_size+2.0;float dd=1.0/size;vec2 pos=u_dem_size*(uvTex*u_dem_scale+u_dem_tl)+1.0;vec2 f=fract(pos);pos=(pos-f+0.5)*dd;vec4 h=fourSample(pos,vec2(dd));float z=mix(mix(h.x,h.y,f.x),mix(h.z,h.w,f.x),f.y);vec2 w=floor(0.5*(span*u_meter_to_dem-1.0));vec2 d=dd*w;h=fourSample(pos-d,2.0*d+vec2(dd));vec4 diff=abs(h.xzxy-h.ywzw);vec2 slope=min(vec2(0.25),u_meter_to_dem*0.5*(diff.xz+diff.yw)/(2.0*w+vec2(1.0)));vec2 fix=slope*span;float base=z+max(fix.x,fix.y);return u_exaggeration*base;}float elevationFromUint16(float word) {return u_exaggeration*(word/ELEVATION_SCALE-ELEVATION_OFFSET);}\n#else\nfloat elevation(vec2 pos) { return 0.0; }\n#endif\n#ifdef DEPTH_OCCLUSION\nuniform highp sampler2D u_depth;uniform highp vec2 u_depth_size_inv;uniform highp vec2 u_depth_range_unpack;uniform highp float u_occluder_half_size;uniform highp float u_occlusion_depth_offset;\n#ifdef DEPTH_D24\nfloat unpack_depth(float depth) {return depth*u_depth_range_unpack.x+u_depth_range_unpack.y;}vec4 unpack_depth4(vec4 depth) {return depth*u_depth_range_unpack.x+vec4(u_depth_range_unpack.y);}\n#else\nhighp float unpack_depth_rgba(vec4 rgba_depth)\n{const highp vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);return dot(rgba_depth,bit_shift)*2.0-1.0;}\n#endif\nbool isOccluded(vec4 frag) {vec3 coord=frag.xyz/frag.w;\n#ifdef CLIP_ZERO_TO_ONE\ncoord.z=-1.0+2.0*coord.z; \n#endif\n#ifdef DEPTH_D24\nfloat depth=unpack_depth(texture(u_depth,(coord.xy+1.0)*0.5).r);\n#else\nfloat depth=unpack_depth_rgba(texture(u_depth,(coord.xy+1.0)*0.5));\n#endif\nreturn coord.z+u_occlusion_depth_offset > depth;}highp vec4 getCornerDepths(vec2 coord) {highp vec3 df=vec3(u_occluder_half_size*u_depth_size_inv,0.0);highp vec2 uv=0.5*coord.xy+0.5;\n#ifdef DEPTH_D24\nhighp vec4 depth=vec4(\ntexture(u_depth,uv-df.xz).r,texture(u_depth,uv+df.xz).r,texture(u_depth,uv-df.zy).r,texture(u_depth,uv+df.zy).r\n);depth=unpack_depth4(depth);\n#else\nhighp vec4 depth=vec4(\nunpack_depth_rgba(texture(u_depth,uv-df.xz)),unpack_depth_rgba(texture(u_depth,uv+df.xz)),unpack_depth_rgba(texture(u_depth,uv-df.zy)),unpack_depth_rgba(texture(u_depth,uv+df.zy))\n);\n#endif\nreturn depth;}highp float occlusionFadeMultiSample(vec4 frag) {highp vec3 coord=frag.xyz/frag.w;highp vec2 uv=0.5*coord.xy+0.5;\n#ifdef CLIP_ZERO_TO_ONE\ncoord.z=-1.0+2.0*coord.z; \n#endif\nint NX=3;int NY=4;highp vec2 df=u_occluder_half_size*u_depth_size_inv;highp vec2 oneStep=2.0*u_occluder_half_size*u_depth_size_inv/vec2(NX-1,NY-1);highp float res=0.0;for (int y=0; y < NY;++y) {for (int x=0; x < NX;++x) {\n#ifdef DEPTH_D24\nhighp float depth=unpack_depth(texture(u_depth,uv-df+vec2(float(x)*oneStep.x,float(y)*oneStep.y)).r);\n#else\nhighp float depth=unpack_depth_rgba(texture(u_depth,uv-df+vec2(float(x)*oneStep.x,float(y)*oneStep.y)));\n#endif\nres+=1.0-clamp(300.0*(coord.z+u_occlusion_depth_offset-depth),0.0,1.0);}}res=clamp(2.0*res/float(NX*NY)-0.5,0.0,1.0);return res;}highp float occlusionFade(vec4 frag) {highp vec3 coord=frag.xyz/frag.w;\n#ifdef CLIP_ZERO_TO_ONE\ncoord.z=-1.0+2.0*coord.z; \n#endif\nhighp vec4 depth=getCornerDepths(coord.xy);return dot(vec4(0.25),vec4(1.0)-clamp(300.0*(vec4(coord.z+u_occlusion_depth_offset)-depth),0.0,1.0));}\n#else\nbool isOccluded(vec4 frag) { return false; }highp float occlusionFade(vec4 frag) { return 1.0; }highp float occlusionFadeMultiSample(vec4 frag) { return 1.0; }\n#endif";
var preludeFogVert = "#ifdef FOG\nuniform mediump vec4 u_fog_color;uniform mediump vec2 u_fog_range;uniform mediump float u_fog_horizon_blend;uniform mediump mat4 u_fog_matrix;out vec3 v_fog_pos;float fog_range(float depth) {return (depth-u_fog_range[0])/(u_fog_range[1]-u_fog_range[0]);}float fog_horizon_blending(vec3 camera_dir) {float t=max(0.0,camera_dir.z/u_fog_horizon_blend);return u_fog_color.a*exp(-3.0*t*t);}float fog_opacity(float t) {const float decay=6.0;float falloff=1.0-min(1.0,exp(-decay*t));falloff*=falloff*falloff;return u_fog_color.a*min(1.0,1.00747*falloff);}vec3 fog_position(vec3 pos) {return (u_fog_matrix*vec4(pos,1.0)).xyz;}vec3 fog_position(vec2 pos) {return fog_position(vec3(pos,0.0));}float fog(vec3 pos) {float depth=length(pos);float opacity=fog_opacity(fog_range(depth));return opacity*fog_horizon_blending(pos/depth);}\n#endif";
var preludeFogFrag = "#ifdef FOG\nuniform mediump vec4 u_fog_color;uniform mediump vec2 u_fog_range;uniform mediump float u_fog_horizon_blend;uniform mediump vec2 u_fog_vertical_limit;uniform mediump float u_fog_temporal_offset;in vec3 v_fog_pos;uniform highp vec3 u_frustum_tl;uniform highp vec3 u_frustum_tr;uniform highp vec3 u_frustum_br;uniform highp vec3 u_frustum_bl;uniform highp vec3 u_globe_pos;uniform highp float u_globe_radius;uniform highp vec2 u_viewport;uniform float u_globe_transition;uniform int u_is_globe;float fog_range(float depth) {return (depth-u_fog_range[0])/(u_fog_range[1]-u_fog_range[0]);}float fog_horizon_blending(vec3 camera_dir) {float t=max(0.0,camera_dir.z/u_fog_horizon_blend);return u_fog_color.a*exp(-3.0*t*t);}float fog_opacity(float t) {const float decay=6.0;float falloff=1.0-min(1.0,exp(-decay*t));falloff*=falloff*falloff;return u_fog_color.a*min(1.0,1.00747*falloff);}float globe_glow_progress() {highp vec2 uv=gl_FragCoord.xy/u_viewport;\n#ifdef FLIP_Y\nuv.y=1.0-uv.y;\n#endif\nhighp vec3 ray_dir=mix(\nmix(u_frustum_tl,u_frustum_tr,uv.x),mix(u_frustum_bl,u_frustum_br,uv.x),1.0-uv.y);highp vec3 dir=normalize(ray_dir);highp vec3 closest_point=dot(u_globe_pos,dir)*dir;highp float sdf=length(closest_point-u_globe_pos)/u_globe_radius;return sdf+PI*0.5;}float fog_opacity(vec3 pos) {float depth=length(pos);return fog_opacity(fog_range(depth));}vec3 fog_apply(vec3 color,vec3 pos,float opacity_limit) {float depth=length(pos);float opacity;if (u_is_globe==1) {float glow_progress=globe_glow_progress();float t=mix(glow_progress,depth,u_globe_transition);opacity=fog_opacity(fog_range(t));} else {opacity=fog_opacity(fog_range(depth));opacity*=fog_horizon_blending(pos/depth);}return mix(color,u_fog_color.rgb,min(opacity,opacity_limit));}vec3 fog_apply(vec3 color,vec3 pos) {return fog_apply(color,pos,1.0);}vec4 fog_apply_from_vert(vec4 color,float fog_opac) {float alpha=EPSILON+color.a;color.rgb=mix(color.rgb/alpha,u_fog_color.rgb,fog_opac)*alpha;return color;}vec3 fog_apply_sky_gradient(vec3 camera_ray,vec3 sky_color) {float horizon_blend=fog_horizon_blending(normalize(camera_ray));return mix(sky_color,u_fog_color.rgb,horizon_blend);}vec4 fog_apply_premultiplied(vec4 color,vec3 pos) {float alpha=EPSILON+color.a;color.rgb=fog_apply(color.rgb/alpha,pos)*alpha;return color;}vec4 fog_apply_premultiplied(vec4 color,vec3 pos,float heightMeters) {float verticalProgress=(u_fog_vertical_limit.x > 0.0 || u_fog_vertical_limit.y > 0.0) ? smoothstep(u_fog_vertical_limit.x,u_fog_vertical_limit.y,heightMeters) : 0.0;float opacityLimit=1.0-smoothstep(0.9,1.0,fog_opacity(pos));return mix(fog_apply_premultiplied(color,pos),color,min(verticalProgress,opacityLimit));}vec3 fog_dither(vec3 color) {return color;}vec4 fog_dither(vec4 color) {return vec4(fog_dither(color.rgb),color.a);}\n#endif";
var preludeLighting = "\n#ifdef LIGHTING_3D_MODE\nuniform mediump vec3 u_lighting_ambient_color;uniform mediump vec3 u_lighting_directional_dir;uniform mediump vec3 u_lighting_directional_color;uniform mediump vec3 u_ground_radiance;float calculate_ambient_directional_factor(vec3 normal) {float NdotL=dot(normal,u_lighting_directional_dir);const float factor_reduction_max=0.3;float dir_luminance=dot(u_lighting_directional_color,vec3(0.2126,0.7152,0.0722));float directional_factor_min=1.0-factor_reduction_max*min(dir_luminance,1.0);float ambient_directional_factor=mix(directional_factor_min,1.0,min((NdotL+1.0),1.0));const float vertical_factor_min=0.92;float vertical_factor=mix(vertical_factor_min,1.0,normal.z*0.5+0.5);return vertical_factor*ambient_directional_factor;}vec3 linearProduct(vec3 srgbIn,vec3 k) {return srgbIn*pow(k,vec3(1./2.2));}vec3 apply_lighting(vec3 color,vec3 normal,float dir_factor) {float ambient_directional_factor=calculate_ambient_directional_factor(normal);vec3 ambient_contrib=ambient_directional_factor*u_lighting_ambient_color;vec3 directional_contrib=u_lighting_directional_color*dir_factor;return linearProduct(color,ambient_contrib+directional_contrib);}vec4 apply_lighting(vec4 color,vec3 normal,float dir_factor) {return vec4(apply_lighting(color.rgb,normal,dir_factor),color.a);}vec3 apply_lighting(vec3 color,vec3 normal) {float dir_factor=max(dot(normal,u_lighting_directional_dir),0.0);return apply_lighting(color.rgb,normal,dir_factor);}vec4 apply_lighting(vec4 color,vec3 normal) {float dir_factor=max(dot(normal,u_lighting_directional_dir),0.0);return vec4(apply_lighting(color.rgb,normal,dir_factor),color.a);}vec3 apply_lighting_ground(vec3 color) {return color*u_ground_radiance;}vec4 apply_lighting_ground(vec4 color) {return vec4(apply_lighting_ground(color.rgb),color.a);}float calculate_NdotL(vec3 normal) {const float ext=0.70710678118;return (clamp(dot(normal,u_lighting_directional_dir),-ext,1.0)+ext)/(1.0+ext);}vec4 apply_lighting_with_emission_ground(vec4 color,float emissive_strength) {return mix(apply_lighting_ground(color),color,emissive_strength);}vec3 compute_flood_lighting(vec3 flood_light_color,float fully_occluded_factor,float occlusion,vec3 ground_shadow_factor) {vec3 fully_occluded_color=flood_light_color*mix(ground_shadow_factor,vec3(1.0),fully_occluded_factor);float occlusion_ramp=smoothstep(0.0,0.2,1.0-occlusion);return mix(fully_occluded_color,flood_light_color,occlusion_ramp);}vec3 compute_emissive_draped(vec3 unlit_color,float fully_occluded_factor,float occlusion,vec3 ground_shadow_factor) {vec3 fully_occluded_color=unlit_color*mix(ground_shadow_factor,vec3(1.0),fully_occluded_factor);return mix(fully_occluded_color,unlit_color,1.0-occlusion);}\n#endif";
var preludeRasterArrayFrag = "#ifdef RASTER_ARRAY\nuniform highp sampler2D u_image0;uniform sampler2D u_image1;const vec4 NODATA=vec4(1);ivec4 _raTexLinearCoord(highp vec2 texCoord,highp vec2 texResolution,out highp vec2 fxy) {texCoord=texCoord*texResolution-0.5;fxy=fract(texCoord);texCoord-=fxy;return ivec4(texCoord.xxyy+vec2(1.5,0.5).xyxy);}vec2 _raTexLinearMix(highp vec2 fxy,highp vec4 colorMix,highp float colorOffset,highp vec4 t00,highp vec4 t10,highp vec4 t01,highp vec4 t11) {vec2 c00=t00==NODATA ? vec2(0) : vec2(colorOffset+dot(t00,colorMix),1);vec2 c10=t10==NODATA ? vec2(0) : vec2(colorOffset+dot(t10,colorMix),1);vec2 c01=t01==NODATA ? vec2(0) : vec2(colorOffset+dot(t01,colorMix),1);vec2 c11=t11==NODATA ? vec2(0) : vec2(colorOffset+dot(t11,colorMix),1);return mix(mix(c01,c11,fxy.x),mix(c00,c10,fxy.x),fxy.y);}vec2 raTexture2D_image0_linear(highp vec2 texCoord,highp vec2 texResolution,highp vec4 colorMix,highp float colorOffset) {vec2 fxy;ivec4 c=_raTexLinearCoord(texCoord,texResolution,fxy);return _raTexLinearMix(fxy,colorMix,colorOffset,texelFetch(u_image0,c.yz,0),texelFetch(u_image0,c.xz,0),texelFetch(u_image0,c.yw,0),texelFetch(u_image0,c.xw,0)\n);}vec2 raTexture2D_image1_linear(highp vec2 texCoord,highp vec2 texResolution,highp vec4 colorMix,highp float colorOffset) {vec2 fxy;ivec4 c=_raTexLinearCoord(texCoord,texResolution,fxy);return _raTexLinearMix(fxy,colorMix,colorOffset,texelFetch(u_image1,c.yz,0),texelFetch(u_image1,c.xz,0),texelFetch(u_image1,c.yw,0),texelFetch(u_image1,c.xw,0)\n);}vec2 raTexture2D_image0_nearest(highp vec2 texCoord,highp vec2 texResolution,highp vec4 colorMix,highp float colorOffset) {vec4 t=texelFetch(u_image0,ivec2(texCoord*texResolution),0);return t==NODATA ? vec2(0) : vec2(colorOffset+dot(t,colorMix),1);}vec2 raTexture2D_image1_nearest(highp vec2 texCoord,highp vec2 texResolution,highp vec4 colorMix,highp float colorOffset) {vec4 t=texelFetch(u_image1,ivec2(texCoord*texResolution),0);return t==NODATA ? vec2(0) : vec2(colorOffset+dot(t,colorMix),1);}\n#endif";
var preludeRasterParticleFrag = "#ifdef RASTER_ARRAY\nuniform sampler2D u_velocity;uniform mediump vec2 u_velocity_res;uniform mediump float u_max_speed;const vec4 NO_DATA=vec4(1);const vec2 INVALID_VELOCITY=vec2(-1);uniform highp vec2 u_uv_offset;uniform highp float u_data_offset;uniform highp vec2 u_data_scale;ivec4 rasterArrayLinearCoord(highp vec2 texCoord,highp vec2 texResolution,out highp vec2 fxy) {texCoord=texCoord*texResolution-0.5;fxy=fract(texCoord);texCoord-=fxy;return ivec4(texCoord.xxyy+vec2(1.5,0.5).xyxy);}highp vec2 lookup_velocity(highp vec2 uv) {uv=u_uv_offset.x+u_uv_offset.y*uv;highp vec2 fxy;ivec4 c=rasterArrayLinearCoord(uv,u_velocity_res,fxy);highp vec4 tl=texelFetch(u_velocity,c.yz,0);highp vec4 tr=texelFetch(u_velocity,c.xz,0);highp vec4 bl=texelFetch(u_velocity,c.yw,0);highp vec4 br=texelFetch(u_velocity,c.xw,0);if (tl==NO_DATA) {return INVALID_VELOCITY;}if (tr==NO_DATA) {return INVALID_VELOCITY;}if (bl==NO_DATA) {return INVALID_VELOCITY;}if (br==NO_DATA) {return INVALID_VELOCITY;}highp vec4 t=mix(mix(bl,br,fxy.x),mix(tl,tr,fxy.x),fxy.y);highp vec2 velocity=u_data_offset+vec2(dot(t.rg,u_data_scale),dot(t.ba,u_data_scale));velocity.y=-velocity.y;velocity/=max(u_max_speed,length(velocity));return velocity;}\n#endif\nuniform highp float u_particle_pos_scale;uniform highp vec2 u_particle_pos_offset;highp vec4 pack_pos_to_rgba(highp vec2 p) {highp vec2 v=(p+u_particle_pos_offset)/u_particle_pos_scale;highp vec4 r=vec4(v.x,fract(v.x*255.0),v.y,fract(v.y*255.0));return vec4(r.x-r.y/255.0,r.y,r.z-r.w/255.0,r.w);}highp vec2 unpack_pos_from_rgba(highp vec4 v) {v=floor(v*255.0+0.5)/255.0;highp vec2 p=vec2(v.x+(v.y/255.0),v.z+(v.w/255.0));return u_particle_pos_scale*p-u_particle_pos_offset;}";
var skyboxCaptureFrag = "\nin highp vec3 v_position;uniform highp float u_sun_intensity;uniform highp float u_luminance;uniform lowp vec3 u_sun_direction;uniform highp vec4 u_color_tint_r;uniform highp vec4 u_color_tint_m;precision highp float;\n#define BETA_R vec3(5.5e-6,13.0e-6,22.4e-6)\n#define BETA_M vec3(21e-6,21e-6,21e-6)\n#define MIE_G 0.76\n#define DENSITY_HEIGHT_SCALE_R 8000.0\n#define DENSITY_HEIGHT_SCALE_M 1200.0\n#define PLANET_RADIUS 6360e3\n#define ATMOSPHERE_RADIUS 6420e3\n#define SAMPLE_STEPS 10\n#define DENSITY_STEPS 4\nfloat ray_sphere_exit(vec3 orig,vec3 dir,float radius) {float a=dot(dir,dir);float b=2.0*dot(dir,orig);float c=dot(orig,orig)-radius*radius;float d=sqrt(b*b-4.0*a*c);return (-b+d)/(2.0*a);}vec3 extinction(vec2 density) {return exp(-vec3(BETA_R*u_color_tint_r.a*density.x+BETA_M*u_color_tint_m.a*density.y));}vec2 local_density(vec3 point) {float height=max(length(point)-PLANET_RADIUS,0.0);float exp_r=exp(-height/DENSITY_HEIGHT_SCALE_R);float exp_m=exp(-height/DENSITY_HEIGHT_SCALE_M);return vec2(exp_r,exp_m);}float phase_ray(float cos_angle) {return (3.0/(16.0*PI))*(1.0+cos_angle*cos_angle);}float phase_mie(float cos_angle) {return (3.0/(8.0*PI))*((1.0-MIE_G*MIE_G)*(1.0+cos_angle*cos_angle))/((2.0+MIE_G*MIE_G)*pow(1.0+MIE_G*MIE_G-2.0*MIE_G*cos_angle,1.5));}vec2 density_to_atmosphere(vec3 point,vec3 light_dir) {float ray_len=ray_sphere_exit(point,light_dir,ATMOSPHERE_RADIUS);float step_len=ray_len/float(DENSITY_STEPS);vec2 density_point_to_atmosphere=vec2(0.0);for (int i=0; i < DENSITY_STEPS;++i) {vec3 point_on_ray=point+light_dir*((float(i)+0.5)*step_len);density_point_to_atmosphere+=local_density(point_on_ray)*step_len;;}return density_point_to_atmosphere;}vec3 atmosphere(vec3 ray_dir,vec3 sun_direction,float sun_intensity) {vec2 density_orig_to_point=vec2(0.0);vec3 scatter_r=vec3(0.0);vec3 scatter_m=vec3(0.0);vec3 origin=vec3(0.0,PLANET_RADIUS,0.0);float ray_len=ray_sphere_exit(origin,ray_dir,ATMOSPHERE_RADIUS);float step_len=ray_len/float(SAMPLE_STEPS);for (int i=0; i < SAMPLE_STEPS;++i) {vec3 point_on_ray=origin+ray_dir*((float(i)+0.5)*step_len);vec2 density=local_density(point_on_ray)*step_len;density_orig_to_point+=density;vec2 density_point_to_atmosphere=density_to_atmosphere(point_on_ray,sun_direction);vec2 density_orig_to_atmosphere=density_orig_to_point+density_point_to_atmosphere;vec3 extinction=extinction(density_orig_to_atmosphere);scatter_r+=density.x*extinction;scatter_m+=density.y*extinction;}float cos_angle=dot(ray_dir,sun_direction);float phase_r=phase_ray(cos_angle);float phase_m=phase_mie(cos_angle);vec3 beta_r=BETA_R*u_color_tint_r.rgb*u_color_tint_r.a;vec3 beta_m=BETA_M*u_color_tint_m.rgb*u_color_tint_m.a;return (scatter_r*phase_r*beta_r+scatter_m*phase_m*beta_m)*sun_intensity;}const float A=0.15;const float B=0.50;const float C=0.10;const float D=0.20;const float E=0.02;const float F=0.30;vec3 uncharted2_tonemap(vec3 x) {return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;}void main() {vec3 ray_direction=v_position;ray_direction.y=pow(ray_direction.y,5.0);const float y_bias=0.015;ray_direction.y+=y_bias;vec3 color=atmosphere(normalize(ray_direction),u_sun_direction,u_sun_intensity);float white_scale=1.0748724675633854;color=uncharted2_tonemap((log2(2.0/pow(u_luminance,4.0)))*color)*white_scale;glFragColor=vec4(color,1.0);}";
var skyboxCaptureVert = "in highp vec3 a_pos_3f;uniform mat3 u_matrix_3f;out highp vec3 v_position;float map(float value,float start,float end,float new_start,float new_end) {return ((value-start)*(new_end-new_start))/(end-start)+new_start;}void main() {vec4 pos=vec4(u_matrix_3f*a_pos_3f,1.0);v_position=pos.xyz;v_position.y*=-1.0;v_position.y=map(v_position.y,-1.0,1.0,0.0,1.0);gl_Position=vec4(a_pos_3f.xy,0.0,1.0);}";
var globeFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform sampler2D u_image0;\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nuniform sampler2D u_image1;uniform float u_emissive_texture_available;\n#endif\nuniform float u_far_z_cutoff;in vec2 v_pos0;\n#ifndef FOG\nuniform highp vec3 u_frustum_tl;uniform highp vec3 u_frustum_tr;uniform highp vec3 u_frustum_br;uniform highp vec3 u_frustum_bl;uniform highp vec3 u_globe_pos;uniform highp float u_globe_radius;uniform vec2 u_viewport;\n#endif\nvoid main() {vec4 color;\n#ifdef CUSTOM_ANTIALIASING\nhighp vec2 uv=gl_FragCoord.xy/u_viewport;\n#ifdef FLIP_Y\nuv.y=1.0-uv.y;\n#endif\nhighp vec3 ray_dir=mix(\nmix(u_frustum_tl,u_frustum_tr,uv.x),mix(u_frustum_bl,u_frustum_br,uv.x),1.0-uv.y);highp vec3 dir=normalize(ray_dir);highp vec3 closest_point=dot(u_globe_pos,dir)*dir;highp float norm_dist_from_center=1.0-length(closest_point-u_globe_pos)/u_globe_radius;const float antialias_pixel=2.0;highp float antialias_factor=antialias_pixel*fwidth(norm_dist_from_center);highp float antialias=smoothstep(0.0,antialias_factor,norm_dist_from_center);vec4 raster=texture(u_image0,v_pos0);\n#ifdef LIGHTING_3D_MODE\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nfloat emissive_strength=u_emissive_texture_available > 0.5 ? texture(u_image1,v_pos0).r : raster.a;raster=apply_lighting_with_emission_ground(raster,emissive_strength);color=vec4(clamp(raster.rgb,vec3(0),vec3(1))*antialias,antialias);\n#else\nraster=apply_lighting_ground(raster);color=vec4(raster.rgb*antialias,raster.a*antialias);\n#endif\n#else\ncolor=vec4(raster.rgb*antialias,raster.a*antialias);\n#endif\n#else\ncolor=texture(u_image0,v_pos0);\n#ifdef LIGHTING_3D_MODE\n#ifdef LIGHTING_3D_ALPHA_EMISSIVENESS\nfloat emissive_strength=u_emissive_texture_available > 0.5 ? texture(u_image1,v_pos0).r : color.a;color=apply_lighting_with_emission_ground(color,emissive_strength);color.a=1.0;\n#else\ncolor=apply_lighting_ground(color);\n#endif\n#endif\n#endif\n#ifdef FOG\ncolor=fog_dither(fog_apply_premultiplied(color,v_fog_pos));\n#endif\ncolor*=1.0-step(u_far_z_cutoff,1.0/gl_FragCoord.w);glFragColor=color;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var globeVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_terrain.vertex.glsl\"\nuniform mat4 u_proj_matrix;uniform mat4 u_normalize_matrix;uniform mat4 u_globe_matrix;uniform mat4 u_merc_matrix;uniform float u_zoom_transition;uniform vec2 u_merc_center;uniform mat3 u_grid_matrix;uniform float u_skirt_height;\n#ifdef GLOBE_POLES\nin vec3 a_globe_pos;in vec2 a_uv;\n#else\nin vec2 a_pos;\n#endif\nout vec2 v_pos0;void main() {\n#ifdef GLOBE_POLES\nvec3 globe_pos=a_globe_pos;vec2 uv=a_uv;\n#else\nfloat tiles=u_grid_matrix[0][2];float idx=u_grid_matrix[1][2];float idy=u_grid_matrix[2][2];vec3 decomposed_pos_and_skirt=decomposeToPosAndSkirt(a_pos);vec3 latLng=u_grid_matrix*vec3(decomposed_pos_and_skirt.xy,1.0);float mercatorY=mercatorYfromLat(latLng[0]);float uvY=mercatorY*tiles-idy;float mercatorX=mercatorXfromLng(latLng[1]);float uvX=mercatorX*tiles-idx;vec3 globe_pos=latLngToECEF(latLng.xy);vec2 merc_pos=vec2(mercatorX,mercatorY);vec2 uv=vec2(uvX,uvY);\n#endif\nv_pos0=uv;vec2 tile_pos=uv*EXTENT;vec3 globe_derived_up_vector=normalize(globe_pos)*u_tile_up_scale;\n#ifdef GLOBE_POLES\nvec3 up_vector=globe_derived_up_vector;\n#else\nvec3 up_vector=elevationVector(tile_pos);\n#endif\nfloat height=elevation(tile_pos);globe_pos+=up_vector*height;\n#ifndef GLOBE_POLES\nglobe_pos-=globe_derived_up_vector*u_skirt_height*decomposed_pos_and_skirt.z;\n#endif\n#ifdef GLOBE_POLES\nvec4 interpolated_pos=u_globe_matrix*vec4(globe_pos,1.0);\n#else\nvec4 globe_world_pos=u_globe_matrix*vec4(globe_pos,1.0);vec4 merc_world_pos=vec4(0.0);if (u_zoom_transition > 0.0) {merc_world_pos=vec4(merc_pos,height-u_skirt_height*decomposed_pos_and_skirt.z,1.0);merc_world_pos.xy-=u_merc_center;merc_world_pos.x=wrap(merc_world_pos.x,-0.5,0.5);merc_world_pos=u_merc_matrix*merc_world_pos;}vec4 interpolated_pos=vec4(mix(globe_world_pos.xyz,merc_world_pos.xyz,u_zoom_transition),1.0);\n#endif\ngl_Position=u_proj_matrix*interpolated_pos;\n#ifdef FOG\nv_fog_pos=fog_position((u_normalize_matrix*vec4(globe_pos,1.0)).xyz);\n#endif\n}";
var atmosphereFrag = "#include \"_prelude_fog.fragment.glsl\"\nuniform float u_transition;uniform highp float u_fadeout_range;uniform highp float u_temporal_offset;uniform vec4 u_atmosphere_fog_color;uniform vec4 u_high_color;uniform vec4 u_space_color;uniform float u_horizon_angle;in highp vec3 v_ray_dir;in highp vec3 v_horizon_dir;void main() {highp vec3 dir=normalize(v_ray_dir);float globe_pos_dot_dir;\n#ifdef PROJECTION_GLOBE_VIEW\nglobe_pos_dot_dir=dot(u_globe_pos,dir);highp vec3 closest_point_forward=abs(globe_pos_dot_dir)*dir;float norm_dist_from_center=length(closest_point_forward-u_globe_pos)/u_globe_radius;if (norm_dist_from_center < 0.98) {\n#ifdef ALPHA_PASS\nglFragColor=vec4(0,0,0,0);return;\n#else\n#ifdef NATIVE\nglFragColor=vec4(1,1,1,1);\n#else\nglFragColor=vec4(0,0,0,1);\n#endif\nreturn;\n#endif\n}\n#endif\nhighp vec3 horizon_dir=normalize(v_horizon_dir);float horizon_angle_mercator=dir.y < horizon_dir.y ?\n0.0 : max(acos(clamp(dot(dir,horizon_dir),-1.0,1.0)),0.0);float horizon_angle;\n#ifdef PROJECTION_GLOBE_VIEW\nhighp vec3 closest_point=globe_pos_dot_dir*dir;highp float closest_point_to_center=length(closest_point-u_globe_pos);highp float theta=asin(clamp(closest_point_to_center/length(u_globe_pos),-1.0,1.0));horizon_angle=globe_pos_dot_dir < 0.0 ?\nPI-theta-u_horizon_angle : theta-u_horizon_angle;float angle_t=pow(u_transition,10.0);horizon_angle=mix(horizon_angle,horizon_angle_mercator,angle_t);\n#else\nhorizon_angle=horizon_angle_mercator;\n#endif\nhorizon_angle/=PI;float t=exp(-horizon_angle/u_fadeout_range);float alpha_0=u_atmosphere_fog_color.a;float alpha_1=u_high_color.a;float alpha_2=u_space_color.a;vec3 color_stop_0=u_atmosphere_fog_color.rgb;vec3 color_stop_1=u_high_color.rgb;vec3 color_stop_2=u_space_color.rgb;\n#ifdef ALPHA_PASS\nfloat a0=mix(alpha_2,1.0,alpha_1);float a1=mix(a0,1.0,alpha_0);float a2=mix(a0,a1,t);float a =mix(alpha_2,a2,t);glFragColor=vec4(1.0,1.0,1.0,a);\n#else\nvec3 c0=mix(color_stop_2,color_stop_1,alpha_1);vec3 c1=mix(c0,color_stop_0,alpha_0);vec3 c2=mix(c0,c1,t);vec3 c=c2;glFragColor=vec4(c*t,t);\n#endif\n}";
var atmosphereVert = "in vec3 a_pos;in vec2 a_uv;uniform vec3 u_frustum_tl;uniform vec3 u_frustum_tr;uniform vec3 u_frustum_br;uniform vec3 u_frustum_bl;uniform float u_horizon;out highp vec3 v_ray_dir;out highp vec3 v_horizon_dir;void main() {v_ray_dir=mix(\nmix(u_frustum_tl,u_frustum_tr,a_uv.x),mix(u_frustum_bl,u_frustum_br,a_uv.x),a_uv.y);v_horizon_dir=mix(\nmix(u_frustum_tl,u_frustum_bl,u_horizon),mix(u_frustum_tr,u_frustum_br,u_horizon),a_uv.x);gl_Position=vec4(a_pos,1.0);}";
var starsFrag = "in highp vec2 v_uv;in mediump float v_intensity;float shapeCircle(in vec2 uv)\n{float beginFade=0.6;float lengthFromCenter=length(v_uv);return 1.0-clamp((lengthFromCenter-beginFade)/(1.0-beginFade),0.0,1.0);}void main() {float alpha=shapeCircle(v_uv);vec3 color=vec3(1.0,1.0,1.0);alpha*=v_intensity;glFragColor=vec4(color*alpha,alpha);HANDLE_WIREFRAME_DEBUG;}";
var starsVert = "\nin vec3 a_pos_3f;in vec2 a_uv;in float a_size_scale;in float a_fade_opacity;uniform mat4 u_matrix;uniform vec3 u_up;uniform vec3 u_right;uniform float u_intensity_multiplier;out highp vec2 v_uv;out mediump float v_intensity;void main() {v_uv=a_uv;v_intensity=a_fade_opacity*u_intensity_multiplier;vec3 pos=a_pos_3f;pos+=a_uv.x*u_right*a_size_scale;pos+=a_uv.y*u_up*a_size_scale;gl_Position=u_matrix*vec4(pos,1.0);}";
var snowFrag = "in highp vec2 uv;in highp float alphaMultiplier;uniform vec4 u_particleColor;uniform vec2 u_simpleShapeParameters;void main() {float t=clamp((length(uv)-u_simpleShapeParameters.x)/(1.0-u_simpleShapeParameters.x),0.0,1.0);float alpha=1.0-pow(t,pow(10.0,u_simpleShapeParameters.y));alpha*=alphaMultiplier;alpha*=u_particleColor.a;vec3 color=u_particleColor.rgb*alpha;glFragColor=vec4(color,alpha) ;HANDLE_WIREFRAME_DEBUG;}";
var snowVert = "\nin highp vec3 a_pos_3f;in highp vec2 a_uv;in highp vec4 a_snowParticleData;in highp vec4 a_snowParticleDataHorizontalOscillation;uniform mat4 u_modelview;uniform mat4 u_projection;uniform vec3 u_cam_pos;uniform vec2 u_screenSize;uniform float u_time;uniform float u_boxSize;uniform float u_velocityConeAperture; \nuniform float u_velocity;uniform vec3 u_direction;uniform float u_horizontalOscillationRadius; \nuniform float u_horizontalOscillationRate; \nuniform float u_billboardSize;uniform vec2 u_thinningCenterPos;uniform vec3 u_thinningShape;uniform float u_thinningAffectedRatio;uniform float u_thinningParticleOffset;out highp vec2 uv;out highp float alphaMultiplier;void main() {vec3 pos=a_pos_3f;float halfBoxSize=0.5*u_boxSize;pos.xyz*=halfBoxSize;pos+=u_cam_pos;float velocityConeApertureRad=radians(u_velocityConeAperture*0.5);float coneAnglePichRad=velocityConeApertureRad*a_snowParticleData.z;float coneAngleHeadingRad=a_snowParticleData.w*radians(360.0);vec3 localZ=normalize(u_direction);vec3 localX=normalize(cross(localZ,vec3(1,0,0)));vec3 localY=normalize(cross(localZ,localX));vec3 direction;direction.x=cos(coneAngleHeadingRad)*sin(coneAnglePichRad);direction.y=sin(coneAngleHeadingRad)*sin(coneAnglePichRad);direction.z=cos(coneAnglePichRad);direction=normalize(direction);vec3 simPosLocal=vec3(0,0,0);float velocityScale=(1.0+3.0*a_snowParticleData.y)*u_velocity;simPosLocal+=direction*velocityScale*u_time;float horizontalOscillationRadius=u_horizontalOscillationRadius*a_snowParticleDataHorizontalOscillation.x;float horizontalOscillationAngle=u_horizontalOscillationRate*u_time*(-1.0+2.0*a_snowParticleDataHorizontalOscillation.y);simPosLocal.xy+=horizontalOscillationRadius*vec2(cos(horizontalOscillationAngle),sin(horizontalOscillationAngle));vec3 simPos=localX*simPosLocal.x+\nlocalY*simPosLocal.y+localZ*simPosLocal.z;pos+=simPos;pos=fract((pos+vec3(halfBoxSize))/vec3(u_boxSize))*u_boxSize-vec3(halfBoxSize);float clipZ=-u_cam_pos.z+pos.z;vec4 posView=u_modelview*vec4(pos,1.0);float size=u_billboardSize;alphaMultiplier=1.0;vec4 posScreen=u_projection*posView;posScreen/=posScreen.w;posScreen.xy=vec2(0.5)+posScreen.xy*0.5;posScreen.xy*=u_screenSize;vec2 thinningCenterPos=u_thinningCenterPos.xy;thinningCenterPos.y=u_screenSize.y-thinningCenterPos.y;float screenDist=length((thinningCenterPos-posScreen.xy)/(0.5*u_screenSize));screenDist+=a_snowParticleData.x*u_thinningParticleOffset;float scaleFactorMode=0.0;float thinningShapeDist=u_thinningShape.x+u_thinningShape.y;if (screenDist < thinningShapeDist) {float thinningFadeRatio=clamp((screenDist-u_thinningShape.x)/u_thinningShape.y,0.0,1.0);thinningFadeRatio=pow(thinningFadeRatio,u_thinningShape.z);if (a_snowParticleData.x < u_thinningAffectedRatio) {scaleFactorMode=1.0-thinningFadeRatio;alphaMultiplier=thinningFadeRatio;}}vec4 posScreen1=u_projection*vec4(posView.x-size,posView.yzw);posScreen1/=posScreen1.w;vec4 posScreen2=u_projection*vec4(posView.x+size,posView.yzw);posScreen2/=posScreen2.w;posScreen1.xy=vec2(0.5)+posScreen1.xy*0.5;posScreen1.xy*=u_screenSize;posScreen2.xy=vec2(0.5)+posScreen2.xy*0.5;posScreen2.xy*=u_screenSize;float screenLength=length(posScreen1.xy-posScreen2.xy);float screenEpsilon=3.0;float scaleFactor=1.0;if (screenLength < screenEpsilon) {scaleFactor=screenEpsilon/max(screenLength,0.01);scaleFactor=mix(scaleFactor,1.0,scaleFactorMode);}float screenEpsilon2=15.0;if (screenLength > screenEpsilon2) {scaleFactor=screenEpsilon2/max(screenLength,0.01);}size*=scaleFactor;vec2 right=size*vec2(1,0);vec2 up=size*vec2(0,1);posView.xy+=right*a_uv.x;posView.xy+=up*a_uv.y;uv=a_uv;gl_Position=u_projection*posView;}";
var rainFrag = "in highp vec2 uv;in highp float particleRandomValue;uniform sampler2D u_texScreen;uniform float u_distortionStrength;uniform vec4 u_color;uniform vec2 u_thinningCenterPos;uniform vec3 u_thinningShape;uniform float u_thinningAffectedRatio;uniform float u_thinningParticleOffset;uniform float u_shapeDirectionalPower;uniform float u_mode;void main() {vec2 st=uv*0.5+vec2(0.5);vec2 uvm=uv;uvm.y=-1.0+2.0*pow(st.y,u_shapeDirectionalPower);float shape=clamp(1.0-length(uvm),0.0,1.0);float alpha=abs(shape)*u_color.a;vec2 screenSize=vec2(textureSize(u_texScreen,0));vec2 thinningCenterPos=u_thinningCenterPos.xy;thinningCenterPos.y=screenSize.y-thinningCenterPos.y;float screenDist=length((thinningCenterPos-gl_FragCoord.xy)/(0.5*screenSize));screenDist+=(0.5+0.5*particleRandomValue)*u_thinningParticleOffset;float thinningShapeDist=u_thinningShape.x+u_thinningShape.y;float thinningAlpha=1.0;if (screenDist < thinningShapeDist) {float thinningFadeRatio=clamp((screenDist-u_thinningShape.x)/u_thinningShape.y,0.0,1.0);thinningFadeRatio=pow(thinningFadeRatio,u_thinningShape.z);thinningAlpha*=thinningFadeRatio;}vec2 offsetXY=normalize(uvm)*abs(shape);vec2 stScreen=(gl_FragCoord.xy+offsetXY*u_distortionStrength*thinningAlpha)/screenSize;vec3 colorScreen=texture(u_texScreen,stScreen).rgb;alpha*=thinningAlpha;glFragColor=mix(vec4(colorScreen,1.0),vec4(u_color.rgb*alpha,alpha),u_mode);HANDLE_WIREFRAME_DEBUG;}";
var rainVert = "\nin highp vec3 a_pos_3f;in highp vec2 a_uv;in highp vec4 a_rainParticleData;uniform mat4 u_modelview;uniform mat4 u_projection;uniform vec3 u_cam_pos;uniform float u_time;uniform float u_boxSize;uniform float u_velocityConeAperture; \nuniform float u_velocity; \nuniform vec2 u_rainDropletSize;uniform vec3 u_rainDirection;out highp vec2 uv;out highp float particleRandomValue;void main() {vec3 pos=a_pos_3f;float halfBoxSize=0.5*u_boxSize;pos*=halfBoxSize; \npos+=u_cam_pos;float velocityConeApertureRad=radians(u_velocityConeAperture*0.5);float coneAnglePichRad=velocityConeApertureRad*a_rainParticleData.z;float coneAngleHeadingRad=a_rainParticleData.w*radians(360.0);vec3 localZ=normalize(u_rainDirection);vec3 localX=normalize(cross(localZ,vec3(1,0,0)));vec3 localY=normalize(cross(localZ,localX));vec3 directionLocal;directionLocal.x=cos(coneAngleHeadingRad)*sin(coneAnglePichRad);directionLocal.y=sin(coneAngleHeadingRad)*sin(coneAnglePichRad);directionLocal.z=cos(coneAnglePichRad);directionLocal=normalize(directionLocal);vec3 directionWorld=localX*directionLocal.x+localY*directionLocal.y+localZ*directionLocal.z;float velocityScale=(1.0+3.0*a_rainParticleData.y)*u_velocity;vec3 simPosLocal=vec3(0,0,0);simPosLocal+=directionLocal*velocityScale*u_time;vec3 simPos=localX*simPosLocal.x+\nlocalY*simPosLocal.y+localZ*simPosLocal.z;pos+=simPos;pos=fract((pos+vec3(halfBoxSize))/vec3(u_boxSize))*u_boxSize-vec3(halfBoxSize);vec4 posView=u_modelview*vec4(pos,1.0);vec3 directionView=normalize((u_modelview*vec4(directionWorld,0.0)).xyz);vec3 side=cross(directionView,normalize(posView.xyz));posView.xyz+=side*a_uv.x*u_rainDropletSize.x;posView.xyz+=directionView*a_uv.y*u_rainDropletSize.y;uv=a_uv;particleRandomValue=a_rainParticleData.x;gl_Position=u_projection*posView;}";
var vignetteFrag = "uniform vec3 u_vignetteShape;uniform vec4 u_vignetteColor;in vec2 st;void main() {float screenDist=length(st);float alpha=clamp((screenDist-u_vignetteShape.x)/u_vignetteShape.y,0.0,1.0);alpha=pow(alpha,u_vignetteShape.z)*u_vignetteColor.a;vec3 color=u_vignetteColor.rgb;glFragColor=vec4(color*alpha,alpha) ;}";
var vignetteVert = "in vec2 a_pos_2f;out vec2 st;void main() {st=a_pos_2f;gl_Position=vec4(a_pos_2f,0,1);}";
var occlusionFrag = "uniform vec4 u_color;void main() {glFragColor=u_color;}";
var occlusionVert = "#include \"_prelude_terrain.vertex.glsl\"\nin highp vec2 a_offset_xy;uniform highp vec3 u_anchorPos;uniform mat4 u_matrix;uniform vec2 u_screenSizePx;uniform vec2 u_occluderSizePx;void main() {vec3 world_pos=u_anchorPos;\n#ifdef TERRAIN\nfloat e=elevation(world_pos.xy);world_pos.z+=e;\n#endif\nvec4 projected_point=u_matrix*vec4(world_pos,1.0);projected_point.xy+=projected_point.w*a_offset_xy*0.5*u_occluderSizePx/u_screenSizePx;gl_Position=projected_point;}";
var elevatedStructuresDepthReconstructFrag = "#ifdef DEPTH_RECONSTRUCTION\nin float v_height;\n#endif\nvoid main() {\n#ifdef DEPTH_RECONSTRUCTION\nif (v_height >=0.0)\ndiscard;\n#else\n#ifdef FEATURE_CUTOUT\napply_feature_cutout(vec4(0.0,0.0,0.0,1.0),gl_FragCoord);\n#endif\n#endif\nglFragColor=vec4(1.0,0.0,0.0,1.0);}";
var elevatedStructuresDepthReconstructVert = "in vec2 a_pos;in float a_height;uniform mat4 u_matrix;uniform vec3 u_camera_pos;uniform highp float u_depth_bias;uniform lowp float u_height_scale;uniform lowp float u_reset_depth;\n#ifdef DEPTH_RECONSTRUCTION\nout float v_height;\n#endif\nvoid main() {vec3 vpos=vec3(a_pos,a_height*u_height_scale);\n#ifdef DEPTH_RECONSTRUCTION\nif (u_camera_pos.z > vpos.z) {vpos-=(u_camera_pos-vpos)*(vpos.z/(u_camera_pos.z-vpos.z));}v_height=a_height;\n#endif\ngl_Position=u_matrix*vec4(vpos,1);gl_Position.z=u_reset_depth==1.0 ? gl_Position.w : gl_Position.z+u_depth_bias;}";
var elevatedStructuresDepthFrag = "void main() {\n#ifndef DEPTH_TEXTURE\nglFragColor=vec4(0.);\n#endif\n}";
var elevatedStructuresDepthVert = "in vec2 a_pos;in float a_height;uniform mat4 u_matrix;uniform float u_depth_bias;void main() {gl_Position=u_matrix*vec4(a_pos,a_height,1);gl_Position.z=gl_Position.z+u_depth_bias;}";
var elevatedStructuresModelFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\nin vec3 v_normal;in float v_height;\n#ifdef RENDER_SHADOWS\nin highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in float v_depth;\n#endif\nvec3 linearTosRGB(vec3 color) {return pow(color,vec3(1./2.2));}vec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}vec3 compute_view_dependent_emissive_color(float ndotl,float emissive_strength,vec3 color)\n{color=sRGBToLinear(color);color=color*(ndotl+(1.0-min(ndotl*57.29,1.0))*emissive_strength);color=linearTosRGB(color.rgb);return color;}uniform float u_emissive_strength;\n#pragma mapbox: define highp vec4 structure_color\nvoid main() {\n#pragma mapbox: initialize highp vec4 structure_color\nvec3 color=structure_color.xyz;\n#ifdef LIGHTING_3D_MODE\nvec3 normal=normalize(v_normal);vec3 transformed_normal=vec3(-normal.xy,normal.z);float ndotl=calculate_NdotL(transformed_normal);float emissive_strength=u_emissive_strength;emissive_strength=0.0;vec3 emissive_color=compute_view_dependent_emissive_color(ndotl,emissive_strength,color.xyz);\n#ifdef RENDER_SHADOWS\nfloat shadowed_lighting_factor=shadowed_light_factor_normal(transformed_normal,v_pos_light_view_0,v_pos_light_view_1,v_depth);color.rgb=apply_lighting(color.rgb,transformed_normal,shadowed_lighting_factor);\n#else\ncolor=apply_lighting(color,transformed_normal);\n#endif\ncolor=mix(color,emissive_color,emissive_strength);if (v_height < 0.0) {float penetration=max(v_height+7.5,0.0);float occlusion=1.0-1.0/PI*acos(1.0-penetration/4.0);color=color*(1.0-pow(occlusion,2.0)*0.3);}\n#endif\n#ifdef FOG\ncolor=fog_apply(color,v_fog_pos);\n#endif\nvec4 out_color=vec4(color,1.0);\n#ifdef INDICATOR_CUTOUT\nout_color=applyCutout(out_color,v_height);\n#endif\n#ifdef FEATURE_CUTOUT\nout_color=apply_feature_cutout(out_color,gl_FragCoord);\n#endif\nglFragColor=out_color;HANDLE_WIREFRAME_DEBUG;}";
var elevatedStructuresModelVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nin vec2 a_pos;in float a_height;in vec3 a_pos_normal_3;uniform mat4 u_matrix;out vec3 v_normal;out float v_height;\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out float v_depth;\n#endif\n#pragma mapbox: define highp vec4 structure_color\nvoid main() {\n#pragma mapbox: initialize highp vec4 structure_color\nv_normal=a_pos_normal_3/16384.0;v_height=a_height;vec3 pos=vec3(a_pos,a_height);gl_Position=u_matrix*vec4(pos,1);\n#ifdef RENDER_SHADOWS\nvec3 shd_pos0=pos;vec3 shd_pos1=pos;\n#ifdef NORMAL_OFFSET\nvec3 offset=shadow_normal_offset(vec3(-v_normal.xy,v_normal.z));shd_pos0+=offset*shadow_normal_offset_multiplier0();shd_pos1+=offset*shadow_normal_offset_multiplier1();\n#endif\nv_pos_light_view_0=u_light_matrix_0*vec4(shd_pos0,1);v_pos_light_view_1=u_light_matrix_1*vec4(shd_pos1,1);v_depth=gl_Position.w;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);\n#endif\n}";
var fillExtrusionDepthFrag = "in highp float v_depth;void main() {\n#ifndef DEPTH_TEXTURE\nglFragColor=pack_depth(v_depth);\n#endif\n}";
var fillExtrusionDepthVert = "#include \"_prelude_terrain.vertex.glsl\"\n#include \"_prelude_material_table.vertex.glsl\"\nuniform mat4 u_matrix;uniform float u_edge_radius;uniform float u_width_scale;uniform float u_vertical_scale;\n#ifdef TERRAIN\nuniform int u_height_type;uniform int u_base_type;\n#endif\nin vec4 a_pos_normal_ed;in vec2 a_centroid_pos;\n#ifdef RENDER_WALL_MODE\nin vec3 a_join_normal_inside;\n#endif\n#pragma mapbox: define highp float base\n#pragma mapbox: define highp float height\n#pragma mapbox: define highp float line_width\n#pragma mapbox: define highp vec4 color\nout highp float v_depth;void main() {DECLARE_MATERIAL_TABLE_INFO\n#pragma mapbox: initialize highp float base\n#pragma mapbox: initialize highp float height\n#pragma mapbox: initialize highp float line_width\n#pragma mapbox: initialize highp vec4 color\nbase*=u_vertical_scale;height*=u_vertical_scale;vec3 pos_nx=floor(a_pos_normal_ed.xyz*0.5);mediump vec3 top_up_ny=a_pos_normal_ed.xyz-2.0*pos_nx;base=max(0.0,base);height=max(0.0,top_up_ny.y==0.0 && top_up_ny.x==1.0 ? height-u_edge_radius : height);float t=top_up_ny.x;vec2 centroid_pos=vec2(0.0);\n#if defined(HAS_CENTROID) || defined(TERRAIN)\ncentroid_pos=a_centroid_pos;\n#endif\nvec3 pos;\n#ifdef TERRAIN\nbool is_flat_height=centroid_pos.x !=0.0 && u_height_type==1;bool is_flat_base=centroid_pos.x !=0.0 && u_base_type==1;float ele=elevation(pos_nx.xy);float c_ele=is_flat_height || is_flat_base ? (centroid_pos.y==0.0 ? elevationFromUint16(centroid_pos.x) : flatElevation(centroid_pos)) : ele;float h_height=is_flat_height ? max(c_ele+height,ele+base+2.0) : ele+height;float h_base=is_flat_base ? max(c_ele+base,ele+base) : ele+(base==0.0 ?-5.0 : base);float h=t > 0.0 ? max(h_base,h_height) : h_base;pos=vec3(pos_nx.xy,h);\n#else\npos=vec3(pos_nx.xy,t > 0.0 ? height : base);\n#endif\n#ifdef RENDER_WALL_MODE\nvec2 wall_offset=u_width_scale*line_width*(a_join_normal_inside.xy/EXTENT);pos.xy+=(1.0-a_join_normal_inside.z)*wall_offset*0.5;pos.xy-=a_join_normal_inside.z*wall_offset*0.5;\n#endif\nfloat hidden=float((centroid_pos.x==0.0 && centroid_pos.y==1.0) || (color.a==0.0));gl_Position=mix(u_matrix*vec4(pos,1),AWAY,hidden);v_depth=gl_Position.z/gl_Position.w;}";
var groundShadowFrag = "#include \"_prelude_shadow.fragment.glsl\"\nprecision highp float;uniform vec3 u_ground_shadow_factor;in vec4 v_pos_light_view_0;in vec4 v_pos_light_view_1;\n#ifdef FOG\nin float v_fog_opacity;\n#endif\nvoid main() {float light=shadowed_light_factor_plane_bias(v_pos_light_view_0,v_pos_light_view_1,1.0/gl_FragCoord.w);vec3 shadow=mix(u_ground_shadow_factor,vec3(1.0),light);\n#ifdef RENDER_CUTOFF\nshadow=mix(vec3(1.0),shadow,cutoff_opacity(u_cutoff_params,1.0/gl_FragCoord.w));\n#endif\n#ifdef FOG\nshadow=mix(shadow,vec3(1.0),v_fog_opacity);\n#endif\n#ifdef INDICATOR_CUTOUT\nshadow=mix(shadow,vec3(1.0),1.0-applyCutout(vec4(1.0),0.0).r);\n#endif\nglFragColor=vec4(shadow,1.0);}";
var groundShadowVert = "#include \"_prelude_fog.vertex.glsl\"\nuniform mat4 u_matrix;uniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;in vec2 a_pos;out vec4 v_pos_light_view_0;out vec4 v_pos_light_view_1;\n#ifdef FOG\nout float v_fog_opacity;\n#endif\nvoid main() {gl_Position=u_matrix*vec4(a_pos,0.0,1.0);v_pos_light_view_0=u_light_matrix_0*vec4(a_pos,0.0,1.0);v_pos_light_view_1=u_light_matrix_1*vec4(a_pos,0.0,1.0);\n#ifdef FOG\nv_fog_pos=fog_position(a_pos);v_fog_opacity=fog(v_fog_pos);\n#endif\n}";
var modelVert = "#include \"_prelude_fog.vertex.glsl\"\n#include \"_prelude_shadow.vertex.glsl\"\nin vec3 a_pos_3f;\n#pragma mapbox: define-attribute highp vec3 normal_3f\n#pragma mapbox: define-attribute highp vec2 uv_2f\n#pragma mapbox: define-attribute highp vec3 color_3f\n#pragma mapbox: define-attribute highp vec4 color_4f\n#pragma mapbox: define-attribute-vertex-shader-only highp vec4 pbr\n#pragma mapbox: define-attribute-vertex-shader-only highp vec3 heightBasedEmissiveStrength\nuniform mat4 u_matrix;uniform mat4 u_node_matrix;uniform mat4 u_lighting_matrix;uniform vec3 u_camera_pos;uniform vec4 u_color_mix;\n#ifdef INSTANCED_ARRAYS\nin vec4 a_normal_matrix0;in vec4 a_normal_matrix1;in vec4 a_normal_matrix2;in vec4 a_normal_matrix3;\n#else\nuniform highp mat4 u_normal_matrix;\n#endif\n#ifdef RENDER_SHADOWS\nuniform mat4 u_light_matrix_0;uniform mat4 u_light_matrix_1;out highp vec4 v_pos_light_view_0;out highp vec4 v_pos_light_view_1;out float v_depth_shadows;\n#endif\nout vec4 v_position_height;out lowp vec4 v_color_mix;\n#ifdef TERRAIN_FRAGMENT_OCCLUSION\nout highp float v_depth;\n#endif\n#ifdef HAS_ATTRIBUTE_a_pbr\nout lowp vec4 v_roughness_metallic_emissive_alpha;out mediump vec4 v_height_based_emission_params;\n#endif\nvec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}void main() {\n#pragma mapbox: initialize-attribute highp vec3 normal_3f\n#pragma mapbox: initialize-attribute highp vec2 uv_2f\n#pragma mapbox: initialize-attribute highp vec3 color_3f\n#pragma mapbox: initialize-attribute highp vec4 color_4f\n#pragma mapbox: initialize-attribute-custom highp vec4 pbr\n#pragma mapbox: initialize-attribute-custom highp vec3 heightBasedEmissiveStrength\nhighp mat4 normal_matrix;\n#ifdef INSTANCED_ARRAYS\nnormal_matrix=mat4(a_normal_matrix0,a_normal_matrix1,a_normal_matrix2,a_normal_matrix3);\n#else\nnormal_matrix=u_normal_matrix;\n#endif\nvec3 local_pos;mat3 rs;\n#ifdef MODEL_POSITION_ON_GPU\nvec3 pos_color=normal_matrix[0].xyz;vec4 translate=normal_matrix[1];vec3 pos_a=floor(pos_color);vec3 rgb=1.05*(pos_color-pos_a);float hidden=float(pos_a.x > EXTENT);float color_mix=pos_a.z/100.0;v_color_mix=vec4(sRGBToLinear(rgb),color_mix);float meter_to_tile=normal_matrix[0].w;vec4 pos=vec4(pos_a.xy,translate.z,1.0);rs[0].x=normal_matrix[1].w;rs[0].yz=normal_matrix[2].xy;rs[1].xy=normal_matrix[2].zw;rs[1].z=normal_matrix[3].x;rs[2].xyz=normal_matrix[3].yzw;vec4 pos_node=u_lighting_matrix*vec4(a_pos_3f,1.0);vec3 rotated_pos_node=rs*pos_node.xyz;vec3 pos_model_tile=(rotated_pos_node+vec3(translate.xy,0.0))*vec3(meter_to_tile,meter_to_tile,1.0);pos.xyz+=pos_model_tile;local_pos=pos.xyz;gl_Position=mix(u_matrix*pos,AWAY,hidden);pos.z*=meter_to_tile;v_position_height.xyz=pos.xyz-u_camera_pos;\n#else\nlocal_pos=a_pos_3f;gl_Position=u_matrix*vec4(a_pos_3f,1);v_position_height.xyz=vec3(u_lighting_matrix*vec4(a_pos_3f,1));v_color_mix=vec4(sRGBToLinear(u_color_mix.rgb),u_color_mix.a);\n#endif\nv_position_height.w=a_pos_3f.z;\n#ifdef HAS_ATTRIBUTE_a_pbr\nvec4 albedo_c=decode_color(pbr.xy);vec2 e_r_m=unpack_float(pbr.z);vec2 r_m= unpack_float(e_r_m.y*16.0);r_m.r=r_m.r*16.0;v_color_mix=vec4(albedo_c.rgb,1.0);v_roughness_metallic_emissive_alpha=vec4(vec3(r_m,e_r_m.x)/255.0,albedo_c.a);v_roughness_metallic_emissive_alpha.z*=2.0;float heightBasedRelativeIntepolation=a_pos_3f.z*heightBasedEmissiveStrength.x+heightBasedEmissiveStrength.y;v_height_based_emission_params.x=heightBasedRelativeIntepolation;v_height_based_emission_params.y=heightBasedEmissiveStrength.z;vec2 emissionMultiplierValues=unpack_float(pbr.w)/256.0;v_height_based_emission_params.z=emissionMultiplierValues.x;v_height_based_emission_params.w=emissionMultiplierValues.y-emissionMultiplierValues.x;\n#endif\n#ifdef FOG\nv_fog_pos=fog_position(local_pos);\n#endif\n#ifdef RENDER_CUTOFF\nv_cutoff_opacity=cutoff_opacity(u_cutoff_params,gl_Position.z);\n#endif\n#ifdef TERRAIN_FRAGMENT_OCCLUSION\nv_depth=gl_Position.z/gl_Position.w;\n#ifdef CLIP_ZERO_TO_ONE\nv_depth=-1.0+2.0*v_depth; \n#endif\n#endif\n#ifdef HAS_ATTRIBUTE_a_normal_3f\n#ifdef MODEL_POSITION_ON_GPU\nfloat x_squared_scale=dot(rs[0],rs[0]);float y_squared_scale=dot(rs[1],rs[1]);float z_squared_scale=dot(rs[2],rs[2]);vec3 squared_scale=vec3(x_squared_scale,y_squared_scale,z_squared_scale);normal_3f=rs*((u_lighting_matrix*vec4(normal_3f,0.0)).xyz/squared_scale);normal_3f=normalize(normal_3f);\n#else\nnormal_3f=vec3(normal_matrix*vec4(normal_3f,0));\n#endif\n#endif\n#ifdef HAS_ATTRIBUTE_a_pbr\n#ifdef HAS_ATTRIBUTE_a_color_4f\nv_roughness_metallic_emissive_alpha.w=clamp(color_4f.a*v_roughness_metallic_emissive_alpha.w*(v_roughness_metallic_emissive_alpha.z-1.0),0.0,1.0);\n#endif\n#endif\n#ifdef RENDER_SHADOWS\nvec4 shadow_pos=u_node_matrix*vec4(local_pos,1.0);\n#ifdef NORMAL_OFFSET\n#ifdef HAS_ATTRIBUTE_a_normal_3f\n#ifdef MODEL_POSITION_ON_GPU\nvec3 offset=shadow_normal_offset(vec3(-normal_3f.xy,normal_3f.z));shadow_pos.xyz+=offset*shadow_normal_offset_multiplier0();\n#else\nvec3 offset=shadow_normal_offset_model(normal_3f);shadow_pos.xyz+=offset*shadow_normal_offset_multiplier0();\n#endif\n#endif\n#endif\nv_pos_light_view_0=u_light_matrix_0*shadow_pos;v_pos_light_view_1=u_light_matrix_1*shadow_pos;v_depth_shadows=gl_Position.w;\n#endif\n}";
var modelFrag = "#include \"_prelude_fog.fragment.glsl\"\n#include \"_prelude_shadow.fragment.glsl\"\n#include \"_prelude_lighting.glsl\"\nuniform float u_opacity;uniform vec3 u_lightcolor;uniform vec3 u_lightpos;uniform float u_lightintensity;uniform vec4 u_baseColorFactor;uniform vec4 u_emissiveFactor;uniform float u_metallicFactor;uniform float u_roughnessFactor;uniform float u_emissive_strength;in highp vec4 v_position_height;in lowp vec4 v_color_mix;\n#ifdef RENDER_SHADOWS\nin highp vec4 v_pos_light_view_0;in highp vec4 v_pos_light_view_1;in float v_depth_shadows;\n#endif\n#ifdef OCCLUSION_TEXTURE_TRANSFORM\nuniform vec4 u_occlusionTextureTransform;\n#endif\n#pragma mapbox: define-attribute highp vec3 normal_3f\n#pragma mapbox: define-attribute highp vec3 color_3f\n#pragma mapbox: define-attribute highp vec4 color_4f\n#pragma mapbox: define-attribute highp vec2 uv_2f\n#pragma mapbox: initialize-attribute highp vec3 normal_3f\n#pragma mapbox: initialize-attribute highp vec3 color_3f\n#pragma mapbox: initialize-attribute highp vec4 color_4f\n#pragma mapbox: initialize-attribute highp vec2 uv_2f\n#ifdef HAS_ATTRIBUTE_a_pbr\nin lowp vec4 v_roughness_metallic_emissive_alpha;in mediump vec4 v_height_based_emission_params;\n#endif\n#ifdef HAS_TEXTURE_u_baseColorTexture\nuniform sampler2D u_baseColorTexture;uniform bool u_baseTextureIsAlpha;uniform bool u_alphaMask;uniform float u_alphaCutoff;\n#endif\n#ifdef HAS_TEXTURE_u_metallicRoughnessTexture\nuniform sampler2D u_metallicRoughnessTexture;\n#endif\n#ifdef HAS_TEXTURE_u_occlusionTexture\nuniform sampler2D u_occlusionTexture;uniform float u_aoIntensity;\n#endif\n#ifdef HAS_TEXTURE_u_normalTexture\nuniform sampler2D u_normalTexture;\n#endif\n#ifdef HAS_TEXTURE_u_emissionTexture\nuniform sampler2D u_emissionTexture;\n#endif\n#ifdef APPLY_LUT_ON_GPU\nuniform highp sampler3D u_lutTexture;\n#endif\n#ifdef TERRAIN_FRAGMENT_OCCLUSION\nin highp float v_depth;uniform highp sampler2D u_depthTexture;uniform highp vec2 u_inv_depth_size;uniform highp vec2 u_depth_range_unpack;\n#ifdef DEPTH_D24\nhighp float unpack_depth(highp float depth) {return depth*u_depth_range_unpack.x+u_depth_range_unpack.y;}\n#else\nhighp float unpack_depth_rgba(highp vec4 rgba_depth)\n{const highp vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);return dot(rgba_depth,bit_shift)*2.0-1.0;}\n#endif\nbool isOccluded() {highp vec2 coord=gl_FragCoord.xy*u_inv_depth_size;\n#ifdef FLIP_Y\ncoord.y=1.0-coord.y;\n#endif\n#ifdef DEPTH_D24\nhighp float depth=unpack_depth(texture(u_depthTexture,coord).r);\n#else\nhighp float depth=unpack_depth_rgba(texture(u_depthTexture,coord));\n#endif\nreturn v_depth > depth+0.0005;}\n#endif\n#define saturate(_x) clamp(_x,0.,1.)\nvec3 linearTosRGB(vec3 color) {return pow(color,vec3(1./2.2));}vec3 sRGBToLinear(vec3 srgbIn) {return pow(srgbIn,vec3(2.2));}float calculate_NdotL(vec3 normal,vec3 lightDir) {const float ext=0.70710678118;return (clamp(dot(normal,lightDir),-ext,1.0)+ext)/(1.0+ext);}vec3 getDiffuseShadedColor(vec3 albedo,vec3 normal,vec3 lightDir,vec3 lightColor)\n{\n#ifdef LIGHTING_3D_MODE\nvec3 transformed_normal=vec3(-normal.xy,normal.z);float lighting_factor;\n#ifdef RENDER_SHADOWS\nlighting_factor=shadowed_light_factor_normal(transformed_normal,v_pos_light_view_0,v_pos_light_view_1,v_depth_shadows);\n#else\nlighting_factor=saturate(dot(transformed_normal,u_lighting_directional_dir));\n#endif\nreturn apply_lighting(albedo,transformed_normal,lighting_factor);\n#else\nvec3 n=normal;float colorvalue=((albedo.x*0.2126)+(albedo.y*0.7152))+(albedo.z*0.0722);vec3 c=vec3(0.03,0.03,0.03);float directional=clamp(dot(n,vec3(lightDir)),0.0,1.0);directional=mix(1.0-u_lightintensity,max((1.0-colorvalue)+u_lightintensity,1.0),directional);vec3 c3=c+clamp((albedo*directional)*lightColor,mix(vec3(0.0),vec3(0.3),vec3(1.0)-lightColor),vec3(1.0));return c3;\n#endif\n}vec4 getBaseColor() {vec4 albedo=u_baseColorFactor;\n#ifdef HAS_ATTRIBUTE_a_color_3f\nalbedo*=vec4(color_3f,1.0);\n#endif\n#ifdef HAS_ATTRIBUTE_a_pbr\n#else\n#ifdef HAS_ATTRIBUTE_a_color_4f\nalbedo*=color_4f;\n#endif\n#endif\n#if defined (HAS_TEXTURE_u_baseColorTexture) && defined (HAS_ATTRIBUTE_a_uv_2f)\nvec4 texColor=texture(u_baseColorTexture,uv_2f);if(u_alphaMask) {if (texColor.w < u_alphaCutoff) {discard;}}\n#ifdef UNPREMULT_TEXTURE_IN_SHADER\nif(texColor.w > 0.0) {texColor.rgb/=texColor.w;}texColor.w=1.0;\n#endif\nif(u_baseTextureIsAlpha) {if (texColor.r < 0.5) {discard;}} else {texColor.rgb=sRGBToLinear(texColor.rgb);albedo*=texColor;}\n#endif\nvec4 color=vec4(mix(albedo.rgb,v_color_mix.rgb,v_color_mix.a),albedo.a);\n#ifdef APPLY_LUT_ON_GPU\ncolor=applyLUT(u_lutTexture,color);\n#endif\nreturn color;}highp mat3 cotangentFrame(highp vec3 N,highp vec3 p,highp vec2 uv ) {\n#ifdef HAS_TEXTURE_u_normalTexture\nhighp vec3 dp1=vec3(dFdx(p.x),dFdx(p.y),dFdx(p.z));highp vec3 dp2=vec3(dFdy(p.x),dFdy(p.y),dFdy(p.z));highp vec2 duv1=vec2(dFdx(uv.x),dFdx(uv.y));highp vec2 duv2=vec2(dFdy(uv.x),dFdy(uv.y));highp vec3 dp2perp=cross( dp2,N );highp vec3 dp1perp=cross( N,dp1 );highp vec3 T=dp2perp*duv1.x+dp1perp*duv2.x;highp vec3 B=dp2perp*duv1.y+dp1perp*duv2.y;\n#ifdef FLIP_Y\nT=-T;B=-B;\n#endif\nhighp float lengthT=dot(T,T);highp float lengthB=dot(B,B);highp float maxLength=max(lengthT,lengthB);highp float invmax=inversesqrt( maxLength );highp mat3 res=mat3( T*invmax,B*invmax,N );return res;\n#else\nreturn mat3(1.0);\n#endif\n}highp vec3 getNormal(){highp vec3 n;\n#ifdef HAS_ATTRIBUTE_a_normal_3f\nn=normalize(normal_3f);\n#else\nhighp vec3 fdx=vec3(dFdx(v_position_height.x),dFdx(v_position_height.y),dFdx(v_position_height.z));highp vec3 fdy=vec3(dFdy(v_position_height.x),dFdy(v_position_height.y),dFdy(v_position_height.z));\n#ifdef FLIP_Y\nn=normalize(cross(fdx,fdy));\n#else\nn=normalize(cross(fdx,fdy))*-1.0;\n#endif\n#endif\n#if defined(HAS_TEXTURE_u_normalTexture) && defined(HAS_ATTRIBUTE_a_uv_2f)\nvec3 nMap=texture( u_normalTexture,uv_2f).xyz;nMap=normalize(2.0*nMap-vec3(1.0));highp vec3 v=normalize(-v_position_height.xyz);highp mat3 TBN=cotangentFrame(n,v,uv_2f);n=normalize(TBN*nMap);\n#endif\nreturn n;}struct Material {float perceptualRoughness;float alphaRoughness;float metallic;vec3 f90;vec4 baseColor;vec3 diffuseColor;vec3 specularColor;highp vec3 normal;};Material getPBRMaterial() {Material mat;mat.baseColor=getBaseColor();mat.perceptualRoughness=u_roughnessFactor;mat.metallic=u_metallicFactor;\n#ifdef HAS_ATTRIBUTE_a_pbr\nmat.perceptualRoughness=v_roughness_metallic_emissive_alpha.x;mat.metallic=v_roughness_metallic_emissive_alpha.y;mat.baseColor.w*=v_roughness_metallic_emissive_alpha.w;\n#endif\n#if defined(HAS_TEXTURE_u_metallicRoughnessTexture) && defined(HAS_ATTRIBUTE_a_uv_2f) \nvec4 mrSample=texture(u_metallicRoughnessTexture,uv_2f);mat.perceptualRoughness*=mrSample.g;mat.metallic*=mrSample.b;\n#endif\nconst float c_minRoughness=0.04;mat.perceptualRoughness=clamp(mat.perceptualRoughness,c_minRoughness,1.0);mat.metallic=saturate(mat.metallic);mat.alphaRoughness=mat.perceptualRoughness*mat.perceptualRoughness;const vec3 f0=vec3(0.04);mat.diffuseColor=mat.baseColor.rgb*(vec3(1.0)-f0);mat.diffuseColor*=1.0-mat.metallic;mat.specularColor=mix(f0,mat.baseColor.rgb,mat.metallic);highp float reflectance=max(max(mat.specularColor.r,mat.specularColor.g),mat.specularColor.b);highp float reflectance90=saturate(reflectance*25.0);mat.f90=vec3(reflectance90);mat.normal=getNormal();return mat;}float V_GGX(float NdotL,float NdotV,float roughness)\n{float a2=roughness*roughness;float GGXV=NdotL*sqrt(NdotV*NdotV*(1.0-a2)+a2);float GGXL=NdotV*sqrt(NdotL*NdotL*(1.0-a2)+a2);return 0.5/(GGXV+GGXL);}float V_GGXFast(float NdotL,float NdotV,float roughness) {float a=roughness;float GGXV=NdotL*(NdotV*(1.0-a)+a);float GGXL=NdotV*(NdotL*(1.0-a)+a);return 0.5/(GGXV+GGXL);}vec3 F_Schlick(vec3 specularColor,vec3 f90,float VdotH)\n{return specularColor+(f90-specularColor)*pow(clamp(1.0-VdotH,0.0,1.0),5.0);}vec3 F_SchlickFast(vec3 specularColor,float VdotH)\n{float x=1.0-VdotH;float x4=x*x*x*x;return specularColor+(1.0-specularColor)*x4*x;}float D_GGX(highp float NdotH,float alphaRoughness)\n{highp float a4=alphaRoughness*alphaRoughness;highp float f=(NdotH*a4-NdotH)*NdotH+1.0;return a4/(PI*f*f);}vec3 diffuseBurley(Material mat,float LdotH,float NdotL,float NdotV)\n{float f90=2.0*LdotH*LdotH*mat.alphaRoughness-0.5;return (mat.diffuseColor/PI)*(1.0+f90*pow((1.0-NdotL),5.0))*(1.0+f90*pow((1.0-NdotV),5.0));}vec3 diffuseLambertian(Material mat)\n{\n#ifdef LIGHTING_3D_MODE\nreturn mat.diffuseColor;\n#else\nreturn mat.diffuseColor/PI;\n#endif\n}vec3 EnvBRDFApprox(vec3 specularColor,float roughness,highp float NdotV)\n{vec4 c0=vec4(-1,-0.0275,-0.572,0.022);vec4 c1=vec4(1,0.0425,1.04,-0.04);highp vec4 r=roughness*c0+c1;highp float a004=min(r.x*r.x,exp2(-9.28*NdotV))*r.x+r.y;vec2 AB=vec2(-1.04,1.04)*a004+r.zw;return specularColor*AB.x+AB.y;}vec3 computeIndirectLightContribution(Material mat,float NdotV,vec3 normal)\n{vec3 env_light=vec3(0.65,0.65,0.65);\n#ifdef LIGHTING_3D_MODE\nfloat ambient_factor=calculate_ambient_directional_factor(normal);env_light=u_lighting_ambient_color*ambient_factor;\n#endif\nvec3 envBRDF=EnvBRDFApprox(mat.specularColor,mat.perceptualRoughness,NdotV);vec3 indirectSpecular= envBRDF*env_light;vec3 indirectDiffuse=mat.diffuseColor*env_light;return indirectSpecular+indirectDiffuse;}vec3 computeLightContribution(Material mat,vec3 lightPosition,vec3 lightColor)\n{highp vec3 n=mat.normal;highp vec3 v=normalize(-v_position_height.xyz);highp vec3 l=normalize(lightPosition);highp vec3 h=normalize(v+l);float NdotV=clamp(abs(dot(n,v)),0.001,1.0);float NdotL=saturate(dot(n,l));highp float NdotH=saturate(dot(n,h));float VdotH=saturate(dot(v,h));vec3 f=F_SchlickFast(mat.specularColor,VdotH);float g=V_GGXFast(NdotL,NdotV,mat.alphaRoughness);float d=D_GGX(NdotH,mat.alphaRoughness);vec3 diffuseTerm=(1.0-f)*diffuseLambertian(mat);vec3 specularTerm=f*g*d;vec3 transformed_normal=vec3(-n.xy,n.z);float lighting_factor;\n#ifdef RENDER_SHADOWS\nlighting_factor=shadowed_light_factor_normal(transformed_normal,v_pos_light_view_0,v_pos_light_view_1,v_depth_shadows);\n#else\nlighting_factor=NdotL;\n#endif\nvec3 directLightColor=(specularTerm+diffuseTerm)*lighting_factor*lightColor;vec3 indirectLightColor=computeIndirectLightContribution(mat,NdotV,transformed_normal);vec3 color=(saturate(directLightColor)+indirectLightColor);float intensityFactor=1.0;\n#if !defined(LIGHTING_3D_MODE)\nconst vec3 luminosityFactor=vec3(0.2126,0.7152,0.0722);float luminance=dot(diffuseTerm,luminosityFactor);intensityFactor=mix((1.0-u_lightintensity),max((1.0-luminance+u_lightintensity),1.0),NdotL);\n#endif\ncolor*=intensityFactor;return color;}void main() {\n#ifdef TERRAIN_FRAGMENT_OCCLUSION\nif (isOccluded()) {discard;}\n#endif\nvec3 lightDir=u_lightpos;vec3 lightColor=u_lightcolor;\n#ifdef LIGHTING_3D_MODE\nlightDir=u_lighting_directional_dir;lightDir.xy=-lightDir.xy;lightColor=u_lighting_directional_color;\n#endif\nvec4 finalColor;\n#ifdef DIFFUSE_SHADED\nvec3 N=getNormal();vec3 baseColor=getBaseColor().rgb;vec3 diffuse=getDiffuseShadedColor(baseColor,N,lightDir,lightColor);\n#ifdef HAS_TEXTURE_u_occlusionTexture\nfloat ao=(texture(u_occlusionTexture,uv_2f).r-1.0)*u_aoIntensity+1.0;diffuse*=ao;\n#endif\nfinalColor=vec4(mix(diffuse,baseColor,u_emissive_strength),1.0)*u_opacity;\n#else\nMaterial mat=getPBRMaterial();vec3 color=computeLightContribution(mat,lightDir,lightColor);float ao=1.0;\n#if defined (HAS_TEXTURE_u_occlusionTexture) && defined(HAS_ATTRIBUTE_a_uv_2f)\n#ifdef OCCLUSION_TEXTURE_TRANSFORM\nvec2 uv=uv_2f.xy*u_occlusionTextureTransform.zw+u_occlusionTextureTransform.xy;\n#else\nvec2 uv=uv_2f;\n#endif\nao=(texture(u_occlusionTexture,uv).x-1.0)*u_aoIntensity+1.0;color*=ao;\n#endif\nvec4 emissive=u_emissiveFactor;\n#if defined(HAS_TEXTURE_u_emissionTexture) && defined(HAS_ATTRIBUTE_a_uv_2f)\nemissive.rgb*=sRGBToLinear(texture(u_emissionTexture,uv_2f).rgb);\n#endif\n#ifdef APPLY_LUT_ON_GPU\nfloat emissiveFactorLength=max(length(u_emissiveFactor.rgb),0.001);emissive.rgb=sRGBToLinear(applyLUT(u_lutTexture,linearTosRGB(emissive.rgb/emissiveFactorLength).rbg))*emissiveFactorLength;\n#endif\ncolor+=emissive.rgb;float opacity=mat.baseColor.w*u_opacity;\n#ifdef HAS_ATTRIBUTE_a_pbr\nfloat resEmission=v_roughness_metallic_emissive_alpha.z;resEmission*=v_height_based_emission_params.z+v_height_based_emission_params.w*pow(clamp(v_height_based_emission_params.x,0.0,1.0),v_height_based_emission_params.y);vec3 color_mix=v_color_mix.rgb;\n#ifdef APPLY_LUT_ON_GPU\ncolor_mix=applyLUT(u_lutTexture,color_mix);\n#endif\ncolor=mix(color,color_mix,min(1.0,resEmission));\n#ifdef HAS_ATTRIBUTE_a_color_4f\nfloat distance=length(vec2(1.3*max(0.0,abs(color_4f.x)-color_4f.z),color_4f.y));distance+= mix(0.5,0.0,clamp(resEmission-1.0,0.0,1.0));opacity*=v_roughness_metallic_emissive_alpha.w*saturate(1.0-distance*distance);\n#endif\n#endif\nvec3 unlitColor=mat.baseColor.rgb*ao+emissive.rgb;color=mix(color,unlitColor,u_emissive_strength);color=linearTosRGB(color);color*=opacity;finalColor=vec4(color,opacity);\n#endif\n#ifdef FOG\nfinalColor=fog_dither(fog_apply_premultiplied(finalColor,v_fog_pos,v_position_height.w));\n#endif\n#ifdef RENDER_CUTOFF\nfinalColor*=v_cutoff_opacity;\n#endif\n#ifdef INDICATOR_CUTOUT\nfinalColor=applyCutout(finalColor,v_position_height.w);\n#endif\n#ifdef FEATURE_CUTOUT\nfinalColor=apply_feature_cutout(finalColor,gl_FragCoord);\n#endif\nglFragColor=finalColor;\n#ifdef OVERDRAW_INSPECTOR\nglFragColor=vec4(1.0);\n#endif\nHANDLE_WIREFRAME_DEBUG;}";
var modelDepthVert = "in vec3 a_pos_3f;uniform mat4 u_matrix;out highp float v_depth;\n#ifdef MODEL_POSITION_ON_GPU\n#ifdef INSTANCED_ARRAYS\nin vec4 a_normal_matrix0;in vec4 a_normal_matrix1;in vec4 a_normal_matrix2;in vec4 a_normal_matrix3;\n#else\nuniform highp mat4 u_instance;\n#endif\nuniform highp mat4 u_node_matrix;\n#endif\nvoid main() {\n#ifdef MODEL_POSITION_ON_GPU\nhighp mat4 instance;\n#ifdef INSTANCED_ARRAYS\ninstance=mat4(a_normal_matrix0,a_normal_matrix1,a_normal_matrix2,a_normal_matrix3);\n#else\ninstance=u_instance;\n#endif\nvec3 pos_color=instance[0].xyz;vec4 translate=instance[1];vec3 pos_a=floor(pos_color);float hidden=float(pos_a.x > EXTENT);float meter_to_tile=instance[0].w;vec4 pos=vec4(pos_a.xy,translate.z,1.0);mat3 rs;rs[0].x=instance[1].w;rs[0].yz=instance[2].xy;rs[1].xy=instance[2].zw;rs[1].z=instance[3].x;rs[2].xyz=instance[3].yzw;vec4 pos_node=u_node_matrix*vec4(a_pos_3f,1.0);vec3 rotated_pos_node=rs*pos_node.xyz;vec3 pos_model_tile=(rotated_pos_node+vec3(translate.xy,0.0))*vec3(meter_to_tile,meter_to_tile,1.0);pos.xyz+=pos_model_tile;gl_Position=mix(u_matrix*pos,AWAY,hidden);\n#else\ngl_Position=u_matrix*vec4(a_pos_3f,1);\n#endif\nv_depth=gl_Position.z/gl_Position.w;}";
var modelDepthFrag = "in highp float v_depth;void main() {\n#ifndef DEPTH_TEXTURE\nglFragColor=pack_depth(v_depth);\n#endif\n}";
var preludeShadowVert = "#ifdef RENDER_SHADOWS\nuniform mediump vec3 u_shadow_direction;uniform highp vec3 u_shadow_normal_offset;vec3 shadow_normal_offset(vec3 normal) {float tileInMeters=u_shadow_normal_offset[0];vec3 n=vec3(-normal.xy,tileInMeters*normal.z);float dotScale=min(1.0-dot(normal,u_shadow_direction),1.0)*0.5+0.5;return n*dotScale;}vec3 shadow_normal_offset_model(vec3 normal) {vec3 transformed_normal=vec3(-normal.xy,normal.z);float NDotL=dot(normalize(transformed_normal),u_shadow_direction);float dotScale=min(1.0-NDotL,1.0)*0.5+0.5;return normal*dotScale;}float shadow_normal_offset_multiplier0() {return u_shadow_normal_offset[1];}float shadow_normal_offset_multiplier1() {return u_shadow_normal_offset[2];}\n#endif";
var preludeShadowFrag = "#ifdef RENDER_SHADOWS\nprecision highp sampler2DShadow;uniform sampler2DShadow u_shadowmap_0;uniform sampler2DShadow u_shadowmap_1;uniform float u_shadow_intensity;uniform float u_shadow_map_resolution;uniform float u_shadow_texel_size;uniform highp vec3 u_shadow_normal_offset;uniform vec2 u_fade_range;uniform mediump vec3 u_shadow_direction;uniform highp vec3 u_shadow_bias;float shadow_sample(sampler2DShadow shadowmap,highp vec3 pos,highp float bias) {\n#ifdef CLIP_ZERO_TO_ONE\nhighp vec3 coord=vec3(pos.xy*0.5+0.5,pos.z-bias);\n#else\nhighp vec3 coord=vec3(pos.xy*0.5+0.5,pos.z*0.5+0.5-bias);\n#endif\nreturn texture(shadowmap,coord);}float shadow_occlusion(highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth,highp float bias) {light_view_pos0.xyz/=light_view_pos0.w;\n#ifdef SHADOWS_SINGLE_CASCADE\nvec2 abs_bounds=abs(light_view_pos0.xy);if (abs_bounds.x >=1.0 || abs_bounds.y >=1.0) {return 0.0;}return shadow_sample(u_shadowmap_0,light_view_pos0.xyz,bias);\n#else\nlight_view_pos1.xyz/=light_view_pos1.w;vec4 abs_bounds=abs(vec4(light_view_pos0.xy,light_view_pos1.xy));if (abs_bounds.x < 1.0 && abs_bounds.y < 1.0) {return shadow_sample(u_shadowmap_0,light_view_pos0.xyz,bias);}if (abs_bounds.z >=1.0 || abs_bounds.w >=1.0) {return 0.0;}float occlusion1=shadow_sample(u_shadowmap_1,light_view_pos1.xyz,bias);return clamp(mix(occlusion1,0.0,smoothstep(u_fade_range.x,u_fade_range.y,view_depth)),0.0,1.0);\n#endif\n}highp float calculate_shadow_bias(float NDotL) {\n#ifdef NORMAL_OFFSET\nreturn 0.5*u_shadow_bias.x;\n#else\nreturn 0.5*(u_shadow_bias.x+clamp(u_shadow_bias.y*tan(acos(NDotL)),0.0,u_shadow_bias.z));\n#endif\n}float shadowed_light_factor_normal(vec3 N,highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth) {float NDotL=dot(N,u_shadow_direction);float bias=calculate_shadow_bias(NDotL);float occlusion=shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias);return mix(0.0,(1.0-(u_shadow_intensity*occlusion))*NDotL,step(0.0,NDotL));}float shadowed_light_factor_normal_opacity(vec3 N,highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth,float shadow_opacity) {float NDotL=dot(N,u_shadow_direction);float bias=calculate_shadow_bias(NDotL);float occlusion=shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias)*shadow_opacity;return mix(0.0,(1.0-(u_shadow_intensity*occlusion))*NDotL,step(0.0,NDotL));}float shadowed_light_factor_normal_unbiased(vec3 N,highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth) {float NDotL=dot(N,u_shadow_direction);float bias=0.0;float occlusion=shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias);return mix(0.0,(1.0-(u_shadow_intensity*occlusion))*NDotL,step(0.0,NDotL));}highp vec2 compute_receiver_plane_depth_bias(highp vec3 pos_dx,highp vec3 pos_dy)\n{highp vec2 biasUV=vec2(\npos_dy.y*pos_dx.z-pos_dx.y*pos_dy.z,pos_dx.x*pos_dy.z-pos_dy.x*pos_dx.z);biasUV*=1.0/((pos_dx.x*pos_dy.y)-(pos_dx.y*pos_dy.x));return biasUV;}float shadowed_light_factor_plane_bias(highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth) {highp vec3 light_view_pos0_xyz=light_view_pos0.xyz/light_view_pos0.w*0.5+0.5;highp vec3 light_view_pos0_ddx=dFdx(light_view_pos0_xyz);highp vec3 light_view_pos0_ddy=dFdy(light_view_pos0_xyz);highp vec2 plane_depth_bias=compute_receiver_plane_depth_bias(light_view_pos0_ddx,light_view_pos0_ddy);highp float bias=dot(vec2(u_shadow_texel_size,u_shadow_texel_size),plane_depth_bias)+0.0001;float occlusion=shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias);return 1.0-(u_shadow_intensity*occlusion);}float shadowed_light_factor(highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth) {float bias=0.0;float occlusion=shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias);return 1.0-(u_shadow_intensity*occlusion);}float shadow_occlusion(float ndotl,highp vec4 light_view_pos0,highp vec4 light_view_pos1,float view_depth) {float bias=calculate_shadow_bias(ndotl);return shadow_occlusion(light_view_pos0,light_view_pos1,view_depth,bias);}\n#endif";
var preludeMaterialTableVert = "#ifdef HAS_SHADER_STORAGE_BLOCK_material_buffer\n#define MATERIAL_TABLE_DEBUG 0\nuniform int u_material_offset;uniform int u_vertex_offset;layout(std140,binding=0)readonly buffer material_buffer{uvec4 material_data[];};struct MaterialInfo{uint dataOffset;\n#if MATERIAL_TABLE_DEBUG\nvec4 colorDebug;\n#endif\n};uint read_buf_no_offset(uint iDword) {return material_data[iDword/4u][iDword % 4u];}uint read_buf(uint iDword) {iDword+=uint(u_material_offset/4);return read_buf_no_offset(iDword);}float read_buf_float(uint iDword){return uintBitsToFloat(read_buf(iDword));}uint read_buf_uint8(uint iDword,uint iUint8){uint dwordOffset=iDword+(iUint8/4u);uint byteOffset=iUint8 & 3u;uint bitOffset=8u*byteOffset;uint mask=0xffu << bitOffset;uint dwordVal=read_buf(dwordOffset);return (dwordVal & mask) >> bitOffset;}uint read_buf_uint16(uint iDword,uint iUint16){uint dwordOffset=iDword+(iUint16 >> 1u);uint bitOffset=(iUint16 & 1u)*16u;uint mask=0xffffu << bitOffset;uint dwordVal=read_buf(dwordOffset);return (dwordVal & mask) >> bitOffset;}uint nrDwordsForVertexIdEntries(uint nrMaterialLookupEntries) {return nrMaterialLookupEntries;}uint nrDwordsForMaterialIdEntries(uint nrMaterialLookupEntries) {return (nrMaterialLookupEntries*2u+3u)/4u;}uint findRangeBinarySearch(uint vertexId,uint numRanges,uint dwordOffset) {uint left=0u;uint right=numRanges-1u;for (uint i=0u; i < 16u; i++) { \nif (left > right) {break;}uint mid=(left+right)/2u;uint start=read_buf(dwordOffset+mid);uint nextStart=(mid+1u < numRanges) ? read_buf(dwordOffset+mid+1u) : 0xffffffffu;if (vertexId >=start && vertexId < nextStart) {return mid;} else if (vertexId < start) {if (mid==0u) {break;}right=mid-1u;} else {left=mid+1u;}}return 0u; \n}uint readVertexId(uint dwordOffset,uint iMaterialLookupEntry) {return read_buf(dwordOffset+iMaterialLookupEntry);}uint findRange(uint vertexId,uint numRanges,uint dwordOffset) {uint iRange;if(numRanges <=64u){uint vertexBegin;for(iRange=0u; iRange < numRanges;++iRange) {vertexBegin=readVertexId(dwordOffset,iRange);if(vertexBegin > vertexId) {break;}}iRange=iRange==0u? 0u : iRange-1u;} else { \niRange=findRangeBinarySearch(vertexId,numRanges,dwordOffset);}return iRange;}MaterialInfo read_material_info(uint vertex_id) {MaterialInfo info;\n#if MATERIAL_TABLE_DEBUG\nconst vec4 red=vec4(1.0,0.0,0.0,1.0);const vec4 orange=vec4(1.0,0.5,0.0,1.0);const vec4 yellow=vec4(1.0,1.0,0.0,1.0);const vec4 green=vec4(0.0,1.0,0.0,1.0);const vec4 indigo=vec4(0.294,0.0,0.510,1.0);const vec4 blue=vec4(0.0,0.0,1.0,1.0);const vec4 purple=vec4(0.5,0.0,0.5,1.0);const vec4 pink=vec4(1.0,0.0,1.0,1.0);info.colorDebug=green;\n#endif\nuint offset=0u;\n#if MATERIAL_TABLE_DEBUG\nbool keepFinding=true;uint magic=read_buf(offset);if(magic !=0xCAFEBABEu) {info.colorDebug=red;keepFinding=false;return info;}\n#endif\noffset++;\n#if MATERIAL_TABLE_DEBUG\nuint nrMaterials=read_buf(offset);uint nrVertices=read_buf(offset+1u);if(keepFinding && vertex_id >=nrVertices) {info.colorDebug=red;keepFinding=false;}\n#endif\noffset+=2u;uint nrMaterialLookupEntries=read_buf(offset++);uint perMaterialEntrySizeDwords=read_buf(offset++);\n#if MATERIAL_TABLE_DEBUG\nif(keepFinding && perMaterialEntrySizeDwords !=1u) {info.colorDebug=red;keepFinding=false;}\n#endif\nuint iMaterialLookup=findRange(vertex_id,nrMaterialLookupEntries,offset);\n#if MATERIAL_TABLE_DEBUG\nif(keepFinding)\n{uint vertexBeginCheck=readVertexId(offset,iMaterialLookup);if(vertexBeginCheck > vertex_id) {info.colorDebug=red;keepFinding=false;}if(iMaterialLookup < nrMaterialLookupEntries-1u) {uint vertexEndCheck=readVertexId(offset,iMaterialLookup+1u);if(vertexEndCheck <=vertex_id) {info.colorDebug=red;keepFinding=false;}}}\n#endif\noffset+=nrDwordsForVertexIdEntries(nrMaterialLookupEntries);uint materialId=iMaterialLookup;\n#if MATERIAL_TABLE_DEBUG\nif(keepFinding) {if(materialId >=nrMaterialLookupEntries) {info.colorDebug=red;}}\n#endif\ninfo.dataOffset=offset+materialId*perMaterialEntrySizeDwords;return info;}uint get_data_location(const MaterialInfo matInfo,uint attribOffsetBytes)\n{uint attribFieldOffsetDwords=attribOffsetBytes/4u;return matInfo.dataOffset+attribFieldOffsetDwords;}vec4 read_material_vec4(const MaterialInfo matInfo,uint attribOffsetBytes){uint loc=get_data_location(matInfo,attribOffsetBytes);return vec4(read_buf_float(loc),read_buf_float(loc+1u),read_buf_float(loc+2u),read_buf_float(loc+3u));}vec2 read_material_vec2(const MaterialInfo matInfo,uint attribOffsetBytes){uint loc=get_data_location(matInfo,attribOffsetBytes);return vec2(read_buf_float(loc),read_buf_float(loc+1u));}float read_material_float(const MaterialInfo matInfo,uint attribOffsetBytes){uint loc=get_data_location(matInfo,attribOffsetBytes);return read_buf_float(loc);}\n#define GET_ATTRIBUTE_float(attrib,matInfo,attrib_offset) read_material_float(matInfo,attrib_offset)\n#define GET_ATTRIBUTE_vec4(attrib,matInfo,attrib_offset) read_material_vec4(matInfo,attrib_offset)\n#define GET_ATTRIBUTE_vec2(attrib,matInfo,attrib_offset) read_material_vec2(matInfo,attrib_offset)\n#define DECLARE_MATERIAL_TABLE_INFO MaterialInfo materialInfo=read_material_info(uint(gl_VertexID));\n#define DECLARE_MATERIAL_TABLE_INFO_DEBUG(dbgColor) MaterialInfo materialInfo=read_material_info(uint(gl_VertexID)); dbgColor=materialInfo.colorDebug;\n#endif";
const INCLUDE_REGEX = /#include\s+"([^"]+)"/g;
const PRAGMA_REGEX = /#pragma mapbox: ([\w\-]+) ([\w]+) ([\w]+) ([\w]+)/g;
const IDENTIFIER_REGEX = /\b[A-Za-z_][A-Za-z0-9_]*\b/g;
const PREPROCESSOR_KEYWORDS = /* @__PURE__ */ new Set(["ifdef", "ifndef", "elif", "if", "defined"]);
const commonDefines = /* @__PURE__ */ new Set();
parseUsedPreprocessorDefines(preludeCommon, commonDefines);
parseUsedPreprocessorDefines(preludeVert, commonDefines);
parseUsedPreprocessorDefines(preludeFrag, commonDefines);
const includeMap = {
"_prelude_fog.vertex.glsl": preludeFogVert,
"_prelude_terrain.vertex.glsl": preludeTerrainVert,
"_prelude_shadow.vertex.glsl": preludeShadowVert,
"_prelude_material_table.vertex.glsl": preludeMaterialTableVert,
"_prelude_fog.fragment.glsl": preludeFogFrag,
"_prelude_shadow.fragment.glsl": preludeShadowFrag,
"_prelude_lighting.glsl": preludeLighting,
"_prelude_raster_array.glsl": preludeRasterArrayFrag,
"_prelude_raster_particle.glsl": preludeRasterParticleFrag
};
const defineMap = {};
const preludeTerrain = compile("", preludeTerrainVert);
const preludeFog = compile(preludeFogFrag, preludeFogVert);
const preludeShadow = compile(preludeShadowFrag, preludeShadowVert);
const preludeRasterArray = compile(preludeRasterArrayFrag, "");
const preludeRasterParticle = compile(preludeRasterParticleFrag, "");
const prelude = compile(preludeFrag, preludeVert);
const preludeCommonSource = preludeCommon;
const preludeLightingSource = preludeLighting;
const preludeVertPrecisionQualifiers = `precision highp float;`;
const preludeFragPrecisionQualifiers = `precision mediump float;`;
const preludeFragExtensions = `
#if defined(GL_EXT_blend_func_extended) && defined(DUAL_SOURCE_BLENDING)
#extension GL_EXT_blend_func_extended : require
#endif`;
const FRAGMENT_PRELUDE_BLOCK = [
preludeFragExtensions,
preludeFragPrecisionQualifiers,
preludeCommonSource,
prelude.fragmentSource
].join("\n");
const VERTEX_PRELUDE_BLOCK = [
preludeVertPrecisionQualifiers,
preludeCommonSource,
prelude.vertexSource
].join("\n");
var shaders = {
background: compile(backgroundFrag, backgroundVert),
backgroundPattern: compile(backgroundPatternFrag, backgroundPatternVert),
building: compile(buildingFrag, buildingVert),
buildingBloom: compile(buildingBloomFrag, buildingBloomVert),
buildingDepth: compile(buildingDepthFrag, buildingDepthVert),
circle: compile(circleFrag, circleVert),
clippingMask: compile(clippingMaskFrag, clippingMaskVert),
heatmap: compile(heatmapFrag, heatmapVert),
heatmapTexture: compile(heatmapTextureFrag, heatmapTextureVert),
collisionBox: compile(collisionBoxFrag, collisionBoxVert),
collisionCircle: compile(collisionCircleFrag, collisionCircleVert),
debug: compile(debugFrag, debugVert),
elevatedStructuresDepth: compile(elevatedStructuresDepthFrag, elevatedStructuresDepthVert),
elevatedStructuresDepthReconstruct: compile(elevatedStructuresDepthReconstructFrag, elevatedStructuresDepthReconstructVert),
elevatedStructures: compile(elevatedStructuresModelFrag, elevatedStructuresModelVert),
fill: compile(fillFrag, fillVert),
fillOutline: compile(fillOutlineFrag, fillOutlineVert),
fillOutlinePattern: compile(fillOutlinePatternFrag, fillOutlinePatternVert),
fillPattern: compile(fillPatternFrag, fillPatternVert),
fillExtrusion: compile(fillExtrusionFrag, fillExtrusionVert),
fillExtrusionDepth: compile(fillExtrusionDepthFrag, fillExtrusionDepthVert),
fillExtrusionPattern: compile(fillExtrusionPatternFrag, fillExtrusionPatternVert),
groundShadow: compile(groundShadowFrag, groundShadowVert),
fillExtrusionGroundEffect: compile(fillExtrusionGroundEffectFrag, fillExtrusionGroundEffectVert),
hillshadePrepare: compile(hillshadePrepareFrag, hillshadePrepareVert),
hillshade: compile(hillshadeFrag, hillshadeVert),
line: compile(lineFrag, lineVert),
linePattern: compile(linePatternFrag, linePatternVert),
raster: compile(rasterFrag, rasterVert),
rasterParticle: compile(rasterParticleFrag, rasterParticleVert),
rasterParticleDraw: compile(rasterParticleDrawFrag, rasterParticleDrawVert),
rasterParticleTexture: compile(rasterParticleTextureFrag, rasterParticleTextureVert),
rasterParticleUpdate: compile(rasterParticleUpdateFrag, rasterParticleUpdateVert),
symbol: compile(symbolFrag, symbolVert),
terrainRaster: compile(terrainRasterFrag, terrainRasterVert),
terrainDepth: compile(terrainDepthFrag, terrainDepthVert),
skybox: compile(skyboxFrag, skyboxVert),
skyboxGradient: compile(skyboxGradientFrag, skyboxVert),
skyboxCapture: compile(skyboxCaptureFrag, skyboxCaptureVert),
globeRaster: compile(globeFrag, globeVert),
globeAtmosphere: compile(atmosphereFrag, atmosphereVert),
model: compile(modelFrag, modelVert),
modelDepth: compile(modelDepthFrag, modelDepthVert),
stars: compile(starsFrag, starsVert),
snowParticle: compile(snowFrag, snowVert),
rainParticle: compile(rainFrag, rainVert),
vignette: compile(vignetteFrag, vignetteVert),
occlusion: compile(occlusionFrag, occlusionVert)
};
function parseUsedPreprocessorDefines(source, defines) {
const lines = source.split("\n");
for (let line of lines) {
line = line.trimStart();
if (line[0] !== "#") continue;
if (!line.includes("if")) continue;
if (line.startsWith("#endif")) continue;
const matches = line.match(IDENTIFIER_REGEX);
if (!matches) continue;
for (const match of matches) {
if (!PREPROCESSOR_KEYWORDS.has(match)) {
defines.add(match);
}
}
}
}
function compile(fragmentSource, vertexSource) {
const fragmentPragmas = /* @__PURE__ */ new Set();
const vertexIncludes = [];
const fragmentIncludes = [];
fragmentSource = fragmentSource.replace(INCLUDE_REGEX, (_, name) => {
fragmentIncludes.push(name);
return "";
});
vertexSource = vertexSource.replace(INCLUDE_REGEX, (_, name) => {
vertexIncludes.push(name);
return "";
});
index$1.assert(!vertexSource.includes("flat out"), 'The usage of "flat" qualifier is disallowed, see: https://bugs.webkit.org/show_bug.cgi?id=268071');
let usedDefines = new Set(commonDefines);
parseUsedPreprocessorDefines(fragmentSource, usedDefines);
parseUsedPreprocessorDefines(vertexSource, usedDefines);
for (const includePath of [...vertexIncludes, ...fragmentIncludes]) {
index$1.assert(includeMap[includePath], `Unknown include: ${includePath}`);
if (!defineMap[includePath]) {
defineMap[includePath] = /* @__PURE__ */ new Set();
parseUsedPreprocessorDefines(includeMap[includePath], defineMap[includePath]);
}
usedDefines = /* @__PURE__ */ new Set([...usedDefines, ...defineMap[includePath]]);
}
fragmentSource = fragmentSource.replace(PRAGMA_REGEX, (_, operation, precision, type, name) => {
fragmentPragmas.add(name);
if (operation === "define") {
return `
#ifndef HAS_UNIFORM_u_${name}
in ${precision} ${type} ${name};
#else
uniform ${precision} ${type} u_${name};
#endif
`;
} else if (operation === "initialize") {
return `
#ifdef HAS_UNIFORM_u_${name}
${precision} ${type} ${name} = u_${name};
#endif
`;
} else if (operation === "define-attribute") {
return `
#ifdef HAS_ATTRIBUTE_a_${name}
in ${precision} ${type} ${name};
#endif
`;
} else if (operation === "initialize-attribute") {
return "";
}
});
vertexSource = vertexSource.replace(PRAGMA_REGEX, (_, operation, precision, type, name) => {
const materialOffsetNameDefineName = `MATERIAL_ATTRIBUTE_OFFSET_${name}`;
const attrType = type === "float" ? "vec2" : type;
const materialAttribExpression = `GET_ATTRIBUTE_${attrType}(a_${name}, materialInfo, ${materialOffsetNameDefineName})`;
const unpackType = name.match(/color/) ? "color" : attrType;
if (operation === "define-attribute-vertex-shader-only") {
return `
#ifdef HAS_ATTRIBUTE_a_${name}
in ${precision} ${type} a_${name};
#endif
`;
} else if (fragmentPragmas.has(name)) {
if (operation === "define") {
return `
#ifndef HAS_UNIFORM_u_${name}
uniform lowp float u_${name}_t;
#if !defined(${materialOffsetNameDefineName})
in ${precision} ${attrType} a_${name};
#endif
out ${precision} ${type} ${name};
#else
uniform ${precision} ${type} u_${name};
#endif
`;
} else if (operation === "initialize") {
if (unpackType === "vec4") {
return `
#ifndef HAS_UNIFORM_u_${name}
${name} = a_${name};
#else
${precision} ${type} ${name} = u_${name};
#endif
`;
} else {
return `
#if !defined(HAS_UNIFORM_u_${name})
#ifdef ${materialOffsetNameDefineName}
${name} = unpack_mix_${unpackType}(${materialAttribExpression}, u_${name}_t);
#else
${name} = unpack_mix_${unpackType}(a_${name}, u_${name}_t);
#endif
#else
${precision} ${type} ${name} = u_${name};
#endif
`;
}
} else if (operation === "define-attribute") {
return `
#ifdef HAS_ATTRIBUTE_a_${name}
in ${precision} ${type} a_${name};
out ${precision} ${type} ${name};
#endif
`;
} else if (operation === "initialize-attribute") {
return `
#ifdef HAS_ATTRIBUTE_a_${name}
${name} = a_${name};
#endif
`;
}
} else {
if (operation === "define") {
return `
#ifndef HAS_UNIFORM_u_${name}
uniform lowp float u_${name}_t;
#if !defined(${materialOffsetNameDefineName})
in ${precision} ${attrType} a_${name};
#endif
#else
uniform ${precision} ${type} u_${name};
#endif
`;
} else if (operation === "define-instanced") {
if (unpackType === "mat4") {
return `
#ifdef INSTANCED_ARRAYS
in vec4 a_${name}0;
in vec4 a_${name}1;
in vec4 a_${name}2;
in vec4 a_${name}3;
#else
uniform ${precision} ${type} u_${name};
#endif
`;
} else {
return `
#ifdef INSTANCED_ARRAYS
in ${precision} ${attrType} a_${name};
#else
uniform ${precision} ${type} u_${name};
#endif
`;
}
} else if (operation === "initialize-attribute-custom") {
return `
#ifdef HAS_ATTRIBUTE_a_${name}
${precision} ${type} ${name} = a_${name};
#endif
`;
} else {
if (unpackType === "vec4") {
return `
#ifndef HAS_UNIFORM_u_${name}
#ifdef ${materialOffsetNameDefineName}
${precision} ${type} ${name} = ${materialAttribExpression};
#else
${precision} ${type} ${name} = a_${name};
#endif
#else
${precision} ${type} ${name} = u_${name};
#endif
`;
} else {
return `
#ifndef HAS_UNIFORM_u_${name}
#ifdef ${materialOffsetNameDefineName}
${precision} ${type} ${name} = unpack_mix_${unpackType}(${materialAttribExpression}, u_${name}_t);
#else
${precision} ${type} ${name} = unpack_mix_${unpackType}(a_${name}, u_${name}_t);
#endif
#else
${precision} ${type} ${name} = u_${name};
#endif
`;
}
}
}
});
return { fragmentSource, vertexSource, usedDefines, vertexIncludes, fragmentIncludes };
}
class VertexArrayObject {
constructor() {
this.boundProgram = null;
this.boundLayoutVertexBuffer = null;
this.boundPaintVertexBuffers = [];
this.boundIndexBuffer = null;
this.boundVertexOffset = null;
this.boundDynamicVertexBuffers = [];
this.vao = null;
}
bind(context, program, layoutVertexBuffer, paintVertexBuffers, indexBuffer, vertexOffset, dynamicVertexBuffers, vertexAttribDivisorValue) {
this.context = context;
let paintBuffersDiffer = this.boundPaintVertexBuffers.length !== paintVertexBuffers.length;
for (let i = 0; !paintBuffersDiffer && i < paintVertexBuffers.length; i++) {
if (this.boundPaintVertexBuffers[i] !== paintVertexBuffers[i]) {
paintBuffersDiffer = true;
}
}
let dynamicBuffersDiffer = this.boundDynamicVertexBuffers.length !== dynamicVertexBuffers.length;
for (let i = 0; !dynamicBuffersDiffer && i < dynamicVertexBuffers.length; i++) {
if (this.boundDynamicVertexBuffers[i] !== dynamicVertexBuffers[i]) {
dynamicBuffersDiffer = true;
}
}
const isFreshBindRequired = !this.vao || this.boundProgram !== program || this.boundLayoutVertexBuffer !== layoutVertexBuffer || paintBuffersDiffer || dynamicBuffersDiffer || this.boundIndexBuffer !== indexBuffer || this.boundVertexOffset !== vertexOffset;
if (isFreshBindRequired) {
this.freshBind(program, layoutVertexBuffer, paintVertexBuffers, indexBuffer, vertexOffset, dynamicVertexBuffers, vertexAttribDivisorValue);
} else {
context.bindVertexArrayOES.set(this.vao);
for (const dynamicBuffer of dynamicVertexBuffers) {
if (dynamicBuffer) {
dynamicBuffer.bind();
if (vertexAttribDivisorValue && dynamicBuffer.instanceCount) {
dynamicBuffer.setVertexAttribDivisor(context.gl, program, vertexAttribDivisorValue);
}
}
}
if (indexBuffer && indexBuffer.dynamicDraw) {
indexBuffer.bind();
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
freshBind(program, layoutVertexBuffer, paintVertexBuffers, indexBuffer, vertexOffset, dynamicVertexBuffers, vertexAttribDivisorValue) {
const context = this.context;
const gl = context.gl;
if (this.vao) this.destroy();
this.vao = context.gl.createVertexArray();
context.bindVertexArrayOES.set(this.vao);
this.boundProgram = program;
this.boundLayoutVertexBuffer = layoutVertexBuffer;
this.boundPaintVertexBuffers = paintVertexBuffers;
this.boundIndexBuffer = indexBuffer;
this.boundVertexOffset = vertexOffset;
this.boundDynamicVertexBuffers = dynamicVertexBuffers;
layoutVertexBuffer.enableAttributes(gl, program);
layoutVertexBuffer.bind();
layoutVertexBuffer.setVertexAttribPointers(gl, program, vertexOffset);
for (const vertexBuffer of paintVertexBuffers) {
vertexBuffer.enableAttributes(gl, program);
vertexBuffer.bind();
vertexBuffer.setVertexAttribPointers(gl, program, vertexOffset);
}
for (const dynamicBuffer of dynamicVertexBuffers) {
if (dynamicBuffer) {
dynamicBuffer.enableAttributes(gl, program);
dynamicBuffer.bind();
dynamicBuffer.setVertexAttribPointers(gl, program, vertexOffset);
if (vertexAttribDivisorValue && dynamicBuffer.instanceCount) {
dynamicBuffer.setVertexAttribDivisor(gl, program, vertexAttribDivisorValue);
}
}
}
if (indexBuffer) {
indexBuffer.bind();
}
}
destroy() {
if (this.vao) {
this.context.gl.deleteVertexArray(this.vao);
this.vao = null;
}
}
}
const hillshadeUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_image": new index$1.Uniform1i(context),
"u_latrange": new index$1.Uniform2f(context),
"u_light": new index$1.Uniform2f(context),
"u_shadow": new index$1.UniformColor(context),
"u_highlight": new index$1.UniformColor(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_accent": new index$1.UniformColor(context)
});
const hillshadePrepareUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_image": new index$1.Uniform1i(context),
"u_dimension": new index$1.Uniform2f(context),
"u_zoom": new index$1.Uniform1f(context)
});
const hillshadeUniformValues = (painter, tile, layer, matrix) => {
const shadow = layer.paint.get("hillshade-shadow-color");
const shadowIgnoreLut = layer.paint.get("hillshade-shadow-color-use-theme").constantOr("default") === "none";
const highlight = layer.paint.get("hillshade-highlight-color");
const highlightIgnoreLut = layer.paint.get("hillshade-highlight-color-use-theme").constantOr("default") === "none";
const accent = layer.paint.get("hillshade-accent-color");
const accentIgnoreLut = layer.paint.get("hillshade-accent-color-use-theme").constantOr("default") === "none";
const emissiveStrength = layer.paint.get("hillshade-emissive-strength");
let azimuthal = index$1.degToRad(layer.paint.get("hillshade-illumination-direction"));
if (layer.paint.get("hillshade-illumination-anchor") === "viewport") {
azimuthal -= painter.transform.angle;
} else if (painter.style && painter.style.enable3dLights()) {
if (painter.style.directionalLight) {
const direction = painter.style.directionalLight.properties.get("direction");
const spherical = index$1.cartesianPositionToSpherical(direction.x, direction.y, direction.z);
azimuthal = index$1.degToRad(spherical[1]);
}
}
const align = !painter.options.moving;
return {
"u_matrix": matrix ? matrix : painter.transform.calculateProjMatrix(tile.tileID.toUnwrapped(), align),
"u_image": 0,
"u_latrange": getTileLatRange(painter, tile.tileID),
"u_light": [layer.paint.get("hillshade-exaggeration"), azimuthal],
"u_shadow": shadow.toPremultipliedRenderColor(shadowIgnoreLut ? null : layer.lut),
"u_highlight": highlight.toPremultipliedRenderColor(highlightIgnoreLut ? null : layer.lut),
"u_emissive_strength": emissiveStrength,
"u_accent": accent.toPremultipliedRenderColor(accentIgnoreLut ? null : layer.lut)
};
};
const hillshadeUniformPrepareValues = (tileID, dem) => {
const stride = dem.stride;
const matrix = index$1.create();
index$1.ortho(matrix, 0, index$1.EXTENT, -index$1.EXTENT, 0, 0, 1);
index$1.translate(matrix, matrix, [0, -index$1.EXTENT, 0]);
return {
"u_matrix": matrix,
"u_image": 1,
"u_dimension": [stride, stride],
"u_zoom": tileID.overscaledZ
};
};
function getTileLatRange(painter, tileID) {
const tilesAtZoom = Math.pow(2, tileID.canonical.z);
const y = tileID.canonical.y;
return [
new index$1.MercatorCoordinate(0, y / tilesAtZoom).toLngLat().lat,
new index$1.MercatorCoordinate(0, (y + 1) / tilesAtZoom).toLngLat().lat
];
}
function drawHillshade(painter, sourceCache, layer, tileIDs) {
if (painter.renderPass !== "offscreen" && painter.renderPass !== "translucent") return;
if (painter.style.disableElevatedTerrain) return;
const context = painter.context;
const renderingToTexture = painter.terrain && painter.terrain.renderingToTexture;
const [stencilModes, coords] = painter.renderPass === "translucent" && !renderingToTexture ? painter.stencilConfigForOverlap(tileIDs) : [{}, tileIDs];
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
if (tile.needsHillshadePrepare && painter.renderPass === "offscreen") {
prepareHillshade(painter, tile, layer);
} else if (painter.renderPass === "translucent") {
const depthMode = painter.depthModeForSublayer(0, DepthMode.ReadOnly);
const emissiveStrength = layer.paint.get("hillshade-emissive-strength");
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const stencilMode = renderingToTexture && painter.terrain ? painter.terrain.stencilModeForRTTOverlap(coord) : stencilModes[coord.overscaledZ];
renderHillshade(painter, coord, tile, layer, depthMode, stencilMode, colorMode);
}
}
context.viewport.set([0, 0, painter.width, painter.height]);
painter.resetStencilClippingMasks();
}
function renderHillshade(painter, coord, tile, layer, depthMode, stencilMode, colorMode) {
const context = painter.context;
const gl = context.gl;
const fbo = tile.hillshadeFBO;
if (!fbo) return;
painter.prepareDrawTile();
const affectedByFog = painter.isTileAffectedByFog(coord);
const definesValues = [];
const isDraping = painter.terrain && painter.terrain.renderingToTexture;
if (isDraping && painter.emissiveMode === "mrt-fallback") {
definesValues.push("USE_MRT1");
}
const program = painter.getOrCreateProgram("hillshade", { overrideFog: affectedByFog, defines: definesValues });
context.activeTexture.set(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, fbo.colorAttachment0.get());
const uniformValues = hillshadeUniformValues(painter, tile, layer, painter.terrain ? coord.projMatrix : null);
painter.uploadCommonUniforms(context, program, coord.toUnwrapped());
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = painter.getTileBoundsBuffers(tile);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
}
function prepareDEMTexture(painter, tile, dem) {
if (!tile.needsDEMTextureUpload) return;
const context = painter.context;
const gl = context.gl;
context.pixelStoreUnpackPremultiplyAlpha.set(false);
const textureStride = dem.stride;
tile.demTexture = tile.demTexture || painter.getTileTexture(textureStride);
const demImage = dem.getPixels();
if (tile.demTexture) {
tile.demTexture.update(demImage, { premultiply: false });
} else {
tile.demTexture = new index$1.Texture(context, demImage, gl.R32F, { premultiply: false });
}
tile.needsDEMTextureUpload = false;
}
function prepareHillshade(painter, tile, layer) {
const context = painter.context;
const gl = context.gl;
if (!tile.dem) return;
const dem = tile.dem;
context.activeTexture.set(gl.TEXTURE1);
prepareDEMTexture(painter, tile, dem);
index$1.assert(tile.demTexture);
if (!tile.demTexture) return;
tile.demTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
const tileSize = dem.dim;
context.activeTexture.set(gl.TEXTURE0);
let fbo = tile.hillshadeFBO;
if (!fbo) {
const renderTexture = new index$1.Texture(context, { width: tileSize, height: tileSize, data: null }, gl.RGBA8);
renderTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
fbo = tile.hillshadeFBO = context.createFramebuffer(tileSize, tileSize, 1, "renderbuffer");
fbo.colorAttachment0.set(renderTexture.texture);
}
context.bindFramebuffer.set(fbo.framebuffer);
context.viewport.set([0, 0, tileSize, tileSize]);
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = painter.getMercatorTileBoundsBuffers();
const definesValues = [];
if (painter.linearFloatFilteringSupported()) definesValues.push("TERRAIN_DEM_FLOAT_FORMAT");
const isDraping = painter.terrain && painter.terrain.renderingToTexture;
if (isDraping && painter.emissiveMode === "mrt-fallback") {
definesValues.push("USE_MRT1");
}
painter.getOrCreateProgram("hillshadePrepare", { defines: definesValues }).draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.unblended,
CullFaceMode.disabled,
hillshadeUniformPrepareValues(tile.tileID, dem),
layer.id,
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
tile.needsHillshadePrepare = false;
}
class BaseValue {
constructor(context) {
this.gl = context.gl;
this.default = this.getDefault();
this.current = this.default;
this.dirty = false;
}
get() {
return this.current;
}
set(value) {
}
getDefault() {
return this.default;
}
setDefault() {
this.set(this.default);
}
}
class ClearColor extends BaseValue {
getDefault() {
return index$1.Color.transparent.toNonPremultipliedRenderColor(null);
}
set(v) {
const c = this.current;
if (v.r === c.r && v.g === c.g && v.b === c.b && v.a === c.a && !this.dirty) return;
this.gl.clearColor(v.r, v.g, v.b, v.a);
this.current = v;
this.dirty = false;
}
}
class ClearDepth extends BaseValue {
getDefault() {
return 1;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.clearDepth(v);
this.current = v;
this.dirty = false;
}
}
class ClearStencil extends BaseValue {
getDefault() {
return 0;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.clearStencil(v);
this.current = v;
this.dirty = false;
}
}
class ColorMask extends BaseValue {
getDefault() {
return [true, true, true, true];
}
set(v) {
const c = this.current;
if (v[0] === c[0] && v[1] === c[1] && v[2] === c[2] && v[3] === c[3] && !this.dirty) return;
this.gl.colorMask(v[0], v[1], v[2], v[3]);
this.current = v;
this.dirty = false;
}
}
class DepthMask extends BaseValue {
getDefault() {
return true;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.depthMask(v);
this.current = v;
this.dirty = false;
}
}
class StencilMask extends BaseValue {
getDefault() {
return 255;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.stencilMask(v);
this.current = v;
this.dirty = false;
}
}
class StencilFunc extends BaseValue {
getDefault() {
return {
func: this.gl.ALWAYS,
ref: 0,
mask: 255
};
}
set(v) {
const c = this.current;
if (v.func === c.func && v.ref === c.ref && v.mask === c.mask && !this.dirty) return;
index$1.assert(v.ref >= 0 && v.ref <= 255);
this.gl.stencilFunc(v.func, v.ref, v.mask);
this.current = v;
this.dirty = false;
}
}
class StencilOp extends BaseValue {
getDefault() {
const gl = this.gl;
return [gl.KEEP, gl.KEEP, gl.KEEP];
}
set(v) {
const c = this.current;
if (v[0] === c[0] && v[1] === c[1] && v[2] === c[2] && !this.dirty) return;
this.gl.stencilOp(v[0], v[1], v[2]);
this.current = v;
this.dirty = false;
}
}
class StencilTest extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
if (v) {
gl.enable(gl.STENCIL_TEST);
} else {
gl.disable(gl.STENCIL_TEST);
}
this.current = v;
this.dirty = false;
}
}
class DepthRange extends BaseValue {
getDefault() {
return [0, 1];
}
set(v) {
const c = this.current;
if (v[0] === c[0] && v[1] === c[1] && !this.dirty) return;
this.gl.depthRange(v[0], v[1]);
this.current = v;
this.dirty = false;
}
}
class DepthTest extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
if (v) {
gl.enable(gl.DEPTH_TEST);
} else {
gl.disable(gl.DEPTH_TEST);
}
this.current = v;
this.dirty = false;
}
}
class DepthFunc extends BaseValue {
getDefault() {
return this.gl.LESS;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.depthFunc(v);
this.current = v;
this.dirty = false;
}
}
class Blend extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
if (v) {
gl.enable(gl.BLEND);
} else {
gl.disable(gl.BLEND);
}
this.current = v;
this.dirty = false;
}
}
class BlendFunc extends BaseValue {
getDefault() {
const gl = this.gl;
return [gl.ONE, gl.ZERO, gl.ONE, gl.ZERO];
}
set(v) {
const c = this.current;
if (v[0] === c[0] && v[1] === c[1] && v[2] === c[2] && v[3] === c[3] && !this.dirty) return;
this.gl.blendFuncSeparate(v[0], v[1], v[2], v[3]);
this.current = v;
this.dirty = false;
}
}
class BlendColor extends BaseValue {
getDefault() {
return index$1.Color.transparent.toNonPremultipliedRenderColor(null);
}
set(v) {
const c = this.current;
if (v.r === c.r && v.g === c.g && v.b === c.b && v.a === c.a && !this.dirty) return;
this.gl.blendColor(v.r, v.g, v.b, v.a);
this.current = v;
this.dirty = false;
}
}
class BlendEquation extends BaseValue {
getDefault() {
return this.gl.FUNC_ADD;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.blendEquationSeparate(v, v);
this.current = v;
this.dirty = false;
}
}
class CullFace extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
if (v) {
gl.enable(gl.CULL_FACE);
} else {
gl.disable(gl.CULL_FACE);
}
this.current = v;
this.dirty = false;
}
}
class CullFaceSide extends BaseValue {
getDefault() {
return this.gl.BACK;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.cullFace(v);
this.current = v;
this.dirty = false;
}
}
class FrontFace extends BaseValue {
getDefault() {
return this.gl.CCW;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.frontFace(v);
this.current = v;
this.dirty = false;
}
}
let Program$1 = class Program extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.useProgram(v);
this.current = v;
this.dirty = false;
}
};
class ActiveTextureUnit extends BaseValue {
getDefault() {
return this.gl.TEXTURE0;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.gl.activeTexture(v);
this.current = v;
this.dirty = false;
}
}
class Viewport extends BaseValue {
getDefault() {
const gl = this.gl;
return [0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight];
}
set(v) {
const c = this.current;
if (v[0] === c[0] && v[1] === c[1] && v[2] === c[2] && v[3] === c[3] && !this.dirty) return;
this.gl.viewport(v[0], v[1], v[2], v[3]);
this.current = v;
this.dirty = false;
}
}
class BindFramebuffer extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, v);
this.current = v;
this.dirty = false;
}
}
class BindRenderbuffer extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.bindRenderbuffer(gl.RENDERBUFFER, v);
this.current = v;
this.dirty = false;
}
}
class BindTexture extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.bindTexture(gl.TEXTURE_2D, v);
this.current = v;
this.dirty = false;
}
}
class BindVertexBuffer extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.bindBuffer(gl.ARRAY_BUFFER, v);
this.current = v;
this.dirty = false;
}
}
class BindElementBuffer extends BaseValue {
getDefault() {
return null;
}
set(v) {
const gl = this.gl;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, v);
this.current = v;
this.dirty = false;
}
}
class BindVertexArrayOES extends BaseValue {
getDefault() {
return null;
}
set(v) {
if (!this.gl || v === this.current && !this.dirty) return;
this.gl.bindVertexArray(v);
this.current = v;
this.dirty = false;
}
}
class PixelStoreUnpack extends BaseValue {
getDefault() {
return 4;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.pixelStorei(gl.UNPACK_ALIGNMENT, v);
this.current = v;
this.dirty = false;
}
}
class PixelStoreUnpackPremultiplyAlpha extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, v);
this.current = v;
this.dirty = false;
}
}
class PixelStoreUnpackFlipY extends BaseValue {
getDefault() {
return false;
}
set(v) {
if (v === this.current && !this.dirty) return;
const gl = this.gl;
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, v);
this.current = v;
this.dirty = false;
}
}
class FramebufferAttachment extends BaseValue {
constructor(context, parent) {
super(context);
this.context = context;
this.parent = parent;
}
getDefault() {
return null;
}
}
class ColorAttachment extends FramebufferAttachment {
constructor(context, parent, attachmentIndex = 0) {
super(context, parent);
this.attachmentPoint = context.gl.COLOR_ATTACHMENT0 + attachmentIndex;
}
setDirty() {
this.dirty = true;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.context.bindFramebuffer.set(this.parent);
const gl = this.gl;
gl.framebufferTexture2D(gl.FRAMEBUFFER, this.attachmentPoint, gl.TEXTURE_2D, v, 0);
this.current = v;
this.dirty = false;
}
}
class DepthRenderbufferAttachment extends FramebufferAttachment {
attachment() {
return this.gl.DEPTH_ATTACHMENT;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.context.bindFramebuffer.set(this.parent);
const gl = this.gl;
gl.framebufferRenderbuffer(gl.FRAMEBUFFER, this.attachment(), gl.RENDERBUFFER, v);
this.current = v;
this.dirty = false;
}
}
class DepthTextureAttachment extends FramebufferAttachment {
attachment() {
return this.gl.DEPTH_ATTACHMENT;
}
set(v) {
if (v === this.current && !this.dirty) return;
this.context.bindFramebuffer.set(this.parent);
const gl = this.gl;
gl.framebufferTexture2D(gl.FRAMEBUFFER, this.attachment(), gl.TEXTURE_2D, v, 0);
this.current = v;
this.dirty = false;
}
}
class DepthStencilAttachment extends DepthRenderbufferAttachment {
attachment() {
return this.gl.DEPTH_STENCIL_ATTACHMENT;
}
}
const terrainRasterUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_image0": new index$1.Uniform1i(context),
"u_image1": new index$1.Uniform1i(context),
"u_skirt_height": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context),
"u_emissive_texture_available": new index$1.Uniform1f(context)
});
const terrainRasterUniformValues = (matrix, skirtHeight, groundShadowFactor, emissiveTextureAvailable) => ({
"u_matrix": matrix,
"u_image0": 0,
"u_image1": 1,
"u_skirt_height": skirtHeight,
"u_ground_shadow_factor": groundShadowFactor,
"u_emissive_texture_available": emissiveTextureAvailable
});
const globeRasterUniforms = (context) => ({
"u_proj_matrix": new index$1.UniformMatrix4f(context),
"u_globe_matrix": new index$1.UniformMatrix4f(context),
"u_normalize_matrix": new index$1.UniformMatrix4f(context),
"u_merc_matrix": new index$1.UniformMatrix4f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_image0": new index$1.Uniform1i(context),
"u_image1": new index$1.Uniform1i(context),
"u_grid_matrix": new index$1.UniformMatrix3f(context),
"u_skirt_height": new index$1.Uniform1f(context),
"u_far_z_cutoff": new index$1.Uniform1f(context),
"u_frustum_tl": new index$1.Uniform3f(context),
"u_frustum_tr": new index$1.Uniform3f(context),
"u_frustum_br": new index$1.Uniform3f(context),
"u_frustum_bl": new index$1.Uniform3f(context),
"u_globe_pos": new index$1.Uniform3f(context),
"u_globe_radius": new index$1.Uniform1f(context),
"u_viewport": new index$1.Uniform2f(context),
"u_emissive_texture_available": new index$1.Uniform1f(context)
});
const atmosphereUniforms = (context) => ({
"u_frustum_tl": new index$1.Uniform3f(context),
"u_frustum_tr": new index$1.Uniform3f(context),
"u_frustum_br": new index$1.Uniform3f(context),
"u_frustum_bl": new index$1.Uniform3f(context),
"u_horizon": new index$1.Uniform1f(context),
"u_transition": new index$1.Uniform1f(context),
"u_fadeout_range": new index$1.Uniform1f(context),
"u_atmosphere_fog_color": new index$1.Uniform4f(context),
"u_high_color": new index$1.Uniform4f(context),
"u_space_color": new index$1.Uniform4f(context),
"u_temporal_offset": new index$1.Uniform1f(context),
"u_horizon_angle": new index$1.Uniform1f(context)
});
const globeRasterUniformValues = (projMatrix, globeMatrix, globeMercatorMatrix, normalizeMatrix, zoomTransition, mercCenter, frustumDirTl, frustumDirTr, frustumDirBr, frustumDirBl, globePosition, globeRadius, viewport, skirtHeight, farZCutoff, emissiveTextureAvailable, gridMatrix) => ({
"u_proj_matrix": Float32Array.from(projMatrix),
"u_globe_matrix": globeMatrix,
"u_normalize_matrix": Float32Array.from(normalizeMatrix),
"u_merc_matrix": globeMercatorMatrix,
"u_zoom_transition": zoomTransition,
"u_merc_center": mercCenter,
"u_image0": 0,
"u_image1": 1,
"u_frustum_tl": frustumDirTl,
"u_frustum_tr": frustumDirTr,
"u_frustum_br": frustumDirBr,
"u_frustum_bl": frustumDirBl,
"u_globe_pos": globePosition,
"u_globe_radius": globeRadius,
"u_viewport": viewport,
"u_grid_matrix": gridMatrix ? Float32Array.from(gridMatrix) : new Float32Array(9),
"u_skirt_height": skirtHeight,
"u_far_z_cutoff": farZCutoff,
"u_emissive_texture_available": emissiveTextureAvailable
});
const atmosphereUniformValues = (frustumDirTl, frustumDirTr, frustumDirBr, frustumDirBl, horizon, transitionT, fadeoutRange, atmosphereFogColor, highColor, spaceColor, temporalOffset, horizonAngle) => ({
"u_frustum_tl": frustumDirTl,
"u_frustum_tr": frustumDirTr,
"u_frustum_br": frustumDirBr,
"u_frustum_bl": frustumDirBl,
"u_horizon": horizon,
"u_transition": transitionT,
"u_fadeout_range": fadeoutRange,
"u_atmosphere_fog_color": atmosphereFogColor.toArray01(),
"u_high_color": highColor.toArray01(),
"u_space_color": spaceColor.toArray01(),
"u_temporal_offset": temporalOffset,
"u_horizon_angle": horizonAngle
});
class VertexMorphing {
constructor() {
this.operations = {};
}
newMorphing(key, from, to, now, duration) {
index$1.assert(from.demTexture && to.demTexture);
index$1.assert(from.tileID.key !== to.tileID.key);
if (key in this.operations) {
const op = this.operations[key];
index$1.assert(op.from && op.to);
if (op.to.tileID.key !== to.tileID.key)
op.queued = to;
} else {
this.operations[key] = {
startTime: now,
phase: 0,
duration,
from,
to,
queued: null
};
}
}
getMorphValuesForProxy(key) {
if (!(key in this.operations))
return null;
const op = this.operations[key];
const from = op.from;
const to = op.to;
index$1.assert(from && to);
return { from, to, phase: op.phase };
}
update(now) {
for (const key in this.operations) {
const op = this.operations[key];
index$1.assert(op.from && op.to);
op.phase = (now - op.startTime) / op.duration;
while (op.phase >= 1 || !this._validOp(op)) {
if (!this._nextOp(op, now)) {
delete this.operations[key];
break;
}
}
}
}
_nextOp(op, now) {
if (!op.queued)
return false;
op.from = op.to;
op.to = op.queued;
op.queued = null;
op.phase = 0;
op.startTime = now;
return true;
}
_validOp(op) {
return op.from.hasData() && op.to.hasData();
}
}
function demTileChanged(prev, next) {
if (prev == null || next == null)
return false;
if (!prev.hasData() || !next.hasData())
return false;
if (prev.demTexture == null || next.demTexture == null)
return false;
return prev.tileID.key !== next.tileID.key;
}
const vertexMorphing = new VertexMorphing();
const SHADER_DEFAULT = 0;
const SHADER_MORPHING = 1;
const defaultDuration = 250;
const shaderDefines = {
"0": null,
"1": "TERRAIN_VERTEX_MORPHING"
};
function drawTerrainForGlobe(painter, terrain, sourceCache, tileIDs, now) {
const context = painter.context;
const gl = context.gl;
let program;
let programMode;
const tr = painter.transform;
const useCustomAntialiasing = index$1.globeUseCustomAntiAliasing(painter, context, tr);
const setShaderMode = (coord, mode) => {
if (programMode === mode) return;
const defines = [shaderDefines[mode], "PROJECTION_GLOBE_VIEW"];
if (useCustomAntialiasing) defines.push("CUSTOM_ANTIALIASING");
const affectedByFog = painter.isTileAffectedByFog(coord);
program = painter.getOrCreateProgram("globeRaster", { defines, overrideFog: affectedByFog });
programMode = mode;
};
const colorMode = painter.colorModeForRenderPass();
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
vertexMorphing.update(now);
const globeMercatorMatrix = index$1.calculateGlobeMercatorMatrix(tr);
const mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
const sharedBuffers = painter.globeSharedBuffers;
const viewport = [tr.width * index$1.exported$1.devicePixelRatio, tr.height * index$1.exported$1.devicePixelRatio];
const globeMatrix = Float32Array.from(tr.globeMatrix);
const elevationOptions = { useDenormalizedUpVectorScale: true };
{
const tr2 = painter.transform;
const skirtHeightValue = skirtHeight(tr2.zoom, terrain.exaggeration(), terrain.sourceCache._source.tileSize);
programMode = -1;
const primitive = gl.TRIANGLES;
for (const coord of tileIDs) {
const tile = sourceCache.getTile(coord);
const stencilMode = StencilMode.disabled;
const prevDemTile = terrain.prevTerrainTileForTile[coord.key];
const nextDemTile = terrain.terrainTileForTile[coord.key];
if (demTileChanged(prevDemTile, nextDemTile)) {
vertexMorphing.newMorphing(coord.key, prevDemTile, nextDemTile, now, defaultDuration);
}
bindEmissiveTexture(painter, tile.emissiveTexture);
context.activeTexture.set(gl.TEXTURE0);
if (tile.texture) {
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
const morph = vertexMorphing.getMorphValuesForProxy(coord.key);
const shaderMode = morph ? SHADER_MORPHING : SHADER_DEFAULT;
if (morph) {
Object.assign(elevationOptions, { morphing: { srcDemTile: morph.from, dstDemTile: morph.to, phase: index$1.easeCubicInOut(morph.phase) } });
}
const tileBounds = index$1.tileCornersToBounds(coord.canonical);
const latitudinalLod = index$1.getLatitudinalLod(tileBounds.getCenter().lat);
const gridMatrix = index$1.getGridMatrix(coord.canonical, tileBounds, latitudinalLod, tr2.worldSize / tr2._pixelsPerMercatorPixel);
const normalizeMatrix = index$1.globeNormalizeECEF(index$1.globeTileBounds(coord.canonical));
const emissiveTexture = painter.emissiveMode === "mrt-fallback" ? 1 : 0;
const uniformValues = globeRasterUniformValues(
tr2.expandedFarZProjMatrix,
globeMatrix,
globeMercatorMatrix,
normalizeMatrix,
index$1.globeToMercatorTransition(tr2.zoom),
mercatorCenter,
tr2.frustumCorners.TL,
tr2.frustumCorners.TR,
tr2.frustumCorners.BR,
tr2.frustumCorners.BL,
tr2.globeCenterInViewSpace,
tr2.globeRadius,
viewport,
skirtHeightValue,
tr2._farZ,
emissiveTexture,
gridMatrix
);
setShaderMode(coord, shaderMode);
if (!program) {
continue;
}
terrain.setupElevationDraw(tile, program, elevationOptions);
painter.uploadCommonUniforms(context, program, coord.toUnwrapped());
if (sharedBuffers) {
const [buffer, indexBuffer, segments] = sharedBuffers.getGridBuffers(latitudinalLod, skirtHeightValue !== 0);
program.draw(
painter,
primitive,
depthMode,
stencilMode,
colorMode,
CullFaceMode.backCCW,
uniformValues,
"globe_raster",
buffer,
indexBuffer,
segments
);
}
}
}
if (sharedBuffers && (painter.renderDefaultNorthPole || painter.renderDefaultSouthPole)) {
const defines = ["GLOBE_POLES", "PROJECTION_GLOBE_VIEW"];
if (useCustomAntialiasing) defines.push("CUSTOM_ANTIALIASING");
program = painter.getOrCreateProgram("globeRaster", { defines });
for (const coord of tileIDs) {
const { x, y, z } = coord.canonical;
const topCap = y === 0;
const bottomCap = y === (1 << z) - 1;
const [northPoleBuffer, southPoleBuffer, indexBuffer, segment] = sharedBuffers.getPoleBuffers(z, false);
if (segment && (topCap || bottomCap)) {
const tile = sourceCache.getTile(coord);
bindEmissiveTexture(painter, tile.emissiveTexture);
context.activeTexture.set(gl.TEXTURE0);
if (tile.texture) {
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
let poleMatrix = index$1.globePoleMatrixForTile(z, x, tr);
const normalizeMatrix = index$1.globeNormalizeECEF(index$1.globeTileBounds(coord.canonical));
const emissiveTexture = painter.emissiveMode === "mrt-fallback" ? 1 : 0;
const drawPole = (program2, vertexBuffer) => program2.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.disabled,
globeRasterUniformValues(
tr.expandedFarZProjMatrix,
poleMatrix,
poleMatrix,
normalizeMatrix,
0,
mercatorCenter,
tr.frustumCorners.TL,
tr.frustumCorners.TR,
tr.frustumCorners.BR,
tr.frustumCorners.BL,
tr.globeCenterInViewSpace,
tr.globeRadius,
viewport,
0,
tr._farZ,
emissiveTexture
),
"globe_pole_raster",
vertexBuffer,
indexBuffer,
segment
);
terrain.setupElevationDraw(tile, program, elevationOptions);
painter.uploadCommonUniforms(context, program, coord.toUnwrapped());
if (topCap && painter.renderDefaultNorthPole) {
drawPole(program, northPoleBuffer);
}
if (bottomCap && painter.renderDefaultSouthPole) {
poleMatrix = index$1.scale$3(index$1.create(), poleMatrix, [1, -1, 1]);
drawPole(program, southPoleBuffer);
}
}
}
}
}
function drawTerrainRaster(painter, terrain, sourceCache, tileIDs, now) {
if (painter.transform.projection.name === "globe") {
drawTerrainForGlobe(painter, terrain, sourceCache, tileIDs, now);
} else {
const context = painter.context;
const gl = context.gl;
let program;
let programMode;
const shadowRenderer = painter.shadowRenderer;
const cutoffParams = getCutoffParams(painter, painter.longestCutoffRange);
const setShaderMode = (mode) => {
if (programMode === mode)
return;
const modes = [];
modes.push(shaderDefines[mode]);
if (cutoffParams.shouldRenderCutoff) {
modes.push("RENDER_CUTOFF");
}
if (shadowRenderer) {
modes.push("RENDER_SHADOWS", "DEPTH_TEXTURE");
if (shadowRenderer.useNormalOffset) {
modes.push("NORMAL_OFFSET");
}
}
program = painter.getOrCreateProgram("terrainRaster", { defines: modes });
programMode = mode;
};
const colorMode = painter.colorModeForRenderPass();
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
vertexMorphing.update(now);
const tr = painter.transform;
const skirt = skirtHeight(tr.zoom, terrain.exaggeration(), terrain.sourceCache._source.tileSize);
let groundShadowFactor = [0, 0, 0];
if (shadowRenderer) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
}
{
programMode = -1;
const primitive = gl.TRIANGLES;
const [buffer, segments] = [terrain.gridIndexBuffer, terrain.gridSegments];
for (const coord of tileIDs) {
const tile = sourceCache.getTile(coord);
const stencilMode = StencilMode.disabled;
const prevDemTile = terrain.prevTerrainTileForTile[coord.key];
const nextDemTile = terrain.terrainTileForTile[coord.key];
if (demTileChanged(prevDemTile, nextDemTile)) {
vertexMorphing.newMorphing(coord.key, prevDemTile, nextDemTile, now, defaultDuration);
}
bindEmissiveTexture(painter, tile.emissiveTexture);
context.activeTexture.set(gl.TEXTURE0);
if (tile.texture) {
tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
const morph = vertexMorphing.getMorphValuesForProxy(coord.key);
const shaderMode = morph ? SHADER_MORPHING : SHADER_DEFAULT;
let elevationOptions;
if (morph) {
elevationOptions = { morphing: { srcDemTile: morph.from, dstDemTile: morph.to, phase: index$1.easeCubicInOut(morph.phase) } };
}
const emissiveTexture = painter.emissiveMode === "mrt-fallback" ? 1 : 0;
const uniformValues = terrainRasterUniformValues(coord.projMatrix, isEdgeTile(coord.canonical, tr.renderWorldCopies) ? skirt / 10 : skirt, groundShadowFactor, emissiveTexture);
setShaderMode(shaderMode);
if (!program) {
continue;
}
terrain.setupElevationDraw(tile, program, elevationOptions);
const unwrappedId = coord.toUnwrapped();
if (shadowRenderer) {
shadowRenderer.setupShadows(unwrappedId, program);
}
painter.uploadCommonUniforms(context, program, unwrappedId, null, cutoffParams);
program.draw(
painter,
primitive,
depthMode,
stencilMode,
colorMode,
CullFaceMode.backCCW,
uniformValues,
"terrain_raster",
terrain.gridBuffer,
buffer,
segments
);
}
}
}
}
function skirtHeight(zoom, terrainExaggeration, tileSize) {
if (terrainExaggeration === 0) return 0;
const exaggerationFactor = terrainExaggeration < 1 && tileSize === 514 ? 0.25 / terrainExaggeration : 1;
return 6 * Math.pow(1.5, 22 - zoom) * Math.max(terrainExaggeration, 1) * exaggerationFactor;
}
function isEdgeTile(cid, renderWorldCopies) {
const numTiles = 1 << cid.z;
return !renderWorldCopies && (cid.x === 0 || cid.x === numTiles - 1) || cid.y === 0 || cid.y === numTiles - 1;
}
function bindEmissiveTexture(painter, texture) {
if (!painter.style || !painter.style.enable3dLights()) {
return;
}
const context = painter.context;
const gl = context.gl;
context.activeTexture.set(gl.TEXTURE1);
if (texture) {
texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
} else {
painter.emptyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
}
const clippingMaskUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context)
});
const clippingMaskUniformValues = (matrix) => ({
"u_matrix": matrix
});
function rasterFade(tile, parentTile, sourceCache, transform, fadeDuration) {
if (fadeDuration > 0) {
const now = index$1.exported$1.now();
const sinceTile = (now - tile.timeAdded) / fadeDuration;
const sinceParent = parentTile ? (now - parentTile.timeAdded) / fadeDuration : -1;
const source = sourceCache.getSource();
const idealZ = transform.coveringZoomLevel({
tileSize: source.tileSize,
roundZoom: source.roundZoom
});
const fadeIn = !parentTile || Math.abs(parentTile.tileID.overscaledZ - idealZ) > Math.abs(tile.tileID.overscaledZ - idealZ);
const childOpacity = fadeIn && tile.refreshedUponExpiration ? 1 : index$1.clamp(fadeIn ? sinceTile : 1 - sinceParent, 0, 1);
if (parentTile) {
return {
opacity: 1,
mix: 1 - childOpacity,
isFading: sinceTile < 1
};
} else {
return {
opacity: childOpacity,
mix: 0,
isFading: sinceTile < 1
};
}
} else {
return {
opacity: 1,
mix: 0,
isFading: false
};
}
}
const GRID_DIM = 128;
const FBO_POOL_SIZE = 5;
const RENDER_CACHE_MAX_SIZE = 50;
class MockSourceCache extends SourceCache {
constructor(map) {
const sourceSpec = { type: "raster-dem", maxzoom: map.transform.maxZoom };
const source = create("mock-dem", sourceSpec, map.style.dispatcher, map.style);
super("mock-dem", source, false);
source.setEventedParent(this);
this._sourceLoaded = true;
}
_loadTile(tile, callback) {
tile.state = "loaded";
callback(null);
}
}
class ProxySourceCache extends SourceCache {
constructor(map) {
const source = create("proxy", {
type: "geojson",
maxzoom: map.transform.maxZoom
}, map.style.dispatcher, map.style);
super("proxy", source, false);
source.setEventedParent(this);
this.map = this.getSource().map = map;
this.used = this._sourceLoaded = true;
this.renderCache = [];
this.renderCachePool = [];
this.proxyCachedFBO = {};
}
// Override for transient nature of cover here: don't cache and retain.
update(transform, tileSize, updateForTerrain) {
if (transform.freezeTileCoverage) {
return;
}
this.transform = transform;
const idealTileIDs = transform.coveringTiles({
tileSize: this._source.tileSize,
minzoom: this._source.minzoom,
maxzoom: this._source.maxzoom,
roundZoom: this._source.roundZoom,
reparseOverscaled: this._source.reparseOverscaled
});
const incoming = idealTileIDs.reduce((acc, tileID) => {
acc[tileID.key] = "";
if (!this._tiles[tileID.key]) {
const tile = new Tile(tileID, this._source.tileSize * tileID.overscaleFactor(), transform.tileZoom, void 0, void 0, this._source.worldview);
tile.state = "loaded";
this._tiles[tileID.key] = tile;
}
return acc;
}, {});
for (const id in this._tiles) {
if (!(id in incoming)) {
this.freeFBO(id);
this._tiles[id].unloadVectorData();
delete this._tiles[id];
}
}
}
freeFBO(id) {
const fbos = this.proxyCachedFBO[id];
if (fbos !== void 0) {
const fboIds = Object.values(fbos);
this.renderCachePool.push(...fboIds);
delete this.proxyCachedFBO[id];
}
}
deallocRenderCache() {
this.renderCache.forEach((fbo) => fbo.fb.destroy());
this.renderCache = [];
this.renderCachePool = [];
this.proxyCachedFBO = {};
}
}
class ProxiedTileID extends index$1.OverscaledTileID {
constructor(tileID, proxyTileKey, projMatrix) {
super(tileID.overscaledZ, tileID.wrap, tileID.canonical.z, tileID.canonical.x, tileID.canonical.y);
this.proxyTileKey = proxyTileKey;
this.projMatrix = projMatrix;
}
}
class Terrain extends index$1.Elevation {
constructor(painter, style) {
super();
this._debugParams = { sortTilesHiZFirst: true, disableRenderCache: false };
DevTools.addParameter(this._debugParams, "sortTilesHiZFirst", "Terrain", {}, () => {
this._style.map.triggerRepaint();
});
DevTools.addParameter(this._debugParams, "disableRenderCache", "Terrain", {}, () => {
this._style.map.triggerRepaint();
});
DevTools.addButton("Terrain", "Invalidate Render Cache", () => {
this.invalidateRenderCache = true;
this._style.map.triggerRepaint();
});
this.painter = painter;
this.terrainTileForTile = {};
this.prevTerrainTileForTile = {};
const [triangleGridArray, triangleGridIndices, skirtIndicesOffset] = createGrid(GRID_DIM + 1);
const context = painter.context;
this.gridBuffer = context.createVertexBuffer(triangleGridArray, index$1.posAttributes.members);
this.gridIndexBuffer = context.createIndexBuffer(triangleGridIndices);
this.gridSegments = index$1.SegmentVector.simpleSegment(0, 0, triangleGridArray.length, triangleGridIndices.length);
this.gridNoSkirtSegments = index$1.SegmentVector.simpleSegment(0, 0, triangleGridArray.length, skirtIndicesOffset);
this.proxyCoords = [];
this.proxiedCoords = {};
this._visibleDemTiles = [];
this._drapedRenderBatches = [];
this._sourceTilesOverlap = {};
this.proxySourceCache = new ProxySourceCache(style.map);
this.orthoMatrix = index$1.create();
const epsilon = this.painter.transform.projection.name === "globe" ? 0.015 : 0;
index$1.ortho(this.orthoMatrix, epsilon, index$1.EXTENT, 0, index$1.EXTENT, 0, 1);
const gl = context.gl;
this._overlapStencilMode = new StencilMode({ func: gl.GEQUAL, mask: 255 }, 0, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
this._previousZoom = painter.transform.zoom;
this.pool = [];
this._findCoveringTileCache = {};
this._tilesDirty = {};
this.style = style;
this._useVertexMorphing = true;
this._exaggeration = 1;
this._mockSourceCache = new MockSourceCache(style.map);
this._pendingGroundEffectLayers = [];
this._emissiveTexture = false;
}
set style(style) {
style.on("data", this._onStyleDataEvent.bind(this));
this._style = style;
this._style.map.on("moveend", () => {
this._clearLineLayersFromRenderCache();
});
}
/*
* Validate terrain and update source cache used for elevation.
* Explicitly pass transform to update elevation (Transform.updateElevation)
* before using transform for source cache update.
*/
update(style, transform, adaptCameraAltitude) {
if (style && style.terrain) {
if (this._style !== style) {
this.style = style;
this._evaluationZoom = void 0;
}
const terrainProps = style.terrain.properties;
const isDrapeModeDeferred = style.terrain.drapeRenderMode === DrapeRenderMode.deferred;
const zoomDependentExaggeration = style.terrain.isZoomDependent();
this._previousUpdateTimestamp = this.enabled ? this._updateTimestamp : void 0;
this._updateTimestamp = index$1.exported$1.now();
const scope = style.terrain && style.terrain.scope;
const sourceCacheId = terrainProps.get("source");
const sourceCache = isDrapeModeDeferred ? this._mockSourceCache : style.getSourceCache(sourceCacheId, scope);
if (!sourceCache) {
index$1.warnOnce(`Couldn't find terrain source "${sourceCacheId}".`);
return;
}
this.sourceCache = sourceCache;
this._attenuationRange = style.terrain.getAttenuationRange();
this._exaggeration = zoomDependentExaggeration ? this.calculateExaggeration(transform) : terrainProps.get("exaggeration");
if (!transform.projection.requiresDraping && zoomDependentExaggeration && this._exaggeration === 0) {
this._disable();
return;
}
this.enabled = true;
const updateSourceCache = () => {
if (this.sourceCache.used) {
index$1.warnOnce(`Raster DEM source '${this.sourceCache.id}' is used both for terrain and as layer source.
This leads to lower resolution of hillshade. For full hillshade resolution but higher memory consumption, define another raster DEM source.`);
}
const scaledDemTileSize = this.getScaledDemTileSize();
this.sourceCache.update(transform, scaledDemTileSize, true);
this.resetTileLookupCache(this.sourceCache.id);
};
if (!this.sourceCache.usedForTerrain) {
this.resetTileLookupCache(this.sourceCache.id);
this.sourceCache.usedForTerrain = true;
updateSourceCache();
this._initializing = true;
}
updateSourceCache();
transform.updateElevation(true, adaptCameraAltitude);
this.resetTileLookupCache(this.proxySourceCache.id);
this.proxySourceCache.update(transform);
this._emptyDEMTextureDirty = true;
this._previousZoom = transform.zoom;
} else {
this._disable();
}
}
calculateExaggeration(transform) {
if (this._attenuationRange && transform.zoom >= Math.ceil(this._attenuationRange[1])) {
const terrainStyle2 = this._style.terrain;
return terrainStyle2.getExaggeration(transform.zoom);
}
const previousAltitude = this._previousCameraAltitude;
const altitude = transform.getFreeCameraOptions().position.z / transform.pixelsPerMeter * transform.worldSize;
this._previousCameraAltitude = altitude;
const altitudeDelta = previousAltitude != null ? altitude - previousAltitude : Number.MAX_VALUE;
if (Math.abs(altitudeDelta) < 2) {
return this._exaggeration;
}
const cameraZoom = transform.zoom;
index$1.assert(this._style.terrain);
const terrainStyle = this._style.terrain;
if (!this._previousUpdateTimestamp) {
return terrainStyle.getExaggeration(cameraZoom);
}
let zoomDelta = cameraZoom - this._previousZoom;
const previousUpdateTimestamp = this._previousUpdateTimestamp;
let z = cameraZoom;
if (this._evaluationZoom != null) {
z = this._evaluationZoom;
index$1.assert(previousAltitude != null);
if (Math.abs(cameraZoom - z) > 0.5) {
zoomDelta = 0.5 * (cameraZoom - z + zoomDelta);
}
if (zoomDelta * altitudeDelta < 0) {
z += zoomDelta;
}
}
this._evaluationZoom = z;
const evaluatedExaggeration = terrainStyle.getExaggeration(z);
index$1.assert(this._previousUpdateTimestamp != null);
const evaluatedExaggerationLowerZ = terrainStyle.getExaggeration(Math.max(0, z - 0.1));
const fixedExaggeration = evaluatedExaggeration === evaluatedExaggerationLowerZ;
const lowExaggerationTreshold = 0.1;
const exaggerationSmoothTarget = 0.01;
if (fixedExaggeration && Math.abs(evaluatedExaggeration - this._exaggeration) < exaggerationSmoothTarget) {
return evaluatedExaggeration;
}
let interpolateStrength = Math.min(0.1, (this._updateTimestamp - previousUpdateTimestamp) * 375e-5);
if (fixedExaggeration || evaluatedExaggeration < lowExaggerationTreshold || Math.abs(zoomDelta) < 1e-4) {
interpolateStrength = Math.min(0.2, interpolateStrength * 4);
}
return index$1.number(this._exaggeration, evaluatedExaggeration, interpolateStrength);
}
resetTileLookupCache(sourceCacheID) {
this._findCoveringTileCache[sourceCacheID] = {};
}
attenuationRange() {
return this._attenuationRange;
}
getDemUpscale() {
const proxyTileSize = this.proxySourceCache.getSource().tileSize;
return proxyTileSize / GRID_DIM;
}
getScaledDemTileSize() {
const demScale = this.sourceCache.getSource().tileSize / GRID_DIM;
const proxyTileSize = this.proxySourceCache.getSource().tileSize;
return demScale * proxyTileSize;
}
_onStyleDataEvent(event) {
if (event.dataType === "source" && event.coord) {
this._clearRenderCacheForTile(event.sourceCacheId, event.coord);
} else if (event.dataType === "style") {
this.invalidateRenderCache = true;
this._evaluationZoom = void 0;
this._previousUpdateTimestamp = void 0;
this._previousCameraAltitude = void 0;
}
}
// Terrain
_disable() {
if (!this.enabled) return;
this.enabled = false;
this._emptyDEMTextureDirty = true;
this._sharedDepthStencil = void 0;
this._evaluationZoom = void 0;
this._previousUpdateTimestamp = void 0;
this.proxySourceCache.deallocRenderCache();
if (this._style) {
for (const id in this._style._mergedSourceCaches) {
this._style._mergedSourceCaches[id].usedForTerrain = false;
}
}
}
destroy() {
this._disable();
if (this._emptyDEMTexture) this._emptyDEMTexture.destroy();
this.pool.forEach((fbo) => fbo.fb.destroy());
this.pool = [];
if (this.framebufferCopyTexture) this.framebufferCopyTexture.destroy();
}
// Implements Elevation::_source.
_source() {
return this.enabled ? this.sourceCache : null;
}
isUsingMockSource() {
return this.sourceCache === this._mockSourceCache;
}
// Implements Elevation::exaggeration.
exaggeration() {
return this.enabled ? this._exaggeration : 0;
}
get visibleDemTiles() {
return this._visibleDemTiles;
}
get drapeBufferSize() {
const extent = this.proxySourceCache.getSource().tileSize * 2;
return [extent, extent];
}
set useVertexMorphing(enable) {
this._useVertexMorphing = enable;
}
// For every renderable coordinate in every source cache, assign one proxy
// tile (see _setupProxiedCoordsForOrtho). Mapping of source tile to proxy
// tile is modeled by ProxiedTileID. In general case, source and proxy tile
// are of different zoom: ProxiedTileID.projMatrix models ortho, scale and
// translate from source to proxy. This matrix is used when rendering source
// tile to proxy tile's texture.
// One proxy tile can have multiple source tiles, or pieces of source tiles,
// that get rendered to it.
// For each proxy tile we assign one terrain tile (_assignTerrainTiles). The
// terrain tile provides elevation data when rendering (draping) proxy tile
// texture over terrain grid.
updateTileBinding(sourcesCoords) {
if (!this.enabled) return;
this.prevTerrainTileForTile = this.terrainTileForTile;
const proxySourceCache = this.proxySourceCache;
const tr = this.painter.transform;
if (this._initializing) {
this._initializing = tr._centerAltitude === 0 && this.getAtPointOrZero(index$1.MercatorCoordinate.fromLngLat(tr.center), -1) === -1;
this._emptyDEMTextureDirty = !this._initializing;
}
const coords = this.proxyCoords = proxySourceCache.getIds().map((id) => {
const tileID = proxySourceCache.getTileByID(id).tileID;
tileID.projMatrix = tr.calculateProjMatrix(tileID.toUnwrapped());
return tileID;
});
sortByDistanceToCamera(coords, this.painter);
const previousProxyToSource = this.proxyToSource || {};
this.proxyToSource = {};
coords.forEach((tileID) => {
this.proxyToSource[tileID.key] = {};
});
this.terrainTileForTile = {};
const sourceCaches = this._style._mergedSourceCaches;
for (const fqid in sourceCaches) {
const sourceCache = sourceCaches[fqid];
if (!sourceCache.used) continue;
if (sourceCache !== this.sourceCache) this.resetTileLookupCache(sourceCache.id);
this._setupProxiedCoordsForOrtho(sourceCache, sourcesCoords[fqid], previousProxyToSource);
if (sourceCache.usedForTerrain) continue;
const coordinates = sourcesCoords[fqid];
if (sourceCache.getSource().reparseOverscaled) {
this._assignTerrainTiles(coordinates);
}
}
this.proxiedCoords[proxySourceCache.id] = coords.map((tileID) => new ProxiedTileID(tileID, tileID.key, this.orthoMatrix));
this._assignTerrainTiles(coords);
this._prepareDEMTextures();
this._setupDrapedRenderBatches();
this._initFBOPool();
this._setupRenderCache(previousProxyToSource);
this.renderingToTexture = false;
const visibleKeys = {};
this._visibleDemTiles = [];
for (const id of this.proxyCoords) {
const demTile = this.terrainTileForTile[id.key];
if (!demTile)
continue;
const key = demTile.tileID.key;
if (key in visibleKeys)
continue;
this._visibleDemTiles.push(demTile);
visibleKeys[key] = key;
}
}
_assignTerrainTiles(coords) {
if (this._initializing) return;
coords.forEach((tileID) => {
if (this.terrainTileForTile[tileID.key]) return;
const demTile = this._findTileCoveringTileID(tileID, this.sourceCache);
if (demTile) this.terrainTileForTile[tileID.key] = demTile;
});
}
_prepareDEMTextures() {
const context = this.painter.context;
const gl = context.gl;
for (const key in this.terrainTileForTile) {
const tile = this.terrainTileForTile[key];
const dem = tile.dem;
if (dem && (!tile.demTexture || tile.needsDEMTextureUpload)) {
context.activeTexture.set(gl.TEXTURE1);
prepareDEMTexture(this.painter, tile, dem);
}
}
}
_prepareDemTileUniforms(proxyTile, demTile, uniforms, uniformSuffix) {
if (!demTile || demTile.demTexture == null)
return false;
index$1.assert(demTile.dem);
const proxyId = proxyTile.tileID.canonical;
const demId = demTile.tileID.canonical;
const demScaleBy = Math.pow(2, demId.z - proxyId.z);
const suffix = uniformSuffix || "";
uniforms[`u_dem_tl${suffix}`] = [proxyId.x * demScaleBy % 1, proxyId.y * demScaleBy % 1];
uniforms[`u_dem_scale${suffix}`] = demScaleBy;
return true;
}
get emptyDEMTexture() {
return !this._emptyDEMTextureDirty && this._emptyDEMTexture ? this._emptyDEMTexture : this._updateEmptyDEMTexture();
}
_getLoadedAreaMinimum() {
if (!this.enabled) return 0;
let nonzero = 0;
const min = this._visibleDemTiles.reduce((acc, tile) => {
if (!tile.dem) return acc;
const m = tile.dem.tree.minimums[0];
acc += m;
if (m > 0) nonzero++;
return acc;
}, 0);
return nonzero ? min / nonzero : 0;
}
_updateEmptyDEMTexture() {
const context = this.painter.context;
const gl = context.gl;
context.activeTexture.set(gl.TEXTURE2);
const min = this._getLoadedAreaMinimum();
const image = new index$1.Float32Image({ width: 1, height: 1 }, new Float32Array([min]));
this._emptyDEMTextureDirty = false;
let texture = this._emptyDEMTexture;
if (!texture) {
texture = this._emptyDEMTexture = new index$1.Texture(context, image, gl.R32F, { premultiply: false });
} else {
texture.update(image, { premultiply: false });
}
return texture;
}
// useDepthForOcclusion: Pre-rendered depth texture is used for occlusion
// useMeterToDem: u_meter_to_dem uniform is not used for all terrain programs,
// optimization to avoid unnecessary computation and upload.
setupElevationDraw(tile, program, options) {
const context = this.painter.context;
const gl = context.gl;
const uniforms = defaultTerrainUniforms();
uniforms["u_exaggeration"] = this.exaggeration();
let demTile = null;
let prevDemTile = null;
let morphingPhase = 1;
if (options && options.morphing && this._useVertexMorphing) {
const srcTile = options.morphing.srcDemTile;
const dstTile = options.morphing.dstDemTile;
morphingPhase = options.morphing.phase;
if (srcTile && dstTile) {
if (this._prepareDemTileUniforms(tile, srcTile, uniforms, "_prev"))
prevDemTile = srcTile;
if (this._prepareDemTileUniforms(tile, dstTile, uniforms))
demTile = dstTile;
}
}
const filteringForDemTile = (tile2) => {
if (!tile2 || !tile2.demTexture) {
return gl.NEAREST;
}
return this.painter.linearFloatFilteringSupported() ? gl.LINEAR : gl.NEAREST;
};
const setDemSizeUniform = (demTexture2) => {
uniforms["u_dem_size"] = demTexture2.size[0] === 1 ? 1 : demTexture2.size[0] - 2;
};
let demTexture = null;
if (!this.enabled) {
demTexture = this.emptyDEMTexture;
} else if (prevDemTile && demTile) {
demTexture = demTile.demTexture;
context.activeTexture.set(gl.TEXTURE4);
prevDemTile.demTexture.bind(filteringForDemTile(prevDemTile), gl.CLAMP_TO_EDGE);
uniforms["u_dem_lerp"] = morphingPhase;
} else {
demTile = this.terrainTileForTile[tile.tileID.key];
demTexture = this._prepareDemTileUniforms(tile, demTile, uniforms) ? demTile.demTexture : this.emptyDEMTexture;
}
index$1.assert(demTexture);
context.activeTexture.set(gl.TEXTURE2);
if (demTexture) {
setDemSizeUniform(demTexture);
demTexture.bind(filteringForDemTile(demTile), gl.CLAMP_TO_EDGE);
}
this.painter.setupDepthForOcclusion(options && options.useDepthForOcclusion, program, uniforms);
if (options && options.useMeterToDem && demTile) {
const meterToDEM = (1 << demTile.tileID.canonical.z) * index$1.mercatorZfromAltitude(1, this.painter.transform.center.lat) * this.sourceCache.getSource().tileSize;
uniforms["u_meter_to_dem"] = meterToDEM;
}
if (options && options.labelPlaneMatrixInv) {
uniforms["u_label_plane_matrix_inv"] = options.labelPlaneMatrixInv;
}
program.setTerrainUniformValues(context, uniforms);
if (this.painter.transform.projection.name === "globe") {
const globeUniforms2 = this.globeUniformValues(this.painter.transform, tile.tileID.canonical, options && options.useDenormalizedUpVectorScale);
program.setGlobeUniformValues(context, globeUniforms2);
}
}
globeUniformValues(tr, id, useDenormalizedUpVectorScale) {
const projection = tr.projection;
return {
"u_tile_tl_up": projection.upVector(id, 0, 0),
"u_tile_tr_up": projection.upVector(id, index$1.EXTENT, 0),
"u_tile_br_up": projection.upVector(id, index$1.EXTENT, index$1.EXTENT),
"u_tile_bl_up": projection.upVector(id, 0, index$1.EXTENT),
"u_tile_up_scale": useDenormalizedUpVectorScale ? index$1.globeMetersToEcef(1) : projection.upVectorScale(id, tr.center.lat, tr.worldSize).metersToTile
};
}
renderToBackBuffer(accumulatedDrapes) {
const painter = this.painter;
const context = this.painter.context;
if (accumulatedDrapes.length === 0) {
return;
}
context.bindFramebuffer.set(null);
context.viewport.set([0, 0, painter.width, painter.height]);
painter.gpuTimingDeferredRenderStart();
this.renderingToTexture = false;
drawTerrainRaster(painter, this, this.proxySourceCache, accumulatedDrapes, this._updateTimestamp);
this.renderingToTexture = true;
painter.gpuTimingDeferredRenderEnd();
accumulatedDrapes.splice(0, accumulatedDrapes.length);
}
// For each proxy tile, render all layers until the non-draped layer (and
// render the tile to the screen) before advancing to the next proxy tile.
// Returns the last drawn index that is used as a start
// layer for interleaved draped rendering.
// Apart to layer-by-layer rendering used in 2D, here we have proxy-tile-by-proxy-tile
// rendering.
renderBatch(startLayerIndex) {
if (this._drapedRenderBatches.length === 0) {
return startLayerIndex + 1;
}
this.renderingToTexture = true;
const painter = this.painter;
const context = this.painter.context;
const proxySourceCache = this.proxySourceCache;
const proxies = this.proxiedCoords[proxySourceCache.id];
const drapedLayerBatch = this._drapedRenderBatches.shift();
index$1.assert(drapedLayerBatch.start === startLayerIndex);
const layerIds = painter.style.order;
const accumulatedDrapes = [];
const needsEmissiveTexture = painter.emissiveMode === "mrt-fallback";
this._updateFBOs(needsEmissiveTexture);
let poolIndex = 0;
for (const proxy of proxies) {
const tile = proxySourceCache.getTileByID(proxy.proxyTileKey);
const renderCacheIndex = proxySourceCache.proxyCachedFBO[proxy.key] ? proxySourceCache.proxyCachedFBO[proxy.key][startLayerIndex] : void 0;
const fbo = renderCacheIndex !== void 0 ? proxySourceCache.renderCache[renderCacheIndex] : this.pool[poolIndex++];
const useRenderCache = renderCacheIndex !== void 0;
tile.texture = fbo.tex;
tile.emissiveTexture = fbo.emissiveTex;
if (useRenderCache && !fbo.dirty) {
accumulatedDrapes.push(tile.tileID);
continue;
}
context.bindFramebuffer.set(fbo.fb.framebuffer);
const gl = context.gl;
if (painter.emissiveMode === "mrt-fallback") {
index$1.assert(fbo.emissiveTex);
gl.drawBuffers([gl.COLOR_ATTACHMENT0, gl.COLOR_ATTACHMENT1]);
} else {
gl.drawBuffers([gl.COLOR_ATTACHMENT0]);
}
this.renderedToTile = false;
if (fbo.dirty) {
context.clear({ color: index$1.Color.transparent, stencil: 0 });
fbo.dirty = false;
}
let currentStencilSource;
for (let j = drapedLayerBatch.start; j <= drapedLayerBatch.end; ++j) {
const layer = painter.style._mergedLayers[layerIds[j]];
const hidden = layer.isHidden(painter.transform.zoom);
index$1.assert(this._style.isLayerDraped(layer) || hidden);
if (hidden) continue;
const sourceCache = painter.style.getLayerSourceCache(layer);
const proxiedCoords = sourceCache ? this.proxyToSource[proxy.key][sourceCache.id] : [proxy];
if (!proxiedCoords) continue;
const coords = proxiedCoords;
context.viewport.set([0, 0, fbo.fb.width, fbo.fb.height]);
if (currentStencilSource !== (sourceCache ? sourceCache.id : null)) {
this._setupStencil(fbo, proxiedCoords, layer, sourceCache);
currentStencilSource = sourceCache ? sourceCache.id : null;
}
painter.renderLayer(painter, sourceCache, layer, coords);
}
gl.drawBuffers([gl.COLOR_ATTACHMENT0]);
const isLastBatch = this._drapedRenderBatches.length === 0;
if (isLastBatch) {
for (const id of this._pendingGroundEffectLayers) {
const layer = painter.style._mergedLayers[layerIds[id]];
if (layer.isHidden(painter.transform.zoom)) continue;
const sourceCache = painter.style.getLayerSourceCache(layer);
const proxiedCoords = sourceCache ? this.proxyToSource[proxy.key][sourceCache.id] : [proxy];
if (!proxiedCoords) continue;
const coords = proxiedCoords;
context.viewport.set([0, 0, fbo.fb.width, fbo.fb.height]);
if (currentStencilSource !== (sourceCache ? sourceCache.id : null)) {
this._setupStencil(fbo, proxiedCoords, layer, sourceCache);
currentStencilSource = sourceCache ? sourceCache.id : null;
}
painter.renderLayer(painter, sourceCache, layer, coords);
}
}
if (this.renderedToTile) {
fbo.dirty = true;
accumulatedDrapes.push(tile.tileID);
} else if (!useRenderCache) {
--poolIndex;
index$1.assert(poolIndex >= 0);
}
if (poolIndex === FBO_POOL_SIZE) {
poolIndex = 0;
this.renderToBackBuffer(accumulatedDrapes);
}
}
this.renderToBackBuffer(accumulatedDrapes);
this.renderingToTexture = false;
context.bindFramebuffer.set(null);
context.viewport.set([0, 0, painter.width, painter.height]);
return drapedLayerBatch.end + 1;
}
postRender() {
index$1.assert(this._drapedRenderBatches.length === 0);
}
isLayerOrderingCorrect(style) {
const layerCount = style.order.length;
let drapedMax = -1;
let immediateMin = layerCount;
for (let i = 0; i < layerCount; ++i) {
const layer = style._mergedLayers[style.order[i]];
if (this._style.isLayerDraped(layer)) {
drapedMax = Math.max(drapedMax, i);
} else {
immediateMin = Math.min(immediateMin, i);
}
}
return immediateMin > drapedMax;
}
getMinElevationBelowMSL() {
let min = 0;
const maxDEMError = 30;
this._visibleDemTiles.filter((tile) => tile.dem).forEach((tile) => {
const minMaxTree = tile.dem.tree;
min = Math.min(min, minMaxTree.minimums[0]);
});
return min === 0 ? min : (min - maxDEMError) * this._exaggeration;
}
// Performs raycast against visible DEM tiles on the screen and returns the distance travelled along the ray.
// x & y components of the position are expected to be in normalized mercator coordinates [0, 1] and z in meters.
raycast(pos, dir, exaggeration) {
if (!this._visibleDemTiles)
return null;
const preparedTiles = this._visibleDemTiles.filter((tile) => tile.dem).map((tile) => {
const id = tile.tileID;
const tiles = 1 << id.overscaledZ;
const { x, y } = id.canonical;
const minx = x / tiles;
const maxx = (x + 1) / tiles;
const miny = y / tiles;
const maxy = (y + 1) / tiles;
const tree = tile.dem.tree;
return {
minx,
miny,
maxx,
maxy,
t: tree.raycastRoot(minx, miny, maxx, maxy, pos, dir, exaggeration),
tile
};
});
preparedTiles.sort((a, b) => {
const at = a.t !== null ? a.t : Number.MAX_VALUE;
const bt = b.t !== null ? b.t : Number.MAX_VALUE;
return at - bt;
});
for (const obj of preparedTiles) {
if (obj.t == null)
return null;
const tree = obj.tile.dem.tree;
const t = tree.raycast(obj.minx, obj.miny, obj.maxx, obj.maxy, pos, dir, exaggeration);
if (t != null)
return t;
}
return null;
}
_createFBO() {
const painter = this.painter;
const context = painter.context;
const gl = context.gl;
const bufferSize = this.drapeBufferSize;
context.activeTexture.set(gl.TEXTURE0);
const tex = new index$1.Texture(context, { width: bufferSize[0], height: bufferSize[1], data: null }, gl.RGBA8);
tex.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
const fb = context.createFramebuffer(bufferSize[0], bufferSize[1], 1, null);
fb.colorAttachment0.set(tex.texture);
let emissiveTex;
if (this._emissiveTexture) {
emissiveTex = new index$1.Texture(context, { width: bufferSize[0], height: bufferSize[1], data: null }, gl.R8);
emissiveTex.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
fb.createColorAttachment(context, 1);
fb.colorAttachment1.set(emissiveTex.texture);
}
fb.depthAttachment = new DepthStencilAttachment(context, fb.framebuffer);
if (this._sharedDepthStencil === void 0) {
this._sharedDepthStencil = context.createRenderbuffer(context.gl.DEPTH_STENCIL, bufferSize[0], bufferSize[1]);
this._stencilRef = 0;
fb.depthAttachment.set(this._sharedDepthStencil);
context.clear({ stencil: 0 });
} else {
fb.depthAttachment.set(this._sharedDepthStencil);
}
if (context.extTextureFilterAnisotropic) {
gl.texParameterf(
gl.TEXTURE_2D,
context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
context.extTextureFilterAnisotropicMax
);
}
return { fb, tex, emissiveTex, dirty: false };
}
_updateFBOs(needsEmissiveTexture) {
if (this._emissiveTexture === needsEmissiveTexture) return;
for (const fbo of this.pool) {
this._updateFBO(fbo, needsEmissiveTexture);
}
for (const fbo of this.proxySourceCache.renderCache) {
this._updateFBO(fbo, needsEmissiveTexture);
}
this._emissiveTexture = needsEmissiveTexture;
}
_updateFBO(fbo, needsEmissiveTexture) {
index$1.assert(!!fbo.emissiveTex !== needsEmissiveTexture);
const fb = fbo.fb;
const context = this.painter.context;
const gl = context.gl;
const bufferSize = this.drapeBufferSize;
if (needsEmissiveTexture) {
const emissiveTex = new index$1.Texture(context, { width: bufferSize[0], height: bufferSize[1], data: null }, gl.R8);
emissiveTex.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
fbo.emissiveTex = emissiveTex;
fb.createColorAttachment(context, 1);
fb.colorAttachment1.set(emissiveTex.texture);
} else {
fbo.emissiveTex = void 0;
fb.removeColorAttachment(context, 1);
}
fbo.dirty = true;
}
_initFBOPool() {
while (this.pool.length < Math.min(FBO_POOL_SIZE, this.proxyCoords.length)) {
this.pool.push(this._createFBO());
}
}
_shouldDisableRenderCache() {
if (this._debugParams.disableRenderCache) {
return true;
}
if (this._style.hasLightTransitions()) {
return true;
}
for (const id in this._style._mergedSourceCaches) {
if (this._style._mergedSourceCaches[id].hasTransition()) {
return true;
}
}
const isTransitioning = (id) => {
const layer = this._style._mergedLayers[id];
const isHidden = layer.isHidden(this.painter.transform.zoom);
if (layer.type === "hillshade") {
return !isHidden && layer.shouldRedrape();
}
if (layer.type === "custom") {
return !isHidden && layer.shouldRedrape();
}
return !isHidden && layer.hasTransition();
};
return this._style.order.some(isTransitioning);
}
_clearLineLayersFromRenderCache() {
let hasVectorSource = false;
for (const source of this._style.getSources()) {
if (source instanceof VectorTileSource) {
hasVectorSource = true;
break;
}
}
if (!hasVectorSource) return;
const clearSourceCaches = {};
for (let i = 0; i < this._style.order.length; ++i) {
const layer = this._style._mergedLayers[this._style.order[i]];
const sourceCache = this._style.getLayerSourceCache(layer);
if (!sourceCache || clearSourceCaches[sourceCache.id]) continue;
const isHidden = layer.isHidden(this.painter.transform.zoom);
if (isHidden || layer.type !== "line") continue;
const widthExpression = layer.widthExpression();
const emissiveStrengthExpression = layer.emissiveStrengthExpression();
if (!(widthExpression instanceof index$1.ZoomDependentExpression) && !(emissiveStrengthExpression instanceof index$1.ZoomDependentExpression)) continue;
clearSourceCaches[sourceCache.id] = true;
for (const proxy of this.proxyCoords) {
const proxiedCoords = this.proxyToSource[proxy.key][sourceCache.id];
const coords = proxiedCoords;
if (!coords) continue;
for (const coord of coords) {
this._clearRenderCacheForTile(sourceCache.id, coord);
}
}
}
}
_clearRasterLayersFromRenderCache() {
let hasRasterSource = false;
for (const id in this._style._mergedSourceCaches) {
if (this._style._mergedSourceCaches[id]._source instanceof RasterTileSource) {
hasRasterSource = true;
break;
}
}
if (!hasRasterSource) return;
const clearSourceCaches = {};
for (let i = 0; i < this._style.order.length; ++i) {
const layer = this._style._mergedLayers[this._style.order[i]];
const sourceCache = this._style.getLayerSourceCache(layer);
if (!sourceCache || clearSourceCaches[sourceCache.id]) continue;
const isHidden = layer.isHidden(this.painter.transform.zoom);
if (isHidden || layer.type !== "raster") continue;
const fadeDuration = layer.paint.get("raster-fade-duration");
for (const proxy of this.proxyCoords) {
const proxiedCoords = this.proxyToSource[proxy.key][sourceCache.id];
const coords = proxiedCoords;
if (!coords) continue;
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const parent = sourceCache.findLoadedParent(coord, 0);
const fade = rasterFade(tile, parent, sourceCache, this.painter.transform, fadeDuration);
const isFading = fade.opacity !== 1 || fade.mix !== 0;
if (isFading) {
this._clearRenderCacheForTile(sourceCache.id, coord);
}
}
}
}
}
_setupDrapedRenderBatches() {
this._style.updateDrapeFirstLayers();
const layerIds = this._style.order;
const layerCount = layerIds.length;
if (layerCount === 0) {
return;
}
const batches = [];
this._pendingGroundEffectLayers = [];
let currentLayer = 0;
let layer = this._style._mergedLayers[layerIds[currentLayer]];
while (!this._style.isLayerDraped(layer) && layer.isHidden(this.painter.transform.zoom) && ++currentLayer < layerCount) {
layer = this._style._mergedLayers[layerIds[currentLayer]];
}
let batchStart;
for (; currentLayer < layerCount; ++currentLayer) {
const layer2 = this._style._mergedLayers[layerIds[currentLayer]];
if (layer2.isHidden(this.painter.transform.zoom)) {
continue;
}
if (!this._style.isLayerDraped(layer2)) {
if (layer2.type === "fill-extrusion") {
this._pendingGroundEffectLayers.push(currentLayer);
}
if (batchStart !== void 0) {
batches.push({ start: batchStart, end: currentLayer - 1 });
batchStart = void 0;
}
continue;
}
if (batchStart === void 0) {
batchStart = currentLayer;
}
}
if (batchStart !== void 0) {
batches.push({ start: batchStart, end: currentLayer - 1 });
}
index$1.assert(batches.length === 1 || batches.length === 0);
if (batches.length !== 0) {
const lastBatch = batches[batches.length - 1];
const groundEffectLayersComeLast = this._pendingGroundEffectLayers.every((id) => {
return id > lastBatch.end;
});
if (!groundEffectLayersComeLast) {
index$1.warnOnce("fill-extrusion with flood lighting and/or ground ambient occlusion should be moved to be on top of all draped layers.");
}
}
this._drapedRenderBatches = batches;
}
_setupRenderCache(previousProxyToSource) {
const psc = this.proxySourceCache;
if (this._shouldDisableRenderCache() || this.invalidateRenderCache) {
this.invalidateRenderCache = false;
if (psc.renderCache.length > psc.renderCachePool.length) {
const used = Object.values(psc.proxyCachedFBO);
psc.proxyCachedFBO = {};
for (let i = 0; i < used.length; ++i) {
const fbos = Object.values(used[i]);
psc.renderCachePool.push(...fbos);
}
index$1.assert(psc.renderCache.length === psc.renderCachePool.length);
}
return;
}
this._clearRasterLayersFromRenderCache();
const coords = this.proxyCoords;
const dirty = this._tilesDirty;
for (let i = coords.length - 1; i >= 0; i--) {
const proxy = coords[i];
const tile = psc.getTileByID(proxy.key);
if (psc.proxyCachedFBO[proxy.key] !== void 0) {
index$1.assert(tile.texture);
const prev = previousProxyToSource[proxy.key];
index$1.assert(prev);
const current = this.proxyToSource[proxy.key];
let equal = 0;
for (const source in current) {
const tiles = current[source];
const prevTiles = prev[source];
if (!prevTiles || prevTiles.length !== tiles.length || tiles.some((t, index) => t !== prevTiles[index] || dirty[source] && dirty[source].hasOwnProperty(t.key))) {
equal = -1;
break;
}
++equal;
}
for (const proxyFBO in psc.proxyCachedFBO[proxy.key]) {
psc.renderCache[psc.proxyCachedFBO[proxy.key][proxyFBO]].dirty = equal < 0 || equal !== Object.values(prev).length;
}
}
}
const sortedRenderBatches = [...this._drapedRenderBatches];
sortedRenderBatches.sort((batchA, batchB) => {
const batchASize = batchA.end - batchA.start;
const batchBSize = batchB.end - batchB.start;
return batchBSize - batchASize;
});
for (const batch of sortedRenderBatches) {
for (const id of coords) {
if (psc.proxyCachedFBO[id.key]) {
continue;
}
let index = psc.renderCachePool.pop();
if (index === void 0 && psc.renderCache.length < RENDER_CACHE_MAX_SIZE) {
index = psc.renderCache.length;
psc.renderCache.push(this._createFBO());
}
if (index !== void 0) {
psc.proxyCachedFBO[id.key] = {};
psc.proxyCachedFBO[id.key][batch.start] = index;
psc.renderCache[index].dirty = true;
}
}
}
this._tilesDirty = {};
}
_setupStencil(fbo, proxiedCoords, layer, sourceCache) {
if (!sourceCache || !this._sourceTilesOverlap[sourceCache.id]) {
if (this._overlapStencilType) this._overlapStencilType = false;
return;
}
const context = this.painter.context;
const gl = context.gl;
if (proxiedCoords.length <= 1) {
this._overlapStencilType = false;
return;
}
let stencilRange;
if (layer.isTileClipped()) {
stencilRange = proxiedCoords.length;
this._overlapStencilMode.test = { func: gl.EQUAL, mask: 255 };
this._overlapStencilType = "Clip";
} else if (proxiedCoords[0].overscaledZ > proxiedCoords[proxiedCoords.length - 1].overscaledZ) {
stencilRange = 1;
this._overlapStencilMode.test = { func: gl.GREATER, mask: 255 };
this._overlapStencilType = "Mask";
} else {
this._overlapStencilType = false;
return;
}
if (this._stencilRef + stencilRange > 255) {
context.clear({ stencil: 0 });
this._stencilRef = 0;
}
this._stencilRef += stencilRange;
this._overlapStencilMode.ref = this._stencilRef;
if (layer.isTileClipped()) {
this._renderTileClippingMasks(proxiedCoords, this._overlapStencilMode.ref);
}
}
clipOrMaskOverlapStencilType() {
return this._overlapStencilType === "Clip" || this._overlapStencilType === "Mask";
}
stencilModeForRTTOverlap(id) {
if (!this.renderingToTexture || !this._overlapStencilType) {
return StencilMode.disabled;
}
if (this._overlapStencilType === "Clip") {
this._overlapStencilMode.ref = this.painter._tileClippingMaskIDs[id.key];
}
return this._overlapStencilMode;
}
_renderTileClippingMasks(proxiedCoords, ref) {
const painter = this.painter;
const context = this.painter.context;
const gl = context.gl;
painter._tileClippingMaskIDs = {};
context.setColorMode(ColorMode.disabled);
context.setDepthMode(DepthMode.disabled);
const program = painter.getOrCreateProgram("clippingMask");
for (const tileID of proxiedCoords) {
const id = painter._tileClippingMaskIDs[tileID.key] = --ref;
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
// Tests will always pass, and ref value will be written to stencil buffer.
new StencilMode({ func: gl.ALWAYS, mask: 0 }, id, 255, gl.KEEP, gl.KEEP, gl.REPLACE),
ColorMode.disabled,
CullFaceMode.disabled,
clippingMaskUniformValues(tileID.projMatrix),
"$clipping",
painter.tileExtentBuffer,
painter.quadTriangleIndexBuffer,
painter.tileExtentSegments
);
}
}
// Casts a ray from a point on screen and returns the intersection point with the terrain.
// The returned point contains the mercator coordinates in its first 3 components, and elevation
// in meter in its 4th coordinate.
pointCoordinate(screenPoint) {
const transform = this.painter.transform;
if (screenPoint.x < 0 || screenPoint.x > transform.width || screenPoint.y < 0 || screenPoint.y > transform.height) {
return null;
}
const far = [screenPoint.x, screenPoint.y, 1, 1];
index$1.transformMat4$1(far, far, transform.pixelMatrixInverse);
index$1.scale$2(far, far, 1 / far[3]);
far[0] /= transform.worldSize;
far[1] /= transform.worldSize;
const camera = transform._camera.position;
const mercatorZScale = index$1.mercatorZfromAltitude(1, transform.center.lat);
const p = [camera[0], camera[1], camera[2] / mercatorZScale, 0];
const dir = index$1.subtract([], far.slice(0, 3), p);
index$1.normalize(dir, dir);
const exaggeration = this._exaggeration;
const distanceAlongRay = this.raycast(p, dir, exaggeration);
if (distanceAlongRay === null || !distanceAlongRay) return null;
index$1.scaleAndAdd(p, p, dir, distanceAlongRay);
p[3] = p[2];
p[2] *= mercatorZScale;
return p;
}
_setupProxiedCoordsForOrtho(sourceCache, sourceCoords, previousProxyToSource) {
if (sourceCache.getSource() instanceof index$1.ImageSource) {
return this._setupProxiedCoordsForImageSource(sourceCache, sourceCoords, previousProxyToSource);
}
this._findCoveringTileCache[sourceCache.id] = this._findCoveringTileCache[sourceCache.id] || {};
const coords = this.proxiedCoords[sourceCache.id] = [];
const proxys = this.proxyCoords;
for (let i = 0; i < proxys.length; i++) {
const proxyTileID = proxys[i];
const proxied = this._findTileCoveringTileID(proxyTileID, sourceCache);
if (proxied) {
index$1.assert(proxied.hasData());
const id = this._createProxiedId(proxyTileID, proxied, previousProxyToSource[proxyTileID.key] && previousProxyToSource[proxyTileID.key][sourceCache.id]);
coords.push(id);
this.proxyToSource[proxyTileID.key][sourceCache.id] = [id];
}
}
let hasOverlap = false;
const proxiesToSort = /* @__PURE__ */ new Set();
for (let i = 0; i < sourceCoords.length; i++) {
const tile = sourceCache.getTile(sourceCoords[i]);
if (!tile || !tile.hasData()) continue;
const proxy = this._findTileCoveringTileID(tile.tileID, this.proxySourceCache);
if (proxy && proxy.tileID.canonical.z !== tile.tileID.canonical.z) {
const array = this.proxyToSource[proxy.tileID.key][sourceCache.id];
const id = this._createProxiedId(proxy.tileID, tile, previousProxyToSource[proxy.tileID.key] && previousProxyToSource[proxy.tileID.key][sourceCache.id]);
if (!array) {
this.proxyToSource[proxy.tileID.key][sourceCache.id] = [id];
} else {
array.splice(array.length - 1, 0, id);
}
const arr = this.proxyToSource[proxy.tileID.key][sourceCache.id];
if (!proxiesToSort.has(arr)) {
proxiesToSort.add(arr);
}
coords.push(id);
hasOverlap = true;
}
}
this._sourceTilesOverlap[sourceCache.id] = hasOverlap;
if (hasOverlap && this._debugParams.sortTilesHiZFirst) {
for (const arr of proxiesToSort) {
arr.sort((a, b) => {
return b.overscaledZ - a.overscaledZ;
});
}
}
}
_setupProxiedCoordsForImageSource(sourceCache, sourceCoords, previousProxyToSource) {
if (!sourceCache.getSource().loaded()) return;
const coords = this.proxiedCoords[sourceCache.id] = [];
const proxys = this.proxyCoords;
const imageSource = sourceCache.getSource();
const tileID = imageSource.tileID;
if (!tileID) return;
const anchor = new index$1.Point(tileID.x, tileID.y)._div(1 << tileID.z);
const aabb = imageSource.coordinates.map(index$1.MercatorCoordinate.fromLngLat).reduce((acc, coord) => {
acc.min.x = Math.min(acc.min.x, coord.x - anchor.x);
acc.min.y = Math.min(acc.min.y, coord.y - anchor.y);
acc.max.x = Math.max(acc.max.x, coord.x - anchor.x);
acc.max.y = Math.max(acc.max.y, coord.y - anchor.y);
return acc;
}, { min: new index$1.Point(Number.MAX_VALUE, Number.MAX_VALUE), max: new index$1.Point(-Number.MAX_VALUE, -Number.MAX_VALUE) });
const tileOutsideImage = (tileID2, imageTileID) => {
const x = tileID2.wrap + tileID2.canonical.x / (1 << tileID2.canonical.z);
const y = tileID2.canonical.y / (1 << tileID2.canonical.z);
const d = index$1.EXTENT / (1 << tileID2.canonical.z);
const ix = imageTileID.wrap + imageTileID.canonical.x / (1 << imageTileID.canonical.z);
const iy = imageTileID.canonical.y / (1 << imageTileID.canonical.z);
return x + d < ix + aabb.min.x || x > ix + aabb.max.x || y + d < iy + aabb.min.y || y > iy + aabb.max.y;
};
for (let i = 0; i < proxys.length; i++) {
const proxyTileID = proxys[i];
for (let j = 0; j < sourceCoords.length; j++) {
const tile = sourceCache.getTile(sourceCoords[j]);
if (!tile || !tile.hasData()) continue;
if (tileOutsideImage(proxyTileID, tile.tileID)) continue;
const id = this._createProxiedId(proxyTileID, tile, previousProxyToSource[proxyTileID.key] && previousProxyToSource[proxyTileID.key][sourceCache.id]);
const array = this.proxyToSource[proxyTileID.key][sourceCache.id];
if (!array) {
this.proxyToSource[proxyTileID.key][sourceCache.id] = [id];
} else {
array.push(id);
}
coords.push(id);
}
}
}
// recycle is previous pass content that likely contains proxied ID combining proxy and source tile.
_createProxiedId(proxyTileID, tile, recycle) {
let matrix = this.orthoMatrix;
if (recycle) {
const recycled = recycle.find((proxied) => proxied.key === tile.tileID.key);
if (recycled) return recycled;
}
if (tile.tileID.key !== proxyTileID.key) {
const scale = proxyTileID.canonical.z - tile.tileID.canonical.z;
matrix = index$1.create();
let size, xOffset, yOffset;
const wrap = tile.tileID.wrap - proxyTileID.wrap << proxyTileID.overscaledZ;
if (scale > 0) {
size = index$1.EXTENT >> scale;
xOffset = size * ((tile.tileID.canonical.x << scale) - proxyTileID.canonical.x + wrap);
yOffset = size * ((tile.tileID.canonical.y << scale) - proxyTileID.canonical.y);
} else {
size = index$1.EXTENT << -scale;
xOffset = index$1.EXTENT * (tile.tileID.canonical.x - (proxyTileID.canonical.x + wrap << -scale));
yOffset = index$1.EXTENT * (tile.tileID.canonical.y - (proxyTileID.canonical.y << -scale));
}
index$1.ortho(matrix, 0, size, 0, size, 0, 1);
index$1.translate(matrix, matrix, [xOffset, yOffset, 0]);
}
return new ProxiedTileID(tile.tileID, proxyTileID.key, matrix);
}
// A variant of SourceCache.findLoadedParent that considers only visible
// tiles (and doesn't check SourceCache._cache). Another difference is in
// caching "not found" results along the lookup, to leave the lookup early.
// Not found is cached by this._findCoveringTileCache[key] = null;
_findTileCoveringTileID(tileID, sourceCache) {
let tile = sourceCache.getTile(tileID);
if (tile && tile.hasData()) return tile;
const lookup = this._findCoveringTileCache[sourceCache.id];
const key = lookup[tileID.key];
tile = key ? sourceCache.getTileByID(key) : null;
if (tile && tile.hasData() || key === null) return tile;
index$1.assert(!key || tile);
let sourceTileID = tile ? tile.tileID : tileID;
let z = sourceTileID.overscaledZ;
const minzoom = sourceCache.getSource().minzoom;
const path = [];
if (!key) {
const maxzoom = sourceCache.getSource().maxzoom;
if (tileID.canonical.z >= maxzoom) {
const downscale = tileID.canonical.z - maxzoom;
if (sourceCache.getSource().reparseOverscaled) {
z = Math.max(tileID.canonical.z + 2, sourceCache.transform.tileZoom);
sourceTileID = new index$1.OverscaledTileID(
z,
tileID.wrap,
maxzoom,
tileID.canonical.x >> downscale,
tileID.canonical.y >> downscale
);
} else if (downscale !== 0) {
z = maxzoom;
sourceTileID = new index$1.OverscaledTileID(
z,
tileID.wrap,
maxzoom,
tileID.canonical.x >> downscale,
tileID.canonical.y >> downscale
);
}
}
if (sourceTileID.key !== tileID.key) {
path.push(sourceTileID.key);
tile = sourceCache.getTile(sourceTileID);
}
}
const pathToLookup = (key2) => {
path.forEach((id) => {
lookup[id] = key2;
});
path.length = 0;
};
for (z = z - 1; z >= minzoom && !(tile && tile.hasData()); z--) {
if (tile) {
pathToLookup(tile.tileID.key);
}
const id = sourceTileID.calculateScaledKey(z);
tile = sourceCache.getTileByID(id);
if (tile && tile.hasData()) break;
const key2 = lookup[id];
if (key2 === null) {
break;
} else if (key2 !== void 0) {
tile = sourceCache.getTileByID(key2);
index$1.assert(tile);
continue;
}
path.push(id);
}
pathToLookup(tile ? tile.tileID.key : null);
return tile && tile.hasData() ? tile : null;
}
findDEMTileFor(tileID) {
return this.enabled ? this._findTileCoveringTileID(tileID, this.sourceCache) : null;
}
/*
* Bookkeeping if something gets rendered to the tile.
*/
prepareDrawTile() {
this.renderedToTile = true;
}
_clearRenderCacheForTile(sourceCacheFQID, coord) {
let sourceTiles = this._tilesDirty[sourceCacheFQID];
if (!sourceTiles) sourceTiles = this._tilesDirty[sourceCacheFQID] = {};
sourceTiles[coord.key] = true;
}
}
function sortByDistanceToCamera(tileIDs, painter) {
const cameraCoordinate = painter.transform.pointCoordinate(painter.transform.getCameraPoint());
const cameraPoint = new index$1.Point(cameraCoordinate.x, cameraCoordinate.y);
tileIDs.sort((a, b) => {
if (b.overscaledZ - a.overscaledZ) return b.overscaledZ - a.overscaledZ;
const aPoint = new index$1.Point(a.canonical.x + (1 << a.canonical.z) * a.wrap, a.canonical.y);
const bPoint = new index$1.Point(b.canonical.x + (1 << b.canonical.z) * b.wrap, b.canonical.y);
const cameraScaled = cameraPoint.mult(1 << a.canonical.z);
cameraScaled.x -= 0.5;
cameraScaled.y -= 0.5;
return cameraScaled.distSqr(aPoint) - cameraScaled.distSqr(bPoint);
});
}
function createGrid(count) {
const boundsArray = new index$1.StructArrayLayout2i4();
const indexArray = new index$1.StructArrayLayout3ui6();
const size = count + 2;
boundsArray.reserve(size * size);
indexArray.reserve((size - 1) * (size - 1) * 2);
const step = index$1.EXTENT / (count - 1);
const gridBound = index$1.EXTENT + step / 2;
const bound = gridBound + step;
const skirtOffset = 24575;
for (let y = -step; y < bound; y += step) {
for (let x = -step; x < bound; x += step) {
const offset = x < 0 || x > gridBound || y < 0 || y > gridBound ? skirtOffset : 0;
const xi = index$1.clamp(Math.round(x), 0, index$1.EXTENT);
const yi = index$1.clamp(Math.round(y), 0, index$1.EXTENT);
boundsArray.emplaceBack(xi + offset, yi);
}
}
const skirtIndicesOffset = (size - 3) * (size - 3) * 2;
const quad = (i, j) => {
const index = j * size + i;
indexArray.emplaceBack(index + 1, index, index + size);
indexArray.emplaceBack(index + size, index + size + 1, index + 1);
};
for (let j = 1; j < size - 2; j++) {
for (let i = 1; i < size - 2; i++) {
quad(i, j);
}
}
[0, size - 2].forEach((j) => {
for (let i = 0; i < size - 1; i++) {
quad(i, j);
quad(j, i);
}
});
return [boundsArray, indexArray, skirtIndicesOffset];
}
const terrainUniforms = (context) => ({
"u_dem": new index$1.Uniform1i(context),
"u_dem_prev": new index$1.Uniform1i(context),
"u_dem_tl": new index$1.Uniform2f(context),
"u_dem_scale": new index$1.Uniform1f(context),
"u_dem_tl_prev": new index$1.Uniform2f(context),
"u_dem_scale_prev": new index$1.Uniform1f(context),
"u_dem_size": new index$1.Uniform1f(context),
"u_dem_lerp": new index$1.Uniform1f(context),
"u_exaggeration": new index$1.Uniform1f(context),
"u_depth": new index$1.Uniform1i(context),
"u_depth_size_inv": new index$1.Uniform2f(context),
"u_depth_range_unpack": new index$1.Uniform2f(context),
"u_occluder_half_size": new index$1.Uniform1f(context),
"u_occlusion_depth_offset": new index$1.Uniform1f(context),
"u_meter_to_dem": new index$1.Uniform1f(context),
"u_label_plane_matrix_inv": new index$1.UniformMatrix4f(context)
});
function defaultTerrainUniforms() {
return {
"u_dem": 2,
"u_dem_prev": 4,
"u_dem_tl": [0, 0],
"u_dem_tl_prev": [0, 0],
"u_dem_scale": 0,
"u_dem_scale_prev": 0,
"u_dem_size": 0,
"u_dem_lerp": 1,
"u_depth": 3,
"u_depth_size_inv": [0, 0],
"u_depth_range_unpack": [0, 1],
"u_occluder_half_size": 16,
"u_occlusion_depth_offset": -1e-4,
"u_exaggeration": 0
};
}
const globeUniforms = (context) => ({
"u_tile_tl_up": new index$1.Uniform3f(context),
"u_tile_tr_up": new index$1.Uniform3f(context),
"u_tile_br_up": new index$1.Uniform3f(context),
"u_tile_bl_up": new index$1.Uniform3f(context),
"u_tile_up_scale": new index$1.Uniform1f(context)
});
const fogUniforms = (context) => ({
"u_fog_matrix": new index$1.UniformMatrix4f(context),
"u_fog_range": new index$1.Uniform2f(context),
"u_fog_color": new index$1.Uniform4f(context),
"u_fog_horizon_blend": new index$1.Uniform1f(context),
"u_fog_vertical_limit": new index$1.Uniform2f(context),
"u_fog_temporal_offset": new index$1.Uniform1f(context),
"u_frustum_tl": new index$1.Uniform3f(context),
"u_frustum_tr": new index$1.Uniform3f(context),
"u_frustum_br": new index$1.Uniform3f(context),
"u_frustum_bl": new index$1.Uniform3f(context),
"u_globe_pos": new index$1.Uniform3f(context),
"u_globe_radius": new index$1.Uniform1f(context),
"u_globe_transition": new index$1.Uniform1f(context),
"u_is_globe": new index$1.Uniform1i(context),
"u_viewport": new index$1.Uniform2f(context)
});
const fogUniformValues = (painter, fog, tileID, fogOpacity, frustumDirTl, frustumDirTr, frustumDirBr, frustumDirBl, globePosition, globeRadius, viewport, fogMatrix) => {
const tr = painter.transform;
const ignoreLUT = fog.properties.get("color-use-theme") === "none";
const fogColor = fog.properties.get("color").toNonPremultipliedRenderColor(ignoreLUT ? null : painter.style.getLut(fog.scope)).toArray01();
fogColor[3] = fogOpacity;
const temporalOffset = painter.frameCounter / 1e3 % 1;
const [verticalRangeMin, verticalRangeMax] = fog.properties.get("vertical-range");
return {
"u_fog_matrix": tileID ? tr.calculateFogTileMatrix(tileID) : fogMatrix ? fogMatrix : painter.identityMat,
"u_fog_range": fog.getFovAdjustedRange(tr._fov),
"u_fog_color": fogColor,
"u_fog_horizon_blend": fog.properties.get("horizon-blend"),
"u_fog_vertical_limit": [Math.min(verticalRangeMin, verticalRangeMax), verticalRangeMax],
"u_fog_temporal_offset": temporalOffset,
"u_frustum_tl": frustumDirTl,
"u_frustum_tr": frustumDirTr,
"u_frustum_br": frustumDirBr,
"u_frustum_bl": frustumDirBl,
"u_globe_pos": globePosition,
"u_globe_radius": globeRadius,
"u_viewport": viewport,
"u_globe_transition": index$1.globeToMercatorTransition(tr.zoom),
"u_is_globe": +(tr.projection.name === "globe")
};
};
const lightsUniforms = (context) => ({
"u_lighting_ambient_color": new index$1.Uniform3f(context),
"u_lighting_directional_dir": new index$1.Uniform3f(context),
"u_lighting_directional_color": new index$1.Uniform3f(context),
"u_ground_radiance": new index$1.Uniform3f(context)
});
function calculateAmbientDirectionalFactor(dir, normal, dirColor) {
const NdotL = index$1.dot(normal, dir);
const factorReductionMax = 0.3;
const dirLuminance = index$1.dot(dirColor, [0.2126, 0.7152, 0.0722]);
const directionalFactorMin = 1 - factorReductionMax * Math.min(dirLuminance, 1);
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const ambientDirectionalFactor = lerp(directionalFactorMin, 1, Math.min(NdotL + 1, 1));
const verticalFactorMin = 0.92;
const verticalFactor = lerp(verticalFactorMin, 1, Math.asin(index$1.clamp(normal[2], -1, 1)) / Math.PI + 0.5);
return verticalFactor * ambientDirectionalFactor;
}
function calculateGroundRadiance(dir, dirColor, ambientColor) {
const groundNormal = [0, 0, 1];
const ambientDirectionalFactor = calculateAmbientDirectionalFactor(dir, groundNormal, dirColor);
const ambientContrib = [0, 0, 0];
index$1.scale$1(ambientContrib, ambientColor.slice(0, 3), ambientDirectionalFactor);
const dirContrib = [0, 0, 0];
index$1.scale$1(dirContrib, dirColor.slice(0, 3), dir[2]);
const radiance = [0, 0, 0];
index$1.add$1(radiance, ambientContrib, dirContrib);
return index$1.linearVec3TosRGB(radiance);
}
const lightsUniformValues = (directional, ambient, style) => {
const direction = directional.properties.get("direction");
const dirIgnoreLut = directional.properties.get("color-use-theme") === "none";
const directionalColor = directional.properties.get("color").toNonPremultipliedRenderColor(dirIgnoreLut ? null : style.getLut(directional.scope)).toArray01();
const directionalIntensity = directional.properties.get("intensity");
const ambIgnoreLut = ambient.properties.get("color-use-theme") === "none";
const ambientColor = ambient.properties.get("color").toNonPremultipliedRenderColor(ambIgnoreLut ? null : style.getLut(ambient.scope)).toArray01();
const ambientIntensity = ambient.properties.get("intensity");
const dirVec = [direction.x, direction.y, direction.z];
const ambientColorLinear = index$1.sRGBToLinearAndScale(ambientColor, ambientIntensity);
const directionalColorLinear = index$1.sRGBToLinearAndScale(directionalColor, directionalIntensity);
const groundRadianceSrgb = calculateGroundRadiance(dirVec, directionalColorLinear, ambientColorLinear);
return {
"u_lighting_ambient_color": ambientColorLinear,
"u_lighting_directional_dir": dirVec,
"u_lighting_directional_color": directionalColorLinear,
"u_ground_radiance": groundRadianceSrgb
};
};
const debugWireframe2DLayerProgramNames = [
"fill",
"fillOutline",
"fillPattern",
"line",
"linePattern",
"background",
"backgroundPattern",
"hillshade",
"raster"
];
const debugWireframe3DLayerProgramNames = [
"stars",
"rainParticle",
"snowParticle",
"fillExtrusion",
"fillExtrusionGroundEffect",
"building",
"buildingBloom",
"elevatedStructures",
"model",
"symbol"
];
const instancingUniforms = (context) => ({
"u_instanceID": new index$1.Uniform1i(context)
});
class Program {
static cacheKey(source, name, defines, programConfiguration) {
const parts = [name];
if (programConfiguration) {
parts.push(programConfiguration.cacheKey);
}
for (const define of defines) {
if (source.usedDefines.has(define)) {
parts.push(define);
}
}
return parts.join("/");
}
constructor(context, name, source, configuration, fixedUniforms, fixedDefines) {
const gl = context.gl;
this.program = gl.createProgram();
this.configuration = configuration;
this.name = name;
this.fixedDefines = [...fixedDefines];
const configDefines = configuration ? configuration.defines() : [];
const defines = configDefines.concat(fixedDefines.map((define) => `#define ${define}`)).join("\n");
const definesBlock = `#version 300 es
${defines}`;
const fragmentParts = [definesBlock, FRAGMENT_PRELUDE_BLOCK];
for (const include of source.fragmentIncludes) {
fragmentParts.push(includeMap[include]);
}
fragmentParts.push(source.fragmentSource);
const fragmentSource = fragmentParts.join("\n");
const vertexParts = [definesBlock, VERTEX_PRELUDE_BLOCK];
for (const include of source.vertexIncludes) {
vertexParts.push(includeMap[include]);
}
this.forceManualRenderingForInstanceIDShaders = context.forceManualRenderingForInstanceIDShaders && source.vertexSource.includes("gl_InstanceID");
if (this.forceManualRenderingForInstanceIDShaders) {
vertexParts.push("uniform int u_instanceID;");
}
vertexParts.push(source.vertexSource);
let vertexSource = vertexParts.join("\n");
if (this.forceManualRenderingForInstanceIDShaders) {
vertexSource = vertexSource.replaceAll("gl_InstanceID", "u_instanceID");
}
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
if (gl.isContextLost()) {
this.failedToCreate = true;
return;
}
gl.shaderSource(fragmentShader, fragmentSource);
gl.compileShader(fragmentShader);
index$1.assert(gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(fragmentShader));
gl.attachShader(this.program, fragmentShader);
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
if (gl.isContextLost()) {
this.failedToCreate = true;
return;
}
gl.shaderSource(vertexShader, vertexSource);
gl.compileShader(vertexShader);
index$1.assert(gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS), gl.getShaderInfoLog(vertexShader));
gl.attachShader(this.program, vertexShader);
this.attributes = {};
gl.linkProgram(this.program);
index$1.assert(gl.getProgramParameter(this.program, gl.LINK_STATUS), gl.getProgramInfoLog(this.program));
gl.deleteShader(vertexShader);
gl.deleteShader(fragmentShader);
this.fixedUniforms = fixedUniforms(context);
this.fixedUniformsEntries = Object.entries(this.fixedUniforms);
this.binderUniforms = configuration ? configuration.getUniforms(context) : [];
if (this.forceManualRenderingForInstanceIDShaders) {
this.instancingUniforms = instancingUniforms(context);
}
if (fixedDefines.includes("TERRAIN") || name.includes("symbol") || name.includes("circle")) {
this.terrainUniforms = terrainUniforms(context);
}
if (fixedDefines.includes("GLOBE")) {
this.globeUniforms = globeUniforms(context);
}
if (fixedDefines.includes("FOG")) {
this.fogUniforms = fogUniforms(context);
}
if (fixedDefines.includes("RENDER_CUTOFF")) {
this.cutoffUniforms = cutoffUniforms(context);
}
if (fixedDefines.includes("LIGHTING_3D_MODE")) {
this.lightsUniforms = lightsUniforms(context);
}
if (fixedDefines.includes("RENDER_SHADOWS")) {
this.shadowUniforms = shadowUniforms(context);
}
}
getAttributeLocation(gl, name) {
let location = this.attributes[name];
if (location === void 0) {
location = this.attributes[name] = gl.getAttribLocation(this.program, name);
}
return location;
}
setTerrainUniformValues(context, terrainUniformValues) {
if (!this.terrainUniforms) return;
const uniforms = this.terrainUniforms;
if (this.failedToCreate) return;
context.program.set(this.program);
for (const name in terrainUniformValues) {
if (uniforms[name]) {
uniforms[name].set(this.program, name, terrainUniformValues[name]);
}
}
}
setGlobeUniformValues(context, globeUniformValues) {
if (!this.globeUniforms) return;
const uniforms = this.globeUniforms;
if (this.failedToCreate) return;
context.program.set(this.program);
for (const name in globeUniformValues) {
if (uniforms[name]) {
uniforms[name].set(this.program, name, globeUniformValues[name]);
}
}
}
setFogUniformValues(context, fogUniformValues) {
if (!this.fogUniforms) return;
const uniforms = this.fogUniforms;
if (this.failedToCreate) return;
context.program.set(this.program);
for (const name in fogUniformValues) {
uniforms[name].set(this.program, name, fogUniformValues[name]);
}
}
setCutoffUniformValues(context, cutoffUniformValues) {
if (!this.cutoffUniforms) return;
const uniforms = this.cutoffUniforms;
if (this.failedToCreate) return;
context.program.set(this.program);
for (const name in cutoffUniformValues) {
uniforms[name].set(this.program, name, cutoffUniformValues[name]);
}
}
setLightsUniformValues(context, lightsUniformValues) {
if (!this.lightsUniforms) return;
const uniforms = this.lightsUniforms;
if (this.failedToCreate) return;
context.program.set(this.program);
for (const name in lightsUniformValues) {
uniforms[name].set(this.program, name, lightsUniformValues[name]);
}
}
setShadowUniformValues(context, shadowUniformValues) {
if (this.failedToCreate || !this.shadowUniforms) return;
const uniforms = this.shadowUniforms;
context.program.set(this.program);
for (const name in shadowUniformValues) {
uniforms[name].set(this.program, name, shadowUniformValues[name]);
}
}
_drawDebugWireframe(painter, depthMode, stencilMode, colorMode, indexBuffer, segment, currentProperties, zoom, configuration, instanceCount) {
const wireframe = painter.options.wireframe;
if (wireframe.terrain === false && wireframe.layers2D === false && wireframe.layers3D === false) {
return;
}
const context = painter.context;
const subjectForWireframe = (() => {
if (wireframe.terrain && (this.name === "terrainRaster" || this.name === "globeRaster")) {
return true;
}
const drapingInProgress = painter._terrain && painter._terrain.renderingToTexture;
if (wireframe.layers2D && !drapingInProgress) {
if (debugWireframe2DLayerProgramNames.includes(this.name)) {
return true;
}
}
if (wireframe.layers3D) {
if (debugWireframe3DLayerProgramNames.includes(this.name)) {
return true;
}
}
return false;
})();
if (!subjectForWireframe) {
return;
}
const gl = context.gl;
const linesIndexBuffer = painter.wireframeDebugCache.getLinesFromTrianglesBuffer(painter.frameCounter, indexBuffer, context);
if (!linesIndexBuffer) {
return;
}
const debugDefines = [...this.fixedDefines, "DEBUG_WIREFRAME"];
const debugProgram = painter.getOrCreateProgram(this.name, { config: this.configuration, defines: debugDefines });
context.program.set(debugProgram.program);
const copyUniformValues = (group, pSrc, pDst) => {
if (pSrc[group] && pDst[group]) {
for (const name in pSrc[group]) {
if (pDst[group][name]) {
pDst[group][name].set(pDst.program, name, pSrc[group][name].current);
}
}
}
};
if (configuration) {
configuration.setUniforms(debugProgram.program, context, debugProgram.binderUniforms, currentProperties, { zoom });
}
copyUniformValues("fixedUniforms", this, debugProgram);
copyUniformValues("terrainUniforms", this, debugProgram);
copyUniformValues("globeUniforms", this, debugProgram);
copyUniformValues("fogUniforms", this, debugProgram);
copyUniformValues("lightsUniforms", this, debugProgram);
copyUniformValues("shadowUniforms", this, debugProgram);
linesIndexBuffer.bind();
context.setColorMode(new ColorMode(
[gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE],
index$1.Color.transparent,
[true, true, true, false]
));
context.setDepthMode(new DepthMode(depthMode.func === gl.LESS ? gl.LEQUAL : depthMode.func, DepthMode.ReadOnly, depthMode.range));
context.setStencilMode(StencilMode.disabled);
const count = segment.primitiveLength * 3 * 2;
const offset = segment.primitiveOffset * 3 * 2 * 2;
if (this.forceManualRenderingForInstanceIDShaders) {
const renderInstanceCount = instanceCount ? instanceCount : 1;
for (let i = 0; i < renderInstanceCount; ++i) {
debugProgram.instancingUniforms["u_instanceID"].set(this.program, "u_instanceID", i);
gl.drawElements(
gl.LINES,
count,
gl.UNSIGNED_SHORT,
offset
);
}
} else {
if (instanceCount && instanceCount > 1) {
gl.drawElementsInstanced(
gl.LINES,
count,
gl.UNSIGNED_SHORT,
offset,
instanceCount
);
} else {
gl.drawElements(
gl.LINES,
count,
gl.UNSIGNED_SHORT,
offset
);
}
}
indexBuffer.bind();
context.program.set(this.program);
context.setDepthMode(depthMode);
context.setStencilMode(stencilMode);
context.setColorMode(colorMode);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
checkUniforms(name, define, uniforms) {
if (this.fixedDefines.includes(define)) {
for (const key of Object.keys(uniforms)) {
if (!uniforms[key].initialized) {
throw new Error(`Program '${this.name}', from draw '${name}': uniform ${key} not set but required by ${define} being defined`);
}
}
}
}
draw(painter, drawMode, depthMode, stencilMode, colorMode, cullFaceMode, uniformValues, layerID, layoutVertexBuffer, indexBuffer, segments, currentProperties, zoom, configuration, dynamicLayoutBuffers, instanceCount) {
const context = painter.context;
const gl = context.gl;
if (this.failedToCreate) return;
context.program.set(this.program);
context.setDepthMode(depthMode);
context.setStencilMode(stencilMode);
context.setColorMode(colorMode);
context.setCullFace(cullFaceMode);
for (const [name, uniform] of this.fixedUniformsEntries) {
uniform.set(this.program, name, uniformValues[name]);
}
if (configuration) {
configuration.setUniforms(this.program, context, this.binderUniforms, currentProperties, { zoom });
}
const primitiveSize = {
[gl.POINTS]: 1,
[gl.LINES]: 2,
[gl.TRIANGLES]: 3,
[gl.LINE_STRIP]: 1
}[drawMode];
this.checkUniforms(layerID, "RENDER_SHADOWS", this.shadowUniforms);
const dynamicBuffers = dynamicLayoutBuffers || [];
const paintVertexBuffers = configuration ? configuration.getPaintVertexBuffers() : [];
const shouldDrawWireframe = drawMode === gl.TRIANGLES && indexBuffer;
const vertexAttribDivisorValue = instanceCount && instanceCount > 0 ? 1 : void 0;
for (const segment of segments.get()) {
const vaos = segment.vaos || (segment.vaos = {});
const vao = vaos[layerID] || (vaos[layerID] = new VertexArrayObject());
vao.bind(
context,
this,
layoutVertexBuffer,
paintVertexBuffers,
indexBuffer,
segment.vertexOffset,
dynamicBuffers,
vertexAttribDivisorValue
);
if (this.forceManualRenderingForInstanceIDShaders) {
const renderInstanceCount = instanceCount ? instanceCount : 1;
for (let i = 0; i < renderInstanceCount; ++i) {
this.instancingUniforms["u_instanceID"].set(this.program, "u_instanceID", i);
if (indexBuffer) {
gl.drawElements(
drawMode,
segment.primitiveLength * primitiveSize,
gl.UNSIGNED_SHORT,
segment.primitiveOffset * primitiveSize * 2
);
} else {
gl.drawArrays(drawMode, segment.vertexOffset, segment.vertexLength);
}
}
} else {
if (instanceCount && instanceCount > 1) {
index$1.assert(indexBuffer);
gl.drawElementsInstanced(
drawMode,
segment.primitiveLength * primitiveSize,
gl.UNSIGNED_SHORT,
segment.primitiveOffset * primitiveSize * 2,
instanceCount
);
} else if (indexBuffer) {
gl.drawElements(
drawMode,
segment.primitiveLength * primitiveSize,
gl.UNSIGNED_SHORT,
segment.primitiveOffset * primitiveSize * 2
);
} else {
gl.drawArrays(drawMode, segment.vertexOffset, segment.vertexLength);
}
}
if (shouldDrawWireframe) {
this._drawDebugWireframe(
painter,
depthMode,
stencilMode,
colorMode,
indexBuffer,
segment,
currentProperties,
zoom,
configuration,
instanceCount
);
}
}
}
}
function patternUniformValues(painter, tile, patternTransition = 0) {
const numTiles = Math.pow(2, tile.tileID.overscaledZ);
const tileSizeAtNearestZoom = tile.tileSize * Math.pow(2, painter.transform.tileZoom) / numTiles;
const pixelX = tileSizeAtNearestZoom * (tile.tileID.canonical.x + tile.tileID.wrap * numTiles);
const pixelY = tileSizeAtNearestZoom * tile.tileID.canonical.y;
return {
"u_image": 0,
"u_texsize": tile.imageAtlasTexture ? tile.imageAtlasTexture.size : [0, 0],
"u_tile_units_to_pixels": 1 / index$1.pixelsToTileUnits(tile, 1, painter.transform.tileZoom),
// split the pixel coord into two pairs of 16 bit numbers. The glsl spec only guarantees 16 bits of precision.
"u_pixel_coord_upper": [pixelX >> 16, pixelY >> 16],
"u_pixel_coord_lower": [pixelX & 65535, pixelY & 65535],
"u_pattern_transition": patternTransition
};
}
function bgPatternUniformValues(image, scope, patternPosition, painter, isViewport, tile) {
index$1.assert(patternPosition);
const { width, height } = painter.imageManager.getPixelSize(scope);
const numTiles = Math.pow(2, tile.tileID.overscaledZ);
const tileSizeAtNearestZoom = tile.tileSize * Math.pow(2, painter.transform.tileZoom) / numTiles;
const pixelX = tileSizeAtNearestZoom * (tile.tileID.canonical.x + tile.tileID.wrap * numTiles);
const pixelY = tileSizeAtNearestZoom * tile.tileID.canonical.y;
return {
"u_image": 0,
"u_pattern_tl": patternPosition.tl,
"u_pattern_br": patternPosition.br,
"u_texsize": [width, height],
"u_pattern_size": patternPosition.displaySize,
"u_pattern_units_to_pixels": isViewport ? [painter.transform.width, -1 * painter.transform.height] : [1 / index$1.pixelsToTileUnits(tile, 1, painter.transform.tileZoom), 1 / index$1.pixelsToTileUnits(tile, 1, painter.transform.tileZoom)],
// split the pixel coord into two pairs of 16 bit numbers. The glsl spec only guarantees 16 bits of precision.
"u_pixel_coord_upper": [pixelX >> 16, pixelY >> 16],
"u_pixel_coord_lower": [pixelX & 65535, pixelY & 65535]
};
}
const fillExtrusionAlignmentType = {
"terrain": 0,
"flat": 1
};
const fillExtrusionUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_lightpos": new index$1.Uniform3f(context),
"u_lightintensity": new index$1.Uniform1f(context),
"u_lightcolor": new index$1.Uniform3f(context),
"u_vertical_gradient": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_edge_radius": new index$1.Uniform1f(context),
"u_width_scale": new index$1.Uniform1f(context),
"u_ao": new index$1.Uniform2f(context),
"u_height_type": new index$1.Uniform1i(context),
"u_base_type": new index$1.Uniform1i(context),
// globe uniforms:
"u_tile_id": new index$1.Uniform3f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_inv_rot_matrix": new index$1.UniformMatrix4f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_up_dir": new index$1.Uniform3f(context),
"u_height_lift": new index$1.Uniform1f(context),
"u_flood_light_color": new index$1.Uniform3f(context),
"u_vertical_scale": new index$1.Uniform1f(context),
"u_flood_light_intensity": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context)
});
const fillExtrusionDepthUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_edge_radius": new index$1.Uniform1f(context),
"u_width_scale": new index$1.Uniform1f(context),
"u_vertical_scale": new index$1.Uniform1f(context),
"u_height_type": new index$1.Uniform1i(context),
"u_base_type": new index$1.Uniform1i(context)
});
const fillExtrusionPatternUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_lightpos": new index$1.Uniform3f(context),
"u_lightintensity": new index$1.Uniform1f(context),
"u_lightcolor": new index$1.Uniform3f(context),
"u_vertical_gradient": new index$1.Uniform1f(context),
"u_height_factor": new index$1.Uniform1f(context),
"u_edge_radius": new index$1.Uniform1f(context),
"u_width_scale": new index$1.Uniform1f(context),
"u_ao": new index$1.Uniform2f(context),
"u_height_type": new index$1.Uniform1i(context),
"u_base_type": new index$1.Uniform1i(context),
// globe uniforms:
"u_tile_id": new index$1.Uniform3f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_inv_rot_matrix": new index$1.UniformMatrix4f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_up_dir": new index$1.Uniform3f(context),
"u_height_lift": new index$1.Uniform1f(context),
// pattern uniforms
"u_image": new index$1.Uniform1i(context),
"u_texsize": new index$1.Uniform2f(context),
"u_pixel_coord_upper": new index$1.Uniform2f(context),
"u_pixel_coord_lower": new index$1.Uniform2f(context),
"u_tile_units_to_pixels": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_pattern_transition": new index$1.Uniform1f(context)
});
const fillExtrusionGroundEffectUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_ao_pass": new index$1.Uniform1f(context),
"u_meter_to_tile": new index$1.Uniform1f(context),
"u_ao": new index$1.Uniform2f(context),
"u_flood_light_intensity": new index$1.Uniform1f(context),
"u_flood_light_color": new index$1.Uniform3f(context),
"u_attenuation": new index$1.Uniform1f(context),
"u_edge_radius": new index$1.Uniform1f(context),
"u_fb": new index$1.Uniform1i(context),
"u_fb_size": new index$1.Uniform1f(context),
"u_dynamic_offset": new index$1.Uniform1f(context)
});
const identityMatrix$2 = index$1.create();
const fillExtrusionUniformValues = (matrix, painter, shouldUseVerticalGradient, opacity, aoIntensityRadius, edgeRadius, lineWidthScale, coord, heightLift, heightAlignment, baseAlignment, zoomTransition, mercatorCenter, invMatrix, floodLightColor, verticalScale, floodLightIntensity, groundShadowFactor) => {
const light = painter.style.light;
const _lp = light.properties.get("position");
const lightPos = [_lp.x, _lp.y, _lp.z];
const lightMat = index$1.create$3();
const anchor = light.properties.get("anchor");
if (anchor === "viewport") {
index$1.fromRotation(lightMat, -painter.transform.angle);
index$1.transformMat3(lightPos, lightPos, lightMat);
}
const lightColor = light.properties.get("color").toPremultipliedRenderColor(null);
const tr = painter.transform;
const uniformValues = {
"u_matrix": matrix,
"u_lightpos": lightPos,
"u_lightintensity": light.properties.get("intensity"),
"u_lightcolor": [lightColor.r, lightColor.g, lightColor.b],
"u_vertical_gradient": +shouldUseVerticalGradient,
"u_opacity": opacity,
"u_tile_id": [0, 0, 0],
"u_zoom_transition": 0,
"u_inv_rot_matrix": identityMatrix$2,
"u_merc_center": [0, 0],
"u_up_dir": [0, 0, 0],
"u_height_lift": 0,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"u_height_type": fillExtrusionAlignmentType[heightAlignment],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"u_base_type": fillExtrusionAlignmentType[baseAlignment],
"u_ao": aoIntensityRadius,
"u_edge_radius": edgeRadius,
"u_width_scale": lineWidthScale,
"u_flood_light_color": floodLightColor,
"u_vertical_scale": verticalScale,
"u_flood_light_intensity": floodLightIntensity,
"u_ground_shadow_factor": groundShadowFactor
};
if (tr.projection.name === "globe") {
uniformValues["u_tile_id"] = [coord.canonical.x, coord.canonical.y, 1 << coord.canonical.z];
uniformValues["u_zoom_transition"] = zoomTransition;
uniformValues["u_inv_rot_matrix"] = invMatrix;
uniformValues["u_merc_center"] = mercatorCenter;
uniformValues["u_up_dir"] = tr.projection.upVector(new index$1.CanonicalTileID(0, 0, 0), mercatorCenter[0] * index$1.EXTENT, mercatorCenter[1] * index$1.EXTENT);
uniformValues["u_height_lift"] = heightLift;
}
return uniformValues;
};
const fillExtrusionDepthUniformValues = (matrix, edgeRadius, lineWidthScale, verticalScale, heightAlignment, baseAlignment) => {
return {
"u_matrix": matrix,
"u_edge_radius": edgeRadius,
"u_width_scale": lineWidthScale,
"u_vertical_scale": verticalScale,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"u_height_type": fillExtrusionAlignmentType[heightAlignment],
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
"u_base_type": fillExtrusionAlignmentType[baseAlignment]
};
};
const fillExtrusionPatternUniformValues = (matrix, painter, shouldUseVerticalGradient, opacity, aoIntensityRadius, edgeRadius, lineWidthScale, coord, tile, heightLift, heightAlignment, baseAlignment, zoomTransition, mercatorCenter, invMatrix, floodLightColor, verticalScale, patternTransition) => {
const uniformValues = fillExtrusionUniformValues(
matrix,
painter,
shouldUseVerticalGradient,
opacity,
aoIntensityRadius,
edgeRadius,
lineWidthScale,
coord,
heightLift,
heightAlignment,
baseAlignment,
zoomTransition,
mercatorCenter,
invMatrix,
floodLightColor,
verticalScale,
1,
[0, 0, 0]
);
const heightFactorUniform = {
"u_height_factor": -Math.pow(2, coord.overscaledZ) / tile.tileSize / 8
};
return Object.assign(uniformValues, patternUniformValues(painter, tile, patternTransition), heightFactorUniform);
};
const fillExtrusionGroundEffectUniformValues = (painter, matrix, opacity, aoPass, meterToTile, ao, floodLightIntensity, floodLightColor, attenuation, edgeRadius, fbSize) => {
const uniformValues = {
"u_matrix": matrix,
"u_opacity": opacity,
"u_ao_pass": aoPass ? 1 : 0,
"u_meter_to_tile": meterToTile,
"u_ao": ao,
"u_flood_light_intensity": floodLightIntensity,
"u_flood_light_color": floodLightColor,
"u_attenuation": attenuation,
"u_edge_radius": edgeRadius,
"u_fb": 0,
"u_fb_size": fbSize,
"u_dynamic_offset": 1
};
return uniformValues;
};
const fillUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context)
});
const fillPatternUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_image": new index$1.Uniform1i(context),
"u_texsize": new index$1.Uniform2f(context),
"u_pixel_coord_upper": new index$1.Uniform2f(context),
"u_pixel_coord_lower": new index$1.Uniform2f(context),
"u_tile_units_to_pixels": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context),
"u_pattern_transition": new index$1.Uniform1f(context)
});
const fillOutlineUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_world": new index$1.Uniform2f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context)
});
const fillOutlinePatternUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_world": new index$1.Uniform2f(context),
"u_image": new index$1.Uniform1i(context),
"u_texsize": new index$1.Uniform2f(context),
"u_pixel_coord_upper": new index$1.Uniform2f(context),
"u_pixel_coord_lower": new index$1.Uniform2f(context),
"u_tile_units_to_pixels": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context),
"u_pattern_transition": new index$1.Uniform1f(context)
});
const elevatedStructuresDepthUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_depth_bias": new index$1.Uniform1f(context)
});
const elevatedStructuresUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context)
});
const elevatedStructuresDepthReconstructUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_camera_pos": new index$1.Uniform3f(context),
"u_depth_bias": new index$1.Uniform1f(context),
"u_height_scale": new index$1.Uniform1f(context),
"u_reset_depth": new index$1.Uniform1f(context)
});
const fillUniformValues = (matrix, emissiveStrength, groundShadowFactor) => ({
"u_matrix": matrix,
"u_emissive_strength": emissiveStrength,
"u_ground_shadow_factor": groundShadowFactor
});
const fillPatternUniformValues = (matrix, emissiveStrength, painter, tile, groundShadowFactor, patternTransition = 0) => Object.assign(
fillUniformValues(matrix, emissiveStrength, groundShadowFactor),
patternUniformValues(painter, tile, patternTransition)
);
const fillOutlineUniformValues = (matrix, emissiveStrength, drawingBufferSize, groundShadowFactor) => ({
"u_matrix": matrix,
"u_world": drawingBufferSize,
"u_emissive_strength": emissiveStrength,
"u_ground_shadow_factor": groundShadowFactor
});
const fillOutlinePatternUniformValues = (matrix, emissiveStrength, painter, tile, drawingBufferSize, groundShadowFactor, patternTransision = 0) => Object.assign(
fillPatternUniformValues(matrix, emissiveStrength, painter, tile, groundShadowFactor, patternTransision),
{
"u_world": drawingBufferSize
}
);
const elevatedStructuresDepthUniformValues = (matrix, depthBias) => ({
"u_matrix": matrix,
"u_depth_bias": depthBias
});
const elevatedStructuresUniformValues = (matrix, groundShadowFactor) => ({
"u_matrix": matrix,
"u_ground_shadow_factor": groundShadowFactor
});
const elevatedStructuresDepthReconstructUniformValues = (tileMatrix, cameraTilePos, depthBias, heightScale, resetDepth) => ({
"u_matrix": tileMatrix,
"u_camera_pos": [cameraTilePos[0], cameraTilePos[1], cameraTilePos[2]],
"u_depth_bias": depthBias,
"u_height_scale": heightScale,
"u_reset_depth": resetDepth
});
const buildingUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_normal_matrix": new index$1.UniformMatrix4f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_faux_facade_ao_intensity": new index$1.Uniform1f(context),
"u_camera_pos": new index$1.Uniform3f(context),
"u_tile_to_meter": new index$1.Uniform1f(context),
"u_facade_emissive_chance": new index$1.Uniform1f(context),
"u_flood_light_color": new index$1.Uniform3f(context),
"u_flood_light_intensity": new index$1.Uniform1f(context)
});
const buildingUniformValues = (matrix, normalMatrix, opacity, aoIntensity, cameraPos, tileToMeter, emissiveChance, floodLightColor, floodLightIntensity) => {
const uniformValues = {
"u_matrix": matrix,
"u_normal_matrix": normalMatrix,
"u_opacity": opacity,
"u_faux_facade_ao_intensity": aoIntensity,
"u_camera_pos": cameraPos,
"u_tile_to_meter": tileToMeter,
"u_facade_emissive_chance": emissiveChance,
"u_flood_light_color": floodLightColor,
"u_flood_light_intensity": floodLightIntensity
};
return uniformValues;
};
const buildingBloomUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context)
});
const buildingBloomUniformValues = (matrix) => {
const uniformValues = {
"u_matrix": matrix
};
return uniformValues;
};
const buildingDepthUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context)
});
const buildingDepthUniformValues = (matrix) => {
const uniformValues = {
"u_matrix": matrix
};
return uniformValues;
};
const collisionUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_inv_rot_matrix": new index$1.UniformMatrix4f(context),
"u_camera_to_center_distance": new index$1.Uniform1f(context),
"u_extrude_scale": new index$1.Uniform2f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_tile_id": new index$1.Uniform3f(context)
});
const collisionCircleUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_inv_matrix": new index$1.UniformMatrix4f(context),
"u_camera_to_center_distance": new index$1.Uniform1f(context),
"u_viewport_size": new index$1.Uniform2f(context)
});
const collisionUniformValues = (matrix, invMatrix, transform, globeToMercator, mercatorCenter, tile, tileId, projection) => {
const pixelRatio = index$1.EXTENT / tile.tileSize;
return {
"u_matrix": matrix,
"u_inv_rot_matrix": invMatrix,
"u_camera_to_center_distance": transform.getCameraToCenterDistance(projection),
"u_extrude_scale": [
transform.pixelsToGLUnits[0] / pixelRatio,
transform.pixelsToGLUnits[1] / pixelRatio
],
"u_zoom_transition": globeToMercator,
"u_tile_id": tileId,
"u_merc_center": mercatorCenter
};
};
const collisionCircleUniformValues = (matrix, invMatrix, transform, projection) => {
return {
"u_matrix": matrix,
"u_inv_matrix": invMatrix,
"u_camera_to_center_distance": transform.getCameraToCenterDistance(projection),
"u_viewport_size": [transform.width, transform.height]
};
};
const debugUniforms = (context) => ({
"u_color": new index$1.UniformColor(context),
"u_matrix": new index$1.UniformMatrix4f(context),
"u_overlay": new index$1.Uniform1i(context),
"u_overlay_scale": new index$1.Uniform1f(context)
});
const debugUniformValues = (matrix, color, scaleRatio = 1) => ({
"u_matrix": matrix,
"u_color": color,
"u_overlay": 0,
"u_overlay_scale": scaleRatio
});
const heatmapUniforms = (context) => ({
"u_extrude_scale": new index$1.Uniform1f(context),
"u_intensity": new index$1.Uniform1f(context),
"u_matrix": new index$1.UniformMatrix4f(context),
"u_inv_rot_matrix": new index$1.UniformMatrix4f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_tile_id": new index$1.Uniform3f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_up_dir": new index$1.Uniform3f(context)
});
const heatmapTextureUniforms = (context) => ({
"u_image": new index$1.Uniform1i(context),
"u_color_ramp": new index$1.Uniform1i(context),
"u_opacity": new index$1.Uniform1f(context)
});
const identityMatrix$1 = index$1.create();
const heatmapUniformValues = (painter, coord, tile, invMatrix, mercatorCenter, zoom, intensity) => {
const transform = painter.transform;
const isGlobe = transform.projection.name === "globe";
const extrudeScale = isGlobe ? index$1.globePixelsToTileUnits(transform.zoom, coord.canonical) * transform._pixelsPerMercatorPixel : index$1.pixelsToTileUnits(tile, 1, zoom);
const values = {
"u_matrix": coord.projMatrix,
"u_extrude_scale": extrudeScale,
"u_intensity": intensity,
"u_inv_rot_matrix": identityMatrix$1,
"u_merc_center": [0, 0],
"u_tile_id": [0, 0, 0],
"u_zoom_transition": 0,
"u_up_dir": [0, 0, 0]
};
if (isGlobe) {
values["u_inv_rot_matrix"] = invMatrix;
values["u_merc_center"] = mercatorCenter;
values["u_tile_id"] = [coord.canonical.x, coord.canonical.y, 1 << coord.canonical.z];
values["u_zoom_transition"] = index$1.globeToMercatorTransition(transform.zoom);
const x = mercatorCenter[0] * index$1.EXTENT;
const y = mercatorCenter[1] * index$1.EXTENT;
values["u_up_dir"] = transform.projection.upVector(new index$1.CanonicalTileID(0, 0, 0), x, y);
}
return values;
};
const heatmapTextureUniformValues = (painter, layer, textureUnit, colorRampUnit) => {
return {
"u_image": textureUnit,
"u_color_ramp": colorRampUnit,
"u_opacity": layer.paint.get("heatmap-opacity")
};
};
function computeRasterColorMix(colorRampRes, [mixR, mixG, mixB, mixA], [min, max]) {
if (min === max) return [0, 0, 0, 0];
const factor = 255 * (colorRampRes - 1) / (colorRampRes * (max - min));
return [
mixR * factor,
mixG * factor,
mixB * factor,
mixA * factor
];
}
function computeRasterColorOffset(colorRampRes, offset, [min, max]) {
if (min === max) return 0;
return 0.5 / colorRampRes + (offset - min) * (colorRampRes - 1) / (colorRampRes * (max - min));
}
const rasterUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_normalize_matrix": new index$1.UniformMatrix4f(context),
"u_globe_matrix": new index$1.UniformMatrix4f(context),
"u_merc_matrix": new index$1.UniformMatrix4f(context),
"u_grid_matrix": new index$1.UniformMatrix3f(context),
"u_tl_parent": new index$1.Uniform2f(context),
"u_scale_parent": new index$1.Uniform1f(context),
"u_fade_t": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_image0": new index$1.Uniform1i(context),
"u_image1": new index$1.Uniform1i(context),
"u_brightness_low": new index$1.Uniform1f(context),
"u_brightness_high": new index$1.Uniform1f(context),
"u_saturation_factor": new index$1.Uniform1f(context),
"u_contrast_factor": new index$1.Uniform1f(context),
"u_spin_weights": new index$1.Uniform3f(context),
"u_perspective_transform": new index$1.Uniform2f(context),
"u_raster_elevation": new index$1.Uniform1f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_cutoff_params": new index$1.Uniform4f(context),
"u_colorization_mix": new index$1.Uniform4f(context),
"u_colorization_offset": new index$1.Uniform1f(context),
"u_color_ramp": new index$1.Uniform1i(context),
"u_texture_offset": new index$1.Uniform2f(context),
"u_texture_res": new index$1.Uniform2f(context),
"u_emissive_strength": new index$1.Uniform1f(context)
});
const rasterUniformValues = (matrix, normalizeMatrix, globeMatrix, mercMatrix, gridMatrix, parentTL, zoomTransition, mercatorCenter, cutoffParams, parentScaleBy, fade, layer, perspectiveTransform, elevation, colorRampUnit, colorMix, colorOffset, colorRange, tileSize, buffer, emissiveStrength) => ({
"u_matrix": matrix,
"u_normalize_matrix": normalizeMatrix,
"u_globe_matrix": globeMatrix,
"u_merc_matrix": mercMatrix,
"u_grid_matrix": gridMatrix,
"u_tl_parent": parentTL,
"u_scale_parent": parentScaleBy,
"u_fade_t": fade.mix,
"u_opacity": fade.opacity * layer.paint.get("raster-opacity"),
"u_image0": 0,
"u_image1": 1,
"u_brightness_low": layer.paint.get("raster-brightness-min"),
"u_brightness_high": layer.paint.get("raster-brightness-max"),
"u_saturation_factor": index$1.saturationFactor(layer.paint.get("raster-saturation")),
"u_contrast_factor": index$1.contrastFactor(layer.paint.get("raster-contrast")),
"u_spin_weights": spinWeights(layer.paint.get("raster-hue-rotate")),
"u_perspective_transform": perspectiveTransform,
"u_raster_elevation": elevation,
"u_zoom_transition": zoomTransition,
"u_merc_center": mercatorCenter,
"u_cutoff_params": cutoffParams,
"u_colorization_mix": computeRasterColorMix(index$1.COLOR_RAMP_RES, colorMix, colorRange),
"u_colorization_offset": computeRasterColorOffset(index$1.COLOR_RAMP_RES, colorOffset, colorRange),
"u_color_ramp": colorRampUnit,
"u_texture_offset": [
buffer / (tileSize + 2 * buffer),
tileSize / (tileSize + 2 * buffer)
],
"u_texture_res": [tileSize + 2 * buffer, tileSize + 2 * buffer],
"u_emissive_strength": emissiveStrength
});
const rasterPoleUniformValues = (matrix, normalizeMatrix, globeMatrix, zoomTransition, fade, layer, perspectiveTransform, elevation, colorRampUnit, colorMix, colorOffset, colorRange, emissiveStrength) => rasterUniformValues(
matrix,
normalizeMatrix,
globeMatrix,
new Float32Array(16),
new Float32Array(9),
[0, 0],
zoomTransition,
[0, 0],
[0, 0, 0, 0],
1,
fade,
layer,
perspectiveTransform || [0, 0],
elevation,
colorRampUnit,
colorMix,
colorOffset,
colorRange,
1,
0,
emissiveStrength
);
function spinWeights(angle) {
angle *= Math.PI / 180;
const s = Math.sin(angle);
const c = Math.cos(angle);
return [
(2 * c + 1) / 3,
(-Math.sqrt(3) * s - c + 1) / 3,
(Math.sqrt(3) * s - c + 1) / 3
];
}
const RASTER_PARTICLE_POS_OFFSET = 0.05;
const RASTER_PARTICLE_POS_SCALE = 1 + 2 * RASTER_PARTICLE_POS_OFFSET;
const rasterParticleUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_normalize_matrix": new index$1.UniformMatrix4f(context),
"u_globe_matrix": new index$1.UniformMatrix4f(context),
"u_merc_matrix": new index$1.UniformMatrix4f(context),
"u_grid_matrix": new index$1.UniformMatrix3f(context),
"u_tl_parent": new index$1.Uniform2f(context),
"u_scale_parent": new index$1.Uniform1f(context),
"u_fade_t": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_image0": new index$1.Uniform1i(context),
"u_image1": new index$1.Uniform1i(context),
"u_raster_elevation": new index$1.Uniform1f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_cutoff_params": new index$1.Uniform4f(context)
});
const rasterParticleUniformValues = (matrix, normalizeMatrix, globeMatrix, mercMatrix, gridMatrix, parentTL, zoomTransition, mercatorCenter, cutoffParams, parentScaleBy, fade, elevation) => ({
"u_matrix": matrix,
"u_normalize_matrix": normalizeMatrix,
"u_globe_matrix": globeMatrix,
"u_merc_matrix": mercMatrix,
"u_grid_matrix": gridMatrix,
"u_tl_parent": parentTL,
"u_scale_parent": parentScaleBy,
"u_fade_t": fade.mix,
"u_opacity": fade.opacity,
"u_image0": 0,
"u_image1": 1,
"u_raster_elevation": elevation,
"u_zoom_transition": zoomTransition,
"u_merc_center": mercatorCenter,
"u_cutoff_params": cutoffParams
});
const rasterParticleTextureUniforms = (context) => ({
"u_texture": new index$1.Uniform1i(context),
"u_opacity": new index$1.Uniform1f(context)
});
const rasterParticleTextureUniformValues = (textureUnit, opacity) => ({
"u_texture": textureUnit,
"u_opacity": opacity
});
const rasterParticleDrawUniforms = (context) => ({
"u_particle_texture": new index$1.Uniform1i(context),
"u_particle_texture_side_len": new index$1.Uniform1f(context),
"u_tile_offset": new index$1.Uniform2f(context),
"u_velocity": new index$1.Uniform1i(context),
"u_color_ramp": new index$1.Uniform1i(context),
"u_velocity_res": new index$1.Uniform2f(context),
"u_max_speed": new index$1.Uniform1f(context),
"u_uv_offset": new index$1.Uniform2f(context),
"u_data_scale": new index$1.Uniform2f(context),
"u_data_offset": new index$1.Uniform1f(context),
"u_particle_pos_scale": new index$1.Uniform1f(context),
"u_particle_pos_offset": new index$1.Uniform2f(context)
});
const rasterParticleDrawUniformValues = (particleTextureUnit, particleTextureSideLen, tileOffset, velocityTextureUnit, velocityTextureSize, colorRampUnit, maxSpeed, textureOffset, dataScale, dataOffset) => ({
"u_particle_texture": particleTextureUnit,
"u_particle_texture_side_len": particleTextureSideLen,
"u_tile_offset": tileOffset,
"u_velocity": velocityTextureUnit,
"u_color_ramp": colorRampUnit,
"u_velocity_res": velocityTextureSize,
"u_max_speed": maxSpeed,
"u_uv_offset": textureOffset,
"u_data_scale": [
255 * dataScale[0],
255 * dataScale[1]
],
"u_data_offset": dataOffset,
"u_particle_pos_scale": RASTER_PARTICLE_POS_SCALE,
"u_particle_pos_offset": [RASTER_PARTICLE_POS_OFFSET, RASTER_PARTICLE_POS_OFFSET]
});
const rasterParticleUpdateUniforms = (context) => ({
"u_particle_texture": new index$1.Uniform1i(context),
"u_particle_texture_side_len": new index$1.Uniform1f(context),
"u_velocity": new index$1.Uniform1i(context),
"u_velocity_res": new index$1.Uniform2f(context),
"u_max_speed": new index$1.Uniform1f(context),
"u_speed_factor": new index$1.Uniform1f(context),
"u_reset_rate": new index$1.Uniform1f(context),
"u_rand_seed": new index$1.Uniform1f(context),
"u_uv_offset": new index$1.Uniform2f(context),
"u_data_scale": new index$1.Uniform2f(context),
"u_data_offset": new index$1.Uniform1f(context),
"u_particle_pos_scale": new index$1.Uniform1f(context),
"u_particle_pos_offset": new index$1.Uniform2f(context)
});
const rasterParticleUpdateUniformValues = (particleTextureUnit, particleTextureSideLen, velocityTextureUnit, velocityTextureSize, maxSpeed, speedFactor, resetRate, textureOffset, dataScale, dataOffset) => ({
"u_particle_texture": particleTextureUnit,
"u_particle_texture_side_len": particleTextureSideLen,
"u_velocity": velocityTextureUnit,
"u_velocity_res": velocityTextureSize,
"u_max_speed": maxSpeed,
"u_speed_factor": speedFactor,
"u_reset_rate": resetRate,
"u_rand_seed": Math.random(),
"u_uv_offset": textureOffset,
"u_data_scale": [
255 * dataScale[0],
255 * dataScale[1]
],
"u_data_offset": dataOffset,
"u_particle_pos_scale": RASTER_PARTICLE_POS_SCALE,
"u_particle_pos_offset": [RASTER_PARTICLE_POS_OFFSET, RASTER_PARTICLE_POS_OFFSET]
});
const symbolUniforms = (context) => ({
"u_is_size_zoom_constant": new index$1.Uniform1i(context),
"u_is_size_feature_constant": new index$1.Uniform1i(context),
"u_size_t": new index$1.Uniform1f(context),
"u_size": new index$1.Uniform1f(context),
"u_camera_to_center_distance": new index$1.Uniform1f(context),
"u_rotate_symbol": new index$1.Uniform1i(context),
"u_aspect_ratio": new index$1.Uniform1f(context),
"u_fade_change": new index$1.Uniform1f(context),
"u_matrix": new index$1.UniformMatrix4f(context),
"u_label_plane_matrix": new index$1.UniformMatrix4f(context),
"u_coord_matrix": new index$1.UniformMatrix4f(context),
"u_is_text": new index$1.Uniform1i(context),
"u_elevation_from_sea": new index$1.Uniform1i(context),
"u_pitch_with_map": new index$1.Uniform1i(context),
"u_texsize": new index$1.Uniform2f(context),
"u_texsize_icon": new index$1.Uniform2f(context),
"u_texture": new index$1.Uniform1i(context),
"u_texture_icon": new index$1.Uniform1i(context),
"u_gamma_scale": new index$1.Uniform1f(context),
"u_device_pixel_ratio": new index$1.Uniform1f(context),
"u_tile_id": new index$1.Uniform3f(context),
"u_zoom_transition": new index$1.Uniform1f(context),
"u_inv_rot_matrix": new index$1.UniformMatrix4f(context),
"u_merc_center": new index$1.Uniform2f(context),
"u_camera_forward": new index$1.Uniform3f(context),
"u_tile_matrix": new index$1.UniformMatrix4f(context),
"u_up_vector": new index$1.Uniform3f(context),
"u_ecef_origin": new index$1.Uniform3f(context),
"u_is_halo": new index$1.Uniform1i(context),
"u_icon_transition": new index$1.Uniform1f(context),
"u_color_adj_mat": new index$1.UniformMatrix4f(context),
"u_scale_factor": new index$1.Uniform1f(context),
"u_ground_shadow_factor": new index$1.Uniform3f(context),
"u_inv_matrix": new index$1.UniformMatrix4f(context),
"u_normal_scale": new index$1.Uniform1f(context),
"u_lutTexture": new index$1.Uniform1i(context)
});
const identityMatrix = index$1.create();
const symbolUniformValues = (functionType, size, rotateInShader, pitchWithMap, painter, matrix, labelPlaneMatrix, glCoordMatrix, elevationFromSea, isText, texSize, texSizeIcon, isHalo, coord, zoomTransition, mercatorCenter, invMatrix, upVector, projection, groundShadowFactor, normalScale, colorAdjustmentMatrix, transition, scaleFactor) => {
const transform = painter.transform;
const values = {
"u_is_size_zoom_constant": +(functionType === "constant" || functionType === "source"),
"u_is_size_feature_constant": +(functionType === "constant" || functionType === "camera"),
"u_size_t": size ? size.uSizeT : 0,
"u_size": size ? size.uSize : 0,
"u_camera_to_center_distance": transform.getCameraToCenterDistance(projection),
"u_rotate_symbol": +rotateInShader,
"u_aspect_ratio": transform.width / transform.height,
"u_fade_change": painter.options.fadeDuration ? painter.symbolFadeChange : 1,
"u_matrix": matrix,
"u_label_plane_matrix": labelPlaneMatrix,
"u_coord_matrix": glCoordMatrix,
"u_is_text": +isText,
"u_elevation_from_sea": elevationFromSea ? 1 : 0,
"u_pitch_with_map": +pitchWithMap,
"u_texsize": texSize,
"u_texsize_icon": texSizeIcon,
"u_texture": 0,
"u_texture_icon": 1,
"u_tile_id": [0, 0, 0],
"u_zoom_transition": 0,
"u_inv_rot_matrix": identityMatrix,
"u_merc_center": [0, 0],
"u_camera_forward": [0, 0, 0],
"u_ecef_origin": [0, 0, 0],
"u_tile_matrix": identityMatrix,
"u_up_vector": [0, -1, 0],
"u_color_adj_mat": colorAdjustmentMatrix,
"u_icon_transition": transition ? transition : 0,
"u_gamma_scale": pitchWithMap ? painter.transform.getCameraToCenterDistance(projection) * Math.cos(painter.terrain ? 0 : painter.transform._pitch) : 1,
"u_device_pixel_ratio": index$1.exported$1.devicePixelRatio,
"u_is_halo": +isHalo,
"u_scale_factor": scaleFactor ? scaleFactor : 1,
"u_ground_shadow_factor": groundShadowFactor,
"u_inv_matrix": index$1.invert(index$1.create(), labelPlaneMatrix),
"u_normal_scale": normalScale,
"u_lutTexture": TextureSlots.LUT
};
if (projection.name === "globe") {
values["u_tile_id"] = [coord.canonical.x, coord.canonical.y, 1 << coord.canonical.z];
values["u_zoom_transition"] = zoomTransition;
values["u_inv_rot_matrix"] = invMatrix;
values["u_merc_center"] = mercatorCenter;
values["u_camera_forward"] = transform._camera.forward();
values["u_ecef_origin"] = index$1.globeECEFOrigin(transform.globeMatrix, coord.toUnwrapped());
values["u_tile_matrix"] = Float32Array.from(transform.globeMatrix);
values["u_up_vector"] = upVector;
}
return values;
};
const backgroundUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_color": new index$1.UniformColor(context)
});
const backgroundPatternUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_image": new index$1.Uniform1i(context),
"u_pattern_tl": new index$1.Uniform2f(context),
"u_pattern_br": new index$1.Uniform2f(context),
"u_texsize": new index$1.Uniform2f(context),
"u_pattern_size": new index$1.Uniform2f(context),
"u_pixel_coord_upper": new index$1.Uniform2f(context),
"u_pixel_coord_lower": new index$1.Uniform2f(context),
"u_pattern_units_to_pixels": new index$1.Uniform2f(context)
});
const backgroundUniformValues = (matrix, emissiveStrength, opacity, color) => ({
"u_matrix": matrix,
"u_emissive_strength": emissiveStrength,
"u_opacity": opacity,
"u_color": color
});
const backgroundPatternUniformValues = (matrix, emissiveStrength, opacity, painter, image, scope, patternPosition, isViewport, tile) => Object.assign(
bgPatternUniformValues(image, scope, patternPosition, painter, isViewport, tile),
{
"u_matrix": matrix,
"u_emissive_strength": emissiveStrength,
"u_opacity": opacity
}
);
const skyboxUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_sun_direction": new index$1.Uniform3f(context),
"u_cubemap": new index$1.Uniform1i(context),
"u_opacity": new index$1.Uniform1f(context),
"u_temporal_offset": new index$1.Uniform1f(context)
});
const skyboxUniformValues = (matrix, sunDirection, cubemap, opacity, temporalOffset) => ({
"u_matrix": matrix,
"u_sun_direction": sunDirection,
"u_cubemap": cubemap,
"u_opacity": opacity,
"u_temporal_offset": temporalOffset
});
const skyboxGradientUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_color_ramp": new index$1.Uniform1i(context),
// radial gradient uniforms
"u_center_direction": new index$1.Uniform3f(context),
"u_radius": new index$1.Uniform1f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_temporal_offset": new index$1.Uniform1f(context)
});
const skyboxGradientUniformValues = (matrix, centerDirection, radius, opacity, temporalOffset) => {
return {
"u_matrix": matrix,
"u_color_ramp": 0,
"u_center_direction": centerDirection,
"u_radius": index$1.degToRad(radius),
"u_opacity": opacity,
"u_temporal_offset": temporalOffset
};
};
const skyboxCaptureUniforms = (context) => ({
"u_matrix_3f": new index$1.UniformMatrix3f(context),
"u_sun_direction": new index$1.Uniform3f(context),
"u_sun_intensity": new index$1.Uniform1f(context),
"u_color_tint_r": new index$1.Uniform4f(context),
"u_color_tint_m": new index$1.Uniform4f(context),
"u_luminance": new index$1.Uniform1f(context)
});
const skyboxCaptureUniformValues = (matrix, sunDirection, sunIntensity, atmosphereColor, atmosphereHaloColor) => ({
"u_matrix_3f": matrix,
"u_sun_direction": sunDirection,
"u_sun_intensity": sunIntensity,
"u_color_tint_r": [
atmosphereColor.r,
atmosphereColor.g,
atmosphereColor.b,
atmosphereColor.a
],
"u_color_tint_m": [
atmosphereHaloColor.r,
atmosphereHaloColor.g,
atmosphereHaloColor.b,
atmosphereHaloColor.a
],
"u_luminance": 5e-5
});
const modelUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_lighting_matrix": new index$1.UniformMatrix4f(context),
"u_normal_matrix": new index$1.UniformMatrix4f(context),
"u_node_matrix": new index$1.UniformMatrix4f(context),
"u_lightpos": new index$1.Uniform3f(context),
"u_lightintensity": new index$1.Uniform1f(context),
"u_lightcolor": new index$1.Uniform3f(context),
"u_camera_pos": new index$1.Uniform3f(context),
"u_opacity": new index$1.Uniform1f(context),
"u_baseColorFactor": new index$1.Uniform4f(context),
"u_emissiveFactor": new index$1.Uniform4f(context),
"u_metallicFactor": new index$1.Uniform1f(context),
"u_roughnessFactor": new index$1.Uniform1f(context),
"u_baseTextureIsAlpha": new index$1.Uniform1i(context),
"u_alphaMask": new index$1.Uniform1i(context),
"u_alphaCutoff": new index$1.Uniform1f(context),
"u_baseColorTexture": new index$1.Uniform1i(context),
"u_metallicRoughnessTexture": new index$1.Uniform1i(context),
"u_normalTexture": new index$1.Uniform1i(context),
"u_occlusionTexture": new index$1.Uniform1i(context),
"u_emissionTexture": new index$1.Uniform1i(context),
"u_lutTexture": new index$1.Uniform1i(context),
"u_color_mix": new index$1.Uniform4f(context),
"u_aoIntensity": new index$1.Uniform1f(context),
"u_emissive_strength": new index$1.Uniform1f(context),
"u_occlusionTextureTransform": new index$1.Uniform4f(context)
});
const emptyMat4 = new Float32Array(index$1.identity([]));
const modelUniformValues = (matrix, lightingMatrix, normalMatrix, nodeMatrix, painter, opacity, baseColorFactor, emissiveFactor, metallicFactor, roughnessFactor, material, emissiveStrength, layer, cameraPos = [0, 0, 0], occlusionTextureTransform, materialOverride, modelColorMix) => {
const light = painter.style.light;
const _lp = light.properties.get("position");
const lightPos = [-_lp.x, -_lp.y, _lp.z];
const lightMat = index$1.create$3();
const anchor = light.properties.get("anchor");
if (anchor === "viewport") {
index$1.fromRotation(lightMat, -painter.transform.angle);
index$1.transformMat3(lightPos, lightPos, lightMat);
}
const alphaMask = material.alphaMode === "MASK";
const lightColor = light.properties.get("color").toNonPremultipliedRenderColor(null);
const aoIntensity = layer.paint.get("model-ambient-occlusion-intensity");
const colorMix = layer.paint.get("model-color").constantOr(index$1.Color.white).toNonPremultipliedRenderColor(null);
colorMix.a = layer.paint.get("model-color-mix-intensity").constantOr(0);
if (modelColorMix) {
colorMix.r = modelColorMix[0];
colorMix.g = modelColorMix[1];
colorMix.b = modelColorMix[2];
colorMix.a = modelColorMix[3];
}
if (materialOverride) {
colorMix.r = materialOverride.color.r;
colorMix.g = materialOverride.color.g;
colorMix.b = materialOverride.color.b;
colorMix.a = materialOverride.colorMix;
emissiveStrength = materialOverride.emissionStrength;
opacity = materialOverride.opacity * opacity;
}
const uniformValues = {
"u_matrix": matrix,
"u_lighting_matrix": lightingMatrix,
"u_normal_matrix": normalMatrix,
"u_node_matrix": nodeMatrix ? nodeMatrix : emptyMat4,
"u_lightpos": lightPos,
"u_lightintensity": light.properties.get("intensity"),
"u_lightcolor": [lightColor.r, lightColor.g, lightColor.b],
"u_camera_pos": cameraPos,
"u_opacity": opacity,
"u_baseTextureIsAlpha": 0,
"u_alphaMask": +alphaMask,
"u_alphaCutoff": material.alphaCutoff,
"u_baseColorFactor": baseColorFactor.toNonPremultipliedRenderColor(null).toArray01(),
"u_emissiveFactor": emissiveFactor.toNonPremultipliedRenderColor(null).toArray01(),
"u_metallicFactor": metallicFactor,
"u_roughnessFactor": roughnessFactor,
"u_baseColorTexture": TextureSlots.BaseColor,
"u_metallicRoughnessTexture": TextureSlots.MetallicRoughness,
"u_normalTexture": TextureSlots.Normal,
"u_occlusionTexture": TextureSlots.Occlusion,
"u_emissionTexture": TextureSlots.Emission,
"u_lutTexture": TextureSlots.LUT,
"u_color_mix": colorMix.toArray01(),
"u_aoIntensity": aoIntensity,
"u_emissive_strength": emissiveStrength,
"u_occlusionTextureTransform": occlusionTextureTransform ? occlusionTextureTransform : [0, 0, 0, 0]
};
return uniformValues;
};
const modelDepthUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_instance": new index$1.UniformMatrix4f(context),
"u_node_matrix": new index$1.UniformMatrix4f(context)
});
const modelDepthUniformValues = (matrix, instance = emptyMat4, nodeMatrix = emptyMat4) => {
return {
"u_matrix": matrix,
"u_instance": instance,
"u_node_matrix": nodeMatrix
};
};
const starsUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_up": new index$1.Uniform3f(context),
"u_right": new index$1.Uniform3f(context),
"u_intensity_multiplier": new index$1.Uniform1f(context)
});
const starsUniformValues = (matrix, up, right, intensityMultiplier) => ({
"u_matrix": Float32Array.from(matrix),
"u_up": up,
"u_right": right,
"u_intensity_multiplier": intensityMultiplier
});
const occlusionUniforms = (context) => ({
"u_matrix": new index$1.UniformMatrix4f(context),
"u_anchorPos": new index$1.Uniform3f(context),
"u_screenSizePx": new index$1.Uniform2f(context),
"u_occluderSizePx": new index$1.Uniform2f(context),
"u_color": new index$1.Uniform4f(context)
});
const occlusionUniformValues = (matrix, anchorPos, screenSize, occluderSize, color) => ({
"u_matrix": Float32Array.from(matrix),
"u_anchorPos": anchorPos,
"u_screenSizePx": screenSize,
"u_occluderSizePx": occluderSize,
"u_color": color
});
const snowUniforms = (context) => ({
"u_modelview": new index$1.UniformMatrix4f(context),
"u_projection": new index$1.UniformMatrix4f(context),
"u_time": new index$1.Uniform1f(context),
"u_cam_pos": new index$1.Uniform3f(context),
"u_velocityConeAperture": new index$1.Uniform1f(context),
"u_velocity": new index$1.Uniform1f(context),
"u_horizontalOscillationRadius": new index$1.Uniform1f(context),
"u_horizontalOscillationRate": new index$1.Uniform1f(context),
"u_boxSize": new index$1.Uniform1f(context),
"u_billboardSize": new index$1.Uniform1f(context),
"u_simpleShapeParameters": new index$1.Uniform2f(context),
"u_screenSize": new index$1.Uniform2f(context),
"u_thinningCenterPos": new index$1.Uniform2f(context),
"u_thinningShape": new index$1.Uniform3f(context),
"u_thinningAffectedRatio": new index$1.Uniform1f(context),
"u_thinningParticleOffset": new index$1.Uniform1f(context),
"u_particleColor": new index$1.Uniform4f(context),
"u_direction": new index$1.Uniform3f(context)
});
const snowUniformValues = (values) => ({
"u_modelview": Float32Array.from(values.modelview),
"u_projection": Float32Array.from(values.projection),
"u_time": values.time,
"u_cam_pos": values.camPos,
"u_velocityConeAperture": values.velocityConeAperture,
"u_velocity": values.velocity,
"u_horizontalOscillationRadius": values.horizontalOscillationRadius,
"u_horizontalOscillationRate": values.horizontalOscillationRate,
"u_boxSize": values.boxSize,
"u_billboardSize": values.billboardSize,
"u_simpleShapeParameters": values.simpleShapeParameters,
"u_screenSize": values.screenSize,
"u_thinningCenterPos": values.thinningCenterPos,
"u_thinningShape": values.thinningShape,
"u_thinningAffectedRatio": values.thinningAffectedRatio,
"u_thinningParticleOffset": values.thinningParticleOffset,
"u_particleColor": values.color,
"u_direction": values.direction
});
const rainUniforms = (context) => ({
"u_modelview": new index$1.UniformMatrix4f(context),
"u_projection": new index$1.UniformMatrix4f(context),
"u_time": new index$1.Uniform1f(context),
"u_cam_pos": new index$1.Uniform3f(context),
"u_texScreen": new index$1.Uniform1i(context),
"u_velocityConeAperture": new index$1.Uniform1f(context),
"u_velocity": new index$1.Uniform1f(context),
"u_boxSize": new index$1.Uniform1f(context),
"u_rainDropletSize": new index$1.Uniform2f(context),
"u_distortionStrength": new index$1.Uniform1f(context),
"u_rainDirection": new index$1.Uniform3f(context),
"u_color": new index$1.Uniform4f(context),
"u_screenSize": new index$1.Uniform2f(context),
"u_thinningCenterPos": new index$1.Uniform2f(context),
"u_thinningShape": new index$1.Uniform3f(context),
"u_thinningAffectedRatio": new index$1.Uniform1f(context),
"u_thinningParticleOffset": new index$1.Uniform1f(context),
"u_shapeDirectionalPower": new index$1.Uniform1f(context),
"u_shapeNormalPower": new index$1.Uniform1f(context),
"u_mode": new index$1.Uniform1f(context)
});
const rainUniformValues = (values) => ({
"u_modelview": Float32Array.from(values.modelview),
"u_projection": Float32Array.from(values.projection),
"u_time": values.time,
"u_cam_pos": values.camPos,
"u_texScreen": 0,
"u_velocityConeAperture": values.velocityConeAperture,
"u_velocity": values.velocity,
"u_boxSize": values.boxSize,
"u_rainDropletSize": values.rainDropletSize,
"u_distortionStrength": values.distortionStrength,
"u_rainDirection": values.rainDirection,
"u_color": values.color,
"u_screenSize": values.screenSize,
"u_thinningCenterPos": values.thinningCenterPos,
"u_thinningShape": values.thinningShape,
"u_thinningAffectedRatio": values.thinningAffectedRatio,
"u_thinningParticleOffset": values.thinningParticleOffset,
"u_shapeDirectionalPower": values.shapeDirectionalPower,
"u_shapeNormalPower": values.shapeNormalPower,
"u_mode": values.mode
});
const vignetteUniforms = (context) => ({
"u_vignetteShape": new index$1.Uniform3f(context),
"u_vignetteColor": new index$1.Uniform4f(context)
});
const vignetteUniformValues = (values) => ({
"u_vignetteShape": values.vignetteShape,
"u_vignetteColor": values.vignetteColor
});
const programUniforms = {
fillExtrusion: fillExtrusionUniforms,
fillExtrusionDepth: fillExtrusionDepthUniforms,
fillExtrusionPattern: fillExtrusionPatternUniforms,
fillExtrusionGroundEffect: fillExtrusionGroundEffectUniforms,
fill: fillUniforms,
fillPattern: fillPatternUniforms,
fillOutline: fillOutlineUniforms,
fillOutlinePattern: fillOutlinePatternUniforms,
building: buildingUniforms,
buildingBloom: buildingBloomUniforms,
buildingDepth: buildingDepthUniforms,
elevatedStructuresDepth: elevatedStructuresDepthUniforms,
elevatedStructures: elevatedStructuresUniforms,
elevatedStructuresDepthReconstruct: elevatedStructuresDepthReconstructUniforms,
circle: index$1.circleUniforms,
collisionBox: collisionUniforms,
collisionCircle: collisionCircleUniforms,
debug: debugUniforms,
clippingMask: clippingMaskUniforms,
heatmap: heatmapUniforms,
heatmapTexture: heatmapTextureUniforms,
hillshade: hillshadeUniforms,
hillshadePrepare: hillshadePrepareUniforms,
line: index$1.lineUniforms,
linePattern: index$1.linePatternUniforms,
raster: rasterUniforms,
rasterParticle: rasterParticleUniforms,
rasterParticleTexture: rasterParticleTextureUniforms,
rasterParticleDraw: rasterParticleDrawUniforms,
rasterParticleUpdate: rasterParticleUpdateUniforms,
symbol: symbolUniforms,
background: backgroundUniforms,
backgroundPattern: backgroundPatternUniforms,
terrainRaster: terrainRasterUniforms,
skybox: skyboxUniforms,
skyboxGradient: skyboxGradientUniforms,
skyboxCapture: skyboxCaptureUniforms,
globeRaster: globeRasterUniforms,
globeAtmosphere: atmosphereUniforms,
model: modelUniforms,
modelDepth: modelDepthUniforms,
groundShadow: groundShadowUniforms,
stars: starsUniforms,
snowParticle: snowUniforms,
rainParticle: rainUniforms,
vignette: vignetteUniforms,
occlusion: occlusionUniforms
};
class IndexBuffer {
constructor(context, array, dynamicDraw, noDestroy) {
this.id = IndexBuffer.uniqueIdxCounter;
IndexBuffer.uniqueIdxCounter++;
this.context = context;
const gl = context.gl;
this.buffer = gl.createBuffer();
this.dynamicDraw = Boolean(dynamicDraw);
this.context.unbindVAO();
context.bindElementBuffer.set(this.buffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, array.arrayBuffer, this.dynamicDraw ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW);
if (!this.dynamicDraw && !noDestroy) {
array.destroy();
}
}
bind() {
this.context.bindElementBuffer.set(this.buffer);
}
updateData(array) {
this.id = IndexBuffer.uniqueIdxCounter;
IndexBuffer.uniqueIdxCounter++;
const gl = this.context.gl;
index$1.assert(this.dynamicDraw);
this.context.unbindVAO();
this.bind();
gl.bufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, array.arrayBuffer);
}
destroy() {
const gl = this.context.gl;
if (this.buffer) {
gl.deleteBuffer(this.buffer);
delete this.buffer;
}
}
}
IndexBuffer.uniqueIdxCounter = 0;
const AttributeType = {
Int8: "BYTE",
Uint8: "UNSIGNED_BYTE",
Int16: "SHORT",
Uint16: "UNSIGNED_SHORT",
Int32: "INT",
Uint32: "UNSIGNED_INT",
Float32: "FLOAT"
};
class VertexBuffer {
/**
* @param dynamicDraw Whether this buffer will be repeatedly updated.
* @private
*/
constructor(context, array, attributes, dynamicDraw, noDestroy, instanceCount) {
this.length = array.length;
this.attributes = attributes;
this.itemSize = array.bytesPerElement;
this.dynamicDraw = dynamicDraw;
this.instanceCount = instanceCount;
this.context = context;
const gl = context.gl;
this.buffer = gl.createBuffer();
context.bindVertexBuffer.set(this.buffer);
gl.bufferData(gl.ARRAY_BUFFER, array.arrayBuffer, this.dynamicDraw ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW);
if (!this.dynamicDraw && !noDestroy) {
array.destroy();
}
}
bind() {
this.context.bindVertexBuffer.set(this.buffer);
}
updateData(array) {
index$1.assert(array.length === this.length);
const gl = this.context.gl;
this.bind();
gl.bufferSubData(gl.ARRAY_BUFFER, 0, array.arrayBuffer);
}
enableAttributes(gl, program) {
for (let j = 0; j < this.attributes.length; j++) {
const member = this.attributes[j];
const attribIndex = program.getAttributeLocation(gl, member.name);
if (attribIndex !== -1) {
gl.enableVertexAttribArray(attribIndex);
}
}
}
/**
* Set the attribute pointers in a WebGL context.
* @param gl The WebGL context.
* @param program The active WebGL program.
* @param vertexOffset Index of the starting vertex of the segment.
*/
setVertexAttribPointers(gl, program, vertexOffset) {
for (let j = 0; j < this.attributes.length; j++) {
const member = this.attributes[j];
const attribIndex = program.getAttributeLocation(gl, member.name);
if (attribIndex !== -1) {
gl.vertexAttribPointer(
attribIndex,
member.components,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
gl[AttributeType[member.type]],
false,
this.itemSize,
member.offset + this.itemSize * (vertexOffset || 0)
);
}
}
}
setVertexAttribDivisor(gl, program, value) {
for (let j = 0; j < this.attributes.length; j++) {
const member = this.attributes[j];
const attribIndex = program.getAttributeLocation(gl, member.name);
if (attribIndex !== -1 && this.instanceCount && this.instanceCount > 0) {
gl.vertexAttribDivisor(attribIndex, value);
}
}
}
/**
* Destroy the GL buffer bound to the given WebGL context.
*/
destroy() {
const gl = this.context.gl;
if (this.buffer) {
gl.deleteBuffer(this.buffer);
delete this.buffer;
}
}
}
class Framebuffer {
constructor(context, width, height, numColorAttachments, depthType) {
this.context = context;
this.width = width;
this.height = height;
const gl = context.gl;
const fbo = this.framebuffer = gl.createFramebuffer();
index$1.assert(numColorAttachments <= 2);
if (numColorAttachments > 0) {
this.colorAttachment0 = new ColorAttachment(context, fbo, 0);
}
if (numColorAttachments > 1) {
this.colorAttachment1 = new ColorAttachment(context, fbo, 1);
}
if (depthType) {
this.depthAttachmentType = depthType;
if (depthType === "renderbuffer") {
this.depthAttachment = new DepthRenderbufferAttachment(context, fbo);
} else {
this.depthAttachment = new DepthTextureAttachment(context, fbo);
}
}
}
createColorAttachment(context, idx) {
if (idx === 0) {
this.colorAttachment0 = new ColorAttachment(context, this.framebuffer, 0);
} else if (idx === 1) {
this.colorAttachment1 = new ColorAttachment(context, this.framebuffer, 1);
}
}
removeColorAttachment(context, idx) {
const gl = this.context.gl;
let texture;
if (idx === 0) {
texture = this.colorAttachment0.get();
this.colorAttachment0 = void 0;
} else if (idx === 1) {
texture = this.colorAttachment1.get();
this.colorAttachment1 = void 0;
}
if (texture) gl.deleteTexture(texture);
}
destroy() {
const gl = this.context.gl;
if (this.colorAttachment0) {
const texture = this.colorAttachment0.get();
if (texture) gl.deleteTexture(texture);
}
if (this.colorAttachment1) {
const texture = this.colorAttachment1.get();
if (texture) gl.deleteTexture(texture);
}
if (this.depthAttachment && this.depthAttachmentType) {
if (this.depthAttachmentType === "renderbuffer") {
const renderbuffer = this.depthAttachment.get();
if (renderbuffer) gl.deleteRenderbuffer(renderbuffer);
} else {
const texture = this.depthAttachment.get();
if (texture) gl.deleteTexture(texture);
}
}
gl.deleteFramebuffer(this.framebuffer);
}
}
class Context {
constructor(gl, options) {
this.gl = gl;
this.clearColor = new ClearColor(this);
this.clearDepth = new ClearDepth(this);
this.clearStencil = new ClearStencil(this);
this.colorMask = new ColorMask(this);
this.depthMask = new DepthMask(this);
this.stencilMask = new StencilMask(this);
this.stencilFunc = new StencilFunc(this);
this.stencilOp = new StencilOp(this);
this.stencilTest = new StencilTest(this);
this.depthRange = new DepthRange(this);
this.depthTest = new DepthTest(this);
this.depthFunc = new DepthFunc(this);
this.blend = new Blend(this);
this.blendFunc = new BlendFunc(this);
this.blendColor = new BlendColor(this);
this.blendEquation = new BlendEquation(this);
this.cullFace = new CullFace(this);
this.cullFaceSide = new CullFaceSide(this);
this.frontFace = new FrontFace(this);
this.program = new Program$1(this);
this.activeTexture = new ActiveTextureUnit(this);
this.viewport = new Viewport(this);
this.bindFramebuffer = new BindFramebuffer(this);
this.bindRenderbuffer = new BindRenderbuffer(this);
this.bindTexture = new BindTexture(this);
this.bindVertexBuffer = new BindVertexBuffer(this);
this.bindElementBuffer = new BindElementBuffer(this);
this.bindVertexArrayOES = new BindVertexArrayOES(this);
this.pixelStoreUnpack = new PixelStoreUnpack(this);
this.pixelStoreUnpackPremultiplyAlpha = new PixelStoreUnpackPremultiplyAlpha(this);
this.pixelStoreUnpackFlipY = new PixelStoreUnpackFlipY(this);
this.options = options ? Object.assign({}, options) : {};
if (!this.options.extTextureFilterAnisotropicForceOff) {
this.extTextureFilterAnisotropic = gl.getExtension("EXT_texture_filter_anisotropic") || gl.getExtension("MOZ_EXT_texture_filter_anisotropic") || gl.getExtension("WEBKIT_EXT_texture_filter_anisotropic");
if (this.extTextureFilterAnisotropic) {
this.extTextureFilterAnisotropicMax = gl.getParameter(this.extTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
}
}
this.extDebugRendererInfo = gl.getExtension("WEBGL_debug_renderer_info");
if (this.extDebugRendererInfo) {
this.renderer = gl.getParameter(this.extDebugRendererInfo.UNMASKED_RENDERER_WEBGL);
this.vendor = gl.getParameter(this.extDebugRendererInfo.UNMASKED_VENDOR_WEBGL);
}
this.forceManualRenderingForInstanceIDShaders = options && !!options.forceManualRenderingForInstanceIDShaders || this.renderer && this.renderer.indexOf("PowerVR") !== -1;
if (!this.options.extTextureFloatLinearForceOff) {
this.extTextureFloatLinear = gl.getExtension("OES_texture_float_linear");
}
this.extRenderToTextureHalfFloat = gl.getExtension("EXT_color_buffer_half_float");
this.extTimerQuery = gl.getExtension("EXT_disjoint_timer_query_webgl2");
this.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
this.extBlendFuncExtended = gl.getExtension("WEBGL_blend_func_extended");
}
setDefault() {
this.unbindVAO();
this.clearColor.setDefault();
this.clearDepth.setDefault();
this.clearStencil.setDefault();
this.colorMask.setDefault();
this.depthMask.setDefault();
this.stencilMask.setDefault();
this.stencilFunc.setDefault();
this.stencilOp.setDefault();
this.stencilTest.setDefault();
this.depthRange.setDefault();
this.depthTest.setDefault();
this.depthFunc.setDefault();
this.blend.setDefault();
this.blendFunc.setDefault();
this.blendColor.setDefault();
this.blendEquation.setDefault();
this.cullFace.setDefault();
this.cullFaceSide.setDefault();
this.frontFace.setDefault();
this.program.setDefault();
this.activeTexture.setDefault();
this.bindFramebuffer.setDefault();
this.pixelStoreUnpack.setDefault();
this.pixelStoreUnpackPremultiplyAlpha.setDefault();
this.pixelStoreUnpackFlipY.setDefault();
}
setDirty() {
this.clearColor.dirty = true;
this.clearDepth.dirty = true;
this.clearStencil.dirty = true;
this.colorMask.dirty = true;
this.depthMask.dirty = true;
this.stencilMask.dirty = true;
this.stencilFunc.dirty = true;
this.stencilOp.dirty = true;
this.stencilTest.dirty = true;
this.depthRange.dirty = true;
this.depthTest.dirty = true;
this.depthFunc.dirty = true;
this.blend.dirty = true;
this.blendFunc.dirty = true;
this.blendColor.dirty = true;
this.blendEquation.dirty = true;
this.cullFace.dirty = true;
this.cullFaceSide.dirty = true;
this.frontFace.dirty = true;
this.program.dirty = true;
this.activeTexture.dirty = true;
this.viewport.dirty = true;
this.bindFramebuffer.dirty = true;
this.bindRenderbuffer.dirty = true;
this.bindTexture.dirty = true;
this.bindVertexBuffer.dirty = true;
this.bindElementBuffer.dirty = true;
this.bindVertexArrayOES.dirty = true;
this.pixelStoreUnpack.dirty = true;
this.pixelStoreUnpackPremultiplyAlpha.dirty = true;
this.pixelStoreUnpackFlipY.dirty = true;
}
createIndexBuffer(array, dynamicDraw, noDestroy) {
return new IndexBuffer(this, array, dynamicDraw, noDestroy);
}
createVertexBuffer(array, attributes, dynamicDraw, noDestroy, instanceCount) {
return new VertexBuffer(this, array, attributes, dynamicDraw, noDestroy, instanceCount);
}
createRenderbuffer(storageFormat, width, height) {
const gl = this.gl;
const rbo = gl.createRenderbuffer();
this.bindRenderbuffer.set(rbo);
gl.renderbufferStorage(gl.RENDERBUFFER, storageFormat, width, height);
this.bindRenderbuffer.set(null);
return rbo;
}
createFramebuffer(width, height, numColorAttachments, depthType) {
return new Framebuffer(this, width, height, numColorAttachments, depthType);
}
clear({
color,
depth,
stencil,
colorMask
}) {
const gl = this.gl;
let mask = 0;
if (color) {
mask |= gl.COLOR_BUFFER_BIT;
this.clearColor.set(color.toNonPremultipliedRenderColor(null));
if (colorMask) {
this.colorMask.set(colorMask);
} else {
this.colorMask.set([true, true, true, true]);
}
}
if (typeof depth !== "undefined") {
mask |= gl.DEPTH_BUFFER_BIT;
this.depthRange.set([0, 1]);
this.clearDepth.set(depth);
this.depthMask.set(true);
}
if (typeof stencil !== "undefined") {
mask |= gl.STENCIL_BUFFER_BIT;
this.clearStencil.set(stencil);
this.stencilMask.set(255);
}
gl.clear(mask);
}
setCullFace(cullFaceMode) {
if (cullFaceMode.enable === false) {
this.cullFace.set(false);
} else {
this.cullFace.set(true);
this.cullFaceSide.set(cullFaceMode.mode);
this.frontFace.set(cullFaceMode.frontFace);
}
}
setDepthMode(depthMode) {
if (depthMode.func === this.gl.ALWAYS && !depthMode.mask) {
this.depthTest.set(false);
} else {
this.depthTest.set(true);
this.depthFunc.set(depthMode.func);
this.depthMask.set(depthMode.mask);
this.depthRange.set(depthMode.range);
}
}
setStencilMode(stencilMode) {
if (stencilMode.test.func === this.gl.ALWAYS && !stencilMode.mask) {
this.stencilTest.set(false);
} else {
this.stencilTest.set(true);
this.stencilMask.set(stencilMode.mask);
this.stencilOp.set([stencilMode.fail, stencilMode.depthFail, stencilMode.pass]);
this.stencilFunc.set({
func: stencilMode.test.func,
ref: stencilMode.ref,
mask: stencilMode.test.mask
});
}
}
setColorMode(colorMode) {
if (index$1.deepEqual(colorMode.blendFunction, ColorMode.Replace)) {
this.blend.set(false);
} else {
this.blend.set(true);
this.blendFunc.set(colorMode.blendFunction);
this.blendColor.set(colorMode.blendColor);
if (colorMode.blendEquation) {
this.blendEquation.set(colorMode.blendEquation);
} else {
this.blendEquation.setDefault();
}
}
this.colorMask.set(colorMode.mask);
}
unbindVAO() {
this.bindVertexArrayOES.set(null);
}
}
let quadTriangles;
function drawCollisionDebug(painter, sourceCache, layer, coords, translate, translateAnchor, isText) {
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const mercatorCenter = [
index$1.mercatorXfromLng(tr.center.lng),
index$1.mercatorYfromLat(tr.center.lat)
];
const symbolPlacement = layer.layout.get("symbol-placement");
const textVariableAnchor = layer.layout.get("text-variable-anchor");
const iconRotateWithMap = layer.layout.get("icon-rotation-alignment") === "map";
const textRotateWithMap = layer.layout.get("text-rotation-alignment") === "map";
const alongLine = symbolPlacement !== "point";
const tileBatches = [];
let circleCount = 0;
let circleOffset = 0;
for (let i = 0; i < coords.length; i++) {
const coord = coords[i];
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const invMatrix = bucket.getProjection().createInversionMatrix(tr, coord.canonical);
const defines = [];
const tileMatrix = getCollisionDebugTileProjectionMatrix(coord, bucket, tr);
const isIconPlacedAlongLine = !isText && iconRotateWithMap && alongLine;
const isTextPlacedAlongLine = isText && textRotateWithMap && alongLine;
const hasVariableAnchors = textVariableAnchor && bucket.hasTextData();
const updateIconTextFit = bucket.hasIconTextFit() && hasVariableAnchors && bucket.hasIconData();
const projectedPosOnLabelSpace = isIconPlacedAlongLine || isTextPlacedAlongLine || isText && hasVariableAnchors || updateIconTextFit;
const bucketIsGlobeProjection = bucket.projection.name === "globe";
const globeToMercator = bucketIsGlobeProjection ? index$1.globeToMercatorTransition(tr.zoom) : 0;
if (bucketIsGlobeProjection) {
defines.push("PROJECTION_GLOBE_VIEW");
if (projectedPosOnLabelSpace) {
defines.push("PROJECTED_POS_ON_VIEWPORT");
}
}
const program = painter.getOrCreateProgram("collisionBox", { defines });
let posMatrix = tileMatrix;
if (translate[0] !== 0 || translate[1] !== 0) {
posMatrix = painter.translatePosMatrix(tileMatrix, tile, translate, translateAnchor);
}
const buffers = isText ? bucket.textCollisionBox : bucket.iconCollisionBox;
const circleArray = bucket.collisionCircleArray;
if (circleArray.length > 0) {
const invTransform = index$1.create();
const transform = posMatrix;
index$1.mul(invTransform, bucket.placementInvProjMatrix, tr.glCoordMatrix);
index$1.mul(invTransform, invTransform, bucket.placementViewportMatrix);
tileBatches.push({
circleArray,
circleOffset,
transform,
invTransform,
projection: bucket.getProjection()
});
circleCount += circleArray.length / 4;
circleOffset = circleCount;
}
if (!buffers) continue;
if (painter.terrain) painter.terrain.setupElevationDraw(tile, program);
const tileId = bucketIsGlobeProjection ? [coord.canonical.x, coord.canonical.y, 1 << coord.canonical.z] : [0, 0, 0];
program.draw(
painter,
gl.LINES,
DepthMode.disabled,
StencilMode.disabled,
painter.colorModeForRenderPass(),
CullFaceMode.disabled,
collisionUniformValues(posMatrix, invMatrix, tr, globeToMercator, mercatorCenter, tile, tileId, bucket.getProjection()),
layer.id,
buffers.layoutVertexBuffer,
buffers.indexBuffer,
buffers.segments,
null,
tr.zoom,
null,
[buffers.collisionVertexBuffer, buffers.collisionVertexBufferExt]
);
}
if (!isText || !tileBatches.length) {
return;
}
const circleProgram = painter.getOrCreateProgram("collisionCircle");
const vertexData = new index$1.StructArrayLayout2f1f2i16();
vertexData.resize(circleCount * 4);
vertexData._trim();
let vertexOffset = 0;
for (const batch of tileBatches) {
for (let i = 0; i < batch.circleArray.length / 4; i++) {
const circleIdx = i * 4;
const x = batch.circleArray[circleIdx + 0];
const y = batch.circleArray[circleIdx + 1];
const radius = batch.circleArray[circleIdx + 2];
const collision = batch.circleArray[circleIdx + 3];
vertexData.emplace(vertexOffset++, x, y, radius, collision, 0);
vertexData.emplace(vertexOffset++, x, y, radius, collision, 1);
vertexData.emplace(vertexOffset++, x, y, radius, collision, 2);
vertexData.emplace(vertexOffset++, x, y, radius, collision, 3);
}
}
if (!quadTriangles || quadTriangles.length < circleCount * 2) {
quadTriangles = createQuadTriangles(circleCount);
}
const indexBuffer = context.createIndexBuffer(quadTriangles, true);
const vertexBuffer = context.createVertexBuffer(vertexData, index$1.collisionCircleLayout.members, true);
for (const batch of tileBatches) {
const uniforms = collisionCircleUniformValues(batch.transform, batch.invTransform, tr, batch.projection);
circleProgram.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
painter.colorModeForRenderPass(),
CullFaceMode.disabled,
uniforms,
layer.id,
vertexBuffer,
indexBuffer,
index$1.SegmentVector.simpleSegment(0, batch.circleOffset * 2, batch.circleArray.length, batch.circleArray.length / 2),
null,
tr.zoom
);
}
vertexBuffer.destroy();
indexBuffer.destroy();
}
function createQuadTriangles(quadCount) {
const triCount = quadCount * 2;
const array = new index$1.StructArrayLayout3ui6();
array.resize(triCount);
array._trim();
for (let i = 0; i < triCount; i++) {
const idx = i * 6;
array.uint16[idx + 0] = i * 4 + 0;
array.uint16[idx + 1] = i * 4 + 1;
array.uint16[idx + 2] = i * 4 + 2;
array.uint16[idx + 3] = i * 4 + 2;
array.uint16[idx + 4] = i * 4 + 3;
array.uint16[idx + 5] = i * 4 + 0;
}
return array;
}
const identityMat4 = index$1.create();
function drawSymbols(painter, sourceCache, layer, coords, variableOffsets) {
if (painter.renderPass !== "translucent") return;
const stencilMode = StencilMode.disabled;
const colorMode = painter.colorModeForRenderPass();
const variablePlacement = layer.layout.get("text-variable-anchor");
const textSizeScaleRange = layer.layout.get("text-size-scale-range");
const textScaleFactor = index$1.clamp(painter.scaleFactor, textSizeScaleRange[0], textSizeScaleRange[1]);
if (variablePlacement) {
updateVariableAnchors(
coords,
painter,
layer,
sourceCache,
layer.layout.get("text-rotation-alignment"),
layer.layout.get("text-pitch-alignment"),
variableOffsets,
textScaleFactor
);
}
const areIconsVisible = layer.paint.get("icon-opacity").constantOr(1) !== 0;
const areTextsVisible = layer.paint.get("text-opacity").constantOr(1) !== 0;
if (layer.layout.get("symbol-sort-key").constantOr(1) !== void 0 && (areIconsVisible || areTextsVisible)) {
drawLayerSymbols(painter, sourceCache, layer, coords, stencilMode, colorMode);
} else {
if (areIconsVisible) {
drawLayerSymbols(painter, sourceCache, layer, coords, stencilMode, colorMode, { onlyIcons: true });
}
if (areTextsVisible) {
drawLayerSymbols(painter, sourceCache, layer, coords, stencilMode, colorMode, { onlyText: true });
}
}
if (sourceCache.map.showCollisionBoxes) {
drawCollisionDebug(
painter,
sourceCache,
layer,
coords,
layer.paint.get("text-translate"),
layer.paint.get("text-translate-anchor"),
true
);
drawCollisionDebug(
painter,
sourceCache,
layer,
coords,
layer.paint.get("icon-translate"),
layer.paint.get("icon-translate-anchor"),
false
);
}
}
function computeGlobeCameraUp(transform) {
const viewMatrix = transform._camera.getWorldToCamera(transform.worldSize, 1);
const viewToEcef = index$1.multiply([], viewMatrix, transform.globeMatrix);
index$1.invert(viewToEcef, viewToEcef);
const cameraUpVector = [0, 0, 0];
const up = [0, 1, 0, 0];
index$1.transformMat4$1(up, up, viewToEcef);
cameraUpVector[0] = up[0];
cameraUpVector[1] = up[1];
cameraUpVector[2] = up[2];
index$1.normalize(cameraUpVector, cameraUpVector);
return cameraUpVector;
}
function calculateVariableRenderShift({
width,
height,
anchor,
textOffset,
textScale
}, renderTextSize) {
const { horizontalAlign, verticalAlign } = index$1.getAnchorAlignment(anchor);
const shiftX = -(horizontalAlign - 0.5) * width;
const shiftY = -(verticalAlign - 0.5) * height;
const variableOffset = index$1.evaluateVariableOffset(anchor, textOffset);
return new index$1.Point(
(shiftX / textScale + variableOffset[0]) * renderTextSize,
(shiftY / textScale + variableOffset[1]) * renderTextSize
);
}
function updateVariableAnchors(coords, painter, layer, sourceCache, rotationAlignment, pitchAlignment, variableOffsets, textScaleFactor) {
const tr = painter.transform;
const rotateWithMap = rotationAlignment === "map";
const pitchWithMap = pitchAlignment === "map";
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || !bucket.text || !bucket.text.segments.get().length) {
continue;
}
const sizeData = bucket.textSizeData;
const size = index$1.evaluateSizeForZoom(sizeData, tr.zoom, textScaleFactor);
const tileMatrix = getSymbolTileProjectionMatrix(coord, bucket.getProjection(), tr);
const pixelsToTileUnits = tr.calculatePixelsToTileUnitsMatrix(tile);
const labelPlaneMatrix = getLabelPlaneMatrixForRendering(tileMatrix, tile.tileID.canonical, pitchWithMap, rotateWithMap, tr, bucket.getProjection(), pixelsToTileUnits);
const updateTextFitIcon = bucket.hasIconTextFit() && bucket.hasIconData();
if (size) {
const tileScale = Math.pow(2, tr.zoom - tile.tileID.overscaledZ);
updateVariableAnchorsForBucket(
bucket,
rotateWithMap,
pitchWithMap,
variableOffsets,
tr,
labelPlaneMatrix,
coord,
tileScale,
size,
updateTextFitIcon
);
}
}
}
function updateVariableAnchorsForBucket(bucket, rotateWithMap, pitchWithMap, variableOffsets, transform, labelPlaneMatrix, coord, tileScale, size, updateTextFitIcon) {
const placedSymbols = bucket.text.placedSymbolArray;
const dynamicTextLayoutVertexArray = bucket.text.dynamicLayoutVertexArray;
const dynamicIconLayoutVertexArray = bucket.icon.dynamicLayoutVertexArray;
const placedTextShifts = {};
const projection = bucket.getProjection();
const tileMatrix = getSymbolTileProjectionMatrix(coord, projection, transform);
const elevation = transform.elevation;
const metersToTile = projection.upVectorScale(coord.canonical, transform.center.lat, transform.worldSize).metersToTile;
dynamicTextLayoutVertexArray.clear();
for (let s = 0; s < placedSymbols.length; s++) {
const symbol = placedSymbols.get(s);
const { tileAnchorX, tileAnchorY, numGlyphs } = symbol;
const skipOrientation = bucket.allowVerticalPlacement && !symbol.placedOrientation;
const variableOffset = !symbol.hidden && symbol.crossTileID && !skipOrientation ? variableOffsets[symbol.crossTileID] : null;
if (!variableOffset) {
hideGlyphs(numGlyphs, dynamicTextLayoutVertexArray);
} else {
let dx = 0, dy = 0, dz = 0;
const renderElevatedRoads = bucket.elevationType === "road";
if (elevation || renderElevatedRoads) {
const elevationFeature = renderElevatedRoads ? bucket.getElevationFeatureForText(s) : null;
const h = index$1.Elevation.getAtTileOffset(coord, new index$1.Point(tileAnchorX, tileAnchorY), elevation, elevationFeature);
const [ux, uy, uz] = projection.upVector(coord.canonical, tileAnchorX, tileAnchorY);
dx = h * ux * metersToTile;
dy = h * uy * metersToTile;
dz = h * uz * metersToTile;
}
let [x, y, z, w] = project(
symbol.projectedAnchorX + dx,
symbol.projectedAnchorY + dy,
symbol.projectedAnchorZ + dz,
pitchWithMap ? tileMatrix : labelPlaneMatrix
);
const perspectiveRatio = getPerspectiveRatio(transform.getCameraToCenterDistance(projection), w);
let renderTextSize = index$1.evaluateSizeForFeature(bucket.textSizeData, size, symbol) * perspectiveRatio / index$1.ONE_EM;
if (pitchWithMap) {
renderTextSize *= bucket.tilePixelRatio / tileScale;
}
const shift = calculateVariableRenderShift(variableOffset, renderTextSize);
if (pitchWithMap) {
({ x, y, z } = projection.projectTilePoint(tileAnchorX + shift.x, tileAnchorY + shift.y, coord.canonical));
[x, y, z] = project(x + dx, y + dy, z + dz, labelPlaneMatrix);
} else {
if (rotateWithMap) shift._rotate(-transform.angle);
x += shift.x;
y += shift.y;
z = 0;
}
const angle = bucket.allowVerticalPlacement && symbol.placedOrientation === index$1.WritingMode.vertical ? Math.PI / 2 : 0;
for (let g = 0; g < numGlyphs; g++) {
index$1.addDynamicAttributes(dynamicTextLayoutVertexArray, x, y, z, angle);
}
if (updateTextFitIcon && symbol.associatedIconIndex >= 0) {
placedTextShifts[symbol.associatedIconIndex] = { x, y, z, angle };
}
}
}
if (updateTextFitIcon) {
dynamicIconLayoutVertexArray.clear();
const placedIcons = bucket.icon.placedSymbolArray;
for (let i = 0; i < placedIcons.length; i++) {
const placedIcon = placedIcons.get(i);
const { numGlyphs } = placedIcon;
const shift = placedTextShifts[i];
if (placedIcon.hidden || !shift) {
hideGlyphs(numGlyphs, dynamicIconLayoutVertexArray);
} else {
const { x, y, z, angle } = shift;
for (let g = 0; g < numGlyphs; g++) {
index$1.addDynamicAttributes(dynamicIconLayoutVertexArray, x, y, z, angle);
}
}
}
bucket.icon.dynamicLayoutVertexBuffer.updateData(dynamicIconLayoutVertexArray);
}
bucket.text.dynamicLayoutVertexBuffer.updateData(dynamicTextLayoutVertexArray);
}
function drawLayerSymbols(painter, sourceCache, layer, coords, stencilMode, colorMode, options = {}) {
const iconTranslate = layer.paint.get("icon-translate");
const textTranslate = layer.paint.get("text-translate");
const iconTranslateAnchor = layer.paint.get("icon-translate-anchor");
const textTranslateAnchor = layer.paint.get("text-translate-anchor");
const iconRotationAlignment = layer.layout.get("icon-rotation-alignment");
const textRotationAlignment = layer.layout.get("text-rotation-alignment");
const iconPitchAlignment = layer.layout.get("icon-pitch-alignment");
const textPitchAlignment = layer.layout.get("text-pitch-alignment");
const iconKeepUpright = layer.layout.get("icon-keep-upright");
const textKeepUpright = layer.layout.get("text-keep-upright");
const iconSaturation = layer.paint.get("icon-color-saturation");
const iconContrast = layer.paint.get("icon-color-contrast");
const iconBrightnessMin = layer.paint.get("icon-color-brightness-min");
const iconBrightnessMax = layer.paint.get("icon-color-brightness-max");
const elevationFromSea = layer.layout.get("symbol-elevation-reference") === "sea";
const ignoreLut = layer.layout.get("icon-image-use-theme") === "none";
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const iconRotateWithMap = iconRotationAlignment === "map";
const textRotateWithMap = textRotationAlignment === "map";
const iconPitchWithMap = iconPitchAlignment === "map";
const textPitchWithMap = textPitchAlignment === "map";
const hasSortKey = layer.layout.get("symbol-sort-key").constantOr(1) !== void 0;
let sortFeaturesByKey = false;
const depthModeForLayer = painter.depthModeForSublayer(0, DepthMode.ReadOnly);
const depthModeFor3D = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const mercatorCenter = [
index$1.mercatorXfromLng(tr.center.lng),
index$1.mercatorYfromLat(tr.center.lat)
];
const variablePlacement = layer.layout.get("text-variable-anchor");
const isGlobeProjection = tr.projection.name === "globe";
const tileRenderState = [];
const mercatorCameraUp = [0, -1, 0];
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
if (bucket.projection.name === "mercator" && isGlobeProjection) {
continue;
}
if (bucket.fullyClipped) continue;
const bucketIsGlobeProjection = bucket.projection.name === "globe";
const globeToMercator = bucketIsGlobeProjection ? index$1.globeToMercatorTransition(tr.zoom) : 0;
const tileMatrix = getSymbolTileProjectionMatrix(coord, bucket.getProjection(), tr);
const s = tr.calculatePixelsToTileUnitsMatrix(tile);
const hasVariableAnchors = variablePlacement && bucket.hasTextData();
const updateTextFitIcon = bucket.hasIconTextFit() && hasVariableAnchors && bucket.hasIconData();
const invMatrix = bucket.getProjection().createInversionMatrix(tr, coord.canonical);
const orientationNormalScale = (1 << tile.tileID.canonical.z) * index$1.EXTENT / painter.transform.worldSize;
const getGroundShadowFactor = (renderWithShadows) => {
let groundShadowFactor = [0, 0, 0];
if (renderWithShadows) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
}
return groundShadowFactor;
};
const setOcclusionDefines = (defines) => {
if (!tr.depthOcclusionForSymbolsAndCircles) {
return;
}
if (!layer.hasOcclusionOpacityProperties) {
if (painter.terrain) {
defines.push("DEPTH_D24");
defines.push("DEPTH_OCCLUSION");
}
} else {
defines.push("DEPTH_D24");
defines.push("DEPTH_OCCLUSION");
}
};
const setLutDefines = (defines) => {
if (!layer.lut || ignoreLut) {
return;
}
if (!layer.lut.texture) {
layer.lut.texture = new index$1.Texture3D(painter.context, layer.lut.image, [layer.lut.image.height, layer.lut.image.height, layer.lut.image.height], context.gl.RGBA8);
}
context.activeTexture.set(context.gl.TEXTURE0 + TextureSlots.LUT);
if (layer.lut.texture) {
layer.lut.texture.bind(context.gl.LINEAR, context.gl.CLAMP_TO_EDGE);
}
defines.push("APPLY_LUT_ON_GPU");
};
const getIconState = () => {
const alongLine = iconRotateWithMap && layer.layout.get("symbol-placement") !== "point";
const baseDefines = [];
setOcclusionDefines(baseDefines);
setLutDefines(baseDefines);
const projectedPosOnLabelSpace = alongLine || updateTextFitIcon;
const renderElevatedRoads = bucket.elevationType === "road";
const shadowRenderer = painter.shadowRenderer;
const renderWithShadows = renderElevatedRoads && iconPitchWithMap && !!shadowRenderer && shadowRenderer.enabled;
const groundShadowFactor = getGroundShadowFactor(renderWithShadows);
const depthMode = renderElevatedRoads && iconPitchWithMap && !painter.terrain ? depthModeFor3D : depthModeForLayer;
const transitionProgress = layer.paint.get("icon-image-cross-fade");
if (painter.terrainRenderModeElevated() && iconPitchWithMap) {
baseDefines.push("PITCH_WITH_MAP_TERRAIN");
}
if (bucketIsGlobeProjection) {
baseDefines.push("PROJECTION_GLOBE_VIEW");
if (projectedPosOnLabelSpace) {
baseDefines.push("PROJECTED_POS_ON_VIEWPORT");
}
}
if (transitionProgress > 0 && bucket.hasAnySecondaryIcon) {
baseDefines.push("ICON_TRANSITION");
}
if (bucket.icon.zOffsetVertexBuffer && (!renderElevatedRoads || !painter.terrain)) {
baseDefines.push("Z_OFFSET");
}
if (iconSaturation !== 0 || iconContrast !== 0 || iconBrightnessMin !== 0 || iconBrightnessMax !== 1) {
baseDefines.push("COLOR_ADJUSTMENT");
}
if (bucket.sdfIcons) {
baseDefines.push("RENDER_SDF");
}
if (renderWithShadows) {
baseDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE", "NORMAL_OFFSET");
}
if (renderElevatedRoads && iconPitchWithMap && !painter.terrain && bucket.icon.orientationVertexBuffer) {
baseDefines.push("ELEVATED_ROADS");
}
const programConfiguration = bucket.icon.programConfigurations.get(layer.id);
const program = painter.getOrCreateProgram("symbol", { config: programConfiguration, defines: baseDefines });
const texSize = tile.imageAtlasTexture ? tile.imageAtlasTexture.size : [0, 0];
const sizeData = bucket.iconSizeData;
const size = index$1.evaluateSizeForZoom(sizeData, tr.zoom);
const transformed = iconPitchWithMap || !tr.isOrthographic;
const labelPlaneMatrixRendering = getLabelPlaneMatrixForRendering(tileMatrix, tile.tileID.canonical, iconPitchWithMap, iconRotateWithMap, tr, bucket.getProjection(), s);
const glCoordMatrix = getGlCoordMatrix(tileMatrix, tile.tileID.canonical, iconPitchWithMap, iconRotateWithMap, tr, bucket.getProjection(), s);
const uglCoordMatrix = painter.translatePosMatrix(glCoordMatrix, tile, iconTranslate, iconTranslateAnchor, true);
const matrix = painter.translatePosMatrix(tileMatrix, tile, iconTranslate, iconTranslateAnchor);
const uLabelPlaneMatrix = projectedPosOnLabelSpace ? identityMat4 : labelPlaneMatrixRendering;
const rotateInShader = iconRotateWithMap && !iconPitchWithMap && !alongLine;
let globeCameraUp = mercatorCameraUp;
if ((isGlobeProjection || tr.mercatorFromTransition) && !iconRotateWithMap) {
globeCameraUp = computeGlobeCameraUp(tr);
}
const cameraUpVector = bucketIsGlobeProjection ? globeCameraUp : mercatorCameraUp;
const colorAdjustmentMatrix = layer.getColorAdjustmentMatrix(iconSaturation, iconContrast, iconBrightnessMin, iconBrightnessMax);
const uniformValues = symbolUniformValues(
sizeData.kind,
size,
rotateInShader,
iconPitchWithMap,
painter,
matrix,
uLabelPlaneMatrix,
uglCoordMatrix,
elevationFromSea,
false,
texSize,
[0, 0],
true,
coord,
globeToMercator,
mercatorCenter,
invMatrix,
cameraUpVector,
bucket.getProjection(),
groundShadowFactor,
orientationNormalScale,
colorAdjustmentMatrix,
transitionProgress,
null
);
const atlasTexture = tile.imageAtlasTexture ? tile.imageAtlasTexture : null;
const iconScaled = layer.layout.get("icon-size").constantOr(0) !== 1 || bucket.iconsNeedLinear;
const atlasInterpolation = bucket.sdfIcons || painter.options.rotating || painter.options.zooming || iconScaled || transformed ? gl.LINEAR : gl.NEAREST;
const hasHalo = bucket.sdfIcons && layer.paint.get("icon-halo-width").constantOr(1) !== 0;
const labelPlaneMatrixInv = painter.terrain && iconPitchWithMap && alongLine ? index$1.invert(index$1.create(), labelPlaneMatrixRendering) : identityMat4;
if (alongLine && bucket.icon) {
const getElevation = index$1.Elevation.getAtTileOffsetFunc(coord, tr.center.lat, tr.worldSize, bucket.getProjection());
const labelPlaneMatrixPlacement = getLabelPlaneMatrixForPlacement(tileMatrix, tile.tileID.canonical, iconPitchWithMap, iconRotateWithMap, tr, bucket.getProjection(), s);
const iconSizeScaleRange = layer.layout.get("icon-size-scale-range");
const iconScaleFactor = index$1.clamp(painter.scaleFactor, iconSizeScaleRange[0], iconSizeScaleRange[1]);
updateLineLabels(bucket, tileMatrix, painter, false, labelPlaneMatrixPlacement, glCoordMatrix, iconPitchWithMap, iconKeepUpright, getElevation, coord, iconScaleFactor);
}
return {
program,
buffers: bucket.icon,
uniformValues,
atlasTexture,
atlasTextureIcon: null,
atlasInterpolation,
atlasInterpolationIcon: null,
isSDF: bucket.sdfIcons,
hasHalo,
depthMode,
tile,
renderWithShadows,
labelPlaneMatrixInv
};
};
const getTextState = () => {
const alongLine = textRotateWithMap && layer.layout.get("symbol-placement") !== "point";
const baseDefines = [];
const projectedPosOnLabelSpace = alongLine || variablePlacement || updateTextFitIcon;
const renderElevatedRoads = bucket.elevationType === "road";
const shadowRenderer = painter.shadowRenderer;
const renderWithShadows = renderElevatedRoads && textPitchWithMap && !!shadowRenderer && shadowRenderer.enabled;
const groundShadowFactor = getGroundShadowFactor(renderWithShadows);
const depthMode = renderElevatedRoads && textPitchWithMap && !painter.terrain ? depthModeFor3D : depthModeForLayer;
if (painter.terrainRenderModeElevated() && textPitchWithMap) {
baseDefines.push("PITCH_WITH_MAP_TERRAIN");
}
if (bucketIsGlobeProjection) {
baseDefines.push("PROJECTION_GLOBE_VIEW");
if (projectedPosOnLabelSpace) {
baseDefines.push("PROJECTED_POS_ON_VIEWPORT");
}
}
if (bucket.text.zOffsetVertexBuffer && (!renderElevatedRoads || !painter.terrain)) {
baseDefines.push("Z_OFFSET");
}
if (bucket.iconsInText) {
baseDefines.push("RENDER_TEXT_AND_SYMBOL");
}
baseDefines.push("RENDER_SDF");
if (renderWithShadows) {
baseDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE", "NORMAL_OFFSET");
}
if (renderElevatedRoads && textPitchWithMap && !painter.terrain && bucket.text.orientationVertexBuffer) {
baseDefines.push("ELEVATED_ROADS");
}
setOcclusionDefines(baseDefines);
const programConfiguration = bucket.text.programConfigurations.get(layer.id);
const program = painter.getOrCreateProgram("symbol", { config: programConfiguration, defines: baseDefines });
let texSizeIcon = [0, 0];
let atlasTextureIcon = null;
let atlasInterpolationIcon;
const sizeData = bucket.textSizeData;
if (bucket.iconsInText) {
texSizeIcon = tile.imageAtlasTexture ? tile.imageAtlasTexture.size : [0, 0];
atlasTextureIcon = tile.imageAtlasTexture ? tile.imageAtlasTexture : null;
const transformed = textPitchWithMap || !tr.isOrthographic;
const zoomDependentSize = sizeData.kind === "composite" || sizeData.kind === "camera";
atlasInterpolationIcon = transformed || painter.options.rotating || painter.options.zooming || zoomDependentSize ? gl.LINEAR : gl.NEAREST;
}
const texSize = tile.glyphAtlasTexture ? tile.glyphAtlasTexture.size : [0, 0];
const textSizeScaleRange = layer.layout.get("text-size-scale-range");
const textScaleFactor = index$1.clamp(painter.scaleFactor, textSizeScaleRange[0], textSizeScaleRange[1]);
const size = index$1.evaluateSizeForZoom(sizeData, tr.zoom, textScaleFactor);
const labelPlaneMatrixRendering = getLabelPlaneMatrixForRendering(tileMatrix, tile.tileID.canonical, textPitchWithMap, textRotateWithMap, tr, bucket.getProjection(), s);
const glCoordMatrix = getGlCoordMatrix(tileMatrix, tile.tileID.canonical, textPitchWithMap, textRotateWithMap, tr, bucket.getProjection(), s);
const uglCoordMatrix = painter.translatePosMatrix(glCoordMatrix, tile, textTranslate, textTranslateAnchor, true);
const matrix = painter.translatePosMatrix(tileMatrix, tile, textTranslate, textTranslateAnchor);
const uLabelPlaneMatrix = projectedPosOnLabelSpace ? identityMat4 : labelPlaneMatrixRendering;
const rotateInShader = textRotateWithMap && !textPitchWithMap && !alongLine;
let globeCameraUp = mercatorCameraUp;
if ((isGlobeProjection || tr.mercatorFromTransition) && !textRotateWithMap) {
globeCameraUp = computeGlobeCameraUp(tr);
}
const cameraUpVector = bucketIsGlobeProjection ? globeCameraUp : mercatorCameraUp;
const uniformValues = symbolUniformValues(
sizeData.kind,
size,
rotateInShader,
textPitchWithMap,
painter,
matrix,
uLabelPlaneMatrix,
uglCoordMatrix,
elevationFromSea,
true,
texSize,
texSizeIcon,
true,
coord,
globeToMercator,
mercatorCenter,
invMatrix,
cameraUpVector,
bucket.getProjection(),
groundShadowFactor,
orientationNormalScale,
null,
null,
textScaleFactor
);
const atlasTexture = tile.glyphAtlasTexture ? tile.glyphAtlasTexture : null;
const atlasInterpolation = gl.LINEAR;
const hasHalo = layer.paint.get("text-halo-width").constantOr(1) !== 0;
const labelPlaneMatrixInv = painter.terrain && textPitchWithMap && alongLine ? index$1.invert(index$1.create(), labelPlaneMatrixRendering) : identityMat4;
if (alongLine && bucket.text) {
const getElevation = index$1.Elevation.getAtTileOffsetFunc(coord, tr.center.lat, tr.worldSize, bucket.getProjection());
const labelPlaneMatrixPlacement = getLabelPlaneMatrixForPlacement(tileMatrix, tile.tileID.canonical, textPitchWithMap, textRotateWithMap, tr, bucket.getProjection(), s);
updateLineLabels(bucket, tileMatrix, painter, true, labelPlaneMatrixPlacement, glCoordMatrix, textPitchWithMap, textKeepUpright, getElevation, coord, textScaleFactor);
}
return {
program,
buffers: bucket.text,
uniformValues,
atlasTexture,
atlasTextureIcon,
atlasInterpolation,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
atlasInterpolationIcon,
isSDF: true,
hasHalo,
depthMode,
tile,
renderWithShadows,
labelPlaneMatrixInv
};
};
const iconSegmentsLength = bucket.icon.segments.get().length;
const textSegmentsLength = bucket.text.segments.get().length;
const iconState = iconSegmentsLength && !options.onlyText ? getIconState() : null;
const textState = textSegmentsLength && !options.onlyIcons ? getTextState() : null;
const iconOpacity = layer.paint.get("icon-opacity").constantOr(1);
const textOpacity = layer.paint.get("text-opacity").constantOr(1);
if (hasSortKey && bucket.canOverlap) {
sortFeaturesByKey = true;
const oldIconSegments = iconOpacity && !options.onlyText ? bucket.icon.segments.get() : [];
const oldTextSegments = textOpacity && !options.onlyIcons ? bucket.text.segments.get() : [];
for (const segment of oldIconSegments) {
tileRenderState.push({
segments: new index$1.SegmentVector([segment]),
sortKey: segment.sortKey,
state: iconState
});
}
for (const segment of oldTextSegments) {
tileRenderState.push({
segments: new index$1.SegmentVector([segment]),
sortKey: segment.sortKey,
state: textState
});
}
} else {
if (!options.onlyText) {
tileRenderState.push({
segments: iconOpacity ? bucket.icon.segments : new index$1.SegmentVector([]),
sortKey: 0,
state: iconState
});
}
if (!options.onlyIcons) {
tileRenderState.push({
segments: textOpacity ? bucket.text.segments : new index$1.SegmentVector([]),
sortKey: 0,
state: textState
});
}
}
}
if (sortFeaturesByKey) {
tileRenderState.sort((a, b) => a.sortKey - b.sortKey);
}
for (const segmentState of tileRenderState) {
const state = segmentState.state;
if (!state) {
continue;
}
if (painter.terrain) {
const options2 = {
// Use depth occlusion only for unspecified opacity multiplier case
useDepthForOcclusion: tr.depthOcclusionForSymbolsAndCircles,
labelPlaneMatrixInv: state.labelPlaneMatrixInv
};
painter.terrain.setupElevationDraw(state.tile, state.program, options2);
} else {
painter.setupDepthForOcclusion(tr.depthOcclusionForSymbolsAndCircles, state.program);
}
context.activeTexture.set(gl.TEXTURE0);
if (state.atlasTexture) {
state.atlasTexture.bind(state.atlasInterpolation, gl.CLAMP_TO_EDGE, true);
}
if (state.atlasTextureIcon) {
context.activeTexture.set(gl.TEXTURE1);
if (state.atlasTextureIcon) {
state.atlasTextureIcon.bind(state.atlasInterpolationIcon, gl.CLAMP_TO_EDGE, true);
}
}
if (state.renderWithShadows) {
painter.shadowRenderer.setupShadows(state.tile.tileID.toUnwrapped(), state.program, "vector-tile");
}
painter.uploadCommonLightUniforms(painter.context, state.program);
if (state.hasHalo) {
const uniformValues = state.uniformValues;
uniformValues["u_is_halo"] = 1;
drawSymbolElements(state.buffers, segmentState.segments, layer, painter, state.program, state.depthMode, stencilMode, colorMode, uniformValues, 2);
uniformValues["u_is_halo"] = 0;
} else {
if (state.isSDF) {
const uniformValues = state.uniformValues;
if (state.hasHalo) {
uniformValues["u_is_halo"] = 1;
drawSymbolElements(state.buffers, segmentState.segments, layer, painter, state.program, state.depthMode, stencilMode, colorMode, uniformValues, 1);
}
uniformValues["u_is_halo"] = 0;
}
drawSymbolElements(state.buffers, segmentState.segments, layer, painter, state.program, state.depthMode, stencilMode, colorMode, state.uniformValues, 1);
}
}
}
function drawSymbolElements(buffers, segments, layer, painter, program, depthMode, stencilMode, colorMode, uniformValues, instanceCount) {
const context = painter.context;
const gl = context.gl;
const dynamicBuffers = [buffers.dynamicLayoutVertexBuffer, buffers.opacityVertexBuffer, buffers.iconTransitioningVertexBuffer, buffers.globeExtVertexBuffer, buffers.zOffsetVertexBuffer, buffers.orientationVertexBuffer];
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
buffers.layoutVertexBuffer,
buffers.indexBuffer,
segments,
layer.paint,
painter.transform.zoom,
buffers.programConfigurations.get(layer.id),
dynamicBuffers,
instanceCount
);
}
function drawCircles(painter, sourceCache, layer, coords) {
if (painter.renderPass !== "translucent") return;
const opacity = layer.paint.get("circle-opacity");
const strokeWidth = layer.paint.get("circle-stroke-width");
const strokeOpacity = layer.paint.get("circle-stroke-opacity");
const sortFeaturesByKey = layer.layout.get("circle-sort-key").constantOr(1) !== void 0;
const emissiveStrength = layer.paint.get("circle-emissive-strength");
if (opacity.constantOr(1) === 0 && (strokeWidth.constantOr(1) === 0 || strokeOpacity.constantOr(1) === 0)) {
return;
}
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const terrainEnabled = !!(painter.terrain && painter.terrain.enabled);
const elevationReference = layer.layout.get("circle-elevation-reference");
const depthModeForLayer = painter.depthModeForSublayer(0, DepthMode.ReadOnly);
const depthModeFor3D = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const depthMode = elevationReference !== "none" && !terrainEnabled ? depthModeFor3D : depthModeForLayer;
const stencilMode = StencilMode.disabled;
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const isGlobeProjection = tr.projection.name === "globe";
const mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
const segmentsRenderStates = [];
for (let i = 0; i < coords.length; i++) {
const coord = coords[i];
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || bucket.projection.name !== tr.projection.name) continue;
const programConfiguration = bucket.programConfigurations.get(layer.id);
const layoutVertexBuffer = bucket.layoutVertexBuffer;
const globeExtVertexBuffer = bucket.globeExtVertexBuffer;
const indexBuffer = bucket.indexBuffer;
const definesValues = index$1.circleDefinesValues(layer);
const dynamicBuffers = [globeExtVertexBuffer];
const affectedByFog = painter.isTileAffectedByFog(coord);
if (isGlobeProjection) {
definesValues.push("PROJECTION_GLOBE_VIEW");
}
definesValues.push("DEPTH_D24");
if (painter.terrain && tr.depthOcclusionForSymbolsAndCircles) {
definesValues.push("DEPTH_OCCLUSION");
}
if (bucket.hasElevation && !painter.terrain) {
definesValues.push("ELEVATED_ROADS");
index$1.assert(bucket.elevatedLayoutVertexBuffer);
dynamicBuffers.push(bucket.elevatedLayoutVertexBuffer);
}
const program = painter.getOrCreateProgram("circle", { config: programConfiguration, defines: definesValues, overrideFog: affectedByFog });
const invMatrix = tr.projection.createInversionMatrix(tr, coord.canonical);
const uniformValues = index$1.circleUniformValues(painter, coord, tile, invMatrix, mercatorCenter, layer);
const state = {
programConfiguration,
program,
layoutVertexBuffer,
dynamicBuffers,
indexBuffer,
uniformValues,
tile
};
if (sortFeaturesByKey) {
const oldSegments = bucket.segments.get();
for (const segment of oldSegments) {
segmentsRenderStates.push({
segments: new index$1.SegmentVector([segment]),
sortKey: segment.sortKey,
state
});
}
} else {
segmentsRenderStates.push({
segments: bucket.segments,
sortKey: 0,
state
});
}
}
if (sortFeaturesByKey) {
segmentsRenderStates.sort((a, b) => a.sortKey - b.sortKey);
}
const terrainOptions = { useDepthForOcclusion: tr.depthOcclusionForSymbolsAndCircles };
for (const segmentsState of segmentsRenderStates) {
const { programConfiguration, program, layoutVertexBuffer, dynamicBuffers, indexBuffer, uniformValues, tile } = segmentsState.state;
const segments = segmentsState.segments;
if (painter.terrain) {
painter.terrain.setupElevationDraw(tile, program, terrainOptions);
}
painter.uploadCommonUniforms(context, program, tile.tileID.toUnwrapped());
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
layoutVertexBuffer,
indexBuffer,
segments,
layer.paint,
tr.zoom,
programConfiguration,
dynamicBuffers
);
}
}
function drawHeatmap(painter, sourceCache, layer, coords) {
if (layer.paint.get("heatmap-opacity") === 0) {
return;
}
if (painter.renderPass === "offscreen") {
const context = painter.context;
const gl = context.gl;
const stencilMode = StencilMode.disabled;
const colorMode = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [true, true, true, true]);
const resolutionScaling = painter.transform.projection.name === "globe" ? 0.5 : 0.25;
bindFramebuffer(context, painter, layer, resolutionScaling);
context.clear({ color: index$1.Color.transparent });
const tr = painter.transform;
const isGlobeProjection = tr.projection.name === "globe";
const definesValues = isGlobeProjection ? ["PROJECTION_GLOBE_VIEW"] : [];
const cullMode = isGlobeProjection ? CullFaceMode.frontCCW : CullFaceMode.disabled;
const mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
for (let i = 0; i < coords.length; i++) {
const coord = coords[i];
if (sourceCache.hasRenderableParent(coord)) continue;
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || bucket.projection.name !== tr.projection.name) continue;
const affectedByFog = painter.isTileAffectedByFog(coord);
const programConfiguration = bucket.programConfigurations.get(layer.id);
const program = painter.getOrCreateProgram("heatmap", { config: programConfiguration, defines: definesValues, overrideFog: affectedByFog });
const { zoom } = painter.transform;
if (painter.terrain) painter.terrain.setupElevationDraw(tile, program);
painter.uploadCommonUniforms(context, program, coord.toUnwrapped());
const invMatrix = tr.projection.createInversionMatrix(tr, coord.canonical);
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
stencilMode,
colorMode,
cullMode,
heatmapUniformValues(
painter,
coord,
tile,
invMatrix,
mercatorCenter,
zoom,
layer.paint.get("heatmap-intensity")
),
layer.id,
bucket.layoutVertexBuffer,
bucket.indexBuffer,
bucket.segments,
layer.paint,
painter.transform.zoom,
programConfiguration,
isGlobeProjection ? [bucket.globeExtVertexBuffer] : null
);
}
context.viewport.set([0, 0, painter.width, painter.height]);
} else if (painter.renderPass === "translucent") {
painter.context.setColorMode(painter.colorModeForRenderPass());
renderTextureToMap$1(painter, layer);
}
}
function bindFramebuffer(context, painter, layer, scaling) {
const gl = context.gl;
const width = painter.width * scaling;
const height = painter.height * scaling;
context.activeTexture.set(gl.TEXTURE1);
context.viewport.set([0, 0, width, height]);
let fbo = layer.heatmapFbo;
if (!fbo || fbo && (fbo.width !== width || fbo.height !== height)) {
if (fbo) {
fbo.destroy();
}
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
fbo = layer.heatmapFbo = context.createFramebuffer(width, height, 1, null);
bindTextureToFramebuffer(context, painter, texture, fbo, width, height);
} else {
gl.bindTexture(gl.TEXTURE_2D, fbo.colorAttachment0.get());
context.bindFramebuffer.set(fbo.framebuffer);
}
}
function bindTextureToFramebuffer(context, painter, texture, fbo, width, height) {
const gl = context.gl;
const type = context.extRenderToTextureHalfFloat ? gl.HALF_FLOAT : gl.UNSIGNED_BYTE;
gl.texImage2D(gl.TEXTURE_2D, 0, context.extRenderToTextureHalfFloat ? gl.RGBA16F : gl.RGBA, width, height, 0, gl.RGBA, type, null);
fbo.colorAttachment0.set(texture);
}
function renderTextureToMap$1(painter, layer) {
const context = painter.context;
const gl = context.gl;
const fbo = layer.heatmapFbo;
if (!fbo) return;
context.activeTexture.set(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, fbo.colorAttachment0.get());
context.activeTexture.set(gl.TEXTURE1);
let colorRampTexture = layer.colorRampTexture;
if (!colorRampTexture) {
colorRampTexture = layer.colorRampTexture = new index$1.Texture(context, layer.colorRamp, gl.RGBA8);
}
colorRampTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
painter.getOrCreateProgram("heatmapTexture").draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
painter.colorModeForRenderPass(),
CullFaceMode.disabled,
heatmapTextureUniformValues(painter, layer, 0, 1),
layer.id,
painter.viewportBuffer,
painter.quadTriangleIndexBuffer,
painter.viewportSegments,
layer.paint,
painter.transform.zoom
);
}
function prepare$4(layer, sourceCache, painter) {
layer.hasElevatedBuckets = false;
layer.hasNonElevatedBuckets = false;
if (layer._unevaluatedLayout.getValue("line-elevation-reference") === void 0 && layer._unevaluatedLayout.getValue("line-z-offset") === void 0) {
layer.hasNonElevatedBuckets = true;
return;
}
if (sourceCache) {
const coords = sourceCache.getVisibleCoordinates();
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
if (bucket.elevationType !== "none") {
layer.hasElevatedBuckets = true;
} else {
layer.hasNonElevatedBuckets = true;
}
if (layer.hasElevatedBuckets && layer.hasNonElevatedBuckets) {
break;
}
}
}
}
function drawLine(painter, sourceCache, layer, coords) {
if (painter.renderPass !== "translucent") return;
const opacity = layer.paint.get("line-opacity");
const width = layer.paint.get("line-width");
if (opacity.constantOr(1) === 0 || width.constantOr(1) === 0) return;
const constantEmissiveStrength = layer.paint.get("line-emissive-strength").isConstant();
index$1.assert(painter.emissiveMode !== "constant" || constantEmissiveStrength);
const emissiveStrengthForDrapedLayers = layer.paint.get("line-emissive-strength").constantOr(0);
const occlusionOpacity = layer.paint.get("line-occlusion-opacity");
const elevationReference = layer.layout.get("line-elevation-reference");
const unitInMeters = layer.layout.get("line-width-unit") === "meters";
const elevationFromSea = elevationReference === "sea";
const terrainEnabled = !!(painter.terrain && painter.terrain.enabled);
const context = painter.context;
const gl = context.gl;
if (layer.hasElevatedBuckets && painter.transform.projection.name === "globe") return;
const crossSlope = layer.layout.get("line-cross-slope");
const hasCrossSlope = crossSlope !== void 0;
const crossSlopeHorizontal = crossSlope < 1;
const colorMode = painter.colorModeForDrapableLayerRenderPass(constantEmissiveStrength ? emissiveStrengthForDrapedLayers : null);
const isDraping = painter.terrain && painter.terrain.renderingToTexture;
const pixelRatio = isDraping ? 1 : index$1.exported$1.devicePixelRatio;
const dasharrayProperty = layer.paint.get("line-dasharray");
const dasharray = dasharrayProperty.constantOr(1);
const capProperty = layer.layout.get("line-cap");
const constantDash = dasharrayProperty.constantOr(null);
const constantCap = capProperty.constantOr(null);
const patternProperty = layer.paint.get("line-pattern");
const image = patternProperty.constantOr(1);
const patternTransition = layer.paint.get("line-pattern-cross-fade");
const constantPattern = patternProperty.constantOr(null);
const lineOpacity = layer.paint.get("line-opacity").constantOr(1);
const hasOpacity = lineOpacity !== 1;
let useStencilMaskRenderPass = !image && hasOpacity || // Only semi-transparent lines need stencil masking
painter.depthOcclusion && occlusionOpacity > 0 && occlusionOpacity < 1;
const gradient = layer.paint.get("line-gradient");
const programId = image ? "linePattern" : "line";
const definesValues = index$1.lineDefinesValues(layer);
if (isDraping && painter.terrain && painter.terrain.clipOrMaskOverlapStencilType()) {
useStencilMaskRenderPass = false;
}
let lineOpacityForOcclusion;
if (occlusionOpacity !== 0 && painter.depthOcclusion) {
const value = layer.paint._values["line-opacity"];
if (value && value.value && value.value.kind === "constant") {
lineOpacityForOcclusion = value.value;
} else {
index$1.warnOnce(`Occlusion opacity for layer ${layer.id} is supported only when line-opacity isn't data-driven.`);
}
}
if (width.value.kind !== "constant" && width.value.isLineProgressConstant === false) {
definesValues.push("VARIABLE_LINE_WIDTH");
}
if (isDraping) {
if (painter.emissiveMode === "dual-source-blending" && !constantEmissiveStrength) {
definesValues.push("DUAL_SOURCE_BLENDING");
} else if (painter.emissiveMode === "mrt-fallback") {
definesValues.push("USE_MRT1");
}
}
const renderTiles = (coords2, baseDefines, depthMode2, stencilMode3D, elevated, firstPass) => {
for (const coord of coords2) {
const tile = sourceCache.getTile(coord);
if (image && !tile.patternsLoaded()) continue;
const bucket = tile.getBucket(layer);
if (!bucket) continue;
if (bucket.elevationType !== "none" && !elevated || bucket.elevationType === "none" && elevated) continue;
painter.prepareDrawTile();
const defines = [...baseDefines];
const renderElevatedRoads = bucket.elevationType === "road";
const shadowRenderer = painter.shadowRenderer;
const renderWithShadows = renderElevatedRoads && !!shadowRenderer && shadowRenderer.enabled;
let groundShadowFactor = [0, 0, 0];
if (renderWithShadows) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
defines.push("RENDER_SHADOWS", "DEPTH_TEXTURE", "NORMAL_OFFSET");
}
const programConfiguration = bucket.programConfigurations.get(layer.id);
let transitionableConstantPattern = false;
if (constantPattern && tile.imageAtlas) {
const pattern = index$1.ResolvedImage.from(constantPattern);
const primaryPatternImage = pattern.getPrimary().scaleSelf(pixelRatio).toString();
const primaryPosTo = tile.imageAtlas.patternPositions.get(primaryPatternImage);
const secondaryPatternImageVariant = pattern.getSecondary();
const secondaryPosTo = secondaryPatternImageVariant ? tile.imageAtlas.patternPositions.get(secondaryPatternImageVariant.scaleSelf(pixelRatio).toString()) : null;
transitionableConstantPattern = !!primaryPosTo && !!secondaryPosTo;
if (primaryPosTo) programConfiguration.setConstantPatternPositions(primaryPosTo, secondaryPosTo);
}
if (patternTransition > 0 && (transitionableConstantPattern || !!programConfiguration.getPatternTransitionVertexBuffer("line-pattern"))) {
defines.push("LINE_PATTERN_TRANSITION");
}
const affectedByFog = painter.isTileAffectedByFog(coord);
const program = painter.getOrCreateProgram(programId, { config: programConfiguration, defines, overrideFog: affectedByFog });
if (!image && constantDash && constantCap && tile.lineAtlas) {
const posTo = tile.lineAtlas.getDash(constantDash, constantCap);
if (posTo) programConfiguration.setConstantPatternPositions(posTo);
}
if (renderWithShadows) {
shadowRenderer.setupShadows(tile.tileID.toUnwrapped(), program, "vector-tile");
}
let [trimStart, trimEnd] = layer.paint.get("line-trim-offset");
if (constantCap === "round" || constantCap === "square") {
const fakeOffsetShift = 1;
if (trimStart !== trimEnd) {
if (trimStart === 0) {
trimStart -= fakeOffsetShift;
}
if (trimEnd === 1) {
trimEnd += fakeOffsetShift;
}
}
}
const matrix = isDraping ? coord.projMatrix : null;
const lineWidthScale = unitInMeters ? 1 / bucket.tileToMeter / index$1.pixelsToTileUnits(tile, 1, painter.transform.zoom) : 1;
const lineFloorWidthScale = unitInMeters ? 1 / bucket.tileToMeter / index$1.pixelsToTileUnits(tile, 1, Math.floor(painter.transform.zoom)) : 1;
const uniformValues = image ? index$1.linePatternUniformValues(painter, tile, layer, matrix, pixelRatio, lineWidthScale, lineFloorWidthScale, [trimStart, trimEnd], groundShadowFactor, patternTransition) : index$1.lineUniformValues(painter, tile, layer, matrix, bucket.lineClipsArray.length, pixelRatio, lineWidthScale, lineFloorWidthScale, [trimStart, trimEnd], groundShadowFactor);
if (gradient) {
const layerGradient = bucket.gradients[layer.id];
let gradientTexture = layerGradient.texture;
if (layer.gradientVersion !== layerGradient.version) {
let textureResolution = 256;
if (layer.stepInterpolant) {
const sourceMaxZoom = sourceCache.getSource().maxzoom;
const potentialOverzoom = coord.canonical.z === sourceMaxZoom ? Math.ceil(1 << painter.transform.maxZoom - coord.canonical.z) : 1;
const lineLength = bucket.maxLineLength / index$1.EXTENT;
const maxTilePixelSize = 1024;
const maxTextureCoverage = lineLength * maxTilePixelSize * potentialOverzoom;
textureResolution = index$1.clamp(index$1.nextPowerOfTwo(maxTextureCoverage), 256, context.maxTextureSize);
}
layerGradient.gradient = index$1.renderColorRamp({
expression: layer.gradientExpression(),
evaluationKey: "lineProgress",
resolution: textureResolution,
image: layerGradient.gradient || void 0,
clips: bucket.lineClipsArray
});
if (layerGradient.texture) {
layerGradient.texture.update(layerGradient.gradient);
} else {
layerGradient.texture = new index$1.Texture(context, layerGradient.gradient, gl.RGBA8);
}
layerGradient.version = layer.gradientVersion;
gradientTexture = layerGradient.texture;
}
context.activeTexture.set(gl.TEXTURE1);
gradientTexture.bind(layer.stepInterpolant ? gl.NEAREST : gl.LINEAR, gl.CLAMP_TO_EDGE);
}
if (dasharray) {
context.activeTexture.set(gl.TEXTURE0);
if (tile.lineAtlasTexture) {
tile.lineAtlasTexture.bind(gl.LINEAR, gl.REPEAT);
}
programConfiguration.updatePaintBuffers();
}
if (image) {
context.activeTexture.set(gl.TEXTURE0);
if (tile.imageAtlasTexture) {
tile.imageAtlasTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
programConfiguration.updatePaintBuffers();
}
if (elevated && !elevationFromSea) {
index$1.assert(painter.terrain);
painter.terrain.setupElevationDraw(tile, program);
}
painter.uploadCommonUniforms(context, program, coord.toUnwrapped());
const renderLine = (stencilMode) => {
if (lineOpacityForOcclusion != null) {
lineOpacityForOcclusion.value = lineOpacity * occlusionOpacity;
}
program.draw(
painter,
gl.TRIANGLES,
depthMode2,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
bucket.layoutVertexBuffer,
bucket.indexBuffer,
bucket.segments,
layer.paint,
painter.transform.zoom,
programConfiguration,
[bucket.layoutVertexBuffer2, bucket.patternVertexBuffer, bucket.zOffsetVertexBuffer]
);
if (lineOpacityForOcclusion != null) {
lineOpacityForOcclusion.value = lineOpacity;
}
};
if (useStencilMaskRenderPass && !elevated) {
const stencilId = painter.stencilModeForClipping(coord).ref;
const needsClearing = stencilId === 0 && isDraping;
if (needsClearing) {
context.clear({ stencil: 0 });
}
const stencilFunc = { func: gl.EQUAL, mask: 255 };
uniformValues["u_alpha_discard_threshold"] = 0.8;
renderLine(new StencilMode(stencilFunc, stencilId, 255, gl.KEEP, gl.KEEP, gl.INVERT));
uniformValues["u_alpha_discard_threshold"] = 0;
renderLine(new StencilMode(stencilFunc, stencilId, 255, gl.KEEP, gl.KEEP, gl.KEEP));
} else {
if (useStencilMaskRenderPass && elevated && firstPass) {
uniformValues["u_alpha_discard_threshold"] = 0.8;
} else {
uniformValues["u_alpha_discard_threshold"] = 0;
}
renderLine(elevated ? stencilMode3D : painter.stencilModeForClipping(coord));
}
}
};
let depthMode = painter.depthModeForSublayer(0, DepthMode.ReadOnly);
const depthModeFor3D = new DepthMode(painter.depthOcclusion ? gl.GREATER : gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
if (layer.hasNonElevatedBuckets) {
const terrainEnabledImmediateMode = !isDraping && painter.terrain;
if (occlusionOpacity !== 0 && terrainEnabledImmediateMode) {
index$1.warnOnce(`Occlusion opacity for layer ${layer.id} is supported on terrain only if the layer has line-z-offset enabled.`);
} else {
if (!terrainEnabledImmediateMode) {
const stencilMode3D = StencilMode.disabled;
renderTiles(coords, definesValues, depthMode, stencilMode3D, false, true);
} else {
index$1.warnOnce(`Cannot render non-elevated lines in immediate mode when terrain is enabled. Layer: ${layer.id}.`);
}
}
}
if (layer.hasElevatedBuckets) {
if (elevationReference === "hd-road-markup") {
if (!terrainEnabled) {
depthMode = depthModeFor3D;
definesValues.push("ELEVATED_ROADS");
}
} else {
definesValues.push("ELEVATED");
depthMode = depthModeFor3D;
if (hasCrossSlope) {
definesValues.push(crossSlopeHorizontal ? "CROSS_SLOPE_HORIZONTAL" : "CROSS_SLOPE_VERTICAL");
}
if (elevationFromSea) {
definesValues.push("ELEVATION_REFERENCE_SEA");
}
}
const stencilMode3D = useStencilMaskRenderPass ? painter.stencilModeFor3D() : StencilMode.disabled;
painter.forceTerrainMode = true;
renderTiles(coords, definesValues, depthMode, stencilMode3D, true, true);
if (useStencilMaskRenderPass) {
renderTiles(coords, definesValues, depthMode, stencilMode3D, true, false);
}
painter.forceTerrainMode = false;
}
if (useStencilMaskRenderPass) {
painter.resetStencilClippingMasks();
if (isDraping) {
context.clear({ stencil: 0 });
}
}
if (occlusionOpacity !== 0 && !painter.depthOcclusion && !isDraping) {
painter.layersWithOcclusionOpacity.push(painter.currentLayer);
}
}
function drawFill(painter, sourceCache, layer, coords) {
const color = layer.paint.get("fill-color");
const opacity = layer.paint.get("fill-opacity");
if (opacity.constantOr(1) === 0) {
return;
}
const emissiveStrength = layer.paint.get("fill-emissive-strength");
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const pattern = layer.paint.get("fill-pattern");
const pass = painter.opaquePassEnabledForLayer() && (!pattern.constantOr(1) && color.constantOr(index$1.Color.transparent).a === 1 && opacity.constantOr(0) === 1) ? "opaque" : "translucent";
let elevationType = "none";
if (layer.layout.get("fill-elevation-reference") !== "none") {
elevationType = "road";
} else if (layer.paint.get("fill-z-offset").constantOr(1) !== 0) {
elevationType = "offset";
}
const terrainEnabled = !!(painter.terrain && painter.terrain.enabled);
const drawFillParams = {
painter,
sourceCache,
layer,
coords,
colorMode,
elevationType,
terrainEnabled,
pass
};
if (painter.renderPass === "shadow") {
if (painter.shadowRenderer && elevationType === "road" && !terrainEnabled) {
drawShadows(drawFillParams);
}
return;
}
const mrt = painter.emissiveMode === "mrt-fallback";
if (elevationType === "offset") {
drawFillTiles(drawFillParams, false, mrt, painter.stencilModeFor3D());
return;
}
drawFillTiles(drawFillParams, false, mrt);
if (elevationType === "road") {
const roadElevationActive = !terrainEnabled && painter.renderPass === "translucent";
if (roadElevationActive) {
drawDepthPrepass(painter, sourceCache, layer, coords, "geometry");
}
drawFillTiles(drawFillParams, true, mrt, StencilMode.disabled);
if (roadElevationActive) {
drawElevatedStructures(drawFillParams);
}
}
}
function computeCameraPositionInTile(id, cameraMercPos) {
const tiles = 1 << id.canonical.z;
const x = (cameraMercPos.x * tiles - id.canonical.x - id.wrap * tiles) * index$1.EXTENT;
const y = (cameraMercPos.y * tiles - id.canonical.y) * index$1.EXTENT;
const z = index$1.altitudeFromMercatorZ(cameraMercPos.z, cameraMercPos.y);
return index$1.fromValues$2(x, y, z);
}
function computeDepthBias(tr) {
let bias = 0.01;
if (tr.isOrthographic) {
const mixValue = tr.pitch >= OrthographicPitchTranstionValue ? 1 : tr.pitch / OrthographicPitchTranstionValue;
bias = index$1.number(1e-4, bias, index$1.easeIn(mixValue));
}
return 2 * bias;
}
function drawDepthPrepass(painter, sourceCache, layer, coords, pass) {
if (!layer.layout || layer.layout.get("fill-elevation-reference") === "none" || layer.paint.get("fill-opacity").constantOr(1) === 0) return;
const gl = painter.context.gl;
index$1.assert(!(painter.terrain && painter.terrain.enabled));
const depthModeFor3D = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
const depthModeReset = new DepthMode(painter.context.gl.GREATER, DepthMode.ReadWrite, painter.depthRangeFor3D);
const depthBias = computeDepthBias(painter.transform);
const cameraMercPos = painter.transform.getFreeCameraOptions().position;
const programName = "elevatedStructuresDepthReconstruct";
const depthReconstructProgram = painter.getOrCreateProgram(programName, { defines: ["DEPTH_RECONSTRUCTION"] });
const depthGeometryProgram = painter.getOrCreateProgram(programName);
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const elevatedStructures = bucket.elevatedStructures;
if (!elevatedStructures) {
continue;
}
const heightRange = bucket.elevationBufferData.heightRange;
const unwrappedTileID = coord.toUnwrapped();
const cameraTilePos = computeCameraPositionInTile(unwrappedTileID, cameraMercPos);
const tileMatrix = painter.translatePosMatrix(
coord.projMatrix,
tile,
layer.paint.get("fill-translate"),
layer.paint.get("fill-translate-anchor")
);
let uniformValues;
let depthMode;
let segments;
let program;
if (pass === "initialize") {
const heightMargin = 1;
if (!heightRange || heightRange.min >= heightMargin || elevatedStructures.depthSegments.segments[0].primitiveLength === 0) continue;
uniformValues = elevatedStructuresDepthReconstructUniformValues(tileMatrix, cameraTilePos, depthBias, 1, 0);
depthMode = depthModeFor3D;
segments = elevatedStructures.depthSegments;
program = depthReconstructProgram;
} else if (pass === "reset") {
if (!heightRange || heightRange.min >= 0 || elevatedStructures.maskSegments.segments[0].primitiveLength === 0) continue;
uniformValues = elevatedStructuresDepthReconstructUniformValues(tileMatrix, cameraTilePos, 0, 0, 1);
depthMode = depthModeReset;
segments = elevatedStructures.maskSegments;
program = depthReconstructProgram;
} else if (pass === "geometry") {
if (elevatedStructures.depthSegments.segments[0].primitiveLength === 0) continue;
uniformValues = elevatedStructuresDepthReconstructUniformValues(tileMatrix, cameraTilePos, depthBias, 1, 0);
depthMode = depthModeFor3D;
segments = elevatedStructures.depthSegments;
program = depthGeometryProgram;
}
index$1.assert(uniformValues && depthMode && segments && program);
index$1.assert(elevatedStructures.vertexBuffer && elevatedStructures.indexBuffer);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
ColorMode.disabled,
CullFaceMode.disabled,
uniformValues,
layer.id,
elevatedStructures.vertexBuffer,
elevatedStructures.indexBuffer,
segments,
layer.paint,
painter.transform.zoom
);
}
}
function drawGroundShadowMask(painter, sourceCache, layer, coords) {
if (!layer.layout || layer.layout.get("fill-elevation-reference") === "none" || layer.paint.get("fill-opacity").constantOr(1) === 0) return;
index$1.assert(!(painter.terrain && painter.terrain.enabled));
const gl = painter.context.gl;
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const stencilMode = new StencilMode({ func: gl.ALWAYS, mask: 255 }, 255, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
const cameraMercPos = painter.transform.getFreeCameraOptions().position;
const program = painter.getOrCreateProgram("elevatedStructuresDepthReconstruct");
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const elevatedStructures = bucket.elevatedStructures;
if (!elevatedStructures || elevatedStructures.depthSegments.segments[0].primitiveLength === 0) {
continue;
}
const unwrappedTileID = coord.toUnwrapped();
const cameraTilePos = computeCameraPositionInTile(unwrappedTileID, cameraMercPos);
const tileMatrix = painter.translatePosMatrix(
coord.projMatrix,
tile,
layer.paint.get("fill-translate"),
layer.paint.get("fill-translate-anchor")
);
const uniformValues = elevatedStructuresDepthReconstructUniformValues(tileMatrix, cameraTilePos, 0, 1, 0);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
ColorMode.disabled,
CullFaceMode.disabled,
uniformValues,
layer.id,
elevatedStructures.vertexBuffer,
elevatedStructures.indexBuffer,
elevatedStructures.depthSegments,
layer.paint,
painter.transform.zoom
);
}
}
function drawElevatedStructures(params) {
const { painter, sourceCache, layer, coords, colorMode } = params;
const gl = painter.context.gl;
const programName = "elevatedStructures";
const shadowRenderer = params.painter.shadowRenderer;
const renderWithShadows = !!shadowRenderer && shadowRenderer.enabled;
const depthMode = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
let groundShadowFactor = [0, 0, 0];
if (renderWithShadows) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
}
const draw = (drawBridges) => {
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const elevatedStructures = bucket.elevatedStructures;
if (!elevatedStructures) continue;
let renderableSegments;
let programConfiguration;
if (drawBridges) {
renderableSegments = elevatedStructures.renderableBridgeSegments;
programConfiguration = elevatedStructures.bridgeProgramConfigurations.get(layer.id);
} else {
renderableSegments = elevatedStructures.renderableTunnelSegments;
programConfiguration = elevatedStructures.tunnelProgramConfigurations.get(layer.id);
}
if (!renderableSegments || renderableSegments.segments[0].primitiveLength === 0) continue;
index$1.assert(elevatedStructures.vertexBuffer && elevatedStructures.vertexBufferNormal && elevatedStructures.indexBuffer);
programConfiguration.updatePaintBuffers();
painter.prepareDrawTile();
const affectedByFog = painter.isTileAffectedByFog(coord);
const dynamicDefines = [];
if (renderWithShadows) {
dynamicDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE", "NORMAL_OFFSET");
}
const program = painter.getOrCreateProgram(programName, { config: programConfiguration, overrideFog: affectedByFog, defines: dynamicDefines });
const tileMatrix = painter.translatePosMatrix(
coord.projMatrix,
tile,
layer.paint.get("fill-translate"),
layer.paint.get("fill-translate-anchor")
);
if (renderWithShadows) {
shadowRenderer.setupShadows(tile.tileID.toUnwrapped(), program, "vector-tile");
}
const uniformValues = elevatedStructuresUniformValues(tileMatrix, groundShadowFactor);
painter.uploadCommonUniforms(painter.context, program, coord.toUnwrapped());
program.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.backCCW,
uniformValues,
layer.id,
elevatedStructures.vertexBuffer,
elevatedStructures.indexBuffer,
renderableSegments,
layer.paint,
painter.transform.zoom,
programConfiguration,
[elevatedStructures.vertexBufferNormal]
);
}
};
draw(true);
draw(false);
}
function drawFillTiles(params, elevatedGeometry, multipleRenderTargets, stencilModeOverride) {
const { painter, sourceCache, layer, coords, colorMode, elevationType, terrainEnabled, pass } = params;
const gl = painter.context.gl;
const patternProperty = layer.paint.get("fill-pattern");
const patternTransition = layer.paint.get("fill-pattern-cross-fade");
const constantPattern = patternProperty.constantOr(null);
let activeElevationType = elevationType;
if (elevationType === "road" && (!elevatedGeometry || terrainEnabled)) {
activeElevationType = "none";
}
const renderElevatedRoads = activeElevationType === "road";
const shadowRenderer = params.painter.shadowRenderer;
const renderWithShadows = renderElevatedRoads && !!shadowRenderer && shadowRenderer.enabled;
const depthModeFor3D = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
let groundShadowFactor = [0, 0, 0];
if (renderWithShadows) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
}
const image = patternProperty && patternProperty.constantOr(1);
const isDraping = painter.terrain && painter.terrain.renderingToTexture;
const draw = (depthMode, isOutline) => {
let programName;
let uniformValues;
let drawMode, indexBuffer, segments;
if (!isOutline) {
programName = image ? "fillPattern" : "fill";
drawMode = gl.TRIANGLES;
} else {
programName = image && !layer.getPaintProperty("fill-outline-color") ? "fillOutlinePattern" : "fillOutline";
drawMode = gl.LINES;
}
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
if (image && !tile.patternsLoaded()) continue;
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const bufferData = elevatedGeometry ? bucket.elevationBufferData : bucket.bufferData;
if (bufferData.isEmpty()) continue;
painter.prepareDrawTile();
const programConfiguration = bufferData.programConfigurations.get(layer.id);
const affectedByFog = painter.isTileAffectedByFog(coord);
const dynamicDefines = [];
const dynamicBuffers = [];
if (renderElevatedRoads) {
dynamicDefines.push("ELEVATED_ROADS");
dynamicBuffers.push(bufferData.elevatedLayoutVertexBuffer);
}
if (renderWithShadows) {
dynamicDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE", "NORMAL_OFFSET");
}
if (isDraping && multipleRenderTargets) {
dynamicDefines.push("USE_MRT1");
}
if (image) {
painter.context.activeTexture.set(gl.TEXTURE0);
if (tile.imageAtlasTexture) {
tile.imageAtlasTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
programConfiguration.updatePaintBuffers();
}
let transitionableConstantPattern = false;
if (constantPattern && tile.imageAtlas) {
const atlas = tile.imageAtlas;
const pattern = index$1.ResolvedImage.from(constantPattern);
const primaryPatternImage = pattern.getPrimary().scaleSelf(index$1.exported$1.devicePixelRatio).toString();
const secondaryPatternImageVariant = pattern.getSecondary();
const primaryPosTo = atlas.patternPositions.get(primaryPatternImage);
const secondaryPosTo = secondaryPatternImageVariant ? atlas.patternPositions.get(secondaryPatternImageVariant.scaleSelf(index$1.exported$1.devicePixelRatio).toString()) : null;
transitionableConstantPattern = !!primaryPosTo && !!secondaryPosTo;
if (primaryPosTo) programConfiguration.setConstantPatternPositions(primaryPosTo, secondaryPosTo);
}
if (patternTransition > 0 && (transitionableConstantPattern || !!programConfiguration.getPatternTransitionVertexBuffer("fill-pattern"))) {
dynamicDefines.push("FILL_PATTERN_TRANSITION");
}
const program = painter.getOrCreateProgram(programName, { config: programConfiguration, overrideFog: affectedByFog, defines: dynamicDefines });
const tileMatrix = painter.translatePosMatrix(
coord.projMatrix,
tile,
layer.paint.get("fill-translate"),
layer.paint.get("fill-translate-anchor")
);
if (renderWithShadows) {
shadowRenderer.setupShadows(tile.tileID.toUnwrapped(), program, "vector-tile");
}
const emissiveStrength = layer.paint.get("fill-emissive-strength");
if (!isOutline) {
indexBuffer = bufferData.indexBuffer;
segments = bufferData.triangleSegments;
uniformValues = image ? fillPatternUniformValues(tileMatrix, emissiveStrength, painter, tile, groundShadowFactor, patternTransition) : fillUniformValues(tileMatrix, emissiveStrength, groundShadowFactor);
} else {
indexBuffer = bufferData.lineIndexBuffer;
segments = bufferData.lineSegments;
const drawingBufferSize = painter.terrain && painter.terrain.renderingToTexture ? painter.terrain.drapeBufferSize : [gl.drawingBufferWidth, gl.drawingBufferHeight];
uniformValues = programName === "fillOutlinePattern" && image ? fillOutlinePatternUniformValues(tileMatrix, emissiveStrength, painter, tile, drawingBufferSize, groundShadowFactor, patternTransition) : fillOutlineUniformValues(tileMatrix, emissiveStrength, drawingBufferSize, groundShadowFactor);
}
painter.uploadCommonUniforms(painter.context, program, coord.toUnwrapped());
let activeDepthMode = depthMode;
if (elevationType === "road" && !terrainEnabled || elevationType === "offset") {
activeDepthMode = depthModeFor3D;
}
program.draw(
painter,
drawMode,
activeDepthMode,
stencilModeOverride ? stencilModeOverride : painter.stencilModeForClipping(coord),
colorMode,
CullFaceMode.disabled,
uniformValues,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
layer.id,
bufferData.layoutVertexBuffer,
indexBuffer,
segments,
layer.paint,
painter.transform.zoom,
programConfiguration,
dynamicBuffers
);
}
};
if (painter.renderPass === pass) {
const depthMode = painter.depthModeForSublayer(1, painter.renderPass === "opaque" ? DepthMode.ReadWrite : DepthMode.ReadOnly);
draw(depthMode, false);
}
if (activeElevationType === "none" && painter.renderPass === "translucent" && layer.paint.get("fill-antialias")) {
const depthMode = painter.depthModeForSublayer(layer.getPaintProperty("fill-outline-color") ? 2 : 0, DepthMode.ReadOnly);
draw(depthMode, true);
}
}
function drawShadows(params) {
index$1.assert(!params.terrainEnabled);
index$1.assert(params.painter.renderPass === "shadow");
index$1.assert(params.painter.shadowRenderer);
const { painter, sourceCache, layer, coords } = params;
const gl = painter.context.gl;
const shadowRenderer = params.painter.shadowRenderer;
const programName = "elevatedStructuresDepth";
for (const coord of coords) {
const tile = sourceCache.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
const elevatedStructures = bucket.elevatedStructures;
if (!elevatedStructures) {
continue;
}
if (!elevatedStructures.shadowCasterSegments || elevatedStructures.shadowCasterSegments.segments[0].primitiveLength === 0) {
continue;
}
index$1.assert(elevatedStructures.vertexBuffer && elevatedStructures.indexBuffer);
painter.prepareDrawTile();
const programConfiguration = bucket.bufferData.programConfigurations.get(layer.id);
const affectedByFog = painter.isTileAffectedByFog(coord);
const program = painter.getOrCreateProgram(programName, { config: programConfiguration, overrideFog: affectedByFog });
const tileMatrix = shadowRenderer.calculateShadowPassMatrixFromTile(tile.tileID.toUnwrapped());
painter.uploadCommonUniforms(painter.context, program, coord.toUnwrapped());
const uniformValues = elevatedStructuresDepthUniformValues(tileMatrix, 0);
program.draw(
painter,
gl.TRIANGLES,
shadowRenderer.getShadowPassDepthMode(),
StencilMode.disabled,
shadowRenderer.getShadowPassColorMode(),
CullFaceMode.disabled,
uniformValues,
layer.id,
elevatedStructures.vertexBuffer,
elevatedStructures.indexBuffer,
elevatedStructures.shadowCasterSegments,
layer.paint,
painter.transform.zoom,
programConfiguration
);
}
}
function draw$2(painter, source, layer, coords) {
const perfStartTime = index$1.PerformanceUtils.now();
const opacity = layer.paint.get("fill-extrusion-opacity");
const context = painter.context;
const gl = context.gl;
const terrain = painter.terrain;
const rtt = terrain && terrain.renderingToTexture;
if (opacity === 0) {
return;
}
const mrt = painter.emissiveMode === "mrt-fallback";
const conflateLayer = painter.conflationActive && painter.style.isLayerClipped(layer, source.getSource());
const layerIdx = painter.style.order.indexOf(layer.fqid);
if (conflateLayer) {
updateReplacement(painter, source, layer, coords, layerIdx);
}
if (terrain || conflateLayer) {
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
updateBorders(painter.context, source, coord, bucket, layer, terrain, conflateLayer);
}
}
if (painter.renderPass === "shadow" && painter.shadowRenderer) {
const shadowRenderer = painter.shadowRenderer;
if (terrain) {
const noShadowCutoff = 0.65;
if (opacity < noShadowCutoff) {
const expression = layer._transitionablePaint._values["fill-extrusion-opacity"].value.expression;
if (expression instanceof index$1.ZoomDependentExpression) {
return;
}
}
}
const depthMode = shadowRenderer.getShadowPassDepthMode();
const colorMode = shadowRenderer.getShadowPassColorMode();
drawExtrusionTiles(painter, source, layer, coords, depthMode, StencilMode.disabled, colorMode, conflateLayer);
} else if (painter.renderPass === "translucent") {
const noPattern = !layer.paint.get("fill-extrusion-pattern").constantOr(1);
const color = layer.paint.get("fill-extrusion-color").constantOr(index$1.Color.white);
if (!rtt && color.a !== 0) {
const depthMode = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
if (opacity === 1 && noPattern) {
drawExtrusionTiles(painter, source, layer, coords, depthMode, StencilMode.disabled, ColorMode.unblended, conflateLayer);
} else {
drawExtrusionTiles(
painter,
source,
layer,
coords,
depthMode,
StencilMode.disabled,
ColorMode.disabled,
conflateLayer
);
drawExtrusionTiles(
painter,
source,
layer,
coords,
depthMode,
painter.stencilModeFor3D(),
painter.colorModeForRenderPass(),
conflateLayer
);
painter.resetStencilClippingMasks();
}
}
const lighting3DMode = painter.style.enable3dLights();
const noTerrain = !terrain;
const noGlobe = painter.transform.projection.name !== "globe";
const immediateMode = noTerrain && noGlobe;
if (lighting3DMode && noPattern && (immediateMode || rtt)) {
index$1.assert(immediateMode ? !rtt : !!rtt);
const opacity2 = layer.paint.get("fill-extrusion-opacity");
const aoIntensity = layer.paint.get("fill-extrusion-ambient-occlusion-intensity");
const aoRadius = layer.paint.get("fill-extrusion-ambient-occlusion-ground-radius");
const floodLightIntensity = layer.paint.get("fill-extrusion-flood-light-intensity");
const floodLightIgnoreLut = layer.paint.get("fill-extrusion-flood-light-color-use-theme").constantOr("default") === "none";
const floodLightColor = layer.paint.get("fill-extrusion-flood-light-color").toNonPremultipliedRenderColor(floodLightIgnoreLut ? null : layer.lut).toArray01().slice(0, 3);
const aoEnabled = aoIntensity > 0 && aoRadius > 0;
const floodLightEnabled = floodLightIntensity > 0;
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const groundEffectProps = new GroundEffectProperties();
groundEffectProps.translate = layer.paint.get("fill-extrusion-translate");
groundEffectProps.translateAnchor = layer.paint.get("fill-extrusion-translate-anchor");
groundEffectProps.edgeRadius = layer.layout.get("fill-extrusion-edge-radius");
groundEffectProps.cutoffFadeRange = layer.paint.get("fill-extrusion-cutoff-fade-range");
const passImmediate = (aoPass) => {
const depthMode = painter.depthModeForSublayer(1, DepthMode.ReadOnly, gl.LEQUAL, true);
const t = aoPass ? layer.paint.get("fill-extrusion-ambient-occlusion-ground-attenuation") : layer.paint.get("fill-extrusion-flood-light-ground-attenuation");
const attenuation = lerp(0.1, 3, t);
const showOverdraw = painter._showOverdrawInspector;
if (!showOverdraw) {
const stencilSdfPass = new StencilMode({ func: gl.ALWAYS, mask: 255 }, 255, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
const colorSdfPass = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [false, false, false, true], gl.MIN);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilSdfPass, colorSdfPass, CullFaceMode.disabled, aoPass, "sdf", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, false);
}
{
const stencilColorPass = showOverdraw ? StencilMode.disabled : new StencilMode({ func: gl.EQUAL, mask: 255 }, 255, 255, gl.KEEP, gl.DECR, gl.DECR);
const colorColorPass = showOverdraw ? painter.colorModeForRenderPass() : new ColorMode([gl.ONE_MINUS_DST_ALPHA, gl.DST_ALPHA, gl.ONE, gl.ONE], index$1.Color.transparent, [true, true, true, true]);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilColorPass, colorColorPass, CullFaceMode.disabled, aoPass, "color", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, false);
}
};
if (rtt) {
const createFramebufferCopyTexture = () => {
const width = terrain.drapeBufferSize[0];
const height = terrain.drapeBufferSize[1];
let framebufferCopyTexture = terrain.framebufferCopyTexture;
if (!framebufferCopyTexture || framebufferCopyTexture && (framebufferCopyTexture.size[0] !== width || framebufferCopyTexture.size[1] !== height)) {
if (framebufferCopyTexture) framebufferCopyTexture.destroy();
framebufferCopyTexture = terrain.framebufferCopyTexture = new index$1.Texture(
context,
new index$1.RGBAImage({ width, height }),
gl.RGBA8
);
}
framebufferCopyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
return framebufferCopyTexture;
};
const passDraped = (aoPass, renderNeighbors, framebufferCopyTexture) => {
const depthMode = painter.depthModeForSublayer(1, DepthMode.ReadOnly, gl.LEQUAL, false);
const t = aoPass ? layer.paint.get("fill-extrusion-ambient-occlusion-ground-attenuation") : layer.paint.get("fill-extrusion-flood-light-ground-attenuation");
const attenuation = lerp(0.1, 3, t);
{
const colorMode = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [false, false, false, true]);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, StencilMode.disabled, colorMode, CullFaceMode.disabled, aoPass, "clear", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, renderNeighbors);
}
{
const stencilSdfPass = new StencilMode({ func: gl.ALWAYS, mask: 255 }, 255, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
const colorSdfPass = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [false, false, false, true], gl.MIN);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilSdfPass, colorSdfPass, CullFaceMode.disabled, aoPass, "sdf", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, renderNeighbors);
}
if (mrt && !aoPass) {
framebufferCopyTexture = createFramebufferCopyTexture();
}
index$1.assert(framebufferCopyTexture);
{
const srcColorFactor = aoPass ? gl.ZERO : gl.ONE_MINUS_DST_ALPHA;
const stencilColorPass = new StencilMode({ func: gl.EQUAL, mask: 255 }, 255, 255, gl.KEEP, gl.DECR, gl.DECR);
const colorColorPass = new ColorMode([srcColorFactor, gl.DST_ALPHA, gl.ONE_MINUS_DST_ALPHA, gl.ZERO], index$1.Color.transparent, [true, true, true, true]);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilColorPass, colorColorPass, CullFaceMode.disabled, aoPass, "color", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, renderNeighbors);
}
if (!mrt || aoPass) {
const dstAlphaFactor = aoPass ? gl.ZERO : gl.ONE;
const blendEquation = aoPass ? gl.FUNC_ADD : gl.MAX;
const colorMode = new ColorMode([gl.ONE, gl.ONE, gl.ONE, dstAlphaFactor], index$1.Color.transparent, [false, false, false, true], blendEquation);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, StencilMode.disabled, colorMode, CullFaceMode.disabled, aoPass, "clear", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, renderNeighbors, framebufferCopyTexture);
} else {
gl.drawBuffers([gl.NONE, gl.COLOR_ATTACHMENT1]);
const stencilColorPass = new StencilMode({ func: gl.EQUAL, mask: 255 }, 254, 255, gl.KEEP, gl.DECR, gl.DECR);
const colorColorPass = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [true, false, false, false], gl.MAX);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilColorPass, colorColorPass, CullFaceMode.disabled, aoPass, "emissive", opacity2, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, renderNeighbors, framebufferCopyTexture);
gl.drawBuffers([gl.COLOR_ATTACHMENT0]);
}
};
if (aoEnabled || floodLightEnabled) {
painter.prepareDrawTile();
let framebufferCopyTexture;
if (!mrt || aoEnabled) {
framebufferCopyTexture = createFramebufferCopyTexture();
}
if (aoEnabled) {
passDraped(true, false, framebufferCopyTexture);
}
if (floodLightEnabled) {
passDraped(false, true, framebufferCopyTexture);
}
}
} else {
if (aoEnabled) {
passImmediate(true);
}
if (floodLightEnabled) {
passImmediate(false);
}
if (aoEnabled || floodLightEnabled) {
painter.resetStencilClippingMasks();
}
}
}
}
index$1.PerformanceUtils.measureWithDetails(index$1.PerformanceUtils.GROUP_RENDERING, `FillExtrusion.draw(${painter.renderPass})`, "FillExtrusion", perfStartTime);
}
function drawExtrusionTiles(painter, source, layer, coords, depthMode, stencilMode, colorMode, replacementActive) {
layer.resetLayerRenderingStats(painter);
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const patternProperty = layer.paint.get("fill-extrusion-pattern");
const patternTransition = layer.paint.get("fill-extrusion-pattern-cross-fade");
const constantPattern = patternProperty.constantOr(null);
const image = patternProperty.constantOr(1);
const opacity = layer.paint.get("fill-extrusion-opacity");
const lighting3DMode = painter.style.enable3dLights();
const aoRadius = lighting3DMode && !image ? layer.paint.get("fill-extrusion-ambient-occlusion-wall-radius") : layer.paint.get("fill-extrusion-ambient-occlusion-radius");
const ao = [layer.paint.get("fill-extrusion-ambient-occlusion-intensity"), aoRadius];
const edgeRadius = layer.layout.get("fill-extrusion-edge-radius");
const zeroRoofRadius = edgeRadius > 0 && !layer.paint.get("fill-extrusion-rounded-roof");
const roofEdgeRadius = zeroRoofRadius ? 0 : edgeRadius;
const heightLift = tr.projection.name === "globe" ? index$1.fillExtrusionHeightLift() : 0;
const isGlobeProjection = tr.projection.name === "globe";
const globeToMercator = isGlobeProjection ? index$1.globeToMercatorTransition(tr.zoom) : 0;
const mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
const floodLightColorUseTheme = layer.paint.get("fill-extrusion-flood-light-color-use-theme").constantOr("default") === "none";
const floodLightColor = layer.paint.get("fill-extrusion-flood-light-color").toNonPremultipliedRenderColor(floodLightColorUseTheme ? null : layer.lut).toArray01().slice(0, 3);
const floodLightIntensity = layer.paint.get("fill-extrusion-flood-light-intensity");
const verticalScale = layer.paint.get("fill-extrusion-vertical-scale");
const wallMode = layer.paint.get("fill-extrusion-line-width").constantOr(1) !== 0;
const heightAlignment = layer.paint.get("fill-extrusion-height-alignment");
const baseAlignment = layer.paint.get("fill-extrusion-base-alignment");
const cutoffParams = getCutoffParams(painter, layer.paint.get("fill-extrusion-cutoff-fade-range"));
const baseDefines = [];
if (isGlobeProjection) {
baseDefines.push("PROJECTION_GLOBE_VIEW");
}
if (ao[0] > 0) {
baseDefines.push("FAUX_AO");
}
if (zeroRoofRadius) {
baseDefines.push("ZERO_ROOF_RADIUS");
}
if (replacementActive) {
baseDefines.push("HAS_CENTROID");
}
if (floodLightIntensity > 0) {
baseDefines.push("FLOOD_LIGHT");
}
if (cutoffParams.shouldRenderCutoff) {
baseDefines.push("RENDER_CUTOFF");
}
if (wallMode) {
baseDefines.push("RENDER_WALL_MODE");
}
let singleCascadeDefines;
const isShadowPass = painter.renderPass === "shadow";
const shadowRenderer = painter.shadowRenderer;
const drawDepth = isShadowPass && !!shadowRenderer;
const cullFaceMode = isShadowPass ? CullFaceMode.disabled : CullFaceMode.backCCW;
if (painter.shadowRenderer) painter.shadowRenderer.useNormalOffset = true;
let groundShadowFactor = [0, 0, 0];
if (shadowRenderer) {
const directionalLight = painter.style.directionalLight;
const ambientLight = painter.style.ambientLight;
if (directionalLight && ambientLight) {
groundShadowFactor = calculateGroundShadowFactor(painter.style, directionalLight, ambientLight);
}
if (!isShadowPass) {
baseDefines.push("RENDER_SHADOWS", "DEPTH_TEXTURE");
if (shadowRenderer.useNormalOffset) {
baseDefines.push("NORMAL_OFFSET");
}
}
singleCascadeDefines = baseDefines.concat(["SHADOWS_SINGLE_CASCADE"]);
}
const programName = drawDepth ? "fillExtrusionDepth" : image ? "fillExtrusionPattern" : "fillExtrusion";
const stats = layer.getLayerRenderingStats();
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || bucket.projection.name !== tr.projection.name) continue;
let singleCascade = false;
if (shadowRenderer) {
singleCascade = shadowRenderer.getMaxCascadeForTile(coord.toUnwrapped()) === 0;
}
const affectedByFog = painter.isTileAffectedByFog(coord);
const programConfiguration = bucket.programConfigurations.get(layer.id);
let transitionableConstantPattern = false;
if (constantPattern && tile.imageAtlas) {
const atlas = tile.imageAtlas;
const pattern = index$1.ResolvedImage.from(constantPattern);
const primaryPatternImage = pattern.getPrimary().scaleSelf(index$1.exported$1.devicePixelRatio).toString();
const secondaryPatternImageVariant = pattern.getSecondary();
const primaryPosTo = atlas.patternPositions.get(primaryPatternImage);
const secondaryPosTo = secondaryPatternImageVariant ? atlas.patternPositions.get(secondaryPatternImageVariant.scaleSelf(index$1.exported$1.devicePixelRatio).toString()) : null;
transitionableConstantPattern = !!primaryPosTo && !!secondaryPosTo;
if (primaryPosTo) programConfiguration.setConstantPatternPositions(primaryPosTo, secondaryPosTo);
}
if (patternTransition > 0 && (transitionableConstantPattern || !!programConfiguration.getPatternTransitionVertexBuffer("fill-extrusion-pattern"))) {
baseDefines.push("FILL_EXTRUSION_PATTERN_TRANSITION");
}
const program = painter.getOrCreateProgram(
programName,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
{ config: programConfiguration, defines: singleCascade ? singleCascadeDefines : baseDefines, overrideFog: affectedByFog }
);
if (painter.terrain) {
const terrain = painter.terrain;
terrain.setupElevationDraw(tile, program, { useMeterToDem: true });
}
if (!bucket.centroidVertexBuffer) {
const attrIndex = program.getAttributeLocation(gl, "a_centroid_pos");
if (attrIndex !== -1) gl.vertexAttrib2f(attrIndex, 0, 0);
}
if (!isShadowPass && shadowRenderer) {
shadowRenderer.setupShadows(tile.tileID.toUnwrapped(), program, "vector-tile");
}
if (image) {
painter.context.activeTexture.set(gl.TEXTURE0);
if (tile.imageAtlasTexture) {
tile.imageAtlasTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
programConfiguration.updatePaintBuffers();
}
const shouldUseVerticalGradient = layer.paint.get("fill-extrusion-vertical-gradient");
const lineWidthScale = 1 / bucket.tileToMeter;
let uniformValues;
if (isShadowPass && shadowRenderer) {
if (frustumCullShadowCaster(tile.tileID, bucket.maxHeight, painter)) {
continue;
}
const tileMatrix = shadowRenderer.calculateShadowPassMatrixFromTile(tile.tileID.toUnwrapped());
uniformValues = fillExtrusionDepthUniformValues(tileMatrix, roofEdgeRadius, lineWidthScale, verticalScale, heightAlignment, baseAlignment);
} else {
const matrix = painter.translatePosMatrix(
coord.expandedProjMatrix,
tile,
layer.paint.get("fill-extrusion-translate"),
layer.paint.get("fill-extrusion-translate-anchor")
);
const invMatrix = tr.projection.createInversionMatrix(tr, coord.canonical);
if (image) {
uniformValues = fillExtrusionPatternUniformValues(
matrix,
painter,
shouldUseVerticalGradient,
opacity,
ao,
roofEdgeRadius,
lineWidthScale,
coord,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
tile,
heightLift,
heightAlignment,
baseAlignment,
globeToMercator,
mercatorCenter,
invMatrix,
floodLightColor,
verticalScale,
patternTransition
);
} else {
uniformValues = fillExtrusionUniformValues(
matrix,
painter,
shouldUseVerticalGradient,
opacity,
ao,
roofEdgeRadius,
lineWidthScale,
coord,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
heightLift,
heightAlignment,
baseAlignment,
globeToMercator,
mercatorCenter,
invMatrix,
floodLightColor,
verticalScale,
floodLightIntensity,
groundShadowFactor
);
}
}
painter.uploadCommonUniforms(context, program, coord.toUnwrapped(), null, cutoffParams);
index$1.assert(!isGlobeProjection || bucket.layoutVertexExtBuffer);
let segments = bucket.segments;
if (tr.projection.name === "mercator" && !isShadowPass) {
segments = bucket.getVisibleSegments(tile.tileID, painter.terrain, painter.transform.getFrustum(0));
if (!segments.get().length) {
continue;
}
}
if (stats) {
if (!isShadowPass) {
for (const segment of segments.get()) {
stats.numRenderedVerticesInTransparentPass += segment.primitiveLength;
}
} else {
for (const segment of segments.get()) {
stats.numRenderedVerticesInShadowPass += segment.primitiveLength;
}
}
}
const dynamicBuffers = [];
if (painter.terrain || replacementActive) dynamicBuffers.push(bucket.centroidVertexBuffer);
if (isGlobeProjection) dynamicBuffers.push(bucket.layoutVertexExtBuffer);
if (wallMode) dynamicBuffers.push(bucket.wallVertexBuffer);
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
bucket.layoutVertexBuffer,
bucket.indexBuffer,
segments,
layer.paint,
painter.transform.zoom,
programConfiguration,
dynamicBuffers
);
}
if (painter.shadowRenderer) painter.shadowRenderer.useNormalOffset = false;
}
function updateReplacement(painter, source, layer, coords, layerIndex) {
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
bucket.updateReplacement(coord, painter.replacementSource, layerIndex);
bucket.uploadCentroid(painter.context);
}
}
class GroundEffectProperties {
constructor() {
this.translate = [0, 0];
this.translateAnchor = "map";
this.edgeRadius = 0;
this.cutoffFadeRange = 0;
}
}
function drawGroundEffect$1(props, painter, source, layer, coords, depthMode, stencilMode, colorMode, cullFaceMode, aoPass, subpass, opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, replacementActive, renderNeighbors, framebufferCopyTexture) {
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const zoom = painter.transform.zoom;
const defines = [];
const paintPropertyTranslate = props.translate;
const paintPropertyTranslateAnchor = props.translateAnchor;
const edgeRadius = props.edgeRadius;
const cutoffParams = getCutoffParams(painter, props.cutoffFadeRange);
if (subpass === "clear") {
defines.push("CLEAR_SUBPASS");
if (framebufferCopyTexture) {
defines.push("CLEAR_FROM_TEXTURE");
context.activeTexture.set(gl.TEXTURE0);
framebufferCopyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
} else if (subpass === "sdf") {
defines.push("SDF_SUBPASS");
} else if (subpass === "emissive") {
defines.push("USE_MRT1");
context.activeTexture.set(gl.TEXTURE0);
framebufferCopyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
if (replacementActive) {
defines.push("HAS_CENTROID");
}
if (cutoffParams.shouldRenderCutoff) {
defines.push("RENDER_CUTOFF");
}
const renderGroundEffectTile = (coord, groundEffect, segments, matrix, meterToTile) => {
let programDefines = defines;
if (groundEffect.groundRadiusBuffer != null) {
programDefines = defines.concat("HAS_ATTRIBUTE_a_flood_light_ground_radius");
}
const programConfiguration = groundEffect.programConfigurations.get(layer.id);
const affectedByFog = painter.isTileAffectedByFog(coord);
const program = painter.getOrCreateProgram("fillExtrusionGroundEffect", { config: programConfiguration, defines: programDefines, overrideFog: affectedByFog });
const ao = [aoIntensity, aoRadius * meterToTile];
const edgeRadiusTile = zoom >= 17 ? 0 : edgeRadius * meterToTile;
const fbSize = framebufferCopyTexture ? framebufferCopyTexture.size[0] : 0;
const uniformValues = fillExtrusionGroundEffectUniformValues(painter, matrix, opacity, aoPass, meterToTile, ao, floodLightIntensity, floodLightColor, attenuation, edgeRadiusTile, fbSize);
const dynamicBuffers = [];
if (replacementActive) dynamicBuffers.push(groundEffect.hiddenByLandmarkVertexBuffer);
if (groundEffect.groundRadiusBuffer != null) {
dynamicBuffers.push(groundEffect.groundRadiusBuffer);
}
painter.uploadCommonUniforms(context, program, coord.toUnwrapped(), null, cutoffParams);
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
groundEffect.vertexBuffer,
groundEffect.indexBuffer,
segments,
layer.paint,
zoom,
programConfiguration,
dynamicBuffers
);
};
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || bucket.projection.name !== tr.projection.name || !bucket.groundEffect || bucket.groundEffect && !bucket.groundEffect.hasData()) continue;
const groundEffect = bucket.groundEffect;
const meterToTile = 1 / bucket.tileToMeter;
{
const matrix = painter.translatePosMatrix(
coord.projMatrix,
tile,
paintPropertyTranslate,
paintPropertyTranslateAnchor
);
const segments = groundEffect.getDefaultSegment();
renderGroundEffectTile(coord, groundEffect, segments, matrix, meterToTile);
}
if (renderNeighbors) {
for (let i = 0; i < 4; i++) {
const nCoord = index$1.neighborCoord[i](coord);
const nTile = source.getTile(nCoord);
if (!nTile) continue;
const nBucket = nTile.getBucket(layer);
if (!nBucket || nBucket.projection.name !== tr.projection.name || !nBucket.groundEffect || nBucket.groundEffect && !nBucket.groundEffect.hasData()) continue;
const nGroundEffect = nBucket.groundEffect;
index$1.assert(nGroundEffect.regionSegments);
let translation, regionId;
if (i === 0) {
translation = [-index$1.EXTENT, 0, 0];
regionId = 1;
} else if (i === 1) {
translation = [index$1.EXTENT, 0, 0];
regionId = 0;
} else if (i === 2) {
translation = [0, -index$1.EXTENT, 0];
regionId = 3;
} else {
translation = [0, index$1.EXTENT, 0];
regionId = 2;
}
const segments = nGroundEffect.regionSegments[regionId];
if (!segments) continue;
const proj = new Float32Array(16);
index$1.translate(proj, coord.projMatrix, translation);
const matrix = painter.translatePosMatrix(
proj,
tile,
paintPropertyTranslate,
paintPropertyTranslateAnchor
);
renderGroundEffectTile(coord, nGroundEffect, segments, matrix, meterToTile);
}
}
}
}
function updateBorders(context, source, coord, bucket, layer, terrain, reconcileReplacementState) {
if (bucket.centroidVertexArray.length === 0) {
bucket.createCentroidsBuffer();
}
const demTile = terrain ? terrain.findDEMTileFor(coord) : null;
if ((!demTile || !demTile.dem) && !reconcileReplacementState) {
return;
}
if (terrain && demTile && demTile.dem) {
if (bucket.selfDEMTileTimestamp !== demTile.dem._timestamp) {
bucket.borderDoneWithNeighborZ = [-1, -1, -1, -1];
bucket.selfDEMTileTimestamp = demTile.dem._timestamp;
}
}
const reconcileReplacement = (centroid1, centroid2) => {
const hiddenFlag = (centroid1.flags | centroid2.flags) & index$1.HIDDEN_BY_REPLACEMENT;
if (hiddenFlag) {
centroid1.flags |= index$1.HIDDEN_BY_REPLACEMENT;
centroid2.flags |= index$1.HIDDEN_BY_REPLACEMENT;
} else {
centroid1.flags &= ~index$1.HIDDEN_BY_REPLACEMENT;
centroid2.flags &= ~index$1.HIDDEN_BY_REPLACEMENT;
}
};
const encodeHeightAsCentroid = (height) => {
return new index$1.Point(Math.ceil((height + index$1.ELEVATION_OFFSET) * index$1.ELEVATION_SCALE), 0);
};
const getLoadedBucket = (nid) => {
const minzoom = source.getSource().minzoom;
const getBucket = (key) => {
const n = source.getTileByID(key);
if (n && n.hasData()) {
return n.getBucket(layer);
}
};
const zoomLevels = [0, -1, 1];
for (const i of zoomLevels) {
const z = nid.overscaledZ + i;
if (z < minzoom) continue;
const key = nid.calculateScaledKey(nid.overscaledZ + i);
const b = getBucket(key);
if (b) {
return b;
}
}
};
const projectedToBorder = [0, 0, 0];
const xjoin = (a, b) => {
projectedToBorder[0] = Math.min(a.min.y, b.min.y);
projectedToBorder[1] = Math.max(a.max.y, b.max.y);
projectedToBorder[2] = index$1.EXTENT - b.min.x > a.max.x ? b.min.x - index$1.EXTENT : a.max.x;
return projectedToBorder;
};
const yjoin = (a, b) => {
projectedToBorder[0] = Math.min(a.min.x, b.min.x);
projectedToBorder[1] = Math.max(a.max.x, b.max.x);
projectedToBorder[2] = index$1.EXTENT - b.min.y > a.max.y ? b.min.y - index$1.EXTENT : a.max.y;
return projectedToBorder;
};
const projectCombinedSpanToBorder = [
(a, b) => xjoin(a, b),
(a, b) => xjoin(b, a),
(a, b) => yjoin(a, b),
(a, b) => yjoin(b, a)
];
const error = 3;
const flatBase = (min, max, edge, neighborDEMTile, neighborTileID, verticalEdge, maxOffsetFromBorder) => {
if (!terrain) {
return 0;
}
const points = [[verticalEdge ? edge : min, verticalEdge ? min : edge, 0], [verticalEdge ? edge : max, verticalEdge ? max : edge, 0]];
const coord3 = maxOffsetFromBorder < 0 ? index$1.EXTENT + maxOffsetFromBorder : maxOffsetFromBorder;
const thirdPoint = [verticalEdge ? coord3 : (min + max) / 2, verticalEdge ? (min + max) / 2 : coord3, 0];
if (edge === 0 && maxOffsetFromBorder < 0 || edge !== 0 && maxOffsetFromBorder > 0) {
terrain.getForTilePoints(neighborTileID, [thirdPoint], true, neighborDEMTile);
} else {
points.push(thirdPoint);
}
terrain.getForTilePoints(coord, points, true, demTile);
return Math.max(points[0][2], points[1][2], thirdPoint[2]) / terrain.exaggeration();
};
for (let i = 0; i < 4; i++) {
const a = bucket.borderFeatureIndices[i];
if (a.length === 0) {
continue;
}
const nid = index$1.neighborCoord[i](coord);
const nBucket = getLoadedBucket(nid);
if (!nBucket || !(nBucket instanceof index$1.FillExtrusionBucket)) {
continue;
}
const neighborDEMTile = terrain ? terrain.findDEMTileFor(nid) : null;
if ((!neighborDEMTile || !neighborDEMTile.dem) && !reconcileReplacementState) {
continue;
}
if (terrain && neighborDEMTile && neighborDEMTile.dem) {
if (bucket.borderDEMTileTimestamp[i] !== neighborDEMTile.dem._timestamp) {
bucket.borderDoneWithNeighborZ[i] = -1;
bucket.borderDEMTileTimestamp[i] = neighborDEMTile.dem._timestamp;
}
}
if (bucket.borderDoneWithNeighborZ[i] === nBucket.canonical.z) {
continue;
}
if (nBucket.centroidVertexArray.length === 0) {
nBucket.createCentroidsBuffer();
}
const j = (i < 2 ? 1 : 5) - i;
const updateNeighbor = nBucket.borderDoneWithNeighborZ[j] !== bucket.canonical.z;
const b = nBucket.borderFeatureIndices[j];
let ib = 0;
if (bucket.canonical.z !== nBucket.canonical.z) {
for (const index of a) {
bucket.showCentroid(bucket.featuresOnBorder[index]);
}
if (updateNeighbor) {
for (const index of b) {
nBucket.showCentroid(nBucket.featuresOnBorder[index]);
}
}
bucket.borderDoneWithNeighborZ[i] = nBucket.canonical.z;
nBucket.borderDoneWithNeighborZ[j] = bucket.canonical.z;
}
for (const ia of a) {
const partA = bucket.featuresOnBorder[ia];
const centroidA = bucket.centroidData[partA.centroidDataIndex];
index$1.assert(partA.borders);
const partABorderRange = partA.borders[i];
let partB;
while (ib < b.length) {
partB = nBucket.featuresOnBorder[b[ib]];
index$1.assert(partB.borders);
const partBBorderRange = partB.borders[j];
if (partBBorderRange[1] > partABorderRange[0] + error || // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
partBBorderRange[0] > partABorderRange[0] - error) {
break;
}
nBucket.showCentroid(partB);
ib++;
}
if (partB && ib < b.length) {
const saveIb = ib;
let count = 0;
while (true) {
index$1.assert(partB.borders);
const partBBorderRange = partB.borders[j];
if (partBBorderRange[0] > partABorderRange[1] - error) {
break;
}
count++;
if (++ib === b.length) {
break;
}
partB = nBucket.featuresOnBorder[b[ib]];
}
partB = nBucket.featuresOnBorder[b[saveIb]];
let doReconcile = false;
if (count >= 1) {
index$1.assert(partB.borders);
const partBBorderRange = partB.borders[j];
if (Math.abs(partABorderRange[0] - partBBorderRange[0]) < error && // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Math.abs(partABorderRange[1] - partBBorderRange[1]) < error) {
count = 1;
doReconcile = true;
ib = saveIb + 1;
}
} else if (count === 0) {
bucket.showCentroid(partA);
continue;
}
const centroidB = nBucket.centroidData[partB.centroidDataIndex];
if (reconcileReplacementState && doReconcile) {
reconcileReplacement(centroidA, centroidB);
}
const moreThanOneBorderIntersected = partA.intersectsCount() > 1 || partB.intersectsCount() > 1;
if (count > 1) {
ib = saveIb;
centroidA.centroidXY = centroidB.centroidXY = new index$1.Point(0, 0);
} else if (neighborDEMTile && neighborDEMTile.dem && !moreThanOneBorderIntersected) {
const span = projectCombinedSpanToBorder[i](centroidA, centroidB);
const edge = i % 2 ? index$1.EXTENT - 1 : 0;
const height = flatBase(span[0], Math.min(index$1.EXTENT - 1, span[1]), edge, neighborDEMTile, nid, i < 2, span[2]);
centroidA.centroidXY = centroidB.centroidXY = encodeHeightAsCentroid(height);
} else if (moreThanOneBorderIntersected) {
centroidA.centroidXY = centroidB.centroidXY = new index$1.Point(0, 0);
} else {
centroidA.centroidXY = bucket.encodeBorderCentroid(partA);
centroidB.centroidXY = nBucket.encodeBorderCentroid(partB);
}
bucket.writeCentroidToBuffer(centroidA);
nBucket.writeCentroidToBuffer(centroidB);
} else {
bucket.showCentroid(partA);
}
}
bucket.borderDoneWithNeighborZ[i] = nBucket.canonical.z;
nBucket.borderDoneWithNeighborZ[j] = bucket.canonical.z;
}
if (bucket.needsCentroidUpdate || !bucket.centroidVertexBuffer && bucket.centroidVertexArray.length !== 0) {
bucket.uploadCentroid(context);
}
}
const XAxis = [1, 0, 0];
const YAxis = [0, 1, 0];
const ZAxis = [0, 0, 1];
function frustumCullShadowCaster(id, bucketMaxHeight, painter) {
const transform = painter.transform;
const shadowRenderer = painter.shadowRenderer;
if (!shadowRenderer) {
return true;
}
const unwrappedId = id.toUnwrapped();
const ws = transform.tileSize * shadowRenderer._cascades[painter.currentShadowCascade].scale;
let height = bucketMaxHeight;
if (transform.elevation) {
const minmax = transform.elevation.getMinMaxForTile(id);
if (minmax) {
height += minmax.max;
}
}
const shadowDir = [...shadowRenderer.shadowDirection];
shadowDir[2] = -shadowDir[2];
const tileShadowVolume = shadowRenderer.computeSimplifiedTileShadowVolume(unwrappedId, height, ws, shadowDir);
if (!tileShadowVolume) {
return false;
}
const edges = [XAxis, YAxis, ZAxis, shadowDir, [shadowDir[0], 0, shadowDir[2]], [0, shadowDir[1], shadowDir[2]]];
const isGlobe = transform.projection.name === "globe";
const zoom = transform.scaleZoom(ws);
const cameraFrustum = index$1.Frustum.fromInvProjectionMatrix(transform.invProjMatrix, transform.worldSize, zoom, !isGlobe);
const cascadeFrustum = shadowRenderer.getCurrentCascadeFrustum();
if (cameraFrustum.intersectsPrecise(tileShadowVolume.vertices, tileShadowVolume.planes, edges) === 0) {
return true;
}
if (cascadeFrustum.intersectsPrecise(tileShadowVolume.vertices, tileShadowVolume.planes, edges) === 0) {
return true;
}
return false;
}
function drawTiles(params) {
const { painter, source, layer, coords } = params;
let defines = params.defines;
const context = painter.context;
const isShadowPass = painter.renderPass === "shadow";
const isBloomPass = painter.renderPass === "light-beam";
const shadowRenderer = painter.shadowRenderer;
const metersPerPixel = index$1.getMetersPerPixelAtLatitude(painter.transform.center.lat, painter.transform.zoom);
const cutoffParams = getCutoffParams(painter, layer.paint.get("building-cutoff-fade-range"));
if (cutoffParams.shouldRenderCutoff) {
defines = defines.concat("RENDER_CUTOFF");
}
if (params.floodLightIntensity > 0) {
defines = defines.concat("FLOOD_LIGHT");
}
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
if (shadowRenderer) {
const singleCascade = shadowRenderer.getMaxCascadeForTile(coord.toUnwrapped()) === 0;
if (singleCascade) {
defines = defines.concat("SHADOWS_SINGLE_CASCADE");
}
}
const programConfiguration = bucket.programConfigurations.get(layer.id);
let programWithFacades;
let programWithoutFacades;
let matrix = painter.translatePosMatrix(
coord.expandedProjMatrix,
tile,
[0, 0],
"map"
);
matrix = index$1.scale$3(index$1.create(), matrix, [1, 1, params.verticalScale]);
let uniformValues;
if (isShadowPass && shadowRenderer) {
const bucketMaxHeight = bucket.maxHeight * metersPerPixel;
if (frustumCullShadowCaster(tile.tileID, bucketMaxHeight, painter)) {
continue;
}
let tileShadowPassMatrix = shadowRenderer.calculateShadowPassMatrixFromTile(tile.tileID.toUnwrapped());
tileShadowPassMatrix = index$1.scale$3(index$1.create(), tileShadowPassMatrix, [1, 1, params.verticalScale]);
uniformValues = buildingDepthUniformValues(tileShadowPassMatrix);
programWithFacades = programWithoutFacades = painter.getOrCreateProgram(
"buildingDepth",
{ config: programConfiguration, defines, overrideFog: false }
);
} else if (!isBloomPass) {
const tileMatrix = painter.transform.calculatePosMatrix(coord.toUnwrapped(), painter.transform.worldSize);
index$1.scale$3(tileMatrix, tileMatrix, [1, 1, params.verticalScale]);
const normalMatrix = index$1.create();
index$1.scale$3(normalMatrix, tileMatrix, [1, -1, 1 / metersPerPixel]);
index$1.invert(normalMatrix, normalMatrix);
index$1.transpose(normalMatrix, normalMatrix);
const mercCameraPos = painter.transform.getFreeCameraOptions().position;
const tiles = 1 << coord.canonical.z;
const cameraPos = [
((mercCameraPos.x - coord.wrap) * tiles - coord.canonical.x) * index$1.EXTENT,
(mercCameraPos.y * tiles - coord.canonical.y) * index$1.EXTENT,
mercCameraPos.z * tiles * index$1.EXTENT
];
uniformValues = buildingUniformValues(matrix, normalMatrix, params.opacity, params.facadeAOIntensity, cameraPos, bucket.tileToMeter, params.facadeEmissiveChance, params.floodLightColor, params.floodLightIntensity);
programWithoutFacades = painter.getOrCreateProgram(
"building",
{ config: programConfiguration, defines, overrideFog: false }
);
if (params.depthOnly === true) {
programWithFacades = programWithoutFacades;
} else {
const facadeDefines = defines.concat(["BUILDING_FAUX_FACADE", "HAS_ATTRIBUTE_a_faux_facade_color_emissive"]);
programWithFacades = painter.getOrCreateProgram(
"building",
{ config: programConfiguration, defines: facadeDefines, overrideFog: false }
);
}
if (shadowRenderer) {
shadowRenderer.setupShadowsFromMatrix(tileMatrix, programWithoutFacades, true);
if (programWithFacades !== programWithoutFacades) {
shadowRenderer.setupShadowsFromMatrix(tileMatrix, programWithFacades, true);
}
}
} else {
programWithFacades = programWithoutFacades = painter.getOrCreateProgram(
"buildingBloom",
{ config: programConfiguration, defines, overrideFog: false }
);
uniformValues = buildingBloomUniformValues(matrix);
}
const renderBuilding = (building, program) => {
if (!isBloomPass) {
const segments = building.segmentsBucket;
let dynamicBuffers = [building.layoutNormalBuffer, building.layoutCentroidBuffer, building.layoutColorBuffer, building.layoutFloodLightDataBuffer];
if (building.layoutFacadePaintBuffer) {
dynamicBuffers = dynamicBuffers.concat([building.layoutFacadeDataBuffer, building.layoutFacadeVerticalRangeBuffer, building.layoutFacadePaintBuffer]);
}
const stencilMode = StencilMode.disabled;
program.draw(
painter,
context.gl.TRIANGLES,
params.depthMode,
stencilMode,
params.blendMode,
isShadowPass ? CullFaceMode.disabled : CullFaceMode.backCW,
uniformValues,
layer.id,
building.layoutVertexBuffer,
building.indexBuffer,
segments,
layer.paint,
painter.transform.zoom,
programConfiguration,
dynamicBuffers
);
} else {
const bloomGeometry = building.entranceBloom;
const dynamicBuffers = [bloomGeometry.layoutAttenuationBuffer, bloomGeometry.layoutColorBuffer];
program.draw(
painter,
context.gl.TRIANGLES,
params.depthMode,
StencilMode.disabled,
params.blendMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
bloomGeometry.layoutVertexBuffer,
bloomGeometry.indexBuffer,
bloomGeometry.segmentsBucket,
layer.paint,
painter.transform.zoom,
programConfiguration,
dynamicBuffers
);
}
};
painter.uploadCommonUniforms(context, programWithoutFacades, coord.toUnwrapped(), null, cutoffParams);
if (bucket.buildingWithoutFacade) {
renderBuilding(bucket.buildingWithoutFacade, programWithoutFacades);
}
if (bucket.buildingWithFacade) {
if (programWithFacades !== programWithoutFacades) {
painter.uploadCommonUniforms(context, programWithFacades, coord.toUnwrapped(), null, cutoffParams);
}
renderBuilding(bucket.buildingWithFacade, programWithFacades);
}
}
}
let drawBuildingsDebugParams = null;
class DrawBuildingsDebugParams {
constructor(painter) {
this.showNormals = false;
this.drawGroundAO = true;
this.drawShadowPass = true;
this.drawTranslucentPass = true;
DevTools.addParameter(this, "drawTranslucentPass", "Buildings", { label: "Draw Translucent Pass" }, () => {
painter.style.map.triggerRepaint();
});
DevTools.addParameter(this, "drawShadowPass", "Buildings", { label: "Draw Shadow Pass" }, () => {
painter.style.map.triggerRepaint();
});
DevTools.addParameter(this, "showNormals", "Buildings", { label: "Show normals" }, () => {
painter.style.map.triggerRepaint();
});
DevTools.addParameter(this, "drawGroundAO", "Buildings", { label: "Ground AO" }, () => {
painter.style.map.triggerRepaint();
});
}
static getOrCreateInstance(painter) {
if (!drawBuildingsDebugParams) {
drawBuildingsDebugParams = new DrawBuildingsDebugParams(painter);
}
return drawBuildingsDebugParams;
}
}
function drawGroundEffect(painter, source, layer, coords, aoPass, opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuationFactor, replacementActive, renderNeighbors) {
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const gl = painter.context.gl;
const depthMode = painter.depthModeForSublayer(1, DepthMode.ReadOnly, gl.LEQUAL, true);
const attenuation = lerp(0.1, 3, attenuationFactor);
const showOverdraw = painter._showOverdrawInspector;
const conflateLayer = replacementActive;
const groundEffectProps = new GroundEffectProperties();
if (!showOverdraw) {
const stencilSdfPass = new StencilMode({ func: gl.ALWAYS, mask: 255 }, 255, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
const colorSdfPass = new ColorMode([gl.ONE, gl.ONE, gl.ONE, gl.ONE], index$1.Color.transparent, [false, false, false, true], gl.MIN);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilSdfPass, colorSdfPass, CullFaceMode.disabled, aoPass, "sdf", opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, false);
}
{
const stencilColorPass = showOverdraw ? StencilMode.disabled : new StencilMode({ func: gl.EQUAL, mask: 255 }, 255, 255, gl.KEEP, gl.DECR, gl.DECR);
const colorColorPass = showOverdraw ? painter.colorModeForRenderPass() : new ColorMode([gl.ONE_MINUS_DST_ALPHA, gl.DST_ALPHA, gl.ONE, gl.ONE], index$1.Color.transparent, [true, true, true, true]);
drawGroundEffect$1(groundEffectProps, painter, source, layer, coords, depthMode, stencilColorPass, colorColorPass, CullFaceMode.disabled, aoPass, "color", opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, attenuation, conflateLayer, false);
}
}
function evaluateBucket(painter, source, layer, coords) {
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
if (bucket.needsEvaluation()) {
bucket.uploadUpdatedColorBuffer(painter.context);
}
}
}
function updateBuildingReplacementsAndTileBorderVisibility(painter, source, layer, layerIndex, layerConflate, coords) {
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
if (layerConflate) {
bucket.updateReplacement(coord, painter.replacementSource, layerIndex);
}
bucket.uploadUpdatedIndexBuffer(painter.context);
}
}
function draw$1(painter, source, layer, coords) {
const perfStartTime = index$1.PerformanceUtils.now();
if (painter.currentLayer < painter.firstLightBeamLayer) {
painter.firstLightBeamLayer = painter.currentLayer;
}
const aoIntensity = layer.paint.get("building-ambient-occlusion-ground-intensity");
const aoRadius = layer.paint.get("building-ambient-occlusion-ground-radius");
const aoGroundAttenuation = layer.paint.get("building-ambient-occlusion-ground-attenuation");
const opacity = layer.paint.get("building-opacity");
if (opacity <= 0) {
return;
}
let aoEnabled = aoIntensity > 0 && aoRadius > 0;
let castsShadowsEnabled = true;
let receiveShadowsEnabled = true;
let drawLayer = true;
const verticalScale = layer.paint.get("building-vertical-scale");
if (verticalScale <= 0) {
return;
}
Debug.run(() => {
const debugParams = DrawBuildingsDebugParams.getOrCreateInstance(painter);
aoEnabled = aoEnabled && debugParams.drawGroundAO;
castsShadowsEnabled = castsShadowsEnabled && debugParams.drawShadowPass;
drawLayer = drawLayer && debugParams.drawTranslucentPass;
});
if (!painter.shadowRenderer) {
receiveShadowsEnabled = false;
}
const conflateLayer = painter.conflationActive && painter.style.isLayerClipped(layer, source.getSource());
const layerIndex = painter.style.order.indexOf(layer.fqid);
updateBuildingReplacementsAndTileBorderVisibility(painter, source, layer, layerIndex, conflateLayer, coords);
evaluateBucket(painter, source, layer, coords);
layer.resetLayerRenderingStats(painter);
if (painter.shadowRenderer) painter.shadowRenderer.useNormalOffset = true;
if (painter.renderPass === "shadow" && painter.shadowRenderer && castsShadowsEnabled) {
const shadowRenderer = painter.shadowRenderer;
const definesForPass = [];
const depthMode = shadowRenderer.getShadowPassDepthMode();
const colorMode = shadowRenderer.getShadowPassColorMode();
drawTiles({
painter,
source,
layer,
coords,
defines: definesForPass,
blendMode: colorMode,
depthMode,
opacity,
verticalScale,
facadeEmissiveChance: 0,
facadeAOIntensity: 0,
floodLightIntensity: 0,
floodLightColor: [0, 0, 0]
});
} else if (painter.renderPass === "translucent" && drawLayer) {
let definesForPass = [
"HAS_ATTRIBUTE_a_part_color_emissive",
"LIGHTING_3D_MODE"
];
if (receiveShadowsEnabled) {
definesForPass = definesForPass.concat("RENDER_SHADOWS", "DEPTH_TEXTURE");
}
if (painter.shadowRenderer && painter.shadowRenderer.useNormalOffset) {
definesForPass = definesForPass.concat("NORMAL_OFFSET");
}
Debug.run(() => {
const debugParams = DrawBuildingsDebugParams.getOrCreateInstance(painter);
if (debugParams.showNormals) {
definesForPass = definesForPass.concat("DEBUG_SHOW_NORMALS");
}
});
const facadeEmissiveChance = layer.paint.get("building-facade-emissive-chance");
const facadeAOIntensity = layer.paint.get("building-ambient-occlusion-intensity");
const floodLightIntensity = layer.paint.get("building-flood-light-intensity");
const ignoreLut = layer.paint.get("building-flood-light-color-use-theme").constantOr("default") === "none";
const floodLightColor = layer.paint.get("building-flood-light-color").toNonPremultipliedRenderColor(ignoreLut ? null : layer.lut).toArray01().slice(0, 3);
const floodLightGroundAttenuation = layer.paint.get("building-flood-light-ground-attenuation");
const floodLightEnabled = floodLightIntensity > 0;
const depthMode = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
if (opacity < 1) {
drawTiles({
painter,
source,
layer,
coords,
defines: definesForPass,
blendMode: ColorMode.disabled,
depthMode,
opacity,
verticalScale,
facadeEmissiveChance,
facadeAOIntensity,
floodLightIntensity,
floodLightColor,
depthOnly: true
});
}
const blendMode = painter.colorModeForRenderPass();
drawTiles({
painter,
source,
layer,
coords,
defines: definesForPass,
blendMode,
depthMode,
opacity,
verticalScale,
facadeEmissiveChance,
facadeAOIntensity,
floodLightIntensity,
floodLightColor
});
if (aoEnabled) {
drawGroundEffect(painter, source, layer, coords, true, opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, aoGroundAttenuation, conflateLayer, false);
}
if (floodLightEnabled) {
drawGroundEffect(painter, source, layer, coords, false, opacity, aoIntensity, aoRadius, floodLightIntensity, floodLightColor, floodLightGroundAttenuation, conflateLayer, false);
}
} else if (painter.renderPass === "light-beam" && drawLayer) {
const definesForPass = [
"HAS_ATTRIBUTE_a_part_color_emissive",
"HAS_ATTRIBUTE_a_bloom_attenuation"
];
const depthMode = new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const blendMode = ColorMode.alphaBlended;
drawTiles({
painter,
source,
layer,
coords,
defines: definesForPass,
blendMode,
depthMode,
opacity,
verticalScale,
facadeEmissiveChance: 0,
facadeAOIntensity: 0,
floodLightIntensity: 0,
floodLightColor: [0, 0, 0]
});
}
if (painter.shadowRenderer) painter.shadowRenderer.useNormalOffset = false;
painter.resetStencilClippingMasks();
index$1.PerformanceUtils.measureWithDetails(index$1.PerformanceUtils.GROUP_RENDERING, `Building.draw(${painter.renderPass})`, "Building", perfStartTime);
}
const RASTER_COLOR_TEXTURE_UNIT$1 = 2;
function adjustColorMix(colorMix) {
return [
colorMix[0] * index$1.COLOR_MIX_FACTOR,
colorMix[1] * index$1.COLOR_MIX_FACTOR,
colorMix[2] * index$1.COLOR_MIX_FACTOR,
0
];
}
function drawRaster(painter, sourceCache, layer, tileIDs, variableOffsets, isInitialLoad) {
if (painter.renderPass !== "translucent") return;
if (layer.paint.get("raster-opacity") === 0) return;
const isGlobeProjection = painter.transform.projection.name === "globe";
const renderingWithElevation = layer.paint.get("raster-elevation") !== 0;
const renderingElevatedOnGlobe = renderingWithElevation && isGlobeProjection;
if (painter.renderElevatedRasterBackface && !renderingElevatedOnGlobe) {
return;
}
const context = painter.context;
const gl = context.gl;
const source = sourceCache.getSource();
const mrt = painter.terrain && painter.terrain.renderingToTexture && painter.emissiveMode === "mrt-fallback";
const rasterConfig = configureRaster(source, layer, context, gl, mrt);
if (source instanceof index$1.ImageSource && !tileIDs.length) {
if (!isGlobeProjection) {
return;
}
}
const emissiveStrength = layer.paint.get("raster-emissive-strength");
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const renderingToTexture = painter.terrain && painter.terrain.renderingToTexture;
const align = !painter.options.moving;
const textureFilter = layer.paint.get("raster-resampling") === "nearest" ? gl.NEAREST : gl.LINEAR;
if (source instanceof index$1.ImageSource && !tileIDs.length && (source.onNorthPole || source.onSouthPole)) {
const stencilMode = renderingWithElevation ? painter.stencilModeFor3D() : StencilMode.disabled;
if (source.onNorthPole) {
drawPole(true, null, painter, sourceCache, layer, emissiveStrength, rasterConfig, CullFaceMode.disabled, stencilMode);
} else {
drawPole(false, null, painter, sourceCache, layer, emissiveStrength, rasterConfig, CullFaceMode.disabled, stencilMode);
}
return;
}
if (!tileIDs.length) {
return;
}
const [stencilModes, coords] = source instanceof index$1.ImageSource || renderingToTexture ? [{}, tileIDs] : painter.stencilConfigForOverlap(tileIDs);
const minTileZ = coords[coords.length - 1].overscaledZ;
if (renderingElevatedOnGlobe) {
rasterConfig.defines.push("PROJECTION_GLOBE_VIEW");
}
if (renderingWithElevation) {
rasterConfig.defines.push("RENDER_CUTOFF");
}
const drawTiles = (tiles, cullFaceMode, elevatedStencilMode) => {
for (const coord of tiles) {
const unwrappedTileID = coord.toUnwrapped();
const tile = sourceCache.getTile(coord);
if (renderingToTexture && !(tile && tile.hasData())) continue;
context.activeTexture.set(gl.TEXTURE0);
const textureDescriptor = getTextureDescriptor(tile, source, layer, rasterConfig);
if (!textureDescriptor || !textureDescriptor.texture) continue;
const { texture, mix: rasterColorMix, offset: rasterColorOffset, tileSize, buffer } = textureDescriptor;
let depthMode;
let projMatrix;
if (renderingToTexture) {
depthMode = DepthMode.disabled;
projMatrix = coord.projMatrix;
} else if (renderingWithElevation) {
depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
projMatrix = isGlobeProjection ? Float32Array.from(painter.transform.expandedFarZProjMatrix) : painter.transform.calculateProjMatrix(unwrappedTileID, align);
} else {
depthMode = painter.depthModeForSublayer(
coord.overscaledZ - minTileZ,
layer.paint.get("raster-opacity") === 1 ? DepthMode.ReadWrite : DepthMode.ReadOnly,
gl.LESS
);
projMatrix = painter.transform.calculateProjMatrix(unwrappedTileID, align);
}
const stencilMode = painter.terrain && renderingToTexture ? painter.terrain.stencilModeForRTTOverlap(coord) : stencilModes[coord.overscaledZ];
const rasterFadeDuration = isInitialLoad ? 0 : layer.paint.get("raster-fade-duration");
tile.registerFadeDuration(rasterFadeDuration);
const parentTile = sourceCache.findLoadedParent(coord, 0);
const fade = rasterFade(tile, parentTile, sourceCache, painter.transform, rasterFadeDuration);
if (!fade.isFading && tile.refreshedUponExpiration) {
tile.refreshedUponExpiration = false;
}
if (painter.terrain) painter.terrain.prepareDrawTile();
let parentScaleBy, parentTL;
context.activeTexture.set(gl.TEXTURE0);
texture.bind(textureFilter, gl.CLAMP_TO_EDGE);
context.activeTexture.set(gl.TEXTURE1);
if (parentTile) {
if (parentTile.texture) {
parentTile.texture.bind(textureFilter, gl.CLAMP_TO_EDGE);
}
parentScaleBy = Math.pow(2, parentTile.tileID.overscaledZ - tile.tileID.overscaledZ);
parentTL = [tile.tileID.canonical.x * parentScaleBy % 1, tile.tileID.canonical.y * parentScaleBy % 1];
} else {
texture.bind(textureFilter, gl.CLAMP_TO_EDGE);
}
if ("useMipmap" in texture && context.extTextureFilterAnisotropic && painter.transform.pitch > 20) {
gl.texParameterf(gl.TEXTURE_2D, context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, context.extTextureFilterAnisotropicMax);
}
const tr = painter.transform;
let perspectiveTransform;
const cutoffParams = renderingWithElevation ? cutoffParamsForElevation$1(tr) : [0, 0, 0, 0];
let normalizeMatrix;
let globeMatrix;
let globeMercatorMatrix;
let mercatorCenter;
let gridMatrix;
let latitudinalLod = 0;
if (renderingElevatedOnGlobe && source instanceof index$1.ImageSource && source.coordinates.length > 3) {
normalizeMatrix = Float32Array.from(index$1.globeNormalizeECEF(index$1.globeTileBounds(new index$1.CanonicalTileID(0, 0, 0))));
globeMatrix = Float32Array.from(tr.globeMatrix);
globeMercatorMatrix = Float32Array.from(index$1.calculateGlobeMercatorMatrix(tr));
mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
perspectiveTransform = source.elevatedGlobePerspectiveTransform;
gridMatrix = source.elevatedGlobeGridMatrix || new Float32Array(9);
} else if (renderingElevatedOnGlobe) {
const tileBounds = index$1.tileCornersToBounds(coord.canonical);
latitudinalLod = index$1.getLatitudinalLod(tileBounds.getCenter().lat);
normalizeMatrix = Float32Array.from(index$1.globeNormalizeECEF(index$1.globeTileBounds(coord.canonical)));
globeMatrix = Float32Array.from(tr.globeMatrix);
globeMercatorMatrix = Float32Array.from(index$1.calculateGlobeMercatorMatrix(tr));
mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
perspectiveTransform = [0, 0];
gridMatrix = Float32Array.from(index$1.getGridMatrix(coord.canonical, tileBounds, latitudinalLod, tr.worldSize / tr._pixelsPerMercatorPixel));
} else {
perspectiveTransform = source instanceof index$1.ImageSource ? source.perspectiveTransform : [0, 0];
normalizeMatrix = new Float32Array(16);
globeMatrix = new Float32Array(9);
globeMercatorMatrix = new Float32Array(16);
mercatorCenter = [0, 0];
gridMatrix = new Float32Array(9);
}
const uniformValues = rasterUniformValues(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
projMatrix,
normalizeMatrix,
globeMatrix,
globeMercatorMatrix,
gridMatrix,
parentTL || [0, 0],
index$1.globeToMercatorTransition(painter.transform.zoom),
mercatorCenter,
cutoffParams,
parentScaleBy || 1,
fade,
layer,
perspectiveTransform,
renderingWithElevation ? layer.paint.get("raster-elevation") : 0,
RASTER_COLOR_TEXTURE_UNIT$1,
rasterColorMix,
rasterColorOffset,
rasterConfig.range,
tileSize,
buffer,
emissiveStrength
);
const affectedByFog = painter.isTileAffectedByFog(coord);
const program = painter.getOrCreateProgram("raster", { defines: rasterConfig.defines, overrideFog: affectedByFog });
painter.uploadCommonUniforms(context, program, unwrappedTileID);
if (source instanceof index$1.ImageSource) {
const elevatedGlobeVertexBuffer = source.elevatedGlobeVertexBuffer;
const elevatedGlobeIndexBuffer = source.elevatedGlobeIndexBuffer;
if (renderingToTexture || !isGlobeProjection) {
if (source.boundsBuffer && source.boundsSegments) program.draw(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
source.boundsBuffer,
painter.quadTriangleIndexBuffer,
source.boundsSegments
);
} else if (elevatedGlobeVertexBuffer && elevatedGlobeIndexBuffer) {
const segments = tr.zoom <= index$1.GLOBE_ZOOM_THRESHOLD_MIN ? source.elevatedGlobeSegments : source.getSegmentsForLongitude(tr.center.lng);
if (segments) {
program.draw(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
elevatedGlobeVertexBuffer,
elevatedGlobeIndexBuffer,
segments
);
}
}
} else if (renderingElevatedOnGlobe) {
depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const sharedBuffers = painter.globeSharedBuffers;
if (sharedBuffers) {
const [buffer2, indexBuffer, segments] = sharedBuffers.getGridBuffers(latitudinalLod, false);
index$1.assert(buffer2);
index$1.assert(indexBuffer);
index$1.assert(segments);
program.draw(painter, gl.TRIANGLES, depthMode, elevatedStencilMode || stencilMode, painter.colorModeForRenderPass(), cullFaceMode, uniformValues, layer.id, buffer2, indexBuffer, segments);
}
} else {
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = painter.getTileBoundsBuffers(tile);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
}
}
if (!(source instanceof index$1.ImageSource) && renderingElevatedOnGlobe) {
for (const coord of tiles) {
const topCap = coord.canonical.y === 0;
const bottomCap = coord.canonical.y === (1 << coord.canonical.z) - 1;
if (topCap) {
drawPole(true, coord, painter, sourceCache, layer, emissiveStrength, rasterConfig, cullFaceMode, elevatedStencilMode || StencilMode.disabled);
}
if (bottomCap) {
drawPole(false, coord, painter, sourceCache, layer, emissiveStrength, rasterConfig, cullFaceMode === CullFaceMode.frontCW ? CullFaceMode.backCW : CullFaceMode.frontCW, elevatedStencilMode || StencilMode.disabled);
}
}
}
};
if (renderingElevatedOnGlobe) {
if (painter.renderElevatedRasterBackface) {
drawTiles(coords, CullFaceMode.backCW, painter.stencilModeFor3D());
} else {
drawTiles(coords, CullFaceMode.frontCW, painter.stencilModeFor3D());
}
} else {
drawTiles(coords, CullFaceMode.disabled, void 0);
}
painter.resetStencilClippingMasks();
}
function drawPole(isNorth, coord, painter, sourceCache, layer, emissiveStrength, rasterConfig, cullFaceMode, stencilMode) {
const source = sourceCache.getSource();
const sharedBuffers = painter.globeSharedBuffers;
if (!sharedBuffers) return;
let tile;
if (coord) {
tile = sourceCache.getTile(coord);
}
let texture;
let globeMatrix;
if (source instanceof index$1.ImageSource) {
texture = source.texture;
globeMatrix = index$1.globePoleMatrixForTile(0, 0, painter.transform);
} else if (tile && coord) {
texture = tile.texture;
globeMatrix = index$1.globePoleMatrixForTile(coord.canonical.z, coord.canonical.x, painter.transform);
}
if (!texture || !globeMatrix) return;
if (!isNorth) {
globeMatrix = index$1.scale$3(index$1.create(), globeMatrix, [1, -1, 1]);
}
const context = painter.context;
const gl = context.gl;
const textureFilter = layer.paint.get("raster-resampling") === "nearest" ? gl.NEAREST : gl.LINEAR;
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const defines = rasterConfig.defines;
defines.push("GLOBE_POLES");
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
const projMatrix = Float32Array.from(painter.transform.expandedFarZProjMatrix);
const normalizeMatrix = Float32Array.from(index$1.globeNormalizeECEF(index$1.globeTileBounds(new index$1.CanonicalTileID(0, 0, 0))));
const fade = { opacity: 1, mix: 0 };
if (painter.terrain) painter.terrain.prepareDrawTile();
context.activeTexture.set(gl.TEXTURE0);
texture.bind(textureFilter, gl.CLAMP_TO_EDGE);
context.activeTexture.set(gl.TEXTURE1);
texture.bind(textureFilter, gl.CLAMP_TO_EDGE);
if ("useMipmap" in texture && context.extTextureFilterAnisotropic && painter.transform.pitch > 20) {
gl.texParameterf(gl.TEXTURE_2D, context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, context.extTextureFilterAnisotropicMax);
}
const [
northPoleBuffer,
southPoleBuffer,
indexBuffer,
segment
] = coord ? sharedBuffers.getPoleBuffers(coord.canonical.z, false) : sharedBuffers.getPoleBuffers(0, true);
const elevation = layer.paint.get("raster-elevation");
let vertexBuffer;
if (isNorth) {
vertexBuffer = northPoleBuffer;
painter.renderDefaultNorthPole = elevation !== 0;
} else {
vertexBuffer = southPoleBuffer;
painter.renderDefaultSouthPole = elevation !== 0;
}
const rasterColorMix = adjustColorMix(rasterConfig.mix);
const uniformValues = rasterPoleUniformValues(projMatrix, normalizeMatrix, globeMatrix, index$1.globeToMercatorTransition(painter.transform.zoom), fade, layer, [0, 0], elevation, RASTER_COLOR_TEXTURE_UNIT$1, rasterColorMix, rasterConfig.offset, rasterConfig.range, emissiveStrength);
const program = painter.getOrCreateProgram("raster", { defines });
painter.uploadCommonUniforms(context, program, null);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
vertexBuffer,
indexBuffer,
segment
);
}
function cutoffParamsForElevation$1(tr) {
const near = tr._nearZ;
const far = tr.projection.farthestPixelDistance(tr);
const zRange = far - near;
const fadeRangePixels = tr.height * 0.2;
const cutoffDistance = near + fadeRangePixels;
const relativeCutoffDistance = (cutoffDistance - near) / zRange;
const relativeCutoffFadeDistance = (cutoffDistance - fadeRangePixels - near) / zRange;
return [near, far, relativeCutoffFadeDistance, relativeCutoffDistance];
}
function prepare$3(layer, sourceCache, _) {
const source = sourceCache.getSource();
if (!(source instanceof RasterArrayTileSource) || !source.loaded()) return;
const sourceLayer = layer.sourceLayer || source.rasterLayerIds && source.rasterLayerIds[0];
if (!sourceLayer) return;
const band = layer.paint.get("raster-array-band") || source.getInitialBand(sourceLayer);
if (band == null) return;
const tiles = sourceCache.getIds().map((id) => sourceCache.getTileByID(id));
for (const tile of tiles) {
if (tile.updateNeeded(layer.id, band)) {
source.prepareTile(tile, sourceLayer, layer.id, band);
}
}
}
function getTextureDescriptor(tile, source, layer, rasterConfig) {
if (!tile) return;
if (source instanceof RasterArrayTileSource && tile instanceof RasterArrayTile) {
return source.getTextureDescriptor(tile, layer, true);
}
return {
texture: tile.texture,
mix: adjustColorMix(rasterConfig.mix),
offset: rasterConfig.offset,
buffer: 0,
tileSize: 1
};
}
function configureRaster(source, layer, context, gl, mrt) {
const isRasterColor = layer.paint.get("raster-color");
const isRasterArray = source.type === "raster-array";
const defines = [];
const inputResampling = layer.paint.get("raster-resampling");
const inputMix = layer.paint.get("raster-color-mix");
let range = layer.paint.get("raster-color-range");
const mix = [inputMix[0], inputMix[1], inputMix[2], 0];
const offset = inputMix[3];
let resampling = inputResampling === "nearest" ? gl.NEAREST : gl.LINEAR;
if (isRasterArray) {
defines.push("RASTER_ARRAY");
if (!isRasterColor) defines.push("RASTER_COLOR");
if (inputResampling === "linear") defines.push("RASTER_ARRAY_LINEAR");
resampling = gl.NEAREST;
if (!range) {
if (source.rasterLayers) {
const foundLayer = source.rasterLayers.find(({ id }) => id === layer.sourceLayer);
if (foundLayer && foundLayer.fields && foundLayer.fields.range) {
range = foundLayer.fields.range;
}
}
}
}
range = range || [0, 1];
if (isRasterColor) {
defines.push("RASTER_COLOR");
context.activeTexture.set(gl.TEXTURE2);
layer.updateColorRamp(range);
let tex = layer.colorRampTexture;
if (!tex) tex = layer.colorRampTexture = new index$1.Texture(context, layer.colorRamp, gl.RGBA8);
tex.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
if (mrt) {
defines.push("USE_MRT1");
}
return {
mix,
range,
offset,
defines,
resampling
};
}
var particleAttributes = index$1.createLayout([
{ name: "a_index", type: "Int16", components: 1 }
]);
class RasterParticleState {
constructor(context, id, textureSize, RGBAPositions) {
const emptyImage = {
width: textureSize[0],
height: textureSize[1],
data: null
};
const gl = context.gl;
this.targetColorTexture = new index$1.Texture(context, emptyImage, gl.RGBA8, { useMipmap: false });
this.backgroundColorTexture = new index$1.Texture(context, emptyImage, gl.RGBA8, { useMipmap: false });
this.context = context;
this.updateParticleTexture(id, RGBAPositions);
this.lastInvalidatedAt = 0;
}
updateParticleTexture(id, RGBAPositions) {
index$1.assert(RGBAPositions.width === RGBAPositions.height);
if (this.particleTextureDimension === RGBAPositions.width) {
return;
}
if (this.particleTexture0 || this.particleTexture1 || this.particleIndexBuffer || this.particleSegment) {
index$1.assert(this.particleTexture0 && this.particleTexture1 && this.particleIndexBuffer && this.particleSegment);
this.particleTexture0.destroy();
this.particleTexture1.destroy();
this.particleIndexBuffer.destroy();
this.particleSegment.destroy();
}
const gl = this.context.gl;
const numParticles = RGBAPositions.width * RGBAPositions.height;
this.particleTexture0 = new index$1.Texture(this.context, RGBAPositions, gl.RGBA8, { premultiply: false, useMipmap: false });
this.particleTexture1 = new index$1.Texture(this.context, RGBAPositions, gl.RGBA8, { premultiply: false, useMipmap: false });
const particleIndices = new index$1.StructArrayLayout1i2();
particleIndices.reserve(numParticles);
for (let i = 0; i < numParticles; i++) {
particleIndices.emplaceBack(i);
}
this.particleIndexBuffer = this.context.createVertexBuffer(particleIndices, particleAttributes.members, true);
this.particleSegment = index$1.SegmentVector.simpleSegment(0, 0, this.particleIndexBuffer.length, 0);
this.particleTextureDimension = RGBAPositions.width;
}
update(layerLastInvalidatedAt) {
if (this.lastInvalidatedAt < layerLastInvalidatedAt) {
this.lastInvalidatedAt = index$1.exported$1.now();
return false;
}
return true;
}
destroy() {
this.targetColorTexture.destroy();
this.backgroundColorTexture.destroy();
this.particleIndexBuffer.destroy();
this.particleTexture0.destroy();
this.particleTexture1.destroy();
this.particleSegment.destroy();
}
}
const VELOCITY_TEXTURE_UNIT = 0;
const RASTER_PARTICLE_TEXTURE_UNIT = 1;
const RASTER_COLOR_TEXTURE_UNIT = 2;
const SPEED_MAX_VALUE = 0.15;
function drawRasterParticle(painter, sourceCache, layer, tileIDs, _, isInitialLoad) {
if (painter.renderPass === "offscreen") {
renderParticlesToTexture(painter, sourceCache, layer, tileIDs);
}
if (painter.renderPass === "translucent") {
renderTextureToMap(painter, sourceCache, layer, tileIDs, isInitialLoad);
painter.style.map.triggerRepaint();
}
}
function createPositionRGBAData(textureDimension) {
const numParticles = textureDimension * textureDimension;
const RGBAPositions = new Uint8Array(4 * numParticles);
const esgtsa = function(s) {
s |= 0;
s = Math.imul(s ^ 2747636419, 2654435769);
s = Math.imul(s ^ s >>> 16, 2654435769);
s = Math.imul(s ^ s >>> 16, 2654435769);
return (s >>> 0) / 4294967296;
};
const invScale = 1 / RASTER_PARTICLE_POS_SCALE;
for (let i = 0; i < numParticles; i++) {
const x = invScale * (esgtsa(2 * i + 0) + RASTER_PARTICLE_POS_OFFSET);
const y = invScale * (esgtsa(2 * i + 1) + RASTER_PARTICLE_POS_OFFSET);
const rx = x;
const ry = x * 255 % 1;
const rz = y;
const rw = y * 255 % 1;
const px = rx - ry / 255;
const py = ry;
const pz = rz - rw / 255;
const pw = rw;
RGBAPositions[4 * i + 0] = 255 * px;
RGBAPositions[4 * i + 1] = 255 * py;
RGBAPositions[4 * i + 2] = 255 * pz;
RGBAPositions[4 * i + 3] = 255 * pw;
}
return RGBAPositions;
}
function renderParticlesToTexture(painter, sourceCache, layer, tileIDs) {
if (!tileIDs.length) {
return;
}
const context = painter.context;
const gl = context.gl;
const source = sourceCache.getSource();
if (!(source instanceof RasterArrayTileSource)) return;
const particleTextureDimension = Math.ceil(Math.sqrt(layer.paint.get("raster-particle-count")));
let particlePositionRGBAImage = layer.particlePositionRGBAImage;
if (!particlePositionRGBAImage || particlePositionRGBAImage.width !== particleTextureDimension) {
const RGBAData = createPositionRGBAData(particleTextureDimension);
const imageSize = { width: particleTextureDimension, height: particleTextureDimension };
particlePositionRGBAImage = layer.particlePositionRGBAImage = new index$1.RGBAImage(imageSize, RGBAData);
}
let particleFramebuffer = layer.particleFramebuffer;
if (!particleFramebuffer) {
particleFramebuffer = layer.particleFramebuffer = context.createFramebuffer(particleTextureDimension, particleTextureDimension, 1, null);
} else if (particleFramebuffer.width !== particleTextureDimension) {
index$1.assert(particleFramebuffer.width === particleFramebuffer.height);
particleFramebuffer.destroy();
particleFramebuffer = layer.particleFramebuffer = context.createFramebuffer(particleTextureDimension, particleTextureDimension, 1, null);
}
const tiles = [];
for (const id of tileIDs) {
const tile = sourceCache.getTile(id);
if (!(tile instanceof RasterArrayTile)) continue;
const data = getTileData(tile, source, layer);
if (!data) continue;
index$1.assert(data.texture);
const textureSize = [tile.tileSize, tile.tileSize];
let tileFramebuffer = layer.tileFramebuffer;
if (!tileFramebuffer) {
const fbWidth = textureSize[0];
const fbHeight = textureSize[1];
tileFramebuffer = layer.tileFramebuffer = context.createFramebuffer(fbWidth, fbHeight, 1, null);
}
index$1.assert(tileFramebuffer.width === textureSize[0] && tileFramebuffer.height === textureSize[1]);
let state = tile.rasterParticleState;
if (!state) {
state = tile.rasterParticleState = new RasterParticleState(context, id, textureSize, particlePositionRGBAImage);
}
const renderBackground2 = state.update(layer.lastInvalidatedAt);
if (state.particleTextureDimension !== particleTextureDimension) {
state.updateParticleTexture(id, particlePositionRGBAImage);
}
const t = state.targetColorTexture;
state.targetColorTexture = state.backgroundColorTexture;
state.backgroundColorTexture = t;
const p = state.particleTexture0;
state.particleTexture0 = state.particleTexture1;
state.particleTexture1 = p;
tiles.push([id, data, state, renderBackground2]);
}
if (tiles.length === 0) {
return;
}
const now = index$1.exported$1.now();
const frameDeltaSeconds = layer.previousDrawTimestamp ? 1e-3 * (now - layer.previousDrawTimestamp) : 0.0167;
layer.previousDrawTimestamp = now;
if (layer.hasColorMap()) {
context.activeTexture.set(gl.TEXTURE0 + RASTER_COLOR_TEXTURE_UNIT);
let tex = layer.colorRampTexture;
if (!tex) tex = layer.colorRampTexture = new index$1.Texture(context, layer.colorRamp, gl.RGBA8);
tex.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
context.bindFramebuffer.set(layer.tileFramebuffer.framebuffer);
renderBackground(painter, layer, tiles);
renderParticles(painter, sourceCache, layer, tiles);
context.bindFramebuffer.set(layer.particleFramebuffer.framebuffer);
updateParticles(painter, layer, tiles, frameDeltaSeconds);
}
function getTileData(tile, source, layer) {
if (!tile) {
return null;
}
const textureDesc = source.getTextureDescriptor(tile, layer, true);
if (!textureDesc) {
return null;
}
let { texture, mix, offset, tileSize, buffer, format } = textureDesc;
if (!texture || !format) {
return null;
}
let scalarData = false;
if (format === "uint32") {
scalarData = true;
mix[3] = 0;
mix = computeRasterColorMix(index$1.COLOR_RAMP_RES$1, mix, [0, layer.paint.get("raster-particle-max-speed")]);
offset = computeRasterColorOffset(index$1.COLOR_RAMP_RES$1, offset, [0, layer.paint.get("raster-particle-max-speed")]);
}
const dataFormatDefine = {
uint8: "DATA_FORMAT_UINT8",
uint16: "DATA_FORMAT_UINT16",
uint32: "DATA_FORMAT_UINT32"
}[format];
return {
texture,
textureOffset: [buffer / (tileSize + 2 * buffer), tileSize / (tileSize + 2 * buffer)],
tileSize,
scalarData,
scale: mix,
offset,
defines: ["RASTER_ARRAY", dataFormatDefine]
};
}
function renderBackground(painter, layer, tiles) {
const context = painter.context;
const gl = context.gl;
const framebuffer = layer.tileFramebuffer;
context.activeTexture.set(gl.TEXTURE0);
const textureUnit = 0;
const opacityValue = fadeOpacityCurve(layer.paint.get("raster-particle-fade-opacity-factor"));
const uniforms = rasterParticleTextureUniformValues(textureUnit, opacityValue);
const program = painter.getOrCreateProgram("rasterParticleTexture", { defines: [], overrideFog: false });
for (const tile of tiles) {
const [, , particleState, renderBackground2] = tile;
framebuffer.colorAttachment0.set(particleState.targetColorTexture.texture);
context.viewport.set([0, 0, framebuffer.width, framebuffer.height]);
context.clear({ color: index$1.Color.transparent });
if (!renderBackground2) continue;
particleState.backgroundColorTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniforms,
layer.id,
painter.viewportBuffer,
painter.quadTriangleIndexBuffer,
painter.viewportSegments
);
}
}
function fadeOpacityCurve(fadeOpacityFactor) {
const x = fadeOpacityFactor;
const a = 0.05;
return (1 + a) * x / (x + a);
}
function resetRateCurve(resetRate) {
return Math.pow(resetRate, 6);
}
function renderParticles(painter, sourceCache, layer, tiles) {
const context = painter.context;
const gl = context.gl;
const framebuffer = layer.tileFramebuffer;
const isGlobeProjection = painter.transform.projection.name === "globe";
const maxSpeed = layer.paint.get("raster-particle-max-speed");
for (const targetTile of tiles) {
const [targetTileID, targetTileData, targetTileState] = targetTile;
context.activeTexture.set(gl.TEXTURE0 + VELOCITY_TEXTURE_UNIT);
targetTileData.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
framebuffer.colorAttachment0.set(targetTileState.targetColorTexture.texture);
const defines = targetTileData.defines;
const program = painter.getOrCreateProgram("rasterParticleDraw", { defines, overrideFog: false });
context.activeTexture.set(gl.TEXTURE0 + RASTER_PARTICLE_TEXTURE_UNIT);
const tileIDs = targetTileData.scalarData ? [] : [0, 1, 2, 3].map((idx) => index$1.neighborCoord[idx](targetTileID));
tileIDs.push(targetTileID);
const x = targetTileID.canonical.x;
const y = targetTileID.canonical.y;
for (const tileID of tileIDs) {
const tile = sourceCache.getTile(isGlobeProjection ? tileID.wrapped() : tileID);
if (!tile) continue;
const state = tile.rasterParticleState;
if (!state) continue;
const wrapDelta = tileID.wrap - targetTileID.wrap;
const nx = tileID.canonical.x + (1 << tileID.canonical.z) * wrapDelta;
const ny = tileID.canonical.y;
state.particleTexture0.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
const rasterParticleTextureRes = state.particleTexture0.size;
index$1.assert(rasterParticleTextureRes[0] === rasterParticleTextureRes[1]);
const rasterParticleTextureSideLen = rasterParticleTextureRes[0];
const tileOffset = [nx - x, ny - y];
const uniforms = rasterParticleDrawUniformValues(
RASTER_PARTICLE_TEXTURE_UNIT,
rasterParticleTextureSideLen,
tileOffset,
VELOCITY_TEXTURE_UNIT,
targetTileData.texture.size,
RASTER_COLOR_TEXTURE_UNIT,
maxSpeed,
targetTileData.textureOffset,
targetTileData.scale,
targetTileData.offset
);
program.draw(
painter,
gl.POINTS,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniforms,
layer.id,
state.particleIndexBuffer,
void 0,
state.particleSegment
);
}
}
}
function updateParticles(painter, layer, tiles, frameDeltaSeconds) {
const context = painter.context;
const gl = context.gl;
const maxSpeed = layer.paint.get("raster-particle-max-speed");
const speedFactor = frameDeltaSeconds * layer.paint.get("raster-particle-speed-factor") * SPEED_MAX_VALUE;
const resetRateFactor = layer.paint.get("raster-particle-reset-rate-factor");
const resetRate = resetRateCurve(0.01 + resetRateFactor * 1);
const particleFramebuffer = layer.particleFramebuffer;
context.viewport.set([0, 0, particleFramebuffer.width, particleFramebuffer.height]);
for (const tile of tiles) {
const [, data, state] = tile;
context.activeTexture.set(gl.TEXTURE0 + VELOCITY_TEXTURE_UNIT);
data.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
context.activeTexture.set(gl.TEXTURE0 + RASTER_PARTICLE_TEXTURE_UNIT);
const particleTexture = state.particleTexture0;
particleTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
const uniforms = rasterParticleUpdateUniformValues(
RASTER_PARTICLE_TEXTURE_UNIT,
particleTexture.size[0],
VELOCITY_TEXTURE_UNIT,
data.texture.size,
maxSpeed,
speedFactor,
resetRate,
data.textureOffset,
data.scale,
data.offset
);
particleFramebuffer.colorAttachment0.set(state.particleTexture1.texture);
context.clear({ color: index$1.Color.transparent });
const updateProgram = painter.getOrCreateProgram("rasterParticleUpdate", { defines: data.defines });
updateProgram.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.unblended,
CullFaceMode.disabled,
uniforms,
layer.id,
painter.viewportBuffer,
painter.quadTriangleIndexBuffer,
painter.viewportSegments
);
}
}
function renderTextureToMap(painter, sourceCache, layer, tileIDs, _) {
const context = painter.context;
const gl = context.gl;
const tileSize = sourceCache.getSource().tileSize;
const minLiftForZoom = (1 - index$1.smoothstep(index$1.GLOBE_ZOOM_THRESHOLD_MAX, index$1.GLOBE_ZOOM_THRESHOLD_MAX + 1, painter.transform.zoom)) * 5 * tileSize;
const rasterElevation = minLiftForZoom + layer.paint.get("raster-particle-elevation");
const align = !painter.options.moving;
const isGlobeProjection = painter.transform.projection.name === "globe";
if (!tileIDs.length) {
return;
}
const [stencilModes, coords] = painter.stencilConfigForOverlap(tileIDs);
const defines = [];
if (isGlobeProjection) {
defines.push("PROJECTION_GLOBE_VIEW");
}
const stencilMode = painter.stencilModeFor3D();
for (const coord of coords) {
const unwrappedTileID = coord.toUnwrapped();
const tile = sourceCache.getTile(coord);
if (!tile.rasterParticleState) continue;
const particleState = tile.rasterParticleState;
const rasterFadeDuration = 100;
tile.registerFadeDuration(rasterFadeDuration);
const parentTile = sourceCache.findLoadedParent(coord, 0);
const fade = rasterFade(tile, parentTile, sourceCache, painter.transform, rasterFadeDuration);
if (painter.terrain) painter.terrain.prepareDrawTile();
context.activeTexture.set(gl.TEXTURE0);
particleState.targetColorTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
context.activeTexture.set(gl.TEXTURE1);
let parentScaleBy, parentTL;
if (parentTile && parentTile.rasterParticleState) {
parentTile.rasterParticleState.targetColorTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
parentScaleBy = Math.pow(2, parentTile.tileID.overscaledZ - tile.tileID.overscaledZ);
parentTL = [tile.tileID.canonical.x * parentScaleBy % 1, tile.tileID.canonical.y * parentScaleBy % 1];
} else {
particleState.targetColorTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
const projMatrix = isGlobeProjection ? Float32Array.from(painter.transform.expandedFarZProjMatrix) : painter.transform.calculateProjMatrix(unwrappedTileID, align);
const tr = painter.transform;
const cutoffParams = cutoffParamsForElevation(tr);
const tileBounds = index$1.tileCornersToBounds(coord.canonical);
const latitudinalLod = index$1.getLatitudinalLod(tileBounds.getCenter().lat);
let normalizeMatrix;
let globeMatrix;
let globeMercatorMatrix;
let mercatorCenter;
let gridMatrix;
if (isGlobeProjection) {
normalizeMatrix = Float32Array.from(index$1.globeNormalizeECEF(index$1.globeTileBounds(coord.canonical)));
globeMatrix = Float32Array.from(tr.globeMatrix);
globeMercatorMatrix = Float32Array.from(index$1.calculateGlobeMercatorMatrix(tr));
mercatorCenter = [index$1.mercatorXfromLng(tr.center.lng), index$1.mercatorYfromLat(tr.center.lat)];
gridMatrix = Float32Array.from(index$1.getGridMatrix(coord.canonical, tileBounds, latitudinalLod, tr.worldSize / tr._pixelsPerMercatorPixel));
} else {
normalizeMatrix = new Float32Array(16);
globeMatrix = new Float32Array(9);
globeMercatorMatrix = new Float32Array(16);
mercatorCenter = [0, 0];
gridMatrix = new Float32Array(9);
}
const uniformValues = rasterParticleUniformValues(
projMatrix,
normalizeMatrix,
globeMatrix,
globeMercatorMatrix,
gridMatrix,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
parentTL || [0, 0],
index$1.globeToMercatorTransition(painter.transform.zoom),
mercatorCenter,
cutoffParams,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
parentScaleBy || 1,
fade,
rasterElevation
);
const overrideFog = painter.isTileAffectedByFog(coord);
const program = painter.getOrCreateProgram("rasterParticle", { defines, overrideFog });
painter.uploadCommonUniforms(context, program, unwrappedTileID);
if (isGlobeProjection) {
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const skirtHeightValue = 0;
const sharedBuffers = painter.globeSharedBuffers;
if (sharedBuffers) {
const [buffer, indexBuffer, segments] = sharedBuffers.getGridBuffers(latitudinalLod, skirtHeightValue !== 0);
index$1.assert(buffer);
index$1.assert(indexBuffer);
index$1.assert(segments);
program.draw(painter, gl.TRIANGLES, depthMode, stencilMode, ColorMode.alphaBlended, painter.renderElevatedRasterBackface ? CullFaceMode.frontCCW : CullFaceMode.backCCW, uniformValues, layer.id, buffer, indexBuffer, segments);
}
} else {
const depthMode = painter.depthModeForSublayer(0, DepthMode.ReadOnly);
const stencilMode2 = stencilModes[coord.overscaledZ];
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = painter.getTileBoundsBuffers(tile);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode2,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniformValues,
layer.id,
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
}
}
painter.resetStencilClippingMasks();
}
function cutoffParamsForElevation(tr) {
const near = tr._nearZ;
const far = tr.projection.farthestPixelDistance(tr);
const zRange = far - near;
const fadeRangePixels = tr.height * 0.2;
const cutoffDistance = near + fadeRangePixels;
const relativeCutoffDistance = (cutoffDistance - near) / zRange;
const relativeCutoffFadeDistance = (cutoffDistance - fadeRangePixels - near) / zRange;
return [near, far, relativeCutoffFadeDistance, relativeCutoffDistance];
}
function prepare$2(layer, sourceCache, _) {
const source = sourceCache.getSource();
if (!(source instanceof RasterArrayTileSource) || !source.loaded()) return;
const sourceLayer = layer.sourceLayer || source.rasterLayerIds && source.rasterLayerIds[0];
if (!sourceLayer) return;
const band = layer.paint.get("raster-particle-array-band") || source.getInitialBand(sourceLayer);
if (band == null) return;
const tiles = sourceCache.getIds().map((id) => sourceCache.getTileByID(id));
for (const tile of tiles) {
if (tile.updateNeeded(layer.id, band)) {
source.prepareTile(tile, sourceLayer, layer.id, band);
}
}
}
function drawBackground(painter, sourceCache, layer, coords) {
const color = layer.paint.get("background-color");
const ignoreLut = layer.paint.get("background-color-use-theme").constantOr("default") === "none";
const opacity = layer.paint.get("background-opacity");
const emissiveStrength = layer.paint.get("background-emissive-strength");
const isViewportPitch = layer.paint.get("background-pitch-alignment") === "viewport";
if (opacity === 0) return;
const context = painter.context;
const gl = context.gl;
const transform = painter.transform;
const tileSize = transform.tileSize;
const image = layer.paint.get("background-pattern");
let patternPosition;
if (image !== void 0) {
if (image === null) {
return;
}
patternPosition = painter.imageManager.getPattern(index$1.ImageId.from(image.toString()), layer.scope, painter.style.getLut(layer.scope));
if (!patternPosition) {
return;
}
}
const pass = !image && color.a === 1 && opacity === 1 && painter.opaquePassEnabledForLayer() ? "opaque" : "translucent";
if (painter.renderPass !== pass) return;
const stencilMode = StencilMode.disabled;
const depthMode = painter.depthModeForSublayer(0, pass === "opaque" ? DepthMode.ReadWrite : DepthMode.ReadOnly);
const colorMode = painter.colorModeForDrapableLayerRenderPass(emissiveStrength);
const programName = image ? "backgroundPattern" : "background";
let tileIDs = coords;
let backgroundTiles;
if (!tileIDs) {
backgroundTiles = painter.getBackgroundTiles();
tileIDs = Object.values(backgroundTiles).map((tile) => tile.tileID);
}
if (image) {
context.activeTexture.set(gl.TEXTURE0);
painter.imageManager.bind(painter.context, layer.scope);
}
const isDraping = painter.terrain && painter.terrain.renderingToTexture;
const defines = [];
if (isDraping && painter.emissiveMode === "mrt-fallback") {
defines.push("USE_MRT1");
}
if (isViewportPitch) {
const program = painter.getOrCreateProgram(programName, { overrideFog: false, overrideRtt: true, defines });
const matrix = new Float32Array(index$1.identity([]));
const tileID = new index$1.OverscaledTileID(0, 0, 0, 0, 0);
const uniformValues = image ? backgroundPatternUniformValues(matrix, emissiveStrength, opacity, painter, image, layer.scope, patternPosition, isViewportPitch, { tileID, tileSize }) : backgroundUniformValues(matrix, emissiveStrength, opacity, color.toPremultipliedRenderColor(ignoreLut ? null : layer.lut));
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
painter.viewportBuffer,
painter.quadTriangleIndexBuffer,
painter.viewportSegments
);
return;
}
for (const tileID of tileIDs) {
const affectedByFog = painter.isTileAffectedByFog(tileID);
const program = painter.getOrCreateProgram(programName, { overrideFog: affectedByFog, defines });
const unwrappedTileID = tileID.toUnwrapped();
const matrix = coords ? tileID.projMatrix : painter.transform.calculateProjMatrix(unwrappedTileID);
painter.prepareDrawTile();
const tile = sourceCache ? sourceCache.getTile(tileID) : backgroundTiles ? backgroundTiles[tileID.key] : new Tile(tileID, tileSize, transform.zoom, painter);
const uniformValues = image ? backgroundPatternUniformValues(matrix, emissiveStrength, opacity, painter, image, layer.scope, patternPosition, isViewportPitch, { tileID, tileSize }) : backgroundUniformValues(matrix, emissiveStrength, opacity, color.toPremultipliedRenderColor(ignoreLut ? null : layer.lut));
painter.uploadCommonUniforms(context, program, unwrappedTileID);
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = painter.getTileBoundsBuffers(tile);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
}
}
const topColor = new index$1.Color(1, 0, 0, 1);
const btmColor = new index$1.Color(0, 1, 0, 1);
const leftColor = new index$1.Color(0, 0, 1, 1);
const rightColor = new index$1.Color(1, 0, 1, 1);
const centerColor = new index$1.Color(0, 1, 1, 1);
function drawDebug(painter, sourceCache, coords, color, silhouette, showParseStatus) {
for (let i = 0; i < coords.length; i++) {
if (silhouette) {
const radius = 1;
const darkenFactor = 0.8;
const colorMiddle = new index$1.Color(color.r * darkenFactor, color.g * darkenFactor, color.b * darkenFactor, 1);
drawDebugTile(painter, sourceCache, coords[i], color, -radius, -radius, showParseStatus);
drawDebugTile(painter, sourceCache, coords[i], color, -radius, radius, showParseStatus);
drawDebugTile(painter, sourceCache, coords[i], color, radius, radius, showParseStatus);
drawDebugTile(painter, sourceCache, coords[i], color, radius, -radius, showParseStatus);
drawDebugTile(painter, sourceCache, coords[i], colorMiddle, 0, 0, showParseStatus);
} else {
drawDebugTile(painter, sourceCache, coords[i], color, 0, 0, showParseStatus);
}
}
}
function drawDebugPadding(painter) {
const padding = painter.transform.padding;
const lineWidth = 3;
drawHorizontalLine(painter, painter.transform.height - (padding.top || 0), lineWidth, topColor);
drawHorizontalLine(painter, padding.bottom || 0, lineWidth, btmColor);
drawVerticalLine(painter, padding.left || 0, lineWidth, leftColor);
drawVerticalLine(painter, painter.transform.width - (padding.right || 0), lineWidth, rightColor);
const center = painter.transform.centerPoint;
drawCrosshair(painter, center.x, painter.transform.height - center.y, centerColor);
}
function drawDebugQueryGeometry(painter, sourceCache, coords) {
for (let i = 0; i < coords.length; i++) {
drawTileQueryGeometry(painter, sourceCache, coords[i]);
}
}
function drawDebugTile(painter, sourceCache, coord, color, offsetX, offsetY, showParseStatus) {
const context = painter.context;
const tr = painter.transform;
const gl = context.gl;
const isGlobeProjection = tr.projection.name === "globe";
const definesValues = isGlobeProjection ? ["PROJECTION_GLOBE_VIEW"] : [];
let posMatrix = index$1.clone(coord.projMatrix);
if (isGlobeProjection && index$1.globeToMercatorTransition(tr.zoom) > 0) {
const bounds = index$1.transitionTileAABBinECEF(coord.canonical, tr);
const decode = index$1.globeDenormalizeECEF(bounds);
posMatrix = index$1.multiply(new Float32Array(16), tr.globeMatrix, decode);
index$1.multiply(posMatrix, tr.projMatrix, posMatrix);
}
const jitterMatrix = index$1.create();
jitterMatrix[12] += 2 * offsetX / (index$1.exported$1.devicePixelRatio * tr.width);
jitterMatrix[13] += 2 * offsetY / (index$1.exported$1.devicePixelRatio * tr.height);
index$1.multiply(posMatrix, jitterMatrix, posMatrix);
const program = painter.getOrCreateProgram("debug", { defines: definesValues });
const tile = sourceCache.getTileByID(coord.key);
if (painter.terrain) painter.terrain.setupElevationDraw(tile, program);
const depthMode = DepthMode.disabled;
const stencilMode = StencilMode.disabled;
const colorMode = painter.colorModeForRenderPass();
const id = "$debug";
context.activeTexture.set(gl.TEXTURE0);
painter.emptyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
if (isGlobeProjection) {
tile._makeGlobeTileDebugBuffers(painter.context, tr);
} else {
tile._makeDebugTileBoundsBuffers(painter.context, tr.projection);
}
const debugBuffer = tile._tileDebugBuffer || painter.debugBuffer;
const debugIndexBuffer = tile._tileDebugIndexBuffer || painter.debugIndexBuffer;
const debugSegments = tile._tileDebugSegments || painter.debugSegments;
program.draw(
painter,
gl.LINE_STRIP,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
debugUniformValues(posMatrix, color.toPremultipliedRenderColor(null)),
id,
debugBuffer,
debugIndexBuffer,
debugSegments,
null,
null,
null,
[tile._globeTileDebugBorderBuffer]
);
if (showParseStatus) {
const tileRawData = tile.latestRawTileData;
const tileByteLength = tileRawData && tileRawData.byteLength || 0;
const tileSizeKb = Math.floor(tileByteLength / 1024);
let tileLabel = coord.canonical.toString();
if (coord.overscaledZ !== coord.canonical.z) {
tileLabel += ` => ${coord.overscaledZ}`;
}
tileLabel += ` ${tile.state}`;
tileLabel += ` ${tileSizeKb}kb`;
drawTextToOverlay(painter, tileLabel);
}
const tileSize = sourceCache.getTile(coord).tileSize;
const scaleRatio = 512 / Math.min(tileSize, 512) * (coord.overscaledZ / tr.zoom) * 0.5;
const debugTextBuffer = tile._tileDebugTextBuffer || painter.debugBuffer;
const debugTextIndexBuffer = tile._tileDebugTextIndexBuffer || painter.quadTriangleIndexBuffer;
const debugTextSegments = tile._tileDebugTextSegments || painter.debugSegments;
program.draw(
painter,
gl.TRIANGLES,
depthMode,
stencilMode,
ColorMode.alphaBlended,
CullFaceMode.disabled,
debugUniformValues(posMatrix, index$1.Color.transparent.toPremultipliedRenderColor(null), scaleRatio),
id,
debugTextBuffer,
debugTextIndexBuffer,
debugTextSegments,
null,
null,
null,
[tile._globeTileDebugTextBuffer]
);
}
function drawCrosshair(painter, x, y, color) {
const size = 20;
const lineWidth = 2;
drawDebugSSRect(painter, x - lineWidth / 2, y - size / 2, lineWidth, size, color);
drawDebugSSRect(painter, x - size / 2, y - lineWidth / 2, size, lineWidth, color);
}
function drawHorizontalLine(painter, y, lineWidth, color) {
drawDebugSSRect(painter, 0, y + lineWidth / 2, painter.transform.width, lineWidth, color);
}
function drawVerticalLine(painter, x, lineWidth, color) {
drawDebugSSRect(painter, x - lineWidth / 2, 0, lineWidth, painter.transform.height, color);
}
function drawDebugSSRect(painter, x, y, width, height, color) {
const context = painter.context;
const gl = context.gl;
gl.enable(gl.SCISSOR_TEST);
gl.scissor(x * index$1.exported$1.devicePixelRatio, y * index$1.exported$1.devicePixelRatio, width * index$1.exported$1.devicePixelRatio, height * index$1.exported$1.devicePixelRatio);
context.clear({ color });
gl.disable(gl.SCISSOR_TEST);
}
function drawTileQueryGeometry(painter, sourceCache, coord) {
const context = painter.context;
const gl = context.gl;
const posMatrix = coord.projMatrix;
const program = painter.getOrCreateProgram("debug");
const tile = sourceCache.getTileByID(coord.key);
if (painter.terrain) painter.terrain.setupElevationDraw(tile, program);
const depthMode = DepthMode.disabled;
const stencilMode = StencilMode.disabled;
const colorMode = painter.colorModeForRenderPass();
const id = "$debug";
context.activeTexture.set(gl.TEXTURE0);
painter.emptyTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
const queryViz = tile.queryGeometryDebugViz;
const boundsViz = tile.queryBoundsDebugViz;
if (queryViz && queryViz.vertices.length > 0) {
queryViz.lazyUpload(context);
const vertexBuffer = queryViz.vertexBuffer;
const indexBuffer = queryViz.indexBuffer;
const segments = queryViz.segments;
if (vertexBuffer != null && indexBuffer != null && segments != null) {
program.draw(
painter,
gl.LINE_STRIP,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
debugUniformValues(posMatrix, queryViz.color.toPremultipliedRenderColor(null)),
id,
vertexBuffer,
indexBuffer,
segments
);
}
}
if (boundsViz && boundsViz.vertices.length > 0) {
boundsViz.lazyUpload(context);
const vertexBuffer = boundsViz.vertexBuffer;
const indexBuffer = boundsViz.indexBuffer;
const segments = boundsViz.segments;
if (vertexBuffer != null && indexBuffer != null && segments != null) {
program.draw(
painter,
gl.LINE_STRIP,
depthMode,
stencilMode,
colorMode,
CullFaceMode.disabled,
debugUniformValues(posMatrix, boundsViz.color.toPremultipliedRenderColor(null)),
id,
vertexBuffer,
indexBuffer,
segments
);
}
}
}
function drawTextToOverlay(painter, text) {
painter.initDebugOverlayCanvas();
const canvas = painter.debugOverlayCanvas;
const gl = painter.context.gl;
const ctx2d = painter.debugOverlayCanvas.getContext("2d");
ctx2d.clearRect(0, 0, canvas.width, canvas.height);
ctx2d.shadowColor = "white";
ctx2d.shadowBlur = 2;
ctx2d.lineWidth = 1.5;
ctx2d.strokeStyle = "white";
ctx2d.textBaseline = "top";
ctx2d.font = `bold ${36}px Open Sans, sans-serif`;
ctx2d.fillText(text, 5, 5);
ctx2d.strokeText(text, 5, 5);
painter.debugOverlayTexture.update(canvas);
painter.debugOverlayTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
}
function drawCustom(painter, sourceCache, layer, coords) {
const context = painter.context;
const implementation = layer.implementation;
if (painter.transform.projection.unsupportedLayers && painter.transform.projection.unsupportedLayers.includes("custom") && !(painter.terrain && (painter.terrain.renderingToTexture || painter.renderPass === "offscreen") && layer.isDraped(sourceCache))) {
index$1.warnOnce("Custom layers are not yet supported with this projection. Use mercator or globe to enable usage of custom layers.");
return;
}
if (painter.renderPass === "offscreen") {
const prerender = implementation.prerender;
if (prerender) {
painter.setCustomLayerDefaults();
context.setColorMode(painter.colorModeForRenderPass());
if (painter.transform.projection.name === "globe") {
const center = painter.transform.pointMerc;
prerender.call(implementation, context.gl, painter.transform.customLayerMatrix(), painter.transform.getProjection(), painter.transform.globeToMercatorMatrix(), index$1.globeToMercatorTransition(painter.transform.zoom), [center.x, center.y], painter.transform.pixelsPerMeterRatio);
} else {
prerender.call(implementation, context.gl, painter.transform.customLayerMatrix());
}
context.setDirty();
painter.setBaseState();
}
} else if (painter.renderPass === "translucent") {
if (painter.terrain && painter.terrain.renderingToTexture) {
index$1.assert(implementation.renderToTile);
index$1.assert(coords.length === 1);
const renderToTile = implementation.renderToTile;
if (renderToTile) {
const c = coords[0].canonical;
const renderCoords = {
/*
* We intentionally baked wrap into x coordinate before and
* we need to keep backward-compatibility.
*
* https://github.com/mapbox/mapbox-gl-js/pull/12182/commits/8b9071f751b9ed9ae4389dce7fb2e30aae984f9d
*/
x: c.x + coords[0].wrap * (implementation.wrapTileId ? 0 : 1 << c.z),
y: c.y,
z: c.z
};
context.setDepthMode(DepthMode.disabled);
context.setStencilMode(StencilMode.disabled);
context.setColorMode(painter.colorModeForRenderPass());
painter.setCustomLayerDefaults();
renderToTile.call(implementation, context.gl, renderCoords);
context.setDirty();
painter.setBaseState();
}
return;
}
painter.setCustomLayerDefaults();
context.setColorMode(painter.colorModeForRenderPass());
context.setStencilMode(StencilMode.disabled);
const depthMode = implementation.renderingMode === "3d" ? new DepthMode(painter.context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D) : painter.depthModeForSublayer(0, DepthMode.ReadOnly);
context.setDepthMode(depthMode);
if (painter.transform.projection.name === "globe") {
const center = painter.transform.pointMerc;
implementation.render(context.gl, painter.transform.customLayerMatrix(), painter.transform.getProjection(), painter.transform.globeToMercatorMatrix(), index$1.globeToMercatorTransition(painter.transform.zoom), [center.x, center.y], painter.transform.pixelsPerMeterRatio);
} else {
implementation.render(context.gl, painter.transform.customLayerMatrix());
}
context.setDirty();
painter.setBaseState();
context.bindFramebuffer.set(null);
}
}
const skyboxAttributes = index$1.createLayout([
{ name: "a_pos_3f", components: 3, type: "Float32" }
]);
const { members, size, alignment } = skyboxAttributes;
function addVertex(vertexArray, x, y, z) {
vertexArray.emplaceBack(
// a_pos
x,
y,
z
);
}
class SkyboxGeometry {
constructor(context) {
this.vertexArray = new index$1.StructArrayLayout3f12();
this.indices = new index$1.StructArrayLayout3ui6();
addVertex(this.vertexArray, -1, -1, 1);
addVertex(this.vertexArray, 1, -1, 1);
addVertex(this.vertexArray, -1, 1, 1);
addVertex(this.vertexArray, 1, 1, 1);
addVertex(this.vertexArray, -1, -1, -1);
addVertex(this.vertexArray, 1, -1, -1);
addVertex(this.vertexArray, -1, 1, -1);
addVertex(this.vertexArray, 1, 1, -1);
this.indices.emplaceBack(5, 1, 3);
this.indices.emplaceBack(3, 7, 5);
this.indices.emplaceBack(6, 2, 0);
this.indices.emplaceBack(0, 4, 6);
this.indices.emplaceBack(2, 6, 7);
this.indices.emplaceBack(7, 3, 2);
this.indices.emplaceBack(5, 4, 0);
this.indices.emplaceBack(0, 1, 5);
this.indices.emplaceBack(0, 2, 3);
this.indices.emplaceBack(3, 1, 0);
this.indices.emplaceBack(7, 6, 4);
this.indices.emplaceBack(4, 5, 7);
this.vertexBuffer = context.createVertexBuffer(this.vertexArray, members);
this.indexBuffer = context.createIndexBuffer(this.indices);
this.segment = index$1.SegmentVector.simpleSegment(0, 0, 36, 12);
}
}
function drawSky(painter, sourceCache, layer) {
const tr = painter.transform;
const transitionOpacity = !painter._atmosphere ? 1 : index$1.globeToMercatorTransition(tr.zoom);
const opacity = layer.paint.get("sky-opacity") * transitionOpacity;
if (opacity === 0) {
return;
}
const context = painter.context;
const type = layer.paint.get("sky-type");
const depthMode = new DepthMode(context.gl.LEQUAL, DepthMode.ReadOnly, [0, 1]);
const temporalOffset = painter.frameCounter / 1e3 % 1;
if (type === "atmosphere") {
if (painter.renderPass === "offscreen") {
if (layer.needsSkyboxCapture(painter)) {
captureSkybox(painter, layer, 32, 32);
layer.markSkyboxValid(painter);
}
} else if (painter.renderPass === "sky") {
drawSkyboxFromCapture(painter, layer, depthMode, opacity, temporalOffset);
}
} else if (type === "gradient") {
if (painter.renderPass === "sky") {
drawSkyboxGradient(painter, layer, depthMode, opacity, temporalOffset);
}
} else {
index$1.assert(false, `${type} is unsupported sky-type`);
}
}
function drawSkyboxGradient(painter, layer, depthMode, opacity, temporalOffset) {
const context = painter.context;
const gl = context.gl;
const transform = painter.transform;
const program = painter.getOrCreateProgram("skyboxGradient");
if (!layer.skyboxGeometry) {
layer.skyboxGeometry = new SkyboxGeometry(context);
}
context.activeTexture.set(gl.TEXTURE0);
let colorRampTexture = layer.colorRampTexture;
if (!colorRampTexture) {
colorRampTexture = layer.colorRampTexture = new index$1.Texture(context, layer.colorRamp, gl.RGBA8);
}
colorRampTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
const uniformValues = skyboxGradientUniformValues(
transform.skyboxMatrix,
layer.getCenter(painter, false),
layer.paint.get("sky-gradient-radius"),
opacity,
temporalOffset
);
painter.uploadCommonUniforms(context, program);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
painter.colorModeForRenderPass(),
CullFaceMode.backCW,
uniformValues,
"skyboxGradient",
layer.skyboxGeometry.vertexBuffer,
layer.skyboxGeometry.indexBuffer,
layer.skyboxGeometry.segment
);
}
function drawSkyboxFromCapture(painter, layer, depthMode, opacity, temporalOffset) {
const context = painter.context;
const gl = context.gl;
const transform = painter.transform;
const program = painter.getOrCreateProgram("skybox");
context.activeTexture.set(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, layer.skyboxTexture);
const uniformValues = skyboxUniformValues(transform.skyboxMatrix, layer.getCenter(painter, false), 0, opacity, temporalOffset);
painter.uploadCommonUniforms(context, program);
program.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
painter.colorModeForRenderPass(),
CullFaceMode.backCW,
uniformValues,
"skybox",
layer.skyboxGeometry.vertexBuffer,
layer.skyboxGeometry.indexBuffer,
layer.skyboxGeometry.segment
);
}
function drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, i) {
const context = painter.context;
const gl = context.gl;
const atmosphereColor = layer.paint.get("sky-atmosphere-color");
const atmosphereHaloColor = layer.paint.get("sky-atmosphere-halo-color");
const sunIntensity = layer.paint.get("sky-atmosphere-sun-intensity");
const uniformValues = skyboxCaptureUniformValues(
index$1.fromMat4(index$1.create$3(), faceRotate),
sunDirection,
sunIntensity,
atmosphereColor.toPremultipliedRenderColor(null),
atmosphereHaloColor.toPremultipliedRenderColor(null)
);
const glFace = gl.TEXTURE_CUBE_MAP_POSITIVE_X + i;
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, glFace, layer.skyboxTexture, 0);
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.unblended,
CullFaceMode.frontCW,
uniformValues,
"skyboxCapture",
layer.skyboxGeometry.vertexBuffer,
layer.skyboxGeometry.indexBuffer,
layer.skyboxGeometry.segment
);
}
function captureSkybox(painter, layer, width, height) {
const context = painter.context;
const gl = context.gl;
let fbo = layer.skyboxFbo;
if (!fbo) {
fbo = layer.skyboxFbo = context.createFramebuffer(width, height, 1, null);
layer.skyboxGeometry = new SkyboxGeometry(context);
layer.skyboxTexture = context.gl.createTexture();
gl.bindTexture(gl.TEXTURE_CUBE_MAP, layer.skyboxTexture);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
for (let i = 0; i < 6; ++i) {
const glFace = gl.TEXTURE_CUBE_MAP_POSITIVE_X + i;
gl.texImage2D(glFace, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
}
}
context.bindFramebuffer.set(fbo.framebuffer);
context.viewport.set([0, 0, width, height]);
const sunDirection = layer.getCenter(painter, true);
const program = painter.getOrCreateProgram("skyboxCapture");
const faceRotate = new Float64Array(16);
index$1.identity(faceRotate);
index$1.rotateY(faceRotate, faceRotate, -Math.PI * 0.5);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 0);
index$1.identity(faceRotate);
index$1.rotateY(faceRotate, faceRotate, Math.PI * 0.5);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 1);
index$1.identity(faceRotate);
index$1.rotateX$1(faceRotate, faceRotate, -Math.PI * 0.5);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 2);
index$1.identity(faceRotate);
index$1.rotateX$1(faceRotate, faceRotate, Math.PI * 0.5);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 3);
index$1.identity(faceRotate);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 4);
index$1.identity(faceRotate);
index$1.rotateY(faceRotate, faceRotate, Math.PI);
drawSkyboxFace(painter, layer, program, faceRotate, sunDirection, 5);
context.viewport.set([0, 0, painter.width, painter.height]);
}
const atmosphereLayout = index$1.createLayout([
{ type: "Float32", name: "a_pos", components: 3 },
{ type: "Float32", name: "a_uv", components: 2 }
]);
class AtmosphereBuffer {
constructor(context) {
const vertices = new index$1.StructArrayLayout5f20();
vertices.emplaceBack(-1, 1, 1, 0, 0);
vertices.emplaceBack(1, 1, 1, 1, 0);
vertices.emplaceBack(1, -1, 1, 1, 1);
vertices.emplaceBack(-1, -1, 1, 0, 1);
const triangles = new index$1.StructArrayLayout3ui6();
triangles.emplaceBack(0, 1, 2);
triangles.emplaceBack(2, 3, 0);
this.vertexBuffer = context.createVertexBuffer(vertices, atmosphereLayout.members);
this.indexBuffer = context.createIndexBuffer(triangles);
this.segments = index$1.SegmentVector.simpleSegment(0, 0, 4, 2);
}
destroy() {
this.vertexBuffer.destroy();
this.indexBuffer.destroy();
this.segments.destroy();
}
}
const starsLayout = index$1.createLayout([
{ type: "Float32", name: "a_pos_3f", components: 3 },
{ type: "Float32", name: "a_uv", components: 2 },
{ type: "Float32", name: "a_size_scale", components: 1 },
{ type: "Float32", name: "a_fade_opacity", components: 1 }
]);
function generateUniformDistributedPointsOnSphere(pointsCount) {
const sRand = index$1.mulberry32(30);
const points = [];
for (let i = 0; i < pointsCount; ++i) {
const lon = 2 * Math.PI * sRand();
const lat = Math.acos(1 - 2 * sRand()) - Math.PI * 0.5;
points.push(index$1.fromValues$2(Math.cos(lat) * Math.cos(lon), Math.cos(lat) * Math.sin(lon), Math.sin(lat)));
}
return points;
}
class StarsParams {
constructor() {
this.starsCount = 16e3;
this.sizeMultiplier = 0.15;
this.sizeRange = 100;
this.intensityRange = 200;
}
}
class Atmosphere {
constructor(painter) {
this.colorModeAlphaBlendedWriteRGB = new ColorMode([ONE, ONE_MINUS_SRC_ALPHA, ONE, ONE_MINUS_SRC_ALPHA], index$1.Color.transparent, [true, true, true, false]);
this.colorModeWriteAlpha = new ColorMode([ONE, ZERO, ONE, ZERO], index$1.Color.transparent, [false, false, false, true]);
this.params = new StarsParams();
this.updateNeeded = true;
DevTools.addParameter(this.params, "starsCount", "Stars", { min: 100, max: 16e3, step: 1 }, () => {
this.updateNeeded = true;
});
DevTools.addParameter(this.params, "sizeMultiplier", "Stars", { min: 0.01, max: 2, step: 0.01 });
DevTools.addParameter(this.params, "sizeRange", "Stars", { min: 0, max: 200, step: 1 }, () => {
this.updateNeeded = true;
});
DevTools.addParameter(this.params, "intensityRange", "Stars", { min: 0, max: 200, step: 1 }, () => {
this.updateNeeded = true;
});
}
update(painter) {
const context = painter.context;
if (!this.atmosphereBuffer || this.updateNeeded) {
this.updateNeeded = false;
this.atmosphereBuffer = new AtmosphereBuffer(context);
const sizeRange = this.params.sizeRange;
const intensityRange = this.params.intensityRange;
const stars = generateUniformDistributedPointsOnSphere(this.params.starsCount);
const sRand = index$1.mulberry32(300);
const vertices = new index$1.StructArrayLayout7f28();
const triangles = new index$1.StructArrayLayout3ui6();
let base = 0;
for (let i = 0; i < stars.length; ++i) {
const star = index$1.scale$1([], stars[i], 200);
const size = Math.max(0, 1 + 0.01 * sizeRange * (-0.5 + 1 * sRand()));
const intensity = Math.max(0, 1 + 0.01 * intensityRange * (-0.5 + 1 * sRand()));
vertices.emplaceBack(star[0], star[1], star[2], -1, -1, size, intensity);
vertices.emplaceBack(star[0], star[1], star[2], 1, -1, size, intensity);
vertices.emplaceBack(star[0], star[1], star[2], 1, 1, size, intensity);
vertices.emplaceBack(star[0], star[1], star[2], -1, 1, size, intensity);
triangles.emplaceBack(base + 0, base + 1, base + 2);
triangles.emplaceBack(base + 0, base + 2, base + 3);
base += 4;
}
this.starsVx = context.createVertexBuffer(vertices, starsLayout.members);
this.starsIdx = context.createIndexBuffer(triangles);
this.starsSegments = index$1.SegmentVector.simpleSegment(0, 0, vertices.length, triangles.length);
}
}
destroy() {
if (this.atmosphereBuffer) {
this.atmosphereBuffer.destroy();
}
if (this.starsVx) {
this.starsVx.destroy();
}
if (this.starsIdx) {
this.starsIdx.destroy();
}
}
drawAtmosphereGlow(painter, fog) {
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const depthMode = new DepthMode(gl.LEQUAL, DepthMode.ReadOnly, [0, 1]);
const transitionT = index$1.globeToMercatorTransition(tr.zoom);
const fogLUT = painter.style.getLut(fog.scope);
const colorIgnoreLut = fog.properties.get("color-use-theme") === "none";
const fogColor = fog.properties.get("color").toNonPremultipliedRenderColor(colorIgnoreLut ? null : fogLUT);
const hignoreLutIgnoreLut = fog.properties.get("high-color-use-theme") === "none";
const highColor = fog.properties.get("high-color").toNonPremultipliedRenderColor(hignoreLutIgnoreLut ? null : fogLUT);
const spaceColorIgnoreLut = fog.properties.get("space-color-use-theme") === "none";
const spaceColor = fog.properties.get("space-color").toNonPremultipliedRenderColor(spaceColorIgnoreLut ? null : fogLUT);
const minHorizonBlend = 5e-4;
const horizonBlend = index$1.mapValue(fog.properties.get("horizon-blend"), 0, 1, minHorizonBlend, 0.25);
const globeRadius = index$1.globeUseCustomAntiAliasing(painter, context, tr) && horizonBlend === minHorizonBlend ? tr.worldSize / (2 * Math.PI * 1.025) - 1 : tr.globeRadius;
const temporalOffset = painter.frameCounter / 1e3 % 1;
const globeCenterInViewSpace = tr.globeCenterInViewSpace;
const globeCenterDistance = index$1.length(globeCenterInViewSpace);
const distanceToHorizon = Math.sqrt(Math.pow(globeCenterDistance, 2) - Math.pow(globeRadius, 2));
const horizonAngle = Math.acos(distanceToHorizon / globeCenterDistance);
const draw = (alphaPass) => {
const defines = tr.projection.name === "globe" ? ["PROJECTION_GLOBE_VIEW", "FOG"] : ["FOG"];
if (alphaPass) {
defines.push("ALPHA_PASS");
}
const program = painter.getOrCreateProgram("globeAtmosphere", { defines });
const uniforms = atmosphereUniformValues(
tr.frustumCorners.TL,
tr.frustumCorners.TR,
tr.frustumCorners.BR,
tr.frustumCorners.BL,
tr.frustumCorners.horizon,
transitionT,
horizonBlend,
fogColor,
highColor,
spaceColor,
temporalOffset,
horizonAngle
);
painter.uploadCommonUniforms(context, program);
const buffer = this.atmosphereBuffer;
const colorMode = alphaPass ? this.colorModeWriteAlpha : this.colorModeAlphaBlendedWriteRGB;
const name = alphaPass ? "atmosphere_glow_alpha" : "atmosphere_glow";
if (buffer) {
program.draw(
painter,
gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.backCW,
uniforms,
name,
buffer.vertexBuffer,
buffer.indexBuffer,
buffer.segments
);
}
};
draw(false);
draw(true);
}
drawStars(painter, fog) {
const starIntensity = index$1.clamp(fog.properties.get("star-intensity"), 0, 1);
if (starIntensity === 0) {
return;
}
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const program = painter.getOrCreateProgram("stars");
const orientation = index$1.identity$1([]);
index$1.rotateX(orientation, orientation, -tr._pitch);
index$1.rotateZ$1(orientation, orientation, -tr.angle);
index$1.rotateX(orientation, orientation, index$1.degToRad(tr._center.lat));
index$1.rotateY$1(orientation, orientation, -index$1.degToRad(tr._center.lng));
const rotationMatrix = index$1.fromQuat(new Float32Array(16), orientation);
const mvp = index$1.multiply([], tr.starsProjMatrix, rotationMatrix);
const modelView3 = index$1.fromMat4([], rotationMatrix);
const modelviewInv = index$1.invert$2([], modelView3);
const camUp = [0, 1, 0];
index$1.transformMat3(camUp, camUp, modelviewInv);
index$1.scale$1(camUp, camUp, this.params.sizeMultiplier);
const camRight = [1, 0, 0];
index$1.transformMat3(camRight, camRight, modelviewInv);
index$1.scale$1(camRight, camRight, this.params.sizeMultiplier);
const uniforms = starsUniformValues(
mvp,
camUp,
camRight,
starIntensity
);
painter.uploadCommonUniforms(context, program);
if (this.starsVx && this.starsIdx) {
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
this.colorModeAlphaBlendedWriteRGB,
CullFaceMode.disabled,
uniforms,
"atmosphere_stars",
this.starsVx,
this.starsIdx,
this.starsSegments
);
}
}
}
class BuildingTileBorderManager {
constructor() {
this.visibleTiles = [];
}
updateBorders(source, layer) {
const visibleTiles = [];
const buckets = [];
const coords = source._getRenderableCoordinates(false, true);
for (const coord of coords) {
const tile = source.getTile(coord);
if (!tile.hasData()) {
continue;
}
const bucket = tile.getBucket(layer);
if (!bucket) {
continue;
}
if (bucket.isEmpty()) {
continue;
}
visibleTiles.push(coord.key);
buckets.push({ bucket, tileID: coord.canonical });
}
let bordersChanged = visibleTiles.length !== this.visibleTiles.length;
if (!bordersChanged) {
visibleTiles.sort();
for (let i = 0; i < visibleTiles.length; i++) {
if (visibleTiles[i] !== this.visibleTiles[i]) {
bordersChanged = true;
break;
}
}
}
if (!bordersChanged) {
return;
}
const uniqueFeatureIDInstances = /* @__PURE__ */ new Set();
this.visibleTiles = visibleTiles;
buckets.sort((lhs, rhs) => {
return lhs.tileID.z - rhs.tileID.z || lhs.tileID.x - rhs.tileID.x || lhs.tileID.y - rhs.tileID.y;
});
for (const bucketAndTileID of buckets) {
const footprintsToShowIndices = new Array();
const footprintsToHideIndices = new Array();
const bucket = bucketAndTileID.bucket;
for (const featureOnBorder of bucket.featuresOnBorder) {
if (!uniqueFeatureIDInstances.has(featureOnBorder.featureId)) {
uniqueFeatureIDInstances.add(featureOnBorder.featureId);
footprintsToShowIndices.push(featureOnBorder.footprintIndex);
} else {
footprintsToHideIndices.push(featureOnBorder.footprintIndex);
}
}
bucket.updateFootprintHiddenFlags(footprintsToShowIndices, index$1.BUILDING_HIDDEN_BY_TILE_BORDER_DEDUPLICATION, false);
bucket.updateFootprintHiddenFlags(footprintsToHideIndices, index$1.BUILDING_HIDDEN_BY_TILE_BORDER_DEDUPLICATION, true);
}
}
}
function fogMatrixForModel(modelMatrix, transform) {
const fogMatrix = [...modelMatrix];
const scale = transform.cameraWorldSizeForFog / transform.worldSize;
const scaleMatrix = index$1.identity([]);
index$1.scale$3(scaleMatrix, scaleMatrix, [scale, scale, 1]);
index$1.multiply(fogMatrix, scaleMatrix, fogMatrix);
index$1.multiply(fogMatrix, transform.worldToFogMatrix, fogMatrix);
return fogMatrix;
}
function setupMeshDraw(definesValues, dynamicBuffers, mesh, painter, lut) {
const material = mesh.material;
const context = painter.context;
const { baseColorTexture, metallicRoughnessTexture } = material.pbrMetallicRoughness;
const { normalTexture, occlusionTexture, emissionTexture } = material;
function setupTexture(texture, define, slot) {
if (!texture) return;
definesValues.push(define);
context.activeTexture.set(context.gl.TEXTURE0 + slot);
if (texture.gfxTexture) {
const { minFilter, magFilter, wrapS, wrapT } = texture.sampler;
texture.gfxTexture.bindExtraParam(minFilter, magFilter, wrapS, wrapT);
}
}
setupTexture(baseColorTexture, "HAS_TEXTURE_u_baseColorTexture", TextureSlots.BaseColor);
setupTexture(metallicRoughnessTexture, "HAS_TEXTURE_u_metallicRoughnessTexture", TextureSlots.MetallicRoughness);
setupTexture(normalTexture, "HAS_TEXTURE_u_normalTexture", TextureSlots.Normal);
setupTexture(occlusionTexture, "HAS_TEXTURE_u_occlusionTexture", TextureSlots.Occlusion);
setupTexture(emissionTexture, "HAS_TEXTURE_u_emissionTexture", TextureSlots.Emission);
if (lut) {
if (!lut.texture) {
lut.texture = new index$1.Texture3D(painter.context, lut.image, [lut.image.height, lut.image.height, lut.image.height], context.gl.RGBA8);
}
context.activeTexture.set(context.gl.TEXTURE0 + TextureSlots.LUT);
if (lut.texture) {
lut.texture.bind(context.gl.LINEAR, context.gl.CLAMP_TO_EDGE);
}
definesValues.push("APPLY_LUT_ON_GPU");
}
if (mesh.texcoordBuffer) {
definesValues.push("HAS_ATTRIBUTE_a_uv_2f");
dynamicBuffers.push(mesh.texcoordBuffer);
}
if (mesh.colorBuffer) {
const colorDefine = mesh.colorBuffer.itemSize === 12 ? "HAS_ATTRIBUTE_a_color_3f" : "HAS_ATTRIBUTE_a_color_4f";
definesValues.push(colorDefine);
dynamicBuffers.push(mesh.colorBuffer);
}
if (mesh.normalBuffer) {
definesValues.push("HAS_ATTRIBUTE_a_normal_3f");
dynamicBuffers.push(mesh.normalBuffer);
}
if (mesh.pbrBuffer) {
definesValues.push("HAS_ATTRIBUTE_a_pbr");
definesValues.push("HAS_ATTRIBUTE_a_heightBasedEmissiveStrength");
dynamicBuffers.push(mesh.pbrBuffer);
}
if (material.alphaMode === "OPAQUE" || material.alphaMode === "MASK") {
definesValues.push("UNPREMULT_TEXTURE_IN_SHADER");
}
if (!material.defined) {
definesValues.push("DIFFUSE_SHADED");
}
const shadowRenderer = painter.shadowRenderer;
if (shadowRenderer) {
definesValues.push("RENDER_SHADOWS", "DEPTH_TEXTURE");
if (shadowRenderer.useNormalOffset) {
definesValues.push("NORMAL_OFFSET");
}
}
}
function drawMesh(sortedMesh, painter, layer, modelParameters, stencilMode, colorMode) {
const opacity = sortedMesh.modelOpacity;
index$1.assert(opacity > 0);
const context = painter.context;
const depthMode = new DepthMode(painter.context.gl.LEQUAL, sortedMesh.isLightMesh ? DepthMode.ReadOnly : DepthMode.ReadWrite, painter.depthRangeFor3D);
const tr = painter.transform;
const mesh = sortedMesh.mesh;
const material = mesh.material;
const pbr = material.pbrMetallicRoughness;
const fog = painter.style.fog;
let lightingMatrix;
if (painter.transform.projection.zAxisUnit === "pixels") {
lightingMatrix = [...sortedMesh.nodeModelMatrix];
} else {
lightingMatrix = index$1.multiply([], modelParameters.zScaleMatrix, sortedMesh.nodeModelMatrix);
}
index$1.multiply(lightingMatrix, modelParameters.negCameraPosMatrix, lightingMatrix);
const normalMatrix = index$1.invert([], lightingMatrix);
index$1.transpose(normalMatrix, normalMatrix);
const ignoreLut = layer.paint.get("model-color-use-theme").constantOr("default") === "none";
const emissiveStrength = layer.paint.get("model-emissive-strength").constantOr(0);
const uniformValues = modelUniformValues(
new Float32Array(sortedMesh.worldViewProjection),
new Float32Array(lightingMatrix),
new Float32Array(normalMatrix),
null,
painter,
opacity,
pbr.baseColorFactor,
material.emissiveFactor,
pbr.metallicFactor,
pbr.roughnessFactor,
material,
emissiveStrength,
layer,
void 0,
void 0,
sortedMesh.materialOverride,
sortedMesh.modelColor
);
const programOptions = {
defines: []
};
const dynamicBuffers = [];
const shadowRenderer = painter.shadowRenderer;
if (shadowRenderer) {
shadowRenderer.useNormalOffset = false;
}
setupMeshDraw(programOptions.defines, dynamicBuffers, mesh, painter, ignoreLut ? null : layer.lut);
let fogMatrixArray = null;
if (fog) {
const fogMatrix = fogMatrixForModel(sortedMesh.nodeModelMatrix, painter.transform);
fogMatrixArray = new Float32Array(fogMatrix);
if (tr.projection.name !== "globe") {
const min = mesh.aabb.min;
const max = mesh.aabb.max;
const [minOpacity, maxOpacity] = fog.getOpacityForBounds(fogMatrix, min[0], min[1], max[0], max[1]);
programOptions.overrideFog = minOpacity >= FOG_OPACITY_THRESHOLD || maxOpacity >= FOG_OPACITY_THRESHOLD;
}
}
const cutoffParams = getCutoffParams(painter, layer.paint.get("model-cutoff-fade-range"));
if (cutoffParams.shouldRenderCutoff) {
programOptions.defines.push("RENDER_CUTOFF");
}
const program = painter.getOrCreateProgram("model", programOptions);
painter.uploadCommonUniforms(context, program, null, fogMatrixArray, cutoffParams);
const isShadowPass = painter.renderPass === "shadow";
if (!isShadowPass && shadowRenderer) {
shadowRenderer.setupShadowsFromMatrix(sortedMesh.nodeModelMatrix, program);
}
const cullFaceMode = mesh.material.doubleSided ? CullFaceMode.disabled : CullFaceMode.backCCW;
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
stencilMode,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
void 0,
dynamicBuffers
);
}
function getScope(painter, layer) {
return painter.style._importedAsBasemap ? "basemap" : layer.scope;
}
function prepare$1(layer, sourceCache, painter) {
const modelSource = sourceCache.getSource();
if (!modelSource.loaded()) return;
if (modelSource.type === "vector" || modelSource.type === "geojson") {
if (painter.modelManager) {
painter.modelManager.upload(painter, getScope(painter, layer));
}
return;
}
if (modelSource.type === "batched-model") {
return;
}
if (modelSource.type !== "model") return;
const models = modelSource.getModels();
for (const model of models) {
model.upload(painter.context);
}
}
function prepareMeshes(painter, node, modelMatrix, projectionMatrix, modelIndex, transparentMeshes, opaqueMeshes, materialOverrides, modelOpacity, modelColorMix) {
const transform = painter.transform;
const isGlobe = transform.projection.name === "globe";
const isShadowPass = painter.renderPass === "shadow";
const isLightMesh = node.isGeometryBloom ? node.isGeometryBloom : false;
if (isLightMesh && isShadowPass) return;
const nodeModelMatrix = isGlobe ? index$1.convertModelMatrixForGlobe(modelMatrix, transform) : [...modelMatrix];
index$1.multiply(nodeModelMatrix, nodeModelMatrix, node.globalMatrix);
const worldViewProjection = index$1.multiply([], projectionMatrix, nodeModelMatrix);
if (node.meshes) {
for (const mesh of node.meshes) {
const materialOverride = materialOverrides.get(mesh.material.name);
if (materialOverride && materialOverride.opacity <= 0) continue;
if (mesh.material.alphaMode !== "BLEND") {
const opaqueMesh = { mesh, depth: 0, modelIndex, worldViewProjection, nodeModelMatrix, isLightMesh, materialOverride, modelOpacity, modelColor: modelColorMix };
opaqueMeshes.push(opaqueMesh);
continue;
}
const centroidPos = index$1.transformMat4([], mesh.centroid, worldViewProjection);
if (!transform.isOrthographic && centroidPos[2] <= 0) continue;
const transparentMesh = { mesh, depth: centroidPos[2], modelIndex, worldViewProjection, nodeModelMatrix, isLightMesh, materialOverride, modelOpacity, modelColor: modelColorMix };
transparentMeshes.push(transparentMesh);
}
}
if (node.children) {
for (const child of node.children) {
prepareMeshes(painter, child, modelMatrix, projectionMatrix, modelIndex, transparentMeshes, opaqueMeshes, materialOverrides, modelOpacity, modelColorMix);
}
}
}
function drawShadowCaster(mesh, matrix, painter, layer) {
const shadowRenderer = painter.shadowRenderer;
if (!shadowRenderer) return;
const depthMode = shadowRenderer.getShadowPassDepthMode();
const colorMode = shadowRenderer.getShadowPassColorMode();
const shadowMatrix = shadowRenderer.calculateShadowPassMatrixFromMatrix(matrix);
const uniformValues = modelDepthUniformValues(shadowMatrix);
const definesValues = painter._shadowMapDebug ? [] : ["DEPTH_TEXTURE"];
const program = painter.getOrCreateProgram("modelDepth", { defines: definesValues });
const context = painter.context;
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.disabled,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
void 0,
void 0
);
}
function evaluateFeatureStateForNodeOverrides(layer, featureId, featureState, featureProperties, nodeNamesToEvaluate, nodeOverrides) {
for (const nodeId of nodeNamesToEvaluate) {
const partProperties = Object.assign({}, featureProperties);
partProperties["part"] = nodeId;
const part = {
type: "Unknown",
id: featureId,
properties: partProperties
};
const nodeOverride = {
orientation: layer.paint.get("model-rotation").evaluate(part, featureState)
};
nodeOverrides.set(nodeId, nodeOverride);
}
}
function evaluateFeatureStateForMaterialOverrides(layer, featureId, featureState, featureProperties, materialNamesToEvaluate, materialOverrides) {
for (const materialId of materialNamesToEvaluate) {
const partProperties = Object.assign({}, featureProperties);
partProperties["part"] = materialId;
const part = {
type: "Unknown",
id: featureId,
properties: partProperties
};
const materialOverride = {
color: layer.paint.get("model-color").evaluate(part, featureState),
colorMix: layer.paint.get("model-color-mix-intensity").evaluate(part, featureState),
opacity: layer.paint.get("model-opacity").evaluate(part, featureState),
emissionStrength: layer.paint.get("model-emissive-strength").evaluate(part, featureState)
};
materialOverrides.set(materialId, materialOverride);
}
}
function drawModels(painter, sourceCache, layer, coords) {
if (painter.renderPass === "opaque") {
return;
}
const opacity = layer.paint.get("model-opacity").constantOr(1);
const elevationReference = layer.paint.get("model-elevation-reference");
const shouldFollowTerrainSlope = elevationReference === "ground";
const shouldApplyElevation = elevationReference === "ground";
if (opacity === 0) {
return;
}
const castShadows = layer.paint.get("model-cast-shadows");
if (painter.renderPass === "shadow") {
if (!castShadows) {
return;
}
if (painter.terrain) {
const noShadowCutoff = 0.65;
if (opacity < noShadowCutoff) {
const expression = layer._transitionablePaint._values["model-opacity"].value.expression;
if (expression instanceof index$1.ZoomDependentExpression) {
return;
}
}
}
}
const shadowRenderer = painter.shadowRenderer;
const receiveShadows = layer.paint.get("model-receive-shadows");
if (shadowRenderer) {
shadowRenderer.useNormalOffset = true;
if (!receiveShadows) {
shadowRenderer.enabled = false;
}
}
const cleanup = () => {
if (shadowRenderer) {
shadowRenderer.useNormalOffset = true;
if (!receiveShadows) {
shadowRenderer.enabled = true;
}
}
};
const modelSource = sourceCache.getSource();
if (painter.renderPass === "light-beam" && modelSource.type !== "batched-model") {
return;
}
if (modelSource.type === "vector" || modelSource.type === "geojson") {
drawVectorLayerModels(painter, sourceCache, layer, coords, getScope(painter, layer));
cleanup();
return;
}
if (!modelSource.loaded()) return;
if (modelSource.type === "batched-model") {
drawBatchedModels(painter, sourceCache, layer, coords);
cleanup();
return;
}
if (modelSource.type !== "model") return;
const models = modelSource.getModels();
const modelParametersVector = [];
const mercCameraPos = painter.transform.getFreeCameraOptions().position;
const cameraPos = index$1.scale$1([], [mercCameraPos.x, mercCameraPos.y, mercCameraPos.z], painter.transform.worldSize);
index$1.negate(cameraPos, cameraPos);
const transparentMeshes = [];
const opaqueMeshes = [];
let modelIndex = 0;
for (const model of models) {
const modelFeatureState = sourceCache.getFeatureState("", model.id);
const modelFeature = {
type: "Unknown",
id: model.id,
properties: model.featureProperties
};
const rotation = layer.paint.get("model-rotation").evaluate(modelFeature, modelFeatureState);
const scale = layer.paint.get("model-scale").evaluate(modelFeature, modelFeatureState);
const translation = layer.paint.get("model-translation").evaluate(modelFeature, modelFeatureState);
const modelOpacity = layer.paint.get("model-opacity").evaluate(modelFeature, modelFeatureState);
evaluateFeatureStateForNodeOverrides(layer, model.id, modelFeatureState, model.featureProperties, model.nodeOverrideNames, model.nodeOverrides);
evaluateFeatureStateForMaterialOverrides(layer, model.id, modelFeatureState, model.featureProperties, model.materialOverrideNames, model.materialOverrides);
if (model.nodeOverrides.size > 0) {
model.computeBoundsAndApplyParent();
}
model.computeModelMatrix(painter, rotation, scale, translation, shouldApplyElevation, shouldFollowTerrainSlope, false);
const negCameraPosMatrix = index$1.identity([]);
const modelMetersPerPixel = index$1.getMetersPerPixelAtLatitude(model.position.lat, painter.transform.zoom);
const modelPixelsPerMeter = 1 / modelMetersPerPixel;
const zScaleMatrix = index$1.fromScaling([], [1, 1, modelPixelsPerMeter]);
index$1.translate(negCameraPosMatrix, negCameraPosMatrix, cameraPos);
const modelParameters = { zScaleMatrix, negCameraPosMatrix };
modelParametersVector.push(modelParameters);
for (const node of model.nodes) {
prepareMeshes(painter, node, model.matrix, painter.transform.expandedFarZProjMatrix, modelIndex, transparentMeshes, opaqueMeshes, model.materialOverrides, modelOpacity);
}
modelIndex++;
}
transparentMeshes.sort((a, b) => {
return b.depth - a.depth;
});
if (painter.renderPass === "shadow") {
for (const opaqueMesh of opaqueMeshes) {
drawShadowCaster(opaqueMesh.mesh, opaqueMesh.nodeModelMatrix, painter, layer);
}
for (const transparentMesh of transparentMeshes) {
drawShadowCaster(transparentMesh.mesh, transparentMesh.nodeModelMatrix, painter, layer);
}
cleanup();
return;
}
drawSortedMeshes(painter, layer, transparentMeshes, opaqueMeshes, modelParametersVector);
cleanup();
}
function drawSortedMeshes(painter, layer, transparentMeshes, opaqueMeshes, modelParametersVector) {
let needsStencilClear = false;
for (const opaqueMesh of opaqueMeshes) {
if (opaqueMesh.modelOpacity !== 1) {
drawMesh(opaqueMesh, painter, layer, modelParametersVector[opaqueMesh.modelIndex], StencilMode.disabled, ColorMode.disabled);
needsStencilClear = true;
}
}
for (const opaqueMesh of opaqueMeshes) {
if (opaqueMesh.modelOpacity !== 1) {
drawMesh(opaqueMesh, painter, layer, modelParametersVector[opaqueMesh.modelIndex], painter.stencilModeFor3D(), painter.colorModeForRenderPass());
} else {
drawMesh(opaqueMesh, painter, layer, modelParametersVector[opaqueMesh.modelIndex], StencilMode.disabled, painter.colorModeForRenderPass());
}
}
if (needsStencilClear) {
painter.resetStencilClippingMasks();
}
const additiveBlending = ColorMode.additive;
for (const transparentMesh of transparentMeshes) {
drawMesh(transparentMesh, painter, layer, modelParametersVector[transparentMesh.modelIndex], StencilMode.disabled, transparentMesh.isLightMesh ? additiveBlending : painter.colorModeForRenderPass());
}
}
function updateModelBucketsElevation(painter, bucket, bucketTileID) {
let exaggeration = painter.terrain ? painter.terrain.exaggeration() : 0;
let dem;
if (painter.terrain && exaggeration > 0) {
const terrain = painter.terrain;
const demTile = terrain.findDEMTileFor(bucketTileID);
if (demTile && demTile.dem) {
dem = index$1.DEMSampler.create(terrain, bucketTileID, demTile);
} else {
exaggeration = 0;
}
}
if (exaggeration === 0) {
bucket.terrainElevationMin = 0;
bucket.terrainElevationMax = 0;
}
if (exaggeration === bucket.validForExaggeration && (exaggeration === 0 || dem && dem._demTile && dem._demTile.tileID === bucket.validForDEMTile.id && dem._dem._timestamp === bucket.validForDEMTile.timestamp)) {
return false;
}
let elevationMin;
let elevationMax;
for (const modelId in bucket.instancesPerModel) {
const instances = bucket.instancesPerModel[modelId];
index$1.assert(instances.instancedDataArray.bytesPerElement === 64);
for (let i = 0; i < instances.instancedDataArray.length; ++i) {
const x = instances.instancedDataArray.float32[i * 16] | 0;
const y = instances.instancedDataArray.float32[i * 16 + 1] | 0;
const elevation = (dem ? exaggeration * dem.getElevationAt(x, y, true, true) : 0) + instances.instancesEvaluatedElevation[i];
instances.instancedDataArray.float32[i * 16 + 6] = elevation;
elevationMin = elevationMin ? Math.min(bucket.terrainElevationMin, elevation) : elevation;
elevationMax = elevationMax ? Math.max(bucket.terrainElevationMax, elevation) : elevation;
}
}
bucket.terrainElevationMin = elevationMin ? elevationMin : 0;
bucket.terrainElevationMax = elevationMax ? elevationMax : 0;
bucket.validForExaggeration = exaggeration;
bucket.validForDEMTile = dem && dem._demTile ? { id: dem._demTile.tileID, timestamp: dem._dem._timestamp } : { id: void 0, timestamp: 0 };
return true;
}
function updateModelBucketData(painter, bucket, bucketTileID) {
const bucketContentsUpdatedByZoom = bucket.updateZoomBasedPaintProperties();
const bucketContentsUpdatedByElevation = updateModelBucketsElevation(painter, bucket, bucketTileID);
if (bucketContentsUpdatedByZoom || bucketContentsUpdatedByElevation) {
bucket.uploaded = false;
bucket.upload(painter.context);
}
}
const renderData = {
shadowUniformsInitialized: false,
useSingleShadowCascade: false,
tileMatrix: new Float64Array(16),
shadowTileMatrix: new Float32Array(16),
aabb: new index$1.Aabb([0, 0, 0], [index$1.EXTENT, index$1.EXTENT, 0])
};
function calculateTileZoom(id, tr) {
const tiles = 1 << id.canonical.z;
const cameraPos = tr.getFreeCameraOptions().position;
const elevation = tr.elevation;
const minx = id.canonical.x / tiles;
const maxx = (id.canonical.x + 1) / tiles;
const miny = id.canonical.y / tiles;
const maxy = (id.canonical.y + 1) / tiles;
let height = tr._centerAltitude;
if (elevation) {
const minmax = elevation.getMinMaxForTile(id);
if (minmax && minmax.max > height) {
height = minmax.max;
}
}
const distx = index$1.clamp(cameraPos.x, minx, maxx) - cameraPos.x;
const disty = index$1.clamp(cameraPos.y, miny, maxy) - cameraPos.y;
const distz = index$1.mercatorZfromAltitude(height, tr.center.lat) - cameraPos.z;
return tr._zoomFromMercatorZ(Math.sqrt(distx * distx + disty * disty + distz * distz));
}
function drawVectorLayerModels(painter, source, layer, coords, scope) {
const tr = painter.transform;
const isGlobe = tr.projection.name === "globe";
const mercCameraPos = tr.getFreeCameraOptions().position;
if (!painter.modelManager) return;
const modelManager = painter.modelManager;
layer.modelManager = modelManager;
const shadowRenderer = painter.shadowRenderer;
if (!layer._unevaluatedLayout._values.hasOwnProperty("model-id")) {
return;
}
const modelIdUnevaluatedProperty = layer._unevaluatedLayout._values["model-id"];
const evaluationParameters = Object.assign({}, layer.layout.get("model-id").parameters);
const layerIndex = painter.style.order.indexOf(layer.fqid);
const layerOpacity = layer.paint.get("model-opacity").constantOr(1);
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || bucket.projection.name !== tr.projection.name) continue;
const modelUris = bucket.getModelUris();
if (modelUris && !bucket.modelsRequested) {
modelManager.addModelsFromBucket(modelUris, scope);
bucket.modelsRequested = true;
}
if (!isGlobe) {
const tileZoom = calculateTileZoom(coord, tr);
evaluationParameters.zoom = tileZoom;
} else {
evaluationParameters.zoom = coord.overscaledZ;
}
const modelIdProperty = modelIdUnevaluatedProperty.possiblyEvaluate(evaluationParameters);
updateModelBucketData(painter, bucket, coord);
renderData.shadowUniformsInitialized = false;
renderData.useSingleShadowCascade = !!shadowRenderer && shadowRenderer.getMaxCascadeForTile(coord.toUnwrapped()) === 0;
if (painter.renderPass === "shadow" && shadowRenderer) {
if (painter.currentShadowCascade === 1 && bucket.isInsideFirstShadowMapFrustum) continue;
const tileMatrix = tr.calculatePosMatrix(coord.toUnwrapped(), tr.worldSize);
renderData.tileMatrix.set(tileMatrix);
renderData.shadowTileMatrix = Float32Array.from(shadowRenderer.calculateShadowPassMatrixFromMatrix(tileMatrix));
renderData.aabb.min = [0, 0, 0];
renderData.aabb.max[0] = renderData.aabb.max[1] = index$1.EXTENT;
renderData.aabb.max[2] = 0;
if (calculateTileShadowPassCulling(bucket, renderData, painter, layer.scope)) continue;
}
const tiles = 1 << coord.canonical.z;
const cameraPos = [
((mercCameraPos.x - coord.wrap) * tiles - coord.canonical.x) * index$1.EXTENT,
(mercCameraPos.y * tiles - coord.canonical.y) * index$1.EXTENT,
mercCameraPos.z * tiles * index$1.EXTENT
];
const clippable = painter.conflationActive && Object.keys(bucket.instancesPerModel).length > 0 && painter.style.isLayerClipped(layer, source.getSource());
if (clippable) {
if (bucket.updateReplacement(coord, painter.replacementSource, layerIndex, layer.scope)) {
bucket.uploaded = false;
bucket.upload(painter.context);
}
}
let modelIndex = 0;
const transparentMeshes = new Array();
const opaqueMeshes = new Array();
const modelParametersVector = new Array();
for (let modelId in bucket.instancesPerModel) {
const modelInstances = bucket.instancesPerModel[modelId];
if (modelInstances.features.length > 0 && !isGlobe) {
modelId = modelIdProperty.evaluate(modelInstances.features[0].feature, {});
}
const model = modelManager.getModel(modelId, scope);
if (!model && !modelManager.hasURLBeenRequested(modelId) && !bucket.modelUris.includes(modelId)) {
bucket.modelUris.push(modelId);
bucket.modelsRequested = false;
}
if (!model || !model.uploaded) continue;
if (isGlobe) {
const cameraPosGlobe = index$1.scale$1([], [mercCameraPos.x, mercCameraPos.y, mercCameraPos.z], painter.transform.worldSize);
index$1.negate(cameraPosGlobe, cameraPosGlobe);
for (let instanceIndex = 0; instanceIndex < modelInstances.instancedDataArray.length; ++instanceIndex) {
const rotation = [0, 0, 0];
const scale = [1, 1, 1];
const rotationQuat = index$1.create$4();
const tilePosition = modelInstances.tileCoordinatesForInstance(instanceIndex);
const instanceTransform = modelInstances.transformForInstance(instanceIndex);
index$1.getScaling(scale, instanceTransform);
index$1.getRotation(rotationQuat, instanceTransform);
index$1.getAxisAngle(rotation, rotationQuat);
const translation = modelInstances.translationForInstance(instanceIndex);
const position = new index$1.LngLat(0, 0);
index$1.tileToLngLat(bucket.canonical, position, tilePosition.x, tilePosition.y);
const modelMatrix = index$1.create();
index$1.calculateModelMatrix(modelMatrix, model, painter.transform, position, rotation, scale, translation, true, false, false);
const colorMix = modelInstances.colorForInstance(instanceIndex);
const negCameraPosMatrix = index$1.identity([]);
const modelMetersPerPixel = index$1.getMetersPerPixelAtLatitude(position.lat, painter.transform.zoom);
const modelPixelsPerMeter = 1 / modelMetersPerPixel;
const zScaleMatrix = index$1.fromScaling([], [1, 1, modelPixelsPerMeter]);
index$1.translate(negCameraPosMatrix, negCameraPosMatrix, cameraPosGlobe);
const modelParameters = { zScaleMatrix, negCameraPosMatrix };
modelParametersVector.push(modelParameters);
for (const node of model.nodes) {
prepareMeshes(painter, node, modelMatrix, painter.transform.expandedFarZProjMatrix, modelIndex, transparentMeshes, opaqueMeshes, model.materialOverrides, layerOpacity, colorMix);
}
++modelIndex;
}
} else {
for (const node of model.nodes) {
drawInstancedNode(painter, layer, node, modelInstances, cameraPos, coord, renderData);
}
}
}
if (isGlobe) {
if (painter.renderPass === "shadow") {
for (const opaqueMesh of opaqueMeshes) {
drawShadowCaster(opaqueMesh.mesh, opaqueMesh.nodeModelMatrix, painter, layer);
}
for (const transparentMesh of transparentMeshes) {
drawShadowCaster(transparentMesh.mesh, transparentMesh.nodeModelMatrix, painter, layer);
}
} else {
drawSortedMeshes(painter, layer, transparentMeshes, opaqueMeshes, modelParametersVector);
}
}
}
}
const minimumInstanceCount = 20;
function drawInstancedNode(painter, layer, node, modelInstances, cameraPos, coord, renderData2) {
const context = painter.context;
const isShadowPass = painter.renderPass === "shadow";
const shadowRenderer = painter.shadowRenderer;
const depthMode = isShadowPass && shadowRenderer ? shadowRenderer.getShadowPassDepthMode() : new DepthMode(context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
const affectedByFog = painter.isTileAffectedByFog(coord);
const isGlobe = painter.transform.projection.name === "globe";
if (node.meshes) {
for (const mesh of node.meshes) {
const definesValues = isGlobe ? [] : ["MODEL_POSITION_ON_GPU"];
const dynamicBuffers = [];
let program;
let uniformValues;
let colorMode;
const useInstancing = !isGlobe && modelInstances.instancedDataArray.length > minimumInstanceCount;
if (useInstancing) {
definesValues.push("INSTANCED_ARRAYS");
}
const cutoffParams = getCutoffParams(painter, layer.paint.get("model-cutoff-fade-range"));
if (cutoffParams.shouldRenderCutoff) {
definesValues.push("RENDER_CUTOFF");
}
if (isShadowPass && shadowRenderer) {
program = painter.getOrCreateProgram("modelDepth", { defines: definesValues });
uniformValues = modelDepthUniformValues(renderData2.shadowTileMatrix, renderData2.shadowTileMatrix, Float32Array.from(node.globalMatrix));
colorMode = shadowRenderer.getShadowPassColorMode();
} else {
const ignoreLut = layer.paint.get("model-color-use-theme").constantOr("default") === "none";
setupMeshDraw(definesValues, dynamicBuffers, mesh, painter, ignoreLut ? null : layer.lut);
program = painter.getOrCreateProgram("model", { defines: definesValues, overrideFog: affectedByFog });
const material = mesh.material;
const pbr = material.pbrMetallicRoughness;
const layerOpacity = layer.paint.get("model-opacity").constantOr(1);
const emissiveStrength = layer.paint.get("model-emissive-strength").constantOr(0);
uniformValues = modelUniformValues(
coord.expandedProjMatrix,
Float32Array.from(node.globalMatrix),
new Float32Array(16),
null,
painter,
layerOpacity,
pbr.baseColorFactor,
material.emissiveFactor,
pbr.metallicFactor,
pbr.roughnessFactor,
material,
emissiveStrength,
layer,
cameraPos
);
if (shadowRenderer) {
if (!renderData2.shadowUniformsInitialized) {
shadowRenderer.setupShadows(coord.toUnwrapped(), program, "model-tile");
renderData2.shadowUniformsInitialized = true;
} else {
program.setShadowUniformValues(context, shadowRenderer.getShadowUniformValues());
}
}
const needsBlending = cutoffParams.shouldRenderCutoff || layerOpacity < 1 || material.alphaMode !== "OPAQUE";
colorMode = needsBlending ? ColorMode.alphaBlended : ColorMode.unblended;
}
painter.uploadCommonUniforms(context, program, coord.toUnwrapped(), null, cutoffParams);
index$1.assert(modelInstances.instancedDataArray.bytesPerElement === 64);
const cullFaceMode = mesh.material.doubleSided ? CullFaceMode.disabled : CullFaceMode.backCCW;
if (useInstancing) {
dynamicBuffers.push(modelInstances.instancedDataBuffer);
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
void 0,
dynamicBuffers,
modelInstances.instancedDataArray.length
);
} else {
const instanceUniform = isShadowPass ? "u_instance" : "u_normal_matrix";
for (let i = 0; i < modelInstances.instancedDataArray.length; ++i) {
uniformValues[instanceUniform] = new Float32Array(modelInstances.instancedDataArray.arrayBuffer, i * 64, 16);
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
cullFaceMode,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
void 0,
dynamicBuffers
);
}
}
}
}
if (node.children) {
for (const child of node.children) {
drawInstancedNode(painter, layer, child, modelInstances, cameraPos, coord, renderData2);
}
}
}
const normalScale = [1, -1, 1];
function prepareBatched(painter, source, layer, coords) {
const exaggeration = painter.terrain ? painter.terrain.exaggeration() : 0;
const zoom = painter.transform.zoom;
for (const coord of coords) {
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket) continue;
bucket.setFilter(layer.filter);
if (painter.conflationActive) bucket.updateReplacement(coord, painter.replacementSource);
bucket.evaluateTransform(painter, layer);
if (painter.terrain && exaggeration > 0) {
bucket.elevationUpdate(painter.terrain, exaggeration, coord, layer.source);
}
if (bucket.needsReEvaluation(painter, zoom, layer)) {
bucket.evaluate(layer);
}
}
}
function drawBatchedModels(painter, source, layer, coords) {
layer.resetLayerRenderingStats(painter);
const context = painter.context;
const tr = painter.transform;
const fog = painter.style.fog;
const shadowRenderer = painter.shadowRenderer;
if (tr.projection.name !== "mercator") {
index$1.warnOnce(`Drawing 3D landmark models for ${tr.projection.name} projection is not yet implemented`);
return;
}
const mercCameraPos = painter.transform.getFreeCameraOptions().position;
const cameraPos = index$1.scale$1([], [mercCameraPos.x, mercCameraPos.y, mercCameraPos.z], painter.transform.worldSize);
const negCameraPos = index$1.negate([], cameraPos);
const negCameraPosMatrix = index$1.identity([]);
const metersPerPixel = index$1.getMetersPerPixelAtLatitude(tr.center.lat, tr.zoom);
const pixelsPerMeter = 1 / metersPerPixel;
const zScaleMatrix = index$1.fromScaling([], [1, 1, pixelsPerMeter]);
index$1.translate(negCameraPosMatrix, negCameraPosMatrix, negCameraPos);
const layerOpacity = layer.paint.get("model-opacity").constantOr(1);
const depthModeRW = new DepthMode(context.gl.LEQUAL, DepthMode.ReadWrite, painter.depthRangeFor3D);
const depthModeRO = new DepthMode(context.gl.LEQUAL, DepthMode.ReadOnly, painter.depthRangeFor3D);
const aabb = new index$1.Aabb([Infinity, Infinity, Infinity], [-Infinity, -Infinity, -Infinity]);
const isShadowPass = painter.renderPass === "shadow";
const frustum = isShadowPass && shadowRenderer ? shadowRenderer.getCurrentCascadeFrustum() : tr.getFrustum(tr.scaleZoom(tr.worldSize));
const frontCutoffParams = layer.paint.get("model-front-cutoff");
const frontCutoffEnabled = frontCutoffParams[2] < 1;
const cutoffParams = getCutoffParams(painter, layer.paint.get("model-cutoff-fade-range"));
const stats = layer.getLayerRenderingStats();
const drawTiles = function() {
let start, end, step;
if (frontCutoffEnabled) {
start = coords.length - 1;
end = -1;
step = -1;
} else {
start = 0;
end = coords.length;
step = 1;
}
const invTileMatrix = new Float64Array(16);
const cameraPosTileCoord = index$1.create$2();
const cameraPointTileCoord = new index$1.Point(0, 0);
for (let i = start; i !== end; i += step) {
const coord = coords[i];
const tile = source.getTile(coord);
const bucket = tile.getBucket(layer);
if (!bucket || !bucket.uploaded) continue;
let singleCascade = false;
if (shadowRenderer) {
singleCascade = shadowRenderer.getMaxCascadeForTile(coord.toUnwrapped()) === 0;
}
const tileMatrix = tr.calculatePosMatrix(coord.toUnwrapped(), tr.worldSize);
const modelTraits = bucket.modelTraits;
if (!isShadowPass && frontCutoffEnabled) {
index$1.invert(invTileMatrix, tileMatrix);
index$1.transformMat4(cameraPosTileCoord, cameraPos, invTileMatrix);
cameraPointTileCoord.x = cameraPosTileCoord[0];
cameraPointTileCoord.y = cameraPosTileCoord[1];
}
const sortedNodes = [];
bucket.setFilter(layer.filter);
for (const nodeInfo of bucket.getNodesInfo()) {
if (nodeInfo.hiddenByReplacement) continue;
if (!nodeInfo.node.meshes) continue;
const node = nodeInfo.node;
let elevation = 0;
if (painter.terrain && node.elevation) {
elevation = node.elevation * painter.terrain.exaggeration();
}
const calculateNodeAabb = () => {
const localBounds = nodeInfo.aabb;
aabb.min = [...localBounds.min];
aabb.max = [...localBounds.max];
aabb.min[2] += elevation;
aabb.max[2] += elevation;
index$1.transformMat4(aabb.min, aabb.min, tileMatrix);
index$1.transformMat4(aabb.max, aabb.max, tileMatrix);
return aabb;
};
const nodeAabb = calculateNodeAabb();
const scale = nodeInfo.evaluatedScale;
if (scale[0] <= 1 && scale[1] <= 1 && scale[2] <= 1 && nodeAabb.intersects(frustum) === 0) {
continue;
}
if (!isShadowPass && frontCutoffEnabled) {
const opacityChangePerFrame = 1 / 6;
if (cameraPos[0] > nodeAabb.min[0] && cameraPos[0] < nodeAabb.max[0] && cameraPos[1] > nodeAabb.min[1] && cameraPos[1] < nodeAabb.max[1] && cameraPos[2] * metersPerPixel < nodeAabb.max[2] && node.footprint && index$1.pointInFootprint(cameraPointTileCoord, node.footprint)) {
nodeInfo.cameraCollisionOpacity = Math.max(nodeInfo.cameraCollisionOpacity - opacityChangePerFrame, 0);
} else {
nodeInfo.cameraCollisionOpacity = Math.min(1, nodeInfo.cameraCollisionOpacity + opacityChangePerFrame);
}
}
const tileModelMatrix = [...tileMatrix];
const tileUnitsPerMeter = 1 / index$1.tileToMeter(coord.canonical);
const anchorX = node.anchor ? node.anchor[0] : 0;
const anchorY = node.anchor ? node.anchor[1] : 0;
index$1.translate(tileModelMatrix, tileModelMatrix, [
anchorX * (scale[0] - 1) + nodeInfo.evaluatedTranslation[0] * tileUnitsPerMeter,
anchorY * (scale[1] - 1) + nodeInfo.evaluatedTranslation[1] * tileUnitsPerMeter,
elevation + nodeInfo.evaluatedTranslation[2]
]);
if (!index$1.exactEquals$1(scale, index$1.DefaultModelScale)) {
index$1.scale$3(tileModelMatrix, tileModelMatrix, scale);
}
const nodeModelMatrix = index$1.multiply([], tileModelMatrix, node.globalMatrix);
const wvpForNode = index$1.multiply([], tr.expandedFarZProjMatrix, nodeModelMatrix);
const wvpForTile = index$1.multiply([], tr.expandedFarZProjMatrix, tileModelMatrix);
const anchorPos = index$1.transformMat4$1([], [anchorX, anchorY, elevation, 1], wvpForNode);
const depth = anchorPos[2];
node.hidden = false;
let opacity = layerOpacity;
if (!isShadowPass) {
if (frontCutoffEnabled) {
opacity *= nodeInfo.cameraCollisionOpacity;
opacity *= calculateFrontCutoffOpacity(tileModelMatrix, tr, nodeInfo.aabb, frontCutoffParams);
}
opacity *= calculateFarCutoffOpacity(cutoffParams, depth);
}
if (opacity === 0) {
node.hidden = true;
continue;
}
const sortedNode = {
nodeInfo,
depth,
opacity,
wvpForNode,
wvpForTile,
nodeModelMatrix,
tileModelMatrix
};
sortedNodes.push(sortedNode);
}
if (!isShadowPass) {
sortedNodes.sort((a, b) => {
if (!frontCutoffEnabled || a.opacity === 1 && b.opacity === 1) {
return a.depth < b.depth ? -1 : 1;
}
if (a.opacity === 1) {
return -1;
}
if (b.opacity === 1) {
return 1;
}
return a.depth > b.depth ? -1 : 1;
});
}
for (const sortedNode of sortedNodes) {
const nodeInfo = sortedNode.nodeInfo;
const node = nodeInfo.node;
let lightingMatrix = index$1.multiply([], zScaleMatrix, sortedNode.tileModelMatrix);
index$1.multiply(lightingMatrix, negCameraPosMatrix, lightingMatrix);
const normalMatrix = index$1.invert([], lightingMatrix);
index$1.transpose(normalMatrix, normalMatrix);
index$1.scale$3(normalMatrix, normalMatrix, normalScale);
lightingMatrix = index$1.multiply(lightingMatrix, lightingMatrix, node.globalMatrix);
const isLightBeamPass = painter.renderPass === "light-beam";
const ignoreLut = layer.paint.get("model-color-use-theme").constantOr("default") === "none";
const hasMapboxFeatures = modelTraits & index$1.ModelTraits.HasMapboxMeshFeatures;
const emissiveStrength = hasMapboxFeatures ? 0 : nodeInfo.evaluatedRMEA[0][2];
for (let i2 = 0; i2 < node.meshes.length; ++i2) {
const mesh = node.meshes[i2];
const isLight = i2 === node.lightMeshIndex;
let worldViewProjection = sortedNode.wvpForNode;
if (isLight) {
if (!isLightBeamPass && !painter.terrain && painter.shadowRenderer) {
if (painter.currentLayer < painter.firstLightBeamLayer) {
painter.firstLightBeamLayer = painter.currentLayer;
}
continue;
}
worldViewProjection = sortedNode.wvpForTile;
} else if (isLightBeamPass) {
continue;
}
const programOptions = {
defines: []
};
const dynamicBuffers = [];
if (!isShadowPass && shadowRenderer) {
shadowRenderer.useNormalOffset = !!mesh.normalBuffer;
}
setupMeshDraw(programOptions.defines, dynamicBuffers, mesh, painter, ignoreLut ? null : layer.lut);
if (!hasMapboxFeatures) {
programOptions.defines.push("DIFFUSE_SHADED");
}
if (singleCascade) {
programOptions.defines.push("SHADOWS_SINGLE_CASCADE");
}
if (stats) {
if (!isShadowPass) {
stats.numRenderedVerticesInTransparentPass += mesh.vertexArray.length;
} else {
stats.numRenderedVerticesInShadowPass += mesh.vertexArray.length;
}
}
if (isShadowPass) {
drawShadowCaster(mesh, sortedNode.nodeModelMatrix, painter, layer);
continue;
}
let fogMatrixArray = null;
if (fog) {
const fogMatrix = fogMatrixForModel(sortedNode.nodeModelMatrix, painter.transform);
fogMatrixArray = new Float32Array(fogMatrix);
if (tr.projection.name !== "globe") {
const min = mesh.aabb.min;
const max = mesh.aabb.max;
const [minOpacity, maxOpacity] = fog.getOpacityForBounds(fogMatrix, min[0], min[1], max[0], max[1]);
programOptions.overrideFog = minOpacity >= FOG_OPACITY_THRESHOLD || maxOpacity >= FOG_OPACITY_THRESHOLD;
}
}
const material = mesh.material;
let occlusionTextureTransform;
if (material.occlusionTexture && material.occlusionTexture.offsetScale) {
occlusionTextureTransform = material.occlusionTexture.offsetScale;
programOptions.defines.push("OCCLUSION_TEXTURE_TRANSFORM");
}
const program = painter.getOrCreateProgram("model", programOptions);
if (!isShadowPass && shadowRenderer) {
shadowRenderer.setupShadowsFromMatrix(sortedNode.tileModelMatrix, program, shadowRenderer.useNormalOffset);
}
painter.uploadCommonUniforms(context, program, null, fogMatrixArray);
const pbr = material.pbrMetallicRoughness;
pbr.metallicFactor = 0.9;
pbr.roughnessFactor = 0.5;
const uniformValues = modelUniformValues(
new Float32Array(worldViewProjection),
new Float32Array(lightingMatrix),
new Float32Array(normalMatrix),
new Float32Array(node.globalMatrix),
painter,
sortedNode.opacity,
pbr.baseColorFactor,
material.emissiveFactor,
pbr.metallicFactor,
pbr.roughnessFactor,
material,
emissiveStrength,
layer,
[0, 0, 0],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
occlusionTextureTransform
);
if (!isLight && (nodeInfo.hasTranslucentParts || sortedNode.opacity < 1)) {
program.draw(
painter,
context.gl.TRIANGLES,
depthModeRW,
StencilMode.disabled,
ColorMode.disabled,
CullFaceMode.backCCW,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
void 0,
dynamicBuffers
);
}
const meshNeedsBlending = isLight || sortedNode.opacity < 1 || nodeInfo.hasTranslucentParts;
const colorMode = meshNeedsBlending ? ColorMode.alphaBlended : ColorMode.unblended;
const depthMode = !isLight ? depthModeRW : depthModeRO;
program.draw(
painter,
context.gl.TRIANGLES,
depthMode,
StencilMode.disabled,
colorMode,
CullFaceMode.backCCW,
uniformValues,
layer.id,
mesh.vertexBuffer,
mesh.indexBuffer,
mesh.segments,
layer.paint,
painter.transform.zoom,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
void 0,
dynamicBuffers
);
}
}
}
};
prepareBatched(painter, source, layer, coords);
drawTiles();
}
function calculateTileShadowPassCulling(bucket, renderData2, painter, scope) {
if (!painter.modelManager) return true;
const modelManager = painter.modelManager;
if (!painter.shadowRenderer) return true;
const shadowRenderer = painter.shadowRenderer;
index$1.assert(painter.renderPass === "shadow");
const aabb = renderData2.aabb;
let allModelsLoaded = true;
let maxHeight = bucket.maxHeight;
if (maxHeight === 0) {
let maxDim = 0;
for (const modelId in bucket.instancesPerModel) {
const model = modelManager.getModel(modelId, scope);
if (!model) {
allModelsLoaded = false;
continue;
}
maxDim = Math.max(maxDim, Math.max(Math.max(model.aabb.max[0], model.aabb.max[1]), model.aabb.max[2]));
}
maxHeight = bucket.maxScale * maxDim * 1.41 + bucket.maxVerticalOffset;
if (allModelsLoaded) bucket.maxHeight = maxHeight;
}
aabb.max[2] = maxHeight;
aabb.min[2] += bucket.terrainElevationMin;
aabb.max[2] += bucket.terrainElevationMax;
index$1.transformMat4(aabb.min, aabb.min, renderData2.tileMatrix);
index$1.transformMat4(aabb.max, aabb.max, renderData2.tileMatrix);
const intersection = aabb.intersects(shadowRenderer.getCurrentCascadeFrustum());
if (painter.currentShadowCascade === 0) {
bucket.isInsideFirstShadowMapFrustum = intersection === 2;
}
return intersection === 0;
}
function calculateFarCutoffOpacity(cutoffParams, depth) {
index$1.assert(cutoffParams.uniformValues.u_cutoff_params.length === 4);
const near = cutoffParams.uniformValues.u_cutoff_params[0];
const far = cutoffParams.uniformValues.u_cutoff_params[1];
const cutoffStart = cutoffParams.uniformValues.u_cutoff_params[2];
const cutoffEnd = cutoffParams.uniformValues.u_cutoff_params[3];
if (far === near || cutoffEnd === cutoffStart) {
return 1;
}
const linearDepth = (depth - near) / (far - near);
return index$1.clamp((linearDepth - cutoffStart) / (cutoffEnd - cutoffStart), 0, 1);
}
function calculateFrontCutoffOpacity(tileModelMatrix, tr, aabb, cutoffParams) {
const fullyOpaquePitch = 20;
const fullyTransparentPitch = 40;
index$1.assert(fullyOpaquePitch !== fullyTransparentPitch);
if (tr.pitch < fullyOpaquePitch) {
return 1;
}
const mat = tr.getWorldToCameraMatrix();
index$1.multiply(mat, mat, tileModelMatrix);
const p = index$1.fromValues$1(aabb.min[0], aabb.min[1], aabb.min[2], 1);
let r = index$1.transformMat4$1(index$1.create$5(), p, mat);
let pMin = r;
let pMax = r;
p[1] = aabb.max[1];
r = index$1.transformMat4$1(index$1.create$5(), p, mat);
pMin = r[1] < pMin[1] ? r : pMin;
pMax = r[1] > pMax[1] ? r : pMax;
p[0] = aabb.max[0];
r = index$1.transformMat4$1(index$1.create$5(), p, mat);
pMin = r[1] < pMin[1] ? r : pMin;
pMax = r[1] > pMax[1] ? r : pMax;
p[1] = aabb.min[1];
r = index$1.transformMat4$1(index$1.create$5(), p, mat);
pMin = r[1] < pMin[1] ? r : pMin;
pMax = r[1] > pMax[1] ? r : pMax;
const cutoffStartParam = index$1.clamp(cutoffParams[0], 0, 1);
const cutoffRangeParam = 100 * tr.pixelsPerMeter * index$1.clamp(cutoffParams[1], 0, 1);
const finalOpacity = index$1.clamp(cutoffParams[2], 0, 1);
const cutoffStart = index$1.lerp$1(index$1.create$5(), pMin, pMax, cutoffStartParam);
const fovScale = Math.tan(tr.fovX * 0.5);
const yMinLimit = -cutoffStart[2] * fovScale;
if (cutoffRangeParam === 0) {
return cutoffStart[1] < -Math.abs(yMinLimit) ? finalOpacity : 1;
}
const cutoffFactor = (-Math.abs(yMinLimit) - cutoffStart[1]) / cutoffRangeParam;
const lerp = (a, b, t) => {
return (1 - t) * a + t * b;
};
const opacity = index$1.clamp(lerp(1, finalOpacity, cutoffFactor), finalOpacity, 1);
return lerp(1, opacity, index$1.clamp((tr.pitch - fullyOpaquePitch) / (fullyTransparentPitch - fullyOpaquePitch), 0, 1));
}
class CacheEntry {
}
const TimeoutFrames = 30;
class WireframeDebugCache {
constructor() {
this._storage = /* @__PURE__ */ new Map();
}
getLinesFromTrianglesBuffer(frameIdx, indexBuffer, context) {
{
const entry = this._storage.get(indexBuffer.id);
if (entry) {
entry.lastUsedFrameIdx = frameIdx;
return entry.buf;
}
}
const gl = context.gl;
const bufSize = gl.getBufferParameter(gl.ELEMENT_ARRAY_BUFFER, gl.BUFFER_SIZE);
const bufTmp = new ArrayBuffer(bufSize);
const intView = new Int16Array(bufTmp);
gl.getBufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, new Int16Array(bufTmp));
const lineIndexArray = new index$1.StructArrayLayout2ui4();
for (let i = 0; i < bufSize / 2; i += 3) {
const i0 = intView[i];
const i1 = intView[i + 1];
const i2 = intView[i + 2];
lineIndexArray.emplaceBack(i0, i1);
lineIndexArray.emplaceBack(i1, i2);
lineIndexArray.emplaceBack(i2, i0);
}
const previousBoundVAO = context.bindVertexArrayOES.current;
const newEntry = new CacheEntry();
newEntry.buf = new IndexBuffer(context, lineIndexArray);
newEntry.lastUsedFrameIdx = frameIdx;
this._storage.set(indexBuffer.id, newEntry);
context.bindVertexArrayOES.set(previousBoundVAO);
return newEntry.buf;
}
update(frameIdx) {
for (const [key, obj] of this._storage) {
if (frameIdx - obj.lastUsedFrameIdx > TimeoutFrames) {
obj.buf.destroy();
this._storage.delete(key);
}
}
}
destroy() {
for (const [key, obj] of this._storage) {
obj.buf.destroy();
this._storage.delete(key);
}
}
}
class OcclusionParams {
constructor() {
this.occluderSize = 30;
this.depthOffset = -1e-4;
DevTools.addParameter(this, "occluderSize", "Occlusion", { min: 1, max: 100, step: 1 });
DevTools.addParameter(this, "depthOffset", "Occlusion", { min: -0.05, max: 0, step: 1e-5 });
}
}
const rainLayout = index$1.createLayout([
{ type: "Float32", name: "a_pos_3f", components: 3 },
{ type: "Float32", name: "a_uv", components: 2 },
{ type: "Float32", name: "a_rainParticleData", components: 4 }
]);
class PrecipitationRevealParams {
constructor(folder) {
this.revealStart = 11;
this.revealRange = 2;
DevTools.addParameter(this, "revealStart", `${folder} > Reveal`, { min: 0, max: 17, step: 0.05 });
DevTools.addParameter(this, "revealRange", `${folder} > Reveal`, { min: 0.1, max: 5.1, step: 0.05 });
}
}
const vignetteLayout = index$1.createLayout([
{ type: "Float32", name: "a_pos_2f", components: 2 }
]);
function createDevToolsBindings(params, painter, folder) {
DevTools.addParameter(params, "start", folder, { min: 0, max: 2 });
DevTools.addParameter(params, "range", folder, { min: 0, max: 2 });
DevTools.addParameter(params, "fadePower", folder, { min: -1, max: 1, step: 0.01 });
DevTools.addParameter(params, "strength", folder, { min: 0, max: 1 });
DevTools.addParameter(params, "color", folder, {
color: { type: "float" }
});
}
class Vignette {
destroy() {
if (this.vignetteVx) {
this.vignetteVx.destroy();
}
if (this.vignetteIdx) {
this.vignetteIdx.destroy();
}
}
draw(painter, params) {
const program = painter.getOrCreateProgram("vignette");
if (!this.vignetteVx || !this.vignetteIdx) {
const vertices = new index$1.StructArrayLayout2f8();
const triangles = new index$1.StructArrayLayout3ui6();
vertices.emplaceBack(-1, -1);
vertices.emplaceBack(1, -1);
vertices.emplaceBack(1, 1);
vertices.emplaceBack(-1, 1);
triangles.emplaceBack(0, 1, 2);
triangles.emplaceBack(0, 2, 3);
this.vignetteVx = painter.context.createVertexBuffer(vertices, vignetteLayout.members);
this.vignetteIdx = painter.context.createIndexBuffer(triangles);
}
const vignetteSegments = index$1.SegmentVector.simpleSegment(0, 0, 4, 6);
if (this.vignetteVx && this.vignetteIdx) {
painter.uploadCommonUniforms(painter.context, program);
const uniforms = vignetteUniformValues({
vignetteShape: [params.start, params.range, Math.pow(10, params.fadePower)],
vignetteColor: [params.color.r, params.color.g, params.color.b, params.color.a * params.strength]
});
const gl = painter.context.gl;
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniforms,
"vignette",
this.vignetteVx,
this.vignetteIdx,
vignetteSegments
);
}
}
}
class Movement {
constructor() {
this._accumulatedOffsetX = 0;
this._accumulatedOffsetY = 0;
this._accumulatedElevation = 0;
}
update(tr, ppmScaleFactor) {
const options = tr.getFreeCameraOptions();
const cameraMercatorPos = options.position;
const elevation = cameraMercatorPos.toAltitude();
const latLng = cameraMercatorPos.toLngLat();
const lng = index$1.degToRad(latLng.lng);
const lat = index$1.degToRad(latLng.lat);
const ppmScale = tr.pixelsPerMeter / ppmScaleFactor;
const offsetXCur = lng * index$1.earthRadius;
const offsetYCur = index$1.earthRadius * Math.log(Math.tan(Math.PI / 4 + lat / 2));
if (this._offsetXPrev === void 0) {
this._offsetXPrev = 0;
this._offsetYPrev = 0;
this._elevationPrev = 0;
this._accumulatedOffsetX = 0;
this._accumulatedOffsetY = 0;
this._accumulatedElevation = 0;
} else {
const deltaX = -this._offsetXPrev + offsetXCur;
const deltaY = -this._offsetYPrev + offsetYCur;
const deltaE = -this._elevationPrev + elevation;
this._accumulatedOffsetX += deltaX * ppmScale;
this._accumulatedOffsetY += deltaY * ppmScale;
this._accumulatedElevation += deltaE * ppmScale;
this._offsetXPrev = offsetXCur;
this._offsetYPrev = offsetYCur;
this._elevationPrev = elevation;
}
}
getPosition() {
return [this._accumulatedOffsetX, this._accumulatedOffsetY, this._accumulatedElevation];
}
}
function boxWrap(unwrappedPos, boxSize) {
const wrappedOffsetX = unwrappedPos[0] - Math.floor(unwrappedPos[0] / boxSize) * boxSize;
const wrappedOffsetY = unwrappedPos[1] - Math.floor(unwrappedPos[1] / boxSize) * boxSize;
const wrappedOffsetZ = unwrappedPos[2] - Math.floor(unwrappedPos[2] / boxSize) * boxSize;
return [-wrappedOffsetX, -wrappedOffsetY, -wrappedOffsetZ];
}
function generateUniformDistributedPointsInsideCube(pointsCount) {
const sRand = index$1.mulberry32(1323123451230);
const points = [];
for (let i = 0; i < pointsCount; ++i) {
const vx = -1 + 2 * sRand();
const vy = -1 + 2 * sRand();
const vz = -1 + 2 * sRand();
points.push(index$1.fromValues$2(vx, vy, vz));
}
return points;
}
function lerpClamp(a, b, t1, t2, tMid) {
const t = index$1.clamp((tMid - t1) / (t2 - t1), 0, 1);
return (1 - t) * a + t * b;
}
class DrawParams {
}
class PrecipitationBase {
constructor(ppmScaleFactor) {
this._movement = new Movement();
this._accumulatedTimeFromStart = 0;
this._prevTime = Date.now() / 1e3;
this._vignette = new Vignette();
this._ppmScaleFactor = ppmScaleFactor;
}
destroy() {
if (this.particlesVx) {
this.particlesVx.destroy();
}
if (this.particlesIdx) {
this.particlesIdx.destroy();
}
if (this._vignette) {
this._vignette.destroy();
}
}
updateOnRender(painter, timeFactor) {
const tr = painter.transform;
this._movement.update(tr, this._ppmScaleFactor);
const projectionMatrix = tr.starsProjMatrix;
const orientation = index$1.identity$1([]);
index$1.rotateX(orientation, orientation, index$1.degToRad(90) - tr._pitch);
index$1.rotateZ$1(orientation, orientation, -tr.angle);
const rotationMatrix = index$1.fromQuat(new Float32Array(16), orientation);
const swapAxesT = index$1.fromValues$3(
1,
0,
0,
0,
0,
0,
1,
0,
0,
-1,
0,
0,
0,
0,
0,
1
);
const swapAxes = index$1.transpose([], swapAxesT);
const modelviewMatrix = index$1.multiply([], swapAxes, rotationMatrix);
const curTime = Date.now() / 1e3;
this._accumulatedTimeFromStart += (curTime - this._prevTime) * timeFactor;
this._prevTime = curTime;
return { projectionMatrix, modelviewMatrix };
}
}
class Rain extends PrecipitationBase {
constructor(painter) {
super(4.25);
this._params = {
overrideStyleParameters: false,
intensity: 0.5,
timeFactor: 1,
velocityConeAperture: 0,
velocity: 300,
boxSize: 2500,
dropletSizeX: 1,
dropletSizeYScale: 10,
distortionStrength: 70,
screenThinning: {
intensity: 0.57,
start: 0.46,
range: 1.17,
fadePower: 0.17,
affectedRatio: 1,
particleOffset: -0.2
},
color: { r: 0.66, g: 0.68, b: 0.74, a: 0.7 },
direction: { x: -50, y: -35 },
shapeDirPower: 2,
shapeNormalPower: 1
};
const folder = "Precipitation > Rain";
this._revealParams = new PrecipitationRevealParams(folder);
this._vignetteParams = {
strength: 1,
start: 0.7,
range: 1,
fadePower: 0.4,
color: { r: 0.27, g: 0.27, b: 0.27, a: 1 }
};
this.particlesCount = 16e3;
DevTools.addParameter(this._params, "overrideStyleParameters", folder);
DevTools.addParameter(this._params, "intensity", folder, { min: 0, max: 1 });
DevTools.addParameter(this._params, "timeFactor", folder, { min: 0, max: 3, step: 0.01 });
DevTools.addParameter(this._params, "velocityConeAperture", folder, { min: 0, max: 160, step: 1 });
DevTools.addParameter(this._params, "velocity", folder, { min: 0, max: 1500, step: 5 });
DevTools.addParameter(this._params, "boxSize", folder, { min: 100, max: 4400, step: 10 });
DevTools.addParameter(this._params, "dropletSizeX", folder, { min: 0.1, max: 10, step: 0.1 });
DevTools.addParameter(this._params, "dropletSizeYScale", folder, { min: 0.1, max: 10, step: 0.1 });
DevTools.addParameter(this._params, "distortionStrength", folder, { min: 0, max: 100, step: 0.5 });
DevTools.addParameter(this._params, "direction", folder, {
picker: "inline",
expanded: true,
x: { min: -200, max: 200 },
y: { min: -200, max: 200 }
});
DevTools.addParameter(this._params, "shapeDirPower", `${folder} > Shape`, { min: 1, max: 10, step: 0.01 });
DevTools.addParameter(this._params, "shapeNormalPower", `${folder} > Shape`, { min: 1, max: 10, step: 0.01 });
DevTools.addParameter(this._params, "color", folder, {
color: { type: "float" }
});
DevTools.addParameter(this._params.screenThinning, "intensity", `${folder} > ScreenThinning`, { min: 0, max: 1 });
DevTools.addParameter(this._params.screenThinning, "start", `${folder} > ScreenThinning`, { min: 0, max: 2 });
DevTools.addParameter(this._params.screenThinning, "range", `${folder} > ScreenThinning`, { min: 0, max: 2 });
DevTools.addParameter(this._params.screenThinning, "fadePower", `${folder} > ScreenThinning`, { min: -1, max: 1, step: 0.01 });
DevTools.addParameter(this._params.screenThinning, "affectedRatio", `${folder} > ScreenThinning`, { min: 0, max: 1, step: 0.01 });
DevTools.addParameter(this._params.screenThinning, "particleOffset", `${folder} > ScreenThinning`, { min: -1, max: 1, step: 0.01 });
createDevToolsBindings(this._vignetteParams, painter, `${folder} > Vignette`);
}
update(painter) {
const context = painter.context;
if (!this.particlesVx) {
const positions = generateUniformDistributedPointsInsideCube(this.particlesCount);
const vertices = new index$1.StructArrayLayout9f36();
const triangles = new index$1.StructArrayLayout3ui6();
let base = 0;
const sRand = index$1.mulberry32(1323123451230);
for (let i = 0; i < positions.length; ++i) {
const p = positions[i];
const angularVelocityScale = -1 + 2 * sRand();
const velocityScale = sRand();
const directionConeHeading = sRand();
const directionConePitch = sRand();
const data = [angularVelocityScale, velocityScale, directionConeHeading, directionConePitch];
vertices.emplaceBack(p[0], p[1], p[2], -1, -1, ...data);
vertices.emplaceBack(p[0], p[1], p[2], 1, -1, ...data);
vertices.emplaceBack(p[0], p[1], p[2], 1, 1, ...data);
vertices.emplaceBack(p[0], p[1], p[2], -1, 1, ...data);
triangles.emplaceBack(base + 0, base + 1, base + 2);
triangles.emplaceBack(base + 0, base + 2, base + 3);
base += 4;
}
this.particlesVx = context.createVertexBuffer(vertices, rainLayout.members);
this.particlesIdx = context.createIndexBuffer(triangles);
}
}
draw(painter) {
if (!this._params.overrideStyleParameters && !painter.style.rain) {
return;
}
const gp = this._params.overrideStyleParameters ? this._revealParams : { revealStart: 0, revealRange: 0.01 };
const zoom = painter.transform.zoom;
if (gp.revealStart > zoom) {
return;
}
const revealFactor = lerpClamp(0, 1, gp.revealStart, gp.revealStart + gp.revealRange, zoom);
if (!this.particlesVx || !this.particlesIdx) {
return;
}
const params = structuredClone(this._params);
let rainDirection = [-params.direction.x, params.direction.y, -100];
index$1.normalize(rainDirection, rainDirection);
const vignetteParams = structuredClone(this._vignetteParams);
vignetteParams.strength *= revealFactor;
if (!params.overrideStyleParameters) {
params.intensity = painter.style.rain.state.density;
params.timeFactor = painter.style.rain.state.intensity;
params.color = structuredClone(painter.style.rain.state.color);
rainDirection = structuredClone(painter.style.rain.state.direction);
params.screenThinning.intensity = painter.style.rain.state.centerThinning;
params.dropletSizeX = painter.style.rain.state.dropletSize[0];
params.dropletSizeYScale = painter.style.rain.state.dropletSize[1] / painter.style.rain.state.dropletSize[0];
params.distortionStrength = painter.style.rain.state.distortionStrength * 100;
vignetteParams.strength = 1;
vignetteParams.color = structuredClone(painter.style.rain.state.vignetteColor);
}
const drawData = this.updateOnRender(painter, params.timeFactor);
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
if (!this.screenTexture || this.screenTexture.size[0] !== painter.width || this.screenTexture.size[1] !== painter.height) {
this.screenTexture = new index$1.Texture(context, { width: painter.width, height: painter.height, data: null }, gl.RGBA8);
}
if (params.distortionStrength > 0) {
context.activeTexture.set(gl.TEXTURE0);
this.screenTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
gl.copyTexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, 0, 0, painter.width, painter.height);
}
const program = painter.getOrCreateProgram("rainParticle");
painter.uploadCommonUniforms(context, program);
context.activeTexture.set(gl.TEXTURE0);
this.screenTexture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE);
const colorVec = [params.color.r, params.color.g, params.color.b, params.color.a];
const drawParticlesBox = (boxSize, distortionOnly) => {
const camPos = boxWrap(this._movement.getPosition(), boxSize);
const sizeX = params.dropletSizeX;
const sizeY = params.dropletSizeX * params.dropletSizeYScale;
const thinningX = painter.width / 2;
const thinningY = painter.height / 2;
const thinningStart = lerpClamp(0, params.screenThinning.start, 0, 1, params.screenThinning.intensity);
const thinningRange = lerpClamp(1e-3, params.screenThinning.range, 0, 1, params.screenThinning.intensity);
const thinningParticleOffset = lerpClamp(0, params.screenThinning.particleOffset, 0, 1, params.screenThinning.intensity);
const uniforms = rainUniformValues({
modelview: drawData.modelviewMatrix,
projection: drawData.projectionMatrix,
time: this._accumulatedTimeFromStart,
camPos,
velocityConeAperture: params.velocityConeAperture,
velocity: params.velocity,
boxSize,
rainDropletSize: [sizeX, sizeY],
distortionStrength: params.distortionStrength,
rainDirection,
color: colorVec,
screenSize: [tr.width, tr.height],
thinningCenterPos: [thinningX, thinningY],
thinningShape: [thinningStart, thinningRange, Math.pow(10, params.screenThinning.fadePower)],
thinningAffectedRatio: params.screenThinning.affectedRatio,
thinningParticleOffset,
shapeDirectionalPower: params.shapeDirPower,
shapeNormalPower: params.shapeNormalPower,
mode: distortionOnly ? 0 : 1
});
const count = Math.round(params.intensity * this.particlesCount);
const particlesSegments = index$1.SegmentVector.simpleSegment(0, 0, count * 4, count * 2);
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniforms,
"rain_particles",
this.particlesVx,
this.particlesIdx,
particlesSegments
);
};
if (params.distortionStrength > 0) {
drawParticlesBox(params.boxSize, true);
}
drawParticlesBox(params.boxSize, false);
this._vignette.draw(painter, vignetteParams);
}
}
const snowLayout = index$1.createLayout([
{ type: "Float32", name: "a_pos_3f", components: 3 },
{ type: "Float32", name: "a_uv", components: 2 },
{ type: "Float32", name: "a_snowParticleData", components: 4 },
{ type: "Float32", name: "a_snowParticleDataHorizontalOscillation", components: 2 }
]);
class Snow extends PrecipitationBase {
constructor(painter) {
super(2.25);
this._params = {
overrideStyleParameters: false,
intensity: 0.85,
timeFactor: 0.75,
velocityConeAperture: 70,
velocity: 40,
horizontalOscillationRadius: 4,
horizontalOscillationRate: 1.5,
boxSize: 2e3,
billboardSize: 2,
shapeFadeStart: 0.27,
shapeFadePower: 0.21,
screenThinning: {
intensity: 0.4,
start: 0.15,
range: 1.4,
fadePower: 0.24,
affectedRatio: 1,
particleOffset: -0.2
},
color: { r: 1, g: 1, b: 1, a: 1 },
direction: { x: -50, y: -35 }
};
const folder = "Precipitation > Snow";
this._revealParams = new PrecipitationRevealParams(folder);
this._vignetteParams = {
strength: 0.3,
start: 0.78,
range: 0.46,
fadePower: 0.2,
color: { r: 1, g: 1, b: 1, a: 1 }
};
this.particlesCount = 16e3;
DevTools.addParameter(this._params, "overrideStyleParameters", folder);
DevTools.addParameter(this._params, "intensity", folder, { min: 0, max: 1 });
DevTools.addParameter(this._params, "timeFactor", folder, { min: 0, max: 1, step: 0.01 });
DevTools.addParameter(this._params, "velocityConeAperture", folder, { min: 0, max: 160, step: 1 });
DevTools.addParameter(this._params, "velocity", folder, { min: 0, max: 500, step: 0.5 });
DevTools.addParameter(this._params, "horizontalOscillationRadius", folder, { min: 0, max: 10, step: 0.1 });
DevTools.addParameter(this._params, "horizontalOscillationRate", folder, { min: 0.3, max: 3, step: 0.05 });
DevTools.addParameter(this._params, "boxSize", folder, { min: 100, max: 1e4, step: 50 });
DevTools.addParameter(this._params, "billboardSize", folder, { min: 0.1, max: 10, step: 0.01 });
DevTools.addParameter(this._params.screenThinning, "intensity", `${folder} > ScreenThinning`, { min: 0, max: 1 });
DevTools.addParameter(this._params.screenThinning, "start", `${folder} > ScreenThinning`, { min: 0, max: 2 });
DevTools.addParameter(this._params.screenThinning, "range", `${folder} > ScreenThinning`, { min: 0, max: 2 });
DevTools.addParameter(this._params.screenThinning, "fadePower", `${folder} > ScreenThinning`, { min: -1, max: 1, step: 0.01 });
DevTools.addParameter(this._params.screenThinning, "affectedRatio", `${folder} > ScreenThinning`, { min: 0, max: 1, step: 0.01 });
DevTools.addParameter(this._params.screenThinning, "particleOffset", `${folder} > ScreenThinning`, { min: -1, max: 1, step: 0.01 });
DevTools.addParameter(this._params, "shapeFadeStart", `${folder} > Shape`, { min: 0, max: 1, step: 0.01 });
DevTools.addParameter(this._params, "shapeFadePower", `${folder} > Shape`, { min: -1, max: 0.99, step: 0.01 });
DevTools.addParameter(this._params, "color", folder, {
color: { type: "float" }
});
createDevToolsBindings(this._vignetteParams, painter, `${folder} > Vignette`);
DevTools.addParameter(this._params, "direction", folder, {
picker: "inline",
expanded: true,
x: { min: -200, max: 200 },
y: { min: -200, max: 200 }
});
}
update(painter) {
const context = painter.context;
if (!this.particlesVx) {
const positions = generateUniformDistributedPointsInsideCube(this.particlesCount);
const vertices = new index$1.StructArrayLayout11f44();
const triangles = new index$1.StructArrayLayout3ui6();
let base = 0;
const sRand = index$1.mulberry32(1323123451230);
for (let i = 0; i < positions.length; ++i) {
const p = positions[i];
const velocityScale = sRand();
const directionConeHeading = sRand();
const directionConePitch = sRand();
const data = [i / positions.length, velocityScale, directionConeHeading, directionConePitch];
const dataHorizontalOscillation = [sRand(), sRand()];
vertices.emplaceBack(p[0], p[1], p[2], -1, -1, ...data, ...dataHorizontalOscillation);
vertices.emplaceBack(p[0], p[1], p[2], 1, -1, ...data, ...dataHorizontalOscillation);
vertices.emplaceBack(p[0], p[1], p[2], 1, 1, ...data, ...dataHorizontalOscillation);
vertices.emplaceBack(p[0], p[1], p[2], -1, 1, ...data, ...dataHorizontalOscillation);
triangles.emplaceBack(base + 0, base + 1, base + 2);
triangles.emplaceBack(base + 0, base + 2, base + 3);
base += 4;
}
this.particlesVx = context.createVertexBuffer(vertices, snowLayout.members);
this.particlesIdx = context.createIndexBuffer(triangles);
}
}
draw(painter) {
if (!this._params.overrideStyleParameters && !painter.style.snow) {
return;
}
const params = structuredClone(this._params);
let snowDirection = [-params.direction.x, params.direction.y, -100];
index$1.normalize(snowDirection, snowDirection);
const vignetteParams = structuredClone(this._vignetteParams);
const gp = params.overrideStyleParameters ? this._revealParams : { revealStart: 0, revealRange: 0.01 };
const zoom = painter.transform.zoom;
if (gp.revealStart > zoom) {
return;
}
const revealFactor = lerpClamp(0, 1, gp.revealStart, gp.revealStart + gp.revealRange, zoom);
vignetteParams.strength *= revealFactor;
if (!params.overrideStyleParameters) {
params.intensity = painter.style.snow.state.density;
params.timeFactor = painter.style.snow.state.intensity;
params.color = structuredClone(painter.style.snow.state.color);
snowDirection = structuredClone(painter.style.snow.state.direction);
params.screenThinning.intensity = painter.style.snow.state.centerThinning;
params.billboardSize = 2.79 * painter.style.snow.state.flakeSize;
vignetteParams.strength = 1;
vignetteParams.color = structuredClone(painter.style.snow.state.vignetteColor);
}
const drawData = this.updateOnRender(painter, params.timeFactor);
if (!this.particlesVx || !this.particlesIdx) {
return;
}
const context = painter.context;
const gl = context.gl;
const tr = painter.transform;
const program = painter.getOrCreateProgram("snowParticle");
painter.uploadCommonUniforms(context, program);
const drawParticlesBox = (boxSize, sizeScale, dp) => {
const camPos = boxWrap(this._movement.getPosition(), boxSize);
const thinningX = tr.width / 2;
const thinningY = tr.height / 2;
const thinningStart = lerpClamp(0, dp.screenThinning.start, 0, 1, dp.screenThinning.intensity);
const thinningRange = lerpClamp(1e-3, dp.screenThinning.range, 0, 1, dp.screenThinning.intensity);
const thinningParticleOffset = lerpClamp(0, dp.screenThinning.particleOffset, 0, 1, dp.screenThinning.intensity);
const uniforms = snowUniformValues(
{
modelview: drawData.modelviewMatrix,
projection: drawData.projectionMatrix,
time: this._accumulatedTimeFromStart,
camPos,
velocityConeAperture: dp.velocityConeAperture,
velocity: dp.velocity,
horizontalOscillationRadius: dp.horizontalOscillationRadius,
horizontalOscillationRate: dp.horizontalOscillationRate,
boxSize,
billboardSize: dp.billboardSize * sizeScale,
simpleShapeParameters: [dp.shapeFadeStart, dp.shapeFadePower],
screenSize: [tr.width, tr.height],
thinningCenterPos: [thinningX, thinningY],
thinningShape: [thinningStart, thinningRange, Math.pow(10, dp.screenThinning.fadePower)],
thinningAffectedRatio: dp.screenThinning.affectedRatio,
thinningParticleOffset,
color: [dp.color.r, dp.color.g, dp.color.b, dp.color.a],
direction: snowDirection
}
);
const count = Math.round(dp.intensity * this.particlesCount);
const particlesSegments = index$1.SegmentVector.simpleSegment(0, 0, count * 4, count * 2);
if (this.particlesVx && this.particlesIdx) {
program.draw(
painter,
gl.TRIANGLES,
DepthMode.disabled,
StencilMode.disabled,
ColorMode.alphaBlended,
CullFaceMode.disabled,
uniforms,
"snow_particles",
this.particlesVx,
this.particlesIdx,
particlesSegments
);
}
};
drawParticlesBox(params.boxSize, 1, params);
this._vignette.draw(painter, vignetteParams);
}
}
const draw = {
symbol: drawSymbols,
circle: drawCircles,
heatmap: drawHeatmap,
line: drawLine,
fill: drawFill,
"fill-extrusion": draw$2,
building: draw$1,
hillshade: drawHillshade,
raster: drawRaster,
"raster-particle": drawRasterParticle,
background: drawBackground,
sky: drawSky,
custom: drawCustom,
model: drawModels
};
const prepare = {
line: prepare$4,
model: prepare$1,
raster: prepare$3,
"raster-particle": prepare$2
};
const depthPrepass = {
fill: drawDepthPrepass
};
const groundShadowMask = {
fill: drawGroundShadowMask
};
class Painter {
constructor(gl, contextCreateOptions, transform, scaleFactor, worldview) {
this.context = new Context(gl, contextCreateOptions);
this.transform = transform;
this._tileTextures = {};
this.frameCopies = [];
this.loadTimeStamps = [];
this._timeStamp = index$1.exported$1.now();
this._averageFPS = 0;
this._fpsHistory = [];
this._dt = 0;
this._debugParams = {
forceEnablePrecipitation: false,
showTerrainProxyTiles: false,
fpsWindow: 30,
continousRedraw: false,
enabledLayers: {}
};
const layerTypes = ["fill", "line", "symbol", "circle", "heatmap", "fill-extrusion", "building", "raster", "raster-particle", "hillshade", "model", "background", "sky"];
for (const layerType of layerTypes) {
this._debugParams.enabledLayers[layerType] = true;
}
DevTools.addParameter(this._debugParams, "showTerrainProxyTiles", "Terrain", {}, () => {
this.style.map.triggerRepaint();
});
DevTools.addParameter(this._debugParams, "forceEnablePrecipitation", "Precipitation");
DevTools.addParameter(this._debugParams, "fpsWindow", "FPS", { min: 1, max: 100, step: 1 });
DevTools.addBinding(this._debugParams, "continousRedraw", "FPS", {
readonly: true,
label: "continuous redraw"
});
DevTools.addBinding(this, "_averageFPS", "FPS", {
readonly: true,
label: "value"
});
DevTools.addBinding(this, "_averageFPS", "FPS", {
readonly: true,
label: "graph",
view: "graph",
min: 0,
max: 200
});
for (const layerType of layerTypes) {
DevTools.addParameter(this._debugParams.enabledLayers, layerType, "Debug > Layers");
}
this.occlusionParams = new OcclusionParams();
this.setup();
this.numSublayers = SourceCache.maxUnderzooming + SourceCache.maxOverzooming + 1;
this.depthEpsilon = 1 / Math.pow(2, 16);
this.deferredRenderGpuTimeQueries = [];
this.gpuTimers = {};
this.frameCounter = 0;
this._backgroundTiles = {};
this.conflationActive = false;
this.replacementSource = new index$1.ReplacementSource();
this.longestCutoffRange = 0;
this.minCutoffZoom = 0;
this._fogVisible = false;
this._cachedTileFogOpacities = {};
this._shadowRenderer = new ShadowRenderer(this);
this._wireframeDebugCache = new WireframeDebugCache();
this.renderDefaultNorthPole = true;
this.renderDefaultSouthPole = true;
this.layersWithOcclusionOpacity = [];
const emptyDepth = new index$1.RGBAImage({ width: 1, height: 1 }, Uint8Array.of(0, 0, 0, 0));
this.emptyDepthTexture = new index$1.Texture(this.context, emptyDepth, gl.RGBA8);
this._clippingActiveLastFrame = false;
this.scaleFactor = scaleFactor;
this.worldview = worldview;
this._forceEmissiveMode = false;
this.emissiveMode = "constant";
}
updateTerrain(style, adaptCameraAltitude) {
const enabled = !!style && !!style.terrain && this.transform.projection.supportsTerrain;
if (!enabled && (!this._terrain || !this._terrain.enabled)) return;
if (!this._terrain) {
this._terrain = new Terrain(this, style);
}
const terrain = this._terrain;
this.transform.elevation = enabled ? terrain : null;
terrain.update(style, this.transform, adaptCameraAltitude);
if (this.transform.elevation && !terrain.enabled) {
this.transform.elevation = null;
}
}
_updateFog(style) {
const isGlobe = this.transform.projection.name === "globe";
const fog = style.fog;
if (!fog || isGlobe || fog.getOpacity(this.transform.pitch) < 1 || fog.properties.get("horizon-blend") < 0.03) {
this.transform.fogCullDistSq = null;
return;
}
const [start, end] = fog.getFovAdjustedRange(this.transform._fov);
if (start > end) {
this.transform.fogCullDistSq = null;
return;
}
const fogBoundFraction = 0.78;
const fogCullDist = start + (end - start) * fogBoundFraction;
this.transform.fogCullDistSq = fogCullDist * fogCullDist;
}
get terrain() {
return this.transform._terrainEnabled() && this._terrain && this._terrain.enabled || this._forceTerrainMode ? this._terrain : null;
}
get forceTerrainMode() {
return this._forceTerrainMode;
}
set forceTerrainMode(value) {
if (value && !this._terrain) {
this._terrain = new Terrain(this, this.style);
}
this._forceTerrainMode = value;
}
get shadowRenderer() {
return this._shadowRenderer && this._shadowRenderer.enabled ? this._shadowRenderer : null;
}
get wireframeDebugCache() {
return this._wireframeDebugCache;
}
/*
* Update the GL viewport, projection matrix, and transforms to compensate
* for a new width and height value.
*/
resize(width, height) {
this.width = width * index$1.exported$1.devicePixelRatio;
this.height = height * index$1.exported$1.devicePixelRatio;
this.context.viewport.set([0, 0, this.width, this.height]);
if (this.style) {
for (const layerId of this.style.order) {
this.style._mergedLayers[layerId].resize();
}
}
}
setup() {
const context = this.context;
const tileExtentArray = new index$1.StructArrayLayout2i4();
tileExtentArray.emplaceBack(0, 0);
tileExtentArray.emplaceBack(index$1.EXTENT, 0);
tileExtentArray.emplaceBack(0, index$1.EXTENT);
tileExtentArray.emplaceBack(index$1.EXTENT, index$1.EXTENT);
this.tileExtentBuffer = context.createVertexBuffer(tileExtentArray, index$1.posAttributes.members);
this.tileExtentSegments = index$1.SegmentVector.simpleSegment(0, 0, 4, 2);
const debugArray = new index$1.StructArrayLayout2i4();
debugArray.emplaceBack(0, 0);
debugArray.emplaceBack(index$1.EXTENT, 0);
debugArray.emplaceBack(0, index$1.EXTENT);
debugArray.emplaceBack(index$1.EXTENT, index$1.EXTENT);
this.debugBuffer = context.createVertexBuffer(debugArray, index$1.posAttributes.members);
this.debugSegments = index$1.SegmentVector.simpleSegment(0, 0, 4, 5);
const viewportArray = new index$1.StructArrayLayout2i4();
viewportArray.emplaceBack(-1, -1);
viewportArray.emplaceBack(1, -1);
viewportArray.emplaceBack(-1, 1);
viewportArray.emplaceBack(1, 1);
this.viewportBuffer = context.createVertexBuffer(viewportArray, index$1.posAttributes.members);
this.viewportSegments = index$1.SegmentVector.simpleSegment(0, 0, 4, 2);
const tileBoundsArray = new index$1.StructArrayLayout4i8();
tileBoundsArray.emplaceBack(0, 0, 0, 0);
tileBoundsArray.emplaceBack(index$1.EXTENT, 0, index$1.EXTENT, 0);
tileBoundsArray.emplaceBack(0, index$1.EXTENT, 0, index$1.EXTENT);
tileBoundsArray.emplaceBack(index$1.EXTENT, index$1.EXTENT, index$1.EXTENT, index$1.EXTENT);
this.mercatorBoundsBuffer = context.createVertexBuffer(tileBoundsArray, index$1.boundsAttributes.members);
this.mercatorBoundsSegments = index$1.SegmentVector.simpleSegment(0, 0, 4, 2);
const quadTriangleIndices = new index$1.StructArrayLayout3ui6();
quadTriangleIndices.emplaceBack(0, 1, 2);
quadTriangleIndices.emplaceBack(2, 1, 3);
this.quadTriangleIndexBuffer = context.createIndexBuffer(quadTriangleIndices);
const tileLineStripIndices = new index$1.StructArrayLayout1ui2();
for (const i of [0, 1, 3, 2, 0]) tileLineStripIndices.emplaceBack(i);
this.debugIndexBuffer = context.createIndexBuffer(tileLineStripIndices);
this.emptyTexture = new index$1.Texture(
context,
new index$1.RGBAImage({ width: 1, height: 1 }, Uint8Array.of(0, 0, 0, 0)),
context.gl.RGBA8
);
this.identityMat = index$1.create();
const gl = this.context.gl;
this.stencilClearMode = new StencilMode({ func: gl.ALWAYS, mask: 0 }, 0, 255, gl.ZERO, gl.ZERO, gl.ZERO);
this.loadTimeStamps.push(performance.now());
}
getMercatorTileBoundsBuffers() {
return {
tileBoundsBuffer: this.mercatorBoundsBuffer,
tileBoundsIndexBuffer: this.quadTriangleIndexBuffer,
tileBoundsSegments: this.mercatorBoundsSegments
};
}
getTileBoundsBuffers(tile) {
tile._makeTileBoundsBuffers(this.context, this.transform.projection);
if (tile._tileBoundsBuffer) {
const tileBoundsBuffer = tile._tileBoundsBuffer;
const tileBoundsIndexBuffer = tile._tileBoundsIndexBuffer;
const tileBoundsSegments = tile._tileBoundsSegments;
return { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments };
} else {
return this.getMercatorTileBoundsBuffers();
}
}
/*
* Reset the drawing canvas by clearing the stencil buffer so that we can draw
* new tiles at the same location, while retaining previously drawn pixels.
*/
clearStencil() {
const context = this.context;
const gl = context.gl;
this.nextStencilID = 1;
this.currentStencilSource = void 0;
this._tileClippingMaskIDs = {};
this.getOrCreateProgram("clippingMask").draw(
this,
gl.TRIANGLES,
DepthMode.disabled,
this.stencilClearMode,
ColorMode.disabled,
CullFaceMode.disabled,
clippingMaskUniformValues(this.identityMat),
"$clipping",
this.viewportBuffer,
this.quadTriangleIndexBuffer,
this.viewportSegments
);
}
resetStencilClippingMasks() {
if (!this.terrain) {
this.currentStencilSource = void 0;
this._tileClippingMaskIDs = {};
}
}
_renderTileClippingMasks(layer, sourceCache, tileIDs) {
if (!sourceCache || this.currentStencilSource === sourceCache.id || !layer.isTileClipped() || !tileIDs || tileIDs.length === 0) {
return;
}
if (this._tileClippingMaskIDs && !this.terrain) {
let dirtyStencilClippingMasks = false;
for (const coord of tileIDs) {
if (this._tileClippingMaskIDs[coord.key] === void 0) {
dirtyStencilClippingMasks = true;
break;
}
}
if (!dirtyStencilClippingMasks) {
return;
}
}
this.currentStencilSource = sourceCache.id;
const context = this.context;
const gl = context.gl;
if (this.nextStencilID + tileIDs.length > 256) {
this.clearStencil();
}
context.setColorMode(ColorMode.disabled);
context.setDepthMode(DepthMode.disabled);
const program = this.getOrCreateProgram("clippingMask");
this._tileClippingMaskIDs = {};
for (const tileID of tileIDs) {
const tile = sourceCache.getTile(tileID);
const id = this._tileClippingMaskIDs[tileID.key] = this.nextStencilID++;
const { tileBoundsBuffer, tileBoundsIndexBuffer, tileBoundsSegments } = this.getTileBoundsBuffers(tile);
program.draw(
this,
gl.TRIANGLES,
DepthMode.disabled,
// Tests will always pass, and ref value will be written to stencil buffer.
new StencilMode({ func: gl.ALWAYS, mask: 0 }, id, 255, gl.KEEP, gl.KEEP, gl.REPLACE),
ColorMode.disabled,
CullFaceMode.disabled,
clippingMaskUniformValues(tileID.projMatrix),
"$clipping",
tileBoundsBuffer,
tileBoundsIndexBuffer,
tileBoundsSegments
);
}
}
stencilModeFor3D() {
this.currentStencilSource = void 0;
if (this.nextStencilID + 1 > 256) {
this.clearStencil();
}
const id = this.nextStencilID++;
const gl = this.context.gl;
return new StencilMode({ func: gl.NOTEQUAL, mask: 255 }, id, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
}
stencilModeForClipping(tileID) {
if (this.terrain) return this.terrain.stencilModeForRTTOverlap(tileID);
const gl = this.context.gl;
index$1.assert(this._tileClippingMaskIDs[tileID.key] != null);
return new StencilMode({ func: gl.EQUAL, mask: 255 }, this._tileClippingMaskIDs[tileID.key], 0, gl.KEEP, gl.KEEP, gl.REPLACE);
}
/*
* Sort coordinates by Z as drawing tiles is done in Z-descending order.
* All children with the same Z write the same stencil value. Children
* stencil values are greater than parent's. This is used only for raster
* and raster-dem tiles, which are already clipped to tile boundaries, to
* mask area of tile overlapped by children tiles.
* Stencil ref values continue range used in _tileClippingMaskIDs.
*
* Returns [StencilMode for tile overscaleZ map, sortedCoords].
*/
stencilConfigForOverlap(tileIDs) {
const gl = this.context.gl;
const coords = tileIDs.sort((a, b) => b.overscaledZ - a.overscaledZ);
const minTileZ = coords[coords.length - 1].overscaledZ;
const stencilValues = coords[0].overscaledZ - minTileZ + 1;
if (stencilValues > 1) {
this.currentStencilSource = void 0;
if (this.nextStencilID + stencilValues > 256) {
this.clearStencil();
}
const zToStencilMode = {};
for (let i = 0; i < stencilValues; i++) {
zToStencilMode[i + minTileZ] = new StencilMode({ func: gl.GEQUAL, mask: 255 }, i + this.nextStencilID, 255, gl.KEEP, gl.KEEP, gl.REPLACE);
}
this.nextStencilID += stencilValues;
return [zToStencilMode, coords];
}
return [{ [minTileZ]: StencilMode.disabled }, coords];
}
colorModeForRenderPass() {
const gl = this.context.gl;
if (this._showOverdrawInspector) {
const numOverdrawSteps = 8;
const a = 1 / numOverdrawSteps;
return new ColorMode([gl.CONSTANT_COLOR, gl.ONE, gl.CONSTANT_COLOR, gl.ONE], new index$1.Color(a, a, a, 0), [true, true, true, true]);
} else if (this.renderPass === "opaque") {
return ColorMode.unblended;
} else {
return ColorMode.alphaBlended;
}
}
colorModeForDrapableLayerRenderPass(emissiveStrengthForDrapedLayers) {
const deferredDrapingEnabled = () => {
return this.style && this.style.enable3dLights() && this.terrain && this.terrain.renderingToTexture;
};
const gl = this.context.gl;
if (deferredDrapingEnabled() && this.renderPass === "translucent") {
if (emissiveStrengthForDrapedLayers != null && this.emissiveMode !== "mrt-fallback" || this.emissiveMode === "constant") {
return new ColorMode([gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.CONSTANT_ALPHA, gl.ONE_MINUS_SRC_ALPHA], new index$1.Color(0, 0, 0, emissiveStrengthForDrapedLayers != null ? emissiveStrengthForDrapedLayers : 0), [true, true, true, true]);
} else if (this.emissiveMode === "dual-source-blending") {
const extBlendFuncExtended = this.context.extBlendFuncExtended;
return new ColorMode([gl.ONE, gl.ONE_MINUS_SRC_ALPHA, extBlendFuncExtended.SRC1_ALPHA_WEBGL, gl.ONE_MINUS_SRC_ALPHA], index$1.Color.transparent, [true, true, true, true]);
} else {
return this.colorModeForRenderPass();
}
} else {
return this.colorModeForRenderPass();
}
}
depthModeForSublayer(n, mask, func, skipOpaquePassCutoff = false) {
if (this.depthOcclusion) {
return new DepthMode(this.context.gl.GREATER, DepthMode.ReadOnly, this.depthRangeFor3D);
}
if (!this.opaquePassEnabledForLayer() && !skipOpaquePassCutoff) return DepthMode.disabled;
const depth = 1 - ((1 + this.currentLayer) * this.numSublayers + n) * this.depthEpsilon;
return new DepthMode(func || this.context.gl.LEQUAL, mask, [depth, depth]);
}
/*
* The opaque pass and 3D layers both use the depth buffer.
* Layers drawn above 3D layers need to be drawn using the
* painter's algorithm so that they appear above 3D features.
* This returns true for layers that can be drawn using the
* opaque pass.
*/
opaquePassEnabledForLayer() {
return this.currentLayer < this.opaquePassCutoff;
}
blitDepth() {
const gl = this.context.gl;
const depthWidth = Math.ceil(this.width);
const depthHeight = Math.ceil(this.height);
const fboPrev = this.context.bindFramebuffer.get();
const texturePrev = gl.getParameter(gl.TEXTURE_BINDING_2D);
if (!this.depthFBO || this.depthFBO.width !== depthWidth || this.depthFBO.height !== depthHeight) {
if (this.depthFBO) {
this.depthFBO.destroy();
this.depthFBO = void 0;
this.depthTexture = void 0;
}
if (depthWidth !== 0 && depthHeight !== 0) {
this.depthFBO = new Framebuffer(this.context, depthWidth, depthHeight, 0, "texture");
this.depthTexture = new index$1.Texture(this.context, { width: depthWidth, height: depthHeight, data: null }, gl.DEPTH24_STENCIL8);
this.depthFBO.depthAttachment.set(this.depthTexture.texture);
}
}
this.context.bindFramebuffer.set(fboPrev);
gl.bindTexture(gl.TEXTURE_2D, texturePrev);
if (this.depthFBO) {
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, null);
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this.depthFBO.framebuffer);
gl.blitFramebuffer(0, 0, depthWidth, depthHeight, 0, 0, depthWidth, depthHeight, gl.DEPTH_BUFFER_BIT, gl.NEAREST);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.context.bindFramebuffer.current);
}
}
updateAverageFPS() {
const fps = this._dt === 0 ? 0 : 1e3 / this._dt;
this._fpsHistory.push(fps);
if (this._fpsHistory.length > this._debugParams.fpsWindow) {
this._fpsHistory.splice(0, this._fpsHistory.length - this._debugParams.fpsWindow);
}
this._averageFPS = Math.round(this._fpsHistory.reduce((accum, current) => {
return accum + current / this._fpsHistory.length;
}, 0));
}
render(style, options) {
const curTime = index$1.exported$1.now();
this._dt = curTime - this._timeStamp;
this._timeStamp = curTime;
const renderStartTime = index$1.PerformanceUtils.now();
Debug.run(() => {
this.updateAverageFPS();
});
this._wireframeDebugCache.update(this.frameCounter);
this._debugParams.continousRedraw = style.map.repaint;
this.style = style;
this.options = options;
const layers = this.style._mergedLayers;
const drapingEnabled = !!(this.terrain && this.terrain.enabled);
const getLayerIds = () => this.style._getOrder(drapingEnabled).filter((id) => {
const layer = layers[id];
if (layer.type in this._debugParams.enabledLayers) {
return this._debugParams.enabledLayers[layer.type];
}
return true;
});
let layerIds = getLayerIds();
let layersRequireTerrainDepth = false;
let layersRequireFinalDepth = false;
let buildingLayer = null;
let conflationSourcesOrLayersInStyle = 0;
let conflationActiveThisFrame = false;
for (const id of layerIds) {
const layer = layers[id];
if (layer.visibility === "none") continue;
if (layer.type === "circle") {
layersRequireTerrainDepth = true;
} else if (layer.type === "building") {
buildingLayer = layer;
++conflationSourcesOrLayersInStyle;
} else if (layer.type === "symbol") {
if (layer.hasOcclusionOpacityProperties) {
layersRequireFinalDepth = true;
} else {
layersRequireTerrainDepth = true;
}
}
}
this.updateEmissiveMode();
let orderedLayers = layerIds.map((id) => layers[id]);
const sourceCaches = this.style._mergedSourceCaches;
this.imageManager = style.imageManager;
this.modelManager = style.modelManager;
this.symbolFadeChange = style.placement.symbolFadeChange(index$1.exported$1.now());
this.imageManager.beginFrame();
const prepareStartTime = index$1.PerformanceUtils.now();
for (const id in sourceCaches) {
const sourceCache = sourceCaches[id];
if (sourceCache.used) {
const sourceCachePrepareStartTime = index$1.PerformanceUtils.now();
sourceCache.prepare(this.context);
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, `prepare: ${sourceCache.id.toString()}`, sourceCachePrepareStartTime, void 0);
if (sourceCache.getSource().usedInConflation) {
++conflationSourcesOrLayersInStyle;
}
}
}
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, "sourceCaches: prepare", prepareStartTime, void 0);
let clippingActiveThisFrame = false;
for (const layer of orderedLayers) {
if (layer.isHidden(this.transform.zoom)) continue;
if (layer.type === "clip") {
clippingActiveThisFrame = true;
}
this.prepareLayer(layer);
}
const coordsAscending = {};
const coordsDescending = {};
const coordsDescendingSymbol = {};
const coordsShadowCasters = {};
const coordsSortedByDistance = {};
for (const id in sourceCaches) {
const sourceCache = sourceCaches[id];
coordsAscending[id] = sourceCache.getVisibleCoordinates();
coordsDescending[id] = coordsAscending[id].slice().reverse();
coordsDescendingSymbol[id] = sourceCache.getVisibleCoordinates(true).reverse();
coordsShadowCasters[id] = sourceCache.getShadowCasterCoordinates();
coordsSortedByDistance[id] = sourceCache.sortCoordinatesByDistance(coordsAscending[id]);
}
const getLayerSource = (layer) => {
const cache = this.style.getLayerSourceCache(layer);
if (!cache || !cache.used) return null;
return cache.getSource();
};
if (conflationSourcesOrLayersInStyle || clippingActiveThisFrame || this._clippingActiveLastFrame) {
const conflationLayersInStyle = [];
const conflationLayerIndicesInStyle = [];
let idx = 0;
for (const layer of orderedLayers) {
if (this.isSourceForClippingOrConflation(layer, getLayerSource(layer))) {
conflationLayersInStyle.push(layer);
conflationLayerIndicesInStyle.push(idx);
}
idx++;
}
if (conflationLayersInStyle && (clippingActiveThisFrame || conflationLayersInStyle.length > 1) || this._clippingActiveLastFrame) {
clippingActiveThisFrame = false;
const conflationSources = [];
for (let i = 0; i < conflationLayersInStyle.length; i++) {
const layer = conflationLayersInStyle[i];
const layerIdx = conflationLayerIndicesInStyle[i];
const sourceCache = this.style.getLayerSourceCache(layer);
if (!sourceCache || !sourceCache.used || !sourceCache.getSource().usedInConflation && layer.type !== "clip" && layer.type !== "building") {
continue;
}
let order = index$1.ReplacementOrderLandmark;
let clipMask = index$1.LayerTypeMask.None;
const clipScope = [];
let addToSources = true;
if (layer.type === "building") {
order = index$1.ReplacementOrderBuilding;
} else if (layer.type === "clip") {
order = layerIdx;
for (const mask of layer.layout.get("clip-layer-types")) {
clipMask |= mask === "model" ? index$1.LayerTypeMask.Model : mask === "symbol" ? index$1.LayerTypeMask.Symbol : index$1.LayerTypeMask.FillExtrusion;
}
for (const scope of layer.layout.get("clip-layer-scope")) {
clipScope.push(scope);
}
if (layer.isHidden(this.transform.zoom)) {
addToSources = false;
} else {
clippingActiveThisFrame = true;
}
}
if (addToSources) {
conflationSources.push({ layer: layer.fqid, cache: sourceCache, order, clipMask, clipScope });
}
}
this.replacementSource.setSources(conflationSources);
conflationActiveThisFrame = true;
}
}
this._clippingActiveLastFrame = clippingActiveThisFrame;
if (!conflationActiveThisFrame) {
this.replacementSource.clear();
}
this.conflationActive = conflationActiveThisFrame;
this.minCutoffZoom = 0;
this.longestCutoffRange = 0;
this.opaquePassCutoff = Infinity;
this._lastOcclusionLayer = -1;
this.layersWithOcclusionOpacity = [];
for (let i = 0; i < orderedLayers.length; i++) {
const layer = orderedLayers[i];
if (layer.visibility === "none") continue;
const cutoffRange = layer.cutoffRange();
this.longestCutoffRange = Math.max(cutoffRange, this.longestCutoffRange);
if (cutoffRange > 0) {
const source = getLayerSource(layer);
if (source) {
this.minCutoffZoom = Math.max(source.minzoom, this.minCutoffZoom);
}
if (layer.minzoom) {
this.minCutoffZoom = Math.max(layer.minzoom, this.minCutoffZoom);
}
}
if (layer.is3D(drapingEnabled)) {
if (this.opaquePassCutoff === Infinity) {
this.opaquePassCutoff = i;
}
this._lastOcclusionLayer = i;
}
}
const fog = this.style && this.style.fog;
if (fog) {
this._fogVisible = fog.getOpacity(this.transform.pitch) !== 0;
if (this._fogVisible && this.transform.projection.name !== "globe") {
this._fogVisible = fog.isVisibleOnFrustum(this.transform.cameraFrustum);
}
} else {
this._fogVisible = false;
}
this._cachedTileFogOpacities = {};
if (this.terrain) {
this.terrain.updateTileBinding(coordsDescendingSymbol);
this.opaquePassCutoff = 0;
layerIds = getLayerIds();
orderedLayers = layerIds.map((id) => layers[id]);
}
const shadowRenderer = this._shadowRenderer;
if (shadowRenderer) {
shadowRenderer.updateShadowParameters(this.transform, this.style.directionalLight);
for (const id in sourceCaches) {
for (const coord of coordsAscending[id]) {
let tileHeight = { min: 0, max: 0 };
if (this.terrain) {
tileHeight = this.terrain.getMinMaxForTile(coord) || tileHeight;
}
shadowRenderer.addShadowReceiver(coord.toUnwrapped(), tileHeight.min, tileHeight.max);
}
}
}
if (this.transform.projection.name === "globe" && !this.globeSharedBuffers) {
this.globeSharedBuffers = new index$1.GlobeSharedBuffers(this.context);
}
if (this.style.fog && this.transform.projection.supportsFog) {
if (!this._atmosphere) {
this._atmosphere = new Atmosphere(this);
}
this._atmosphere.update(this);
} else {
if (this._atmosphere) {
this._atmosphere.destroy();
this._atmosphere = void 0;
}
}
const snow = this._debugParams.forceEnablePrecipitation || !!(this.style && this.style.snow);
const rain = this._debugParams.forceEnablePrecipitation || !!(this.style && this.style.rain);
if (snow && !this._snow) {
this._snow = new Snow(this);
}
if (!snow && this._snow) {
this._snow.destroy();
delete this._snow;
}
if (rain && !this._rain) {
this._rain = new Rain(this);
}
if (!rain && this._rain) {
this._rain.destroy();
delete this._rain;
}
if (this._snow) {
this._snow.update(this);
}
if (this._rain) {
this._rain.update(this);
}
if (buildingLayer) {
if (!this.buildingTileBorderManager) {
this.buildingTileBorderManager = new BuildingTileBorderManager();
}
const buildingLayerSourceCache = this.style.getLayerSourceCache(buildingLayer);
this.buildingTileBorderManager.updateBorders(buildingLayerSourceCache, buildingLayer);
}
if (!isMapAuthenticated(this.context.gl)) return;
this.renderPass = "offscreen";
for (const layer of orderedLayers) {
const sourceCache = style.getLayerSourceCache(layer);
if (!layer.hasOffscreenPass() || layer.isHidden(this.transform.zoom)) continue;
const coords = sourceCache ? coordsDescending[sourceCache.id] : void 0;
if (!(layer.type === "custom" || layer.type === "raster" || layer.type === "raster-particle" || layer.isSky()) && !(coords && coords.length)) continue;
this.renderLayer(this, sourceCache, layer, coords);
}
this.depthRangeFor3D = [0, 1 - (orderedLayers.length + 2) * this.numSublayers * this.depthEpsilon];
if (this._shadowRenderer) {
const shadowPassStartTime = index$1.PerformanceUtils.now();
this.renderPass = "shadow";
this._shadowRenderer.drawShadowPass(this.style, coordsShadowCasters);
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, "Shadow Pass", shadowPassStartTime);
}
this.context.bindFramebuffer.set(null);
this.context.viewport.set([0, 0, this.width, this.height]);
const shouldRenderAtmosphere = this.transform.projection.name === "globe" || this.transform.isHorizonVisible();
const clearColor = (() => {
if (options.showOverdrawInspector) {
return index$1.Color.black;
}
const fog2 = this.style.fog;
if (fog2 && this.transform.projection.supportsFog) {
const fogLUT = this.style.getLut(fog2.scope);
if (!shouldRenderAtmosphere) {
const ignoreLutColor = fog2.properties.get("color-use-theme") === "none";
const fogColor = fog2.properties.get("color").toNonPremultipliedRenderColor(ignoreLutColor ? null : fogLUT).toArray01();
return new index$1.Color(...fogColor);
}
if (shouldRenderAtmosphere) {
const ignoreLutColor = fog2.properties.get("space-color-use-theme") === "none";
const spaceColor = fog2.properties.get("space-color").toNonPremultipliedRenderColor(ignoreLutColor ? null : fogLUT).toArray01();
return new index$1.Color(...spaceColor);
}
}
return index$1.Color.transparent;
})();
this.context.clear({ color: clearColor, depth: 1 });
this.clearStencil();
this._showOverdrawInspector = options.showOverdrawInspector;
this.renderPass = "opaque";
const opaquePassStartTime = index$1.PerformanceUtils.now();
if (this.style.fog && this.transform.projection.supportsFog && this._atmosphere && !this._showOverdrawInspector && shouldRenderAtmosphere) {
this._atmosphere.drawStars(this, this.style.fog);
}
if (!this.terrain) {
for (this.currentLayer = layerIds.length - 1; this.currentLayer >= 0; this.currentLayer--) {
const layer = orderedLayers[this.currentLayer];
const sourceCache = style.getLayerSourceCache(layer);
if (layer.isSky()) continue;
const coords = sourceCache ? (layer.is3D(drapingEnabled) ? coordsSortedByDistance : coordsDescending)[sourceCache.id] : void 0;
this._renderTileClippingMasks(layer, sourceCache, coords);
this.renderLayer(this, sourceCache, layer, coords);
}
}
if (this.style.fog && this.transform.projection.supportsFog && this._atmosphere && !this._showOverdrawInspector && shouldRenderAtmosphere) {
this._atmosphere.drawAtmosphereGlow(this, this.style.fog);
}
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, "Opaque Pass", opaquePassStartTime);
this.renderPass = "sky";
const drawSkyOnGlobe = !this._atmosphere || index$1.globeToMercatorTransition(this.transform.zoom) > 0;
if (drawSkyOnGlobe && (this.transform.projection.name === "globe" || this.transform.isHorizonVisible())) {
for (this.currentLayer = 0; this.currentLayer < layerIds.length; this.currentLayer++) {
const layer = orderedLayers[this.currentLayer];
const sourceCache = style.getLayerSourceCache(layer);
if (!layer.isSky()) continue;
const coords = sourceCache ? coordsDescending[sourceCache.id] : void 0;
this.renderLayer(this, sourceCache, layer, coords);
}
}
this.renderPass = "translucent";
const translucentPassStartTime = index$1.PerformanceUtils.now();
function coordsForTranslucentLayer(layer, sourceCache) {
let coords;
if (sourceCache) {
const coordsSet = layer.type === "symbol" ? coordsDescendingSymbol : layer.is3D(drapingEnabled) ? coordsSortedByDistance : coordsDescending;
coords = coordsSet[sourceCache.id];
}
return coords;
}
const isGlobe = this.transform.projection.name === "globe";
if (isGlobe) {
this.renderElevatedRasterBackface = true;
this.currentLayer = 0;
while (this.currentLayer < layerIds.length) {
const layer = orderedLayers[this.currentLayer];
if (layer.type === "raster" || layer.type === "raster-particle") {
const sourceCache = style.getLayerSourceCache(layer);
this.renderLayer(this, sourceCache, layer, coordsForTranslucentLayer(layer, sourceCache));
}
++this.currentLayer;
}
this.renderElevatedRasterBackface = false;
}
this.currentLayer = 0;
this.firstLightBeamLayer = Number.MAX_SAFE_INTEGER;
let shadowLayers = 0;
if (shadowRenderer) {
shadowLayers = shadowRenderer.getShadowCastingLayerCount();
}
let terrainDepthCopied = false;
let last3DLayerIdx = -1;
for (let i = 0; i < layerIds.length; ++i) {
const layer = orderedLayers[i];
if (layer.isHidden(this.transform.zoom)) {
continue;
}
if (layer.is3D(drapingEnabled)) {
last3DLayerIdx = i;
}
}
if (layersRequireFinalDepth && last3DLayerIdx === -1) {
layersRequireTerrainDepth = true;
}
let depthPrepassRendered = false;
while (this.currentLayer < layerIds.length) {
const layer = orderedLayers[this.currentLayer];
const sourceCache = style.getLayerSourceCache(layer);
if (layer.isSky()) {
++this.currentLayer;
continue;
}
if (this.terrain && this.style.isLayerDraped(layer)) {
if (layer.isHidden(this.transform.zoom)) {
++this.currentLayer;
continue;
}
const prevLayer = this.currentLayer;
this.currentLayer = this.terrain.renderBatch(this.currentLayer);
this._lastOcclusionLayer = Math.max(this.currentLayer, this._lastOcclusionLayer);
index$1.assert(this.context.bindFramebuffer.current === null);
index$1.assert(this.currentLayer > prevLayer);
continue;
}
if (!depthPrepassRendered && layer.is3D(drapingEnabled) && !drapingEnabled) {
const saveCurrentLayer = this.currentLayer;
const renderDepthSubpass = (pass) => {
for (this.currentLayer = 0; this.currentLayer < orderedLayers.length; this.currentLayer++) {
const depthPassLayer = orderedLayers[this.currentLayer];
if (depthPrepass[depthPassLayer.type]) {
const sourceCache2 = this.style.getLayerSourceCache(depthPassLayer);
depthPrepass[depthPassLayer.type](this, sourceCache2, depthPassLayer, coordsForTranslucentLayer(depthPassLayer, sourceCache2), pass);
}
}
};
renderDepthSubpass("initialize");
renderDepthSubpass("reset");
this.currentLayer = saveCurrentLayer;
depthPrepassRendered = true;
}
if (layersRequireTerrainDepth && !terrainDepthCopied && this.terrain && !this.transform.isOrthographic) {
terrainDepthCopied = true;
this.blitDepth();
}
if (layersRequireFinalDepth && last3DLayerIdx !== -1 && this.currentLayer === last3DLayerIdx + 1 && !this.transform.isOrthographic) {
this.blitDepth();
}
if (!this.terrain) {
this._renderTileClippingMasks(layer, sourceCache, sourceCache ? coordsAscending[sourceCache.id] : void 0);
}
this.renderLayer(this, sourceCache, layer, coordsForTranslucentLayer(layer, sourceCache));
if (!this.terrain && shadowRenderer && shadowLayers > 0 && layer.hasShadowPass() && --shadowLayers === 0) {
{
this.clearStencil();
this.resetStencilClippingMasks();
const saveCurrentLayer = this.currentLayer;
for (this.currentLayer = 0; this.currentLayer < orderedLayers.length; this.currentLayer++) {
const maskLayer = orderedLayers[this.currentLayer];
if (groundShadowMask[maskLayer.type]) {
const sourceCache2 = this.style.getLayerSourceCache(maskLayer);
groundShadowMask[maskLayer.type](this, sourceCache2, maskLayer, coordsForTranslucentLayer(maskLayer, sourceCache2));
}
}
this.currentLayer = saveCurrentLayer;
}
shadowRenderer.drawGroundShadows();
if (this.firstLightBeamLayer <= this.currentLayer) {
const saveCurrentLayer = this.currentLayer;
this.renderPass = "light-beam";
for (this.currentLayer = this.firstLightBeamLayer; this.currentLayer <= saveCurrentLayer; this.currentLayer++) {
const layer2 = orderedLayers[this.currentLayer];
if (!layer2.hasLightBeamPass()) continue;
const sourceCache2 = style.getLayerSourceCache(layer2);
const coords = sourceCache2 ? coordsDescending[sourceCache2.id] : void 0;
this.renderLayer(this, sourceCache2, layer2, coords);
}
this.currentLayer = saveCurrentLayer;
this.renderPass = "translucent";
}
}
if (this.currentLayer >= this._lastOcclusionLayer && this.layersWithOcclusionOpacity.length > 0) {
const saveCurrentLayer = this.currentLayer;
this.depthOcclusion = true;
for (const current of this.layersWithOcclusionOpacity) {
this.currentLayer = current;
const layer2 = orderedLayers[this.currentLayer];
const sourceCache2 = style.getLayerSourceCache(layer2);
const coords = sourceCache2 ? coordsDescending[sourceCache2.id] : void 0;
if (!this.terrain) {
this._renderTileClippingMasks(layer2, sourceCache2, sourceCache2 ? coordsAscending[sourceCache2.id] : void 0);
}
this.renderLayer(this, sourceCache2, layer2, coords);
}
this.depthOcclusion = false;
this.currentLayer = saveCurrentLayer;
this.renderPass = "translucent";
this.layersWithOcclusionOpacity = [];
}
++this.currentLayer;
}
if (this.terrain) {
this.terrain.postRender();
}
if (this._snow) {
this._snow.draw(this);
}
if (this._rain) {
this._rain.draw(this);
}
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, "Translucent Pass", translucentPassStartTime);
if (this.options.showTileBoundaries || this.options.showQueryGeometry || this.options.showTileAABBs) {
let selectedSource = null;
orderedLayers.forEach((layer) => {
const sourceCache = style.getLayerSourceCache(layer);
if (sourceCache && !layer.isHidden(this.transform.zoom) && sourceCache.getVisibleCoordinates().length) {
if (!selectedSource || selectedSource.getSource().maxzoom < sourceCache.getSource().maxzoom) {
selectedSource = sourceCache;
}
}
});
if (selectedSource) {
if (this.options.showTileBoundaries) {
drawDebug(this, selectedSource, selectedSource.getVisibleCoordinates(), index$1.Color.red, false, this.options.showParseStatus);
}
Debug.run(() => {
if (!selectedSource) return;
if (this.options.showQueryGeometry) {
drawDebugQueryGeometry(this, selectedSource, selectedSource.getVisibleCoordinates());
}
if (this.options.showTileAABBs) {
Debug.drawAabbs(this, selectedSource, selectedSource.getVisibleCoordinates());
}
});
}
}
if (this.terrain && this._debugParams.showTerrainProxyTiles) {
drawDebug(this, this.terrain.proxySourceCache, this.terrain.proxyCoords, new index$1.Color(1, 0.8, 0.1, 1), true, this.options.showParseStatus);
}
if (this.options.showPadding) {
drawDebugPadding(this);
}
this.context.setDefault();
this.frameCounter = (this.frameCounter + 1) % Number.MAX_SAFE_INTEGER;
if (this.tileLoaded && this.options.speedIndexTiming) {
this.loadTimeStamps.push(performance.now());
this.saveCanvasCopy();
}
if (!conflationActiveThisFrame) {
this.conflationActive = false;
}
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING, "Painter.render", renderStartTime);
}
prepareLayer(layer) {
this.gpuTimingStart(layer);
const { unsupportedLayers } = this.transform.projection;
const isLayerSupported = unsupportedLayers ? !unsupportedLayers.includes(layer.type) : true;
const isCustomLayerWithTerrain = this.terrain && layer.type === "custom";
if (prepare[layer.type] && (isLayerSupported || isCustomLayerWithTerrain)) {
const sourceCache = this.style.getLayerSourceCache(layer);
prepare[layer.type](layer, sourceCache, this);
}
this.gpuTimingEnd();
}
renderLayer(painter, sourceCache, layer, coords) {
if (layer.isHidden(this.transform.zoom)) return;
if (layer.type !== "background" && layer.type !== "sky" && layer.type !== "custom" && layer.type !== "model" && layer.type !== "raster" && layer.type !== "raster-particle" && !(coords && coords.length)) return;
this.id = layer.id;
const startTime = index$1.PerformanceUtils.now();
this.gpuTimingStart(layer);
if ((!painter.transform.projection.unsupportedLayers || !painter.transform.projection.unsupportedLayers.includes(layer.type) || painter.terrain && layer.type === "custom") && layer.type !== "clip") {
draw[layer.type](painter, sourceCache, layer, coords, this.style.placement.variableOffsets, this.options.isInitialLoad);
}
this.gpuTimingEnd();
index$1.PerformanceUtils.measureLowOverhead(index$1.PerformanceUtils.GROUP_RENDERING_DETAILED, `renderLayer: ${layer.type.toString()}`, startTime, void 0);
}
gpuTimingStart(layer) {
if (!this.options.gpuTiming) return;
const ext = this.context.extTimerQuery;
const gl = this.context.gl;
let layerTimer = this.gpuTimers[layer.id];
if (!layerTimer) {
layerTimer = this.gpuTimers[layer.id] = {
calls: 0,
cpuTime: 0,
query: gl.createQuery()
};
}
layerTimer.calls++;
gl.beginQuery(ext.TIME_ELAPSED_EXT, layerTimer.query);
}
gpuTimingDeferredRenderStart() {
if (this.options.gpuTimingDeferredRender) {
const ext = this.context.extTimerQuery;
const gl = this.context.gl;
const query = gl.createQuery();
this.deferredRenderGpuTimeQueries.push(query);
gl.beginQuery(ext.TIME_ELAPSED_EXT, query);
}
}
gpuTimingDeferredRenderEnd() {
if (!this.options.gpuTimingDeferredRender) return;
const ext = this.context.extTimerQuery;
const gl = this.context.gl;
gl.endQuery(ext.TIME_ELAPSED_EXT);
}
gpuTimingEnd() {
if (!this.options.gpuTiming) return;
const ext = this.context.extTimerQuery;
const gl = this.context.gl;
gl.endQuery(ext.TIME_ELAPSED_EXT);
}
collectGpuTimers() {
const currentLayerTimers = this.gpuTimers;
this.gpuTimers = {};
return currentLayerTimers;
}
collectDeferredRenderGpuQueries() {
const currentQueries = this.deferredRenderGpuTimeQueries;
this.deferredRenderGpuTimeQueries = [];
return currentQueries;
}
queryGpuTimers(gpuTimers) {
const layers = {};
for (const layerId in gpuTimers) {
const gpuTimer = gpuTimers[layerId];
const ext = this.context.extTimerQuery;
const gl = this.context.gl;
const gpuTime = ext.getQueryParameter(gpuTimer.query, gl.QUERY_RESULT) / (1e3 * 1e3);
ext.deleteQueryEXT(gpuTimer.query);
layers[layerId] = gpuTime;
}
return layers;
}
queryGpuTimeDeferredRender(gpuQueries) {
if (!this.options.gpuTimingDeferredRender) return 0;
const gl = this.context.gl;
let gpuTime = 0;
for (const query of gpuQueries) {
gpuTime += gl.getQueryParameter(query, gl.QUERY_RESULT) / (1e3 * 1e3);
gl.deleteQuery(query);
}
return gpuTime;
}
/**
* Transform a matrix to incorporate the *-translate and *-translate-anchor properties into it.
* @param inViewportPixelUnitsUnits True when the units accepted by the matrix are in viewport pixels instead of tile units.
* @returns {Float32Array} matrix
* @private
*/
translatePosMatrix(matrix, tile, translate, translateAnchor, inViewportPixelUnitsUnits) {
if (!translate[0] && !translate[1]) return matrix;
const angle = inViewportPixelUnitsUnits ? translateAnchor === "map" ? this.transform.angle : 0 : translateAnchor === "viewport" ? -this.transform.angle : 0;
if (angle) {
const sinA = Math.sin(angle);
const cosA = Math.cos(angle);
translate = [
translate[0] * cosA - translate[1] * sinA,
translate[0] * sinA + translate[1] * cosA
];
}
const translation = [
inViewportPixelUnitsUnits ? translate[0] : index$1.pixelsToTileUnits(tile, translate[0], this.transform.zoom),
inViewportPixelUnitsUnits ? translate[1] : index$1.pixelsToTileUnits(tile, translate[1], this.transform.zoom),
0
];
const translatedMatrix = new Float32Array(16);
index$1.translate(translatedMatrix, matrix, translation);
return translatedMatrix;
}
/**
* Saves the tile texture for re-use when another tile is loaded.
*
* @returns true if the tile was cached, false if the tile was not cached and should be destroyed.
* @private
*/
saveTileTexture(texture) {
if (texture.context !== this.context) {
return;
}
const tileSize = texture.size[0];
const textures = this._tileTextures[tileSize];
if (!textures) {
this._tileTextures[tileSize] = [texture];
} else {
textures.push(texture);
}
}
getTileTexture(size) {
const textures = this._tileTextures[size];
return textures && textures.length > 0 ? textures.pop() : null;
}
terrainRenderModeElevated() {
return this.style && !!this.style.getTerrain() && !!this.terrain && !this.terrain.renderingToTexture || this.forceTerrainMode;
}
linearFloatFilteringSupported() {
const context = this.context;
return context.extTextureFloatLinear != null;
}
/**
* Returns #defines that would need to be injected into every Program
* based on the current state of Painter.
*
* @returns {string[]}
* @private
*/
currentGlobalDefines(name, overrideFog, overrideRtt) {
const rtt = overrideRtt === void 0 ? this.terrain && this.terrain.renderingToTexture : overrideRtt;
const defines = [];
if (this.style && this.style.enable3dLights()) {
if (name === "globeRaster" || name === "terrainRaster") {
defines.push("LIGHTING_3D_MODE");
defines.push("LIGHTING_3D_ALPHA_EMISSIVENESS");
} else {
if (!rtt) {
defines.push("LIGHTING_3D_MODE");
}
}
}
if (this.renderPass === "shadow") {
if (!this._shadowMapDebug) defines.push("DEPTH_TEXTURE");
}
if (this.terrainRenderModeElevated()) {
defines.push("TERRAIN");
if (this.linearFloatFilteringSupported()) defines.push("TERRAIN_DEM_FLOAT_FORMAT");
}
if (this.transform.projection.name === "globe") defines.push("GLOBE");
if (this._fogVisible && !rtt && (overrideFog === void 0 || overrideFog)) {
defines.push("FOG", "FOG_DITHERING");
}
if (rtt) defines.push("RENDER_TO_TEXTURE");
if (this._showOverdrawInspector) defines.push("OVERDRAW_INSPECTOR");
return defines;
}
getOrCreateProgram(name, options) {
this.cache = this.cache || {};
const defines = options && options.defines || [];
const config = options && options.config;
const overrideFog = options && options.overrideFog;
const overrideRtt = options && options.overrideRtt;
const globalDefines = this.currentGlobalDefines(name, overrideFog, overrideRtt);
const allDefines = globalDefines.concat(defines);
const key = Program.cacheKey(shaders[name], name, allDefines, config);
if (!this.cache[key]) {
this.cache[key] = new Program(this.context, name, shaders[name], config, programUniforms[name], allDefines);
}
return this.cache[key];
}
/*
* Reset some GL state to default values to avoid hard-to-debug bugs
* in custom layers.
*/
setCustomLayerDefaults() {
this.context.unbindVAO();
this.context.cullFace.setDefault();
this.context.frontFace.setDefault();
this.context.cullFaceSide.setDefault();
this.context.activeTexture.setDefault();
this.context.pixelStoreUnpack.setDefault();
this.context.pixelStoreUnpackPremultiplyAlpha.setDefault();
this.context.pixelStoreUnpackFlipY.setDefault();
}
/*
* Set GL state that is shared by all layers.
*/
setBaseState() {
const gl = this.context.gl;
this.context.cullFace.set(false);
this.context.viewport.set([0, 0, this.width, this.height]);
this.context.blendEquation.set(gl.FUNC_ADD);
}
initDebugOverlayCanvas() {
if (this.debugOverlayCanvas == null) {
this.debugOverlayCanvas = document.createElement("canvas");
this.debugOverlayCanvas.width = 512;
this.debugOverlayCanvas.height = 512;
const gl = this.context.gl;
this.debugOverlayTexture = new index$1.Texture(this.context, this.debugOverlayCanvas, gl.RGBA8);
}
}
destroy() {
if (this._terrain) {
this._terrain.destroy();
}
if (this._atmosphere) {
this._atmosphere.destroy();
this._atmosphere = void 0;
}
if (this.globeSharedBuffers) {
this.globeSharedBuffers.destroy();
}
this.emptyTexture.destroy();
if (this.debugOverlayTexture) {
this.debugOverlayTexture.destroy();
}
this._wireframeDebugCache.destroy();
if (this.depthFBO) {
this.depthFBO.destroy();
this.depthFBO = void 0;
this.depthTexture = void 0;
}
if (this.emptyDepthTexture) {
this.emptyDepthTexture.destroy();
}
}
prepareDrawTile() {
if (this.terrain) {
this.terrain.prepareDrawTile();
}
}
uploadCommonLightUniforms(context, program) {
if (this.style.enable3dLights()) {
const directionalLight = this.style.directionalLight;
const ambientLight = this.style.ambientLight;
if (directionalLight && ambientLight) {
const lightsUniforms = lightsUniformValues(directionalLight, ambientLight, this.style);
program.setLightsUniformValues(context, lightsUniforms);
}
}
}
uploadCommonUniforms(context, program, tileID, fogMatrix, cutoffParams) {
this.uploadCommonLightUniforms(context, program);
if (this.terrain && this.terrain.renderingToTexture) {
return;
}
const fog = this.style.fog;
if (fog) {
const fogOpacity = fog.getOpacity(this.transform.pitch);
const fogUniforms = fogUniformValues(
this,
fog,
tileID,
fogOpacity,
this.transform.frustumCorners.TL,
this.transform.frustumCorners.TR,
this.transform.frustumCorners.BR,
this.transform.frustumCorners.BL,
this.transform.globeCenterInViewSpace,
this.transform.globeRadius,
[
this.transform.width * index$1.exported$1.devicePixelRatio,
this.transform.height * index$1.exported$1.devicePixelRatio
],
fogMatrix
);
program.setFogUniformValues(context, fogUniforms);
}
if (cutoffParams) {
program.setCutoffUniformValues(context, cutoffParams.uniformValues);
}
}
setTileLoadedFlag(flag) {
this.tileLoaded = flag;
}
saveCanvasCopy() {
const canvas = this.canvasCopy();
if (!canvas) return;
this.frameCopies.push(canvas);
this.tileLoaded = false;
}
canvasCopy() {
const gl = this.context.gl;
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight, 0);
return texture;
}
getCanvasCopiesAndTimestamps() {
return {
canvasCopies: this.frameCopies,
timeStamps: this.loadTimeStamps
};
}
averageElevationNeedsEasing() {
if (!this.transform._elevation) return false;
const fog = this.style && this.style.fog;
if (!fog) return false;
const fogOpacity = fog.getOpacity(this.transform.pitch);
if (fogOpacity === 0) return false;
return true;
}
getBackgroundTiles() {
const oldTiles = this._backgroundTiles;
const newTiles = this._backgroundTiles = {};
const tileSize = 512;
const tileIDs = this.transform.coveringTiles({ tileSize });
for (const tileID of tileIDs) {
newTiles[tileID.key] = oldTiles[tileID.key] || new Tile(tileID, tileSize, this.transform.tileZoom, this, void 0, this.worldview);
}
return newTiles;
}
clearBackgroundTiles() {
this._backgroundTiles = {};
}
/*
* Replacement source's features get precedence over features defined in other sources.
* E.g. landmark features replace fill extrusion buildings at the same position.
* Initially planned to be used for Tiled3DModelSource, 2D source that is used with ModelLayer of buildings type and
* custom layer buildings.
*/
isSourceForClippingOrConflation(layer, source) {
if (!layer.is3D(!!(this.terrain && this.terrain.enabled))) {
return false;
}
if (layer.type === "clip") {
return true;
}
if (layer.type === "building") {
return true;
}
if (layer.minzoom && layer.minzoom > this.transform.zoom) {
return false;
}
if (!this.style._clipLayerPresent) {
if (layer.sourceLayer === "building" || layer.sourceLayer === "procedural_buildings") {
return true;
}
}
return !!source && source.type === "batched-model";
}
isTileAffectedByFog(id) {
if (!this.style || !this.style.fog) return false;
if (this.transform.projection.name === "globe") return true;
let cachedRange = this._cachedTileFogOpacities[id.key];
if (!cachedRange) {
this._cachedTileFogOpacities[id.key] = cachedRange = this.style.fog.getOpacityForTile(id);
}
return cachedRange[0] >= FOG_OPACITY_THRESHOLD || cachedRange[1] >= FOG_OPACITY_THRESHOLD;
}
// For native compatibility depth for occlusion is kept as before
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setupDepthForOcclusion(useDepthForOcclusion, program, uniforms) {
const context = this.context;
const gl = context.gl;
const uniformsPresent = !!uniforms;
if (!uniforms) {
uniforms = defaultTerrainUniforms();
}
context.activeTexture.set(gl.TEXTURE3);
if (useDepthForOcclusion && this.depthFBO && this.depthTexture) {
this.depthTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
uniforms["u_depth_size_inv"] = [1 / this.depthFBO.width, 1 / this.depthFBO.height];
const getUnpackDepthRangeParams = (depthRange) => {
const a = 2 / (depthRange[1] - depthRange[0]);
const b = -1 - 2 * depthRange[0] / (depthRange[1] - depthRange[0]);
return [a, b];
};
uniforms["u_depth_range_unpack"] = getUnpackDepthRangeParams(this.depthRangeFor3D);
uniforms["u_occluder_half_size"] = this.occlusionParams.occluderSize * 0.5;
uniforms["u_occlusion_depth_offset"] = this.occlusionParams.depthOffset;
} else {
this.emptyDepthTexture.bind(gl.NEAREST, gl.CLAMP_TO_EDGE);
}
context.activeTexture.set(gl.TEXTURE0);
if (!uniformsPresent) {
program.setTerrainUniformValues(context, uniforms);
}
}
updateEmissiveMode() {
if (this._forceEmissiveMode) return;
const hasDataDriven = this.style.hasDataDrivenEmissiveStrength();
if (!hasDataDriven) {
this.emissiveMode = "constant";
} else if (this.context.extBlendFuncExtended) {
this.emissiveMode = "dual-source-blending";
} else {
this.emissiveMode = "mrt-fallback";
}
}
}
function throttle(fn, time) {
let pending = false;
let timerId = null;
const later = () => {
timerId = null;
if (pending) {
fn();
timerId = setTimeout(later, time);
pending = false;
}
};
return () => {
pending = true;
if (!timerId) {
later();
}
return timerId;
};
}
class Hash {
constructor(hashName) {
this._hashName = hashName && encodeURIComponent(hashName);
index$1.bindAll([
"_getCurrentHash",
"_onHashChange",
"_updateHash"
], this);
this._updateHash = throttle(this._updateHashUnthrottled.bind(this), 30 * 1e3 / 100);
}
/*
* Map element to listen for coordinate changes
*
* @param {Object} map
* @returns {Hash} `this`
*/
addTo(map) {
this._map = map;
window.addEventListener("hashchange", this._onHashChange, false);
map.on("moveend", this._updateHash);
return this;
}
/*
* Removes hash
*
* @returns {Popup} `this`
*/
remove() {
if (!this._map) return this;
this._map.off("moveend", this._updateHash);
window.removeEventListener("hashchange", this._onHashChange, false);
clearTimeout(this._updateHash());
this._map = void 0;
return this;
}
getHashString() {
const map = this._map;
if (!map) return "";
const hash = getHashString(map);
if (this._hashName) {
const hashName = this._hashName;
let found = false;
const parts = location.hash.slice(1).split("&").map((part) => {
const key = part.split("=")[0];
if (key === hashName) {
found = true;
return `${key}=${hash}`;
}
return part;
}).filter((a) => a);
if (!found) {
parts.push(`${hashName}=${hash}`);
}
return `#${parts.join("&")}`;
}
return `#${hash}`;
}
_getCurrentHash() {
const hash = location.hash.replace("#", "");
if (this._hashName) {
let keyval;
hash.split("&").map(
(part) => part.split("=")
).forEach((part) => {
if (part[0] === this._hashName) {
keyval = part;
}
});
return (keyval ? keyval[1] || "" : "").split("/");
}
return hash.split("/");
}
_onHashChange() {
const map = this._map;
if (!map) return false;
const loc = this._getCurrentHash();
if (loc.length >= 3 && !loc.some((v) => isNaN(Number(v)))) {
const bearing = map.dragRotate.isEnabled() && map.touchZoomRotate.isEnabled() ? +(loc[3] || 0) : map.getBearing();
map.jumpTo({
center: [+loc[2], +loc[1]],
zoom: +loc[0],
bearing,
pitch: +(loc[4] || 0)
});
return true;
}
return false;
}
_updateHashUnthrottled() {
history.replaceState(history.state, "", location.href.replace(/(#.+)?$/, this.getHashString()));
}
}
function getHashString(map, mapFeedback) {
const center = map.getCenter(), zoom = Math.round(map.getZoom() * 100) / 100, precision = Math.ceil((zoom * Math.LN2 + Math.log(512 / 360 / 0.5)) / Math.LN10), m = Math.pow(10, precision), lng = Math.round(center.lng * m) / m, lat = Math.round(center.lat * m) / m, bearing = map.getBearing(), pitch = map.getPitch();
let hash = mapFeedback ? `/${lng}/${lat}/${zoom}` : `${zoom}/${lat}/${lng}`;
if (bearing || pitch) hash += `/${Math.round(bearing * 10) / 10}`;
if (pitch) hash += `/${Math.round(pitch)}`;
return hash;
}
const defaultInertiaOptions = {
linearity: 0.3,
easing: index$1.bezier(0, 0, 0.3, 1)
};
const defaultPanInertiaOptions = Object.assign({
deceleration: 2500,
maxSpeed: 1400
}, defaultInertiaOptions);
const defaultZoomInertiaOptions = Object.assign({
deceleration: 20,
maxSpeed: 1400
}, defaultInertiaOptions);
const defaultBearingInertiaOptions = Object.assign({
deceleration: 1e3,
maxSpeed: 360
}, defaultInertiaOptions);
const defaultPitchInertiaOptions = Object.assign({
deceleration: 1e3,
maxSpeed: 90
}, defaultInertiaOptions);
class HandlerInertia {
constructor(map) {
this._map = map;
this.clear();
}
clear() {
this._inertiaBuffer = [];
}
record(settings) {
this._drainInertiaBuffer();
this._inertiaBuffer.push({ time: index$1.exported$1.now(), settings });
}
_drainInertiaBuffer() {
const inertia = this._inertiaBuffer, now = index$1.exported$1.now(), cutoff = 160;
while (inertia.length > 0 && now - inertia[0].time > cutoff)
inertia.shift();
}
_onMoveEnd(panInertiaOptions) {
if (this._map._prefersReducedMotion()) {
return;
}
this._drainInertiaBuffer();
if (this._inertiaBuffer.length < 2) {
return;
}
const deltas = {
zoom: 0,
bearing: 0,
pitch: 0,
pan: new index$1.Point(0, 0),
pinchAround: void 0,
around: void 0
};
for (const { settings } of this._inertiaBuffer) {
deltas.zoom += settings.zoomDelta || 0;
deltas.bearing += settings.bearingDelta || 0;
deltas.pitch += settings.pitchDelta || 0;
if (settings.panDelta) deltas.pan._add(settings.panDelta);
if (settings.around) deltas.around = settings.around;
if (settings.pinchAround) deltas.pinchAround = settings.pinchAround;
}
const lastEntry = this._inertiaBuffer[this._inertiaBuffer.length - 1];
const duration = lastEntry.time - this._inertiaBuffer[0].time;
const easeOptions = {};
if (deltas.pan.mag()) {
const result = calculateEasing(deltas.pan.mag(), duration, Object.assign({}, defaultPanInertiaOptions, panInertiaOptions || {}));
easeOptions.offset = deltas.pan.mult(result.amount / deltas.pan.mag());
easeOptions.center = this._map.transform.center;
extendDuration(easeOptions, result);
}
if (deltas.zoom) {
const result = calculateEasing(deltas.zoom, duration, defaultZoomInertiaOptions);
easeOptions.zoom = this._map.transform.zoom + result.amount;
extendDuration(easeOptions, result);
}
if (deltas.bearing) {
const result = calculateEasing(deltas.bearing, duration, defaultBearingInertiaOptions);
easeOptions.bearing = this._map.transform.bearing + index$1.clamp(result.amount, -179, 179);
extendDuration(easeOptions, result);
}
if (deltas.pitch) {
const result = calculateEasing(deltas.pitch, duration, defaultPitchInertiaOptions);
easeOptions.pitch = this._map.transform.pitch + result.amount;
extendDuration(easeOptions, result);
}
if (easeOptions.zoom || easeOptions.bearing) {
const last = deltas.pinchAround === void 0 ? deltas.around : deltas.pinchAround;
easeOptions.around = last ? this._map.unproject(last) : this._map.getCenter();
}
this.clear();
easeOptions.noMoveStart = true;
return easeOptions;
}
}
function extendDuration(easeOptions, result) {
if (!easeOptions.duration || easeOptions.duration < result.duration) {
easeOptions.duration = result.duration;
easeOptions.easing = result.easing;
}
}
function calculateEasing(amount, inertiaDuration, inertiaOptions) {
const { maxSpeed, linearity, deceleration } = inertiaOptions;
const speed = index$1.clamp(
amount * linearity / (inertiaDuration / 1e3),
-maxSpeed,
maxSpeed
);
const duration = Math.abs(speed) / (deceleration * linearity);
return {
easing: inertiaOptions.easing,
duration: duration * 1e3,
amount: speed * (duration / 2)
};
}
class MapMouseEvent extends index$1.Event {
/**
* Prevents subsequent default processing of the event by the map.
*
* Calling this method will prevent the following default map behaviors:
*
* * On `mousedown` events, the behavior of {@link DragPanHandler}.
* * On `mousedown` events, the behavior of {@link DragRotateHandler}.
* * On `mousedown` events, the behavior of {@link BoxZoomHandler}.
* * On `dblclick` events, the behavior of {@link DoubleClickZoomHandler}.
*
* @example
* map.on('click', (e) => {
* e.preventDefault();
* });
*/
preventDefault() {
this._defaultPrevented = true;
}
/**
* `true` if `preventDefault` has been called.
* @private
*/
get defaultPrevented() {
return this._defaultPrevented;
}
/**
* @private
*/
constructor(type, map, originalEvent, data = {}) {
const point = mousePos(map.getCanvasContainer(), originalEvent);
const lngLat = map.unproject(point);
super(type, Object.assign({ point, lngLat, originalEvent }, data));
this._defaultPrevented = false;
this.target = map;
}
}
class MapTouchEvent extends index$1.Event {
/**
* Prevents subsequent default processing of the event by the map.
*
* Calling this method will prevent the following default map behaviors:
*
* * On `touchstart` events, the behavior of {@link DragPanHandler}.
* * On `touchstart` events, the behavior of {@link TouchZoomRotateHandler}.
*
* @example
* map.on('touchstart', (e) => {
* e.preventDefault();
* });
*/
preventDefault() {
this._defaultPrevented = true;
}
/**
* Returns `true` if `preventDefault` has been called.
* @private
*/
get defaultPrevented() {
return this._defaultPrevented;
}
/**
* @private
*/
constructor(type, map, originalEvent) {
const touches = type === "touchend" ? originalEvent.changedTouches : originalEvent.touches;
const points = touchPos(map.getCanvasContainer(), touches);
const lngLats = points.map((t) => map.unproject(t));
const point = points.reduce((prev, curr, i, arr) => {
return prev.add(curr.div(arr.length));
}, new index$1.Point(0, 0));
const lngLat = map.unproject(point);
super(type, { points, point, lngLats, lngLat, originalEvent });
this._defaultPrevented = false;
}
}
class MapWheelEvent extends index$1.Event {
/**
* Prevents subsequent default processing of the event by the map.
* Calling this method will prevent the the behavior of {@link ScrollZoomHandler}.
*
* @example
* map.on('wheel', (e) => {
* // Prevent the default map scroll zoom behavior.
* e.preventDefault();
* });
*/
preventDefault() {
this._defaultPrevented = true;
}
/**
* `true` if `preventDefault` has been called.
* @private
*/
get defaultPrevented() {
return this._defaultPrevented;
}
/**
* @private
*/
constructor(map, originalEvent) {
super("wheel", { originalEvent });
this._defaultPrevented = false;
}
}
class MapEventHandler {
constructor(map, options) {
this._map = map;
this._clickTolerance = options.clickTolerance;
}
reset() {
this._mousedownPos = void 0;
}
wheel(e) {
return this._firePreventable(new MapWheelEvent(this._map, e));
}
mousedown(e, point) {
this._mousedownPos = point;
return this._firePreventable(new MapMouseEvent(e.type, this._map, e));
}
mouseup(e) {
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
preclick(e) {
const synth = new MouseEvent("preclick", e);
this._map.fire(new MapMouseEvent(synth.type, this._map, synth));
}
click(e, point) {
if (this._mousedownPos && this._mousedownPos.dist(point) >= this._clickTolerance) return;
this.preclick(e);
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
dblclick(e) {
return this._firePreventable(new MapMouseEvent(e.type, this._map, e));
}
mouseover(e) {
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
mouseout(e) {
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
touchstart(e) {
return this._firePreventable(new MapTouchEvent(e.type, this._map, e));
}
touchmove(e) {
this._map.fire(new MapTouchEvent(e.type, this._map, e));
}
touchend(e) {
this._map.fire(new MapTouchEvent(e.type, this._map, e));
}
touchcancel(e) {
this._map.fire(new MapTouchEvent(e.type, this._map, e));
}
_firePreventable(mapEvent) {
this._map.fire(mapEvent);
if (mapEvent.defaultPrevented) {
return {};
}
}
isEnabled() {
return true;
}
isActive() {
return false;
}
enable() {
}
disable() {
}
}
class BlockableMapEventHandler {
constructor(map) {
this._map = map;
}
reset() {
this._delayContextMenu = false;
this._contextMenuEvent = void 0;
}
mousemove(e) {
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
mousedown() {
this._delayContextMenu = true;
}
mouseup() {
this._delayContextMenu = false;
if (this._contextMenuEvent) {
this._map.fire(new MapMouseEvent("contextmenu", this._map, this._contextMenuEvent));
delete this._contextMenuEvent;
}
}
contextmenu(e) {
if (this._delayContextMenu) {
this._contextMenuEvent = e;
} else {
this._map.fire(new MapMouseEvent(e.type, this._map, e));
}
if (this._map.listens("contextmenu")) {
e.preventDefault();
}
}
isEnabled() {
return true;
}
isActive() {
return false;
}
enable() {
}
disable() {
}
}
class BoxZoomHandler {
/**
* @private
*/
constructor(map, options) {
this._map = map;
this._el = map.getCanvasContainer();
this._container = map.getContainer();
this._clickTolerance = options.clickTolerance || 1;
}
/**
* Returns a Boolean indicating whether the "box zoom" interaction is enabled.
*
* @returns {boolean} Returns `true` if the "box zoom" interaction is enabled.
* @example
* const isBoxZoomEnabled = map.boxZoom.isEnabled();
*/
isEnabled() {
return !!this._enabled;
}
/**
* Returns a Boolean indicating whether the "box zoom" interaction is active (currently being used).
*
* @returns {boolean} Returns `true` if the "box zoom" interaction is active.
* @example
* const isBoxZoomActive = map.boxZoom.isActive();
*/
isActive() {
return !!this._active;
}
/**
* Enables the "box zoom" interaction.
*
* @example
* map.boxZoom.enable();
*/
enable() {
if (this.isEnabled()) return;
this._enabled = true;
}
/**
* Disables the "box zoom" interaction.
*
* @example
* map.boxZoom.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._enabled = false;
}
mousedown(e, point) {
if (!this.isEnabled()) return;
if (!(e.shiftKey && e.button === 0)) return;
disableDrag();
this._startPos = this._lastPos = point;
this._active = true;
}
mousemoveWindow(e, point) {
if (!this._active) return;
const pos = point;
const p0 = this._startPos;
const p1 = this._lastPos;
if (!p0 || !p1 || p1.equals(pos) || !this._box && pos.dist(p0) < this._clickTolerance) {
return;
}
this._lastPos = pos;
if (!this._box) {
this._box = create$1("div", "mapboxgl-boxzoom", this._container);
this._container.classList.add("mapboxgl-crosshair");
this._fireEvent("boxzoomstart", e);
}
const minX = Math.min(p0.x, pos.x), maxX = Math.max(p0.x, pos.x), minY = Math.min(p0.y, pos.y), maxY = Math.max(p0.y, pos.y);
this._map._requestDomTask(() => {
if (this._box) {
this._box.style.transform = `translate(${minX}px,${minY}px)`;
this._box.style.width = `${maxX - minX}px`;
this._box.style.height = `${maxY - minY}px`;
}
});
}
mouseupWindow(e, point) {
if (!this._active) return;
const p0 = this._startPos, p1 = point;
if (!p0 || e.button !== 0) return;
this.reset();
suppressClick();
if (p0.x === p1.x && p0.y === p1.y) {
this._fireEvent("boxzoomcancel", e);
} else {
this._map.fire(new index$1.Event("boxzoomend", { originalEvent: e }));
return {
cameraAnimation: (map) => map.fitScreenCoordinates(p0, p1, this._map.getBearing(), { linear: false })
};
}
}
keydown(e) {
if (!this._active) return;
if (e.keyCode === 27) {
this.reset();
this._fireEvent("boxzoomcancel", e);
}
}
blur() {
this.reset();
}
reset() {
this._active = false;
this._container.classList.remove("mapboxgl-crosshair");
if (this._box) {
this._box.remove();
this._box = null;
}
enableDrag();
delete this._startPos;
delete this._lastPos;
}
_fireEvent(type, e) {
return this._map.fire(new index$1.Event(type, { originalEvent: e }));
}
}
function indexTouches(touches, points) {
index$1.assert(touches.length === points.length);
const obj = {};
for (let i = 0; i < touches.length; i++) {
obj[touches[i].identifier] = points[i];
}
return obj;
}
function getCentroid(points) {
const sum = new index$1.Point(0, 0);
for (const point of points) {
sum._add(point);
}
return sum.div(points.length);
}
const MAX_TAP_INTERVAL = 500;
const MAX_TOUCH_TIME = 500;
const MAX_DIST = 30;
class SingleTapRecognizer {
constructor(options) {
this.reset();
this.numTouches = options.numTouches;
}
reset() {
this.centroid = void 0;
this.startTime = 0;
this.touches = {};
this.aborted = false;
}
touchstart(e, points, mapTouches) {
if (this.centroid || mapTouches.length > this.numTouches) {
this.aborted = true;
}
if (this.aborted) {
return;
}
if (this.startTime === 0) {
this.startTime = e.timeStamp;
}
if (mapTouches.length === this.numTouches) {
this.centroid = getCentroid(points);
this.touches = indexTouches(mapTouches, points);
}
}
touchmove(e, points, mapTouches) {
if (this.aborted || !this.centroid) return;
const newTouches = indexTouches(mapTouches, points);
for (const id in this.touches) {
const prevPos = this.touches[id];
const pos = newTouches[id];
if (!pos || pos.dist(prevPos) > MAX_DIST) {
this.aborted = true;
}
}
}
touchend(e, points, mapTouches) {
if (!this.centroid || e.timeStamp - this.startTime > MAX_TOUCH_TIME) {
this.aborted = true;
}
if (mapTouches.length === 0) {
const centroid = !this.aborted && this.centroid;
this.reset();
if (centroid) return centroid;
}
}
}
class TapRecognizer {
constructor(options) {
this.singleTap = new SingleTapRecognizer(options);
this.numTaps = options.numTaps;
this.reset();
}
reset() {
this.lastTime = Infinity;
this.lastTap = void 0;
this.count = 0;
this.singleTap.reset();
}
touchstart(e, points, mapTouches) {
this.singleTap.touchstart(e, points, mapTouches);
}
touchmove(e, points, mapTouches) {
this.singleTap.touchmove(e, points, mapTouches);
}
touchend(e, points, mapTouches) {
const tap = this.singleTap.touchend(e, points, mapTouches);
if (tap) {
const soonEnough = e.timeStamp - this.lastTime < MAX_TAP_INTERVAL;
const closeEnough = !this.lastTap || this.lastTap.dist(tap) < MAX_DIST;
if (!soonEnough || !closeEnough) {
this.reset();
}
this.count++;
this.lastTime = e.timeStamp;
this.lastTap = tap;
if (this.count === this.numTaps) {
this.reset();
return tap;
}
}
}
}
class TapZoomHandler {
constructor() {
this._zoomIn = new TapRecognizer({
numTouches: 1,
numTaps: 2
});
this._zoomOut = new TapRecognizer({
numTouches: 2,
numTaps: 1
});
this.reset();
}
reset() {
this._active = false;
this._zoomIn.reset();
this._zoomOut.reset();
}
touchstart(e, points, mapTouches) {
this._zoomIn.touchstart(e, points, mapTouches);
this._zoomOut.touchstart(e, points, mapTouches);
}
touchmove(e, points, mapTouches) {
this._zoomIn.touchmove(e, points, mapTouches);
this._zoomOut.touchmove(e, points, mapTouches);
}
touchend(e, points, mapTouches) {
const zoomInPoint = this._zoomIn.touchend(e, points, mapTouches);
const zoomOutPoint = this._zoomOut.touchend(e, points, mapTouches);
if (zoomInPoint) {
this._active = true;
e.preventDefault();
setTimeout(() => this.reset(), 0);
return {
cameraAnimation: (map) => map.easeTo({
duration: 300,
zoom: map.getZoom() + 1,
around: map.unproject(zoomInPoint)
}, { originalEvent: e })
};
} else if (zoomOutPoint) {
this._active = true;
e.preventDefault();
setTimeout(() => this.reset(), 0);
return {
cameraAnimation: (map) => map.easeTo({
duration: 300,
zoom: map.getZoom() - 1,
around: map.unproject(zoomOutPoint)
}, { originalEvent: e })
};
}
}
touchcancel() {
this.reset();
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
const LEFT_BUTTON = 0;
const RIGHT_BUTTON = 2;
const BUTTONS_FLAGS = {
[LEFT_BUTTON]: 1,
[RIGHT_BUTTON]: 2
};
const MODIFIER_KEYS = {
"Control": "ctrlKey",
"Alt": "altKey",
"Shift": "shiftKey",
"Meta": "metaKey"
};
function buttonStillPressed(e, button) {
const flag = BUTTONS_FLAGS[button];
return e.buttons === void 0 || (e.buttons & flag) !== flag;
}
class MouseHandler {
constructor(options) {
this.reset();
this._clickTolerance = options.clickTolerance || 1;
}
blur() {
this.reset();
}
reset() {
this._active = false;
this._moved = false;
this._lastPoint = void 0;
this._eventButton = void 0;
}
_correctButton(e, button) {
return false;
}
_move(lastPoint, point) {
return {};
}
mousedown(e, point) {
if (this._lastPoint) return;
const eventButton = mouseButton(e);
if (!this._correctButton(e, eventButton)) return;
this._lastPoint = point;
this._eventButton = eventButton;
}
mousemoveWindow(e, point) {
const lastPoint = this._lastPoint;
if (!lastPoint) return;
e.preventDefault();
if (this._eventButton != null && buttonStillPressed(e, this._eventButton)) {
this.reset();
return;
}
if (!this._moved && point.dist(lastPoint) < this._clickTolerance) return;
this._moved = true;
this._lastPoint = point;
return this._move(lastPoint, point);
}
mouseupWindow(e) {
if (!this._lastPoint) return;
const eventButton = mouseButton(e);
if (eventButton !== this._eventButton) return;
if (this._moved) suppressClick();
this.reset();
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
class MousePanHandler extends MouseHandler {
mousedown(e, point) {
super.mousedown(e, point);
if (this._lastPoint) this._active = true;
}
_correctButton(e, button) {
return button === LEFT_BUTTON && !e.ctrlKey;
}
_move(lastPoint, point) {
return {
around: point,
panDelta: point.sub(lastPoint)
};
}
}
class MouseRotateHandler extends MouseHandler {
constructor(options) {
super(options);
this._pitchRotateKey = options.pitchRotateKey ? MODIFIER_KEYS[options.pitchRotateKey] : void 0;
}
_correctButton(e, button) {
return this._pitchRotateKey ? button === LEFT_BUTTON && e[this._pitchRotateKey] : button === LEFT_BUTTON && e.ctrlKey || button === RIGHT_BUTTON;
}
_move(lastPoint, point) {
const degreesPerPixelMoved = 0.8;
const bearingDelta = (point.x - lastPoint.x) * degreesPerPixelMoved;
if (bearingDelta) {
this._active = true;
return { bearingDelta };
}
}
contextmenu(e) {
if (this._pitchRotateKey) return;
e.preventDefault();
}
}
class MousePitchHandler extends MouseHandler {
constructor(options) {
super(options);
this._pitchRotateKey = options.pitchRotateKey ? MODIFIER_KEYS[options.pitchRotateKey] : void 0;
}
_correctButton(e, button) {
return this._pitchRotateKey ? button === LEFT_BUTTON && e[this._pitchRotateKey] : button === LEFT_BUTTON && e.ctrlKey || button === RIGHT_BUTTON;
}
_move(lastPoint, point) {
const degreesPerPixelMoved = -0.5;
const pitchDelta = (point.y - lastPoint.y) * degreesPerPixelMoved;
if (pitchDelta) {
this._active = true;
return { pitchDelta };
}
}
contextmenu(e) {
if (this._pitchRotateKey) return;
e.preventDefault();
}
}
class TouchPanHandler {
constructor(map, options) {
this._map = map;
this._el = map.getCanvasContainer();
this._minTouches = 1;
this._clickTolerance = options.clickTolerance || 1;
this.reset();
index$1.bindAll(["_addTouchPanBlocker", "_showTouchPanBlockerAlert"], this);
}
reset() {
this._active = false;
this._touches = {};
this._sum = new index$1.Point(0, 0);
}
touchstart(e, points, mapTouches) {
return this._calculateTransform(e, points, mapTouches);
}
touchmove(e, points, mapTouches) {
if (!this._active || mapTouches.length < this._minTouches) return;
if (this._map._cooperativeGestures && !this._map.isMoving()) {
if (mapTouches.length === 1 && !index$1.isFullscreen()) {
this._showTouchPanBlockerAlert();
return;
} else if (this._alertContainer.style.visibility !== "hidden") {
this._alertContainer.style.visibility = "hidden";
clearTimeout(this._alertTimer);
}
}
if (e.cancelable) {
e.preventDefault();
}
return this._calculateTransform(e, points, mapTouches);
}
touchend(e, points, mapTouches) {
this._calculateTransform(e, points, mapTouches);
if (this._active && mapTouches.length < this._minTouches) {
this.reset();
}
}
touchcancel() {
this.reset();
}
_calculateTransform(e, points, mapTouches) {
if (mapTouches.length > 0) this._active = true;
const touches = indexTouches(mapTouches, points);
const touchPointSum = new index$1.Point(0, 0);
const touchDeltaSum = new index$1.Point(0, 0);
let touchDeltaCount = 0;
for (const identifier in touches) {
const point = touches[identifier];
const prevPoint = this._touches[identifier];
if (prevPoint) {
touchPointSum._add(point);
touchDeltaSum._add(point.sub(prevPoint));
touchDeltaCount++;
touches[identifier] = point;
}
}
this._touches = touches;
if (touchDeltaCount < this._minTouches || !touchDeltaSum.mag()) return;
const panDelta = touchDeltaSum.div(touchDeltaCount);
this._sum._add(panDelta);
if (this._sum.mag() < this._clickTolerance) return;
const around = touchPointSum.div(touchDeltaCount);
return {
around,
panDelta
};
}
enable() {
this._enabled = true;
if (this._map._cooperativeGestures) {
this._addTouchPanBlocker();
this._el.classList.add("mapboxgl-touch-pan-blocker-override", "mapboxgl-scrollable-page");
}
}
disable() {
this._enabled = false;
if (this._map._cooperativeGestures) {
clearTimeout(this._alertTimer);
this._alertContainer.remove();
this._el.classList.remove("mapboxgl-touch-pan-blocker-override", "mapboxgl-scrollable-page");
}
this.reset();
}
isEnabled() {
return !!this._enabled;
}
isActive() {
return !!this._active;
}
_addTouchPanBlocker() {
if (this._map && !this._alertContainer) {
this._alertContainer = create$1("div", "mapboxgl-touch-pan-blocker", this._map._container);
this._alertContainer.textContent = this._map._getUIString("TouchPanBlocker.Message");
this._alertContainer.style.fontSize = `${Math.max(10, Math.min(24, Math.floor(this._el.clientWidth * 0.05)))}px`;
}
}
_showTouchPanBlockerAlert() {
this._alertContainer.style.visibility = "visible";
this._alertContainer.classList.add("mapboxgl-touch-pan-blocker-show");
this._alertContainer.setAttribute("role", "alert");
clearTimeout(this._alertTimer);
this._alertTimer = window.setTimeout(() => {
this._alertContainer.classList.remove("mapboxgl-touch-pan-blocker-show");
this._alertContainer.removeAttribute("role");
}, 500);
}
}
class TwoTouchHandler {
constructor() {
this.reset();
}
reset() {
this._active = false;
this._firstTwoTouches = void 0;
}
_start(points) {
}
_move(points, pinchAround, e) {
return {};
}
touchstart(e, points, mapTouches) {
if (this._firstTwoTouches || mapTouches.length < 2) return;
this._firstTwoTouches = [
mapTouches[0].identifier,
mapTouches[1].identifier
];
this._start([points[0], points[1]]);
}
touchmove(e, points, mapTouches) {
const firstTouches = this._firstTwoTouches;
if (!firstTouches) return;
e.preventDefault();
const [idA, idB] = firstTouches;
const a = getTouchById(mapTouches, points, idA);
const b = getTouchById(mapTouches, points, idB);
if (!a || !b) return;
const pinchAround = this._aroundCenter ? null : a.add(b).div(2);
return this._move([a, b], pinchAround, e);
}
touchend(e, points, mapTouches) {
if (!this._firstTwoTouches) return;
const [idA, idB] = this._firstTwoTouches;
const a = getTouchById(mapTouches, points, idA);
const b = getTouchById(mapTouches, points, idB);
if (a && b) return;
if (this._active) suppressClick();
this.reset();
}
touchcancel() {
this.reset();
}
enable(options) {
this._enabled = true;
this._aroundCenter = !!options && options.around === "center";
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
function getTouchById(mapTouches, points, identifier) {
for (let i = 0; i < mapTouches.length; i++) {
if (mapTouches[i].identifier === identifier) return points[i];
}
}
const ZOOM_THRESHOLD = 0.1;
function getZoomDelta(distance, lastDistance) {
return Math.log2(distance / lastDistance);
}
class TouchZoomHandler extends TwoTouchHandler {
reset() {
super.reset();
this._distance = 0;
this._startDistance = 0;
}
_start(points) {
this._startDistance = this._distance = points[0].dist(points[1]);
}
_move(points, pinchAround) {
const lastDistance = this._distance;
this._distance = points[0].dist(points[1]);
if (!this._active && Math.abs(getZoomDelta(this._distance, this._startDistance)) < ZOOM_THRESHOLD) return;
this._active = true;
return {
zoomDelta: getZoomDelta(this._distance, lastDistance),
pinchAround
};
}
}
const ROTATION_THRESHOLD = 25;
function getBearingDelta(a, b) {
return a.angleWith(b) * 180 / Math.PI;
}
class TouchRotateHandler extends TwoTouchHandler {
reset() {
super.reset();
this._minDiameter = 0;
this._startVector = void 0;
this._vector = void 0;
}
_start(points) {
this._startVector = this._vector = points[0].sub(points[1]);
this._minDiameter = points[0].dist(points[1]);
}
_move(points, pinchAround) {
const lastVector = this._vector;
this._vector = points[0].sub(points[1]);
if (!lastVector || !this._active && this._isBelowThreshold(this._vector)) return;
this._active = true;
return {
bearingDelta: getBearingDelta(this._vector, lastVector),
pinchAround
};
}
_isBelowThreshold(vector) {
this._minDiameter = Math.min(this._minDiameter, vector.mag());
const circumference = Math.PI * this._minDiameter;
const threshold = ROTATION_THRESHOLD / circumference * 360;
const startVector = this._startVector;
if (!startVector) return false;
const bearingDeltaSinceStart = getBearingDelta(vector, startVector);
return Math.abs(bearingDeltaSinceStart) < threshold;
}
}
function isVertical(vector) {
return Math.abs(vector.y) > Math.abs(vector.x);
}
const ALLOWED_SINGLE_TOUCH_TIME = 100;
class TouchPitchHandler extends TwoTouchHandler {
constructor(map) {
super();
this._map = map;
}
reset() {
super.reset();
this._valid = void 0;
this._firstMove = void 0;
this._lastPoints = void 0;
}
_start(points) {
this._lastPoints = points;
if (isVertical(points[0].sub(points[1]))) {
this._valid = false;
}
}
_move(points, center, e) {
const lastPoints = this._lastPoints;
if (!lastPoints) return;
const vectorA = points[0].sub(lastPoints[0]);
const vectorB = points[1].sub(lastPoints[1]);
if (this._map._cooperativeGestures && !index$1.isFullscreen() && e.touches.length < 3) return;
this._valid = this.gestureBeginsVertically(vectorA, vectorB, e.timeStamp);
if (!this._valid) return;
this._lastPoints = points;
this._active = true;
const yDeltaAverage = (vectorA.y + vectorB.y) / 2;
const degreesPerPixelMoved = -0.5;
return {
pitchDelta: yDeltaAverage * degreesPerPixelMoved
};
}
gestureBeginsVertically(vectorA, vectorB, timeStamp) {
if (this._valid !== void 0) return this._valid;
const threshold = 2;
const movedA = vectorA.mag() >= threshold;
const movedB = vectorB.mag() >= threshold;
if (!movedA && !movedB) return;
if (!movedA || !movedB) {
if (this._firstMove == null) {
this._firstMove = timeStamp;
}
if (timeStamp - this._firstMove < ALLOWED_SINGLE_TOUCH_TIME) {
return void 0;
} else {
return false;
}
}
const isSameDirection = vectorA.y > 0 === vectorB.y > 0;
return isVertical(vectorA) && isVertical(vectorB) && isSameDirection;
}
}
const defaultOptions$6 = {
panStep: 100,
bearingStep: 15,
pitchStep: 10
};
class KeyboardHandler {
/**
* @private
*/
constructor() {
const stepOptions = defaultOptions$6;
this._panStep = stepOptions.panStep;
this._bearingStep = stepOptions.bearingStep;
this._pitchStep = stepOptions.pitchStep;
this._rotationDisabled = false;
}
blur() {
this.reset();
}
reset() {
this._active = false;
}
keydown(e) {
if (e.altKey || e.ctrlKey || e.metaKey) return;
let zoomDir = 0;
let bearingDir = 0;
let pitchDir = 0;
let xDir = 0;
let yDir = 0;
switch (e.keyCode) {
case 61:
case 107:
case 171:
case 187:
zoomDir = 1;
break;
case 189:
case 109:
case 173:
zoomDir = -1;
break;
case 37:
if (e.shiftKey) {
bearingDir = -1;
} else {
e.preventDefault();
xDir = -1;
}
break;
case 39:
if (e.shiftKey) {
bearingDir = 1;
} else {
e.preventDefault();
xDir = 1;
}
break;
case 38:
if (e.shiftKey) {
pitchDir = 1;
} else {
e.preventDefault();
yDir = -1;
}
break;
case 40:
if (e.shiftKey) {
pitchDir = -1;
} else {
e.preventDefault();
yDir = 1;
}
break;
default:
return;
}
if (this._rotationDisabled) {
bearingDir = 0;
pitchDir = 0;
}
return {
cameraAnimation: (map) => {
const zoom = map.getZoom();
map.easeTo({
duration: 300,
easeId: "keyboardHandler",
easing: easeOut,
zoom: zoomDir ? Math.round(zoom) + zoomDir * (e.shiftKey ? 2 : 1) : zoom,
bearing: map.getBearing() + bearingDir * this._bearingStep,
pitch: map.getPitch() + pitchDir * this._pitchStep,
offset: [-xDir * this._panStep, -yDir * this._panStep],
center: map.getCenter()
}, { originalEvent: e });
}
};
}
/**
* Enables the "keyboard rotate and zoom" interaction.
*
* @example
* map.keyboard.enable();
*/
enable() {
this._enabled = true;
}
/**
* Disables the "keyboard rotate and zoom" interaction.
*
* @example
* map.keyboard.disable();
*/
disable() {
this._enabled = false;
this.reset();
}
/**
* Returns a Boolean indicating whether the "keyboard rotate and zoom"
* interaction is enabled.
*
* @returns {boolean} `true` if the "keyboard rotate and zoom"
* interaction is enabled.
* @example
* const isKeyboardEnabled = map.keyboard.isEnabled();
*/
isEnabled() {
return this._enabled;
}
/**
* Returns true if the handler is enabled and has detected the start of a
* zoom/rotate gesture.
*
* @returns {boolean} `true` if the handler is enabled and has detected the
* start of a zoom/rotate gesture.
* @example
* const isKeyboardActive = map.keyboard.isActive();
*/
isActive() {
return this._active;
}
/**
* Disables the "keyboard pan/rotate" interaction, leaving the
* "keyboard zoom" interaction enabled.
*
* @example
* map.keyboard.disableRotation();
*/
disableRotation() {
this._rotationDisabled = true;
}
/**
* Enables the "keyboard pan/rotate" interaction.
*
* @example
* map.keyboard.enable();
* map.keyboard.enableRotation();
*/
enableRotation() {
this._rotationDisabled = false;
}
}
function easeOut(t) {
return t * (2 - t);
}
const wheelZoomDelta = 4.000244140625;
const defaultZoomRate = 1 / 100;
const wheelZoomRate = 1 / 450;
const maxScalePerFrame = 2;
class ScrollZoomHandler {
/**
* @private
*/
constructor(map, handler) {
this._map = map;
this._el = map.getCanvasContainer();
this._handler = handler;
this._delta = 0;
this._lastDelta = 0;
this._defaultZoomRate = defaultZoomRate;
this._wheelZoomRate = wheelZoomRate;
index$1.bindAll(["_onTimeout", "_addScrollZoomBlocker", "_showBlockerAlert"], this);
}
/**
* Sets the zoom rate of a trackpad.
*
* @param {number} [zoomRate=1/100] The rate used to scale trackpad movement to a zoom value.
* @example
* // Speed up trackpad zoom
* map.scrollZoom.setZoomRate(1 / 25);
*/
setZoomRate(zoomRate) {
this._defaultZoomRate = zoomRate;
}
/**
* Sets the zoom rate of a mouse wheel.
*
* @param {number} [wheelZoomRate=1/450] The rate used to scale mouse wheel movement to a zoom value.
* @example
* // Slow down zoom of mouse wheel
* map.scrollZoom.setWheelZoomRate(1 / 600);
*/
setWheelZoomRate(wheelZoomRate2) {
this._wheelZoomRate = wheelZoomRate2;
}
/**
* Returns a Boolean indicating whether the "scroll to zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "scroll to zoom" interaction is enabled.
* @example
* const isScrollZoomEnabled = map.scrollZoom.isEnabled();
*/
isEnabled() {
return !!this._enabled;
}
/*
* Active state is turned on and off with every scroll wheel event and is set back to false before the map
* render is called, so _active is not a good candidate for determining if a scroll zoom animation is in
* progress.
*/
isActive() {
return this._active || this._finishTimeout !== void 0;
}
isZooming() {
return !!this._zooming;
}
/**
* Enables the "scroll to zoom" interaction.
*
* @param {Object} [options] Options object.
* @param {string} [options.around] If "center" is passed, map will zoom around center of map.
*
* @example
* map.scrollZoom.enable();
* @example
* map.scrollZoom.enable({around: 'center'});
*/
enable(options) {
if (this.isEnabled()) return;
this._enabled = true;
this._aroundCenter = !!options && options.around === "center";
if (this._map._cooperativeGestures) this._addScrollZoomBlocker();
}
/**
* Disables the "scroll to zoom" interaction.
*
* @example
* map.scrollZoom.disable();
*/
disable() {
if (!this.isEnabled()) return;
this._enabled = false;
if (this._map._cooperativeGestures) {
clearTimeout(this._alertTimer);
this._alertContainer.remove();
}
}
wheel(e) {
if (!this.isEnabled()) return;
if (this._map._cooperativeGestures) {
if (!e.ctrlKey && !e.metaKey && !this.isZooming() && !index$1.isFullscreen()) {
this._showBlockerAlert();
return;
} else if (this._alertContainer.style.visibility !== "hidden") {
this._alertContainer.style.visibility = "hidden";
clearTimeout(this._alertTimer);
}
}
let value = e.deltaMode === WheelEvent.DOM_DELTA_LINE ? e.deltaY * 40 : e.deltaY;
const now = index$1.exported$1.now(), timeDelta = now - (this._lastWheelEventTime || 0);
this._lastWheelEventTime = now;
if (value !== 0 && value % wheelZoomDelta === 0) {
this._type = "wheel";
} else if (value !== 0 && Math.abs(value) < 4) {
this._type = "trackpad";
} else if (timeDelta > 400) {
this._type = null;
this._lastValue = value;
this._timeout = window.setTimeout(this._onTimeout, 40, e);
} else if (!this._type) {
this._type = Math.abs(timeDelta * value) < 200 ? "trackpad" : "wheel";
if (this._timeout) {
clearTimeout(this._timeout);
this._timeout = null;
value += this._lastValue;
}
}
if (e.shiftKey && value) value = value / 4;
if (this._type) {
this._lastWheelEvent = e;
this._delta -= value;
if (!this._active) {
this._start(e);
}
}
e.preventDefault();
}
_onTimeout(initialEvent) {
this._type = "wheel";
this._delta -= this._lastValue;
if (!this._active) {
this._start(initialEvent);
}
}
_start(e) {
if (!this._delta) return;
if (this._frameId) {
this._frameId = null;
}
this._active = true;
if (!this.isZooming()) {
this._zooming = true;
}
if (this._finishTimeout) {
clearTimeout(this._finishTimeout);
delete this._finishTimeout;
}
const pos = mousePos(this._el, e);
this._aroundPoint = this._aroundCenter ? this._map.transform.centerPoint : pos;
this._aroundCoord = this._map.transform.pointCoordinate3D(this._aroundPoint);
this._targetZoom = void 0;
if (!this._frameId) {
this._frameId = true;
this._handler._triggerRenderFrame();
}
}
renderFrame() {
if (!this._frameId) return;
this._frameId = null;
if (!this.isActive()) return;
const tr = this._map.transform;
if (this._type === "wheel" && tr.projection.wrap && (tr._center.lng >= 180 || tr._center.lng <= -180)) {
this._prevEase = null;
this._easing = null;
this._lastWheelEvent = null;
this._lastWheelEventTime = 0;
}
const startingZoom = () => {
return tr._terrainEnabled() && this._aroundCoord ? tr.computeZoomRelativeTo(this._aroundCoord) : tr.zoom;
};
if (this._delta !== 0) {
const zoomRate = this._type === "wheel" && Math.abs(this._delta) > wheelZoomDelta ? this._wheelZoomRate : this._defaultZoomRate;
let scale = maxScalePerFrame / (1 + Math.exp(-Math.abs(this._delta * zoomRate)));
if (this._delta < 0 && scale !== 0) {
scale = 1 / scale;
}
const startZoom2 = startingZoom();
const startScale = Math.pow(2, startZoom2);
const fromScale = typeof this._targetZoom === "number" ? tr.zoomScale(this._targetZoom) : startScale;
this._targetZoom = Math.min(tr.maxZoom, Math.max(tr.minZoom, tr.scaleZoom(fromScale * scale)));
if (this._type === "wheel") {
this._startZoom = startZoom2;
this._easing = this._smoothOutEasing(200);
}
this._lastDelta = this._delta;
this._delta = 0;
}
const targetZoom = typeof this._targetZoom === "number" ? this._targetZoom : startingZoom();
const startZoom = this._startZoom;
const easing = this._easing;
let finished = false;
let zoom;
if (this._type === "wheel" && startZoom && easing) {
index$1.assert(easing && typeof startZoom === "number");
const t = Math.min((index$1.exported$1.now() - this._lastWheelEventTime) / 200, 1);
const k = easing(t);
zoom = index$1.number(startZoom, targetZoom, k);
if (t < 1) {
if (!this._frameId) {
this._frameId = true;
}
} else {
finished = true;
}
} else {
zoom = targetZoom;
finished = true;
}
this._active = true;
if (finished) {
this._active = false;
this._finishTimeout = window.setTimeout(() => {
this._zooming = false;
this._handler._triggerRenderFrame();
delete this._targetZoom;
delete this._finishTimeout;
}, 200);
}
let zoomDelta = zoom - startingZoom();
if (zoomDelta * this._lastDelta < 0) {
zoomDelta = 0;
}
return {
noInertia: true,
needsRenderFrame: !finished,
zoomDelta,
around: this._aroundPoint,
aroundCoord: this._aroundCoord,
originalEvent: this._lastWheelEvent
};
}
_smoothOutEasing(duration) {
let easing = index$1.ease;
if (this._prevEase) {
const ease = this._prevEase, t = (index$1.exported$1.now() - ease.start) / ease.duration, speed = ease.easing(t + 0.01) - ease.easing(t), x = 0.27 / Math.sqrt(speed * speed + 1e-4) * 0.01, y = Math.sqrt(0.27 * 0.27 - x * x);
easing = index$1.bezier(x, y, 0.25, 1);
}
this._prevEase = {
start: index$1.exported$1.now(),
duration,
easing
};
return easing;
}
blur() {
this.reset();
}
reset() {
this._active = false;
}
_addScrollZoomBlocker() {
if (this._map && !this._alertContainer) {
this._alertContainer = create$1("div", "mapboxgl-scroll-zoom-blocker", this._map._container);
if (/(Mac|iPad)/i.test(navigator.userAgent)) {
this._alertContainer.textContent = this._map._getUIString("ScrollZoomBlocker.CmdMessage");
} else {
this._alertContainer.textContent = this._map._getUIString("ScrollZoomBlocker.CtrlMessage");
}
this._alertContainer.style.fontSize = `${Math.max(10, Math.min(24, Math.floor(this._el.clientWidth * 0.05)))}px`;
}
}
_showBlockerAlert() {
this._alertContainer.style.visibility = "visible";
this._alertContainer.classList.add("mapboxgl-scroll-zoom-blocker-show");
this._alertContainer.setAttribute("role", "alert");
clearTimeout(this._alertTimer);
this._alertTimer = window.setTimeout(() => {
this._alertContainer.classList.remove("mapboxgl-scroll-zoom-blocker-show");
this._alertContainer.removeAttribute("role");
}, 200);
}
}
class DoubleClickZoomHandler {
/**
* @private
*/
constructor(clickZoom, TapZoom) {
this._clickZoom = clickZoom;
this._tapZoom = TapZoom;
}
/**
* Enables the "double click to zoom" interaction.
*
* @example
* map.doubleClickZoom.enable();
*/
enable() {
this._clickZoom.enable();
this._tapZoom.enable();
}
/**
* Disables the "double click to zoom" interaction.
*
* @example
* map.doubleClickZoom.disable();
*/
disable() {
this._clickZoom.disable();
this._tapZoom.disable();
}
/**
* Returns a Boolean indicating whether the "double click to zoom" interaction is enabled.
*
* @returns {boolean} Returns `true` if the "double click to zoom" interaction is enabled.
* @example
* const isDoubleClickZoomEnabled = map.doubleClickZoom.isEnabled();
*/
isEnabled() {
return this._clickZoom.isEnabled() && this._tapZoom.isEnabled();
}
/**
* Returns a Boolean indicating whether the "double click to zoom" interaction is active (currently being used).
*
* @returns {boolean} Returns `true` if the "double click to zoom" interaction is active.
* @example
* const isDoubleClickZoomActive = map.doubleClickZoom.isActive();
*/
isActive() {
return this._clickZoom.isActive() || this._tapZoom.isActive();
}
}
class ClickZoomHandler {
constructor() {
this.reset();
}
reset() {
this._active = false;
}
blur() {
this.reset();
}
dblclick(e, point) {
e.preventDefault();
return {
cameraAnimation: (map) => {
map.easeTo({
duration: 300,
zoom: map.getZoom() + (e.shiftKey ? -1 : 1),
around: map.unproject(point)
}, { originalEvent: e });
}
};
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
class TapDragZoomHandler {
constructor() {
this._tap = new TapRecognizer({
numTouches: 1,
numTaps: 1
});
this.reset();
}
reset() {
this._active = false;
this._swipePoint = void 0;
this._swipeTouch = 0;
this._tapTime = 0;
this._tap.reset();
}
touchstart(e, points, mapTouches) {
if (this._swipePoint) return;
if (this._tapTime && e.timeStamp - this._tapTime > MAX_TAP_INTERVAL) {
this.reset();
}
if (!this._tapTime) {
this._tap.touchstart(e, points, mapTouches);
} else if (mapTouches.length > 0) {
this._swipePoint = points[0];
this._swipeTouch = mapTouches[0].identifier;
}
}
touchmove(e, points, mapTouches) {
if (!this._tapTime) {
this._tap.touchmove(e, points, mapTouches);
} else if (this._swipePoint) {
if (mapTouches[0].identifier !== this._swipeTouch) {
return;
}
const newSwipePoint = points[0];
const dist = newSwipePoint.y - this._swipePoint.y;
this._swipePoint = newSwipePoint;
e.preventDefault();
this._active = true;
return {
zoomDelta: dist / 128
};
}
}
touchend(e, points, mapTouches) {
if (!this._tapTime) {
const point = this._tap.touchend(e, points, mapTouches);
if (point) {
this._tapTime = e.timeStamp;
}
} else if (this._swipePoint) {
if (mapTouches.length === 0) {
this.reset();
}
}
}
touchcancel() {
this.reset();
}
enable() {
this._enabled = true;
}
disable() {
this._enabled = false;
this.reset();
}
isEnabled() {
return this._enabled;
}
isActive() {
return this._active;
}
}
class DragPanHandler {
/**
* @private
*/
constructor(el, mousePan, touchPan) {
this._el = el;
this._mousePan = mousePan;
this._touchPan = touchPan;
}
/**
* Enables the "drag to pan" interaction and accepts options to control the behavior of the panning inertia.
*
* @param {Object} [options] Options object.
* @param {number} [options.linearity=0] Factor used to scale the drag velocity.
* @param {Function} [options.easing] Optional easing function applied to {@link Map#panTo} when applying the drag. Defaults to bezier function using [@mapbox/unitbezier](https://github.com/mapbox/unitbezier).
* @param {number} [options.maxSpeed=1400] The maximum value of the drag velocity.
* @param {number} [options.deceleration=2500] The rate at which the speed reduces after the pan ends.
*
* @example
* map.dragPan.enable();
* @example
* map.dragPan.enable({
* linearity: 0.3,
* easing: t => t,
* maxSpeed: 1400,
* deceleration: 2500
* });
* @see [Example: Highlight features within a bounding box](https://docs.mapbox.com/mapbox-gl-js/example/using-box-queryrenderedfeatures/)
*/
enable(options) {
this._inertiaOptions = options || {};
this._mousePan.enable();
this._touchPan.enable();
this._el.classList.add("mapboxgl-touch-drag-pan");
}
/**
* Disables the "drag to pan" interaction.
*
* @example
* map.dragPan.disable();
*/
disable() {
this._mousePan.disable();
this._touchPan.disable();
this._el.classList.remove("mapboxgl-touch-drag-pan");
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is enabled.
*
* @returns {boolean} Returns `true` if the "drag to pan" interaction is enabled.
* @example
* const isDragPanEnabled = map.dragPan.isEnabled();
*/
isEnabled() {
return this._mousePan.isEnabled() && this._touchPan.isEnabled();
}
/**
* Returns a Boolean indicating whether the "drag to pan" interaction is active (currently being used).
*
* @returns {boolean} Returns `true` if the "drag to pan" interaction is active.
* @example
* const isDragPanActive = map.dragPan.isActive();
*/
isActive() {
return this._mousePan.isActive() || this._touchPan.isActive();
}
}
class DragRotateHandler {
/**
* @param {Object} [options]
* @param {number} [options.bearingSnap] The threshold, measured in degrees, that determines when the map's
* bearing will snap to north.
* @param {bool} [options.pitchWithRotate=true] Control the map pitch in addition to the bearing
* @private
*/
constructor(options, mouseRotate, mousePitch) {
this._pitchWithRotate = options.pitchWithRotate;
this._mouseRotate = mouseRotate;
this._mousePitch = mousePitch;
}
/**
* Enables the "drag to rotate" interaction.
*
* @example
* map.dragRotate.enable();
*/
enable() {
this._mouseRotate.enable();
if (this._pitchWithRotate) this._mousePitch.enable();
}
/**
* Disables the "drag to rotate" interaction.
*
* @example
* map.dragRotate.disable();
*/
disable() {
this._mouseRotate.disable();
this._mousePitch.disable();
}
/**
* Returns a Boolean indicating whether the "drag to rotate" interaction is enabled.
*
* @returns {boolean} `true` if the "drag to rotate" interaction is enabled.
* @example
* const isDragRotateEnabled = map.dragRotate.isEnabled();
*/
isEnabled() {
return this._mouseRotate.isEnabled() && (!this._pitchWithRotate || this._mousePitch.isEnabled());
}
/**
* Returns a Boolean indicating whether the "drag to rotate" interaction is active (currently being used).
*
* @returns {boolean} Returns `true` if the "drag to rotate" interaction is active.
* @example
* const isDragRotateActive = map.dragRotate.isActive();
*/
isActive() {
return this._mouseRotate.isActive() || this._mousePitch.isActive();
}
}
class TouchZoomRotateHandler {
/**
* @private
*/
constructor(el, touchZoom, touchRotate, tapDragZoom) {
this._el = el;
this._touchZoom = touchZoom;
this._touchRotate = touchRotate;
this._tapDragZoom = tapDragZoom;
this._rotationDisabled = false;
this._enabled = true;
}
/**
* Enables the "pinch to rotate and zoom" interaction.
*
* @param {Object} [options] Options object.
* @param {string} [options.around] If "center" is passed, map will zoom around the center.
*
* @example
* map.touchZoomRotate.enable();
* @example
* map.touchZoomRotate.enable({around: 'center'});
*/
enable(options) {
this._touchZoom.enable(options);
if (!this._rotationDisabled) this._touchRotate.enable(options);
this._tapDragZoom.enable();
this._el.classList.add("mapboxgl-touch-zoom-rotate");
}
/**
* Disables the "pinch to rotate and zoom" interaction.
*
* @example
* map.touchZoomRotate.disable();
*/
disable() {
this._touchZoom.disable();
this._touchRotate.disable();
this._tapDragZoom.disable();
this._el.classList.remove("mapboxgl-touch-zoom-rotate");
}
/**
* Returns a Boolean indicating whether the "pinch to rotate and zoom" interaction is enabled.
*
* @returns {boolean} `true` if the "pinch to rotate and zoom" interaction is enabled.
* @example
* const isTouchZoomRotateEnabled = map.touchZoomRotate.isEnabled();
*/
isEnabled() {
return this._touchZoom.isEnabled() && (this._rotationDisabled || this._touchRotate.isEnabled()) && this._tapDragZoom.isEnabled();
}
/**
* Returns true if the handler is enabled and has detected the start of a zoom/rotate gesture.
*
* @returns {boolean} `true` if enabled and a zoom/rotate gesture was detected.
* @example
* const isTouchZoomRotateActive = map.touchZoomRotate.isActive();
*/
isActive() {
return this._touchZoom.isActive() || this._touchRotate.isActive() || this._tapDragZoom.isActive();
}
/**
* Disables the "pinch to rotate" interaction, leaving the "pinch to zoom"
* interaction enabled.
*
* @example
* map.touchZoomRotate.disableRotation();
*/
disableRotation() {
this._rotationDisabled = true;
this._touchRotate.disable();
}
/**
* Enables the "pinch to rotate" interaction.
*
* @example
* map.touchZoomRotate.enable();
* map.touchZoomRotate.enableRotation();
*/
enableRotation() {
this._rotationDisabled = false;
if (this._touchZoom.isEnabled()) this._touchRotate.enable();
}
}
const isMoving = (p) => p.zoom || p.drag || p.pitch || p.rotate;
class RenderFrameEvent extends index$1.Event {
}
class TrackingEllipsoid {
constructor() {
this.constants = [1, 1, 0.01];
this.radius = 0;
}
setup(center, pointOnSurface) {
const centerToSurface = index$1.sub([], pointOnSurface, center);
if (centerToSurface[2] < 0) {
this.radius = index$1.length(index$1.div([], centerToSurface, this.constants));
} else {
this.radius = index$1.length([centerToSurface[0], centerToSurface[1], 0]);
}
}
// Cast a ray from the center of the ellipsoid and the intersection point.
projectRay(dir) {
index$1.div(dir, dir, this.constants);
index$1.normalize(dir, dir);
index$1.mul$1(dir, dir, this.constants);
const intersection = index$1.scale$1([], dir, this.radius);
if (intersection[2] > 0) {
const h = index$1.scale$1([], [0, 0, 1], index$1.dot(intersection, [0, 0, 1]));
const r = index$1.scale$1([], index$1.normalize([], [intersection[0], intersection[1], 0]), this.radius);
const p = index$1.add$1([], intersection, index$1.scale$1([], index$1.sub([], index$1.add$1([], r, h), intersection), 2));
intersection[0] = p[0];
intersection[1] = p[1];
}
return intersection;
}
}
function hasChange(result) {
return result.panDelta && result.panDelta.mag() || result.zoomDelta || result.bearingDelta || result.pitchDelta;
}
class HandlerManager {
constructor(map, options) {
this._map = map;
this._el = this._map.getCanvasContainer();
this._handlers = [];
this._handlersById = {};
this._changes = [];
this._inertia = new HandlerInertia(map);
this._bearingSnap = options.bearingSnap;
this._previousActiveHandlers = {};
this._trackingEllipsoid = new TrackingEllipsoid();
this._dragOrigin = null;
this._eventsInProgress = {};
this._addDefaultHandlers(options);
index$1.bindAll(["handleEvent", "handleWindowEvent"], this);
const el = this._el;
this._listeners = [
// This needs to be `passive: true` so that a double tap fires two
// pairs of touchstart/end events in iOS Safari 13. If this is set to
// `passive: false` then the second pair of events is only fired if
// preventDefault() is called on the first touchstart. Calling preventDefault()
// undesirably prevents click events.
[el, "touchstart", { passive: true }],
// This needs to be `passive: false` so that scrolls and pinches can be
// prevented in browsers that don't support `touch-actions: none`, for example iOS Safari 12.
[el, "touchmove", { passive: false }],
[el, "touchend", void 0],
[el, "touchcancel", void 0],
[el, "mousedown", void 0],
[el, "mousemove", void 0],
[el, "mouseup", void 0],
// Bind window-level event listeners for move and up/end events. In the absence of
// the pointer capture API, which is not supported by all necessary platforms,
// window-level event listeners give us the best shot at capturing events that
// fall outside the map canvas element. Use `{capture: true}` for the move event
// to prevent map move events from being fired during a drag.
[document, "mousemove", { capture: true }],
[document, "mouseup", void 0],
[el, "mouseover", void 0],
[el, "mouseout", void 0],
[el, "dblclick", void 0],
[el, "click", void 0],
[el, "keydown", { capture: false }],
[el, "keyup", void 0],
[el, "wheel", { passive: false }],
[el, "contextmenu", void 0],
[window, "blur", void 0]
];
for (const [target, type, listenerOptions] of this._listeners) {
const listener = target === document ? this.handleWindowEvent : this.handleEvent;
target.addEventListener(type, listener, listenerOptions);
}
}
destroy() {
for (const [target, type, listenerOptions] of this._listeners) {
const listener = target === document ? this.handleWindowEvent : this.handleEvent;
target.removeEventListener(type, listener, listenerOptions);
}
}
_addDefaultHandlers(options) {
const map = this._map;
const el = map.getCanvasContainer();
this._add("mapEvent", new MapEventHandler(map, options));
const boxZoom = map.boxZoom = new BoxZoomHandler(map, options);
this._add("boxZoom", boxZoom);
const tapZoom = new TapZoomHandler();
const clickZoom = new ClickZoomHandler();
map.doubleClickZoom = new DoubleClickZoomHandler(clickZoom, tapZoom);
this._add("tapZoom", tapZoom);
this._add("clickZoom", clickZoom);
const tapDragZoom = new TapDragZoomHandler();
this._add("tapDragZoom", tapDragZoom);
const touchPitch = map.touchPitch = new TouchPitchHandler(map);
this._add("touchPitch", touchPitch);
const mouseRotate = new MouseRotateHandler(options);
const mousePitch = new MousePitchHandler(options);
map.dragRotate = new DragRotateHandler(options, mouseRotate, mousePitch);
this._add("mouseRotate", mouseRotate, ["mousePitch"]);
this._add("mousePitch", mousePitch, ["mouseRotate"]);
const mousePan = new MousePanHandler(options);
const touchPan = new TouchPanHandler(map, options);
map.dragPan = new DragPanHandler(el, mousePan, touchPan);
this._add("mousePan", mousePan);
this._add("touchPan", touchPan, ["touchZoom", "touchRotate"]);
const touchRotate = new TouchRotateHandler();
const touchZoom = new TouchZoomHandler();
map.touchZoomRotate = new TouchZoomRotateHandler(el, touchZoom, touchRotate, tapDragZoom);
this._add("touchRotate", touchRotate, ["touchPan", "touchZoom"]);
this._add("touchZoom", touchZoom, ["touchPan", "touchRotate"]);
this._add("blockableMapEvent", new BlockableMapEventHandler(map));
const scrollZoom = map.scrollZoom = new ScrollZoomHandler(map, this);
this._add("scrollZoom", scrollZoom, ["mousePan"]);
const keyboard = map.keyboard = new KeyboardHandler();
this._add("keyboard", keyboard);
for (const name of ["boxZoom", "doubleClickZoom", "tapDragZoom", "touchPitch", "dragRotate", "dragPan", "touchZoomRotate", "scrollZoom", "keyboard"]) {
if (options.interactive && options[name]) {
map[name].enable(options[name]);
}
}
}
_add(handlerName, handler, allowed) {
this._handlers.push({ handlerName, handler, allowed });
this._handlersById[handlerName] = handler;
}
stop(allowEndAnimation) {
if (this._updatingCamera) return;
for (const { handler } of this._handlers) {
handler.reset();
}
this._inertia.clear();
this._fireEvents({}, {}, allowEndAnimation);
this._changes = [];
this._originalZoom = void 0;
}
isActive() {
for (const { handler } of this._handlers) {
if (handler.isActive()) return true;
}
return false;
}
isZooming() {
return !!this._eventsInProgress.zoom || this._map.scrollZoom.isZooming();
}
isRotating() {
return !!this._eventsInProgress.rotate;
}
isMoving() {
return !!isMoving(this._eventsInProgress) || this.isZooming();
}
_isDragging() {
return !!this._eventsInProgress.drag;
}
_blockedByActive(activeHandlers, allowed, myName) {
for (const name in activeHandlers) {
if (name === myName) continue;
if (!allowed || allowed.indexOf(name) < 0) {
return true;
}
}
return false;
}
handleWindowEvent(e) {
this.handleEvent(e, `${e.type}Window`);
}
_getMapTouches(touches) {
const mapTouches = [];
for (const t of touches) {
const target = t.target;
if (this._el.contains(target)) {
mapTouches.push(t);
}
}
return mapTouches;
}
handleEvent(e, eventName) {
this._updatingCamera = true;
index$1.assert(e.timeStamp !== void 0);
const isRenderFrame = e.type === "renderFrame";
const inputEvent = isRenderFrame ? void 0 : e;
const mergedHandlerResult = { needsRenderFrame: false };
const eventsInProgress = {};
const activeHandlers = {};
const mapTouches = e.touches ? this._getMapTouches(e.touches) : void 0;
const points = mapTouches ? touchPos(this._el, mapTouches) : isRenderFrame ? void 0 : (
// renderFrame event doesn't have any points
mousePos(this._el, e)
);
for (const { handlerName, handler, allowed } of this._handlers) {
if (!handler.isEnabled()) continue;
let data;
if (this._blockedByActive(activeHandlers, allowed, handlerName)) {
handler.reset();
} else {
if (handler[eventName || e.type]) {
data = handler[eventName || e.type](e, points, mapTouches);
this.mergeHandlerResult(mergedHandlerResult, eventsInProgress, data, handlerName, inputEvent);
if (data && data.needsRenderFrame) {
this._triggerRenderFrame();
}
}
}
if (data || handler.isActive()) {
activeHandlers[handlerName] = handler;
}
}
const deactivatedHandlers = {};
for (const name in this._previousActiveHandlers) {
if (!activeHandlers[name]) {
deactivatedHandlers[name] = inputEvent;
}
}
this._previousActiveHandlers = activeHandlers;
if (Object.keys(deactivatedHandlers).length || hasChange(mergedHandlerResult)) {
this._changes.push([mergedHandlerResult, eventsInProgress, deactivatedHandlers]);
this._triggerRenderFrame();
}
if (Object.keys(activeHandlers).length || hasChange(mergedHandlerResult)) {
this._map._stop(true);
}
this._updatingCamera = false;
const { cameraAnimation } = mergedHandlerResult;
if (cameraAnimation) {
this._inertia.clear();
this._fireEvents({}, {}, true);
this._changes = [];
cameraAnimation(this._map);
}
}
mergeHandlerResult(mergedHandlerResult, eventsInProgress, handlerResult, name, e) {
if (!handlerResult) return;
Object.assign(mergedHandlerResult, handlerResult);
const eventData = { handlerName: name, originalEvent: handlerResult.originalEvent || e };
if (handlerResult.zoomDelta !== void 0) {
eventsInProgress.zoom = eventData;
}
if (handlerResult.panDelta !== void 0) {
eventsInProgress.drag = eventData;
}
if (handlerResult.pitchDelta !== void 0) {
eventsInProgress.pitch = eventData;
}
if (handlerResult.bearingDelta !== void 0) {
eventsInProgress.rotate = eventData;
}
}
_applyChanges() {
const combined = {};
const combinedEventsInProgress = {};
const combinedDeactivatedHandlers = {};
for (const [change, eventsInProgress, deactivatedHandlers] of this._changes) {
if (change.panDelta) combined.panDelta = (combined.panDelta || new index$1.Point(0, 0))._add(change.panDelta);
if (change.zoomDelta) combined.zoomDelta = (combined.zoomDelta || 0) + change.zoomDelta;
if (change.bearingDelta) combined.bearingDelta = (combined.bearingDelta || 0) + change.bearingDelta;
if (change.pitchDelta) combined.pitchDelta = (combined.pitchDelta || 0) + change.pitchDelta;
if (change.around !== void 0) combined.around = change.around;
if (change.aroundCoord !== void 0) combined.aroundCoord = change.aroundCoord;
if (change.pinchAround !== void 0) combined.pinchAround = change.pinchAround;
if (change.noInertia) combined.noInertia = change.noInertia;
Object.assign(combinedEventsInProgress, eventsInProgress);
Object.assign(combinedDeactivatedHandlers, deactivatedHandlers);
}
this._updateMapTransform(combined, combinedEventsInProgress, combinedDeactivatedHandlers);
this._changes = [];
}
_updateMapTransform(combinedResult, combinedEventsInProgress, deactivatedHandlers) {
const map = this._map;
const tr = map.transform;
const eventStarted = (type) => {
const newEvent = combinedEventsInProgress[type];
return newEvent && !this._eventsInProgress[type];
};
const eventEnded = (type) => {
const event = this._eventsInProgress[type];
return event && !this._handlersById[event.handlerName].isActive();
};
const toVec3 = (p) => [p.x, p.y, p.z];
if (eventEnded("drag") && !hasChange(combinedResult)) {
const preZoom = tr.zoom;
tr.cameraElevationReference = "sea";
if (this._originalZoom != null && tr._orthographicProjectionAtLowPitch && tr.projection.name !== "globe" && tr.pitch === 0) {
tr.cameraElevationReference = "ground";
tr.zoom = this._originalZoom;
} else {
tr.recenterOnTerrain();
tr.cameraElevationReference = "ground";
}
if (preZoom !== tr.zoom) this._map._update(true);
}
if (tr._isCameraConstrained) map._stop(true);
if (!hasChange(combinedResult)) {
this._fireEvents(combinedEventsInProgress, deactivatedHandlers, true);
return;
}
let { panDelta, zoomDelta, bearingDelta, pitchDelta, around, aroundCoord, pinchAround } = combinedResult;
if (tr._isCameraConstrained) {
if (zoomDelta > 0) zoomDelta = 0;
tr._isCameraConstrained = false;
}
if (pinchAround !== void 0) {
around = pinchAround;
}
if ((zoomDelta || eventStarted("drag")) && around) {
this._dragOrigin = toVec3(tr.pointCoordinate3D(around));
this._originalZoom = tr.zoom;
this._trackingEllipsoid.setup(tr._camera.position, this._dragOrigin);
}
tr.cameraElevationReference = "sea";
map._stop(true);
around = around || map.transform.centerPoint;
if (bearingDelta) tr.bearing += bearingDelta;
if (pitchDelta) tr.pitch += pitchDelta;
tr._updateCameraState();
const panVec = [0, 0, 0];
if (panDelta) {
if (tr.projection.name === "mercator") {
index$1.assert(this._dragOrigin, "_dragOrigin should have been setup with a previous dragstart");
const startPoint = this._trackingEllipsoid.projectRay(tr.screenPointToMercatorRay(around).dir);
const endPoint = this._trackingEllipsoid.projectRay(tr.screenPointToMercatorRay(around.sub(panDelta)).dir);
panVec[0] = endPoint[0] - startPoint[0];
panVec[1] = endPoint[1] - startPoint[1];
} else {
const startPoint = tr.pointCoordinate(around);
if (tr.projection.name === "globe") {
panDelta = panDelta.rotate(-tr.angle);
const scale = tr._pixelsPerMercatorPixel / tr.worldSize;
panVec[0] = -panDelta.x * index$1.mercatorScale(index$1.latFromMercatorY(startPoint.y)) * scale;
panVec[1] = -panDelta.y * index$1.mercatorScale(tr.center.lat) * scale;
} else {
const endPoint = tr.pointCoordinate(around.sub(panDelta));
if (startPoint && endPoint) {
panVec[0] = endPoint.x - startPoint.x;
panVec[1] = endPoint.y - startPoint.y;
}
}
}
}
const originalZoom = tr.zoom;
const zoomVec = [0, 0, 0];
if (zoomDelta) {
const pickedPosition = aroundCoord ? toVec3(aroundCoord) : toVec3(tr.pointCoordinate3D(around));
const aroundRay = { dir: index$1.normalize([], index$1.sub([], pickedPosition, tr._camera.position)) };
if (aroundRay.dir[2] < 0) {
const movement = tr.zoomDeltaToMovement(pickedPosition, zoomDelta);
index$1.scale$1(zoomVec, aroundRay.dir, movement);
}
}
const translation = index$1.add$1(panVec, panVec, zoomVec);
tr._translateCameraConstrained(translation);
if (zoomDelta && Math.abs(tr.zoom - originalZoom) > 1e-4) {
tr.recenterOnTerrain();
}
tr.cameraElevationReference = "ground";
this._map._update();
if (!combinedResult.noInertia) this._inertia.record(combinedResult);
this._fireEvents(combinedEventsInProgress, deactivatedHandlers, true);
}
_fireEvents(newEventsInProgress, deactivatedHandlers, allowEndAnimation) {
const wasMoving = isMoving(this._eventsInProgress);
const nowMoving = isMoving(newEventsInProgress);
const startEvents = {};
for (const eventName in newEventsInProgress) {
const { originalEvent } = newEventsInProgress[eventName];
if (!this._eventsInProgress[eventName]) {
startEvents[`${eventName}start`] = originalEvent;
}
this._eventsInProgress[eventName] = newEventsInProgress[eventName];
}
if (!wasMoving && nowMoving) {
this._fireEvent("movestart", nowMoving.originalEvent);
}
for (const name in startEvents) {
this._fireEvent(name, startEvents[name]);
}
if (nowMoving) {
this._fireEvent("move", nowMoving.originalEvent);
}
for (const eventName in newEventsInProgress) {
const { originalEvent } = newEventsInProgress[eventName];
this._fireEvent(eventName, originalEvent);
}
const endEvents = {};
let originalEndEvent;
for (const eventName in this._eventsInProgress) {
const { handlerName, originalEvent } = this._eventsInProgress[eventName];
if (!this._handlersById[handlerName].isActive()) {
delete this._eventsInProgress[eventName];
originalEndEvent = deactivatedHandlers[handlerName] || originalEvent;
endEvents[`${eventName}end`] = originalEndEvent;
}
}
for (const name in endEvents) {
this._fireEvent(name, endEvents[name]);
}
const stillMoving = isMoving(this._eventsInProgress);
if (allowEndAnimation && (wasMoving || nowMoving) && !stillMoving) {
this._updatingCamera = true;
const inertialEase = this._inertia._onMoveEnd(this._map.dragPan._inertiaOptions);
const shouldSnapToNorth = (bearing) => bearing !== 0 && -this._bearingSnap < bearing && bearing < this._bearingSnap;
if (inertialEase) {
if (shouldSnapToNorth(inertialEase.bearing || this._map.getBearing())) {
inertialEase.bearing = 0;
}
this._map.easeTo(inertialEase, { originalEvent: originalEndEvent });
} else {
this._map.fire(new index$1.Event("moveend", { originalEvent: originalEndEvent }));
if (shouldSnapToNorth(this._map.getBearing())) {
this._map.resetNorth();
}
}
this._updatingCamera = false;
}
}
_fireEvent(type, event) {
const eventData = event ? { originalEvent: event } : {};
this._map.fire(new index$1.Event(type, eventData));
}
_requestFrame() {
this._map.triggerRepaint();
return this._map._renderTaskQueue.add((timeStamp) => {
this._frameId = void 0;
this.handleEvent(new RenderFrameEvent("renderFrame", { timeStamp }));
this._applyChanges();
});
}
_triggerRenderFrame() {
if (this._frameId === void 0) {
this._frameId = this._requestFrame();
}
}
}
const freeCameraNotSupportedWarning = "map.setFreeCameraOptions(...) and map.getFreeCameraOptions() are not yet supported for non-mercator projections.";
class Camera extends index$1.Evented {
constructor(transform, options) {
super();
this._moving = false;
this._zooming = false;
this.transform = transform;
this._bearingSnap = options.bearingSnap;
this._respectPrefersReducedMotion = options.respectPrefersReducedMotion !== false;
index$1.bindAll(["_renderFrameCallback"], this);
}
/** @section Camera */
/**
* Returns the map's geographical centerpoint.
*
* @memberof Map#
* @returns {LngLat} The map's geographical centerpoint.
* @example
* // Return a LngLat object such as {lng: 0, lat: 0}.
* const center = map.getCenter();
* // Access longitude and latitude values directly.
* const {lng, lat} = map.getCenter();
* @see [Tutorial: Use Mapbox GL JS in a React app](https://docs.mapbox.com/help/tutorials/use-mapbox-gl-js-with-react/#store-the-new-coordinates)
*/
getCenter() {
return new index$1.LngLat(this.transform.center.lng, this.transform.center.lat);
}
/**
* Sets the map's geographical centerpoint. Equivalent to `jumpTo({center: center})`.
*
* @memberof Map#
* @param {LngLatLike} center The centerpoint to set.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* map.setCenter([-74, 38]);
*/
setCenter(center, eventData) {
return this.jumpTo({ center }, eventData);
}
/**
* Pans the map by the specified offset.
*
* @memberof Map#
* @param {PointLike} offset The `x` and `y` coordinates by which to pan the map.
* @param {AnimationOptions | null} options An options object describing the destination and animation of the transition. We do not recommend using `options.offset` since this value will override the value of the `offset` parameter.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} `this` Returns itself to allow for method chaining.
* @example
* map.panBy([-74, 38]);
* @example
* // panBy with an animation of 5 seconds.
* map.panBy([-74, 38], {duration: 5000});
* @see [Example: Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
panBy(offset, options, eventData) {
offset = index$1.Point.convert(offset).mult(-1);
return this.panTo(this.transform.center, Object.assign({ offset }, options), eventData);
}
/**
* Pans the map to the specified location with an animated transition.
*
* @memberof Map#
* @param {LngLatLike} lnglat The location to pan the map to.
* @param {AnimationOptions | null} options Options describing the destination and animation of the transition.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* map.panTo([-74, 38]);
* @example
* // Specify that the panTo animation should last 5000 milliseconds.
* map.panTo([-74, 38], {duration: 5000});
* @see [Example: Update a feature in realtime](https://docs.mapbox.com/mapbox-gl-js/example/live-update-feature/)
*/
panTo(lnglat, options, eventData) {
return this.easeTo(Object.assign({
center: lnglat
}, options), eventData);
}
/**
* Returns the map's current zoom level.
*
* @memberof Map#
* @returns {number} The map's current zoom level.
* @example
* map.getZoom();
*/
getZoom() {
return this.transform.zoom;
}
/**
* Sets the map's zoom level. Equivalent to `jumpTo({zoom: zoom})`.
*
* @memberof Map#
* @param {number} zoom The zoom level to set (0-20).
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Zoom to the zoom level 5 without an animated transition
* map.setZoom(5);
*/
setZoom(zoom, eventData) {
this.jumpTo({ zoom }, eventData);
return this;
}
/**
* Zooms the map to the specified zoom level, with an animated transition.
*
* @memberof Map#
* @param {number} zoom The zoom level to transition to.
* @param {AnimationOptions | null} options Options object.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Zoom to the zoom level 5 without an animated transition
* map.zoomTo(5);
* // Zoom to the zoom level 8 with an animated transition
* map.zoomTo(8, {
* duration: 2000,
* offset: [100, 50]
* });
*/
zoomTo(zoom, options, eventData) {
return this.easeTo(Object.assign({
zoom
}, options), eventData);
}
/**
* Increases the map's zoom level by 1.
*
* @memberof Map#
* @param {AnimationOptions | null} options Options object.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // zoom the map in one level with a custom animation duration
* map.zoomIn({duration: 1000});
*/
zoomIn(options, eventData) {
this.zoomTo(this.getZoom() + 1, options, eventData);
return this;
}
/**
* Decreases the map's zoom level by 1.
*
* @memberof Map#
* @param {AnimationOptions | null} options Options object.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // zoom the map out one level with a custom animation offset
* map.zoomOut({offset: [80, 60]});
*/
zoomOut(options, eventData) {
this.zoomTo(this.getZoom() - 1, options, eventData);
return this;
}
/**
* Returns the map's current bearing. The bearing is the compass direction that is "up"; for example, a bearing
* of 90° orients the map so that east is up.
*
* @memberof Map#
* @returns {number} The map's current bearing.
* @example
* const bearing = map.getBearing();
* @see [Example: Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
getBearing() {
return this.transform.bearing;
}
/**
* Sets the map's bearing (rotation). The bearing is the compass direction that is "up"; for example, a bearing
* of 90° orients the map so that east is up.
*
* Equivalent to `jumpTo({bearing: bearing})`.
*
* @memberof Map#
* @param {number} bearing The desired bearing.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Rotate the map to 90 degrees.
* map.setBearing(90);
*/
setBearing(bearing, eventData) {
this.jumpTo({ bearing }, eventData);
return this;
}
/**
* Returns the current padding applied around the map viewport.
*
* @memberof Map#
* @returns {PaddingOptions} The current padding around the map viewport.
* @example
* const padding = map.getPadding();
*/
getPadding() {
return this.transform.padding;
}
/**
* Sets the padding in pixels around the viewport.
*
* Equivalent to `jumpTo({padding: padding})`.
*
* @memberof Map#
* @param {PaddingOptions} padding The desired padding. Format: {left: number, right: number, top: number, bottom: number}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Sets a left padding of 300px, and a top padding of 50px
* map.setPadding({left: 300, top: 50});
*/
setPadding(padding, eventData) {
this.jumpTo({ padding }, eventData);
return this;
}
/**
* Rotates the map to the specified bearing, with an animated transition. The bearing is the compass direction
* that is \"up\"; for example, a bearing of 90° orients the map so that east is up.
*
* @memberof Map#
* @param {number} bearing The desired bearing.
* @param {EasingOptions | null} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* map.rotateTo(30);
* @example
* // rotateTo with an animation of 2 seconds.
* map.rotateTo(30, {duration: 2000});
*/
rotateTo(bearing, options, eventData) {
return this.easeTo(Object.assign({
bearing
}, options), eventData);
}
/**
* Rotates the map so that north is up (0° bearing), with an animated transition.
*
* @memberof Map#
* @param {EasingOptions | null} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // resetNorth with an animation of 2 seconds.
* map.resetNorth({duration: 2000});
*/
resetNorth(options, eventData) {
this.rotateTo(0, Object.assign({ duration: 1e3 }, options), eventData);
return this;
}
/**
* Rotates and pitches the map so that north is up (0° bearing) and pitch is 0°, with an animated transition.
*
* @memberof Map#
* @param {EasingOptions | null} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // resetNorthPitch with an animation of 2 seconds.
* map.resetNorthPitch({duration: 2000});
*/
resetNorthPitch(options, eventData) {
this.easeTo(Object.assign({
bearing: 0,
pitch: 0,
duration: 1e3
}, options), eventData);
return this;
}
/**
* Snaps the map so that north is up (0° bearing), if the current bearing is
* close enough to it (within the `bearingSnap` threshold).
*
* @memberof Map#
* @param {EasingOptions | null} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // snapToNorth with an animation of 2 seconds.
* map.snapToNorth({duration: 2000});
*/
snapToNorth(options, eventData) {
if (Math.abs(this.getBearing()) < this._bearingSnap) {
return this.resetNorth(options, eventData);
}
return this;
}
/**
* Returns the map's current [pitch](https://docs.mapbox.com/help/glossary/camera/) (tilt).
*
* @memberof Map#
* @returns {number} The map's current pitch, measured in degrees away from the plane of the screen.
* @example
* const pitch = map.getPitch();
*/
getPitch() {
return this.transform.pitch;
}
/**
* Sets the map's [pitch](https://docs.mapbox.com/help/glossary/camera/) (tilt). Equivalent to `jumpTo({pitch: pitch})`.
*
* @memberof Map#
* @param {number} pitch The pitch to set, measured in degrees away from the plane of the screen (0-60).
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:pitchstart
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // setPitch with an animation of 2 seconds.
* map.setPitch(80, {duration: 2000});
*/
setPitch(pitch, eventData) {
this.jumpTo({ pitch }, eventData);
return this;
}
/**
* Returns a {@link CameraOptions} object for the highest zoom level
* up to and including `Map#getMaxZoom()` that fits the bounds
* in the viewport at the specified bearing.
*
* @memberof Map#
* @param {LngLatBoundsLike} bounds Calculate the center for these bounds in the viewport and use
* the highest zoom level up to and including `Map#getMaxZoom()` that fits
* in the viewport. LngLatBounds represent a box that is always axis-aligned with bearing 0.
* @param {CameraOptions | null} options Options object.
* @param {number | PaddingOptions} [options.padding] The amount of padding in pixels to add to the given bounds.
* @param {number} [options.bearing=0] Desired map bearing at end of animation, in degrees.
* @param {number} [options.pitch=0] Desired map pitch at end of animation, in degrees.
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels.
* @param {number} [options.maxZoom] The maximum zoom level to allow when the camera would transition to the specified bounds.
* @returns {CameraOptions | void} If map is able to fit to provided bounds, returns `CameraOptions` with
* `center`, `zoom`, and `bearing`. If map is unable to fit, method will warn and return undefined.
* @example
* const bbox = [[-79, 43], [-73, 45]];
* const newCameraTransform = map.cameraForBounds(bbox, {
* padding: {top: 10, bottom:25, left: 15, right: 5}
* });
*/
cameraForBounds(bounds, options) {
bounds = index$1.LngLatBounds.convert(bounds);
const bearing = options && options.bearing || 0;
const pitch = options && options.pitch || 0;
const lnglat0 = bounds.getNorthWest();
const lnglat1 = bounds.getSouthEast();
return this._cameraForBounds(this.transform, lnglat0, lnglat1, bearing, pitch, options);
}
_extendPadding(padding) {
const defaultPadding = { top: 0, right: 0, bottom: 0, left: 0 };
if (padding == null) return Object.assign({}, defaultPadding, this.transform.padding);
if (typeof padding === "number") {
return { top: padding, bottom: padding, right: padding, left: padding };
}
return Object.assign({}, defaultPadding, padding);
}
_extendCameraOptions(options) {
options = Object.assign({
offset: [0, 0],
maxZoom: this.transform.maxZoom
}, options);
options.padding = this._extendPadding(options.padding);
return options;
}
_minimumAABBFrustumDistance(tr, aabb) {
const aabbW = aabb.max[0] - aabb.min[0];
const aabbH = aabb.max[1] - aabb.min[1];
const aabbAspectRatio = aabbW / aabbH;
const selectXAxis = aabbAspectRatio > tr.aspect;
const minimumDistance = selectXAxis ? aabbW / (2 * Math.tan(tr.fovX * 0.5) * tr.aspect) : aabbH / (2 * Math.tan(tr.fovY * 0.5) * tr.aspect);
return minimumDistance;
}
_cameraForBoundsOnGlobe(transform, p0, p1, bearing, pitch, options) {
const tr = transform.clone();
const eOptions = this._extendCameraOptions(options);
tr.bearing = bearing;
tr.pitch = pitch;
const coord0 = index$1.LngLat.convert(p0);
const coord1 = index$1.LngLat.convert(p1);
const midLat = (coord0.lat + coord1.lat) * 0.5;
const midLng = (coord0.lng + coord1.lng) * 0.5;
const origin = index$1.latLngToECEF(midLat, midLng);
const zAxis = index$1.normalize([], origin);
const xAxis = index$1.normalize([], index$1.cross([], zAxis, [0, 1, 0]));
const yAxis = index$1.cross([], xAxis, zAxis);
const aabbOrientation = [
xAxis[0],
xAxis[1],
xAxis[2],
0,
yAxis[0],
yAxis[1],
yAxis[2],
0,
zAxis[0],
zAxis[1],
zAxis[2],
0,
0,
0,
0,
1
];
const ecefCoords = [
origin,
index$1.latLngToECEF(coord0.lat, coord0.lng),
index$1.latLngToECEF(coord1.lat, coord0.lng),
index$1.latLngToECEF(coord1.lat, coord1.lng),
index$1.latLngToECEF(coord0.lat, coord1.lng),
index$1.latLngToECEF(midLat, coord0.lng),
index$1.latLngToECEF(midLat, coord1.lng),
index$1.latLngToECEF(coord0.lat, midLng),
index$1.latLngToECEF(coord1.lat, midLng)
];
let aabb = index$1.Aabb.fromPoints(ecefCoords.map((p) => [index$1.dot(xAxis, p), index$1.dot(yAxis, p), index$1.dot(zAxis, p)]));
const center = index$1.transformMat4([], aabb.center, aabbOrientation);
if (index$1.squaredLength(center) === 0) {
index$1.set(center, 0, 0, 1);
}
index$1.normalize(center, center);
index$1.scale$1(center, center, index$1.GLOBE_RADIUS);
tr.center = index$1.ecefToLatLng(center);
const worldToCamera = tr.getWorldToCameraMatrix();
const cameraToWorld = index$1.invert(new Float64Array(16), worldToCamera);
aabb = index$1.Aabb.applyTransform(aabb, index$1.multiply([], worldToCamera, aabbOrientation));
const extendedAabb = this._extendAABB(aabb, tr, eOptions, bearing);
if (!extendedAabb) {
index$1.warnOnce("Map cannot fit within canvas with the given bounds, padding, and/or offset.");
return;
}
aabb = extendedAabb;
index$1.transformMat4(center, center, worldToCamera);
const aabbHalfExtentZ = (aabb.max[2] - aabb.min[2]) * 0.5;
const frustumDistance = this._minimumAABBFrustumDistance(tr, aabb);
const offsetZ = index$1.scale$1([], [0, 0, 1], aabbHalfExtentZ);
const aabbClosestPoint = index$1.add$1(offsetZ, center, offsetZ);
const offsetDistance = frustumDistance + (tr.pitch === 0 ? 0 : index$1.distance(center, aabbClosestPoint));
const globeCenter = tr.globeCenterInViewSpace;
const normal = index$1.sub([], center, [globeCenter[0], globeCenter[1], globeCenter[2]]);
index$1.normalize(normal, normal);
index$1.scale$1(normal, normal, offsetDistance);
const cameraPosition = index$1.add$1([], center, normal);
index$1.transformMat4(cameraPosition, cameraPosition, cameraToWorld);
const meterPerECEF = index$1.earthRadius / index$1.GLOBE_RADIUS;
const altitudeECEF = index$1.length(cameraPosition);
const altitudeMeter = altitudeECEF * meterPerECEF - index$1.earthRadius;
const mercatorZ = index$1.mercatorZfromAltitude(Math.max(altitudeMeter, Number.EPSILON), 0);
const zoom = Math.min(tr.zoomFromMercatorZAdjusted(mercatorZ), eOptions.maxZoom);
const halfZoomTransition = (index$1.GLOBE_ZOOM_THRESHOLD_MIN + index$1.GLOBE_ZOOM_THRESHOLD_MAX) * 0.5;
if (zoom > halfZoomTransition) {
tr.setProjection({ name: "mercator" });
tr.zoom = zoom;
return this._cameraForBounds(tr, p0, p1, bearing, pitch, options);
}
return { center: tr.center, zoom, bearing, pitch };
}
/**
* Extends the AABB with padding, offset, and bearing.
*
* @param {Aabb} aabb The AABB.
* @param {Transform} tr The transform.
* @param {FullCameraOptions} options Camera options.
* @param {number} bearing The bearing.
* @returns {Aabb | null} The extended AABB or null if couldn't scale.
* @private
*/
_extendAABB(aabb, tr, options, bearing) {
const padL = options.padding.left || 0;
const padR = options.padding.right || 0;
const padB = options.padding.bottom || 0;
const padT = options.padding.top || 0;
const halfScreenPadX = (padL + padR) * 0.5;
const halfScreenPadY = (padT + padB) * 0.5;
const top = halfScreenPadY;
const left = halfScreenPadX;
const right = halfScreenPadX;
const bottom = halfScreenPadY;
const width = tr.width - (left + right);
const height = tr.height - (top + bottom);
const aabbSize = index$1.sub([], aabb.max, aabb.min);
const scaleX = width / aabbSize[0];
const scaleY = height / aabbSize[1];
const scale = Math.min(scaleX, scaleY);
const zoomRef = Math.min(tr.scaleZoom(tr.scale * scale), options.maxZoom);
if (isNaN(zoomRef)) {
return null;
}
const scaleRatio = tr.scale / tr.zoomScale(zoomRef);
const extendedAABB = new index$1.Aabb(
[aabb.min[0] - left * scaleRatio, aabb.min[1] - bottom * scaleRatio, aabb.min[2]],
[aabb.max[0] + right * scaleRatio, aabb.max[1] + top * scaleRatio, aabb.max[2]]
);
const centerOffset = typeof options.offset.x === "number" && typeof options.offset.y === "number" ? new index$1.Point(options.offset.x, options.offset.y) : index$1.Point.convert(options.offset);
const rotatedOffset = centerOffset.rotate(-index$1.degToRad(bearing));
extendedAABB.center[0] -= rotatedOffset.x * scaleRatio;
extendedAABB.center[1] += rotatedOffset.y * scaleRatio;
return extendedAABB;
}
/** @section Querying features */
/**
* Queries the currently loaded data for elevation at a geographical location. The elevation is returned in `meters` relative to mean sea-level.
* Returns `null` if `terrain` is disabled or if terrain data for the location hasn't been loaded yet.
*
* In order to guarantee that the terrain data is loaded ensure that the geographical location is visible and wait for the `idle` event to occur.
*
* @memberof Map#
* @param {LngLatLike} lnglat The geographical location at which to query.
* @param {ElevationQueryOptions} [options] Options object.
* @param {boolean} [options.exaggerated=true] When `true` returns the terrain elevation with the value of `exaggeration` from the style already applied.
* When `false`, returns the raw value of the underlying data without styling applied.
* @returns {number | null} The elevation in meters.
* @example
* const coordinate = [-122.420679, 37.772537];
* const elevation = map.queryTerrainElevation(coordinate);
* @see [Example: Query terrain elevation](https://docs.mapbox.com/mapbox-gl-js/example/query-terrain-elevation/)
*/
queryTerrainElevation(lnglat, options) {
const elevation = this.transform.elevation;
if (elevation) {
options = Object.assign({}, { exaggerated: true }, options);
return elevation.getAtPoint(index$1.MercatorCoordinate.fromLngLat(lnglat), null, options.exaggerated);
}
return null;
}
/**
* Calculate the center of these two points in the viewport and use
* the highest zoom level up to and including `Map#getMaxZoom()` that fits
* the points in the viewport at the specified bearing.
* @memberof Map#
* @param transform The current transform
* @param {LngLatLike} p0 First point
* @param {LngLatLike} p1 Second point
* @param {number} bearing Desired map bearing at end of animation, in degrees
* @param {number} pitch Desired map pitch at end of animation, in degrees
* @param {CameraOptions | null} options
* @param {number | PaddingOptions} [options.padding] The amount of padding in pixels to add to the given bounds.
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels.
* @param {number} [options.maxZoom] The maximum zoom level to allow when the camera would transition to the specified bounds.
* @returns {CameraOptions | void} If map is able to fit to provided bounds, returns `CameraOptions` with
* `center`, `zoom`, and `bearing`. If map is unable to fit, method will warn and return undefined.
* @private
* @example
* var p0 = [-79, 43];
* var p1 = [-73, 45];
* var bearing = 90;
* var newCameraTransform = map._cameraForBounds(p0, p1, bearing, pitch, {
* padding: {top: 10, bottom:25, left: 15, right: 5}
* });
*/
_cameraForBounds(transform, p0, p1, bearing, pitch, options) {
if (transform.projection.name === "globe") {
return this._cameraForBoundsOnGlobe(transform, p0, p1, bearing, pitch, options);
}
const tr = transform.clone();
const eOptions = this._extendCameraOptions(options);
tr.bearing = bearing;
tr.pitch = pitch;
const coord0 = index$1.LngLat.convert(p0);
const coord1 = index$1.LngLat.convert(p1);
const coord2 = new index$1.LngLat(coord0.lng, coord1.lat);
const coord3 = new index$1.LngLat(coord1.lng, coord0.lat);
const p0world = tr.project(coord0);
const p1world = tr.project(coord1);
const z0 = this.queryTerrainElevation(coord0);
const z1 = this.queryTerrainElevation(coord1);
const z2 = this.queryTerrainElevation(coord2);
const z3 = this.queryTerrainElevation(coord3);
const worldCoords = [
[p0world.x, p0world.y, Math.min(z0 || 0, z1 || 0, z2 || 0, z3 || 0)],
[p1world.x, p1world.y, Math.max(z0 || 0, z1 || 0, z2 || 0, z3 || 0)]
];
let aabb = index$1.Aabb.fromPoints(worldCoords);
const worldToCamera = tr.getWorldToCameraMatrix();
const cameraToWorld = index$1.invert(new Float64Array(16), worldToCamera);
aabb = index$1.Aabb.applyTransform(aabb, worldToCamera);
const extendedAabb = this._extendAABB(aabb, tr, eOptions, bearing);
if (!extendedAabb) {
index$1.warnOnce("Map cannot fit within canvas with the given bounds, padding, and/or offset.");
return;
}
aabb = extendedAabb;
const size = index$1.sub([], aabb.max, aabb.min);
const aabbHalfExtentZ = size[2] * 0.5;
const frustumDistance = this._minimumAABBFrustumDistance(tr, aabb);
const normalZ = [0, 0, 1, 0];
index$1.transformMat4$1(normalZ, normalZ, worldToCamera);
index$1.normalize$2(normalZ, normalZ);
const offset = index$1.scale$1([], normalZ, frustumDistance + aabbHalfExtentZ);
const cameraPosition = index$1.add$1([], aabb.center, offset);
index$1.transformMat4(aabb.center, aabb.center, cameraToWorld);
index$1.transformMat4(cameraPosition, cameraPosition, cameraToWorld);
const center = tr.unproject(new index$1.Point(aabb.center[0], aabb.center[1]));
const zoomAdjustment = index$1.getZoomAdjustment(tr.projection, center);
const scaleAdjustment = Math.pow(2, zoomAdjustment);
const mercatorZ = cameraPosition[2] * tr.pixelsPerMeter * scaleAdjustment / tr.worldSize;
const zoom = Math.min(tr._zoomFromMercatorZ(mercatorZ), eOptions.maxZoom);
const halfZoomTransition = (index$1.GLOBE_ZOOM_THRESHOLD_MIN + index$1.GLOBE_ZOOM_THRESHOLD_MAX) * 0.5;
if (tr.mercatorFromTransition && zoom < halfZoomTransition) {
tr.setProjection({ name: "globe" });
tr.zoom = zoom;
return this._cameraForBounds(tr, p0, p1, bearing, pitch, options);
}
return { center, zoom, bearing, pitch };
}
/**
* Pans and zooms the map to contain its visible area within the specified geographical bounds.
* If a padding is set on the map, the bounds are fit to the inset.
*
* @memberof Map#
* @param {LngLatBoundsLike} bounds Center these bounds in the viewport and use the highest
* zoom level up to and including `Map#getMaxZoom()` that fits them in the viewport.
* @param {Object} [options] Options supports all properties from {@link AnimationOptions} and {@link CameraOptions} in addition to the fields below.
* @param {number | PaddingOptions} [options.padding] The amount of padding in pixels to add to the given bounds.
* @param {number} [options.pitch=0] Desired map pitch at end of animation, in degrees.
* @param {number} [options.bearing=0] Desired map bearing at end of animation, in degrees.
* @param {boolean} [options.linear=false] If `true`, the map transitions using
* {@link Map#easeTo}. If `false`, the map transitions using {@link Map#flyTo}. See
* those functions and {@link AnimationOptions} for information about options available.
* @param {Function} [options.easing] An easing function for the animated transition. See {@link AnimationOptions}.
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels.
* @param {number} [options.maxZoom] The maximum zoom level to allow when the map view transitions to the specified bounds.
* @param {Object} [eventData] Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* const bbox = [[-79, 43], [-73, 45]];
* map.fitBounds(bbox, {
* padding: {top: 10, bottom:25, left: 15, right: 5}
* });
* @see [Example: Fit a map to a bounding box](https://www.mapbox.com/mapbox-gl-js/example/fitbounds/)
*/
fitBounds(bounds, options, eventData) {
const cameraPlacement = this.cameraForBounds(bounds, options);
return this._fitInternal(cameraPlacement, options, eventData);
}
/**
* Pans, rotates and zooms the map to to fit the box made by points p0 and p1
* once the map is rotated to the specified bearing. To zoom without rotating,
* pass in the current map bearing.
*
* @memberof Map#
* @param {PointLike} p0 First point on screen, in pixel coordinates.
* @param {PointLike} p1 Second point on screen, in pixel coordinates.
* @param {number} bearing Desired map bearing at end of animation, in degrees.
* @param {EasingOptions | null} options Options object.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {number | PaddingOptions} [options.padding] The amount of padding in pixels to add to the given bounds.
* @param {boolean} [options.linear=false] If `true`, the map transitions using
* {@link Map#easeTo}. If `false`, the map transitions using {@link Map#flyTo}. See
* those functions and {@link AnimationOptions} for information about options available.
* @param {number} [options.pitch=0] Desired map pitch at end of animation, in degrees.
* @param {Function} [options.easing] An easing function for the animated transition. See {@link AnimationOptions}.
* @param {PointLike} [options.offset=[0, 0]] The center of the given bounds relative to the map's center, measured in pixels.
* @param {number} [options.maxZoom] The maximum zoom level to allow when the map view transitions to the specified bounds.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:moveend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* const p0 = [220, 400];
* const p1 = [500, 900];
* map.fitScreenCoordinates(p0, p1, map.getBearing(), {
* padding: {top: 10, bottom:25, left: 15, right: 5}
* });
* @see Used by {@link BoxZoomHandler}
*/
fitScreenCoordinates(p0, p1, bearing, options, eventData) {
const screen0 = index$1.Point.convert(p0);
const screen1 = index$1.Point.convert(p1);
const min = new index$1.Point(Math.min(screen0.x, screen1.x), Math.min(screen0.y, screen1.y));
const max = new index$1.Point(Math.max(screen0.x, screen1.x), Math.max(screen0.y, screen1.y));
if (this.transform.projection.name === "mercator" && this.transform.anyCornerOffEdge(screen0, screen1)) {
return this;
}
const lnglat0 = this.transform.pointLocation3D(min);
const lnglat1 = this.transform.pointLocation3D(max);
const lnglat2 = this.transform.pointLocation3D(new index$1.Point(min.x, max.y));
const lnglat3 = this.transform.pointLocation3D(new index$1.Point(max.x, min.y));
const p0coord = [
Math.min(lnglat0.lng, lnglat1.lng, lnglat2.lng, lnglat3.lng),
Math.min(lnglat0.lat, lnglat1.lat, lnglat2.lat, lnglat3.lat)
];
const p1coord = [
Math.max(lnglat0.lng, lnglat1.lng, lnglat2.lng, lnglat3.lng),
Math.max(lnglat0.lat, lnglat1.lat, lnglat2.lat, lnglat3.lat)
];
const pitch = options && options.pitch ? options.pitch : this.getPitch();
const cameraPlacement = this._cameraForBounds(this.transform, p0coord, p1coord, bearing, pitch, options);
return this._fitInternal(cameraPlacement, options, eventData);
}
_fitInternal(calculatedOptions, options, eventData) {
if (!calculatedOptions) return this;
options = Object.assign(calculatedOptions, options);
return options.linear ? this.easeTo(options, eventData) : this.flyTo(options, eventData);
}
/**
* Changes any combination of center, zoom, bearing, and pitch, without
* an animated transition. The map will retain its current values for any
* details not specified in `options`.
*
* @memberof Map#
* @param {CameraOptions} options Options object.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:pitchstart
* @fires Map.event:rotate
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:pitch
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @fires Map.event:pitchend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // jump to coordinates at current zoom
* map.jumpTo({center: [0, 0]});
* // jump with zoom, pitch, and bearing options
* map.jumpTo({
* center: [0, 0],
* zoom: 8,
* pitch: 45,
* bearing: 90
* });
* @see [Example: Jump to a series of locations](https://docs.mapbox.com/mapbox-gl-js/example/jump-to/)
* @see [Example: Update a feature in realtime](https://docs.mapbox.com/mapbox-gl-js/example/live-update-feature/)
*/
jumpTo(options, eventData) {
this.stop();
const tr = options.preloadOnly ? this.transform.clone() : this.transform;
let zoomChanged = false, bearingChanged = false, pitchChanged = false;
if ("zoom" in options && tr.zoom !== +options.zoom) {
zoomChanged = true;
tr.zoom = +options.zoom;
}
if (options.center !== void 0) {
tr.center = index$1.LngLat.convert(options.center);
}
if ("bearing" in options && tr.bearing !== +options.bearing) {
bearingChanged = true;
tr.bearing = +options.bearing;
}
if ("pitch" in options && tr.pitch !== +options.pitch) {
pitchChanged = true;
tr.pitch = +options.pitch;
}
const padding = typeof options.padding === "number" ? this._extendPadding(options.padding) : options.padding;
if (options.padding != null && !tr.isPaddingEqual(padding)) {
if (options.retainPadding === false) {
const transformForPadding = tr.clone();
transformForPadding.padding = padding;
tr.setLocationAtPoint(tr.center, transformForPadding.centerPoint);
} else {
tr.padding = padding;
}
}
if (options.preloadOnly) {
this._preloadTiles(tr);
return this;
}
this.fire(new index$1.Event("movestart", eventData)).fire(new index$1.Event("move", eventData));
if (zoomChanged) {
this.fire(new index$1.Event("zoomstart", eventData)).fire(new index$1.Event("zoom", eventData)).fire(new index$1.Event("zoomend", eventData));
}
if (bearingChanged) {
this.fire(new index$1.Event("rotatestart", eventData)).fire(new index$1.Event("rotate", eventData)).fire(new index$1.Event("rotateend", eventData));
}
if (pitchChanged) {
this.fire(new index$1.Event("pitchstart", eventData)).fire(new index$1.Event("pitch", eventData)).fire(new index$1.Event("pitchend", eventData));
}
return this.fire(new index$1.Event("moveend", eventData));
}
/**
* Returns position and orientation of the camera entity.
*
* This method is not supported for projections other than mercator.
*
* @memberof Map#
* @returns {FreeCameraOptions} The camera state.
* @example
* const camera = map.getFreeCameraOptions();
*
* const position = [138.72649, 35.33974];
* const altitude = 3000;
*
* camera.position = mapboxgl.MercatorCoordinate.fromLngLat(position, altitude);
* camera.lookAtPoint([138.73036, 35.36197]);
*
* map.setFreeCameraOptions(camera);
*/
getFreeCameraOptions() {
if (!this.transform.projection.supportsFreeCamera) {
index$1.warnOnce(freeCameraNotSupportedWarning);
}
return this.transform.getFreeCameraOptions();
}
/**
* `FreeCameraOptions` provides more direct access to the underlying camera entity.
* For backwards compatibility the state set using this API must be representable with
* `CameraOptions` as well. Parameters are clamped into a valid range or discarded as invalid
* if the conversion to the pitch and bearing presentation is ambiguous. For example orientation
* can be invalid if it leads to the camera being upside down, the quaternion has zero length,
* or the pitch is over the maximum pitch limit.
*
* This method is not supported for projections other than mercator.
*
* @memberof Map#
* @param {FreeCameraOptions} options `FreeCameraOptions` object.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:pitchstart
* @fires Map.event:rotate
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:pitch
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @fires Map.event:pitchend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* const camera = map.getFreeCameraOptions();
*
* const position = [138.72649, 35.33974];
* const altitude = 3000;
*
* camera.position = mapboxgl.MercatorCoordinate.fromLngLat(position, altitude);
* camera.lookAtPoint([138.73036, 35.36197]);
*
* map.setFreeCameraOptions(camera);
*/
setFreeCameraOptions(options, eventData) {
const tr = this.transform;
if (!tr.projection.supportsFreeCamera) {
index$1.warnOnce(freeCameraNotSupportedWarning);
return this;
}
this.stop();
const prevZoom = tr.zoom;
const prevPitch = tr.pitch;
const prevBearing = tr.bearing;
tr.setFreeCameraOptions(options);
const zoomChanged = prevZoom !== tr.zoom;
const pitchChanged = prevPitch !== tr.pitch;
const bearingChanged = prevBearing !== tr.bearing;
this.fire(new index$1.Event("movestart", eventData)).fire(new index$1.Event("move", eventData));
if (zoomChanged) {
this.fire(new index$1.Event("zoomstart", eventData)).fire(new index$1.Event("zoom", eventData)).fire(new index$1.Event("zoomend", eventData));
}
if (bearingChanged) {
this.fire(new index$1.Event("rotatestart", eventData)).fire(new index$1.Event("rotate", eventData)).fire(new index$1.Event("rotateend", eventData));
}
if (pitchChanged) {
this.fire(new index$1.Event("pitchstart", eventData)).fire(new index$1.Event("pitch", eventData)).fire(new index$1.Event("pitchend", eventData));
}
this.fire(new index$1.Event("moveend", eventData));
return this;
}
/**
* Changes any combination of `center`, `zoom`, `bearing`, `pitch`, and `padding` with an animated transition
* between old and new values. The map will retain its current values for any
* details not specified in `options`.
*
* Note: The transition will happen instantly if the user has enabled
* the `reduced motion` accessibility feature enabled in their operating system,
* unless `options` includes `essential: true`.
*
* @memberof Map#
* @param {EasingOptions} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions} and {@link AnimationOptions}.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:pitchstart
* @fires Map.event:rotate
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:pitch
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @fires Map.event:pitchend
* @returns {Map} `this` Returns itself to allow for method chaining.
* @example
* // Ease with default options to null island for 5 seconds.
* map.easeTo({center: [0, 0], zoom: 9, duration: 5000});
* @example
* // Using easeTo options.
* map.easeTo({
* center: [0, 0],
* zoom: 9,
* speed: 0.2,
* curve: 1,
* duration: 5000,
* easing(t) {
* return t;
* }
* });
* @see [Example: Navigate the map with game-like controls](https://www.mapbox.com/mapbox-gl-js/example/game-controls/)
*/
easeTo(options, eventData) {
this._stop(false, options.easeId);
options = Object.assign({
offset: [0, 0],
duration: 500,
easing: index$1.ease
}, options);
if (options.animate === false || this._prefersReducedMotion(options)) options.duration = 0;
const tr = this.transform, startZoom = this.getZoom(), startBearing = this.getBearing(), startPitch = this.getPitch(), startPadding = this.getPadding(), zoom = "zoom" in options ? +options.zoom : startZoom, bearing = "bearing" in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing, pitch = "pitch" in options ? +options.pitch : startPitch, padding = this._extendPadding(options.padding);
const offsetAsPoint = index$1.Point.convert(options.offset);
let pointAtOffset;
let from;
let delta;
if (tr.projection.name === "globe") {
const centerCoord = index$1.MercatorCoordinate.fromLngLat(tr.center);
const rotatedOffset = offsetAsPoint.rotate(-tr.angle);
centerCoord.x += rotatedOffset.x / tr.worldSize;
centerCoord.y += rotatedOffset.y / tr.worldSize;
const locationAtOffset = centerCoord.toLngLat();
const center = index$1.LngLat.convert(options.center || locationAtOffset);
this._normalizeCenter(center);
pointAtOffset = tr.centerPoint.add(rotatedOffset);
from = new index$1.Point(centerCoord.x, centerCoord.y).mult(tr.worldSize);
delta = new index$1.Point(index$1.mercatorXfromLng(center.lng), index$1.mercatorYfromLat(center.lat)).mult(tr.worldSize).sub(from);
} else {
pointAtOffset = tr.centerPoint.add(offsetAsPoint);
const locationAtOffset = tr.pointLocation(pointAtOffset);
const center = index$1.LngLat.convert(options.center || locationAtOffset);
this._normalizeCenter(center);
from = tr.project(locationAtOffset);
delta = tr.project(center).sub(from);
}
const finalScale = tr.zoomScale(zoom - startZoom);
let around;
let aroundPoint;
if (options.around) {
around = index$1.LngLat.convert(options.around);
aroundPoint = tr.locationPoint(around);
}
const zoomChanged = this._zooming || zoom !== startZoom;
const bearingChanged = this._rotating || startBearing !== bearing;
const pitchChanged = this._pitching || pitch !== startPitch;
const paddingChanged = !tr.isPaddingEqual(padding);
const transformForPadding = options.retainPadding === false ? tr.clone() : tr;
const frame = (tr2) => (k) => {
if (zoomChanged) {
tr2.zoom = index$1.number(startZoom, zoom, k);
}
if (bearingChanged) {
tr2.bearing = index$1.number(startBearing, bearing, k);
}
if (pitchChanged) {
tr2.pitch = index$1.number(startPitch, pitch, k);
}
if (paddingChanged) {
transformForPadding.interpolatePadding(startPadding, padding, k);
pointAtOffset = transformForPadding.centerPoint.add(offsetAsPoint);
}
if (around) {
tr2.setLocationAtPoint(around, aroundPoint);
} else {
const scale = tr2.zoomScale(tr2.zoom - startZoom);
const base = zoom > startZoom ? Math.min(2, finalScale) : Math.max(0.5, finalScale);
const speedup = Math.pow(base, 1 - k);
const newCenter = tr2.unproject(from.add(delta.mult(k * speedup)).mult(scale));
tr2.setLocationAtPoint(tr2.renderWorldCopies ? newCenter.wrap() : newCenter, pointAtOffset);
}
if (!options.preloadOnly) {
this._fireMoveEvents(eventData);
}
return tr2;
};
if (options.preloadOnly) {
const predictedTransforms = this._emulate(frame, options.duration, tr);
this._preloadTiles(predictedTransforms);
return this;
}
const currently = {
moving: this._moving,
zooming: this._zooming,
rotating: this._rotating,
pitching: this._pitching
};
this._zooming = zoomChanged;
this._rotating = bearingChanged;
this._pitching = pitchChanged;
this._padding = paddingChanged;
this._easeId = options.easeId;
this._prepareEase(eventData, options.noMoveStart, currently);
this._ease(frame(tr), (interruptingEaseId) => {
if (tr.cameraElevationReference === "sea") tr.recenterOnTerrain();
this._afterEase(eventData, interruptingEaseId);
}, options);
return this;
}
_prepareEase(eventData, noMoveStart, currently = {}) {
this._moving = true;
this.transform.cameraElevationReference = "sea";
if (this.transform._orthographicProjectionAtLowPitch && this.transform.pitch === 0 && this.transform.projection.name !== "globe") {
this.transform.cameraElevationReference = "ground";
}
if (!noMoveStart && !currently.moving) {
this.fire(new index$1.Event("movestart", eventData));
}
if (this._zooming && !currently.zooming) {
this.fire(new index$1.Event("zoomstart", eventData));
}
if (this._rotating && !currently.rotating) {
this.fire(new index$1.Event("rotatestart", eventData));
}
if (this._pitching && !currently.pitching) {
this.fire(new index$1.Event("pitchstart", eventData));
}
}
_fireMoveEvents(eventData) {
this.fire(new index$1.Event("move", eventData));
if (this._zooming) {
this.fire(new index$1.Event("zoom", eventData));
}
if (this._rotating) {
this.fire(new index$1.Event("rotate", eventData));
}
if (this._pitching) {
this.fire(new index$1.Event("pitch", eventData));
}
}
_afterEase(eventData, easeId) {
if (this._easeId && easeId && this._easeId === easeId) {
return;
}
this._easeId = void 0;
this.transform.cameraElevationReference = "ground";
const wasZooming = this._zooming;
const wasRotating = this._rotating;
const wasPitching = this._pitching;
this._moving = false;
this._zooming = false;
this._rotating = false;
this._pitching = false;
this._padding = false;
if (wasZooming) {
this.fire(new index$1.Event("zoomend", eventData));
}
if (wasRotating) {
this.fire(new index$1.Event("rotateend", eventData));
}
if (wasPitching) {
this.fire(new index$1.Event("pitchend", eventData));
}
this.fire(new index$1.Event("moveend", eventData));
}
/**
* Changes any combination of center, zoom, bearing, and pitch, animating the transition along a curve that
* evokes flight. The animation seamlessly incorporates zooming and panning to help
* the user maintain their bearings even after traversing a great distance.
*
* If a user has the `reduced motion` accessibility feature enabled in their
* operating system, the animation will be skipped and this will behave
* equivalently to `jumpTo`, unless 'options' includes `essential: true`.
*
* @memberof Map#
* @param {Object} options Options describing the destination and animation of the transition.
* Accepts {@link CameraOptions}, {@link AnimationOptions},
* and the following additional options.
* @param {number} [options.curve=1.42] The zooming "curve" that will occur along the
* flight path. A high value maximizes zooming for an exaggerated animation, while a low
* value minimizes zooming for an effect closer to {@link Map#easeTo}. 1.42 is the average
* value selected by participants in the user study discussed in
* [van Wijk (2003)](https://www.win.tue.nl/~vanwijk/zoompan.pdf). A value of
* `Math.pow(6, 0.25)` would be equivalent to the root mean squared average velocity. A
* value of 1 would produce a circular motion. If `options.minZoom` is specified, this option will be ignored.
* @param {number} [options.minZoom] The zero-based zoom level at the peak of the flight path. If
* this option is specified, `options.curve` will be ignored.
* @param {number} [options.speed=1.2] The average speed of the animation defined in relation to
* `options.curve`. A speed of 1.2 means that the map appears to move along the flight path
* by 1.2 times `options.curve` screenfuls every second. A _screenful_ is the map's visible span.
* It does not correspond to a fixed physical distance, but varies by zoom level.
* @param {number} [options.screenSpeed] The average speed of the animation measured in screenfuls
* per second, assuming a linear timing curve. If `options.speed` is specified, this option is ignored.
* @param {number} [options.maxDuration] The animation's maximum duration, measured in milliseconds.
* If duration exceeds maximum duration, it resets to 0.
* @param {Object | null} eventData Additional properties to be added to event objects of events triggered by this method.
* @fires Map.event:movestart
* @fires Map.event:zoomstart
* @fires Map.event:pitchstart
* @fires Map.event:move
* @fires Map.event:zoom
* @fires Map.event:rotate
* @fires Map.event:pitch
* @fires Map.event:moveend
* @fires Map.event:zoomend
* @fires Map.event:pitchend
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // fly with default options to null island
* map.flyTo({center: [0, 0], zoom: 9});
* // using flyTo options
* map.flyTo({
* center: [0, 0],
* zoom: 9,
* speed: 0.2,
* curve: 1,
* easing(t) {
* return t;
* }
* });
* @see [Example: Fly to a location](https://www.mapbox.com/mapbox-gl-js/example/flyto/)
* @see [Example: Slowly fly to a location](https://www.mapbox.com/mapbox-gl-js/example/flyto-options/)
* @see [Example: Fly to a location based on scroll position](https://www.mapbox.com/mapbox-gl-js/example/scroll-fly-to/)
*/
flyTo(options, eventData) {
if (this._prefersReducedMotion(options)) {
const coercedOptions = index$1.pick(options, ["center", "zoom", "bearing", "pitch", "around", "padding", "retainPadding"]);
return this.jumpTo(coercedOptions, eventData);
}
this.stop();
options = Object.assign({
offset: [0, 0],
speed: 1.2,
curve: 1.42,
easing: index$1.ease
}, options);
const tr = this.transform, startZoom = this.getZoom(), startBearing = this.getBearing(), startPitch = this.getPitch(), startPadding = this.getPadding();
const zoom = "zoom" in options ? index$1.clamp(+options.zoom, tr.minZoom, tr.maxZoom) : startZoom;
const bearing = "bearing" in options ? this._normalizeBearing(options.bearing, startBearing) : startBearing;
const pitch = "pitch" in options ? +options.pitch : startPitch;
const padding = this._extendPadding(options.padding);
const scale = tr.zoomScale(zoom - startZoom);
const offsetAsPoint = index$1.Point.convert(options.offset);
let pointAtOffset = tr.centerPoint.add(offsetAsPoint);
const locationAtOffset = tr.pointLocation(pointAtOffset);
const center = index$1.LngLat.convert(options.center || locationAtOffset);
this._normalizeCenter(center);
const from = tr.project(locationAtOffset);
const delta = tr.project(center).sub(from);
let rho = options.curve;
const w0 = Math.max(tr.width, tr.height), w1 = w0 / scale, u1 = delta.mag();
if ("minZoom" in options) {
const minZoom = index$1.clamp(Math.min(options.minZoom, startZoom, zoom), tr.minZoom, tr.maxZoom);
const wMax = w0 / tr.zoomScale(minZoom - startZoom);
rho = Math.sqrt(wMax / u1 * 2);
}
const rho2 = rho * rho;
function r(i) {
const b = (w1 * w1 - w0 * w0 + (i ? -1 : 1) * rho2 * rho2 * u1 * u1) / (2 * (i ? w1 : w0) * rho2 * u1);
return Math.log(Math.sqrt(b * b + 1) - b);
}
function sinh(n) {
return (Math.exp(n) - Math.exp(-n)) / 2;
}
function cosh(n) {
return (Math.exp(n) + Math.exp(-n)) / 2;
}
function tanh(n) {
return sinh(n) / cosh(n);
}
const r0 = r(0);
let w = function(s) {
return cosh(r0) / cosh(r0 + rho * s);
};
let u = function(s) {
return w0 * ((cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2) / u1;
};
let S = (r(1) - r0) / rho;
if (Math.abs(u1) < 1e-6 || !isFinite(S)) {
if (Math.abs(w0 - w1) < 1e-6) return this.easeTo(options, eventData);
const k = w1 < w0 ? -1 : 1;
S = Math.abs(Math.log(w1 / w0)) / rho;
u = function() {
return 0;
};
w = function(s) {
return Math.exp(k * rho * s);
};
}
if ("duration" in options) {
options.duration = +options.duration;
} else {
const V = "screenSpeed" in options ? +options.screenSpeed / rho : +options.speed;
options.duration = 1e3 * S / V;
}
if (options.maxDuration && options.duration > options.maxDuration) {
options.duration = 0;
}
const zoomChanged = true;
const bearingChanged = startBearing !== bearing;
const pitchChanged = pitch !== startPitch;
const paddingChanged = !tr.isPaddingEqual(padding);
const transformForPadding = options.retainPadding === false ? tr.clone() : tr;
const frame = (tr2) => (k) => {
const s = k * S;
const scale2 = 1 / w(s);
tr2.zoom = k === 1 ? zoom : startZoom + tr2.scaleZoom(scale2);
if (bearingChanged) {
tr2.bearing = index$1.number(startBearing, bearing, k);
}
if (pitchChanged) {
tr2.pitch = index$1.number(startPitch, pitch, k);
}
if (paddingChanged) {
transformForPadding.interpolatePadding(startPadding, padding, k);
pointAtOffset = transformForPadding.centerPoint.add(offsetAsPoint);
}
const newCenter = k === 1 ? center : tr2.unproject(from.add(delta.mult(u(s))).mult(scale2));
tr2.setLocationAtPoint(tr2.renderWorldCopies ? newCenter.wrap() : newCenter, pointAtOffset);
tr2._updateCameraOnTerrain();
if (!options.preloadOnly) {
this._fireMoveEvents(eventData);
}
return tr2;
};
if (options.preloadOnly) {
const predictedTransforms = this._emulate(frame, options.duration, tr);
this._preloadTiles(predictedTransforms);
return this;
}
this._zooming = zoomChanged;
this._rotating = bearingChanged;
this._pitching = pitchChanged;
this._padding = paddingChanged;
this._prepareEase(eventData, false);
this._ease(frame(tr), () => this._afterEase(eventData), options);
return this;
}
isEasing() {
return !!this._easeFrameId;
}
/**
* Stops any animated transition underway.
*
* @memberof Map#
* @returns {Map} Returns itself to allow for method chaining.
* @example
* map.stop();
*/
stop() {
return this._stop();
}
// @ts-expect-error - No-op in the Camera class, implemented by the Map class
_requestRenderFrame(_callback) {
}
// No-op in the Camera class, implemented by the Map class
_cancelRenderFrame(_) {
}
_stop(allowGestures, easeId) {
if (this._easeFrameId) {
this._cancelRenderFrame(this._easeFrameId);
this._easeFrameId = void 0;
this._onEaseFrame = void 0;
}
if (this._onEaseEnd) {
const onEaseEnd = this._onEaseEnd;
this._onEaseEnd = void 0;
onEaseEnd.call(this, easeId);
}
if (!allowGestures) {
const handlers = this.handlers;
if (handlers) handlers.stop(false);
}
return this;
}
_ease(frame, finish, options) {
if (options.animate === false || options.duration === 0) {
frame(1);
finish();
} else {
this._easeStart = index$1.exported$1.now();
this._easeOptions = options;
this._onEaseFrame = frame;
this._onEaseEnd = finish;
this._easeFrameId = this._requestRenderFrame(this._renderFrameCallback);
}
}
// Callback for map._requestRenderFrame
_renderFrameCallback() {
const t = Math.min((index$1.exported$1.now() - this._easeStart) / this._easeOptions.duration, 1);
const frame = this._onEaseFrame;
if (frame) frame(this._easeOptions.easing(t));
if (t < 1) {
this._easeFrameId = this._requestRenderFrame(this._renderFrameCallback);
} else {
this.stop();
}
}
// convert bearing so that it's numerically close to the current one so that it interpolates properly
_normalizeBearing(bearing, currentBearing) {
bearing = index$1.wrap(bearing, -180, 180);
const diff = Math.abs(bearing - currentBearing);
if (Math.abs(bearing - 360 - currentBearing) < diff) bearing -= 360;
if (Math.abs(bearing + 360 - currentBearing) < diff) bearing += 360;
return bearing;
}
// If a path crossing the antimeridian would be shorter, extend the final coordinate so that
// interpolating between the two endpoints will cross it.
_normalizeCenter(center) {
const tr = this.transform;
if (tr.maxBounds) return;
const isGlobe = tr.projection.name === "globe";
if (!isGlobe && !tr.renderWorldCopies) return;
const delta = center.lng - tr.center.lng;
center.lng += delta > 180 ? -360 : delta < -180 ? 360 : 0;
}
_prefersReducedMotion(options) {
const essential = options && options.essential;
const prefersReducedMotion = this._respectPrefersReducedMotion && index$1.exported$1.prefersReducedMotion;
return prefersReducedMotion && !essential;
}
// emulates frame function for some transform
_emulate(frame, duration, initialTransform) {
const frameRate = 15;
const numFrames = Math.ceil(duration * frameRate / 1e3);
const transforms = [];
const emulateFrame = frame(initialTransform.clone());
for (let i = 0; i <= numFrames; i++) {
const transform = emulateFrame(i / numFrames);
transforms.push(transform.clone());
}
return transforms;
}
// No-op in the Camera class, implemented by the Map class
_preloadTiles(_transform, _callback) {
}
}
function addAssertions(camera) {
Debug.run(() => {
const inProgress = {};
["drag", "zoom", "rotate", "pitch", "move"].forEach((name) => {
inProgress[name] = false;
camera.on(`${name}start`, () => {
index$1.assert(!inProgress[name], `"${name}start" fired twice without a "${name}end"`);
inProgress[name] = true;
index$1.assert(inProgress.move);
});
camera.on(name, () => {
index$1.assert(inProgress[name]);
index$1.assert(inProgress.move);
});
camera.on(`${name}end`, () => {
index$1.assert(inProgress.move);
index$1.assert(inProgress[name]);
inProgress[name] = false;
});
});
canary = "canary debug run";
});
}
let canary;
function sanitizeLinks(html) {
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const elements = Array.from(doc.body.querySelectorAll("*"));
elements.forEach((el) => {
const text = el.textContent || "";
if (el.tagName !== "A") {
el.replaceWith(doc.createTextNode(text));
return;
}
const href = el.getAttribute("href");
if (!href || !/^(https?:|mailto:)/i.test(href)) {
el.replaceWith(doc.createTextNode(text));
return;
}
const a = doc.createElement("a");
a.href = href;
a.textContent = text;
a.rel = "noopener nofollow";
const className = el.getAttribute("class");
if (className) a.className = className;
el.replaceWith(a);
});
return doc.body.innerHTML;
}
class AttributionControl {
constructor(options = {}) {
this.options = options;
index$1.bindAll([
"_toggleAttribution",
"_updateEditLink",
"_updateData",
"_updateCompact"
], this);
}
getDefaultPosition() {
return "bottom-right";
}
onAdd(map) {
const compact = this.options && this.options.compact;
const title = map._getUIString("AttributionControl.ToggleAttribution");
this._map = map;
this._container = create$1("div", "mapboxgl-ctrl mapboxgl-ctrl-attrib");
this._compactButton = create$1("button", "mapboxgl-ctrl-attrib-button", this._container);
this._compactButton.type = "button";
this._compactButton.addEventListener("click", this._toggleAttribution);
this._compactButton.setAttribute("aria-label", title);
const buttonIcon = create$1("span", `mapboxgl-ctrl-icon`, this._compactButton);
buttonIcon.setAttribute("aria-hidden", "true");
buttonIcon.setAttribute("title", title);
this._innerContainer = create$1("div", "mapboxgl-ctrl-attrib-inner", this._container);
if (compact) {
this._container.classList.add("mapboxgl-compact");
}
this._updateAttributions();
this._updateEditLink();
this._map.on("styledata", this._updateData);
this._map.on("sourcedata", this._updateData);
this._map.on("moveend", this._updateEditLink);
if (compact === void 0) {
this._map.on("resize", this._updateCompact);
this._updateCompact();
}
return this._container;
}
onRemove() {
this._container.remove();
this._map.off("styledata", this._updateData);
this._map.off("sourcedata", this._updateData);
this._map.off("moveend", this._updateEditLink);
this._map.off("resize", this._updateCompact);
this._map = void 0;
this._attribHTML = void 0;
}
_toggleAttribution() {
if (this._container.classList.contains("mapboxgl-compact-show")) {
this._container.classList.remove("mapboxgl-compact-show");
this._compactButton.setAttribute("aria-expanded", "false");
} else {
this._container.classList.add("mapboxgl-compact-show");
this._compactButton.setAttribute("aria-expanded", "true");
}
}
_updateEditLink() {
let editLink = this._editLink;
if (!editLink) {
editLink = this._editLink = this._container.querySelector(".mapbox-improve-map");
}
const params = [
{ key: "owner", value: this.styleOwner },
{ key: "id", value: this.styleId },
{ key: "access_token", value: this._map._requestManager._customAccessToken || index$1.config.ACCESS_TOKEN }
];
if (editLink) {
const paramString = params.reduce((acc, next, i) => {
if (next.value) {
acc += `${next.key}=${next.value}${i < params.length - 1 ? "&" : ""}`;
}
return acc;
}, `?`);
editLink.href = `${index$1.config.FEEDBACK_URL}/${paramString}#${getHashString(this._map, true)}`;
editLink.rel = "noopener nofollow";
}
}
_updateData(e) {
if (e && (e.dataType === "source" && (e.sourceDataType === "metadata" || e.sourceDataType === "visibility") || e.dataType === "style")) {
this._updateAttributions();
this._updateEditLink();
}
}
_updateAttributions() {
if (!this._map.style) return;
let attributions = [];
if (this._map.style.stylesheet) {
const stylesheet = this._map.style.stylesheet;
this.styleOwner = stylesheet.owner;
this.styleId = stylesheet.id;
}
const sourceCaches = this._map.style._mergedSourceCaches;
for (const id in sourceCaches) {
const sourceCache = sourceCaches[id];
if (sourceCache.used) {
const source = sourceCache.getSource();
if (source.attribution && attributions.indexOf(source.attribution) < 0) {
attributions.push(source.attribution);
}
}
}
attributions.sort((a, b) => a.length - b.length);
attributions = attributions.filter((attrib, i) => {
for (let j = i + 1; j < attributions.length; j++) {
if (attributions[j].indexOf(attrib) >= 0) {
return false;
}
}
return true;
});
if (this.options.customAttribution) {
if (Array.isArray(this.options.customAttribution)) {
attributions = [...this.options.customAttribution, ...attributions];
} else {
attributions.unshift(this.options.customAttribution);
}
}
const attribHTML = attributions.map((attr) => sanitizeLinks(attr)).join(" | ");
if (attribHTML === this._attribHTML) return;
this._attribHTML = attribHTML;
if (attributions.length) {
this._innerContainer.innerHTML = attribHTML;
this._container.classList.remove("mapboxgl-attrib-empty");
} else {
this._container.classList.add("mapboxgl-attrib-empty");
}
this._editLink = null;
}
_updateCompact() {
if (this._map.getCanvasContainer().offsetWidth <= 640) {
this._container.classList.add("mapboxgl-compact");
} else {
this._container.classList.remove("mapboxgl-compact", "mapboxgl-compact-show");
}
}
}
class LogoControl {
constructor() {
index$1.bindAll(["_updateLogo", "_updateCompact"], this);
}
onAdd(map) {
this._map = map;
this._container = create$1("div", "mapboxgl-ctrl");
const anchor = create$1("a", "mapboxgl-ctrl-logo");
anchor.target = "_blank";
anchor.rel = "noopener nofollow";
anchor.href = "https://www.mapbox.com/";
anchor.setAttribute("aria-label", this._map._getUIString("LogoControl.Title"));
anchor.setAttribute("rel", "noopener nofollow");
this._container.appendChild(anchor);
this._container.style.display = "none";
this._map.on("sourcedata", this._updateLogo);
this._updateLogo();
this._map.on("resize", this._updateCompact);
this._updateCompact();
return this._container;
}
onRemove() {
this._container.remove();
this._map.off("sourcedata", this._updateLogo);
this._map.off("resize", this._updateCompact);
}
getDefaultPosition() {
return "bottom-left";
}
_updateLogo(e) {
if (!e || e.sourceDataType === "metadata") {
this._container.style.display = this._logoRequired() ? "block" : "none";
}
}
_logoRequired() {
if (!this._map.style) return true;
const sourceCaches = this._map.style._sourceCaches;
if (Object.entries(sourceCaches).length === 0) return true;
for (const id in sourceCaches) {
const source = sourceCaches[id].getSource();
if (source.hasOwnProperty("mapbox_logo") && !source.mapbox_logo) {
return false;
}
}
return true;
}
_updateCompact() {
const containerChildren = this._container.children;
if (containerChildren.length) {
const anchor = containerChildren[0];
if (this._map.getCanvasContainer().offsetWidth < 250) {
anchor.classList.add("mapboxgl-compact");
} else {
anchor.classList.remove("mapboxgl-compact");
}
}
}
}
class IndoorControl {
constructor() {
index$1.bindAll(["_onIndoorUpdate"], this);
}
onAdd(map) {
this._map = map;
this._container = create$1("div", "mapboxgl-ctrl mapboxgl-ctrl-group");
this._map.indoor.on("selector-update", (controlModel) => this._onIndoorUpdate(controlModel));
return this._container;
}
_createButton(className, fn) {
const a = create$1("button", className, this._container);
a.type = "button";
a.addEventListener("click", fn);
return a;
}
_createSeparator() {
return create$1("div", "mapboxgl-ctrl-separator", this._container);
}
_setButtonTitle(button, title) {
if (!this._map) return;
button.setAttribute("aria-label", title);
button.textContent = title;
}
onRemove() {
if (this._container) {
this._container.remove();
}
if (this._map && this._map.indoor) {
this._map.indoor.off("selector-update", this._onIndoorUpdate);
this._map = null;
}
}
getDefaultPosition() {
return "right";
}
_onIndoorUpdate(model) {
if (!model || !model.floors) {
this._model = model;
this._container.style.display = "none";
return;
}
const oldModel = this._model;
this._model = model;
this._container.style.display = "inline-block";
this._container.style.borderRadius = "8px";
if (oldModel) {
Array.from(this._container.children).forEach((child) => child.remove());
}
if (model.floors.length > 0) {
this.addBuildingsToggleButton();
this.addCurrentFloors(model.floors, model.activeFloorsVisible);
this._updateBuildingsButtonState();
}
}
addBuildingsToggleButton() {
const buildingsButton = this._createButton("mapboxgl-ctrl-buildings-toggle", () => {
const map = this._map;
if (this._model && map) {
map._setIndoorActiveFloorsVisibility(!this._model.activeFloorsVisible);
}
});
create$1("span", `mapboxgl-ctrl-icon`, buildingsButton).setAttribute("aria-hidden", "true");
buildingsButton.classList.add("mapboxgl-ctrl-level-button", "mapboxgl-ctrl-buildings-toggle");
if (this._model && !this._model.activeFloorsVisible) {
buildingsButton.classList.add("mapboxgl-ctrl-level-button-selected");
}
this._container.append(buildingsButton);
this._createSeparator();
}
_updateBuildingsButtonState() {
const buildingsButton = this._container.querySelector(".mapboxgl-ctrl-buildings-toggle");
if (buildingsButton && this._model) {
if (!this._model.activeFloorsVisible) {
buildingsButton.classList.add("mapboxgl-ctrl-level-button-selected");
} else {
buildingsButton.classList.remove("mapboxgl-ctrl-level-button-selected");
}
}
}
addCurrentFloors(floors, showSelectedFloor) {
for (let i = 0; i < floors.length; i++) {
const floor = floors[i];
const levelButton = this._createButton("mapboxgl-ctrl-level-button", () => {
this._map._selectIndoorFloor(floor.id);
});
this._setButtonTitle(levelButton, floor.zIndex.toString());
if (this._model && floor.id === this._model.selectedFloorId && showSelectedFloor) {
levelButton.classList.add("mapboxgl-ctrl-level-button-selected");
}
this._container.append(levelButton);
if (i < floors.length - 1) {
this._createSeparator();
}
}
}
}
class TaskQueue {
constructor() {
this._queue = [];
this._id = 0;
this._cleared = false;
this._currentlyRunning = false;
}
add(callback) {
const id = ++this._id;
const queue = this._queue;
queue.push({ callback, id, cancelled: false });
return id;
}
remove(id) {
const running = this._currentlyRunning;
const queue = running ? this._queue.concat(running) : this._queue;
for (const task of queue) {
if (task.id === id) {
task.cancelled = true;
return;
}
}
}
run(timeStamp = 0) {
index$1.assert(!this._currentlyRunning);
const queue = this._currentlyRunning = this._queue;
this._queue = [];
for (const task of queue) {
if (task.cancelled) continue;
task.callback(timeStamp);
if (this._cleared) break;
}
this._cleared = false;
this._currentlyRunning = false;
}
clear() {
if (this._currentlyRunning) {
this._cleared = true;
}
this._queue = [];
}
}
class EasedVariable {
constructor(initialValue) {
this.jumpTo(initialValue);
}
/**
* Evaluate the current value, given a timestamp.
*
* @param timeStamp {number} Time at which to evaluate.
*
* @returns {number} Evaluated value.
*/
getValue(timeStamp) {
if (timeStamp <= this._startTime) return this._start;
if (timeStamp >= this._endTime) return this._end;
const t = index$1.easeCubicInOut((timeStamp - this._startTime) / (this._endTime - this._startTime));
return this._start * (1 - t) + this._end * t;
}
/**
* Check if an ease is in progress.
*
* @param timeStamp {number} Current time stamp.
*
* @returns {boolean} Returns `true` if ease is in progress.
*/
isEasing(timeStamp) {
return timeStamp >= this._startTime && timeStamp <= this._endTime;
}
/**
* Set the value without easing and cancel any in progress ease.
*
* @param value {number} New value.
*/
jumpTo(value) {
this._startTime = -Infinity;
this._endTime = -Infinity;
this._start = value;
this._end = value;
}
/**
* Cancel any in-progress ease and begin a new ease.
*
* @param value {number} New value to which to ease.
* @param timeStamp {number} Current time stamp.
* @param duration {number} Ease duration, in same units as timeStamp.
*/
easeTo(value, timeStamp, duration) {
this._start = this.getValue(timeStamp);
this._end = value;
this._startTime = timeStamp;
this._endTime = timeStamp + duration;
}
}
const defaultLocale = {
"AttributionControl.ToggleAttribution": "Toggle attribution",
"FullscreenControl.Enter": "Enter fullscreen",
"FullscreenControl.Exit": "Exit fullscreen",
"GeolocateControl.FindMyLocation": "Find my location",
"GeolocateControl.LocationNotAvailable": "Location not available",
"LogoControl.Title": "Mapbox homepage",
"Map.Title": "Map",
"NavigationControl.ResetBearing": "Reset bearing to north",
"NavigationControl.ZoomIn": "Zoom in",
"NavigationControl.ZoomOut": "Zoom out",
"ScrollZoomBlocker.CtrlMessage": "Use ctrl + scroll to zoom the map",
"ScrollZoomBlocker.CmdMessage": "Use \u2318 + scroll to zoom the map",
"TouchPanBlocker.Message": "Use two fingers to move the map"
};
class InteractionEvent extends index$1.Event {
/**
* @private
*/
constructor(e, id, interaction, feature) {
const { point, lngLat, originalEvent, target } = e;
super(e.type, { point, lngLat, originalEvent, target });
this.preventDefault = () => {
e.preventDefault();
};
this.id = id;
this.interaction = interaction;
this.feature = feature;
}
}
class InteractionSet {
constructor(map) {
this.map = map;
this.interactionsByType = /* @__PURE__ */ new Map();
this.delegatedInteractions = /* @__PURE__ */ new Map();
this.typeById = /* @__PURE__ */ new Map();
this.filters = /* @__PURE__ */ new Map();
this.handleType = this.handleType.bind(this);
this.handleMove = this.handleMove.bind(this);
this.handleOut = this.handleOut.bind(this);
this.hoveredFeatures = /* @__PURE__ */ new Map();
this.prevHoveredFeatures = /* @__PURE__ */ new Map();
}
add(id, interaction) {
if (this.typeById.has(id)) {
throw new Error(`Interaction id "${id}" already exists.`);
}
const filter = interaction.filter;
let type = interaction.type;
if (filter) {
this.filters.set(id, index$1.createFilter(filter));
}
if (type === "mouseover") type = "mouseenter";
if (type === "mouseout") type = "mouseleave";
const interactions = this.interactionsByType.get(type) || /* @__PURE__ */ new Map();
if (type === "mouseenter" || type === "mouseleave") {
if (this.delegatedInteractions.size === 0) {
this.map.on("mousemove", this.handleMove);
this.map.on("mouseout", this.handleOut);
}
this.delegatedInteractions.set(id, interaction);
} else if (interactions.size === 0) {
this.map.on(type, this.handleType);
}
if (interactions.size === 0) {
this.interactionsByType.set(type, interactions);
}
interactions.set(id, interaction);
this.typeById.set(id, type);
}
get(id) {
const type = this.typeById.get(id);
if (!type) return;
const interactions = this.interactionsByType.get(type);
if (!interactions) return;
return interactions.get(id);
}
remove(id) {
const type = this.typeById.get(id);
if (!type) return;
this.typeById.delete(id);
this.filters.delete(id);
const interactions = this.interactionsByType.get(type);
if (!interactions) return;
interactions.delete(id);
if (type === "mouseenter" || type === "mouseleave") {
this.delegatedInteractions.delete(id);
if (this.delegatedInteractions.size === 0) {
this.map.off("mousemove", this.handleMove);
this.map.off("mouseout", this.handleOut);
}
} else if (interactions.size === 0) {
this.map.off(type, this.handleType);
}
}
queryTargets(point, interactions) {
const targets = [];
for (const [targetId, interaction] of interactions) {
if (interaction.target) {
targets.push({ targetId, target: interaction.target, filter: this.filters.get(targetId) });
}
}
return this.map.style.queryRenderedTargets(point, targets, this.map.transform);
}
handleMove(event) {
this.prevHoveredFeatures = this.hoveredFeatures;
this.hoveredFeatures = /* @__PURE__ */ new Map();
const features = this.queryTargets(event.point, Array.from(this.delegatedInteractions).reverse());
if (features.length) {
event.type = "mouseenter";
this.handleType(event, features);
}
const featuresLeaving = /* @__PURE__ */ new Map();
for (const [id, { feature }] of this.prevHoveredFeatures) {
if (!this.hoveredFeatures.has(id)) {
featuresLeaving.set(feature.id, feature);
}
}
if (featuresLeaving.size) {
event.type = "mouseleave";
this.handleType(event, Array.from(featuresLeaving.values()));
}
}
handleOut(event) {
const featuresLeaving = Array.from(this.hoveredFeatures.values()).map(({ feature }) => feature);
if (featuresLeaving.length) {
event.type = "mouseleave";
this.handleType(event, featuresLeaving);
}
this.hoveredFeatures.clear();
}
handleType(event, features) {
const isMouseEnter = event.type === "mouseenter";
if (isMouseEnter && !this.interactionsByType.has(event.type)) {
index$1.warnOnce(`mouseenter interaction required for mouseleave to work.`);
return;
}
const interactions = Array.from(this.interactionsByType.get(event.type)).reverse();
const delegated = !!features;
features = features || this.queryTargets(event.point, interactions);
let eventHandled = false;
const uniqueFeatureSet = /* @__PURE__ */ new Set();
for (const feature of features) {
for (const [id, interaction] of interactions) {
if (!interaction.target) continue;
const variants = feature.variants ? feature.variants[id] : null;
if (!variants) continue;
for (const variant of variants) {
if (shouldSkipFeatureVariant(variant, feature, uniqueFeatureSet, id)) {
continue;
}
const targetFeature = new index$1.TargetFeature(feature, variant);
const targetFeatureId = getFeatureTargetKey(variant, feature, id);
if (delegated && targetFeature.id !== void 0) targetFeature.state = this.map.getFeatureState(targetFeature);
const hovered = isMouseEnter ? this.prevHoveredFeatures.get(targetFeatureId) : null;
const interactionEvent = new InteractionEvent(event, id, interaction, targetFeature);
const stop = hovered ? hovered.stop : interaction.handler(interactionEvent);
if (isMouseEnter) {
this.hoveredFeatures.set(targetFeatureId, { feature, stop });
}
if (stop !== false) {
eventHandled = true;
break;
}
}
if (eventHandled) break;
}
if (eventHandled) break;
}
if (eventHandled) return;
for (const [id, interaction] of interactions) {
const { handler, target } = interaction;
if (target) continue;
const stop = handler(new InteractionEvent(event, id, interaction, null));
if (stop !== false) {
break;
}
}
}
}
const AVERAGE_ELEVATION_SAMPLING_INTERVAL = 500;
const AVERAGE_ELEVATION_EASE_TIME = 300;
const AVERAGE_ELEVATION_EASE_THRESHOLD = 1;
const AVERAGE_ELEVATION_CHANGE_THRESHOLD = 1e-4;
function areTargetsEqual(a, b) {
if (Array.isArray(a) && Array.isArray(b)) {
const aSet = new Set(a);
const bSet = new Set(b);
return aSet.size === bSet.size && a.every((id) => bSet.has(id));
} else {
return index$1.deepEqual(a, b);
}
}
const defaultMinZoom = -2;
const defaultMaxZoom = 22;
const defaultMinPitch = 0;
const defaultMaxPitch = 85;
const defaultOptions$5 = {
center: [0, 0],
zoom: 0,
bearing: 0,
pitch: 0,
minZoom: defaultMinZoom,
maxZoom: defaultMaxZoom,
minPitch: defaultMinPitch,
maxPitch: defaultMaxPitch,
interactive: true,
scrollZoom: true,
boxZoom: true,
dragRotate: true,
dragPan: true,
keyboard: true,
doubleClickZoom: true,
touchZoomRotate: true,
touchPitch: true,
cooperativeGestures: false,
performanceMetricsCollection: true,
bearingSnap: 7,
clickTolerance: 3,
pitchWithRotate: true,
hash: false,
attributionControl: true,
antialias: false,
failIfMajorPerformanceCaveat: false,
preserveDrawingBuffer: false,
trackResize: true,
renderWorldCopies: true,
refreshExpiredTiles: true,
minTileCacheSize: null,
maxTileCacheSize: null,
localIdeographFontFamily: "sans-serif",
localFontFamily: null,
transformRequest: null,
accessToken: null,
fadeDuration: 300,
respectPrefersReducedMotion: true,
crossSourceCollisions: true,
collectResourceTiming: false,
testMode: false,
precompilePrograms: true,
scaleFactor: 1,
spriteFormat: "auto"
};
let Map$1 = class Map extends Camera {
constructor(options) {
index$1.LivePerformanceUtils.mark(index$1.LivePerformanceMarkers.create);
const initialOptions = options;
options = Object.assign({}, defaultOptions$5, options);
if (options.minZoom != null && options.maxZoom != null && options.minZoom > options.maxZoom) {
throw new Error(`maxZoom must be greater than or equal to minZoom`);
}
if (options.minPitch != null && options.maxPitch != null && options.minPitch > options.maxPitch) {
throw new Error(`maxPitch must be greater than or equal to minPitch`);
}
if (options.minPitch != null && options.minPitch < defaultMinPitch) {
throw new Error(`minPitch must be greater than or equal to ${defaultMinPitch}`);
}
if (options.maxPitch != null && options.maxPitch > defaultMaxPitch) {
throw new Error(`maxPitch must be less than or equal to ${defaultMaxPitch}`);
}
if (options.antialias && index$1.isSafariWithAntialiasingBug(window)) {
options.antialias = false;
index$1.warnOnce("Antialiasing is disabled for this WebGL context to avoid browser bug: https://github.com/mapbox/mapbox-gl-js/issues/11609");
}
const transform = new Transform(options.minZoom, options.maxZoom, options.minPitch, options.maxPitch, options.renderWorldCopies, null, null);
super(transform, options);
this._repaint = !!options.repaint;
this._interactive = options.interactive;
this._minTileCacheSize = options.minTileCacheSize;
this._maxTileCacheSize = options.maxTileCacheSize;
this._failIfMajorPerformanceCaveat = options.failIfMajorPerformanceCaveat;
this._preserveDrawingBuffer = options.preserveDrawingBuffer;
this._antialias = options.antialias;
this._trackResize = options.trackResize;
this._bearingSnap = options.bearingSnap;
this._refreshExpiredTiles = options.refreshExpiredTiles;
this._fadeDuration = options.fadeDuration;
this._isInitialLoad = true;
this._crossSourceCollisions = options.crossSourceCollisions;
this._collectResourceTiming = options.collectResourceTiming;
this._language = this._parseLanguage(options.language);
this._worldview = options.worldview;
this._renderTaskQueue = new TaskQueue();
this._domRenderTaskQueue = new TaskQueue();
this._controls = [];
this._markers = [];
this._popups = [];
this._mapId = index$1.uniqueId();
this._locale = Object.assign({}, defaultLocale, options.locale);
this._clickTolerance = options.clickTolerance;
this._cooperativeGestures = options.cooperativeGestures;
this._performanceMetricsCollection = options.performanceMetricsCollection;
this._tessellationStep = options.tessellationStep;
this._containerWidth = 0;
this._containerHeight = 0;
this._showParseStatus = true;
this._precompilePrograms = options.precompilePrograms;
this._scaleFactorChanged = false;
this._averageElevationLastSampledAt = -Infinity;
this._averageElevationExaggeration = 0;
this._averageElevation = new EasedVariable(0);
this._interactionRange = [Infinity, -Infinity];
this._visibilityHidden = 0;
this._useExplicitProjection = false;
this._frameId = 0;
this._scaleFactor = options.scaleFactor;
this._requestManager = new RequestManager(options.transformRequest, options.accessToken, options.testMode);
this._silenceAuthErrors = !!options.testMode;
if (options.contextCreateOptions) {
this._contextCreateOptions = Object.assign({}, options.contextCreateOptions);
} else {
this._contextCreateOptions = {};
}
if (typeof options.container === "string") {
const container = document.getElementById(options.container);
if (container) {
this._container = container;
} else {
throw new Error(`Container '${options.container.toString()}' not found.`);
}
} else if (options.container instanceof HTMLElement) {
this._container = options.container;
} else {
throw new Error(`Invalid type: 'container' must be a String or HTMLElement.`);
}
if (this._container.childNodes.length > 0) {
index$1.warnOnce(`The map container element should be empty, otherwise the map's interactivity will be negatively impacted. If you want to display a message when WebGL is not supported, use the Mapbox GL Supported plugin instead.`);
}
if (options.maxBounds) {
this.setMaxBounds(options.maxBounds);
}
this._spriteFormat = options.spriteFormat;
index$1.bindAll([
"_onWindowOnline",
"_onWindowResize",
"_onVisibilityChange",
"_onMapScroll",
"_contextLost",
"_contextRestored"
], this);
this._setupContainer();
if (options.devtools) DevTools.addTo(this);
DevTools.addParameter(this, "showOverdrawInspector", "Debug");
DevTools.addParameter(this, "showTileBoundaries", "Debug");
DevTools.addParameter(this, "showParseStatus", "Debug");
DevTools.addParameter(this, "repaint", "Debug");
DevTools.addParameter(this, "showTileAABBs", "Debug");
DevTools.addParameter(this, "showPadding", "Debug");
DevTools.addParameter(this, "showCollisionBoxes", "Debug", {}, () => this._update());
DevTools.addParameter(this.transform, "freezeTileCoverage", "Debug", {}, () => this._update());
DevTools.addParameter(this, "showTerrainWireframe", "Debug");
DevTools.addParameter(this, "showLayers2DWireframe", "Debug");
DevTools.addParameter(this, "showLayers3DWireframe", "Debug");
DevTools.addParameter(this, "_scaleFactor", "Scaling", { label: "scaleFactor", min: 0.1, max: 10, step: 0.1 }, () => this.setScaleFactor(this._scaleFactor));
this._setupPainter();
if (this.painter === void 0) {
throw new Error(`Failed to initialize WebGL.`);
}
this.on("move", () => this._update(false));
this.on("moveend", () => this._update(false));
this.on("zoom", () => this._update(true));
this._fullscreenchangeEvent = "onfullscreenchange" in document ? "fullscreenchange" : "webkitfullscreenchange";
window.addEventListener("online", this._onWindowOnline, false);
window.addEventListener("resize", this._onWindowResize, false);
window.addEventListener("orientationchange", this._onWindowResize, false);
window.addEventListener(this._fullscreenchangeEvent, this._onWindowResize, false);
window.addEventListener("visibilitychange", this._onVisibilityChange, false);
this.handlers = new HandlerManager(this, options);
this._localFontFamily = options.localFontFamily;
this._localIdeographFontFamily = options.localIdeographFontFamily;
if (options.style || !options.testMode) {
const style = options.style || index$1.config.DEFAULT_STYLE;
this.setStyle(style, {
config: options.config,
localFontFamily: this._localFontFamily,
localIdeographFontFamily: this._localIdeographFontFamily
});
}
if (options.projection) {
this.setProjection(options.projection);
}
const hashName = typeof options.hash === "string" && options.hash || void 0;
if (options.hash) this._hash = new Hash(hashName).addTo(this);
if (!this._hash || !this._hash._onHashChange()) {
if (initialOptions.center != null || initialOptions.zoom != null) {
this.transform._unmodified = false;
}
this.jumpTo({
center: options.center,
zoom: options.zoom,
bearing: options.bearing,
pitch: options.pitch
});
const bounds = options.bounds;
if (bounds) {
this.resize();
this.fitBounds(bounds, Object.assign({}, options.fitBoundsOptions, { duration: 0 }));
}
}
this.resize();
if (options.attributionControl)
this.addControl(new AttributionControl({ customAttribution: options.customAttribution }));
this._logoControl = new LogoControl();
this.addControl(this._logoControl, options.logoPosition);
this.on("style.load", () => {
if (this.transform.unmodified) {
this.jumpTo(this.style.stylesheet);
}
this._postStyleLoadEvent();
this._postStyleWithAppearanceEvent();
this._setupIndoor();
});
this.on("data", (event) => {
this._update(event.dataType === "style");
this.fire(new index$1.Event(`${event.dataType}data`, event));
});
this.on("dataloading", (event) => {
this.fire(new index$1.Event(`${event.dataType}dataloading`, event));
});
this._interactions = new InteractionSet(this);
}
/*
* Returns a unique number for this map instance which is used for the MapLoadEvent
* to make sure we only fire one event per instantiated map object.
* @private
* @returns {number}
*/
_getMapId() {
return this._mapId;
}
/** @section Controls */
/**
* Adds an {@link IControl} to the map, calling `control.onAdd(this)`.
*
* @param {IControl} control The {@link IControl} to add.
* @param {string} [position] Position on the map to which the control will be added.
* Valid values are `'top-left'`, `'top'`, `'top-right'`, `'right'`, `'bottom-right'`,
* `'bottom'`, `'bottom-left'`, and `'left'`. Defaults to `'top-right'`.
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Add zoom and rotation controls to the map.
* map.addControl(new mapboxgl.NavigationControl());
* @see [Example: Display map navigation controls](https://www.mapbox.com/mapbox-gl-js/example/navigation/)
*/
addControl(control, position) {
if (position === void 0) {
if (control.getDefaultPosition) {
position = control.getDefaultPosition();
} else {
position = "top-right";
}
}
if (!control || !control.onAdd) {
return this.fire(new index$1.ErrorEvent(new Error(
"Invalid argument to map.addControl(). Argument must be a control with onAdd and onRemove methods."
)));
}
const controlElement = control.onAdd(this);
this._controls.push(control);
const positionContainer = this._controlPositions[position];
if (position.indexOf("bottom") !== -1) {
positionContainer.insertBefore(controlElement, positionContainer.firstChild);
} else {
positionContainer.appendChild(controlElement);
}
return this;
}
/**
* Removes the control from the map.
*
* @param {IControl} control The {@link IControl} to remove.
* @returns {Map} Returns itself to allow for method chaining.
* @example
* // Define a new navigation control.
* const navigation = new mapboxgl.NavigationControl();
* // Add zoom and rotation controls to the map.
* map.addControl(navigation);
* // Remove zoom and rotation controls from the map.
* map.removeControl(navigation);
*/
removeControl(control) {
if (!control || !control.onRemove) {
return this.fire(new index$1.ErrorEvent(new Error(
"Invalid argument to map.removeControl(). Argument must be a control with onAdd and onRemove methods."
)));
}
const ci = this._controls.indexOf(control);
if (ci > -1) this._controls.splice(ci, 1);
control.onRemove(this);
return this;
}
/**
* Checks if a control is on the map.
*
* @param {IControl} control The {@link IControl} to check.
* @returns {boolean} True if map contains control.
* @example
* // Define a new navigation control.
* const navigation = new mapboxgl.NavigationControl();
* // Add zoom and rotation controls to the map.
* map.addControl(navigation);
* // Check that the navigation control exists on the map.
* const added = map.hasControl(navigation);
* // added === true
*/
hasControl(control) {
return this._controls.indexOf(control) > -1;
}
/**
* Returns the map's containing HTML element.
*
* @returns {HTMLElement} The map's container.
* @example
* const container = map.getContainer();
*/
getContainer() {
return this._container;
}
/**
* Returns the HTML element containing the map's `