Debian Codegraph @

scene/resources/particle_process_material.h

raw symbols SCIP
1 /**************************************************************************/
2 /* particle_process_material.h */
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 #pragma once
32
33 #include "core/templates/rid.h"
34 #include "core/templates/self_list.h"
35 #include "scene/resources/curve_texture.h"
36 #include "scene/resources/material.h"
37
38 /*
39 TODO:
40 -Path following
41 -Emitter positions deformable by bones
42 -Proper trails
43 */
44
45 class ParticleProcessMaterial : public Material {
46 GDCLASS(ParticleProcessMaterial, Material);
47
48 public:
49 enum Parameter {
50 PARAM_INITIAL_LINEAR_VELOCITY,
51 PARAM_ANGULAR_VELOCITY,
52 PARAM_ORBIT_VELOCITY,
53 PARAM_LINEAR_ACCEL,
54 PARAM_RADIAL_ACCEL,
55 PARAM_TANGENTIAL_ACCEL,
56 PARAM_DAMPING,
57 PARAM_ANGLE,
58 PARAM_SCALE,
59 PARAM_HUE_VARIATION,
60 PARAM_ANIM_SPEED,
61 PARAM_ANIM_OFFSET,
62 PARAM_TURB_INFLUENCE_OVER_LIFE,
63 PARAM_TURB_VEL_INFLUENCE,
64 PARAM_TURB_INIT_DISPLACEMENT,
65 PARAM_RADIAL_VELOCITY,
66 PARAM_DIRECTIONAL_VELOCITY,
67 PARAM_SCALE_OVER_VELOCITY,
68 PARAM_MAX
69 };
70
71 // When extending, make sure not to overflow the size of the MaterialKey below.
72 enum ParticleFlags {
73 PARTICLE_FLAG_ALIGN_Y_TO_VELOCITY,
74 PARTICLE_FLAG_ROTATE_Y,
75 PARTICLE_FLAG_DISABLE_Z,
76 PARTICLE_FLAG_DAMPING_AS_FRICTION,
77 PARTICLE_FLAG_MAX
78 };
79
80 // When extending, make sure not to overflow the size of the MaterialKey below.
81 enum EmissionShape {
82 EMISSION_SHAPE_POINT,
83 EMISSION_SHAPE_SPHERE,
84 EMISSION_SHAPE_SPHERE_SURFACE,
85 EMISSION_SHAPE_BOX,
86 EMISSION_SHAPE_POINTS,
87 EMISSION_SHAPE_DIRECTED_POINTS,
88 EMISSION_SHAPE_RING,
89 EMISSION_SHAPE_MAX
90 };
91
92 // When extending, make sure not to overflow the size of the MaterialKey below.
93 enum SubEmitterMode {
94 SUB_EMITTER_DISABLED,
95 SUB_EMITTER_CONSTANT,
96 SUB_EMITTER_AT_END,
97 SUB_EMITTER_AT_COLLISION,
98 SUB_EMITTER_AT_START,
99 SUB_EMITTER_MAX
100 };
101
102 // When extending, make sure not to overflow the size of the MaterialKey below.
103 enum CollisionMode {
104 COLLISION_DISABLED,
105 COLLISION_RIGID,
106 COLLISION_HIDE_ON_CONTACT,
107 COLLISION_MAX
108 };
109
110 private:
111 struct MaterialKey {
112 // The bit size of the struct must be kept below or equal to 64 bits.
113 // Consider this when extending ParticleFlags, EmissionShape, or SubEmitterMode.
114 uint64_t texture_mask : PARAM_MAX;
115 uint64_t texture_color : 1;
116 uint64_t particle_flags : PARTICLE_FLAG_MAX;
117 uint64_t emission_shape : 3;
118 uint64_t invalid_key : 1;
119 uint64_t has_emission_color : 1;
120 uint64_t sub_emitter : 3;
121 uint64_t attractor_enabled : 1;
122 uint64_t collision_mode : 2;
123 uint64_t collision_scale : 1;
124 uint64_t turbulence_enabled : 1;
125 uint64_t limiter_curve : 1;
126 uint64_t alpha_curve : 1;
127 uint64_t emission_curve : 1;
128 uint64_t has_initial_ramp : 1;
129 uint64_t orbit_uses_curve_xyz : 1;
130
131 MaterialKey() {
132 memset(this, 0, sizeof(MaterialKey));
133 }
134
135 static uint32_t hash(const MaterialKey &p_key) {
136 return hash_djb2_buffer((const uint8_t *)&p_key, sizeof(MaterialKey));
137 }
138 bool operator==(const MaterialKey &p_key) const {
139 return memcmp(this, &p_key, sizeof(MaterialKey)) == 0;
140 }
141 bool operator<(const MaterialKey &p_key) const {
142 return memcmp(this, &p_key, sizeof(MaterialKey)) < 0;
143 }
144 };
145
146 struct ShaderData {
147 RID shader;
148 int users = 0;
149 };
150
151 static Mutex shader_map_mutex;
152 static HashMap<MaterialKey, ShaderData, MaterialKey> shader_map;
153 static RBSet<String> min_max_properties;
154
155 MaterialKey current_key;
156 RID shader_rid;
157
158 _FORCE_INLINE_ MaterialKey _compute_key() const {
159 MaterialKey mk;
160
161 mk.texture_color = color_ramp.is_valid() ? 1 : 0;
162 mk.emission_shape = emission_shape;
163 mk.has_emission_color = emission_shape >= EMISSION_SHAPE_POINTS && emission_color_texture.is_valid();
164 mk.sub_emitter = sub_emitter_mode;
165 mk.collision_mode = collision_mode;
166 mk.attractor_enabled = attractor_interaction_enabled;
167 mk.collision_scale = collision_scale;
168 mk.turbulence_enabled = turbulence_enabled;
169 mk.limiter_curve = velocity_limit_curve.is_valid() ? 1 : 0;
170 mk.alpha_curve = alpha_curve.is_valid() ? 1 : 0;
171 mk.emission_curve = emission_curve.is_valid() ? 1 : 0;
172 mk.has_initial_ramp = color_initial_ramp.is_valid() ? 1 : 0;
173 CurveXYZTexture *texture = Object::cast_to<CurveXYZTexture>(tex_parameters[PARAM_ORBIT_VELOCITY].ptr());
174 mk.orbit_uses_curve_xyz = texture ? 1 : 0;
175
176 for (int i = 0; i < PARAM_MAX; i++) {
177 if (tex_parameters[i].is_valid()) {
178 mk.texture_mask |= ((uint64_t)1 << i);
179 }
180 }
181 for (int i = 0; i < PARTICLE_FLAG_MAX; i++) {
182 if (particle_flags[i]) {
183 mk.particle_flags |= ((uint64_t)1 << i);
184 }
185 }
186
187 return mk;
188 }
189
190 static Mutex dirty_materials_mutex;
191 static SelfList<ParticleProcessMaterial>::List dirty_materials;
192
193 struct ShaderNames {
194 StringName direction;
195 StringName spread;
196 StringName flatness;
197 StringName initial_linear_velocity_min;
198 StringName initial_angle_min;
199 StringName angular_velocity_min;
200 StringName orbit_velocity_min;
201 StringName radial_velocity_min;
202 StringName linear_accel_min;
203 StringName radial_accel_min;
204 StringName tangent_accel_min;
205 StringName damping_min;
206 StringName scale_min;
207 StringName scale_over_velocity_min;
208 StringName hue_variation_min;
209 StringName anim_speed_min;
210 StringName anim_offset_min;
211 StringName directional_velocity_min;
212
213 StringName initial_linear_velocity_max;
214 StringName initial_angle_max;
215 StringName angular_velocity_max;
216 StringName orbit_velocity_max;
217 StringName radial_velocity_max;
218 StringName linear_accel_max;
219 StringName radial_accel_max;
220 StringName tangent_accel_max;
221 StringName damping_max;
222 StringName scale_max;
223 StringName scale_over_velocity_max;
224 StringName hue_variation_max;
225 StringName anim_speed_max;
226 StringName anim_offset_max;
227 StringName directional_velocity_max;
228
229 StringName angle_texture;
230 StringName angular_velocity_texture;
231 StringName orbit_velocity_texture;
232 StringName radial_velocity_texture;
233 StringName linear_accel_texture;
234 StringName radial_accel_texture;
235 StringName tangent_accel_texture;
236 StringName damping_texture;
237 StringName scale_texture;
238 StringName scale_over_velocity_texture;
239 StringName hue_variation_texture;
240 StringName anim_speed_texture;
241 StringName anim_offset_texture;
242 StringName velocity_limiter_texture;
243 StringName directional_velocity_texture;
244
245 StringName color;
246 StringName color_ramp;
247 StringName alpha_ramp;
248 StringName emission_ramp;
249 StringName color_initial_ramp;
250
251 StringName velocity_limit_curve;
252 StringName velocity_pivot;
253
254 StringName emission_sphere_radius;
255 StringName emission_box_extents;
256 StringName emission_texture_point_count;
257 StringName emission_texture_points;
258 StringName emission_texture_normal;
259 StringName emission_texture_color;
260 StringName emission_ring_axis;
261 StringName emission_ring_height;
262 StringName emission_ring_radius;
263 StringName emission_ring_inner_radius;
264 StringName emission_ring_cone_angle;
265 StringName emission_shape_offset;
266 StringName emission_shape_scale;
267
268 StringName turbulence_enabled;
269 StringName turbulence_noise_strength;
270 StringName turbulence_noise_scale;
271 StringName turbulence_noise_speed;
272 StringName turbulence_noise_speed_random;
273 StringName turbulence_influence_over_life;
274 StringName turbulence_influence_min;
275 StringName turbulence_influence_max;
276 StringName turbulence_initial_displacement_min;
277 StringName turbulence_initial_displacement_max;
278
279 StringName gravity;
280 StringName inherit_emitter_velocity_ratio;
281
282 StringName lifetime_randomness;
283
284 StringName sub_emitter_frequency;
285 StringName sub_emitter_amount_at_end;
286 StringName sub_emitter_amount_at_collision;
287 StringName sub_emitter_amount_at_start;
288 StringName sub_emitter_keep_velocity;
289
290 StringName collision_friction;
291 StringName collision_bounce;
292 };
293
294 static ShaderNames *shader_names;
295
296 SelfList<ParticleProcessMaterial> element;
297
298 void _update_shader();
299 _FORCE_INLINE_ void _queue_shader_change();
300
301 Vector3 direction;
302 float spread = 0.0f;
303 float flatness = 0.0f;
304
305 float params_min[PARAM_MAX] = {};
306 float params_max[PARAM_MAX] = {};
307 float params[PARAM_MAX] = {};
308
309 Ref<Texture2D> tex_parameters[PARAM_MAX];
310 Color color;
311 Ref<Texture2D> color_ramp;
312 Ref<Texture2D> alpha_curve;
313 Ref<Texture2D> emission_curve;
314 Ref<Texture2D> color_initial_ramp;
315 Ref<Texture2D> velocity_limit_curve;
316
317 bool directional_velocity_global = false;
318 Vector3 velocity_pivot;
319
320 bool particle_flags[PARTICLE_FLAG_MAX];
321
322 EmissionShape emission_shape;
323 float emission_sphere_radius = 0.0f;
324 Vector3 emission_box_extents;
325 Ref<Texture2D> emission_point_texture;
326 Ref<Texture2D> emission_normal_texture;
327 Ref<Texture2D> emission_color_texture;
328 Vector3 emission_ring_axis;
329 real_t emission_ring_height = 0.0f;
330 real_t emission_ring_radius = 0.0f;
331 real_t emission_ring_inner_radius = 0.0f;
332 real_t emission_ring_cone_angle = 0.0f;
333 int emission_point_count = 1;
334 Vector3 emission_shape_offset;
335 Vector3 emission_shape_scale;
336
337 bool turbulence_enabled;
338 Vector3 turbulence_noise_speed;
339 Ref<Texture2D> turbulence_color_ramp;
340 float turbulence_noise_strength = 0.0f;
341 float turbulence_noise_scale = 0.0f;
342 float turbulence_noise_speed_random = 0.0f;
343
344 Vector3 gravity;
345
346 double lifetime_randomness = 0.0;
347 double inherit_emitter_velocity_ratio = 0.0;
348
349 SubEmitterMode sub_emitter_mode;
350 double sub_emitter_frequency = 0.0;
351 int sub_emitter_amount_at_end = 0;
352 int sub_emitter_amount_at_collision = 0;
353 int sub_emitter_amount_at_start = 0;
354 bool sub_emitter_keep_velocity = false;
355 //do not save emission points here
356
357 bool attractor_interaction_enabled = false;
358 CollisionMode collision_mode;
359 bool collision_scale = false;
360 float collision_friction = 0.0f;
361 float collision_bounce = 0.0f;
362
363 protected:
364 static void _bind_methods();
365 void _validate_property(PropertyInfo &p_property) const;
366
367 public:
368 static bool has_min_max_property(const String &p_name);
369
370 void set_direction(Vector3 p_direction);
371 Vector3 get_direction() const;
372
373 void set_spread(float p_spread);
374 float get_spread() const;
375
376 void set_flatness(float p_flatness);
377 float get_flatness() const;
378
379 void set_velocity_pivot(const Vector3 &p_pivot);
380 Vector3 get_velocity_pivot();
381
382 void set_param(Parameter p_param, const Vector2 &p_value);
383 Vector2 get_param(Parameter p_param) const;
384
385 void set_param_min(Parameter p_param, float p_value);
386 float get_param_min(Parameter p_param) const;
387
388 void set_param_max(Parameter p_param, float p_value);
389 float get_param_max(Parameter p_param) const;
390
391 void set_param_texture(Parameter p_param, const Ref<Texture2D> &p_texture);
392 Ref<Texture2D> get_param_texture(Parameter p_param) const;
393
394 void set_velocity_limit_curve(const Ref<Texture2D> &p_texture);
395 Ref<Texture2D> get_velocity_limit_curve() const;
396
397 void set_alpha_curve(const Ref<Texture2D> &p_texture);
398 Ref<Texture2D> get_alpha_curve() const;
399 void set_color(const Color &p_color);
400 Color get_color() const;
401
402 void set_color_ramp(const Ref<Texture2D> &p_texture);
403 Ref<Texture2D> get_color_ramp() const;
404
405 void set_color_initial_ramp(const Ref<Texture2D> &p_texture);
406 Ref<Texture2D> get_color_initial_ramp() const;
407
408 void set_emission_curve(const Ref<Texture2D> &p_texture);
409 Ref<Texture2D> get_emission_curve() const;
410
411 void set_particle_flag(ParticleFlags p_particle_flag, bool p_enable);
412 bool get_particle_flag(ParticleFlags p_particle_flag) const;
413
414 void set_emission_shape(EmissionShape p_shape);
415 void set_emission_sphere_radius(real_t p_radius);
416 void set_emission_box_extents(Vector3 p_extents);
417 void set_emission_point_texture(const Ref<Texture2D> &p_points);
418 void set_emission_normal_texture(const Ref<Texture2D> &p_normals);
419 void set_emission_color_texture(const Ref<Texture2D> &p_colors);
420 void set_emission_ring_axis(Vector3 p_axis);
421 void set_emission_ring_height(real_t p_height);
422 void set_emission_ring_radius(real_t p_radius);
423 void set_emission_ring_inner_radius(real_t p_radius);
424 void set_emission_ring_cone_angle(real_t p_angle);
425 void set_emission_point_count(int p_count);
426
427 EmissionShape get_emission_shape() const;
428 real_t get_emission_sphere_radius() const;
429 Vector3 get_emission_box_extents() const;
430 Ref<Texture2D> get_emission_point_texture() const;
431 Ref<Texture2D> get_emission_normal_texture() const;
432 Ref<Texture2D> get_emission_color_texture() const;
433 Vector3 get_emission_ring_axis() const;
434 real_t get_emission_ring_height() const;
435 real_t get_emission_ring_radius() const;
436 real_t get_emission_ring_inner_radius() const;
437 real_t get_emission_ring_cone_angle() const;
438 int get_emission_point_count() const;
439
440 void set_turbulence_enabled(bool p_turbulence_enabled);
441 void set_turbulence_noise_strength(float p_turbulence_noise_strength);
442 void set_turbulence_noise_scale(float p_turbulence_noise_scale);
443 void set_turbulence_noise_speed_random(float p_turbulence_noise_speed_random);
444 void set_turbulence_noise_speed(const Vector3 &p_turbulence_noise_speed);
445
446 bool get_turbulence_enabled() const;
447 float get_turbulence_noise_strength() const;
448 float get_turbulence_noise_scale() const;
449 float get_turbulence_noise_speed_random() const;
450 Vector3 get_turbulence_noise_speed() const;
451
452 void set_gravity(const Vector3 &p_gravity);
453 Vector3 get_gravity() const;
454
455 void set_lifetime_randomness(double p_lifetime);
456 double get_lifetime_randomness() const;
457
458 void set_inherit_velocity_ratio(double p_ratio);
459 double get_inherit_velocity_ratio();
460
461 void set_attractor_interaction_enabled(bool p_enable);
462 bool is_attractor_interaction_enabled() const;
463
464 void set_collision_mode(CollisionMode p_collision_mode);
465 CollisionMode get_collision_mode() const;
466
467 void set_collision_use_scale(bool p_scale);
468 bool is_collision_using_scale() const;
469
470 void set_collision_friction(float p_friction);
471 float get_collision_friction() const;
472
473 void set_collision_bounce(float p_bounce);
474 float get_collision_bounce() const;
475
476 static void init_shaders();
477 static void finish_shaders();
478 static void flush_changes();
479
480 void set_sub_emitter_mode(SubEmitterMode p_sub_emitter_mode);
481 SubEmitterMode get_sub_emitter_mode() const;
482
483 void set_sub_emitter_frequency(double p_frequency);
484 double get_sub_emitter_frequency() const;
485
486 void set_sub_emitter_amount_at_end(int p_amount);
487 int get_sub_emitter_amount_at_end() const;
488
489 void set_sub_emitter_amount_at_collision(int p_amount);
490 int get_sub_emitter_amount_at_collision() const;
491
492 void set_sub_emitter_amount_at_start(int p_amount);
493 int get_sub_emitter_amount_at_start() const;
494
495 void set_sub_emitter_keep_velocity(bool p_enable);
496 bool get_sub_emitter_keep_velocity() const;
497
498 void set_emission_shape_offset(const Vector3 &p_emission_shape_offset);
499 Vector3 get_emission_shape_offset() const;
500
501 void set_emission_shape_scale(const Vector3 &p_emission_shape_scale);
502 Vector3 get_emission_shape_scale() const;
503
504 virtual RID get_rid() const override;
505 virtual RID get_shader_rid() const override;
506
507 virtual Shader::Mode get_shader_mode() const override;
508
509 ParticleProcessMaterial();
510 ~ParticleProcessMaterial();
511 };
512
513 VARIANT_ENUM_CAST(ParticleProcessMaterial::Parameter)
514 VARIANT_ENUM_CAST(ParticleProcessMaterial::ParticleFlags)
515 VARIANT_ENUM_CAST(ParticleProcessMaterial::EmissionShape)
516 VARIANT_ENUM_CAST(ParticleProcessMaterial::SubEmitterMode)
517 VARIANT_ENUM_CAST(ParticleProcessMaterial::CollisionMode)