sprite_3d.cpp 46 KB

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