1 /*************************************************************************/
2 /* particles.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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 "particles.h"
32
33 #include "core/os/os.h"
34 #include "scene/resources/particles_material.h"
35
36 #include "servers/visual_server.h"
37
get_aabb() const38 AABB Particles::get_aabb() const {
39
40 return AABB();
41 }
get_faces(uint32_t p_usage_flags) const42 PoolVector<Face3> Particles::get_faces(uint32_t p_usage_flags) const {
43
44 return PoolVector<Face3>();
45 }
46
set_emitting(bool p_emitting)47 void Particles::set_emitting(bool p_emitting) {
48
49 VS::get_singleton()->particles_set_emitting(particles, p_emitting);
50
51 if (p_emitting && one_shot) {
52 set_process_internal(true);
53 } else if (!p_emitting) {
54 set_process_internal(false);
55 }
56 }
57
set_amount(int p_amount)58 void Particles::set_amount(int p_amount) {
59
60 ERR_FAIL_COND_MSG(p_amount < 1, "Amount of particles cannot be smaller than 1.");
61 amount = p_amount;
62 VS::get_singleton()->particles_set_amount(particles, amount);
63 }
set_lifetime(float p_lifetime)64 void Particles::set_lifetime(float p_lifetime) {
65
66 ERR_FAIL_COND_MSG(p_lifetime <= 0, "Particles lifetime must be greater than 0.");
67 lifetime = p_lifetime;
68 VS::get_singleton()->particles_set_lifetime(particles, lifetime);
69 }
70
set_one_shot(bool p_one_shot)71 void Particles::set_one_shot(bool p_one_shot) {
72
73 one_shot = p_one_shot;
74 VS::get_singleton()->particles_set_one_shot(particles, one_shot);
75
76 if (is_emitting()) {
77
78 set_process_internal(true);
79 if (!one_shot)
80 VisualServer::get_singleton()->particles_restart(particles);
81 }
82
83 if (!one_shot)
84 set_process_internal(false);
85 }
86
set_pre_process_time(float p_time)87 void Particles::set_pre_process_time(float p_time) {
88
89 pre_process_time = p_time;
90 VS::get_singleton()->particles_set_pre_process_time(particles, pre_process_time);
91 }
set_explosiveness_ratio(float p_ratio)92 void Particles::set_explosiveness_ratio(float p_ratio) {
93
94 explosiveness_ratio = p_ratio;
95 VS::get_singleton()->particles_set_explosiveness_ratio(particles, explosiveness_ratio);
96 }
set_randomness_ratio(float p_ratio)97 void Particles::set_randomness_ratio(float p_ratio) {
98
99 randomness_ratio = p_ratio;
100 VS::get_singleton()->particles_set_randomness_ratio(particles, randomness_ratio);
101 }
set_visibility_aabb(const AABB & p_aabb)102 void Particles::set_visibility_aabb(const AABB &p_aabb) {
103
104 visibility_aabb = p_aabb;
105 VS::get_singleton()->particles_set_custom_aabb(particles, visibility_aabb);
106 update_gizmo();
107 _change_notify("visibility_aabb");
108 }
set_use_local_coordinates(bool p_enable)109 void Particles::set_use_local_coordinates(bool p_enable) {
110
111 local_coords = p_enable;
112 VS::get_singleton()->particles_set_use_local_coordinates(particles, local_coords);
113 }
set_process_material(const Ref<Material> & p_material)114 void Particles::set_process_material(const Ref<Material> &p_material) {
115
116 process_material = p_material;
117 RID material_rid;
118 if (process_material.is_valid())
119 material_rid = process_material->get_rid();
120 VS::get_singleton()->particles_set_process_material(particles, material_rid);
121
122 update_configuration_warning();
123 }
124
set_speed_scale(float p_scale)125 void Particles::set_speed_scale(float p_scale) {
126
127 speed_scale = p_scale;
128 VS::get_singleton()->particles_set_speed_scale(particles, p_scale);
129 }
130
is_emitting() const131 bool Particles::is_emitting() const {
132
133 return VS::get_singleton()->particles_get_emitting(particles);
134 }
get_amount() const135 int Particles::get_amount() const {
136
137 return amount;
138 }
get_lifetime() const139 float Particles::get_lifetime() const {
140
141 return lifetime;
142 }
get_one_shot() const143 bool Particles::get_one_shot() const {
144
145 return one_shot;
146 }
147
get_pre_process_time() const148 float Particles::get_pre_process_time() const {
149
150 return pre_process_time;
151 }
get_explosiveness_ratio() const152 float Particles::get_explosiveness_ratio() const {
153
154 return explosiveness_ratio;
155 }
get_randomness_ratio() const156 float Particles::get_randomness_ratio() const {
157
158 return randomness_ratio;
159 }
get_visibility_aabb() const160 AABB Particles::get_visibility_aabb() const {
161
162 return visibility_aabb;
163 }
get_use_local_coordinates() const164 bool Particles::get_use_local_coordinates() const {
165
166 return local_coords;
167 }
get_process_material() const168 Ref<Material> Particles::get_process_material() const {
169
170 return process_material;
171 }
172
get_speed_scale() const173 float Particles::get_speed_scale() const {
174
175 return speed_scale;
176 }
177
set_draw_order(DrawOrder p_order)178 void Particles::set_draw_order(DrawOrder p_order) {
179
180 draw_order = p_order;
181 VS::get_singleton()->particles_set_draw_order(particles, VS::ParticlesDrawOrder(p_order));
182 }
183
get_draw_order() const184 Particles::DrawOrder Particles::get_draw_order() const {
185
186 return draw_order;
187 }
188
set_draw_passes(int p_count)189 void Particles::set_draw_passes(int p_count) {
190
191 ERR_FAIL_COND(p_count < 1);
192 draw_passes.resize(p_count);
193 VS::get_singleton()->particles_set_draw_passes(particles, p_count);
194 _change_notify();
195 }
get_draw_passes() const196 int Particles::get_draw_passes() const {
197
198 return draw_passes.size();
199 }
200
set_draw_pass_mesh(int p_pass,const Ref<Mesh> & p_mesh)201 void Particles::set_draw_pass_mesh(int p_pass, const Ref<Mesh> &p_mesh) {
202
203 ERR_FAIL_INDEX(p_pass, draw_passes.size());
204
205 draw_passes.write[p_pass] = p_mesh;
206
207 RID mesh_rid;
208 if (p_mesh.is_valid())
209 mesh_rid = p_mesh->get_rid();
210
211 VS::get_singleton()->particles_set_draw_pass_mesh(particles, p_pass, mesh_rid);
212
213 update_configuration_warning();
214 }
215
get_draw_pass_mesh(int p_pass) const216 Ref<Mesh> Particles::get_draw_pass_mesh(int p_pass) const {
217
218 ERR_FAIL_INDEX_V(p_pass, draw_passes.size(), Ref<Mesh>());
219
220 return draw_passes[p_pass];
221 }
222
set_fixed_fps(int p_count)223 void Particles::set_fixed_fps(int p_count) {
224 fixed_fps = p_count;
225 VS::get_singleton()->particles_set_fixed_fps(particles, p_count);
226 }
227
get_fixed_fps() const228 int Particles::get_fixed_fps() const {
229 return fixed_fps;
230 }
231
set_fractional_delta(bool p_enable)232 void Particles::set_fractional_delta(bool p_enable) {
233 fractional_delta = p_enable;
234 VS::get_singleton()->particles_set_fractional_delta(particles, p_enable);
235 }
236
get_fractional_delta() const237 bool Particles::get_fractional_delta() const {
238 return fractional_delta;
239 }
240
get_configuration_warning() const241 String Particles::get_configuration_warning() const {
242
243 if (OS::get_singleton()->get_current_video_driver() == OS::VIDEO_DRIVER_GLES2) {
244 return TTR("GPU-based particles are not supported by the GLES2 video driver.\nUse the CPUParticles node instead. You can use the \"Convert to CPUParticles\" option for this purpose.");
245 }
246
247 String warnings;
248
249 bool meshes_found = false;
250 bool anim_material_found = false;
251
252 for (int i = 0; i < draw_passes.size(); i++) {
253 if (draw_passes[i].is_valid()) {
254 meshes_found = true;
255 for (int j = 0; j < draw_passes[i]->get_surface_count(); j++) {
256 anim_material_found = Object::cast_to<ShaderMaterial>(draw_passes[i]->surface_get_material(j).ptr()) != NULL;
257 SpatialMaterial *spat = Object::cast_to<SpatialMaterial>(draw_passes[i]->surface_get_material(j).ptr());
258 anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == SpatialMaterial::BILLBOARD_PARTICLES);
259 }
260 if (anim_material_found) break;
261 }
262 }
263
264 anim_material_found = anim_material_found || Object::cast_to<ShaderMaterial>(get_material_override().ptr()) != NULL;
265 SpatialMaterial *spat = Object::cast_to<SpatialMaterial>(get_material_override().ptr());
266 anim_material_found = anim_material_found || (spat && spat->get_billboard_mode() == SpatialMaterial::BILLBOARD_PARTICLES);
267
268 if (!meshes_found) {
269 if (warnings != String())
270 warnings += "\n";
271 warnings += "- " + TTR("Nothing is visible because meshes have not been assigned to draw passes.");
272 }
273
274 if (process_material.is_null()) {
275 if (warnings != String())
276 warnings += "\n";
277 warnings += "- " + TTR("A material to process the particles is not assigned, so no behavior is imprinted.");
278 } else {
279 const ParticlesMaterial *process = Object::cast_to<ParticlesMaterial>(process_material.ptr());
280 if (!anim_material_found && process &&
281 (process->get_param(ParticlesMaterial::PARAM_ANIM_SPEED) != 0.0 || process->get_param(ParticlesMaterial::PARAM_ANIM_OFFSET) != 0.0 ||
282 process->get_param_texture(ParticlesMaterial::PARAM_ANIM_SPEED).is_valid() || process->get_param_texture(ParticlesMaterial::PARAM_ANIM_OFFSET).is_valid())) {
283 if (warnings != String())
284 warnings += "\n";
285 warnings += "- " + TTR("Particles animation requires the usage of a SpatialMaterial whose Billboard Mode is set to \"Particle Billboard\".");
286 }
287 }
288
289 return warnings;
290 }
291
restart()292 void Particles::restart() {
293
294 VisualServer::get_singleton()->particles_restart(particles);
295 VisualServer::get_singleton()->particles_set_emitting(particles, true);
296 }
297
capture_aabb() const298 AABB Particles::capture_aabb() const {
299
300 return VS::get_singleton()->particles_get_current_aabb(particles);
301 }
302
_validate_property(PropertyInfo & property) const303 void Particles::_validate_property(PropertyInfo &property) const {
304
305 if (property.name.begins_with("draw_pass_")) {
306 int index = property.name.get_slicec('_', 2).to_int() - 1;
307 if (index >= draw_passes.size()) {
308 property.usage = 0;
309 return;
310 }
311 }
312 }
313
_notification(int p_what)314 void Particles::_notification(int p_what) {
315
316 if (p_what == NOTIFICATION_PAUSED || p_what == NOTIFICATION_UNPAUSED) {
317 if (can_process()) {
318 VS::get_singleton()->particles_set_speed_scale(particles, speed_scale);
319 } else {
320
321 VS::get_singleton()->particles_set_speed_scale(particles, 0);
322 }
323 }
324
325 // Use internal process when emitting and one_shot are on so that when
326 // the shot ends the editor can properly update
327 if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
328
329 if (one_shot && !is_emitting()) {
330 _change_notify();
331 set_process_internal(false);
332 }
333 }
334
335 if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
336 // make sure particles are updated before rendering occurs if they were active before
337 if (is_visible_in_tree() && !VS::get_singleton()->particles_is_inactive(particles)) {
338 VS::get_singleton()->particles_request_process(particles);
339 }
340 }
341 }
342
_bind_methods()343 void Particles::_bind_methods() {
344
345 ClassDB::bind_method(D_METHOD("set_emitting", "emitting"), &Particles::set_emitting);
346 ClassDB::bind_method(D_METHOD("set_amount", "amount"), &Particles::set_amount);
347 ClassDB::bind_method(D_METHOD("set_lifetime", "secs"), &Particles::set_lifetime);
348 ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Particles::set_one_shot);
349 ClassDB::bind_method(D_METHOD("set_pre_process_time", "secs"), &Particles::set_pre_process_time);
350 ClassDB::bind_method(D_METHOD("set_explosiveness_ratio", "ratio"), &Particles::set_explosiveness_ratio);
351 ClassDB::bind_method(D_METHOD("set_randomness_ratio", "ratio"), &Particles::set_randomness_ratio);
352 ClassDB::bind_method(D_METHOD("set_visibility_aabb", "aabb"), &Particles::set_visibility_aabb);
353 ClassDB::bind_method(D_METHOD("set_use_local_coordinates", "enable"), &Particles::set_use_local_coordinates);
354 ClassDB::bind_method(D_METHOD("set_fixed_fps", "fps"), &Particles::set_fixed_fps);
355 ClassDB::bind_method(D_METHOD("set_fractional_delta", "enable"), &Particles::set_fractional_delta);
356 ClassDB::bind_method(D_METHOD("set_process_material", "material"), &Particles::set_process_material);
357 ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &Particles::set_speed_scale);
358
359 ClassDB::bind_method(D_METHOD("is_emitting"), &Particles::is_emitting);
360 ClassDB::bind_method(D_METHOD("get_amount"), &Particles::get_amount);
361 ClassDB::bind_method(D_METHOD("get_lifetime"), &Particles::get_lifetime);
362 ClassDB::bind_method(D_METHOD("get_one_shot"), &Particles::get_one_shot);
363 ClassDB::bind_method(D_METHOD("get_pre_process_time"), &Particles::get_pre_process_time);
364 ClassDB::bind_method(D_METHOD("get_explosiveness_ratio"), &Particles::get_explosiveness_ratio);
365 ClassDB::bind_method(D_METHOD("get_randomness_ratio"), &Particles::get_randomness_ratio);
366 ClassDB::bind_method(D_METHOD("get_visibility_aabb"), &Particles::get_visibility_aabb);
367 ClassDB::bind_method(D_METHOD("get_use_local_coordinates"), &Particles::get_use_local_coordinates);
368 ClassDB::bind_method(D_METHOD("get_fixed_fps"), &Particles::get_fixed_fps);
369 ClassDB::bind_method(D_METHOD("get_fractional_delta"), &Particles::get_fractional_delta);
370 ClassDB::bind_method(D_METHOD("get_process_material"), &Particles::get_process_material);
371 ClassDB::bind_method(D_METHOD("get_speed_scale"), &Particles::get_speed_scale);
372
373 ClassDB::bind_method(D_METHOD("set_draw_order", "order"), &Particles::set_draw_order);
374
375 ClassDB::bind_method(D_METHOD("get_draw_order"), &Particles::get_draw_order);
376
377 ClassDB::bind_method(D_METHOD("set_draw_passes", "passes"), &Particles::set_draw_passes);
378 ClassDB::bind_method(D_METHOD("set_draw_pass_mesh", "pass", "mesh"), &Particles::set_draw_pass_mesh);
379
380 ClassDB::bind_method(D_METHOD("get_draw_passes"), &Particles::get_draw_passes);
381 ClassDB::bind_method(D_METHOD("get_draw_pass_mesh", "pass"), &Particles::get_draw_pass_mesh);
382
383 ClassDB::bind_method(D_METHOD("restart"), &Particles::restart);
384 ClassDB::bind_method(D_METHOD("capture_aabb"), &Particles::capture_aabb);
385
386 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emitting"), "set_emitting", "is_emitting");
387 ADD_PROPERTY(PropertyInfo(Variant::INT, "amount", PROPERTY_HINT_EXP_RANGE, "1,1000000,1"), "set_amount", "get_amount");
388 ADD_GROUP("Time", "");
389 ADD_PROPERTY(PropertyInfo(Variant::REAL, "lifetime", PROPERTY_HINT_EXP_RANGE, "0.01,600.0,0.01,or_greater"), "set_lifetime", "get_lifetime");
390 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
391 ADD_PROPERTY(PropertyInfo(Variant::REAL, "preprocess", PROPERTY_HINT_EXP_RANGE, "0.00,600.0,0.01"), "set_pre_process_time", "get_pre_process_time");
392 ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed_scale", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_speed_scale", "get_speed_scale");
393 ADD_PROPERTY(PropertyInfo(Variant::REAL, "explosiveness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_explosiveness_ratio", "get_explosiveness_ratio");
394 ADD_PROPERTY(PropertyInfo(Variant::REAL, "randomness", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_randomness_ratio", "get_randomness_ratio");
395 ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_fps", PROPERTY_HINT_RANGE, "0,1000,1"), "set_fixed_fps", "get_fixed_fps");
396 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fract_delta"), "set_fractional_delta", "get_fractional_delta");
397 ADD_GROUP("Drawing", "");
398 ADD_PROPERTY(PropertyInfo(Variant::AABB, "visibility_aabb"), "set_visibility_aabb", "get_visibility_aabb");
399 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "local_coords"), "set_use_local_coordinates", "get_use_local_coordinates");
400 ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_order", PROPERTY_HINT_ENUM, "Index,Lifetime,View Depth"), "set_draw_order", "get_draw_order");
401 ADD_GROUP("Process Material", "");
402 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "process_material", PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,ParticlesMaterial"), "set_process_material", "get_process_material");
403 ADD_GROUP("Draw Passes", "draw_");
404 ADD_PROPERTY(PropertyInfo(Variant::INT, "draw_passes", PROPERTY_HINT_RANGE, "0," + itos(MAX_DRAW_PASSES) + ",1"), "set_draw_passes", "get_draw_passes");
405 for (int i = 0; i < MAX_DRAW_PASSES; i++) {
406
407 ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "draw_pass_" + itos(i + 1), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_draw_pass_mesh", "get_draw_pass_mesh", i);
408 }
409
410 BIND_ENUM_CONSTANT(DRAW_ORDER_INDEX);
411 BIND_ENUM_CONSTANT(DRAW_ORDER_LIFETIME);
412 BIND_ENUM_CONSTANT(DRAW_ORDER_VIEW_DEPTH);
413
414 BIND_CONSTANT(MAX_DRAW_PASSES);
415 }
416
Particles()417 Particles::Particles() {
418
419 particles = VS::get_singleton()->particles_create();
420 set_base(particles);
421 one_shot = false; // Needed so that set_emitting doesn't access uninitialized values
422 set_emitting(true);
423 set_one_shot(false);
424 set_amount(8);
425 set_lifetime(1);
426 set_fixed_fps(0);
427 set_fractional_delta(true);
428 set_pre_process_time(0);
429 set_explosiveness_ratio(0);
430 set_randomness_ratio(0);
431 set_visibility_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)));
432 set_use_local_coordinates(true);
433 set_draw_passes(1);
434 set_draw_order(DRAW_ORDER_INDEX);
435 set_speed_scale(1);
436 }
437
~Particles()438 Particles::~Particles() {
439
440 VS::get_singleton()->free(particles);
441 }
442