Skip to content

Commit fb81777

Browse files
committed
Merge pull request godotengine#110385 from WhalesState/move-picker-shaders
Move ColorPicker shaders to ColorPickerShape class
2 parents b137476 + a07bd3f commit fb81777

File tree

5 files changed

+129
-127
lines changed

5 files changed

+129
-127
lines changed

scene/gui/color_picker.cpp

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include "scene/resources/style_box_flat.h"
5353
#include "scene/resources/style_box_texture.h"
5454
#include "scene/theme/theme_db.h"
55-
#include "thirdparty/misc/ok_color_shader.h"
5655

5756
static inline bool is_color_overbright(const Color &color) {
5857
return (color.r > 1.0) || (color.g > 1.0) || (color.b > 1.0);
@@ -255,117 +254,6 @@ void ColorPicker::_update_theme_item_cache() {
255254
theme_cache.base_scale = get_theme_default_base_scale();
256255
}
257256

258-
void ColorPicker::init_shaders() {
259-
wheel_shader.instantiate();
260-
wheel_shader->set_code(R"(
261-
// ColorPicker wheel shader.
262-
263-
shader_type canvas_item;
264-
265-
uniform float wheel_radius = 0.42;
266-
267-
void fragment() {
268-
float x = UV.x - 0.5;
269-
float y = UV.y - 0.5;
270-
float a = atan(y, x);
271-
x += 0.001;
272-
y += 0.001;
273-
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
274-
x -= 0.002;
275-
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
276-
y -= 0.002;
277-
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
278-
x += 0.002;
279-
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
280-
281-
COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
282-
}
283-
)");
284-
285-
circle_shader.instantiate();
286-
circle_shader->set_code(R"(
287-
// ColorPicker circle shader.
288-
289-
shader_type canvas_item;
290-
291-
uniform float v = 1.0;
292-
293-
void fragment() {
294-
float x = UV.x - 0.5;
295-
float y = UV.y - 0.5;
296-
float a = atan(y, x);
297-
x += 0.001;
298-
y += 0.001;
299-
float b = float(sqrt(x * x + y * y) < 0.5);
300-
x -= 0.002;
301-
float b2 = float(sqrt(x * x + y * y) < 0.5);
302-
y -= 0.002;
303-
float b3 = float(sqrt(x * x + y * y) < 0.5);
304-
x += 0.002;
305-
float b4 = float(sqrt(x * x + y * y) < 0.5);
306-
307-
COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
308-
})");
309-
310-
circle_ok_color_shader.instantiate();
311-
circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
312-
// ColorPicker ok color hsl circle shader.
313-
314-
uniform float ok_hsl_l = 1.0;
315-
316-
void fragment() {
317-
float x = UV.x - 0.5;
318-
float y = UV.y - 0.5;
319-
float h = atan(y, x) / (2.0 * M_PI);
320-
float s = sqrt(x * x + y * y) * 2.0;
321-
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
322-
x += 0.001;
323-
y += 0.001;
324-
float b = float(sqrt(x * x + y * y) < 0.5);
325-
x -= 0.002;
326-
float b2 = float(sqrt(x * x + y * y) < 0.5);
327-
y -= 0.002;
328-
float b3 = float(sqrt(x * x + y * y) < 0.5);
329-
x += 0.002;
330-
float b4 = float(sqrt(x * x + y * y) < 0.5);
331-
COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
332-
})");
333-
334-
rectangle_ok_color_hs_shader.instantiate();
335-
rectangle_ok_color_hs_shader->set_code(OK_COLOR_SHADER + R"(
336-
// ColorPicker ok color hs rectangle shader.
337-
338-
uniform float ok_hsl_l = 0.0;
339-
340-
void fragment() {
341-
float h = UV.x;
342-
float s = 1.0 - UV.y;
343-
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
344-
COLOR = vec4(col, 1.0);
345-
})");
346-
347-
rectangle_ok_color_hl_shader.instantiate();
348-
rectangle_ok_color_hl_shader->set_code(OK_COLOR_SHADER + R"(
349-
// ColorPicker ok color hl rectangle shader.
350-
351-
uniform float ok_hsl_s = 0.0;
352-
353-
void fragment() {
354-
float h = UV.x;
355-
float l = 1.0 - UV.y;
356-
vec3 col = okhsl_to_srgb(vec3(h, ok_hsl_s, l));
357-
COLOR = vec4(col, 1.0);
358-
})");
359-
}
360-
361-
void ColorPicker::finish_shaders() {
362-
wheel_shader.unref();
363-
circle_shader.unref();
364-
circle_ok_color_shader.unref();
365-
rectangle_ok_color_hs_shader.unref();
366-
rectangle_ok_color_hl_shader.unref();
367-
}
368-
369257
void ColorPicker::set_focus_on_line_edit() {
370258
callable_mp((Control *)c_text, &Control::grab_focus).call_deferred(false);
371259
}

scene/gui/color_picker.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,6 @@ class ColorPicker : public VBoxContainer {
168168
};
169169

170170
private:
171-
static inline Ref<Shader> wheel_shader;
172-
static inline Ref<Shader> circle_shader;
173-
static inline Ref<Shader> circle_ok_color_shader;
174-
static inline Ref<Shader> rectangle_ok_color_hs_shader;
175-
static inline Ref<Shader> rectangle_ok_color_hl_shader;
176171
static inline List<Color> preset_cache;
177172
static inline List<Color> recent_preset_cache;
178173

@@ -434,9 +429,6 @@ class ColorPicker : public VBoxContainer {
434429
HSlider *get_slider(int idx);
435430
Vector<float> get_active_slider_values();
436431

437-
static void init_shaders();
438-
static void finish_shaders();
439-
440432
void add_mode(ColorMode *p_mode);
441433
void add_shape(ColorPickerShape *p_shape);
442434

scene/gui/color_picker_shape.cpp

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,118 @@
3232

3333
#include "scene/gui/margin_container.h"
3434
#include "scene/resources/material.h"
35+
#include "thirdparty/misc/ok_color_shader.h"
36+
37+
void ColorPickerShape::init_shaders() {
38+
wheel_shader.instantiate();
39+
wheel_shader->set_code(R"(
40+
// ColorPicker wheel shader.
41+
42+
shader_type canvas_item;
43+
44+
uniform float wheel_radius = 0.42;
45+
46+
void fragment() {
47+
float x = UV.x - 0.5;
48+
float y = UV.y - 0.5;
49+
float a = atan(y, x);
50+
x += 0.001;
51+
y += 0.001;
52+
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
53+
x -= 0.002;
54+
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
55+
y -= 0.002;
56+
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
57+
x += 0.002;
58+
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > wheel_radius);
59+
60+
COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
61+
}
62+
)");
63+
64+
circle_shader.instantiate();
65+
circle_shader->set_code(R"(
66+
// ColorPicker circle shader.
67+
68+
shader_type canvas_item;
69+
70+
uniform float v = 1.0;
71+
72+
void fragment() {
73+
float x = UV.x - 0.5;
74+
float y = UV.y - 0.5;
75+
float a = atan(y, x);
76+
x += 0.001;
77+
y += 0.001;
78+
float b = float(sqrt(x * x + y * y) < 0.5);
79+
x -= 0.002;
80+
float b2 = float(sqrt(x * x + y * y) < 0.5);
81+
y -= 0.002;
82+
float b3 = float(sqrt(x * x + y * y) < 0.5);
83+
x += 0.002;
84+
float b4 = float(sqrt(x * x + y * y) < 0.5);
85+
86+
COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
87+
})");
88+
89+
circle_ok_color_shader.instantiate();
90+
circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
91+
// ColorPicker ok color hsl circle shader.
92+
93+
uniform float ok_hsl_l = 1.0;
94+
95+
void fragment() {
96+
float x = UV.x - 0.5;
97+
float y = UV.y - 0.5;
98+
float h = atan(y, x) / (2.0 * M_PI);
99+
float s = sqrt(x * x + y * y) * 2.0;
100+
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
101+
x += 0.001;
102+
y += 0.001;
103+
float b = float(sqrt(x * x + y * y) < 0.5);
104+
x -= 0.002;
105+
float b2 = float(sqrt(x * x + y * y) < 0.5);
106+
y -= 0.002;
107+
float b3 = float(sqrt(x * x + y * y) < 0.5);
108+
x += 0.002;
109+
float b4 = float(sqrt(x * x + y * y) < 0.5);
110+
COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
111+
})");
112+
113+
rectangle_ok_color_hs_shader.instantiate();
114+
rectangle_ok_color_hs_shader->set_code(OK_COLOR_SHADER + R"(
115+
// ColorPicker ok color hs rectangle shader.
116+
117+
uniform float ok_hsl_l = 0.0;
118+
119+
void fragment() {
120+
float h = UV.x;
121+
float s = 1.0 - UV.y;
122+
vec3 col = okhsl_to_srgb(vec3(h, s, ok_hsl_l));
123+
COLOR = vec4(col, 1.0);
124+
})");
125+
126+
rectangle_ok_color_hl_shader.instantiate();
127+
rectangle_ok_color_hl_shader->set_code(OK_COLOR_SHADER + R"(
128+
// ColorPicker ok color hl rectangle shader.
129+
130+
uniform float ok_hsl_s = 0.0;
131+
132+
void fragment() {
133+
float h = UV.x;
134+
float l = 1.0 - UV.y;
135+
vec3 col = okhsl_to_srgb(vec3(h, ok_hsl_s, l));
136+
COLOR = vec4(col, 1.0);
137+
})");
138+
}
139+
140+
void ColorPickerShape::finish_shaders() {
141+
wheel_shader.unref();
142+
circle_shader.unref();
143+
circle_ok_color_shader.unref();
144+
rectangle_ok_color_hs_shader.unref();
145+
rectangle_ok_color_hl_shader.unref();
146+
}
35147

36148
void ColorPickerShape::_emit_color_changed() {
37149
color_picker->emit_signal(SNAME("color_changed"), color_picker->color);
@@ -698,7 +810,7 @@ void ColorPickerShapeWheel::_initialize_controls() {
698810

699811
Ref<ShaderMaterial> material;
700812
material.instantiate();
701-
material->set_shader(ColorPicker::wheel_shader);
813+
material->set_shader(ColorPickerShape::wheel_shader);
702814
material->set_shader_parameter("wheel_radius", WHEEL_RADIUS);
703815

704816
wheel = memnew(Control);

scene/gui/color_picker_shape.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class ColorPickerShape : public Object {
3838
void _emit_color_changed();
3939

4040
protected:
41+
static inline Ref<Shader> wheel_shader;
42+
static inline Ref<Shader> circle_shader;
43+
static inline Ref<Shader> circle_ok_color_shader;
44+
static inline Ref<Shader> rectangle_ok_color_hs_shader;
45+
static inline Ref<Shader> rectangle_ok_color_hl_shader;
46+
4147
ColorPicker *color_picker = nullptr;
4248
bool is_dragging = false;
4349

@@ -68,6 +74,9 @@ class ColorPickerShape : public Object {
6874
bool cursor_editing = false;
6975
float echo_multiplier = 1;
7076

77+
static void init_shaders();
78+
static void finish_shaders();
79+
7180
virtual String get_name() const = 0;
7281
virtual Ref<Texture2D> get_icon() const = 0;
7382
virtual bool is_ok_hsl() const { return false; }
@@ -113,7 +122,7 @@ class ColorPickerShapeOKHSRectangle : public ColorPickerShape {
113122
Control *square = nullptr;
114123
Control *square_overlay = nullptr;
115124
Control *value_slider = nullptr;
116-
virtual Ref<Shader> _get_shader() const { return ColorPicker::rectangle_ok_color_hs_shader; }
125+
virtual Ref<Shader> _get_shader() const { return ColorPickerShape::rectangle_ok_color_hs_shader; }
117126
virtual void _initialize_controls() override;
118127
virtual void _update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) override;
119128

@@ -139,7 +148,7 @@ class ColorPickerShapeOKHLRectangle : public ColorPickerShapeOKHSRectangle {
139148
GDCLASS(ColorPickerShapeOKHLRectangle, ColorPickerShapeOKHSRectangle);
140149

141150
protected:
142-
virtual Ref<Shader> _get_shader() const override { return ColorPicker::rectangle_ok_color_hl_shader; }
151+
virtual Ref<Shader> _get_shader() const override { return ColorPickerShape::rectangle_ok_color_hl_shader; }
143152
virtual void _update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) override;
144153

145154
virtual void _square_draw() override;
@@ -230,7 +239,7 @@ class ColorPickerShapeVHSCircle : public ColorPickerShapeCircle {
230239

231240
protected:
232241
virtual void _update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) override;
233-
virtual Ref<Shader> _get_shader() const override { return ColorPicker::circle_shader; }
242+
virtual Ref<Shader> _get_shader() const override { return ColorPickerShape::circle_shader; }
234243

235244
virtual void _circle_input(const Ref<InputEvent> &p_event) override;
236245
virtual void _value_slider_input(const Ref<InputEvent> &p_event) override;
@@ -251,7 +260,7 @@ class ColorPickerShapeOKHSLCircle : public ColorPickerShapeCircle {
251260

252261
protected:
253262
virtual void _update_cursor(const Vector2 &p_color_change_vector, bool p_is_echo) override;
254-
virtual Ref<Shader> _get_shader() const override { return ColorPicker::circle_ok_color_shader; }
263+
virtual Ref<Shader> _get_shader() const override { return ColorPickerShape::circle_ok_color_shader; }
255264

256265
virtual void _circle_input(const Ref<InputEvent> &p_event) override;
257266
virtual void _value_slider_input(const Ref<InputEvent> &p_event) override;

scene/register_scene_types.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "scene/gui/check_button.h"
5353
#include "scene/gui/code_edit.h"
5454
#include "scene/gui/color_picker.h"
55+
#include "scene/gui/color_picker_shape.h"
5556
#include "scene/gui/color_rect.h"
5657
#include "scene/gui/control.h"
5758
#include "scene/gui/dialogs.h"
@@ -1377,7 +1378,7 @@ void register_scene_types() {
13771378

13781379
if (RenderingServer::get_singleton()) {
13791380
// RenderingServer needs to exist for this to succeed.
1380-
ColorPicker::init_shaders();
1381+
ColorPickerShape::init_shaders();
13811382
GraphEdit::init_shaders();
13821383
}
13831384

@@ -1439,7 +1440,7 @@ void unregister_scene_types() {
14391440

14401441
ParticleProcessMaterial::finish_shaders();
14411442
CanvasItemMaterial::finish_shaders();
1442-
ColorPicker::finish_shaders();
1443+
ColorPickerShape::finish_shaders();
14431444
GraphEdit::finish_shaders();
14441445
SceneStringNames::free();
14451446

0 commit comments

Comments
 (0)