visual_server_raster.cpp 199 KB

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