WebGLRenderer.js 174 KB

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