eniallator

Love2D Tile Shader

Dec 1st, 2017
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. local tile_shader = love.graphics.newShader[[
  2.     vec4 darkenPixel();
  3.  
  4.     vec4 darkenPixel( vec4 pixel, number darkness , number initial_lightness) {
  5.         number r = pixel.r * darkness * initial_lightness;
  6.         number g = pixel.g * darkness * initial_lightness;
  7.         number b = pixel.b * darkness * initial_lightness;
  8.         return vec4(r, g, b, pixel.a);
  9.     }
  10.  
  11.     extern number initial_lightness;
  12.  
  13.     extern bool top;
  14.     extern bool bottom;
  15.     extern bool left;
  16.     extern bool right;
  17.  
  18.     extern bool top_left;
  19.     extern bool top_right;
  20.     extern bool bottom_left;
  21.     extern bool bottom_right;
  22.  
  23.     vec4 effect( vec4 colour, Image texture, vec2 texture_coords, vec2 screen_coords ) {
  24.         number lightness_multiplier = 1.75;
  25.         number spread = 2.3;
  26.  
  27.         number x = texture_coords.x;
  28.         number y = texture_coords.y;
  29.  
  30.         number diffX = x - 0.5;
  31.         number diffY = y - 0.5;
  32.  
  33.         vec4 pixel = Texel(texture, texture_coords);
  34.        
  35.         number magnitude = sqrt(pow(diffX, 2) + pow(diffY, 2));
  36.  
  37.         if (top && diffY <= 0 || bottom && diffY >= 0)
  38.             magnitude = min(abs(0.5 - x), magnitude);
  39.  
  40.         if (left && diffX <= 0 || right && diffX >= 0)
  41.             magnitude = min(abs(0.5 - y), magnitude);
  42.        
  43.         if (top && left && top_left && y < 1 - x || bottom && right && bottom_right && y > 1 - x) {
  44.             magnitude = min(abs(x - y) / sqrt(2), magnitude);
  45.  
  46.             bool top_left_top = top && diffX <= 0 && y < x;
  47.             bool top_left_left = left && diffY <= 0 && y > x;
  48.             bool bot_right_bot = bottom && diffX >= 0 && y > x;
  49.             bool bot_right_right = right && diffY >= 0 && y < x;
  50.  
  51.             if (top_left_top || top_left_left || bot_right_bot || bot_right_right)
  52.                 magnitude = 0;
  53.         }
  54.        
  55.         if (top && right && top_right && y < x || bottom && left && bottom_left && y > x){
  56.             magnitude = min(abs(x + y - 1) / sqrt(2), magnitude);
  57.  
  58.             bool top_right_top = top && diffX >= 0 && y < 1 - x;
  59.             bool top_right_right = right && diffY <= 0 && y > 1 - x;
  60.             bool bot_left_bot = bottom && diffX <= 0 && y > 1 - x;
  61.             bool bot_left_left = left && diffY >= 0 && y < 1 - x;
  62.  
  63.             if (top_right_top || top_right_right || bot_left_bot || bot_left_left)
  64.                 magnitude = 0;
  65.         }
  66.  
  67.         magnitude = pow(magnitude, 2) * spread;
  68.        
  69.         number darkness = 1 - ((1 - magnitude) / lightness_multiplier);
  70.         vec4 newPixel = darkenPixel(pixel, darkness, initial_lightness);
  71.         return newPixel;
  72.     }
  73. ]]
Advertisement
Add Comment
Please, Sign In to add comment