sprite_3d.cpp 47 KB

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