WebGLRenderer.js 163 KB

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