diff --git a/etc/shaders.h b/etc/shaders.h index 908338e1..608a0aad 100644 --- a/etc/shaders.h +++ b/etc/shaders.h @@ -1,6 +1,8 @@ #include "shaders/unlit.vert.h" #include "shaders/unlit.frag.h" +#include "shaders/font.frag.h" + #include "shaders/lovr.glsl.h" #define LOCATION_POSITION 10 diff --git a/etc/shaders/font.frag b/etc/shaders/font.frag new file mode 100644 index 00000000..29776687 --- /dev/null +++ b/etc/shaders/font.frag @@ -0,0 +1,24 @@ +#version 460 +#extension GL_EXT_multiview : require +#extension GL_GOOGLE_include_directive : require +#extension GL_EXT_samplerless_texture_functions : require + +#include "lovr.glsl" + +float screenPxRange() { + vec2 screenTexSize = vec2(1.) / fwidth(FragUV); + return max(.5 * dot(Material.sdfRange, screenTexSize), 1.); +} + +float median(float r, float g, float b) { + return max(min(r, g), min(max(r, g), b)); +} + +void main() { + vec3 msdf = texture(sampler2D(Texture, Sampler), FragUV).rgb; + float sdf = median(msdf.r, msdf.g, msdf.b); + float screenPxDistance = screenPxRange() * (sdf - .5); + float alpha = clamp(screenPxDistance + .5, 0., 1.); + if (alpha <= 0.) discard; + PixelColors[0] = vec4(FragColor.rgb, alpha); +} diff --git a/etc/shaders/lovr.glsl b/etc/shaders/lovr.glsl index d915182a..eed3319e 100644 --- a/etc/shaders/lovr.glsl +++ b/etc/shaders/lovr.glsl @@ -22,6 +22,7 @@ struct MaterialData { vec4 glow; vec2 uvShift; vec2 uvScale; + vec2 sdfRange; float metalness; float roughness; float clearcoat; @@ -31,7 +32,6 @@ struct MaterialData { float normalScale; float alphaCutoff; float pointSize; - float sdfRange; }; layout(set = 1, binding = 0) uniform MaterialBlock { MaterialData Material; }; diff --git a/src/modules/data/modelData.h b/src/modules/data/modelData.h index c2a3ed70..482b768a 100644 --- a/src/modules/data/modelData.h +++ b/src/modules/data/modelData.h @@ -99,6 +99,7 @@ typedef struct { float glow[4]; float uvShift[2]; float uvScale[2]; + float sdfRange[2]; float metalness; float roughness; float clearcoat; @@ -108,7 +109,6 @@ typedef struct { float normalScale; float alphaCutoff; float pointSize; - float sdfRange; uint32_t texture; uint32_t glowTexture; uint32_t occlusionTexture; diff --git a/src/modules/graphics/graphics.h b/src/modules/graphics/graphics.h index 02b0e105..b3b00d53 100644 --- a/src/modules/graphics/graphics.h +++ b/src/modules/graphics/graphics.h @@ -291,6 +291,7 @@ typedef struct { float glow[4]; float uvShift[2]; float uvScale[2]; + float sdfRange[2]; float metalness; float roughness; float clearcoat; @@ -300,7 +301,6 @@ typedef struct { float normalScale; float alphaCutoff; float pointSize; - float sdfRange; } MaterialData; typedef struct {