WebGLRenderer.js 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author szimek / https://github.com/szimek/
  6. */
  7. THREE.WebGLRenderer = function ( parameters ) {
  8. console.log( 'THREE.WebGLRenderer', THREE.REVISION );
  9. parameters = parameters || {};
  10. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  11. _context = parameters.context !== undefined ? parameters.context : null,
  12. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  13. _alpha = parameters.alpha !== undefined ? parameters.alpha : false,
  14. _depth = parameters.depth !== undefined ? parameters.depth : true,
  15. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  16. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  17. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  18. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  19. _logarithmicDepthBuffer = parameters.logarithmicDepthBuffer !== undefined ? parameters.logarithmicDepthBuffer : false,
  20. _clearColor = new THREE.Color( 0x000000 ),
  21. _clearAlpha = 0;
  22. var lights = [];
  23. var _webglObjects = {};
  24. var _webglObjectsImmediate = [];
  25. var opaqueObjects = [];
  26. var transparentObjects = [];
  27. // public properties
  28. this.domElement = _canvas;
  29. this.context = null;
  30. this.devicePixelRatio = parameters.devicePixelRatio !== undefined
  31. ? parameters.devicePixelRatio
  32. : self.devicePixelRatio !== undefined
  33. ? self.devicePixelRatio
  34. : 1;
  35. // clearing
  36. this.autoClear = true;
  37. this.autoClearColor = true;
  38. this.autoClearDepth = true;
  39. this.autoClearStencil = true;
  40. // scene graph
  41. this.sortObjects = true;
  42. // physically based shading
  43. this.gammaInput = false;
  44. this.gammaOutput = false;
  45. // shadow map
  46. this.shadowMapEnabled = false;
  47. this.shadowMapAutoUpdate = true;
  48. this.shadowMapType = THREE.PCFShadowMap;
  49. this.shadowMapCullFace = THREE.CullFaceFront;
  50. this.shadowMapDebug = false;
  51. this.shadowMapCascade = false;
  52. // morphs
  53. this.maxMorphTargets = 8;
  54. this.maxMorphNormals = 4;
  55. // flags
  56. this.autoScaleCubemaps = true;
  57. // custom render plugins
  58. this.renderPluginsPre = [];
  59. this.renderPluginsPost = [];
  60. // info
  61. this.info = {
  62. memory: {
  63. programs: 0,
  64. geometries: 0,
  65. textures: 0
  66. },
  67. render: {
  68. calls: 0,
  69. vertices: 0,
  70. faces: 0,
  71. points: 0
  72. }
  73. };
  74. // internal properties
  75. var _this = this,
  76. _programs = [],
  77. // internal state cache
  78. _currentProgram = null,
  79. _currentFramebuffer = null,
  80. _currentMaterialId = - 1,
  81. _currentGeometryGroupHash = null,
  82. _currentCamera = null,
  83. _usedTextureUnits = 0,
  84. // GL state cache
  85. _oldDoubleSided = - 1,
  86. _oldFlipSided = - 1,
  87. _oldBlending = - 1,
  88. _oldBlendEquation = - 1,
  89. _oldBlendSrc = - 1,
  90. _oldBlendDst = - 1,
  91. _oldDepthTest = - 1,
  92. _oldDepthWrite = - 1,
  93. _oldPolygonOffset = null,
  94. _oldPolygonOffsetFactor = null,
  95. _oldPolygonOffsetUnits = null,
  96. _oldLineWidth = null,
  97. _viewportX = 0,
  98. _viewportY = 0,
  99. _viewportWidth = _canvas.width,
  100. _viewportHeight = _canvas.height,
  101. _currentWidth = 0,
  102. _currentHeight = 0,
  103. _newAttributes = new Uint8Array( 16 ),
  104. _enabledAttributes = new Uint8Array( 16 ),
  105. // frustum
  106. _frustum = new THREE.Frustum(),
  107. // camera matrices cache
  108. _projScreenMatrix = new THREE.Matrix4(),
  109. _projScreenMatrixPS = new THREE.Matrix4(),
  110. _vector3 = new THREE.Vector3(),
  111. // light arrays cache
  112. _direction = new THREE.Vector3(),
  113. _lightsNeedUpdate = true,
  114. _lights = {
  115. ambient: [ 0, 0, 0 ],
  116. directional: { length: 0, colors:[], positions: [] },
  117. point: { length: 0, colors: [], positions: [], distances: [] },
  118. spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [] },
  119. hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
  120. };
  121. // initialize
  122. var _gl;
  123. var _glExtensionTextureFloat;
  124. var _glExtensionTextureFloatLinear;
  125. var _glExtensionStandardDerivatives;
  126. var _glExtensionTextureFilterAnisotropic;
  127. var _glExtensionCompressedTextureS3TC;
  128. var _glExtensionCompressedTexturePVRTC;
  129. var _glExtensionElementIndexUint;
  130. var _glExtensionFragDepth;
  131. initGL();
  132. setDefaultGLState();
  133. this.context = _gl;
  134. // GPU capabilities
  135. var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
  136. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  137. var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
  138. var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  139. var _maxAnisotropy = _glExtensionTextureFilterAnisotropic ? _gl.getParameter( _glExtensionTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
  140. var _supportsVertexTextures = ( _maxVertexTextures > 0 );
  141. var _supportsBoneTextures = _supportsVertexTextures && _glExtensionTextureFloat;
  142. // COMPRESSED_TEXTURE_FORMATS return IntArray
  143. // but we need Array.indexOf to test formats availabilities
  144. var _compressedTextureFormats = [];
  145. if( _glExtensionCompressedTexturePVRTC || _glExtensionCompressedTextureS3TC ) {
  146. var compressedfmts = _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS );
  147. for( var i = 0; i < compressedfmts.length; i++ ){
  148. _compressedTextureFormats.push( compressedfmts[i] );
  149. }
  150. }
  151. //
  152. var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
  153. var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
  154. var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT );
  155. var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
  156. var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
  157. var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT );
  158. // clamp precision to maximum available
  159. var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
  160. var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
  161. if ( _precision === 'highp' && ! highpAvailable ) {
  162. if ( mediumpAvailable ) {
  163. _precision = 'mediump';
  164. console.warn( 'THREE.WebGLRenderer: highp not supported, using mediump.' );
  165. } else {
  166. _precision = 'lowp';
  167. console.warn( 'THREE.WebGLRenderer: highp and mediump not supported, using lowp.' );
  168. }
  169. }
  170. if ( _precision === 'mediump' && ! mediumpAvailable ) {
  171. _precision = 'lowp';
  172. console.warn( 'THREE.WebGLRenderer: mediump not supported, using lowp.' );
  173. }
  174. // API
  175. this.getContext = function () {
  176. return _gl;
  177. };
  178. this.supportsVertexTextures = function () {
  179. return _supportsVertexTextures;
  180. };
  181. this.supportsFloatTextures = function () {
  182. return _glExtensionTextureFloat;
  183. };
  184. this.supportsStandardDerivatives = function () {
  185. return _glExtensionStandardDerivatives;
  186. };
  187. this.supportsCompressedTextureS3TC = function () {
  188. return _glExtensionCompressedTextureS3TC;
  189. };
  190. this.supportsCompressedTexturePVRTC = function () {
  191. return _glExtensionCompressedTexturePVRTC;
  192. };
  193. this.getMaxAnisotropy = function () {
  194. return _maxAnisotropy;
  195. };
  196. this.getPrecision = function () {
  197. return _precision;
  198. };
  199. this.setSize = function ( width, height, updateStyle ) {
  200. _canvas.width = width * this.devicePixelRatio;
  201. _canvas.height = height * this.devicePixelRatio;
  202. if ( updateStyle !== false ) {
  203. _canvas.style.width = width + 'px';
  204. _canvas.style.height = height + 'px';
  205. }
  206. this.setViewport( 0, 0, width, height );
  207. };
  208. this.setViewport = function ( x, y, width, height ) {
  209. _viewportX = x * this.devicePixelRatio;
  210. _viewportY = y * this.devicePixelRatio;
  211. _viewportWidth = width * this.devicePixelRatio;
  212. _viewportHeight = height * this.devicePixelRatio;
  213. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  214. };
  215. this.setScissor = function ( x, y, width, height ) {
  216. _gl.scissor(
  217. x * this.devicePixelRatio,
  218. y * this.devicePixelRatio,
  219. width * this.devicePixelRatio,
  220. height * this.devicePixelRatio
  221. );
  222. };
  223. this.enableScissorTest = function ( enable ) {
  224. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  225. };
  226. // Clearing
  227. this.setClearColor = function ( color, alpha ) {
  228. _clearColor.set( color );
  229. _clearAlpha = alpha !== undefined ? alpha : 1;
  230. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  231. };
  232. this.setClearColorHex = function ( hex, alpha ) {
  233. console.warn( 'THREE.WebGLRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  234. this.setClearColor( hex, alpha );
  235. };
  236. this.getClearColor = function () {
  237. return _clearColor;
  238. };
  239. this.getClearAlpha = function () {
  240. return _clearAlpha;
  241. };
  242. this.clear = function ( color, depth, stencil ) {
  243. var bits = 0;
  244. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  245. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  246. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  247. _gl.clear( bits );
  248. };
  249. this.clearColor = function () {
  250. _gl.clear( _gl.COLOR_BUFFER_BIT );
  251. };
  252. this.clearDepth = function () {
  253. _gl.clear( _gl.DEPTH_BUFFER_BIT );
  254. };
  255. this.clearStencil = function () {
  256. _gl.clear( _gl.STENCIL_BUFFER_BIT );
  257. };
  258. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  259. this.setRenderTarget( renderTarget );
  260. this.clear( color, depth, stencil );
  261. };
  262. // Plugins
  263. this.addPostPlugin = function ( plugin ) {
  264. plugin.init( this, lights, _webglObjects, _webglObjectsImmediate );
  265. this.renderPluginsPost.push( plugin );
  266. };
  267. this.addPrePlugin = function ( plugin ) {
  268. plugin.init( this, lights, _webglObjects, _webglObjectsImmediate );
  269. this.renderPluginsPre.push( plugin );
  270. };
  271. // Rendering
  272. this.updateShadowMap = function ( scene, camera ) {
  273. _currentProgram = null;
  274. _oldBlending = - 1;
  275. _oldDepthTest = - 1;
  276. _oldDepthWrite = - 1;
  277. _currentGeometryGroupHash = - 1;
  278. _currentMaterialId = - 1;
  279. _lightsNeedUpdate = true;
  280. _oldDoubleSided = - 1;
  281. _oldFlipSided = - 1;
  282. this.shadowMapPlugin.update( scene, camera );
  283. };
  284. // Internal functions
  285. // Buffer allocation
  286. function createParticleBuffers ( geometry ) {
  287. geometry.__webglVertexBuffer = _gl.createBuffer();
  288. geometry.__webglColorBuffer = _gl.createBuffer();
  289. _this.info.memory.geometries ++;
  290. };
  291. function createLineBuffers ( geometry ) {
  292. geometry.__webglVertexBuffer = _gl.createBuffer();
  293. geometry.__webglColorBuffer = _gl.createBuffer();
  294. geometry.__webglLineDistanceBuffer = _gl.createBuffer();
  295. _this.info.memory.geometries ++;
  296. };
  297. function createMeshBuffers ( geometryGroup ) {
  298. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  299. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  300. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  301. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  302. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  303. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  304. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  305. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  306. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  307. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  308. var m, ml;
  309. if ( geometryGroup.numMorphTargets ) {
  310. geometryGroup.__webglMorphTargetsBuffers = [];
  311. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  312. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  313. }
  314. }
  315. if ( geometryGroup.numMorphNormals ) {
  316. geometryGroup.__webglMorphNormalsBuffers = [];
  317. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  318. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  319. }
  320. }
  321. _this.info.memory.geometries ++;
  322. };
  323. // Events
  324. var onObjectRemoved = function ( event ) {
  325. var object = event.target;
  326. object.traverse( function ( child ) {
  327. child.removeEventListener( 'remove', onObjectRemoved );
  328. removeObject( child );
  329. } );
  330. };
  331. var onGeometryDispose = function ( event ) {
  332. var geometry = event.target;
  333. geometry.removeEventListener( 'dispose', onGeometryDispose );
  334. deallocateGeometry( geometry );
  335. };
  336. var onTextureDispose = function ( event ) {
  337. var texture = event.target;
  338. texture.removeEventListener( 'dispose', onTextureDispose );
  339. deallocateTexture( texture );
  340. _this.info.memory.textures --;
  341. };
  342. var onRenderTargetDispose = function ( event ) {
  343. var renderTarget = event.target;
  344. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  345. deallocateRenderTarget( renderTarget );
  346. _this.info.memory.textures --;
  347. };
  348. var onMaterialDispose = function ( event ) {
  349. var material = event.target;
  350. material.removeEventListener( 'dispose', onMaterialDispose );
  351. deallocateMaterial( material );
  352. };
  353. // Buffer deallocation
  354. var deleteBuffers = function ( geometry ) {
  355. if ( geometry.__webglVertexBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglVertexBuffer );
  356. if ( geometry.__webglNormalBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglNormalBuffer );
  357. if ( geometry.__webglTangentBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglTangentBuffer );
  358. if ( geometry.__webglColorBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglColorBuffer );
  359. if ( geometry.__webglUVBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglUVBuffer );
  360. if ( geometry.__webglUV2Buffer !== undefined ) _gl.deleteBuffer( geometry.__webglUV2Buffer );
  361. if ( geometry.__webglSkinIndicesBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinIndicesBuffer );
  362. if ( geometry.__webglSkinWeightsBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinWeightsBuffer );
  363. if ( geometry.__webglFaceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglFaceBuffer );
  364. if ( geometry.__webglLineBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineBuffer );
  365. if ( geometry.__webglLineDistanceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineDistanceBuffer );
  366. // custom attributes
  367. if ( geometry.__webglCustomAttributesList !== undefined ) {
  368. for ( var name in geometry.__webglCustomAttributesList ) {
  369. _gl.deleteBuffer( geometry.__webglCustomAttributesList[ name ].buffer );
  370. }
  371. }
  372. _this.info.memory.geometries --;
  373. };
  374. var deallocateGeometry = function ( geometry ) {
  375. geometry.__webglInit = undefined;
  376. if ( geometry instanceof THREE.BufferGeometry ) {
  377. for ( var name in geometry.attributes ) {
  378. if ( geometry.attributes[ name ].buffer !== undefined ) {
  379. _gl.deleteBuffer( geometry.attributes[ name ].buffer );
  380. }
  381. }
  382. _this.info.memory.geometries --;
  383. } else {
  384. if ( geometry.geometryGroups !== undefined ) {
  385. for ( var i = 0,l = geometry.geometryGroupsList.length; i<l;i++ ) {
  386. var geometryGroup = geometry.geometryGroupsList[ i ];
  387. if ( geometryGroup.numMorphTargets !== undefined ) {
  388. for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  389. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  390. }
  391. }
  392. if ( geometryGroup.numMorphNormals !== undefined ) {
  393. for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  394. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  395. }
  396. }
  397. deleteBuffers( geometryGroup );
  398. }
  399. } else {
  400. deleteBuffers( geometry );
  401. }
  402. }
  403. };
  404. var deallocateTexture = function ( texture ) {
  405. if ( texture.image && texture.image.__webglTextureCube ) {
  406. // cube texture
  407. _gl.deleteTexture( texture.image.__webglTextureCube );
  408. } else {
  409. // 2D texture
  410. if ( ! texture.__webglInit ) return;
  411. texture.__webglInit = false;
  412. _gl.deleteTexture( texture.__webglTexture );
  413. }
  414. };
  415. var deallocateRenderTarget = function ( renderTarget ) {
  416. if ( ! renderTarget || ! renderTarget.__webglTexture ) return;
  417. _gl.deleteTexture( renderTarget.__webglTexture );
  418. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  419. for ( var i = 0; i < 6; i ++ ) {
  420. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  421. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  422. }
  423. } else {
  424. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  425. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  426. }
  427. };
  428. var deallocateMaterial = function ( material ) {
  429. var program = material.program.program;
  430. if ( program === undefined ) return;
  431. material.program = undefined;
  432. // only deallocate GL program if this was the last use of shared program
  433. // assumed there is only single copy of any program in the _programs list
  434. // (that's how it's constructed)
  435. var i, il, programInfo;
  436. var deleteProgram = false;
  437. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  438. programInfo = _programs[ i ];
  439. if ( programInfo.program === program ) {
  440. programInfo.usedTimes --;
  441. if ( programInfo.usedTimes === 0 ) {
  442. deleteProgram = true;
  443. }
  444. break;
  445. }
  446. }
  447. if ( deleteProgram === true ) {
  448. // avoid using array.splice, this is costlier than creating new array from scratch
  449. var newPrograms = [];
  450. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  451. programInfo = _programs[ i ];
  452. if ( programInfo.program !== program ) {
  453. newPrograms.push( programInfo );
  454. }
  455. }
  456. _programs = newPrograms;
  457. _gl.deleteProgram( program );
  458. _this.info.memory.programs --;
  459. }
  460. };
  461. // Buffer initialization
  462. function initCustomAttributes ( geometry, object ) {
  463. var nvertices = geometry.vertices.length;
  464. var material = object.material;
  465. if ( material.attributes ) {
  466. if ( geometry.__webglCustomAttributesList === undefined ) {
  467. geometry.__webglCustomAttributesList = [];
  468. }
  469. for ( var name in material.attributes ) {
  470. var attribute = material.attributes[ name ];
  471. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  472. attribute.__webglInitialized = true;
  473. var size = 1; // "f" and "i"
  474. if ( attribute.type === 'v2' ) size = 2;
  475. else if ( attribute.type === 'v3' ) size = 3;
  476. else if ( attribute.type === 'v4' ) size = 4;
  477. else if ( attribute.type === 'c' ) size = 3;
  478. attribute.size = size;
  479. attribute.array = new Float32Array( nvertices * size );
  480. attribute.buffer = _gl.createBuffer();
  481. attribute.buffer.belongsToAttribute = name;
  482. attribute.needsUpdate = true;
  483. }
  484. geometry.__webglCustomAttributesList.push( attribute );
  485. }
  486. }
  487. };
  488. function initParticleBuffers ( geometry, object ) {
  489. var nvertices = geometry.vertices.length;
  490. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  491. geometry.__colorArray = new Float32Array( nvertices * 3 );
  492. geometry.__sortArray = [];
  493. geometry.__webglParticleCount = nvertices;
  494. initCustomAttributes ( geometry, object );
  495. };
  496. function initLineBuffers ( geometry, object ) {
  497. var nvertices = geometry.vertices.length;
  498. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  499. geometry.__colorArray = new Float32Array( nvertices * 3 );
  500. geometry.__lineDistanceArray = new Float32Array( nvertices * 1 );
  501. geometry.__webglLineCount = nvertices;
  502. initCustomAttributes ( geometry, object );
  503. };
  504. function initMeshBuffers ( geometryGroup, object ) {
  505. var geometry = object.geometry,
  506. faces3 = geometryGroup.faces3,
  507. nvertices = faces3.length * 3,
  508. ntris = faces3.length * 1,
  509. nlines = faces3.length * 3,
  510. material = getBufferMaterial( object, geometryGroup );
  511. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  512. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  513. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  514. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  515. if ( geometry.faceVertexUvs.length > 1 ) {
  516. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  517. }
  518. if ( geometry.hasTangents ) {
  519. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  520. }
  521. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  522. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  523. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  524. }
  525. var UintArray = _glExtensionElementIndexUint !== null && ntris > 21845 ? Uint32Array : Uint16Array; // 65535 / 3
  526. geometryGroup.__typeArray = UintArray;
  527. geometryGroup.__faceArray = new UintArray( ntris * 3 );
  528. geometryGroup.__lineArray = new UintArray( nlines * 2 );
  529. var m, ml;
  530. if ( geometryGroup.numMorphTargets ) {
  531. geometryGroup.__morphTargetsArrays = [];
  532. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  533. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  534. }
  535. }
  536. if ( geometryGroup.numMorphNormals ) {
  537. geometryGroup.__morphNormalsArrays = [];
  538. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  539. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  540. }
  541. }
  542. geometryGroup.__webglFaceCount = ntris * 3;
  543. geometryGroup.__webglLineCount = nlines * 2;
  544. // custom attributes
  545. if ( material.attributes ) {
  546. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  547. geometryGroup.__webglCustomAttributesList = [];
  548. }
  549. for ( var name in material.attributes ) {
  550. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  551. // attribute buffers which are correctly indexed in the setMeshBuffers function
  552. var originalAttribute = material.attributes[ name ];
  553. var attribute = {};
  554. for ( var property in originalAttribute ) {
  555. attribute[ property ] = originalAttribute[ property ];
  556. }
  557. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  558. attribute.__webglInitialized = true;
  559. var size = 1; // "f" and "i"
  560. if ( attribute.type === 'v2' ) size = 2;
  561. else if ( attribute.type === 'v3' ) size = 3;
  562. else if ( attribute.type === 'v4' ) size = 4;
  563. else if ( attribute.type === 'c' ) size = 3;
  564. attribute.size = size;
  565. attribute.array = new Float32Array( nvertices * size );
  566. attribute.buffer = _gl.createBuffer();
  567. attribute.buffer.belongsToAttribute = name;
  568. originalAttribute.needsUpdate = true;
  569. attribute.__original = originalAttribute;
  570. }
  571. geometryGroup.__webglCustomAttributesList.push( attribute );
  572. }
  573. }
  574. geometryGroup.__inittedArrays = true;
  575. };
  576. function getBufferMaterial( object, geometryGroup ) {
  577. return object.material instanceof THREE.MeshFaceMaterial
  578. ? object.material.materials[ geometryGroup.materialIndex ]
  579. : object.material;
  580. };
  581. function materialNeedsSmoothNormals ( material ) {
  582. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  583. };
  584. // Buffer setting
  585. function setParticleBuffers ( geometry, hint, object ) {
  586. var v, c, vertex, offset, index, color,
  587. vertices = geometry.vertices,
  588. vl = vertices.length,
  589. colors = geometry.colors,
  590. cl = colors.length,
  591. vertexArray = geometry.__vertexArray,
  592. colorArray = geometry.__colorArray,
  593. sortArray = geometry.__sortArray,
  594. dirtyVertices = geometry.verticesNeedUpdate,
  595. dirtyElements = geometry.elementsNeedUpdate,
  596. dirtyColors = geometry.colorsNeedUpdate,
  597. customAttributes = geometry.__webglCustomAttributesList,
  598. i, il,
  599. a, ca, cal, value,
  600. customAttribute;
  601. if ( object.sortParticles ) {
  602. _projScreenMatrixPS.copy( _projScreenMatrix );
  603. _projScreenMatrixPS.multiply( object.matrixWorld );
  604. for ( v = 0; v < vl; v ++ ) {
  605. vertex = vertices[ v ];
  606. _vector3.copy( vertex );
  607. _vector3.applyProjection( _projScreenMatrixPS );
  608. sortArray[ v ] = [ _vector3.z, v ];
  609. }
  610. sortArray.sort( numericalSort );
  611. for ( v = 0; v < vl; v ++ ) {
  612. vertex = vertices[ sortArray[ v ][ 1 ] ];
  613. offset = v * 3;
  614. vertexArray[ offset ] = vertex.x;
  615. vertexArray[ offset + 1 ] = vertex.y;
  616. vertexArray[ offset + 2 ] = vertex.z;
  617. }
  618. for ( c = 0; c < cl; c ++ ) {
  619. offset = c * 3;
  620. color = colors[ sortArray[ c ][ 1 ] ];
  621. colorArray[ offset ] = color.r;
  622. colorArray[ offset + 1 ] = color.g;
  623. colorArray[ offset + 2 ] = color.b;
  624. }
  625. if ( customAttributes ) {
  626. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  627. customAttribute = customAttributes[ i ];
  628. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) ) continue;
  629. offset = 0;
  630. cal = customAttribute.value.length;
  631. if ( customAttribute.size === 1 ) {
  632. for ( ca = 0; ca < cal; ca ++ ) {
  633. index = sortArray[ ca ][ 1 ];
  634. customAttribute.array[ ca ] = customAttribute.value[ index ];
  635. }
  636. } else if ( customAttribute.size === 2 ) {
  637. for ( ca = 0; ca < cal; ca ++ ) {
  638. index = sortArray[ ca ][ 1 ];
  639. value = customAttribute.value[ index ];
  640. customAttribute.array[ offset ] = value.x;
  641. customAttribute.array[ offset + 1 ] = value.y;
  642. offset += 2;
  643. }
  644. } else if ( customAttribute.size === 3 ) {
  645. if ( customAttribute.type === 'c' ) {
  646. for ( ca = 0; ca < cal; ca ++ ) {
  647. index = sortArray[ ca ][ 1 ];
  648. value = customAttribute.value[ index ];
  649. customAttribute.array[ offset ] = value.r;
  650. customAttribute.array[ offset + 1 ] = value.g;
  651. customAttribute.array[ offset + 2 ] = value.b;
  652. offset += 3;
  653. }
  654. } else {
  655. for ( ca = 0; ca < cal; ca ++ ) {
  656. index = sortArray[ ca ][ 1 ];
  657. value = customAttribute.value[ index ];
  658. customAttribute.array[ offset ] = value.x;
  659. customAttribute.array[ offset + 1 ] = value.y;
  660. customAttribute.array[ offset + 2 ] = value.z;
  661. offset += 3;
  662. }
  663. }
  664. } else if ( customAttribute.size === 4 ) {
  665. for ( ca = 0; ca < cal; ca ++ ) {
  666. index = sortArray[ ca ][ 1 ];
  667. value = customAttribute.value[ index ];
  668. customAttribute.array[ offset ] = value.x;
  669. customAttribute.array[ offset + 1 ] = value.y;
  670. customAttribute.array[ offset + 2 ] = value.z;
  671. customAttribute.array[ offset + 3 ] = value.w;
  672. offset += 4;
  673. }
  674. }
  675. }
  676. }
  677. } else {
  678. if ( dirtyVertices ) {
  679. for ( v = 0; v < vl; v ++ ) {
  680. vertex = vertices[ v ];
  681. offset = v * 3;
  682. vertexArray[ offset ] = vertex.x;
  683. vertexArray[ offset + 1 ] = vertex.y;
  684. vertexArray[ offset + 2 ] = vertex.z;
  685. }
  686. }
  687. if ( dirtyColors ) {
  688. for ( c = 0; c < cl; c ++ ) {
  689. color = colors[ c ];
  690. offset = c * 3;
  691. colorArray[ offset ] = color.r;
  692. colorArray[ offset + 1 ] = color.g;
  693. colorArray[ offset + 2 ] = color.b;
  694. }
  695. }
  696. if ( customAttributes ) {
  697. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  698. customAttribute = customAttributes[ i ];
  699. if ( customAttribute.needsUpdate &&
  700. ( customAttribute.boundTo === undefined ||
  701. customAttribute.boundTo === 'vertices' ) ) {
  702. cal = customAttribute.value.length;
  703. offset = 0;
  704. if ( customAttribute.size === 1 ) {
  705. for ( ca = 0; ca < cal; ca ++ ) {
  706. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  707. }
  708. } else if ( customAttribute.size === 2 ) {
  709. for ( ca = 0; ca < cal; ca ++ ) {
  710. value = customAttribute.value[ ca ];
  711. customAttribute.array[ offset ] = value.x;
  712. customAttribute.array[ offset + 1 ] = value.y;
  713. offset += 2;
  714. }
  715. } else if ( customAttribute.size === 3 ) {
  716. if ( customAttribute.type === 'c' ) {
  717. for ( ca = 0; ca < cal; ca ++ ) {
  718. value = customAttribute.value[ ca ];
  719. customAttribute.array[ offset ] = value.r;
  720. customAttribute.array[ offset + 1 ] = value.g;
  721. customAttribute.array[ offset + 2 ] = value.b;
  722. offset += 3;
  723. }
  724. } else {
  725. for ( ca = 0; ca < cal; ca ++ ) {
  726. value = customAttribute.value[ ca ];
  727. customAttribute.array[ offset ] = value.x;
  728. customAttribute.array[ offset + 1 ] = value.y;
  729. customAttribute.array[ offset + 2 ] = value.z;
  730. offset += 3;
  731. }
  732. }
  733. } else if ( customAttribute.size === 4 ) {
  734. for ( ca = 0; ca < cal; ca ++ ) {
  735. value = customAttribute.value[ ca ];
  736. customAttribute.array[ offset ] = value.x;
  737. customAttribute.array[ offset + 1 ] = value.y;
  738. customAttribute.array[ offset + 2 ] = value.z;
  739. customAttribute.array[ offset + 3 ] = value.w;
  740. offset += 4;
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. if ( dirtyVertices || object.sortParticles ) {
  748. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  749. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  750. }
  751. if ( dirtyColors || object.sortParticles ) {
  752. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  753. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  754. }
  755. if ( customAttributes ) {
  756. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  757. customAttribute = customAttributes[ i ];
  758. if ( customAttribute.needsUpdate || object.sortParticles ) {
  759. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  760. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  761. }
  762. }
  763. }
  764. }
  765. function setLineBuffers ( geometry, hint ) {
  766. var v, c, d, vertex, offset, color,
  767. vertices = geometry.vertices,
  768. colors = geometry.colors,
  769. lineDistances = geometry.lineDistances,
  770. vl = vertices.length,
  771. cl = colors.length,
  772. dl = lineDistances.length,
  773. vertexArray = geometry.__vertexArray,
  774. colorArray = geometry.__colorArray,
  775. lineDistanceArray = geometry.__lineDistanceArray,
  776. dirtyVertices = geometry.verticesNeedUpdate,
  777. dirtyColors = geometry.colorsNeedUpdate,
  778. dirtyLineDistances = geometry.lineDistancesNeedUpdate,
  779. customAttributes = geometry.__webglCustomAttributesList,
  780. i, il,
  781. a, ca, cal, value,
  782. customAttribute;
  783. if ( dirtyVertices ) {
  784. for ( v = 0; v < vl; v ++ ) {
  785. vertex = vertices[ v ];
  786. offset = v * 3;
  787. vertexArray[ offset ] = vertex.x;
  788. vertexArray[ offset + 1 ] = vertex.y;
  789. vertexArray[ offset + 2 ] = vertex.z;
  790. }
  791. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  792. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  793. }
  794. if ( dirtyColors ) {
  795. for ( c = 0; c < cl; c ++ ) {
  796. color = colors[ c ];
  797. offset = c * 3;
  798. colorArray[ offset ] = color.r;
  799. colorArray[ offset + 1 ] = color.g;
  800. colorArray[ offset + 2 ] = color.b;
  801. }
  802. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  803. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  804. }
  805. if ( dirtyLineDistances ) {
  806. for ( d = 0; d < dl; d ++ ) {
  807. lineDistanceArray[ d ] = lineDistances[ d ];
  808. }
  809. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer );
  810. _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint );
  811. }
  812. if ( customAttributes ) {
  813. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  814. customAttribute = customAttributes[ i ];
  815. if ( customAttribute.needsUpdate &&
  816. ( customAttribute.boundTo === undefined ||
  817. customAttribute.boundTo === 'vertices' ) ) {
  818. offset = 0;
  819. cal = customAttribute.value.length;
  820. if ( customAttribute.size === 1 ) {
  821. for ( ca = 0; ca < cal; ca ++ ) {
  822. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  823. }
  824. } else if ( customAttribute.size === 2 ) {
  825. for ( ca = 0; ca < cal; ca ++ ) {
  826. value = customAttribute.value[ ca ];
  827. customAttribute.array[ offset ] = value.x;
  828. customAttribute.array[ offset + 1 ] = value.y;
  829. offset += 2;
  830. }
  831. } else if ( customAttribute.size === 3 ) {
  832. if ( customAttribute.type === 'c' ) {
  833. for ( ca = 0; ca < cal; ca ++ ) {
  834. value = customAttribute.value[ ca ];
  835. customAttribute.array[ offset ] = value.r;
  836. customAttribute.array[ offset + 1 ] = value.g;
  837. customAttribute.array[ offset + 2 ] = value.b;
  838. offset += 3;
  839. }
  840. } else {
  841. for ( ca = 0; ca < cal; ca ++ ) {
  842. value = customAttribute.value[ ca ];
  843. customAttribute.array[ offset ] = value.x;
  844. customAttribute.array[ offset + 1 ] = value.y;
  845. customAttribute.array[ offset + 2 ] = value.z;
  846. offset += 3;
  847. }
  848. }
  849. } else if ( customAttribute.size === 4 ) {
  850. for ( ca = 0; ca < cal; ca ++ ) {
  851. value = customAttribute.value[ ca ];
  852. customAttribute.array[ offset ] = value.x;
  853. customAttribute.array[ offset + 1 ] = value.y;
  854. customAttribute.array[ offset + 2 ] = value.z;
  855. customAttribute.array[ offset + 3 ] = value.w;
  856. offset += 4;
  857. }
  858. }
  859. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  860. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  861. }
  862. }
  863. }
  864. }
  865. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  866. if ( ! geometryGroup.__inittedArrays ) {
  867. return;
  868. }
  869. var needsSmoothNormals = materialNeedsSmoothNormals( material );
  870. var f, fl, fi, face,
  871. vertexNormals, faceNormal, normal,
  872. vertexColors, faceColor,
  873. vertexTangents,
  874. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  875. c1, c2, c3,
  876. sw1, sw2, sw3, sw4,
  877. si1, si2, si3, si4,
  878. sa1, sa2, sa3, sa4,
  879. sb1, sb2, sb3, sb4,
  880. m, ml, i, il,
  881. vn, uvi, uv2i,
  882. vk, vkl, vka,
  883. nka, chf, faceVertexNormals,
  884. a,
  885. vertexIndex = 0,
  886. offset = 0,
  887. offset_uv = 0,
  888. offset_uv2 = 0,
  889. offset_face = 0,
  890. offset_normal = 0,
  891. offset_tangent = 0,
  892. offset_line = 0,
  893. offset_color = 0,
  894. offset_skin = 0,
  895. offset_morphTarget = 0,
  896. offset_custom = 0,
  897. offset_customSrc = 0,
  898. value,
  899. vertexArray = geometryGroup.__vertexArray,
  900. uvArray = geometryGroup.__uvArray,
  901. uv2Array = geometryGroup.__uv2Array,
  902. normalArray = geometryGroup.__normalArray,
  903. tangentArray = geometryGroup.__tangentArray,
  904. colorArray = geometryGroup.__colorArray,
  905. skinIndexArray = geometryGroup.__skinIndexArray,
  906. skinWeightArray = geometryGroup.__skinWeightArray,
  907. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  908. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  909. customAttributes = geometryGroup.__webglCustomAttributesList,
  910. customAttribute,
  911. faceArray = geometryGroup.__faceArray,
  912. lineArray = geometryGroup.__lineArray,
  913. geometry = object.geometry, // this is shared for all chunks
  914. dirtyVertices = geometry.verticesNeedUpdate,
  915. dirtyElements = geometry.elementsNeedUpdate,
  916. dirtyUvs = geometry.uvsNeedUpdate,
  917. dirtyNormals = geometry.normalsNeedUpdate,
  918. dirtyTangents = geometry.tangentsNeedUpdate,
  919. dirtyColors = geometry.colorsNeedUpdate,
  920. dirtyMorphTargets = geometry.morphTargetsNeedUpdate,
  921. vertices = geometry.vertices,
  922. chunk_faces3 = geometryGroup.faces3,
  923. obj_faces = geometry.faces,
  924. obj_uvs = geometry.faceVertexUvs[ 0 ],
  925. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  926. obj_colors = geometry.colors,
  927. obj_skinIndices = geometry.skinIndices,
  928. obj_skinWeights = geometry.skinWeights,
  929. morphTargets = geometry.morphTargets,
  930. morphNormals = geometry.morphNormals;
  931. if ( dirtyVertices ) {
  932. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  933. face = obj_faces[ chunk_faces3[ f ] ];
  934. v1 = vertices[ face.a ];
  935. v2 = vertices[ face.b ];
  936. v3 = vertices[ face.c ];
  937. vertexArray[ offset ] = v1.x;
  938. vertexArray[ offset + 1 ] = v1.y;
  939. vertexArray[ offset + 2 ] = v1.z;
  940. vertexArray[ offset + 3 ] = v2.x;
  941. vertexArray[ offset + 4 ] = v2.y;
  942. vertexArray[ offset + 5 ] = v2.z;
  943. vertexArray[ offset + 6 ] = v3.x;
  944. vertexArray[ offset + 7 ] = v3.y;
  945. vertexArray[ offset + 8 ] = v3.z;
  946. offset += 9;
  947. }
  948. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  949. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  950. }
  951. if ( dirtyMorphTargets ) {
  952. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  953. offset_morphTarget = 0;
  954. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  955. chf = chunk_faces3[ f ];
  956. face = obj_faces[ chf ];
  957. // morph positions
  958. v1 = morphTargets[ vk ].vertices[ face.a ];
  959. v2 = morphTargets[ vk ].vertices[ face.b ];
  960. v3 = morphTargets[ vk ].vertices[ face.c ];
  961. vka = morphTargetsArrays[ vk ];
  962. vka[ offset_morphTarget ] = v1.x;
  963. vka[ offset_morphTarget + 1 ] = v1.y;
  964. vka[ offset_morphTarget + 2 ] = v1.z;
  965. vka[ offset_morphTarget + 3 ] = v2.x;
  966. vka[ offset_morphTarget + 4 ] = v2.y;
  967. vka[ offset_morphTarget + 5 ] = v2.z;
  968. vka[ offset_morphTarget + 6 ] = v3.x;
  969. vka[ offset_morphTarget + 7 ] = v3.y;
  970. vka[ offset_morphTarget + 8 ] = v3.z;
  971. // morph normals
  972. if ( material.morphNormals ) {
  973. if ( needsSmoothNormals ) {
  974. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  975. n1 = faceVertexNormals.a;
  976. n2 = faceVertexNormals.b;
  977. n3 = faceVertexNormals.c;
  978. } else {
  979. n1 = morphNormals[ vk ].faceNormals[ chf ];
  980. n2 = n1;
  981. n3 = n1;
  982. }
  983. nka = morphNormalsArrays[ vk ];
  984. nka[ offset_morphTarget ] = n1.x;
  985. nka[ offset_morphTarget + 1 ] = n1.y;
  986. nka[ offset_morphTarget + 2 ] = n1.z;
  987. nka[ offset_morphTarget + 3 ] = n2.x;
  988. nka[ offset_morphTarget + 4 ] = n2.y;
  989. nka[ offset_morphTarget + 5 ] = n2.z;
  990. nka[ offset_morphTarget + 6 ] = n3.x;
  991. nka[ offset_morphTarget + 7 ] = n3.y;
  992. nka[ offset_morphTarget + 8 ] = n3.z;
  993. }
  994. //
  995. offset_morphTarget += 9;
  996. }
  997. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  998. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  999. if ( material.morphNormals ) {
  1000. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  1001. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  1002. }
  1003. }
  1004. }
  1005. if ( obj_skinWeights.length ) {
  1006. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1007. face = obj_faces[ chunk_faces3[ f ] ];
  1008. // weights
  1009. sw1 = obj_skinWeights[ face.a ];
  1010. sw2 = obj_skinWeights[ face.b ];
  1011. sw3 = obj_skinWeights[ face.c ];
  1012. skinWeightArray[ offset_skin ] = sw1.x;
  1013. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  1014. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  1015. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  1016. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  1017. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  1018. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  1019. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  1020. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  1021. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  1022. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  1023. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  1024. // indices
  1025. si1 = obj_skinIndices[ face.a ];
  1026. si2 = obj_skinIndices[ face.b ];
  1027. si3 = obj_skinIndices[ face.c ];
  1028. skinIndexArray[ offset_skin ] = si1.x;
  1029. skinIndexArray[ offset_skin + 1 ] = si1.y;
  1030. skinIndexArray[ offset_skin + 2 ] = si1.z;
  1031. skinIndexArray[ offset_skin + 3 ] = si1.w;
  1032. skinIndexArray[ offset_skin + 4 ] = si2.x;
  1033. skinIndexArray[ offset_skin + 5 ] = si2.y;
  1034. skinIndexArray[ offset_skin + 6 ] = si2.z;
  1035. skinIndexArray[ offset_skin + 7 ] = si2.w;
  1036. skinIndexArray[ offset_skin + 8 ] = si3.x;
  1037. skinIndexArray[ offset_skin + 9 ] = si3.y;
  1038. skinIndexArray[ offset_skin + 10 ] = si3.z;
  1039. skinIndexArray[ offset_skin + 11 ] = si3.w;
  1040. offset_skin += 12;
  1041. }
  1042. if ( offset_skin > 0 ) {
  1043. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1044. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  1045. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1046. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  1047. }
  1048. }
  1049. if ( dirtyColors ) {
  1050. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1051. face = obj_faces[ chunk_faces3[ f ] ];
  1052. vertexColors = face.vertexColors;
  1053. faceColor = face.color;
  1054. if ( vertexColors.length === 3 && material.vertexColors === THREE.VertexColors ) {
  1055. c1 = vertexColors[ 0 ];
  1056. c2 = vertexColors[ 1 ];
  1057. c3 = vertexColors[ 2 ];
  1058. } else {
  1059. c1 = faceColor;
  1060. c2 = faceColor;
  1061. c3 = faceColor;
  1062. }
  1063. colorArray[ offset_color ] = c1.r;
  1064. colorArray[ offset_color + 1 ] = c1.g;
  1065. colorArray[ offset_color + 2 ] = c1.b;
  1066. colorArray[ offset_color + 3 ] = c2.r;
  1067. colorArray[ offset_color + 4 ] = c2.g;
  1068. colorArray[ offset_color + 5 ] = c2.b;
  1069. colorArray[ offset_color + 6 ] = c3.r;
  1070. colorArray[ offset_color + 7 ] = c3.g;
  1071. colorArray[ offset_color + 8 ] = c3.b;
  1072. offset_color += 9;
  1073. }
  1074. if ( offset_color > 0 ) {
  1075. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1076. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1077. }
  1078. }
  1079. if ( dirtyTangents && geometry.hasTangents ) {
  1080. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1081. face = obj_faces[ chunk_faces3[ f ] ];
  1082. vertexTangents = face.vertexTangents;
  1083. t1 = vertexTangents[ 0 ];
  1084. t2 = vertexTangents[ 1 ];
  1085. t3 = vertexTangents[ 2 ];
  1086. tangentArray[ offset_tangent ] = t1.x;
  1087. tangentArray[ offset_tangent + 1 ] = t1.y;
  1088. tangentArray[ offset_tangent + 2 ] = t1.z;
  1089. tangentArray[ offset_tangent + 3 ] = t1.w;
  1090. tangentArray[ offset_tangent + 4 ] = t2.x;
  1091. tangentArray[ offset_tangent + 5 ] = t2.y;
  1092. tangentArray[ offset_tangent + 6 ] = t2.z;
  1093. tangentArray[ offset_tangent + 7 ] = t2.w;
  1094. tangentArray[ offset_tangent + 8 ] = t3.x;
  1095. tangentArray[ offset_tangent + 9 ] = t3.y;
  1096. tangentArray[ offset_tangent + 10 ] = t3.z;
  1097. tangentArray[ offset_tangent + 11 ] = t3.w;
  1098. offset_tangent += 12;
  1099. }
  1100. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1101. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  1102. }
  1103. if ( dirtyNormals ) {
  1104. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1105. face = obj_faces[ chunk_faces3[ f ] ];
  1106. vertexNormals = face.vertexNormals;
  1107. faceNormal = face.normal;
  1108. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  1109. for ( i = 0; i < 3; i ++ ) {
  1110. vn = vertexNormals[ i ];
  1111. normalArray[ offset_normal ] = vn.x;
  1112. normalArray[ offset_normal + 1 ] = vn.y;
  1113. normalArray[ offset_normal + 2 ] = vn.z;
  1114. offset_normal += 3;
  1115. }
  1116. } else {
  1117. for ( i = 0; i < 3; i ++ ) {
  1118. normalArray[ offset_normal ] = faceNormal.x;
  1119. normalArray[ offset_normal + 1 ] = faceNormal.y;
  1120. normalArray[ offset_normal + 2 ] = faceNormal.z;
  1121. offset_normal += 3;
  1122. }
  1123. }
  1124. }
  1125. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1126. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  1127. }
  1128. if ( dirtyUvs && obj_uvs ) {
  1129. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1130. fi = chunk_faces3[ f ];
  1131. uv = obj_uvs[ fi ];
  1132. if ( uv === undefined ) continue;
  1133. for ( i = 0; i < 3; i ++ ) {
  1134. uvi = uv[ i ];
  1135. uvArray[ offset_uv ] = uvi.x;
  1136. uvArray[ offset_uv + 1 ] = uvi.y;
  1137. offset_uv += 2;
  1138. }
  1139. }
  1140. if ( offset_uv > 0 ) {
  1141. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1142. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  1143. }
  1144. }
  1145. if ( dirtyUvs && obj_uvs2 ) {
  1146. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1147. fi = chunk_faces3[ f ];
  1148. uv2 = obj_uvs2[ fi ];
  1149. if ( uv2 === undefined ) continue;
  1150. for ( i = 0; i < 3; i ++ ) {
  1151. uv2i = uv2[ i ];
  1152. uv2Array[ offset_uv2 ] = uv2i.x;
  1153. uv2Array[ offset_uv2 + 1 ] = uv2i.y;
  1154. offset_uv2 += 2;
  1155. }
  1156. }
  1157. if ( offset_uv2 > 0 ) {
  1158. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1159. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  1160. }
  1161. }
  1162. if ( dirtyElements ) {
  1163. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1164. faceArray[ offset_face ] = vertexIndex;
  1165. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  1166. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  1167. offset_face += 3;
  1168. lineArray[ offset_line ] = vertexIndex;
  1169. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  1170. lineArray[ offset_line + 2 ] = vertexIndex;
  1171. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  1172. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  1173. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  1174. offset_line += 6;
  1175. vertexIndex += 3;
  1176. }
  1177. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1178. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  1179. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1180. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  1181. }
  1182. if ( customAttributes ) {
  1183. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  1184. customAttribute = customAttributes[ i ];
  1185. if ( ! customAttribute.__original.needsUpdate ) continue;
  1186. offset_custom = 0;
  1187. offset_customSrc = 0;
  1188. if ( customAttribute.size === 1 ) {
  1189. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1190. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1191. face = obj_faces[ chunk_faces3[ f ] ];
  1192. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  1193. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  1194. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  1195. offset_custom += 3;
  1196. }
  1197. } else if ( customAttribute.boundTo === 'faces' ) {
  1198. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1199. value = customAttribute.value[ chunk_faces3[ f ] ];
  1200. customAttribute.array[ offset_custom ] = value;
  1201. customAttribute.array[ offset_custom + 1 ] = value;
  1202. customAttribute.array[ offset_custom + 2 ] = value;
  1203. offset_custom += 3;
  1204. }
  1205. }
  1206. } else if ( customAttribute.size === 2 ) {
  1207. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1208. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1209. face = obj_faces[ chunk_faces3[ f ] ];
  1210. v1 = customAttribute.value[ face.a ];
  1211. v2 = customAttribute.value[ face.b ];
  1212. v3 = customAttribute.value[ face.c ];
  1213. customAttribute.array[ offset_custom ] = v1.x;
  1214. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1215. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1216. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1217. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1218. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1219. offset_custom += 6;
  1220. }
  1221. } else if ( customAttribute.boundTo === 'faces' ) {
  1222. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1223. value = customAttribute.value[ chunk_faces3[ f ] ];
  1224. v1 = value;
  1225. v2 = value;
  1226. v3 = value;
  1227. customAttribute.array[ offset_custom ] = v1.x;
  1228. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1229. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1230. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1231. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1232. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1233. offset_custom += 6;
  1234. }
  1235. }
  1236. } else if ( customAttribute.size === 3 ) {
  1237. var pp;
  1238. if ( customAttribute.type === 'c' ) {
  1239. pp = [ 'r', 'g', 'b' ];
  1240. } else {
  1241. pp = [ 'x', 'y', 'z' ];
  1242. }
  1243. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1244. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1245. face = obj_faces[ chunk_faces3[ f ] ];
  1246. v1 = customAttribute.value[ face.a ];
  1247. v2 = customAttribute.value[ face.b ];
  1248. v3 = customAttribute.value[ face.c ];
  1249. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1250. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1251. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1252. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1253. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1254. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1255. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1256. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1257. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1258. offset_custom += 9;
  1259. }
  1260. } else if ( customAttribute.boundTo === 'faces' ) {
  1261. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1262. value = customAttribute.value[ chunk_faces3[ f ] ];
  1263. v1 = value;
  1264. v2 = value;
  1265. v3 = value;
  1266. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1267. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1268. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1269. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1270. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1271. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1272. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1273. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1274. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1275. offset_custom += 9;
  1276. }
  1277. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  1278. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1279. value = customAttribute.value[ chunk_faces3[ f ] ];
  1280. v1 = value[ 0 ];
  1281. v2 = value[ 1 ];
  1282. v3 = value[ 2 ];
  1283. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1284. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1285. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1286. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1287. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1288. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1289. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1290. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1291. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1292. offset_custom += 9;
  1293. }
  1294. }
  1295. } else if ( customAttribute.size === 4 ) {
  1296. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1297. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1298. face = obj_faces[ chunk_faces3[ f ] ];
  1299. v1 = customAttribute.value[ face.a ];
  1300. v2 = customAttribute.value[ face.b ];
  1301. v3 = customAttribute.value[ face.c ];
  1302. customAttribute.array[ offset_custom ] = v1.x;
  1303. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1304. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1305. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1306. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1307. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1308. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1309. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1310. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1311. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1312. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1313. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1314. offset_custom += 12;
  1315. }
  1316. } else if ( customAttribute.boundTo === 'faces' ) {
  1317. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1318. value = customAttribute.value[ chunk_faces3[ f ] ];
  1319. v1 = value;
  1320. v2 = value;
  1321. v3 = value;
  1322. customAttribute.array[ offset_custom ] = v1.x;
  1323. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1324. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1325. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1326. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1327. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1328. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1329. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1330. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1331. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1332. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1333. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1334. offset_custom += 12;
  1335. }
  1336. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  1337. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1338. value = customAttribute.value[ chunk_faces3[ f ] ];
  1339. v1 = value[ 0 ];
  1340. v2 = value[ 1 ];
  1341. v3 = value[ 2 ];
  1342. customAttribute.array[ offset_custom ] = v1.x;
  1343. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1344. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1345. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1346. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1347. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1348. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1349. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1350. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1351. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1352. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1353. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1354. offset_custom += 12;
  1355. }
  1356. }
  1357. }
  1358. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  1359. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  1360. }
  1361. }
  1362. if ( dispose ) {
  1363. delete geometryGroup.__inittedArrays;
  1364. delete geometryGroup.__colorArray;
  1365. delete geometryGroup.__normalArray;
  1366. delete geometryGroup.__tangentArray;
  1367. delete geometryGroup.__uvArray;
  1368. delete geometryGroup.__uv2Array;
  1369. delete geometryGroup.__faceArray;
  1370. delete geometryGroup.__vertexArray;
  1371. delete geometryGroup.__lineArray;
  1372. delete geometryGroup.__skinIndexArray;
  1373. delete geometryGroup.__skinWeightArray;
  1374. }
  1375. };
  1376. function setDirectBuffers( geometry ) {
  1377. var attributes = geometry.attributes;
  1378. var attributesKeys = geometry.attributesKeys;
  1379. for ( var i = 0, l = attributesKeys.length; i < l; i ++ ) {
  1380. var attributeName = attributesKeys[ i ];
  1381. var attributeItem = attributes[ attributeName ];
  1382. if ( attributeItem.buffer === undefined ) {
  1383. attributeItem.buffer = _gl.createBuffer();
  1384. attributeItem.needsUpdate = true;
  1385. }
  1386. if ( attributeItem.needsUpdate === true ) {
  1387. var bufferType = ( attributeName === 'index' ) ? _gl.ELEMENT_ARRAY_BUFFER : _gl.ARRAY_BUFFER;
  1388. _gl.bindBuffer( bufferType, attributeItem.buffer );
  1389. _gl.bufferData( bufferType, attributeItem.array, _gl.STATIC_DRAW );
  1390. attributeItem.needsUpdate = false;
  1391. }
  1392. }
  1393. }
  1394. // Buffer rendering
  1395. this.renderBufferImmediate = function ( object, program, material ) {
  1396. initAttributes();
  1397. if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  1398. if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  1399. if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
  1400. if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
  1401. if ( object.hasPositions ) {
  1402. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  1403. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  1404. enableAttribute( program.attributes.position );
  1405. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1406. }
  1407. if ( object.hasNormals ) {
  1408. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  1409. if ( material.shading === THREE.FlatShading ) {
  1410. var nx, ny, nz,
  1411. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  1412. normalArray,
  1413. i, il = object.count * 3;
  1414. for ( i = 0; i < il; i += 9 ) {
  1415. normalArray = object.normalArray;
  1416. nax = normalArray[ i ];
  1417. nay = normalArray[ i + 1 ];
  1418. naz = normalArray[ i + 2 ];
  1419. nbx = normalArray[ i + 3 ];
  1420. nby = normalArray[ i + 4 ];
  1421. nbz = normalArray[ i + 5 ];
  1422. ncx = normalArray[ i + 6 ];
  1423. ncy = normalArray[ i + 7 ];
  1424. ncz = normalArray[ i + 8 ];
  1425. nx = ( nax + nbx + ncx ) / 3;
  1426. ny = ( nay + nby + ncy ) / 3;
  1427. nz = ( naz + nbz + ncz ) / 3;
  1428. normalArray[ i ] = nx;
  1429. normalArray[ i + 1 ] = ny;
  1430. normalArray[ i + 2 ] = nz;
  1431. normalArray[ i + 3 ] = nx;
  1432. normalArray[ i + 4 ] = ny;
  1433. normalArray[ i + 5 ] = nz;
  1434. normalArray[ i + 6 ] = nx;
  1435. normalArray[ i + 7 ] = ny;
  1436. normalArray[ i + 8 ] = nz;
  1437. }
  1438. }
  1439. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  1440. enableAttribute( program.attributes.normal );
  1441. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1442. }
  1443. if ( object.hasUvs && material.map ) {
  1444. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
  1445. _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
  1446. enableAttribute( program.attributes.uv );
  1447. _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1448. }
  1449. if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
  1450. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
  1451. _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
  1452. enableAttribute( program.attributes.color );
  1453. _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1454. }
  1455. disableUnusedAttributes();
  1456. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  1457. object.count = 0;
  1458. };
  1459. function setupVertexAttributes( material, program, geometry, startIndex ) {
  1460. var geometryAttributes = geometry.attributes;
  1461. var programAttributes = program.attributes;
  1462. var programAttributesKeys = program.attributesKeys;
  1463. for ( var i = 0, l = programAttributesKeys.length; i < l; i ++ ) {
  1464. var attributeName = programAttributesKeys[ i ];
  1465. var attributePointer = programAttributes[ attributeName ];
  1466. if ( attributePointer >= 0 ) {
  1467. var attributeItem = geometryAttributes[ attributeName ];
  1468. if ( attributeItem !== undefined ) {
  1469. var attributeSize = attributeItem.itemSize;
  1470. _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
  1471. enableAttribute( attributePointer );
  1472. _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32
  1473. } else if ( material.defaultAttributeValues !== undefined ) {
  1474. if ( material.defaultAttributeValues[ attributeName ].length === 2 ) {
  1475. _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] );
  1476. } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) {
  1477. _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] );
  1478. }
  1479. }
  1480. }
  1481. }
  1482. disableUnusedAttributes();
  1483. }
  1484. this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
  1485. if ( material.visible === false ) return;
  1486. var linewidth, a, attribute;
  1487. var attributeItem, attributeName, attributePointer, attributeSize;
  1488. var program = setProgram( camera, lights, fog, material, object );
  1489. var updateBuffers = false,
  1490. wireframeBit = material.wireframe ? 1 : 0,
  1491. geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  1492. if ( geometryHash !== _currentGeometryGroupHash ) {
  1493. _currentGeometryGroupHash = geometryHash;
  1494. updateBuffers = true;
  1495. }
  1496. if ( updateBuffers ) {
  1497. initAttributes();
  1498. }
  1499. // render mesh
  1500. if ( object instanceof THREE.Mesh ) {
  1501. var mode = material.wireframe === true ? _gl.LINES : _gl.TRIANGLES;
  1502. var index = geometry.attributes.index;
  1503. if ( index ) {
  1504. // indexed triangles
  1505. var type, size;
  1506. if ( index.array instanceof Uint32Array ) {
  1507. type = _gl.UNSIGNED_INT;
  1508. size = 4;
  1509. } else {
  1510. type = _gl.UNSIGNED_SHORT;
  1511. size = 2;
  1512. }
  1513. var offsets = geometry.offsets;
  1514. if ( offsets.length === 0 ) {
  1515. if ( updateBuffers ) {
  1516. setupVertexAttributes( material, program, geometry, 0 );
  1517. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1518. }
  1519. _gl.drawElements( mode, index.array.length, type, 0 );
  1520. _this.info.render.calls ++;
  1521. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  1522. _this.info.render.faces += index.array.length / 3;
  1523. } else {
  1524. // if there is more than 1 chunk
  1525. // must set attribute pointers to use new offsets for each chunk
  1526. // even if geometry and materials didn't change
  1527. updateBuffers = true;
  1528. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  1529. var startIndex = offsets[ i ].index;
  1530. if ( updateBuffers ) {
  1531. setupVertexAttributes( material, program, geometry, startIndex );
  1532. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1533. }
  1534. // render indexed triangles
  1535. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
  1536. _this.info.render.calls ++;
  1537. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  1538. _this.info.render.faces += offsets[ i ].count / 3;
  1539. }
  1540. }
  1541. } else {
  1542. // non-indexed triangles
  1543. if ( updateBuffers ) {
  1544. setupVertexAttributes( material, program, geometry, 0 );
  1545. }
  1546. var position = geometry.attributes[ 'position' ];
  1547. // render non-indexed triangles
  1548. _gl.drawArrays( mode, 0, position.array.length / 3 );
  1549. _this.info.render.calls ++;
  1550. _this.info.render.vertices += position.array.length / 3;
  1551. _this.info.render.faces += position.array.length / 9;
  1552. }
  1553. } else if ( object instanceof THREE.PointCloud ) {
  1554. // render particles
  1555. if ( updateBuffers ) {
  1556. setupVertexAttributes( material, program, geometry, 0 );
  1557. }
  1558. var position = geometry.attributes.position;
  1559. // render particles
  1560. _gl.drawArrays( _gl.POINTS, 0, position.array.length / 3 );
  1561. _this.info.render.calls ++;
  1562. _this.info.render.points += position.array.length / 3;
  1563. } else if ( object instanceof THREE.Line ) {
  1564. var mode = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1565. setLineWidth( material.linewidth );
  1566. var index = geometry.attributes.index;
  1567. if ( index ) {
  1568. // indexed lines
  1569. var type, size;
  1570. if ( index.array instanceof Uint32Array ) {
  1571. type = _gl.UNSIGNED_INT;
  1572. size = 4;
  1573. } else {
  1574. type = _gl.UNSIGNED_SHORT;
  1575. size = 2;
  1576. }
  1577. var offsets = geometry.offsets;
  1578. if ( offsets.length === 0 ) {
  1579. if ( updateBuffers ) {
  1580. setupVertexAttributes( material, program, geometry, 0 );
  1581. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1582. }
  1583. _gl.drawElements( mode, index.array.length, type, 0 ); // 2 bytes per Uint16Array
  1584. _this.info.render.calls ++;
  1585. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  1586. } else {
  1587. // if there is more than 1 chunk
  1588. // must set attribute pointers to use new offsets for each chunk
  1589. // even if geometry and materials didn't change
  1590. if ( offsets.length > 1 ) updateBuffers = true;
  1591. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  1592. var startIndex = offsets[ i ].index;
  1593. if ( updateBuffers ) {
  1594. setupVertexAttributes( material, program, geometry, startIndex );
  1595. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1596. }
  1597. // render indexed lines
  1598. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size ); // 2 bytes per Uint16Array
  1599. _this.info.render.calls ++;
  1600. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  1601. }
  1602. }
  1603. } else {
  1604. // non-indexed lines
  1605. if ( updateBuffers ) {
  1606. setupVertexAttributes( material, program, geometry, 0 );
  1607. }
  1608. var position = geometry.attributes.position;
  1609. _gl.drawArrays( mode, 0, position.array.length / 3 );
  1610. _this.info.render.calls ++;
  1611. _this.info.render.points += position.array.length / 3;
  1612. }
  1613. }
  1614. };
  1615. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  1616. if ( material.visible === false ) return;
  1617. var linewidth, a, attribute, i, il;
  1618. var program = setProgram( camera, lights, fog, material, object );
  1619. var attributes = program.attributes;
  1620. var updateBuffers = false,
  1621. wireframeBit = material.wireframe ? 1 : 0,
  1622. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  1623. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  1624. _currentGeometryGroupHash = geometryGroupHash;
  1625. updateBuffers = true;
  1626. }
  1627. if ( updateBuffers ) {
  1628. initAttributes();
  1629. }
  1630. // vertices
  1631. if ( ! material.morphTargets && attributes.position >= 0 ) {
  1632. if ( updateBuffers ) {
  1633. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1634. enableAttribute( attributes.position );
  1635. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1636. }
  1637. } else {
  1638. if ( object.morphTargetBase ) {
  1639. setupMorphTargets( material, geometryGroup, object );
  1640. }
  1641. }
  1642. if ( updateBuffers ) {
  1643. // custom attributes
  1644. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  1645. if ( geometryGroup.__webglCustomAttributesList ) {
  1646. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  1647. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  1648. if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  1649. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  1650. enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
  1651. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  1652. }
  1653. }
  1654. }
  1655. // colors
  1656. if ( attributes.color >= 0 ) {
  1657. if ( object.geometry.colors.length > 0 || object.geometry.faces.length > 0 ) {
  1658. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1659. enableAttribute( attributes.color );
  1660. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1661. } else if ( material.defaultAttributeValues !== undefined ) {
  1662. _gl.vertexAttrib3fv( attributes.color, material.defaultAttributeValues.color );
  1663. }
  1664. }
  1665. // normals
  1666. if ( attributes.normal >= 0 ) {
  1667. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1668. enableAttribute( attributes.normal );
  1669. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1670. }
  1671. // tangents
  1672. if ( attributes.tangent >= 0 ) {
  1673. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1674. enableAttribute( attributes.tangent );
  1675. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  1676. }
  1677. // uvs
  1678. if ( attributes.uv >= 0 ) {
  1679. if ( object.geometry.faceVertexUvs[ 0 ] ) {
  1680. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1681. enableAttribute( attributes.uv );
  1682. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1683. } else if ( material.defaultAttributeValues !== undefined ) {
  1684. _gl.vertexAttrib2fv( attributes.uv, material.defaultAttributeValues.uv );
  1685. }
  1686. }
  1687. if ( attributes.uv2 >= 0 ) {
  1688. if ( object.geometry.faceVertexUvs[ 1 ] ) {
  1689. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1690. enableAttribute( attributes.uv2 );
  1691. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  1692. } else if ( material.defaultAttributeValues !== undefined ) {
  1693. _gl.vertexAttrib2fv( attributes.uv2, material.defaultAttributeValues.uv2 );
  1694. }
  1695. }
  1696. if ( material.skinning &&
  1697. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1698. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1699. enableAttribute( attributes.skinIndex );
  1700. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  1701. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1702. enableAttribute( attributes.skinWeight );
  1703. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  1704. }
  1705. // line distances
  1706. if ( attributes.lineDistance >= 0 ) {
  1707. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
  1708. enableAttribute( attributes.lineDistance );
  1709. _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
  1710. }
  1711. }
  1712. disableUnusedAttributes();
  1713. // render mesh
  1714. if ( object instanceof THREE.Mesh ) {
  1715. var type = geometryGroup.__typeArray === Uint32Array ? _gl.UNSIGNED_INT : _gl.UNSIGNED_SHORT;
  1716. // wireframe
  1717. if ( material.wireframe ) {
  1718. setLineWidth( material.wireframeLinewidth );
  1719. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1720. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, type, 0 );
  1721. // triangles
  1722. } else {
  1723. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1724. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, type, 0 );
  1725. }
  1726. _this.info.render.calls ++;
  1727. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  1728. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  1729. // render lines
  1730. } else if ( object instanceof THREE.Line ) {
  1731. var mode = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1732. setLineWidth( material.linewidth );
  1733. _gl.drawArrays( mode, 0, geometryGroup.__webglLineCount );
  1734. _this.info.render.calls ++;
  1735. // render particles
  1736. } else if ( object instanceof THREE.PointCloud ) {
  1737. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  1738. _this.info.render.calls ++;
  1739. _this.info.render.points += geometryGroup.__webglParticleCount;
  1740. }
  1741. };
  1742. function initAttributes() {
  1743. for ( var i = 0, l = _newAttributes.length; i < l; i ++ ) {
  1744. _newAttributes[ i ] = 0;
  1745. }
  1746. }
  1747. function enableAttribute( attribute ) {
  1748. _newAttributes[ attribute ] = 1;
  1749. if ( _enabledAttributes[ attribute ] === 0 ) {
  1750. _gl.enableVertexAttribArray( attribute );
  1751. _enabledAttributes[ attribute ] = 1;
  1752. }
  1753. }
  1754. function disableUnusedAttributes() {
  1755. for ( var i = 0, l = _enabledAttributes.length; i < l; i ++ ) {
  1756. if ( _enabledAttributes[ i ] !== _newAttributes[ i ] ) {
  1757. _gl.disableVertexAttribArray( i );
  1758. _enabledAttributes[ i ] = 0;
  1759. }
  1760. }
  1761. }
  1762. function setupMorphTargets ( material, geometryGroup, object ) {
  1763. // set base
  1764. var attributes = material.program.attributes;
  1765. if ( object.morphTargetBase !== - 1 && attributes.position >= 0 ) {
  1766. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  1767. enableAttribute( attributes.position );
  1768. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1769. } else if ( attributes.position >= 0 ) {
  1770. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1771. enableAttribute( attributes.position );
  1772. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1773. }
  1774. if ( object.morphTargetForcedOrder.length ) {
  1775. // set forced order
  1776. var m = 0;
  1777. var order = object.morphTargetForcedOrder;
  1778. var influences = object.morphTargetInfluences;
  1779. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  1780. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  1781. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  1782. enableAttribute( attributes[ 'morphTarget' + m ] );
  1783. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1784. }
  1785. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  1786. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  1787. enableAttribute( attributes[ 'morphNormal' + m ] );
  1788. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1789. }
  1790. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  1791. m ++;
  1792. }
  1793. } else {
  1794. // find the most influencing
  1795. var influence, activeInfluenceIndices = [];
  1796. var influences = object.morphTargetInfluences;
  1797. var i, il = influences.length;
  1798. for ( i = 0; i < il; i ++ ) {
  1799. influence = influences[ i ];
  1800. if ( influence > 0 ) {
  1801. activeInfluenceIndices.push( [ influence, i ] );
  1802. }
  1803. }
  1804. if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
  1805. activeInfluenceIndices.sort( numericalSort );
  1806. activeInfluenceIndices.length = material.numSupportedMorphTargets;
  1807. } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
  1808. activeInfluenceIndices.sort( numericalSort );
  1809. } else if ( activeInfluenceIndices.length === 0 ) {
  1810. activeInfluenceIndices.push( [ 0, 0 ] );
  1811. };
  1812. var influenceIndex, m = 0;
  1813. while ( m < material.numSupportedMorphTargets ) {
  1814. if ( activeInfluenceIndices[ m ] ) {
  1815. influenceIndex = activeInfluenceIndices[ m ][ 1 ];
  1816. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  1817. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
  1818. enableAttribute( attributes[ 'morphTarget' + m ] );
  1819. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1820. }
  1821. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  1822. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
  1823. enableAttribute( attributes[ 'morphNormal' + m ] );
  1824. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1825. }
  1826. object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
  1827. } else {
  1828. /*
  1829. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1830. if ( material.morphNormals ) {
  1831. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1832. }
  1833. */
  1834. object.__webglMorphTargetInfluences[ m ] = 0;
  1835. }
  1836. m ++;
  1837. }
  1838. }
  1839. // load updated influences uniform
  1840. if ( material.program.uniforms.morphTargetInfluences !== null ) {
  1841. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  1842. }
  1843. };
  1844. // Sorting
  1845. function painterSortStable ( a, b ) {
  1846. if ( a.material.id !== b.material.id ) {
  1847. return b.material.id - a.material.id;
  1848. } else if ( a.z !== b.z ) {
  1849. return b.z - a.z;
  1850. } else {
  1851. return a.id - b.id;
  1852. }
  1853. };
  1854. function reversePainterSortStable ( a, b ) {
  1855. if ( a.z !== b.z ) {
  1856. return a.z - b.z;
  1857. } else {
  1858. return a.id - b.id;
  1859. }
  1860. };
  1861. function numericalSort ( a, b ) {
  1862. return b[ 0 ] - a[ 0 ];
  1863. };
  1864. // Rendering
  1865. this.render = function ( scene, camera, renderTarget, forceClear ) {
  1866. if ( camera instanceof THREE.Camera === false ) {
  1867. console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
  1868. return;
  1869. }
  1870. var i, il,
  1871. webglObject, object,
  1872. renderList,
  1873. fog = scene.fog;
  1874. // reset caching for this frame
  1875. _currentMaterialId = - 1;
  1876. _currentCamera = null;
  1877. _lightsNeedUpdate = true;
  1878. // update scene graph
  1879. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  1880. // update camera matrices and frustum
  1881. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  1882. // update Skeleton objects
  1883. scene.traverse( function ( object ) {
  1884. if ( object instanceof THREE.SkinnedMesh ) {
  1885. object.skeleton.update();
  1886. }
  1887. } );
  1888. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1889. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  1890. _frustum.setFromMatrix( _projScreenMatrix );
  1891. lights.length = 0;
  1892. opaqueObjects.length = 0;
  1893. transparentObjects.length = 0;
  1894. projectObject( scene, scene, camera );
  1895. if ( _this.sortObjects === true ) {
  1896. opaqueObjects.sort( painterSortStable );
  1897. transparentObjects.sort( reversePainterSortStable );
  1898. }
  1899. // custom render plugins (pre pass)
  1900. renderPlugins( this.renderPluginsPre, scene, camera );
  1901. //
  1902. _this.info.render.calls = 0;
  1903. _this.info.render.vertices = 0;
  1904. _this.info.render.faces = 0;
  1905. _this.info.render.points = 0;
  1906. this.setRenderTarget( renderTarget );
  1907. if ( this.autoClear || forceClear ) {
  1908. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  1909. }
  1910. // set matrices for immediate objects
  1911. for ( i = 0, il = _webglObjectsImmediate.length; i < il; i ++ ) {
  1912. webglObject = _webglObjectsImmediate[ i ];
  1913. object = webglObject.object;
  1914. if ( object.visible ) {
  1915. setupMatrices( object, camera );
  1916. unrollImmediateBufferMaterial( webglObject );
  1917. }
  1918. }
  1919. if ( scene.overrideMaterial ) {
  1920. var material = scene.overrideMaterial;
  1921. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  1922. this.setDepthTest( material.depthTest );
  1923. this.setDepthWrite( material.depthWrite );
  1924. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  1925. renderObjects( opaqueObjects, camera, lights, fog, true, material );
  1926. renderObjects( transparentObjects, camera, lights, fog, true, material );
  1927. renderObjectsImmediate( _webglObjectsImmediate, '', camera, lights, fog, false, material );
  1928. } else {
  1929. var material = null;
  1930. // opaque pass (front-to-back order)
  1931. this.setBlending( THREE.NoBlending );
  1932. renderObjects( opaqueObjects, camera, lights, fog, false, material );
  1933. renderObjectsImmediate( _webglObjectsImmediate, 'opaque', camera, lights, fog, false, material );
  1934. // transparent pass (back-to-front order)
  1935. renderObjects( transparentObjects, camera, lights, fog, true, material );
  1936. renderObjectsImmediate( _webglObjectsImmediate, 'transparent', camera, lights, fog, true, material );
  1937. }
  1938. // custom render plugins (post pass)
  1939. renderPlugins( this.renderPluginsPost, scene, camera );
  1940. // Generate mipmap if we're using any kind of mipmap filtering
  1941. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  1942. updateRenderTargetMipmap( renderTarget );
  1943. }
  1944. // Ensure depth buffer writing is enabled so it can be cleared on next render
  1945. this.setDepthTest( true );
  1946. this.setDepthWrite( true );
  1947. // _gl.finish();
  1948. };
  1949. function projectObject(scene, object,camera){
  1950. if ( object.visible === false ) return;
  1951. if ( object instanceof THREE.Light ) {
  1952. lights.push( object );
  1953. }
  1954. if ( object instanceof THREE.Scene ) {
  1955. //
  1956. } else {
  1957. initObject( object, scene );
  1958. var webglObjects = _webglObjects[ object.id ];
  1959. if ( webglObjects && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
  1960. updateObject( object, scene );
  1961. for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
  1962. var webglObject = webglObjects[i];
  1963. unrollBufferMaterial( webglObject );
  1964. webglObject.render = true;
  1965. if ( _this.sortObjects === true ) {
  1966. if ( object.renderDepth !== null ) {
  1967. webglObject.z = object.renderDepth;
  1968. } else {
  1969. _vector3.setFromMatrixPosition( object.matrixWorld );
  1970. _vector3.applyProjection( _projScreenMatrix );
  1971. webglObject.z = _vector3.z;
  1972. }
  1973. }
  1974. }
  1975. }
  1976. }
  1977. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  1978. projectObject( scene, object.children[ i ], camera );
  1979. }
  1980. }
  1981. function renderPlugins( plugins, scene, camera ) {
  1982. if ( plugins.length === 0 ) return;
  1983. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  1984. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  1985. }
  1986. };
  1987. function renderObjects( renderList, camera, lights, fog, useBlending, overrideMaterial ) {
  1988. var webglObject, object, buffer, material;
  1989. for ( var i = renderList.length - 1; i !== - 1; i -- ) {
  1990. webglObject = renderList[ i ];
  1991. object = webglObject.object;
  1992. buffer = webglObject.buffer;
  1993. setupMatrices( object, camera );
  1994. if ( overrideMaterial ) {
  1995. material = overrideMaterial;
  1996. } else {
  1997. material = webglObject.material;
  1998. if ( ! material ) continue;
  1999. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2000. _this.setDepthTest( material.depthTest );
  2001. _this.setDepthWrite( material.depthWrite );
  2002. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2003. }
  2004. _this.setMaterialFaces( material );
  2005. if ( buffer instanceof THREE.BufferGeometry ) {
  2006. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  2007. } else {
  2008. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  2009. }
  2010. }
  2011. };
  2012. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  2013. var webglObject, object, material, program;
  2014. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  2015. webglObject = renderList[ i ];
  2016. object = webglObject.object;
  2017. if ( object.visible ) {
  2018. if ( overrideMaterial ) {
  2019. material = overrideMaterial;
  2020. } else {
  2021. material = webglObject[ materialType ];
  2022. if ( ! material ) continue;
  2023. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2024. _this.setDepthTest( material.depthTest );
  2025. _this.setDepthWrite( material.depthWrite );
  2026. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2027. }
  2028. _this.renderImmediateObject( camera, lights, fog, material, object );
  2029. }
  2030. }
  2031. };
  2032. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  2033. var program = setProgram( camera, lights, fog, material, object );
  2034. _currentGeometryGroupHash = - 1;
  2035. _this.setMaterialFaces( material );
  2036. if ( object.immediateRenderCallback ) {
  2037. object.immediateRenderCallback( program, _gl, _frustum );
  2038. } else {
  2039. object.render( function ( object ) { _this.renderBufferImmediate( object, program, material ); } );
  2040. }
  2041. };
  2042. function unrollImmediateBufferMaterial ( globject ) {
  2043. var object = globject.object,
  2044. material = object.material;
  2045. if ( material.transparent ) {
  2046. globject.transparent = material;
  2047. globject.opaque = null;
  2048. } else {
  2049. globject.opaque = material;
  2050. globject.transparent = null;
  2051. }
  2052. };
  2053. function unrollBufferMaterial ( globject ) {
  2054. var object = globject.object;
  2055. var buffer = globject.buffer;
  2056. var geometry = object.geometry;
  2057. var material = object.material;
  2058. if ( material instanceof THREE.MeshFaceMaterial ) {
  2059. var materialIndex = geometry instanceof THREE.BufferGeometry ? 0 : buffer.materialIndex;
  2060. material = material.materials[ materialIndex ];
  2061. globject.material = material;
  2062. if ( material.transparent ) {
  2063. transparentObjects.push( globject );
  2064. } else {
  2065. opaqueObjects.push( globject );
  2066. }
  2067. } else if ( material ) {
  2068. globject.material = material;
  2069. if ( material.transparent ) {
  2070. transparentObjects.push( globject );
  2071. } else {
  2072. opaqueObjects.push( globject );
  2073. }
  2074. }
  2075. };
  2076. function initObject( object, scene ) {
  2077. if ( object.__webglInit === undefined ) {
  2078. object.__webglInit = true;
  2079. object._modelViewMatrix = new THREE.Matrix4();
  2080. object._normalMatrix = new THREE.Matrix3();
  2081. object.addEventListener( 'removed', onObjectRemoved );
  2082. }
  2083. var geometry = object.geometry;
  2084. if ( geometry === undefined ) {
  2085. // ImmediateRenderObject
  2086. } else if ( geometry.__webglInit === undefined ) {
  2087. geometry.__webglInit = true;
  2088. geometry.addEventListener( 'dispose', onGeometryDispose );
  2089. if ( geometry instanceof THREE.BufferGeometry ) {
  2090. //
  2091. } else if ( object instanceof THREE.Mesh ) {
  2092. initGeometryGroups(scene, object, geometry);
  2093. } else if ( object instanceof THREE.Line ) {
  2094. if ( ! geometry.__webglVertexBuffer ) {
  2095. createLineBuffers( geometry );
  2096. initLineBuffers( geometry, object );
  2097. geometry.verticesNeedUpdate = true;
  2098. geometry.colorsNeedUpdate = true;
  2099. geometry.lineDistancesNeedUpdate = true;
  2100. }
  2101. } else if ( object instanceof THREE.PointCloud ) {
  2102. if ( ! geometry.__webglVertexBuffer ) {
  2103. createParticleBuffers( geometry );
  2104. initParticleBuffers( geometry, object );
  2105. geometry.verticesNeedUpdate = true;
  2106. geometry.colorsNeedUpdate = true;
  2107. }
  2108. }
  2109. }
  2110. if ( object.__webglActive === undefined) {
  2111. if ( object instanceof THREE.Mesh ) {
  2112. if ( geometry instanceof THREE.BufferGeometry ) {
  2113. addBuffer( _webglObjects, geometry, object );
  2114. } else if ( geometry instanceof THREE.Geometry ) {
  2115. for ( var i = 0,l = geometry.geometryGroupsList.length; i<l;i++ ) {
  2116. var geometryGroup = geometry.geometryGroupsList[ i ];
  2117. addBuffer( _webglObjects, geometryGroup, object );
  2118. }
  2119. }
  2120. } else if ( object instanceof THREE.Line ||
  2121. object instanceof THREE.PointCloud ) {
  2122. addBuffer( _webglObjects, geometry, object );
  2123. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  2124. addBufferImmediate( _webglObjectsImmediate, object );
  2125. }
  2126. object.__webglActive = true;
  2127. }
  2128. };
  2129. function initGeometryGroups( scene, object, geometry ) {
  2130. var g, geometryGroup, material,addBuffers = false;
  2131. material = object.material;
  2132. if ( geometry.geometryGroups === undefined || geometry.groupsNeedUpdate ) {
  2133. delete _webglObjects[object.id];
  2134. geometry.makeGroups( material instanceof THREE.MeshFaceMaterial, _glExtensionElementIndexUint ? 4294967296 : 65535 );
  2135. geometry.groupsNeedUpdate = false;
  2136. }
  2137. // create separate VBOs per geometry chunk
  2138. for ( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  2139. geometryGroup = geometry.geometryGroupsList[ i ];
  2140. // initialise VBO on the first access
  2141. if ( ! geometryGroup.__webglVertexBuffer ) {
  2142. createMeshBuffers( geometryGroup );
  2143. initMeshBuffers( geometryGroup, object );
  2144. geometry.verticesNeedUpdate = true;
  2145. geometry.morphTargetsNeedUpdate = true;
  2146. geometry.elementsNeedUpdate = true;
  2147. geometry.uvsNeedUpdate = true;
  2148. geometry.normalsNeedUpdate = true;
  2149. geometry.tangentsNeedUpdate = true;
  2150. geometry.colorsNeedUpdate = true;
  2151. addBuffers = true;
  2152. } else {
  2153. addBuffers = false;
  2154. }
  2155. if ( addBuffers || object.__webglActive === undefined ) {
  2156. addBuffer( _webglObjects, geometryGroup, object );
  2157. }
  2158. }
  2159. object.__webglActive = true;
  2160. }
  2161. function addBuffer( objlist, buffer, object ) {
  2162. var id = object.id;
  2163. objlist[id] = objlist[id] || [];
  2164. objlist[id].push(
  2165. {
  2166. id: id,
  2167. buffer: buffer,
  2168. object: object,
  2169. material: null,
  2170. z: 0
  2171. }
  2172. );
  2173. };
  2174. function addBufferImmediate( objlist, object ) {
  2175. objlist.push(
  2176. {
  2177. id: null,
  2178. object: object,
  2179. opaque: null,
  2180. transparent: null,
  2181. z: 0
  2182. }
  2183. );
  2184. };
  2185. // Objects updates
  2186. function updateObject( object, scene ) {
  2187. var geometry = object.geometry,
  2188. geometryGroup, customAttributesDirty, material;
  2189. if ( geometry instanceof THREE.BufferGeometry ) {
  2190. setDirectBuffers( geometry );
  2191. } else if ( object instanceof THREE.Mesh ) {
  2192. // check all geometry groups
  2193. if ( geometry.buffersNeedUpdate || geometry.groupsNeedUpdate ) {
  2194. initGeometryGroups(scene, object,geometry);
  2195. }
  2196. for ( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  2197. geometryGroup = geometry.geometryGroupsList[ i ];
  2198. material = getBufferMaterial( object, geometryGroup );
  2199. if ( geometry.buffersNeedUpdate || geometry.groupsNeedUpdate) {
  2200. initMeshBuffers( geometryGroup, object );
  2201. }
  2202. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2203. if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
  2204. geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
  2205. geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
  2206. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, ! geometry.dynamic, material );
  2207. }
  2208. }
  2209. geometry.verticesNeedUpdate = false;
  2210. geometry.morphTargetsNeedUpdate = false;
  2211. geometry.elementsNeedUpdate = false;
  2212. geometry.uvsNeedUpdate = false;
  2213. geometry.normalsNeedUpdate = false;
  2214. geometry.colorsNeedUpdate = false;
  2215. geometry.tangentsNeedUpdate = false;
  2216. geometry.buffersNeedUpdate = false;
  2217. material.attributes && clearCustomAttributes( material );
  2218. } else if ( object instanceof THREE.Line ) {
  2219. material = getBufferMaterial( object, geometry );
  2220. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2221. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) {
  2222. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  2223. }
  2224. geometry.verticesNeedUpdate = false;
  2225. geometry.colorsNeedUpdate = false;
  2226. geometry.lineDistancesNeedUpdate = false;
  2227. material.attributes && clearCustomAttributes( material );
  2228. } else if ( object instanceof THREE.PointCloud ) {
  2229. material = getBufferMaterial( object, geometry );
  2230. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2231. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || object.sortParticles || customAttributesDirty ) {
  2232. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  2233. }
  2234. geometry.verticesNeedUpdate = false;
  2235. geometry.colorsNeedUpdate = false;
  2236. material.attributes && clearCustomAttributes( material );
  2237. }
  2238. };
  2239. // Objects updates - custom attributes check
  2240. function areCustomAttributesDirty( material ) {
  2241. for ( var name in material.attributes ) {
  2242. if ( material.attributes[ name ].needsUpdate ) return true;
  2243. }
  2244. return false;
  2245. };
  2246. function clearCustomAttributes( material ) {
  2247. for ( var name in material.attributes ) {
  2248. material.attributes[ name ].needsUpdate = false;
  2249. }
  2250. };
  2251. // Objects removal
  2252. function removeObject( object ) {
  2253. if ( object instanceof THREE.Mesh ||
  2254. object instanceof THREE.PointCloud ||
  2255. object instanceof THREE.Line ) {
  2256. delete _webglObjects[ object.id ];
  2257. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  2258. removeInstances( _webglObjectsImmediate, object );
  2259. }
  2260. delete object.__webglInit;
  2261. delete object._modelViewMatrix;
  2262. delete object._normalMatrix;
  2263. delete object.__webglActive;
  2264. };
  2265. function removeInstances( objlist, object ) {
  2266. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  2267. if ( objlist[ o ].object === object ) {
  2268. objlist.splice( o, 1 );
  2269. }
  2270. }
  2271. };
  2272. // Materials
  2273. this.initMaterial = function () {
  2274. console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
  2275. };
  2276. function initMaterial( material, lights, fog, object ) {
  2277. material.addEventListener( 'dispose', onMaterialDispose );
  2278. var shaderID;
  2279. if ( material instanceof THREE.MeshDepthMaterial ) {
  2280. shaderID = 'depth';
  2281. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  2282. shaderID = 'normal';
  2283. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  2284. shaderID = 'basic';
  2285. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  2286. shaderID = 'lambert';
  2287. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  2288. shaderID = 'phong';
  2289. } else if ( material instanceof THREE.LineBasicMaterial ) {
  2290. shaderID = 'basic';
  2291. } else if ( material instanceof THREE.LineDashedMaterial ) {
  2292. shaderID = 'dashed';
  2293. } else if ( material instanceof THREE.PointCloudMaterial ) {
  2294. shaderID = 'particle_basic';
  2295. }
  2296. if ( shaderID ) {
  2297. var shader = THREE.ShaderLib[ shaderID ];
  2298. material.__webglShader = {
  2299. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  2300. vertexShader: shader.vertexShader,
  2301. fragmentShader: shader.fragmentShader
  2302. }
  2303. } else {
  2304. material.__webglShader = {
  2305. uniforms: material.uniforms,
  2306. vertexShader: material.vertexShader,
  2307. fragmentShader: material.fragmentShader
  2308. }
  2309. }
  2310. // heuristics to create shader parameters according to lights in the scene
  2311. // (not to blow over maxLights budget)
  2312. var maxLightCount = allocateLights( lights );
  2313. var maxShadows = allocateShadows( lights );
  2314. var maxBones = allocateBones( object );
  2315. var parameters = {
  2316. precision: _precision,
  2317. supportsVertexTextures: _supportsVertexTextures,
  2318. map: !! material.map,
  2319. envMap: !! material.envMap,
  2320. lightMap: !! material.lightMap,
  2321. bumpMap: !! material.bumpMap,
  2322. normalMap: !! material.normalMap,
  2323. specularMap: !! material.specularMap,
  2324. alphaMap: !! material.alphaMap,
  2325. vertexColors: material.vertexColors,
  2326. fog: fog,
  2327. useFog: material.fog,
  2328. fogExp: fog instanceof THREE.FogExp2,
  2329. sizeAttenuation: material.sizeAttenuation,
  2330. logarithmicDepthBuffer: _logarithmicDepthBuffer,
  2331. skinning: material.skinning,
  2332. maxBones: maxBones,
  2333. useVertexTexture: _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture,
  2334. morphTargets: material.morphTargets,
  2335. morphNormals: material.morphNormals,
  2336. maxMorphTargets: _this.maxMorphTargets,
  2337. maxMorphNormals: _this.maxMorphNormals,
  2338. maxDirLights: maxLightCount.directional,
  2339. maxPointLights: maxLightCount.point,
  2340. maxSpotLights: maxLightCount.spot,
  2341. maxHemiLights: maxLightCount.hemi,
  2342. maxShadows: maxShadows,
  2343. shadowMapEnabled: _this.shadowMapEnabled && object.receiveShadow && maxShadows > 0,
  2344. shadowMapType: _this.shadowMapType,
  2345. shadowMapDebug: _this.shadowMapDebug,
  2346. shadowMapCascade: _this.shadowMapCascade,
  2347. alphaTest: material.alphaTest,
  2348. metal: material.metal,
  2349. wrapAround: material.wrapAround,
  2350. doubleSided: material.side === THREE.DoubleSide,
  2351. flipSided: material.side === THREE.BackSide
  2352. };
  2353. // Generate code
  2354. var chunks = [];
  2355. if ( shaderID ) {
  2356. chunks.push( shaderID );
  2357. } else {
  2358. chunks.push( material.fragmentShader );
  2359. chunks.push( material.vertexShader );
  2360. }
  2361. if ( material.defines !== undefined ) {
  2362. for ( var name in material.defines ) {
  2363. chunks.push( name );
  2364. chunks.push( material.defines[ name ] );
  2365. }
  2366. }
  2367. for ( var name in parameters ) {
  2368. chunks.push( name );
  2369. chunks.push( parameters[ name ] );
  2370. }
  2371. var code = chunks.join();
  2372. var program;
  2373. // Check if code has been already compiled
  2374. for ( var p = 0, pl = _programs.length; p < pl; p ++ ) {
  2375. var programInfo = _programs[ p ];
  2376. if ( programInfo.code === code ) {
  2377. program = programInfo;
  2378. program.usedTimes ++;
  2379. break;
  2380. }
  2381. }
  2382. if ( program === undefined ) {
  2383. program = new THREE.WebGLProgram( _this, code, material, parameters );
  2384. _programs.push( program );
  2385. _this.info.memory.programs = _programs.length;
  2386. }
  2387. material.program = program;
  2388. var attributes = program.attributes;
  2389. if ( material.morphTargets ) {
  2390. material.numSupportedMorphTargets = 0;
  2391. var id, base = 'morphTarget';
  2392. for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
  2393. id = base + i;
  2394. if ( attributes[ id ] >= 0 ) {
  2395. material.numSupportedMorphTargets ++;
  2396. }
  2397. }
  2398. }
  2399. if ( material.morphNormals ) {
  2400. material.numSupportedMorphNormals = 0;
  2401. var id, base = 'morphNormal';
  2402. for ( i = 0; i < _this.maxMorphNormals; i ++ ) {
  2403. id = base + i;
  2404. if ( attributes[ id ] >= 0 ) {
  2405. material.numSupportedMorphNormals ++;
  2406. }
  2407. }
  2408. }
  2409. material.uniformsList = [];
  2410. for ( var u in material.__webglShader.uniforms ) {
  2411. var location = material.program.uniforms[ u ];
  2412. if ( location ) {
  2413. material.uniformsList.push( [ material.__webglShader.uniforms[ u ], location ] );
  2414. }
  2415. }
  2416. };
  2417. function setProgram( camera, lights, fog, material, object ) {
  2418. _usedTextureUnits = 0;
  2419. if ( material.needsUpdate ) {
  2420. if ( material.program ) deallocateMaterial( material );
  2421. initMaterial( material, lights, fog, object );
  2422. material.needsUpdate = false;
  2423. }
  2424. if ( material.morphTargets ) {
  2425. if ( ! object.__webglMorphTargetInfluences ) {
  2426. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  2427. }
  2428. }
  2429. var refreshProgram = false;
  2430. var refreshMaterial = false;
  2431. var refreshLights = false;
  2432. var program = material.program,
  2433. p_uniforms = program.uniforms,
  2434. m_uniforms = material.__webglShader.uniforms;
  2435. if ( program.id !== _currentProgram ) {
  2436. _gl.useProgram( program.program );
  2437. _currentProgram = program.id;
  2438. refreshProgram = true;
  2439. refreshMaterial = true;
  2440. refreshLights = true;
  2441. }
  2442. if ( material.id !== _currentMaterialId ) {
  2443. if ( _currentMaterialId === -1 ) refreshLights = true;
  2444. _currentMaterialId = material.id;
  2445. refreshMaterial = true;
  2446. }
  2447. if ( refreshProgram || camera !== _currentCamera ) {
  2448. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
  2449. if ( _logarithmicDepthBuffer ) {
  2450. _gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
  2451. }
  2452. if ( camera !== _currentCamera ) _currentCamera = camera;
  2453. // load material specific uniforms
  2454. // (shader material also gets them for the sake of genericity)
  2455. if ( material instanceof THREE.ShaderMaterial ||
  2456. material instanceof THREE.MeshPhongMaterial ||
  2457. material.envMap ) {
  2458. if ( p_uniforms.cameraPosition !== null ) {
  2459. _vector3.setFromMatrixPosition( camera.matrixWorld );
  2460. _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
  2461. }
  2462. }
  2463. if ( material instanceof THREE.MeshPhongMaterial ||
  2464. material instanceof THREE.MeshLambertMaterial ||
  2465. material instanceof THREE.ShaderMaterial ||
  2466. material.skinning ) {
  2467. if ( p_uniforms.viewMatrix !== null ) {
  2468. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
  2469. }
  2470. }
  2471. }
  2472. // skinning uniforms must be set even if material didn't change
  2473. // auto-setting of texture unit for bone texture must go before other textures
  2474. // not sure why, but otherwise weird things happen
  2475. if ( material.skinning ) {
  2476. if ( object.bindMatrix && p_uniforms.bindMatrix !== null ) {
  2477. _gl.uniformMatrix4fv( p_uniforms.bindMatrix, false, object.bindMatrix.elements );
  2478. }
  2479. if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== null ) {
  2480. _gl.uniformMatrix4fv( p_uniforms.bindMatrixInverse, false, object.bindMatrixInverse.elements );
  2481. }
  2482. if ( _supportsBoneTextures && object.skeleton && object.skeleton.useVertexTexture ) {
  2483. if ( p_uniforms.boneTexture !== null ) {
  2484. var textureUnit = getTextureUnit();
  2485. _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
  2486. _this.setTexture( object.skeleton.boneTexture, textureUnit );
  2487. }
  2488. if ( p_uniforms.boneTextureWidth !== null ) {
  2489. _gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
  2490. }
  2491. if ( p_uniforms.boneTextureHeight !== null ) {
  2492. _gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
  2493. }
  2494. } else if ( object.skeleton && object.skeleton.boneMatrices ) {
  2495. if ( p_uniforms.boneGlobalMatrices !== null ) {
  2496. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
  2497. }
  2498. }
  2499. }
  2500. if ( refreshMaterial ) {
  2501. // refresh uniforms common to several materials
  2502. if ( fog && material.fog ) {
  2503. refreshUniformsFog( m_uniforms, fog );
  2504. }
  2505. if ( material instanceof THREE.MeshPhongMaterial ||
  2506. material instanceof THREE.MeshLambertMaterial ||
  2507. material.lights ) {
  2508. if ( _lightsNeedUpdate ) {
  2509. refreshLights = true;
  2510. setupLights( lights );
  2511. _lightsNeedUpdate = false;
  2512. }
  2513. if ( refreshLights ) {
  2514. refreshUniformsLights( m_uniforms, _lights );
  2515. markUniformsLightsNeedsUpdate( m_uniforms, true );
  2516. } else {
  2517. markUniformsLightsNeedsUpdate( m_uniforms, false );
  2518. }
  2519. }
  2520. if ( material instanceof THREE.MeshBasicMaterial ||
  2521. material instanceof THREE.MeshLambertMaterial ||
  2522. material instanceof THREE.MeshPhongMaterial ) {
  2523. refreshUniformsCommon( m_uniforms, material );
  2524. }
  2525. // refresh single material specific uniforms
  2526. if ( material instanceof THREE.LineBasicMaterial ) {
  2527. refreshUniformsLine( m_uniforms, material );
  2528. } else if ( material instanceof THREE.LineDashedMaterial ) {
  2529. refreshUniformsLine( m_uniforms, material );
  2530. refreshUniformsDash( m_uniforms, material );
  2531. } else if ( material instanceof THREE.PointCloudMaterial ) {
  2532. refreshUniformsParticle( m_uniforms, material );
  2533. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  2534. refreshUniformsPhong( m_uniforms, material );
  2535. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  2536. refreshUniformsLambert( m_uniforms, material );
  2537. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  2538. m_uniforms.mNear.value = camera.near;
  2539. m_uniforms.mFar.value = camera.far;
  2540. m_uniforms.opacity.value = material.opacity;
  2541. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  2542. m_uniforms.opacity.value = material.opacity;
  2543. }
  2544. if ( object.receiveShadow && ! material._shadowPass ) {
  2545. refreshUniformsShadow( m_uniforms, lights );
  2546. }
  2547. // load common uniforms
  2548. loadUniformsGeneric( material.uniformsList );
  2549. }
  2550. loadUniformsMatrices( p_uniforms, object );
  2551. if ( p_uniforms.modelMatrix !== null ) {
  2552. _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
  2553. }
  2554. return program;
  2555. };
  2556. // Uniforms (refresh uniforms objects)
  2557. function refreshUniformsCommon ( uniforms, material ) {
  2558. uniforms.opacity.value = material.opacity;
  2559. if ( _this.gammaInput ) {
  2560. uniforms.diffuse.value.copyGammaToLinear( material.color );
  2561. } else {
  2562. uniforms.diffuse.value = material.color;
  2563. }
  2564. uniforms.map.value = material.map;
  2565. uniforms.lightMap.value = material.lightMap;
  2566. uniforms.specularMap.value = material.specularMap;
  2567. uniforms.alphaMap.value = material.alphaMap;
  2568. if ( material.bumpMap ) {
  2569. uniforms.bumpMap.value = material.bumpMap;
  2570. uniforms.bumpScale.value = material.bumpScale;
  2571. }
  2572. if ( material.normalMap ) {
  2573. uniforms.normalMap.value = material.normalMap;
  2574. uniforms.normalScale.value.copy( material.normalScale );
  2575. }
  2576. // uv repeat and offset setting priorities
  2577. // 1. color map
  2578. // 2. specular map
  2579. // 3. normal map
  2580. // 4. bump map
  2581. // 5. alpha map
  2582. var uvScaleMap;
  2583. if ( material.map ) {
  2584. uvScaleMap = material.map;
  2585. } else if ( material.specularMap ) {
  2586. uvScaleMap = material.specularMap;
  2587. } else if ( material.normalMap ) {
  2588. uvScaleMap = material.normalMap;
  2589. } else if ( material.bumpMap ) {
  2590. uvScaleMap = material.bumpMap;
  2591. } else if ( material.alphaMap ) {
  2592. uvScaleMap = material.alphaMap;
  2593. }
  2594. if ( uvScaleMap !== undefined ) {
  2595. var offset = uvScaleMap.offset;
  2596. var repeat = uvScaleMap.repeat;
  2597. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  2598. }
  2599. uniforms.envMap.value = material.envMap;
  2600. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
  2601. if ( _this.gammaInput ) {
  2602. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  2603. uniforms.reflectivity.value = material.reflectivity;
  2604. } else {
  2605. uniforms.reflectivity.value = material.reflectivity;
  2606. }
  2607. uniforms.refractionRatio.value = material.refractionRatio;
  2608. uniforms.combine.value = material.combine;
  2609. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  2610. };
  2611. function refreshUniformsLine ( uniforms, material ) {
  2612. uniforms.diffuse.value = material.color;
  2613. uniforms.opacity.value = material.opacity;
  2614. };
  2615. function refreshUniformsDash ( uniforms, material ) {
  2616. uniforms.dashSize.value = material.dashSize;
  2617. uniforms.totalSize.value = material.dashSize + material.gapSize;
  2618. uniforms.scale.value = material.scale;
  2619. };
  2620. function refreshUniformsParticle ( uniforms, material ) {
  2621. uniforms.psColor.value = material.color;
  2622. uniforms.opacity.value = material.opacity;
  2623. uniforms.size.value = material.size;
  2624. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  2625. uniforms.map.value = material.map;
  2626. };
  2627. function refreshUniformsFog ( uniforms, fog ) {
  2628. uniforms.fogColor.value = fog.color;
  2629. if ( fog instanceof THREE.Fog ) {
  2630. uniforms.fogNear.value = fog.near;
  2631. uniforms.fogFar.value = fog.far;
  2632. } else if ( fog instanceof THREE.FogExp2 ) {
  2633. uniforms.fogDensity.value = fog.density;
  2634. }
  2635. };
  2636. function refreshUniformsPhong ( uniforms, material ) {
  2637. uniforms.shininess.value = material.shininess;
  2638. if ( _this.gammaInput ) {
  2639. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  2640. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  2641. uniforms.specular.value.copyGammaToLinear( material.specular );
  2642. } else {
  2643. uniforms.ambient.value = material.ambient;
  2644. uniforms.emissive.value = material.emissive;
  2645. uniforms.specular.value = material.specular;
  2646. }
  2647. if ( material.wrapAround ) {
  2648. uniforms.wrapRGB.value.copy( material.wrapRGB );
  2649. }
  2650. };
  2651. function refreshUniformsLambert ( uniforms, material ) {
  2652. if ( _this.gammaInput ) {
  2653. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  2654. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  2655. } else {
  2656. uniforms.ambient.value = material.ambient;
  2657. uniforms.emissive.value = material.emissive;
  2658. }
  2659. if ( material.wrapAround ) {
  2660. uniforms.wrapRGB.value.copy( material.wrapRGB );
  2661. }
  2662. };
  2663. function refreshUniformsLights ( uniforms, lights ) {
  2664. uniforms.ambientLightColor.value = lights.ambient;
  2665. uniforms.directionalLightColor.value = lights.directional.colors;
  2666. uniforms.directionalLightDirection.value = lights.directional.positions;
  2667. uniforms.pointLightColor.value = lights.point.colors;
  2668. uniforms.pointLightPosition.value = lights.point.positions;
  2669. uniforms.pointLightDistance.value = lights.point.distances;
  2670. uniforms.spotLightColor.value = lights.spot.colors;
  2671. uniforms.spotLightPosition.value = lights.spot.positions;
  2672. uniforms.spotLightDistance.value = lights.spot.distances;
  2673. uniforms.spotLightDirection.value = lights.spot.directions;
  2674. uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
  2675. uniforms.spotLightExponent.value = lights.spot.exponents;
  2676. uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
  2677. uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
  2678. uniforms.hemisphereLightDirection.value = lights.hemi.positions;
  2679. };
  2680. // If uniforms are marked as clean, they don't need to be loaded to the GPU.
  2681. function markUniformsLightsNeedsUpdate ( uniforms, boolean ) {
  2682. uniforms.ambientLightColor.needsUpdate = boolean;
  2683. uniforms.directionalLightColor.needsUpdate = boolean;
  2684. uniforms.directionalLightDirection.needsUpdate = boolean;
  2685. uniforms.pointLightColor.needsUpdate = boolean;
  2686. uniforms.pointLightPosition.needsUpdate = boolean;
  2687. uniforms.pointLightDistance.needsUpdate = boolean;
  2688. uniforms.spotLightColor.needsUpdate = boolean;
  2689. uniforms.spotLightPosition.needsUpdate = boolean;
  2690. uniforms.spotLightDistance.needsUpdate = boolean;
  2691. uniforms.spotLightDirection.needsUpdate = boolean;
  2692. uniforms.spotLightAngleCos.needsUpdate = boolean;
  2693. uniforms.spotLightExponent.needsUpdate = boolean;
  2694. uniforms.hemisphereLightSkyColor.needsUpdate = boolean;
  2695. uniforms.hemisphereLightGroundColor.needsUpdate = boolean;
  2696. uniforms.hemisphereLightDirection.needsUpdate = boolean;
  2697. };
  2698. function refreshUniformsShadow ( uniforms, lights ) {
  2699. if ( uniforms.shadowMatrix ) {
  2700. var j = 0;
  2701. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  2702. var light = lights[ i ];
  2703. if ( ! light.castShadow ) continue;
  2704. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  2705. uniforms.shadowMap.value[ j ] = light.shadowMap;
  2706. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  2707. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  2708. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  2709. uniforms.shadowBias.value[ j ] = light.shadowBias;
  2710. j ++;
  2711. }
  2712. }
  2713. }
  2714. };
  2715. // Uniforms (load to GPU)
  2716. function loadUniformsMatrices ( uniforms, object ) {
  2717. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  2718. if ( uniforms.normalMatrix ) {
  2719. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
  2720. }
  2721. };
  2722. function getTextureUnit() {
  2723. var textureUnit = _usedTextureUnits;
  2724. if ( textureUnit >= _maxTextures ) {
  2725. console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + _maxTextures );
  2726. }
  2727. _usedTextureUnits += 1;
  2728. return textureUnit;
  2729. };
  2730. function loadUniformsGeneric ( uniforms ) {
  2731. var texture, textureUnit, offset;
  2732. for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {
  2733. var uniform = uniforms[ j ][ 0 ];
  2734. // needsUpdate property is not added to all uniforms.
  2735. if ( uniform.needsUpdate === false ) continue;
  2736. var type = uniform.type;
  2737. var value = uniform.value;
  2738. var location = uniforms[ j ][ 1 ];
  2739. switch ( type ) {
  2740. case '1i':
  2741. _gl.uniform1i( location, value );
  2742. break;
  2743. case '1f':
  2744. _gl.uniform1f( location, value );
  2745. break;
  2746. case '2f':
  2747. _gl.uniform2f( location, value[ 0 ], value[ 1 ] );
  2748. break;
  2749. case '3f':
  2750. _gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
  2751. break;
  2752. case '4f':
  2753. _gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
  2754. break;
  2755. case '1iv':
  2756. _gl.uniform1iv( location, value );
  2757. break;
  2758. case '3iv':
  2759. _gl.uniform3iv( location, value );
  2760. break;
  2761. case '1fv':
  2762. _gl.uniform1fv( location, value );
  2763. break;
  2764. case '2fv':
  2765. _gl.uniform2fv( location, value );
  2766. break;
  2767. case '3fv':
  2768. _gl.uniform3fv( location, value );
  2769. break;
  2770. case '4fv':
  2771. _gl.uniform4fv( location, value );
  2772. break;
  2773. case 'Matrix3fv':
  2774. _gl.uniformMatrix3fv( location, false, value );
  2775. break;
  2776. case 'Matrix4fv':
  2777. _gl.uniformMatrix4fv( location, false, value );
  2778. break;
  2779. //
  2780. case 'i':
  2781. // single integer
  2782. _gl.uniform1i( location, value );
  2783. break;
  2784. case 'f':
  2785. // single float
  2786. _gl.uniform1f( location, value );
  2787. break;
  2788. case 'v2':
  2789. // single THREE.Vector2
  2790. _gl.uniform2f( location, value.x, value.y );
  2791. break;
  2792. case 'v3':
  2793. // single THREE.Vector3
  2794. _gl.uniform3f( location, value.x, value.y, value.z );
  2795. break;
  2796. case 'v4':
  2797. // single THREE.Vector4
  2798. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  2799. break;
  2800. case 'c':
  2801. // single THREE.Color
  2802. _gl.uniform3f( location, value.r, value.g, value.b );
  2803. break;
  2804. case 'iv1':
  2805. // flat array of integers (JS or typed array)
  2806. _gl.uniform1iv( location, value );
  2807. break;
  2808. case 'iv':
  2809. // flat array of integers with 3 x N size (JS or typed array)
  2810. _gl.uniform3iv( location, value );
  2811. break;
  2812. case 'fv1':
  2813. // flat array of floats (JS or typed array)
  2814. _gl.uniform1fv( location, value );
  2815. break;
  2816. case 'fv':
  2817. // flat array of floats with 3 x N size (JS or typed array)
  2818. _gl.uniform3fv( location, value );
  2819. break;
  2820. case 'v2v':
  2821. // array of THREE.Vector2
  2822. if ( uniform._array === undefined ) {
  2823. uniform._array = new Float32Array( 2 * value.length );
  2824. }
  2825. for ( var i = 0, il = value.length; i < il; i ++ ) {
  2826. offset = i * 2;
  2827. uniform._array[ offset ] = value[ i ].x;
  2828. uniform._array[ offset + 1 ] = value[ i ].y;
  2829. }
  2830. _gl.uniform2fv( location, uniform._array );
  2831. break;
  2832. case 'v3v':
  2833. // array of THREE.Vector3
  2834. if ( uniform._array === undefined ) {
  2835. uniform._array = new Float32Array( 3 * value.length );
  2836. }
  2837. for ( var i = 0, il = value.length; i < il; i ++ ) {
  2838. offset = i * 3;
  2839. uniform._array[ offset ] = value[ i ].x;
  2840. uniform._array[ offset + 1 ] = value[ i ].y;
  2841. uniform._array[ offset + 2 ] = value[ i ].z;
  2842. }
  2843. _gl.uniform3fv( location, uniform._array );
  2844. break;
  2845. case 'v4v':
  2846. // array of THREE.Vector4
  2847. if ( uniform._array === undefined ) {
  2848. uniform._array = new Float32Array( 4 * value.length );
  2849. }
  2850. for ( var i = 0, il = value.length; i < il; i ++ ) {
  2851. offset = i * 4;
  2852. uniform._array[ offset ] = value[ i ].x;
  2853. uniform._array[ offset + 1 ] = value[ i ].y;
  2854. uniform._array[ offset + 2 ] = value[ i ].z;
  2855. uniform._array[ offset + 3 ] = value[ i ].w;
  2856. }
  2857. _gl.uniform4fv( location, uniform._array );
  2858. break;
  2859. case 'm3':
  2860. // single THREE.Matrix3
  2861. _gl.uniformMatrix3fv( location, false, value.elements );
  2862. break;
  2863. case 'm3v':
  2864. // array of THREE.Matrix3
  2865. if ( uniform._array === undefined ) {
  2866. uniform._array = new Float32Array( 9 * value.length );
  2867. }
  2868. for ( var i = 0, il = value.length; i < il; i ++ ) {
  2869. value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
  2870. }
  2871. _gl.uniformMatrix3fv( location, false, uniform._array );
  2872. break;
  2873. case 'm4':
  2874. // single THREE.Matrix4
  2875. _gl.uniformMatrix4fv( location, false, value.elements );
  2876. break;
  2877. case 'm4v':
  2878. // array of THREE.Matrix4
  2879. if ( uniform._array === undefined ) {
  2880. uniform._array = new Float32Array( 16 * value.length );
  2881. }
  2882. for ( var i = 0, il = value.length; i < il; i ++ ) {
  2883. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  2884. }
  2885. _gl.uniformMatrix4fv( location, false, uniform._array );
  2886. break;
  2887. case 't':
  2888. // single THREE.Texture (2d or cube)
  2889. texture = value;
  2890. textureUnit = getTextureUnit();
  2891. _gl.uniform1i( location, textureUnit );
  2892. if ( ! texture ) continue;
  2893. if ( texture instanceof THREE.CubeTexture ||
  2894. ( texture.image instanceof Array && texture.image.length === 6 ) ) { // CompressedTexture can have Array in image :/
  2895. setCubeTexture( texture, textureUnit );
  2896. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  2897. setCubeTextureDynamic( texture, textureUnit );
  2898. } else {
  2899. _this.setTexture( texture, textureUnit );
  2900. }
  2901. break;
  2902. case 'tv':
  2903. // array of THREE.Texture (2d)
  2904. if ( uniform._array === undefined ) {
  2905. uniform._array = [];
  2906. }
  2907. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  2908. uniform._array[ i ] = getTextureUnit();
  2909. }
  2910. _gl.uniform1iv( location, uniform._array );
  2911. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  2912. texture = uniform.value[ i ];
  2913. textureUnit = uniform._array[ i ];
  2914. if ( ! texture ) continue;
  2915. _this.setTexture( texture, textureUnit );
  2916. }
  2917. break;
  2918. default:
  2919. console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
  2920. }
  2921. }
  2922. };
  2923. function setupMatrices ( object, camera ) {
  2924. object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  2925. object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
  2926. };
  2927. //
  2928. function setColorGamma( array, offset, color, intensitySq ) {
  2929. array[ offset ] = color.r * color.r * intensitySq;
  2930. array[ offset + 1 ] = color.g * color.g * intensitySq;
  2931. array[ offset + 2 ] = color.b * color.b * intensitySq;
  2932. };
  2933. function setColorLinear( array, offset, color, intensity ) {
  2934. array[ offset ] = color.r * intensity;
  2935. array[ offset + 1 ] = color.g * intensity;
  2936. array[ offset + 2 ] = color.b * intensity;
  2937. };
  2938. function setupLights ( lights ) {
  2939. var l, ll, light, n,
  2940. r = 0, g = 0, b = 0,
  2941. color, skyColor, groundColor,
  2942. intensity, intensitySq,
  2943. position,
  2944. distance,
  2945. zlights = _lights,
  2946. dirColors = zlights.directional.colors,
  2947. dirPositions = zlights.directional.positions,
  2948. pointColors = zlights.point.colors,
  2949. pointPositions = zlights.point.positions,
  2950. pointDistances = zlights.point.distances,
  2951. spotColors = zlights.spot.colors,
  2952. spotPositions = zlights.spot.positions,
  2953. spotDistances = zlights.spot.distances,
  2954. spotDirections = zlights.spot.directions,
  2955. spotAnglesCos = zlights.spot.anglesCos,
  2956. spotExponents = zlights.spot.exponents,
  2957. hemiSkyColors = zlights.hemi.skyColors,
  2958. hemiGroundColors = zlights.hemi.groundColors,
  2959. hemiPositions = zlights.hemi.positions,
  2960. dirLength = 0,
  2961. pointLength = 0,
  2962. spotLength = 0,
  2963. hemiLength = 0,
  2964. dirCount = 0,
  2965. pointCount = 0,
  2966. spotCount = 0,
  2967. hemiCount = 0,
  2968. dirOffset = 0,
  2969. pointOffset = 0,
  2970. spotOffset = 0,
  2971. hemiOffset = 0;
  2972. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  2973. light = lights[ l ];
  2974. if ( light.onlyShadow ) continue;
  2975. color = light.color;
  2976. intensity = light.intensity;
  2977. distance = light.distance;
  2978. if ( light instanceof THREE.AmbientLight ) {
  2979. if ( ! light.visible ) continue;
  2980. if ( _this.gammaInput ) {
  2981. r += color.r * color.r;
  2982. g += color.g * color.g;
  2983. b += color.b * color.b;
  2984. } else {
  2985. r += color.r;
  2986. g += color.g;
  2987. b += color.b;
  2988. }
  2989. } else if ( light instanceof THREE.DirectionalLight ) {
  2990. dirCount += 1;
  2991. if ( ! light.visible ) continue;
  2992. _direction.setFromMatrixPosition( light.matrixWorld );
  2993. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  2994. _direction.sub( _vector3 );
  2995. _direction.normalize();
  2996. dirOffset = dirLength * 3;
  2997. dirPositions[ dirOffset ] = _direction.x;
  2998. dirPositions[ dirOffset + 1 ] = _direction.y;
  2999. dirPositions[ dirOffset + 2 ] = _direction.z;
  3000. if ( _this.gammaInput ) {
  3001. setColorGamma( dirColors, dirOffset, color, intensity * intensity );
  3002. } else {
  3003. setColorLinear( dirColors, dirOffset, color, intensity );
  3004. }
  3005. dirLength += 1;
  3006. } else if ( light instanceof THREE.PointLight ) {
  3007. pointCount += 1;
  3008. if ( ! light.visible ) continue;
  3009. pointOffset = pointLength * 3;
  3010. if ( _this.gammaInput ) {
  3011. setColorGamma( pointColors, pointOffset, color, intensity * intensity );
  3012. } else {
  3013. setColorLinear( pointColors, pointOffset, color, intensity );
  3014. }
  3015. _vector3.setFromMatrixPosition( light.matrixWorld );
  3016. pointPositions[ pointOffset ] = _vector3.x;
  3017. pointPositions[ pointOffset + 1 ] = _vector3.y;
  3018. pointPositions[ pointOffset + 2 ] = _vector3.z;
  3019. pointDistances[ pointLength ] = distance;
  3020. pointLength += 1;
  3021. } else if ( light instanceof THREE.SpotLight ) {
  3022. spotCount += 1;
  3023. if ( ! light.visible ) continue;
  3024. spotOffset = spotLength * 3;
  3025. if ( _this.gammaInput ) {
  3026. setColorGamma( spotColors, spotOffset, color, intensity * intensity );
  3027. } else {
  3028. setColorLinear( spotColors, spotOffset, color, intensity );
  3029. }
  3030. _direction.setFromMatrixPosition( light.matrixWorld );
  3031. spotPositions[ spotOffset ] = _direction.x;
  3032. spotPositions[ spotOffset + 1 ] = _direction.y;
  3033. spotPositions[ spotOffset + 2 ] = _direction.z;
  3034. spotDistances[ spotLength ] = distance;
  3035. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  3036. _direction.sub( _vector3 );
  3037. _direction.normalize();
  3038. spotDirections[ spotOffset ] = _direction.x;
  3039. spotDirections[ spotOffset + 1 ] = _direction.y;
  3040. spotDirections[ spotOffset + 2 ] = _direction.z;
  3041. spotAnglesCos[ spotLength ] = Math.cos( light.angle );
  3042. spotExponents[ spotLength ] = light.exponent;
  3043. spotLength += 1;
  3044. } else if ( light instanceof THREE.HemisphereLight ) {
  3045. hemiCount += 1;
  3046. if ( ! light.visible ) continue;
  3047. _direction.setFromMatrixPosition( light.matrixWorld );
  3048. _direction.normalize();
  3049. hemiOffset = hemiLength * 3;
  3050. hemiPositions[ hemiOffset ] = _direction.x;
  3051. hemiPositions[ hemiOffset + 1 ] = _direction.y;
  3052. hemiPositions[ hemiOffset + 2 ] = _direction.z;
  3053. skyColor = light.color;
  3054. groundColor = light.groundColor;
  3055. if ( _this.gammaInput ) {
  3056. intensitySq = intensity * intensity;
  3057. setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
  3058. setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
  3059. } else {
  3060. setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
  3061. setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
  3062. }
  3063. hemiLength += 1;
  3064. }
  3065. }
  3066. // null eventual remains from removed lights
  3067. // (this is to avoid if in shader)
  3068. for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
  3069. for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
  3070. for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
  3071. for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
  3072. for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
  3073. zlights.directional.length = dirLength;
  3074. zlights.point.length = pointLength;
  3075. zlights.spot.length = spotLength;
  3076. zlights.hemi.length = hemiLength;
  3077. zlights.ambient[ 0 ] = r;
  3078. zlights.ambient[ 1 ] = g;
  3079. zlights.ambient[ 2 ] = b;
  3080. };
  3081. // GL state setting
  3082. this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
  3083. if ( cullFace === THREE.CullFaceNone ) {
  3084. _gl.disable( _gl.CULL_FACE );
  3085. } else {
  3086. if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
  3087. _gl.frontFace( _gl.CW );
  3088. } else {
  3089. _gl.frontFace( _gl.CCW );
  3090. }
  3091. if ( cullFace === THREE.CullFaceBack ) {
  3092. _gl.cullFace( _gl.BACK );
  3093. } else if ( cullFace === THREE.CullFaceFront ) {
  3094. _gl.cullFace( _gl.FRONT );
  3095. } else {
  3096. _gl.cullFace( _gl.FRONT_AND_BACK );
  3097. }
  3098. _gl.enable( _gl.CULL_FACE );
  3099. }
  3100. };
  3101. this.setMaterialFaces = function ( material ) {
  3102. var doubleSided = material.side === THREE.DoubleSide;
  3103. var flipSided = material.side === THREE.BackSide;
  3104. if ( _oldDoubleSided !== doubleSided ) {
  3105. if ( doubleSided ) {
  3106. _gl.disable( _gl.CULL_FACE );
  3107. } else {
  3108. _gl.enable( _gl.CULL_FACE );
  3109. }
  3110. _oldDoubleSided = doubleSided;
  3111. }
  3112. if ( _oldFlipSided !== flipSided ) {
  3113. if ( flipSided ) {
  3114. _gl.frontFace( _gl.CW );
  3115. } else {
  3116. _gl.frontFace( _gl.CCW );
  3117. }
  3118. _oldFlipSided = flipSided;
  3119. }
  3120. };
  3121. this.setDepthTest = function ( depthTest ) {
  3122. if ( _oldDepthTest !== depthTest ) {
  3123. if ( depthTest ) {
  3124. _gl.enable( _gl.DEPTH_TEST );
  3125. } else {
  3126. _gl.disable( _gl.DEPTH_TEST );
  3127. }
  3128. _oldDepthTest = depthTest;
  3129. }
  3130. };
  3131. this.setDepthWrite = function ( depthWrite ) {
  3132. if ( _oldDepthWrite !== depthWrite ) {
  3133. _gl.depthMask( depthWrite );
  3134. _oldDepthWrite = depthWrite;
  3135. }
  3136. };
  3137. function setLineWidth ( width ) {
  3138. if ( width !== _oldLineWidth ) {
  3139. _gl.lineWidth( width );
  3140. _oldLineWidth = width;
  3141. }
  3142. };
  3143. function setPolygonOffset ( polygonoffset, factor, units ) {
  3144. if ( _oldPolygonOffset !== polygonoffset ) {
  3145. if ( polygonoffset ) {
  3146. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  3147. } else {
  3148. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  3149. }
  3150. _oldPolygonOffset = polygonoffset;
  3151. }
  3152. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  3153. _gl.polygonOffset( factor, units );
  3154. _oldPolygonOffsetFactor = factor;
  3155. _oldPolygonOffsetUnits = units;
  3156. }
  3157. };
  3158. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  3159. if ( blending !== _oldBlending ) {
  3160. if ( blending === THREE.NoBlending ) {
  3161. _gl.disable( _gl.BLEND );
  3162. } else if ( blending === THREE.AdditiveBlending ) {
  3163. _gl.enable( _gl.BLEND );
  3164. _gl.blendEquation( _gl.FUNC_ADD );
  3165. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  3166. } else if ( blending === THREE.SubtractiveBlending ) {
  3167. // TODO: Find blendFuncSeparate() combination
  3168. _gl.enable( _gl.BLEND );
  3169. _gl.blendEquation( _gl.FUNC_ADD );
  3170. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  3171. } else if ( blending === THREE.MultiplyBlending ) {
  3172. // TODO: Find blendFuncSeparate() combination
  3173. _gl.enable( _gl.BLEND );
  3174. _gl.blendEquation( _gl.FUNC_ADD );
  3175. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  3176. } else if ( blending === THREE.CustomBlending ) {
  3177. _gl.enable( _gl.BLEND );
  3178. } else {
  3179. _gl.enable( _gl.BLEND );
  3180. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  3181. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  3182. }
  3183. _oldBlending = blending;
  3184. }
  3185. if ( blending === THREE.CustomBlending ) {
  3186. if ( blendEquation !== _oldBlendEquation ) {
  3187. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  3188. _oldBlendEquation = blendEquation;
  3189. }
  3190. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  3191. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  3192. _oldBlendSrc = blendSrc;
  3193. _oldBlendDst = blendDst;
  3194. }
  3195. } else {
  3196. _oldBlendEquation = null;
  3197. _oldBlendSrc = null;
  3198. _oldBlendDst = null;
  3199. }
  3200. };
  3201. // Textures
  3202. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  3203. if ( isImagePowerOfTwo ) {
  3204. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  3205. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  3206. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  3207. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  3208. } else {
  3209. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  3210. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  3211. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  3212. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  3213. }
  3214. if ( _glExtensionTextureFilterAnisotropic && texture.type !== THREE.FloatType ) {
  3215. if ( texture.anisotropy > 1 || texture.__oldAnisotropy ) {
  3216. _gl.texParameterf( textureType, _glExtensionTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _maxAnisotropy ) );
  3217. texture.__oldAnisotropy = texture.anisotropy;
  3218. }
  3219. }
  3220. };
  3221. this.uploadTexture = function ( texture ) {
  3222. if ( ! texture.__webglInit ) {
  3223. texture.__webglInit = true;
  3224. texture.addEventListener( 'dispose', onTextureDispose );
  3225. texture.__webglTexture = _gl.createTexture();
  3226. _this.info.memory.textures ++;
  3227. }
  3228. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  3229. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  3230. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  3231. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  3232. texture.image = clampToMaxSize( texture.image, _maxTextureSize );
  3233. var image = texture.image,
  3234. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  3235. glFormat = paramThreeToGL( texture.format ),
  3236. glType = paramThreeToGL( texture.type );
  3237. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  3238. var mipmap, mipmaps = texture.mipmaps;
  3239. if ( texture instanceof THREE.DataTexture ) {
  3240. // use manually created mipmaps if available
  3241. // if there are no manual mipmaps
  3242. // set 0 level mipmap and then use GL to generate other mipmap levels
  3243. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  3244. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  3245. mipmap = mipmaps[ i ];
  3246. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  3247. }
  3248. texture.generateMipmaps = false;
  3249. } else {
  3250. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  3251. }
  3252. } else if ( texture instanceof THREE.CompressedTexture ) {
  3253. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  3254. mipmap = mipmaps[ i ];
  3255. if ( texture.format !== THREE.RGBAFormat ) {
  3256. if ( _compressedTextureFormats.indexOf( glFormat ) > -1 ) {
  3257. _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  3258. } else {
  3259. console.warn( "Attempt to load unsupported compressed texture format" );
  3260. }
  3261. } else {
  3262. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  3263. }
  3264. }
  3265. } else { // regular Texture (image, video, canvas)
  3266. // use manually created mipmaps if available
  3267. // if there are no manual mipmaps
  3268. // set 0 level mipmap and then use GL to generate other mipmap levels
  3269. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  3270. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  3271. mipmap = mipmaps[ i ];
  3272. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
  3273. }
  3274. texture.generateMipmaps = false;
  3275. } else {
  3276. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  3277. }
  3278. }
  3279. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  3280. texture.needsUpdate = false;
  3281. if ( texture.onUpdate ) texture.onUpdate();
  3282. };
  3283. this.setTexture = function ( texture, slot ) {
  3284. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3285. if ( texture.needsUpdate ) {
  3286. _this.uploadTexture( texture );
  3287. } else {
  3288. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  3289. }
  3290. };
  3291. function clampToMaxSize ( image, maxSize ) {
  3292. if ( image.width > maxSize || image.height > maxSize ) {
  3293. // Warning: Scaling through the canvas will only work with images that use
  3294. // premultiplied alpha.
  3295. var scale = maxSize / Math.max( image.width, image.height );
  3296. var canvas = document.createElement( 'canvas' );
  3297. canvas.width = Math.floor( image.width * scale );
  3298. canvas.height = Math.floor( image.height * scale );
  3299. var context = canvas.getContext( '2d' );
  3300. context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
  3301. console.log( 'THREE.WebGLRenderer:', image, 'is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height + '.' );
  3302. return canvas;
  3303. }
  3304. return image;
  3305. }
  3306. function setCubeTexture ( texture, slot ) {
  3307. if ( texture.image.length === 6 ) {
  3308. if ( texture.needsUpdate ) {
  3309. if ( ! texture.image.__webglTextureCube ) {
  3310. texture.addEventListener( 'dispose', onTextureDispose );
  3311. texture.image.__webglTextureCube = _gl.createTexture();
  3312. _this.info.memory.textures ++;
  3313. }
  3314. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3315. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  3316. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  3317. var isCompressed = texture instanceof THREE.CompressedTexture;
  3318. var cubeImage = [];
  3319. for ( var i = 0; i < 6; i ++ ) {
  3320. if ( _this.autoScaleCubemaps && ! isCompressed ) {
  3321. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  3322. } else {
  3323. cubeImage[ i ] = texture.image[ i ];
  3324. }
  3325. }
  3326. var image = cubeImage[ 0 ],
  3327. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  3328. glFormat = paramThreeToGL( texture.format ),
  3329. glType = paramThreeToGL( texture.type );
  3330. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  3331. for ( var i = 0; i < 6; i ++ ) {
  3332. if ( ! isCompressed ) {
  3333. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  3334. } else {
  3335. var mipmap, mipmaps = cubeImage[ i ].mipmaps;
  3336. for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
  3337. mipmap = mipmaps[ j ];
  3338. if ( texture.format !== THREE.RGBAFormat ) {
  3339. if ( _compressedTextureFormats.indexOf( glFormat ) > -1 ) {
  3340. _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  3341. } else {
  3342. console.warn( "Attempt to load unsupported compressed texture format" );
  3343. }
  3344. } else {
  3345. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  3346. }
  3347. }
  3348. }
  3349. }
  3350. if ( texture.generateMipmaps && isImagePowerOfTwo ) {
  3351. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3352. }
  3353. texture.needsUpdate = false;
  3354. if ( texture.onUpdate ) texture.onUpdate();
  3355. } else {
  3356. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3357. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  3358. }
  3359. }
  3360. };
  3361. function setCubeTextureDynamic ( texture, slot ) {
  3362. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3363. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  3364. };
  3365. // Render targets
  3366. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  3367. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  3368. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  3369. };
  3370. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  3371. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  3372. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  3373. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  3374. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3375. /* For some reason this is not working. Defaulting to RGBA4.
  3376. } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3377. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  3378. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3379. */
  3380. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3381. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  3382. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3383. } else {
  3384. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  3385. }
  3386. };
  3387. this.setRenderTarget = function ( renderTarget ) {
  3388. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  3389. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  3390. if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  3391. if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  3392. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  3393. renderTarget.__webglTexture = _gl.createTexture();
  3394. _this.info.memory.textures ++;
  3395. // Setup texture, create render and frame buffers
  3396. var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ),
  3397. glFormat = paramThreeToGL( renderTarget.format ),
  3398. glType = paramThreeToGL( renderTarget.type );
  3399. if ( isCube ) {
  3400. renderTarget.__webglFramebuffer = [];
  3401. renderTarget.__webglRenderbuffer = [];
  3402. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  3403. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  3404. for ( var i = 0; i < 6; i ++ ) {
  3405. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  3406. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  3407. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  3408. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  3409. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  3410. }
  3411. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3412. } else {
  3413. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  3414. if ( renderTarget.shareDepthFrom ) {
  3415. renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
  3416. } else {
  3417. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  3418. }
  3419. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  3420. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  3421. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  3422. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  3423. if ( renderTarget.shareDepthFrom ) {
  3424. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  3425. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  3426. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3427. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  3428. }
  3429. } else {
  3430. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  3431. }
  3432. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  3433. }
  3434. // Release everything
  3435. if ( isCube ) {
  3436. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  3437. } else {
  3438. _gl.bindTexture( _gl.TEXTURE_2D, null );
  3439. }
  3440. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  3441. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
  3442. }
  3443. var framebuffer, width, height, vx, vy;
  3444. if ( renderTarget ) {
  3445. if ( isCube ) {
  3446. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  3447. } else {
  3448. framebuffer = renderTarget.__webglFramebuffer;
  3449. }
  3450. width = renderTarget.width;
  3451. height = renderTarget.height;
  3452. vx = 0;
  3453. vy = 0;
  3454. } else {
  3455. framebuffer = null;
  3456. width = _viewportWidth;
  3457. height = _viewportHeight;
  3458. vx = _viewportX;
  3459. vy = _viewportY;
  3460. }
  3461. if ( framebuffer !== _currentFramebuffer ) {
  3462. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  3463. _gl.viewport( vx, vy, width, height );
  3464. _currentFramebuffer = framebuffer;
  3465. }
  3466. _currentWidth = width;
  3467. _currentHeight = height;
  3468. };
  3469. function updateRenderTargetMipmap ( renderTarget ) {
  3470. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  3471. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  3472. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3473. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  3474. } else {
  3475. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  3476. _gl.generateMipmap( _gl.TEXTURE_2D );
  3477. _gl.bindTexture( _gl.TEXTURE_2D, null );
  3478. }
  3479. };
  3480. // Fallback filters for non-power-of-2 textures
  3481. function filterFallback ( f ) {
  3482. if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
  3483. return _gl.NEAREST;
  3484. }
  3485. return _gl.LINEAR;
  3486. };
  3487. // Map three.js constants to WebGL constants
  3488. function paramThreeToGL ( p ) {
  3489. if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
  3490. if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
  3491. if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
  3492. if ( p === THREE.NearestFilter ) return _gl.NEAREST;
  3493. if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
  3494. if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
  3495. if ( p === THREE.LinearFilter ) return _gl.LINEAR;
  3496. if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
  3497. if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
  3498. if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
  3499. if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
  3500. if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
  3501. if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
  3502. if ( p === THREE.ByteType ) return _gl.BYTE;
  3503. if ( p === THREE.ShortType ) return _gl.SHORT;
  3504. if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
  3505. if ( p === THREE.IntType ) return _gl.INT;
  3506. if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
  3507. if ( p === THREE.FloatType ) return _gl.FLOAT;
  3508. if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
  3509. if ( p === THREE.RGBFormat ) return _gl.RGB;
  3510. if ( p === THREE.RGBAFormat ) return _gl.RGBA;
  3511. if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
  3512. if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
  3513. if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
  3514. if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
  3515. if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
  3516. if ( p === THREE.ZeroFactor ) return _gl.ZERO;
  3517. if ( p === THREE.OneFactor ) return _gl.ONE;
  3518. if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
  3519. if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
  3520. if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
  3521. if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
  3522. if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
  3523. if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
  3524. if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
  3525. if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
  3526. if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
  3527. if ( _glExtensionCompressedTextureS3TC !== null ) {
  3528. if ( p === THREE.RGB_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGB_S3TC_DXT1_EXT;
  3529. if ( p === THREE.RGBA_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3530. if ( p === THREE.RGBA_S3TC_DXT3_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3531. if ( p === THREE.RGBA_S3TC_DXT5_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3532. }
  3533. if ( _glExtensionCompressedTexturePVRTC !== null ) {
  3534. if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGB_PVRTC_4BPPV1_IMG ;
  3535. if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGB_PVRTC_2BPPV1_IMG ;
  3536. if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ;
  3537. if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG ;
  3538. }
  3539. return 0;
  3540. };
  3541. // Allocations
  3542. function allocateBones ( object ) {
  3543. if ( _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  3544. return 1024;
  3545. } else {
  3546. // default for when object is not specified
  3547. // ( for example when prebuilding shader
  3548. // to be used with multiple objects )
  3549. //
  3550. // - leave some extra space for other uniforms
  3551. // - limit here is ANGLE's 254 max uniform vectors
  3552. // (up to 54 should be safe)
  3553. var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
  3554. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  3555. var maxBones = nVertexMatrices;
  3556. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  3557. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  3558. if ( maxBones < object.skeleton.bones.length ) {
  3559. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  3560. }
  3561. }
  3562. return maxBones;
  3563. }
  3564. };
  3565. function allocateLights( lights ) {
  3566. var dirLights = 0;
  3567. var pointLights = 0;
  3568. var spotLights = 0;
  3569. var hemiLights = 0;
  3570. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  3571. var light = lights[ l ];
  3572. if ( light.onlyShadow || light.visible === false ) continue;
  3573. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  3574. if ( light instanceof THREE.PointLight ) pointLights ++;
  3575. if ( light instanceof THREE.SpotLight ) spotLights ++;
  3576. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  3577. }
  3578. return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
  3579. };
  3580. function allocateShadows( lights ) {
  3581. var maxShadows = 0;
  3582. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  3583. var light = lights[ l ];
  3584. if ( ! light.castShadow ) continue;
  3585. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  3586. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  3587. }
  3588. return maxShadows;
  3589. };
  3590. // Initialization
  3591. function initGL() {
  3592. try {
  3593. var attributes = {
  3594. alpha: _alpha,
  3595. depth: _depth,
  3596. stencil: _stencil,
  3597. antialias: _antialias,
  3598. premultipliedAlpha: _premultipliedAlpha,
  3599. preserveDrawingBuffer: _preserveDrawingBuffer
  3600. };
  3601. _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
  3602. if ( _gl === null ) {
  3603. throw 'Error creating WebGL context.';
  3604. }
  3605. } catch ( error ) {
  3606. console.error( error );
  3607. }
  3608. _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' );
  3609. _glExtensionTextureFloatLinear = _gl.getExtension( 'OES_texture_float_linear' );
  3610. _glExtensionStandardDerivatives = _gl.getExtension( 'OES_standard_derivatives' );
  3611. _glExtensionTextureFilterAnisotropic = _gl.getExtension( 'EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
  3612. _glExtensionCompressedTextureS3TC = _gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
  3613. _glExtensionCompressedTexturePVRTC = _gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
  3614. _glExtensionElementIndexUint = _gl.getExtension( 'OES_element_index_uint' );
  3615. if ( _glExtensionTextureFloat === null ) {
  3616. console.log( 'THREE.WebGLRenderer: Float textures not supported.' );
  3617. }
  3618. if ( _glExtensionStandardDerivatives === null ) {
  3619. console.log( 'THREE.WebGLRenderer: Standard derivatives not supported.' );
  3620. }
  3621. if ( _glExtensionTextureFilterAnisotropic === null ) {
  3622. console.log( 'THREE.WebGLRenderer: Anisotropic texture filtering not supported.' );
  3623. }
  3624. if ( _glExtensionCompressedTextureS3TC === null ) {
  3625. console.log( 'THREE.WebGLRenderer: S3TC compressed textures not supported.' );
  3626. }
  3627. if ( _glExtensionCompressedTexturePVRTC === null ) {
  3628. console.log( 'THREE.WebGLRenderer: PVRTC compressed textures not supported.' );
  3629. }
  3630. if ( _glExtensionElementIndexUint === null ) {
  3631. console.log( 'THREE.WebGLRenderer: elementindex as unsigned integer not supported.' );
  3632. }
  3633. if ( _gl.getShaderPrecisionFormat === undefined ) {
  3634. _gl.getShaderPrecisionFormat = function () {
  3635. return {
  3636. 'rangeMin': 1,
  3637. 'rangeMax': 1,
  3638. 'precision': 1
  3639. };
  3640. }
  3641. }
  3642. if ( _logarithmicDepthBuffer ) {
  3643. _glExtensionFragDepth = _gl.getExtension( 'EXT_frag_depth' );
  3644. }
  3645. }
  3646. function setDefaultGLState () {
  3647. _gl.clearColor( 0, 0, 0, 1 );
  3648. _gl.clearDepth( 1 );
  3649. _gl.clearStencil( 0 );
  3650. _gl.enable( _gl.DEPTH_TEST );
  3651. _gl.depthFunc( _gl.LEQUAL );
  3652. _gl.frontFace( _gl.CCW );
  3653. _gl.cullFace( _gl.BACK );
  3654. _gl.enable( _gl.CULL_FACE );
  3655. _gl.enable( _gl.BLEND );
  3656. _gl.blendEquation( _gl.FUNC_ADD );
  3657. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  3658. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  3659. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  3660. }
  3661. function resetGLState() {
  3662. _currentProgram = null;
  3663. _currentCamera = null;
  3664. _oldBlending = - 1;
  3665. _oldDepthTest = - 1;
  3666. _oldDepthWrite = - 1;
  3667. _oldDoubleSided = - 1;
  3668. _oldFlipSided = - 1;
  3669. _currentGeometryGroupHash = - 1;
  3670. _currentMaterialId = - 1;
  3671. _lightsNeedUpdate = true;
  3672. }
  3673. this.resetGLState = resetGLState;
  3674. // default plugins (order is important)
  3675. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  3676. this.addPrePlugin( this.shadowMapPlugin );
  3677. this.addPostPlugin( new THREE.SpritePlugin() );
  3678. this.addPostPlugin( new THREE.LensFlarePlugin() );
  3679. };