visual_server_raster.cpp 201 KB

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