sprite_3d.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. /*************************************************************************/
  2. /* sprite_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/core_string_names.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_update();
  55. for (SpriteBase3D *&E : children) {
  56. E->_propagate_color_changed();
  57. }
  58. }
  59. void SpriteBase3D::_notification(int p_what) {
  60. if (p_what == 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. }
  69. if (p_what == NOTIFICATION_EXIT_TREE) {
  70. if (parent_sprite) {
  71. parent_sprite->children.erase(pI);
  72. pI = nullptr;
  73. parent_sprite = nullptr;
  74. }
  75. }
  76. }
  77. void SpriteBase3D::set_centered(bool p_center) {
  78. centered = p_center;
  79. _queue_update();
  80. }
  81. bool SpriteBase3D::is_centered() const {
  82. return centered;
  83. }
  84. void SpriteBase3D::set_offset(const Point2 &p_offset) {
  85. offset = p_offset;
  86. _queue_update();
  87. }
  88. Point2 SpriteBase3D::get_offset() const {
  89. return offset;
  90. }
  91. void SpriteBase3D::set_flip_h(bool p_flip) {
  92. hflip = p_flip;
  93. _queue_update();
  94. }
  95. bool SpriteBase3D::is_flipped_h() const {
  96. return hflip;
  97. }
  98. void SpriteBase3D::set_flip_v(bool p_flip) {
  99. vflip = p_flip;
  100. _queue_update();
  101. }
  102. bool SpriteBase3D::is_flipped_v() const {
  103. return vflip;
  104. }
  105. void SpriteBase3D::set_modulate(const Color &p_color) {
  106. modulate = p_color;
  107. _propagate_color_changed();
  108. _queue_update();
  109. }
  110. Color SpriteBase3D::get_modulate() const {
  111. return modulate;
  112. }
  113. void SpriteBase3D::set_pixel_size(real_t p_amount) {
  114. pixel_size = p_amount;
  115. _queue_update();
  116. }
  117. real_t SpriteBase3D::get_pixel_size() const {
  118. return pixel_size;
  119. }
  120. void SpriteBase3D::set_axis(Vector3::Axis p_axis) {
  121. ERR_FAIL_INDEX(p_axis, 3);
  122. axis = p_axis;
  123. _queue_update();
  124. }
  125. Vector3::Axis SpriteBase3D::get_axis() const {
  126. return axis;
  127. }
  128. void SpriteBase3D::_im_update() {
  129. _draw();
  130. pending_update = false;
  131. //texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  132. }
  133. void SpriteBase3D::_queue_update() {
  134. if (pending_update) {
  135. return;
  136. }
  137. triangle_mesh.unref();
  138. update_gizmos();
  139. pending_update = true;
  140. call_deferred(SceneStringNames::get_singleton()->_im_update);
  141. }
  142. AABB SpriteBase3D::get_aabb() const {
  143. return aabb;
  144. }
  145. Vector<Face3> SpriteBase3D::get_faces(uint32_t p_usage_flags) const {
  146. return Vector<Face3>();
  147. }
  148. Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const {
  149. if (triangle_mesh.is_valid()) {
  150. return triangle_mesh;
  151. }
  152. Vector<Vector3> faces;
  153. faces.resize(6);
  154. Vector3 *facesw = faces.ptrw();
  155. Rect2 final_rect = get_item_rect();
  156. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  157. return Ref<TriangleMesh>();
  158. }
  159. real_t pixel_size = get_pixel_size();
  160. Vector2 vertices[4] = {
  161. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  162. (final_rect.position + final_rect.size) * pixel_size,
  163. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  164. final_rect.position * pixel_size,
  165. };
  166. int x_axis = ((axis + 1) % 3);
  167. int y_axis = ((axis + 2) % 3);
  168. if (axis != Vector3::AXIS_Z) {
  169. SWAP(x_axis, y_axis);
  170. for (int i = 0; i < 4; i++) {
  171. if (axis == Vector3::AXIS_Y) {
  172. vertices[i].y = -vertices[i].y;
  173. } else if (axis == Vector3::AXIS_X) {
  174. vertices[i].x = -vertices[i].x;
  175. }
  176. }
  177. }
  178. static const int indices[6] = {
  179. 0, 1, 2,
  180. 0, 2, 3
  181. };
  182. for (int j = 0; j < 6; j++) {
  183. int i = indices[j];
  184. Vector3 vtx;
  185. vtx[x_axis] = vertices[i][0];
  186. vtx[y_axis] = vertices[i][1];
  187. facesw[j] = vtx;
  188. }
  189. triangle_mesh = Ref<TriangleMesh>(memnew(TriangleMesh));
  190. triangle_mesh->create(faces);
  191. return triangle_mesh;
  192. }
  193. void SpriteBase3D::set_draw_flag(DrawFlags p_flag, bool p_enable) {
  194. ERR_FAIL_INDEX(p_flag, FLAG_MAX);
  195. flags[p_flag] = p_enable;
  196. _queue_update();
  197. }
  198. bool SpriteBase3D::get_draw_flag(DrawFlags p_flag) const {
  199. ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false);
  200. return flags[p_flag];
  201. }
  202. void SpriteBase3D::set_alpha_cut_mode(AlphaCutMode p_mode) {
  203. ERR_FAIL_INDEX(p_mode, 3);
  204. alpha_cut = p_mode;
  205. _queue_update();
  206. }
  207. SpriteBase3D::AlphaCutMode SpriteBase3D::get_alpha_cut_mode() const {
  208. return alpha_cut;
  209. }
  210. void SpriteBase3D::set_billboard_mode(StandardMaterial3D::BillboardMode p_mode) {
  211. ERR_FAIL_INDEX(p_mode, 3);
  212. billboard_mode = p_mode;
  213. _queue_update();
  214. }
  215. StandardMaterial3D::BillboardMode SpriteBase3D::get_billboard_mode() const {
  216. return billboard_mode;
  217. }
  218. void SpriteBase3D::_bind_methods() {
  219. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered);
  220. ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered);
  221. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset);
  222. ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset);
  223. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h);
  224. ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h);
  225. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v);
  226. ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v);
  227. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate);
  228. ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate);
  229. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size);
  230. ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size);
  231. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis);
  232. ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis);
  233. ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag);
  234. ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag);
  235. ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode);
  236. ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode);
  237. ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpriteBase3D::set_billboard_mode);
  238. ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpriteBase3D::get_billboard_mode);
  239. ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect);
  240. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh);
  241. ClassDB::bind_method(D_METHOD("_queue_update"), &SpriteBase3D::_queue_update);
  242. ClassDB::bind_method(D_METHOD("_im_update"), &SpriteBase3D::_im_update);
  243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  244. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  247. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  248. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
  249. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis");
  250. ADD_GROUP("Flags", "");
  251. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode");
  252. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT);
  253. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
  254. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
  255. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass"), "set_alpha_cut_mode", "get_alpha_cut_mode");
  256. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  257. BIND_ENUM_CONSTANT(FLAG_SHADED);
  258. BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
  259. BIND_ENUM_CONSTANT(FLAG_MAX);
  260. BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
  261. BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
  262. BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
  263. }
  264. SpriteBase3D::SpriteBase3D() {
  265. for (int i = 0; i < FLAG_MAX; i++) {
  266. flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED;
  267. }
  268. material = RenderingServer::get_singleton()->material_create();
  269. // Set defaults for material, names need to match up those in StandardMaterial3D
  270. RS::get_singleton()->material_set_param(material, "albedo", Color(1, 1, 1, 1));
  271. RS::get_singleton()->material_set_param(material, "specular", 0.5);
  272. RS::get_singleton()->material_set_param(material, "metallic", 0.0);
  273. RS::get_singleton()->material_set_param(material, "roughness", 1.0);
  274. RS::get_singleton()->material_set_param(material, "uv1_offset", Vector3(0, 0, 0));
  275. RS::get_singleton()->material_set_param(material, "uv1_scale", Vector3(1, 1, 1));
  276. RS::get_singleton()->material_set_param(material, "uv2_offset", Vector3(0, 0, 0));
  277. RS::get_singleton()->material_set_param(material, "uv2_scale", Vector3(1, 1, 1));
  278. RS::get_singleton()->material_set_param(material, "alpha_scissor_threshold", 0.98);
  279. mesh = RenderingServer::get_singleton()->mesh_create();
  280. PackedVector3Array mesh_vertices;
  281. PackedVector3Array mesh_normals;
  282. PackedFloat32Array mesh_tangents;
  283. PackedColorArray mesh_colors;
  284. PackedVector2Array mesh_uvs;
  285. PackedInt32Array indices;
  286. mesh_vertices.resize(4);
  287. mesh_normals.resize(4);
  288. mesh_tangents.resize(16);
  289. mesh_colors.resize(4);
  290. mesh_uvs.resize(4);
  291. // create basic mesh and store format information
  292. for (int i = 0; i < 4; i++) {
  293. mesh_normals.write[i] = Vector3(0.0, 0.0, 0.0);
  294. mesh_tangents.write[i * 4 + 0] = 0.0;
  295. mesh_tangents.write[i * 4 + 1] = 0.0;
  296. mesh_tangents.write[i * 4 + 2] = 0.0;
  297. mesh_tangents.write[i * 4 + 3] = 0.0;
  298. mesh_colors.write[i] = Color(1.0, 1.0, 1.0, 1.0);
  299. mesh_uvs.write[i] = Vector2(0.0, 0.0);
  300. mesh_vertices.write[i] = Vector3(0.0, 0.0, 0.0);
  301. }
  302. indices.resize(6);
  303. indices.write[0] = 0;
  304. indices.write[1] = 1;
  305. indices.write[2] = 2;
  306. indices.write[3] = 0;
  307. indices.write[4] = 2;
  308. indices.write[5] = 3;
  309. Array mesh_array;
  310. mesh_array.resize(RS::ARRAY_MAX);
  311. mesh_array[RS::ARRAY_VERTEX] = mesh_vertices;
  312. mesh_array[RS::ARRAY_NORMAL] = mesh_normals;
  313. mesh_array[RS::ARRAY_TANGENT] = mesh_tangents;
  314. mesh_array[RS::ARRAY_COLOR] = mesh_colors;
  315. mesh_array[RS::ARRAY_TEX_UV] = mesh_uvs;
  316. mesh_array[RS::ARRAY_INDEX] = indices;
  317. RS::SurfaceData sd;
  318. RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, mesh_array);
  319. mesh_surface_format = sd.format;
  320. vertex_buffer = sd.vertex_data;
  321. attribute_buffer = sd.attribute_data;
  322. sd.material = material;
  323. RS::get_singleton()->mesh_surface_make_offsets_from_format(sd.format, sd.vertex_count, sd.index_count, mesh_surface_offsets, vertex_stride, attrib_stride, skin_stride);
  324. RS::get_singleton()->mesh_add_surface(mesh, sd);
  325. set_base(mesh);
  326. }
  327. SpriteBase3D::~SpriteBase3D() {
  328. RenderingServer::get_singleton()->free(mesh);
  329. RenderingServer::get_singleton()->free(material);
  330. }
  331. ///////////////////////////////////////////
  332. void Sprite3D::_draw() {
  333. if (get_base() != get_mesh()) {
  334. set_base(get_mesh());
  335. }
  336. if (!texture.is_valid()) {
  337. set_base(RID());
  338. return;
  339. }
  340. Vector2 tsize = texture->get_size();
  341. if (tsize.x == 0 || tsize.y == 0) {
  342. return;
  343. }
  344. Rect2 base_rect;
  345. if (region) {
  346. base_rect = region_rect;
  347. } else {
  348. base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
  349. }
  350. Size2 frame_size = base_rect.size / Size2(hframes, vframes);
  351. Point2 frame_offset = Point2(frame % hframes, frame / hframes);
  352. frame_offset *= frame_size;
  353. Point2 dest_offset = get_offset();
  354. if (is_centered()) {
  355. dest_offset -= frame_size / 2;
  356. }
  357. Rect2 src_rect(base_rect.position + frame_offset, frame_size);
  358. Rect2 final_dst_rect(dest_offset, frame_size);
  359. Rect2 final_rect;
  360. Rect2 final_src_rect;
  361. if (!texture->get_rect_region(final_dst_rect, src_rect, final_rect, final_src_rect)) {
  362. return;
  363. }
  364. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  365. return;
  366. }
  367. Color color = _get_color_accum();
  368. real_t pixel_size = get_pixel_size();
  369. Vector2 vertices[4] = {
  370. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  371. (final_rect.position + final_rect.size) * pixel_size,
  372. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  373. final_rect.position * pixel_size,
  374. };
  375. Vector2 src_tsize = tsize;
  376. // Properly setup UVs for impostor textures (AtlasTexture).
  377. Ref<AtlasTexture> atlas_tex = texture;
  378. if (atlas_tex != nullptr) {
  379. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  380. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  381. }
  382. Vector2 uvs[4] = {
  383. final_src_rect.position / src_tsize,
  384. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  385. (final_src_rect.position + final_src_rect.size) / src_tsize,
  386. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  387. };
  388. if (is_flipped_h()) {
  389. SWAP(uvs[0], uvs[1]);
  390. SWAP(uvs[2], uvs[3]);
  391. }
  392. if (is_flipped_v()) {
  393. SWAP(uvs[0], uvs[3]);
  394. SWAP(uvs[1], uvs[2]);
  395. }
  396. Vector3 normal;
  397. int axis = get_axis();
  398. normal[axis] = 1.0;
  399. Plane tangent;
  400. if (axis == Vector3::AXIS_X) {
  401. tangent = Plane(0, 0, -1, 1);
  402. } else {
  403. tangent = Plane(1, 0, 0, 1);
  404. }
  405. int x_axis = ((axis + 1) % 3);
  406. int y_axis = ((axis + 2) % 3);
  407. if (axis != Vector3::AXIS_Z) {
  408. SWAP(x_axis, y_axis);
  409. for (int i = 0; i < 4; i++) {
  410. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  411. //SWAP(vertices[i].x,vertices[i].y);
  412. if (axis == Vector3::AXIS_Y) {
  413. vertices[i].y = -vertices[i].y;
  414. } else if (axis == Vector3::AXIS_X) {
  415. vertices[i].x = -vertices[i].x;
  416. }
  417. }
  418. }
  419. AABB aabb;
  420. // Everything except position and UV is compressed
  421. uint8_t *vertex_write_buffer = vertex_buffer.ptrw();
  422. uint8_t *attribute_write_buffer = attribute_buffer.ptrw();
  423. uint32_t v_normal;
  424. {
  425. Vector3 n = normal * Vector3(0.5, 0.5, 0.5) + Vector3(0.5, 0.5, 0.5);
  426. uint32_t value = 0;
  427. value |= CLAMP(int(n.x * 1023.0), 0, 1023);
  428. value |= CLAMP(int(n.y * 1023.0), 0, 1023) << 10;
  429. value |= CLAMP(int(n.z * 1023.0), 0, 1023) << 20;
  430. v_normal = value;
  431. }
  432. uint32_t v_tangent;
  433. {
  434. Plane t = tangent;
  435. uint32_t value = 0;
  436. value |= CLAMP(int((t.normal.x * 0.5 + 0.5) * 1023.0), 0, 1023);
  437. value |= CLAMP(int((t.normal.y * 0.5 + 0.5) * 1023.0), 0, 1023) << 10;
  438. value |= CLAMP(int((t.normal.z * 0.5 + 0.5) * 1023.0), 0, 1023) << 20;
  439. if (t.d > 0) {
  440. value |= 3 << 30;
  441. }
  442. v_tangent = value;
  443. }
  444. uint8_t v_color[4] = {
  445. uint8_t(CLAMP(color.r * 255.0, 0.0, 255.0)),
  446. uint8_t(CLAMP(color.g * 255.0, 0.0, 255.0)),
  447. uint8_t(CLAMP(color.b * 255.0, 0.0, 255.0)),
  448. uint8_t(CLAMP(color.a * 255.0, 0.0, 255.0))
  449. };
  450. for (int i = 0; i < 4; i++) {
  451. Vector3 vtx;
  452. vtx[x_axis] = vertices[i][0];
  453. vtx[y_axis] = vertices[i][1];
  454. if (i == 0) {
  455. aabb.position = vtx;
  456. aabb.size = Vector3();
  457. } else {
  458. aabb.expand_to(vtx);
  459. }
  460. float v_uv[2] = { (float)uvs[i].x, (float)uvs[i].y };
  461. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_TEX_UV]], v_uv, 8);
  462. float v_vertex[3] = { (float)vtx.x, (float)vtx.y, (float)vtx.z };
  463. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
  464. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_NORMAL]], &v_normal, 4);
  465. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_TANGENT]], &v_tangent, 4);
  466. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_COLOR]], v_color, 4);
  467. }
  468. RID mesh = get_mesh();
  469. RS::get_singleton()->mesh_surface_update_vertex_region(mesh, 0, 0, vertex_buffer);
  470. RS::get_singleton()->mesh_surface_update_attribute_region(mesh, 0, 0, attribute_buffer);
  471. RS::get_singleton()->mesh_set_custom_aabb(mesh, aabb);
  472. set_aabb(aabb);
  473. RID shader_rid;
  474. StandardMaterial3D::get_material_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS, get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y, &shader_rid);
  475. if (last_shader != shader_rid) {
  476. RS::get_singleton()->material_set_shader(get_material(), shader_rid);
  477. last_shader = shader_rid;
  478. }
  479. if (last_texture != texture->get_rid()) {
  480. RS::get_singleton()->material_set_param(get_material(), "texture_albedo", texture->get_rid());
  481. last_texture = texture->get_rid();
  482. }
  483. }
  484. void Sprite3D::set_texture(const Ref<Texture2D> &p_texture) {
  485. if (p_texture == texture) {
  486. return;
  487. }
  488. if (texture.is_valid()) {
  489. texture->disconnect(CoreStringNames::get_singleton()->changed, Callable(this, "_queue_update"));
  490. }
  491. texture = p_texture;
  492. if (texture.is_valid()) {
  493. texture->connect(CoreStringNames::get_singleton()->changed, Callable(this, "_queue_update"));
  494. }
  495. _queue_update();
  496. emit_signal(SceneStringNames::get_singleton()->texture_changed);
  497. }
  498. Ref<Texture2D> Sprite3D::get_texture() const {
  499. return texture;
  500. }
  501. void Sprite3D::set_region_enabled(bool p_region) {
  502. if (p_region == region) {
  503. return;
  504. }
  505. region = p_region;
  506. _queue_update();
  507. }
  508. bool Sprite3D::is_region_enabled() const {
  509. return region;
  510. }
  511. void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
  512. bool changed = region_rect != p_region_rect;
  513. region_rect = p_region_rect;
  514. if (region && changed) {
  515. _queue_update();
  516. }
  517. }
  518. Rect2 Sprite3D::get_region_rect() const {
  519. return region_rect;
  520. }
  521. void Sprite3D::set_frame(int p_frame) {
  522. ERR_FAIL_INDEX(p_frame, int64_t(vframes) * hframes);
  523. frame = p_frame;
  524. _queue_update();
  525. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  526. }
  527. int Sprite3D::get_frame() const {
  528. return frame;
  529. }
  530. void Sprite3D::set_frame_coords(const Vector2i &p_coord) {
  531. ERR_FAIL_INDEX(p_coord.x, hframes);
  532. ERR_FAIL_INDEX(p_coord.y, vframes);
  533. set_frame(p_coord.y * hframes + p_coord.x);
  534. }
  535. Vector2i Sprite3D::get_frame_coords() const {
  536. return Vector2i(frame % hframes, frame / hframes);
  537. }
  538. void Sprite3D::set_vframes(int p_amount) {
  539. ERR_FAIL_COND(p_amount < 1);
  540. vframes = p_amount;
  541. _queue_update();
  542. notify_property_list_changed();
  543. }
  544. int Sprite3D::get_vframes() const {
  545. return vframes;
  546. }
  547. void Sprite3D::set_hframes(int p_amount) {
  548. ERR_FAIL_COND(p_amount < 1);
  549. hframes = p_amount;
  550. _queue_update();
  551. notify_property_list_changed();
  552. }
  553. int Sprite3D::get_hframes() const {
  554. return hframes;
  555. }
  556. Rect2 Sprite3D::get_item_rect() const {
  557. if (texture.is_null()) {
  558. return Rect2(0, 0, 1, 1);
  559. }
  560. /*
  561. if (texture.is_null())
  562. return CanvasItem::get_item_rect();
  563. */
  564. Size2 s;
  565. if (region) {
  566. s = region_rect.size;
  567. } else {
  568. s = texture->get_size();
  569. s = s / Point2(hframes, vframes);
  570. }
  571. Point2 ofs = get_offset();
  572. if (is_centered()) {
  573. ofs -= s / 2;
  574. }
  575. if (s == Size2(0, 0)) {
  576. s = Size2(1, 1);
  577. }
  578. return Rect2(ofs, s);
  579. }
  580. void Sprite3D::_validate_property(PropertyInfo &property) const {
  581. if (property.name == "frame") {
  582. property.hint = PROPERTY_HINT_RANGE;
  583. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  584. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  585. }
  586. if (property.name == "frame_coords") {
  587. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  588. }
  589. SpriteBase3D::_validate_property(property);
  590. }
  591. void Sprite3D::_bind_methods() {
  592. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
  593. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
  594. ClassDB::bind_method(D_METHOD("set_region_enabled", "enabled"), &Sprite3D::set_region_enabled);
  595. ClassDB::bind_method(D_METHOD("is_region_enabled"), &Sprite3D::is_region_enabled);
  596. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
  597. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
  598. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
  599. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
  600. ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords);
  601. ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords);
  602. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
  603. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
  604. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
  605. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
  606. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  607. ADD_GROUP("Animation", "");
  608. ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  609. ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  610. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  611. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_frame_coords", "get_frame_coords");
  612. ADD_GROUP("Region", "region_");
  613. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region_enabled", "is_region_enabled");
  614. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  615. ADD_SIGNAL(MethodInfo("frame_changed"));
  616. ADD_SIGNAL(MethodInfo("texture_changed"));
  617. }
  618. Sprite3D::Sprite3D() {
  619. }
  620. ////////////////////////////////////////
  621. void AnimatedSprite3D::_draw() {
  622. if (get_base() != get_mesh()) {
  623. set_base(get_mesh());
  624. }
  625. if (frames.is_null()) {
  626. return;
  627. }
  628. if (frame < 0) {
  629. return;
  630. }
  631. if (!frames->has_animation(animation)) {
  632. return;
  633. }
  634. Ref<Texture2D> texture = frames->get_frame(animation, frame);
  635. if (!texture.is_valid()) {
  636. set_base(RID());
  637. return; //no texuture no life
  638. }
  639. Size2 tsize = texture->get_size();
  640. if (tsize.x == 0 || tsize.y == 0) {
  641. return;
  642. }
  643. Rect2 src_rect;
  644. src_rect.size = tsize;
  645. Point2 ofs = get_offset();
  646. if (is_centered()) {
  647. ofs -= tsize / 2;
  648. }
  649. Rect2 dst_rect(ofs, tsize);
  650. Rect2 final_rect;
  651. Rect2 final_src_rect;
  652. if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect)) {
  653. return;
  654. }
  655. if (final_rect.size.x == 0 || final_rect.size.y == 0) {
  656. return;
  657. }
  658. Color color = _get_color_accum();
  659. real_t pixel_size = get_pixel_size();
  660. Vector2 vertices[4] = {
  661. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  662. (final_rect.position + final_rect.size) * pixel_size,
  663. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  664. final_rect.position * pixel_size,
  665. };
  666. Vector2 src_tsize = tsize;
  667. // Properly setup UVs for impostor textures (AtlasTexture).
  668. Ref<AtlasTexture> atlas_tex = texture;
  669. if (atlas_tex != nullptr) {
  670. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  671. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  672. }
  673. Vector2 uvs[4] = {
  674. final_src_rect.position / src_tsize,
  675. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  676. (final_src_rect.position + final_src_rect.size) / src_tsize,
  677. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  678. };
  679. if (is_flipped_h()) {
  680. SWAP(uvs[0], uvs[1]);
  681. SWAP(uvs[2], uvs[3]);
  682. }
  683. if (is_flipped_v()) {
  684. SWAP(uvs[0], uvs[3]);
  685. SWAP(uvs[1], uvs[2]);
  686. }
  687. Vector3 normal;
  688. int axis = get_axis();
  689. normal[axis] = 1.0;
  690. Plane tangent;
  691. if (axis == Vector3::AXIS_X) {
  692. tangent = Plane(0, 0, -1, -1);
  693. } else {
  694. tangent = Plane(1, 0, 0, -1);
  695. }
  696. int x_axis = ((axis + 1) % 3);
  697. int y_axis = ((axis + 2) % 3);
  698. if (axis != Vector3::AXIS_Z) {
  699. SWAP(x_axis, y_axis);
  700. for (int i = 0; i < 4; i++) {
  701. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  702. //SWAP(vertices[i].x,vertices[i].y);
  703. if (axis == Vector3::AXIS_Y) {
  704. vertices[i].y = -vertices[i].y;
  705. } else if (axis == Vector3::AXIS_X) {
  706. vertices[i].x = -vertices[i].x;
  707. }
  708. }
  709. }
  710. AABB aabb;
  711. // Everything except position and UV is compressed
  712. uint8_t *vertex_write_buffer = vertex_buffer.ptrw();
  713. uint8_t *attribute_write_buffer = attribute_buffer.ptrw();
  714. uint32_t v_normal;
  715. {
  716. Vector3 n = normal * Vector3(0.5, 0.5, 0.5) + Vector3(0.5, 0.5, 0.5);
  717. uint32_t value = 0;
  718. value |= CLAMP(int(n.x * 1023.0), 0, 1023);
  719. value |= CLAMP(int(n.y * 1023.0), 0, 1023) << 10;
  720. value |= CLAMP(int(n.z * 1023.0), 0, 1023) << 20;
  721. v_normal = value;
  722. }
  723. uint32_t v_tangent;
  724. {
  725. Plane t = tangent;
  726. uint32_t value = 0;
  727. value |= CLAMP(int((t.normal.x * 0.5 + 0.5) * 1023.0), 0, 1023);
  728. value |= CLAMP(int((t.normal.y * 0.5 + 0.5) * 1023.0), 0, 1023) << 10;
  729. value |= CLAMP(int((t.normal.z * 0.5 + 0.5) * 1023.0), 0, 1023) << 20;
  730. if (t.d > 0) {
  731. value |= 3 << 30;
  732. }
  733. v_tangent = value;
  734. }
  735. uint8_t v_color[4] = {
  736. uint8_t(CLAMP(color.r * 255.0, 0.0, 255.0)),
  737. uint8_t(CLAMP(color.g * 255.0, 0.0, 255.0)),
  738. uint8_t(CLAMP(color.b * 255.0, 0.0, 255.0)),
  739. uint8_t(CLAMP(color.a * 255.0, 0.0, 255.0))
  740. };
  741. for (int i = 0; i < 4; i++) {
  742. Vector3 vtx;
  743. vtx[x_axis] = vertices[i][0];
  744. vtx[y_axis] = vertices[i][1];
  745. if (i == 0) {
  746. aabb.position = vtx;
  747. aabb.size = Vector3();
  748. } else {
  749. aabb.expand_to(vtx);
  750. }
  751. float v_uv[2] = { (float)uvs[i].x, (float)uvs[i].y };
  752. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_TEX_UV]], v_uv, 8);
  753. float v_vertex[3] = { (float)vtx.x, (float)vtx.y, (float)vtx.z };
  754. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3);
  755. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_NORMAL]], &v_normal, 4);
  756. memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_TANGENT]], &v_tangent, 4);
  757. memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_COLOR]], v_color, 4);
  758. }
  759. RID mesh = get_mesh();
  760. RS::get_singleton()->mesh_surface_update_vertex_region(mesh, 0, 0, vertex_buffer);
  761. RS::get_singleton()->mesh_surface_update_attribute_region(mesh, 0, 0, attribute_buffer);
  762. RS::get_singleton()->mesh_set_custom_aabb(mesh, aabb);
  763. set_aabb(aabb);
  764. RID shader_rid;
  765. StandardMaterial3D::get_material_for_2d(get_draw_flag(FLAG_SHADED), get_draw_flag(FLAG_TRANSPARENT), get_draw_flag(FLAG_DOUBLE_SIDED), get_alpha_cut_mode() == ALPHA_CUT_DISCARD, get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS, get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y, &shader_rid);
  766. if (last_shader != shader_rid) {
  767. RS::get_singleton()->material_set_shader(get_material(), shader_rid);
  768. last_shader = shader_rid;
  769. }
  770. if (last_texture != texture->get_rid()) {
  771. RS::get_singleton()->material_set_param(get_material(), "texture_albedo", texture->get_rid());
  772. last_texture = texture->get_rid();
  773. }
  774. }
  775. void AnimatedSprite3D::_validate_property(PropertyInfo &property) const {
  776. if (!frames.is_valid()) {
  777. return;
  778. }
  779. if (property.name == "animation") {
  780. property.hint = PROPERTY_HINT_ENUM;
  781. List<StringName> names;
  782. frames->get_animation_list(&names);
  783. names.sort_custom<StringName::AlphCompare>();
  784. bool current_found = false;
  785. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  786. if (E->prev()) {
  787. property.hint_string += ",";
  788. }
  789. property.hint_string += String(E->get());
  790. if (animation == E->get()) {
  791. current_found = true;
  792. }
  793. }
  794. if (!current_found) {
  795. if (property.hint_string.is_empty()) {
  796. property.hint_string = String(animation);
  797. } else {
  798. property.hint_string = String(animation) + "," + property.hint_string;
  799. }
  800. }
  801. }
  802. if (property.name == "frame") {
  803. property.hint = PROPERTY_HINT_RANGE;
  804. if (frames->has_animation(animation) && frames->get_frame_count(animation) > 1) {
  805. property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  806. }
  807. property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS;
  808. }
  809. SpriteBase3D::_validate_property(property);
  810. }
  811. void AnimatedSprite3D::_notification(int p_what) {
  812. switch (p_what) {
  813. case NOTIFICATION_INTERNAL_PROCESS: {
  814. if (frames.is_null()) {
  815. return;
  816. }
  817. if (!frames->has_animation(animation)) {
  818. return;
  819. }
  820. if (frame < 0) {
  821. return;
  822. }
  823. float speed = frames->get_animation_speed(animation);
  824. if (speed == 0) {
  825. return; //do nothing
  826. }
  827. double remaining = get_process_delta_time();
  828. while (remaining) {
  829. if (timeout <= 0) {
  830. timeout = 1.0 / speed;
  831. int fc = frames->get_frame_count(animation);
  832. if (frame >= fc - 1) {
  833. if (frames->get_animation_loop(animation)) {
  834. frame = 0;
  835. } else {
  836. frame = fc - 1;
  837. }
  838. emit_signal(SceneStringNames::get_singleton()->animation_finished);
  839. } else {
  840. frame++;
  841. }
  842. _queue_update();
  843. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  844. }
  845. double to_process = MIN(timeout, remaining);
  846. remaining -= to_process;
  847. timeout -= to_process;
  848. }
  849. } break;
  850. }
  851. }
  852. void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  853. if (frames.is_valid()) {
  854. frames->disconnect("changed", Callable(this, "_res_changed"));
  855. }
  856. frames = p_frames;
  857. if (frames.is_valid()) {
  858. frames->connect("changed", Callable(this, "_res_changed"));
  859. }
  860. if (!frames.is_valid()) {
  861. frame = 0;
  862. } else {
  863. set_frame(frame);
  864. }
  865. notify_property_list_changed();
  866. _reset_timeout();
  867. _queue_update();
  868. update_configuration_warnings();
  869. }
  870. Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
  871. return frames;
  872. }
  873. void AnimatedSprite3D::set_frame(int p_frame) {
  874. if (!frames.is_valid()) {
  875. return;
  876. }
  877. if (frames->has_animation(animation)) {
  878. int limit = frames->get_frame_count(animation);
  879. if (p_frame >= limit) {
  880. p_frame = limit - 1;
  881. }
  882. }
  883. if (p_frame < 0) {
  884. p_frame = 0;
  885. }
  886. if (frame == p_frame) {
  887. return;
  888. }
  889. frame = p_frame;
  890. _reset_timeout();
  891. _queue_update();
  892. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  893. }
  894. int AnimatedSprite3D::get_frame() const {
  895. return frame;
  896. }
  897. Rect2 AnimatedSprite3D::get_item_rect() const {
  898. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  899. return Rect2(0, 0, 1, 1);
  900. }
  901. Ref<Texture2D> t;
  902. if (animation) {
  903. t = frames->get_frame(animation, frame);
  904. }
  905. if (t.is_null()) {
  906. return Rect2(0, 0, 1, 1);
  907. }
  908. Size2 s = t->get_size();
  909. Point2 ofs = get_offset();
  910. if (centered) {
  911. ofs -= s / 2;
  912. }
  913. if (s == Size2(0, 0)) {
  914. s = Size2(1, 1);
  915. }
  916. return Rect2(ofs, s);
  917. }
  918. void AnimatedSprite3D::_res_changed() {
  919. set_frame(frame);
  920. _queue_update();
  921. }
  922. void AnimatedSprite3D::_set_playing(bool p_playing) {
  923. if (playing == p_playing) {
  924. return;
  925. }
  926. playing = p_playing;
  927. _reset_timeout();
  928. set_process_internal(playing);
  929. }
  930. bool AnimatedSprite3D::_is_playing() const {
  931. return playing;
  932. }
  933. void AnimatedSprite3D::play(const StringName &p_animation) {
  934. if (p_animation) {
  935. set_animation(p_animation);
  936. }
  937. _set_playing(true);
  938. }
  939. void AnimatedSprite3D::stop() {
  940. _set_playing(false);
  941. }
  942. bool AnimatedSprite3D::is_playing() const {
  943. return playing;
  944. }
  945. void AnimatedSprite3D::_reset_timeout() {
  946. if (!playing) {
  947. return;
  948. }
  949. if (frames.is_valid() && frames->has_animation(animation)) {
  950. float speed = frames->get_animation_speed(animation);
  951. if (speed > 0) {
  952. timeout = 1.0 / speed;
  953. } else {
  954. timeout = 0;
  955. }
  956. } else {
  957. timeout = 0;
  958. }
  959. }
  960. void AnimatedSprite3D::set_animation(const StringName &p_animation) {
  961. if (animation == p_animation) {
  962. return;
  963. }
  964. animation = p_animation;
  965. _reset_timeout();
  966. set_frame(0);
  967. notify_property_list_changed();
  968. _queue_update();
  969. }
  970. StringName AnimatedSprite3D::get_animation() const {
  971. return animation;
  972. }
  973. TypedArray<String> AnimatedSprite3D::get_configuration_warnings() const {
  974. TypedArray<String> warnings = SpriteBase3D::get_configuration_warnings();
  975. if (frames.is_null()) {
  976. warnings.push_back(TTR("A SpriteFrames resource must be created or set in the \"Frames\" property in order for AnimatedSprite3D to display frames."));
  977. }
  978. return warnings;
  979. }
  980. void AnimatedSprite3D::_bind_methods() {
  981. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
  982. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
  983. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite3D::set_animation);
  984. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
  985. ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite3D::_set_playing);
  986. ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite3D::_is_playing);
  987. ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
  988. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
  989. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
  990. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
  991. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
  992. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed);
  993. ADD_SIGNAL(MethodInfo("frame_changed"));
  994. ADD_SIGNAL(MethodInfo("animation_finished"));
  995. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  996. ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
  997. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame");
  998. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
  999. }
  1000. AnimatedSprite3D::AnimatedSprite3D() {
  1001. }