| 1 | /**************************************************************************/ |
| 2 | /* cpu_particles_3d.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 "cpu_particles_3d.h" |
| 32 | #include "cpu_particles_3d.compat.inc" |
| 33 | |
| 34 | #include "core/math/random_number_generator.h" |
| 35 | #include "scene/3d/camera_3d.h" |
| 36 | #include "scene/3d/gpu_particles_3d.h" |
| 37 | #include "scene/main/viewport.h" |
| 38 | #include "scene/resources/curve_texture.h" |
| 39 | #include "scene/resources/gradient_texture.h" |
| 40 | #include "scene/resources/mesh.h" |
| 41 | #include "scene/resources/particle_process_material.h" |
| 42 | |
| 43 | AABB CPUParticles3D::get_aabb() const { |
| 44 | return AABB(); |
| 45 | } |
| 46 | |
| 47 | void CPUParticles3D::set_emitting(bool p_emitting) { |
| 48 | if (emitting == p_emitting) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if (p_emitting && !use_fixed_seed && one_shot) { |
| 53 | set_seed(Math::rand()); |
| 54 | } |
| 55 | |
| 56 | emitting = p_emitting; |
| 57 | if (emitting) { |
| 58 | _set_emitting(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void CPUParticles3D::_set_emitting() { |
| 63 | active = true; |
| 64 | set_process_internal(true); |
| 65 | // first update before rendering to avoid one frame delay after emitting starts |
| 66 | if (time == 0) { |
| 67 | _update_internal(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void CPUParticles3D::set_amount(int p_amount) { |
| 72 | ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles must be greater than 0."); |
| 73 | |
| 74 | particles.resize(p_amount); |
| 75 | { |
| 76 | Particle *w = particles.ptrw(); |
| 77 | |
| 78 | for (int i = 0; i < p_amount; i++) { |
| 79 | w[i].active = false; |
| 80 | w[i].custom[3] = 1.0; // Make sure w component isn't garbage data and doesn't break shaders with CUSTOM.y/Custom.w |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | particle_data.resize((12 + 4 + 4) * p_amount); |
| 85 | RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1); |
| 86 | RS::get_singleton()->multimesh_allocate_data(multimesh, p_amount, RS::MULTIMESH_TRANSFORM_3D, true, true); |
| 87 | |
| 88 | particle_order.resize(p_amount); |
| 89 | } |
| 90 | |
| 91 | void CPUParticles3D::set_lifetime(double p_lifetime) { |
| 92 | ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0."); |
| 93 | lifetime = p_lifetime; |
| 94 | } |
| 95 | |
| 96 | void CPUParticles3D::set_one_shot(bool p_one_shot) { |
| 97 | one_shot = p_one_shot; |
| 98 | } |
| 99 | |
| 100 | void CPUParticles3D::set_pre_process_time(double p_time) { |
| 101 | pre_process_time = p_time; |
| 102 | } |
| 103 | |
| 104 | void CPUParticles3D::set_explosiveness_ratio(real_t p_ratio) { |
| 105 | explosiveness_ratio = p_ratio; |
| 106 | } |
| 107 | |
| 108 | void CPUParticles3D::set_randomness_ratio(real_t p_ratio) { |
| 109 | randomness_ratio = p_ratio; |
| 110 | } |
| 111 | |
| 112 | void CPUParticles3D::set_visibility_aabb(const AABB &p_aabb) { |
| 113 | RS::get_singleton()->multimesh_set_custom_aabb(multimesh, p_aabb); |
| 114 | visibility_aabb = p_aabb; |
| 115 | update_gizmos(); |
| 116 | } |
| 117 | |
| 118 | void CPUParticles3D::set_lifetime_randomness(double p_random) { |
| 119 | lifetime_randomness = p_random; |
| 120 | } |
| 121 | |
| 122 | void CPUParticles3D::set_use_local_coordinates(bool p_enable) { |
| 123 | local_coords = p_enable; |
| 124 | } |
| 125 | |
| 126 | void CPUParticles3D::set_speed_scale(double p_scale) { |
| 127 | speed_scale = p_scale; |
| 128 | } |
| 129 | |
| 130 | bool CPUParticles3D::is_emitting() const { |
| 131 | return emitting; |
| 132 | } |
| 133 | |
| 134 | int CPUParticles3D::get_amount() const { |
| 135 | return particles.size(); |
| 136 | } |
| 137 | |
| 138 | double CPUParticles3D::get_lifetime() const { |
| 139 | return lifetime; |
| 140 | } |
| 141 | |
| 142 | bool CPUParticles3D::get_one_shot() const { |
| 143 | return one_shot; |
| 144 | } |
| 145 | |
| 146 | double CPUParticles3D::get_pre_process_time() const { |
| 147 | return pre_process_time; |
| 148 | } |
| 149 | |
| 150 | real_t CPUParticles3D::get_explosiveness_ratio() const { |
| 151 | return explosiveness_ratio; |
| 152 | } |
| 153 | |
| 154 | real_t CPUParticles3D::get_randomness_ratio() const { |
| 155 | return randomness_ratio; |
| 156 | } |
| 157 | |
| 158 | AABB CPUParticles3D::get_visibility_aabb() const { |
| 159 | return visibility_aabb; |
| 160 | } |
| 161 | |
| 162 | double CPUParticles3D::get_lifetime_randomness() const { |
| 163 | return lifetime_randomness; |
| 164 | } |
| 165 | |
| 166 | bool CPUParticles3D::get_use_local_coordinates() const { |
| 167 | return local_coords; |
| 168 | } |
| 169 | |
| 170 | double CPUParticles3D::get_speed_scale() const { |
| 171 | return speed_scale; |
| 172 | } |
| 173 | |
| 174 | void CPUParticles3D::set_draw_order(DrawOrder p_order) { |
| 175 | ERR_FAIL_INDEX(p_order, DRAW_ORDER_MAX); |
| 176 | draw_order = p_order; |
| 177 | } |
| 178 | |
| 179 | CPUParticles3D::DrawOrder CPUParticles3D::get_draw_order() const { |
| 180 | return draw_order; |
| 181 | } |
| 182 | |
| 183 | void CPUParticles3D::set_mesh(const Ref<Mesh> &p_mesh) { |
| 184 | mesh = p_mesh; |
| 185 | if (mesh.is_valid()) { |
| 186 | RS::get_singleton()->multimesh_set_mesh(multimesh, mesh->get_rid()); |
| 187 | } else { |
| 188 | RS::get_singleton()->multimesh_set_mesh(multimesh, RID()); |
| 189 | } |
| 190 | |
| 191 | update_configuration_warnings(); |
| 192 | } |
| 193 | |
| 194 | Ref<Mesh> CPUParticles3D::get_mesh() const { |
| 195 | return mesh; |
| 196 | } |
| 197 | |
| 198 | void CPUParticles3D::set_fixed_fps(int p_count) { |
| 199 | fixed_fps = p_count; |
| 200 | } |
| 201 | |
| 202 | int CPUParticles3D::get_fixed_fps() const { |
| 203 | return fixed_fps; |
| 204 | } |
| 205 | |
| 206 | void CPUParticles3D::set_fractional_delta(bool p_enable) { |
| 207 | fractional_delta = p_enable; |
| 208 | } |
| 209 | |
| 210 | bool CPUParticles3D::get_fractional_delta() const { |
| 211 | return fractional_delta; |
| 212 | } |
| 213 | |
| 214 | PackedStringArray CPUParticles3D::get_configuration_warnings() const { |
| 215 | PackedStringArray warnings = GeometryInstance3D::get_configuration_warnings(); |
| 216 | |
| 217 | bool mesh_found = false; |
| 218 | bool anim_material_found = false; |
| 219 | |
| 220 | if (get_mesh().is_valid()) { |
| 221 | mesh_found = true; |
| 222 | for (int j = 0; j < get_mesh()->get_surface_count(); j++) { |
| 223 | anim_material_found = Object::cast_to<ShaderMaterial>(get_mesh()->surface_get_material(j).ptr()) != nullptr; |
| 224 | StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(get_mesh()->surface_get_material(j).ptr()); |
| 225 | anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != nullptr; |
| 230 | StandardMaterial3D *spat = Object::cast_to<StandardMaterial3D>(get_material_override().ptr()); |
| 231 | anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == StandardMaterial3D::BILLBOARD_PARTICLES); |
| 232 | |
| 233 | if (!mesh_found) { |
| 234 | warnings.push_back(RTR("Nothing is visible because no mesh has been assigned.")); |
| 235 | } |
| 236 | |
| 237 | if (!anim_material_found && (get_param_max(PARAM_ANIM_SPEED) != 0.0 || get_param_max(PARAM_ANIM_OFFSET) != 0.0 || get_param_curve(PARAM_ANIM_SPEED).is_valid() || get_param_curve(PARAM_ANIM_OFFSET).is_valid())) { |
| 238 | warnings.push_back(RTR("CPUParticles3D animation requires the usage of a StandardMaterial3D whose Billboard Mode is set to \"Particle Billboard\".")); |
| 239 | } |
| 240 | |
| 241 | return warnings; |
| 242 | } |
| 243 | |
| 244 | void CPUParticles3D::restart(bool p_keep_seed) { |
| 245 | time = 0; |
| 246 | frame_remainder = 0; |
| 247 | cycle = 0; |
| 248 | emitting = false; |
| 249 | |
| 250 | { |
| 251 | int pc = particles.size(); |
| 252 | Particle *w = particles.ptrw(); |
| 253 | |
| 254 | for (int i = 0; i < pc; i++) { |
| 255 | w[i].active = false; |
| 256 | } |
| 257 | } |
| 258 | if (!p_keep_seed && !use_fixed_seed) { |
| 259 | seed = Math::rand(); |
| 260 | } |
| 261 | |
| 262 | emitting = true; |
| 263 | _set_emitting(); |
| 264 | } |
| 265 | |
| 266 | void CPUParticles3D::set_direction(Vector3 p_direction) { |
| 267 | direction = p_direction; |
| 268 | } |
| 269 | |
| 270 | Vector3 CPUParticles3D::get_direction() const { |
| 271 | return direction; |
| 272 | } |
| 273 | |
| 274 | void CPUParticles3D::set_spread(real_t p_spread) { |
| 275 | spread = p_spread; |
| 276 | } |
| 277 | |
| 278 | real_t CPUParticles3D::get_spread() const { |
| 279 | return spread; |
| 280 | } |
| 281 | |
| 282 | void CPUParticles3D::set_flatness(real_t p_flatness) { |
| 283 | flatness = p_flatness; |
| 284 | } |
| 285 | |
| 286 | real_t CPUParticles3D::get_flatness() const { |
| 287 | return flatness; |
| 288 | } |
| 289 | |
| 290 | void CPUParticles3D::set_param_min(Parameter p_param, real_t p_value) { |
| 291 | ERR_FAIL_INDEX(p_param, PARAM_MAX); |
| 292 | |
| 293 | parameters_min[p_param] = p_value; |
| 294 | if (parameters_min[p_param] > parameters_max[p_param]) { |
| 295 | set_param_max(p_param, p_value); |
| 296 | } |
| 297 | |
| 298 | update_configuration_warnings(); |
| 299 | } |
| 300 | |
| 301 | real_t CPUParticles3D::get_param_min(Parameter p_param) const { |
| 302 | ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0); |
| 303 | |
| 304 | return parameters_min[p_param]; |
| 305 | } |
| 306 | |
| 307 | void CPUParticles3D::set_param_max(Parameter p_param, real_t p_value) { |
| 308 | ERR_FAIL_INDEX(p_param, PARAM_MAX); |
| 309 | |
| 310 | parameters_max[p_param] = p_value; |
| 311 | if (parameters_min[p_param] > parameters_max[p_param]) { |
| 312 | set_param_min(p_param, p_value); |
| 313 | } |
| 314 | |
| 315 | update_configuration_warnings(); |
| 316 | } |
| 317 | |
| 318 | real_t CPUParticles3D::get_param_max(Parameter p_param) const { |
| 319 | ERR_FAIL_INDEX_V(p_param, PARAM_MAX, 0); |
| 320 | |
| 321 | return parameters_max[p_param]; |
| 322 | } |
| 323 | |
| 324 | static void _adjust_curve_range(const Ref<Curve> &p_curve, real_t p_min, real_t p_max) { |
| 325 | Ref<Curve> curve = p_curve; |
| 326 | if (curve.is_null()) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | curve->ensure_default_setup(p_min, p_max); |
| 331 | } |
| 332 | |
| 333 | void CPUParticles3D::set_param_curve(Parameter p_param, const Ref<Curve> &p_curve) { |
| 334 | ERR_FAIL_INDEX(p_param, PARAM_MAX); |
| 335 | |
| 336 | curve_parameters[p_param] = p_curve; |
| 337 | |
| 338 | switch (p_param) { |
| 339 | case PARAM_INITIAL_LINEAR_VELOCITY: { |
| 340 | //do none for this one |
| 341 | } break; |
| 342 | case PARAM_ANGULAR_VELOCITY: { |
| 343 | _adjust_curve_range(p_curve, -360, 360); |
| 344 | } break; |
| 345 | case PARAM_ORBIT_VELOCITY: { |
| 346 | _adjust_curve_range(p_curve, -500, 500); |
| 347 | } break; |
| 348 | case PARAM_LINEAR_ACCEL: { |
| 349 | _adjust_curve_range(p_curve, -200, 200); |
| 350 | } break; |
| 351 | case PARAM_RADIAL_ACCEL: { |
| 352 | _adjust_curve_range(p_curve, -200, 200); |
| 353 | } break; |
| 354 | case PARAM_TANGENTIAL_ACCEL: { |
| 355 | _adjust_curve_range(p_curve, -200, 200); |
| 356 | } break; |
| 357 | case PARAM_DAMPING: { |
| 358 | _adjust_curve_range(p_curve, 0, 100); |
| 359 | } break; |
| 360 | case PARAM_ANGLE: { |
| 361 | _adjust_curve_range(p_curve, -360, 360); |
| 362 | } break; |
| 363 | case PARAM_SCALE: { |
| 364 | } break; |
| 365 | case PARAM_HUE_VARIATION: { |
| 366 | _adjust_curve_range(p_curve, -1, 1); |
| 367 | } break; |
| 368 | case PARAM_ANIM_SPEED: { |
| 369 | _adjust_curve_range(p_curve, 0, 200); |
| 370 | } break; |
| 371 | case PARAM_ANIM_OFFSET: { |
| 372 | } break; |
| 373 | default: { |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | update_configuration_warnings(); |
| 378 | } |
| 379 | |
| 380 | Ref<Curve> CPUParticles3D::get_param_curve(Parameter p_param) const { |
| 381 | ERR_FAIL_INDEX_V(p_param, PARAM_MAX, Ref<Curve>()); |
| 382 | |
| 383 | return curve_parameters[p_param]; |
| 384 | } |
| 385 | |
| 386 | void CPUParticles3D::set_color(const Color &p_color) { |
| 387 | color = p_color; |
| 388 | } |
| 389 | |
| 390 | Color CPUParticles3D::get_color() const { |
| 391 | return color; |
| 392 | } |
| 393 | |
| 394 | void CPUParticles3D::set_color_ramp(const Ref<Gradient> &p_ramp) { |
| 395 | color_ramp = p_ramp; |
| 396 | } |
| 397 | |
| 398 | Ref<Gradient> CPUParticles3D::get_color_ramp() const { |
| 399 | return color_ramp; |
| 400 | } |
| 401 | |
| 402 | void CPUParticles3D::set_color_initial_ramp(const Ref<Gradient> &p_ramp) { |
| 403 | color_initial_ramp = p_ramp; |
| 404 | } |
| 405 | |
| 406 | Ref<Gradient> CPUParticles3D::get_color_initial_ramp() const { |
| 407 | return color_initial_ramp; |
| 408 | } |
| 409 | |
| 410 | void CPUParticles3D::set_particle_flag(ParticleFlags p_particle_flag, bool p_enable) { |
| 411 | ERR_FAIL_INDEX(p_particle_flag, PARTICLE_FLAG_MAX); |
| 412 | particle_flags[p_particle_flag] = p_enable; |
| 413 | if (p_particle_flag == PARTICLE_FLAG_DISABLE_Z) { |
| 414 | notify_property_list_changed(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | bool CPUParticles3D::get_particle_flag(ParticleFlags p_particle_flag) const { |
| 419 | ERR_FAIL_INDEX_V(p_particle_flag, PARTICLE_FLAG_MAX, false); |
| 420 | return particle_flags[p_particle_flag]; |
| 421 | } |
| 422 | |
| 423 | void CPUParticles3D::set_emission_shape(EmissionShape p_shape) { |
| 424 | ERR_FAIL_INDEX(p_shape, EMISSION_SHAPE_MAX); |
| 425 | emission_shape = p_shape; |
| 426 | update_gizmos(); |
| 427 | } |
| 428 | |
| 429 | void CPUParticles3D::set_emission_sphere_radius(real_t p_radius) { |
| 430 | emission_sphere_radius = p_radius; |
| 431 | update_gizmos(); |
| 432 | } |
| 433 | |
| 434 | void CPUParticles3D::set_emission_box_extents(Vector3 p_extents) { |
| 435 | emission_box_extents = p_extents; |
| 436 | update_gizmos(); |
| 437 | } |
| 438 | |
| 439 | void CPUParticles3D::set_emission_points(const Vector<Vector3> &p_points) { |
| 440 | emission_points = p_points; |
| 441 | } |
| 442 | |
| 443 | void CPUParticles3D::set_emission_normals(const Vector<Vector3> &p_normals) { |
| 444 | emission_normals = p_normals; |
| 445 | } |
| 446 | |
| 447 | void CPUParticles3D::set_emission_colors(const Vector<Color> &p_colors) { |
| 448 | emission_colors = p_colors; |
| 449 | } |
| 450 | |
| 451 | void CPUParticles3D::set_emission_ring_axis(Vector3 p_axis) { |
| 452 | emission_ring_axis = p_axis; |
| 453 | update_gizmos(); |
| 454 | } |
| 455 | |
| 456 | void CPUParticles3D::set_emission_ring_height(real_t p_height) { |
| 457 | emission_ring_height = p_height; |
| 458 | update_gizmos(); |
| 459 | } |
| 460 | |
| 461 | void CPUParticles3D::set_emission_ring_radius(real_t p_radius) { |
| 462 | emission_ring_radius = p_radius; |
| 463 | update_gizmos(); |
| 464 | } |
| 465 | |
| 466 | void CPUParticles3D::set_emission_ring_inner_radius(real_t p_radius) { |
| 467 | emission_ring_inner_radius = p_radius; |
| 468 | update_gizmos(); |
| 469 | } |
| 470 | |
| 471 | void CPUParticles3D::set_emission_ring_cone_angle(real_t p_angle) { |
| 472 | emission_ring_cone_angle = p_angle; |
| 473 | update_gizmos(); |
| 474 | } |
| 475 | |
| 476 | void CPUParticles3D::set_scale_curve_x(Ref<Curve> p_scale_curve) { |
| 477 | scale_curve_x = p_scale_curve; |
| 478 | } |
| 479 | |
| 480 | void CPUParticles3D::set_scale_curve_y(Ref<Curve> p_scale_curve) { |
| 481 | scale_curve_y = p_scale_curve; |
| 482 | } |
| 483 | |
| 484 | void CPUParticles3D::set_scale_curve_z(Ref<Curve> p_scale_curve) { |
| 485 | scale_curve_z = p_scale_curve; |
| 486 | } |
| 487 | |
| 488 | void CPUParticles3D::set_split_scale(bool p_split_scale) { |
| 489 | split_scale = p_split_scale; |
| 490 | notify_property_list_changed(); |
| 491 | } |
| 492 | |
| 493 | real_t CPUParticles3D::get_emission_sphere_radius() const { |
| 494 | return emission_sphere_radius; |
| 495 | } |
| 496 | |
| 497 | Vector3 CPUParticles3D::get_emission_box_extents() const { |
| 498 | return emission_box_extents; |
| 499 | } |
| 500 | |
| 501 | Vector<Vector3> CPUParticles3D::get_emission_points() const { |
| 502 | return emission_points; |
| 503 | } |
| 504 | |
| 505 | Vector<Vector3> CPUParticles3D::get_emission_normals() const { |
| 506 | return emission_normals; |
| 507 | } |
| 508 | |
| 509 | Vector<Color> CPUParticles3D::get_emission_colors() const { |
| 510 | return emission_colors; |
| 511 | } |
| 512 | |
| 513 | Vector3 CPUParticles3D::get_emission_ring_axis() const { |
| 514 | return emission_ring_axis; |
| 515 | } |
| 516 | |
| 517 | real_t CPUParticles3D::get_emission_ring_height() const { |
| 518 | return emission_ring_height; |
| 519 | } |
| 520 | |
| 521 | real_t CPUParticles3D::get_emission_ring_radius() const { |
| 522 | return emission_ring_radius; |
| 523 | } |
| 524 | |
| 525 | real_t CPUParticles3D::get_emission_ring_inner_radius() const { |
| 526 | return emission_ring_inner_radius; |
| 527 | } |
| 528 | |
| 529 | real_t CPUParticles3D::get_emission_ring_cone_angle() const { |
| 530 | return emission_ring_cone_angle; |
| 531 | } |
| 532 | |
| 533 | CPUParticles3D::EmissionShape CPUParticles3D::get_emission_shape() const { |
| 534 | return emission_shape; |
| 535 | } |
| 536 | |
| 537 | void CPUParticles3D::set_gravity(const Vector3 &p_gravity) { |
| 538 | gravity = p_gravity; |
| 539 | } |
| 540 | |
| 541 | Vector3 CPUParticles3D::get_gravity() const { |
| 542 | return gravity; |
| 543 | } |
| 544 | |
| 545 | Ref<Curve> CPUParticles3D::get_scale_curve_x() const { |
| 546 | return scale_curve_x; |
| 547 | } |
| 548 | |
| 549 | Ref<Curve> CPUParticles3D::get_scale_curve_y() const { |
| 550 | return scale_curve_y; |
| 551 | } |
| 552 | |
| 553 | Ref<Curve> CPUParticles3D::get_scale_curve_z() const { |
| 554 | return scale_curve_z; |
| 555 | } |
| 556 | |
| 557 | bool CPUParticles3D::get_split_scale() { |
| 558 | return split_scale; |
| 559 | } |
| 560 | |
| 561 | AABB CPUParticles3D::capture_aabb() const { |
| 562 | RS::get_singleton()->multimesh_set_custom_aabb(multimesh, AABB()); |
| 563 | return RS::get_singleton()->multimesh_get_aabb(multimesh); |
| 564 | } |
| 565 | |
| 566 | void CPUParticles3D::set_use_fixed_seed(bool p_use_fixed_seed) { |
| 567 | if (p_use_fixed_seed == use_fixed_seed) { |
| 568 | return; |
| 569 | } |
| 570 | use_fixed_seed = p_use_fixed_seed; |
| 571 | notify_property_list_changed(); |
| 572 | } |
| 573 | |
| 574 | bool CPUParticles3D::get_use_fixed_seed() const { |
| 575 | return use_fixed_seed; |
| 576 | } |
| 577 | |
| 578 | void CPUParticles3D::set_seed(uint32_t p_seed) { |
| 579 | seed = p_seed; |
| 580 | } |
| 581 | |
| 582 | uint32_t CPUParticles3D::get_seed() const { |
| 583 | return seed; |
| 584 | } |
| 585 | |
| 586 | void CPUParticles3D::request_particles_process(real_t p_requested_process_time) { |
| 587 | _requested_process_time = p_requested_process_time; |
| 588 | } |
| 589 | |
| 590 | void CPUParticles3D::_validate_property(PropertyInfo &p_property) const { |
| 591 | if (Engine::get_singleton()->is_editor_hint() && p_property.name == "emitting") { |
| 592 | p_property.hint = one_shot ? PROPERTY_HINT_ONESHOT : PROPERTY_HINT_NONE; |
| 593 | } |
| 594 | |
| 595 | if (p_property.name == "emission_sphere_radius" && (emission_shape != EMISSION_SHAPE_SPHERE && emission_shape != EMISSION_SHAPE_SPHERE_SURFACE)) { |
| 596 | p_property.usage = PROPERTY_USAGE_NONE; |
| 597 | } |
| 598 | |
| 599 | if (p_property.name == "emission_box_extents" && emission_shape != EMISSION_SHAPE_BOX) { |
| 600 | p_property.usage = PROPERTY_USAGE_NONE; |
| 601 | } |
| 602 | |
| 603 | if ((p_property.name == "emission_point_texture" || p_property.name == "emission_color_texture" || p_property.name == "emission_points") && (emission_shape != EMISSION_SHAPE_POINTS && (emission_shape != EMISSION_SHAPE_DIRECTED_POINTS))) { |
| 604 | p_property.usage = PROPERTY_USAGE_NONE; |
| 605 | } |
| 606 | |
| 607 | if (p_property.name == "emission_normals" && emission_shape != EMISSION_SHAPE_DIRECTED_POINTS) { |
| 608 | p_property.usage = PROPERTY_USAGE_NONE; |
| 609 | } |
| 610 | |
| 611 | if (p_property.name.begins_with("emission_ring_") && emission_shape != EMISSION_SHAPE_RING) { |
| 612 | p_property.usage = PROPERTY_USAGE_NONE; |
| 613 | } |
| 614 | |
| 615 | if (p_property.name.begins_with("orbit_") && !particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 616 | p_property.usage = PROPERTY_USAGE_NONE; |
| 617 | } |
| 618 | |
| 619 | if (p_property.name.begins_with("scale_curve_") && !split_scale) { |
| 620 | p_property.usage = PROPERTY_USAGE_NONE; |
| 621 | } |
| 622 | |
| 623 | if (p_property.name == "seed" && !use_fixed_seed) { |
| 624 | p_property.usage = PROPERTY_USAGE_NONE; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | static uint32_t idhash(uint32_t x) { |
| 629 | x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b); |
| 630 | x = ((x >> uint32_t(16)) ^ x) * uint32_t(0x45d9f3b); |
| 631 | x = (x >> uint32_t(16)) ^ x; |
| 632 | return x; |
| 633 | } |
| 634 | |
| 635 | static real_t rand_from_seed(uint32_t &seed) { |
| 636 | int k; |
| 637 | int s = int(seed); |
| 638 | if (s == 0) { |
| 639 | s = 305420679; |
| 640 | } |
| 641 | k = s / 127773; |
| 642 | s = 16807 * (s - k * 127773) - 2836 * k; |
| 643 | if (s < 0) { |
| 644 | s += 2147483647; |
| 645 | } |
| 646 | seed = uint32_t(s); |
| 647 | return (seed % uint32_t(65536)) / 65535.0; |
| 648 | } |
| 649 | |
| 650 | void CPUParticles3D::_update_internal() { |
| 651 | if (particles.is_empty() || !is_visible_in_tree()) { |
| 652 | _set_redraw(false); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | double delta = get_process_delta_time(); |
| 657 | if (!active && !emitting) { |
| 658 | set_process_internal(false); |
| 659 | _set_redraw(false); |
| 660 | |
| 661 | //reset variables |
| 662 | time = 0; |
| 663 | frame_remainder = 0; |
| 664 | cycle = 0; |
| 665 | return; |
| 666 | } |
| 667 | _set_redraw(true); |
| 668 | |
| 669 | bool processed = false; |
| 670 | |
| 671 | double frame_time; |
| 672 | if (fixed_fps > 0) { |
| 673 | frame_time = 1.0 / fixed_fps; |
| 674 | } else { |
| 675 | frame_time = 1.0 / 30.0; |
| 676 | } |
| 677 | double todo = _requested_process_time; |
| 678 | _requested_process_time = 0.; |
| 679 | if (time == 0 && pre_process_time > 0.0) { |
| 680 | todo += pre_process_time; |
| 681 | } |
| 682 | real_t tmp_speed = speed_scale; |
| 683 | speed_scale = 1.0; |
| 684 | while (todo > 0) { |
| 685 | _particles_process(frame_time); |
| 686 | todo -= frame_time; |
| 687 | } |
| 688 | speed_scale = tmp_speed; |
| 689 | todo = 0.0; |
| 690 | |
| 691 | if (fixed_fps > 0) { |
| 692 | double decr = frame_time; |
| 693 | |
| 694 | double ldelta = delta; |
| 695 | if (ldelta > 0.1) { //avoid recursive stalls if fps goes below 10 |
| 696 | ldelta = 0.1; |
| 697 | } else if (ldelta <= 0.0) { //unlikely but.. |
| 698 | ldelta = 0.001; |
| 699 | } |
| 700 | todo = frame_remainder + ldelta; |
| 701 | |
| 702 | while (todo >= frame_time) { |
| 703 | _particles_process(frame_time); |
| 704 | processed = true; |
| 705 | todo -= decr; |
| 706 | } |
| 707 | |
| 708 | frame_remainder = todo; |
| 709 | |
| 710 | } else { |
| 711 | _particles_process(delta); |
| 712 | processed = true; |
| 713 | } |
| 714 | |
| 715 | if (processed) { |
| 716 | _update_particle_data_buffer(); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | void CPUParticles3D::_particles_process(double p_delta) { |
| 721 | p_delta *= speed_scale; |
| 722 | |
| 723 | int pcount = particles.size(); |
| 724 | Particle *w = particles.ptrw(); |
| 725 | |
| 726 | Particle *parray = w; |
| 727 | |
| 728 | double prev_time = time; |
| 729 | time += p_delta; |
| 730 | if (time > lifetime) { |
| 731 | time = Math::fmod(time, lifetime); |
| 732 | cycle++; |
| 733 | if (one_shot && cycle > 0) { |
| 734 | set_emitting(false); |
| 735 | notify_property_list_changed(); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | Transform3D emission_xform; |
| 740 | Basis velocity_xform; |
| 741 | if (!local_coords) { |
| 742 | emission_xform = get_global_transform_interpolated(); |
| 743 | velocity_xform = emission_xform.basis; |
| 744 | } |
| 745 | |
| 746 | double system_phase = time / lifetime; |
| 747 | |
| 748 | bool should_be_active = false; |
| 749 | for (int i = 0; i < pcount; i++) { |
| 750 | Particle &p = parray[i]; |
| 751 | |
| 752 | if (!emitting && !p.active) { |
| 753 | continue; |
| 754 | } |
| 755 | |
| 756 | double local_delta = p_delta; |
| 757 | |
| 758 | // The phase is a ratio between 0 (birth) and 1 (end of life) for each particle. |
| 759 | // While we use time in tests later on, for randomness we use the phase as done in the |
| 760 | // original shader code, and we later multiply by lifetime to get the time. |
| 761 | double restart_phase = double(i) / double(pcount); |
| 762 | |
| 763 | if (randomness_ratio > 0.0) { |
| 764 | uint32_t _seed = cycle; |
| 765 | if (restart_phase >= system_phase) { |
| 766 | _seed -= uint32_t(1); |
| 767 | } |
| 768 | _seed *= uint32_t(pcount); |
| 769 | _seed += uint32_t(i); |
| 770 | double random = double(idhash(_seed) % uint32_t(65536)) / 65536.0; |
| 771 | restart_phase += randomness_ratio * random * 1.0 / double(pcount); |
| 772 | } |
| 773 | |
| 774 | restart_phase *= (1.0 - explosiveness_ratio); |
| 775 | double restart_time = restart_phase * lifetime; |
| 776 | bool restart = false; |
| 777 | |
| 778 | if (time > prev_time) { |
| 779 | // restart_time >= prev_time is used so particles emit in the first frame they are processed |
| 780 | |
| 781 | if (restart_time >= prev_time && restart_time < time) { |
| 782 | restart = true; |
| 783 | if (fractional_delta) { |
| 784 | local_delta = time - restart_time; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | } else if (local_delta > 0.0) { |
| 789 | if (restart_time >= prev_time) { |
| 790 | restart = true; |
| 791 | if (fractional_delta) { |
| 792 | local_delta = lifetime - restart_time + time; |
| 793 | } |
| 794 | |
| 795 | } else if (restart_time < time) { |
| 796 | restart = true; |
| 797 | if (fractional_delta) { |
| 798 | local_delta = time - restart_time; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | if (p.time * (1.0 - explosiveness_ratio) > p.lifetime) { |
| 804 | restart = true; |
| 805 | } |
| 806 | |
| 807 | float tv = 0.0; |
| 808 | |
| 809 | if (restart) { |
| 810 | if (!emitting) { |
| 811 | p.active = false; |
| 812 | continue; |
| 813 | } |
| 814 | p.active = true; |
| 815 | |
| 816 | /*real_t tex_linear_velocity = 0; |
| 817 | if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) { |
| 818 | tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(0); |
| 819 | }*/ |
| 820 | |
| 821 | real_t tex_angle = 1.0; |
| 822 | if (curve_parameters[PARAM_ANGLE].is_valid()) { |
| 823 | tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv); |
| 824 | } |
| 825 | |
| 826 | real_t tex_anim_offset = 1.0; |
| 827 | if (curve_parameters[PARAM_ANGLE].is_valid()) { |
| 828 | tex_anim_offset = curve_parameters[PARAM_ANGLE]->sample(tv); |
| 829 | } |
| 830 | |
| 831 | p.seed = seed + uint32_t(1) + i + cycle * pcount; |
| 832 | rng->set_seed(p.seed); |
| 833 | p.angle_rand = rng->randf(); |
| 834 | p.scale_rand = rng->randf(); |
| 835 | p.hue_rot_rand = rng->randf(); |
| 836 | p.anim_offset_rand = rng->randf(); |
| 837 | |
| 838 | if (color_initial_ramp.is_valid()) { |
| 839 | p.start_color_rand = color_initial_ramp->get_color_at_offset(rng->randf()); |
| 840 | } else { |
| 841 | p.start_color_rand = Color(1, 1, 1, 1); |
| 842 | } |
| 843 | |
| 844 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 845 | real_t angle1_rad = Math::atan2(direction.y, direction.x) + Math::deg_to_rad((rng->randf() * 2.0 - 1.0) * spread); |
| 846 | Vector3 rot = Vector3(Math::cos(angle1_rad), Math::sin(angle1_rad), 0.0); |
| 847 | p.velocity = rot * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], rng->randf()); |
| 848 | } else { |
| 849 | //initiate velocity spread in 3D |
| 850 | real_t angle1_rad = Math::deg_to_rad((rng->randf() * (real_t)2.0 - (real_t)1.0) * spread); |
| 851 | real_t angle2_rad = Math::deg_to_rad((rng->randf() * (real_t)2.0 - (real_t)1.0) * ((real_t)1.0 - flatness) * spread); |
| 852 | |
| 853 | Vector3 direction_xz = Vector3(Math::sin(angle1_rad), 0, Math::cos(angle1_rad)); |
| 854 | Vector3 direction_yz = Vector3(0, Math::sin(angle2_rad), Math::cos(angle2_rad)); |
| 855 | Vector3 spread_direction = Vector3(direction_xz.x * direction_yz.z, direction_yz.y, direction_xz.z * direction_yz.z); |
| 856 | Vector3 direction_nrm = direction; |
| 857 | if (direction_nrm.length_squared() > 0) { |
| 858 | direction_nrm.normalize(); |
| 859 | } else { |
| 860 | direction_nrm = Vector3(0, 0, 1); |
| 861 | } |
| 862 | // rotate spread to direction |
| 863 | Vector3 binormal = Vector3(0.0, 1.0, 0.0).cross(direction_nrm); |
| 864 | if (binormal.length_squared() < 0.00000001) { |
| 865 | // direction is parallel to Y. Choose Z as the binormal. |
| 866 | binormal = Vector3(0.0, 0.0, 1.0); |
| 867 | } |
| 868 | binormal.normalize(); |
| 869 | Vector3 normal = binormal.cross(direction_nrm); |
| 870 | spread_direction = binormal * spread_direction.x + normal * spread_direction.y + direction_nrm * spread_direction.z; |
| 871 | p.velocity = spread_direction * Math::lerp(parameters_min[PARAM_INITIAL_LINEAR_VELOCITY], parameters_max[PARAM_INITIAL_LINEAR_VELOCITY], rng->randf()); |
| 872 | } |
| 873 | |
| 874 | real_t base_angle = tex_angle * Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand); |
| 875 | p.custom[0] = Math::deg_to_rad(base_angle); //angle |
| 876 | p.custom[1] = 0.0; //phase |
| 877 | p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand); //animation offset (0-1) |
| 878 | p.custom[3] = (1.0 - rng->randf() * lifetime_randomness); |
| 879 | p.transform = Transform3D(); |
| 880 | p.time = 0; |
| 881 | p.lifetime = lifetime * p.custom[3]; |
| 882 | p.base_color = Color(1, 1, 1, 1); |
| 883 | |
| 884 | switch (emission_shape) { |
| 885 | case EMISSION_SHAPE_POINT: { |
| 886 | //do none |
| 887 | } break; |
| 888 | case EMISSION_SHAPE_SPHERE: { |
| 889 | real_t s = 2.0 * rng->randf() - 1.0; |
| 890 | real_t t = Math::TAU * rng->randf(); |
| 891 | real_t x = rng->randf(); |
| 892 | real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s); |
| 893 | p.transform.origin = Vector3(0, 0, 0).lerp(Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s), x); |
| 894 | } break; |
| 895 | case EMISSION_SHAPE_SPHERE_SURFACE: { |
| 896 | real_t s = 2.0 * rng->randf() - 1.0; |
| 897 | real_t t = Math::TAU * rng->randf(); |
| 898 | real_t radius = emission_sphere_radius * Math::sqrt(1.0 - s * s); |
| 899 | p.transform.origin = Vector3(radius * Math::cos(t), radius * Math::sin(t), emission_sphere_radius * s); |
| 900 | } break; |
| 901 | case EMISSION_SHAPE_BOX: { |
| 902 | p.transform.origin = Vector3(rng->randf() * 2.0 - 1.0, rng->randf() * 2.0 - 1.0, rng->randf() * 2.0 - 1.0) * emission_box_extents; |
| 903 | } break; |
| 904 | case EMISSION_SHAPE_POINTS: |
| 905 | case EMISSION_SHAPE_DIRECTED_POINTS: { |
| 906 | int pc = emission_points.size(); |
| 907 | if (pc == 0) { |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | int random_idx = Math::rand() % pc; |
| 912 | |
| 913 | p.transform.origin = emission_points.get(random_idx); |
| 914 | |
| 915 | if (emission_shape == EMISSION_SHAPE_DIRECTED_POINTS && emission_normals.size() == pc) { |
| 916 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 917 | Vector3 normal = emission_normals.get(random_idx); |
| 918 | Vector2 normal_2d(normal.x, normal.y); |
| 919 | Transform2D m2; |
| 920 | m2.columns[0] = normal_2d; |
| 921 | m2.columns[1] = normal_2d.orthogonal(); |
| 922 | Vector2 velocity_2d(p.velocity.x, p.velocity.y); |
| 923 | velocity_2d = m2.basis_xform(velocity_2d); |
| 924 | p.velocity.x = velocity_2d.x; |
| 925 | p.velocity.y = velocity_2d.y; |
| 926 | } else { |
| 927 | Vector3 normal = emission_normals.get(random_idx); |
| 928 | Vector3 v0 = Math::abs(normal.z) < 0.999 ? Vector3(0.0, 0.0, 1.0) : Vector3(0, 1.0, 0.0); |
| 929 | Vector3 tangent = v0.cross(normal).normalized(); |
| 930 | Vector3 bitangent = tangent.cross(normal).normalized(); |
| 931 | Basis m3; |
| 932 | m3.set_column(0, tangent); |
| 933 | m3.set_column(1, bitangent); |
| 934 | m3.set_column(2, normal); |
| 935 | p.velocity = m3.xform(p.velocity); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | if (emission_colors.size() == pc) { |
| 940 | p.base_color = emission_colors.get(random_idx); |
| 941 | } |
| 942 | } break; |
| 943 | case EMISSION_SHAPE_RING: { |
| 944 | real_t radius_clamped = MAX(0.001, emission_ring_radius); |
| 945 | real_t top_radius = MAX(radius_clamped - Math::tan(Math::deg_to_rad(90.0 - emission_ring_cone_angle)) * emission_ring_height, 0.0); |
| 946 | real_t y_pos = rng->randf(); |
| 947 | real_t skew = MAX(MIN(radius_clamped, top_radius) / MAX(radius_clamped, top_radius), 0.5); |
| 948 | y_pos = radius_clamped < top_radius ? Math::pow(y_pos, skew) : 1.0 - Math::pow(y_pos, skew); |
| 949 | real_t ring_random_angle = rng->randf() * Math::TAU; |
| 950 | real_t ring_random_radius = Math::sqrt(rng->randf() * (radius_clamped * radius_clamped - emission_ring_inner_radius * emission_ring_inner_radius) + emission_ring_inner_radius * emission_ring_inner_radius); |
| 951 | ring_random_radius = Math::lerp(ring_random_radius, ring_random_radius * (top_radius / radius_clamped), y_pos); |
| 952 | Vector3 axis = emission_ring_axis == Vector3(0.0, 0.0, 0.0) ? Vector3(0.0, 0.0, 1.0) : emission_ring_axis.normalized(); |
| 953 | Vector3 ortho_axis; |
| 954 | if (axis.abs() == Vector3(1.0, 0.0, 0.0)) { |
| 955 | ortho_axis = Vector3(0.0, 1.0, 0.0).cross(axis); |
| 956 | } else { |
| 957 | ortho_axis = Vector3(1.0, 0.0, 0.0).cross(axis); |
| 958 | } |
| 959 | ortho_axis = ortho_axis.normalized(); |
| 960 | ortho_axis.rotate(axis, ring_random_angle); |
| 961 | ortho_axis = ortho_axis.normalized(); |
| 962 | p.transform.origin = ortho_axis * ring_random_radius + (y_pos * emission_ring_height - emission_ring_height / 2.0) * axis; |
| 963 | } break; |
| 964 | case EMISSION_SHAPE_MAX: { // Max value for validity check. |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if (!local_coords) { |
| 970 | p.velocity = velocity_xform.xform(p.velocity); |
| 971 | p.transform = emission_xform * p.transform; |
| 972 | } |
| 973 | |
| 974 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 975 | p.velocity.z = 0.0; |
| 976 | p.transform.origin.z = 0.0; |
| 977 | } |
| 978 | |
| 979 | } else if (!p.active) { |
| 980 | continue; |
| 981 | } else if (p.time > p.lifetime) { |
| 982 | p.active = false; |
| 983 | tv = 1.0; |
| 984 | } else { |
| 985 | uint32_t alt_seed = p.seed; |
| 986 | |
| 987 | p.time += local_delta; |
| 988 | p.custom[1] = p.time / lifetime; |
| 989 | tv = p.time / p.lifetime; |
| 990 | |
| 991 | real_t tex_linear_velocity = 1.0; |
| 992 | if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) { |
| 993 | tex_linear_velocity = curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY]->sample(tv); |
| 994 | } |
| 995 | |
| 996 | real_t tex_orbit_velocity = 1.0; |
| 997 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 998 | if (curve_parameters[PARAM_ORBIT_VELOCITY].is_valid()) { |
| 999 | tex_orbit_velocity = curve_parameters[PARAM_ORBIT_VELOCITY]->sample(tv); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | real_t tex_angular_velocity = 1.0; |
| 1004 | if (curve_parameters[PARAM_ANGULAR_VELOCITY].is_valid()) { |
| 1005 | tex_angular_velocity = curve_parameters[PARAM_ANGULAR_VELOCITY]->sample(tv); |
| 1006 | } |
| 1007 | |
| 1008 | real_t tex_linear_accel = 1.0; |
| 1009 | if (curve_parameters[PARAM_LINEAR_ACCEL].is_valid()) { |
| 1010 | tex_linear_accel = curve_parameters[PARAM_LINEAR_ACCEL]->sample(tv); |
| 1011 | } |
| 1012 | |
| 1013 | real_t tex_tangential_accel = 1.0; |
| 1014 | if (curve_parameters[PARAM_TANGENTIAL_ACCEL].is_valid()) { |
| 1015 | tex_tangential_accel = curve_parameters[PARAM_TANGENTIAL_ACCEL]->sample(tv); |
| 1016 | } |
| 1017 | |
| 1018 | real_t tex_radial_accel = 1.0; |
| 1019 | if (curve_parameters[PARAM_RADIAL_ACCEL].is_valid()) { |
| 1020 | tex_radial_accel = curve_parameters[PARAM_RADIAL_ACCEL]->sample(tv); |
| 1021 | } |
| 1022 | |
| 1023 | real_t tex_damping = 1.0; |
| 1024 | if (curve_parameters[PARAM_DAMPING].is_valid()) { |
| 1025 | tex_damping = curve_parameters[PARAM_DAMPING]->sample(tv); |
| 1026 | } |
| 1027 | |
| 1028 | real_t tex_angle = 1.0; |
| 1029 | if (curve_parameters[PARAM_ANGLE].is_valid()) { |
| 1030 | tex_angle = curve_parameters[PARAM_ANGLE]->sample(tv); |
| 1031 | } |
| 1032 | real_t tex_anim_speed = 1.0; |
| 1033 | if (curve_parameters[PARAM_ANIM_SPEED].is_valid()) { |
| 1034 | tex_anim_speed = curve_parameters[PARAM_ANIM_SPEED]->sample(tv); |
| 1035 | } |
| 1036 | |
| 1037 | real_t tex_anim_offset = 1.0; |
| 1038 | if (curve_parameters[PARAM_ANIM_OFFSET].is_valid()) { |
| 1039 | tex_anim_offset = curve_parameters[PARAM_ANIM_OFFSET]->sample(tv); |
| 1040 | } |
| 1041 | |
| 1042 | Vector3 force = gravity; |
| 1043 | Vector3 position = p.transform.origin; |
| 1044 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 1045 | position.z = 0.0; |
| 1046 | } |
| 1047 | //apply linear acceleration |
| 1048 | force += p.velocity.length() > 0.0 ? p.velocity.normalized() * tex_linear_accel * Math::lerp(parameters_min[PARAM_LINEAR_ACCEL], parameters_max[PARAM_LINEAR_ACCEL], rand_from_seed(alt_seed)) : Vector3(); |
| 1049 | //apply radial acceleration |
| 1050 | Vector3 org = emission_xform.origin; |
| 1051 | Vector3 diff = position - org; |
| 1052 | force += diff.length() > 0.0 ? diff.normalized() * (tex_radial_accel)*Math::lerp(parameters_min[PARAM_RADIAL_ACCEL], parameters_max[PARAM_RADIAL_ACCEL], rand_from_seed(alt_seed)) : Vector3(); |
| 1053 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 1054 | Vector2 yx = Vector2(diff.y, diff.x); |
| 1055 | Vector2 yx2 = (yx * Vector2(-1.0, 1.0)).normalized(); |
| 1056 | force += yx.length() > 0.0 ? Vector3(yx2.x, yx2.y, 0.0) * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3(); |
| 1057 | |
| 1058 | } else { |
| 1059 | Vector3 crossDiff = diff.normalized().cross(gravity.normalized()); |
| 1060 | force += crossDiff.length() > 0.0 ? crossDiff.normalized() * (tex_tangential_accel * Math::lerp(parameters_min[PARAM_TANGENTIAL_ACCEL], parameters_max[PARAM_TANGENTIAL_ACCEL], rand_from_seed(alt_seed))) : Vector3(); |
| 1061 | } |
| 1062 | //apply attractor forces |
| 1063 | p.velocity += force * local_delta; |
| 1064 | //orbit velocity |
| 1065 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 1066 | real_t orbit_amount = tex_orbit_velocity * Math::lerp(parameters_min[PARAM_ORBIT_VELOCITY], parameters_max[PARAM_ORBIT_VELOCITY], rand_from_seed(alt_seed)); |
| 1067 | if (orbit_amount != 0.0) { |
| 1068 | real_t ang = orbit_amount * local_delta * Math::TAU; |
| 1069 | // Not sure why the ParticleProcessMaterial code uses a clockwise rotation matrix, |
| 1070 | // but we use -ang here to reproduce its behavior. |
| 1071 | Transform2D rot = Transform2D(-ang, Vector2()); |
| 1072 | Vector2 rotv = rot.basis_xform(Vector2(diff.x, diff.y)); |
| 1073 | p.transform.origin -= Vector3(diff.x, diff.y, 0); |
| 1074 | p.transform.origin += Vector3(rotv.x, rotv.y, 0); |
| 1075 | } |
| 1076 | } |
| 1077 | if (curve_parameters[PARAM_INITIAL_LINEAR_VELOCITY].is_valid()) { |
| 1078 | p.velocity = p.velocity.normalized() * tex_linear_velocity; |
| 1079 | } |
| 1080 | |
| 1081 | if (parameters_max[PARAM_DAMPING] + tex_damping > 0.0) { |
| 1082 | real_t v = p.velocity.length(); |
| 1083 | real_t damp = tex_damping * Math::lerp(parameters_min[PARAM_DAMPING], parameters_max[PARAM_DAMPING], rand_from_seed(alt_seed)); |
| 1084 | v -= damp * local_delta; |
| 1085 | if (v < 0.0) { |
| 1086 | p.velocity = Vector3(); |
| 1087 | } else { |
| 1088 | p.velocity = p.velocity.normalized() * v; |
| 1089 | } |
| 1090 | } |
| 1091 | real_t base_angle = (tex_angle)*Math::lerp(parameters_min[PARAM_ANGLE], parameters_max[PARAM_ANGLE], p.angle_rand); |
| 1092 | base_angle += p.custom[1] * lifetime * tex_angular_velocity * Math::lerp(parameters_min[PARAM_ANGULAR_VELOCITY], parameters_max[PARAM_ANGULAR_VELOCITY], rand_from_seed(alt_seed)); |
| 1093 | p.custom[0] = Math::deg_to_rad(base_angle); //angle |
| 1094 | p.custom[2] = tex_anim_offset * Math::lerp(parameters_min[PARAM_ANIM_OFFSET], parameters_max[PARAM_ANIM_OFFSET], p.anim_offset_rand) + tv * tex_anim_speed * Math::lerp(parameters_min[PARAM_ANIM_SPEED], parameters_max[PARAM_ANIM_SPEED], rand_from_seed(alt_seed)); //angle |
| 1095 | } |
| 1096 | //apply color |
| 1097 | //apply hue rotation |
| 1098 | |
| 1099 | Vector3 tex_scale = Vector3(1.0, 1.0, 1.0); |
| 1100 | if (split_scale) { |
| 1101 | if (scale_curve_x.is_valid()) { |
| 1102 | tex_scale.x = scale_curve_x->sample(tv); |
| 1103 | } else { |
| 1104 | tex_scale.x = 1.0; |
| 1105 | } |
| 1106 | if (scale_curve_y.is_valid()) { |
| 1107 | tex_scale.y = scale_curve_y->sample(tv); |
| 1108 | } else { |
| 1109 | tex_scale.y = 1.0; |
| 1110 | } |
| 1111 | if (scale_curve_z.is_valid()) { |
| 1112 | tex_scale.z = scale_curve_z->sample(tv); |
| 1113 | } else { |
| 1114 | tex_scale.z = 1.0; |
| 1115 | } |
| 1116 | } else { |
| 1117 | if (curve_parameters[PARAM_SCALE].is_valid()) { |
| 1118 | float tmp_scale = curve_parameters[PARAM_SCALE]->sample(tv); |
| 1119 | tex_scale.x = tmp_scale; |
| 1120 | tex_scale.y = tmp_scale; |
| 1121 | tex_scale.z = tmp_scale; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | real_t tex_hue_variation = 0.0; |
| 1126 | if (curve_parameters[PARAM_HUE_VARIATION].is_valid()) { |
| 1127 | tex_hue_variation = curve_parameters[PARAM_HUE_VARIATION]->sample(tv); |
| 1128 | } |
| 1129 | |
| 1130 | real_t hue_rot_angle = (tex_hue_variation)*Math::TAU * Math::lerp(parameters_min[PARAM_HUE_VARIATION], parameters_max[PARAM_HUE_VARIATION], p.hue_rot_rand); |
| 1131 | real_t hue_rot_c = Math::cos(hue_rot_angle); |
| 1132 | real_t hue_rot_s = Math::sin(hue_rot_angle); |
| 1133 | |
| 1134 | Basis hue_rot_mat; |
| 1135 | { |
| 1136 | Basis mat1(0.299, 0.587, 0.114, 0.299, 0.587, 0.114, 0.299, 0.587, 0.114); |
| 1137 | Basis mat2(0.701, -0.587, -0.114, -0.299, 0.413, -0.114, -0.300, -0.588, 0.886); |
| 1138 | Basis mat3(0.168, 0.330, -0.497, -0.328, 0.035, 0.292, 1.250, -1.050, -0.203); |
| 1139 | |
| 1140 | for (int j = 0; j < 3; j++) { |
| 1141 | hue_rot_mat[j] = mat1[j] + mat2[j] * hue_rot_c + mat3[j] * hue_rot_s; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | if (color_ramp.is_valid()) { |
| 1146 | p.color = color_ramp->get_color_at_offset(tv) * color; |
| 1147 | } else { |
| 1148 | p.color = color; |
| 1149 | } |
| 1150 | |
| 1151 | Vector3 color_rgb = hue_rot_mat.xform_inv(Vector3(p.color.r, p.color.g, p.color.b)); |
| 1152 | p.color.r = color_rgb.x; |
| 1153 | p.color.g = color_rgb.y; |
| 1154 | p.color.b = color_rgb.z; |
| 1155 | |
| 1156 | p.color *= p.base_color * p.start_color_rand; |
| 1157 | |
| 1158 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 1159 | if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) { |
| 1160 | if (p.velocity.length() > 0.0) { |
| 1161 | p.transform.basis.set_column(1, p.velocity.normalized()); |
| 1162 | } else { |
| 1163 | p.transform.basis.set_column(1, p.transform.basis.get_column(1)); |
| 1164 | } |
| 1165 | p.transform.basis.set_column(0, p.transform.basis.get_column(1).cross(p.transform.basis.get_column(2)).normalized()); |
| 1166 | p.transform.basis.set_column(2, Vector3(0, 0, 1)); |
| 1167 | |
| 1168 | } else { |
| 1169 | p.transform.basis.set_column(0, Vector3(Math::cos(p.custom[0]), -Math::sin(p.custom[0]), 0.0)); |
| 1170 | p.transform.basis.set_column(1, Vector3(Math::sin(p.custom[0]), Math::cos(p.custom[0]), 0.0)); |
| 1171 | p.transform.basis.set_column(2, Vector3(0, 0, 1)); |
| 1172 | } |
| 1173 | |
| 1174 | } else { |
| 1175 | //orient particle Y towards velocity |
| 1176 | if (particle_flags[PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY]) { |
| 1177 | if (p.velocity.length() > 0.0) { |
| 1178 | p.transform.basis.set_column(1, p.velocity.normalized()); |
| 1179 | } else { |
| 1180 | p.transform.basis.set_column(1, p.transform.basis.get_column(1).normalized()); |
| 1181 | } |
| 1182 | if (p.transform.basis.get_column(1) == p.transform.basis.get_column(0)) { |
| 1183 | p.transform.basis.set_column(0, p.transform.basis.get_column(1).cross(p.transform.basis.get_column(2)).normalized()); |
| 1184 | p.transform.basis.set_column(2, p.transform.basis.get_column(0).cross(p.transform.basis.get_column(1)).normalized()); |
| 1185 | } else { |
| 1186 | p.transform.basis.set_column(2, p.transform.basis.get_column(0).cross(p.transform.basis.get_column(1)).normalized()); |
| 1187 | p.transform.basis.set_column(0, p.transform.basis.get_column(1).cross(p.transform.basis.get_column(2)).normalized()); |
| 1188 | } |
| 1189 | } else { |
| 1190 | p.transform.basis.orthonormalize(); |
| 1191 | } |
| 1192 | |
| 1193 | //turn particle by rotation in Y |
| 1194 | if (particle_flags[PARTICLE_FLAG_ROTATE_Y]) { |
| 1195 | Basis rot_y(Vector3(0, 1, 0), p.custom[0]); |
| 1196 | p.transform.basis = rot_y; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | p.transform.basis = p.transform.basis.orthonormalized(); |
| 1201 | //scale by scale |
| 1202 | |
| 1203 | Vector3 base_scale = tex_scale * Math::lerp(parameters_min[PARAM_SCALE], parameters_max[PARAM_SCALE], p.scale_rand); |
| 1204 | if (base_scale.x < CMP_EPSILON) { |
| 1205 | base_scale.x = CMP_EPSILON; |
| 1206 | } |
| 1207 | if (base_scale.y < CMP_EPSILON) { |
| 1208 | base_scale.y = CMP_EPSILON; |
| 1209 | } |
| 1210 | if (base_scale.z < CMP_EPSILON) { |
| 1211 | base_scale.z = CMP_EPSILON; |
| 1212 | } |
| 1213 | |
| 1214 | p.transform.basis.scale(base_scale); |
| 1215 | |
| 1216 | if (particle_flags[PARTICLE_FLAG_DISABLE_Z]) { |
| 1217 | p.velocity.z = 0.0; |
| 1218 | p.transform.origin.z = 0.0; |
| 1219 | } |
| 1220 | |
| 1221 | p.transform.origin += p.velocity * local_delta; |
| 1222 | |
| 1223 | should_be_active = true; |
| 1224 | } |
| 1225 | if (!Math::is_equal_approx(time, 0.0) && active && !should_be_active) { |
| 1226 | active = false; |
| 1227 | emit_signal(SceneStringName(finished)); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | void CPUParticles3D::_update_particle_data_buffer() { |
| 1232 | MutexLock lock(update_mutex); |
| 1233 | |
| 1234 | int pc = particles.size(); |
| 1235 | |
| 1236 | int *ow; |
| 1237 | int *order = nullptr; |
| 1238 | |
| 1239 | float *w = particle_data.ptrw(); |
| 1240 | const Particle *r = particles.ptr(); |
| 1241 | float *ptr = w; |
| 1242 | |
| 1243 | if (draw_order != DRAW_ORDER_INDEX) { |
| 1244 | ow = particle_order.ptrw(); |
| 1245 | order = ow; |
| 1246 | |
| 1247 | for (int i = 0; i < pc; i++) { |
| 1248 | order[i] = i; |
| 1249 | } |
| 1250 | if (draw_order == DRAW_ORDER_LIFETIME) { |
| 1251 | SortArray<int, SortLifetime> sorter; |
| 1252 | sorter.compare.particles = r; |
| 1253 | sorter.sort(order, pc); |
| 1254 | } else if (draw_order == DRAW_ORDER_VIEW_DEPTH) { |
| 1255 | ERR_FAIL_NULL(get_viewport()); |
| 1256 | Camera3D *c = get_viewport()->get_camera_3d(); |
| 1257 | if (c) { |
| 1258 | Vector3 dir = c->get_global_transform().basis.get_column(2); //far away to close |
| 1259 | |
| 1260 | if (local_coords) { |
| 1261 | // will look different from Particles in editor as this is based on the camera in the scenetree |
| 1262 | // and not the editor camera |
| 1263 | dir = inv_emission_transform.xform(dir).normalized(); |
| 1264 | } else { |
| 1265 | dir = dir.normalized(); |
| 1266 | } |
| 1267 | |
| 1268 | SortArray<int, SortAxis> sorter; |
| 1269 | sorter.compare.particles = r; |
| 1270 | sorter.compare.axis = dir; |
| 1271 | sorter.sort(order, pc); |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | for (int i = 0; i < pc; i++) { |
| 1277 | int idx = order ? order[i] : i; |
| 1278 | |
| 1279 | Transform3D t = r[idx].transform; |
| 1280 | |
| 1281 | if (!local_coords) { |
| 1282 | t = inv_emission_transform * t; |
| 1283 | } |
| 1284 | |
| 1285 | if (r[idx].active) { |
| 1286 | ptr[0] = t.basis.rows[0][0]; |
| 1287 | ptr[1] = t.basis.rows[0][1]; |
| 1288 | ptr[2] = t.basis.rows[0][2]; |
| 1289 | ptr[3] = t.origin.x; |
| 1290 | ptr[4] = t.basis.rows[1][0]; |
| 1291 | ptr[5] = t.basis.rows[1][1]; |
| 1292 | ptr[6] = t.basis.rows[1][2]; |
| 1293 | ptr[7] = t.origin.y; |
| 1294 | ptr[8] = t.basis.rows[2][0]; |
| 1295 | ptr[9] = t.basis.rows[2][1]; |
| 1296 | ptr[10] = t.basis.rows[2][2]; |
| 1297 | ptr[11] = t.origin.z; |
| 1298 | } else { |
| 1299 | memset(ptr, 0, sizeof(float) * 12); |
| 1300 | } |
| 1301 | |
| 1302 | Color c = r[idx].color; |
| 1303 | |
| 1304 | ptr[12] = c.r; |
| 1305 | ptr[13] = c.g; |
| 1306 | ptr[14] = c.b; |
| 1307 | ptr[15] = c.a; |
| 1308 | |
| 1309 | ptr[16] = r[idx].custom[0]; |
| 1310 | ptr[17] = r[idx].custom[1]; |
| 1311 | ptr[18] = r[idx].custom[2]; |
| 1312 | ptr[19] = r[idx].custom[3]; |
| 1313 | |
| 1314 | ptr += 20; |
| 1315 | } |
| 1316 | |
| 1317 | can_update.set(); |
| 1318 | } |
| 1319 | |
| 1320 | void CPUParticles3D::_set_redraw(bool p_redraw) { |
| 1321 | if (redraw == p_redraw) { |
| 1322 | return; |
| 1323 | } |
| 1324 | redraw = p_redraw; |
| 1325 | |
| 1326 | { |
| 1327 | MutexLock lock(update_mutex); |
| 1328 | |
| 1329 | if (redraw) { |
| 1330 | RS::get_singleton()->connect("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread)); |
| 1331 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, true); |
| 1332 | RS::get_singleton()->multimesh_set_visible_instances(multimesh, -1); |
| 1333 | } else { |
| 1334 | if (RS::get_singleton()->is_connected("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread))) { |
| 1335 | RS::get_singleton()->disconnect("frame_pre_draw", callable_mp(this, &CPUParticles3D::_update_render_thread)); |
| 1336 | } |
| 1337 | RS::get_singleton()->instance_geometry_set_flag(get_instance(), RS::INSTANCE_FLAG_DRAW_NEXT_FRAME_IF_VISIBLE, false); |
| 1338 | RS::get_singleton()->multimesh_set_visible_instances(multimesh, 0); |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | void CPUParticles3D::_update_render_thread() { |
| 1344 | MutexLock lock(update_mutex); |
| 1345 | |
| 1346 | if (can_update.is_set()) { |
| 1347 | RS::get_singleton()->multimesh_set_buffer(multimesh, particle_data); |
| 1348 | can_update.clear(); //wait for next time |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | void CPUParticles3D::_notification(int p_what) { |
| 1353 | switch (p_what) { |
| 1354 | case NOTIFICATION_ENTER_TREE: { |
| 1355 | set_process_internal(emitting); |
| 1356 | |
| 1357 | // first update before rendering to avoid one frame delay after emitting starts |
| 1358 | if (emitting && (time == 0)) { |
| 1359 | _update_internal(); |
| 1360 | } |
| 1361 | } break; |
| 1362 | |
| 1363 | case NOTIFICATION_EXIT_TREE: { |
| 1364 | _set_redraw(false); |
| 1365 | } break; |
| 1366 | |
| 1367 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 1368 | // first update before rendering to avoid one frame delay after emitting starts |
| 1369 | if (emitting && (time == 0)) { |
| 1370 | _update_internal(); |
| 1371 | } |
| 1372 | } break; |
| 1373 | |
| 1374 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 1375 | _update_internal(); |
| 1376 | } break; |
| 1377 | |
| 1378 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 1379 | Transform3D global_transform = get_global_transform(); |
| 1380 | if (unlikely(global_transform.basis.determinant() == 0)) { |
| 1381 | return; |
| 1382 | } |
| 1383 | inv_emission_transform = global_transform.affine_inverse(); |
| 1384 | |
| 1385 | if (!local_coords) { |
| 1386 | int pc = particles.size(); |
| 1387 | |
| 1388 | float *w = particle_data.ptrw(); |
| 1389 | const Particle *r = particles.ptr(); |
| 1390 | float *ptr = w; |
| 1391 | |
| 1392 | for (int i = 0; i < pc; i++) { |
| 1393 | Transform3D t = inv_emission_transform * r[i].transform; |
| 1394 | |
| 1395 | if (r[i].active) { |
| 1396 | ptr[0] = t.basis.rows[0][0]; |
| 1397 | ptr[1] = t.basis.rows[0][1]; |
| 1398 | ptr[2] = t.basis.rows[0][2]; |
| 1399 | ptr[3] = t.origin.x; |
| 1400 | ptr[4] = t.basis.rows[1][0]; |
| 1401 | ptr[5] = t.basis.rows[1][1]; |
| 1402 | ptr[6] = t.basis.rows[1][2]; |
| 1403 | ptr[7] = t.origin.y; |
| 1404 | ptr[8] = t.basis.rows[2][0]; |
| 1405 | ptr[9] = t.basis.rows[2][1]; |
| 1406 | ptr[10] = t.basis.rows[2][2]; |
| 1407 | ptr[11] = t.origin.z; |
| 1408 | } else { |
| 1409 | memset(ptr, 0, sizeof(float) * 12); |
| 1410 | } |
| 1411 | |
| 1412 | ptr += 20; |
| 1413 | } |
| 1414 | |
| 1415 | can_update.set(); |
| 1416 | } |
| 1417 | } break; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | void CPUParticles3D::convert_from_particles(Node *p_particles) { |
| 1422 | GPUParticles3D *gpu_particles = Object::cast_to<GPUParticles3D>(p_particles); |
| 1423 | ERR_FAIL_NULL_MSG(gpu_particles, "Only GPUParticles3D nodes can be converted to CPUParticles3D."); |
| 1424 | |
| 1425 | set_emitting(gpu_particles->is_emitting()); |
| 1426 | set_amount(gpu_particles->get_amount()); |
| 1427 | set_lifetime(gpu_particles->get_lifetime()); |
| 1428 | set_one_shot(gpu_particles->get_one_shot()); |
| 1429 | set_pre_process_time(gpu_particles->get_pre_process_time()); |
| 1430 | set_explosiveness_ratio(gpu_particles->get_explosiveness_ratio()); |
| 1431 | set_randomness_ratio(gpu_particles->get_randomness_ratio()); |
| 1432 | set_visibility_aabb(gpu_particles->get_visibility_aabb()); |
| 1433 | set_use_local_coordinates(gpu_particles->get_use_local_coordinates()); |
| 1434 | set_fixed_fps(gpu_particles->get_fixed_fps()); |
| 1435 | set_fractional_delta(gpu_particles->get_fractional_delta()); |
| 1436 | set_speed_scale(gpu_particles->get_speed_scale()); |
| 1437 | set_draw_order(DrawOrder(gpu_particles->get_draw_order())); |
| 1438 | set_mesh(gpu_particles->get_draw_pass_mesh(0)); |
| 1439 | |
| 1440 | Ref<ParticleProcessMaterial> material = gpu_particles->get_process_material(); |
| 1441 | if (material.is_null()) { |
| 1442 | return; |
| 1443 | } |
| 1444 | |
| 1445 | set_direction(material->get_direction()); |
| 1446 | set_spread(material->get_spread()); |
| 1447 | set_flatness(material->get_flatness()); |
| 1448 | |
| 1449 | set_color(material->get_color()); |
| 1450 | |
| 1451 | Ref<GradientTexture1D> gt = material->get_color_ramp(); |
| 1452 | if (gt.is_valid()) { |
| 1453 | set_color_ramp(gt->get_gradient()); |
| 1454 | } |
| 1455 | |
| 1456 | Ref<GradientTexture1D> gti = material->get_color_initial_ramp(); |
| 1457 | if (gti.is_valid()) { |
| 1458 | set_color_initial_ramp(gti->get_gradient()); |
| 1459 | } |
| 1460 | |
| 1461 | set_particle_flag(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY, material->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY)); |
| 1462 | set_particle_flag(PARTICLE_FLAG_ROTATE_Y, material->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_ROTATE_Y)); |
| 1463 | set_particle_flag(PARTICLE_FLAG_DISABLE_Z, material->get_particle_flag(ParticleProcessMaterial::PARTICLE_FLAG_DISABLE_Z)); |
| 1464 | |
| 1465 | set_emission_shape(EmissionShape(material->get_emission_shape())); |
| 1466 | set_emission_sphere_radius(material->get_emission_sphere_radius()); |
| 1467 | set_emission_box_extents(material->get_emission_box_extents()); |
| 1468 | set_emission_ring_height(material->get_emission_ring_height()); |
| 1469 | set_emission_ring_radius(material->get_emission_ring_radius()); |
| 1470 | set_emission_ring_inner_radius(material->get_emission_ring_inner_radius()); |
| 1471 | set_emission_ring_cone_angle(material->get_emission_ring_cone_angle()); |
| 1472 | |
| 1473 | Ref<CurveXYZTexture> scale3D = material->get_param_texture(ParticleProcessMaterial::PARAM_SCALE); |
| 1474 | if (scale3D.is_valid()) { |
| 1475 | split_scale = true; |
| 1476 | scale_curve_x = scale3D->get_curve_x(); |
| 1477 | scale_curve_y = scale3D->get_curve_y(); |
| 1478 | scale_curve_z = scale3D->get_curve_z(); |
| 1479 | } |
| 1480 | |
| 1481 | set_gravity(material->get_gravity()); |
| 1482 | set_lifetime_randomness(material->get_lifetime_randomness()); |
| 1483 | |
| 1484 | #define CONVERT_PARAM(m_param) \ |
| 1485 | set_param_min(m_param, material->get_param_min(ParticleProcessMaterial::m_param)); \ |
| 1486 | { \ |
| 1487 | Ref<CurveTexture> ctex = material->get_param_texture(ParticleProcessMaterial::m_param); \ |
| 1488 | if (ctex.is_valid()) \ |
| 1489 | set_param_curve(m_param, ctex->get_curve()); \ |
| 1490 | } \ |
| 1491 | set_param_max(m_param, material->get_param_max(ParticleProcessMaterial::m_param)); |
| 1492 | |
| 1493 | CONVERT_PARAM(PARAM_INITIAL_LINEAR_VELOCITY); |
| 1494 | CONVERT_PARAM(PARAM_ANGULAR_VELOCITY); |
| 1495 | CONVERT_PARAM(PARAM_ORBIT_VELOCITY); |
| 1496 | CONVERT_PARAM(PARAM_LINEAR_ACCEL); |
| 1497 | CONVERT_PARAM(PARAM_RADIAL_ACCEL); |
| 1498 | CONVERT_PARAM(PARAM_TANGENTIAL_ACCEL); |
| 1499 | CONVERT_PARAM(PARAM_DAMPING); |
| 1500 | CONVERT_PARAM(PARAM_ANGLE); |
| 1501 | CONVERT_PARAM(PARAM_SCALE); |
| 1502 | CONVERT_PARAM(PARAM_HUE_VARIATION); |
| 1503 | CONVERT_PARAM(PARAM_ANIM_SPEED); |
| 1504 | CONVERT_PARAM(PARAM_ANIM_OFFSET); |
| 1505 | |
| 1506 | #undef CONVERT_PARAM |
| 1507 | } |
| 1508 | |
| 1509 | void CPUParticles3D::_bind_methods() { |
| 1510 | ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &CPUParticles3D::set_emitting); |
| 1511 | ClassDB::bind_method(D_METHOD("set_amount", "amount"), &CPUParticles3D::set_amount); |
| 1512 | ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &CPUParticles3D::set_lifetime); |
| 1513 | ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &CPUParticles3D::set_one_shot); |
| 1514 | ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &CPUParticles3D::set_pre_process_time); |
| 1515 | ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &CPUParticles3D::set_explosiveness_ratio); |
| 1516 | ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &CPUParticles3D::set_randomness_ratio); |
| 1517 | ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &CPUParticles3D::set_visibility_aabb); |
| 1518 | ClassDB::bind_method(D_METHOD("set_lifetime_randomness", "random"), &CPUParticles3D::set_lifetime_randomness); |
| 1519 | ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &CPUParticles3D::set_use_local_coordinates); |
| 1520 | ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &CPUParticles3D::set_fixed_fps); |
| 1521 | ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &CPUParticles3D::set_fractional_delta); |
| 1522 | ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &CPUParticles3D::set_speed_scale); |
| 1523 | |
| 1524 | ClassDB::bind_method(D_METHOD("is_emitting"), &CPUParticles3D::is_emitting); |
| 1525 | ClassDB::bind_method(D_METHOD("get_amount"), &CPUParticles3D::get_amount); |
| 1526 | ClassDB::bind_method(D_METHOD("get_lifetime"), &CPUParticles3D::get_lifetime); |
| 1527 | ClassDB::bind_method(D_METHOD("get_one_shot"), &CPUParticles3D::get_one_shot); |
| 1528 | ClassDB::bind_method(D_METHOD("get_pre_process_time"), &CPUParticles3D::get_pre_process_time); |
| 1529 | ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &CPUParticles3D::get_explosiveness_ratio); |
| 1530 | ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &CPUParticles3D::get_randomness_ratio); |
| 1531 | ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &CPUParticles3D::get_visibility_aabb); |
| 1532 | ClassDB::bind_method(D_METHOD("get_lifetime_randomness"), &CPUParticles3D::get_lifetime_randomness); |
| 1533 | ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &CPUParticles3D::get_use_local_coordinates); |
| 1534 | ClassDB::bind_method(D_METHOD("get_fixed_fps"), &CPUParticles3D::get_fixed_fps); |
| 1535 | ClassDB::bind_method(D_METHOD("get_fractional_delta"), &CPUParticles3D::get_fractional_delta); |
| 1536 | ClassDB::bind_method(D_METHOD("get_speed_scale"), &CPUParticles3D::get_speed_scale); |
| 1537 | |
| 1538 | ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &CPUParticles3D::set_draw_order); |
| 1539 | |
| 1540 | ClassDB::bind_method(D_METHOD("get_draw_order"), &CPUParticles3D::get_draw_order); |
| 1541 | |
| 1542 | ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &CPUParticles3D::set_mesh); |
| 1543 | ClassDB::bind_method(D_METHOD("get_mesh"), &CPUParticles3D::get_mesh); |
| 1544 | |
| 1545 | ClassDB::bind_method(D_METHOD("set_use_fixed_seed", "use_fixed_seed"), &CPUParticles3D::set_use_fixed_seed); |
| 1546 | ClassDB::bind_method(D_METHOD("get_use_fixed_seed"), &CPUParticles3D::get_use_fixed_seed); |
| 1547 | |
| 1548 | ClassDB::bind_method(D_METHOD("set_seed", "seed"), &CPUParticles3D::set_seed); |
| 1549 | ClassDB::bind_method(D_METHOD("get_seed"), &CPUParticles3D::get_seed); |
| 1550 | |
| 1551 | ClassDB::bind_method(D_METHOD("restart", "keep_seed"), &CPUParticles3D::restart, DEFVAL(false)); |
| 1552 | ClassDB::bind_method(D_METHOD("request_particles_process", "process_time"), &CPUParticles3D::request_particles_process); |
| 1553 | ClassDB::bind_method(D_METHOD("capture_aabb"), &CPUParticles3D::capture_aabb); |
| 1554 | |
| 1555 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting", PROPERTY_HINT_ONESHOT), "set_emitting", "is_emitting"); |
| 1556 | ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_RANGE, "1,1000000,1,exp"), "set_amount", "get_amount"); // FIXME: Evaluate support for `exp` in integer properties, or remove this. |
| 1557 | ADD_GROUP("Time", ""); |
| 1558 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime", PROPERTY_HINT_RANGE, "0.01,600.0,0.01,or_greater,exp,suffix:s"), "set_lifetime", "get_lifetime"); |
| 1559 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot"); |
| 1560 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "preprocess", PROPERTY_HINT_RANGE, "0.00,10.0,0.01,or_greater,exp,suffix:s"), "set_pre_process_time", "get_pre_process_time"); |
| 1561 | |
| 1562 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale"); |
| 1563 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio"); |
| 1564 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio"); |
| 1565 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_fixed_seed"), "set_use_fixed_seed", "get_use_fixed_seed"); |
| 1566 | ADD_PROPERTY(PropertyInfo(Variant::INT, "seed", PROPERTY_HINT_RANGE, "0," + itos(UINT32_MAX) + ",1"), "set_seed", "get_seed"); |
| 1567 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "lifetime_randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_lifetime_randomness", "get_lifetime_randomness"); |
| 1568 | ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1,suffix:FPS"), "set_fixed_fps", "get_fixed_fps"); |
| 1569 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta"); |
| 1570 | ADD_GROUP("Drawing", ""); |
| 1571 | ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb", PROPERTY_HINT_NONE, "suffix:m"), "set_visibility_aabb", "get_visibility_aabb"); |
| 1572 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates"); |
| 1573 | ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order"); |
| 1574 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh"); |
| 1575 | |
| 1576 | BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX); |
| 1577 | BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME); |
| 1578 | BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH); |
| 1579 | |
| 1580 | ADD_PROPERTY_DEFAULT("seed", 0); |
| 1581 | |
| 1582 | //////////////////////////////// |
| 1583 | |
| 1584 | ClassDB::bind_method(D_METHOD("set_direction", "direction"), &CPUParticles3D::set_direction); |
| 1585 | ClassDB::bind_method(D_METHOD("get_direction"), &CPUParticles3D::get_direction); |
| 1586 | |
| 1587 | ClassDB::bind_method(D_METHOD("set_spread", "degrees"), &CPUParticles3D::set_spread); |
| 1588 | ClassDB::bind_method(D_METHOD("get_spread"), &CPUParticles3D::get_spread); |
| 1589 | |
| 1590 | ClassDB::bind_method(D_METHOD("set_flatness", "amount"), &CPUParticles3D::set_flatness); |
| 1591 | ClassDB::bind_method(D_METHOD("get_flatness"), &CPUParticles3D::get_flatness); |
| 1592 | |
| 1593 | ClassDB::bind_method(D_METHOD("set_param_min", "param", "value"), &CPUParticles3D::set_param_min); |
| 1594 | ClassDB::bind_method(D_METHOD("get_param_min", "param"), &CPUParticles3D::get_param_min); |
| 1595 | |
| 1596 | ClassDB::bind_method(D_METHOD("set_param_max", "param", "value"), &CPUParticles3D::set_param_max); |
| 1597 | ClassDB::bind_method(D_METHOD("get_param_max", "param"), &CPUParticles3D::get_param_max); |
| 1598 | |
| 1599 | ClassDB::bind_method(D_METHOD("set_param_curve", "param", "curve"), &CPUParticles3D::set_param_curve); |
| 1600 | ClassDB::bind_method(D_METHOD("get_param_curve", "param"), &CPUParticles3D::get_param_curve); |
| 1601 | |
| 1602 | ClassDB::bind_method(D_METHOD("set_color", "color"), &CPUParticles3D::set_color); |
| 1603 | ClassDB::bind_method(D_METHOD("get_color"), &CPUParticles3D::get_color); |
| 1604 | |
| 1605 | ClassDB::bind_method(D_METHOD("set_color_ramp", "ramp"), &CPUParticles3D::set_color_ramp); |
| 1606 | ClassDB::bind_method(D_METHOD("get_color_ramp"), &CPUParticles3D::get_color_ramp); |
| 1607 | |
| 1608 | ClassDB::bind_method(D_METHOD("set_color_initial_ramp", "ramp"), &CPUParticles3D::set_color_initial_ramp); |
| 1609 | ClassDB::bind_method(D_METHOD("get_color_initial_ramp"), &CPUParticles3D::get_color_initial_ramp); |
| 1610 | |
| 1611 | ClassDB::bind_method(D_METHOD("set_particle_flag", "particle_flag", "enable"), &CPUParticles3D::set_particle_flag); |
| 1612 | ClassDB::bind_method(D_METHOD("get_particle_flag", "particle_flag"), &CPUParticles3D::get_particle_flag); |
| 1613 | |
| 1614 | ClassDB::bind_method(D_METHOD("set_emission_shape", "shape"), &CPUParticles3D::set_emission_shape); |
| 1615 | ClassDB::bind_method(D_METHOD("get_emission_shape"), &CPUParticles3D::get_emission_shape); |
| 1616 | |
| 1617 | ClassDB::bind_method(D_METHOD("set_emission_sphere_radius", "radius"), &CPUParticles3D::set_emission_sphere_radius); |
| 1618 | ClassDB::bind_method(D_METHOD("get_emission_sphere_radius"), &CPUParticles3D::get_emission_sphere_radius); |
| 1619 | |
| 1620 | ClassDB::bind_method(D_METHOD("set_emission_box_extents", "extents"), &CPUParticles3D::set_emission_box_extents); |
| 1621 | ClassDB::bind_method(D_METHOD("get_emission_box_extents"), &CPUParticles3D::get_emission_box_extents); |
| 1622 | |
| 1623 | ClassDB::bind_method(D_METHOD("set_emission_points", "array"), &CPUParticles3D::set_emission_points); |
| 1624 | ClassDB::bind_method(D_METHOD("get_emission_points"), &CPUParticles3D::get_emission_points); |
| 1625 | |
| 1626 | ClassDB::bind_method(D_METHOD("set_emission_normals", "array"), &CPUParticles3D::set_emission_normals); |
| 1627 | ClassDB::bind_method(D_METHOD("get_emission_normals"), &CPUParticles3D::get_emission_normals); |
| 1628 | |
| 1629 | ClassDB::bind_method(D_METHOD("set_emission_colors", "array"), &CPUParticles3D::set_emission_colors); |
| 1630 | ClassDB::bind_method(D_METHOD("get_emission_colors"), &CPUParticles3D::get_emission_colors); |
| 1631 | |
| 1632 | ClassDB::bind_method(D_METHOD("set_emission_ring_axis", "axis"), &CPUParticles3D::set_emission_ring_axis); |
| 1633 | ClassDB::bind_method(D_METHOD("get_emission_ring_axis"), &CPUParticles3D::get_emission_ring_axis); |
| 1634 | |
| 1635 | ClassDB::bind_method(D_METHOD("set_emission_ring_height", "height"), &CPUParticles3D::set_emission_ring_height); |
| 1636 | ClassDB::bind_method(D_METHOD("get_emission_ring_height"), &CPUParticles3D::get_emission_ring_height); |
| 1637 | |
| 1638 | ClassDB::bind_method(D_METHOD("set_emission_ring_radius", "radius"), &CPUParticles3D::set_emission_ring_radius); |
| 1639 | ClassDB::bind_method(D_METHOD("get_emission_ring_radius"), &CPUParticles3D::get_emission_ring_radius); |
| 1640 | |
| 1641 | ClassDB::bind_method(D_METHOD("set_emission_ring_inner_radius", "inner_radius"), &CPUParticles3D::set_emission_ring_inner_radius); |
| 1642 | ClassDB::bind_method(D_METHOD("get_emission_ring_inner_radius"), &CPUParticles3D::get_emission_ring_inner_radius); |
| 1643 | |
| 1644 | ClassDB::bind_method(D_METHOD("set_emission_ring_cone_angle", "cone_angle"), &CPUParticles3D::set_emission_ring_cone_angle); |
| 1645 | ClassDB::bind_method(D_METHOD("get_emission_ring_cone_angle"), &CPUParticles3D::get_emission_ring_cone_angle); |
| 1646 | |
| 1647 | ClassDB::bind_method(D_METHOD("get_gravity"), &CPUParticles3D::get_gravity); |
| 1648 | ClassDB::bind_method(D_METHOD("set_gravity", "accel_vec"), &CPUParticles3D::set_gravity); |
| 1649 | |
| 1650 | ClassDB::bind_method(D_METHOD("get_split_scale"), &CPUParticles3D::get_split_scale); |
| 1651 | ClassDB::bind_method(D_METHOD("set_split_scale", "split_scale"), &CPUParticles3D::set_split_scale); |
| 1652 | |
| 1653 | ClassDB::bind_method(D_METHOD("get_scale_curve_x"), &CPUParticles3D::get_scale_curve_x); |
| 1654 | ClassDB::bind_method(D_METHOD("set_scale_curve_x", "scale_curve"), &CPUParticles3D::set_scale_curve_x); |
| 1655 | |
| 1656 | ClassDB::bind_method(D_METHOD("get_scale_curve_y"), &CPUParticles3D::get_scale_curve_y); |
| 1657 | ClassDB::bind_method(D_METHOD("set_scale_curve_y", "scale_curve"), &CPUParticles3D::set_scale_curve_y); |
| 1658 | |
| 1659 | ClassDB::bind_method(D_METHOD("get_scale_curve_z"), &CPUParticles3D::get_scale_curve_z); |
| 1660 | ClassDB::bind_method(D_METHOD("set_scale_curve_z", "scale_curve"), &CPUParticles3D::set_scale_curve_z); |
| 1661 | |
| 1662 | ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles3D::convert_from_particles); |
| 1663 | |
| 1664 | ADD_SIGNAL(MethodInfo("finished")); |
| 1665 | |
| 1666 | ADD_GROUP("Emission Shape", "emission_"); |
| 1667 | ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Sphere Surface,Box,Points,Directed Points,Ring", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_emission_shape", "get_emission_shape"); |
| 1668 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_sphere_radius", PROPERTY_HINT_RANGE, "0.01,128,0.01"), "set_emission_sphere_radius", "get_emission_sphere_radius"); |
| 1669 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_box_extents"), "set_emission_box_extents", "get_emission_box_extents"); |
| 1670 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "emission_points"), "set_emission_points", "get_emission_points"); |
| 1671 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR3_ARRAY, "emission_normals"), "set_emission_normals", "get_emission_normals"); |
| 1672 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "emission_colors"), "set_emission_colors", "get_emission_colors"); |
| 1673 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_ring_axis"), "set_emission_ring_axis", "get_emission_ring_axis"); |
| 1674 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_height", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_height", "get_emission_ring_height"); |
| 1675 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_radius", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_radius", "get_emission_ring_radius"); |
| 1676 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_inner_radius", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_emission_ring_inner_radius", "get_emission_ring_inner_radius"); |
| 1677 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "emission_ring_cone_angle", PROPERTY_HINT_RANGE, "0,90,0.01,degrees"), "set_emission_ring_cone_angle", "get_emission_ring_cone_angle"); |
| 1678 | ADD_GROUP("Particle Flags", "particle_flag_"); |
| 1679 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_align_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY); |
| 1680 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_rotate_y"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_ROTATE_Y); |
| 1681 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_disable_z"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DISABLE_Z); |
| 1682 | ADD_GROUP("Direction", ""); |
| 1683 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "direction"), "set_direction", "get_direction"); |
| 1684 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "spread", PROPERTY_HINT_RANGE, "0,180,0.01"), "set_spread", "get_spread"); |
| 1685 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "flatness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_flatness", "get_flatness"); |
| 1686 | ADD_GROUP("Gravity", ""); |
| 1687 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity"), "set_gravity", "get_gravity"); |
| 1688 | ADD_GROUP("Initial Velocity", "initial_"); |
| 1689 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_INITIAL_LINEAR_VELOCITY); |
| 1690 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "initial_velocity_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_INITIAL_LINEAR_VELOCITY); |
| 1691 | ADD_GROUP("Angular Velocity", "angular_"); |
| 1692 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_min", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ANGULAR_VELOCITY); |
| 1693 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angular_velocity_max", PROPERTY_HINT_RANGE, "-720,720,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ANGULAR_VELOCITY); |
| 1694 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angular_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGULAR_VELOCITY); |
| 1695 | ADD_GROUP("Orbit Velocity", "orbit_"); |
| 1696 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_min", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_ORBIT_VELOCITY); |
| 1697 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "orbit_velocity_max", PROPERTY_HINT_RANGE, "-1000,1000,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_ORBIT_VELOCITY); |
| 1698 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "orbit_velocity_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ORBIT_VELOCITY); |
| 1699 | ADD_GROUP("Linear Accel", "linear_"); |
| 1700 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_LINEAR_ACCEL); |
| 1701 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "linear_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_LINEAR_ACCEL); |
| 1702 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "linear_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_LINEAR_ACCEL); |
| 1703 | ADD_GROUP("Radial Accel", "radial_"); |
| 1704 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_RADIAL_ACCEL); |
| 1705 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "radial_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_RADIAL_ACCEL); |
| 1706 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "radial_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_RADIAL_ACCEL); |
| 1707 | ADD_GROUP("Tangential Accel", "tangential_"); |
| 1708 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_min", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_min", "get_param_min", PARAM_TANGENTIAL_ACCEL); |
| 1709 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "tangential_accel_max", PROPERTY_HINT_RANGE, "-100,100,0.01,or_less,or_greater"), "set_param_max", "get_param_max", PARAM_TANGENTIAL_ACCEL); |
| 1710 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "tangential_accel_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_TANGENTIAL_ACCEL); |
| 1711 | ADD_GROUP("Damping", ""); |
| 1712 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_min", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_min", "get_param_min", PARAM_DAMPING); |
| 1713 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "damping_max", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_param_max", "get_param_max", PARAM_DAMPING); |
| 1714 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "damping_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_DAMPING); |
| 1715 | ADD_GROUP("Angle", ""); |
| 1716 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_min", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_min", "get_param_min", PARAM_ANGLE); |
| 1717 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "angle_max", PROPERTY_HINT_RANGE, "-720,720,0.1,or_less,or_greater,degrees"), "set_param_max", "get_param_max", PARAM_ANGLE); |
| 1718 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "angle_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANGLE); |
| 1719 | ADD_GROUP("Scale", ""); |
| 1720 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_min", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_min", "get_param_min", PARAM_SCALE); |
| 1721 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "scale_amount_max", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater"), "set_param_max", "get_param_max", PARAM_SCALE); |
| 1722 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "scale_amount_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_SCALE); |
| 1723 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "split_scale"), "set_split_scale", "get_split_scale"); |
| 1724 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_x", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_x", "get_scale_curve_x"); |
| 1725 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_y", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_y", "get_scale_curve_y"); |
| 1726 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "scale_curve_z", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_scale_curve_z", "get_scale_curve_z"); |
| 1727 | ADD_GROUP("Color", ""); |
| 1728 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); |
| 1729 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_ramp", "get_color_ramp"); |
| 1730 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "color_initial_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_color_initial_ramp", "get_color_initial_ramp"); |
| 1731 | |
| 1732 | ADD_GROUP("Hue Variation", "hue_"); |
| 1733 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_min", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_min", "get_param_min", PARAM_HUE_VARIATION); |
| 1734 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "hue_variation_max", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_param_max", "get_param_max", PARAM_HUE_VARIATION); |
| 1735 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "hue_variation_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_HUE_VARIATION); |
| 1736 | ADD_GROUP("Animation", "anim_"); |
| 1737 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_min", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_min", "get_param_min", PARAM_ANIM_SPEED); |
| 1738 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_speed_max", PROPERTY_HINT_RANGE, "0,128,0.01,or_greater,or_less"), "set_param_max", "get_param_max", PARAM_ANIM_SPEED); |
| 1739 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_speed_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_SPEED); |
| 1740 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_min", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_min", "get_param_min", PARAM_ANIM_OFFSET); |
| 1741 | ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "anim_offset_max", PROPERTY_HINT_RANGE, "0,1,0.0001"), "set_param_max", "get_param_max", PARAM_ANIM_OFFSET); |
| 1742 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "anim_offset_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_param_curve", "get_param_curve", PARAM_ANIM_OFFSET); |
| 1743 | |
| 1744 | BIND_ENUM_CONSTANT(PARAM_INITIAL_LINEAR_VELOCITY); |
| 1745 | BIND_ENUM_CONSTANT(PARAM_ANGULAR_VELOCITY); |
| 1746 | BIND_ENUM_CONSTANT(PARAM_ORBIT_VELOCITY); |
| 1747 | BIND_ENUM_CONSTANT(PARAM_LINEAR_ACCEL); |
| 1748 | BIND_ENUM_CONSTANT(PARAM_RADIAL_ACCEL); |
| 1749 | BIND_ENUM_CONSTANT(PARAM_TANGENTIAL_ACCEL); |
| 1750 | BIND_ENUM_CONSTANT(PARAM_DAMPING); |
| 1751 | BIND_ENUM_CONSTANT(PARAM_ANGLE); |
| 1752 | BIND_ENUM_CONSTANT(PARAM_SCALE); |
| 1753 | BIND_ENUM_CONSTANT(PARAM_HUE_VARIATION); |
| 1754 | BIND_ENUM_CONSTANT(PARAM_ANIM_SPEED); |
| 1755 | BIND_ENUM_CONSTANT(PARAM_ANIM_OFFSET); |
| 1756 | BIND_ENUM_CONSTANT(PARAM_MAX); |
| 1757 | |
| 1758 | BIND_ENUM_CONSTANT(PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY); |
| 1759 | BIND_ENUM_CONSTANT(PARTICLE_FLAG_ROTATE_Y); |
| 1760 | BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z); |
| 1761 | BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX); |
| 1762 | |
| 1763 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT); |
| 1764 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE); |
| 1765 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_SPHERE_SURFACE); |
| 1766 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_BOX); |
| 1767 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINTS); |
| 1768 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_DIRECTED_POINTS); |
| 1769 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_RING); |
| 1770 | BIND_ENUM_CONSTANT(EMISSION_SHAPE_MAX); |
| 1771 | } |
| 1772 | |
| 1773 | CPUParticles3D::CPUParticles3D() { |
| 1774 | set_notify_transform(true); |
| 1775 | |
| 1776 | multimesh = RenderingServer::get_singleton()->multimesh_create(); |
| 1777 | RenderingServer::get_singleton()->multimesh_set_visible_instances(multimesh, 0); |
| 1778 | set_base(multimesh); |
| 1779 | |
| 1780 | set_emitting(true); |
| 1781 | set_amount(8); |
| 1782 | set_seed(Math::rand()); |
| 1783 | |
| 1784 | rng.instantiate(); |
| 1785 | |
| 1786 | set_param_min(PARAM_INITIAL_LINEAR_VELOCITY, 0); |
| 1787 | set_param_min(PARAM_ANGULAR_VELOCITY, 0); |
| 1788 | set_param_min(PARAM_ORBIT_VELOCITY, 0); |
| 1789 | set_param_min(PARAM_LINEAR_ACCEL, 0); |
| 1790 | set_param_min(PARAM_RADIAL_ACCEL, 0); |
| 1791 | set_param_min(PARAM_TANGENTIAL_ACCEL, 0); |
| 1792 | set_param_min(PARAM_DAMPING, 0); |
| 1793 | set_param_min(PARAM_ANGLE, 0); |
| 1794 | set_param_min(PARAM_SCALE, 1); |
| 1795 | set_param_min(PARAM_HUE_VARIATION, 0); |
| 1796 | set_param_min(PARAM_ANIM_SPEED, 0); |
| 1797 | set_param_min(PARAM_ANIM_OFFSET, 0); |
| 1798 | set_param_max(PARAM_INITIAL_LINEAR_VELOCITY, 0); |
| 1799 | set_param_max(PARAM_ANGULAR_VELOCITY, 0); |
| 1800 | set_param_max(PARAM_ORBIT_VELOCITY, 0); |
| 1801 | set_param_max(PARAM_LINEAR_ACCEL, 0); |
| 1802 | set_param_max(PARAM_RADIAL_ACCEL, 0); |
| 1803 | set_param_max(PARAM_TANGENTIAL_ACCEL, 0); |
| 1804 | set_param_max(PARAM_DAMPING, 0); |
| 1805 | set_param_max(PARAM_ANGLE, 0); |
| 1806 | set_param_max(PARAM_SCALE, 1); |
| 1807 | set_param_max(PARAM_HUE_VARIATION, 0); |
| 1808 | set_param_max(PARAM_ANIM_SPEED, 0); |
| 1809 | set_param_max(PARAM_ANIM_OFFSET, 0); |
| 1810 | set_emission_shape(EMISSION_SHAPE_POINT); |
| 1811 | set_emission_sphere_radius(1); |
| 1812 | set_emission_box_extents(Vector3(1, 1, 1)); |
| 1813 | set_emission_ring_axis(Vector3(0, 0, 1.0)); |
| 1814 | set_emission_ring_height(1); |
| 1815 | set_emission_ring_radius(1); |
| 1816 | set_emission_ring_inner_radius(0); |
| 1817 | set_emission_ring_cone_angle(90); |
| 1818 | |
| 1819 | set_gravity(Vector3(0, -9.8, 0)); |
| 1820 | |
| 1821 | for (int i = 0; i < PARTICLE_FLAG_MAX; i++) { |
| 1822 | particle_flags[i] = false; |
| 1823 | } |
| 1824 | |
| 1825 | set_color(Color(1, 1, 1, 1)); |
| 1826 | } |
| 1827 | |
| 1828 | CPUParticles3D::~CPUParticles3D() { |
| 1829 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 1830 | RS::get_singleton()->free_rid(multimesh); |
| 1831 | } |