Refactoring ol.render.webgl.LineStringReplay

This commit is contained in:
GaborFarkas
2016-06-15 11:42:08 +02:00
parent c13d09ba23
commit 0f8e1a7e40
3 changed files with 111 additions and 186 deletions

View File

@@ -138,17 +138,15 @@ goog.inherits(ol.render.webgl.Replay, ol.render.VectorContext);
* @enum {number} * @enum {number}
*/ */
ol.render.webgl.LineStringInstruction = { ol.render.webgl.LineStringInstruction = {
BEGIN_LINE: 0, ROUND: 2,
END_LINE: 1, BEGIN_LINE: 3,
BEGIN_LINE_CAP: 2, END_LINE: 5,
BEVEL_FIRST: 3, BEGIN_LINE_CAP: 7,
BEVEL_SECOND: 4, END_LINE_CAP : 11,
MITER_BOTTOM: 5, BEVEL_FIRST: 13,
MITER_TOP: 6, BEVEL_SECOND: 17,
ROUND_JOIN: 7, MITER_BOTTOM: 19,
ROUND_CAP: 8, MITER_TOP: 23
ROUND_BOTH: 9,
END_LINE_CAP : 10
}; };
ol.render.webgl.Replay.prototype.getDeleteResourcesFunction = goog.abstractMethod; ol.render.webgl.Replay.prototype.getDeleteResourcesFunction = goog.abstractMethod;
@@ -1030,7 +1028,7 @@ ol.inherits(ol.render.webgl.LineStringReplay, ol.render.webgl.Replay);
/** /**
* Draw one line. * Draw one segment.
* @param {Array.<number>} flatCoordinates Flat coordinates. * @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset. * @param {number} offset Offset.
* @param {number} end End. * @param {number} end End.
@@ -1042,8 +1040,13 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
var i, ii; var i, ii;
var numVertices = this.vertices_.length; var numVertices = this.vertices_.length;
var numIndices = this.indices_.length; var numIndices = this.indices_.length;
var lineJoin = this.state_.lineJoin === 'bevel' ? false : true; //To save a vertex, the direction of a point is a product of the sign (1 or -1), a prime from
var lineCap = this.state_.lineCap === 'butt' ? false : true; //ol.render.webgl.LineStringInstruction, and a rounding factor (1 or 2). If the product is even,
//we round it. If it is odd, we don't.
var lineJoin = this.state_.lineJoin === 'bevel' ? 0 :
this.state_.lineJoin === 'miter' ? 1 : 2;
var lineCap = this.state_.lineCap === 'butt' ? 0 :
this.state_.lineCap === 'square' ? 1 : 2;
var closed = this.isClosed_(flatCoordinates, offset, end, stride); var closed = this.isClosed_(flatCoordinates, offset, end, stride);
var lastIndex = numIndices; var lastIndex = numIndices;
var lastSign = 1; var lastSign = 1;
@@ -1053,7 +1056,7 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
for (i = offset, ii = end; i < ii; i += stride) { for (i = offset, ii = end; i < ii; i += stride) {
var n = numVertices / 8; var n = numVertices / 7;
tempP = tempP || p1; tempP = tempP || p1;
p1 = p2 || [flatCoordinates[i] - this.origin_[0], flatCoordinates[i + 1] - this.origin_[1]]; p1 = p2 || [flatCoordinates[i] - this.origin_[0], flatCoordinates[i + 1] - this.origin_[1]];
@@ -1068,26 +1071,13 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
tempP = [flatCoordinates[end - stride] - this.origin_[0], flatCoordinates[end - stride + 1] - this.origin_[1]]; tempP = [flatCoordinates[end - stride] - this.origin_[0], flatCoordinates[end - stride + 1] - this.origin_[1]];
} else { } else {
//Add the first two/four vertices. //Add the first two/four vertices.
p0 = undefined;
if (lineCap) { if (lineCap) {
this.vertices_[numVertices++] = 0; numVertices = this.addVertices_([0, 0], p1, p2,
this.vertices_[numVertices++] = 0; lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP;
this.vertices_[numVertices++] = 0; numVertices = this.addVertices_([0, 0], p1, p2,
this.vertices_[numVertices++] = 0; -lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP * lineCap, numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = -lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEGIN_LINE_CAP;
this.indices_[numIndices++] = n + 2; this.indices_[numIndices++] = n + 2;
this.indices_[numIndices++] = n; this.indices_[numIndices++] = n;
@@ -1100,23 +1090,11 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
n = n + 2; n = n + 2;
} }
this.vertices_[numVertices++] = 0; numVertices = this.addVertices_([0, 0], p1, p2,
this.vertices_[numVertices++] = 0; lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEGIN_LINE;
this.vertices_[numVertices++] = 0; numVertices = this.addVertices_([0, 0], p1, p2,
this.vertices_[numVertices++] = 0; -lastSign * ol.render.webgl.LineStringInstruction.BEGIN_LINE * (lineCap || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = -lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEGIN_LINE;
lastIndex = n + 1; lastIndex = n + 1;
@@ -1128,25 +1106,15 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
p2 = [flatCoordinates[offset] - this.origin_[0], flatCoordinates[offset + 1] - this.origin_[1]]; p2 = [flatCoordinates[offset] - this.origin_[0], flatCoordinates[offset + 1] - this.origin_[1]];
} else { } else {
p2 = undefined; p2 = undefined;
p0 = tempP || p0; //Note, that the third case will never happen, we just have to assure the compiler,
//p0 is always an array of nums.
p0 = tempP || p0 || [0, 0];
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, [0, 0],
this.vertices_[numVertices++] = p0[1]; lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.END_LINE;
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, [0, 0],
this.vertices_[numVertices++] = p0[1]; -lastSign * ol.render.webgl.LineStringInstruction.END_LINE * (lineCap || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = -lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.END_LINE;
this.indices_[numIndices++] = n; this.indices_[numIndices++] = n;
this.indices_[numIndices++] = lastIndex - 1; this.indices_[numIndices++] = lastIndex - 1;
@@ -1157,23 +1125,11 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
this.indices_[numIndices++] = n; this.indices_[numIndices++] = n;
if (lineCap) { if (lineCap) {
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, [0, 0],
this.vertices_[numVertices++] = p0[1]; lastSign * ol.render.webgl.LineStringInstruction.END_LINE_CAP * lineCap, numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.END_LINE_CAP;
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, [0, 0],
this.vertices_[numVertices++] = p0[1]; -lastSign * ol.render.webgl.LineStringInstruction.END_LINE_CAP * lineCap, numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = 0;
this.vertices_[numVertices++] = -lastSign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.END_LINE_CAP;
this.indices_[numIndices++] = n + 2; this.indices_[numIndices++] = n + 2;
this.indices_[numIndices++] = n; this.indices_[numIndices++] = n;
@@ -1198,32 +1154,14 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
var sign = ol.geom.flat.orient.linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2) var sign = ol.geom.flat.orient.linearRingIsClockwise([p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]], 0, 6, 2)
? 1 : -1; ? 1 : -1;
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, p2,
this.vertices_[numVertices++] = p0[1]; sign * ol.render.webgl.LineStringInstruction.BEVEL_FIRST * (lineJoin || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = sign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEVEL_FIRST;
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, p2,
this.vertices_[numVertices++] = p0[1]; sign * ol.render.webgl.LineStringInstruction.BEVEL_SECOND * (lineJoin || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = sign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.BEVEL_SECOND;
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, p2,
this.vertices_[numVertices++] = p0[1]; -sign * ol.render.webgl.LineStringInstruction.MITER_BOTTOM * (lineJoin || 1), numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = -sign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.MITER_BOTTOM;
this.indices_[numIndices++] = n; this.indices_[numIndices++] = n;
this.indices_[numIndices++] = lastIndex - 1; this.indices_[numIndices++] = lastIndex - 1;
@@ -1242,14 +1180,8 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
//Add miter //Add miter
if (lineJoin) { if (lineJoin) {
this.vertices_[numVertices++] = p0[0]; numVertices = this.addVertices_(p0, p1, p2,
this.vertices_[numVertices++] = p0[1]; sign * ol.render.webgl.LineStringInstruction.MITER_TOP * lineJoin, numVertices);
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = sign;
this.vertices_[numVertices++] = ol.render.webgl.LineStringInstruction.MITER_TOP;
this.indices_[numIndices++] = n + 1; this.indices_[numIndices++] = n + 1;
this.indices_[numIndices++] = n + 3; this.indices_[numIndices++] = n + 3;
@@ -1258,6 +1190,27 @@ ol.render.webgl.LineStringReplay.prototype.drawCoordinates_ = function(flatCoord
} }
}; };
/**
* @param {Array.<number>} p0 Last coordinates.
* @param {Array.<number>} p1 Current coordinates.
* @param {Array.<number>} p2 Next coordinates.
* @param {number} product Sign, instruction, and rounding product.
* @param {number} numVertices Vertex counter.
* @return {number} Vertex counter.
* @private
*/
ol.render.webgl.LineStringReplay.prototype.addVertices_ = function(p0, p1, p2, product, numVertices) {
this.vertices_[numVertices++] = p0[0];
this.vertices_[numVertices++] = p0[1];
this.vertices_[numVertices++] = p1[0];
this.vertices_[numVertices++] = p1[1];
this.vertices_[numVertices++] = p2[0];
this.vertices_[numVertices++] = p2[1];
this.vertices_[numVertices++] = product;
return numVertices;
};
/** /**
* Check if the linestring is a boundary. * Check if the linestring is a boundary.
* @param {Array.<number>} flatCoordinates Flat coordinates. * @param {Array.<number>} flatCoordinates Flat coordinates.
@@ -1383,28 +1336,19 @@ ol.render.webgl.LineStringReplay.prototype.setUpProgram_ = function(gl, context,
// enable the vertex attrib arrays // enable the vertex attrib arrays
gl.enableVertexAttribArray(locations.a_lastPos); gl.enableVertexAttribArray(locations.a_lastPos);
gl.vertexAttribPointer(locations.a_lastPos, 2, goog.webgl.FLOAT, gl.vertexAttribPointer(locations.a_lastPos, 2, goog.webgl.FLOAT,
false, 32, 0); false, 28, 0);
gl.enableVertexAttribArray(locations.a_position); gl.enableVertexAttribArray(locations.a_position);
gl.vertexAttribPointer(locations.a_position, 2, goog.webgl.FLOAT, gl.vertexAttribPointer(locations.a_position, 2, goog.webgl.FLOAT,
false, 32, 8); false, 28, 8);
gl.enableVertexAttribArray(locations.a_nextPos); gl.enableVertexAttribArray(locations.a_nextPos);
gl.vertexAttribPointer(locations.a_nextPos, 2, goog.webgl.FLOAT, gl.vertexAttribPointer(locations.a_nextPos, 2, goog.webgl.FLOAT,
false, 32, 16); false, 28, 16);
gl.enableVertexAttribArray(locations.a_direction); gl.enableVertexAttribArray(locations.a_direction);
gl.vertexAttribPointer(locations.a_direction, 1, goog.webgl.FLOAT, gl.vertexAttribPointer(locations.a_direction, 1, goog.webgl.FLOAT,
false, 32, 24); false, 28, 24);
gl.enableVertexAttribArray(locations.a_instruction);
gl.vertexAttribPointer(locations.a_instruction, 1, goog.webgl.FLOAT,
false, 32, 28);
var round = this.state_.lineJoin === 'round' && this.state_.lineCap === 'round' ?
ol.render.webgl.LineStringInstruction.ROUND_BOTH : this.state_.lineJoin === 'round' ?
ol.render.webgl.LineStringInstruction.ROUND_JOIN : this.state_.lineCap === 'round' ?
ol.render.webgl.LineStringInstruction.ROUND_CAP : 0;
// Enable renderer specific uniforms. If clauses needed, as otherwise the compiler complains. // Enable renderer specific uniforms. If clauses needed, as otherwise the compiler complains.
gl.uniform4fv(locations.u_color, this.state_.strokeColor); gl.uniform4fv(locations.u_color, this.state_.strokeColor);
@@ -1415,7 +1359,6 @@ ol.render.webgl.LineStringReplay.prototype.setUpProgram_ = function(gl, context,
gl.uniform1f(locations.u_miterLimit, this.state_.miterLimit); gl.uniform1f(locations.u_miterLimit, this.state_.miterLimit);
} }
gl.uniform2fv(locations.u_size, size); gl.uniform2fv(locations.u_size, size);
gl.uniform1f(locations.u_round, round);
return locations; return locations;
}; };

View File

@@ -13,53 +13,50 @@ attribute vec2 a_lastPos;
attribute vec2 a_position; attribute vec2 a_position;
attribute vec2 a_nextPos; attribute vec2 a_nextPos;
attribute float a_direction; attribute float a_direction;
attribute float a_instruction;
uniform mat4 u_projectionMatrix; uniform mat4 u_projectionMatrix;
uniform mat4 u_offsetScaleMatrix; uniform mat4 u_offsetScaleMatrix;
uniform float u_lineWidth; uniform float u_lineWidth;
uniform float u_miterLimit; uniform float u_miterLimit;
uniform float u_round;
void main(void) { void main(void) {
v_halfWidth = u_lineWidth / 2.0; v_halfWidth = u_lineWidth / 2.0;
vec2 offset; vec2 offset;
v_round = 0.0; v_round = 0.0;
float direction = a_direction / abs(a_direction);
vec4 projPos = u_projectionMatrix * vec4(a_position, 0., 1.); vec4 projPos = u_projectionMatrix * vec4(a_position, 0., 1.);
if (a_instruction == 0. || a_instruction == 4.) { if (mod(a_direction, 3.0) == 0.0 || mod(a_direction, 17.0) == 0.0) {
vec2 dirVect = a_nextPos - a_position; vec2 dirVect = a_nextPos - a_position;
vec2 normal = normalize(vec2(-dirVect.y, dirVect.x)); vec2 normal = normalize(vec2(-dirVect.y, dirVect.x));
offset = v_halfWidth * normal * a_direction; offset = v_halfWidth * normal * direction;
if ((a_instruction == 4. && (u_round == 7. || u_round == 9.)) || if (mod(a_direction, 2.0) == 0.0) {
(a_instruction == 0. && (u_round == 8. || u_round == 9.))) {
v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);
} }
} else if (a_instruction == 1. || a_instruction == 3.) { } else if (mod(a_direction, 5.0) == 0.0 || mod(a_direction, 13.0) == 0.0) {
vec2 dirVect = a_lastPos - a_position; vec2 dirVect = a_lastPos - a_position;
vec2 normal = normalize(vec2(dirVect.y, -dirVect.x)); vec2 normal = normalize(vec2(dirVect.y, -dirVect.x));
offset = v_halfWidth * normal * a_direction; offset = v_halfWidth * normal * direction;
if ((a_instruction == 3. && (u_round == 7. || u_round == 9.)) || if (mod(a_direction, 2.0) == 0.0) {
(a_instruction == 1. && (u_round == 8. || u_round == 9.))) {
v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);
} }
} else if (a_instruction == 5. || a_instruction == 6.) { } else if (mod(a_direction, 19.0) == 0.0 || mod(a_direction, 23.0) == 0.0) {
vec2 dirVect = a_nextPos - a_position; vec2 dirVect = a_nextPos - a_position;
vec2 tmpNormal = normalize(vec2(-dirVect.y, dirVect.x)); vec2 tmpNormal = normalize(vec2(-dirVect.y, dirVect.x));
vec2 tangent = normalize(normalize(a_nextPos - a_position) + normalize(a_position - a_lastPos)); vec2 tangent = normalize(normalize(a_nextPos - a_position) + normalize(a_position - a_lastPos));
vec2 normal = vec2(tangent.y, -tangent.x); vec2 normal = vec2(tangent.y, -tangent.x);
float miterLength = v_halfWidth / dot(normal, tmpNormal); float miterLength = v_halfWidth / dot(normal, tmpNormal);
if (a_instruction == 6.) { if (mod(a_direction, 23.0) == 0.0) {
if (u_round == 7. || u_round == 9.) { if (mod(a_direction, 2.0) == 0.0) {
offset = normal * a_direction * miterLength; offset = normal * direction * miterLength;
v_round = 1.0; v_round = 1.0;
v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);
} else if (miterLength > u_miterLimit) { } else if (miterLength > u_miterLimit) {
offset = tmpNormal * a_direction * v_halfWidth; offset = tmpNormal * direction * v_halfWidth;
} else { } else {
offset = normal * a_direction * miterLength; offset = normal * direction * miterLength;
} }
} else if (a_instruction == 5.) { } else {
offset = normal * a_direction * miterLength; offset = normal * direction * miterLength;
vec4 defaultOffset = u_offsetScaleMatrix * vec4(0., 0., 0., 0.); vec4 defaultOffset = u_offsetScaleMatrix * vec4(0., 0., 0., 0.);
vec4 firstProjPos = u_projectionMatrix * vec4(a_lastPos, 0., 1.) + defaultOffset; vec4 firstProjPos = u_projectionMatrix * vec4(a_lastPos, 0., 1.) + defaultOffset;
vec4 secondProjPos = projPos + defaultOffset; vec4 secondProjPos = projPos + defaultOffset;
@@ -76,30 +73,27 @@ void main(void) {
} else { } else {
projPos = thirdProjPos - defaultOffset; projPos = thirdProjPos - defaultOffset;
} }
offset = tmpNormal * a_direction * v_halfWidth; offset = tmpNormal * direction * v_halfWidth;
} }
} }
} else if (a_instruction == 2.) { } else if (mod(a_direction, 7.0) == 0.0 || mod(a_direction, 11.0) == 0.0) {
vec2 dirVect = a_position - a_nextPos; vec2 normal;
vec2 firstNormal = normalize(dirVect); if (mod(a_direction, 7.0) == 0.0) {
vec2 secondNormal = vec2(firstNormal.y * a_direction, -firstNormal.x * a_direction); vec2 dirVect = a_position - a_nextPos;
vec2 hypotenuse = normalize(firstNormal - secondNormal); vec2 firstNormal = normalize(dirVect);
vec2 normal = vec2(hypotenuse.y * a_direction, -hypotenuse.x * a_direction); vec2 secondNormal = vec2(firstNormal.y * direction, -firstNormal.x * direction);
float length = sqrt(v_halfWidth * v_halfWidth * 2.0); vec2 hypotenuse = normalize(firstNormal - secondNormal);
offset = normal * length; normal = vec2(hypotenuse.y * direction, -hypotenuse.x * direction);
if (u_round == 8. || u_round == 9.) { } else {
v_round = 1.0; vec2 dirVect = a_position - a_lastPos;
v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); vec2 firstNormal = normalize(dirVect);
vec2 secondNormal = vec2(-firstNormal.y * direction, firstNormal.x * direction);
vec2 hypotenuse = normalize(firstNormal - secondNormal);
normal = vec2(-hypotenuse.y * direction, hypotenuse.x * direction);
} }
} else if (a_instruction == 10.) {
vec2 dirVect = a_position - a_lastPos;
vec2 firstNormal = normalize(dirVect);
vec2 secondNormal = vec2(-firstNormal.y * a_direction, firstNormal.x * a_direction);
vec2 hypotenuse = normalize(firstNormal - secondNormal);
vec2 normal = vec2(-hypotenuse.y * a_direction, hypotenuse.x * a_direction);
float length = sqrt(v_halfWidth * v_halfWidth * 2.0); float length = sqrt(v_halfWidth * v_halfWidth * 2.0);
offset = normal * length; offset = normal * length;
if (u_round == 8. || u_round == 9.) { if (mod(a_direction, 2.0) == 0.0) {
v_round = 1.0; v_round = 1.0;
v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);
} }

View File

@@ -30,7 +30,7 @@ ol.render.webgl.linestringreplay.shader.DefaultFragment.DEBUG_SOURCE = 'precisio
* @const * @const
* @type {string} * @type {string}
*/ */
ol.render.webgl.linestringreplay.shader.DefaultFragment.OPTIMIZED_SOURCE = 'precision mediump float;varying float a;varying vec4 b;varying float c;uniform float n;uniform vec4 o;uniform vec2 p;void main(void){if(a>0.0){vec2 windowCoords=vec2((b.x+1.0)/2.0*p.x,(b.y+1.0)/2.0*p.y);if(length(windowCoords-gl_FragCoord.xy)>c){discard;}} gl_FragColor=o;float alpha=o.a*n;if(alpha==0.0){discard;}gl_FragColor.a=alpha;}'; ol.render.webgl.linestringreplay.shader.DefaultFragment.OPTIMIZED_SOURCE = 'precision mediump float;varying float a;varying vec4 b;varying float c;uniform float l;uniform vec4 m;uniform vec2 n;void main(void){if(a>0.0){vec2 windowCoords=vec2((b.x+1.0)/2.0*n.x,(b.y+1.0)/2.0*n.y);if(length(windowCoords-gl_FragCoord.xy)>c){discard;}} gl_FragColor=m;float alpha=m.a*l;if(alpha==0.0){discard;}gl_FragColor.a=alpha;}';
/** /**
@@ -58,14 +58,14 @@ goog.addSingletonGetter(ol.render.webgl.linestringreplay.shader.DefaultVertex);
* @const * @const
* @type {string} * @type {string}
*/ */
ol.render.webgl.linestringreplay.shader.DefaultVertex.DEBUG_SOURCE = 'varying float v_round;\nvarying vec4 v_roundVertex;\nvarying float v_halfWidth;\n\n\nattribute vec2 a_lastPos;\nattribute vec2 a_position;\nattribute vec2 a_nextPos;\nattribute float a_direction;\nattribute float a_instruction;\n\nuniform mat4 u_projectionMatrix;\nuniform mat4 u_offsetScaleMatrix;\nuniform float u_lineWidth;\nuniform float u_miterLimit;\nuniform float u_round;\n\nvoid main(void) {\n v_halfWidth = u_lineWidth / 2.0;\n vec2 offset;\n v_round = 0.0;\n vec4 projPos = u_projectionMatrix * vec4(a_position, 0., 1.);\n if (a_instruction == 0. || a_instruction == 4.) {\n vec2 dirVect = a_nextPos - a_position;\n vec2 normal = normalize(vec2(-dirVect.y, dirVect.x));\n offset = v_halfWidth * normal * a_direction;\n if ((a_instruction == 4. && (u_round == 7. || u_round == 9.)) ||\n (a_instruction == 0. && (u_round == 8. || u_round == 9.))) {\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n } else if (a_instruction == 1. || a_instruction == 3.) {\n vec2 dirVect = a_lastPos - a_position;\n vec2 normal = normalize(vec2(dirVect.y, -dirVect.x));\n offset = v_halfWidth * normal * a_direction;\n if ((a_instruction == 3. && (u_round == 7. || u_round == 9.)) ||\n (a_instruction == 1. && (u_round == 8. || u_round == 9.))) {\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n } else if (a_instruction == 5. || a_instruction == 6.) {\n vec2 dirVect = a_nextPos - a_position;\n vec2 tmpNormal = normalize(vec2(-dirVect.y, dirVect.x));\n vec2 tangent = normalize(normalize(a_nextPos - a_position) + normalize(a_position - a_lastPos));\n vec2 normal = vec2(tangent.y, -tangent.x);\n float miterLength = v_halfWidth / dot(normal, tmpNormal);\n if (a_instruction == 6.) {\n if (u_round == 7. || u_round == 9.) {\n offset = normal * a_direction * miterLength;\n v_round = 1.0;\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n } else if (miterLength > u_miterLimit) {\n offset = tmpNormal * a_direction * v_halfWidth;\n } else {\n offset = normal * a_direction * miterLength;\n }\n } else if (a_instruction == 5.) {\n offset = normal * a_direction * miterLength;\n vec4 defaultOffset = u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n vec4 firstProjPos = u_projectionMatrix * vec4(a_lastPos, 0., 1.) + defaultOffset;\n vec4 secondProjPos = projPos + defaultOffset;\n vec4 thirdProjPos = u_projectionMatrix * vec4(a_nextPos, 0., 1.) + defaultOffset;\n float firstSegLength = distance(secondProjPos.xy, firstProjPos.xy);\n float secondSegLength = distance(thirdProjPos.xy, secondProjPos.xy);\n float miterSegLength = distance(secondProjPos.xy, vec4(projPos + u_offsetScaleMatrix * vec4(offset, 0., 0.)).xy);\n //TODO: Write a more accurate method for identifying sharp angles.\n if (miterSegLength > min(firstSegLength, secondSegLength)) {\n if (firstSegLength < secondSegLength) {\n dirVect = a_lastPos - a_position;\n tmpNormal = normalize(vec2(dirVect.y, -dirVect.x));\n projPos = firstProjPos - defaultOffset;\n } else {\n projPos = thirdProjPos - defaultOffset;\n }\n offset = tmpNormal * a_direction * v_halfWidth;\n }\n }\n } else if (a_instruction == 2.) {\n vec2 dirVect = a_position - a_nextPos;\n vec2 firstNormal = normalize(dirVect);\n vec2 secondNormal = vec2(firstNormal.y * a_direction, -firstNormal.x * a_direction);\n vec2 hypotenuse = normalize(firstNormal - secondNormal);\n vec2 normal = vec2(hypotenuse.y * a_direction, -hypotenuse.x * a_direction);\n float length = sqrt(v_halfWidth * v_halfWidth * 2.0);\n offset = normal * length;\n if (u_round == 8. || u_round == 9.) {\n v_round = 1.0;\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n } else if (a_instruction == 10.) {\n vec2 dirVect = a_position - a_lastPos;\n vec2 firstNormal = normalize(dirVect);\n vec2 secondNormal = vec2(-firstNormal.y * a_direction, firstNormal.x * a_direction);\n vec2 hypotenuse = normalize(firstNormal - secondNormal);\n vec2 normal = vec2(-hypotenuse.y * a_direction, hypotenuse.x * a_direction);\n float length = sqrt(v_halfWidth * v_halfWidth * 2.0);\n offset = normal * length;\n if (u_round == 8. || u_round == 9.) {\n v_round = 1.0;\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n }\n vec4 offsets = u_offsetScaleMatrix * vec4(offset, 0., 0.);\n gl_Position = projPos + offsets;\n}\n\n\n'; ol.render.webgl.linestringreplay.shader.DefaultVertex.DEBUG_SOURCE = 'varying float v_round;\nvarying vec4 v_roundVertex;\nvarying float v_halfWidth;\n\n\nattribute vec2 a_lastPos;\nattribute vec2 a_position;\nattribute vec2 a_nextPos;\nattribute float a_direction;\n\nuniform mat4 u_projectionMatrix;\nuniform mat4 u_offsetScaleMatrix;\nuniform float u_lineWidth;\nuniform float u_miterLimit;\n\nvoid main(void) {\n v_halfWidth = u_lineWidth / 2.0;\n vec2 offset;\n v_round = 0.0;\n float direction = a_direction / abs(a_direction);\n vec4 projPos = u_projectionMatrix * vec4(a_position, 0., 1.);\n if (mod(a_direction, 3.0) == 0.0 || mod(a_direction, 17.0) == 0.0) {\n vec2 dirVect = a_nextPos - a_position;\n vec2 normal = normalize(vec2(-dirVect.y, dirVect.x));\n offset = v_halfWidth * normal * direction;\n if (mod(a_direction, 2.0) == 0.0) {\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n } else if (mod(a_direction, 5.0) == 0.0 || mod(a_direction, 13.0) == 0.0) {\n vec2 dirVect = a_lastPos - a_position;\n vec2 normal = normalize(vec2(dirVect.y, -dirVect.x));\n offset = v_halfWidth * normal * direction;\n if (mod(a_direction, 2.0) == 0.0) {\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n } else if (mod(a_direction, 19.0) == 0.0 || mod(a_direction, 23.0) == 0.0) {\n vec2 dirVect = a_nextPos - a_position;\n vec2 tmpNormal = normalize(vec2(-dirVect.y, dirVect.x));\n vec2 tangent = normalize(normalize(a_nextPos - a_position) + normalize(a_position - a_lastPos));\n vec2 normal = vec2(tangent.y, -tangent.x);\n float miterLength = v_halfWidth / dot(normal, tmpNormal);\n if (mod(a_direction, 23.0) == 0.0) {\n if (mod(a_direction, 2.0) == 0.0) {\n offset = normal * direction * miterLength;\n v_round = 1.0;\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n } else if (miterLength > u_miterLimit) {\n offset = tmpNormal * direction * v_halfWidth;\n } else {\n offset = normal * direction * miterLength;\n }\n } else {\n offset = normal * direction * miterLength;\n vec4 defaultOffset = u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n vec4 firstProjPos = u_projectionMatrix * vec4(a_lastPos, 0., 1.) + defaultOffset;\n vec4 secondProjPos = projPos + defaultOffset;\n vec4 thirdProjPos = u_projectionMatrix * vec4(a_nextPos, 0., 1.) + defaultOffset;\n float firstSegLength = distance(secondProjPos.xy, firstProjPos.xy);\n float secondSegLength = distance(thirdProjPos.xy, secondProjPos.xy);\n float miterSegLength = distance(secondProjPos.xy, vec4(projPos + u_offsetScaleMatrix * vec4(offset, 0., 0.)).xy);\n //TODO: Write a more accurate method for identifying sharp angles.\n if (miterSegLength > min(firstSegLength, secondSegLength)) {\n if (firstSegLength < secondSegLength) {\n dirVect = a_lastPos - a_position;\n tmpNormal = normalize(vec2(dirVect.y, -dirVect.x));\n projPos = firstProjPos - defaultOffset;\n } else {\n projPos = thirdProjPos - defaultOffset;\n }\n offset = tmpNormal * direction * v_halfWidth;\n }\n }\n } else if (mod(a_direction, 7.0) == 0.0 || mod(a_direction, 11.0) == 0.0) {\n vec2 normal;\n if (mod(a_direction, 7.0) == 0.0) {\n vec2 dirVect = a_position - a_nextPos;\n vec2 firstNormal = normalize(dirVect);\n vec2 secondNormal = vec2(firstNormal.y * direction, -firstNormal.x * direction);\n vec2 hypotenuse = normalize(firstNormal - secondNormal);\n normal = vec2(hypotenuse.y * direction, -hypotenuse.x * direction);\n } else {\n vec2 dirVect = a_position - a_lastPos;\n vec2 firstNormal = normalize(dirVect);\n vec2 secondNormal = vec2(-firstNormal.y * direction, firstNormal.x * direction);\n vec2 hypotenuse = normalize(firstNormal - secondNormal);\n normal = vec2(-hypotenuse.y * direction, hypotenuse.x * direction);\n }\n float length = sqrt(v_halfWidth * v_halfWidth * 2.0);\n offset = normal * length;\n if (mod(a_direction, 2.0) == 0.0) {\n v_round = 1.0;\n v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.);\n }\n }\n vec4 offsets = u_offsetScaleMatrix * vec4(offset, 0., 0.);\n gl_Position = projPos + offsets;\n}\n\n\n';
/** /**
* @const * @const
* @type {string} * @type {string}
*/ */
ol.render.webgl.linestringreplay.shader.DefaultVertex.OPTIMIZED_SOURCE = 'varying float a;varying vec4 b;varying float c;attribute vec2 d;attribute vec2 e;attribute vec2 f;attribute float g;attribute float h;uniform mat4 i;uniform mat4 j;uniform float k;uniform float l;uniform float m;void main(void){c=k/2.0;vec2 offset;a=0.0;vec4 projPos=i*vec4(e,0.,1.);if(h==0.||h==4.){vec2 dirVect=f-e;vec2 normal=normalize(vec2(-dirVect.y,dirVect.x));offset=c*normal*g;if((h==4.&&(m==7.||m==9.))||(h==0.&&(m==8.||m==9.))){b=projPos+j*vec4(0.,0.,0.,0.);}} else if(h==1.||h==3.){vec2 dirVect=d-e;vec2 normal=normalize(vec2(dirVect.y,-dirVect.x));offset=c*normal*g;if((h==3.&&(m==7.||m==9.))||(h==1.&&(m==8.||m==9.))){b=projPos+j*vec4(0.,0.,0.,0.);}} else if(h==5.||h==6.){vec2 dirVect=f-e;vec2 tmpNormal=normalize(vec2(-dirVect.y,dirVect.x));vec2 tangent=normalize(normalize(f-e)+normalize(e-d));vec2 normal=vec2(tangent.y,-tangent.x);float miterLength=c/dot(normal,tmpNormal);if(h==6.){if(m==7.||m==9.){offset=normal*g*miterLength;a=1.0;b=projPos+j*vec4(0.,0.,0.,0.);}else if(miterLength>l){offset=tmpNormal*g*c;}else{offset=normal*g*miterLength;}} else if(h==5.){offset=normal*g*miterLength;vec4 defaultOffset=j*vec4(0.,0.,0.,0.);vec4 firstProjPos=i*vec4(d,0.,1.)+defaultOffset;vec4 secondProjPos=projPos+defaultOffset;vec4 thirdProjPos=i*vec4(f,0.,1.)+defaultOffset;float firstSegLength=distance(secondProjPos.xy,firstProjPos.xy);float secondSegLength=distance(thirdProjPos.xy,secondProjPos.xy);float miterSegLength=distance(secondProjPos.xy,vec4(projPos+j*vec4(offset,0.,0.)).xy);if(miterSegLength>min(firstSegLength,secondSegLength)){if(firstSegLength<secondSegLength){dirVect=d-e;tmpNormal=normalize(vec2(dirVect.y,-dirVect.x));projPos=firstProjPos-defaultOffset;}else{projPos=thirdProjPos-defaultOffset;}offset=tmpNormal*g*c;}}}else if(h==2.){vec2 dirVect=e-f;vec2 firstNormal=normalize(dirVect);vec2 secondNormal=vec2(firstNormal.y*g,-firstNormal.x*g);vec2 hypotenuse=normalize(firstNormal-secondNormal);vec2 normal=vec2(hypotenuse.y*g,-hypotenuse.x*g);float length=sqrt(c*c*2.0);offset=normal*length;if(m==8.||m==9.){a=1.0;b=projPos+j*vec4(0.,0.,0.,0.);}} else if(h==10.){vec2 dirVect=e-d;vec2 firstNormal=normalize(dirVect);vec2 secondNormal=vec2(-firstNormal.y*g,firstNormal.x*g);vec2 hypotenuse=normalize(firstNormal-secondNormal);vec2 normal=vec2(-hypotenuse.y*g,hypotenuse.x*g);float length=sqrt(c*c*2.0);offset=normal*length;if(m==8.||m==9.){a=1.0;b=projPos+j*vec4(0.,0.,0.,0.);}} vec4 offsets=j*vec4(offset,0.,0.);gl_Position=projPos+offsets;}'; ol.render.webgl.linestringreplay.shader.DefaultVertex.OPTIMIZED_SOURCE = 'varying float a;varying vec4 b;varying float c;attribute vec2 d;attribute vec2 e;attribute vec2 f;attribute float g;uniform mat4 h;uniform mat4 i;uniform float j;uniform float k;void main(void){c=j/2.0;vec2 offset;a=0.0;float direction=g/abs(g);vec4 projPos=h*vec4(e,0.,1.);if(mod(g,3.0)==0.0||mod(g,17.0)==0.0){vec2 dirVect=f-e;vec2 normal=normalize(vec2(-dirVect.y,dirVect.x));offset=c*normal*direction;if(mod(g,2.0)==0.0){b=projPos+i*vec4(0.,0.,0.,0.);}} else if(mod(g,5.0)==0.0||mod(g,13.0)==0.0){vec2 dirVect=d-e;vec2 normal=normalize(vec2(dirVect.y,-dirVect.x));offset=c*normal*direction;if(mod(g,2.0)==0.0){b=projPos+i*vec4(0.,0.,0.,0.);}} else if(mod(g,19.0)==0.0||mod(g,23.0)==0.0){vec2 dirVect=f-e;vec2 tmpNormal=normalize(vec2(-dirVect.y,dirVect.x));vec2 tangent=normalize(normalize(f-e)+normalize(e-d));vec2 normal=vec2(tangent.y,-tangent.x);float miterLength=c/dot(normal,tmpNormal);if(mod(g,23.0)==0.0){if(mod(g,2.0)==0.0){offset=normal*direction*miterLength;a=1.0;b=projPos+i*vec4(0.,0.,0.,0.);}else if(miterLength>k){offset=tmpNormal*direction*c;}else{offset=normal*direction*miterLength;}} else{offset=normal*direction*miterLength;vec4 defaultOffset=i*vec4(0.,0.,0.,0.);vec4 firstProjPos=h*vec4(d,0.,1.)+defaultOffset;vec4 secondProjPos=projPos+defaultOffset;vec4 thirdProjPos=h*vec4(f,0.,1.)+defaultOffset;float firstSegLength=distance(secondProjPos.xy,firstProjPos.xy);float secondSegLength=distance(thirdProjPos.xy,secondProjPos.xy);float miterSegLength=distance(secondProjPos.xy,vec4(projPos+i*vec4(offset,0.,0.)).xy);if(miterSegLength>min(firstSegLength,secondSegLength)){if(firstSegLength<secondSegLength){dirVect=d-e;tmpNormal=normalize(vec2(dirVect.y,-dirVect.x));projPos=firstProjPos-defaultOffset;}else{projPos=thirdProjPos-defaultOffset;}offset=tmpNormal*direction*c;}}}else if(mod(g,7.0)==0.0||mod(g,11.0)==0.0){vec2 normal;if(mod(g,7.0)==0.0){vec2 dirVect=e-f;vec2 firstNormal=normalize(dirVect);vec2 secondNormal=vec2(firstNormal.y*direction,-firstNormal.x*direction);vec2 hypotenuse=normalize(firstNormal-secondNormal);normal=vec2(hypotenuse.y*direction,-hypotenuse.x*direction);}else{vec2 dirVect=e-d;vec2 firstNormal=normalize(dirVect);vec2 secondNormal=vec2(-firstNormal.y*direction,firstNormal.x*direction);vec2 hypotenuse=normalize(firstNormal-secondNormal);normal=vec2(-hypotenuse.y*direction,hypotenuse.x*direction);}float length=sqrt(c*c*2.0);offset=normal*length;if(mod(g,2.0)==0.0){a=1.0;b=projPos+i*vec4(0.,0.,0.,0.);}} vec4 offsets=i*vec4(offset,0.,0.);gl_Position=projPos+offsets;}';
/** /**
@@ -89,49 +89,43 @@ ol.render.webgl.linestringreplay.shader.Default.Locations = function(gl, program
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_color = gl.getUniformLocation( this.u_color = gl.getUniformLocation(
program, goog.DEBUG ? 'u_color' : 'o'); program, goog.DEBUG ? 'u_color' : 'm');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_lineWidth = gl.getUniformLocation( this.u_lineWidth = gl.getUniformLocation(
program, goog.DEBUG ? 'u_lineWidth' : 'k'); program, goog.DEBUG ? 'u_lineWidth' : 'j');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_miterLimit = gl.getUniformLocation( this.u_miterLimit = gl.getUniformLocation(
program, goog.DEBUG ? 'u_miterLimit' : 'l'); program, goog.DEBUG ? 'u_miterLimit' : 'k');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_offsetScaleMatrix = gl.getUniformLocation( this.u_offsetScaleMatrix = gl.getUniformLocation(
program, goog.DEBUG ? 'u_offsetScaleMatrix' : 'j'); program, goog.DEBUG ? 'u_offsetScaleMatrix' : 'i');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_opacity = gl.getUniformLocation( this.u_opacity = gl.getUniformLocation(
program, goog.DEBUG ? 'u_opacity' : 'n'); program, goog.DEBUG ? 'u_opacity' : 'l');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_projectionMatrix = gl.getUniformLocation( this.u_projectionMatrix = gl.getUniformLocation(
program, goog.DEBUG ? 'u_projectionMatrix' : 'i'); program, goog.DEBUG ? 'u_projectionMatrix' : 'h');
/**
* @type {WebGLUniformLocation}
*/
this.u_round = gl.getUniformLocation(
program, goog.DEBUG ? 'u_round' : 'm');
/** /**
* @type {WebGLUniformLocation} * @type {WebGLUniformLocation}
*/ */
this.u_size = gl.getUniformLocation( this.u_size = gl.getUniformLocation(
program, goog.DEBUG ? 'u_size' : 'p'); program, goog.DEBUG ? 'u_size' : 'n');
/** /**
* @type {number} * @type {number}
@@ -139,12 +133,6 @@ ol.render.webgl.linestringreplay.shader.Default.Locations = function(gl, program
this.a_direction = gl.getAttribLocation( this.a_direction = gl.getAttribLocation(
program, goog.DEBUG ? 'a_direction' : 'g'); program, goog.DEBUG ? 'a_direction' : 'g');
/**
* @type {number}
*/
this.a_instruction = gl.getAttribLocation(
program, goog.DEBUG ? 'a_instruction' : 'h');
/** /**
* @type {number} * @type {number}
*/ */