visual_server_raster.cpp 209 KB

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