visual_server_raster.cpp 206 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769
  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-2016 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. #include "io/marshalls.h"
  35. // careful, these may run in different threads than the visual server
  36. BalloonAllocator<> *VisualServerRaster::OctreeAllocator::allocator=NULL;
  37. #define VS_CHANGED\
  38. changes++;\
  39. // print_line(__FUNCTION__);
  40. RID VisualServerRaster::texture_create() {
  41. return rasterizer->texture_create();
  42. }
  43. void VisualServerRaster::texture_allocate(RID p_texture, int p_width, int p_height,Image::Format p_format,uint32_t p_flags) {
  44. rasterizer->texture_allocate(p_texture,p_width,p_height,p_format,p_flags);
  45. }
  46. void VisualServerRaster::texture_set_flags(RID p_texture,uint32_t p_flags) {
  47. VS_CHANGED;
  48. rasterizer->texture_set_flags(p_texture,p_flags);
  49. }
  50. void VisualServerRaster::texture_set_data(RID p_texture,const Image& p_image,CubeMapSide p_cube_side) {
  51. VS_CHANGED;
  52. rasterizer->texture_set_data(p_texture,p_image,p_cube_side);
  53. }
  54. Image VisualServerRaster::texture_get_data(RID p_texture,CubeMapSide p_cube_side) const {
  55. return rasterizer->texture_get_data(p_texture,p_cube_side);
  56. }
  57. uint32_t VisualServerRaster::texture_get_flags(RID p_texture) const {
  58. return rasterizer->texture_get_flags(p_texture);
  59. }
  60. Image::Format VisualServerRaster::texture_get_format(RID p_texture) const {
  61. return rasterizer->texture_get_format(p_texture);
  62. }
  63. uint32_t VisualServerRaster::texture_get_width(RID p_texture) const {
  64. return rasterizer->texture_get_width(p_texture);
  65. }
  66. uint32_t VisualServerRaster::texture_get_height(RID p_texture) const {
  67. return rasterizer->texture_get_height(p_texture);
  68. }
  69. void VisualServerRaster::texture_set_size_override(RID p_texture,int p_width, int p_height) {
  70. rasterizer->texture_set_size_override(p_texture,p_width,p_height);
  71. }
  72. bool VisualServerRaster::texture_can_stream(RID p_texture) const {
  73. return false;
  74. }
  75. void VisualServerRaster::texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const {
  76. rasterizer->texture_set_reload_hook(p_texture,p_owner,p_function);
  77. }
  78. void VisualServerRaster::texture_set_path(RID p_texture,const String& p_path) {
  79. rasterizer->texture_set_path(p_texture,p_path);
  80. }
  81. String VisualServerRaster::texture_get_path(RID p_texture) const{
  82. return rasterizer->texture_get_path(p_texture);
  83. }
  84. void VisualServerRaster::texture_debug_usage(List<TextureInfo> *r_info){
  85. rasterizer->texture_debug_usage(r_info);
  86. }
  87. void VisualServerRaster::texture_set_shrink_all_x2_on_set_data(bool p_enable) {
  88. rasterizer->texture_set_shrink_all_x2_on_set_data(p_enable);
  89. }
  90. /* SHADER API */
  91. RID VisualServerRaster::shader_create(ShaderMode p_mode) {
  92. return rasterizer->shader_create(p_mode);
  93. }
  94. void VisualServerRaster::shader_set_mode(RID p_shader,ShaderMode p_mode){
  95. VS_CHANGED;
  96. rasterizer->shader_set_mode(p_shader,p_mode);
  97. }
  98. VisualServer::ShaderMode VisualServerRaster::shader_get_mode(RID p_shader) const{
  99. return rasterizer->shader_get_mode(p_shader);
  100. }
  101. void VisualServerRaster::shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,const String& p_light,int p_vertex_ofs,int p_fragment_ofs,int p_light_ofs) {
  102. VS_CHANGED;
  103. rasterizer->shader_set_code(p_shader,p_vertex,p_fragment,p_light,p_vertex_ofs,p_fragment_ofs,p_light_ofs);
  104. }
  105. String VisualServerRaster::shader_get_vertex_code(RID p_shader) const{
  106. return rasterizer->shader_get_vertex_code(p_shader);
  107. }
  108. String VisualServerRaster::shader_get_fragment_code(RID p_shader) const{
  109. return rasterizer->shader_get_fragment_code(p_shader);
  110. }
  111. String VisualServerRaster::shader_get_light_code(RID p_shader) const{
  112. return rasterizer->shader_get_light_code(p_shader);
  113. }
  114. void VisualServerRaster::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  115. return rasterizer->shader_get_param_list(p_shader,p_param_list);
  116. }
  117. void VisualServerRaster::shader_set_default_texture_param(RID p_shader, const StringName& p_name, RID p_texture) {
  118. rasterizer->shader_set_default_texture_param(p_shader,p_name,p_texture);
  119. }
  120. RID VisualServerRaster::shader_get_default_texture_param(RID p_shader, const StringName& p_name) const{
  121. return rasterizer->shader_get_default_texture_param(p_shader,p_name);
  122. }
  123. /* Material */
  124. RID VisualServerRaster::material_create() {
  125. return rasterizer->material_create();
  126. }
  127. void VisualServerRaster::material_set_shader(RID p_material, RID p_shader) {
  128. VS_CHANGED;
  129. rasterizer->material_set_shader(p_material, p_shader );
  130. }
  131. RID VisualServerRaster::material_get_shader(RID p_material) const {
  132. return rasterizer->material_get_shader(p_material);
  133. }
  134. void VisualServerRaster::material_set_param(RID p_material, const StringName& p_param, const Variant& p_value) {
  135. VS_CHANGED;
  136. rasterizer->material_set_param(p_material, p_param,p_value );
  137. }
  138. Variant VisualServerRaster::material_get_param(RID p_material, const StringName& p_param) const {
  139. return rasterizer->material_get_param(p_material,p_param);
  140. }
  141. void VisualServerRaster::material_set_flag(RID p_material, MaterialFlag p_flag,bool p_enabled) {
  142. VS_CHANGED;
  143. rasterizer->material_set_flag(p_material,p_flag,p_enabled);
  144. }
  145. void VisualServerRaster::material_set_depth_draw_mode(RID p_material, MaterialDepthDrawMode p_mode) {
  146. VS_CHANGED;
  147. rasterizer->material_set_depth_draw_mode(p_material,p_mode);
  148. }
  149. VS::MaterialDepthDrawMode VisualServerRaster::material_get_depth_draw_mode(RID p_material) const {
  150. return rasterizer->material_get_depth_draw_mode(p_material);
  151. }
  152. bool VisualServerRaster::material_get_flag(RID p_material,MaterialFlag p_flag) const {
  153. return rasterizer->material_get_flag(p_material,p_flag);
  154. }
  155. void VisualServerRaster::material_set_blend_mode(RID p_material,MaterialBlendMode p_mode) {
  156. VS_CHANGED;
  157. rasterizer->material_set_blend_mode(p_material,p_mode);
  158. }
  159. VS::MaterialBlendMode VisualServerRaster::material_get_blend_mode(RID p_material) const {
  160. return rasterizer->material_get_blend_mode(p_material);
  161. }
  162. void VisualServerRaster::material_set_line_width(RID p_material,float p_line_width) {
  163. VS_CHANGED;
  164. rasterizer->material_set_line_width(p_material,p_line_width);
  165. }
  166. float VisualServerRaster::material_get_line_width(RID p_material) const {
  167. return rasterizer->material_get_line_width(p_material);
  168. }
  169. /* FIXED MATERIAL */
  170. RID VisualServerRaster::fixed_material_create() {
  171. return rasterizer->fixed_material_create();
  172. }
  173. void VisualServerRaster::fixed_material_set_flag(RID p_material, FixedMaterialFlags p_flag, bool p_enabled) {
  174. rasterizer->fixed_material_set_flag(p_material,p_flag,p_enabled);
  175. }
  176. bool VisualServerRaster::fixed_material_get_flag(RID p_material, FixedMaterialFlags p_flag) const {
  177. return rasterizer->fixed_material_get_flag(p_material,p_flag);
  178. }
  179. void VisualServerRaster::fixed_material_set_param(RID p_material, FixedMaterialParam p_parameter, const Variant& p_value) {
  180. VS_CHANGED;
  181. rasterizer->fixed_material_set_parameter(p_material,p_parameter,p_value);
  182. }
  183. Variant VisualServerRaster::fixed_material_get_param(RID p_material,FixedMaterialParam p_parameter) const {
  184. return rasterizer->fixed_material_get_parameter(p_material,p_parameter);
  185. }
  186. void VisualServerRaster::fixed_material_set_texture(RID p_material,FixedMaterialParam p_parameter, RID p_texture) {
  187. VS_CHANGED;
  188. rasterizer->fixed_material_set_texture(p_material,p_parameter,p_texture);
  189. }
  190. RID VisualServerRaster::fixed_material_get_texture(RID p_material,FixedMaterialParam p_parameter) const {
  191. return rasterizer->fixed_material_get_texture(p_material,p_parameter);
  192. }
  193. void VisualServerRaster::fixed_material_set_texcoord_mode(RID p_material,FixedMaterialParam p_parameter, FixedMaterialTexCoordMode p_mode) {
  194. VS_CHANGED;
  195. rasterizer->fixed_material_set_texcoord_mode(p_material,p_parameter,p_mode);
  196. }
  197. VS::FixedMaterialTexCoordMode VisualServerRaster::fixed_material_get_texcoord_mode(RID p_material,FixedMaterialParam p_parameter) const {
  198. return rasterizer->fixed_material_get_texcoord_mode(p_material,p_parameter);
  199. }
  200. void VisualServerRaster::fixed_material_set_point_size(RID p_material,float p_size) {
  201. VS_CHANGED
  202. rasterizer->fixed_material_set_point_size(p_material,p_size);
  203. }
  204. float VisualServerRaster::fixed_material_get_point_size(RID p_material) const{
  205. return rasterizer->fixed_material_get_point_size(p_material);
  206. }
  207. void VisualServerRaster::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) {
  208. VS_CHANGED;
  209. rasterizer->fixed_material_set_uv_transform(p_material,p_transform);
  210. }
  211. Transform VisualServerRaster::fixed_material_get_uv_transform(RID p_material) const {
  212. return rasterizer->fixed_material_get_uv_transform(p_material);
  213. }
  214. void VisualServerRaster::fixed_material_set_light_shader(RID p_material,FixedMaterialLightShader p_shader) {
  215. VS_CHANGED;
  216. rasterizer->fixed_material_set_light_shader(p_material,p_shader);
  217. }
  218. VisualServerRaster::FixedMaterialLightShader VisualServerRaster::fixed_material_get_light_shader(RID p_material) const{
  219. return rasterizer->fixed_material_get_light_shader(p_material);
  220. }
  221. /* MESH API */
  222. RID VisualServerRaster::mesh_create() {
  223. return rasterizer->mesh_create();
  224. }
  225. void VisualServerRaster::mesh_set_morph_target_count(RID p_mesh,int p_amount) {
  226. rasterizer->mesh_set_morph_target_count(p_mesh,p_amount);
  227. int amount = rasterizer->mesh_get_morph_target_count(p_mesh);
  228. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_mesh );
  229. if (!E)
  230. return;
  231. Set<RID>::Element *I = E->get().front();
  232. while(I) {
  233. Instance *ins = instance_owner.get( I->get() );
  234. ins->data.morph_values.resize(amount);
  235. I = I->next();
  236. }
  237. }
  238. int VisualServerRaster::mesh_get_morph_target_count(RID p_mesh) const {
  239. return rasterizer->mesh_get_morph_target_count(p_mesh);
  240. }
  241. void VisualServerRaster::mesh_set_morph_target_mode(RID p_mesh,MorphTargetMode p_mode) {
  242. rasterizer->mesh_set_morph_target_mode(p_mesh,p_mode);
  243. }
  244. VisualServer::MorphTargetMode VisualServerRaster::mesh_get_morph_target_mode(RID p_mesh) const{
  245. return rasterizer->mesh_get_morph_target_mode(p_mesh);
  246. }
  247. void VisualServerRaster::mesh_add_custom_surface(RID p_mesh,const Variant& p_dat) {
  248. }
  249. void VisualServerRaster::mesh_add_surface(RID p_mesh,PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alpha_sort) {
  250. VS_CHANGED;
  251. _dependency_queue_update(p_mesh,true,true);
  252. rasterizer->mesh_add_surface(p_mesh,p_primitive,p_arrays,p_blend_shapes,p_alpha_sort);
  253. }
  254. Array VisualServerRaster::mesh_get_surface_arrays(RID p_mesh,int p_surface) const {
  255. return rasterizer->mesh_get_surface_arrays(p_mesh,p_surface);
  256. }
  257. Array VisualServerRaster::mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const {
  258. return rasterizer->mesh_get_surface_morph_arrays(p_mesh,p_surface);
  259. }
  260. void VisualServerRaster::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned){
  261. VS_CHANGED;
  262. rasterizer->mesh_surface_set_material(p_mesh,p_surface,p_material,p_owned);
  263. }
  264. RID VisualServerRaster::mesh_surface_get_material(RID p_mesh,int p_surface) const {
  265. return rasterizer->mesh_surface_get_material(p_mesh,p_surface);
  266. }
  267. int VisualServerRaster::mesh_surface_get_array_len(RID p_mesh, int p_surface) const{
  268. return rasterizer->mesh_surface_get_array_len(p_mesh,p_surface);
  269. }
  270. int VisualServerRaster::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const{
  271. return rasterizer->mesh_surface_get_array_index_len(p_mesh,p_surface);
  272. }
  273. uint32_t VisualServerRaster::mesh_surface_get_format(RID p_mesh, int p_surface) const{
  274. return rasterizer->mesh_surface_get_format(p_mesh,p_surface);
  275. }
  276. VisualServer::PrimitiveType VisualServerRaster::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const{
  277. return rasterizer->mesh_surface_get_primitive_type(p_mesh,p_surface);
  278. }
  279. void VisualServerRaster::mesh_remove_surface(RID p_mesh,int p_surface){
  280. rasterizer->mesh_remove_surface(p_mesh,p_surface);
  281. _dependency_queue_update(p_mesh,true,true);
  282. }
  283. int VisualServerRaster::mesh_get_surface_count(RID p_mesh) const{
  284. return rasterizer->mesh_get_surface_count(p_mesh);
  285. }
  286. void VisualServerRaster::mesh_set_custom_aabb(RID p_mesh,const AABB& p_aabb) {
  287. VS_CHANGED;
  288. _dependency_queue_update(p_mesh,true);
  289. rasterizer->mesh_set_custom_aabb(p_mesh,p_aabb);
  290. }
  291. AABB VisualServerRaster::mesh_get_custom_aabb(RID p_mesh) const {
  292. return rasterizer->mesh_get_custom_aabb(p_mesh);
  293. }
  294. void VisualServerRaster::mesh_clear(RID p_mesh) {
  295. ERR_FAIL_COND(!rasterizer->is_mesh(p_mesh));
  296. while(rasterizer->mesh_get_surface_count(p_mesh)) {
  297. rasterizer->mesh_remove_surface(p_mesh,0);
  298. }
  299. _dependency_queue_update(p_mesh,true,true);
  300. }
  301. /* MULTIMESH */
  302. RID VisualServerRaster::multimesh_create() {
  303. return rasterizer->multimesh_create();
  304. }
  305. void VisualServerRaster::multimesh_set_instance_count(RID p_multimesh,int p_count) {
  306. VS_CHANGED;
  307. rasterizer->multimesh_set_instance_count(p_multimesh,p_count);
  308. }
  309. int VisualServerRaster::multimesh_get_instance_count(RID p_multimesh) const {
  310. return rasterizer->multimesh_get_instance_count(p_multimesh);
  311. }
  312. void VisualServerRaster::multimesh_set_mesh(RID p_multimesh,RID p_mesh) {
  313. VS_CHANGED;
  314. rasterizer->multimesh_set_mesh(p_multimesh,p_mesh);
  315. }
  316. void VisualServerRaster::multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb) {
  317. VS_CHANGED;
  318. rasterizer->multimesh_set_aabb(p_multimesh,p_aabb);
  319. _dependency_queue_update(p_multimesh,true);
  320. }
  321. void VisualServerRaster::multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform) {
  322. VS_CHANGED;
  323. rasterizer->multimesh_instance_set_transform(p_multimesh,p_index,p_transform);
  324. }
  325. void VisualServerRaster::multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color) {
  326. VS_CHANGED;
  327. rasterizer->multimesh_instance_set_color(p_multimesh,p_index,p_color);
  328. }
  329. RID VisualServerRaster::multimesh_get_mesh(RID p_multimesh) const {
  330. return rasterizer->multimesh_get_mesh(p_multimesh);
  331. }
  332. AABB VisualServerRaster::multimesh_get_aabb(RID p_multimesh,const AABB& p_aabb) const {
  333. return rasterizer->multimesh_get_aabb(p_multimesh);
  334. }
  335. Transform VisualServerRaster::multimesh_instance_get_transform(RID p_multimesh,int p_index) const {
  336. return rasterizer->multimesh_instance_get_transform(p_multimesh,p_index);
  337. }
  338. Color VisualServerRaster::multimesh_instance_get_color(RID p_multimesh,int p_index) const {
  339. return rasterizer->multimesh_instance_get_color(p_multimesh,p_index);
  340. }
  341. void VisualServerRaster::multimesh_set_visible_instances(RID p_multimesh,int p_visible) {
  342. rasterizer->multimesh_set_visible_instances(p_multimesh,p_visible);
  343. }
  344. int VisualServerRaster::multimesh_get_visible_instances(RID p_multimesh) const {
  345. return rasterizer->multimesh_get_visible_instances(p_multimesh);
  346. }
  347. /* IMMEDIATE API */
  348. RID VisualServerRaster::immediate_create() {
  349. return rasterizer->immediate_create();
  350. }
  351. void VisualServerRaster::immediate_begin(RID p_immediate,PrimitiveType p_primitive,RID p_texture){
  352. rasterizer->immediate_begin(p_immediate,p_primitive,p_texture);
  353. }
  354. void VisualServerRaster::immediate_vertex(RID p_immediate,const Vector3& p_vertex){
  355. rasterizer->immediate_vertex(p_immediate,p_vertex);
  356. }
  357. void VisualServerRaster::immediate_normal(RID p_immediate,const Vector3& p_normal){
  358. rasterizer->immediate_normal(p_immediate,p_normal);
  359. }
  360. void VisualServerRaster::immediate_tangent(RID p_immediate,const Plane& p_tangent){
  361. rasterizer->immediate_tangent(p_immediate,p_tangent);
  362. }
  363. void VisualServerRaster::immediate_color(RID p_immediate,const Color& p_color){
  364. rasterizer->immediate_color(p_immediate,p_color);
  365. }
  366. void VisualServerRaster::immediate_uv(RID p_immediate,const Vector2& p_uv){
  367. rasterizer->immediate_uv(p_immediate,p_uv);
  368. }
  369. void VisualServerRaster::immediate_uv2(RID p_immediate,const Vector2& p_uv2){
  370. rasterizer->immediate_uv2(p_immediate,p_uv2);
  371. }
  372. void VisualServerRaster::immediate_end(RID p_immediate){
  373. VS_CHANGED;
  374. _dependency_queue_update(p_immediate,true);
  375. rasterizer->immediate_end(p_immediate);
  376. }
  377. void VisualServerRaster::immediate_clear(RID p_immediate){
  378. VS_CHANGED;
  379. _dependency_queue_update(p_immediate,true);
  380. rasterizer->immediate_clear(p_immediate);
  381. }
  382. void VisualServerRaster::immediate_set_material(RID p_immediate,RID p_material) {
  383. rasterizer->immediate_set_material(p_immediate,p_material);
  384. }
  385. RID VisualServerRaster::immediate_get_material(RID p_immediate) const {
  386. return rasterizer->immediate_get_material(p_immediate);
  387. }
  388. /* PARTICLES API */
  389. RID VisualServerRaster::particles_create() {
  390. return rasterizer->particles_create();
  391. }
  392. void VisualServerRaster::particles_set_amount(RID p_particles, int p_amount) {
  393. VS_CHANGED;
  394. rasterizer->particles_set_amount(p_particles,p_amount);
  395. }
  396. int VisualServerRaster::particles_get_amount(RID p_particles) const {
  397. return rasterizer->particles_get_amount(p_particles);
  398. }
  399. void VisualServerRaster::particles_set_emitting(RID p_particles, bool p_emitting) {
  400. VS_CHANGED;
  401. rasterizer->particles_set_emitting(p_particles,p_emitting);
  402. }
  403. bool VisualServerRaster::particles_is_emitting(RID p_particles) const {
  404. return rasterizer->particles_is_emitting(p_particles);
  405. }
  406. void VisualServerRaster::particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility) {
  407. VS_CHANGED;
  408. rasterizer->particles_set_visibility_aabb(p_particles, p_visibility);
  409. }
  410. AABB VisualServerRaster::particles_get_visibility_aabb(RID p_particles) const {
  411. return rasterizer->particles_get_visibility_aabb(p_particles);
  412. }
  413. void VisualServerRaster::particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents) {
  414. VS_CHANGED;
  415. rasterizer->particles_set_emission_half_extents(p_particles,p_half_extents);
  416. }
  417. Vector3 VisualServerRaster::particles_get_emission_half_extents(RID p_particles) const {
  418. return rasterizer->particles_get_emission_half_extents(p_particles);
  419. }
  420. void VisualServerRaster::particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity) {
  421. VS_CHANGED;
  422. rasterizer->particles_set_emission_base_velocity(p_particles,p_base_velocity);
  423. }
  424. Vector3 VisualServerRaster::particles_get_emission_base_velocity(RID p_particles) const {
  425. return rasterizer->particles_get_emission_base_velocity(p_particles);
  426. }
  427. void VisualServerRaster::particles_set_emission_points(RID p_particles, const DVector<Vector3>& p_points) {
  428. VS_CHANGED;
  429. rasterizer->particles_set_emission_points(p_particles,p_points);
  430. }
  431. DVector<Vector3> VisualServerRaster::particles_get_emission_points(RID p_particles) const {
  432. return rasterizer->particles_get_emission_points(p_particles);
  433. }
  434. void VisualServerRaster::particles_set_gravity_normal(RID p_particles, const Vector3& p_normal) {
  435. VS_CHANGED;
  436. rasterizer->particles_set_gravity_normal(p_particles,p_normal);
  437. }
  438. Vector3 VisualServerRaster::particles_get_gravity_normal(RID p_particles) const {
  439. return rasterizer->particles_get_gravity_normal(p_particles);
  440. }
  441. void VisualServerRaster::particles_set_variable(RID p_particles, ParticleVariable p_variable,float p_value) {
  442. VS_CHANGED;
  443. rasterizer->particles_set_variable(p_particles,p_variable,p_value);
  444. }
  445. float VisualServerRaster::particles_get_variable(RID p_particles, ParticleVariable p_variable) const {
  446. return rasterizer->particles_get_variable(p_particles,p_variable);
  447. }
  448. void VisualServerRaster::particles_set_randomness(RID p_particles, ParticleVariable p_variable,float p_randomness) {
  449. VS_CHANGED;
  450. rasterizer->particles_set_randomness(p_particles,p_variable,p_randomness);
  451. }
  452. float VisualServerRaster::particles_get_randomness(RID p_particles, ParticleVariable p_variable) const {
  453. return rasterizer->particles_get_randomness(p_particles,p_variable);
  454. }
  455. void VisualServerRaster::particles_set_color_phases(RID p_particles, int p_phases) {
  456. VS_CHANGED;
  457. rasterizer->particles_set_color_phases(p_particles,p_phases);
  458. }
  459. int VisualServerRaster::particles_get_color_phases(RID p_particles) const {
  460. return rasterizer->particles_get_color_phases(p_particles);
  461. }
  462. void VisualServerRaster::particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) {
  463. VS_CHANGED;
  464. rasterizer->particles_set_color_phase_pos(p_particles,p_phase,p_pos);
  465. }
  466. float VisualServerRaster::particles_get_color_phase_pos(RID p_particles, int p_phase) const {
  467. return rasterizer->particles_get_color_phase_pos(p_particles,p_phase);
  468. }
  469. void VisualServerRaster::particles_set_attractors(RID p_particles, int p_attractors) {
  470. VS_CHANGED;
  471. rasterizer->particles_set_attractors(p_particles,p_attractors);
  472. }
  473. int VisualServerRaster::particles_get_attractors(RID p_particles) const {
  474. return rasterizer->particles_get_attractors(p_particles);
  475. }
  476. void VisualServerRaster::particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos) {
  477. VS_CHANGED;
  478. rasterizer->particles_set_attractor_pos(p_particles,p_attractor,p_pos);
  479. }
  480. Vector3 VisualServerRaster::particles_get_attractor_pos(RID p_particles,int p_attractor) const {
  481. return rasterizer->particles_get_attractor_pos(p_particles,p_attractor);
  482. }
  483. void VisualServerRaster::particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) {
  484. VS_CHANGED;
  485. rasterizer->particles_set_attractor_strength(p_particles,p_attractor,p_force);
  486. }
  487. float VisualServerRaster::particles_get_attractor_strength(RID p_particles,int p_attractor) const {
  488. return rasterizer->particles_get_attractor_strength(p_particles,p_attractor);
  489. }
  490. void VisualServerRaster::particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color) {
  491. VS_CHANGED;
  492. rasterizer->particles_set_color_phase_color(p_particles,p_phase,p_color);
  493. }
  494. Color VisualServerRaster::particles_get_color_phase_color(RID p_particles, int p_phase) const {
  495. return rasterizer->particles_get_color_phase_color(p_particles,p_phase);
  496. }
  497. void VisualServerRaster::particles_set_material(RID p_particles, RID p_material,bool p_owned) {
  498. VS_CHANGED;
  499. rasterizer->particles_set_material(p_particles,p_material,p_owned);
  500. }
  501. RID VisualServerRaster::particles_get_material(RID p_particles) const {
  502. return rasterizer->particles_get_material(p_particles);
  503. }
  504. void VisualServerRaster::particles_set_height_from_velocity(RID p_particles, bool p_enable) {
  505. VS_CHANGED;
  506. rasterizer->particles_set_height_from_velocity(p_particles,p_enable);
  507. }
  508. bool VisualServerRaster::particles_has_height_from_velocity(RID p_particles) const {
  509. return rasterizer->particles_has_height_from_velocity(p_particles);
  510. }
  511. void VisualServerRaster::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  512. rasterizer->particles_set_use_local_coordinates(p_particles,p_enable);
  513. }
  514. bool VisualServerRaster::particles_is_using_local_coordinates(RID p_particles) const {
  515. return rasterizer->particles_is_using_local_coordinates(p_particles);
  516. }
  517. /* Light API */
  518. RID VisualServerRaster::light_create(LightType p_type) {
  519. return rasterizer->light_create(p_type);
  520. }
  521. VisualServer::LightType VisualServerRaster::light_get_type(RID p_light) const {
  522. return rasterizer->light_get_type(p_light);
  523. }
  524. void VisualServerRaster::light_set_color(RID p_light,LightColor p_type, const Color& p_color) {
  525. VS_CHANGED;
  526. rasterizer->light_set_color(p_light,p_type,p_color);
  527. }
  528. Color VisualServerRaster::light_get_color(RID p_light,LightColor p_type) const {
  529. return rasterizer->light_get_color(p_light,p_type);
  530. }
  531. void VisualServerRaster::light_set_shadow(RID p_light,bool p_enabled) {
  532. VS_CHANGED;
  533. rasterizer->light_set_shadow(p_light,p_enabled);
  534. }
  535. bool VisualServerRaster::light_has_shadow(RID p_light) const {
  536. return rasterizer->light_has_shadow(p_light);
  537. }
  538. void VisualServerRaster::light_set_volumetric(RID p_light,bool p_enabled) {
  539. VS_CHANGED;
  540. rasterizer->light_set_volumetric(p_light,p_enabled);
  541. }
  542. bool VisualServerRaster::light_is_volumetric(RID p_light) const {
  543. return rasterizer->light_is_volumetric(p_light);
  544. }
  545. void VisualServerRaster::light_set_projector(RID p_light,RID p_texture) {
  546. VS_CHANGED;
  547. rasterizer->light_set_projector(p_light,p_texture);
  548. }
  549. RID VisualServerRaster::light_get_projector(RID p_light) const {
  550. return rasterizer->light_get_projector(p_light);
  551. }
  552. void VisualServerRaster::light_set_param(RID p_light, LightParam p_var, float p_value) {
  553. VS_CHANGED;
  554. rasterizer->light_set_var(p_light,p_var,p_value);
  555. _dependency_queue_update(p_light,true);
  556. }
  557. float VisualServerRaster::light_get_param(RID p_light, LightParam p_var) const {
  558. return rasterizer->light_get_var(p_light,p_var);
  559. }
  560. void VisualServerRaster::light_set_operator(RID p_light,LightOp p_op) {
  561. VS_CHANGED;
  562. rasterizer->light_set_operator(p_light,p_op);
  563. }
  564. VisualServerRaster::LightOp VisualServerRaster::light_get_operator(RID p_light) const {
  565. return rasterizer->light_get_operator(p_light);
  566. }
  567. void VisualServerRaster::light_omni_set_shadow_mode(RID p_light,LightOmniShadowMode p_mode) {
  568. VS_CHANGED;
  569. rasterizer->light_omni_set_shadow_mode(p_light,p_mode);
  570. }
  571. VisualServerRaster::LightOmniShadowMode VisualServerRaster::light_omni_get_shadow_mode(RID p_light) const {
  572. return rasterizer->light_omni_get_shadow_mode(p_light);
  573. }
  574. void VisualServerRaster::light_directional_set_shadow_mode(RID p_light,LightDirectionalShadowMode p_mode){
  575. VS_CHANGED;
  576. rasterizer->light_directional_set_shadow_mode(p_light,p_mode);
  577. }
  578. VS::LightDirectionalShadowMode VisualServerRaster::light_directional_get_shadow_mode(RID p_light) const{
  579. return rasterizer->light_directional_get_shadow_mode(p_light);
  580. }
  581. void VisualServerRaster::light_directional_set_shadow_param(RID p_light,LightDirectionalShadowParam p_param, float p_value) {
  582. VS_CHANGED;
  583. rasterizer->light_directional_set_shadow_param(p_light,p_param,p_value);
  584. }
  585. float VisualServerRaster::light_directional_get_shadow_param(RID p_light,LightDirectionalShadowParam p_param) const {
  586. return rasterizer->light_directional_get_shadow_param(p_light,p_param);
  587. }
  588. RID VisualServerRaster::skeleton_create() {
  589. return rasterizer->skeleton_create();
  590. }
  591. void VisualServerRaster::skeleton_resize(RID p_skeleton,int p_bones) {
  592. VS_CHANGED;
  593. rasterizer->skeleton_resize(p_skeleton,p_bones);
  594. }
  595. int VisualServerRaster::skeleton_get_bone_count(RID p_skeleton) const {
  596. return rasterizer->skeleton_get_bone_count(p_skeleton);
  597. }
  598. void VisualServerRaster::skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform) {
  599. VS_CHANGED;
  600. rasterizer->skeleton_bone_set_transform(p_skeleton,p_bone,p_transform);
  601. Map< RID, Set<Instance*> >::Element *E=skeleton_dependency_map.find(p_skeleton);
  602. if (E) {
  603. //detach skeletons
  604. for (Set<Instance*>::Element *F=E->get().front();F;F=F->next()) {
  605. _instance_queue_update( F->get() , true);
  606. }
  607. }
  608. }
  609. Transform VisualServerRaster::skeleton_bone_get_transform(RID p_skeleton,int p_bone) {
  610. return rasterizer->skeleton_bone_get_transform(p_skeleton,p_bone);
  611. }
  612. /* VISIBILITY API */
  613. /* ROOM API */
  614. RID VisualServerRaster::room_create() {
  615. Room *room = memnew( Room );
  616. ERR_FAIL_COND_V(!room,RID());
  617. return room_owner.make_rid( room );
  618. }
  619. void VisualServerRaster::room_set_bounds(RID p_room, const BSP_Tree& p_bounds) {
  620. VS_CHANGED;
  621. Room *room = room_owner.get(p_room);
  622. ERR_FAIL_COND(!room);
  623. room->bounds=p_bounds;
  624. _dependency_queue_update(p_room,true);
  625. }
  626. BSP_Tree VisualServerRaster::room_get_bounds(RID p_room) const {
  627. Room *room = room_owner.get(p_room);
  628. ERR_FAIL_COND_V(!room, BSP_Tree());
  629. return room->bounds;
  630. }
  631. /* PORTAL API */
  632. RID VisualServerRaster::portal_create() {
  633. VS_CHANGED;
  634. Portal *portal = memnew( Portal );
  635. ERR_FAIL_COND_V(!portal,RID());
  636. return portal_owner.make_rid( portal );
  637. }
  638. void VisualServerRaster::portal_set_shape(RID p_portal, const Vector<Point2>& p_shape) {
  639. VS_CHANGED;
  640. Portal *portal = portal_owner.get(p_portal);
  641. ERR_FAIL_COND(!portal);
  642. portal->shape=p_shape;
  643. portal->bounds=Rect2();
  644. for(int i=0;i<p_shape.size();i++) {
  645. if (i==0)
  646. portal->bounds.pos=p_shape[i];
  647. else
  648. portal->bounds.expand_to(p_shape[i]);
  649. }
  650. _dependency_queue_update(p_portal,true);
  651. }
  652. Vector<Point2> VisualServerRaster::portal_get_shape(RID p_portal) const {
  653. Portal *portal = portal_owner.get(p_portal);
  654. ERR_FAIL_COND_V(!portal, Vector<Point2>());
  655. return portal->shape;
  656. }
  657. void VisualServerRaster::portal_set_enabled(RID p_portal, bool p_enabled) {
  658. VS_CHANGED;
  659. Portal *portal = portal_owner.get(p_portal);
  660. ERR_FAIL_COND(!portal);
  661. portal->enabled=p_enabled;
  662. }
  663. bool VisualServerRaster::portal_is_enabled(RID p_portal) const {
  664. Portal *portal = portal_owner.get(p_portal);
  665. ERR_FAIL_COND_V(!portal, false);
  666. return portal->enabled;
  667. }
  668. void VisualServerRaster::portal_set_disable_distance(RID p_portal, float p_distance) {
  669. VS_CHANGED;
  670. Portal *portal = portal_owner.get(p_portal);
  671. ERR_FAIL_COND(!portal);
  672. portal->disable_distance=p_distance;
  673. }
  674. float VisualServerRaster::portal_get_disable_distance(RID p_portal) const {
  675. Portal *portal = portal_owner.get(p_portal);
  676. ERR_FAIL_COND_V(!portal, -1);
  677. return portal->disable_distance;
  678. }
  679. void VisualServerRaster::portal_set_disabled_color(RID p_portal, const Color& p_color) {
  680. VS_CHANGED;
  681. Portal *portal = portal_owner.get(p_portal);
  682. ERR_FAIL_COND(!portal);
  683. portal->disable_color=p_color;
  684. }
  685. Color VisualServerRaster::portal_get_disabled_color(RID p_portal) const {
  686. Portal *portal = portal_owner.get(p_portal);
  687. ERR_FAIL_COND_V(!portal, Color());
  688. return portal->disable_color;
  689. }
  690. void VisualServerRaster::portal_set_connect_range(RID p_portal, float p_range) {
  691. VS_CHANGED;
  692. Portal *portal = portal_owner.get(p_portal);
  693. ERR_FAIL_COND(!portal);
  694. portal->connect_range=p_range;
  695. _dependency_queue_update(p_portal,true);
  696. }
  697. float VisualServerRaster::portal_get_connect_range(RID p_portal) const {
  698. Portal *portal = portal_owner.get(p_portal);
  699. ERR_FAIL_COND_V(!portal,0);
  700. return portal->connect_range;
  701. }
  702. RID VisualServerRaster::baked_light_create() {
  703. BakedLight *baked_light = memnew( BakedLight );
  704. ERR_FAIL_COND_V(!baked_light,RID());
  705. baked_light->data.mode=BAKED_LIGHT_OCTREE;
  706. baked_light->data.octree_lattice_size=0;
  707. baked_light->data.octree_lattice_divide=0;
  708. baked_light->data.octree_steps=1;
  709. baked_light->data.lightmap_multiplier=1.0;
  710. baked_light->data.realtime_color_enabled=false;
  711. baked_light->data.realtime_color=Color(1.0, 1.0, 1.0);
  712. baked_light->data.realtime_energy = 1.0;
  713. return baked_light_owner.make_rid( baked_light );
  714. }
  715. void VisualServerRaster::baked_light_set_mode(RID p_baked_light,BakedLightMode p_mode){
  716. VS_CHANGED;
  717. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  718. ERR_FAIL_COND(!baked_light);
  719. baked_light->data.mode=p_mode;
  720. baked_light->data.color_multiplier=1.0;
  721. _dependency_queue_update(p_baked_light,true);
  722. }
  723. VisualServer::BakedLightMode VisualServerRaster::baked_light_get_mode(RID p_baked_light) const{
  724. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  725. ERR_FAIL_COND_V(!baked_light,BAKED_LIGHT_OCTREE);
  726. return baked_light->data.mode;
  727. }
  728. void VisualServerRaster::baked_light_set_lightmap_multiplier(RID p_baked_light,float p_multiplier) {
  729. VS_CHANGED;
  730. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  731. ERR_FAIL_COND(!baked_light);
  732. baked_light->data.lightmap_multiplier=p_multiplier;
  733. }
  734. float VisualServerRaster::baked_light_get_lightmap_multiplier(RID p_baked_light) const{
  735. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  736. ERR_FAIL_COND_V(!baked_light,0);
  737. return baked_light->data.lightmap_multiplier;
  738. }
  739. void VisualServerRaster::baked_light_set_octree(RID p_baked_light,const DVector<uint8_t> p_octree){
  740. VS_CHANGED;
  741. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  742. ERR_FAIL_COND(!baked_light);
  743. if (p_octree.size()==0) {
  744. if (baked_light->data.octree_texture.is_valid())
  745. rasterizer->free(baked_light->data.octree_texture);
  746. baked_light->data.octree_texture=RID();
  747. baked_light->octree_aabb=AABB();
  748. baked_light->octree_tex_size=Size2();
  749. } else {
  750. int tex_w;
  751. int tex_h;
  752. int light_tex_w;
  753. int light_tex_h;
  754. bool is16;
  755. bool has_light_tex=false;
  756. {
  757. DVector<uint8_t>::Read r=p_octree.read();
  758. tex_w = decode_uint32(&r[0]);
  759. tex_h = decode_uint32(&r[4]);
  760. print_line("TEX W: "+itos(tex_w)+" TEX H:"+itos(tex_h)+" LEN: "+itos(p_octree.size()));
  761. is16=decode_uint32(&r[8]);
  762. baked_light->data.octree_lattice_size=decode_float(&r[12]);
  763. baked_light->data.octree_lattice_divide=tex_w/4.0;
  764. print_line("LATTICE SIZE: "+rtos(baked_light->data.octree_lattice_size));
  765. print_line("LATTICE DIVIDE: "+rtos(baked_light->data.octree_lattice_divide));
  766. baked_light->data.octree_steps=decode_uint32(&r[16]);
  767. baked_light->data.octree_tex_pixel_size.x=1.0/tex_w;
  768. baked_light->data.octree_tex_pixel_size.y=1.0/tex_h;
  769. baked_light->data.texture_multiplier=decode_uint32(&r[20]);
  770. light_tex_w=decode_uint16(&r[24]);
  771. light_tex_h=decode_uint16(&r[26]);
  772. print_line("ltexw "+itos(light_tex_w));
  773. print_line("ltexh "+itos(light_tex_h));
  774. if (light_tex_w>0 && light_tex_h>0) {
  775. baked_light->data.light_tex_pixel_size.x=1.0/light_tex_w;
  776. baked_light->data.light_tex_pixel_size.y=1.0/light_tex_h;
  777. has_light_tex=true;
  778. } else {
  779. baked_light->data.light_tex_pixel_size=baked_light->data.octree_tex_pixel_size;
  780. }
  781. baked_light->octree_aabb.pos.x=decode_float(&r[32]);
  782. baked_light->octree_aabb.pos.y=decode_float(&r[36]);
  783. baked_light->octree_aabb.pos.z=decode_float(&r[40]);
  784. baked_light->octree_aabb.size.x=decode_float(&r[44]);
  785. baked_light->octree_aabb.size.y=decode_float(&r[48]);
  786. baked_light->octree_aabb.size.z=decode_float(&r[52]);
  787. }
  788. if (baked_light->data.octree_texture.is_valid()) {
  789. if (tex_w!=baked_light->octree_tex_size.x || tex_h!=baked_light->octree_tex_size.y) {
  790. rasterizer->free(baked_light->data.octree_texture);
  791. baked_light->data.octree_texture=RID();
  792. baked_light->octree_tex_size.x=0;
  793. baked_light->octree_tex_size.y=0;
  794. }
  795. }
  796. if (baked_light->data.light_texture.is_valid()) {
  797. if (!has_light_tex || light_tex_w!=baked_light->light_tex_size.x || light_tex_h!=baked_light->light_tex_size.y) {
  798. rasterizer->free(baked_light->data.light_texture);
  799. baked_light->data.light_texture=RID();
  800. baked_light->light_tex_size.x=0;
  801. baked_light->light_tex_size.y=0;
  802. }
  803. }
  804. if (!baked_light->data.octree_texture.is_valid()) {
  805. baked_light->data.octree_texture=rasterizer->texture_create();
  806. rasterizer->texture_allocate(baked_light->data.octree_texture,tex_w,tex_h,Image::FORMAT_RGBA,TEXTURE_FLAG_FILTER);
  807. baked_light->octree_tex_size.x=tex_w;
  808. baked_light->octree_tex_size.y=tex_h;
  809. }
  810. if (!baked_light->data.light_texture.is_valid() && has_light_tex) {
  811. baked_light->data.light_texture=rasterizer->texture_create();
  812. rasterizer->texture_allocate(baked_light->data.light_texture,light_tex_w,light_tex_h,Image::FORMAT_RGBA,TEXTURE_FLAG_FILTER);
  813. baked_light->light_tex_size.x=light_tex_w;
  814. baked_light->light_tex_size.y=light_tex_h;
  815. }
  816. Image img(tex_w,tex_h,0,Image::FORMAT_RGBA,p_octree);
  817. rasterizer->texture_set_data(baked_light->data.octree_texture,img);
  818. }
  819. _dependency_queue_update(p_baked_light,true);
  820. }
  821. DVector<uint8_t> VisualServerRaster::baked_light_get_octree(RID p_baked_light) const{
  822. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  823. ERR_FAIL_COND_V(!baked_light,DVector<uint8_t>());
  824. if (rasterizer->is_texture(baked_light->data.octree_texture)) {
  825. Image img = rasterizer->texture_get_data(baked_light->data.octree_texture);
  826. return img.get_data();
  827. } else {
  828. return DVector<uint8_t>();
  829. }
  830. }
  831. void VisualServerRaster::baked_light_set_light(RID p_baked_light,const DVector<uint8_t> p_light) {
  832. VS_CHANGED;
  833. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  834. ERR_FAIL_COND(!baked_light);
  835. ERR_FAIL_COND(p_light.size()==0);
  836. int tex_w=baked_light->light_tex_size.x;
  837. int tex_h=baked_light->light_tex_size.y;
  838. ERR_FAIL_COND(tex_w==0 && tex_h==0);
  839. ERR_FAIL_COND(!baked_light->data.light_texture.is_valid());
  840. print_line("w: "+itos(tex_w)+" h: "+itos(tex_h)+" lightsize: "+itos(p_light.size()));
  841. Image img(tex_w,tex_h,0,Image::FORMAT_RGBA,p_light);
  842. rasterizer->texture_set_data(baked_light->data.light_texture,img);
  843. }
  844. DVector<uint8_t> VisualServerRaster::baked_light_get_light(RID p_baked_light) const{
  845. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  846. ERR_FAIL_COND_V(!baked_light,DVector<uint8_t>());
  847. if (rasterizer->is_texture(baked_light->data.light_texture)) {
  848. Image img = rasterizer->texture_get_data(baked_light->data.light_texture);
  849. return img.get_data();
  850. } else {
  851. return DVector<uint8_t>();
  852. }
  853. }
  854. void VisualServerRaster::baked_light_set_sampler_octree(RID p_baked_light, const DVector<int> &p_sampler) {
  855. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  856. ERR_FAIL_COND(!baked_light);
  857. baked_light->sampler=p_sampler;
  858. }
  859. DVector<int> VisualServerRaster::baked_light_get_sampler_octree(RID p_baked_light) const {
  860. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  861. ERR_FAIL_COND_V(!baked_light,DVector<int>());
  862. return baked_light->sampler;
  863. }
  864. void VisualServerRaster::baked_light_add_lightmap(RID p_baked_light,const RID p_texture,int p_id){
  865. VS_CHANGED;
  866. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  867. ERR_FAIL_COND(!baked_light);
  868. baked_light->data.lightmaps.insert(p_id,p_texture);
  869. }
  870. void VisualServerRaster::baked_light_clear_lightmaps(RID p_baked_light){
  871. VS_CHANGED;
  872. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  873. ERR_FAIL_COND(!baked_light);
  874. baked_light->data.lightmaps.clear();
  875. }
  876. void VisualServerRaster::baked_light_set_realtime_color_enabled(RID p_baked_light, const bool p_enabled) {
  877. VS_CHANGED;
  878. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  879. ERR_FAIL_COND(!baked_light);
  880. baked_light->data.realtime_color_enabled = p_enabled;
  881. }
  882. bool VisualServerRaster::baked_light_get_realtime_color_enabled(RID p_baked_light) const{
  883. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  884. ERR_FAIL_COND_V(!baked_light, false);
  885. return baked_light->data.realtime_color_enabled;
  886. }
  887. void VisualServerRaster::baked_light_set_realtime_color(RID p_baked_light, const Color& p_color) {
  888. VS_CHANGED;
  889. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  890. ERR_FAIL_COND(!baked_light);
  891. baked_light->data.realtime_color = p_color;
  892. }
  893. Color VisualServerRaster::baked_light_get_realtime_color(RID p_baked_light) const{
  894. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  895. ERR_FAIL_COND_V(!baked_light, Color(1.0, 1.0, 1.0));
  896. return baked_light->data.realtime_color;
  897. }
  898. void VisualServerRaster::baked_light_set_realtime_energy(RID p_baked_light, const float p_energy) {
  899. VS_CHANGED;
  900. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  901. ERR_FAIL_COND(!baked_light);
  902. baked_light->data.realtime_energy = p_energy;
  903. }
  904. float VisualServerRaster::baked_light_get_realtime_energy(RID p_baked_light) const{
  905. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  906. ERR_FAIL_COND_V(!baked_light, 1.0f);
  907. return baked_light->data.realtime_energy;
  908. }
  909. /* BAKED LIGHT SAMPLER */
  910. RID VisualServerRaster::baked_light_sampler_create() {
  911. BakedLightSampler * blsamp = memnew( BakedLightSampler );
  912. RID rid = baked_light_sampler_owner.make_rid(blsamp);
  913. _update_baked_light_sampler_dp_cache(blsamp);
  914. return rid;
  915. }
  916. void VisualServerRaster::baked_light_sampler_set_param(RID p_baked_light_sampler,BakedLightSamplerParam p_param,float p_value){
  917. VS_CHANGED;
  918. BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler);
  919. ERR_FAIL_COND(!blsamp);
  920. ERR_FAIL_INDEX(p_param,BAKED_LIGHT_SAMPLER_MAX);
  921. blsamp->params[p_param]=p_value;
  922. _dependency_queue_update(p_baked_light_sampler,true);
  923. }
  924. float VisualServerRaster::baked_light_sampler_get_param(RID p_baked_light_sampler,BakedLightSamplerParam p_param) const{
  925. BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler);
  926. ERR_FAIL_COND_V(!blsamp,0);
  927. ERR_FAIL_INDEX_V(p_param,BAKED_LIGHT_SAMPLER_MAX,0);
  928. return blsamp->params[p_param];
  929. }
  930. void VisualServerRaster::_update_baked_light_sampler_dp_cache(BakedLightSampler * blsamp) {
  931. int res = blsamp->resolution;
  932. blsamp->dp_cache.resize(res*res*2);
  933. Vector3 *dp_normals=blsamp->dp_cache.ptr();
  934. for(int p=0;p<2;p++) {
  935. float sign = p==0?1:-1;
  936. int ofs = res*res*p;
  937. for(int i=0;i<res;i++) {
  938. for(int j=0;j<res;j++) {
  939. Vector2 v(
  940. (i/float(res))*2.0-1.0,
  941. (j/float(res))*2.0-1.0
  942. );
  943. float l=v.length();
  944. if (l>1.0) {
  945. v/=l;
  946. l=1.0; //clamp to avoid imaginary
  947. }
  948. v*=(2*l)/(l*l+1); //inverse of the dual paraboloid function
  949. Vector3 n = Vector3(v.x,v.y,sign*sqrtf(MAX(1 - v.dot(v),0))); //reconstruction of z
  950. n.y*=sign;
  951. dp_normals[j*res+i+ofs]=n;
  952. }
  953. }
  954. }
  955. }
  956. void VisualServerRaster::baked_light_sampler_set_resolution(RID p_baked_light_sampler,int p_resolution){
  957. ERR_FAIL_COND(p_resolution<4 || p_resolution>64);
  958. VS_CHANGED;
  959. BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler);
  960. ERR_FAIL_COND(!blsamp);
  961. blsamp->resolution=p_resolution;
  962. _update_baked_light_sampler_dp_cache(blsamp);
  963. }
  964. int VisualServerRaster::baked_light_sampler_get_resolution(RID p_baked_light_sampler) const{
  965. BakedLightSampler * blsamp = baked_light_sampler_owner.get(p_baked_light_sampler);
  966. ERR_FAIL_COND_V(!blsamp,0);
  967. return blsamp->resolution;
  968. }
  969. /* CAMERA API */
  970. RID VisualServerRaster::camera_create() {
  971. Camera * camera = memnew( Camera );
  972. return camera_owner.make_rid( camera );
  973. }
  974. void VisualServerRaster::camera_set_perspective(RID p_camera,float p_fovy_degrees, float p_z_near, float p_z_far) {
  975. VS_CHANGED
  976. Camera *camera = camera_owner.get( p_camera );
  977. ERR_FAIL_COND(!camera);
  978. camera->type=Camera::PERSPECTIVE;
  979. camera->fov=p_fovy_degrees;
  980. camera->znear=p_z_near;
  981. camera->zfar=p_z_far;
  982. }
  983. void VisualServerRaster::camera_set_orthogonal(RID p_camera,float p_size, float p_z_near, float p_z_far) {
  984. VS_CHANGED;
  985. Camera *camera = camera_owner.get( p_camera );
  986. ERR_FAIL_COND(!camera);
  987. camera->type=Camera::ORTHOGONAL;
  988. camera->size=p_size;
  989. camera->znear=p_z_near;
  990. camera->zfar=p_z_far;
  991. }
  992. void VisualServerRaster::camera_set_transform(RID p_camera,const Transform& p_transform) {
  993. VS_CHANGED;
  994. Camera *camera = camera_owner.get( p_camera );
  995. ERR_FAIL_COND(!camera);
  996. camera->transform=p_transform.orthonormalized();
  997. }
  998. void VisualServerRaster::camera_set_visible_layers(RID p_camera,uint32_t p_layers) {
  999. VS_CHANGED;
  1000. Camera *camera = camera_owner.get( p_camera );
  1001. ERR_FAIL_COND(!camera);
  1002. camera->visible_layers=p_layers;
  1003. }
  1004. uint32_t VisualServerRaster::camera_get_visible_layers(RID p_camera) const{
  1005. const Camera *camera = camera_owner.get( p_camera );
  1006. ERR_FAIL_COND_V(!camera,0);
  1007. return camera->visible_layers;
  1008. }
  1009. void VisualServerRaster::camera_set_environment(RID p_camera,RID p_env) {
  1010. Camera *camera = camera_owner.get( p_camera );
  1011. ERR_FAIL_COND(!camera);
  1012. camera->env=p_env;
  1013. }
  1014. RID VisualServerRaster::camera_get_environment(RID p_camera) const {
  1015. const Camera *camera = camera_owner.get( p_camera );
  1016. ERR_FAIL_COND_V(!camera,RID());
  1017. return camera->env;
  1018. }
  1019. void VisualServerRaster::camera_set_use_vertical_aspect(RID p_camera,bool p_enable) {
  1020. Camera *camera = camera_owner.get( p_camera );
  1021. ERR_FAIL_COND(!camera);
  1022. camera->vaspect=p_enable;
  1023. }
  1024. bool VisualServerRaster::camera_is_using_vertical_aspect(RID p_camera,bool p_enable) const{
  1025. const Camera *camera = camera_owner.get( p_camera );
  1026. ERR_FAIL_COND_V(!camera,false);
  1027. return camera->vaspect;
  1028. }
  1029. /* VIEWPORT API */
  1030. RID VisualServerRaster::viewport_create() {
  1031. Viewport *viewport = memnew( Viewport );
  1032. RID rid = viewport_owner.make_rid( viewport );
  1033. ERR_FAIL_COND_V( !rid.is_valid(), rid );
  1034. viewport->self=rid;
  1035. viewport->hide_scenario=false;
  1036. viewport->hide_canvas=false;
  1037. viewport->viewport_data=rasterizer->viewport_data_create();
  1038. return rid;
  1039. }
  1040. void VisualServerRaster::viewport_attach_to_screen(RID p_viewport,int p_screen) {
  1041. VS_CHANGED;
  1042. Viewport *viewport = viewport_owner.get( p_viewport );
  1043. ERR_FAIL_COND(!viewport);
  1044. screen_viewports[p_viewport]=p_screen;
  1045. }
  1046. void VisualServerRaster::viewport_detach(RID p_viewport) {
  1047. VS_CHANGED;
  1048. Viewport *viewport = viewport_owner.get( p_viewport );
  1049. ERR_FAIL_COND(!viewport);
  1050. ERR_FAIL_COND(!screen_viewports.has(p_viewport));
  1051. screen_viewports.erase(p_viewport);
  1052. }
  1053. void VisualServerRaster::viewport_set_as_render_target(RID p_viewport,bool p_enable) {
  1054. VS_CHANGED;
  1055. Viewport *viewport = viewport_owner.get( p_viewport );
  1056. ERR_FAIL_COND(!viewport);
  1057. if (viewport->render_target.is_valid()==p_enable)
  1058. return;
  1059. if (!p_enable) {
  1060. rasterizer->free(viewport->render_target);
  1061. viewport->render_target=RID();
  1062. viewport->render_target_texture=RID();
  1063. if (viewport->update_list.in_list())
  1064. viewport_update_list.remove(&viewport->update_list);
  1065. } else {
  1066. viewport->render_target=rasterizer->render_target_create();
  1067. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  1068. viewport->render_target_texture=rasterizer->render_target_get_texture(viewport->render_target);
  1069. if (viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  1070. viewport_update_list.add(&viewport->update_list);
  1071. }
  1072. }
  1073. void VisualServerRaster::viewport_set_render_target_update_mode(RID p_viewport,RenderTargetUpdateMode p_mode){
  1074. VS_CHANGED;
  1075. Viewport *viewport = viewport_owner.get( p_viewport );
  1076. ERR_FAIL_COND(!viewport);
  1077. if (viewport->render_target.is_valid() && viewport->update_list.in_list())
  1078. viewport_update_list.remove(&viewport->update_list);
  1079. viewport->render_target_update_mode=p_mode;
  1080. if (viewport->render_target.is_valid() &&viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  1081. viewport_update_list.add(&viewport->update_list);
  1082. }
  1083. VisualServer::RenderTargetUpdateMode VisualServerRaster::viewport_get_render_target_update_mode(RID p_viewport) const{
  1084. const Viewport *viewport = viewport_owner.get( p_viewport );
  1085. ERR_FAIL_COND_V(!viewport,RENDER_TARGET_UPDATE_DISABLED);
  1086. return viewport->render_target_update_mode;
  1087. }
  1088. RID VisualServerRaster::viewport_get_render_target_texture(RID p_viewport) const{
  1089. Viewport *viewport = viewport_owner.get( p_viewport );
  1090. ERR_FAIL_COND_V(!viewport,RID());
  1091. return viewport->render_target_texture;
  1092. }
  1093. void VisualServerRaster::viewport_set_render_target_vflip(RID p_viewport,bool p_enable) {
  1094. Viewport *viewport = viewport_owner.get( p_viewport );
  1095. ERR_FAIL_COND(!viewport);
  1096. viewport->render_target_vflip=p_enable;
  1097. }
  1098. void VisualServerRaster::viewport_set_render_target_clear_on_new_frame(RID p_viewport,bool p_enable) {
  1099. Viewport *viewport = viewport_owner.get( p_viewport );
  1100. ERR_FAIL_COND(!viewport);
  1101. viewport->render_target_clear_on_new_frame=p_enable;
  1102. }
  1103. void VisualServerRaster::viewport_set_render_target_to_screen_rect(RID p_viewport,const Rect2& p_rect) {
  1104. Viewport *viewport = viewport_owner.get( p_viewport );
  1105. ERR_FAIL_COND(!viewport);
  1106. viewport->rt_to_screen_rect=p_rect;
  1107. }
  1108. bool VisualServerRaster::viewport_get_render_target_vflip(RID p_viewport) const{
  1109. const Viewport *viewport = viewport_owner.get( p_viewport );
  1110. ERR_FAIL_COND_V(!viewport,false);
  1111. return viewport->render_target_vflip;
  1112. }
  1113. bool VisualServerRaster::viewport_get_render_target_clear_on_new_frame(RID p_viewport) const{
  1114. const Viewport *viewport = viewport_owner.get( p_viewport );
  1115. ERR_FAIL_COND_V(!viewport,false);
  1116. return viewport->render_target_clear_on_new_frame;
  1117. }
  1118. void VisualServerRaster::viewport_render_target_clear(RID p_viewport) {
  1119. Viewport *viewport = viewport_owner.get( p_viewport );
  1120. ERR_FAIL_COND(!viewport);
  1121. viewport->render_target_clear=true;
  1122. }
  1123. void VisualServerRaster::viewport_queue_screen_capture(RID p_viewport) {
  1124. VS_CHANGED;
  1125. Viewport *viewport = viewport_owner.get( p_viewport );
  1126. ERR_FAIL_COND(!viewport);
  1127. viewport->queue_capture=true;
  1128. }
  1129. Image VisualServerRaster::viewport_get_screen_capture(RID p_viewport) const {
  1130. Viewport *viewport = (Viewport*)viewport_owner.get( p_viewport );
  1131. ERR_FAIL_COND_V(!viewport,Image());
  1132. Image ret = viewport->capture;
  1133. viewport->capture=Image();
  1134. return ret;
  1135. }
  1136. void VisualServerRaster::viewport_set_rect(RID p_viewport,const ViewportRect& p_rect) {
  1137. VS_CHANGED;
  1138. Viewport *viewport=NULL;
  1139. viewport = viewport_owner.get( p_viewport );
  1140. ERR_FAIL_COND(!viewport);
  1141. viewport->rect=p_rect;
  1142. if (viewport->render_target.is_valid()) {
  1143. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  1144. }
  1145. }
  1146. VisualServer::ViewportRect VisualServerRaster::viewport_get_rect(RID p_viewport) const {
  1147. const Viewport *viewport=NULL;
  1148. viewport = viewport_owner.get( p_viewport );
  1149. ERR_FAIL_COND_V(!viewport, ViewportRect());
  1150. return viewport->rect;
  1151. }
  1152. void VisualServerRaster::viewport_set_hide_scenario(RID p_viewport,bool p_hide) {
  1153. VS_CHANGED;
  1154. Viewport *viewport=NULL;
  1155. viewport = viewport_owner.get( p_viewport );
  1156. ERR_FAIL_COND(!viewport);
  1157. viewport->hide_scenario=p_hide;
  1158. }
  1159. void VisualServerRaster::viewport_set_hide_canvas(RID p_viewport,bool p_hide) {
  1160. VS_CHANGED;
  1161. Viewport *viewport=NULL;
  1162. viewport = viewport_owner.get( p_viewport );
  1163. ERR_FAIL_COND(!viewport);
  1164. viewport->hide_canvas=p_hide;
  1165. }
  1166. void VisualServerRaster::viewport_set_disable_environment(RID p_viewport,bool p_disable) {
  1167. VS_CHANGED;
  1168. Viewport *viewport=NULL;
  1169. viewport = viewport_owner.get( p_viewport );
  1170. ERR_FAIL_COND(!viewport);
  1171. viewport->disable_environment=p_disable;
  1172. }
  1173. void VisualServerRaster::viewport_attach_camera(RID p_viewport,RID p_camera) {
  1174. VS_CHANGED;
  1175. Viewport *viewport=NULL;
  1176. viewport = viewport_owner.get( p_viewport );
  1177. ERR_FAIL_COND(!viewport);
  1178. if (p_camera.is_valid()) {
  1179. ERR_FAIL_COND(!camera_owner.owns(p_camera));
  1180. // a camera
  1181. viewport->camera=p_camera;
  1182. } else {
  1183. viewport->camera=RID();
  1184. }
  1185. }
  1186. void VisualServerRaster::viewport_set_scenario(RID p_viewport,RID p_scenario) {
  1187. VS_CHANGED;
  1188. Viewport *viewport=NULL;
  1189. viewport = viewport_owner.get( p_viewport );
  1190. ERR_FAIL_COND(!viewport);
  1191. if (p_scenario.is_valid()) {
  1192. ERR_FAIL_COND(!scenario_owner.owns(p_scenario));
  1193. // a camera
  1194. viewport->scenario=p_scenario;
  1195. } else {
  1196. viewport->scenario=RID();
  1197. }
  1198. }
  1199. RID VisualServerRaster::viewport_get_attached_camera(RID p_viewport) const {
  1200. const Viewport *viewport=NULL;
  1201. viewport = viewport_owner.get( p_viewport );
  1202. ERR_FAIL_COND_V(!viewport, RID());
  1203. return viewport->camera;
  1204. }
  1205. void VisualServerRaster::viewport_attach_canvas(RID p_viewport,RID p_canvas) {
  1206. VS_CHANGED;
  1207. Viewport *viewport=NULL;
  1208. viewport = viewport_owner.get( p_viewport );
  1209. ERR_FAIL_COND(!viewport);
  1210. Canvas *canvas = canvas_owner.get( p_canvas );
  1211. ERR_FAIL_COND(!canvas);
  1212. ERR_EXPLAIN("Canvas already attached.");
  1213. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  1214. Viewport::CanvasData cd;
  1215. cd.canvas=canvas;
  1216. cd.layer=0;
  1217. viewport->canvas_map[p_canvas]=cd;
  1218. canvas->viewports.insert(p_viewport);
  1219. }
  1220. void VisualServerRaster::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_transform) {
  1221. VS_CHANGED;
  1222. Viewport *viewport=NULL;
  1223. viewport = viewport_owner.get( p_viewport );
  1224. ERR_FAIL_COND(!viewport);
  1225. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1226. if (!E) {
  1227. ERR_EXPLAIN("Viewport does not contain the canvas");
  1228. ERR_FAIL_COND(!E);
  1229. }
  1230. E->get().transform=p_transform;
  1231. }
  1232. Matrix32 VisualServerRaster::viewport_get_canvas_transform(RID p_viewport,RID p_canvas) const {
  1233. Viewport *viewport=NULL;
  1234. viewport = viewport_owner.get( p_viewport );
  1235. ERR_FAIL_COND_V(!viewport,Matrix32());
  1236. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1237. if (!E) {
  1238. ERR_EXPLAIN("Viewport does not contain the canvas");
  1239. ERR_FAIL_COND_V(!E,Matrix32());
  1240. }
  1241. return E->get().transform;
  1242. }
  1243. void VisualServerRaster::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform) {
  1244. VS_CHANGED
  1245. Viewport *viewport=NULL;
  1246. viewport = viewport_owner.get( p_viewport );
  1247. ERR_FAIL_COND(!viewport);
  1248. viewport->global_transform=p_transform;
  1249. }
  1250. Matrix32 VisualServerRaster::viewport_get_global_canvas_transform(RID p_viewport) const{
  1251. Viewport *viewport=NULL;
  1252. viewport = viewport_owner.get( p_viewport );
  1253. ERR_FAIL_COND_V(!viewport,Matrix32());
  1254. return viewport->global_transform;
  1255. }
  1256. void VisualServerRaster::viewport_remove_canvas(RID p_viewport,RID p_canvas) {
  1257. VS_CHANGED;
  1258. Viewport *viewport=NULL;
  1259. viewport = viewport_owner.get( p_viewport );
  1260. ERR_FAIL_COND(!viewport);
  1261. Canvas *canvas = canvas_owner.get( p_canvas );
  1262. ERR_FAIL_COND(!canvas);
  1263. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1264. if (!E) {
  1265. ERR_EXPLAIN("Viewport does not contain the canvas");
  1266. ERR_FAIL_COND(!E);
  1267. }
  1268. canvas->viewports.erase(p_viewport);
  1269. viewport->canvas_map.erase(E);
  1270. }
  1271. void VisualServerRaster::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer) {
  1272. VS_CHANGED;
  1273. Viewport *viewport=NULL;
  1274. viewport = viewport_owner.get( p_viewport );
  1275. ERR_FAIL_COND(!viewport);
  1276. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1277. if (!E) {
  1278. ERR_EXPLAIN("Viewport does not contain the canvas");
  1279. ERR_FAIL_COND(!E);
  1280. }
  1281. E->get().layer=p_layer;
  1282. }
  1283. void VisualServerRaster::viewport_set_transparent_background(RID p_viewport,bool p_enabled) {
  1284. VS_CHANGED;
  1285. Viewport *viewport=viewport_owner.get( p_viewport );
  1286. ERR_FAIL_COND(!viewport);
  1287. viewport->transparent_bg=p_enabled;
  1288. }
  1289. bool VisualServerRaster::viewport_has_transparent_background(RID p_viewport) const {
  1290. Viewport *viewport=viewport_owner.get( p_viewport );
  1291. ERR_FAIL_COND_V(!viewport, false);
  1292. return viewport->transparent_bg;
  1293. }
  1294. RID VisualServerRaster::viewport_get_scenario(RID p_viewport) const {
  1295. const Viewport *viewport=NULL;
  1296. viewport = viewport_owner.get( p_viewport );
  1297. ERR_FAIL_COND_V(!viewport, RID());
  1298. return viewport->scenario;
  1299. }
  1300. RID VisualServerRaster::environment_create() {
  1301. return rasterizer->environment_create();
  1302. }
  1303. void VisualServerRaster::environment_set_background(RID p_env,EnvironmentBG p_bg){
  1304. rasterizer->environment_set_background(p_env,p_bg);
  1305. }
  1306. VisualServer::EnvironmentBG VisualServerRaster::environment_get_background(RID p_env) const{
  1307. return rasterizer->environment_get_background(p_env);
  1308. }
  1309. void VisualServerRaster::environment_set_background_param(RID p_env,EnvironmentBGParam p_param, const Variant& p_value){
  1310. rasterizer->environment_set_background_param(p_env,p_param,p_value);
  1311. }
  1312. Variant VisualServerRaster::environment_get_background_param(RID p_env,EnvironmentBGParam p_param) const{
  1313. return rasterizer->environment_get_background_param(p_env,p_param);
  1314. }
  1315. void VisualServerRaster::environment_set_enable_fx(RID p_env,EnvironmentFx p_effect,bool p_enabled){
  1316. rasterizer->environment_set_enable_fx(p_env,p_effect,p_enabled);
  1317. }
  1318. bool VisualServerRaster::environment_is_fx_enabled(RID p_env,EnvironmentFx p_effect) const{
  1319. return rasterizer->environment_is_fx_enabled(p_env,p_effect);
  1320. }
  1321. void VisualServerRaster::environment_fx_set_param(RID p_env,EnvironmentFxParam p_param,const Variant& p_value){
  1322. rasterizer->environment_fx_set_param(p_env,p_param,p_value);
  1323. }
  1324. Variant VisualServerRaster::environment_fx_get_param(RID p_env,EnvironmentFxParam p_param) const {
  1325. return environment_fx_get_param(p_env,p_param);
  1326. }
  1327. /* SCENARIO API */
  1328. void VisualServerRaster::_dependency_queue_update(RID p_rid,bool p_update_aabb,bool p_update_materials) {
  1329. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  1330. if (!E)
  1331. return;
  1332. Set<RID>::Element *I = E->get().front();
  1333. while(I) {
  1334. Instance *ins = instance_owner.get( I->get() );
  1335. _instance_queue_update( ins , p_update_aabb, p_update_materials );
  1336. I = I->next();
  1337. }
  1338. }
  1339. void VisualServerRaster::_instance_queue_update(Instance *p_instance,bool p_update_aabb,bool p_update_materials) {
  1340. if (p_update_aabb)
  1341. p_instance->update_aabb=true;
  1342. if (p_update_materials)
  1343. p_instance->update_materials=true;
  1344. if (p_instance->update)
  1345. return;
  1346. p_instance->update_next=instance_update_list;
  1347. instance_update_list=p_instance;
  1348. p_instance->update=true;
  1349. }
  1350. RID VisualServerRaster::scenario_create() {
  1351. Scenario *scenario = memnew( Scenario );
  1352. ERR_FAIL_COND_V(!scenario,RID());
  1353. RID scenario_rid = scenario_owner.make_rid( scenario );
  1354. scenario->self=scenario_rid;
  1355. scenario->octree.set_pair_callback(instance_pair,this);
  1356. scenario->octree.set_unpair_callback(instance_unpair,this);
  1357. return scenario_rid;
  1358. }
  1359. void VisualServerRaster::scenario_set_debug(RID p_scenario,ScenarioDebugMode p_debug_mode) {
  1360. VS_CHANGED;
  1361. Scenario *scenario = scenario_owner.get(p_scenario);
  1362. ERR_FAIL_COND(!scenario);
  1363. scenario->debug=p_debug_mode;
  1364. }
  1365. void VisualServerRaster::scenario_set_environment(RID p_scenario, RID p_environment) {
  1366. VS_CHANGED;
  1367. Scenario *scenario = scenario_owner.get(p_scenario);
  1368. ERR_FAIL_COND(!scenario);
  1369. scenario->environment=p_environment;
  1370. }
  1371. void VisualServerRaster::scenario_set_fallback_environment(RID p_scenario, RID p_environment) {
  1372. VS_CHANGED;
  1373. Scenario *scenario = scenario_owner.get(p_scenario);
  1374. ERR_FAIL_COND(!scenario);
  1375. scenario->fallback_environment=p_environment;
  1376. }
  1377. RID VisualServerRaster::scenario_get_environment(RID p_scenario, RID p_environment) const{
  1378. const Scenario *scenario = scenario_owner.get(p_scenario);
  1379. ERR_FAIL_COND_V(!scenario,RID());
  1380. return scenario->environment;
  1381. }
  1382. /* INSTANCING API */
  1383. RID VisualServerRaster::instance_create() {
  1384. Instance *instance = memnew( Instance );
  1385. ERR_FAIL_COND_V(!instance,RID());
  1386. RID instance_rid = instance_owner.make_rid(instance);
  1387. instance->self=instance_rid;
  1388. instance->base_type=INSTANCE_NONE;
  1389. instance->scenario=NULL;
  1390. return instance_rid;
  1391. }
  1392. void VisualServerRaster::instance_set_base(RID p_instance, RID p_base) {
  1393. VS_CHANGED;
  1394. Instance *instance = instance_owner.get( p_instance );
  1395. ERR_FAIL_COND( !instance );
  1396. if (instance->base_type!=INSTANCE_NONE) {
  1397. //free anything related to that base
  1398. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( instance->base_rid );
  1399. if (E) {
  1400. // wtf, no E?
  1401. E->get().erase( instance->self );
  1402. } else {
  1403. ERR_PRINT("no base E? Bug?");
  1404. }
  1405. if ( instance->room ) {
  1406. instance_set_room(p_instance,RID());
  1407. /*
  1408. if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK)
  1409. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1410. else if (instance->base_type==INSTANCE_PORTAL) {
  1411. print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE)));
  1412. instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1413. } else if (instance->base_type==INSTANCE_ROOM)
  1414. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1415. else if (instance->base_type==INSTANCE_LIGHT)
  1416. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1417. instance->RE=NULL;*/
  1418. }
  1419. if (instance->light_info) {
  1420. if (instance->scenario && instance->light_info->D)
  1421. instance->scenario->directional_lights.erase( instance->light_info->D );
  1422. rasterizer->free(instance->light_info->instance);
  1423. memdelete(instance->light_info);
  1424. instance->light_info=NULL;
  1425. }
  1426. if (instance->portal_info) {
  1427. _portal_disconnect(instance,true);
  1428. memdelete(instance->portal_info);
  1429. instance->portal_info=NULL;
  1430. }
  1431. if (instance->baked_light_info) {
  1432. while(instance->baked_light_info->owned_instances.size()) {
  1433. Instance *owned=instance->baked_light_info->owned_instances.front()->get();
  1434. owned->baked_light=NULL;
  1435. owned->data.baked_light=NULL;
  1436. owned->data.baked_light_octree_xform=NULL;
  1437. owned->BLE=NULL;
  1438. instance->baked_light_info->owned_instances.pop_front();
  1439. }
  1440. memdelete(instance->baked_light_info);
  1441. instance->baked_light_info=NULL;
  1442. }
  1443. if (instance->scenario && instance->octree_id) {
  1444. instance->scenario->octree.erase( instance->octree_id );
  1445. instance->octree_id=0;
  1446. }
  1447. if (instance->room_info) {
  1448. for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) {
  1449. Instance *owned = E->get();
  1450. owned->room=NULL;
  1451. owned->RE=NULL;
  1452. }
  1453. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1454. _portal_disconnect(E->get(),true);
  1455. Instance *owned = E->get();
  1456. owned->room=NULL;
  1457. owned->RE=NULL;
  1458. }
  1459. for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) {
  1460. Instance *owned = E->get();
  1461. owned->room=NULL;
  1462. owned->RE=NULL;
  1463. }
  1464. if (instance->room_info->disconnected_child_portals.size()) {
  1465. ERR_PRINT("BUG: Disconnected portals remain!");
  1466. }
  1467. memdelete(instance->room_info);
  1468. instance->room_info=NULL;
  1469. }
  1470. if (instance->particles_info) {
  1471. rasterizer->free( instance->particles_info->instance );
  1472. memdelete(instance->particles_info);
  1473. instance->particles_info=NULL;
  1474. }
  1475. if (instance->baked_light_sampler_info) {
  1476. while (instance->baked_light_sampler_info->owned_instances.size()) {
  1477. instance_geometry_set_baked_light_sampler(instance->baked_light_sampler_info->owned_instances.front()->get()->self,RID());
  1478. }
  1479. if (instance->baked_light_sampler_info->sampled_light.is_valid()) {
  1480. rasterizer->free(instance->baked_light_sampler_info->sampled_light);
  1481. }
  1482. memdelete( instance->baked_light_sampler_info );
  1483. instance->baked_light_sampler_info=NULL;
  1484. }
  1485. instance->data.morph_values.clear();
  1486. instance->data.materials.clear();
  1487. }
  1488. instance->base_type=INSTANCE_NONE;
  1489. instance->base_rid=RID();
  1490. if (p_base.is_valid()) {
  1491. if (rasterizer->is_mesh(p_base)) {
  1492. instance->base_type=INSTANCE_MESH;
  1493. instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base));
  1494. instance->data.materials.resize( rasterizer->mesh_get_surface_count(p_base));
  1495. } else if (rasterizer->is_multimesh(p_base)) {
  1496. instance->base_type=INSTANCE_MULTIMESH;
  1497. } else if (rasterizer->is_immediate(p_base)) {
  1498. instance->base_type=INSTANCE_IMMEDIATE;
  1499. } else if (rasterizer->is_particles(p_base)) {
  1500. instance->base_type=INSTANCE_PARTICLES;
  1501. instance->particles_info=memnew( Instance::ParticlesInfo );
  1502. instance->particles_info->instance = rasterizer->particles_instance_create( p_base );
  1503. } else if (rasterizer->is_light(p_base)) {
  1504. instance->base_type=INSTANCE_LIGHT;
  1505. instance->light_info = memnew( Instance::LightInfo );
  1506. instance->light_info->instance = rasterizer->light_instance_create(p_base);
  1507. if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) {
  1508. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1509. }
  1510. } else if (room_owner.owns(p_base)) {
  1511. instance->base_type=INSTANCE_ROOM;
  1512. instance->room_info = memnew( Instance::RoomInfo );
  1513. instance->room_info->room=room_owner.get(p_base);
  1514. } else if (portal_owner.owns(p_base)) {
  1515. instance->base_type=INSTANCE_PORTAL;
  1516. instance->portal_info = memnew(Instance::PortalInfo);
  1517. instance->portal_info->portal=portal_owner.get(p_base);
  1518. } else if (baked_light_owner.owns(p_base)) {
  1519. instance->base_type=INSTANCE_BAKED_LIGHT;
  1520. instance->baked_light_info=memnew(Instance::BakedLightInfo);
  1521. instance->baked_light_info->baked_light=baked_light_owner.get(p_base);
  1522. //instance->portal_info = memnew(Instance::PortalInfo);
  1523. //instance->portal_info->portal=portal_owner.get(p_base);
  1524. } else if (baked_light_sampler_owner.owns(p_base)) {
  1525. instance->base_type=INSTANCE_BAKED_LIGHT_SAMPLER;
  1526. instance->baked_light_sampler_info=memnew( Instance::BakedLightSamplerInfo);
  1527. instance->baked_light_sampler_info->sampler=baked_light_sampler_owner.get(p_base);
  1528. //instance->portal_info = memnew(Instance::PortalInfo);
  1529. //instance->portal_info->portal=portal_owner.get(p_base);
  1530. } else {
  1531. ERR_EXPLAIN("Invalid base RID for instance!")
  1532. ERR_FAIL();
  1533. }
  1534. instance_dependency_map[ p_base ].insert( instance->self );
  1535. instance->base_rid=p_base;
  1536. if (instance->scenario)
  1537. _instance_queue_update(instance,true);
  1538. }
  1539. }
  1540. RID VisualServerRaster::instance_get_base(RID p_instance) const {
  1541. Instance *instance = instance_owner.get( p_instance );
  1542. ERR_FAIL_COND_V( !instance, RID() );
  1543. return instance->base_rid;
  1544. }
  1545. void VisualServerRaster::instance_set_scenario(RID p_instance, RID p_scenario) {
  1546. VS_CHANGED;
  1547. Instance *instance = instance_owner.get( p_instance );
  1548. ERR_FAIL_COND( !instance );
  1549. if (instance->scenario) {
  1550. Map< RID, Set<RID> >::Element *E = instance_dependency_map.find( instance->scenario->self );
  1551. if (E) {
  1552. // wtf, no E?
  1553. E->get().erase( instance->self );
  1554. } else {
  1555. ERR_PRINT("no scenario E? Bug?");
  1556. }
  1557. if (instance->light_info) {
  1558. if (instance->light_info->D)
  1559. instance->scenario->directional_lights.erase( instance->light_info->D );
  1560. }
  1561. if (instance->portal_info) {
  1562. _portal_disconnect(instance,true);
  1563. }
  1564. if (instance->octree_id) {
  1565. instance->scenario->octree.erase( instance->octree_id );
  1566. instance->octree_id=0;
  1567. }
  1568. instance->scenario=NULL;
  1569. }
  1570. if (p_scenario.is_valid()) {
  1571. Scenario *scenario = scenario_owner.get( p_scenario );
  1572. ERR_FAIL_COND(!scenario);
  1573. instance->scenario=scenario;
  1574. instance_dependency_map[ p_scenario ].insert( instance->self );
  1575. instance->scenario=scenario;
  1576. if (instance->base_type==INSTANCE_LIGHT && rasterizer->light_get_type(instance->base_rid)==LIGHT_DIRECTIONAL) {
  1577. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1578. }
  1579. _instance_queue_update(instance,true);
  1580. }
  1581. }
  1582. RID VisualServerRaster::instance_get_scenario(RID p_instance) const {
  1583. Instance *instance = instance_owner.get( p_instance );
  1584. ERR_FAIL_COND_V( !instance, RID() );
  1585. if (instance->scenario)
  1586. return instance->scenario->self;
  1587. else
  1588. return RID();
  1589. }
  1590. void VisualServerRaster::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  1591. VS_CHANGED;
  1592. Instance *instance = instance_owner.get( p_instance );
  1593. ERR_FAIL_COND( !instance );
  1594. instance->layer_mask=p_mask;
  1595. }
  1596. uint32_t VisualServerRaster::instance_get_layer_mask(RID p_instance) const{
  1597. Instance *instance = instance_owner.get( p_instance );
  1598. ERR_FAIL_COND_V( !instance, 0 );
  1599. return instance->layer_mask;
  1600. }
  1601. AABB VisualServerRaster::instance_get_base_aabb(RID p_instance) const {
  1602. Instance *instance = instance_owner.get( p_instance );
  1603. ERR_FAIL_COND_V( !instance, AABB() );
  1604. return instance->aabb;
  1605. }
  1606. void VisualServerRaster::instance_attach_object_instance_ID(RID p_instance,uint32_t p_ID) {
  1607. VS_CHANGED;
  1608. Instance *instance = instance_owner.get( p_instance );
  1609. ERR_FAIL_COND( !instance );
  1610. instance->object_ID=p_ID;
  1611. }
  1612. uint32_t VisualServerRaster::instance_get_object_instance_ID(RID p_instance) const {
  1613. Instance *instance = instance_owner.get( p_instance );
  1614. ERR_FAIL_COND_V( !instance, 0 );
  1615. return instance->object_ID;
  1616. }
  1617. void VisualServerRaster::instance_attach_skeleton(RID p_instance,RID p_skeleton) {
  1618. VS_CHANGED;
  1619. Instance *instance = instance_owner.get( p_instance );
  1620. ERR_FAIL_COND( !instance );
  1621. if (instance->data.skeleton.is_valid()) {
  1622. skeleton_dependency_map[instance->data.skeleton].erase(instance);
  1623. }
  1624. instance->data.skeleton=p_skeleton;
  1625. if (instance->data.skeleton.is_valid()) {
  1626. skeleton_dependency_map[instance->data.skeleton].insert(instance);
  1627. }
  1628. }
  1629. RID VisualServerRaster::instance_get_skeleton(RID p_instance) const {
  1630. Instance *instance = instance_owner.get( p_instance );
  1631. ERR_FAIL_COND_V( !instance, RID() );
  1632. return instance->data.skeleton;
  1633. }
  1634. void VisualServerRaster::instance_set_morph_target_weight(RID p_instance,int p_shape, float p_weight) {
  1635. VS_CHANGED;
  1636. Instance *instance = instance_owner.get( p_instance );
  1637. ERR_FAIL_COND( !instance );
  1638. ERR_FAIL_INDEX( p_shape, instance->data.morph_values.size() );
  1639. instance->data.morph_values[p_shape]=p_weight;
  1640. }
  1641. float VisualServerRaster::instance_get_morph_target_weight(RID p_instance,int p_shape) const {
  1642. Instance *instance = instance_owner.get( p_instance );
  1643. ERR_FAIL_COND_V( !instance, 0 );
  1644. ERR_FAIL_INDEX_V( p_shape, instance->data.morph_values.size(), 0 );
  1645. return instance->data.morph_values[p_shape];
  1646. }
  1647. void VisualServerRaster::instance_set_surface_material(RID p_instance,int p_surface, RID p_material) {
  1648. VS_CHANGED;
  1649. Instance *instance = instance_owner.get( p_instance );
  1650. ERR_FAIL_COND( !instance);
  1651. ERR_FAIL_INDEX( p_surface, instance->data.materials.size() );
  1652. instance->data.materials[p_surface]=p_material;
  1653. }
  1654. void VisualServerRaster::instance_set_transform(RID p_instance, const Transform& p_transform) {
  1655. VS_CHANGED;
  1656. Instance *instance = instance_owner.get( p_instance );
  1657. ERR_FAIL_COND( !instance );
  1658. if (p_transform==instance->data.transform) // must improve somehow
  1659. return;
  1660. instance->data.transform=p_transform;
  1661. if (instance->base_type==INSTANCE_LIGHT)
  1662. instance->data.transform.orthonormalize();
  1663. _instance_queue_update(instance);
  1664. }
  1665. Transform VisualServerRaster::instance_get_transform(RID p_instance) const {
  1666. Instance *instance = instance_owner.get( p_instance );
  1667. ERR_FAIL_COND_V( !instance, Transform() );
  1668. return instance->data.transform;
  1669. }
  1670. void VisualServerRaster::instance_set_exterior( RID p_instance, bool p_enabled ) {
  1671. VS_CHANGED;
  1672. Instance *instance = instance_owner.get( p_instance );
  1673. ERR_FAIL_COND( !instance );
  1674. ERR_EXPLAIN("Portals can't be assigned to be exterior");
  1675. ERR_FAIL_COND( instance->base_type == INSTANCE_PORTAL );
  1676. if (instance->exterior==p_enabled)
  1677. return;
  1678. instance->exterior=p_enabled;
  1679. _instance_queue_update( instance );
  1680. }
  1681. bool VisualServerRaster::instance_is_exterior( RID p_instance) const {
  1682. Instance *instance = instance_owner.get( p_instance );
  1683. ERR_FAIL_COND_V( !instance, false );
  1684. return instance->exterior;
  1685. }
  1686. void VisualServerRaster::instance_set_room( RID p_instance, RID p_room ) {
  1687. VS_CHANGED;
  1688. Instance *instance = instance_owner.get( p_instance );
  1689. ERR_FAIL_COND( !instance );
  1690. if (instance->room && instance->RE) {
  1691. //instance already havs a room, remove it from there
  1692. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1693. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1694. if (!p_room.is_valid() && instance->octree_id) {
  1695. //remove from the octree, so it's re-added with different flags
  1696. instance->scenario->octree.erase( instance->octree_id );
  1697. instance->octree_id=0;
  1698. _instance_queue_update( instance,true );
  1699. }
  1700. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1701. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1702. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1703. _portal_disconnect(E->get());
  1704. _instance_queue_update( E->get(),false );
  1705. }
  1706. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1707. _portal_disconnect(instance,true);
  1708. bool ss = instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1709. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1710. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1711. } else {
  1712. ERR_FAIL();
  1713. }
  1714. instance->RE=NULL;
  1715. instance->room=NULL;
  1716. } else {
  1717. if (p_room.is_valid() && instance->octree_id) {
  1718. //remove from the octree, so it's re-added with different flags
  1719. instance->scenario->octree.erase( instance->octree_id );
  1720. instance->octree_id=0;
  1721. _instance_queue_update( instance,true );
  1722. }
  1723. }
  1724. if (!p_room.is_valid())
  1725. return; // just clearning the room
  1726. Instance *room = instance_owner.get( p_room );
  1727. ERR_FAIL_COND( !room );
  1728. ERR_FAIL_COND( room->base_type!=INSTANCE_ROOM );
  1729. if (instance->base_type==INSTANCE_ROOM) {
  1730. //perform cycle test
  1731. Instance *parent = instance;
  1732. while(parent) {
  1733. ERR_EXPLAIN("Cycle in room assignment");
  1734. ERR_FAIL_COND( parent == room );
  1735. parent=parent->room;
  1736. }
  1737. }
  1738. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1739. instance->RE = room->room_info->owned_geometry_instances.push_back(instance);
  1740. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1741. instance->RE = room->room_info->owned_room_instances.push_back(instance);
  1742. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next())
  1743. _instance_queue_update( E->get(),false );
  1744. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1745. instance->RE = room->room_info->owned_portal_instances.push_back(instance);
  1746. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1747. instance->RE = room->room_info->owned_light_instances.push_back(instance);
  1748. } else {
  1749. ERR_FAIL();
  1750. }
  1751. instance->room=room;
  1752. }
  1753. RID VisualServerRaster::instance_get_room( RID p_instance ) const {
  1754. Instance *instance = instance_owner.get( p_instance );
  1755. ERR_FAIL_COND_V( !instance, RID() );
  1756. if (instance->room)
  1757. return instance->room->self;
  1758. else
  1759. return RID();
  1760. }
  1761. void VisualServerRaster::instance_set_extra_visibility_margin( RID p_instance, real_t p_margin ) {
  1762. VS_CHANGED;
  1763. Instance *instance = instance_owner.get( p_instance );
  1764. ERR_FAIL_COND( !instance );
  1765. instance->extra_margin=p_margin;
  1766. }
  1767. real_t VisualServerRaster::instance_get_extra_visibility_margin( RID p_instance ) const{
  1768. Instance *instance = instance_owner.get( p_instance );
  1769. ERR_FAIL_COND_V( !instance, 0 );
  1770. return instance->extra_margin;
  1771. }
  1772. Vector<RID> VisualServerRaster::instances_cull_aabb(const AABB& p_aabb, RID p_scenario) const {
  1773. Vector<RID> instances;
  1774. Scenario *scenario=scenario_owner.get(p_scenario);
  1775. ERR_FAIL_COND_V(!scenario,instances);
  1776. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1777. int culled=0;
  1778. Instance *cull[1024];
  1779. culled=scenario->octree.cull_AABB(p_aabb,cull,1024);
  1780. for (int i=0;i<culled;i++) {
  1781. Instance *instance=cull[i];
  1782. ERR_CONTINUE(!instance);
  1783. instances.push_back(instance->self);
  1784. }
  1785. return instances;
  1786. }
  1787. Vector<RID> VisualServerRaster::instances_cull_ray(const Vector3& p_from, const Vector3& p_to, RID p_scenario) const{
  1788. Vector<RID> instances;
  1789. Scenario *scenario=scenario_owner.get(p_scenario);
  1790. ERR_FAIL_COND_V(!scenario,instances);
  1791. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1792. int culled=0;
  1793. Instance *cull[1024];
  1794. culled=scenario->octree.cull_segment(p_from,p_to*10000,cull,1024);
  1795. for (int i=0;i<culled;i++) {
  1796. Instance *instance=cull[i];
  1797. ERR_CONTINUE(!instance);
  1798. instances.push_back(instance->self);
  1799. }
  1800. return instances;
  1801. }
  1802. Vector<RID> VisualServerRaster::instances_cull_convex(const Vector<Plane>& p_convex, RID p_scenario) const{
  1803. Vector<RID> instances;
  1804. Scenario *scenario=scenario_owner.get(p_scenario);
  1805. ERR_FAIL_COND_V(!scenario,instances);
  1806. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1807. int culled=0;
  1808. Instance *cull[1024];
  1809. culled=scenario->octree.cull_convex(p_convex,cull,1024);
  1810. for (int i=0;i<culled;i++) {
  1811. Instance *instance=cull[i];
  1812. ERR_CONTINUE(!instance);
  1813. instances.push_back(instance->self);
  1814. }
  1815. return instances;
  1816. }
  1817. void VisualServerRaster::instance_geometry_set_flag(RID p_instance,InstanceFlags p_flags,bool p_enabled) {
  1818. Instance *instance = instance_owner.get( p_instance );
  1819. ERR_FAIL_COND( !instance );
  1820. // ERR_FAIL_COND( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK) );
  1821. switch(p_flags) {
  1822. case INSTANCE_FLAG_VISIBLE: {
  1823. instance->visible=p_enabled;
  1824. } break;
  1825. case INSTANCE_FLAG_BILLBOARD: {
  1826. instance->data.billboard=p_enabled;
  1827. } break;
  1828. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1829. instance->data.billboard_y=p_enabled;
  1830. } break;
  1831. case INSTANCE_FLAG_CAST_SHADOW: {
  1832. if (p_enabled == true) {
  1833. instance->data.cast_shadows = SHADOW_CASTING_SETTING_ON;
  1834. }
  1835. else {
  1836. instance->data.cast_shadows = SHADOW_CASTING_SETTING_OFF;
  1837. }
  1838. } break;
  1839. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1840. instance->data.receive_shadows=p_enabled;
  1841. } break;
  1842. case INSTANCE_FLAG_DEPH_SCALE: {
  1843. instance->data.depth_scale=p_enabled;
  1844. } break;
  1845. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1846. instance->visible_in_all_rooms=p_enabled;
  1847. } break;
  1848. }
  1849. }
  1850. bool VisualServerRaster::instance_geometry_get_flag(RID p_instance,InstanceFlags p_flags) const{
  1851. const Instance *instance = instance_owner.get( p_instance );
  1852. ERR_FAIL_COND_V( !instance, false );
  1853. // ERR_FAIL_COND_V( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK), false );
  1854. switch(p_flags) {
  1855. case INSTANCE_FLAG_VISIBLE: {
  1856. return instance->visible;
  1857. } break;
  1858. case INSTANCE_FLAG_BILLBOARD: {
  1859. return instance->data.billboard;
  1860. } break;
  1861. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1862. return instance->data.billboard_y;
  1863. } break;
  1864. case INSTANCE_FLAG_CAST_SHADOW: {
  1865. if(instance->data.cast_shadows == SHADOW_CASTING_SETTING_OFF) {
  1866. return false;
  1867. }
  1868. else {
  1869. return true;
  1870. }
  1871. } break;
  1872. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1873. return instance->data.receive_shadows;
  1874. } break;
  1875. case INSTANCE_FLAG_DEPH_SCALE: {
  1876. return instance->data.depth_scale;
  1877. } break;
  1878. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1879. return instance->visible_in_all_rooms;
  1880. } break;
  1881. }
  1882. return false;
  1883. }
  1884. void VisualServerRaster::instance_geometry_set_cast_shadows_setting(RID p_instance, VS::ShadowCastingSetting p_shadow_casting_setting) {
  1885. Instance *instance = instance_owner.get( p_instance );
  1886. ERR_FAIL_COND( !instance );
  1887. instance->data.cast_shadows = p_shadow_casting_setting;
  1888. }
  1889. VS::ShadowCastingSetting VisualServerRaster::instance_geometry_get_cast_shadows_setting(RID p_instance) const{
  1890. const Instance *instance = instance_owner.get( p_instance );
  1891. ERR_FAIL_COND_V( !instance, SHADOW_CASTING_SETTING_OFF );
  1892. return instance->data.cast_shadows;
  1893. }
  1894. void VisualServerRaster::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  1895. VS_CHANGED;
  1896. Instance *instance = instance_owner.get( p_instance );
  1897. ERR_FAIL_COND( !instance );
  1898. instance->data.material_override=p_material;
  1899. }
  1900. RID VisualServerRaster::instance_geometry_get_material_override(RID p_instance) const{
  1901. Instance *instance = instance_owner.get( p_instance );
  1902. ERR_FAIL_COND_V( !instance, RID() );
  1903. return instance->data.material_override;
  1904. }
  1905. void VisualServerRaster::instance_geometry_set_draw_range(RID p_instance,float p_min,float p_max){
  1906. VS_CHANGED;
  1907. Instance *instance = instance_owner.get( p_instance );
  1908. ERR_FAIL_COND( !instance );
  1909. instance->draw_range_begin=p_min;
  1910. instance->draw_range_end=p_max;
  1911. }
  1912. float VisualServerRaster::instance_geometry_get_draw_range_min(RID p_instance) const{
  1913. const Instance *instance = instance_owner.get( p_instance );
  1914. ERR_FAIL_COND_V( !instance,0 );
  1915. return instance->draw_range_begin;
  1916. }
  1917. float VisualServerRaster::instance_geometry_get_draw_range_max(RID p_instance) const{
  1918. const Instance *instance = instance_owner.get( p_instance );
  1919. ERR_FAIL_COND_V( !instance,0 );
  1920. return instance->draw_range_end;
  1921. }
  1922. void VisualServerRaster::instance_geometry_set_baked_light(RID p_instance,RID p_baked_light) {
  1923. VS_CHANGED;
  1924. Instance *instance = instance_owner.get( p_instance );
  1925. ERR_FAIL_COND( !instance );
  1926. if (instance->baked_light) {
  1927. instance->baked_light->baked_light_info->owned_instances.erase(instance->BLE);
  1928. instance->BLE=NULL;
  1929. instance->baked_light=NULL;
  1930. instance->data.baked_light=NULL;
  1931. instance->data.baked_light_octree_xform=NULL;
  1932. }
  1933. if (!p_baked_light.is_valid())
  1934. return;
  1935. Instance *bl_instance = instance_owner.get( p_baked_light );
  1936. ERR_FAIL_COND( !bl_instance );
  1937. ERR_FAIL_COND( bl_instance->base_type!=INSTANCE_BAKED_LIGHT );
  1938. instance->baked_light=bl_instance;
  1939. instance->BLE=bl_instance->baked_light_info->owned_instances.push_back(instance);
  1940. instance->data.baked_light=&bl_instance->baked_light_info->baked_light->data;
  1941. instance->data.baked_light_octree_xform=&bl_instance->baked_light_info->affine_inverse;
  1942. }
  1943. RID VisualServerRaster::instance_geometry_get_baked_light(RID p_instance) const{
  1944. const Instance *instance = instance_owner.get( p_instance );
  1945. ERR_FAIL_COND_V( !instance,RID() );
  1946. if (instance->baked_light)
  1947. return instance->baked_light->self;
  1948. return RID();
  1949. }
  1950. void VisualServerRaster::instance_geometry_set_baked_light_sampler(RID p_instance,RID p_baked_light_sampler) {
  1951. VS_CHANGED;
  1952. Instance *instance = instance_owner.get( p_instance );
  1953. ERR_FAIL_COND( !instance );
  1954. if (instance->sampled_light) {
  1955. instance->sampled_light->baked_light_sampler_info->owned_instances.erase(instance);
  1956. instance->data.sampled_light=RID();
  1957. }
  1958. if(p_baked_light_sampler.is_valid()) {
  1959. Instance *sampler_instance = instance_owner.get( p_baked_light_sampler );
  1960. ERR_FAIL_COND( !sampler_instance );
  1961. ERR_FAIL_COND( sampler_instance->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER );
  1962. instance->sampled_light=sampler_instance;
  1963. instance->sampled_light->baked_light_sampler_info->owned_instances.insert(instance);
  1964. } else {
  1965. instance->sampled_light=NULL;
  1966. }
  1967. instance->data.sampled_light=RID();
  1968. }
  1969. RID VisualServerRaster::instance_geometry_get_baked_light_sampler(RID p_instance) const {
  1970. Instance *instance = instance_owner.get( p_instance );
  1971. ERR_FAIL_COND_V( !instance,RID() );
  1972. if (instance->sampled_light)
  1973. return instance->sampled_light->self;
  1974. else
  1975. return RID();
  1976. }
  1977. void VisualServerRaster::instance_geometry_set_baked_light_texture_index(RID p_instance,int p_tex_id){
  1978. VS_CHANGED;
  1979. Instance *instance = instance_owner.get( p_instance );
  1980. ERR_FAIL_COND( !instance );
  1981. instance->data.baked_lightmap_id=p_tex_id;
  1982. }
  1983. int VisualServerRaster::instance_geometry_get_baked_light_texture_index(RID p_instance) const{
  1984. const Instance *instance = instance_owner.get( p_instance );
  1985. ERR_FAIL_COND_V( !instance,0 );
  1986. return instance->data.baked_lightmap_id;
  1987. }
  1988. void VisualServerRaster::_update_instance(Instance *p_instance) {
  1989. p_instance->version++;
  1990. if (p_instance->base_type == INSTANCE_LIGHT) {
  1991. rasterizer->light_instance_set_transform( p_instance->light_info->instance, p_instance->data.transform );
  1992. }
  1993. if (p_instance->aabb.has_no_surface())
  1994. return;
  1995. if (p_instance->base_type == INSTANCE_PARTICLES) {
  1996. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  1997. }
  1998. if ((1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1999. //make sure lights are updated
  2000. InstanceSet::Element *E=p_instance->lights.front();
  2001. while(E) {
  2002. E->get()->version++;
  2003. E=E->next();
  2004. }
  2005. } else if (p_instance->base_type == INSTANCE_ROOM) {
  2006. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  2007. } else if (p_instance->base_type == INSTANCE_BAKED_LIGHT) {
  2008. Transform scale;
  2009. scale.basis.scale(p_instance->baked_light_info->baked_light->octree_aabb.size);
  2010. scale.origin=p_instance->baked_light_info->baked_light->octree_aabb.pos;
  2011. //print_line("scale: "+scale);
  2012. p_instance->baked_light_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse();
  2013. }
  2014. p_instance->data.mirror = p_instance->data.transform.basis.determinant() < 0.0;
  2015. AABB new_aabb;
  2016. if (p_instance->base_type==INSTANCE_PORTAL) {
  2017. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  2018. Transform portal_xform = p_instance->data.transform;
  2019. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  2020. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  2021. int point_count=p_instance->portal_info->portal->shape.size();
  2022. p_instance->portal_info->transformed_point_cache.resize(point_count);
  2023. AABB portal_aabb;
  2024. for(int i=0;i<point_count;i++) {
  2025. Point2 src = p_instance->portal_info->portal->shape[i];
  2026. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  2027. p_instance->portal_info->transformed_point_cache[i]=point;
  2028. if (i==0)
  2029. portal_aabb.pos=point;
  2030. else
  2031. portal_aabb.expand_to(point);
  2032. }
  2033. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  2034. new_aabb = portal_aabb;
  2035. } else {
  2036. new_aabb = p_instance->data.transform.xform(p_instance->aabb);
  2037. }
  2038. for(InstanceSet::Element *E=p_instance->lights.front();E;E=E->next()) {
  2039. Instance *light = E->get();
  2040. light->version++;
  2041. }
  2042. p_instance->transformed_aabb=new_aabb;
  2043. if (!p_instance->scenario) {
  2044. return;
  2045. }
  2046. if (p_instance->octree_id==0) {
  2047. uint32_t base_type = 1<<p_instance->base_type;
  2048. uint32_t pairable_mask=0;
  2049. bool pairable=false;
  2050. if (p_instance->base_type == INSTANCE_LIGHT) {
  2051. pairable_mask=p_instance->light_info->enabled?INSTANCE_GEOMETRY_MASK:0;
  2052. pairable=true;
  2053. }
  2054. if (p_instance->base_type == INSTANCE_PORTAL) {
  2055. pairable_mask=(1<<INSTANCE_PORTAL);
  2056. pairable=true;
  2057. }
  2058. if (p_instance->base_type == INSTANCE_BAKED_LIGHT_SAMPLER) {
  2059. pairable_mask=(1<<INSTANCE_BAKED_LIGHT);
  2060. pairable=true;
  2061. }
  2062. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  2063. base_type|=INSTANCE_ROOMLESS_MASK;
  2064. }
  2065. if (p_instance->base_type == INSTANCE_ROOM) {
  2066. pairable_mask=INSTANCE_ROOMLESS_MASK;
  2067. pairable=true;
  2068. }
  2069. // not inside octree
  2070. p_instance->octree_id = p_instance->scenario->octree.create(p_instance,new_aabb,0,pairable,base_type,pairable_mask);
  2071. } else {
  2072. // if (new_aabb==p_instance->data.transformed_aabb)
  2073. // return;
  2074. p_instance->scenario->octree.move(p_instance->octree_id,new_aabb);
  2075. }
  2076. if (p_instance->base_type==INSTANCE_PORTAL) {
  2077. _portal_attempt_connect(p_instance);
  2078. }
  2079. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  2080. _instance_validate_autorooms(p_instance);
  2081. }
  2082. if (p_instance->base_type == INSTANCE_ROOM) {
  2083. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  2084. _instance_validate_autorooms(E->get());
  2085. }
  2086. }
  2087. void VisualServerRaster::_update_instance_aabb(Instance *p_instance) {
  2088. AABB new_aabb;
  2089. ERR_FAIL_COND(p_instance->base_type!=INSTANCE_NONE && !p_instance->base_rid.is_valid());
  2090. switch(p_instance->base_type) {
  2091. case VisualServer::INSTANCE_NONE: {
  2092. // do nothing
  2093. } break;
  2094. case VisualServer::INSTANCE_MESH: {
  2095. new_aabb = rasterizer->mesh_get_aabb(p_instance->base_rid,p_instance->data.skeleton);
  2096. } break;
  2097. case VisualServer::INSTANCE_MULTIMESH: {
  2098. new_aabb = rasterizer->multimesh_get_aabb(p_instance->base_rid);
  2099. } break;
  2100. case VisualServer::INSTANCE_IMMEDIATE: {
  2101. new_aabb = rasterizer->immediate_get_aabb(p_instance->base_rid);
  2102. } break;
  2103. case VisualServer::INSTANCE_PARTICLES: {
  2104. new_aabb = rasterizer->particles_get_aabb(p_instance->base_rid);
  2105. } break;
  2106. case VisualServer::INSTANCE_LIGHT: {
  2107. new_aabb = rasterizer->light_get_aabb(p_instance->base_rid);
  2108. } break;
  2109. case VisualServer::INSTANCE_ROOM: {
  2110. Room *room = room_owner.get( p_instance->base_rid );
  2111. ERR_FAIL_COND(!room);
  2112. new_aabb=room->bounds.get_aabb();
  2113. } break;
  2114. case VisualServer::INSTANCE_PORTAL: {
  2115. Portal *portal = portal_owner.get( p_instance->base_rid );
  2116. ERR_FAIL_COND(!portal);
  2117. for (int i=0;i<portal->shape.size();i++) {
  2118. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  2119. if (i==0) {
  2120. new_aabb.pos=point;
  2121. new_aabb.size.z=0.01; // make it not flat for octree
  2122. } else {
  2123. new_aabb.expand_to(point);
  2124. }
  2125. }
  2126. } break;
  2127. case VisualServer::INSTANCE_BAKED_LIGHT: {
  2128. BakedLight *baked_light = baked_light_owner.get( p_instance->base_rid );
  2129. ERR_FAIL_COND(!baked_light);
  2130. new_aabb=baked_light->octree_aabb;
  2131. } break;
  2132. case VisualServer::INSTANCE_BAKED_LIGHT_SAMPLER: {
  2133. BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get( p_instance->base_rid );
  2134. ERR_FAIL_COND(!baked_light_sampler);
  2135. float radius = baked_light_sampler->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  2136. new_aabb=AABB(Vector3(-radius,-radius,-radius),Vector3(radius*2,radius*2,radius*2));
  2137. } break;
  2138. default: {}
  2139. }
  2140. if (p_instance->extra_margin)
  2141. new_aabb.grow_by(p_instance->extra_margin);
  2142. p_instance->aabb=new_aabb;
  2143. }
  2144. void VisualServerRaster::_update_instances() {
  2145. while(instance_update_list) {
  2146. Instance *instance=instance_update_list;
  2147. instance_update_list=instance_update_list->update_next;
  2148. if (instance->update_aabb)
  2149. _update_instance_aabb(instance);
  2150. if (instance->update_materials) {
  2151. if (instance->base_type==INSTANCE_MESH) {
  2152. instance->data.materials.resize(rasterizer->mesh_get_surface_count(instance->base_rid));
  2153. }
  2154. }
  2155. _update_instance(instance);
  2156. instance->update=false;
  2157. instance->update_aabb=false;
  2158. instance->update_materials=false;
  2159. instance->update_next=0;
  2160. }
  2161. }
  2162. void VisualServerRaster::instance_light_set_enabled(RID p_instance,bool p_enabled) {
  2163. VS_CHANGED;
  2164. Instance *instance = instance_owner.get( p_instance );
  2165. ERR_FAIL_COND( !instance );
  2166. ERR_FAIL_COND( instance->base_type!=INSTANCE_LIGHT );
  2167. if (p_enabled==instance->light_info->enabled)
  2168. return;
  2169. instance->light_info->enabled=p_enabled;
  2170. if (light_get_type(instance->base_rid)!=VS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario)
  2171. instance->scenario->octree.set_pairable(instance->octree_id,p_enabled,1<<INSTANCE_LIGHT,p_enabled?INSTANCE_GEOMETRY_MASK:0);
  2172. //_instance_queue_update( instance , true );
  2173. }
  2174. bool VisualServerRaster::instance_light_is_enabled(RID p_instance) const {
  2175. const Instance *instance = instance_owner.get( p_instance );
  2176. ERR_FAIL_COND_V( !instance,false );
  2177. ERR_FAIL_COND_V( instance->base_type!=INSTANCE_LIGHT,false );
  2178. return instance->light_info->enabled;
  2179. }
  2180. /****** CANVAS *********/
  2181. RID VisualServerRaster::canvas_create() {
  2182. Canvas * canvas = memnew( Canvas );
  2183. ERR_FAIL_COND_V(!canvas,RID());
  2184. RID rid = canvas_owner.make_rid( canvas );
  2185. return rid;
  2186. }
  2187. void VisualServerRaster::canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring) {
  2188. Canvas * canvas = canvas_owner.get(p_canvas);
  2189. ERR_FAIL_COND(!canvas);
  2190. CanvasItem *canvas_item = canvas_item_owner.get(p_item);
  2191. ERR_FAIL_COND(!canvas_item);
  2192. int idx = canvas->find_item(canvas_item);
  2193. ERR_FAIL_COND(idx==-1);
  2194. canvas->child_items[idx].mirror=p_mirroring;
  2195. }
  2196. Point2 VisualServerRaster::canvas_get_item_mirroring(RID p_canvas,RID p_item) const {
  2197. Canvas * canvas = canvas_owner.get(p_canvas);
  2198. ERR_FAIL_COND_V(!canvas,Point2());
  2199. CanvasItem *canvas_item = memnew( CanvasItem );
  2200. ERR_FAIL_COND_V(!canvas_item,Point2());
  2201. int idx = canvas->find_item(canvas_item);
  2202. ERR_FAIL_COND_V(idx==-1,Point2());
  2203. return canvas->child_items[idx].mirror;
  2204. }
  2205. void VisualServerRaster::canvas_set_modulate(RID p_canvas,const Color& p_color) {
  2206. Canvas * canvas = canvas_owner.get(p_canvas);
  2207. ERR_FAIL_COND(!canvas);
  2208. canvas->modulate=p_color;
  2209. }
  2210. RID VisualServerRaster::canvas_item_create() {
  2211. CanvasItem *canvas_item = memnew( CanvasItem );
  2212. ERR_FAIL_COND_V(!canvas_item,RID());
  2213. return canvas_item_owner.make_rid( canvas_item );
  2214. }
  2215. void VisualServerRaster::canvas_item_set_parent(RID p_item,RID p_parent) {
  2216. VS_CHANGED;
  2217. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2218. ERR_FAIL_COND(!canvas_item);
  2219. if (canvas_item->parent.is_valid()) {
  2220. if (canvas_owner.owns(canvas_item->parent)) {
  2221. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2222. canvas->erase_item(canvas_item);
  2223. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2224. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2225. item_owner->child_items.erase(canvas_item);
  2226. }
  2227. canvas_item->parent=RID();
  2228. }
  2229. if (p_parent.is_valid()) {
  2230. if (canvas_owner.owns(p_parent)) {
  2231. Canvas *canvas = canvas_owner.get(p_parent);
  2232. Canvas::ChildItem ci;
  2233. ci.item=canvas_item;
  2234. canvas->child_items.push_back(ci);
  2235. } else if (canvas_item_owner.owns(p_parent)) {
  2236. CanvasItem *item_owner = canvas_item_owner.get(p_parent);
  2237. item_owner->child_items.push_back(canvas_item);
  2238. } else {
  2239. ERR_EXPLAIN("Invalid parent");
  2240. ERR_FAIL();
  2241. }
  2242. }
  2243. canvas_item->parent=p_parent;
  2244. }
  2245. RID VisualServerRaster::canvas_item_get_parent(RID p_canvas_item) const {
  2246. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  2247. ERR_FAIL_COND_V(!canvas_item,RID());
  2248. return canvas_item->parent;
  2249. }
  2250. void VisualServerRaster::canvas_item_set_visible(RID p_item,bool p_visible) {
  2251. VS_CHANGED;
  2252. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2253. ERR_FAIL_COND(!canvas_item);
  2254. canvas_item->visible=p_visible;
  2255. }
  2256. bool VisualServerRaster::canvas_item_is_visible(RID p_item) const {
  2257. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2258. ERR_FAIL_COND_V(!canvas_item,RID());
  2259. return canvas_item->visible;
  2260. }
  2261. void VisualServerRaster::canvas_item_set_light_mask(RID p_canvas_item,int p_mask) {
  2262. VS_CHANGED;
  2263. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  2264. ERR_FAIL_COND(!canvas_item);
  2265. if (canvas_item->light_mask==p_mask)
  2266. return;
  2267. VS_CHANGED;
  2268. canvas_item->light_mask=p_mask;
  2269. }
  2270. void VisualServerRaster::canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend) {
  2271. VS_CHANGED;
  2272. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  2273. ERR_FAIL_COND(!canvas_item);
  2274. if (canvas_item->blend_mode==p_blend)
  2275. return;
  2276. VS_CHANGED;
  2277. canvas_item->blend_mode=p_blend;
  2278. }
  2279. void VisualServerRaster::canvas_item_attach_viewport(RID p_canvas_item, RID p_viewport) {
  2280. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  2281. ERR_FAIL_COND(!canvas_item);
  2282. VS_CHANGED;
  2283. canvas_item->viewport=p_viewport;
  2284. }
  2285. /*
  2286. void VisualServerRaster::canvas_item_set_rect(RID p_item, const Rect2& p_rect) {
  2287. VS_CHANGED;
  2288. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2289. ERR_FAIL_COND(!canvas_item);
  2290. canvas_item->rect=p_rect;
  2291. }*/
  2292. void VisualServerRaster::canvas_item_set_clip(RID p_item, bool p_clip) {
  2293. VS_CHANGED;
  2294. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2295. ERR_FAIL_COND(!canvas_item);
  2296. canvas_item->clip=p_clip;
  2297. }
  2298. void VisualServerRaster::canvas_item_set_distance_field_mode(RID p_item, bool p_distance_field) {
  2299. VS_CHANGED;
  2300. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2301. ERR_FAIL_COND(!canvas_item);
  2302. canvas_item->distance_field=p_distance_field;
  2303. }
  2304. void VisualServerRaster::canvas_item_set_transform(RID p_item, const Matrix32& p_transform) {
  2305. VS_CHANGED;
  2306. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2307. ERR_FAIL_COND(!canvas_item);
  2308. canvas_item->xform=p_transform;
  2309. }
  2310. void VisualServerRaster::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect,const Rect2& p_rect) {
  2311. VS_CHANGED;
  2312. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2313. ERR_FAIL_COND(!canvas_item);
  2314. canvas_item->custom_rect=p_custom_rect;
  2315. if (p_custom_rect)
  2316. canvas_item->rect=p_rect;
  2317. }
  2318. void VisualServerRaster::canvas_item_set_opacity(RID p_item, float p_opacity) {
  2319. VS_CHANGED;
  2320. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2321. ERR_FAIL_COND(!canvas_item);
  2322. canvas_item->opacity=p_opacity;
  2323. }
  2324. float VisualServerRaster::canvas_item_get_opacity(RID p_item, float p_opacity) const {
  2325. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2326. ERR_FAIL_COND_V(!canvas_item,-1);
  2327. return canvas_item->opacity;
  2328. }
  2329. void VisualServerRaster::canvas_item_set_on_top(RID p_item, bool p_on_top) {
  2330. VS_CHANGED;
  2331. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2332. ERR_FAIL_COND(!canvas_item);
  2333. canvas_item->ontop=p_on_top;
  2334. }
  2335. bool VisualServerRaster::canvas_item_is_on_top(RID p_item) const{
  2336. const CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2337. ERR_FAIL_COND_V(!canvas_item,false);
  2338. return canvas_item->ontop;
  2339. }
  2340. void VisualServerRaster::canvas_item_set_self_opacity(RID p_item, float p_self_opacity) {
  2341. VS_CHANGED;
  2342. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2343. ERR_FAIL_COND(!canvas_item);
  2344. canvas_item->self_opacity=p_self_opacity;
  2345. }
  2346. float VisualServerRaster::canvas_item_get_self_opacity(RID p_item, float p_self_opacity) const {
  2347. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2348. ERR_FAIL_COND_V(!canvas_item,-1);
  2349. return canvas_item->self_opacity;
  2350. }
  2351. void VisualServerRaster::canvas_item_add_line(RID p_item, const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width) {
  2352. VS_CHANGED;
  2353. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2354. ERR_FAIL_COND(!canvas_item);
  2355. CanvasItem::CommandLine * line = memnew( CanvasItem::CommandLine );
  2356. ERR_FAIL_COND(!line);
  2357. line->color=p_color;
  2358. line->from=p_from;
  2359. line->to=p_to;
  2360. line->width=p_width;
  2361. canvas_item->rect_dirty=true;
  2362. canvas_item->commands.push_back(line);
  2363. }
  2364. void VisualServerRaster::canvas_item_add_rect(RID p_item, const Rect2& p_rect, const Color& p_color) {
  2365. VS_CHANGED;
  2366. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2367. ERR_FAIL_COND(!canvas_item);
  2368. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2369. ERR_FAIL_COND(!rect);
  2370. rect->modulate=p_color;
  2371. rect->rect=p_rect;
  2372. canvas_item->rect_dirty=true;
  2373. canvas_item->commands.push_back(rect);
  2374. }
  2375. void VisualServerRaster::canvas_item_add_circle(RID p_item, const Point2& p_pos, float p_radius,const Color& p_color) {
  2376. VS_CHANGED;
  2377. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2378. ERR_FAIL_COND(!canvas_item);
  2379. CanvasItem::CommandCircle * circle = memnew( CanvasItem::CommandCircle );
  2380. ERR_FAIL_COND(!circle);
  2381. circle->color=p_color;
  2382. circle->pos=p_pos;
  2383. circle->radius=p_radius;
  2384. canvas_item->commands.push_back(circle);
  2385. }
  2386. void VisualServerRaster::canvas_item_add_texture_rect(RID p_item, const Rect2& p_rect, RID p_texture,bool p_tile,const Color& p_modulate,bool p_transpose) {
  2387. VS_CHANGED;
  2388. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2389. ERR_FAIL_COND(!canvas_item);
  2390. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2391. ERR_FAIL_COND(!rect);
  2392. rect->modulate=p_modulate;
  2393. rect->rect=p_rect;
  2394. rect->flags=0;
  2395. if (p_tile) {
  2396. rect->flags|=Rasterizer::CANVAS_RECT_TILE;
  2397. rect->flags|=Rasterizer::CANVAS_RECT_REGION;
  2398. rect->source=Rect2(0,0,p_rect.size.width,p_rect.size.height);
  2399. }
  2400. if (p_rect.size.x<0) {
  2401. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2402. rect->rect.size.x = -rect->rect.size.x;
  2403. }
  2404. if (p_rect.size.y<0) {
  2405. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2406. rect->rect.size.y = -rect->rect.size.y;
  2407. }
  2408. if (p_transpose) {
  2409. rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE;
  2410. SWAP(rect->rect.size.x, rect->rect.size.y);
  2411. }
  2412. rect->texture=p_texture;
  2413. canvas_item->rect_dirty=true;
  2414. canvas_item->commands.push_back(rect);
  2415. }
  2416. 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,bool p_transpose) {
  2417. VS_CHANGED;
  2418. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2419. ERR_FAIL_COND(!canvas_item);
  2420. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2421. ERR_FAIL_COND(!rect);
  2422. rect->modulate=p_modulate;
  2423. rect->rect=p_rect;
  2424. rect->texture=p_texture;
  2425. rect->source=p_src_rect;
  2426. rect->flags=Rasterizer::CANVAS_RECT_REGION;
  2427. if (p_rect.size.x<0) {
  2428. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2429. rect->rect.size.x = -rect->rect.size.x;
  2430. }
  2431. if (p_rect.size.y<0) {
  2432. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2433. rect->rect.size.y = -rect->rect.size.y;
  2434. }
  2435. if (p_transpose) {
  2436. rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE;
  2437. SWAP(rect->rect.size.x, rect->rect.size.y);
  2438. }
  2439. canvas_item->rect_dirty=true;
  2440. canvas_item->commands.push_back(rect);
  2441. }
  2442. void VisualServerRaster::canvas_item_add_style_box(RID p_item, const Rect2& p_rect, const Rect2& p_source, RID p_texture, const Vector2& p_topleft, const Vector2& p_bottomright, bool p_draw_center,const Color& p_modulate) {
  2443. VS_CHANGED;
  2444. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2445. ERR_FAIL_COND(!canvas_item);
  2446. CanvasItem::CommandStyle * style = memnew( CanvasItem::CommandStyle );
  2447. ERR_FAIL_COND(!style);
  2448. style->texture=p_texture;
  2449. style->rect=p_rect;
  2450. style->source=p_source;
  2451. style->draw_center=p_draw_center;
  2452. style->color=p_modulate;
  2453. style->margin[MARGIN_LEFT]=p_topleft.x;
  2454. style->margin[MARGIN_TOP]=p_topleft.y;
  2455. style->margin[MARGIN_RIGHT]=p_bottomright.x;
  2456. style->margin[MARGIN_BOTTOM]=p_bottomright.y;
  2457. canvas_item->rect_dirty=true;
  2458. canvas_item->commands.push_back(style);
  2459. }
  2460. 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) {
  2461. VS_CHANGED;
  2462. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2463. ERR_FAIL_COND(!canvas_item);
  2464. CanvasItem::CommandPrimitive * prim = memnew( CanvasItem::CommandPrimitive );
  2465. ERR_FAIL_COND(!prim);
  2466. prim->texture=p_texture;
  2467. prim->points=p_points;
  2468. prim->uvs=p_uvs;
  2469. prim->colors=p_colors;
  2470. prim->width=p_width;
  2471. canvas_item->rect_dirty=true;
  2472. canvas_item->commands.push_back(prim);
  2473. }
  2474. 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) {
  2475. VS_CHANGED;
  2476. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2477. ERR_FAIL_COND(!canvas_item);
  2478. #ifdef DEBUG_ENABLED
  2479. int pointcount = p_points.size();
  2480. ERR_FAIL_COND(pointcount<3);
  2481. int color_size=p_colors.size();
  2482. int uv_size=p_uvs.size();
  2483. ERR_FAIL_COND(color_size!=0 && color_size!=1 && color_size!=pointcount);
  2484. ERR_FAIL_COND(uv_size!=0 && (uv_size!=pointcount || !p_texture.is_valid()));
  2485. #endif
  2486. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  2487. if (indices.empty()) {
  2488. ERR_EXPLAIN("Bad Polygon!");
  2489. ERR_FAIL_V();
  2490. }
  2491. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2492. ERR_FAIL_COND(!polygon);
  2493. polygon->texture=p_texture;
  2494. polygon->points=p_points;
  2495. polygon->uvs=p_uvs;
  2496. polygon->colors=p_colors;
  2497. polygon->indices=indices;
  2498. polygon->count=indices.size();
  2499. canvas_item->rect_dirty=true;
  2500. canvas_item->commands.push_back(polygon);
  2501. }
  2502. 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) {
  2503. VS_CHANGED;
  2504. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2505. ERR_FAIL_COND(!canvas_item);
  2506. ERR_FAIL_COND(p_count <= 0);
  2507. ERR_FAIL_COND(p_points == NULL);
  2508. CanvasItem::CommandPolygonPtr * polygon = memnew( CanvasItem::CommandPolygonPtr );
  2509. ERR_FAIL_COND(!polygon);
  2510. polygon->texture=p_texture;
  2511. polygon->points=p_points;
  2512. polygon->uvs=p_uvs;
  2513. polygon->colors=p_colors;
  2514. polygon->indices=p_indices;
  2515. polygon->count = p_count * 3;
  2516. canvas_item->rect_dirty=true;
  2517. canvas_item->commands.push_back(polygon);
  2518. };
  2519. 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) {
  2520. VS_CHANGED;
  2521. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2522. ERR_FAIL_COND(!canvas_item);
  2523. int ps = p_points.size();
  2524. ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps && p_colors.size()!=1);
  2525. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps);
  2526. Vector<int> indices = p_indices;
  2527. int count = p_count * 3;
  2528. if (indices.empty()) {
  2529. ERR_FAIL_COND( ps % 3 != 0 );
  2530. if (p_count == -1)
  2531. count = ps;
  2532. } else {
  2533. ERR_FAIL_COND( indices.size() % 3 != 0 );
  2534. if (p_count == -1)
  2535. count = indices.size();
  2536. }
  2537. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2538. ERR_FAIL_COND(!polygon);
  2539. polygon->texture=p_texture;
  2540. polygon->points=p_points;
  2541. polygon->uvs=p_uvs;
  2542. polygon->colors=p_colors;
  2543. polygon->indices=indices;
  2544. polygon->count = count;
  2545. canvas_item->rect_dirty=true;
  2546. canvas_item->commands.push_back(polygon);
  2547. }
  2548. void VisualServerRaster::canvas_item_add_set_transform(RID p_item,const Matrix32& p_transform) {
  2549. VS_CHANGED;
  2550. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2551. ERR_FAIL_COND(!canvas_item);
  2552. CanvasItem::CommandTransform * tr = memnew( CanvasItem::CommandTransform );
  2553. ERR_FAIL_COND(!tr);
  2554. tr->xform=p_transform;
  2555. canvas_item->commands.push_back(tr);
  2556. }
  2557. void VisualServerRaster::canvas_item_add_set_blend_mode(RID p_item, MaterialBlendMode p_blend) {
  2558. VS_CHANGED;
  2559. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2560. ERR_FAIL_COND(!canvas_item);
  2561. CanvasItem::CommandBlendMode * bm = memnew( CanvasItem::CommandBlendMode );
  2562. ERR_FAIL_COND(!bm);
  2563. bm->blend_mode = p_blend;
  2564. canvas_item->commands.push_back(bm);
  2565. };
  2566. void VisualServerRaster::canvas_item_set_z(RID p_item, int p_z) {
  2567. ERR_FAIL_COND(p_z<CANVAS_ITEM_Z_MIN || p_z>CANVAS_ITEM_Z_MAX);
  2568. VS_CHANGED;
  2569. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2570. ERR_FAIL_COND(!canvas_item);
  2571. canvas_item->z=p_z;
  2572. }
  2573. void VisualServerRaster::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  2574. VS_CHANGED;
  2575. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2576. ERR_FAIL_COND(!canvas_item);
  2577. canvas_item->z_relative=p_enable;
  2578. }
  2579. void VisualServerRaster::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2& p_rect) {
  2580. VS_CHANGED;
  2581. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2582. ERR_FAIL_COND(!canvas_item);
  2583. if (bool(canvas_item->copy_back_buffer!=NULL) !=p_enable) {
  2584. if (p_enable) {
  2585. canvas_item->copy_back_buffer = memnew( Rasterizer::CanvasItem::CopyBackBuffer );
  2586. } else {
  2587. memdelete(canvas_item->copy_back_buffer);
  2588. canvas_item->copy_back_buffer=NULL;
  2589. }
  2590. }
  2591. if (p_enable) {
  2592. canvas_item->copy_back_buffer->rect=p_rect;
  2593. canvas_item->copy_back_buffer->full=p_rect==Rect2();
  2594. }
  2595. }
  2596. void VisualServerRaster::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  2597. VS_CHANGED;
  2598. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2599. ERR_FAIL_COND(!canvas_item);
  2600. canvas_item->use_parent_material=p_enable;
  2601. }
  2602. void VisualServerRaster::canvas_item_set_material(RID p_item, RID p_material) {
  2603. VS_CHANGED;
  2604. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2605. ERR_FAIL_COND(!canvas_item);
  2606. if (canvas_item->material)
  2607. canvas_item->material->owners.erase(canvas_item);
  2608. canvas_item->material=NULL;
  2609. if (canvas_item_material_owner.owns(p_material)) {
  2610. canvas_item->material=canvas_item_material_owner.get(p_material);
  2611. canvas_item->material->owners.insert(canvas_item);
  2612. }
  2613. }
  2614. void VisualServerRaster::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  2615. VS_CHANGED;
  2616. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2617. ERR_FAIL_COND(!canvas_item);
  2618. canvas_item->sort_y=p_enable;
  2619. }
  2620. void VisualServerRaster::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  2621. VS_CHANGED;
  2622. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2623. ERR_FAIL_COND(!canvas_item);
  2624. CanvasItem::CommandClipIgnore * ci = memnew( CanvasItem::CommandClipIgnore);
  2625. ERR_FAIL_COND(!ci);
  2626. ci->ignore=p_ignore;
  2627. canvas_item->commands.push_back(ci);
  2628. }
  2629. void VisualServerRaster::canvas_item_clear(RID p_item) {
  2630. VS_CHANGED;
  2631. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2632. ERR_FAIL_COND(!canvas_item);
  2633. canvas_item->clear();
  2634. }
  2635. void VisualServerRaster::canvas_item_raise(RID p_item) {
  2636. VS_CHANGED;
  2637. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2638. ERR_FAIL_COND(!canvas_item);
  2639. if (canvas_item->parent.is_valid()) {
  2640. if (canvas_owner.owns(canvas_item->parent)) {
  2641. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2642. int idx = canvas->find_item(canvas_item);
  2643. ERR_FAIL_COND(idx<0);
  2644. Canvas::ChildItem ci = canvas->child_items[idx];
  2645. canvas->child_items.remove(idx);
  2646. canvas->child_items.push_back(ci);
  2647. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2648. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2649. int idx = item_owner->child_items.find(canvas_item);
  2650. ERR_FAIL_COND(idx<0);
  2651. item_owner->child_items.remove(idx);
  2652. item_owner->child_items.push_back(canvas_item);
  2653. }
  2654. }
  2655. }
  2656. /***** CANVAS LIGHT *******/
  2657. RID VisualServerRaster::canvas_light_create() {
  2658. Rasterizer::CanvasLight *clight = memnew( Rasterizer::CanvasLight );
  2659. return canvas_light_owner.make_rid(clight);
  2660. }
  2661. void VisualServerRaster::canvas_light_attach_to_canvas(RID p_light,RID p_canvas){
  2662. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2663. ERR_FAIL_COND(!clight);
  2664. if (clight->canvas.is_valid()) {
  2665. Canvas *canvas = canvas_owner.get(clight->canvas);
  2666. canvas->lights.erase(clight);
  2667. }
  2668. if (!canvas_owner.owns(p_canvas))
  2669. p_canvas=RID();
  2670. clight->canvas=p_canvas;
  2671. if (clight->canvas.is_valid()) {
  2672. Canvas *canvas = canvas_owner.get(clight->canvas);
  2673. canvas->lights.insert(clight);
  2674. }
  2675. }
  2676. void VisualServerRaster::canvas_light_set_enabled(RID p_light, bool p_enabled){
  2677. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2678. ERR_FAIL_COND(!clight);
  2679. clight->enabled=p_enabled;
  2680. }
  2681. void VisualServerRaster::canvas_light_set_transform(RID p_light, const Matrix32& p_transform){
  2682. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2683. ERR_FAIL_COND(!clight);
  2684. clight->xform=p_transform;
  2685. }
  2686. void VisualServerRaster::canvas_light_set_scale(RID p_light, float p_scale) {
  2687. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2688. ERR_FAIL_COND(!clight);
  2689. clight->scale=p_scale;
  2690. }
  2691. void VisualServerRaster::canvas_light_set_texture(RID p_light, RID p_texture){
  2692. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2693. ERR_FAIL_COND(!clight);
  2694. clight->texture=p_texture;
  2695. }
  2696. void VisualServerRaster::canvas_light_set_texture_offset(RID p_light, const Vector2& p_offset){
  2697. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2698. ERR_FAIL_COND(!clight);
  2699. clight->texture_offset=p_offset;
  2700. }
  2701. void VisualServerRaster::canvas_light_set_color(RID p_light, const Color& p_color){
  2702. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2703. ERR_FAIL_COND(!clight);
  2704. clight->color=p_color;
  2705. }
  2706. void VisualServerRaster::canvas_light_set_height(RID p_light, float p_height){
  2707. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2708. ERR_FAIL_COND(!clight);
  2709. clight->height=p_height;
  2710. }
  2711. void VisualServerRaster::canvas_light_set_energy(RID p_light, float p_energy){
  2712. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2713. ERR_FAIL_COND(!clight);
  2714. clight->energy=p_energy;
  2715. }
  2716. void VisualServerRaster::canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z){
  2717. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2718. ERR_FAIL_COND(!clight);
  2719. clight->z_min=p_min_z;
  2720. clight->z_max=p_max_z;
  2721. }
  2722. void VisualServerRaster::canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer) {
  2723. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2724. ERR_FAIL_COND(!clight);
  2725. clight->layer_min=p_min_layer;
  2726. clight->layer_max=p_max_layer;
  2727. }
  2728. void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){
  2729. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2730. ERR_FAIL_COND(!clight);
  2731. clight->item_mask=p_mask;
  2732. }
  2733. void VisualServerRaster::canvas_light_set_item_shadow_mask(RID p_light, int p_mask){
  2734. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2735. ERR_FAIL_COND(!clight);
  2736. clight->item_shadow_mask=p_mask;
  2737. }
  2738. void VisualServerRaster::canvas_light_set_mode(RID p_light, CanvasLightMode p_mode) {
  2739. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2740. ERR_FAIL_COND(!clight);
  2741. clight->mode=p_mode;
  2742. }
  2743. void VisualServerRaster::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled){
  2744. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2745. ERR_FAIL_COND(!clight);
  2746. if (clight->shadow_buffer.is_valid()==p_enabled)
  2747. return;
  2748. if (p_enabled) {
  2749. clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  2750. } else {
  2751. rasterizer->free(clight->shadow_buffer);
  2752. clight->shadow_buffer=RID();
  2753. }
  2754. }
  2755. void VisualServerRaster::canvas_light_set_shadow_buffer_size(RID p_light, int p_size){
  2756. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2757. ERR_FAIL_COND(!clight);
  2758. ERR_FAIL_COND(p_size<32 || p_size>16384);
  2759. clight->shadow_buffer_size=nearest_power_of_2(p_size);
  2760. if (clight->shadow_buffer.is_valid()) {
  2761. rasterizer->free(clight->shadow_buffer);
  2762. clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  2763. }
  2764. }
  2765. void VisualServerRaster::canvas_light_set_shadow_esm_multiplier(RID p_light, float p_multiplier) {
  2766. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2767. ERR_FAIL_COND(!clight);
  2768. clight->shadow_esm_mult=p_multiplier;
  2769. }
  2770. void VisualServerRaster::canvas_light_set_shadow_color(RID p_light, const Color& p_color) {
  2771. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2772. ERR_FAIL_COND(!clight);
  2773. clight->shadow_color=p_color;
  2774. }
  2775. /****** CANVAS LIGHT OCCLUDER ******/
  2776. RID VisualServerRaster::canvas_light_occluder_create() {
  2777. Rasterizer::CanvasLightOccluderInstance *occluder = memnew( Rasterizer::CanvasLightOccluderInstance );
  2778. return canvas_light_occluder_owner.make_rid( occluder );
  2779. }
  2780. void VisualServerRaster::canvas_light_occluder_attach_to_canvas(RID p_occluder,RID p_canvas) {
  2781. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2782. ERR_FAIL_COND(!occluder);
  2783. if (occluder->canvas.is_valid()) {
  2784. Canvas *canvas = canvas_owner.get(occluder->canvas);
  2785. canvas->occluders.erase(occluder);
  2786. }
  2787. if (!canvas_owner.owns(p_canvas))
  2788. p_canvas=RID();
  2789. occluder->canvas=p_canvas;
  2790. if (occluder->canvas.is_valid()) {
  2791. Canvas *canvas = canvas_owner.get(occluder->canvas);
  2792. canvas->occluders.insert(occluder);
  2793. }
  2794. }
  2795. void VisualServerRaster::canvas_light_occluder_set_enabled(RID p_occluder,bool p_enabled){
  2796. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2797. ERR_FAIL_COND(!occluder);
  2798. occluder->enabled=p_enabled;
  2799. }
  2800. void VisualServerRaster::canvas_light_occluder_set_polygon(RID p_occluder,RID p_polygon) {
  2801. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2802. ERR_FAIL_COND(!occluder);
  2803. if (occluder->polygon.is_valid()) {
  2804. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  2805. if (occluder_poly) {
  2806. occluder_poly->owners.erase(occluder);
  2807. }
  2808. }
  2809. occluder->polygon=p_polygon;
  2810. occluder->polygon_buffer=RID();
  2811. if (occluder->polygon.is_valid()) {
  2812. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  2813. if (!occluder_poly)
  2814. occluder->polygon=RID();
  2815. ERR_FAIL_COND(!occluder_poly);
  2816. occluder_poly->owners.insert(occluder);
  2817. occluder->polygon_buffer=occluder_poly->occluder;
  2818. occluder->aabb_cache=occluder_poly->aabb;
  2819. occluder->cull_cache=occluder_poly->cull_mode;
  2820. }
  2821. }
  2822. void VisualServerRaster::canvas_light_occluder_set_transform(RID p_occluder,const Matrix32& p_xform) {
  2823. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2824. ERR_FAIL_COND(!occluder);
  2825. occluder->xform=p_xform;
  2826. }
  2827. void VisualServerRaster::canvas_light_occluder_set_light_mask(RID p_occluder,int p_mask) {
  2828. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2829. ERR_FAIL_COND(!occluder);
  2830. occluder->light_mask=p_mask;
  2831. }
  2832. RID VisualServerRaster::canvas_occluder_polygon_create() {
  2833. CanvasLightOccluderPolygon * occluder_poly = memnew( CanvasLightOccluderPolygon );
  2834. occluder_poly->occluder=rasterizer->canvas_light_occluder_create();
  2835. return canvas_light_occluder_polygon_owner.make_rid(occluder_poly);
  2836. }
  2837. void VisualServerRaster::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const DVector<Vector2>& p_shape, bool p_close){
  2838. if (p_shape.size()<3) {
  2839. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,p_shape);
  2840. return;
  2841. }
  2842. DVector<Vector2> lines;
  2843. int lc = p_shape.size()*2;
  2844. lines.resize(lc-(p_close?0:2));
  2845. {
  2846. DVector<Vector2>::Write w = lines.write();
  2847. DVector<Vector2>::Read r = p_shape.read();
  2848. int max=lc/2;
  2849. if (!p_close) {
  2850. max--;
  2851. }
  2852. for(int i=0;i<max;i++) {
  2853. Vector2 a = r[i];
  2854. Vector2 b = r[(i+1)%(lc/2)];
  2855. w[i*2+0]=a;
  2856. w[i*2+1]=b;
  2857. }
  2858. }
  2859. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,lines);
  2860. }
  2861. void VisualServerRaster::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon,const DVector<Vector2>& p_shape) {
  2862. CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  2863. ERR_FAIL_COND(!occluder_poly);
  2864. ERR_FAIL_COND(p_shape.size()&1);
  2865. int lc = p_shape.size();
  2866. occluder_poly->aabb=Rect2();
  2867. {
  2868. DVector<Vector2>::Read r = p_shape.read();
  2869. for(int i=0;i<lc;i++) {
  2870. if (i==0)
  2871. occluder_poly->aabb.pos=r[i];
  2872. else
  2873. occluder_poly->aabb.expand_to(r[i]);
  2874. }
  2875. }
  2876. rasterizer->canvas_light_occluder_set_polylines(occluder_poly->occluder,p_shape);
  2877. for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) {
  2878. E->get()->aabb_cache=occluder_poly->aabb;
  2879. }
  2880. }
  2881. void VisualServerRaster::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon,CanvasOccluderPolygonCullMode p_mode) {
  2882. CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  2883. ERR_FAIL_COND(!occluder_poly);
  2884. occluder_poly->cull_mode=p_mode;
  2885. for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) {
  2886. E->get()->cull_cache=p_mode;
  2887. }
  2888. }
  2889. RID VisualServerRaster::canvas_item_material_create() {
  2890. Rasterizer::CanvasItemMaterial *material = memnew( Rasterizer::CanvasItemMaterial );
  2891. return canvas_item_material_owner.make_rid(material);
  2892. }
  2893. void VisualServerRaster::canvas_item_material_set_shader(RID p_material, RID p_shader){
  2894. VS_CHANGED;
  2895. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2896. ERR_FAIL_COND(!material);
  2897. material->shader=p_shader;
  2898. }
  2899. void VisualServerRaster::canvas_item_material_set_shader_param(RID p_material, const StringName& p_param, const Variant& p_value){
  2900. VS_CHANGED;
  2901. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2902. ERR_FAIL_COND(!material);
  2903. if (p_value.get_type()==Variant::NIL)
  2904. material->shader_param.erase(p_param);
  2905. else
  2906. material->shader_param[p_param]=p_value;
  2907. }
  2908. Variant VisualServerRaster::canvas_item_material_get_shader_param(RID p_material, const StringName& p_param) const{
  2909. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2910. ERR_FAIL_COND_V(!material,Variant());
  2911. if (!material->shader_param.has(p_param)) {
  2912. ERR_FAIL_COND_V(!material->shader.is_valid(),Variant());
  2913. return rasterizer->shader_get_default_param(material->shader,p_param);
  2914. }
  2915. return material->shader_param[p_param];
  2916. }
  2917. void VisualServerRaster::canvas_item_material_set_shading_mode(RID p_material, CanvasItemShadingMode p_mode) {
  2918. VS_CHANGED;
  2919. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2920. ERR_FAIL_COND(!material);
  2921. material->shading_mode=p_mode;
  2922. }
  2923. /******** CANVAS *********/
  2924. void VisualServerRaster::cursor_set_rotation(float p_rotation, int p_cursor) {
  2925. VS_CHANGED;
  2926. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2927. cursors[p_cursor].rot = p_rotation;
  2928. };
  2929. void VisualServerRaster::cursor_set_texture(RID p_texture, const Point2 &p_center_offset, int p_cursor) {
  2930. VS_CHANGED;
  2931. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2932. cursors[p_cursor].texture = p_texture;
  2933. cursors[p_cursor].center = p_center_offset;
  2934. };
  2935. void VisualServerRaster::cursor_set_visible(bool p_visible, int p_cursor) {
  2936. VS_CHANGED;
  2937. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2938. cursors[p_cursor].visible = p_visible;
  2939. };
  2940. void VisualServerRaster::cursor_set_pos(const Point2& p_pos, int p_cursor) {
  2941. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2942. if (cursors[p_cursor].pos==p_pos)
  2943. return;
  2944. VS_CHANGED;
  2945. cursors[p_cursor].pos = p_pos;
  2946. };
  2947. void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  2948. black_margin[MARGIN_LEFT]=p_left;
  2949. black_margin[MARGIN_TOP]=p_top;
  2950. black_margin[MARGIN_RIGHT]=p_right;
  2951. black_margin[MARGIN_BOTTOM]=p_bottom;
  2952. }
  2953. void VisualServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  2954. black_image[MARGIN_LEFT]=p_left;
  2955. black_image[MARGIN_TOP]=p_top;
  2956. black_image[MARGIN_RIGHT]=p_right;
  2957. black_image[MARGIN_BOTTOM]=p_bottom;
  2958. }
  2959. void VisualServerRaster::_free_attached_instances(RID p_rid,bool p_free_scenario) {
  2960. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  2961. if (E) {
  2962. // has instances
  2963. while( E->get().size() ) {
  2964. // erase all attached instances
  2965. if (p_free_scenario)
  2966. instance_set_scenario( E->get().front()->get(), RID() );
  2967. else
  2968. instance_set_base( E->get().front()->get(), RID() );
  2969. }
  2970. }
  2971. instance_dependency_map.erase(p_rid);
  2972. }
  2973. void VisualServerRaster::custom_shade_model_set_shader(int p_model, RID p_shader) {
  2974. VS_CHANGED;
  2975. // rasterizer->custom_shade_model_set_shader(p_model,p_shader);
  2976. }
  2977. RID VisualServerRaster::custom_shade_model_get_shader(int p_model) const {
  2978. //return rasterizer->custom_shade_model_get_shader(p_model);
  2979. return RID();
  2980. }
  2981. void VisualServerRaster::custom_shade_model_set_name(int p_model, const String& p_name) {
  2982. //rasterizer->custom_shade_model_set_name(p_model,p_name);
  2983. }
  2984. String VisualServerRaster::custom_shade_model_get_name(int p_model) const {
  2985. //return rasterizer->custom_shade_model_get_name(p_model);
  2986. return "";
  2987. }
  2988. void VisualServerRaster::custom_shade_model_set_param_info(int p_model, const List<PropertyInfo>& p_info) {
  2989. VS_CHANGED;
  2990. //rasterizer->custom_shade_model_set_param_info(p_model,p_info);
  2991. }
  2992. void VisualServerRaster::custom_shade_model_get_param_info(int p_model, List<PropertyInfo>* p_info) const {
  2993. //rasterizer->custom_shade_model_get_param_info(p_model,p_info);
  2994. }
  2995. void VisualServerRaster::free( RID p_rid ) {
  2996. VS_CHANGED;
  2997. if (rasterizer->is_texture(p_rid) || rasterizer->is_material(p_rid) || rasterizer->is_shader(p_rid) || rasterizer->is_environment(p_rid)) {
  2998. rasterizer->free(p_rid);
  2999. } else if (rasterizer->is_skeleton(p_rid)) {
  3000. Map< RID, Set<Instance*> >::Element *E=skeleton_dependency_map.find(p_rid);
  3001. if (E) {
  3002. //detach skeletons
  3003. for (Set<Instance*>::Element *F=E->get().front();F;F=F->next()) {
  3004. F->get()->data.skeleton=RID();
  3005. }
  3006. skeleton_dependency_map.erase(E);
  3007. }
  3008. rasterizer->free(p_rid);
  3009. } else if (rasterizer->is_mesh(p_rid) || rasterizer->is_multimesh(p_rid) || rasterizer->is_light(p_rid) || rasterizer->is_particles(p_rid) || rasterizer->is_immediate(p_rid)) {
  3010. //delete the resource
  3011. _free_attached_instances(p_rid);
  3012. rasterizer->free(p_rid);
  3013. } else if (room_owner.owns(p_rid)) {
  3014. _free_attached_instances(p_rid);
  3015. Room *room = room_owner.get(p_rid);
  3016. ERR_FAIL_COND(!room);
  3017. room_owner.free(p_rid);
  3018. memdelete(room);
  3019. } else if (portal_owner.owns(p_rid)) {
  3020. _free_attached_instances(p_rid);
  3021. Portal *portal = portal_owner.get(p_rid);
  3022. ERR_FAIL_COND(!portal);
  3023. portal_owner.free(p_rid);
  3024. memdelete(portal);
  3025. } else if (baked_light_owner.owns(p_rid)) {
  3026. _free_attached_instances(p_rid);
  3027. BakedLight *baked_light = baked_light_owner.get(p_rid);
  3028. ERR_FAIL_COND(!baked_light);
  3029. if (baked_light->data.octree_texture.is_valid())
  3030. rasterizer->free(baked_light->data.octree_texture);
  3031. baked_light_owner.free(p_rid);
  3032. memdelete(baked_light);
  3033. } else if (baked_light_sampler_owner.owns(p_rid)) {
  3034. _free_attached_instances(p_rid);
  3035. BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get(p_rid);
  3036. ERR_FAIL_COND(!baked_light_sampler);
  3037. //if (baked_light->data.octree_texture.is_valid())
  3038. // rasterizer->free(baked_light->data.octree_texture);
  3039. baked_light_sampler_owner.free(p_rid);
  3040. memdelete(baked_light_sampler);
  3041. } else if (camera_owner.owns(p_rid)) {
  3042. // delete te camera
  3043. Camera *camera = camera_owner.get(p_rid);
  3044. ERR_FAIL_COND(!camera);
  3045. camera_owner.free( p_rid );
  3046. memdelete(camera);
  3047. } else if (viewport_owner.owns(p_rid)) {
  3048. // delete the viewport
  3049. Viewport *viewport = viewport_owner.get( p_rid );
  3050. ERR_FAIL_COND(!viewport);
  3051. // Viewport *parent=NULL;
  3052. rasterizer->free(viewport->viewport_data);
  3053. if (viewport->render_target.is_valid()) {
  3054. rasterizer->free(viewport->render_target);
  3055. }
  3056. if (viewport->update_list.in_list())
  3057. viewport_update_list.remove(&viewport->update_list);
  3058. if (screen_viewports.has(p_rid))
  3059. screen_viewports.erase(p_rid);
  3060. while(viewport->canvas_map.size()) {
  3061. Canvas *c = viewport->canvas_map.front()->get().canvas;
  3062. c->viewports.erase(p_rid);
  3063. viewport->canvas_map.erase(viewport->canvas_map.front());
  3064. }
  3065. viewport_owner.free(p_rid);
  3066. memdelete(viewport);
  3067. } else if (instance_owner.owns(p_rid)) {
  3068. // delete the instance
  3069. _update_instances(); // be sure
  3070. Instance *instance = instance_owner.get(p_rid);
  3071. ERR_FAIL_COND(!instance);
  3072. instance_set_room(p_rid,RID());
  3073. instance_set_scenario(p_rid,RID());
  3074. instance_geometry_set_baked_light(p_rid,RID());
  3075. instance_geometry_set_baked_light_sampler(p_rid,RID());
  3076. instance_set_base(p_rid,RID());
  3077. if (instance->data.skeleton.is_valid())
  3078. instance_attach_skeleton(p_rid,RID());
  3079. instance_owner.free(p_rid);
  3080. memdelete(instance);
  3081. } else if (canvas_owner.owns(p_rid)) {
  3082. Canvas *canvas = canvas_owner.get(p_rid);
  3083. ERR_FAIL_COND(!canvas);
  3084. while(canvas->viewports.size()) {
  3085. Viewport *vp = viewport_owner.get(canvas->viewports.front()->get());
  3086. ERR_FAIL_COND(!vp);
  3087. Map<RID,Viewport::CanvasData>::Element *E=vp->canvas_map.find(p_rid);
  3088. ERR_FAIL_COND(!E);
  3089. vp->canvas_map.erase(p_rid);
  3090. canvas->viewports.erase( canvas->viewports.front() );
  3091. }
  3092. for (int i=0;i<canvas->child_items.size();i++) {
  3093. canvas->child_items[i].item->parent=RID();
  3094. }
  3095. for (Set<Rasterizer::CanvasLight*>::Element *E=canvas->lights.front();E;E=E->next()) {
  3096. E->get()->canvas=RID();
  3097. }
  3098. for (Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=canvas->occluders.front();E;E=E->next()) {
  3099. E->get()->canvas=RID();
  3100. }
  3101. canvas_owner.free( p_rid );
  3102. memdelete( canvas );
  3103. } else if (canvas_item_owner.owns(p_rid)) {
  3104. CanvasItem *canvas_item = canvas_item_owner.get(p_rid);
  3105. ERR_FAIL_COND(!canvas_item);
  3106. if (canvas_item->parent.is_valid()) {
  3107. if (canvas_owner.owns(canvas_item->parent)) {
  3108. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  3109. canvas->erase_item(canvas_item);
  3110. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  3111. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  3112. item_owner->child_items.erase(canvas_item);
  3113. }
  3114. }
  3115. for (int i=0;i<canvas_item->child_items.size();i++) {
  3116. canvas_item->child_items[i]->parent=RID();
  3117. }
  3118. if (canvas_item->material) {
  3119. canvas_item->material->owners.erase(canvas_item);
  3120. }
  3121. canvas_item_owner.free( p_rid );
  3122. memdelete( canvas_item );
  3123. } else if (canvas_item_material_owner.owns(p_rid)) {
  3124. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get(p_rid);
  3125. ERR_FAIL_COND(!material);
  3126. for(Set<Rasterizer::CanvasItem*>::Element *E=material->owners.front();E;E=E->next()) {
  3127. E->get()->material=NULL;
  3128. }
  3129. canvas_item_material_owner.free(p_rid);
  3130. memdelete(material);
  3131. } else if (canvas_light_owner.owns(p_rid)) {
  3132. Rasterizer::CanvasLight *canvas_light = canvas_light_owner.get(p_rid);
  3133. ERR_FAIL_COND(!canvas_light);
  3134. if (canvas_light->canvas.is_valid()) {
  3135. Canvas* canvas = canvas_owner.get(canvas_light->canvas);
  3136. if (canvas)
  3137. canvas->lights.erase(canvas_light);
  3138. }
  3139. if (canvas_light->shadow_buffer.is_valid())
  3140. rasterizer->free(canvas_light->shadow_buffer);
  3141. canvas_light_owner.free( p_rid );
  3142. memdelete( canvas_light );
  3143. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  3144. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid);
  3145. ERR_FAIL_COND(!occluder);
  3146. if (occluder->polygon.is_valid()) {
  3147. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  3148. if (occluder_poly) {
  3149. occluder_poly->owners.erase(occluder);
  3150. }
  3151. }
  3152. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  3153. Canvas *canvas = canvas_owner.get(occluder->canvas);
  3154. canvas->occluders.erase(occluder);
  3155. }
  3156. canvas_light_occluder_owner.free( p_rid );
  3157. memdelete(occluder);
  3158. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  3159. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid);
  3160. ERR_FAIL_COND(!occluder_poly);
  3161. rasterizer->free(occluder_poly->occluder);
  3162. while(occluder_poly->owners.size()) {
  3163. occluder_poly->owners.front()->get()->polygon=RID();
  3164. occluder_poly->owners.erase( occluder_poly->owners.front() );
  3165. }
  3166. canvas_light_occluder_polygon_owner.free( p_rid );
  3167. memdelete(occluder_poly);
  3168. } else if (scenario_owner.owns(p_rid)) {
  3169. Scenario *scenario=scenario_owner.get(p_rid);
  3170. ERR_FAIL_COND(!scenario);
  3171. _update_instances(); // be sure
  3172. _free_attached_instances(p_rid,true);
  3173. //rasterizer->free( scenario->environment );
  3174. scenario_owner.free(p_rid);
  3175. memdelete(scenario);
  3176. } else {
  3177. ERR_FAIL();
  3178. }
  3179. }
  3180. void VisualServerRaster::_instance_draw(Instance *p_instance) {
  3181. if (p_instance->light_cache_dirty) {
  3182. int l=0;
  3183. //add positional lights
  3184. InstanceSet::Element *LE=p_instance->lights.front();
  3185. p_instance->data.light_instances.resize(p_instance->lights.size());
  3186. while(LE) {
  3187. p_instance->data.light_instances[l++]=LE->get()->light_info->instance;
  3188. LE=LE->next();
  3189. }
  3190. p_instance->light_cache_dirty=false;
  3191. }
  3192. switch(p_instance->base_type) {
  3193. case INSTANCE_MESH: {
  3194. const float *morphs = NULL;
  3195. if (!p_instance->data.morph_values.empty()) {
  3196. morphs=&p_instance->data.morph_values[0];
  3197. }
  3198. rasterizer->add_mesh(p_instance->base_rid, &p_instance->data);
  3199. } break;
  3200. case INSTANCE_MULTIMESH: {
  3201. rasterizer->add_multimesh(p_instance->base_rid, &p_instance->data);
  3202. } break;
  3203. case INSTANCE_IMMEDIATE: {
  3204. rasterizer->add_immediate(p_instance->base_rid, &p_instance->data);
  3205. } break;
  3206. case INSTANCE_PARTICLES: {
  3207. rasterizer->add_particles(p_instance->particles_info->instance, &p_instance->data);
  3208. } break;
  3209. default: {};
  3210. }
  3211. }
  3212. Vector<Vector3> VisualServerRaster::_camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  3213. // setup a camera matrix for that range!
  3214. CameraMatrix camera_matrix;
  3215. switch(p_camera->type) {
  3216. case Camera::ORTHOGONAL: {
  3217. camera_matrix.set_orthogonal(p_camera->size,viewport_rect.width / (float)viewport_rect.height,p_range_min,p_range_max,p_camera->vaspect);
  3218. } break;
  3219. case Camera::PERSPECTIVE: {
  3220. camera_matrix.set_perspective(
  3221. p_camera->fov,
  3222. viewport_rect.width / (float)viewport_rect.height,
  3223. p_range_min,
  3224. p_range_max,
  3225. p_camera->vaspect
  3226. );
  3227. } break;
  3228. }
  3229. //obtain the frustum endpoints
  3230. Vector<Vector3> endpoints;
  3231. endpoints.resize(8);
  3232. bool res = camera_matrix.get_endpoints(p_camera->transform,&endpoints[0]);
  3233. ERR_FAIL_COND_V(!res,Vector<Vector3>());
  3234. return endpoints;
  3235. }
  3236. Vector<Plane> VisualServerRaster::_camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  3237. Vector<Vector3> endpoints=_camera_generate_endpoints(p_light,p_camera,p_range_min,p_range_max); // frustum plane endpoints
  3238. ERR_FAIL_COND_V(endpoints.empty(),Vector<Plane>());
  3239. // obtain the light frustm ranges (given endpoints)
  3240. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3241. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3242. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3243. float x_min,x_max;
  3244. float y_min,y_max;
  3245. float z_min,z_max;
  3246. for(int j=0;j<8;j++) {
  3247. float d_x=x_vec.dot(endpoints[j]);
  3248. float d_y=y_vec.dot(endpoints[j]);
  3249. float d_z=z_vec.dot(endpoints[j]);
  3250. if (j==0 || d_x<x_min)
  3251. x_min=d_x;
  3252. if (j==0 || d_x>x_max)
  3253. x_max=d_x;
  3254. if (j==0 || d_y<y_min)
  3255. y_min=d_y;
  3256. if (j==0 || d_y>y_max)
  3257. y_max=d_y;
  3258. if (j==0 || d_z<z_min)
  3259. z_min=d_z;
  3260. if (j==0 || d_z>z_max)
  3261. z_max=d_z;
  3262. }
  3263. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3264. Vector<Plane> light_frustum_planes;
  3265. light_frustum_planes.resize(6);
  3266. //right/left
  3267. light_frustum_planes[0]=Plane( x_vec, x_max );
  3268. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  3269. //top/bottom
  3270. light_frustum_planes[2]=Plane( y_vec, y_max );
  3271. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  3272. //near/far
  3273. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  3274. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  3275. //TODO@ add more actual frustum planes to minimize get
  3276. return light_frustum_planes;
  3277. }
  3278. void VisualServerRaster::_light_instance_update_pssm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3279. int splits = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  3280. float split_weight=rasterizer->light_directional_get_shadow_param(p_light->base_rid,LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT);
  3281. float distances[5];
  3282. float texsize=rasterizer->light_instance_get_shadow_size( p_light->light_info->instance );
  3283. // float cull_min=p_cull_range.min;
  3284. //float cull_max=p_cull_range.max;
  3285. bool overlap = rasterizer->light_instance_get_pssm_shadow_overlap(p_light->light_info->instance);
  3286. float cull_min=p_camera->znear;
  3287. float cull_max=p_camera->zfar;
  3288. float max_dist = rasterizer->light_directional_get_shadow_param(p_light->base_rid,VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE);
  3289. if (max_dist>0.0)
  3290. cull_max=MIN(cull_max,max_dist);
  3291. for(int i = 0; i < splits; i++) {
  3292. float idm = i / (float)splits;
  3293. float lg = cull_min * Math::pow(cull_max/cull_min, idm);
  3294. float uniform = cull_min + (cull_max - cull_min) * idm;
  3295. distances[i] = lg * split_weight + uniform * (1.0 - split_weight);
  3296. }
  3297. distances[0]=cull_min;
  3298. distances[splits]=cull_max;
  3299. for (int i=0;i<splits;i++) {
  3300. // setup a camera matrix for that range!
  3301. CameraMatrix camera_matrix;
  3302. switch(p_camera->type) {
  3303. case Camera::ORTHOGONAL: {
  3304. camera_matrix.set_orthogonal(
  3305. p_camera->size,
  3306. viewport_rect.width / (float)viewport_rect.height,
  3307. distances[(i==0 || !overlap )?i:i-1],
  3308. distances[i+1],
  3309. p_camera->vaspect
  3310. );
  3311. } break;
  3312. case Camera::PERSPECTIVE: {
  3313. camera_matrix.set_perspective(
  3314. p_camera->fov,
  3315. viewport_rect.width / (float)viewport_rect.height,
  3316. distances[(i==0 || !overlap )?i:i-1],
  3317. distances[i+1],
  3318. p_camera->vaspect
  3319. );
  3320. } break;
  3321. }
  3322. //obtain the frustum endpoints
  3323. Vector3 endpoints[8]; // frustum plane endpoints
  3324. bool res = camera_matrix.get_endpoints(p_camera->transform,endpoints);
  3325. ERR_CONTINUE(!res);
  3326. // obtain the light frustm ranges (given endpoints)
  3327. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3328. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3329. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3330. //z_vec points agsint the camera, like in default opengl
  3331. float x_min,x_max;
  3332. float y_min,y_max;
  3333. float z_min,z_max;
  3334. float x_min_cam,x_max_cam;
  3335. float y_min_cam,y_max_cam;
  3336. float z_min_cam,z_max_cam;
  3337. //used for culling
  3338. for(int j=0;j<8;j++) {
  3339. float d_x=x_vec.dot(endpoints[j]);
  3340. float d_y=y_vec.dot(endpoints[j]);
  3341. float d_z=z_vec.dot(endpoints[j]);
  3342. if (j==0 || d_x<x_min)
  3343. x_min=d_x;
  3344. if (j==0 || d_x>x_max)
  3345. x_max=d_x;
  3346. if (j==0 || d_y<y_min)
  3347. y_min=d_y;
  3348. if (j==0 || d_y>y_max)
  3349. y_max=d_y;
  3350. if (j==0 || d_z<z_min)
  3351. z_min=d_z;
  3352. if (j==0 || d_z>z_max)
  3353. z_max=d_z;
  3354. }
  3355. {
  3356. //camera viewport stuff
  3357. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  3358. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  3359. Vector3 center;
  3360. for(int j=0;j<8;j++) {
  3361. center+=endpoints[j];
  3362. }
  3363. center/=8.0;
  3364. //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;
  3365. float radius=0;
  3366. for(int j=0;j<8;j++) {
  3367. float d = center.distance_to(endpoints[j]);
  3368. if (d>radius)
  3369. radius=d;
  3370. }
  3371. radius *= texsize/(texsize-2.0); //add a texel by each side, so stepified texture will always fit
  3372. x_max_cam=x_vec.dot(center)+radius;
  3373. x_min_cam=x_vec.dot(center)-radius;
  3374. y_max_cam=y_vec.dot(center)+radius;
  3375. y_min_cam=y_vec.dot(center)-radius;
  3376. z_max_cam=z_vec.dot(center)+radius;
  3377. z_min_cam=z_vec.dot(center)-radius;
  3378. float unit = radius*2.0/texsize;
  3379. x_max_cam=Math::stepify(x_max_cam,unit);
  3380. x_min_cam=Math::stepify(x_min_cam,unit);
  3381. y_max_cam=Math::stepify(y_max_cam,unit);
  3382. y_min_cam=Math::stepify(y_min_cam,unit);
  3383. }
  3384. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3385. Vector<Plane> light_frustum_planes;
  3386. light_frustum_planes.resize(6);
  3387. //right/left
  3388. light_frustum_planes[0]=Plane( x_vec, x_max );
  3389. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  3390. //top/bottom
  3391. light_frustum_planes[2]=Plane( y_vec, y_max );
  3392. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  3393. //near/far
  3394. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  3395. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  3396. int caster_cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3397. // a pre pass will need to be needed to determine the actual z-near to be used
  3398. for(int j=0;j<caster_cull_count;j++) {
  3399. float min,max;
  3400. Instance *ins=instance_shadow_cull_result[j];
  3401. if (!ins->visible || ins->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF)
  3402. continue;
  3403. ins->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max);
  3404. if (max>z_max)
  3405. z_max=max;
  3406. }
  3407. {
  3408. CameraMatrix ortho_camera;
  3409. real_t half_x = (x_max_cam-x_min_cam) * 0.5;
  3410. real_t half_y = (y_max_cam-y_min_cam) * 0.5;
  3411. ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) );
  3412. Transform ortho_transform;
  3413. ortho_transform.basis=p_light->data.transform.basis;
  3414. ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max;
  3415. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance, i, ortho_camera, ortho_transform,distances[i],distances[i+1] );
  3416. }
  3417. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  3418. for (int j=0;j<caster_cull_count;j++) {
  3419. Instance *instance = instance_shadow_cull_result[j];
  3420. if (!instance->visible || instance->data.cast_shadows==VS::SHADOW_CASTING_SETTING_OFF)
  3421. continue;
  3422. _instance_draw(instance);
  3423. }
  3424. rasterizer->end_shadow_map();
  3425. }
  3426. }
  3427. CameraMatrix _lispm_look( const Vector3 pos, const Vector3 dir, const Vector3 up) {
  3428. Vector3 dirN;
  3429. Vector3 upN;
  3430. Vector3 lftN;
  3431. lftN=dir.cross(up);
  3432. lftN.normalize();
  3433. upN=lftN.cross(dir);
  3434. upN.normalize();
  3435. dirN=dir.normalized();
  3436. CameraMatrix cmout;
  3437. float *output=&cmout.matrix[0][0];
  3438. output[ 0] = lftN[0];
  3439. output[ 1] = upN[0];
  3440. output[ 2] = -dirN[0];
  3441. output[ 3] = 0.0;
  3442. output[ 4] = lftN[1];
  3443. output[ 5] = upN[1];
  3444. output[ 6] = -dirN[1];
  3445. output[ 7] = 0.0;
  3446. output[ 8] = lftN[2];
  3447. output[ 9] = upN[2];
  3448. output[10] = -dirN[2];
  3449. output[11] = 0.0;
  3450. output[12] = -lftN.dot(pos);
  3451. output[13] = -upN.dot(pos);
  3452. output[14] = dirN.dot(pos);
  3453. output[15] = 1.0;
  3454. return cmout;
  3455. }
  3456. #if 1
  3457. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3458. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  3459. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  3460. float viewdot = light_vec.normalized().dot(view_vec.normalized());
  3461. float near_dist=1;
  3462. Vector<Plane> light_frustum_planes = _camera_generate_orthogonal_planes(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3463. int caster_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3464. // this could be faster by just getting supports from the AABBs..
  3465. // but, safer to do as the original implementation explains for now..
  3466. Vector<Vector3> caster_pointcloud;
  3467. caster_pointcloud.resize(caster_count*8);
  3468. int caster_pointcloud_size=0;
  3469. {
  3470. //fill pointcloud
  3471. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3472. for(int i=0;i<caster_count;i++) {
  3473. Instance *ins = instance_shadow_cull_result[i];
  3474. if (!ins->visible || ins->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF)
  3475. continue;
  3476. for(int j=0;j<8;j++) {
  3477. Vector3 v = ins->aabb.get_endpoint(j);
  3478. v = ins->data.transform.xform(v);
  3479. caster_pointcloud_ptr[caster_pointcloud_size+j]=v;
  3480. }
  3481. caster_pointcloud_size+=8;
  3482. }
  3483. }
  3484. // now generate a pointcloud that contains the maximum bound (camera extruded by light)
  3485. Vector<Vector3> camera_pointcloud = _camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3486. int cpcsize=camera_pointcloud.size();
  3487. camera_pointcloud.resize( cpcsize*2 );
  3488. for(int i=0;i<cpcsize;i++) {
  3489. camera_pointcloud[i+cpcsize]=camera_pointcloud[i]-light_vec*1000;
  3490. }
  3491. // Vector<Vector3> frustum_points=_camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3492. // compute the "light-space" basis, using the algorithm described in the paper
  3493. // note: since bodyB is defined in eye space, all of these vectors should also be defined in eye space
  3494. Vector3 eye = p_camera->transform.origin;
  3495. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  3496. CameraMatrix light_space_basis = _lispm_look(eye,light_vec,up);
  3497. AABB light_space_aabb;
  3498. { //create an optimal AABB from both the camera pointcloud and the objects pointcloud
  3499. AABB light_space_pointcloud_aabb;
  3500. AABB light_space_camera_aabb;
  3501. //xform pointcloud
  3502. const Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3503. for(int i=0;i<caster_pointcloud_size;i++) {
  3504. Vector3 p = light_space_basis.xform(caster_pointcloud_ptr[i]);
  3505. if (i==0) {
  3506. light_space_pointcloud_aabb.pos=p;
  3507. } else {
  3508. light_space_pointcloud_aabb.expand_to(p);
  3509. }
  3510. }
  3511. for(int i=0;i<camera_pointcloud.size();i++) {
  3512. Vector3 p = light_space_basis.xform(camera_pointcloud[i]);
  3513. if (i==0) {
  3514. light_space_camera_aabb.pos=p;
  3515. } else {
  3516. light_space_camera_aabb.expand_to(p);
  3517. }
  3518. }
  3519. light_space_aabb=light_space_pointcloud_aabb.intersection(light_space_camera_aabb);
  3520. }
  3521. float lvdp = light_vec.dot(view_vec);
  3522. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  3523. //use the formulas of the paper to get n (and f)
  3524. float factor = 1.0/sin_gamma;
  3525. float z_n = factor*near_dist; //often 1
  3526. float d = Math::abs(light_space_aabb.size.y); //perspective transform depth //light space y extents
  3527. float z_f = z_n + d*sin_gamma;
  3528. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  3529. float f = n+d;
  3530. Vector3 pos = eye - up*(n-near_dist);
  3531. CameraMatrix light_space_basis2 = _lispm_look(pos,light_vec,up);
  3532. //Transform light_space_basis2;
  3533. //light_space_basis2.set_look_at(pos,light_vec-pos,up);
  3534. //light_space_basis2.affine_invert();
  3535. //one possibility for a simple perspective transformation matrix
  3536. //with the two parameters n(near) and f(far) in y direction
  3537. CameraMatrix lisp_matrix;
  3538. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  3539. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  3540. lisp_matrix.matrix[1][3]=1;
  3541. lisp_matrix.matrix[3][3]=0;
  3542. CameraMatrix projection = lisp_matrix * light_space_basis2;
  3543. //CameraMatrix projection = light_space_basis2 * lisp_matrix;
  3544. AABB proj_space_aabb;
  3545. {
  3546. AABB proj_space_pointcloud_aabb;
  3547. AABB proj_space_camera_aabb;
  3548. //xform pointcloud
  3549. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3550. for(int i=0;i<caster_pointcloud_size;i++) {
  3551. Vector3 p = projection.xform(caster_pointcloud_ptr[i]);
  3552. if (i==0) {
  3553. proj_space_pointcloud_aabb.pos=p;
  3554. } else {
  3555. proj_space_pointcloud_aabb.expand_to(p);
  3556. }
  3557. }
  3558. for(int i=0;i<camera_pointcloud.size();i++) {
  3559. Vector3 p = projection.xform(camera_pointcloud[i]);
  3560. if (i==0) {
  3561. proj_space_camera_aabb.pos=p;
  3562. } else {
  3563. proj_space_camera_aabb.expand_to(p);
  3564. }
  3565. }
  3566. //proj_space_aabb=proj_space_pointcloud_aabb.intersection_with(proj_space_camera_aabb);
  3567. proj_space_aabb=proj_space_pointcloud_aabb;
  3568. }
  3569. projection.scale_translate_to_fit(proj_space_aabb);
  3570. projection=projection * lisp_matrix;
  3571. CameraMatrix scale;
  3572. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  3573. projection=scale * projection;
  3574. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection , light_space_basis2.inverse() );
  3575. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3576. for(int i=0;i<caster_count;i++) {
  3577. Instance *instance = instance_shadow_cull_result[i];
  3578. if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF)
  3579. continue;
  3580. _instance_draw(instance);
  3581. }
  3582. rasterizer->end_shadow_map();
  3583. }
  3584. #else
  3585. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3586. /* STEP 1: GENERATE LIGHT TRANSFORM */
  3587. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  3588. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  3589. float viewdot = Math::absf(light_vec.dot(view_vec));
  3590. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  3591. Transform light_transform;
  3592. light_transform.set_look_at(Vector3(),light_vec,up);
  3593. /* STEP 2: GENERATE WORDLSPACE PLANES AND VECTORS*/
  3594. float range_min=0.01; //p_cull_range.min
  3595. float range_max=20;//p_cull_range.max;
  3596. Vector<Vector3> camera_endpoints=_camera_generate_endpoints(p_light,p_camera,range_min,range_max); // frustum plane endpoints
  3597. ERR_FAIL_COND(camera_endpoints.empty());
  3598. // obtain the light frustm ranges (given endpoints)
  3599. Vector3 light_x_vec=light_transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3600. Vector3 light_y_vec=light_transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3601. Vector3 light_z_vec=light_transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3602. Vector3 light_axis_max;
  3603. Vector3 light_axis_min;
  3604. for(int j=0;j<8;j++) {
  3605. float d_x=light_x_vec.dot(camera_endpoints[j]);
  3606. float d_y=light_y_vec.dot(camera_endpoints[j]);
  3607. float d_z=light_z_vec.dot(camera_endpoints[j]);
  3608. if (j==0 || d_x<light_axis_min.x)
  3609. light_axis_min.x=d_x;
  3610. if (j==0 || d_x>light_axis_max.x)
  3611. light_axis_max.x=d_x;
  3612. if (j==0 || d_y<light_axis_min.y)
  3613. light_axis_min.y=d_y;
  3614. if (j==0 || d_y>light_axis_max.y)
  3615. light_axis_max.y=d_y;
  3616. if (j==0 || d_z<light_axis_min.z)
  3617. light_axis_min.z=d_z;
  3618. if (j==0 || d_z>light_axis_max.z)
  3619. light_axis_max.z=d_z;
  3620. }
  3621. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3622. Vector<Plane> light_cull_planes;
  3623. light_cull_planes.resize(6);
  3624. //right/left
  3625. light_cull_planes[0]=Plane( light_x_vec, light_axis_max.x );
  3626. light_cull_planes[1]=Plane( -light_x_vec, -light_axis_min.x );
  3627. //top/bottom
  3628. light_cull_planes[2]=Plane( light_y_vec, light_axis_max.y );
  3629. light_cull_planes[3]=Plane( -light_y_vec, -light_axis_min.y );
  3630. //near/far
  3631. light_cull_planes[4]=Plane( light_z_vec, light_axis_max.z+1e6 );
  3632. 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
  3633. /* STEP 3: CULL CASTERS */
  3634. int caster_count = p_scenario->octree.cull_convex(light_cull_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3635. /* STEP 4: ADJUST FAR Z PLANE */
  3636. float caster_max_z=1e-1;
  3637. for(int i=0;i<caster_count;i++) {
  3638. Instance *ins=instance_shadow_cull_result[i];
  3639. if (!ins->visible || ins->cast_shadows==VS::SHADOW_CASTING_SETTING_OFF)
  3640. continue;
  3641. //@TODO optimize using support mapping
  3642. for(int j=0;j<8;j++) {
  3643. Vector3 v=ins->data.transform.xform(ins->aabb.get_endpoint(j));
  3644. float d = light_z_vec.dot(v);
  3645. if (d>caster_max_z)
  3646. caster_max_z=d;
  3647. }
  3648. }
  3649. float expand = caster_max_z-light_axis_max.z;
  3650. if (expand<0)
  3651. expand=0;
  3652. light_axis_max.z=MAX(caster_max_z,light_axis_max.z);
  3653. /* STEP 5: CREATE ORTHOGONAL PROJECTION */
  3654. CameraMatrix light_projection;
  3655. real_t half_x = (light_axis_max.x-light_axis_min.x) * 0.5;
  3656. real_t half_y = (light_axis_max.y-light_axis_min.y) * 0.5;
  3657. light_projection.set_orthogonal( -half_x, half_x,half_y, -half_y, 0, (light_axis_max.z-light_axis_min.z) );
  3658. 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;
  3659. if (/*false &&*/ viewdot<0.96) {
  3660. float lvdp = light_vec.dot(view_vec);
  3661. float near_dist=1.0;
  3662. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  3663. //use the formulas of the paper to get n (and f)
  3664. float factor = 1.0/sin_gamma;
  3665. float z_n = factor*near_dist; //often 1
  3666. float d = Math::abs(light_axis_max.y-light_axis_min.y); //perspective transform depth //light space y extents
  3667. float z_f = z_n + d*sin_gamma;
  3668. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  3669. float f = n+d;
  3670. CameraMatrix lisp_matrix;
  3671. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  3672. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  3673. lisp_matrix.matrix[1][3]=1;
  3674. lisp_matrix.matrix[3][3]=0;
  3675. Vector3 pos = p_camera->transform.origin - up*(n-near_dist);
  3676. CameraMatrix world2light = _lispm_look(pos,light_vec,up);
  3677. CameraMatrix projection = lisp_matrix * world2light;
  3678. AABB projection_bounds;
  3679. for(int i=0;i<camera_endpoints.size();i++) {
  3680. Vector3 p=camera_endpoints[i];
  3681. if (i==0)
  3682. projection_bounds.pos=projection.xform(p);
  3683. else
  3684. projection_bounds.expand_to(projection.xform(p));
  3685. projection_bounds.expand_to(projection.xform(p+light_vec*-expand));
  3686. }
  3687. CameraMatrix scaletrans;
  3688. scaletrans.scale_translate_to_fit(projection_bounds);
  3689. projection=scaletrans * lisp_matrix;
  3690. CameraMatrix scale;
  3691. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  3692. projection=scale * projection;
  3693. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection, world2light.inverse(), viewdot);
  3694. } else {
  3695. //orthogonal
  3696. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, light_projection , light_transform, viewdot);
  3697. }
  3698. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3699. for(int i=0;i<caster_count;i++) {
  3700. Instance *instance = instance_shadow_cull_result[i];
  3701. if (!instance->visible || instance->cast_shadows==VS::SHADOW_CASTING_SETTING_OFF)
  3702. continue;
  3703. _instance_draw(instance);
  3704. }
  3705. rasterizer->end_shadow_map();
  3706. }
  3707. #endif
  3708. void VisualServerRaster::_light_instance_update_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3709. if (!rasterizer->shadow_allocate_near( p_light->light_info->instance ))
  3710. return; // shadow could not be updated
  3711. /* VisualServerRaster supports for many shadow techniques, using the one the rasterizer requests */
  3712. Rasterizer::ShadowType shadow_type = rasterizer->light_instance_get_shadow_type(p_light->light_info->instance);
  3713. switch(shadow_type) {
  3714. case Rasterizer::SHADOW_SIMPLE: {
  3715. /* SPOT SHADOW */
  3716. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3717. //using this one ensures that raster deferred will have it
  3718. float far = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3719. float angle = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_SPOT_ANGLE );
  3720. CameraMatrix cm;
  3721. cm.set_perspective( angle*2.0, 1.0, 0.001, far );
  3722. Vector<Plane> planes = cm.get_projection_planes(p_light->data.transform);
  3723. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3724. for (int i=0;i<cull_count;i++) {
  3725. Instance *instance = instance_shadow_cull_result[i];
  3726. if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF)
  3727. continue;
  3728. _instance_draw(instance);
  3729. }
  3730. rasterizer->end_shadow_map();
  3731. } break;
  3732. case Rasterizer::SHADOW_DUAL_PARABOLOID: {
  3733. /* OMNI SHADOW */
  3734. int passes = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  3735. if (passes==2) {
  3736. for(int i=0;i<2;i++) {
  3737. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  3738. //using this one ensures that raster deferred will have it
  3739. float radius = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3740. float z =i==0?-1:1;
  3741. Vector<Plane> planes;
  3742. planes.resize(5);
  3743. planes[0]=p_light->data.transform.xform(Plane(Vector3(0,0,z),radius));
  3744. planes[1]=p_light->data.transform.xform(Plane(Vector3(1,0,z).normalized(),radius));
  3745. planes[2]=p_light->data.transform.xform(Plane(Vector3(-1,0,z).normalized(),radius));
  3746. planes[3]=p_light->data.transform.xform(Plane(Vector3(0,1,z).normalized(),radius));
  3747. planes[4]=p_light->data.transform.xform(Plane(Vector3(0,-1,z).normalized(),radius));
  3748. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3749. for (int j=0;j<cull_count;j++) {
  3750. Instance *instance = instance_shadow_cull_result[j];
  3751. if (!instance->visible || instance->data.cast_shadows == VS::SHADOW_CASTING_SETTING_OFF)
  3752. continue;
  3753. _instance_draw(instance);
  3754. }
  3755. rasterizer->end_shadow_map();
  3756. }
  3757. } else if (passes==1) {
  3758. //one go
  3759. }
  3760. } break;
  3761. case Rasterizer::SHADOW_CUBE: {
  3762. // todo
  3763. } break;
  3764. case Rasterizer::SHADOW_ORTHOGONAL: {
  3765. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3766. } break;
  3767. case Rasterizer::SHADOW_PSSM: {
  3768. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3769. } break;
  3770. case Rasterizer::SHADOW_PSM: {
  3771. _light_instance_update_lispsm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3772. // todo
  3773. } break;
  3774. default: {}
  3775. }
  3776. }
  3777. void VisualServerRaster::_portal_disconnect(Instance *p_portal,bool p_cleanup) {
  3778. if (p_portal->portal_info->connected) {
  3779. //disconnect first
  3780. p_portal->portal_info->connected->portal_info->connected=NULL;
  3781. p_portal->portal_info->connected=NULL;
  3782. }
  3783. if (p_portal->room && p_portal->room->room) {
  3784. if (p_cleanup) {
  3785. p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3786. //p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3787. } else {
  3788. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3789. }
  3790. }
  3791. }
  3792. void VisualServerRaster::_instance_validate_autorooms(Instance *p_geometry) {
  3793. if (p_geometry->auto_rooms.size()==0)
  3794. return;
  3795. p_geometry->valid_auto_rooms.clear();
  3796. int point_count = aabb_random_points.size();
  3797. const Vector3 * src_points = &aabb_random_points[0];
  3798. for(Set<Instance*>::Element *E=p_geometry->valid_auto_rooms.front();E;E=E->next()) {
  3799. Instance *room = E->get();
  3800. Vector3 *dst_points=&transformed_aabb_random_points[0];
  3801. //generate points
  3802. for(int i=0;i<point_count;i++) {
  3803. 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));
  3804. }
  3805. int pass = room->room_info->room->bounds.get_points_inside(dst_points,point_count);
  3806. float ratio = pass;
  3807. if( point_count != 0 ) {
  3808. ratio /= (float)point_count;
  3809. }
  3810. if (ratio>0.5) // should make some constant
  3811. p_geometry->valid_auto_rooms.insert(room);
  3812. }
  3813. }
  3814. void VisualServerRaster::_portal_attempt_connect(Instance *p_portal) {
  3815. _portal_disconnect(p_portal);
  3816. Vector3 A_norm = p_portal->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3817. Plane A_plane( p_portal->data.transform.origin, A_norm );
  3818. float A_surface = p_portal->portal_info->portal->bounds.get_area();
  3819. if (A_surface==0)
  3820. return; //wtf
  3821. Instance *found=NULL;
  3822. Transform affine_inverse = p_portal->data.transform.affine_inverse();
  3823. for(Set<Instance*>::Element *E=p_portal->portal_info->candidate_set.front();E;E=E->next()) {
  3824. Instance *B = E->get();
  3825. if (B->portal_info->connected)
  3826. continue; // in use
  3827. Vector3 B_norm = B->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3828. // check that they are in front of another
  3829. float dot = A_norm.dot(-B_norm);
  3830. if (dot<0.707) // 45 degrees, TODO unharcode this
  3831. continue;
  3832. // check the max distance to the other portal
  3833. bool valid=true;
  3834. Rect2 local_bounds;
  3835. for(int i=0;i<B->portal_info->portal->shape.size();i++) {
  3836. Point2 point2 = B->portal_info->portal->shape[i];
  3837. Vector3 point = B->data.transform.xform( Vector3( point2.x, point2.y, 0 ) );
  3838. float dist = Math::abs(A_plane.distance_to(point));
  3839. if (
  3840. dist>p_portal->portal_info->portal->connect_range ||
  3841. dist>B->portal_info->portal->connect_range ) {
  3842. valid=false;
  3843. break;
  3844. }
  3845. Vector3 point_local = affine_inverse.xform(A_plane.project(point));
  3846. point2 = Point2(point_local.x,point_local.y);
  3847. if (i==0)
  3848. local_bounds.pos=point2;
  3849. else
  3850. local_bounds.expand_to(point2);
  3851. }
  3852. if (!valid)
  3853. continue;
  3854. float B_surface = B->portal_info->portal->bounds.get_area();
  3855. if (B_surface==0)
  3856. continue; //wtf
  3857. float clip_area = p_portal->portal_info->portal->bounds.clip(local_bounds).get_area();
  3858. //check that most of the area is shared
  3859. if ( (clip_area/A_surface) < 0.5 || (clip_area/B_surface) < 0.5) // TODO change for something else
  3860. continue;
  3861. found=B;
  3862. break;
  3863. }
  3864. if (!found) {
  3865. if (p_portal->room && p_portal->room->room) {
  3866. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3867. }
  3868. return;
  3869. }
  3870. p_portal->portal_info->connected=found;
  3871. found->portal_info->connected=p_portal;
  3872. }
  3873. void* VisualServerRaster::instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) {
  3874. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3875. Instance *A = p_A;
  3876. Instance *B = p_B;
  3877. if (A->base_type==INSTANCE_PORTAL) {
  3878. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  3879. A->portal_info->candidate_set.insert(B);
  3880. B->portal_info->candidate_set.insert(A);
  3881. self->_portal_attempt_connect(A);
  3882. //attempt to conncet portal A (will go through B anyway)
  3883. //this is a little hackish, but works fine in practice
  3884. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  3885. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  3886. SWAP(A,B);
  3887. }
  3888. ERR_FAIL_COND_V(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER,NULL);
  3889. B->baked_light_sampler_info->baked_lights.insert(A);
  3890. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3891. if (B->base_type==INSTANCE_ROOM)
  3892. SWAP(A,B);
  3893. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  3894. B->auto_rooms.insert(A);
  3895. A->room_info->owned_autoroom_geometry.insert(B);
  3896. self->_instance_validate_autorooms(B);
  3897. } else {
  3898. if (B->base_type==INSTANCE_LIGHT) {
  3899. SWAP(A,B);
  3900. } else if (A->base_type!=INSTANCE_LIGHT) {
  3901. return NULL;
  3902. }
  3903. A->light_info->affected.insert(B);
  3904. B->lights.insert(A);
  3905. B->light_cache_dirty=true;
  3906. }
  3907. return NULL;
  3908. }
  3909. void VisualServerRaster::instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void*) {
  3910. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3911. Instance *A = p_A;
  3912. Instance *B = p_B;
  3913. if (A->base_type==INSTANCE_PORTAL) {
  3914. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  3915. A->portal_info->candidate_set.erase(B);
  3916. B->portal_info->candidate_set.erase(A);
  3917. //after disconnecting them, see if they can connect again
  3918. self->_portal_attempt_connect(A);
  3919. self->_portal_attempt_connect(B);
  3920. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  3921. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  3922. SWAP(A,B);
  3923. }
  3924. ERR_FAIL_COND(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER);
  3925. B->baked_light_sampler_info->baked_lights.erase(A);
  3926. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3927. if (B->base_type==INSTANCE_ROOM)
  3928. SWAP(A,B);
  3929. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  3930. B->auto_rooms.erase(A);
  3931. B->valid_auto_rooms.erase(A);
  3932. A->room_info->owned_autoroom_geometry.erase(B);
  3933. }else {
  3934. if (B->base_type==INSTANCE_LIGHT) {
  3935. SWAP(A,B);
  3936. } else if (A->base_type!=INSTANCE_LIGHT) {
  3937. return;
  3938. }
  3939. A->light_info->affected.erase(B);
  3940. B->lights.erase(A);
  3941. B->light_cache_dirty=true;
  3942. }
  3943. }
  3944. bool VisualServerRaster::_test_portal_cull(Camera *p_camera, Instance *p_from_portal, Instance *p_to_portal) {
  3945. int src_point_count=p_from_portal->portal_info->transformed_point_cache.size();
  3946. int dst_point_count=p_to_portal->portal_info->transformed_point_cache.size();
  3947. if (src_point_count<2 || dst_point_count<2)
  3948. return false;
  3949. const Vector3 *src_points=&p_from_portal->portal_info->transformed_point_cache[0];
  3950. const Vector3 *dst_points=&p_to_portal->portal_info->transformed_point_cache[0];
  3951. bool outside=false;
  3952. bool clockwise = !p_from_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin);
  3953. for(int i=0;i<src_point_count;i++) {
  3954. const Vector3& point_prev = src_points[i?(i-1):(src_point_count-1)];
  3955. const Vector3& point = src_points[i];
  3956. Plane p = clockwise?Plane(p_camera->transform.origin,point,point_prev):Plane(p_camera->transform.origin,point_prev,point);
  3957. bool all_over=true;
  3958. for(int j=0;j<dst_point_count;j++) {
  3959. if (!p.is_point_over(dst_points[j])) {
  3960. all_over=false;
  3961. break;
  3962. }
  3963. }
  3964. if (all_over) {
  3965. outside=true;
  3966. break;
  3967. }
  3968. }
  3969. return !outside;
  3970. }
  3971. void VisualServerRaster::_cull_portal(Camera *p_camera, Instance *p_portal,Instance *p_from_portal) {
  3972. ERR_FAIL_COND(!p_portal->scenario); //scenario outside
  3973. Instance *portal = p_portal;
  3974. if (!portal->room) {
  3975. return; //portals need all to belong to a room, it may be unconfigured yet
  3976. } else if (portal->last_render_pass!=render_pass) {
  3977. return; //invalid portal, ignore
  3978. } else if (portal->portal_info->last_visited_pass==render_pass) {
  3979. return; //portal already visited
  3980. } else if (portal==p_from_portal) {
  3981. return; // came from this portal, don't even bother testing
  3982. }
  3983. /* TEST DISABLE DISTANCE */
  3984. float disable_distance = p_portal->portal_info->portal->disable_distance;
  3985. if (disable_distance) {
  3986. //has disable distance..
  3987. float distance = p_camera->transform.origin.distance_to(portal->data.transform.origin);
  3988. if (disable_distance < distance) {
  3989. return;
  3990. }
  3991. }
  3992. /* TEST PORTAL NOT FACING OPTIMIZATION */
  3993. if (p_portal->portal_info->connected) {
  3994. //connected portal means, it must face against the camera to be seen
  3995. if (p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3996. return;
  3997. }
  3998. } else {
  3999. //disconencted portals (go from room to parent room or exterior) must face towards the canera
  4000. if (!p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  4001. return;
  4002. }
  4003. }
  4004. if (p_from_portal && !_test_portal_cull(p_camera, p_from_portal, portal)) {
  4005. return; // portal not visible (culled)
  4006. }
  4007. portal->portal_info->last_visited_pass=render_pass;
  4008. if (portal->portal_info->connected) {
  4009. //interior<->interior portal
  4010. Instance *to_room = portal->portal_info->connected->room;
  4011. if (!to_room) {
  4012. return; //wtf.. oh well, connected to a roomless (invalid) portal
  4013. }
  4014. _cull_room(p_camera, to_room, portal->portal_info->connected);
  4015. } else {
  4016. //to exterior/to parent roomportal
  4017. Instance *parent_room = portal->room->room;
  4018. _cull_room(p_camera, parent_room, portal);
  4019. }
  4020. }
  4021. void VisualServerRaster::_cull_room(Camera *p_camera, Instance *p_room,Instance *p_from_portal) {
  4022. if (p_room==NULL) {
  4023. //exterior
  4024. exterior_visited=true;
  4025. for(int i=0;i<exterior_portal_cull_count;i++) {
  4026. _cull_portal(p_camera, exterior_portal_cull_result[i],p_from_portal);
  4027. }
  4028. } else {
  4029. ERR_FAIL_COND(!p_room->scenario);
  4030. if (p_room->last_render_pass!=render_pass)
  4031. return; //this room is invalid
  4032. //interior
  4033. //first of all, validate the room
  4034. p_room->room_info->last_visited_pass=render_pass;
  4035. //see about going around portals
  4036. if (!p_room->room_info->room->occlude_exterior)
  4037. exterior_visited=true;
  4038. for(List<Instance*>::Element * E=p_room->room_info->owned_portal_instances.front();E;E=E->next()) {
  4039. _cull_portal(p_camera, E->get(),p_from_portal);
  4040. }
  4041. for(Set<Instance*>::Element * E=p_room->room_info->disconnected_child_portals.front();E;E=E->next()) {
  4042. _cull_portal(p_camera, E->get(),p_from_portal);
  4043. }
  4044. }
  4045. }
  4046. void VisualServerRaster::_process_sampled_light(const Transform& p_camera,Instance *p_sampled_light,bool p_linear_colorspace) {
  4047. BakedLightSampler *sampler_opts = p_sampled_light->baked_light_sampler_info->sampler;
  4048. int res = sampler_opts->resolution;
  4049. int dp_size = res*res*2;
  4050. Color * dp_map = (Color*)alloca( sizeof(Color)*dp_size); //allocate the dual parabolloid colors
  4051. Vector3 * dp_normals = (Vector3*)alloca( sizeof(Vector3)*dp_size); //allocate the dual parabolloid normals
  4052. const Vector3 * dp_src_normals = p_sampled_light->baked_light_sampler_info->sampler->dp_cache.ptr();
  4053. if (!p_sampled_light->baked_light_sampler_info->sampled_light.is_valid() || p_sampled_light->baked_light_sampler_info->resolution!=sampler_opts->resolution) {
  4054. if (p_sampled_light->baked_light_sampler_info->sampled_light.is_valid()) {
  4055. rasterizer->free(p_sampled_light->baked_light_sampler_info->sampled_light);
  4056. }
  4057. p_sampled_light->baked_light_sampler_info->resolution=sampler_opts->resolution;
  4058. p_sampled_light->baked_light_sampler_info->sampled_light=rasterizer->sampled_light_dp_create(sampler_opts->resolution,sampler_opts->resolution*2);
  4059. }
  4060. zeromem(dp_map,sizeof(Color)*dp_size);
  4061. bool valid=false;
  4062. int samples=0;
  4063. for(Set<Instance*>::Element *E=p_sampled_light->baked_light_sampler_info->baked_lights.front();E;E=E->next()) {
  4064. Instance *bl = E->get();
  4065. if (bl->baked_light_info->baked_light->sampler.size()==0)
  4066. continue; //not usable
  4067. Matrix3 norm_xform = bl->baked_light_info->affine_inverse.basis;//.inverse();
  4068. for(int i=0;i<dp_size;i++) {
  4069. dp_normals[i]=norm_xform.xform(dp_src_normals[i]).normalized();
  4070. }
  4071. //normals in place
  4072. //sample octree
  4073. float r = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  4074. float att = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_ATTENUATION];
  4075. float str = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_STRENGTH];
  4076. Vector3 s = p_sampled_light->data.transform.basis.get_scale();
  4077. r*=MAX(MAX(s.x,s.y),s.z);
  4078. AABB sample_aabb= bl->data.transform.affine_inverse().xform(AABB(Vector3(-r,-r,-r)+p_sampled_light->data.transform.origin,Vector3(r*2,r*2,r*2)));
  4079. //ok got octree local AABB
  4080. DVector<int>::Read rp = bl->baked_light_info->baked_light->sampler.read();
  4081. const int *rptr = rp.ptr();
  4082. int first = rptr[1];
  4083. int depth = rptr[2];
  4084. bool islinear = rptr[3]&1;
  4085. depth+=1;
  4086. AABB aabb;
  4087. aabb.pos.x=decode_float((const uint8_t*)&rptr[4]);
  4088. aabb.pos.y=decode_float((const uint8_t*)&rptr[5]);
  4089. aabb.pos.z=decode_float((const uint8_t*)&rptr[6]);
  4090. aabb.size.x=decode_float((const uint8_t*)&rptr[7]);
  4091. aabb.size.y=decode_float((const uint8_t*)&rptr[8]);
  4092. aabb.size.z=decode_float((const uint8_t*)&rptr[9]);
  4093. uint32_t *stack=(uint32_t*)alloca(depth*sizeof(uint32_t));
  4094. int *stack_ptr=(int*)alloca(depth*sizeof(int));
  4095. AABB *aabb_stack=(AABB*)alloca(depth*sizeof(AABB));
  4096. stack[0]=0;
  4097. stack_ptr[0]=first;
  4098. aabb_stack[0]=aabb;
  4099. Vector3 center = sample_aabb.pos + sample_aabb.size * 0.5;
  4100. int stack_pos=0;
  4101. Color max_col;
  4102. //int reso = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO];
  4103. int lalimit = sample_aabb.get_longest_axis_index();
  4104. float limit = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO]*sample_aabb.size[lalimit];
  4105. while(true) {
  4106. bool leaf = (rptr[ stack_ptr[stack_pos] ]>>16)==0;
  4107. if (aabb_stack[stack_pos].size[lalimit]<limit) {
  4108. leaf=true;
  4109. }
  4110. if (leaf) {
  4111. Vector3 from = aabb_stack[stack_pos].pos + aabb_stack[stack_pos].size * 0.5;
  4112. Vector3 norm = (from-center).normalized();
  4113. Color col;
  4114. col.r = ((rptr[ stack_ptr[stack_pos] ]&0xFFFF)/256.0);
  4115. col.g = ((rptr[ stack_ptr[stack_pos]+1 ]>>16)/256.0);
  4116. col.b = ((rptr[ stack_ptr[stack_pos]+1 ]&0xFFFF)/256.0);
  4117. max_col.r = MAX(max_col.r,col.r);
  4118. max_col.g = MAX(max_col.g,col.g);
  4119. max_col.b = MAX(max_col.b,col.b);
  4120. if (!islinear && p_linear_colorspace) {
  4121. col=col.to_linear();
  4122. }
  4123. float distance;
  4124. if (aabb_stack[stack_pos].has_point(center)) {
  4125. distance=0;
  4126. } else {
  4127. Vector3 support = aabb_stack[stack_pos].get_support(norm);
  4128. distance = Math::absf(norm.dot(support)-norm.dot(center));
  4129. }
  4130. if (distance>r)
  4131. distance=r;
  4132. float mult = Math::pow(1.0-distance/r,att)*str;
  4133. if (mult>0) {
  4134. col.r*=mult;
  4135. col.g*=mult;
  4136. col.b*=mult;
  4137. for(int i=0;i<dp_size;i++) {
  4138. float mult2 = norm.dot(dp_normals[i]);
  4139. if (mult2<0)
  4140. mult2=0;
  4141. Color col2(col.r*mult2,col.g*mult2,col.b*mult2,1.0);
  4142. dp_map[i].r=MAX(dp_map[i].r,col2.r);
  4143. dp_map[i].g=MAX(dp_map[i].g,col2.g);
  4144. dp_map[i].b=MAX(dp_map[i].b,col2.b);
  4145. }
  4146. }
  4147. samples++;
  4148. //nothing is valid unless you hit a leaf
  4149. valid=true;
  4150. stack_pos--;
  4151. } else if ((stack[stack_pos]&0xFF)<8) {
  4152. int i = stack[stack_pos]&0xFF;
  4153. int base = (stack[stack_pos]>>8);
  4154. if (!((rptr[ stack_ptr[stack_pos] ]>>16)&(1<<i))) {
  4155. //no bit, no test
  4156. stack[stack_pos]=(base<<8)+(i+1);
  4157. continue;
  4158. }
  4159. stack[stack_pos]=((base+1)<<8)+(i+1);
  4160. AABB child_aabb = aabb_stack[stack_pos];
  4161. child_aabb.size*=0.5;
  4162. if (i&1)
  4163. child_aabb.pos.x+=child_aabb.size.x;
  4164. if (i&2)
  4165. child_aabb.pos.y+=child_aabb.size.y;
  4166. if (i&4)
  4167. child_aabb.pos.z+=child_aabb.size.z;
  4168. if (!child_aabb.intersects(sample_aabb)) {
  4169. continue;
  4170. }
  4171. if (child_aabb.encloses(sample_aabb)) {
  4172. stack[stack_pos]=(base<<8)|8; //don't test the rest
  4173. }
  4174. stack_pos++;
  4175. ERR_FAIL_COND(stack_pos>=depth);
  4176. stack[stack_pos]=0;
  4177. stack_ptr[stack_pos]=rptr[ stack_ptr[stack_pos-1]+2+base ];
  4178. aabb_stack[stack_pos]=child_aabb;
  4179. } else {
  4180. stack_pos--;
  4181. if (stack_pos<0)
  4182. break;
  4183. }
  4184. }
  4185. }
  4186. //print_line("samples "+itos(samples) );
  4187. if (valid) {
  4188. for(int i=0;i<res;i++) {
  4189. //average seams to avoid aliasing
  4190. {
  4191. //top
  4192. int ofs1 = i;
  4193. int ofs2 = dp_size-res+i;
  4194. Color avg(
  4195. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4196. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4197. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4198. 1.0
  4199. );
  4200. dp_map[ofs1]=avg;
  4201. dp_map[ofs2]=avg;
  4202. }
  4203. {
  4204. //bottom
  4205. int ofs1 = res*res-res+i;
  4206. int ofs2 = res*res+i;
  4207. Color avg(
  4208. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4209. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4210. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4211. 1.0
  4212. );
  4213. dp_map[ofs1]=avg;
  4214. dp_map[ofs2]=avg;
  4215. }
  4216. {
  4217. //left
  4218. int ofs1 = i*res;
  4219. int ofs2 = res*res+(res-i-1)*res;
  4220. Color avg(
  4221. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4222. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4223. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4224. 1.0
  4225. );
  4226. dp_map[ofs1]=avg;
  4227. dp_map[ofs2]=avg;
  4228. }
  4229. {
  4230. //right
  4231. int ofs1 = i*res+(res-1);
  4232. int ofs2 = res*res+(res-i-1)*res+(res-1);
  4233. Color avg(
  4234. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4235. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4236. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4237. 1.0
  4238. );
  4239. dp_map[ofs1]=avg;
  4240. dp_map[ofs2]=avg;
  4241. }
  4242. }
  4243. rasterizer->sampled_light_dp_update(p_sampled_light->baked_light_sampler_info->sampled_light,dp_map,1.0);
  4244. for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) {
  4245. F->get()->data.sampled_light=p_sampled_light->baked_light_sampler_info->sampled_light;
  4246. }
  4247. } else {
  4248. for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) {
  4249. F->get()->data.sampled_light=RID(); //do not use because nothing close
  4250. }
  4251. }
  4252. /*
  4253. highp vec3 vtx = vertex_interp;
  4254. vtx.z*=dual_paraboloid.y; //side to affect
  4255. vtx.z+=0.01;
  4256. dp_clip=vtx.z;
  4257. highp float len=length( vtx );
  4258. vtx=normalize(vtx);
  4259. vtx.xy/=1.0+vtx.z;
  4260. vtx.z = len*dual_paraboloid.x; // it's a reciprocal(len - z_near) / (z_far - z_near);
  4261. vtx+=normalize(vtx)*0.025;
  4262. vtx.z = vtx.z * 2.0 - 1.0; // fit to clipspace
  4263. vertex_interp=vtx;
  4264. */
  4265. }
  4266. void VisualServerRaster::_render_no_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  4267. RID environment;
  4268. if (p_scenario->environment.is_valid())
  4269. environment=p_scenario->environment;
  4270. else
  4271. environment=p_scenario->fallback_environment;
  4272. rasterizer->set_camera(Transform(),CameraMatrix(),false);
  4273. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  4274. rasterizer->set_viewport(viewport_rect);
  4275. rasterizer->end_scene();
  4276. }
  4277. void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  4278. uint64_t t = OS::get_singleton()->get_ticks_usec();
  4279. render_pass++;
  4280. uint32_t camera_layer_mask=p_camera->visible_layers;
  4281. /* STEP 1 - SETUP CAMERA */
  4282. CameraMatrix camera_matrix;
  4283. bool ortho=false;
  4284. switch(p_camera->type) {
  4285. case Camera::ORTHOGONAL: {
  4286. camera_matrix.set_orthogonal(
  4287. p_camera->size,
  4288. viewport_rect.width / (float)viewport_rect.height,
  4289. p_camera->znear,
  4290. p_camera->zfar,
  4291. p_camera->vaspect
  4292. );
  4293. ortho=true;
  4294. } break;
  4295. case Camera::PERSPECTIVE: {
  4296. camera_matrix.set_perspective(
  4297. p_camera->fov,
  4298. viewport_rect.width / (float)viewport_rect.height,
  4299. p_camera->znear,
  4300. p_camera->zfar,
  4301. p_camera->vaspect
  4302. );
  4303. ortho=false;
  4304. } break;
  4305. }
  4306. rasterizer->set_camera(p_camera->transform, camera_matrix,ortho);
  4307. Vector<Plane> planes = camera_matrix.get_projection_planes(p_camera->transform);
  4308. CullRange cull_range; // cull range is used for PSSM, and having an idea of the rendering depth
  4309. cull_range.nearp=Plane(p_camera->transform.origin,-p_camera->transform.basis.get_axis(2).normalized());
  4310. cull_range.z_near=camera_matrix.get_z_near();
  4311. cull_range.z_far=camera_matrix.get_z_far();
  4312. cull_range.min=cull_range.z_far;
  4313. cull_range.max=cull_range.z_near;
  4314. /* STEP 2 - CULL */
  4315. int cull_count = p_scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL);
  4316. light_cull_count=0;
  4317. light_samplers_culled=0;
  4318. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  4319. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  4320. // print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  4321. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  4322. */
  4323. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  4324. // compute portals
  4325. exterior_visited=false;
  4326. exterior_portal_cull_count=0;
  4327. if (room_cull_enabled) {
  4328. for(int i=0;i<cull_count;i++) {
  4329. Instance *ins = instance_cull_result[i];
  4330. ins->last_render_pass=render_pass;
  4331. if (ins->base_type!=INSTANCE_PORTAL)
  4332. continue;
  4333. if (ins->room)
  4334. continue;
  4335. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  4336. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  4337. }
  4338. 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));
  4339. Set<Instance*> current_rooms;
  4340. Set<Instance*> portal_rooms;
  4341. //add to set
  4342. for(int i=0;i<room_cull_count;i++) {
  4343. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  4344. current_rooms.insert(room_cull_result[i]);
  4345. }
  4346. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  4347. //assume inside that room if also inside the portal..
  4348. if (room_cull_result[i]->room) {
  4349. portal_rooms.insert(room_cull_result[i]->room);
  4350. }
  4351. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  4352. room_cull_count--;
  4353. i--;
  4354. }
  4355. }
  4356. //remove from set if it has a parent room or BSP doesn't contain
  4357. for(int i=0;i<room_cull_count;i++) {
  4358. Instance *r = room_cull_result[i];
  4359. //check inside BSP
  4360. Vector3 room_local_point = r->room_info->affine_inverse.xform( p_camera->transform.origin );
  4361. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  4362. current_rooms.erase(r);
  4363. continue;
  4364. }
  4365. //check parent
  4366. while (r->room) {// has parent room
  4367. current_rooms.erase(r);
  4368. r=r->room;
  4369. }
  4370. }
  4371. if (current_rooms.size()) {
  4372. //camera is inside a room
  4373. // go through rooms
  4374. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  4375. _cull_room(p_camera,E->get());
  4376. }
  4377. } else {
  4378. //start from exterior
  4379. _cull_room(p_camera,NULL);
  4380. }
  4381. }
  4382. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  4383. for(int i=0;i<cull_count;i++) {
  4384. Instance *ins = instance_cull_result[i];
  4385. bool keep=false;
  4386. if ((camera_layer_mask&ins->layer_mask)==0) {
  4387. //failure
  4388. } else if (ins->base_type==INSTANCE_LIGHT) {
  4389. if (light_cull_count<MAX_LIGHTS_CULLED) {
  4390. light_cull_result[light_cull_count++]=ins;
  4391. // rasterizer->light_instance_set_active_hint(ins->light_info->instance);
  4392. {
  4393. //compute distance to camera using aabb support
  4394. Vector3 n = ins->data.transform.basis.xform_inv(cull_range.nearp.normal).normalized();
  4395. Vector3 s = ins->data.transform.xform(ins->aabb.get_support(n));
  4396. ins->light_info->dtc=cull_range.nearp.distance_to(s);
  4397. }
  4398. }
  4399. } else if ((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK && ins->visible && ins->data.cast_shadows!=VS::SHADOW_CASTING_SETTING_SHADOWS_ONLY) {
  4400. bool discarded=false;
  4401. if (ins->draw_range_end>0) {
  4402. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  4403. if (d<0)
  4404. d=0;
  4405. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  4406. }
  4407. if (!discarded) {
  4408. // test if this geometry should be visible
  4409. if (room_cull_enabled) {
  4410. if (ins->visible_in_all_rooms) {
  4411. keep=true;
  4412. } else if (ins->room) {
  4413. if (ins->room->room_info->last_visited_pass==render_pass)
  4414. keep=true;
  4415. } else if (ins->auto_rooms.size()) {
  4416. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  4417. if (E->get()->room_info->last_visited_pass==render_pass) {
  4418. keep=true;
  4419. break;
  4420. }
  4421. }
  4422. } else if(exterior_visited)
  4423. keep=true;
  4424. } else {
  4425. keep=true;
  4426. }
  4427. }
  4428. if (keep) {
  4429. // update cull range
  4430. float min,max;
  4431. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  4432. if (min<cull_range.min)
  4433. cull_range.min=min;
  4434. if (max>cull_range.max)
  4435. cull_range.max=max;
  4436. if (ins->sampled_light && ins->sampled_light->baked_light_sampler_info->last_pass!=render_pass) {
  4437. if (light_samplers_culled<MAX_LIGHT_SAMPLERS) {
  4438. light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light;
  4439. ins->sampled_light->baked_light_sampler_info->last_pass=render_pass;
  4440. }
  4441. }
  4442. }
  4443. }
  4444. if (!keep) {
  4445. // remove, no reason to keep
  4446. cull_count--;
  4447. SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] );
  4448. i--;
  4449. ins->last_render_pass=0; // make invalid
  4450. } else {
  4451. ins->last_render_pass=render_pass;
  4452. }
  4453. }
  4454. if (cull_range.max > cull_range.z_far )
  4455. cull_range.max=cull_range.z_far;
  4456. if (cull_range.min < cull_range.z_near )
  4457. cull_range.min=cull_range.z_near;
  4458. /* STEP 5 - PROCESS LIGHTS */
  4459. rasterizer->shadow_clear_near(); //clear near shadows, will be recreated
  4460. // directional lights
  4461. {
  4462. List<RID>::Element *E=p_scenario->directional_lights.front();
  4463. while(E) {
  4464. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  4465. if (light && light->light_info->enabled && rasterizer->light_has_shadow(light->base_rid)) {
  4466. //rasterizer->light_instance_set_active_hint(light->light_info->instance);
  4467. _light_instance_update_shadow(light,p_scenario,p_camera,cull_range);
  4468. }
  4469. E=E->next();
  4470. }
  4471. }
  4472. //discard lights not affecting anything (useful for deferred rendering, shadowmaps, etc)
  4473. for (int i=0;i<light_cull_count;i++) {
  4474. Instance *ins = light_cull_result[i];
  4475. if (light_discard_enabled) {
  4476. //see if the light should be pre discarded because no one is seeing it
  4477. //this test may seem expensive, but in reality, it shouldn't be
  4478. //because of early out condition. It will only go through everything
  4479. //if it's being discarded.
  4480. bool valid=false;
  4481. InstanceSet::Element *E =ins->light_info->affected.front();
  4482. while(E) {
  4483. if (E->get()->last_render_pass==render_pass) {
  4484. valid=true; // early out.
  4485. break;
  4486. }
  4487. E=E->next();
  4488. }
  4489. if (!valid) {
  4490. light_cull_count--;
  4491. SWAP( light_cull_result[i], light_cull_result[ light_cull_count ] );
  4492. i--;
  4493. }
  4494. }
  4495. }
  4496. { //this should eventually change to
  4497. //assign shadows by distance to camera
  4498. SortArray<Instance*,_InstanceLightsort> sorter;
  4499. sorter.sort(light_cull_result,light_cull_count);
  4500. for (int i=0;i<light_cull_count;i++) {
  4501. Instance *ins = light_cull_result[i];
  4502. if (!rasterizer->light_has_shadow(ins->base_rid) || !shadows_enabled)
  4503. continue;
  4504. /* for far shadows?
  4505. if (ins->version == ins->light_info->last_version && rasterizer->light_instance_has_far_shadow(ins->light_info->instance))
  4506. continue; // didn't change
  4507. */
  4508. _light_instance_update_shadow(ins,p_scenario,p_camera,cull_range);
  4509. ins->light_info->last_version=ins->version;
  4510. }
  4511. }
  4512. /* ENVIRONMENT */
  4513. RID environment;
  4514. if (p_camera->env.is_valid()) //camera has more environment priority
  4515. environment=p_camera->env;
  4516. else if (p_scenario->environment.is_valid())
  4517. environment=p_scenario->environment;
  4518. else
  4519. environment=p_scenario->fallback_environment;
  4520. /* STEP 6 - SAMPLE BAKED LIGHT */
  4521. bool islinear =false;
  4522. if (environment.is_valid()) {
  4523. islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB);
  4524. }
  4525. for(int i=0;i<light_samplers_culled;i++) {
  4526. _process_sampled_light(p_camera->transform,light_sampler_cull_result[i],islinear);
  4527. }
  4528. /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/
  4529. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  4530. rasterizer->set_viewport(viewport_rect);
  4531. // add lights
  4532. {
  4533. List<RID>::Element *E=p_scenario->directional_lights.front();
  4534. for(;E;E=E->next()) {
  4535. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  4536. ERR_CONTINUE(!light);
  4537. if (!light->light_info->enabled)
  4538. continue;
  4539. rasterizer->add_light(light->light_info->instance);
  4540. light->light_info->last_add_pass=render_pass;
  4541. }
  4542. for (int i=0;i<light_cull_count;i++) {
  4543. Instance *ins = light_cull_result[i];
  4544. rasterizer->add_light(ins->light_info->instance);
  4545. ins->light_info->last_add_pass=render_pass;
  4546. }
  4547. }
  4548. // add geometry
  4549. for(int i=0;i<cull_count;i++) {
  4550. Instance *ins = instance_cull_result[i];
  4551. ERR_CONTINUE(!((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK));
  4552. _instance_draw(ins);
  4553. }
  4554. rasterizer->end_scene();
  4555. }
  4556. void VisualServerRaster::_render_canvas_item_tree(CanvasItem *p_canvas_item, const Matrix32& p_transform, const Rect2& p_clip_rect, const Color& p_modulate, Rasterizer::CanvasLight *p_lights) {
  4557. static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1;
  4558. Rasterizer::CanvasItem *z_list[z_range];
  4559. Rasterizer::CanvasItem *z_last_list[z_range];
  4560. for(int i=0;i<z_range;i++) {
  4561. z_list[i]=NULL;
  4562. z_last_list[i]=NULL;
  4563. }
  4564. _render_canvas_item(p_canvas_item,p_transform,p_clip_rect,1.0,0,z_list,z_last_list,NULL,NULL);
  4565. for(int i=0;i<z_range;i++) {
  4566. if (!z_list[i])
  4567. continue;
  4568. rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_modulate,p_lights);
  4569. }
  4570. }
  4571. void VisualServerRaster::_render_canvas_item_viewport(VisualServer* p_self,void *p_vp,const Rect2& p_rect) {
  4572. VisualServerRaster *self=(VisualServerRaster*)(p_self);
  4573. Viewport *vp=(Viewport*)p_vp;
  4574. self->_draw_viewport(vp,p_rect.pos.x,p_rect.pos.y,p_rect.size.x,p_rect.size.y);
  4575. self->rasterizer->canvas_begin();
  4576. }
  4577. void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Matrix32& p_transform,const Rect2& p_clip_rect, float p_opacity,int p_z,Rasterizer::CanvasItem **z_list,Rasterizer::CanvasItem **z_last_list,CanvasItem *p_canvas_clip,CanvasItem *p_material_owner) {
  4578. CanvasItem *ci = p_canvas_item;
  4579. if (!ci->visible)
  4580. return;
  4581. if (p_opacity<0.007)
  4582. return;
  4583. Rect2 rect = ci->get_rect();
  4584. Matrix32 xform = p_transform * ci->xform;
  4585. Rect2 global_rect = xform.xform(rect);
  4586. global_rect.pos+=p_clip_rect.pos;
  4587. if (global_rect.intersects(p_clip_rect) && ci->viewport.is_valid() && viewport_owner.owns(ci->viewport)) {
  4588. Viewport *vp = viewport_owner.get(ci->viewport);
  4589. Point2i from = xform.get_origin() + Point2(viewport_rect.x,viewport_rect.y);
  4590. Point2i size = rect.size;
  4591. size.x *= xform[0].length();
  4592. size.y *= xform[1].length();
  4593. ci->vp_render = memnew( Rasterizer::CanvasItem::ViewportRender );
  4594. ci->vp_render->owner=this;
  4595. ci->vp_render->udata=vp;
  4596. ci->vp_render->rect=Rect2(from.x,
  4597. from.y,
  4598. size.x,
  4599. size.y);
  4600. /*
  4601. _draw_viewport(vp,
  4602. from.x,
  4603. from.y,
  4604. size.x,
  4605. size.y);
  4606. */
  4607. //rasterizer->canvas_begin();
  4608. } else {
  4609. ci->vp_render=NULL;
  4610. }
  4611. if (ci->use_parent_material && p_material_owner)
  4612. ci->material_owner=p_material_owner;
  4613. else {
  4614. p_material_owner=ci;
  4615. ci->material_owner=NULL;
  4616. }
  4617. float opacity = ci->opacity * p_opacity;
  4618. int child_item_count=ci->child_items.size();
  4619. CanvasItem **child_items=(CanvasItem**)alloca(child_item_count*sizeof(CanvasItem*));
  4620. copymem(child_items,ci->child_items.ptr(),child_item_count*sizeof(CanvasItem*));
  4621. if (ci->clip) {
  4622. if (p_canvas_clip != NULL) {
  4623. ci->final_clip_rect=p_canvas_clip->final_clip_rect.clip(global_rect);
  4624. } else {
  4625. ci->final_clip_rect=global_rect;
  4626. }
  4627. ci->final_clip_owner=ci;
  4628. } else {
  4629. ci->final_clip_owner=p_canvas_clip;
  4630. }
  4631. if (ci->sort_y) {
  4632. SortArray<CanvasItem*,CanvasItemPtrSort> sorter;
  4633. sorter.sort(child_items,child_item_count);
  4634. }
  4635. if (ci->z_relative)
  4636. p_z=CLAMP(p_z+ci->z,CANVAS_ITEM_Z_MIN,CANVAS_ITEM_Z_MAX);
  4637. else
  4638. p_z=ci->z;
  4639. for(int i=0;i<child_item_count;i++) {
  4640. if (child_items[i]->ontop)
  4641. continue;
  4642. _render_canvas_item(child_items[i],xform,p_clip_rect,opacity,p_z,z_list,z_last_list,(CanvasItem*)ci->final_clip_owner,p_material_owner);
  4643. }
  4644. if (ci->copy_back_buffer) {
  4645. ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  4646. }
  4647. if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect)) || ci->vp_render || ci->copy_back_buffer) {
  4648. //something to draw?
  4649. ci->final_transform=xform;
  4650. ci->final_opacity=opacity * ci->self_opacity;
  4651. ci->global_rect_cache=global_rect;
  4652. ci->global_rect_cache.pos-=p_clip_rect.pos;
  4653. ci->light_masked=false;
  4654. int zidx = p_z-CANVAS_ITEM_Z_MIN;
  4655. if (z_last_list[zidx]) {
  4656. z_last_list[zidx]->next=ci;
  4657. z_last_list[zidx]=ci;
  4658. } else {
  4659. z_list[zidx]=ci;
  4660. z_last_list[zidx]=ci;
  4661. }
  4662. ci->next=NULL;
  4663. }
  4664. for(int i=0;i<child_item_count;i++) {
  4665. if (!child_items[i]->ontop)
  4666. continue;
  4667. _render_canvas_item(child_items[i],xform,p_clip_rect,opacity,p_z,z_list,z_last_list,(CanvasItem*)ci->final_clip_owner,p_material_owner);
  4668. }
  4669. }
  4670. void VisualServerRaster::_light_mask_canvas_items(int p_z,Rasterizer::CanvasItem *p_canvas_item,Rasterizer::CanvasLight *p_masked_lights) {
  4671. if (!p_masked_lights)
  4672. return;
  4673. Rasterizer::CanvasItem *ci=p_canvas_item;
  4674. while(ci) {
  4675. Rasterizer::CanvasLight *light=p_masked_lights;
  4676. while(light) {
  4677. if (ci->light_mask&light->item_mask && p_z>=light->z_min && p_z<=light->z_max && ci->global_rect_cache.intersects_transformed(light->xform_cache,light->rect_cache)) {
  4678. ci->light_masked=true;
  4679. }
  4680. light=light->mask_next_ptr;
  4681. }
  4682. ci=ci->next;
  4683. }
  4684. }
  4685. void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform,Rasterizer::CanvasLight *p_lights,Rasterizer::CanvasLight *p_masked_lights) {
  4686. rasterizer->canvas_begin();
  4687. int l = p_canvas->child_items.size();
  4688. Canvas::ChildItem *ci=p_canvas->child_items.ptr();
  4689. bool has_mirror=false;
  4690. for(int i=0;i<l;i++) {
  4691. if (ci[i].mirror.x || ci[i].mirror.y) {
  4692. has_mirror=true;
  4693. break;
  4694. }
  4695. }
  4696. Rect2 clip_rect(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height);
  4697. if (!has_mirror) {
  4698. static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1;
  4699. Rasterizer::CanvasItem *z_list[z_range];
  4700. Rasterizer::CanvasItem *z_last_list[z_range];
  4701. for(int i=0;i<z_range;i++) {
  4702. z_list[i]=NULL;
  4703. z_last_list[i]=NULL;
  4704. }
  4705. for(int i=0;i<l;i++) {
  4706. _render_canvas_item(ci[i].item,p_transform,clip_rect,1.0,0,z_list,z_last_list,NULL,NULL);
  4707. }
  4708. for(int i=0;i<z_range;i++) {
  4709. if (!z_list[i])
  4710. continue;
  4711. if (p_masked_lights) {
  4712. _light_mask_canvas_items(CANVAS_ITEM_Z_MIN+i,z_list[i],p_masked_lights);
  4713. }
  4714. rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_canvas->modulate,p_lights);
  4715. }
  4716. } else {
  4717. for(int i=0;i<l;i++) {
  4718. Canvas::ChildItem& ci=p_canvas->child_items[i];
  4719. _render_canvas_item_tree(ci.item,p_transform,clip_rect,p_canvas->modulate,p_lights);
  4720. //mirroring (useful for scrolling backgrounds)
  4721. if (ci.mirror.x!=0) {
  4722. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0));
  4723. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4724. }
  4725. if (ci.mirror.y!=0) {
  4726. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y));
  4727. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4728. }
  4729. if (ci.mirror.y!=0 && ci.mirror.x!=0) {
  4730. Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror);
  4731. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4732. }
  4733. }
  4734. }
  4735. }
  4736. void VisualServerRaster::_draw_viewport_camera(Viewport *p_viewport,bool p_ignore_camera) {
  4737. Camera *camera=NULL;
  4738. if (camera_owner.owns( p_viewport->camera ))
  4739. camera=camera_owner.get( p_viewport->camera );
  4740. Scenario *scenario = scenario_owner.get( p_viewport->scenario );
  4741. _update_instances(); // check dirty instances before rendering
  4742. if (p_ignore_camera)
  4743. _render_no_camera(p_viewport, camera,scenario );
  4744. else
  4745. _render_camera(p_viewport, camera,scenario );
  4746. }
  4747. void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ofs_y,int p_parent_w,int p_parent_h) {
  4748. ViewportRect desired_rect=p_viewport->rect;
  4749. ViewportRect old_rect = viewport_rect;
  4750. // bool vpchanged=false;
  4751. // convert default expanding viewports to actual size
  4752. //if (desired_rect.x==0 && desired_rect.y==0 && desired_rect.width==0 && desired_rect.height==0) {
  4753. if (p_parent_w != 0 && p_parent_h != 0) {
  4754. desired_rect.width=p_parent_w;
  4755. desired_rect.height=p_parent_h;
  4756. }
  4757. ERR_FAIL_COND(desired_rect.width<=0 || desired_rect.height<=0);
  4758. desired_rect.x+=p_ofs_x;
  4759. desired_rect.y+=p_ofs_y;
  4760. // if the viewport is different than the actual one, change it
  4761. if ( p_viewport->render_target.is_valid() || viewport_rect.x != desired_rect.x ||
  4762. viewport_rect.y != desired_rect.y ||
  4763. viewport_rect.width != desired_rect.width ||
  4764. viewport_rect.height != desired_rect.height ) {
  4765. viewport_rect=desired_rect;
  4766. rasterizer->set_viewport(viewport_rect);
  4767. }
  4768. /* Camera should always be BEFORE any other 3D */
  4769. bool scenario_draw_canvas_bg=false;
  4770. int scenario_canvas_max_layer=0;
  4771. if (!p_viewport->hide_canvas && !p_viewport->disable_environment && scenario_owner.owns(p_viewport->scenario)) {
  4772. Scenario *scenario=scenario_owner.get(p_viewport->scenario);
  4773. if (scenario->environment.is_valid()) {
  4774. if (rasterizer->is_environment(scenario->environment)) {
  4775. scenario_draw_canvas_bg=rasterizer->environment_get_background(scenario->environment)==VS::ENV_BG_CANVAS;
  4776. scenario_canvas_max_layer=rasterizer->environment_get_background_param(scenario->environment,VS::ENV_BG_PARAM_CANVAS_MAX_LAYER);
  4777. }
  4778. }
  4779. }
  4780. bool can_draw_3d=!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario);
  4781. if (scenario_draw_canvas_bg) {
  4782. rasterizer->begin_canvas_bg();
  4783. }
  4784. if (!scenario_draw_canvas_bg && can_draw_3d) {
  4785. _draw_viewport_camera(p_viewport,false);
  4786. } else if (true /*|| !p_viewport->canvas_list.empty()*/){
  4787. //clear the viewport black because of no camera? i seriously should..
  4788. if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) {
  4789. if (p_viewport->transparent_bg) {
  4790. rasterizer->clear_viewport(Color(0,0,0,0));
  4791. }
  4792. else {
  4793. Color cc=clear_color;
  4794. if (scenario_draw_canvas_bg)
  4795. cc.a=0;
  4796. rasterizer->clear_viewport(cc);
  4797. }
  4798. p_viewport->render_target_clear=false;
  4799. }
  4800. }
  4801. if (!p_viewport->hide_canvas) {
  4802. int i=0;
  4803. Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
  4804. Rect2 clip_rect(0,0,viewport_rect.width,viewport_rect.height);
  4805. Rasterizer::CanvasLight *lights=NULL;
  4806. Rasterizer::CanvasLight *lights_with_shadow=NULL;
  4807. Rasterizer::CanvasLight *lights_with_mask=NULL;
  4808. Rect2 shadow_rect;
  4809. int light_count=0;
  4810. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  4811. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  4812. //find lights in canvas
  4813. for(Set<Rasterizer::CanvasLight*>::Element *F=E->get().canvas->lights.front();F;F=F->next()) {
  4814. Rasterizer::CanvasLight* cl=F->get();
  4815. if (cl->enabled && cl->texture.is_valid()) {
  4816. //not super efficient..
  4817. Size2 tsize(rasterizer->texture_get_width(cl->texture),rasterizer->texture_get_height(cl->texture));
  4818. tsize*=cl->scale;
  4819. Vector2 offset=tsize/2.0;
  4820. cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize);
  4821. cl->xform_cache=xf * cl->xform;
  4822. if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) {
  4823. cl->filter_next_ptr=lights;
  4824. lights=cl;
  4825. cl->texture_cache=NULL;
  4826. Matrix32 scale;
  4827. scale.scale(cl->rect_cache.size);
  4828. scale.elements[2]=cl->rect_cache.pos;
  4829. cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
  4830. cl->light_shader_pos=cl->xform_cache[2];
  4831. if (cl->shadow_buffer.is_valid()) {
  4832. cl->shadows_next_ptr=lights_with_shadow;
  4833. if (lights_with_shadow==NULL) {
  4834. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  4835. } else {
  4836. shadow_rect=shadow_rect.merge( cl->xform_cache.xform(cl->rect_cache) );
  4837. }
  4838. lights_with_shadow=cl;
  4839. cl->radius_cache=cl->rect_cache.size.length();
  4840. }
  4841. if (cl->mode==CANVAS_LIGHT_MODE_MASK) {
  4842. cl->mask_next_ptr=lights_with_mask;
  4843. lights_with_mask=cl;
  4844. }
  4845. light_count++;
  4846. }
  4847. }
  4848. }
  4849. //print_line("lights: "+itos(light_count));
  4850. canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
  4851. }
  4852. if (lights_with_shadow) {
  4853. //update shadows if any
  4854. Rasterizer::CanvasLightOccluderInstance * occluders=NULL;
  4855. //make list of occluders
  4856. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  4857. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  4858. for(Set<Rasterizer::CanvasLightOccluderInstance*>::Element *F=E->get().canvas->occluders.front();F;F=F->next()) {
  4859. if (!F->get()->enabled)
  4860. continue;
  4861. F->get()->xform_cache = xf * F->get()->xform;
  4862. if (shadow_rect.intersects_transformed(F->get()->xform_cache,F->get()->aabb_cache)) {
  4863. F->get()->next=occluders;
  4864. occluders=F->get();
  4865. }
  4866. }
  4867. }
  4868. //update the light shadowmaps with them
  4869. Rasterizer::CanvasLight *light=lights_with_shadow;
  4870. while(light) {
  4871. rasterizer->canvas_light_shadow_buffer_update(light->shadow_buffer,light->xform_cache.affine_inverse(),light->item_mask,light->radius_cache/1000.0,light->radius_cache*1.1,occluders,&light->shadow_matrix_cache);
  4872. light=light->shadows_next_ptr;
  4873. }
  4874. rasterizer->set_viewport(viewport_rect); //must reset viewport afterwards
  4875. }
  4876. if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().layer>scenario_canvas_max_layer) {
  4877. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4878. scenario_draw_canvas_bg=false;
  4879. }
  4880. for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
  4881. // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
  4882. //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
  4883. Matrix32 xform = p_viewport->global_transform * E->get()->transform;
  4884. Rasterizer::CanvasLight *canvas_lights=NULL;
  4885. Rasterizer::CanvasLight *ptr=lights;
  4886. while(ptr) {
  4887. if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) {
  4888. ptr->next_ptr=canvas_lights;
  4889. canvas_lights=ptr;
  4890. }
  4891. ptr=ptr->filter_next_ptr;
  4892. }
  4893. _render_canvas( E->get()->canvas,xform,canvas_lights,lights_with_mask );
  4894. i++;
  4895. if (scenario_draw_canvas_bg && E->key().layer>=scenario_canvas_max_layer) {
  4896. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4897. scenario_draw_canvas_bg=false;
  4898. }
  4899. }
  4900. if (scenario_draw_canvas_bg) {
  4901. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4902. scenario_draw_canvas_bg=false;
  4903. }
  4904. // rasterizer->canvas_debug_viewport_shadows(lights_with_shadow);
  4905. }
  4906. //capture
  4907. if (p_viewport->queue_capture) {
  4908. rasterizer->capture_viewport(&p_viewport->capture);
  4909. p_viewport->queue_capture = false;
  4910. }
  4911. //restore
  4912. if ( viewport_rect.x != old_rect.x ||
  4913. viewport_rect.y != old_rect.y ||
  4914. viewport_rect.width != old_rect.width ||
  4915. viewport_rect.height != old_rect.height ) {
  4916. viewport_rect=old_rect;
  4917. rasterizer->set_viewport(viewport_rect);
  4918. }
  4919. }
  4920. void VisualServerRaster::_draw_viewports() {
  4921. //draw viewports for render targets
  4922. List<Viewport*> to_blit;
  4923. List<Viewport*> to_disable;
  4924. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  4925. Viewport *vp = E->self();
  4926. ERR_CONTINUE(!vp);
  4927. if (
  4928. vp->render_target_update_mode==RENDER_TARGET_UPDATE_WHEN_VISIBLE &&
  4929. !vp->rendered_in_prev_frame &&
  4930. !vp->queue_capture
  4931. ) {
  4932. continue;
  4933. }
  4934. if (vp->rt_to_screen_rect!=Rect2())
  4935. to_blit.push_back(vp);
  4936. rasterizer->set_render_target(vp->render_target,vp->transparent_bg,vp->render_target_vflip);
  4937. _draw_viewport(vp,0,0,vp->rect.width,vp->rect.height);
  4938. if ( (vp->queue_capture && vp->render_target_update_mode==RENDER_TARGET_UPDATE_DISABLED) || vp->render_target_update_mode==RENDER_TARGET_UPDATE_ONCE) {
  4939. //was only enabled for capture
  4940. to_disable.push_back(vp);
  4941. vp->render_target_update_mode=RENDER_TARGET_UPDATE_DISABLED;
  4942. }
  4943. }
  4944. rasterizer->set_render_target(RID());
  4945. while(to_disable.size()) {
  4946. //disable again because it was only for capture
  4947. viewport_update_list.remove(&to_disable.front()->get()->update_list);
  4948. to_disable.pop_front();
  4949. }
  4950. //draw RTs directly to screen when requested
  4951. for (List<Viewport*>::Element *E=to_blit.front();E;E=E->next()) {
  4952. int window_w = OS::get_singleton()->get_video_mode().width;
  4953. int window_h = OS::get_singleton()->get_video_mode().height;
  4954. ViewportRect desired_rect;
  4955. desired_rect.x = desired_rect.y = 0;
  4956. desired_rect.width = window_w;
  4957. desired_rect.height = window_h;
  4958. if ( viewport_rect.x != desired_rect.x ||
  4959. viewport_rect.y != desired_rect.y ||
  4960. viewport_rect.width != desired_rect.width ||
  4961. viewport_rect.height != desired_rect.height ) {
  4962. viewport_rect=desired_rect;
  4963. rasterizer->set_viewport(viewport_rect);
  4964. }
  4965. rasterizer->canvas_begin();
  4966. rasterizer->canvas_disable_blending();
  4967. rasterizer->canvas_begin_rect(Matrix32());
  4968. 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));
  4969. }
  4970. //draw viewports attached to screen
  4971. for(Map<RID,int>::Element *E=screen_viewports.front();E;E=E->next()) {
  4972. Viewport *vp = viewport_owner.get(E->key());
  4973. ERR_CONTINUE(!vp);
  4974. int window_w = OS::get_singleton()->get_video_mode(E->get()).width;
  4975. int window_h = OS::get_singleton()->get_video_mode(E->get()).height;
  4976. Rect2 r(0,0,vp->rect.width,vp->rect.height);
  4977. if (r.size.width==0)
  4978. r.size.width=window_w;
  4979. if (r.size.height==0)
  4980. r.size.height=window_h;
  4981. _draw_viewport(vp,r.pos.x,r.pos.y,r.size.width,r.size.height);
  4982. }
  4983. //check when a viewport associated to a render target was drawn
  4984. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  4985. Viewport *vp = E->self();
  4986. ERR_CONTINUE(!vp);
  4987. if (vp->render_target_update_mode!=RENDER_TARGET_UPDATE_WHEN_VISIBLE)
  4988. continue;
  4989. vp->rendered_in_prev_frame=rasterizer->render_target_renedered_in_frame(vp->render_target);
  4990. }
  4991. }
  4992. void VisualServerRaster::_draw_cursors_and_margins() {
  4993. int window_w = OS::get_singleton()->get_video_mode().width;
  4994. int window_h = OS::get_singleton()->get_video_mode().height;
  4995. ViewportRect desired_rect;
  4996. desired_rect.x = desired_rect.y = 0;
  4997. desired_rect.width = window_w;
  4998. desired_rect.height = window_h;
  4999. if ( viewport_rect.x != desired_rect.x ||
  5000. viewport_rect.y != desired_rect.y ||
  5001. viewport_rect.width != desired_rect.width ||
  5002. viewport_rect.height != desired_rect.height ) {
  5003. viewport_rect=desired_rect;
  5004. rasterizer->set_viewport(viewport_rect);
  5005. }
  5006. rasterizer->canvas_begin();
  5007. rasterizer->canvas_begin_rect(Matrix32());
  5008. for (int i=0; i<MAX_CURSORS; i++) {
  5009. if (!cursors[i].visible) {
  5010. continue;
  5011. };
  5012. RID tex = cursors[i].texture?cursors[i].texture:default_cursor_texture;
  5013. ERR_CONTINUE( !tex );
  5014. Point2 size(texture_get_width(tex), texture_get_height(tex));
  5015. rasterizer->canvas_draw_rect(Rect2(cursors[i].pos, size), 0, Rect2(), tex, Color(1, 1, 1, 1));
  5016. };
  5017. if (black_image[MARGIN_LEFT].is_valid()) {
  5018. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_LEFT]),rasterizer->texture_get_height(black_image[MARGIN_LEFT]));
  5019. rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_LEFT],Color(1,1,1));
  5020. } else if (black_margin[MARGIN_LEFT])
  5021. rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  5022. if (black_image[MARGIN_RIGHT].is_valid()) {
  5023. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_RIGHT]),rasterizer->texture_get_height(black_image[MARGIN_RIGHT]));
  5024. rasterizer->canvas_draw_rect(Rect2(window_w-black_margin[MARGIN_RIGHT],0,black_margin[MARGIN_RIGHT],window_h),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_RIGHT],Color(1,1,1));
  5025. } else if (black_margin[MARGIN_RIGHT])
  5026. 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));
  5027. if (black_image[MARGIN_TOP].is_valid()) {
  5028. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_TOP]),rasterizer->texture_get_height(black_image[MARGIN_TOP]));
  5029. rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_TOP],Color(1,1,1));
  5030. } else if (black_margin[MARGIN_TOP]) {
  5031. rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  5032. }
  5033. if (black_image[MARGIN_BOTTOM].is_valid()) {
  5034. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_BOTTOM]),rasterizer->texture_get_height(black_image[MARGIN_BOTTOM]));
  5035. rasterizer->canvas_draw_rect(Rect2(0,window_h-black_margin[MARGIN_BOTTOM],window_w,black_margin[MARGIN_BOTTOM]),0,Rect2(0,0,sz.x,sz.y),black_image[MARGIN_BOTTOM],Color(1,1,1));
  5036. } else if (black_margin[MARGIN_BOTTOM]) {
  5037. 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));
  5038. }
  5039. rasterizer->canvas_end_rect();
  5040. };
  5041. void VisualServerRaster::sync() {
  5042. //do none
  5043. }
  5044. void VisualServerRaster::draw() {
  5045. //if (changes)
  5046. // print_line("changes: "+itos(changes));
  5047. changes=0;
  5048. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  5049. room_cull_enabled = GLOBAL_DEF("render/room_cull_enabled",true);
  5050. light_discard_enabled = GLOBAL_DEF("render/light_discard_enabled",true);
  5051. rasterizer->begin_frame();
  5052. _draw_viewports();
  5053. _draw_cursors_and_margins();
  5054. rasterizer->end_frame();
  5055. draw_extra_frame=rasterizer->needs_to_draw_next_frame();
  5056. }
  5057. bool VisualServerRaster::has_changed() const {
  5058. return changes>0 || draw_extra_frame;
  5059. }
  5060. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  5061. return rasterizer->get_render_info(p_info);
  5062. }
  5063. bool VisualServerRaster::has_feature(Features p_feature) const {
  5064. return rasterizer->has_feature(p_feature); // lies for now
  5065. }
  5066. void VisualServerRaster::set_default_clear_color(const Color& p_color) {
  5067. clear_color=p_color;
  5068. }
  5069. void VisualServerRaster::set_boot_image(const Image& p_image, const Color& p_color,bool p_scale) {
  5070. if (p_image.empty())
  5071. return;
  5072. rasterizer->restore_framebuffer();
  5073. rasterizer->begin_frame();
  5074. int window_w = OS::get_singleton()->get_video_mode(0).width;
  5075. int window_h = OS::get_singleton()->get_video_mode(0).height;
  5076. ViewportRect vr;
  5077. vr.x=0;
  5078. vr.y=0;
  5079. vr.width=OS::get_singleton()->get_video_mode(0).width;
  5080. vr.height=OS::get_singleton()->get_video_mode(0).height;
  5081. rasterizer->set_viewport(vr);
  5082. rasterizer->clear_viewport(p_color);
  5083. rasterizer->canvas_begin();
  5084. RID texture = texture_create();
  5085. texture_allocate(texture,p_image.get_width(),p_image.get_height(),p_image.get_format(),TEXTURE_FLAG_FILTER);
  5086. texture_set_data(texture,p_image);
  5087. rasterizer->canvas_begin_rect(Matrix32());
  5088. Rect2 imgrect(0,0,p_image.get_width(),p_image.get_height());
  5089. Rect2 screenrect;
  5090. if (p_scale) {
  5091. if (window_w > window_h) {
  5092. //scale horizontally
  5093. screenrect.size.y = window_h;
  5094. screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y;
  5095. screenrect.pos.x = (window_w - screenrect.size.x)/2;
  5096. } else {
  5097. //scale vertically
  5098. screenrect.size.x = window_w;
  5099. screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x;
  5100. screenrect.pos.y = (window_h - screenrect.size.y)/2;
  5101. }
  5102. } else {
  5103. screenrect=imgrect;
  5104. screenrect.pos+=((Size2(vr.width,vr.height)-screenrect.size)/2.0).floor();
  5105. }
  5106. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,1));
  5107. rasterizer->canvas_end_rect();
  5108. rasterizer->end_frame();
  5109. rasterizer->flush_frame();
  5110. free(texture); // free since it's only one frame that stays there
  5111. }
  5112. void VisualServerRaster::init() {
  5113. rasterizer->init();
  5114. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  5115. //default_scenario = scenario_create();
  5116. //default_viewport = viewport_create();
  5117. for(int i=0;i<4;i++)
  5118. black_margin[i]=0;
  5119. Image img;
  5120. img.create(default_mouse_cursor_xpm);
  5121. //img.convert(Image::FORMAT_RGB);
  5122. default_cursor_texture = texture_create_from_image(img, 0);
  5123. aabb_random_points.resize( GLOBAL_DEF("render/aabb_random_points",16) );
  5124. for(int i=0;i<aabb_random_points.size();i++)
  5125. aabb_random_points[i]=Vector3(Math::random(0,1),Math::random(0,1),Math::random(0,1));
  5126. transformed_aabb_random_points.resize(aabb_random_points.size());
  5127. changes=0;
  5128. }
  5129. void VisualServerRaster::_clean_up_owner(RID_OwnerBase *p_owner,String p_type) {
  5130. List<RID> rids;
  5131. p_owner->get_owned_list(&rids);
  5132. int lost=0;
  5133. for(List<RID>::Element *I=rids.front();I;I=I->next()) {
  5134. if (OS::get_singleton()->is_stdout_verbose()) {
  5135. lost++;
  5136. }
  5137. free(I->get());
  5138. }
  5139. if (lost)
  5140. print_line("VisualServerRaster: WARNING: Lost "+itos(lost)+" RIDs of type "+p_type);
  5141. }
  5142. void VisualServerRaster::finish() {
  5143. free(default_cursor_texture);
  5144. if (test_cube.is_valid())
  5145. free(test_cube);
  5146. _free_internal_rids();
  5147. _clean_up_owner( &room_owner,"Room" );
  5148. _clean_up_owner( &portal_owner,"Portal" );
  5149. _clean_up_owner( &camera_owner,"Camera" );
  5150. _clean_up_owner( &viewport_owner,"Viewport" );
  5151. _clean_up_owner( &scenario_owner,"Scenario" );
  5152. _clean_up_owner( &instance_owner,"Instance" );
  5153. _clean_up_owner( &canvas_owner,"Canvas" );
  5154. _clean_up_owner( &canvas_item_owner,"CanvasItem" );
  5155. rasterizer->finish();
  5156. octree_allocator.clear();
  5157. if (instance_dependency_map.size()) {
  5158. print_line("Base resources missing amount: "+itos(instance_dependency_map.size()));
  5159. }
  5160. ERR_FAIL_COND( instance_dependency_map.size() );
  5161. }
  5162. RID VisualServerRaster::get_test_cube() {
  5163. if (test_cube.is_valid())
  5164. return test_cube;
  5165. test_cube=_make_test_cube();
  5166. return test_cube;
  5167. }
  5168. VisualServerRaster::VisualServerRaster(Rasterizer *p_rasterizer) {
  5169. rasterizer=p_rasterizer;
  5170. rasterizer->draw_viewport_func=_render_canvas_item_viewport;
  5171. instance_update_list=NULL;
  5172. render_pass=0;
  5173. clear_color=Color(0.3,0.3,0.3,1.0);
  5174. OctreeAllocator::allocator=&octree_allocator;
  5175. draw_extra_frame=false;
  5176. }
  5177. VisualServerRaster::~VisualServerRaster()
  5178. {
  5179. }