visual_server_raster.cpp 199 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553
  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-2015 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_distance_field_mode(RID p_item, bool p_distance_field) {
  2206. VS_CHANGED;
  2207. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2208. ERR_FAIL_COND(!canvas_item);
  2209. canvas_item->distance_field=p_distance_field;
  2210. }
  2211. void VisualServerRaster::canvas_item_set_transform(RID p_item, const Matrix32& p_transform) {
  2212. VS_CHANGED;
  2213. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2214. ERR_FAIL_COND(!canvas_item);
  2215. canvas_item->xform=p_transform;
  2216. }
  2217. void VisualServerRaster::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect,const Rect2& p_rect) {
  2218. VS_CHANGED;
  2219. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2220. ERR_FAIL_COND(!canvas_item);
  2221. canvas_item->custom_rect=p_custom_rect;
  2222. if (p_custom_rect)
  2223. canvas_item->rect=p_rect;
  2224. }
  2225. void VisualServerRaster::canvas_item_set_opacity(RID p_item, float p_opacity) {
  2226. VS_CHANGED;
  2227. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2228. ERR_FAIL_COND(!canvas_item);
  2229. canvas_item->opacity=p_opacity;
  2230. }
  2231. float VisualServerRaster::canvas_item_get_opacity(RID p_item, float p_opacity) const {
  2232. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2233. ERR_FAIL_COND_V(!canvas_item,-1);
  2234. return canvas_item->opacity;
  2235. }
  2236. void VisualServerRaster::canvas_item_set_on_top(RID p_item, bool p_on_top) {
  2237. VS_CHANGED;
  2238. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2239. ERR_FAIL_COND(!canvas_item);
  2240. canvas_item->ontop=p_on_top;
  2241. }
  2242. bool VisualServerRaster::canvas_item_is_on_top(RID p_item) const{
  2243. const CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2244. ERR_FAIL_COND_V(!canvas_item,false);
  2245. return canvas_item->ontop;
  2246. }
  2247. void VisualServerRaster::canvas_item_set_self_opacity(RID p_item, float p_self_opacity) {
  2248. VS_CHANGED;
  2249. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2250. ERR_FAIL_COND(!canvas_item);
  2251. canvas_item->self_opacity=p_self_opacity;
  2252. }
  2253. float VisualServerRaster::canvas_item_get_self_opacity(RID p_item, float p_self_opacity) const {
  2254. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2255. ERR_FAIL_COND_V(!canvas_item,-1);
  2256. return canvas_item->self_opacity;
  2257. }
  2258. void VisualServerRaster::canvas_item_add_line(RID p_item, const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width) {
  2259. VS_CHANGED;
  2260. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2261. ERR_FAIL_COND(!canvas_item);
  2262. CanvasItem::CommandLine * line = memnew( CanvasItem::CommandLine );
  2263. ERR_FAIL_COND(!line);
  2264. line->color=p_color;
  2265. line->from=p_from;
  2266. line->to=p_to;
  2267. line->width=p_width;
  2268. canvas_item->rect_dirty=true;
  2269. canvas_item->commands.push_back(line);
  2270. }
  2271. void VisualServerRaster::canvas_item_add_rect(RID p_item, const Rect2& p_rect, const Color& p_color) {
  2272. VS_CHANGED;
  2273. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2274. ERR_FAIL_COND(!canvas_item);
  2275. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2276. ERR_FAIL_COND(!rect);
  2277. rect->modulate=p_color;
  2278. rect->rect=p_rect;
  2279. canvas_item->rect_dirty=true;
  2280. canvas_item->commands.push_back(rect);
  2281. }
  2282. void VisualServerRaster::canvas_item_add_circle(RID p_item, const Point2& p_pos, float p_radius,const Color& p_color) {
  2283. VS_CHANGED;
  2284. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2285. ERR_FAIL_COND(!canvas_item);
  2286. CanvasItem::CommandCircle * circle = memnew( CanvasItem::CommandCircle );
  2287. ERR_FAIL_COND(!circle);
  2288. circle->color=p_color;
  2289. circle->pos=p_pos;
  2290. circle->radius=p_radius;
  2291. canvas_item->commands.push_back(circle);
  2292. }
  2293. 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) {
  2294. VS_CHANGED;
  2295. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2296. ERR_FAIL_COND(!canvas_item);
  2297. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2298. ERR_FAIL_COND(!rect);
  2299. rect->modulate=p_modulate;
  2300. rect->rect=p_rect;
  2301. rect->flags=0;
  2302. if (p_tile)
  2303. rect->flags|=Rasterizer::CANVAS_RECT_TILE;
  2304. if (p_rect.size.x<0) {
  2305. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2306. rect->rect.size.x = -rect->rect.size.x;
  2307. }
  2308. if (p_rect.size.y<0) {
  2309. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2310. rect->rect.size.y = -rect->rect.size.y;
  2311. }
  2312. if (p_transpose) {
  2313. rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE;
  2314. SWAP(rect->rect.size.x, rect->rect.size.y);
  2315. }
  2316. rect->texture=p_texture;
  2317. canvas_item->rect_dirty=true;
  2318. canvas_item->commands.push_back(rect);
  2319. }
  2320. 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) {
  2321. VS_CHANGED;
  2322. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2323. ERR_FAIL_COND(!canvas_item);
  2324. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2325. ERR_FAIL_COND(!rect);
  2326. rect->modulate=p_modulate;
  2327. rect->rect=p_rect;
  2328. rect->texture=p_texture;
  2329. rect->source=p_src_rect;
  2330. rect->flags=Rasterizer::CANVAS_RECT_REGION;
  2331. if (p_rect.size.x<0) {
  2332. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2333. rect->rect.size.x = -rect->rect.size.x;
  2334. }
  2335. if (p_rect.size.y<0) {
  2336. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2337. rect->rect.size.y = -rect->rect.size.y;
  2338. }
  2339. if (p_transpose) {
  2340. rect->flags|=Rasterizer::CANVAS_RECT_TRANSPOSE;
  2341. SWAP(rect->rect.size.x, rect->rect.size.y);
  2342. }
  2343. canvas_item->rect_dirty=true;
  2344. canvas_item->commands.push_back(rect);
  2345. }
  2346. 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) {
  2347. VS_CHANGED;
  2348. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2349. ERR_FAIL_COND(!canvas_item);
  2350. CanvasItem::CommandStyle * style = memnew( CanvasItem::CommandStyle );
  2351. ERR_FAIL_COND(!style);
  2352. style->texture=p_texture;
  2353. style->rect=p_rect;
  2354. style->draw_center=p_draw_center;
  2355. style->color=p_modulate;
  2356. style->margin[MARGIN_LEFT]=p_topleft.x;
  2357. style->margin[MARGIN_TOP]=p_topleft.y;
  2358. style->margin[MARGIN_RIGHT]=p_bottomright.x;
  2359. style->margin[MARGIN_BOTTOM]=p_bottomright.y;
  2360. canvas_item->rect_dirty=true;
  2361. canvas_item->commands.push_back(style);
  2362. }
  2363. 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) {
  2364. VS_CHANGED;
  2365. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2366. ERR_FAIL_COND(!canvas_item);
  2367. CanvasItem::CommandPrimitive * prim = memnew( CanvasItem::CommandPrimitive );
  2368. ERR_FAIL_COND(!prim);
  2369. prim->texture=p_texture;
  2370. prim->points=p_points;
  2371. prim->uvs=p_uvs;
  2372. prim->colors=p_colors;
  2373. prim->width=p_width;
  2374. canvas_item->rect_dirty=true;
  2375. canvas_item->commands.push_back(prim);
  2376. }
  2377. 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) {
  2378. VS_CHANGED;
  2379. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2380. ERR_FAIL_COND(!canvas_item);
  2381. #ifdef DEBUG_ENABLED
  2382. int pointcount = p_points.size();
  2383. ERR_FAIL_COND(pointcount<3);
  2384. int color_size=p_colors.size();
  2385. int uv_size=p_uvs.size();
  2386. ERR_FAIL_COND(color_size!=0 && color_size!=1 && color_size!=pointcount);
  2387. ERR_FAIL_COND(uv_size!=0 && (uv_size!=pointcount || !p_texture.is_valid()));
  2388. #endif
  2389. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  2390. if (indices.empty()) {
  2391. ERR_EXPLAIN("Bad Polygon!");
  2392. ERR_FAIL_V();
  2393. }
  2394. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2395. ERR_FAIL_COND(!polygon);
  2396. polygon->texture=p_texture;
  2397. polygon->points=p_points;
  2398. polygon->uvs=p_uvs;
  2399. polygon->colors=p_colors;
  2400. polygon->indices=indices;
  2401. polygon->count=indices.size();
  2402. canvas_item->rect_dirty=true;
  2403. canvas_item->commands.push_back(polygon);
  2404. }
  2405. 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) {
  2406. VS_CHANGED;
  2407. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2408. ERR_FAIL_COND(!canvas_item);
  2409. ERR_FAIL_COND(p_count <= 0);
  2410. ERR_FAIL_COND(p_points == NULL);
  2411. CanvasItem::CommandPolygonPtr * polygon = memnew( CanvasItem::CommandPolygonPtr );
  2412. ERR_FAIL_COND(!polygon);
  2413. polygon->texture=p_texture;
  2414. polygon->points=p_points;
  2415. polygon->uvs=p_uvs;
  2416. polygon->colors=p_colors;
  2417. polygon->indices=p_indices;
  2418. polygon->count = p_count * 3;
  2419. canvas_item->rect_dirty=true;
  2420. canvas_item->commands.push_back(polygon);
  2421. };
  2422. 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) {
  2423. VS_CHANGED;
  2424. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2425. ERR_FAIL_COND(!canvas_item);
  2426. int ps = p_points.size();
  2427. ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps && p_colors.size()!=1);
  2428. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps);
  2429. Vector<int> indices = p_indices;
  2430. int count = p_count * 3;
  2431. if (indices.empty()) {
  2432. ERR_FAIL_COND( ps % 3 != 0 );
  2433. if (p_count == -1)
  2434. count = ps;
  2435. } else {
  2436. ERR_FAIL_COND( indices.size() % 3 != 0 );
  2437. if (p_count == -1)
  2438. count = indices.size();
  2439. }
  2440. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2441. ERR_FAIL_COND(!polygon);
  2442. polygon->texture=p_texture;
  2443. polygon->points=p_points;
  2444. polygon->uvs=p_uvs;
  2445. polygon->colors=p_colors;
  2446. polygon->indices=indices;
  2447. polygon->count = count;
  2448. canvas_item->rect_dirty=true;
  2449. canvas_item->commands.push_back(polygon);
  2450. }
  2451. void VisualServerRaster::canvas_item_add_set_transform(RID p_item,const Matrix32& p_transform) {
  2452. VS_CHANGED;
  2453. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2454. ERR_FAIL_COND(!canvas_item);
  2455. CanvasItem::CommandTransform * tr = memnew( CanvasItem::CommandTransform );
  2456. ERR_FAIL_COND(!tr);
  2457. tr->xform=p_transform;
  2458. canvas_item->commands.push_back(tr);
  2459. }
  2460. void VisualServerRaster::canvas_item_add_set_blend_mode(RID p_item, MaterialBlendMode p_blend) {
  2461. VS_CHANGED;
  2462. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2463. ERR_FAIL_COND(!canvas_item);
  2464. CanvasItem::CommandBlendMode * bm = memnew( CanvasItem::CommandBlendMode );
  2465. ERR_FAIL_COND(!bm);
  2466. bm->blend_mode = p_blend;
  2467. canvas_item->commands.push_back(bm);
  2468. };
  2469. void VisualServerRaster::canvas_item_set_z(RID p_item, int p_z) {
  2470. ERR_FAIL_COND(p_z<CANVAS_ITEM_Z_MIN || p_z>CANVAS_ITEM_Z_MAX);
  2471. VS_CHANGED;
  2472. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2473. ERR_FAIL_COND(!canvas_item);
  2474. canvas_item->z=p_z;
  2475. }
  2476. void VisualServerRaster::canvas_item_set_z_as_relative_to_parent(RID p_item, bool p_enable) {
  2477. VS_CHANGED;
  2478. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2479. ERR_FAIL_COND(!canvas_item);
  2480. canvas_item->z_relative=p_enable;
  2481. }
  2482. void VisualServerRaster::canvas_item_set_copy_to_backbuffer(RID p_item, bool p_enable, const Rect2& p_rect) {
  2483. VS_CHANGED;
  2484. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2485. ERR_FAIL_COND(!canvas_item);
  2486. if (bool(canvas_item->copy_back_buffer!=NULL) !=p_enable) {
  2487. if (p_enable) {
  2488. canvas_item->copy_back_buffer = memnew( Rasterizer::CanvasItem::CopyBackBuffer );
  2489. } else {
  2490. memdelete(canvas_item->copy_back_buffer);
  2491. canvas_item->copy_back_buffer=NULL;
  2492. }
  2493. }
  2494. if (p_enable) {
  2495. canvas_item->copy_back_buffer->rect=p_rect;
  2496. canvas_item->copy_back_buffer->full=p_rect==Rect2();
  2497. }
  2498. }
  2499. void VisualServerRaster::canvas_item_set_use_parent_material(RID p_item, bool p_enable) {
  2500. VS_CHANGED;
  2501. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2502. ERR_FAIL_COND(!canvas_item);
  2503. canvas_item->use_parent_material=p_enable;
  2504. }
  2505. void VisualServerRaster::canvas_item_set_material(RID p_item, RID p_material) {
  2506. VS_CHANGED;
  2507. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2508. ERR_FAIL_COND(!canvas_item);
  2509. if (canvas_item->material)
  2510. canvas_item->material->owners.erase(canvas_item);
  2511. canvas_item->material=NULL;
  2512. if (canvas_item_material_owner.owns(p_material)) {
  2513. canvas_item->material=canvas_item_material_owner.get(p_material);
  2514. canvas_item->material->owners.insert(canvas_item);
  2515. }
  2516. }
  2517. void VisualServerRaster::canvas_item_set_sort_children_by_y(RID p_item, bool p_enable) {
  2518. VS_CHANGED;
  2519. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2520. ERR_FAIL_COND(!canvas_item);
  2521. canvas_item->sort_y=p_enable;
  2522. }
  2523. void VisualServerRaster::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  2524. VS_CHANGED;
  2525. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2526. ERR_FAIL_COND(!canvas_item);
  2527. CanvasItem::CommandClipIgnore * ci = memnew( CanvasItem::CommandClipIgnore);
  2528. ERR_FAIL_COND(!ci);
  2529. ci->ignore=p_ignore;
  2530. canvas_item->commands.push_back(ci);
  2531. }
  2532. void VisualServerRaster::canvas_item_clear(RID p_item) {
  2533. VS_CHANGED;
  2534. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2535. ERR_FAIL_COND(!canvas_item);
  2536. canvas_item->clear();
  2537. }
  2538. void VisualServerRaster::canvas_item_raise(RID p_item) {
  2539. VS_CHANGED;
  2540. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2541. ERR_FAIL_COND(!canvas_item);
  2542. if (canvas_item->parent.is_valid()) {
  2543. if (canvas_owner.owns(canvas_item->parent)) {
  2544. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2545. int idx = canvas->find_item(canvas_item);
  2546. ERR_FAIL_COND(idx<0);
  2547. Canvas::ChildItem ci = canvas->child_items[idx];
  2548. canvas->child_items.remove(idx);
  2549. canvas->child_items.push_back(ci);
  2550. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2551. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2552. int idx = item_owner->child_items.find(canvas_item);
  2553. ERR_FAIL_COND(idx<0);
  2554. item_owner->child_items.remove(idx);
  2555. item_owner->child_items.push_back(canvas_item);
  2556. }
  2557. }
  2558. }
  2559. /***** CANVAS LIGHT *******/
  2560. RID VisualServerRaster::canvas_light_create() {
  2561. Rasterizer::CanvasLight *clight = memnew( Rasterizer::CanvasLight );
  2562. return canvas_light_owner.make_rid(clight);
  2563. }
  2564. void VisualServerRaster::canvas_light_attach_to_canvas(RID p_light,RID p_canvas){
  2565. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2566. ERR_FAIL_COND(!clight);
  2567. if (clight->canvas.is_valid()) {
  2568. Canvas *canvas = canvas_owner.get(clight->canvas);
  2569. canvas->lights.erase(clight);
  2570. }
  2571. if (!canvas_owner.owns(p_canvas))
  2572. p_canvas=RID();
  2573. clight->canvas=p_canvas;
  2574. if (clight->canvas.is_valid()) {
  2575. Canvas *canvas = canvas_owner.get(clight->canvas);
  2576. canvas->lights.insert(clight);
  2577. }
  2578. }
  2579. void VisualServerRaster::canvas_light_set_enabled(RID p_light, bool p_enabled){
  2580. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2581. ERR_FAIL_COND(!clight);
  2582. clight->enabled=p_enabled;
  2583. }
  2584. void VisualServerRaster::canvas_light_set_transform(RID p_light, const Matrix32& p_transform){
  2585. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2586. ERR_FAIL_COND(!clight);
  2587. clight->xform=p_transform;
  2588. }
  2589. void VisualServerRaster::canvas_light_set_scale(RID p_light, float p_scale) {
  2590. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2591. ERR_FAIL_COND(!clight);
  2592. clight->scale=p_scale;
  2593. }
  2594. void VisualServerRaster::canvas_light_set_texture(RID p_light, RID p_texture){
  2595. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2596. ERR_FAIL_COND(!clight);
  2597. clight->texture=p_texture;
  2598. }
  2599. void VisualServerRaster::canvas_light_set_texture_offset(RID p_light, const Vector2& p_offset){
  2600. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2601. ERR_FAIL_COND(!clight);
  2602. clight->texture_offset=p_offset;
  2603. }
  2604. void VisualServerRaster::canvas_light_set_color(RID p_light, const Color& p_color){
  2605. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2606. ERR_FAIL_COND(!clight);
  2607. clight->color=p_color;
  2608. }
  2609. void VisualServerRaster::canvas_light_set_height(RID p_light, float p_height){
  2610. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2611. ERR_FAIL_COND(!clight);
  2612. clight->height=p_height;
  2613. }
  2614. void VisualServerRaster::canvas_light_set_energy(RID p_light, float p_energy){
  2615. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2616. ERR_FAIL_COND(!clight);
  2617. clight->energy=p_energy;
  2618. }
  2619. void VisualServerRaster::canvas_light_set_z_range(RID p_light, int p_min_z,int p_max_z){
  2620. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2621. ERR_FAIL_COND(!clight);
  2622. clight->z_min=p_min_z;
  2623. clight->z_max=p_max_z;
  2624. }
  2625. void VisualServerRaster::canvas_light_set_layer_range(RID p_light, int p_min_layer,int p_max_layer) {
  2626. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2627. ERR_FAIL_COND(!clight);
  2628. clight->layer_min=p_min_layer;
  2629. clight->layer_max=p_max_layer;
  2630. }
  2631. void VisualServerRaster::canvas_light_set_item_mask(RID p_light, int p_mask){
  2632. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2633. ERR_FAIL_COND(!clight);
  2634. clight->item_mask=p_mask;
  2635. }
  2636. void VisualServerRaster::canvas_light_set_item_shadow_mask(RID p_light, int p_mask){
  2637. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2638. ERR_FAIL_COND(!clight);
  2639. clight->item_shadow_mask=p_mask;
  2640. }
  2641. void VisualServerRaster::canvas_light_set_mode(RID p_light, CanvasLightMode p_mode) {
  2642. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2643. ERR_FAIL_COND(!clight);
  2644. clight->mode=p_mode;
  2645. }
  2646. void VisualServerRaster::canvas_light_set_shadow_enabled(RID p_light, bool p_enabled){
  2647. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2648. ERR_FAIL_COND(!clight);
  2649. if (clight->shadow_buffer.is_valid()==p_enabled)
  2650. return;
  2651. if (p_enabled) {
  2652. clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  2653. } else {
  2654. rasterizer->free(clight->shadow_buffer);
  2655. clight->shadow_buffer=RID();
  2656. }
  2657. }
  2658. void VisualServerRaster::canvas_light_set_shadow_buffer_size(RID p_light, int p_size){
  2659. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2660. ERR_FAIL_COND(!clight);
  2661. ERR_FAIL_COND(p_size<32 || p_size>16384);
  2662. clight->shadow_buffer_size=nearest_power_of_2(p_size);
  2663. if (clight->shadow_buffer.is_valid()) {
  2664. rasterizer->free(clight->shadow_buffer);
  2665. clight->shadow_buffer=rasterizer->canvas_light_shadow_buffer_create(clight->shadow_buffer_size);
  2666. }
  2667. }
  2668. void VisualServerRaster::canvas_light_set_shadow_esm_multiplier(RID p_light, float p_multiplier) {
  2669. Rasterizer::CanvasLight *clight = canvas_light_owner.get(p_light);
  2670. ERR_FAIL_COND(!clight);
  2671. clight->shadow_esm_mult=p_multiplier;
  2672. }
  2673. /****** CANVAS LIGHT OCCLUDER ******/
  2674. RID VisualServerRaster::canvas_light_occluder_create() {
  2675. Rasterizer::CanvasLightOccluderInstance *occluder = memnew( Rasterizer::CanvasLightOccluderInstance );
  2676. return canvas_light_occluder_owner.make_rid( occluder );
  2677. }
  2678. void VisualServerRaster::canvas_light_occluder_attach_to_canvas(RID p_occluder,RID p_canvas) {
  2679. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2680. ERR_FAIL_COND(!occluder);
  2681. if (occluder->canvas.is_valid()) {
  2682. Canvas *canvas = canvas_owner.get(occluder->canvas);
  2683. canvas->occluders.erase(occluder);
  2684. }
  2685. if (!canvas_owner.owns(p_canvas))
  2686. p_canvas=RID();
  2687. occluder->canvas=p_canvas;
  2688. if (occluder->canvas.is_valid()) {
  2689. Canvas *canvas = canvas_owner.get(occluder->canvas);
  2690. canvas->occluders.insert(occluder);
  2691. }
  2692. }
  2693. void VisualServerRaster::canvas_light_occluder_set_enabled(RID p_occluder,bool p_enabled){
  2694. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2695. ERR_FAIL_COND(!occluder);
  2696. occluder->enabled=p_enabled;
  2697. }
  2698. void VisualServerRaster::canvas_light_occluder_set_polygon(RID p_occluder,RID p_polygon) {
  2699. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2700. ERR_FAIL_COND(!occluder);
  2701. if (occluder->polygon.is_valid()) {
  2702. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  2703. if (occluder_poly) {
  2704. occluder_poly->owners.erase(occluder);
  2705. }
  2706. }
  2707. occluder->polygon=p_polygon;
  2708. occluder->polygon_buffer=RID();
  2709. if (occluder->polygon.is_valid()) {
  2710. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_polygon);
  2711. if (!occluder_poly)
  2712. occluder->polygon=RID();
  2713. ERR_FAIL_COND(!occluder_poly);
  2714. occluder_poly->owners.insert(occluder);
  2715. occluder->polygon_buffer=occluder_poly->occluder;
  2716. occluder->aabb_cache=occluder_poly->aabb;
  2717. occluder->cull_cache=occluder_poly->cull_mode;
  2718. }
  2719. }
  2720. void VisualServerRaster::canvas_light_occluder_set_transform(RID p_occluder,const Matrix32& p_xform) {
  2721. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2722. ERR_FAIL_COND(!occluder);
  2723. occluder->xform=p_xform;
  2724. }
  2725. void VisualServerRaster::canvas_light_occluder_set_light_mask(RID p_occluder,int p_mask) {
  2726. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_occluder);
  2727. ERR_FAIL_COND(!occluder);
  2728. occluder->light_mask=p_mask;
  2729. }
  2730. RID VisualServerRaster::canvas_occluder_polygon_create() {
  2731. CanvasLightOccluderPolygon * occluder_poly = memnew( CanvasLightOccluderPolygon );
  2732. occluder_poly->occluder=rasterizer->canvas_light_occluder_create();
  2733. return canvas_light_occluder_polygon_owner.make_rid(occluder_poly);
  2734. }
  2735. void VisualServerRaster::canvas_occluder_polygon_set_shape(RID p_occluder_polygon, const DVector<Vector2>& p_shape, bool p_close){
  2736. if (p_shape.size()<3) {
  2737. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,p_shape);
  2738. return;
  2739. }
  2740. DVector<Vector2> lines;
  2741. int lc = p_shape.size()*2;
  2742. lines.resize(lc-(p_close?0:2));
  2743. {
  2744. DVector<Vector2>::Write w = lines.write();
  2745. DVector<Vector2>::Read r = p_shape.read();
  2746. int max=lc/2;
  2747. if (!p_close) {
  2748. max--;
  2749. }
  2750. for(int i=0;i<max;i++) {
  2751. Vector2 a = r[i];
  2752. Vector2 b = r[(i+1)%(lc/2)];
  2753. w[i*2+0]=a;
  2754. w[i*2+1]=b;
  2755. }
  2756. }
  2757. canvas_occluder_polygon_set_shape_as_lines(p_occluder_polygon,lines);
  2758. }
  2759. void VisualServerRaster::canvas_occluder_polygon_set_shape_as_lines(RID p_occluder_polygon,const DVector<Vector2>& p_shape) {
  2760. CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  2761. ERR_FAIL_COND(!occluder_poly);
  2762. ERR_FAIL_COND(p_shape.size()&1);
  2763. int lc = p_shape.size();
  2764. occluder_poly->aabb=Rect2();
  2765. {
  2766. DVector<Vector2>::Read r = p_shape.read();
  2767. for(int i=0;i<lc;i++) {
  2768. if (i==0)
  2769. occluder_poly->aabb.pos=r[i];
  2770. else
  2771. occluder_poly->aabb.expand_to(r[i]);
  2772. }
  2773. }
  2774. rasterizer->canvas_light_occluder_set_polylines(occluder_poly->occluder,p_shape);
  2775. for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) {
  2776. E->get()->aabb_cache=occluder_poly->aabb;
  2777. }
  2778. }
  2779. void VisualServerRaster::canvas_occluder_polygon_set_cull_mode(RID p_occluder_polygon,CanvasOccluderPolygonCullMode p_mode) {
  2780. CanvasLightOccluderPolygon * occluder_poly = canvas_light_occluder_polygon_owner.get(p_occluder_polygon);
  2781. ERR_FAIL_COND(!occluder_poly);
  2782. occluder_poly->cull_mode=p_mode;
  2783. for( Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=occluder_poly->owners.front();E;E=E->next()) {
  2784. E->get()->cull_cache=p_mode;
  2785. }
  2786. }
  2787. RID VisualServerRaster::canvas_item_material_create() {
  2788. Rasterizer::CanvasItemMaterial *material = memnew( Rasterizer::CanvasItemMaterial );
  2789. return canvas_item_material_owner.make_rid(material);
  2790. }
  2791. void VisualServerRaster::canvas_item_material_set_shader(RID p_material, RID p_shader){
  2792. VS_CHANGED;
  2793. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2794. ERR_FAIL_COND(!material);
  2795. material->shader=p_shader;
  2796. }
  2797. void VisualServerRaster::canvas_item_material_set_shader_param(RID p_material, const StringName& p_param, const Variant& p_value){
  2798. VS_CHANGED;
  2799. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2800. ERR_FAIL_COND(!material);
  2801. if (p_value.get_type()==Variant::NIL)
  2802. material->shader_param.erase(p_param);
  2803. else
  2804. material->shader_param[p_param]=p_value;
  2805. }
  2806. Variant VisualServerRaster::canvas_item_material_get_shader_param(RID p_material, const StringName& p_param) const{
  2807. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2808. ERR_FAIL_COND_V(!material,Variant());
  2809. if (!material->shader_param.has(p_param)) {
  2810. ERR_FAIL_COND_V(!material->shader.is_valid(),Variant());
  2811. return rasterizer->shader_get_default_param(material->shader,p_param);
  2812. }
  2813. return material->shader_param[p_param];
  2814. }
  2815. void VisualServerRaster::canvas_item_material_set_shading_mode(RID p_material, CanvasItemShadingMode p_mode) {
  2816. VS_CHANGED;
  2817. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get( p_material );
  2818. ERR_FAIL_COND(!material);
  2819. material->shading_mode=p_mode;
  2820. }
  2821. /******** CANVAS *********/
  2822. void VisualServerRaster::cursor_set_rotation(float p_rotation, int p_cursor) {
  2823. VS_CHANGED;
  2824. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2825. cursors[p_cursor].rot = p_rotation;
  2826. };
  2827. void VisualServerRaster::cursor_set_texture(RID p_texture, const Point2 &p_center_offset, int p_cursor) {
  2828. VS_CHANGED;
  2829. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2830. cursors[p_cursor].texture = p_texture;
  2831. cursors[p_cursor].center = p_center_offset;
  2832. };
  2833. void VisualServerRaster::cursor_set_visible(bool p_visible, int p_cursor) {
  2834. VS_CHANGED;
  2835. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2836. cursors[p_cursor].visible = p_visible;
  2837. };
  2838. void VisualServerRaster::cursor_set_pos(const Point2& p_pos, int p_cursor) {
  2839. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2840. if (cursors[p_cursor].pos==p_pos)
  2841. return;
  2842. VS_CHANGED;
  2843. cursors[p_cursor].pos = p_pos;
  2844. };
  2845. void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  2846. black_margin[MARGIN_LEFT]=p_left;
  2847. black_margin[MARGIN_TOP]=p_top;
  2848. black_margin[MARGIN_RIGHT]=p_right;
  2849. black_margin[MARGIN_BOTTOM]=p_bottom;
  2850. }
  2851. void VisualServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  2852. black_image[MARGIN_LEFT]=p_left;
  2853. black_image[MARGIN_TOP]=p_top;
  2854. black_image[MARGIN_RIGHT]=p_right;
  2855. black_image[MARGIN_BOTTOM]=p_bottom;
  2856. }
  2857. void VisualServerRaster::_free_attached_instances(RID p_rid,bool p_free_scenario) {
  2858. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  2859. if (E) {
  2860. // has instances
  2861. while( E->get().size() ) {
  2862. // erase all attached instances
  2863. if (p_free_scenario)
  2864. instance_set_scenario( E->get().front()->get(), RID() );
  2865. else
  2866. instance_set_base( E->get().front()->get(), RID() );
  2867. }
  2868. }
  2869. instance_dependency_map.erase(p_rid);
  2870. }
  2871. void VisualServerRaster::custom_shade_model_set_shader(int p_model, RID p_shader) {
  2872. VS_CHANGED;
  2873. // rasterizer->custom_shade_model_set_shader(p_model,p_shader);
  2874. }
  2875. RID VisualServerRaster::custom_shade_model_get_shader(int p_model) const {
  2876. //return rasterizer->custom_shade_model_get_shader(p_model);
  2877. return RID();
  2878. }
  2879. void VisualServerRaster::custom_shade_model_set_name(int p_model, const String& p_name) {
  2880. //rasterizer->custom_shade_model_set_name(p_model,p_name);
  2881. }
  2882. String VisualServerRaster::custom_shade_model_get_name(int p_model) const {
  2883. //return rasterizer->custom_shade_model_get_name(p_model);
  2884. return "";
  2885. }
  2886. void VisualServerRaster::custom_shade_model_set_param_info(int p_model, const List<PropertyInfo>& p_info) {
  2887. VS_CHANGED;
  2888. //rasterizer->custom_shade_model_set_param_info(p_model,p_info);
  2889. }
  2890. void VisualServerRaster::custom_shade_model_get_param_info(int p_model, List<PropertyInfo>* p_info) const {
  2891. //rasterizer->custom_shade_model_get_param_info(p_model,p_info);
  2892. }
  2893. void VisualServerRaster::free( RID p_rid ) {
  2894. VS_CHANGED;
  2895. if (rasterizer->is_texture(p_rid) || rasterizer->is_material(p_rid) || rasterizer->is_shader(p_rid) || rasterizer->is_environment(p_rid)) {
  2896. rasterizer->free(p_rid);
  2897. } else if (rasterizer->is_skeleton(p_rid)) {
  2898. Map< RID, Set<Instance*> >::Element *E=skeleton_dependency_map.find(p_rid);
  2899. if (E) {
  2900. //detach skeletons
  2901. for (Set<Instance*>::Element *F=E->get().front();F;F=F->next()) {
  2902. F->get()->data.skeleton=RID();
  2903. }
  2904. skeleton_dependency_map.erase(E);
  2905. }
  2906. rasterizer->free(p_rid);
  2907. } else if (rasterizer->is_mesh(p_rid) || rasterizer->is_multimesh(p_rid) || rasterizer->is_light(p_rid) || rasterizer->is_particles(p_rid) || rasterizer->is_immediate(p_rid)) {
  2908. //delete the resource
  2909. _free_attached_instances(p_rid);
  2910. rasterizer->free(p_rid);
  2911. } else if (room_owner.owns(p_rid)) {
  2912. _free_attached_instances(p_rid);
  2913. Room *room = room_owner.get(p_rid);
  2914. ERR_FAIL_COND(!room);
  2915. room_owner.free(p_rid);
  2916. memdelete(room);
  2917. } else if (portal_owner.owns(p_rid)) {
  2918. _free_attached_instances(p_rid);
  2919. Portal *portal = portal_owner.get(p_rid);
  2920. ERR_FAIL_COND(!portal);
  2921. portal_owner.free(p_rid);
  2922. memdelete(portal);
  2923. } else if (baked_light_owner.owns(p_rid)) {
  2924. _free_attached_instances(p_rid);
  2925. BakedLight *baked_light = baked_light_owner.get(p_rid);
  2926. ERR_FAIL_COND(!baked_light);
  2927. if (baked_light->data.octree_texture.is_valid())
  2928. rasterizer->free(baked_light->data.octree_texture);
  2929. baked_light_owner.free(p_rid);
  2930. memdelete(baked_light);
  2931. } else if (baked_light_sampler_owner.owns(p_rid)) {
  2932. _free_attached_instances(p_rid);
  2933. BakedLightSampler *baked_light_sampler = baked_light_sampler_owner.get(p_rid);
  2934. ERR_FAIL_COND(!baked_light_sampler);
  2935. //if (baked_light->data.octree_texture.is_valid())
  2936. // rasterizer->free(baked_light->data.octree_texture);
  2937. baked_light_sampler_owner.free(p_rid);
  2938. memdelete(baked_light_sampler);
  2939. } else if (camera_owner.owns(p_rid)) {
  2940. // delete te camera
  2941. Camera *camera = camera_owner.get(p_rid);
  2942. ERR_FAIL_COND(!camera);
  2943. camera_owner.free( p_rid );
  2944. memdelete(camera);
  2945. } else if (viewport_owner.owns(p_rid)) {
  2946. // delete the viewport
  2947. Viewport *viewport = viewport_owner.get( p_rid );
  2948. ERR_FAIL_COND(!viewport);
  2949. // Viewport *parent=NULL;
  2950. rasterizer->free(viewport->viewport_data);
  2951. if (viewport->render_target.is_valid()) {
  2952. rasterizer->free(viewport->render_target);
  2953. }
  2954. if (viewport->update_list.in_list())
  2955. viewport_update_list.remove(&viewport->update_list);
  2956. if (screen_viewports.has(p_rid))
  2957. screen_viewports.erase(p_rid);
  2958. while(viewport->canvas_map.size()) {
  2959. Canvas *c = viewport->canvas_map.front()->get().canvas;
  2960. c->viewports.erase(p_rid);
  2961. viewport->canvas_map.erase(viewport->canvas_map.front());
  2962. }
  2963. viewport_owner.free(p_rid);
  2964. memdelete(viewport);
  2965. } else if (instance_owner.owns(p_rid)) {
  2966. // delete the instance
  2967. _update_instances(); // be sure
  2968. Instance *instance = instance_owner.get(p_rid);
  2969. ERR_FAIL_COND(!instance);
  2970. instance_set_room(p_rid,RID());
  2971. instance_set_scenario(p_rid,RID());
  2972. instance_geometry_set_baked_light(p_rid,RID());
  2973. instance_geometry_set_baked_light_sampler(p_rid,RID());
  2974. instance_set_base(p_rid,RID());
  2975. if (instance->data.skeleton.is_valid())
  2976. instance_attach_skeleton(p_rid,RID());
  2977. instance_owner.free(p_rid);
  2978. memdelete(instance);
  2979. } else if (canvas_owner.owns(p_rid)) {
  2980. Canvas *canvas = canvas_owner.get(p_rid);
  2981. ERR_FAIL_COND(!canvas);
  2982. while(canvas->viewports.size()) {
  2983. Viewport *vp = viewport_owner.get(canvas->viewports.front()->get());
  2984. ERR_FAIL_COND(!vp);
  2985. Map<RID,Viewport::CanvasData>::Element *E=vp->canvas_map.find(p_rid);
  2986. ERR_FAIL_COND(!E);
  2987. vp->canvas_map.erase(p_rid);
  2988. canvas->viewports.erase( canvas->viewports.front() );
  2989. }
  2990. for (int i=0;i<canvas->child_items.size();i++) {
  2991. canvas->child_items[i].item->parent=RID();
  2992. }
  2993. for (Set<Rasterizer::CanvasLight*>::Element *E=canvas->lights.front();E;E=E->next()) {
  2994. E->get()->canvas=RID();
  2995. }
  2996. for (Set<Rasterizer::CanvasLightOccluderInstance*>::Element *E=canvas->occluders.front();E;E=E->next()) {
  2997. E->get()->canvas=RID();
  2998. }
  2999. canvas_owner.free( p_rid );
  3000. memdelete( canvas );
  3001. } else if (canvas_item_owner.owns(p_rid)) {
  3002. CanvasItem *canvas_item = canvas_item_owner.get(p_rid);
  3003. ERR_FAIL_COND(!canvas_item);
  3004. if (canvas_item->parent.is_valid()) {
  3005. if (canvas_owner.owns(canvas_item->parent)) {
  3006. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  3007. canvas->erase_item(canvas_item);
  3008. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  3009. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  3010. item_owner->child_items.erase(canvas_item);
  3011. }
  3012. }
  3013. for (int i=0;i<canvas_item->child_items.size();i++) {
  3014. canvas_item->child_items[i]->parent=RID();
  3015. }
  3016. if (canvas_item->material) {
  3017. canvas_item->material->owners.erase(canvas_item);
  3018. }
  3019. canvas_item_owner.free( p_rid );
  3020. memdelete( canvas_item );
  3021. } else if (canvas_item_material_owner.owns(p_rid)) {
  3022. Rasterizer::CanvasItemMaterial *material = canvas_item_material_owner.get(p_rid);
  3023. ERR_FAIL_COND(!material);
  3024. for(Set<Rasterizer::CanvasItem*>::Element *E=material->owners.front();E;E=E->next()) {
  3025. E->get()->material=NULL;
  3026. }
  3027. canvas_item_material_owner.free(p_rid);
  3028. memdelete(material);
  3029. } else if (canvas_light_owner.owns(p_rid)) {
  3030. Rasterizer::CanvasLight *canvas_light = canvas_light_owner.get(p_rid);
  3031. ERR_FAIL_COND(!canvas_light);
  3032. if (canvas_light->canvas.is_valid()) {
  3033. Canvas* canvas = canvas_owner.get(canvas_light->canvas);
  3034. if (canvas)
  3035. canvas->lights.erase(canvas_light);
  3036. }
  3037. if (canvas_light->shadow_buffer.is_valid())
  3038. rasterizer->free(canvas_light->shadow_buffer);
  3039. canvas_light_owner.free( p_rid );
  3040. memdelete( canvas_light );
  3041. } else if (canvas_light_occluder_owner.owns(p_rid)) {
  3042. Rasterizer::CanvasLightOccluderInstance *occluder = canvas_light_occluder_owner.get(p_rid);
  3043. ERR_FAIL_COND(!occluder);
  3044. if (occluder->polygon.is_valid()) {
  3045. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(occluder->polygon);
  3046. if (occluder_poly) {
  3047. occluder_poly->owners.erase(occluder);
  3048. }
  3049. }
  3050. if (occluder->canvas.is_valid() && canvas_owner.owns(occluder->canvas)) {
  3051. Canvas *canvas = canvas_owner.get(occluder->canvas);
  3052. canvas->occluders.erase(occluder);
  3053. }
  3054. canvas_light_occluder_owner.free( p_rid );
  3055. memdelete(occluder);
  3056. } else if (canvas_light_occluder_polygon_owner.owns(p_rid)) {
  3057. CanvasLightOccluderPolygon *occluder_poly = canvas_light_occluder_polygon_owner.get(p_rid);
  3058. ERR_FAIL_COND(!occluder_poly);
  3059. rasterizer->free(occluder_poly->occluder);
  3060. while(occluder_poly->owners.size()) {
  3061. occluder_poly->owners.front()->get()->polygon=RID();
  3062. occluder_poly->owners.erase( occluder_poly->owners.front() );
  3063. }
  3064. canvas_light_occluder_polygon_owner.free( p_rid );
  3065. memdelete(occluder_poly);
  3066. } else if (scenario_owner.owns(p_rid)) {
  3067. Scenario *scenario=scenario_owner.get(p_rid);
  3068. ERR_FAIL_COND(!scenario);
  3069. _update_instances(); // be sure
  3070. _free_attached_instances(p_rid,true);
  3071. //rasterizer->free( scenario->environment );
  3072. scenario_owner.free(p_rid);
  3073. memdelete(scenario);
  3074. } else {
  3075. ERR_FAIL();
  3076. }
  3077. }
  3078. void VisualServerRaster::_instance_draw(Instance *p_instance) {
  3079. if (p_instance->light_cache_dirty) {
  3080. int l=0;
  3081. //add positional lights
  3082. InstanceSet::Element *LE=p_instance->lights.front();
  3083. p_instance->data.light_instances.resize(p_instance->lights.size());
  3084. while(LE) {
  3085. p_instance->data.light_instances[l++]=LE->get()->light_info->instance;
  3086. LE=LE->next();
  3087. }
  3088. p_instance->light_cache_dirty=false;
  3089. }
  3090. switch(p_instance->base_type) {
  3091. case INSTANCE_MESH: {
  3092. const float *morphs = NULL;
  3093. if (!p_instance->data.morph_values.empty()) {
  3094. morphs=&p_instance->data.morph_values[0];
  3095. }
  3096. rasterizer->add_mesh(p_instance->base_rid, &p_instance->data);
  3097. } break;
  3098. case INSTANCE_MULTIMESH: {
  3099. rasterizer->add_multimesh(p_instance->base_rid, &p_instance->data);
  3100. } break;
  3101. case INSTANCE_IMMEDIATE: {
  3102. rasterizer->add_immediate(p_instance->base_rid, &p_instance->data);
  3103. } break;
  3104. case INSTANCE_PARTICLES: {
  3105. rasterizer->add_particles(p_instance->particles_info->instance, &p_instance->data);
  3106. } break;
  3107. default: {};
  3108. }
  3109. }
  3110. Vector<Vector3> VisualServerRaster::_camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  3111. // setup a camera matrix for that range!
  3112. CameraMatrix camera_matrix;
  3113. switch(p_camera->type) {
  3114. case Camera::ORTHOGONAL: {
  3115. camera_matrix.set_orthogonal(p_camera->size,viewport_rect.width / (float)viewport_rect.height,p_range_min,p_range_max,p_camera->vaspect);
  3116. } break;
  3117. case Camera::PERSPECTIVE: {
  3118. camera_matrix.set_perspective(
  3119. p_camera->fov,
  3120. viewport_rect.width / (float)viewport_rect.height,
  3121. p_range_min,
  3122. p_range_max,
  3123. p_camera->vaspect
  3124. );
  3125. } break;
  3126. }
  3127. //obtain the frustum endpoints
  3128. Vector<Vector3> endpoints;
  3129. endpoints.resize(8);
  3130. bool res = camera_matrix.get_endpoints(p_camera->transform,&endpoints[0]);
  3131. ERR_FAIL_COND_V(!res,Vector<Vector3>());
  3132. return endpoints;
  3133. }
  3134. Vector<Plane> VisualServerRaster::_camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  3135. Vector<Vector3> endpoints=_camera_generate_endpoints(p_light,p_camera,p_range_min,p_range_max); // frustum plane endpoints
  3136. ERR_FAIL_COND_V(endpoints.empty(),Vector<Plane>());
  3137. // obtain the light frustm ranges (given endpoints)
  3138. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3139. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3140. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3141. float x_min,x_max;
  3142. float y_min,y_max;
  3143. float z_min,z_max;
  3144. for(int j=0;j<8;j++) {
  3145. float d_x=x_vec.dot(endpoints[j]);
  3146. float d_y=y_vec.dot(endpoints[j]);
  3147. float d_z=z_vec.dot(endpoints[j]);
  3148. if (j==0 || d_x<x_min)
  3149. x_min=d_x;
  3150. if (j==0 || d_x>x_max)
  3151. x_max=d_x;
  3152. if (j==0 || d_y<y_min)
  3153. y_min=d_y;
  3154. if (j==0 || d_y>y_max)
  3155. y_max=d_y;
  3156. if (j==0 || d_z<z_min)
  3157. z_min=d_z;
  3158. if (j==0 || d_z>z_max)
  3159. z_max=d_z;
  3160. }
  3161. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3162. Vector<Plane> light_frustum_planes;
  3163. light_frustum_planes.resize(6);
  3164. //right/left
  3165. light_frustum_planes[0]=Plane( x_vec, x_max );
  3166. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  3167. //top/bottom
  3168. light_frustum_planes[2]=Plane( y_vec, y_max );
  3169. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  3170. //near/far
  3171. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  3172. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  3173. //TODO@ add more actual frustum planes to minimize get
  3174. return light_frustum_planes;
  3175. }
  3176. void VisualServerRaster::_light_instance_update_pssm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3177. int splits = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  3178. float split_weight=rasterizer->light_directional_get_shadow_param(p_light->base_rid,LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT);
  3179. float distances[5];
  3180. float texsize=rasterizer->light_instance_get_shadow_size( p_light->light_info->instance );
  3181. // float cull_min=p_cull_range.min;
  3182. //float cull_max=p_cull_range.max;
  3183. bool overlap = rasterizer->light_instance_get_pssm_shadow_overlap(p_light->light_info->instance);
  3184. float cull_min=p_camera->znear;
  3185. float cull_max=p_camera->zfar;
  3186. float max_dist = rasterizer->light_directional_get_shadow_param(p_light->base_rid,VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE);
  3187. if (max_dist>0.0)
  3188. cull_max=MIN(cull_max,max_dist);
  3189. for(int i = 0; i < splits; i++) {
  3190. float idm = i / (float)splits;
  3191. float lg = cull_min * Math::pow(cull_max/cull_min, idm);
  3192. float uniform = cull_min + (cull_max - cull_min) * idm;
  3193. distances[i] = lg * split_weight + uniform * (1.0 - split_weight);
  3194. }
  3195. distances[0]=cull_min;
  3196. distances[splits]=cull_max;
  3197. for (int i=0;i<splits;i++) {
  3198. // setup a camera matrix for that range!
  3199. CameraMatrix camera_matrix;
  3200. switch(p_camera->type) {
  3201. case Camera::ORTHOGONAL: {
  3202. camera_matrix.set_orthogonal(
  3203. p_camera->size,
  3204. viewport_rect.width / (float)viewport_rect.height,
  3205. distances[(i==0 || !overlap )?i:i-1],
  3206. distances[i+1],
  3207. p_camera->vaspect
  3208. );
  3209. } break;
  3210. case Camera::PERSPECTIVE: {
  3211. camera_matrix.set_perspective(
  3212. p_camera->fov,
  3213. viewport_rect.width / (float)viewport_rect.height,
  3214. distances[(i==0 || !overlap )?i:i-1],
  3215. distances[i+1],
  3216. p_camera->vaspect
  3217. );
  3218. } break;
  3219. }
  3220. //obtain the frustum endpoints
  3221. Vector3 endpoints[8]; // frustum plane endpoints
  3222. bool res = camera_matrix.get_endpoints(p_camera->transform,endpoints);
  3223. ERR_CONTINUE(!res);
  3224. // obtain the light frustm ranges (given endpoints)
  3225. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3226. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3227. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3228. //z_vec points agsint the camera, like in default opengl
  3229. float x_min,x_max;
  3230. float y_min,y_max;
  3231. float z_min,z_max;
  3232. float x_min_cam,x_max_cam;
  3233. float y_min_cam,y_max_cam;
  3234. float z_min_cam,z_max_cam;
  3235. //used for culling
  3236. for(int j=0;j<8;j++) {
  3237. float d_x=x_vec.dot(endpoints[j]);
  3238. float d_y=y_vec.dot(endpoints[j]);
  3239. float d_z=z_vec.dot(endpoints[j]);
  3240. if (j==0 || d_x<x_min)
  3241. x_min=d_x;
  3242. if (j==0 || d_x>x_max)
  3243. x_max=d_x;
  3244. if (j==0 || d_y<y_min)
  3245. y_min=d_y;
  3246. if (j==0 || d_y>y_max)
  3247. y_max=d_y;
  3248. if (j==0 || d_z<z_min)
  3249. z_min=d_z;
  3250. if (j==0 || d_z>z_max)
  3251. z_max=d_z;
  3252. }
  3253. {
  3254. //camera viewport stuff
  3255. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  3256. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  3257. Vector3 center;
  3258. for(int j=0;j<8;j++) {
  3259. center+=endpoints[j];
  3260. }
  3261. center/=8.0;
  3262. //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;
  3263. float radius=0;
  3264. for(int j=0;j<8;j++) {
  3265. float d = center.distance_to(endpoints[j]);
  3266. if (d>radius)
  3267. radius=d;
  3268. }
  3269. radius *= texsize/(texsize-2.0); //add a texel by each side, so stepified texture will always fit
  3270. x_max_cam=x_vec.dot(center)+radius;
  3271. x_min_cam=x_vec.dot(center)-radius;
  3272. y_max_cam=y_vec.dot(center)+radius;
  3273. y_min_cam=y_vec.dot(center)-radius;
  3274. z_max_cam=z_vec.dot(center)+radius;
  3275. z_min_cam=z_vec.dot(center)-radius;
  3276. float unit = radius*2.0/texsize;
  3277. x_max_cam=Math::stepify(x_max_cam,unit);
  3278. x_min_cam=Math::stepify(x_min_cam,unit);
  3279. y_max_cam=Math::stepify(y_max_cam,unit);
  3280. y_min_cam=Math::stepify(y_min_cam,unit);
  3281. }
  3282. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3283. Vector<Plane> light_frustum_planes;
  3284. light_frustum_planes.resize(6);
  3285. //right/left
  3286. light_frustum_planes[0]=Plane( x_vec, x_max );
  3287. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  3288. //top/bottom
  3289. light_frustum_planes[2]=Plane( y_vec, y_max );
  3290. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  3291. //near/far
  3292. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  3293. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  3294. int caster_cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3295. // a pre pass will need to be needed to determine the actual z-near to be used
  3296. for(int j=0;j<caster_cull_count;j++) {
  3297. float min,max;
  3298. Instance *ins=instance_shadow_cull_result[j];
  3299. if (!ins->visible || !ins->cast_shadows)
  3300. continue;
  3301. ins->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max);
  3302. if (max>z_max)
  3303. z_max=max;
  3304. }
  3305. {
  3306. CameraMatrix ortho_camera;
  3307. real_t half_x = (x_max_cam-x_min_cam) * 0.5;
  3308. real_t half_y = (y_max_cam-y_min_cam) * 0.5;
  3309. ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) );
  3310. Transform ortho_transform;
  3311. ortho_transform.basis=p_light->data.transform.basis;
  3312. ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max;
  3313. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance, i, ortho_camera, ortho_transform,distances[i],distances[i+1] );
  3314. }
  3315. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  3316. for (int j=0;j<caster_cull_count;j++) {
  3317. Instance *instance = instance_shadow_cull_result[j];
  3318. if (!instance->visible || !instance->cast_shadows)
  3319. continue;
  3320. _instance_draw(instance);
  3321. }
  3322. rasterizer->end_shadow_map();
  3323. }
  3324. }
  3325. CameraMatrix _lispm_look( const Vector3 pos, const Vector3 dir, const Vector3 up) {
  3326. Vector3 dirN;
  3327. Vector3 upN;
  3328. Vector3 lftN;
  3329. lftN=dir.cross(up);
  3330. lftN.normalize();
  3331. upN=lftN.cross(dir);
  3332. upN.normalize();
  3333. dirN=dir.normalized();
  3334. CameraMatrix cmout;
  3335. float *output=&cmout.matrix[0][0];
  3336. output[ 0] = lftN[0];
  3337. output[ 1] = upN[0];
  3338. output[ 2] = -dirN[0];
  3339. output[ 3] = 0.0;
  3340. output[ 4] = lftN[1];
  3341. output[ 5] = upN[1];
  3342. output[ 6] = -dirN[1];
  3343. output[ 7] = 0.0;
  3344. output[ 8] = lftN[2];
  3345. output[ 9] = upN[2];
  3346. output[10] = -dirN[2];
  3347. output[11] = 0.0;
  3348. output[12] = -lftN.dot(pos);
  3349. output[13] = -upN.dot(pos);
  3350. output[14] = dirN.dot(pos);
  3351. output[15] = 1.0;
  3352. return cmout;
  3353. }
  3354. #if 1
  3355. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3356. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  3357. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  3358. float viewdot = light_vec.normalized().dot(view_vec.normalized());
  3359. float near_dist=1;
  3360. Vector<Plane> light_frustum_planes = _camera_generate_orthogonal_planes(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3361. int caster_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3362. // this could be faster by just getting supports from the AABBs..
  3363. // but, safer to do as the original implementation explains for now..
  3364. Vector<Vector3> caster_pointcloud;
  3365. caster_pointcloud.resize(caster_count*8);
  3366. int caster_pointcloud_size=0;
  3367. {
  3368. //fill pointcloud
  3369. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3370. for(int i=0;i<caster_count;i++) {
  3371. Instance *ins = instance_shadow_cull_result[i];
  3372. if (!ins->visible || !ins->cast_shadows)
  3373. continue;
  3374. for(int j=0;j<8;j++) {
  3375. Vector3 v = ins->aabb.get_endpoint(j);
  3376. v = ins->data.transform.xform(v);
  3377. caster_pointcloud_ptr[caster_pointcloud_size+j]=v;
  3378. }
  3379. caster_pointcloud_size+=8;
  3380. }
  3381. }
  3382. // now generate a pointcloud that contains the maximum bound (camera extruded by light)
  3383. Vector<Vector3> camera_pointcloud = _camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3384. int cpcsize=camera_pointcloud.size();
  3385. camera_pointcloud.resize( cpcsize*2 );
  3386. for(int i=0;i<cpcsize;i++) {
  3387. camera_pointcloud[i+cpcsize]=camera_pointcloud[i]-light_vec*1000;
  3388. }
  3389. // Vector<Vector3> frustum_points=_camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  3390. // compute the "light-space" basis, using the algorithm described in the paper
  3391. // note: since bodyB is defined in eye space, all of these vectors should also be defined in eye space
  3392. Vector3 eye = p_camera->transform.origin;
  3393. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  3394. CameraMatrix light_space_basis = _lispm_look(eye,light_vec,up);
  3395. AABB light_space_aabb;
  3396. { //create an optimal AABB from both the camera pointcloud and the objects pointcloud
  3397. AABB light_space_pointcloud_aabb;
  3398. AABB light_space_camera_aabb;
  3399. //xform pointcloud
  3400. const Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3401. for(int i=0;i<caster_pointcloud_size;i++) {
  3402. Vector3 p = light_space_basis.xform(caster_pointcloud_ptr[i]);
  3403. if (i==0) {
  3404. light_space_pointcloud_aabb.pos=p;
  3405. } else {
  3406. light_space_pointcloud_aabb.expand_to(p);
  3407. }
  3408. }
  3409. for(int i=0;i<camera_pointcloud.size();i++) {
  3410. Vector3 p = light_space_basis.xform(camera_pointcloud[i]);
  3411. if (i==0) {
  3412. light_space_camera_aabb.pos=p;
  3413. } else {
  3414. light_space_camera_aabb.expand_to(p);
  3415. }
  3416. }
  3417. light_space_aabb=light_space_pointcloud_aabb.intersection(light_space_camera_aabb);
  3418. }
  3419. float lvdp = light_vec.dot(view_vec);
  3420. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  3421. //use the formulas of the paper to get n (and f)
  3422. float factor = 1.0/sin_gamma;
  3423. float z_n = factor*near_dist; //often 1
  3424. float d = Math::abs(light_space_aabb.size.y); //perspective transform depth //light space y extents
  3425. float z_f = z_n + d*sin_gamma;
  3426. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  3427. float f = n+d;
  3428. Vector3 pos = eye - up*(n-near_dist);
  3429. CameraMatrix light_space_basis2 = _lispm_look(pos,light_vec,up);
  3430. //Transform light_space_basis2;
  3431. //light_space_basis2.set_look_at(pos,light_vec-pos,up);
  3432. //light_space_basis2.affine_invert();
  3433. //one possibility for a simple perspective transformation matrix
  3434. //with the two parameters n(near) and f(far) in y direction
  3435. CameraMatrix lisp_matrix;
  3436. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  3437. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  3438. lisp_matrix.matrix[1][3]=1;
  3439. lisp_matrix.matrix[3][3]=0;
  3440. CameraMatrix projection = lisp_matrix * light_space_basis2;
  3441. //CameraMatrix projection = light_space_basis2 * lisp_matrix;
  3442. AABB proj_space_aabb;
  3443. float max_d,min_d;
  3444. {
  3445. AABB proj_space_pointcloud_aabb;
  3446. AABB proj_space_camera_aabb;
  3447. //xform pointcloud
  3448. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  3449. for(int i=0;i<caster_pointcloud_size;i++) {
  3450. Vector3 p = projection.xform(caster_pointcloud_ptr[i]);
  3451. if (i==0) {
  3452. proj_space_pointcloud_aabb.pos=p;
  3453. } else {
  3454. proj_space_pointcloud_aabb.expand_to(p);
  3455. }
  3456. }
  3457. for(int i=0;i<camera_pointcloud.size();i++) {
  3458. Vector3 p = projection.xform(camera_pointcloud[i]);
  3459. if (i==0) {
  3460. proj_space_camera_aabb.pos=p;
  3461. } else {
  3462. proj_space_camera_aabb.expand_to(p);
  3463. }
  3464. }
  3465. //proj_space_aabb=proj_space_pointcloud_aabb.intersection_with(proj_space_camera_aabb);
  3466. proj_space_aabb=proj_space_pointcloud_aabb;
  3467. }
  3468. projection.scale_translate_to_fit(proj_space_aabb);
  3469. projection=projection * lisp_matrix;
  3470. CameraMatrix scale;
  3471. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  3472. projection=scale * projection;
  3473. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection , light_space_basis2.inverse() );
  3474. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3475. for(int i=0;i<caster_count;i++) {
  3476. Instance *instance = instance_shadow_cull_result[i];
  3477. if (!instance->visible || !instance->cast_shadows)
  3478. continue;
  3479. _instance_draw(instance);
  3480. }
  3481. rasterizer->end_shadow_map();
  3482. }
  3483. #else
  3484. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3485. /* STEP 1: GENERATE LIGHT TRANSFORM */
  3486. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  3487. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  3488. float viewdot = Math::absf(light_vec.dot(view_vec));
  3489. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  3490. Transform light_transform;
  3491. light_transform.set_look_at(Vector3(),light_vec,up);
  3492. /* STEP 2: GENERATE WORDLSPACE PLANES AND VECTORS*/
  3493. float range_min=0.01; //p_cull_range.min
  3494. float range_max=20;//p_cull_range.max;
  3495. Vector<Vector3> camera_endpoints=_camera_generate_endpoints(p_light,p_camera,range_min,range_max); // frustum plane endpoints
  3496. ERR_FAIL_COND(camera_endpoints.empty());
  3497. // obtain the light frustm ranges (given endpoints)
  3498. Vector3 light_x_vec=light_transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  3499. Vector3 light_y_vec=light_transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  3500. Vector3 light_z_vec=light_transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  3501. Vector3 light_axis_max;
  3502. Vector3 light_axis_min;
  3503. for(int j=0;j<8;j++) {
  3504. float d_x=light_x_vec.dot(camera_endpoints[j]);
  3505. float d_y=light_y_vec.dot(camera_endpoints[j]);
  3506. float d_z=light_z_vec.dot(camera_endpoints[j]);
  3507. if (j==0 || d_x<light_axis_min.x)
  3508. light_axis_min.x=d_x;
  3509. if (j==0 || d_x>light_axis_max.x)
  3510. light_axis_max.x=d_x;
  3511. if (j==0 || d_y<light_axis_min.y)
  3512. light_axis_min.y=d_y;
  3513. if (j==0 || d_y>light_axis_max.y)
  3514. light_axis_max.y=d_y;
  3515. if (j==0 || d_z<light_axis_min.z)
  3516. light_axis_min.z=d_z;
  3517. if (j==0 || d_z>light_axis_max.z)
  3518. light_axis_max.z=d_z;
  3519. }
  3520. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  3521. Vector<Plane> light_cull_planes;
  3522. light_cull_planes.resize(6);
  3523. //right/left
  3524. light_cull_planes[0]=Plane( light_x_vec, light_axis_max.x );
  3525. light_cull_planes[1]=Plane( -light_x_vec, -light_axis_min.x );
  3526. //top/bottom
  3527. light_cull_planes[2]=Plane( light_y_vec, light_axis_max.y );
  3528. light_cull_planes[3]=Plane( -light_y_vec, -light_axis_min.y );
  3529. //near/far
  3530. light_cull_planes[4]=Plane( light_z_vec, light_axis_max.z+1e6 );
  3531. 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
  3532. /* STEP 3: CULL CASTERS */
  3533. int caster_count = p_scenario->octree.cull_convex(light_cull_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3534. /* STEP 4: ADJUST FAR Z PLANE */
  3535. float caster_max_z=1e-1;
  3536. for(int i=0;i<caster_count;i++) {
  3537. Instance *ins=instance_shadow_cull_result[i];
  3538. if (!ins->visible || !ins->cast_shadows)
  3539. continue;
  3540. //@TODO optimize using support mapping
  3541. for(int j=0;j<8;j++) {
  3542. Vector3 v=ins->data.transform.xform(ins->aabb.get_endpoint(j));
  3543. float d = light_z_vec.dot(v);
  3544. if (d>caster_max_z)
  3545. caster_max_z=d;
  3546. }
  3547. }
  3548. float expand = caster_max_z-light_axis_max.z;
  3549. if (expand<0)
  3550. expand=0;
  3551. light_axis_max.z=MAX(caster_max_z,light_axis_max.z);
  3552. /* STEP 5: CREATE ORTHOGONAL PROJECTION */
  3553. CameraMatrix light_projection;
  3554. real_t half_x = (light_axis_max.x-light_axis_min.x) * 0.5;
  3555. real_t half_y = (light_axis_max.y-light_axis_min.y) * 0.5;
  3556. light_projection.set_orthogonal( -half_x, half_x,half_y, -half_y, 0, (light_axis_max.z-light_axis_min.z) );
  3557. 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;
  3558. if (/*false &&*/ viewdot<0.96) {
  3559. float lvdp = light_vec.dot(view_vec);
  3560. float near_dist=1.0;
  3561. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  3562. //use the formulas of the paper to get n (and f)
  3563. float factor = 1.0/sin_gamma;
  3564. float z_n = factor*near_dist; //often 1
  3565. float d = Math::abs(light_axis_max.y-light_axis_min.y); //perspective transform depth //light space y extents
  3566. float z_f = z_n + d*sin_gamma;
  3567. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  3568. float f = n+d;
  3569. CameraMatrix lisp_matrix;
  3570. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  3571. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  3572. lisp_matrix.matrix[1][3]=1;
  3573. lisp_matrix.matrix[3][3]=0;
  3574. Vector3 pos = p_camera->transform.origin - up*(n-near_dist);
  3575. CameraMatrix world2light = _lispm_look(pos,light_vec,up);
  3576. CameraMatrix projection = lisp_matrix * world2light;
  3577. AABB projection_bounds;
  3578. for(int i=0;i<camera_endpoints.size();i++) {
  3579. Vector3 p=camera_endpoints[i];
  3580. if (i==0)
  3581. projection_bounds.pos=projection.xform(p);
  3582. else
  3583. projection_bounds.expand_to(projection.xform(p));
  3584. projection_bounds.expand_to(projection.xform(p+light_vec*-expand));
  3585. }
  3586. CameraMatrix scaletrans;
  3587. scaletrans.scale_translate_to_fit(projection_bounds);
  3588. projection=scaletrans * lisp_matrix;
  3589. CameraMatrix scale;
  3590. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  3591. projection=scale * projection;
  3592. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection, world2light.inverse(), viewdot);
  3593. } else {
  3594. //orthogonal
  3595. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, light_projection , light_transform, viewdot);
  3596. }
  3597. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3598. for(int i=0;i<caster_count;i++) {
  3599. Instance *instance = instance_shadow_cull_result[i];
  3600. if (!instance->visible || !instance->cast_shadows)
  3601. continue;
  3602. _instance_draw(instance);
  3603. }
  3604. rasterizer->end_shadow_map();
  3605. }
  3606. #endif
  3607. void VisualServerRaster::_light_instance_update_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3608. if (!rasterizer->shadow_allocate_near( p_light->light_info->instance ))
  3609. return; // shadow could not be updated
  3610. /* VisualServerRaster supports for many shadow techniques, using the one the rasterizer requests */
  3611. Rasterizer::ShadowType shadow_type = rasterizer->light_instance_get_shadow_type(p_light->light_info->instance);
  3612. switch(shadow_type) {
  3613. case Rasterizer::SHADOW_SIMPLE: {
  3614. /* SPOT SHADOW */
  3615. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3616. //using this one ensures that raster deferred will have it
  3617. float far = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3618. float angle = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_SPOT_ANGLE );
  3619. CameraMatrix cm;
  3620. cm.set_perspective( angle*2.0, 1.0, 0.001, far );
  3621. Vector<Plane> planes = cm.get_projection_planes(p_light->data.transform);
  3622. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3623. for (int i=0;i<cull_count;i++) {
  3624. Instance *instance = instance_shadow_cull_result[i];
  3625. if (!instance->visible || !instance->cast_shadows)
  3626. continue;
  3627. _instance_draw(instance);
  3628. }
  3629. rasterizer->end_shadow_map();
  3630. } break;
  3631. case Rasterizer::SHADOW_DUAL_PARABOLOID: {
  3632. /* OMNI SHADOW */
  3633. int passes = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  3634. if (passes==2) {
  3635. for(int i=0;i<2;i++) {
  3636. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  3637. //using this one ensures that raster deferred will have it
  3638. float radius = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3639. float z =i==0?-1:1;
  3640. Vector<Plane> planes;
  3641. planes.resize(5);
  3642. planes[0]=p_light->data.transform.xform(Plane(Vector3(0,0,z),radius));
  3643. planes[1]=p_light->data.transform.xform(Plane(Vector3(1,0,z).normalized(),radius));
  3644. planes[2]=p_light->data.transform.xform(Plane(Vector3(-1,0,z).normalized(),radius));
  3645. planes[3]=p_light->data.transform.xform(Plane(Vector3(0,1,z).normalized(),radius));
  3646. planes[4]=p_light->data.transform.xform(Plane(Vector3(0,-1,z).normalized(),radius));
  3647. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3648. for (int j=0;j<cull_count;j++) {
  3649. Instance *instance = instance_shadow_cull_result[j];
  3650. if (!instance->visible || !instance->cast_shadows)
  3651. continue;
  3652. _instance_draw(instance);
  3653. }
  3654. rasterizer->end_shadow_map();
  3655. }
  3656. } else if (passes==1) {
  3657. //one go
  3658. }
  3659. } break;
  3660. case Rasterizer::SHADOW_CUBE: {
  3661. // todo
  3662. } break;
  3663. case Rasterizer::SHADOW_ORTHOGONAL: {
  3664. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3665. } break;
  3666. case Rasterizer::SHADOW_PSSM: {
  3667. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3668. } break;
  3669. case Rasterizer::SHADOW_PSM: {
  3670. _light_instance_update_lispsm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3671. // todo
  3672. } break;
  3673. default: {}
  3674. }
  3675. }
  3676. void VisualServerRaster::_portal_disconnect(Instance *p_portal,bool p_cleanup) {
  3677. if (p_portal->portal_info->connected) {
  3678. //disconnect first
  3679. p_portal->portal_info->connected->portal_info->connected=NULL;
  3680. p_portal->portal_info->connected=NULL;
  3681. }
  3682. if (p_portal->room && p_portal->room->room) {
  3683. if (p_cleanup) {
  3684. p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3685. //p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3686. } else {
  3687. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3688. }
  3689. }
  3690. }
  3691. void VisualServerRaster::_instance_validate_autorooms(Instance *p_geometry) {
  3692. if (p_geometry->auto_rooms.size()==0)
  3693. return;
  3694. p_geometry->valid_auto_rooms.clear();
  3695. int point_count = aabb_random_points.size();
  3696. const Vector3 * src_points = &aabb_random_points[0];
  3697. for(Set<Instance*>::Element *E=p_geometry->valid_auto_rooms.front();E;E=E->next()) {
  3698. Instance *room = E->get();
  3699. Vector3 *dst_points=&transformed_aabb_random_points[0];
  3700. //generate points
  3701. for(int i=0;i<point_count;i++) {
  3702. 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));
  3703. }
  3704. int pass = room->room_info->room->bounds.get_points_inside(dst_points,point_count);
  3705. float ratio = (float)pass / point_count;
  3706. if (ratio>0.5) // should make some constant
  3707. p_geometry->valid_auto_rooms.insert(room);
  3708. }
  3709. }
  3710. void VisualServerRaster::_portal_attempt_connect(Instance *p_portal) {
  3711. _portal_disconnect(p_portal);
  3712. Vector3 A_norm = p_portal->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3713. Plane A_plane( p_portal->data.transform.origin, A_norm );
  3714. float A_surface = p_portal->portal_info->portal->bounds.get_area();
  3715. if (A_surface==0)
  3716. return; //wtf
  3717. Instance *found=NULL;
  3718. Transform affine_inverse = p_portal->data.transform.affine_inverse();
  3719. for(Set<Instance*>::Element *E=p_portal->portal_info->candidate_set.front();E;E=E->next()) {
  3720. Instance *B = E->get();
  3721. if (B->portal_info->connected)
  3722. continue; // in use
  3723. Vector3 B_norm = B->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3724. // check that they are in front of another
  3725. float dot = A_norm.dot(-B_norm);
  3726. if (dot<0.707) // 45 degrees, TODO unharcode this
  3727. continue;
  3728. // check the max distance to the other portal
  3729. bool valid=true;
  3730. Rect2 local_bounds;
  3731. for(int i=0;i<B->portal_info->portal->shape.size();i++) {
  3732. Point2 point2 = B->portal_info->portal->shape[i];
  3733. Vector3 point = B->data.transform.xform( Vector3( point2.x, point2.y, 0 ) );
  3734. float dist = Math::abs(A_plane.distance_to(point));
  3735. if (
  3736. dist>p_portal->portal_info->portal->connect_range ||
  3737. dist>B->portal_info->portal->connect_range ) {
  3738. valid=false;
  3739. break;
  3740. }
  3741. Vector3 point_local = affine_inverse.xform(A_plane.project(point));
  3742. point2 = Point2(point_local.x,point_local.y);
  3743. if (i==0)
  3744. local_bounds.pos=point2;
  3745. else
  3746. local_bounds.expand_to(point2);
  3747. }
  3748. if (!valid)
  3749. continue;
  3750. float B_surface = B->portal_info->portal->bounds.get_area();
  3751. if (B_surface==0)
  3752. continue; //wtf
  3753. float clip_area = p_portal->portal_info->portal->bounds.clip(local_bounds).get_area();
  3754. //check that most of the area is shared
  3755. if ( (clip_area/A_surface) < 0.5 || (clip_area/B_surface) < 0.5) // TODO change for something else
  3756. continue;
  3757. found=B;
  3758. break;
  3759. }
  3760. if (!found) {
  3761. if (p_portal->room && p_portal->room->room) {
  3762. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3763. }
  3764. return;
  3765. }
  3766. p_portal->portal_info->connected=found;
  3767. found->portal_info->connected=p_portal;
  3768. }
  3769. void* VisualServerRaster::instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) {
  3770. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3771. Instance *A = p_A;
  3772. Instance *B = p_B;
  3773. if (A->base_type==INSTANCE_PORTAL) {
  3774. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  3775. A->portal_info->candidate_set.insert(B);
  3776. B->portal_info->candidate_set.insert(A);
  3777. self->_portal_attempt_connect(A);
  3778. //attempt to conncet portal A (will go through B anyway)
  3779. //this is a little hackish, but works fine in practice
  3780. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  3781. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  3782. SWAP(A,B);
  3783. }
  3784. ERR_FAIL_COND_V(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER,NULL);
  3785. B->baked_light_sampler_info->baked_lights.insert(A);
  3786. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3787. if (B->base_type==INSTANCE_ROOM)
  3788. SWAP(A,B);
  3789. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  3790. B->auto_rooms.insert(A);
  3791. A->room_info->owned_autoroom_geometry.insert(B);
  3792. self->_instance_validate_autorooms(B);
  3793. } else {
  3794. if (B->base_type==INSTANCE_LIGHT) {
  3795. SWAP(A,B);
  3796. } else if (A->base_type!=INSTANCE_LIGHT) {
  3797. return NULL;
  3798. }
  3799. A->light_info->affected.insert(B);
  3800. B->lights.insert(A);
  3801. B->light_cache_dirty=true;
  3802. }
  3803. return NULL;
  3804. }
  3805. void VisualServerRaster::instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void*) {
  3806. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3807. Instance *A = p_A;
  3808. Instance *B = p_B;
  3809. if (A->base_type==INSTANCE_PORTAL) {
  3810. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  3811. A->portal_info->candidate_set.erase(B);
  3812. B->portal_info->candidate_set.erase(A);
  3813. //after disconnecting them, see if they can connect again
  3814. self->_portal_attempt_connect(A);
  3815. self->_portal_attempt_connect(B);
  3816. } else if (A->base_type==INSTANCE_BAKED_LIGHT || B->base_type==INSTANCE_BAKED_LIGHT) {
  3817. if (B->base_type==INSTANCE_BAKED_LIGHT) {
  3818. SWAP(A,B);
  3819. }
  3820. ERR_FAIL_COND(B->base_type!=INSTANCE_BAKED_LIGHT_SAMPLER);
  3821. B->baked_light_sampler_info->baked_lights.erase(A);
  3822. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3823. if (B->base_type==INSTANCE_ROOM)
  3824. SWAP(A,B);
  3825. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  3826. B->auto_rooms.erase(A);
  3827. B->valid_auto_rooms.erase(A);
  3828. A->room_info->owned_autoroom_geometry.erase(B);
  3829. }else {
  3830. if (B->base_type==INSTANCE_LIGHT) {
  3831. SWAP(A,B);
  3832. } else if (A->base_type!=INSTANCE_LIGHT) {
  3833. return;
  3834. }
  3835. A->light_info->affected.erase(B);
  3836. B->lights.erase(A);
  3837. B->light_cache_dirty=true;
  3838. }
  3839. }
  3840. bool VisualServerRaster::_test_portal_cull(Camera *p_camera, Instance *p_from_portal, Instance *p_to_portal) {
  3841. int src_point_count=p_from_portal->portal_info->transformed_point_cache.size();
  3842. int dst_point_count=p_to_portal->portal_info->transformed_point_cache.size();
  3843. if (src_point_count<2 || dst_point_count<2)
  3844. return false;
  3845. const Vector3 *src_points=&p_from_portal->portal_info->transformed_point_cache[0];
  3846. const Vector3 *dst_points=&p_to_portal->portal_info->transformed_point_cache[0];
  3847. bool outside=false;
  3848. bool clockwise = !p_from_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin);
  3849. for(int i=0;i<src_point_count;i++) {
  3850. const Vector3& point_prev = src_points[i?(i-1):(src_point_count-1)];
  3851. const Vector3& point = src_points[i];
  3852. Plane p = clockwise?Plane(p_camera->transform.origin,point,point_prev):Plane(p_camera->transform.origin,point_prev,point);
  3853. bool all_over=true;
  3854. for(int j=0;j<dst_point_count;j++) {
  3855. if (!p.is_point_over(dst_points[j])) {
  3856. all_over=false;
  3857. break;
  3858. }
  3859. }
  3860. if (all_over) {
  3861. outside=true;
  3862. break;
  3863. }
  3864. }
  3865. return !outside;
  3866. }
  3867. void VisualServerRaster::_cull_portal(Camera *p_camera, Instance *p_portal,Instance *p_from_portal) {
  3868. ERR_FAIL_COND(!p_portal->scenario); //scenario outside
  3869. Instance *portal = p_portal;
  3870. if (!portal->room) {
  3871. return; //portals need all to belong to a room, it may be unconfigured yet
  3872. } else if (portal->last_render_pass!=render_pass) {
  3873. return; //invalid portal, ignore
  3874. } else if (portal->portal_info->last_visited_pass==render_pass) {
  3875. return; //portal already visited
  3876. } else if (portal==p_from_portal) {
  3877. return; // came from this portal, don't even bother testing
  3878. }
  3879. /* TEST DISABLE DISTANCE */
  3880. float disable_distance = p_portal->portal_info->portal->disable_distance;
  3881. if (disable_distance) {
  3882. //has disable distance..
  3883. float distance = p_camera->transform.origin.distance_to(portal->data.transform.origin);
  3884. if (disable_distance < distance) {
  3885. return;
  3886. }
  3887. }
  3888. /* TEST PORTAL NOT FACING OPTIMIZATION */
  3889. if (p_portal->portal_info->connected) {
  3890. //connected portal means, it must face against the camera to be seen
  3891. if (p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3892. return;
  3893. }
  3894. } else {
  3895. //disconencted portals (go from room to parent room or exterior) must face towards the canera
  3896. if (!p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3897. return;
  3898. }
  3899. }
  3900. if (p_from_portal && !_test_portal_cull(p_camera, p_from_portal, portal)) {
  3901. return; // portal not visible (culled)
  3902. }
  3903. portal->portal_info->last_visited_pass=render_pass;
  3904. if (portal->portal_info->connected) {
  3905. //interior<->interior portal
  3906. Instance *to_room = portal->portal_info->connected->room;
  3907. if (!to_room) {
  3908. return; //wtf.. oh well, connected to a roomless (invalid) portal
  3909. }
  3910. _cull_room(p_camera, to_room, portal->portal_info->connected);
  3911. } else {
  3912. //to exterior/to parent roomportal
  3913. Instance *parent_room = portal->room->room;
  3914. _cull_room(p_camera, parent_room, portal);
  3915. }
  3916. }
  3917. void VisualServerRaster::_cull_room(Camera *p_camera, Instance *p_room,Instance *p_from_portal) {
  3918. if (p_room==NULL) {
  3919. //exterior
  3920. exterior_visited=true;
  3921. for(int i=0;i<exterior_portal_cull_count;i++) {
  3922. _cull_portal(p_camera, exterior_portal_cull_result[i],p_from_portal);
  3923. }
  3924. } else {
  3925. ERR_FAIL_COND(!p_room->scenario);
  3926. if (p_room->last_render_pass!=render_pass)
  3927. return; //this room is invalid
  3928. //interior
  3929. //first of all, validate the room
  3930. p_room->room_info->last_visited_pass=render_pass;
  3931. //see about going around portals
  3932. if (!p_room->room_info->room->occlude_exterior)
  3933. exterior_visited=true;
  3934. for(List<Instance*>::Element * E=p_room->room_info->owned_portal_instances.front();E;E=E->next()) {
  3935. _cull_portal(p_camera, E->get(),p_from_portal);
  3936. }
  3937. for(Set<Instance*>::Element * E=p_room->room_info->disconnected_child_portals.front();E;E=E->next()) {
  3938. _cull_portal(p_camera, E->get(),p_from_portal);
  3939. }
  3940. }
  3941. }
  3942. void VisualServerRaster::_process_sampled_light(const Transform& p_camera,Instance *p_sampled_light,bool p_linear_colorspace) {
  3943. BakedLightSampler *sampler_opts = p_sampled_light->baked_light_sampler_info->sampler;
  3944. int res = sampler_opts->resolution;
  3945. int dp_size = res*res*2;
  3946. Color * dp_map = (Color*)alloca( sizeof(Color)*dp_size); //allocate the dual parabolloid colors
  3947. Vector3 * dp_normals = (Vector3*)alloca( sizeof(Vector3)*dp_size); //allocate the dual parabolloid normals
  3948. const Vector3 * dp_src_normals = p_sampled_light->baked_light_sampler_info->sampler->dp_cache.ptr();
  3949. if (!p_sampled_light->baked_light_sampler_info->sampled_light.is_valid() || p_sampled_light->baked_light_sampler_info->resolution!=sampler_opts->resolution) {
  3950. if (p_sampled_light->baked_light_sampler_info->sampled_light.is_valid()) {
  3951. rasterizer->free(p_sampled_light->baked_light_sampler_info->sampled_light);
  3952. }
  3953. p_sampled_light->baked_light_sampler_info->resolution=sampler_opts->resolution;
  3954. p_sampled_light->baked_light_sampler_info->sampled_light=rasterizer->sampled_light_dp_create(sampler_opts->resolution,sampler_opts->resolution*2);
  3955. }
  3956. zeromem(dp_map,sizeof(Color)*dp_size);
  3957. bool valid=false;
  3958. int samples=0;
  3959. for(Set<Instance*>::Element *E=p_sampled_light->baked_light_sampler_info->baked_lights.front();E;E=E->next()) {
  3960. Instance *bl = E->get();
  3961. if (bl->baked_light_info->baked_light->sampler.size()==0)
  3962. continue; //not usable
  3963. Matrix3 norm_xform = bl->baked_light_info->affine_inverse.basis;//.inverse();
  3964. for(int i=0;i<dp_size;i++) {
  3965. dp_normals[i]=norm_xform.xform(dp_src_normals[i]).normalized();
  3966. }
  3967. //normals in place
  3968. //sample octree
  3969. float r = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_RADIUS];
  3970. float att = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_ATTENUATION];
  3971. float str = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_STRENGTH];
  3972. Vector3 s = p_sampled_light->data.transform.basis.get_scale();
  3973. r*=MAX(MAX(s.x,s.y),s.z);
  3974. 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)));
  3975. //ok got octree local AABB
  3976. DVector<int>::Read rp = bl->baked_light_info->baked_light->sampler.read();
  3977. const int *rptr = rp.ptr();
  3978. int first = rptr[1];
  3979. int depth = rptr[2];
  3980. bool islinear = rptr[3]&1;
  3981. depth+=1;
  3982. AABB aabb;
  3983. aabb.pos.x=decode_float((const uint8_t*)&rptr[4]);
  3984. aabb.pos.y=decode_float((const uint8_t*)&rptr[5]);
  3985. aabb.pos.z=decode_float((const uint8_t*)&rptr[6]);
  3986. aabb.size.x=decode_float((const uint8_t*)&rptr[7]);
  3987. aabb.size.y=decode_float((const uint8_t*)&rptr[8]);
  3988. aabb.size.z=decode_float((const uint8_t*)&rptr[9]);
  3989. uint32_t *stack=(uint32_t*)alloca(depth*sizeof(uint32_t));
  3990. int *stack_ptr=(int*)alloca(depth*sizeof(int));
  3991. AABB *aabb_stack=(AABB*)alloca(depth*sizeof(AABB));
  3992. stack[0]=0;
  3993. stack_ptr[0]=first;
  3994. aabb_stack[0]=aabb;
  3995. Vector3 center = sample_aabb.pos + sample_aabb.size * 0.5;
  3996. int stack_pos=0;
  3997. Color max_col;
  3998. //int reso = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO];
  3999. int lalimit = sample_aabb.get_longest_axis_index();
  4000. float limit = sampler_opts->params[VS::BAKED_LIGHT_SAMPLER_DETAIL_RATIO]*sample_aabb.size[lalimit];
  4001. while(true) {
  4002. bool leaf = (rptr[ stack_ptr[stack_pos] ]>>16)==0;
  4003. if (aabb_stack[stack_pos].size[lalimit]<limit) {
  4004. leaf=true;
  4005. }
  4006. if (leaf) {
  4007. Vector3 from = aabb_stack[stack_pos].pos + aabb_stack[stack_pos].size * 0.5;
  4008. Vector3 norm = (from-center).normalized();
  4009. Color col;
  4010. col.r = ((rptr[ stack_ptr[stack_pos] ]&0xFFFF)/256.0);
  4011. col.g = ((rptr[ stack_ptr[stack_pos]+1 ]>>16)/256.0);
  4012. col.b = ((rptr[ stack_ptr[stack_pos]+1 ]&0xFFFF)/256.0);
  4013. max_col.r = MAX(max_col.r,col.r);
  4014. max_col.g = MAX(max_col.g,col.g);
  4015. max_col.b = MAX(max_col.b,col.b);
  4016. if (!islinear && p_linear_colorspace) {
  4017. col=col.to_linear();
  4018. }
  4019. float distance;
  4020. if (aabb_stack[stack_pos].has_point(center)) {
  4021. distance=0;
  4022. } else {
  4023. Vector3 support = aabb_stack[stack_pos].get_support(norm);
  4024. distance = Math::absf(norm.dot(support)-norm.dot(center));
  4025. }
  4026. if (distance>r)
  4027. distance=r;
  4028. float mult = Math::pow(1.0-distance/r,att)*str;
  4029. if (mult>0) {
  4030. col.r*=mult;
  4031. col.g*=mult;
  4032. col.b*=mult;
  4033. for(int i=0;i<dp_size;i++) {
  4034. float mult2 = norm.dot(dp_normals[i]);
  4035. if (mult2<0)
  4036. mult2=0;
  4037. Color col2(col.r*mult2,col.g*mult2,col.b*mult2,1.0);
  4038. dp_map[i].r=MAX(dp_map[i].r,col2.r);
  4039. dp_map[i].g=MAX(dp_map[i].g,col2.g);
  4040. dp_map[i].b=MAX(dp_map[i].b,col2.b);
  4041. }
  4042. }
  4043. samples++;
  4044. //nothing is valid unless you hit a leaf
  4045. valid=true;
  4046. stack_pos--;
  4047. } else if ((stack[stack_pos]&0xFF)<8) {
  4048. int i = stack[stack_pos]&0xFF;
  4049. int base = (stack[stack_pos]>>8);
  4050. if (!((rptr[ stack_ptr[stack_pos] ]>>16)&(1<<i))) {
  4051. //no bit, no test
  4052. stack[stack_pos]=(base<<8)+(i+1);
  4053. continue;
  4054. }
  4055. stack[stack_pos]=((base+1)<<8)+(i+1);
  4056. AABB child_aabb = aabb_stack[stack_pos];
  4057. child_aabb.size*=0.5;
  4058. if (i&1)
  4059. child_aabb.pos.x+=child_aabb.size.x;
  4060. if (i&2)
  4061. child_aabb.pos.y+=child_aabb.size.y;
  4062. if (i&4)
  4063. child_aabb.pos.z+=child_aabb.size.z;
  4064. if (!child_aabb.intersects(sample_aabb)) {
  4065. continue;
  4066. }
  4067. if (child_aabb.encloses(sample_aabb)) {
  4068. stack[stack_pos]=(base<<8)|8; //don't test the rest
  4069. }
  4070. stack_pos++;
  4071. ERR_FAIL_COND(stack_pos>=depth);
  4072. stack[stack_pos]=0;
  4073. stack_ptr[stack_pos]=rptr[ stack_ptr[stack_pos-1]+2+base ];
  4074. aabb_stack[stack_pos]=child_aabb;
  4075. } else {
  4076. stack_pos--;
  4077. if (stack_pos<0)
  4078. break;
  4079. }
  4080. }
  4081. }
  4082. //print_line("samples "+itos(samples) );
  4083. if (valid) {
  4084. for(int i=0;i<res;i++) {
  4085. //average seams to avoid aliasing
  4086. {
  4087. //top
  4088. int ofs1 = i;
  4089. int ofs2 = dp_size-res+i;
  4090. Color avg(
  4091. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4092. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4093. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4094. 1.0
  4095. );
  4096. dp_map[ofs1]=avg;
  4097. dp_map[ofs2]=avg;
  4098. }
  4099. {
  4100. //bottom
  4101. int ofs1 = res*res-res+i;
  4102. int ofs2 = res*res+i;
  4103. Color avg(
  4104. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4105. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4106. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4107. 1.0
  4108. );
  4109. dp_map[ofs1]=avg;
  4110. dp_map[ofs2]=avg;
  4111. }
  4112. {
  4113. //left
  4114. int ofs1 = i*res;
  4115. int ofs2 = res*res+(res-i-1)*res;
  4116. Color avg(
  4117. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4118. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4119. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4120. 1.0
  4121. );
  4122. dp_map[ofs1]=avg;
  4123. dp_map[ofs2]=avg;
  4124. }
  4125. {
  4126. //right
  4127. int ofs1 = i*res+(res-1);
  4128. int ofs2 = res*res+(res-i-1)*res+(res-1);
  4129. Color avg(
  4130. (dp_map[ofs1].r+dp_map[ofs2].r)*0.5,
  4131. (dp_map[ofs1].g+dp_map[ofs2].g)*0.5,
  4132. (dp_map[ofs1].b+dp_map[ofs2].b)*0.5,
  4133. 1.0
  4134. );
  4135. dp_map[ofs1]=avg;
  4136. dp_map[ofs2]=avg;
  4137. }
  4138. }
  4139. rasterizer->sampled_light_dp_update(p_sampled_light->baked_light_sampler_info->sampled_light,dp_map,1.0);
  4140. for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) {
  4141. F->get()->data.sampled_light=p_sampled_light->baked_light_sampler_info->sampled_light;
  4142. }
  4143. } else {
  4144. for(Set<Instance*>::Element *F=p_sampled_light->baked_light_sampler_info->owned_instances.front();F;F=F->next()) {
  4145. F->get()->data.sampled_light=RID(); //do not use because nothing close
  4146. }
  4147. }
  4148. /*
  4149. highp vec3 vtx = vertex_interp;
  4150. vtx.z*=dual_paraboloid.y; //side to affect
  4151. vtx.z+=0.01;
  4152. dp_clip=vtx.z;
  4153. highp float len=length( vtx );
  4154. vtx=normalize(vtx);
  4155. vtx.xy/=1.0+vtx.z;
  4156. vtx.z = len*dual_paraboloid.x; // it's a reciprocal(len - z_near) / (z_far - z_near);
  4157. vtx+=normalize(vtx)*0.025;
  4158. vtx.z = vtx.z * 2.0 - 1.0; // fit to clipspace
  4159. vertex_interp=vtx;
  4160. */
  4161. }
  4162. void VisualServerRaster::_render_no_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  4163. RID environment;
  4164. if (p_scenario->environment.is_valid())
  4165. environment=p_scenario->environment;
  4166. else
  4167. environment=p_scenario->fallback_environment;
  4168. rasterizer->set_camera(Transform(),CameraMatrix());
  4169. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  4170. rasterizer->set_viewport(viewport_rect);
  4171. rasterizer->end_scene();
  4172. }
  4173. void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  4174. uint64_t t = OS::get_singleton()->get_ticks_usec();
  4175. render_pass++;
  4176. uint32_t camera_layer_mask=p_camera->visible_layers;
  4177. /* STEP 1 - SETUP CAMERA */
  4178. CameraMatrix camera_matrix;
  4179. switch(p_camera->type) {
  4180. case Camera::ORTHOGONAL: {
  4181. camera_matrix.set_orthogonal(
  4182. p_camera->size,
  4183. viewport_rect.width / (float)viewport_rect.height,
  4184. p_camera->znear,
  4185. p_camera->zfar,
  4186. p_camera->vaspect
  4187. );
  4188. } break;
  4189. case Camera::PERSPECTIVE: {
  4190. camera_matrix.set_perspective(
  4191. p_camera->fov,
  4192. viewport_rect.width / (float)viewport_rect.height,
  4193. p_camera->znear,
  4194. p_camera->zfar,
  4195. p_camera->vaspect
  4196. );
  4197. } break;
  4198. }
  4199. rasterizer->set_camera(p_camera->transform, camera_matrix);
  4200. Vector<Plane> planes = camera_matrix.get_projection_planes(p_camera->transform);
  4201. CullRange cull_range; // cull range is used for PSSM, and having an idea of the rendering depth
  4202. cull_range.nearp=Plane(p_camera->transform.origin,-p_camera->transform.basis.get_axis(2).normalized());
  4203. cull_range.z_near=camera_matrix.get_z_near();
  4204. cull_range.z_far=camera_matrix.get_z_far();
  4205. cull_range.min=cull_range.z_far;
  4206. cull_range.max=cull_range.z_near;
  4207. /* STEP 2 - CULL */
  4208. int cull_count = p_scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL);
  4209. light_cull_count=0;
  4210. light_samplers_culled=0;
  4211. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  4212. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  4213. // print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  4214. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  4215. */
  4216. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  4217. // compute portals
  4218. exterior_visited=false;
  4219. exterior_portal_cull_count=0;
  4220. if (room_cull_enabled) {
  4221. for(int i=0;i<cull_count;i++) {
  4222. Instance *ins = instance_cull_result[i];
  4223. ins->last_render_pass=render_pass;
  4224. if (ins->base_type!=INSTANCE_PORTAL)
  4225. continue;
  4226. if (ins->room)
  4227. continue;
  4228. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  4229. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  4230. }
  4231. 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));
  4232. Set<Instance*> current_rooms;
  4233. Set<Instance*> portal_rooms;
  4234. //add to set
  4235. for(int i=0;i<room_cull_count;i++) {
  4236. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  4237. current_rooms.insert(room_cull_result[i]);
  4238. }
  4239. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  4240. //assume inside that room if also inside the portal..
  4241. if (room_cull_result[i]->room) {
  4242. portal_rooms.insert(room_cull_result[i]->room);
  4243. }
  4244. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  4245. room_cull_count--;
  4246. i--;
  4247. }
  4248. }
  4249. //remove from set if it has a parent room or BSP doesn't contain
  4250. for(int i=0;i<room_cull_count;i++) {
  4251. Instance *r = room_cull_result[i];
  4252. //check inside BSP
  4253. Vector3 room_local_point = r->room_info->affine_inverse.xform( p_camera->transform.origin );
  4254. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  4255. current_rooms.erase(r);
  4256. continue;
  4257. }
  4258. //check parent
  4259. while (r->room) {// has parent room
  4260. current_rooms.erase(r);
  4261. r=r->room;
  4262. }
  4263. }
  4264. if (current_rooms.size()) {
  4265. //camera is inside a room
  4266. // go through rooms
  4267. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  4268. _cull_room(p_camera,E->get());
  4269. }
  4270. } else {
  4271. //start from exterior
  4272. _cull_room(p_camera,NULL);
  4273. }
  4274. }
  4275. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  4276. for(int i=0;i<cull_count;i++) {
  4277. Instance *ins = instance_cull_result[i];
  4278. bool keep=false;
  4279. if ((camera_layer_mask&ins->layer_mask)==0) {
  4280. //failure
  4281. } else if (ins->base_type==INSTANCE_LIGHT) {
  4282. if (light_cull_count<MAX_LIGHTS_CULLED) {
  4283. light_cull_result[light_cull_count++]=ins;
  4284. // rasterizer->light_instance_set_active_hint(ins->light_info->instance);
  4285. {
  4286. //compute distance to camera using aabb support
  4287. Vector3 n = ins->data.transform.basis.xform_inv(cull_range.nearp.normal).normalized();
  4288. Vector3 s = ins->data.transform.xform(ins->aabb.get_support(n));
  4289. ins->light_info->dtc=cull_range.nearp.distance_to(s);
  4290. }
  4291. }
  4292. } else if ((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK && ins->visible) {
  4293. bool discarded=false;
  4294. if (ins->draw_range_end>0) {
  4295. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  4296. if (d<0)
  4297. d=0;
  4298. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  4299. }
  4300. if (!discarded) {
  4301. // test if this geometry should be visible
  4302. if (room_cull_enabled) {
  4303. if (ins->visible_in_all_rooms) {
  4304. keep=true;
  4305. } else if (ins->room) {
  4306. if (ins->room->room_info->last_visited_pass==render_pass)
  4307. keep=true;
  4308. } else if (ins->auto_rooms.size()) {
  4309. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  4310. if (E->get()->room_info->last_visited_pass==render_pass) {
  4311. keep=true;
  4312. break;
  4313. }
  4314. }
  4315. } else if(exterior_visited)
  4316. keep=true;
  4317. } else {
  4318. keep=true;
  4319. }
  4320. }
  4321. if (keep) {
  4322. // update cull range
  4323. float min,max;
  4324. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  4325. if (min<cull_range.min)
  4326. cull_range.min=min;
  4327. if (max>cull_range.max)
  4328. cull_range.max=max;
  4329. if (ins->sampled_light && ins->sampled_light->baked_light_sampler_info->last_pass!=render_pass) {
  4330. if (light_samplers_culled<MAX_LIGHT_SAMPLERS) {
  4331. light_sampler_cull_result[light_samplers_culled++]=ins->sampled_light;
  4332. ins->sampled_light->baked_light_sampler_info->last_pass=render_pass;
  4333. }
  4334. }
  4335. }
  4336. }
  4337. if (!keep) {
  4338. // remove, no reason to keep
  4339. cull_count--;
  4340. SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] );
  4341. i--;
  4342. ins->last_render_pass=0; // make invalid
  4343. } else {
  4344. ins->last_render_pass=render_pass;
  4345. }
  4346. }
  4347. if (cull_range.max > cull_range.z_far )
  4348. cull_range.max=cull_range.z_far;
  4349. if (cull_range.min < cull_range.z_near )
  4350. cull_range.min=cull_range.z_near;
  4351. /* STEP 5 - PROCESS LIGHTS */
  4352. rasterizer->shadow_clear_near(); //clear near shadows, will be recreated
  4353. // directional lights
  4354. {
  4355. List<RID>::Element *E=p_scenario->directional_lights.front();
  4356. while(E) {
  4357. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  4358. if (light && light->light_info->enabled && rasterizer->light_has_shadow(light->base_rid)) {
  4359. //rasterizer->light_instance_set_active_hint(light->light_info->instance);
  4360. _light_instance_update_shadow(light,p_scenario,p_camera,cull_range);
  4361. }
  4362. E=E->next();
  4363. }
  4364. }
  4365. //discard lights not affecting anything (useful for deferred rendering, shadowmaps, etc)
  4366. for (int i=0;i<light_cull_count;i++) {
  4367. Instance *ins = light_cull_result[i];
  4368. if (light_discard_enabled) {
  4369. //see if the light should be pre discarded because no one is seeing it
  4370. //this test may seem expensive, but in reality, it shouldn't be
  4371. //because of early out condition. It will only go through everything
  4372. //if it's being discarded.
  4373. bool valid=false;
  4374. InstanceSet::Element *E =ins->light_info->affected.front();
  4375. while(E) {
  4376. if (E->get()->last_render_pass==render_pass) {
  4377. valid=true; // early out.
  4378. break;
  4379. }
  4380. E=E->next();
  4381. }
  4382. if (!valid) {
  4383. light_cull_count--;
  4384. SWAP( light_cull_result[i], light_cull_result[ light_cull_count ] );
  4385. i--;
  4386. }
  4387. }
  4388. }
  4389. { //this should eventually change to
  4390. //assign shadows by distance to camera
  4391. SortArray<Instance*,_InstanceLightsort> sorter;
  4392. sorter.sort(light_cull_result,light_cull_count);
  4393. for (int i=0;i<light_cull_count;i++) {
  4394. Instance *ins = light_cull_result[i];
  4395. if (!rasterizer->light_has_shadow(ins->base_rid) || !shadows_enabled)
  4396. continue;
  4397. /* for far shadows?
  4398. if (ins->version == ins->light_info->last_version && rasterizer->light_instance_has_far_shadow(ins->light_info->instance))
  4399. continue; // didn't change
  4400. */
  4401. _light_instance_update_shadow(ins,p_scenario,p_camera,cull_range);
  4402. ins->light_info->last_version=ins->version;
  4403. }
  4404. }
  4405. /* ENVIRONMENT */
  4406. RID environment;
  4407. if (p_camera->env.is_valid()) //camera has more environment priority
  4408. environment=p_camera->env;
  4409. else if (p_scenario->environment.is_valid())
  4410. environment=p_scenario->environment;
  4411. else
  4412. environment=p_scenario->fallback_environment;
  4413. /* STEP 6 - SAMPLE BAKED LIGHT */
  4414. bool islinear =false;
  4415. if (environment.is_valid()) {
  4416. islinear = rasterizer->environment_is_fx_enabled(environment,VS::ENV_FX_SRGB);
  4417. }
  4418. for(int i=0;i<light_samplers_culled;i++) {
  4419. _process_sampled_light(p_camera->transform,light_sampler_cull_result[i],islinear);
  4420. }
  4421. /* STEP 7 - PROCESS GEOMETRY AND DRAW SCENE*/
  4422. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  4423. rasterizer->set_viewport(viewport_rect);
  4424. // add lights
  4425. {
  4426. List<RID>::Element *E=p_scenario->directional_lights.front();
  4427. for(;E;E=E->next()) {
  4428. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  4429. ERR_CONTINUE(!light);
  4430. if (!light->light_info->enabled)
  4431. continue;
  4432. rasterizer->add_light(light->light_info->instance);
  4433. light->light_info->last_add_pass=render_pass;
  4434. }
  4435. for (int i=0;i<light_cull_count;i++) {
  4436. Instance *ins = light_cull_result[i];
  4437. rasterizer->add_light(ins->light_info->instance);
  4438. ins->light_info->last_add_pass=render_pass;
  4439. }
  4440. }
  4441. // add geometry
  4442. for(int i=0;i<cull_count;i++) {
  4443. Instance *ins = instance_cull_result[i];
  4444. ERR_CONTINUE(!((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK));
  4445. _instance_draw(ins);
  4446. }
  4447. rasterizer->end_scene();
  4448. }
  4449. 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) {
  4450. static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1;
  4451. Rasterizer::CanvasItem *z_list[z_range];
  4452. Rasterizer::CanvasItem *z_last_list[z_range];
  4453. for(int i=0;i<z_range;i++) {
  4454. z_list[i]=NULL;
  4455. z_last_list[i]=NULL;
  4456. }
  4457. _render_canvas_item(p_canvas_item,p_transform,p_clip_rect,1.0,0,z_list,z_last_list,NULL,NULL);
  4458. for(int i=0;i<z_range;i++) {
  4459. if (!z_list[i])
  4460. continue;
  4461. rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_modulate,p_lights);
  4462. }
  4463. }
  4464. void VisualServerRaster::_render_canvas_item_viewport(VisualServer* p_self,void *p_vp,const Rect2& p_rect) {
  4465. VisualServerRaster *self=(VisualServerRaster*)(p_self);
  4466. Viewport *vp=(Viewport*)p_vp;
  4467. self->_draw_viewport(vp,p_rect.pos.x,p_rect.pos.y,p_rect.size.x,p_rect.size.y);
  4468. self->rasterizer->canvas_begin();
  4469. }
  4470. 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) {
  4471. CanvasItem *ci = p_canvas_item;
  4472. if (!ci->visible)
  4473. return;
  4474. if (p_opacity<0.007)
  4475. return;
  4476. Rect2 rect = ci->get_rect();
  4477. Matrix32 xform = p_transform * ci->xform;
  4478. Rect2 global_rect = xform.xform(rect);
  4479. global_rect.pos+=p_clip_rect.pos;
  4480. if (global_rect.intersects(p_clip_rect) && ci->viewport.is_valid() && viewport_owner.owns(ci->viewport)) {
  4481. Viewport *vp = viewport_owner.get(ci->viewport);
  4482. Point2i from = xform.get_origin() + Point2(viewport_rect.x,viewport_rect.y);
  4483. Point2i size = rect.size;
  4484. size.x *= xform[0].length();
  4485. size.y *= xform[1].length();
  4486. ci->vp_render = memnew( Rasterizer::CanvasItem::ViewportRender );
  4487. ci->vp_render->owner=this;
  4488. ci->vp_render->udata=vp;
  4489. ci->vp_render->rect=Rect2(from.x,
  4490. from.y,
  4491. size.x,
  4492. size.y);
  4493. /*
  4494. _draw_viewport(vp,
  4495. from.x,
  4496. from.y,
  4497. size.x,
  4498. size.y);
  4499. */
  4500. //rasterizer->canvas_begin();
  4501. } else {
  4502. ci->vp_render=NULL;
  4503. }
  4504. if (ci->use_parent_material && p_material_owner)
  4505. ci->material_owner=p_material_owner;
  4506. else {
  4507. p_material_owner=ci;
  4508. ci->material_owner=NULL;
  4509. }
  4510. float opacity = ci->opacity * p_opacity;
  4511. int child_item_count=ci->child_items.size();
  4512. CanvasItem **child_items=(CanvasItem**)alloca(child_item_count*sizeof(CanvasItem*));
  4513. copymem(child_items,ci->child_items.ptr(),child_item_count*sizeof(CanvasItem*));
  4514. if (ci->clip) {
  4515. ci->final_clip_rect=global_rect;
  4516. ci->final_clip_owner=ci;
  4517. } else {
  4518. ci->final_clip_owner=p_canvas_clip;
  4519. }
  4520. if (ci->sort_y) {
  4521. SortArray<CanvasItem*,CanvasItemPtrSort> sorter;
  4522. sorter.sort(child_items,child_item_count);
  4523. }
  4524. if (ci->z_relative)
  4525. p_z=CLAMP(p_z+ci->z,CANVAS_ITEM_Z_MIN,CANVAS_ITEM_Z_MAX);
  4526. else
  4527. p_z=ci->z;
  4528. for(int i=0;i<child_item_count;i++) {
  4529. if (child_items[i]->ontop)
  4530. continue;
  4531. _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);
  4532. }
  4533. if (ci->copy_back_buffer) {
  4534. ci->copy_back_buffer->screen_rect = xform.xform(ci->copy_back_buffer->rect).clip(p_clip_rect);
  4535. }
  4536. if ((!ci->commands.empty() && p_clip_rect.intersects(global_rect)) || ci->vp_render || ci->copy_back_buffer) {
  4537. //something to draw?
  4538. ci->final_transform=xform;
  4539. ci->final_opacity=opacity * ci->self_opacity;
  4540. ci->global_rect_cache=global_rect;
  4541. ci->global_rect_cache.pos-=p_clip_rect.pos;
  4542. int zidx = p_z-CANVAS_ITEM_Z_MIN;
  4543. if (z_last_list[zidx]) {
  4544. z_last_list[zidx]->next=ci;
  4545. z_last_list[zidx]=ci;
  4546. } else {
  4547. z_list[zidx]=ci;
  4548. z_last_list[zidx]=ci;
  4549. }
  4550. ci->next=NULL;
  4551. }
  4552. for(int i=0;i<child_item_count;i++) {
  4553. if (!child_items[i]->ontop)
  4554. continue;
  4555. _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);
  4556. }
  4557. }
  4558. void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform,Rasterizer::CanvasLight *p_lights) {
  4559. rasterizer->canvas_begin();
  4560. int l = p_canvas->child_items.size();
  4561. Canvas::ChildItem *ci=p_canvas->child_items.ptr();
  4562. bool has_mirror=false;
  4563. for(int i=0;i<l;i++) {
  4564. if (ci[i].mirror.x || ci[i].mirror.y) {
  4565. has_mirror=true;
  4566. break;
  4567. }
  4568. }
  4569. Rect2 clip_rect(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height);
  4570. if (!has_mirror) {
  4571. static const int z_range = CANVAS_ITEM_Z_MAX-CANVAS_ITEM_Z_MIN+1;
  4572. Rasterizer::CanvasItem *z_list[z_range];
  4573. Rasterizer::CanvasItem *z_last_list[z_range];
  4574. for(int i=0;i<z_range;i++) {
  4575. z_list[i]=NULL;
  4576. z_last_list[i]=NULL;
  4577. }
  4578. for(int i=0;i<l;i++) {
  4579. _render_canvas_item(ci[i].item,p_transform,clip_rect,1.0,0,z_list,z_last_list,NULL,NULL);
  4580. }
  4581. for(int i=0;i<z_range;i++) {
  4582. if (!z_list[i])
  4583. continue;
  4584. rasterizer->canvas_render_items(z_list[i],CANVAS_ITEM_Z_MIN+i,p_canvas->modulate,p_lights);
  4585. }
  4586. } else {
  4587. for(int i=0;i<l;i++) {
  4588. Canvas::ChildItem& ci=p_canvas->child_items[i];
  4589. _render_canvas_item_tree(ci.item,p_transform,clip_rect,p_canvas->modulate,p_lights);
  4590. //mirroring (useful for scrolling backgrounds)
  4591. if (ci.mirror.x!=0) {
  4592. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0));
  4593. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4594. }
  4595. if (ci.mirror.y!=0) {
  4596. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y));
  4597. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4598. }
  4599. if (ci.mirror.y!=0 && ci.mirror.x!=0) {
  4600. Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror);
  4601. _render_canvas_item_tree(ci.item,xform2,clip_rect,p_canvas->modulate,p_lights);
  4602. }
  4603. }
  4604. }
  4605. }
  4606. void VisualServerRaster::_draw_viewport_camera(Viewport *p_viewport,bool p_ignore_camera) {
  4607. Camera *camera=NULL;
  4608. if (camera_owner.owns( p_viewport->camera ))
  4609. camera=camera_owner.get( p_viewport->camera );
  4610. Scenario *scenario = scenario_owner.get( p_viewport->scenario );
  4611. _update_instances(); // check dirty instances before rendering
  4612. if (p_ignore_camera)
  4613. _render_no_camera(p_viewport, camera,scenario );
  4614. else
  4615. _render_camera(p_viewport, camera,scenario );
  4616. }
  4617. void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ofs_y,int p_parent_w,int p_parent_h) {
  4618. ViewportRect desired_rect=p_viewport->rect;
  4619. ViewportRect old_rect = viewport_rect;
  4620. // bool vpchanged=false;
  4621. // convert default expanding viewports to actual size
  4622. //if (desired_rect.x==0 && desired_rect.y==0 && desired_rect.width==0 && desired_rect.height==0) {
  4623. if (p_parent_w != 0 && p_parent_h != 0) {
  4624. desired_rect.width=p_parent_w;
  4625. desired_rect.height=p_parent_h;
  4626. }
  4627. ERR_FAIL_COND(desired_rect.width<=0 || desired_rect.height<=0);
  4628. desired_rect.x+=p_ofs_x;
  4629. desired_rect.y+=p_ofs_y;
  4630. // if the viewport is different than the actual one, change it
  4631. if ( p_viewport->render_target.is_valid() || viewport_rect.x != desired_rect.x ||
  4632. viewport_rect.y != desired_rect.y ||
  4633. viewport_rect.width != desired_rect.width ||
  4634. viewport_rect.height != desired_rect.height ) {
  4635. viewport_rect=desired_rect;
  4636. rasterizer->set_viewport(viewport_rect);
  4637. }
  4638. /* Camera should always be BEFORE any other 3D */
  4639. bool scenario_draw_canvas_bg=false;
  4640. int scenario_canvas_max_layer=0;
  4641. if (!p_viewport->hide_canvas && !p_viewport->disable_environment && scenario_owner.owns(p_viewport->scenario)) {
  4642. Scenario *scenario=scenario_owner.get(p_viewport->scenario);
  4643. if (scenario->environment.is_valid()) {
  4644. if (rasterizer->is_environment(scenario->environment)) {
  4645. scenario_draw_canvas_bg=rasterizer->environment_get_background(scenario->environment)==VS::ENV_BG_CANVAS;
  4646. scenario_canvas_max_layer=rasterizer->environment_get_background_param(scenario->environment,VS::ENV_BG_PARAM_CANVAS_MAX_LAYER);
  4647. }
  4648. }
  4649. }
  4650. bool can_draw_3d=!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario);
  4651. if (scenario_draw_canvas_bg) {
  4652. rasterizer->begin_canvas_bg();
  4653. }
  4654. if (!scenario_draw_canvas_bg && can_draw_3d) {
  4655. _draw_viewport_camera(p_viewport,false);
  4656. } else if (true /*|| !p_viewport->canvas_list.empty()*/){
  4657. //clear the viewport black because of no camera? i seriously should..
  4658. if (p_viewport->render_target_clear_on_new_frame || p_viewport->render_target_clear) {
  4659. if (p_viewport->transparent_bg) {
  4660. rasterizer->clear_viewport(Color(0,0,0,0));
  4661. }
  4662. else {
  4663. Color cc=clear_color;
  4664. if (scenario_draw_canvas_bg)
  4665. cc.a=0;
  4666. rasterizer->clear_viewport(cc);
  4667. }
  4668. p_viewport->render_target_clear=false;
  4669. }
  4670. }
  4671. if (!p_viewport->hide_canvas) {
  4672. int i=0;
  4673. Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
  4674. Rect2 clip_rect(0,0,viewport_rect.width,viewport_rect.height);
  4675. Rasterizer::CanvasLight *lights=NULL;
  4676. Rasterizer::CanvasLight *lights_with_shadow=NULL;
  4677. Rect2 shadow_rect;
  4678. int light_count=0;
  4679. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  4680. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  4681. //find lights in canvas
  4682. for(Set<Rasterizer::CanvasLight*>::Element *F=E->get().canvas->lights.front();F;F=F->next()) {
  4683. Rasterizer::CanvasLight* cl=F->get();
  4684. if (cl->enabled && cl->texture.is_valid()) {
  4685. //not super efficient..
  4686. Size2 tsize(rasterizer->texture_get_width(cl->texture),rasterizer->texture_get_height(cl->texture));
  4687. tsize*=cl->scale;
  4688. Vector2 offset=tsize/2.0;
  4689. cl->rect_cache=Rect2(-offset+cl->texture_offset,tsize);
  4690. cl->xform_cache=xf * cl->xform;
  4691. if (clip_rect.intersects_transformed(cl->xform_cache,cl->rect_cache)) {
  4692. cl->filter_next_ptr=lights;
  4693. lights=cl;
  4694. cl->texture_cache=NULL;
  4695. Matrix32 scale;
  4696. scale.scale(cl->rect_cache.size);
  4697. scale.elements[2]=cl->rect_cache.pos;
  4698. cl->light_shader_xform = (cl->xform_cache * scale).affine_inverse();
  4699. cl->light_shader_pos=cl->xform_cache[2];
  4700. if (cl->shadow_buffer.is_valid()) {
  4701. cl->shadows_next_ptr=lights_with_shadow;
  4702. if (lights_with_shadow==NULL) {
  4703. shadow_rect = cl->xform_cache.xform(cl->rect_cache);
  4704. } else {
  4705. shadow_rect=shadow_rect.merge( cl->xform_cache.xform(cl->rect_cache) );
  4706. }
  4707. lights_with_shadow=cl;
  4708. cl->radius_cache=cl->rect_cache.size.length();
  4709. }
  4710. light_count++;
  4711. }
  4712. }
  4713. }
  4714. //print_line("lights: "+itos(light_count));
  4715. canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
  4716. }
  4717. if (lights_with_shadow) {
  4718. //update shadows if any
  4719. Rasterizer::CanvasLightOccluderInstance * occluders=NULL;
  4720. //make list of occluders
  4721. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  4722. Matrix32 xf = p_viewport->global_transform * E->get().transform;
  4723. for(Set<Rasterizer::CanvasLightOccluderInstance*>::Element *F=E->get().canvas->occluders.front();F;F=F->next()) {
  4724. F->get()->xform_cache = xf * F->get()->xform;
  4725. if (shadow_rect.intersects_transformed(F->get()->xform_cache,F->get()->aabb_cache)) {
  4726. F->get()->next=occluders;
  4727. occluders=F->get();
  4728. }
  4729. }
  4730. }
  4731. //update the light shadowmaps with them
  4732. Rasterizer::CanvasLight *light=lights_with_shadow;
  4733. while(light) {
  4734. 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);
  4735. light=light->shadows_next_ptr;
  4736. }
  4737. rasterizer->set_viewport(viewport_rect); //must reset viewport afterwards
  4738. }
  4739. if (scenario_draw_canvas_bg && canvas_map.front() && canvas_map.front()->key().layer>scenario_canvas_max_layer) {
  4740. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4741. scenario_draw_canvas_bg=false;
  4742. }
  4743. for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
  4744. // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
  4745. //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
  4746. Matrix32 xform = p_viewport->global_transform * E->get()->transform;
  4747. Rasterizer::CanvasLight *canvas_lights=NULL;
  4748. Rasterizer::CanvasLight *ptr=lights;
  4749. while(ptr) {
  4750. if (E->get()->layer>=ptr->layer_min && E->get()->layer<=ptr->layer_max) {
  4751. ptr->next_ptr=canvas_lights;
  4752. canvas_lights=ptr;
  4753. }
  4754. ptr=ptr->filter_next_ptr;
  4755. }
  4756. _render_canvas( E->get()->canvas,xform,canvas_lights );
  4757. i++;
  4758. if (scenario_draw_canvas_bg && E->key().layer>=scenario_canvas_max_layer) {
  4759. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4760. scenario_draw_canvas_bg=false;
  4761. }
  4762. }
  4763. if (scenario_draw_canvas_bg) {
  4764. _draw_viewport_camera(p_viewport,!can_draw_3d);
  4765. scenario_draw_canvas_bg=false;
  4766. }
  4767. // rasterizer->canvas_debug_viewport_shadows(lights_with_shadow);
  4768. }
  4769. //capture
  4770. if (p_viewport->queue_capture) {
  4771. rasterizer->capture_viewport(&p_viewport->capture);
  4772. p_viewport->queue_capture = false;
  4773. }
  4774. //restore
  4775. if ( viewport_rect.x != old_rect.x ||
  4776. viewport_rect.y != old_rect.y ||
  4777. viewport_rect.width != old_rect.width ||
  4778. viewport_rect.height != old_rect.height ) {
  4779. viewport_rect=old_rect;
  4780. rasterizer->set_viewport(viewport_rect);
  4781. }
  4782. }
  4783. void VisualServerRaster::_draw_viewports() {
  4784. //draw viewports for render targets
  4785. List<Viewport*> to_blit;
  4786. List<Viewport*> to_disable;
  4787. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  4788. Viewport *vp = E->self();
  4789. ERR_CONTINUE(!vp);
  4790. if (
  4791. vp->render_target_update_mode==RENDER_TARGET_UPDATE_WHEN_VISIBLE &&
  4792. !vp->rendered_in_prev_frame &&
  4793. !vp->queue_capture
  4794. ) {
  4795. continue;
  4796. }
  4797. if (vp->rt_to_screen_rect!=Rect2())
  4798. to_blit.push_back(vp);
  4799. rasterizer->set_render_target(vp->render_target,vp->transparent_bg,vp->render_target_vflip);
  4800. _draw_viewport(vp,0,0,vp->rect.width,vp->rect.height);
  4801. if ( (vp->queue_capture && vp->render_target_update_mode==RENDER_TARGET_UPDATE_DISABLED) || vp->render_target_update_mode==RENDER_TARGET_UPDATE_ONCE) {
  4802. //was only enabled for capture
  4803. to_disable.push_back(vp);
  4804. vp->render_target_update_mode=RENDER_TARGET_UPDATE_DISABLED;
  4805. }
  4806. }
  4807. rasterizer->set_render_target(RID());
  4808. while(to_disable.size()) {
  4809. //disable again because it was only for capture
  4810. viewport_update_list.remove(&to_disable.front()->get()->update_list);
  4811. to_disable.pop_front();
  4812. }
  4813. //draw RTs directly to screen when requested
  4814. for (List<Viewport*>::Element *E=to_blit.front();E;E=E->next()) {
  4815. int window_w = OS::get_singleton()->get_video_mode().width;
  4816. int window_h = OS::get_singleton()->get_video_mode().height;
  4817. ViewportRect desired_rect;
  4818. desired_rect.x = desired_rect.y = 0;
  4819. desired_rect.width = window_w;
  4820. desired_rect.height = window_h;
  4821. if ( viewport_rect.x != desired_rect.x ||
  4822. viewport_rect.y != desired_rect.y ||
  4823. viewport_rect.width != desired_rect.width ||
  4824. viewport_rect.height != desired_rect.height ) {
  4825. viewport_rect=desired_rect;
  4826. rasterizer->set_viewport(viewport_rect);
  4827. }
  4828. rasterizer->canvas_begin();
  4829. rasterizer->canvas_disable_blending();
  4830. rasterizer->canvas_begin_rect(Matrix32());
  4831. 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));
  4832. }
  4833. //draw viewports attached to screen
  4834. for(Map<RID,int>::Element *E=screen_viewports.front();E;E=E->next()) {
  4835. Viewport *vp = viewport_owner.get(E->key());
  4836. ERR_CONTINUE(!vp);
  4837. int window_w = OS::get_singleton()->get_video_mode(E->get()).width;
  4838. int window_h = OS::get_singleton()->get_video_mode(E->get()).height;
  4839. Rect2 r(0,0,vp->rect.width,vp->rect.height);
  4840. if (r.size.width==0)
  4841. r.size.width=window_w;
  4842. if (r.size.height==0)
  4843. r.size.height=window_h;
  4844. _draw_viewport(vp,r.pos.x,r.pos.y,r.size.width,r.size.height);
  4845. }
  4846. //check when a viewport associated to a render target was drawn
  4847. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  4848. Viewport *vp = E->self();
  4849. ERR_CONTINUE(!vp);
  4850. if (vp->render_target_update_mode!=RENDER_TARGET_UPDATE_WHEN_VISIBLE)
  4851. continue;
  4852. vp->rendered_in_prev_frame=rasterizer->render_target_renedered_in_frame(vp->render_target);
  4853. }
  4854. }
  4855. void VisualServerRaster::_draw_cursors_and_margins() {
  4856. int window_w = OS::get_singleton()->get_video_mode().width;
  4857. int window_h = OS::get_singleton()->get_video_mode().height;
  4858. ViewportRect desired_rect;
  4859. desired_rect.x = desired_rect.y = 0;
  4860. desired_rect.width = window_w;
  4861. desired_rect.height = window_h;
  4862. if ( viewport_rect.x != desired_rect.x ||
  4863. viewport_rect.y != desired_rect.y ||
  4864. viewport_rect.width != desired_rect.width ||
  4865. viewport_rect.height != desired_rect.height ) {
  4866. viewport_rect=desired_rect;
  4867. rasterizer->set_viewport(viewport_rect);
  4868. }
  4869. rasterizer->canvas_begin();
  4870. rasterizer->canvas_begin_rect(Matrix32());
  4871. for (int i=0; i<MAX_CURSORS; i++) {
  4872. if (!cursors[i].visible) {
  4873. continue;
  4874. };
  4875. RID tex = cursors[i].texture?cursors[i].texture:default_cursor_texture;
  4876. ERR_CONTINUE( !tex );
  4877. Point2 size(texture_get_width(tex), texture_get_height(tex));
  4878. rasterizer->canvas_draw_rect(Rect2(cursors[i].pos, size), 0, Rect2(), tex, Color(1, 1, 1, 1));
  4879. };
  4880. if (black_image[MARGIN_LEFT].is_valid()) {
  4881. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_LEFT]),rasterizer->texture_get_height(black_image[MARGIN_LEFT]));
  4882. 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));
  4883. } else if (black_margin[MARGIN_LEFT])
  4884. rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  4885. if (black_image[MARGIN_RIGHT].is_valid()) {
  4886. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_RIGHT]),rasterizer->texture_get_height(black_image[MARGIN_RIGHT]));
  4887. 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));
  4888. } else if (black_margin[MARGIN_RIGHT])
  4889. 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));
  4890. if (black_margin[MARGIN_TOP])
  4891. rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  4892. if (black_margin[MARGIN_BOTTOM])
  4893. 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));
  4894. rasterizer->canvas_end_rect();
  4895. };
  4896. void VisualServerRaster::flush() {
  4897. //do none
  4898. }
  4899. void VisualServerRaster::draw() {
  4900. //if (changes)
  4901. // print_line("changes: "+itos(changes));
  4902. changes=0;
  4903. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  4904. room_cull_enabled = GLOBAL_DEF("render/room_cull_enabled",true);
  4905. light_discard_enabled = GLOBAL_DEF("render/light_discard_enabled",true);
  4906. rasterizer->begin_frame();
  4907. _draw_viewports();
  4908. _draw_cursors_and_margins();
  4909. rasterizer->end_frame();
  4910. draw_extra_frame=rasterizer->needs_to_draw_next_frame();
  4911. }
  4912. bool VisualServerRaster::has_changed() const {
  4913. return changes>0 || draw_extra_frame;
  4914. }
  4915. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  4916. return rasterizer->get_render_info(p_info);
  4917. }
  4918. bool VisualServerRaster::has_feature(Features p_feature) const {
  4919. return rasterizer->has_feature(p_feature); // lies for now
  4920. }
  4921. void VisualServerRaster::set_default_clear_color(const Color& p_color) {
  4922. clear_color=p_color;
  4923. }
  4924. void VisualServerRaster::set_boot_image(const Image& p_image, const Color& p_color,bool p_scale) {
  4925. if (p_image.empty())
  4926. return;
  4927. rasterizer->begin_frame();
  4928. int window_w = OS::get_singleton()->get_video_mode(0).width;
  4929. int window_h = OS::get_singleton()->get_video_mode(0).height;
  4930. ViewportRect vr;
  4931. vr.x=0;
  4932. vr.y=0;
  4933. vr.width=OS::get_singleton()->get_video_mode(0).width;
  4934. vr.height=OS::get_singleton()->get_video_mode(0).height;
  4935. rasterizer->set_viewport(vr);
  4936. rasterizer->clear_viewport(p_color);
  4937. rasterizer->canvas_begin();
  4938. RID texture = texture_create();
  4939. texture_allocate(texture,p_image.get_width(),p_image.get_height(),p_image.get_format(),TEXTURE_FLAG_FILTER);
  4940. texture_set_data(texture,p_image);
  4941. rasterizer->canvas_begin_rect(Matrix32());
  4942. Rect2 imgrect(0,0,p_image.get_width(),p_image.get_height());
  4943. Rect2 screenrect;
  4944. if (p_scale) {
  4945. if (window_w > window_h) {
  4946. //scale horizontally
  4947. screenrect.size.y = window_h;
  4948. screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y;
  4949. screenrect.pos.x = (window_w - screenrect.size.x)/2;
  4950. } else {
  4951. //scale vertically
  4952. screenrect.size.x = window_w;
  4953. screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x;
  4954. screenrect.pos.y = (window_h - screenrect.size.y)/2;
  4955. }
  4956. } else {
  4957. screenrect=imgrect;
  4958. screenrect.pos+=((Size2(vr.width,vr.height)-screenrect.size)/2.0).floor();
  4959. }
  4960. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,1));
  4961. rasterizer->canvas_end_rect();
  4962. rasterizer->end_frame();
  4963. rasterizer->flush_frame();
  4964. free(texture); // free since it's only one frame that stays there
  4965. }
  4966. void VisualServerRaster::init() {
  4967. rasterizer->init();
  4968. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  4969. //default_scenario = scenario_create();
  4970. //default_viewport = viewport_create();
  4971. for(int i=0;i<4;i++)
  4972. black_margin[i]=0;
  4973. Image img;
  4974. img.create(default_mouse_cursor_xpm);
  4975. //img.convert(Image::FORMAT_RGB);
  4976. default_cursor_texture = texture_create_from_image(img, 0);
  4977. aabb_random_points.resize( GLOBAL_DEF("render/aabb_random_points",16) );
  4978. for(int i=0;i<aabb_random_points.size();i++)
  4979. aabb_random_points[i]=Vector3(Math::random(0,1),Math::random(0,1),Math::random(0,1));
  4980. transformed_aabb_random_points.resize(aabb_random_points.size());
  4981. changes=0;
  4982. }
  4983. void VisualServerRaster::_clean_up_owner(RID_OwnerBase *p_owner,String p_type) {
  4984. List<RID> rids;
  4985. p_owner->get_owned_list(&rids);
  4986. int lost=0;
  4987. for(List<RID>::Element *I=rids.front();I;I=I->next()) {
  4988. if (OS::get_singleton()->is_stdout_verbose()) {
  4989. lost++;
  4990. }
  4991. free(I->get());
  4992. }
  4993. if (lost)
  4994. print_line("VisualServerRaster: WARNING: Lost "+itos(lost)+" RIDs of type "+p_type);
  4995. }
  4996. void VisualServerRaster::finish() {
  4997. free(default_cursor_texture);
  4998. if (test_cube.is_valid())
  4999. free(test_cube);
  5000. _free_internal_rids();
  5001. _clean_up_owner( &room_owner,"Room" );
  5002. _clean_up_owner( &portal_owner,"Portal" );
  5003. _clean_up_owner( &camera_owner,"Camera" );
  5004. _clean_up_owner( &viewport_owner,"Viewport" );
  5005. _clean_up_owner( &scenario_owner,"Scenario" );
  5006. _clean_up_owner( &instance_owner,"Instance" );
  5007. _clean_up_owner( &canvas_owner,"Canvas" );
  5008. _clean_up_owner( &canvas_item_owner,"CanvasItem" );
  5009. rasterizer->finish();
  5010. octree_allocator.clear();
  5011. if (instance_dependency_map.size()) {
  5012. print_line("Base resources missing amount: "+itos(instance_dependency_map.size()));
  5013. }
  5014. ERR_FAIL_COND( instance_dependency_map.size() );
  5015. }
  5016. RID VisualServerRaster::get_test_cube() {
  5017. if (test_cube.is_valid())
  5018. return test_cube;
  5019. test_cube=_make_test_cube();
  5020. return test_cube;
  5021. }
  5022. VisualServerRaster::VisualServerRaster(Rasterizer *p_rasterizer) {
  5023. rasterizer=p_rasterizer;
  5024. rasterizer->draw_viewport_func=_render_canvas_item_viewport;
  5025. instance_update_list=NULL;
  5026. render_pass=0;
  5027. clear_color=Color(0.3,0.3,0.3,1.0);
  5028. OctreeAllocator::allocator=&octree_allocator;
  5029. draw_extra_frame=false;
  5030. }
  5031. VisualServerRaster::~VisualServerRaster()
  5032. {
  5033. }