WebGLRenderer.js 178 KB

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