visual_server_raster.cpp 198 KB

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