ldirko

Metaballs 80fps

Jul 22nd, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Metaballs
  2. //16x16 rgb led matrix demo
  3. //Yaroslaw Turbin 20.07.2020
  4.  
  5. //https://www.reddit.com/user/ldirko/
  6. //https://www.reddit.com/user/sutaburosu/
  7.  
  8. //thx to u/sutaburosu/ for incredible speedup with square table!!!
  9.  
  10. #include "FastLED.h"
  11.  
  12. // Matrix size
  13. #define NUM_ROWS 16
  14. #define NUM_COLS 16
  15. #define NUM_LEDS (NUM_ROWS * NUM_COLS)
  16.  
  17.  
  18.  
  19.  
  20. // LEDs pin
  21. #define DATA_PIN 3
  22.  
  23. // LED brightness
  24. #define BRIGHTNESS 255
  25.  
  26.  
  27. // Define the array of leds
  28. CRGB leds[NUM_LEDS];
  29.  
  30. DEFINE_GRADIENT_PALETTE( lava) {
  31.   0,      255,  30,  0,
  32.   32,     0,    0,   0,
  33.   128,    0,    0,   0,
  34.   150,    180,  180, 0,
  35.   255,    255,  50,  0
  36. };
  37.  
  38. CRGBPalette16 myPal = lava;
  39. byte sqrt_LUT[NUM_ROWS][NUM_COLS];
  40.  
  41.  
  42. void setup() {
  43.   Serial.begin(250000);
  44.    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  45.   FastLED.setBrightness(BRIGHTNESS);
  46.   for (uint8_t i = 0; i < NUM_COLS; i++)
  47.     for (uint8_t j = 0; j < NUM_ROWS; j++)
  48.       sqrt_LUT[i][j] = dist(0, 0, i, j);
  49. }
  50.  
  51.  
  52. void loop() {
  53.  
  54.   uint8_t bx1 = beatsin8(15, 0, NUM_COLS - 1, 0, 0);
  55.   uint8_t by1 = beatsin8(18, 0, NUM_ROWS - 1, 0, 0);
  56.   uint8_t bx2 = beatsin8(28, 0, NUM_COLS - 1, 0, 32);
  57.   uint8_t by2 = beatsin8(23, 0, NUM_ROWS - 1, 0, 32);
  58.   uint8_t bx3 = beatsin8(30, 0, NUM_COLS - 1, 0, 64);
  59.   uint8_t by3 = beatsin8(24, 0, NUM_ROWS - 1, 0, 64);
  60.   uint8_t bx4 = beatsin8(17, 0, NUM_COLS - 1, 0, 128);
  61.   uint8_t by4 = beatsin8(25, 0, NUM_ROWS - 1, 0, 128);
  62.   uint8_t bx5 = beatsin8(19, 0, NUM_COLS - 1, 0, 170);
  63.   uint8_t by5 = beatsin8(21, 0, NUM_ROWS - 1, 0, 170);
  64.  
  65.   for (uint8_t i = 0; i < NUM_COLS; i++)    {
  66.     for (uint8_t j = 0; j < NUM_ROWS; j++) {
  67.  
  68.       uint16_t sum =   distLUT(i, j, bx1, by1);
  69.       sum = qadd8(sum, distLUT(i, j, bx2, by2));
  70.       sum = qadd8(sum, distLUT(i, j, bx3, by3));
  71.       sum = qadd8(sum, distLUT(i, j, bx4, by4));
  72.       sum = qadd8(sum, distLUT(i, j, bx5, by5));
  73.  
  74.       byte col =  constrain(sum, 128, 255);
  75.       leds[XY (i, j)] =  ColorFromPalette(myPal, col, BRIGHTNESS);
  76.  
  77.     }
  78.   }
  79.  
  80.   FastLED.show();
  81.   static int frame = 0;
  82.   if (frame++ % 32 == 0)
  83.     Serial.println(FastLED.getFPS());
  84. } //loop
  85.  
  86.  
  87. byte distLUT (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  88.   byte a = abs8(y2 - y1);
  89.   byte b = abs8(x2 - x1);
  90.   return sqrt_LUT[a][b];
  91. }
  92.  
  93. byte dist (uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)  {
  94.   byte a = y2 - y1;
  95.   byte b = x2 - x1;
  96.   a *= a;
  97.   b *= b;
  98.   byte dist = 220;
  99.   if (a + b > 0)
  100.     dist = 220.0 / sqrt16(a + b);
  101.   return dist;
  102. }
  103.  
  104. uint16_t XY (uint8_t x, uint8_t y) { return (y * NUM_COLS + x);}
Add Comment
Please, Sign In to add comment