summaryrefslogtreecommitdiff
path: root/scene/3d/sprite_3d.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/3d/sprite_3d.cpp')
-rw-r--r--scene/3d/sprite_3d.cpp137
1 files changed, 68 insertions, 69 deletions
diff --git a/scene/3d/sprite_3d.cpp b/scene/3d/sprite_3d.cpp
index be6eab2178..71d76c0d1c 100644
--- a/scene/3d/sprite_3d.cpp
+++ b/scene/3d/sprite_3d.cpp
@@ -1,32 +1,32 @@
-/*************************************************************************/
-/* sprite_3d.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
+/**************************************************************************/
+/* sprite_3d.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
#include "sprite_3d.h"
@@ -191,9 +191,7 @@ void SpriteBase3D::draw_texture_rect(Ref<Texture2D> p_texture, Rect2 p_dst_rect,
uint32_t v_normal;
{
- Vector3 n = normal * Vector3(0.5, 0.5, 0.5) + Vector3(0.5, 0.5, 0.5);
-
- Vector2 res = n.octahedron_encode();
+ Vector2 res = normal.octahedron_encode();
uint32_t value = 0;
value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535);
value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16;
@@ -613,6 +611,7 @@ SpriteBase3D::SpriteBase3D() {
}
SpriteBase3D::~SpriteBase3D() {
+ ERR_FAIL_NULL(RenderingServer::get_singleton());
RenderingServer::get_singleton()->free(mesh);
RenderingServer::get_singleton()->free(material);
}
@@ -680,6 +679,7 @@ void Sprite3D::set_region_enabled(bool p_region) {
region = p_region;
_queue_redraw();
+ notify_property_list_changed();
}
bool Sprite3D::is_region_enabled() const {
@@ -781,6 +781,10 @@ void Sprite3D::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "frame_coords") {
p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
}
+
+ if (!region && (p_property.name == "region_rect")) {
+ p_property.usage = PROPERTY_USAGE_NO_EDITOR;
+ }
}
void Sprite3D::_bind_methods() {
@@ -833,7 +837,7 @@ void AnimatedSprite3D::_draw() {
return;
}
- Ref<Texture2D> texture = frames->get_frame(animation, frame);
+ Ref<Texture2D> texture = frames->get_frame_texture(animation, frame);
if (texture.is_null()) {
set_base(RID());
return;
@@ -917,17 +921,21 @@ void AnimatedSprite3D::_notification(int p_what) {
return;
}
- double speed = frames->get_animation_speed(animation) * Math::abs(speed_scale);
- if (speed == 0) {
- return; // Do nothing.
- }
- int last_frame = frames->get_frame_count(animation) - 1;
-
double remaining = get_process_delta_time();
+ int i = 0;
while (remaining) {
- if (timeout <= 0) {
- timeout = _get_frame_duration();
+ // Animation speed may be changed by animation_finished or frame_changed signals.
+ double speed = frames->get_animation_speed(animation) * Math::abs(speed_scale);
+
+ if (speed == 0) {
+ return; // Do nothing.
+ }
+
+ // Frame count may be changed by animation_finished or frame_changed signals.
+ int fc = frames->get_frame_count(animation);
+ if (timeout <= 0) {
+ int last_frame = fc - 1;
if (!playing_backwards) {
// Forward.
if (frame >= last_frame) {
@@ -962,14 +970,21 @@ void AnimatedSprite3D::_notification(int p_what) {
}
}
+ timeout = _get_frame_duration();
+
_queue_redraw();
emit_signal(SceneStringNames::get_singleton()->frame_changed);
}
- double to_process = MIN(timeout, remaining);
+ double to_process = MIN(timeout / speed, remaining);
+ timeout -= to_process * speed;
remaining -= to_process;
- timeout -= to_process;
+
+ i++;
+ if (i > fc) {
+ return; // Prevents freezing if to_process is each time much less than remaining.
+ }
}
} break;
}
@@ -1024,7 +1039,6 @@ void AnimatedSprite3D::set_frame(int p_frame) {
frame = p_frame;
_reset_timeout();
_queue_redraw();
-
emit_signal(SceneStringNames::get_singleton()->frame_changed);
}
@@ -1032,22 +1046,12 @@ int AnimatedSprite3D::get_frame() const {
return frame;
}
-void AnimatedSprite3D::set_speed_scale(double p_speed_scale) {
- if (speed_scale == p_speed_scale) {
- return;
- }
-
- double elapsed = _get_frame_duration() - timeout;
-
+void AnimatedSprite3D::set_speed_scale(float p_speed_scale) {
speed_scale = p_speed_scale;
playing_backwards = signbit(speed_scale) != backwards;
-
- // We adapt the timeout so that the animation speed adapts as soon as the speed scale is changed.
- _reset_timeout();
- timeout -= elapsed;
}
-double AnimatedSprite3D::get_speed_scale() const {
+float AnimatedSprite3D::get_speed_scale() const {
return speed_scale;
}
@@ -1061,7 +1065,7 @@ Rect2 AnimatedSprite3D::get_item_rect() const {
Ref<Texture2D> t;
if (animation) {
- t = frames->get_frame(animation, frame);
+ t = frames->get_frame_texture(animation, frame);
}
if (t.is_null()) {
return Rect2(0, 0, 1, 1);
@@ -1082,8 +1086,8 @@ Rect2 AnimatedSprite3D::get_item_rect() const {
void AnimatedSprite3D::_res_changed() {
set_frame(frame);
-
_queue_redraw();
+ notify_property_list_changed();
}
void AnimatedSprite3D::set_playing(bool p_playing) {
@@ -1091,7 +1095,7 @@ void AnimatedSprite3D::set_playing(bool p_playing) {
return;
}
playing = p_playing;
- _reset_timeout();
+ playing_backwards = signbit(speed_scale) != backwards;
set_process_internal(playing);
notify_property_list_changed();
}
@@ -1117,23 +1121,18 @@ void AnimatedSprite3D::play(const StringName &p_animation, bool p_backwards) {
void AnimatedSprite3D::stop() {
set_playing(false);
+ backwards = false;
+ _reset_timeout();
}
double AnimatedSprite3D::_get_frame_duration() {
if (frames.is_valid() && frames->has_animation(animation)) {
- double speed = frames->get_animation_speed(animation) * Math::abs(speed_scale);
- if (speed > 0) {
- return 1.0 / speed;
- }
+ return frames->get_frame_duration(animation, frame);
}
return 0.0;
}
void AnimatedSprite3D::_reset_timeout() {
- if (!playing) {
- return;
- }
-
timeout = _get_frame_duration();
is_over = false;
}
@@ -1141,13 +1140,14 @@ void AnimatedSprite3D::_reset_timeout() {
void AnimatedSprite3D::set_animation(const StringName &p_animation) {
ERR_FAIL_COND_MSG(frames == nullptr, vformat("There is no animation with name '%s'.", p_animation));
ERR_FAIL_COND_MSG(!frames->get_animation_names().has(p_animation), vformat("There is no animation with name '%s'.", p_animation));
+
if (animation == p_animation) {
return;
}
animation = p_animation;
- _reset_timeout();
set_frame(0);
+ _reset_timeout();
notify_property_list_changed();
_queue_redraw();
}
@@ -1161,7 +1161,6 @@ PackedStringArray AnimatedSprite3D::get_configuration_warnings() const {
if (frames.is_null()) {
warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite3D to display frames."));
}
-
return warnings;
}