Spaces:
Sleeping
Sleeping
File size: 6,754 Bytes
9a14526 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | // @flow
import {warnOnce} from '../util/util';
import type Context from '../gl/context';
/**
* A LineAtlas lets us reuse rendered dashed lines
* by writing many of them to a texture and then fetching their positions
* using .getDash.
*
* @param {number} width
* @param {number} height
* @private
*/
class LineAtlas {
width: number;
height: number;
nextRow: number;
bytes: number;
data: Uint8Array;
dashEntry: {[_: string]: any};
dirty: boolean;
texture: WebGLTexture;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
this.nextRow = 0;
this.data = new Uint8Array(this.width * this.height);
this.dashEntry = {};
}
/**
* Get or create a dash line pattern.
*
* @param {Array<number>} dasharray
* @param {boolean} round whether to add circle caps in between dash segments
* @returns {Object} position of dash texture in { y, height, width }
* @private
*/
getDash(dasharray: Array<number>, round: boolean) {
const key = dasharray.join(",") + String(round);
if (!this.dashEntry[key]) {
this.dashEntry[key] = this.addDash(dasharray, round);
}
return this.dashEntry[key];
}
getDashRanges(dasharray: Array<number>, lineAtlasWidth: number, stretch: number) {
// If dasharray has an odd length, both the first and last parts
// are dashes and should be joined seamlessly.
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: Object, stretch: number, n: number) {
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.data[index + x] = Math.max(0, Math.min(255, signedDistance + 128));
}
}
}
addRegularDash(ranges: Object) {
// Collapse any zero-length range
// Collapse neighbouring same-type parts into a single part
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);
}
}
// Combine the first and last parts if possible
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;
this.data[index + x] = Math.max(0, Math.min(255, signedDistance + 128));
}
}
addDash(dasharray: Array<number>, round: boolean) {
const n = round ? 7 : 0;
const height = 2 * n + 1;
if (this.nextRow + height > this.height) {
warnOnce('LineAtlas out of space');
return null;
}
let length = 0;
for (let i = 0; i < dasharray.length; i++) { 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 {
this.addRegularDash(ranges);
}
}
const dashEntry = {
y: (this.nextRow + n + 0.5) / this.height,
height: 2 * n / this.height,
width: length
};
this.nextRow += height;
this.dirty = true;
return dashEntry;
}
bind(context: Context) {
const gl = context.gl;
if (!this.texture) {
this.texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.ALPHA, this.width, this.height, 0, gl.ALPHA, gl.UNSIGNED_BYTE, this.data);
} else {
gl.bindTexture(gl.TEXTURE_2D, this.texture);
if (this.dirty) {
this.dirty = false;
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this.width, this.height, gl.ALPHA, gl.UNSIGNED_BYTE, this.data);
}
}
}
}
export default LineAtlas;
|