WebGLRenderer.js 180 KB

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