| 1 | /**************************************************************************/ |
| 2 | /* text_shader_language_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "text_shader_language_plugin.h" |
| 32 | |
| 33 | #include "text_shader_editor.h" |
| 34 | |
| 35 | #include "core/string/string_builder.h" |
| 36 | #include "servers/rendering/shader_types.h" |
| 37 | |
| 38 | bool TextShaderLanguagePlugin::handles_shader(const Ref<Shader> &p_shader) const { |
| 39 | // The text shader editor only edits the base Shader class, |
| 40 | // not classes that inherit from it like VisualShader. |
| 41 | return p_shader->get_class_name() == Shader::get_class_static(); |
| 42 | } |
| 43 | |
| 44 | bool TextShaderLanguagePlugin::handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const { |
| 45 | return p_shader_inc->get_class_static() == ShaderInclude::get_class_static(); |
| 46 | } |
| 47 | |
| 48 | ShaderEditor *TextShaderLanguagePlugin::edit_shader(const Ref<Shader> &p_shader) { |
| 49 | TextShaderEditor *editor = memnew(TextShaderEditor); |
| 50 | editor->edit_shader(p_shader); |
| 51 | return editor; |
| 52 | } |
| 53 | |
| 54 | ShaderEditor *TextShaderLanguagePlugin::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) { |
| 55 | TextShaderEditor *editor = memnew(TextShaderEditor); |
| 56 | editor->edit_shader_include(p_shader_inc); |
| 57 | return editor; |
| 58 | } |
| 59 | |
| 60 | Ref<Shader> TextShaderLanguagePlugin::create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) { |
| 61 | Ref<Shader> shader; |
| 62 | shader.instantiate(); |
| 63 | |
| 64 | StringBuilder code; |
| 65 | const String &shader_type = ShaderTypes::get_singleton()->get_types_list().get(p_shader_mode); |
| 66 | code += vformat("shader_type %s;\n", shader_type); |
| 67 | |
| 68 | if (p_template_index == 0) { // Default template. |
| 69 | switch (p_shader_mode) { |
| 70 | case Shader::MODE_SPATIAL: { |
| 71 | code += R"( |
| 72 | void vertex() { |
| 73 | // Called for every vertex the material is visible on. |
| 74 | } |
| 75 | |
| 76 | void fragment() { |
| 77 | // Called for every pixel the material is visible on. |
| 78 | } |
| 79 | |
| 80 | //void light() { |
| 81 | // // Called for every pixel for every light affecting the material. |
| 82 | // // Uncomment to replace the default light processing function with this one. |
| 83 | //} |
| 84 | )"; |
| 85 | } break; |
| 86 | case Shader::MODE_CANVAS_ITEM: { |
| 87 | code += R"( |
| 88 | void vertex() { |
| 89 | // Called for every vertex the material is visible on. |
| 90 | } |
| 91 | |
| 92 | void fragment() { |
| 93 | // Called for every pixel the material is visible on. |
| 94 | } |
| 95 | |
| 96 | //void light() { |
| 97 | // // Called for every pixel for every light affecting the CanvasItem. |
| 98 | // // Uncomment to replace the default light processing function with this one. |
| 99 | //} |
| 100 | )"; |
| 101 | } break; |
| 102 | case Shader::MODE_PARTICLES: { |
| 103 | code += R"( |
| 104 | void start() { |
| 105 | // Called when a particle is spawned. |
| 106 | } |
| 107 | |
| 108 | void process() { |
| 109 | // Called every frame on existing particles (according to the Fixed FPS property). |
| 110 | } |
| 111 | )"; |
| 112 | } break; |
| 113 | case Shader::MODE_SKY: { |
| 114 | code += R"( |
| 115 | void sky() { |
| 116 | // Called for every visible pixel in the sky background, as well as all pixels |
| 117 | // in the radiance cubemap. |
| 118 | } |
| 119 | )"; |
| 120 | } break; |
| 121 | case Shader::MODE_FOG: { |
| 122 | code += R"( |
| 123 | void fog() { |
| 124 | // Called once for every froxel that is touched by an axis-aligned bounding box |
| 125 | // of the associated FogVolume. This means that froxels that just barely touch |
| 126 | // a given FogVolume will still be used. |
| 127 | } |
| 128 | )"; |
| 129 | } break; |
| 130 | case Shader::MODE_MAX: { |
| 131 | ERR_FAIL_V_MSG(Ref<Shader>(), "Invalid shader mode for text shader editor."); |
| 132 | } break; |
| 133 | } |
| 134 | } |
| 135 | shader->set_code(code.as_string()); |
| 136 | return shader; |
| 137 | } |
| 138 | |
| 139 | Ref<ShaderInclude> TextShaderLanguagePlugin::create_new_shader_include() { |
| 140 | Ref<ShaderInclude> shader_inc; |
| 141 | shader_inc.instantiate(); |
| 142 | return shader_inc; |
| 143 | } |
| 144 | |
| 145 | PackedStringArray TextShaderLanguagePlugin::get_language_variations() const { |
| 146 | return PackedStringArray{ "Shader", "ShaderInclude" }; |
| 147 | } |
| 148 | |
| 149 | String TextShaderLanguagePlugin::get_file_extension(int p_variation_index) const { |
| 150 | if (p_variation_index == 0) { |
| 151 | return "gdshader"; |
| 152 | } else if (p_variation_index == 1) { |
| 153 | return "gdshaderinc"; |
| 154 | } |
| 155 | return "tres"; |
| 156 | } |