webgl2-sdf 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +56 -45
  2. package/browser/index.min.js +1 -1
  3. package/node/debug-shaders.d.ts +9 -0
  4. package/node/debug-shaders.js +28 -0
  5. package/node/debug-shaders.js.map +1 -0
  6. package/node/generate-sdf.d.ts +1 -1
  7. package/node/generate-sdf.js +5 -10
  8. package/node/generate-sdf.js.map +1 -1
  9. package/node/main-program.d.ts +1 -1
  10. package/node/main-program.js +16 -17
  11. package/node/main-program.js.map +1 -1
  12. package/node/prepare-buffers.js +21 -18
  13. package/node/prepare-buffers.js.map +1 -1
  14. package/node/shaders/fragment.js +28 -20
  15. package/node/shaders/fragment.js.map +1 -1
  16. package/node/shaders/vertex.d.ts +1 -1
  17. package/node/shaders/vertex.js +1 -5
  18. package/node/shaders/vertex.js.map +1 -1
  19. package/node/svg/get-paths-from-str.d.ts +1 -0
  20. package/node/svg/get-paths-from-str.js +1 -0
  21. package/node/svg/get-paths-from-str.js.map +1 -1
  22. package/node/svg/path-data-polyfill/source.d.ts +0 -1
  23. package/node/svg/path-data-polyfill/source.js +0 -2
  24. package/node/svg/path-data-polyfill/source.js.map +1 -1
  25. package/node/svg/path-state.d.ts +0 -1
  26. package/node/types/gl-context.d.ts +1 -1
  27. package/node/webgl-utils/get-web-gl-context.js +7 -14
  28. package/node/webgl-utils/get-web-gl-context.js.map +1 -1
  29. package/package.json +1 -1
  30. package/src/debug-shaders.ts +4 -4
  31. package/src/generate-sdf.ts +12 -18
  32. package/src/main-program.ts +26 -21
  33. package/src/prepare-buffers.ts +24 -21
  34. package/src/shaders/fragment.ts +28 -20
  35. package/src/shaders/vertex.ts +1 -5
  36. package/src/svg/get-paths-from-str.ts +1 -0
  37. package/src/svg/path-data-polyfill/source.ts +0 -2
  38. package/src/svg/path-state.ts +0 -1
  39. package/src/types/gl-context.ts +1 -1
  40. package/src/webgl-utils/get-web-gl-context.ts +10 -18
@@ -17,7 +17,6 @@ const main_Fragment =
17
17
  precision highp float;
18
18
 
19
19
  uniform float uMaxDistance;
20
- uniform float uExponent;
21
20
  uniform highp sampler2D uSegs;
22
21
  uniform highp isampler2D uCloseCellIdxs;
23
22
  uniform highp isampler2D uCrossCellIdxs;
@@ -41,7 +40,7 @@ float absDistToSegment(vec2 point, vec2 lineA, vec2 lineB) {
41
40
  vec2 lineDir = lineB - lineA;
42
41
  float lenSq = dot(lineDir, lineDir);
43
42
  float t = clamp(dot(point - lineA, lineDir) / lenSq, 0.0, 1.0);
44
- vec2 linePt = lineA + t * lineDir;
43
+ vec2 linePt = lineA + t*lineDir;
45
44
 
46
45
  return distance(point, linePt);
47
46
  }
@@ -60,15 +59,16 @@ void main() {
60
59
  for (int i = crossIdxS; i < crossIdxS + crossLen; i++) {
61
60
  int crossIdx = texelFetch(uCrossCellIdxs, ivec2(i%256, i/256), 0).x;
62
61
 
63
- bool isEven = crossIdx % 2 == 0;
62
+ ivec2 uSegIdxRange = crossIdx % 2 == 0
63
+ ? uSegIdxRangePerCell[crossIdx / 2].xy
64
+ : uSegIdxRangePerCell[crossIdx / 2].zw;
64
65
 
65
- ivec4 uSegIdxRange = uSegIdxRangePerCell[crossIdx / 2];
66
- int segIdx = isEven ? uSegIdxRange.x : uSegIdxRange.z;
67
- int segLen = isEven ? uSegIdxRange.y : uSegIdxRange.w;
66
+ int segIdx = uSegIdxRange.x;
67
+ int segLen = uSegIdxRange.y;
68
68
 
69
69
  for (int j = segIdx; j < segIdx + segLen; j++) {
70
70
  // Fetch segment from texture
71
- vec4 seg = texelFetch(uSegs, ivec2(j, 0), 0);
71
+ vec4 seg = texelFetch(uSegs, ivec2(j%256, j/256), 0);
72
72
 
73
73
  // line segment's min-y is excluded
74
74
  bool crossing =
@@ -82,15 +82,18 @@ void main() {
82
82
  }
83
83
 
84
84
  {
85
- bool isEven = (instanceId % ${ROW_COUNT}) % 2 == 0;
85
+ int cellIdx = (instanceId % ${ROW_COUNT});
86
+
87
+ bool isEven = cellIdx % 2 == 0;
86
88
 
87
- ivec4 uSegIdxRange = uSegIdxRangePerStrip[(instanceId % ${ROW_COUNT}) / 2];
89
+ ivec4 uSegIdxRange = uSegIdxRangePerStrip[cellIdx / 2];
88
90
  int segIdx = isEven ? uSegIdxRange.x : uSegIdxRange.z;
89
91
  int segLen = isEven ? uSegIdxRange.y : uSegIdxRange.w;
92
+
90
93
 
91
94
  for (int j = segIdx; j < segIdx + segLen; j++) {
92
95
  // Fetch segment from texture
93
- vec4 seg = texelFetch(uSegs, ivec2(j, 0), 0);
96
+ vec4 seg = texelFetch(uSegs, ivec2(j%256, j/256), 0);
94
97
 
95
98
  // line segment's min-y is excluded
96
99
  bool crossing =
@@ -117,20 +120,22 @@ void main() {
117
120
  for (int i = cellIdxS; i < cellIdxS + cellLen; i++) {
118
121
  int cellIdx = texelFetch(uCloseCellIdxs, ivec2(i%256, i/256), 0).x;
119
122
 
120
- bool isEven = cellIdx % 2 == 0;
121
- ivec4 uSegIdxRange = uSegIdxRangePerCell[cellIdx / 2];
122
- int segIdx = isEven ? uSegIdxRange.x : uSegIdxRange.z;
123
- int segLen = isEven ? uSegIdxRange.y : uSegIdxRange.w;
123
+ ivec2 uSegIdxRange = cellIdx % 2 == 0
124
+ ? uSegIdxRangePerCell[cellIdx / 2].xy
125
+ : uSegIdxRangePerCell[cellIdx / 2].zw;
126
+
127
+ int segIdx = uSegIdxRange.x;
128
+ int segLen = uSegIdxRange.y;
124
129
 
125
130
  for (int j = segIdx; j < segIdx + segLen; j++) {
126
131
  // Fetch segment from texture
127
- vec4 seg = texelFetch(uSegs, ivec2(j, 0), 0);
132
+ vec4 seg = texelFetch(uSegs, ivec2(j%256, j/256), 0);
128
133
 
129
134
  // Find unsigned distance to the segment; only the nearest will be kept
130
135
  float d = absDistToSegment(vXY, seg.xy, seg.zw);
131
- // Apply exponential transform TODO
132
- // val = pow(1.0 - clamp(d / uMaxDistance, 0.0, 1.0), uExponent) * 0.5;
136
+ // Apply exponential transform
133
137
  float val = clamp(d / uMaxDistance, 0.0, 1.0);
138
+
134
139
  res = min(res, val);
135
140
  }
136
141
  }
@@ -139,12 +144,15 @@ void main() {
139
144
 
140
145
  // DEBUG!
141
146
  // float alpha = ((instanceId + instanceId/${ROW_COUNT}) % 2 == 0 ? 0.3 : 0.5);
142
- float alpha = res == 1.0 ? 0.0 : 0.5;
143
147
 
148
+
149
+ float exponent = 2;
150
+ res = pow(1.0 - val, exponent) * 0.5;
151
+
152
+ float alpha = res == 1.0 ? 0.0 : 0.5;
144
153
  float red = inside ? 0.2 : 0.8;
145
- float green = abs(sin(50.0 * res));
154
+ float green = abs(sin(25.0 * res));
146
155
  float blue = 0.5;
147
- // float alpha = inside ? 0.5 : 0.0;
148
156
 
149
157
  FragColor = vec4(red, green, blue, alpha);
150
158
  }
@@ -5,7 +5,6 @@ const vertex = /*glsl*/`#version 300 es
5
5
  precision highp float;
6
6
 
7
7
  uniform vec2 uWidthHeight;
8
- uniform vec2 uOffset;
9
8
  in vec2 aUV;
10
9
  in ivec2 aCloseCellIdxRangePerCell;
11
10
  in ivec2 aCrossIdxRangePerCell;
@@ -41,14 +40,11 @@ void main() {
41
40
 
42
41
  float aspectRatio = width / height;
43
42
 
44
- float x = (uOffset.x / 2.0*width) - 1.0;
45
- float y = (uOffset.y / 2.0*height) - 1.0;
46
-
47
43
  gl_Position = vec4(
48
44
  vec2(
49
45
  (2.0*(uv.x / aspectRatio) - 1.0),
50
46
  (2.0*uv.y - 1.0)
51
- ) + vec2(x,y),
47
+ ),
52
48
  0.0, 1.0
53
49
  );
54
50
  }
@@ -7,6 +7,7 @@ import { parsePathDataString } from './path-data-polyfill/parse-path-data-string
7
7
  * and each bezier in turn consisting of an array of control points from the
8
8
  * given SVG path string. An array of loops are returned (as opposed to a single
9
9
  * loop) since an SVG path may have sub-paths.
10
+ *
10
11
  * @param str The SVG path string, e.g. 'M1 1 C 5 1 5 2 4 2 C 3 3 1 3 1 1 z'
11
12
  */
12
13
  function getPathsFromStr(str: string): number[][][][] {
@@ -1,14 +1,12 @@
1
1
  import { parseNumber } from './parse-number.js';
2
2
 
3
3
 
4
- /** @hidden */
5
4
  const COMMAND_MAP: { [index:string]: string } = {
6
5
  Z:"Z", M:"M", L:"L", C:"C", Q:"Q", A:"A", H:"H", V:"V", S:"S", T:"T",
7
6
  z:"Z", m:"m", l:"l", c:"c", q:"q", a:"a", h:"h", v:"v", s:"s", t:"t"
8
7
  };
9
8
 
10
9
 
11
- /** @hidden */
12
10
  class Source {
13
11
  _string: string;
14
12
  _currentIndex: number;
@@ -1,5 +1,4 @@
1
1
 
2
- /** @hidden */
3
2
  interface PathState {
4
3
  initialPoint?: number[] | undefined;
5
4
  p: number[];
@@ -20,7 +20,7 @@ interface GlContext {
20
20
  readonly gl: WebGL2RenderingContext;
21
21
  readonly textures: { [index:string]: Texture };
22
22
  readonly programs: { [index:string]: Program };
23
- readonly onContextLoss: () => void;
23
+ onContextLoss?: (event: Event) => void;
24
24
  }
25
25
 
26
26
 
@@ -24,29 +24,21 @@ function getWebGlContext(
24
24
  const programs: { [index:string]: Program } = {};
25
25
  const textures: { [index:string]: Texture } = {};
26
26
 
27
- gl.canvas.addEventListener('webglcontextlost', e => {
28
- onContextLoss();
29
- e.preventDefault();
30
- }, false);
31
-
32
- const glContext: GlContext = {
33
- gl,
34
- onContextLoss,
35
- textures,
36
- programs
37
- };
27
+ const glContext: GlContext = { gl, textures, programs };
38
28
 
39
- cache.set(gl, glContext);
29
+ gl.canvas.addEventListener('webglcontextlost', event => {
30
+ // event.preventDefault(); // Prevent the default action (which is to not restore automatically)
40
31
 
41
- return glContext;
32
+ deleteAllProps(programs);
33
+ deleteAllProps(textures);
34
+ cache.delete(gl);
42
35
 
36
+ glContext.onContextLoss?.(event);
37
+ }, false);
43
38
 
44
- ////////////////////////
39
+ cache.set(gl, glContext);
45
40
 
46
- function onContextLoss() {
47
- deleteAllProps(programs);
48
- deleteAllProps(textures);
49
- }
41
+ return glContext;
50
42
  }
51
43
 
52
44