Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add a flag to preserve color in particle process.

Qbieshay85d7cdb8592f
diff --git a/doc/classes/GPUParticles2D.xml b/doc/classes/GPUParticles2D.xml
index 45d90eda4ab..75ef3041fb9 100644
--- a/doc/classes/GPUParticles2D.xml
+++ b/doc/classes/GPUParticles2D.xml
@@ -37,7 +37,8 @@
<param index="4" name="flags" type="int" />
<description>
Emits a single particle. Whether [param xform], [param velocity], [param color] and [param custom] are applied depends on the value of [param flags]. See [enum EmitFlags].
- The default ParticleProcessMaterial will overwrite [param color] and use the contents of [param custom] as [code](rotation, age, animation, lifetime)[/code].
+ The default ParticleProcessMaterial needs to have [member ParticleProcessMaterial.particle_flag_preserve_color] set to [code]true[/code] to correctly use the color given from this function.
+ Additionally, it will use the contents of [param custom] as [code](rotation, lifetime, animation, lifetime randomness)[/code].
[b]Note:[/b] [method emit_particle] is only supported on the Forward+ and Mobile rendering methods, not Compatibility.
</description>
</method>
diff --git a/doc/classes/GPUParticles3D.xml b/doc/classes/GPUParticles3D.xml
index fcc5f06f3e6..5231eabc827 100644
--- a/doc/classes/GPUParticles3D.xml
+++ b/doc/classes/GPUParticles3D.xml
@@ -35,7 +35,8 @@
<param index="4" name="flags" type="int" />
<description>
Emits a single particle. Whether [param xform], [param velocity], [param color] and [param custom] are applied depends on the value of [param flags]. See [enum EmitFlags].
- The default ParticleProcessMaterial will overwrite [param color] and use the contents of [param custom] as [code](rotation, age, animation, lifetime)[/code].
+ The default ParticleProcessMaterial needs to have [member ParticleProcessMaterial.particle_flag_preserve_color] set to [code]true[/code] to correctly use the color given from this function.
+ Additionally, it will use the contents of [param custom] as [code](rotation, lifetime, animation, lifetime randomness)[/code].
[b]Note:[/b] [method emit_particle] is only supported on the Forward+ and Mobile rendering methods, not Compatibility.
</description>
</method>
diff --git a/doc/classes/ParticleProcessMaterial.xml b/doc/classes/ParticleProcessMaterial.xml
index d0faf0af27c..e2f510bf0f7 100644
--- a/doc/classes/ParticleProcessMaterial.xml
+++ b/doc/classes/ParticleProcessMaterial.xml
@@ -295,6 +295,9 @@
If [code]true[/code], particles will inherit the scale of the emitter.
[b]Note:[/b] This has no effect when [member GPUParticles3D.local_coords] is [code]true[/code], since particles in local space are already affected by the emitter's scale.
</member>
+ <member name="particle_flag_preserve_color" type="bool" setter="set_particle_flag" getter="get_particle_flag" default="false">
+ If [code]true[/code], preserves the color given by manual emission. To use when using [method GPUParticles3D.emit_particle] or when working with subemitters.
+ </member>
<member name="particle_flag_rotate_y" type="bool" setter="set_particle_flag" getter="get_particle_flag" default="false">
If [code]true[/code], particles rotate around Y axis by [member angle_min].
</member>
@@ -517,10 +520,15 @@
Use with [method set_particle_flag] to set [member particle_flag_disable_z].
</constant>
<constant name="PARTICLE_FLAG_DAMPING_AS_FRICTION" value="3" enum="ParticleFlags">
+ Use with [method set_particle_flag] to set [member particle_flag_damping_as_friction].
</constant>
<constant name="PARTICLE_FLAG_INHERIT_EMITTER_SCALE" value="4" enum="ParticleFlags">
+ Use with [method set_particle_flag] to set [member particle_flag_inherit_emitter_scale].
+ </constant>
+ <constant name="PARTICLE_FLAG_PRESERVE_COLOR" value="5" enum="ParticleFlags">
+ Use with [method set_particle_flag] to set [member particle_flag_preserve_color].
</constant>
- <constant name="PARTICLE_FLAG_MAX" value="5" enum="ParticleFlags">
+ <constant name="PARTICLE_FLAG_MAX" value="6" enum="ParticleFlags">
Represents the size of the [enum ParticleFlags] enum.
</constant>
<constant name="EMISSION_SHAPE_POINT" value="0" enum="EmissionShape">
diff --git a/scene/resources/particle_process_material.cpp b/scene/resources/particle_process_material.cpp
index 8c0dbb521e9..4a59f95b8ef 100644
--- a/scene/resources/particle_process_material.cpp
+++ b/scene/resources/particle_process_material.cpp
@@ -635,7 +635,7 @@ void ParticleProcessMaterial::_update_shader() {
}
code += "}\n\n";
- code += "void calculate_initial_display_params(inout DisplayParameters params, inout uint alt_seed) {\n";
+ code += "void calculate_initial_display_params(inout DisplayParameters params, inout uint alt_seed, vec4 color_multiplier) {\n";
code += " // -------------------- DO NOT REORDER OPERATIONS, IT BREAKS VISUAL COMPATIBILITY\n";
code += " // -------------------- ADD NEW OPERATIONS AT THE BOTTOM\n";
code += " float pi = 3.14159;\n";
@@ -649,7 +649,8 @@ void ParticleProcessMaterial::_update_shader() {
code += " params.animation_speed = mix(anim_speed_min, anim_speed_max, rand_from_seed(alt_seed));\n";
code += " params.animation_offset = mix(anim_offset_min, anim_offset_max, rand_from_seed(alt_seed));\n";
code += " params.lifetime = (1.0 - lifetime_randomness * rand_from_seed(alt_seed));\n";
- code += " params.color = color_value;\n";
+ code += " params.color = color_value * color_multiplier;\n";
+
if (color_initial_ramp.is_valid()) {
code += " params.color *= texture(color_initial_ramp, vec2(rand_from_seed(alt_seed)));\n";
}
@@ -913,7 +914,12 @@ void ParticleProcessMaterial::_update_shader() {
code += " uint base_number = NUMBER;\n";
code += " uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
code += " DisplayParameters params;\n";
- code += " calculate_initial_display_params(params, alt_seed);\n";
+ if (particle_flags[PARTICLE_FLAG_PRESERVE_COLOR]) {
+ code += " USERDATA2 = COLOR;\n";
+ code += " calculate_initial_display_params(params, alt_seed, USERDATA2);\n";
+ } else {
+ code += " calculate_initial_display_params(params, alt_seed, vec4(1.0));\n";
+ }
code += " // Reset alt seed?\n";
code += " //alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
code += " DynamicsParameters dynamic_params;\n";
@@ -932,9 +938,7 @@ void ParticleProcessMaterial::_update_shader() {
// Accumulated angle from angular velocity is stored in userdata W
code += " USERDATA1.w = 0.0;\n";
code += " }\n";
- code += " if (RESTART_COLOR) {\n";
- code += " COLOR = params.color;\n";
- code += " }\n";
+ code += " COLOR = params.color;\n";
code += " if (RESTART_ROT_SCALE) {\n";
code += " TRANSFORM[0].xyz = vec3(1.0, 0.0, 0.0);\n";
code += " TRANSFORM[1].xyz = vec3(0.0, 1.0, 0.0);\n";
@@ -1022,7 +1026,11 @@ void ParticleProcessMaterial::_update_shader() {
code += " //}\n";
code += " uint alt_seed = hash(base_number + uint(1) + RANDOM_SEED);\n";
code += " DisplayParameters params;\n";
- code += " calculate_initial_display_params(params, alt_seed);\n";
+ if (particle_flags[PARTICLE_FLAG_PRESERVE_COLOR]) {
+ code += " calculate_initial_display_params(params, alt_seed, USERDATA2);\n";
+ } else {
+ code += " calculate_initial_display_params(params, alt_seed, vec4(1.0));\n";
+ }
code += " DynamicsParameters dynamic_params;\n";
code += " calculate_initial_dynamics_params(dynamic_params, alt_seed);\n";
code += " PhysicalParameters physics_params;\n";
@@ -2571,6 +2579,7 @@ void ParticleProcessMaterial::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_disable_z"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DISABLE_Z);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_damping_as_friction"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_DAMPING_AS_FRICTION);
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_inherit_emitter_scale"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_INHERIT_EMITTER_SCALE);
+ ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particle_flag_preserve_color"), "set_particle_flag", "get_particle_flag", PARTICLE_FLAG_PRESERVE_COLOR);
ADD_GROUP("Spawn", "");
ADD_SUBGROUP("Position", "");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "emission_shape_offset"), "set_emission_shape_offset", "get_emission_shape_offset");
@@ -2714,6 +2723,7 @@ void ParticleProcessMaterial::_bind_methods() {
BIND_ENUM_CONSTANT(PARTICLE_FLAG_DISABLE_Z);
BIND_ENUM_CONSTANT(PARTICLE_FLAG_DAMPING_AS_FRICTION);
BIND_ENUM_CONSTANT(PARTICLE_FLAG_INHERIT_EMITTER_SCALE);
+ BIND_ENUM_CONSTANT(PARTICLE_FLAG_PRESERVE_COLOR);
BIND_ENUM_CONSTANT(PARTICLE_FLAG_MAX);
BIND_ENUM_CONSTANT(EMISSION_SHAPE_POINT);
diff --git a/scene/resources/particle_process_material.h b/scene/resources/particle_process_material.h
index 1dc04f947e3..f6460168f57 100644
--- a/scene/resources/particle_process_material.h
+++ b/scene/resources/particle_process_material.h
@@ -76,6 +76,7 @@ public:
PARTICLE_FLAG_DISABLE_Z,
PARTICLE_FLAG_DAMPING_AS_FRICTION,
PARTICLE_FLAG_INHERIT_EMITTER_SCALE,
+ PARTICLE_FLAG_PRESERVE_COLOR,
PARTICLE_FLAG_MAX
};