Skip to content

armgit / godot

Public Imported from GitHub
0

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

Separate particles animation from particles bilboard

kobewi88dbf5a5843e
diff --git a/doc/classes/BaseMaterial3D.xml b/doc/classes/BaseMaterial3D.xml
index c255382f74d..75d4b2832cc 100644
--- a/doc/classes/BaseMaterial3D.xml
+++ b/doc/classes/BaseMaterial3D.xml
@@ -315,14 +315,21 @@
<member name="orm_texture" type="Texture2D" setter="set_texture" getter="get_texture">
The Occlusion/Roughness/Metallic texture to use. This is a more efficient replacement of [member ao_texture], [member roughness_texture] and [member metallic_texture] in [ORMMaterial3D]. Ambient occlusion is stored in the red channel. Roughness map is stored in the green channel. Metallic map is stored in the blue channel. The alpha channel is ignored.
</member>
- <member name="particles_anim_h_frames" type="int" setter="set_particles_anim_h_frames" getter="get_particles_anim_h_frames">
- The number of horizontal frames in the particle sprite sheet. Only enabled when using [constant BILLBOARD_PARTICLES]. See [member billboard_mode].
+ <member name="particles_anim_enabled" type="bool" setter="set_feature" getter="get_feature" default="false">
+ If [code]true[/code], enables [code]particles_anim_*[/code] properties. The [member ParticleProcessMaterial.anim_speed_min] or [member CPUParticles3D.anim_speed_min] should also be set to a value bigger than zero for the animation to play.
+ [b]Note:[/b] For compatibility reasons, this property is ignored and always enabled when [member billboard_mode] is set to [constant BILLBOARD_PARTICLES].
</member>
- <member name="particles_anim_loop" type="bool" setter="set_particles_anim_loop" getter="get_particles_anim_loop">
- If [code]true[/code], particle animations are looped. Only enabled when using [constant BILLBOARD_PARTICLES]. See [member billboard_mode].
+ <member name="particles_anim_h_frames" type="int" setter="set_particles_anim_h_frames" getter="get_particles_anim_h_frames" default="1">
+ The number of horizontal frames in the particle sprite sheet.
+ Only enabled when [member particles_anim_enabled] is [code]true[/code] or [member billboard_mode] is set to [constant BILLBOARD_PARTICLES].
</member>
- <member name="particles_anim_v_frames" type="int" setter="set_particles_anim_v_frames" getter="get_particles_anim_v_frames">
- The number of vertical frames in the particle sprite sheet. Only enabled when using [constant BILLBOARD_PARTICLES]. See [member billboard_mode].
+ <member name="particles_anim_loop" type="bool" setter="set_particles_anim_loop" getter="get_particles_anim_loop" default="false">
+ If [code]true[/code], particle animations are looped.
+ Only enabled when [member particles_anim_enabled] is [code]true[/code] or [member billboard_mode] is set to [constant BILLBOARD_PARTICLES].
+ </member>
+ <member name="particles_anim_v_frames" type="int" setter="set_particles_anim_v_frames" getter="get_particles_anim_v_frames" default="1">
+ The number of vertical frames in the particle sprite sheet.
+ Only enabled when [member particles_anim_enabled] is [code]true[/code] or [member billboard_mode] is set to [constant BILLBOARD_PARTICLES].
</member>
<member name="point_size" type="float" setter="set_point_size" getter="get_point_size" default="1.0">
The point size in pixels. See [member use_point_size].
@@ -647,7 +654,10 @@
<constant name="FEATURE_BENT_NORMAL_MAPPING" value="12" enum="Feature">
Constant for setting [member bent_normal_enabled].
</constant>
- <constant name="FEATURE_MAX" value="13" enum="Feature">
+ <constant name="FEATURE_PARTICLES_ANIMATION" value="13" enum="Feature">
+ Constant for setting [member particles_anim_enabled].
+ </constant>
+ <constant name="FEATURE_MAX" value="14" enum="Feature">
Represents the size of the [enum Feature] enum.
</constant>
<constant name="BLEND_MODE_MIX" value="0" enum="BlendMode">
@@ -813,6 +823,7 @@
<constant name="BILLBOARD_PARTICLES" value="3" enum="BillboardMode">
Used for particle systems when assigned to [GPUParticles3D] and [CPUParticles3D] nodes (flipbook animation). Enables [code]particles_anim_*[/code] properties.
The [member ParticleProcessMaterial.anim_speed_min] or [member CPUParticles3D.anim_speed_min] should also be set to a value bigger than zero for the animation to play.
+ For animations without bilboarding, see [member particles_anim_enabled].
</constant>
<constant name="TEXTURE_CHANNEL_RED" value="0" enum="TextureChannel">
Used to read from the red channel of a texture.
diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp
index 2de6e7e84f7..f2abee77150 100644
--- a/scene/resources/material.cpp
+++ b/scene/resources/material.cpp
@@ -36,6 +36,7 @@
#include "core/io/resource_loader.h"
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
+#include "core/object/property_info.h"
#include "core/os/os.h"
#include "core/version.h"
#include "scene/main/scene_tree.h"
@@ -736,6 +737,11 @@ void BaseMaterial3D::_update_shader() {
texfilter_height_str += ", repeat_disable";
}
+ bool animation_enabled = features[FEATURE_PARTICLES_ANIMATION];
+#ifndef DISABLE_DEPRECATED
+ animation_enabled = animation_enabled || billboard_mode == BILLBOARD_PARTICLES;
+#endif
+
// Add a comment to describe the shader origin (useful when converting to ShaderMaterial).
String code = vformat(
"// NOTE: Shader automatically converted from " GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG "'s %s.\n\n",
@@ -1024,7 +1030,7 @@ uniform float metallic : hint_range(0.0, 1.0, 0.01);
code += "uniform sampler2D texture_orm : hint_roughness_g, " + texfilter_str + ";\n";
}
- if (billboard_mode == BILLBOARD_PARTICLES) {
+ if (animation_enabled) {
code += R"(
uniform int particles_anim_h_frames : hint_range(1, 128);
uniform int particles_anim_v_frames : hint_range(1, 128);
@@ -1304,27 +1310,32 @@ void vertex() {)";
vec4(0.0, 0.0, 0.0, 1.0));
)";
}
- // Set modelview normal and handle animation.
+ // Set modelview normal.
code += R"(
MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);
-
- float h_frames = float(particles_anim_h_frames);
- float v_frames = float(particles_anim_v_frames);
- float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);
- float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));
- if (!particles_anim_loop) {
- particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);
- } else {
- particle_frame = mod(particle_frame, particle_total_frames);
- }
- UV /= vec2(h_frames, v_frames);
- UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);
)";
} break;
case BILLBOARD_MAX:
break; // Internal value, skip.
}
+ // Handle animation.
+ if (animation_enabled) {
+ code += R"(
+ float h_frames = float(particles_anim_h_frames);
+ float v_frames = float(particles_anim_v_frames);
+ float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);
+ float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));
+ if (!particles_anim_loop) {
+ particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);
+ } else {
+ particle_frame = mod(particle_frame, particle_total_frames);
+ }
+ UV /= vec2(h_frames, v_frames);
+ UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);
+)";
+ }
+
if (flags[FLAG_FIXED_SIZE]) {
code += R"(
// Fixed Size: Enabled
@@ -2549,9 +2560,16 @@ void BaseMaterial3D::_validate_property(PropertyInfo &p_property) const {
p_property.usage = PROPERTY_USAGE_NONE;
}
- if (p_property.name.begins_with("particles_anim_") && billboard_mode != BILLBOARD_PARTICLES) {
- p_property.usage = PROPERTY_USAGE_NONE;
+#ifndef DISABLE_DEPRECATED
+ if (billboard_mode == BILLBOARD_PARTICLES) {
+ if (p_property.name == "particles_anim_enabled") {
+ p_property.hint = PROPERTY_HINT_NONE;
+ p_property.usage = PROPERTY_USAGE_NONE;
+ } else if (p_property.name.begins_with("particles_anim_")) {
+ p_property.usage = PROPERTY_USAGE_DEFAULT;
+ }
}
+#endif
if (Engine::get_singleton()->is_editor_hint()) {
if (p_property.name == "billboard_keep_scale" && billboard_mode == BILLBOARD_DISABLED) {
@@ -3712,6 +3730,7 @@ void BaseMaterial3D::_bind_methods() {
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "billboard_keep_scale"), "set_flag", "get_flag", FLAG_BILLBOARD_KEEP_SCALE);
ADD_GROUP("Particles Anim", "particles_anim_");
+ ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "particles_anim_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_feature", "get_feature", FEATURE_PARTICLES_ANIMATION);
ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_h_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_h_frames", "get_particles_anim_h_frames");
ADD_PROPERTY(PropertyInfo(Variant::INT, "particles_anim_v_frames", PROPERTY_HINT_RANGE, "1,128,1"), "set_particles_anim_v_frames", "get_particles_anim_v_frames");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "particles_anim_loop"), "set_particles_anim_loop", "get_particles_anim_loop");
@@ -3809,6 +3828,7 @@ void BaseMaterial3D::_bind_methods() {
BIND_ENUM_CONSTANT(FEATURE_REFRACTION);
BIND_ENUM_CONSTANT(FEATURE_DETAIL);
BIND_ENUM_CONSTANT(FEATURE_BENT_NORMAL_MAPPING);
+ BIND_ENUM_CONSTANT(FEATURE_PARTICLES_ANIMATION);
BIND_ENUM_CONSTANT(FEATURE_MAX);
BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
diff --git a/scene/resources/material.h b/scene/resources/material.h
index 2397824f6f1..130332c1cb8 100644
--- a/scene/resources/material.h
+++ b/scene/resources/material.h
@@ -220,6 +220,7 @@ public:
FEATURE_REFRACTION,
FEATURE_DETAIL,
FEATURE_BENT_NORMAL_MAPPING,
+ FEATURE_PARTICLES_ANIMATION,
FEATURE_MAX
};