WebGLRenderer.js 170 KB

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