Skip to content

shader_fs_core

The standard Fragment Shader package. Handles texture lookups, sprite/text switching, and basic coloring.


vec4 getTexColor(vec4 bytes, vec2 uv) {
int texIndex = int(bytes.x * 255.0 + 0.5);
vec4 texColor = vec4(1.0);
if (texIndex == 0) {
// sprite atlas
texColor = texture(sampler2D(uTex, uNearestSampler), uv);
} else if (texIndex == 1) {
// pixel font
texColor = texture(sampler2D(uFontTex, uNearestSampler), uv);
} else if (texIndex == 2) {
// vector font (MSDF)
vec4 msd = texture(sampler2D(uFontTex, uLinearSampler), uv);
float sd = median(msd.r, msd.g, msd.b) - 0.5;
float edgeWidth = fwidth(sd) + 0.0001;
edgeWidth = min(edgeWidth, 0.08);
float alpha = clamp(sd / edgeWidth + 0.5, 0.0, 1.0);
texColor = vec4(1.0, 1.0, 1.0, alpha);
}
return texColor;
}

Retreives the correct pixel color for the current fragment.

This function automatically detects if the primitive is a Sprite or a Text based on the bytes data and samples the correct atlas accordingly.

Arguments:

  • bytes: The vBytes varying passed from the vertex shader.
  • uv: The texture coordinates.

bool hasFlag(int flags, int flag) {
return (flags & flag) != 0;
}

Helper to check if a specific bit is set in an integer bitmask.

Arguments:

  • flags: The integer containing all flags.
  • flag: The specific bit to check.

float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}

Calculates the median of three values.


layout(binding=1) uniform texture2D uFontTex;

Binding 1 (Texture): The font atlas. Sampled when textureIndex == 1. Stores font data in the Red color channel.


layout(binding=1) uniform sampler uLinearSampler;

Binding 1 (Sampler): The optional, linear sampler.


layout(binding=0) uniform sampler uNearestSampler;

Binding 0 (Sampler): The default, nearest sampler. Shared by both texture atlases.


layout(binding=0) uniform texture2D uTex;

Binding 0 (Texture): The main sprite atlas. Sampled when textureIndex == 0.


in vec4 vBytes;

Input: Packed data passed from the vertex attributes.

  • x: Texture index (0 = Sprite, 1 = Font)

in vec4 vColor;

Input: Vertex color (multiplied by sprite color).


in vec4 vColorOverride;

Input: Color override.

  • rgb: The tint color.
  • a: The blend strength (0.0 = off, 1.0 = full override)

in vec2 vLocalUv;

Input: Local UV coordinates relative to the sprite itself.


in vec4 vParams;

Input: Custom parameters passed via Vertex.parameters


in vec3 vPosition;

Input: Interpolated world space position of the fragment. Z position is used for depth.


in vec2 vSize;

Input: World space size of the sprite in pixels.


in vec2 vUv;

Input: Texture coordinates for the atlas.