WebGLRenderer.js 174 KB

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