WebGLRenderer.js 170 KB

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