WebGLRenderer.js 147 KB

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