visual_server_canvas.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. /**************************************************************************/
  2. /* visual_server_canvas.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "visual_server_canvas.h"
  31. #include "core/fixed_array.h"
  32. #include "core/math/transform_interpolator.h"
  33. #include "visual_server_globals.h"
  34. #include "visual_server_raster.h"
  35. #include "visual_server_viewport.h"
  36. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  37. void VisualServerCanvas::_render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights) {
  38. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  39. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  40. _current_camera_transform = p_transform;
  41. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  42. _prepare_tree_bounds(p_canvas_item);
  43. _render_canvas_item_cull_by_node(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  44. } else {
  45. _render_canvas_item_cull_by_item(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  46. }
  47. VSG::canvas_render->canvas_render_items_begin(p_modulate, p_lights, p_transform);
  48. for (int i = 0; i < z_range; i++) {
  49. if (!z_list[i]) {
  50. continue;
  51. }
  52. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_modulate, p_lights, p_transform);
  53. }
  54. VSG::canvas_render->canvas_render_items_end();
  55. }
  56. void _collect_ysort_children(VisualServerCanvas::Item *p_canvas_item, Transform2D p_transform, VisualServerCanvas::Item *p_material_owner, const Color p_modulate, VisualServerCanvas::Item **r_items, int &r_index) {
  57. int child_item_count = p_canvas_item->child_items.size();
  58. VisualServerCanvas::Item **child_items = p_canvas_item->child_items.ptrw();
  59. for (int i = 0; i < child_item_count; i++) {
  60. if (child_items[i]->visible) {
  61. if (r_items) {
  62. r_items[r_index] = child_items[i];
  63. child_items[i]->ysort_modulate = p_modulate;
  64. child_items[i]->ysort_xform = p_transform;
  65. child_items[i]->ysort_pos = p_transform.xform(child_items[i]->xform_curr.elements[2]);
  66. child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : nullptr;
  67. child_items[i]->ysort_index = r_index;
  68. }
  69. r_index++;
  70. if (child_items[i]->sort_y) {
  71. _collect_ysort_children(child_items[i],
  72. p_transform * child_items[i]->xform_curr,
  73. child_items[i]->use_parent_material ? p_material_owner : child_items[i],
  74. p_modulate * child_items[i]->modulate,
  75. r_items, r_index);
  76. }
  77. }
  78. }
  79. }
  80. void _mark_ysort_dirty(VisualServerCanvas::Item *ysort_owner, RID_Owner<VisualServerCanvas::Item> &canvas_item_owner) {
  81. do {
  82. ysort_owner->ysort_children_count = -1;
  83. ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.getornull(ysort_owner->parent) : nullptr;
  84. } while (ysort_owner && ysort_owner->sort_y);
  85. }
  86. void VisualServerCanvas::_make_bound_dirty_reparent(Item *p_item) {
  87. MutexLock lock(_bound_mutex);
  88. DEV_ASSERT(p_item);
  89. Item *p_orig_item = p_item;
  90. // propagate up
  91. while (p_item) {
  92. // Don't worry about invisible objects
  93. if (!p_item->visible) {
  94. return;
  95. }
  96. if (!p_item->bound_dirty) {
  97. p_item->bound_dirty = true;
  98. if (canvas_item_owner.owns(p_item->parent)) {
  99. p_item = canvas_item_owner.get(p_item->parent);
  100. } else {
  101. break;
  102. }
  103. } else {
  104. break;
  105. }
  106. }
  107. // propagate down
  108. _make_bound_dirty_down(p_orig_item);
  109. }
  110. void VisualServerCanvas::_make_bound_dirty(Item *p_item, bool p_changing_visibility) {
  111. if (_canvas_cull_mode != CANVAS_CULL_MODE_NODE) {
  112. return;
  113. }
  114. MutexLock lock(_bound_mutex);
  115. DEV_ASSERT(p_item);
  116. if (!p_changing_visibility) {
  117. _check_bound_integrity(p_item);
  118. }
  119. if (!p_changing_visibility) {
  120. // Traverse up the tree, making each item bound dirty until
  121. // we reach an item that is already dirty (as by definition, if this happens,
  122. // the tree should already be dirty up until the root).
  123. while (p_item) {
  124. // Don't worry about invisible objects
  125. if (!p_item->visible) {
  126. return;
  127. }
  128. if (!p_item->bound_dirty) {
  129. p_item->bound_dirty = true;
  130. if (canvas_item_owner.owns(p_item->parent)) {
  131. p_item = canvas_item_owner.get(p_item->parent);
  132. } else {
  133. break;
  134. }
  135. } else {
  136. break;
  137. }
  138. }
  139. } else {
  140. // special case for visibility changes.
  141. // if hiding, we propagate upwards.
  142. while (p_item) {
  143. if (!p_item->bound_dirty) {
  144. p_item->bound_dirty = true;
  145. }
  146. if (canvas_item_owner.owns(p_item->parent)) {
  147. p_item = canvas_item_owner.get(p_item->parent);
  148. } else {
  149. break;
  150. }
  151. }
  152. // if showing we propagate upwards AND downwards
  153. if (p_item->visible) {
  154. _make_bound_dirty_down(p_item);
  155. }
  156. }
  157. }
  158. void VisualServerCanvas::_make_bound_dirty_down(Item *p_item) {
  159. // Bounds below an item that is being made visible may be out of date,
  160. // so we make them all dirty.
  161. if (!p_item->visible) {
  162. return;
  163. }
  164. p_item->bound_dirty = true;
  165. int child_item_count = p_item->child_items.size();
  166. Item **child_items = p_item->child_items.ptrw();
  167. for (int i = 0; i < child_item_count; i++) {
  168. _make_bound_dirty_down(child_items[i]);
  169. }
  170. }
  171. void VisualServerCanvas::_prepare_tree_bounds(Item *p_root) {
  172. Rect2 root_bound;
  173. _calculate_canvas_item_bound(p_root, &root_bound);
  174. }
  175. // This function provides an alternative means of recursively calculating canvas item
  176. // bounds through a branch, leading to an identical (hopefully) result to that
  177. // calculated for the bound in _render_canvas_item().
  178. // The reason for this function's existence is that there are some conditions which
  179. // prevent further drawing in the tree (such as alpha nearing 0.0), in which we
  180. // *still* need to calculate the bounds for lower branches and use them for culling,
  181. // just in case alpha increases above the threshold in a later frame.
  182. void VisualServerCanvas::_calculate_canvas_item_bound(Item *p_canvas_item, Rect2 *r_branch_bound) {
  183. // TODO - this higher level technique may be able to be optimized better,
  184. // to perhaps only recalculate this on "reappearance" of the child branch, in a
  185. // similar manner to how visibility is handled.
  186. Item *ci = p_canvas_item;
  187. if (!ci->visible) {
  188. return;
  189. }
  190. // easy case, not dirty
  191. if (!ci->bound_dirty) {
  192. _merge_local_bound_to_branch(ci, r_branch_bound);
  193. return;
  194. }
  195. // recalculate the local bound only if out of date
  196. Rect2 *local_bound = nullptr;
  197. if (ci->bound_dirty) {
  198. local_bound = &ci->local_bound;
  199. *local_bound = Rect2();
  200. ci->bound_dirty = false;
  201. }
  202. int child_item_count = ci->child_items.size();
  203. Item **child_items = ci->child_items.ptrw();
  204. for (int i = 0; i < child_item_count; i++) {
  205. // if (ci->sort_y)
  206. // NYI do we need to apply the child_items[i]->ysort_xform? TEST
  207. // See the _render_canvas_item for how to apply.
  208. _calculate_canvas_item_bound(child_items[i], local_bound);
  209. }
  210. _finalize_and_merge_local_bound_to_branch(ci, r_branch_bound);
  211. // If we are interpolating, we want to modify the local_bound (combined)
  212. // to include both the previous AND current bounds.
  213. if (local_bound && _interpolation_data.interpolation_enabled && ci->interpolated) {
  214. Rect2 bound_prev = ci->local_bound_prev;
  215. // Keep track of the previously assigned exact bound for the next tick.
  216. ci->local_bound_prev = ci->local_bound;
  217. // The combined bound is the exact current bound merged with the previous exact bound.
  218. ci->local_bound = ci->local_bound.merge(bound_prev);
  219. // This can overflow, it's no problem, it is just rough to detect when items stop
  220. // having local bounds updated, so we can set prev to curr.
  221. ci->local_bound_last_update_tick = Engine::get_singleton()->get_physics_frames();
  222. // Detect special case of overflow.
  223. // This is omitted but included for reference.
  224. // It is such a rare possibility, and even if it did occur
  225. // so it should just result in slightly larger culling bounds
  226. // probably for one tick (and no visual errors).
  227. // Would occur once every 828.5 days at 60 ticks per second
  228. // with uint32_t counter.
  229. #if 0
  230. if (!ci->local_bound_last_update_tick) {
  231. // Prevents it being treated as non-dirty.
  232. // Just has an increased delay of one tick in this very rare occurrence.
  233. ci->local_bound_last_update_tick = 1;
  234. }
  235. #endif
  236. }
  237. }
  238. Transform2D VisualServerCanvas::_calculate_item_global_xform(const Item *p_canvas_item) {
  239. // If we use more than the maximum scene tree depth, we are out of luck.
  240. // But that would be super inefficient anyway.
  241. FixedArray<const Transform2D *, 64> transforms;
  242. while (p_canvas_item) {
  243. // Should only happen if scene tree depth too high.
  244. if (transforms.is_full()) {
  245. WARN_PRINT_ONCE("SceneTree depth too high for hierarchical culling.");
  246. break;
  247. }
  248. // Note this is only using the CURRENT transform.
  249. // This may have implications for interpolated bounds - investigate.
  250. transforms.push_back(&p_canvas_item->xform_curr);
  251. if (canvas_item_owner.owns(p_canvas_item->parent)) {
  252. p_canvas_item = canvas_item_owner.get(p_canvas_item->parent);
  253. } else {
  254. p_canvas_item = nullptr;
  255. }
  256. }
  257. Transform2D tr;
  258. for (int n = (int)transforms.size() - 1; n >= 0; n--) {
  259. tr *= *transforms[n];
  260. }
  261. return tr;
  262. }
  263. void VisualServerCanvas::_finalize_and_merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound) {
  264. if (r_branch_bound) {
  265. Rect2 this_rect = p_canvas_item->get_rect();
  266. // Special case .. if the canvas_item has use_identity_xform,
  267. // we need to transform the rect from global space to local space,
  268. // because the hierarchical culling expects local space.
  269. if (p_canvas_item->use_identity_xform) {
  270. // This is incredibly inefficient, but should only occur for e.g. CPUParticles2D,
  271. // and is difficult to avoid because global transform is not usually kept track of
  272. // in VisualServer (only final transform which is combinated with camera, and that
  273. // is only calculated on render, so is no use for culling purposes).
  274. Transform2D global_xform = _calculate_item_global_xform(p_canvas_item);
  275. this_rect = global_xform.affine_inverse().xform(this_rect);
  276. // Note that the efficiency will depend linearly on the scene tree depth of the
  277. // identity transform item.
  278. // So e.g. interpolated global CPUParticles2D may run faster at lower depths
  279. // in extreme circumstances.
  280. }
  281. // If this item has a bound...
  282. if (!p_canvas_item->local_bound.has_no_area()) {
  283. // If the rect has an area...
  284. if (!this_rect.has_no_area()) {
  285. p_canvas_item->local_bound = p_canvas_item->local_bound.merge(this_rect);
  286. } else {
  287. // The local bound is set by the children, but is not affected by the canvas item rect.
  288. // So pass through and merge the local bound to the parent.
  289. }
  290. } else {
  291. p_canvas_item->local_bound = this_rect;
  292. // don't merge zero area, as it may expand the branch bound
  293. // unnecessarily.
  294. if (p_canvas_item->local_bound.has_no_area()) {
  295. return;
  296. }
  297. }
  298. // Merge the local bound to the parent.
  299. _merge_local_bound_to_branch(p_canvas_item, r_branch_bound);
  300. }
  301. }
  302. void VisualServerCanvas::_merge_local_bound_to_branch(Item *p_canvas_item, Rect2 *r_branch_bound) {
  303. if (!r_branch_bound) {
  304. return;
  305. }
  306. Rect2 this_item_total_local_bound = p_canvas_item->xform_curr.xform(p_canvas_item->local_bound);
  307. if (!r_branch_bound->has_no_area()) {
  308. *r_branch_bound = r_branch_bound->merge(this_item_total_local_bound);
  309. } else {
  310. *r_branch_bound = this_item_total_local_bound;
  311. }
  312. }
  313. void VisualServerCanvas::_render_canvas_item_cull_by_item(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner) {
  314. Item *ci = p_canvas_item;
  315. if (!ci->visible) {
  316. return;
  317. }
  318. if (ci->children_order_dirty) {
  319. ci->child_items.sort_custom<ItemIndexSort>();
  320. ci->children_order_dirty = false;
  321. }
  322. Rect2 rect = ci->get_rect();
  323. Transform2D final_xform;
  324. if (!_interpolation_data.interpolation_enabled || !ci->interpolated) {
  325. final_xform = ci->xform_curr;
  326. } else {
  327. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  328. TransformInterpolator::interpolate_transform_2d(ci->xform_prev, ci->xform_curr, final_xform, f);
  329. }
  330. // Always calculate final transform as if not using identity xform.
  331. // This is so the expected transform is passed to children.
  332. // However, if use_identity_xform is set,
  333. // we can override the transform for rendering purposes for this item only.
  334. final_xform = p_transform * final_xform;
  335. Rect2 global_rect;
  336. if (!p_canvas_item->use_identity_xform) {
  337. global_rect = final_xform.xform(rect);
  338. } else {
  339. global_rect = _current_camera_transform.xform(rect);
  340. }
  341. global_rect.position += p_clip_rect.position;
  342. if (ci->use_parent_material && p_material_owner) {
  343. ci->material_owner = p_material_owner;
  344. } else {
  345. p_material_owner = ci;
  346. ci->material_owner = nullptr;
  347. }
  348. Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a);
  349. if (modulate.a < 0.007) {
  350. return;
  351. }
  352. int child_item_count = ci->child_items.size();
  353. Item **child_items = ci->child_items.ptrw();
  354. if (ci->clip) {
  355. if (p_canvas_clip != nullptr) {
  356. ci->final_clip_rect = p_canvas_clip->final_clip_rect.clip(global_rect);
  357. } else {
  358. ci->final_clip_rect = global_rect;
  359. }
  360. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  361. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  362. ci->final_clip_owner = ci;
  363. } else {
  364. ci->final_clip_owner = p_canvas_clip;
  365. }
  366. if (ci->sort_y) {
  367. if (ci->ysort_children_count == -1) {
  368. ci->ysort_children_count = 0;
  369. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count);
  370. }
  371. child_item_count = ci->ysort_children_count;
  372. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  373. int i = 0;
  374. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i);
  375. SortArray<Item *, ItemPtrSort> sorter;
  376. sorter.sort(child_items, child_item_count);
  377. }
  378. if (ci->z_relative) {
  379. p_z = CLAMP(p_z + ci->z_index, VS::CANVAS_ITEM_Z_MIN, VS::CANVAS_ITEM_Z_MAX);
  380. } else {
  381. p_z = ci->z_index;
  382. }
  383. for (int i = 0; i < child_item_count; i++) {
  384. if (!child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  385. continue;
  386. }
  387. if (ci->sort_y) {
  388. _render_canvas_item_cull_by_item(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner);
  389. } else {
  390. _render_canvas_item_cull_by_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  391. }
  392. }
  393. if (ci->copy_back_buffer) {
  394. ci->copy_back_buffer->screen_rect = final_xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  395. }
  396. if (ci->update_when_visible) {
  397. VisualServerRaster::redraw_request(false);
  398. }
  399. if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect, true)) || ci->vp_render || ci->copy_back_buffer) {
  400. //something to draw?
  401. ci->final_transform = !p_canvas_item->use_identity_xform ? final_xform : _current_camera_transform;
  402. ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a);
  403. ci->global_rect_cache = global_rect;
  404. ci->global_rect_cache.position -= p_clip_rect.position;
  405. ci->light_masked = false;
  406. int zidx = p_z - VS::CANVAS_ITEM_Z_MIN;
  407. if (z_last_list[zidx]) {
  408. z_last_list[zidx]->next = ci;
  409. z_last_list[zidx] = ci;
  410. } else {
  411. z_list[zidx] = ci;
  412. z_last_list[zidx] = ci;
  413. }
  414. ci->next = nullptr;
  415. }
  416. for (int i = 0; i < child_item_count; i++) {
  417. if (child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  418. continue;
  419. }
  420. if (ci->sort_y) {
  421. _render_canvas_item_cull_by_item(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner);
  422. } else {
  423. _render_canvas_item_cull_by_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  424. }
  425. }
  426. }
  427. void VisualServerCanvas::_render_canvas_item_cull_by_node(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RasterizerCanvas::Item **z_list, RasterizerCanvas::Item **z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_enclosed) {
  428. Item *ci = p_canvas_item;
  429. if (!ci->visible) {
  430. return;
  431. }
  432. // This should have been calculated as a pre-process.
  433. DEV_ASSERT(!ci->bound_dirty);
  434. // If we are interpolating, and the updates have stopped, we can reduce the local bound.
  435. if (ci->local_bound_last_update_tick && (ci->local_bound_last_update_tick != Engine::get_singleton()->get_physics_frames())) {
  436. // The combined bound is reduced to the last calculated exact bound.
  437. ci->local_bound = ci->local_bound_prev;
  438. ci->local_bound_last_update_tick = 0;
  439. }
  440. Rect2 rect = ci->get_rect();
  441. Transform2D final_xform;
  442. if (!_interpolation_data.interpolation_enabled || !ci->interpolated) {
  443. final_xform = ci->xform_curr;
  444. } else {
  445. real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
  446. TransformInterpolator::interpolate_transform_2d(ci->xform_prev, ci->xform_curr, final_xform, f);
  447. }
  448. // Always calculate final transform as if not using identity xform.
  449. // This is so the expected transform is passed to children.
  450. // However, if use_identity_xform is set,
  451. // we can override the transform for rendering purposes for this item only.
  452. final_xform = p_transform * final_xform;
  453. Rect2 global_rect;
  454. if (!p_canvas_item->use_identity_xform) {
  455. global_rect = final_xform.xform(rect);
  456. } else {
  457. global_rect = _current_camera_transform.xform(rect);
  458. }
  459. ci->global_rect_cache = global_rect;
  460. ci->final_transform = !p_canvas_item->use_identity_xform ? final_xform : _current_camera_transform;
  461. global_rect.position += p_clip_rect.position;
  462. int child_item_count = ci->child_items.size();
  463. // If there are children, we can maybe cull them all out if the cached bound has not changed.
  464. if (!p_enclosed && child_item_count) {
  465. // Get the bound in final space.
  466. Rect2 bound = final_xform.xform(ci->local_bound);
  467. bound.position += p_clip_rect.position;
  468. if (!ci->vp_render && !ci->copy_back_buffer) {
  469. // Cull out ALL children in one step.
  470. if (!p_clip_rect.intersects(bound, true)) {
  471. return;
  472. }
  473. }
  474. // can we combine with earlier check?
  475. // if we enclose the bound completely, no need to check further children
  476. p_enclosed = p_clip_rect.encloses(bound);
  477. }
  478. // if we are culled, and no children, no more needs doing
  479. bool item_is_visible = ((!ci->commands.empty() && (p_enclosed ? true : p_clip_rect.intersects(global_rect, true))) || ci->vp_render || ci->copy_back_buffer);
  480. if (!item_is_visible && !child_item_count) {
  481. return;
  482. }
  483. if (ci->use_parent_material && p_material_owner) {
  484. ci->material_owner = p_material_owner;
  485. } else {
  486. p_material_owner = ci;
  487. ci->material_owner = nullptr;
  488. }
  489. Color modulate(ci->modulate.r * p_modulate.r, ci->modulate.g * p_modulate.g, ci->modulate.b * p_modulate.b, ci->modulate.a * p_modulate.a);
  490. if (modulate.a < 0.007) {
  491. return;
  492. }
  493. if (ci->children_order_dirty) {
  494. ci->child_items.sort_custom<ItemIndexSort>();
  495. ci->children_order_dirty = false;
  496. }
  497. Item **child_items = ci->child_items.ptrw();
  498. if (ci->clip) {
  499. if (p_canvas_clip != nullptr) {
  500. ci->final_clip_rect = p_canvas_clip->final_clip_rect.clip(global_rect);
  501. } else {
  502. ci->final_clip_rect = global_rect;
  503. }
  504. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  505. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  506. ci->final_clip_owner = ci;
  507. } else {
  508. ci->final_clip_owner = p_canvas_clip;
  509. }
  510. if (ci->sort_y) {
  511. if (ci->ysort_children_count == -1) {
  512. ci->ysort_children_count = 0;
  513. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count);
  514. }
  515. child_item_count = ci->ysort_children_count;
  516. // NOTE : Use of alloca here in a recursive function could make it susceptible to stack overflow.
  517. // This was present in the original Item code. Consider changing to make safer.
  518. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  519. int i = 0;
  520. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i);
  521. SortArray<Item *, ItemPtrSort> sorter;
  522. sorter.sort(child_items, child_item_count);
  523. }
  524. if (ci->z_relative) {
  525. p_z = CLAMP(p_z + ci->z_index, VS::CANVAS_ITEM_Z_MIN, VS::CANVAS_ITEM_Z_MAX);
  526. } else {
  527. p_z = ci->z_index;
  528. }
  529. for (int i = 0; i < child_item_count; i++) {
  530. if (!child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  531. continue;
  532. }
  533. if (ci->sort_y) {
  534. _render_canvas_item_cull_by_node(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, p_enclosed);
  535. } else {
  536. _render_canvas_item_cull_by_node(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, p_enclosed);
  537. }
  538. }
  539. if (ci->copy_back_buffer) {
  540. ci->copy_back_buffer->screen_rect = final_xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  541. }
  542. // something to draw?
  543. if (item_is_visible) {
  544. // Note : This has been moved to inside the (item_is_visible) check.
  545. // It was OUTSIDE in the item culled code, which I suspect was incorrect.
  546. // A redraw should not be issued if an item is not on screen?
  547. // Even so, watch for regressions here.
  548. if (ci->update_when_visible) {
  549. VisualServerRaster::redraw_request(false);
  550. }
  551. // Note we have already stored ci->final_transform
  552. // and ci->global_rect_cache, and made sure these are up to date.
  553. ci->final_modulate = Color(modulate.r * ci->self_modulate.r, modulate.g * ci->self_modulate.g, modulate.b * ci->self_modulate.b, modulate.a * ci->self_modulate.a);
  554. ci->light_masked = false;
  555. int zidx = p_z - VS::CANVAS_ITEM_Z_MIN;
  556. if (z_last_list[zidx]) {
  557. z_last_list[zidx]->next = ci;
  558. z_last_list[zidx] = ci;
  559. } else {
  560. z_list[zidx] = ci;
  561. z_last_list[zidx] = ci;
  562. }
  563. ci->next = nullptr;
  564. }
  565. for (int i = 0; i < child_item_count; i++) {
  566. if (child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  567. continue;
  568. }
  569. if (ci->sort_y) {
  570. _render_canvas_item_cull_by_node(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, p_enclosed);
  571. } else {
  572. _render_canvas_item_cull_by_node(child_items[i], final_xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner, p_enclosed);
  573. }
  574. }
  575. }
  576. void VisualServerCanvas::_light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights, int p_canvas_layer_id) {
  577. RasterizerCanvas::Item *ci = p_canvas_item;
  578. while (ci) {
  579. RasterizerCanvas::Light *light = p_masked_lights;
  580. while (light) {
  581. if ((p_canvas_layer_id >= light->layer_min) && (p_canvas_layer_id <= light->layer_max) && (ci->light_mask & light->item_mask) && (p_z >= light->z_min) && (p_z <= light->z_max) && (ci->global_rect_cache.intersects_transformed(light->xform_cache, light->rect_cache))) {
  582. ci->light_masked = true;
  583. }
  584. light = light->mask_next_ptr;
  585. }
  586. ci = ci->next;
  587. }
  588. }
  589. void VisualServerCanvas::render_canvas(Canvas *p_canvas, const Transform2D &p_transform, RasterizerCanvas::Light *p_lights, RasterizerCanvas::Light *p_masked_lights, const Rect2 &p_clip_rect, int p_canvas_layer_id) {
  590. VSG::canvas_render->canvas_begin();
  591. if (p_canvas->children_order_dirty) {
  592. p_canvas->child_items.sort();
  593. p_canvas->children_order_dirty = false;
  594. }
  595. int l = p_canvas->child_items.size();
  596. Canvas::ChildItem *ci = p_canvas->child_items.ptrw();
  597. bool has_mirror = false;
  598. for (int i = 0; i < l; i++) {
  599. if (ci[i].mirror.x || ci[i].mirror.y) {
  600. has_mirror = true;
  601. break;
  602. }
  603. }
  604. if (!has_mirror) {
  605. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  606. RasterizerCanvas::Item *z_list[z_range];
  607. RasterizerCanvas::Item *z_last_list[z_range];
  608. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  609. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  610. _current_camera_transform = p_transform;
  611. #ifdef VISUAL_SERVER_CANVAS_TIME_NODE_CULLING
  612. bool measure = (Engine::get_singleton()->get_frames_drawn() % 100) == 0;
  613. measure &= !Engine::get_singleton()->is_editor_hint();
  614. if (measure) {
  615. uint64_t totalA = 0;
  616. uint64_t totalB = 0;
  617. for (int i = 0; i < l; i++) {
  618. uint64_t beforeB = OS::get_singleton()->get_ticks_usec();
  619. _render_canvas_item_cull_by_item(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  620. uint64_t afterB = OS::get_singleton()->get_ticks_usec();
  621. uint64_t beforeA = OS::get_singleton()->get_ticks_usec();
  622. _prepare_tree_bounds(ci[i].item);
  623. _render_canvas_item_cull_by_node(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  624. uint64_t afterA = OS::get_singleton()->get_ticks_usec();
  625. totalA += afterA - beforeA;
  626. totalB += afterB - beforeB;
  627. } // for i
  628. print_line("old : " + itos(totalB) + ", new : " + itos(totalA));
  629. } // if measure
  630. else {
  631. #else
  632. {
  633. #endif
  634. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  635. for (int i = 0; i < l; i++) {
  636. _prepare_tree_bounds(ci[i].item);
  637. _render_canvas_item_cull_by_node(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, false);
  638. }
  639. } else {
  640. for (int i = 0; i < l; i++) {
  641. _render_canvas_item_cull_by_item(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  642. }
  643. }
  644. } // if not measure
  645. VSG::canvas_render->canvas_render_items_begin(p_canvas->modulate, p_lights, p_transform);
  646. for (int i = 0; i < z_range; i++) {
  647. if (!z_list[i]) {
  648. continue;
  649. }
  650. if (p_masked_lights) {
  651. _light_mask_canvas_items(VS::CANVAS_ITEM_Z_MIN + i, z_list[i], p_masked_lights, p_canvas_layer_id);
  652. }
  653. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_canvas->modulate, p_lights, p_transform);
  654. }
  655. VSG::canvas_render->canvas_render_items_end();
  656. } else {
  657. for (int i = 0; i < l; i++) {
  658. const Canvas::ChildItem &ci2 = p_canvas->child_items[i];
  659. _render_canvas_item_tree(ci2.item, p_transform, p_clip_rect, p_canvas->modulate, p_lights);
  660. //mirroring (useful for scrolling backgrounds)
  661. if (ci2.mirror.x != 0) {
  662. Transform2D xform2 = p_transform * Transform2D(0, Vector2(ci2.mirror.x, 0));
  663. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  664. }
  665. if (ci2.mirror.y != 0) {
  666. Transform2D xform2 = p_transform * Transform2D(0, Vector2(0, ci2.mirror.y));
  667. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  668. }
  669. if (ci2.mirror.y != 0 && ci2.mirror.x != 0) {
  670. Transform2D xform2 = p_transform * Transform2D(0, ci2.mirror);
  671. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  672. }
  673. }
  674. }
  675. VSG::canvas_render->canvas_end();
  676. }
  677. RID VisualServerCanvas::canvas_create() {
  678. Canvas *canvas = memnew(Canvas);
  679. ERR_FAIL_COND_V(!canvas, RID());
  680. RID rid = canvas_owner.make_rid(canvas);
  681. return rid;
  682. }
  683. void VisualServerCanvas::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) {
  684. Canvas *canvas = canvas_owner.getornull(p_canvas);
  685. ERR_FAIL_COND(!canvas);
  686. Item *canvas_item = canvas_item_owner.getornull(p_item);
  687. ERR_FAIL_COND(!canvas_item);
  688. int idx = canvas->find_item(canvas_item);
  689. ERR_FAIL_COND(idx == -1);
  690. canvas->child_items.write[idx].mirror = p_mirroring;
  691. }
  692. void VisualServerCanvas::canvas_set_modulate(RID p_canvas, const Color &p_color) {
  693. Canvas *canvas = canvas_owner.get(p_canvas);
  694. ERR_FAIL_COND(!canvas);
  695. canvas->modulate = p_color;
  696. }
  697. void VisualServerCanvas::canvas_set_disable_scale(bool p_disable) {
  698. disable_scale = p_disable;
  699. }
  700. void VisualServerCanvas::canvas_set_parent(RID p_canvas, RID p_parent, float p_scale) {
  701. Canvas *canvas = canvas_owner.get(p_canvas);
  702. ERR_FAIL_COND(!canvas);
  703. canvas->parent = p_parent;
  704. canvas->parent_scale = p_scale;
  705. }
  706. RID VisualServerCanvas::canvas_item_create() {
  707. Item *canvas_item = memnew(Item);
  708. ERR_FAIL_COND_V(!canvas_item, RID());
  709. return canvas_item_owner.make_rid(canvas_item);
  710. }
  711. void VisualServerCanvas::canvas_item_set_name(RID p_item, String p_name) {
  712. #ifdef VISUAL_SERVER_CANVAS_DEBUG_ITEM_NAMES
  713. Item *canvas_item = canvas_item_owner.getornull(p_item);
  714. ERR_FAIL_COND(!canvas_item);
  715. canvas_item->name = p_name;
  716. #endif
  717. }
  718. void VisualServerCanvas::canvas_item_set_parent(RID p_item, RID p_parent) {
  719. Item *canvas_item = canvas_item_owner.getornull(p_item);
  720. ERR_FAIL_COND(!canvas_item);
  721. // dirty the item and any previous parents
  722. _make_bound_dirty(canvas_item);
  723. if (canvas_item->parent.is_valid()) {
  724. if (canvas_owner.owns(canvas_item->parent)) {
  725. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  726. canvas->erase_item(canvas_item);
  727. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  728. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  729. item_owner->child_items.erase(canvas_item);
  730. if (item_owner->sort_y) {
  731. _mark_ysort_dirty(item_owner, canvas_item_owner);
  732. }
  733. }
  734. canvas_item->parent = RID();
  735. }
  736. if (p_parent.is_valid()) {
  737. if (canvas_owner.owns(p_parent)) {
  738. Canvas *canvas = canvas_owner.get(p_parent);
  739. Canvas::ChildItem ci;
  740. ci.item = canvas_item;
  741. canvas->child_items.push_back(ci);
  742. canvas->children_order_dirty = true;
  743. _make_bound_dirty(canvas_item);
  744. } else if (canvas_item_owner.owns(p_parent)) {
  745. Item *item_owner = canvas_item_owner.get(p_parent);
  746. item_owner->child_items.push_back(canvas_item);
  747. item_owner->children_order_dirty = true;
  748. if (item_owner->sort_y) {
  749. _mark_ysort_dirty(item_owner, canvas_item_owner);
  750. }
  751. // keep the integrity of the bounds when adding to avoid false
  752. // warning flags, by forcing the added child to be dirty
  753. canvas_item->bound_dirty = false;
  754. canvas_item->parent = p_parent;
  755. _make_bound_dirty_reparent(canvas_item);
  756. } else {
  757. ERR_FAIL_MSG("Invalid parent.");
  758. }
  759. }
  760. canvas_item->parent = p_parent;
  761. _check_bound_integrity(canvas_item);
  762. }
  763. void VisualServerCanvas::canvas_item_set_visible(RID p_item, bool p_visible) {
  764. Item *canvas_item = canvas_item_owner.getornull(p_item);
  765. ERR_FAIL_COND(!canvas_item);
  766. // check for noop
  767. if (p_visible != canvas_item->visible) {
  768. canvas_item->visible = p_visible;
  769. _make_bound_dirty(canvas_item, true);
  770. }
  771. // could this be enclosed in the noop? not sure
  772. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  773. }
  774. void VisualServerCanvas::canvas_item_set_light_mask(RID p_item, int p_mask) {
  775. Item *canvas_item = canvas_item_owner.getornull(p_item);
  776. ERR_FAIL_COND(!canvas_item);
  777. canvas_item->light_mask = p_mask;
  778. _check_bound_integrity(canvas_item);
  779. }
  780. void VisualServerCanvas::canvas_item_set_transform(RID p_item, const Transform2D &p_transform) {
  781. Item *canvas_item = canvas_item_owner.getornull(p_item);
  782. ERR_FAIL_COND(!canvas_item);
  783. if (_interpolation_data.interpolation_enabled && canvas_item->interpolated) {
  784. if (!canvas_item->on_interpolate_transform_list) {
  785. _interpolation_data.canvas_item_transform_update_list_curr->push_back(p_item);
  786. canvas_item->on_interpolate_transform_list = true;
  787. } else {
  788. DEV_ASSERT(_interpolation_data.canvas_item_transform_update_list_curr->size());
  789. }
  790. }
  791. canvas_item->xform_curr = p_transform;
  792. // Special case!
  793. // Modifying the transform DOES NOT affect the local bound.
  794. // It only affects the local bound of the PARENT node (if there is one).
  795. if (canvas_item_owner.owns(canvas_item->parent)) {
  796. Item *canvas_item_parent = canvas_item_owner.get(canvas_item->parent);
  797. _make_bound_dirty(canvas_item_parent);
  798. }
  799. }
  800. void VisualServerCanvas::canvas_item_set_clip(RID p_item, bool p_clip) {
  801. Item *canvas_item = canvas_item_owner.getornull(p_item);
  802. ERR_FAIL_COND(!canvas_item);
  803. canvas_item->clip = p_clip;
  804. _make_bound_dirty(canvas_item);
  805. }
  806. void VisualServerCanvas::canvas_item_set_distance_field_mode(RID p_item, bool p_enable) {
  807. Item *canvas_item = canvas_item_owner.getornull(p_item);
  808. ERR_FAIL_COND(!canvas_item);
  809. canvas_item->distance_field = p_enable;
  810. _make_bound_dirty(canvas_item);
  811. }
  812. void VisualServerCanvas::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect) {
  813. Item *canvas_item = canvas_item_owner.getornull(p_item);
  814. ERR_FAIL_COND(!canvas_item);
  815. canvas_item->custom_rect = p_custom_rect;
  816. canvas_item->rect = p_rect;
  817. _make_bound_dirty(canvas_item);
  818. }
  819. void VisualServerCanvas::canvas_item_set_modulate(RID p_item, const Color &p_color) {
  820. Item *canvas_item = canvas_item_owner.getornull(p_item);
  821. ERR_FAIL_COND(!canvas_item);
  822. canvas_item->modulate = p_color;
  823. _make_bound_dirty(canvas_item);
  824. }
  825. void VisualServerCanvas::canvas_item_set_self_modulate(RID p_item, const Color &p_color) {
  826. Item *canvas_item = canvas_item_owner.getornull(p_item);
  827. ERR_FAIL_COND(!canvas_item);
  828. canvas_item->self_modulate = p_color;
  829. _make_bound_dirty(canvas_item);
  830. }
  831. void VisualServerCanvas::canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) {
  832. Item *canvas_item = canvas_item_owner.getornull(p_item);
  833. ERR_FAIL_COND(!canvas_item);
  834. canvas_item->behind = p_enable;
  835. _check_bound_integrity(canvas_item);
  836. }
  837. void VisualServerCanvas::canvas_item_set_use_identity_transform(RID p_item, bool p_enable) {
  838. Item *canvas_item = canvas_item_owner.getornull(p_item);
  839. ERR_FAIL_COND(!canvas_item);
  840. canvas_item->use_identity_xform = p_enable;
  841. _make_bound_dirty(canvas_item);
  842. }
  843. void VisualServerCanvas::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
  844. Item *canvas_item = canvas_item_owner.getornull(p_item);
  845. ERR_FAIL_COND(!canvas_item);
  846. canvas_item->update_when_visible = p_update;
  847. _check_bound_integrity(canvas_item);
  848. }
  849. void VisualServerCanvas::canvas_item_add_line(RID p_item, const Point2 &p_from, const Point2 &p_to, const Color &p_color, float p_width, bool p_antialiased) {
  850. // Try drawing as a poly, because polys are batched and thus should run faster than thick lines,
  851. // which run extremely slowly.
  852. if (!p_antialiased && (p_width > 1.0)) {
  853. // use poly drawing, as it is faster as it can use batching
  854. static Vector<Point2> points;
  855. static Vector<Color> colors;
  856. static Vector<Point2> uvs;
  857. if (points.size() != 4) {
  858. // this should only be done once at runtime due to use of a static
  859. points.resize(4);
  860. colors.resize(4);
  861. uvs.resize(4);
  862. }
  863. Vector2 side = p_to - p_from;
  864. real_t length = side.length();
  865. if (length == 0.0) {
  866. // Not sure yet whether zero length is a noop operation later on,
  867. // watch for visual errors. If there are visual errors, pass through
  868. // to the line drawing routine below.
  869. return;
  870. }
  871. // normalize
  872. side /= length;
  873. // 90 degrees
  874. side = Vector2(-side.y, side.x);
  875. side *= p_width * 0.5;
  876. points.set(0, p_from + side);
  877. points.set(1, p_from - side);
  878. points.set(2, p_to - side);
  879. points.set(3, p_to + side);
  880. for (int n = 0; n < 4; n++) {
  881. colors.set(n, p_color);
  882. }
  883. canvas_item_add_polygon(p_item, points, colors, uvs, RID(), RID(), false);
  884. return;
  885. }
  886. Item *canvas_item = canvas_item_owner.getornull(p_item);
  887. ERR_FAIL_COND(!canvas_item);
  888. Item::CommandLine *line = memnew(Item::CommandLine);
  889. ERR_FAIL_COND(!line);
  890. line->color = p_color;
  891. line->from = p_from;
  892. line->to = p_to;
  893. line->width = p_width;
  894. line->antialiased = p_antialiased;
  895. canvas_item->rect_dirty = true;
  896. canvas_item->commands.push_back(line);
  897. _make_bound_dirty(canvas_item);
  898. }
  899. void VisualServerCanvas::canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  900. ERR_FAIL_COND(p_points.size() < 2);
  901. Item *canvas_item = canvas_item_owner.getornull(p_item);
  902. ERR_FAIL_COND(!canvas_item);
  903. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  904. ERR_FAIL_COND(!pline);
  905. pline->antialiased = p_antialiased;
  906. pline->multiline = false;
  907. if (p_width <= 1) {
  908. pline->lines = p_points;
  909. pline->line_colors = p_colors;
  910. if (pline->line_colors.size() == 0) {
  911. pline->line_colors.push_back(Color(1, 1, 1, 1));
  912. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  913. pline->line_colors.resize(1);
  914. }
  915. } else {
  916. //make a trianglestrip for drawing the line...
  917. Vector2 prev_t;
  918. pline->triangles.resize(p_points.size() * 2);
  919. if (p_antialiased) {
  920. pline->lines.resize(p_points.size() * 2);
  921. }
  922. if (p_colors.size() == 0) {
  923. pline->triangle_colors.push_back(Color(1, 1, 1, 1));
  924. if (p_antialiased) {
  925. pline->line_colors.push_back(Color(1, 1, 1, 1));
  926. }
  927. } else if (p_colors.size() == 1) {
  928. pline->triangle_colors = p_colors;
  929. pline->line_colors = p_colors;
  930. } else {
  931. if (p_colors.size() != p_points.size()) {
  932. pline->triangle_colors.push_back(p_colors[0]);
  933. pline->line_colors.push_back(p_colors[0]);
  934. } else {
  935. pline->triangle_colors.resize(pline->triangles.size());
  936. pline->line_colors.resize(pline->lines.size());
  937. }
  938. }
  939. for (int i = 0; i < p_points.size(); i++) {
  940. Vector2 t;
  941. if (i == p_points.size() - 1) {
  942. t = prev_t;
  943. } else {
  944. t = (p_points[i + 1] - p_points[i]).normalized().tangent();
  945. if (i == 0) {
  946. prev_t = t;
  947. }
  948. }
  949. Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;
  950. if (p_antialiased) {
  951. pline->lines.write[i] = p_points[i] + tangent;
  952. pline->lines.write[p_points.size() * 2 - i - 1] = p_points[i] - tangent;
  953. if (pline->line_colors.size() > 1) {
  954. pline->line_colors.write[i] = p_colors[i];
  955. pline->line_colors.write[p_points.size() * 2 - i - 1] = p_colors[i];
  956. }
  957. }
  958. pline->triangles.write[i * 2 + 0] = p_points[i] + tangent;
  959. pline->triangles.write[i * 2 + 1] = p_points[i] - tangent;
  960. if (pline->triangle_colors.size() > 1) {
  961. pline->triangle_colors.write[i * 2 + 0] = p_colors[i];
  962. pline->triangle_colors.write[i * 2 + 1] = p_colors[i];
  963. }
  964. prev_t = t;
  965. }
  966. }
  967. canvas_item->rect_dirty = true;
  968. canvas_item->commands.push_back(pline);
  969. _make_bound_dirty(canvas_item);
  970. }
  971. void VisualServerCanvas::canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
  972. ERR_FAIL_COND(p_points.size() < 2);
  973. Item *canvas_item = canvas_item_owner.getornull(p_item);
  974. ERR_FAIL_COND(!canvas_item);
  975. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  976. ERR_FAIL_COND(!pline);
  977. pline->antialiased = false; //todo
  978. pline->multiline = true;
  979. pline->lines = p_points;
  980. pline->line_colors = p_colors;
  981. if (pline->line_colors.size() == 0) {
  982. pline->line_colors.push_back(Color(1, 1, 1, 1));
  983. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  984. pline->line_colors.resize(1);
  985. }
  986. canvas_item->rect_dirty = true;
  987. canvas_item->commands.push_back(pline);
  988. _make_bound_dirty(canvas_item);
  989. }
  990. void VisualServerCanvas::canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color) {
  991. Item *canvas_item = canvas_item_owner.getornull(p_item);
  992. ERR_FAIL_COND(!canvas_item);
  993. Item::CommandRect *rect = memnew(Item::CommandRect);
  994. ERR_FAIL_COND(!rect);
  995. rect->modulate = p_color;
  996. rect->rect = p_rect;
  997. canvas_item->rect_dirty = true;
  998. canvas_item->commands.push_back(rect);
  999. _make_bound_dirty(canvas_item);
  1000. }
  1001. void VisualServerCanvas::canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color) {
  1002. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1003. ERR_FAIL_COND(!canvas_item);
  1004. Item::CommandCircle *circle = memnew(Item::CommandCircle);
  1005. ERR_FAIL_COND(!circle);
  1006. circle->color = p_color;
  1007. circle->pos = p_pos;
  1008. circle->radius = p_radius;
  1009. canvas_item->commands.push_back(circle);
  1010. _make_bound_dirty(canvas_item);
  1011. }
  1012. void VisualServerCanvas::canvas_item_add_texture_rect(RID p_item, const Rect2 &p_rect, RID p_texture, bool p_tile, const Color &p_modulate, bool p_transpose, RID p_normal_map) {
  1013. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1014. ERR_FAIL_COND(!canvas_item);
  1015. Item::CommandRect *rect = memnew(Item::CommandRect);
  1016. ERR_FAIL_COND(!rect);
  1017. rect->modulate = p_modulate;
  1018. rect->rect = p_rect;
  1019. rect->flags = 0;
  1020. if (p_tile) {
  1021. rect->flags |= RasterizerCanvas::CANVAS_RECT_TILE;
  1022. rect->flags |= RasterizerCanvas::CANVAS_RECT_REGION;
  1023. rect->source = Rect2(0, 0, fabsf(p_rect.size.width), fabsf(p_rect.size.height));
  1024. }
  1025. if (p_rect.size.x < 0) {
  1026. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  1027. rect->rect.size.x = -rect->rect.size.x;
  1028. }
  1029. if (p_rect.size.y < 0) {
  1030. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  1031. rect->rect.size.y = -rect->rect.size.y;
  1032. }
  1033. if (p_transpose) {
  1034. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  1035. SWAP(rect->rect.size.x, rect->rect.size.y);
  1036. }
  1037. rect->texture = p_texture;
  1038. rect->normal_map = p_normal_map;
  1039. canvas_item->rect_dirty = true;
  1040. canvas_item->commands.push_back(rect);
  1041. _make_bound_dirty(canvas_item);
  1042. }
  1043. void VisualServerCanvas::canvas_item_add_texture_multirect_region(RID p_item, const Vector<Rect2> &p_rects, RID p_texture, const Vector<Rect2> &p_src_rects, const Color &p_modulate, uint32_t p_canvas_rect_flags, RID p_normal_map) {
  1044. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1045. ERR_FAIL_COND(!canvas_item);
  1046. ERR_FAIL_COND(p_rects.size() != p_src_rects.size());
  1047. ERR_FAIL_COND(!p_rects.size());
  1048. Item::CommandMultiRect *rect = memnew(Item::CommandMultiRect);
  1049. ERR_FAIL_COND(!rect);
  1050. rect->modulate = p_modulate;
  1051. rect->texture = p_texture;
  1052. rect->normal_map = p_normal_map;
  1053. // Rects should have flips and transposes pre-applied, and the relevant
  1054. // flags added to p_canvas_rect_flags.
  1055. // A single Multirect should contain rects ALL of the same flag type.
  1056. // The idea is to simplify the renderer as much as possible, and push the complexity
  1057. // to the one off creation code.
  1058. rect->flags = p_canvas_rect_flags | RasterizerCanvas::CANVAS_RECT_REGION;
  1059. rect->rects = p_rects;
  1060. rect->sources = p_src_rects;
  1061. canvas_item->rect_dirty = true;
  1062. canvas_item->commands.push_back(rect);
  1063. }
  1064. void VisualServerCanvas::canvas_item_add_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, RID p_normal_map, bool p_clip_uv) {
  1065. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1066. ERR_FAIL_COND(!canvas_item);
  1067. Item::CommandRect *rect = memnew(Item::CommandRect);
  1068. ERR_FAIL_COND(!rect);
  1069. rect->modulate = p_modulate;
  1070. rect->rect = p_rect;
  1071. rect->texture = p_texture;
  1072. rect->normal_map = p_normal_map;
  1073. rect->source = p_src_rect;
  1074. rect->flags = RasterizerCanvas::CANVAS_RECT_REGION;
  1075. if (p_rect.size.x < 0) {
  1076. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  1077. rect->rect.size.x = -rect->rect.size.x;
  1078. }
  1079. if (p_src_rect.size.x < 0) {
  1080. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  1081. rect->source.size.x = -rect->source.size.x;
  1082. }
  1083. if (p_rect.size.y < 0) {
  1084. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  1085. rect->rect.size.y = -rect->rect.size.y;
  1086. }
  1087. if (p_src_rect.size.y < 0) {
  1088. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  1089. rect->source.size.y = -rect->source.size.y;
  1090. }
  1091. if (p_transpose) {
  1092. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  1093. SWAP(rect->rect.size.x, rect->rect.size.y);
  1094. }
  1095. if (p_clip_uv) {
  1096. rect->flags |= RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  1097. }
  1098. canvas_item->rect_dirty = true;
  1099. canvas_item->commands.push_back(rect);
  1100. _make_bound_dirty(canvas_item);
  1101. }
  1102. void VisualServerCanvas::canvas_item_add_nine_patch(RID p_item, const Rect2 &p_rect, const Rect2 &p_source, RID p_texture, const Vector2 &p_topleft, const Vector2 &p_bottomright, VS::NinePatchAxisMode p_x_axis_mode, VS::NinePatchAxisMode p_y_axis_mode, bool p_draw_center, const Color &p_modulate, RID p_normal_map) {
  1103. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1104. ERR_FAIL_COND(!canvas_item);
  1105. Item::CommandNinePatch *style = memnew(Item::CommandNinePatch);
  1106. ERR_FAIL_COND(!style);
  1107. style->texture = p_texture;
  1108. style->normal_map = p_normal_map;
  1109. style->rect = p_rect;
  1110. style->source = p_source;
  1111. style->draw_center = p_draw_center;
  1112. style->color = p_modulate;
  1113. style->margin[MARGIN_LEFT] = p_topleft.x;
  1114. style->margin[MARGIN_TOP] = p_topleft.y;
  1115. style->margin[MARGIN_RIGHT] = p_bottomright.x;
  1116. style->margin[MARGIN_BOTTOM] = p_bottomright.y;
  1117. style->axis_x = p_x_axis_mode;
  1118. style->axis_y = p_y_axis_mode;
  1119. canvas_item->rect_dirty = true;
  1120. canvas_item->commands.push_back(style);
  1121. _make_bound_dirty(canvas_item);
  1122. }
  1123. void VisualServerCanvas::canvas_item_add_primitive(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, float p_width, RID p_normal_map) {
  1124. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1125. ERR_FAIL_COND(!canvas_item);
  1126. Item::CommandPrimitive *prim = memnew(Item::CommandPrimitive);
  1127. ERR_FAIL_COND(!prim);
  1128. prim->texture = p_texture;
  1129. prim->normal_map = p_normal_map;
  1130. prim->points = p_points;
  1131. prim->uvs = p_uvs;
  1132. prim->colors = p_colors;
  1133. prim->width = p_width;
  1134. canvas_item->rect_dirty = true;
  1135. canvas_item->commands.push_back(prim);
  1136. _make_bound_dirty(canvas_item);
  1137. }
  1138. void VisualServerCanvas::canvas_item_add_polygon(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, RID p_texture, RID p_normal_map, bool p_antialiased) {
  1139. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1140. ERR_FAIL_COND(!canvas_item);
  1141. #ifdef DEBUG_ENABLED
  1142. int pointcount = p_points.size();
  1143. ERR_FAIL_COND(pointcount < 3);
  1144. int color_size = p_colors.size();
  1145. int uv_size = p_uvs.size();
  1146. ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount);
  1147. ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount));
  1148. #endif
  1149. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  1150. ERR_FAIL_COND_MSG(indices.empty(), "Invalid polygon data, triangulation failed.");
  1151. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  1152. ERR_FAIL_COND(!polygon);
  1153. polygon->texture = p_texture;
  1154. polygon->normal_map = p_normal_map;
  1155. polygon->points = p_points;
  1156. polygon->uvs = p_uvs;
  1157. polygon->colors = p_colors;
  1158. polygon->indices = indices;
  1159. polygon->count = indices.size();
  1160. polygon->antialiased = p_antialiased;
  1161. polygon->antialiasing_use_indices = false;
  1162. canvas_item->rect_dirty = true;
  1163. canvas_item->commands.push_back(polygon);
  1164. _make_bound_dirty(canvas_item);
  1165. }
  1166. void VisualServerCanvas::canvas_item_add_triangle_array(RID p_item, const Vector<int> &p_indices, const Vector<Point2> &p_points, const Vector<Color> &p_colors, const Vector<Point2> &p_uvs, const Vector<int> &p_bones, const Vector<float> &p_weights, RID p_texture, int p_count, RID p_normal_map, bool p_antialiased, bool p_antialiasing_use_indices) {
  1167. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1168. ERR_FAIL_COND(!canvas_item);
  1169. int vertex_count = p_points.size();
  1170. ERR_FAIL_COND(vertex_count == 0);
  1171. ERR_FAIL_COND(!p_colors.empty() && p_colors.size() != vertex_count && p_colors.size() != 1);
  1172. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size() != vertex_count);
  1173. ERR_FAIL_COND(!p_bones.empty() && p_bones.size() != vertex_count * 4);
  1174. ERR_FAIL_COND(!p_weights.empty() && p_weights.size() != vertex_count * 4);
  1175. const Vector<int> &indices = p_indices;
  1176. int count = p_count * 3;
  1177. if (indices.empty()) {
  1178. ERR_FAIL_COND(vertex_count % 3 != 0);
  1179. if (p_count == -1) {
  1180. count = vertex_count;
  1181. }
  1182. } else {
  1183. ERR_FAIL_COND(indices.size() % 3 != 0);
  1184. if (p_count == -1) {
  1185. count = indices.size();
  1186. }
  1187. }
  1188. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  1189. ERR_FAIL_COND(!polygon);
  1190. polygon->texture = p_texture;
  1191. polygon->normal_map = p_normal_map;
  1192. polygon->points = p_points;
  1193. polygon->uvs = p_uvs;
  1194. polygon->colors = p_colors;
  1195. polygon->bones = p_bones;
  1196. polygon->weights = p_weights;
  1197. polygon->indices = indices;
  1198. polygon->count = count;
  1199. polygon->antialiased = p_antialiased;
  1200. polygon->antialiasing_use_indices = p_antialiasing_use_indices;
  1201. canvas_item->rect_dirty = true;
  1202. canvas_item->commands.push_back(polygon);
  1203. _make_bound_dirty(canvas_item);
  1204. }
  1205. void VisualServerCanvas::canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) {
  1206. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1207. ERR_FAIL_COND(!canvas_item);
  1208. Item::CommandTransform *tr = memnew(Item::CommandTransform);
  1209. ERR_FAIL_COND(!tr);
  1210. tr->xform = p_transform;
  1211. canvas_item->commands.push_back(tr);
  1212. _make_bound_dirty(canvas_item);
  1213. }
  1214. void VisualServerCanvas::canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform, const Color &p_modulate, RID p_texture, RID p_normal_map) {
  1215. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1216. ERR_FAIL_COND(!canvas_item);
  1217. Item::CommandMesh *m = memnew(Item::CommandMesh);
  1218. ERR_FAIL_COND(!m);
  1219. m->mesh = p_mesh;
  1220. m->texture = p_texture;
  1221. m->normal_map = p_normal_map;
  1222. m->transform = p_transform;
  1223. m->modulate = p_modulate;
  1224. canvas_item->commands.push_back(m);
  1225. _make_bound_dirty(canvas_item);
  1226. }
  1227. void VisualServerCanvas::canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture, RID p_normal) {
  1228. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1229. ERR_FAIL_COND(!canvas_item);
  1230. Item::CommandParticles *part = memnew(Item::CommandParticles);
  1231. ERR_FAIL_COND(!part);
  1232. part->particles = p_particles;
  1233. part->texture = p_texture;
  1234. part->normal_map = p_normal;
  1235. //take the chance and request processing for them, at least once until they become visible again
  1236. VSG::storage->particles_request_process(p_particles);
  1237. canvas_item->rect_dirty = true;
  1238. canvas_item->commands.push_back(part);
  1239. _make_bound_dirty(canvas_item);
  1240. }
  1241. void VisualServerCanvas::canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture, RID p_normal_map) {
  1242. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1243. ERR_FAIL_COND(!canvas_item);
  1244. Item::CommandMultiMesh *mm = memnew(Item::CommandMultiMesh);
  1245. ERR_FAIL_COND(!mm);
  1246. mm->multimesh = p_mesh;
  1247. mm->texture = p_texture;
  1248. mm->normal_map = p_normal_map;
  1249. mm->canvas_item = p_item;
  1250. canvas_item->rect_dirty = true;
  1251. canvas_item->commands.push_back(mm);
  1252. _make_bound_dirty(canvas_item);
  1253. // Attach to multimesh a backlink to enable updating
  1254. // the canvas item local bound when the multimesh changes.
  1255. if (p_mesh.is_valid()) {
  1256. VSG::storage->multimesh_attach_canvas_item(p_mesh, p_item, true);
  1257. }
  1258. }
  1259. void VisualServerCanvas::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  1260. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1261. ERR_FAIL_COND(!canvas_item);
  1262. Item::CommandClipIgnore *ci = memnew(Item::CommandClipIgnore);
  1263. ERR_FAIL_COND(!ci);
  1264. ci->ignore = p_ignore;
  1265. canvas_item->commands.push_back(ci);
  1266. _make_bound_dirty(canvas_item);
  1267. }
  1268. void VisualServerCanvas::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  1269. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1270. ERR_FAIL_COND(!canvas_item);
  1271. canvas_item->sort_y = p_enable;
  1272. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  1273. _check_bound_integrity(canvas_item);
  1274. }
  1275. void VisualServerCanvas::canvas_item_set_z_index(RID p_item, int p_z) {
  1276. ERR_FAIL_COND(p_z < VS::CANVAS_ITEM_Z_MIN || p_z > VS::CANVAS_ITEM_Z_MAX);
  1277. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1278. ERR_FAIL_COND(!canvas_item);
  1279. canvas_item->z_index = p_z;
  1280. _check_bound_integrity(canvas_item);
  1281. }
  1282. void VisualServerCanvas::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  1283. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1284. ERR_FAIL_COND(!canvas_item);
  1285. canvas_item->z_relative = p_enable;
  1286. _check_bound_integrity(canvas_item);
  1287. }
  1288. Rect2 VisualServerCanvas::_debug_canvas_item_get_rect(RID p_item) {
  1289. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1290. ERR_FAIL_COND_V(!canvas_item, Rect2());
  1291. return canvas_item->get_rect();
  1292. }
  1293. Rect2 VisualServerCanvas::_debug_canvas_item_get_local_bound(RID p_item) {
  1294. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1295. ERR_FAIL_COND_V(!canvas_item, Rect2());
  1296. return canvas_item->local_bound;
  1297. }
  1298. void VisualServerCanvas::canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) {
  1299. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1300. ERR_FAIL_COND(!canvas_item);
  1301. if (!canvas_item->skinning_data) {
  1302. canvas_item->skinning_data = memnew(Item::SkinningData);
  1303. }
  1304. canvas_item->skinning_data->skeleton_relative_xform = p_relative_xform;
  1305. canvas_item->skinning_data->skeleton_relative_xform_inv = p_relative_xform.affine_inverse();
  1306. // Set any Polygon2Ds pre-calced bone bounds to dirty.
  1307. for (int n = 0; n < canvas_item->commands.size(); n++) {
  1308. Item::Command *c = canvas_item->commands[n];
  1309. if (c->type == Item::Command::TYPE_POLYGON) {
  1310. Item::CommandPolygon *polygon = static_cast<Item::CommandPolygon *>(c);
  1311. // Make sure skinning data is present.
  1312. if (!polygon->skinning_data) {
  1313. polygon->skinning_data = memnew(Item::CommandPolygon::SkinningData);
  1314. }
  1315. polygon->skinning_data->dirty = true;
  1316. }
  1317. }
  1318. }
  1319. // Useful especially for origin shifting.
  1320. void VisualServerCanvas::canvas_item_transform_physics_interpolation(RID p_item, const Transform2D &p_transform) {
  1321. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1322. ERR_FAIL_COND(!canvas_item);
  1323. canvas_item->xform_prev = p_transform * canvas_item->xform_prev;
  1324. canvas_item->xform_curr = p_transform * canvas_item->xform_curr;
  1325. }
  1326. void VisualServerCanvas::canvas_item_reset_physics_interpolation(RID p_item) {
  1327. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1328. ERR_FAIL_COND(!canvas_item);
  1329. canvas_item->xform_prev = canvas_item->xform_curr;
  1330. }
  1331. void VisualServerCanvas::canvas_item_set_interpolated(RID p_item, bool p_interpolated) {
  1332. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1333. ERR_FAIL_COND(!canvas_item);
  1334. canvas_item->interpolated = p_interpolated;
  1335. }
  1336. void VisualServerCanvas::canvas_light_set_interpolated(RID p_light, bool p_interpolated) {
  1337. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1338. ERR_FAIL_COND(!clight);
  1339. clight->interpolated = p_interpolated;
  1340. }
  1341. void VisualServerCanvas::canvas_light_reset_physics_interpolation(RID p_light) {
  1342. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1343. ERR_FAIL_COND(!clight);
  1344. clight->xform_prev = clight->xform_curr;
  1345. }
  1346. void VisualServerCanvas::canvas_light_transform_physics_interpolation(RID p_light, const Transform2D &p_transform) {
  1347. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1348. ERR_FAIL_COND(!clight);
  1349. clight->xform_prev = p_transform * clight->xform_prev;
  1350. clight->xform_curr = p_transform * clight->xform_curr;
  1351. }
  1352. void VisualServerCanvas::canvas_light_occluder_set_interpolated(RID p_occluder, bool p_interpolated) {
  1353. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1354. ERR_FAIL_COND(!occluder);
  1355. occluder->interpolated = p_interpolated;
  1356. }
  1357. void VisualServerCanvas::canvas_light_occluder_reset_physics_interpolation(RID p_occluder) {
  1358. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1359. ERR_FAIL_COND(!occluder);
  1360. occluder->xform_prev = occluder->xform_curr;
  1361. }
  1362. void VisualServerCanvas::canvas_light_occluder_transform_physics_interpolation(RID p_occluder, const Transform2D &p_transform) {
  1363. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1364. ERR_FAIL_COND(!occluder);
  1365. occluder->xform_prev = p_transform * occluder->xform_prev;
  1366. occluder->xform_curr = p_transform * occluder->xform_curr;
  1367. }
  1368. void VisualServerCanvas::canvas_item_attach_skeleton(RID p_item, RID p_skeleton) {
  1369. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1370. ERR_FAIL_COND(!canvas_item);
  1371. if (_canvas_cull_mode == CANVAS_CULL_MODE_NODE) {
  1372. // No op?
  1373. if (canvas_item->skeleton == p_skeleton) {
  1374. return;
  1375. }
  1376. // Detach from any previous skeleton.
  1377. if (canvas_item->skeleton.is_valid()) {
  1378. VSG::storage->skeleton_attach_canvas_item(canvas_item->skeleton, p_item, false);
  1379. }
  1380. canvas_item->skeleton = p_skeleton;
  1381. // Attach to new skeleton.
  1382. if (p_skeleton.is_valid()) {
  1383. VSG::storage->skeleton_attach_canvas_item(p_skeleton, p_item, true);
  1384. }
  1385. _make_bound_dirty(canvas_item);
  1386. } else {
  1387. canvas_item->skeleton = p_skeleton;
  1388. }
  1389. }
  1390. // Canvas items may contain references to other resources (such as MultiMesh).
  1391. // If the resources are deleted first, and the canvas_item retains references, it
  1392. // will crash / error when it tries to access these.
  1393. void VisualServerCanvas::_canvas_item_remove_references(RID p_item, RID p_rid) {
  1394. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1395. ERR_FAIL_COND(!canvas_item);
  1396. canvas_item->remove_references(p_rid);
  1397. }
  1398. void VisualServerCanvas::_canvas_item_invalidate_local_bound(RID p_item) {
  1399. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1400. ERR_FAIL_COND(!canvas_item);
  1401. _make_bound_dirty(canvas_item);
  1402. }
  1403. void VisualServerCanvas::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect) {
  1404. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1405. ERR_FAIL_COND(!canvas_item);
  1406. if (bool(canvas_item->copy_back_buffer != nullptr) != p_enable) {
  1407. if (p_enable) {
  1408. canvas_item->copy_back_buffer = memnew(RasterizerCanvas::Item::CopyBackBuffer);
  1409. } else {
  1410. memdelete(canvas_item->copy_back_buffer);
  1411. canvas_item->copy_back_buffer = nullptr;
  1412. }
  1413. }
  1414. if (p_enable) {
  1415. canvas_item->copy_back_buffer->rect = p_rect;
  1416. canvas_item->copy_back_buffer->full = p_rect == Rect2();
  1417. }
  1418. _make_bound_dirty(canvas_item);
  1419. }
  1420. void VisualServerCanvas::canvas_item_clear(RID p_item) {
  1421. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1422. ERR_FAIL_COND(!canvas_item);
  1423. _make_bound_dirty(canvas_item);
  1424. canvas_item->clear();
  1425. }
  1426. void VisualServerCanvas::canvas_item_set_draw_index(RID p_item, int p_index) {
  1427. Item *canvas_item = canvas_item_owner.getornull(p_item);
  1428. ERR_FAIL_COND(!canvas_item);
  1429. canvas_item->index = p_index;
  1430. if (canvas_item_owner.owns(canvas_item->parent)) {
  1431. Item *canvas_item_parent = canvas_item_owner.getornull(canvas_item->parent);
  1432. canvas_item_parent->children_order_dirty = true;
  1433. return;
  1434. }
  1435. Canvas *canvas = canvas_owner.getornull(canvas_item->parent);
  1436. if (canvas) {
  1437. canvas->children_order_dirty = true;
  1438. return;
  1439. }
  1440. _check_bound_integrity(canvas_item);
  1441. }
  1442. void VisualServerCanvas::canvas_item_set_material(RID p_item, RID p_material) {
  1443. Item *canvas_item = canvas_item_owner.get(p_item);
  1444. ERR_FAIL_COND(!canvas_item);
  1445. canvas_item->material = p_material;
  1446. _make_bound_dirty(canvas_item);
  1447. }
  1448. void VisualServerCanvas::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  1449. Item *canvas_item = canvas_item_owner.get(p_item);
  1450. ERR_FAIL_COND(!canvas_item);
  1451. canvas_item->use_parent_material = p_enable;
  1452. _make_bound_dirty(canvas_item);
  1453. }
  1454. RID VisualServerCanvas::canvas_light_create() {
  1455. RasterizerCanvas::Light *clight = memnew(RasterizerCanvas::Light);
  1456. clight->light_internal = VSG::canvas_render->light_internal_create();
  1457. return canvas_light_owner.make_rid(clight);
  1458. }
  1459. void VisualServerCanvas::canvas_light_attach_to_canvas(RID p_light, RID p_canvas) {
  1460. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1461. ERR_FAIL_COND(!clight);
  1462. if (clight->canvas.is_valid()) {
  1463. Canvas *canvas = canvas_owner.getornull(clight->canvas);
  1464. canvas->lights.erase(clight);
  1465. }
  1466. if (!canvas_owner.owns(p_canvas)) {
  1467. p_canvas = RID();
  1468. }
  1469. clight->canvas = p_canvas;
  1470. if (clight->canvas.is_valid()) {
  1471. Canvas *canvas = canvas_owner.get(clight->canvas);
  1472. canvas->lights.insert(clight);
  1473. }
  1474. }
  1475. void VisualServerCanvas::canvas_light_set_enabled(RID p_light, bool p_enabled) {
  1476. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1477. ERR_FAIL_COND(!clight);
  1478. clight->enabled = p_enabled;
  1479. }
  1480. void VisualServerCanvas::canvas_light_set_scale(RID p_light, float p_scale) {
  1481. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1482. ERR_FAIL_COND(!clight);
  1483. clight->scale = p_scale;
  1484. }
  1485. void VisualServerCanvas::canvas_light_set_transform(RID p_light, const Transform2D &p_transform) {
  1486. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1487. ERR_FAIL_COND(!clight);
  1488. if (_interpolation_data.interpolation_enabled && clight->interpolated) {
  1489. if (!clight->on_interpolate_transform_list) {
  1490. _interpolation_data.canvas_light_transform_update_list_curr->push_back(p_light);
  1491. clight->on_interpolate_transform_list = true;
  1492. } else {
  1493. DEV_ASSERT(_interpolation_data.canvas_light_transform_update_list_curr->size());
  1494. }
  1495. }
  1496. clight->xform_curr = p_transform;
  1497. }
  1498. void VisualServerCanvas::canvas_light_set_texture(RID p_light, RID p_texture) {
  1499. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1500. ERR_FAIL_COND(!clight);
  1501. clight->texture = p_texture;
  1502. }
  1503. void VisualServerCanvas::canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset) {
  1504. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1505. ERR_FAIL_COND(!clight);
  1506. clight->texture_offset = p_offset;
  1507. }
  1508. void VisualServerCanvas::canvas_light_set_color(RID p_light, const Color &p_color) {
  1509. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1510. ERR_FAIL_COND(!clight);
  1511. clight->color = p_color;
  1512. }
  1513. void VisualServerCanvas::canvas_light_set_height(RID p_light, float p_height) {
  1514. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1515. ERR_FAIL_COND(!clight);
  1516. clight->height = p_height;
  1517. }
  1518. void VisualServerCanvas::canvas_light_set_energy(RID p_light, float p_energy) {
  1519. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1520. ERR_FAIL_COND(!clight);
  1521. clight->energy = p_energy;
  1522. }
  1523. void VisualServerCanvas::canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z) {
  1524. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1525. ERR_FAIL_COND(!clight);
  1526. clight->z_min = p_min_z;
  1527. clight->z_max = p_max_z;
  1528. }
  1529. void VisualServerCanvas::canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer) {
  1530. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1531. ERR_FAIL_COND(!clight);
  1532. clight->layer_max = p_max_layer;
  1533. clight->layer_min = p_min_layer;
  1534. }
  1535. void VisualServerCanvas::canvas_light_set_item_cull_mask(RID p_light, int p_mask) {
  1536. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1537. ERR_FAIL_COND(!clight);
  1538. clight->item_mask = p_mask;
  1539. }
  1540. void VisualServerCanvas::canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask) {
  1541. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1542. ERR_FAIL_COND(!clight);
  1543. clight->item_shadow_mask = p_mask;
  1544. }
  1545. void VisualServerCanvas::canvas_light_set_mode(RID p_light, VS::CanvasLightMode p_mode) {
  1546. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1547. ERR_FAIL_COND(!clight);
  1548. clight->mode = p_mode;
  1549. }
  1550. void VisualServerCanvas::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled) {
  1551. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1552. ERR_FAIL_COND(!clight);
  1553. if (clight->shadow_buffer.is_valid() == p_enabled) {
  1554. return;
  1555. }
  1556. if (p_enabled) {
  1557. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  1558. } else {
  1559. VSG::storage->free(clight->shadow_buffer);
  1560. clight->shadow_buffer = RID();
  1561. }
  1562. }
  1563. void VisualServerCanvas::canvas_light_set_shadow_buffer_size(RID p_light, int p_size) {
  1564. ERR_FAIL_COND(p_size < 32 || p_size > 16384);
  1565. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1566. ERR_FAIL_COND(!clight);
  1567. int new_size = next_power_of_2(p_size);
  1568. if (new_size == clight->shadow_buffer_size) {
  1569. return;
  1570. }
  1571. clight->shadow_buffer_size = next_power_of_2(p_size);
  1572. if (clight->shadow_buffer.is_valid()) {
  1573. VSG::storage->free(clight->shadow_buffer);
  1574. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  1575. }
  1576. }
  1577. void VisualServerCanvas::canvas_light_set_shadow_gradient_length(RID p_light, float p_length) {
  1578. ERR_FAIL_COND(p_length < 0);
  1579. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1580. ERR_FAIL_COND(!clight);
  1581. clight->shadow_gradient_length = p_length;
  1582. }
  1583. void VisualServerCanvas::canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter) {
  1584. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1585. ERR_FAIL_COND(!clight);
  1586. clight->shadow_filter = p_filter;
  1587. }
  1588. void VisualServerCanvas::canvas_light_set_shadow_color(RID p_light, const Color &p_color) {
  1589. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1590. ERR_FAIL_COND(!clight);
  1591. clight->shadow_color = p_color;
  1592. }
  1593. void VisualServerCanvas::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) {
  1594. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  1595. ERR_FAIL_COND(!clight);
  1596. clight->shadow_smooth = p_smooth;
  1597. }
  1598. RID VisualServerCanvas::canvas_light_occluder_create() {
  1599. RasterizerCanvas::LightOccluderInstance *occluder = memnew(RasterizerCanvas::LightOccluderInstance);
  1600. return canvas_light_occluder_owner.make_rid(occluder);
  1601. }
  1602. void VisualServerCanvas::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) {
  1603. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1604. ERR_FAIL_COND(!occluder);
  1605. if (occluder->canvas.is_valid()) {
  1606. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1607. canvas->occluders.erase(occluder);
  1608. }
  1609. if (!canvas_owner.owns(p_canvas)) {
  1610. p_canvas = RID();
  1611. }
  1612. occluder->canvas = p_canvas;
  1613. if (occluder->canvas.is_valid()) {
  1614. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1615. canvas->occluders.insert(occluder);
  1616. }
  1617. }
  1618. void VisualServerCanvas::canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled) {
  1619. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1620. ERR_FAIL_COND(!occluder);
  1621. occluder->enabled = p_enabled;
  1622. }
  1623. void VisualServerCanvas::canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon) {
  1624. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1625. ERR_FAIL_COND(!occluder);
  1626. if (occluder->polygon.is_valid()) {
  1627. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  1628. if (occluder_poly) {
  1629. occluder_poly->owners.erase(occluder);
  1630. }
  1631. }
  1632. occluder->polygon = p_polygon;
  1633. occluder->polygon_buffer = RID();
  1634. if (occluder->polygon.is_valid()) {
  1635. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  1636. if (!occluder_poly) {
  1637. occluder->polygon = RID();
  1638. ERR_FAIL_COND(!occluder_poly);
  1639. } else {
  1640. occluder_poly->owners.insert(occluder);
  1641. occluder->polygon_buffer = occluder_poly->occluder;
  1642. occluder->aabb_cache = occluder_poly->aabb;
  1643. occluder->cull_cache = occluder_poly->cull_mode;
  1644. }
  1645. }
  1646. }
  1647. void VisualServerCanvas::canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform) {
  1648. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1649. ERR_FAIL_COND(!occluder);
  1650. if (_interpolation_data.interpolation_enabled && occluder->interpolated) {
  1651. if (!occluder->on_interpolate_transform_list) {
  1652. _interpolation_data.canvas_light_occluder_transform_update_list_curr->push_back(p_occluder);
  1653. occluder->on_interpolate_transform_list = true;
  1654. } else {
  1655. DEV_ASSERT(_interpolation_data.canvas_light_occluder_transform_update_list_curr->size());
  1656. }
  1657. }
  1658. occluder->xform_curr = p_xform;
  1659. }
  1660. void VisualServerCanvas::canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask) {
  1661. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  1662. ERR_FAIL_COND(!occluder);
  1663. occluder->light_mask = p_mask;
  1664. }
  1665. RID VisualServerCanvas::canvas_occluder_polygon_create() {
  1666. LightOccluderPolygon *occluder_poly = memnew(LightOccluderPolygon);
  1667. occluder_poly->occluder = VSG::storage->canvas_light_occluder_create();
  1668. return canvas_light_occluder_polygon_owner.make_rid(occluder_poly);
  1669. }
  1670. void VisualServerCanvas::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape, bool p_closed) {
  1671. if (p_shape.size() < 3) {
  1672. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, p_shape);
  1673. return;
  1674. }
  1675. PoolVector<Vector2> lines;
  1676. int lc = p_shape.size() * 2;
  1677. lines.resize(lc - (p_closed ? 0 : 2));
  1678. {
  1679. PoolVector<Vector2>::Write w = lines.write();
  1680. PoolVector<Vector2>::Read r = p_shape.read();
  1681. int max = lc / 2;
  1682. if (!p_closed) {
  1683. max--;
  1684. }
  1685. for (int i = 0; i < max; i++) {
  1686. Vector2 a = r[i];
  1687. Vector2 b = r[(i + 1) % (lc / 2)];
  1688. w[i * 2 + 0] = a;
  1689. w[i * 2 + 1] = b;
  1690. }
  1691. }
  1692. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, lines);
  1693. }
  1694. void VisualServerCanvas::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape) {
  1695. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1696. ERR_FAIL_COND(!occluder_poly);
  1697. ERR_FAIL_COND(p_shape.size() & 1);
  1698. int lc = p_shape.size();
  1699. occluder_poly->aabb = Rect2();
  1700. {
  1701. PoolVector<Vector2>::Read r = p_shape.read();
  1702. for (int i = 0; i < lc; i++) {
  1703. if (i == 0) {
  1704. occluder_poly->aabb.position = r[i];
  1705. } else {
  1706. occluder_poly->aabb.expand_to(r[i]);
  1707. }
  1708. }
  1709. }
  1710. VSG::storage->canvas_light_occluder_set_polylines(occluder_poly->occluder, p_shape);
  1711. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1712. E->get()->aabb_cache = occluder_poly->aabb;
  1713. }
  1714. }
  1715. void VisualServerCanvas::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, VS::CanvasOccluderPolygonCullMode p_mode) {
  1716. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1717. ERR_FAIL_COND(!occluder_poly);
  1718. occluder_poly->cull_mode = p_mode;
  1719. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1720. E->get()->cull_cache = p_mode;
  1721. }
  1722. }
  1723. bool VisualServerCanvas::free(RID p_rid) {
  1724. if (canvas_owner.owns(p_rid)) {
  1725. Canvas *canvas = canvas_owner.get(p_rid);
  1726. ERR_FAIL_COND_V(!canvas, false);
  1727. while (canvas->viewports.size()) {
  1728. VisualServerViewport::Viewport *vp = VSG::viewport->viewport_owner.get(canvas->viewports.front()->get());
  1729. ERR_FAIL_COND_V(!vp, true);
  1730. Map<RID, VisualServerViewport::Viewport::CanvasData>::Element *E = vp->canvas_map.find(p_rid);
  1731. ERR_FAIL_COND_V(!E, true);
  1732. vp->canvas_map.erase(p_rid);
  1733. canvas->viewports.erase(canvas->viewports.front());
  1734. }
  1735. for (int i = 0; i < canvas->child_items.size(); i++) {
  1736. canvas->child_items[i].item->parent = RID();
  1737. }
  1738. for (Set<RasterizerCanvas::Light *>::Element *E = canvas->lights.front(); E; E = E->next()) {
  1739. E->get()->canvas = RID();
  1740. }
  1741. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = canvas->occluders.front(); E; E = E->next()) {
  1742. E->get()->canvas = RID();
  1743. }
  1744. canvas_owner.free(p_rid);
  1745. memdelete(canvas);
  1746. } else if (canvas_item_owner.owns(p_rid)) {
  1747. Item *canvas_item = canvas_item_owner.get(p_rid);
  1748. ERR_FAIL_COND_V(!canvas_item, true);
  1749. _make_bound_dirty(canvas_item);
  1750. _interpolation_data.notify_free_canvas_item(p_rid, *canvas_item);
  1751. if (canvas_item->parent.is_valid()) {
  1752. if (canvas_owner.owns(canvas_item->parent)) {
  1753. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  1754. canvas->erase_item(canvas_item);
  1755. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1756. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  1757. item_owner->child_items.erase(canvas_item);
  1758. if (item_owner->sort_y) {
  1759. _mark_ysort_dirty(item_owner, canvas_item_owner);
  1760. }
  1761. _check_bound_integrity(item_owner);
  1762. }
  1763. }
  1764. for (int i = 0; i < canvas_item->child_items.size(); i++) {
  1765. canvas_item->child_items[i]->parent = RID();
  1766. }
  1767. /*
  1768. if (canvas_item->material) {
  1769. canvas_item->material->owners.erase(canvas_item);
  1770. }
  1771. */
  1772. canvas_item_owner.free(p_rid);
  1773. memdelete(canvas_item);
  1774. } else if (canvas_light_owner.owns(p_rid)) {
  1775. RasterizerCanvas::Light *canvas_light = canvas_light_owner.get(p_rid);
  1776. ERR_FAIL_COND_V(!canvas_light, true);
  1777. _interpolation_data.notify_free_canvas_light(p_rid, *canvas_light);
  1778. if (canvas_light->canvas.is_valid()) {
  1779. Canvas *canvas = canvas_owner.get(canvas_light->canvas);
  1780. if (canvas) {
  1781. canvas->lights.erase(canvas_light);
  1782. }
  1783. }
  1784. if (canvas_light->shadow_buffer.is_valid()) {
  1785. VSG::storage->free(canvas_light->shadow_buffer);
  1786. }
  1787. VSG::canvas_render->light_internal_free(canvas_light->light_internal);
  1788. canvas_light_owner.free(p_rid);
  1789. memdelete(canvas_light);
  1790. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  1791. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid);
  1792. ERR_FAIL_COND_V(!occluder, true);
  1793. _interpolation_data.notify_free_canvas_light_occluder(p_rid, *occluder);
  1794. if (occluder->polygon.is_valid()) {
  1795. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  1796. if (occluder_poly) {
  1797. occluder_poly->owners.erase(occluder);
  1798. }
  1799. }
  1800. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  1801. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1802. canvas->occluders.erase(occluder);
  1803. }
  1804. canvas_light_occluder_owner.free(p_rid);
  1805. memdelete(occluder);
  1806. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  1807. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid);
  1808. ERR_FAIL_COND_V(!occluder_poly, true);
  1809. VSG::storage->free(occluder_poly->occluder);
  1810. while (occluder_poly->owners.size()) {
  1811. occluder_poly->owners.front()->get()->polygon = RID();
  1812. occluder_poly->owners.erase(occluder_poly->owners.front());
  1813. }
  1814. canvas_light_occluder_polygon_owner.free(p_rid);
  1815. memdelete(occluder_poly);
  1816. } else {
  1817. return false;
  1818. }
  1819. return true;
  1820. }
  1821. #ifdef VISUAL_SERVER_CANVAS_CHECK_BOUNDS
  1822. // Debugging function to check that the bound dirty flags in the tree make sense.
  1823. // Any item that has is dirty, all parents should be dirty up to the root
  1824. // (except in hidden branches, which are not kept track of for performance reasons).
  1825. bool VisualServerCanvas::_check_bound_integrity(const Item *p_item) {
  1826. while (p_item) {
  1827. if (canvas_item_owner.owns(p_item->parent)) {
  1828. p_item = canvas_item_owner.get(p_item->parent);
  1829. } else {
  1830. return _check_bound_integrity_down(p_item, p_item->bound_dirty);
  1831. }
  1832. }
  1833. return true;
  1834. }
  1835. bool VisualServerCanvas::_check_bound_integrity_down(const Item *p_item, bool p_bound_dirty) {
  1836. // don't care about integrity into invisible branches
  1837. if (!p_item->visible) {
  1838. return true;
  1839. }
  1840. if (p_item->bound_dirty) {
  1841. if (!p_bound_dirty) {
  1842. _print_tree(p_item);
  1843. ERR_PRINT("bound integrity check failed");
  1844. return false;
  1845. }
  1846. }
  1847. // go through children
  1848. int child_item_count = p_item->child_items.size();
  1849. Item *const *child_items = p_item->child_items.ptr();
  1850. for (int n = 0; n < child_item_count; n++) {
  1851. if (!_check_bound_integrity_down(child_items[n], p_bound_dirty)) {
  1852. return false;
  1853. }
  1854. }
  1855. return true;
  1856. }
  1857. void VisualServerCanvas::_print_tree(const Item *p_item) {
  1858. const Item *highlight = p_item;
  1859. while (p_item) {
  1860. if (canvas_item_owner.owns(p_item->parent)) {
  1861. p_item = canvas_item_owner.get(p_item->parent);
  1862. } else {
  1863. _print_tree_down(0, 0, p_item, highlight);
  1864. return;
  1865. }
  1866. }
  1867. }
  1868. void VisualServerCanvas::_print_tree_down(int p_child_id, int p_depth, const Item *p_item, const Item *p_highlight, bool p_hidden) {
  1869. String sz;
  1870. for (int n = 0; n < p_depth; n++) {
  1871. sz += "\t";
  1872. }
  1873. if (p_item == p_highlight) {
  1874. sz += "* ";
  1875. }
  1876. sz += itos(p_child_id) + " ";
  1877. #ifdef VISUAL_SERVER_CANVAS_DEBUG_ITEM_NAMES
  1878. sz += p_item->name + "\t";
  1879. #endif
  1880. sz += String(Variant(p_item->global_rect_cache)) + " ";
  1881. if (!p_item->visible) {
  1882. sz += "(H) ";
  1883. p_hidden = true;
  1884. } else if (p_hidden) {
  1885. sz += "(HI) ";
  1886. }
  1887. if (p_item->bound_dirty) {
  1888. sz += "(dirty) ";
  1889. }
  1890. if (p_item->parent == RID()) {
  1891. sz += "(parent NULL) ";
  1892. }
  1893. print_line(sz);
  1894. // go through children
  1895. int child_item_count = p_item->child_items.size();
  1896. Item *const *child_items = p_item->child_items.ptr();
  1897. for (int n = 0; n < child_item_count; n++) {
  1898. _print_tree_down(n, p_depth + 1, child_items[n], p_highlight, p_hidden);
  1899. }
  1900. }
  1901. #endif
  1902. void VisualServerCanvas::tick() {
  1903. if (_interpolation_data.interpolation_enabled) {
  1904. update_interpolation_tick(true);
  1905. }
  1906. }
  1907. void VisualServerCanvas::update_interpolation_tick(bool p_process) {
  1908. #define GODOT_UPDATE_INTERPOLATION_TICK(LIST_PREV, LIST_CURR, TYPE, OWNER_LIST) \
  1909. /* Detect any that were on the previous transform list that are no longer active. */ \
  1910. for (unsigned int n = 0; n < _interpolation_data.LIST_PREV->size(); n++) { \
  1911. const RID &rid = (*_interpolation_data.LIST_PREV)[n]; \
  1912. TYPE *item = OWNER_LIST.getornull(rid); \
  1913. /* no longer active? (either the instance deleted or no longer being transformed) */ \
  1914. if (item && !item->on_interpolate_transform_list) { \
  1915. item->xform_prev = item->xform_curr; \
  1916. } \
  1917. } \
  1918. /* and now for any in the transform list (being actively interpolated), */ \
  1919. /* keep the previous transform value up to date and ready for next tick */ \
  1920. if (p_process) { \
  1921. for (unsigned int n = 0; n < _interpolation_data.LIST_CURR->size(); n++) { \
  1922. const RID &rid = (*_interpolation_data.LIST_CURR)[n]; \
  1923. TYPE *item = OWNER_LIST.getornull(rid); \
  1924. if (item) { \
  1925. item->xform_prev = item->xform_curr; \
  1926. item->on_interpolate_transform_list = false; \
  1927. } \
  1928. } \
  1929. } \
  1930. SWAP(_interpolation_data.LIST_CURR, _interpolation_data.LIST_PREV); \
  1931. _interpolation_data.LIST_CURR->clear();
  1932. GODOT_UPDATE_INTERPOLATION_TICK(canvas_item_transform_update_list_prev, canvas_item_transform_update_list_curr, Item, canvas_item_owner);
  1933. GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_transform_update_list_prev, canvas_light_transform_update_list_curr, RasterizerCanvas::Light, canvas_light_owner);
  1934. GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_occluder_transform_update_list_prev, canvas_light_occluder_transform_update_list_curr, RasterizerCanvas::LightOccluderInstance, canvas_light_occluder_owner);
  1935. #undef GODOT_UPDATE_INTERPOLATION_TICK
  1936. }
  1937. void VisualServerCanvas::InterpolationData::notify_free_canvas_item(RID p_rid, VisualServerCanvas::Item &r_canvas_item) {
  1938. r_canvas_item.on_interpolate_transform_list = false;
  1939. if (!interpolation_enabled) {
  1940. return;
  1941. }
  1942. // If the instance was on any of the lists, remove.
  1943. canvas_item_transform_update_list_curr->erase_multiple_unordered(p_rid);
  1944. canvas_item_transform_update_list_prev->erase_multiple_unordered(p_rid);
  1945. }
  1946. void VisualServerCanvas::InterpolationData::notify_free_canvas_light(RID p_rid, RasterizerCanvas::Light &r_canvas_light) {
  1947. r_canvas_light.on_interpolate_transform_list = false;
  1948. if (!interpolation_enabled) {
  1949. return;
  1950. }
  1951. // If the instance was on any of the lists, remove.
  1952. canvas_light_transform_update_list_curr->erase_multiple_unordered(p_rid);
  1953. canvas_light_transform_update_list_prev->erase_multiple_unordered(p_rid);
  1954. }
  1955. void VisualServerCanvas::InterpolationData::notify_free_canvas_light_occluder(RID p_rid, RasterizerCanvas::LightOccluderInstance &r_canvas_light_occluder) {
  1956. r_canvas_light_occluder.on_interpolate_transform_list = false;
  1957. if (!interpolation_enabled) {
  1958. return;
  1959. }
  1960. // If the instance was on any of the lists, remove.
  1961. canvas_light_occluder_transform_update_list_curr->erase_multiple_unordered(p_rid);
  1962. canvas_light_occluder_transform_update_list_prev->erase_multiple_unordered(p_rid);
  1963. }
  1964. VisualServerCanvas::VisualServerCanvas() {
  1965. z_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1966. z_last_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1967. disable_scale = false;
  1968. int mode = GLOBAL_DEF("rendering/2d/options/culling_mode", 1);
  1969. ProjectSettings::get_singleton()->set_custom_property_info("rendering/2d/options/culling_mode", PropertyInfo(Variant::INT, "rendering/2d/options/culling_mode", PROPERTY_HINT_ENUM, "Item,Node"));
  1970. switch (mode) {
  1971. default: {
  1972. _canvas_cull_mode = CANVAS_CULL_MODE_NODE;
  1973. } break;
  1974. case 0: {
  1975. _canvas_cull_mode = CANVAS_CULL_MODE_ITEM;
  1976. } break;
  1977. }
  1978. }
  1979. VisualServerCanvas::~VisualServerCanvas() {
  1980. memfree(z_list);
  1981. memfree(z_last_list);
  1982. }