WebGLRenderer.js 150 KB

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