sprite_3d.cpp 47 KB

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