@multiplekex/shallot 0.2.2 → 0.2.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@multiplekex/shallot",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -28,7 +28,7 @@ import { Transform } from "../../standard/transforms";
28
28
  const MAX_GLYPHS = 50000;
29
29
  const GLYPH_FLOATS = 16;
30
30
  const SDF_SIZE = 96;
31
- const SDF_EXPONENT = 6;
31
+ const SDF_EXPONENT = 9;
32
32
  const fontUrls: string[] = [];
33
33
  const loadedFonts: (Font | null)[] = [];
34
34
 
@@ -498,12 +498,27 @@ struct FragmentOutput {
498
498
  @location(1) mask: f32,
499
499
  }
500
500
 
501
+ const SDF_EXPONENT: f32 = 9.0;
502
+
503
+ fn sdfToSignedDistance(sdfValue: f32, maxDimension: f32) -> f32 {
504
+ let alpha = select(sdfValue, 1.0 - sdfValue, sdfValue > 0.5);
505
+ let absDist = (1.0 - pow(2.0 * alpha, 1.0 / SDF_EXPONENT)) * maxDimension;
506
+ return absDist * select(1.0, -1.0, sdfValue > 0.5);
507
+ }
508
+
501
509
  @fragment
502
510
  fn fs(input: VertexOutput) -> FragmentOutput {
503
511
  let sdfValue = textureSample(atlasTexture, atlasSampler, input.uv).r;
504
512
 
505
- let aaWidth = fwidth(sdfValue) * 0.707;
506
- let alpha = smoothstep(0.5 - aaWidth, 0.5 + aaWidth, sdfValue);
513
+ // Decode SDF to signed distance (negative = inside, positive = outside)
514
+ let maxDimension = max(input.glyphDimensions.x, input.glyphDimensions.y);
515
+ let signedDist = sdfToSignedDistance(sdfValue, maxDimension);
516
+
517
+ // Screen-space AA distance from UV derivatives
518
+ let aaDist = length(fwidth(input.localUV * input.glyphDimensions)) * 0.5;
519
+
520
+ // Smoothstep with adaptive band
521
+ let alpha = smoothstep(aaDist, -aaDist, signedDist);
507
522
 
508
523
  if alpha < 0.01 {
509
524
  discard;