| 1 | /**************************************************************************/ |
| 2 | /* particles_storage.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 | #ifdef GLES3_ENABLED |
| 32 | |
| 33 | #include "particles_storage.h" |
| 34 | |
| 35 | #include "config.h" |
| 36 | #include "material_storage.h" |
| 37 | #include "mesh_storage.h" |
| 38 | #include "texture_storage.h" |
| 39 | #include "utilities.h" |
| 40 | |
| 41 | #include "servers/rendering/rendering_server_globals.h" |
| 42 | |
| 43 | using namespace GLES3; |
| 44 | |
| 45 | ParticlesStorage *ParticlesStorage::singleton = nullptr; |
| 46 | |
| 47 | ParticlesStorage *ParticlesStorage::get_singleton() { |
| 48 | return singleton; |
| 49 | } |
| 50 | |
| 51 | ParticlesStorage::ParticlesStorage() { |
| 52 | singleton = this; |
| 53 | GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); |
| 54 | |
| 55 | { |
| 56 | String global_defines; |
| 57 | global_defines += "#define MAX_GLOBAL_SHADER_UNIFORMS 256\n"; // TODO: this is arbitrary for now |
| 58 | material_storage->shaders.particles_process_shader.initialize(global_defines, 1); |
| 59 | } |
| 60 | { |
| 61 | // default material and shader for particles shader |
| 62 | particles_shader.default_shader = material_storage->shader_allocate(); |
| 63 | material_storage->shader_initialize(particles_shader.default_shader); |
| 64 | material_storage->shader_set_code(particles_shader.default_shader, R"( |
| 65 | // Default particles shader. |
| 66 | |
| 67 | shader_type particles; |
| 68 | |
| 69 | void process() { |
| 70 | COLOR = vec4(1.0); |
| 71 | } |
| 72 | )"); |
| 73 | particles_shader.default_material = material_storage->material_allocate(); |
| 74 | material_storage->material_initialize(particles_shader.default_material); |
| 75 | material_storage->material_set_shader(particles_shader.default_material, particles_shader.default_shader); |
| 76 | } |
| 77 | { |
| 78 | particles_shader.copy_shader.initialize(); |
| 79 | particles_shader.copy_shader_version = particles_shader.copy_shader.version_create(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | ParticlesStorage::~ParticlesStorage() { |
| 84 | singleton = nullptr; |
| 85 | GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); |
| 86 | |
| 87 | material_storage->material_free(particles_shader.default_material); |
| 88 | material_storage->shader_free(particles_shader.default_shader); |
| 89 | particles_shader.copy_shader.version_free(particles_shader.copy_shader_version); |
| 90 | } |
| 91 | |
| 92 | /* PARTICLES */ |
| 93 | |
| 94 | RID ParticlesStorage::particles_allocate() { |
| 95 | return particles_owner.allocate_rid(); |
| 96 | } |
| 97 | |
| 98 | void ParticlesStorage::particles_initialize(RID p_rid) { |
| 99 | particles_owner.initialize_rid(p_rid); |
| 100 | } |
| 101 | |
| 102 | void ParticlesStorage::particles_free(RID p_rid) { |
| 103 | Particles *particles = particles_owner.get_or_null(p_rid); |
| 104 | |
| 105 | particles->dependency.deleted_notify(p_rid); |
| 106 | particles->update_list.remove_from_list(); |
| 107 | |
| 108 | _particles_free_data(particles); |
| 109 | particles_owner.free(p_rid); |
| 110 | } |
| 111 | |
| 112 | void ParticlesStorage::particles_set_mode(RID p_particles, RS::ParticlesMode p_mode) { |
| 113 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 114 | ERR_FAIL_NULL(particles); |
| 115 | if (particles->mode == p_mode) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | _particles_free_data(particles); |
| 120 | |
| 121 | particles->mode = p_mode; |
| 122 | } |
| 123 | |
| 124 | void ParticlesStorage::particles_set_emitting(RID p_particles, bool p_emitting) { |
| 125 | ERR_FAIL_COND_MSG(GLES3::Config::get_singleton()->disable_particles_workaround, "Due to driver bugs, GPUParticles are not supported on Adreno 3XX devices. Please use CPUParticles instead."); |
| 126 | |
| 127 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 128 | ERR_FAIL_NULL(particles); |
| 129 | |
| 130 | particles->emitting = p_emitting; |
| 131 | } |
| 132 | |
| 133 | bool ParticlesStorage::particles_get_emitting(RID p_particles) { |
| 134 | if (GLES3::Config::get_singleton()->disable_particles_workaround) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 139 | ERR_FAIL_NULL_V(particles, false); |
| 140 | |
| 141 | return particles->emitting; |
| 142 | } |
| 143 | |
| 144 | void ParticlesStorage::_particles_free_data(Particles *particles) { |
| 145 | particles->userdata_count = 0; |
| 146 | particles->instance_buffer_size_cache = 0; |
| 147 | particles->instance_buffer_stride_cache = 0; |
| 148 | particles->num_attrib_arrays_cache = 0; |
| 149 | particles->process_buffer_stride_cache = 0; |
| 150 | |
| 151 | if (particles->front_process_buffer != 0) { |
| 152 | glDeleteVertexArrays(1, &particles->front_vertex_array); |
| 153 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->front_process_buffer); |
| 154 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->front_instance_buffer); |
| 155 | particles->front_vertex_array = 0; |
| 156 | particles->front_process_buffer = 0; |
| 157 | particles->front_instance_buffer = 0; |
| 158 | |
| 159 | glDeleteVertexArrays(1, &particles->back_vertex_array); |
| 160 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->back_process_buffer); |
| 161 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->back_instance_buffer); |
| 162 | particles->back_vertex_array = 0; |
| 163 | particles->back_process_buffer = 0; |
| 164 | particles->back_instance_buffer = 0; |
| 165 | } |
| 166 | |
| 167 | if (particles->sort_buffer != 0) { |
| 168 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->last_frame_buffer); |
| 169 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->sort_buffer); |
| 170 | particles->last_frame_buffer = 0; |
| 171 | particles->sort_buffer = 0; |
| 172 | particles->sort_buffer_filled = false; |
| 173 | particles->last_frame_buffer_filled = false; |
| 174 | } |
| 175 | |
| 176 | if (particles->frame_params_ubo != 0) { |
| 177 | GLES3::Utilities::get_singleton()->buffer_free_data(particles->frame_params_ubo); |
| 178 | particles->frame_params_ubo = 0; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void ParticlesStorage::particles_set_amount(RID p_particles, int p_amount) { |
| 183 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 184 | ERR_FAIL_NULL(particles); |
| 185 | |
| 186 | if (particles->amount == p_amount) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | _particles_free_data(particles); |
| 191 | |
| 192 | particles->amount = p_amount; |
| 193 | |
| 194 | particles->prev_ticks = 0; |
| 195 | particles->phase = 0; |
| 196 | particles->prev_phase = 0; |
| 197 | particles->clear = true; |
| 198 | |
| 199 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 200 | } |
| 201 | |
| 202 | void ParticlesStorage::particles_set_amount_ratio(RID p_particles, float p_amount_ratio) { |
| 203 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 204 | ERR_FAIL_NULL(particles); |
| 205 | |
| 206 | particles->amount_ratio = p_amount_ratio; |
| 207 | } |
| 208 | |
| 209 | void ParticlesStorage::particles_set_lifetime(RID p_particles, double p_lifetime) { |
| 210 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 211 | ERR_FAIL_NULL(particles); |
| 212 | particles->lifetime = p_lifetime; |
| 213 | } |
| 214 | |
| 215 | void ParticlesStorage::particles_set_one_shot(RID p_particles, bool p_one_shot) { |
| 216 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 217 | ERR_FAIL_NULL(particles); |
| 218 | particles->one_shot = p_one_shot; |
| 219 | } |
| 220 | |
| 221 | void ParticlesStorage::particles_set_pre_process_time(RID p_particles, double p_time) { |
| 222 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 223 | ERR_FAIL_NULL(particles); |
| 224 | particles->pre_process_time = p_time; |
| 225 | } |
| 226 | |
| 227 | void ParticlesStorage::particles_request_process_time(RID p_particles, real_t p_request_process_time) { |
| 228 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 229 | ERR_FAIL_NULL(particles); |
| 230 | particles->request_process_time = p_request_process_time; |
| 231 | } |
| 232 | |
| 233 | void ParticlesStorage::particles_set_seed(RID p_particles, uint32_t p_seed) { |
| 234 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 235 | ERR_FAIL_NULL(particles); |
| 236 | particles->random_seed = p_seed; |
| 237 | } |
| 238 | |
| 239 | void ParticlesStorage::particles_set_explosiveness_ratio(RID p_particles, real_t p_ratio) { |
| 240 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 241 | ERR_FAIL_NULL(particles); |
| 242 | particles->explosiveness = p_ratio; |
| 243 | } |
| 244 | void ParticlesStorage::particles_set_randomness_ratio(RID p_particles, real_t p_ratio) { |
| 245 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 246 | ERR_FAIL_NULL(particles); |
| 247 | particles->randomness = p_ratio; |
| 248 | } |
| 249 | |
| 250 | void ParticlesStorage::particles_set_custom_aabb(RID p_particles, const AABB &p_aabb) { |
| 251 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 252 | ERR_FAIL_NULL(particles); |
| 253 | particles->custom_aabb = p_aabb; |
| 254 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 255 | } |
| 256 | |
| 257 | void ParticlesStorage::particles_set_speed_scale(RID p_particles, double p_scale) { |
| 258 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 259 | ERR_FAIL_NULL(particles); |
| 260 | |
| 261 | particles->speed_scale = p_scale; |
| 262 | } |
| 263 | void ParticlesStorage::particles_set_use_local_coordinates(RID p_particles, bool p_enable) { |
| 264 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 265 | ERR_FAIL_NULL(particles); |
| 266 | |
| 267 | particles->use_local_coords = p_enable; |
| 268 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 269 | } |
| 270 | |
| 271 | void ParticlesStorage::particles_set_fixed_fps(RID p_particles, int p_fps) { |
| 272 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 273 | ERR_FAIL_NULL(particles); |
| 274 | |
| 275 | particles->fixed_fps = p_fps; |
| 276 | |
| 277 | _particles_free_data(particles); |
| 278 | |
| 279 | particles->prev_ticks = 0; |
| 280 | particles->phase = 0; |
| 281 | particles->prev_phase = 0; |
| 282 | particles->clear = true; |
| 283 | |
| 284 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 285 | } |
| 286 | |
| 287 | void ParticlesStorage::particles_set_interpolate(RID p_particles, bool p_enable) { |
| 288 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 289 | ERR_FAIL_NULL(particles); |
| 290 | |
| 291 | particles->interpolate = p_enable; |
| 292 | } |
| 293 | |
| 294 | void ParticlesStorage::particles_set_fractional_delta(RID p_particles, bool p_enable) { |
| 295 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 296 | ERR_FAIL_NULL(particles); |
| 297 | |
| 298 | particles->fractional_delta = p_enable; |
| 299 | } |
| 300 | |
| 301 | void ParticlesStorage::particles_set_trails(RID p_particles, bool p_enable, double p_length) { |
| 302 | if (p_enable) { |
| 303 | WARN_PRINT_ONCE_ED("The Compatibility renderer does not support particle trails."); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void ParticlesStorage::particles_set_trail_bind_poses(RID p_particles, const Vector<Transform3D> &p_bind_poses) { |
| 308 | if (p_bind_poses.size() != 0) { |
| 309 | WARN_PRINT_ONCE_ED("The Compatibility renderer does not support particle trails."); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void ParticlesStorage::particles_set_collision_base_size(RID p_particles, real_t p_size) { |
| 314 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 315 | ERR_FAIL_NULL(particles); |
| 316 | |
| 317 | particles->collision_base_size = p_size; |
| 318 | } |
| 319 | |
| 320 | void ParticlesStorage::particles_set_transform_align(RID p_particles, RS::ParticlesTransformAlign p_transform_align) { |
| 321 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 322 | ERR_FAIL_NULL(particles); |
| 323 | |
| 324 | particles->transform_align = p_transform_align; |
| 325 | } |
| 326 | |
| 327 | void ParticlesStorage::particles_set_process_material(RID p_particles, RID p_material) { |
| 328 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 329 | ERR_FAIL_NULL(particles); |
| 330 | |
| 331 | particles->process_material = p_material; |
| 332 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); //the instance buffer may have changed |
| 333 | } |
| 334 | |
| 335 | RID ParticlesStorage::particles_get_process_material(RID p_particles) const { |
| 336 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 337 | ERR_FAIL_NULL_V(particles, RID()); |
| 338 | |
| 339 | return particles->process_material; |
| 340 | } |
| 341 | |
| 342 | void ParticlesStorage::particles_set_draw_order(RID p_particles, RS::ParticlesDrawOrder p_order) { |
| 343 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 344 | ERR_FAIL_NULL(particles); |
| 345 | |
| 346 | particles->draw_order = p_order; |
| 347 | } |
| 348 | |
| 349 | void ParticlesStorage::particles_set_draw_passes(RID p_particles, int p_passes) { |
| 350 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 351 | ERR_FAIL_NULL(particles); |
| 352 | |
| 353 | particles->draw_passes.resize(p_passes); |
| 354 | } |
| 355 | |
| 356 | void ParticlesStorage::particles_set_draw_pass_mesh(RID p_particles, int p_pass, RID p_mesh) { |
| 357 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 358 | ERR_FAIL_NULL(particles); |
| 359 | ERR_FAIL_INDEX(p_pass, particles->draw_passes.size()); |
| 360 | particles->draw_passes.write[p_pass] = p_mesh; |
| 361 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_PARTICLES); |
| 362 | } |
| 363 | |
| 364 | void ParticlesStorage::particles_restart(RID p_particles) { |
| 365 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 366 | ERR_FAIL_NULL(particles); |
| 367 | |
| 368 | particles->restart_request = true; |
| 369 | } |
| 370 | |
| 371 | void ParticlesStorage::particles_set_subemitter(RID p_particles, RID p_subemitter_particles) { |
| 372 | if (p_subemitter_particles.is_valid()) { |
| 373 | WARN_PRINT_ONCE_ED("The Compatibility renderer does not support particle sub-emitters."); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | void ParticlesStorage::particles_emit(RID p_particles, const Transform3D &p_transform, const Vector3 &p_velocity, const Color &p_color, const Color &p_custom, uint32_t p_emit_flags) { |
| 378 | WARN_PRINT_ONCE_ED("The Compatibility renderer does not support manually emitting particles."); |
| 379 | } |
| 380 | |
| 381 | void ParticlesStorage::particles_request_process(RID p_particles) { |
| 382 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 383 | ERR_FAIL_NULL(particles); |
| 384 | |
| 385 | if (!particles->dirty) { |
| 386 | particles->dirty = true; |
| 387 | |
| 388 | if (!particles->update_list.in_list()) { |
| 389 | particle_update_list.add(&particles->update_list); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | AABB ParticlesStorage::particles_get_current_aabb(RID p_particles) { |
| 395 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 396 | ERR_FAIL_NULL_V(particles, AABB()); |
| 397 | |
| 398 | int total_amount = particles->amount; |
| 399 | |
| 400 | // If available, read from the sort buffer which should be 2 frames out of date. |
| 401 | // This will help alleviate GPU stalls. |
| 402 | GLuint read_buffer = particles->sort_buffer_filled ? particles->sort_buffer : particles->back_instance_buffer; |
| 403 | |
| 404 | Vector<uint8_t> buffer = Utilities::buffer_get_data(GL_ARRAY_BUFFER, read_buffer, total_amount * sizeof(ParticleInstanceData3D)); |
| 405 | ERR_FAIL_COND_V(buffer.size() != (int)(total_amount * sizeof(ParticleInstanceData3D)), AABB()); |
| 406 | |
| 407 | Transform3D inv = particles->emission_transform.affine_inverse(); |
| 408 | |
| 409 | AABB aabb; |
| 410 | if (buffer.size()) { |
| 411 | bool first = true; |
| 412 | |
| 413 | const uint8_t *data_ptr = (const uint8_t *)buffer.ptr(); |
| 414 | uint32_t particle_data_size = sizeof(ParticleInstanceData3D); |
| 415 | |
| 416 | for (int i = 0; i < total_amount; i++) { |
| 417 | const ParticleInstanceData3D &particle_data = *(const ParticleInstanceData3D *)&data_ptr[particle_data_size * i]; |
| 418 | // If scale is 0.0, we assume the particle is inactive. |
| 419 | if (particle_data.xform[0] > 0.0) { |
| 420 | Vector3 pos = Vector3(particle_data.xform[3], particle_data.xform[7], particle_data.xform[11]); |
| 421 | if (!particles->use_local_coords) { |
| 422 | pos = inv.xform(pos); |
| 423 | } |
| 424 | if (first) { |
| 425 | aabb.position = pos; |
| 426 | first = false; |
| 427 | } else { |
| 428 | aabb.expand_to(pos); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | float longest_axis_size = 0; |
| 435 | for (int i = 0; i < particles->draw_passes.size(); i++) { |
| 436 | if (particles->draw_passes[i].is_valid()) { |
| 437 | AABB maabb = MeshStorage::get_singleton()->mesh_get_aabb(particles->draw_passes[i], RID()); |
| 438 | longest_axis_size = MAX(maabb.get_longest_axis_size(), longest_axis_size); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | aabb.grow_by(longest_axis_size); |
| 443 | |
| 444 | return aabb; |
| 445 | } |
| 446 | |
| 447 | AABB ParticlesStorage::particles_get_aabb(RID p_particles) const { |
| 448 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 449 | ERR_FAIL_NULL_V(particles, AABB()); |
| 450 | |
| 451 | return particles->custom_aabb; |
| 452 | } |
| 453 | |
| 454 | void ParticlesStorage::particles_set_emission_transform(RID p_particles, const Transform3D &p_transform) { |
| 455 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 456 | ERR_FAIL_NULL(particles); |
| 457 | |
| 458 | particles->emission_transform = p_transform; |
| 459 | } |
| 460 | |
| 461 | void ParticlesStorage::particles_set_emitter_velocity(RID p_particles, const Vector3 &p_velocity) { |
| 462 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 463 | ERR_FAIL_NULL(particles); |
| 464 | |
| 465 | particles->emitter_velocity = p_velocity; |
| 466 | } |
| 467 | |
| 468 | void ParticlesStorage::particles_set_interp_to_end(RID p_particles, float p_interp) { |
| 469 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 470 | ERR_FAIL_NULL(particles); |
| 471 | |
| 472 | particles->interp_to_end = p_interp; |
| 473 | } |
| 474 | |
| 475 | int ParticlesStorage::particles_get_draw_passes(RID p_particles) const { |
| 476 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 477 | ERR_FAIL_NULL_V(particles, 0); |
| 478 | |
| 479 | return particles->draw_passes.size(); |
| 480 | } |
| 481 | |
| 482 | RID ParticlesStorage::particles_get_draw_pass_mesh(RID p_particles, int p_pass) const { |
| 483 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 484 | ERR_FAIL_NULL_V(particles, RID()); |
| 485 | ERR_FAIL_INDEX_V(p_pass, particles->draw_passes.size(), RID()); |
| 486 | |
| 487 | return particles->draw_passes[p_pass]; |
| 488 | } |
| 489 | |
| 490 | void ParticlesStorage::particles_add_collision(RID p_particles, RID p_particles_collision_instance) { |
| 491 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 492 | ERR_FAIL_NULL(particles); |
| 493 | particles->collisions.insert(p_particles_collision_instance); |
| 494 | } |
| 495 | |
| 496 | void ParticlesStorage::particles_remove_collision(RID p_particles, RID p_particles_collision_instance) { |
| 497 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 498 | ERR_FAIL_NULL(particles); |
| 499 | particles->collisions.erase(p_particles_collision_instance); |
| 500 | } |
| 501 | |
| 502 | void ParticlesStorage::particles_set_canvas_sdf_collision(RID p_particles, bool p_enable, const Transform2D &p_xform, const Rect2 &p_to_screen, GLuint p_texture) { |
| 503 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 504 | ERR_FAIL_NULL(particles); |
| 505 | particles->has_sdf_collision = p_enable; |
| 506 | particles->sdf_collision_transform = p_xform; |
| 507 | particles->sdf_collision_to_screen = p_to_screen; |
| 508 | particles->sdf_collision_texture = p_texture; |
| 509 | } |
| 510 | |
| 511 | // Does one step of processing particles by reading from back_process_buffer and writing to front_process_buffer. |
| 512 | void ParticlesStorage::_particles_process(Particles *p_particles, double p_delta) { |
| 513 | GLES3::TextureStorage *texture_storage = GLES3::TextureStorage::get_singleton(); |
| 514 | GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); |
| 515 | |
| 516 | double new_phase = Math::fmod(p_particles->phase + (p_delta / p_particles->lifetime), 1.0); |
| 517 | |
| 518 | //update current frame |
| 519 | ParticlesFrameParams frame_params; |
| 520 | |
| 521 | if (p_particles->clear) { |
| 522 | p_particles->cycle_number = 0; |
| 523 | } else if (new_phase < p_particles->phase) { |
| 524 | if (p_particles->one_shot) { |
| 525 | p_particles->emitting = false; |
| 526 | } |
| 527 | p_particles->cycle_number++; |
| 528 | } |
| 529 | |
| 530 | frame_params.emitting = p_particles->emitting; |
| 531 | frame_params.system_phase = new_phase; |
| 532 | frame_params.prev_system_phase = p_particles->phase; |
| 533 | |
| 534 | p_particles->phase = new_phase; |
| 535 | |
| 536 | frame_params.time = RSG::rasterizer->get_total_time(); |
| 537 | frame_params.delta = p_delta; |
| 538 | frame_params.random_seed = p_particles->random_seed; |
| 539 | frame_params.explosiveness = p_particles->explosiveness; |
| 540 | frame_params.randomness = p_particles->randomness; |
| 541 | |
| 542 | if (p_particles->use_local_coords) { |
| 543 | GLES3::MaterialStorage::store_transform(Transform3D(), frame_params.emission_transform); |
| 544 | } else { |
| 545 | GLES3::MaterialStorage::store_transform(p_particles->emission_transform, frame_params.emission_transform); |
| 546 | } |
| 547 | |
| 548 | frame_params.cycle = p_particles->cycle_number; |
| 549 | frame_params.frame = p_particles->frame_counter++; |
| 550 | frame_params.amount_ratio = p_particles->amount_ratio; |
| 551 | frame_params.pad1 = 0; |
| 552 | frame_params.pad2 = 0; |
| 553 | frame_params.interp_to_end = p_particles->interp_to_end; |
| 554 | frame_params.emitter_velocity[0] = p_particles->emitter_velocity.x; |
| 555 | frame_params.emitter_velocity[1] = p_particles->emitter_velocity.y; |
| 556 | frame_params.emitter_velocity[2] = p_particles->emitter_velocity.z; |
| 557 | |
| 558 | { //collision and attractors |
| 559 | |
| 560 | frame_params.collider_count = 0; |
| 561 | frame_params.attractor_count = 0; |
| 562 | frame_params.particle_size = p_particles->collision_base_size; |
| 563 | |
| 564 | GLuint collision_heightmap_texture = 0; |
| 565 | |
| 566 | Transform3D to_particles; |
| 567 | if (p_particles->use_local_coords) { |
| 568 | to_particles = p_particles->emission_transform.affine_inverse(); |
| 569 | } |
| 570 | |
| 571 | if (p_particles->has_sdf_collision && p_particles->sdf_collision_texture != 0) { |
| 572 | //2D collision |
| 573 | |
| 574 | Transform2D xform = p_particles->sdf_collision_transform; //will use dotproduct manually so invert beforehand |
| 575 | |
| 576 | if (!p_particles->use_local_coords) { |
| 577 | Transform2D emission; |
| 578 | emission.columns[0] = Vector2(p_particles->emission_transform.basis.get_column(0).x, p_particles->emission_transform.basis.get_column(0).y); |
| 579 | emission.columns[1] = Vector2(p_particles->emission_transform.basis.get_column(1).x, p_particles->emission_transform.basis.get_column(1).y); |
| 580 | emission.set_origin(Vector2(p_particles->emission_transform.origin.x, p_particles->emission_transform.origin.y)); |
| 581 | xform = xform * emission.affine_inverse(); |
| 582 | } |
| 583 | |
| 584 | Transform2D revert = xform.affine_inverse(); |
| 585 | frame_params.collider_count = 1; |
| 586 | frame_params.colliders[0].transform[0] = xform.columns[0][0]; |
| 587 | frame_params.colliders[0].transform[1] = xform.columns[0][1]; |
| 588 | frame_params.colliders[0].transform[2] = 0; |
| 589 | frame_params.colliders[0].transform[3] = xform.columns[2][0]; |
| 590 | |
| 591 | frame_params.colliders[0].transform[4] = xform.columns[1][0]; |
| 592 | frame_params.colliders[0].transform[5] = xform.columns[1][1]; |
| 593 | frame_params.colliders[0].transform[6] = 0; |
| 594 | frame_params.colliders[0].transform[7] = xform.columns[2][1]; |
| 595 | |
| 596 | frame_params.colliders[0].transform[8] = revert.columns[0][0]; |
| 597 | frame_params.colliders[0].transform[9] = revert.columns[0][1]; |
| 598 | frame_params.colliders[0].transform[10] = 0; |
| 599 | frame_params.colliders[0].transform[11] = revert.columns[2][0]; |
| 600 | |
| 601 | frame_params.colliders[0].transform[12] = revert.columns[1][0]; |
| 602 | frame_params.colliders[0].transform[13] = revert.columns[1][1]; |
| 603 | frame_params.colliders[0].transform[14] = 0; |
| 604 | frame_params.colliders[0].transform[15] = revert.columns[2][1]; |
| 605 | |
| 606 | frame_params.colliders[0].extents[0] = p_particles->sdf_collision_to_screen.size.x; |
| 607 | frame_params.colliders[0].extents[1] = p_particles->sdf_collision_to_screen.size.y; |
| 608 | frame_params.colliders[0].extents[2] = p_particles->sdf_collision_to_screen.position.x; |
| 609 | frame_params.colliders[0].scale = p_particles->sdf_collision_to_screen.position.y; |
| 610 | frame_params.colliders[0].type = ParticlesFrameParams::COLLISION_TYPE_2D_SDF; |
| 611 | |
| 612 | collision_heightmap_texture = p_particles->sdf_collision_texture; |
| 613 | } |
| 614 | |
| 615 | for (const RID &E : p_particles->collisions) { |
| 616 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(E); |
| 617 | if (!pci || !pci->active) { |
| 618 | continue; |
| 619 | } |
| 620 | ParticlesCollision *pc = particles_collision_owner.get_or_null(pci->collision); |
| 621 | ERR_CONTINUE(!pc); |
| 622 | |
| 623 | Transform3D to_collider = pci->transform; |
| 624 | if (p_particles->use_local_coords) { |
| 625 | to_collider = to_particles * to_collider; |
| 626 | } |
| 627 | Vector3 scale = to_collider.basis.get_scale(); |
| 628 | to_collider.basis.orthonormalize(); |
| 629 | |
| 630 | if (pc->type <= RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT) { |
| 631 | //attractor |
| 632 | if (frame_params.attractor_count >= ParticlesFrameParams::MAX_ATTRACTORS) { |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | ParticlesFrameParams::Attractor &attr = frame_params.attractors[frame_params.attractor_count]; |
| 637 | |
| 638 | GLES3::MaterialStorage::store_transform(to_collider, attr.transform); |
| 639 | attr.strength = pc->attractor_strength; |
| 640 | attr.attenuation = pc->attractor_attenuation; |
| 641 | attr.directionality = pc->attractor_directionality; |
| 642 | |
| 643 | switch (pc->type) { |
| 644 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: { |
| 645 | attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_SPHERE; |
| 646 | float radius = pc->radius; |
| 647 | radius *= (scale.x + scale.y + scale.z) / 3.0; |
| 648 | attr.extents[0] = radius; |
| 649 | attr.extents[1] = radius; |
| 650 | attr.extents[2] = radius; |
| 651 | } break; |
| 652 | case RS::PARTICLES_COLLISION_TYPE_BOX_ATTRACT: { |
| 653 | attr.type = ParticlesFrameParams::ATTRACTOR_TYPE_BOX; |
| 654 | Vector3 extents = pc->extents * scale; |
| 655 | attr.extents[0] = extents.x; |
| 656 | attr.extents[1] = extents.y; |
| 657 | attr.extents[2] = extents.z; |
| 658 | } break; |
| 659 | case RS::PARTICLES_COLLISION_TYPE_VECTOR_FIELD_ATTRACT: { |
| 660 | WARN_PRINT_ONCE_ED("Vector field particle attractors are not available in the Compatibility renderer."); |
| 661 | } break; |
| 662 | default: { |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | frame_params.attractor_count++; |
| 667 | } else { |
| 668 | //collider |
| 669 | if (frame_params.collider_count >= ParticlesFrameParams::MAX_COLLIDERS) { |
| 670 | continue; |
| 671 | } |
| 672 | |
| 673 | ParticlesFrameParams::Collider &col = frame_params.colliders[frame_params.collider_count]; |
| 674 | |
| 675 | GLES3::MaterialStorage::store_transform(to_collider, col.transform); |
| 676 | switch (pc->type) { |
| 677 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: { |
| 678 | col.type = ParticlesFrameParams::COLLISION_TYPE_SPHERE; |
| 679 | float radius = pc->radius; |
| 680 | radius *= (scale.x + scale.y + scale.z) / 3.0; |
| 681 | col.extents[0] = radius; |
| 682 | col.extents[1] = radius; |
| 683 | col.extents[2] = radius; |
| 684 | } break; |
| 685 | case RS::PARTICLES_COLLISION_TYPE_BOX_COLLIDE: { |
| 686 | col.type = ParticlesFrameParams::COLLISION_TYPE_BOX; |
| 687 | Vector3 extents = pc->extents * scale; |
| 688 | col.extents[0] = extents.x; |
| 689 | col.extents[1] = extents.y; |
| 690 | col.extents[2] = extents.z; |
| 691 | } break; |
| 692 | case RS::PARTICLES_COLLISION_TYPE_SDF_COLLIDE: { |
| 693 | WARN_PRINT_ONCE_ED("SDF Particle Colliders are not available in the Compatibility renderer."); |
| 694 | } break; |
| 695 | case RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE: { |
| 696 | if (collision_heightmap_texture != 0) { //already taken |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | col.type = ParticlesFrameParams::COLLISION_TYPE_HEIGHT_FIELD; |
| 701 | Vector3 extents = pc->extents * scale; |
| 702 | col.extents[0] = extents.x; |
| 703 | col.extents[1] = extents.y; |
| 704 | col.extents[2] = extents.z; |
| 705 | collision_heightmap_texture = pc->heightfield_texture; |
| 706 | } break; |
| 707 | default: { |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | frame_params.collider_count++; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | // Bind heightmap or SDF texture. |
| 716 | GLuint heightmap = collision_heightmap_texture; |
| 717 | if (heightmap == 0) { |
| 718 | GLES3::Texture *tex = texture_storage->get_texture(texture_storage->texture_gl_get_default(GLES3::DEFAULT_GL_TEXTURE_BLACK)); |
| 719 | heightmap = tex->tex_id; |
| 720 | } |
| 721 | glActiveTexture(GL_TEXTURE0); |
| 722 | glBindTexture(GL_TEXTURE_2D, heightmap); |
| 723 | } |
| 724 | |
| 725 | if (p_particles->frame_params_ubo == 0) { |
| 726 | glGenBuffers(1, &p_particles->frame_params_ubo); |
| 727 | glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_FRAME_UNIFORM_LOCATION, p_particles->frame_params_ubo); |
| 728 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_UNIFORM_BUFFER, p_particles->frame_params_ubo, sizeof(ParticlesFrameParams), &frame_params, GL_STREAM_DRAW, "Particle Frame UBO"); |
| 729 | } else { |
| 730 | // Update per-frame UBO. |
| 731 | glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_FRAME_UNIFORM_LOCATION, p_particles->frame_params_ubo); |
| 732 | glBufferData(GL_UNIFORM_BUFFER, sizeof(ParticlesFrameParams), &frame_params, GL_STREAM_DRAW); |
| 733 | } |
| 734 | |
| 735 | // Get shader and set shader uniforms; |
| 736 | ParticleProcessMaterialData *m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(p_particles->process_material, RS::SHADER_PARTICLES)); |
| 737 | if (!m) { |
| 738 | m = static_cast<ParticleProcessMaterialData *>(material_storage->material_get_data(particles_shader.default_material, RS::SHADER_PARTICLES)); |
| 739 | } |
| 740 | |
| 741 | ERR_FAIL_NULL(m); |
| 742 | |
| 743 | ParticlesShaderGLES3::ShaderVariant variant = ParticlesShaderGLES3::MODE_DEFAULT; |
| 744 | |
| 745 | uint32_t specialization = 0; |
| 746 | for (uint32_t i = 0; i < PARTICLES_MAX_USERDATAS; i++) { |
| 747 | if (m->shader_data->userdatas_used[i]) { |
| 748 | specialization |= ParticlesShaderGLES3::USERDATA1_USED << i; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (p_particles->mode == RS::ParticlesMode::PARTICLES_MODE_3D) { |
| 753 | specialization |= ParticlesShaderGLES3::MODE_3D; |
| 754 | } |
| 755 | |
| 756 | RID version = particles_shader.default_shader_version; |
| 757 | if (m->shader_data->version.is_valid() && m->shader_data->valid) { |
| 758 | // Bind material uniform buffer and textures. |
| 759 | m->bind_uniforms(); |
| 760 | version = m->shader_data->version; |
| 761 | } |
| 762 | |
| 763 | bool success = material_storage->shaders.particles_process_shader.version_bind_shader(version, variant, specialization); |
| 764 | if (!success) { |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::LIFETIME, p_particles->lifetime, version, variant, specialization); |
| 769 | material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::CLEAR, p_particles->clear, version, variant, specialization); |
| 770 | material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::TOTAL_PARTICLES, uint32_t(p_particles->amount), version, variant, specialization); |
| 771 | material_storage->shaders.particles_process_shader.version_set_uniform(ParticlesShaderGLES3::USE_FRACTIONAL_DELTA, p_particles->fractional_delta, version, variant, specialization); |
| 772 | |
| 773 | p_particles->clear = false; |
| 774 | |
| 775 | p_particles->has_collision_cache = m->shader_data->uses_collision; |
| 776 | |
| 777 | glBindVertexArray(p_particles->back_vertex_array); |
| 778 | |
| 779 | glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, p_particles->front_process_buffer); |
| 780 | |
| 781 | glBeginTransformFeedback(GL_POINTS); |
| 782 | glDrawArrays(GL_POINTS, 0, p_particles->amount); |
| 783 | glEndTransformFeedback(); |
| 784 | |
| 785 | glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); |
| 786 | glBindVertexArray(0); |
| 787 | |
| 788 | SWAP(p_particles->front_process_buffer, p_particles->back_process_buffer); |
| 789 | SWAP(p_particles->front_vertex_array, p_particles->back_vertex_array); |
| 790 | } |
| 791 | |
| 792 | void ParticlesStorage::particles_set_view_axis(RID p_particles, const Vector3 &p_axis, const Vector3 &p_up_axis) { |
| 793 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 794 | ERR_FAIL_NULL(particles); |
| 795 | |
| 796 | if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) { |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | if (particles->front_process_buffer == 0) { |
| 801 | return; //particles have not processed yet |
| 802 | } |
| 803 | |
| 804 | Vector3 axis = -p_axis; // cameras look to z negative |
| 805 | |
| 806 | if (particles->use_local_coords) { |
| 807 | axis = particles->emission_transform.basis.xform_inv(axis).normalized(); |
| 808 | } |
| 809 | |
| 810 | // Sort will be done on CPU since we don't have compute shaders. |
| 811 | // If the sort_buffer has valid data |
| 812 | // Use a buffer that is 2 frames out of date to avoid stalls. |
| 813 | if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->sort_buffer_filled) { |
| 814 | glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer); |
| 815 | |
| 816 | ParticleInstanceData3D *particle_array; |
| 817 | #ifndef __EMSCRIPTEN__ |
| 818 | particle_array = static_cast<ParticleInstanceData3D *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)); |
| 819 | ERR_FAIL_NULL(particle_array); |
| 820 | #else |
| 821 | LocalVector<ParticleInstanceData3D> particle_vector; |
| 822 | particle_vector.resize(particles->amount); |
| 823 | particle_array = particle_vector.ptr(); |
| 824 | godot_webgl2_glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_array); |
| 825 | #endif |
| 826 | SortArray<ParticleInstanceData3D, ParticlesViewSort> sorter; |
| 827 | sorter.compare.z_dir = axis; |
| 828 | sorter.sort(particle_array, particles->amount); |
| 829 | |
| 830 | #ifndef __EMSCRIPTEN__ |
| 831 | glUnmapBuffer(GL_ARRAY_BUFFER); |
| 832 | #else |
| 833 | glBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(ParticleInstanceData3D), particle_vector.ptr()); |
| 834 | #endif |
| 835 | } |
| 836 | |
| 837 | glEnable(GL_RASTERIZER_DISCARD); |
| 838 | glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 839 | _particles_update_instance_buffer(particles, axis, p_up_axis); |
| 840 | glDisable(GL_RASTERIZER_DISCARD); |
| 841 | } |
| 842 | |
| 843 | void ParticlesStorage::_particles_update_buffers(Particles *particles) { |
| 844 | GLES3::MaterialStorage *material_storage = GLES3::MaterialStorage::get_singleton(); |
| 845 | uint32_t userdata_count = 0; |
| 846 | |
| 847 | if (particles->process_material.is_valid()) { |
| 848 | GLES3::ParticleProcessMaterialData *material_data = static_cast<GLES3::ParticleProcessMaterialData *>(material_storage->material_get_data(particles->process_material, RS::SHADER_PARTICLES)); |
| 849 | if (material_data && material_data->shader_data->version.is_valid() && material_data->shader_data->valid) { |
| 850 | userdata_count = material_data->shader_data->userdata_count; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | if (userdata_count != particles->userdata_count) { |
| 855 | // Mismatch userdata, re-create buffers. |
| 856 | _particles_free_data(particles); |
| 857 | } |
| 858 | |
| 859 | if (particles->amount > 0 && particles->front_process_buffer == 0) { |
| 860 | int total_amount = particles->amount; |
| 861 | |
| 862 | particles->userdata_count = userdata_count; |
| 863 | |
| 864 | uint32_t xform_size = particles->mode == RS::PARTICLES_MODE_2D ? 2 : 3; |
| 865 | particles->instance_buffer_stride_cache = sizeof(float) * 4 * (xform_size + 1); |
| 866 | particles->instance_buffer_size_cache = particles->instance_buffer_stride_cache * total_amount; |
| 867 | particles->num_attrib_arrays_cache = 5 + userdata_count + (xform_size - 2); |
| 868 | particles->process_buffer_stride_cache = sizeof(float) * 4 * particles->num_attrib_arrays_cache; |
| 869 | |
| 870 | PackedByteArray data; |
| 871 | data.resize_initialized(particles->process_buffer_stride_cache * total_amount); |
| 872 | |
| 873 | PackedByteArray instance_data; |
| 874 | instance_data.resize_initialized(particles->instance_buffer_size_cache); |
| 875 | |
| 876 | { |
| 877 | glGenVertexArrays(1, &particles->front_vertex_array); |
| 878 | glBindVertexArray(particles->front_vertex_array); |
| 879 | glGenBuffers(1, &particles->front_process_buffer); |
| 880 | glGenBuffers(1, &particles->front_instance_buffer); |
| 881 | |
| 882 | glBindBuffer(GL_ARRAY_BUFFER, particles->front_process_buffer); |
| 883 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->front_process_buffer, particles->process_buffer_stride_cache * total_amount, data.ptr(), GL_DYNAMIC_COPY, "Particles front process buffer"); |
| 884 | |
| 885 | for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) { |
| 886 | glEnableVertexAttribArray(j); |
| 887 | glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, particles->process_buffer_stride_cache, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j)); |
| 888 | } |
| 889 | glBindVertexArray(0); |
| 890 | |
| 891 | glBindBuffer(GL_ARRAY_BUFFER, particles->front_instance_buffer); |
| 892 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->front_instance_buffer, particles->instance_buffer_size_cache, instance_data.ptr(), GL_DYNAMIC_COPY, "Particles front instance buffer"); |
| 893 | } |
| 894 | |
| 895 | { |
| 896 | glGenVertexArrays(1, &particles->back_vertex_array); |
| 897 | glBindVertexArray(particles->back_vertex_array); |
| 898 | glGenBuffers(1, &particles->back_process_buffer); |
| 899 | glGenBuffers(1, &particles->back_instance_buffer); |
| 900 | |
| 901 | glBindBuffer(GL_ARRAY_BUFFER, particles->back_process_buffer); |
| 902 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->back_process_buffer, particles->process_buffer_stride_cache * total_amount, data.ptr(), GL_DYNAMIC_COPY, "Particles back process buffer"); |
| 903 | |
| 904 | for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) { |
| 905 | glEnableVertexAttribArray(j); |
| 906 | glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, particles->process_buffer_stride_cache, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j)); |
| 907 | } |
| 908 | glBindVertexArray(0); |
| 909 | |
| 910 | glBindBuffer(GL_ARRAY_BUFFER, particles->back_instance_buffer); |
| 911 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->back_instance_buffer, particles->instance_buffer_size_cache, instance_data.ptr(), GL_DYNAMIC_COPY, "Particles back instance buffer"); |
| 912 | } |
| 913 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | void ParticlesStorage::_particles_allocate_history_buffers(Particles *particles) { |
| 918 | if (particles->sort_buffer == 0) { |
| 919 | glGenBuffers(1, &particles->last_frame_buffer); |
| 920 | glBindBuffer(GL_ARRAY_BUFFER, particles->last_frame_buffer); |
| 921 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->last_frame_buffer, particles->instance_buffer_size_cache, nullptr, GL_DYNAMIC_READ, "Particles last frame buffer"); |
| 922 | |
| 923 | glGenBuffers(1, &particles->sort_buffer); |
| 924 | glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer); |
| 925 | GLES3::Utilities::get_singleton()->buffer_allocate_data(GL_ARRAY_BUFFER, particles->sort_buffer, particles->instance_buffer_size_cache, nullptr, GL_DYNAMIC_READ, "Particles sort buffer"); |
| 926 | |
| 927 | particles->sort_buffer_filled = false; |
| 928 | particles->last_frame_buffer_filled = false; |
| 929 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 930 | } |
| 931 | } |
| 932 | void ParticlesStorage::_particles_update_instance_buffer(Particles *particles, const Vector3 &p_axis, const Vector3 &p_up_axis) { |
| 933 | ParticlesCopyShaderGLES3::ShaderVariant variant = ParticlesCopyShaderGLES3::MODE_DEFAULT; |
| 934 | |
| 935 | uint64_t specialization = 0; |
| 936 | if (particles->mode == RS::ParticlesMode::PARTICLES_MODE_3D) { |
| 937 | specialization |= ParticlesCopyShaderGLES3::MODE_3D; |
| 938 | } |
| 939 | |
| 940 | bool success = particles_shader.copy_shader.version_bind_shader(particles_shader.copy_shader_version, variant, specialization); |
| 941 | if (!success) { |
| 942 | return; |
| 943 | } |
| 944 | |
| 945 | // Affect 2D only. |
| 946 | if (particles->use_local_coords) { |
| 947 | // In local mode, particle positions are calculated locally (relative to the node position) |
| 948 | // and they're also drawn locally. |
| 949 | // It works as expected, so we just pass an identity transform. |
| 950 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::INV_EMISSION_TRANSFORM, Transform3D(), particles_shader.copy_shader_version, variant, specialization); |
| 951 | } else { |
| 952 | // In global mode, particle positions are calculated globally (relative to the canvas origin) |
| 953 | // but they're drawn locally. |
| 954 | // So, we need to pass the inverse of the emission transform to bring the |
| 955 | // particles to local coordinates before drawing. |
| 956 | Transform3D inv = particles->emission_transform.affine_inverse(); |
| 957 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::INV_EMISSION_TRANSFORM, inv, particles_shader.copy_shader_version, variant, specialization); |
| 958 | } |
| 959 | |
| 960 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::FRAME_REMAINDER, particles->interpolate ? particles->frame_remainder : 0.0, particles_shader.copy_shader_version, variant, specialization); |
| 961 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::ALIGN_MODE, uint32_t(particles->transform_align), particles_shader.copy_shader_version, variant, specialization); |
| 962 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::ALIGN_UP, p_up_axis, particles_shader.copy_shader_version, variant, specialization); |
| 963 | particles_shader.copy_shader.version_set_uniform(ParticlesCopyShaderGLES3::SORT_DIRECTION, p_axis, particles_shader.copy_shader_version, variant, specialization); |
| 964 | |
| 965 | glBindVertexArray(particles->back_vertex_array); |
| 966 | glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, particles->front_instance_buffer, 0, particles->instance_buffer_size_cache); |
| 967 | glBeginTransformFeedback(GL_POINTS); |
| 968 | |
| 969 | if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_LIFETIME) { |
| 970 | uint32_t lifetime_split = (MIN(int(particles->amount * particles->phase), particles->amount - 1) + 1) % particles->amount; |
| 971 | uint32_t stride = particles->process_buffer_stride_cache; |
| 972 | |
| 973 | glBindBuffer(GL_ARRAY_BUFFER, particles->back_process_buffer); |
| 974 | |
| 975 | // Offset VBO so you render starting at the newest particle. |
| 976 | if (particles->amount - lifetime_split > 0) { |
| 977 | glEnableVertexAttribArray(0); // Color. |
| 978 | glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 0)); |
| 979 | glEnableVertexAttribArray(1); // .xyz: velocity. .z: flags. |
| 980 | glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 1)); |
| 981 | glEnableVertexAttribArray(2); // Custom. |
| 982 | glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 2)); |
| 983 | glEnableVertexAttribArray(3); // Xform1. |
| 984 | glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 3)); |
| 985 | glEnableVertexAttribArray(4); // Xform2. |
| 986 | glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 4)); |
| 987 | if (particles->mode == RS::PARTICLES_MODE_3D) { |
| 988 | glEnableVertexAttribArray(5); // Xform3. |
| 989 | glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(stride * lifetime_split + sizeof(float) * 4 * 5)); |
| 990 | } |
| 991 | |
| 992 | uint32_t to_draw = particles->amount - lifetime_split; |
| 993 | glDrawArrays(GL_POINTS, 0, to_draw); |
| 994 | } |
| 995 | |
| 996 | // Then render from index 0 up intil the newest particle. |
| 997 | if (lifetime_split > 0) { |
| 998 | glEndTransformFeedback(); |
| 999 | // Now output to the second portion of the instance buffer. |
| 1000 | glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, particles->front_instance_buffer, particles->instance_buffer_stride_cache * (particles->amount - lifetime_split), particles->instance_buffer_stride_cache * (lifetime_split)); |
| 1001 | glBeginTransformFeedback(GL_POINTS); |
| 1002 | // Reset back to normal. |
| 1003 | for (uint32_t j = 0; j < particles->num_attrib_arrays_cache; j++) { |
| 1004 | glEnableVertexAttribArray(j); |
| 1005 | glVertexAttribPointer(j, 4, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(sizeof(float) * 4 * j)); |
| 1006 | } |
| 1007 | |
| 1008 | glDrawArrays(GL_POINTS, 0, lifetime_split); |
| 1009 | } |
| 1010 | } else { |
| 1011 | glDrawArrays(GL_POINTS, 0, particles->amount); |
| 1012 | } |
| 1013 | |
| 1014 | glEndTransformFeedback(); |
| 1015 | glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0, 0, 0); |
| 1016 | glBindVertexArray(0); |
| 1017 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 1018 | } |
| 1019 | |
| 1020 | void ParticlesStorage::update_particles() { |
| 1021 | if (!particle_update_list.first()) { |
| 1022 | // Return early to avoid unnecessary state changes. |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | RENDER_TIMESTAMP("Update GPUParticles"); |
| 1027 | glEnable(GL_RASTERIZER_DISCARD); |
| 1028 | glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 1029 | |
| 1030 | GLuint global_buffer = GLES3::MaterialStorage::get_singleton()->global_shader_parameters_get_uniform_buffer(); |
| 1031 | |
| 1032 | glBindBufferBase(GL_UNIFORM_BUFFER, PARTICLES_GLOBALS_UNIFORM_LOCATION, global_buffer); |
| 1033 | glBindBuffer(GL_UNIFORM_BUFFER, 0); |
| 1034 | |
| 1035 | while (particle_update_list.first()) { |
| 1036 | // Use transform feedback to process particles. |
| 1037 | |
| 1038 | Particles *particles = particle_update_list.first()->self(); |
| 1039 | |
| 1040 | particles->update_list.remove_from_list(); |
| 1041 | particles->dirty = false; |
| 1042 | |
| 1043 | _particles_update_buffers(particles); |
| 1044 | |
| 1045 | if (particles->restart_request) { |
| 1046 | particles->prev_ticks = 0; |
| 1047 | particles->phase = 0; |
| 1048 | particles->prev_phase = 0; |
| 1049 | particles->clear = true; |
| 1050 | particles->restart_request = false; |
| 1051 | } |
| 1052 | |
| 1053 | if (particles->inactive && !particles->emitting) { |
| 1054 | //go next |
| 1055 | continue; |
| 1056 | } |
| 1057 | |
| 1058 | if (particles->emitting) { |
| 1059 | if (particles->inactive) { |
| 1060 | //restart system from scratch |
| 1061 | particles->prev_ticks = 0; |
| 1062 | particles->phase = 0; |
| 1063 | particles->prev_phase = 0; |
| 1064 | particles->clear = true; |
| 1065 | } |
| 1066 | particles->inactive = false; |
| 1067 | particles->inactive_time = 0; |
| 1068 | } else { |
| 1069 | particles->inactive_time += particles->speed_scale * RSG::rasterizer->get_frame_delta_time(); |
| 1070 | if (particles->inactive_time > particles->lifetime * 1.2) { |
| 1071 | particles->inactive = true; |
| 1072 | continue; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // Copy the instance buffer that was last used into the last_frame buffer. |
| 1077 | // sort_buffer should now be 2 frames out of date. |
| 1078 | if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH || particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME) { |
| 1079 | _particles_allocate_history_buffers(particles); |
| 1080 | SWAP(particles->last_frame_buffer, particles->sort_buffer); |
| 1081 | |
| 1082 | glBindBuffer(GL_COPY_READ_BUFFER, particles->back_instance_buffer); |
| 1083 | glBindBuffer(GL_COPY_WRITE_BUFFER, particles->last_frame_buffer); |
| 1084 | glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, particles->instance_buffer_size_cache); |
| 1085 | |
| 1086 | // Last frame's last_frame turned into this frame's sort buffer. |
| 1087 | particles->sort_buffer_filled = particles->last_frame_buffer_filled; |
| 1088 | particles->sort_buffer_phase = particles->last_frame_phase; |
| 1089 | particles->last_frame_buffer_filled = true; |
| 1090 | particles->last_frame_phase = particles->phase; |
| 1091 | glBindBuffer(GL_COPY_READ_BUFFER, 0); |
| 1092 | glBindBuffer(GL_COPY_WRITE_BUFFER, 0); |
| 1093 | } |
| 1094 | |
| 1095 | int fixed_fps = 0; |
| 1096 | if (particles->fixed_fps > 0) { |
| 1097 | fixed_fps = particles->fixed_fps; |
| 1098 | } |
| 1099 | |
| 1100 | if (particles->clear && particles->pre_process_time > 0.0) { |
| 1101 | double frame_time; |
| 1102 | if (fixed_fps > 0) { |
| 1103 | frame_time = 1.0 / fixed_fps; |
| 1104 | } else { |
| 1105 | frame_time = 1.0 / 30.0; |
| 1106 | } |
| 1107 | |
| 1108 | double todo = particles->pre_process_time; |
| 1109 | |
| 1110 | while (todo >= 0) { |
| 1111 | _particles_process(particles, frame_time); |
| 1112 | todo -= frame_time; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | double time_scale = MAX(particles->speed_scale, 0.0); |
| 1117 | |
| 1118 | if (fixed_fps > 0) { |
| 1119 | double frame_time = 1.0 / fixed_fps; |
| 1120 | double delta = RSG::rasterizer->get_frame_delta_time(); |
| 1121 | if (delta > 0.1) { //avoid recursive stalls if fps goes below 10 |
| 1122 | delta = 0.1; |
| 1123 | } else if (delta <= 0.0) { //unlikely but.. |
| 1124 | delta = 0.001; |
| 1125 | } |
| 1126 | double todo = particles->frame_remainder + delta * time_scale; |
| 1127 | |
| 1128 | while (todo >= frame_time) { |
| 1129 | _particles_process(particles, frame_time); |
| 1130 | todo -= frame_time; |
| 1131 | } |
| 1132 | |
| 1133 | particles->frame_remainder = todo; |
| 1134 | |
| 1135 | } else { |
| 1136 | _particles_process(particles, RSG::rasterizer->get_frame_delta_time() * time_scale); |
| 1137 | } |
| 1138 | |
| 1139 | if (particles->request_process_time > 0.0) { |
| 1140 | double frame_time; |
| 1141 | if (fixed_fps > 0) { |
| 1142 | frame_time = 1.0 / fixed_fps; |
| 1143 | } else { |
| 1144 | frame_time = 1.0 / 30.0; |
| 1145 | } |
| 1146 | float tmp_scale = particles->speed_scale; |
| 1147 | particles->speed_scale = 1.0; |
| 1148 | double todo = particles->request_process_time; |
| 1149 | while (todo >= 0) { |
| 1150 | _particles_process(particles, frame_time); |
| 1151 | todo -= frame_time; |
| 1152 | } |
| 1153 | particles->speed_scale = tmp_scale; |
| 1154 | particles->request_process_time = 0.0; |
| 1155 | } |
| 1156 | |
| 1157 | // Copy particles to instance buffer and pack Color/Custom. |
| 1158 | // We don't have camera information here, so don't copy here if we need camera information for view depth or align mode. |
| 1159 | if (particles->draw_order != RS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD && particles->transform_align != RS::PARTICLES_TRANSFORM_ALIGN_Z_BILLBOARD_Y_TO_VELOCITY) { |
| 1160 | _particles_update_instance_buffer(particles, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0)); |
| 1161 | |
| 1162 | if (particles->draw_order == RS::PARTICLES_DRAW_ORDER_REVERSE_LIFETIME && particles->sort_buffer_filled) { |
| 1163 | if (particles->mode == RS::ParticlesMode::PARTICLES_MODE_2D) { |
| 1164 | _particles_reverse_lifetime_sort<ParticleInstanceData2D>(particles); |
| 1165 | } else { |
| 1166 | _particles_reverse_lifetime_sort<ParticleInstanceData3D>(particles); |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | SWAP(particles->front_instance_buffer, particles->back_instance_buffer); |
| 1172 | particles->last_change = RSG::rasterizer->get_frame_number(); |
| 1173 | |
| 1174 | // At the end of update, the back_buffer contains the most up-to-date-information to read from. |
| 1175 | |
| 1176 | particles->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1177 | } |
| 1178 | |
| 1179 | glDisable(GL_RASTERIZER_DISCARD); |
| 1180 | } |
| 1181 | |
| 1182 | template <typename ParticleInstanceData> |
| 1183 | void ParticlesStorage::_particles_reverse_lifetime_sort(Particles *particles) { |
| 1184 | glBindBuffer(GL_ARRAY_BUFFER, particles->sort_buffer); |
| 1185 | |
| 1186 | ParticleInstanceData *particle_array; |
| 1187 | uint32_t buffer_size = particles->amount * sizeof(ParticleInstanceData); |
| 1188 | #ifndef __EMSCRIPTEN__ |
| 1189 | particle_array = static_cast<ParticleInstanceData *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, buffer_size, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)); |
| 1190 | |
| 1191 | ERR_FAIL_NULL(particle_array); |
| 1192 | #else |
| 1193 | LocalVector<ParticleInstanceData> particle_vector; |
| 1194 | particle_vector.resize(particles->amount); |
| 1195 | particle_array = particle_vector.ptr(); |
| 1196 | godot_webgl2_glGetBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_array); |
| 1197 | #endif |
| 1198 | |
| 1199 | uint32_t lifetime_split = (MIN(int(particles->amount * particles->sort_buffer_phase), particles->amount - 1) + 1) % particles->amount; |
| 1200 | for (uint32_t i = 0; i < lifetime_split / 2; i++) { |
| 1201 | SWAP(particle_array[i], particle_array[lifetime_split - i - 1]); |
| 1202 | } |
| 1203 | |
| 1204 | for (uint32_t i = 0; i < (particles->amount - lifetime_split) / 2; i++) { |
| 1205 | SWAP(particle_array[lifetime_split + i], particle_array[particles->amount - 1 - i]); |
| 1206 | } |
| 1207 | |
| 1208 | #ifndef __EMSCRIPTEN__ |
| 1209 | glUnmapBuffer(GL_ARRAY_BUFFER); |
| 1210 | #else |
| 1211 | glBufferSubData(GL_ARRAY_BUFFER, 0, buffer_size, particle_vector.ptr()); |
| 1212 | #endif |
| 1213 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 1214 | } |
| 1215 | |
| 1216 | Dependency *ParticlesStorage::particles_get_dependency(RID p_particles) const { |
| 1217 | Particles *particles = particles_owner.get_or_null(p_particles); |
| 1218 | ERR_FAIL_NULL_V(particles, nullptr); |
| 1219 | |
| 1220 | return &particles->dependency; |
| 1221 | } |
| 1222 | |
| 1223 | bool ParticlesStorage::particles_is_inactive(RID p_particles) const { |
| 1224 | const Particles *particles = particles_owner.get_or_null(p_particles); |
| 1225 | ERR_FAIL_NULL_V(particles, false); |
| 1226 | return !particles->emitting && particles->inactive; |
| 1227 | } |
| 1228 | |
| 1229 | /* PARTICLES COLLISION API */ |
| 1230 | |
| 1231 | RID ParticlesStorage::particles_collision_allocate() { |
| 1232 | return particles_collision_owner.allocate_rid(); |
| 1233 | } |
| 1234 | void ParticlesStorage::particles_collision_initialize(RID p_rid) { |
| 1235 | particles_collision_owner.initialize_rid(p_rid, ParticlesCollision()); |
| 1236 | } |
| 1237 | |
| 1238 | void ParticlesStorage::particles_collision_free(RID p_rid) { |
| 1239 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_rid); |
| 1240 | |
| 1241 | if (particles_collision->heightfield_texture != 0) { |
| 1242 | GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture); |
| 1243 | particles_collision->heightfield_texture = 0; |
| 1244 | glDeleteFramebuffers(1, &particles_collision->heightfield_fb); |
| 1245 | particles_collision->heightfield_fb = 0; |
| 1246 | } |
| 1247 | particles_collision->dependency.deleted_notify(p_rid); |
| 1248 | particles_collision_owner.free(p_rid); |
| 1249 | } |
| 1250 | |
| 1251 | GLuint ParticlesStorage::particles_collision_get_heightfield_framebuffer(RID p_particles_collision) const { |
| 1252 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1253 | ERR_FAIL_NULL_V(particles_collision, 0); |
| 1254 | ERR_FAIL_COND_V(particles_collision->type != RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE, 0); |
| 1255 | |
| 1256 | if (particles_collision->heightfield_texture == 0) { |
| 1257 | //create |
| 1258 | const int resolutions[RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX] = { 256, 512, 1024, 2048, 4096, 8192 }; |
| 1259 | Size2i size; |
| 1260 | if (particles_collision->extents.x > particles_collision->extents.z) { |
| 1261 | size.x = resolutions[particles_collision->heightfield_resolution]; |
| 1262 | size.y = int32_t(particles_collision->extents.z / particles_collision->extents.x * size.x); |
| 1263 | } else { |
| 1264 | size.y = resolutions[particles_collision->heightfield_resolution]; |
| 1265 | size.x = int32_t(particles_collision->extents.x / particles_collision->extents.z * size.y); |
| 1266 | } |
| 1267 | |
| 1268 | glGenTextures(1, &particles_collision->heightfield_texture); |
| 1269 | glActiveTexture(GL_TEXTURE0); |
| 1270 | glBindTexture(GL_TEXTURE_2D, particles_collision->heightfield_texture); |
| 1271 | glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr); |
| 1272 | |
| 1273 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 1274 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 1275 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); |
| 1276 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1); |
| 1277 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 1278 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 1279 | |
| 1280 | glGenFramebuffers(1, &particles_collision->heightfield_fb); |
| 1281 | glBindFramebuffer(GL_FRAMEBUFFER, particles_collision->heightfield_fb); |
| 1282 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, particles_collision->heightfield_texture, 0); |
| 1283 | #ifdef DEBUG_ENABLED |
| 1284 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 1285 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 1286 | WARN_PRINT("Could not create heightmap texture, status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status)); |
| 1287 | } |
| 1288 | #endif |
| 1289 | GLES3::Utilities::get_singleton()->texture_allocated_data(particles_collision->heightfield_texture, size.x * size.y * 4, "Particles collision heightfield texture"); |
| 1290 | |
| 1291 | particles_collision->heightfield_fb_size = size; |
| 1292 | |
| 1293 | glBindTexture(GL_TEXTURE_2D, 0); |
| 1294 | glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo); |
| 1295 | } |
| 1296 | |
| 1297 | return particles_collision->heightfield_fb; |
| 1298 | } |
| 1299 | |
| 1300 | void ParticlesStorage::particles_collision_set_collision_type(RID p_particles_collision, RS::ParticlesCollisionType p_type) { |
| 1301 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1302 | ERR_FAIL_NULL(particles_collision); |
| 1303 | |
| 1304 | if (p_type == particles_collision->type) { |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | if (particles_collision->heightfield_texture != 0) { |
| 1309 | GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture); |
| 1310 | particles_collision->heightfield_texture = 0; |
| 1311 | glDeleteFramebuffers(1, &particles_collision->heightfield_fb); |
| 1312 | particles_collision->heightfield_fb = 0; |
| 1313 | } |
| 1314 | |
| 1315 | particles_collision->type = p_type; |
| 1316 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1317 | } |
| 1318 | |
| 1319 | void ParticlesStorage::particles_collision_set_cull_mask(RID p_particles_collision, uint32_t p_cull_mask) { |
| 1320 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1321 | ERR_FAIL_NULL(particles_collision); |
| 1322 | particles_collision->cull_mask = p_cull_mask; |
| 1323 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_CULL_MASK); |
| 1324 | } |
| 1325 | |
| 1326 | uint32_t ParticlesStorage::particles_collision_get_cull_mask(RID p_particles_collision) const { |
| 1327 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1328 | ERR_FAIL_NULL_V(particles_collision, 0); |
| 1329 | return particles_collision->cull_mask; |
| 1330 | } |
| 1331 | |
| 1332 | void ParticlesStorage::particles_collision_set_sphere_radius(RID p_particles_collision, real_t p_radius) { |
| 1333 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1334 | ERR_FAIL_NULL(particles_collision); |
| 1335 | |
| 1336 | particles_collision->radius = p_radius; |
| 1337 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1338 | } |
| 1339 | |
| 1340 | void ParticlesStorage::particles_collision_set_box_extents(RID p_particles_collision, const Vector3 &p_extents) { |
| 1341 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1342 | ERR_FAIL_NULL(particles_collision); |
| 1343 | |
| 1344 | particles_collision->extents = p_extents; |
| 1345 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1346 | } |
| 1347 | |
| 1348 | void ParticlesStorage::particles_collision_set_attractor_strength(RID p_particles_collision, real_t p_strength) { |
| 1349 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1350 | ERR_FAIL_NULL(particles_collision); |
| 1351 | |
| 1352 | particles_collision->attractor_strength = p_strength; |
| 1353 | } |
| 1354 | |
| 1355 | void ParticlesStorage::particles_collision_set_attractor_directionality(RID p_particles_collision, real_t p_directionality) { |
| 1356 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1357 | ERR_FAIL_NULL(particles_collision); |
| 1358 | |
| 1359 | particles_collision->attractor_directionality = p_directionality; |
| 1360 | } |
| 1361 | |
| 1362 | void ParticlesStorage::particles_collision_set_attractor_attenuation(RID p_particles_collision, real_t p_curve) { |
| 1363 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1364 | ERR_FAIL_NULL(particles_collision); |
| 1365 | |
| 1366 | particles_collision->attractor_attenuation = p_curve; |
| 1367 | } |
| 1368 | |
| 1369 | void ParticlesStorage::particles_collision_set_field_texture(RID p_particles_collision, RID p_texture) { |
| 1370 | WARN_PRINT_ONCE_ED("The Compatibility renderer does not support SDF collisions in 3D particle shaders"); |
| 1371 | } |
| 1372 | |
| 1373 | void ParticlesStorage::particles_collision_height_field_update(RID p_particles_collision) { |
| 1374 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1375 | ERR_FAIL_NULL(particles_collision); |
| 1376 | particles_collision->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 1377 | } |
| 1378 | |
| 1379 | void ParticlesStorage::particles_collision_set_height_field_resolution(RID p_particles_collision, RS::ParticlesCollisionHeightfieldResolution p_resolution) { |
| 1380 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1381 | ERR_FAIL_NULL(particles_collision); |
| 1382 | ERR_FAIL_INDEX(p_resolution, RS::PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX); |
| 1383 | |
| 1384 | if (particles_collision->heightfield_resolution == p_resolution) { |
| 1385 | return; |
| 1386 | } |
| 1387 | |
| 1388 | particles_collision->heightfield_resolution = p_resolution; |
| 1389 | |
| 1390 | if (particles_collision->heightfield_texture != 0) { |
| 1391 | GLES3::Utilities::get_singleton()->texture_free_data(particles_collision->heightfield_texture); |
| 1392 | particles_collision->heightfield_texture = 0; |
| 1393 | glDeleteFramebuffers(1, &particles_collision->heightfield_fb); |
| 1394 | particles_collision->heightfield_fb = 0; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | AABB ParticlesStorage::particles_collision_get_aabb(RID p_particles_collision) const { |
| 1399 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1400 | ERR_FAIL_NULL_V(particles_collision, AABB()); |
| 1401 | |
| 1402 | switch (particles_collision->type) { |
| 1403 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_ATTRACT: |
| 1404 | case RS::PARTICLES_COLLISION_TYPE_SPHERE_COLLIDE: { |
| 1405 | AABB aabb; |
| 1406 | aabb.position = -Vector3(1, 1, 1) * particles_collision->radius; |
| 1407 | aabb.size = Vector3(2, 2, 2) * particles_collision->radius; |
| 1408 | return aabb; |
| 1409 | } |
| 1410 | default: { |
| 1411 | AABB aabb; |
| 1412 | aabb.position = -particles_collision->extents; |
| 1413 | aabb.size = particles_collision->extents * 2; |
| 1414 | return aabb; |
| 1415 | } |
| 1416 | } |
| 1417 | } |
| 1418 | |
| 1419 | Vector3 ParticlesStorage::particles_collision_get_extents(RID p_particles_collision) const { |
| 1420 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1421 | ERR_FAIL_NULL_V(particles_collision, Vector3()); |
| 1422 | return particles_collision->extents; |
| 1423 | } |
| 1424 | |
| 1425 | bool ParticlesStorage::particles_collision_is_heightfield(RID p_particles_collision) const { |
| 1426 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1427 | ERR_FAIL_NULL_V(particles_collision, false); |
| 1428 | return particles_collision->type == RS::PARTICLES_COLLISION_TYPE_HEIGHTFIELD_COLLIDE; |
| 1429 | } |
| 1430 | |
| 1431 | uint32_t ParticlesStorage::particles_collision_get_height_field_mask(RID p_particles_collision) const { |
| 1432 | const ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1433 | ERR_FAIL_NULL_V(particles_collision, false); |
| 1434 | return particles_collision->heightfield_mask; |
| 1435 | } |
| 1436 | |
| 1437 | void ParticlesStorage::particles_collision_set_height_field_mask(RID p_particles_collision, uint32_t p_heightfield_mask) { |
| 1438 | ParticlesCollision *particles_collision = particles_collision_owner.get_or_null(p_particles_collision); |
| 1439 | ERR_FAIL_NULL(particles_collision); |
| 1440 | particles_collision->heightfield_mask = p_heightfield_mask; |
| 1441 | } |
| 1442 | |
| 1443 | Dependency *ParticlesStorage::particles_collision_get_dependency(RID p_particles_collision) const { |
| 1444 | ParticlesCollision *pc = particles_collision_owner.get_or_null(p_particles_collision); |
| 1445 | ERR_FAIL_NULL_V(pc, nullptr); |
| 1446 | |
| 1447 | return &pc->dependency; |
| 1448 | } |
| 1449 | |
| 1450 | /* Particles collision instance */ |
| 1451 | |
| 1452 | RID ParticlesStorage::particles_collision_instance_create(RID p_collision) { |
| 1453 | ParticlesCollisionInstance pci; |
| 1454 | pci.collision = p_collision; |
| 1455 | return particles_collision_instance_owner.make_rid(pci); |
| 1456 | } |
| 1457 | |
| 1458 | void ParticlesStorage::particles_collision_instance_free(RID p_rid) { |
| 1459 | particles_collision_instance_owner.free(p_rid); |
| 1460 | } |
| 1461 | |
| 1462 | void ParticlesStorage::particles_collision_instance_set_transform(RID p_collision_instance, const Transform3D &p_transform) { |
| 1463 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance); |
| 1464 | ERR_FAIL_NULL(pci); |
| 1465 | pci->transform = p_transform; |
| 1466 | } |
| 1467 | |
| 1468 | void ParticlesStorage::particles_collision_instance_set_active(RID p_collision_instance, bool p_active) { |
| 1469 | ParticlesCollisionInstance *pci = particles_collision_instance_owner.get_or_null(p_collision_instance); |
| 1470 | ERR_FAIL_NULL(pci); |
| 1471 | pci->active = p_active; |
| 1472 | } |
| 1473 | |
| 1474 | #endif // GLES3_ENABLED |