WebGLRenderer.js 138 KB

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