visual_server_canvas.cpp 46 KB

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