WebGLRenderer.js 176 KB

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