// Copyright (c) 2023 Lachlan McDonald
// This work is licensed under the MIT License (MIT)
// https://github.com/lachlanmcdonald/magicavoxel-shaders
//
// xs brush/zigzag3 [Direction] [Width A] [Width B] [Width C]
//
// xs_begin
// author : '@lmcdx.bsky.social'
// arg : { name = 'Direction'  var = 'm_direction'  range = '0 3'  value = '0'  step = '1'  precision = '0' }
// arg : { name = 'Width A'  var = 'm_width_a'  range = '1 256'  value = '2'  step = '1'  precision = '0' }
// arg : { name = 'Width B'  var = 'm_width_b'  range = '1 256'  value = '2'  step = '1'  precision = '0' }
// arg : { name = 'Width C'  var = 'm_width_c'  range = '1 256'  value = '2'  step = '1'  precision = '0' }
// xs_end

int direction = int(m_direction);
float width_a = m_width_a;
float width_b = m_width_b;
float width_c = m_width_c;
float color_a = i_args[4];
float color_b = i_args[5];
float color_c = i_args[6];

float map(vec3 v) {
	v = floor(v);

	if (direction == 1) {
		v.y = -v.y;
	} else if (direction == 2) {
		v.x = -v.x;
		v.y = -v.y;
	} else if (direction == 3) {
		v.x = -v.x;
	}

	float size = width_a + width_b + width_c;
	float row = floor(v.y / size);
	float col = floor(v.x / size);
	vec2 k = v.xy - vec2(col, row) * size;

	if (mod(row + col, 2.0) == 0.0) {
		if (k.x < width_a && k.y < width_a) {
			return color_a;
		} else if (k.x < (width_a + width_b) && k.y < (width_a + width_b)) {
			return color_b;
		} else {
			return color_c;
		}
	} else {
		if (k.x < width_a || k.y < width_a) {
			return color_a;
		} else if (k.x < (width_a + width_b) || k.y < (width_a + width_b)) {
			return color_b;
		} else {
			return color_c;
		}
	}
}