WebGLRenderer.js 156 KB

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