WebGLRenderer.js 134 KB

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