visual_server_canvas.cpp 44 KB

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