-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathfillbucket.js
76 lines (59 loc) · 2.47 KB
/
fillbucket.js
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
'use strict';
var ElementGroups = require('./elementgroups.js');
module.exports = FillBucket;
function FillBucket(info, buffers, placement, elementGroups) {
this.info = info;
this.buffers = buffers;
this.elementGroups = elementGroups || new ElementGroups(buffers.fillVertex, buffers.fillElement);
}
FillBucket.prototype.addFeatures = function() {
var features = this.features;
for (var i = 0; i < features.length; i++) {
var feature = features[i];
this.addFeature(feature.loadGeometry());
}
};
FillBucket.prototype.addFeature = function(lines) {
for (var i = 0; i < lines.length; i++) {
this.addFill(lines[i]);
}
};
FillBucket.prototype.addFill = function(vertices) {
if (vertices.length < 3) {
//console.warn('a fill must have at least three vertices');
return;
}
// Calculate the total number of vertices we're going to produce so that we
// can resize the buffer beforehand, or detect whether the current line
// won't fit into the buffer anymore.
// In order to be able to use the vertex buffer for drawing the antialiased
// outlines, we separate all polygon vertices with a degenerate (out-of-
// viewplane) vertex.
var len = vertices.length;
// Check whether this geometry buffer can hold all the required vertices.
this.elementGroups.makeRoomFor(len + 1);
var elementGroup = this.elementGroups.current;
var fillVertex = this.buffers.fillVertex;
var fillElement = this.buffers.fillElement;
// Start all lines with a degenerate vertex
fillVertex.addDegenerate();
elementGroup.vertexLength++;
// We're generating triangle fans, so we always start with the first coordinate in this polygon.
var firstIndex = fillVertex.index - elementGroup.vertexStartIndex,
prevIndex, currentIndex, currentVertex;
for (var i = 0; i < vertices.length; i++) {
currentIndex = fillVertex.index - elementGroup.vertexStartIndex;
currentVertex = vertices[i];
fillVertex.add(currentVertex.x, currentVertex.y);
elementGroup.vertexLength++;
// Only add triangles that have distinct vertices.
if (i >= 2 && (currentVertex.x !== vertices[0].x || currentVertex.y !== vertices[0].y)) {
fillElement.add(firstIndex, prevIndex, currentIndex);
elementGroup.elementLength++;
}
prevIndex = currentIndex;
}
};
FillBucket.prototype.hasData = function() {
return !!this.elementGroups.current;
};