WebGLRenderer.js 148 KB

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