WebGLRenderer.js 154 KB

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