diff --git a/src/ol/render/webgl/webgllinestringdefault.glsl b/src/ol/render/webgl/webgllinestringdefault.glsl index 635d7856b4..fbf287894b 100644 --- a/src/ol/render/webgl/webgllinestringdefault.glsl +++ b/src/ol/render/webgl/webgllinestringdefault.glsl @@ -20,6 +20,7 @@ uniform float u_lineWidth; uniform float u_miterLimit; void main(void) { + bool degenerate = false; v_halfWidth = u_lineWidth / 2.0; vec2 offset; v_round = 0.0; @@ -44,7 +45,7 @@ void main(void) { vec2 tmpNormal = normalize(vec2(-dirVect.y, dirVect.x)); vec2 tangent = normalize(normalize(a_nextPos - a_position) + normalize(a_position - a_lastPos)); vec2 normal = vec2(-tangent.y, tangent.x); - float miterLength = v_halfWidth / dot(normal, tmpNormal); + float miterLength = abs(v_halfWidth / dot(normal, tmpNormal)); if (mod(a_direction, 23.0) == 0.0) { offset = normal * direction * miterLength; if (mod(a_direction, 2.0) == 0.0) { @@ -54,24 +55,36 @@ void main(void) { offset = tmpNormal * direction * v_halfWidth; } } else { - offset = normal * direction * miterLength; - vec4 defaultOffset = u_offsetScaleMatrix * vec4(0., 0., 0., 0.); - vec4 firstProjPos = u_projectionMatrix * vec4(a_lastPos, 0., 1.) + defaultOffset; - vec4 secondProjPos = projPos + defaultOffset; - vec4 thirdProjPos = u_projectionMatrix * vec4(a_nextPos, 0., 1.) + defaultOffset; - float firstSegLength = distance(secondProjPos.xy, firstProjPos.xy); - float secondSegLength = distance(thirdProjPos.xy, secondProjPos.xy); - float miterSegLength = distance(secondProjPos.xy, vec4(projPos + u_offsetScaleMatrix * vec4(offset, 0., 0.)).xy); - //TODO: Write a more accurate method for identifying sharp angles. - if (miterSegLength > min(firstSegLength, secondSegLength)) { - if (firstSegLength < secondSegLength) { - dirVect = a_lastPos - a_position; - tmpNormal = normalize(vec2(dirVect.y, -dirVect.x)); - projPos = firstProjPos - defaultOffset; - } else { - projPos = thirdProjPos - defaultOffset; - } - offset = tmpNormal * direction * v_halfWidth; + dirVect = a_lastPos - a_position; + vec2 longOffset, shortOffset, longVertex; + vec4 shortProjVertex; + if (length(a_nextPos - a_position) > length(a_lastPos - a_position)) { + longOffset = tmpNormal * direction * v_halfWidth; + shortOffset = normalize(vec2(dirVect.y, -dirVect.x)) * direction * v_halfWidth; + longVertex = a_nextPos; + shortProjVertex = u_projectionMatrix * vec4(a_lastPos, 0., 1.); + } else { + shortOffset = tmpNormal * direction * v_halfWidth; + longOffset = normalize(vec2(dirVect.y, -dirVect.x)) * direction * v_halfWidth; + longVertex = a_lastPos; + shortProjVertex = u_projectionMatrix * vec4(a_nextPos, 0., 1.); + } + //Intersection algorithm based on theory by Paul Bourke (http://paulbourke.net/geometry/pointlineplane/). + vec4 p1 = u_projectionMatrix * vec4(longVertex, 0., 1.) + u_offsetScaleMatrix * vec4(longOffset, 0., 0.); + vec4 p2 = projPos + u_offsetScaleMatrix * vec4(longOffset, 0., 0.); + vec4 p3 = shortProjVertex + u_offsetScaleMatrix * vec4(-shortOffset, 0., 0.); + vec4 p4 = shortProjVertex + u_offsetScaleMatrix * vec4(shortOffset, 0., 0.); + float denom = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y); + float epsilon = 0.000000000001; + float firstU = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denom; + float secondU = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denom; + if (firstU > epsilon && firstU < 1.0 - epsilon && secondU > epsilon && secondU < 1.0 - epsilon) { + gl_Position = shortProjVertex; + gl_Position.x = p1.x + firstU * (p2.x - p1.x); + gl_Position.y = p1.y + firstU * (p2.y - p1.y); + degenerate = true; + } else { + offset = normal * direction * miterLength; } } } else if (mod(a_direction, 7.0) == 0.0 || mod(a_direction, 11.0) == 0.0) { @@ -96,8 +109,10 @@ void main(void) { v_roundVertex = projPos + u_offsetScaleMatrix * vec4(0., 0., 0., 0.); } } - vec4 offsets = u_offsetScaleMatrix * vec4(offset, 0., 0.); - gl_Position = projPos + offsets; + if (!degenerate) { + vec4 offsets = u_offsetScaleMatrix * vec4(offset, 0., 0.); + gl_Position = projPos + offsets; + } } diff --git a/src/ol/render/webgl/webgllinestringdefaultshader.js b/src/ol/render/webgl/webgllinestringdefaultshader.js index 9e9def9550..e69ebcb62b 100644 --- a/src/ol/render/webgl/webgllinestringdefaultshader.js +++ b/src/ol/render/webgl/webgllinestringdefaultshader.js @@ -58,14 +58,14 @@ goog.addSingletonGetter(ol.render.webgl.linestringreplay.shader.DefaultVertex); * @const * @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;\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 offset = normal * direction * miterLength;\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 } else if (miterLength > u_miterLimit) {\n offset = tmpNormal * direction * v_halfWidth;\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'; +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 bool degenerate = false;\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 = abs(v_halfWidth / dot(normal, tmpNormal));\n if (mod(a_direction, 23.0) == 0.0) {\n offset = normal * direction * miterLength;\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 } else if (miterLength > u_miterLimit) {\n offset = tmpNormal * direction * v_halfWidth;\n }\n } else {\n dirVect = a_lastPos - a_position;\n vec2 longOffset, shortOffset, longVertex;\n vec4 shortProjVertex;\n if (length(a_nextPos - a_position) > length(a_lastPos - a_position)) {\n longOffset = tmpNormal * direction * v_halfWidth;\n shortOffset = normalize(vec2(dirVect.y, -dirVect.x)) * direction * v_halfWidth;\n longVertex = a_nextPos;\n shortProjVertex = u_projectionMatrix * vec4(a_lastPos, 0., 1.);\n } else {\n shortOffset = tmpNormal * direction * v_halfWidth;\n longOffset = normalize(vec2(dirVect.y, -dirVect.x)) * direction * v_halfWidth;\n longVertex = a_lastPos;\n shortProjVertex = u_projectionMatrix * vec4(a_nextPos, 0., 1.);\n }\n //Intersection algorithm based on theory by Paul Bourke (http://paulbourke.net/geometry/pointlineplane/).\n vec4 p1 = u_projectionMatrix * vec4(longVertex, 0., 1.) + u_offsetScaleMatrix * vec4(longOffset, 0., 0.);\n vec4 p2 = projPos + u_offsetScaleMatrix * vec4(longOffset, 0., 0.);\n vec4 p3 = shortProjVertex + u_offsetScaleMatrix * vec4(-shortOffset, 0., 0.);\n vec4 p4 = shortProjVertex + u_offsetScaleMatrix * vec4(shortOffset, 0., 0.);\n float denom = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);\n float epsilon = 0.000000000001;\n float firstU = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / denom;\n float secondU = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / denom;\n if (firstU > epsilon && firstU < 1.0 - epsilon && secondU > epsilon && secondU < 1.0 - epsilon) {\n gl_Position = shortProjVertex;\n gl_Position.x = p1.x + firstU * (p2.x - p1.x);\n gl_Position.y = p1.y + firstU * (p2.y - p1.y);\n degenerate = true;\n } else {\n offset = normal * direction * miterLength;\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 if (!degenerate) {\n vec4 offsets = u_offsetScaleMatrix * vec4(offset, 0., 0.);\n gl_Position = projPos + offsets;\n }\n}\n\n\n'; /** * @const * @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;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){offset=normal*direction*miterLength;if(mod(g,2.0)==0.0){a=1.0;b=projPos+i*vec4(0.,0.,0.,0.);}else if(miterLength>k){offset=tmpNormal*direction*c;}} 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