Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created December 14, 2017 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphlinus/d486a51af070afd4ca45bed16d8f7740 to your computer and use it in GitHub Desktop.
Save raphlinus/d486a51af070afd4ca45bed16d8f7740 to your computer and use it in GitHub Desktop.
text compositing fragment shader
// Copyright 2017 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#version 330 core
flat in vec4 passColor;
in vec2 uv;
layout (location = 0, index = 0) out vec4 color;
layout (location = 0, index = 1) out vec4 alphaMask;
uniform sampler2D mask;
void main() {
vec3 textColor = texture(mask, uv).rgb;
vec3 fg = passColor.rgb;
// linear luminance of black-on-white text
vec3 linText = textColor * textColor;
// linear luminance of fg-on-white text
vec3 linBlend = mix(fg * fg, vec3(1.0201), linText);
// luminance of fg-on-white text in gamma=2 space
vec3 gamBlend = sqrt(linBlend);
vec3 a = vec3(1.0) - (gamBlend - fg) / (vec3(1.01) - fg);
alphaMask = vec4(a * passColor.a, 1.0);
color = vec4(fg, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment