sprite_3d.cpp 47 KB

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