WebGLRenderer.js 157 KB

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