IsodiModel.fragmentShader

Default fragment shader used to render the model.

The data is null-terminated for C compatibility.

struct IsodiModel
static immutable
auto fragmentShader = ` #version 330 ` ~ q{ in vec2 fragTexCoord; in vec4 fragVariantUV; in vec2 fragAnchor; flat in int angle; uniform sampler2D texture0; uniform vec4 colDiffuse; uniform mat4 transform; uniform mat4 modelview; uniform mat4 projection; uniform int performFold; out vec4 finalColor; void setColor() { // Get texture coordinates in the atlas vec2 coords = vec2(fragVariantUV.x, fragVariantUV.y); // Get size of the texture vec2 size = vec2(fragVariantUV.z, fragVariantUV.w); vec2 offset; // No folding, render like normal if (performFold == 0) { // Set the offset in the region with no special overrides offset = fragTexCoord * size; } // Perform fold else { // Get texture ratio vec2 ratio = vec2(1, size.y / size.x); // Get segment where the texture starts to repeat vec2 fold = vec2(0, size.y - size.x); // Get offset 1. until the fold offset = min(fold, fragTexCoord * size / ratio) // 2. repeat after the fold + fract(max(vec2(0), fragTexCoord - ratio + 1))*size.x; } // Apply angle offset offset.x += angle * size.x; // Fetch the data from the texture vec4 texelColor = texture(texture0, coords + offset); if (texelColor.w == 0) discard; // Set the color finalColor = texelColor * colDiffuse; } void setDepth() { // Change Y axis in the modelview matrix to (0, 1, 0, 0) so camera height doesn't affect depth // calculations mat4 flatview = modelview; flatview[0].y = 0; flatview[1].y = 1; flatview[2].y = 0; flatview[3].y = 0; mat4 flatform = transform; flatform[0].y = 0; flatform[1].y = 1; flatform[2].y = 0; flatform[3].y = 0; // Transform the anchor in the world vec4 anchor = flatview * flatform * vec4(fragAnchor.x, 0, fragAnchor.y, 1); // Get the clip space coordinates vec4 clip = projection * anchor * vec4(1, 0, 1, 1); // Convert to OpenGL's value range float depth = (clip.z / clip.w + 1) / 2.0; gl_FragDepth = gl_DepthRange.diff * depth + gl_DepthRange.near; } void main() { setDepth(); setColor(); } } ~ '\0';

Meta