visual_server_raster.cpp 210 KB

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