{
	"name": "tiler_advanced",
	"node_position": {
		"x": 0,
		"y": 0
	},
	"parameters": {
		"inputs": 0,
		"overlap": 1,
		"rotate": 0,
		"scale_x": 1,
		"scale_y": 1,
		"translate_x": 0,
		"translate_y": 0,
		"tx": 4,
		"ty": 4,
		"variations": false
	},
	"seed_int": 0,
	"shader_model": {
		"code": [
			"vec3 $(name_uv)_instance_uv = vec3(0.0);",
			"vec4 $(name_uv)_rch = tiler_$(name)($uv, vec2($tx, $ty), int($overlap), $(name_uv)_instance_uv, float($seed));"
		],
		"global": "",
		"includes": [
			"custom_uv"
		],
		"inputs": [
			{
				"default": "0.0",
				"function": true,
				"label": "",
				"longdesc": "The input image or atlas of 4 or 16 input images",
				"name": "in",
				"shortdesc": "Input",
				"type": "f"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "The mask applied to the pattern",
				"name": "mask",
				"shortdesc": "Mask",
				"type": "f"
			},
			{
				"default": "vec4(rand2($uv+vec2(float($seed))), rand2($uv-vec2(float($seed))))",
				"label": "",
				"longdesc": "An input color map used to generate the Instance map 1 output",
				"name": "color1",
				"shortdesc": "Color map 1",
				"type": "rgba"
			},
			{
				"default": "vec4(rand2(-$uv+vec2(float($seed))), rand2(-$uv-vec2(float($seed))))",
				"label": "",
				"longdesc": "An input color map used to generate the Instance map 2 output",
				"name": "color2",
				"shortdesc": "Color map 2",
				"type": "rgba"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "5:",
				"longdesc": "A map for translation along the X axis",
				"name": "tr_x",
				"shortdesc": "Translate map X",
				"type": "f"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "A map for translation along the Y axis",
				"name": "tr_y",
				"shortdesc": "Translate map Y",
				"type": "f"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "A map for rotation",
				"name": "r",
				"shortdesc": "Rotate map",
				"type": "f"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "A map for scale along the X axis",
				"name": "sc_x",
				"shortdesc": "Scale map X",
				"type": "f"
			},
			{
				"default": "1.0",
				"function": true,
				"label": "",
				"longdesc": "A map for scale along the Y axis",
				"name": "sc_y",
				"shortdesc": "Scale map Y",
				"type": "f"
			}
		],
		"instance": [
			"vec4 tiler_$(name)(vec2 uv, vec2 tile, int overlap, inout vec3 instance_uv, float _seed) {",
			"\tfloat c = 0.0;",
			"\tvec2 map_uv = vec2(0.0);",
			"\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\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\tpos = fract(pos+vec2(0.5));",
			"\t\t\t\tpv -= vec2($translate_x*$tr_x(pos), $translate_y*$tr_y(pos))/tile;",
			"\t\t\t\tfloat angle = $r(pos) * $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 /= vec2($scale_x*$sc_x(pos), $scale_y*$sc_y(pos));",
			"\t\t\t\tpv += vec2(0.5);",
			"\t\t\t\tvec2 clamped_pv = clamp(pv, vec2(0.0), vec2(1.0));",
			"\t\t\t\tif (pv.x != clamped_pv.x || pv.y != clamped_pv.y) {",
			"\t\t\t\t\tcontinue;",
			"\t\t\t\t}",
			"\t\t\t\tvec2 seed = rand2(vec2(_seed)+pos);",
			"\t\t\t\tvec2 full_uv = pv;",
			"\t\t\t\tpv = get_from_tileset($inputs, seed.x, pv);",
			"\t\t\t\tfloat c1 = $in.variation(pv, $variations ? seed.x : 0.0)*mask;",
			"\t\t\t\tc = max(c, c1);",
			"\t\t\t\tmap_uv = mix(map_uv, pos, step(c, c1));",
			"\t\t\t\tinstance_uv = mix(instance_uv, vec3(full_uv, seed.x), step(c, c1));",
			"\t\t\t}",
			"\t\t}",
			"\t}",
			"\treturn vec4(map_uv, 0.0, c);",
			"}"
		],
		"longdesc": "Tiles several occurences of an input image while adding randomness.",
		"name": "Advanced Tiler",
		"outputs": [
			{
				"f": "$(name_uv)_rch.a",
				"longdesc": "Shows the generated pattern",
				"shortdesc": "Output",
				"type": "f"
			},
			{
				"longdesc": "Shows a color for each instance of the input image",
				"rgba": "$color1($(name_uv)_rch.rg)",
				"shortdesc": "Instance map 1",
				"type": "rgba"
			},
			{
				"longdesc": "Shows a color for each instance of the input image",
				"rgba": "$color2($(name_uv)_rch.rg)",
				"shortdesc": "Instance map 2",
				"type": "rgba"
			},
			{
				"longdesc": "Per instance UVs. Can be used with Custom UV node.",
				"rgb": "$(name_uv)_instance_uv",
				"shortdesc": "Instance UV",
				"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": "inputs",
				"shortdesc": "Input",
				"type": "enum",
				"values": [
					{
						"name": "1",
						"value": " 1.0"
					},
					{
						"name": "4",
						"value": "2.0"
					},
					{
						"name": "16",
						"value": "4.0"
					}
				]
			},
			{
				"control": "None",
				"default": 0,
				"label": "Translate X",
				"longdesc": "The translation along the X axis applied to the instances",
				"max": 1,
				"min": 0,
				"name": "translate_x",
				"shortdesc": "Translate.x",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0,
				"label": "Translate Y",
				"longdesc": "The translation along the Y axis applied to the instances",
				"max": 1,
				"min": 0,
				"name": "translate_y",
				"shortdesc": "Translate.y",
				"step": 0.01,
				"type": "float"
			},
			{
				"control": "None",
				"default": 0,
				"label": "Rotate",
				"longdesc": "The angle of instances of the input",
				"max": 180,
				"min": 0,
				"name": "rotate",
				"shortdesc": "Rotate",
				"step": 0.1,
				"type": "float"
			},
			{
				"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"
			},
			{
				"default": false,
				"label": "Variations",
				"longdesc": "Check to tile variations of the input",
				"name": "variations",
				"shortdesc": "Variations",
				"type": "boolean"
			}
		],
		"shortdesc": "Tiler"
	},
	"type": "shader"
}