WebGLRenderer.js 151 KB

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