diff --git a/Shaders/osgText_Text.frag b/Shaders/osgText_Text.frag deleted file mode 100644 index 1a6091ce0..000000000 --- a/Shaders/osgText_Text.frag +++ /dev/null @@ -1,254 +0,0 @@ -// -*-C++-*- -#version 330 -#pragma vscode_glsllint_stage : frag - -#pragma import_defines( BACKDROP_COLOR, SHADOW, OUTLINE) -#pragma import_defines( SIGNED_DISTANCE_FIELD, TEXTURE_DIMENSION, GLYPH_DIMENSION) - -#if __VERSION__ >= 400 - #define osg_TextureQueryLOD textureQueryLod -#else - #extension GL_ARB_texture_query_lod : enable - #ifdef GL_ARB_texture_query_lod - #define osg_TextureQueryLOD textureQueryLOD - #endif -#endif - -#define TEXTURE texture -#define TEXTURELOD textureLod - -#define ALPHA r -#define SDF g - -uniform sampler2D glyphTexture; - -in vec2 texCoord; -in vec4 vertexColor; - -out vec4 osg_FragColor; - -#ifndef TEXTURE_DIMENSION -const float TEXTURE_DIMENSION = 1024.0; -#endif - -#ifndef GLYPH_DIMENSION -const float GLYPH_DIMENSION = 32.0; -#endif - -#ifdef SIGNED_DISTANCE_FIELD - -float distanceFromEdge(vec2 tc) -{ - float center_alpha = TEXTURELOD(glyphTexture, tc, 0.0).SDF; - if (center_alpha == 0.0) - return -1.0; - - float distance_scale = (1.0 / 6.0) * 1.41; - - return (center_alpha - 0.5) * distance_scale; -} - -#ifdef OUTLINE -vec2 colorCoeff(float edge_distance, float blend_width, float blend_half_width) -{ - float outline_width = OUTLINE * 0.5; - - if (edge_distance > blend_half_width) - return vec2(1.0, 0.0); - else if (edge_distance > -blend_half_width) - { - float f = smoothstep(0.0, 1.0, (blend_half_width - edge_distance) / (blend_width)); - return vec2(1.0 - f, f); - } - else if (edge_distance > (blend_half_width - outline_width)) - return vec2(0.0, 1.0); - else if (edge_distance > -(outline_width + blend_half_width)) - return vec2(0.0, smoothstep(0.0, 1.0, (blend_half_width + outline_width + edge_distance) / blend_width)); - - return vec2(0.0, 0.0); -} - -#else // OUTLINE - -float colorCoeff(float edge_distance, float blend_width, float blend_half_width) -{ - if (edge_distance > blend_half_width) - return 1.0; - else if (edge_distance > -blend_half_width) - return smoothstep(1.0, 0.0, (blend_half_width - edge_distance) / (blend_width)); - - return 0.0; -} -#endif // OUTLINE - -vec4 textColor(vec2 src_texCoord) -{ - float sample_distance_scale = 0.75; - vec2 dx = dFdx(src_texCoord) * sample_distance_scale; - vec2 dy = dFdy(src_texCoord) * sample_distance_scale; - - float distance_across_pixel = length(dx + dy) * (TEXTURE_DIMENSION / GLYPH_DIMENSION); - - // compute the appropriate number of samples required to avoid aliasing. - int maxNumSamplesAcrossSide = 4; - - int numSamplesX = int(TEXTURE_DIMENSION * length(dx)); - int numSamplesY = int(TEXTURE_DIMENSION * length(dy)); - if (numSamplesX < 2) - numSamplesX = 2; - if (numSamplesY < 2) - numSamplesY = 2; - if (numSamplesX > maxNumSamplesAcrossSide) - numSamplesX = maxNumSamplesAcrossSide; - if (numSamplesY > maxNumSamplesAcrossSide) - numSamplesY = maxNumSamplesAcrossSide; - - vec2 delta_tx = dx / float(numSamplesX - 1); - vec2 delta_ty = dy / float(numSamplesY - 1); - - float numSamples = float(numSamplesX) * float(numSamplesY); - float blend_width = 1.5 * distance_across_pixel / numSamples; - float blend_half_width = blend_width * 0.5; - - // check whether fragment is wholly within or outwith glyph body+outline - float cd = distanceFromEdge(src_texCoord); // central distance (distance from center to edge) - if (cd-blend_half_width > distance_across_pixel) - return vertexColor; // pixel fully within glyph body - -#ifdef OUTLINE - float outline_width = OUTLINE * 0.5; - if ((-cd-outline_width - blend_half_width) > distance_across_pixel) - return vec4(0.0, 0.0, 0.0, 0.0); // pixel fully outside outline+glyph body - vec2 color_coeff = vec2(0.0, 0.0); -#else // OUTLINE - if (-cd-blend_half_width > distance_across_pixel) - return vec4(0.0, 0.0, 0.0, 0.0); // pixel fully outside glyph body - float color_coeff = 0.0; -#endif // OUTLINE - - // use multi-sampling to provide high quality antialised fragments - vec2 origin = src_texCoord - dx * 0.5 - dy * 0.5; - for(; numSamplesY > 0; --numSamplesY) - { - vec2 pos = origin; - int numX = numSamplesX; - for(; numX > 0; --numX) - { - #ifdef OUTLINE - color_coeff += colorCoeff(distanceFromEdge(pos), blend_width, blend_half_width); - #else - color_coeff += colorCoeff(distanceFromEdge(pos), blend_width, blend_half_width); - #endif - pos += delta_tx; - } - origin += delta_ty; - } - - color_coeff /= numSamples; - -#ifdef OUTLINE - float vertex_alpha = vertexColor.a * color_coeff.x; - float outline_alpha = BACKDROP_COLOR.a * color_coeff.y; - float total_alpha = vertex_alpha + outline_alpha; - - if (total_alpha <= 0.0) - return vec4(0.0, 0.0, 0.0, 0.0); - - return vec4(vertexColor.rgb * (vertex_alpha / total_alpha) + BACKDROP_COLOR.rgb * (outline_alpha / total_alpha), total_alpha); -#else - return vec4(vertexColor.rgb, vertexColor.a * color_coeff); -#endif -} - -#else // SIGNED_DISTANCE_FIELD - -vec4 textColor(vec2 src_texCoord) -{ -#ifdef OUTLINE - float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA; - float delta_tc = 1.6 * OUTLINE * GLYPH_DIMENSION / TEXTURE_DIMENSION; - - float outline_alpha = alpha; - vec2 origin = src_texCoord - vec2(delta_tc * 0.5, delta_tc * 0.5); - - float numSamples = 3.0; - delta_tc = delta_tc / (numSamples - 1.0); - - float background_alpha = 1.0; - - for(float i = 0.0; i < numSamples; ++i) - { - for(float j = 0.0; j < numSamples; ++j) - { - float local_alpha = TEXTURE(glyphTexture, origin + vec2(i * delta_tc, j * delta_tc)).ALPHA; - outline_alpha = max(outline_alpha, local_alpha); - background_alpha *= (1.0 - local_alpha); - } - } - -#ifdef osg_TextureQueryLOD - float mipmapLevel = osg_TextureQueryLOD(glyphTexture, src_texCoord).x; - if (mipmapLevel < 1.0) - outline_alpha = mix(1.0 - background_alpha, outline_alpha, mipmapLevel / 1.0); -#endif - - if (outline_alpha < alpha) - outline_alpha = alpha; - if (outline_alpha > 1.0) - outline_alpha = 1.0; - if (outline_alpha == 0.0) - return vec4(0.0, 0.0, 0.0, 0.0); // outside glyph and outline - - vec4 color = mix(BACKDROP_COLOR, vertexColor, smoothstep(0.0, 1.0, alpha)); - color.a = vertexColor.a * smoothstep(0.0, 1.0, outline_alpha); - - return color; - -#else // OUTLINE - float alpha = TEXTURE(glyphTexture, src_texCoord).ALPHA; - if (alpha == 0.0) - return vec4(0.0, 0.0, 0.0, 0.0); - - return vec4(vertexColor.rgb, vertexColor.a * alpha); -#endif // OUTLINE -} - -#endif // SIGNED_DISTANCE_FIELD - -void main(void) -{ - if (texCoord.x < 0.0 && texCoord.y < 0.0) - { - osg_FragColor = vertexColor; - return; - } - -#ifdef SHADOW - float scale = -1.0 * GLYPH_DIMENSION / TEXTURE_DIMENSION; - vec2 delta_tc = SHADOW * scale; - vec4 shadow_color = textColor(texCoord + delta_tc); - - shadow_color.rgb = BACKDROP_COLOR.rgb; - vec4 glyph_color = textColor(texCoord); - - // lower the alpha_power value the greater the staturation, no need to be so aggressive with SDF than GREYSCALE -#if SIGNED_DISTANCE_FIELD - float alpha_power = 0.6; -#else - float alpha_power = 0.5; -#endif - - // over saturate the alpha values to make sure the font and it's shadow are clear - shadow_color.a = pow(shadow_color.a, alpha_power); - glyph_color.a = pow(glyph_color.a, alpha_power); - vec4 color = mix(shadow_color, glyph_color, glyph_color.a); - -#else // SHADOW - vec4 color = textColor(texCoord); -#endif // SHADOW - - if (color.a == 0.0) - discard; - - osg_FragColor = color; -} diff --git a/Shaders/osgText_Text.vert b/Shaders/osgText_Text.vert deleted file mode 100644 index d69a9c72d..000000000 --- a/Shaders/osgText_Text.vert +++ /dev/null @@ -1,13 +0,0 @@ -// -*-C++-*- -#version 330 -#pragma vscode_glsllint_stage : vert - -out vec2 texCoord; -out vec4 vertexColor; - -void main(void) -{ - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; - texCoord = gl_MultiTexCoord0.xy; - vertexColor = gl_Color; -}