visual_server_raster.cpp 162 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187
  1. /*************************************************************************/
  2. /* visual_server_raster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "visual_server_raster.h"
  30. #include "os/os.h"
  31. #include "globals.h"
  32. #include "default_mouse_cursor.xpm"
  33. #include "sort.h"
  34. #include "io/marshalls.h"
  35. // careful, these may run in different threads than the visual server
  36. BalloonAllocator<> *VisualServerRaster::OctreeAllocator::allocator=NULL;
  37. #define VS_CHANGED\
  38. changes++;\
  39. // print_line(__FUNCTION__);
  40. RID VisualServerRaster::texture_create() {
  41. return rasterizer->texture_create();
  42. }
  43. void VisualServerRaster::texture_allocate(RID p_texture, int p_width, int p_height,Image::Format p_format,uint32_t p_flags) {
  44. rasterizer->texture_allocate(p_texture,p_width,p_height,p_format,p_flags);
  45. }
  46. void VisualServerRaster::texture_set_flags(RID p_texture,uint32_t p_flags) {
  47. VS_CHANGED;
  48. rasterizer->texture_set_flags(p_texture,p_flags);
  49. }
  50. void VisualServerRaster::texture_set_data(RID p_texture,const Image& p_image,CubeMapSide p_cube_side) {
  51. VS_CHANGED;
  52. rasterizer->texture_set_data(p_texture,p_image,p_cube_side);
  53. }
  54. Image VisualServerRaster::texture_get_data(RID p_texture,CubeMapSide p_cube_side) const {
  55. return rasterizer->texture_get_data(p_texture,p_cube_side);
  56. }
  57. uint32_t VisualServerRaster::texture_get_flags(RID p_texture) const {
  58. return rasterizer->texture_get_flags(p_texture);
  59. }
  60. Image::Format VisualServerRaster::texture_get_format(RID p_texture) const {
  61. return rasterizer->texture_get_format(p_texture);
  62. }
  63. uint32_t VisualServerRaster::texture_get_width(RID p_texture) const {
  64. return rasterizer->texture_get_width(p_texture);
  65. }
  66. uint32_t VisualServerRaster::texture_get_height(RID p_texture) const {
  67. return rasterizer->texture_get_height(p_texture);
  68. }
  69. void VisualServerRaster::texture_set_size_override(RID p_texture,int p_width, int p_height) {
  70. rasterizer->texture_set_size_override(p_texture,p_width,p_height);
  71. }
  72. bool VisualServerRaster::texture_can_stream(RID p_texture) const {
  73. return false;
  74. }
  75. void VisualServerRaster::texture_set_reload_hook(RID p_texture,ObjectID p_owner,const StringName& p_function) const {
  76. rasterizer->texture_set_reload_hook(p_texture,p_owner,p_function);
  77. }
  78. /* SHADER API */
  79. RID VisualServerRaster::shader_create(ShaderMode p_mode) {
  80. return rasterizer->shader_create(p_mode);
  81. }
  82. void VisualServerRaster::shader_set_mode(RID p_shader,ShaderMode p_mode){
  83. VS_CHANGED;
  84. rasterizer->shader_set_mode(p_shader,p_mode);
  85. }
  86. VisualServer::ShaderMode VisualServerRaster::shader_get_mode(RID p_shader) const{
  87. return rasterizer->shader_get_mode(p_shader);
  88. }
  89. void VisualServerRaster::shader_set_code(RID p_shader, const String& p_vertex, const String& p_fragment,const String& p_light,int p_vertex_ofs,int p_fragment_ofs,int p_light_ofs) {
  90. VS_CHANGED;
  91. rasterizer->shader_set_code(p_shader,p_vertex,p_fragment,p_light,p_vertex_ofs,p_fragment_ofs,p_light_ofs);
  92. }
  93. String VisualServerRaster::shader_get_vertex_code(RID p_shader) const{
  94. return rasterizer->shader_get_vertex_code(p_shader);
  95. }
  96. String VisualServerRaster::shader_get_fragment_code(RID p_shader) const{
  97. return rasterizer->shader_get_fragment_code(p_shader);
  98. }
  99. String VisualServerRaster::shader_get_light_code(RID p_shader) const{
  100. return rasterizer->shader_get_light_code(p_shader);
  101. }
  102. void VisualServerRaster::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  103. return rasterizer->shader_get_param_list(p_shader,p_param_list);
  104. }
  105. /* Material */
  106. RID VisualServerRaster::material_create() {
  107. return rasterizer->material_create();
  108. }
  109. void VisualServerRaster::material_set_shader(RID p_material, RID p_shader) {
  110. VS_CHANGED;
  111. rasterizer->material_set_shader(p_material, p_shader );
  112. }
  113. RID VisualServerRaster::material_get_shader(RID p_material) const {
  114. return rasterizer->material_get_shader(p_material);
  115. }
  116. void VisualServerRaster::material_set_param(RID p_material, const StringName& p_param, const Variant& p_value) {
  117. VS_CHANGED;
  118. rasterizer->material_set_param(p_material, p_param,p_value );
  119. }
  120. Variant VisualServerRaster::material_get_param(RID p_material, const StringName& p_param) const {
  121. return rasterizer->material_get_param(p_material,p_param);
  122. }
  123. void VisualServerRaster::material_set_flag(RID p_material, MaterialFlag p_flag,bool p_enabled) {
  124. VS_CHANGED;
  125. rasterizer->material_set_flag(p_material,p_flag,p_enabled);
  126. }
  127. void VisualServerRaster::material_set_depth_draw_mode(RID p_material, MaterialDepthDrawMode p_mode) {
  128. VS_CHANGED;
  129. rasterizer->material_set_depth_draw_mode(p_material,p_mode);
  130. }
  131. VS::MaterialDepthDrawMode VisualServerRaster::material_get_depth_draw_mode(RID p_material) const {
  132. return rasterizer->material_get_depth_draw_mode(p_material);
  133. }
  134. bool VisualServerRaster::material_get_flag(RID p_material,MaterialFlag p_flag) const {
  135. return rasterizer->material_get_flag(p_material,p_flag);
  136. }
  137. void VisualServerRaster::material_set_blend_mode(RID p_material,MaterialBlendMode p_mode) {
  138. VS_CHANGED;
  139. rasterizer->material_set_blend_mode(p_material,p_mode);
  140. }
  141. VS::MaterialBlendMode VisualServerRaster::material_get_blend_mode(RID p_material) const {
  142. return rasterizer->material_get_blend_mode(p_material);
  143. }
  144. void VisualServerRaster::material_set_line_width(RID p_material,float p_line_width) {
  145. VS_CHANGED;
  146. rasterizer->material_set_line_width(p_material,p_line_width);
  147. }
  148. float VisualServerRaster::material_get_line_width(RID p_material) const {
  149. return rasterizer->material_get_line_width(p_material);
  150. }
  151. /* FIXED MATERIAL */
  152. RID VisualServerRaster::fixed_material_create() {
  153. return rasterizer->fixed_material_create();
  154. }
  155. void VisualServerRaster::fixed_material_set_flag(RID p_material, FixedMaterialFlags p_flag, bool p_enabled) {
  156. rasterizer->fixed_material_set_flag(p_material,p_flag,p_enabled);
  157. }
  158. bool VisualServerRaster::fixed_material_get_flag(RID p_material, FixedMaterialFlags p_flag) const {
  159. return rasterizer->fixed_material_get_flag(p_material,p_flag);
  160. }
  161. void VisualServerRaster::fixed_material_set_param(RID p_material, FixedMaterialParam p_parameter, const Variant& p_value) {
  162. VS_CHANGED;
  163. rasterizer->fixed_material_set_parameter(p_material,p_parameter,p_value);
  164. }
  165. Variant VisualServerRaster::fixed_material_get_param(RID p_material,FixedMaterialParam p_parameter) const {
  166. return rasterizer->fixed_material_get_parameter(p_material,p_parameter);
  167. }
  168. void VisualServerRaster::fixed_material_set_texture(RID p_material,FixedMaterialParam p_parameter, RID p_texture) {
  169. VS_CHANGED;
  170. rasterizer->fixed_material_set_texture(p_material,p_parameter,p_texture);
  171. }
  172. RID VisualServerRaster::fixed_material_get_texture(RID p_material,FixedMaterialParam p_parameter) const {
  173. return rasterizer->fixed_material_get_texture(p_material,p_parameter);
  174. }
  175. void VisualServerRaster::fixed_material_set_texcoord_mode(RID p_material,FixedMaterialParam p_parameter, FixedMaterialTexCoordMode p_mode) {
  176. VS_CHANGED;
  177. rasterizer->fixed_material_set_texcoord_mode(p_material,p_parameter,p_mode);
  178. }
  179. VS::FixedMaterialTexCoordMode VisualServerRaster::fixed_material_get_texcoord_mode(RID p_material,FixedMaterialParam p_parameter) const {
  180. return rasterizer->fixed_material_get_texcoord_mode(p_material,p_parameter);
  181. }
  182. void VisualServerRaster::fixed_material_set_point_size(RID p_material,float p_size) {
  183. VS_CHANGED
  184. rasterizer->fixed_material_set_point_size(p_material,p_size);
  185. }
  186. float VisualServerRaster::fixed_material_get_point_size(RID p_material) const{
  187. return rasterizer->fixed_material_get_point_size(p_material);
  188. }
  189. void VisualServerRaster::fixed_material_set_uv_transform(RID p_material,const Transform& p_transform) {
  190. VS_CHANGED;
  191. rasterizer->fixed_material_set_uv_transform(p_material,p_transform);
  192. }
  193. Transform VisualServerRaster::fixed_material_get_uv_transform(RID p_material) const {
  194. return rasterizer->fixed_material_get_uv_transform(p_material);
  195. }
  196. void VisualServerRaster::fixed_material_set_light_shader(RID p_material,FixedMaterialLightShader p_shader) {
  197. VS_CHANGED;
  198. rasterizer->fixed_material_set_light_shader(p_material,p_shader);
  199. }
  200. VisualServerRaster::FixedMaterialLightShader VisualServerRaster::fixed_material_get_light_shader(RID p_material) const{
  201. return rasterizer->fixed_material_get_light_shader(p_material);
  202. }
  203. /* MESH API */
  204. RID VisualServerRaster::mesh_create() {
  205. return rasterizer->mesh_create();
  206. }
  207. void VisualServerRaster::mesh_set_morph_target_count(RID p_mesh,int p_amount) {
  208. rasterizer->mesh_set_morph_target_count(p_mesh,p_amount);
  209. int amount = rasterizer->mesh_get_morph_target_count(p_mesh);
  210. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_mesh );
  211. if (!E)
  212. return;
  213. Set<RID>::Element *I = E->get().front();
  214. while(I) {
  215. Instance *ins = instance_owner.get( I->get() );
  216. ins->data.morph_values.resize(amount);
  217. I = I->next();
  218. }
  219. }
  220. int VisualServerRaster::mesh_get_morph_target_count(RID p_mesh) const {
  221. return rasterizer->mesh_get_morph_target_count(p_mesh);
  222. }
  223. void VisualServerRaster::mesh_set_morph_target_mode(RID p_mesh,MorphTargetMode p_mode) {
  224. rasterizer->mesh_set_morph_target_mode(p_mesh,p_mode);
  225. }
  226. VisualServer::MorphTargetMode VisualServerRaster::mesh_get_morph_target_mode(RID p_mesh) const{
  227. return rasterizer->mesh_get_morph_target_mode(p_mesh);
  228. }
  229. void VisualServerRaster::mesh_add_custom_surface(RID p_mesh,const Variant& p_dat) {
  230. }
  231. void VisualServerRaster::mesh_add_surface(RID p_mesh,PrimitiveType p_primitive,const Array& p_arrays,const Array& p_blend_shapes,bool p_alpha_sort) {
  232. VS_CHANGED;
  233. _dependency_queue_update(p_mesh,true);
  234. rasterizer->mesh_add_surface(p_mesh,p_primitive,p_arrays,p_blend_shapes,p_alpha_sort);
  235. }
  236. Array VisualServerRaster::mesh_get_surface_arrays(RID p_mesh,int p_surface) const {
  237. return rasterizer->mesh_get_surface_arrays(p_mesh,p_surface);
  238. }
  239. Array VisualServerRaster::mesh_get_surface_morph_arrays(RID p_mesh,int p_surface) const {
  240. return rasterizer->mesh_get_surface_morph_arrays(p_mesh,p_surface);
  241. }
  242. void VisualServerRaster::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material,bool p_owned){
  243. VS_CHANGED;
  244. rasterizer->mesh_surface_set_material(p_mesh,p_surface,p_material,p_owned);
  245. }
  246. RID VisualServerRaster::mesh_surface_get_material(RID p_mesh,int p_surface) const {
  247. return rasterizer->mesh_surface_get_material(p_mesh,p_surface);
  248. }
  249. int VisualServerRaster::mesh_surface_get_array_len(RID p_mesh, int p_surface) const{
  250. return rasterizer->mesh_surface_get_array_len(p_mesh,p_surface);
  251. }
  252. int VisualServerRaster::mesh_surface_get_array_index_len(RID p_mesh, int p_surface) const{
  253. return rasterizer->mesh_surface_get_array_index_len(p_mesh,p_surface);
  254. }
  255. uint32_t VisualServerRaster::mesh_surface_get_format(RID p_mesh, int p_surface) const{
  256. return rasterizer->mesh_surface_get_format(p_mesh,p_surface);
  257. }
  258. VisualServer::PrimitiveType VisualServerRaster::mesh_surface_get_primitive_type(RID p_mesh, int p_surface) const{
  259. return rasterizer->mesh_surface_get_primitive_type(p_mesh,p_surface);
  260. }
  261. void VisualServerRaster::mesh_remove_surface(RID p_mesh,int p_surface){
  262. rasterizer->mesh_remove_surface(p_mesh,p_surface);
  263. }
  264. int VisualServerRaster::mesh_get_surface_count(RID p_mesh) const{
  265. return rasterizer->mesh_get_surface_count(p_mesh);
  266. }
  267. void VisualServerRaster::mesh_set_custom_aabb(RID p_mesh,const AABB& p_aabb) {
  268. VS_CHANGED;
  269. _dependency_queue_update(p_mesh,true);
  270. rasterizer->mesh_set_custom_aabb(p_mesh,p_aabb);
  271. }
  272. AABB VisualServerRaster::mesh_get_custom_aabb(RID p_mesh) const {
  273. return rasterizer->mesh_get_custom_aabb(p_mesh);
  274. }
  275. /* MULTIMESH */
  276. RID VisualServerRaster::multimesh_create() {
  277. return rasterizer->multimesh_create();
  278. }
  279. void VisualServerRaster::multimesh_set_instance_count(RID p_multimesh,int p_count) {
  280. VS_CHANGED;
  281. rasterizer->multimesh_set_instance_count(p_multimesh,p_count);
  282. }
  283. int VisualServerRaster::multimesh_get_instance_count(RID p_multimesh) const {
  284. return rasterizer->multimesh_get_instance_count(p_multimesh);
  285. }
  286. void VisualServerRaster::multimesh_set_mesh(RID p_multimesh,RID p_mesh) {
  287. VS_CHANGED;
  288. rasterizer->multimesh_set_mesh(p_multimesh,p_mesh);
  289. }
  290. void VisualServerRaster::multimesh_set_aabb(RID p_multimesh,const AABB& p_aabb) {
  291. VS_CHANGED;
  292. rasterizer->multimesh_set_aabb(p_multimesh,p_aabb);
  293. _dependency_queue_update(p_multimesh,true);
  294. }
  295. void VisualServerRaster::multimesh_instance_set_transform(RID p_multimesh,int p_index,const Transform& p_transform) {
  296. VS_CHANGED;
  297. rasterizer->multimesh_instance_set_transform(p_multimesh,p_index,p_transform);
  298. }
  299. void VisualServerRaster::multimesh_instance_set_color(RID p_multimesh,int p_index,const Color& p_color) {
  300. VS_CHANGED;
  301. rasterizer->multimesh_instance_set_color(p_multimesh,p_index,p_color);
  302. }
  303. RID VisualServerRaster::multimesh_get_mesh(RID p_multimesh) const {
  304. return rasterizer->multimesh_get_mesh(p_multimesh);
  305. }
  306. AABB VisualServerRaster::multimesh_get_aabb(RID p_multimesh,const AABB& p_aabb) const {
  307. return rasterizer->multimesh_get_aabb(p_multimesh);
  308. }
  309. Transform VisualServerRaster::multimesh_instance_get_transform(RID p_multimesh,int p_index) const {
  310. return rasterizer->multimesh_instance_get_transform(p_multimesh,p_index);
  311. }
  312. Color VisualServerRaster::multimesh_instance_get_color(RID p_multimesh,int p_index) const {
  313. return rasterizer->multimesh_instance_get_color(p_multimesh,p_index);
  314. }
  315. void VisualServerRaster::multimesh_set_visible_instances(RID p_multimesh,int p_visible) {
  316. rasterizer->multimesh_set_visible_instances(p_multimesh,p_visible);
  317. }
  318. int VisualServerRaster::multimesh_get_visible_instances(RID p_multimesh) const {
  319. return rasterizer->multimesh_get_visible_instances(p_multimesh);
  320. }
  321. /* IMMEDIATE API */
  322. RID VisualServerRaster::immediate_create() {
  323. return rasterizer->immediate_create();
  324. }
  325. void VisualServerRaster::immediate_begin(RID p_immediate,PrimitiveType p_primitive,RID p_texture){
  326. rasterizer->immediate_begin(p_immediate,p_primitive,p_texture);
  327. }
  328. void VisualServerRaster::immediate_vertex(RID p_immediate,const Vector3& p_vertex){
  329. rasterizer->immediate_vertex(p_immediate,p_vertex);
  330. }
  331. void VisualServerRaster::immediate_normal(RID p_immediate,const Vector3& p_normal){
  332. rasterizer->immediate_normal(p_immediate,p_normal);
  333. }
  334. void VisualServerRaster::immediate_tangent(RID p_immediate,const Plane& p_tangent){
  335. rasterizer->immediate_tangent(p_immediate,p_tangent);
  336. }
  337. void VisualServerRaster::immediate_color(RID p_immediate,const Color& p_color){
  338. rasterizer->immediate_color(p_immediate,p_color);
  339. }
  340. void VisualServerRaster::immediate_uv(RID p_immediate,const Vector2& p_uv){
  341. rasterizer->immediate_uv(p_immediate,p_uv);
  342. }
  343. void VisualServerRaster::immediate_uv2(RID p_immediate,const Vector2& p_uv2){
  344. rasterizer->immediate_uv2(p_immediate,p_uv2);
  345. }
  346. void VisualServerRaster::immediate_end(RID p_immediate){
  347. VS_CHANGED;
  348. _dependency_queue_update(p_immediate,true);
  349. rasterizer->immediate_end(p_immediate);
  350. }
  351. void VisualServerRaster::immediate_clear(RID p_immediate){
  352. VS_CHANGED;
  353. _dependency_queue_update(p_immediate,true);
  354. rasterizer->immediate_clear(p_immediate);
  355. }
  356. void VisualServerRaster::immediate_set_material(RID p_immediate,RID p_material) {
  357. rasterizer->immediate_set_material(p_immediate,p_material);
  358. }
  359. RID VisualServerRaster::immediate_get_material(RID p_immediate) const {
  360. return rasterizer->immediate_get_material(p_immediate);
  361. }
  362. /* PARTICLES API */
  363. RID VisualServerRaster::particles_create() {
  364. return rasterizer->particles_create();
  365. }
  366. void VisualServerRaster::particles_set_amount(RID p_particles, int p_amount) {
  367. VS_CHANGED;
  368. rasterizer->particles_set_amount(p_particles,p_amount);
  369. }
  370. int VisualServerRaster::particles_get_amount(RID p_particles) const {
  371. return rasterizer->particles_get_amount(p_particles);
  372. }
  373. void VisualServerRaster::particles_set_emitting(RID p_particles, bool p_emitting) {
  374. VS_CHANGED;
  375. rasterizer->particles_set_emitting(p_particles,p_emitting);
  376. }
  377. bool VisualServerRaster::particles_is_emitting(RID p_particles) const {
  378. return rasterizer->particles_is_emitting(p_particles);
  379. }
  380. void VisualServerRaster::particles_set_visibility_aabb(RID p_particles, const AABB& p_visibility) {
  381. VS_CHANGED;
  382. rasterizer->particles_set_visibility_aabb(p_particles, p_visibility);
  383. }
  384. AABB VisualServerRaster::particles_get_visibility_aabb(RID p_particles) const {
  385. return rasterizer->particles_get_visibility_aabb(p_particles);
  386. }
  387. void VisualServerRaster::particles_set_emission_half_extents(RID p_particles, const Vector3& p_half_extents) {
  388. VS_CHANGED;
  389. rasterizer->particles_set_emission_half_extents(p_particles,p_half_extents);
  390. }
  391. Vector3 VisualServerRaster::particles_get_emission_half_extents(RID p_particles) const {
  392. return rasterizer->particles_get_emission_half_extents(p_particles);
  393. }
  394. void VisualServerRaster::particles_set_emission_base_velocity(RID p_particles, const Vector3& p_base_velocity) {
  395. VS_CHANGED;
  396. rasterizer->particles_set_emission_base_velocity(p_particles,p_base_velocity);
  397. }
  398. Vector3 VisualServerRaster::particles_get_emission_base_velocity(RID p_particles) const {
  399. return rasterizer->particles_get_emission_base_velocity(p_particles);
  400. }
  401. void VisualServerRaster::particles_set_emission_points(RID p_particles, const DVector<Vector3>& p_points) {
  402. VS_CHANGED;
  403. rasterizer->particles_set_emission_points(p_particles,p_points);
  404. }
  405. DVector<Vector3> VisualServerRaster::particles_get_emission_points(RID p_particles) const {
  406. return rasterizer->particles_get_emission_points(p_particles);
  407. }
  408. void VisualServerRaster::particles_set_gravity_normal(RID p_particles, const Vector3& p_normal) {
  409. VS_CHANGED;
  410. rasterizer->particles_set_gravity_normal(p_particles,p_normal);
  411. }
  412. Vector3 VisualServerRaster::particles_get_gravity_normal(RID p_particles) const {
  413. return rasterizer->particles_get_gravity_normal(p_particles);
  414. }
  415. void VisualServerRaster::particles_set_variable(RID p_particles, ParticleVariable p_variable,float p_value) {
  416. VS_CHANGED;
  417. rasterizer->particles_set_variable(p_particles,p_variable,p_value);
  418. }
  419. float VisualServerRaster::particles_get_variable(RID p_particles, ParticleVariable p_variable) const {
  420. return rasterizer->particles_get_variable(p_particles,p_variable);
  421. }
  422. void VisualServerRaster::particles_set_randomness(RID p_particles, ParticleVariable p_variable,float p_randomness) {
  423. VS_CHANGED;
  424. rasterizer->particles_set_randomness(p_particles,p_variable,p_randomness);
  425. }
  426. float VisualServerRaster::particles_get_randomness(RID p_particles, ParticleVariable p_variable) const {
  427. return rasterizer->particles_get_randomness(p_particles,p_variable);
  428. }
  429. void VisualServerRaster::particles_set_color_phases(RID p_particles, int p_phases) {
  430. VS_CHANGED;
  431. rasterizer->particles_set_color_phases(p_particles,p_phases);
  432. }
  433. int VisualServerRaster::particles_get_color_phases(RID p_particles) const {
  434. return rasterizer->particles_get_color_phases(p_particles);
  435. }
  436. void VisualServerRaster::particles_set_color_phase_pos(RID p_particles, int p_phase, float p_pos) {
  437. VS_CHANGED;
  438. rasterizer->particles_set_color_phase_pos(p_particles,p_phase,p_pos);
  439. }
  440. float VisualServerRaster::particles_get_color_phase_pos(RID p_particles, int p_phase) const {
  441. return rasterizer->particles_get_color_phase_pos(p_particles,p_phase);
  442. }
  443. void VisualServerRaster::particles_set_attractors(RID p_particles, int p_attractors) {
  444. VS_CHANGED;
  445. rasterizer->particles_set_attractors(p_particles,p_attractors);
  446. }
  447. int VisualServerRaster::particles_get_attractors(RID p_particles) const {
  448. return rasterizer->particles_get_attractors(p_particles);
  449. }
  450. void VisualServerRaster::particles_set_attractor_pos(RID p_particles, int p_attractor, const Vector3& p_pos) {
  451. VS_CHANGED;
  452. rasterizer->particles_set_attractor_pos(p_particles,p_attractor,p_pos);
  453. }
  454. Vector3 VisualServerRaster::particles_get_attractor_pos(RID p_particles,int p_attractor) const {
  455. return rasterizer->particles_get_attractor_pos(p_particles,p_attractor);
  456. }
  457. void VisualServerRaster::particles_set_attractor_strength(RID p_particles, int p_attractor, float p_force) {
  458. VS_CHANGED;
  459. rasterizer->particles_set_attractor_strength(p_particles,p_attractor,p_force);
  460. }
  461. float VisualServerRaster::particles_get_attractor_strength(RID p_particles,int p_attractor) const {
  462. return rasterizer->particles_get_attractor_strength(p_particles,p_attractor);
  463. }
  464. void VisualServerRaster::particles_set_color_phase_color(RID p_particles, int p_phase, const Color& p_color) {
  465. VS_CHANGED;
  466. rasterizer->particles_set_color_phase_color(p_particles,p_phase,p_color);
  467. }
  468. Color VisualServerRaster::particles_get_color_phase_color(RID p_particles, int p_phase) const {
  469. return rasterizer->particles_get_color_phase_color(p_particles,p_phase);
  470. }
  471. void VisualServerRaster::particles_set_material(RID p_particles, RID p_material,bool p_owned) {
  472. VS_CHANGED;
  473. rasterizer->particles_set_material(p_particles,p_material,p_owned);
  474. }
  475. RID VisualServerRaster::particles_get_material(RID p_particles) const {
  476. return rasterizer->particles_get_material(p_particles);
  477. }
  478. void VisualServerRaster::particles_set_height_from_velocity(RID p_particles, bool p_enable) {
  479. VS_CHANGED;
  480. rasterizer->particles_set_height_from_velocity(p_particles,p_enable);
  481. }
  482. bool VisualServerRaster::particles_has_height_from_velocity(RID p_particles) const {
  483. return rasterizer->particles_has_height_from_velocity(p_particles);
  484. }
  485. void VisualServerRaster::particles_set_use_local_coordinates(RID p_particles, bool p_enable) {
  486. rasterizer->particles_set_use_local_coordinates(p_particles,p_enable);
  487. }
  488. bool VisualServerRaster::particles_is_using_local_coordinates(RID p_particles) const {
  489. return rasterizer->particles_is_using_local_coordinates(p_particles);
  490. }
  491. /* Light API */
  492. RID VisualServerRaster::light_create(LightType p_type) {
  493. return rasterizer->light_create(p_type);
  494. }
  495. VisualServer::LightType VisualServerRaster::light_get_type(RID p_light) const {
  496. return rasterizer->light_get_type(p_light);
  497. }
  498. void VisualServerRaster::light_set_color(RID p_light,LightColor p_type, const Color& p_color) {
  499. VS_CHANGED;
  500. rasterizer->light_set_color(p_light,p_type,p_color);
  501. }
  502. Color VisualServerRaster::light_get_color(RID p_light,LightColor p_type) const {
  503. return rasterizer->light_get_color(p_light,p_type);
  504. }
  505. void VisualServerRaster::light_set_shadow(RID p_light,bool p_enabled) {
  506. VS_CHANGED;
  507. rasterizer->light_set_shadow(p_light,p_enabled);
  508. }
  509. bool VisualServerRaster::light_has_shadow(RID p_light) const {
  510. return rasterizer->light_has_shadow(p_light);
  511. }
  512. void VisualServerRaster::light_set_volumetric(RID p_light,bool p_enabled) {
  513. VS_CHANGED;
  514. rasterizer->light_set_volumetric(p_light,p_enabled);
  515. }
  516. bool VisualServerRaster::light_is_volumetric(RID p_light) const {
  517. return rasterizer->light_is_volumetric(p_light);
  518. }
  519. void VisualServerRaster::light_set_projector(RID p_light,RID p_texture) {
  520. VS_CHANGED;
  521. rasterizer->light_set_projector(p_light,p_texture);
  522. }
  523. RID VisualServerRaster::light_get_projector(RID p_light) const {
  524. return rasterizer->light_get_projector(p_light);
  525. }
  526. void VisualServerRaster::light_set_param(RID p_light, LightParam p_var, float p_value) {
  527. VS_CHANGED;
  528. rasterizer->light_set_var(p_light,p_var,p_value);
  529. _dependency_queue_update(p_light,true);
  530. }
  531. float VisualServerRaster::light_get_param(RID p_light, LightParam p_var) const {
  532. return rasterizer->light_get_var(p_light,p_var);
  533. }
  534. void VisualServerRaster::light_set_operator(RID p_light,LightOp p_op) {
  535. VS_CHANGED;
  536. rasterizer->light_set_operator(p_light,p_op);
  537. }
  538. VisualServerRaster::LightOp VisualServerRaster::light_get_operator(RID p_light) const {
  539. return rasterizer->light_get_operator(p_light);
  540. }
  541. void VisualServerRaster::light_omni_set_shadow_mode(RID p_light,LightOmniShadowMode p_mode) {
  542. VS_CHANGED;
  543. rasterizer->light_omni_set_shadow_mode(p_light,p_mode);
  544. }
  545. VisualServerRaster::LightOmniShadowMode VisualServerRaster::light_omni_get_shadow_mode(RID p_light) const {
  546. return rasterizer->light_omni_get_shadow_mode(p_light);
  547. }
  548. void VisualServerRaster::light_directional_set_shadow_mode(RID p_light,LightDirectionalShadowMode p_mode){
  549. VS_CHANGED;
  550. rasterizer->light_directional_set_shadow_mode(p_light,p_mode);
  551. }
  552. VS::LightDirectionalShadowMode VisualServerRaster::light_directional_get_shadow_mode(RID p_light) const{
  553. return rasterizer->light_directional_get_shadow_mode(p_light);
  554. }
  555. void VisualServerRaster::light_directional_set_shadow_param(RID p_light,LightDirectionalShadowParam p_param, float p_value) {
  556. VS_CHANGED;
  557. rasterizer->light_directional_set_shadow_param(p_light,p_param,p_value);
  558. }
  559. float VisualServerRaster::light_directional_get_shadow_param(RID p_light,LightDirectionalShadowParam p_param) const {
  560. return rasterizer->light_directional_get_shadow_param(p_light,p_param);
  561. }
  562. RID VisualServerRaster::skeleton_create() {
  563. return rasterizer->skeleton_create();
  564. }
  565. void VisualServerRaster::skeleton_resize(RID p_skeleton,int p_bones) {
  566. VS_CHANGED;
  567. rasterizer->skeleton_resize(p_skeleton,p_bones);
  568. }
  569. int VisualServerRaster::skeleton_get_bone_count(RID p_skeleton) const {
  570. return rasterizer->skeleton_get_bone_count(p_skeleton);
  571. }
  572. void VisualServerRaster::skeleton_bone_set_transform(RID p_skeleton,int p_bone, const Transform& p_transform) {
  573. VS_CHANGED;
  574. return rasterizer->skeleton_bone_set_transform(p_skeleton,p_bone,p_transform);
  575. }
  576. Transform VisualServerRaster::skeleton_bone_get_transform(RID p_skeleton,int p_bone) {
  577. return rasterizer->skeleton_bone_get_transform(p_skeleton,p_bone);
  578. }
  579. /* VISIBILITY API */
  580. /* ROOM API */
  581. RID VisualServerRaster::room_create() {
  582. Room *room = memnew( Room );
  583. ERR_FAIL_COND_V(!room,RID());
  584. return room_owner.make_rid( room );
  585. }
  586. void VisualServerRaster::room_set_bounds(RID p_room, const BSP_Tree& p_bounds) {
  587. VS_CHANGED;
  588. Room *room = room_owner.get(p_room);
  589. ERR_FAIL_COND(!room);
  590. room->bounds=p_bounds;
  591. _dependency_queue_update(p_room,true);
  592. }
  593. BSP_Tree VisualServerRaster::room_get_bounds(RID p_room) const {
  594. Room *room = room_owner.get(p_room);
  595. ERR_FAIL_COND_V(!room, BSP_Tree());
  596. return room->bounds;
  597. }
  598. /* PORTAL API */
  599. RID VisualServerRaster::portal_create() {
  600. VS_CHANGED;
  601. Portal *portal = memnew( Portal );
  602. ERR_FAIL_COND_V(!portal,RID());
  603. return portal_owner.make_rid( portal );
  604. }
  605. void VisualServerRaster::portal_set_shape(RID p_portal, const Vector<Point2>& p_shape) {
  606. VS_CHANGED;
  607. Portal *portal = portal_owner.get(p_portal);
  608. ERR_FAIL_COND(!portal);
  609. portal->shape=p_shape;
  610. portal->bounds=Rect2();
  611. for(int i=0;i<p_shape.size();i++) {
  612. if (i==0)
  613. portal->bounds.pos=p_shape[i];
  614. else
  615. portal->bounds.expand_to(p_shape[i]);
  616. }
  617. _dependency_queue_update(p_portal,true);
  618. }
  619. Vector<Point2> VisualServerRaster::portal_get_shape(RID p_portal) const {
  620. Portal *portal = portal_owner.get(p_portal);
  621. ERR_FAIL_COND_V(!portal, Vector<Point2>());
  622. return portal->shape;
  623. }
  624. void VisualServerRaster::portal_set_enabled(RID p_portal, bool p_enabled) {
  625. VS_CHANGED;
  626. Portal *portal = portal_owner.get(p_portal);
  627. ERR_FAIL_COND(!portal);
  628. portal->enabled=p_enabled;
  629. }
  630. bool VisualServerRaster::portal_is_enabled(RID p_portal) const {
  631. Portal *portal = portal_owner.get(p_portal);
  632. ERR_FAIL_COND_V(!portal, false);
  633. return portal->enabled;
  634. }
  635. void VisualServerRaster::portal_set_disable_distance(RID p_portal, float p_distance) {
  636. VS_CHANGED;
  637. Portal *portal = portal_owner.get(p_portal);
  638. ERR_FAIL_COND(!portal);
  639. portal->disable_distance=p_distance;
  640. }
  641. float VisualServerRaster::portal_get_disable_distance(RID p_portal) const {
  642. Portal *portal = portal_owner.get(p_portal);
  643. ERR_FAIL_COND_V(!portal, -1);
  644. return portal->disable_distance;
  645. }
  646. void VisualServerRaster::portal_set_disabled_color(RID p_portal, const Color& p_color) {
  647. VS_CHANGED;
  648. Portal *portal = portal_owner.get(p_portal);
  649. ERR_FAIL_COND(!portal);
  650. portal->disable_color=p_color;
  651. }
  652. Color VisualServerRaster::portal_get_disabled_color(RID p_portal) const {
  653. Portal *portal = portal_owner.get(p_portal);
  654. ERR_FAIL_COND_V(!portal, Color());
  655. return portal->disable_color;
  656. }
  657. void VisualServerRaster::portal_set_connect_range(RID p_portal, float p_range) {
  658. VS_CHANGED;
  659. Portal *portal = portal_owner.get(p_portal);
  660. ERR_FAIL_COND(!portal);
  661. portal->connect_range=p_range;
  662. _dependency_queue_update(p_portal,true);
  663. }
  664. float VisualServerRaster::portal_get_connect_range(RID p_portal) const {
  665. Portal *portal = portal_owner.get(p_portal);
  666. ERR_FAIL_COND_V(!portal,0);
  667. return portal->connect_range;
  668. }
  669. RID VisualServerRaster::baked_light_create() {
  670. BakedLight *baked_light = memnew( BakedLight );
  671. ERR_FAIL_COND_V(!baked_light,RID());
  672. baked_light->data.mode=BAKED_LIGHT_OCTREE;
  673. baked_light->data.octree_lattice_size=0;
  674. baked_light->data.octree_lattice_divide=0;
  675. baked_light->data.octree_steps=1;
  676. baked_light->data.lightmap_multiplier=1.0;
  677. return baked_light_owner.make_rid( baked_light );
  678. }
  679. void VisualServerRaster::baked_light_set_mode(RID p_baked_light,BakedLightMode p_mode){
  680. VS_CHANGED;
  681. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  682. ERR_FAIL_COND(!baked_light);
  683. baked_light->data.mode=p_mode;
  684. baked_light->data.color_multiplier=1.0;
  685. _dependency_queue_update(p_baked_light,true);
  686. }
  687. VisualServer::BakedLightMode VisualServerRaster::baked_light_get_mode(RID p_baked_light) const{
  688. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  689. ERR_FAIL_COND_V(!baked_light,BAKED_LIGHT_OCTREE);
  690. return baked_light->data.mode;
  691. }
  692. void VisualServerRaster::baked_light_set_lightmap_multiplier(RID p_baked_light,float p_multiplier) {
  693. VS_CHANGED;
  694. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  695. ERR_FAIL_COND(!baked_light);
  696. baked_light->data.lightmap_multiplier=p_multiplier;
  697. }
  698. float VisualServerRaster::baked_light_get_lightmap_multiplier(RID p_baked_light) const{
  699. const BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  700. ERR_FAIL_COND_V(!baked_light,0);
  701. return baked_light->data.lightmap_multiplier;
  702. }
  703. void VisualServerRaster::baked_light_set_octree(RID p_baked_light,const DVector<uint8_t> p_octree){
  704. VS_CHANGED;
  705. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  706. ERR_FAIL_COND(!baked_light);
  707. if (p_octree.size()==0) {
  708. if (baked_light->data.octree_texture.is_valid())
  709. rasterizer->free(baked_light->data.octree_texture);
  710. baked_light->data.octree_texture=RID();
  711. baked_light->octree_aabb=AABB();
  712. baked_light->octree_tex_size=Size2();
  713. } else {
  714. int tex_w;
  715. int tex_h;
  716. bool is16;
  717. {
  718. DVector<uint8_t>::Read r=p_octree.read();
  719. tex_w = decode_uint32(&r[0]);
  720. tex_h = decode_uint32(&r[4]);
  721. print_line("TEX W: "+itos(tex_w)+" TEX H:"+itos(tex_h)+" LEN: "+itos(p_octree.size()));
  722. is16=decode_uint32(&r[8]);
  723. baked_light->data.octree_lattice_size=decode_float(&r[12]);
  724. baked_light->data.octree_lattice_divide=tex_w/4.0;
  725. print_line("LATTICE SIZE: "+rtos(baked_light->data.octree_lattice_size));
  726. print_line("LATTICE DIVIDE: "+rtos(baked_light->data.octree_lattice_divide));
  727. baked_light->data.octree_steps=decode_uint32(&r[16]);
  728. baked_light->data.octree_tex_pixel_size.x=1.0/tex_w;
  729. baked_light->data.octree_tex_pixel_size.y=1.0/tex_h;
  730. baked_light->data.texture_multiplier=decode_uint32(&r[20]);
  731. baked_light->octree_aabb.pos.x=decode_float(&r[32]);
  732. baked_light->octree_aabb.pos.y=decode_float(&r[36]);
  733. baked_light->octree_aabb.pos.z=decode_float(&r[40]);
  734. baked_light->octree_aabb.size.x=decode_float(&r[44]);
  735. baked_light->octree_aabb.size.y=decode_float(&r[48]);
  736. baked_light->octree_aabb.size.z=decode_float(&r[52]);
  737. }
  738. if (baked_light->data.octree_texture.is_valid()) {
  739. if (tex_w!=baked_light->octree_tex_size.x || tex_h!=baked_light->octree_tex_size.y) {
  740. rasterizer->free(baked_light->data.octree_texture);
  741. baked_light->data.octree_texture=RID();
  742. }
  743. }
  744. if (!baked_light->data.octree_texture.is_valid()) {
  745. baked_light->data.octree_texture=rasterizer->texture_create();
  746. rasterizer->texture_allocate(baked_light->data.octree_texture,tex_w,tex_h,Image::FORMAT_RGBA,TEXTURE_FLAG_FILTER);
  747. }
  748. Image img(tex_w,tex_h,0,Image::FORMAT_RGBA,p_octree);
  749. rasterizer->texture_set_data(baked_light->data.octree_texture,img);
  750. }
  751. _dependency_queue_update(p_baked_light,true);
  752. }
  753. DVector<uint8_t> VisualServerRaster::baked_light_get_octree(RID p_baked_light) const{
  754. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  755. ERR_FAIL_COND_V(!baked_light,DVector<uint8_t>());
  756. if (rasterizer->is_texture(baked_light->data.octree_texture)) {
  757. Image img = rasterizer->texture_get_data(baked_light->data.octree_texture);
  758. return img.get_data();
  759. } else {
  760. return DVector<uint8_t>();
  761. }
  762. }
  763. void VisualServerRaster::baked_light_add_lightmap(RID p_baked_light,const RID p_texture,int p_id){
  764. VS_CHANGED;
  765. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  766. ERR_FAIL_COND(!baked_light);
  767. baked_light->data.lightmaps.insert(p_id,p_texture);
  768. }
  769. void VisualServerRaster::baked_light_clear_lightmaps(RID p_baked_light){
  770. VS_CHANGED;
  771. BakedLight *baked_light = baked_light_owner.get(p_baked_light);
  772. ERR_FAIL_COND(!baked_light);
  773. baked_light->data.lightmaps.clear();
  774. }
  775. /* CAMERA API */
  776. RID VisualServerRaster::camera_create() {
  777. Camera * camera = memnew( Camera );
  778. return camera_owner.make_rid( camera );
  779. }
  780. void VisualServerRaster::camera_set_perspective(RID p_camera,float p_fovy_degrees, float p_z_near, float p_z_far) {
  781. VS_CHANGED;
  782. Camera *camera = camera_owner.get( p_camera );
  783. ERR_FAIL_COND(!camera);
  784. camera->type=Camera::PERSPECTIVE;
  785. camera->fov=p_fovy_degrees;
  786. camera->znear=p_z_near;
  787. camera->zfar=p_z_far;
  788. }
  789. void VisualServerRaster::camera_set_orthogonal(RID p_camera,float p_size, float p_z_near, float p_z_far) {
  790. VS_CHANGED;
  791. Camera *camera = camera_owner.get( p_camera );
  792. ERR_FAIL_COND(!camera);
  793. camera->type=Camera::ORTHOGONAL;
  794. camera->size=p_size;
  795. camera->znear=p_z_near;
  796. camera->zfar=p_z_far;
  797. }
  798. void VisualServerRaster::camera_set_transform(RID p_camera,const Transform& p_transform) {
  799. VS_CHANGED;
  800. Camera *camera = camera_owner.get( p_camera );
  801. ERR_FAIL_COND(!camera);
  802. camera->transform=p_transform;
  803. }
  804. void VisualServerRaster::camera_set_visible_layers(RID p_camera,uint32_t p_layers) {
  805. VS_CHANGED;
  806. Camera *camera = camera_owner.get( p_camera );
  807. ERR_FAIL_COND(!camera);
  808. camera->visible_layers=p_layers;
  809. }
  810. uint32_t VisualServerRaster::camera_get_visible_layers(RID p_camera) const{
  811. const Camera *camera = camera_owner.get( p_camera );
  812. ERR_FAIL_COND_V(!camera,0);
  813. return camera->visible_layers;
  814. }
  815. void VisualServerRaster::camera_set_environment(RID p_camera,RID p_env) {
  816. Camera *camera = camera_owner.get( p_camera );
  817. ERR_FAIL_COND(!camera);
  818. camera->env=p_env;
  819. }
  820. RID VisualServerRaster::camera_get_environment(RID p_camera) const {
  821. const Camera *camera = camera_owner.get( p_camera );
  822. ERR_FAIL_COND_V(!camera,RID());
  823. return camera->env;
  824. }
  825. void VisualServerRaster::camera_set_use_vertical_aspect(RID p_camera,bool p_enable) {
  826. Camera *camera = camera_owner.get( p_camera );
  827. ERR_FAIL_COND(!camera);
  828. camera->vaspect=p_enable;
  829. }
  830. bool VisualServerRaster::camera_is_using_vertical_aspect(RID p_camera,bool p_enable) const{
  831. const Camera *camera = camera_owner.get( p_camera );
  832. ERR_FAIL_COND_V(!camera,false);
  833. return camera->vaspect;
  834. }
  835. /* VIEWPORT API */
  836. RID VisualServerRaster::viewport_create() {
  837. Viewport *viewport = memnew( Viewport );
  838. RID rid = viewport_owner.make_rid( viewport );
  839. ERR_FAIL_COND_V( !rid.is_valid(), rid );
  840. viewport->self=rid;
  841. viewport->hide_scenario=false;
  842. viewport->hide_canvas=false;
  843. viewport->viewport_data=rasterizer->viewport_data_create();
  844. return rid;
  845. }
  846. void VisualServerRaster::viewport_attach_to_screen(RID p_viewport,int p_screen) {
  847. VS_CHANGED;
  848. Viewport *viewport = viewport_owner.get( p_viewport );
  849. ERR_FAIL_COND(!viewport);
  850. screen_viewports[p_viewport]=p_screen;
  851. }
  852. void VisualServerRaster::viewport_detach(RID p_viewport) {
  853. VS_CHANGED;
  854. Viewport *viewport = viewport_owner.get( p_viewport );
  855. ERR_FAIL_COND(!viewport);
  856. ERR_FAIL_COND(!screen_viewports.has(p_viewport));
  857. screen_viewports.erase(p_viewport);
  858. }
  859. void VisualServerRaster::viewport_set_as_render_target(RID p_viewport,bool p_enable) {
  860. VS_CHANGED;
  861. Viewport *viewport = viewport_owner.get( p_viewport );
  862. ERR_FAIL_COND(!viewport);
  863. if (viewport->render_target.is_valid()==p_enable)
  864. return;
  865. if (!p_enable) {
  866. rasterizer->free(viewport->render_target);
  867. viewport->render_target=RID();
  868. viewport->render_target_texture=RID();
  869. if (viewport->update_list.in_list())
  870. viewport_update_list.remove(&viewport->update_list);
  871. } else {
  872. viewport->render_target=rasterizer->render_target_create();
  873. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  874. viewport->render_target_texture=rasterizer->render_target_get_texture(viewport->render_target);
  875. if (viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  876. viewport_update_list.add(&viewport->update_list);
  877. }
  878. }
  879. void VisualServerRaster::viewport_set_render_target_update_mode(RID p_viewport,RenderTargetUpdateMode p_mode){
  880. VS_CHANGED;
  881. Viewport *viewport = viewport_owner.get( p_viewport );
  882. ERR_FAIL_COND(!viewport);
  883. if (viewport->render_target.is_valid() && viewport->update_list.in_list())
  884. viewport_update_list.remove(&viewport->update_list);
  885. viewport->render_target_update_mode=p_mode;
  886. if (viewport->render_target.is_valid() &&viewport->render_target_update_mode!=RENDER_TARGET_UPDATE_DISABLED)
  887. viewport_update_list.add(&viewport->update_list);
  888. }
  889. VisualServer::RenderTargetUpdateMode VisualServerRaster::viewport_get_render_target_update_mode(RID p_viewport) const{
  890. const Viewport *viewport = viewport_owner.get( p_viewport );
  891. ERR_FAIL_COND_V(!viewport,RENDER_TARGET_UPDATE_DISABLED);
  892. return viewport->render_target_update_mode;
  893. }
  894. RID VisualServerRaster::viewport_get_render_target_texture(RID p_viewport) const{
  895. Viewport *viewport = viewport_owner.get( p_viewport );
  896. ERR_FAIL_COND_V(!viewport,RID());
  897. return viewport->render_target_texture;
  898. }
  899. void VisualServerRaster::viewport_set_render_target_vflip(RID p_viewport,bool p_enable) {
  900. Viewport *viewport = viewport_owner.get( p_viewport );
  901. ERR_FAIL_COND(!viewport);
  902. viewport->render_target_vflip=p_enable;
  903. }
  904. void VisualServerRaster::viewport_set_render_target_to_screen_rect(RID p_viewport,const Rect2& p_rect) {
  905. Viewport *viewport = viewport_owner.get( p_viewport );
  906. ERR_FAIL_COND(!viewport);
  907. viewport->rt_to_screen_rect=p_rect;
  908. }
  909. bool VisualServerRaster::viewport_get_render_target_vflip(RID p_viewport) const{
  910. const Viewport *viewport = viewport_owner.get( p_viewport );
  911. ERR_FAIL_COND_V(!viewport,false);
  912. return viewport->render_target_vflip;
  913. }
  914. void VisualServerRaster::viewport_queue_screen_capture(RID p_viewport) {
  915. VS_CHANGED;
  916. Viewport *viewport = viewport_owner.get( p_viewport );
  917. ERR_FAIL_COND(!viewport);
  918. viewport->queue_capture=true;
  919. }
  920. Image VisualServerRaster::viewport_get_screen_capture(RID p_viewport) const {
  921. Viewport *viewport = (Viewport*)viewport_owner.get( p_viewport );
  922. ERR_FAIL_COND_V(!viewport,Image());
  923. Image ret = viewport->capture;
  924. viewport->capture=Image();
  925. return ret;
  926. }
  927. void VisualServerRaster::viewport_set_rect(RID p_viewport,const ViewportRect& p_rect) {
  928. VS_CHANGED;
  929. Viewport *viewport=NULL;
  930. viewport = viewport_owner.get( p_viewport );
  931. ERR_FAIL_COND(!viewport);
  932. viewport->rect=p_rect;
  933. if (viewport->render_target.is_valid()) {
  934. rasterizer->render_target_set_size(viewport->render_target,viewport->rect.width,viewport->rect.height);
  935. }
  936. }
  937. VisualServer::ViewportRect VisualServerRaster::viewport_get_rect(RID p_viewport) const {
  938. const Viewport *viewport=NULL;
  939. viewport = viewport_owner.get( p_viewport );
  940. ERR_FAIL_COND_V(!viewport, ViewportRect());
  941. return viewport->rect;
  942. }
  943. void VisualServerRaster::viewport_set_hide_scenario(RID p_viewport,bool p_hide) {
  944. VS_CHANGED;
  945. Viewport *viewport=NULL;
  946. viewport = viewport_owner.get( p_viewport );
  947. ERR_FAIL_COND(!viewport);
  948. viewport->hide_scenario=p_hide;
  949. }
  950. void VisualServerRaster::viewport_set_hide_canvas(RID p_viewport,bool p_hide) {
  951. VS_CHANGED;
  952. Viewport *viewport=NULL;
  953. viewport = viewport_owner.get( p_viewport );
  954. ERR_FAIL_COND(!viewport);
  955. viewport->hide_canvas=p_hide;
  956. }
  957. void VisualServerRaster::viewport_attach_camera(RID p_viewport,RID p_camera) {
  958. VS_CHANGED;
  959. Viewport *viewport=NULL;
  960. viewport = viewport_owner.get( p_viewport );
  961. ERR_FAIL_COND(!viewport);
  962. if (p_camera.is_valid()) {
  963. ERR_FAIL_COND(!camera_owner.owns(p_camera));
  964. // a camera
  965. viewport->camera=p_camera;
  966. } else {
  967. viewport->camera=RID();
  968. }
  969. }
  970. void VisualServerRaster::viewport_set_scenario(RID p_viewport,RID p_scenario) {
  971. VS_CHANGED;
  972. Viewport *viewport=NULL;
  973. viewport = viewport_owner.get( p_viewport );
  974. ERR_FAIL_COND(!viewport);
  975. if (p_scenario.is_valid()) {
  976. ERR_FAIL_COND(!scenario_owner.owns(p_scenario));
  977. // a camera
  978. viewport->scenario=p_scenario;
  979. } else {
  980. viewport->scenario=RID();
  981. }
  982. }
  983. RID VisualServerRaster::viewport_get_attached_camera(RID p_viewport) const {
  984. const Viewport *viewport=NULL;
  985. viewport = viewport_owner.get( p_viewport );
  986. ERR_FAIL_COND_V(!viewport, RID());
  987. return viewport->camera;
  988. }
  989. void VisualServerRaster::viewport_attach_canvas(RID p_viewport,RID p_canvas) {
  990. VS_CHANGED;
  991. Viewport *viewport=NULL;
  992. viewport = viewport_owner.get( p_viewport );
  993. ERR_FAIL_COND(!viewport);
  994. Canvas *canvas = canvas_owner.get( p_canvas );
  995. ERR_FAIL_COND(!canvas);
  996. ERR_EXPLAIN("Canvas already attached.");
  997. ERR_FAIL_COND(viewport->canvas_map.has(p_canvas));
  998. Viewport::CanvasData cd;
  999. cd.canvas=canvas;
  1000. cd.layer=0;
  1001. viewport->canvas_map[p_canvas]=cd;
  1002. canvas->viewports.insert(p_viewport);
  1003. }
  1004. void VisualServerRaster::viewport_set_canvas_transform(RID p_viewport,RID p_canvas,const Matrix32& p_transform) {
  1005. VS_CHANGED;
  1006. Viewport *viewport=NULL;
  1007. viewport = viewport_owner.get( p_viewport );
  1008. ERR_FAIL_COND(!viewport);
  1009. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1010. if (!E) {
  1011. ERR_EXPLAIN("Viewport does not contain the canvas");
  1012. ERR_FAIL_COND(!E);
  1013. }
  1014. E->get().transform=p_transform;
  1015. }
  1016. Matrix32 VisualServerRaster::viewport_get_canvas_transform(RID p_viewport,RID p_canvas) const {
  1017. Viewport *viewport=NULL;
  1018. viewport = viewport_owner.get( p_viewport );
  1019. ERR_FAIL_COND_V(!viewport,Matrix32());
  1020. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1021. if (!E) {
  1022. ERR_EXPLAIN("Viewport does not contain the canvas");
  1023. ERR_FAIL_COND_V(!E,Matrix32());
  1024. }
  1025. return E->get().transform;
  1026. }
  1027. void VisualServerRaster::viewport_set_global_canvas_transform(RID p_viewport,const Matrix32& p_transform) {
  1028. VS_CHANGED
  1029. Viewport *viewport=NULL;
  1030. viewport = viewport_owner.get( p_viewport );
  1031. ERR_FAIL_COND(!viewport);
  1032. viewport->global_transform=p_transform;
  1033. }
  1034. Matrix32 VisualServerRaster::viewport_get_global_canvas_transform(RID p_viewport) const{
  1035. Viewport *viewport=NULL;
  1036. viewport = viewport_owner.get( p_viewport );
  1037. ERR_FAIL_COND_V(!viewport,Matrix32());
  1038. return viewport->global_transform;
  1039. }
  1040. void VisualServerRaster::viewport_remove_canvas(RID p_viewport,RID p_canvas) {
  1041. VS_CHANGED;
  1042. Viewport *viewport=NULL;
  1043. viewport = viewport_owner.get( p_viewport );
  1044. ERR_FAIL_COND(!viewport);
  1045. Canvas *canvas = canvas_owner.get( p_canvas );
  1046. ERR_FAIL_COND(!canvas);
  1047. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1048. if (!E) {
  1049. ERR_EXPLAIN("Viewport does not contain the canvas");
  1050. ERR_FAIL_COND(!E);
  1051. }
  1052. canvas->viewports.erase(p_viewport);
  1053. viewport->canvas_map.erase(E);
  1054. }
  1055. void VisualServerRaster::viewport_set_canvas_layer(RID p_viewport,RID p_canvas,int p_layer) {
  1056. VS_CHANGED;
  1057. Viewport *viewport=NULL;
  1058. viewport = viewport_owner.get( p_viewport );
  1059. ERR_FAIL_COND(!viewport);
  1060. Map<RID,Viewport::CanvasData>::Element *E=viewport->canvas_map.find(p_canvas);
  1061. if (!E) {
  1062. ERR_EXPLAIN("Viewport does not contain the canvas");
  1063. ERR_FAIL_COND(!E);
  1064. }
  1065. E->get().layer=p_layer;
  1066. }
  1067. void VisualServerRaster::viewport_set_transparent_background(RID p_viewport,bool p_enabled) {
  1068. VS_CHANGED;
  1069. Viewport *viewport=viewport_owner.get( p_viewport );
  1070. ERR_FAIL_COND(!viewport);
  1071. viewport->transparent_bg=p_enabled;
  1072. }
  1073. bool VisualServerRaster::viewport_has_transparent_background(RID p_viewport) const {
  1074. Viewport *viewport=viewport_owner.get( p_viewport );
  1075. ERR_FAIL_COND_V(!viewport, false);
  1076. return viewport->transparent_bg;
  1077. }
  1078. RID VisualServerRaster::viewport_get_scenario(RID p_viewport) const {
  1079. const Viewport *viewport=NULL;
  1080. viewport = viewport_owner.get( p_viewport );
  1081. ERR_FAIL_COND_V(!viewport, RID());
  1082. return viewport->scenario;
  1083. }
  1084. RID VisualServerRaster::environment_create() {
  1085. return rasterizer->environment_create();
  1086. }
  1087. void VisualServerRaster::environment_set_background(RID p_env,EnvironmentBG p_bg){
  1088. rasterizer->environment_set_background(p_env,p_bg);
  1089. }
  1090. VisualServer::EnvironmentBG VisualServerRaster::environment_get_background(RID p_env) const{
  1091. return rasterizer->environment_get_background(p_env);
  1092. }
  1093. void VisualServerRaster::environment_set_background_param(RID p_env,EnvironmentBGParam p_param, const Variant& p_value){
  1094. rasterizer->environment_set_background_param(p_env,p_param,p_value);
  1095. }
  1096. Variant VisualServerRaster::environment_get_background_param(RID p_env,EnvironmentBGParam p_param) const{
  1097. return rasterizer->environment_get_background_param(p_env,p_param);
  1098. }
  1099. void VisualServerRaster::environment_set_enable_fx(RID p_env,EnvironmentFx p_effect,bool p_enabled){
  1100. rasterizer->environment_set_enable_fx(p_env,p_effect,p_enabled);
  1101. }
  1102. bool VisualServerRaster::environment_is_fx_enabled(RID p_env,EnvironmentFx p_effect) const{
  1103. return rasterizer->environment_is_fx_enabled(p_env,p_effect);
  1104. }
  1105. void VisualServerRaster::environment_fx_set_param(RID p_env,EnvironmentFxParam p_param,const Variant& p_value){
  1106. rasterizer->environment_fx_set_param(p_env,p_param,p_value);
  1107. }
  1108. Variant VisualServerRaster::environment_fx_get_param(RID p_env,EnvironmentFxParam p_param) const {
  1109. return environment_fx_get_param(p_env,p_param);
  1110. }
  1111. /* SCENARIO API */
  1112. void VisualServerRaster::_dependency_queue_update(RID p_rid,bool p_update_aabb) {
  1113. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  1114. if (!E)
  1115. return;
  1116. Set<RID>::Element *I = E->get().front();
  1117. while(I) {
  1118. Instance *ins = instance_owner.get( I->get() );
  1119. _instance_queue_update( ins , p_update_aabb );
  1120. I = I->next();
  1121. }
  1122. }
  1123. void VisualServerRaster::_instance_queue_update(Instance *p_instance,bool p_update_aabb) {
  1124. if (p_update_aabb)
  1125. p_instance->update_aabb=true;
  1126. if (p_instance->update)
  1127. return;
  1128. p_instance->update_next=instance_update_list;
  1129. instance_update_list=p_instance;
  1130. p_instance->update=true;
  1131. }
  1132. RID VisualServerRaster::scenario_create() {
  1133. Scenario *scenario = memnew( Scenario );
  1134. ERR_FAIL_COND_V(!scenario,RID());
  1135. RID scenario_rid = scenario_owner.make_rid( scenario );
  1136. scenario->self=scenario_rid;
  1137. scenario->octree.set_pair_callback(instance_pair,this);
  1138. scenario->octree.set_unpair_callback(instance_unpair,this);
  1139. return scenario_rid;
  1140. }
  1141. void VisualServerRaster::scenario_set_debug(RID p_scenario,ScenarioDebugMode p_debug_mode) {
  1142. VS_CHANGED;
  1143. Scenario *scenario = scenario_owner.get(p_scenario);
  1144. ERR_FAIL_COND(!scenario);
  1145. scenario->debug=p_debug_mode;
  1146. }
  1147. void VisualServerRaster::scenario_set_environment(RID p_scenario, RID p_environment) {
  1148. VS_CHANGED;
  1149. Scenario *scenario = scenario_owner.get(p_scenario);
  1150. ERR_FAIL_COND(!scenario);
  1151. scenario->environment=p_environment;
  1152. }
  1153. RID VisualServerRaster::scenario_get_environment(RID p_scenario, RID p_environment) const{
  1154. const Scenario *scenario = scenario_owner.get(p_scenario);
  1155. ERR_FAIL_COND_V(!scenario,RID());
  1156. return scenario->environment;
  1157. }
  1158. /* INSTANCING API */
  1159. RID VisualServerRaster::instance_create() {
  1160. Instance *instance = memnew( Instance );
  1161. ERR_FAIL_COND_V(!instance,RID());
  1162. RID instance_rid = instance_owner.make_rid(instance);
  1163. instance->self=instance_rid;
  1164. instance->base_type=INSTANCE_NONE;
  1165. instance->scenario=NULL;
  1166. return instance_rid;
  1167. }
  1168. void VisualServerRaster::instance_set_base(RID p_instance, RID p_base) {
  1169. VS_CHANGED;
  1170. Instance *instance = instance_owner.get( p_instance );
  1171. ERR_FAIL_COND( !instance );
  1172. if (instance->base_type!=INSTANCE_NONE) {
  1173. //free anything related to that base
  1174. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( instance->base_rid );
  1175. if (E) {
  1176. // wtf, no E?
  1177. E->get().erase( instance->self );
  1178. } else {
  1179. ERR_PRINT("no base E? Bug?");
  1180. }
  1181. if ( instance->room ) {
  1182. instance_set_room(p_instance,RID());
  1183. /*
  1184. if((1<<instance->base_type)&INSTANCE_GEOMETRY_MASK)
  1185. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1186. else if (instance->base_type==INSTANCE_PORTAL) {
  1187. print_line("freeing portal, is it there? "+itos(instance->room->room_info->owned_portal_instances.(instance->RE)));
  1188. instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1189. } else if (instance->base_type==INSTANCE_ROOM)
  1190. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1191. else if (instance->base_type==INSTANCE_LIGHT)
  1192. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1193. instance->RE=NULL;*/
  1194. }
  1195. if (instance->light_info) {
  1196. if (instance->scenario && instance->light_info->D)
  1197. instance->scenario->directional_lights.erase( instance->light_info->D );
  1198. rasterizer->free(instance->light_info->instance);
  1199. memdelete(instance->light_info);
  1200. instance->light_info=NULL;
  1201. }
  1202. if (instance->portal_info) {
  1203. _portal_disconnect(instance,true);
  1204. memdelete(instance->portal_info);
  1205. instance->portal_info=NULL;
  1206. }
  1207. if (instance->baked_light_info) {
  1208. while(instance->baked_light_info->owned_instances.size()) {
  1209. Instance *owned=instance->baked_light_info->owned_instances.front()->get();
  1210. owned->baked_light=NULL;
  1211. owned->data.baked_light=NULL;
  1212. owned->data.baked_light_octree_xform=NULL;
  1213. owned->BLE=NULL;
  1214. instance->baked_light_info->owned_instances.pop_front();
  1215. }
  1216. memdelete(instance->baked_light_info);
  1217. instance->baked_light_info=NULL;
  1218. }
  1219. if (instance->scenario && instance->octree_id) {
  1220. instance->scenario->octree.erase( instance->octree_id );
  1221. instance->octree_id=0;
  1222. }
  1223. if (instance->room_info) {
  1224. for(List<Instance*>::Element *E=instance->room_info->owned_geometry_instances.front();E;E=E->next()) {
  1225. Instance *owned = E->get();
  1226. owned->room=NULL;
  1227. owned->RE=NULL;
  1228. }
  1229. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1230. _portal_disconnect(E->get(),true);
  1231. Instance *owned = E->get();
  1232. owned->room=NULL;
  1233. owned->RE=NULL;
  1234. }
  1235. for(List<Instance*>::Element *E=instance->room_info->owned_room_instances.front();E;E=E->next()) {
  1236. Instance *owned = E->get();
  1237. owned->room=NULL;
  1238. owned->RE=NULL;
  1239. }
  1240. if (instance->room_info->disconnected_child_portals.size()) {
  1241. ERR_PRINT("BUG: Disconnected portals remain!");
  1242. }
  1243. memdelete(instance->room_info);
  1244. instance->room_info=NULL;
  1245. }
  1246. if (instance->particles_info) {
  1247. rasterizer->free( instance->particles_info->instance );
  1248. memdelete(instance->particles_info);
  1249. instance->particles_info=NULL;
  1250. }
  1251. instance->data.morph_values.clear();
  1252. }
  1253. instance->base_type=INSTANCE_NONE;
  1254. instance->base_rid=RID();
  1255. if (p_base.is_valid()) {
  1256. if (rasterizer->is_mesh(p_base)) {
  1257. instance->base_type=INSTANCE_MESH;
  1258. instance->data.morph_values.resize( rasterizer->mesh_get_morph_target_count(p_base));
  1259. } else if (rasterizer->is_multimesh(p_base)) {
  1260. instance->base_type=INSTANCE_MULTIMESH;
  1261. } else if (rasterizer->is_immediate(p_base)) {
  1262. instance->base_type=INSTANCE_IMMEDIATE;
  1263. } else if (rasterizer->is_particles(p_base)) {
  1264. instance->base_type=INSTANCE_PARTICLES;
  1265. instance->particles_info=memnew( Instance::ParticlesInfo );
  1266. instance->particles_info->instance = rasterizer->particles_instance_create( p_base );
  1267. } else if (rasterizer->is_light(p_base)) {
  1268. instance->base_type=INSTANCE_LIGHT;
  1269. instance->light_info = memnew( Instance::LightInfo );
  1270. instance->light_info->instance = rasterizer->light_instance_create(p_base);
  1271. if (instance->scenario && rasterizer->light_get_type(p_base)==LIGHT_DIRECTIONAL) {
  1272. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1273. }
  1274. } else if (room_owner.owns(p_base)) {
  1275. instance->base_type=INSTANCE_ROOM;
  1276. instance->room_info = memnew( Instance::RoomInfo );
  1277. instance->room_info->room=room_owner.get(p_base);
  1278. } else if (portal_owner.owns(p_base)) {
  1279. instance->base_type=INSTANCE_PORTAL;
  1280. instance->portal_info = memnew(Instance::PortalInfo);
  1281. instance->portal_info->portal=portal_owner.get(p_base);
  1282. } else if (baked_light_owner.owns(p_base)) {
  1283. instance->base_type=INSTANCE_BAKED_LIGHT;
  1284. instance->baked_light_info=memnew(Instance::BakedLightInfo);
  1285. instance->baked_light_info->baked_light=baked_light_owner.get(p_base);
  1286. //instance->portal_info = memnew(Instance::PortalInfo);
  1287. //instance->portal_info->portal=portal_owner.get(p_base);
  1288. } else {
  1289. ERR_EXPLAIN("Invalid base RID for instance!")
  1290. ERR_FAIL();
  1291. }
  1292. instance_dependency_map[ p_base ].insert( instance->self );
  1293. instance->base_rid=p_base;
  1294. if (instance->scenario)
  1295. _instance_queue_update(instance,true);
  1296. }
  1297. }
  1298. RID VisualServerRaster::instance_get_base(RID p_instance) const {
  1299. Instance *instance = instance_owner.get( p_instance );
  1300. ERR_FAIL_COND_V( !instance, RID() );
  1301. return instance->base_rid;
  1302. }
  1303. void VisualServerRaster::instance_set_scenario(RID p_instance, RID p_scenario) {
  1304. VS_CHANGED;
  1305. Instance *instance = instance_owner.get( p_instance );
  1306. ERR_FAIL_COND( !instance );
  1307. if (instance->scenario) {
  1308. Map< RID, Set<RID> >::Element *E = instance_dependency_map.find( instance->scenario->self );
  1309. if (E) {
  1310. // wtf, no E?
  1311. E->get().erase( instance->self );
  1312. } else {
  1313. ERR_PRINT("no scenario E? Bug?");
  1314. }
  1315. if (instance->light_info) {
  1316. if (instance->light_info->D)
  1317. instance->scenario->directional_lights.erase( instance->light_info->D );
  1318. }
  1319. if (instance->portal_info) {
  1320. _portal_disconnect(instance,true);
  1321. }
  1322. if (instance->octree_id) {
  1323. instance->scenario->octree.erase( instance->octree_id );
  1324. instance->octree_id=0;
  1325. }
  1326. instance->scenario=NULL;
  1327. }
  1328. if (p_scenario.is_valid()) {
  1329. Scenario *scenario = scenario_owner.get( p_scenario );
  1330. ERR_FAIL_COND(!scenario);
  1331. instance->scenario=scenario;
  1332. instance_dependency_map[ p_scenario ].insert( instance->self );
  1333. instance->scenario=scenario;
  1334. if (instance->base_type==INSTANCE_LIGHT && rasterizer->light_get_type(instance->base_rid)==LIGHT_DIRECTIONAL) {
  1335. instance->light_info->D = instance->scenario->directional_lights.push_back(instance->self);
  1336. }
  1337. _instance_queue_update(instance,true);
  1338. }
  1339. }
  1340. RID VisualServerRaster::instance_get_scenario(RID p_instance) const {
  1341. Instance *instance = instance_owner.get( p_instance );
  1342. ERR_FAIL_COND_V( !instance, RID() );
  1343. if (instance->scenario)
  1344. return instance->scenario->self;
  1345. else
  1346. return RID();
  1347. }
  1348. void VisualServerRaster::instance_set_layer_mask(RID p_instance, uint32_t p_mask) {
  1349. VS_CHANGED;
  1350. Instance *instance = instance_owner.get( p_instance );
  1351. ERR_FAIL_COND( !instance );
  1352. instance->layer_mask=p_mask;
  1353. }
  1354. uint32_t VisualServerRaster::instance_get_layer_mask(RID p_instance) const{
  1355. Instance *instance = instance_owner.get( p_instance );
  1356. ERR_FAIL_COND_V( !instance, 0 );
  1357. return instance->layer_mask;
  1358. }
  1359. AABB VisualServerRaster::instance_get_base_aabb(RID p_instance) const {
  1360. Instance *instance = instance_owner.get( p_instance );
  1361. ERR_FAIL_COND_V( !instance, AABB() );
  1362. return instance->aabb;
  1363. }
  1364. void VisualServerRaster::instance_attach_object_instance_ID(RID p_instance,uint32_t p_ID) {
  1365. VS_CHANGED;
  1366. Instance *instance = instance_owner.get( p_instance );
  1367. ERR_FAIL_COND( !instance );
  1368. instance->object_ID=p_ID;
  1369. }
  1370. uint32_t VisualServerRaster::instance_get_object_instance_ID(RID p_instance) const {
  1371. Instance *instance = instance_owner.get( p_instance );
  1372. ERR_FAIL_COND_V( !instance, 0 );
  1373. return instance->object_ID;
  1374. }
  1375. void VisualServerRaster::instance_attach_skeleton(RID p_instance,RID p_skeleton) {
  1376. VS_CHANGED;
  1377. Instance *instance = instance_owner.get( p_instance );
  1378. ERR_FAIL_COND( !instance );
  1379. instance->data.skeleton=p_skeleton;
  1380. }
  1381. RID VisualServerRaster::instance_get_skeleton(RID p_instance) const {
  1382. Instance *instance = instance_owner.get( p_instance );
  1383. ERR_FAIL_COND_V( !instance, RID() );
  1384. return instance->data.skeleton;
  1385. }
  1386. void VisualServerRaster::instance_set_morph_target_weight(RID p_instance,int p_shape, float p_weight) {
  1387. VS_CHANGED;
  1388. Instance *instance = instance_owner.get( p_instance );
  1389. ERR_FAIL_COND( !instance );
  1390. ERR_FAIL_INDEX( p_shape, instance->data.morph_values.size() );
  1391. instance->data.morph_values[p_shape]=p_weight;
  1392. }
  1393. float VisualServerRaster::instance_get_morph_target_weight(RID p_instance,int p_shape) const {
  1394. Instance *instance = instance_owner.get( p_instance );
  1395. ERR_FAIL_COND_V( !instance, 0 );
  1396. ERR_FAIL_INDEX_V( p_shape, instance->data.morph_values.size(), 0 );
  1397. return instance->data.morph_values[p_shape];
  1398. }
  1399. void VisualServerRaster::instance_set_transform(RID p_instance, const Transform& p_transform) {
  1400. VS_CHANGED;
  1401. Instance *instance = instance_owner.get( p_instance );
  1402. ERR_FAIL_COND( !instance );
  1403. if (p_transform==instance->data.transform) // must improve somehow
  1404. return;
  1405. instance->data.transform=p_transform;
  1406. if (instance->base_type==INSTANCE_LIGHT)
  1407. instance->data.transform.orthonormalize();
  1408. _instance_queue_update(instance);
  1409. }
  1410. Transform VisualServerRaster::instance_get_transform(RID p_instance) const {
  1411. Instance *instance = instance_owner.get( p_instance );
  1412. ERR_FAIL_COND_V( !instance, Transform() );
  1413. return instance->data.transform;
  1414. }
  1415. void VisualServerRaster::instance_set_exterior( RID p_instance, bool p_enabled ) {
  1416. VS_CHANGED;
  1417. Instance *instance = instance_owner.get( p_instance );
  1418. ERR_FAIL_COND( !instance );
  1419. ERR_EXPLAIN("Portals can't be assigned to be exterior");
  1420. ERR_FAIL_COND( instance->base_type == INSTANCE_PORTAL );
  1421. if (instance->exterior==p_enabled)
  1422. return;
  1423. instance->exterior=p_enabled;
  1424. _instance_queue_update( instance );
  1425. }
  1426. bool VisualServerRaster::instance_is_exterior( RID p_instance) const {
  1427. Instance *instance = instance_owner.get( p_instance );
  1428. ERR_FAIL_COND_V( !instance, false );
  1429. return instance->exterior;
  1430. }
  1431. void VisualServerRaster::instance_set_room( RID p_instance, RID p_room ) {
  1432. VS_CHANGED;
  1433. Instance *instance = instance_owner.get( p_instance );
  1434. ERR_FAIL_COND( !instance );
  1435. if (instance->room && instance->RE) {
  1436. //instance already havs a room, remove it from there
  1437. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1438. instance->room->room_info->owned_geometry_instances.erase(instance->RE);
  1439. if (!p_room.is_valid() && instance->octree_id) {
  1440. //remove from the octree, so it's re-added with different flags
  1441. instance->scenario->octree.erase( instance->octree_id );
  1442. instance->octree_id=0;
  1443. _instance_queue_update( instance,true );
  1444. }
  1445. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1446. instance->room->room_info->owned_room_instances.erase(instance->RE);
  1447. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next()) {
  1448. _portal_disconnect(E->get());
  1449. _instance_queue_update( E->get(),false );
  1450. }
  1451. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1452. _portal_disconnect(instance,true);
  1453. bool ss = instance->room->room_info->owned_portal_instances.erase(instance->RE);
  1454. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1455. instance->room->room_info->owned_light_instances.erase(instance->RE);
  1456. } else {
  1457. ERR_FAIL();
  1458. }
  1459. instance->RE=NULL;
  1460. instance->room=NULL;
  1461. } else {
  1462. if (p_room.is_valid() && instance->octree_id) {
  1463. //remove from the octree, so it's re-added with different flags
  1464. instance->scenario->octree.erase( instance->octree_id );
  1465. instance->octree_id=0;
  1466. _instance_queue_update( instance,true );
  1467. }
  1468. }
  1469. if (!p_room.is_valid())
  1470. return; // just clearning the room
  1471. Instance *room = instance_owner.get( p_room );
  1472. ERR_FAIL_COND( !room );
  1473. ERR_FAIL_COND( room->base_type!=INSTANCE_ROOM );
  1474. if (instance->base_type==INSTANCE_ROOM) {
  1475. //perform cycle test
  1476. Instance *parent = instance;
  1477. while(parent) {
  1478. ERR_EXPLAIN("Cycle in room assignment");
  1479. ERR_FAIL_COND( parent == room );
  1480. parent=parent->room;
  1481. }
  1482. }
  1483. if ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK ) {
  1484. instance->RE = room->room_info->owned_geometry_instances.push_back(instance);
  1485. } else if ( instance->base_type==INSTANCE_ROOM ) {
  1486. instance->RE = room->room_info->owned_room_instances.push_back(instance);
  1487. for(List<Instance*>::Element *E=instance->room_info->owned_portal_instances.front();E;E=E->next())
  1488. _instance_queue_update( E->get(),false );
  1489. } else if ( instance->base_type==INSTANCE_PORTAL ) {
  1490. instance->RE = room->room_info->owned_portal_instances.push_back(instance);
  1491. } else if ( instance->base_type==INSTANCE_LIGHT ) {
  1492. instance->RE = room->room_info->owned_light_instances.push_back(instance);
  1493. } else {
  1494. ERR_FAIL();
  1495. }
  1496. instance->room=room;
  1497. }
  1498. RID VisualServerRaster::instance_get_room( RID p_instance ) const {
  1499. Instance *instance = instance_owner.get( p_instance );
  1500. ERR_FAIL_COND_V( !instance, RID() );
  1501. if (instance->room)
  1502. return instance->room->self;
  1503. else
  1504. return RID();
  1505. }
  1506. void VisualServerRaster::instance_set_extra_visibility_margin( RID p_instance, real_t p_margin ) {
  1507. VS_CHANGED;
  1508. Instance *instance = instance_owner.get( p_instance );
  1509. ERR_FAIL_COND( !instance );
  1510. instance->extra_margin=p_margin;
  1511. }
  1512. real_t VisualServerRaster::instance_get_extra_visibility_margin( RID p_instance ) const{
  1513. Instance *instance = instance_owner.get( p_instance );
  1514. ERR_FAIL_COND_V( !instance, 0 );
  1515. return instance->extra_margin;
  1516. }
  1517. Vector<RID> VisualServerRaster::instances_cull_aabb(const AABB& p_aabb, RID p_scenario) const {
  1518. Vector<RID> instances;
  1519. Scenario *scenario=scenario_owner.get(p_scenario);
  1520. ERR_FAIL_COND_V(!scenario,instances);
  1521. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1522. int culled=0;
  1523. Instance *cull[1024];
  1524. culled=scenario->octree.cull_AABB(p_aabb,cull,1024);
  1525. for (int i=0;i<culled;i++) {
  1526. Instance *instance=cull[i];
  1527. ERR_CONTINUE(!instance);
  1528. instances.push_back(instance->self);
  1529. }
  1530. return instances;
  1531. }
  1532. Vector<RID> VisualServerRaster::instances_cull_ray(const Vector3& p_from, const Vector3& p_to, RID p_scenario) const{
  1533. Vector<RID> instances;
  1534. Scenario *scenario=scenario_owner.get(p_scenario);
  1535. ERR_FAIL_COND_V(!scenario,instances);
  1536. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1537. int culled=0;
  1538. Instance *cull[1024];
  1539. culled=scenario->octree.cull_segment(p_from,p_to*10000,cull,1024);
  1540. for (int i=0;i<culled;i++) {
  1541. Instance *instance=cull[i];
  1542. ERR_CONTINUE(!instance);
  1543. instances.push_back(instance->self);
  1544. }
  1545. return instances;
  1546. }
  1547. Vector<RID> VisualServerRaster::instances_cull_convex(const Vector<Plane>& p_convex, RID p_scenario) const{
  1548. Vector<RID> instances;
  1549. Scenario *scenario=scenario_owner.get(p_scenario);
  1550. ERR_FAIL_COND_V(!scenario,instances);
  1551. const_cast<VisualServerRaster*>(this)->_update_instances(); // check dirty instances before culling
  1552. int culled=0;
  1553. Instance *cull[1024];
  1554. culled=scenario->octree.cull_convex(p_convex,cull,1024);
  1555. for (int i=0;i<culled;i++) {
  1556. Instance *instance=cull[i];
  1557. ERR_CONTINUE(!instance);
  1558. instances.push_back(instance->self);
  1559. }
  1560. return instances;
  1561. }
  1562. void VisualServerRaster::instance_geometry_set_flag(RID p_instance,InstanceFlags p_flags,bool p_enabled) {
  1563. Instance *instance = instance_owner.get( p_instance );
  1564. ERR_FAIL_COND( !instance );
  1565. // ERR_FAIL_COND( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK) );
  1566. switch(p_flags) {
  1567. case INSTANCE_FLAG_VISIBLE: {
  1568. instance->visible=p_enabled;
  1569. } break;
  1570. case INSTANCE_FLAG_BILLBOARD: {
  1571. instance->data.billboard=p_enabled;
  1572. } break;
  1573. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1574. instance->data.billboard_y=p_enabled;
  1575. } break;
  1576. case INSTANCE_FLAG_CAST_SHADOW: {
  1577. instance->cast_shadows=p_enabled;
  1578. } break;
  1579. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1580. instance->receive_shadows=p_enabled;
  1581. } break;
  1582. case INSTANCE_FLAG_DEPH_SCALE: {
  1583. instance->data.depth_scale=p_enabled;
  1584. } break;
  1585. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1586. instance->visible_in_all_rooms=p_enabled;
  1587. } break;
  1588. }
  1589. }
  1590. bool VisualServerRaster::instance_geometry_get_flag(RID p_instance,InstanceFlags p_flags) const{
  1591. const Instance *instance = instance_owner.get( p_instance );
  1592. ERR_FAIL_COND_V( !instance, false );
  1593. // ERR_FAIL_COND_V( ! ( (1<<instance->base_type) & INSTANCE_GEOMETRY_MASK), false );
  1594. switch(p_flags) {
  1595. case INSTANCE_FLAG_VISIBLE: {
  1596. return instance->visible;
  1597. } break;
  1598. case INSTANCE_FLAG_BILLBOARD: {
  1599. return instance->data.billboard;
  1600. } break;
  1601. case INSTANCE_FLAG_BILLBOARD_FIX_Y: {
  1602. return instance->data.billboard_y;
  1603. } break;
  1604. case INSTANCE_FLAG_CAST_SHADOW: {
  1605. return instance->cast_shadows;
  1606. } break;
  1607. case INSTANCE_FLAG_RECEIVE_SHADOWS: {
  1608. return instance->receive_shadows;
  1609. } break;
  1610. case INSTANCE_FLAG_DEPH_SCALE: {
  1611. return instance->data.depth_scale;
  1612. } break;
  1613. case INSTANCE_FLAG_VISIBLE_IN_ALL_ROOMS: {
  1614. return instance->visible_in_all_rooms;
  1615. } break;
  1616. }
  1617. return false;
  1618. }
  1619. void VisualServerRaster::instance_geometry_set_material_override(RID p_instance, RID p_material) {
  1620. VS_CHANGED;
  1621. Instance *instance = instance_owner.get( p_instance );
  1622. ERR_FAIL_COND( !instance );
  1623. instance->data.material_override=p_material;
  1624. }
  1625. RID VisualServerRaster::instance_geometry_get_material_override(RID p_instance) const{
  1626. Instance *instance = instance_owner.get( p_instance );
  1627. ERR_FAIL_COND_V( !instance, RID() );
  1628. return instance->data.material_override;
  1629. }
  1630. void VisualServerRaster::instance_geometry_set_draw_range(RID p_instance,float p_min,float p_max){
  1631. VS_CHANGED;
  1632. Instance *instance = instance_owner.get( p_instance );
  1633. ERR_FAIL_COND( !instance );
  1634. instance->draw_range_begin=p_min;
  1635. instance->draw_range_end=p_max;
  1636. }
  1637. float VisualServerRaster::instance_geometry_get_draw_range_min(RID p_instance) const{
  1638. const Instance *instance = instance_owner.get( p_instance );
  1639. ERR_FAIL_COND_V( !instance,0 );
  1640. return instance->draw_range_begin;
  1641. }
  1642. float VisualServerRaster::instance_geometry_get_draw_range_max(RID p_instance) const{
  1643. const Instance *instance = instance_owner.get( p_instance );
  1644. ERR_FAIL_COND_V( !instance,0 );
  1645. return instance->draw_range_end;
  1646. }
  1647. void VisualServerRaster::instance_geometry_set_baked_light(RID p_instance,RID p_baked_light) {
  1648. VS_CHANGED;
  1649. Instance *instance = instance_owner.get( p_instance );
  1650. ERR_FAIL_COND( !instance );
  1651. if (instance->baked_light) {
  1652. instance->baked_light->baked_light_info->owned_instances.erase(instance->BLE);
  1653. instance->BLE=NULL;
  1654. instance->baked_light=NULL;
  1655. instance->data.baked_light=NULL;
  1656. instance->data.baked_light_octree_xform=NULL;
  1657. }
  1658. if (!p_baked_light.is_valid())
  1659. return;
  1660. Instance *bl_instance = instance_owner.get( p_baked_light );
  1661. ERR_FAIL_COND( !bl_instance );
  1662. ERR_FAIL_COND( bl_instance->base_type!=INSTANCE_BAKED_LIGHT );
  1663. instance->baked_light=bl_instance;
  1664. instance->BLE=bl_instance->baked_light_info->owned_instances.push_back(instance);
  1665. instance->data.baked_light=&bl_instance->baked_light_info->baked_light->data;
  1666. instance->data.baked_light_octree_xform=&bl_instance->baked_light_info->affine_inverse;
  1667. }
  1668. RID VisualServerRaster::instance_geometry_get_baked_light(RID p_instance) const{
  1669. const Instance *instance = instance_owner.get( p_instance );
  1670. ERR_FAIL_COND_V( !instance,RID() );
  1671. if (instance->baked_light)
  1672. instance->baked_light->self;
  1673. return RID();
  1674. }
  1675. void VisualServerRaster::instance_geometry_set_baked_light_texture_index(RID p_instance,int p_tex_id){
  1676. VS_CHANGED;
  1677. Instance *instance = instance_owner.get( p_instance );
  1678. ERR_FAIL_COND( !instance );
  1679. instance->data.baked_lightmap_id=p_tex_id;
  1680. }
  1681. int VisualServerRaster::instance_geometry_get_baked_light_texture_index(RID p_instance) const{
  1682. const Instance *instance = instance_owner.get( p_instance );
  1683. ERR_FAIL_COND_V( !instance,0 );
  1684. return instance->data.baked_lightmap_id;
  1685. }
  1686. void VisualServerRaster::_update_instance(Instance *p_instance) {
  1687. p_instance->version++;
  1688. if (p_instance->base_type == INSTANCE_LIGHT) {
  1689. rasterizer->light_instance_set_transform( p_instance->light_info->instance, p_instance->data.transform );
  1690. }
  1691. if (p_instance->aabb.has_no_surface())
  1692. return;
  1693. if (p_instance->base_type == INSTANCE_PARTICLES) {
  1694. rasterizer->particles_instance_set_transform( p_instance->particles_info->instance, p_instance->data.transform );
  1695. }
  1696. if ((1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1697. //make sure lights are updated
  1698. InstanceSet::Element *E=p_instance->lights.front();
  1699. while(E) {
  1700. E->get()->version++;
  1701. E=E->next();
  1702. }
  1703. } else if (p_instance->base_type == INSTANCE_ROOM) {
  1704. p_instance->room_info->affine_inverse=p_instance->data.transform.affine_inverse();
  1705. } else if (p_instance->base_type == INSTANCE_BAKED_LIGHT) {
  1706. Transform scale;
  1707. scale.basis.scale(p_instance->baked_light_info->baked_light->octree_aabb.size);
  1708. scale.origin=p_instance->baked_light_info->baked_light->octree_aabb.pos;
  1709. //print_line("scale: "+scale);
  1710. p_instance->baked_light_info->affine_inverse=(p_instance->data.transform*scale).affine_inverse();
  1711. }
  1712. p_instance->data.mirror = p_instance->data.transform.basis.determinant() < 0.0;
  1713. AABB new_aabb;
  1714. if (p_instance->base_type==INSTANCE_PORTAL) {
  1715. //portals need to be transformed in a special way, so they don't become too wide if they have scale..
  1716. Transform portal_xform = p_instance->data.transform;
  1717. portal_xform.basis.set_axis(2,portal_xform.basis.get_axis(2).normalized());
  1718. p_instance->portal_info->plane_cache=Plane( p_instance->data.transform.origin, portal_xform.basis.get_axis(2));
  1719. int point_count=p_instance->portal_info->portal->shape.size();
  1720. p_instance->portal_info->transformed_point_cache.resize(point_count);
  1721. AABB portal_aabb;
  1722. for(int i=0;i<point_count;i++) {
  1723. Point2 src = p_instance->portal_info->portal->shape[i];
  1724. Vector3 point = portal_xform.xform(Vector3(src.x,src.y,0));
  1725. p_instance->portal_info->transformed_point_cache[i]=point;
  1726. if (i==0)
  1727. portal_aabb.pos=point;
  1728. else
  1729. portal_aabb.expand_to(point);
  1730. }
  1731. portal_aabb.grow_by(p_instance->portal_info->portal->connect_range);
  1732. new_aabb = portal_aabb;
  1733. } else {
  1734. new_aabb = p_instance->data.transform.xform(p_instance->aabb);
  1735. }
  1736. for(InstanceSet::Element *E=p_instance->lights.front();E;E=E->next()) {
  1737. Instance *light = E->get();
  1738. light->version++;
  1739. }
  1740. p_instance->transformed_aabb=new_aabb;
  1741. if (!p_instance->scenario) {
  1742. return;
  1743. }
  1744. if (p_instance->octree_id==0) {
  1745. uint32_t base_type = 1<<p_instance->base_type;
  1746. uint32_t pairable_mask=0;
  1747. bool pairable=false;
  1748. if (p_instance->base_type == INSTANCE_LIGHT) {
  1749. pairable_mask=p_instance->light_info->enabled?INSTANCE_GEOMETRY_MASK:0;
  1750. pairable=true;
  1751. }
  1752. if (p_instance->base_type == INSTANCE_PORTAL) {
  1753. pairable_mask=(1<<INSTANCE_PORTAL);
  1754. pairable=true;
  1755. }
  1756. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1757. base_type|=INSTANCE_ROOMLESS_MASK;
  1758. }
  1759. if (p_instance->base_type == INSTANCE_ROOM) {
  1760. pairable_mask=INSTANCE_ROOMLESS_MASK;
  1761. pairable=true;
  1762. }
  1763. // not inside octree
  1764. p_instance->octree_id = p_instance->scenario->octree.create(p_instance,new_aabb,0,pairable,base_type,pairable_mask);
  1765. } else {
  1766. // if (new_aabb==p_instance->data.transformed_aabb)
  1767. // return;
  1768. p_instance->scenario->octree.move(p_instance->octree_id,new_aabb);
  1769. }
  1770. if (p_instance->base_type==INSTANCE_PORTAL) {
  1771. _portal_attempt_connect(p_instance);
  1772. }
  1773. if (!p_instance->room && (1<<p_instance->base_type)&INSTANCE_GEOMETRY_MASK) {
  1774. _instance_validate_autorooms(p_instance);
  1775. }
  1776. if (p_instance->base_type == INSTANCE_ROOM) {
  1777. for(Set<Instance*>::Element *E=p_instance->room_info->owned_autoroom_geometry.front();E;E=E->next())
  1778. _instance_validate_autorooms(E->get());
  1779. }
  1780. }
  1781. void VisualServerRaster::_update_instance_aabb(Instance *p_instance) {
  1782. AABB new_aabb;
  1783. ERR_FAIL_COND(p_instance->base_type!=INSTANCE_NONE && !p_instance->base_rid.is_valid());
  1784. switch(p_instance->base_type) {
  1785. case VisualServer::INSTANCE_NONE: {
  1786. // do nothing
  1787. } break;
  1788. case VisualServer::INSTANCE_MESH: {
  1789. new_aabb = rasterizer->mesh_get_aabb(p_instance->base_rid);
  1790. } break;
  1791. case VisualServer::INSTANCE_MULTIMESH: {
  1792. new_aabb = rasterizer->multimesh_get_aabb(p_instance->base_rid);
  1793. } break;
  1794. case VisualServer::INSTANCE_IMMEDIATE: {
  1795. new_aabb = rasterizer->immediate_get_aabb(p_instance->base_rid);
  1796. } break;
  1797. case VisualServer::INSTANCE_PARTICLES: {
  1798. new_aabb = rasterizer->particles_get_aabb(p_instance->base_rid);
  1799. } break;
  1800. case VisualServer::INSTANCE_LIGHT: {
  1801. new_aabb = rasterizer->light_get_aabb(p_instance->base_rid);
  1802. } break;
  1803. case VisualServer::INSTANCE_ROOM: {
  1804. Room *room = room_owner.get( p_instance->base_rid );
  1805. ERR_FAIL_COND(!room);
  1806. new_aabb=room->bounds.get_aabb();
  1807. } break;
  1808. case VisualServer::INSTANCE_PORTAL: {
  1809. Portal *portal = portal_owner.get( p_instance->base_rid );
  1810. ERR_FAIL_COND(!portal);
  1811. for (int i=0;i<portal->shape.size();i++) {
  1812. Vector3 point( portal->shape[i].x, portal->shape[i].y, 0 );
  1813. if (i==0) {
  1814. new_aabb.pos=point;
  1815. new_aabb.size.z=0.01; // make it not flat for octree
  1816. } else {
  1817. new_aabb.expand_to(point);
  1818. }
  1819. }
  1820. } break;
  1821. case VisualServer::INSTANCE_BAKED_LIGHT: {
  1822. BakedLight *baked_light = baked_light_owner.get( p_instance->base_rid );
  1823. ERR_FAIL_COND(!baked_light);
  1824. new_aabb=baked_light->octree_aabb;
  1825. } break;
  1826. default: {}
  1827. }
  1828. if (p_instance->extra_margin)
  1829. new_aabb.grow_by(p_instance->extra_margin);
  1830. p_instance->aabb=new_aabb;
  1831. }
  1832. void VisualServerRaster::_update_instances() {
  1833. while(instance_update_list) {
  1834. Instance *instance=instance_update_list;
  1835. instance_update_list=instance_update_list->update_next;
  1836. if (instance->update_aabb)
  1837. _update_instance_aabb(instance);
  1838. _update_instance(instance);
  1839. instance->update=false;
  1840. instance->update_aabb=false;
  1841. instance->update_next=0;
  1842. }
  1843. }
  1844. void VisualServerRaster::instance_light_set_enabled(RID p_instance,bool p_enabled) {
  1845. VS_CHANGED;
  1846. Instance *instance = instance_owner.get( p_instance );
  1847. ERR_FAIL_COND( !instance );
  1848. ERR_FAIL_COND( instance->base_type!=INSTANCE_LIGHT );
  1849. if (p_enabled==instance->light_info->enabled)
  1850. return;
  1851. instance->light_info->enabled=p_enabled;
  1852. if (light_get_type(instance->base_rid)!=VS::LIGHT_DIRECTIONAL && instance->octree_id && instance->scenario)
  1853. instance->scenario->octree.set_pairable(instance->octree_id,p_enabled,1<<INSTANCE_LIGHT,p_enabled?INSTANCE_GEOMETRY_MASK:0);
  1854. //_instance_queue_update( instance , true );
  1855. }
  1856. bool VisualServerRaster::instance_light_is_enabled(RID p_instance) const {
  1857. const Instance *instance = instance_owner.get( p_instance );
  1858. ERR_FAIL_COND_V( !instance,false );
  1859. ERR_FAIL_COND_V( instance->base_type!=INSTANCE_LIGHT,false );
  1860. return instance->light_info->enabled;
  1861. }
  1862. /****** CANVAS *********/
  1863. RID VisualServerRaster::canvas_create() {
  1864. Canvas * canvas = memnew( Canvas );
  1865. ERR_FAIL_COND_V(!canvas,RID());
  1866. RID rid = canvas_owner.make_rid( canvas );
  1867. return rid;
  1868. }
  1869. void VisualServerRaster::canvas_set_item_mirroring(RID p_canvas,RID p_item,const Point2& p_mirroring) {
  1870. Canvas * canvas = canvas_owner.get(p_canvas);
  1871. ERR_FAIL_COND(!canvas);
  1872. CanvasItem *canvas_item = canvas_item_owner.get(p_item);
  1873. ERR_FAIL_COND(!canvas_item);
  1874. int idx = canvas->find_item(canvas_item);
  1875. ERR_FAIL_COND(idx==-1);
  1876. canvas->child_items[idx].mirror=p_mirroring;
  1877. }
  1878. Point2 VisualServerRaster::canvas_get_item_mirroring(RID p_canvas,RID p_item) const {
  1879. Canvas * canvas = canvas_owner.get(p_canvas);
  1880. ERR_FAIL_COND_V(!canvas,Point2());
  1881. CanvasItem *canvas_item = memnew( CanvasItem );
  1882. ERR_FAIL_COND_V(!canvas_item,Point2());
  1883. int idx = canvas->find_item(canvas_item);
  1884. ERR_FAIL_COND_V(idx==-1,Point2());
  1885. return canvas->child_items[idx].mirror;
  1886. }
  1887. RID VisualServerRaster::canvas_item_create() {
  1888. CanvasItem *canvas_item = memnew( CanvasItem );
  1889. ERR_FAIL_COND_V(!canvas_item,RID());
  1890. return canvas_item_owner.make_rid( canvas_item );
  1891. }
  1892. void VisualServerRaster::canvas_item_set_parent(RID p_item,RID p_parent) {
  1893. VS_CHANGED;
  1894. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1895. ERR_FAIL_COND(!canvas_item);
  1896. if (canvas_item->parent.is_valid()) {
  1897. if (canvas_owner.owns(canvas_item->parent)) {
  1898. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  1899. canvas->erase_item(canvas_item);
  1900. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  1901. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  1902. item_owner->child_items.erase(canvas_item);
  1903. }
  1904. canvas_item->parent=RID();
  1905. }
  1906. if (p_parent.is_valid()) {
  1907. if (canvas_owner.owns(p_parent)) {
  1908. Canvas *canvas = canvas_owner.get(p_parent);
  1909. Canvas::ChildItem ci;
  1910. ci.item=canvas_item;
  1911. canvas->child_items.push_back(ci);
  1912. } else if (canvas_item_owner.owns(p_parent)) {
  1913. CanvasItem *item_owner = canvas_item_owner.get(p_parent);
  1914. item_owner->child_items.push_back(canvas_item);
  1915. } else {
  1916. ERR_EXPLAIN("Invalid parent");
  1917. ERR_FAIL();
  1918. }
  1919. }
  1920. canvas_item->parent=p_parent;
  1921. }
  1922. RID VisualServerRaster::canvas_item_get_parent(RID p_canvas_item) const {
  1923. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1924. ERR_FAIL_COND_V(!canvas_item,RID());
  1925. return canvas_item->parent;
  1926. }
  1927. void VisualServerRaster::canvas_item_set_visible(RID p_item,bool p_visible) {
  1928. VS_CHANGED;
  1929. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1930. ERR_FAIL_COND(!canvas_item);
  1931. canvas_item->visible=p_visible;
  1932. }
  1933. bool VisualServerRaster::canvas_item_is_visible(RID p_item) const {
  1934. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1935. ERR_FAIL_COND_V(!canvas_item,RID());
  1936. return canvas_item->visible;
  1937. }
  1938. void VisualServerRaster::canvas_item_set_blend_mode(RID p_canvas_item,MaterialBlendMode p_blend) {
  1939. VS_CHANGED;
  1940. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1941. if (!canvas_item) {
  1942. printf("!canvas_item\n");
  1943. };
  1944. ERR_FAIL_COND(!canvas_item);
  1945. if (canvas_item->blend_mode==p_blend)
  1946. return;
  1947. VS_CHANGED;
  1948. canvas_item->blend_mode=p_blend;
  1949. }
  1950. void VisualServerRaster::canvas_item_attach_viewport(RID p_canvas_item, RID p_viewport) {
  1951. CanvasItem *canvas_item = canvas_item_owner.get( p_canvas_item );
  1952. ERR_FAIL_COND(!canvas_item);
  1953. VS_CHANGED;
  1954. canvas_item->viewport=p_viewport;
  1955. }
  1956. /*
  1957. void VisualServerRaster::canvas_item_set_rect(RID p_item, const Rect2& p_rect) {
  1958. VS_CHANGED;
  1959. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1960. ERR_FAIL_COND(!canvas_item);
  1961. canvas_item->rect=p_rect;
  1962. }*/
  1963. void VisualServerRaster::canvas_item_set_clip(RID p_item, bool p_clip) {
  1964. VS_CHANGED;
  1965. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  1966. ERR_FAIL_COND(!canvas_item);
  1967. canvas_item->clip=p_clip;
  1968. }
  1969. const Rect2& VisualServerRaster::CanvasItem::get_rect() const {
  1970. if (custom_rect || !rect_dirty)
  1971. return rect;
  1972. //must update rect
  1973. int s=commands.size();
  1974. if (s==0) {
  1975. rect=Rect2();
  1976. rect_dirty=false;
  1977. return rect;
  1978. }
  1979. Matrix32 xf;
  1980. bool found_xform=false;
  1981. bool first=true;
  1982. const CanvasItem::Command * const *cmd = &commands[0];
  1983. for (int i=0;i<s;i++) {
  1984. const CanvasItem::Command *c=cmd[i];
  1985. Rect2 r;
  1986. switch(c->type) {
  1987. case CanvasItem::Command::TYPE_LINE: {
  1988. const CanvasItem::CommandLine* line = static_cast< const CanvasItem::CommandLine*>(c);
  1989. r.pos=line->from;
  1990. r.expand_to(line->to);
  1991. } break;
  1992. case CanvasItem::Command::TYPE_RECT: {
  1993. const CanvasItem::CommandRect* crect = static_cast< const CanvasItem::CommandRect*>(c);
  1994. r=crect->rect;
  1995. } break;
  1996. case CanvasItem::Command::TYPE_STYLE: {
  1997. const CanvasItem::CommandStyle* style = static_cast< const CanvasItem::CommandStyle*>(c);
  1998. r=style->rect;
  1999. } break;
  2000. case CanvasItem::Command::TYPE_PRIMITIVE: {
  2001. const CanvasItem::CommandPrimitive* primitive = static_cast< const CanvasItem::CommandPrimitive*>(c);
  2002. r.pos=primitive->points[0];
  2003. for(int i=1;i<primitive->points.size();i++) {
  2004. r.expand_to(primitive->points[i]);
  2005. }
  2006. } break;
  2007. case CanvasItem::Command::TYPE_POLYGON: {
  2008. const CanvasItem::CommandPolygon* polygon = static_cast< const CanvasItem::CommandPolygon*>(c);
  2009. int l = polygon->points.size();
  2010. const Point2*pp=&polygon->points[0];
  2011. r.pos=pp[0];
  2012. for(int i=1;i<l;i++) {
  2013. r.expand_to(pp[i]);
  2014. }
  2015. } break;
  2016. case CanvasItem::Command::TYPE_POLYGON_PTR: {
  2017. const CanvasItem::CommandPolygonPtr* polygon = static_cast< const CanvasItem::CommandPolygonPtr*>(c);
  2018. int l = polygon->count;
  2019. if (polygon->indices != NULL) {
  2020. r.pos=polygon->points[polygon->indices[0]];
  2021. for (int i=1; i<polygon->count; i++) {
  2022. r.expand_to(polygon->points[polygon->indices[i]]);
  2023. };
  2024. } else {
  2025. r.pos=polygon->points[0];
  2026. for (int i=1; i<polygon->count; i++) {
  2027. r.expand_to(polygon->points[i]);
  2028. };
  2029. };
  2030. } break;
  2031. case CanvasItem::Command::TYPE_CIRCLE: {
  2032. const CanvasItem::CommandCircle* circle = static_cast< const CanvasItem::CommandCircle*>(c);
  2033. r.pos=Point2(-circle->radius,-circle->radius)+circle->pos;
  2034. r.size=Point2(circle->radius*2.0,circle->radius*2.0);
  2035. } break;
  2036. case CanvasItem::Command::TYPE_TRANSFORM: {
  2037. const CanvasItem::CommandTransform* transform = static_cast<const CanvasItem::CommandTransform*>(c);
  2038. xf=transform->xform;
  2039. found_xform=true;
  2040. continue;
  2041. } break;
  2042. case CanvasItem::Command::TYPE_BLEND_MODE: {
  2043. } break;
  2044. case CanvasItem::Command::TYPE_CLIP_IGNORE: {
  2045. } break;
  2046. }
  2047. if (found_xform) {
  2048. r = xf.xform(r);
  2049. found_xform=false;
  2050. }
  2051. if (first) {
  2052. rect=r;
  2053. first=false;
  2054. } else
  2055. rect=rect.merge(r);
  2056. }
  2057. rect_dirty=false;
  2058. return rect;
  2059. }
  2060. void VisualServerRaster::canvas_item_set_transform(RID p_item, const Matrix32& p_transform) {
  2061. VS_CHANGED;
  2062. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2063. ERR_FAIL_COND(!canvas_item);
  2064. canvas_item->xform=p_transform;
  2065. }
  2066. void VisualServerRaster::canvas_item_set_custom_rect(RID p_item, bool p_custom_rect,const Rect2& p_rect) {
  2067. VS_CHANGED;
  2068. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2069. ERR_FAIL_COND(!canvas_item);
  2070. canvas_item->custom_rect=p_custom_rect;
  2071. if (p_custom_rect)
  2072. canvas_item->rect=p_rect;
  2073. }
  2074. void VisualServerRaster::canvas_item_set_opacity(RID p_item, float p_opacity) {
  2075. VS_CHANGED;
  2076. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2077. ERR_FAIL_COND(!canvas_item);
  2078. canvas_item->opacity=p_opacity;
  2079. }
  2080. float VisualServerRaster::canvas_item_get_opacity(RID p_item, float p_opacity) const {
  2081. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2082. ERR_FAIL_COND_V(!canvas_item,-1);
  2083. return canvas_item->opacity;
  2084. }
  2085. void VisualServerRaster::canvas_item_set_on_top(RID p_item, bool p_on_top) {
  2086. VS_CHANGED;
  2087. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2088. ERR_FAIL_COND(!canvas_item);
  2089. canvas_item->ontop=p_on_top;
  2090. }
  2091. bool VisualServerRaster::canvas_item_is_on_top(RID p_item) const{
  2092. const CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2093. ERR_FAIL_COND_V(!canvas_item,false);
  2094. return canvas_item->ontop;
  2095. }
  2096. void VisualServerRaster::canvas_item_set_self_opacity(RID p_item, float p_self_opacity) {
  2097. VS_CHANGED;
  2098. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2099. ERR_FAIL_COND(!canvas_item);
  2100. canvas_item->self_opacity=p_self_opacity;
  2101. }
  2102. float VisualServerRaster::canvas_item_get_self_opacity(RID p_item, float p_self_opacity) const {
  2103. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2104. ERR_FAIL_COND_V(!canvas_item,-1);
  2105. return canvas_item->self_opacity;
  2106. }
  2107. void VisualServerRaster::canvas_item_add_line(RID p_item, const Point2& p_from, const Point2& p_to,const Color& p_color,float p_width) {
  2108. VS_CHANGED;
  2109. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2110. ERR_FAIL_COND(!canvas_item);
  2111. CanvasItem::CommandLine * line = memnew( CanvasItem::CommandLine );
  2112. ERR_FAIL_COND(!line);
  2113. line->color=p_color;
  2114. line->from=p_from;
  2115. line->to=p_to;
  2116. line->width=p_width;
  2117. canvas_item->rect_dirty=true;
  2118. canvas_item->commands.push_back(line);
  2119. }
  2120. void VisualServerRaster::canvas_item_add_rect(RID p_item, const Rect2& p_rect, const Color& p_color) {
  2121. VS_CHANGED;
  2122. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2123. ERR_FAIL_COND(!canvas_item);
  2124. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2125. ERR_FAIL_COND(!rect);
  2126. rect->modulate=p_color;
  2127. rect->rect=p_rect;
  2128. canvas_item->rect_dirty=true;
  2129. canvas_item->commands.push_back(rect);
  2130. }
  2131. void VisualServerRaster::canvas_item_add_circle(RID p_item, const Point2& p_pos, float p_radius,const Color& p_color) {
  2132. VS_CHANGED;
  2133. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2134. ERR_FAIL_COND(!canvas_item);
  2135. CanvasItem::CommandCircle * circle = memnew( CanvasItem::CommandCircle );
  2136. ERR_FAIL_COND(!circle);
  2137. circle->color=p_color;
  2138. circle->pos=p_pos;
  2139. circle->radius=p_radius;
  2140. canvas_item->commands.push_back(circle);
  2141. }
  2142. void VisualServerRaster::canvas_item_add_texture_rect(RID p_item, const Rect2& p_rect, RID p_texture,bool p_tile,const Color& p_modulate) {
  2143. VS_CHANGED;
  2144. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2145. ERR_FAIL_COND(!canvas_item);
  2146. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2147. ERR_FAIL_COND(!rect);
  2148. rect->modulate=p_modulate;
  2149. rect->rect=p_rect;
  2150. rect->flags=0;
  2151. if (p_tile)
  2152. rect->flags|=Rasterizer::CANVAS_RECT_TILE;
  2153. if (p_rect.size.x<0) {
  2154. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2155. rect->rect.size.x = -rect->rect.size.x;
  2156. }
  2157. if (p_rect.size.y<0) {
  2158. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2159. rect->rect.size.y = -rect->rect.size.y;
  2160. }
  2161. rect->texture=p_texture;
  2162. canvas_item->rect_dirty=true;
  2163. canvas_item->commands.push_back(rect);
  2164. }
  2165. 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) {
  2166. VS_CHANGED;
  2167. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2168. ERR_FAIL_COND(!canvas_item);
  2169. CanvasItem::CommandRect * rect = memnew( CanvasItem::CommandRect );
  2170. ERR_FAIL_COND(!rect);
  2171. rect->modulate=p_modulate;
  2172. rect->rect=p_rect;
  2173. rect->texture=p_texture;
  2174. rect->source=p_src_rect;
  2175. rect->flags=Rasterizer::CANVAS_RECT_REGION;
  2176. if (p_rect.size.x<0) {
  2177. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  2178. rect->rect.size.x = -rect->rect.size.x;
  2179. }
  2180. if (p_rect.size.y<0) {
  2181. rect->flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  2182. rect->rect.size.y = -rect->rect.size.y;
  2183. }
  2184. canvas_item->rect_dirty=true;
  2185. canvas_item->commands.push_back(rect);
  2186. }
  2187. 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) {
  2188. VS_CHANGED;
  2189. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2190. ERR_FAIL_COND(!canvas_item);
  2191. CanvasItem::CommandStyle * style = memnew( CanvasItem::CommandStyle );
  2192. ERR_FAIL_COND(!style);
  2193. style->texture=p_texture;
  2194. style->rect=p_rect;
  2195. style->draw_center=p_draw_center;
  2196. style->color=p_modulate;
  2197. style->margin[MARGIN_LEFT]=p_topleft.x;
  2198. style->margin[MARGIN_TOP]=p_topleft.y;
  2199. style->margin[MARGIN_RIGHT]=p_bottomright.x;
  2200. style->margin[MARGIN_BOTTOM]=p_bottomright.y;
  2201. canvas_item->rect_dirty=true;
  2202. canvas_item->commands.push_back(style);
  2203. }
  2204. 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) {
  2205. VS_CHANGED;
  2206. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2207. ERR_FAIL_COND(!canvas_item);
  2208. CanvasItem::CommandPrimitive * prim = memnew( CanvasItem::CommandPrimitive );
  2209. ERR_FAIL_COND(!prim);
  2210. prim->texture=p_texture;
  2211. prim->points=p_points;
  2212. prim->uvs=p_uvs;
  2213. prim->colors=p_colors;
  2214. prim->width=p_width;
  2215. canvas_item->rect_dirty=true;
  2216. canvas_item->commands.push_back(prim);
  2217. }
  2218. 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) {
  2219. VS_CHANGED;
  2220. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2221. ERR_FAIL_COND(!canvas_item);
  2222. #ifdef DEBUG_ENABLED
  2223. int pointcount = p_points.size();
  2224. ERR_FAIL_COND(pointcount<3);
  2225. int color_size=p_colors.size();
  2226. int uv_size=p_uvs.size();
  2227. ERR_FAIL_COND(color_size!=0 && color_size!=1 && color_size!=pointcount);
  2228. ERR_FAIL_COND(uv_size!=0 && (uv_size!=pointcount || !p_texture.is_valid()));
  2229. #endif
  2230. Vector<int> indices = Geometry::triangulate_polygon(p_points);
  2231. if (indices.empty()) {
  2232. ERR_EXPLAIN("Bad Polygon!");
  2233. ERR_FAIL_V();
  2234. }
  2235. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2236. ERR_FAIL_COND(!polygon);
  2237. polygon->texture=p_texture;
  2238. polygon->points=p_points;
  2239. polygon->uvs=p_uvs;
  2240. polygon->colors=p_colors;
  2241. polygon->indices=indices;
  2242. polygon->count=indices.size();
  2243. canvas_item->rect_dirty=true;
  2244. canvas_item->commands.push_back(polygon);
  2245. }
  2246. 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) {
  2247. VS_CHANGED;
  2248. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2249. ERR_FAIL_COND(!canvas_item);
  2250. ERR_FAIL_COND(p_count <= 0);
  2251. ERR_FAIL_COND(p_points == NULL);
  2252. CanvasItem::CommandPolygonPtr * polygon = memnew( CanvasItem::CommandPolygonPtr );
  2253. ERR_FAIL_COND(!polygon);
  2254. polygon->texture=p_texture;
  2255. polygon->points=p_points;
  2256. polygon->uvs=p_uvs;
  2257. polygon->colors=p_colors;
  2258. polygon->indices=p_indices;
  2259. polygon->count = p_count * 3;
  2260. canvas_item->rect_dirty=true;
  2261. canvas_item->commands.push_back(polygon);
  2262. };
  2263. 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) {
  2264. VS_CHANGED;
  2265. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2266. ERR_FAIL_COND(!canvas_item);
  2267. int ps = p_points.size();
  2268. ERR_FAIL_COND(!p_colors.empty() && p_colors.size()!=ps && p_colors.size()!=1);
  2269. ERR_FAIL_COND(!p_uvs.empty() && p_uvs.size()!=ps);
  2270. Vector<int> indices = p_indices;
  2271. int count = p_count * 3;
  2272. if (indices.empty()) {
  2273. ERR_FAIL_COND( ps % 3 != 0 );
  2274. if (p_count == -1)
  2275. count = ps;
  2276. } else {
  2277. ERR_FAIL_COND( indices.size() % 3 != 0 );
  2278. if (p_count == -1)
  2279. count = indices.size();
  2280. }
  2281. CanvasItem::CommandPolygon * polygon = memnew( CanvasItem::CommandPolygon );
  2282. ERR_FAIL_COND(!polygon);
  2283. polygon->texture=p_texture;
  2284. polygon->points=p_points;
  2285. polygon->uvs=p_uvs;
  2286. polygon->colors=p_colors;
  2287. polygon->indices=indices;
  2288. polygon->count = count;
  2289. canvas_item->rect_dirty=true;
  2290. canvas_item->commands.push_back(polygon);
  2291. }
  2292. void VisualServerRaster::canvas_item_add_set_transform(RID p_item,const Matrix32& p_transform) {
  2293. VS_CHANGED;
  2294. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2295. ERR_FAIL_COND(!canvas_item);
  2296. CanvasItem::CommandTransform * tr = memnew( CanvasItem::CommandTransform );
  2297. ERR_FAIL_COND(!tr);
  2298. tr->xform=p_transform;
  2299. canvas_item->commands.push_back(tr);
  2300. }
  2301. void VisualServerRaster::canvas_item_add_set_blend_mode(RID p_item, MaterialBlendMode p_blend) {
  2302. VS_CHANGED;
  2303. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2304. ERR_FAIL_COND(!canvas_item);
  2305. CanvasItem::CommandBlendMode * bm = memnew( CanvasItem::CommandBlendMode );
  2306. ERR_FAIL_COND(!bm);
  2307. bm->blend_mode = p_blend;
  2308. canvas_item->commands.push_back(bm);
  2309. };
  2310. void VisualServerRaster::canvas_item_add_clip_ignore(RID p_item, bool p_ignore) {
  2311. VS_CHANGED;
  2312. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2313. ERR_FAIL_COND(!canvas_item);
  2314. CanvasItem::CommandClipIgnore * ci = memnew( CanvasItem::CommandClipIgnore);
  2315. ERR_FAIL_COND(!ci);
  2316. ci->ignore=p_ignore;
  2317. canvas_item->commands.push_back(ci);
  2318. }
  2319. void VisualServerRaster::canvas_item_clear(RID p_item) {
  2320. VS_CHANGED;
  2321. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2322. ERR_FAIL_COND(!canvas_item);
  2323. canvas_item->clear();
  2324. }
  2325. void VisualServerRaster::canvas_item_raise(RID p_item) {
  2326. VS_CHANGED;
  2327. CanvasItem *canvas_item = canvas_item_owner.get( p_item );
  2328. ERR_FAIL_COND(!canvas_item);
  2329. if (canvas_item->parent.is_valid()) {
  2330. if (canvas_owner.owns(canvas_item->parent)) {
  2331. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2332. int idx = canvas->find_item(canvas_item);
  2333. ERR_FAIL_COND(idx<0);
  2334. Canvas::ChildItem ci = canvas->child_items[idx];
  2335. canvas->child_items.remove(idx);
  2336. canvas->child_items.push_back(ci);
  2337. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2338. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2339. int idx = item_owner->child_items.find(canvas_item);
  2340. ERR_FAIL_COND(idx<0);
  2341. item_owner->child_items.remove(idx);
  2342. item_owner->child_items.push_back(canvas_item);
  2343. }
  2344. }
  2345. }
  2346. /******** CANVAS *********/
  2347. void VisualServerRaster::cursor_set_rotation(float p_rotation, int p_cursor) {
  2348. VS_CHANGED;
  2349. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2350. cursors[p_cursor].rot = p_rotation;
  2351. };
  2352. void VisualServerRaster::cursor_set_texture(RID p_texture, const Point2 &p_center_offset, int p_cursor) {
  2353. VS_CHANGED;
  2354. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2355. cursors[p_cursor].texture = p_texture;
  2356. cursors[p_cursor].center = p_center_offset;
  2357. };
  2358. void VisualServerRaster::cursor_set_visible(bool p_visible, int p_cursor) {
  2359. VS_CHANGED;
  2360. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2361. cursors[p_cursor].visible = p_visible;
  2362. };
  2363. void VisualServerRaster::cursor_set_pos(const Point2& p_pos, int p_cursor) {
  2364. ERR_FAIL_INDEX(p_cursor, MAX_CURSORS);
  2365. if (cursors[p_cursor].pos==p_pos)
  2366. return;
  2367. VS_CHANGED;
  2368. cursors[p_cursor].pos = p_pos;
  2369. };
  2370. void VisualServerRaster::black_bars_set_margins(int p_left, int p_top, int p_right, int p_bottom) {
  2371. black_margin[MARGIN_LEFT]=p_left;
  2372. black_margin[MARGIN_TOP]=p_top;
  2373. black_margin[MARGIN_RIGHT]=p_right;
  2374. black_margin[MARGIN_BOTTOM]=p_bottom;
  2375. }
  2376. void VisualServerRaster::black_bars_set_images(RID p_left, RID p_top, RID p_right, RID p_bottom) {
  2377. black_image[MARGIN_LEFT]=p_left;
  2378. black_image[MARGIN_TOP]=p_top;
  2379. black_image[MARGIN_RIGHT]=p_right;
  2380. black_image[MARGIN_BOTTOM]=p_bottom;
  2381. }
  2382. void VisualServerRaster::_free_attached_instances(RID p_rid,bool p_free_scenario) {
  2383. Map< RID, Set<RID> >::Element * E = instance_dependency_map.find( p_rid );
  2384. if (E) {
  2385. // has instances
  2386. while( E->get().size() ) {
  2387. // erase all attached instances
  2388. if (p_free_scenario)
  2389. instance_set_scenario( E->get().front()->get(), RID() );
  2390. else
  2391. instance_set_base( E->get().front()->get(), RID() );
  2392. }
  2393. }
  2394. instance_dependency_map.erase(p_rid);
  2395. }
  2396. void VisualServerRaster::custom_shade_model_set_shader(int p_model, RID p_shader) {
  2397. VS_CHANGED;
  2398. // rasterizer->custom_shade_model_set_shader(p_model,p_shader);
  2399. }
  2400. RID VisualServerRaster::custom_shade_model_get_shader(int p_model) const {
  2401. //return rasterizer->custom_shade_model_get_shader(p_model);
  2402. return RID();
  2403. }
  2404. void VisualServerRaster::custom_shade_model_set_name(int p_model, const String& p_name) {
  2405. //rasterizer->custom_shade_model_set_name(p_model,p_name);
  2406. }
  2407. String VisualServerRaster::custom_shade_model_get_name(int p_model) const {
  2408. //return rasterizer->custom_shade_model_get_name(p_model);
  2409. return "";
  2410. }
  2411. void VisualServerRaster::custom_shade_model_set_param_info(int p_model, const List<PropertyInfo>& p_info) {
  2412. VS_CHANGED;
  2413. //rasterizer->custom_shade_model_set_param_info(p_model,p_info);
  2414. }
  2415. void VisualServerRaster::custom_shade_model_get_param_info(int p_model, List<PropertyInfo>* p_info) const {
  2416. //rasterizer->custom_shade_model_get_param_info(p_model,p_info);
  2417. }
  2418. void VisualServerRaster::free( RID p_rid ) {
  2419. VS_CHANGED;
  2420. if (rasterizer->is_texture(p_rid) || rasterizer->is_material(p_rid) || rasterizer->is_skeleton(p_rid) || rasterizer->is_shader(p_rid)) {
  2421. rasterizer->free(p_rid);
  2422. } else if (rasterizer->is_mesh(p_rid) || rasterizer->is_multimesh(p_rid) || rasterizer->is_light(p_rid) || rasterizer->is_particles(p_rid) ) {
  2423. //delete the resource
  2424. _free_attached_instances(p_rid);
  2425. rasterizer->free(p_rid);
  2426. } else if (room_owner.owns(p_rid)) {
  2427. _free_attached_instances(p_rid);
  2428. Room *room = room_owner.get(p_rid);
  2429. ERR_FAIL_COND(!room);
  2430. room_owner.free(p_rid);
  2431. memdelete(room);
  2432. } else if (portal_owner.owns(p_rid)) {
  2433. _free_attached_instances(p_rid);
  2434. Portal *portal = portal_owner.get(p_rid);
  2435. ERR_FAIL_COND(!portal);
  2436. portal_owner.free(p_rid);
  2437. memdelete(portal);
  2438. } else if (baked_light_owner.owns(p_rid)) {
  2439. _free_attached_instances(p_rid);
  2440. BakedLight *baked_light = baked_light_owner.get(p_rid);
  2441. ERR_FAIL_COND(!baked_light);
  2442. if (baked_light->data.octree_texture.is_valid())
  2443. rasterizer->free(baked_light->data.octree_texture);
  2444. baked_light_owner.free(p_rid);
  2445. memdelete(baked_light);
  2446. } else if (camera_owner.owns(p_rid)) {
  2447. // delete te camera
  2448. Camera *camera = camera_owner.get(p_rid);
  2449. ERR_FAIL_COND(!camera);
  2450. camera_owner.free( p_rid );
  2451. memdelete(camera);
  2452. } else if (viewport_owner.owns(p_rid)) {
  2453. // delete the viewport
  2454. Viewport *viewport = viewport_owner.get( p_rid );
  2455. ERR_FAIL_COND(!viewport);
  2456. // Viewport *parent=NULL;
  2457. rasterizer->free(viewport->viewport_data);
  2458. if (viewport->render_target.is_valid()) {
  2459. rasterizer->free(viewport->render_target);
  2460. }
  2461. if (viewport->update_list.in_list())
  2462. viewport_update_list.remove(&viewport->update_list);
  2463. if (screen_viewports.has(p_rid))
  2464. screen_viewports.erase(p_rid);
  2465. while(viewport->canvas_map.size()) {
  2466. Canvas *c = viewport->canvas_map.front()->get().canvas;
  2467. c->viewports.erase(p_rid);
  2468. viewport->canvas_map.erase(viewport->canvas_map.front());
  2469. }
  2470. viewport_owner.free(p_rid);
  2471. memdelete(viewport);
  2472. } else if (instance_owner.owns(p_rid)) {
  2473. // delete the instance
  2474. _update_instances(); // be sure
  2475. Instance *instance = instance_owner.get(p_rid);
  2476. ERR_FAIL_COND(!instance);
  2477. instance_set_room(p_rid,RID());
  2478. instance_set_scenario(p_rid,RID());
  2479. instance_geometry_set_baked_light(p_rid,RID());
  2480. instance_set_base(p_rid,RID());
  2481. instance_owner.free(p_rid);
  2482. memdelete(instance);
  2483. } else if (canvas_owner.owns(p_rid)) {
  2484. Canvas *canvas = canvas_owner.get(p_rid);
  2485. ERR_FAIL_COND(!canvas);
  2486. while(canvas->viewports.size()) {
  2487. Viewport *vp = viewport_owner.get(canvas->viewports.front()->get());
  2488. ERR_FAIL_COND(!vp);
  2489. Map<RID,Viewport::CanvasData>::Element *E=vp->canvas_map.find(p_rid);
  2490. ERR_FAIL_COND(!E);
  2491. vp->canvas_map.erase(p_rid);
  2492. canvas->viewports.erase( canvas->viewports.front() );
  2493. }
  2494. for (int i=0;i<canvas->child_items.size();i++) {
  2495. canvas->child_items[i].item->parent=RID();
  2496. }
  2497. canvas_owner.free( p_rid );
  2498. memdelete( canvas );
  2499. } else if (canvas_item_owner.owns(p_rid)) {
  2500. CanvasItem *canvas_item = canvas_item_owner.get(p_rid);
  2501. ERR_FAIL_COND(!canvas_item);
  2502. if (canvas_item->parent.is_valid()) {
  2503. if (canvas_owner.owns(canvas_item->parent)) {
  2504. Canvas *canvas = canvas_owner.get(canvas_item->parent);
  2505. canvas->erase_item(canvas_item);
  2506. } else if (canvas_item_owner.owns(canvas_item->parent)) {
  2507. CanvasItem *item_owner = canvas_item_owner.get(canvas_item->parent);
  2508. item_owner->child_items.erase(canvas_item);
  2509. }
  2510. }
  2511. for (int i=0;i<canvas_item->child_items.size();i++) {
  2512. canvas_item->child_items[i]->parent=RID();
  2513. }
  2514. canvas_item_owner.free( p_rid );
  2515. memdelete( canvas_item );
  2516. } else if (scenario_owner.owns(p_rid)) {
  2517. Scenario *scenario=scenario_owner.get(p_rid);
  2518. ERR_FAIL_COND(!scenario);
  2519. _update_instances(); // be sure
  2520. _free_attached_instances(p_rid,true);
  2521. //rasterizer->free( scenario->environment );
  2522. scenario_owner.free(p_rid);
  2523. memdelete(scenario);
  2524. } else {
  2525. ERR_FAIL();
  2526. }
  2527. }
  2528. void VisualServerRaster::_instance_draw(Instance *p_instance) {
  2529. if (p_instance->light_cache_dirty) {
  2530. int l=0;
  2531. //add positional lights
  2532. InstanceSet::Element *LE=p_instance->lights.front();
  2533. p_instance->data.light_instances.resize(p_instance->lights.size());
  2534. while(LE) {
  2535. p_instance->data.light_instances[l++]=LE->get()->light_info->instance;
  2536. LE=LE->next();
  2537. }
  2538. p_instance->light_cache_dirty=false;
  2539. }
  2540. switch(p_instance->base_type) {
  2541. case INSTANCE_MESH: {
  2542. const float *morphs = NULL;
  2543. if (!p_instance->data.morph_values.empty()) {
  2544. morphs=&p_instance->data.morph_values[0];
  2545. }
  2546. rasterizer->add_mesh(p_instance->base_rid, &p_instance->data);
  2547. } break;
  2548. case INSTANCE_MULTIMESH: {
  2549. rasterizer->add_multimesh(p_instance->base_rid, &p_instance->data);
  2550. } break;
  2551. case INSTANCE_IMMEDIATE: {
  2552. rasterizer->add_immediate(p_instance->base_rid, &p_instance->data);
  2553. } break;
  2554. case INSTANCE_PARTICLES: {
  2555. rasterizer->add_particles(p_instance->particles_info->instance, &p_instance->data);
  2556. } break;
  2557. default: {};
  2558. }
  2559. }
  2560. Vector<Vector3> VisualServerRaster::_camera_generate_endpoints(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  2561. // setup a camera matrix for that range!
  2562. CameraMatrix camera_matrix;
  2563. switch(p_camera->type) {
  2564. case Camera::ORTHOGONAL: {
  2565. camera_matrix.set_orthogonal(p_camera->size,viewport_rect.width / (float)viewport_rect.height,p_range_min,p_range_max,p_camera->vaspect);
  2566. } break;
  2567. case Camera::PERSPECTIVE: {
  2568. camera_matrix.set_perspective(
  2569. p_camera->fov,
  2570. viewport_rect.width / (float)viewport_rect.height,
  2571. p_range_min,
  2572. p_range_max,
  2573. p_camera->vaspect
  2574. );
  2575. } break;
  2576. }
  2577. //obtain the frustum endpoints
  2578. Vector<Vector3> endpoints;
  2579. endpoints.resize(8);
  2580. bool res = camera_matrix.get_endpoints(p_camera->transform,&endpoints[0]);
  2581. ERR_FAIL_COND_V(!res,Vector<Vector3>());
  2582. return endpoints;
  2583. }
  2584. Vector<Plane> VisualServerRaster::_camera_generate_orthogonal_planes(Instance *p_light,Camera *p_camera,float p_range_min, float p_range_max) {
  2585. Vector<Vector3> endpoints=_camera_generate_endpoints(p_light,p_camera,p_range_min,p_range_max); // frustum plane endpoints
  2586. ERR_FAIL_COND_V(endpoints.empty(),Vector<Plane>());
  2587. // obtain the light frustm ranges (given endpoints)
  2588. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2589. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2590. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2591. float x_min,x_max;
  2592. float y_min,y_max;
  2593. float z_min,z_max;
  2594. for(int j=0;j<8;j++) {
  2595. float d_x=x_vec.dot(endpoints[j]);
  2596. float d_y=y_vec.dot(endpoints[j]);
  2597. float d_z=z_vec.dot(endpoints[j]);
  2598. if (j==0 || d_x<x_min)
  2599. x_min=d_x;
  2600. if (j==0 || d_x>x_max)
  2601. x_max=d_x;
  2602. if (j==0 || d_y<y_min)
  2603. y_min=d_y;
  2604. if (j==0 || d_y>y_max)
  2605. y_max=d_y;
  2606. if (j==0 || d_z<z_min)
  2607. z_min=d_z;
  2608. if (j==0 || d_z>z_max)
  2609. z_max=d_z;
  2610. }
  2611. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2612. Vector<Plane> light_frustum_planes;
  2613. light_frustum_planes.resize(6);
  2614. //right/left
  2615. light_frustum_planes[0]=Plane( x_vec, x_max );
  2616. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  2617. //top/bottom
  2618. light_frustum_planes[2]=Plane( y_vec, y_max );
  2619. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  2620. //near/far
  2621. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  2622. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  2623. //TODO@ add more actual frustum planes to minimize get
  2624. return light_frustum_planes;
  2625. }
  2626. void VisualServerRaster::_light_instance_update_pssm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2627. int splits = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  2628. float split_weight=rasterizer->light_directional_get_shadow_param(p_light->base_rid,LIGHT_DIRECTIONAL_SHADOW_PARAM_PSSM_SPLIT_WEIGHT);
  2629. float distances[5];
  2630. float texsize=rasterizer->light_instance_get_shadow_size( p_light->light_info->instance );
  2631. // float cull_min=p_cull_range.min;
  2632. //float cull_max=p_cull_range.max;
  2633. bool overlap = rasterizer->light_instance_get_pssm_shadow_overlap(p_light->light_info->instance);
  2634. float cull_min=p_camera->znear;
  2635. float cull_max=p_camera->zfar;
  2636. float max_dist = rasterizer->light_directional_get_shadow_param(p_light->base_rid,VS::LIGHT_DIRECTIONAL_SHADOW_PARAM_MAX_DISTANCE);
  2637. if (max_dist>0.0)
  2638. cull_max=MIN(cull_max,max_dist);
  2639. for(int i = 0; i < splits; i++) {
  2640. float idm = i / (float)splits;
  2641. float lg = cull_min * Math::pow(cull_max/cull_min, idm);
  2642. float uniform = cull_min + (cull_max - cull_min) * idm;
  2643. distances[i] = lg * split_weight + uniform * (1.0 - split_weight);
  2644. }
  2645. distances[0]=cull_min;
  2646. distances[splits]=cull_max;
  2647. for (int i=0;i<splits;i++) {
  2648. // setup a camera matrix for that range!
  2649. CameraMatrix camera_matrix;
  2650. switch(p_camera->type) {
  2651. case Camera::ORTHOGONAL: {
  2652. camera_matrix.set_orthogonal(
  2653. p_camera->size,
  2654. viewport_rect.width / (float)viewport_rect.height,
  2655. distances[(i==0 || !overlap )?i:i-1],
  2656. distances[i+1],
  2657. p_camera->vaspect
  2658. );
  2659. } break;
  2660. case Camera::PERSPECTIVE: {
  2661. camera_matrix.set_perspective(
  2662. p_camera->fov,
  2663. viewport_rect.width / (float)viewport_rect.height,
  2664. distances[(i==0 || !overlap )?i:i-1],
  2665. distances[i+1],
  2666. p_camera->vaspect
  2667. );
  2668. } break;
  2669. }
  2670. //obtain the frustum endpoints
  2671. Vector3 endpoints[8]; // frustum plane endpoints
  2672. bool res = camera_matrix.get_endpoints(p_camera->transform,endpoints);
  2673. ERR_CONTINUE(!res);
  2674. // obtain the light frustm ranges (given endpoints)
  2675. Vector3 x_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2676. Vector3 y_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2677. Vector3 z_vec=p_light->data.transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2678. //z_vec points agsint the camera, like in default opengl
  2679. float x_min,x_max;
  2680. float y_min,y_max;
  2681. float z_min,z_max;
  2682. float x_min_cam,x_max_cam;
  2683. float y_min_cam,y_max_cam;
  2684. float z_min_cam,z_max_cam;
  2685. //used for culling
  2686. for(int j=0;j<8;j++) {
  2687. float d_x=x_vec.dot(endpoints[j]);
  2688. float d_y=y_vec.dot(endpoints[j]);
  2689. float d_z=z_vec.dot(endpoints[j]);
  2690. if (j==0 || d_x<x_min)
  2691. x_min=d_x;
  2692. if (j==0 || d_x>x_max)
  2693. x_max=d_x;
  2694. if (j==0 || d_y<y_min)
  2695. y_min=d_y;
  2696. if (j==0 || d_y>y_max)
  2697. y_max=d_y;
  2698. if (j==0 || d_z<z_min)
  2699. z_min=d_z;
  2700. if (j==0 || d_z>z_max)
  2701. z_max=d_z;
  2702. }
  2703. {
  2704. //camera viewport stuff
  2705. //this trick here is what stabilizes the shadow (make potential jaggies to not move)
  2706. //at the cost of some wasted resolution. Still the quality increase is very well worth it
  2707. Vector3 center;
  2708. for(int j=0;j<8;j++) {
  2709. center+=endpoints[j];
  2710. }
  2711. center/=8.0;
  2712. //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;
  2713. float radius=0;
  2714. for(int j=0;j<8;j++) {
  2715. float d = center.distance_to(endpoints[j]);
  2716. if (d>radius)
  2717. radius=d;
  2718. }
  2719. radius *= texsize/(texsize-2.0); //add a texel by each side, so stepified texture will always fit
  2720. x_max_cam=x_vec.dot(center)+radius;
  2721. x_min_cam=x_vec.dot(center)-radius;
  2722. y_max_cam=y_vec.dot(center)+radius;
  2723. y_min_cam=y_vec.dot(center)-radius;
  2724. z_max_cam=z_vec.dot(center)+radius;
  2725. z_min_cam=z_vec.dot(center)-radius;
  2726. float unit = radius*2.0/texsize;
  2727. x_max_cam=Math::stepify(x_max_cam,unit);
  2728. x_min_cam=Math::stepify(x_min_cam,unit);
  2729. y_max_cam=Math::stepify(y_max_cam,unit);
  2730. y_min_cam=Math::stepify(y_min_cam,unit);
  2731. }
  2732. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2733. Vector<Plane> light_frustum_planes;
  2734. light_frustum_planes.resize(6);
  2735. //right/left
  2736. light_frustum_planes[0]=Plane( x_vec, x_max );
  2737. light_frustum_planes[1]=Plane( -x_vec, -x_min );
  2738. //top/bottom
  2739. light_frustum_planes[2]=Plane( y_vec, y_max );
  2740. light_frustum_planes[3]=Plane( -y_vec, -y_min );
  2741. //near/far
  2742. light_frustum_planes[4]=Plane( z_vec, z_max+1e6 );
  2743. light_frustum_planes[5]=Plane( -z_vec, -z_min ); // z_min is ok, since casters further than far-light plane are not needed
  2744. int caster_cull_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2745. // a pre pass will need to be needed to determine the actual z-near to be used
  2746. for(int j=0;j<caster_cull_count;j++) {
  2747. float min,max;
  2748. Instance *ins=instance_shadow_cull_result[j];
  2749. if (!ins->visible || !ins->cast_shadows)
  2750. continue;
  2751. ins->transformed_aabb.project_range_in_plane(Plane(z_vec,0),min,max);
  2752. if (max>z_max)
  2753. z_max=max;
  2754. }
  2755. {
  2756. CameraMatrix ortho_camera;
  2757. real_t half_x = (x_max_cam-x_min_cam) * 0.5;
  2758. real_t half_y = (y_max_cam-y_min_cam) * 0.5;
  2759. ortho_camera.set_orthogonal( -half_x, half_x,-half_y,half_y, 0, (z_max-z_min_cam) );
  2760. Transform ortho_transform;
  2761. ortho_transform.basis=p_light->data.transform.basis;
  2762. ortho_transform.origin=x_vec*(x_min_cam+half_x)+y_vec*(y_min_cam+half_y)+z_vec*z_max;
  2763. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance, i, ortho_camera, ortho_transform,distances[i],distances[i+1] );
  2764. }
  2765. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  2766. for (int j=0;j<caster_cull_count;j++) {
  2767. Instance *instance = instance_shadow_cull_result[j];
  2768. if (!instance->visible || !instance->cast_shadows)
  2769. continue;
  2770. _instance_draw(instance);
  2771. }
  2772. rasterizer->end_shadow_map();
  2773. }
  2774. }
  2775. CameraMatrix _lispm_look( const Vector3 pos, const Vector3 dir, const Vector3 up) {
  2776. Vector3 dirN;
  2777. Vector3 upN;
  2778. Vector3 lftN;
  2779. lftN=dir.cross(up);
  2780. lftN.normalize();
  2781. upN=lftN.cross(dir);
  2782. upN.normalize();
  2783. dirN=dir.normalized();
  2784. CameraMatrix cmout;
  2785. float *output=&cmout.matrix[0][0];
  2786. output[ 0] = lftN[0];
  2787. output[ 1] = upN[0];
  2788. output[ 2] = -dirN[0];
  2789. output[ 3] = 0.0;
  2790. output[ 4] = lftN[1];
  2791. output[ 5] = upN[1];
  2792. output[ 6] = -dirN[1];
  2793. output[ 7] = 0.0;
  2794. output[ 8] = lftN[2];
  2795. output[ 9] = upN[2];
  2796. output[10] = -dirN[2];
  2797. output[11] = 0.0;
  2798. output[12] = -lftN.dot(pos);
  2799. output[13] = -upN.dot(pos);
  2800. output[14] = dirN.dot(pos);
  2801. output[15] = 1.0;
  2802. return cmout;
  2803. }
  2804. #if 1
  2805. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2806. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  2807. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  2808. float viewdot = light_vec.normalized().dot(view_vec.normalized());
  2809. float near_dist=1;
  2810. Vector<Plane> light_frustum_planes = _camera_generate_orthogonal_planes(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2811. int caster_count = p_scenario->octree.cull_convex(light_frustum_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2812. // this could be faster by just getting supports from the AABBs..
  2813. // but, safer to do as the original implementation explains for now..
  2814. Vector<Vector3> caster_pointcloud;
  2815. caster_pointcloud.resize(caster_count*8);
  2816. int caster_pointcloud_size=0;
  2817. {
  2818. //fill pointcloud
  2819. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2820. for(int i=0;i<caster_count;i++) {
  2821. Instance *ins = instance_shadow_cull_result[i];
  2822. if (!ins->visible || !ins->cast_shadows)
  2823. continue;
  2824. for(int j=0;j<8;j++) {
  2825. Vector3 v = ins->aabb.get_endpoint(j);
  2826. v = ins->data.transform.xform(v);
  2827. caster_pointcloud_ptr[caster_pointcloud_size+j]=v;
  2828. }
  2829. caster_pointcloud_size+=8;
  2830. }
  2831. }
  2832. // now generate a pointcloud that contains the maximum bound (camera extruded by light)
  2833. Vector<Vector3> camera_pointcloud = _camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2834. int cpcsize=camera_pointcloud.size();
  2835. camera_pointcloud.resize( cpcsize*2 );
  2836. for(int i=0;i<cpcsize;i++) {
  2837. camera_pointcloud[i+cpcsize]=camera_pointcloud[i]-light_vec*1000;
  2838. }
  2839. // Vector<Vector3> frustum_points=_camera_generate_endpoints(p_light,p_camera,p_cull_range.min,p_cull_range.max);
  2840. // compute the "light-space" basis, using the algorithm described in the paper
  2841. // note: since bodyB is defined in eye space, all of these vectors should also be defined in eye space
  2842. Vector3 eye = p_camera->transform.origin;
  2843. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  2844. CameraMatrix light_space_basis = _lispm_look(eye,light_vec,up);
  2845. AABB light_space_aabb;
  2846. { //create an optimal AABB from both the camera pointcloud and the objects pointcloud
  2847. AABB light_space_pointcloud_aabb;
  2848. AABB light_space_camera_aabb;
  2849. //xform pointcloud
  2850. const Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2851. for(int i=0;i<caster_pointcloud_size;i++) {
  2852. Vector3 p = light_space_basis.xform(caster_pointcloud_ptr[i]);
  2853. if (i==0) {
  2854. light_space_pointcloud_aabb.pos=p;
  2855. } else {
  2856. light_space_pointcloud_aabb.expand_to(p);
  2857. }
  2858. }
  2859. for(int i=0;i<camera_pointcloud.size();i++) {
  2860. Vector3 p = light_space_basis.xform(camera_pointcloud[i]);
  2861. if (i==0) {
  2862. light_space_camera_aabb.pos=p;
  2863. } else {
  2864. light_space_camera_aabb.expand_to(p);
  2865. }
  2866. }
  2867. light_space_aabb=light_space_pointcloud_aabb.intersection(light_space_camera_aabb);
  2868. }
  2869. float lvdp = light_vec.dot(view_vec);
  2870. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  2871. //use the formulas of the paper to get n (and f)
  2872. float factor = 1.0/sin_gamma;
  2873. float z_n = factor*near_dist; //often 1
  2874. float d = Math::abs(light_space_aabb.size.y); //perspective transform depth //light space y extents
  2875. float z_f = z_n + d*sin_gamma;
  2876. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  2877. float f = n+d;
  2878. Vector3 pos = eye - up*(n-near_dist);
  2879. CameraMatrix light_space_basis2 = _lispm_look(pos,light_vec,up);
  2880. //Transform light_space_basis2;
  2881. //light_space_basis2.set_look_at(pos,light_vec-pos,up);
  2882. //light_space_basis2.affine_invert();
  2883. //one possibility for a simple perspective transformation matrix
  2884. //with the two parameters n(near) and f(far) in y direction
  2885. CameraMatrix lisp_matrix;
  2886. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  2887. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  2888. lisp_matrix.matrix[1][3]=1;
  2889. lisp_matrix.matrix[3][3]=0;
  2890. CameraMatrix projection = lisp_matrix * light_space_basis2;
  2891. //CameraMatrix projection = light_space_basis2 * lisp_matrix;
  2892. AABB proj_space_aabb;
  2893. float max_d,min_d;
  2894. {
  2895. AABB proj_space_pointcloud_aabb;
  2896. AABB proj_space_camera_aabb;
  2897. //xform pointcloud
  2898. Vector3* caster_pointcloud_ptr=&caster_pointcloud[0];
  2899. for(int i=0;i<caster_pointcloud_size;i++) {
  2900. Vector3 p = projection.xform(caster_pointcloud_ptr[i]);
  2901. if (i==0) {
  2902. proj_space_pointcloud_aabb.pos=p;
  2903. } else {
  2904. proj_space_pointcloud_aabb.expand_to(p);
  2905. }
  2906. }
  2907. for(int i=0;i<camera_pointcloud.size();i++) {
  2908. Vector3 p = projection.xform(camera_pointcloud[i]);
  2909. if (i==0) {
  2910. proj_space_camera_aabb.pos=p;
  2911. } else {
  2912. proj_space_camera_aabb.expand_to(p);
  2913. }
  2914. }
  2915. //proj_space_aabb=proj_space_pointcloud_aabb.intersection_with(proj_space_camera_aabb);
  2916. proj_space_aabb=proj_space_pointcloud_aabb;
  2917. }
  2918. projection.scale_translate_to_fit(proj_space_aabb);
  2919. projection=projection * lisp_matrix;
  2920. CameraMatrix scale;
  2921. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  2922. projection=scale * projection;
  2923. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection , light_space_basis2.inverse() );
  2924. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  2925. for(int i=0;i<caster_count;i++) {
  2926. Instance *instance = instance_shadow_cull_result[i];
  2927. if (!instance->visible || !instance->cast_shadows)
  2928. continue;
  2929. _instance_draw(instance);
  2930. }
  2931. rasterizer->end_shadow_map();
  2932. }
  2933. #else
  2934. void VisualServerRaster::_light_instance_update_lispsm_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  2935. /* STEP 1: GENERATE LIGHT TRANSFORM */
  2936. Vector3 light_vec = -p_light->data.transform.basis.get_axis(2);
  2937. Vector3 view_vec = -p_camera->transform.basis.get_axis(2);
  2938. float viewdot = Math::absf(light_vec.dot(view_vec));
  2939. Vector3 up = light_vec.cross(view_vec).cross(light_vec).normalized();
  2940. Transform light_transform;
  2941. light_transform.set_look_at(Vector3(),light_vec,up);
  2942. /* STEP 2: GENERATE WORDLSPACE PLANES AND VECTORS*/
  2943. float range_min=0.01; //p_cull_range.min
  2944. float range_max=20;//p_cull_range.max;
  2945. Vector<Vector3> camera_endpoints=_camera_generate_endpoints(p_light,p_camera,range_min,range_max); // frustum plane endpoints
  2946. ERR_FAIL_COND(camera_endpoints.empty());
  2947. // obtain the light frustm ranges (given endpoints)
  2948. Vector3 light_x_vec=light_transform.basis.get_axis( Vector3::AXIS_X ).normalized();
  2949. Vector3 light_y_vec=light_transform.basis.get_axis( Vector3::AXIS_Y ).normalized();
  2950. Vector3 light_z_vec=light_transform.basis.get_axis( Vector3::AXIS_Z ).normalized();
  2951. Vector3 light_axis_max;
  2952. Vector3 light_axis_min;
  2953. for(int j=0;j<8;j++) {
  2954. float d_x=light_x_vec.dot(camera_endpoints[j]);
  2955. float d_y=light_y_vec.dot(camera_endpoints[j]);
  2956. float d_z=light_z_vec.dot(camera_endpoints[j]);
  2957. if (j==0 || d_x<light_axis_min.x)
  2958. light_axis_min.x=d_x;
  2959. if (j==0 || d_x>light_axis_max.x)
  2960. light_axis_max.x=d_x;
  2961. if (j==0 || d_y<light_axis_min.y)
  2962. light_axis_min.y=d_y;
  2963. if (j==0 || d_y>light_axis_max.y)
  2964. light_axis_max.y=d_y;
  2965. if (j==0 || d_z<light_axis_min.z)
  2966. light_axis_min.z=d_z;
  2967. if (j==0 || d_z>light_axis_max.z)
  2968. light_axis_max.z=d_z;
  2969. }
  2970. //now that we now all ranges, we can proceed to make the light frustum planes, for culling octree
  2971. Vector<Plane> light_cull_planes;
  2972. light_cull_planes.resize(6);
  2973. //right/left
  2974. light_cull_planes[0]=Plane( light_x_vec, light_axis_max.x );
  2975. light_cull_planes[1]=Plane( -light_x_vec, -light_axis_min.x );
  2976. //top/bottom
  2977. light_cull_planes[2]=Plane( light_y_vec, light_axis_max.y );
  2978. light_cull_planes[3]=Plane( -light_y_vec, -light_axis_min.y );
  2979. //near/far
  2980. light_cull_planes[4]=Plane( light_z_vec, light_axis_max.z+1e6 );
  2981. 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
  2982. /* STEP 3: CULL CASTERS */
  2983. int caster_count = p_scenario->octree.cull_convex(light_cull_planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  2984. /* STEP 4: ADJUST FAR Z PLANE */
  2985. float caster_max_z=1e-1;
  2986. for(int i=0;i<caster_count;i++) {
  2987. Instance *ins=instance_shadow_cull_result[i];
  2988. if (!ins->visible || !ins->cast_shadows)
  2989. continue;
  2990. //@TODO optimize using support mapping
  2991. for(int j=0;j<8;j++) {
  2992. Vector3 v=ins->data.transform.xform(ins->aabb.get_endpoint(j));
  2993. float d = light_z_vec.dot(v);
  2994. if (d>caster_max_z)
  2995. caster_max_z=d;
  2996. }
  2997. }
  2998. float expand = caster_max_z-light_axis_max.z;
  2999. if (expand<0)
  3000. expand=0;
  3001. light_axis_max.z=MAX(caster_max_z,light_axis_max.z);
  3002. /* STEP 5: CREATE ORTHOGONAL PROJECTION */
  3003. CameraMatrix light_projection;
  3004. real_t half_x = (light_axis_max.x-light_axis_min.x) * 0.5;
  3005. real_t half_y = (light_axis_max.y-light_axis_min.y) * 0.5;
  3006. light_projection.set_orthogonal( -half_x, half_x,half_y, -half_y, 0, (light_axis_max.z-light_axis_min.z) );
  3007. 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;
  3008. if (/*false &&*/ viewdot<0.96) {
  3009. float lvdp = light_vec.dot(view_vec);
  3010. float near_dist=1.0;
  3011. float sin_gamma = Math::sqrt(1.0-lvdp*lvdp);
  3012. //use the formulas of the paper to get n (and f)
  3013. float factor = 1.0/sin_gamma;
  3014. float z_n = factor*near_dist; //often 1
  3015. float d = Math::abs(light_axis_max.y-light_axis_min.y); //perspective transform depth //light space y extents
  3016. float z_f = z_n + d*sin_gamma;
  3017. float n = (z_n+Math::sqrt(z_f*z_n))/sin_gamma;
  3018. float f = n+d;
  3019. CameraMatrix lisp_matrix;
  3020. lisp_matrix.matrix[1][1]=(f+n)/(f-n);
  3021. lisp_matrix.matrix[3][1]=-2*f*n/(f-n);
  3022. lisp_matrix.matrix[1][3]=1;
  3023. lisp_matrix.matrix[3][3]=0;
  3024. Vector3 pos = p_camera->transform.origin - up*(n-near_dist);
  3025. CameraMatrix world2light = _lispm_look(pos,light_vec,up);
  3026. CameraMatrix projection = lisp_matrix * world2light;
  3027. AABB projection_bounds;
  3028. for(int i=0;i<camera_endpoints.size();i++) {
  3029. Vector3 p=camera_endpoints[i];
  3030. if (i==0)
  3031. projection_bounds.pos=projection.xform(p);
  3032. else
  3033. projection_bounds.expand_to(projection.xform(p));
  3034. projection_bounds.expand_to(projection.xform(p+light_vec*-expand));
  3035. }
  3036. CameraMatrix scaletrans;
  3037. scaletrans.scale_translate_to_fit(projection_bounds);
  3038. projection=scaletrans * lisp_matrix;
  3039. CameraMatrix scale;
  3040. scale.make_scale(Vector3(1.0,1.0,-1.0)); // transform to left handed
  3041. projection=scale * projection;
  3042. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, projection, world2light.inverse(), viewdot);
  3043. } else {
  3044. //orthogonal
  3045. rasterizer->light_instance_set_shadow_transform(p_light->light_info->instance,0, light_projection , light_transform, viewdot);
  3046. }
  3047. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3048. for(int i=0;i<caster_count;i++) {
  3049. Instance *instance = instance_shadow_cull_result[i];
  3050. if (!instance->visible || !instance->cast_shadows)
  3051. continue;
  3052. _instance_draw(instance);
  3053. }
  3054. rasterizer->end_shadow_map();
  3055. }
  3056. #endif
  3057. void VisualServerRaster::_light_instance_update_shadow(Instance *p_light,Scenario *p_scenario,Camera *p_camera,const CullRange& p_cull_range) {
  3058. if (!rasterizer->shadow_allocate_near( p_light->light_info->instance ))
  3059. return; // shadow could not be updated
  3060. /* VisualServerRaster supports for many shadow techniques, using the one the rasterizer requests */
  3061. Rasterizer::ShadowType shadow_type = rasterizer->light_instance_get_shadow_type(p_light->light_info->instance);
  3062. switch(shadow_type) {
  3063. case Rasterizer::SHADOW_SIMPLE: {
  3064. /* SPOT SHADOW */
  3065. rasterizer->begin_shadow_map( p_light->light_info->instance, 0 );
  3066. //using this one ensures that raster deferred will have it
  3067. float far = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3068. float angle = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_SPOT_ANGLE );
  3069. CameraMatrix cm;
  3070. cm.set_perspective( angle*2.0, 1.0, 0.001, far );
  3071. Vector<Plane> planes = cm.get_projection_planes(p_light->data.transform);
  3072. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3073. for (int i=0;i<cull_count;i++) {
  3074. Instance *instance = instance_shadow_cull_result[i];
  3075. if (!instance->visible || !instance->cast_shadows)
  3076. continue;
  3077. _instance_draw(instance);
  3078. }
  3079. rasterizer->end_shadow_map();
  3080. } break;
  3081. case Rasterizer::SHADOW_DUAL_PARABOLOID: {
  3082. /* OMNI SHADOW */
  3083. int passes = rasterizer->light_instance_get_shadow_passes( p_light->light_info->instance );
  3084. if (passes==2) {
  3085. for(int i=0;i<2;i++) {
  3086. rasterizer->begin_shadow_map( p_light->light_info->instance, i );
  3087. //using this one ensures that raster deferred will have it
  3088. float radius = rasterizer->light_get_var( p_light->base_rid, VS::LIGHT_PARAM_RADIUS);
  3089. float z =i==0?-1:1;
  3090. Vector<Plane> planes;
  3091. planes.resize(5);
  3092. planes[0]=p_light->data.transform.xform(Plane(Vector3(0,0,z),radius));
  3093. planes[1]=p_light->data.transform.xform(Plane(Vector3(1,0,z).normalized(),radius));
  3094. planes[2]=p_light->data.transform.xform(Plane(Vector3(-1,0,z).normalized(),radius));
  3095. planes[3]=p_light->data.transform.xform(Plane(Vector3(0,1,z).normalized(),radius));
  3096. planes[4]=p_light->data.transform.xform(Plane(Vector3(0,-1,z).normalized(),radius));
  3097. int cull_count = p_scenario->octree.cull_convex(planes,instance_shadow_cull_result,MAX_INSTANCE_CULL,INSTANCE_GEOMETRY_MASK);
  3098. for (int j=0;j<cull_count;j++) {
  3099. Instance *instance = instance_shadow_cull_result[j];
  3100. if (!instance->visible || !instance->cast_shadows)
  3101. continue;
  3102. _instance_draw(instance);
  3103. }
  3104. rasterizer->end_shadow_map();
  3105. }
  3106. } else if (passes==1) {
  3107. //one go
  3108. }
  3109. } break;
  3110. case Rasterizer::SHADOW_CUBE: {
  3111. // todo
  3112. } break;
  3113. case Rasterizer::SHADOW_ORTHOGONAL: {
  3114. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3115. } break;
  3116. case Rasterizer::SHADOW_PSSM: {
  3117. _light_instance_update_pssm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3118. } break;
  3119. case Rasterizer::SHADOW_PSM: {
  3120. _light_instance_update_lispsm_shadow(p_light,p_scenario,p_camera,p_cull_range);
  3121. // todo
  3122. } break;
  3123. default: {}
  3124. }
  3125. }
  3126. void VisualServerRaster::_portal_disconnect(Instance *p_portal,bool p_cleanup) {
  3127. if (p_portal->portal_info->connected) {
  3128. //disconnect first
  3129. p_portal->portal_info->connected->portal_info->connected=NULL;
  3130. p_portal->portal_info->connected=NULL;
  3131. }
  3132. if (p_portal->room && p_portal->room->room) {
  3133. if (p_cleanup) {
  3134. p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3135. //p_portal->room->room->room_info->disconnected_child_portals.erase(p_portal);
  3136. } else {
  3137. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3138. }
  3139. }
  3140. }
  3141. void VisualServerRaster::_instance_validate_autorooms(Instance *p_geometry) {
  3142. if (p_geometry->auto_rooms.size()==0)
  3143. return;
  3144. p_geometry->valid_auto_rooms.clear();
  3145. int point_count = aabb_random_points.size();
  3146. const Vector3 * src_points = &aabb_random_points[0];
  3147. for(Set<Instance*>::Element *E=p_geometry->valid_auto_rooms.front();E;E=E->next()) {
  3148. Instance *room = E->get();
  3149. Vector3 *dst_points=&transformed_aabb_random_points[0];
  3150. //generate points
  3151. for(int i=0;i<point_count;i++) {
  3152. 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));
  3153. }
  3154. int pass = room->room_info->room->bounds.get_points_inside(dst_points,point_count);
  3155. float ratio = (float)pass / point_count;
  3156. if (ratio>0.5) // should make some constant
  3157. p_geometry->valid_auto_rooms.insert(room);
  3158. }
  3159. }
  3160. void VisualServerRaster::_portal_attempt_connect(Instance *p_portal) {
  3161. _portal_disconnect(p_portal);
  3162. Vector3 A_norm = p_portal->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3163. Plane A_plane( p_portal->data.transform.origin, A_norm );
  3164. float A_surface = p_portal->portal_info->portal->bounds.get_area();
  3165. if (A_surface==0)
  3166. return; //wtf
  3167. Instance *found=NULL;
  3168. Transform affine_inverse = p_portal->data.transform.affine_inverse();
  3169. for(Set<Instance*>::Element *E=p_portal->portal_info->candidate_set.front();E;E=E->next()) {
  3170. Instance *B = E->get();
  3171. if (B->portal_info->connected)
  3172. continue; // in use
  3173. Vector3 B_norm = B->data.transform.basis.get_axis(Vector3::AXIS_Z).normalized();
  3174. // check that they are in front of another
  3175. float dot = A_norm.dot(-B_norm);
  3176. if (dot<0.707) // 45 degrees, TODO unharcode this
  3177. continue;
  3178. // check the max distance to the other portal
  3179. bool valid=true;
  3180. Rect2 local_bounds;
  3181. for(int i=0;i<B->portal_info->portal->shape.size();i++) {
  3182. Point2 point2 = B->portal_info->portal->shape[i];
  3183. Vector3 point = B->data.transform.xform( Vector3( point2.x, point2.y, 0 ) );
  3184. float dist = Math::abs(A_plane.distance_to(point));
  3185. if (
  3186. dist>p_portal->portal_info->portal->connect_range ||
  3187. dist>B->portal_info->portal->connect_range ) {
  3188. valid=false;
  3189. break;
  3190. }
  3191. Vector3 point_local = affine_inverse.xform(A_plane.project(point));
  3192. point2 = Point2(point_local.x,point_local.y);
  3193. if (i==0)
  3194. local_bounds.pos=point2;
  3195. else
  3196. local_bounds.expand_to(point2);
  3197. }
  3198. if (!valid)
  3199. continue;
  3200. float B_surface = B->portal_info->portal->bounds.get_area();
  3201. if (B_surface==0)
  3202. continue; //wtf
  3203. float clip_area = p_portal->portal_info->portal->bounds.clip(local_bounds).get_area();
  3204. //check that most of the area is shared
  3205. if ( (clip_area/A_surface) < 0.5 || (clip_area/B_surface) < 0.5) // TODO change for something else
  3206. continue;
  3207. found=B;
  3208. break;
  3209. }
  3210. if (!found) {
  3211. if (p_portal->room && p_portal->room->room) {
  3212. p_portal->room->room->room_info->disconnected_child_portals.insert(p_portal);
  3213. }
  3214. return;
  3215. }
  3216. p_portal->portal_info->connected=found;
  3217. found->portal_info->connected=p_portal;
  3218. }
  3219. void* VisualServerRaster::instance_pair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int) {
  3220. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3221. Instance *A = p_A;
  3222. Instance *B = p_B;
  3223. if (A->base_type==INSTANCE_PORTAL) {
  3224. ERR_FAIL_COND_V( B->base_type!=INSTANCE_PORTAL,NULL );
  3225. A->portal_info->candidate_set.insert(B);
  3226. B->portal_info->candidate_set.insert(A);
  3227. self->_portal_attempt_connect(A);
  3228. //attempt to conncet portal A (will go through B anyway)
  3229. //this is a little hackish, but works fine in practice
  3230. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3231. if (B->base_type==INSTANCE_ROOM)
  3232. SWAP(A,B);
  3233. ERR_FAIL_COND_V(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ),NULL);
  3234. B->auto_rooms.insert(A);
  3235. A->room_info->owned_autoroom_geometry.insert(B);
  3236. self->_instance_validate_autorooms(B);
  3237. } else {
  3238. if (B->base_type==INSTANCE_LIGHT) {
  3239. SWAP(A,B);
  3240. } else if (A->base_type!=INSTANCE_LIGHT) {
  3241. return NULL;
  3242. }
  3243. A->light_info->affected.insert(B);
  3244. B->lights.insert(A);
  3245. B->light_cache_dirty=true;
  3246. }
  3247. return NULL;
  3248. }
  3249. void VisualServerRaster::instance_unpair(void *p_self, OctreeElementID, Instance *p_A,int, OctreeElementID, Instance *p_B,int,void*) {
  3250. VisualServerRaster *self = (VisualServerRaster*)p_self;
  3251. Instance *A = p_A;
  3252. Instance *B = p_B;
  3253. if (A->base_type==INSTANCE_PORTAL) {
  3254. ERR_FAIL_COND( B->base_type!=INSTANCE_PORTAL );
  3255. A->portal_info->candidate_set.erase(B);
  3256. B->portal_info->candidate_set.erase(A);
  3257. //after disconnecting them, see if they can connect again
  3258. self->_portal_attempt_connect(A);
  3259. self->_portal_attempt_connect(B);
  3260. } else if (A->base_type==INSTANCE_ROOM || B->base_type==INSTANCE_ROOM) {
  3261. if (B->base_type==INSTANCE_ROOM)
  3262. SWAP(A,B);
  3263. ERR_FAIL_COND(! ((1<<B->base_type)&INSTANCE_GEOMETRY_MASK ));
  3264. B->auto_rooms.erase(A);
  3265. B->valid_auto_rooms.erase(A);
  3266. A->room_info->owned_autoroom_geometry.erase(B);
  3267. }else {
  3268. if (B->base_type==INSTANCE_LIGHT) {
  3269. SWAP(A,B);
  3270. } else if (A->base_type!=INSTANCE_LIGHT) {
  3271. return;
  3272. }
  3273. A->light_info->affected.erase(B);
  3274. B->lights.erase(A);
  3275. B->light_cache_dirty=true;
  3276. }
  3277. }
  3278. bool VisualServerRaster::_test_portal_cull(Camera *p_camera, Instance *p_from_portal, Instance *p_to_portal) {
  3279. int src_point_count=p_from_portal->portal_info->transformed_point_cache.size();
  3280. int dst_point_count=p_to_portal->portal_info->transformed_point_cache.size();
  3281. if (src_point_count<2 || dst_point_count<2)
  3282. return false;
  3283. const Vector3 *src_points=&p_from_portal->portal_info->transformed_point_cache[0];
  3284. const Vector3 *dst_points=&p_to_portal->portal_info->transformed_point_cache[0];
  3285. bool outside=false;
  3286. bool clockwise = !p_from_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin);
  3287. for(int i=0;i<src_point_count;i++) {
  3288. const Vector3& point_prev = src_points[i?(i-1):(src_point_count-1)];
  3289. const Vector3& point = src_points[i];
  3290. Plane p = clockwise?Plane(p_camera->transform.origin,point,point_prev):Plane(p_camera->transform.origin,point_prev,point);
  3291. bool all_over=true;
  3292. for(int j=0;j<dst_point_count;j++) {
  3293. if (!p.is_point_over(dst_points[j])) {
  3294. all_over=false;
  3295. break;
  3296. }
  3297. }
  3298. if (all_over) {
  3299. outside=true;
  3300. break;
  3301. }
  3302. }
  3303. return !outside;
  3304. }
  3305. void VisualServerRaster::_cull_portal(Camera *p_camera, Instance *p_portal,Instance *p_from_portal) {
  3306. ERR_FAIL_COND(!p_portal->scenario); //scenario outside
  3307. Instance *portal = p_portal;
  3308. if (!portal->room) {
  3309. return; //portals need all to belong to a room, it may be unconfigured yet
  3310. } else if (portal->last_render_pass!=render_pass) {
  3311. return; //invalid portal, ignore
  3312. } else if (portal->portal_info->last_visited_pass==render_pass) {
  3313. return; //portal already visited
  3314. } else if (portal==p_from_portal) {
  3315. return; // came from this portal, don't even bother testing
  3316. }
  3317. /* TEST DISABLE DISTANCE */
  3318. float disable_distance = p_portal->portal_info->portal->disable_distance;
  3319. if (disable_distance) {
  3320. //has disable distance..
  3321. float distance = p_camera->transform.origin.distance_to(portal->data.transform.origin);
  3322. if (disable_distance < distance) {
  3323. return;
  3324. }
  3325. }
  3326. /* TEST PORTAL NOT FACING OPTIMIZATION */
  3327. if (p_portal->portal_info->connected) {
  3328. //connected portal means, it must face against the camera to be seen
  3329. if (p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3330. return;
  3331. }
  3332. } else {
  3333. //disconencted portals (go from room to parent room or exterior) must face towards the canera
  3334. if (!p_portal->portal_info->plane_cache.is_point_over(p_camera->transform.origin)) { //portal facing against camera (exterior)
  3335. return;
  3336. }
  3337. }
  3338. if (p_from_portal && !_test_portal_cull(p_camera, p_from_portal, portal)) {
  3339. return; // portal not visible (culled)
  3340. }
  3341. portal->portal_info->last_visited_pass=render_pass;
  3342. if (portal->portal_info->connected) {
  3343. //interior<->interior portal
  3344. Instance *to_room = portal->portal_info->connected->room;
  3345. if (!to_room) {
  3346. return; //wtf.. oh well, connected to a roomless (invalid) portal
  3347. }
  3348. _cull_room(p_camera, to_room, portal->portal_info->connected);
  3349. } else {
  3350. //to exterior/to parent roomportal
  3351. Instance *parent_room = portal->room->room;
  3352. _cull_room(p_camera, parent_room, portal);
  3353. }
  3354. }
  3355. void VisualServerRaster::_cull_room(Camera *p_camera, Instance *p_room,Instance *p_from_portal) {
  3356. if (p_room==NULL) {
  3357. //exterior
  3358. exterior_visited=true;
  3359. for(int i=0;i<exterior_portal_cull_count;i++) {
  3360. _cull_portal(p_camera, exterior_portal_cull_result[i],p_from_portal);
  3361. }
  3362. } else {
  3363. ERR_FAIL_COND(!p_room->scenario);
  3364. if (p_room->last_render_pass!=render_pass)
  3365. return; //this room is invalid
  3366. //interior
  3367. //first of all, validate the room
  3368. p_room->room_info->last_visited_pass=render_pass;
  3369. //see about going around portals
  3370. if (!p_room->room_info->room->occlude_exterior)
  3371. exterior_visited=true;
  3372. for(List<Instance*>::Element * E=p_room->room_info->owned_portal_instances.front();E;E=E->next()) {
  3373. _cull_portal(p_camera, E->get(),p_from_portal);
  3374. }
  3375. for(Set<Instance*>::Element * E=p_room->room_info->disconnected_child_portals.front();E;E=E->next()) {
  3376. _cull_portal(p_camera, E->get(),p_from_portal);
  3377. }
  3378. }
  3379. }
  3380. void VisualServerRaster::_render_camera(Viewport *p_viewport,Camera *p_camera, Scenario *p_scenario) {
  3381. uint64_t t = OS::get_singleton()->get_ticks_usec();
  3382. render_pass++;
  3383. uint32_t camera_layer_mask=p_camera->visible_layers;
  3384. /* STEP 1 - SETUP CAMERA */
  3385. CameraMatrix camera_matrix;
  3386. switch(p_camera->type) {
  3387. case Camera::ORTHOGONAL: {
  3388. camera_matrix.set_orthogonal(
  3389. p_camera->size,
  3390. viewport_rect.width / (float)viewport_rect.height,
  3391. p_camera->znear,
  3392. p_camera->zfar,
  3393. p_camera->vaspect
  3394. );
  3395. } break;
  3396. case Camera::PERSPECTIVE: {
  3397. camera_matrix.set_perspective(
  3398. p_camera->fov,
  3399. viewport_rect.width / (float)viewport_rect.height,
  3400. p_camera->znear,
  3401. p_camera->zfar,
  3402. p_camera->vaspect
  3403. );
  3404. } break;
  3405. }
  3406. rasterizer->set_camera(p_camera->transform, camera_matrix);
  3407. Vector<Plane> planes = camera_matrix.get_projection_planes(p_camera->transform);
  3408. CullRange cull_range; // cull range is used for PSSM, and having an idea of the rendering depth
  3409. cull_range.nearp=Plane(p_camera->transform.origin,-p_camera->transform.basis.get_axis(2).normalized());
  3410. cull_range.z_near=camera_matrix.get_z_near();
  3411. cull_range.z_far=camera_matrix.get_z_far();
  3412. cull_range.min=cull_range.z_far;
  3413. cull_range.max=cull_range.z_near;
  3414. /* STEP 2 - CULL */
  3415. int cull_count = p_scenario->octree.cull_convex(planes,instance_cull_result,MAX_INSTANCE_CULL);
  3416. light_cull_count=0;
  3417. /* print_line("OT: "+rtos( (OS::get_singleton()->get_ticks_usec()-t)/1000.0));
  3418. print_line("OTO: "+itos(p_scenario->octree.get_octant_count()));
  3419. // print_line("OTE: "+itos(p_scenario->octree.get_elem_count()));
  3420. print_line("OTP: "+itos(p_scenario->octree.get_pair_count()));
  3421. */
  3422. /* STEP 3 - PROCESS PORTALS, VALIDATE ROOMS */
  3423. // compute portals
  3424. exterior_visited=false;
  3425. exterior_portal_cull_count=0;
  3426. if (room_cull_enabled) {
  3427. for(int i=0;i<cull_count;i++) {
  3428. Instance *ins = instance_cull_result[i];
  3429. ins->last_render_pass=render_pass;
  3430. if (ins->base_type!=INSTANCE_PORTAL)
  3431. continue;
  3432. if (ins->room)
  3433. continue;
  3434. ERR_CONTINUE(exterior_portal_cull_count>=MAX_EXTERIOR_PORTALS);
  3435. exterior_portal_cull_result[exterior_portal_cull_count++]=ins;
  3436. }
  3437. 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));
  3438. Set<Instance*> current_rooms;
  3439. Set<Instance*> portal_rooms;
  3440. //add to set
  3441. for(int i=0;i<room_cull_count;i++) {
  3442. if (room_cull_result[i]->base_type==INSTANCE_ROOM) {
  3443. current_rooms.insert(room_cull_result[i]);
  3444. }
  3445. if (room_cull_result[i]->base_type==INSTANCE_PORTAL) {
  3446. //assume inside that room if also inside the portal..
  3447. if (room_cull_result[i]->room) {
  3448. portal_rooms.insert(room_cull_result[i]->room);
  3449. }
  3450. SWAP(room_cull_result[i],room_cull_result[room_cull_count-1]);
  3451. room_cull_count--;
  3452. i--;
  3453. }
  3454. }
  3455. //remove from set if it has a parent room or BSP doesn't contain
  3456. for(int i=0;i<room_cull_count;i++) {
  3457. Instance *r = room_cull_result[i];
  3458. //check inside BSP
  3459. Vector3 room_local_point = r->room_info->affine_inverse.xform( p_camera->transform.origin );
  3460. if (!portal_rooms.has(r) && !r->room_info->room->bounds.point_is_inside(room_local_point)) {
  3461. current_rooms.erase(r);
  3462. continue;
  3463. }
  3464. //check parent
  3465. while (r->room) {// has parent room
  3466. current_rooms.erase(r);
  3467. r=r->room;
  3468. }
  3469. }
  3470. if (current_rooms.size()) {
  3471. //camera is inside a room
  3472. // go through rooms
  3473. for(Set<Instance*>::Element *E=current_rooms.front();E;E=E->next()) {
  3474. _cull_room(p_camera,E->get());
  3475. }
  3476. } else {
  3477. //start from exterior
  3478. _cull_room(p_camera,NULL);
  3479. }
  3480. }
  3481. /* STEP 4 - REMOVE FURTHER CULLED OBJECTS, ADD LIGHTS */
  3482. for(int i=0;i<cull_count;i++) {
  3483. Instance *ins = instance_cull_result[i];
  3484. bool keep=false;
  3485. if ((camera_layer_mask&ins->layer_mask)==0) {
  3486. //failure
  3487. } else if (ins->base_type==INSTANCE_LIGHT) {
  3488. if (light_cull_count<MAX_LIGHTS_CULLED) {
  3489. light_cull_result[light_cull_count++]=ins;
  3490. // rasterizer->light_instance_set_active_hint(ins->light_info->instance);
  3491. {
  3492. //compute distance to camera using aabb support
  3493. Vector3 n = ins->data.transform.basis.xform_inv(cull_range.nearp.normal).normalized();
  3494. Vector3 s = ins->data.transform.xform(ins->aabb.get_support(n));
  3495. ins->light_info->dtc=cull_range.nearp.distance_to(s);
  3496. }
  3497. }
  3498. } else if ((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK && ins->visible) {
  3499. bool discarded=false;
  3500. if (ins->draw_range_end>0) {
  3501. float d = cull_range.nearp.distance_to(ins->data.transform.origin);
  3502. if (d<0)
  3503. d=0;
  3504. discarded=(d<ins->draw_range_begin || d>=ins->draw_range_end);
  3505. }
  3506. if (!discarded) {
  3507. // test if this geometry should be visible
  3508. if (room_cull_enabled) {
  3509. if (ins->visible_in_all_rooms) {
  3510. keep=true;
  3511. } else if (ins->room) {
  3512. if (ins->room->room_info->last_visited_pass==render_pass)
  3513. keep=true;
  3514. } else if (ins->auto_rooms.size()) {
  3515. for(Set<Instance*>::Element *E=ins->auto_rooms.front();E;E=E->next()) {
  3516. if (E->get()->room_info->last_visited_pass==render_pass) {
  3517. keep=true;
  3518. break;
  3519. }
  3520. }
  3521. } else if(exterior_visited)
  3522. keep=true;
  3523. } else {
  3524. keep=true;
  3525. }
  3526. }
  3527. if (keep) {
  3528. // update cull range
  3529. float min,max;
  3530. ins->transformed_aabb.project_range_in_plane(cull_range.nearp,min,max);
  3531. if (min<cull_range.min)
  3532. cull_range.min=min;
  3533. if (max>cull_range.max)
  3534. cull_range.max=max;
  3535. }
  3536. }
  3537. if (!keep) {
  3538. // remove, no reason to keep
  3539. cull_count--;
  3540. SWAP( instance_cull_result[i], instance_cull_result[ cull_count ] );
  3541. i--;
  3542. ins->last_render_pass=0; // make invalid
  3543. } else {
  3544. ins->last_render_pass=render_pass;
  3545. }
  3546. }
  3547. if (cull_range.max > cull_range.z_far )
  3548. cull_range.max=cull_range.z_far;
  3549. if (cull_range.min < cull_range.z_near )
  3550. cull_range.min=cull_range.z_near;
  3551. /* STEP 5 - PROCESS LIGHTS */
  3552. rasterizer->shadow_clear_near(); //clear near shadows, will be recreated
  3553. // directional lights
  3554. {
  3555. List<RID>::Element *E=p_scenario->directional_lights.front();
  3556. while(E) {
  3557. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  3558. if (light && light->light_info->enabled && rasterizer->light_has_shadow(light->base_rid)) {
  3559. //rasterizer->light_instance_set_active_hint(light->light_info->instance);
  3560. _light_instance_update_shadow(light,p_scenario,p_camera,cull_range);
  3561. }
  3562. E=E->next();
  3563. }
  3564. }
  3565. //discard lights not affecting anything (useful for deferred rendering, shadowmaps, etc)
  3566. for (int i=0;i<light_cull_count;i++) {
  3567. Instance *ins = light_cull_result[i];
  3568. if (light_discard_enabled) {
  3569. //see if the light should be pre discarded because no one is seeing it
  3570. //this test may seem expensive, but in reality, it shouldn't be
  3571. //because of early out condition. It will only go through everything
  3572. //if it's being discarded.
  3573. bool valid=false;
  3574. InstanceSet::Element *E =ins->light_info->affected.front();
  3575. while(E) {
  3576. if (E->get()->last_render_pass==render_pass) {
  3577. valid=true; // early out.
  3578. break;
  3579. }
  3580. E=E->next();
  3581. }
  3582. if (!valid) {
  3583. light_cull_count--;
  3584. SWAP( light_cull_result[i], light_cull_result[ light_cull_count ] );
  3585. i--;
  3586. }
  3587. }
  3588. }
  3589. {
  3590. //assign shadows by distance to camera
  3591. SortArray<Instance*,_InstanceLightsort> sorter;
  3592. sorter.sort(light_cull_result,light_cull_count);
  3593. for (int i=0;i<light_cull_count;i++) {
  3594. Instance *ins = light_cull_result[i];
  3595. if (!rasterizer->light_has_shadow(ins->base_rid) || !shadows_enabled)
  3596. continue;
  3597. /* for far shadows?
  3598. if (ins->version == ins->light_info->last_version && rasterizer->light_instance_has_far_shadow(ins->light_info->instance))
  3599. continue; // didn't change
  3600. */
  3601. _light_instance_update_shadow(ins,p_scenario,p_camera,cull_range);
  3602. ins->light_info->last_version=ins->version;
  3603. }
  3604. }
  3605. /* STEP 6 - PROCESS GEOMETRY AND DRAW SCENE*/
  3606. RID environment;
  3607. if (p_camera->env.is_valid()) //camera has more environment priority
  3608. environment=p_camera->env;
  3609. else
  3610. environment=p_scenario->environment;
  3611. rasterizer->begin_scene(p_viewport->viewport_data,environment,p_scenario->debug);
  3612. rasterizer->set_viewport(viewport_rect);
  3613. // add lights
  3614. {
  3615. List<RID>::Element *E=p_scenario->directional_lights.front();
  3616. for(;E;E=E->next()) {
  3617. Instance *light = E->get().is_valid()?instance_owner.get(E->get()):NULL;
  3618. ERR_CONTINUE(!light);
  3619. if (!light->light_info->enabled)
  3620. continue;
  3621. rasterizer->add_light(light->light_info->instance);
  3622. light->light_info->last_add_pass=render_pass;
  3623. }
  3624. for (int i=0;i<light_cull_count;i++) {
  3625. Instance *ins = light_cull_result[i];
  3626. rasterizer->add_light(ins->light_info->instance);
  3627. ins->light_info->last_add_pass=render_pass;
  3628. }
  3629. }
  3630. // add geometry
  3631. for(int i=0;i<cull_count;i++) {
  3632. Instance *ins = instance_cull_result[i];
  3633. ERR_CONTINUE(!((1<<ins->base_type)&INSTANCE_GEOMETRY_MASK));
  3634. _instance_draw(ins);
  3635. }
  3636. rasterizer->end_scene();
  3637. }
  3638. void VisualServerRaster::_render_canvas_item(CanvasItem *p_canvas_item,const Matrix32& p_transform,const Rect2& p_clip_rect, float p_opacity) {
  3639. CanvasItem *ci = p_canvas_item;
  3640. if (!ci->visible)
  3641. return;
  3642. if (p_opacity<0.007)
  3643. return;
  3644. Rect2 rect = ci->get_rect();
  3645. Matrix32 xform = p_transform * ci->xform;
  3646. Rect2 global_rect = xform.xform(rect);
  3647. global_rect.pos+=p_clip_rect.pos;
  3648. if (global_rect.intersects(p_clip_rect) && ci->viewport.is_valid() && viewport_owner.owns(ci->viewport)) {
  3649. Viewport *vp = viewport_owner.get(ci->viewport);
  3650. Point2i from = xform.get_origin() + Point2(viewport_rect.x,viewport_rect.y);
  3651. Point2i size = rect.size;
  3652. size.x *= xform[0].length();
  3653. size.y *= xform[1].length();
  3654. _draw_viewport(vp,
  3655. from.x,
  3656. from.y,
  3657. size.x,
  3658. size.y);
  3659. rasterizer->canvas_begin();
  3660. }
  3661. int s = ci->commands.size();
  3662. bool reclip=false;
  3663. float opacity = ci->opacity * p_opacity;
  3664. #ifndef ONTOP_DISABLED
  3665. CanvasItem **child_items = ci->child_items.ptr();
  3666. int child_item_count=ci->child_items.size();
  3667. int top_item_count=0;
  3668. CanvasItem **top_items=(CanvasItem**)alloca(child_item_count*sizeof(CanvasItem*));
  3669. if (ci->clip) {
  3670. rasterizer->canvas_set_clip(true,global_rect);
  3671. canvas_clip=global_rect;
  3672. }
  3673. for(int i=0;i<child_item_count;i++) {
  3674. if (child_items[i]->ontop)
  3675. top_items[top_item_count++]=child_items[i];
  3676. else {
  3677. _render_canvas_item(child_items[i],xform,p_clip_rect,opacity);
  3678. }
  3679. }
  3680. #endif
  3681. if (s!=0) {
  3682. //Rect2 rect( ci->rect.pos + p_ofs, ci->rect.size);
  3683. if (p_clip_rect.intersects(global_rect)) {
  3684. rasterizer->canvas_begin_rect(xform);
  3685. rasterizer->canvas_set_opacity( opacity * ci->self_opacity );
  3686. rasterizer->canvas_set_blend_mode( ci->blend_mode );
  3687. CanvasItem::Command **commands = &ci->commands[0];
  3688. for (int i=0;i<s;i++) {
  3689. CanvasItem::Command *c=commands[i];
  3690. switch(c->type) {
  3691. case CanvasItem::Command::TYPE_LINE: {
  3692. CanvasItem::CommandLine* line = static_cast<CanvasItem::CommandLine*>(c);
  3693. rasterizer->canvas_draw_line(line->from,line->to,line->color,line->width);
  3694. } break;
  3695. case CanvasItem::Command::TYPE_RECT: {
  3696. CanvasItem::CommandRect* rect = static_cast<CanvasItem::CommandRect*>(c);
  3697. // rasterizer->canvas_draw_rect(rect->rect,rect->region,rect->source,rect->flags&CanvasItem::CommandRect::FLAG_TILE,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H,rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V,rect->texture,rect->modulate);
  3698. #if 0
  3699. int flags=0;
  3700. if (rect->flags&CanvasItem::CommandRect::FLAG_REGION) {
  3701. flags|=Rasterizer::CANVAS_RECT_REGION;
  3702. }
  3703. if (rect->flags&CanvasItem::CommandRect::FLAG_TILE) {
  3704. flags|=Rasterizer::CANVAS_RECT_TILE;
  3705. }
  3706. if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_H) {
  3707. flags|=Rasterizer::CANVAS_RECT_FLIP_H;
  3708. }
  3709. if (rect->flags&CanvasItem::CommandRect::FLAG_FLIP_V) {
  3710. flags|=Rasterizer::CANVAS_RECT_FLIP_V;
  3711. }
  3712. #else
  3713. int flags=rect->flags;
  3714. #endif
  3715. rasterizer->canvas_draw_rect(rect->rect,flags,rect->source,rect->texture,rect->modulate);
  3716. } break;
  3717. case CanvasItem::Command::TYPE_STYLE: {
  3718. CanvasItem::CommandStyle* style = static_cast<CanvasItem::CommandStyle*>(c);
  3719. rasterizer->canvas_draw_style_box(style->rect,style->texture,style->margin,style->draw_center,style->color);
  3720. } break;
  3721. case CanvasItem::Command::TYPE_PRIMITIVE: {
  3722. CanvasItem::CommandPrimitive* primitive = static_cast<CanvasItem::CommandPrimitive*>(c);
  3723. rasterizer->canvas_draw_primitive(primitive->points,primitive->colors,primitive->uvs,primitive->texture,primitive->width);
  3724. } break;
  3725. case CanvasItem::Command::TYPE_POLYGON: {
  3726. CanvasItem::CommandPolygon* polygon = static_cast<CanvasItem::CommandPolygon*>(c);
  3727. rasterizer->canvas_draw_polygon(polygon->count,polygon->indices.ptr(),polygon->points.ptr(),polygon->uvs.ptr(),polygon->colors.ptr(),polygon->texture,polygon->colors.size()==1);
  3728. } break;
  3729. case CanvasItem::Command::TYPE_POLYGON_PTR: {
  3730. CanvasItem::CommandPolygonPtr* polygon = static_cast<CanvasItem::CommandPolygonPtr*>(c);
  3731. rasterizer->canvas_draw_polygon(polygon->count,polygon->indices,polygon->points,polygon->uvs,polygon->colors,polygon->texture,false);
  3732. } break;
  3733. case CanvasItem::Command::TYPE_CIRCLE: {
  3734. CanvasItem::CommandCircle* circle = static_cast<CanvasItem::CommandCircle*>(c);
  3735. static const int numpoints=32;
  3736. Vector2 points[numpoints+1];
  3737. points[numpoints]=circle->pos;
  3738. int indices[numpoints*3];
  3739. for(int i=0;i<numpoints;i++) {
  3740. points[i]=circle->pos+Vector2( Math::sin(i*Math_PI*2.0/numpoints),Math::cos(i*Math_PI*2.0/numpoints) )*circle->radius;
  3741. indices[i*3+0]=i;
  3742. indices[i*3+1]=(i+1)%numpoints;
  3743. indices[i*3+2]=numpoints;
  3744. }
  3745. rasterizer->canvas_draw_polygon(numpoints*3,indices,points,NULL,&circle->color,RID(),true);
  3746. //rasterizer->canvas_draw_circle(circle->indices.size(),circle->indices.ptr(),circle->points.ptr(),circle->uvs.ptr(),circle->colors.ptr(),circle->texture,circle->colors.size()==1);
  3747. } break;
  3748. case CanvasItem::Command::TYPE_TRANSFORM: {
  3749. CanvasItem::CommandTransform* transform = static_cast<CanvasItem::CommandTransform*>(c);
  3750. rasterizer->canvas_set_transform(transform->xform);
  3751. } break;
  3752. case CanvasItem::Command::TYPE_BLEND_MODE: {
  3753. CanvasItem::CommandBlendMode* bm = static_cast<CanvasItem::CommandBlendMode*>(c);
  3754. rasterizer->canvas_set_blend_mode(bm->blend_mode);
  3755. } break;
  3756. case CanvasItem::Command::TYPE_CLIP_IGNORE: {
  3757. CanvasItem::CommandClipIgnore* ci = static_cast<CanvasItem::CommandClipIgnore*>(c);
  3758. if (canvas_clip!=Rect2()) {
  3759. if (ci->ignore!=reclip) {
  3760. if (ci->ignore) {
  3761. rasterizer->canvas_set_clip(false,Rect2());
  3762. reclip=true;
  3763. } else {
  3764. rasterizer->canvas_set_clip(true,canvas_clip);
  3765. reclip=false;
  3766. }
  3767. }
  3768. }
  3769. } break;
  3770. }
  3771. }
  3772. rasterizer->canvas_end_rect();
  3773. }
  3774. }
  3775. if (reclip) {
  3776. rasterizer->canvas_set_clip(true,canvas_clip);
  3777. }
  3778. #ifndef ONTOP_DISABLED
  3779. for(int i=0;i<top_item_count;i++) {
  3780. _render_canvas_item(top_items[i],xform,p_clip_rect,opacity);
  3781. }
  3782. #else
  3783. for(int i=0;i<p_canvas_item->child_items.size();i++) {
  3784. _render_canvas_item(p_canvas_item->child_items[i],xform,p_clip_rect,opacity);
  3785. }
  3786. #endif
  3787. if (ci->clip) {
  3788. rasterizer->canvas_set_clip(false,Rect2());
  3789. canvas_clip=Rect2();
  3790. }
  3791. }
  3792. void VisualServerRaster::_render_canvas(Canvas *p_canvas,const Matrix32 &p_transform) {
  3793. rasterizer->canvas_begin();
  3794. int l = p_canvas->child_items.size();
  3795. for(int i=0;i<l;i++) {
  3796. Canvas::ChildItem& ci=p_canvas->child_items[i];
  3797. _render_canvas_item(ci.item,p_transform,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3798. //mirroring (useful for scrolling backgrounds)
  3799. if (ci.mirror.x!=0) {
  3800. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(ci.mirror.x,0));
  3801. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3802. }
  3803. if (ci.mirror.y!=0) {
  3804. Matrix32 xform2 = p_transform * Matrix32(0,Vector2(0,ci.mirror.y));
  3805. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3806. }
  3807. if (ci.mirror.y!=0 && ci.mirror.x!=0) {
  3808. Matrix32 xform2 = p_transform * Matrix32(0,ci.mirror);
  3809. _render_canvas_item(ci.item,xform2,Rect2(viewport_rect.x,viewport_rect.y,viewport_rect.width,viewport_rect.height),1);
  3810. }
  3811. }
  3812. }
  3813. void VisualServerRaster::_draw_viewport(Viewport *p_viewport,int p_ofs_x, int p_ofs_y,int p_parent_w,int p_parent_h) {
  3814. ViewportRect desired_rect=p_viewport->rect;
  3815. ViewportRect old_rect = viewport_rect;
  3816. // bool vpchanged=false;
  3817. // convert default expanding viewports to actual size
  3818. //if (desired_rect.x==0 && desired_rect.y==0 && desired_rect.width==0 && desired_rect.height==0) {
  3819. if (p_parent_w != 0 && p_parent_h != 0) {
  3820. desired_rect.width=p_parent_w;
  3821. desired_rect.height=p_parent_h;
  3822. }
  3823. ERR_FAIL_COND(desired_rect.width<=0 || desired_rect.height<=0);
  3824. desired_rect.x+=p_ofs_x;
  3825. desired_rect.y+=p_ofs_y;
  3826. // if the viewport is different than the actual one, change it
  3827. if ( p_viewport->render_target.is_valid() || viewport_rect.x != desired_rect.x ||
  3828. viewport_rect.y != desired_rect.y ||
  3829. viewport_rect.width != desired_rect.width ||
  3830. viewport_rect.height != desired_rect.height ) {
  3831. viewport_rect=desired_rect;
  3832. rasterizer->set_viewport(viewport_rect);
  3833. }
  3834. /* Camera should always be BEFORE any other 3D */
  3835. if (!p_viewport->hide_scenario && camera_owner.owns(p_viewport->camera) && scenario_owner.owns(p_viewport->scenario)) {
  3836. Camera *camera = camera_owner.get( p_viewport->camera );
  3837. Scenario *scenario = scenario_owner.get( p_viewport->scenario );
  3838. _update_instances(); // check dirty instances before rendering
  3839. _render_camera(p_viewport, camera,scenario );
  3840. } else if (true /*|| !p_viewport->canvas_list.empty()*/){
  3841. //clear the viewport black because of no camera? i seriously should..
  3842. rasterizer->clear_viewport(clear_color);
  3843. }
  3844. if (!p_viewport->hide_canvas) {
  3845. int i=0;
  3846. Map<Viewport::CanvasKey,Viewport::CanvasData*> canvas_map;
  3847. for (Map<RID,Viewport::CanvasData>::Element *E=p_viewport->canvas_map.front();E;E=E->next()) {
  3848. canvas_map[ Viewport::CanvasKey( E->key(), E->get().layer) ]=&E->get();
  3849. }
  3850. for (Map<Viewport::CanvasKey,Viewport::CanvasData*>::Element *E=canvas_map.front();E;E=E->next()) {
  3851. // print_line("canvas "+itos(i)+" size: "+itos(I->get()->canvas->child_items.size()));
  3852. //print_line("GT "+p_viewport->global_transform+". CT: "+E->get()->transform);
  3853. Matrix32 xform = p_viewport->global_transform * E->get()->transform;
  3854. _render_canvas( E->get()->canvas,xform );
  3855. i++;
  3856. }
  3857. }
  3858. //capture
  3859. if (p_viewport->queue_capture) {
  3860. rasterizer->capture_viewport(&p_viewport->capture);
  3861. }
  3862. //restore
  3863. if ( viewport_rect.x != old_rect.x ||
  3864. viewport_rect.y != old_rect.y ||
  3865. viewport_rect.width != old_rect.width ||
  3866. viewport_rect.height != old_rect.height ) {
  3867. viewport_rect=old_rect;
  3868. rasterizer->set_viewport(viewport_rect);
  3869. }
  3870. }
  3871. void VisualServerRaster::_draw_viewports() {
  3872. //draw viewports for render targets
  3873. List<Viewport*> to_blit;
  3874. List<Viewport*> to_disable;
  3875. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  3876. Viewport *vp = E->self();
  3877. ERR_CONTINUE(!vp);
  3878. if (
  3879. vp->render_target_update_mode==RENDER_TARGET_UPDATE_WHEN_VISIBLE &&
  3880. !vp->rendered_in_prev_frame &&
  3881. !vp->queue_capture
  3882. ) {
  3883. continue;
  3884. }
  3885. if (vp->rt_to_screen_rect!=Rect2())
  3886. to_blit.push_back(vp);
  3887. rasterizer->set_render_target(vp->render_target,vp->transparent_bg,vp->render_target_vflip);
  3888. _draw_viewport(vp,0,0,vp->rect.width,vp->rect.height);
  3889. if ( (vp->queue_capture && vp->render_target_update_mode==RENDER_TARGET_UPDATE_DISABLED) || vp->render_target_update_mode==RENDER_TARGET_UPDATE_ONCE) {
  3890. //was only enabled for capture
  3891. to_disable.push_back(vp);
  3892. vp->render_target_update_mode=RENDER_TARGET_UPDATE_DISABLED;
  3893. }
  3894. }
  3895. rasterizer->set_render_target(RID());
  3896. while(to_disable.size()) {
  3897. //disable again because it was only for capture
  3898. viewport_update_list.remove(&to_disable.front()->get()->update_list);
  3899. to_disable.pop_front();
  3900. }
  3901. //draw RTs directly to screen when requested
  3902. for (List<Viewport*>::Element *E=to_blit.front();E;E=E->next()) {
  3903. int window_w = OS::get_singleton()->get_video_mode().width;
  3904. int window_h = OS::get_singleton()->get_video_mode().height;
  3905. ViewportRect desired_rect;
  3906. desired_rect.x = desired_rect.y = 0;
  3907. desired_rect.width = window_w;
  3908. desired_rect.height = window_h;
  3909. if ( viewport_rect.x != desired_rect.x ||
  3910. viewport_rect.y != desired_rect.y ||
  3911. viewport_rect.width != desired_rect.width ||
  3912. viewport_rect.height != desired_rect.height ) {
  3913. viewport_rect=desired_rect;
  3914. rasterizer->set_viewport(viewport_rect);
  3915. }
  3916. rasterizer->canvas_begin();
  3917. rasterizer->canvas_disable_blending();
  3918. rasterizer->canvas_begin_rect(Matrix32());
  3919. 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));
  3920. }
  3921. //draw viewports attached to screen
  3922. for(Map<RID,int>::Element *E=screen_viewports.front();E;E=E->next()) {
  3923. Viewport *vp = viewport_owner.get(E->key());
  3924. ERR_CONTINUE(!vp);
  3925. int window_w = OS::get_singleton()->get_video_mode(E->get()).width;
  3926. int window_h = OS::get_singleton()->get_video_mode(E->get()).height;
  3927. _draw_viewport(vp,0,0,window_w,window_h);
  3928. }
  3929. //check when a viewport associated to a render target was drawn
  3930. for(SelfList<Viewport> *E=viewport_update_list.first();E;E=E->next()) {
  3931. Viewport *vp = E->self();
  3932. ERR_CONTINUE(!vp);
  3933. if (vp->render_target_update_mode!=RENDER_TARGET_UPDATE_WHEN_VISIBLE)
  3934. continue;
  3935. vp->rendered_in_prev_frame=rasterizer->render_target_renedered_in_frame(vp->render_target);
  3936. }
  3937. }
  3938. void VisualServerRaster::_draw_cursors_and_margins() {
  3939. int window_w = OS::get_singleton()->get_video_mode().width;
  3940. int window_h = OS::get_singleton()->get_video_mode().height;
  3941. ViewportRect desired_rect;
  3942. desired_rect.x = desired_rect.y = 0;
  3943. desired_rect.width = window_w;
  3944. desired_rect.height = window_h;
  3945. if ( viewport_rect.x != desired_rect.x ||
  3946. viewport_rect.y != desired_rect.y ||
  3947. viewport_rect.width != desired_rect.width ||
  3948. viewport_rect.height != desired_rect.height ) {
  3949. viewport_rect=desired_rect;
  3950. rasterizer->set_viewport(viewport_rect);
  3951. }
  3952. rasterizer->canvas_begin();
  3953. rasterizer->canvas_begin_rect(Matrix32());
  3954. for (int i=0; i<MAX_CURSORS; i++) {
  3955. if (!cursors[i].visible) {
  3956. continue;
  3957. };
  3958. RID tex = cursors[i].texture?cursors[i].texture:default_cursor_texture;
  3959. ERR_CONTINUE( !tex );
  3960. Point2 size(texture_get_width(tex), texture_get_height(tex));
  3961. rasterizer->canvas_draw_rect(Rect2(cursors[i].pos, size), 0, Rect2(), tex, Color(1, 1, 1, 1));
  3962. };
  3963. if (black_image[MARGIN_LEFT].is_valid()) {
  3964. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_LEFT]),rasterizer->texture_get_height(black_image[MARGIN_LEFT]));
  3965. 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));
  3966. } else if (black_margin[MARGIN_LEFT])
  3967. rasterizer->canvas_draw_rect(Rect2(0,0,black_margin[MARGIN_LEFT],window_h),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3968. if (black_image[MARGIN_RIGHT].is_valid()) {
  3969. Size2 sz(rasterizer->texture_get_width(black_image[MARGIN_RIGHT]),rasterizer->texture_get_height(black_image[MARGIN_RIGHT]));
  3970. 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));
  3971. } else if (black_margin[MARGIN_RIGHT])
  3972. 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));
  3973. if (black_margin[MARGIN_TOP])
  3974. rasterizer->canvas_draw_rect(Rect2(0,0,window_w,black_margin[MARGIN_TOP]),0,Rect2(0,0,1,1),RID(),Color(0,0,0));
  3975. if (black_margin[MARGIN_BOTTOM])
  3976. 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));
  3977. rasterizer->canvas_end_rect();
  3978. };
  3979. void VisualServerRaster::flush() {
  3980. //do none
  3981. }
  3982. void VisualServerRaster::draw() {
  3983. //if (changes)
  3984. // print_line("changes: "+itos(changes));
  3985. changes=0;
  3986. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  3987. room_cull_enabled = GLOBAL_DEF("render/room_cull_enabled",true);
  3988. light_discard_enabled = GLOBAL_DEF("render/light_discard_enabled",true);
  3989. rasterizer->begin_frame();
  3990. _draw_viewports();
  3991. _draw_cursors_and_margins();
  3992. rasterizer->end_frame();
  3993. draw_extra_frame=rasterizer->needs_to_draw_next_frame();
  3994. }
  3995. bool VisualServerRaster::has_changed() const {
  3996. return changes>0 || draw_extra_frame;
  3997. }
  3998. int VisualServerRaster::get_render_info(RenderInfo p_info) {
  3999. return rasterizer->get_render_info(p_info);
  4000. }
  4001. bool VisualServerRaster::has_feature(Features p_feature) const {
  4002. return rasterizer->has_feature(p_feature); // lies for now
  4003. }
  4004. void VisualServerRaster::set_default_clear_color(const Color& p_color) {
  4005. clear_color=p_color;
  4006. }
  4007. void VisualServerRaster::set_boot_image(const Image& p_image, const Color& p_color) {
  4008. if (p_image.empty())
  4009. return;
  4010. rasterizer->begin_frame();
  4011. int window_w = OS::get_singleton()->get_video_mode(0).width;
  4012. int window_h = OS::get_singleton()->get_video_mode(0).height;
  4013. ViewportRect vr;
  4014. vr.x=0;
  4015. vr.y=0;
  4016. vr.width=OS::get_singleton()->get_video_mode(0).width;
  4017. vr.height=OS::get_singleton()->get_video_mode(0).height;
  4018. rasterizer->set_viewport(vr);
  4019. rasterizer->clear_viewport(p_color);
  4020. rasterizer->canvas_begin();
  4021. RID texture = texture_create();
  4022. texture_allocate(texture,p_image.get_width(),p_image.get_height(),p_image.get_format(),TEXTURE_FLAG_FILTER);
  4023. texture_set_data(texture,p_image);
  4024. rasterizer->canvas_begin_rect(Matrix32());
  4025. Rect2 imgrect(0,0,p_image.get_width(),p_image.get_height());
  4026. Rect2 screenrect=imgrect;
  4027. screenrect.pos+=((Size2(vr.width,vr.height)-screenrect.size)/2.0).floor();
  4028. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,0));
  4029. rasterizer->canvas_draw_rect(screenrect,0,imgrect,texture,Color(1,1,1,1));
  4030. rasterizer->canvas_end_rect();
  4031. rasterizer->end_frame();
  4032. rasterizer->flush_frame();
  4033. free(texture); // free since it's only one frame that stays there
  4034. }
  4035. void VisualServerRaster::init() {
  4036. rasterizer->init();
  4037. shadows_enabled=GLOBAL_DEF("render/shadows_enabled",true);
  4038. //default_scenario = scenario_create();
  4039. //default_viewport = viewport_create();
  4040. for(int i=0;i<4;i++)
  4041. black_margin[i]=0;
  4042. Image img;
  4043. img.create(default_mouse_cursor_xpm);
  4044. //img.convert(Image::FORMAT_RGB);
  4045. default_cursor_texture = texture_create_from_image(img, 0);
  4046. aabb_random_points.resize( GLOBAL_DEF("render/aabb_random_points",16) );
  4047. for(int i=0;i<aabb_random_points.size();i++)
  4048. aabb_random_points[i]=Vector3(Math::random(0,1),Math::random(0,1),Math::random(0,1));
  4049. transformed_aabb_random_points.resize(aabb_random_points.size());
  4050. changes=0;
  4051. }
  4052. void VisualServerRaster::_clean_up_owner(RID_OwnerBase *p_owner,String p_type) {
  4053. List<RID> rids;
  4054. p_owner->get_owned_list(&rids);
  4055. int lost=0;
  4056. for(List<RID>::Element *I=rids.front();I;I=I->next()) {
  4057. if (OS::get_singleton()->is_stdout_verbose()) {
  4058. lost++;
  4059. }
  4060. free(I->get());
  4061. }
  4062. if (lost)
  4063. print_line("VisualServerRaster: WARNING: Lost "+itos(lost)+" RIDs of type "+p_type);
  4064. }
  4065. void VisualServerRaster::finish() {
  4066. free(default_cursor_texture);
  4067. _clean_up_owner( &room_owner,"Room" );
  4068. _clean_up_owner( &portal_owner,"Portal" );
  4069. _clean_up_owner( &camera_owner,"Camera" );
  4070. _clean_up_owner( &viewport_owner,"Viewport" );
  4071. _clean_up_owner( &scenario_owner,"Scenario" );
  4072. _clean_up_owner( &instance_owner,"Instance" );
  4073. _clean_up_owner( &canvas_owner,"Canvas" );
  4074. _clean_up_owner( &canvas_item_owner,"CanvasItem" );
  4075. rasterizer->finish();
  4076. octree_allocator.clear();
  4077. if (instance_dependency_map.size()) {
  4078. print_line("base resources missing "+itos(instance_dependency_map.size()));
  4079. }
  4080. ERR_FAIL_COND( instance_dependency_map.size() );
  4081. }
  4082. RID VisualServerRaster::get_test_cube() {
  4083. if (test_cube.is_valid())
  4084. return test_cube;
  4085. test_cube=_make_test_cube();
  4086. return test_cube;
  4087. }
  4088. VisualServerRaster::VisualServerRaster(Rasterizer *p_rasterizer) {
  4089. rasterizer=p_rasterizer;
  4090. instance_update_list=NULL;
  4091. render_pass=0;
  4092. clear_color=Color(0.3,0.3,0.3,1.0);
  4093. OctreeAllocator::allocator=&octree_allocator;
  4094. draw_extra_frame=false;
  4095. }
  4096. VisualServerRaster::~VisualServerRaster()
  4097. {
  4098. }