text_shader_language_plugin.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "text_shader_language_plugin.h"
  31. #include "text_shader_editor.h"
  32. #include "core/string/string_builder.h"
  33. #include "servers/rendering/shader_types.h"
  34. bool TextShaderLanguagePlugin::handles_shader(const Ref<Shader> &p_shader) const {
  35. // The text shader editor only edits the base Shader class,
  36. // not classes that inherit from it like VisualShader.
  37. return p_shader->get_class_name() == Shader::get_class_static();
  38. }
  39. bool TextShaderLanguagePlugin::handles_shader_include(const Ref<ShaderInclude> &p_shader_inc) const {
  40. return p_shader_inc->get_class_static() == ShaderInclude::get_class_static();
  41. }
  42. ShaderEditor *TextShaderLanguagePlugin::edit_shader(const Ref<Shader> &p_shader) {
  43. TextShaderEditor *editor = memnew(TextShaderEditor);
  44. editor->edit_shader(p_shader);
  45. return editor;
  46. }
  47. ShaderEditor *TextShaderLanguagePlugin::edit_shader_include(const Ref<ShaderInclude> &p_shader_inc) {
  48. TextShaderEditor *editor = memnew(TextShaderEditor);
  49. editor->edit_shader_include(p_shader_inc);
  50. return editor;
  51. }
  52. Ref<Shader> TextShaderLanguagePlugin::create_new_shader(int p_variation_index, Shader::Mode p_shader_mode, int p_template_index) {
  53. Ref<Shader> shader;
  54. shader.instantiate();
  55. StringBuilder code;
  56. const String &shader_type = ShaderTypes::get_singleton()->get_types_list().get(p_shader_mode);
  57. code += vformat("shader_type %s;\n", shader_type);
  58. if (p_template_index == 0) { // Default template.
  59. switch (p_shader_mode) {
  60. case Shader::MODE_SPATIAL: {
  61. code += R"(
  62. void vertex() {
  63. // Called for every vertex the material is visible on.
  64. }
  65. void fragment() {
  66. // Called for every pixel the material is visible on.
  67. }
  68. //void light() {
  69. // // Called for every pixel for every light affecting the material.
  70. // // Uncomment to replace the default light processing function with this one.
  71. //}
  72. )";
  73. } break;
  74. case Shader::MODE_CANVAS_ITEM: {
  75. code += R"(
  76. void vertex() {
  77. // Called for every vertex the material is visible on.
  78. }
  79. void fragment() {
  80. // Called for every pixel the material is visible on.
  81. }
  82. //void light() {
  83. // // Called for every pixel for every light affecting the CanvasItem.
  84. // // Uncomment to replace the default light processing function with this one.
  85. //}
  86. )";
  87. } break;
  88. case Shader::MODE_PARTICLES: {
  89. code += R"(
  90. void start() {
  91. // Called when a particle is spawned.
  92. }
  93. void process() {
  94. // Called every frame on existing particles (according to the Fixed FPS property).
  95. }
  96. )";
  97. } break;
  98. case Shader::MODE_SKY: {
  99. code += R"(
  100. void sky() {
  101. // Called for every visible pixel in the sky background, as well as all pixels
  102. // in the radiance cubemap.
  103. }
  104. )";
  105. } break;
  106. case Shader::MODE_FOG: {
  107. code += R"(
  108. void fog() {
  109. // Called once for every froxel that is touched by an axis-aligned bounding box
  110. // of the associated FogVolume. This means that froxels that just barely touch
  111. // a given FogVolume will still be used.
  112. }
  113. )";
  114. } break;
  115. case Shader::MODE_MAX: {
  116. ERR_FAIL_V_MSG(Ref<Shader>(), "Invalid shader mode for text shader editor.");
  117. } break;
  118. }
  119. }
  120. shader->set_code(code.as_string());
  121. return shader;
  122. }
  123. Ref<ShaderInclude> TextShaderLanguagePlugin::create_new_shader_include() {
  124. Ref<ShaderInclude> shader_inc;
  125. shader_inc.instantiate();
  126. return shader_inc;
  127. }
  128. PackedStringArray TextShaderLanguagePlugin::get_language_variations() const {
  129. return PackedStringArray{ "Shader", "ShaderInclude" };
  130. }
  131. String TextShaderLanguagePlugin::get_file_extension(int p_variation_index) const {
  132. if (p_variation_index == 0) {
  133. return "gdshader";
  134. } else if (p_variation_index == 1) {
  135. return "gdshaderinc";
  136. }
  137. return "tres";
  138. }