WebGLRenderer.js 148 KB

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