-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwatchdMaterial.gd
73 lines (63 loc) · 2.47 KB
/
SwatchdMaterial.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
tool
extends ShaderMaterial
class_name SwatchdMaterial
export(Resource) var swatch setget set_swatch
export(int) var color_index setget set_color_index
export(String) var color_name setget set_color_name
func set_swatch_index(i:int) -> void:
# print ("[SwatchdMaterial] set_swatch_index " + str(i))
self.set_shader_param("color_index", i)
func apply_texture_index() -> void:
# print ("[SwatchdMaterial] apply_texture_index")
if swatch:
if color_name != null && !color_name.empty():
if swatch.has_named_color(color_name):
var texture_index = swatch.get_texture_index_for_name(color_name)
set_swatch_index(texture_index)
else:
print ("[SwatchdMaterial] color name not found: " + str(color_name))
elif color_index >= 0 && color_index < swatch.colors.size():
set_swatch_index(color_index)
#self.get_parent().material_override.albedo_color = color
else:
pass
#print ("[SwatchdMaterial] apply_color " + str(color_index) + " is out of range [0, " + str(swatch.colors.size() - 1) + "]")
func on_swatch_changed() -> void:
# print ("[SwatchdMaterial] on_swatch_changed resource_name " + self.resource_path)
self.set_shader_param("texture_albedo", swatch.cached_texture())
apply_texture_index()
func set_swatch(new_swatch) -> void:
# print ("[SwatchdMaterial] set_swatch.")
# Disconnect old swatch's signal
if swatch:
if swatch.is_connected("changed", self, "on_swatch_changed"):
swatch.disconnect("changed", self, "on_swatch_changed")
# Connect new swatch's signal
if (new_swatch is Swatchd):
swatch = new_swatch
self.set_shader_param("texture_albedo", swatch.cached_texture())
if not swatch.is_connected("changed", self, "on_swatch_changed"):
var e = swatch.connect("changed", self, "on_swatch_changed")
assert(e == 0)
elif new_swatch == null:
swatch = new_swatch
else:
print ("[SwatchdApplier] set_swatch resource is not a swatch.")
print (new_swatch)
func set_color_index(color_index_) -> void:
# print ("[SwatchdMaterial] set_color_index.")
color_index = color_index_
apply_texture_index()
func set_color_name(color_name_) -> void:
# print ("[SwatchdMaterial] set_color_name.")
color_name = color_name_
apply_texture_index()
func _ready() -> void:
# print ("[SwatchdMaterial] ready")
apply_texture_index()
func _exit_tree() -> void:
# print ("[SwatchdMaterial] _exit_tree")
# Disconnect swatch's signal
if swatch:
if swatch.is_connected("changed", self, "on_swatch_changed"):
swatch.disconnect("changed", self, "on_swatch_changed")