sprite_3d.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. /**************************************************************************/
  2. /* sprite_3d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "scene/resources/atlas_texture.h"
  32. Color SpriteBase3D::_get_color_accum() {
  33. if (!color_dirty) {
  34. return color_accum;
  35. }
  36. if (parent_sprite) {
  37. color_accum = parent_sprite->_get_color_accum();
  38. } else {
  39. color_accum = Color(1, 1, 1, 1);
  40. }
  41. color_accum.r *= modulate.r;
  42. color_accum.g *= modulate.g;
  43. color_accum.b *= modulate.b;
  44. color_accum.a *= modulate.a;
  45. color_dirty = false;
  46. return color_accum;
  47. }
  48. void SpriteBase3D::_propagate_color_changed() {
  49. if (color_dirty) {
  50. return;
  51. }
  52. color_dirty = true;
  53. _queue_redraw();
  54. for (SpriteBase3D *&E : children) {
  55. E->_propagate_color_changed();
  56. }
  57. }
  58. void SpriteBase3D::_notification(int p_what) {
  59. switch (p_what) {
  60. case 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. } break;
  69. case NOTIFICATION_EXIT_TREE: {
  70. if (parent_sprite) {
  71. parent_sprite->children.erase(pI);
  72. pI = nullptr;
  73. parent_sprite = nullptr;
  74. }
  75. } break;
  76. }
  77. }
  78. void SpriteBase3D::draw_texture_rect(Ref<Texture2D> p_texture, Rect2 p_dst_rect, Rect2 p_src_rect) {
  79. ERR_FAIL_COND(p_texture.is_null());
  80. Rect2 final_rect;
  81. Rect2 final_src_rect;
  82. if (!p_texture->get_rect_region(p_dst_rect, p_src_rect, final_rect, final_src_rect)) {
  83. return;
  84. }
  85. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  86. return;
  87. }
  88. // 2D: 3D plane (axes match exactly when `axis == Vector3::AXIS_Z`):
  89. // -X+ -X+
  90. // - +
  91. // Y +--------+ +--------+ +--------+ Y +--------+
  92. // + | +--+ | | | (2) | | - | 0--1 |
  93. // | |ab| | (1) | +--+ | (3) | 3--2 | | |ab| |
  94. // | |cd| | --> | |ab| | --> | |cd| | <==> | |cd| |
  95. // | +--+ | | |cd| | | |ab| | | 3--2 |
  96. // | | | +--+ | | 0--1 | | |
  97. // +--------+ +--------+ +--------+ +--------+
  98. // (1) Y-wise shift `final_rect` within `p_dst_rect` so after inverting Y
  99. // axis distances between top/bottom borders will be preserved (so for
  100. // example AtlasTextures with vertical margins will look the same in 2D/3D).
  101. final_rect.position.y = (p_dst_rect.position.y + p_dst_rect.size.y) - ((final_rect.position.y + final_rect.size.y) - p_dst_rect.position.y);
  102. Color color = _get_color_accum();
  103. real_t px_size = get_pixel_size();
  104. // (2) Order vertices (0123) bottom-top in 2D / top-bottom in 3D.
  105. Vector2 vertices[4] = {
  106. (final_rect.position + Vector2(0, final_rect.size.y)) * px_size,
  107. (final_rect.position + final_rect.size) * px_size,
  108. (final_rect.position + Vector2(final_rect.size.x, 0)) * px_size,
  109. final_rect.position * px_size,
  110. };
  111. Vector2 src_tsize = p_texture->get_size();
  112. // Properly setup UVs for impostor textures (AtlasTexture).
  113. Ref<AtlasTexture> atlas_tex = p_texture;
  114. if (atlas_tex.is_valid()) {
  115. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  116. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  117. }
  118. // (3) Assign UVs (abcd) according to the vertices order (bottom-top in 2D / top-bottom in 3D).
  119. Vector2 uvs[4] = {
  120. final_src_rect.position / src_tsize,
  121. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  122. (final_src_rect.position + final_src_rect.size) / src_tsize,
  123. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  124. };
  125. if (is_flipped_h()) {
  126. SWAP(uvs[0], uvs[1]);
  127. SWAP(uvs[2], uvs[3]);
  128. }
  129. if (is_flipped_v()) {
  130. SWAP(uvs[0], uvs[3]);
  131. SWAP(uvs[1], uvs[2]);
  132. }
  133. Vector3 normal;
  134. int ax = get_axis();
  135. normal[ax] = 1.0;
  136. Plane tangent;
  137. if (ax == Vector3::AXIS_X) {
  138. tangent = Plane(0, 0, -1, 1);
  139. } else {
  140. tangent = Plane(1, 0, 0, 1);
  141. }
  142. int x_axis = ((ax + 1) % 3);
  143. int y_axis = ((ax + 2) % 3);
  144. if (ax != Vector3::AXIS_Z) {
  145. SWAP(x_axis, y_axis);
  146. for (int i = 0; i < 4; i++) {
  147. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  148. //SWAP(vertices[i].x,vertices[i].y);
  149. if (ax == Vector3::AXIS_Y) {
  150. vertices[i].y = -vertices[i].y;
  151. } else if (ax == Vector3::AXIS_X) {
  152. vertices[i].x = -vertices[i].x;
  153. }
  154. }
  155. }
  156. AABB aabb_new;
  157. // Everything except position and UV is compressed.
  158. uint8_t *vertex_write_buffer = vertex_buffer.ptrw();
  159. uint8_t *attribute_write_buffer = attribute_buffer.ptrw();
  160. uint32_t v_normal;
  161. {
  162. Vector2 res = normal.octahedron_encode();
  163. uint32_t value = 0;
  164. value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  165. value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16;
  166. v_normal = value;
  167. }
  168. uint32_t v_tangent;
  169. {
  170. Plane t = tangent;
  171. Vector2 res = t.normal.octahedron_tangent_encode(t.d);
  172. uint32_t value = 0;
  173. value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535);
  174. value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16;
  175. if (value == 4294901760) {
  176. // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection.
  177. // So we sanitize here.
  178. value = 4294967295;
  179. }
  180. v_tangent = value;
  181. }
  182. uint8_t v_color[4] = {
  183. uint8_t(CLAMP(color.r * 255.0, 0.0, 255.0)),
  184. uint8_t(CLAMP(color.g * 255.0, 0.0, 255.0)),
  185. uint8_t(CLAMP(color.b * 255.0, 0.0, 255.0)),
  186. uint8_t(CLAMP(color.a * 255.0, 0.0, 255.0))
  187. };
  188. for (int i = 0; i < 4; i++) {
  189. Vector3 vtx;
  190. vtx[x_axis] = vertices[i][0];
  191. vtx[y_axis] = vertices[i][1];
  192. if (i == 0) {
  193. aabb_new.position = vtx;
  194. aabb_new.size = Vector3();
  195. } else {
  196. aabb_new.expand_to(vtx);
  197. }
  198. float v_uv[2] = { (float)uvs[i].x, (float)uvs[i].y };
  199. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_TEX_UV]], v_uv, 8);
  200. float v_vertex[3] = { (float)vtx.x, (float)vtx.y, (float)vtx.z };
  201. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
  202. memcpy(&vertex_write_buffer[i * normal_tangent_stride + mesh_surface_offsets[RS::ARRAY_NORMAL]], &v_normal, 4);
  203. memcpy(&vertex_write_buffer[i * normal_tangent_stride + mesh_surface_offsets[RS::ARRAY_TANGENT]], &v_tangent, 4);
  204. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_COLOR]], v_color, 4);
  205. }
  206. RID mesh_new = get_mesh();
  207. RS::get_singleton()->mesh_surface_update_vertex_region(mesh_new, 0, 0, vertex_buffer);
  208. RS::get_singleton()->mesh_surface_update_attribute_region(mesh_new, 0, 0, attribute_buffer);
  209. RS::get_singleton()->mesh_set_custom_aabb(mesh_new, aabb_new);
  210. set_aabb(aabb_new);
  211. RS::get_singleton()->material_set_param(get_material(), "alpha_scissor_threshold", alpha_scissor_threshold);
  212. RS::get_singleton()->material_set_param(get_material(), "alpha_hash_scale", alpha_hash_scale);
  213. RS::get_singleton()->material_set_param(get_material(), "alpha_antialiasing_edge", alpha_antialiasing_edge);
  214. BaseMaterial3D::Transparency mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_DISABLED;
  215. if (get_draw_flag(FLAG_TRANSPARENT)) {
  216. if (get_alpha_cut_mode() == ALPHA_CUT_DISCARD) {
  217. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_SCISSOR;
  218. } else if (get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS) {
  219. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS;
  220. } else if (get_alpha_cut_mode() == ALPHA_CUT_HASH) {
  221. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_HASH;
  222. } else {
  223. mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA;
  224. }
  225. }
  226. RID shader_rid;
  227. StandardMaterial3D::get_material_for_2d(get_draw_flag(FLAG_SHADED), mat_transparency, get_draw_flag(FLAG_DOUBLE_SIDED), 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(), alpha_antialiasing_mode, &shader_rid);
  228. if (last_shader != shader_rid) {
  229. RS::get_singleton()->material_set_shader(get_material(), shader_rid);
  230. last_shader = shader_rid;
  231. }
  232. if (last_texture != p_texture->get_rid()) {
  233. RS::get_singleton()->material_set_param(get_material(), "texture_albedo", p_texture->get_rid());
  234. RS::get_singleton()->material_set_param(get_material(), "albedo_texture_size", Vector2i(p_texture->get_width(), p_texture->get_height()));
  235. last_texture = p_texture->get_rid();
  236. }
  237. if (get_alpha_cut_mode() == ALPHA_CUT_DISABLED) {
  238. RS::get_singleton()->material_set_render_priority(get_material(), get_render_priority());
  239. RS::get_singleton()->mesh_surface_set_material(mesh, 0, get_material());
  240. }
  241. }
  242. void SpriteBase3D::set_centered(bool p_center) {
  243. if (centered == p_center) {
  244. return;
  245. }
  246. centered = p_center;
  247. _queue_redraw();
  248. }
  249. bool SpriteBase3D::is_centered() const {
  250. return centered;
  251. }
  252. void SpriteBase3D::set_offset(const Point2 &p_offset) {
  253. if (offset == p_offset) {
  254. return;
  255. }
  256. offset = p_offset;
  257. _queue_redraw();
  258. }
  259. Point2 SpriteBase3D::get_offset() const {
  260. return offset;
  261. }
  262. void SpriteBase3D::set_flip_h(bool p_flip) {
  263. if (hflip == p_flip) {
  264. return;
  265. }
  266. hflip = p_flip;
  267. _queue_redraw();
  268. }
  269. bool SpriteBase3D::is_flipped_h() const {
  270. return hflip;
  271. }
  272. void SpriteBase3D::set_flip_v(bool p_flip) {
  273. if (vflip == p_flip) {
  274. return;
  275. }
  276. vflip = p_flip;
  277. _queue_redraw();
  278. }
  279. bool SpriteBase3D::is_flipped_v() const {
  280. return vflip;
  281. }
  282. void SpriteBase3D::set_modulate(const Color &p_color) {
  283. if (modulate == p_color) {
  284. return;
  285. }
  286. modulate = p_color;
  287. _propagate_color_changed();
  288. _queue_redraw();
  289. }
  290. Color SpriteBase3D::get_modulate() const {
  291. return modulate;
  292. }
  293. void SpriteBase3D::set_render_priority(int p_priority) {
  294. ERR_FAIL_COND(p_priority < RS::MATERIAL_RENDER_PRIORITY_MIN || p_priority > RS::MATERIAL_RENDER_PRIORITY_MAX);
  295. if (render_priority == p_priority) {
  296. return;
  297. }
  298. render_priority = p_priority;
  299. _queue_redraw();
  300. }
  301. int SpriteBase3D::get_render_priority() const {
  302. return render_priority;
  303. }
  304. void SpriteBase3D::set_pixel_size(real_t p_amount) {
  305. if (pixel_size == p_amount) {
  306. return;
  307. }
  308. pixel_size = p_amount;
  309. _queue_redraw();
  310. }
  311. real_t SpriteBase3D::get_pixel_size() const {
  312. return pixel_size;
  313. }
  314. void SpriteBase3D::set_axis(Vector3::Axis p_axis) {
  315. ERR_FAIL_INDEX(p_axis, 3);
  316. if (axis == p_axis) {
  317. return;
  318. }
  319. axis = p_axis;
  320. _queue_redraw();
  321. }
  322. Vector3::Axis SpriteBase3D::get_axis() const {
  323. return axis;
  324. }
  325. void SpriteBase3D::_im_update() {
  326. _draw();
  327. pending_update = false;
  328. //texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  329. }
  330. void SpriteBase3D::_queue_redraw() {
  331. // The 3D equivalent of CanvasItem.queue_redraw().
  332. if (pending_update) {
  333. return;
  334. }
  335. triangle_mesh.unref();
  336. update_gizmos();
  337. pending_update = true;
  338. callable_mp(this, &SpriteBase3D::_im_update).call_deferred();
  339. }
  340. AABB SpriteBase3D::get_aabb() const {
  341. return aabb;
  342. }
  343. Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const {
  344. if (triangle_mesh.is_valid()) {
  345. return triangle_mesh;
  346. }
  347. Vector<Vector3> faces;
  348. faces.resize(6);
  349. Vector3 *facesw = faces.ptrw();
  350. Rect2 final_rect = get_item_rect();
  351. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  352. return Ref<TriangleMesh>();
  353. }
  354. real_t px_size = get_pixel_size();
  355. Vector2 vertices[4] = {
  356. (final_rect.position + Vector2(0, final_rect.size.y)) * px_size,
  357. (final_rect.position + final_rect.size) * px_size,
  358. (final_rect.position + Vector2(final_rect.size.x, 0)) * px_size,
  359. final_rect.position * px_size,
  360. };
  361. int x_axis = ((axis + 1) % 3);
  362. int y_axis = ((axis + 2) % 3);
  363. if (axis != Vector3::AXIS_Z) {
  364. SWAP(x_axis, y_axis);
  365. for (int i = 0; i < 4; i++) {
  366. if (axis == Vector3::AXIS_Y) {
  367. vertices[i].y = -vertices[i].y;
  368. } else if (axis == Vector3::AXIS_X) {
  369. vertices[i].x = -vertices[i].x;
  370. }
  371. }
  372. }
  373. static const int indices[6] = {
  374. 0, 1, 2,
  375. 0, 2, 3
  376. };
  377. for (int j = 0; j < 6; j++) {
  378. int i = indices[j];
  379. Vector3 vtx;
  380. vtx[x_axis] = vertices[i][0];
  381. vtx[y_axis] = vertices[i][1];
  382. facesw[j] = vtx;
  383. }
  384. triangle_mesh.instantiate();
  385. triangle_mesh->create(faces);
  386. return triangle_mesh;
  387. }
  388. void SpriteBase3D::set_draw_flag(DrawFlags p_flag, bool p_enable) {
  389. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  390. if (flags[p_flag] == p_enable) {
  391. return;
  392. }
  393. flags[p_flag] = p_enable;
  394. _queue_redraw();
  395. }
  396. bool SpriteBase3D::get_draw_flag(DrawFlags p_flag) const {
  397. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  398. return flags[p_flag];
  399. }
  400. void SpriteBase3D::set_alpha_cut_mode(AlphaCutMode p_mode) {
  401. ERR_FAIL_INDEX(p_mode, ALPHA_CUT_MAX);
  402. if (alpha_cut == p_mode) {
  403. return;
  404. }
  405. alpha_cut = p_mode;
  406. _queue_redraw();
  407. }
  408. SpriteBase3D::AlphaCutMode SpriteBase3D::get_alpha_cut_mode() const {
  409. return alpha_cut;
  410. }
  411. void SpriteBase3D::set_alpha_hash_scale(float p_hash_scale) {
  412. if (alpha_hash_scale == p_hash_scale) {
  413. return;
  414. }
  415. alpha_hash_scale = p_hash_scale;
  416. _queue_redraw();
  417. }
  418. float SpriteBase3D::get_alpha_hash_scale() const {
  419. return alpha_hash_scale;
  420. }
  421. void SpriteBase3D::set_alpha_scissor_threshold(float p_threshold) {
  422. if (alpha_scissor_threshold == p_threshold) {
  423. return;
  424. }
  425. alpha_scissor_threshold = p_threshold;
  426. _queue_redraw();
  427. }
  428. float SpriteBase3D::get_alpha_scissor_threshold() const {
  429. return alpha_scissor_threshold;
  430. }
  431. void SpriteBase3D::set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa) {
  432. if (alpha_antialiasing_mode == p_alpha_aa) {
  433. return;
  434. }
  435. alpha_antialiasing_mode = p_alpha_aa;
  436. _queue_redraw();
  437. }
  438. BaseMaterial3D::AlphaAntiAliasing SpriteBase3D::get_alpha_antialiasing() const {
  439. return alpha_antialiasing_mode;
  440. }
  441. void SpriteBase3D::set_alpha_antialiasing_edge(float p_edge) {
  442. if (alpha_antialiasing_edge == p_edge) {
  443. return;
  444. }
  445. alpha_antialiasing_edge = p_edge;
  446. _queue_redraw();
  447. }
  448. float SpriteBase3D::get_alpha_antialiasing_edge() const {
  449. return alpha_antialiasing_edge;
  450. }
  451. void SpriteBase3D::set_billboard_mode(StandardMaterial3D::BillboardMode p_mode) {
  452. ERR_FAIL_INDEX(p_mode, 3); // Cannot use BILLBOARD_PARTICLES.
  453. if (billboard_mode == p_mode) {
  454. return;
  455. }
  456. billboard_mode = p_mode;
  457. _queue_redraw();
  458. }
  459. StandardMaterial3D::BillboardMode SpriteBase3D::get_billboard_mode() const {
  460. return billboard_mode;
  461. }
  462. void SpriteBase3D::set_texture_filter(StandardMaterial3D::TextureFilter p_filter) {
  463. if (texture_filter == p_filter) {
  464. return;
  465. }
  466. texture_filter = p_filter;
  467. _queue_redraw();
  468. }
  469. StandardMaterial3D::TextureFilter SpriteBase3D::get_texture_filter() const {
  470. return texture_filter;
  471. }
  472. void SpriteBase3D::_bind_methods() {
  473. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered);
  474. ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered);
  475. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset);
  476. ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset);
  477. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h);
  478. ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h);
  479. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v);
  480. ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v);
  481. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate);
  482. ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate);
  483. ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &SpriteBase3D::set_render_priority);
  484. ClassDB::bind_method(D_METHOD("get_render_priority"), &SpriteBase3D::get_render_priority);
  485. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size);
  486. ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size);
  487. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis);
  488. ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis);
  489. ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag);
  490. ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag);
  491. ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode);
  492. ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode);
  493. ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &SpriteBase3D::set_alpha_scissor_threshold);
  494. ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &SpriteBase3D::get_alpha_scissor_threshold);
  495. ClassDB::bind_method(D_METHOD("set_alpha_hash_scale", "threshold"), &SpriteBase3D::set_alpha_hash_scale);
  496. ClassDB::bind_method(D_METHOD("get_alpha_hash_scale"), &SpriteBase3D::get_alpha_hash_scale);
  497. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing", "alpha_aa"), &SpriteBase3D::set_alpha_antialiasing);
  498. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing"), &SpriteBase3D::get_alpha_antialiasing);
  499. ClassDB::bind_method(D_METHOD("set_alpha_antialiasing_edge", "edge"), &SpriteBase3D::set_alpha_antialiasing_edge);
  500. ClassDB::bind_method(D_METHOD("get_alpha_antialiasing_edge"), &SpriteBase3D::get_alpha_antialiasing_edge);
  501. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpriteBase3D::set_billboard_mode);
  502. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpriteBase3D::get_billboard_mode);
  503. ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &SpriteBase3D::set_texture_filter);
  504. ClassDB::bind_method(D_METHOD("get_texture_filter"), &SpriteBase3D::get_texture_filter);
  505. ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect);
  506. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh);
  507. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  508. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset");
  509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  510. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  511. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  512. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001,suffix:m"), "set_pixel_size", "get_pixel_size");
  513. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis");
  514. ADD_GROUP("Flags", "");
  515. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode");
  516. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT);
  517. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
  518. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
  519. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "no_depth_test"), "set_draw_flag", "get_draw_flag", FLAG_DISABLE_DEPTH_TEST);
  520. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fixed_size"), "set_draw_flag", "get_draw_flag", FLAG_FIXED_SIZE);
  521. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass,Alpha Hash"), "set_alpha_cut_mode", "get_alpha_cut_mode");
  522. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold");
  523. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_hash_scale", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_alpha_hash_scale", "get_alpha_hash_scale");
  524. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_antialiasing_mode", PROPERTY_HINT_ENUM, "Disabled,Alpha Edge Blend,Alpha Edge Clip"), "set_alpha_antialiasing", "get_alpha_antialiasing");
  525. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_antialiasing_edge", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_antialiasing_edge", "get_alpha_antialiasing_edge");
  526. 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");
  527. 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");
  528. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  529. BIND_ENUM_CONSTANT(FLAG_SHADED);
  530. BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
  531. BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
  532. BIND_ENUM_CONSTANT(FLAG_FIXED_SIZE);
  533. BIND_ENUM_CONSTANT(FLAG_MAX);
  534. BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
  535. BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
  536. BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
  537. BIND_ENUM_CONSTANT(ALPHA_CUT_HASH);
  538. }
  539. SpriteBase3D::SpriteBase3D() {
  540. for (int i = 0; i < FLAG_MAX; i++) {
  541. flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED;
  542. }
  543. material = RenderingServer::get_singleton()->material_create();
  544. // Set defaults for material, names need to match up those in StandardMaterial3D.
  545. RS::get_singleton()->material_set_param(material, "albedo", Color(1, 1, 1, 1));
  546. RS::get_singleton()->material_set_param(material, "specular", 0.5);
  547. RS::get_singleton()->material_set_param(material, "metallic", 0.0);
  548. RS::get_singleton()->material_set_param(material, "roughness", 1.0);
  549. RS::get_singleton()->material_set_param(material, "uv1_offset", Vector3(0, 0, 0));
  550. RS::get_singleton()->material_set_param(material, "uv1_scale", Vector3(1, 1, 1));
  551. RS::get_singleton()->material_set_param(material, "uv2_offset", Vector3(0, 0, 0));
  552. RS::get_singleton()->material_set_param(material, "uv2_scale", Vector3(1, 1, 1));
  553. mesh = RenderingServer::get_singleton()->mesh_create();
  554. PackedVector3Array mesh_vertices;
  555. PackedVector3Array mesh_normals;
  556. PackedFloat32Array mesh_tangents;
  557. PackedColorArray mesh_colors;
  558. PackedVector2Array mesh_uvs;
  559. PackedInt32Array indices;
  560. mesh_vertices.resize(4);
  561. mesh_normals.resize(4);
  562. mesh_tangents.resize(16);
  563. mesh_colors.resize(4);
  564. mesh_uvs.resize(4);
  565. // Create basic mesh and store format information.
  566. for (int i = 0; i < 4; i++) {
  567. mesh_normals.write[i] = Vector3(0.0, 0.0, 1.0);
  568. mesh_tangents.write[i * 4 + 0] = 1.0;
  569. mesh_tangents.write[i * 4 + 1] = 0.0;
  570. mesh_tangents.write[i * 4 + 2] = 0.0;
  571. mesh_tangents.write[i * 4 + 3] = 1.0;
  572. mesh_colors.write[i] = Color(1.0, 1.0, 1.0, 1.0);
  573. mesh_uvs.write[i] = Vector2(0.0, 0.0);
  574. mesh_vertices.write[i] = Vector3(0.0, 0.0, 0.0);
  575. }
  576. indices.resize(6);
  577. indices.write[0] = 0;
  578. indices.write[1] = 1;
  579. indices.write[2] = 2;
  580. indices.write[3] = 0;
  581. indices.write[4] = 2;
  582. indices.write[5] = 3;
  583. Array mesh_array;
  584. mesh_array.resize(RS::ARRAY_MAX);
  585. mesh_array[RS::ARRAY_VERTEX] = mesh_vertices;
  586. mesh_array[RS::ARRAY_NORMAL] = mesh_normals;
  587. mesh_array[RS::ARRAY_TANGENT] = mesh_tangents;
  588. mesh_array[RS::ARRAY_COLOR] = mesh_colors;
  589. mesh_array[RS::ARRAY_TEX_UV] = mesh_uvs;
  590. mesh_array[RS::ARRAY_INDEX] = indices;
  591. RS::SurfaceData sd;
  592. RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, mesh_array);
  593. mesh_surface_format = sd.format;
  594. vertex_buffer = sd.vertex_data;
  595. attribute_buffer = sd.attribute_data;
  596. sd.material = material;
  597. RS::get_singleton()->mesh_surface_make_offsets_from_format(sd.format, sd.vertex_count, sd.index_count, mesh_surface_offsets, vertex_stride, normal_tangent_stride, attrib_stride, skin_stride);
  598. RS::get_singleton()->mesh_add_surface(mesh, sd);
  599. set_base(mesh);
  600. }
  601. SpriteBase3D::~SpriteBase3D() {
  602. ERR_FAIL_NULL(RenderingServer::get_singleton());
  603. RenderingServer::get_singleton()->free(mesh);
  604. RenderingServer::get_singleton()->free(material);
  605. }
  606. ///////////////////////////////////////////
  607. void Sprite3D::_draw() {
  608. if (get_base() != get_mesh()) {
  609. set_base(get_mesh());
  610. }
  611. if (texture.is_null()) {
  612. set_base(RID());
  613. return;
  614. }
  615. Vector2 tsize = texture->get_size();
  616. if (tsize.x == 0 || tsize.y == 0) {
  617. return;
  618. }
  619. Rect2 base_rect;
  620. if (region) {
  621. base_rect = region_rect;
  622. } else {
  623. base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
  624. }
  625. Size2 frame_size = base_rect.size / Size2(hframes, vframes);
  626. Point2 frame_offset = Point2(frame % hframes, frame / hframes) * frame_size;
  627. Point2 dst_offset = get_offset();
  628. if (is_centered()) {
  629. dst_offset -= frame_size / 2.0f;
  630. }
  631. Rect2 src_rect(base_rect.position + frame_offset, frame_size);
  632. Rect2 dst_rect(dst_offset, frame_size);
  633. draw_texture_rect(texture, dst_rect, src_rect);
  634. }
  635. void Sprite3D::set_texture(const Ref<Texture2D> &p_texture) {
  636. if (p_texture == texture) {
  637. return;
  638. }
  639. if (texture.is_valid()) {
  640. texture->disconnect(CoreStringName(changed), callable_mp((SpriteBase3D *)this, &Sprite3D::_queue_redraw));
  641. }
  642. texture = p_texture;
  643. if (texture.is_valid()) {
  644. texture->connect(CoreStringName(changed), callable_mp((SpriteBase3D *)this, &Sprite3D::_queue_redraw));
  645. }
  646. _queue_redraw();
  647. emit_signal(SceneStringName(texture_changed));
  648. }
  649. Ref<Texture2D> Sprite3D::get_texture() const {
  650. return texture;
  651. }
  652. void Sprite3D::set_region_enabled(bool p_region) {
  653. if (p_region == region) {
  654. return;
  655. }
  656. region = p_region;
  657. _queue_redraw();
  658. notify_property_list_changed();
  659. }
  660. bool Sprite3D::is_region_enabled() const {
  661. return region;
  662. }
  663. void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
  664. if (region_rect == p_region_rect) {
  665. return;
  666. }
  667. region_rect = p_region_rect;
  668. if (region) {
  669. _queue_redraw();
  670. }
  671. }
  672. Rect2 Sprite3D::get_region_rect() const {
  673. return region_rect;
  674. }
  675. void Sprite3D::set_frame(int p_frame) {
  676. ERR_FAIL_INDEX(p_frame, int64_t(vframes) * hframes);
  677. if (frame == p_frame) {
  678. return;
  679. }
  680. frame = p_frame;
  681. _queue_redraw();
  682. emit_signal(SceneStringName(frame_changed));
  683. }
  684. int Sprite3D::get_frame() const {
  685. return frame;
  686. }
  687. void Sprite3D::set_frame_coords(const Vector2i &p_coord) {
  688. ERR_FAIL_INDEX(p_coord.x, hframes);
  689. ERR_FAIL_INDEX(p_coord.y, vframes);
  690. set_frame(p_coord.y * hframes + p_coord.x);
  691. }
  692. Vector2i Sprite3D::get_frame_coords() const {
  693. return Vector2i(frame % hframes, frame / hframes);
  694. }
  695. void Sprite3D::set_vframes(int p_amount) {
  696. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of vframes cannot be smaller than 1.");
  697. if (vframes == p_amount) {
  698. return;
  699. }
  700. vframes = p_amount;
  701. if (frame >= vframes * hframes) {
  702. frame = 0;
  703. }
  704. _queue_redraw();
  705. notify_property_list_changed();
  706. }
  707. int Sprite3D::get_vframes() const {
  708. return vframes;
  709. }
  710. void Sprite3D::set_hframes(int p_amount) {
  711. ERR_FAIL_COND_MSG(p_amount < 1, "Amount of hframes cannot be smaller than 1.");
  712. if (hframes == p_amount) {
  713. return;
  714. }
  715. if (vframes > 1) {
  716. // Adjust the frame to fit new sheet dimensions.
  717. int original_column = frame % hframes;
  718. if (original_column >= p_amount) {
  719. // Frame's column was dropped, reset.
  720. frame = 0;
  721. } else {
  722. int original_row = frame / hframes;
  723. frame = original_row * p_amount + original_column;
  724. }
  725. }
  726. hframes = p_amount;
  727. if (frame >= vframes * hframes) {
  728. frame = 0;
  729. }
  730. _queue_redraw();
  731. notify_property_list_changed();
  732. }
  733. int Sprite3D::get_hframes() const {
  734. return hframes;
  735. }
  736. Rect2 Sprite3D::get_item_rect() const {
  737. if (texture.is_null()) {
  738. return Rect2(0, 0, 1, 1);
  739. }
  740. Size2 s;
  741. if (region) {
  742. s = region_rect.size;
  743. } else {
  744. s = texture->get_size();
  745. s = s / Point2(hframes, vframes);
  746. }
  747. Point2 ofs = get_offset();
  748. if (is_centered()) {
  749. ofs -= s / 2;
  750. }
  751. if (s == Size2(0, 0)) {
  752. s = Size2(1, 1);
  753. }
  754. return Rect2(ofs, s);
  755. }
  756. void Sprite3D::_validate_property(PropertyInfo &p_property) const {
  757. if (p_property.name == "frame") {
  758. p_property.hint = PROPERTY_HINT_RANGE;
  759. p_property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  760. p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  761. }
  762. if (p_property.name == "frame_coords") {
  763. p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  764. }
  765. if (!region && (p_property.name == "region_rect")) {
  766. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  767. }
  768. }
  769. void Sprite3D::_bind_methods() {
  770. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
  771. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
  772. ClassDB::bind_method(D_METHOD("set_region_enabled", "enabled"), &Sprite3D::set_region_enabled);
  773. ClassDB::bind_method(D_METHOD("is_region_enabled"), &Sprite3D::is_region_enabled);
  774. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
  775. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
  776. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
  777. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
  778. ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords);
  779. ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords);
  780. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
  781. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
  782. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
  783. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
  784. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  785. ADD_GROUP("Animation", "");
  786. ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  787. ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  788. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  789. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords");
  790. ADD_GROUP("Region", "region_");
  791. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region_enabled", "is_region_enabled");
  792. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect");
  793. ADD_SIGNAL(MethodInfo("frame_changed"));
  794. ADD_SIGNAL(MethodInfo("texture_changed"));
  795. }
  796. Sprite3D::Sprite3D() {
  797. }
  798. ////////////////////////////////////////
  799. void AnimatedSprite3D::_draw() {
  800. if (get_base() != get_mesh()) {
  801. set_base(get_mesh());
  802. }
  803. if (frames.is_null() || !frames->has_animation(animation)) {
  804. return;
  805. }
  806. Ref<Texture2D> texture = frames->get_frame_texture(animation, frame);
  807. if (texture.is_null()) {
  808. set_base(RID());
  809. return;
  810. }
  811. Size2 tsize = texture->get_size();
  812. if (tsize.x == 0 || tsize.y == 0) {
  813. return;
  814. }
  815. Rect2 src_rect;
  816. src_rect.size = tsize;
  817. Point2 ofs = get_offset();
  818. if (is_centered()) {
  819. ofs -= tsize / 2;
  820. }
  821. Rect2 dst_rect(ofs, tsize);
  822. draw_texture_rect(texture, dst_rect, src_rect);
  823. }
  824. void AnimatedSprite3D::_validate_property(PropertyInfo &p_property) const {
  825. if (frames.is_null()) {
  826. return;
  827. }
  828. if (p_property.name == "animation") {
  829. List<StringName> names;
  830. frames->get_animation_list(&names);
  831. names.sort_custom<StringName::AlphCompare>();
  832. bool current_found = false;
  833. bool is_first_element = true;
  834. for (const StringName &E : names) {
  835. if (!is_first_element) {
  836. p_property.hint_string += ",";
  837. } else {
  838. is_first_element = false;
  839. }
  840. p_property.hint_string += String(E);
  841. if (animation == E) {
  842. current_found = true;
  843. }
  844. }
  845. if (!current_found) {
  846. if (p_property.hint_string.is_empty()) {
  847. p_property.hint_string = String(animation);
  848. } else {
  849. p_property.hint_string = String(animation) + "," + p_property.hint_string;
  850. }
  851. }
  852. return;
  853. }
  854. if (p_property.name == "frame") {
  855. if (playing) {
  856. p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY;
  857. return;
  858. }
  859. p_property.hint = PROPERTY_HINT_RANGE;
  860. if (frames->has_animation(animation) && frames->get_frame_count(animation) > 0) {
  861. p_property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  862. } else {
  863. // Avoid an error, `hint_string` is required for `PROPERTY_HINT_RANGE`.
  864. p_property.hint_string = "0,0,1";
  865. }
  866. p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  867. }
  868. }
  869. void AnimatedSprite3D::_notification(int p_what) {
  870. switch (p_what) {
  871. case NOTIFICATION_READY: {
  872. if (!Engine::get_singleton()->is_editor_hint() && frames.is_valid() && frames->has_animation(autoplay)) {
  873. play(autoplay);
  874. }
  875. } break;
  876. case NOTIFICATION_INTERNAL_PROCESS: {
  877. if (frames.is_null() || !frames->has_animation(animation)) {
  878. return;
  879. }
  880. double remaining = get_process_delta_time();
  881. int i = 0;
  882. while (remaining) {
  883. // Animation speed may be changed by animation_finished or frame_changed signals.
  884. double speed = frames->get_animation_speed(animation) * speed_scale * custom_speed_scale * frame_speed_scale;
  885. double abs_speed = Math::abs(speed);
  886. if (speed == 0) {
  887. return; // Do nothing.
  888. }
  889. // Frame count may be changed by animation_finished or frame_changed signals.
  890. int fc = frames->get_frame_count(animation);
  891. int last_frame = fc - 1;
  892. if (!signbit(speed)) {
  893. // Forwards.
  894. if (frame_progress >= 1.0) {
  895. if (frame >= last_frame) {
  896. if (frames->get_animation_loop(animation)) {
  897. frame = 0;
  898. emit_signal("animation_looped");
  899. } else {
  900. frame = last_frame;
  901. pause();
  902. emit_signal(SceneStringName(animation_finished));
  903. return;
  904. }
  905. } else {
  906. frame++;
  907. }
  908. _calc_frame_speed_scale();
  909. frame_progress = 0.0;
  910. _queue_redraw();
  911. emit_signal(SceneStringName(frame_changed));
  912. }
  913. double to_process = MIN((1.0 - frame_progress) / abs_speed, remaining);
  914. frame_progress += to_process * abs_speed;
  915. remaining -= to_process;
  916. } else {
  917. // Backwards.
  918. if (frame_progress <= 0) {
  919. if (frame <= 0) {
  920. if (frames->get_animation_loop(animation)) {
  921. frame = last_frame;
  922. emit_signal("animation_looped");
  923. } else {
  924. frame = 0;
  925. pause();
  926. emit_signal(SceneStringName(animation_finished));
  927. return;
  928. }
  929. } else {
  930. frame--;
  931. }
  932. _calc_frame_speed_scale();
  933. frame_progress = 1.0;
  934. _queue_redraw();
  935. emit_signal(SceneStringName(frame_changed));
  936. }
  937. double to_process = MIN(frame_progress / abs_speed, remaining);
  938. frame_progress -= to_process * abs_speed;
  939. remaining -= to_process;
  940. }
  941. i++;
  942. if (i > fc) {
  943. return; // Prevents freezing if to_process is each time much less than remaining.
  944. }
  945. }
  946. } break;
  947. }
  948. }
  949. void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  950. if (frames == p_frames) {
  951. return;
  952. }
  953. if (frames.is_valid()) {
  954. frames->disconnect(CoreStringName(changed), callable_mp(this, &AnimatedSprite3D::_res_changed));
  955. }
  956. stop();
  957. frames = p_frames;
  958. if (frames.is_valid()) {
  959. frames->connect(CoreStringName(changed), callable_mp(this, &AnimatedSprite3D::_res_changed));
  960. List<StringName> al;
  961. frames->get_animation_list(&al);
  962. if (al.size() == 0) {
  963. set_animation(StringName());
  964. autoplay = String();
  965. } else {
  966. if (!frames->has_animation(animation)) {
  967. set_animation(al.front()->get());
  968. }
  969. if (!frames->has_animation(autoplay)) {
  970. autoplay = String();
  971. }
  972. }
  973. }
  974. notify_property_list_changed();
  975. _queue_redraw();
  976. update_configuration_warnings();
  977. emit_signal("sprite_frames_changed");
  978. }
  979. Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
  980. return frames;
  981. }
  982. void AnimatedSprite3D::set_frame(int p_frame) {
  983. set_frame_and_progress(p_frame, signbit(get_playing_speed()) ? 1.0 : 0.0);
  984. }
  985. int AnimatedSprite3D::get_frame() const {
  986. return frame;
  987. }
  988. void AnimatedSprite3D::set_frame_progress(real_t p_progress) {
  989. frame_progress = p_progress;
  990. }
  991. real_t AnimatedSprite3D::get_frame_progress() const {
  992. return frame_progress;
  993. }
  994. void AnimatedSprite3D::set_frame_and_progress(int p_frame, real_t p_progress) {
  995. if (frames.is_null()) {
  996. return;
  997. }
  998. bool has_animation = frames->has_animation(animation);
  999. int end_frame = has_animation ? MAX(0, frames->get_frame_count(animation) - 1) : 0;
  1000. bool is_changed = frame != p_frame;
  1001. if (p_frame < 0) {
  1002. frame = 0;
  1003. } else if (has_animation && p_frame > end_frame) {
  1004. frame = end_frame;
  1005. } else {
  1006. frame = p_frame;
  1007. }
  1008. _calc_frame_speed_scale();
  1009. frame_progress = p_progress;
  1010. if (!is_changed) {
  1011. return; // No change, don't redraw.
  1012. }
  1013. _queue_redraw();
  1014. emit_signal(SceneStringName(frame_changed));
  1015. }
  1016. void AnimatedSprite3D::set_speed_scale(float p_speed_scale) {
  1017. speed_scale = p_speed_scale;
  1018. }
  1019. float AnimatedSprite3D::get_speed_scale() const {
  1020. return speed_scale;
  1021. }
  1022. float AnimatedSprite3D::get_playing_speed() const {
  1023. if (!playing) {
  1024. return 0;
  1025. }
  1026. return speed_scale * custom_speed_scale;
  1027. }
  1028. Rect2 AnimatedSprite3D::get_item_rect() const {
  1029. if (frames.is_null() || !frames->has_animation(animation)) {
  1030. return Rect2(0, 0, 1, 1);
  1031. }
  1032. if (frame < 0 || frame >= frames->get_frame_count(animation)) {
  1033. return Rect2(0, 0, 1, 1);
  1034. }
  1035. Ref<Texture2D> t;
  1036. if (animation) {
  1037. t = frames->get_frame_texture(animation, frame);
  1038. }
  1039. if (t.is_null()) {
  1040. return Rect2(0, 0, 1, 1);
  1041. }
  1042. Size2 s = t->get_size();
  1043. Point2 ofs = get_offset();
  1044. if (is_centered()) {
  1045. ofs -= s / 2;
  1046. }
  1047. if (s == Size2(0, 0)) {
  1048. s = Size2(1, 1);
  1049. }
  1050. return Rect2(ofs, s);
  1051. }
  1052. void AnimatedSprite3D::_res_changed() {
  1053. set_frame_and_progress(frame, frame_progress);
  1054. _queue_redraw();
  1055. notify_property_list_changed();
  1056. }
  1057. bool AnimatedSprite3D::is_playing() const {
  1058. return playing;
  1059. }
  1060. void AnimatedSprite3D::set_autoplay(const String &p_name) {
  1061. if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
  1062. WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
  1063. }
  1064. autoplay = p_name;
  1065. }
  1066. String AnimatedSprite3D::get_autoplay() const {
  1067. return autoplay;
  1068. }
  1069. void AnimatedSprite3D::play(const StringName &p_name, float p_custom_scale, bool p_from_end) {
  1070. StringName name = p_name;
  1071. if (name == StringName()) {
  1072. name = animation;
  1073. }
  1074. ERR_FAIL_COND_MSG(frames.is_null(), vformat("There is no animation with name '%s'.", name));
  1075. ERR_FAIL_COND_MSG(!frames->get_animation_names().has(name), vformat("There is no animation with name '%s'.", name));
  1076. if (frames->get_frame_count(name) == 0) {
  1077. return;
  1078. }
  1079. playing = true;
  1080. custom_speed_scale = p_custom_scale;
  1081. if (name != animation) {
  1082. animation = name;
  1083. int end_frame = MAX(0, frames->get_frame_count(animation) - 1);
  1084. if (p_from_end) {
  1085. set_frame_and_progress(end_frame, 1.0);
  1086. } else {
  1087. set_frame_and_progress(0, 0.0);
  1088. }
  1089. emit_signal(SceneStringName(animation_changed));
  1090. } else {
  1091. int end_frame = MAX(0, frames->get_frame_count(animation) - 1);
  1092. bool is_backward = signbit(speed_scale * custom_speed_scale);
  1093. if (p_from_end && is_backward && frame == 0 && frame_progress <= 0.0) {
  1094. set_frame_and_progress(end_frame, 1.0);
  1095. } else if (!p_from_end && !is_backward && frame == end_frame && frame_progress >= 1.0) {
  1096. set_frame_and_progress(0, 0.0);
  1097. }
  1098. }
  1099. set_process_internal(true);
  1100. notify_property_list_changed();
  1101. _queue_redraw();
  1102. }
  1103. void AnimatedSprite3D::play_backwards(const StringName &p_name) {
  1104. play(p_name, -1, true);
  1105. }
  1106. void AnimatedSprite3D::_stop_internal(bool p_reset) {
  1107. playing = false;
  1108. if (p_reset) {
  1109. custom_speed_scale = 1.0;
  1110. set_frame_and_progress(0, 0.0);
  1111. }
  1112. notify_property_list_changed();
  1113. set_process_internal(false);
  1114. }
  1115. void AnimatedSprite3D::pause() {
  1116. _stop_internal(false);
  1117. }
  1118. void AnimatedSprite3D::stop() {
  1119. _stop_internal(true);
  1120. }
  1121. double AnimatedSprite3D::_get_frame_duration() {
  1122. if (frames.is_valid() && frames->has_animation(animation)) {
  1123. return frames->get_frame_duration(animation, frame);
  1124. }
  1125. return 1.0;
  1126. }
  1127. void AnimatedSprite3D::_calc_frame_speed_scale() {
  1128. frame_speed_scale = 1.0 / _get_frame_duration();
  1129. }
  1130. void AnimatedSprite3D::set_animation(const StringName &p_name) {
  1131. if (animation == p_name) {
  1132. return;
  1133. }
  1134. animation = p_name;
  1135. emit_signal(SceneStringName(animation_changed));
  1136. if (frames.is_null()) {
  1137. animation = StringName();
  1138. stop();
  1139. ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name));
  1140. }
  1141. int frame_count = frames->get_frame_count(animation);
  1142. if (animation == StringName() || frame_count == 0) {
  1143. stop();
  1144. return;
  1145. } else if (!frames->get_animation_names().has(animation)) {
  1146. animation = StringName();
  1147. stop();
  1148. ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name));
  1149. }
  1150. if (signbit(get_playing_speed())) {
  1151. set_frame_and_progress(frame_count - 1, 1.0);
  1152. } else {
  1153. set_frame_and_progress(0, 0.0);
  1154. }
  1155. notify_property_list_changed();
  1156. _queue_redraw();
  1157. }
  1158. StringName AnimatedSprite3D::get_animation() const {
  1159. return animation;
  1160. }
  1161. PackedStringArray AnimatedSprite3D::get_configuration_warnings() const {
  1162. PackedStringArray warnings = SpriteBase3D::get_configuration_warnings();
  1163. if (frames.is_null()) {
  1164. warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Sprite Frames\" property in order for AnimatedSprite3D to display frames."));
  1165. }
  1166. return warnings;
  1167. }
  1168. #ifdef TOOLS_ENABLED
  1169. void AnimatedSprite3D::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1170. const String pf = p_function;
  1171. if (p_idx == 0 && frames.is_valid()) {
  1172. if (pf == "play" || pf == "play_backwards" || pf == "set_animation" || pf == "set_autoplay") {
  1173. List<StringName> al;
  1174. frames->get_animation_list(&al);
  1175. for (const StringName &name : al) {
  1176. r_options->push_back(String(name).quote());
  1177. }
  1178. }
  1179. }
  1180. SpriteBase3D::get_argument_options(p_function, p_idx, r_options);
  1181. }
  1182. #endif
  1183. #ifndef DISABLE_DEPRECATED
  1184. bool AnimatedSprite3D::_set(const StringName &p_name, const Variant &p_value) {
  1185. if ((p_name == SNAME("frames"))) {
  1186. set_sprite_frames(p_value);
  1187. return true;
  1188. }
  1189. return false;
  1190. }
  1191. #endif
  1192. void AnimatedSprite3D::_bind_methods() {
  1193. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
  1194. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
  1195. ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimatedSprite3D::set_animation);
  1196. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
  1197. ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimatedSprite3D::set_autoplay);
  1198. ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimatedSprite3D::get_autoplay);
  1199. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
  1200. ClassDB::bind_method(D_METHOD("play", "name", "custom_speed", "from_end"), &AnimatedSprite3D::play, DEFVAL(StringName()), DEFVAL(1.0), DEFVAL(false));
  1201. ClassDB::bind_method(D_METHOD("play_backwards", "name"), &AnimatedSprite3D::play_backwards, DEFVAL(StringName()));
  1202. ClassDB::bind_method(D_METHOD("pause"), &AnimatedSprite3D::pause);
  1203. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
  1204. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
  1205. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
  1206. ClassDB::bind_method(D_METHOD("set_frame_progress", "progress"), &AnimatedSprite3D::set_frame_progress);
  1207. ClassDB::bind_method(D_METHOD("get_frame_progress"), &AnimatedSprite3D::get_frame_progress);
  1208. ClassDB::bind_method(D_METHOD("set_frame_and_progress", "frame", "progress"), &AnimatedSprite3D::set_frame_and_progress);
  1209. ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite3D::set_speed_scale);
  1210. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite3D::get_speed_scale);
  1211. ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimatedSprite3D::get_playing_speed);
  1212. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed);
  1213. ADD_SIGNAL(MethodInfo("sprite_frames_changed"));
  1214. ADD_SIGNAL(MethodInfo("animation_changed"));
  1215. ADD_SIGNAL(MethodInfo("frame_changed"));
  1216. ADD_SIGNAL(MethodInfo("animation_looped"));
  1217. ADD_SIGNAL(MethodInfo("animation_finished"));
  1218. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sprite_frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  1219. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation", PROPERTY_HINT_ENUM, ""), "set_animation", "get_animation");
  1220. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
  1221. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  1222. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frame_progress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_frame_progress", "get_frame_progress");
  1223. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale");
  1224. }
  1225. AnimatedSprite3D::AnimatedSprite3D() {
  1226. }