visual_server_canvas.cpp 42 KB

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