visual_shader_sdf_nodes.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**************************************************************************/
  2. /* visual_shader_sdf_nodes.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 "visual_shader_sdf_nodes.h"
  31. // VisualShaderNodeSDFToScreenUV
  32. String VisualShaderNodeSDFToScreenUV::get_caption() const {
  33. return "SDFToScreenUV";
  34. }
  35. int VisualShaderNodeSDFToScreenUV::get_input_port_count() const {
  36. return 1;
  37. }
  38. VisualShaderNodeSDFToScreenUV::PortType VisualShaderNodeSDFToScreenUV::get_input_port_type(int p_port) const {
  39. return PORT_TYPE_VECTOR_2D;
  40. }
  41. String VisualShaderNodeSDFToScreenUV::get_input_port_name(int p_port) const {
  42. return "sdf_pos";
  43. }
  44. int VisualShaderNodeSDFToScreenUV::get_output_port_count() const {
  45. return 1;
  46. }
  47. VisualShaderNodeSDFToScreenUV::PortType VisualShaderNodeSDFToScreenUV::get_output_port_type(int p_port) const {
  48. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  49. }
  50. String VisualShaderNodeSDFToScreenUV::get_output_port_name(int p_port) const {
  51. return "";
  52. }
  53. String VisualShaderNodeSDFToScreenUV::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  54. return " " + p_output_vars[0] + " = sdf_to_screen_uv(" + (p_input_vars[0].is_empty() ? "vec2(0.0)" : p_input_vars[0]) + ");\n";
  55. }
  56. VisualShaderNodeSDFToScreenUV::VisualShaderNodeSDFToScreenUV() {
  57. }
  58. // VisualShaderNodeScreenUVToSDF
  59. String VisualShaderNodeScreenUVToSDF::get_caption() const {
  60. return "ScreenUVToSDF";
  61. }
  62. int VisualShaderNodeScreenUVToSDF::get_input_port_count() const {
  63. return 1;
  64. }
  65. VisualShaderNodeScreenUVToSDF::PortType VisualShaderNodeScreenUVToSDF::get_input_port_type(int p_port) const {
  66. return PORT_TYPE_VECTOR_2D;
  67. }
  68. String VisualShaderNodeScreenUVToSDF::get_input_port_name(int p_port) const {
  69. return "uv";
  70. }
  71. int VisualShaderNodeScreenUVToSDF::get_output_port_count() const {
  72. return 1;
  73. }
  74. VisualShaderNodeScreenUVToSDF::PortType VisualShaderNodeScreenUVToSDF::get_output_port_type(int p_port) const {
  75. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  76. }
  77. String VisualShaderNodeScreenUVToSDF::get_output_port_name(int p_port) const {
  78. return "";
  79. }
  80. bool VisualShaderNodeScreenUVToSDF::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  81. if (p_port == 0) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. String VisualShaderNodeScreenUVToSDF::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  87. return " " + p_output_vars[0] + " = screen_uv_to_sdf(" + (p_input_vars[0].is_empty() ? "SCREEN_UV" : p_input_vars[0]) + ");\n";
  88. }
  89. VisualShaderNodeScreenUVToSDF::VisualShaderNodeScreenUVToSDF() {
  90. }
  91. // VisualShaderNodeTextureSDF
  92. String VisualShaderNodeTextureSDF::get_caption() const {
  93. return "TextureSDF";
  94. }
  95. int VisualShaderNodeTextureSDF::get_input_port_count() const {
  96. return 1;
  97. }
  98. VisualShaderNodeTextureSDF::PortType VisualShaderNodeTextureSDF::get_input_port_type(int p_port) const {
  99. return PORT_TYPE_VECTOR_2D;
  100. }
  101. String VisualShaderNodeTextureSDF::get_input_port_name(int p_port) const {
  102. return "sdf_pos";
  103. }
  104. int VisualShaderNodeTextureSDF::get_output_port_count() const {
  105. return 1;
  106. }
  107. VisualShaderNodeTextureSDF::PortType VisualShaderNodeTextureSDF::get_output_port_type(int p_port) const {
  108. return PORT_TYPE_SCALAR;
  109. }
  110. String VisualShaderNodeTextureSDF::get_output_port_name(int p_port) const {
  111. return "";
  112. }
  113. String VisualShaderNodeTextureSDF::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  114. return " " + p_output_vars[0] + " = texture_sdf(" + (p_input_vars[0].is_empty() ? "vec2(0.0)" : p_input_vars[0]) + ");\n";
  115. }
  116. VisualShaderNodeTextureSDF::VisualShaderNodeTextureSDF() {
  117. }
  118. // VisualShaderNodeTextureSDFNormal
  119. String VisualShaderNodeTextureSDFNormal::get_caption() const {
  120. return "TextureSDFNormal";
  121. }
  122. int VisualShaderNodeTextureSDFNormal::get_input_port_count() const {
  123. return 1;
  124. }
  125. VisualShaderNodeTextureSDFNormal::PortType VisualShaderNodeTextureSDFNormal::get_input_port_type(int p_port) const {
  126. return PORT_TYPE_VECTOR_2D;
  127. }
  128. String VisualShaderNodeTextureSDFNormal::get_input_port_name(int p_port) const {
  129. return "sdf_pos";
  130. }
  131. int VisualShaderNodeTextureSDFNormal::get_output_port_count() const {
  132. return 1;
  133. }
  134. VisualShaderNodeTextureSDFNormal::PortType VisualShaderNodeTextureSDFNormal::get_output_port_type(int p_port) const {
  135. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  136. }
  137. String VisualShaderNodeTextureSDFNormal::get_output_port_name(int p_port) const {
  138. return "";
  139. }
  140. String VisualShaderNodeTextureSDFNormal::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  141. return " " + p_output_vars[0] + " = texture_sdf_normal(" + (p_input_vars[0].is_empty() ? "vec2(0.0)" : p_input_vars[0]) + ");\n";
  142. }
  143. VisualShaderNodeTextureSDFNormal::VisualShaderNodeTextureSDFNormal() {
  144. }
  145. // VisualShaderNodeSDFRaymarch
  146. String VisualShaderNodeSDFRaymarch::get_caption() const {
  147. return "SDFRaymarch";
  148. }
  149. int VisualShaderNodeSDFRaymarch::get_input_port_count() const {
  150. return 2;
  151. }
  152. VisualShaderNodeSDFRaymarch::PortType VisualShaderNodeSDFRaymarch::get_input_port_type(int p_port) const {
  153. if (p_port == 0 || p_port == 1) {
  154. return PORT_TYPE_VECTOR_2D;
  155. }
  156. return PORT_TYPE_SCALAR;
  157. }
  158. String VisualShaderNodeSDFRaymarch::get_input_port_name(int p_port) const {
  159. if (p_port == 0) {
  160. return "from_pos";
  161. } else if (p_port == 1) {
  162. return "to_pos";
  163. }
  164. return String();
  165. }
  166. int VisualShaderNodeSDFRaymarch::get_output_port_count() const {
  167. return 3;
  168. }
  169. VisualShaderNodeSDFRaymarch::PortType VisualShaderNodeSDFRaymarch::get_output_port_type(int p_port) const {
  170. if (p_port == 0) {
  171. return PORT_TYPE_SCALAR;
  172. } else if (p_port == 1) {
  173. return PORT_TYPE_BOOLEAN;
  174. } else if (p_port == 2) {
  175. return PORT_TYPE_VECTOR_2D;
  176. }
  177. return PORT_TYPE_SCALAR;
  178. }
  179. String VisualShaderNodeSDFRaymarch::get_output_port_name(int p_port) const {
  180. if (p_port == 0) {
  181. return "distance";
  182. } else if (p_port == 1) {
  183. return "hit";
  184. } else if (p_port == 2) {
  185. return "end_pos";
  186. }
  187. return String();
  188. }
  189. String VisualShaderNodeSDFRaymarch::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  190. String code;
  191. code += " {\n";
  192. if (p_input_vars[0].is_empty()) {
  193. code += " vec2 __from_pos = vec2(0.0f);\n";
  194. } else {
  195. code += " vec2 __from_pos = " + p_input_vars[0] + ";\n";
  196. }
  197. if (p_input_vars[1].is_empty()) {
  198. code += " vec2 __to_pos = vec2(0.0f);\n";
  199. } else {
  200. code += " vec2 __to_pos = " + p_input_vars[1] + ";\n";
  201. }
  202. code += "\n vec2 __at = __from_pos;\n";
  203. code += " float __max_dist = distance(__from_pos, __to_pos);\n";
  204. code += " vec2 __dir = normalize(__to_pos - __from_pos);\n\n";
  205. code += " float __accum = 0.0f;\n";
  206. code += " while(__accum < __max_dist) {\n";
  207. code += " float __d = texture_sdf(__at);\n";
  208. code += " __accum += __d;\n";
  209. code += " if (__d < 0.01f) {\n";
  210. code += " break;\n";
  211. code += " }\n";
  212. code += " __at += __d * __dir;\n";
  213. code += " }\n";
  214. code += " float __dist = min(__max_dist, __accum);\n";
  215. code += " " + p_output_vars[0] + " = __dist;\n";
  216. code += " " + p_output_vars[1] + " = __accum < __max_dist;\n";
  217. code += " " + p_output_vars[2] + " = __from_pos + __dir * __dist;\n";
  218. code += " }\n";
  219. return code;
  220. }
  221. VisualShaderNodeSDFRaymarch::VisualShaderNodeSDFRaymarch() {
  222. simple_decl = false;
  223. }