visual_server_raster.cpp 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800
  1. /*************************************************************************/
  2. /* visual_server_raster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "visual_server_raster.h"
  30. #include "os/os.h"
  31. #include "globals.h"
  32. #include "default_mouse_cursor.xpm"
  33. #include "sort.h"
  34. // careful, these may run in different threads than the visual server
  35. BalloonAllocator<> *VisualServerRaster::OctreeAllocator::allocator=NULL;
  36. #define VS_CHANGED\
  37. changes++;\
  38. // print_line(__FUNCTION__);
  39. RID VisualServerRaster::texture_create() {
  40. return rasterizer->texture_create();
  41. }
  42. void VisualServerRaster::texture_allocate(RID p_texture, int p_width, int p_height,Image::Format p_format,uint32_t p_flags) {
  43. rasterizer->texture_allocate(p_texture,p_width,p_height,p_format,p_flags);
  44. }
  45. void VisualServerRaster::texture_set_flags(RID p_texture,uint32_t p_flags) {
  46. VS_CHANGED;
  47. rasterizer->texture_set_flags(p_texture,p_flags);
  48. }
  49. void VisualServerRaster::texture_set_data(RID p_texture,const Image& p_image,CubeMapSide p_cube_side) {
  50. VS_CHANGED;
  51. rasterizer->texture_set_data(p_texture,p_image,p_cube_side);
  52. }
  53. Image VisualServerRaster::texture_get_data(RID p_texture,CubeMapSide p_cube_side) const {
  54. return rasterizer->texture_get_data(p_texture,p_cube_side);
  55. }
  56. uint32_t VisualServerRaster::texture_get_flags(RID p_texture) const {
  57. return rasterizer->texture_get_flags(p_texture);
  58. }
  59. Image::Format VisualServerRaster::texture_get_format(RID p_texture) const {
  60. return rasterizer->texture_get_format(p_texture);
  61. }
  62. uint32_t VisualServerRaster::texture_get_width(RID p_texture) const {
  63. return rasterizer->texture_get_width(p_texture);
  64. }
  65. uint32_t VisualServerRaster::texture_get_height(RID p_texture) const {
  66. return rasterizer->texture_get_height(p_texture);
  67. }
  68. void VisualServerRaster::texture_set_size_override(RID p_texture,int p_width, int p_height) {
  69. rasterizer->texture_set_size_override(p_texture,p_width,p_height);
  70. }
  71. bool VisualServerRaster::texture_can_stream(RID p_texture) const {
  72. return false;
  73. }
  74. void VisualServerRaster::texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const {
  75. rasterizer->texture_set_reload_hook(p_texture,p_owner,p_function);
  76. }
  77. /* SHADER API */
  78. RID VisualServerRaster::shader_create(ShaderMode p_mode) {
  79. return rasterizer->shader_create(p_mode);
  80. }
  81. void VisualServerRaster::shader_set_mode(RID p_shader,ShaderMode p_mode){
  82. VS_CHANGED;
  83. rasterizer->shader_set_mode(p_shader,p_mode);
  84. }
  85. VisualServer::ShaderMode VisualServerRaster::shader_get_mode(RID p_shader) const{
  86. return rasterizer->shader_get_mode(p_shader);
  87. }
  88. void VisualServerRaster::shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,int p_vertex_ofs,int p_fragment_ofs) {
  89. VS_CHANGED;
  90. rasterizer->shader_set_code(p_shader,p_vertex,p_fragment,p_vertex_ofs,p_fragment_ofs);
  91. }
  92. String VisualServerRaster::shader_get_vertex_code(RID p_shader) const{
  93. return rasterizer->shader_get_vertex_code(p_shader);
  94. }
  95. String VisualServerRaster::shader_get_fragment_code(RID p_shader) const{
  96. return rasterizer->shader_get_fragment_code(p_shader);
  97. }
  98. void VisualServerRaster::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  99. return rasterizer->shader_get_param_list(p_shader,p_param_list);
  100. }
  101. /* Material */
  102. RID VisualServerRaster::material_create() {
  103. return rasterizer->material_create();
  104. }
  105. void VisualServerRaster::material_set_shader(RID p_material, RID p_shader) {
  106. VS_CHANGED;
  107. rasterizer->material_set_shader(p_material, p_shader );
  108. }
  109. RID VisualServerRaster::material_get_shader(RID p_material) const {
  110. return rasterizer->material_get_shader(p_material);
  111. }
  112. void VisualServerRaster::material_set_param(RID p_material, const StringName& p_param, const Variant& p_value) {
  113. VS_CHANGED;
  114. rasterizer->material_set_param(p_material, p_param,p_value );
  115. }
  116. Variant VisualServerRaster::material_get_param(RID p_material, const StringName& p_param) const {
  117. return rasterizer->material_get_param(p_material,p_param);
  118. }
  119. void VisualServerRaster::material_set_flag(RID p_material, MaterialFlag p_flag,bool p_enabled) {
  120. VS_CHANGED;
  121. rasterizer->material_set_flag(p_material,p_flag,p_enabled);
  122. }
  123. void VisualServerRaster::material_set_hint(RID p_material, MaterialHint p_hint,bool p_enabled) {
  124. VS_CHANGED;
  125. rasterizer->material_set_hint(p_material,p_hint,p_enabled);
  126. }
  127. bool VisualServerRaster::material_get_hint(RID p_material,MaterialHint p_hint) const {
  128. return rasterizer->material_get_hint(p_material,p_hint);
  129. }
  130. void VisualServerRaster::material_set_shade_model(RID p_material, MaterialShadeModel p_model) {
  131. VS_CHANGED;
  132. rasterizer->material_set_shade_model(p_material,p_model);
  133. }
  134. VisualServer::MaterialShadeModel VisualServerRaster::material_get_shade_model(RID p_material) const {
  135. return rasterizer->material_get_shade_model(p_material);
  136. }
  137. bool VisualServerRaster::material_get_flag(RID p_material,MaterialFlag p_flag) const {
  138. return rasterizer->material_get_flag(p_material,p_flag);
  139. }
  140. void VisualServerRaster::material_set_blend_mode(RID p_material,MaterialBlendMode p_mode) {
  141. VS_CHANGED;
  142. rasterizer->material_set_blend_mode(p_material,p_mode);
  143. }
  144. VS::MaterialBlendMode VisualServerRaster::material_get_blend_mode(RID p_material) const {
  145. return rasterizer->material_get_blend_mode(p_material);
  146. }
  147. void VisualServerRaster::material_set_line_width(RID p_material,float p_line_width) {
  148. VS_CHANGED;
  149. rasterizer->material_set_line_width(p_material,p_line_width);
  150. }
  151. float VisualServerRaster::material_get_line_width(RID p_material) const {
  152. return rasterizer->material_get_line_width(p_material);
  153. }
  154. /* FIXED MATERIAL */
  155. RID VisualServerRaster::fixed_material_create() {
  156. return rasterizer->fixed_material_create();
  157. }
  158. void VisualServerRaster::fixed_material_set_flag(RID p_material, FixedMaterialFlags p_flag, bool p_enabled) {
  159. rasterizer->fixed_material_set_flag(p_material,p_flag,p_enabled);
  160. }
  161. bool VisualServerRaster::fixed_material_get_flag(RID p_material, FixedMaterialFlags p_flag) const {
  162. return rasterizer->fixed_material_get_flag(p_material,p_flag);
  163. }
  164. void VisualServerRaster::fixed_material_set_param(RID p_material, FixedMaterialParam p_parameter, const Variant& p_value) {
  165. VS_CHANGED;
  166. rasterizer->fixed_material_set_parameter(p_material,p_parameter,p_value);
  167. }
  168. Variant VisualServerRaster::fixed_material_get_param(RID p_material,FixedMaterialParam p_parameter) const {
  169. return rasterizer->fixed_material_get_parameter(p_material,p_parameter);
  170. }
  171. void VisualServerRaster::fixed_material_set_texture(RID p_material,FixedMaterialParam p_parameter, RID p_texture) {
  172. VS_CHANGED;
  173. rasterizer->fixed_material_set_texture(p_material,p_parameter,p_texture);
  174. }
  175. RID VisualServerRaster::fixed_material_get_texture(RID p_material,FixedMaterialParam p_parameter) const {
  176. return rasterizer->fixed_material_get_texture(p_material,p_parameter);
  177. }
  178. void VisualServerRaster::fixed_material_set_detail_blend_mode(RID p_material,MaterialBlendMode p_mode) {
  179. VS_CHANGED;
  180. rasterizer->fixed_material_set_detail_blend_mode(p_material,p_mode);
  181. }
  182. VS::MaterialBlendMode VisualServerRaster::fixed_material_get_detail_blend_mode(RID p_material) const {
  183. return rasterizer->fixed_material_get_detail_blend_mode(p_material);
  184. }
  185. void VisualServerRaster::fixed_material_set_texcoord_mode(RID p_material,FixedMaterialParam p_parameter, FixedMaterialTexCoordMode p_mode) {
  186. VS_CHANGED;
  187. rasterizer->fixed_material_set_texcoord_mode(p_material,p_parameter,p_mode);
  188. }
  189. VS::FixedMaterialTexCoordMode VisualServerRaster::fixed_material_get_texcoord_mode(RID p_material,FixedMaterialParam p_parameter) const {
  190. return rasterizer->fixed_material_get_texcoord_mode(p_material,p_parameter);
  191. }
  192. void VisualServerRaster::fixed_material_set_point_size(RID p_material,float p_size) {
  193. VS_CHANGED
  194. rasterizer->fixed_material_set_point_size(p_material,p_size);
  195. }
  196. float VisualServerRaster::fixed_material_get_point_size(RID p_material) const{
  197. return rasterizer->fixed_material_get_point_size(p_material);
  198. }
  199. void VisualServerRaster::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) {
  200. VS_CHANGED;
  201. rasterizer->fixed_material_set_uv_transform(p_material,p_transform);
  202. }
  203. Transform VisualServerRaster::fixed_material_get_uv_transform(RID p_material) const {
  204. return rasterizer->fixed_material_get_uv_transform(p_material);
  205. }
  206. /* MESH API */
  207. RID VisualServerRaster::mesh_create() {
  208. return rasterizer->mesh_create();
  209. }
  210. void VisualServerRaster::mesh_set_morph_target_count(RID p_mesh,int p_amount) {
  211. rasterizer->mesh_set_morph_target_count(p_mesh,p_amount);
  212. int amount = rasterizer->mesh_get_morph_target_count(p_mesh);
  213. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_mesh );
  214. if (!E)
  215. return;
  216. Set<RID>::Element *I = E->get().front();
  217. while(I) {
  218. Instance *ins = instance_owner.get( I->get() );
  219. ins->data.morph_values.resize(amount);
  220. I = I->next();
  221. }
  222. }
  223. int VisualServerRaster::mesh_get_morph_target_count(RID p_mesh) const {
  224. return rasterizer->mesh_get_morph_target_count(p_mesh);
  225. }
  226. void VisualServerRaster::mesh_set_morph_target_mode(RID p_mesh,MorphTargetMode p_mode) {
  227. rasterizer->mesh_set_morph_target_mode(p_mesh,p_mode);
  228. }
  229. VisualServer::MorphTargetMode VisualServerRaster::mesh_get_morph_target_mode(RID p_mesh) const{
  230. return rasterizer->mesh_get_morph_target_mode(p_mesh);
  231. }
  232. void VisualServerRaster::mesh_add_custom_surface(RID p_mesh,const Variant& p_dat) {
  233. }
  234. void VisualServerRaster::mesh_add_surface(RID p_mesh,PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alpha_sort) {
  235. VS_CHANGED;
  236. _dependency_queue_update(p_mesh,true);
  237. rasterizer->mesh_add_surface(p_mesh,p_primitive,p_arrays,p_blend_shapes,p_alpha_sort);
  238. }
  239. Array VisualServerRaster::mesh_get_surface_arrays(RID p_mesh,int p_surface) const {
  240. return rasterizer->mesh_get_surface_arrays(p_mesh,p_surface);
  241. }
  242. Array VisualServerRaster::mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const {
  243. return rasterizer->mesh_get_surface_morph_arrays(p_mesh,p_surface);
  244. }
  245. void VisualServerRaster::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned){
  246. VS_CHANGED;
  247. rasterizer->mesh_surface_set_material(p_mesh,p_surface,p_material,p_owned);
  248. }
  249. RID VisualServerRaster::mesh_surface_get_material(RID p_mesh,int p_surface) const {
  250. return rasterizer->mesh_surface_get_material(p_mesh,p_surface);
  251. }
  252. int VisualServerRaster::mesh_surface_get_array_len(RID p_mesh, int p_surface) const{
  253. return rasterizer->mesh_surface_get_array_len(p_mesh,p_surface);
  254. }
  255. int VisualServerRaster::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const{
  256. return rasterizer->mesh_surface_get_array_index_len(p_mesh,p_surface);
  257. }
  258. uint32_t VisualServerRaster::mesh_surface_get_format(RID p_mesh, int p_surface) const{
  259. return rasterizer->mesh_surface_get_format(p_mesh,p_surface);
  260. }
  261. VisualServer::PrimitiveType VisualServerRaster::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const{
  262. return rasterizer->mesh_surface_get_primitive_type(p_mesh,p_surface);
  263. }
  264. void VisualServerRaster::mesh_remove_surface(RID p_mesh,int p_surface){
  265. rasterizer->mesh_remove_surface(p_mesh,p_surface);
  266. }
  267. int VisualServerRaster::mesh_get_surface_count(RID p_mesh) const{
  268. return rasterizer->mesh_get_surface_count(p_mesh);
  269. }
  270. void VisualServerRaster::mesh_set_custom_aabb(RID p_mesh,const AABB& p_aabb) {
  271. VS_CHANGED;
  272. _dependency_queue_update(p_mesh,true);
  273. rasterizer->mesh_set_custom_aabb(p_mesh,p_aabb);
  274. }
  275. AABB VisualServerRaster::mesh_get_custom_aabb(RID p_mesh) const {
  276. return rasterizer->mesh_get_custom_aabb(p_mesh);
  277. }
  278. /* MULTIMESH */
  279. RID VisualServerRaster::multimesh_create() {
  280. return rasterizer->multimesh_create();
  281. }
  282. void VisualServerRaster::multimesh_set_instance_count(RID p_multimesh,int p_count) {
  283. VS_CHANGED;
  284. rasterizer->multimesh_set_instance_count(p_multimesh,p_count);
  285. }
  286. int VisualServerRaster::multimesh_get_instance_count(RID p_multimesh) const {
  287. return rasterizer->multimesh_get_instance_count(p_multimesh);
  288. }
  289. void VisualServerRaster::multimesh_set_mesh(RID p_multimesh,RID p_mesh) {
  290. VS_CHANGED;
  291. rasterizer->multimesh_set_mesh(p_multimesh,p_mesh);
  292. }
  293. void VisualServerRaster::multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb) {
  294. VS_CHANGED;
  295. rasterizer->multimesh_set_aabb(p_multimesh,p_aabb);
  296. _dependency_queue_update(p_multimesh,true);
  297. }
  298. void VisualServerRaster::multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform) {
  299. VS_CHANGED;
  300. rasterizer->multimesh_instance_set_transform(p_multimesh,p_index,p_transform);
  301. }
  302. void VisualServerRaster::multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color) {
  303. VS_CHANGED;
  304. rasterizer->multimesh_instance_set_color(p_multimesh,p_index,p_color);
  305. }
  306. RID VisualServerRaster::multimesh_get_mesh(RID p_multimesh) const {
  307. return rasterizer->multimesh_get_mesh(p_multimesh);
  308. }
  309. AABB VisualServerRaster::multimesh_get_aabb(RID p_multimesh,const AABB& p_aabb) const {
  310. return rasterizer->multimesh_get_aabb(p_multimesh);
  311. }
  312. Transform VisualServerRaster::multimesh_instance_get_transform(RID p_multimesh,int p_index) const {
  313. return rasterizer->multimesh_instance_get_transform(p_multimesh,p_index);
  314. }
  315. Color VisualServerRaster::multimesh_instance_get_color(RID p_multimesh,int p_index) const {
  316. return rasterizer->multimesh_instance_get_color(p_multimesh,p_index);
  317. }
  318. void VisualServerRaster::multimesh_set_visible_instances(RID p_multimesh,int p_visible) {
  319. rasterizer->multimesh_set_visible_instances(p_multimesh,p_visible);
  320. }
  321. int VisualServerRaster::multimesh_get_visible_instances(RID p_multimesh) const {
  322. return rasterizer->multimesh_get_visible_instances(p_multimesh);
  323. }
  324. /* PARTICLES API */
  325. RID VisualServerRaster::particles_create() {
  326. return rasterizer->particles_create();
  327. }
  328. void VisualServerRaster::particles_set_amount(RID p_particles, int p_amount) {
  329. VS_CHANGED;
  330. rasterizer->particles_set_amount(p_particles,p_amount);
  331. }
  332. int VisualServerRaster::particles_get_amount(RID p_particles) const {
  333. return rasterizer->particles_get_amount(p_particles);
  334. }
  335. void VisualServerRaster::particles_set_emitting(RID p_particles, bool p_emitting) {
  336. VS_CHANGED;
  337. rasterizer->particles_set_emitting(p_particles,p_emitting);
  338. }
  339. bool VisualServerRaster::particles_is_emitting(RID p_particles) const {
  340. return rasterizer->particles_is_emitting(p_particles);
  341. }
  342. void VisualServerRaster::particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility) {
  343. VS_CHANGED;
  344. rasterizer->particles_set_visibility_aabb(p_particles, p_visibility);
  345. }
  346. AABB VisualServerRaster::particles_get_visibility_aabb(RID p_particles) const {
  347. return rasterizer->particles_get_visibility_aabb(p_particles);
  348. }
  349. void VisualServerRaster::particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents) {
  350. VS_CHANGED;
  351. rasterizer->particles_set_emission_half_extents(p_particles,p_half_extents);
  352. }
  353. Vector3 VisualServerRaster::particles_get_emission_half_extents(RID p_particles) const {
  354. return rasterizer->particles_get_emission_half_extents(p_particles);
  355. }
  356. void VisualServerRaster::particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity) {
  357. VS_CHANGED;
  358. rasterizer->particles_set_emission_base_velocity(p_particles,p_base_velocity);
  359. }
  360. Vector3 VisualServerRaster::particles_get_emission_base_velocity(RID p_particles) const {
  361. return rasterizer->particles_get_emission_base_velocity(p_particles);
  362. }
  363. void VisualServerRaster::particles_set_emission_points(RID p_particles, const DVector<Vector3>& p_points) {
  364. VS_CHANGED;
  365. rasterizer->particles_set_emission_points(p_particles,p_points);
  366. }
  367. DVector<Vector3> VisualServerRaster::particles_get_emission_points(RID p_particles) const {
  368. return rasterizer->particles_get_emission_points(p_particles);
  369. }
  370. void VisualServerRaster::particles_set_gravity_normal(RID p_particles, const Vector3& p_normal) {
  371. VS_CHANGED;
  372. rasterizer->particles_set_gravity_normal(p_particles,p_normal);
  373. }
  374. Vector3 VisualServerRaster::particles_get_gravity_normal(RID p_particles) const {
  375. return rasterizer->particles_get_gravity_normal(p_particles);
  376. }
  377. void VisualServerRaster::particles_set_variable(RID p_particles, ParticleVariable p_variable,float p_value) {
  378. VS_CHANGED;
  379. rasterizer->particles_set_variable(p_particles,p_variable,p_value);
  380. }
  381. float VisualServerRaster::particles_get_variable(RID p_particles, ParticleVariable p_variable) const {
  382. return rasterizer->particles_get_variable(p_particles,p_variable);
  383. }
  384. void VisualServerRaster::particles_set_randomness(RID p_particles, ParticleVariable p_variable,float p_randomness) {
  385. VS_CHANGED;
  386. rasterizer->particles_set_randomness(p_particles,p_variable,p_randomness);
  387. }
  388. float VisualServerRaster::particles_get_randomness(RID p_particles, ParticleVariable p_variable) const {
  389. return rasterizer->particles_get_randomness(p_particles,p_variable);
  390. }
  391. void VisualServerRaster::particles_set_color_phases(RID p_particles, int p_phases) {
  392. VS_CHANGED;
  393. rasterizer->particles_set_color_phases(p_particles,p_phases);
  394. }
  395. int VisualServerRaster::particles_get_color_phases(RID p_particles) const {
  396. return rasterizer->particles_get_color_phases(p_particles);
  397. }
  398. void VisualServerRaster::particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) {
  399. VS_CHANGED;
  400. rasterizer->particles_set_color_phase_pos(p_particles,p_phase,p_pos);
  401. }
  402. float VisualServerRaster::particles_get_color_phase_pos(RID p_particles, int p_phase) const {
  403. return rasterizer->particles_get_color_phase_pos(p_particles,p_phase);
  404. }
  405. void VisualServerRaster::particles_set_attractors(RID p_particles, int p_attractors) {
  406. VS_CHANGED;
  407. rasterizer->particles_set_attractors(p_particles,p_attractors);
  408. }
  409. int VisualServerRaster::particles_get_attractors(RID p_particles) const {
  410. return rasterizer->particles_get_attractors(p_particles);
  411. }
  412. void VisualServerRaster::particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos) {
  413. VS_CHANGED;
  414. rasterizer->particles_set_attractor_pos(p_particles,p_attractor,p_pos);
  415. }
  416. Vector3 VisualServerRaster::particles_get_attractor_pos(RID p_particles,int p_attractor) const {
  417. return rasterizer->particles_get_attractor_pos(p_particles,p_attractor);
  418. }
  419. void VisualServerRaster::particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) {
  420. VS_CHANGED;
  421. rasterizer->particles_set_attractor_strength(p_particles,p_attractor,p_force);
  422. }
  423. float VisualServerRaster::particles_get_attractor_strength(RID p_particles,int p_attractor) const {
  424. return rasterizer->particles_get_attractor_strength(p_particles,p_attractor);
  425. }
  426. void VisualServerRaster::particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color) {
  427. VS_CHANGED;
  428. rasterizer->particles_set_color_phase_color(p_particles,p_phase,p_color);
  429. }
  430. Color VisualServerRaster::particles_get_color_phase_color(RID p_particles, int p_phase) const {
  431. return rasterizer->particles_get_color_phase_color(p_particles,p_phase);
  432. }
  433. void VisualServerRaster::particles_set_material(RID p_particles, RID p_material,bool p_owned) {
  434. VS_CHANGED;
  435. rasterizer->particles_set_material(p_particles,p_material,p_owned);
  436. }
  437. RID VisualServerRaster::particles_get_material(RID p_particles) const {
  438. return rasterizer->particles_get_material(p_particles);
  439. }
  440. void VisualServerRaster::particles_set_height_from_velocity(RID p_particles, bool p_enable) {
  441. VS_CHANGED;
  442. rasterizer->particles_set_height_from_velocity(p_particles,p_enable);
  443. }
  444. bool VisualServerRaster::particles_has_height_from_velocity(RID p_particles) const {
  445. return rasterizer->particles_has_height_from_velocity(p_particles);
  446. }
  447. void VisualServerRaster::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  448. rasterizer->particles_set_use_local_coordinates(p_particles,p_enable);
  449. }
  450. bool VisualServerRaster::particles_is_using_local_coordinates(RID p_particles) const {
  451. return rasterizer->particles_is_using_local_coordinates(p_particles);
  452. }
  453. /* Light API */
  454. RID VisualServerRaster::light_create(LightType p_type) {
  455. return rasterizer->light_create(p_type);
  456. }
  457. VisualServer::LightType VisualServerRaster::light_get_type(RID p_light) const {
  458. return rasterizer->light_get_type(p_light);
  459. }
  460. void VisualServerRaster::light_set_color(RID p_light,LightColor p_type, const Color& p_color) {
  461. VS_CHANGED;
  462. rasterizer->light_set_color(p_light,p_type,p_color);
  463. }
  464. Color VisualServerRaster::light_get_color(RID p_light,LightColor p_type) const {
  465. return rasterizer->light_get_color(p_light,p_type);
  466. }
  467. void VisualServerRaster::light_set_shadow(RID p_light,bool p_enabled) {
  468. VS_CHANGED;
  469. rasterizer->light_set_shadow(p_light,p_enabled);
  470. }
  471. bool VisualServerRaster::light_has_shadow(RID p_light) const {
  472. return rasterizer->light_has_shadow(p_light);
  473. }
  474. void VisualServerRaster::light_set_volumetric(RID p_light,bool p_enabled) {
  475. VS_CHANGED;
  476. rasterizer->light_set_volumetric(p_light,p_enabled);
  477. }
  478. bool VisualServerRaster::light_is_volumetric(RID p_light) const {
  479. return rasterizer->light_is_volumetric(p_light);
  480. }
  481. void VisualServerRaster::light_set_projector(RID p_light,RID p_texture) {
  482. VS_CHANGED;
  483. rasterizer->light_set_projector(p_light,p_texture);
  484. }
  485. RID VisualServerRaster::light_get_projector(RID p_light) const {
  486. return rasterizer->light_get_projector(p_light);
  487. }
  488. void VisualServerRaster::light_set_param(RID p_light, LightParam p_var, float p_value) {
  489. VS_CHANGED;
  490. rasterizer->light_set_var(p_light,p_var,p_value);
  491. _dependency_queue_update(p_light,true);
  492. }
  493. float VisualServerRaster::light_get_param(RID p_light, LightParam p_var) const {
  494. return rasterizer->light_get_var(p_light,p_var);
  495. }
  496. void VisualServerRaster::light_set_operator(RID p_light,LightOp p_op) {
  497. VS_CHANGED;
  498. rasterizer->light_set_operator(p_light,p_op);
  499. }
  500. VisualServerRaster::LightOp VisualServerRaster::light_get_operator(RID p_light) const {
  501. return rasterizer->light_get_operator(p_light);
  502. }
  503. void VisualServerRaster::light_omni_set_shadow_mode(RID p_light,LightOmniShadowMode p_mode) {
  504. VS_CHANGED;
  505. rasterizer->light_omni_set_shadow_mode(p_light,p_mode);
  506. }
  507. VisualServerRaster::LightOmniShadowMode VisualServerRaster::light_omni_get_shadow_mode(RID p_light) const {
  508. return rasterizer->light_omni_get_shadow_mode(p_light);
  509. }
  510. void VisualServerRaster::light_directional_set_shadow_mode(RID p_light,LightDirectionalShadowMode p_mode){
  511. VS_CHANGED;
  512. rasterizer->light_directional_set_shadow_mode(p_light,p_mode);
  513. }
  514. VS::LightDirectionalShadowMode VisualServerRaster::light_directional_get_shadow_mode(RID p_light) const{
  515. return rasterizer->light_directional_get_shadow_mode(p_light);
  516. }
  517. void VisualServerRaster::light_directional_set_shadow_param(RID p_light,LightDirectionalShadowParam p_param, float p_value) {
  518. VS_CHANGED;
  519. rasterizer->light_directional_set_shadow_param(p_light,p_param,p_value);
  520. }
  521. float VisualServerRaster::light_directional_get_shadow_param(RID p_light,LightDirectionalShadowParam p_param) const {
  522. return rasterizer->light_directional_get_shadow_param(p_light,p_param);
  523. }
  524. RID VisualServerRaster::skeleton_create() {
  525. return rasterizer->skeleton_create();
  526. }
  527. void VisualServerRaster::skeleton_resize(RID p_skeleton,int p_bones) {
  528. VS_CHANGED;
  529. rasterizer->skeleton_resize(p_skeleton,p_bones);
  530. }
  531. int VisualServerRaster::skeleton_get_bone_count(RID p_skeleton) const {
  532. return rasterizer->skeleton_get_bone_count(p_skeleton);
  533. }
  534. void VisualServerRaster::skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform) {
  535. VS_CHANGED;
  536. return rasterizer->skeleton_bone_set_transform(p_skeleton,p_bone,p_transform);
  537. }
  538. Transform VisualServerRaster::skeleton_bone_get_transform(RID p_skeleton,int p_bone) {
  539. return rasterizer->skeleton_bone_get_transform(p_skeleton,p_bone);
  540. }
  541. /* VISIBILITY API */
  542. /* ROOM API */
  543. RID VisualServerRaster::room_create() {
  544. Room *room = memnew( Room );
  545. ERR_FAIL_COND_V(!room,RID());
  546. return room_owner.make_rid( room );
  547. }
  548. void VisualServerRaster::room_set_bounds(RID p_room, const BSP_Tree& p_bounds) {
  549. VS_CHANGED;
  550. Room *room = room_owner.get(p_room);
  551. ERR_FAIL_COND(!room);
  552. room->bounds=p_bounds;
  553. }
  554. BSP_Tree VisualServerRaster::room_get_bounds(RID p_room) const {
  555. Room *room = room_owner.get(p_room);
  556. ERR_FAIL_COND_V(!room, BSP_Tree());
  557. return room->bounds;
  558. }
  559. /* PORTAL API */
  560. RID VisualServerRaster::portal_create() {
  561. VS_CHANGED;
  562. Portal *portal = memnew( Portal );
  563. ERR_FAIL_COND_V(!portal,RID());
  564. return portal_owner.make_rid( portal );
  565. }
  566. void VisualServerRaster::portal_set_shape(RID p_portal, const Vector<Point2>& p_shape) {
  567. VS_CHANGED;
  568. Portal *portal = portal_owner.get(p_portal);
  569. ERR_FAIL_COND(!portal);
  570. portal->shape=p_shape;
  571. portal->bounds=Rect2();
  572. for(int i=0;i<p_shape.size();i++) {
  573. if (i==0)
  574. portal->bounds.pos=p_shape[i];
  575. else
  576. portal->bounds.expand_to(p_shape[i]);
  577. }
  578. _dependency_queue_update(p_portal,true);
  579. }
  580. Vector<Point2> VisualServerRaster::portal_get_shape(RID p_portal) const {
  581. Portal *portal = portal_owner.get(p_portal);
  582. ERR_FAIL_COND_V(!portal, Vector<Point2>());
  583. return portal->shape;
  584. }
  585. void VisualServerRaster::portal_set_enabled(RID p_portal, bool p_enabled) {
  586. VS_CHANGED;
  587. Portal *portal = portal_owner.get(p_portal);
  588. ERR_FAIL_COND(!portal);
  589. portal->enabled=p_enabled;
  590. }
  591. bool VisualServerRaster::portal_is_enabled(RID p_portal) const {
  592. Portal *portal = portal_owner.get(p_portal);
  593. ERR_FAIL_COND_V(!portal, false);
  594. return portal->enabled;
  595. }
  596. void VisualServerRaster::portal_set_disable_distance(RID p_portal, float p_distance) {
  597. VS_CHANGED;
  598. Portal *portal = portal_owner.get(p_portal);
  599. ERR_FAIL_COND(!portal);
  600. portal->disable_distance=p_distance;
  601. }
  602. float VisualServerRaster::portal_get_disable_distance(RID p_portal) const {
  603. Portal *portal = portal_owner.get(p_portal);
  604. ERR_FAIL_COND_V(!portal, -1);
  605. return portal->disable_distance;
  606. }
  607. void VisualServerRaster::portal_set_disabled_color(RID p_portal, const Color& p_color) {
  608. VS_CHANGED;
  609. Portal *portal = portal_owner.get(p_portal);
  610. ERR_FAIL_COND(!portal);
  611. portal->disable_color=p_color;
  612. }
  613. Color VisualServerRaster::portal_get_disabled_color(RID p_portal) const {
  614. Portal *portal = portal_owner.get(p_portal);
  615. ERR_FAIL_COND_V(!portal, Color());
  616. return portal->disable_color;
  617. }
  618. void VisualServerRaster::portal_set_connect_range(RID p_portal, float p_range) {
  619. VS_CHANGED;
  620. Portal *portal = portal_owner.get(p_portal);
  621. ERR_FAIL_COND(!portal);
  622. portal->connect_range=p_range;
  623. _dependency_queue_update(p_portal,true);
  624. }
  625. float VisualServerRaster::portal_get_connect_range(RID p_portal) const {
  626. Portal *portal = portal_owner.get(p_portal);
  627. ERR_FAIL_COND_V(!portal,0);
  628. return portal->connect_range;
  629. }
  630. /* CAMERA API */
  631. RID VisualServerRaster::camera_create() {
  632. Camera * camera = memnew( Camera );
  633. return camera_owner.make_rid( camera );
  634. }
  635. void VisualServerRaster::camera_set_perspective(RID p_camera,float p_fovy_degrees, float p_z_near, float p_z_far) {
  636. VS_CHANGED;
  637. Camera *camera = camera_owner.get( p_camera );
  638. ERR_FAIL_COND(!camera);
  639. camera->type=Camera::PERSPECTIVE;
  640. camera->fov=p_fovy_degrees;
  641. camera->znear=p_z_near;
  642. camera->zfar=p_z_far;
  643. }
  644. void VisualServerRaster::camera_set_orthogonal(RID p_camera,float p_size, float p_z_near, float p_z_far) {
  645. VS_CHANGED;
  646. Camera *camera = camera_owner.get( p_camera );
  647. ERR_FAIL_COND(!camera);
  648. camera->type=Camera::ORTHOGONAL;
  649. camera->size=p_size;
  650. camera->znear=p_z_near;
  651. camera->zfar=p_z_far;
  652. }
  653. void VisualServerRaster::camera_set_transform(RID p_camera,const Transform& p_transform) {
  654. VS_CHANGED;
  655. Camera *camera = camera_owner.get( p_camera );
  656. ERR_FAIL_COND(!camera);
  657. camera->transform=p_transform;
  658. }
  659. void VisualServerRaster::camera_set_visible_layers(RID p_camera,uint32_t p_layers) {
  660. VS_CHANGED;
  661. Camera *camera = camera_owner.get( p_camera );
  662. ERR_FAIL_COND(!camera);
  663. camera->visible_layers=p_layers;
  664. }
  665. uint32_t VisualServerRaster::camera_get_visible_layers(RID p_camera) const{
  666. const Camera *camera = camera_owner.get( p_camera );
  667. ERR_FAIL_COND_V(!camera,0);
  668. return camera->visible_layers;
  669. }
  670. void VisualServerRaster::camera_set_environment(RID p_camera,RID p_env) {
  671. Camera *camera = camera_owner.get( p_camera );
  672. ERR_FAIL_COND(!camera);
  673. camera->env=p_env;
  674. }
  675. RID VisualServerRaster::camera_get_environment(RID p_camera) const {
  676. const Camera *camera = camera_owner.get( p_camera );
  677. ERR_FAIL_COND_V(!camera,RID());
  678. return camera->env;
  679. }
  680. void VisualServerRaster::camera_set_use_vertical_aspect(RID p_camera,bool p_enable) {
  681. Camera *camera = camera_owner.get( p_camera );
  682. ERR_FAIL_COND(!camera);
  683. camera->vaspect=p_enable;
  684. }
  685. bool VisualServerRaster::camera_is_using_vertical_aspect(RID p_camera,bool p_enable) const{
  686. const Camera *camera = camera_owner.get( p_camera );
  687. ERR_FAIL_COND_V(!camera,false);
  688. return camera->vaspect;
  689. }
  690. /* VIEWPORT API */
  691. RID VisualServerRaster::viewport_create() {
  692. Viewport *viewport = memnew( Viewport );
  693. RID rid = viewport_owner.make_rid( viewport );
  694. ERR_FAIL_COND_V( !rid.is_valid(), rid );
  695. viewport->self=rid;
  696. viewport->hide_scenario=false;
  697. viewport->hide_canvas=false;
  698. viewport->viewport_data=rasterizer->viewport_data_create();
  699. return rid;
  700. }
  701. void VisualServerRaster::viewport_attach_to_screen(RID p_viewport,int p_screen) {
  702. VS_CHANGED;
  703. Viewport *viewport = viewport_owner.get( p_viewport );
  704. ERR_FAIL_COND(!viewport);
  705. screen_viewports[p_viewport]=p_screen;
  706. }
  707. void VisualServerRaster::viewport_detach(RID p_viewport) {
  708. VS_CHANGED;
  709. Viewport *viewport = viewport_owner.get( p_viewport );
  710. ERR_FAIL_COND(!viewport);
  711. ERR_FAIL_COND(!screen_viewports.has(p_viewport));
  712. screen_viewports.erase(p_viewport);
  713. }
  714. void VisualServerRaster::viewport_set_as_render_target(RID p_viewport,bool p_enable) {
  715. VS_CHANGED;
  716. Viewport *viewport = viewport_owner.get( p_viewport );
  717. ERR_FAIL_COND(!viewport);
  718. if (viewport->render_target.is_valid()==p_enable)
  719. return;
  720. if (!p_enable) {
  721. rasterizer->free(viewport->render_target);
  722. viewport->render_target=RID();
  723. viewport->render_target_texture=RID();
  724. if (viewport->update_list.in_list())
  725. viewport_update_list.remove(&viewport->update_list);
  726. } else {
  727. viewport->render_target=rasterizer->render_target_create();
  728. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  729. viewport->render_target_texture=rasterizer->render_target_get_texture(viewport->render_target);
  730. if (viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  731. viewport_update_list.add(&viewport->update_list);
  732. }
  733. }
  734. void VisualServerRaster::viewport_set_render_target_update_mode(RID p_viewport,RenderTargetUpdateMode p_mode){
  735. VS_CHANGED;
  736. Viewport *viewport = viewport_owner.get( p_viewport );
  737. ERR_FAIL_COND(!viewport);
  738. if (viewport->render_target.is_valid() && viewport->update_list.in_list())
  739. viewport_update_list.remove(&viewport->update_list);
  740. viewport->render_target_update_mode=p_mode;
  741. if (viewport->render_target.is_valid() &&viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  742. viewport_update_list.add(&viewport->update_list);
  743. }
  744. VisualServer::RenderTargetUpdateMode VisualServerRaster::viewport_get_render_target_update_mode(RID p_viewport) const{
  745. const Viewport *viewport = viewport_owner.get( p_viewport );
  746. ERR_FAIL_COND_V(!viewport,RENDER_TARGET_UPDATE_DISABLED);
  747. return viewport->render_target_update_mode;
  748. }
  749. RID VisualServerRaster::viewport_get_render_target_texture(RID p_viewport) const{
  750. Viewport *viewport = viewport_owner.get( p_viewport );
  751. ERR_FAIL_COND_V(!viewport,RID());
  752. return viewport->render_target_texture;
  753. }
  754. void VisualServerRaster::viewport_set_render_target_vflip(RID p_viewport,bool p_enable) {
  755. Viewport *viewport = viewport_owner.get( p_viewport );
  756. ERR_FAIL_COND(!viewport);
  757. viewport->render_target_vflip=p_enable;
  758. }
  759. void VisualServerRaster::viewport_set_render_target_to_screen_rect(RID p_viewport,const Rect2& p_rect) {
  760. Viewport *viewport = viewport_owner.get( p_viewport );
  761. ERR_FAIL_COND(!viewport);
  762. viewport->rt_to_screen_rect=p_rect;
  763. }
  764. bool VisualServerRaster::viewport_get_render_target_vflip(RID p_viewport) const{
  765. const Viewport *viewport = viewport_owner.get( p_viewport );
  766. ERR_FAIL_COND_V(!viewport,false);
  767. return viewport->render_target_vflip;
  768. }
  769. void VisualServerRaster::viewport_queue_screen_capture(RID p_viewport) {
  770. VS_CHANGED;
  771. Viewport *viewport = viewport_owner.get( p_viewport );
  772. ERR_FAIL_COND(!viewport);
  773. viewport->queue_capture=true;
  774. }
  775. Image VisualServerRaster::viewport_get_screen_capture(RID p_viewport) const {
  776. Viewport *viewport = (Viewport*)viewport_owner.get( p_viewport );
  777. ERR_FAIL_COND_V(!viewport,Image());
  778. Image ret = viewport->capture;
  779. viewport->capture=Image();
  780. return ret;
  781. }
  782. void VisualServerRaster::viewport_set_rect(RID p_viewport,const ViewportRect& p_rect) {
  783. VS_CHANGED;
  784. Viewport *viewport=NULL;
  785. viewport = viewport_owner.get( p_viewport );
  786. ERR_FAIL_COND(!viewport);
  787. viewport->rect=p_rect;
  788. if (viewport->render_target.is_valid()) {
  789. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  790. }
  791. }
  792. VisualServer::ViewportRect VisualServerRaster::viewport_get_rect(RID p_viewport) const {
  793. const Viewport *viewport=NULL;
  794. viewport = viewport_owner.get( p_viewport );
  795. ERR_FAIL_COND_V(!viewport, ViewportRect());
  796. return viewport->rect;
  797. }
  798. void VisualServerRaster::viewport_set_hide_scenario(RID p_viewport,bool p_hide) {
  799. VS_CHANGED;
  800. Viewport *viewport=NULL;
  801. viewport = viewport_owner.get( p_viewport );
  802. ERR_FAIL_COND(!viewport);
  803. viewport->hide_scenario=p_hide;
  804. }
  805. void VisualServerRaster::viewport_set_hide_canvas(RID p_viewport,bool p_hide) {
  806. VS_CHANGED;
  807. Viewport *viewport=NULL;
  808. viewport = viewport_owner.get( p_viewport );
  809. ERR_FAIL_COND(!viewport);
  810. viewport->hide_canvas=p_hide;
  811. }
  812. void VisualServerRaster::viewport_attach_camera(RID p_viewport,RID p_camera) {
  813. VS_CHANGED;
  814. Viewport *viewport=NULL;
  815. viewport = viewport_owner.get( p_viewport );
  816. ERR_FAIL_COND(!viewport);
  817. if (p_camera.is_valid()) {
  818. ERR_FAIL_COND(!camera_owner.owns(p_camera));
  819. // a camera
  820. viewport->camera=p_camera;
  821. } else {
  822. viewport->camera=RID();
  823. }
  824. }
  825. void VisualServerRaster::viewport_set_scenario(RID p_viewport,RID p_scenario) {
  826. VS_CHANGED;
  827. Viewport *viewport=NULL;
  828. viewport = viewport_owner.get( p_viewport );
  829. ERR_FAIL_COND(!viewport);
  830. if (p_scenario.is_valid()) {
  831. ERR_FAIL_COND(!scenario_owner.owns(p_scenario));
  832. // a camera
  833. viewport->scenario=p_scenario;
  834. } else {
  835. viewport->scenario=RID();
  836. }
  837. }
  838. RID VisualServerRaster::viewport_get_attached_camera(RID p_viewport) const {
  839. const Viewport *viewport=NULL;
  840. viewport = viewport_owner.get( p_viewport );
  841. ERR_FAIL_COND_V(!viewport, RID());
  842. return viewport->camera;
  843. }
  844. void VisualServerRaster::viewport_attach_canvas(RID p_viewport,RID p_canvas) {
  845. VS_CHANGED;
  846. Viewport *viewport=NULL;
  847. viewport = viewport_owner.get( p_viewport );
  848. ERR_FAIL_COND(!viewport);
  849. Canvas *canvas = canvas_owner.get( p_canvas );
  850. ERR_FAIL_COND(!canvas);
  851. ERR_EXPLAIN("Canvas already attached.");
  852. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  853. Viewport::CanvasData cd;
  854. cd.canvas=canvas;
  855. cd.layer=0;
  856. viewport->canvas_map[p_canvas]=cd;
  857. canvas->viewports.insert(p_viewport);
  858. }
  859. void VisualServerRaster::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_transform) {
  860. VS_CHANGED;
  861. Viewport *viewport=NULL;
  862. viewport = viewport_owner.get( p_viewport );
  863. ERR_FAIL_COND(!viewport);
  864. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  865. if (!E) {
  866. ERR_EXPLAIN("Viewport does not contain the canvas");
  867. ERR_FAIL_COND(!E);
  868. }
  869. E->get().transform=p_transform;
  870. }
  871. Matrix32 VisualServerRaster::viewport_get_canvas_transform(RID p_viewport,RID p_canvas) const {
  872. Viewport *viewport=NULL;
  873. viewport = viewport_owner.get( p_viewport );
  874. ERR_FAIL_COND_V(!viewport,Matrix32());
  875. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  876. if (!E) {
  877. ERR_EXPLAIN("Viewport does not contain the canvas");
  878. ERR_FAIL_COND_V(!E,Matrix32());
  879. }
  880. return E->get().transform;
  881. }
  882. void VisualServerRaster::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform) {
  883. VS_CHANGED
  884. Viewport *viewport=NULL;
  885. viewport = viewport_owner.get( p_viewport );
  886. ERR_FAIL_COND(!viewport);
  887. viewport->global_transform=p_transform;
  888. }
  889. Matrix32 VisualServerRaster::viewport_get_global_canvas_transform(RID p_viewport) const{
  890. Viewport *viewport=NULL;
  891. viewport = viewport_owner.get( p_viewport );
  892. ERR_FAIL_COND_V(!viewport,Matrix32());
  893. return viewport->global_transform;
  894. }
  895. void VisualServerRaster::viewport_remove_canvas(RID p_viewport,RID p_canvas) {
  896. VS_CHANGED;
  897. Viewport *viewport=NULL;
  898. viewport = viewport_owner.get( p_viewport );
  899. ERR_FAIL_COND(!viewport);
  900. Canvas *canvas = canvas_owner.get( p_canvas );
  901. ERR_FAIL_COND(!canvas);
  902. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  903. if (!E) {
  904. ERR_EXPLAIN("Viewport does not contain the canvas");
  905. ERR_FAIL_COND(!E);
  906. }
  907. canvas->viewports.erase(p_viewport);
  908. viewport->canvas_map.erase(E);
  909. }
  910. void VisualServerRaster::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer) {
  911. VS_CHANGED;
  912. Viewport *viewport=NULL;
  913. viewport = viewport_owner.get( p_viewport );
  914. ERR_FAIL_COND(!viewport);
  915. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  916. if (!E) {
  917. ERR_EXPLAIN("Viewport does not contain the canvas");
  918. ERR_FAIL_COND(!E);
  919. }
  920. E->get().layer=p_layer;
  921. }
  922. void VisualServerRaster::viewport_set_transparent_background(RID p_viewport,bool p_enabled) {
  923. VS_CHANGED;
  924. Viewport *viewport=viewport_owner.get( p_viewport );
  925. ERR_FAIL_COND(!viewport);
  926. viewport->transparent_bg=p_enabled;
  927. }
  928. bool VisualServerRaster::viewport_has_transparent_background(RID p_viewport) const {
  929. Viewport *viewport=viewport_owner.get( p_viewport );
  930. ERR_FAIL_COND_V(!viewport, false);
  931. return viewport->transparent_bg;
  932. }
  933. RID VisualServerRaster::viewport_get_scenario(RID p_viewport) const {
  934. const Viewport *viewport=NULL;
  935. viewport = viewport_owner.get( p_viewport );
  936. ERR_FAIL_COND_V(!viewport, RID());
  937. return viewport->scenario;
  938. }
  939. RID VisualServerRaster::environment_create() {
  940. return rasterizer->environment_create();
  941. }
  942. void VisualServerRaster::environment_set_background(RID p_env,EnvironmentBG p_bg){
  943. rasterizer->environment_set_background(p_env,p_bg);
  944. }
  945. VisualServer::EnvironmentBG VisualServerRaster::environment_get_background(RID p_env) const{
  946. return rasterizer->environment_get_background(p_env);
  947. }
  948. void VisualServerRaster::environment_set_background_param(RID p_env,EnvironmentBGParam p_param, const Variant& p_value){
  949. rasterizer->environment_set_background_param(p_env,p_param,p_value);
  950. }
  951. Variant VisualServerRaster::environment_get_background_param(RID p_env,EnvironmentBGParam p_param) const{
  952. return rasterizer->environment_get_background_param(p_env,p_param);
  953. }
  954. void VisualServerRaster::environment_set_enable_fx(RID p_env,EnvironmentFx p_effect,bool p_enabled){
  955. rasterizer->environment_set_enable_fx(p_env,p_effect,p_enabled);
  956. }
  957. bool VisualServerRaster::environment_is_fx_enabled(RID p_env,EnvironmentFx p_effect) const{
  958. return rasterizer->environment_is_fx_enabled(p_env,p_effect);
  959. }
  960. void VisualServerRaster::environment_fx_set_param(RID p_env,EnvironmentFxParam p_param,const Variant& p_value){
  961. rasterizer->environment_fx_set_param(p_env,p_param,p_value);
  962. }
  963. Variant VisualServerRaster::environment_fx_get_param(RID p_env,EnvironmentFxParam p_param) const {
  964. return environment_fx_get_param(p_env,p_param);
  965. }
  966. /* SCENARIO API */
  967. void VisualServerRaster::_dependency_queue_update(RID p_rid,bool p_update_aabb) {
  968. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  969. if (!E)
  970. return;
  971. Set<RID>::Element *I = E->get().front();
  972. while(I) {
  973. Instance *ins = instance_owner.get( I->get() );
  974. _instance_queue_update( ins , p_update_aabb );
  975. I = I->next();
  976. }
  977. }
  978. void VisualServerRaster::_instance_queue_update(Instance *p_instance,bool p_update_aabb) {
  979. if (p_update_aabb)
  980. p_instance->update_aabb=true;
  981. if (p_instance->update)
  982. return;
  983. p_instance->update_next=instance_update_list;
  984. instance_update_list=p_instance;
  985. p_instance->update=true;
  986. }
  987. RID VisualServerRaster::scenario_create() {
  988. Scenario *scenario = memnew( Scenario );
  989. ERR_FAIL_COND_V(!scenario,RID());
  990. RID scenario_rid = scenario_owner.make_rid( scenario );
  991. scenario->self=scenario_rid;
  992. scenario->octree.set_pair_callback(instance_pair,this);
  993. scenario->octree.set_unpair_callback(instance_unpair,this);
  994. return scenario_rid;
  995. }
  996. void VisualServerRaster::scenario_set_debug(RID p_scenario,ScenarioDebugMode p_debug_mode) {
  997. VS_CHANGED;
  998. Scenario *scenario = scenario_owner.get(p_scenario);
  999. ERR_FAIL_COND(!scenario);
  1000. scenario->debug=p_debug_mode;
  1001. }
  1002. void VisualServerRaster::scenario_set_environment(RID p_scenario, RID p_environment) {
  1003. VS_CHANGED;
  1004. Scenario *scenario = scenario_owner.get(p_scenario);
  1005. ERR_FAIL_COND(!scenario);
  1006. scenario->environment=p_environment;
  1007. }
  1008. RID VisualServerRaster::scenario_get_environment(RID p_scenario, RID p_environment) const{
  1009. const Scenario *scenario = scenario_owner.get(p_scenario);
  1010. ERR_FAIL_COND_V(!scenario,RID());
  1011. return scenario->environment;
  1012. }
  1013. /* INSTANCING API */
  1014. RID VisualServerRaster::instance_create() {
  1015. Instance *instance = memnew( Instance );
  1016. ERR_FAIL_COND_V(!instance,RID());
  1017. RID instance_rid = instance_owner.make_rid(instance);
  1018. instance->self=instance_rid;
  1019. instance->base_type=INSTANCE_NONE;
  1020. instance->scenario=NULL;
  1021. return instance_rid;
  1022. }
  1023. void VisualServerRaster::instance_set_base(RID p_instance, RID p_base) {
  1024. VS_CHANGED;
  1025. Instance *instance = instance_owner.get( p_instance );
  1026. ERR_FAIL_COND( !instance );
  1027. if (instance->base_type!=INSTANCE_NONE) {
  1028. //free anything related to that base
  1029. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( instance->base_rid );
  1030. if (E) {
  1031. // wtf, no E?
  1032. E->get().erase( instance->self );
  1033. } else {
  1034. ERR_PRINT("no base E? Bug?");
  1035. }
  1036. if ( instance->room ) {
  1037. instance_set_room(p_instance,RID());
  1038. /*
  1039. if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK)
  1040. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1041. else if (instance->base_type==INSTANCE_PORTAL) {
  1042. print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE)));
  1043. instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1044. } else if (instance->base_type==INSTANCE_ROOM)
  1045. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1046. else if (instance->base_type==INSTANCE_LIGHT)
  1047. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1048. instance->RE=NULL;*/
  1049. }
  1050. if (instance->light_info) {
  1051. if (instance->scenario && instance->light_info->D)
  1052. instance->scenario->directional_lights.erase( instance->light_info->D );
  1053. rasterizer->free(instance->light_info->instance);
  1054. memdelete(instance->light_info);
  1055. instance->light_info=NULL;
  1056. }
  1057. if (instance->portal_info) {
  1058. _portal_disconnect(instance,true);
  1059. memdelete(instance->portal_info);
  1060. instance->portal_info=NULL;
  1061. }
  1062. if (instance->scenario && instance->octree_id) {
  1063. instance->scenario->octree.erase( instance->octree_id );
  1064. instance->octree_id=0;
  1065. }
  1066. if (instance->room_info) {
  1067. for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) {
  1068. Instance *owned = E->get();
  1069. owned->room=NULL;
  1070. owned->RE=NULL;
  1071. }
  1072. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1073. _portal_disconnect(E->get(),true);
  1074. Instance *owned = E->get();
  1075. owned->room=NULL;
  1076. owned->RE=NULL;
  1077. }
  1078. for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) {
  1079. Instance *owned = E->get();
  1080. owned->room=NULL;
  1081. owned->RE=NULL;
  1082. }
  1083. if (instance->room_info->disconnected_child_portals.size()) {
  1084. ERR_PRINT("BUG: Disconnected portals remain!");
  1085. }
  1086. memdelete(instance->room_info);
  1087. instance->room_info=NULL;
  1088. }
  1089. if (instance->particles_info) {
  1090. rasterizer->free( instance->particles_info->instance );
  1091. memdelete(instance->particles_info);
  1092. instance->particles_info=NULL;
  1093. }
  1094. instance->data.morph_values.clear();
  1095. }
  1096. instance->base_type=INSTANCE_NONE;
  1097. instance->base_rid=RID();
  1098. if (p_base.is_valid()) {
  1099. if (rasterizer->is_mesh(p_base)) {
  1100. instance->base_type=INSTANCE_MESH;
  1101. instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base));
  1102. } else if (rasterizer->is_multimesh(p_base)) {
  1103. instance->base_type=INSTANCE_MULTIMESH;
  1104. } else if (rasterizer->is_particles(p_base)) {
  1105. instance->base_type=INSTANCE_PARTICLES;
  1106. instance->particles_info=memnew( Instance::ParticlesInfo );
  1107. instance->particles_info->instance = rasterizer->particles_instance_create( p_base );
  1108. } else if (rasterizer->is_light(p_base)) {
  1109. instance->base_type=INSTANCE_LIGHT;
  1110. instance->light_info = memnew( Instance::LightInfo );
  1111. instance->light_info->instance = rasterizer->light_instance_create(p_base);
  1112. if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) {
  1113. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1114. }
  1115. } else if (room_owner.owns(p_base)) {
  1116. instance->base_type=INSTANCE_ROOM;
  1117. instance->room_info = memnew( Instance::RoomInfo );
  1118. instance->room_info->room=room_owner.get(p_base);
  1119. } else if (portal_owner.owns(p_base)) {
  1120. instance->base_type=INSTANCE_PORTAL;
  1121. instance->portal_info = memnew(Instance::PortalInfo);
  1122. instance->portal_info->portal=portal_owner.get(p_base);
  1123. } else {
  1124. ERR_EXPLAIN("Invalid base RID for instance!")
  1125. ERR_FAIL();
  1126. }
  1127. instance_dependency_map[ p_base ].insert( instance->self );
  1128. instance->base_rid=p_base;
  1129. if (instance->scenario)
  1130. _instance_queue_update(instance,true);
  1131. }
  1132. }
  1133. RID VisualServerRaster::instance_get_base(RID p_instance) const {
  1134. Instance *instance = instance_owner.get( p_instance );
  1135. ERR_FAIL_COND_V( !instance, RID() );
  1136. return instance->base_rid;
  1137. }
  1138. void VisualServerRaster::instance_set_scenario(RID p_instance, RID p_scenario) {
  1139. VS_CHANGED;
  1140. Instance *instance = instance_owner.get( p_instance );
  1141. ERR_FAIL_COND( !instance );
  1142. if (instance->scenario) {
  1143. Map< RID, Set<RID> >::Element *E = instance_dependency_map.find( instance->scenario->self );
  1144. if (E) {
  1145. // wtf, no E?
  1146. E->get().erase( instance->self );
  1147. } else {
  1148. ERR_PRINT("no scenario E? Bug?");
  1149. }
  1150. if (instance->light_info) {
  1151. if (instance->light_info->D)
  1152. instance->scenario->directional_lights.erase( instance->light_info->D );
  1153. }
  1154. if (instance->portal_info) {
  1155. _portal_disconnect(instance,true);
  1156. }
  1157. if (instance->octree_id) {
  1158. instance->scenario->octree.erase( instance->octree_id );
  1159. instance->octree_id=0;
  1160. }
  1161. instance->scenario=NULL;
  1162. }
  1163. if (p_scenario.is_valid()) {
  1164. Scenario *scenario = scenario_owner.get( p_scenario );
  1165. ERR_FAIL_COND(!scenario);
  1166. instance->scenario=scenario;
  1167. instance_dependency_map[ p_scenario ].insert( instance->self );
  1168. instance->scenario=scenario;
  1169. if (instance->base_type==INSTANCE_LIGHT && rasterizer->light_get_type(instance->base_rid)==LIGHT_DIRECTIONAL) {
  1170. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1171. }
  1172. _instance_queue_update(instance,true);
  1173. }
  1174. }
  1175. RID VisualServerRaster::instance_get_scenario(RID p_instance) const {
  1176. Instance *instance = instance_owner.get( p_instance );
  1177. ERR_FAIL_COND_V( !instance, RID() );
  1178. if (instance->scenario)
  1179. return instance->scenario->self;
  1180. else
  1181. return RID();
  1182. }
  1183. void VisualServerRaster::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  1184. VS_CHANGED;
  1185. Instance *instance = instance_owner.get( p_instance );
  1186. ERR_FAIL_COND( !instance );
  1187. instance->layer_mask=p_mask;
  1188. }
  1189. uint32_t VisualServerRaster::instance_get_layer_mask(RID p_instance) const{
  1190. Instance *instance = instance_owner.get( p_instance );
  1191. ERR_FAIL_COND_V( !instance, 0 );
  1192. return instance->layer_mask;
  1193. }
  1194. AABB VisualServerRaster::instance_get_base_aabb(RID p_instance) const {
  1195. Instance *instance = instance_owner.get( p_instance );
  1196. ERR_FAIL_COND_V( !instance, AABB() );
  1197. return instance->aabb;
  1198. }
  1199. void VisualServerRaster::instance_attach_object_instance_ID(RID p_instance,uint32_t p_ID) {
  1200. VS_CHANGED;
  1201. Instance *instance = instance_owner.get( p_instance );
  1202. ERR_FAIL_COND( !instance );
  1203. instance->object_ID=p_ID;
  1204. }
  1205. uint32_t VisualServerRaster::instance_get_object_instance_ID(RID p_instance) const {
  1206. Instance *instance = instance_owner.get( p_instance );
  1207. ERR_FAIL_COND_V( !instance, 0 );
  1208. return instance->object_ID;
  1209. }
  1210. void VisualServerRaster::instance_attach_skeleton(RID p_instance,RID p_skeleton) {
  1211. VS_CHANGED;
  1212. Instance *instance = instance_owner.get( p_instance );
  1213. ERR_FAIL_COND( !instance );
  1214. instance->data.skeleton=p_skeleton;
  1215. }
  1216. RID VisualServerRaster::instance_get_skeleton(RID p_instance) const {
  1217. Instance *instance = instance_owner.get( p_instance );
  1218. ERR_FAIL_COND_V( !instance, RID() );
  1219. return instance->data.skeleton;
  1220. }
  1221. void VisualServerRaster::instance_set_morph_target_weight(RID p_instance,int p_shape, float p_weight) {
  1222. VS_CHANGED;
  1223. Instance *instance = instance_owner.get( p_instance );
  1224. ERR_FAIL_COND( !instance );
  1225. ERR_FAIL_INDEX( p_shape, instance->data.morph_values.size() );
  1226. instance->data.morph_values[p_shape]=p_weight;
  1227. }
  1228. float VisualServerRaster::instance_get_morph_target_weight(RID p_instance,int p_shape) const {
  1229. Instance *instance = instance_owner.get( p_instance );
  1230. ERR_FAIL_COND_V( !instance, 0 );
  1231. ERR_FAIL_INDEX_V( p_shape, instance->data.morph_values.size(), 0 );
  1232. return instance->data.morph_values[p_shape];
  1233. }
  1234. void VisualServerRaster::instance_set_transform(RID p_instance, const Transform& p_transform) {
  1235. VS_CHANGED;
  1236. Instance *instance = instance_owner.get( p_instance );
  1237. ERR_FAIL_COND( !instance );
  1238. if (p_transform==instance->data.transform) // must improve somehow
  1239. return;
  1240. instance->data.transform=p_transform;
  1241. if (instance->base_type==INSTANCE_LIGHT)
  1242. instance->data.transform.orthonormalize();
  1243. _instance_queue_update(instance);
  1244. }
  1245. Transform VisualServerRaster::instance_get_transform(RID p_instance) const {
  1246. Instance *instance = instance_owner.get( p_instance );
  1247. ERR_FAIL_COND_V( !instance, Transform() );
  1248. return instance->data.transform;
  1249. }
  1250. void VisualServerRaster::instance_set_exterior( RID p_instance, bool p_enabled ) {
  1251. VS_CHANGED;
  1252. Instance *instance = instance_owner.get( p_instance );
  1253. ERR_FAIL_COND( !instance );
  1254. ERR_EXPLAIN("Portals can't be assigned to be exterior");
  1255. ERR_FAIL_COND( instance->base_type == INSTANCE_PORTAL );
  1256. if (instance->exterior==p_enabled)
  1257. return;
  1258. instance->exterior=p_enabled;
  1259. _instance_queue_update( instance );
  1260. }
  1261. bool VisualServerRaster::instance_is_exterior( RID p_instance) const {
  1262. Instance *instance = instance_owner.get( p_instance );
  1263. ERR_FAIL_COND_V( !instance, false );
  1264. return instance->exterior;
  1265. }
  1266. void VisualServerRaster::instance_set_room( RID p_instance, RID p_room ) {
  1267. VS_CHANGED;
  1268. Instance *instance = instance_owner.get( p_instance );
  1269. ERR_FAIL_COND( !instance );
  1270. if (instance->room && instance->RE) {
  1271. //instance already havs a room, remove it from there
  1272. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1273. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1274. if (!p_room.is_valid() && instance->octree_id) {
  1275. //remove from the octree, so it's re-added with different flags
  1276. instance->scenario->octree.erase( instance->octree_id );
  1277. instance->octree_id=0;
  1278. _instance_queue_update( instance,true );
  1279. }
  1280. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1281. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1282. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1283. _portal_disconnect(E->get());
  1284. _instance_queue_update( E->get(),false );
  1285. }
  1286. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1287. _portal_disconnect(instance,true);
  1288. bool ss = instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1289. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1290. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1291. } else {
  1292. ERR_FAIL();
  1293. }
  1294. instance->RE=NULL;
  1295. instance->room=NULL;
  1296. } else {
  1297. if (p_room.is_valid() && instance->octree_id) {
  1298. //remove from the octree, so it's re-added with different flags
  1299. instance->scenario->octree.erase( instance->octree_id );
  1300. instance->octree_id=0;
  1301. _instance_queue_update( instance,true );
  1302. }
  1303. }
  1304. if (!p_room.is_valid())
  1305. return; // just clearning the room
  1306. Instance *room = instance_owner.get( p_room );
  1307. ERR_FAIL_COND( !room );
  1308. ERR_FAIL_COND( room->base_type!=INSTANCE_ROOM );
  1309. if (instance->base_type==INSTANCE_ROOM) {
  1310. //perform cycle test
  1311. Instance *parent = instance;
  1312. while(parent) {
  1313. ERR_EXPLAIN("Cycle in room assignment");
  1314. ERR_FAIL_COND( parent == room );
  1315. parent=parent->room;
  1316. }
  1317. }
  1318. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1319. instance->RE = room->room_info->owned_geometry_instances.push_back(instance);
  1320. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1321. instance->RE = room->room_info->owned_room_instances.push_back(instance);
  1322. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next())
  1323. _instance_queue_update( E->get(),false );
  1324. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1325. instance->RE = room->room_info->owned_portal_instances.push_back(instance);
  1326. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1327. instance->RE = room->room_info->owned_light_instances.push_back(instance);
  1328. } else {
  1329. ERR_FAIL();
  1330. }
  1331. instance->room=room;
  1332. }
  1333. RID VisualServerRaster::instance_get_room( RID p_instance ) const {
  1334. Instance *instance = instance_owner.get( p_instance );
  1335. ERR_FAIL_COND_V( !instance, RID() );
  1336. if (instance->room)
  1337. return instance->room->self;
  1338. else
  1339. return RID();
  1340. }
  1341. void VisualServerRaster::instance_set_extra_visibility_margin( RID p_instance, real_t p_margin ) {
  1342. VS_CHANGED;
  1343. Instance *instance = instance_owner.get( p_instance );
  1344. ERR_FAIL_COND( !instance );
  1345. instance->extra_margin=p_margin;
  1346. }
  1347. real_t VisualServerRaster::instance_get_extra_visibility_margin( RID p_instance ) const{
  1348. Instance *instance = instance_owner.get( p_instance );
  1349. ERR_FAIL_COND_V( !instance, 0 );
  1350. return instance->extra_margin;
  1351. }
  1352. Vector<RID> VisualServerRaster::instances_cull_aabb(const AABB& p_aabb, RID p_scenario) const {
  1353. Vector<RID> instances;
  1354. Scenario *scenario=scenario_owner.get(p_scenario);
  1355. ERR_FAIL_COND_V(!scenario,instances);
  1356. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1357. int culled=0;
  1358. Instance *cull[1024];
  1359. culled=scenario->octree.cull_AABB(p_aabb,cull,1024);
  1360. for (int i=0;i<culled;i++) {
  1361. Instance *instance=cull[i];
  1362. ERR_CONTINUE(!instance);
  1363. instances.push_back(instance->self);
  1364. }
  1365. return instances;
  1366. }
  1367. Vector<RID> VisualServerRaster::instances_cull_ray(const Vector3& p_from, const Vector3& p_to, RID p_scenario) const{
  1368. Vector<RID> instances;
  1369. Scenario *scenario=scenario_owner.get(p_scenario);
  1370. ERR_FAIL_COND_V(!scenario,instances);
  1371. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1372. int culled=0;
  1373. Instance *cull[1024];
  1374. culled=scenario->octree.cull_segment(p_from,p_to*10000,cull,1024);
  1375. for (int i=0;i<culled;i++) {
  1376. Instance *instance=cull[i];
  1377. ERR_CONTINUE(!instance);
  1378. instances.push_back(instance->self);
  1379. }
  1380. return instances;
  1381. }
  1382. Vector<RID> VisualServerRaster::instances_cull_convex(const Vector<Plane>& p_convex, RID p_scenario) const{
  1383. Vector<RID> instances;
  1384. Scenario *scenario=scenario_owner.get(p_scenario);
  1385. ERR_FAIL_COND_V(!scenario,instances);
  1386. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1387. int culled=0;
  1388. Instance *cull[1024];
  1389. culled=scenario->octree.cull_convex(p_convex,cull,1024);
  1390. for (int i=0;i<culled;i++) {
  1391. Instance *instance=cull[i];
  1392. ERR_CONTINUE(!instance);
  1393. instances.push_back(instance->self);
  1394. }
  1395. return instances;
  1396. }
  1397. void VisualServerRaster::instance_geometry_set_flag(RID p_instance,InstanceFlags p_flags,bool p_enabled) {
  1398. Instance *instance = instance_owner.get( p_instance );
  1399. ERR_FAIL_COND( !instance );
  1400. // ERR_FAIL_COND( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK) );
  1401. switch(p_flags) {
  1402. case INSTANCE_FLAG_VISIBLE: {
  1403. instance->visible=p_enabled;
  1404. } break;
  1405. case INSTANCE_FLAG_BILLBOARD: {
  1406. instance->data.billboard=p_enabled;
  1407. } break;
  1408. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1409. instance->data.billboard_y=p_enabled;
  1410. } break;
  1411. case INSTANCE_FLAG_CAST_SHADOW: {
  1412. instance->cast_shadows=p_enabled;
  1413. } break;
  1414. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1415. instance->receive_shadows=p_enabled;
  1416. } break;
  1417. case INSTANCE_FLAG_DEPH_SCALE: {
  1418. instance->data.depth_scale=p_enabled;
  1419. } break;
  1420. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1421. instance->visible_in_all_rooms=p_enabled;
  1422. } break;
  1423. }
  1424. }
  1425. bool VisualServerRaster::instance_geometry_get_flag(RID p_instance,InstanceFlags p_flags) const{
  1426. const Instance *instance = instance_owner.get( p_instance );
  1427. ERR_FAIL_COND_V( !instance, false );
  1428. // ERR_FAIL_COND_V( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK), false );
  1429. switch(p_flags) {
  1430. case INSTANCE_FLAG_VISIBLE: {
  1431. return instance->visible;
  1432. } break;
  1433. case INSTANCE_FLAG_BILLBOARD: {
  1434. return instance->data.billboard;
  1435. } break;
  1436. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1437. return instance->data.billboard_y;
  1438. } break;
  1439. case INSTANCE_FLAG_CAST_SHADOW: {
  1440. return instance->cast_shadows;
  1441. } break;
  1442. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1443. return instance->receive_shadows;
  1444. } break;
  1445. case INSTANCE_FLAG_DEPH_SCALE: {
  1446. return instance->data.depth_scale;
  1447. } break;
  1448. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1449. return instance->visible_in_all_rooms;
  1450. } break;
  1451. }
  1452. return false;
  1453. }
  1454. void VisualServerRaster::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  1455. VS_CHANGED;
  1456. Instance *instance = instance_owner.get( p_instance );
  1457. ERR_FAIL_COND( !instance );
  1458. instance->data.material_override=p_material;
  1459. }
  1460. RID VisualServerRaster::instance_geometry_get_material_override(RID p_instance) const{
  1461. Instance *instance = instance_owner.get( p_instance );
  1462. ERR_FAIL_COND_V( !instance, RID() );
  1463. return instance->data.material_override;
  1464. }
  1465. void VisualServerRaster::instance_geometry_set_draw_range(RID p_instance,float p_min,float p_max){
  1466. VS_CHANGED;
  1467. Instance *instance = instance_owner.get( p_instance );
  1468. ERR_FAIL_COND( !instance );
  1469. instance->draw_range_begin=p_min;
  1470. instance->draw_range_end=p_max;
  1471. }
  1472. float VisualServerRaster::instance_geometry_get_draw_range_min(RID p_instance) const{
  1473. const Instance *instance = instance_owner.get( p_instance );
  1474. ERR_FAIL_COND_V( !instance,0 );
  1475. return instance->draw_range_begin;
  1476. }
  1477. float VisualServerRaster::instance_geometry_get_draw_range_max(RID p_instance) const{
  1478. const Instance *instance = instance_owner.get( p_instance );
  1479. ERR_FAIL_COND_V( !instance,0 );
  1480. return instance->draw_range_end;
  1481. }
  1482. void VisualServerRaster::_update_instance(Instance *p_instance) {
  1483. p_instance->version++;
  1484. if (p_instance->base_type == INSTANCE_LIGHT) {
  1485. rasterizer->light_instance_set_transform( p_instance->light_info->instance, p_instance->data.transform );
  1486. }
  1487. if (p_instance->aabb.has_no_surface())
  1488. return;
  1489. if (p_instance->base_type == INSTANCE_PARTICLES) {
  1490. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  1491. }
  1492. if (p_instance->base_type&INSTANCE_GEOMETRY_MASK) {
  1493. //make sure lights are updated
  1494. InstanceSet::Element *E=p_instance->lights.front();
  1495. while(E) {
  1496. E->get()->version++;
  1497. E=E->next();
  1498. }
  1499. }
  1500. if (p_instance->base_type == INSTANCE_ROOM) {
  1501. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  1502. }
  1503. p_instance->data.mirror = p_instance->data.transform.basis.determinant() < 0.0;
  1504. AABB new_aabb;
  1505. if (p_instance->base_type==INSTANCE_PORTAL) {
  1506. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  1507. Transform portal_xform = p_instance->data.transform;
  1508. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  1509. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  1510. int point_count=p_instance->portal_info->portal->shape.size();
  1511. p_instance->portal_info->transformed_point_cache.resize(point_count);
  1512. AABB portal_aabb;
  1513. for(int i=0;i<point_count;i++) {
  1514. Point2 src = p_instance->portal_info->portal->shape[i];
  1515. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  1516. p_instance->portal_info->transformed_point_cache[i]=point;
  1517. if (i==0)
  1518. portal_aabb.pos=point;
  1519. else
  1520. portal_aabb.expand_to(point);
  1521. }
  1522. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  1523. new_aabb = portal_aabb;
  1524. } else {
  1525. new_aabb = p_instance->data.transform.xform(p_instance->aabb);
  1526. }
  1527. for(InstanceSet::Element *E=p_instance->lights.front();E;E=E->next()) {
  1528. Instance *light = E->get();
  1529. light->version++;
  1530. }
  1531. p_instance->transformed_aabb=new_aabb;
  1532. if (!p_instance->scenario) {
  1533. return;
  1534. }
  1535. if (p_instance->octree_id==0) {
  1536. uint32_t base_type = 1<<p_instance->base_type;
  1537. uint32_t pairable_mask=0;
  1538. bool pairable=false;
  1539. if (p_instance->base_type == INSTANCE_LIGHT) {
  1540. pairable_mask=INSTANCE_GEOMETRY_MASK;
  1541. pairable=true;
  1542. }
  1543. if (p_instance->base_type == INSTANCE_PORTAL) {
  1544. pairable_mask=(1<<INSTANCE_PORTAL);
  1545. pairable=true;
  1546. }
  1547. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1548. base_type|=INSTANCE_ROOMLESS_MASK;
  1549. }
  1550. if (p_instance->base_type == INSTANCE_ROOM) {
  1551. pairable_mask=INSTANCE_ROOMLESS_MASK;
  1552. pairable=true;
  1553. }
  1554. // not inside octree
  1555. p_instance->octree_id = p_instance->scenario->octree.create(p_instance,new_aabb,0,pairable,base_type,pairable_mask);
  1556. } else {
  1557. // if (new_aabb==p_instance->data.transformed_aabb)
  1558. // return;
  1559. p_instance->scenario->octree.move(p_instance->octree_id,new_aabb);
  1560. }
  1561. if (p_instance->base_type==INSTANCE_PORTAL) {
  1562. _portal_attempt_connect(p_instance);
  1563. }
  1564. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1565. _instance_validate_autorooms(p_instance);
  1566. }
  1567. if (p_instance->base_type == INSTANCE_ROOM) {
  1568. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  1569. _instance_validate_autorooms(E->get());
  1570. }
  1571. }
  1572. void VisualServerRaster::_update_instance_aabb(Instance *p_instance) {
  1573. AABB new_aabb;
  1574. ERR_FAIL_COND(p_instance->base_type!=INSTANCE_NONE && !p_instance->base_rid.is_valid());
  1575. switch(p_instance->base_type) {
  1576. case VisualServer::INSTANCE_NONE: {
  1577. // do nothing
  1578. } break;
  1579. case VisualServer::INSTANCE_MESH: {
  1580. new_aabb = rasterizer->mesh_get_aabb(p_instance->base_rid);
  1581. } break;
  1582. case VisualServer::INSTANCE_MULTIMESH: {
  1583. new_aabb = rasterizer->multimesh_get_aabb(p_instance->base_rid);
  1584. } break;
  1585. case VisualServer::INSTANCE_PARTICLES: {
  1586. new_aabb = rasterizer->particles_get_aabb(p_instance->base_rid);
  1587. } break;
  1588. case VisualServer::INSTANCE_LIGHT: {
  1589. new_aabb = rasterizer->light_get_aabb(p_instance->base_rid);
  1590. } break;
  1591. case VisualServer::INSTANCE_ROOM: {
  1592. Room *room = room_owner.get( p_instance->base_rid );
  1593. ERR_FAIL_COND(!room);
  1594. new_aabb=room->bounds.get_aabb();
  1595. } break;
  1596. case VisualServer::INSTANCE_PORTAL: {
  1597. Portal *portal = portal_owner.get( p_instance->base_rid );
  1598. ERR_FAIL_COND(!portal);
  1599. for (int i=0;i<portal->shape.size();i++) {
  1600. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  1601. if (i==0) {
  1602. new_aabb.pos=point;
  1603. new_aabb.size.z=0.01; // make it not flat for octree
  1604. } else {
  1605. new_aabb.expand_to(point);
  1606. }
  1607. }
  1608. } break;
  1609. default: {}
  1610. }
  1611. if (p_instance->extra_margin)
  1612. new_aabb.grow_by(p_instance->extra_margin);
  1613. p_instance->aabb=new_aabb;
  1614. }
  1615. void VisualServerRaster::_update_instances() {
  1616. while(instance_update_list) {
  1617. Instance *instance=instance_update_list;
  1618. instance_update_list=instance_update_list->update_next;
  1619. if (instance->update_aabb)
  1620. _update_instance_aabb(instance);
  1621. _update_instance(instance);
  1622. instance->update=false;
  1623. instance->update_aabb=false;
  1624. instance->update_next=0;
  1625. }
  1626. }
  1627. /****** CANVAS *********/
  1628. RID VisualServerRaster::canvas_create() {
  1629. Canvas * canvas = memnew( Canvas );
  1630. ERR_FAIL_COND_V(!canvas,RID());
  1631. RID rid = canvas_owner.make_rid( canvas );
  1632. return rid;
  1633. }
  1634. void VisualServerRaster::canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring) {
  1635. Canvas * canvas = canvas_owner.get(p_canvas);
  1636. ERR_FAIL_COND(!canvas);
  1637. CanvasItem *canvas_item = canvas_item_owner.get(p_item);
  1638. ERR_FAIL_COND(!canvas_item);
  1639. int idx = canvas->find_item(canvas_item);
  1640. ERR_FAIL_COND(idx==-1);
  1641. canvas->child_items[idx].mirror=p_mirroring;
  1642. }
  1643. Point2 VisualServerRaster::canvas_get_item_mirroring(RID p_canvas,RID p_item) const {
  1644. Canvas * canvas = canvas_owner.get(p_canvas);
  1645. ERR_FAIL_COND_V(!canvas,Point2());
  1646. CanvasItem *canvas_item = memnew( CanvasItem );
  1647. ERR_FAIL_COND_V(!canvas_item,Point2());
  1648. int idx = canvas->find_item(canvas_item);
  1649. ERR_FAIL_COND_V(idx==-1,Point2());
  1650. return canvas->child_items[idx].mirror;
  1651. }
  1652. RID VisualServerRaster::canvas_item_create() {
  1653. CanvasItem *canvas_item = memnew( CanvasItem );
  1654. ERR_FAIL_COND_V(!canvas_item,RID());
  1655. return canvas_item_owner.make_rid( canvas_item );
  1656. }
  1657. void VisualServerRaster::canvas_item_set_parent(RID p_item,RID p_parent) {
  1658. VS_CHANGED;
  1659. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1660. ERR_FAIL_COND(!canvas_item);
  1661. if (canvas_item->parent.is_valid()) {
  1662. if (canvas_owner.owns(canvas_item->parent)) {
  1663. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  1664. canvas->erase_item(canvas_item);
  1665. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1666. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  1667. item_owner->child_items.erase(canvas_item);
  1668. }
  1669. canvas_item->parent=RID();
  1670. }
  1671. if (p_parent.is_valid()) {
  1672. if (canvas_owner.owns(p_parent)) {
  1673. Canvas *canvas = canvas_owner.get(p_parent);
  1674. Canvas::ChildItem ci;
  1675. ci.item=canvas_item;
  1676. canvas->child_items.push_back(ci);
  1677. } else if (canvas_item_owner.owns(p_parent)) {
  1678. CanvasItem *item_owner = canvas_item_owner.get(p_parent);
  1679. item_owner->child_items.push_back(canvas_item);
  1680. } else {
  1681. ERR_EXPLAIN("Invalid parent");
  1682. ERR_FAIL();
  1683. }
  1684. }
  1685. canvas_item->parent=p_parent;
  1686. }
  1687. RID VisualServerRaster::canvas_item_get_parent(RID p_canvas_item) const {
  1688. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1689. ERR_FAIL_COND_V(!canvas_item,RID());
  1690. return canvas_item->parent;
  1691. }
  1692. void VisualServerRaster::canvas_item_set_visible(RID p_item,bool p_visible) {
  1693. VS_CHANGED;
  1694. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1695. ERR_FAIL_COND(!canvas_item);
  1696. canvas_item->visible=p_visible;
  1697. }
  1698. bool VisualServerRaster::canvas_item_is_visible(RID p_item) const {
  1699. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1700. ERR_FAIL_COND_V(!canvas_item,RID());
  1701. return canvas_item->visible;
  1702. }
  1703. void VisualServerRaster::canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend) {
  1704. VS_CHANGED;
  1705. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1706. if (!canvas_item) {
  1707. printf("!canvas_item\n");
  1708. };
  1709. ERR_FAIL_COND(!canvas_item);
  1710. if (canvas_item->blend_mode==p_blend)
  1711. return;
  1712. VS_CHANGED;
  1713. canvas_item->blend_mode=p_blend;
  1714. }
  1715. void VisualServerRaster::canvas_item_attach_viewport(RID p_canvas_item, RID p_viewport) {
  1716. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1717. ERR_FAIL_COND(!canvas_item);
  1718. VS_CHANGED;
  1719. canvas_item->viewport=p_viewport;
  1720. }
  1721. /*
  1722. void VisualServerRaster::canvas_item_set_rect(RID p_item, const Rect2& p_rect) {
  1723. VS_CHANGED;
  1724. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1725. ERR_FAIL_COND(!canvas_item);
  1726. canvas_item->rect=p_rect;
  1727. }*/
  1728. void VisualServerRaster::canvas_item_set_clip(RID p_item, bool p_clip) {
  1729. VS_CHANGED;
  1730. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1731. ERR_FAIL_COND(!canvas_item);
  1732. canvas_item->clip=p_clip;
  1733. }
  1734. const Rect2& VisualServerRaster::CanvasItem::get_rect() const {
  1735. if (custom_rect || !rect_dirty)
  1736. return rect;
  1737. //must update rect
  1738. int s=commands.size();
  1739. if (s==0) {
  1740. rect=Rect2();
  1741. rect_dirty=false;
  1742. return rect;
  1743. }
  1744. Matrix32 xf;
  1745. bool found_xform=false;
  1746. bool first=true;
  1747. const CanvasItem::Command * const *cmd = &commands[0];
  1748. for (int i=0;i<s;i++) {
  1749. const CanvasItem::Command *c=cmd[i];
  1750. Rect2 r;
  1751. switch(c->type) {
  1752. case CanvasItem::Command::TYPE_LINE: {
  1753. const CanvasItem::CommandLine* line = static_cast< const CanvasItem::CommandLine*>(c);
  1754. r.pos=line->from;
  1755. r.expand_to(line->to);
  1756. } break;
  1757. case CanvasItem::Command::TYPE_RECT: {
  1758. const CanvasItem::CommandRect* crect = static_cast< const CanvasItem::CommandRect*>(c);
  1759. r=crect->rect;
  1760. } break;
  1761. case CanvasItem::Command::TYPE_STYLE: {
  1762. const CanvasItem::CommandStyle* style = static_cast< const CanvasItem::CommandStyle*>(c);
  1763. r=style->rect;
  1764. } break;
  1765. case CanvasItem::Command::TYPE_PRIMITIVE: {
  1766. const CanvasItem::CommandPrimitive* primitive = static_cast< const CanvasItem::CommandPrimitive*>(c);
  1767. r.pos=primitive->points[0];
  1768. for(int i=1;i<primitive->points.size();i++) {
  1769. r.expand_to(primitive->points[i]);
  1770. }
  1771. } break;
  1772. case CanvasItem::Command::TYPE_POLYGON: {
  1773. const CanvasItem::CommandPolygon* polygon = static_cast< const CanvasItem::CommandPolygon*>(c);
  1774. int l = polygon->points.size();
  1775. const Point2*pp=&polygon->points[0];
  1776. r.pos=pp[0];
  1777. for(int i=1;i<l;i++) {
  1778. r.expand_to(pp[i]);
  1779. }
  1780. } break;
  1781. case CanvasItem::Command::TYPE_POLYGON_PTR: {
  1782. const CanvasItem::CommandPolygonPtr* polygon = static_cast< const CanvasItem::CommandPolygonPtr*>(c);
  1783. int l = polygon->count;
  1784. if (polygon->indices != NULL) {
  1785. r.pos=polygon->points[polygon->indices[0]];
  1786. for (int i=1; i<polygon->count; i++) {
  1787. r.expand_to(polygon->points[polygon->indices[i]]);
  1788. };
  1789. } else {
  1790. r.pos=polygon->points[0];
  1791. for (int i=1; i<polygon->count; i++) {
  1792. r.expand_to(polygon->points[i]);
  1793. };
  1794. };
  1795. } break;
  1796. case CanvasItem::Command::TYPE_CIRCLE: {
  1797. const CanvasItem::CommandCircle* circle = static_cast< const CanvasItem::CommandCircle*>(c);
  1798. r.pos=Point2(-circle->radius,-circle->radius)+circle->pos;
  1799. r.size=Point2(circle->radius*2.0,circle->radius*2.0);
  1800. } break;
  1801. case CanvasItem::Command::TYPE_TRANSFORM: {
  1802. const CanvasItem::CommandTransform* transform = static_cast<const CanvasItem::CommandTransform*>(c);
  1803. xf=transform->xform;
  1804. found_xform=true;
  1805. continue;
  1806. } break;
  1807. case CanvasItem::Command::TYPE_BLEND_MODE: {
  1808. } break;
  1809. case CanvasItem::Command::TYPE_CLIP_IGNORE: {
  1810. } break;
  1811. }
  1812. if (found_xform) {
  1813. r = xf.xform(r);
  1814. found_xform=false;
  1815. }
  1816. if (first) {
  1817. rect=r;
  1818. first=false;
  1819. } else
  1820. rect=rect.merge(r);
  1821. }
  1822. rect_dirty=false;
  1823. return rect;
  1824. }
  1825. void VisualServerRaster::canvas_item_set_transform(RID p_item, const Matrix32& p_transform) {
  1826. VS_CHANGED;
  1827. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1828. ERR_FAIL_COND(!canvas_item);
  1829. canvas_item->xform=p_transform;
  1830. }
  1831. void VisualServerRaster::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect,const Rect2& p_rect) {
  1832. VS_CHANGED;
  1833. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1834. ERR_FAIL_COND(!canvas_item);
  1835. canvas_item->custom_rect=p_custom_rect;
  1836. if (p_custom_rect)
  1837. canvas_item->rect=p_rect;
  1838. }
  1839. void VisualServerRaster::canvas_item_set_opacity(RID p_item, float p_opacity) {
  1840. VS_CHANGED;
  1841. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1842. ERR_FAIL_COND(!canvas_item);
  1843. canvas_item->opacity=p_opacity;
  1844. }
  1845. float VisualServerRaster::canvas_item_get_opacity(RID p_item, float p_opacity) const {
  1846. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1847. ERR_FAIL_COND_V(!canvas_item,-1);
  1848. return canvas_item->opacity;
  1849. }
  1850. void VisualServerRaster::canvas_item_set_on_top(RID p_item, bool p_on_top) {
  1851. VS_CHANGED;
  1852. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1853. ERR_FAIL_COND(!canvas_item);
  1854. canvas_item->ontop=p_on_top;
  1855. }
  1856. bool VisualServerRaster::canvas_item_is_on_top(RID p_item) const{
  1857. const CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1858. ERR_FAIL_COND_V(!canvas_item,false);
  1859. return canvas_item->ontop;
  1860. }
  1861. void VisualServerRaster::canvas_item_set_self_opacity(RID p_item, float p_self_opacity) {
  1862. VS_CHANGED;
  1863. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1864. ERR_FAIL_COND(!canvas_item);
  1865. canvas_item->self_opacity=p_self_opacity;
  1866. }
  1867. float VisualServerRaster::canvas_item_get_self_opacity(RID p_item, float p_self_opacity) const {
  1868. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1869. ERR_FAIL_COND_V(!canvas_item,-1);
  1870. return canvas_item->self_opacity;
  1871. }
  1872. void VisualServerRaster::canvas_item_add_line(RID p_item, const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width) {
  1873. VS_CHANGED;
  1874. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1875. ERR_FAIL_COND(!canvas_item);
  1876. CanvasItem::CommandLine * line = memnew( CanvasItem::CommandLine );
  1877. ERR_FAIL_COND(!line);
  1878. line->color=p_color;
  1879. line->from=p_from;
  1880. line->to=p_to;
  1881. line->width=p_width;
  1882. canvas_item->rect_dirty=true;
  1883. canvas_item->commands.push_back(line);
  1884. }
  1885. void VisualServerRaster::canvas_item_add_rect(RID p_item, const Rect2& p_rect, const Color& p_color) {
  1886. VS_CHANGED;
  1887. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1888. ERR_FAIL_COND(!canvas_item);
  1889. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  1890. ERR_FAIL_COND(!rect);
  1891. rect->modulate=p_color;
  1892. rect->rect=p_rect;
  1893. canvas_item->rect_dirty=true;
  1894. canvas_item->commands.push_back(rect);
  1895. }
  1896. void VisualServerRaster::canvas_item_add_circle(RID p_item, const Point2& p_pos, float p_radius,const Color& p_color) {
  1897. VS_CHANGED;
  1898. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1899. ERR_FAIL_COND(!canvas_item);
  1900. CanvasItem::CommandCircle * circle = memnew( CanvasItem::CommandCircle );
  1901. ERR_FAIL_COND(!circle);
  1902. circle->color=p_color;
  1903. circle->pos=p_pos;
  1904. circle->radius=p_radius;
  1905. canvas_item->commands.push_back(circle);
  1906. }
  1907. void VisualServerRaster::canvas_item_add_texture_rect(RID p_item, const Rect2& p_rect, RID p_texture,bool p_tile,const Color& p_modulate) {
  1908. VS_CHANGED;
  1909. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1910. ERR_FAIL_COND(!canvas_item);
  1911. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  1912. ERR_FAIL_COND(!rect);
  1913. rect->modulate=p_modulate;
  1914. rect->rect=p_rect;
  1915. rect->flags=0;
  1916. if (p_tile)
  1917. rect->flags|=Rasterizer::CANVAS_RECT_TILE;
  1918. if (p_rect.size.x<0) {
  1919. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  1920. rect->rect.size.x = -rect->rect.size.x;
  1921. }
  1922. if (p_rect.size.y<0) {
  1923. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  1924. rect->rect.size.y = -rect->rect.size.y;
  1925. }
  1926. rect->texture=p_texture;
  1927. canvas_item->rect_dirty=true;
  1928. canvas_item->commands.push_back(rect);
  1929. }
  1930. void VisualServerRaster::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) {
  1931. VS_CHANGED;
  1932. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1933. ERR_FAIL_COND(!canvas_item);
  1934. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  1935. ERR_FAIL_COND(!rect);
  1936. rect->modulate=p_modulate;
  1937. rect->rect=p_rect;
  1938. rect->texture=p_texture;
  1939. rect->source=p_src_rect;
  1940. rect->flags=Rasterizer::CANVAS_RECT_REGION;
  1941. if (p_rect.size.x<0) {
  1942. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  1943. rect->rect.size.x = -rect->rect.size.x;
  1944. }
  1945. if (p_rect.size.y<0) {
  1946. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  1947. rect->rect.size.y = -rect->rect.size.y;
  1948. }
  1949. canvas_item->rect_dirty=true;
  1950. canvas_item->commands.push_back(rect);
  1951. }
  1952. void VisualServerRaster::canvas_item_add_style_box(RID p_item, const Rect2& p_rect, RID p_texture,const Vector2& p_topleft, const Vector2& p_bottomright, bool p_draw_center,const Color& p_modulate) {
  1953. VS_CHANGED;
  1954. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1955. ERR_FAIL_COND(!canvas_item);
  1956. CanvasItem::CommandStyle * style = memnew( CanvasItem::CommandStyle );
  1957. ERR_FAIL_COND(!style);
  1958. style->texture=p_texture;
  1959. style->rect=p_rect;
  1960. style->draw_center=p_draw_center;
  1961. style->color=p_modulate;
  1962. style->margin[MARGIN_LEFT]=p_topleft.x;
  1963. style->margin[MARGIN_TOP]=p_topleft.y;
  1964. style->margin[MARGIN_RIGHT]=p_bottomright.x;
  1965. style->margin[MARGIN_BOTTOM]=p_bottomright.y;
  1966. canvas_item->rect_dirty=true;
  1967. canvas_item->commands.push_back(style);
  1968. }
  1969. void VisualServerRaster::canvas_item_add_primitive(RID p_item,const Vector<Point2>& p_points, const Vector<Color>& p_colors,const Vector<Point2>& p_uvs, RID p_texture,float p_width) {
  1970. VS_CHANGED;
  1971. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1972. ERR_FAIL_COND(!canvas_item);
  1973. CanvasItem::CommandPrimitive * prim = memnew( CanvasItem::CommandPrimitive );
  1974. ERR_FAIL_COND(!prim);
  1975. prim->texture=p_texture;
  1976. prim->points=p_points;
  1977. prim->uvs=p_uvs;
  1978. prim->colors=p_colors;
  1979. prim->width=p_width;
  1980. canvas_item->rect_dirty=true;
  1981. canvas_item->commands.push_back(prim);
  1982. }
  1983. void VisualServerRaster::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) {
  1984. VS_CHANGED;
  1985. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1986. ERR_FAIL_COND(!canvas_item);
  1987. #ifdef DEBUG_ENABLED
  1988. int pointcount = p_points.size();
  1989. ERR_FAIL_COND(pointcount<3);
  1990. int color_size=p_colors.size();
  1991. int uv_size=p_uvs.size();
  1992. ERR_FAIL_COND(color_size!=0 && color_size!=1 && color_size!=pointcount);
  1993. ERR_FAIL_COND(uv_size!=0 && (uv_size!=pointcount || !p_texture.is_valid()));
  1994. #endif
  1995. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  1996. if (indices.empty()) {
  1997. ERR_EXPLAIN("Bad Polygon!");
  1998. ERR_FAIL_V();
  1999. }
  2000. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2001. ERR_FAIL_COND(!polygon);
  2002. polygon->texture=p_texture;
  2003. polygon->points=p_points;
  2004. polygon->uvs=p_uvs;
  2005. polygon->colors=p_colors;
  2006. polygon->indices=indices;
  2007. polygon->count=indices.size();
  2008. canvas_item->rect_dirty=true;
  2009. canvas_item->commands.push_back(polygon);
  2010. }
  2011. void VisualServerRaster::canvas_item_add_triangle_array_ptr(RID p_item, int p_count, const int* p_indices, const Point2* p_points, const Color* p_colors,const Point2* p_uvs, RID p_texture) {
  2012. VS_CHANGED;
  2013. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2014. ERR_FAIL_COND(!canvas_item);
  2015. ERR_FAIL_COND(p_count <= 0);
  2016. ERR_FAIL_COND(p_points == NULL);
  2017. CanvasItem::CommandPolygonPtr * polygon = memnew( CanvasItem::CommandPolygonPtr );
  2018. ERR_FAIL_COND(!polygon);
  2019. polygon->texture=p_texture;
  2020. polygon->points=p_points;
  2021. polygon->uvs=p_uvs;
  2022. polygon->colors=p_colors;
  2023. polygon->indices=p_indices;
  2024. polygon->count = p_count * 3;
  2025. canvas_item->rect_dirty=true;
  2026. canvas_item->commands.push_back(polygon);
  2027. };
  2028. void VisualServerRaster::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, RID p_texture, int p_count) {
  2029. VS_CHANGED;
  2030. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2031. ERR_FAIL_COND(!canvas_item);
  2032. int ps = p_points.size();
  2033. ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps);
  2034. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps);
  2035. Vector<int> indices = p_indices;
  2036. int count = p_count * 3;
  2037. if (indices.empty()) {
  2038. ERR_FAIL_COND( ps % 3 != 0 );
  2039. if (p_count == -1)
  2040. count = ps;
  2041. } else {
  2042. ERR_FAIL_COND( indices.size() % 3 != 0 );
  2043. if (p_count == -1)
  2044. count = indices.size();
  2045. }
  2046. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2047. ERR_FAIL_COND(!polygon);
  2048. polygon->texture=p_texture;
  2049. polygon->points=p_points;
  2050. polygon->uvs=p_uvs;
  2051. polygon->colors=p_colors;
  2052. polygon->indices=indices;
  2053. polygon->count = count;
  2054. canvas_item->rect_dirty=true;
  2055. canvas_item->commands.push_back(polygon);
  2056. }
  2057. void VisualServerRaster::canvas_item_add_set_transform(RID p_item,const Matrix32& p_transform) {
  2058. VS_CHANGED;
  2059. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2060. ERR_FAIL_COND(!canvas_item);
  2061. CanvasItem::CommandTransform * tr = memnew( CanvasItem::CommandTransform );
  2062. ERR_FAIL_COND(!tr);
  2063. tr->xform=p_transform;
  2064. canvas_item->commands.push_back(tr);
  2065. }
  2066. void VisualServerRaster::canvas_item_add_set_blend_mode(RID p_item, MaterialBlendMode p_blend) {
  2067. VS_CHANGED;
  2068. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2069. ERR_FAIL_COND(!canvas_item);
  2070. CanvasItem::CommandBlendMode * bm = memnew( CanvasItem::CommandBlendMode );
  2071. ERR_FAIL_COND(!bm);
  2072. bm->blend_mode = p_blend;
  2073. canvas_item->commands.push_back(bm);
  2074. };
  2075. void VisualServerRaster::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  2076. VS_CHANGED;
  2077. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2078. ERR_FAIL_COND(!canvas_item);
  2079. CanvasItem::CommandClipIgnore * ci = memnew( CanvasItem::CommandClipIgnore);
  2080. ERR_FAIL_COND(!ci);
  2081. ci->ignore=p_ignore;
  2082. canvas_item->commands.push_back(ci);
  2083. }
  2084. void VisualServerRaster::canvas_item_clear(RID p_item) {
  2085. VS_CHANGED;
  2086. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2087. ERR_FAIL_COND(!canvas_item);
  2088. canvas_item->clear();
  2089. }
  2090. void VisualServerRaster::canvas_item_raise(RID p_item) {
  2091. VS_CHANGED;
  2092. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2093. ERR_FAIL_COND(!canvas_item);
  2094. if (canvas_item->parent.is_valid()) {
  2095. if (canvas_owner.owns(canvas_item->parent)) {
  2096. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2097. int idx = canvas->find_item(canvas_item);
  2098. ERR_FAIL_COND(idx<0);
  2099. Canvas::ChildItem ci = canvas->child_items[idx];
  2100. canvas->child_items.remove(idx);
  2101. canvas->child_items.push_back(ci);
  2102. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2103. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2104. int idx = item_owner->child_items.find(canvas_item);
  2105. ERR_FAIL_COND(idx<0);
  2106. item_owner->child_items.remove(idx);
  2107. item_owner->child_items.push_back(canvas_item);
  2108. }
  2109. }
  2110. }
  2111. /******** CANVAS *********/
  2112. void VisualServerRaster::cursor_set_rotation(float p_rotation, int p_cursor) {
  2113. VS_CHANGED;
  2114. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2115. cursors[p_cursor].rot = p_rotation;
  2116. };
  2117. void VisualServerRaster::cursor_set_texture(RID p_texture, const Point2 &p_center_offset, int p_cursor) {
  2118. VS_CHANGED;
  2119. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2120. cursors[p_cursor].texture = p_texture;
  2121. cursors[p_cursor].center = p_center_offset;
  2122. };
  2123. void VisualServerRaster::cursor_set_visible(bool p_visible, int p_cursor) {
  2124. VS_CHANGED;
  2125. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2126. cursors[p_cursor].visible = p_visible;
  2127. };
  2128. void VisualServerRaster::cursor_set_pos(const Point2& p_pos, int p_cursor) {
  2129. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2130. if (cursors[p_cursor].pos==p_pos)
  2131. return;
  2132. VS_CHANGED;
  2133. cursors[p_cursor].pos = p_pos;
  2134. };
  2135. void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  2136. black_margin[MARGIN_LEFT]=p_left;
  2137. black_margin[MARGIN_TOP]=p_top;
  2138. black_margin[MARGIN_RIGHT]=p_right;
  2139. black_margin[MARGIN_BOTTOM]=p_bottom;
  2140. }
  2141. void VisualServerRaster::_free_attached_instances(RID p_rid,bool p_free_scenario) {
  2142. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  2143. if (E) {
  2144. // has instances
  2145. while( E->get().size() ) {
  2146. // erase all attached instances
  2147. if (p_free_scenario)
  2148. instance_set_scenario( E->get().front()->get(), RID() );
  2149. else
  2150. instance_set_base( E->get().front()->get(), RID() );
  2151. }
  2152. }
  2153. instance_dependency_map.erase(p_rid);
  2154. }
  2155. void VisualServerRaster::custom_shade_model_set_shader(int p_model, RID p_shader) {
  2156. VS_CHANGED;
  2157. // rasterizer->custom_shade_model_set_shader(p_model,p_shader);
  2158. }
  2159. RID VisualServerRaster::custom_shade_model_get_shader(int p_model) const {
  2160. //return rasterizer->custom_shade_model_get_shader(p_model);
  2161. return RID();
  2162. }
  2163. void VisualServerRaster::custom_shade_model_set_name(int p_model, const String& p_name) {
  2164. //rasterizer->custom_shade_model_set_name(p_model,p_name);
  2165. }
  2166. String VisualServerRaster::custom_shade_model_get_name(int p_model) const {
  2167. //return rasterizer->custom_shade_model_get_name(p_model);
  2168. return "";
  2169. }
  2170. void VisualServerRaster::custom_shade_model_set_param_info(int p_model, const List<PropertyInfo>& p_info) {
  2171. VS_CHANGED;
  2172. //rasterizer->custom_shade_model_set_param_info(p_model,p_info);
  2173. }
  2174. void VisualServerRaster::custom_shade_model_get_param_info(int p_model, List<PropertyInfo>* p_info) const {
  2175. //rasterizer->custom_shade_model_get_param_info(p_model,p_info);
  2176. }
  2177. void VisualServerRaster::free( RID p_rid ) {
  2178. VS_CHANGED;
  2179. if (rasterizer->is_texture(p_rid) || rasterizer->is_material(p_rid) || rasterizer->is_skeleton(p_rid) || rasterizer->is_shader(p_rid)) {
  2180. rasterizer->free(p_rid);
  2181. } else if (rasterizer->is_mesh(p_rid) || rasterizer->is_multimesh(p_rid) || rasterizer->is_light(p_rid) || rasterizer->is_particles(p_rid) ) {
  2182. //delete the resource
  2183. _free_attached_instances(p_rid);
  2184. rasterizer->free(p_rid);
  2185. } else if (room_owner.owns(p_rid)) {
  2186. _free_attached_instances(p_rid);
  2187. Room *room = room_owner.get(p_rid);
  2188. ERR_FAIL_COND(!room);
  2189. room_owner.free(p_rid);
  2190. memdelete(room);
  2191. } else if (portal_owner.owns(p_rid)) {
  2192. _free_attached_instances(p_rid);
  2193. Portal *portal = portal_owner.get(p_rid);
  2194. ERR_FAIL_COND(!portal);
  2195. portal_owner.free(p_rid);
  2196. memdelete(portal);
  2197. } else if (camera_owner.owns(p_rid)) {
  2198. // delete te camera
  2199. Camera *camera = camera_owner.get(p_rid);
  2200. ERR_FAIL_COND(!camera);
  2201. camera_owner.free( p_rid );
  2202. memdelete(camera);
  2203. } else if (viewport_owner.owns(p_rid)) {
  2204. // delete the viewport
  2205. Viewport *viewport = viewport_owner.get( p_rid );
  2206. ERR_FAIL_COND(!viewport);
  2207. // Viewport *parent=NULL;
  2208. rasterizer->free(viewport->viewport_data);
  2209. if (viewport->render_target.is_valid()) {
  2210. rasterizer->free(viewport->render_target);
  2211. }
  2212. if (viewport->update_list.in_list())
  2213. viewport_update_list.remove(&viewport->update_list);
  2214. if (screen_viewports.has(p_rid))
  2215. screen_viewports.erase(p_rid);
  2216. while(viewport->canvas_map.size()) {
  2217. Canvas *c = viewport->canvas_map.front()->get().canvas;
  2218. c->viewports.erase(p_rid);
  2219. viewport->canvas_map.erase(viewport->canvas_map.front());
  2220. }
  2221. viewport_owner.free(p_rid);
  2222. memdelete(viewport);
  2223. } else if (instance_owner.owns(p_rid)) {
  2224. // delete the instance
  2225. _update_instances(); // be sure
  2226. Instance *instance = instance_owner.get(p_rid);
  2227. ERR_FAIL_COND(!instance);
  2228. instance_set_room(p_rid,RID());
  2229. instance_set_scenario(p_rid,RID());
  2230. instance_set_base(p_rid,RID());
  2231. instance_owner.free(p_rid);
  2232. memdelete(instance);
  2233. } else if (canvas_owner.owns(p_rid)) {
  2234. Canvas *canvas = canvas_owner.get(p_rid);
  2235. ERR_FAIL_COND(!canvas);
  2236. while(canvas->viewports.size()) {
  2237. Viewport *vp = viewport_owner.get(canvas->viewports.front()->get());
  2238. ERR_FAIL_COND(!vp);
  2239. Map<RID,Viewport::CanvasData>::Element *E=vp->canvas_map.find(p_rid);
  2240. ERR_FAIL_COND(!E);
  2241. vp->canvas_map.erase(p_rid);
  2242. canvas->viewports.erase( canvas->viewports.front() );
  2243. }
  2244. for (int i=0;i<canvas->child_items.size();i++) {
  2245. canvas->child_items[i].item->parent=RID();
  2246. }
  2247. canvas_owner.free( p_rid );
  2248. memdelete( canvas );
  2249. } else if (canvas_item_owner.owns(p_rid)) {
  2250. CanvasItem *canvas_item = canvas_item_owner.get(p_rid);
  2251. ERR_FAIL_COND(!canvas_item);
  2252. if (canvas_item->parent.is_valid()) {
  2253. if (canvas_owner.owns(canvas_item->parent)) {
  2254. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2255. canvas->erase_item(canvas_item);
  2256. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2257. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2258. item_owner->child_items.erase(canvas_item);
  2259. }
  2260. }
  2261. for (int i=0;i<canvas_item->child_items.size();i++) {
  2262. canvas_item->child_items[i]->parent=RID();
  2263. }
  2264. canvas_item_owner.free( p_rid );
  2265. memdelete( canvas_item );
  2266. } else if (scenario_owner.owns(p_rid)) {
  2267. Scenario *scenario=scenario_owner.get(p_rid);
  2268. ERR_FAIL_COND(!scenario);
  2269. _update_instances(); // be sure
  2270. _free_attached_instances(p_rid,true);
  2271. //rasterizer->free( scenario->environment );
  2272. scenario_owner.free(p_rid);
  2273. memdelete(scenario);
  2274. } else {
  2275. ERR_FAIL();
  2276. }
  2277. }
  2278. void VisualServerRaster::_instance_draw(Instance *p_instance) {
  2279. if (p_instance->light_cache_dirty) {
  2280. int l=0;
  2281. //add positional lights
  2282. InstanceSet::Element *LE=p_instance->lights.front();
  2283. p_instance->data.light_instances.resize(p_instance->lights.size());
  2284. while(LE) {
  2285. p_instance->data.light_instances[l++]=LE->get()->light_info->instance;
  2286. LE=LE->next();
  2287. }
  2288. p_instance->light_cache_dirty=false;
  2289. }
  2290. switch(p_instance->base_type) {
  2291. case INSTANCE_MESH: {
  2292. const float *morphs = NULL;
  2293. if (!p_instance->data.morph_values.empty()) {
  2294. morphs=&p_instance->data.morph_values[0];
  2295. }
  2296. rasterizer->add_mesh(p_instance->base_rid, &p_instance->data);
  2297. } break;
  2298. case INSTANCE_MULTIMESH: {
  2299. rasterizer->add_multimesh(p_instance->base_rid, &p_instance->data);
  2300. } break;
  2301. case INSTANCE_PARTICLES: {
  2302. rasterizer->add_particles(p_instance->particles_info->instance, &p_instance->data);
  2303. } break;
  2304. default: {};
  2305. }
  2306. }
  2307. Vector<Vector3> VisualServerRaster::_camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  2308. // setup a camera matrix for that range!
  2309. CameraMatrix camera_matrix;
  2310. switch(p_camera->type) {
  2311. case Camera::ORTHOGONAL: {
  2312. camera_matrix.set_orthogonal(p_camera->size,viewport_rect.width / (float)viewport_rect.height,p_range_min,p_range_max,p_camera->vaspect);
  2313. } break;
  2314. case Camera::PERSPECTIVE: {
  2315. camera_matrix.set_perspective(
  2316. p_camera->fov,
  2317. viewport_rect.width / (float)viewport_rect.height,
  2318. p_range_min,
  2319. p_range_max,
  2320. p_camera->vaspect
  2321. );
  2322. } break;
  2323. }
  2324. //obtain the frustum endpoints
  2325. Vector<Vector3> endpoints;
  2326. endpoints.resize(8);
  2327. bool res = camera_matrix.get_endpoints(p_camera->transform,&endpoints[0]);
  2328. ERR_FAIL_COND_V(!res,Vector<Vector3>());
  2329. return endpoints;
  2330. }
  2331. Vector<Plane> VisualServerRaster::_camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  2332. Vector<Vector3> endpoints=_camera_generate_endpoints(p_light,p_camera,p_range_min,p_range_max); // frustum plane endpoints
  2333. ERR_FAIL_COND_V(endpoints.empty(),Vector<Plane>());
  2334. // obtain the light frustm ranges (given endpoints)
  2335. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2336. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2337. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2338. float x_min,x_max;
  2339. float y_min,y_max;
  2340. float z_min,z_max;
  2341. for(int j=0;j<8;j++) {
  2342. float d_x=x_vec.dot(endpoints[j]);
  2343. float d_y=y_vec.dot(endpoints[j]);
  2344. float d_z=z_vec.dot(endpoints[j]);
  2345. if (j==0 || d_x<x_min)
  2346. x_min=d_x;
  2347. if (j==0 || d_x>x_max)
  2348. x_max=d_x;
  2349. if (j==0 || d_y<y_min)
  2350. y_min=d_y;
  2351. if (j==0 || d_y>y_max)
  2352. y_max=d_y;
  2353. if (j==0 || d_z<z_min)
  2354. z_min=d_z;
  2355. if (j==0 || d_z>z_max)
  2356. z_max=d_z;
  2357. }
  2358. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2359. Vector<Plane> light_frustum_planes;
  2360. light_frustum_planes.resize(6);
  2361. //right/left
  2362. light_frustum_planes[0]=Plane( x_vec, x_max );
  2363. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  2364. //top/bottom
  2365. light_frustum_planes[2]=Plane( y_vec, y_max );
  2366. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  2367. //near/far
  2368. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  2369. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  2370. //TODO@ add more actual frustum planes to minimize get
  2371. return light_frustum_planes;
  2372. }
  2373. void VisualServerRaster::_light_instance_update_pssm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2374. int splits = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  2375. float split_weight=rasterizer->light_directional_get_shadow_param(p_light->base_rid,LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT);
  2376. float distances[5];
  2377. float texsize=rasterizer->light_instance_get_shadow_size( p_light->light_info->instance );
  2378. // float cull_min=p_cull_range.min;
  2379. //float cull_max=p_cull_range.max;
  2380. float cull_min=p_camera->znear;
  2381. float cull_max=p_camera->zfar;
  2382. float max_dist = rasterizer->light_directional_get_shadow_param(p_light->base_rid,VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE);
  2383. if (max_dist>0.0)
  2384. cull_max=MIN(cull_max,max_dist);
  2385. for(int i = 0; i < splits; i++) {
  2386. float idm = i / (float)splits;
  2387. float lg = cull_min * Math::pow(cull_max/cull_min, idm);
  2388. float uniform = cull_min + (cull_max - cull_min) * idm;
  2389. distances[i] = lg * split_weight + uniform * (1.0 - split_weight);
  2390. }
  2391. distances[0]=cull_min;
  2392. distances[splits]=cull_max;
  2393. for (int i=0;i<splits;i++) {
  2394. // setup a camera matrix for that range!
  2395. CameraMatrix camera_matrix;
  2396. switch(p_camera->type) {
  2397. case Camera::ORTHOGONAL: {
  2398. camera_matrix.set_orthogonal(
  2399. p_camera->size,
  2400. viewport_rect.width / (float)viewport_rect.height,
  2401. distances[i],
  2402. distances[i+1],
  2403. p_camera->vaspect
  2404. );
  2405. } break;
  2406. case Camera::PERSPECTIVE: {
  2407. camera_matrix.set_perspective(
  2408. p_camera->fov,
  2409. viewport_rect.width / (float)viewport_rect.height,
  2410. distances[i],
  2411. distances[i+1],
  2412. p_camera->vaspect
  2413. );
  2414. } break;
  2415. }
  2416. //obtain the frustum endpoints
  2417. Vector3 endpoints[8]; // frustum plane endpoints
  2418. bool res = camera_matrix.get_endpoints(p_camera->transform,endpoints);
  2419. ERR_CONTINUE(!res);
  2420. // obtain the light frustm ranges (given endpoints)
  2421. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2422. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2423. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2424. //z_vec points agsint the camera, like in default opengl
  2425. float x_min,x_max;
  2426. float y_min,y_max;
  2427. float z_min,z_max;
  2428. float x_min_cam,x_max_cam;
  2429. float y_min_cam,y_max_cam;
  2430. float z_min_cam,z_max_cam;
  2431. //used for culling
  2432. for(int j=0;j<8;j++) {
  2433. float d_x=x_vec.dot(endpoints[j]);
  2434. float d_y=y_vec.dot(endpoints[j]);
  2435. float d_z=z_vec.dot(endpoints[j]);
  2436. if (j==0 || d_x<x_min)
  2437. x_min=d_x;
  2438. if (j==0 || d_x>x_max)
  2439. x_max=d_x;
  2440. if (j==0 || d_y<y_min)
  2441. y_min=d_y;
  2442. if (j==0 || d_y>y_max)
  2443. y_max=d_y;
  2444. if (j==0 || d_z<z_min)
  2445. z_min=d_z;
  2446. if (j==0 || d_z>z_max)
  2447. z_max=d_z;
  2448. }
  2449. {
  2450. //camera viewport stuff
  2451. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  2452. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  2453. Vector3 center;
  2454. for(int j=0;j<8;j++) {
  2455. center+=endpoints[j];
  2456. }
  2457. center/=8.0;
  2458. //center=x_vec*(x_max-x_min)*0.5 + y_vec*(y_max-y_min)*0.5 + z_vec*(z_max-z_min)*0.5;
  2459. float radius=0;
  2460. for(int j=0;j<8;j++) {
  2461. float d = center.distance_to(endpoints[j]);
  2462. if (d>radius)
  2463. radius=d;
  2464. }
  2465. radius *= texsize/(texsize-2.0); //add a texel by each side, so stepified texture will always fit
  2466. x_max_cam=x_vec.dot(center)+radius;
  2467. x_min_cam=x_vec.dot(center)-radius;
  2468. y_max_cam=y_vec.dot(center)+radius;
  2469. y_min_cam=y_vec.dot(center)-radius;
  2470. z_max_cam=z_vec.dot(center)+radius;
  2471. z_min_cam=z_vec.dot(center)-radius;
  2472. float unit = radius*2.0/texsize;
  2473. x_max_cam=Math::stepify(x_max_cam,unit);
  2474. x_min_cam=Math::stepify(x_min_cam,unit);
  2475. y_max_cam=Math::stepify(y_max_cam,unit);
  2476. y_min_cam=Math::stepify(y_min_cam,unit);
  2477. }
  2478. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2479. Vector<Plane> light_frustum_planes;
  2480. light_frustum_planes.resize(6);
  2481. //right/left
  2482. light_frustum_planes[0]=Plane( x_vec, x_max );
  2483. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  2484. //top/bottom
  2485. light_frustum_planes[2]=Plane( y_vec, y_max );
  2486. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  2487. //near/far
  2488. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  2489. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  2490. int caster_cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2491. // a pre pass will need to be needed to determine the actual z-near to be used
  2492. for(int j=0;j<caster_cull_count;j++) {
  2493. float min,max;
  2494. Instance *ins=instance_shadow_cull_result[j];
  2495. if (!ins->visible || !ins->cast_shadows)
  2496. continue;
  2497. ins->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max);
  2498. if (max>z_max)
  2499. z_max=max;
  2500. }
  2501. {
  2502. CameraMatrix ortho_camera;
  2503. real_t half_x = (x_max_cam-x_min_cam) * 0.5;
  2504. real_t half_y = (y_max_cam-y_min_cam) * 0.5;
  2505. ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) );
  2506. Transform ortho_transform;
  2507. ortho_transform.basis=p_light->data.transform.basis;
  2508. ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max;
  2509. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance, i, ortho_camera, ortho_transform,distances[i],distances[i+1] );
  2510. }
  2511. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  2512. for (int j=0;j<caster_cull_count;j++) {
  2513. Instance *instance = instance_shadow_cull_result[j];
  2514. if (!instance->visible || !instance->cast_shadows)
  2515. continue;
  2516. _instance_draw(instance);
  2517. }
  2518. rasterizer->end_shadow_map();
  2519. }
  2520. }
  2521. CameraMatrix _lispm_look( const Vector3 pos, const Vector3 dir, const Vector3 up) {
  2522. Vector3 dirN;
  2523. Vector3 upN;
  2524. Vector3 lftN;
  2525. lftN=dir.cross(up);
  2526. lftN.normalize();
  2527. upN=lftN.cross(dir);
  2528. upN.normalize();
  2529. dirN=dir.normalized();
  2530. CameraMatrix cmout;
  2531. float *output=&cmout.matrix[0][0];
  2532. output[ 0] = lftN[0];
  2533. output[ 1] = upN[0];
  2534. output[ 2] = -dirN[0];
  2535. output[ 3] = 0.0;
  2536. output[ 4] = lftN[1];
  2537. output[ 5] = upN[1];
  2538. output[ 6] = -dirN[1];
  2539. output[ 7] = 0.0;
  2540. output[ 8] = lftN[2];
  2541. output[ 9] = upN[2];
  2542. output[10] = -dirN[2];
  2543. output[11] = 0.0;
  2544. output[12] = -lftN.dot(pos);
  2545. output[13] = -upN.dot(pos);
  2546. output[14] = dirN.dot(pos);
  2547. output[15] = 1.0;
  2548. return cmout;
  2549. }
  2550. #if 1
  2551. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2552. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  2553. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  2554. float viewdot = light_vec.normalized().dot(view_vec.normalized());
  2555. float near_dist=1;
  2556. Vector<Plane> light_frustum_planes = _camera_generate_orthogonal_planes(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2557. int caster_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2558. // this could be faster by just getting supports from the AABBs..
  2559. // but, safer to do as the original implementation explains for now..
  2560. Vector<Vector3> caster_pointcloud;
  2561. caster_pointcloud.resize(caster_count*8);
  2562. int caster_pointcloud_size=0;
  2563. {
  2564. //fill pointcloud
  2565. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2566. for(int i=0;i<caster_count;i++) {
  2567. Instance *ins = instance_shadow_cull_result[i];
  2568. if (!ins->visible || !ins->cast_shadows)
  2569. continue;
  2570. for(int j=0;j<8;j++) {
  2571. Vector3 v = ins->aabb.get_endpoint(j);
  2572. v = ins->data.transform.xform(v);
  2573. caster_pointcloud_ptr[caster_pointcloud_size+j]=v;
  2574. }
  2575. caster_pointcloud_size+=8;
  2576. }
  2577. }
  2578. // now generate a pointcloud that contains the maximum bound (camera extruded by light)
  2579. Vector<Vector3> camera_pointcloud = _camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2580. int cpcsize=camera_pointcloud.size();
  2581. camera_pointcloud.resize( cpcsize*2 );
  2582. for(int i=0;i<cpcsize;i++) {
  2583. camera_pointcloud[i+cpcsize]=camera_pointcloud[i]-light_vec*1000;
  2584. }
  2585. // Vector<Vector3> frustum_points=_camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2586. // compute the "light-space" basis, using the algorithm described in the paper
  2587. // note: since bodyB is defined in eye space, all of these vectors should also be defined in eye space
  2588. Vector3 eye = p_camera->transform.origin;
  2589. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  2590. CameraMatrix light_space_basis = _lispm_look(eye,light_vec,up);
  2591. AABB light_space_aabb;
  2592. { //create an optimal AABB from both the camera pointcloud and the objects pointcloud
  2593. AABB light_space_pointcloud_aabb;
  2594. AABB light_space_camera_aabb;
  2595. //xform pointcloud
  2596. const Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2597. for(int i=0;i<caster_pointcloud_size;i++) {
  2598. Vector3 p = light_space_basis.xform(caster_pointcloud_ptr[i]);
  2599. if (i==0) {
  2600. light_space_pointcloud_aabb.pos=p;
  2601. } else {
  2602. light_space_pointcloud_aabb.expand_to(p);
  2603. }
  2604. }
  2605. for(int i=0;i<camera_pointcloud.size();i++) {
  2606. Vector3 p = light_space_basis.xform(camera_pointcloud[i]);
  2607. if (i==0) {
  2608. light_space_camera_aabb.pos=p;
  2609. } else {
  2610. light_space_camera_aabb.expand_to(p);
  2611. }
  2612. }
  2613. light_space_aabb=light_space_pointcloud_aabb.intersection(light_space_camera_aabb);
  2614. }
  2615. float lvdp = light_vec.dot(view_vec);
  2616. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  2617. //use the formulas of the paper to get n (and f)
  2618. float factor = 1.0/sin_gamma;
  2619. float z_n = factor*near_dist; //often 1
  2620. float d = Math::abs(light_space_aabb.size.y); //perspective transform depth //light space y extents
  2621. float z_f = z_n + d*sin_gamma;
  2622. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  2623. float f = n+d;
  2624. Vector3 pos = eye - up*(n-near_dist);
  2625. CameraMatrix light_space_basis2 = _lispm_look(pos,light_vec,up);
  2626. //Transform light_space_basis2;
  2627. //light_space_basis2.set_look_at(pos,light_vec-pos,up);
  2628. //light_space_basis2.affine_invert();
  2629. //one possibility for a simple perspective transformation matrix
  2630. //with the two parameters n(near) and f(far) in y direction
  2631. CameraMatrix lisp_matrix;
  2632. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  2633. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  2634. lisp_matrix.matrix[1][3]=1;
  2635. lisp_matrix.matrix[3][3]=0;
  2636. CameraMatrix projection = lisp_matrix * light_space_basis2;
  2637. //CameraMatrix projection = light_space_basis2 * lisp_matrix;
  2638. AABB proj_space_aabb;
  2639. float max_d,min_d;
  2640. {
  2641. AABB proj_space_pointcloud_aabb;
  2642. AABB proj_space_camera_aabb;
  2643. //xform pointcloud
  2644. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2645. for(int i=0;i<caster_pointcloud_size;i++) {
  2646. Vector3 p = projection.xform(caster_pointcloud_ptr[i]);
  2647. if (i==0) {
  2648. proj_space_pointcloud_aabb.pos=p;
  2649. } else {
  2650. proj_space_pointcloud_aabb.expand_to(p);
  2651. }
  2652. }
  2653. for(int i=0;i<camera_pointcloud.size();i++) {
  2654. Vector3 p = projection.xform(camera_pointcloud[i]);
  2655. if (i==0) {
  2656. proj_space_camera_aabb.pos=p;
  2657. } else {
  2658. proj_space_camera_aabb.expand_to(p);
  2659. }
  2660. }
  2661. //proj_space_aabb=proj_space_pointcloud_aabb.intersection_with(proj_space_camera_aabb);
  2662. proj_space_aabb=proj_space_pointcloud_aabb;
  2663. }
  2664. projection.scale_translate_to_fit(proj_space_aabb);
  2665. projection=projection * lisp_matrix;
  2666. CameraMatrix scale;
  2667. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  2668. projection=scale * projection;
  2669. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection , light_space_basis2.inverse() );
  2670. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  2671. for(int i=0;i<caster_count;i++) {
  2672. Instance *instance = instance_shadow_cull_result[i];
  2673. if (!instance->visible || !instance->cast_shadows)
  2674. continue;
  2675. _instance_draw(instance);
  2676. }
  2677. rasterizer->end_shadow_map();
  2678. }
  2679. #else
  2680. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2681. /* STEP 1: GENERATE LIGHT TRANSFORM */
  2682. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  2683. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  2684. float viewdot = Math::absf(light_vec.dot(view_vec));
  2685. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  2686. Transform light_transform;
  2687. light_transform.set_look_at(Vector3(),light_vec,up);
  2688. /* STEP 2: GENERATE WORDLSPACE PLANES AND VECTORS*/
  2689. float range_min=0.01; //p_cull_range.min
  2690. float range_max=20;//p_cull_range.max;
  2691. Vector<Vector3> camera_endpoints=_camera_generate_endpoints(p_light,p_camera,range_min,range_max); // frustum plane endpoints
  2692. ERR_FAIL_COND(camera_endpoints.empty());
  2693. // obtain the light frustm ranges (given endpoints)
  2694. Vector3 light_x_vec=light_transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2695. Vector3 light_y_vec=light_transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2696. Vector3 light_z_vec=light_transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2697. Vector3 light_axis_max;
  2698. Vector3 light_axis_min;
  2699. for(int j=0;j<8;j++) {
  2700. float d_x=light_x_vec.dot(camera_endpoints[j]);
  2701. float d_y=light_y_vec.dot(camera_endpoints[j]);
  2702. float d_z=light_z_vec.dot(camera_endpoints[j]);
  2703. if (j==0 || d_x<light_axis_min.x)
  2704. light_axis_min.x=d_x;
  2705. if (j==0 || d_x>light_axis_max.x)
  2706. light_axis_max.x=d_x;
  2707. if (j==0 || d_y<light_axis_min.y)
  2708. light_axis_min.y=d_y;
  2709. if (j==0 || d_y>light_axis_max.y)
  2710. light_axis_max.y=d_y;
  2711. if (j==0 || d_z<light_axis_min.z)
  2712. light_axis_min.z=d_z;
  2713. if (j==0 || d_z>light_axis_max.z)
  2714. light_axis_max.z=d_z;
  2715. }
  2716. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2717. Vector<Plane> light_cull_planes;
  2718. light_cull_planes.resize(6);
  2719. //right/left
  2720. light_cull_planes[0]=Plane( light_x_vec, light_axis_max.x );
  2721. light_cull_planes[1]=Plane( -light_x_vec, -light_axis_min.x );
  2722. //top/bottom
  2723. light_cull_planes[2]=Plane( light_y_vec, light_axis_max.y );
  2724. light_cull_planes[3]=Plane( -light_y_vec, -light_axis_min.y );
  2725. //near/far
  2726. light_cull_planes[4]=Plane( light_z_vec, light_axis_max.z+1e6 );
  2727. light_cull_planes[5]=Plane( -light_z_vec, -light_axis_min.z ); // z_min is ok, since casters further than far-light plane are not needed
  2728. /* STEP 3: CULL CASTERS */
  2729. int caster_count = p_scenario->octree.cull_convex(light_cull_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2730. /* STEP 4: ADJUST FAR Z PLANE */
  2731. float caster_max_z=1e-1;
  2732. for(int i=0;i<caster_count;i++) {
  2733. Instance *ins=instance_shadow_cull_result[i];
  2734. if (!ins->visible || !ins->cast_shadows)
  2735. continue;
  2736. //@TODO optimize using support mapping
  2737. for(int j=0;j<8;j++) {
  2738. Vector3 v=ins->data.transform.xform(ins->aabb.get_endpoint(j));
  2739. float d = light_z_vec.dot(v);
  2740. if (d>caster_max_z)
  2741. caster_max_z=d;
  2742. }
  2743. }
  2744. float expand = caster_max_z-light_axis_max.z;
  2745. if (expand<0)
  2746. expand=0;
  2747. light_axis_max.z=MAX(caster_max_z,light_axis_max.z);
  2748. /* STEP 5: CREATE ORTHOGONAL PROJECTION */
  2749. CameraMatrix light_projection;
  2750. real_t half_x = (light_axis_max.x-light_axis_min.x) * 0.5;
  2751. real_t half_y = (light_axis_max.y-light_axis_min.y) * 0.5;
  2752. light_projection.set_orthogonal( -half_x, half_x,half_y, -half_y, 0, (light_axis_max.z-light_axis_min.z) );
  2753. light_transform.origin=light_x_vec*(light_axis_min.x+half_x)+light_y_vec*(light_axis_min.y+half_y)+light_z_vec*light_axis_max.z;
  2754. if (/*false &&*/ viewdot<0.96) {
  2755. float lvdp = light_vec.dot(view_vec);
  2756. float near_dist=1.0;
  2757. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  2758. //use the formulas of the paper to get n (and f)
  2759. float factor = 1.0/sin_gamma;
  2760. float z_n = factor*near_dist; //often 1
  2761. float d = Math::abs(light_axis_max.y-light_axis_min.y); //perspective transform depth //light space y extents
  2762. float z_f = z_n + d*sin_gamma;
  2763. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  2764. float f = n+d;
  2765. CameraMatrix lisp_matrix;
  2766. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  2767. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  2768. lisp_matrix.matrix[1][3]=1;
  2769. lisp_matrix.matrix[3][3]=0;
  2770. Vector3 pos = p_camera->transform.origin - up*(n-near_dist);
  2771. CameraMatrix world2light = _lispm_look(pos,light_vec,up);
  2772. CameraMatrix projection = lisp_matrix * world2light;
  2773. AABB projection_bounds;
  2774. for(int i=0;i<camera_endpoints.size();i++) {
  2775. Vector3 p=camera_endpoints[i];
  2776. if (i==0)
  2777. projection_bounds.pos=projection.xform(p);
  2778. else
  2779. projection_bounds.expand_to(projection.xform(p));
  2780. projection_bounds.expand_to(projection.xform(p+light_vec*-expand));
  2781. }
  2782. CameraMatrix scaletrans;
  2783. scaletrans.scale_translate_to_fit(projection_bounds);
  2784. projection=scaletrans * lisp_matrix;
  2785. CameraMatrix scale;
  2786. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  2787. projection=scale * projection;
  2788. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection, world2light.inverse(), viewdot);
  2789. } else {
  2790. //orthogonal
  2791. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, light_projection , light_transform, viewdot);
  2792. }
  2793. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  2794. for(int i=0;i<caster_count;i++) {
  2795. Instance *instance = instance_shadow_cull_result[i];
  2796. if (!instance->visible || !instance->cast_shadows)
  2797. continue;
  2798. _instance_draw(instance);
  2799. }
  2800. rasterizer->end_shadow_map();
  2801. }
  2802. #endif
  2803. void VisualServerRaster::_light_instance_update_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2804. if (!rasterizer->shadow_allocate_near( p_light->light_info->instance ))
  2805. return; // shadow could not be updated
  2806. /* VisualServerRaster supports for many shadow techniques, using the one the rasterizer requests */
  2807. Rasterizer::ShadowType shadow_type = rasterizer->light_instance_get_shadow_type(p_light->light_info->instance);
  2808. switch(shadow_type) {
  2809. case Rasterizer::SHADOW_SIMPLE: {
  2810. /* SPOT SHADOW */
  2811. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  2812. //using this one ensures that raster deferred will have it
  2813. float far = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  2814. float angle = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_SPOT_ANGLE );
  2815. CameraMatrix cm;
  2816. cm.set_perspective( angle*2.0, 1.0, 0.001, far );
  2817. Vector<Plane> planes = cm.get_projection_planes(p_light->data.transform);
  2818. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2819. for (int i=0;i<cull_count;i++) {
  2820. Instance *instance = instance_shadow_cull_result[i];
  2821. if (!instance->visible || !instance->cast_shadows)
  2822. continue;
  2823. _instance_draw(instance);
  2824. }
  2825. rasterizer->end_shadow_map();
  2826. } break;
  2827. case Rasterizer::SHADOW_DUAL_PARABOLOID: {
  2828. /* OMNI SHADOW */
  2829. int passes = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  2830. if (passes==2) {
  2831. for(int i=0;i<2;i++) {
  2832. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  2833. //using this one ensures that raster deferred will have it
  2834. float radius = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  2835. float z =i==0?-1:1;
  2836. Vector<Plane> planes;
  2837. planes.resize(5);
  2838. planes[0]=p_light->data.transform.xform(Plane(Vector3(0,0,z),radius));
  2839. planes[1]=p_light->data.transform.xform(Plane(Vector3(1,0,z).normalized(),radius));
  2840. planes[2]=p_light->data.transform.xform(Plane(Vector3(-1,0,z).normalized(),radius));
  2841. planes[3]=p_light->data.transform.xform(Plane(Vector3(0,1,z).normalized(),radius));
  2842. planes[4]=p_light->data.transform.xform(Plane(Vector3(0,-1,z).normalized(),radius));
  2843. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2844. for (int j=0;j<cull_count;j++) {
  2845. Instance *instance = instance_shadow_cull_result[j];
  2846. if (!instance->visible || !instance->cast_shadows)
  2847. continue;
  2848. _instance_draw(instance);
  2849. }
  2850. rasterizer->end_shadow_map();
  2851. }
  2852. } else if (passes==1) {
  2853. //one go
  2854. }
  2855. } break;
  2856. case Rasterizer::SHADOW_CUBE: {
  2857. // todo
  2858. } break;
  2859. case Rasterizer::SHADOW_ORTHOGONAL: {
  2860. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  2861. } break;
  2862. case Rasterizer::SHADOW_PSSM: {
  2863. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  2864. } break;
  2865. case Rasterizer::SHADOW_PSM: {
  2866. _light_instance_update_lispsm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  2867. // todo
  2868. } break;
  2869. default: {}
  2870. }
  2871. }
  2872. void VisualServerRaster::_portal_disconnect(Instance *p_portal,bool p_cleanup) {
  2873. if (p_portal->portal_info->connected) {
  2874. //disconnect first
  2875. p_portal->portal_info->connected->portal_info->connected=NULL;
  2876. p_portal->portal_info->connected=NULL;
  2877. }
  2878. if (p_portal->room && p_portal->room->room) {
  2879. if (p_cleanup) {
  2880. p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  2881. //p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  2882. } else {
  2883. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  2884. }
  2885. }
  2886. }
  2887. void VisualServerRaster::_instance_validate_autorooms(Instance *p_geometry) {
  2888. if (p_geometry->auto_rooms.size()==0)
  2889. return;
  2890. p_geometry->valid_auto_rooms.clear();
  2891. int point_count = aabb_random_points.size();
  2892. const Vector3 * src_points = &aabb_random_points[0];
  2893. for(Set<Instance*>::Element *E=p_geometry->valid_auto_rooms.front();E;E=E->next()) {
  2894. Instance *room = E->get();
  2895. Vector3 *dst_points=&transformed_aabb_random_points[0];
  2896. //generate points
  2897. for(int i=0;i<point_count;i++) {
  2898. dst_points[i] = room->room_info->affine_inverse.xform(p_geometry->data.transform.xform((src_points[i]*p_geometry->transformed_aabb.size)+p_geometry->transformed_aabb.pos));
  2899. }
  2900. int pass = room->room_info->room->bounds.get_points_inside(dst_points,point_count);
  2901. float ratio = (float)pass / point_count;
  2902. if (ratio>0.5) // should make some constant
  2903. p_geometry->valid_auto_rooms.insert(room);
  2904. }
  2905. }
  2906. void VisualServerRaster::_portal_attempt_connect(Instance *p_portal) {
  2907. _portal_disconnect(p_portal);
  2908. Vector3 A_norm = p_portal->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  2909. Plane A_plane( p_portal->data.transform.origin, A_norm );
  2910. float A_surface = p_portal->portal_info->portal->bounds.get_area();
  2911. if (A_surface==0)
  2912. return; //wtf
  2913. Instance *found=NULL;
  2914. Transform affine_inverse = p_portal->data.transform.affine_inverse();
  2915. for(Set<Instance*>::Element *E=p_portal->portal_info->candidate_set.front();E;E=E->next()) {
  2916. Instance *B = E->get();
  2917. if (B->portal_info->connected)
  2918. continue; // in use
  2919. Vector3 B_norm = B->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  2920. // check that they are in front of another
  2921. float dot = A_norm.dot(-B_norm);
  2922. if (dot<0.707) // 45 degrees, TODO unharcode this
  2923. continue;
  2924. // check the max distance to the other portal
  2925. bool valid=true;
  2926. Rect2 local_bounds;
  2927. for(int i=0;i<B->portal_info->portal->shape.size();i++) {
  2928. Point2 point2 = B->portal_info->portal->shape[i];
  2929. Vector3 point = B->data.transform.xform( Vector3( point2.x, point2.y, 0 ) );
  2930. float dist = Math::abs(A_plane.distance_to(point));
  2931. if (
  2932. dist>p_portal->portal_info->portal->connect_range ||
  2933. dist>B->portal_info->portal->connect_range ) {
  2934. valid=false;
  2935. break;
  2936. }
  2937. Vector3 point_local = affine_inverse.xform(A_plane.project(point));
  2938. point2 = Point2(point_local.x,point_local.y);
  2939. if (i==0)
  2940. local_bounds.pos=point2;
  2941. else
  2942. local_bounds.expand_to(point2);
  2943. }
  2944. if (!valid)
  2945. continue;
  2946. float B_surface = B->portal_info->portal->bounds.get_area();
  2947. if (B_surface==0)
  2948. continue; //wtf
  2949. float clip_area = p_portal->portal_info->portal->bounds.clip(local_bounds).get_area();
  2950. //check that most of the area is shared
  2951. if ( (clip_area/A_surface) < 0.5 || (clip_area/B_surface) < 0.5) // TODO change for something else
  2952. continue;
  2953. found=B;
  2954. break;
  2955. }
  2956. if (!found) {
  2957. if (p_portal->room && p_portal->room->room) {
  2958. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  2959. }
  2960. return;
  2961. }
  2962. p_portal->portal_info->connected=found;
  2963. found->portal_info->connected=p_portal;
  2964. }
  2965. void* VisualServerRaster::instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) {
  2966. VisualServerRaster *self = (VisualServerRaster*)p_self;
  2967. Instance *A = p_A;
  2968. Instance *B = p_B;
  2969. if (A->base_type==INSTANCE_PORTAL) {
  2970. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  2971. A->portal_info->candidate_set.insert(B);
  2972. B->portal_info->candidate_set.insert(A);
  2973. self->_portal_attempt_connect(A);
  2974. //attempt to conncet portal A (will go through B anyway)
  2975. //this is a little hackish, but works fine in practice
  2976. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  2977. if (B->base_type==INSTANCE_ROOM)
  2978. SWAP(A,B);
  2979. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  2980. B->auto_rooms.insert(A);
  2981. A->room_info->owned_autoroom_geometry.insert(B);
  2982. self->_instance_validate_autorooms(B);
  2983. } else {
  2984. if (B->base_type==INSTANCE_LIGHT) {
  2985. SWAP(A,B);
  2986. } else if (A->base_type!=INSTANCE_LIGHT) {
  2987. return NULL;
  2988. }
  2989. A->light_info->affected.insert(B);
  2990. B->lights.insert(A);
  2991. B->light_cache_dirty=true;
  2992. }
  2993. return NULL;
  2994. }
  2995. void VisualServerRaster::instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void*) {
  2996. VisualServerRaster *self = (VisualServerRaster*)p_self;
  2997. Instance *A = p_A;
  2998. Instance *B = p_B;
  2999. if (A->base_type==INSTANCE_PORTAL) {
  3000. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  3001. A->portal_info->candidate_set.erase(B);
  3002. B->portal_info->candidate_set.erase(A);
  3003. //after disconnecting them, see if they can connect again
  3004. self->_portal_attempt_connect(A);
  3005. self->_portal_attempt_connect(B);
  3006. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3007. if (B->base_type==INSTANCE_ROOM)
  3008. SWAP(A,B);
  3009. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  3010. B->auto_rooms.erase(A);
  3011. B->valid_auto_rooms.erase(A);
  3012. A->room_info->owned_autoroom_geometry.erase(B);
  3013. }else {
  3014. if (B->base_type==INSTANCE_LIGHT) {
  3015. SWAP(A,B);
  3016. } else if (A->base_type!=INSTANCE_LIGHT) {
  3017. return;
  3018. }
  3019. A->light_info->affected.erase(B);
  3020. B->lights.erase(A);
  3021. B->light_cache_dirty=true;
  3022. }
  3023. }
  3024. bool VisualServerRaster::_test_portal_cull(Camera *p_camera, Instance *p_from_portal, Instance *p_to_portal) {
  3025. int src_point_count=p_from_portal->portal_info->transformed_point_cache.size();
  3026. int dst_point_count=p_to_portal->portal_info->transformed_point_cache.size();
  3027. if (src_point_count<2 || dst_point_count<2)
  3028. return false;
  3029. const Vector3 *src_points=&p_from_portal->portal_info->transformed_point_cache[0];
  3030. const Vector3 *dst_points=&p_to_portal->portal_info->transformed_point_cache[0];
  3031. bool outside=false;
  3032. bool clockwise = !p_from_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin);
  3033. for(int i=0;i<src_point_count;i++) {
  3034. const Vector3& point_prev = src_points[i?(i-1):(src_point_count-1)];
  3035. const Vector3& point = src_points[i];
  3036. Plane p = clockwise?Plane(p_camera->transform.origin,point,point_prev):Plane(p_camera->transform.origin,point_prev,point);
  3037. bool all_over=true;
  3038. for(int j=0;j<dst_point_count;j++) {
  3039. if (!p.is_point_over(dst_points[j])) {
  3040. all_over=false;
  3041. break;
  3042. }
  3043. }
  3044. if (all_over) {
  3045. outside=true;
  3046. break;
  3047. }
  3048. }
  3049. return !outside;
  3050. }
  3051. void VisualServerRaster::_cull_portal(Camera *p_camera, Instance *p_portal,Instance *p_from_portal) {
  3052. ERR_FAIL_COND(!p_portal->scenario); //scenario outside
  3053. Instance *portal = p_portal;
  3054. if (!portal->room) {
  3055. return; //portals need all to belong to a room, it may be unconfigured yet
  3056. } else if (portal->last_render_pass!=render_pass) {
  3057. return; //invalid portal, ignore
  3058. } else if (portal->portal_info->last_visited_pass==render_pass) {
  3059. return; //portal already visited
  3060. } else if (portal==p_from_portal) {
  3061. return; // came from this portal, don't even bother testing
  3062. }
  3063. /* TEST DISABLE DISTANCE */
  3064. float disable_distance = p_portal->portal_info->portal->disable_distance;
  3065. if (disable_distance) {
  3066. //has disable distance..
  3067. float distance = p_camera->transform.origin.distance_to(portal->data.transform.origin);
  3068. if (disable_distance < distance) {
  3069. return;
  3070. }
  3071. }
  3072. /* TEST PORTAL NOT FACING OPTIMIZATION */
  3073. if (p_portal->portal_info->connected) {
  3074. //connected portal means, it must face against the camera to be seen
  3075. if (p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3076. return;
  3077. }
  3078. } else {
  3079. //disconencted portals (go from room to parent room or exterior) must face towards the canera
  3080. if (!p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3081. return;
  3082. }
  3083. }
  3084. if (p_from_portal && !_test_portal_cull(p_camera, p_from_portal, portal)) {
  3085. return; // portal not visible (culled)
  3086. }
  3087. portal->portal_info->last_visited_pass=render_pass;
  3088. if (portal->portal_info->connected) {
  3089. //interior<->interior portal
  3090. Instance *to_room = portal->portal_info->connected->room;
  3091. if (!to_room) {
  3092. return; //wtf.. oh well, connected to a roomless (invalid) portal
  3093. }
  3094. _cull_room(p_camera, to_room, portal->portal_info->connected);
  3095. } else {
  3096. //to exterior/to parent roomportal
  3097. Instance *parent_room = portal->room->room;
  3098. _cull_room(p_camera, parent_room, portal);
  3099. }
  3100. }
  3101. void VisualServerRaster::_cull_room(Camera *p_camera, Instance *p_room,Instance *p_from_portal) {
  3102. if (p_room==NULL) {
  3103. //exterior
  3104. exterior_visited=true;
  3105. for(int i=0;i<exterior_portal_cull_count;i++) {
  3106. _cull_portal(p_camera, exterior_portal_cull_result[i],p_from_portal);
  3107. }
  3108. } else {
  3109. ERR_FAIL_COND(!p_room->scenario);
  3110. if (p_room->last_render_pass!=render_pass)
  3111. return; //this room is invalid
  3112. //interior
  3113. //first of all, validate the room
  3114. p_room->room_info->last_visited_pass=render_pass;
  3115. //see about going around portals
  3116. if (!p_room->room_info->room->occlude_exterior)
  3117. exterior_visited=true;
  3118. for(List<Instance*>::Element * E=p_room->room_info->owned_portal_instances.front();E;E=E->next()) {
  3119. _cull_portal(p_camera, E->get(),p_from_portal);
  3120. }
  3121. for(Set<Instance*>::Element * E=p_room->room_info->disconnected_child_portals.front();E;E=E->next()) {
  3122. _cull_portal(p_camera, E->get(),p_from_portal);
  3123. }
  3124. }
  3125. }
  3126. void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  3127. uint64_t t = OS::get_singleton()->get_ticks_usec();
  3128. render_pass++;
  3129. uint32_t camera_layer_mask=p_camera->visible_layers;
  3130. /* STEP 1 - SETUP CAMERA */
  3131. CameraMatrix camera_matrix;
  3132. switch(p_camera->type) {
  3133. case Camera::ORTHOGONAL: {
  3134. camera_matrix.set_orthogonal(
  3135. p_camera->size,
  3136. viewport_rect.width / (float)viewport_rect.height,
  3137. p_camera->znear,
  3138. p_camera->zfar,
  3139. p_camera->vaspect
  3140. );
  3141. } break;
  3142. case Camera::PERSPECTIVE: {
  3143. camera_matrix.set_perspective(
  3144. p_camera->fov,
  3145. viewport_rect.width / (float)viewport_rect.height,
  3146. p_camera->znear,
  3147. p_camera->zfar,
  3148. p_camera->vaspect
  3149. );
  3150. } break;
  3151. }
  3152. rasterizer->set_camera(p_camera->transform, camera_matrix);
  3153. Vector<Plane> planes = camera_matrix.get_projection_planes(p_camera->transform);
  3154. CullRange cull_range; // cull range is used for PSSM, and having an idea of the rendering depth
  3155. cull_range.nearp=Plane(p_camera->transform.origin,-p_camera->transform.basis.get_axis(2).normalized());
  3156. cull_range.z_near=camera_matrix.get_z_near();
  3157. cull_range.z_far=camera_matrix.get_z_far();
  3158. cull_range.min=cull_range.z_far;
  3159. cull_range.max=cull_range.z_near;
  3160. /* STEP 2 - CULL */
  3161. int cull_count = p_scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL);
  3162. light_cull_count=0;
  3163. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  3164. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  3165. // print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  3166. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  3167. */
  3168. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  3169. // compute portals
  3170. exterior_visited=false;
  3171. exterior_portal_cull_count=0;
  3172. if (room_cull_enabled) {
  3173. for(int i=0;i<cull_count;i++) {
  3174. Instance *ins = instance_cull_result[i];
  3175. ins->last_render_pass=render_pass;
  3176. if (ins->base_type!=INSTANCE_PORTAL)
  3177. continue;
  3178. if (ins->room)
  3179. continue;
  3180. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  3181. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  3182. }
  3183. room_cull_count = p_scenario->octree.cull_point(p_camera->transform.origin,room_cull_result,MAX_ROOM_CULL,NULL,(1<<INSTANCE_ROOM)|(1<<INSTANCE_PORTAL));
  3184. Set<Instance*> current_rooms;
  3185. Set<Instance*> portal_rooms;
  3186. //add to set
  3187. for(int i=0;i<room_cull_count;i++) {
  3188. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  3189. current_rooms.insert(room_cull_result[i]);
  3190. }
  3191. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  3192. //assume inside that room if also inside the portal..
  3193. if (room_cull_result[i]->room) {
  3194. portal_rooms.insert(room_cull_result[i]->room);
  3195. }
  3196. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  3197. room_cull_count--;
  3198. i--;
  3199. }
  3200. }
  3201. //remove from set if it has a parent room or BSP doesn't contain
  3202. for(int i=0;i<room_cull_count;i++) {
  3203. Instance *r = room_cull_result[i];
  3204. //check inside BSP
  3205. Vector3 room_local_point = r->room_info->affine_inverse.xform( p_camera->transform.origin );
  3206. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  3207. current_rooms.erase(r);
  3208. continue;
  3209. }
  3210. //check parent
  3211. while (r->room) {// has parent room
  3212. current_rooms.erase(r);
  3213. r=r->room;
  3214. }
  3215. }
  3216. if (current_rooms.size()) {
  3217. //camera is inside a room
  3218. // go through rooms
  3219. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  3220. _cull_room(p_camera,E->get());
  3221. }
  3222. } else {
  3223. //start from exterior
  3224. _cull_room(p_camera,NULL);
  3225. }
  3226. }
  3227. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  3228. for(int i=0;i<cull_count;i++) {
  3229. Instance *ins = instance_cull_result[i];
  3230. bool keep=false;
  3231. if ((camera_layer_mask&ins->layer_mask)==0) {
  3232. //failure
  3233. } else if (ins->base_type==INSTANCE_LIGHT) {
  3234. if (light_cull_count<MAX_LIGHTS_CULLED) {
  3235. light_cull_result[light_cull_count++]=ins;
  3236. // rasterizer->light_instance_set_active_hint(ins->light_info->instance);
  3237. {
  3238. //compute distance to camera using aabb support
  3239. Vector3 n = ins->data.transform.basis.xform_inv(cull_range.nearp.normal).normalized();
  3240. Vector3 s = ins->data.transform.xform(ins->aabb.get_support(n));
  3241. ins->light_info->dtc=cull_range.nearp.distance_to(s);
  3242. }
  3243. }
  3244. } else if ((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK && ins->visible) {
  3245. bool discarded=false;
  3246. if (ins->draw_range_end>0) {
  3247. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  3248. if (d<0)
  3249. d=0;
  3250. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  3251. }
  3252. if (!discarded) {
  3253. // test if this geometry should be visible
  3254. if (room_cull_enabled) {
  3255. if (ins->visible_in_all_rooms) {
  3256. keep=true;
  3257. } else if (ins->room) {
  3258. if (ins->room->room_info->last_visited_pass==render_pass)
  3259. keep=true;
  3260. } else if (ins->auto_rooms.size()) {
  3261. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  3262. if (E->get()->room_info->last_visited_pass==render_pass) {
  3263. keep=true;
  3264. break;
  3265. }
  3266. }
  3267. } else if(exterior_visited)
  3268. keep=true;
  3269. } else {
  3270. keep=true;
  3271. }
  3272. }
  3273. if (keep) {
  3274. // update cull range
  3275. float min,max;
  3276. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  3277. if (min<cull_range.min)
  3278. cull_range.min=min;
  3279. if (max>cull_range.max)
  3280. cull_range.max=max;
  3281. }
  3282. }
  3283. if (!keep) {
  3284. // remove, no reason to keep
  3285. cull_count--;
  3286. SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] );
  3287. i--;
  3288. ins->last_render_pass=0; // make invalid
  3289. } else {
  3290. ins->last_render_pass=render_pass;
  3291. }
  3292. }
  3293. if (cull_range.max > cull_range.z_far )
  3294. cull_range.max=cull_range.z_far;
  3295. if (cull_range.min < cull_range.z_near )
  3296. cull_range.min=cull_range.z_near;
  3297. /* STEP 5 - PROCESS LIGHTS */
  3298. rasterizer->shadow_clear_near(); //clear near shadows, will be recreated
  3299. // directional lights
  3300. {
  3301. List<RID>::Element *E=p_scenario->directional_lights.front();
  3302. while(E) {
  3303. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  3304. if (rasterizer->light_has_shadow(light->base_rid)) {
  3305. //rasterizer->light_instance_set_active_hint(light->light_info->instance);
  3306. _light_instance_update_shadow(light,p_scenario,p_camera,cull_range);
  3307. }
  3308. E=E->next();
  3309. }
  3310. }
  3311. //discard lights not affecting anything (useful for deferred rendering, shadowmaps, etc)
  3312. for (int i=0;i<light_cull_count;i++) {
  3313. Instance *ins = light_cull_result[i];
  3314. if (light_discard_enabled) {
  3315. //see if the light should be pre discarded because no one is seeing it
  3316. //this test may seem expensive, but in reality, it shouldn't be
  3317. //because of early out condition. It will only go through everything
  3318. //if it's being discarded.
  3319. bool valid=false;
  3320. InstanceSet::Element *E =ins->light_info->affected.front();
  3321. while(E) {
  3322. if (E->get()->last_render_pass==render_pass) {
  3323. valid=true; // early out.
  3324. break;
  3325. }
  3326. E=E->next();
  3327. }
  3328. if (!valid) {
  3329. light_cull_count--;
  3330. SWAP( light_cull_result[i], light_cull_result[ light_cull_count ] );
  3331. i--;
  3332. }
  3333. }
  3334. }
  3335. {
  3336. //assign shadows by distance to camera
  3337. SortArray<Instance*,_InstanceLightsort> sorter;
  3338. sorter.sort(light_cull_result,light_cull_count);
  3339. for (int i=0;i<light_cull_count;i++) {
  3340. Instance *ins = light_cull_result[i];
  3341. if (!rasterizer->light_has_shadow(ins->base_rid) || !shadows_enabled)
  3342. continue;
  3343. /* for far shadows?
  3344. if (ins->version == ins->light_info->last_version && rasterizer->light_instance_has_far_shadow(ins->light_info->instance))
  3345. continue; // didn't change
  3346. */
  3347. _light_instance_update_shadow(ins,p_scenario,p_camera,cull_range);
  3348. ins->light_info->last_version=ins->version;
  3349. }
  3350. }
  3351. /* STEP 6 - PROCESS GEOMETRY AND DRAW SCENE*/
  3352. RID environment;
  3353. if (p_camera->env.is_valid()) //camera has more environment priority
  3354. environment=p_camera->env;
  3355. else
  3356. environment=p_scenario->environment;
  3357. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  3358. rasterizer->set_viewport(viewport_rect);
  3359. // add lights
  3360. {
  3361. List<RID>::Element *E=p_scenario->directional_lights.front();
  3362. for(;E;E=E->next()) {
  3363. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  3364. ERR_CONTINUE(!light);
  3365. rasterizer->add_light(light->light_info->instance);
  3366. light->light_info->last_add_pass=render_pass;
  3367. }
  3368. for (int i=0;i<light_cull_count;i++) {
  3369. Instance *ins = light_cull_result[i];
  3370. rasterizer->add_light(ins->light_info->instance);
  3371. ins->light_info->last_add_pass=render_pass;
  3372. }
  3373. }
  3374. // add geometry
  3375. for(int i=0;i<cull_count;i++) {
  3376. Instance *ins = instance_cull_result[i];
  3377. ERR_CONTINUE(!((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK));
  3378. _instance_draw(ins);
  3379. }
  3380. rasterizer->end_scene();
  3381. }
  3382. void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Matrix32& p_transform,const Rect2& p_clip_rect, float p_opacity) {
  3383. CanvasItem *ci = p_canvas_item;
  3384. if (!ci->visible)
  3385. return;
  3386. if (p_opacity<0.007)
  3387. return;
  3388. Rect2 rect = ci->get_rect();
  3389. Matrix32 xform = p_transform * ci->xform;
  3390. Rect2 global_rect = xform.xform(rect);
  3391. global_rect.pos+=p_clip_rect.pos;
  3392. if (global_rect.intersects(p_clip_rect) && ci->viewport.is_valid() && viewport_owner.owns(ci->viewport)) {
  3393. Viewport *vp = viewport_owner.get(ci->viewport);
  3394. Point2i from = xform.get_origin() + Point2(viewport_rect.x,viewport_rect.y);
  3395. Point2i size = rect.size;
  3396. size.x *= xform[0].length();
  3397. size.y *= xform[1].length();
  3398. _draw_viewport(vp,
  3399. from.x,
  3400. from.y,
  3401. size.x,
  3402. size.y);
  3403. rasterizer->canvas_begin();
  3404. }
  3405. int s = ci->commands.size();
  3406. bool reclip=false;
  3407. float opacity = ci->opacity * p_opacity;
  3408. #ifndef ONTOP_DISABLED
  3409. CanvasItem **child_items = ci->child_items.ptr();
  3410. int child_item_count=ci->child_items.size();
  3411. int top_item_count=0;
  3412. CanvasItem **top_items=(CanvasItem**)alloca(child_item_count*sizeof(CanvasItem*));
  3413. if (ci->clip) {
  3414. rasterizer->canvas_set_clip(true,global_rect);
  3415. canvas_clip=global_rect;
  3416. }
  3417. for(int i=0;i<child_item_count;i++) {
  3418. if (child_items[i]->ontop)
  3419. top_items[top_item_count++]=child_items[i];
  3420. else {
  3421. _render_canvas_item(child_items[i],xform,p_clip_rect,opacity);
  3422. }
  3423. }
  3424. #endif
  3425. if (s!=0) {
  3426. //Rect2 rect( ci->rect.pos + p_ofs, ci->rect.size);
  3427. if (p_clip_rect.intersects(global_rect)) {
  3428. rasterizer->canvas_begin_rect(xform);
  3429. rasterizer->canvas_set_opacity( opacity * ci->self_opacity );
  3430. rasterizer->canvas_set_blend_mode( ci->blend_mode );
  3431. CanvasItem::Command **commands = &ci->commands[0];
  3432. for (int i=0;i<s;i++) {
  3433. CanvasItem::Command *c=commands[i];
  3434. switch(c->type) {
  3435. case CanvasItem::Command::TYPE_LINE: {
  3436. CanvasItem::CommandLine* line = static_cast<CanvasItem::CommandLine*>(c);
  3437. rasterizer->canvas_draw_line(line->from,line->to,line->color,line->width);
  3438. } break;
  3439. case CanvasItem::Command::TYPE_RECT: {
  3440. CanvasItem::CommandRect* rect = static_cast<CanvasItem::CommandRect*>(c);
  3441. // rasterizer->canvas_draw_rect(rect->rect,rect->region,rect->source,rect->flags&CanvasItem::CommandRect::FLAG_TILE,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V,rect->texture,rect->modulate);
  3442. #if 0
  3443. int flags=0;
  3444. if (rect->flags&CanvasItem::CommandRect::FLAG_REGION) {
  3445. flags|=Rasterizer::CANVAS_RECT_REGION;
  3446. }
  3447. if (rect->flags&CanvasItem::CommandRect::FLAG_TILE) {
  3448. flags|=Rasterizer::CANVAS_RECT_TILE;
  3449. }
  3450. if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H) {
  3451. flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  3452. }
  3453. if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V) {
  3454. flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  3455. }
  3456. #else
  3457. int flags=rect->flags;
  3458. #endif
  3459. rasterizer->canvas_draw_rect(rect->rect,flags,rect->source,rect->texture,rect->modulate);
  3460. } break;
  3461. case CanvasItem::Command::TYPE_STYLE: {
  3462. CanvasItem::CommandStyle* style = static_cast<CanvasItem::CommandStyle*>(c);
  3463. rasterizer->canvas_draw_style_box(style->rect,style->texture,style->margin,style->draw_center,style->color);
  3464. } break;
  3465. case CanvasItem::Command::TYPE_PRIMITIVE: {
  3466. CanvasItem::CommandPrimitive* primitive = static_cast<CanvasItem::CommandPrimitive*>(c);
  3467. rasterizer->canvas_draw_primitive(primitive->points,primitive->colors,primitive->uvs,primitive->texture,primitive->width);
  3468. } break;
  3469. case CanvasItem::Command::TYPE_POLYGON: {
  3470. CanvasItem::CommandPolygon* polygon = static_cast<CanvasItem::CommandPolygon*>(c);
  3471. rasterizer->canvas_draw_polygon(polygon->count,polygon->indices.ptr(),polygon->points.ptr(),polygon->uvs.ptr(),polygon->colors.ptr(),polygon->texture,polygon->colors.size()==1);
  3472. } break;
  3473. case CanvasItem::Command::TYPE_POLYGON_PTR: {
  3474. CanvasItem::CommandPolygonPtr* polygon = static_cast<CanvasItem::CommandPolygonPtr*>(c);
  3475. rasterizer->canvas_draw_polygon(polygon->count,polygon->indices,polygon->points,polygon->uvs,polygon->colors,polygon->texture,false);
  3476. } break;
  3477. case CanvasItem::Command::TYPE_CIRCLE: {
  3478. CanvasItem::CommandCircle* circle = static_cast<CanvasItem::CommandCircle*>(c);
  3479. static const int numpoints=32;
  3480. Vector2 points[numpoints+1];
  3481. points[numpoints]=circle->pos;
  3482. int indices[numpoints*3];
  3483. for(int i=0;i<numpoints;i++) {
  3484. points[i]=circle->pos+Vector2( Math::sin(i*Math_PI*2.0/numpoints),Math::cos(i*Math_PI*2.0/numpoints) )*circle->radius;
  3485. indices[i*3+0]=i;
  3486. indices[i*3+1]=(i+1)%numpoints;
  3487. indices[i*3+2]=numpoints;
  3488. }
  3489. rasterizer->canvas_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
  3490. //rasterizer->canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
  3491. } break;
  3492. case CanvasItem::Command::TYPE_TRANSFORM: {
  3493. CanvasItem::CommandTransform* transform = static_cast<CanvasItem::CommandTransform*>(c);
  3494. rasterizer->canvas_set_transform(transform->xform);
  3495. } break;
  3496. case CanvasItem::Command::TYPE_BLEND_MODE: {
  3497. CanvasItem::CommandBlendMode* bm = static_cast<CanvasItem::CommandBlendMode*>(c);
  3498. rasterizer->canvas_set_blend_mode(bm->blend_mode);
  3499. } break;
  3500. case CanvasItem::Command::TYPE_CLIP_IGNORE: {
  3501. CanvasItem::CommandClipIgnore* ci = static_cast<CanvasItem::CommandClipIgnore*>(c);
  3502. if (canvas_clip!=Rect2()) {
  3503. if (ci->ignore!=reclip) {
  3504. if (ci->ignore) {
  3505. rasterizer->canvas_set_clip(false,Rect2());
  3506. reclip=true;
  3507. } else {
  3508. rasterizer->canvas_set_clip(true,canvas_clip);
  3509. reclip=false;
  3510. }
  3511. }
  3512. }
  3513. } break;
  3514. }
  3515. }
  3516. rasterizer->canvas_end_rect();
  3517. }
  3518. }
  3519. if (reclip) {
  3520. rasterizer->canvas_set_clip(true,canvas_clip);
  3521. }
  3522. #ifndef ONTOP_DISABLED
  3523. for(int i=0;i<top_item_count;i++) {
  3524. _render_canvas_item(top_items[i],xform,p_clip_rect,opacity);
  3525. }
  3526. #else
  3527. for(int i=0;i<p_canvas_item->child_items.size();i++) {
  3528. _render_canvas_item(p_canvas_item->child_items[i],xform,p_clip_rect,opacity);
  3529. }
  3530. #endif
  3531. if (ci->clip) {
  3532. rasterizer->canvas_set_clip(false,Rect2());
  3533. canvas_clip=Rect2();
  3534. }
  3535. }
  3536. void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform) {
  3537. rasterizer->canvas_begin();
  3538. int l = p_canvas->child_items.size();
  3539. for(int i=0;i<l;i++) {
  3540. Canvas::ChildItem& ci=p_canvas->child_items[i];
  3541. _render_canvas_item(ci.item,p_transform,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3542. //mirroring (useful for scrolling backgrounds)
  3543. if (ci.mirror.x!=0) {
  3544. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0));
  3545. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3546. }
  3547. if (ci.mirror.y!=0) {
  3548. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y));
  3549. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3550. }
  3551. if (ci.mirror.y!=0 && ci.mirror.x!=0) {
  3552. Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror);
  3553. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3554. }
  3555. }
  3556. }
  3557. void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ofs_y,int p_parent_w,int p_parent_h) {
  3558. ViewportRect desired_rect=p_viewport->rect;
  3559. ViewportRect old_rect = viewport_rect;
  3560. // bool vpchanged=false;
  3561. // convert default expanding viewports to actual size
  3562. //if (desired_rect.x==0 && desired_rect.y==0 && desired_rect.width==0 && desired_rect.height==0) {
  3563. if (p_parent_w != 0 && p_parent_h != 0) {
  3564. desired_rect.width=p_parent_w;
  3565. desired_rect.height=p_parent_h;
  3566. }
  3567. ERR_FAIL_COND(desired_rect.width<=0 || desired_rect.height<=0);
  3568. desired_rect.x+=p_ofs_x;
  3569. desired_rect.y+=p_ofs_y;
  3570. // if the viewport is different than the actual one, change it
  3571. if ( p_viewport->render_target.is_valid() || viewport_rect.x != desired_rect.x ||
  3572. viewport_rect.y != desired_rect.y ||
  3573. viewport_rect.width != desired_rect.width ||
  3574. viewport_rect.height != desired_rect.height ) {
  3575. viewport_rect=desired_rect;
  3576. rasterizer->set_viewport(viewport_rect);
  3577. }
  3578. /* Camera should always be BEFORE any other 3D */
  3579. if (!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario)) {
  3580. Camera *camera = camera_owner.get( p_viewport->camera );
  3581. Scenario *scenario = scenario_owner.get( p_viewport->scenario );
  3582. _update_instances(); // check dirty instances before rendering
  3583. _render_camera(p_viewport, camera,scenario );
  3584. } else if (true /*|| !p_viewport->canvas_list.empty()*/){
  3585. //clear the viewport black because of no camera? i seriously should..
  3586. rasterizer->clear_viewport(clear_color);
  3587. }
  3588. if (!p_viewport->hide_canvas) {
  3589. int i=0;
  3590. Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
  3591. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  3592. canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
  3593. }
  3594. for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
  3595. // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
  3596. //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
  3597. Matrix32 xform = p_viewport->global_transform * E->get()->transform;
  3598. _render_canvas( E->get()->canvas,xform );
  3599. i++;
  3600. }
  3601. }
  3602. //capture
  3603. if (p_viewport->queue_capture) {
  3604. rasterizer->capture_viewport(&p_viewport->capture);
  3605. }
  3606. //restore
  3607. if ( viewport_rect.x != old_rect.x ||
  3608. viewport_rect.y != old_rect.y ||
  3609. viewport_rect.width != old_rect.width ||
  3610. viewport_rect.height != old_rect.height ) {
  3611. viewport_rect=old_rect;
  3612. rasterizer->set_viewport(viewport_rect);
  3613. }
  3614. }
  3615. void VisualServerRaster::_draw_viewports() {
  3616. //draw viewports for render targets
  3617. List<Viewport*> to_blit;
  3618. List<Viewport*> to_disable;
  3619. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  3620. Viewport *vp = E->self();
  3621. ERR_CONTINUE(!vp);
  3622. if (
  3623. vp->render_target_update_mode==RENDER_TARGET_UPDATE_WHEN_VISIBLE &&
  3624. !vp->rendered_in_prev_frame &&
  3625. !vp->queue_capture
  3626. ) {
  3627. continue;
  3628. }
  3629. if (vp->rt_to_screen_rect!=Rect2())
  3630. to_blit.push_back(vp);
  3631. rasterizer->set_render_target(vp->render_target,vp->transparent_bg,vp->render_target_vflip);
  3632. _draw_viewport(vp,0,0,vp->rect.width,vp->rect.height);
  3633. if ( (vp->queue_capture && vp->render_target_update_mode==RENDER_TARGET_UPDATE_DISABLED) || vp->render_target_update_mode==RENDER_TARGET_UPDATE_ONCE) {
  3634. //was only enabled for capture
  3635. to_disable.push_back(vp);
  3636. vp->render_target_update_mode=RENDER_TARGET_UPDATE_DISABLED;
  3637. }
  3638. }
  3639. rasterizer->set_render_target(RID());
  3640. while(to_disable.size()) {
  3641. //disable again because it was only for capture
  3642. viewport_update_list.remove(&to_disable.front()->get()->update_list);
  3643. to_disable.pop_front();
  3644. }
  3645. //draw RTs directly to screen when requested
  3646. for (List<Viewport*>::Element *E=to_blit.front();E;E=E->next()) {
  3647. int window_w = OS::get_singleton()->get_video_mode().width;
  3648. int window_h = OS::get_singleton()->get_video_mode().height;
  3649. ViewportRect desired_rect;
  3650. desired_rect.x = desired_rect.y = 0;
  3651. desired_rect.width = window_w;
  3652. desired_rect.height = window_h;
  3653. if ( viewport_rect.x != desired_rect.x ||
  3654. viewport_rect.y != desired_rect.y ||
  3655. viewport_rect.width != desired_rect.width ||
  3656. viewport_rect.height != desired_rect.height ) {
  3657. viewport_rect=desired_rect;
  3658. rasterizer->set_viewport(viewport_rect);
  3659. }
  3660. rasterizer->canvas_begin();
  3661. rasterizer->canvas_disable_blending();
  3662. rasterizer->canvas_begin_rect(Matrix32());
  3663. rasterizer->canvas_draw_rect(E->get()->rt_to_screen_rect,0,Rect2(Point2(),E->get()->rt_to_screen_rect.size),E->get()->render_target_texture,Color(1,1,1));
  3664. }
  3665. //draw viewports attached to screen
  3666. for(Map<RID,int>::Element *E=screen_viewports.front();E;E=E->next()) {
  3667. Viewport *vp = viewport_owner.get(E->key());
  3668. ERR_CONTINUE(!vp);
  3669. int window_w = OS::get_singleton()->get_video_mode(E->get()).width;
  3670. int window_h = OS::get_singleton()->get_video_mode(E->get()).height;
  3671. _draw_viewport(vp,0,0,window_w,window_h);
  3672. }
  3673. //check when a viewport associated to a render target was drawn
  3674. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  3675. Viewport *vp = E->self();
  3676. ERR_CONTINUE(!vp);
  3677. if (vp->render_target_update_mode!=RENDER_TARGET_UPDATE_WHEN_VISIBLE)
  3678. continue;
  3679. vp->rendered_in_prev_frame=rasterizer->render_target_renedered_in_frame(vp->render_target);
  3680. }
  3681. }
  3682. void VisualServerRaster::_draw_cursors_and_margins() {
  3683. int window_w = OS::get_singleton()->get_video_mode().width;
  3684. int window_h = OS::get_singleton()->get_video_mode().height;
  3685. ViewportRect desired_rect;
  3686. desired_rect.x = desired_rect.y = 0;
  3687. desired_rect.width = window_w;
  3688. desired_rect.height = window_h;
  3689. if ( viewport_rect.x != desired_rect.x ||
  3690. viewport_rect.y != desired_rect.y ||
  3691. viewport_rect.width != desired_rect.width ||
  3692. viewport_rect.height != desired_rect.height ) {
  3693. viewport_rect=desired_rect;
  3694. rasterizer->set_viewport(viewport_rect);
  3695. }
  3696. rasterizer->canvas_begin();
  3697. rasterizer->canvas_begin_rect(Matrix32());
  3698. for (int i=0; i<MAX_CURSORS; i++) {
  3699. if (!cursors[i].visible) {
  3700. continue;
  3701. };
  3702. RID tex = cursors[i].texture?cursors[i].texture:default_cursor_texture;
  3703. ERR_CONTINUE( !tex );
  3704. Point2 size(texture_get_width(tex), texture_get_height(tex));
  3705. rasterizer->canvas_draw_rect(Rect2(cursors[i].pos, size), 0, Rect2(), tex, Color(1, 1, 1, 1));
  3706. };
  3707. if (black_margin[MARGIN_LEFT])
  3708. rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3709. if (black_margin[MARGIN_RIGHT])
  3710. rasterizer->canvas_draw_rect(Rect2(window_w-black_margin[MARGIN_RIGHT],0,black_margin[MARGIN_RIGHT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3711. if (black_margin[MARGIN_TOP])
  3712. rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3713. if (black_margin[MARGIN_BOTTOM])
  3714. rasterizer->canvas_draw_rect(Rect2(0,window_h-black_margin[MARGIN_BOTTOM],window_w,black_margin[MARGIN_BOTTOM]),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3715. rasterizer->canvas_end_rect();
  3716. };
  3717. void VisualServerRaster::flush() {
  3718. //do none
  3719. }
  3720. void VisualServerRaster::draw() {
  3721. //if (changes)
  3722. // print_line("changes: "+itos(changes));
  3723. changes=0;
  3724. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  3725. room_cull_enabled = GLOBAL_DEF("render/room_cull_enabled",true);
  3726. light_discard_enabled = GLOBAL_DEF("render/light_discard_enabled",true);
  3727. rasterizer->begin_frame();
  3728. _draw_viewports();
  3729. _draw_cursors_and_margins();
  3730. rasterizer->end_frame();
  3731. draw_extra_frame=rasterizer->needs_to_draw_next_frame();
  3732. }
  3733. bool VisualServerRaster::has_changed() const {
  3734. return changes>0 || draw_extra_frame;
  3735. }
  3736. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  3737. return rasterizer->get_render_info(p_info);
  3738. }
  3739. bool VisualServerRaster::has_feature(Features p_feature) const {
  3740. return rasterizer->has_feature(p_feature); // lies for now
  3741. }
  3742. void VisualServerRaster::set_default_clear_color(const Color& p_color) {
  3743. clear_color=p_color;
  3744. }
  3745. void VisualServerRaster::set_boot_image(const Image& p_image, const Color& p_color) {
  3746. if (p_image.empty())
  3747. return;
  3748. rasterizer->begin_frame();
  3749. int window_w = OS::get_singleton()->get_video_mode(0).width;
  3750. int window_h = OS::get_singleton()->get_video_mode(0).height;
  3751. ViewportRect vr;
  3752. vr.x=0;
  3753. vr.y=0;
  3754. vr.width=OS::get_singleton()->get_video_mode(0).width;
  3755. vr.height=OS::get_singleton()->get_video_mode(0).height;
  3756. rasterizer->set_viewport(vr);
  3757. rasterizer->clear_viewport(p_color);
  3758. rasterizer->canvas_begin();
  3759. RID texture = texture_create();
  3760. texture_allocate(texture,p_image.get_width(),p_image.get_height(),p_image.get_format(),TEXTURE_FLAG_FILTER);
  3761. texture_set_data(texture,p_image);
  3762. rasterizer->canvas_begin_rect(Matrix32());
  3763. Rect2 imgrect(0,0,p_image.get_width(),p_image.get_height());
  3764. Rect2 screenrect=imgrect;
  3765. screenrect.pos+=((Size2(vr.width,vr.height)-screenrect.size)/2.0).floor();
  3766. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,0));
  3767. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,1));
  3768. rasterizer->canvas_end_rect();
  3769. rasterizer->end_frame();
  3770. rasterizer->flush_frame();
  3771. free(texture); // free since it's only one frame that stays there
  3772. }
  3773. void VisualServerRaster::init() {
  3774. rasterizer->init();
  3775. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  3776. //default_scenario = scenario_create();
  3777. //default_viewport = viewport_create();
  3778. for(int i=0;i<4;i++)
  3779. black_margin[i]=0;
  3780. Image img;
  3781. img.create(default_mouse_cursor_xpm);
  3782. //img.convert(Image::FORMAT_RGB);
  3783. default_cursor_texture = texture_create_from_image(img, 0);
  3784. aabb_random_points.resize( GLOBAL_DEF("render/aabb_random_points",16) );
  3785. for(int i=0;i<aabb_random_points.size();i++)
  3786. aabb_random_points[i]=Vector3(Math::random(0,1),Math::random(0,1),Math::random(0,1));
  3787. transformed_aabb_random_points.resize(aabb_random_points.size());
  3788. changes=0;
  3789. }
  3790. void VisualServerRaster::_clean_up_owner(RID_OwnerBase *p_owner,String p_type) {
  3791. List<RID> rids;
  3792. p_owner->get_owned_list(&rids);
  3793. int lost=0;
  3794. for(List<RID>::Element *I=rids.front();I;I=I->next()) {
  3795. if (OS::get_singleton()->is_stdout_verbose()) {
  3796. lost++;
  3797. }
  3798. free(I->get());
  3799. }
  3800. if (lost)
  3801. print_line("VisualServerRaster: WARNING: Lost "+itos(lost)+" RIDs of type "+p_type);
  3802. }
  3803. void VisualServerRaster::finish() {
  3804. free(default_cursor_texture);
  3805. _clean_up_owner( &room_owner,"Room" );
  3806. _clean_up_owner( &portal_owner,"Portal" );
  3807. _clean_up_owner( &camera_owner,"Camera" );
  3808. _clean_up_owner( &viewport_owner,"Viewport" );
  3809. _clean_up_owner( &scenario_owner,"Scenario" );
  3810. _clean_up_owner( &instance_owner,"Instance" );
  3811. _clean_up_owner( &canvas_owner,"Canvas" );
  3812. _clean_up_owner( &canvas_item_owner,"CanvasItem" );
  3813. rasterizer->finish();
  3814. octree_allocator.clear();
  3815. if (instance_dependency_map.size()) {
  3816. print_line("base resources missing "+itos(instance_dependency_map.size()));
  3817. }
  3818. ERR_FAIL_COND( instance_dependency_map.size() );
  3819. }
  3820. RID VisualServerRaster::get_test_cube() {
  3821. if (test_cube.is_valid())
  3822. return test_cube;
  3823. test_cube=_make_test_cube();
  3824. return test_cube;
  3825. }
  3826. VisualServerRaster::VisualServerRaster(Rasterizer *p_rasterizer) {
  3827. rasterizer=p_rasterizer;
  3828. instance_update_list=NULL;
  3829. render_pass=0;
  3830. clear_color=Color(0.3,0.3,0.3,1.0);
  3831. OctreeAllocator::allocator=&octree_allocator;
  3832. draw_extra_frame=false;
  3833. }
  3834. VisualServerRaster::~VisualServerRaster()
  3835. {
  3836. }