WebGLRenderer.js 156 KB

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