WebGLRenderer.js 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363
  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. var _glExtensionBlendMinMax;
  132. initGL();
  133. setDefaultGLState();
  134. this.context = _gl;
  135. // GPU capabilities
  136. var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
  137. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  138. var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
  139. var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  140. var _maxAnisotropy = _glExtensionTextureFilterAnisotropic ? _gl.getParameter( _glExtensionTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
  141. var _supportsVertexTextures = ( _maxVertexTextures > 0 );
  142. var _supportsBoneTextures = _supportsVertexTextures && _glExtensionTextureFloat;
  143. // COMPRESSED_TEXTURE_FORMATS return IntArray
  144. // but we need Array.indexOf to test formats availabilities
  145. var _compressedTextureFormats = [];
  146. if( _glExtensionCompressedTexturePVRTC || _glExtensionCompressedTextureS3TC ) {
  147. var compressedfmts = _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS );
  148. for( var i = 0; i < compressedfmts.length; i++ ){
  149. _compressedTextureFormats.push( compressedfmts[i] );
  150. }
  151. }
  152. //
  153. var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
  154. var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
  155. var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT );
  156. var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
  157. var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
  158. var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT );
  159. // clamp precision to maximum available
  160. var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
  161. var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
  162. if ( _precision === 'highp' && ! highpAvailable ) {
  163. if ( mediumpAvailable ) {
  164. _precision = 'mediump';
  165. console.warn( 'THREE.WebGLRenderer: highp not supported, using mediump.' );
  166. } else {
  167. _precision = 'lowp';
  168. console.warn( 'THREE.WebGLRenderer: highp and mediump not supported, using lowp.' );
  169. }
  170. }
  171. if ( _precision === 'mediump' && ! mediumpAvailable ) {
  172. _precision = 'lowp';
  173. console.warn( 'THREE.WebGLRenderer: mediump not supported, using lowp.' );
  174. }
  175. // API
  176. this.getContext = function () {
  177. return _gl;
  178. };
  179. this.supportsVertexTextures = function () {
  180. return _supportsVertexTextures;
  181. };
  182. this.supportsFloatTextures = function () {
  183. return _glExtensionTextureFloat;
  184. };
  185. this.supportsStandardDerivatives = function () {
  186. return _glExtensionStandardDerivatives;
  187. };
  188. this.supportsCompressedTextureS3TC = function () {
  189. return _glExtensionCompressedTextureS3TC;
  190. };
  191. this.supportsCompressedTexturePVRTC = function () {
  192. return _glExtensionCompressedTexturePVRTC;
  193. };
  194. this.supportsBlendMinMax = function () {
  195. return _glExtensionBlendMinMax;
  196. };
  197. this.getMaxAnisotropy = function () {
  198. return _maxAnisotropy;
  199. };
  200. this.getPrecision = function () {
  201. return _precision;
  202. };
  203. this.setSize = function ( width, height, updateStyle ) {
  204. _canvas.width = width * this.devicePixelRatio;
  205. _canvas.height = height * this.devicePixelRatio;
  206. if ( updateStyle !== false ) {
  207. _canvas.style.width = width + 'px';
  208. _canvas.style.height = height + 'px';
  209. }
  210. this.setViewport( 0, 0, width, height );
  211. };
  212. this.setViewport = function ( x, y, width, height ) {
  213. _viewportX = x * this.devicePixelRatio;
  214. _viewportY = y * this.devicePixelRatio;
  215. _viewportWidth = width * this.devicePixelRatio;
  216. _viewportHeight = height * this.devicePixelRatio;
  217. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  218. };
  219. this.setScissor = function ( x, y, width, height ) {
  220. _gl.scissor(
  221. x * this.devicePixelRatio,
  222. y * this.devicePixelRatio,
  223. width * this.devicePixelRatio,
  224. height * this.devicePixelRatio
  225. );
  226. };
  227. this.enableScissorTest = function ( enable ) {
  228. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  229. };
  230. // Clearing
  231. this.setClearColor = function ( color, alpha ) {
  232. _clearColor.set( color );
  233. _clearAlpha = alpha !== undefined ? alpha : 1;
  234. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  235. };
  236. this.setClearColorHex = function ( hex, alpha ) {
  237. console.warn( 'THREE.WebGLRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  238. this.setClearColor( hex, alpha );
  239. };
  240. this.getClearColor = function () {
  241. return _clearColor;
  242. };
  243. this.getClearAlpha = function () {
  244. return _clearAlpha;
  245. };
  246. this.clear = function ( color, depth, stencil ) {
  247. var bits = 0;
  248. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  249. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  250. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  251. _gl.clear( bits );
  252. };
  253. this.clearColor = function () {
  254. _gl.clear( _gl.COLOR_BUFFER_BIT );
  255. };
  256. this.clearDepth = function () {
  257. _gl.clear( _gl.DEPTH_BUFFER_BIT );
  258. };
  259. this.clearStencil = function () {
  260. _gl.clear( _gl.STENCIL_BUFFER_BIT );
  261. };
  262. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  263. this.setRenderTarget( renderTarget );
  264. this.clear( color, depth, stencil );
  265. };
  266. // Plugins
  267. this.addPostPlugin = function ( plugin ) {
  268. plugin.init( this, lights, _webglObjects, _webglObjectsImmediate );
  269. this.renderPluginsPost.push( plugin );
  270. };
  271. this.addPrePlugin = function ( plugin ) {
  272. plugin.init( this, lights, _webglObjects, _webglObjectsImmediate );
  273. this.renderPluginsPre.push( plugin );
  274. };
  275. // Rendering
  276. this.updateShadowMap = function ( scene, camera ) {
  277. _currentProgram = null;
  278. _oldBlending = - 1;
  279. _oldDepthTest = - 1;
  280. _oldDepthWrite = - 1;
  281. _currentGeometryGroupHash = - 1;
  282. _currentMaterialId = - 1;
  283. _lightsNeedUpdate = true;
  284. _oldDoubleSided = - 1;
  285. _oldFlipSided = - 1;
  286. this.shadowMapPlugin.update( scene, camera );
  287. };
  288. // Internal functions
  289. // Buffer allocation
  290. function createParticleBuffers ( geometry ) {
  291. geometry.__webglVertexBuffer = _gl.createBuffer();
  292. geometry.__webglColorBuffer = _gl.createBuffer();
  293. _this.info.memory.geometries ++;
  294. };
  295. function createLineBuffers ( geometry ) {
  296. geometry.__webglVertexBuffer = _gl.createBuffer();
  297. geometry.__webglColorBuffer = _gl.createBuffer();
  298. geometry.__webglLineDistanceBuffer = _gl.createBuffer();
  299. _this.info.memory.geometries ++;
  300. };
  301. function createMeshBuffers ( geometryGroup ) {
  302. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  303. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  304. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  305. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  306. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  307. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  308. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  309. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  310. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  311. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  312. var m, ml;
  313. if ( geometryGroup.numMorphTargets ) {
  314. geometryGroup.__webglMorphTargetsBuffers = [];
  315. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  316. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  317. }
  318. }
  319. if ( geometryGroup.numMorphNormals ) {
  320. geometryGroup.__webglMorphNormalsBuffers = [];
  321. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  322. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  323. }
  324. }
  325. _this.info.memory.geometries ++;
  326. };
  327. // Events
  328. var onObjectRemoved = function ( event ) {
  329. var object = event.target;
  330. object.traverse( function ( child ) {
  331. child.removeEventListener( 'remove', onObjectRemoved );
  332. removeObject( child );
  333. } );
  334. };
  335. var onGeometryDispose = function ( event ) {
  336. var geometry = event.target;
  337. geometry.removeEventListener( 'dispose', onGeometryDispose );
  338. deallocateGeometry( geometry );
  339. };
  340. var onTextureDispose = function ( event ) {
  341. var texture = event.target;
  342. texture.removeEventListener( 'dispose', onTextureDispose );
  343. deallocateTexture( texture );
  344. _this.info.memory.textures --;
  345. };
  346. var onRenderTargetDispose = function ( event ) {
  347. var renderTarget = event.target;
  348. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  349. deallocateRenderTarget( renderTarget );
  350. _this.info.memory.textures --;
  351. };
  352. var onMaterialDispose = function ( event ) {
  353. var material = event.target;
  354. material.removeEventListener( 'dispose', onMaterialDispose );
  355. deallocateMaterial( material );
  356. };
  357. // Buffer deallocation
  358. var deleteBuffers = function ( geometry ) {
  359. if ( geometry.__webglVertexBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglVertexBuffer );
  360. if ( geometry.__webglNormalBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglNormalBuffer );
  361. if ( geometry.__webglTangentBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglTangentBuffer );
  362. if ( geometry.__webglColorBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglColorBuffer );
  363. if ( geometry.__webglUVBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglUVBuffer );
  364. if ( geometry.__webglUV2Buffer !== undefined ) _gl.deleteBuffer( geometry.__webglUV2Buffer );
  365. if ( geometry.__webglSkinIndicesBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinIndicesBuffer );
  366. if ( geometry.__webglSkinWeightsBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinWeightsBuffer );
  367. if ( geometry.__webglFaceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglFaceBuffer );
  368. if ( geometry.__webglLineBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineBuffer );
  369. if ( geometry.__webglLineDistanceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineDistanceBuffer );
  370. // custom attributes
  371. if ( geometry.__webglCustomAttributesList !== undefined ) {
  372. for ( var name in geometry.__webglCustomAttributesList ) {
  373. _gl.deleteBuffer( geometry.__webglCustomAttributesList[ name ].buffer );
  374. }
  375. }
  376. _this.info.memory.geometries --;
  377. };
  378. var deallocateGeometry = function ( geometry ) {
  379. geometry.__webglInit = undefined;
  380. if ( geometry instanceof THREE.BufferGeometry ) {
  381. for ( var name in geometry.attributes ) {
  382. if ( geometry.attributes[ name ].buffer !== undefined ) {
  383. _gl.deleteBuffer( geometry.attributes[ name ].buffer );
  384. }
  385. }
  386. _this.info.memory.geometries --;
  387. } else {
  388. if ( geometry.geometryGroups !== undefined ) {
  389. for ( var i = 0,l = geometry.geometryGroupsList.length; i<l;i++ ) {
  390. var geometryGroup = geometry.geometryGroupsList[ i ];
  391. if ( geometryGroup.numMorphTargets !== undefined ) {
  392. for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  393. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  394. }
  395. }
  396. if ( geometryGroup.numMorphNormals !== undefined ) {
  397. for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  398. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  399. }
  400. }
  401. deleteBuffers( geometryGroup );
  402. }
  403. } else {
  404. deleteBuffers( geometry );
  405. }
  406. }
  407. };
  408. var deallocateTexture = function ( texture ) {
  409. if ( texture.image && texture.image.__webglTextureCube ) {
  410. // cube texture
  411. _gl.deleteTexture( texture.image.__webglTextureCube );
  412. } else {
  413. // 2D texture
  414. if ( ! texture.__webglInit ) return;
  415. texture.__webglInit = false;
  416. _gl.deleteTexture( texture.__webglTexture );
  417. }
  418. };
  419. var deallocateRenderTarget = function ( renderTarget ) {
  420. if ( ! renderTarget || ! renderTarget.__webglTexture ) return;
  421. _gl.deleteTexture( renderTarget.__webglTexture );
  422. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  423. for ( var i = 0; i < 6; i ++ ) {
  424. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  425. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  426. }
  427. } else {
  428. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  429. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  430. }
  431. };
  432. var deallocateMaterial = function ( material ) {
  433. var program = material.program.program;
  434. if ( program === undefined ) return;
  435. material.program = undefined;
  436. // only deallocate GL program if this was the last use of shared program
  437. // assumed there is only single copy of any program in the _programs list
  438. // (that's how it's constructed)
  439. var i, il, programInfo;
  440. var deleteProgram = false;
  441. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  442. programInfo = _programs[ i ];
  443. if ( programInfo.program === program ) {
  444. programInfo.usedTimes --;
  445. if ( programInfo.usedTimes === 0 ) {
  446. deleteProgram = true;
  447. }
  448. break;
  449. }
  450. }
  451. if ( deleteProgram === true ) {
  452. // avoid using array.splice, this is costlier than creating new array from scratch
  453. var newPrograms = [];
  454. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  455. programInfo = _programs[ i ];
  456. if ( programInfo.program !== program ) {
  457. newPrograms.push( programInfo );
  458. }
  459. }
  460. _programs = newPrograms;
  461. _gl.deleteProgram( program );
  462. _this.info.memory.programs --;
  463. }
  464. };
  465. // Buffer initialization
  466. function initCustomAttributes ( geometry, object ) {
  467. var nvertices = geometry.vertices.length;
  468. var material = object.material;
  469. if ( material.attributes ) {
  470. if ( geometry.__webglCustomAttributesList === undefined ) {
  471. geometry.__webglCustomAttributesList = [];
  472. }
  473. for ( var name in material.attributes ) {
  474. var attribute = material.attributes[ name ];
  475. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  476. attribute.__webglInitialized = true;
  477. var size = 1; // "f" and "i"
  478. if ( attribute.type === 'v2' ) size = 2;
  479. else if ( attribute.type === 'v3' ) size = 3;
  480. else if ( attribute.type === 'v4' ) size = 4;
  481. else if ( attribute.type === 'c' ) size = 3;
  482. attribute.size = size;
  483. attribute.array = new Float32Array( nvertices * size );
  484. attribute.buffer = _gl.createBuffer();
  485. attribute.buffer.belongsToAttribute = name;
  486. attribute.needsUpdate = true;
  487. }
  488. geometry.__webglCustomAttributesList.push( attribute );
  489. }
  490. }
  491. };
  492. function initParticleBuffers ( geometry, object ) {
  493. var nvertices = geometry.vertices.length;
  494. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  495. geometry.__colorArray = new Float32Array( nvertices * 3 );
  496. geometry.__sortArray = [];
  497. geometry.__webglParticleCount = nvertices;
  498. initCustomAttributes ( geometry, object );
  499. };
  500. function initLineBuffers ( geometry, object ) {
  501. var nvertices = geometry.vertices.length;
  502. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  503. geometry.__colorArray = new Float32Array( nvertices * 3 );
  504. geometry.__lineDistanceArray = new Float32Array( nvertices * 1 );
  505. geometry.__webglLineCount = nvertices;
  506. initCustomAttributes ( geometry, object );
  507. };
  508. function initMeshBuffers ( geometryGroup, object ) {
  509. var geometry = object.geometry,
  510. faces3 = geometryGroup.faces3,
  511. nvertices = faces3.length * 3,
  512. ntris = faces3.length * 1,
  513. nlines = faces3.length * 3,
  514. material = getBufferMaterial( object, geometryGroup );
  515. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  516. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  517. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  518. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  519. if ( geometry.faceVertexUvs.length > 1 ) {
  520. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  521. }
  522. if ( geometry.hasTangents ) {
  523. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  524. }
  525. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  526. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  527. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  528. }
  529. var UintArray = _glExtensionElementIndexUint !== null && ntris > 21845 ? Uint32Array : Uint16Array; // 65535 / 3
  530. geometryGroup.__typeArray = UintArray;
  531. geometryGroup.__faceArray = new UintArray( ntris * 3 );
  532. geometryGroup.__lineArray = new UintArray( nlines * 2 );
  533. var m, ml;
  534. if ( geometryGroup.numMorphTargets ) {
  535. geometryGroup.__morphTargetsArrays = [];
  536. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  537. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  538. }
  539. }
  540. if ( geometryGroup.numMorphNormals ) {
  541. geometryGroup.__morphNormalsArrays = [];
  542. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  543. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  544. }
  545. }
  546. geometryGroup.__webglFaceCount = ntris * 3;
  547. geometryGroup.__webglLineCount = nlines * 2;
  548. // custom attributes
  549. if ( material.attributes ) {
  550. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  551. geometryGroup.__webglCustomAttributesList = [];
  552. }
  553. for ( var name in material.attributes ) {
  554. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  555. // attribute buffers which are correctly indexed in the setMeshBuffers function
  556. var originalAttribute = material.attributes[ name ];
  557. var attribute = {};
  558. for ( var property in originalAttribute ) {
  559. attribute[ property ] = originalAttribute[ property ];
  560. }
  561. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  562. attribute.__webglInitialized = true;
  563. var size = 1; // "f" and "i"
  564. if ( attribute.type === 'v2' ) size = 2;
  565. else if ( attribute.type === 'v3' ) size = 3;
  566. else if ( attribute.type === 'v4' ) size = 4;
  567. else if ( attribute.type === 'c' ) size = 3;
  568. attribute.size = size;
  569. attribute.array = new Float32Array( nvertices * size );
  570. attribute.buffer = _gl.createBuffer();
  571. attribute.buffer.belongsToAttribute = name;
  572. originalAttribute.needsUpdate = true;
  573. attribute.__original = originalAttribute;
  574. }
  575. geometryGroup.__webglCustomAttributesList.push( attribute );
  576. }
  577. }
  578. geometryGroup.__inittedArrays = true;
  579. };
  580. function getBufferMaterial( object, geometryGroup ) {
  581. return object.material instanceof THREE.MeshFaceMaterial
  582. ? object.material.materials[ geometryGroup.materialIndex ]
  583. : object.material;
  584. };
  585. function materialNeedsSmoothNormals ( material ) {
  586. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  587. };
  588. // Buffer setting
  589. function setParticleBuffers ( geometry, hint, object ) {
  590. var v, c, vertex, offset, index, color,
  591. vertices = geometry.vertices,
  592. vl = vertices.length,
  593. colors = geometry.colors,
  594. cl = colors.length,
  595. vertexArray = geometry.__vertexArray,
  596. colorArray = geometry.__colorArray,
  597. sortArray = geometry.__sortArray,
  598. dirtyVertices = geometry.verticesNeedUpdate,
  599. dirtyElements = geometry.elementsNeedUpdate,
  600. dirtyColors = geometry.colorsNeedUpdate,
  601. customAttributes = geometry.__webglCustomAttributesList,
  602. i, il,
  603. a, ca, cal, value,
  604. customAttribute;
  605. if ( object.sortParticles ) {
  606. _projScreenMatrixPS.copy( _projScreenMatrix );
  607. _projScreenMatrixPS.multiply( object.matrixWorld );
  608. for ( v = 0; v < vl; v ++ ) {
  609. vertex = vertices[ v ];
  610. _vector3.copy( vertex );
  611. _vector3.applyProjection( _projScreenMatrixPS );
  612. sortArray[ v ] = [ _vector3.z, v ];
  613. }
  614. sortArray.sort( numericalSort );
  615. for ( v = 0; v < vl; v ++ ) {
  616. vertex = vertices[ sortArray[ v ][ 1 ] ];
  617. offset = v * 3;
  618. vertexArray[ offset ] = vertex.x;
  619. vertexArray[ offset + 1 ] = vertex.y;
  620. vertexArray[ offset + 2 ] = vertex.z;
  621. }
  622. for ( c = 0; c < cl; c ++ ) {
  623. offset = c * 3;
  624. color = colors[ sortArray[ c ][ 1 ] ];
  625. colorArray[ offset ] = color.r;
  626. colorArray[ offset + 1 ] = color.g;
  627. colorArray[ offset + 2 ] = color.b;
  628. }
  629. if ( customAttributes ) {
  630. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  631. customAttribute = customAttributes[ i ];
  632. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) ) continue;
  633. offset = 0;
  634. cal = customAttribute.value.length;
  635. if ( customAttribute.size === 1 ) {
  636. for ( ca = 0; ca < cal; ca ++ ) {
  637. index = sortArray[ ca ][ 1 ];
  638. customAttribute.array[ ca ] = customAttribute.value[ index ];
  639. }
  640. } else if ( customAttribute.size === 2 ) {
  641. for ( ca = 0; ca < cal; ca ++ ) {
  642. index = sortArray[ ca ][ 1 ];
  643. value = customAttribute.value[ index ];
  644. customAttribute.array[ offset ] = value.x;
  645. customAttribute.array[ offset + 1 ] = value.y;
  646. offset += 2;
  647. }
  648. } else if ( customAttribute.size === 3 ) {
  649. if ( customAttribute.type === 'c' ) {
  650. for ( ca = 0; ca < cal; ca ++ ) {
  651. index = sortArray[ ca ][ 1 ];
  652. value = customAttribute.value[ index ];
  653. customAttribute.array[ offset ] = value.r;
  654. customAttribute.array[ offset + 1 ] = value.g;
  655. customAttribute.array[ offset + 2 ] = value.b;
  656. offset += 3;
  657. }
  658. } else {
  659. for ( ca = 0; ca < cal; ca ++ ) {
  660. index = sortArray[ ca ][ 1 ];
  661. value = customAttribute.value[ index ];
  662. customAttribute.array[ offset ] = value.x;
  663. customAttribute.array[ offset + 1 ] = value.y;
  664. customAttribute.array[ offset + 2 ] = value.z;
  665. offset += 3;
  666. }
  667. }
  668. } else if ( customAttribute.size === 4 ) {
  669. for ( ca = 0; ca < cal; ca ++ ) {
  670. index = sortArray[ ca ][ 1 ];
  671. value = customAttribute.value[ index ];
  672. customAttribute.array[ offset ] = value.x;
  673. customAttribute.array[ offset + 1 ] = value.y;
  674. customAttribute.array[ offset + 2 ] = value.z;
  675. customAttribute.array[ offset + 3 ] = value.w;
  676. offset += 4;
  677. }
  678. }
  679. }
  680. }
  681. } else {
  682. if ( dirtyVertices ) {
  683. for ( v = 0; v < vl; v ++ ) {
  684. vertex = vertices[ v ];
  685. offset = v * 3;
  686. vertexArray[ offset ] = vertex.x;
  687. vertexArray[ offset + 1 ] = vertex.y;
  688. vertexArray[ offset + 2 ] = vertex.z;
  689. }
  690. }
  691. if ( dirtyColors ) {
  692. for ( c = 0; c < cl; c ++ ) {
  693. color = colors[ c ];
  694. offset = c * 3;
  695. colorArray[ offset ] = color.r;
  696. colorArray[ offset + 1 ] = color.g;
  697. colorArray[ offset + 2 ] = color.b;
  698. }
  699. }
  700. if ( customAttributes ) {
  701. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  702. customAttribute = customAttributes[ i ];
  703. if ( customAttribute.needsUpdate &&
  704. ( customAttribute.boundTo === undefined ||
  705. customAttribute.boundTo === 'vertices' ) ) {
  706. cal = customAttribute.value.length;
  707. offset = 0;
  708. if ( customAttribute.size === 1 ) {
  709. for ( ca = 0; ca < cal; ca ++ ) {
  710. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  711. }
  712. } else if ( customAttribute.size === 2 ) {
  713. for ( ca = 0; ca < cal; ca ++ ) {
  714. value = customAttribute.value[ ca ];
  715. customAttribute.array[ offset ] = value.x;
  716. customAttribute.array[ offset + 1 ] = value.y;
  717. offset += 2;
  718. }
  719. } else if ( customAttribute.size === 3 ) {
  720. if ( customAttribute.type === 'c' ) {
  721. for ( ca = 0; ca < cal; ca ++ ) {
  722. value = customAttribute.value[ ca ];
  723. customAttribute.array[ offset ] = value.r;
  724. customAttribute.array[ offset + 1 ] = value.g;
  725. customAttribute.array[ offset + 2 ] = value.b;
  726. offset += 3;
  727. }
  728. } else {
  729. for ( ca = 0; ca < cal; ca ++ ) {
  730. value = customAttribute.value[ ca ];
  731. customAttribute.array[ offset ] = value.x;
  732. customAttribute.array[ offset + 1 ] = value.y;
  733. customAttribute.array[ offset + 2 ] = value.z;
  734. offset += 3;
  735. }
  736. }
  737. } else if ( customAttribute.size === 4 ) {
  738. for ( ca = 0; ca < cal; ca ++ ) {
  739. value = customAttribute.value[ ca ];
  740. customAttribute.array[ offset ] = value.x;
  741. customAttribute.array[ offset + 1 ] = value.y;
  742. customAttribute.array[ offset + 2 ] = value.z;
  743. customAttribute.array[ offset + 3 ] = value.w;
  744. offset += 4;
  745. }
  746. }
  747. }
  748. }
  749. }
  750. }
  751. if ( dirtyVertices || object.sortParticles ) {
  752. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  753. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  754. }
  755. if ( dirtyColors || object.sortParticles ) {
  756. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  757. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  758. }
  759. if ( customAttributes ) {
  760. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  761. customAttribute = customAttributes[ i ];
  762. if ( customAttribute.needsUpdate || object.sortParticles ) {
  763. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  764. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  765. }
  766. }
  767. }
  768. }
  769. function setLineBuffers ( geometry, hint ) {
  770. var v, c, d, vertex, offset, color,
  771. vertices = geometry.vertices,
  772. colors = geometry.colors,
  773. lineDistances = geometry.lineDistances,
  774. vl = vertices.length,
  775. cl = colors.length,
  776. dl = lineDistances.length,
  777. vertexArray = geometry.__vertexArray,
  778. colorArray = geometry.__colorArray,
  779. lineDistanceArray = geometry.__lineDistanceArray,
  780. dirtyVertices = geometry.verticesNeedUpdate,
  781. dirtyColors = geometry.colorsNeedUpdate,
  782. dirtyLineDistances = geometry.lineDistancesNeedUpdate,
  783. customAttributes = geometry.__webglCustomAttributesList,
  784. i, il,
  785. a, ca, cal, value,
  786. customAttribute;
  787. if ( dirtyVertices ) {
  788. for ( v = 0; v < vl; v ++ ) {
  789. vertex = vertices[ v ];
  790. offset = v * 3;
  791. vertexArray[ offset ] = vertex.x;
  792. vertexArray[ offset + 1 ] = vertex.y;
  793. vertexArray[ offset + 2 ] = vertex.z;
  794. }
  795. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  796. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  797. }
  798. if ( dirtyColors ) {
  799. for ( c = 0; c < cl; c ++ ) {
  800. color = colors[ c ];
  801. offset = c * 3;
  802. colorArray[ offset ] = color.r;
  803. colorArray[ offset + 1 ] = color.g;
  804. colorArray[ offset + 2 ] = color.b;
  805. }
  806. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  807. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  808. }
  809. if ( dirtyLineDistances ) {
  810. for ( d = 0; d < dl; d ++ ) {
  811. lineDistanceArray[ d ] = lineDistances[ d ];
  812. }
  813. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer );
  814. _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint );
  815. }
  816. if ( customAttributes ) {
  817. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  818. customAttribute = customAttributes[ i ];
  819. if ( customAttribute.needsUpdate &&
  820. ( customAttribute.boundTo === undefined ||
  821. customAttribute.boundTo === 'vertices' ) ) {
  822. offset = 0;
  823. cal = customAttribute.value.length;
  824. if ( customAttribute.size === 1 ) {
  825. for ( ca = 0; ca < cal; ca ++ ) {
  826. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  827. }
  828. } else if ( customAttribute.size === 2 ) {
  829. for ( ca = 0; ca < cal; ca ++ ) {
  830. value = customAttribute.value[ ca ];
  831. customAttribute.array[ offset ] = value.x;
  832. customAttribute.array[ offset + 1 ] = value.y;
  833. offset += 2;
  834. }
  835. } else if ( customAttribute.size === 3 ) {
  836. if ( customAttribute.type === 'c' ) {
  837. for ( ca = 0; ca < cal; ca ++ ) {
  838. value = customAttribute.value[ ca ];
  839. customAttribute.array[ offset ] = value.r;
  840. customAttribute.array[ offset + 1 ] = value.g;
  841. customAttribute.array[ offset + 2 ] = value.b;
  842. offset += 3;
  843. }
  844. } else {
  845. for ( ca = 0; ca < cal; ca ++ ) {
  846. value = customAttribute.value[ ca ];
  847. customAttribute.array[ offset ] = value.x;
  848. customAttribute.array[ offset + 1 ] = value.y;
  849. customAttribute.array[ offset + 2 ] = value.z;
  850. offset += 3;
  851. }
  852. }
  853. } else if ( customAttribute.size === 4 ) {
  854. for ( ca = 0; ca < cal; ca ++ ) {
  855. value = customAttribute.value[ ca ];
  856. customAttribute.array[ offset ] = value.x;
  857. customAttribute.array[ offset + 1 ] = value.y;
  858. customAttribute.array[ offset + 2 ] = value.z;
  859. customAttribute.array[ offset + 3 ] = value.w;
  860. offset += 4;
  861. }
  862. }
  863. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  864. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  865. }
  866. }
  867. }
  868. }
  869. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  870. if ( ! geometryGroup.__inittedArrays ) {
  871. return;
  872. }
  873. var needsSmoothNormals = materialNeedsSmoothNormals( material );
  874. var f, fl, fi, face,
  875. vertexNormals, faceNormal, normal,
  876. vertexColors, faceColor,
  877. vertexTangents,
  878. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  879. c1, c2, c3,
  880. sw1, sw2, sw3, sw4,
  881. si1, si2, si3, si4,
  882. sa1, sa2, sa3, sa4,
  883. sb1, sb2, sb3, sb4,
  884. m, ml, i, il,
  885. vn, uvi, uv2i,
  886. vk, vkl, vka,
  887. nka, chf, faceVertexNormals,
  888. a,
  889. vertexIndex = 0,
  890. offset = 0,
  891. offset_uv = 0,
  892. offset_uv2 = 0,
  893. offset_face = 0,
  894. offset_normal = 0,
  895. offset_tangent = 0,
  896. offset_line = 0,
  897. offset_color = 0,
  898. offset_skin = 0,
  899. offset_morphTarget = 0,
  900. offset_custom = 0,
  901. offset_customSrc = 0,
  902. value,
  903. vertexArray = geometryGroup.__vertexArray,
  904. uvArray = geometryGroup.__uvArray,
  905. uv2Array = geometryGroup.__uv2Array,
  906. normalArray = geometryGroup.__normalArray,
  907. tangentArray = geometryGroup.__tangentArray,
  908. colorArray = geometryGroup.__colorArray,
  909. skinIndexArray = geometryGroup.__skinIndexArray,
  910. skinWeightArray = geometryGroup.__skinWeightArray,
  911. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  912. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  913. customAttributes = geometryGroup.__webglCustomAttributesList,
  914. customAttribute,
  915. faceArray = geometryGroup.__faceArray,
  916. lineArray = geometryGroup.__lineArray,
  917. geometry = object.geometry, // this is shared for all chunks
  918. dirtyVertices = geometry.verticesNeedUpdate,
  919. dirtyElements = geometry.elementsNeedUpdate,
  920. dirtyUvs = geometry.uvsNeedUpdate,
  921. dirtyNormals = geometry.normalsNeedUpdate,
  922. dirtyTangents = geometry.tangentsNeedUpdate,
  923. dirtyColors = geometry.colorsNeedUpdate,
  924. dirtyMorphTargets = geometry.morphTargetsNeedUpdate,
  925. vertices = geometry.vertices,
  926. chunk_faces3 = geometryGroup.faces3,
  927. obj_faces = geometry.faces,
  928. obj_uvs = geometry.faceVertexUvs[ 0 ],
  929. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  930. obj_colors = geometry.colors,
  931. obj_skinIndices = geometry.skinIndices,
  932. obj_skinWeights = geometry.skinWeights,
  933. morphTargets = geometry.morphTargets,
  934. morphNormals = geometry.morphNormals;
  935. if ( dirtyVertices ) {
  936. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  937. face = obj_faces[ chunk_faces3[ f ] ];
  938. v1 = vertices[ face.a ];
  939. v2 = vertices[ face.b ];
  940. v3 = vertices[ face.c ];
  941. vertexArray[ offset ] = v1.x;
  942. vertexArray[ offset + 1 ] = v1.y;
  943. vertexArray[ offset + 2 ] = v1.z;
  944. vertexArray[ offset + 3 ] = v2.x;
  945. vertexArray[ offset + 4 ] = v2.y;
  946. vertexArray[ offset + 5 ] = v2.z;
  947. vertexArray[ offset + 6 ] = v3.x;
  948. vertexArray[ offset + 7 ] = v3.y;
  949. vertexArray[ offset + 8 ] = v3.z;
  950. offset += 9;
  951. }
  952. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  953. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  954. }
  955. if ( dirtyMorphTargets ) {
  956. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  957. offset_morphTarget = 0;
  958. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  959. chf = chunk_faces3[ f ];
  960. face = obj_faces[ chf ];
  961. // morph positions
  962. v1 = morphTargets[ vk ].vertices[ face.a ];
  963. v2 = morphTargets[ vk ].vertices[ face.b ];
  964. v3 = morphTargets[ vk ].vertices[ face.c ];
  965. vka = morphTargetsArrays[ vk ];
  966. vka[ offset_morphTarget ] = v1.x;
  967. vka[ offset_morphTarget + 1 ] = v1.y;
  968. vka[ offset_morphTarget + 2 ] = v1.z;
  969. vka[ offset_morphTarget + 3 ] = v2.x;
  970. vka[ offset_morphTarget + 4 ] = v2.y;
  971. vka[ offset_morphTarget + 5 ] = v2.z;
  972. vka[ offset_morphTarget + 6 ] = v3.x;
  973. vka[ offset_morphTarget + 7 ] = v3.y;
  974. vka[ offset_morphTarget + 8 ] = v3.z;
  975. // morph normals
  976. if ( material.morphNormals ) {
  977. if ( needsSmoothNormals ) {
  978. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  979. n1 = faceVertexNormals.a;
  980. n2 = faceVertexNormals.b;
  981. n3 = faceVertexNormals.c;
  982. } else {
  983. n1 = morphNormals[ vk ].faceNormals[ chf ];
  984. n2 = n1;
  985. n3 = n1;
  986. }
  987. nka = morphNormalsArrays[ vk ];
  988. nka[ offset_morphTarget ] = n1.x;
  989. nka[ offset_morphTarget + 1 ] = n1.y;
  990. nka[ offset_morphTarget + 2 ] = n1.z;
  991. nka[ offset_morphTarget + 3 ] = n2.x;
  992. nka[ offset_morphTarget + 4 ] = n2.y;
  993. nka[ offset_morphTarget + 5 ] = n2.z;
  994. nka[ offset_morphTarget + 6 ] = n3.x;
  995. nka[ offset_morphTarget + 7 ] = n3.y;
  996. nka[ offset_morphTarget + 8 ] = n3.z;
  997. }
  998. //
  999. offset_morphTarget += 9;
  1000. }
  1001. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  1002. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  1003. if ( material.morphNormals ) {
  1004. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  1005. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  1006. }
  1007. }
  1008. }
  1009. if ( obj_skinWeights.length ) {
  1010. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1011. face = obj_faces[ chunk_faces3[ f ] ];
  1012. // weights
  1013. sw1 = obj_skinWeights[ face.a ];
  1014. sw2 = obj_skinWeights[ face.b ];
  1015. sw3 = obj_skinWeights[ face.c ];
  1016. skinWeightArray[ offset_skin ] = sw1.x;
  1017. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  1018. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  1019. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  1020. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  1021. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  1022. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  1023. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  1024. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  1025. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  1026. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  1027. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  1028. // indices
  1029. si1 = obj_skinIndices[ face.a ];
  1030. si2 = obj_skinIndices[ face.b ];
  1031. si3 = obj_skinIndices[ face.c ];
  1032. skinIndexArray[ offset_skin ] = si1.x;
  1033. skinIndexArray[ offset_skin + 1 ] = si1.y;
  1034. skinIndexArray[ offset_skin + 2 ] = si1.z;
  1035. skinIndexArray[ offset_skin + 3 ] = si1.w;
  1036. skinIndexArray[ offset_skin + 4 ] = si2.x;
  1037. skinIndexArray[ offset_skin + 5 ] = si2.y;
  1038. skinIndexArray[ offset_skin + 6 ] = si2.z;
  1039. skinIndexArray[ offset_skin + 7 ] = si2.w;
  1040. skinIndexArray[ offset_skin + 8 ] = si3.x;
  1041. skinIndexArray[ offset_skin + 9 ] = si3.y;
  1042. skinIndexArray[ offset_skin + 10 ] = si3.z;
  1043. skinIndexArray[ offset_skin + 11 ] = si3.w;
  1044. offset_skin += 12;
  1045. }
  1046. if ( offset_skin > 0 ) {
  1047. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1048. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  1049. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1050. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  1051. }
  1052. }
  1053. if ( dirtyColors ) {
  1054. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1055. face = obj_faces[ chunk_faces3[ f ] ];
  1056. vertexColors = face.vertexColors;
  1057. faceColor = face.color;
  1058. if ( vertexColors.length === 3 && material.vertexColors === THREE.VertexColors ) {
  1059. c1 = vertexColors[ 0 ];
  1060. c2 = vertexColors[ 1 ];
  1061. c3 = vertexColors[ 2 ];
  1062. } else {
  1063. c1 = faceColor;
  1064. c2 = faceColor;
  1065. c3 = faceColor;
  1066. }
  1067. colorArray[ offset_color ] = c1.r;
  1068. colorArray[ offset_color + 1 ] = c1.g;
  1069. colorArray[ offset_color + 2 ] = c1.b;
  1070. colorArray[ offset_color + 3 ] = c2.r;
  1071. colorArray[ offset_color + 4 ] = c2.g;
  1072. colorArray[ offset_color + 5 ] = c2.b;
  1073. colorArray[ offset_color + 6 ] = c3.r;
  1074. colorArray[ offset_color + 7 ] = c3.g;
  1075. colorArray[ offset_color + 8 ] = c3.b;
  1076. offset_color += 9;
  1077. }
  1078. if ( offset_color > 0 ) {
  1079. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1080. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1081. }
  1082. }
  1083. if ( dirtyTangents && geometry.hasTangents ) {
  1084. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1085. face = obj_faces[ chunk_faces3[ f ] ];
  1086. vertexTangents = face.vertexTangents;
  1087. t1 = vertexTangents[ 0 ];
  1088. t2 = vertexTangents[ 1 ];
  1089. t3 = vertexTangents[ 2 ];
  1090. tangentArray[ offset_tangent ] = t1.x;
  1091. tangentArray[ offset_tangent + 1 ] = t1.y;
  1092. tangentArray[ offset_tangent + 2 ] = t1.z;
  1093. tangentArray[ offset_tangent + 3 ] = t1.w;
  1094. tangentArray[ offset_tangent + 4 ] = t2.x;
  1095. tangentArray[ offset_tangent + 5 ] = t2.y;
  1096. tangentArray[ offset_tangent + 6 ] = t2.z;
  1097. tangentArray[ offset_tangent + 7 ] = t2.w;
  1098. tangentArray[ offset_tangent + 8 ] = t3.x;
  1099. tangentArray[ offset_tangent + 9 ] = t3.y;
  1100. tangentArray[ offset_tangent + 10 ] = t3.z;
  1101. tangentArray[ offset_tangent + 11 ] = t3.w;
  1102. offset_tangent += 12;
  1103. }
  1104. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1105. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  1106. }
  1107. if ( dirtyNormals ) {
  1108. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1109. face = obj_faces[ chunk_faces3[ f ] ];
  1110. vertexNormals = face.vertexNormals;
  1111. faceNormal = face.normal;
  1112. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  1113. for ( i = 0; i < 3; i ++ ) {
  1114. vn = vertexNormals[ i ];
  1115. normalArray[ offset_normal ] = vn.x;
  1116. normalArray[ offset_normal + 1 ] = vn.y;
  1117. normalArray[ offset_normal + 2 ] = vn.z;
  1118. offset_normal += 3;
  1119. }
  1120. } else {
  1121. for ( i = 0; i < 3; i ++ ) {
  1122. normalArray[ offset_normal ] = faceNormal.x;
  1123. normalArray[ offset_normal + 1 ] = faceNormal.y;
  1124. normalArray[ offset_normal + 2 ] = faceNormal.z;
  1125. offset_normal += 3;
  1126. }
  1127. }
  1128. }
  1129. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1130. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  1131. }
  1132. if ( dirtyUvs && obj_uvs ) {
  1133. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1134. fi = chunk_faces3[ f ];
  1135. uv = obj_uvs[ fi ];
  1136. if ( uv === undefined ) continue;
  1137. for ( i = 0; i < 3; i ++ ) {
  1138. uvi = uv[ i ];
  1139. uvArray[ offset_uv ] = uvi.x;
  1140. uvArray[ offset_uv + 1 ] = uvi.y;
  1141. offset_uv += 2;
  1142. }
  1143. }
  1144. if ( offset_uv > 0 ) {
  1145. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1146. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  1147. }
  1148. }
  1149. if ( dirtyUvs && obj_uvs2 ) {
  1150. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1151. fi = chunk_faces3[ f ];
  1152. uv2 = obj_uvs2[ fi ];
  1153. if ( uv2 === undefined ) continue;
  1154. for ( i = 0; i < 3; i ++ ) {
  1155. uv2i = uv2[ i ];
  1156. uv2Array[ offset_uv2 ] = uv2i.x;
  1157. uv2Array[ offset_uv2 + 1 ] = uv2i.y;
  1158. offset_uv2 += 2;
  1159. }
  1160. }
  1161. if ( offset_uv2 > 0 ) {
  1162. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1163. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  1164. }
  1165. }
  1166. if ( dirtyElements ) {
  1167. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1168. faceArray[ offset_face ] = vertexIndex;
  1169. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  1170. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  1171. offset_face += 3;
  1172. lineArray[ offset_line ] = vertexIndex;
  1173. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  1174. lineArray[ offset_line + 2 ] = vertexIndex;
  1175. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  1176. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  1177. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  1178. offset_line += 6;
  1179. vertexIndex += 3;
  1180. }
  1181. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1182. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  1183. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1184. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  1185. }
  1186. if ( customAttributes ) {
  1187. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  1188. customAttribute = customAttributes[ i ];
  1189. if ( ! customAttribute.__original.needsUpdate ) continue;
  1190. offset_custom = 0;
  1191. offset_customSrc = 0;
  1192. if ( customAttribute.size === 1 ) {
  1193. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1194. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1195. face = obj_faces[ chunk_faces3[ f ] ];
  1196. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  1197. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  1198. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  1199. offset_custom += 3;
  1200. }
  1201. } else if ( customAttribute.boundTo === 'faces' ) {
  1202. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1203. value = customAttribute.value[ chunk_faces3[ f ] ];
  1204. customAttribute.array[ offset_custom ] = value;
  1205. customAttribute.array[ offset_custom + 1 ] = value;
  1206. customAttribute.array[ offset_custom + 2 ] = value;
  1207. offset_custom += 3;
  1208. }
  1209. }
  1210. } else if ( customAttribute.size === 2 ) {
  1211. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1212. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1213. face = obj_faces[ chunk_faces3[ f ] ];
  1214. v1 = customAttribute.value[ face.a ];
  1215. v2 = customAttribute.value[ face.b ];
  1216. v3 = customAttribute.value[ face.c ];
  1217. customAttribute.array[ offset_custom ] = v1.x;
  1218. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1219. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1220. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1221. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1222. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1223. offset_custom += 6;
  1224. }
  1225. } else if ( customAttribute.boundTo === 'faces' ) {
  1226. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1227. value = customAttribute.value[ chunk_faces3[ f ] ];
  1228. v1 = value;
  1229. v2 = value;
  1230. v3 = value;
  1231. customAttribute.array[ offset_custom ] = v1.x;
  1232. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1233. customAttribute.array[ offset_custom + 2 ] = v2.x;
  1234. customAttribute.array[ offset_custom + 3 ] = v2.y;
  1235. customAttribute.array[ offset_custom + 4 ] = v3.x;
  1236. customAttribute.array[ offset_custom + 5 ] = v3.y;
  1237. offset_custom += 6;
  1238. }
  1239. }
  1240. } else if ( customAttribute.size === 3 ) {
  1241. var pp;
  1242. if ( customAttribute.type === 'c' ) {
  1243. pp = [ 'r', 'g', 'b' ];
  1244. } else {
  1245. pp = [ 'x', 'y', 'z' ];
  1246. }
  1247. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1248. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1249. face = obj_faces[ chunk_faces3[ f ] ];
  1250. v1 = customAttribute.value[ face.a ];
  1251. v2 = customAttribute.value[ face.b ];
  1252. v3 = customAttribute.value[ face.c ];
  1253. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1254. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1255. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1256. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1257. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1258. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1259. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1260. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1261. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1262. offset_custom += 9;
  1263. }
  1264. } else if ( customAttribute.boundTo === 'faces' ) {
  1265. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1266. value = customAttribute.value[ chunk_faces3[ f ] ];
  1267. v1 = value;
  1268. v2 = value;
  1269. v3 = value;
  1270. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1271. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1272. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1273. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1274. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1275. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1276. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1277. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1278. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1279. offset_custom += 9;
  1280. }
  1281. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  1282. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1283. value = customAttribute.value[ chunk_faces3[ f ] ];
  1284. v1 = value[ 0 ];
  1285. v2 = value[ 1 ];
  1286. v3 = value[ 2 ];
  1287. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  1288. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  1289. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  1290. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  1291. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  1292. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  1293. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  1294. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  1295. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  1296. offset_custom += 9;
  1297. }
  1298. }
  1299. } else if ( customAttribute.size === 4 ) {
  1300. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  1301. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1302. face = obj_faces[ chunk_faces3[ f ] ];
  1303. v1 = customAttribute.value[ face.a ];
  1304. v2 = customAttribute.value[ face.b ];
  1305. v3 = customAttribute.value[ face.c ];
  1306. customAttribute.array[ offset_custom ] = v1.x;
  1307. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1308. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1309. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1310. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1311. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1312. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1313. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1314. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1315. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1316. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1317. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1318. offset_custom += 12;
  1319. }
  1320. } else if ( customAttribute.boundTo === 'faces' ) {
  1321. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1322. value = customAttribute.value[ chunk_faces3[ f ] ];
  1323. v1 = value;
  1324. v2 = value;
  1325. v3 = value;
  1326. customAttribute.array[ offset_custom ] = v1.x;
  1327. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1328. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1329. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1330. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1331. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1332. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1333. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1334. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1335. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1336. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1337. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1338. offset_custom += 12;
  1339. }
  1340. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  1341. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  1342. value = customAttribute.value[ chunk_faces3[ f ] ];
  1343. v1 = value[ 0 ];
  1344. v2 = value[ 1 ];
  1345. v3 = value[ 2 ];
  1346. customAttribute.array[ offset_custom ] = v1.x;
  1347. customAttribute.array[ offset_custom + 1 ] = v1.y;
  1348. customAttribute.array[ offset_custom + 2 ] = v1.z;
  1349. customAttribute.array[ offset_custom + 3 ] = v1.w;
  1350. customAttribute.array[ offset_custom + 4 ] = v2.x;
  1351. customAttribute.array[ offset_custom + 5 ] = v2.y;
  1352. customAttribute.array[ offset_custom + 6 ] = v2.z;
  1353. customAttribute.array[ offset_custom + 7 ] = v2.w;
  1354. customAttribute.array[ offset_custom + 8 ] = v3.x;
  1355. customAttribute.array[ offset_custom + 9 ] = v3.y;
  1356. customAttribute.array[ offset_custom + 10 ] = v3.z;
  1357. customAttribute.array[ offset_custom + 11 ] = v3.w;
  1358. offset_custom += 12;
  1359. }
  1360. }
  1361. }
  1362. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  1363. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  1364. }
  1365. }
  1366. if ( dispose ) {
  1367. delete geometryGroup.__inittedArrays;
  1368. delete geometryGroup.__colorArray;
  1369. delete geometryGroup.__normalArray;
  1370. delete geometryGroup.__tangentArray;
  1371. delete geometryGroup.__uvArray;
  1372. delete geometryGroup.__uv2Array;
  1373. delete geometryGroup.__faceArray;
  1374. delete geometryGroup.__vertexArray;
  1375. delete geometryGroup.__lineArray;
  1376. delete geometryGroup.__skinIndexArray;
  1377. delete geometryGroup.__skinWeightArray;
  1378. }
  1379. };
  1380. function setDirectBuffers( geometry ) {
  1381. var attributes = geometry.attributes;
  1382. var attributesKeys = geometry.attributesKeys;
  1383. for ( var i = 0, l = attributesKeys.length; i < l; i ++ ) {
  1384. var attributeName = attributesKeys[ i ];
  1385. var attributeItem = attributes[ attributeName ];
  1386. if ( attributeItem.buffer === undefined ) {
  1387. attributeItem.buffer = _gl.createBuffer();
  1388. attributeItem.needsUpdate = true;
  1389. }
  1390. if ( attributeItem.needsUpdate === true ) {
  1391. var bufferType = ( attributeName === 'index' ) ? _gl.ELEMENT_ARRAY_BUFFER : _gl.ARRAY_BUFFER;
  1392. _gl.bindBuffer( bufferType, attributeItem.buffer );
  1393. _gl.bufferData( bufferType, attributeItem.array, _gl.STATIC_DRAW );
  1394. attributeItem.needsUpdate = false;
  1395. }
  1396. }
  1397. }
  1398. // Buffer rendering
  1399. this.renderBufferImmediate = function ( object, program, material ) {
  1400. initAttributes();
  1401. if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  1402. if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  1403. if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
  1404. if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
  1405. if ( object.hasPositions ) {
  1406. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  1407. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  1408. enableAttribute( program.attributes.position );
  1409. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1410. }
  1411. if ( object.hasNormals ) {
  1412. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  1413. if ( material.shading === THREE.FlatShading ) {
  1414. var nx, ny, nz,
  1415. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  1416. normalArray,
  1417. i, il = object.count * 3;
  1418. for ( i = 0; i < il; i += 9 ) {
  1419. normalArray = object.normalArray;
  1420. nax = normalArray[ i ];
  1421. nay = normalArray[ i + 1 ];
  1422. naz = normalArray[ i + 2 ];
  1423. nbx = normalArray[ i + 3 ];
  1424. nby = normalArray[ i + 4 ];
  1425. nbz = normalArray[ i + 5 ];
  1426. ncx = normalArray[ i + 6 ];
  1427. ncy = normalArray[ i + 7 ];
  1428. ncz = normalArray[ i + 8 ];
  1429. nx = ( nax + nbx + ncx ) / 3;
  1430. ny = ( nay + nby + ncy ) / 3;
  1431. nz = ( naz + nbz + ncz ) / 3;
  1432. normalArray[ i ] = nx;
  1433. normalArray[ i + 1 ] = ny;
  1434. normalArray[ i + 2 ] = nz;
  1435. normalArray[ i + 3 ] = nx;
  1436. normalArray[ i + 4 ] = ny;
  1437. normalArray[ i + 5 ] = nz;
  1438. normalArray[ i + 6 ] = nx;
  1439. normalArray[ i + 7 ] = ny;
  1440. normalArray[ i + 8 ] = nz;
  1441. }
  1442. }
  1443. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  1444. enableAttribute( program.attributes.normal );
  1445. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1446. }
  1447. if ( object.hasUvs && material.map ) {
  1448. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
  1449. _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
  1450. enableAttribute( program.attributes.uv );
  1451. _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1452. }
  1453. if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
  1454. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
  1455. _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
  1456. enableAttribute( program.attributes.color );
  1457. _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1458. }
  1459. disableUnusedAttributes();
  1460. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  1461. object.count = 0;
  1462. };
  1463. function setupVertexAttributes( material, program, geometry, startIndex ) {
  1464. var geometryAttributes = geometry.attributes;
  1465. var programAttributes = program.attributes;
  1466. var programAttributesKeys = program.attributesKeys;
  1467. for ( var i = 0, l = programAttributesKeys.length; i < l; i ++ ) {
  1468. var attributeName = programAttributesKeys[ i ];
  1469. var attributePointer = programAttributes[ attributeName ];
  1470. if ( attributePointer >= 0 ) {
  1471. var attributeItem = geometryAttributes[ attributeName ];
  1472. if ( attributeItem !== undefined ) {
  1473. var attributeSize = attributeItem.itemSize;
  1474. _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
  1475. enableAttribute( attributePointer );
  1476. _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32
  1477. } else if ( material.defaultAttributeValues !== undefined ) {
  1478. if ( material.defaultAttributeValues[ attributeName ].length === 2 ) {
  1479. _gl.vertexAttrib2fv( attributePointer, material.defaultAttributeValues[ attributeName ] );
  1480. } else if ( material.defaultAttributeValues[ attributeName ].length === 3 ) {
  1481. _gl.vertexAttrib3fv( attributePointer, material.defaultAttributeValues[ attributeName ] );
  1482. }
  1483. }
  1484. }
  1485. }
  1486. disableUnusedAttributes();
  1487. }
  1488. this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
  1489. if ( material.visible === false ) return;
  1490. var linewidth, a, attribute;
  1491. var attributeItem, attributeName, attributePointer, attributeSize;
  1492. var program = setProgram( camera, lights, fog, material, object );
  1493. var updateBuffers = false,
  1494. wireframeBit = material.wireframe ? 1 : 0,
  1495. geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  1496. if ( geometryHash !== _currentGeometryGroupHash ) {
  1497. _currentGeometryGroupHash = geometryHash;
  1498. updateBuffers = true;
  1499. }
  1500. if ( updateBuffers ) {
  1501. initAttributes();
  1502. }
  1503. // render mesh
  1504. if ( object instanceof THREE.Mesh ) {
  1505. var mode = material.wireframe === true ? _gl.LINES : _gl.TRIANGLES;
  1506. var index = geometry.attributes.index;
  1507. if ( index ) {
  1508. // indexed triangles
  1509. var type, size;
  1510. if ( index.array instanceof Uint32Array ) {
  1511. type = _gl.UNSIGNED_INT;
  1512. size = 4;
  1513. } else {
  1514. type = _gl.UNSIGNED_SHORT;
  1515. size = 2;
  1516. }
  1517. var offsets = geometry.offsets;
  1518. if ( offsets.length === 0 ) {
  1519. if ( updateBuffers ) {
  1520. setupVertexAttributes( material, program, geometry, 0 );
  1521. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1522. }
  1523. _gl.drawElements( mode, index.array.length, type, 0 );
  1524. _this.info.render.calls ++;
  1525. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  1526. _this.info.render.faces += index.array.length / 3;
  1527. } else {
  1528. // if there is more than 1 chunk
  1529. // must set attribute pointers to use new offsets for each chunk
  1530. // even if geometry and materials didn't change
  1531. updateBuffers = true;
  1532. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  1533. var startIndex = offsets[ i ].index;
  1534. if ( updateBuffers ) {
  1535. setupVertexAttributes( material, program, geometry, startIndex );
  1536. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1537. }
  1538. // render indexed triangles
  1539. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
  1540. _this.info.render.calls ++;
  1541. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  1542. _this.info.render.faces += offsets[ i ].count / 3;
  1543. }
  1544. }
  1545. } else {
  1546. // non-indexed triangles
  1547. if ( updateBuffers ) {
  1548. setupVertexAttributes( material, program, geometry, 0 );
  1549. }
  1550. var position = geometry.attributes[ 'position' ];
  1551. // render non-indexed triangles
  1552. _gl.drawArrays( mode, 0, position.array.length / 3 );
  1553. _this.info.render.calls ++;
  1554. _this.info.render.vertices += position.array.length / 3;
  1555. _this.info.render.faces += position.array.length / 9;
  1556. }
  1557. } else if ( object instanceof THREE.PointCloud ) {
  1558. // render particles
  1559. if ( updateBuffers ) {
  1560. setupVertexAttributes( material, program, geometry, 0 );
  1561. }
  1562. var position = geometry.attributes.position;
  1563. // render particles
  1564. _gl.drawArrays( _gl.POINTS, 0, position.array.length / 3 );
  1565. _this.info.render.calls ++;
  1566. _this.info.render.points += position.array.length / 3;
  1567. } else if ( object instanceof THREE.Line ) {
  1568. var mode = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1569. setLineWidth( material.linewidth );
  1570. var index = geometry.attributes.index;
  1571. if ( index ) {
  1572. // indexed lines
  1573. var type, size;
  1574. if ( index.array instanceof Uint32Array ) {
  1575. type = _gl.UNSIGNED_INT;
  1576. size = 4;
  1577. } else {
  1578. type = _gl.UNSIGNED_SHORT;
  1579. size = 2;
  1580. }
  1581. var offsets = geometry.offsets;
  1582. if ( offsets.length === 0 ) {
  1583. if ( updateBuffers ) {
  1584. setupVertexAttributes( material, program, geometry, 0 );
  1585. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1586. }
  1587. _gl.drawElements( mode, index.array.length, type, 0 ); // 2 bytes per Uint16Array
  1588. _this.info.render.calls ++;
  1589. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  1590. } else {
  1591. // if there is more than 1 chunk
  1592. // must set attribute pointers to use new offsets for each chunk
  1593. // even if geometry and materials didn't change
  1594. if ( offsets.length > 1 ) updateBuffers = true;
  1595. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  1596. var startIndex = offsets[ i ].index;
  1597. if ( updateBuffers ) {
  1598. setupVertexAttributes( material, program, geometry, startIndex );
  1599. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  1600. }
  1601. // render indexed lines
  1602. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size ); // 2 bytes per Uint16Array
  1603. _this.info.render.calls ++;
  1604. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  1605. }
  1606. }
  1607. } else {
  1608. // non-indexed lines
  1609. if ( updateBuffers ) {
  1610. setupVertexAttributes( material, program, geometry, 0 );
  1611. }
  1612. var position = geometry.attributes.position;
  1613. _gl.drawArrays( mode, 0, position.array.length / 3 );
  1614. _this.info.render.calls ++;
  1615. _this.info.render.points += position.array.length / 3;
  1616. }
  1617. }
  1618. };
  1619. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  1620. if ( material.visible === false ) return;
  1621. var linewidth, a, attribute, i, il;
  1622. var program = setProgram( camera, lights, fog, material, object );
  1623. var attributes = program.attributes;
  1624. var updateBuffers = false,
  1625. wireframeBit = material.wireframe ? 1 : 0,
  1626. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  1627. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  1628. _currentGeometryGroupHash = geometryGroupHash;
  1629. updateBuffers = true;
  1630. }
  1631. if ( updateBuffers ) {
  1632. initAttributes();
  1633. }
  1634. // vertices
  1635. if ( ! material.morphTargets && attributes.position >= 0 ) {
  1636. if ( updateBuffers ) {
  1637. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1638. enableAttribute( attributes.position );
  1639. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1640. }
  1641. } else {
  1642. if ( object.morphTargetBase ) {
  1643. setupMorphTargets( material, geometryGroup, object );
  1644. }
  1645. }
  1646. if ( updateBuffers ) {
  1647. // custom attributes
  1648. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  1649. if ( geometryGroup.__webglCustomAttributesList ) {
  1650. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  1651. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  1652. if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  1653. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  1654. enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
  1655. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  1656. }
  1657. }
  1658. }
  1659. // colors
  1660. if ( attributes.color >= 0 ) {
  1661. if ( object.geometry.colors.length > 0 || object.geometry.faces.length > 0 ) {
  1662. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1663. enableAttribute( attributes.color );
  1664. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1665. } else if ( material.defaultAttributeValues !== undefined ) {
  1666. _gl.vertexAttrib3fv( attributes.color, material.defaultAttributeValues.color );
  1667. }
  1668. }
  1669. // normals
  1670. if ( attributes.normal >= 0 ) {
  1671. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1672. enableAttribute( attributes.normal );
  1673. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1674. }
  1675. // tangents
  1676. if ( attributes.tangent >= 0 ) {
  1677. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1678. enableAttribute( attributes.tangent );
  1679. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  1680. }
  1681. // uvs
  1682. if ( attributes.uv >= 0 ) {
  1683. if ( object.geometry.faceVertexUvs[ 0 ] ) {
  1684. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1685. enableAttribute( attributes.uv );
  1686. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1687. } else if ( material.defaultAttributeValues !== undefined ) {
  1688. _gl.vertexAttrib2fv( attributes.uv, material.defaultAttributeValues.uv );
  1689. }
  1690. }
  1691. if ( attributes.uv2 >= 0 ) {
  1692. if ( object.geometry.faceVertexUvs[ 1 ] ) {
  1693. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1694. enableAttribute( attributes.uv2 );
  1695. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  1696. } else if ( material.defaultAttributeValues !== undefined ) {
  1697. _gl.vertexAttrib2fv( attributes.uv2, material.defaultAttributeValues.uv2 );
  1698. }
  1699. }
  1700. if ( material.skinning &&
  1701. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1702. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1703. enableAttribute( attributes.skinIndex );
  1704. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  1705. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1706. enableAttribute( attributes.skinWeight );
  1707. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  1708. }
  1709. // line distances
  1710. if ( attributes.lineDistance >= 0 ) {
  1711. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
  1712. enableAttribute( attributes.lineDistance );
  1713. _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
  1714. }
  1715. }
  1716. disableUnusedAttributes();
  1717. // render mesh
  1718. if ( object instanceof THREE.Mesh ) {
  1719. var type = geometryGroup.__typeArray === Uint32Array ? _gl.UNSIGNED_INT : _gl.UNSIGNED_SHORT;
  1720. // wireframe
  1721. if ( material.wireframe ) {
  1722. setLineWidth( material.wireframeLinewidth );
  1723. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1724. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, type, 0 );
  1725. // triangles
  1726. } else {
  1727. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1728. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, type, 0 );
  1729. }
  1730. _this.info.render.calls ++;
  1731. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  1732. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  1733. // render lines
  1734. } else if ( object instanceof THREE.Line ) {
  1735. var mode = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1736. setLineWidth( material.linewidth );
  1737. _gl.drawArrays( mode, 0, geometryGroup.__webglLineCount );
  1738. _this.info.render.calls ++;
  1739. // render particles
  1740. } else if ( object instanceof THREE.PointCloud ) {
  1741. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  1742. _this.info.render.calls ++;
  1743. _this.info.render.points += geometryGroup.__webglParticleCount;
  1744. }
  1745. };
  1746. function initAttributes() {
  1747. for ( var i = 0, l = _newAttributes.length; i < l; i ++ ) {
  1748. _newAttributes[ i ] = 0;
  1749. }
  1750. }
  1751. function enableAttribute( attribute ) {
  1752. _newAttributes[ attribute ] = 1;
  1753. if ( _enabledAttributes[ attribute ] === 0 ) {
  1754. _gl.enableVertexAttribArray( attribute );
  1755. _enabledAttributes[ attribute ] = 1;
  1756. }
  1757. }
  1758. function disableUnusedAttributes() {
  1759. for ( var i = 0, l = _enabledAttributes.length; i < l; i ++ ) {
  1760. if ( _enabledAttributes[ i ] !== _newAttributes[ i ] ) {
  1761. _gl.disableVertexAttribArray( i );
  1762. _enabledAttributes[ i ] = 0;
  1763. }
  1764. }
  1765. }
  1766. function setupMorphTargets ( material, geometryGroup, object ) {
  1767. // set base
  1768. var attributes = material.program.attributes;
  1769. if ( object.morphTargetBase !== - 1 && attributes.position >= 0 ) {
  1770. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  1771. enableAttribute( attributes.position );
  1772. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1773. } else if ( attributes.position >= 0 ) {
  1774. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1775. enableAttribute( attributes.position );
  1776. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1777. }
  1778. if ( object.morphTargetForcedOrder.length ) {
  1779. // set forced order
  1780. var m = 0;
  1781. var order = object.morphTargetForcedOrder;
  1782. var influences = object.morphTargetInfluences;
  1783. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  1784. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  1785. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  1786. enableAttribute( attributes[ 'morphTarget' + m ] );
  1787. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1788. }
  1789. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  1790. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  1791. enableAttribute( attributes[ 'morphNormal' + m ] );
  1792. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1793. }
  1794. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  1795. m ++;
  1796. }
  1797. } else {
  1798. // find the most influencing
  1799. var influence, activeInfluenceIndices = [];
  1800. var influences = object.morphTargetInfluences;
  1801. var i, il = influences.length;
  1802. for ( i = 0; i < il; i ++ ) {
  1803. influence = influences[ i ];
  1804. if ( influence > 0 ) {
  1805. activeInfluenceIndices.push( [ influence, i ] );
  1806. }
  1807. }
  1808. if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
  1809. activeInfluenceIndices.sort( numericalSort );
  1810. activeInfluenceIndices.length = material.numSupportedMorphTargets;
  1811. } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
  1812. activeInfluenceIndices.sort( numericalSort );
  1813. } else if ( activeInfluenceIndices.length === 0 ) {
  1814. activeInfluenceIndices.push( [ 0, 0 ] );
  1815. };
  1816. var influenceIndex, m = 0;
  1817. while ( m < material.numSupportedMorphTargets ) {
  1818. if ( activeInfluenceIndices[ m ] ) {
  1819. influenceIndex = activeInfluenceIndices[ m ][ 1 ];
  1820. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  1821. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
  1822. enableAttribute( attributes[ 'morphTarget' + m ] );
  1823. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1824. }
  1825. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  1826. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
  1827. enableAttribute( attributes[ 'morphNormal' + m ] );
  1828. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  1829. }
  1830. object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
  1831. } else {
  1832. /*
  1833. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1834. if ( material.morphNormals ) {
  1835. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1836. }
  1837. */
  1838. object.__webglMorphTargetInfluences[ m ] = 0;
  1839. }
  1840. m ++;
  1841. }
  1842. }
  1843. // load updated influences uniform
  1844. if ( material.program.uniforms.morphTargetInfluences !== null ) {
  1845. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  1846. }
  1847. };
  1848. // Sorting
  1849. function painterSortStable ( a, b ) {
  1850. if ( a.material.id !== b.material.id ) {
  1851. return b.material.id - a.material.id;
  1852. } else if ( a.z !== b.z ) {
  1853. return b.z - a.z;
  1854. } else {
  1855. return a.id - b.id;
  1856. }
  1857. };
  1858. function reversePainterSortStable ( a, b ) {
  1859. if ( a.z !== b.z ) {
  1860. return a.z - b.z;
  1861. } else {
  1862. return a.id - b.id;
  1863. }
  1864. };
  1865. function numericalSort ( a, b ) {
  1866. return b[ 0 ] - a[ 0 ];
  1867. };
  1868. // Rendering
  1869. this.render = function ( scene, camera, renderTarget, forceClear ) {
  1870. if ( camera instanceof THREE.Camera === false ) {
  1871. console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
  1872. return;
  1873. }
  1874. var i, il,
  1875. webglObject, object,
  1876. renderList,
  1877. fog = scene.fog;
  1878. // reset caching for this frame
  1879. _currentMaterialId = - 1;
  1880. _currentCamera = null;
  1881. _lightsNeedUpdate = true;
  1882. // update scene graph
  1883. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  1884. // update camera matrices and frustum
  1885. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  1886. // update Skeleton objects
  1887. scene.traverse( function ( object ) {
  1888. if ( object instanceof THREE.SkinnedMesh ) {
  1889. object.skeleton.update();
  1890. }
  1891. } );
  1892. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1893. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  1894. _frustum.setFromMatrix( _projScreenMatrix );
  1895. lights.length = 0;
  1896. opaqueObjects.length = 0;
  1897. transparentObjects.length = 0;
  1898. projectObject( scene, scene );
  1899. if ( _this.sortObjects === true ) {
  1900. opaqueObjects.sort( painterSortStable );
  1901. transparentObjects.sort( reversePainterSortStable );
  1902. }
  1903. // custom render plugins (pre pass)
  1904. renderPlugins( this.renderPluginsPre, scene, camera );
  1905. //
  1906. _this.info.render.calls = 0;
  1907. _this.info.render.vertices = 0;
  1908. _this.info.render.faces = 0;
  1909. _this.info.render.points = 0;
  1910. this.setRenderTarget( renderTarget );
  1911. if ( this.autoClear || forceClear ) {
  1912. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  1913. }
  1914. // set matrices for immediate objects
  1915. for ( i = 0, il = _webglObjectsImmediate.length; i < il; i ++ ) {
  1916. webglObject = _webglObjectsImmediate[ i ];
  1917. object = webglObject.object;
  1918. if ( object.visible ) {
  1919. setupMatrices( object, camera );
  1920. unrollImmediateBufferMaterial( webglObject );
  1921. }
  1922. }
  1923. if ( scene.overrideMaterial ) {
  1924. var material = scene.overrideMaterial;
  1925. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  1926. this.setDepthTest( material.depthTest );
  1927. this.setDepthWrite( material.depthWrite );
  1928. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  1929. renderObjects( opaqueObjects, camera, lights, fog, true, material );
  1930. renderObjects( transparentObjects, camera, lights, fog, true, material );
  1931. renderObjectsImmediate( _webglObjectsImmediate, '', camera, lights, fog, false, material );
  1932. } else {
  1933. var material = null;
  1934. // opaque pass (front-to-back order)
  1935. this.setBlending( THREE.NoBlending );
  1936. renderObjects( opaqueObjects, camera, lights, fog, false, material );
  1937. renderObjectsImmediate( _webglObjectsImmediate, 'opaque', camera, lights, fog, false, material );
  1938. // transparent pass (back-to-front order)
  1939. renderObjects( transparentObjects, camera, lights, fog, true, material );
  1940. renderObjectsImmediate( _webglObjectsImmediate, 'transparent', camera, lights, fog, true, material );
  1941. }
  1942. // custom render plugins (post pass)
  1943. renderPlugins( this.renderPluginsPost, scene, camera );
  1944. // Generate mipmap if we're using any kind of mipmap filtering
  1945. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  1946. updateRenderTargetMipmap( renderTarget );
  1947. }
  1948. // Ensure depth buffer writing is enabled so it can be cleared on next render
  1949. this.setDepthTest( true );
  1950. this.setDepthWrite( true );
  1951. // _gl.finish();
  1952. };
  1953. function projectObject( scene, object ) {
  1954. if ( object.visible === false ) return;
  1955. if ( object instanceof THREE.Light ) {
  1956. lights.push( object );
  1957. }
  1958. if ( object instanceof THREE.Scene || object instanceof THREE.Group ) {
  1959. // skip
  1960. } else {
  1961. initObject( object, scene );
  1962. var webglObjects = _webglObjects[ object.id ];
  1963. if ( webglObjects && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
  1964. updateObject( object, scene );
  1965. for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
  1966. var webglObject = webglObjects[i];
  1967. unrollBufferMaterial( webglObject );
  1968. webglObject.render = true;
  1969. if ( _this.sortObjects === true ) {
  1970. if ( object.renderDepth !== null ) {
  1971. webglObject.z = object.renderDepth;
  1972. } else {
  1973. _vector3.setFromMatrixPosition( object.matrixWorld );
  1974. _vector3.applyProjection( _projScreenMatrix );
  1975. webglObject.z = _vector3.z;
  1976. }
  1977. }
  1978. }
  1979. }
  1980. }
  1981. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  1982. projectObject( scene, object.children[ i ] );
  1983. }
  1984. }
  1985. function renderPlugins( plugins, scene, camera ) {
  1986. if ( plugins.length === 0 ) return;
  1987. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  1988. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  1989. }
  1990. };
  1991. function renderObjects( renderList, camera, lights, fog, useBlending, overrideMaterial ) {
  1992. var webglObject, object, buffer, material;
  1993. for ( var i = renderList.length - 1; i !== - 1; i -- ) {
  1994. webglObject = renderList[ i ];
  1995. object = webglObject.object;
  1996. buffer = webglObject.buffer;
  1997. setupMatrices( object, camera );
  1998. if ( overrideMaterial ) {
  1999. material = overrideMaterial;
  2000. } else {
  2001. material = webglObject.material;
  2002. if ( ! material ) continue;
  2003. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2004. _this.setDepthTest( material.depthTest );
  2005. _this.setDepthWrite( material.depthWrite );
  2006. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2007. }
  2008. _this.setMaterialFaces( material );
  2009. if ( buffer instanceof THREE.BufferGeometry ) {
  2010. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  2011. } else {
  2012. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  2013. }
  2014. }
  2015. };
  2016. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  2017. var webglObject, object, material, program;
  2018. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  2019. webglObject = renderList[ i ];
  2020. object = webglObject.object;
  2021. if ( object.visible ) {
  2022. if ( overrideMaterial ) {
  2023. material = overrideMaterial;
  2024. } else {
  2025. material = webglObject[ materialType ];
  2026. if ( ! material ) continue;
  2027. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  2028. _this.setDepthTest( material.depthTest );
  2029. _this.setDepthWrite( material.depthWrite );
  2030. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  2031. }
  2032. _this.renderImmediateObject( camera, lights, fog, material, object );
  2033. }
  2034. }
  2035. };
  2036. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  2037. var program = setProgram( camera, lights, fog, material, object );
  2038. _currentGeometryGroupHash = - 1;
  2039. _this.setMaterialFaces( material );
  2040. if ( object.immediateRenderCallback ) {
  2041. object.immediateRenderCallback( program, _gl, _frustum );
  2042. } else {
  2043. object.render( function ( object ) { _this.renderBufferImmediate( object, program, material ); } );
  2044. }
  2045. };
  2046. function unrollImmediateBufferMaterial ( globject ) {
  2047. var object = globject.object,
  2048. material = object.material;
  2049. if ( material.transparent ) {
  2050. globject.transparent = material;
  2051. globject.opaque = null;
  2052. } else {
  2053. globject.opaque = material;
  2054. globject.transparent = null;
  2055. }
  2056. };
  2057. function unrollBufferMaterial ( globject ) {
  2058. var object = globject.object;
  2059. var buffer = globject.buffer;
  2060. var geometry = object.geometry;
  2061. var material = object.material;
  2062. if ( material instanceof THREE.MeshFaceMaterial ) {
  2063. var materialIndex = geometry instanceof THREE.BufferGeometry ? 0 : buffer.materialIndex;
  2064. material = material.materials[ materialIndex ];
  2065. globject.material = material;
  2066. if ( material.transparent ) {
  2067. transparentObjects.push( globject );
  2068. } else {
  2069. opaqueObjects.push( globject );
  2070. }
  2071. } else if ( material ) {
  2072. globject.material = material;
  2073. if ( material.transparent ) {
  2074. transparentObjects.push( globject );
  2075. } else {
  2076. opaqueObjects.push( globject );
  2077. }
  2078. }
  2079. };
  2080. function initObject( object, scene ) {
  2081. if ( object.__webglInit === undefined ) {
  2082. object.__webglInit = true;
  2083. object._modelViewMatrix = new THREE.Matrix4();
  2084. object._normalMatrix = new THREE.Matrix3();
  2085. object.addEventListener( 'removed', onObjectRemoved );
  2086. }
  2087. var geometry = object.geometry;
  2088. if ( geometry === undefined ) {
  2089. // ImmediateRenderObject
  2090. } else if ( geometry.__webglInit === undefined ) {
  2091. geometry.__webglInit = true;
  2092. geometry.addEventListener( 'dispose', onGeometryDispose );
  2093. if ( geometry instanceof THREE.BufferGeometry ) {
  2094. //
  2095. } else if ( object instanceof THREE.Mesh ) {
  2096. initGeometryGroups( scene, object, geometry );
  2097. } else if ( object instanceof THREE.Line ) {
  2098. if ( geometry.__webglVertexBuffer === undefined ) {
  2099. createLineBuffers( geometry );
  2100. initLineBuffers( geometry, object );
  2101. geometry.verticesNeedUpdate = true;
  2102. geometry.colorsNeedUpdate = true;
  2103. geometry.lineDistancesNeedUpdate = true;
  2104. }
  2105. } else if ( object instanceof THREE.PointCloud ) {
  2106. if ( geometry.__webglVertexBuffer === undefined ) {
  2107. createParticleBuffers( geometry );
  2108. initParticleBuffers( geometry, object );
  2109. geometry.verticesNeedUpdate = true;
  2110. geometry.colorsNeedUpdate = true;
  2111. }
  2112. }
  2113. }
  2114. if ( object.__webglActive === undefined) {
  2115. object.__webglActive = true;
  2116. if ( object instanceof THREE.Mesh ) {
  2117. if ( geometry instanceof THREE.BufferGeometry ) {
  2118. addBuffer( _webglObjects, geometry, object );
  2119. } else if ( geometry instanceof THREE.Geometry ) {
  2120. for ( var i = 0,l = geometry.geometryGroupsList.length; i < l; i ++ ) {
  2121. addBuffer( _webglObjects, geometry.geometryGroupsList[ i ], object );
  2122. }
  2123. }
  2124. } else if ( object instanceof THREE.Line || object instanceof THREE.PointCloud ) {
  2125. addBuffer( _webglObjects, geometry, object );
  2126. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  2127. addBufferImmediate( _webglObjectsImmediate, object );
  2128. }
  2129. }
  2130. };
  2131. function initGeometryGroups( scene, object, geometry ) {
  2132. var g, geometryGroup, material = object.material, addBuffers = false;
  2133. if ( geometry.geometryGroups === undefined || geometry.groupsNeedUpdate === true ) {
  2134. delete _webglObjects[ object.id ];
  2135. geometry.makeGroups( material instanceof THREE.MeshFaceMaterial, _glExtensionElementIndexUint ? 4294967296 : 65535 );
  2136. geometry.groupsNeedUpdate = false;
  2137. }
  2138. // create separate VBOs per geometry chunk
  2139. for ( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  2140. geometryGroup = geometry.geometryGroupsList[ i ];
  2141. // initialise VBO on the first access
  2142. if ( geometryGroup.__webglVertexBuffer === undefined ) {
  2143. createMeshBuffers( geometryGroup );
  2144. initMeshBuffers( geometryGroup, object );
  2145. geometry.verticesNeedUpdate = true;
  2146. geometry.morphTargetsNeedUpdate = true;
  2147. geometry.elementsNeedUpdate = true;
  2148. geometry.uvsNeedUpdate = true;
  2149. geometry.normalsNeedUpdate = true;
  2150. geometry.tangentsNeedUpdate = true;
  2151. geometry.colorsNeedUpdate = true;
  2152. addBuffers = true;
  2153. } else {
  2154. addBuffers = false;
  2155. }
  2156. if ( addBuffers || object.__webglActive === undefined ) {
  2157. addBuffer( _webglObjects, geometryGroup, object );
  2158. }
  2159. }
  2160. object.__webglActive = true;
  2161. }
  2162. function addBuffer( objlist, buffer, object ) {
  2163. var id = object.id;
  2164. objlist[id] = objlist[id] || [];
  2165. objlist[id].push(
  2166. {
  2167. id: id,
  2168. buffer: buffer,
  2169. object: object,
  2170. material: null,
  2171. z: 0
  2172. }
  2173. );
  2174. };
  2175. function addBufferImmediate( objlist, object ) {
  2176. objlist.push(
  2177. {
  2178. id: null,
  2179. object: object,
  2180. opaque: null,
  2181. transparent: null,
  2182. z: 0
  2183. }
  2184. );
  2185. };
  2186. // Objects updates
  2187. function updateObject( object, scene ) {
  2188. var geometry = object.geometry,
  2189. geometryGroup, customAttributesDirty, material;
  2190. if ( geometry instanceof THREE.BufferGeometry ) {
  2191. setDirectBuffers( geometry );
  2192. } else if ( object instanceof THREE.Mesh ) {
  2193. // check all geometry groups
  2194. if ( geometry.groupsNeedUpdate === true ) {
  2195. initGeometryGroups( scene, object, geometry );
  2196. }
  2197. for ( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  2198. geometryGroup = geometry.geometryGroupsList[ i ];
  2199. material = getBufferMaterial( object, geometryGroup );
  2200. if ( geometry.groupsNeedUpdate === true ) {
  2201. initMeshBuffers( geometryGroup, object );
  2202. }
  2203. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  2204. if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
  2205. geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
  2206. geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
  2207. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, ! geometry.dynamic, material );
  2208. }
  2209. }
  2210. geometry.verticesNeedUpdate = false;
  2211. geometry.morphTargetsNeedUpdate = false;
  2212. geometry.elementsNeedUpdate = false;
  2213. geometry.uvsNeedUpdate = false;
  2214. geometry.normalsNeedUpdate = false;
  2215. geometry.colorsNeedUpdate = false;
  2216. geometry.tangentsNeedUpdate = 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 && texture.format !== THREE.RGBFormat ) {
  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 isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
  3319. var cubeImage = [];
  3320. for ( var i = 0; i < 6; i ++ ) {
  3321. if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
  3322. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  3323. } else {
  3324. cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
  3325. }
  3326. }
  3327. var image = cubeImage[ 0 ],
  3328. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  3329. glFormat = paramThreeToGL( texture.format ),
  3330. glType = paramThreeToGL( texture.type );
  3331. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  3332. for ( var i = 0; i < 6; i ++ ) {
  3333. if ( ! isCompressed ) {
  3334. if ( isDataTexture ) {
  3335. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
  3336. } else {
  3337. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  3338. }
  3339. } else {
  3340. var mipmap, mipmaps = cubeImage[ i ].mipmaps;
  3341. for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
  3342. mipmap = mipmaps[ j ];
  3343. if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
  3344. if ( _compressedTextureFormats.indexOf( glFormat ) > -1 ) {
  3345. _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  3346. } else {
  3347. console.warn( "Attempt to load unsupported compressed texture format" );
  3348. }
  3349. } else {
  3350. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  3351. }
  3352. }
  3353. }
  3354. }
  3355. if ( texture.generateMipmaps && isImagePowerOfTwo ) {
  3356. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3357. }
  3358. texture.needsUpdate = false;
  3359. if ( texture.onUpdate ) texture.onUpdate();
  3360. } else {
  3361. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3362. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  3363. }
  3364. }
  3365. };
  3366. function setCubeTextureDynamic ( texture, slot ) {
  3367. _gl.activeTexture( _gl.TEXTURE0 + slot );
  3368. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  3369. };
  3370. // Render targets
  3371. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  3372. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  3373. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  3374. };
  3375. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  3376. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  3377. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  3378. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  3379. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3380. /* For some reason this is not working. Defaulting to RGBA4.
  3381. } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3382. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  3383. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3384. */
  3385. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3386. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  3387. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  3388. } else {
  3389. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  3390. }
  3391. };
  3392. this.setRenderTarget = function ( renderTarget ) {
  3393. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  3394. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  3395. if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  3396. if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  3397. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  3398. renderTarget.__webglTexture = _gl.createTexture();
  3399. _this.info.memory.textures ++;
  3400. // Setup texture, create render and frame buffers
  3401. var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ),
  3402. glFormat = paramThreeToGL( renderTarget.format ),
  3403. glType = paramThreeToGL( renderTarget.type );
  3404. if ( isCube ) {
  3405. renderTarget.__webglFramebuffer = [];
  3406. renderTarget.__webglRenderbuffer = [];
  3407. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  3408. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  3409. for ( var i = 0; i < 6; i ++ ) {
  3410. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  3411. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  3412. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  3413. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  3414. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  3415. }
  3416. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3417. } else {
  3418. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  3419. if ( renderTarget.shareDepthFrom ) {
  3420. renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
  3421. } else {
  3422. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  3423. }
  3424. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  3425. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  3426. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  3427. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  3428. if ( renderTarget.shareDepthFrom ) {
  3429. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  3430. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  3431. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  3432. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  3433. }
  3434. } else {
  3435. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  3436. }
  3437. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  3438. }
  3439. // Release everything
  3440. if ( isCube ) {
  3441. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  3442. } else {
  3443. _gl.bindTexture( _gl.TEXTURE_2D, null );
  3444. }
  3445. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  3446. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
  3447. }
  3448. var framebuffer, width, height, vx, vy;
  3449. if ( renderTarget ) {
  3450. if ( isCube ) {
  3451. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  3452. } else {
  3453. framebuffer = renderTarget.__webglFramebuffer;
  3454. }
  3455. width = renderTarget.width;
  3456. height = renderTarget.height;
  3457. vx = 0;
  3458. vy = 0;
  3459. } else {
  3460. framebuffer = null;
  3461. width = _viewportWidth;
  3462. height = _viewportHeight;
  3463. vx = _viewportX;
  3464. vy = _viewportY;
  3465. }
  3466. if ( framebuffer !== _currentFramebuffer ) {
  3467. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  3468. _gl.viewport( vx, vy, width, height );
  3469. _currentFramebuffer = framebuffer;
  3470. }
  3471. _currentWidth = width;
  3472. _currentHeight = height;
  3473. };
  3474. function updateRenderTargetMipmap ( renderTarget ) {
  3475. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  3476. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  3477. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  3478. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  3479. } else {
  3480. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  3481. _gl.generateMipmap( _gl.TEXTURE_2D );
  3482. _gl.bindTexture( _gl.TEXTURE_2D, null );
  3483. }
  3484. };
  3485. // Fallback filters for non-power-of-2 textures
  3486. function filterFallback ( f ) {
  3487. if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
  3488. return _gl.NEAREST;
  3489. }
  3490. return _gl.LINEAR;
  3491. };
  3492. // Map three.js constants to WebGL constants
  3493. function paramThreeToGL ( p ) {
  3494. if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
  3495. if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
  3496. if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
  3497. if ( p === THREE.NearestFilter ) return _gl.NEAREST;
  3498. if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
  3499. if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
  3500. if ( p === THREE.LinearFilter ) return _gl.LINEAR;
  3501. if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
  3502. if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
  3503. if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
  3504. if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
  3505. if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
  3506. if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
  3507. if ( p === THREE.ByteType ) return _gl.BYTE;
  3508. if ( p === THREE.ShortType ) return _gl.SHORT;
  3509. if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
  3510. if ( p === THREE.IntType ) return _gl.INT;
  3511. if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
  3512. if ( p === THREE.FloatType ) return _gl.FLOAT;
  3513. if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
  3514. if ( p === THREE.RGBFormat ) return _gl.RGB;
  3515. if ( p === THREE.RGBAFormat ) return _gl.RGBA;
  3516. if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
  3517. if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
  3518. if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
  3519. if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
  3520. if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
  3521. if ( p === THREE.ZeroFactor ) return _gl.ZERO;
  3522. if ( p === THREE.OneFactor ) return _gl.ONE;
  3523. if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
  3524. if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
  3525. if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
  3526. if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
  3527. if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
  3528. if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
  3529. if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
  3530. if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
  3531. if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
  3532. if ( _glExtensionCompressedTextureS3TC !== null ) {
  3533. if ( p === THREE.RGB_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGB_S3TC_DXT1_EXT;
  3534. if ( p === THREE.RGBA_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  3535. if ( p === THREE.RGBA_S3TC_DXT3_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  3536. if ( p === THREE.RGBA_S3TC_DXT5_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  3537. }
  3538. if ( _glExtensionCompressedTexturePVRTC !== null ) {
  3539. if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGB_PVRTC_4BPPV1_IMG ;
  3540. if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGB_PVRTC_2BPPV1_IMG ;
  3541. if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG ;
  3542. if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return _glExtensionCompressedTexturePVRTC.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG ;
  3543. }
  3544. if ( _glExtensionBlendMinMax !== undefined ) {
  3545. if ( p === THREE.MinEquation ) return _glExtensionBlendMinMax.MIN_EXT;
  3546. if ( p === THREE.MaxEquation ) return _glExtensionBlendMinMax.MAX_EXT;
  3547. }
  3548. return 0;
  3549. };
  3550. // Allocations
  3551. function allocateBones ( object ) {
  3552. if ( _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  3553. return 1024;
  3554. } else {
  3555. // default for when object is not specified
  3556. // ( for example when prebuilding shader
  3557. // to be used with multiple objects )
  3558. //
  3559. // - leave some extra space for other uniforms
  3560. // - limit here is ANGLE's 254 max uniform vectors
  3561. // (up to 54 should be safe)
  3562. var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
  3563. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  3564. var maxBones = nVertexMatrices;
  3565. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  3566. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  3567. if ( maxBones < object.skeleton.bones.length ) {
  3568. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  3569. }
  3570. }
  3571. return maxBones;
  3572. }
  3573. };
  3574. function allocateLights( lights ) {
  3575. var dirLights = 0;
  3576. var pointLights = 0;
  3577. var spotLights = 0;
  3578. var hemiLights = 0;
  3579. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  3580. var light = lights[ l ];
  3581. if ( light.onlyShadow || light.visible === false ) continue;
  3582. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  3583. if ( light instanceof THREE.PointLight ) pointLights ++;
  3584. if ( light instanceof THREE.SpotLight ) spotLights ++;
  3585. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  3586. }
  3587. return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
  3588. };
  3589. function allocateShadows( lights ) {
  3590. var maxShadows = 0;
  3591. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  3592. var light = lights[ l ];
  3593. if ( ! light.castShadow ) continue;
  3594. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  3595. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  3596. }
  3597. return maxShadows;
  3598. };
  3599. // Initialization
  3600. function initGL() {
  3601. try {
  3602. var attributes = {
  3603. alpha: _alpha,
  3604. depth: _depth,
  3605. stencil: _stencil,
  3606. antialias: _antialias,
  3607. premultipliedAlpha: _premultipliedAlpha,
  3608. preserveDrawingBuffer: _preserveDrawingBuffer
  3609. };
  3610. _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
  3611. if ( _gl === null ) {
  3612. throw 'Error creating WebGL context.';
  3613. }
  3614. } catch ( error ) {
  3615. console.error( error );
  3616. }
  3617. _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' );
  3618. _glExtensionTextureFloatLinear = _gl.getExtension( 'OES_texture_float_linear' );
  3619. _glExtensionStandardDerivatives = _gl.getExtension( 'OES_standard_derivatives' );
  3620. _glExtensionTextureFilterAnisotropic = _gl.getExtension( 'EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
  3621. _glExtensionCompressedTextureS3TC = _gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
  3622. _glExtensionCompressedTexturePVRTC = _gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
  3623. _glExtensionElementIndexUint = _gl.getExtension( 'OES_element_index_uint' );
  3624. _glExtensionBlendMinMax = _gl.getExtension( 'EXT_blend_minmax' );
  3625. if ( _glExtensionTextureFloat === null ) {
  3626. console.log( 'THREE.WebGLRenderer: Float textures not supported.' );
  3627. }
  3628. if ( _glExtensionStandardDerivatives === null ) {
  3629. console.log( 'THREE.WebGLRenderer: Standard derivatives not supported.' );
  3630. }
  3631. if ( _glExtensionTextureFilterAnisotropic === null ) {
  3632. console.log( 'THREE.WebGLRenderer: Anisotropic texture filtering not supported.' );
  3633. }
  3634. if ( _glExtensionCompressedTextureS3TC === null ) {
  3635. console.log( 'THREE.WebGLRenderer: S3TC compressed textures not supported.' );
  3636. }
  3637. if ( _glExtensionCompressedTexturePVRTC === null ) {
  3638. console.log( 'THREE.WebGLRenderer: PVRTC compressed textures not supported.' );
  3639. }
  3640. if ( _glExtensionElementIndexUint === null ) {
  3641. console.log( 'THREE.WebGLRenderer: elementindex as unsigned integer not supported.' );
  3642. }
  3643. if ( _glExtensionBlendMinMax === null ) {
  3644. console.log( 'THREE.WebGLRenderer: min max blend equations not supported.' );
  3645. }
  3646. if ( _gl.getShaderPrecisionFormat === undefined ) {
  3647. _gl.getShaderPrecisionFormat = function () {
  3648. return {
  3649. 'rangeMin': 1,
  3650. 'rangeMax': 1,
  3651. 'precision': 1
  3652. };
  3653. }
  3654. }
  3655. if ( _logarithmicDepthBuffer ) {
  3656. _glExtensionFragDepth = _gl.getExtension( 'EXT_frag_depth' );
  3657. }
  3658. }
  3659. function setDefaultGLState () {
  3660. _gl.clearColor( 0, 0, 0, 1 );
  3661. _gl.clearDepth( 1 );
  3662. _gl.clearStencil( 0 );
  3663. _gl.enable( _gl.DEPTH_TEST );
  3664. _gl.depthFunc( _gl.LEQUAL );
  3665. _gl.frontFace( _gl.CCW );
  3666. _gl.cullFace( _gl.BACK );
  3667. _gl.enable( _gl.CULL_FACE );
  3668. _gl.enable( _gl.BLEND );
  3669. _gl.blendEquation( _gl.FUNC_ADD );
  3670. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  3671. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  3672. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  3673. }
  3674. function resetGLState() {
  3675. _currentProgram = null;
  3676. _currentCamera = null;
  3677. _oldBlending = - 1;
  3678. _oldDepthTest = - 1;
  3679. _oldDepthWrite = - 1;
  3680. _oldDoubleSided = - 1;
  3681. _oldFlipSided = - 1;
  3682. _currentGeometryGroupHash = - 1;
  3683. _currentMaterialId = - 1;
  3684. _lightsNeedUpdate = true;
  3685. }
  3686. this.resetGLState = resetGLState;
  3687. // default plugins (order is important)
  3688. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  3689. this.addPrePlugin( this.shadowMapPlugin );
  3690. this.addPostPlugin( new THREE.SpritePlugin() );
  3691. this.addPostPlugin( new THREE.LensFlarePlugin() );
  3692. };
粤ICP备19079148号