visual_server_raster.cpp 205 KB

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