visual_server_canvas.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*************************************************************************/
  2. /* visual_server_canvas.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_server_canvas.h"
  31. #include "visual_server_globals.h"
  32. #include "visual_server_raster.h"
  33. #include "visual_server_viewport.h"
  34. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  35. 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) {
  36. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  37. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  38. _render_canvas_item(p_canvas_item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  39. VSG::canvas_render->canvas_render_items_begin(p_modulate, p_lights, p_transform);
  40. for (int i = 0; i < z_range; i++) {
  41. if (!z_list[i]) {
  42. continue;
  43. }
  44. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_modulate, p_lights, p_transform);
  45. }
  46. VSG::canvas_render->canvas_render_items_end();
  47. }
  48. 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) {
  49. int child_item_count = p_canvas_item->child_items.size();
  50. VisualServerCanvas::Item **child_items = p_canvas_item->child_items.ptrw();
  51. for (int i = 0; i < child_item_count; i++) {
  52. if (child_items[i]->visible) {
  53. if (r_items) {
  54. r_items[r_index] = child_items[i];
  55. child_items[i]->ysort_modulate = p_modulate;
  56. child_items[i]->ysort_xform = p_transform;
  57. child_items[i]->ysort_pos = p_transform.xform(child_items[i]->xform.elements[2]);
  58. child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : nullptr;
  59. child_items[i]->ysort_index = r_index;
  60. }
  61. r_index++;
  62. if (child_items[i]->sort_y) {
  63. _collect_ysort_children(child_items[i],
  64. p_transform * child_items[i]->xform,
  65. child_items[i]->use_parent_material ? p_material_owner : child_items[i],
  66. p_modulate * child_items[i]->modulate,
  67. r_items, r_index);
  68. }
  69. }
  70. }
  71. }
  72. void _mark_ysort_dirty(VisualServerCanvas::Item *ysort_owner, RID_Owner<VisualServerCanvas::Item> &canvas_item_owner) {
  73. do {
  74. ysort_owner->ysort_children_count = -1;
  75. ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.getornull(ysort_owner->parent) : nullptr;
  76. } while (ysort_owner && ysort_owner->sort_y);
  77. }
  78. void VisualServerCanvas::_render_canvas_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) {
  79. Item *ci = p_canvas_item;
  80. if (!ci->visible) {
  81. return;
  82. }
  83. if (ci->children_order_dirty) {
  84. ci->child_items.sort_custom<ItemIndexSort>();
  85. ci->children_order_dirty = false;
  86. }
  87. Rect2 rect = ci->get_rect();
  88. Transform2D xform = ci->xform;
  89. xform = p_transform * xform;
  90. Rect2 global_rect = xform.xform(rect);
  91. global_rect.position += p_clip_rect.position;
  92. if (ci->use_parent_material && p_material_owner) {
  93. ci->material_owner = p_material_owner;
  94. } else {
  95. p_material_owner = ci;
  96. ci->material_owner = nullptr;
  97. }
  98. 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);
  99. if (modulate.a < 0.007) {
  100. return;
  101. }
  102. int child_item_count = ci->child_items.size();
  103. Item **child_items = ci->child_items.ptrw();
  104. if (ci->clip) {
  105. if (p_canvas_clip != nullptr) {
  106. ci->final_clip_rect = p_canvas_clip->final_clip_rect.clip(global_rect);
  107. } else {
  108. ci->final_clip_rect = global_rect;
  109. }
  110. ci->final_clip_rect.position = ci->final_clip_rect.position.round();
  111. ci->final_clip_rect.size = ci->final_clip_rect.size.round();
  112. ci->final_clip_owner = ci;
  113. } else {
  114. ci->final_clip_owner = p_canvas_clip;
  115. }
  116. if (ci->sort_y) {
  117. if (ci->ysort_children_count == -1) {
  118. ci->ysort_children_count = 0;
  119. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count);
  120. }
  121. child_item_count = ci->ysort_children_count;
  122. child_items = (Item **)alloca(child_item_count * sizeof(Item *));
  123. int i = 0;
  124. _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i);
  125. SortArray<Item *, ItemPtrSort> sorter;
  126. sorter.sort(child_items, child_item_count);
  127. }
  128. if (ci->z_relative) {
  129. p_z = CLAMP(p_z + ci->z_index, VS::CANVAS_ITEM_Z_MIN, VS::CANVAS_ITEM_Z_MAX);
  130. } else {
  131. p_z = ci->z_index;
  132. }
  133. for (int i = 0; i < child_item_count; i++) {
  134. if (!child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  135. continue;
  136. }
  137. if (ci->sort_y) {
  138. _render_canvas_item(child_items[i], 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);
  139. } else {
  140. _render_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  141. }
  142. }
  143. if (ci->copy_back_buffer) {
  144. ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  145. }
  146. if (ci->update_when_visible) {
  147. VisualServerRaster::redraw_request();
  148. }
  149. if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect, true)) || ci->vp_render || ci->copy_back_buffer) {
  150. //something to draw?
  151. ci->final_transform = xform;
  152. 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);
  153. ci->global_rect_cache = global_rect;
  154. ci->global_rect_cache.position -= p_clip_rect.position;
  155. ci->light_masked = false;
  156. int zidx = p_z - VS::CANVAS_ITEM_Z_MIN;
  157. if (z_last_list[zidx]) {
  158. z_last_list[zidx]->next = ci;
  159. z_last_list[zidx] = ci;
  160. } else {
  161. z_list[zidx] = ci;
  162. z_last_list[zidx] = ci;
  163. }
  164. ci->next = nullptr;
  165. }
  166. for (int i = 0; i < child_item_count; i++) {
  167. if (child_items[i]->behind || (ci->sort_y && child_items[i]->sort_y)) {
  168. continue;
  169. }
  170. if (ci->sort_y) {
  171. _render_canvas_item(child_items[i], 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);
  172. } else {
  173. _render_canvas_item(child_items[i], xform, p_clip_rect, modulate, p_z, z_list, z_last_list, (Item *)ci->final_clip_owner, p_material_owner);
  174. }
  175. }
  176. }
  177. void VisualServerCanvas::_light_mask_canvas_items(int p_z, RasterizerCanvas::Item *p_canvas_item, RasterizerCanvas::Light *p_masked_lights, int p_canvas_layer_id) {
  178. RasterizerCanvas::Item *ci = p_canvas_item;
  179. while (ci) {
  180. RasterizerCanvas::Light *light = p_masked_lights;
  181. while (light) {
  182. 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))) {
  183. ci->light_masked = true;
  184. }
  185. light = light->mask_next_ptr;
  186. }
  187. ci = ci->next;
  188. }
  189. }
  190. 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) {
  191. VSG::canvas_render->canvas_begin();
  192. if (p_canvas->children_order_dirty) {
  193. p_canvas->child_items.sort();
  194. p_canvas->children_order_dirty = false;
  195. }
  196. int l = p_canvas->child_items.size();
  197. Canvas::ChildItem *ci = p_canvas->child_items.ptrw();
  198. bool has_mirror = false;
  199. for (int i = 0; i < l; i++) {
  200. if (ci[i].mirror.x || ci[i].mirror.y) {
  201. has_mirror = true;
  202. break;
  203. }
  204. }
  205. if (!has_mirror) {
  206. static const int z_range = VS::CANVAS_ITEM_Z_MAX - VS::CANVAS_ITEM_Z_MIN + 1;
  207. RasterizerCanvas::Item *z_list[z_range];
  208. RasterizerCanvas::Item *z_last_list[z_range];
  209. memset(z_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  210. memset(z_last_list, 0, z_range * sizeof(RasterizerCanvas::Item *));
  211. for (int i = 0; i < l; i++) {
  212. _render_canvas_item(ci[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr);
  213. }
  214. VSG::canvas_render->canvas_render_items_begin(p_canvas->modulate, p_lights, p_transform);
  215. for (int i = 0; i < z_range; i++) {
  216. if (!z_list[i]) {
  217. continue;
  218. }
  219. if (p_masked_lights) {
  220. _light_mask_canvas_items(VS::CANVAS_ITEM_Z_MIN + i, z_list[i], p_masked_lights, p_canvas_layer_id);
  221. }
  222. VSG::canvas_render->canvas_render_items(z_list[i], VS::CANVAS_ITEM_Z_MIN + i, p_canvas->modulate, p_lights, p_transform);
  223. }
  224. VSG::canvas_render->canvas_render_items_end();
  225. } else {
  226. for (int i = 0; i < l; i++) {
  227. const Canvas::ChildItem &ci2 = p_canvas->child_items[i];
  228. _render_canvas_item_tree(ci2.item, p_transform, p_clip_rect, p_canvas->modulate, p_lights);
  229. //mirroring (useful for scrolling backgrounds)
  230. if (ci2.mirror.x != 0) {
  231. Transform2D xform2 = p_transform * Transform2D(0, Vector2(ci2.mirror.x, 0));
  232. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  233. }
  234. if (ci2.mirror.y != 0) {
  235. Transform2D xform2 = p_transform * Transform2D(0, Vector2(0, ci2.mirror.y));
  236. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  237. }
  238. if (ci2.mirror.y != 0 && ci2.mirror.x != 0) {
  239. Transform2D xform2 = p_transform * Transform2D(0, ci2.mirror);
  240. _render_canvas_item_tree(ci2.item, xform2, p_clip_rect, p_canvas->modulate, p_lights);
  241. }
  242. }
  243. }
  244. VSG::canvas_render->canvas_end();
  245. }
  246. RID VisualServerCanvas::canvas_create() {
  247. Canvas *canvas = memnew(Canvas);
  248. ERR_FAIL_COND_V(!canvas, RID());
  249. RID rid = canvas_owner.make_rid(canvas);
  250. return rid;
  251. }
  252. void VisualServerCanvas::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) {
  253. Canvas *canvas = canvas_owner.getornull(p_canvas);
  254. ERR_FAIL_COND(!canvas);
  255. Item *canvas_item = canvas_item_owner.getornull(p_item);
  256. ERR_FAIL_COND(!canvas_item);
  257. int idx = canvas->find_item(canvas_item);
  258. ERR_FAIL_COND(idx == -1);
  259. canvas->child_items.write[idx].mirror = p_mirroring;
  260. }
  261. void VisualServerCanvas::canvas_set_modulate(RID p_canvas, const Color &p_color) {
  262. Canvas *canvas = canvas_owner.get(p_canvas);
  263. ERR_FAIL_COND(!canvas);
  264. canvas->modulate = p_color;
  265. }
  266. void VisualServerCanvas::canvas_set_disable_scale(bool p_disable) {
  267. disable_scale = p_disable;
  268. }
  269. void VisualServerCanvas::canvas_set_parent(RID p_canvas, RID p_parent, float p_scale) {
  270. Canvas *canvas = canvas_owner.get(p_canvas);
  271. ERR_FAIL_COND(!canvas);
  272. canvas->parent = p_parent;
  273. canvas->parent_scale = p_scale;
  274. }
  275. RID VisualServerCanvas::canvas_item_create() {
  276. Item *canvas_item = memnew(Item);
  277. ERR_FAIL_COND_V(!canvas_item, RID());
  278. return canvas_item_owner.make_rid(canvas_item);
  279. }
  280. void VisualServerCanvas::canvas_item_set_parent(RID p_item, RID p_parent) {
  281. Item *canvas_item = canvas_item_owner.getornull(p_item);
  282. ERR_FAIL_COND(!canvas_item);
  283. if (canvas_item->parent.is_valid()) {
  284. if (canvas_owner.owns(canvas_item->parent)) {
  285. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  286. canvas->erase_item(canvas_item);
  287. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  288. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  289. item_owner->child_items.erase(canvas_item);
  290. if (item_owner->sort_y) {
  291. _mark_ysort_dirty(item_owner, canvas_item_owner);
  292. }
  293. }
  294. canvas_item->parent = RID();
  295. }
  296. if (p_parent.is_valid()) {
  297. if (canvas_owner.owns(p_parent)) {
  298. Canvas *canvas = canvas_owner.get(p_parent);
  299. Canvas::ChildItem ci;
  300. ci.item = canvas_item;
  301. canvas->child_items.push_back(ci);
  302. canvas->children_order_dirty = true;
  303. } else if (canvas_item_owner.owns(p_parent)) {
  304. Item *item_owner = canvas_item_owner.get(p_parent);
  305. item_owner->child_items.push_back(canvas_item);
  306. item_owner->children_order_dirty = true;
  307. if (item_owner->sort_y) {
  308. _mark_ysort_dirty(item_owner, canvas_item_owner);
  309. }
  310. } else {
  311. ERR_FAIL_MSG("Invalid parent.");
  312. }
  313. }
  314. canvas_item->parent = p_parent;
  315. }
  316. void VisualServerCanvas::canvas_item_set_visible(RID p_item, bool p_visible) {
  317. Item *canvas_item = canvas_item_owner.getornull(p_item);
  318. ERR_FAIL_COND(!canvas_item);
  319. canvas_item->visible = p_visible;
  320. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  321. }
  322. void VisualServerCanvas::canvas_item_set_light_mask(RID p_item, int p_mask) {
  323. Item *canvas_item = canvas_item_owner.getornull(p_item);
  324. ERR_FAIL_COND(!canvas_item);
  325. canvas_item->light_mask = p_mask;
  326. }
  327. void VisualServerCanvas::canvas_item_set_transform(RID p_item, const Transform2D &p_transform) {
  328. Item *canvas_item = canvas_item_owner.getornull(p_item);
  329. ERR_FAIL_COND(!canvas_item);
  330. canvas_item->xform = p_transform;
  331. }
  332. void VisualServerCanvas::canvas_item_set_clip(RID p_item, bool p_clip) {
  333. Item *canvas_item = canvas_item_owner.getornull(p_item);
  334. ERR_FAIL_COND(!canvas_item);
  335. canvas_item->clip = p_clip;
  336. }
  337. void VisualServerCanvas::canvas_item_set_distance_field_mode(RID p_item, bool p_enable) {
  338. Item *canvas_item = canvas_item_owner.getornull(p_item);
  339. ERR_FAIL_COND(!canvas_item);
  340. canvas_item->distance_field = p_enable;
  341. }
  342. void VisualServerCanvas::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect) {
  343. Item *canvas_item = canvas_item_owner.getornull(p_item);
  344. ERR_FAIL_COND(!canvas_item);
  345. canvas_item->custom_rect = p_custom_rect;
  346. canvas_item->rect = p_rect;
  347. }
  348. void VisualServerCanvas::canvas_item_set_modulate(RID p_item, const Color &p_color) {
  349. Item *canvas_item = canvas_item_owner.getornull(p_item);
  350. ERR_FAIL_COND(!canvas_item);
  351. canvas_item->modulate = p_color;
  352. }
  353. void VisualServerCanvas::canvas_item_set_self_modulate(RID p_item, const Color &p_color) {
  354. Item *canvas_item = canvas_item_owner.getornull(p_item);
  355. ERR_FAIL_COND(!canvas_item);
  356. canvas_item->self_modulate = p_color;
  357. }
  358. void VisualServerCanvas::canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) {
  359. Item *canvas_item = canvas_item_owner.getornull(p_item);
  360. ERR_FAIL_COND(!canvas_item);
  361. canvas_item->behind = p_enable;
  362. }
  363. void VisualServerCanvas::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
  364. Item *canvas_item = canvas_item_owner.getornull(p_item);
  365. ERR_FAIL_COND(!canvas_item);
  366. canvas_item->update_when_visible = p_update;
  367. }
  368. 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) {
  369. Item *canvas_item = canvas_item_owner.getornull(p_item);
  370. ERR_FAIL_COND(!canvas_item);
  371. Item::CommandLine *line = memnew(Item::CommandLine);
  372. ERR_FAIL_COND(!line);
  373. line->color = p_color;
  374. line->from = p_from;
  375. line->to = p_to;
  376. line->width = p_width;
  377. line->antialiased = p_antialiased;
  378. canvas_item->rect_dirty = true;
  379. canvas_item->commands.push_back(line);
  380. }
  381. 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) {
  382. ERR_FAIL_COND(p_points.size() < 2);
  383. Item *canvas_item = canvas_item_owner.getornull(p_item);
  384. ERR_FAIL_COND(!canvas_item);
  385. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  386. ERR_FAIL_COND(!pline);
  387. pline->antialiased = p_antialiased;
  388. pline->multiline = false;
  389. if (p_width <= 1) {
  390. pline->lines = p_points;
  391. pline->line_colors = p_colors;
  392. if (pline->line_colors.size() == 0) {
  393. pline->line_colors.push_back(Color(1, 1, 1, 1));
  394. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  395. pline->line_colors.resize(1);
  396. }
  397. } else {
  398. //make a trianglestrip for drawing the line...
  399. Vector2 prev_t;
  400. pline->triangles.resize(p_points.size() * 2);
  401. if (p_antialiased) {
  402. pline->lines.resize(p_points.size() * 2);
  403. }
  404. if (p_colors.size() == 0) {
  405. pline->triangle_colors.push_back(Color(1, 1, 1, 1));
  406. if (p_antialiased) {
  407. pline->line_colors.push_back(Color(1, 1, 1, 1));
  408. }
  409. } else if (p_colors.size() == 1) {
  410. pline->triangle_colors = p_colors;
  411. pline->line_colors = p_colors;
  412. } else {
  413. if (p_colors.size() != p_points.size()) {
  414. pline->triangle_colors.push_back(p_colors[0]);
  415. pline->line_colors.push_back(p_colors[0]);
  416. } else {
  417. pline->triangle_colors.resize(pline->triangles.size());
  418. pline->line_colors.resize(pline->lines.size());
  419. }
  420. }
  421. for (int i = 0; i < p_points.size(); i++) {
  422. Vector2 t;
  423. if (i == p_points.size() - 1) {
  424. t = prev_t;
  425. } else {
  426. t = (p_points[i + 1] - p_points[i]).normalized().tangent();
  427. if (i == 0) {
  428. prev_t = t;
  429. }
  430. }
  431. Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;
  432. if (p_antialiased) {
  433. pline->lines.write[i] = p_points[i] + tangent;
  434. pline->lines.write[p_points.size() * 2 - i - 1] = p_points[i] - tangent;
  435. if (pline->line_colors.size() > 1) {
  436. pline->line_colors.write[i] = p_colors[i];
  437. pline->line_colors.write[p_points.size() * 2 - i - 1] = p_colors[i];
  438. }
  439. }
  440. pline->triangles.write[i * 2 + 0] = p_points[i] + tangent;
  441. pline->triangles.write[i * 2 + 1] = p_points[i] - tangent;
  442. if (pline->triangle_colors.size() > 1) {
  443. pline->triangle_colors.write[i * 2 + 0] = p_colors[i];
  444. pline->triangle_colors.write[i * 2 + 1] = p_colors[i];
  445. }
  446. prev_t = t;
  447. }
  448. }
  449. canvas_item->rect_dirty = true;
  450. canvas_item->commands.push_back(pline);
  451. }
  452. 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) {
  453. ERR_FAIL_COND(p_points.size() < 2);
  454. Item *canvas_item = canvas_item_owner.getornull(p_item);
  455. ERR_FAIL_COND(!canvas_item);
  456. Item::CommandPolyLine *pline = memnew(Item::CommandPolyLine);
  457. ERR_FAIL_COND(!pline);
  458. pline->antialiased = false; //todo
  459. pline->multiline = true;
  460. pline->lines = p_points;
  461. pline->line_colors = p_colors;
  462. if (pline->line_colors.size() == 0) {
  463. pline->line_colors.push_back(Color(1, 1, 1, 1));
  464. } else if (pline->line_colors.size() > 1 && pline->line_colors.size() != pline->lines.size()) {
  465. pline->line_colors.resize(1);
  466. }
  467. canvas_item->rect_dirty = true;
  468. canvas_item->commands.push_back(pline);
  469. }
  470. void VisualServerCanvas::canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color) {
  471. Item *canvas_item = canvas_item_owner.getornull(p_item);
  472. ERR_FAIL_COND(!canvas_item);
  473. Item::CommandRect *rect = memnew(Item::CommandRect);
  474. ERR_FAIL_COND(!rect);
  475. rect->modulate = p_color;
  476. rect->rect = p_rect;
  477. canvas_item->rect_dirty = true;
  478. canvas_item->commands.push_back(rect);
  479. }
  480. void VisualServerCanvas::canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color) {
  481. Item *canvas_item = canvas_item_owner.getornull(p_item);
  482. ERR_FAIL_COND(!canvas_item);
  483. Item::CommandCircle *circle = memnew(Item::CommandCircle);
  484. ERR_FAIL_COND(!circle);
  485. circle->color = p_color;
  486. circle->pos = p_pos;
  487. circle->radius = p_radius;
  488. canvas_item->commands.push_back(circle);
  489. }
  490. 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) {
  491. Item *canvas_item = canvas_item_owner.getornull(p_item);
  492. ERR_FAIL_COND(!canvas_item);
  493. Item::CommandRect *rect = memnew(Item::CommandRect);
  494. ERR_FAIL_COND(!rect);
  495. rect->modulate = p_modulate;
  496. rect->rect = p_rect;
  497. rect->flags = 0;
  498. if (p_tile) {
  499. rect->flags |= RasterizerCanvas::CANVAS_RECT_TILE;
  500. rect->flags |= RasterizerCanvas::CANVAS_RECT_REGION;
  501. rect->source = Rect2(0, 0, fabsf(p_rect.size.width), fabsf(p_rect.size.height));
  502. }
  503. if (p_rect.size.x < 0) {
  504. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  505. rect->rect.size.x = -rect->rect.size.x;
  506. }
  507. if (p_rect.size.y < 0) {
  508. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  509. rect->rect.size.y = -rect->rect.size.y;
  510. }
  511. if (p_transpose) {
  512. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  513. SWAP(rect->rect.size.x, rect->rect.size.y);
  514. }
  515. rect->texture = p_texture;
  516. rect->normal_map = p_normal_map;
  517. canvas_item->rect_dirty = true;
  518. canvas_item->commands.push_back(rect);
  519. }
  520. 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) {
  521. Item *canvas_item = canvas_item_owner.getornull(p_item);
  522. ERR_FAIL_COND(!canvas_item);
  523. Item::CommandRect *rect = memnew(Item::CommandRect);
  524. ERR_FAIL_COND(!rect);
  525. rect->modulate = p_modulate;
  526. rect->rect = p_rect;
  527. rect->texture = p_texture;
  528. rect->normal_map = p_normal_map;
  529. rect->source = p_src_rect;
  530. rect->flags = RasterizerCanvas::CANVAS_RECT_REGION;
  531. if (p_rect.size.x < 0) {
  532. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  533. rect->rect.size.x = -rect->rect.size.x;
  534. }
  535. if (p_src_rect.size.x < 0) {
  536. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_H;
  537. rect->source.size.x = -rect->source.size.x;
  538. }
  539. if (p_rect.size.y < 0) {
  540. rect->flags |= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  541. rect->rect.size.y = -rect->rect.size.y;
  542. }
  543. if (p_src_rect.size.y < 0) {
  544. rect->flags ^= RasterizerCanvas::CANVAS_RECT_FLIP_V;
  545. rect->source.size.y = -rect->source.size.y;
  546. }
  547. if (p_transpose) {
  548. rect->flags |= RasterizerCanvas::CANVAS_RECT_TRANSPOSE;
  549. SWAP(rect->rect.size.x, rect->rect.size.y);
  550. }
  551. if (p_clip_uv) {
  552. rect->flags |= RasterizerCanvas::CANVAS_RECT_CLIP_UV;
  553. }
  554. canvas_item->rect_dirty = true;
  555. canvas_item->commands.push_back(rect);
  556. }
  557. 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) {
  558. Item *canvas_item = canvas_item_owner.getornull(p_item);
  559. ERR_FAIL_COND(!canvas_item);
  560. Item::CommandNinePatch *style = memnew(Item::CommandNinePatch);
  561. ERR_FAIL_COND(!style);
  562. style->texture = p_texture;
  563. style->normal_map = p_normal_map;
  564. style->rect = p_rect;
  565. style->source = p_source;
  566. style->draw_center = p_draw_center;
  567. style->color = p_modulate;
  568. style->margin[MARGIN_LEFT] = p_topleft.x;
  569. style->margin[MARGIN_TOP] = p_topleft.y;
  570. style->margin[MARGIN_RIGHT] = p_bottomright.x;
  571. style->margin[MARGIN_BOTTOM] = p_bottomright.y;
  572. style->axis_x = p_x_axis_mode;
  573. style->axis_y = p_y_axis_mode;
  574. canvas_item->rect_dirty = true;
  575. canvas_item->commands.push_back(style);
  576. }
  577. 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) {
  578. Item *canvas_item = canvas_item_owner.getornull(p_item);
  579. ERR_FAIL_COND(!canvas_item);
  580. Item::CommandPrimitive *prim = memnew(Item::CommandPrimitive);
  581. ERR_FAIL_COND(!prim);
  582. prim->texture = p_texture;
  583. prim->normal_map = p_normal_map;
  584. prim->points = p_points;
  585. prim->uvs = p_uvs;
  586. prim->colors = p_colors;
  587. prim->width = p_width;
  588. canvas_item->rect_dirty = true;
  589. canvas_item->commands.push_back(prim);
  590. }
  591. 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) {
  592. Item *canvas_item = canvas_item_owner.getornull(p_item);
  593. ERR_FAIL_COND(!canvas_item);
  594. #ifdef DEBUG_ENABLED
  595. int pointcount = p_points.size();
  596. ERR_FAIL_COND(pointcount < 3);
  597. int color_size = p_colors.size();
  598. int uv_size = p_uvs.size();
  599. ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount);
  600. ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount));
  601. #endif
  602. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  603. ERR_FAIL_COND_MSG(indices.empty(), "Invalid polygon data, triangulation failed.");
  604. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  605. ERR_FAIL_COND(!polygon);
  606. polygon->texture = p_texture;
  607. polygon->normal_map = p_normal_map;
  608. polygon->points = p_points;
  609. polygon->uvs = p_uvs;
  610. polygon->colors = p_colors;
  611. polygon->indices = indices;
  612. polygon->count = indices.size();
  613. polygon->antialiased = p_antialiased;
  614. polygon->antialiasing_use_indices = false;
  615. canvas_item->rect_dirty = true;
  616. canvas_item->commands.push_back(polygon);
  617. }
  618. 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) {
  619. Item *canvas_item = canvas_item_owner.getornull(p_item);
  620. ERR_FAIL_COND(!canvas_item);
  621. int vertex_count = p_points.size();
  622. ERR_FAIL_COND(vertex_count == 0);
  623. ERR_FAIL_COND(!p_colors.empty() && p_colors.size() != vertex_count && p_colors.size() != 1);
  624. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size() != vertex_count);
  625. ERR_FAIL_COND(!p_bones.empty() && p_bones.size() != vertex_count * 4);
  626. ERR_FAIL_COND(!p_weights.empty() && p_weights.size() != vertex_count * 4);
  627. const Vector<int> &indices = p_indices;
  628. int count = p_count * 3;
  629. if (indices.empty()) {
  630. ERR_FAIL_COND(vertex_count % 3 != 0);
  631. if (p_count == -1) {
  632. count = vertex_count;
  633. }
  634. } else {
  635. ERR_FAIL_COND(indices.size() % 3 != 0);
  636. if (p_count == -1) {
  637. count = indices.size();
  638. }
  639. }
  640. Item::CommandPolygon *polygon = memnew(Item::CommandPolygon);
  641. ERR_FAIL_COND(!polygon);
  642. polygon->texture = p_texture;
  643. polygon->normal_map = p_normal_map;
  644. polygon->points = p_points;
  645. polygon->uvs = p_uvs;
  646. polygon->colors = p_colors;
  647. polygon->bones = p_bones;
  648. polygon->weights = p_weights;
  649. polygon->indices = indices;
  650. polygon->count = count;
  651. polygon->antialiased = p_antialiased;
  652. polygon->antialiasing_use_indices = p_antialiasing_use_indices;
  653. canvas_item->rect_dirty = true;
  654. canvas_item->commands.push_back(polygon);
  655. }
  656. void VisualServerCanvas::canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) {
  657. Item *canvas_item = canvas_item_owner.getornull(p_item);
  658. ERR_FAIL_COND(!canvas_item);
  659. Item::CommandTransform *tr = memnew(Item::CommandTransform);
  660. ERR_FAIL_COND(!tr);
  661. tr->xform = p_transform;
  662. canvas_item->commands.push_back(tr);
  663. }
  664. 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) {
  665. Item *canvas_item = canvas_item_owner.getornull(p_item);
  666. ERR_FAIL_COND(!canvas_item);
  667. Item::CommandMesh *m = memnew(Item::CommandMesh);
  668. ERR_FAIL_COND(!m);
  669. m->mesh = p_mesh;
  670. m->texture = p_texture;
  671. m->normal_map = p_normal_map;
  672. m->transform = p_transform;
  673. m->modulate = p_modulate;
  674. canvas_item->commands.push_back(m);
  675. }
  676. void VisualServerCanvas::canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture, RID p_normal) {
  677. Item *canvas_item = canvas_item_owner.getornull(p_item);
  678. ERR_FAIL_COND(!canvas_item);
  679. Item::CommandParticles *part = memnew(Item::CommandParticles);
  680. ERR_FAIL_COND(!part);
  681. part->particles = p_particles;
  682. part->texture = p_texture;
  683. part->normal_map = p_normal;
  684. //take the chance and request processing for them, at least once until they become visible again
  685. VSG::storage->particles_request_process(p_particles);
  686. canvas_item->rect_dirty = true;
  687. canvas_item->commands.push_back(part);
  688. }
  689. void VisualServerCanvas::canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture, RID p_normal_map) {
  690. Item *canvas_item = canvas_item_owner.getornull(p_item);
  691. ERR_FAIL_COND(!canvas_item);
  692. Item::CommandMultiMesh *mm = memnew(Item::CommandMultiMesh);
  693. ERR_FAIL_COND(!mm);
  694. mm->multimesh = p_mesh;
  695. mm->texture = p_texture;
  696. mm->normal_map = p_normal_map;
  697. canvas_item->rect_dirty = true;
  698. canvas_item->commands.push_back(mm);
  699. }
  700. void VisualServerCanvas::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  701. Item *canvas_item = canvas_item_owner.getornull(p_item);
  702. ERR_FAIL_COND(!canvas_item);
  703. Item::CommandClipIgnore *ci = memnew(Item::CommandClipIgnore);
  704. ERR_FAIL_COND(!ci);
  705. ci->ignore = p_ignore;
  706. canvas_item->commands.push_back(ci);
  707. }
  708. void VisualServerCanvas::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  709. Item *canvas_item = canvas_item_owner.getornull(p_item);
  710. ERR_FAIL_COND(!canvas_item);
  711. canvas_item->sort_y = p_enable;
  712. _mark_ysort_dirty(canvas_item, canvas_item_owner);
  713. }
  714. void VisualServerCanvas::canvas_item_set_z_index(RID p_item, int p_z) {
  715. ERR_FAIL_COND(p_z < VS::CANVAS_ITEM_Z_MIN || p_z > VS::CANVAS_ITEM_Z_MAX);
  716. Item *canvas_item = canvas_item_owner.getornull(p_item);
  717. ERR_FAIL_COND(!canvas_item);
  718. canvas_item->z_index = p_z;
  719. }
  720. void VisualServerCanvas::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  721. Item *canvas_item = canvas_item_owner.getornull(p_item);
  722. ERR_FAIL_COND(!canvas_item);
  723. canvas_item->z_relative = p_enable;
  724. }
  725. void VisualServerCanvas::canvas_item_attach_skeleton(RID p_item, RID p_skeleton) {
  726. Item *canvas_item = canvas_item_owner.getornull(p_item);
  727. ERR_FAIL_COND(!canvas_item);
  728. canvas_item->skeleton = p_skeleton;
  729. }
  730. void VisualServerCanvas::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect) {
  731. Item *canvas_item = canvas_item_owner.getornull(p_item);
  732. ERR_FAIL_COND(!canvas_item);
  733. if (bool(canvas_item->copy_back_buffer != nullptr) != p_enable) {
  734. if (p_enable) {
  735. canvas_item->copy_back_buffer = memnew(RasterizerCanvas::Item::CopyBackBuffer);
  736. } else {
  737. memdelete(canvas_item->copy_back_buffer);
  738. canvas_item->copy_back_buffer = nullptr;
  739. }
  740. }
  741. if (p_enable) {
  742. canvas_item->copy_back_buffer->rect = p_rect;
  743. canvas_item->copy_back_buffer->full = p_rect == Rect2();
  744. }
  745. }
  746. void VisualServerCanvas::canvas_item_clear(RID p_item) {
  747. Item *canvas_item = canvas_item_owner.getornull(p_item);
  748. ERR_FAIL_COND(!canvas_item);
  749. canvas_item->clear();
  750. }
  751. void VisualServerCanvas::canvas_item_set_draw_index(RID p_item, int p_index) {
  752. Item *canvas_item = canvas_item_owner.getornull(p_item);
  753. ERR_FAIL_COND(!canvas_item);
  754. canvas_item->index = p_index;
  755. if (canvas_item_owner.owns(canvas_item->parent)) {
  756. Item *canvas_item_parent = canvas_item_owner.getornull(canvas_item->parent);
  757. canvas_item_parent->children_order_dirty = true;
  758. return;
  759. }
  760. Canvas *canvas = canvas_owner.getornull(canvas_item->parent);
  761. if (canvas) {
  762. canvas->children_order_dirty = true;
  763. return;
  764. }
  765. }
  766. void VisualServerCanvas::canvas_item_set_material(RID p_item, RID p_material) {
  767. Item *canvas_item = canvas_item_owner.get(p_item);
  768. ERR_FAIL_COND(!canvas_item);
  769. canvas_item->material = p_material;
  770. }
  771. void VisualServerCanvas::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  772. Item *canvas_item = canvas_item_owner.get(p_item);
  773. ERR_FAIL_COND(!canvas_item);
  774. canvas_item->use_parent_material = p_enable;
  775. }
  776. RID VisualServerCanvas::canvas_light_create() {
  777. RasterizerCanvas::Light *clight = memnew(RasterizerCanvas::Light);
  778. clight->light_internal = VSG::canvas_render->light_internal_create();
  779. return canvas_light_owner.make_rid(clight);
  780. }
  781. void VisualServerCanvas::canvas_light_attach_to_canvas(RID p_light, RID p_canvas) {
  782. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  783. ERR_FAIL_COND(!clight);
  784. if (clight->canvas.is_valid()) {
  785. Canvas *canvas = canvas_owner.getornull(clight->canvas);
  786. canvas->lights.erase(clight);
  787. }
  788. if (!canvas_owner.owns(p_canvas)) {
  789. p_canvas = RID();
  790. }
  791. clight->canvas = p_canvas;
  792. if (clight->canvas.is_valid()) {
  793. Canvas *canvas = canvas_owner.get(clight->canvas);
  794. canvas->lights.insert(clight);
  795. }
  796. }
  797. void VisualServerCanvas::canvas_light_set_enabled(RID p_light, bool p_enabled) {
  798. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  799. ERR_FAIL_COND(!clight);
  800. clight->enabled = p_enabled;
  801. }
  802. void VisualServerCanvas::canvas_light_set_scale(RID p_light, float p_scale) {
  803. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  804. ERR_FAIL_COND(!clight);
  805. clight->scale = p_scale;
  806. }
  807. void VisualServerCanvas::canvas_light_set_transform(RID p_light, const Transform2D &p_transform) {
  808. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  809. ERR_FAIL_COND(!clight);
  810. clight->xform = p_transform;
  811. }
  812. void VisualServerCanvas::canvas_light_set_texture(RID p_light, RID p_texture) {
  813. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  814. ERR_FAIL_COND(!clight);
  815. clight->texture = p_texture;
  816. }
  817. void VisualServerCanvas::canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset) {
  818. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  819. ERR_FAIL_COND(!clight);
  820. clight->texture_offset = p_offset;
  821. }
  822. void VisualServerCanvas::canvas_light_set_color(RID p_light, const Color &p_color) {
  823. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  824. ERR_FAIL_COND(!clight);
  825. clight->color = p_color;
  826. }
  827. void VisualServerCanvas::canvas_light_set_height(RID p_light, float p_height) {
  828. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  829. ERR_FAIL_COND(!clight);
  830. clight->height = p_height;
  831. }
  832. void VisualServerCanvas::canvas_light_set_energy(RID p_light, float p_energy) {
  833. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  834. ERR_FAIL_COND(!clight);
  835. clight->energy = p_energy;
  836. }
  837. void VisualServerCanvas::canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z) {
  838. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  839. ERR_FAIL_COND(!clight);
  840. clight->z_min = p_min_z;
  841. clight->z_max = p_max_z;
  842. }
  843. void VisualServerCanvas::canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer) {
  844. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  845. ERR_FAIL_COND(!clight);
  846. clight->layer_max = p_max_layer;
  847. clight->layer_min = p_min_layer;
  848. }
  849. void VisualServerCanvas::canvas_light_set_item_cull_mask(RID p_light, int p_mask) {
  850. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  851. ERR_FAIL_COND(!clight);
  852. clight->item_mask = p_mask;
  853. }
  854. void VisualServerCanvas::canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask) {
  855. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  856. ERR_FAIL_COND(!clight);
  857. clight->item_shadow_mask = p_mask;
  858. }
  859. void VisualServerCanvas::canvas_light_set_mode(RID p_light, VS::CanvasLightMode p_mode) {
  860. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  861. ERR_FAIL_COND(!clight);
  862. clight->mode = p_mode;
  863. }
  864. void VisualServerCanvas::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled) {
  865. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  866. ERR_FAIL_COND(!clight);
  867. if (clight->shadow_buffer.is_valid() == p_enabled) {
  868. return;
  869. }
  870. if (p_enabled) {
  871. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  872. } else {
  873. VSG::storage->free(clight->shadow_buffer);
  874. clight->shadow_buffer = RID();
  875. }
  876. }
  877. void VisualServerCanvas::canvas_light_set_shadow_buffer_size(RID p_light, int p_size) {
  878. ERR_FAIL_COND(p_size < 32 || p_size > 16384);
  879. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  880. ERR_FAIL_COND(!clight);
  881. int new_size = next_power_of_2(p_size);
  882. if (new_size == clight->shadow_buffer_size) {
  883. return;
  884. }
  885. clight->shadow_buffer_size = next_power_of_2(p_size);
  886. if (clight->shadow_buffer.is_valid()) {
  887. VSG::storage->free(clight->shadow_buffer);
  888. clight->shadow_buffer = VSG::storage->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  889. }
  890. }
  891. void VisualServerCanvas::canvas_light_set_shadow_gradient_length(RID p_light, float p_length) {
  892. ERR_FAIL_COND(p_length < 0);
  893. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  894. ERR_FAIL_COND(!clight);
  895. clight->shadow_gradient_length = p_length;
  896. }
  897. void VisualServerCanvas::canvas_light_set_shadow_filter(RID p_light, VS::CanvasLightShadowFilter p_filter) {
  898. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  899. ERR_FAIL_COND(!clight);
  900. clight->shadow_filter = p_filter;
  901. }
  902. void VisualServerCanvas::canvas_light_set_shadow_color(RID p_light, const Color &p_color) {
  903. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  904. ERR_FAIL_COND(!clight);
  905. clight->shadow_color = p_color;
  906. }
  907. void VisualServerCanvas::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) {
  908. RasterizerCanvas::Light *clight = canvas_light_owner.get(p_light);
  909. ERR_FAIL_COND(!clight);
  910. clight->shadow_smooth = p_smooth;
  911. }
  912. RID VisualServerCanvas::canvas_light_occluder_create() {
  913. RasterizerCanvas::LightOccluderInstance *occluder = memnew(RasterizerCanvas::LightOccluderInstance);
  914. return canvas_light_occluder_owner.make_rid(occluder);
  915. }
  916. void VisualServerCanvas::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) {
  917. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  918. ERR_FAIL_COND(!occluder);
  919. if (occluder->canvas.is_valid()) {
  920. Canvas *canvas = canvas_owner.get(occluder->canvas);
  921. canvas->occluders.erase(occluder);
  922. }
  923. if (!canvas_owner.owns(p_canvas)) {
  924. p_canvas = RID();
  925. }
  926. occluder->canvas = p_canvas;
  927. if (occluder->canvas.is_valid()) {
  928. Canvas *canvas = canvas_owner.get(occluder->canvas);
  929. canvas->occluders.insert(occluder);
  930. }
  931. }
  932. void VisualServerCanvas::canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled) {
  933. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  934. ERR_FAIL_COND(!occluder);
  935. occluder->enabled = p_enabled;
  936. }
  937. void VisualServerCanvas::canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon) {
  938. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  939. ERR_FAIL_COND(!occluder);
  940. if (occluder->polygon.is_valid()) {
  941. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  942. if (occluder_poly) {
  943. occluder_poly->owners.erase(occluder);
  944. }
  945. }
  946. occluder->polygon = p_polygon;
  947. occluder->polygon_buffer = RID();
  948. if (occluder->polygon.is_valid()) {
  949. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  950. if (!occluder_poly) {
  951. occluder->polygon = RID();
  952. ERR_FAIL_COND(!occluder_poly);
  953. } else {
  954. occluder_poly->owners.insert(occluder);
  955. occluder->polygon_buffer = occluder_poly->occluder;
  956. occluder->aabb_cache = occluder_poly->aabb;
  957. occluder->cull_cache = occluder_poly->cull_mode;
  958. }
  959. }
  960. }
  961. void VisualServerCanvas::canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform) {
  962. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  963. ERR_FAIL_COND(!occluder);
  964. occluder->xform = p_xform;
  965. }
  966. void VisualServerCanvas::canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask) {
  967. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  968. ERR_FAIL_COND(!occluder);
  969. occluder->light_mask = p_mask;
  970. }
  971. RID VisualServerCanvas::canvas_occluder_polygon_create() {
  972. LightOccluderPolygon *occluder_poly = memnew(LightOccluderPolygon);
  973. occluder_poly->occluder = VSG::storage->canvas_light_occluder_create();
  974. return canvas_light_occluder_polygon_owner.make_rid(occluder_poly);
  975. }
  976. void VisualServerCanvas::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape, bool p_closed) {
  977. if (p_shape.size() < 3) {
  978. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, p_shape);
  979. return;
  980. }
  981. PoolVector<Vector2> lines;
  982. int lc = p_shape.size() * 2;
  983. lines.resize(lc - (p_closed ? 0 : 2));
  984. {
  985. PoolVector<Vector2>::Write w = lines.write();
  986. PoolVector<Vector2>::Read r = p_shape.read();
  987. int max = lc / 2;
  988. if (!p_closed) {
  989. max--;
  990. }
  991. for (int i = 0; i < max; i++) {
  992. Vector2 a = r[i];
  993. Vector2 b = r[(i + 1) % (lc / 2)];
  994. w[i * 2 + 0] = a;
  995. w[i * 2 + 1] = b;
  996. }
  997. }
  998. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon, lines);
  999. }
  1000. void VisualServerCanvas::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon, const PoolVector<Vector2> &p_shape) {
  1001. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1002. ERR_FAIL_COND(!occluder_poly);
  1003. ERR_FAIL_COND(p_shape.size() & 1);
  1004. int lc = p_shape.size();
  1005. occluder_poly->aabb = Rect2();
  1006. {
  1007. PoolVector<Vector2>::Read r = p_shape.read();
  1008. for (int i = 0; i < lc; i++) {
  1009. if (i == 0) {
  1010. occluder_poly->aabb.position = r[i];
  1011. } else {
  1012. occluder_poly->aabb.expand_to(r[i]);
  1013. }
  1014. }
  1015. }
  1016. VSG::storage->canvas_light_occluder_set_polylines(occluder_poly->occluder, p_shape);
  1017. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1018. E->get()->aabb_cache = occluder_poly->aabb;
  1019. }
  1020. }
  1021. void VisualServerCanvas::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, VS::CanvasOccluderPolygonCullMode p_mode) {
  1022. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  1023. ERR_FAIL_COND(!occluder_poly);
  1024. occluder_poly->cull_mode = p_mode;
  1025. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = occluder_poly->owners.front(); E; E = E->next()) {
  1026. E->get()->cull_cache = p_mode;
  1027. }
  1028. }
  1029. bool VisualServerCanvas::free(RID p_rid) {
  1030. if (canvas_owner.owns(p_rid)) {
  1031. Canvas *canvas = canvas_owner.get(p_rid);
  1032. ERR_FAIL_COND_V(!canvas, false);
  1033. while (canvas->viewports.size()) {
  1034. VisualServerViewport::Viewport *vp = VSG::viewport->viewport_owner.get(canvas->viewports.front()->get());
  1035. ERR_FAIL_COND_V(!vp, true);
  1036. Map<RID, VisualServerViewport::Viewport::CanvasData>::Element *E = vp->canvas_map.find(p_rid);
  1037. ERR_FAIL_COND_V(!E, true);
  1038. vp->canvas_map.erase(p_rid);
  1039. canvas->viewports.erase(canvas->viewports.front());
  1040. }
  1041. for (int i = 0; i < canvas->child_items.size(); i++) {
  1042. canvas->child_items[i].item->parent = RID();
  1043. }
  1044. for (Set<RasterizerCanvas::Light *>::Element *E = canvas->lights.front(); E; E = E->next()) {
  1045. E->get()->canvas = RID();
  1046. }
  1047. for (Set<RasterizerCanvas::LightOccluderInstance *>::Element *E = canvas->occluders.front(); E; E = E->next()) {
  1048. E->get()->canvas = RID();
  1049. }
  1050. canvas_owner.free(p_rid);
  1051. memdelete(canvas);
  1052. } else if (canvas_item_owner.owns(p_rid)) {
  1053. Item *canvas_item = canvas_item_owner.get(p_rid);
  1054. ERR_FAIL_COND_V(!canvas_item, true);
  1055. if (canvas_item->parent.is_valid()) {
  1056. if (canvas_owner.owns(canvas_item->parent)) {
  1057. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  1058. canvas->erase_item(canvas_item);
  1059. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1060. Item *item_owner = canvas_item_owner.get(canvas_item->parent);
  1061. item_owner->child_items.erase(canvas_item);
  1062. if (item_owner->sort_y) {
  1063. _mark_ysort_dirty(item_owner, canvas_item_owner);
  1064. }
  1065. }
  1066. }
  1067. for (int i = 0; i < canvas_item->child_items.size(); i++) {
  1068. canvas_item->child_items[i]->parent = RID();
  1069. }
  1070. /*
  1071. if (canvas_item->material) {
  1072. canvas_item->material->owners.erase(canvas_item);
  1073. }
  1074. */
  1075. canvas_item_owner.free(p_rid);
  1076. memdelete(canvas_item);
  1077. } else if (canvas_light_owner.owns(p_rid)) {
  1078. RasterizerCanvas::Light *canvas_light = canvas_light_owner.get(p_rid);
  1079. ERR_FAIL_COND_V(!canvas_light, true);
  1080. if (canvas_light->canvas.is_valid()) {
  1081. Canvas *canvas = canvas_owner.get(canvas_light->canvas);
  1082. if (canvas) {
  1083. canvas->lights.erase(canvas_light);
  1084. }
  1085. }
  1086. if (canvas_light->shadow_buffer.is_valid()) {
  1087. VSG::storage->free(canvas_light->shadow_buffer);
  1088. }
  1089. VSG::canvas_render->light_internal_free(canvas_light->light_internal);
  1090. canvas_light_owner.free(p_rid);
  1091. memdelete(canvas_light);
  1092. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  1093. RasterizerCanvas::LightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid);
  1094. ERR_FAIL_COND_V(!occluder, true);
  1095. if (occluder->polygon.is_valid()) {
  1096. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  1097. if (occluder_poly) {
  1098. occluder_poly->owners.erase(occluder);
  1099. }
  1100. }
  1101. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  1102. Canvas *canvas = canvas_owner.get(occluder->canvas);
  1103. canvas->occluders.erase(occluder);
  1104. }
  1105. canvas_light_occluder_owner.free(p_rid);
  1106. memdelete(occluder);
  1107. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  1108. LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid);
  1109. ERR_FAIL_COND_V(!occluder_poly, true);
  1110. VSG::storage->free(occluder_poly->occluder);
  1111. while (occluder_poly->owners.size()) {
  1112. occluder_poly->owners.front()->get()->polygon = RID();
  1113. occluder_poly->owners.erase(occluder_poly->owners.front());
  1114. }
  1115. canvas_light_occluder_polygon_owner.free(p_rid);
  1116. memdelete(occluder_poly);
  1117. } else {
  1118. return false;
  1119. }
  1120. return true;
  1121. }
  1122. VisualServerCanvas::VisualServerCanvas() {
  1123. z_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1124. z_last_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));
  1125. disable_scale = false;
  1126. }
  1127. VisualServerCanvas::~VisualServerCanvas() {
  1128. memfree(z_list);
  1129. memfree(z_last_list);
  1130. }