{
	"name": "tiler_color",
	"node_position": {
		"x": 0,
		"y": 0
	},
	"parameters": {
		"fixed_offset": 0.5,
		"offset": 0.25,
		"opacity": 0,
		"overlap": 1,
		"rotate": 45,
		"scale": 0.2,
		"scale_x": 1,
		"scale_y": 1,
		"select_inputs": 0,
		"tx": 4,
		"ty": 4,
		"variations": false
	},
	"seed_int": 0,
	"shader_model": {
		"code": [
			"vec3 $(name_uv)_random_color;",
			"vec4 $(name_uv)_tiled_output = tiler_$(name)($uv, vec2($tx, $ty), int($overlap), vec2(float($seed)), $(name_uv)_random_color);"
		],
		"global": "",
		"inputs": [
			{
				"default": "0.0",
				"function": true,
				"label": "",
				"longdesc": "The input image or atlas of 4 or 16 input images",
				"name": "in",
				"shortdesc": "Input",
				"type": "rgba"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "The mask applied to the pattern",
				"name": "mask",
				"shortdesc": "Mask",
				"type": "f"
			}
		],
		"instance": [
			"vec4 tiler_$(name)(vec2 uv, vec2 tile, int overlap, vec2 _seed, out vec3 random_color) {",
			"\tvec4 c = vec4(0.0);",
			"\tvec3 rc = vec3(0.0);",
			"\tvec3 rc1;",
			"\tfor (int dx = -overlap; dx <= overlap; ++dx) {",
			"\t\tfor (int dy = -overlap; dy <= overlap; ++dy) {",
			"\t\t\tvec2 pos = uv*tile+vec2(float(dx), float(dy)); pos = fract((floor(mod(pos, tile))+vec2(0.5))/tile)-vec2(0.5);",
			"\t\t\tvec2 seed = rand2(pos+_seed);",
			"\t\t\trc1 = rand3(seed);",
			"\t\t\tpos = fract(pos+vec2($fixed_offset/tile.x, 0.0)*floor(mod(pos.y*tile.y, 2.0))+$offset*seed/tile);",
			"\t\t\tfloat mask = $mask(fract(pos+vec2(0.5)));",
			"\t\t\tif (mask > 0.01) {",
			"\t\t\t\tvec2 pv = fract(uv - pos)-vec2(0.5);",
			"\t\t\t\tseed = rand2(seed);",
			"\t\t\t\tfloat angle = (seed.x * 2.0 - 1.0) * $rotate * 0.01745329251;",
			"\t\t\t\tfloat ca = cos(angle);",
			"\t\t\t\tfloat sa = sin(angle);",
			"\t\t\t\tpv = vec2(ca*pv.x+sa*pv.y, -sa*pv.x+ca*pv.y);",
			"\t\t\t\tpv *= (seed.y-0.5)*2.0*$scale+1.0;",
			"\t\t\t\tpv /= vec2($scale_x, $scale_y);",
			"\t\t\t\tpv += vec2(0.5);",
			"\t\t\t\tpv = clamp(pv, vec2(0.0), vec2(1.0));",
			"\t\t\t\t$select_inputs",
			"\t\t\t\tvec4 n = $in.variation(pv, $variations ? seed.x : 0.0);",
			"\t\t\t\tseed = rand2(seed);",
			"\t\t\t\tfloat na = n.a*mask*(1.0-$opacity*seed.x);",
			"\t\t\t\tfloat a = (1.0-c.a)*(1.0*na);",
			"\t\t\t\tc = mix(c, n, na);",
			"\t\t\t\trc = mix(rc, rc1, n.a);",
			"\t\t\t}",
			"\t\t}",
			"\t}",
			"\trandom_color = rc;",
			"\treturn c;",
			"}",
			""
		],
		"longdesc": "Tiles several occurences of an input image while adding randomness.",
		"name": "Color Tiler",
		"outputs": [
			{
				"longdesc": "Shows the generated pattern",
				"rgba": "$(name_uv)_tiled_output",
				"shortdesc": "Output",
				"type": "rgba"
			},
			{
				"longdesc": "Shows a random color for each instance of the input image",
				"rgb": "$(name_uv)_random_color",
				"shortdesc": "Instance Map",
				"type": "rgb"
			}
		],
		"parameters": [
			{
				"control": "None",
				"default": 4,
				"label": "Tile X",
				"longdesc": "The number of columns of the tiles pattern",
				"max": 64,
				"min": 1,
				"name": "tx",
				"shortdesc": "Tile.x",
				"step": 1,
				"type": "float"
			},
			{
				"control": "None",
				"default": 4,
				"label": "Tile Y",
				"longdesc": "The number of rows of the tiles pattern",
				"max": 64,
				"min": 1,
				"name": "ty",
				"shortdesc": "Tile.y",
				"step": 1,
				"type": "float"
			},
			{
				"control": "None",
				"default": 1,
				"label": "Overlap",
				"longdesc": "The number of neighbour tiles an instance of the input image can overlap. Set this parameter to the lowest value that generates the expected result (where all instances are fully visible) to improve performance.",
				"max": 5,
				"min": 0,
				"name": "overlap",
				"shortdesc": "Overlap",
				"step": 1,
				"type": "float"
			},
			{
				"default": 0,
				"label": "Inputs",
				"longdesc": "The input type of the node:\n- 1: single image\n- 4: atlas of 4 images\n- 16: atlas of 16 images\nAtlases can be created using the Tile2x2 node.",
				"name": "select_inputs",
				"shortdesc": "Input",
				"type": "enum",
				"values": [
					{
						"name": "1",
						"value": " "
					},
					{
						"name": "4",
						"value": "pv = clamp(0.5*(pv+floor(rand2(seed)*2.0)), vec2(0.0), vec2(1.0));"
					},
					{
						"name": "16",
						"value": "pv = clamp(0.25*(pv+floor(rand2(seed)*4.0)), vec2(0.0), vec2(1.0));"
					}
				]
			},
			{
				"control": "None",
				"default": 1,
				"label": "Scale X",
				"longdesc": "The scale of input images on the X axis",
				"max": 2,
				"min": 0,
				"name": "scale_x",
				"shortdesc": "Scale.x",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 1,
				"label": "Scale Y",
				"longdesc": "The scale of input images on the Y axis",
				"max": 2,
				"min": 0,
				"name": "scale_y",
				"shortdesc": "Scale.y",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0.5,
				"label": "Fixed Offset",
				"longdesc": "The relative offset of odd rows",
				"max": 1,
				"min": 0,
				"name": "fixed_offset",
				"shortdesc": "FixedOffset",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0.5,
				"label": "Rnd Offset",
				"max": 1,
				"min": 0,
				"name": "offset",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0,
				"label": "Rnd Rotate",
				"longdesc": "The random rotation applied to each image instance",
				"max": 180,
				"min": 0,
				"name": "rotate",
				"shortdesc": "RndRotate",
				"step": 0.1,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0,
				"label": "Rnd Scale",
				"longdesc": "The random scale applied to each image instance",
				"max": 1,
				"min": 0,
				"name": "scale",
				"shortdesc": "RndScale",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0.5,
				"label": "Rnd Opacity",
				"longdesc": "The random opacity applied to each image instance",
				"max": 1,
				"min": 0,
				"name": "opacity",
				"shortdesc": "RndOpacity",
				"step": 0.01,
				"type": "float"
			},
			{
				"default": false,
				"label": "Variations",
				"longdesc": "Check to tile variations of the input",
				"name": "variations",
				"shortdesc": "Variations",
				"type": "boolean"
			}
		],
		"shortdesc": "Color Tiler"
	},
	"type": "shader"
}