Added support for alpha channel in colorize node

This commit is contained in:
Rodolphe Suescun 2018-11-03 17:43:32 +01:00
parent ade213921c
commit c624e0f6ee
5 changed files with 69 additions and 26 deletions

View File

@ -16,8 +16,8 @@ func _get_shader_code(uv):
if variant_index == -1:
variant_index = generated_variants.size()
generated_variants.append(uv)
rv.code = src_code.code+"vec3 %s_%d_rgb = %s_gradient(%s);\n" % [ name, variant_index, name, src_code.f ]
rv.rgb = "%s_%d_rgb" % [ name, variant_index ]
rv.code = src_code.code+"vec4 %s_%d_rgba = %s_gradient(%s);\n" % [ name, variant_index, name, src_code.f ]
rv.rgba = "%s_%d_rgba" % [ name, variant_index ]
return rv
func _on_Control_updated(v):

View File

@ -3,11 +3,10 @@
[ext_resource path="res://addons/material_maker/nodes/colorize/colorize.gd" type="Script" id=1]
[ext_resource path="res://addons/material_maker/widgets/gradient_editor.tscn" type="PackedScene" id=2]
[sub_resource type="Theme" id=1]
[node name="Colorize" type="GraphNode"]
[node name="Colorize" type="GraphNode" index="0"]
anchor_left = 0.0
anchor_top = 0.0
@ -36,7 +35,7 @@ slot/0/left_type = 0
slot/0/left_color = Color( 0.756863, 0.756863, 0.756863, 1 )
slot/0/right_enabled = true
slot/0/right_type = 0
slot/0/right_color = Color( 0.5, 0.5, 1, 1 )
slot/0/right_color = Color( 0, 1, 0, 0.502745 )
script = ExtResource( 1 )
_sections_unfolded = [ "Theme", "slot", "slot/0" ]

View File

@ -4,7 +4,7 @@ class CustomSorter:
static func compare(a, b):
return a.v < b.v
var points = [ { v=0.0, c=Color(0.0, 0.0, 0.0) }, { v=1.0, c=Color(1.0, 1.0, 1.0) } ]
var points = [ { v=0.0, c=Color(0.0, 0.0, 0.0, 0.0) }, { v=1.0, c=Color(1.0, 1.0, 1.0, 1.0) } ]
var sorted = true
func _ready():
@ -46,12 +46,12 @@ func get_color(x):
# get_color_in_shader
func gcis(color):
return "vec3(%.9f,%.9f,%.9f)" % [color.r, color.g, color.b]
return "vec4(%.9f,%.9f,%.9f,%.9f)" % [color.r, color.g, color.b, color.a]
func get_shader(name):
sort()
var shader
shader = "vec3 "+name+"(float x) {\n"
shader = "vec4 "+name+"(float x) {\n"
shader += " if (x < %.9f) {\n" % points[0].v
shader += " return "+gcis(points[0].c)+";\n"
var s = points.size()-1
@ -72,7 +72,7 @@ func serialize():
sort()
var rv = []
for p in points:
rv.append({ pos=p.v, r=p.c.r, g=p.c.g, b=p.c.b })
rv.append({ pos=p.v, r=p.c.r, g=p.c.g, b=p.c.b, a=p.c.a })
rv = { type="Gradient", points=rv }
return rv
@ -80,7 +80,9 @@ func deserialize(v):
clear()
if typeof(v) == TYPE_ARRAY:
for i in v:
add_point(i.pos, Color(i.r, i.g, i.b))
if !i.has("a"): i.a = 1.0
add_point(i.pos, Color(i.r, i.g, i.b, i.a))
elif typeof(v) == TYPE_DICTIONARY && v.has("type") && v.type == "Gradient":
for i in v.points:
add_point(i.pos, Color(i.r, i.g, i.b))
if !i.has("a"): i.a = 1.0
add_point(i.pos, Color(i.r, i.g, i.b, i.a))

View File

@ -9,7 +9,7 @@ class GradientCursor:
func _ready():
rect_position = Vector2(0, 15)
rect_size = Vector2(WIDTH, 15)
func _gui_input(ev):
if ev is InputEventMouseButton:
if ev.button_index == BUTTON_LEFT && ev.doubleclick:
@ -30,11 +30,16 @@ class GradientCursor:
func set_color(c):
color = c
get_parent().update_value()
static func sort(a, b):
if a.get_position() < b.get_position():
return true
return false
func _draw():
var c = color
c.a = 1.0
draw_rect(Rect2(0, 0, rect_size.x, rect_size.y), c, false)
var value = null setget set_value
@ -49,7 +54,7 @@ func _ready():
func set_value(v):
value = v
for c in get_children():
if c != $Gradient:
if c != $Gradient && c != $Background:
remove_child(c)
c.free()
for p in value.points:
@ -59,7 +64,7 @@ func set_value(v):
func update_value():
value.clear()
for p in get_children():
if p != $Gradient:
if p != $Gradient && p != $Background:
value.add_point(p.rect_position.x/(rect_size.x-GradientCursor.WIDTH), p.color)
update_shader()
@ -94,6 +99,7 @@ func _on_Popup_popup_hide():
func get_sorted_cursors():
var array = get_children()
array.erase($Gradient)
array.erase($Background)
array.sort_custom(GradientCursor, "sort")
return array
@ -104,6 +110,6 @@ func update_shader():
var shader
shader = "shader_type canvas_item;\n"
shader += value.get_shader("gradient")
shader += "void fragment() { COLOR = vec4(gradient(UV.x), 1.0); }"
shader += "void fragment() { COLOR = gradient(UV.x); }"
$Gradient.material.shader.set_code(shader)
emit_signal("updated", value)

View File

@ -1,26 +1,42 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=7 format=2]
[ext_resource path="res://addons/material_maker/widgets/gradient_editor.gd" type="Script" id=1]
[sub_resource type="Shader" id=3]
code = "shader_type canvas_item;
void fragment() {
COLOR = vec4(vec3(2.0*fract(0.5*(floor(10.0*UV.x)+floor(2.0*UV.y)))), 1.0);
}"
[sub_resource type="ShaderMaterial" id=4]
render_priority = 0
shader = SubResource( 3 )
[sub_resource type="Shader" id=1]
code = "shader_type canvas_item;
vec3 gradient(float x) {
vec4 gradient(float x) {
if (x < 0.000000000) {
return vec3(0.000000000,0.000000000,0.000000000);
return vec4(0.000000000,0.000000000,0.000000000,0.000000000);
} else if (x < 1.000000000) {
return vec3(0.000000000,0.000000000,0.000000000)+x*vec3(1.000000000,1.000000000,1.000000000);
return vec4(0.000000000,0.000000000,0.000000000,0.000000000)+x*vec4(1.000000000,1.000000000,1.000000000,1.000000000);
}
return vec3(1.000000000,1.000000000,1.000000000);
return vec4(1.000000000,1.000000000,1.000000000,1.000000000);
}
void fragment() { COLOR = vec4(gradient((UV.x-0.041666667)*1.090909091), 1.0); }"
void fragment() { COLOR = gradient(UV.x); }"
[sub_resource type="ShaderMaterial" id=2]
render_priority = 0
shader = SubResource( 1 )
[node name="Control" type="Control" index="0"]
[sub_resource type="Theme" id=5]
[node name="Control" type="Control"]
anchor_left = 0.0
anchor_top = 0.0
@ -38,9 +54,9 @@ size_flags_vertical = 1
script = ExtResource( 1 )
_sections_unfolded = [ "Rect" ]
[node name="Gradient" type="ColorRect" parent="." index="0"]
[node name="Background" type="ColorRect" parent="." index="0"]
material = SubResource( 2 )
material = SubResource( 4 )
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
@ -57,6 +73,26 @@ size_flags_vertical = 1
color = Color( 1, 1, 1, 1 )
_sections_unfolded = [ "Material", "Rect" ]
[node name="Gradient" type="ColorRect" parent="." index="1"]
material = SubResource( 2 )
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 120.0
margin_bottom = 20.0
rect_min_size = Vector2( 120, 20 )
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 0
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 1
theme = SubResource( 5 )
color = Color( 1, 1, 1, 1 )
_sections_unfolded = [ "Material", "Theme" ]
[node name="Popup" type="Popup" parent="Gradient" index="0"]
visible = false
@ -95,7 +131,7 @@ size_flags_horizontal = 1
size_flags_vertical = 1
alignment = 0
color = Color( 1, 1, 1, 1 )
edit_alpha = false
edit_alpha = true
raw_mode = false
_sections_unfolded = [ "Rect" ]