1
// expected #define: float pointCount
3
uniform sampler2D points;
5
uniform mat4 projectionMatrix2;
6
uniform mat4 viewMatrix2;
7
//we receive the scale either by distanceScale or by the textureScale
8
//if distanceScale is 0.0 then we go fetch it in the texture
9
uniform float distanceScale;
10
uniform sampler2D textureScale;
17
vec3 FakeEncodeFloatRGB(highp float v) {
18
return vec3(v, 0.0, 0.0);
21
float myDist(vec2 p1, vec2 p2) {
23
return dot(diff, diff);
25
vec2 getPoint(int index) {
26
vec4 coords = vec4(texture2D(points, vec2((0.5 + float(index)) / float(pointCount), 0.5)).ra, 0.0, 1.0);
27
// pass it though the camera matrix
28
return vec2(0.5, 0.5) + vec2(0.5, 0.5) * (projectionMatrix2 * viewMatrix2 * coords).xy;
30
//http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
31
highp float lineDist(vec2 p, vec2 a, vec2 b) {
32
highp vec2 pa = p - a, ba = b - a;
33
highp float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
34
return myDist(pa, ba * h) / readScale;
38
readScale = distanceScale == 0.0 ? texture2D(textureScale, vec2(0.5, 0.5)).r : distanceScale;
39
highp float dist = myDist(getPoint(0), vUv.xy) / readScale;
40
float intersectionCount = 0.0;
41
for (int i = 1; i < pointCount; i++) {
42
vec2 prev = getPoint(i - 1);
43
vec2 curr = getPoint(i);
44
dist = min(dist, lineDist(vUv, prev, curr));
46
float xi = curr.x, yi = curr.y;
47
float xj = prev.x, yj = prev.y;
48
//https://github.com/substack/point-in-polygon/blob/master/index.js
49
//http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
50
if ((yi > vUv.y != yj > vUv.y ) && (vUv.x < (xj - xi) * (vUv.y - yi) / (yj - yi) + xi))
53
bool inside = mod(intersectionCount, 2.0) == 1.0;
56
gl_FragColor = vec4(dist, 0.0, 0.0, 1.0);