From 82d1c94d999654bda5d40108393eade80b77342c Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Wed, 15 Jan 2025 21:21:22 -0500 Subject: Update --- src/assets/shaders/bitmap/frag.glsl | 59 +++++++++++++++++++++++++++++++++++ src/assets/shaders/bitmap/vert.glsl | 26 +++++++++++++++ src/assets/shaders/text/frag.glsl | 59 ----------------------------------- src/assets/shaders/text/vert.glsl | 26 --------------- src/assets/shaders/truetype/frag.glsl | 23 ++++++++++++++ src/assets/shaders/truetype/vert.glsl | 26 +++++++++++++++ 6 files changed, 134 insertions(+), 85 deletions(-) create mode 100644 src/assets/shaders/bitmap/frag.glsl create mode 100644 src/assets/shaders/bitmap/vert.glsl delete mode 100644 src/assets/shaders/text/frag.glsl delete mode 100644 src/assets/shaders/text/vert.glsl create mode 100644 src/assets/shaders/truetype/frag.glsl create mode 100644 src/assets/shaders/truetype/vert.glsl (limited to 'src/assets/shaders') diff --git a/src/assets/shaders/bitmap/frag.glsl b/src/assets/shaders/bitmap/frag.glsl new file mode 100644 index 0000000..94d27c9 --- /dev/null +++ b/src/assets/shaders/bitmap/frag.glsl @@ -0,0 +1,59 @@ +#version 300 es +precision highp float; + +uniform sampler2D texture_data; +uniform int text_length; +uniform int text[256]; +uniform int char_width; +uniform int char_height; +uniform int font_width; +uniform int font_height; +uniform int text_width; +uniform int text_height; +uniform vec3 text_color; + +in vec2 vertex_texcoord; +out vec4 frag_color; + +void main() +{ + vec2 inverted_texcoord = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + vec2 texcoord_pixels = inverted_texcoord * vec2(float(text_width), float(text_height)); + int texcoord_char_x = int(floor(texcoord_pixels.x)) / char_width; + int texcoord_char_y = int(floor(texcoord_pixels.y)) / char_height; + + int x = 0; + int y = 0; + int i = 0; + for (; i < text_length; ++i) { + if (x == texcoord_char_x && y == texcoord_char_y) { + break; + } + if (text[i] == 10) { + x = 0; + y += 1; + } else { + x += 1; + } + } + if (i == text_length || text[i] == 10) discard; + + int entry = text[i] - 32; + vec2 texcoord_base = vec2( + float(entry % (font_width / char_width)) * float(char_width), + float(entry / (font_width / char_width)) * float(char_height) + ); + // vec2 texcoord_base = vec2(8.0, 0.0); + vec2 texcoord_off = vec2( + mod(texcoord_pixels.x, float(char_width)), + mod(texcoord_pixels.y, float(char_height)) + ); + vec2 texcoord_final = (texcoord_base + texcoord_off) / vec2(float(font_width), float(font_height)); + + vec4 texel = texture(texture_data, texcoord_final); + if (texel.rgb == vec3(0.0, 0.0, 0.0)) discard; + texel.r = text_color.r; + texel.g = text_color.g; + texel.b = text_color.b; + frag_color = texel; +} diff --git a/src/assets/shaders/bitmap/vert.glsl b/src/assets/shaders/bitmap/vert.glsl new file mode 100644 index 0000000..4005d75 --- /dev/null +++ b/src/assets/shaders/bitmap/vert.glsl @@ -0,0 +1,26 @@ +#version 300 es +precision highp float; + +uniform mat4 view; +uniform mat4 position; + +out vec2 vertex_texcoord; + +void main() { + const vec2 positions[4] = vec2[]( + vec2(-1, -1), + vec2(+1, -1), + vec2(-1, +1), + vec2(+1, +1) + ); + const vec2 coords[4] = vec2[]( + vec2(0, 0), + vec2(1, 0), + vec2(0, 1), + vec2(1, 1) + ); + vec4 vertex = vec4(positions[gl_VertexID], 0.0, 1.0); + + vertex_texcoord = coords[gl_VertexID]; + gl_Position = view * position * vertex; +} diff --git a/src/assets/shaders/text/frag.glsl b/src/assets/shaders/text/frag.glsl deleted file mode 100644 index 94d27c9..0000000 --- a/src/assets/shaders/text/frag.glsl +++ /dev/null @@ -1,59 +0,0 @@ -#version 300 es -precision highp float; - -uniform sampler2D texture_data; -uniform int text_length; -uniform int text[256]; -uniform int char_width; -uniform int char_height; -uniform int font_width; -uniform int font_height; -uniform int text_width; -uniform int text_height; -uniform vec3 text_color; - -in vec2 vertex_texcoord; -out vec4 frag_color; - -void main() -{ - vec2 inverted_texcoord = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); - vec2 texcoord_pixels = inverted_texcoord * vec2(float(text_width), float(text_height)); - int texcoord_char_x = int(floor(texcoord_pixels.x)) / char_width; - int texcoord_char_y = int(floor(texcoord_pixels.y)) / char_height; - - int x = 0; - int y = 0; - int i = 0; - for (; i < text_length; ++i) { - if (x == texcoord_char_x && y == texcoord_char_y) { - break; - } - if (text[i] == 10) { - x = 0; - y += 1; - } else { - x += 1; - } - } - if (i == text_length || text[i] == 10) discard; - - int entry = text[i] - 32; - vec2 texcoord_base = vec2( - float(entry % (font_width / char_width)) * float(char_width), - float(entry / (font_width / char_width)) * float(char_height) - ); - // vec2 texcoord_base = vec2(8.0, 0.0); - vec2 texcoord_off = vec2( - mod(texcoord_pixels.x, float(char_width)), - mod(texcoord_pixels.y, float(char_height)) - ); - vec2 texcoord_final = (texcoord_base + texcoord_off) / vec2(float(font_width), float(font_height)); - - vec4 texel = texture(texture_data, texcoord_final); - if (texel.rgb == vec3(0.0, 0.0, 0.0)) discard; - texel.r = text_color.r; - texel.g = text_color.g; - texel.b = text_color.b; - frag_color = texel; -} diff --git a/src/assets/shaders/text/vert.glsl b/src/assets/shaders/text/vert.glsl deleted file mode 100644 index 4005d75..0000000 --- a/src/assets/shaders/text/vert.glsl +++ /dev/null @@ -1,26 +0,0 @@ -#version 300 es -precision highp float; - -uniform mat4 view; -uniform mat4 position; - -out vec2 vertex_texcoord; - -void main() { - const vec2 positions[4] = vec2[]( - vec2(-1, -1), - vec2(+1, -1), - vec2(-1, +1), - vec2(+1, +1) - ); - const vec2 coords[4] = vec2[]( - vec2(0, 0), - vec2(1, 0), - vec2(0, 1), - vec2(1, 1) - ); - vec4 vertex = vec4(positions[gl_VertexID], 0.0, 1.0); - - vertex_texcoord = coords[gl_VertexID]; - gl_Position = view * position * vertex; -} diff --git a/src/assets/shaders/truetype/frag.glsl b/src/assets/shaders/truetype/frag.glsl new file mode 100644 index 0000000..b0e25bf --- /dev/null +++ b/src/assets/shaders/truetype/frag.glsl @@ -0,0 +1,23 @@ +#version 300 es +precision highp float; + +uniform sampler2D texture_data; +uniform int text[256]; +uniform int atlas_width; +uniform int cell_width; +uniform int text_width; + +in vec2 vertex_texcoord; +out vec4 frag_color; + +void main() +{ + vec2 inverted_texcoord = vec2(vertex_texcoord.x, 1.0 - vertex_texcoord.y); + float texcoord_pixels_x = inverted_texcoord.x * float(text_width); + int char_idx = int(floor(texcoord_pixels_x)) / cell_width; + int offset = text[char_idx]; + float cbase = float(offset); + float coff = mod(texcoord_pixels_x, float(cell_width)); + float val = texture(texture_data, vec2((cbase + coff) / float(atlas_width), inverted_texcoord.y)).r; + frag_color = vec4(val, val, val, 1.0); +} diff --git a/src/assets/shaders/truetype/vert.glsl b/src/assets/shaders/truetype/vert.glsl new file mode 100644 index 0000000..4005d75 --- /dev/null +++ b/src/assets/shaders/truetype/vert.glsl @@ -0,0 +1,26 @@ +#version 300 es +precision highp float; + +uniform mat4 view; +uniform mat4 position; + +out vec2 vertex_texcoord; + +void main() { + const vec2 positions[4] = vec2[]( + vec2(-1, -1), + vec2(+1, -1), + vec2(-1, +1), + vec2(+1, +1) + ); + const vec2 coords[4] = vec2[]( + vec2(0, 0), + vec2(1, 0), + vec2(0, 1), + vec2(1, 1) + ); + vec4 vertex = vec4(positions[gl_VertexID], 0.0, 1.0); + + vertex_texcoord = coords[gl_VertexID]; + gl_Position = view * position * vertex; +} -- cgit v1.2.3