2
0

sprite_3d.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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;
  283. Vector2 tsize = texture->get_size();
  284. if (tsize.x == 0 || tsize.y == 0)
  285. return;
  286. Rect2 base_rect;
  287. if (region)
  288. base_rect = region_rect;
  289. else
  290. base_rect = Rect2(0, 0, texture->get_width(), texture->get_height());
  291. Size2 frame_size = base_rect.size / Size2(hframes, vframes);
  292. Point2 frame_offset = Point2(frame % hframes, frame / hframes);
  293. frame_offset *= frame_size;
  294. Point2 dest_offset = get_offset();
  295. if (is_centered())
  296. dest_offset -= frame_size / 2;
  297. Rect2 src_rect(base_rect.position + frame_offset, frame_size);
  298. Rect2 final_dst_rect(dest_offset, frame_size);
  299. Rect2 final_rect;
  300. Rect2 final_src_rect;
  301. if (!texture->get_rect_region(final_dst_rect, src_rect, final_rect, final_src_rect))
  302. return;
  303. if (final_rect.size.x == 0 || final_rect.size.y == 0)
  304. return;
  305. Color color = _get_color_accum();
  306. color.a *= get_opacity();
  307. float pixel_size = get_pixel_size();
  308. Vector2 vertices[4] = {
  309. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  310. (final_rect.position + final_rect.size) * pixel_size,
  311. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  312. final_rect.position * pixel_size,
  313. };
  314. Vector2 src_tsize = tsize;
  315. // Properly setup UVs for impostor textures (AtlasTexture).
  316. Ref<AtlasTexture> atlas_tex = texture;
  317. if (atlas_tex != NULL) {
  318. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  319. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  320. }
  321. Vector2 uvs[4] = {
  322. final_src_rect.position / src_tsize,
  323. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  324. (final_src_rect.position + final_src_rect.size) / src_tsize,
  325. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  326. };
  327. if (is_flipped_h()) {
  328. SWAP(uvs[0], uvs[1]);
  329. SWAP(uvs[2], uvs[3]);
  330. }
  331. if (is_flipped_v()) {
  332. SWAP(uvs[0], uvs[3]);
  333. SWAP(uvs[1], uvs[2]);
  334. }
  335. Vector3 normal;
  336. int axis = get_axis();
  337. normal[axis] = 1.0;
  338. Plane tangent;
  339. if (axis == Vector3::AXIS_X) {
  340. tangent = Plane(0, 0, -1, 1);
  341. } else {
  342. tangent = Plane(1, 0, 0, 1);
  343. }
  344. 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);
  345. VS::get_singleton()->immediate_set_material(immediate, mat);
  346. VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid());
  347. int x_axis = ((axis + 1) % 3);
  348. int y_axis = ((axis + 2) % 3);
  349. if (axis != Vector3::AXIS_Z) {
  350. SWAP(x_axis, y_axis);
  351. for (int i = 0; i < 4; i++) {
  352. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  353. //SWAP(vertices[i].x,vertices[i].y);
  354. if (axis == Vector3::AXIS_Y) {
  355. vertices[i].y = -vertices[i].y;
  356. } else if (axis == Vector3::AXIS_X) {
  357. vertices[i].x = -vertices[i].x;
  358. }
  359. }
  360. }
  361. AABB aabb;
  362. for (int i = 0; i < 4; i++) {
  363. VS::get_singleton()->immediate_normal(immediate, normal);
  364. VS::get_singleton()->immediate_tangent(immediate, tangent);
  365. VS::get_singleton()->immediate_color(immediate, color);
  366. VS::get_singleton()->immediate_uv(immediate, uvs[i]);
  367. Vector3 vtx;
  368. vtx[x_axis] = vertices[i][0];
  369. vtx[y_axis] = vertices[i][1];
  370. VS::get_singleton()->immediate_vertex(immediate, vtx);
  371. if (i == 0) {
  372. aabb.position = vtx;
  373. aabb.size = Vector3();
  374. } else {
  375. aabb.expand_to(vtx);
  376. }
  377. }
  378. set_aabb(aabb);
  379. VS::get_singleton()->immediate_end(immediate);
  380. }
  381. void Sprite3D::set_texture(const Ref<Texture> &p_texture) {
  382. if (p_texture == texture)
  383. return;
  384. if (texture.is_valid()) {
  385. texture->disconnect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  386. }
  387. texture = p_texture;
  388. if (texture.is_valid()) {
  389. texture->set_flags(texture->get_flags()); //remove repeat from texture, it looks bad in sprites
  390. texture->connect(CoreStringNames::get_singleton()->changed, this, SceneStringNames::get_singleton()->_queue_update);
  391. }
  392. _queue_update();
  393. }
  394. Ref<Texture> Sprite3D::get_texture() const {
  395. return texture;
  396. }
  397. void Sprite3D::set_region(bool p_region) {
  398. if (p_region == region)
  399. return;
  400. region = p_region;
  401. _queue_update();
  402. }
  403. bool Sprite3D::is_region() const {
  404. return region;
  405. }
  406. void Sprite3D::set_region_rect(const Rect2 &p_region_rect) {
  407. bool changed = region_rect != p_region_rect;
  408. region_rect = p_region_rect;
  409. if (region && changed) {
  410. _queue_update();
  411. }
  412. }
  413. Rect2 Sprite3D::get_region_rect() const {
  414. return region_rect;
  415. }
  416. void Sprite3D::set_frame(int p_frame) {
  417. ERR_FAIL_INDEX(p_frame, vframes * hframes);
  418. if (frame != p_frame)
  419. frame = p_frame;
  420. _queue_update();
  421. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  422. }
  423. int Sprite3D::get_frame() const {
  424. return frame;
  425. }
  426. void Sprite3D::set_vframes(int p_amount) {
  427. ERR_FAIL_COND(p_amount < 1);
  428. vframes = p_amount;
  429. _queue_update();
  430. _change_notify();
  431. }
  432. int Sprite3D::get_vframes() const {
  433. return vframes;
  434. }
  435. void Sprite3D::set_hframes(int p_amount) {
  436. ERR_FAIL_COND(p_amount < 1);
  437. hframes = p_amount;
  438. _queue_update();
  439. _change_notify();
  440. }
  441. int Sprite3D::get_hframes() const {
  442. return hframes;
  443. }
  444. Rect2 Sprite3D::get_item_rect() const {
  445. if (texture.is_null())
  446. return Rect2(0, 0, 1, 1);
  447. /*
  448. if (texture.is_null())
  449. return CanvasItem::get_item_rect();
  450. */
  451. Size2i s;
  452. if (region) {
  453. s = region_rect.size;
  454. } else {
  455. s = texture->get_size();
  456. s = s / Point2(hframes, vframes);
  457. }
  458. Point2 ofs = get_offset();
  459. if (is_centered())
  460. ofs -= s / 2;
  461. if (s == Size2(0, 0))
  462. s = Size2(1, 1);
  463. return Rect2(ofs, s);
  464. }
  465. void Sprite3D::_validate_property(PropertyInfo &property) const {
  466. if (property.name == "frame") {
  467. property.hint = PROPERTY_HINT_SPRITE_FRAME;
  468. property.hint_string = "0," + itos(vframes * hframes - 1) + ",1";
  469. }
  470. }
  471. void Sprite3D::_bind_methods() {
  472. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture);
  473. ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture);
  474. ClassDB::bind_method(D_METHOD("set_region", "enabled"), &Sprite3D::set_region);
  475. ClassDB::bind_method(D_METHOD("is_region"), &Sprite3D::is_region);
  476. ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect);
  477. ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect);
  478. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame);
  479. ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame);
  480. ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes);
  481. ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes);
  482. ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes);
  483. ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes);
  484. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  485. ADD_GROUP("Animation", "");
  486. ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes");
  487. ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes");
  488. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
  489. ADD_GROUP("Region", "region_");
  490. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled"), "set_region", "is_region");
  491. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect"), "set_region_rect", "get_region_rect");
  492. ADD_SIGNAL(MethodInfo("frame_changed"));
  493. }
  494. Sprite3D::Sprite3D() {
  495. region = false;
  496. frame = 0;
  497. vframes = 1;
  498. hframes = 1;
  499. }
  500. ////////////////////////////////////////
  501. void AnimatedSprite3D::_draw() {
  502. RID immediate = get_immediate();
  503. VS::get_singleton()->immediate_clear(immediate);
  504. if (frames.is_null()) {
  505. return;
  506. }
  507. if (frame < 0) {
  508. return;
  509. }
  510. if (!frames->has_animation(animation)) {
  511. return;
  512. }
  513. Ref<Texture> texture = frames->get_frame(animation, frame);
  514. if (!texture.is_valid())
  515. return; //no texuture no life
  516. Vector2 tsize = texture->get_size();
  517. if (tsize.x == 0 || tsize.y == 0)
  518. return;
  519. Size2i s = tsize;
  520. Rect2 src_rect;
  521. src_rect.size = s;
  522. Point2 ofs = get_offset();
  523. if (is_centered())
  524. ofs -= s / 2;
  525. Rect2 dst_rect(ofs, s);
  526. Rect2 final_rect;
  527. Rect2 final_src_rect;
  528. if (!texture->get_rect_region(dst_rect, src_rect, final_rect, final_src_rect))
  529. return;
  530. if (final_rect.size.x == 0 || final_rect.size.y == 0)
  531. return;
  532. Color color = _get_color_accum();
  533. color.a *= get_opacity();
  534. float pixel_size = get_pixel_size();
  535. Vector2 vertices[4] = {
  536. (final_rect.position + Vector2(0, final_rect.size.y)) * pixel_size,
  537. (final_rect.position + final_rect.size) * pixel_size,
  538. (final_rect.position + Vector2(final_rect.size.x, 0)) * pixel_size,
  539. final_rect.position * pixel_size,
  540. };
  541. Vector2 src_tsize = tsize;
  542. // Properly setup UVs for impostor textures (AtlasTexture).
  543. Ref<AtlasTexture> atlas_tex = texture;
  544. if (atlas_tex != NULL) {
  545. src_tsize[0] = atlas_tex->get_atlas()->get_width();
  546. src_tsize[1] = atlas_tex->get_atlas()->get_height();
  547. }
  548. Vector2 uvs[4] = {
  549. final_src_rect.position / src_tsize,
  550. (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize,
  551. (final_src_rect.position + final_src_rect.size) / src_tsize,
  552. (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize,
  553. };
  554. if (is_flipped_h()) {
  555. SWAP(uvs[0], uvs[1]);
  556. SWAP(uvs[2], uvs[3]);
  557. }
  558. if (is_flipped_v()) {
  559. SWAP(uvs[0], uvs[3]);
  560. SWAP(uvs[1], uvs[2]);
  561. }
  562. Vector3 normal;
  563. int axis = get_axis();
  564. normal[axis] = 1.0;
  565. Plane tangent;
  566. if (axis == Vector3::AXIS_X) {
  567. tangent = Plane(0, 0, -1, -1);
  568. } else {
  569. tangent = Plane(1, 0, 0, -1);
  570. }
  571. 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);
  572. VS::get_singleton()->immediate_set_material(immediate, mat);
  573. VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_TRIANGLE_FAN, texture->get_rid());
  574. int x_axis = ((axis + 1) % 3);
  575. int y_axis = ((axis + 2) % 3);
  576. if (axis != Vector3::AXIS_Z) {
  577. SWAP(x_axis, y_axis);
  578. for (int i = 0; i < 4; i++) {
  579. //uvs[i] = Vector2(1.0,1.0)-uvs[i];
  580. //SWAP(vertices[i].x,vertices[i].y);
  581. if (axis == Vector3::AXIS_Y) {
  582. vertices[i].y = -vertices[i].y;
  583. } else if (axis == Vector3::AXIS_X) {
  584. vertices[i].x = -vertices[i].x;
  585. }
  586. }
  587. }
  588. AABB aabb;
  589. for (int i = 0; i < 4; i++) {
  590. VS::get_singleton()->immediate_normal(immediate, normal);
  591. VS::get_singleton()->immediate_tangent(immediate, tangent);
  592. VS::get_singleton()->immediate_color(immediate, color);
  593. VS::get_singleton()->immediate_uv(immediate, uvs[i]);
  594. Vector3 vtx;
  595. vtx[x_axis] = vertices[i][0];
  596. vtx[y_axis] = vertices[i][1];
  597. VS::get_singleton()->immediate_vertex(immediate, vtx);
  598. if (i == 0) {
  599. aabb.position = vtx;
  600. aabb.size = Vector3();
  601. } else {
  602. aabb.expand_to(vtx);
  603. }
  604. }
  605. set_aabb(aabb);
  606. VS::get_singleton()->immediate_end(immediate);
  607. }
  608. void AnimatedSprite3D::_validate_property(PropertyInfo &property) const {
  609. if (!frames.is_valid())
  610. return;
  611. if (property.name == "animation") {
  612. property.hint = PROPERTY_HINT_ENUM;
  613. List<StringName> names;
  614. frames->get_animation_list(&names);
  615. names.sort_custom<StringName::AlphCompare>();
  616. bool current_found = false;
  617. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  618. if (E->prev()) {
  619. property.hint_string += ",";
  620. }
  621. property.hint_string += String(E->get());
  622. if (animation == E->get()) {
  623. current_found = true;
  624. }
  625. }
  626. if (!current_found) {
  627. if (property.hint_string == String()) {
  628. property.hint_string = String(animation);
  629. } else {
  630. property.hint_string = String(animation) + "," + property.hint_string;
  631. }
  632. }
  633. }
  634. if (property.name == "frame") {
  635. property.hint = PROPERTY_HINT_RANGE;
  636. if (frames->has_animation(animation)) {
  637. property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1";
  638. } else {
  639. property.hint_string = "0,0,0";
  640. }
  641. }
  642. }
  643. void AnimatedSprite3D::_notification(int p_what) {
  644. switch (p_what) {
  645. case NOTIFICATION_INTERNAL_PROCESS: {
  646. if (frames.is_null())
  647. return;
  648. if (!frames->has_animation(animation))
  649. return;
  650. if (frame < 0)
  651. return;
  652. float speed = frames->get_animation_speed(animation);
  653. if (speed == 0)
  654. return; //do nothing
  655. float remaining = get_process_delta_time();
  656. while (remaining) {
  657. if (timeout <= 0) {
  658. timeout = 1.0 / speed;
  659. int fc = frames->get_frame_count(animation);
  660. if (frame >= fc - 1) {
  661. if (frames->get_animation_loop(animation)) {
  662. frame = 0;
  663. } else {
  664. frame = fc - 1;
  665. }
  666. } else {
  667. frame++;
  668. }
  669. _queue_update();
  670. _change_notify("frame");
  671. }
  672. float to_process = MIN(timeout, remaining);
  673. remaining -= to_process;
  674. timeout -= to_process;
  675. }
  676. } break;
  677. }
  678. }
  679. void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) {
  680. if (frames.is_valid())
  681. frames->disconnect("changed", this, "_res_changed");
  682. frames = p_frames;
  683. if (frames.is_valid())
  684. frames->connect("changed", this, "_res_changed");
  685. if (!frames.is_valid()) {
  686. frame = 0;
  687. } else {
  688. set_frame(frame);
  689. }
  690. _change_notify();
  691. _reset_timeout();
  692. _queue_update();
  693. update_configuration_warning();
  694. }
  695. Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const {
  696. return frames;
  697. }
  698. void AnimatedSprite3D::set_frame(int p_frame) {
  699. if (!frames.is_valid()) {
  700. return;
  701. }
  702. if (frames->has_animation(animation)) {
  703. int limit = frames->get_frame_count(animation);
  704. if (p_frame >= limit)
  705. p_frame = limit - 1;
  706. }
  707. if (p_frame < 0)
  708. p_frame = 0;
  709. if (frame == p_frame)
  710. return;
  711. frame = p_frame;
  712. _reset_timeout();
  713. _queue_update();
  714. _change_notify("frame");
  715. emit_signal(SceneStringNames::get_singleton()->frame_changed);
  716. }
  717. int AnimatedSprite3D::get_frame() const {
  718. return frame;
  719. }
  720. Rect2 AnimatedSprite3D::get_item_rect() const {
  721. if (!frames.is_valid() || !frames->has_animation(animation) || frame < 0 || frame >= frames->get_frame_count(animation)) {
  722. return Rect2(0, 0, 1, 1);
  723. }
  724. Ref<Texture> t;
  725. if (animation)
  726. t = frames->get_frame(animation, frame);
  727. if (t.is_null())
  728. return Rect2(0, 0, 1, 1);
  729. Size2i s = t->get_size();
  730. Point2 ofs = get_offset();
  731. if (centered)
  732. ofs -= s / 2;
  733. if (s == Size2(0, 0))
  734. s = Size2(1, 1);
  735. return Rect2(ofs, s);
  736. }
  737. void AnimatedSprite3D::_res_changed() {
  738. set_frame(frame);
  739. _change_notify("frame");
  740. _change_notify("animation");
  741. _queue_update();
  742. }
  743. void AnimatedSprite3D::_set_playing(bool p_playing) {
  744. if (playing == p_playing)
  745. return;
  746. playing = p_playing;
  747. _reset_timeout();
  748. set_process_internal(playing);
  749. }
  750. bool AnimatedSprite3D::_is_playing() const {
  751. return playing;
  752. }
  753. void AnimatedSprite3D::play(const StringName &p_animation) {
  754. if (p_animation)
  755. set_animation(p_animation);
  756. _set_playing(true);
  757. }
  758. void AnimatedSprite3D::stop() {
  759. _set_playing(false);
  760. }
  761. bool AnimatedSprite3D::is_playing() const {
  762. return is_processing();
  763. }
  764. void AnimatedSprite3D::_reset_timeout() {
  765. if (!playing)
  766. return;
  767. if (frames.is_valid() && frames->has_animation(animation)) {
  768. float speed = frames->get_animation_speed(animation);
  769. if (speed > 0) {
  770. timeout = 1.0 / speed;
  771. } else {
  772. timeout = 0;
  773. }
  774. } else {
  775. timeout = 0;
  776. }
  777. }
  778. void AnimatedSprite3D::set_animation(const StringName &p_animation) {
  779. if (animation == p_animation)
  780. return;
  781. animation = p_animation;
  782. _reset_timeout();
  783. set_frame(0);
  784. _change_notify();
  785. _queue_update();
  786. }
  787. StringName AnimatedSprite3D::get_animation() const {
  788. return animation;
  789. }
  790. String AnimatedSprite3D::get_configuration_warning() const {
  791. if (frames.is_null()) {
  792. return TTR("A SpriteFrames resource must be created or set in the 'Frames' property in order for AnimatedSprite3D to display frames.");
  793. }
  794. return String();
  795. }
  796. void AnimatedSprite3D::_bind_methods() {
  797. ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames);
  798. ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames);
  799. ClassDB::bind_method(D_METHOD("set_animation", "animation"), &AnimatedSprite3D::set_animation);
  800. ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation);
  801. ClassDB::bind_method(D_METHOD("_set_playing", "playing"), &AnimatedSprite3D::_set_playing);
  802. ClassDB::bind_method(D_METHOD("_is_playing"), &AnimatedSprite3D::_is_playing);
  803. ClassDB::bind_method(D_METHOD("play", "anim"), &AnimatedSprite3D::play, DEFVAL(StringName()));
  804. ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop);
  805. ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing);
  806. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame);
  807. ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame);
  808. ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed);
  809. ADD_SIGNAL(MethodInfo("frame_changed"));
  810. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames");
  811. ADD_PROPERTY(PropertyInfo(Variant::STRING, "animation"), "set_animation", "get_animation");
  812. ADD_PROPERTY(PropertyInfo(Variant::INT, "frame", PROPERTY_HINT_SPRITE_FRAME), "set_frame", "get_frame");
  813. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playing"), "_set_playing", "_is_playing");
  814. }
  815. AnimatedSprite3D::AnimatedSprite3D() {
  816. frame = 0;
  817. playing = false;
  818. animation = "default";
  819. timeout = 0;
  820. }