webgcode

Форк
0
/
distance.frag 
57 строк · 1.9 Кб
1
// expected #define: float pointCount
2

3
uniform sampler2D points;
4
uniform vec2 gridSize;
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;
11

12
varying vec2 vUv;
13

14
float readScale;
15

16
/*INCLUDE_FRAGLIB*/
17
vec3 FakeEncodeFloatRGB(highp float v) {
18
    return vec3(v, 0.0, 0.0);
19
}
20

21
float myDist(vec2 p1, vec2 p2) {
22
    vec2 diff = p1 - p2;
23
    return dot(diff, diff);
24
}
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;
29
}
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;
35
}
36

37
void main() {
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));
45

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))
51
            intersectionCount++;
52
    }
53
    bool inside = mod(intersectionCount, 2.0) == 1.0;
54
    if (!inside)
55
        discard;
56
    gl_FragColor = vec4(dist, 0.0, 0.0, 1.0);
57
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.