2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* sprite_3d.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
#include "sprite_3d.h"
|
2020-02-21 22:26:13 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/core_string_names.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/scene_string_names.h"
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
Color SpriteBase3D::_get_color_accum() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!color_dirty) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return color_accum;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (parent_sprite) {
|
2017-03-05 15:44:50 +00:00
|
|
|
color_accum = parent_sprite->_get_color_accum();
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
color_accum = Color(1, 1, 1, 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
color_accum.r *= modulate.r;
|
|
|
|
color_accum.g *= modulate.g;
|
|
|
|
color_accum.b *= modulate.b;
|
|
|
|
color_accum.a *= modulate.a;
|
|
|
|
color_dirty = false;
|
2014-05-29 13:56:39 +00:00
|
|
|
return color_accum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::_propagate_color_changed() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (color_dirty) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
color_dirty = true;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<SpriteBase3D *>::Element *E = children.front(); E; E = E->next()) {
|
2014-05-29 13:56:39 +00:00
|
|
|
E->get()->_propagate_color_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!pending_update) {
|
2014-05-29 13:56:39 +00:00
|
|
|
_im_update();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-08-24 20:58:51 +00:00
|
|
|
parent_sprite = Object::cast_to<SpriteBase3D>(get_parent());
|
|
|
|
if (parent_sprite) {
|
|
|
|
pI = parent_sprite->children.push_back(this);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_what == NOTIFICATION_EXIT_TREE) {
|
2014-05-29 13:56:39 +00:00
|
|
|
if (parent_sprite) {
|
|
|
|
parent_sprite->children.erase(pI);
|
2020-04-01 23:20:12 +00:00
|
|
|
pI = nullptr;
|
|
|
|
parent_sprite = nullptr;
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_centered(bool p_center) {
|
2017-03-05 15:44:50 +00:00
|
|
|
centered = p_center;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteBase3D::is_centered() const {
|
|
|
|
return centered;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpriteBase3D::set_offset(const Point2 &p_offset) {
|
|
|
|
offset = p_offset;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
Point2 SpriteBase3D::get_offset() const {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_flip_h(bool p_flip) {
|
2017-03-05 15:44:50 +00:00
|
|
|
hflip = p_flip;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
bool SpriteBase3D::is_flipped_h() const {
|
|
|
|
return hflip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_flip_v(bool p_flip) {
|
2017-03-05 15:44:50 +00:00
|
|
|
vflip = p_flip;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
bool SpriteBase3D::is_flipped_v() const {
|
|
|
|
return vflip;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpriteBase3D::set_modulate(const Color &p_color) {
|
|
|
|
modulate = p_color;
|
2014-05-29 13:56:39 +00:00
|
|
|
_propagate_color_changed();
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color SpriteBase3D::get_modulate() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
return modulate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_pixel_size(float p_amount) {
|
2017-03-05 15:44:50 +00:00
|
|
|
pixel_size = p_amount;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
float SpriteBase3D::get_pixel_size() const {
|
|
|
|
return pixel_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_opacity(float p_amount) {
|
2017-03-05 15:44:50 +00:00
|
|
|
opacity = p_amount;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
float SpriteBase3D::get_opacity() const {
|
|
|
|
return opacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::set_axis(Vector3::Axis p_axis) {
|
2019-06-24 12:53:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_axis, 3);
|
2017-03-05 15:44:50 +00:00
|
|
|
axis = p_axis;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
Vector3::Axis SpriteBase3D::get_axis() const {
|
|
|
|
return axis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBase3D::_im_update() {
|
|
|
|
_draw();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
pending_update = false;
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpriteBase3D::_queue_update() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (pending_update) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2018-05-06 18:49:22 +00:00
|
|
|
triangle_mesh.unref();
|
|
|
|
update_gizmo();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
pending_update = true;
|
2014-05-29 13:56:39 +00:00
|
|
|
call_deferred(SceneStringNames::get_singleton()->_im_update);
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB SpriteBase3D::get_aabb() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
return aabb;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Face3> SpriteBase3D::get_faces(uint32_t p_usage_flags) const {
|
|
|
|
return Vector<Face3>();
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 18:49:22 +00:00
|
|
|
Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (triangle_mesh.is_valid()) {
|
2018-05-06 18:49:22 +00:00
|
|
|
return triangle_mesh;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-06 18:49:22 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector3> faces;
|
2018-05-06 18:49:22 +00:00
|
|
|
faces.resize(6);
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector3 *facesw = faces.ptrw();
|
2018-05-06 18:49:22 +00:00
|
|
|
|
|
|
|
Rect2 final_rect = get_item_rect();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (final_rect.size.x == 0 || final_rect.size.y == 0) {
|
2018-05-06 18:49:22 +00:00
|
|
|
return Ref<TriangleMesh>();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-05-06 18:49:22 +00:00
|
|
|
|
|
|
|
float pixel_size = get_pixel_size();
|
|
|
|
|
|
|
|
Vector2 vertices[4] = {
|
|
|
|
(final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
|
|
|
|
(final_rect.position + final_rect.size) * pixel_size,
|
|
|
|
(final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
|
|
|
|
final_rect.position * pixel_size,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
int x_axis = ((axis + 1) % 3);
|
|
|
|
int y_axis = ((axis + 2) % 3);
|
|
|
|
|
|
|
|
if (axis != Vector3::AXIS_Z) {
|
|
|
|
SWAP(x_axis, y_axis);
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (axis == Vector3::AXIS_Y) {
|
|
|
|
vertices[i].y = -vertices[i].y;
|
|
|
|
} else if (axis == Vector3::AXIS_X) {
|
|
|
|
vertices[i].x = -vertices[i].x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int indices[6] = {
|
|
|
|
0, 1, 2,
|
|
|
|
0, 2, 3
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int j = 0; j < 6; j++) {
|
|
|
|
int i = indices[j];
|
|
|
|
Vector3 vtx;
|
|
|
|
vtx[x_axis] = vertices[i][0];
|
|
|
|
vtx[y_axis] = vertices[i][1];
|
|
|
|
facesw[j] = vtx;
|
|
|
|
}
|
|
|
|
|
|
|
|
triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
|
|
|
|
triangle_mesh->create(faces);
|
|
|
|
|
|
|
|
return triangle_mesh;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpriteBase3D::set_draw_flag(DrawFlags p_flag, bool p_enable) {
|
|
|
|
ERR_FAIL_INDEX(p_flag, FLAG_MAX);
|
|
|
|
flags[p_flag] = p_enable;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool SpriteBase3D::get_draw_flag(DrawFlags p_flag) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
|
2014-05-29 13:56:39 +00:00
|
|
|
return flags[p_flag];
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void SpriteBase3D::set_alpha_cut_mode(AlphaCutMode p_mode) {
|
|
|
|
ERR_FAIL_INDEX(p_mode, 3);
|
|
|
|
alpha_cut = p_mode;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
SpriteBase3D::AlphaCutMode SpriteBase3D::get_alpha_cut_mode() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
return alpha_cut;
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:01:52 +00:00
|
|
|
void SpriteBase3D::set_billboard_mode(StandardMaterial3D::BillboardMode p_mode) {
|
2019-07-16 23:04:36 +00:00
|
|
|
ERR_FAIL_INDEX(p_mode, 3);
|
|
|
|
billboard_mode = p_mode;
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:01:52 +00:00
|
|
|
StandardMaterial3D::BillboardMode SpriteBase3D::get_billboard_mode() const {
|
2019-07-16 23:04:36 +00:00
|
|
|
return billboard_mode;
|
|
|
|
}
|
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
void SpriteBase3D::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_opacity", "opacity"), &SpriteBase3D::set_opacity);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_opacity"), &SpriteBase3D::get_opacity);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-07-16 23:04:36 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpriteBase3D::set_billboard_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpriteBase3D::get_billboard_mode);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect);
|
2018-05-06 18:49:22 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 18:20:53 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "opacity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_opacity", "get_opacity");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis");
|
|
|
|
ADD_GROUP("Flags", "");
|
2019-07-16 23:04:36 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT);
|
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
|
2017-04-17 01:32:51 +00:00
|
|
|
ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass"), "set_alpha_cut_mode", "get_alpha_cut_mode");
|
2014-12-07 05:04:20 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_SHADED);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
|
|
|
|
BIND_ENUM_CONSTANT(FLAG_MAX);
|
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
|
|
|
|
BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
|
|
|
|
BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SpriteBase3D::SpriteBase3D() {
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < FLAG_MAX; i++) {
|
2017-04-17 01:32:51 +00:00
|
|
|
flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
immediate = RenderingServer::get_singleton()->immediate_create();
|
2014-05-29 13:56:39 +00:00
|
|
|
set_base(immediate);
|
|
|
|
}
|
|
|
|
|
|
|
|
SpriteBase3D::~SpriteBase3D() {
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->free(immediate);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
|
|
|
void Sprite3D::_draw() {
|
|
|
|
RID immediate = get_immediate();
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_clear(immediate);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!texture.is_valid()) {
|
2019-02-04 22:19:00 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
Vector2 tsize = texture->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (tsize.x == 0 || tsize.y == 0) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-02-04 22:19:00 +00:00
|
|
|
Rect2 base_rect;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (region) {
|
2019-02-04 22:19:00 +00:00
|
|
|
base_rect = region_rect;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2019-02-04 22:19:00 +00:00
|
|
|
base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-02-04 22:19:00 +00:00
|
|
|
Size2 frame_size = base_rect.size / Size2(hframes, vframes);
|
|
|
|
Point2 frame_offset = Point2(frame % hframes, frame / hframes);
|
|
|
|
frame_offset *= frame_size;
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-02-04 22:19:00 +00:00
|
|
|
Point2 dest_offset = get_offset();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_centered()) {
|
2019-02-04 22:19:00 +00:00
|
|
|
dest_offset -= frame_size / 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-02-04 22:19:00 +00:00
|
|
|
Rect2 src_rect(base_rect.position + frame_offset, frame_size);
|
|
|
|
Rect2 final_dst_rect(dest_offset, frame_size);
|
2014-05-29 13:56:39 +00:00
|
|
|
Rect2 final_rect;
|
|
|
|
Rect2 final_src_rect;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!texture->get_rect_region(final_dst_rect, src_rect, final_rect, final_src_rect)) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (final_rect.size.x == 0 || final_rect.size.y == 0) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color color = _get_color_accum();
|
|
|
|
color.a *= get_opacity();
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float pixel_size = get_pixel_size();
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 vertices[4] = {
|
2017-06-03 22:25:13 +00:00
|
|
|
(final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
|
|
|
|
(final_rect.position + final_rect.size) * pixel_size,
|
|
|
|
(final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
|
|
|
|
final_rect.position * pixel_size,
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
};
|
2018-05-07 20:05:28 +00:00
|
|
|
|
2018-08-27 19:24:56 +00:00
|
|
|
Vector2 src_tsize = tsize;
|
2018-05-07 20:05:28 +00:00
|
|
|
|
|
|
|
// Properly setup UVs for impostor textures (AtlasTexture).
|
|
|
|
Ref<AtlasTexture> atlas_tex = texture;
|
2020-04-01 23:20:12 +00:00
|
|
|
if (atlas_tex != nullptr) {
|
2018-05-07 20:05:28 +00:00
|
|
|
src_tsize[0] = atlas_tex->get_atlas()->get_width();
|
|
|
|
src_tsize[1] = atlas_tex->get_atlas()->get_height();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 uvs[4] = {
|
2018-08-27 19:24:56 +00:00
|
|
|
final_src_rect.position / src_tsize,
|
|
|
|
(final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
|
|
|
|
(final_src_rect.position + final_src_rect.size) / src_tsize,
|
|
|
|
(final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
|
2014-05-29 13:56:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (is_flipped_h()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(uvs[0], uvs[1]);
|
|
|
|
SWAP(uvs[2], uvs[3]);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
if (is_flipped_v()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(uvs[0], uvs[3]);
|
|
|
|
SWAP(uvs[1], uvs[2]);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 normal;
|
|
|
|
int axis = get_axis();
|
2017-03-05 15:44:50 +00:00
|
|
|
normal[axis] = 1.0;
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2018-11-17 01:11:35 +00:00
|
|
|
Plane tangent;
|
|
|
|
if (axis == Vector3::AXIS_X) {
|
2018-12-08 02:43:46 +00:00
|
|
|
tangent = Plane(0, 0, -1, 1);
|
2018-11-17 01:11:35 +00:00
|
|
|
} else {
|
2018-12-08 02:43:46 +00:00
|
|
|
tangent = Plane(1, 0, 0, 1);
|
2018-11-17 01:11:35 +00:00
|
|
|
}
|
|
|
|
|
2019-09-15 04:01:52 +00:00
|
|
|
RID mat = StandardMaterial3D::get_material_rid_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS, get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_set_material(immediate, mat);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_begin(immediate, RS::PRIMITIVE_TRIANGLES, texture->get_rid());
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
int x_axis = ((axis + 1) % 3);
|
|
|
|
int y_axis = ((axis + 2) % 3);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (axis != Vector3::AXIS_Z) {
|
|
|
|
SWAP(x_axis, y_axis);
|
2014-09-21 04:43:42 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-09-21 04:43:42 +00:00
|
|
|
//uvs[i] = Vector2(1.0,1.0)-uvs[i];
|
|
|
|
//SWAP(vertices[i].x,vertices[i].y);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (axis == Vector3::AXIS_Y) {
|
|
|
|
vertices[i].y = -vertices[i].y;
|
|
|
|
} else if (axis == Vector3::AXIS_X) {
|
|
|
|
vertices[i].x = -vertices[i].x;
|
2014-09-21 04:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB aabb;
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-06-16 02:45:24 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
static const int index[6] = { 0, 1, 2, 0, 2, 3 };
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_normal(immediate, normal);
|
|
|
|
RS::get_singleton()->immediate_tangent(immediate, tangent);
|
|
|
|
RS::get_singleton()->immediate_color(immediate, color);
|
2020-12-08 18:58:49 +00:00
|
|
|
RS::get_singleton()->immediate_uv(immediate, uvs[index[i]]);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
Vector3 vtx;
|
2019-06-16 02:45:24 +00:00
|
|
|
vtx[x_axis] = vertices[index[i]][0];
|
|
|
|
vtx[y_axis] = vertices[index[i]][1];
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_vertex(immediate, vtx);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i == 0) {
|
2017-06-06 18:33:51 +00:00
|
|
|
aabb.position = vtx;
|
2017-03-05 15:44:50 +00:00
|
|
|
aabb.size = Vector3();
|
2014-05-29 13:56:39 +00:00
|
|
|
} else {
|
|
|
|
aabb.expand_to(vtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_aabb(aabb);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_end(immediate);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 21:49:16 +00:00
|
|
|
void Sprite3D::_texture_changed() {
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
void Sprite3D::set_texture(const Ref<Texture2D> &p_texture) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_texture == texture) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-02-27 21:49:16 +00:00
|
|
|
texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Sprite3D::_texture_changed));
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
texture = p_texture;
|
2014-05-29 13:56:39 +00:00
|
|
|
if (texture.is_valid()) {
|
2020-02-27 21:49:16 +00:00
|
|
|
texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Sprite3D::_texture_changed));
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> Sprite3D::get_texture() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::set_region(bool p_region) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_region == region) {
|
2014-05-29 13:56:39 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
region = p_region;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Sprite3D::is_region() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
|
|
|
|
bool changed = region_rect != p_region_rect;
|
|
|
|
region_rect = p_region_rect;
|
2014-05-29 13:56:39 +00:00
|
|
|
if (region && changed) {
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 Sprite3D::get_region_rect() const {
|
|
|
|
return region_rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::set_frame(int p_frame) {
|
2019-07-20 06:09:57 +00:00
|
|
|
ERR_FAIL_INDEX(p_frame, int64_t(vframes) * hframes);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-10-14 09:40:55 +00:00
|
|
|
frame = p_frame;
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2014-06-11 13:41:03 +00:00
|
|
|
_queue_update();
|
2019-01-25 18:21:23 +00:00
|
|
|
|
2015-01-02 16:08:28 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->frame_changed);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Sprite3D::get_frame() const {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2019-01-25 18:21:23 +00:00
|
|
|
void Sprite3D::set_frame_coords(const Vector2 &p_coord) {
|
2019-10-13 06:44:44 +00:00
|
|
|
ERR_FAIL_INDEX(int(p_coord.x), hframes);
|
|
|
|
ERR_FAIL_INDEX(int(p_coord.y), vframes);
|
2019-01-25 18:21:23 +00:00
|
|
|
|
|
|
|
set_frame(int(p_coord.y) * hframes + int(p_coord.x));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Sprite3D::get_frame_coords() const {
|
|
|
|
return Vector2(frame % hframes, frame / hframes);
|
|
|
|
}
|
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
void Sprite3D::set_vframes(int p_amount) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_amount < 1);
|
|
|
|
vframes = p_amount;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
int Sprite3D::get_vframes() const {
|
|
|
|
return vframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sprite3D::set_hframes(int p_amount) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_amount < 1);
|
|
|
|
hframes = p_amount;
|
2014-05-29 13:56:39 +00:00
|
|
|
_queue_update();
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
int Sprite3D::get_hframes() const {
|
|
|
|
return hframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 Sprite3D::get_item_rect() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (texture.is_null()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return Rect2(0, 0, 1, 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
if (texture.is_null())
|
|
|
|
return CanvasItem::get_item_rect();
|
|
|
|
*/
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
Size2i s;
|
|
|
|
|
|
|
|
if (region) {
|
2017-03-05 15:44:50 +00:00
|
|
|
s = region_rect.size;
|
2014-05-29 13:56:39 +00:00
|
|
|
} else {
|
|
|
|
s = texture->get_size();
|
2017-03-05 15:44:50 +00:00
|
|
|
s = s / Point2(hframes, vframes);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 09:50:21 +00:00
|
|
|
Point2 ofs = get_offset();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_centered()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs -= s / 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (s == Size2(0, 0)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
s = Size2(1, 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Rect2(ofs, s);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Sprite3D::_validate_property(PropertyInfo &property) const {
|
|
|
|
if (property.name == "frame") {
|
2019-07-25 07:11:41 +00:00
|
|
|
property.hint = PROPERTY_HINT_RANGE;
|
2017-03-05 15:44:50 +00:00
|
|
|
property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
|
2019-07-25 07:11:41 +00:00
|
|
|
property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
|
2016-07-07 02:46:04 +00:00
|
|
|
}
|
2019-10-22 17:01:23 +00:00
|
|
|
|
|
|
|
if (property.name == "frame_coords") {
|
|
|
|
property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
|
|
|
|
}
|
2016-07-07 02:46:04 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 13:56:39 +00:00
|
|
|
void Sprite3D::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_region", "enabled"), &Sprite3D::set_region);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_region"), &Sprite3D::is_region);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-01-25 18:21:23 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
|
2017-06-25 21:57:28 +00:00
|
|
|
ADD_GROUP("Animation", "");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
|
2020-08-21 23:56:14 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
|
2019-07-25 07:11:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
|
2019-01-25 18:21:23 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords");
|
2017-06-25 21:57:28 +00:00
|
|
|
ADD_GROUP("Region", "region_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2014-12-07 05:04:20 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("frame_changed"));
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Sprite3D::Sprite3D() {
|
2017-03-05 15:44:50 +00:00
|
|
|
region = false;
|
|
|
|
frame = 0;
|
|
|
|
vframes = 1;
|
|
|
|
hframes = 1;
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2016-06-08 21:03:06 +00:00
|
|
|
void AnimatedSprite3D::_draw() {
|
|
|
|
RID immediate = get_immediate();
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_clear(immediate);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
if (frames.is_null()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (frame < 0) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frames->has_animation(animation)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> texture = frames->get_frame(animation, frame);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!texture.is_valid()) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return; //no texuture no life
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
Vector2 tsize = texture->get_size();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (tsize.x == 0 || tsize.y == 0) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Size2i s = tsize;
|
2018-10-26 09:50:21 +00:00
|
|
|
Rect2 src_rect;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
src_rect.size = s;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2018-10-26 09:50:21 +00:00
|
|
|
Point2 ofs = get_offset();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (is_centered()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs -= s / 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2018-10-26 09:50:21 +00:00
|
|
|
Rect2 dst_rect(ofs, s);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
Rect2 final_rect;
|
|
|
|
Rect2 final_src_rect;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect)) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (final_rect.size.x == 0 || final_rect.size.y == 0) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color color = _get_color_accum();
|
|
|
|
color.a *= get_opacity();
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float pixel_size = get_pixel_size();
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 vertices[4] = {
|
2017-06-03 22:25:13 +00:00
|
|
|
(final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
|
|
|
|
(final_rect.position + final_rect.size) * pixel_size,
|
|
|
|
(final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
|
|
|
|
final_rect.position * pixel_size,
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
};
|
2018-05-07 20:05:28 +00:00
|
|
|
|
2018-08-27 19:24:56 +00:00
|
|
|
Vector2 src_tsize = tsize;
|
2018-05-07 20:05:28 +00:00
|
|
|
|
|
|
|
// Properly setup UVs for impostor textures (AtlasTexture).
|
|
|
|
Ref<AtlasTexture> atlas_tex = texture;
|
2020-04-01 23:20:12 +00:00
|
|
|
if (atlas_tex != nullptr) {
|
2018-05-07 20:05:28 +00:00
|
|
|
src_tsize[0] = atlas_tex->get_atlas()->get_width();
|
|
|
|
src_tsize[1] = atlas_tex->get_atlas()->get_height();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 uvs[4] = {
|
2018-08-27 19:24:56 +00:00
|
|
|
final_src_rect.position / src_tsize,
|
|
|
|
(final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
|
|
|
|
(final_src_rect.position + final_src_rect.size) / src_tsize,
|
|
|
|
(final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
|
2016-06-08 21:03:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (is_flipped_h()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(uvs[0], uvs[1]);
|
|
|
|
SWAP(uvs[2], uvs[3]);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
if (is_flipped_v()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(uvs[0], uvs[3]);
|
|
|
|
SWAP(uvs[1], uvs[2]);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 normal;
|
|
|
|
int axis = get_axis();
|
2017-03-05 15:44:50 +00:00
|
|
|
normal[axis] = 1.0;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2018-11-17 01:11:35 +00:00
|
|
|
Plane tangent;
|
|
|
|
if (axis == Vector3::AXIS_X) {
|
|
|
|
tangent = Plane(0, 0, -1, -1);
|
|
|
|
} else {
|
|
|
|
tangent = Plane(1, 0, 0, -1);
|
|
|
|
}
|
|
|
|
|
2019-09-15 04:01:52 +00:00
|
|
|
RID mat = StandardMaterial3D::get_material_rid_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS, get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y);
|
2017-08-08 20:23:44 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_set_material(immediate, mat);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_begin(immediate, RS::PRIMITIVE_TRIANGLES, texture->get_rid());
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
int x_axis = ((axis + 1) % 3);
|
|
|
|
int y_axis = ((axis + 2) % 3);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (axis != Vector3::AXIS_Z) {
|
|
|
|
SWAP(x_axis, y_axis);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2016-06-08 21:03:06 +00:00
|
|
|
//uvs[i] = Vector2(1.0,1.0)-uvs[i];
|
|
|
|
//SWAP(vertices[i].x,vertices[i].y);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (axis == Vector3::AXIS_Y) {
|
|
|
|
vertices[i].y = -vertices[i].y;
|
|
|
|
} else if (axis == Vector3::AXIS_X) {
|
|
|
|
vertices[i].x = -vertices[i].x;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:00 +00:00
|
|
|
AABB aabb;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2019-06-16 02:45:24 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
static const int indices[6] = {
|
|
|
|
0, 1, 2,
|
|
|
|
0, 2, 3
|
|
|
|
};
|
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_normal(immediate, normal);
|
|
|
|
RS::get_singleton()->immediate_tangent(immediate, tangent);
|
|
|
|
RS::get_singleton()->immediate_color(immediate, color);
|
2020-12-08 18:58:49 +00:00
|
|
|
RS::get_singleton()->immediate_uv(immediate, uvs[indices[i]]);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
Vector3 vtx;
|
2019-06-16 02:45:24 +00:00
|
|
|
vtx[x_axis] = vertices[indices[i]][0];
|
|
|
|
vtx[y_axis] = vertices[indices[i]][1];
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_vertex(immediate, vtx);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i == 0) {
|
2017-06-06 18:33:51 +00:00
|
|
|
aabb.position = vtx;
|
2017-03-05 15:44:50 +00:00
|
|
|
aabb.size = Vector3();
|
2016-06-08 21:03:06 +00:00
|
|
|
} else {
|
|
|
|
aabb.expand_to(vtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_aabb(aabb);
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_end(immediate);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimatedSprite3D::_validate_property(PropertyInfo &property) const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!frames.is_valid()) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
if (property.name == "animation") {
|
|
|
|
property.hint = PROPERTY_HINT_ENUM;
|
2016-06-08 21:03:06 +00:00
|
|
|
List<StringName> names;
|
|
|
|
frames->get_animation_list(&names);
|
|
|
|
names.sort_custom<StringName::AlphCompare>();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool current_found = false;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
|
2016-06-08 21:03:06 +00:00
|
|
|
if (E->prev()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
property.hint_string += ",";
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
property.hint_string += String(E->get());
|
|
|
|
if (animation == E->get()) {
|
|
|
|
current_found = true;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_found) {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (property.hint_string == String()) {
|
|
|
|
property.hint_string = String(animation);
|
2016-06-08 21:03:06 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
property.hint_string = String(animation) + "," + property.hint_string;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (property.name == "frame") {
|
|
|
|
property.hint = PROPERTY_HINT_RANGE;
|
2019-07-25 07:11:41 +00:00
|
|
|
if (frames->has_animation(animation) && frames->get_frame_count(animation) > 1) {
|
2017-03-05 15:44:50 +00:00
|
|
|
property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
2019-07-25 07:11:41 +00:00
|
|
|
property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2017-01-10 21:02:19 +00:00
|
|
|
case NOTIFICATION_INTERNAL_PROCESS: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (frames.is_null()) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (!frames->has_animation(animation)) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (frame < 0) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
float speed = frames->get_animation_speed(animation);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (speed == 0) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return; //do nothing
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
float remaining = get_process_delta_time();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (remaining) {
|
|
|
|
if (timeout <= 0) {
|
|
|
|
timeout = 1.0 / speed;
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
int fc = frames->get_frame_count(animation);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (frame >= fc - 1) {
|
2016-06-08 21:03:06 +00:00
|
|
|
if (frames->get_animation_loop(animation)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
frame = 0;
|
2016-06-08 21:03:06 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
frame = fc - 1;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
frame++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float to_process = MIN(timeout, remaining);
|
|
|
|
remaining -= to_process;
|
|
|
|
timeout -= to_process;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (frames.is_valid()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
frames->disconnect("changed", callable_mp(this, &AnimatedSprite3D::_res_changed));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
frames = p_frames;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (frames.is_valid()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
frames->connect("changed", callable_mp(this, &AnimatedSprite3D::_res_changed));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
if (!frames.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
frame = 0;
|
2016-06-08 21:03:06 +00:00
|
|
|
} else {
|
|
|
|
set_frame(frame);
|
|
|
|
}
|
|
|
|
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2016-06-08 21:03:06 +00:00
|
|
|
_reset_timeout();
|
|
|
|
_queue_update();
|
|
|
|
update_configuration_warning();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::set_frame(int p_frame) {
|
|
|
|
if (!frames.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frames->has_animation(animation)) {
|
|
|
|
int limit = frames->get_frame_count(animation);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_frame >= limit) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_frame = limit - 1;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_frame < 0) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p_frame = 0;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (frame == p_frame) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
frame = p_frame;
|
2016-06-08 21:03:06 +00:00
|
|
|
_reset_timeout();
|
2017-01-14 17:03:38 +00:00
|
|
|
_queue_update();
|
2021-02-10 20:18:45 +00:00
|
|
|
|
2016-06-08 21:03:06 +00:00
|
|
|
emit_signal(SceneStringNames::get_singleton()->frame_changed);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2016-06-08 21:03:06 +00:00
|
|
|
int AnimatedSprite3D::get_frame() const {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 AnimatedSprite3D::get_item_rect() const {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
|
|
|
|
return Rect2(0, 0, 1, 1);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 18:43:37 +00:00
|
|
|
Ref<Texture2D> t;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (animation) {
|
2017-03-05 15:44:50 +00:00
|
|
|
t = frames->get_frame(animation, frame);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (t.is_null()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return Rect2(0, 0, 1, 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
Size2i s = t->get_size();
|
|
|
|
|
2018-05-06 18:49:22 +00:00
|
|
|
Point2 ofs = get_offset();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (centered) {
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs -= s / 2;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (s == Size2(0, 0)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
s = Size2(1, 1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Rect2(ofs, s);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::_res_changed() {
|
|
|
|
set_frame(frame);
|
|
|
|
_queue_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::_set_playing(bool p_playing) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (playing == p_playing) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
playing = p_playing;
|
2016-06-08 21:03:06 +00:00
|
|
|
_reset_timeout();
|
2017-01-10 21:02:19 +00:00
|
|
|
set_process_internal(playing);
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimatedSprite3D::_is_playing() const {
|
|
|
|
return playing;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimatedSprite3D::play(const StringName &p_animation) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p_animation) {
|
2016-06-08 21:03:06 +00:00
|
|
|
set_animation(p_animation);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
_set_playing(true);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimatedSprite3D::stop() {
|
2016-06-08 21:03:06 +00:00
|
|
|
_set_playing(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AnimatedSprite3D::is_playing() const {
|
|
|
|
return is_processing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::_reset_timeout() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!playing) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
if (frames.is_valid() && frames->has_animation(animation)) {
|
|
|
|
float speed = frames->get_animation_speed(animation);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (speed > 0) {
|
|
|
|
timeout = 1.0 / speed;
|
2016-06-08 21:03:06 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
timeout = 0;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
timeout = 0;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimatedSprite3D::set_animation(const StringName &p_animation) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (animation == p_animation) {
|
2016-06-08 21:03:06 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
animation = p_animation;
|
2016-06-08 21:03:06 +00:00
|
|
|
_reset_timeout();
|
|
|
|
set_frame(0);
|
2021-02-10 20:18:45 +00:00
|
|
|
notify_property_list_changed();
|
2017-01-14 17:03:38 +00:00
|
|
|
_queue_update();
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
StringName AnimatedSprite3D::get_animation() const {
|
2016-06-08 21:03:06 +00:00
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
String AnimatedSprite3D::get_configuration_warning() const {
|
2020-05-14 20:59:27 +00:00
|
|
|
String warning = SpriteBase3D::get_configuration_warning();
|
|
|
|
|
2016-06-08 21:03:06 +00:00
|
|
|
if (frames.is_null()) {
|
2020-12-15 12:04:21 +00:00
|
|
|
if (!warning.is_empty()) {
|
2020-05-14 20:59:27 +00:00
|
|
|
warning += "\n\n";
|
|
|
|
}
|
|
|
|
warning += TTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite3D to display frames.");
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 20:59:27 +00:00
|
|
|
return warning;
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimatedSprite3D::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite3D::set_animation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite3D::_set_playing);
|
|
|
|
ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite3D::_is_playing);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
|
|
|
|
ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
|
2016-06-08 21:03:06 +00:00
|
|
|
|
|
|
|
ADD_SIGNAL(MethodInfo("frame_changed"));
|
|
|
|
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
|
2020-02-20 21:58:05 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
|
2019-07-25 07:11:41 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
|
2016-06-08 21:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimatedSprite3D::AnimatedSprite3D() {
|
|
|
|
}
|