visual_server_raster.cpp 181 KB

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