visual_server_raster.cpp 203 KB

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