visual_server_raster.cpp 205 KB

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