|
| 1 | +#version 330 |
| 2 | +// Atlas resize without geometry shader |
| 3 | + |
| 4 | +// The render target for this program is the new |
| 5 | +// texture atlas texture |
| 6 | + |
| 7 | +#include :system:shaders/lib/sprite.glsl |
| 8 | + |
| 9 | +// Old and new texture coordinates |
| 10 | +uniform sampler2D atlas_old; |
| 11 | +uniform sampler2D atlas_new; |
| 12 | + |
| 13 | +uniform sampler2D texcoords_old; |
| 14 | +uniform sampler2D texcoords_new; |
| 15 | + |
| 16 | +uniform mat4 projection; |
| 17 | +uniform float border; |
| 18 | + |
| 19 | +out vec2 uv; |
| 20 | + |
| 21 | +void main() { |
| 22 | + // Get the texture sizes |
| 23 | + ivec2 size_old = textureSize(atlas_old, 0).xy; |
| 24 | + ivec2 size_new = textureSize(atlas_new, 0).xy; |
| 25 | + |
| 26 | + // Read texture coordinates from UV texture here |
| 27 | + int texture_id = gl_VertexID / 6; |
| 28 | + vec2 old_uv0, old_uv1, old_uv2, old_uv3; |
| 29 | + getSpriteUVs(texcoords_old, texture_id, old_uv0, old_uv1, old_uv2, old_uv3); |
| 30 | + vec2 new_uv0, new_uv1, new_uv2, new_uv3; |
| 31 | + getSpriteUVs(texcoords_new, texture_id, new_uv0, new_uv1, new_uv2, new_uv3); |
| 32 | + |
| 33 | + // Lower left corner flipped * size - border |
| 34 | + vec2 pos = vec2(new_uv2.x, 1.0 - new_uv2.y) * vec2(size_new) - vec2(border); |
| 35 | + // absolute value of the diagonal * size + border * 2 |
| 36 | + vec2 size = abs(new_uv3 - new_uv0) * vec2(size_new) + vec2(border * 2.0); |
| 37 | + |
| 38 | + // We need to offset the old coordinates by border size |
| 39 | + vec2 pix_offset = vec2(border) / vec2(size_old); |
| 40 | + |
| 41 | + // Emit two triangles over 6 vertices |
| 42 | + switch (gl_VertexID % 6) { |
| 43 | + // First triangle |
| 44 | + case 0: |
| 45 | + // upper left |
| 46 | + uv = old_uv0 - pix_offset; |
| 47 | + gl_Position = projection * vec4(pos + vec2(0.0, size.y), 0.0, 1.0); |
| 48 | + break; |
| 49 | + case 1: |
| 50 | + // lower left |
| 51 | + uv = old_uv2 + vec2(-pix_offset.x, pix_offset.y); |
| 52 | + gl_Position = projection * vec4(pos, 0.0, 1.0); |
| 53 | + break; |
| 54 | + case 2: |
| 55 | + // upper right |
| 56 | + uv = old_uv1 + vec2(pix_offset.x, -pix_offset.y); |
| 57 | + gl_Position = projection * vec4(pos + vec2(size.x, size.y), 0.0, 1.0); |
| 58 | + break; |
| 59 | + // Second triangle |
| 60 | + case 3: |
| 61 | + // lower left |
| 62 | + uv = old_uv2 + vec2(-pix_offset.x, pix_offset.y); |
| 63 | + gl_Position = projection * vec4(pos, 0.0, 1.0); |
| 64 | + break; |
| 65 | + case 4: |
| 66 | + // upper right |
| 67 | + uv = old_uv1 + vec2(pix_offset.x, -pix_offset.y); |
| 68 | + gl_Position = projection * vec4(pos + vec2(size.x, size.y), 0.0, 1.0); |
| 69 | + break; |
| 70 | + case 5: |
| 71 | + // lower right |
| 72 | + uv = old_uv3 + pix_offset; |
| 73 | + gl_Position = projection * vec4(pos + vec2(size.x, 0.0), 0.0, 1.0); |
| 74 | + break; |
| 75 | + } |
| 76 | +} |
0 commit comments