WebGLRenderer.js 178 KB

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