playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
3 lines (2 loc) • 3.04 kB
TypeScript
declare const _default: "\nuniform sampler2D texture_msdfMap;\n\nfloat median(float r, float g, float b) {\n return max(min(r, g), min(max(r, g), b));\n}\n\nuniform float font_sdfIntensity; // intensity is used to boost the value read from the SDF, 0 is no boost, 1.0 is max boost\nuniform float font_pxrange; // number of texels of SDF spread (inside <-> outside) in the atlas\n\n#ifndef LIT_MSDF_TEXT_ATTRIBUTE\n uniform vec4 outline_color;\n uniform float outline_thickness;\n uniform vec4 shadow_color;\n uniform vec2 shadow_offset;\n#else\n varying vec4 outline_color;\n varying float outline_thickness;\n varying vec4 shadow_color;\n varying vec2 shadow_offset;\n#endif\n\nvec4 applyMsdf(vec4 color) {\n\n // Convert to linear space before processing\n // TODO: ideally this would receive the color in linear space, but that would require larger changes\n // on the engine side, with the way premultiplied alpha is handled as well.\n color.rgb = gammaCorrectInput(color.rgb);\n\n // sample the field\n vec3 tsample = texture2D(texture_msdfMap, vUv0).rgb;\n vec2 uvShdw = vUv0 - shadow_offset;\n vec3 ssample = texture2D(texture_msdfMap, uvShdw).rgb;\n\n // get the signed distance value\n float sigDist = median(tsample.r, tsample.g, tsample.b);\n float sigDistShdw = median(ssample.r, ssample.g, ssample.b);\n\n // coverage threshold (0.5 = glyph edge); font_sdfIntensity fattens the glyph\n float edge = 0.5 - 0.5 * font_sdfIntensity;\n\n // Width of the distance-field transition in screen pixels, from the uv magnification (both\n // axes) and the atlas spread. Stable under motion and minification, unlike fwidth(sigDist)\n // whose noise on undersampled glyphs makes small text shimmer. Floored at 2.5 so the\n // minified coverage ramp spans at most 0.4 of the field's range ([edge - 0.2, edge + 0.2]):\n // a lower floor lets the ramp (and its outline/shadow-shifted copies) reach the field's far\n // tail, which washes translucent outline across the whole glyph quad and hazes distant text.\n vec2 unitRange = vec2(font_pxrange) / vec2(textureSize(texture_msdfMap, 0));\n float screenPxRange = max(0.5 * dot(unitRange, 1.0 / max(fwidth(vUv0), vec2(1e-6))), 2.5);\n\n float inside = clamp(screenPxRange * (sigDist - edge) + 0.5, 0.0, 1.0);\n float outline = clamp(screenPxRange * (sigDist + outline_thickness - edge) + 0.5, 0.0, 1.0);\n float shadow = clamp(screenPxRange * (sigDistShdw + outline_thickness - edge) + 0.5, 0.0, 1.0);\n\n vec4 tcolor = (outline > inside) ? outline * vec4(outline_color.a * outline_color.rgb, outline_color.a) : vec4(0.0);\n tcolor = mix(tcolor, color, inside);\n\n vec4 scolor = (shadow > outline) ? shadow * vec4(shadow_color.a * shadow_color.rgb, shadow_color.a) : tcolor;\n tcolor = mix(scolor, tcolor, outline);\n\n // Convert back to gamma space before returning\n tcolor.rgb = gammaCorrectOutput(tcolor.rgb);\n \n return tcolor;\n}\n";
export default _default;