Skip to content

Commit 7faad0c

Browse files
committed
Merge pull request #97388 from tetrapod00/visualshader-linear-srgb
VisualShader: Add LinearToSRGB and SRGBToLinear to ColorFunc node
2 parents 17e8cf0 + 2191df0 commit 7faad0c

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

doc/classes/VisualShaderNodeColorFunc.xml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,32 @@
4040
return vec3(r, g, b);
4141
[/codeblock]
4242
</constant>
43-
<constant name="FUNC_MAX" value="4" enum="Function">
43+
<constant name="FUNC_LINEAR_TO_SRGB" value="4" enum="Function">
44+
Converts color from linear color space to sRGB color space using the following formula:
45+
[codeblock]
46+
vec3 c = clamp(c, vec3(0.0), vec3(1.0));
47+
const vec3 a = vec3(0.055f);
48+
return mix((vec3(1.0f) + a) * pow(c.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * c.rgb, lessThan(c.rgb, vec3(0.0031308f)));
49+
[/codeblock]
50+
The Compatibility renderer uses a simpler formula:
51+
[codeblock]
52+
vec3 c = input;
53+
return max(vec3(1.055) * pow(c, vec3(0.416666667)) - vec3(0.055), vec3(0.0));
54+
[/codeblock]
55+
</constant>
56+
<constant name="FUNC_SRGB_TO_LINEAR" value="5" enum="Function">
57+
Converts color from sRGB color space to linear color space using the following formula:
58+
[codeblock]
59+
vec3 c = input;
60+
return mix(pow((c.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), c.rgb * (1.0 / 12.92), lessThan(c.rgb, vec3(0.04045)));
61+
[/codeblock]
62+
The Compatibility renderer uses a simpler formula:
63+
[codeblock]
64+
vec3 c = input;
65+
return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
66+
[/codeblock]
67+
</constant>
68+
<constant name="FUNC_MAX" value="6" enum="Function">
4469
Represents the size of the [enum Function] enum.
4570
</constant>
4671
</constants>

editor/plugins/visual_shader_editor_plugin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6879,8 +6879,10 @@ VisualShaderEditor::VisualShaderEditor() {
68796879

68806880
add_options.push_back(AddOption("Grayscale", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), { VisualShaderNodeColorFunc::FUNC_GRAYSCALE }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
68816881
add_options.push_back(AddOption("HSV2RGB", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Converts HSV vector to RGB equivalent."), { VisualShaderNodeColorFunc::FUNC_HSV2RGB, VisualShaderNodeVectorFunc::OP_TYPE_VECTOR_3D }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
6882+
add_options.push_back(AddOption("LinearToSRGB", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Converts color from linear to sRGB color space."), { VisualShaderNodeColorFunc::FUNC_LINEAR_TO_SRGB }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
68826883
add_options.push_back(AddOption("RGB2HSV", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Converts RGB vector to HSV equivalent."), { VisualShaderNodeColorFunc::FUNC_RGB2HSV, VisualShaderNodeVectorFunc::OP_TYPE_VECTOR_3D }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
68836884
add_options.push_back(AddOption("Sepia", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), { VisualShaderNodeColorFunc::FUNC_SEPIA }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
6885+
add_options.push_back(AddOption("SRGBToLinear", "Color/Functions", "VisualShaderNodeColorFunc", TTR("Converts color from sRGB to linear color space."), { VisualShaderNodeColorFunc::FUNC_SRGB_TO_LINEAR }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
68846886

68856887
add_options.push_back(AddOption("Burn", "Color/Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), { VisualShaderNodeColorOp::OP_BURN }, VisualShaderNode::PORT_TYPE_VECTOR_3D));
68866888
add_options.push_back(AddOption("Darken", "Color/Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), { VisualShaderNodeColorOp::OP_DARKEN }, VisualShaderNode::PORT_TYPE_VECTOR_3D));

scene/resources/visual_shader_nodes.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3243,6 +3243,29 @@ String VisualShaderNodeColorFunc::generate_code(Shader::Mode p_mode, VisualShade
32433243
code += " " + p_output_vars[0] + " = vec3(r, g, b);\n";
32443244
code += " }\n";
32453245
break;
3246+
case FUNC_LINEAR_TO_SRGB:
3247+
code += " {\n";
3248+
if (RenderingServer::get_singleton()->is_low_end()) {
3249+
code += " vec3 c = " + p_input_vars[0] + ";\n";
3250+
code += " " + p_output_vars[0] + " = max(vec3(1.055) * pow(c, vec3(0.416666667)) - vec3(0.055), vec3(0.0));\n";
3251+
} else {
3252+
code += " vec3 c = clamp(" + p_input_vars[0] + ", vec3(0.0), vec3(1.0));\n";
3253+
code += " const vec3 a = vec3(0.055f);\n";
3254+
code += " " + p_output_vars[0] + " = mix((vec3(1.0f) + a) * pow(c.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * c.rgb, lessThan(c.rgb, vec3(0.0031308f)));\n";
3255+
}
3256+
code += " }\n";
3257+
break;
3258+
case FUNC_SRGB_TO_LINEAR:
3259+
code += " {\n";
3260+
if (RenderingServer::get_singleton()->is_low_end()) {
3261+
code += " vec3 c = " + p_input_vars[0] + ";\n";
3262+
code += " " + p_output_vars[0] + " = c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);\n";
3263+
} else {
3264+
code += " vec3 c = " + p_input_vars[0] + ";\n";
3265+
code += " " + p_output_vars[0] + " = mix(pow((c.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), c.rgb * (1.0 / 12.92), lessThan(c.rgb, vec3(0.04045)));\n";
3266+
}
3267+
code += " }\n";
3268+
break;
32463269
default:
32473270
break;
32483271
}
@@ -3273,12 +3296,14 @@ void VisualShaderNodeColorFunc::_bind_methods() {
32733296
ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeColorFunc::set_function);
32743297
ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeColorFunc::get_function);
32753298

3276-
ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Grayscale,HSV2RGB,RGB2HSV,Sepia"), "set_function", "get_function");
3299+
ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Grayscale,HSV2RGB,RGB2HSV,Sepia,LinearToSRGB,SRGBToLinear"), "set_function", "get_function");
32773300

32783301
BIND_ENUM_CONSTANT(FUNC_GRAYSCALE);
32793302
BIND_ENUM_CONSTANT(FUNC_HSV2RGB);
32803303
BIND_ENUM_CONSTANT(FUNC_RGB2HSV);
32813304
BIND_ENUM_CONSTANT(FUNC_SEPIA);
3305+
BIND_ENUM_CONSTANT(FUNC_LINEAR_TO_SRGB);
3306+
BIND_ENUM_CONSTANT(FUNC_SRGB_TO_LINEAR);
32823307
BIND_ENUM_CONSTANT(FUNC_MAX);
32833308
}
32843309

scene/resources/visual_shader_nodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,8 @@ class VisualShaderNodeColorFunc : public VisualShaderNode {
13531353
FUNC_HSV2RGB,
13541354
FUNC_RGB2HSV,
13551355
FUNC_SEPIA,
1356+
FUNC_LINEAR_TO_SRGB,
1357+
FUNC_SRGB_TO_LINEAR,
13561358
FUNC_MAX,
13571359
};
13581360

0 commit comments

Comments
 (0)