sprite_3d.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /*************************************************************************/
  2. /* sprite_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "sprite_3d.h"
  31. #include "core/core_string_names.h"
  32. #include "scene/scene_string_names.h"
  33. Color SpriteBase3D::_get_color_accum() {
  34. if (!color_dirty) {
  35. return color_accum;
  36. }
  37. if (parent_sprite) {
  38. color_accum = parent_sprite->_get_color_accum();
  39. } else {
  40. color_accum = Color(1, 1, 1, 1);
  41. }
  42. color_accum.r *= modulate.r;
  43. color_accum.g *= modulate.g;
  44. color_accum.b *= modulate.b;
  45. color_accum.a *= modulate.a;
  46. color_dirty = false;
  47. return color_accum;
  48. }
  49. void SpriteBase3D::_propagate_color_changed() {
  50. if (color_dirty) {
  51. return;
  52. }
  53. color_dirty = true;
  54. _queue_update();
  55. for (List<SpriteBase3D *>::Element *E = children.front(); E; E = E->next()) {
  56. E->get()->_propagate_color_changed();
  57. }
  58. }
  59. void SpriteBase3D::_notification(int p_what) {
  60. if (p_what == NOTIFICATION_ENTER_TREE) {
  61. if (!pending_update) {
  62. _im_update();
  63. }
  64. parent_sprite = Object::cast_to<SpriteBase3D>(get_parent());
  65. if (parent_sprite) {
  66. pI = parent_sprite->children.push_back(this);
  67. }
  68. }
  69. if (p_what == NOTIFICATION_EXIT_TREE) {
  70. if (parent_sprite) {
  71. parent_sprite->children.erase(pI);
  72. pI = nullptr;
  73. parent_sprite = nullptr;
  74. }
  75. }
  76. }
  77. void SpriteBase3D::set_centered(bool p_center) {
  78. centered = p_center;
  79. _queue_update();
  80. }
  81. bool SpriteBase3D::is_centered() const {
  82. return centered;
  83. }
  84. void SpriteBase3D::set_offset(const Point2 &p_offset) {
  85. offset = p_offset;
  86. _queue_update();
  87. }
  88. Point2 SpriteBase3D::get_offset() const {
  89. return offset;
  90. }
  91. void SpriteBase3D::set_flip_h(bool p_flip) {
  92. hflip = p_flip;
  93. _queue_update();
  94. }
  95. bool SpriteBase3D::is_flipped_h() const {
  96. return hflip;
  97. }
  98. void SpriteBase3D::set_flip_v(bool p_flip) {
  99. vflip = p_flip;
  100. _queue_update();
  101. }
  102. bool SpriteBase3D::is_flipped_v() const {
  103. return vflip;
  104. }
  105. void SpriteBase3D::set_modulate(const Color &p_color) {
  106. modulate = p_color;
  107. _propagate_color_changed();
  108. _queue_update();
  109. }
  110. Color SpriteBase3D::get_modulate() const {
  111. return modulate;
  112. }
  113. void SpriteBase3D::set_pixel_size(float p_amount) {
  114. pixel_size = p_amount;
  115. _queue_update();
  116. }
  117. float SpriteBase3D::get_pixel_size() const {
  118. return pixel_size;
  119. }
  120. void SpriteBase3D::set_opacity(float p_amount) {
  121. opacity = p_amount;
  122. _queue_update();
  123. }
  124. float SpriteBase3D::get_opacity() const {
  125. return opacity;
  126. }
  127. void SpriteBase3D::set_axis(Vector3::Axis p_axis) {
  128. ERR_FAIL_INDEX(p_axis, 3);
  129. axis = p_axis;
  130. _queue_update();
  131. }
  132. Vector3::Axis SpriteBase3D::get_axis() const {
  133. return axis;
  134. }
  135. void SpriteBase3D::_im_update() {
  136. _draw();
  137. pending_update = false;
  138. //texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  139. }
  140. void SpriteBase3D::_queue_update() {
  141. if (pending_update) {
  142. return;
  143. }
  144. triangle_mesh.unref();
  145. update_gizmo();
  146. pending_update = true;
  147. call_deferred(SceneStringNames::get_singleton()->_im_update);
  148. }
  149. AABB SpriteBase3D::get_aabb() const {
  150. return aabb;
  151. }
  152. PoolVector<Face3> SpriteBase3D::get_faces(uint32_t p_usage_flags) const {
  153. return PoolVector<Face3>();
  154. }
  155. Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const {
  156. if (triangle_mesh.is_valid()) {
  157. return triangle_mesh;
  158. }
  159. PoolVector<Vector3> faces;
  160. faces.resize(6);
  161. PoolVector<Vector3>::Write facesw = faces.write();
  162. Rect2 final_rect = get_item_rect();
  163. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  164. return Ref<TriangleMesh>();
  165. }
  166. float pixel_size = get_pixel_size();
  167. Vector2 vertices[4] = {
  168. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  169. (final_rect.position + final_rect.size) * pixel_size,
  170. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  171. final_rect.position * pixel_size,
  172. };
  173. int x_axis = ((axis + 1) % 3);
  174. int y_axis = ((axis + 2) % 3);
  175. if (axis != Vector3::AXIS_Z) {
  176. SWAP(x_axis, y_axis);
  177. for (int i = 0; i < 4; i++) {
  178. if (axis == Vector3::AXIS_Y) {
  179. vertices[i].y = -vertices[i].y;
  180. } else if (axis == Vector3::AXIS_X) {
  181. vertices[i].x = -vertices[i].x;
  182. }
  183. }
  184. }
  185. static const int indices[6] = {
  186. 0, 1, 2,
  187. 0, 2, 3
  188. };
  189. for (int j = 0; j < 6; j++) {
  190. int i = indices[j];
  191. Vector3 vtx;
  192. vtx[x_axis] = vertices[i][0];
  193. vtx[y_axis] = vertices[i][1];
  194. facesw[j] = vtx;
  195. }
  196. facesw.release();
  197. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  198. triangle_mesh->create(faces);
  199. return triangle_mesh;
  200. }
  201. void SpriteBase3D::set_draw_flag(DrawFlags p_flag, bool p_enable) {
  202. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  203. flags[p_flag] = p_enable;
  204. _queue_update();
  205. }
  206. bool SpriteBase3D::get_draw_flag(DrawFlags p_flag) const {
  207. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  208. return flags[p_flag];
  209. }
  210. void SpriteBase3D::set_alpha_cut_mode(AlphaCutMode p_mode) {
  211. ERR_FAIL_INDEX(p_mode, 3);
  212. alpha_cut = p_mode;
  213. _queue_update();
  214. }
  215. SpriteBase3D::AlphaCutMode SpriteBase3D::get_alpha_cut_mode() const {
  216. return alpha_cut;
  217. }
  218. void SpriteBase3D::set_billboard_mode(SpatialMaterial::BillboardMode p_mode) {
  219. ERR_FAIL_INDEX(p_mode, 3);
  220. billboard_mode = p_mode;
  221. _queue_update();
  222. }
  223. SpatialMaterial::BillboardMode SpriteBase3D::get_billboard_mode() const {
  224. return billboard_mode;
  225. }
  226. void SpriteBase3D::_bind_methods() {
  227. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered);
  228. ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered);
  229. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset);
  230. ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset);
  231. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h);
  232. ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h);
  233. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v);
  234. ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v);
  235. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate);
  236. ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate);
  237. ClassDB::bind_method(D_METHOD("set_opacity", "opacity"), &SpriteBase3D::set_opacity);
  238. ClassDB::bind_method(D_METHOD("get_opacity"), &SpriteBase3D::get_opacity);
  239. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size);
  240. ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size);
  241. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis);
  242. ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis);
  243. ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag);
  244. ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag);
  245. ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode);
  246. ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode);
  247. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpriteBase3D::set_billboard_mode);
  248. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpriteBase3D::get_billboard_mode);
  249. ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect);
  250. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh);
  251. ClassDB::bind_method(D_METHOD("_queue_update"), &SpriteBase3D::_queue_update);
  252. ClassDB::bind_method(D_METHOD("_im_update"), &SpriteBase3D::_im_update);
  253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  254. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  257. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  258. ADD_PROPERTY(PropertyInfo(Variant::REAL, "opacity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_opacity", "get_opacity");
  259. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
  260. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis");
  261. ADD_GROUP("Flags", "");
  262. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode");
  263. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT);
  264. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
  265. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
  266. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass"), "set_alpha_cut_mode", "get_alpha_cut_mode");
  267. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  268. BIND_ENUM_CONSTANT(FLAG_SHADED);
  269. BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
  270. BIND_ENUM_CONSTANT(FLAG_MAX);
  271. BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
  272. BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
  273. BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
  274. }
  275. SpriteBase3D::SpriteBase3D() {
  276. color_dirty = true;
  277. centered = true;
  278. hflip = false;
  279. vflip = false;
  280. parent_sprite = nullptr;
  281. pI = nullptr;
  282. for (int i = 0; i < FLAG_MAX; i++) {
  283. flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED;
  284. }
  285. alpha_cut = ALPHA_CUT_DISABLED;
  286. billboard_mode = SpatialMaterial::BILLBOARD_DISABLED;
  287. axis = Vector3::AXIS_Z;
  288. pixel_size = 0.01;
  289. modulate = Color(1, 1, 1, 1);
  290. pending_update = false;
  291. opacity = 1.0;
  292. material = VisualServer::get_singleton()->material_create();
  293. // Set defaults for material, names need to match up those in SpatialMaterial
  294. VS::get_singleton()->material_set_param(material, "albedo", Color(1, 1, 1, 1));
  295. VS::get_singleton()->material_set_param(material, "specular", 0.5);
  296. VS::get_singleton()->material_set_param(material, "metallic", 0.0);
  297. VS::get_singleton()->material_set_param(material, "roughness", 1.0);
  298. VS::get_singleton()->material_set_param(material, "uv1_offset", Vector3(0, 0, 0));
  299. VS::get_singleton()->material_set_param(material, "uv1_scale", Vector3(1, 1, 1));
  300. VS::get_singleton()->material_set_param(material, "uv2_offset", Vector3(0, 0, 0));
  301. VS::get_singleton()->material_set_param(material, "uv2_scale", Vector3(1, 1, 1));
  302. VS::get_singleton()->material_set_param(material, "alpha_scissor_threshold", 0.98);
  303. mesh = VisualServer::get_singleton()->mesh_create();
  304. PoolVector3Array mesh_vertices;
  305. PoolVector3Array mesh_normals;
  306. PoolRealArray mesh_tangents;
  307. PoolColorArray mesh_colors;
  308. PoolVector2Array mesh_uvs;
  309. mesh_vertices.resize(4);
  310. mesh_normals.resize(4);
  311. mesh_tangents.resize(16);
  312. mesh_colors.resize(4);
  313. mesh_uvs.resize(4);
  314. // create basic mesh and store format information
  315. for (int i = 0; i < 4; i++) {
  316. mesh_normals.write()[i] = Vector3(0.0, 0.0, 1.0);
  317. mesh_tangents.write()[i * 4 + 0] = 0.0;
  318. mesh_tangents.write()[i * 4 + 1] = 0.0;
  319. mesh_tangents.write()[i * 4 + 2] = 1.0;
  320. mesh_tangents.write()[i * 4 + 3] = 1.0;
  321. mesh_colors.write()[i] = Color(1.0, 1.0, 1.0, 1.0);
  322. mesh_uvs.write()[i] = Vector2(0.0, 0.0);
  323. mesh_vertices.write()[i] = Vector3(0.0, 0.0, 0.0);
  324. }
  325. Array mesh_array;
  326. mesh_array.resize(VS::ARRAY_MAX);
  327. mesh_array[VS::ARRAY_VERTEX] = mesh_vertices;
  328. mesh_array[VS::ARRAY_NORMAL] = mesh_normals;
  329. mesh_array[VS::ARRAY_TANGENT] = mesh_tangents;
  330. mesh_array[VS::ARRAY_COLOR] = mesh_colors;
  331. mesh_array[VS::ARRAY_TEX_UV] = mesh_uvs;
  332. uint32_t compress_format = (VS::ARRAY_COMPRESS_DEFAULT & ~VS::ARRAY_COMPRESS_TEX_UV) & ~VS::ARRAY_COMPRESS_COLOR;
  333. compress_format |= VS::ARRAY_FLAG_USE_DYNAMIC_UPDATE;
  334. VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VS::PRIMITIVE_TRIANGLE_FAN, mesh_array, Array(), compress_format);
  335. const int surface_vertex_len = VS::get_singleton()->mesh_surface_get_array_len(mesh, 0);
  336. const int surface_index_len = VS::get_singleton()->mesh_surface_get_array_index_len(mesh, 0);
  337. mesh_surface_format = VS::get_singleton()->mesh_surface_get_format(mesh, 0);
  338. mesh_buffer = VS::get_singleton()->mesh_surface_get_array(mesh, 0);
  339. VS::get_singleton()->mesh_surface_make_offsets_from_format(mesh_surface_format, surface_vertex_len, surface_index_len, mesh_surface_offsets, mesh_stride);
  340. set_base(mesh);
  341. }
  342. SpriteBase3D::~SpriteBase3D() {
  343. VisualServer::get_singleton()->free(mesh);
  344. VisualServer::get_singleton()->free(material);
  345. }
  346. ///////////////////////////////////////////
  347. void Sprite3D::_draw() {
  348. if (get_base() != get_mesh()) {
  349. set_base(get_mesh());
  350. }
  351. if (!texture.is_valid()) {
  352. set_base(RID());
  353. return;
  354. }
  355. Vector2 tsize = texture->get_size();
  356. if (tsize.x == 0 || tsize.y == 0) {
  357. return;
  358. }
  359. Rect2 base_rect;
  360. if (region) {
  361. base_rect = region_rect;
  362. } else {
  363. base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
  364. }
  365. Size2 frame_size = base_rect.size / Size2(hframes, vframes);
  366. Point2 frame_offset = Point2(frame % hframes, frame / hframes);
  367. frame_offset *= frame_size;
  368. Point2 dest_offset = get_offset();
  369. if (is_centered()) {
  370. dest_offset -= frame_size / 2;
  371. }
  372. Rect2 src_rect(base_rect.position + frame_offset, frame_size);
  373. Rect2 final_dst_rect(dest_offset, frame_size);
  374. Rect2 final_rect;
  375. Rect2 final_src_rect;
  376. if (!texture->get_rect_region(final_dst_rect, src_rect, final_rect, final_src_rect)) {
  377. return;
  378. }
  379. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  380. return;
  381. }
  382. Color color = _get_color_accum();
  383. color.a *= get_opacity();
  384. float pixel_size = get_pixel_size();
  385. Vector2 vertices[4] = {
  386. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  387. (final_rect.position + final_rect.size) * pixel_size,
  388. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  389. final_rect.position * pixel_size,
  390. };
  391. Vector2 src_tsize = tsize;
  392. // Properly setup UVs for impostor textures (AtlasTexture).
  393. Ref<AtlasTexture> atlas_tex = texture;
  394. if (atlas_tex != nullptr) {
  395. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  396. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  397. }
  398. Vector2 uvs[4] = {
  399. final_src_rect.position / src_tsize,
  400. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  401. (final_src_rect.position + final_src_rect.size) / src_tsize,
  402. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  403. };
  404. if (is_flipped_h()) {
  405. SWAP(uvs[0], uvs[1]);
  406. SWAP(uvs[2], uvs[3]);
  407. }
  408. if (is_flipped_v()) {
  409. SWAP(uvs[0], uvs[3]);
  410. SWAP(uvs[1], uvs[2]);
  411. }
  412. Vector3 normal;
  413. int axis = get_axis();
  414. normal[axis] = 1.0;
  415. Plane tangent;
  416. if (axis == Vector3::AXIS_X) {
  417. tangent = Plane(0, 0, -1, 1);
  418. } else {
  419. tangent = Plane(1, 0, 0, 1);
  420. }
  421. int x_axis = ((axis + 1) % 3);
  422. int y_axis = ((axis + 2) % 3);
  423. if (axis != Vector3::AXIS_Z) {
  424. SWAP(x_axis, y_axis);
  425. for (int i = 0; i < 4; i++) {
  426. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  427. //SWAP(vertices[i].x,vertices[i].y);
  428. if (axis == Vector3::AXIS_Y) {
  429. vertices[i].y = -vertices[i].y;
  430. } else if (axis == Vector3::AXIS_X) {
  431. vertices[i].x = -vertices[i].x;
  432. }
  433. }
  434. }
  435. AABB aabb;
  436. // Everything except position, color, and UV is compressed
  437. PoolVector<uint8_t>::Write write_buffer = mesh_buffer.write();
  438. Vector2 normal_oct = VisualServer::get_singleton()->norm_to_oct(normal);
  439. int8_t v_normal[2] = {
  440. (int8_t)CLAMP(normal_oct.x * 127, -128, 127),
  441. (int8_t)CLAMP(normal_oct.y * 127, -128, 127),
  442. };
  443. Vector2 tangent_oct = VisualServer::get_singleton()->tangent_to_oct(tangent.normal, tangent.d, false);
  444. int8_t v_tangent[2] = {
  445. (int8_t)CLAMP(tangent_oct.x * 127, -128, 127),
  446. (int8_t)CLAMP(tangent_oct.y * 127, -128, 127),
  447. };
  448. for (int i = 0; i < 4; i++) {
  449. Vector3 vtx;
  450. vtx[x_axis] = vertices[i][0];
  451. vtx[y_axis] = vertices[i][1];
  452. if (i == 0) {
  453. aabb.position = vtx;
  454. aabb.size = Vector3();
  455. } else {
  456. aabb.expand_to(vtx);
  457. }
  458. float v_uv[2] = { uvs[i].x, uvs[i].y };
  459. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_TEX_UV] + mesh_surface_offsets[VS::ARRAY_TEX_UV]], v_uv, 8);
  460. float v_vertex[3] = { vtx.x, vtx.y, vtx.z };
  461. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_VERTEX] + mesh_surface_offsets[VS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
  462. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_NORMAL] + mesh_surface_offsets[VS::ARRAY_NORMAL]], v_normal, 2);
  463. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_TANGENT] + mesh_surface_offsets[VS::ARRAY_TANGENT]], v_tangent, 2);
  464. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_COLOR] + mesh_surface_offsets[VS::ARRAY_COLOR]], color.components, 4 * 4);
  465. }
  466. write_buffer.release();
  467. RID mesh = get_mesh();
  468. VS::get_singleton()->mesh_surface_update_region(mesh, 0, 0, mesh_buffer);
  469. VS::get_singleton()->mesh_set_custom_aabb(mesh, aabb);
  470. set_aabb(aabb);
  471. RID mat = SpatialMaterial::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() == SpatialMaterial::BILLBOARD_ENABLED, get_billboard_mode() == SpatialMaterial::BILLBOARD_FIXED_Y);
  472. VS::get_singleton()->material_set_shader(get_material(), VS::get_singleton()->material_get_shader(mat));
  473. VS::get_singleton()->material_set_param(get_material(), "texture_albedo", texture->get_rid());
  474. VS::get_singleton()->instance_set_surface_material(get_instance(), 0, get_material());
  475. }
  476. void Sprite3D::set_texture(const Ref<Texture> &p_texture) {
  477. if (p_texture == texture) {
  478. return;
  479. }
  480. if (texture.is_valid()) {
  481. texture->disconnect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  482. }
  483. texture = p_texture;
  484. if (texture.is_valid()) {
  485. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  486. texture->connect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  487. }
  488. _queue_update();
  489. }
  490. Ref<Texture> Sprite3D::get_texture() const {
  491. return texture;
  492. }
  493. void Sprite3D::set_region(bool p_region) {
  494. if (p_region == region) {
  495. return;
  496. }
  497. region = p_region;
  498. _queue_update();
  499. }
  500. bool Sprite3D::is_region() const {
  501. return region;
  502. }
  503. void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
  504. bool changed = region_rect != p_region_rect;
  505. region_rect = p_region_rect;
  506. if (region && changed) {
  507. _queue_update();
  508. }
  509. }
  510. Rect2 Sprite3D::get_region_rect() const {
  511. return region_rect;
  512. }
  513. void Sprite3D::set_frame(int p_frame) {
  514. ERR_FAIL_INDEX(p_frame, int64_t(vframes) * hframes);
  515. frame = p_frame;
  516. _queue_update();
  517. _change_notify("frame");
  518. _change_notify("frame_coords");
  519. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  520. }
  521. int Sprite3D::get_frame() const {
  522. return frame;
  523. }
  524. void Sprite3D::set_frame_coords(const Vector2 &p_coord) {
  525. ERR_FAIL_INDEX(int(p_coord.x), hframes);
  526. ERR_FAIL_INDEX(int(p_coord.y), vframes);
  527. set_frame(int(p_coord.y) * hframes + int(p_coord.x));
  528. }
  529. Vector2 Sprite3D::get_frame_coords() const {
  530. return Vector2(frame % hframes, frame / hframes);
  531. }
  532. void Sprite3D::set_vframes(int p_amount) {
  533. ERR_FAIL_COND(p_amount < 1);
  534. vframes = p_amount;
  535. _queue_update();
  536. _change_notify();
  537. }
  538. int Sprite3D::get_vframes() const {
  539. return vframes;
  540. }
  541. void Sprite3D::set_hframes(int p_amount) {
  542. ERR_FAIL_COND(p_amount < 1);
  543. hframes = p_amount;
  544. _queue_update();
  545. _change_notify();
  546. }
  547. int Sprite3D::get_hframes() const {
  548. return hframes;
  549. }
  550. Rect2 Sprite3D::get_item_rect() const {
  551. if (texture.is_null()) {
  552. return Rect2(0, 0, 1, 1);
  553. }
  554. /*
  555. if (texture.is_null())
  556. return CanvasItem::get_item_rect();
  557. */
  558. Size2 s;
  559. if (region) {
  560. s = region_rect.size;
  561. } else {
  562. s = texture->get_size();
  563. s = s / Point2(hframes, vframes);
  564. }
  565. Point2 ofs = get_offset();
  566. if (is_centered()) {
  567. ofs -= s / 2;
  568. }
  569. if (s == Size2(0, 0)) {
  570. s = Size2(1, 1);
  571. }
  572. return Rect2(ofs, s);
  573. }
  574. void Sprite3D::_validate_property(PropertyInfo &property) const {
  575. if (property.name == "frame") {
  576. property.hint = PROPERTY_HINT_RANGE;
  577. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  578. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  579. }
  580. if (property.name == "frame_coords") {
  581. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  582. }
  583. }
  584. void Sprite3D::_bind_methods() {
  585. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
  586. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
  587. ClassDB::bind_method(D_METHOD("set_region", "enabled"), &Sprite3D::set_region);
  588. ClassDB::bind_method(D_METHOD("is_region"), &Sprite3D::is_region);
  589. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
  590. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
  591. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
  592. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
  593. ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords);
  594. ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords);
  595. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
  596. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
  597. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
  598. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
  599. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  600. ADD_GROUP("Animation", "");
  601. ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  603. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  604. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords");
  605. ADD_GROUP("Region", "region_");
  606. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region");
  607. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  608. ADD_SIGNAL(MethodInfo("frame_changed"));
  609. }
  610. Sprite3D::Sprite3D() {
  611. region = false;
  612. frame = 0;
  613. vframes = 1;
  614. hframes = 1;
  615. }
  616. ////////////////////////////////////////
  617. void AnimatedSprite3D::_draw() {
  618. if (get_base() != get_mesh()) {
  619. set_base(get_mesh());
  620. }
  621. if (frames.is_null()) {
  622. return;
  623. }
  624. if (frame < 0) {
  625. return;
  626. }
  627. if (!frames->has_animation(animation)) {
  628. return;
  629. }
  630. Ref<Texture> texture = frames->get_frame(animation, frame);
  631. if (!texture.is_valid()) {
  632. set_base(RID());
  633. return; //no texuture no life
  634. }
  635. Size2 tsize = texture->get_size();
  636. if (tsize.x == 0 || tsize.y == 0) {
  637. return;
  638. }
  639. Rect2 src_rect;
  640. src_rect.size = tsize;
  641. Point2 ofs = get_offset();
  642. if (is_centered()) {
  643. ofs -= tsize / 2;
  644. }
  645. Rect2 dst_rect(ofs, tsize);
  646. Rect2 final_rect;
  647. Rect2 final_src_rect;
  648. if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect)) {
  649. return;
  650. }
  651. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  652. return;
  653. }
  654. Color color = _get_color_accum();
  655. color.a *= get_opacity();
  656. float pixel_size = get_pixel_size();
  657. Vector2 vertices[4] = {
  658. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  659. (final_rect.position + final_rect.size) * pixel_size,
  660. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  661. final_rect.position * pixel_size,
  662. };
  663. Vector2 src_tsize = tsize;
  664. // Properly setup UVs for impostor textures (AtlasTexture).
  665. Ref<AtlasTexture> atlas_tex = texture;
  666. if (atlas_tex != nullptr) {
  667. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  668. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  669. }
  670. Vector2 uvs[4] = {
  671. final_src_rect.position / src_tsize,
  672. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  673. (final_src_rect.position + final_src_rect.size) / src_tsize,
  674. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  675. };
  676. if (is_flipped_h()) {
  677. SWAP(uvs[0], uvs[1]);
  678. SWAP(uvs[2], uvs[3]);
  679. }
  680. if (is_flipped_v()) {
  681. SWAP(uvs[0], uvs[3]);
  682. SWAP(uvs[1], uvs[2]);
  683. }
  684. Vector3 normal;
  685. int axis = get_axis();
  686. normal[axis] = 1.0;
  687. Plane tangent;
  688. if (axis == Vector3::AXIS_X) {
  689. tangent = Plane(0, 0, -1, -1);
  690. } else {
  691. tangent = Plane(1, 0, 0, -1);
  692. }
  693. int x_axis = ((axis + 1) % 3);
  694. int y_axis = ((axis + 2) % 3);
  695. if (axis != Vector3::AXIS_Z) {
  696. SWAP(x_axis, y_axis);
  697. for (int i = 0; i < 4; i++) {
  698. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  699. //SWAP(vertices[i].x,vertices[i].y);
  700. if (axis == Vector3::AXIS_Y) {
  701. vertices[i].y = -vertices[i].y;
  702. } else if (axis == Vector3::AXIS_X) {
  703. vertices[i].x = -vertices[i].x;
  704. }
  705. }
  706. }
  707. AABB aabb;
  708. // Everything except position, color, and UV is compressed
  709. PoolVector<uint8_t>::Write write_buffer = mesh_buffer.write();
  710. Vector2 normal_oct = VisualServer::get_singleton()->norm_to_oct(normal);
  711. int8_t v_normal[2] = {
  712. (int8_t)CLAMP(normal_oct.x * 127, -128, 127),
  713. (int8_t)CLAMP(normal_oct.y * 127, -128, 127),
  714. };
  715. Vector2 tangent_oct = VisualServer::get_singleton()->tangent_to_oct(tangent.normal, tangent.d, false);
  716. int8_t v_tangent[2] = {
  717. (int8_t)CLAMP(tangent_oct.x * 127, -128, 127),
  718. (int8_t)CLAMP(tangent_oct.y * 127, -128, 127),
  719. };
  720. for (int i = 0; i < 4; i++) {
  721. Vector3 vtx;
  722. vtx[x_axis] = vertices[i][0];
  723. vtx[y_axis] = vertices[i][1];
  724. if (i == 0) {
  725. aabb.position = vtx;
  726. aabb.size = Vector3();
  727. } else {
  728. aabb.expand_to(vtx);
  729. }
  730. float v_uv[2] = { uvs[i].x, uvs[i].y };
  731. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_TEX_UV] + mesh_surface_offsets[VS::ARRAY_TEX_UV]], v_uv, 8);
  732. float v_vertex[3] = { vtx.x, vtx.y, vtx.z };
  733. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_VERTEX] + mesh_surface_offsets[VS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
  734. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_NORMAL] + mesh_surface_offsets[VS::ARRAY_NORMAL]], v_normal, 2);
  735. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_TANGENT] + mesh_surface_offsets[VS::ARRAY_TANGENT]], v_tangent, 2);
  736. memcpy(&write_buffer[i * mesh_stride[VS::ARRAY_COLOR] + mesh_surface_offsets[VS::ARRAY_COLOR]], color.components, 4 * 4);
  737. }
  738. write_buffer.release();
  739. RID mesh = get_mesh();
  740. VS::get_singleton()->mesh_surface_update_region(mesh, 0, 0, mesh_buffer);
  741. VS::get_singleton()->mesh_set_custom_aabb(mesh, aabb);
  742. set_aabb(aabb);
  743. RID mat = SpatialMaterial::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() == SpatialMaterial::BILLBOARD_ENABLED, get_billboard_mode() == SpatialMaterial::BILLBOARD_FIXED_Y);
  744. VS::get_singleton()->material_set_shader(get_material(), VS::get_singleton()->material_get_shader(mat));
  745. VS::get_singleton()->material_set_param(get_material(), "texture_albedo", texture->get_rid());
  746. VS::get_singleton()->instance_set_surface_material(get_instance(), 0, get_material());
  747. }
  748. void AnimatedSprite3D::_validate_property(PropertyInfo &property) const {
  749. if (!frames.is_valid()) {
  750. return;
  751. }
  752. if (property.name == "animation") {
  753. property.hint = PROPERTY_HINT_ENUM;
  754. List<StringName> names;
  755. frames->get_animation_list(&names);
  756. names.sort_custom<StringName::AlphCompare>();
  757. bool current_found = false;
  758. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  759. if (E->prev()) {
  760. property.hint_string += ",";
  761. }
  762. property.hint_string += String(E->get());
  763. if (animation == E->get()) {
  764. current_found = true;
  765. }
  766. }
  767. if (!current_found) {
  768. if (property.hint_string == String()) {
  769. property.hint_string = String(animation);
  770. } else {
  771. property.hint_string = String(animation) + "," + property.hint_string;
  772. }
  773. }
  774. }
  775. if (property.name == "frame") {
  776. property.hint = PROPERTY_HINT_RANGE;
  777. if (frames->has_animation(animation) && frames->get_frame_count(animation) > 1) {
  778. property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  779. }
  780. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  781. }
  782. }
  783. void AnimatedSprite3D::_notification(int p_what) {
  784. switch (p_what) {
  785. case NOTIFICATION_INTERNAL_PROCESS: {
  786. if (frames.is_null()) {
  787. return;
  788. }
  789. if (!frames->has_animation(animation)) {
  790. return;
  791. }
  792. if (frame < 0) {
  793. return;
  794. }
  795. float speed = frames->get_animation_speed(animation);
  796. if (speed == 0) {
  797. return; //do nothing
  798. }
  799. float remaining = get_process_delta_time();
  800. while (remaining) {
  801. if (timeout <= 0) {
  802. timeout = 1.0 / speed;
  803. int fc = frames->get_frame_count(animation);
  804. if (frame >= fc - 1) {
  805. if (frames->get_animation_loop(animation)) {
  806. frame = 0;
  807. } else {
  808. frame = fc - 1;
  809. }
  810. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  811. } else {
  812. frame++;
  813. }
  814. _queue_update();
  815. _change_notify("frame");
  816. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  817. }
  818. float to_process = MIN(timeout, remaining);
  819. remaining -= to_process;
  820. timeout -= to_process;
  821. }
  822. } break;
  823. }
  824. }
  825. void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  826. if (frames.is_valid()) {
  827. frames->disconnect("changed", this, "_res_changed");
  828. }
  829. frames = p_frames;
  830. if (frames.is_valid()) {
  831. frames->connect("changed", this, "_res_changed");
  832. }
  833. if (!frames.is_valid()) {
  834. frame = 0;
  835. } else {
  836. set_frame(frame);
  837. }
  838. _change_notify();
  839. _reset_timeout();
  840. _queue_update();
  841. update_configuration_warning();
  842. }
  843. Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
  844. return frames;
  845. }
  846. void AnimatedSprite3D::set_frame(int p_frame) {
  847. if (!frames.is_valid()) {
  848. return;
  849. }
  850. if (frames->has_animation(animation)) {
  851. int limit = frames->get_frame_count(animation);
  852. if (p_frame >= limit) {
  853. p_frame = limit - 1;
  854. }
  855. }
  856. if (p_frame < 0) {
  857. p_frame = 0;
  858. }
  859. if (frame == p_frame) {
  860. return;
  861. }
  862. frame = p_frame;
  863. _reset_timeout();
  864. _queue_update();
  865. _change_notify("frame");
  866. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  867. }
  868. int AnimatedSprite3D::get_frame() const {
  869. return frame;
  870. }
  871. Rect2 AnimatedSprite3D::get_item_rect() const {
  872. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  873. return Rect2(0, 0, 1, 1);
  874. }
  875. Ref<Texture> t;
  876. if (animation) {
  877. t = frames->get_frame(animation, frame);
  878. }
  879. if (t.is_null()) {
  880. return Rect2(0, 0, 1, 1);
  881. }
  882. Size2 s = t->get_size();
  883. Point2 ofs = get_offset();
  884. if (centered) {
  885. ofs -= s / 2;
  886. }
  887. if (s == Size2(0, 0)) {
  888. s = Size2(1, 1);
  889. }
  890. return Rect2(ofs, s);
  891. }
  892. void AnimatedSprite3D::_res_changed() {
  893. set_frame(frame);
  894. _change_notify("frame");
  895. _change_notify("animation");
  896. _queue_update();
  897. }
  898. void AnimatedSprite3D::_set_playing(bool p_playing) {
  899. if (playing == p_playing) {
  900. return;
  901. }
  902. playing = p_playing;
  903. _reset_timeout();
  904. set_process_internal(playing);
  905. }
  906. bool AnimatedSprite3D::_is_playing() const {
  907. return playing;
  908. }
  909. void AnimatedSprite3D::play(const StringName &p_animation) {
  910. if (p_animation) {
  911. set_animation(p_animation);
  912. }
  913. _set_playing(true);
  914. }
  915. void AnimatedSprite3D::stop() {
  916. _set_playing(false);
  917. }
  918. bool AnimatedSprite3D::is_playing() const {
  919. return playing;
  920. }
  921. void AnimatedSprite3D::_reset_timeout() {
  922. if (!playing) {
  923. return;
  924. }
  925. if (frames.is_valid() && frames->has_animation(animation)) {
  926. float speed = frames->get_animation_speed(animation);
  927. if (speed > 0) {
  928. timeout = 1.0 / speed;
  929. } else {
  930. timeout = 0;
  931. }
  932. } else {
  933. timeout = 0;
  934. }
  935. }
  936. void AnimatedSprite3D::set_animation(const StringName &p_animation) {
  937. if (animation == p_animation) {
  938. return;
  939. }
  940. animation = p_animation;
  941. _reset_timeout();
  942. set_frame(0);
  943. _change_notify();
  944. _queue_update();
  945. }
  946. StringName AnimatedSprite3D::get_animation() const {
  947. return animation;
  948. }
  949. String AnimatedSprite3D::get_configuration_warning() const {
  950. String warning = SpriteBase3D::get_configuration_warning();
  951. if (frames.is_null()) {
  952. if (warning != String()) {
  953. warning += "\n\n";
  954. }
  955. warning += TTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite3D to display frames.");
  956. }
  957. return warning;
  958. }
  959. void AnimatedSprite3D::_bind_methods() {
  960. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
  961. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
  962. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite3D::set_animation);
  963. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
  964. ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite3D::_set_playing);
  965. ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite3D::_is_playing);
  966. ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
  967. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
  968. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
  969. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
  970. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
  971. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed);
  972. ADD_SIGNAL(MethodInfo("frame_changed"));
  973. ADD_SIGNAL(MethodInfo("animation_finished"));
  974. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  975. ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
  976. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  977. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
  978. }
  979. AnimatedSprite3D::AnimatedSprite3D() {
  980. frame = 0;
  981. playing = false;
  982. animation = "default";
  983. timeout = 0;
  984. }