sprite_3d.cpp 45 KB

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