12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618 |
- /**************************************************************************/
- /* renderer_canvas_cull.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "renderer_canvas_cull.h"
- #include "core/config/project_settings.h"
- #include "core/math/geometry_2d.h"
- #include "core/math/transform_interpolator.h"
- #include "renderer_viewport.h"
- #include "rendering_server_default.h"
- #include "rendering_server_globals.h"
- #include "servers/rendering/storage/texture_storage.h"
- // Use the same antialiasing feather size as StyleBoxFlat's default
- // (but doubled, as it's specified for both sides here).
- // This value is empirically determined to provide good antialiasing quality
- // while not making lines appear too soft.
- const static float FEATHER_SIZE = 1.25f;
- void RendererCanvasCull::_render_canvas_item_tree(RID p_to_render_target, Canvas::ChildItem *p_child_items, int p_child_item_count, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_vertices_to_pixel, uint32_t p_canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info) {
- RENDER_TIMESTAMP("Cull CanvasItem Tree");
- memset(z_list, 0, z_range * sizeof(RendererCanvasRender::Item *));
- memset(z_last_list, 0, z_range * sizeof(RendererCanvasRender::Item *));
- for (int i = 0; i < p_child_item_count; i++) {
- _cull_canvas_item(p_child_items[i].item, p_transform, p_clip_rect, Color(1, 1, 1, 1), 0, z_list, z_last_list, nullptr, nullptr, true, p_canvas_cull_mask, Point2(), 1, nullptr);
- }
- RendererCanvasRender::Item *list = nullptr;
- RendererCanvasRender::Item *list_end = nullptr;
- for (int i = 0; i < z_range; i++) {
- if (!z_list[i]) {
- continue;
- }
- if (!list) {
- list = z_list[i];
- list_end = z_last_list[i];
- } else {
- list_end->next = z_list[i];
- list_end = z_last_list[i];
- }
- }
- RENDER_TIMESTAMP("Render CanvasItems");
- bool sdf_flag;
- RSG::canvas_render->canvas_render_items(p_to_render_target, list, p_modulate, p_lights, p_directional_lights, p_transform, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel, sdf_flag, r_render_info);
- if (sdf_flag) {
- sdf_used = true;
- }
- }
- void _collect_ysort_children(RendererCanvasCull::Item *p_canvas_item, const Transform2D &p_transform, RendererCanvasCull::Item *p_material_owner, const Color &p_modulate, RendererCanvasCull::Item **r_items, int &r_index, int p_z) {
- int child_item_count = p_canvas_item->child_items.size();
- RendererCanvasCull::Item **child_items = p_canvas_item->child_items.ptrw();
- for (int i = 0; i < child_item_count; i++) {
- int abs_z = 0;
- if (child_items[i]->visible) {
- if (r_items) {
- r_items[r_index] = child_items[i];
- child_items[i]->ysort_xform = p_transform;
- child_items[i]->ysort_pos = p_transform.xform(child_items[i]->xform_curr.columns[2]);
- child_items[i]->material_owner = child_items[i]->use_parent_material ? p_material_owner : nullptr;
- child_items[i]->ysort_modulate = p_modulate;
- child_items[i]->ysort_index = r_index;
- child_items[i]->ysort_parent_abs_z_index = p_z;
- if (!child_items[i]->repeat_source) {
- child_items[i]->repeat_size = p_canvas_item->repeat_size;
- child_items[i]->repeat_times = p_canvas_item->repeat_times;
- child_items[i]->repeat_source_item = p_canvas_item->repeat_source_item;
- }
- // Y sorted canvas items are flattened into r_items. Calculate their absolute z index to use when rendering r_items.
- if (child_items[i]->z_relative) {
- abs_z = CLAMP(p_z + child_items[i]->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX);
- } else {
- abs_z = child_items[i]->z_index;
- }
- }
- r_index++;
- if (child_items[i]->sort_y) {
- _collect_ysort_children(child_items[i], p_transform * child_items[i]->xform_curr, child_items[i]->use_parent_material ? p_material_owner : child_items[i], p_modulate * child_items[i]->modulate, r_items, r_index, abs_z);
- }
- }
- }
- }
- void _mark_ysort_dirty(RendererCanvasCull::Item *ysort_owner, RID_Owner<RendererCanvasCull::Item, true> &canvas_item_owner) {
- do {
- ysort_owner->ysort_children_count = -1;
- ysort_owner = canvas_item_owner.owns(ysort_owner->parent) ? canvas_item_owner.get_or_null(ysort_owner->parent) : nullptr;
- } while (ysort_owner && ysort_owner->sort_y);
- }
- void RendererCanvasCull::_attach_canvas_item_for_draw(RendererCanvasCull::Item *ci, RendererCanvasCull::Item *p_canvas_clip, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, const Transform2D &p_transform, const Rect2 &p_clip_rect, Rect2 p_global_rect, const Color &p_modulate, int p_z, RendererCanvasCull::Item *p_material_owner, bool p_use_canvas_group, RendererCanvasRender::Item *r_canvas_group_from) {
- if (ci->copy_back_buffer) {
- ci->copy_back_buffer->screen_rect = p_transform.xform(ci->copy_back_buffer->rect).intersection(p_clip_rect);
- }
- if (p_use_canvas_group) {
- int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
- if (r_canvas_group_from == nullptr) {
- // no list before processing this item, means must put stuff in group from the beginning of list.
- r_canvas_group_from = r_z_list[zidx];
- } else {
- // there was a list before processing, so begin group from this one.
- r_canvas_group_from = r_canvas_group_from->next;
- }
- if (r_canvas_group_from) {
- // Has a place to begin the group from!
- //compute a global rect (in global coords) for children in the same z layer
- Rect2 rect_accum;
- RendererCanvasRender::Item *c = r_canvas_group_from;
- while (c) {
- if (c == r_canvas_group_from) {
- rect_accum = c->global_rect_cache;
- } else {
- rect_accum = rect_accum.merge(c->global_rect_cache);
- }
- c = c->next;
- }
- // We have two choices now, if user has drawn something, we must assume users wants to draw the "mask", so compute the size based on this.
- // If nothing has been drawn, we just take it over and draw it ourselves.
- if (ci->canvas_group->fit_empty && (ci->commands == nullptr || (ci->commands->next == nullptr && ci->commands->type == RendererCanvasCull::Item::Command::TYPE_RECT && (static_cast<RendererCanvasCull::Item::CommandRect *>(ci->commands)->flags & RendererCanvasRender::CANVAS_RECT_IS_GROUP)))) {
- // No commands, or sole command is the one used to draw, so we (re)create the draw command.
- ci->clear();
- if (rect_accum == Rect2()) {
- rect_accum.size = Size2(1, 1);
- }
- rect_accum = rect_accum.grow(ci->canvas_group->fit_margin);
- //draw it?
- RendererCanvasRender::Item::CommandRect *crect = ci->alloc_command<RendererCanvasRender::Item::CommandRect>();
- crect->flags = RendererCanvasRender::CANVAS_RECT_IS_GROUP; // so we can recognize it later
- crect->rect = p_transform.affine_inverse().xform(rect_accum);
- crect->modulate = Color(1, 1, 1, 1);
- //the global rect is used to do the copying, so update it
- p_global_rect = rect_accum.grow(ci->canvas_group->clear_margin); //grow again by clear margin
- p_global_rect.position += p_clip_rect.position;
- } else {
- p_global_rect.position -= p_clip_rect.position;
- p_global_rect = p_global_rect.merge(rect_accum); //must use both rects for this
- p_global_rect = p_global_rect.grow(ci->canvas_group->clear_margin); //grow by clear margin
- p_global_rect.position += p_clip_rect.position;
- }
- // Very important that this is cleared after used in RendererCanvasRender to avoid
- // potential crashes.
- r_canvas_group_from->canvas_group_owner = ci;
- }
- }
- if (((ci->commands != nullptr || ci->visibility_notifier) && p_clip_rect.intersects(p_global_rect, true)) || ci->vp_render || ci->copy_back_buffer) {
- //something to draw?
- if (ci->update_when_visible) {
- RenderingServerDefault::redraw_request();
- }
- if (ci->commands != nullptr || ci->copy_back_buffer) {
- ci->final_transform = p_transform;
- ci->final_modulate = p_modulate * ci->self_modulate;
- ci->global_rect_cache = p_global_rect;
- ci->global_rect_cache.position -= p_clip_rect.position;
- ci->light_masked = false;
- int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
- if (r_z_last_list[zidx]) {
- r_z_last_list[zidx]->next = ci;
- r_z_last_list[zidx] = ci;
- } else {
- r_z_list[zidx] = ci;
- r_z_last_list[zidx] = ci;
- }
- ci->z_final = p_z;
- ci->next = nullptr;
- }
- if (ci->visibility_notifier) {
- if (!ci->visibility_notifier->visible_element.in_list()) {
- visibility_notifier_list.add(&ci->visibility_notifier->visible_element);
- ci->visibility_notifier->just_visible = true;
- }
- ci->visibility_notifier->visible_in_frame = RSG::rasterizer->get_frame_number();
- }
- } else if (ci->repeat_source) {
- // If repeat source does not draw itself it still needs transform updated as its child items' repeat offsets are relative to it.
- ci->final_transform = p_transform;
- }
- }
- void RendererCanvasCull::_cull_canvas_item(Item *p_canvas_item, const Transform2D &p_parent_xform, const Rect2 &p_clip_rect, const Color &p_modulate, int p_z, RendererCanvasRender::Item **r_z_list, RendererCanvasRender::Item **r_z_last_list, Item *p_canvas_clip, Item *p_material_owner, bool p_allow_y_sort, uint32_t p_canvas_cull_mask, const Point2 &p_repeat_size, int p_repeat_times, RendererCanvasRender::Item *p_repeat_source_item) {
- Item *ci = p_canvas_item;
- if (!ci->visible) {
- return;
- }
- if (!(ci->visibility_layer & p_canvas_cull_mask)) {
- return;
- }
- if (ci->children_order_dirty) {
- ci->child_items.sort_custom<ItemIndexSort>();
- ci->children_order_dirty = false;
- }
- Rect2 rect = ci->get_rect();
- if (ci->visibility_notifier) {
- if (ci->visibility_notifier->area.size != Vector2()) {
- rect = rect.merge(ci->visibility_notifier->area);
- }
- }
- Transform2D final_xform;
- if (!_interpolation_data.interpolation_enabled || !ci->interpolated) {
- final_xform = ci->xform_curr;
- } else {
- real_t f = Engine::get_singleton()->get_physics_interpolation_fraction();
- TransformInterpolator::interpolate_transform_2d(ci->xform_prev, ci->xform_curr, final_xform, f);
- }
- Transform2D parent_xform = p_parent_xform;
- Point2 repeat_size = p_repeat_size;
- int repeat_times = p_repeat_times;
- RendererCanvasRender::Item *repeat_source_item = p_repeat_source_item;
- if (ci->repeat_source) {
- repeat_size = ci->repeat_size;
- repeat_times = ci->repeat_times;
- repeat_source_item = ci;
- } else {
- ci->repeat_size = repeat_size;
- ci->repeat_times = repeat_times;
- ci->repeat_source_item = repeat_source_item;
- }
- if (snapping_2d_transforms_to_pixel) {
- final_xform.columns[2] = (final_xform.columns[2] + Point2(0.5, 0.5)).floor();
- parent_xform.columns[2] = (parent_xform.columns[2] + Point2(0.5, 0.5)).floor();
- }
- final_xform = parent_xform * final_xform;
- Rect2 global_rect = final_xform.xform(rect);
- if (repeat_source_item && (repeat_size.x || repeat_size.y)) {
- // Top-left repeated rect.
- Rect2 corner_rect = global_rect;
- corner_rect.position -= repeat_source_item->final_transform.basis_xform((repeat_times / 2) * repeat_size);
- global_rect = corner_rect;
- // Plus top-right repeated rect.
- Size2 size_x_offset = repeat_source_item->final_transform.basis_xform(repeat_times * Size2(repeat_size.x, 0));
- corner_rect.position += size_x_offset;
- global_rect = global_rect.merge(corner_rect);
- // Plus bottom-right repeated rect.
- corner_rect.position += repeat_source_item->final_transform.basis_xform(repeat_times * Size2(0, repeat_size.y));
- global_rect = global_rect.merge(corner_rect);
- // Plus bottom-left repeated rect.
- corner_rect.position -= size_x_offset;
- global_rect = global_rect.merge(corner_rect);
- }
- global_rect.position += p_clip_rect.position;
- if (ci->use_parent_material && p_material_owner) {
- ci->material_owner = p_material_owner;
- } else {
- p_material_owner = ci;
- ci->material_owner = nullptr;
- }
- 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);
- if (modulate.a < 0.007) {
- return;
- }
- int child_item_count = ci->child_items.size();
- Item **child_items = ci->child_items.ptrw();
- if (ci->clip) {
- if (p_canvas_clip != nullptr) {
- ci->final_clip_rect = p_canvas_clip->final_clip_rect.intersection(global_rect);
- } else {
- ci->final_clip_rect = p_clip_rect.intersection(global_rect);
- }
- if (ci->final_clip_rect.size.width < 0.5 || ci->final_clip_rect.size.height < 0.5) {
- // The clip rect area is 0, so don't draw the item.
- return;
- }
- ci->final_clip_rect.position = ci->final_clip_rect.position.round();
- ci->final_clip_rect.size = ci->final_clip_rect.size.round();
- ci->final_clip_owner = ci;
- } else {
- ci->final_clip_owner = p_canvas_clip;
- }
- int parent_z = p_z;
- if (ci->z_relative) {
- p_z = CLAMP(p_z + ci->z_index, RS::CANVAS_ITEM_Z_MIN, RS::CANVAS_ITEM_Z_MAX);
- } else {
- p_z = ci->z_index;
- }
- if (ci->sort_y) {
- if (p_allow_y_sort) {
- if (ci->ysort_children_count == -1) {
- ci->ysort_children_count = 0;
- _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), nullptr, ci->ysort_children_count, p_z);
- }
- child_item_count = ci->ysort_children_count + 1;
- child_items = (Item **)alloca(child_item_count * sizeof(Item *));
- ci->ysort_xform = ci->xform_curr.affine_inverse();
- ci->ysort_pos = Vector2();
- ci->ysort_modulate = Color(1, 1, 1, 1);
- ci->ysort_index = 0;
- ci->ysort_parent_abs_z_index = parent_z;
- child_items[0] = ci;
- int i = 1;
- _collect_ysort_children(ci, Transform2D(), p_material_owner, Color(1, 1, 1, 1), child_items, i, p_z);
- SortArray<Item *, ItemPtrSort> sorter;
- sorter.sort(child_items, child_item_count);
- for (i = 0; i < child_item_count; i++) {
- _cull_canvas_item(child_items[i], final_xform * child_items[i]->ysort_xform, p_clip_rect, modulate * child_items[i]->ysort_modulate, child_items[i]->ysort_parent_abs_z_index, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, (Item *)child_items[i]->material_owner, false, p_canvas_cull_mask, child_items[i]->repeat_size, child_items[i]->repeat_times, child_items[i]->repeat_source_item);
- }
- } else {
- RendererCanvasRender::Item *canvas_group_from = nullptr;
- bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr);
- if (use_canvas_group) {
- int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
- canvas_group_from = r_z_last_list[zidx];
- }
- _attach_canvas_item_for_draw(ci, p_canvas_clip, r_z_list, r_z_last_list, final_xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from);
- }
- } else {
- RendererCanvasRender::Item *canvas_group_from = nullptr;
- bool use_canvas_group = ci->canvas_group != nullptr && (ci->canvas_group->fit_empty || ci->commands != nullptr);
- if (use_canvas_group) {
- int zidx = p_z - RS::CANVAS_ITEM_Z_MIN;
- canvas_group_from = r_z_last_list[zidx];
- }
- for (int i = 0; i < child_item_count; i++) {
- if (!child_items[i]->behind && !use_canvas_group) {
- continue;
- }
- _cull_canvas_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, p_material_owner, true, p_canvas_cull_mask, repeat_size, repeat_times, repeat_source_item);
- }
- _attach_canvas_item_for_draw(ci, p_canvas_clip, r_z_list, r_z_last_list, final_xform, p_clip_rect, global_rect, modulate, p_z, p_material_owner, use_canvas_group, canvas_group_from);
- for (int i = 0; i < child_item_count; i++) {
- if (child_items[i]->behind || use_canvas_group) {
- continue;
- }
- _cull_canvas_item(child_items[i], final_xform, p_clip_rect, modulate, p_z, r_z_list, r_z_last_list, (Item *)ci->final_clip_owner, p_material_owner, true, p_canvas_cull_mask, repeat_size, repeat_times, repeat_source_item);
- }
- }
- }
- void RendererCanvasCull::render_canvas(RID p_render_target, Canvas *p_canvas, const Transform2D &p_transform, RendererCanvasRender::Light *p_lights, RendererCanvasRender::Light *p_directional_lights, const Rect2 &p_clip_rect, RenderingServer::CanvasItemTextureFilter p_default_filter, RenderingServer::CanvasItemTextureRepeat p_default_repeat, bool p_snap_2d_transforms_to_pixel, bool p_snap_2d_vertices_to_pixel, uint32_t canvas_cull_mask, RenderingMethod::RenderInfo *r_render_info) {
- RENDER_TIMESTAMP("> Render Canvas");
- sdf_used = false;
- snapping_2d_transforms_to_pixel = p_snap_2d_transforms_to_pixel;
- if (p_canvas->children_order_dirty) {
- p_canvas->child_items.sort();
- p_canvas->children_order_dirty = false;
- }
- int l = p_canvas->child_items.size();
- Canvas::ChildItem *ci = p_canvas->child_items.ptrw();
- _render_canvas_item_tree(p_render_target, ci, l, p_transform, p_clip_rect, p_canvas->modulate, p_lights, p_directional_lights, p_default_filter, p_default_repeat, p_snap_2d_vertices_to_pixel, canvas_cull_mask, r_render_info);
- RENDER_TIMESTAMP("< Render Canvas");
- }
- bool RendererCanvasCull::was_sdf_used() {
- return sdf_used;
- }
- RID RendererCanvasCull::canvas_allocate() {
- return canvas_owner.allocate_rid();
- }
- void RendererCanvasCull::canvas_initialize(RID p_rid) {
- canvas_owner.initialize_rid(p_rid);
- }
- void RendererCanvasCull::canvas_set_item_mirroring(RID p_canvas, RID p_item, const Point2 &p_mirroring) {
- Canvas *canvas = canvas_owner.get_or_null(p_canvas);
- ERR_FAIL_NULL(canvas);
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- int idx = canvas->find_item(canvas_item);
- ERR_FAIL_COND(idx == -1);
- bool is_repeat_source = (p_mirroring.x || p_mirroring.y);
- canvas_item->repeat_source = is_repeat_source;
- canvas_item->repeat_source_item = is_repeat_source ? canvas_item : nullptr;
- canvas_item->repeat_size = p_mirroring;
- canvas_item->repeat_times = 1;
- }
- void RendererCanvasCull::canvas_set_item_repeat(RID p_item, const Point2 &p_repeat_size, int p_repeat_times) {
- ERR_FAIL_COND(p_repeat_times < 0);
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- bool is_repeat_source = (p_repeat_size.x || p_repeat_size.y) && p_repeat_times;
- canvas_item->repeat_source = is_repeat_source;
- canvas_item->repeat_source_item = is_repeat_source ? canvas_item : nullptr;
- canvas_item->repeat_size = p_repeat_size;
- canvas_item->repeat_times = p_repeat_times;
- }
- void RendererCanvasCull::canvas_set_modulate(RID p_canvas, const Color &p_color) {
- Canvas *canvas = canvas_owner.get_or_null(p_canvas);
- ERR_FAIL_NULL(canvas);
- canvas->modulate = p_color;
- }
- void RendererCanvasCull::canvas_set_disable_scale(bool p_disable) {
- disable_scale = p_disable;
- }
- void RendererCanvasCull::canvas_set_parent(RID p_canvas, RID p_parent, float p_scale) {
- Canvas *canvas = canvas_owner.get_or_null(p_canvas);
- ERR_FAIL_NULL(canvas);
- canvas->parent = p_parent;
- canvas->parent_scale = p_scale;
- }
- RID RendererCanvasCull::canvas_item_allocate() {
- return canvas_item_owner.allocate_rid();
- }
- void RendererCanvasCull::canvas_item_initialize(RID p_rid) {
- canvas_item_owner.initialize_rid(p_rid);
- }
- void RendererCanvasCull::canvas_item_set_parent(RID p_item, RID p_parent) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (canvas_item->parent.is_valid()) {
- if (canvas_owner.owns(canvas_item->parent)) {
- Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
- canvas->erase_item(canvas_item);
- } else if (canvas_item_owner.owns(canvas_item->parent)) {
- Item *item_owner = canvas_item_owner.get_or_null(canvas_item->parent);
- item_owner->child_items.erase(canvas_item);
- if (item_owner->sort_y) {
- _mark_ysort_dirty(item_owner, canvas_item_owner);
- }
- }
- canvas_item->parent = RID();
- }
- if (p_parent.is_valid()) {
- if (canvas_owner.owns(p_parent)) {
- Canvas *canvas = canvas_owner.get_or_null(p_parent);
- Canvas::ChildItem ci;
- ci.item = canvas_item;
- canvas->child_items.push_back(ci);
- canvas->children_order_dirty = true;
- } else if (canvas_item_owner.owns(p_parent)) {
- Item *item_owner = canvas_item_owner.get_or_null(p_parent);
- item_owner->child_items.push_back(canvas_item);
- item_owner->children_order_dirty = true;
- if (item_owner->sort_y) {
- _mark_ysort_dirty(item_owner, canvas_item_owner);
- }
- } else {
- ERR_FAIL_MSG("Invalid parent.");
- }
- }
- canvas_item->parent = p_parent;
- }
- void RendererCanvasCull::canvas_item_set_visible(RID p_item, bool p_visible) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->visible = p_visible;
- _mark_ysort_dirty(canvas_item, canvas_item_owner);
- }
- void RendererCanvasCull::canvas_item_set_light_mask(RID p_item, int p_mask) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->light_mask = p_mask;
- }
- void RendererCanvasCull::canvas_item_set_transform(RID p_item, const Transform2D &p_transform) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (_interpolation_data.interpolation_enabled && canvas_item->interpolated) {
- if (!canvas_item->on_interpolate_transform_list) {
- _interpolation_data.canvas_item_transform_update_list_curr->push_back(p_item);
- canvas_item->on_interpolate_transform_list = true;
- } else {
- DEV_ASSERT(_interpolation_data.canvas_item_transform_update_list_curr->size() > 0);
- }
- }
- canvas_item->xform_curr = p_transform;
- }
- void RendererCanvasCull::canvas_item_set_visibility_layer(RID p_item, uint32_t p_visibility_layer) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->visibility_layer = p_visibility_layer;
- }
- uint32_t RendererCanvasCull::canvas_item_get_visibility_layer(RID p_item) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- if (!canvas_item)
- return 0;
- return canvas_item->visibility_layer;
- }
- void RendererCanvasCull::canvas_item_set_clip(RID p_item, bool p_clip) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->clip = p_clip;
- }
- void RendererCanvasCull::canvas_item_set_distance_field_mode(RID p_item, bool p_enable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->distance_field = p_enable;
- }
- void RendererCanvasCull::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect, const Rect2 &p_rect) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->custom_rect = p_custom_rect;
- canvas_item->rect = p_rect;
- }
- void RendererCanvasCull::canvas_item_set_modulate(RID p_item, const Color &p_color) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->modulate = p_color;
- }
- void RendererCanvasCull::canvas_item_set_self_modulate(RID p_item, const Color &p_color) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->self_modulate = p_color;
- }
- void RendererCanvasCull::canvas_item_set_draw_behind_parent(RID p_item, bool p_enable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->behind = p_enable;
- }
- void RendererCanvasCull::canvas_item_set_update_when_visible(RID p_item, bool p_update) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->update_when_visible = p_update;
- }
- void RendererCanvasCull::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) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandPrimitive *line = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(line);
- Vector2 diff = (p_from - p_to);
- Vector2 dir = diff.orthogonal().normalized();
- Vector2 t = dir * p_width * 0.5;
- Vector2 begin_left;
- Vector2 begin_right;
- Vector2 end_left;
- Vector2 end_right;
- if (p_width >= 0.0) {
- begin_left = p_from + t;
- begin_right = p_from - t;
- end_left = p_to + t;
- end_right = p_to - t;
- line->points[0] = begin_left;
- line->points[1] = begin_right;
- line->points[2] = end_right;
- line->points[3] = end_left;
- line->point_count = 4;
- } else {
- begin_left = p_from;
- begin_right = p_from;
- end_left = p_to;
- end_right = p_to;
- line->points[0] = p_from;
- line->points[1] = p_to;
- line->point_count = 2;
- }
- for (uint32_t i = 0; i < line->point_count; i++) {
- line->colors[i] = p_color;
- }
- if (p_antialiased) {
- float border_size = FEATHER_SIZE;
- if (0.0f <= p_width && p_width < 1.0f) {
- border_size *= p_width;
- }
- Vector2 dir2 = diff.normalized();
- Vector2 border = dir * border_size;
- Vector2 border2 = dir2 * border_size;
- Color transparent = Color(p_color, 0.0);
- {
- Item::CommandPrimitive *left_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(left_border);
- left_border->points[0] = begin_left;
- left_border->points[1] = begin_left + border;
- left_border->points[2] = end_left + border;
- left_border->points[3] = end_left;
- left_border->colors[0] = p_color;
- left_border->colors[1] = transparent;
- left_border->colors[2] = transparent;
- left_border->colors[3] = p_color;
- left_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *right_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(right_border);
- right_border->points[0] = begin_right;
- right_border->points[1] = begin_right - border;
- right_border->points[2] = end_right - border;
- right_border->points[3] = end_right;
- right_border->colors[0] = p_color;
- right_border->colors[1] = transparent;
- right_border->colors[2] = transparent;
- right_border->colors[3] = p_color;
- right_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_border);
- top_border->points[0] = begin_left;
- top_border->points[1] = begin_left + border2;
- top_border->points[2] = begin_right + border2;
- top_border->points[3] = begin_right;
- top_border->colors[0] = p_color;
- top_border->colors[1] = transparent;
- top_border->colors[2] = transparent;
- top_border->colors[3] = p_color;
- top_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_border);
- bottom_border->points[0] = end_left;
- bottom_border->points[1] = end_left - border2;
- bottom_border->points[2] = end_right - border2;
- bottom_border->points[3] = end_right;
- bottom_border->colors[0] = p_color;
- bottom_border->colors[1] = transparent;
- bottom_border->colors[2] = transparent;
- bottom_border->colors[3] = p_color;
- bottom_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_left_corner);
- top_left_corner->points[0] = begin_left;
- top_left_corner->points[1] = begin_left + border2;
- top_left_corner->points[2] = begin_left + border + border2;
- top_left_corner->points[3] = begin_left + border;
- top_left_corner->colors[0] = p_color;
- top_left_corner->colors[1] = transparent;
- top_left_corner->colors[2] = transparent;
- top_left_corner->colors[3] = transparent;
- top_left_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_right_corner);
- top_right_corner->points[0] = begin_right;
- top_right_corner->points[1] = begin_right + border2;
- top_right_corner->points[2] = begin_right - border + border2;
- top_right_corner->points[3] = begin_right - border;
- top_right_corner->colors[0] = p_color;
- top_right_corner->colors[1] = transparent;
- top_right_corner->colors[2] = transparent;
- top_right_corner->colors[3] = transparent;
- top_right_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_left_corner);
- bottom_left_corner->points[0] = end_left;
- bottom_left_corner->points[1] = end_left - border2;
- bottom_left_corner->points[2] = end_left + border - border2;
- bottom_left_corner->points[3] = end_left + border;
- bottom_left_corner->colors[0] = p_color;
- bottom_left_corner->colors[1] = transparent;
- bottom_left_corner->colors[2] = transparent;
- bottom_left_corner->colors[3] = transparent;
- bottom_left_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_right_corner);
- bottom_right_corner->points[0] = end_right;
- bottom_right_corner->points[1] = end_right - border2;
- bottom_right_corner->points[2] = end_right - border - border2;
- bottom_right_corner->points[3] = end_right - border;
- bottom_right_corner->colors[0] = p_color;
- bottom_right_corner->colors[1] = transparent;
- bottom_right_corner->colors[2] = transparent;
- bottom_right_corner->colors[3] = transparent;
- bottom_right_corner->point_count = 4;
- }
- }
- }
- static Vector2 compute_polyline_segment_dir(const Vector<Point2> &p_points, int p_index, const Vector2 &p_prev_segment_dir) {
- int point_count = p_points.size();
- bool is_last_point = (p_index == point_count - 1);
- Vector2 segment_dir;
- if (is_last_point) {
- segment_dir = p_prev_segment_dir;
- } else {
- segment_dir = (p_points[p_index + 1] - p_points[p_index]).normalized();
- if (segment_dir.is_zero_approx()) {
- segment_dir = p_prev_segment_dir;
- }
- }
- return segment_dir;
- }
- static Vector2 compute_polyline_edge_offset_clamped(const Vector2 &p_segment_dir, const Vector2 &p_prev_segment_dir) {
- Vector2 bisector;
- float length = 1.0f;
- bisector = (p_prev_segment_dir * p_segment_dir.length() - p_segment_dir * p_prev_segment_dir.length()).normalized();
- float angle = atan2f(bisector.cross(p_prev_segment_dir), bisector.dot(p_prev_segment_dir));
- float sin_angle = sinf(angle);
- if (!Math::is_zero_approx(sin_angle) && !p_segment_dir.is_equal_approx(p_prev_segment_dir)) {
- length = 1.0f / sin_angle;
- length = CLAMP(length, -3.0f, 3.0f);
- } else {
- bisector = p_segment_dir.orthogonal();
- }
- if (bisector.is_zero_approx()) {
- bisector = p_segment_dir.orthogonal();
- }
- return bisector * length;
- }
- void RendererCanvasCull::canvas_item_add_polyline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
- ERR_FAIL_COND(p_points.size() < 2);
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Color color = Color(1, 1, 1, 1);
- Vector<int> indices;
- int point_count = p_points.size();
- Item::CommandPolygon *pline = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(pline);
- if (p_width < 0) {
- if (p_antialiased) {
- WARN_PRINT("Antialiasing is not supported for thin polylines drawn using line strips (`p_width < 0`).");
- }
- pline->primitive = RS::PRIMITIVE_LINE_STRIP;
- if (p_colors.size() == 1 || p_colors.size() == point_count) {
- pline->polygon.create(indices, p_points, p_colors);
- } else {
- Vector<Color> colors;
- if (p_colors.is_empty()) {
- colors.push_back(color);
- } else {
- colors.resize(point_count);
- Color *colors_ptr = colors.ptrw();
- for (int i = 0; i < point_count; i++) {
- if (i < p_colors.size()) {
- color = p_colors[i];
- }
- colors_ptr[i] = color;
- }
- }
- pline->polygon.create(indices, p_points, colors);
- }
- return;
- }
- int polyline_point_count = point_count * 2;
- bool loop = p_points[0].is_equal_approx(p_points[point_count - 1]);
- Vector2 first_segment_dir;
- Vector2 last_segment_dir;
- // Search for first non-zero vector between two segments.
- for (int i = 1; i < point_count; i++) {
- first_segment_dir = (p_points[i] - p_points[i - 1]).normalized();
- if (!first_segment_dir.is_zero_approx()) {
- break;
- }
- }
- // Search for last non-zero vector between two segments.
- for (int i = point_count - 1; i >= 1; i--) {
- last_segment_dir = (p_points[i] - p_points[i - 1]).normalized();
- if (!last_segment_dir.is_zero_approx()) {
- break;
- }
- }
- PackedColorArray colors;
- PackedVector2Array points;
- // Additional 2+2 vertices to antialias begin+end of the middle triangle strip.
- colors.resize(polyline_point_count + ((p_antialiased && !loop) ? 4 : 0));
- points.resize(polyline_point_count + ((p_antialiased && !loop) ? 4 : 0));
- Vector2 *points_ptr = points.ptrw();
- Color *colors_ptr = colors.ptrw();
- if (p_antialiased) {
- float border_size = FEATHER_SIZE;
- if (p_width < 1.0f) {
- border_size *= p_width;
- }
- Color color2 = Color(1, 1, 1, 0);
- Item::CommandPolygon *pline_left = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(pline_left);
- Item::CommandPolygon *pline_right = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(pline_right);
- PackedColorArray colors_left;
- PackedVector2Array points_left;
- PackedColorArray colors_right;
- PackedVector2Array points_right;
- // 2+2 additional vertices for begin+end corners.
- // 1 additional vertex to swap the orientation of the triangles within the end corner's quad.
- colors_left.resize(polyline_point_count + (loop ? 0 : 5));
- points_left.resize(polyline_point_count + (loop ? 0 : 5));
- colors_right.resize(polyline_point_count + (loop ? 0 : 5));
- points_right.resize(polyline_point_count + (loop ? 0 : 5));
- Color *colors_left_ptr = colors_left.ptrw();
- Vector2 *points_left_ptr = points_left.ptrw();
- Vector2 *points_right_ptr = points_right.ptrw();
- Color *colors_right_ptr = colors_right.ptrw();
- Vector2 prev_segment_dir;
- for (int i = 0; i < point_count; i++) {
- bool is_first_point = (i == 0);
- bool is_last_point = (i == point_count - 1);
- Vector2 segment_dir = compute_polyline_segment_dir(p_points, i, prev_segment_dir);
- if (is_first_point && loop) {
- prev_segment_dir = last_segment_dir;
- } else if (is_last_point && loop) {
- prev_segment_dir = first_segment_dir;
- }
- Vector2 base_edge_offset;
- if (is_first_point && !loop) {
- base_edge_offset = first_segment_dir.orthogonal();
- } else if (is_last_point && !loop) {
- base_edge_offset = last_segment_dir.orthogonal();
- } else {
- base_edge_offset = compute_polyline_edge_offset_clamped(segment_dir, prev_segment_dir);
- }
- Vector2 edge_offset = base_edge_offset * (p_width * 0.5f);
- Vector2 border = base_edge_offset * border_size;
- Vector2 pos = p_points[i];
- int j = i * 2 + (loop ? 0 : 2);
- points_ptr[j + 0] = pos + edge_offset;
- points_ptr[j + 1] = pos - edge_offset;
- points_left_ptr[j + 0] = pos + edge_offset;
- points_left_ptr[j + 1] = pos + edge_offset + border;
- points_right_ptr[j + 0] = pos - edge_offset;
- points_right_ptr[j + 1] = pos - edge_offset - border;
- if (i < p_colors.size()) {
- color = p_colors[i];
- color2 = Color(color.r, color.g, color.b, 0);
- }
- colors_ptr[j + 0] = color;
- colors_ptr[j + 1] = color;
- colors_left_ptr[j + 0] = color;
- colors_left_ptr[j + 1] = color2;
- colors_right_ptr[j + 0] = color;
- colors_right_ptr[j + 1] = color2;
- if (is_first_point && !loop) {
- Vector2 begin_border = -segment_dir * border_size;
- points_ptr[0] = pos + edge_offset + begin_border;
- points_ptr[1] = pos - edge_offset + begin_border;
- colors_ptr[0] = color2;
- colors_ptr[1] = color2;
- points_left_ptr[0] = pos + edge_offset + begin_border;
- points_left_ptr[1] = pos + edge_offset + begin_border + border;
- colors_left_ptr[0] = color2;
- colors_left_ptr[1] = color2;
- points_right_ptr[0] = pos - edge_offset + begin_border;
- points_right_ptr[1] = pos - edge_offset + begin_border - border;
- colors_right_ptr[0] = color2;
- colors_right_ptr[1] = color2;
- }
- if (is_last_point && !loop) {
- Vector2 end_border = prev_segment_dir * border_size;
- int end_index = polyline_point_count + 2;
- points_ptr[end_index + 0] = pos + edge_offset + end_border;
- points_ptr[end_index + 1] = pos - edge_offset + end_border;
- colors_ptr[end_index + 0] = color2;
- colors_ptr[end_index + 1] = color2;
- // Swap orientation of the triangles within both end corner quads so the visual seams
- // between triangles goes from the edge corner. Done by going back to the edge corner
- // (1 additional vertex / zero-area triangle per left/right corner).
- points_left_ptr[end_index + 0] = pos + edge_offset;
- points_left_ptr[end_index + 1] = pos + edge_offset + end_border + border;
- points_left_ptr[end_index + 2] = pos + edge_offset + end_border;
- colors_left_ptr[end_index + 0] = color;
- colors_left_ptr[end_index + 1] = color2;
- colors_left_ptr[end_index + 2] = color2;
- points_right_ptr[end_index + 0] = pos - edge_offset;
- points_right_ptr[end_index + 1] = pos - edge_offset + end_border - border;
- points_right_ptr[end_index + 2] = pos - edge_offset + end_border;
- colors_right_ptr[end_index + 0] = color;
- colors_right_ptr[end_index + 1] = color2;
- colors_right_ptr[end_index + 2] = color2;
- }
- prev_segment_dir = segment_dir;
- }
- pline_left->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
- pline_left->polygon.create(indices, points_left, colors_left);
- pline_right->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
- pline_right->polygon.create(indices, points_right, colors_right);
- } else {
- // Makes a single triangle strip for drawing the line.
- Vector2 prev_segment_dir;
- for (int i = 0; i < point_count; i++) {
- bool is_first_point = (i == 0);
- bool is_last_point = (i == point_count - 1);
- Vector2 segment_dir = compute_polyline_segment_dir(p_points, i, prev_segment_dir);
- if (is_first_point && loop) {
- prev_segment_dir = last_segment_dir;
- } else if (is_last_point && loop) {
- prev_segment_dir = first_segment_dir;
- }
- Vector2 base_edge_offset;
- if (is_first_point && !loop) {
- base_edge_offset = first_segment_dir.orthogonal();
- } else if (is_last_point && !loop) {
- base_edge_offset = last_segment_dir.orthogonal();
- } else {
- base_edge_offset = compute_polyline_edge_offset_clamped(segment_dir, prev_segment_dir);
- }
- Vector2 edge_offset = base_edge_offset * (p_width * 0.5f);
- Vector2 pos = p_points[i];
- points_ptr[i * 2 + 0] = pos + edge_offset;
- points_ptr[i * 2 + 1] = pos - edge_offset;
- if (i < p_colors.size()) {
- color = p_colors[i];
- }
- colors_ptr[i * 2 + 0] = color;
- colors_ptr[i * 2 + 1] = color;
- prev_segment_dir = segment_dir;
- }
- }
- pline->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
- pline->polygon.create(indices, points, colors);
- }
- void RendererCanvasCull::canvas_item_add_multiline(RID p_item, const Vector<Point2> &p_points, const Vector<Color> &p_colors, float p_width, bool p_antialiased) {
- ERR_FAIL_COND(p_points.is_empty() || p_points.size() % 2 != 0);
- ERR_FAIL_COND(p_colors.size() != 1 && p_colors.size() * 2 != p_points.size());
- // TODO: `canvas_item_add_line`(`multiline`, `polyline`) share logic, should factor out.
- if (p_width < 0) {
- if (p_antialiased) {
- WARN_PRINT("Antialiasing is not supported for thin multilines drawn using line strips (`p_width < 0`).");
- }
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Vector<Color> colors;
- if (p_colors.size() == 1) {
- colors = p_colors;
- } else { //} else if (p_colors.size() << 1 == p_points.size()) {
- colors.resize(p_points.size());
- Color *colors_ptr = colors.ptrw();
- for (int i = 0; i < p_colors.size(); i++) {
- Color color = p_colors[i];
- colors_ptr[i * 2 + 0] = color;
- colors_ptr[i * 2 + 1] = color;
- }
- }
- Item::CommandPolygon *pline = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(pline);
- pline->primitive = RS::PRIMITIVE_LINES;
- pline->polygon.create(Vector<int>(), p_points, colors);
- } else {
- if (p_colors.size() == 1) {
- Color color = p_colors[0];
- for (int i = 0; i < p_points.size() >> 1; i++) {
- Vector2 from = p_points[i * 2 + 0];
- Vector2 to = p_points[i * 2 + 1];
- canvas_item_add_line(p_item, from, to, color, p_width, p_antialiased);
- }
- } else { //} else if (p_colors.size() << 1 == p_points.size()) {
- for (int i = 0; i < p_colors.size(); i++) {
- Color color = p_colors[i];
- Vector2 from = p_points[i * 2 + 0];
- Vector2 to = p_points[i * 2 + 1];
- canvas_item_add_line(p_item, from, to, color, p_width, p_antialiased);
- }
- }
- }
- }
- void RendererCanvasCull::canvas_item_add_rect(RID p_item, const Rect2 &p_rect, const Color &p_color, bool p_antialiased) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
- ERR_FAIL_NULL(rect);
- rect->modulate = p_color;
- rect->rect = p_rect;
- // Add feathers.
- if (p_antialiased) {
- float border_size = FEATHER_SIZE;
- const real_t size = MIN(p_rect.size.width, p_rect.size.height);
- if (0.0f <= size && size < 1.0f) {
- border_size *= size;
- }
- const Vector2 vec_down = Vector2(0.0f, p_rect.size.height);
- const Vector2 vec_right = Vector2(p_rect.size.width, 0.0f);
- const Vector2 begin_left = p_rect.position;
- const Vector2 begin_right = p_rect.position + vec_down;
- const Vector2 end_left = p_rect.position + vec_right;
- const Vector2 end_right = p_rect.position + p_rect.size;
- const Vector2 dir = Vector2(0.0f, -1.0f);
- const Vector2 dir2 = Vector2(-1.0f, 0.0f);
- const Vector2 border = dir * border_size;
- const Vector2 border2 = dir2 * border_size;
- Color transparent = Color(p_color, 0.0);
- {
- Item::CommandPrimitive *left_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(left_border);
- left_border->points[0] = begin_left;
- left_border->points[1] = begin_left + border;
- left_border->points[2] = end_left + border;
- left_border->points[3] = end_left;
- left_border->colors[0] = p_color;
- left_border->colors[1] = transparent;
- left_border->colors[2] = transparent;
- left_border->colors[3] = p_color;
- left_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *right_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(right_border);
- right_border->points[0] = begin_right;
- right_border->points[1] = begin_right - border;
- right_border->points[2] = end_right - border;
- right_border->points[3] = end_right;
- right_border->colors[0] = p_color;
- right_border->colors[1] = transparent;
- right_border->colors[2] = transparent;
- right_border->colors[3] = p_color;
- right_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_border);
- top_border->points[0] = begin_left;
- top_border->points[1] = begin_left + border2;
- top_border->points[2] = begin_right + border2;
- top_border->points[3] = begin_right;
- top_border->colors[0] = p_color;
- top_border->colors[1] = transparent;
- top_border->colors[2] = transparent;
- top_border->colors[3] = p_color;
- top_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_border = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_border);
- bottom_border->points[0] = end_left;
- bottom_border->points[1] = end_left - border2;
- bottom_border->points[2] = end_right - border2;
- bottom_border->points[3] = end_right;
- bottom_border->colors[0] = p_color;
- bottom_border->colors[1] = transparent;
- bottom_border->colors[2] = transparent;
- bottom_border->colors[3] = p_color;
- bottom_border->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_left_corner);
- top_left_corner->points[0] = begin_left;
- top_left_corner->points[1] = begin_left + border2;
- top_left_corner->points[2] = begin_left + border + border2;
- top_left_corner->points[3] = begin_left + border;
- top_left_corner->colors[0] = p_color;
- top_left_corner->colors[1] = transparent;
- top_left_corner->colors[2] = transparent;
- top_left_corner->colors[3] = transparent;
- top_left_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *top_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(top_right_corner);
- top_right_corner->points[0] = begin_right;
- top_right_corner->points[1] = begin_right + border2;
- top_right_corner->points[2] = begin_right - border + border2;
- top_right_corner->points[3] = begin_right - border;
- top_right_corner->colors[0] = p_color;
- top_right_corner->colors[1] = transparent;
- top_right_corner->colors[2] = transparent;
- top_right_corner->colors[3] = transparent;
- top_right_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_left_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_left_corner);
- bottom_left_corner->points[0] = end_left;
- bottom_left_corner->points[1] = end_left - border2;
- bottom_left_corner->points[2] = end_left + border - border2;
- bottom_left_corner->points[3] = end_left + border;
- bottom_left_corner->colors[0] = p_color;
- bottom_left_corner->colors[1] = transparent;
- bottom_left_corner->colors[2] = transparent;
- bottom_left_corner->colors[3] = transparent;
- bottom_left_corner->point_count = 4;
- }
- {
- Item::CommandPrimitive *bottom_right_corner = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(bottom_right_corner);
- bottom_right_corner->points[0] = end_right;
- bottom_right_corner->points[1] = end_right - border2;
- bottom_right_corner->points[2] = end_right - border - border2;
- bottom_right_corner->points[3] = end_right - border;
- bottom_right_corner->colors[0] = p_color;
- bottom_right_corner->colors[1] = transparent;
- bottom_right_corner->colors[2] = transparent;
- bottom_right_corner->colors[3] = transparent;
- bottom_right_corner->point_count = 4;
- }
- }
- }
- void RendererCanvasCull::canvas_item_add_circle(RID p_item, const Point2 &p_pos, float p_radius, const Color &p_color, bool p_antialiased) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- static const int circle_segments = 64;
- {
- Item::CommandPolygon *circle = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(circle);
- circle->primitive = RS::PRIMITIVE_TRIANGLES;
- Vector<int> indices;
- Vector<Vector2> points;
- points.resize(circle_segments + 2);
- Vector2 *points_ptr = points.ptrw();
- // Store circle center in the last point.
- points_ptr[circle_segments + 1] = p_pos;
- const real_t circle_point_step = Math_TAU / circle_segments;
- for (int i = 0; i < circle_segments + 1; i++) {
- float angle = i * circle_point_step;
- points_ptr[i].x = Math::cos(angle) * p_radius;
- points_ptr[i].y = Math::sin(angle) * p_radius;
- points_ptr[i] += p_pos;
- }
- indices.resize(circle_segments * 3);
- int *indices_ptr = indices.ptrw();
- for (int i = 0; i < circle_segments; i++) {
- indices_ptr[i * 3 + 0] = circle_segments + 1;
- indices_ptr[i * 3 + 1] = i;
- indices_ptr[i * 3 + 2] = i + 1;
- }
- Vector<Color> color;
- color.push_back(p_color);
- circle->polygon.create(indices, points, color);
- }
- if (p_antialiased) {
- float border_size = FEATHER_SIZE;
- const float diameter = p_radius * 2.0f;
- if (0.0f <= diameter && diameter < 1.0f) {
- border_size *= p_radius;
- }
- Item::CommandPolygon *feather = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(feather);
- feather->primitive = RS::PRIMITIVE_TRIANGLE_STRIP;
- Color transparent = Color(p_color, 0.0);
- Vector<int> indices;
- Vector<Color> colors;
- Vector<Vector2> points;
- points.resize(2 * circle_segments + 2);
- colors.resize(2 * circle_segments + 2);
- const real_t circle_point_step = Math_TAU / circle_segments;
- Vector2 *points_ptr = points.ptrw();
- Color *colors_ptr = colors.ptrw();
- for (int i = 0; i < circle_segments + 1; i++) {
- const float angle = i * circle_point_step;
- const float c = Math::cos(angle);
- const float s = Math::sin(angle);
- points_ptr[i * 2].x = c * p_radius;
- points_ptr[i * 2].y = s * p_radius;
- points_ptr[i * 2] += p_pos;
- points_ptr[i * 2 + 1].x = c * (p_radius + border_size);
- points_ptr[i * 2 + 1].y = s * (p_radius + border_size);
- points_ptr[i * 2 + 1] += p_pos;
- colors_ptr[i * 2] = p_color;
- colors_ptr[i * 2 + 1] = transparent;
- }
- feather->polygon.create(indices, points, colors);
- }
- }
- void RendererCanvasCull::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) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
- ERR_FAIL_NULL(rect);
- rect->modulate = p_modulate;
- rect->rect = p_rect;
- rect->flags = 0;
- if (p_tile) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_TILE;
- rect->flags |= RendererCanvasRender::CANVAS_RECT_REGION;
- rect->source = Rect2(0, 0, ABS(p_rect.size.width), ABS(p_rect.size.height));
- }
- if (p_rect.size.x < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->rect.size.x = -rect->rect.size.x;
- }
- if (p_rect.size.y < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->rect.size.y = -rect->rect.size.y;
- }
- if (p_transpose) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_TRANSPOSE;
- SWAP(rect->rect.size.x, rect->rect.size.y);
- }
- rect->texture = p_texture;
- }
- void RendererCanvasCull::canvas_item_add_msdf_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate, int p_outline_size, float p_px_range, float p_scale) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
- ERR_FAIL_NULL(rect);
- rect->modulate = p_modulate;
- rect->rect = p_rect;
- rect->texture = p_texture;
- rect->source = p_src_rect;
- rect->flags = RendererCanvasRender::CANVAS_RECT_REGION | RendererCanvasRender::CANVAS_RECT_MSDF;
- if (p_rect.size.x < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->rect.size.x = -rect->rect.size.x;
- }
- if (p_src_rect.size.x < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->source.size.x = -rect->source.size.x;
- }
- if (p_rect.size.y < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->rect.size.y = -rect->rect.size.y;
- }
- if (p_src_rect.size.y < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->source.size.y = -rect->source.size.y;
- }
- rect->outline = (float)p_outline_size / p_scale / 4.0;
- rect->px_range = p_px_range;
- }
- void RendererCanvasCull::canvas_item_add_lcd_texture_rect_region(RID p_item, const Rect2 &p_rect, RID p_texture, const Rect2 &p_src_rect, const Color &p_modulate) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
- ERR_FAIL_NULL(rect);
- rect->modulate = p_modulate;
- rect->rect = p_rect;
- rect->texture = p_texture;
- rect->source = p_src_rect;
- rect->flags = RendererCanvasRender::CANVAS_RECT_REGION | RendererCanvasRender::CANVAS_RECT_LCD;
- if (p_rect.size.x < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->rect.size.x = -rect->rect.size.x;
- }
- if (p_src_rect.size.x < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->source.size.x = -rect->source.size.x;
- }
- if (p_rect.size.y < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->rect.size.y = -rect->rect.size.y;
- }
- if (p_src_rect.size.y < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->source.size.y = -rect->source.size.y;
- }
- }
- void RendererCanvasCull::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, bool p_clip_uv) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandRect *rect = canvas_item->alloc_command<Item::CommandRect>();
- ERR_FAIL_NULL(rect);
- rect->modulate = p_modulate;
- rect->rect = p_rect;
- rect->texture = p_texture;
- rect->source = p_src_rect;
- rect->flags = RendererCanvasRender::CANVAS_RECT_REGION;
- if (p_rect.size.x < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->rect.size.x = -rect->rect.size.x;
- }
- if (p_src_rect.size.x < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_H;
- rect->source.size.x = -rect->source.size.x;
- }
- if (p_rect.size.y < 0) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->rect.size.y = -rect->rect.size.y;
- }
- if (p_src_rect.size.y < 0) {
- rect->flags ^= RendererCanvasRender::CANVAS_RECT_FLIP_V;
- rect->source.size.y = -rect->source.size.y;
- }
- if (p_transpose) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_TRANSPOSE;
- SWAP(rect->rect.size.x, rect->rect.size.y);
- }
- if (p_clip_uv) {
- rect->flags |= RendererCanvasRender::CANVAS_RECT_CLIP_UV;
- }
- }
- void RendererCanvasCull::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, RS::NinePatchAxisMode p_x_axis_mode, RS::NinePatchAxisMode p_y_axis_mode, bool p_draw_center, const Color &p_modulate) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandNinePatch *style = canvas_item->alloc_command<Item::CommandNinePatch>();
- ERR_FAIL_NULL(style);
- style->texture = p_texture;
- style->rect = p_rect;
- style->source = p_source;
- style->draw_center = p_draw_center;
- style->color = p_modulate;
- style->margin[SIDE_LEFT] = p_topleft.x;
- style->margin[SIDE_TOP] = p_topleft.y;
- style->margin[SIDE_RIGHT] = p_bottomright.x;
- style->margin[SIDE_BOTTOM] = p_bottomright.y;
- style->axis_x = p_x_axis_mode;
- style->axis_y = p_y_axis_mode;
- }
- void RendererCanvasCull::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) {
- uint32_t pc = p_points.size();
- ERR_FAIL_COND(pc == 0 || pc > 4);
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandPrimitive *prim = canvas_item->alloc_command<Item::CommandPrimitive>();
- ERR_FAIL_NULL(prim);
- for (int i = 0; i < p_points.size(); i++) {
- prim->points[i] = p_points[i];
- if (i < p_uvs.size()) {
- prim->uvs[i] = p_uvs[i];
- }
- if (i < p_colors.size()) {
- prim->colors[i] = p_colors[i];
- } else if (p_colors.size()) {
- prim->colors[i] = p_colors[0];
- } else {
- prim->colors[i] = Color(1, 1, 1, 1);
- }
- }
- prim->point_count = p_points.size();
- prim->texture = p_texture;
- }
- void RendererCanvasCull::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) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- #ifdef DEBUG_ENABLED
- int pointcount = p_points.size();
- ERR_FAIL_COND(pointcount < 3);
- int color_size = p_colors.size();
- int uv_size = p_uvs.size();
- ERR_FAIL_COND(color_size != 0 && color_size != 1 && color_size != pointcount);
- ERR_FAIL_COND(uv_size != 0 && (uv_size != pointcount));
- #endif
- Vector<int> indices = Geometry2D::triangulate_polygon(p_points);
- ERR_FAIL_COND_MSG(indices.is_empty(), "Invalid polygon data, triangulation failed.");
- Item::CommandPolygon *polygon = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(polygon);
- polygon->primitive = RS::PRIMITIVE_TRIANGLES;
- polygon->texture = p_texture;
- polygon->polygon.create(indices, p_points, p_colors, p_uvs);
- }
- void RendererCanvasCull::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) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- int vertex_count = p_points.size();
- ERR_FAIL_COND(vertex_count == 0);
- ERR_FAIL_COND(!p_colors.is_empty() && p_colors.size() != vertex_count && p_colors.size() != 1);
- ERR_FAIL_COND(!p_uvs.is_empty() && p_uvs.size() != vertex_count);
- ERR_FAIL_COND(!p_bones.is_empty() && p_bones.size() != vertex_count * 4);
- ERR_FAIL_COND(!p_weights.is_empty() && p_weights.size() != vertex_count * 4);
- Item::CommandPolygon *polygon = canvas_item->alloc_command<Item::CommandPolygon>();
- ERR_FAIL_NULL(polygon);
- polygon->texture = p_texture;
- polygon->polygon.create(p_indices, p_points, p_colors, p_uvs, p_bones, p_weights);
- polygon->primitive = RS::PRIMITIVE_TRIANGLES;
- }
- void RendererCanvasCull::canvas_item_add_set_transform(RID p_item, const Transform2D &p_transform) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandTransform *tr = canvas_item->alloc_command<Item::CommandTransform>();
- ERR_FAIL_NULL(tr);
- tr->xform = p_transform;
- }
- void RendererCanvasCull::canvas_item_add_mesh(RID p_item, const RID &p_mesh, const Transform2D &p_transform, const Color &p_modulate, RID p_texture) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- ERR_FAIL_COND(!p_mesh.is_valid());
- Item::CommandMesh *m = canvas_item->alloc_command<Item::CommandMesh>();
- ERR_FAIL_NULL(m);
- m->mesh = p_mesh;
- if (canvas_item->skeleton.is_valid()) {
- m->mesh_instance = RSG::mesh_storage->mesh_instance_create(p_mesh);
- RSG::mesh_storage->mesh_instance_set_skeleton(m->mesh_instance, canvas_item->skeleton);
- }
- m->texture = p_texture;
- m->transform = p_transform;
- m->modulate = p_modulate;
- }
- void RendererCanvasCull::canvas_item_add_particles(RID p_item, RID p_particles, RID p_texture) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandParticles *part = canvas_item->alloc_command<Item::CommandParticles>();
- ERR_FAIL_NULL(part);
- part->particles = p_particles;
- part->texture = p_texture;
- //take the chance and request processing for them, at least once until they become visible again
- RSG::particles_storage->particles_request_process(p_particles);
- }
- void RendererCanvasCull::canvas_item_add_multimesh(RID p_item, RID p_mesh, RID p_texture) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandMultiMesh *mm = canvas_item->alloc_command<Item::CommandMultiMesh>();
- ERR_FAIL_NULL(mm);
- mm->multimesh = p_mesh;
- mm->texture = p_texture;
- }
- void RendererCanvasCull::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandClipIgnore *ci = canvas_item->alloc_command<Item::CommandClipIgnore>();
- ERR_FAIL_NULL(ci);
- ci->ignore = p_ignore;
- }
- void RendererCanvasCull::canvas_item_add_animation_slice(RID p_item, double p_animation_length, double p_slice_begin, double p_slice_end, double p_offset) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- Item::CommandAnimationSlice *as = canvas_item->alloc_command<Item::CommandAnimationSlice>();
- ERR_FAIL_NULL(as);
- as->animation_length = p_animation_length;
- as->slice_begin = p_slice_begin;
- as->slice_end = p_slice_end;
- as->offset = p_offset;
- }
- void RendererCanvasCull::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->sort_y = p_enable;
- _mark_ysort_dirty(canvas_item, canvas_item_owner);
- }
- void RendererCanvasCull::canvas_item_set_z_index(RID p_item, int p_z) {
- ERR_FAIL_COND(p_z < RS::CANVAS_ITEM_Z_MIN || p_z > RS::CANVAS_ITEM_Z_MAX);
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->z_index = p_z;
- }
- void RendererCanvasCull::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->z_relative = p_enable;
- }
- void RendererCanvasCull::canvas_item_attach_skeleton(RID p_item, RID p_skeleton) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (canvas_item->skeleton == p_skeleton) {
- return;
- }
- canvas_item->skeleton = p_skeleton;
- Item::Command *c = canvas_item->commands;
- while (c) {
- if (c->type == Item::Command::TYPE_MESH) {
- Item::CommandMesh *cm = static_cast<Item::CommandMesh *>(c);
- if (canvas_item->skeleton.is_valid()) {
- if (cm->mesh_instance.is_null()) {
- cm->mesh_instance = RSG::mesh_storage->mesh_instance_create(cm->mesh);
- }
- RSG::mesh_storage->mesh_instance_set_skeleton(cm->mesh_instance, canvas_item->skeleton);
- } else {
- if (cm->mesh_instance.is_valid()) {
- RSG::mesh_storage->mesh_instance_free(cm->mesh_instance);
- cm->mesh_instance = RID();
- }
- }
- }
- c = c->next;
- }
- }
- void RendererCanvasCull::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2 &p_rect) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (p_enable && (canvas_item->copy_back_buffer == nullptr)) {
- canvas_item->copy_back_buffer = memnew(RendererCanvasRender::Item::CopyBackBuffer);
- }
- if (!p_enable && (canvas_item->copy_back_buffer != nullptr)) {
- memdelete(canvas_item->copy_back_buffer);
- canvas_item->copy_back_buffer = nullptr;
- }
- if (p_enable) {
- canvas_item->copy_back_buffer->rect = p_rect;
- canvas_item->copy_back_buffer->full = p_rect == Rect2();
- }
- }
- void RendererCanvasCull::canvas_item_clear(RID p_item) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->clear();
- #ifdef DEBUG_ENABLED
- if (debug_redraw) {
- canvas_item->debug_redraw_time = debug_redraw_time;
- }
- #endif
- }
- void RendererCanvasCull::canvas_item_set_draw_index(RID p_item, int p_index) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->index = p_index;
- if (canvas_item_owner.owns(canvas_item->parent)) {
- Item *canvas_item_parent = canvas_item_owner.get_or_null(canvas_item->parent);
- canvas_item_parent->children_order_dirty = true;
- return;
- }
- Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
- if (canvas) {
- canvas->children_order_dirty = true;
- return;
- }
- }
- void RendererCanvasCull::canvas_item_set_material(RID p_item, RID p_material) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->material = p_material;
- }
- void RendererCanvasCull::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->use_parent_material = p_enable;
- }
- void RendererCanvasCull::canvas_item_set_visibility_notifier(RID p_item, bool p_enable, const Rect2 &p_area, const Callable &p_enter_callable, const Callable &p_exit_callable) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (p_enable) {
- if (!canvas_item->visibility_notifier) {
- canvas_item->visibility_notifier = visibility_notifier_allocator.alloc();
- }
- canvas_item->visibility_notifier->area = p_area;
- canvas_item->visibility_notifier->enter_callable = p_enter_callable;
- canvas_item->visibility_notifier->exit_callable = p_exit_callable;
- } else {
- if (canvas_item->visibility_notifier) {
- visibility_notifier_allocator.free(canvas_item->visibility_notifier);
- canvas_item->visibility_notifier = nullptr;
- }
- }
- }
- void RendererCanvasCull::canvas_item_set_debug_redraw(bool p_enabled) {
- debug_redraw = p_enabled;
- RSG::canvas_render->set_debug_redraw(p_enabled, debug_redraw_time, debug_redraw_color);
- }
- bool RendererCanvasCull::canvas_item_get_debug_redraw() const {
- return debug_redraw;
- }
- void RendererCanvasCull::canvas_item_set_interpolated(RID p_item, bool p_interpolated) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->interpolated = p_interpolated;
- }
- void RendererCanvasCull::canvas_item_reset_physics_interpolation(RID p_item) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->xform_prev = canvas_item->xform_curr;
- }
- // Useful especially for origin shifting.
- void RendererCanvasCull::canvas_item_transform_physics_interpolation(RID p_item, const Transform2D &p_transform) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- canvas_item->xform_prev = p_transform * canvas_item->xform_prev;
- canvas_item->xform_curr = p_transform * canvas_item->xform_curr;
- }
- void RendererCanvasCull::canvas_item_set_canvas_group_mode(RID p_item, RS::CanvasGroupMode p_mode, float p_clear_margin, bool p_fit_empty, float p_fit_margin, bool p_blur_mipmaps) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(canvas_item);
- if (p_mode == RS::CANVAS_GROUP_MODE_DISABLED) {
- if (canvas_item->canvas_group != nullptr) {
- memdelete(canvas_item->canvas_group);
- canvas_item->canvas_group = nullptr;
- }
- } else {
- if (canvas_item->canvas_group == nullptr) {
- canvas_item->canvas_group = memnew(RendererCanvasRender::Item::CanvasGroup);
- }
- canvas_item->canvas_group->mode = p_mode;
- canvas_item->canvas_group->fit_empty = p_fit_empty;
- canvas_item->canvas_group->fit_margin = p_fit_margin;
- canvas_item->canvas_group->blur_mipmaps = p_blur_mipmaps;
- canvas_item->canvas_group->clear_margin = p_clear_margin;
- }
- }
- RID RendererCanvasCull::canvas_light_allocate() {
- return canvas_light_owner.allocate_rid();
- }
- void RendererCanvasCull::canvas_light_initialize(RID p_rid) {
- canvas_light_owner.initialize_rid(p_rid);
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_rid);
- clight->light_internal = RSG::canvas_render->light_create();
- }
- void RendererCanvasCull::canvas_light_set_mode(RID p_light, RS::CanvasLightMode p_mode) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- if (clight->mode == p_mode) {
- return;
- }
- RID canvas = clight->canvas;
- if (canvas.is_valid()) {
- canvas_light_attach_to_canvas(p_light, RID());
- }
- clight->mode = p_mode;
- if (canvas.is_valid()) {
- canvas_light_attach_to_canvas(p_light, canvas);
- }
- }
- void RendererCanvasCull::canvas_light_attach_to_canvas(RID p_light, RID p_canvas) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- if (clight->canvas.is_valid()) {
- Canvas *canvas = canvas_owner.get_or_null(clight->canvas);
- if (clight->mode == RS::CANVAS_LIGHT_MODE_POINT) {
- canvas->lights.erase(clight);
- } else {
- canvas->directional_lights.erase(clight);
- }
- }
- if (!canvas_owner.owns(p_canvas)) {
- p_canvas = RID();
- }
- clight->canvas = p_canvas;
- if (clight->canvas.is_valid()) {
- Canvas *canvas = canvas_owner.get_or_null(clight->canvas);
- if (clight->mode == RS::CANVAS_LIGHT_MODE_POINT) {
- canvas->lights.insert(clight);
- } else {
- canvas->directional_lights.insert(clight);
- }
- }
- }
- void RendererCanvasCull::canvas_light_set_enabled(RID p_light, bool p_enabled) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->enabled = p_enabled;
- }
- void RendererCanvasCull::canvas_light_set_texture_scale(RID p_light, float p_scale) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->scale = p_scale;
- }
- void RendererCanvasCull::canvas_light_set_transform(RID p_light, const Transform2D &p_transform) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- if (_interpolation_data.interpolation_enabled && clight->interpolated) {
- if (!clight->on_interpolate_transform_list) {
- _interpolation_data.canvas_light_transform_update_list_curr->push_back(p_light);
- clight->on_interpolate_transform_list = true;
- } else {
- DEV_ASSERT(_interpolation_data.canvas_light_transform_update_list_curr->size() > 0);
- }
- }
- clight->xform_curr = p_transform;
- }
- void RendererCanvasCull::canvas_light_set_texture(RID p_light, RID p_texture) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- if (clight->texture == p_texture) {
- return;
- }
- clight->texture = p_texture;
- clight->version++;
- RSG::canvas_render->light_set_texture(clight->light_internal, p_texture);
- }
- void RendererCanvasCull::canvas_light_set_texture_offset(RID p_light, const Vector2 &p_offset) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->texture_offset = p_offset;
- }
- void RendererCanvasCull::canvas_light_set_color(RID p_light, const Color &p_color) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->color = p_color;
- }
- void RendererCanvasCull::canvas_light_set_height(RID p_light, float p_height) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->height = p_height;
- }
- void RendererCanvasCull::canvas_light_set_energy(RID p_light, float p_energy) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->energy = p_energy;
- }
- void RendererCanvasCull::canvas_light_set_z_range(RID p_light, int p_min_z, int p_max_z) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->z_min = p_min_z;
- clight->z_max = p_max_z;
- }
- void RendererCanvasCull::canvas_light_set_layer_range(RID p_light, int p_min_layer, int p_max_layer) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->layer_max = p_max_layer;
- clight->layer_min = p_min_layer;
- }
- void RendererCanvasCull::canvas_light_set_item_cull_mask(RID p_light, int p_mask) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->item_mask = p_mask;
- }
- void RendererCanvasCull::canvas_light_set_item_shadow_cull_mask(RID p_light, int p_mask) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->item_shadow_mask = p_mask;
- }
- void RendererCanvasCull::canvas_light_set_directional_distance(RID p_light, float p_distance) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->directional_distance = p_distance;
- }
- void RendererCanvasCull::canvas_light_set_blend_mode(RID p_light, RS::CanvasLightBlendMode p_mode) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->blend_mode = p_mode;
- }
- void RendererCanvasCull::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- if (clight->use_shadow == p_enabled) {
- return;
- }
- clight->use_shadow = p_enabled;
- clight->version++;
- RSG::canvas_render->light_set_use_shadow(clight->light_internal, clight->use_shadow);
- }
- void RendererCanvasCull::canvas_light_set_shadow_filter(RID p_light, RS::CanvasLightShadowFilter p_filter) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->shadow_filter = p_filter;
- }
- void RendererCanvasCull::canvas_light_set_shadow_color(RID p_light, const Color &p_color) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->shadow_color = p_color;
- }
- void RendererCanvasCull::canvas_light_set_shadow_smooth(RID p_light, float p_smooth) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->shadow_smooth = p_smooth;
- }
- void RendererCanvasCull::canvas_light_set_interpolated(RID p_light, bool p_interpolated) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->interpolated = p_interpolated;
- }
- void RendererCanvasCull::canvas_light_reset_physics_interpolation(RID p_light) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->xform_prev = clight->xform_curr;
- }
- void RendererCanvasCull::canvas_light_transform_physics_interpolation(RID p_light, const Transform2D &p_transform) {
- RendererCanvasRender::Light *clight = canvas_light_owner.get_or_null(p_light);
- ERR_FAIL_NULL(clight);
- clight->xform_prev = p_transform * clight->xform_prev;
- clight->xform_curr = p_transform * clight->xform_curr;
- }
- RID RendererCanvasCull::canvas_light_occluder_allocate() {
- return canvas_light_occluder_owner.allocate_rid();
- }
- void RendererCanvasCull::canvas_light_occluder_initialize(RID p_rid) {
- return canvas_light_occluder_owner.initialize_rid(p_rid);
- }
- void RendererCanvasCull::canvas_light_occluder_attach_to_canvas(RID p_occluder, RID p_canvas) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- if (occluder->canvas.is_valid()) {
- Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
- canvas->occluders.erase(occluder);
- }
- if (!canvas_owner.owns(p_canvas)) {
- p_canvas = RID();
- }
- occluder->canvas = p_canvas;
- if (occluder->canvas.is_valid()) {
- Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
- canvas->occluders.insert(occluder);
- }
- }
- void RendererCanvasCull::canvas_light_occluder_set_enabled(RID p_occluder, bool p_enabled) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->enabled = p_enabled;
- }
- void RendererCanvasCull::canvas_light_occluder_set_polygon(RID p_occluder, RID p_polygon) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- if (occluder->polygon.is_valid()) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(occluder->polygon);
- if (occluder_poly) {
- occluder_poly->owners.erase(occluder);
- }
- }
- occluder->polygon = p_polygon;
- occluder->occluder = RID();
- if (occluder->polygon.is_valid()) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_polygon);
- if (!occluder_poly) {
- occluder->polygon = RID();
- ERR_FAIL_NULL(occluder_poly);
- } else {
- occluder_poly->owners.insert(occluder);
- occluder->occluder = occluder_poly->occluder;
- occluder->aabb_cache = occluder_poly->aabb;
- occluder->cull_cache = occluder_poly->cull_mode;
- }
- }
- }
- void RendererCanvasCull::canvas_light_occluder_set_as_sdf_collision(RID p_occluder, bool p_enable) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->sdf_collision = p_enable;
- }
- void RendererCanvasCull::canvas_light_occluder_set_transform(RID p_occluder, const Transform2D &p_xform) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- if (_interpolation_data.interpolation_enabled && occluder->interpolated) {
- if (!occluder->on_interpolate_transform_list) {
- _interpolation_data.canvas_light_occluder_transform_update_list_curr->push_back(p_occluder);
- occluder->on_interpolate_transform_list = true;
- } else {
- DEV_ASSERT(_interpolation_data.canvas_light_occluder_transform_update_list_curr->size() > 0);
- }
- }
- occluder->xform_curr = p_xform;
- }
- void RendererCanvasCull::canvas_light_occluder_set_light_mask(RID p_occluder, int p_mask) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->light_mask = p_mask;
- }
- void RendererCanvasCull::canvas_light_occluder_set_interpolated(RID p_occluder, bool p_interpolated) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->interpolated = p_interpolated;
- }
- void RendererCanvasCull::canvas_light_occluder_reset_physics_interpolation(RID p_occluder) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->xform_prev = occluder->xform_curr;
- }
- void RendererCanvasCull::canvas_light_occluder_transform_physics_interpolation(RID p_occluder, const Transform2D &p_transform) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_occluder);
- ERR_FAIL_NULL(occluder);
- occluder->xform_prev = p_transform * occluder->xform_prev;
- occluder->xform_curr = p_transform * occluder->xform_curr;
- }
- RID RendererCanvasCull::canvas_occluder_polygon_allocate() {
- return canvas_light_occluder_polygon_owner.allocate_rid();
- }
- void RendererCanvasCull::canvas_occluder_polygon_initialize(RID p_rid) {
- canvas_light_occluder_polygon_owner.initialize_rid(p_rid);
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_rid);
- occluder_poly->occluder = RSG::canvas_render->occluder_polygon_create();
- }
- void RendererCanvasCull::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const Vector<Vector2> &p_shape, bool p_closed) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_occluder_polygon);
- ERR_FAIL_NULL(occluder_poly);
- uint32_t pc = p_shape.size();
- ERR_FAIL_COND(pc < 2);
- occluder_poly->aabb = Rect2();
- const Vector2 *r = p_shape.ptr();
- for (uint32_t i = 0; i < pc; i++) {
- if (i == 0) {
- occluder_poly->aabb.position = r[i];
- } else {
- occluder_poly->aabb.expand_to(r[i]);
- }
- }
- RSG::canvas_render->occluder_polygon_set_shape(occluder_poly->occluder, p_shape, p_closed);
- for (RendererCanvasRender::LightOccluderInstance *E : occluder_poly->owners) {
- E->aabb_cache = occluder_poly->aabb;
- }
- }
- void RendererCanvasCull::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon, RS::CanvasOccluderPolygonCullMode p_mode) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_occluder_polygon);
- ERR_FAIL_NULL(occluder_poly);
- occluder_poly->cull_mode = p_mode;
- RSG::canvas_render->occluder_polygon_set_cull_mode(occluder_poly->occluder, p_mode);
- for (RendererCanvasRender::LightOccluderInstance *E : occluder_poly->owners) {
- E->cull_cache = p_mode;
- }
- }
- void RendererCanvasCull::canvas_set_shadow_texture_size(int p_size) {
- RSG::canvas_render->set_shadow_texture_size(p_size);
- }
- RID RendererCanvasCull::canvas_texture_allocate() {
- return RSG::texture_storage->canvas_texture_allocate();
- }
- void RendererCanvasCull::canvas_texture_initialize(RID p_rid) {
- RSG::texture_storage->canvas_texture_initialize(p_rid);
- }
- void RendererCanvasCull::canvas_texture_set_channel(RID p_canvas_texture, RS::CanvasTextureChannel p_channel, RID p_texture) {
- RSG::texture_storage->canvas_texture_set_channel(p_canvas_texture, p_channel, p_texture);
- }
- void RendererCanvasCull::canvas_texture_set_shading_parameters(RID p_canvas_texture, const Color &p_base_color, float p_shininess) {
- RSG::texture_storage->canvas_texture_set_shading_parameters(p_canvas_texture, p_base_color, p_shininess);
- }
- void RendererCanvasCull::canvas_texture_set_texture_filter(RID p_canvas_texture, RS::CanvasItemTextureFilter p_filter) {
- RSG::texture_storage->canvas_texture_set_texture_filter(p_canvas_texture, p_filter);
- }
- void RendererCanvasCull::canvas_texture_set_texture_repeat(RID p_canvas_texture, RS::CanvasItemTextureRepeat p_repeat) {
- RSG::texture_storage->canvas_texture_set_texture_repeat(p_canvas_texture, p_repeat);
- }
- void RendererCanvasCull::canvas_item_set_default_texture_filter(RID p_item, RS::CanvasItemTextureFilter p_filter) {
- Item *ci = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(ci);
- ci->texture_filter = p_filter;
- }
- void RendererCanvasCull::canvas_item_set_default_texture_repeat(RID p_item, RS::CanvasItemTextureRepeat p_repeat) {
- Item *ci = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL(ci);
- ci->texture_repeat = p_repeat;
- }
- void RendererCanvasCull::update_visibility_notifiers() {
- SelfList<Item::VisibilityNotifierData> *E = visibility_notifier_list.first();
- while (E) {
- SelfList<Item::VisibilityNotifierData> *N = E->next();
- Item::VisibilityNotifierData *visibility_notifier = E->self();
- if (visibility_notifier->just_visible) {
- visibility_notifier->just_visible = false;
- if (visibility_notifier->enter_callable.is_valid()) {
- if (RSG::threaded) {
- visibility_notifier->enter_callable.call_deferred();
- } else {
- visibility_notifier->enter_callable.call();
- }
- }
- } else {
- if (visibility_notifier->visible_in_frame != RSG::rasterizer->get_frame_number()) {
- visibility_notifier_list.remove(E);
- if (visibility_notifier->exit_callable.is_valid()) {
- if (RSG::threaded) {
- visibility_notifier->exit_callable.call_deferred();
- } else {
- visibility_notifier->exit_callable.call();
- }
- }
- }
- }
- E = N;
- }
- }
- Rect2 RendererCanvasCull::_debug_canvas_item_get_rect(RID p_item) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_item);
- ERR_FAIL_NULL_V(canvas_item, Rect2());
- return canvas_item->get_rect();
- }
- bool RendererCanvasCull::free(RID p_rid) {
- if (canvas_owner.owns(p_rid)) {
- Canvas *canvas = canvas_owner.get_or_null(p_rid);
- ERR_FAIL_NULL_V(canvas, false);
- while (canvas->viewports.size()) {
- RendererViewport::Viewport *vp = RSG::viewport->viewport_owner.get_or_null(*canvas->viewports.begin());
- ERR_FAIL_NULL_V(vp, true);
- HashMap<RID, RendererViewport::Viewport::CanvasData>::Iterator E = vp->canvas_map.find(p_rid);
- ERR_FAIL_COND_V(!E, true);
- vp->canvas_map.erase(p_rid);
- canvas->viewports.erase(*canvas->viewports.begin());
- }
- for (int i = 0; i < canvas->child_items.size(); i++) {
- canvas->child_items[i].item->parent = RID();
- }
- for (RendererCanvasRender::Light *E : canvas->lights) {
- E->canvas = RID();
- }
- for (RendererCanvasRender::LightOccluderInstance *E : canvas->occluders) {
- E->canvas = RID();
- }
- canvas_owner.free(p_rid);
- } else if (canvas_item_owner.owns(p_rid)) {
- Item *canvas_item = canvas_item_owner.get_or_null(p_rid);
- ERR_FAIL_NULL_V(canvas_item, true);
- _interpolation_data.notify_free_canvas_item(p_rid, *canvas_item);
- if (canvas_item->parent.is_valid()) {
- if (canvas_owner.owns(canvas_item->parent)) {
- Canvas *canvas = canvas_owner.get_or_null(canvas_item->parent);
- canvas->erase_item(canvas_item);
- } else if (canvas_item_owner.owns(canvas_item->parent)) {
- Item *item_owner = canvas_item_owner.get_or_null(canvas_item->parent);
- item_owner->child_items.erase(canvas_item);
- if (item_owner->sort_y) {
- _mark_ysort_dirty(item_owner, canvas_item_owner);
- }
- }
- }
- for (int i = 0; i < canvas_item->child_items.size(); i++) {
- canvas_item->child_items[i]->parent = RID();
- }
- if (canvas_item->visibility_notifier != nullptr) {
- visibility_notifier_allocator.free(canvas_item->visibility_notifier);
- }
- /*
- if (canvas_item->material) {
- canvas_item->material->owners.erase(canvas_item);
- }
- */
- if (canvas_item->canvas_group != nullptr) {
- memdelete(canvas_item->canvas_group);
- canvas_item->canvas_group = nullptr;
- }
- canvas_item_owner.free(p_rid);
- } else if (canvas_light_owner.owns(p_rid)) {
- RendererCanvasRender::Light *canvas_light = canvas_light_owner.get_or_null(p_rid);
- ERR_FAIL_NULL_V(canvas_light, true);
- _interpolation_data.notify_free_canvas_light(p_rid, *canvas_light);
- if (canvas_light->canvas.is_valid()) {
- Canvas *canvas = canvas_owner.get_or_null(canvas_light->canvas);
- if (canvas) {
- canvas->lights.erase(canvas_light);
- }
- }
- RSG::canvas_render->free(canvas_light->light_internal);
- canvas_light_owner.free(p_rid);
- } else if (canvas_light_occluder_owner.owns(p_rid)) {
- RendererCanvasRender::LightOccluderInstance *occluder = canvas_light_occluder_owner.get_or_null(p_rid);
- ERR_FAIL_NULL_V(occluder, true);
- _interpolation_data.notify_free_canvas_light_occluder(p_rid, *occluder);
- if (occluder->polygon.is_valid()) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(occluder->polygon);
- if (occluder_poly) {
- occluder_poly->owners.erase(occluder);
- }
- }
- if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
- Canvas *canvas = canvas_owner.get_or_null(occluder->canvas);
- canvas->occluders.erase(occluder);
- }
- canvas_light_occluder_owner.free(p_rid);
- } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
- LightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get_or_null(p_rid);
- ERR_FAIL_NULL_V(occluder_poly, true);
- RSG::canvas_render->free(occluder_poly->occluder);
- while (occluder_poly->owners.size()) {
- (*occluder_poly->owners.begin())->polygon = RID();
- occluder_poly->owners.remove(occluder_poly->owners.begin());
- }
- canvas_light_occluder_polygon_owner.free(p_rid);
- } else {
- return false;
- }
- return true;
- }
- template <typename T>
- void RendererCanvasCull::_free_rids(T &p_owner, const char *p_type) {
- List<RID> owned;
- p_owner.get_owned_list(&owned);
- if (owned.size()) {
- if (owned.size() == 1) {
- WARN_PRINT(vformat("1 RID of type \"%s\" was leaked.", p_type));
- } else {
- WARN_PRINT(vformat("%d RIDs of type \"%s\" were leaked.", owned.size(), p_type));
- }
- for (const RID &E : owned) {
- free(E);
- }
- }
- }
- void RendererCanvasCull::finalize() {
- _free_rids(canvas_owner, "Canvas");
- _free_rids(canvas_item_owner, "CanvasItem");
- _free_rids(canvas_light_owner, "CanvasLight");
- _free_rids(canvas_light_occluder_owner, "CanvasLightOccluder");
- _free_rids(canvas_light_occluder_polygon_owner, "CanvasLightOccluderPolygon");
- }
- void RendererCanvasCull::tick() {
- if (_interpolation_data.interpolation_enabled) {
- update_interpolation_tick(true);
- }
- }
- void RendererCanvasCull::update_interpolation_tick(bool p_process) {
- #define GODOT_UPDATE_INTERPOLATION_TICK(m_list_prev, m_list_curr, m_type, m_owner_list) \
- /* Detect any that were on the previous transform list that are no longer active. */ \
- for (unsigned int n = 0; n < _interpolation_data.m_list_prev->size(); n++) { \
- const RID &rid = (*_interpolation_data.m_list_prev)[n]; \
- m_type *item = m_owner_list.get_or_null(rid); \
- /* no longer active? (either the instance deleted or no longer being transformed) */ \
- if (item && !item->on_interpolate_transform_list) { \
- item->xform_prev = item->xform_curr; \
- } \
- } \
- /* and now for any in the transform list (being actively interpolated), */ \
- /* keep the previous transform value up to date and ready for next tick */ \
- if (p_process) { \
- for (unsigned int n = 0; n < _interpolation_data.m_list_curr->size(); n++) { \
- const RID &rid = (*_interpolation_data.m_list_curr)[n]; \
- m_type *item = m_owner_list.get_or_null(rid); \
- if (item) { \
- item->xform_prev = item->xform_curr; \
- item->on_interpolate_transform_list = false; \
- } \
- } \
- } \
- SWAP(_interpolation_data.m_list_curr, _interpolation_data.m_list_prev); \
- _interpolation_data.m_list_curr->clear();
- GODOT_UPDATE_INTERPOLATION_TICK(canvas_item_transform_update_list_prev, canvas_item_transform_update_list_curr, Item, canvas_item_owner);
- GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_transform_update_list_prev, canvas_light_transform_update_list_curr, RendererCanvasRender::Light, canvas_light_owner);
- GODOT_UPDATE_INTERPOLATION_TICK(canvas_light_occluder_transform_update_list_prev, canvas_light_occluder_transform_update_list_curr, RendererCanvasRender::LightOccluderInstance, canvas_light_occluder_owner);
- #undef GODOT_UPDATE_INTERPOLATION_TICK
- }
- void RendererCanvasCull::InterpolationData::notify_free_canvas_item(RID p_rid, RendererCanvasCull::Item &r_canvas_item) {
- r_canvas_item.on_interpolate_transform_list = false;
- if (!interpolation_enabled) {
- return;
- }
- // If the instance was on any of the lists, remove.
- canvas_item_transform_update_list_curr->erase_multiple_unordered(p_rid);
- canvas_item_transform_update_list_prev->erase_multiple_unordered(p_rid);
- }
- void RendererCanvasCull::InterpolationData::notify_free_canvas_light(RID p_rid, RendererCanvasRender::Light &r_canvas_light) {
- r_canvas_light.on_interpolate_transform_list = false;
- if (!interpolation_enabled) {
- return;
- }
- // If the instance was on any of the lists, remove.
- canvas_light_transform_update_list_curr->erase_multiple_unordered(p_rid);
- canvas_light_transform_update_list_prev->erase_multiple_unordered(p_rid);
- }
- void RendererCanvasCull::InterpolationData::notify_free_canvas_light_occluder(RID p_rid, RendererCanvasRender::LightOccluderInstance &r_canvas_light_occluder) {
- r_canvas_light_occluder.on_interpolate_transform_list = false;
- if (!interpolation_enabled) {
- return;
- }
- // If the instance was on any of the lists, remove.
- canvas_light_occluder_transform_update_list_curr->erase_multiple_unordered(p_rid);
- canvas_light_occluder_transform_update_list_prev->erase_multiple_unordered(p_rid);
- }
- RendererCanvasCull::RendererCanvasCull() {
- z_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
- z_last_list = (RendererCanvasRender::Item **)memalloc(z_range * sizeof(RendererCanvasRender::Item *));
- disable_scale = false;
- debug_redraw_time = GLOBAL_DEF("debug/canvas_items/debug_redraw_time", 1.0);
- debug_redraw_color = GLOBAL_DEF("debug/canvas_items/debug_redraw_color", Color(1.0, 0.2, 0.2, 0.5));
- }
- RendererCanvasCull::~RendererCanvasCull() {
- memfree(z_list);
- memfree(z_last_list);
- }
|