| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
|
| 5 |
import pyglet |
| 6 |
from pyglet.gl import ( |
| 7 |
GL_BLEND, |
| 8 |
GL_ONE_MINUS_SRC_ALPHA, |
| 9 |
GL_SRC_ALPHA, |
| 10 |
GL_TEXTURE0, |
| 11 |
glActiveTexture, |
| 12 |
glBindTexture, |
| 13 |
glBlendFunc, |
| 14 |
glDisable, |
| 15 |
glEnable, |
| 16 |
) |
| 17 |
from pyglet.window import key |
| 18 |
|
| 19 |
|
| 20 |
class DistFieldGroup(pyglet.sprite.SpriteGroup): |
| 21 |
def set_state(self) -> None: |
| 22 |
self.program.use() |
| 23 |
|
| 24 |
glActiveTexture(GL_TEXTURE0) |
| 25 |
glBindTexture(self.texture.target, self.texture.id) |
| 26 |
|
| 27 |
self.program['bidirectional'] = enable_bidirectional |
| 28 |
|
| 29 |
self.program['antialias'] = enable_antialias |
| 30 |
self.program['outline'] = enable_outline |
| 31 |
self.program['outline_width'] = outline_width |
| 32 |
self.program['glow'] = enable_glow |
| 33 |
self.program['glow_width'] = glow_width |
| 34 |
|
| 35 |
glEnable(GL_BLEND) |
| 36 |
glBlendFunc(self.blend_src, self.blend_dest) |
| 37 |
|
| 38 |
def unset_state(self) -> None: |
| 39 |
glDisable(GL_BLEND) |
| 40 |
self.program.stop() |
| 41 |
|
| 42 |
|
| 43 |
class DistFieldSprite(pyglet.sprite.Sprite): |
| 44 |
"""Override sprite to use DistFieldTextureGroup.""" |
| 45 |
group_class = DistFieldGroup |
| 46 |
|
| 47 |
def __init__(self, img, x=0, y=0, z=0, blend_src=GL_SRC_ALPHA, blend_dest=GL_ONE_MINUS_SRC_ALPHA, batch=None, |
| 48 |
group=None, subpixel=False, program=None) -> None: |
| 49 |
super().__init__(img, x, y, z, blend_src, blend_dest, batch, group, subpixel, program) |
| 50 |
|
| 51 |
|
| 52 |
window = pyglet.window.Window(resizable=True) |
| 53 |
|
| 54 |
|
| 55 |
@window.event |
| 56 |
def on_resize(width, height): |
| 57 |
scale_width = width / float(image.width) |
| 58 |
scale_height = height / float(image.height) |
| 59 |
sprite.scale = min(scale_width, scale_height) |
| 60 |
sprite.x = width / 2 |
| 61 |
sprite.y = height / 2 |
| 62 |
|
| 63 |
|
| 64 |
@window.event |
| 65 |
def on_draw(): |
| 66 |
window.clear() |
| 67 |
sprite.draw() |
| 68 |
|
| 69 |
|
| 70 |
@window.event |
| 71 |
def on_key_press(symbol, modifiers): |
| 72 |
global enable_bidirectional |
| 73 |
global enable_antialias |
| 74 |
global enable_outline |
| 75 |
global enable_glow |
| 76 |
global outline_width |
| 77 |
global glow_width |
| 78 |
if symbol == key.B: |
| 79 |
enable_bidirectional = not enable_bidirectional |
| 80 |
elif symbol == key.A: |
| 81 |
enable_antialias = not enable_antialias |
| 82 |
elif symbol == key.O: |
| 83 |
enable_outline = not enable_outline |
| 84 |
enable_glow = False |
| 85 |
elif symbol == key.G: |
| 86 |
enable_glow = not enable_glow |
| 87 |
enable_outline = False |
| 88 |
elif symbol == key.PERIOD: |
| 89 |
if enable_glow: |
| 90 |
glow_width += 0.005 |
| 91 |
else: |
| 92 |
outline_width += 0.005 |
| 93 |
elif symbol == key.COMMA: |
| 94 |
if enable_glow: |
| 95 |
glow_width -= 0.005 |
| 96 |
else: |
| 97 |
outline_width -= 0.005 |
| 98 |
|
| 99 |
print('-' * 78) |
| 100 |
print('enable_bidirectional:', enable_bidirectional) |
| 101 |
print('enable_antialias:', enable_antialias) |
| 102 |
print('enable_outline:', enable_outline) |
| 103 |
print('enable_glow:', enable_glow) |
| 104 |
print('outline_width:', outline_width) |
| 105 |
|
| 106 |
|
| 107 |
vertex_source = """#version 150 core |
| 108 |
in vec3 translate; |
| 109 |
in vec4 colors; |
| 110 |
in vec3 tex_coords; |
| 111 |
in vec2 scale; |
| 112 |
in vec3 position; |
| 113 |
in float rotation; |
| 114 |
|
| 115 |
out vec4 vertex_colors; |
| 116 |
out vec3 texture_coords; |
| 117 |
|
| 118 |
uniform WindowBlock |
| 119 |
{ |
| 120 |
mat4 projection; |
| 121 |
mat4 view; |
| 122 |
} window; |
| 123 |
|
| 124 |
mat4 m_scale = mat4(1.0); |
| 125 |
mat4 m_rotation = mat4(1.0); |
| 126 |
mat4 m_translate = mat4(1.0); |
| 127 |
|
| 128 |
void main() |
| 129 |
{ |
| 130 |
m_scale[0][0] = scale.x; |
| 131 |
m_scale[1][1] = scale.y; |
| 132 |
m_translate[3][0] = translate.x; |
| 133 |
m_translate[3][1] = translate.y; |
| 134 |
m_translate[3][2] = translate.z; |
| 135 |
m_rotation[0][0] = cos(-radians(rotation)); |
| 136 |
m_rotation[0][1] = sin(-radians(rotation)); |
| 137 |
m_rotation[1][0] = -sin(-radians(rotation)); |
| 138 |
m_rotation[1][1] = cos(-radians(rotation)); |
| 139 |
|
| 140 |
gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0); |
| 141 |
|
| 142 |
vertex_colors = colors; |
| 143 |
texture_coords = tex_coords; |
| 144 |
} |
| 145 |
""" |
| 146 |
|
| 147 |
fragment_source = """#version 150 core |
| 148 |
in vec4 vertex_colors; |
| 149 |
in vec3 texture_coords; |
| 150 |
out vec4 final_colors; |
| 151 |
|
| 152 |
uniform sampler2D sprite_texture; |
| 153 |
|
| 154 |
uniform bool bidirectional; |
| 155 |
uniform bool antialias; |
| 156 |
uniform bool outline; |
| 157 |
uniform bool glow; |
| 158 |
uniform float outline_width; |
| 159 |
uniform float glow_width; |
| 160 |
|
| 161 |
const vec4 outline_color = vec4(0.0, 0.0, 1.0, 1.0); |
| 162 |
const vec4 glow_color = vec4(1.0, 0.0, 0.0, 1.0); |
| 163 |
|
| 164 |
void main() |
| 165 |
{ |
| 166 |
float alpha_mask; |
| 167 |
if (bidirectional) |
| 168 |
{ |
| 169 |
vec4 field = texture2D(sprite_texture, texture_coords.xy); |
| 170 |
alpha_mask = float(field.r >= 0.5 && field.g >= 0.5); |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
alpha_mask = texture2D(sprite_texture, texture_coords.xy).a; |
| 175 |
} |
| 176 |
float alpha_width = fwidth(alpha_mask); |
| 177 |
float intensity = alpha_mask; |
| 178 |
|
| 179 |
final_colors = vertex_colors; |
| 180 |
|
| 181 |
if (glow) |
| 182 |
{ |
| 183 |
float glow_min = 0.5 - glow_width; |
| 184 |
intensity = (alpha_mask - glow_min) / (0.5 - glow_min); |
| 185 |
float glow_intensity = 0.0; |
| 186 |
if (antialias) |
| 187 |
glow_intensity = 1.0 - smoothstep(0.5 - alpha_width, |
| 188 |
0.5 + alpha_width, |
| 189 |
alpha_mask) * 2.0; |
| 190 |
else |
| 191 |
glow_intensity = float(alpha_mask < 0.5); |
| 192 |
|
| 193 |
final_colors = mix(final_colors, glow_color, glow_intensity); |
| 194 |
} |
| 195 |
else if (outline) |
| 196 |
{ |
| 197 |
float outline_intensity = 0.0; |
| 198 |
float outline_min = 0.5 - outline_width; |
| 199 |
float outline_max = 0.5; |
| 200 |
if (antialias) |
| 201 |
{ |
| 202 |
|
| 203 |
outline_intensity = 1.0 - smoothstep(outline_max - alpha_width, |
| 204 |
outline_max + alpha_width, |
| 205 |
alpha_mask) * 2.0; |
| 206 |
|
| 207 |
intensity *= smoothstep(outline_min - alpha_width, |
| 208 |
outline_min + alpha_width, |
| 209 |
alpha_mask) * 2.0; |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
outline_intensity = |
| 214 |
float(alpha_mask >= outline_min && alpha_mask <= outline_max); |
| 215 |
intensity = float(alpha_mask >= outline_min); |
| 216 |
} |
| 217 |
final_colors = mix(final_colors, outline_color, outline_intensity); |
| 218 |
} |
| 219 |
else if (antialias) |
| 220 |
{ |
| 221 |
intensity *= smoothstep(0.5 - alpha_width, |
| 222 |
0.5 + alpha_width, |
| 223 |
alpha_mask) * 2.0; |
| 224 |
} |
| 225 |
else |
| 226 |
{ |
| 227 |
intensity = float(alpha_mask >= 0.5); |
| 228 |
} |
| 229 |
|
| 230 |
final_colors.a = intensity; |
| 231 |
} |
| 232 |
""" |
| 233 |
|
| 234 |
enable_bidirectional = False |
| 235 |
enable_antialias = False |
| 236 |
enable_outline = False |
| 237 |
enable_glow = False |
| 238 |
outline_width = 0.02 |
| 239 |
glow_width = 0.1 |
| 240 |
|
| 241 |
dist_shader = pyglet.graphics.shader.ShaderProgram( |
| 242 |
pyglet.graphics.shader.Shader(vertex_source, "vertex"), |
| 243 |
pyglet.graphics.shader.Shader(fragment_source, "fragment"), |
| 244 |
) |
| 245 |
|
| 246 |
if len(sys.argv) > 1: |
| 247 |
filename = sys.argv[1] |
| 248 |
else: |
| 249 |
filename = 'py.df.png' |
| 250 |
|
| 251 |
controls = """ |
| 252 |
Following keys modify the shader operation: |
| 253 |
|
| 254 |
A Toggle antialiasing |
| 255 |
B Toggle bidirectional (see below) |
| 256 |
O Toggle outline |
| 257 |
G Toggle glow |
| 258 |
, Decrease outline/glow |
| 259 |
. Increase outline/glow |
| 260 |
""" |
| 261 |
print(controls) |
| 262 |
|
| 263 |
image = pyglet.image.load(filename) |
| 264 |
|
| 265 |
image.anchor_x = image.width // 2 |
| 266 |
image.anchor_y = image.height // 2 |
| 267 |
sprite = DistFieldSprite(image, program=dist_shader) |
| 268 |
|
| 269 |
pyglet.app.run() |