sprite_3d.cpp 38 KB

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