view semiconginev2/text.nim @ 1241:a0ed1a918fda

fix: letters sometimes overlapping other letters quad
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 12:51:50 +0700
parents 176383220123
children
line wrap: on
line source

const
  NEWLINE = Rune('\n')
  SPACE = Rune(' ')

type
  GlyphInfo* = object
    uvs*: array[4, Vec2f]
    dimension*: Vec2f
    topOffset*: float32
    leftOffset*: float32
    advance*: float32
  FontObj* = object
    name*: string
    glyphs*: Table[Rune, GlyphInfo]
    fontAtlas*: Image[Gray]
    maxHeight*: int
    kerning*: Table[(Rune, Rune), float32]
    fontscale*: float32
    lineHeight*: float32
    lineAdvance*: float32
    capHeight*: float32
    xHeight*: float32
  Font = ref FontObj

  TextboxData = object
    color: Vec4f
    position: Vec3f
    scale: float32
    aspectratio: float32
  TextboxDescriptorSet = object
    textbox: GPUValue[TextboxData, UniformBufferMapped]
    fontAtlas: Image[Gray]

  DefaultFontShader* = object
    position {.VertexAttribute.}: Vec3f
    uv {.VertexAttribute.}: Vec2f # TODO: maybe we can keep the uvs in a uniform buffer and just pass an index
    fragmentUv {.Pass.}: Vec2f
    color {.ShaderOutput.}: Vec4f
    descriptorSets {.DescriptorSets.}: (TextboxDescriptorSet, )
    vertexCode = """void main() {
  gl_Position = vec4(position * vec3(1, textbox.aspectratio, 1) * textbox.scale + textbox.position, 1.0);
  fragmentUv = uv;
}  """
    fragmentCode = """void main() {
    float v = texture(fontAtlas, fragmentUv).r;
    if(v == 0) {
      discard;
    }
    color = vec4(textbox.color.rgb, textbox.color.a * v);
}"""


include ./text/font
include ./text/textbox