WebGLRenderer.js 162 KB

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