diff options
author | Hugo Locurcio <hugo.locurcio@hugo.pro> | 2021-07-19 08:06:51 +0200 |
---|---|---|
committer | Hugo Locurcio <hugo.locurcio@hugo.pro> | 2021-07-19 08:19:50 +0200 |
commit | abc38b8d66360060296d792a8cf65c33c6010e8a (patch) | |
tree | bf453034e7d5f42393e24e1b9c66a7a991eb7346 /editor/plugins | |
parent | b76dfde329592ecfd6f6b952082ae21859039e67 (diff) |
Use C++11 raw literals for shader code to improve readability
In files that have lots of branching, `\t` was replaced with a
tab character instead.
Diffstat (limited to 'editor/plugins')
-rw-r--r-- | editor/plugins/animation_player_editor_plugin.cpp | 42 | ||||
-rw-r--r-- | editor/plugins/node_3d_editor_plugin.cpp | 190 | ||||
-rw-r--r-- | editor/plugins/texture_3d_editor_plugin.cpp | 19 | ||||
-rw-r--r-- | editor/plugins/texture_layered_editor_plugin.cpp | 67 |
4 files changed, 170 insertions, 148 deletions
diff --git a/editor/plugins/animation_player_editor_plugin.cpp b/editor/plugins/animation_player_editor_plugin.cpp index 2b92943f7e..dc0596843f 100644 --- a/editor/plugins/animation_player_editor_plugin.cpp +++ b/editor/plugins/animation_player_editor_plugin.cpp @@ -1733,27 +1733,27 @@ AnimationPlayerEditor::AnimationPlayerEditor(EditorNode *p_editor, AnimationPlay onion.capture.material = Ref<ShaderMaterial>(memnew(ShaderMaterial)); onion.capture.shader = Ref<Shader>(memnew(Shader)); - onion.capture.shader->set_code(" \ - shader_type canvas_item; \ - \ - uniform vec4 bkg_color; \ - uniform vec4 dir_color; \ - uniform bool differences_only; \ - uniform sampler2D present; \ - \ - float zero_if_equal(vec4 a, vec4 b) { \ - return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0)); \ - } \ - \ - void fragment() { \ - vec4 capture_samp = texture(TEXTURE, UV); \ - vec4 present_samp = texture(present, UV); \ - float bkg_mask = zero_if_equal(capture_samp, bkg_color); \ - float diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color); \ - diff_mask = min(1.0, diff_mask + float(!differences_only)); \ - COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask); \ - } \ - "); + onion.capture.shader->set_code(R"( +shader_type canvas_item; + +uniform vec4 bkg_color; +uniform vec4 dir_color; +uniform bool differences_only; +uniform sampler2D present; + +float zero_if_equal(vec4 a, vec4 b) { + return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0)); +} + +void fragment() { + vec4 capture_samp = texture(TEXTURE, UV); + vec4 present_samp = texture(present, UV); + float bkg_mask = zero_if_equal(capture_samp, bkg_color); + float diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color); + diff_mask = min(1.0, diff_mask + float(!differences_only)); + COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask); +} +)"); RS::get_singleton()->material_set_shader(onion.capture.material->get_rid(), onion.capture.shader->get_rid()); } diff --git a/editor/plugins/node_3d_editor_plugin.cpp b/editor/plugins/node_3d_editor_plugin.cpp index d5302df5e9..c2913085c9 100644 --- a/editor/plugins/node_3d_editor_plugin.cpp +++ b/editor/plugins/node_3d_editor_plugin.cpp @@ -5381,35 +5381,37 @@ void Node3DEditor::_init_indicators() { } Ref<Shader> grid_shader = memnew(Shader); - grid_shader->set_code( - "\n" - "shader_type spatial; \n" - "render_mode unshaded; \n" - "uniform bool orthogonal; \n" - "uniform float grid_size; \n" - "\n" - "void vertex() { \n" - " // From FLAG_SRGB_VERTEX_COLOR \n" - " if (!OUTPUT_IS_SRGB) { \n" - " COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045))); \n" - " } \n" - "} \n" - "\n" - "void fragment() { \n" - " ALBEDO = COLOR.rgb; \n" - " vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW; \n" - " float angle_fade = abs(dot(dir, NORMAL)); \n" - " angle_fade = smoothstep(0.05, 0.2, angle_fade); \n" - " \n" - " vec3 world_pos = (CAMERA_MATRIX * vec4(VERTEX, 1.0)).xyz; \n" - " vec3 world_normal = (CAMERA_MATRIX * vec4(NORMAL, 0.0)).xyz; \n" - " vec3 camera_world_pos = CAMERA_MATRIX[3].xyz; \n" - " vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal); \n" - " float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size); \n" - " dist_fade = smoothstep(0.02, 0.3, dist_fade); \n" - " \n" - " ALPHA = COLOR.a * dist_fade * angle_fade; \n" - "}"); + grid_shader->set_code(R"( +shader_type spatial; + +render_mode unshaded; + +uniform bool orthogonal; +uniform float grid_size; + +void vertex() { + // From FLAG_SRGB_VERTEX_COLOR. + if (!OUTPUT_IS_SRGB) { + COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045))); + } +} + +void fragment() { + ALBEDO = COLOR.rgb; + vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW; + float angle_fade = abs(dot(dir, NORMAL)); + angle_fade = smoothstep(0.05, 0.2, angle_fade); + + vec3 world_pos = (CAMERA_MATRIX * vec4(VERTEX, 1.0)).xyz; + vec3 world_normal = (CAMERA_MATRIX * vec4(NORMAL, 0.0)).xyz; + vec3 camera_world_pos = CAMERA_MATRIX[3].xyz; + vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal); + float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size); + dist_fade = smoothstep(0.02, 0.3, dist_fade); + + ALPHA = COLOR.a * dist_fade * angle_fade; +} +)"); for (int i = 0; i < 3; i++) { grid_mat[i].instantiate(); @@ -5621,33 +5623,35 @@ void Node3DEditor::_init_indicators() { Ref<Shader> rotate_shader = memnew(Shader); - rotate_shader->set_code( - "\n" - "shader_type spatial; \n" - "render_mode unshaded, depth_test_disabled; \n" - "uniform vec4 albedo; \n" - "\n" - "mat3 orthonormalize(mat3 m) { \n" - " vec3 x = normalize(m[0]); \n" - " vec3 y = normalize(m[1] - x * dot(x, m[1])); \n" - " vec3 z = m[2] - x * dot(x, m[2]); \n" - " z = normalize(z - y * (dot(y,m[2]))); \n" - " return mat3(x,y,z); \n" - "} \n" - "\n" - "void vertex() { \n" - " mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); \n" - " vec3 n = mv * VERTEX; \n" - " float orientation = dot(vec3(0,0,-1),n); \n" - " if (orientation <= 0.005) { \n" - " VERTEX += NORMAL*0.02; \n" - " } \n" - "} \n" - "\n" - "void fragment() { \n" - " ALBEDO = albedo.rgb; \n" - " ALPHA = albedo.a; \n" - "}"); + rotate_shader->set_code(R"( +shader_type spatial; + +render_mode unshaded, depth_test_disabled; + +uniform vec4 albedo; + +mat3 orthonormalize(mat3 m) { + vec3 x = normalize(m[0]); + vec3 y = normalize(m[1] - x * dot(x, m[1])); + vec3 z = m[2] - x * dot(x, m[2]); + z = normalize(z - y * (dot(y,m[2]))); + return mat3(x,y,z); +} + +void vertex() { + mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); + vec3 n = mv * VERTEX; + float orientation = dot(vec3(0, 0, -1), n); + if (orientation <= 0.005) { + VERTEX += NORMAL * 0.02; + } +} + +void fragment() { + ALBEDO = albedo.rgb; + ALPHA = albedo.a; +} +)"); Ref<ShaderMaterial> rotate_mat = memnew(ShaderMaterial); rotate_mat->set_render_priority(Material::RENDER_PRIORITY_MAX); @@ -5667,34 +5671,36 @@ void Node3DEditor::_init_indicators() { Ref<ShaderMaterial> border_mat = rotate_mat->duplicate(); Ref<Shader> border_shader = memnew(Shader); - border_shader->set_code( - "\n" - "shader_type spatial; \n" - "render_mode unshaded, depth_test_disabled; \n" - "uniform vec4 albedo; \n" - "\n" - "mat3 orthonormalize(mat3 m) { \n" - " vec3 x = normalize(m[0]); \n" - " vec3 y = normalize(m[1] - x * dot(x, m[1])); \n" - " vec3 z = m[2] - x * dot(x, m[2]); \n" - " z = normalize(z - y * (dot(y,m[2]))); \n" - " return mat3(x,y,z); \n" - "} \n" - "\n" - "void vertex() { \n" - " mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); \n" - " mv = inverse(mv); \n" - " VERTEX += NORMAL*0.008; \n" - " vec3 camera_dir_local = mv * vec3(0,0,1); \n" - " vec3 camera_up_local = mv * vec3(0,1,0); \n" - " mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local); \n" - " VERTEX = rotation_matrix * VERTEX; \n" - "} \n" - "\n" - "void fragment() { \n" - " ALBEDO = albedo.rgb; \n" - " ALPHA = albedo.a; \n" - "}"); + border_shader->set_code(R"( +shader_type spatial; + +render_mode unshaded, depth_test_disabled; + +uniform vec4 albedo; + +mat3 orthonormalize(mat3 m) { + vec3 x = normalize(m[0]); + vec3 y = normalize(m[1] - x * dot(x, m[1])); + vec3 z = m[2] - x * dot(x, m[2]); + z = normalize(z - y * (dot(y,m[2]))); + return mat3(x,y,z); +} + +void vertex() { + mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); + mv = inverse(mv); + VERTEX += NORMAL*0.008; + vec3 camera_dir_local = mv * vec3(0,0,1); + vec3 camera_up_local = mv * vec3(0,1,0); + mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local); + VERTEX = rotation_matrix * VERTEX; +} + +void fragment() { + ALBEDO = albedo.rgb; + ALPHA = albedo.a; +} +)"); border_mat->set_shader(border_shader); border_mat->set_shader_param("albedo", Color(0.75, 0.75, 0.75, col.a / 3.0)); @@ -7155,9 +7161,21 @@ Node3DEditor::Node3DEditor(EditorNode *p_editor) { sun_direction->connect("draw", callable_mp(this, &Node3DEditor::_sun_direction_draw)); sun_direction->set_default_cursor_shape(CURSOR_MOVE); - String sun_dir_shader_code = "shader_type canvas_item; uniform vec3 sun_direction; uniform vec3 sun_color; void fragment() { vec3 n; n.xy = UV * 2.0 - 1.0; n.z = sqrt(max(0.0, 1.0 - dot(n.xy, n.xy))); COLOR.rgb = dot(n,sun_direction) * sun_color; COLOR.a = 1.0 - smoothstep(0.99,1.0,length(n.xy)); }"; sun_direction_shader.instantiate(); - sun_direction_shader->set_code(sun_dir_shader_code); + sun_direction_shader->set_code(R"( +shader_type canvas_item; + +uniform vec3 sun_direction; +uniform vec3 sun_color; + +void fragment() { + vec3 n; + n.xy = UV * 2.0 - 1.0; + n.z = sqrt(max(0.0, 1.0 - dot(n.xy, n.xy))); + COLOR.rgb = dot(n, sun_direction) * sun_color; + COLOR.a = 1.0 - smoothstep(0.99, 1.0, length(n.xy)); +} +)"); sun_direction_material.instantiate(); sun_direction_material->set_shader(sun_direction_shader); sun_direction_material->set_shader_param("sun_direction", Vector3(0, 0, 1)); diff --git a/editor/plugins/texture_3d_editor_plugin.cpp b/editor/plugins/texture_3d_editor_plugin.cpp index 696aa88e23..ae35268142 100644 --- a/editor/plugins/texture_3d_editor_plugin.cpp +++ b/editor/plugins/texture_3d_editor_plugin.cpp @@ -77,16 +77,17 @@ void Texture3DEditor::_update_material() { } void Texture3DEditor::_make_shaders() { - String shader_3d = "" - "shader_type canvas_item;\n" - "uniform sampler3D tex;\n" - "uniform float layer;\n" - "void fragment() {\n" - " COLOR = textureLod(tex,vec3(UV,layer),0.0);\n" - "}"; - shader.instantiate(); - shader->set_code(shader_3d); + shader->set_code(R"( +shader_type canvas_item; + +uniform sampler3D tex; +uniform float layer; + +void fragment() { + COLOR = textureLod(tex, vec3(UV, layer), 0.0); +} +)"); material.instantiate(); material->set_shader(shader); } diff --git a/editor/plugins/texture_layered_editor_plugin.cpp b/editor/plugins/texture_layered_editor_plugin.cpp index 3f46cd64a2..97b45a4f28 100644 --- a/editor/plugins/texture_layered_editor_plugin.cpp +++ b/editor/plugins/texture_layered_editor_plugin.cpp @@ -104,43 +104,46 @@ void TextureLayeredEditor::_update_material() { } void TextureLayeredEditor::_make_shaders() { - String shader_2d_array = "" - "shader_type canvas_item;\n" - "uniform sampler2DArray tex;\n" - "uniform float layer;\n" - "void fragment() {\n" - " COLOR = textureLod(tex,vec3(UV,layer),0.0);\n" - "}"; - shaders[0].instantiate(); - shaders[0]->set_code(shader_2d_array); - - String shader_cube = "" - "shader_type canvas_item;\n" - "uniform samplerCube tex;\n" - "uniform vec3 normal;\n" - "uniform mat3 rot;\n" - "void fragment() {\n" - " vec3 n = rot * normalize(vec3(normal.xy*(UV * 2.0 - 1.0),normal.z));\n" - " COLOR = textureLod(tex,n,0.0);\n" - "}"; + shaders[0]->set_code(R"( +shader_type canvas_item; + +uniform sampler2DArray tex; +uniform float layer; + +void fragment() { + COLOR = textureLod(tex, vec3(UV, layer), 0.0); +} +)"); shaders[1].instantiate(); - shaders[1]->set_code(shader_cube); - - String shader_cube_array = "" - "shader_type canvas_item;\n" - "uniform samplerCubeArray tex;\n" - "uniform vec3 normal;\n" - "uniform mat3 rot;\n" - "uniform float layer;\n" - "void fragment() {\n" - " vec3 n = rot * normalize(vec3(normal.xy*(UV * 2.0 - 1.0),normal.z));\n" - " COLOR = textureLod(tex,vec4(n,layer),0.0);\n" - "}"; + shaders[1]->set_code(R"( +shader_type canvas_item; + +uniform samplerCube tex; +uniform vec3 normal; +uniform mat3 rot; + +void fragment() { + vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z)); + COLOR = textureLod(tex, n, 0.0); +} +)"); shaders[2].instantiate(); - shaders[2]->set_code(shader_cube_array); + shaders[2]->set_code(R"( +shader_type canvas_item; + +uniform samplerCubeArray tex; +uniform vec3 normal; +uniform mat3 rot; +uniform float layer; + +void fragment() { + vec3 n = rot * normalize(vec3(normal.xy * (UV * 2.0 - 1.0), normal.z)); + COLOR = textureLod(tex, vec4(n, layer), 0.0); +} +)"); for (int i = 0; i < 3; i++) { materials[i].instantiate(); |