Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // blisteringly erotic 9slice drawing tool.
  3. //
  4. varying vec2 v_vTexcoord;
  5. varying vec4 v_vColour;
  6. uniform sampler2D s_tex;
  7. uniform float u_width;
  8. uniform float u_height;
  9. uniform float u_tileDim;
  10. uniform float u_offset;
  11.  
  12. uniform float u_originX;
  13. uniform float u_originY;
  14.  
  15.  
  16.  
  17. void main()
  18. {
  19.     //This is for UV correction down the line.
  20.     //60.0 is a magic number, the width of the texture
  21.     float halfTexelOffset = 1.0/60.0/2.0;
  22.     float texelWidth = 1.0/60.0;
  23.     //float minUV = halfTexelOffset;
  24.     float maxUV = 1.0 - texelWidth;
  25.     //ALGORITHM!
  26.     //1. DO everything at once, gloriously.
  27.     float X = floor(gl_FragCoord.x) - u_originX;
  28.     float Y = floor(gl_FragCoord.y) - u_originY;
  29.  
  30.     vec2 realPos = vec2(X,Y);
  31.     vec2 texUV = vec2(0.0,0.0);
  32.  
  33.  
  34.     //X-axis
  35.     if (realPos.x <= u_tileDim)
  36.     {
  37.         texUV.x = mix(0.0,maxUV, (realPos.x / u_tileDim) / 3.0);
  38.         texUV.x += halfTexelOffset;
  39.     }
  40.     else if (realPos.x >= (u_width - u_tileDim))
  41.     {
  42.  
  43.         texUV.x = mix(0.0,maxUV, (2.0 + (realPos.x - (u_width - u_tileDim)) / u_tileDim) / 3.0);
  44.         texUV.x += halfTexelOffset;
  45.     }
  46.     else
  47.     {
  48.         texUV.x = ( 1.0 + mod(realPos.x,u_tileDim) / u_tileDim) / 3.0; //YES FIX THIS ONE TOO
  49.         texUV.x = mix(0.0,maxUV,texUV.x);
  50.         texUV.x += halfTexelOffset;//no, that's not enough. It needs to be in the right range already.
  51.         //texUV -= texelWidth * floor(u_width / u_tileDim);
  52.     }
  53.    
  54.     //Y-axis
  55.     if (realPos.y <= u_tileDim)
  56.     {
  57.        
  58.         texUV.y = mix(0.0,maxUV, (realPos.y / u_tileDim) / 3.0);
  59.         texUV.y += halfTexelOffset;
  60.     }
  61.     else if (realPos.y >= (u_height- u_tileDim))
  62.     {
  63.         texUV.y = mix(0.0,maxUV, (2.0 + (realPos.y - (u_height- u_tileDim)) / u_tileDim) / 3.0);
  64.         texUV.y += halfTexelOffset;
  65.     }
  66.     else
  67.     {
  68.         texUV.y = ( 1.0 + mod(realPos.y,u_tileDim) / u_tileDim) / 3.0; //YES FIX THIS ONE TOO
  69.         texUV.y = mix(0.0,maxUV,texUV.y);
  70.         texUV.y += halfTexelOffset;//no, that's not enough. It needs to be in the right range already.
  71.     }
  72.        
  73.     //THe above calculations give us UVs that fall on gridlines in the sprite.
  74.     //We need to sample the middle of the texels instead
  75.     //float halfTexelOffset = 1.0/u_tileDim/2.0; //WORKS BEST WITH .05
  76.    
  77.     texUV.x += u_offset * halfTexelOffset;
  78.     texUV.y += u_offset * halfTexelOffset;
  79.  
  80.     //And some extra correction
  81.    
  82.     {
  83.         gl_FragColor = texture2D( s_tex, texUV);
  84.     }
  85.    
  86.     if ((clamp(texUV.x, 0.0, 1.0) < texUV.x) || (clamp(texUV.y, 0.0, 1.0) < texUV.y))
  87.     {
  88.         //discard;
  89.     }
  90.     //if (texUV.x <= halfTexelOffset * 2.0)
  91.     //{
  92.     //  gl_FragColor = vec4(255.0,255.0,0.0,255.0);
  93.     //}
  94.    
  95.     //if (texUV.y >= 1.0 - (halfTexelOffset * 2.0))
  96.     //{
  97.     //  gl_FragColor = vec4(255.0,255.0,0.0,255.0);
  98.     //}
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement