Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // blisteringly erotic 9slice drawing tool.
- //
- varying vec2 v_vTexcoord;
- varying vec4 v_vColour;
- uniform sampler2D s_tex;
- uniform float u_width;
- uniform float u_height;
- uniform float u_tileDim;
- uniform float u_offset;
- uniform float u_originX;
- uniform float u_originY;
- void main()
- {
- //This is for UV correction down the line.
- //60.0 is a magic number, the width of the texture
- float halfTexelOffset = 1.0/60.0/2.0;
- float texelWidth = 1.0/60.0;
- //float minUV = halfTexelOffset;
- float maxUV = 1.0 - texelWidth;
- //ALGORITHM!
- //1. DO everything at once, gloriously.
- float X = floor(gl_FragCoord.x) - u_originX;
- float Y = floor(gl_FragCoord.y) - u_originY;
- vec2 realPos = vec2(X,Y);
- vec2 texUV = vec2(0.0,0.0);
- //X-axis
- if (realPos.x <= u_tileDim)
- {
- texUV.x = mix(0.0,maxUV, (realPos.x / u_tileDim) / 3.0);
- texUV.x += halfTexelOffset;
- }
- else if (realPos.x >= (u_width - u_tileDim))
- {
- texUV.x = mix(0.0,maxUV, (2.0 + (realPos.x - (u_width - u_tileDim)) / u_tileDim) / 3.0);
- texUV.x += halfTexelOffset;
- }
- else
- {
- texUV.x = ( 1.0 + mod(realPos.x,u_tileDim) / u_tileDim) / 3.0; //YES FIX THIS ONE TOO
- texUV.x = mix(0.0,maxUV,texUV.x);
- texUV.x += halfTexelOffset;//no, that's not enough. It needs to be in the right range already.
- //texUV -= texelWidth * floor(u_width / u_tileDim);
- }
- //Y-axis
- if (realPos.y <= u_tileDim)
- {
- texUV.y = mix(0.0,maxUV, (realPos.y / u_tileDim) / 3.0);
- texUV.y += halfTexelOffset;
- }
- else if (realPos.y >= (u_height- u_tileDim))
- {
- texUV.y = mix(0.0,maxUV, (2.0 + (realPos.y - (u_height- u_tileDim)) / u_tileDim) / 3.0);
- texUV.y += halfTexelOffset;
- }
- else
- {
- texUV.y = ( 1.0 + mod(realPos.y,u_tileDim) / u_tileDim) / 3.0; //YES FIX THIS ONE TOO
- texUV.y = mix(0.0,maxUV,texUV.y);
- texUV.y += halfTexelOffset;//no, that's not enough. It needs to be in the right range already.
- }
- //THe above calculations give us UVs that fall on gridlines in the sprite.
- //We need to sample the middle of the texels instead
- //float halfTexelOffset = 1.0/u_tileDim/2.0; //WORKS BEST WITH .05
- texUV.x += u_offset * halfTexelOffset;
- texUV.y += u_offset * halfTexelOffset;
- //And some extra correction
- {
- gl_FragColor = texture2D( s_tex, texUV);
- }
- if ((clamp(texUV.x, 0.0, 1.0) < texUV.x) || (clamp(texUV.y, 0.0, 1.0) < texUV.y))
- {
- //discard;
- }
- //if (texUV.x <= halfTexelOffset * 2.0)
- //{
- // gl_FragColor = vec4(255.0,255.0,0.0,255.0);
- //}
- //if (texUV.y >= 1.0 - (halfTexelOffset * 2.0))
- //{
- // gl_FragColor = vec4(255.0,255.0,0.0,255.0);
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement