extends Sprite2D
class_name SpriteElement

func _ready() -> void:
	var shader_material = ShaderMaterial.new()
	var shader = load("res://Resources/Shader/TexturePalette.gdshader")
	if shader:
		shader_material.shader = shader
		# 将着色器材质设置为当前精灵的材质
		material = shader_material
	else:
		push_error("TexturePalette.gdshader 未找到！")

func _process(_delta: float) -> void:
	pass

func set_shader_param(palette_setting):
	var palette_count = palette_setting.size()
	
	var basic_colors = []
	var modulate_colors = []
	var tolerances = []
	var blend_modes = []
	
	for setting in palette_setting:
		var color = setting["BasicColor"]
		basic_colors.append(Color(color["R"], color["G"], color["B"], color["A"]))
		color = setting["ModulateColor"]
		modulate_colors.append(Color(color["R"], color["G"], color["B"], color["A"]))
		tolerances.append(setting["Tolerance"])
		blend_modes.append(int(setting["BlendMode"]))
	
	material.set_shader_parameter("PaletteCount", palette_count)
	material.set_shader_parameter("BasicColor", PackedColorArray(basic_colors))
	material.set_shader_parameter("ModulateColor", PackedColorArray(modulate_colors))
	material.set_shader_parameter("Tolerance", PackedFloat32Array(tolerances))
	material.set_shader_parameter("BlendMode", PackedInt32Array(blend_modes))
	queue_redraw()
