visual_server_raster.cpp 178 KB

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