|
| 1 | +#version 330 |
| 2 | +// vert/frag only version of the sprite list shader |
| 3 | + |
| 4 | +uniform WindowBlock { |
| 5 | + mat4 projection; |
| 6 | + mat4 view; |
| 7 | +} window; |
| 8 | + |
| 9 | +// Texture atlas |
| 10 | +uniform sampler2D sprite_texture; |
| 11 | +// Texture containing UVs for the entire atlas |
| 12 | +uniform sampler2D uv_texture; |
| 13 | + |
| 14 | +uniform vec4 pos_rot; // rect.x, rect.y, 0, angle |
| 15 | +uniform vec4 color; // color.normalized |
| 16 | +uniform vec2 size; // rect.width, rect.height |
| 17 | +uniform int texture_id; |
| 18 | + |
| 19 | +// How much half-pixel offset to apply to the UVs. |
| 20 | +// 0.0 is no offset, 1.0 is half a pixel offset |
| 21 | +uniform float uv_offset_bias; |
| 22 | + |
| 23 | +// Output to frag shader |
| 24 | +out vec2 v_uv; |
| 25 | +out vec4 v_color; |
| 26 | + |
| 27 | +#include :system:shaders/lib/sprite.glsl |
| 28 | + |
| 29 | + |
| 30 | +const vec2 vertices[4] = vec2[4]( |
| 31 | + vec2(-0.5, +0.5), // Upper left |
| 32 | + vec2(-0.5, -0.5), // lower left |
| 33 | + vec2(+0.5, +0.5), // upper right |
| 34 | + vec2(+0.5, -0.5) // lower right |
| 35 | +); |
| 36 | + |
| 37 | +void main() { |
| 38 | + vec2 uv0, uv1, uv2, uv3; |
| 39 | + getSpriteUVs(uv_texture, texture_id, uv0, uv1, uv2, uv3); |
| 40 | + |
| 41 | + vec3 center = pos_rot.xyz; |
| 42 | + float angle = radians(pos_rot.w); |
| 43 | + mat2 rot = mat2( |
| 44 | + cos(angle), -sin(angle), |
| 45 | + sin(angle), cos(angle) |
| 46 | + ); |
| 47 | + |
| 48 | + mat4 mvp = window.projection * window.view; |
| 49 | + |
| 50 | + // Apply half pixel offset modified by bias. |
| 51 | + // What bias to set depends on the texture filtering mode. |
| 52 | + // Linear can have 1.0 bias while nearest should have 0.0 (unless scale is 1:1) |
| 53 | + // uvs ( |
| 54 | + // 0.0, 0.0, |
| 55 | + // 1.0, 0.0, |
| 56 | + // 0.0, 1.0, |
| 57 | + // 1.0, 1.0 |
| 58 | + // ) |
| 59 | + vec2 hp = 0.5 / vec2(textureSize(sprite_texture, 0)) * uv_offset_bias; |
| 60 | + uv0 += hp; |
| 61 | + uv1 += vec2(-hp.x, hp.y); |
| 62 | + uv2 += vec2(hp.x, -hp.y); |
| 63 | + uv3 += -hp; |
| 64 | + |
| 65 | + int vertex_id = gl_VertexID % 4; |
| 66 | + vec2 uvs[4] = vec2[4](uv0, uv2, uv1, uv3); |
| 67 | + v_color = color; |
| 68 | + gl_Position = mvp * vec4(rot * (vertices[vertex_id] * size) + center.xy, 0.0, 1.0); |
| 69 | + v_uv = uvs[vertex_id]; |
| 70 | +} |
0 commit comments