sprite_3d.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*************************************************************************/
  2. /* sprite_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. if (parent_sprite)
  37. color_accum = parent_sprite->_get_color_accum();
  38. else
  39. color_accum = Color(1, 1, 1, 1);
  40. color_accum.r *= modulate.r;
  41. color_accum.g *= modulate.g;
  42. color_accum.b *= modulate.b;
  43. color_accum.a *= modulate.a;
  44. color_dirty = false;
  45. return color_accum;
  46. }
  47. void SpriteBase3D::_propagate_color_changed() {
  48. if (color_dirty)
  49. return;
  50. color_dirty = true;
  51. _queue_update();
  52. for (List<SpriteBase3D *>::Element *E = children.front(); E; E = E->next()) {
  53. E->get()->_propagate_color_changed();
  54. }
  55. }
  56. void SpriteBase3D::_notification(int p_what) {
  57. if (p_what == NOTIFICATION_ENTER_TREE) {
  58. if (!pending_update)
  59. _im_update();
  60. parent_sprite = Object::cast_to<SpriteBase3D>(get_parent());
  61. if (parent_sprite) {
  62. pI = parent_sprite->children.push_back(this);
  63. }
  64. }
  65. if (p_what == NOTIFICATION_EXIT_TREE) {
  66. if (parent_sprite) {
  67. parent_sprite->children.erase(pI);
  68. pI = NULL;
  69. parent_sprite = NULL;
  70. }
  71. }
  72. }
  73. void SpriteBase3D::set_centered(bool p_center) {
  74. centered = p_center;
  75. _queue_update();
  76. }
  77. bool SpriteBase3D::is_centered() const {
  78. return centered;
  79. }
  80. void SpriteBase3D::set_offset(const Point2 &p_offset) {
  81. offset = p_offset;
  82. _queue_update();
  83. }
  84. Point2 SpriteBase3D::get_offset() const {
  85. return offset;
  86. }
  87. void SpriteBase3D::set_flip_h(bool p_flip) {
  88. hflip = p_flip;
  89. _queue_update();
  90. }
  91. bool SpriteBase3D::is_flipped_h() const {
  92. return hflip;
  93. }
  94. void SpriteBase3D::set_flip_v(bool p_flip) {
  95. vflip = p_flip;
  96. _queue_update();
  97. }
  98. bool SpriteBase3D::is_flipped_v() const {
  99. return vflip;
  100. }
  101. void SpriteBase3D::set_modulate(const Color &p_color) {
  102. modulate = p_color;
  103. _propagate_color_changed();
  104. _queue_update();
  105. }
  106. Color SpriteBase3D::get_modulate() const {
  107. return modulate;
  108. }
  109. void SpriteBase3D::set_pixel_size(float p_amount) {
  110. pixel_size = p_amount;
  111. _queue_update();
  112. }
  113. float SpriteBase3D::get_pixel_size() const {
  114. return pixel_size;
  115. }
  116. void SpriteBase3D::set_opacity(float p_amount) {
  117. opacity = p_amount;
  118. _queue_update();
  119. }
  120. float SpriteBase3D::get_opacity() const {
  121. return opacity;
  122. }
  123. void SpriteBase3D::set_axis(Vector3::Axis p_axis) {
  124. axis = p_axis;
  125. _queue_update();
  126. }
  127. Vector3::Axis SpriteBase3D::get_axis() const {
  128. return axis;
  129. }
  130. void SpriteBase3D::_im_update() {
  131. _draw();
  132. pending_update = false;
  133. //texture->draw_rect_region(ci,dst_rect,src_rect,modulate);
  134. }
  135. void SpriteBase3D::_queue_update() {
  136. if (pending_update)
  137. return;
  138. triangle_mesh.unref();
  139. update_gizmo();
  140. pending_update = true;
  141. call_deferred(SceneStringNames::get_singleton()->_im_update);
  142. }
  143. AABB SpriteBase3D::get_aabb() const {
  144. return aabb;
  145. }
  146. PoolVector<Face3> SpriteBase3D::get_faces(uint32_t p_usage_flags) const {
  147. return PoolVector<Face3>();
  148. }
  149. Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const {
  150. if (triangle_mesh.is_valid())
  151. return triangle_mesh;
  152. PoolVector<Vector3> faces;
  153. faces.resize(6);
  154. PoolVector<Vector3>::Write facesw = faces.write();
  155. Rect2 final_rect = get_item_rect();
  156. if (final_rect.size.x == 0 || final_rect.size.y == 0)
  157. return Ref<TriangleMesh>();
  158. float pixel_size = get_pixel_size();
  159. Vector2 vertices[4] = {
  160. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  161. (final_rect.position + final_rect.size) * pixel_size,
  162. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  163. final_rect.position * pixel_size,
  164. };
  165. int x_axis = ((axis + 1) % 3);
  166. int y_axis = ((axis + 2) % 3);
  167. if (axis != Vector3::AXIS_Z) {
  168. SWAP(x_axis, y_axis);
  169. for (int i = 0; i < 4; i++) {
  170. if (axis == Vector3::AXIS_Y) {
  171. vertices[i].y = -vertices[i].y;
  172. } else if (axis == Vector3::AXIS_X) {
  173. vertices[i].x = -vertices[i].x;
  174. }
  175. }
  176. }
  177. static const int indices[6] = {
  178. 0, 1, 2,
  179. 0, 2, 3
  180. };
  181. for (int j = 0; j < 6; j++) {
  182. int i = indices[j];
  183. Vector3 vtx;
  184. vtx[x_axis] = vertices[i][0];
  185. vtx[y_axis] = vertices[i][1];
  186. facesw[j] = vtx;
  187. }
  188. facesw = PoolVector<Vector3>::Write();
  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::_bind_methods() {
  211. ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered);
  212. ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered);
  213. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset);
  214. ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset);
  215. ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h);
  216. ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h);
  217. ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v);
  218. ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v);
  219. ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate);
  220. ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate);
  221. ClassDB::bind_method(D_METHOD("set_opacity", "opacity"), &SpriteBase3D::set_opacity);
  222. ClassDB::bind_method(D_METHOD("get_opacity"), &SpriteBase3D::get_opacity);
  223. ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size);
  224. ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size);
  225. ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis);
  226. ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis);
  227. ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag);
  228. ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag);
  229. ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode);
  230. ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode);
  231. ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect);
  232. ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh);
  233. ClassDB::bind_method(D_METHOD("_queue_update"), &SpriteBase3D::_queue_update);
  234. ClassDB::bind_method(D_METHOD("_im_update"), &SpriteBase3D::_im_update);
  235. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
  236. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  237. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  238. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  239. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate");
  240. ADD_PROPERTY(PropertyInfo(Variant::REAL, "opacity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_opacity", "get_opacity");
  241. ADD_PROPERTY(PropertyInfo(Variant::REAL, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0001"), "set_pixel_size", "get_pixel_size");
  242. ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis");
  243. ADD_GROUP("Flags", "");
  244. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT);
  245. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED);
  246. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED);
  247. ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass"), "set_alpha_cut_mode", "get_alpha_cut_mode");
  248. BIND_ENUM_CONSTANT(FLAG_TRANSPARENT);
  249. BIND_ENUM_CONSTANT(FLAG_SHADED);
  250. BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
  251. BIND_ENUM_CONSTANT(FLAG_MAX);
  252. BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED);
  253. BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD);
  254. BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS);
  255. }
  256. SpriteBase3D::SpriteBase3D() {
  257. color_dirty = true;
  258. centered = true;
  259. hflip = false;
  260. vflip = false;
  261. parent_sprite = NULL;
  262. pI = NULL;
  263. for (int i = 0; i < FLAG_MAX; i++)
  264. flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED;
  265. alpha_cut = ALPHA_CUT_DISABLED;
  266. axis = Vector3::AXIS_Z;
  267. pixel_size = 0.01;
  268. modulate = Color(1, 1, 1, 1);
  269. pending_update = false;
  270. opacity = 1.0;
  271. immediate = VisualServer::get_singleton()->immediate_create();
  272. set_base(immediate);
  273. }
  274. SpriteBase3D::~SpriteBase3D() {
  275. VisualServer::get_singleton()->free(immediate);
  276. }
  277. ///////////////////////////////////////////
  278. void Sprite3D::_draw() {
  279. RID immediate = get_immediate();
  280. VS::get_singleton()->immediate_clear(immediate);
  281. if (!texture.is_valid())
  282. return; //no texuture no life
  283. Vector2 tsize = texture->get_size();
  284. if (tsize.x == 0 || tsize.y == 0)
  285. return;
  286. Size2i s;
  287. Rect2 src_rect;
  288. if (region) {
  289. s = region_rect.size;
  290. src_rect = region_rect;
  291. } else {
  292. s = texture->get_size();
  293. s = s / Size2(hframes, vframes);
  294. src_rect.size = s;
  295. src_rect.position.x += (frame % hframes) * s.x;
  296. src_rect.position.y += (frame / hframes) * s.y;
  297. }
  298. Point2 ofs = get_offset();
  299. if (is_centered())
  300. ofs -= s / 2;
  301. Rect2 dst_rect(ofs, s);
  302. Rect2 final_rect;
  303. Rect2 final_src_rect;
  304. if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect))
  305. return;
  306. if (final_rect.size.x == 0 || final_rect.size.y == 0)
  307. return;
  308. Color color = _get_color_accum();
  309. color.a *= get_opacity();
  310. float pixel_size = get_pixel_size();
  311. Vector2 vertices[4] = {
  312. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  313. (final_rect.position + final_rect.size) * pixel_size,
  314. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  315. final_rect.position * pixel_size,
  316. };
  317. Vector2 src_tsize = tsize;
  318. // Properly setup UVs for impostor textures (AtlasTexture).
  319. Ref<AtlasTexture> atlas_tex = texture;
  320. if (atlas_tex != NULL) {
  321. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  322. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  323. }
  324. Vector2 uvs[4] = {
  325. final_src_rect.position / src_tsize,
  326. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  327. (final_src_rect.position + final_src_rect.size) / src_tsize,
  328. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  329. };
  330. if (is_flipped_h()) {
  331. SWAP(uvs[0], uvs[1]);
  332. SWAP(uvs[2], uvs[3]);
  333. }
  334. if (is_flipped_v()) {
  335. SWAP(uvs[0], uvs[3]);
  336. SWAP(uvs[1], uvs[2]);
  337. }
  338. Vector3 normal;
  339. int axis = get_axis();
  340. normal[axis] = 1.0;
  341. Plane tangent;
  342. if (axis == Vector3::AXIS_X) {
  343. tangent = Plane(0, 0, -1, 1);
  344. } else {
  345. tangent = Plane(1, 0, 0, 1);
  346. }
  347. RID mat = SpatialMaterial::get_material_rid_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);
  348. VS::get_singleton()->immediate_set_material(immediate, mat);
  349. VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid());
  350. int x_axis = ((axis + 1) % 3);
  351. int y_axis = ((axis + 2) % 3);
  352. if (axis != Vector3::AXIS_Z) {
  353. SWAP(x_axis, y_axis);
  354. for (int i = 0; i < 4; i++) {
  355. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  356. //SWAP(vertices[i].x,vertices[i].y);
  357. if (axis == Vector3::AXIS_Y) {
  358. vertices[i].y = -vertices[i].y;
  359. } else if (axis == Vector3::AXIS_X) {
  360. vertices[i].x = -vertices[i].x;
  361. }
  362. }
  363. }
  364. AABB aabb;
  365. for (int i = 0; i < 4; i++) {
  366. VS::get_singleton()->immediate_normal(immediate, normal);
  367. VS::get_singleton()->immediate_tangent(immediate, tangent);
  368. VS::get_singleton()->immediate_color(immediate, color);
  369. VS::get_singleton()->immediate_uv(immediate, uvs[i]);
  370. Vector3 vtx;
  371. vtx[x_axis] = vertices[i][0];
  372. vtx[y_axis] = vertices[i][1];
  373. VS::get_singleton()->immediate_vertex(immediate, vtx);
  374. if (i == 0) {
  375. aabb.position = vtx;
  376. aabb.size = Vector3();
  377. } else {
  378. aabb.expand_to(vtx);
  379. }
  380. }
  381. set_aabb(aabb);
  382. VS::get_singleton()->immediate_end(immediate);
  383. }
  384. void Sprite3D::set_texture(const Ref<Texture> &p_texture) {
  385. if (p_texture == texture)
  386. return;
  387. if (texture.is_valid()) {
  388. texture->disconnect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  389. }
  390. texture = p_texture;
  391. if (texture.is_valid()) {
  392. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  393. texture->connect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  394. }
  395. _queue_update();
  396. }
  397. Ref<Texture> Sprite3D::get_texture() const {
  398. return texture;
  399. }
  400. void Sprite3D::set_region(bool p_region) {
  401. if (p_region == region)
  402. return;
  403. region = p_region;
  404. _queue_update();
  405. }
  406. bool Sprite3D::is_region() const {
  407. return region;
  408. }
  409. void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
  410. bool changed = region_rect != p_region_rect;
  411. region_rect = p_region_rect;
  412. if (region && changed) {
  413. _queue_update();
  414. }
  415. }
  416. Rect2 Sprite3D::get_region_rect() const {
  417. return region_rect;
  418. }
  419. void Sprite3D::set_frame(int p_frame) {
  420. ERR_FAIL_INDEX(p_frame, vframes * hframes);
  421. if (frame != p_frame)
  422. frame = p_frame;
  423. _queue_update();
  424. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  425. }
  426. int Sprite3D::get_frame() const {
  427. return frame;
  428. }
  429. void Sprite3D::set_vframes(int p_amount) {
  430. ERR_FAIL_COND(p_amount < 1);
  431. vframes = p_amount;
  432. _queue_update();
  433. _change_notify();
  434. }
  435. int Sprite3D::get_vframes() const {
  436. return vframes;
  437. }
  438. void Sprite3D::set_hframes(int p_amount) {
  439. ERR_FAIL_COND(p_amount < 1);
  440. hframes = p_amount;
  441. _queue_update();
  442. _change_notify();
  443. }
  444. int Sprite3D::get_hframes() const {
  445. return hframes;
  446. }
  447. Rect2 Sprite3D::get_item_rect() const {
  448. if (texture.is_null())
  449. return Rect2(0, 0, 1, 1);
  450. /*
  451. if (texture.is_null())
  452. return CanvasItem::get_item_rect();
  453. */
  454. Size2i s;
  455. if (region) {
  456. s = region_rect.size;
  457. } else {
  458. s = texture->get_size();
  459. s = s / Point2(hframes, vframes);
  460. }
  461. Point2 ofs = get_offset();
  462. if (is_centered())
  463. ofs -= s / 2;
  464. if (s == Size2(0, 0))
  465. s = Size2(1, 1);
  466. return Rect2(ofs, s);
  467. }
  468. void Sprite3D::_validate_property(PropertyInfo &property) const {
  469. if (property.name == "frame") {
  470. property.hint = PROPERTY_HINT_SPRITE_FRAME;
  471. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  472. }
  473. }
  474. void Sprite3D::_bind_methods() {
  475. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
  476. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
  477. ClassDB::bind_method(D_METHOD("set_region", "enabled"), &Sprite3D::set_region);
  478. ClassDB::bind_method(D_METHOD("is_region"), &Sprite3D::is_region);
  479. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
  480. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
  481. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
  482. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
  483. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
  484. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
  485. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
  486. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
  487. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  488. ADD_GROUP("Animation", "");
  489. ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  490. ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  491. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
  492. ADD_GROUP("Region", "region_");
  493. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region");
  494. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  495. ADD_SIGNAL(MethodInfo("frame_changed"));
  496. }
  497. Sprite3D::Sprite3D() {
  498. region = false;
  499. frame = 0;
  500. vframes = 1;
  501. hframes = 1;
  502. }
  503. ////////////////////////////////////////
  504. void AnimatedSprite3D::_draw() {
  505. RID immediate = get_immediate();
  506. VS::get_singleton()->immediate_clear(immediate);
  507. if (frames.is_null()) {
  508. return;
  509. }
  510. if (frame < 0) {
  511. return;
  512. }
  513. if (!frames->has_animation(animation)) {
  514. return;
  515. }
  516. Ref<Texture> texture = frames->get_frame(animation, frame);
  517. if (!texture.is_valid())
  518. return; //no texuture no life
  519. Vector2 tsize = texture->get_size();
  520. if (tsize.x == 0 || tsize.y == 0)
  521. return;
  522. Size2i s = tsize;
  523. Rect2 src_rect;
  524. src_rect.size = s;
  525. Point2 ofs = get_offset();
  526. if (is_centered())
  527. ofs -= s / 2;
  528. Rect2 dst_rect(ofs, s);
  529. Rect2 final_rect;
  530. Rect2 final_src_rect;
  531. if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect))
  532. return;
  533. if (final_rect.size.x == 0 || final_rect.size.y == 0)
  534. return;
  535. Color color = _get_color_accum();
  536. color.a *= get_opacity();
  537. float pixel_size = get_pixel_size();
  538. Vector2 vertices[4] = {
  539. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  540. (final_rect.position + final_rect.size) * pixel_size,
  541. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  542. final_rect.position * pixel_size,
  543. };
  544. Vector2 src_tsize = tsize;
  545. // Properly setup UVs for impostor textures (AtlasTexture).
  546. Ref<AtlasTexture> atlas_tex = texture;
  547. if (atlas_tex != NULL) {
  548. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  549. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  550. }
  551. Vector2 uvs[4] = {
  552. final_src_rect.position / src_tsize,
  553. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  554. (final_src_rect.position + final_src_rect.size) / src_tsize,
  555. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  556. };
  557. if (is_flipped_h()) {
  558. SWAP(uvs[0], uvs[1]);
  559. SWAP(uvs[2], uvs[3]);
  560. }
  561. if (is_flipped_v()) {
  562. SWAP(uvs[0], uvs[3]);
  563. SWAP(uvs[1], uvs[2]);
  564. }
  565. Vector3 normal;
  566. int axis = get_axis();
  567. normal[axis] = 1.0;
  568. Plane tangent;
  569. if (axis == Vector3::AXIS_X) {
  570. tangent = Plane(0, 0, -1, -1);
  571. } else {
  572. tangent = Plane(1, 0, 0, -1);
  573. }
  574. RID mat = SpatialMaterial::get_material_rid_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);
  575. VS::get_singleton()->immediate_set_material(immediate, mat);
  576. VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid());
  577. int x_axis = ((axis + 1) % 3);
  578. int y_axis = ((axis + 2) % 3);
  579. if (axis != Vector3::AXIS_Z) {
  580. SWAP(x_axis, y_axis);
  581. for (int i = 0; i < 4; i++) {
  582. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  583. //SWAP(vertices[i].x,vertices[i].y);
  584. if (axis == Vector3::AXIS_Y) {
  585. vertices[i].y = -vertices[i].y;
  586. } else if (axis == Vector3::AXIS_X) {
  587. vertices[i].x = -vertices[i].x;
  588. }
  589. }
  590. }
  591. AABB aabb;
  592. for (int i = 0; i < 4; i++) {
  593. VS::get_singleton()->immediate_normal(immediate, normal);
  594. VS::get_singleton()->immediate_tangent(immediate, tangent);
  595. VS::get_singleton()->immediate_color(immediate, color);
  596. VS::get_singleton()->immediate_uv(immediate, uvs[i]);
  597. Vector3 vtx;
  598. vtx[x_axis] = vertices[i][0];
  599. vtx[y_axis] = vertices[i][1];
  600. VS::get_singleton()->immediate_vertex(immediate, vtx);
  601. if (i == 0) {
  602. aabb.position = vtx;
  603. aabb.size = Vector3();
  604. } else {
  605. aabb.expand_to(vtx);
  606. }
  607. }
  608. set_aabb(aabb);
  609. VS::get_singleton()->immediate_end(immediate);
  610. }
  611. void AnimatedSprite3D::_validate_property(PropertyInfo &property) const {
  612. if (!frames.is_valid())
  613. return;
  614. if (property.name == "animation") {
  615. property.hint = PROPERTY_HINT_ENUM;
  616. List<StringName> names;
  617. frames->get_animation_list(&names);
  618. names.sort_custom<StringName::AlphCompare>();
  619. bool current_found = false;
  620. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  621. if (E->prev()) {
  622. property.hint_string += ",";
  623. }
  624. property.hint_string += String(E->get());
  625. if (animation == E->get()) {
  626. current_found = true;
  627. }
  628. }
  629. if (!current_found) {
  630. if (property.hint_string == String()) {
  631. property.hint_string = String(animation);
  632. } else {
  633. property.hint_string = String(animation) + "," + property.hint_string;
  634. }
  635. }
  636. }
  637. if (property.name == "frame") {
  638. property.hint = PROPERTY_HINT_RANGE;
  639. if (frames->has_animation(animation)) {
  640. property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  641. } else {
  642. property.hint_string = "0,0,0";
  643. }
  644. }
  645. }
  646. void AnimatedSprite3D::_notification(int p_what) {
  647. switch (p_what) {
  648. case NOTIFICATION_INTERNAL_PROCESS: {
  649. if (frames.is_null())
  650. return;
  651. if (!frames->has_animation(animation))
  652. return;
  653. if (frame < 0)
  654. return;
  655. float speed = frames->get_animation_speed(animation);
  656. if (speed == 0)
  657. return; //do nothing
  658. float remaining = get_process_delta_time();
  659. while (remaining) {
  660. if (timeout <= 0) {
  661. timeout = 1.0 / speed;
  662. int fc = frames->get_frame_count(animation);
  663. if (frame >= fc - 1) {
  664. if (frames->get_animation_loop(animation)) {
  665. frame = 0;
  666. } else {
  667. frame = fc - 1;
  668. }
  669. } else {
  670. frame++;
  671. }
  672. _queue_update();
  673. _change_notify("frame");
  674. }
  675. float to_process = MIN(timeout, remaining);
  676. remaining -= to_process;
  677. timeout -= to_process;
  678. }
  679. } break;
  680. }
  681. }
  682. void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  683. if (frames.is_valid())
  684. frames->disconnect("changed", this, "_res_changed");
  685. frames = p_frames;
  686. if (frames.is_valid())
  687. frames->connect("changed", this, "_res_changed");
  688. if (!frames.is_valid()) {
  689. frame = 0;
  690. } else {
  691. set_frame(frame);
  692. }
  693. _change_notify();
  694. _reset_timeout();
  695. _queue_update();
  696. update_configuration_warning();
  697. }
  698. Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
  699. return frames;
  700. }
  701. void AnimatedSprite3D::set_frame(int p_frame) {
  702. if (!frames.is_valid()) {
  703. return;
  704. }
  705. if (frames->has_animation(animation)) {
  706. int limit = frames->get_frame_count(animation);
  707. if (p_frame >= limit)
  708. p_frame = limit - 1;
  709. }
  710. if (p_frame < 0)
  711. p_frame = 0;
  712. if (frame == p_frame)
  713. return;
  714. frame = p_frame;
  715. _reset_timeout();
  716. _queue_update();
  717. _change_notify("frame");
  718. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  719. }
  720. int AnimatedSprite3D::get_frame() const {
  721. return frame;
  722. }
  723. Rect2 AnimatedSprite3D::get_item_rect() const {
  724. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  725. return Rect2(0, 0, 1, 1);
  726. }
  727. Ref<Texture> t;
  728. if (animation)
  729. t = frames->get_frame(animation, frame);
  730. if (t.is_null())
  731. return Rect2(0, 0, 1, 1);
  732. Size2i s = t->get_size();
  733. Point2 ofs = get_offset();
  734. if (centered)
  735. ofs -= s / 2;
  736. if (s == Size2(0, 0))
  737. s = Size2(1, 1);
  738. return Rect2(ofs, s);
  739. }
  740. void AnimatedSprite3D::_res_changed() {
  741. set_frame(frame);
  742. _change_notify("frame");
  743. _change_notify("animation");
  744. _queue_update();
  745. }
  746. void AnimatedSprite3D::_set_playing(bool p_playing) {
  747. if (playing == p_playing)
  748. return;
  749. playing = p_playing;
  750. _reset_timeout();
  751. set_process_internal(playing);
  752. }
  753. bool AnimatedSprite3D::_is_playing() const {
  754. return playing;
  755. }
  756. void AnimatedSprite3D::play(const StringName &p_animation) {
  757. if (p_animation)
  758. set_animation(p_animation);
  759. _set_playing(true);
  760. }
  761. void AnimatedSprite3D::stop() {
  762. _set_playing(false);
  763. }
  764. bool AnimatedSprite3D::is_playing() const {
  765. return is_processing();
  766. }
  767. void AnimatedSprite3D::_reset_timeout() {
  768. if (!playing)
  769. return;
  770. if (frames.is_valid() && frames->has_animation(animation)) {
  771. float speed = frames->get_animation_speed(animation);
  772. if (speed > 0) {
  773. timeout = 1.0 / speed;
  774. } else {
  775. timeout = 0;
  776. }
  777. } else {
  778. timeout = 0;
  779. }
  780. }
  781. void AnimatedSprite3D::set_animation(const StringName &p_animation) {
  782. if (animation == p_animation)
  783. return;
  784. animation = p_animation;
  785. _reset_timeout();
  786. set_frame(0);
  787. _change_notify();
  788. _queue_update();
  789. }
  790. StringName AnimatedSprite3D::get_animation() const {
  791. return animation;
  792. }
  793. String AnimatedSprite3D::get_configuration_warning() const {
  794. if (frames.is_null()) {
  795. return TTR("A SpriteFrames resource must be created or set in the 'Frames' property in order for AnimatedSprite3D to display frames.");
  796. }
  797. return String();
  798. }
  799. void AnimatedSprite3D::_bind_methods() {
  800. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
  801. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
  802. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite3D::set_animation);
  803. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
  804. ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite3D::_set_playing);
  805. ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite3D::_is_playing);
  806. ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
  807. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
  808. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
  809. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
  810. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
  811. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed);
  812. ADD_SIGNAL(MethodInfo("frame_changed"));
  813. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  814. ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
  815. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
  816. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
  817. }
  818. AnimatedSprite3D::AnimatedSprite3D() {
  819. frame = 0;
  820. playing = false;
  821. animation = "default";
  822. timeout = 0;
  823. }