shader_fs_core
The standard Fragment Shader package. Handles texture lookups, sprite/text switching, and basic coloring.
getTexColor
Section titled “getTexColor”vec4 getTexColor(vec4 bytes, vec2 uv) { int texIndex = int(bytes.x * 255.0 + 0.5); vec4 texColor = vec4(1.0); if (texIndex == 0) {// sprite atlastexColor = texture(sampler2D(uTex, uNearestSampler), uv); } else if (texIndex == 1) {// pixel fonttexColor = 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: ThevBytesvarying passed from the vertex shader.uv: The texture coordinates.
hasFlag
Section titled “hasFlag”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.
median
Section titled “median”float median(float r, float g, float b) { return max(min(r, g), min(max(r, g), b));}Calculates the median of three values.
uFontTex
Section titled “uFontTex”layout(binding=1) uniform texture2D uFontTex;Binding 1 (Texture): The font atlas.
Sampled when textureIndex == 1. Stores font data in the Red color channel.
uLinearSampler
Section titled “uLinearSampler”layout(binding=1) uniform sampler uLinearSampler;Binding 1 (Sampler): The optional, linear sampler.
uNearestSampler
Section titled “uNearestSampler”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.
vBytes
Section titled “vBytes”in vec4 vBytes;Input: Packed data passed from the vertex attributes.
x: Texture index (0 =Sprite, 1 =Font)
vColor
Section titled “vColor”in vec4 vColor;Input: Vertex color (multiplied by sprite color).
vColorOverride
Section titled “vColorOverride”in vec4 vColorOverride;Input: Color override.
rgb: The tint color.a: The blend strength (0.0= off,1.0= full override)
vLocalUv
Section titled “vLocalUv”in vec2 vLocalUv;Input: Local UV coordinates relative to the sprite itself.
vParams
Section titled “vParams”in vec4 vParams;Input: Custom parameters passed via Vertex.parameters
vPosition
Section titled “vPosition”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.