WebGLRenderer.js 174 KB

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