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