Multimap multi-pattern - it's alive!

Also posted to pattern library. The version above uses more features, but is way more complex. This is a cleaned up, basic and simple version, for new folks to learn from.

/*
  Multiple SubMaps with multiple patterns v1.0 - by ScruffyNerf
  inspired by various code bits by zranger, Jeff Vyduna and Ben Hencke  

  Very Simple demo version, a truly minimal example (2 ranges, 2 patterns)
  There are lot of ways to do what this pattern does without using multiple maps, it's for building on.

  Requirements: Matrix with a 2D map
*/

//  these functions can take index,x,y and modify a set of globals to use instead
//     isInMap is set to true, if the pixel is in this submap
//     NewIndex is the "new" index value, if any [NOT USED IN EXAMPLE]
//     NewPixelCount is the pixelcount of the new map, if any [NOT USED IN EXAMPLE]
//     NewX is the new value of x for that pixel, might be renormalized, might not, still in range of 0..1
//     NewY is the new value of y for that pixel, might be renormalized, might not, still in range of 0..1
var isInMap
var NewIndex = 0;
var NewPixelCount = 0;
var NewX,NewY

// These are the map functions it will consider.
// Order matters, it will consider only the FIRST return of true
// This allows overlapping maps, with the first (0) being "on top", 
// and potentially this also allows a background/default map (matching none of the submaps listed)
var mapCount = 2 // number of maps+patterns
var maps = array(mapCount)
maps[0] = inmap0
maps[1] = inmap1
// maps[2] = your map functions go here

function inmap0(index, x, y) {
  centerx = x - 0.5;
  centery = y - 0.5;
	// circle from center of matrix
  if (centerx * centerx + centery * centery < 0.04) { 
    isInMap = true;
    NewX = centerx;
    NewY = centery;
  }
}

function inmap1(index, x, y) {
  if (x < .5 && y < .5) {
    isInMap = true;
  }
}

// if you are learning how this works, I recommend adding a new map first, and experimenting...
// then add a new pattern  (You'll want to just copy a pattern for the new map.  Map # matches Pattern # for simplicity )

var patterns = array(mapCount)
patterns[0] = pulseBlue
patterns[1] = pulseGreen
// patterns[2] = your pattern function(s) goes here

function pulseBlue(index,x,y){
  hsv(.6, 1, wave(time(.02)))
}

function pulseGreen(index,x,y) {
  hsv(.3, 1, wave(time(x*y+.03)))
}

export function render2D(index, x, y) {
  matched = false;
  NewPixelCount = pixelCount;
  NewIndex = index;
	NewX = x;
	NewY = y;
  for (var p = 0; p < mapCount; p++) {
    isInMap = false;
    maps[p](index, x, y);
    if (isInMap) {
      patterns[p](NewIndex,NewX,NewY);
      matched = true; // to avoid turning light off
      p = mapCount;  // stop the for loop
      }
  }
  if (matched == false) {
		// no match, no light
		hsv(0,0,0)
  }
}

// just so it'll render in 1D
export function render(index) {
    render2D(index, index / pixelCount, 0)
}
2 Likes