12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295 |
- /**
- * @author supereggbert / http://www.paulbrunt.co.uk/
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- * @author szimek / https://github.com/szimek/
- */
- THREE.WebGLRenderer = function ( parameters ) {
- console.log( 'THREE.WebGLRenderer', THREE.REVISION );
- parameters = parameters || {};
- var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
- _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
- _alpha = parameters.alpha !== undefined ? parameters.alpha : true,
- _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
- _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
- _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
- _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
- _clearColor = new THREE.Color( 0x000000 ),
- _clearAlpha = 0;
- if ( parameters.clearColor !== undefined ) {
- console.warn( 'DEPRECATED: clearColor in WebGLRenderer constructor parameters is being removed. Use .setClearColor() instead.' );
- _clearColor.setHex( parameters.clearColor );
- }
- if ( parameters.clearAlpha !== undefined ) {
- console.warn( 'DEPRECATED: clearAlpha in WebGLRenderer constructor parameters is being removed. Use .setClearColor() instead.' );
- _clearAlpha = parameters.clearAlpha;
- }
- // public properties
- this.domElement = _canvas;
- this.context = null;
- this.devicePixelRatio = parameters.devicePixelRatio !== undefined
- ? parameters.devicePixelRatio
- : window.devicePixelRatio !== undefined
- ? window.devicePixelRatio
- : 1;
- // clearing
- this.autoClear = true;
- this.autoClearColor = true;
- this.autoClearDepth = true;
- this.autoClearStencil = true;
- // scene graph
- this.sortObjects = true;
- this.autoUpdateObjects = true;
- // physically based shading
- this.gammaInput = false;
- this.gammaOutput = false;
- this.physicallyBasedShading = false;
- // shadow map
- this.shadowMapEnabled = false;
- this.shadowMapAutoUpdate = true;
- this.shadowMapType = THREE.PCFShadowMap;
- this.shadowMapCullFace = THREE.CullFaceFront;
- this.shadowMapDebug = false;
- this.shadowMapCascade = false;
- // morphs
- this.maxMorphTargets = 8;
- this.maxMorphNormals = 4;
- // flags
- this.autoScaleCubemaps = true;
- // custom render plugins
- this.renderPluginsPre = [];
- this.renderPluginsPost = [];
- // info
- this.info = {
- memory: {
- programs: 0,
- geometries: 0,
- textures: 0
- },
- render: {
- calls: 0,
- vertices: 0,
- faces: 0,
- points: 0
- }
- };
- // internal properties
- var _this = this,
- _programs = [],
- _programs_counter = 0,
- // internal state cache
- _currentProgram = null,
- _currentFramebuffer = null,
- _currentMaterialId = -1,
- _currentGeometryGroupHash = null,
- _currentCamera = null,
- _geometryGroupCounter = 0,
- _usedTextureUnits = 0,
- // GL state cache
- _oldDoubleSided = -1,
- _oldFlipSided = -1,
- _oldBlending = -1,
- _oldBlendEquation = -1,
- _oldBlendSrc = -1,
- _oldBlendDst = -1,
- _oldDepthTest = -1,
- _oldDepthWrite = -1,
- _oldPolygonOffset = null,
- _oldPolygonOffsetFactor = null,
- _oldPolygonOffsetUnits = null,
- _oldLineWidth = null,
- _viewportX = 0,
- _viewportY = 0,
- _viewportWidth = 0,
- _viewportHeight = 0,
- _currentWidth = 0,
- _currentHeight = 0,
- _enabledAttributes = {},
- // frustum
- _frustum = new THREE.Frustum(),
- // camera matrices cache
- _projScreenMatrix = new THREE.Matrix4(),
- _projScreenMatrixPS = new THREE.Matrix4(),
- _vector3 = new THREE.Vector3(),
- // light arrays cache
- _direction = new THREE.Vector3(),
- _lightsNeedUpdate = true,
- _lights = {
- ambient: [ 0, 0, 0 ],
- directional: { length: 0, colors: new Array(), positions: new Array() },
- point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() },
- spot: { length: 0, colors: new Array(), positions: new Array(), distances: new Array(), directions: new Array(), anglesCos: new Array(), exponents: new Array() },
- hemi: { length: 0, skyColors: new Array(), groundColors: new Array(), positions: new Array() }
- };
- // initialize
- var _gl;
- var _glExtensionTextureFloat;
- var _glExtensionTextureFloatLinear;
- var _glExtensionStandardDerivatives;
- var _glExtensionTextureFilterAnisotropic;
- var _glExtensionCompressedTextureS3TC;
- initGL();
- setDefaultGLState();
- this.context = _gl;
- // GPU capabilities
- var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
- var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
- var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
- var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
- var _maxAnisotropy = _glExtensionTextureFilterAnisotropic ? _gl.getParameter( _glExtensionTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
- var _supportsVertexTextures = ( _maxVertexTextures > 0 );
- var _supportsBoneTextures = _supportsVertexTextures && _glExtensionTextureFloat;
- var _compressedTextureFormats = _glExtensionCompressedTextureS3TC ? _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS ) : [];
- //
- var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
- var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
- var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT );
- var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
- var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
- var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT );
- var _vertexShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_INT );
- var _vertexShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_INT );
- var _vertexShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_INT );
- var _fragmentShaderPrecisionHighpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_INT );
- var _fragmentShaderPrecisionMediumpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_INT );
- var _fragmentShaderPrecisionLowpInt = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_INT );
- // clamp precision to maximum available
- var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
- var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
- if ( _precision === "highp" && ! highpAvailable ) {
- if ( mediumpAvailable ) {
- _precision = "mediump";
- console.warn( "WebGLRenderer: highp not supported, using mediump" );
- } else {
- _precision = "lowp";
- console.warn( "WebGLRenderer: highp and mediump not supported, using lowp" );
- }
- }
- if ( _precision === "mediump" && ! mediumpAvailable ) {
- _precision = "lowp";
- console.warn( "WebGLRenderer: mediump not supported, using lowp" );
- }
- // API
- this.getContext = function () {
- return _gl;
- };
- this.supportsVertexTextures = function () {
- return _supportsVertexTextures;
- };
- this.supportsFloatTextures = function () {
- return _glExtensionTextureFloat;
- };
- this.supportsStandardDerivatives = function () {
- return _glExtensionStandardDerivatives;
- };
- this.supportsCompressedTextureS3TC = function () {
- return _glExtensionCompressedTextureS3TC;
- };
- this.getMaxAnisotropy = function () {
- return _maxAnisotropy;
- };
- this.getPrecision = function () {
- return _precision;
- };
- this.setSize = function ( width, height, updateStyle ) {
- _canvas.width = width * this.devicePixelRatio;
- _canvas.height = height * this.devicePixelRatio;
- if ( this.devicePixelRatio !== 1 && updateStyle !== false ) {
- _canvas.style.width = width + 'px';
- _canvas.style.height = height + 'px';
- }
- this.setViewport( 0, 0, _canvas.width, _canvas.height );
- };
- this.setViewport = function ( x, y, width, height ) {
- _viewportX = x !== undefined ? x : 0;
- _viewportY = y !== undefined ? y : 0;
- _viewportWidth = width !== undefined ? width : _canvas.width;
- _viewportHeight = height !== undefined ? height : _canvas.height;
- _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
- };
- this.setScissor = function ( x, y, width, height ) {
- _gl.scissor( x, y, width, height );
- };
- this.enableScissorTest = function ( enable ) {
- enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
- };
- // Clearing
- this.setClearColor = function ( color, alpha ) {
- _clearColor.set( color );
- _clearAlpha = alpha !== undefined ? alpha : 1;
- _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
- };
- this.setClearColorHex = function ( hex, alpha ) {
- console.warn( 'DEPRECATED: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
- this.setClearColor( hex, alpha );
- };
- this.getClearColor = function () {
- return _clearColor;
- };
- this.getClearAlpha = function () {
- return _clearAlpha;
- };
- this.clear = function ( color, depth, stencil ) {
- var bits = 0;
- if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
- if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
- if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
- _gl.clear( bits );
- };
- this.clearTarget = function ( renderTarget, color, depth, stencil ) {
- this.setRenderTarget( renderTarget );
- this.clear( color, depth, stencil );
- };
- // Plugins
- this.addPostPlugin = function ( plugin ) {
- plugin.init( this );
- this.renderPluginsPost.push( plugin );
- };
- this.addPrePlugin = function ( plugin ) {
- plugin.init( this );
- this.renderPluginsPre.push( plugin );
- };
- // Rendering
- this.updateShadowMap = function ( scene, camera ) {
- _currentProgram = null;
- _oldBlending = -1;
- _oldDepthTest = -1;
- _oldDepthWrite = -1;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- _oldDoubleSided = -1;
- _oldFlipSided = -1;
- this.shadowMapPlugin.update( scene, camera );
- };
- // Internal functions
- // Buffer allocation
- function createParticleBuffers ( geometry ) {
- geometry.__webglVertexBuffer = _gl.createBuffer();
- geometry.__webglColorBuffer = _gl.createBuffer();
- _this.info.memory.geometries ++;
- };
- function createLineBuffers ( geometry ) {
- geometry.__webglVertexBuffer = _gl.createBuffer();
- geometry.__webglColorBuffer = _gl.createBuffer();
- geometry.__webglLineDistanceBuffer = _gl.createBuffer();
- _this.info.memory.geometries ++;
- };
- function createRibbonBuffers ( geometry ) {
- geometry.__webglVertexBuffer = _gl.createBuffer();
- geometry.__webglColorBuffer = _gl.createBuffer();
- geometry.__webglNormalBuffer = _gl.createBuffer();
- _this.info.memory.geometries ++;
- };
- function createMeshBuffers ( geometryGroup ) {
- geometryGroup.__webglVertexBuffer = _gl.createBuffer();
- geometryGroup.__webglNormalBuffer = _gl.createBuffer();
- geometryGroup.__webglTangentBuffer = _gl.createBuffer();
- geometryGroup.__webglColorBuffer = _gl.createBuffer();
- geometryGroup.__webglUVBuffer = _gl.createBuffer();
- geometryGroup.__webglUV2Buffer = _gl.createBuffer();
- geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
- geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
- geometryGroup.__webglFaceBuffer = _gl.createBuffer();
- geometryGroup.__webglLineBuffer = _gl.createBuffer();
- var m, ml;
- if ( geometryGroup.numMorphTargets ) {
- geometryGroup.__webglMorphTargetsBuffers = [];
- for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
- geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
- }
- }
- if ( geometryGroup.numMorphNormals ) {
- geometryGroup.__webglMorphNormalsBuffers = [];
- for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
- geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
- }
- }
- _this.info.memory.geometries ++;
- };
- // Events
- var onGeometryDispose = function ( event ) {
- var geometry = event.target;
- geometry.removeEventListener( 'dispose', onGeometryDispose );
- deallocateGeometry( geometry );
- _this.info.memory.geometries --;
- };
- var onTextureDispose = function ( event ) {
- var texture = event.target;
- texture.removeEventListener( 'dispose', onTextureDispose );
- deallocateTexture( texture );
- _this.info.memory.textures --;
- };
- var onRenderTargetDispose = function ( event ) {
- var renderTarget = event.target;
- renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
- deallocateRenderTarget( renderTarget );
- _this.info.memory.textures --;
- };
- var onMaterialDispose = function ( event ) {
- var material = event.target;
- material.removeEventListener( 'dispose', onMaterialDispose );
- deallocateMaterial( material );
- };
- // Buffer deallocation
- var deallocateGeometry = function ( geometry ) {
- geometry.__webglInit = undefined;
- if ( geometry.__webglVertexBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglVertexBuffer );
- if ( geometry.__webglNormalBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglNormalBuffer );
- if ( geometry.__webglTangentBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglTangentBuffer );
- if ( geometry.__webglColorBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglColorBuffer );
- if ( geometry.__webglUVBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglUVBuffer );
- if ( geometry.__webglUV2Buffer !== undefined ) _gl.deleteBuffer( geometry.__webglUV2Buffer );
- if ( geometry.__webglSkinIndicesBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinIndicesBuffer );
- if ( geometry.__webglSkinWeightsBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglSkinWeightsBuffer );
- if ( geometry.__webglFaceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglFaceBuffer );
- if ( geometry.__webglLineBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineBuffer );
- if ( geometry.__webglLineDistanceBuffer !== undefined ) _gl.deleteBuffer( geometry.__webglLineDistanceBuffer );
- // geometry groups
- if ( geometry.geometryGroups !== undefined ) {
- for ( var g in geometry.geometryGroups ) {
- var geometryGroup = geometry.geometryGroups[ g ];
- if ( geometryGroup.numMorphTargets !== undefined ) {
- for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
- _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
- }
- }
- if ( geometryGroup.numMorphNormals !== undefined ) {
- for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
- _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
- }
- }
- deleteCustomAttributesBuffers( geometryGroup );
- }
- }
- deleteCustomAttributesBuffers( geometry );
- };
- var deallocateTexture = function ( texture ) {
- if ( texture.image && texture.image.__webglTextureCube ) {
- // cube texture
- _gl.deleteTexture( texture.image.__webglTextureCube );
- } else {
- // 2D texture
- if ( ! texture.__webglInit ) return;
- texture.__webglInit = false;
- _gl.deleteTexture( texture.__webglTexture );
- }
- };
- var deallocateRenderTarget = function ( renderTarget ) {
- if ( !renderTarget || ! renderTarget.__webglTexture ) return;
- _gl.deleteTexture( renderTarget.__webglTexture );
- if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
- for ( var i = 0; i < 6; i ++ ) {
- _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
- _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
- }
- } else {
- _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
- _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
- }
- };
- var deallocateMaterial = function ( material ) {
- var program = material.program;
- if ( program === undefined ) return;
- material.program = undefined;
- // only deallocate GL program if this was the last use of shared program
- // assumed there is only single copy of any program in the _programs list
- // (that's how it's constructed)
- var i, il, programInfo;
- var deleteProgram = false;
- for ( i = 0, il = _programs.length; i < il; i ++ ) {
- programInfo = _programs[ i ];
- if ( programInfo.program === program ) {
- programInfo.usedTimes --;
- if ( programInfo.usedTimes === 0 ) {
- deleteProgram = true;
- }
- break;
- }
- }
- if ( deleteProgram === true ) {
- // avoid using array.splice, this is costlier than creating new array from scratch
- var newPrograms = [];
- for ( i = 0, il = _programs.length; i < il; i ++ ) {
- programInfo = _programs[ i ];
- if ( programInfo.program !== program ) {
- newPrograms.push( programInfo );
- }
- }
- _programs = newPrograms;
- _gl.deleteProgram( program );
- _this.info.memory.programs --;
- }
- };
- //
- function deleteCustomAttributesBuffers( geometry ) {
- if ( geometry.__webglCustomAttributesList ) {
- for ( var id in geometry.__webglCustomAttributesList ) {
- _gl.deleteBuffer( geometry.__webglCustomAttributesList[ id ].buffer );
- }
- }
- };
- // Buffer initialization
- function initCustomAttributes ( geometry, object ) {
- var nvertices = geometry.vertices.length;
- var material = object.material;
- if ( material.attributes ) {
- if ( geometry.__webglCustomAttributesList === undefined ) {
- geometry.__webglCustomAttributesList = [];
- }
- for ( var a in material.attributes ) {
- var attribute = material.attributes[ a ];
- if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
- attribute.__webglInitialized = true;
- var size = 1; // "f" and "i"
- if ( attribute.type === "v2" ) size = 2;
- else if ( attribute.type === "v3" ) size = 3;
- else if ( attribute.type === "v4" ) size = 4;
- else if ( attribute.type === "c" ) size = 3;
- attribute.size = size;
- attribute.array = new Float32Array( nvertices * size );
- attribute.buffer = _gl.createBuffer();
- attribute.buffer.belongsToAttribute = a;
- attribute.needsUpdate = true;
- }
- geometry.__webglCustomAttributesList.push( attribute );
- }
- }
- };
- function initParticleBuffers ( geometry, object ) {
- var nvertices = geometry.vertices.length;
- geometry.__vertexArray = new Float32Array( nvertices * 3 );
- geometry.__colorArray = new Float32Array( nvertices * 3 );
- geometry.__sortArray = [];
- geometry.__webglParticleCount = nvertices;
- initCustomAttributes ( geometry, object );
- };
- function initLineBuffers ( geometry, object ) {
- var nvertices = geometry.vertices.length;
- geometry.__vertexArray = new Float32Array( nvertices * 3 );
- geometry.__colorArray = new Float32Array( nvertices * 3 );
- geometry.__lineDistanceArray = new Float32Array( nvertices * 1 );
- geometry.__webglLineCount = nvertices;
- initCustomAttributes ( geometry, object );
- };
- function initRibbonBuffers ( geometry, object ) {
- var nvertices = geometry.vertices.length;
- geometry.__vertexArray = new Float32Array( nvertices * 3 );
- geometry.__colorArray = new Float32Array( nvertices * 3 );
- geometry.__normalArray = new Float32Array( nvertices * 3 );
- geometry.__webglVertexCount = nvertices;
- initCustomAttributes ( geometry, object );
- };
- function initMeshBuffers ( geometryGroup, object ) {
- var geometry = object.geometry,
- faces3 = geometryGroup.faces3,
- faces4 = geometryGroup.faces4,
- nvertices = faces3.length * 3 + faces4.length * 4,
- ntris = faces3.length * 1 + faces4.length * 2,
- nlines = faces3.length * 3 + faces4.length * 4,
- material = getBufferMaterial( object, geometryGroup ),
- uvType = bufferGuessUVType( material ),
- normalType = bufferGuessNormalType( material ),
- vertexColorType = bufferGuessVertexColorType( material );
- // console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material );
- geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
- if ( normalType ) {
- geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
- }
- if ( geometry.hasTangents ) {
- geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
- }
- if ( vertexColorType ) {
- geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
- }
- if ( uvType ) {
- if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
- geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
- }
- if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
- geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
- }
- }
- if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
- geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
- geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
- }
- geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
- geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
- var m, ml;
- if ( geometryGroup.numMorphTargets ) {
- geometryGroup.__morphTargetsArrays = [];
- for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
- geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
- }
- }
- if ( geometryGroup.numMorphNormals ) {
- geometryGroup.__morphNormalsArrays = [];
- for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
- geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
- }
- }
- geometryGroup.__webglFaceCount = ntris * 3;
- geometryGroup.__webglLineCount = nlines * 2;
- // custom attributes
- if ( material.attributes ) {
- if ( geometryGroup.__webglCustomAttributesList === undefined ) {
- geometryGroup.__webglCustomAttributesList = [];
- }
- for ( var a in material.attributes ) {
- // Do a shallow copy of the attribute object so different geometryGroup chunks use different
- // attribute buffers which are correctly indexed in the setMeshBuffers function
- var originalAttribute = material.attributes[ a ];
- var attribute = {};
- for ( var property in originalAttribute ) {
- attribute[ property ] = originalAttribute[ property ];
- }
- if ( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
- attribute.__webglInitialized = true;
- var size = 1; // "f" and "i"
- if( attribute.type === "v2" ) size = 2;
- else if( attribute.type === "v3" ) size = 3;
- else if( attribute.type === "v4" ) size = 4;
- else if( attribute.type === "c" ) size = 3;
- attribute.size = size;
- attribute.array = new Float32Array( nvertices * size );
- attribute.buffer = _gl.createBuffer();
- attribute.buffer.belongsToAttribute = a;
- originalAttribute.needsUpdate = true;
- attribute.__original = originalAttribute;
- }
- geometryGroup.__webglCustomAttributesList.push( attribute );
- }
- }
- geometryGroup.__inittedArrays = true;
- };
- function getBufferMaterial( object, geometryGroup ) {
- return object.material instanceof THREE.MeshFaceMaterial
- ? object.material.materials[ geometryGroup.materialIndex ]
- : object.material;
- };
- function materialNeedsSmoothNormals ( material ) {
- return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
- };
- function bufferGuessNormalType ( material ) {
- // only MeshBasicMaterial and MeshDepthMaterial don't need normals
- if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) {
- return false;
- }
- if ( materialNeedsSmoothNormals( material ) ) {
- return THREE.SmoothShading;
- } else {
- return THREE.FlatShading;
- }
- };
- function bufferGuessVertexColorType( material ) {
- if ( material.vertexColors ) {
- return material.vertexColors;
- }
- return false;
- };
- function bufferGuessUVType( material ) {
- // material must use some texture to require uvs
- if ( material.map ||
- material.lightMap ||
- material.bumpMap ||
- material.normalMap ||
- material.specularMap ||
- material instanceof THREE.ShaderMaterial ) {
- return true;
- }
- return false;
- };
- //
- function initDirectBuffers( geometry ) {
- var a, attribute, type;
- for ( a in geometry.attributes ) {
- if ( a === "index" ) {
- type = _gl.ELEMENT_ARRAY_BUFFER;
- } else {
- type = _gl.ARRAY_BUFFER;
- }
- attribute = geometry.attributes[ a ];
- if ( attribute.numItems === undefined ) {
- attribute.numItems = attribute.array.length;
- }
- attribute.buffer = _gl.createBuffer();
- _gl.bindBuffer( type, attribute.buffer );
- _gl.bufferData( type, attribute.array, _gl.STATIC_DRAW );
- }
- };
- // Buffer setting
- function setParticleBuffers ( geometry, hint, object ) {
- var v, c, vertex, offset, index, color,
- vertices = geometry.vertices,
- vl = vertices.length,
- colors = geometry.colors,
- cl = colors.length,
- vertexArray = geometry.__vertexArray,
- colorArray = geometry.__colorArray,
- sortArray = geometry.__sortArray,
- dirtyVertices = geometry.verticesNeedUpdate,
- dirtyElements = geometry.elementsNeedUpdate,
- dirtyColors = geometry.colorsNeedUpdate,
- customAttributes = geometry.__webglCustomAttributesList,
- i, il,
- a, ca, cal, value,
- customAttribute;
- if ( object.sortParticles ) {
- _projScreenMatrixPS.copy( _projScreenMatrix );
- _projScreenMatrixPS.multiply( object.matrixWorld );
- for ( v = 0; v < vl; v ++ ) {
- vertex = vertices[ v ];
- _vector3.copy( vertex );
- _vector3.applyProjection( _projScreenMatrixPS );
- sortArray[ v ] = [ _vector3.z, v ];
- }
- sortArray.sort( numericalSort );
- for ( v = 0; v < vl; v ++ ) {
- vertex = vertices[ sortArray[v][1] ];
- offset = v * 3;
- vertexArray[ offset ] = vertex.x;
- vertexArray[ offset + 1 ] = vertex.y;
- vertexArray[ offset + 2 ] = vertex.z;
- }
- for ( c = 0; c < cl; c ++ ) {
- offset = c * 3;
- color = colors[ sortArray[c][1] ];
- colorArray[ offset ] = color.r;
- colorArray[ offset + 1 ] = color.g;
- colorArray[ offset + 2 ] = color.b;
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
- offset = 0;
- cal = customAttribute.value.length;
- if ( customAttribute.size === 1 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- index = sortArray[ ca ][ 1 ];
- customAttribute.array[ ca ] = customAttribute.value[ index ];
- }
- } else if ( customAttribute.size === 2 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- index = sortArray[ ca ][ 1 ];
- value = customAttribute.value[ index ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- offset += 2;
- }
- } else if ( customAttribute.size === 3 ) {
- if ( customAttribute.type === "c" ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- index = sortArray[ ca ][ 1 ];
- value = customAttribute.value[ index ];
- customAttribute.array[ offset ] = value.r;
- customAttribute.array[ offset + 1 ] = value.g;
- customAttribute.array[ offset + 2 ] = value.b;
- offset += 3;
- }
- } else {
- for ( ca = 0; ca < cal; ca ++ ) {
- index = sortArray[ ca ][ 1 ];
- value = customAttribute.value[ index ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- offset += 3;
- }
- }
- } else if ( customAttribute.size === 4 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- index = sortArray[ ca ][ 1 ];
- value = customAttribute.value[ index ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- customAttribute.array[ offset + 3 ] = value.w;
- offset += 4;
- }
- }
- }
- }
- } else {
- if ( dirtyVertices ) {
- for ( v = 0; v < vl; v ++ ) {
- vertex = vertices[ v ];
- offset = v * 3;
- vertexArray[ offset ] = vertex.x;
- vertexArray[ offset + 1 ] = vertex.y;
- vertexArray[ offset + 2 ] = vertex.z;
- }
- }
- if ( dirtyColors ) {
- for ( c = 0; c < cl; c ++ ) {
- color = colors[ c ];
- offset = c * 3;
- colorArray[ offset ] = color.r;
- colorArray[ offset + 1 ] = color.g;
- colorArray[ offset + 2 ] = color.b;
- }
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( customAttribute.needsUpdate &&
- ( customAttribute.boundTo === undefined ||
- customAttribute.boundTo === "vertices") ) {
- cal = customAttribute.value.length;
- offset = 0;
- if ( customAttribute.size === 1 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- customAttribute.array[ ca ] = customAttribute.value[ ca ];
- }
- } else if ( customAttribute.size === 2 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- offset += 2;
- }
- } else if ( customAttribute.size === 3 ) {
- if ( customAttribute.type === "c" ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.r;
- customAttribute.array[ offset + 1 ] = value.g;
- customAttribute.array[ offset + 2 ] = value.b;
- offset += 3;
- }
- } else {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- offset += 3;
- }
- }
- } else if ( customAttribute.size === 4 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- customAttribute.array[ offset + 3 ] = value.w;
- offset += 4;
- }
- }
- }
- }
- }
- }
- if ( dirtyVertices || object.sortParticles ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
- }
- if ( dirtyColors || object.sortParticles ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( customAttribute.needsUpdate || object.sortParticles ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
- }
- }
- }
- };
- function setLineBuffers ( geometry, hint ) {
- var v, c, d, vertex, offset, color,
- vertices = geometry.vertices,
- colors = geometry.colors,
- lineDistances = geometry.lineDistances,
- vl = vertices.length,
- cl = colors.length,
- dl = lineDistances.length,
- vertexArray = geometry.__vertexArray,
- colorArray = geometry.__colorArray,
- lineDistanceArray = geometry.__lineDistanceArray,
- dirtyVertices = geometry.verticesNeedUpdate,
- dirtyColors = geometry.colorsNeedUpdate,
- dirtyLineDistances = geometry.lineDistancesNeedUpdate,
- customAttributes = geometry.__webglCustomAttributesList,
- i, il,
- a, ca, cal, value,
- customAttribute;
- if ( dirtyVertices ) {
- for ( v = 0; v < vl; v ++ ) {
- vertex = vertices[ v ];
- offset = v * 3;
- vertexArray[ offset ] = vertex.x;
- vertexArray[ offset + 1 ] = vertex.y;
- vertexArray[ offset + 2 ] = vertex.z;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
- }
- if ( dirtyColors ) {
- for ( c = 0; c < cl; c ++ ) {
- color = colors[ c ];
- offset = c * 3;
- colorArray[ offset ] = color.r;
- colorArray[ offset + 1 ] = color.g;
- colorArray[ offset + 2 ] = color.b;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
- }
- if ( dirtyLineDistances ) {
- for ( d = 0; d < dl; d ++ ) {
- lineDistanceArray[ d ] = lineDistances[ d ];
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint );
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( customAttribute.needsUpdate &&
- ( customAttribute.boundTo === undefined ||
- customAttribute.boundTo === "vertices" ) ) {
- offset = 0;
- cal = customAttribute.value.length;
- if ( customAttribute.size === 1 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- customAttribute.array[ ca ] = customAttribute.value[ ca ];
- }
- } else if ( customAttribute.size === 2 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- offset += 2;
- }
- } else if ( customAttribute.size === 3 ) {
- if ( customAttribute.type === "c" ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.r;
- customAttribute.array[ offset + 1 ] = value.g;
- customAttribute.array[ offset + 2 ] = value.b;
- offset += 3;
- }
- } else {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- offset += 3;
- }
- }
- } else if ( customAttribute.size === 4 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- customAttribute.array[ offset + 3 ] = value.w;
- offset += 4;
- }
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
- }
- }
- }
- };
- function setRibbonBuffers ( geometry, hint ) {
- var v, c, n, vertex, offset, color, normal,
- i, il, ca, cal, customAttribute, value,
- vertices = geometry.vertices,
- colors = geometry.colors,
- normals = geometry.normals,
- vl = vertices.length,
- cl = colors.length,
- nl = normals.length,
- vertexArray = geometry.__vertexArray,
- colorArray = geometry.__colorArray,
- normalArray = geometry.__normalArray,
- dirtyVertices = geometry.verticesNeedUpdate,
- dirtyColors = geometry.colorsNeedUpdate,
- dirtyNormals = geometry.normalsNeedUpdate,
- customAttributes = geometry.__webglCustomAttributesList;
- if ( dirtyVertices ) {
- for ( v = 0; v < vl; v ++ ) {
- vertex = vertices[ v ];
- offset = v * 3;
- vertexArray[ offset ] = vertex.x;
- vertexArray[ offset + 1 ] = vertex.y;
- vertexArray[ offset + 2 ] = vertex.z;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
- }
- if ( dirtyColors ) {
- for ( c = 0; c < cl; c ++ ) {
- color = colors[ c ];
- offset = c * 3;
- colorArray[ offset ] = color.r;
- colorArray[ offset + 1 ] = color.g;
- colorArray[ offset + 2 ] = color.b;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
- }
- if ( dirtyNormals ) {
- for ( n = 0; n < nl; n ++ ) {
- normal = normals[ n ];
- offset = n * 3;
- normalArray[ offset ] = normal.x;
- normalArray[ offset + 1 ] = normal.y;
- normalArray[ offset + 2 ] = normal.z;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglNormalBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( customAttribute.needsUpdate &&
- ( customAttribute.boundTo === undefined ||
- customAttribute.boundTo === "vertices" ) ) {
- offset = 0;
- cal = customAttribute.value.length;
- if ( customAttribute.size === 1 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- customAttribute.array[ ca ] = customAttribute.value[ ca ];
- }
- } else if ( customAttribute.size === 2 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- offset += 2;
- }
- } else if ( customAttribute.size === 3 ) {
- if ( customAttribute.type === "c" ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.r;
- customAttribute.array[ offset + 1 ] = value.g;
- customAttribute.array[ offset + 2 ] = value.b;
- offset += 3;
- }
- } else {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- offset += 3;
- }
- }
- } else if ( customAttribute.size === 4 ) {
- for ( ca = 0; ca < cal; ca ++ ) {
- value = customAttribute.value[ ca ];
- customAttribute.array[ offset ] = value.x;
- customAttribute.array[ offset + 1 ] = value.y;
- customAttribute.array[ offset + 2 ] = value.z;
- customAttribute.array[ offset + 3 ] = value.w;
- offset += 4;
- }
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
- }
- }
- }
- };
- function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
- if ( ! geometryGroup.__inittedArrays ) {
- return;
- }
- var normalType = bufferGuessNormalType( material ),
- vertexColorType = bufferGuessVertexColorType( material ),
- uvType = bufferGuessUVType( material ),
- needsSmoothNormals = ( normalType === THREE.SmoothShading );
- var f, fl, fi, face,
- vertexNormals, faceNormal, normal,
- vertexColors, faceColor,
- vertexTangents,
- uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
- c1, c2, c3, c4,
- sw1, sw2, sw3, sw4,
- si1, si2, si3, si4,
- sa1, sa2, sa3, sa4,
- sb1, sb2, sb3, sb4,
- m, ml, i, il,
- vn, uvi, uv2i,
- vk, vkl, vka,
- nka, chf, faceVertexNormals,
- a,
- vertexIndex = 0,
- offset = 0,
- offset_uv = 0,
- offset_uv2 = 0,
- offset_face = 0,
- offset_normal = 0,
- offset_tangent = 0,
- offset_line = 0,
- offset_color = 0,
- offset_skin = 0,
- offset_morphTarget = 0,
- offset_custom = 0,
- offset_customSrc = 0,
- value,
- vertexArray = geometryGroup.__vertexArray,
- uvArray = geometryGroup.__uvArray,
- uv2Array = geometryGroup.__uv2Array,
- normalArray = geometryGroup.__normalArray,
- tangentArray = geometryGroup.__tangentArray,
- colorArray = geometryGroup.__colorArray,
- skinIndexArray = geometryGroup.__skinIndexArray,
- skinWeightArray = geometryGroup.__skinWeightArray,
- morphTargetsArrays = geometryGroup.__morphTargetsArrays,
- morphNormalsArrays = geometryGroup.__morphNormalsArrays,
- customAttributes = geometryGroup.__webglCustomAttributesList,
- customAttribute,
- faceArray = geometryGroup.__faceArray,
- lineArray = geometryGroup.__lineArray,
- geometry = object.geometry, // this is shared for all chunks
- dirtyVertices = geometry.verticesNeedUpdate,
- dirtyElements = geometry.elementsNeedUpdate,
- dirtyUvs = geometry.uvsNeedUpdate,
- dirtyNormals = geometry.normalsNeedUpdate,
- dirtyTangents = geometry.tangentsNeedUpdate,
- dirtyColors = geometry.colorsNeedUpdate,
- dirtyMorphTargets = geometry.morphTargetsNeedUpdate,
- vertices = geometry.vertices,
- chunk_faces3 = geometryGroup.faces3,
- chunk_faces4 = geometryGroup.faces4,
- obj_faces = geometry.faces,
- obj_uvs = geometry.faceVertexUvs[ 0 ],
- obj_uvs2 = geometry.faceVertexUvs[ 1 ],
- obj_colors = geometry.colors,
- obj_skinIndices = geometry.skinIndices,
- obj_skinWeights = geometry.skinWeights,
- morphTargets = geometry.morphTargets,
- morphNormals = geometry.morphNormals;
- if ( dirtyVertices ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- v1 = vertices[ face.a ];
- v2 = vertices[ face.b ];
- v3 = vertices[ face.c ];
- vertexArray[ offset ] = v1.x;
- vertexArray[ offset + 1 ] = v1.y;
- vertexArray[ offset + 2 ] = v1.z;
- vertexArray[ offset + 3 ] = v2.x;
- vertexArray[ offset + 4 ] = v2.y;
- vertexArray[ offset + 5 ] = v2.z;
- vertexArray[ offset + 6 ] = v3.x;
- vertexArray[ offset + 7 ] = v3.y;
- vertexArray[ offset + 8 ] = v3.z;
- offset += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- v1 = vertices[ face.a ];
- v2 = vertices[ face.b ];
- v3 = vertices[ face.c ];
- v4 = vertices[ face.d ];
- vertexArray[ offset ] = v1.x;
- vertexArray[ offset + 1 ] = v1.y;
- vertexArray[ offset + 2 ] = v1.z;
- vertexArray[ offset + 3 ] = v2.x;
- vertexArray[ offset + 4 ] = v2.y;
- vertexArray[ offset + 5 ] = v2.z;
- vertexArray[ offset + 6 ] = v3.x;
- vertexArray[ offset + 7 ] = v3.y;
- vertexArray[ offset + 8 ] = v3.z;
- vertexArray[ offset + 9 ] = v4.x;
- vertexArray[ offset + 10 ] = v4.y;
- vertexArray[ offset + 11 ] = v4.z;
- offset += 12;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
- }
- if ( dirtyMorphTargets ) {
- for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
- offset_morphTarget = 0;
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- chf = chunk_faces3[ f ];
- face = obj_faces[ chf ];
- // morph positions
- v1 = morphTargets[ vk ].vertices[ face.a ];
- v2 = morphTargets[ vk ].vertices[ face.b ];
- v3 = morphTargets[ vk ].vertices[ face.c ];
- vka = morphTargetsArrays[ vk ];
- vka[ offset_morphTarget ] = v1.x;
- vka[ offset_morphTarget + 1 ] = v1.y;
- vka[ offset_morphTarget + 2 ] = v1.z;
- vka[ offset_morphTarget + 3 ] = v2.x;
- vka[ offset_morphTarget + 4 ] = v2.y;
- vka[ offset_morphTarget + 5 ] = v2.z;
- vka[ offset_morphTarget + 6 ] = v3.x;
- vka[ offset_morphTarget + 7 ] = v3.y;
- vka[ offset_morphTarget + 8 ] = v3.z;
- // morph normals
- if ( material.morphNormals ) {
- if ( needsSmoothNormals ) {
- faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
- n1 = faceVertexNormals.a;
- n2 = faceVertexNormals.b;
- n3 = faceVertexNormals.c;
- } else {
- n1 = morphNormals[ vk ].faceNormals[ chf ];
- n2 = n1;
- n3 = n1;
- }
- nka = morphNormalsArrays[ vk ];
- nka[ offset_morphTarget ] = n1.x;
- nka[ offset_morphTarget + 1 ] = n1.y;
- nka[ offset_morphTarget + 2 ] = n1.z;
- nka[ offset_morphTarget + 3 ] = n2.x;
- nka[ offset_morphTarget + 4 ] = n2.y;
- nka[ offset_morphTarget + 5 ] = n2.z;
- nka[ offset_morphTarget + 6 ] = n3.x;
- nka[ offset_morphTarget + 7 ] = n3.y;
- nka[ offset_morphTarget + 8 ] = n3.z;
- }
- //
- offset_morphTarget += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- chf = chunk_faces4[ f ];
- face = obj_faces[ chf ];
- // morph positions
- v1 = morphTargets[ vk ].vertices[ face.a ];
- v2 = morphTargets[ vk ].vertices[ face.b ];
- v3 = morphTargets[ vk ].vertices[ face.c ];
- v4 = morphTargets[ vk ].vertices[ face.d ];
- vka = morphTargetsArrays[ vk ];
- vka[ offset_morphTarget ] = v1.x;
- vka[ offset_morphTarget + 1 ] = v1.y;
- vka[ offset_morphTarget + 2 ] = v1.z;
- vka[ offset_morphTarget + 3 ] = v2.x;
- vka[ offset_morphTarget + 4 ] = v2.y;
- vka[ offset_morphTarget + 5 ] = v2.z;
- vka[ offset_morphTarget + 6 ] = v3.x;
- vka[ offset_morphTarget + 7 ] = v3.y;
- vka[ offset_morphTarget + 8 ] = v3.z;
- vka[ offset_morphTarget + 9 ] = v4.x;
- vka[ offset_morphTarget + 10 ] = v4.y;
- vka[ offset_morphTarget + 11 ] = v4.z;
- // morph normals
- if ( material.morphNormals ) {
- if ( needsSmoothNormals ) {
- faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
- n1 = faceVertexNormals.a;
- n2 = faceVertexNormals.b;
- n3 = faceVertexNormals.c;
- n4 = faceVertexNormals.d;
- } else {
- n1 = morphNormals[ vk ].faceNormals[ chf ];
- n2 = n1;
- n3 = n1;
- n4 = n1;
- }
- nka = morphNormalsArrays[ vk ];
- nka[ offset_morphTarget ] = n1.x;
- nka[ offset_morphTarget + 1 ] = n1.y;
- nka[ offset_morphTarget + 2 ] = n1.z;
- nka[ offset_morphTarget + 3 ] = n2.x;
- nka[ offset_morphTarget + 4 ] = n2.y;
- nka[ offset_morphTarget + 5 ] = n2.z;
- nka[ offset_morphTarget + 6 ] = n3.x;
- nka[ offset_morphTarget + 7 ] = n3.y;
- nka[ offset_morphTarget + 8 ] = n3.z;
- nka[ offset_morphTarget + 9 ] = n4.x;
- nka[ offset_morphTarget + 10 ] = n4.y;
- nka[ offset_morphTarget + 11 ] = n4.z;
- }
- //
- offset_morphTarget += 12;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
- _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
- if ( material.morphNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
- _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
- }
- }
- }
- if ( obj_skinWeights.length ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- // weights
- sw1 = obj_skinWeights[ face.a ];
- sw2 = obj_skinWeights[ face.b ];
- sw3 = obj_skinWeights[ face.c ];
- skinWeightArray[ offset_skin ] = sw1.x;
- skinWeightArray[ offset_skin + 1 ] = sw1.y;
- skinWeightArray[ offset_skin + 2 ] = sw1.z;
- skinWeightArray[ offset_skin + 3 ] = sw1.w;
- skinWeightArray[ offset_skin + 4 ] = sw2.x;
- skinWeightArray[ offset_skin + 5 ] = sw2.y;
- skinWeightArray[ offset_skin + 6 ] = sw2.z;
- skinWeightArray[ offset_skin + 7 ] = sw2.w;
- skinWeightArray[ offset_skin + 8 ] = sw3.x;
- skinWeightArray[ offset_skin + 9 ] = sw3.y;
- skinWeightArray[ offset_skin + 10 ] = sw3.z;
- skinWeightArray[ offset_skin + 11 ] = sw3.w;
- // indices
- si1 = obj_skinIndices[ face.a ];
- si2 = obj_skinIndices[ face.b ];
- si3 = obj_skinIndices[ face.c ];
- skinIndexArray[ offset_skin ] = si1.x;
- skinIndexArray[ offset_skin + 1 ] = si1.y;
- skinIndexArray[ offset_skin + 2 ] = si1.z;
- skinIndexArray[ offset_skin + 3 ] = si1.w;
- skinIndexArray[ offset_skin + 4 ] = si2.x;
- skinIndexArray[ offset_skin + 5 ] = si2.y;
- skinIndexArray[ offset_skin + 6 ] = si2.z;
- skinIndexArray[ offset_skin + 7 ] = si2.w;
- skinIndexArray[ offset_skin + 8 ] = si3.x;
- skinIndexArray[ offset_skin + 9 ] = si3.y;
- skinIndexArray[ offset_skin + 10 ] = si3.z;
- skinIndexArray[ offset_skin + 11 ] = si3.w;
- offset_skin += 12;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- // weights
- sw1 = obj_skinWeights[ face.a ];
- sw2 = obj_skinWeights[ face.b ];
- sw3 = obj_skinWeights[ face.c ];
- sw4 = obj_skinWeights[ face.d ];
- skinWeightArray[ offset_skin ] = sw1.x;
- skinWeightArray[ offset_skin + 1 ] = sw1.y;
- skinWeightArray[ offset_skin + 2 ] = sw1.z;
- skinWeightArray[ offset_skin + 3 ] = sw1.w;
- skinWeightArray[ offset_skin + 4 ] = sw2.x;
- skinWeightArray[ offset_skin + 5 ] = sw2.y;
- skinWeightArray[ offset_skin + 6 ] = sw2.z;
- skinWeightArray[ offset_skin + 7 ] = sw2.w;
- skinWeightArray[ offset_skin + 8 ] = sw3.x;
- skinWeightArray[ offset_skin + 9 ] = sw3.y;
- skinWeightArray[ offset_skin + 10 ] = sw3.z;
- skinWeightArray[ offset_skin + 11 ] = sw3.w;
- skinWeightArray[ offset_skin + 12 ] = sw4.x;
- skinWeightArray[ offset_skin + 13 ] = sw4.y;
- skinWeightArray[ offset_skin + 14 ] = sw4.z;
- skinWeightArray[ offset_skin + 15 ] = sw4.w;
- // indices
- si1 = obj_skinIndices[ face.a ];
- si2 = obj_skinIndices[ face.b ];
- si3 = obj_skinIndices[ face.c ];
- si4 = obj_skinIndices[ face.d ];
- skinIndexArray[ offset_skin ] = si1.x;
- skinIndexArray[ offset_skin + 1 ] = si1.y;
- skinIndexArray[ offset_skin + 2 ] = si1.z;
- skinIndexArray[ offset_skin + 3 ] = si1.w;
- skinIndexArray[ offset_skin + 4 ] = si2.x;
- skinIndexArray[ offset_skin + 5 ] = si2.y;
- skinIndexArray[ offset_skin + 6 ] = si2.z;
- skinIndexArray[ offset_skin + 7 ] = si2.w;
- skinIndexArray[ offset_skin + 8 ] = si3.x;
- skinIndexArray[ offset_skin + 9 ] = si3.y;
- skinIndexArray[ offset_skin + 10 ] = si3.z;
- skinIndexArray[ offset_skin + 11 ] = si3.w;
- skinIndexArray[ offset_skin + 12 ] = si4.x;
- skinIndexArray[ offset_skin + 13 ] = si4.y;
- skinIndexArray[ offset_skin + 14 ] = si4.z;
- skinIndexArray[ offset_skin + 15 ] = si4.w;
- offset_skin += 16;
- }
- if ( offset_skin > 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
- }
- }
- if ( dirtyColors && vertexColorType ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- vertexColors = face.vertexColors;
- faceColor = face.color;
- if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) {
- c1 = vertexColors[ 0 ];
- c2 = vertexColors[ 1 ];
- c3 = vertexColors[ 2 ];
- } else {
- c1 = faceColor;
- c2 = faceColor;
- c3 = faceColor;
- }
- colorArray[ offset_color ] = c1.r;
- colorArray[ offset_color + 1 ] = c1.g;
- colorArray[ offset_color + 2 ] = c1.b;
- colorArray[ offset_color + 3 ] = c2.r;
- colorArray[ offset_color + 4 ] = c2.g;
- colorArray[ offset_color + 5 ] = c2.b;
- colorArray[ offset_color + 6 ] = c3.r;
- colorArray[ offset_color + 7 ] = c3.g;
- colorArray[ offset_color + 8 ] = c3.b;
- offset_color += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- vertexColors = face.vertexColors;
- faceColor = face.color;
- if ( vertexColors.length === 4 && vertexColorType === THREE.VertexColors ) {
- c1 = vertexColors[ 0 ];
- c2 = vertexColors[ 1 ];
- c3 = vertexColors[ 2 ];
- c4 = vertexColors[ 3 ];
- } else {
- c1 = faceColor;
- c2 = faceColor;
- c3 = faceColor;
- c4 = faceColor;
- }
- colorArray[ offset_color ] = c1.r;
- colorArray[ offset_color + 1 ] = c1.g;
- colorArray[ offset_color + 2 ] = c1.b;
- colorArray[ offset_color + 3 ] = c2.r;
- colorArray[ offset_color + 4 ] = c2.g;
- colorArray[ offset_color + 5 ] = c2.b;
- colorArray[ offset_color + 6 ] = c3.r;
- colorArray[ offset_color + 7 ] = c3.g;
- colorArray[ offset_color + 8 ] = c3.b;
- colorArray[ offset_color + 9 ] = c4.r;
- colorArray[ offset_color + 10 ] = c4.g;
- colorArray[ offset_color + 11 ] = c4.b;
- offset_color += 12;
- }
- if ( offset_color > 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
- }
- }
- if ( dirtyTangents && geometry.hasTangents ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- vertexTangents = face.vertexTangents;
- t1 = vertexTangents[ 0 ];
- t2 = vertexTangents[ 1 ];
- t3 = vertexTangents[ 2 ];
- tangentArray[ offset_tangent ] = t1.x;
- tangentArray[ offset_tangent + 1 ] = t1.y;
- tangentArray[ offset_tangent + 2 ] = t1.z;
- tangentArray[ offset_tangent + 3 ] = t1.w;
- tangentArray[ offset_tangent + 4 ] = t2.x;
- tangentArray[ offset_tangent + 5 ] = t2.y;
- tangentArray[ offset_tangent + 6 ] = t2.z;
- tangentArray[ offset_tangent + 7 ] = t2.w;
- tangentArray[ offset_tangent + 8 ] = t3.x;
- tangentArray[ offset_tangent + 9 ] = t3.y;
- tangentArray[ offset_tangent + 10 ] = t3.z;
- tangentArray[ offset_tangent + 11 ] = t3.w;
- offset_tangent += 12;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- vertexTangents = face.vertexTangents;
- t1 = vertexTangents[ 0 ];
- t2 = vertexTangents[ 1 ];
- t3 = vertexTangents[ 2 ];
- t4 = vertexTangents[ 3 ];
- tangentArray[ offset_tangent ] = t1.x;
- tangentArray[ offset_tangent + 1 ] = t1.y;
- tangentArray[ offset_tangent + 2 ] = t1.z;
- tangentArray[ offset_tangent + 3 ] = t1.w;
- tangentArray[ offset_tangent + 4 ] = t2.x;
- tangentArray[ offset_tangent + 5 ] = t2.y;
- tangentArray[ offset_tangent + 6 ] = t2.z;
- tangentArray[ offset_tangent + 7 ] = t2.w;
- tangentArray[ offset_tangent + 8 ] = t3.x;
- tangentArray[ offset_tangent + 9 ] = t3.y;
- tangentArray[ offset_tangent + 10 ] = t3.z;
- tangentArray[ offset_tangent + 11 ] = t3.w;
- tangentArray[ offset_tangent + 12 ] = t4.x;
- tangentArray[ offset_tangent + 13 ] = t4.y;
- tangentArray[ offset_tangent + 14 ] = t4.z;
- tangentArray[ offset_tangent + 15 ] = t4.w;
- offset_tangent += 16;
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
- }
- if ( dirtyNormals && normalType ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- vertexNormals = face.vertexNormals;
- faceNormal = face.normal;
- if ( vertexNormals.length === 3 && needsSmoothNormals ) {
- for ( i = 0; i < 3; i ++ ) {
- vn = vertexNormals[ i ];
- normalArray[ offset_normal ] = vn.x;
- normalArray[ offset_normal + 1 ] = vn.y;
- normalArray[ offset_normal + 2 ] = vn.z;
- offset_normal += 3;
- }
- } else {
- for ( i = 0; i < 3; i ++ ) {
- normalArray[ offset_normal ] = faceNormal.x;
- normalArray[ offset_normal + 1 ] = faceNormal.y;
- normalArray[ offset_normal + 2 ] = faceNormal.z;
- offset_normal += 3;
- }
- }
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- vertexNormals = face.vertexNormals;
- faceNormal = face.normal;
- if ( vertexNormals.length === 4 && needsSmoothNormals ) {
- for ( i = 0; i < 4; i ++ ) {
- vn = vertexNormals[ i ];
- normalArray[ offset_normal ] = vn.x;
- normalArray[ offset_normal + 1 ] = vn.y;
- normalArray[ offset_normal + 2 ] = vn.z;
- offset_normal += 3;
- }
- } else {
- for ( i = 0; i < 4; i ++ ) {
- normalArray[ offset_normal ] = faceNormal.x;
- normalArray[ offset_normal + 1 ] = faceNormal.y;
- normalArray[ offset_normal + 2 ] = faceNormal.z;
- offset_normal += 3;
- }
- }
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
- }
- if ( dirtyUvs && obj_uvs && uvType ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- fi = chunk_faces3[ f ];
- uv = obj_uvs[ fi ];
- if ( uv === undefined ) continue;
- for ( i = 0; i < 3; i ++ ) {
- uvi = uv[ i ];
- uvArray[ offset_uv ] = uvi.x;
- uvArray[ offset_uv + 1 ] = uvi.y;
- offset_uv += 2;
- }
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- fi = chunk_faces4[ f ];
- uv = obj_uvs[ fi ];
- if ( uv === undefined ) continue;
- for ( i = 0; i < 4; i ++ ) {
- uvi = uv[ i ];
- uvArray[ offset_uv ] = uvi.x;
- uvArray[ offset_uv + 1 ] = uvi.y;
- offset_uv += 2;
- }
- }
- if ( offset_uv > 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
- }
- }
- if ( dirtyUvs && obj_uvs2 && uvType ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- fi = chunk_faces3[ f ];
- uv2 = obj_uvs2[ fi ];
- if ( uv2 === undefined ) continue;
- for ( i = 0; i < 3; i ++ ) {
- uv2i = uv2[ i ];
- uv2Array[ offset_uv2 ] = uv2i.x;
- uv2Array[ offset_uv2 + 1 ] = uv2i.y;
- offset_uv2 += 2;
- }
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- fi = chunk_faces4[ f ];
- uv2 = obj_uvs2[ fi ];
- if ( uv2 === undefined ) continue;
- for ( i = 0; i < 4; i ++ ) {
- uv2i = uv2[ i ];
- uv2Array[ offset_uv2 ] = uv2i.x;
- uv2Array[ offset_uv2 + 1 ] = uv2i.y;
- offset_uv2 += 2;
- }
- }
- if ( offset_uv2 > 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
- }
- }
- if ( dirtyElements ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- faceArray[ offset_face ] = vertexIndex;
- faceArray[ offset_face + 1 ] = vertexIndex + 1;
- faceArray[ offset_face + 2 ] = vertexIndex + 2;
- offset_face += 3;
- lineArray[ offset_line ] = vertexIndex;
- lineArray[ offset_line + 1 ] = vertexIndex + 1;
- lineArray[ offset_line + 2 ] = vertexIndex;
- lineArray[ offset_line + 3 ] = vertexIndex + 2;
- lineArray[ offset_line + 4 ] = vertexIndex + 1;
- lineArray[ offset_line + 5 ] = vertexIndex + 2;
- offset_line += 6;
- vertexIndex += 3;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- faceArray[ offset_face ] = vertexIndex;
- faceArray[ offset_face + 1 ] = vertexIndex + 1;
- faceArray[ offset_face + 2 ] = vertexIndex + 3;
- faceArray[ offset_face + 3 ] = vertexIndex + 1;
- faceArray[ offset_face + 4 ] = vertexIndex + 2;
- faceArray[ offset_face + 5 ] = vertexIndex + 3;
- offset_face += 6;
- lineArray[ offset_line ] = vertexIndex;
- lineArray[ offset_line + 1 ] = vertexIndex + 1;
- lineArray[ offset_line + 2 ] = vertexIndex;
- lineArray[ offset_line + 3 ] = vertexIndex + 3;
- lineArray[ offset_line + 4 ] = vertexIndex + 1;
- lineArray[ offset_line + 5 ] = vertexIndex + 2;
- lineArray[ offset_line + 6 ] = vertexIndex + 2;
- lineArray[ offset_line + 7 ] = vertexIndex + 3;
- offset_line += 8;
- vertexIndex += 4;
- }
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
- _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
- _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
- }
- if ( customAttributes ) {
- for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
- customAttribute = customAttributes[ i ];
- if ( ! customAttribute.__original.needsUpdate ) continue;
- offset_custom = 0;
- offset_customSrc = 0;
- if ( customAttribute.size === 1 ) {
- if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
- customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
- customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
- offset_custom += 3;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
- customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
- customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
- customAttribute.array[ offset_custom + 3 ] = customAttribute.value[ face.d ];
- offset_custom += 4;
- }
- } else if ( customAttribute.boundTo === "faces" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- customAttribute.array[ offset_custom ] = value;
- customAttribute.array[ offset_custom + 1 ] = value;
- customAttribute.array[ offset_custom + 2 ] = value;
- offset_custom += 3;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- customAttribute.array[ offset_custom ] = value;
- customAttribute.array[ offset_custom + 1 ] = value;
- customAttribute.array[ offset_custom + 2 ] = value;
- customAttribute.array[ offset_custom + 3 ] = value;
- offset_custom += 4;
- }
- }
- } else if ( customAttribute.size === 2 ) {
- if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v2.x;
- customAttribute.array[ offset_custom + 3 ] = v2.y;
- customAttribute.array[ offset_custom + 4 ] = v3.x;
- customAttribute.array[ offset_custom + 5 ] = v3.y;
- offset_custom += 6;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- v4 = customAttribute.value[ face.d ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v2.x;
- customAttribute.array[ offset_custom + 3 ] = v2.y;
- customAttribute.array[ offset_custom + 4 ] = v3.x;
- customAttribute.array[ offset_custom + 5 ] = v3.y;
- customAttribute.array[ offset_custom + 6 ] = v4.x;
- customAttribute.array[ offset_custom + 7 ] = v4.y;
- offset_custom += 8;
- }
- } else if ( customAttribute.boundTo === "faces" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v2.x;
- customAttribute.array[ offset_custom + 3 ] = v2.y;
- customAttribute.array[ offset_custom + 4 ] = v3.x;
- customAttribute.array[ offset_custom + 5 ] = v3.y;
- offset_custom += 6;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- v4 = value;
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v2.x;
- customAttribute.array[ offset_custom + 3 ] = v2.y;
- customAttribute.array[ offset_custom + 4 ] = v3.x;
- customAttribute.array[ offset_custom + 5 ] = v3.y;
- customAttribute.array[ offset_custom + 6 ] = v4.x;
- customAttribute.array[ offset_custom + 7 ] = v4.y;
- offset_custom += 8;
- }
- }
- } else if ( customAttribute.size === 3 ) {
- var pp;
- if ( customAttribute.type === "c" ) {
- pp = [ "r", "g", "b" ];
- } else {
- pp = [ "x", "y", "z" ];
- }
- if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- offset_custom += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- v4 = customAttribute.value[ face.d ];
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
- offset_custom += 12;
- }
- } else if ( customAttribute.boundTo === "faces" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- offset_custom += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- v4 = value;
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
- offset_custom += 12;
- }
- } else if ( customAttribute.boundTo === "faceVertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- v1 = value[ 0 ];
- v2 = value[ 1 ];
- v3 = value[ 2 ];
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- offset_custom += 9;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- v1 = value[ 0 ];
- v2 = value[ 1 ];
- v3 = value[ 2 ];
- v4 = value[ 3 ];
- customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
- customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
- customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
- customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
- offset_custom += 12;
- }
- }
- } else if ( customAttribute.size === 4 ) {
- if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces3[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- offset_custom += 12;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- face = obj_faces[ chunk_faces4[ f ] ];
- v1 = customAttribute.value[ face.a ];
- v2 = customAttribute.value[ face.b ];
- v3 = customAttribute.value[ face.c ];
- v4 = customAttribute.value[ face.d ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- customAttribute.array[ offset_custom + 12 ] = v4.x;
- customAttribute.array[ offset_custom + 13 ] = v4.y;
- customAttribute.array[ offset_custom + 14 ] = v4.z;
- customAttribute.array[ offset_custom + 15 ] = v4.w;
- offset_custom += 16;
- }
- } else if ( customAttribute.boundTo === "faces" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- offset_custom += 12;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- v1 = value;
- v2 = value;
- v3 = value;
- v4 = value;
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- customAttribute.array[ offset_custom + 12 ] = v4.x;
- customAttribute.array[ offset_custom + 13 ] = v4.y;
- customAttribute.array[ offset_custom + 14 ] = v4.z;
- customAttribute.array[ offset_custom + 15 ] = v4.w;
- offset_custom += 16;
- }
- } else if ( customAttribute.boundTo === "faceVertices" ) {
- for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces3[ f ] ];
- v1 = value[ 0 ];
- v2 = value[ 1 ];
- v3 = value[ 2 ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- offset_custom += 12;
- }
- for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
- value = customAttribute.value[ chunk_faces4[ f ] ];
- v1 = value[ 0 ];
- v2 = value[ 1 ];
- v3 = value[ 2 ];
- v4 = value[ 3 ];
- customAttribute.array[ offset_custom ] = v1.x;
- customAttribute.array[ offset_custom + 1 ] = v1.y;
- customAttribute.array[ offset_custom + 2 ] = v1.z;
- customAttribute.array[ offset_custom + 3 ] = v1.w;
- customAttribute.array[ offset_custom + 4 ] = v2.x;
- customAttribute.array[ offset_custom + 5 ] = v2.y;
- customAttribute.array[ offset_custom + 6 ] = v2.z;
- customAttribute.array[ offset_custom + 7 ] = v2.w;
- customAttribute.array[ offset_custom + 8 ] = v3.x;
- customAttribute.array[ offset_custom + 9 ] = v3.y;
- customAttribute.array[ offset_custom + 10 ] = v3.z;
- customAttribute.array[ offset_custom + 11 ] = v3.w;
- customAttribute.array[ offset_custom + 12 ] = v4.x;
- customAttribute.array[ offset_custom + 13 ] = v4.y;
- customAttribute.array[ offset_custom + 14 ] = v4.z;
- customAttribute.array[ offset_custom + 15 ] = v4.w;
- offset_custom += 16;
- }
- }
- }
- _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
- }
- }
- if ( dispose ) {
- delete geometryGroup.__inittedArrays;
- delete geometryGroup.__colorArray;
- delete geometryGroup.__normalArray;
- delete geometryGroup.__tangentArray;
- delete geometryGroup.__uvArray;
- delete geometryGroup.__uv2Array;
- delete geometryGroup.__faceArray;
- delete geometryGroup.__vertexArray;
- delete geometryGroup.__lineArray;
- delete geometryGroup.__skinIndexArray;
- delete geometryGroup.__skinWeightArray;
- }
- };
- function setDirectBuffers ( geometry, hint, dispose ) {
- var attributes = geometry.attributes;
- var attributeName, attributeItem;
- for ( attributeName in attributes ) {
- attributeItem = attributes[ attributeName ];
- if ( attributeItem.needsUpdate ) {
- if ( attributeName === 'index' ) {
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, attributeItem.buffer );
- _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, attributeItem.array, hint );
- } else {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, attributeItem.array, hint );
- }
- attributeItem.needsUpdate = false;
- }
- if ( dispose && ! attributeItem.dynamic ) {
- attributeItem.array = null;
- }
- }
- };
- // Buffer rendering
- this.renderBufferImmediate = function ( object, program, material ) {
- if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
- if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
- if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
- if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
- if ( object.hasPositions ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
- _gl.enableVertexAttribArray( program.attributes.position );
- _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
- if ( material.shading === THREE.FlatShading ) {
- var nx, ny, nz,
- nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
- normalArray,
- i, il = object.count * 3;
- for( i = 0; i < il; i += 9 ) {
- normalArray = object.normalArray;
- nax = normalArray[ i ];
- nay = normalArray[ i + 1 ];
- naz = normalArray[ i + 2 ];
- nbx = normalArray[ i + 3 ];
- nby = normalArray[ i + 4 ];
- nbz = normalArray[ i + 5 ];
- ncx = normalArray[ i + 6 ];
- ncy = normalArray[ i + 7 ];
- ncz = normalArray[ i + 8 ];
- nx = ( nax + nbx + ncx ) / 3;
- ny = ( nay + nby + ncy ) / 3;
- nz = ( naz + nbz + ncz ) / 3;
- normalArray[ i ] = nx;
- normalArray[ i + 1 ] = ny;
- normalArray[ i + 2 ] = nz;
- normalArray[ i + 3 ] = nx;
- normalArray[ i + 4 ] = ny;
- normalArray[ i + 5 ] = nz;
- normalArray[ i + 6 ] = nx;
- normalArray[ i + 7 ] = ny;
- normalArray[ i + 8 ] = nz;
- }
- }
- _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
- _gl.enableVertexAttribArray( program.attributes.normal );
- _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasUvs && material.map ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
- _gl.enableVertexAttribArray( program.attributes.uv );
- _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
- _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
- _gl.enableVertexAttribArray( program.attributes.color );
- _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
- }
- _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
- object.count = 0;
- };
- this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
- if ( material.visible === false ) return;
- var linewidth, a, attribute;
- var attributeItem, attributeName, attributePointer, attributeSize;
- var program = setProgram( camera, lights, fog, material, object );
- var programAttributes = program.attributes;
- var geometryAttributes = geometry.attributes;
- var updateBuffers = false,
- wireframeBit = material.wireframe ? 1 : 0,
- geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
- if ( geometryHash !== _currentGeometryGroupHash ) {
- _currentGeometryGroupHash = geometryHash;
- updateBuffers = true;
- }
- if ( updateBuffers ) {
- disableAttributes();
- }
- // render mesh
- if ( object instanceof THREE.Mesh ) {
- var index = geometryAttributes[ "index" ];
- // indexed triangles
- if ( index ) {
- var offsets = geometry.offsets;
- // if there is more than 1 chunk
- // must set attribute pointers to use new offsets for each chunk
- // even if geometry and materials didn't change
- if ( offsets.length > 1 ) updateBuffers = true;
- for ( var i = 0, il = offsets.length; i < il; i ++ ) {
- var startIndex = offsets[ i ].index;
- if ( updateBuffers ) {
- for ( attributeName in geometryAttributes ) {
- if ( attributeName === 'index' ) continue;
- attributePointer = programAttributes[ attributeName ];
- attributeItem = geometryAttributes[ attributeName ];
- attributeSize = attributeItem.itemSize;
- if ( attributePointer >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
- enableAttribute( attributePointer );
- _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, startIndex * attributeSize * 4 ); // 4 bytes per Float32
- }
- }
- // indices
- _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
- }
- // render indexed triangles
- _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 bytes per Uint16
- _this.info.render.calls ++;
- _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
- _this.info.render.faces += offsets[ i ].count / 3;
- }
- // non-indexed triangles
- } else {
- if ( updateBuffers ) {
- for ( attributeName in geometryAttributes ) {
- if ( attributeName === 'index') continue;
- attributePointer = programAttributes[ attributeName ];
- attributeItem = geometryAttributes[ attributeName ];
- attributeSize = attributeItem.itemSize;
- if ( attributePointer >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
- enableAttribute( attributePointer );
- _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
- }
- }
- }
- var position = geometry.attributes[ "position" ];
- // render non-indexed triangles
- _gl.drawArrays( _gl.TRIANGLES, 0, position.numItems / 3 );
- _this.info.render.calls ++;
- _this.info.render.vertices += position.numItems / 3;
- _this.info.render.faces += position.numItems / 3 / 3;
- }
- // render particles
- } else if ( object instanceof THREE.ParticleSystem ) {
- if ( updateBuffers ) {
- for ( attributeName in geometryAttributes ) {
- attributePointer = programAttributes[ attributeName ];
- attributeItem = geometryAttributes[ attributeName ];
- attributeSize = attributeItem.itemSize;
- if ( attributePointer >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
- enableAttribute( attributePointer );
- _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
- }
- }
- var position = geometryAttributes[ "position" ];
- // render particles
- _gl.drawArrays( _gl.POINTS, 0, position.numItems / 3 );
- _this.info.render.calls ++;
- _this.info.render.points += position.numItems / 3;
- }
- } else if ( object instanceof THREE.Line ) {
- if ( updateBuffers ) {
- for ( attributeName in geometryAttributes ) {
- attributePointer = programAttributes[ attributeName ];
- attributeItem = geometryAttributes[ attributeName ];
- attributeSize = attributeItem.itemSize;
- if ( attributePointer >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attributeItem.buffer );
- enableAttribute( attributePointer );
- _gl.vertexAttribPointer( attributePointer, attributeSize, _gl.FLOAT, false, 0, 0 );
- }
- }
- // render lines
- var primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
- setLineWidth( material.linewidth );
- var position = geometryAttributes[ "position" ];
- _gl.drawArrays( primitives, 0, position.numItems / 3 );
- _this.info.render.calls ++;
- _this.info.render.points += position.numItems;
- }
- }
- };
- this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
- if ( material.visible === false ) return;
- var linewidth, a, attribute, i, il;
- var program = setProgram( camera, lights, fog, material, object );
- var attributes = program.attributes;
- var updateBuffers = false,
- wireframeBit = material.wireframe ? 1 : 0,
- geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
- if ( geometryGroupHash !== _currentGeometryGroupHash ) {
- _currentGeometryGroupHash = geometryGroupHash;
- updateBuffers = true;
- }
- if ( updateBuffers ) {
- disableAttributes();
- }
- // vertices
- if ( !material.morphTargets && attributes.position >= 0 ) {
- if ( updateBuffers ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
- enableAttribute( attributes.position );
- _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- }
- } else {
- if ( object.morphTargetBase ) {
- setupMorphTargets( material, geometryGroup, object );
- }
- }
- if ( updateBuffers ) {
- // custom attributes
- // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
- if ( geometryGroup.__webglCustomAttributesList ) {
- for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
- attribute = geometryGroup.__webglCustomAttributesList[ i ];
- if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
- enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
- _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
- }
- }
- }
- // colors
- if ( attributes.color >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
- enableAttribute( attributes.color );
- _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
- }
- // normals
- if ( attributes.normal >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
- enableAttribute( attributes.normal );
- _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
- }
- // tangents
- if ( attributes.tangent >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
- enableAttribute( attributes.tangent );
- _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
- }
- // uvs
- if ( attributes.uv >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
- enableAttribute( attributes.uv );
- _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
- }
- if ( attributes.uv2 >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
- enableAttribute( attributes.uv2 );
- _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
- }
- if ( material.skinning &&
- attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
- enableAttribute( attributes.skinIndex );
- _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
- enableAttribute( attributes.skinWeight );
- _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
- }
- // line distances
- if ( attributes.lineDistance >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
- enableAttribute( attributes.lineDistance );
- _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
- }
- }
- // render mesh
- if ( object instanceof THREE.Mesh ) {
- // wireframe
- if ( material.wireframe ) {
- setLineWidth( material.wireframeLinewidth );
- if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
- _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
- // triangles
- } else {
- if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
- _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
- }
- _this.info.render.calls ++;
- _this.info.render.vertices += geometryGroup.__webglFaceCount;
- _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
- // render lines
- } else if ( object instanceof THREE.Line ) {
- var primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
- setLineWidth( material.linewidth );
- _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
- _this.info.render.calls ++;
- // render particles
- } else if ( object instanceof THREE.ParticleSystem ) {
- _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
- _this.info.render.calls ++;
- _this.info.render.points += geometryGroup.__webglParticleCount;
- // render ribbon
- } else if ( object instanceof THREE.Ribbon ) {
- _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
- _this.info.render.calls ++;
- }
- };
- function enableAttribute( attribute ) {
- if ( ! _enabledAttributes[ attribute ] ) {
- _gl.enableVertexAttribArray( attribute );
- _enabledAttributes[ attribute ] = true;
- }
- };
- function disableAttributes() {
- for ( var attribute in _enabledAttributes ) {
- if ( _enabledAttributes[ attribute ] ) {
- _gl.disableVertexAttribArray( attribute );
- _enabledAttributes[ attribute ] = false;
- }
- }
- };
- function setupMorphTargets ( material, geometryGroup, object ) {
- // set base
- var attributes = material.program.attributes;
- if ( object.morphTargetBase !== -1 && attributes.position >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
- enableAttribute( attributes.position );
- _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- } else if ( attributes.position >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
- enableAttribute( attributes.position );
- _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( object.morphTargetForcedOrder.length ) {
- // set forced order
- var m = 0;
- var order = object.morphTargetForcedOrder;
- var influences = object.morphTargetInfluences;
- while ( m < material.numSupportedMorphTargets && m < order.length ) {
- if ( attributes[ "morphTarget" + m ] >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
- enableAttribute( attributes[ "morphTarget" + m ] );
- _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
- enableAttribute( attributes[ "morphNormal" + m ] );
- _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
- m ++;
- }
- } else {
- // find the most influencing
- var influence, activeInfluenceIndices = [];
- var influences = object.morphTargetInfluences;
- var i, il = influences.length;
- for ( i = 0; i < il; i ++ ) {
- influence = influences[ i ];
- if ( influence > 0 ) {
- activeInfluenceIndices.push( [ influence, i ] );
- }
- }
- if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
- activeInfluenceIndices.sort( numericalSort );
- activeInfluenceIndices.length = material.numSupportedMorphTargets;
- } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
- activeInfluenceIndices.sort( numericalSort );
- } else if ( activeInfluenceIndices.length === 0 ) {
- activeInfluenceIndices.push( [ 0, 0 ] );
- };
- var influenceIndex, m = 0;
- while ( m < material.numSupportedMorphTargets ) {
- if ( activeInfluenceIndices[ m ] ) {
- influenceIndex = activeInfluenceIndices[ m ][ 1 ];
- if ( attributes[ "morphTarget" + m ] >= 0 ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
- enableAttribute( attributes[ "morphTarget" + m ] );
- _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- if ( attributes[ "morphNormal" + m ] >= 0 && material.morphNormals ) {
- _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
- enableAttribute( attributes[ "morphNormal" + m ] );
- _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
- } else {
- /*
- _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
- if ( material.morphNormals ) {
- _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
- }
- */
- object.__webglMorphTargetInfluences[ m ] = 0;
- }
- m ++;
- }
- }
- // load updated influences uniform
- if ( material.program.uniforms.morphTargetInfluences !== null ) {
- _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
- }
- };
- // Sorting
- function painterSortStable ( a, b ) {
- if ( a.z !== b.z ) {
- return b.z - a.z;
- } else {
- return a.id - b.id;
- }
- };
- function numericalSort ( a, b ) {
- return b[ 0 ] - a[ 0 ];
- };
- // Rendering
- this.render = function ( scene, camera, renderTarget, forceClear ) {
- if ( camera instanceof THREE.Camera === false ) {
- console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
- return;
- }
- var i, il,
- webglObject, object,
- renderList,
- lights = scene.__lights,
- fog = scene.fog;
- // reset caching for this frame
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- // update scene graph
- if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
- // update camera matrices and frustum
- if ( camera.parent === undefined ) camera.updateMatrixWorld();
- camera.matrixWorldInverse.getInverse( camera.matrixWorld );
- _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
- _frustum.setFromMatrix( _projScreenMatrix );
- // update WebGL objects
- if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
- // custom render plugins (pre pass)
- renderPlugins( this.renderPluginsPre, scene, camera );
- //
- _this.info.render.calls = 0;
- _this.info.render.vertices = 0;
- _this.info.render.faces = 0;
- _this.info.render.points = 0;
- this.setRenderTarget( renderTarget );
- if ( this.autoClear || forceClear ) {
- this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
- }
- // set matrices for regular objects (frustum culled)
- renderList = scene.__webglObjects;
- for ( i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- webglObject.id = i;
- webglObject.render = false;
- if ( object.visible ) {
- if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.intersectsObject( object ) ) {
- setupMatrices( object, camera );
- unrollBufferMaterial( webglObject );
- webglObject.render = true;
- if ( this.sortObjects === true ) {
- if ( object.renderDepth !== null ) {
- webglObject.z = object.renderDepth;
- } else {
- _vector3.getPositionFromMatrix( object.matrixWorld );
- _vector3.applyProjection( _projScreenMatrix );
- webglObject.z = _vector3.z;
- }
- }
- }
- }
- }
- if ( this.sortObjects ) {
- renderList.sort( painterSortStable );
- }
- // set matrices for immediate objects
- renderList = scene.__webglObjectsImmediate;
- for ( i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- if ( object.visible ) {
- setupMatrices( object, camera );
- unrollImmediateBufferMaterial( webglObject );
- }
- }
- if ( scene.overrideMaterial ) {
- var material = scene.overrideMaterial;
- this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- this.setDepthTest( material.depthTest );
- this.setDepthWrite( material.depthWrite );
- setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
- } else {
- var material = null;
- // opaque pass (front-to-back order)
- this.setBlending( THREE.NoBlending );
- renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false, material );
- // transparent pass (back-to-front order)
- renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true, material );
- renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true, material );
- }
- // custom render plugins (post pass)
- renderPlugins( this.renderPluginsPost, scene, camera );
- // Generate mipmap if we're using any kind of mipmap filtering
- if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
- updateRenderTargetMipmap( renderTarget );
- }
- // Ensure depth buffer writing is enabled so it can be cleared on next render
- this.setDepthTest( true );
- this.setDepthWrite( true );
- // _gl.finish();
- };
- function renderPlugins( plugins, scene, camera ) {
- if ( ! plugins.length ) return;
- for ( var i = 0, il = plugins.length; i < il; i ++ ) {
- // reset state for plugin (to start from clean slate)
- _currentProgram = null;
- _currentCamera = null;
- _oldBlending = -1;
- _oldDepthTest = -1;
- _oldDepthWrite = -1;
- _oldDoubleSided = -1;
- _oldFlipSided = -1;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
- // reset state after plugin (anything could have changed)
- _currentProgram = null;
- _currentCamera = null;
- _oldBlending = -1;
- _oldDepthTest = -1;
- _oldDepthWrite = -1;
- _oldDoubleSided = -1;
- _oldFlipSided = -1;
- _currentGeometryGroupHash = -1;
- _currentMaterialId = -1;
- _lightsNeedUpdate = true;
- }
- };
- function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
- var webglObject, object, buffer, material, start, end, delta;
- if ( reverse ) {
- start = renderList.length - 1;
- end = -1;
- delta = -1;
- } else {
- start = 0;
- end = renderList.length;
- delta = 1;
- }
- for ( var i = start; i !== end; i += delta ) {
- webglObject = renderList[ i ];
- if ( webglObject.render ) {
- object = webglObject.object;
- buffer = webglObject.buffer;
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject[ materialType ];
- if ( ! material ) continue;
- if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- _this.setDepthTest( material.depthTest );
- _this.setDepthWrite( material.depthWrite );
- setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- }
- _this.setMaterialFaces( material );
- if ( buffer instanceof THREE.BufferGeometry ) {
- _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
- } else {
- _this.renderBuffer( camera, lights, fog, material, buffer, object );
- }
- }
- }
- };
- function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
- var webglObject, object, material, program;
- for ( var i = 0, il = renderList.length; i < il; i ++ ) {
- webglObject = renderList[ i ];
- object = webglObject.object;
- if ( object.visible ) {
- if ( overrideMaterial ) {
- material = overrideMaterial;
- } else {
- material = webglObject[ materialType ];
- if ( ! material ) continue;
- if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
- _this.setDepthTest( material.depthTest );
- _this.setDepthWrite( material.depthWrite );
- setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
- }
- _this.renderImmediateObject( camera, lights, fog, material, object );
- }
- }
- };
- this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
- var program = setProgram( camera, lights, fog, material, object );
- _currentGeometryGroupHash = -1;
- _this.setMaterialFaces( material );
- if ( object.immediateRenderCallback ) {
- object.immediateRenderCallback( program, _gl, _frustum );
- } else {
- object.render( function( object ) { _this.renderBufferImmediate( object, program, material ); } );
- }
- };
- function unrollImmediateBufferMaterial ( globject ) {
- var object = globject.object,
- material = object.material;
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- };
- function unrollBufferMaterial ( globject ) {
- var object = globject.object,
- buffer = globject.buffer,
- material, materialIndex, meshMaterial;
- meshMaterial = object.material;
- if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
- materialIndex = buffer.materialIndex;
- material = meshMaterial.materials[ materialIndex ];
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- } else {
- material = meshMaterial;
- if ( material ) {
- if ( material.transparent ) {
- globject.transparent = material;
- globject.opaque = null;
- } else {
- globject.opaque = material;
- globject.transparent = null;
- }
- }
- }
- };
- // Geometry splitting
- function sortFacesByMaterial ( geometry, material ) {
- var f, fl, face, materialIndex, vertices,
- groupHash, hash_map = {};
- var numMorphTargets = geometry.morphTargets.length;
- var numMorphNormals = geometry.morphNormals.length;
- var usesFaceMaterial = material instanceof THREE.MeshFaceMaterial;
- geometry.geometryGroups = {};
- for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
- face = geometry.faces[ f ];
- materialIndex = usesFaceMaterial ? face.materialIndex : 0;
- if ( hash_map[ materialIndex ] === undefined ) {
- hash_map[ materialIndex ] = { 'hash': materialIndex, 'counter': 0 };
- }
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( geometry.geometryGroups[ groupHash ] === undefined ) {
- geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- }
- vertices = face instanceof THREE.Face3 ? 3 : 4;
- if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
- hash_map[ materialIndex ].counter += 1;
- groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
- if ( geometry.geometryGroups[ groupHash ] === undefined ) {
- geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
- }
- }
- if ( face instanceof THREE.Face3 ) {
- geometry.geometryGroups[ groupHash ].faces3.push( f );
- } else {
- geometry.geometryGroups[ groupHash ].faces4.push( f );
- }
- geometry.geometryGroups[ groupHash ].vertices += vertices;
- }
- geometry.geometryGroupsList = [];
- for ( var g in geometry.geometryGroups ) {
- geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
- geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
- }
- };
- // Objects refresh
- this.initWebGLObjects = function ( scene ) {
- if ( !scene.__webglObjects ) {
- scene.__webglObjects = [];
- scene.__webglObjectsImmediate = [];
- scene.__webglSprites = [];
- scene.__webglFlares = [];
- }
- while ( scene.__objectsAdded.length ) {
- addObject( scene.__objectsAdded[ 0 ], scene );
- scene.__objectsAdded.splice( 0, 1 );
- }
- while ( scene.__objectsRemoved.length ) {
- removeObject( scene.__objectsRemoved[ 0 ], scene );
- scene.__objectsRemoved.splice( 0, 1 );
- }
- // update must be called after objects adding / removal
- for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
- var object = scene.__webglObjects[ o ].object;
- // TODO: Remove this hack (WebGLRenderer refactoring)
- if ( object.__webglInit === undefined ) {
- if ( object.__webglActive !== undefined ) {
- removeObject( object, scene );
- }
- addObject( object, scene );
- }
- updateObject( object );
- }
- };
- // Objects adding
- function addObject( object, scene ) {
- var g, geometry, material, geometryGroup;
- if ( object.__webglInit === undefined ) {
- object.__webglInit = true;
- object._modelViewMatrix = new THREE.Matrix4();
- object._normalMatrix = new THREE.Matrix3();
- if ( object.geometry !== undefined && object.geometry.__webglInit === undefined ) {
- object.geometry.__webglInit = true;
- object.geometry.addEventListener( 'dispose', onGeometryDispose );
- }
- geometry = object.geometry;
- if ( geometry === undefined ) {
- // fail silently for now
- } else if ( geometry instanceof THREE.BufferGeometry ) {
- initDirectBuffers( geometry );
- } else if ( object instanceof THREE.Mesh ) {
- material = object.material;
- if ( geometry.geometryGroups === undefined ) {
- sortFacesByMaterial( geometry, material );
- }
- // create separate VBOs per geometry chunk
- for ( g in geometry.geometryGroups ) {
- geometryGroup = geometry.geometryGroups[ g ];
- // initialise VBO on the first access
- if ( ! geometryGroup.__webglVertexBuffer ) {
- createMeshBuffers( geometryGroup );
- initMeshBuffers( geometryGroup, object );
- geometry.verticesNeedUpdate = true;
- geometry.morphTargetsNeedUpdate = true;
- geometry.elementsNeedUpdate = true;
- geometry.uvsNeedUpdate = true;
- geometry.normalsNeedUpdate = true;
- geometry.tangentsNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- }
- }
- } else if ( object instanceof THREE.Ribbon ) {
- if ( ! geometry.__webglVertexBuffer ) {
- createRibbonBuffers( geometry );
- initRibbonBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- geometry.normalsNeedUpdate = true;
- }
- } else if ( object instanceof THREE.Line ) {
- if ( ! geometry.__webglVertexBuffer ) {
- createLineBuffers( geometry );
- initLineBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- geometry.lineDistancesNeedUpdate = true;
- }
- } else if ( object instanceof THREE.ParticleSystem ) {
- if ( ! geometry.__webglVertexBuffer ) {
- createParticleBuffers( geometry );
- initParticleBuffers( geometry, object );
- geometry.verticesNeedUpdate = true;
- geometry.colorsNeedUpdate = true;
- }
- }
- }
- if ( object.__webglActive === undefined ) {
- if ( object instanceof THREE.Mesh ) {
- geometry = object.geometry;
- if ( geometry instanceof THREE.BufferGeometry ) {
- addBuffer( scene.__webglObjects, geometry, object );
- } else if ( geometry instanceof THREE.Geometry ) {
- for ( g in geometry.geometryGroups ) {
- geometryGroup = geometry.geometryGroups[ g ];
- addBuffer( scene.__webglObjects, geometryGroup, object );
- }
- }
- } else if ( object instanceof THREE.Ribbon ||
- object instanceof THREE.Line ||
- object instanceof THREE.ParticleSystem ) {
- geometry = object.geometry;
- addBuffer( scene.__webglObjects, geometry, object );
- } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
- addBufferImmediate( scene.__webglObjectsImmediate, object );
- } else if ( object instanceof THREE.Sprite ) {
- scene.__webglSprites.push( object );
- } else if ( object instanceof THREE.LensFlare ) {
- scene.__webglFlares.push( object );
- }
- object.__webglActive = true;
- }
- };
- function addBuffer( objlist, buffer, object ) {
- objlist.push(
- {
- id: null,
- buffer: buffer,
- object: object,
- opaque: null,
- transparent: null,
- z: 0
- }
- );
- };
- function addBufferImmediate( objlist, object ) {
- objlist.push(
- {
- id: null,
- object: object,
- opaque: null,
- transparent: null,
- z: 0
- }
- );
- };
- // Objects updates
- function updateObject( object ) {
- var geometry = object.geometry,
- geometryGroup, customAttributesDirty, material;
- if ( geometry instanceof THREE.BufferGeometry ) {
- setDirectBuffers( geometry, _gl.DYNAMIC_DRAW, !geometry.dynamic );
- } else if ( object instanceof THREE.Mesh ) {
- // check all geometry groups
- for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
- geometryGroup = geometry.geometryGroupsList[ i ];
- material = getBufferMaterial( object, geometryGroup );
- if ( geometry.buffersNeedUpdate ) {
- initMeshBuffers( geometryGroup, object );
- }
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
- geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
- geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
- setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material );
- }
- }
- geometry.verticesNeedUpdate = false;
- geometry.morphTargetsNeedUpdate = false;
- geometry.elementsNeedUpdate = false;
- geometry.uvsNeedUpdate = false;
- geometry.normalsNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.tangentsNeedUpdate = false;
- geometry.buffersNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- } else if ( object instanceof THREE.Ribbon ) {
- material = getBufferMaterial( object, geometry );
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.normalsNeedUpdate || customAttributesDirty ) {
- setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.normalsNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- } else if ( object instanceof THREE.Line ) {
- material = getBufferMaterial( object, geometry );
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) {
- setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- geometry.lineDistancesNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- } else if ( object instanceof THREE.ParticleSystem ) {
- material = getBufferMaterial( object, geometry );
- customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
- if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || object.sortParticles || customAttributesDirty ) {
- setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
- }
- geometry.verticesNeedUpdate = false;
- geometry.colorsNeedUpdate = false;
- material.attributes && clearCustomAttributes( material );
- }
- };
- // Objects updates - custom attributes check
- function areCustomAttributesDirty( material ) {
- for ( var a in material.attributes ) {
- if ( material.attributes[ a ].needsUpdate ) return true;
- }
- return false;
- };
- function clearCustomAttributes( material ) {
- for ( var a in material.attributes ) {
- material.attributes[ a ].needsUpdate = false;
- }
- };
- // Objects removal
- function removeObject( object, scene ) {
- if ( object instanceof THREE.Mesh ||
- object instanceof THREE.ParticleSystem ||
- object instanceof THREE.Ribbon ||
- object instanceof THREE.Line ) {
- removeInstances( scene.__webglObjects, object );
- } else if ( object instanceof THREE.Sprite ) {
- removeInstancesDirect( scene.__webglSprites, object );
- } else if ( object instanceof THREE.LensFlare ) {
- removeInstancesDirect( scene.__webglFlares, object );
- } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
- removeInstances( scene.__webglObjectsImmediate, object );
- }
- delete object.__webglActive;
- };
- function removeInstances( objlist, object ) {
- for ( var o = objlist.length - 1; o >= 0; o -- ) {
- if ( objlist[ o ].object === object ) {
- objlist.splice( o, 1 );
- }
- }
- };
- function removeInstancesDirect( objlist, object ) {
- for ( var o = objlist.length - 1; o >= 0; o -- ) {
- if ( objlist[ o ] === object ) {
- objlist.splice( o, 1 );
- }
- }
- };
- // Materials
- this.initMaterial = function ( material, lights, fog, object ) {
- material.addEventListener( 'dispose', onMaterialDispose );
- var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
- if ( material instanceof THREE.MeshDepthMaterial ) {
- shaderID = 'depth';
- } else if ( material instanceof THREE.MeshNormalMaterial ) {
- shaderID = 'normal';
- } else if ( material instanceof THREE.MeshBasicMaterial ) {
- shaderID = 'basic';
- } else if ( material instanceof THREE.MeshLambertMaterial ) {
- shaderID = 'lambert';
- } else if ( material instanceof THREE.MeshPhongMaterial ) {
- shaderID = 'phong';
- } else if ( material instanceof THREE.LineBasicMaterial ) {
- shaderID = 'basic';
- } else if ( material instanceof THREE.LineDashedMaterial ) {
- shaderID = 'dashed';
- } else if ( material instanceof THREE.ParticleBasicMaterial ) {
- shaderID = 'particle_basic';
- }
- if ( shaderID ) {
- setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
- }
- // heuristics to create shader parameters according to lights in the scene
- // (not to blow over maxLights budget)
- maxLightCount = allocateLights( lights );
- maxShadows = allocateShadows( lights );
- maxBones = allocateBones( object );
- parameters = {
- map: !!material.map,
- envMap: !!material.envMap,
- lightMap: !!material.lightMap,
- bumpMap: !!material.bumpMap,
- normalMap: !!material.normalMap,
- specularMap: !!material.specularMap,
- vertexColors: material.vertexColors,
- fog: fog,
- useFog: material.fog,
- fogExp: fog instanceof THREE.FogExp2,
- sizeAttenuation: material.sizeAttenuation,
- skinning: material.skinning,
- maxBones: maxBones,
- useVertexTexture: _supportsBoneTextures && object && object.useVertexTexture,
- boneTextureWidth: object && object.boneTextureWidth,
- boneTextureHeight: object && object.boneTextureHeight,
- morphTargets: material.morphTargets,
- morphNormals: material.morphNormals,
- maxMorphTargets: this.maxMorphTargets,
- maxMorphNormals: this.maxMorphNormals,
- maxDirLights: maxLightCount.directional,
- maxPointLights: maxLightCount.point,
- maxSpotLights: maxLightCount.spot,
- maxHemiLights: maxLightCount.hemi,
- maxShadows: maxShadows,
- shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow,
- shadowMapType: this.shadowMapType,
- shadowMapDebug: this.shadowMapDebug,
- shadowMapCascade: this.shadowMapCascade,
- alphaTest: material.alphaTest,
- metal: material.metal,
- perPixel: material.perPixel,
- wrapAround: material.wrapAround,
- doubleSided: material.side === THREE.DoubleSide,
- flipSided: material.side === THREE.BackSide
- };
- material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, material.defines, parameters );
- var attributes = material.program.attributes;
- if ( material.morphTargets ) {
- material.numSupportedMorphTargets = 0;
- var id, base = "morphTarget";
- for ( i = 0; i < this.maxMorphTargets; i ++ ) {
- id = base + i;
- if ( attributes[ id ] >= 0 ) {
- material.numSupportedMorphTargets ++;
- }
- }
- }
- if ( material.morphNormals ) {
- material.numSupportedMorphNormals = 0;
- var id, base = "morphNormal";
- for ( i = 0; i < this.maxMorphNormals; i ++ ) {
- id = base + i;
- if ( attributes[ id ] >= 0 ) {
- material.numSupportedMorphNormals ++;
- }
- }
- }
- material.uniformsList = [];
- for ( u in material.uniforms ) {
- material.uniformsList.push( [ material.uniforms[ u ], u ] );
- }
- };
- function setMaterialShaders( material, shaders ) {
- material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
- material.vertexShader = shaders.vertexShader;
- material.fragmentShader = shaders.fragmentShader;
- };
- function setProgram( camera, lights, fog, material, object ) {
- _usedTextureUnits = 0;
- if ( material.needsUpdate ) {
- if ( material.program ) deallocateMaterial( material );
- _this.initMaterial( material, lights, fog, object );
- material.needsUpdate = false;
- }
- if ( material.morphTargets ) {
- if ( ! object.__webglMorphTargetInfluences ) {
- object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
- }
- }
- var refreshMaterial = false;
- var program = material.program,
- p_uniforms = program.uniforms,
- m_uniforms = material.uniforms;
- if ( program !== _currentProgram ) {
- _gl.useProgram( program );
- _currentProgram = program;
- refreshMaterial = true;
- }
- if ( material.id !== _currentMaterialId ) {
- _currentMaterialId = material.id;
- refreshMaterial = true;
- }
- if ( refreshMaterial || camera !== _currentCamera ) {
- _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
- if ( camera !== _currentCamera ) _currentCamera = camera;
- }
- // skinning uniforms must be set even if material didn't change
- // auto-setting of texture unit for bone texture must go before other textures
- // not sure why, but otherwise weird things happen
- if ( material.skinning ) {
- if ( _supportsBoneTextures && object.useVertexTexture ) {
- if ( p_uniforms.boneTexture !== null ) {
- var textureUnit = getTextureUnit();
- _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
- _this.setTexture( object.boneTexture, textureUnit );
- }
- } else {
- if ( p_uniforms.boneGlobalMatrices !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
- }
- }
- }
- if ( refreshMaterial ) {
- // refresh uniforms common to several materials
- if ( fog && material.fog ) {
- refreshUniformsFog( m_uniforms, fog );
- }
- if ( material instanceof THREE.MeshPhongMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material.lights ) {
- if ( _lightsNeedUpdate ) {
- setupLights( program, lights );
- _lightsNeedUpdate = false;
- }
- refreshUniformsLights( m_uniforms, _lights );
- }
- if ( material instanceof THREE.MeshBasicMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material instanceof THREE.MeshPhongMaterial ) {
- refreshUniformsCommon( m_uniforms, material );
- }
- // refresh single material specific uniforms
- if ( material instanceof THREE.LineBasicMaterial ) {
- refreshUniformsLine( m_uniforms, material );
- } else if ( material instanceof THREE.LineDashedMaterial ) {
- refreshUniformsLine( m_uniforms, material );
- refreshUniformsDash( m_uniforms, material );
- } else if ( material instanceof THREE.ParticleBasicMaterial ) {
- refreshUniformsParticle( m_uniforms, material );
- } else if ( material instanceof THREE.MeshPhongMaterial ) {
- refreshUniformsPhong( m_uniforms, material );
- } else if ( material instanceof THREE.MeshLambertMaterial ) {
- refreshUniformsLambert( m_uniforms, material );
- } else if ( material instanceof THREE.MeshDepthMaterial ) {
- m_uniforms.mNear.value = camera.near;
- m_uniforms.mFar.value = camera.far;
- m_uniforms.opacity.value = material.opacity;
- } else if ( material instanceof THREE.MeshNormalMaterial ) {
- m_uniforms.opacity.value = material.opacity;
- }
- if ( object.receiveShadow && ! material._shadowPass ) {
- refreshUniformsShadow( m_uniforms, lights );
- }
- // load common uniforms
- loadUniformsGeneric( program, material.uniformsList );
- // load material specific uniforms
- // (shader material also gets them for the sake of genericity)
- if ( material instanceof THREE.ShaderMaterial ||
- material instanceof THREE.MeshPhongMaterial ||
- material.envMap ) {
- if ( p_uniforms.cameraPosition !== null ) {
- _vector3.getPositionFromMatrix( camera.matrixWorld );
- _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
- }
- }
- if ( material instanceof THREE.MeshPhongMaterial ||
- material instanceof THREE.MeshLambertMaterial ||
- material instanceof THREE.ShaderMaterial ||
- material.skinning ) {
- if ( p_uniforms.viewMatrix !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
- }
- }
- }
- loadUniformsMatrices( p_uniforms, object );
- if ( p_uniforms.modelMatrix !== null ) {
- _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
- }
- return program;
- };
- // Uniforms (refresh uniforms objects)
- function refreshUniformsCommon ( uniforms, material ) {
- uniforms.opacity.value = material.opacity;
- if ( _this.gammaInput ) {
- uniforms.diffuse.value.copyGammaToLinear( material.color );
- } else {
- uniforms.diffuse.value = material.color;
- }
- uniforms.map.value = material.map;
- uniforms.lightMap.value = material.lightMap;
- uniforms.specularMap.value = material.specularMap;
- if ( material.bumpMap ) {
- uniforms.bumpMap.value = material.bumpMap;
- uniforms.bumpScale.value = material.bumpScale;
- }
- if ( material.normalMap ) {
- uniforms.normalMap.value = material.normalMap;
- uniforms.normalScale.value.copy( material.normalScale );
- }
- // uv repeat and offset setting priorities
- // 1. color map
- // 2. specular map
- // 3. normal map
- // 4. bump map
- var uvScaleMap;
- if ( material.map ) {
- uvScaleMap = material.map;
- } else if ( material.specularMap ) {
- uvScaleMap = material.specularMap;
- } else if ( material.normalMap ) {
- uvScaleMap = material.normalMap;
- } else if ( material.bumpMap ) {
- uvScaleMap = material.bumpMap;
- }
- if ( uvScaleMap !== undefined ) {
- var offset = uvScaleMap.offset;
- var repeat = uvScaleMap.repeat;
- uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
- }
- uniforms.envMap.value = material.envMap;
- uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
- if ( _this.gammaInput ) {
- //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
- uniforms.reflectivity.value = material.reflectivity;
- } else {
- uniforms.reflectivity.value = material.reflectivity;
- }
- uniforms.refractionRatio.value = material.refractionRatio;
- uniforms.combine.value = material.combine;
- uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
- };
- function refreshUniformsLine ( uniforms, material ) {
- uniforms.diffuse.value = material.color;
- uniforms.opacity.value = material.opacity;
- };
- function refreshUniformsDash ( uniforms, material ) {
- uniforms.dashSize.value = material.dashSize;
- uniforms.totalSize.value = material.dashSize + material.gapSize;
- uniforms.scale.value = material.scale;
- };
- function refreshUniformsParticle ( uniforms, material ) {
- uniforms.psColor.value = material.color;
- uniforms.opacity.value = material.opacity;
- uniforms.size.value = material.size;
- uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
- uniforms.map.value = material.map;
- };
- function refreshUniformsFog ( uniforms, fog ) {
- uniforms.fogColor.value = fog.color;
- if ( fog instanceof THREE.Fog ) {
- uniforms.fogNear.value = fog.near;
- uniforms.fogFar.value = fog.far;
- } else if ( fog instanceof THREE.FogExp2 ) {
- uniforms.fogDensity.value = fog.density;
- }
- };
- function refreshUniformsPhong ( uniforms, material ) {
- uniforms.shininess.value = material.shininess;
- if ( _this.gammaInput ) {
- uniforms.ambient.value.copyGammaToLinear( material.ambient );
- uniforms.emissive.value.copyGammaToLinear( material.emissive );
- uniforms.specular.value.copyGammaToLinear( material.specular );
- } else {
- uniforms.ambient.value = material.ambient;
- uniforms.emissive.value = material.emissive;
- uniforms.specular.value = material.specular;
- }
- if ( material.wrapAround ) {
- uniforms.wrapRGB.value.copy( material.wrapRGB );
- }
- };
- function refreshUniformsLambert ( uniforms, material ) {
- if ( _this.gammaInput ) {
- uniforms.ambient.value.copyGammaToLinear( material.ambient );
- uniforms.emissive.value.copyGammaToLinear( material.emissive );
- } else {
- uniforms.ambient.value = material.ambient;
- uniforms.emissive.value = material.emissive;
- }
- if ( material.wrapAround ) {
- uniforms.wrapRGB.value.copy( material.wrapRGB );
- }
- };
- function refreshUniformsLights ( uniforms, lights ) {
- uniforms.ambientLightColor.value = lights.ambient;
- uniforms.directionalLightColor.value = lights.directional.colors;
- uniforms.directionalLightDirection.value = lights.directional.positions;
- uniforms.pointLightColor.value = lights.point.colors;
- uniforms.pointLightPosition.value = lights.point.positions;
- uniforms.pointLightDistance.value = lights.point.distances;
- uniforms.spotLightColor.value = lights.spot.colors;
- uniforms.spotLightPosition.value = lights.spot.positions;
- uniforms.spotLightDistance.value = lights.spot.distances;
- uniforms.spotLightDirection.value = lights.spot.directions;
- uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
- uniforms.spotLightExponent.value = lights.spot.exponents;
- uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
- uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
- uniforms.hemisphereLightDirection.value = lights.hemi.positions;
- };
- function refreshUniformsShadow ( uniforms, lights ) {
- if ( uniforms.shadowMatrix ) {
- var j = 0;
- for ( var i = 0, il = lights.length; i < il; i ++ ) {
- var light = lights[ i ];
- if ( ! light.castShadow ) continue;
- if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
- uniforms.shadowMap.value[ j ] = light.shadowMap;
- uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
- uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
- uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
- uniforms.shadowBias.value[ j ] = light.shadowBias;
- j ++;
- }
- }
- }
- };
- // Uniforms (load to GPU)
- function loadUniformsMatrices ( uniforms, object ) {
- _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
- if ( uniforms.normalMatrix ) {
- _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
- }
- };
- function getTextureUnit() {
- var textureUnit = _usedTextureUnits;
- if ( textureUnit >= _maxTextures ) {
- console.warn( "WebGLRenderer: trying to use " + textureUnit + " texture units while this GPU supports only " + _maxTextures );
- }
- _usedTextureUnits += 1;
- return textureUnit;
- };
- function loadUniformsGeneric ( program, uniforms ) {
- var uniform, value, type, location, texture, textureUnit, i, il, j, jl, offset;
- for ( j = 0, jl = uniforms.length; j < jl; j ++ ) {
- location = program.uniforms[ uniforms[ j ][ 1 ] ];
- if ( !location ) continue;
- uniform = uniforms[ j ][ 0 ];
- type = uniform.type;
- value = uniform.value;
- if ( type === "i" ) { // single integer
- _gl.uniform1i( location, value );
- } else if ( type === "f" ) { // single float
- _gl.uniform1f( location, value );
- } else if ( type === "v2" ) { // single THREE.Vector2
- _gl.uniform2f( location, value.x, value.y );
- } else if ( type === "v3" ) { // single THREE.Vector3
- _gl.uniform3f( location, value.x, value.y, value.z );
- } else if ( type === "v4" ) { // single THREE.Vector4
- _gl.uniform4f( location, value.x, value.y, value.z, value.w );
- } else if ( type === "c" ) { // single THREE.Color
- _gl.uniform3f( location, value.r, value.g, value.b );
- } else if ( type === "iv1" ) { // flat array of integers (JS or typed array)
- _gl.uniform1iv( location, value );
- } else if ( type === "iv" ) { // flat array of integers with 3 x N size (JS or typed array)
- _gl.uniform3iv( location, value );
- } else if ( type === "fv1" ) { // flat array of floats (JS or typed array)
- _gl.uniform1fv( location, value );
- } else if ( type === "fv" ) { // flat array of floats with 3 x N size (JS or typed array)
- _gl.uniform3fv( location, value );
- } else if ( type === "v2v" ) { // array of THREE.Vector2
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 2 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 2;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- }
- _gl.uniform2fv( location, uniform._array );
- } else if ( type === "v3v" ) { // array of THREE.Vector3
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 3 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 3;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- }
- _gl.uniform3fv( location, uniform._array );
- } else if ( type === "v4v" ) { // array of THREE.Vector4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 4 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- offset = i * 4;
- uniform._array[ offset ] = value[ i ].x;
- uniform._array[ offset + 1 ] = value[ i ].y;
- uniform._array[ offset + 2 ] = value[ i ].z;
- uniform._array[ offset + 3 ] = value[ i ].w;
- }
- _gl.uniform4fv( location, uniform._array );
- } else if ( type === "m4") { // single THREE.Matrix4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 16 );
- }
- value.flattenToArray( uniform._array );
- _gl.uniformMatrix4fv( location, false, uniform._array );
- } else if ( type === "m4v" ) { // array of THREE.Matrix4
- if ( uniform._array === undefined ) {
- uniform._array = new Float32Array( 16 * value.length );
- }
- for ( i = 0, il = value.length; i < il; i ++ ) {
- value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
- }
- _gl.uniformMatrix4fv( location, false, uniform._array );
- } else if ( type === "t" ) { // single THREE.Texture (2d or cube)
- texture = value;
- textureUnit = getTextureUnit();
- _gl.uniform1i( location, textureUnit );
- if ( !texture ) continue;
- if ( texture.image instanceof Array && texture.image.length === 6 ) {
- setCubeTexture( texture, textureUnit );
- } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
- setCubeTextureDynamic( texture, textureUnit );
- } else {
- _this.setTexture( texture, textureUnit );
- }
- } else if ( type === "tv" ) { // array of THREE.Texture (2d)
- if ( uniform._array === undefined ) {
- uniform._array = [];
- }
- for( i = 0, il = uniform.value.length; i < il; i ++ ) {
- uniform._array[ i ] = getTextureUnit();
- }
- _gl.uniform1iv( location, uniform._array );
- for( i = 0, il = uniform.value.length; i < il; i ++ ) {
- texture = uniform.value[ i ];
- textureUnit = uniform._array[ i ];
- if ( !texture ) continue;
- _this.setTexture( texture, textureUnit );
- }
- } else {
- console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
- }
- }
- };
- function setupMatrices ( object, camera ) {
- object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
- object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
- };
- //
- function setColorGamma( array, offset, color, intensitySq ) {
- array[ offset ] = color.r * color.r * intensitySq;
- array[ offset + 1 ] = color.g * color.g * intensitySq;
- array[ offset + 2 ] = color.b * color.b * intensitySq;
- };
- function setColorLinear( array, offset, color, intensity ) {
- array[ offset ] = color.r * intensity;
- array[ offset + 1 ] = color.g * intensity;
- array[ offset + 2 ] = color.b * intensity;
- };
- function setupLights ( program, lights ) {
- var l, ll, light, n,
- r = 0, g = 0, b = 0,
- color, skyColor, groundColor,
- intensity, intensitySq,
- position,
- distance,
- zlights = _lights,
- dirColors = zlights.directional.colors,
- dirPositions = zlights.directional.positions,
- pointColors = zlights.point.colors,
- pointPositions = zlights.point.positions,
- pointDistances = zlights.point.distances,
- spotColors = zlights.spot.colors,
- spotPositions = zlights.spot.positions,
- spotDistances = zlights.spot.distances,
- spotDirections = zlights.spot.directions,
- spotAnglesCos = zlights.spot.anglesCos,
- spotExponents = zlights.spot.exponents,
- hemiSkyColors = zlights.hemi.skyColors,
- hemiGroundColors = zlights.hemi.groundColors,
- hemiPositions = zlights.hemi.positions,
- dirLength = 0,
- pointLength = 0,
- spotLength = 0,
- hemiLength = 0,
- dirCount = 0,
- pointCount = 0,
- spotCount = 0,
- hemiCount = 0,
- dirOffset = 0,
- pointOffset = 0,
- spotOffset = 0,
- hemiOffset = 0;
- for ( l = 0, ll = lights.length; l < ll; l ++ ) {
- light = lights[ l ];
- if ( light.onlyShadow ) continue;
- color = light.color;
- intensity = light.intensity;
- distance = light.distance;
- if ( light instanceof THREE.AmbientLight ) {
- if ( ! light.visible ) continue;
- if ( _this.gammaInput ) {
- r += color.r * color.r;
- g += color.g * color.g;
- b += color.b * color.b;
- } else {
- r += color.r;
- g += color.g;
- b += color.b;
- }
- } else if ( light instanceof THREE.DirectionalLight ) {
- dirCount += 1;
- if ( ! light.visible ) continue;
- _direction.getPositionFromMatrix( light.matrixWorld );
- _vector3.getPositionFromMatrix( light.target.matrixWorld );
- _direction.sub( _vector3 );
- _direction.normalize();
- // skip lights with undefined direction
- // these create troubles in OpenGL (making pixel black)
- if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
- dirOffset = dirLength * 3;
- dirPositions[ dirOffset ] = _direction.x;
- dirPositions[ dirOffset + 1 ] = _direction.y;
- dirPositions[ dirOffset + 2 ] = _direction.z;
- if ( _this.gammaInput ) {
- setColorGamma( dirColors, dirOffset, color, intensity * intensity );
- } else {
- setColorLinear( dirColors, dirOffset, color, intensity );
- }
- dirLength += 1;
- } else if ( light instanceof THREE.PointLight ) {
- pointCount += 1;
- if ( ! light.visible ) continue;
- pointOffset = pointLength * 3;
- if ( _this.gammaInput ) {
- setColorGamma( pointColors, pointOffset, color, intensity * intensity );
- } else {
- setColorLinear( pointColors, pointOffset, color, intensity );
- }
- _vector3.getPositionFromMatrix( light.matrixWorld );
- pointPositions[ pointOffset ] = _vector3.x;
- pointPositions[ pointOffset + 1 ] = _vector3.y;
- pointPositions[ pointOffset + 2 ] = _vector3.z;
- pointDistances[ pointLength ] = distance;
- pointLength += 1;
- } else if ( light instanceof THREE.SpotLight ) {
- spotCount += 1;
- if ( ! light.visible ) continue;
- spotOffset = spotLength * 3;
- if ( _this.gammaInput ) {
- setColorGamma( spotColors, spotOffset, color, intensity * intensity );
- } else {
- setColorLinear( spotColors, spotOffset, color, intensity );
- }
- _vector3.getPositionFromMatrix( light.matrixWorld );
- spotPositions[ spotOffset ] = _vector3.x;
- spotPositions[ spotOffset + 1 ] = _vector3.y;
- spotPositions[ spotOffset + 2 ] = _vector3.z;
- spotDistances[ spotLength ] = distance;
- _direction.copy( _vector3 );
- _vector3.getPositionFromMatrix( light.target.matrixWorld );
- _direction.sub( _vector3 );
- _direction.normalize();
- spotDirections[ spotOffset ] = _direction.x;
- spotDirections[ spotOffset + 1 ] = _direction.y;
- spotDirections[ spotOffset + 2 ] = _direction.z;
- spotAnglesCos[ spotLength ] = Math.cos( light.angle );
- spotExponents[ spotLength ] = light.exponent;
- spotLength += 1;
- } else if ( light instanceof THREE.HemisphereLight ) {
- hemiCount += 1;
- if ( ! light.visible ) continue;
- _direction.getPositionFromMatrix( light.matrixWorld );
- _direction.normalize();
- // skip lights with undefined direction
- // these create troubles in OpenGL (making pixel black)
- if ( _direction.x === 0 && _direction.y === 0 && _direction.z === 0 ) continue;
- hemiOffset = hemiLength * 3;
- hemiPositions[ hemiOffset ] = _direction.x;
- hemiPositions[ hemiOffset + 1 ] = _direction.y;
- hemiPositions[ hemiOffset + 2 ] = _direction.z;
- skyColor = light.color;
- groundColor = light.groundColor;
- if ( _this.gammaInput ) {
- intensitySq = intensity * intensity;
- setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
- setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
- } else {
- setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
- setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
- }
- hemiLength += 1;
- }
- }
- // null eventual remains from removed lights
- // (this is to avoid if in shader)
- for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
- for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
- for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
- for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
- for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
- zlights.directional.length = dirLength;
- zlights.point.length = pointLength;
- zlights.spot.length = spotLength;
- zlights.hemi.length = hemiLength;
- zlights.ambient[ 0 ] = r;
- zlights.ambient[ 1 ] = g;
- zlights.ambient[ 2 ] = b;
- };
- // GL state setting
- this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
- if ( cullFace === THREE.CullFaceNone ) {
- _gl.disable( _gl.CULL_FACE );
- } else {
- if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
- _gl.frontFace( _gl.CW );
- } else {
- _gl.frontFace( _gl.CCW );
- }
- if ( cullFace === THREE.CullFaceBack ) {
- _gl.cullFace( _gl.BACK );
- } else if ( cullFace === THREE.CullFaceFront ) {
- _gl.cullFace( _gl.FRONT );
- } else {
- _gl.cullFace( _gl.FRONT_AND_BACK );
- }
- _gl.enable( _gl.CULL_FACE );
- }
- };
- this.setMaterialFaces = function ( material ) {
- var doubleSided = material.side === THREE.DoubleSide;
- var flipSided = material.side === THREE.BackSide;
- if ( _oldDoubleSided !== doubleSided ) {
- if ( doubleSided ) {
- _gl.disable( _gl.CULL_FACE );
- } else {
- _gl.enable( _gl.CULL_FACE );
- }
- _oldDoubleSided = doubleSided;
- }
- if ( _oldFlipSided !== flipSided ) {
- if ( flipSided ) {
- _gl.frontFace( _gl.CW );
- } else {
- _gl.frontFace( _gl.CCW );
- }
- _oldFlipSided = flipSided;
- }
- };
- this.setDepthTest = function ( depthTest ) {
- if ( _oldDepthTest !== depthTest ) {
- if ( depthTest ) {
- _gl.enable( _gl.DEPTH_TEST );
- } else {
- _gl.disable( _gl.DEPTH_TEST );
- }
- _oldDepthTest = depthTest;
- }
- };
- this.setDepthWrite = function ( depthWrite ) {
- if ( _oldDepthWrite !== depthWrite ) {
- _gl.depthMask( depthWrite );
- _oldDepthWrite = depthWrite;
- }
- };
- function setLineWidth ( width ) {
- if ( width !== _oldLineWidth ) {
- _gl.lineWidth( width );
- _oldLineWidth = width;
- }
- };
- function setPolygonOffset ( polygonoffset, factor, units ) {
- if ( _oldPolygonOffset !== polygonoffset ) {
- if ( polygonoffset ) {
- _gl.enable( _gl.POLYGON_OFFSET_FILL );
- } else {
- _gl.disable( _gl.POLYGON_OFFSET_FILL );
- }
- _oldPolygonOffset = polygonoffset;
- }
- if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
- _gl.polygonOffset( factor, units );
- _oldPolygonOffsetFactor = factor;
- _oldPolygonOffsetUnits = units;
- }
- };
- this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
- if ( blending !== _oldBlending ) {
- if ( blending === THREE.NoBlending ) {
- _gl.disable( _gl.BLEND );
- } else if ( blending === THREE.AdditiveBlending ) {
- _gl.enable( _gl.BLEND );
- _gl.blendEquation( _gl.FUNC_ADD );
- _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
- } else if ( blending === THREE.SubtractiveBlending ) {
- // TODO: Find blendFuncSeparate() combination
- _gl.enable( _gl.BLEND );
- _gl.blendEquation( _gl.FUNC_ADD );
- _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
- } else if ( blending === THREE.MultiplyBlending ) {
- // TODO: Find blendFuncSeparate() combination
- _gl.enable( _gl.BLEND );
- _gl.blendEquation( _gl.FUNC_ADD );
- _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
- } else if ( blending === THREE.CustomBlending ) {
- _gl.enable( _gl.BLEND );
- } else {
- _gl.enable( _gl.BLEND );
- _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
- _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
- }
- _oldBlending = blending;
- }
- if ( blending === THREE.CustomBlending ) {
- if ( blendEquation !== _oldBlendEquation ) {
- _gl.blendEquation( paramThreeToGL( blendEquation ) );
- _oldBlendEquation = blendEquation;
- }
- if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
- _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
- _oldBlendSrc = blendSrc;
- _oldBlendDst = blendDst;
- }
- } else {
- _oldBlendEquation = null;
- _oldBlendSrc = null;
- _oldBlendDst = null;
- }
- };
- // Defines
- function generateDefines ( defines ) {
- var value, chunk, chunks = [];
- for ( var d in defines ) {
- value = defines[ d ];
- if ( value === false ) continue;
- chunk = "#define " + d + " " + value;
- chunks.push( chunk );
- }
- return chunks.join( "\n" );
- };
- // Shaders
- function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, defines, parameters ) {
- var p, pl, d, program, code;
- var chunks = [];
- // Generate code
- if ( shaderID ) {
- chunks.push( shaderID );
- } else {
- chunks.push( fragmentShader );
- chunks.push( vertexShader );
- }
- for ( d in defines ) {
- chunks.push( d );
- chunks.push( defines[ d ] );
- }
- for ( p in parameters ) {
- chunks.push( p );
- chunks.push( parameters[ p ] );
- }
- code = chunks.join();
- // Check if code has been already compiled
- for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
- var programInfo = _programs[ p ];
- if ( programInfo.code === code ) {
- // console.log( "Code already compiled." /*: \n\n" + code*/ );
- programInfo.usedTimes ++;
- return programInfo.program;
- }
- }
- var shadowMapTypeDefine = "SHADOWMAP_TYPE_BASIC";
- if ( parameters.shadowMapType === THREE.PCFShadowMap ) {
- shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF";
- } else if ( parameters.shadowMapType === THREE.PCFSoftShadowMap ) {
- shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF_SOFT";
- }
- // console.log( "building new program " );
- //
- var customDefines = generateDefines( defines );
- //
- program = _gl.createProgram();
- var prefix_vertex = [
- "precision " + _precision + " float;",
- customDefines,
- _supportsVertexTextures ? "#define VERTEX_TEXTURES" : "",
- _this.gammaInput ? "#define GAMMA_INPUT" : "",
- _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
- _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
- "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
- "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
- "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
- "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
- "#define MAX_SHADOWS " + parameters.maxShadows,
- "#define MAX_BONES " + parameters.maxBones,
- parameters.map ? "#define USE_MAP" : "",
- parameters.envMap ? "#define USE_ENVMAP" : "",
- parameters.lightMap ? "#define USE_LIGHTMAP" : "",
- parameters.bumpMap ? "#define USE_BUMPMAP" : "",
- parameters.normalMap ? "#define USE_NORMALMAP" : "",
- parameters.specularMap ? "#define USE_SPECULARMAP" : "",
- parameters.vertexColors ? "#define USE_COLOR" : "",
- parameters.skinning ? "#define USE_SKINNING" : "",
- parameters.useVertexTexture ? "#define BONE_TEXTURE" : "",
- parameters.boneTextureWidth ? "#define N_BONE_PIXEL_X " + parameters.boneTextureWidth.toFixed( 1 ) : "",
- parameters.boneTextureHeight ? "#define N_BONE_PIXEL_Y " + parameters.boneTextureHeight.toFixed( 1 ) : "",
- parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
- parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
- parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
- parameters.wrapAround ? "#define WRAP_AROUND" : "",
- parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
- parameters.flipSided ? "#define FLIP_SIDED" : "",
- parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
- parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
- parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
- parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
- parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
- "uniform mat4 modelMatrix;",
- "uniform mat4 modelViewMatrix;",
- "uniform mat4 projectionMatrix;",
- "uniform mat4 viewMatrix;",
- "uniform mat3 normalMatrix;",
- "uniform vec3 cameraPosition;",
- "attribute vec3 position;",
- "attribute vec3 normal;",
- "attribute vec2 uv;",
- "attribute vec2 uv2;",
- "#ifdef USE_COLOR",
- "attribute vec3 color;",
- "#endif",
- "#ifdef USE_MORPHTARGETS",
- "attribute vec3 morphTarget0;",
- "attribute vec3 morphTarget1;",
- "attribute vec3 morphTarget2;",
- "attribute vec3 morphTarget3;",
- "#ifdef USE_MORPHNORMALS",
- "attribute vec3 morphNormal0;",
- "attribute vec3 morphNormal1;",
- "attribute vec3 morphNormal2;",
- "attribute vec3 morphNormal3;",
- "#else",
- "attribute vec3 morphTarget4;",
- "attribute vec3 morphTarget5;",
- "attribute vec3 morphTarget6;",
- "attribute vec3 morphTarget7;",
- "#endif",
- "#endif",
- "#ifdef USE_SKINNING",
- "attribute vec4 skinIndex;",
- "attribute vec4 skinWeight;",
- "#endif",
- ""
- ].join("\n");
- var prefix_fragment = [
- "precision " + _precision + " float;",
- ( parameters.bumpMap || parameters.normalMap ) ? "#extension GL_OES_standard_derivatives : enable" : "",
- customDefines,
- "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
- "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
- "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
- "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
- "#define MAX_SHADOWS " + parameters.maxShadows,
- parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
- _this.gammaInput ? "#define GAMMA_INPUT" : "",
- _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
- _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
- ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
- ( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "",
- parameters.map ? "#define USE_MAP" : "",
- parameters.envMap ? "#define USE_ENVMAP" : "",
- parameters.lightMap ? "#define USE_LIGHTMAP" : "",
- parameters.bumpMap ? "#define USE_BUMPMAP" : "",
- parameters.normalMap ? "#define USE_NORMALMAP" : "",
- parameters.specularMap ? "#define USE_SPECULARMAP" : "",
- parameters.vertexColors ? "#define USE_COLOR" : "",
- parameters.metal ? "#define METAL" : "",
- parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
- parameters.wrapAround ? "#define WRAP_AROUND" : "",
- parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
- parameters.flipSided ? "#define FLIP_SIDED" : "",
- parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
- parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
- parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
- parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
- "uniform mat4 viewMatrix;",
- "uniform vec3 cameraPosition;",
- ""
- ].join("\n");
- var glVertexShader = getShader( "vertex", prefix_vertex + vertexShader );
- var glFragmentShader = getShader( "fragment", prefix_fragment + fragmentShader );
- _gl.attachShader( program, glVertexShader );
- _gl.attachShader( program, glFragmentShader );
- _gl.linkProgram( program );
- if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
- console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
- }
- // clean up
- _gl.deleteShader( glFragmentShader );
- _gl.deleteShader( glVertexShader );
- // console.log( prefix_fragment + fragmentShader );
- // console.log( prefix_vertex + vertexShader );
- program.uniforms = {};
- program.attributes = {};
- var identifiers, u, a, i;
- // cache uniform locations
- identifiers = [
- 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'modelMatrix', 'cameraPosition',
- 'morphTargetInfluences'
- ];
- if ( parameters.useVertexTexture ) {
- identifiers.push( 'boneTexture' );
- } else {
- identifiers.push( 'boneGlobalMatrices' );
- }
- for ( u in uniforms ) {
- identifiers.push( u );
- }
- cacheUniformLocations( program, identifiers );
- // cache attributes locations
- identifiers = [
- "position", "normal", "uv", "uv2", "tangent", "color",
- "skinIndex", "skinWeight", "lineDistance"
- ];
- for ( i = 0; i < parameters.maxMorphTargets; i ++ ) {
- identifiers.push( "morphTarget" + i );
- }
- for ( i = 0; i < parameters.maxMorphNormals; i ++ ) {
- identifiers.push( "morphNormal" + i );
- }
- for ( a in attributes ) {
- identifiers.push( a );
- }
- cacheAttributeLocations( program, identifiers );
- program.id = _programs_counter ++;
- _programs.push( { program: program, code: code, usedTimes: 1 } );
- _this.info.memory.programs = _programs.length;
- return program;
- };
- // Shader parameters cache
- function cacheUniformLocations ( program, identifiers ) {
- var i, l, id;
- for( i = 0, l = identifiers.length; i < l; i ++ ) {
- id = identifiers[ i ];
- program.uniforms[ id ] = _gl.getUniformLocation( program, id );
- }
- };
- function cacheAttributeLocations ( program, identifiers ) {
- var i, l, id;
- for( i = 0, l = identifiers.length; i < l; i ++ ) {
- id = identifiers[ i ];
- program.attributes[ id ] = _gl.getAttribLocation( program, id );
- }
- };
- function addLineNumbers ( string ) {
- var chunks = string.split( "\n" );
- for ( var i = 0, il = chunks.length; i < il; i ++ ) {
- // Chrome reports shader errors on lines
- // starting counting from 1
- chunks[ i ] = ( i + 1 ) + ": " + chunks[ i ];
- }
- return chunks.join( "\n" );
- };
- function getShader ( type, string ) {
- var shader;
- if ( type === "fragment" ) {
- shader = _gl.createShader( _gl.FRAGMENT_SHADER );
- } else if ( type === "vertex" ) {
- shader = _gl.createShader( _gl.VERTEX_SHADER );
- }
- _gl.shaderSource( shader, string );
- _gl.compileShader( shader );
- if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
- console.error( _gl.getShaderInfoLog( shader ) );
- console.error( addLineNumbers( string ) );
- return null;
- }
- return shader;
- };
- // Textures
- function isPowerOfTwo ( value ) {
- return ( value & ( value - 1 ) ) === 0;
- };
- function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
- if ( isImagePowerOfTwo ) {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
- } else {
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
- _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
- _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
- _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
- }
- if ( _glExtensionTextureFilterAnisotropic && texture.type !== THREE.FloatType ) {
- if ( texture.anisotropy > 1 || texture.__oldAnisotropy ) {
- _gl.texParameterf( textureType, _glExtensionTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _maxAnisotropy ) );
- texture.__oldAnisotropy = texture.anisotropy;
- }
- }
- };
- this.setTexture = function ( texture, slot ) {
- if ( texture.needsUpdate ) {
- if ( ! texture.__webglInit ) {
- texture.__webglInit = true;
- texture.addEventListener( 'dispose', onTextureDispose );
- texture.__webglTexture = _gl.createTexture();
- _this.info.memory.textures ++;
- }
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
- _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
- var image = texture.image,
- isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
- glFormat = paramThreeToGL( texture.format ),
- glType = paramThreeToGL( texture.type );
- setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
- var mipmap, mipmaps = texture.mipmaps;
- if ( texture instanceof THREE.DataTexture ) {
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
- for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
- }
- texture.generateMipmaps = false;
- } else {
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
- }
- } else if ( texture instanceof THREE.CompressedTexture ) {
- // compressed textures can only use manually created mipmaps
- // WebGL can't generate mipmaps for DDS textures
- for( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- }
- } else { // regular Texture (image, video, canvas)
- // use manually created mipmaps if available
- // if there are no manual mipmaps
- // set 0 level mipmap and then use GL to generate other mipmap levels
- if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
- for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
- mipmap = mipmaps[ i ];
- _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
- }
- texture.generateMipmaps = false;
- } else {
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
- }
- }
- if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
- texture.needsUpdate = false;
- if ( texture.onUpdate ) texture.onUpdate();
- } else {
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
- }
- };
- function clampToMaxSize ( image, maxSize ) {
- if ( image.width <= maxSize && image.height <= maxSize ) {
- return image;
- }
- // Warning: Scaling through the canvas will only work with images that use
- // premultiplied alpha.
- var maxDimension = Math.max( image.width, image.height );
- var newWidth = Math.floor( image.width * maxSize / maxDimension );
- var newHeight = Math.floor( image.height * maxSize / maxDimension );
- var canvas = document.createElement( 'canvas' );
- canvas.width = newWidth;
- canvas.height = newHeight;
- var ctx = canvas.getContext( "2d" );
- ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight );
- return canvas;
- }
- function setCubeTexture ( texture, slot ) {
- if ( texture.image.length === 6 ) {
- if ( texture.needsUpdate ) {
- if ( ! texture.image.__webglTextureCube ) {
- texture.addEventListener( 'dispose', onTextureDispose );
- texture.image.__webglTextureCube = _gl.createTexture();
- _this.info.memory.textures ++;
- }
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
- _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
- var isCompressed = texture instanceof THREE.CompressedTexture;
- var cubeImage = [];
- for ( var i = 0; i < 6; i ++ ) {
- if ( _this.autoScaleCubemaps && ! isCompressed ) {
- cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
- } else {
- cubeImage[ i ] = texture.image[ i ];
- }
- }
- var image = cubeImage[ 0 ],
- isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
- glFormat = paramThreeToGL( texture.format ),
- glType = paramThreeToGL( texture.type );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
- for ( var i = 0; i < 6; i ++ ) {
- if ( isCompressed ) {
- var mipmap, mipmaps = cubeImage[ i ].mipmaps;
- for( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
- mipmap = mipmaps[ j ];
- _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
- }
- } else {
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
- }
- }
- if ( texture.generateMipmaps && isImagePowerOfTwo ) {
- _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- }
- texture.needsUpdate = false;
- if ( texture.onUpdate ) texture.onUpdate();
- } else {
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
- }
- }
- };
- function setCubeTextureDynamic ( texture, slot ) {
- _gl.activeTexture( _gl.TEXTURE0 + slot );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
- };
- // Render targets
- function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
- };
- function setupRenderBuffer ( renderbuffer, renderTarget ) {
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
- if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- /* For some reason this is not working. Defaulting to RGBA4.
- } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- */
- } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
- } else {
- _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
- }
- };
- this.setRenderTarget = function ( renderTarget ) {
- var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
- if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
- if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
- if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
- renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
- renderTarget.__webglTexture = _gl.createTexture();
- _this.info.memory.textures ++;
- // Setup texture, create render and frame buffers
- var isTargetPowerOfTwo = isPowerOfTwo( renderTarget.width ) && isPowerOfTwo( renderTarget.height ),
- glFormat = paramThreeToGL( renderTarget.format ),
- glType = paramThreeToGL( renderTarget.type );
- if ( isCube ) {
- renderTarget.__webglFramebuffer = [];
- renderTarget.__webglRenderbuffer = [];
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
- setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
- for ( var i = 0; i < 6; i ++ ) {
- renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
- renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
- _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
- setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
- setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
- }
- if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- } else {
- renderTarget.__webglFramebuffer = _gl.createFramebuffer();
- if ( renderTarget.shareDepthFrom ) {
- renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
- } else {
- renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
- }
- _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
- setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
- _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
- setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
- if ( renderTarget.shareDepthFrom ) {
- if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
- } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
- _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
- }
- } else {
- setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
- }
- if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
- }
- // Release everything
- if ( isCube ) {
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
- } else {
- _gl.bindTexture( _gl.TEXTURE_2D, null );
- }
- _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
- }
- var framebuffer, width, height, vx, vy;
- if ( renderTarget ) {
- if ( isCube ) {
- framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
- } else {
- framebuffer = renderTarget.__webglFramebuffer;
- }
- width = renderTarget.width;
- height = renderTarget.height;
- vx = 0;
- vy = 0;
- } else {
- framebuffer = null;
- width = _viewportWidth;
- height = _viewportHeight;
- vx = _viewportX;
- vy = _viewportY;
- }
- if ( framebuffer !== _currentFramebuffer ) {
- _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
- _gl.viewport( vx, vy, width, height );
- _currentFramebuffer = framebuffer;
- }
- _currentWidth = width;
- _currentHeight = height;
- };
- function updateRenderTargetMipmap ( renderTarget ) {
- if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
- _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
- _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
- } else {
- _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
- _gl.generateMipmap( _gl.TEXTURE_2D );
- _gl.bindTexture( _gl.TEXTURE_2D, null );
- }
- };
- // Fallback filters for non-power-of-2 textures
- function filterFallback ( f ) {
- if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
- return _gl.NEAREST;
- }
- return _gl.LINEAR;
- };
- // Map three.js constants to WebGL constants
- function paramThreeToGL ( p ) {
- if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
- if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
- if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
- if ( p === THREE.NearestFilter ) return _gl.NEAREST;
- if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
- if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
- if ( p === THREE.LinearFilter ) return _gl.LINEAR;
- if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
- if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
- if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
- if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
- if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
- if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
- if ( p === THREE.ByteType ) return _gl.BYTE;
- if ( p === THREE.ShortType ) return _gl.SHORT;
- if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
- if ( p === THREE.IntType ) return _gl.INT;
- if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
- if ( p === THREE.FloatType ) return _gl.FLOAT;
- if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
- if ( p === THREE.RGBFormat ) return _gl.RGB;
- if ( p === THREE.RGBAFormat ) return _gl.RGBA;
- if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
- if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
- if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
- if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
- if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
- if ( p === THREE.ZeroFactor ) return _gl.ZERO;
- if ( p === THREE.OneFactor ) return _gl.ONE;
- if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
- if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
- if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
- if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
- if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
- if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
- if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
- if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
- if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
- if ( _glExtensionCompressedTextureS3TC !== undefined ) {
- if ( p === THREE.RGB_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGB_S3TC_DXT1_EXT;
- if ( p === THREE.RGBA_S3TC_DXT1_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT1_EXT;
- if ( p === THREE.RGBA_S3TC_DXT3_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT3_EXT;
- if ( p === THREE.RGBA_S3TC_DXT5_Format ) return _glExtensionCompressedTextureS3TC.COMPRESSED_RGBA_S3TC_DXT5_EXT;
- }
- return 0;
- };
- // Allocations
- function allocateBones ( object ) {
- if ( _supportsBoneTextures && object && object.useVertexTexture ) {
- return 1024;
- } else {
- // default for when object is not specified
- // ( for example when prebuilding shader
- // to be used with multiple objects )
- //
- // - leave some extra space for other uniforms
- // - limit here is ANGLE's 254 max uniform vectors
- // (up to 54 should be safe)
- var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
- var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
- var maxBones = nVertexMatrices;
- if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
- maxBones = Math.min( object.bones.length, maxBones );
- if ( maxBones < object.bones.length ) {
- console.warn( "WebGLRenderer: too many bones - " + object.bones.length + ", this GPU supports just " + maxBones + " (try OpenGL instead of ANGLE)" );
- }
- }
- return maxBones;
- }
- };
- function allocateLights( lights ) {
- var dirLights = 0;
- var pointLights = 0;
- var spotLights = 0;
- var hemiLights = 0;
- for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
- var light = lights[ l ];
- if ( light.onlyShadow ) continue;
- if ( light instanceof THREE.DirectionalLight ) dirLights ++;
- if ( light instanceof THREE.PointLight ) pointLights ++;
- if ( light instanceof THREE.SpotLight ) spotLights ++;
- if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
- }
- return { 'directional' : dirLights, 'point' : pointLights, 'spot': spotLights, 'hemi': hemiLights };
- };
- function allocateShadows( lights ) {
- var maxShadows = 0;
- for ( var l = 0, ll = lights.length; l < ll; l++ ) {
- var light = lights[ l ];
- if ( ! light.castShadow ) continue;
- if ( light instanceof THREE.SpotLight ) maxShadows ++;
- if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
- }
- return maxShadows;
- };
- // Initialization
- function initGL() {
- try {
- if ( ! ( _gl = _canvas.getContext( 'experimental-webgl', { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer } ) ) ) {
- throw 'Error creating WebGL context.';
- }
- } catch ( error ) {
- console.error( error );
- }
- _glExtensionTextureFloat = _gl.getExtension( 'OES_texture_float' );
- _glExtensionTextureFloatLinear = _gl.getExtension( 'OES_texture_float_linear' );
- _glExtensionStandardDerivatives = _gl.getExtension( 'OES_standard_derivatives' );
- _glExtensionTextureFilterAnisotropic = _gl.getExtension( 'EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || _gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
- _glExtensionCompressedTextureS3TC = _gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || _gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
- if ( ! _glExtensionTextureFloat ) {
- console.log( 'THREE.WebGLRenderer: Float textures not supported.' );
- }
- if ( ! _glExtensionStandardDerivatives ) {
- console.log( 'THREE.WebGLRenderer: Standard derivatives not supported.' );
- }
- if ( ! _glExtensionTextureFilterAnisotropic ) {
- console.log( 'THREE.WebGLRenderer: Anisotropic texture filtering not supported.' );
- }
- if ( ! _glExtensionCompressedTextureS3TC ) {
- console.log( 'THREE.WebGLRenderer: S3TC compressed textures not supported.' );
- }
- if ( _gl.getShaderPrecisionFormat === undefined ) {
- _gl.getShaderPrecisionFormat = function() {
- return {
- "rangeMin" : 1,
- "rangeMax" : 1,
- "precision" : 1
- };
- }
- }
- };
- function setDefaultGLState () {
- _gl.clearColor( 0, 0, 0, 1 );
- _gl.clearDepth( 1 );
- _gl.clearStencil( 0 );
- _gl.enable( _gl.DEPTH_TEST );
- _gl.depthFunc( _gl.LEQUAL );
- _gl.frontFace( _gl.CCW );
- _gl.cullFace( _gl.BACK );
- _gl.enable( _gl.CULL_FACE );
- _gl.enable( _gl.BLEND );
- _gl.blendEquation( _gl.FUNC_ADD );
- _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
- _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
- };
- // default plugins (order is important)
- this.shadowMapPlugin = new THREE.ShadowMapPlugin();
- this.addPrePlugin( this.shadowMapPlugin );
- this.addPostPlugin( new THREE.SpritePlugin() );
- this.addPostPlugin( new THREE.LensFlarePlugin() );
- };
|