group = "Sampling"
display_name = "Flipbook Sample Texture"
inputs = {
	"c85ccdaf-b0e2-4526-a617-2b14f402f43c" = { name = "uv" display_name = "UV" type = "vector2" }
	"4571da41-20ef-47cc-9811-7ed4d59cd85a" = { name = "mip_level" is_required = false display_name = "Mip Level" type = { scalar: ["HAS_MIPLEVEL"] } }
	"1fd6339d-ba9f-4ac8-b463-4a49f5f3a90e" = { name = "fps" display_name = "FPS" is_required = false type = {scalar: ["HAS_FPS"]} }
	"42b961f8-1b7f-4f41-98ce-11b98adc05e4" = { name = "time" display_name = "Time" type = "scalar" }
	"7c9904c4-2039-451b-a38a-5f77ec69f878" = { name = "sprite_rows" display_name = "Sprite Rows" is_required = false type = { scalar: ["HAS_ROWS"] }}
	"eee52ea3-790c-40e0-ab60-c53308bc4043" = { name = "sprite_cols" display_name = "Sprite Columns" is_required = false type = { scalar: ["HAS_COLS"] }}
}

domain = "pixel"
output = {
	type = "float4"
}

options = {
	"acb6ef9d-5ba0-42e4-85f3-2924b4b4be25" = "ADDRESS_CLAMP"
	"5dd59b3d-1762-4a14-9930-7500230ef3db" = "ADDRESS_WRAP"
	"f669a3a6-0376-4187-840e-80000e2939d5" = "FILTER_LINEAR"
	"43dea0e2-a77d-410d-88bb-945dac9139d8" = "FILTER_POINT"
	"1e067464-12d8-4826-9b72-cfd5765003e3" = "FILTER_ANISOTROPIC"
	"fb3f709b-a54a-4e93-ac9f-e9fc76fb8bcd" = "SRGB"
    "43710e4f-f52a-4038-8ec8-d6cb0546103b" = "RGBM_DECODE"
    "e94e53e6-49b6-4194-a747-8f064a5932e0" = "LINEAR"
    "0268506C-B417-49DC-BBBE-3D5949595940" = "FLIP_GREEN"
}

ui = [
	{
		type = "drop_down"
		display_name = "Encoding"
		options = {
			"Linear Color" = "e94e53e6-49b6-4194-a747-8f064a5932e0"
			"sRGB Color" = "fb3f709b-a54a-4e93-ac9f-e9fc76fb8bcd"
			"RGBM Color" = "43710e4f-f52a-4038-8ec8-d6cb0546103b"
		}
		default = "fb3f709b-a54a-4e93-ac9f-e9fc76fb8bcd"
	}
	{
		type = "drop_down"
		display_name = "Address mode"
		options = {
			"Clamp" = "acb6ef9d-5ba0-42e4-85f3-2924b4b4be25"
			"Wrap" = "5dd59b3d-1762-4a14-9930-7500230ef3db"
		}
		default = "5dd59b3d-1762-4a14-9930-7500230ef3db"
	}
	{
		type = "drop_down"
		display_name = "Filter mode"
		options = {
			"Anisotropic" = "1e067464-12d8-4826-9b72-cfd5765003e3"
			"Linear" = "f669a3a6-0376-4187-840e-80000e2939d5"
			"Point" = "43dea0e2-a77d-410d-88bb-945dac9139d8"
		}
		default = "1e067464-12d8-4826-9b72-cfd5765003e3"
	}
	{ type = "checkbox" display_name = "Invert Green Channel" 	option = "0268506C-B417-49DC-BBBE-3D5949595940" }
]

code_blocks = {
	default = {
		include: ["misc"]
		language = "hlsl"
		samplers = {
			texture_map = {
				display_name = "Texture"
				type = "2d"
				sampler_state = "core/shader_nodes/graph_common#default_node_sampler"
				source = "material"
			}
		}

		code = """
			#if defined(HAS_FPS)
				float frame_per_sec = fps;
			#else
				float frame_per_sec = 5.0;
			#endif

			#if defined(HAS_ROWS)
				float sprite_per_row = sprite_rows;
			#else
				float sprite_per_row = 2.0;
			#endif

			#if defined(HAS_COLS)
				float sprite_per_col = sprite_cols;
			#else
				float sprite_per_col = 2.0;
			#endif

			float frame_value = frame_per_sec * time;

			float frame = fmod(frame_value, sprite_per_row * sprite_per_col);
			float lerp_value = frac(frame);
			float current_frame = floor(frame);
			float next_frame = floor(fmod(frame_value + 1.0, sprite_per_row * sprite_per_col));

			float2 sprites_dim = float2(sprite_per_row, sprite_per_col);
			float2 current_uv 	= calculate_uv(uv, sprites_dim, current_frame);
			float2 next_uv 		= calculate_uv(uv, sprites_dim, next_frame);

			// sample textures

			float4 result_current_frame;
			#if defined(HAS_MIPLEVEL)
				result_current_frame = sample_texture_mip(texture_map, current_uv, mip_level);
			#else
				result_current_frame = sample_texture(texture_map, current_uv);
			#endif

			float4 result_next_frame;
			#if defined(HAS_MIPLEVEL)
				result_next_frame = sample_texture_mip(texture_map, next_uv, mip_level);
			#else
				result_next_frame = sample_texture(texture_map, next_uv);
			#endif

			float4 result = lerp(result_current_frame, result_next_frame, lerp_value);
		    RESULT(result);
		"""
	}

	misc = {
		language = "hlsl"
		code="""
			inline float2 calculate_uv(float2 uv, float2 sprites_dim, float frame) 
			{
				float sprite_u = fmod(frame, sprites_dim.x) / sprites_dim.x;
				float sprite_v = floor(frame / sprites_dim.x) / sprites_dim.y;

				sprite_u += uv.x / sprites_dim.x;
				sprite_v += uv.y / sprites_dim.y;
				float2 texcoord = float2(sprite_u, sprite_v);

				return texcoord;
			}

			inline float4 sample_texture(Sampler2D texture_map, float2 texcoord) 
			{
			    float4 result;
				result = TEX2D(texture_map, texcoord);
					
				#if defined(FLIP_GREEN)
					result.y = 1.0-result.y;
				#endif

				#if defined(RENDERER_GL) && defined(SRGB)
					result = fast_gamma_to_linear_rgb(result);
				#endif

				#if defined(RGBM_DECODE)
					result = float4(rgbm_decode(result), result.a);
				#endif

				return result;
			}

			inline float4 sample_texture_mip(Sampler2D texture_map, float2 texcoord, float mip_level) 
			{
			    float4 result;
				result = TEX2DLOD(texture_map, texcoord, mip_level);
					
				#if defined(FLIP_GREEN)
					result.y = 1.0-result.y;
				#endif

				#if defined(RENDERER_GL) && defined(SRGB)
					result = fast_gamma_to_linear_rgb(result);
				#endif

				#if defined(RGBM_DECODE)
					result = float4(rgbm_decode(result), result.a);
				#endif

				return result;
			}
		"""
	}
}



