1/**************************************************************************/
2/* visual_shader_particle_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_particle_nodes.h"
32
33#include "scene/resources/image_texture.h"
34
35// VisualShaderNodeParticleEmitter
36
37int VisualShaderNodeParticleEmitter::get_output_port_count() const {
38 return 1;
39}
40
41VisualShaderNodeParticleEmitter::PortType VisualShaderNodeParticleEmitter::get_output_port_type(int p_port) const {
42 if (mode_2d) {
43 return PORT_TYPE_VECTOR_2D;
44 }
45 return PORT_TYPE_VECTOR_3D;
46}
47
48String VisualShaderNodeParticleEmitter::get_output_port_name(int p_port) const {
49 if (p_port == 0) {
50 return "position";
51 }
52 return String();
53}
54
55bool VisualShaderNodeParticleEmitter::has_output_port_preview(int p_port) const {
56 return false;
57}
58
59void VisualShaderNodeParticleEmitter::set_mode_2d(bool p_enabled) {
60 if (mode_2d == p_enabled) {
61 return;
62 }
63 mode_2d = p_enabled;
64 emit_changed();
65}
66
67bool VisualShaderNodeParticleEmitter::is_mode_2d() const {
68 return mode_2d;
69}
70
71Vector<StringName> VisualShaderNodeParticleEmitter::get_editable_properties() const {
72 Vector<StringName> props;
73 props.push_back("mode_2d");
74 return props;
75}
76
77HashMap<StringName, String> VisualShaderNodeParticleEmitter::get_editable_properties_names() const {
78 HashMap<StringName, String> names;
79 names.insert("mode_2d", RTR("2D Mode"));
80 return names;
81}
82
83bool VisualShaderNodeParticleEmitter::is_show_prop_names() const {
84 return true;
85}
86
87void VisualShaderNodeParticleEmitter::_bind_methods() {
88 ClassDB::bind_method(D_METHOD("set_mode_2d", "enabled"), &VisualShaderNodeParticleEmitter::set_mode_2d);
89 ClassDB::bind_method(D_METHOD("is_mode_2d"), &VisualShaderNodeParticleEmitter::is_mode_2d);
90
91 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_2d"), "set_mode_2d", "is_mode_2d");
92}
93
94VisualShaderNodeParticleEmitter::VisualShaderNodeParticleEmitter() {
95}
96
97// VisualShaderNodeParticleSphereEmitter
98
99String VisualShaderNodeParticleSphereEmitter::get_caption() const {
100 return "SphereEmitter";
101}
102
103int VisualShaderNodeParticleSphereEmitter::get_input_port_count() const {
104 return 2;
105}
106
107VisualShaderNodeParticleSphereEmitter::PortType VisualShaderNodeParticleSphereEmitter::get_input_port_type(int p_port) const {
108 return PORT_TYPE_SCALAR;
109}
110
111String VisualShaderNodeParticleSphereEmitter::get_input_port_name(int p_port) const {
112 if (p_port == 0) {
113 return "radius";
114 } else if (p_port == 1) {
115 return "inner_radius";
116 }
117 return String();
118}
119
120String VisualShaderNodeParticleSphereEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
121 String code;
122
123 code += "vec2 __get_random_point_in_circle(inout uint seed, float radius, float inner_radius) {\n";
124 code += " return __get_random_unit_vec2(seed) * __randf_range(seed, inner_radius, radius);\n";
125 code += "}\n\n";
126
127 code += "vec3 __get_random_point_in_sphere(inout uint seed, float radius, float inner_radius) {\n";
128 code += " return __get_random_unit_vec3(seed) * __randf_range(seed, inner_radius, radius);\n";
129 code += "}\n\n";
130
131 return code;
132}
133
134String VisualShaderNodeParticleSphereEmitter::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 {
135 String code;
136
137 if (mode_2d) {
138 code += " " + p_output_vars[0] + " = __get_random_point_in_circle(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
139 } else {
140 code += " " + p_output_vars[0] + " = __get_random_point_in_sphere(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
141 }
142
143 return code;
144}
145
146VisualShaderNodeParticleSphereEmitter::VisualShaderNodeParticleSphereEmitter() {
147 set_input_port_default_value(0, 10.0);
148 set_input_port_default_value(1, 0.0);
149}
150
151// VisualShaderNodeParticleBoxEmitter
152
153String VisualShaderNodeParticleBoxEmitter::get_caption() const {
154 return "BoxEmitter";
155}
156
157int VisualShaderNodeParticleBoxEmitter::get_input_port_count() const {
158 return 1;
159}
160
161VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleBoxEmitter::get_input_port_type(int p_port) const {
162 if (p_port == 0) {
163 if (mode_2d) {
164 return PORT_TYPE_VECTOR_2D;
165 }
166 return PORT_TYPE_VECTOR_3D;
167 }
168 return PORT_TYPE_SCALAR;
169}
170
171void VisualShaderNodeParticleBoxEmitter::set_mode_2d(bool p_enabled) {
172 if (mode_2d == p_enabled) {
173 return;
174 }
175 if (p_enabled) {
176 set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
177 } else {
178 set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
179 }
180 mode_2d = p_enabled;
181 emit_changed();
182}
183
184String VisualShaderNodeParticleBoxEmitter::get_input_port_name(int p_port) const {
185 if (p_port == 0) {
186 return "extents";
187 }
188 return String();
189}
190
191String VisualShaderNodeParticleBoxEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
192 String code;
193
194 code += "vec2 __get_random_point_in_box2d(inout uint seed, vec2 extents) {\n";
195 code += " vec2 half_extents = extents / 2.0;\n";
196 code += " return vec2(__randf_range(seed, -half_extents.x, half_extents.x), __randf_range(seed, -half_extents.y, half_extents.y));\n";
197 code += "}\n\n";
198
199 code += "vec3 __get_random_point_in_box3d(inout uint seed, vec3 extents) {\n";
200 code += " vec3 half_extents = extents / 2.0;\n";
201 code += " return vec3(__randf_range(seed, -half_extents.x, half_extents.x), __randf_range(seed, -half_extents.y, half_extents.y), __randf_range(seed, -half_extents.z, half_extents.z));\n";
202 code += "}\n\n";
203
204 return code;
205}
206
207String VisualShaderNodeParticleBoxEmitter::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 {
208 String code;
209 if (mode_2d) {
210 code += " " + p_output_vars[0] + " = __get_random_point_in_box2d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ");\n";
211 } else {
212 code += " " + p_output_vars[0] + " = __get_random_point_in_box3d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ");\n";
213 }
214 return code;
215}
216
217VisualShaderNodeParticleBoxEmitter::VisualShaderNodeParticleBoxEmitter() {
218 set_input_port_default_value(0, Vector3(1.0, 1.0, 1.0));
219}
220
221// VisualShaderNodeParticleRingEmitter
222
223String VisualShaderNodeParticleRingEmitter::get_caption() const {
224 return "RingEmitter";
225}
226
227int VisualShaderNodeParticleRingEmitter::get_input_port_count() const {
228 return 3;
229}
230
231VisualShaderNodeParticleRingEmitter::PortType VisualShaderNodeParticleRingEmitter::get_input_port_type(int p_port) const {
232 return PORT_TYPE_SCALAR;
233}
234
235String VisualShaderNodeParticleRingEmitter::get_input_port_name(int p_port) const {
236 if (p_port == 0) {
237 return "radius";
238 } else if (p_port == 1) {
239 return "inner_radius";
240 } else if (p_port == 2) {
241 return "height";
242 }
243 return String();
244}
245
246String VisualShaderNodeParticleRingEmitter::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
247 String code;
248
249 code += "vec2 __get_random_point_on_ring2d(inout uint seed, float radius, float inner_radius) {\n";
250 code += " float angle = __rand_from_seed(seed) * TAU;\n";
251 code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
252 code += " return vec2(ring.x, ring.y);\n";
253 code += "}\n\n";
254
255 code += "vec3 __get_random_point_on_ring3d(inout uint seed, float radius, float inner_radius, float height) {\n";
256 code += " float angle = __rand_from_seed(seed) * TAU;\n";
257 code += " vec2 ring = vec2(sin(angle), cos(angle)) * __randf_range(seed, inner_radius, radius);\n";
258 code += " return vec3(ring.x, __randf_range(seed, min(0.0, height), max(0.0, height)), ring.y);\n";
259 code += "}\n\n";
260
261 return code;
262}
263
264String VisualShaderNodeParticleRingEmitter::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 {
265 String code;
266
267 if (mode_2d) {
268 code = " " + p_output_vars[0] + " = __get_random_point_on_ring2d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
269 } else {
270 code = " " + p_output_vars[0] + " = __get_random_point_on_ring3d(__seed, " + (p_input_vars[0].is_empty() ? (String)get_input_port_default_value(0) : p_input_vars[0]) + ", " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ", " + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ");\n";
271 }
272
273 return code;
274}
275
276VisualShaderNodeParticleRingEmitter::VisualShaderNodeParticleRingEmitter() {
277 set_input_port_default_value(0, 10.0);
278 set_input_port_default_value(1, 0.0);
279 set_input_port_default_value(2, 0.0);
280}
281
282// VisualShaderNodeParticleMeshEmitter
283
284String VisualShaderNodeParticleMeshEmitter::get_caption() const {
285 return "MeshEmitter";
286}
287
288int VisualShaderNodeParticleMeshEmitter::get_output_port_count() const {
289 return 6;
290}
291
292VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_output_port_type(int p_port) const {
293 switch (p_port) {
294 case 0: // position
295 if (mode_2d) {
296 return PORT_TYPE_VECTOR_2D;
297 }
298 return PORT_TYPE_VECTOR_3D;
299 case 1: // normal
300 if (mode_2d) {
301 return PORT_TYPE_VECTOR_2D;
302 }
303 return PORT_TYPE_VECTOR_3D;
304 case 2: // color
305 return PORT_TYPE_VECTOR_3D;
306 case 3: // alpha
307 return PORT_TYPE_SCALAR;
308 case 4: // uv
309 return PORT_TYPE_VECTOR_2D;
310 case 5: // uv2
311 return PORT_TYPE_VECTOR_2D;
312 }
313 return PORT_TYPE_SCALAR;
314}
315
316String VisualShaderNodeParticleMeshEmitter::get_output_port_name(int p_port) const {
317 switch (p_port) {
318 case 0:
319 return "position";
320 case 1:
321 return "normal";
322 case 2:
323 return "color";
324 case 3:
325 return "alpha";
326 case 4:
327 return "uv";
328 case 5:
329 return "uv2";
330 }
331 return String();
332}
333
334int VisualShaderNodeParticleMeshEmitter::get_input_port_count() const {
335 return 0;
336}
337
338VisualShaderNodeParticleBoxEmitter::PortType VisualShaderNodeParticleMeshEmitter::get_input_port_type(int p_port) const {
339 return PORT_TYPE_SCALAR;
340}
341
342String VisualShaderNodeParticleMeshEmitter::get_input_port_name(int p_port) const {
343 return String();
344}
345
346String VisualShaderNodeParticleMeshEmitter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
347 String code;
348
349 if (is_output_port_connected(0)) { // position
350 code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_vx") + ";\n";
351 }
352
353 if (is_output_port_connected(1)) { // normal
354 code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_nm") + ";\n";
355 }
356
357 if (is_output_port_connected(2) || is_output_port_connected(3)) { // color & alpha
358 code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_col") + ";\n";
359 }
360
361 if (is_output_port_connected(4)) { // uv
362 code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv") + ";\n";
363 }
364
365 if (is_output_port_connected(5)) { // uv2
366 code += "uniform sampler2D " + make_unique_id(p_type, p_id, "mesh_uv2") + ";\n";
367 }
368
369 return code;
370}
371
372String VisualShaderNodeParticleMeshEmitter::_generate_code(VisualShader::Type p_type, int p_id, const String *p_output_vars, int p_index, const String &p_texture_name, PortType p_port_type) const {
373 String code;
374 if (is_output_port_connected(p_index)) {
375 switch (p_port_type) {
376 case PORT_TYPE_VECTOR_2D: {
377 code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xy;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
378 } break;
379 case PORT_TYPE_VECTOR_3D: {
380 if (mode_2d) {
381 code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xy;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
382 } else {
383 code += vformat(" %s = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0).xyz;\n", p_output_vars[p_index], make_unique_id(p_type, p_id, p_texture_name));
384 }
385 } break;
386 default:
387 break;
388 }
389 }
390 return code;
391}
392
393String VisualShaderNodeParticleMeshEmitter::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 {
394 String code;
395 code += " {\n";
396 code += " int __scalar_ibuff = int(__rand_from_seed(__seed) * 65535.0) % " + itos(position_texture->get_width()) + ";\n";
397
398 code += _generate_code(p_type, p_id, p_output_vars, 0, "mesh_vx", VisualShaderNode::PORT_TYPE_VECTOR_3D);
399 code += _generate_code(p_type, p_id, p_output_vars, 1, "mesh_nm", VisualShaderNode::PORT_TYPE_VECTOR_3D);
400
401 if (is_output_port_connected(2) || is_output_port_connected(3)) {
402 code += vformat(" vec4 __vec4_buff = texelFetch(%s, ivec2(__scalar_ibuff, 0), 0);\n", make_unique_id(p_type, p_id, "mesh_col"));
403
404 if (is_output_port_connected(2)) {
405 code += " " + p_output_vars[2] + " = __vec4_buff.rgb;\n";
406 }
407 if (is_output_port_connected(3)) {
408 code += " " + p_output_vars[3] + " = __vec4_buff.a;\n";
409 }
410 }
411
412 code += _generate_code(p_type, p_id, p_output_vars, 4, "mesh_uv", VisualShaderNode::PORT_TYPE_VECTOR_2D);
413 code += _generate_code(p_type, p_id, p_output_vars, 5, "mesh_uv2", VisualShaderNode::PORT_TYPE_VECTOR_2D);
414
415 code += " }\n";
416 return code;
417}
418
419Vector<VisualShader::DefaultTextureParam> VisualShaderNodeParticleMeshEmitter::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
420 Vector<VisualShader::DefaultTextureParam> ret;
421
422 if (is_output_port_connected(0)) {
423 VisualShader::DefaultTextureParam dtp;
424 dtp.name = make_unique_id(p_type, p_id, "mesh_vx");
425 dtp.params.push_back(position_texture);
426 ret.push_back(dtp);
427 }
428
429 if (is_output_port_connected(1)) {
430 VisualShader::DefaultTextureParam dtp;
431 dtp.name = make_unique_id(p_type, p_id, "mesh_nm");
432 dtp.params.push_back(normal_texture);
433 ret.push_back(dtp);
434 }
435
436 if (is_output_port_connected(2) || is_output_port_connected(3)) {
437 VisualShader::DefaultTextureParam dtp;
438 dtp.name = make_unique_id(p_type, p_id, "mesh_col");
439 dtp.params.push_back(color_texture);
440 ret.push_back(dtp);
441 }
442
443 if (is_output_port_connected(4)) {
444 VisualShader::DefaultTextureParam dtp;
445 dtp.name = make_unique_id(p_type, p_id, "mesh_uv");
446 dtp.params.push_back(uv_texture);
447 ret.push_back(dtp);
448 }
449
450 if (is_output_port_connected(5)) {
451 VisualShader::DefaultTextureParam dtp;
452 dtp.name = make_unique_id(p_type, p_id, "mesh_uv2");
453 dtp.params.push_back(uv2_texture);
454 ret.push_back(dtp);
455 }
456
457 return ret;
458}
459
460void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector2> &p_array, Ref<ImageTexture> &r_texture) {
461 Ref<Image> image;
462 image.instantiate();
463
464 if (p_array.size() == 0) {
465 image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
466 } else {
467 image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
468 }
469
470 for (int i = 0; i < p_array.size(); i++) {
471 Vector2 v = p_array[i];
472 image->set_pixel(i, 0, Color(v.x, v.y, 0));
473 }
474 if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
475 r_texture->set_image(image);
476 } else {
477 r_texture->update(image);
478 }
479}
480
481void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Vector3> &p_array, Ref<ImageTexture> &r_texture) {
482 Ref<Image> image;
483 image.instantiate();
484
485 if (p_array.size() == 0) {
486 image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBF);
487 } else {
488 image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBF);
489 }
490
491 for (int i = 0; i < p_array.size(); i++) {
492 Vector3 v = p_array[i];
493 image->set_pixel(i, 0, Color(v.x, v.y, v.z));
494 }
495 if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
496 r_texture->set_image(image);
497 } else {
498 r_texture->update(image);
499 }
500}
501
502void VisualShaderNodeParticleMeshEmitter::_update_texture(const Vector<Color> &p_array, Ref<ImageTexture> &r_texture) {
503 Ref<Image> image;
504 image.instantiate();
505
506 if (p_array.size() == 0) {
507 image->initialize_data(1, 1, false, Image::Format::FORMAT_RGBA8);
508 } else {
509 image->initialize_data(p_array.size(), 1, false, Image::Format::FORMAT_RGBA8);
510 }
511
512 for (int i = 0; i < p_array.size(); i++) {
513 image->set_pixel(i, 0, p_array[i]);
514 }
515 if (r_texture->get_width() != p_array.size() || p_array.size() == 0) {
516 r_texture->set_image(image);
517 } else {
518 r_texture->update(image);
519 }
520}
521
522void VisualShaderNodeParticleMeshEmitter::_update_textures() {
523 if (!mesh.is_valid()) {
524 return;
525 }
526
527 Vector<Vector3> vertices;
528 Vector<Vector3> normals;
529 Vector<Color> colors;
530 Vector<Vector2> uvs;
531 Vector<Vector2> uvs2;
532
533 const int surface_count = mesh->get_surface_count();
534
535 if (use_all_surfaces) {
536 for (int i = 0; i < surface_count; i++) {
537 const Array surface_arrays = mesh->surface_get_arrays(i);
538 const int surface_arrays_size = surface_arrays.size();
539
540 // position
541 if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
542 Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
543 for (int j = 0; j < vertex_array.size(); j++) {
544 vertices.push_back((Vector3)vertex_array[j]);
545 }
546 }
547
548 // normal
549 if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
550 Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
551 for (int j = 0; j < normal_array.size(); j++) {
552 normals.push_back((Vector3)normal_array[j]);
553 }
554 }
555
556 // color
557 if (surface_arrays_size > Mesh::ARRAY_COLOR) {
558 Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
559 for (int j = 0; j < color_array.size(); j++) {
560 colors.push_back((Color)color_array[j]);
561 }
562 }
563
564 // uv
565 if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
566 Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
567 for (int j = 0; j < uv_array.size(); j++) {
568 uvs.push_back((Vector2)uv_array[j]);
569 }
570 }
571
572 // uv2
573 if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
574 Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
575 for (int j = 0; j < uv2_array.size(); j++) {
576 uvs2.push_back((Vector2)uv2_array[j]);
577 }
578 }
579 }
580 } else {
581 if (surface_index >= 0 && surface_index < surface_count) {
582 const Array surface_arrays = mesh->surface_get_arrays(surface_index);
583 const int surface_arrays_size = surface_arrays.size();
584
585 // position
586 if (surface_arrays_size > Mesh::ARRAY_VERTEX) {
587 Array vertex_array = surface_arrays[Mesh::ARRAY_VERTEX];
588 for (int i = 0; i < vertex_array.size(); i++) {
589 vertices.push_back((Vector3)vertex_array[i]);
590 }
591 }
592
593 // normal
594 if (surface_arrays_size > Mesh::ARRAY_NORMAL) {
595 Array normal_array = surface_arrays[Mesh::ARRAY_NORMAL];
596 for (int i = 0; i < normal_array.size(); i++) {
597 normals.push_back((Vector3)normal_array[i]);
598 }
599 }
600
601 // color
602 if (surface_arrays_size > Mesh::ARRAY_COLOR) {
603 Array color_array = surface_arrays[Mesh::ARRAY_COLOR];
604 for (int i = 0; i < color_array.size(); i++) {
605 colors.push_back((Color)color_array[i]);
606 }
607 }
608
609 // uv
610 if (surface_arrays_size > Mesh::ARRAY_TEX_UV) {
611 Array uv_array = surface_arrays[Mesh::ARRAY_TEX_UV];
612 for (int j = 0; j < uv_array.size(); j++) {
613 uvs.push_back((Vector2)uv_array[j]);
614 }
615 }
616
617 // uv2
618 if (surface_arrays_size > Mesh::ARRAY_TEX_UV2) {
619 Array uv2_array = surface_arrays[Mesh::ARRAY_TEX_UV2];
620 for (int j = 0; j < uv2_array.size(); j++) {
621 uvs2.push_back((Vector2)uv2_array[j]);
622 }
623 }
624 }
625 }
626
627 _update_texture(vertices, position_texture);
628 _update_texture(normals, normal_texture);
629 _update_texture(colors, color_texture);
630 _update_texture(uvs, uv_texture);
631 _update_texture(uvs2, uv2_texture);
632}
633
634void VisualShaderNodeParticleMeshEmitter::set_mesh(Ref<Mesh> p_mesh) {
635 if (mesh == p_mesh) {
636 return;
637 }
638
639 if (mesh.is_valid()) {
640 mesh->disconnect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
641 }
642
643 mesh = p_mesh;
644
645 if (mesh.is_valid()) {
646 mesh->connect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
647 }
648
649 emit_changed();
650}
651
652Ref<Mesh> VisualShaderNodeParticleMeshEmitter::get_mesh() const {
653 return mesh;
654}
655
656void VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces(bool p_enabled) {
657 if (use_all_surfaces == p_enabled) {
658 return;
659 }
660 use_all_surfaces = p_enabled;
661 emit_changed();
662}
663
664bool VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces() const {
665 return use_all_surfaces;
666}
667
668void VisualShaderNodeParticleMeshEmitter::set_surface_index(int p_surface_index) {
669 if (mesh.is_valid()) {
670 if (mesh->get_surface_count() > 0) {
671 p_surface_index = CLAMP(p_surface_index, 0, mesh->get_surface_count() - 1);
672 } else {
673 p_surface_index = 0;
674 }
675 } else if (p_surface_index < 0) {
676 p_surface_index = 0;
677 }
678 if (surface_index == p_surface_index) {
679 return;
680 }
681 surface_index = p_surface_index;
682 emit_changed();
683}
684
685int VisualShaderNodeParticleMeshEmitter::get_surface_index() const {
686 return surface_index;
687}
688
689Vector<StringName> VisualShaderNodeParticleMeshEmitter::get_editable_properties() const {
690 Vector<StringName> props = VisualShaderNodeParticleEmitter::get_editable_properties();
691
692 props.push_back("mesh");
693 props.push_back("use_all_surfaces");
694 if (!use_all_surfaces) {
695 props.push_back("surface_index");
696 }
697
698 return props;
699}
700
701HashMap<StringName, String> VisualShaderNodeParticleMeshEmitter::get_editable_properties_names() const {
702 HashMap<StringName, String> names = VisualShaderNodeParticleEmitter::get_editable_properties_names();
703
704 names.insert("mesh", RTR("Mesh"));
705 names.insert("use_all_surfaces", RTR("Use All Surfaces"));
706 if (!use_all_surfaces) {
707 names.insert("surface_index", RTR("Surface Index"));
708 }
709
710 return names;
711}
712
713void VisualShaderNodeParticleMeshEmitter::_bind_methods() {
714 ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &VisualShaderNodeParticleMeshEmitter::set_mesh);
715 ClassDB::bind_method(D_METHOD("get_mesh"), &VisualShaderNodeParticleMeshEmitter::get_mesh);
716 ClassDB::bind_method(D_METHOD("set_use_all_surfaces", "enabled"), &VisualShaderNodeParticleMeshEmitter::set_use_all_surfaces);
717 ClassDB::bind_method(D_METHOD("is_use_all_surfaces"), &VisualShaderNodeParticleMeshEmitter::is_use_all_surfaces);
718 ClassDB::bind_method(D_METHOD("set_surface_index", "surface_index"), &VisualShaderNodeParticleMeshEmitter::set_surface_index);
719 ClassDB::bind_method(D_METHOD("get_surface_index"), &VisualShaderNodeParticleMeshEmitter::get_surface_index);
720
721 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
722 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_all_surfaces"), "set_use_all_surfaces", "is_use_all_surfaces");
723 ADD_PROPERTY(PropertyInfo(Variant::INT, "surface_index"), "set_surface_index", "get_surface_index");
724}
725
726VisualShaderNodeParticleMeshEmitter::VisualShaderNodeParticleMeshEmitter() {
727 connect_changed(callable_mp(this, &VisualShaderNodeParticleMeshEmitter::_update_textures));
728
729 position_texture.instantiate();
730 normal_texture.instantiate();
731 color_texture.instantiate();
732 uv_texture.instantiate();
733 uv2_texture.instantiate();
734
735 simple_decl = false;
736}
737
738// VisualShaderNodeParticleMultiplyByAxisAngle
739
740void VisualShaderNodeParticleMultiplyByAxisAngle::_bind_methods() {
741 ClassDB::bind_method(D_METHOD("set_degrees_mode", "enabled"), &VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode);
742 ClassDB::bind_method(D_METHOD("is_degrees_mode"), &VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode);
743
744 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "degrees_mode"), "set_degrees_mode", "is_degrees_mode");
745}
746
747String VisualShaderNodeParticleMultiplyByAxisAngle::get_caption() const {
748 return "MultiplyByAxisAngle";
749}
750
751int VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_count() const {
752 return 3;
753}
754
755VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_type(int p_port) const {
756 if (p_port == 0 || p_port == 1) { // position, rotation_axis
757 return PORT_TYPE_VECTOR_3D;
758 }
759 return PORT_TYPE_SCALAR; // angle (degrees/radians)
760}
761
762String VisualShaderNodeParticleMultiplyByAxisAngle::get_input_port_name(int p_port) const {
763 if (p_port == 0) {
764 return "position";
765 }
766 if (p_port == 1) {
767 return "axis";
768 }
769 if (p_port == 2) {
770 if (degrees_mode) {
771 return "angle (degrees)";
772 } else {
773 return "angle (radians)";
774 }
775 }
776 return String();
777}
778
779bool VisualShaderNodeParticleMultiplyByAxisAngle::is_show_prop_names() const {
780 return true;
781}
782
783int VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_count() const {
784 return 1;
785}
786
787VisualShaderNodeParticleMultiplyByAxisAngle::PortType VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_type(int p_port) const {
788 return PORT_TYPE_VECTOR_3D;
789}
790
791String VisualShaderNodeParticleMultiplyByAxisAngle::get_output_port_name(int p_port) const {
792 return "position";
793}
794
795String VisualShaderNodeParticleMultiplyByAxisAngle::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 {
796 String code;
797 if (degrees_mode) {
798 code += " " + p_output_vars[0] + " = __build_rotation_mat3(" + (p_input_vars[1].is_empty() ? ("vec3" + (String)get_input_port_default_value(1)) : p_input_vars[1]) + ", radians(" + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ")) * " + (p_input_vars[0].is_empty() ? "vec3(0.0)" : p_input_vars[0]) + ";\n";
799 } else {
800 code += " " + p_output_vars[0] + " = __build_rotation_mat3(" + (p_input_vars[1].is_empty() ? ("vec3" + (String)get_input_port_default_value(1)) : p_input_vars[1]) + ", " + (p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]) + ") * " + (p_input_vars[0].is_empty() ? "vec3(0.0)" : p_input_vars[0]) + ";\n";
801 }
802 return code;
803}
804
805void VisualShaderNodeParticleMultiplyByAxisAngle::set_degrees_mode(bool p_enabled) {
806 degrees_mode = p_enabled;
807 emit_changed();
808}
809
810bool VisualShaderNodeParticleMultiplyByAxisAngle::is_degrees_mode() const {
811 return degrees_mode;
812}
813
814Vector<StringName> VisualShaderNodeParticleMultiplyByAxisAngle::get_editable_properties() const {
815 Vector<StringName> props;
816 props.push_back("degrees_mode");
817 return props;
818}
819
820bool VisualShaderNodeParticleMultiplyByAxisAngle::has_output_port_preview(int p_port) const {
821 return false;
822}
823
824VisualShaderNodeParticleMultiplyByAxisAngle::VisualShaderNodeParticleMultiplyByAxisAngle() {
825 set_input_port_default_value(1, Vector3(1, 0, 0));
826 set_input_port_default_value(2, 0.0);
827}
828
829// VisualShaderNodeParticleConeVelocity
830
831String VisualShaderNodeParticleConeVelocity::get_caption() const {
832 return "ConeVelocity";
833}
834
835int VisualShaderNodeParticleConeVelocity::get_input_port_count() const {
836 return 2;
837}
838
839VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_input_port_type(int p_port) const {
840 if (p_port == 0) {
841 return PORT_TYPE_VECTOR_3D;
842 } else if (p_port == 1) {
843 return PORT_TYPE_SCALAR;
844 }
845 return PORT_TYPE_SCALAR;
846}
847
848String VisualShaderNodeParticleConeVelocity::get_input_port_name(int p_port) const {
849 if (p_port == 0) {
850 return "direction";
851 } else if (p_port == 1) {
852 return "spread(degrees)";
853 }
854 return String();
855}
856
857int VisualShaderNodeParticleConeVelocity::get_output_port_count() const {
858 return 1;
859}
860
861VisualShaderNodeParticleConeVelocity::PortType VisualShaderNodeParticleConeVelocity::get_output_port_type(int p_port) const {
862 return PORT_TYPE_VECTOR_3D;
863}
864
865String VisualShaderNodeParticleConeVelocity::get_output_port_name(int p_port) const {
866 if (p_port == 0) {
867 return "velocity";
868 }
869 return String();
870}
871
872bool VisualShaderNodeParticleConeVelocity::has_output_port_preview(int p_port) const {
873 return false;
874}
875
876String VisualShaderNodeParticleConeVelocity::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 {
877 String code;
878 code += " {\n";
879 code += " float __radians = radians(" + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ");\n";
880 code += " float __scalar_buff1 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
881 code += " float __scalar_buff2 = __rand_from_seed_m1_p1(__seed) * __radians;\n";
882 code += " vec3 __vec3_buff1 = " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + ";\n";
883 code += " __scalar_buff1 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.x, __vec3_buff1.z) : sign(__vec3_buff1.x) * (PI / 2.0);\n";
884 code += " __scalar_buff2 += __vec3_buff1.z != 0.0 ? atan(__vec3_buff1.y, abs(__vec3_buff1.z)) : (__vec3_buff1.x != 0.0 ? atan(__vec3_buff1.y, abs(__vec3_buff1.x)) : sign(__vec3_buff1.y) * (PI / 2.0));\n";
885 code += " __vec3_buff1 = vec3(sin(__scalar_buff1), 0.0, cos(__scalar_buff1));\n";
886 code += " vec3 __vec3_buff2 = vec3(0.0, sin(__scalar_buff2), cos(__scalar_buff2));\n";
887 code += " __vec3_buff2.z = __vec3_buff2.z / max(0.0001, sqrt(abs(__vec3_buff2.z)));\n";
888 code += " " + p_output_vars[0] + " = normalize(vec3(__vec3_buff1.x * __vec3_buff2.z, __vec3_buff2.y, __vec3_buff1.z * __vec3_buff2.z));\n";
889 code += " }\n";
890 return code;
891}
892
893VisualShaderNodeParticleConeVelocity::VisualShaderNodeParticleConeVelocity() {
894 set_input_port_default_value(0, Vector3(1, 0, 0));
895 set_input_port_default_value(1, 45.0);
896
897 simple_decl = false;
898}
899
900// VisualShaderNodeParticleRandomness
901
902void VisualShaderNodeParticleRandomness::_bind_methods() {
903 ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeParticleRandomness::set_op_type);
904 ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeParticleRandomness::get_op_type);
905
906 ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
907
908 BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
909 BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
910 BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
911 BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
912 BIND_ENUM_CONSTANT(OP_TYPE_MAX);
913}
914
915Vector<StringName> VisualShaderNodeParticleRandomness::get_editable_properties() const {
916 Vector<StringName> props;
917 props.push_back("op_type");
918 return props;
919}
920
921String VisualShaderNodeParticleRandomness::get_caption() const {
922 return "ParticleRandomness";
923}
924
925int VisualShaderNodeParticleRandomness::get_output_port_count() const {
926 return 1;
927}
928
929VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_output_port_type(int p_port) const {
930 switch (op_type) {
931 case OP_TYPE_VECTOR_2D:
932 return PORT_TYPE_VECTOR_2D;
933 case OP_TYPE_VECTOR_3D:
934 return PORT_TYPE_VECTOR_3D;
935 case OP_TYPE_VECTOR_4D:
936 return PORT_TYPE_VECTOR_4D;
937 default:
938 break;
939 }
940 return PORT_TYPE_SCALAR;
941}
942
943String VisualShaderNodeParticleRandomness::get_output_port_name(int p_port) const {
944 return "random";
945}
946
947int VisualShaderNodeParticleRandomness::get_input_port_count() const {
948 return 3;
949}
950
951VisualShaderNodeParticleRandomness::PortType VisualShaderNodeParticleRandomness::get_input_port_type(int p_port) const {
952 switch (p_port) {
953 case 0:
954 return PORT_TYPE_SCALAR_UINT;
955 case 1:
956 case 2:
957 switch (op_type) {
958 case OP_TYPE_VECTOR_2D:
959 return PORT_TYPE_VECTOR_2D;
960 case OP_TYPE_VECTOR_3D:
961 return PORT_TYPE_VECTOR_3D;
962 case OP_TYPE_VECTOR_4D:
963 return PORT_TYPE_VECTOR_4D;
964 default:
965 break;
966 }
967 break;
968 }
969 return PORT_TYPE_SCALAR;
970}
971
972String VisualShaderNodeParticleRandomness::get_input_port_name(int p_port) const {
973 switch (p_port) {
974 case 0:
975 return "seed";
976 case 1:
977 return "min";
978 case 2:
979 return "max";
980 }
981 return String();
982}
983
984bool VisualShaderNodeParticleRandomness::is_input_port_default(int p_port, Shader::Mode p_mode) const {
985 return p_port == 0; // seed
986}
987
988String VisualShaderNodeParticleRandomness::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
989 String code;
990
991 code += "vec2 __randv2_range(inout uint seed, vec2 from, vec2 to) {\n";
992 code += " return vec2(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y));\n";
993 code += "}\n\n";
994
995 code += "vec3 __randv3_range(inout uint seed, vec3 from, vec3 to) {\n";
996 code += " return vec3(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y), __randf_range(seed, from.z, to.z));\n";
997 code += "}\n\n";
998
999 code += "vec4 __randv4_range(inout uint seed, vec4 from, vec4 to) {\n";
1000 code += " return vec4(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y), __randf_range(seed, from.z, to.z), __randf_range(seed, from.w, to.w));\n";
1001 code += "}\n\n";
1002
1003 return code;
1004}
1005
1006String VisualShaderNodeParticleRandomness::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 {
1007 static const char *func[(int)OP_TYPE_MAX] = { "__randf_range", "__randv2_range", "__randv3_range", "__randv4_range" };
1008 return vformat(" %s = %s(%s, %s, %s);\n", p_output_vars[0], func[op_type], p_input_vars[0].is_empty() ? "__seed" : p_input_vars[0], p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1], p_input_vars[2].is_empty() ? (String)get_input_port_default_value(2) : p_input_vars[2]);
1009}
1010
1011void VisualShaderNodeParticleRandomness::set_op_type(OpType p_op_type) {
1012 ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
1013 if (op_type == p_op_type) {
1014 return;
1015 }
1016 switch (p_op_type) {
1017 case OP_TYPE_SCALAR: {
1018 set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
1019 set_input_port_default_value(2, 0.0, get_input_port_default_value(2));
1020 } break;
1021 case OP_TYPE_VECTOR_2D: {
1022 set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
1023 set_input_port_default_value(2, Vector2(), get_input_port_default_value(2));
1024 } break;
1025 case OP_TYPE_VECTOR_3D: {
1026 set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
1027 set_input_port_default_value(2, Vector3(), get_input_port_default_value(2));
1028 } break;
1029 case OP_TYPE_VECTOR_4D: {
1030 set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
1031 set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2));
1032 } break;
1033 default:
1034 break;
1035 }
1036 op_type = p_op_type;
1037 emit_changed();
1038}
1039
1040VisualShaderNodeParticleRandomness::OpType VisualShaderNodeParticleRandomness::get_op_type() const {
1041 return op_type;
1042}
1043
1044bool VisualShaderNodeParticleRandomness::has_output_port_preview(int p_port) const {
1045 return false;
1046}
1047
1048VisualShaderNodeParticleRandomness::VisualShaderNodeParticleRandomness() {
1049 set_input_port_default_value(1, -1.0);
1050 set_input_port_default_value(2, 1.0);
1051}
1052
1053// VisualShaderNodeParticleAccelerator
1054
1055void VisualShaderNodeParticleAccelerator::_bind_methods() {
1056 ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShaderNodeParticleAccelerator::set_mode);
1057 ClassDB::bind_method(D_METHOD("get_mode"), &VisualShaderNodeParticleAccelerator::get_mode);
1058
1059 ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Linear,Radial,Tangential"), "set_mode", "get_mode");
1060
1061 BIND_ENUM_CONSTANT(MODE_LINEAR);
1062 BIND_ENUM_CONSTANT(MODE_RADIAL)
1063 BIND_ENUM_CONSTANT(MODE_TANGENTIAL);
1064 BIND_ENUM_CONSTANT(MODE_MAX);
1065}
1066
1067Vector<StringName> VisualShaderNodeParticleAccelerator::get_editable_properties() const {
1068 Vector<StringName> props;
1069 props.push_back("mode");
1070 return props;
1071}
1072
1073String VisualShaderNodeParticleAccelerator::get_caption() const {
1074 return "ParticleAccelerator";
1075}
1076
1077int VisualShaderNodeParticleAccelerator::get_output_port_count() const {
1078 return 1;
1079}
1080
1081VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_output_port_type(int p_port) const {
1082 return PORT_TYPE_VECTOR_3D;
1083}
1084
1085String VisualShaderNodeParticleAccelerator::get_output_port_name(int p_port) const {
1086 return String();
1087}
1088
1089int VisualShaderNodeParticleAccelerator::get_input_port_count() const {
1090 return 3;
1091}
1092
1093VisualShaderNodeParticleAccelerator::PortType VisualShaderNodeParticleAccelerator::get_input_port_type(int p_port) const {
1094 if (p_port == 0) {
1095 return PORT_TYPE_VECTOR_3D;
1096 } else if (p_port == 1) {
1097 return PORT_TYPE_SCALAR;
1098 } else if (p_port == 2) {
1099 return PORT_TYPE_VECTOR_3D;
1100 }
1101 return PORT_TYPE_SCALAR;
1102}
1103
1104String VisualShaderNodeParticleAccelerator::get_input_port_name(int p_port) const {
1105 if (p_port == 0) {
1106 return "amount";
1107 } else if (p_port == 1) {
1108 return "randomness";
1109 } else if (p_port == 2) {
1110 return "axis";
1111 }
1112 return String();
1113}
1114
1115String VisualShaderNodeParticleAccelerator::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 {
1116 String code;
1117 code += " {\n";
1118 switch (mode) {
1119 case MODE_LINEAR:
1120 code += " " + p_output_vars[0] + " = length(VELOCITY) > 0.0 ? " + "normalize(VELOCITY) * " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ") : vec3(0.0);\n";
1121 break;
1122 case MODE_RADIAL:
1123 code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
1124 code += " vec3 __ndiff = normalize(__diff);\n\n";
1125 code += " " + p_output_vars[0] + " = length(__diff) > 0.0 ? __ndiff * " + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ") : vec3(0.0);\n";
1126 break;
1127 case MODE_TANGENTIAL:
1128 code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
1129 code += " vec3 __ndiff = normalize(__diff);\n\n";
1130 code += " vec3 __vec3_buff1 = cross(__ndiff, normalize(" + (p_input_vars[2].is_empty() ? "vec3" + (String)get_input_port_default_value(2) : p_input_vars[2]) + "));\n";
1131 code += " " + p_output_vars[0] + " = length(__vec3_buff1) > 0.0 ? normalize(__vec3_buff1) * (" + (p_input_vars[0].is_empty() ? "vec3" + (String)get_input_port_default_value(0) : p_input_vars[0]) + " * mix(1.0, __rand_from_seed(__seed), " + (p_input_vars[1].is_empty() ? (String)get_input_port_default_value(1) : p_input_vars[1]) + ")) : vec3(0.0);\n";
1132 break;
1133 default:
1134 break;
1135 }
1136 code += " }\n";
1137 return code;
1138}
1139
1140void VisualShaderNodeParticleAccelerator::set_mode(Mode p_mode) {
1141 ERR_FAIL_INDEX(int(p_mode), int(MODE_MAX));
1142 if (mode == p_mode) {
1143 return;
1144 }
1145 mode = p_mode;
1146 emit_changed();
1147}
1148
1149VisualShaderNodeParticleAccelerator::Mode VisualShaderNodeParticleAccelerator::get_mode() const {
1150 return mode;
1151}
1152
1153bool VisualShaderNodeParticleAccelerator::has_output_port_preview(int p_port) const {
1154 return false;
1155}
1156
1157VisualShaderNodeParticleAccelerator::VisualShaderNodeParticleAccelerator() {
1158 set_input_port_default_value(0, Vector3(1, 1, 1));
1159 set_input_port_default_value(1, 0.0);
1160 set_input_port_default_value(2, Vector3(0, -9.8, 0));
1161
1162 simple_decl = false;
1163}
1164
1165// VisualShaderNodeParticleOutput
1166
1167String VisualShaderNodeParticleOutput::get_caption() const {
1168 switch (shader_type) {
1169 case VisualShader::TYPE_START:
1170 return "StartOutput";
1171 case VisualShader::TYPE_PROCESS:
1172 return "ProcessOutput";
1173 case VisualShader::TYPE_COLLIDE:
1174 return "CollideOutput";
1175 case VisualShader::TYPE_START_CUSTOM:
1176 return "CustomStartOutput";
1177 case VisualShader::TYPE_PROCESS_CUSTOM:
1178 return "CustomProcessOutput";
1179 default:
1180 ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
1181 return "";
1182 }
1183}
1184
1185int VisualShaderNodeParticleOutput::get_input_port_count() const {
1186 switch (shader_type) {
1187 case VisualShader::TYPE_START:
1188 return 8;
1189 case VisualShader::TYPE_PROCESS:
1190 return 7;
1191 case VisualShader::TYPE_COLLIDE:
1192 return 5;
1193 case VisualShader::TYPE_START_CUSTOM:
1194 case VisualShader::TYPE_PROCESS_CUSTOM:
1195 return 6;
1196 default:
1197 ERR_PRINT(vformat("Unexpected shader_type %d for VisualShaderNodeParticleOutput.", shader_type));
1198 return 0;
1199 }
1200}
1201
1202VisualShaderNodeParticleOutput::PortType VisualShaderNodeParticleOutput::get_input_port_type(int p_port) const {
1203 switch (p_port) {
1204 case 0:
1205 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1206 return PORT_TYPE_VECTOR_3D; // custom.rgb
1207 }
1208 return PORT_TYPE_BOOLEAN; // active
1209 case 1:
1210 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1211 break; // custom.a (scalar)
1212 }
1213 return PORT_TYPE_VECTOR_3D; // velocity
1214 case 2:
1215 return PORT_TYPE_VECTOR_3D; // color & velocity
1216 case 3:
1217 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1218 return PORT_TYPE_VECTOR_3D; // color
1219 }
1220 break; // alpha (scalar)
1221 case 4:
1222 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1223 break; // alpha
1224 }
1225 if (shader_type == VisualShader::TYPE_PROCESS) {
1226 break; // scale
1227 }
1228 if (shader_type == VisualShader::TYPE_COLLIDE) {
1229 return PORT_TYPE_TRANSFORM; // transform
1230 }
1231 return PORT_TYPE_VECTOR_3D; // position
1232 case 5:
1233 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1234 return PORT_TYPE_TRANSFORM; // transform
1235 }
1236 if (shader_type == VisualShader::TYPE_PROCESS) {
1237 return PORT_TYPE_VECTOR_3D; // rotation_axis
1238 }
1239 break; // scale (scalar)
1240 case 6:
1241 if (shader_type == VisualShader::TYPE_START) {
1242 return PORT_TYPE_VECTOR_3D; // rotation_axis
1243 }
1244 break;
1245 case 7:
1246 break; // angle (scalar)
1247 }
1248 return PORT_TYPE_SCALAR;
1249}
1250
1251String VisualShaderNodeParticleOutput::get_input_port_name(int p_port) const {
1252 String port_name;
1253 switch (p_port) {
1254 case 0:
1255 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1256 port_name = "custom";
1257 break;
1258 }
1259 port_name = "active";
1260 break;
1261 case 1:
1262 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1263 port_name = "custom_alpha";
1264 break;
1265 }
1266 port_name = "velocity";
1267 break;
1268 case 2:
1269 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1270 port_name = "velocity";
1271 break;
1272 }
1273 port_name = "color";
1274 break;
1275 case 3:
1276 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1277 port_name = "color";
1278 break;
1279 }
1280 port_name = "alpha";
1281 break;
1282 case 4:
1283 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1284 port_name = "alpha";
1285 break;
1286 }
1287 if (shader_type == VisualShader::TYPE_PROCESS) {
1288 port_name = "scale";
1289 break;
1290 }
1291 if (shader_type == VisualShader::TYPE_COLLIDE) {
1292 port_name = "transform";
1293 break;
1294 }
1295 port_name = "position";
1296 break;
1297 case 5:
1298 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1299 port_name = "transform";
1300 break;
1301 }
1302 if (shader_type == VisualShader::TYPE_PROCESS) {
1303 port_name = "rotation_axis";
1304 break;
1305 }
1306 port_name = "scale";
1307 break;
1308 case 6:
1309 if (shader_type == VisualShader::TYPE_PROCESS) {
1310 port_name = "angle_in_radians";
1311 break;
1312 }
1313 port_name = "rotation_axis";
1314 break;
1315 case 7:
1316 port_name = "angle_in_radians";
1317 break;
1318 default:
1319 break;
1320 }
1321 if (!port_name.is_empty()) {
1322 return port_name.capitalize();
1323 }
1324 return String();
1325}
1326
1327bool VisualShaderNodeParticleOutput::is_port_separator(int p_index) const {
1328 if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
1329 String port_name = get_input_port_name(p_index);
1330 return bool(port_name == "Scale");
1331 }
1332 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1333 String port_name = get_input_port_name(p_index);
1334 return bool(port_name == "Velocity");
1335 }
1336 return false;
1337}
1338
1339String VisualShaderNodeParticleOutput::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 {
1340 String code;
1341 String tab = " ";
1342
1343 if (shader_type == VisualShader::TYPE_START_CUSTOM || shader_type == VisualShader::TYPE_PROCESS_CUSTOM) {
1344 if (!p_input_vars[0].is_empty()) { // custom.rgb
1345 code += tab + "CUSTOM.rgb = " + p_input_vars[0] + ";\n";
1346 }
1347 if (!p_input_vars[1].is_empty()) { // custom.a
1348 code += tab + "CUSTOM.a = " + p_input_vars[1] + ";\n";
1349 }
1350 if (!p_input_vars[2].is_empty()) { // velocity
1351 code += tab + "VELOCITY = " + p_input_vars[2] + ";\n";
1352 }
1353 if (!p_input_vars[3].is_empty()) { // color.rgb
1354 code += tab + "COLOR.rgb = " + p_input_vars[3] + ";\n";
1355 }
1356 if (!p_input_vars[4].is_empty()) { // color.a
1357 code += tab + "COLOR.a = " + p_input_vars[4] + ";\n";
1358 }
1359 if (!p_input_vars[5].is_empty()) { // transform
1360 code += tab + "TRANSFORM = " + p_input_vars[5] + ";\n";
1361 }
1362 } else {
1363 if (!p_input_vars[0].is_empty()) { // Active (begin).
1364 code += tab + "ACTIVE = " + p_input_vars[0] + ";\n";
1365 code += tab + "if(ACTIVE) {\n";
1366 tab += " ";
1367 }
1368 if (!p_input_vars[1].is_empty()) { // velocity
1369 code += tab + "VELOCITY = " + p_input_vars[1] + ";\n";
1370 }
1371 if (!p_input_vars[2].is_empty()) { // color
1372 code += tab + "COLOR.rgb = " + p_input_vars[2] + ";\n";
1373 }
1374 if (!p_input_vars[3].is_empty()) { // alpha
1375 code += tab + "COLOR.a = " + p_input_vars[3] + ";\n";
1376 }
1377
1378 // position
1379 if (shader_type == VisualShader::TYPE_START) {
1380 code += tab + "if (RESTART_POSITION) {\n";
1381 if (!p_input_vars[4].is_empty()) {
1382 code += tab + " TRANSFORM = mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(" + p_input_vars[4] + ", 1.0));\n";
1383 } else {
1384 code += tab + " TRANSFORM = mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
1385 }
1386 code += tab + " if (RESTART_VELOCITY) {\n";
1387 code += tab + " VELOCITY = (EMISSION_TRANSFORM * vec4(VELOCITY, 0.0)).xyz;\n";
1388 code += tab + " }\n";
1389 code += tab + " TRANSFORM = EMISSION_TRANSFORM * TRANSFORM;\n";
1390 code += tab + "}\n";
1391 } else if (shader_type == VisualShader::TYPE_COLLIDE) { // position
1392 if (!p_input_vars[4].is_empty()) {
1393 code += tab + "TRANSFORM = " + p_input_vars[4] + ";\n";
1394 }
1395 }
1396
1397 if (shader_type == VisualShader::TYPE_START || shader_type == VisualShader::TYPE_PROCESS) {
1398 int scale = 5;
1399 int rotation_axis = 6;
1400 int rotation = 7;
1401 if (shader_type == VisualShader::TYPE_PROCESS) {
1402 scale = 4;
1403 rotation_axis = 5;
1404 rotation = 6;
1405 }
1406 String op;
1407 if (shader_type == VisualShader::TYPE_START) {
1408 op = "*=";
1409 } else {
1410 op = "=";
1411 }
1412
1413 if (!p_input_vars[rotation].is_empty()) { // rotation_axis & angle_in_radians
1414 String axis;
1415 if (p_input_vars[rotation_axis].is_empty()) {
1416 axis = "vec3(0, 1, 0)";
1417 } else {
1418 axis = p_input_vars[rotation_axis];
1419 }
1420 code += tab + "TRANSFORM " + op + " __build_rotation_mat4(" + axis + ", " + p_input_vars[rotation] + ");\n";
1421 }
1422 if (!p_input_vars[scale].is_empty()) { // scale
1423 code += tab + "TRANSFORM " + op + " mat4(vec4(" + p_input_vars[scale] + ", 0, 0, 0), vec4(0, " + p_input_vars[scale] + ", 0, 0), vec4(0, 0, " + p_input_vars[scale] + ", 0), vec4(0, 0, 0, 1));\n";
1424 }
1425 }
1426 if (!p_input_vars[0].is_empty()) { // Active (end).
1427 code += " }\n";
1428 }
1429 }
1430 return code;
1431}
1432
1433VisualShaderNodeParticleOutput::VisualShaderNodeParticleOutput() {
1434}
1435
1436// EmitParticle
1437
1438Vector<StringName> VisualShaderNodeParticleEmit::get_editable_properties() const {
1439 Vector<StringName> props;
1440 props.push_back("flags");
1441 return props;
1442}
1443
1444void VisualShaderNodeParticleEmit::_bind_methods() {
1445 ClassDB::bind_method(D_METHOD("set_flags", "flags"), &VisualShaderNodeParticleEmit::set_flags);
1446 ClassDB::bind_method(D_METHOD("get_flags"), &VisualShaderNodeParticleEmit::get_flags);
1447
1448 ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Position,RotScale,Velocity,Color,Custom"), "set_flags", "get_flags");
1449
1450 BIND_ENUM_CONSTANT(EMIT_FLAG_POSITION);
1451 BIND_ENUM_CONSTANT(EMIT_FLAG_ROT_SCALE);
1452 BIND_ENUM_CONSTANT(EMIT_FLAG_VELOCITY);
1453 BIND_ENUM_CONSTANT(EMIT_FLAG_COLOR);
1454 BIND_ENUM_CONSTANT(EMIT_FLAG_CUSTOM);
1455}
1456
1457String VisualShaderNodeParticleEmit::get_caption() const {
1458 return "EmitParticle";
1459}
1460
1461int VisualShaderNodeParticleEmit::get_input_port_count() const {
1462 return 7;
1463}
1464
1465VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_input_port_type(int p_port) const {
1466 switch (p_port) {
1467 case 0:
1468 return PORT_TYPE_BOOLEAN;
1469 case 1:
1470 return PORT_TYPE_TRANSFORM;
1471 case 2:
1472 return PORT_TYPE_VECTOR_3D;
1473 case 3:
1474 return PORT_TYPE_VECTOR_3D;
1475 case 4:
1476 return PORT_TYPE_SCALAR;
1477 case 5:
1478 return PORT_TYPE_VECTOR_3D;
1479 case 6:
1480 return PORT_TYPE_SCALAR;
1481 }
1482 return PORT_TYPE_SCALAR;
1483}
1484
1485String VisualShaderNodeParticleEmit::get_input_port_name(int p_port) const {
1486 switch (p_port) {
1487 case 0:
1488 return "condition";
1489 case 1:
1490 return "transform";
1491 case 2:
1492 return "velocity";
1493 case 3:
1494 return "color";
1495 case 4:
1496 return "alpha";
1497 case 5:
1498 return "custom";
1499 case 6:
1500 return "custom_alpha";
1501 }
1502 return String();
1503}
1504
1505int VisualShaderNodeParticleEmit::get_output_port_count() const {
1506 return 0;
1507}
1508
1509VisualShaderNodeParticleEmit::PortType VisualShaderNodeParticleEmit::get_output_port_type(int p_port) const {
1510 return PORT_TYPE_SCALAR;
1511}
1512
1513String VisualShaderNodeParticleEmit::get_output_port_name(int p_port) const {
1514 return String();
1515}
1516
1517void VisualShaderNodeParticleEmit::add_flag(EmitFlags p_flag) {
1518 flags |= p_flag;
1519 emit_changed();
1520}
1521
1522bool VisualShaderNodeParticleEmit::has_flag(EmitFlags p_flag) const {
1523 return flags & p_flag;
1524}
1525
1526void VisualShaderNodeParticleEmit::set_flags(EmitFlags p_flags) {
1527 flags = (int)p_flags;
1528 emit_changed();
1529}
1530
1531VisualShaderNodeParticleEmit::EmitFlags VisualShaderNodeParticleEmit::get_flags() const {
1532 return EmitFlags(flags);
1533}
1534
1535bool VisualShaderNodeParticleEmit::is_show_prop_names() const {
1536 return true;
1537}
1538
1539bool VisualShaderNodeParticleEmit::is_generate_input_var(int p_port) const {
1540 if (p_port == 0) {
1541 if (!is_input_port_connected(0)) {
1542 return false;
1543 }
1544 }
1545 return true;
1546}
1547
1548bool VisualShaderNodeParticleEmit::is_input_port_default(int p_port, Shader::Mode p_mode) const {
1549 switch (p_port) {
1550 case 1:
1551 return true;
1552 case 2:
1553 return true;
1554 case 3:
1555 return true;
1556 case 4:
1557 return true;
1558 case 5:
1559 return true;
1560 case 6:
1561 return true;
1562 }
1563 return false;
1564}
1565
1566String VisualShaderNodeParticleEmit::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 {
1567 String code;
1568 String tab;
1569 bool default_condition = false;
1570
1571 if (!is_input_port_connected(0)) {
1572 default_condition = true;
1573 if (get_input_port_default_value(0)) {
1574 tab = " ";
1575 } else {
1576 return code;
1577 }
1578 } else {
1579 tab = " ";
1580 }
1581
1582 String transform;
1583 if (p_input_vars[1].is_empty()) {
1584 transform = "TRANSFORM";
1585 } else {
1586 transform = p_input_vars[1];
1587 }
1588
1589 String velocity;
1590 if (p_input_vars[2].is_empty()) {
1591 velocity = "VELOCITY";
1592 } else {
1593 velocity = p_input_vars[2];
1594 }
1595
1596 String color;
1597 if (p_input_vars[3].is_empty()) {
1598 color = "COLOR.rgb";
1599 } else {
1600 color = p_input_vars[3];
1601 }
1602
1603 String alpha;
1604 if (p_input_vars[4].is_empty()) {
1605 alpha = "COLOR.a";
1606 } else {
1607 alpha = p_input_vars[4];
1608 }
1609
1610 String custom;
1611 if (p_input_vars[5].is_empty()) {
1612 custom = "CUSTOM.rgb";
1613 } else {
1614 custom = p_input_vars[5];
1615 }
1616
1617 String custom_alpha;
1618 if (p_input_vars[6].is_empty()) {
1619 custom_alpha = "CUSTOM.a";
1620 } else {
1621 custom_alpha = p_input_vars[6];
1622 }
1623
1624 List<String> flags_arr;
1625
1626 if (has_flag(EmitFlags::EMIT_FLAG_POSITION)) {
1627 flags_arr.push_back("FLAG_EMIT_POSITION");
1628 }
1629 if (has_flag(EmitFlags::EMIT_FLAG_ROT_SCALE)) {
1630 flags_arr.push_back("FLAG_EMIT_ROT_SCALE");
1631 }
1632 if (has_flag(EmitFlags::EMIT_FLAG_VELOCITY)) {
1633 flags_arr.push_back("FLAG_EMIT_VELOCITY");
1634 }
1635 if (has_flag(EmitFlags::EMIT_FLAG_COLOR)) {
1636 flags_arr.push_back("FLAG_EMIT_COLOR");
1637 }
1638 if (has_flag(EmitFlags::EMIT_FLAG_CUSTOM)) {
1639 flags_arr.push_back("FLAG_EMIT_CUSTOM");
1640 }
1641
1642 String flags_str;
1643
1644 for (int i = 0; i < flags_arr.size(); i++) {
1645 if (i > 0) {
1646 flags_str += "|";
1647 }
1648 flags_str += flags_arr[i];
1649 }
1650
1651 if (flags_str.is_empty()) {
1652 flags_str = "uint(0)";
1653 }
1654
1655 if (!default_condition) {
1656 code += " if (" + p_input_vars[0] + ") {\n";
1657 }
1658
1659 code += tab + "emit_subparticle(" + transform + ", " + velocity + ", vec4(" + color + ", " + alpha + "), vec4(" + custom + ", " + custom_alpha + "), " + flags_str + ");\n";
1660
1661 if (!default_condition) {
1662 code += " }\n";
1663 }
1664
1665 return code;
1666}
1667
1668VisualShaderNodeParticleEmit::VisualShaderNodeParticleEmit() {
1669 set_input_port_default_value(0, true);
1670}
1671