WebGLRenderer.js 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author szimek / https://github.com/szimek/
  6. */
  7. THREE.WebGLRenderer = function ( parameters ) {
  8. // Currently you can use just up to 4 directional / point lights total.
  9. // Chrome barfs on shader linking when there are more than 4 lights :(
  10. // The problem comes from shader using too many varying vectors.
  11. // This is not GPU limitation as the same shader works ok in Firefox
  12. // and Chrome with "--use-gl=desktop" flag.
  13. // Difference comes from Chrome on Windows using by default ANGLE,
  14. // thus going DirectX9 route (while FF uses OpenGL).
  15. // See http://code.google.com/p/chromium/issues/detail?id=63491
  16. var _gl,
  17. _canvas = document.createElement( 'canvas' ),
  18. _programs = [],
  19. _currentProgram = null,
  20. _currentFramebuffer = null,
  21. _currentDepthMask = true,
  22. _this = this,
  23. // gl state cache
  24. _oldDoubleSided = null,
  25. _oldFlipSided = null,
  26. _oldBlending = null,
  27. _oldDepth = null,
  28. _cullEnabled = true,
  29. _viewportX = 0,
  30. _viewportY = 0,
  31. _viewportWidth = 0,
  32. _viewportHeight = 0,
  33. // camera matrices caches
  34. _frustum = [
  35. new THREE.Vector4(),
  36. new THREE.Vector4(),
  37. new THREE.Vector4(),
  38. new THREE.Vector4(),
  39. new THREE.Vector4(),
  40. new THREE.Vector4()
  41. ],
  42. _projScreenMatrix = new THREE.Matrix4(),
  43. _projectionMatrixArray = new Float32Array( 16 ),
  44. _viewMatrixArray = new Float32Array( 16 ),
  45. _vector3 = new THREE.Vector4(),
  46. // light arrays cache
  47. _lights = {
  48. ambient: [ 0, 0, 0 ],
  49. directional: { length: 0, colors: new Array(), positions: new Array() },
  50. point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() }
  51. },
  52. // parameters defaults
  53. stencil = true,
  54. antialias = true,
  55. clearColor = new THREE.Color( 0x000000 ),
  56. clearAlpha = 0;
  57. if ( parameters ) {
  58. if ( parameters.stencil != undefined ) stencil = parameters.stencil;
  59. if ( parameters.antialias !== undefined ) antialias = parameters.antialias;
  60. if ( parameters.clearColor !== undefined ) clearColor.setHex( parameters.clearColor );
  61. if ( parameters.clearAlpha !== undefined ) clearAlpha = parameters.clearAlpha;
  62. }
  63. this.maxMorphTargets = 8;
  64. this.domElement = _canvas;
  65. this.autoClear = true;
  66. this.sortObjects = true;
  67. initGL( antialias, clearColor, clearAlpha, stencil );
  68. this.context = _gl;
  69. // prepare stencil shadow polygon
  70. if ( stencil ) {
  71. var _stencilShadow = {};
  72. _stencilShadow.vertices = new Float32Array( 12 );
  73. _stencilShadow.faces = new Uint16Array( 6 );
  74. _stencilShadow.darkness = 0.5;
  75. _stencilShadow.vertices[ 0 * 3 + 0 ] = -2; _stencilShadow.vertices[ 0 * 3 + 1 ] = -1; _stencilShadow.vertices[ 0 * 3 + 2 ] = -1;
  76. _stencilShadow.vertices[ 1 * 3 + 0 ] = 2; _stencilShadow.vertices[ 1 * 3 + 1 ] = -1; _stencilShadow.vertices[ 1 * 3 + 2 ] = -1;
  77. _stencilShadow.vertices[ 2 * 3 + 0 ] = 2; _stencilShadow.vertices[ 2 * 3 + 1 ] = 1; _stencilShadow.vertices[ 2 * 3 + 2 ] = -1;
  78. _stencilShadow.vertices[ 3 * 3 + 0 ] = -2; _stencilShadow.vertices[ 3 * 3 + 1 ] = 1; _stencilShadow.vertices[ 3 * 3 + 2 ] = -1;
  79. _stencilShadow.faces[ 0 ] = 0; _stencilShadow.faces[ 1 ] = 1; _stencilShadow.faces[ 2 ] = 2;
  80. _stencilShadow.faces[ 3 ] = 0; _stencilShadow.faces[ 4 ] = 2; _stencilShadow.faces[ 5 ] = 3;
  81. _stencilShadow.vertexBuffer = _gl.createBuffer();
  82. _stencilShadow.elementBuffer = _gl.createBuffer();
  83. _gl.bindBuffer( _gl.ARRAY_BUFFER, _stencilShadow.vertexBuffer );
  84. _gl.bufferData( _gl.ARRAY_BUFFER, _stencilShadow.vertices, _gl.STATIC_DRAW );
  85. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _stencilShadow.elementBuffer );
  86. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _stencilShadow.faces, _gl.STATIC_DRAW );
  87. _stencilShadow.program = _gl.createProgram();
  88. _gl.attachShader( _stencilShadow.program, getShader( "fragment", THREE.ShaderLib.shadowPost.fragmentShader ));
  89. _gl.attachShader( _stencilShadow.program, getShader( "vertex", THREE.ShaderLib.shadowPost.vertexShader ));
  90. _gl.linkProgram( _stencilShadow.program );
  91. _stencilShadow.vertexLocation = _gl.getAttribLocation ( _stencilShadow.program, "position" );
  92. _stencilShadow.projectionLocation = _gl.getUniformLocation( _stencilShadow.program, "projectionMatrix" );
  93. _stencilShadow.darknessLocation = _gl.getUniformLocation( _stencilShadow.program, "darkness" );
  94. }
  95. // prepare lens flare
  96. var _lensFlare = {};
  97. var i;
  98. _lensFlare.vertices = new Float32Array( 8 + 8 );
  99. _lensFlare.faces = new Uint16Array( 6 );
  100. _lensFlare.transparency = 0.5;
  101. i = 0;
  102. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex
  103. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc.
  104. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1;
  105. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0;
  106. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  107. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  108. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1;
  109. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1;
  110. i = 0;
  111. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2;
  112. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3;
  113. _lensFlare.vertexBuffer = _gl.createBuffer();
  114. _lensFlare.elementBuffer = _gl.createBuffer();
  115. _lensFlare.tempTexture = _gl.createTexture();
  116. _lensFlare.readBackPixels = new Uint8Array( 16*16*4 );
  117. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  118. _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
  119. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  120. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
  121. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  122. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null );
  123. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  124. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  125. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  126. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  127. _lensFlare.program = _gl.createProgram();
  128. _gl.attachShader( _lensFlare.program, getShader( "fragment", THREE.ShaderLib.lensFlare.fragmentShader ));
  129. _gl.attachShader( _lensFlare.program, getShader( "vertex", THREE.ShaderLib.lensFlare.vertexShader ));
  130. _gl.linkProgram( _lensFlare.program );
  131. _lensFlare.attributes = {};
  132. _lensFlare.uniforms = {};
  133. _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
  134. _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "UV" );
  135. _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
  136. _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
  137. _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
  138. _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
  139. _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
  140. _lensFlare.uniforms.renderPink = _gl.getUniformLocation( _lensFlare.program, "renderPink" );
  141. this.setSize = function ( width, height ) {
  142. _canvas.width = width;
  143. _canvas.height = height;
  144. this.setViewport( 0, 0, _canvas.width, _canvas.height );
  145. };
  146. this.setViewport = function ( x, y, width, height ) {
  147. _viewportX = x;
  148. _viewportY = y;
  149. _viewportWidth = width;
  150. _viewportHeight = height;
  151. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  152. };
  153. this.setScissor = function ( x, y, width, height ) {
  154. _gl.scissor( x, y, width, height );
  155. };
  156. this.enableScissorTest = function ( enable ) {
  157. if ( enable )
  158. _gl.enable( _gl.SCISSOR_TEST );
  159. else
  160. _gl.disable( _gl.SCISSOR_TEST );
  161. };
  162. this.enableDepthBufferWrite = function ( enable ) {
  163. _currentDepthMask = enable;
  164. _gl.depthMask( enable );
  165. };
  166. this.setClearColorHex = function ( hex, alpha ) {
  167. var color = new THREE.Color( hex );
  168. _gl.clearColor( color.r, color.g, color.b, alpha );
  169. };
  170. this.setClearColor = function ( color, alpha ) {
  171. _gl.clearColor( color.r, color.g, color.b, alpha );
  172. };
  173. this.clear = function () {
  174. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT | _gl.STENCIL_BUFFER_BIT );
  175. };
  176. this.setStencilShadowDarkness = function( value ) {
  177. _stencilShadow.darkness = value;
  178. };
  179. function setupLights ( program, lights ) {
  180. var l, ll, light, r = 0, g = 0, b = 0,
  181. color, position, intensity, distance,
  182. zlights = _lights,
  183. dcolors = zlights.directional.colors,
  184. dpositions = zlights.directional.positions,
  185. pcolors = zlights.point.colors,
  186. ppositions = zlights.point.positions,
  187. pdistances = zlights.point.distances,
  188. dlength = 0,
  189. plength = 0,
  190. doffset = 0,
  191. poffset = 0;
  192. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  193. light = lights[ l ];
  194. color = light.color;
  195. position = light.position;
  196. intensity = light.intensity;
  197. distance = light.distance;
  198. if ( light instanceof THREE.AmbientLight ) {
  199. r += color.r;
  200. g += color.g;
  201. b += color.b;
  202. } else if ( light instanceof THREE.DirectionalLight ) {
  203. doffset = dlength * 3;
  204. dcolors[ doffset ] = color.r * intensity;
  205. dcolors[ doffset + 1 ] = color.g * intensity;
  206. dcolors[ doffset + 2 ] = color.b * intensity;
  207. dpositions[ doffset ] = position.x;
  208. dpositions[ doffset + 1 ] = position.y;
  209. dpositions[ doffset + 2 ] = position.z;
  210. dlength += 1;
  211. } else if( light instanceof THREE.PointLight ) {
  212. poffset = plength * 3;
  213. pcolors[ poffset ] = color.r * intensity;
  214. pcolors[ poffset + 1 ] = color.g * intensity;
  215. pcolors[ poffset + 2 ] = color.b * intensity;
  216. ppositions[ poffset ] = position.x;
  217. ppositions[ poffset + 1 ] = position.y;
  218. ppositions[ poffset + 2 ] = position.z;
  219. pdistances[ plength ] = distance;
  220. plength += 1;
  221. }
  222. }
  223. // null eventual remains from removed lights
  224. // (this is to avoid if in shader)
  225. for( l = dlength * 3; l < dcolors.length; l++ ) dcolors[ l ] = 0.0;
  226. for( l = plength * 3; l < pcolors.length; l++ ) pcolors[ l ] = 0.0;
  227. zlights.point.length = plength;
  228. zlights.directional.length = dlength;
  229. zlights.ambient[ 0 ] = r;
  230. zlights.ambient[ 1 ] = g;
  231. zlights.ambient[ 2 ] = b;
  232. };
  233. function createParticleBuffers ( geometry ) {
  234. geometry.__webglVertexBuffer = _gl.createBuffer();
  235. geometry.__webglColorBuffer = _gl.createBuffer();
  236. };
  237. function createLineBuffers( geometry ) {
  238. geometry.__webglVertexBuffer = _gl.createBuffer();
  239. geometry.__webglColorBuffer = _gl.createBuffer();
  240. };
  241. function createRibbonBuffers( geometry ) {
  242. geometry.__webglVertexBuffer = _gl.createBuffer();
  243. geometry.__webglColorBuffer = _gl.createBuffer();
  244. };
  245. function createMeshBuffers( geometryGroup ) {
  246. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  247. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  248. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  249. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  250. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  251. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  252. geometryGroup.__webglSkinVertexABuffer = _gl.createBuffer();
  253. geometryGroup.__webglSkinVertexBBuffer = _gl.createBuffer();
  254. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  255. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  256. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  257. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  258. if ( geometryGroup.numMorphTargets ) {
  259. var m, ml;
  260. geometryGroup.__webglMorphTargetsBuffers = [];
  261. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m++ ) {
  262. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  263. }
  264. }
  265. };
  266. function initLineBuffers ( geometry ) {
  267. var nvertices = geometry.vertices.length;
  268. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  269. geometry.__colorArray = new Float32Array( nvertices * 3 );
  270. geometry.__webglLineCount = nvertices;
  271. };
  272. function initRibbonBuffers ( geometry ) {
  273. var nvertices = geometry.vertices.length;
  274. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  275. geometry.__colorArray = new Float32Array( nvertices * 3 );
  276. geometry.__webglVertexCount = nvertices;
  277. };
  278. function initParticleBuffers ( geometry ) {
  279. var nvertices = geometry.vertices.length;
  280. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  281. geometry.__colorArray = new Float32Array( nvertices * 3 );
  282. geometry.__sortArray = [];
  283. geometry.__webglParticleCount = nvertices;
  284. };
  285. function initMeshBuffers ( geometryGroup, object ) {
  286. var f, fl, fi, face,
  287. m, ml, size,
  288. nvertices = 0, ntris = 0, nlines = 0,
  289. uvType,
  290. vertexColorType,
  291. normalType,
  292. materials,
  293. attribute,
  294. geometry = object.geometry,
  295. obj_faces = geometry.faces,
  296. chunk_faces = geometryGroup.faces;
  297. for ( f = 0, fl = chunk_faces.length; f < fl; f++ ) {
  298. fi = chunk_faces[ f ];
  299. face = obj_faces[ fi ];
  300. if ( face instanceof THREE.Face3 ) {
  301. nvertices += 3;
  302. ntris += 1;
  303. nlines += 3;
  304. } else if ( face instanceof THREE.Face4 ) {
  305. nvertices += 4;
  306. ntris += 2;
  307. nlines += 4;
  308. }
  309. }
  310. materials = unrollGroupMaterials( geometryGroup, object );
  311. uvType = bufferGuessUVType( materials, geometryGroup, object );
  312. normalType = bufferGuessNormalType( materials, geometryGroup, object );
  313. vertexColorType = bufferGuessVertexColorType( materials, geometryGroup, object );
  314. //console.log("uvType",uvType, "normalType",normalType, "vertexColorType",vertexColorType, object, geometryGroup, materials );
  315. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  316. if ( normalType ) {
  317. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  318. }
  319. if ( geometry.hasTangents ) {
  320. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  321. }
  322. if ( vertexColorType ) {
  323. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  324. }
  325. if ( uvType ) {
  326. if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
  327. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  328. }
  329. if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
  330. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  331. }
  332. }
  333. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  334. geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
  335. geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
  336. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  337. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  338. }
  339. geometryGroup.__faceArray = new Uint16Array( ntris * 3 + ( object.geometry.edgeFaces ? object.geometry.edgeFaces.length * 2 * 3 : 0 ));
  340. geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
  341. if ( geometryGroup.numMorphTargets ) {
  342. geometryGroup.__morphTargetsArrays = [];
  343. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m++ ) {
  344. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  345. }
  346. }
  347. geometryGroup.__needsSmoothNormals = ( normalType == THREE.SmoothShading );
  348. geometryGroup.__uvType = uvType;
  349. geometryGroup.__vertexColorType = vertexColorType;
  350. geometryGroup.__normalType = normalType;
  351. geometryGroup.__webglFaceCount = ntris * 3 + ( object.geometry.edgeFaces ? object.geometry.edgeFaces.length * 2 * 3 : 0 );
  352. geometryGroup.__webglLineCount = nlines * 2;
  353. // custom attributes
  354. for ( m = 0, ml = materials.length; m < ml; m ++ ) {
  355. if ( materials[ m ].attributes ) {
  356. geometryGroup.__webglCustomAttributes = {};
  357. for ( a in materials[ m ].attributes ) {
  358. attribute = materials[ m ].attributes[ a ];
  359. size = 1; // "f" and "i"
  360. if( attribute.type === "v2" ) size = 2;
  361. else if( attribute.type === "v3" ) size = 3;
  362. else if( attribute.type === "v4" ) size = 4;
  363. else if( attribute.type === "c" ) size = 3;
  364. attribute.size = size;
  365. attribute.needsUpdate = true;
  366. attribute.array = new Float32Array( nvertices * size );
  367. attribute.buffer = _gl.createBuffer();
  368. geometryGroup.__webglCustomAttributes[ a ] = attribute;
  369. }
  370. }
  371. }
  372. };
  373. function setMeshBuffers ( geometryGroup, object, hint ) {
  374. var f, fl, fi, face,
  375. vertexNormals, faceNormal, normal,
  376. vertexColors, faceColor,
  377. vertexTangents,
  378. uvType, vertexColorType, normalType,
  379. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4,
  380. c1, c2, c3, c4,
  381. sw1, sw2, sw3, sw4,
  382. si1, si2, si3, si4,
  383. sa1, sa2, sa3, sa4,
  384. sb1, sb2, sb3, sb4,
  385. m, ml, i,
  386. vn, uvi, uv2i,
  387. vk, vkl, vka,
  388. a,
  389. vertexIndex = 0,
  390. offset = 0,
  391. offset_uv = 0,
  392. offset_uv2 = 0,
  393. offset_face = 0,
  394. offset_normal = 0,
  395. offset_tangent = 0,
  396. offset_line = 0,
  397. offset_color = 0,
  398. offset_skin = 0,
  399. offset_morphTarget = 0,
  400. offset_custom = 0,
  401. vertexArray = geometryGroup.__vertexArray,
  402. uvArray = geometryGroup.__uvArray,
  403. uv2Array = geometryGroup.__uv2Array,
  404. normalArray = geometryGroup.__normalArray,
  405. tangentArray = geometryGroup.__tangentArray,
  406. colorArray = geometryGroup.__colorArray,
  407. skinVertexAArray = geometryGroup.__skinVertexAArray,
  408. skinVertexBArray = geometryGroup.__skinVertexBArray,
  409. skinIndexArray = geometryGroup.__skinIndexArray,
  410. skinWeightArray = geometryGroup.__skinWeightArray,
  411. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  412. customAttributes = geometryGroup.__webglCustomAttributes,
  413. customAttribute,
  414. faceArray = geometryGroup.__faceArray,
  415. lineArray = geometryGroup.__lineArray,
  416. needsSmoothNormals = geometryGroup.__needsSmoothNormals,
  417. vertexColorType = geometryGroup.__vertexColorType,
  418. uvType = geometryGroup.__uvType,
  419. normalType = geometryGroup.__normalType,
  420. geometry = object.geometry, // this is shared for all chunks
  421. dirtyVertices = geometry.__dirtyVertices,
  422. dirtyElements = geometry.__dirtyElements,
  423. dirtyUvs = geometry.__dirtyUvs,
  424. dirtyNormals = geometry.__dirtyNormals,
  425. dirtyTangents = geometry.__dirtyTangents,
  426. dirtyColors = geometry.__dirtyColors,
  427. dirtyMorphTargets = geometry.__dirtyMorphTargets,
  428. vertices = geometry.vertices,
  429. chunk_faces = geometryGroup.faces,
  430. obj_faces = geometry.faces,
  431. obj_uvs = geometry.faceVertexUvs[ 0 ],
  432. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  433. obj_colors = geometry.colors,
  434. obj_skinVerticesA = geometry.skinVerticesA,
  435. obj_skinVerticesB = geometry.skinVerticesB,
  436. obj_skinIndices = geometry.skinIndices,
  437. obj_skinWeights = geometry.skinWeights,
  438. obj_edgeFaces = geometry.edgeFaces,
  439. morphTargets = geometry.morphTargets;
  440. if ( customAttributes ) {
  441. for ( a in customAttributes ) {
  442. customAttributes[ a ].offset = 0;
  443. }
  444. }
  445. for ( f = 0, fl = chunk_faces.length; f < fl; f ++ ) {
  446. fi = chunk_faces[ f ];
  447. face = obj_faces[ fi ];
  448. if ( obj_uvs ) {
  449. uv = obj_uvs[ fi ];
  450. }
  451. if ( obj_uvs2 ) {
  452. uv2 = obj_uvs2[ fi ];
  453. }
  454. vertexNormals = face.vertexNormals;
  455. faceNormal = face.normal;
  456. vertexColors = face.vertexColors;
  457. faceColor = face.color;
  458. vertexTangents = face.vertexTangents;
  459. if ( face instanceof THREE.Face3 ) {
  460. if ( dirtyVertices ) {
  461. v1 = vertices[ face.a ].position;
  462. v2 = vertices[ face.b ].position;
  463. v3 = vertices[ face.c ].position;
  464. vertexArray[ offset ] = v1.x;
  465. vertexArray[ offset + 1 ] = v1.y;
  466. vertexArray[ offset + 2 ] = v1.z;
  467. vertexArray[ offset + 3 ] = v2.x;
  468. vertexArray[ offset + 4 ] = v2.y;
  469. vertexArray[ offset + 5 ] = v2.z;
  470. vertexArray[ offset + 6 ] = v3.x;
  471. vertexArray[ offset + 7 ] = v3.y;
  472. vertexArray[ offset + 8 ] = v3.z;
  473. offset += 9;
  474. }
  475. if ( customAttributes ) {
  476. for ( a in customAttributes ) {
  477. customAttribute = customAttributes[ a ];
  478. if ( customAttribute.needsUpdate ) {
  479. offset_custom = customAttribute.offset;
  480. if( customAttribute.size === 1 ) {
  481. customAttribute.array[ offset_custom + 0 ] = customAttribute.value[ face.a ];
  482. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  483. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  484. customAttribute.offset += 3;
  485. } else {
  486. v1 = customAttribute.value[ face.a ];
  487. v2 = customAttribute.value[ face.b ];
  488. v3 = customAttribute.value[ face.c ];
  489. if( customAttribute.size === 2 ) {
  490. customAttribute.array[ offset_custom + 0 ] = v1.x;
  491. customAttribute.array[ offset_custom + 1 ] = v1.y;
  492. customAttribute.array[ offset_custom + 2 ] = v2.x;
  493. customAttribute.array[ offset_custom + 3 ] = v2.y;
  494. customAttribute.array[ offset_custom + 4 ] = v3.x;
  495. customAttribute.array[ offset_custom + 5 ] = v3.y;
  496. customAttribute.offset += 6;
  497. } else if( customAttribute.size === 3 ) {
  498. customAttribute.array[ offset_custom + 0 ] = v1.x;
  499. customAttribute.array[ offset_custom + 1 ] = v1.y;
  500. customAttribute.array[ offset_custom + 2 ] = v1.z;
  501. customAttribute.array[ offset_custom + 3 ] = v2.x;
  502. customAttribute.array[ offset_custom + 4 ] = v2.y;
  503. customAttribute.array[ offset_custom + 5 ] = v2.z;
  504. customAttribute.array[ offset_custom + 6 ] = v3.x;
  505. customAttribute.array[ offset_custom + 7 ] = v3.y;
  506. customAttribute.array[ offset_custom + 8 ] = v3.z;
  507. customAttribute.offset += 9;
  508. } else {
  509. customAttribute.array[ offset_custom + 0 ] = v1.x;
  510. customAttribute.array[ offset_custom + 1 ] = v1.y;
  511. customAttribute.array[ offset_custom + 2 ] = v1.z;
  512. customAttribute.array[ offset_custom + 3 ] = v1.w;
  513. customAttribute.array[ offset_custom + 4 ] = v2.x;
  514. customAttribute.array[ offset_custom + 5 ] = v2.y;
  515. customAttribute.array[ offset_custom + 6 ] = v2.z;
  516. customAttribute.array[ offset_custom + 7 ] = v2.w;
  517. customAttribute.array[ offset_custom + 8 ] = v3.x;
  518. customAttribute.array[ offset_custom + 9 ] = v3.y;
  519. customAttribute.array[ offset_custom + 10 ] = v3.z;
  520. customAttribute.array[ offset_custom + 11 ] = v3.w;
  521. customAttribute.offset += 12;
  522. }
  523. }
  524. }
  525. }
  526. }
  527. if ( dirtyMorphTargets ) {
  528. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  529. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  530. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  531. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  532. vka = morphTargetsArrays[ vk ];
  533. vka[ offset_morphTarget + 0 ] = v1.x;
  534. vka[ offset_morphTarget + 1 ] = v1.y;
  535. vka[ offset_morphTarget + 2 ] = v1.z;
  536. vka[ offset_morphTarget + 3 ] = v2.x;
  537. vka[ offset_morphTarget + 4 ] = v2.y;
  538. vka[ offset_morphTarget + 5 ] = v2.z;
  539. vka[ offset_morphTarget + 6 ] = v3.x;
  540. vka[ offset_morphTarget + 7 ] = v3.y;
  541. vka[ offset_morphTarget + 8 ] = v3.z;
  542. }
  543. offset_morphTarget += 9;
  544. }
  545. if ( obj_skinWeights.length ) {
  546. // weights
  547. sw1 = obj_skinWeights[ face.a ];
  548. sw2 = obj_skinWeights[ face.b ];
  549. sw3 = obj_skinWeights[ face.c ];
  550. skinWeightArray[ offset_skin ] = sw1.x;
  551. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  552. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  553. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  554. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  555. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  556. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  557. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  558. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  559. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  560. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  561. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  562. // indices
  563. si1 = obj_skinIndices[ face.a ];
  564. si2 = obj_skinIndices[ face.b ];
  565. si3 = obj_skinIndices[ face.c ];
  566. skinIndexArray[ offset_skin ] = si1.x;
  567. skinIndexArray[ offset_skin + 1 ] = si1.y;
  568. skinIndexArray[ offset_skin + 2 ] = si1.z;
  569. skinIndexArray[ offset_skin + 3 ] = si1.w;
  570. skinIndexArray[ offset_skin + 4 ] = si2.x;
  571. skinIndexArray[ offset_skin + 5 ] = si2.y;
  572. skinIndexArray[ offset_skin + 6 ] = si2.z;
  573. skinIndexArray[ offset_skin + 7 ] = si2.w;
  574. skinIndexArray[ offset_skin + 8 ] = si3.x;
  575. skinIndexArray[ offset_skin + 9 ] = si3.y;
  576. skinIndexArray[ offset_skin + 10 ] = si3.z;
  577. skinIndexArray[ offset_skin + 11 ] = si3.w;
  578. // vertices A
  579. sa1 = obj_skinVerticesA[ face.a ];
  580. sa2 = obj_skinVerticesA[ face.b ];
  581. sa3 = obj_skinVerticesA[ face.c ];
  582. skinVertexAArray[ offset_skin ] = sa1.x;
  583. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  584. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  585. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  586. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  587. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  588. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  589. skinVertexAArray[ offset_skin + 7 ] = 1;
  590. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  591. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  592. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  593. skinVertexAArray[ offset_skin + 11 ] = 1;
  594. // vertices B
  595. sb1 = obj_skinVerticesB[ face.a ];
  596. sb2 = obj_skinVerticesB[ face.b ];
  597. sb3 = obj_skinVerticesB[ face.c ];
  598. skinVertexBArray[ offset_skin ] = sb1.x;
  599. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  600. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  601. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  602. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  603. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  604. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  605. skinVertexBArray[ offset_skin + 7 ] = 1;
  606. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  607. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  608. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  609. skinVertexBArray[ offset_skin + 11 ] = 1;
  610. offset_skin += 12;
  611. }
  612. if ( dirtyColors && vertexColorType ) {
  613. if ( vertexColors.length == 3 && vertexColorType == THREE.VertexColors ) {
  614. c1 = vertexColors[ 0 ];
  615. c2 = vertexColors[ 1 ];
  616. c3 = vertexColors[ 2 ];
  617. } else {
  618. c1 = faceColor;
  619. c2 = faceColor;
  620. c3 = faceColor;
  621. }
  622. colorArray[ offset_color ] = c1.r;
  623. colorArray[ offset_color + 1 ] = c1.g;
  624. colorArray[ offset_color + 2 ] = c1.b;
  625. colorArray[ offset_color + 3 ] = c2.r;
  626. colorArray[ offset_color + 4 ] = c2.g;
  627. colorArray[ offset_color + 5 ] = c2.b;
  628. colorArray[ offset_color + 6 ] = c3.r;
  629. colorArray[ offset_color + 7 ] = c3.g;
  630. colorArray[ offset_color + 8 ] = c3.b;
  631. offset_color += 9;
  632. }
  633. if ( dirtyTangents && geometry.hasTangents ) {
  634. t1 = vertexTangents[ 0 ];
  635. t2 = vertexTangents[ 1 ];
  636. t3 = vertexTangents[ 2 ];
  637. tangentArray[ offset_tangent ] = t1.x;
  638. tangentArray[ offset_tangent + 1 ] = t1.y;
  639. tangentArray[ offset_tangent + 2 ] = t1.z;
  640. tangentArray[ offset_tangent + 3 ] = t1.w;
  641. tangentArray[ offset_tangent + 4 ] = t2.x;
  642. tangentArray[ offset_tangent + 5 ] = t2.y;
  643. tangentArray[ offset_tangent + 6 ] = t2.z;
  644. tangentArray[ offset_tangent + 7 ] = t2.w;
  645. tangentArray[ offset_tangent + 8 ] = t3.x;
  646. tangentArray[ offset_tangent + 9 ] = t3.y;
  647. tangentArray[ offset_tangent + 10 ] = t3.z;
  648. tangentArray[ offset_tangent + 11 ] = t3.w;
  649. offset_tangent += 12;
  650. }
  651. if ( dirtyNormals && normalType ) {
  652. if ( vertexNormals.length == 3 && needsSmoothNormals ) {
  653. for ( i = 0; i < 3; i ++ ) {
  654. vn = vertexNormals[ i ];
  655. normalArray[ offset_normal ] = vn.x;
  656. normalArray[ offset_normal + 1 ] = vn.y;
  657. normalArray[ offset_normal + 2 ] = vn.z;
  658. offset_normal += 3;
  659. }
  660. } else {
  661. for ( i = 0; i < 3; i ++ ) {
  662. normalArray[ offset_normal ] = faceNormal.x;
  663. normalArray[ offset_normal + 1 ] = faceNormal.y;
  664. normalArray[ offset_normal + 2 ] = faceNormal.z;
  665. offset_normal += 3;
  666. }
  667. }
  668. }
  669. if ( dirtyUvs && uv !== undefined && uvType ) {
  670. for ( i = 0; i < 3; i ++ ) {
  671. uvi = uv[ i ];
  672. uvArray[ offset_uv ] = uvi.u;
  673. uvArray[ offset_uv + 1 ] = uvi.v;
  674. offset_uv += 2;
  675. }
  676. }
  677. if ( dirtyUvs && uv2 !== undefined && uvType ) {
  678. for ( i = 0; i < 3; i ++ ) {
  679. uv2i = uv2[ i ];
  680. uv2Array[ offset_uv2 ] = uv2i.u;
  681. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  682. offset_uv2 += 2;
  683. }
  684. }
  685. if ( dirtyElements ) {
  686. faceArray[ offset_face ] = vertexIndex;
  687. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  688. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  689. offset_face += 3;
  690. lineArray[ offset_line ] = vertexIndex;
  691. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  692. lineArray[ offset_line + 2 ] = vertexIndex;
  693. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  694. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  695. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  696. offset_line += 6;
  697. vertexIndex += 3;
  698. }
  699. } else if ( face instanceof THREE.Face4 ) {
  700. if ( dirtyVertices ) {
  701. v1 = vertices[ face.a ].position;
  702. v2 = vertices[ face.b ].position;
  703. v3 = vertices[ face.c ].position;
  704. v4 = vertices[ face.d ].position;
  705. vertexArray[ offset ] = v1.x;
  706. vertexArray[ offset + 1 ] = v1.y;
  707. vertexArray[ offset + 2 ] = v1.z;
  708. vertexArray[ offset + 3 ] = v2.x;
  709. vertexArray[ offset + 4 ] = v2.y;
  710. vertexArray[ offset + 5 ] = v2.z;
  711. vertexArray[ offset + 6 ] = v3.x;
  712. vertexArray[ offset + 7 ] = v3.y;
  713. vertexArray[ offset + 8 ] = v3.z;
  714. vertexArray[ offset + 9 ] = v4.x;
  715. vertexArray[ offset + 10 ] = v4.y;
  716. vertexArray[ offset + 11 ] = v4.z;
  717. offset += 12;
  718. }
  719. if ( customAttributes ) {
  720. for ( a in customAttributes ) {
  721. customAttribute = customAttributes[ a ];
  722. if ( customAttribute.needsUpdate ) {
  723. offset_custom = customAttribute.offset;
  724. if( customAttribute.size === 1 ) {
  725. customAttribute.array[ offset_custom + 0 ] = customAttribute.value[ face.a ];
  726. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  727. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  728. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.d ];
  729. customAttribute.offset += 4;
  730. } else {
  731. v1 = customAttribute.value[ face.a ];
  732. v2 = customAttribute.value[ face.b ];
  733. v3 = customAttribute.value[ face.c ];
  734. v4 = customAttribute.value[ face.d ];
  735. if( customAttribute.size === 2 ) {
  736. customAttribute.array[ offset_custom + 0 ] = v1.x;
  737. customAttribute.array[ offset_custom + 1 ] = v1.y;
  738. customAttribute.array[ offset_custom + 2 ] = v2.x;
  739. customAttribute.array[ offset_custom + 3 ] = v2.y;
  740. customAttribute.array[ offset_custom + 4 ] = v3.x;
  741. customAttribute.array[ offset_custom + 5 ] = v3.y;
  742. customAttribute.array[ offset_custom + 6 ] = v4.x;
  743. customAttribute.array[ offset_custom + 7 ] = v4.y;
  744. customAttribute.offset += 8;
  745. } else if( customAttribute.size === 3 ) {
  746. customAttribute.array[ offset_custom + 0 ] = v1.x;
  747. customAttribute.array[ offset_custom + 1 ] = v1.y;
  748. customAttribute.array[ offset_custom + 2 ] = v1.z;
  749. customAttribute.array[ offset_custom + 3 ] = v2.x;
  750. customAttribute.array[ offset_custom + 4 ] = v2.y;
  751. customAttribute.array[ offset_custom + 5 ] = v2.z;
  752. customAttribute.array[ offset_custom + 6 ] = v3.x;
  753. customAttribute.array[ offset_custom + 7 ] = v3.y;
  754. customAttribute.array[ offset_custom + 8 ] = v3.z;
  755. customAttribute.array[ offset_custom + 9 ] = v4.x;
  756. customAttribute.array[ offset_custom + 10 ] = v4.y;
  757. customAttribute.array[ offset_custom + 11 ] = v4.z;
  758. customAttribute.offset += 12;
  759. } else {
  760. customAttribute.array[ offset_custom + 0 ] = v1.x;
  761. customAttribute.array[ offset_custom + 1 ] = v1.y;
  762. customAttribute.array[ offset_custom + 2 ] = v1.z;
  763. customAttribute.array[ offset_custom + 3 ] = v1.w;
  764. customAttribute.array[ offset_custom + 4 ] = v2.x;
  765. customAttribute.array[ offset_custom + 5 ] = v2.y;
  766. customAttribute.array[ offset_custom + 6 ] = v2.z;
  767. customAttribute.array[ offset_custom + 7 ] = v2.w;
  768. customAttribute.array[ offset_custom + 8 ] = v3.x;
  769. customAttribute.array[ offset_custom + 9 ] = v3.y;
  770. customAttribute.array[ offset_custom + 10 ] = v3.z;
  771. customAttribute.array[ offset_custom + 11 ] = v3.w;
  772. customAttribute.array[ offset_custom + 12 ] = v4.x;
  773. customAttribute.array[ offset_custom + 13 ] = v4.y;
  774. customAttribute.array[ offset_custom + 14 ] = v4.z;
  775. customAttribute.array[ offset_custom + 15 ] = v4.w;
  776. customAttribute.offset += 16;
  777. }
  778. }
  779. }
  780. }
  781. }
  782. if ( dirtyMorphTargets ) {
  783. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk++ ) {
  784. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  785. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  786. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  787. v4 = morphTargets[ vk ].vertices[ face.d ].position;
  788. vka = morphTargetsArrays[ vk ];
  789. vka[ offset_morphTarget + 0 ] = v1.x;
  790. vka[ offset_morphTarget + 1 ] = v1.y;
  791. vka[ offset_morphTarget + 2 ] = v1.z;
  792. vka[ offset_morphTarget + 3 ] = v2.x;
  793. vka[ offset_morphTarget + 4 ] = v2.y;
  794. vka[ offset_morphTarget + 5 ] = v2.z;
  795. vka[ offset_morphTarget + 6 ] = v3.x;
  796. vka[ offset_morphTarget + 7 ] = v3.y;
  797. vka[ offset_morphTarget + 8 ] = v3.z;
  798. vka[ offset_morphTarget + 9 ] = v4.x;
  799. vka[ offset_morphTarget + 10 ] = v4.y;
  800. vka[ offset_morphTarget + 11 ] = v4.z;
  801. }
  802. offset_morphTarget += 12;
  803. }
  804. if ( obj_skinWeights.length ) {
  805. // weights
  806. sw1 = obj_skinWeights[ face.a ];
  807. sw2 = obj_skinWeights[ face.b ];
  808. sw3 = obj_skinWeights[ face.c ];
  809. sw4 = obj_skinWeights[ face.d ];
  810. skinWeightArray[ offset_skin ] = sw1.x;
  811. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  812. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  813. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  814. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  815. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  816. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  817. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  818. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  819. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  820. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  821. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  822. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  823. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  824. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  825. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  826. // indices
  827. si1 = obj_skinIndices[ face.a ];
  828. si2 = obj_skinIndices[ face.b ];
  829. si3 = obj_skinIndices[ face.c ];
  830. si4 = obj_skinIndices[ face.d ];
  831. skinIndexArray[ offset_skin ] = si1.x;
  832. skinIndexArray[ offset_skin + 1 ] = si1.y;
  833. skinIndexArray[ offset_skin + 2 ] = si1.z;
  834. skinIndexArray[ offset_skin + 3 ] = si1.w;
  835. skinIndexArray[ offset_skin + 4 ] = si2.x;
  836. skinIndexArray[ offset_skin + 5 ] = si2.y;
  837. skinIndexArray[ offset_skin + 6 ] = si2.z;
  838. skinIndexArray[ offset_skin + 7 ] = si2.w;
  839. skinIndexArray[ offset_skin + 8 ] = si3.x;
  840. skinIndexArray[ offset_skin + 9 ] = si3.y;
  841. skinIndexArray[ offset_skin + 10 ] = si3.z;
  842. skinIndexArray[ offset_skin + 11 ] = si3.w;
  843. skinIndexArray[ offset_skin + 12 ] = si4.x;
  844. skinIndexArray[ offset_skin + 13 ] = si4.y;
  845. skinIndexArray[ offset_skin + 14 ] = si4.z;
  846. skinIndexArray[ offset_skin + 15 ] = si4.w;
  847. // vertices A
  848. sa1 = obj_skinVerticesA[ face.a ];
  849. sa2 = obj_skinVerticesA[ face.b ];
  850. sa3 = obj_skinVerticesA[ face.c ];
  851. sa4 = obj_skinVerticesA[ face.d ];
  852. skinVertexAArray[ offset_skin ] = sa1.x;
  853. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  854. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  855. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  856. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  857. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  858. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  859. skinVertexAArray[ offset_skin + 7 ] = 1;
  860. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  861. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  862. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  863. skinVertexAArray[ offset_skin + 11 ] = 1;
  864. skinVertexAArray[ offset_skin + 12 ] = sa4.x;
  865. skinVertexAArray[ offset_skin + 13 ] = sa4.y;
  866. skinVertexAArray[ offset_skin + 14 ] = sa4.z;
  867. skinVertexAArray[ offset_skin + 15 ] = 1;
  868. // vertices B
  869. sb1 = obj_skinVerticesB[ face.a ];
  870. sb2 = obj_skinVerticesB[ face.b ];
  871. sb3 = obj_skinVerticesB[ face.c ];
  872. sb4 = obj_skinVerticesB[ face.d ];
  873. skinVertexBArray[ offset_skin ] = sb1.x;
  874. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  875. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  876. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  877. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  878. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  879. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  880. skinVertexBArray[ offset_skin + 7 ] = 1;
  881. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  882. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  883. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  884. skinVertexBArray[ offset_skin + 11 ] = 1;
  885. skinVertexBArray[ offset_skin + 12 ] = sb4.x;
  886. skinVertexBArray[ offset_skin + 13 ] = sb4.y;
  887. skinVertexBArray[ offset_skin + 14 ] = sb4.z;
  888. skinVertexBArray[ offset_skin + 15 ] = 1;
  889. offset_skin += 16;
  890. }
  891. if ( dirtyColors && vertexColorType ) {
  892. if ( vertexColors.length == 4 && vertexColorType == THREE.VertexColors ) {
  893. c1 = vertexColors[ 0 ];
  894. c2 = vertexColors[ 1 ];
  895. c3 = vertexColors[ 2 ];
  896. c4 = vertexColors[ 3 ];
  897. } else {
  898. c1 = faceColor;
  899. c2 = faceColor;
  900. c3 = faceColor;
  901. c4 = faceColor;
  902. }
  903. colorArray[ offset_color ] = c1.r;
  904. colorArray[ offset_color + 1 ] = c1.g;
  905. colorArray[ offset_color + 2 ] = c1.b;
  906. colorArray[ offset_color + 3 ] = c2.r;
  907. colorArray[ offset_color + 4 ] = c2.g;
  908. colorArray[ offset_color + 5 ] = c2.b;
  909. colorArray[ offset_color + 6 ] = c3.r;
  910. colorArray[ offset_color + 7 ] = c3.g;
  911. colorArray[ offset_color + 8 ] = c3.b;
  912. colorArray[ offset_color + 9 ] = c4.r;
  913. colorArray[ offset_color + 10 ] = c4.g;
  914. colorArray[ offset_color + 11 ] = c4.b;
  915. offset_color += 12;
  916. }
  917. if ( dirtyTangents && geometry.hasTangents ) {
  918. t1 = vertexTangents[ 0 ];
  919. t2 = vertexTangents[ 1 ];
  920. t3 = vertexTangents[ 2 ];
  921. t4 = vertexTangents[ 3 ];
  922. tangentArray[ offset_tangent ] = t1.x;
  923. tangentArray[ offset_tangent + 1 ] = t1.y;
  924. tangentArray[ offset_tangent + 2 ] = t1.z;
  925. tangentArray[ offset_tangent + 3 ] = t1.w;
  926. tangentArray[ offset_tangent + 4 ] = t2.x;
  927. tangentArray[ offset_tangent + 5 ] = t2.y;
  928. tangentArray[ offset_tangent + 6 ] = t2.z;
  929. tangentArray[ offset_tangent + 7 ] = t2.w;
  930. tangentArray[ offset_tangent + 8 ] = t3.x;
  931. tangentArray[ offset_tangent + 9 ] = t3.y;
  932. tangentArray[ offset_tangent + 10 ] = t3.z;
  933. tangentArray[ offset_tangent + 11 ] = t3.w;
  934. tangentArray[ offset_tangent + 12 ] = t4.x;
  935. tangentArray[ offset_tangent + 13 ] = t4.y;
  936. tangentArray[ offset_tangent + 14 ] = t4.z;
  937. tangentArray[ offset_tangent + 15 ] = t4.w;
  938. offset_tangent += 16;
  939. }
  940. if ( dirtyNormals && normalType ) {
  941. if ( vertexNormals.length == 4 && needsSmoothNormals ) {
  942. for ( i = 0; i < 4; i ++ ) {
  943. vn = vertexNormals[ i ];
  944. normalArray[ offset_normal ] = vn.x;
  945. normalArray[ offset_normal + 1 ] = vn.y;
  946. normalArray[ offset_normal + 2 ] = vn.z;
  947. offset_normal += 3;
  948. }
  949. } else {
  950. for ( i = 0; i < 4; i ++ ) {
  951. normalArray[ offset_normal ] = faceNormal.x;
  952. normalArray[ offset_normal + 1 ] = faceNormal.y;
  953. normalArray[ offset_normal + 2 ] = faceNormal.z;
  954. offset_normal += 3;
  955. }
  956. }
  957. }
  958. if ( dirtyUvs && uv !== undefined && uvType ) {
  959. for ( i = 0; i < 4; i ++ ) {
  960. uvi = uv[ i ];
  961. uvArray[ offset_uv ] = uvi.u;
  962. uvArray[ offset_uv + 1 ] = uvi.v;
  963. offset_uv += 2;
  964. }
  965. }
  966. if ( dirtyUvs && uv2 !== undefined && uvType ) {
  967. for ( i = 0; i < 4; i ++ ) {
  968. uv2i = uv2[ i ];
  969. uv2Array[ offset_uv2 ] = uv2i.u;
  970. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  971. offset_uv2 += 2;
  972. }
  973. }
  974. if ( dirtyElements ) {
  975. faceArray[ offset_face ] = vertexIndex;
  976. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  977. faceArray[ offset_face + 2 ] = vertexIndex + 3;
  978. faceArray[ offset_face + 3 ] = vertexIndex + 1;
  979. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  980. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  981. offset_face += 6;
  982. lineArray[ offset_line ] = vertexIndex;
  983. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  984. lineArray[ offset_line + 2 ] = vertexIndex;
  985. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  986. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  987. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  988. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  989. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  990. offset_line += 8;
  991. vertexIndex += 4;
  992. }
  993. }
  994. }
  995. if ( obj_edgeFaces ) {
  996. for ( f = 0, fl = obj_edgeFaces.length; f < fl; f ++ ) {
  997. faceArray[ offset_face ] = obj_edgeFaces[ f ].a;
  998. faceArray[ offset_face + 1 ] = obj_edgeFaces[ f ].b;
  999. faceArray[ offset_face + 2 ] = obj_edgeFaces[ f ].c;
  1000. faceArray[ offset_face + 3 ] = obj_edgeFaces[ f ].a;
  1001. faceArray[ offset_face + 4 ] = obj_edgeFaces[ f ].c;
  1002. faceArray[ offset_face + 5 ] = obj_edgeFaces[ f ].d;
  1003. offset_face += 6;
  1004. }
  1005. }
  1006. if ( dirtyVertices ) {
  1007. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1008. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  1009. }
  1010. if ( customAttributes ) {
  1011. for ( a in customAttributes ) {
  1012. customAttribute = customAttributes[ a ];
  1013. if ( customAttribute.needsUpdate ) {
  1014. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  1015. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  1016. customAttribute.needsUpdate = false;
  1017. }
  1018. }
  1019. }
  1020. if ( dirtyMorphTargets ) {
  1021. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  1022. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  1023. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  1024. }
  1025. }
  1026. if ( dirtyColors && offset_color > 0 ) {
  1027. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1028. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1029. }
  1030. if ( dirtyNormals ) {
  1031. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1032. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  1033. }
  1034. if ( dirtyTangents && geometry.hasTangents ) {
  1035. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1036. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  1037. }
  1038. if ( dirtyUvs && offset_uv > 0 ) {
  1039. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1040. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  1041. }
  1042. if ( dirtyUvs && offset_uv2 > 0 ) {
  1043. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1044. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  1045. }
  1046. if ( dirtyElements ) {
  1047. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1048. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  1049. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1050. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  1051. }
  1052. if ( offset_skin > 0 ) {
  1053. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  1054. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexAArray, hint );
  1055. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  1056. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexBArray, hint );
  1057. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1058. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  1059. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1060. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  1061. }
  1062. };
  1063. function setLineBuffers ( geometry, hint ) {
  1064. var v, c, vertex, offset,
  1065. vertices = geometry.vertices,
  1066. colors = geometry.colors,
  1067. vl = vertices.length,
  1068. cl = colors.length,
  1069. vertexArray = geometry.__vertexArray,
  1070. colorArray = geometry.__colorArray,
  1071. dirtyVertices = geometry.__dirtyVertices,
  1072. dirtyColors = geometry.__dirtyColors;
  1073. if ( dirtyVertices ) {
  1074. for ( v = 0; v < vl; v++ ) {
  1075. vertex = vertices[ v ].position;
  1076. offset = v * 3;
  1077. vertexArray[ offset ] = vertex.x;
  1078. vertexArray[ offset + 1 ] = vertex.y;
  1079. vertexArray[ offset + 2 ] = vertex.z;
  1080. }
  1081. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  1082. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  1083. }
  1084. if ( dirtyColors ) {
  1085. for ( c = 0; c < cl; c++ ) {
  1086. color = colors[ c ];
  1087. offset = c * 3;
  1088. colorArray[ offset ] = color.r;
  1089. colorArray[ offset + 1 ] = color.g;
  1090. colorArray[ offset + 2 ] = color.b;
  1091. }
  1092. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  1093. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1094. }
  1095. };
  1096. function setRibbonBuffers ( geometry, hint ) {
  1097. var v, c, vertex, offset,
  1098. vertices = geometry.vertices,
  1099. colors = geometry.colors,
  1100. vl = vertices.length,
  1101. cl = colors.length,
  1102. vertexArray = geometry.__vertexArray,
  1103. colorArray = geometry.__colorArray,
  1104. dirtyVertices = geometry.__dirtyVertices,
  1105. dirtyColors = geometry.__dirtyColors;
  1106. if ( dirtyVertices ) {
  1107. for ( v = 0; v < vl; v++ ) {
  1108. vertex = vertices[ v ].position;
  1109. offset = v * 3;
  1110. vertexArray[ offset ] = vertex.x;
  1111. vertexArray[ offset + 1 ] = vertex.y;
  1112. vertexArray[ offset + 2 ] = vertex.z;
  1113. }
  1114. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  1115. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  1116. }
  1117. if ( dirtyColors ) {
  1118. for ( c = 0; c < cl; c++ ) {
  1119. color = colors[ c ];
  1120. offset = c * 3;
  1121. colorArray[ offset ] = color.r;
  1122. colorArray[ offset + 1 ] = color.g;
  1123. colorArray[ offset + 2 ] = color.b;
  1124. }
  1125. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  1126. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1127. }
  1128. };
  1129. function setParticleBuffers ( geometry, hint, object ) {
  1130. var v, c, vertex, offset,
  1131. vertices = geometry.vertices,
  1132. vl = vertices.length,
  1133. colors = geometry.colors,
  1134. cl = colors.length,
  1135. vertexArray = geometry.__vertexArray,
  1136. colorArray = geometry.__colorArray,
  1137. sortArray = geometry.__sortArray,
  1138. dirtyVertices = geometry.__dirtyVertices,
  1139. dirtyElements = geometry.__dirtyElements,
  1140. dirtyColors = geometry.__dirtyColors;
  1141. if ( object.sortParticles ) {
  1142. _projScreenMatrix.multiplySelf( object.matrixWorld );
  1143. for ( v = 0; v < vl; v++ ) {
  1144. vertex = vertices[ v ].position;
  1145. _vector3.copy( vertex );
  1146. _projScreenMatrix.multiplyVector3( _vector3 );
  1147. sortArray[ v ] = [ _vector3.z, v ];
  1148. }
  1149. sortArray.sort( function(a,b) { return b[0] - a[0]; } );
  1150. for ( v = 0; v < vl; v++ ) {
  1151. vertex = vertices[ sortArray[v][1] ].position;
  1152. offset = v * 3;
  1153. vertexArray[ offset ] = vertex.x;
  1154. vertexArray[ offset + 1 ] = vertex.y;
  1155. vertexArray[ offset + 2 ] = vertex.z;
  1156. }
  1157. for ( c = 0; c < cl; c++ ) {
  1158. offset = c * 3;
  1159. color = colors[ sortArray[c][1] ];
  1160. colorArray[ offset ] = color.r;
  1161. colorArray[ offset + 1 ] = color.g;
  1162. colorArray[ offset + 2 ] = color.b;
  1163. }
  1164. } else {
  1165. if ( dirtyVertices ) {
  1166. for ( v = 0; v < vl; v++ ) {
  1167. vertex = vertices[ v ].position;
  1168. offset = v * 3;
  1169. vertexArray[ offset ] = vertex.x;
  1170. vertexArray[ offset + 1 ] = vertex.y;
  1171. vertexArray[ offset + 2 ] = vertex.z;
  1172. }
  1173. }
  1174. if ( dirtyColors ) {
  1175. for ( c = 0; c < cl; c++ ) {
  1176. color = colors[ c ];
  1177. offset = c * 3;
  1178. colorArray[ offset ] = color.r;
  1179. colorArray[ offset + 1 ] = color.g;
  1180. colorArray[ offset + 2 ] = color.b;
  1181. }
  1182. }
  1183. }
  1184. if ( dirtyVertices || object.sortParticles ) {
  1185. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  1186. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  1187. }
  1188. if ( dirtyColors || object.sortParticles ) {
  1189. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  1190. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  1191. }
  1192. };
  1193. function setMaterialShaders( material, shaders ) {
  1194. material.uniforms = Uniforms.clone( shaders.uniforms );
  1195. material.vertexShader = shaders.vertexShader;
  1196. material.fragmentShader = shaders.fragmentShader;
  1197. };
  1198. function refreshUniformsCommon( uniforms, material ) {
  1199. uniforms.diffuse.value.setRGB( material.color.r, material.color.g, material.color.b );
  1200. uniforms.opacity.value = material.opacity;
  1201. uniforms.map.texture = material.map;
  1202. uniforms.lightMap.texture = material.lightMap;
  1203. uniforms.envMap.texture = material.envMap;
  1204. uniforms.reflectivity.value = material.reflectivity;
  1205. uniforms.refractionRatio.value = material.refractionRatio;
  1206. uniforms.combine.value = material.combine;
  1207. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  1208. };
  1209. function refreshUniformsLine( uniforms, material ) {
  1210. uniforms.diffuse.value.setRGB( material.color.r, material.color.g, material.color.b );
  1211. uniforms.opacity.value = material.opacity;
  1212. };
  1213. function refreshUniformsParticle( uniforms, material ) {
  1214. uniforms.psColor.value.setRGB( material.color.r, material.color.g, material.color.b );
  1215. uniforms.opacity.value = material.opacity;
  1216. uniforms.size.value = material.size;
  1217. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  1218. uniforms.map.texture = material.map;
  1219. };
  1220. function refreshUniformsFog( uniforms, fog ) {
  1221. uniforms.fogColor.value.setHex( fog.color.hex );
  1222. if ( fog instanceof THREE.Fog ) {
  1223. uniforms.fogNear.value = fog.near;
  1224. uniforms.fogFar.value = fog.far;
  1225. } else if ( fog instanceof THREE.FogExp2 ) {
  1226. uniforms.fogDensity.value = fog.density;
  1227. }
  1228. };
  1229. function refreshUniformsPhong( uniforms, material ) {
  1230. //uniforms.ambient.value.setHex( material.ambient.hex );
  1231. //uniforms.specular.value.setHex( material.specular.hex );
  1232. uniforms.ambient.value.setRGB( material.ambient.r, material.ambient.g, material.ambient.b );
  1233. uniforms.specular.value.setRGB( material.specular.r, material.specular.g, material.specular.b );
  1234. uniforms.shininess.value = material.shininess;
  1235. };
  1236. function refreshUniformsLights( uniforms, lights ) {
  1237. uniforms.enableLighting.value = lights.directional.length + lights.point.length;
  1238. uniforms.ambientLightColor.value = lights.ambient;
  1239. uniforms.directionalLightColor.value = lights.directional.colors;
  1240. uniforms.directionalLightDirection.value = lights.directional.positions;
  1241. uniforms.pointLightColor.value = lights.point.colors;
  1242. uniforms.pointLightPosition.value = lights.point.positions;
  1243. uniforms.pointLightDistance.value = lights.point.distances;
  1244. };
  1245. this.initMaterial = function ( material, lights, fog, object ) {
  1246. var u, a, identifiers, i, parameters, maxLightCount, maxBones, shaderID;
  1247. if ( material instanceof THREE.MeshDepthMaterial ) {
  1248. shaderID = 'depth';
  1249. } else if ( material instanceof THREE.ShadowVolumeDynamicMaterial ) {
  1250. shaderID = 'shadowVolumeDynamic';
  1251. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  1252. shaderID = 'normal';
  1253. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  1254. shaderID = 'basic';
  1255. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  1256. shaderID = 'lambert';
  1257. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  1258. shaderID = 'phong';
  1259. } else if ( material instanceof THREE.LineBasicMaterial ) {
  1260. shaderID = 'basic';
  1261. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  1262. shaderID = 'particle_basic';
  1263. }
  1264. if ( shaderID ) {
  1265. setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
  1266. }
  1267. // heuristics to create shader parameters according to lights in the scene
  1268. // (not to blow over maxLights budget)
  1269. maxLightCount = allocateLights( lights, 4 );
  1270. maxBones = allocateBones( object );
  1271. parameters = {
  1272. map: !!material.map, envMap: !!material.envMap, lightMap: !!material.lightMap,
  1273. vertexColors: material.vertexColors,
  1274. fog: fog, sizeAttenuation: material.sizeAttenuation,
  1275. skinning: material.skinning,
  1276. morphTargets: material.morphTargets,
  1277. maxMorphTargets: this.maxMorphTargets,
  1278. maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point,
  1279. maxBones: maxBones
  1280. };
  1281. material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, parameters );
  1282. var attributes = material.program.attributes;
  1283. _gl.enableVertexAttribArray( attributes.position );
  1284. if ( attributes.color >= 0 ) _gl.enableVertexAttribArray( attributes.color );
  1285. if ( attributes.normal >= 0 ) _gl.enableVertexAttribArray( attributes.normal );
  1286. if ( attributes.tangent >= 0 ) _gl.enableVertexAttribArray( attributes.tangent );
  1287. if ( material.skinning &&
  1288. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  1289. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1290. _gl.enableVertexAttribArray( attributes.skinVertexA );
  1291. _gl.enableVertexAttribArray( attributes.skinVertexB );
  1292. _gl.enableVertexAttribArray( attributes.skinIndex );
  1293. _gl.enableVertexAttribArray( attributes.skinWeight );
  1294. }
  1295. for ( a in material.attributes ) {
  1296. if( attributes[ a ] >= 0 ) _gl.enableVertexAttribArray( attributes[ a ] );
  1297. }
  1298. if ( material.morphTargets ) {
  1299. material.numSupportedMorphTargets = 0;
  1300. if ( attributes.morphTarget0 >= 0 ) {
  1301. _gl.enableVertexAttribArray( attributes.morphTarget0 );
  1302. material.numSupportedMorphTargets ++;
  1303. }
  1304. if ( attributes.morphTarget1 >= 0 ) {
  1305. _gl.enableVertexAttribArray( attributes.morphTarget1 );
  1306. material.numSupportedMorphTargets ++;
  1307. }
  1308. if ( attributes.morphTarget2 >= 0 ) {
  1309. _gl.enableVertexAttribArray( attributes.morphTarget2 );
  1310. material.numSupportedMorphTargets ++;
  1311. }
  1312. if ( attributes.morphTarget3 >= 0 ) {
  1313. _gl.enableVertexAttribArray( attributes.morphTarget3 );
  1314. material.numSupportedMorphTargets ++;
  1315. }
  1316. if ( attributes.morphTarget4 >= 0 ) {
  1317. _gl.enableVertexAttribArray( attributes.morphTarget4 );
  1318. material.numSupportedMorphTargets ++;
  1319. }
  1320. if ( attributes.morphTarget5 >= 0 ) {
  1321. _gl.enableVertexAttribArray( attributes.morphTarget5 );
  1322. material.numSupportedMorphTargets ++;
  1323. }
  1324. if ( attributes.morphTarget6 >= 0 ) {
  1325. _gl.enableVertexAttribArray( attributes.morphTarget6 );
  1326. material.numSupportedMorphTargets ++;
  1327. }
  1328. if ( attributes.morphTarget7 >= 0 ) {
  1329. _gl.enableVertexAttribArray( attributes.morphTarget7 );
  1330. material.numSupportedMorphTargets ++;
  1331. }
  1332. object.__webglMorphTargetInfluences = new Float32Array( this.maxMorphTargets );
  1333. for ( var i = 0, il = this.maxMorphTargets; i < il; i ++ ) {
  1334. object.__webglMorphTargetInfluences[ i ] = 0;
  1335. }
  1336. }
  1337. };
  1338. function setProgram( camera, lights, fog, material, object ) {
  1339. if ( ! material.program ) {
  1340. _this.initMaterial( material, lights, fog, object );
  1341. }
  1342. var program = material.program,
  1343. p_uniforms = program.uniforms,
  1344. m_uniforms = material.uniforms;
  1345. if ( program != _currentProgram ) {
  1346. _gl.useProgram( program );
  1347. _currentProgram = program;
  1348. }
  1349. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, _projectionMatrixArray );
  1350. // refresh uniforms common to several materials
  1351. if ( fog && (
  1352. material instanceof THREE.MeshBasicMaterial ||
  1353. material instanceof THREE.MeshLambertMaterial ||
  1354. material instanceof THREE.MeshPhongMaterial ||
  1355. material instanceof THREE.LineBasicMaterial ||
  1356. material instanceof THREE.ParticleBasicMaterial ||
  1357. material.fog )
  1358. ) {
  1359. refreshUniformsFog( m_uniforms, fog );
  1360. }
  1361. if ( material instanceof THREE.MeshPhongMaterial ||
  1362. material instanceof THREE.MeshLambertMaterial ||
  1363. material.lights ) {
  1364. setupLights( program, lights );
  1365. refreshUniformsLights( m_uniforms, _lights );
  1366. }
  1367. if ( material instanceof THREE.MeshBasicMaterial ||
  1368. material instanceof THREE.MeshLambertMaterial ||
  1369. material instanceof THREE.MeshPhongMaterial ) {
  1370. refreshUniformsCommon( m_uniforms, material );
  1371. }
  1372. // refresh single material specific uniforms
  1373. if ( material instanceof THREE.LineBasicMaterial ) {
  1374. refreshUniformsLine( m_uniforms, material );
  1375. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  1376. refreshUniformsParticle( m_uniforms, material );
  1377. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  1378. refreshUniformsPhong( m_uniforms, material );
  1379. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  1380. m_uniforms.mNear.value = camera.near;
  1381. m_uniforms.mFar.value = camera.far;
  1382. m_uniforms.opacity.value = material.opacity;
  1383. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  1384. m_uniforms.opacity.value = material.opacity;
  1385. }
  1386. // load common uniforms
  1387. loadUniformsGeneric( program, m_uniforms );
  1388. loadUniformsMatrices( p_uniforms, object );
  1389. // load material specific uniforms
  1390. // (shader material also gets them for the sake of genericity)
  1391. if ( material instanceof THREE.MeshShaderMaterial ||
  1392. material instanceof THREE.MeshPhongMaterial ||
  1393. material.envMap ) {
  1394. _gl.uniform3f( p_uniforms.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  1395. }
  1396. if ( material instanceof THREE.MeshShaderMaterial ||
  1397. material.envMap ||
  1398. material.skinning ) {
  1399. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  1400. }
  1401. if ( material instanceof THREE.MeshPhongMaterial ||
  1402. material instanceof THREE.MeshLambertMaterial ||
  1403. material instanceof THREE.MeshShaderMaterial ||
  1404. material.skinning ) {
  1405. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
  1406. }
  1407. if ( material instanceof THREE.ShadowVolumeDynamicMaterial ) {
  1408. var dirLight = m_uniforms.directionalLightDirection.value;
  1409. dirLight[ 0 ] = -lights.position.x;
  1410. dirLight[ 1 ] = -lights.position.y;
  1411. dirLight[ 2 ] = -lights.position.z;
  1412. _gl.uniform3fv( p_uniforms.directionalLightDirection, dirLight );
  1413. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  1414. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
  1415. }
  1416. if ( material.skinning ) {
  1417. loadUniformsSkinning( p_uniforms, object );
  1418. }
  1419. return program;
  1420. };
  1421. function renderBuffer( camera, lights, fog, material, geometryGroup, object ) {
  1422. if ( material.opacity == 0 ) return;
  1423. var program, attributes, linewidth, primitives, a, attribute;
  1424. program = setProgram( camera, lights, fog, material, object );
  1425. attributes = program.attributes;
  1426. // vertices
  1427. if ( !material.morphTargets ) {
  1428. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1429. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1430. } else {
  1431. setupMorphTargets( material, geometryGroup, object );
  1432. }
  1433. // custom attributes
  1434. if ( geometryGroup.__webglCustomAttributes ) {
  1435. for( a in geometryGroup.__webglCustomAttributes ) {
  1436. if( attributes[ a ] >= 0 ) {
  1437. attribute = geometryGroup.__webglCustomAttributes[ a ];
  1438. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  1439. _gl.vertexAttribPointer( attributes[ a ], attribute.size, _gl.FLOAT, false, 0, 0 );
  1440. }
  1441. }
  1442. }
  1443. // colors
  1444. if ( attributes.color >= 0 ) {
  1445. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  1446. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1447. }
  1448. // normals
  1449. if ( attributes.normal >= 0 ) {
  1450. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1451. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1452. }
  1453. // tangents
  1454. if ( attributes.tangent >= 0 ) {
  1455. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  1456. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  1457. }
  1458. // uvs
  1459. if ( attributes.uv >= 0 ) {
  1460. if ( geometryGroup.__webglUVBuffer ) {
  1461. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  1462. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1463. _gl.enableVertexAttribArray( attributes.uv );
  1464. } else {
  1465. _gl.disableVertexAttribArray( attributes.uv );
  1466. }
  1467. }
  1468. if ( attributes.uv2 >= 0 ) {
  1469. if ( geometryGroup.__webglUV2Buffer ) {
  1470. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  1471. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  1472. _gl.enableVertexAttribArray( attributes.uv2 );
  1473. } else {
  1474. _gl.disableVertexAttribArray( attributes.uv2 );
  1475. }
  1476. }
  1477. if ( material.skinning &&
  1478. attributes.skinVertexA >= 0 && attributes.skinVertexB >= 0 &&
  1479. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1480. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  1481. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  1482. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  1483. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  1484. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  1485. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  1486. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  1487. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  1488. }
  1489. // render mesh
  1490. if ( object instanceof THREE.Mesh ) {
  1491. // wireframe
  1492. if ( material.wireframe ) {
  1493. _gl.lineWidth( material.wireframeLinewidth );
  1494. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  1495. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
  1496. // triangles
  1497. } else {
  1498. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1499. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  1500. }
  1501. // render lines
  1502. } else if ( object instanceof THREE.Line ) {
  1503. primitives = ( object.type == THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1504. _gl.lineWidth( material.linewidth );
  1505. _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
  1506. // render particles
  1507. } else if ( object instanceof THREE.ParticleSystem ) {
  1508. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  1509. // render ribbon
  1510. } else if ( object instanceof THREE.Ribbon ) {
  1511. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
  1512. }
  1513. };
  1514. function setupMorphTargets( material, geometryGroup, object ) {
  1515. // set base
  1516. var attributes = material.program.attributes;
  1517. if ( object.morphTargetBase !== - 1 ) {
  1518. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  1519. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1520. } else {
  1521. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1522. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1523. }
  1524. if ( object.morphTargetForcedOrder.length ) {
  1525. // set forced order
  1526. var m = 0;
  1527. var order = object.morphTargetForcedOrder;
  1528. var influences = object.morphTargetInfluences;
  1529. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  1530. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  1531. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1532. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ]];
  1533. m ++;
  1534. }
  1535. } else {
  1536. // find most influencing
  1537. var used = [];
  1538. var candidateInfluence = - 1;
  1539. var candidate = 0;
  1540. var influences = object.morphTargetInfluences;
  1541. var i, il = influences.length;
  1542. var m = 0;
  1543. if ( object.morphTargetBase !== - 1 ) {
  1544. used[ object.morphTargetBase ] = true;
  1545. }
  1546. while ( m < material.numSupportedMorphTargets ) {
  1547. for ( i = 0; i < il; i ++ ) {
  1548. if ( !used[ i ] && influences[ i ] > candidateInfluence ) {
  1549. candidate = i;
  1550. candidateInfluence = influences[ candidate ];
  1551. }
  1552. }
  1553. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ candidate ] );
  1554. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1555. object.__webglMorphTargetInfluences[ m ] = candidateInfluence;
  1556. used[ candidate ] = 1;
  1557. candidateInfluence = -1;
  1558. m ++;
  1559. }
  1560. }
  1561. // load updated influences uniform
  1562. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  1563. }
  1564. function renderBufferImmediate( object, program ) {
  1565. if ( ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  1566. if ( ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  1567. if ( object.hasPos ) {
  1568. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  1569. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  1570. _gl.enableVertexAttribArray( program.attributes.position );
  1571. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1572. }
  1573. if ( object.hasNormal ) {
  1574. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  1575. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  1576. _gl.enableVertexAttribArray( program.attributes.normal );
  1577. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1578. }
  1579. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  1580. object.count = 0;
  1581. };
  1582. function setObjectFaces( object ) {
  1583. if ( _oldDoubleSided != object.doubleSided ) {
  1584. if( object.doubleSided ) {
  1585. _gl.disable( _gl.CULL_FACE );
  1586. } else {
  1587. _gl.enable( _gl.CULL_FACE );
  1588. }
  1589. _oldDoubleSided = object.doubleSided;
  1590. }
  1591. if ( _oldFlipSided != object.flipSided ) {
  1592. if( object.flipSided ) {
  1593. _gl.frontFace( _gl.CW );
  1594. } else {
  1595. _gl.frontFace( _gl.CCW );
  1596. }
  1597. _oldFlipSided = object.flipSided;
  1598. }
  1599. };
  1600. function setDepthTest( test ) {
  1601. if ( _oldDepth != test ) {
  1602. if( test ) {
  1603. _gl.enable( _gl.DEPTH_TEST );
  1604. } else {
  1605. _gl.disable( _gl.DEPTH_TEST );
  1606. }
  1607. _oldDepth = test;
  1608. }
  1609. };
  1610. function computeFrustum( m ) {
  1611. _frustum[ 0 ].set( m.n41 - m.n11, m.n42 - m.n12, m.n43 - m.n13, m.n44 - m.n14 );
  1612. _frustum[ 1 ].set( m.n41 + m.n11, m.n42 + m.n12, m.n43 + m.n13, m.n44 + m.n14 );
  1613. _frustum[ 2 ].set( m.n41 + m.n21, m.n42 + m.n22, m.n43 + m.n23, m.n44 + m.n24 );
  1614. _frustum[ 3 ].set( m.n41 - m.n21, m.n42 - m.n22, m.n43 - m.n23, m.n44 - m.n24 );
  1615. _frustum[ 4 ].set( m.n41 - m.n31, m.n42 - m.n32, m.n43 - m.n33, m.n44 - m.n34 );
  1616. _frustum[ 5 ].set( m.n41 + m.n31, m.n42 + m.n32, m.n43 + m.n33, m.n44 + m.n34 );
  1617. var i, plane;
  1618. for ( i = 0; i < 6; i ++ ) {
  1619. plane = _frustum[ i ];
  1620. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  1621. }
  1622. };
  1623. function isInFrustum( object ) {
  1624. var distance, matrix = object.matrixWorld,
  1625. radius = - object.geometry.boundingSphere.radius * Math.max( object.scale.x, Math.max( object.scale.y, object.scale.z ) );
  1626. for ( var i = 0; i < 6; i ++ ) {
  1627. distance = _frustum[ i ].x * matrix.n14 + _frustum[ i ].y * matrix.n24 + _frustum[ i ].z * matrix.n34 + _frustum[ i ].w;
  1628. if ( distance <= radius ) return false;
  1629. }
  1630. return true;
  1631. };
  1632. function addToFixedArray( where, what ) {
  1633. where.list[ where.count ] = what;
  1634. where.count += 1;
  1635. };
  1636. function unrollImmediateBufferMaterials( globject ) {
  1637. var i, l, m, ml, material,
  1638. object = globject.object,
  1639. opaque = globject.opaque,
  1640. transparent = globject.transparent;
  1641. transparent.count = 0;
  1642. opaque.count = 0;
  1643. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1644. material = object.materials[ m ];
  1645. material.transparent ? addToFixedArray( transparent, material ) : addToFixedArray( opaque, material );
  1646. }
  1647. };
  1648. function unrollBufferMaterials( globject ) {
  1649. var i, l, m, ml, material, meshMaterial,
  1650. object = globject.object,
  1651. buffer = globject.buffer,
  1652. opaque = globject.opaque,
  1653. transparent = globject.transparent;
  1654. transparent.count = 0;
  1655. opaque.count = 0;
  1656. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1657. meshMaterial = object.materials[ m ];
  1658. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  1659. for ( i = 0, l = buffer.materials.length; i < l; i++ ) {
  1660. material = buffer.materials[ i ];
  1661. if ( material ) material.transparent ? addToFixedArray( transparent, material ) : addToFixedArray( opaque, material );
  1662. }
  1663. } else {
  1664. material = meshMaterial;
  1665. if ( material ) material.transparent ? addToFixedArray( transparent, material ) : addToFixedArray( opaque, material );
  1666. }
  1667. }
  1668. };
  1669. function painterSort( a, b ) {
  1670. return b.z - a.z;
  1671. };
  1672. this.render = function( scene, camera, renderTarget, forceClear ) {
  1673. var i, program, opaque, transparent, material,
  1674. o, ol, oil, webglObject, object, buffer,
  1675. lights = scene.lights,
  1676. fog = scene.fog;
  1677. camera.matrixAutoUpdate && camera.updateMatrix();
  1678. scene.update( undefined, false, camera );
  1679. camera.matrixWorldInverse.flattenToArray( _viewMatrixArray );
  1680. camera.projectionMatrix.flattenToArray( _projectionMatrixArray );
  1681. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1682. computeFrustum( _projScreenMatrix );
  1683. this.initWebGLObjects( scene );
  1684. setRenderTarget( renderTarget );
  1685. if ( this.autoClear || forceClear ) {
  1686. this.clear();
  1687. }
  1688. // set matrices
  1689. ol = scene.__webglObjects.length;
  1690. for ( o = 0; o < ol; o++ ) {
  1691. webglObject = scene.__webglObjects[ o ];
  1692. object = webglObject.object;
  1693. if ( object.visible ) {
  1694. if ( ! ( object instanceof THREE.Mesh ) || isInFrustum( object ) ) {
  1695. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1696. setupMatrices( object, camera );
  1697. unrollBufferMaterials( webglObject );
  1698. webglObject.render = true;
  1699. if ( this.sortObjects ) {
  1700. _vector3.copy( object.position );
  1701. _projScreenMatrix.multiplyVector3( _vector3 );
  1702. webglObject.z = _vector3.z;
  1703. }
  1704. } else {
  1705. webglObject.render = false;
  1706. }
  1707. } else {
  1708. webglObject.render = false;
  1709. }
  1710. }
  1711. if ( this.sortObjects ) {
  1712. scene.__webglObjects.sort( painterSort );
  1713. }
  1714. oil = scene.__webglObjectsImmediate.length;
  1715. for ( o = 0; o < oil; o++ ) {
  1716. webglObject = scene.__webglObjectsImmediate[ o ];
  1717. object = webglObject.object;
  1718. if ( object.visible ) {
  1719. if( object.matrixAutoUpdate ) {
  1720. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1721. }
  1722. setupMatrices( object, camera );
  1723. unrollImmediateBufferMaterials( webglObject );
  1724. }
  1725. }
  1726. // opaque pass
  1727. setBlending( THREE.NormalBlending );
  1728. for ( o = 0; o < ol; o ++ ) {
  1729. webglObject = scene.__webglObjects[ o ];
  1730. if ( webglObject.render ) {
  1731. object = webglObject.object;
  1732. buffer = webglObject.buffer;
  1733. opaque = webglObject.opaque;
  1734. setObjectFaces( object );
  1735. for ( i = 0; i < opaque.count; i ++ ) {
  1736. material = opaque.list[ i ];
  1737. setDepthTest( material.depthTest );
  1738. renderBuffer( camera, lights, fog, material, buffer, object );
  1739. }
  1740. }
  1741. }
  1742. // opaque pass (immediate simulator)
  1743. for ( o = 0; o < oil; o++ ) {
  1744. webglObject = scene.__webglObjectsImmediate[ o ];
  1745. object = webglObject.object;
  1746. if ( object.visible ) {
  1747. opaque = webglObject.opaque;
  1748. setObjectFaces( object );
  1749. for( i = 0; i < opaque.count; i++ ) {
  1750. material = opaque.list[ i ];
  1751. setDepthTest( material.depthTest );
  1752. program = setProgram( camera, lights, fog, material, object );
  1753. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1754. }
  1755. }
  1756. }
  1757. // transparent pass
  1758. for ( o = 0; o < ol; o ++ ) {
  1759. webglObject = scene.__webglObjects[ o ];
  1760. if ( webglObject.render ) {
  1761. object = webglObject.object;
  1762. buffer = webglObject.buffer;
  1763. transparent = webglObject.transparent;
  1764. setObjectFaces( object );
  1765. for ( i = 0; i < transparent.count; i ++ ) {
  1766. material = transparent.list[ i ];
  1767. setBlending( material.blending );
  1768. setDepthTest( material.depthTest );
  1769. renderBuffer( camera, lights, fog, material, buffer, object );
  1770. }
  1771. }
  1772. }
  1773. // transparent pass (immediate simulator)
  1774. for ( o = 0; o < oil; o++ ) {
  1775. webglObject = scene.__webglObjectsImmediate[ o ];
  1776. object = webglObject.object;
  1777. if ( object.visible ) {
  1778. transparent = webglObject.transparent;
  1779. setObjectFaces( object );
  1780. for ( i = 0; i < transparent.count; i ++ ) {
  1781. material = transparent.list[ i ];
  1782. setBlending( material.blending );
  1783. setDepthTest( material.depthTest );
  1784. program = setProgram( camera, lights, fog, material, object );
  1785. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1786. }
  1787. }
  1788. }
  1789. // render stencil shadows
  1790. if ( stencil && scene.__webglShadowVolumes.length && scene.lights.length ) {
  1791. renderStencilShadows( scene );
  1792. }
  1793. // render lens flares
  1794. if ( scene.__webglLensFlares.length ) {
  1795. renderLensFlares( scene, camera );
  1796. }
  1797. // Generate mipmap if we're using any kind of mipmap filtering
  1798. if ( renderTarget && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  1799. updateRenderTargetMipmap( renderTarget );
  1800. }
  1801. };
  1802. /*
  1803. * Stencil Shadows
  1804. * method: we're rendering the world in light, then the shadow
  1805. * volumes into the stencil and last a big darkening
  1806. * quad over the whole thing. This is not how "you're
  1807. * supposed to" do stencil shadows but is much faster
  1808. *
  1809. */
  1810. function renderStencilShadows( scene ) {
  1811. // setup stencil
  1812. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  1813. _gl.polygonOffset( 0.1, 1.0 );
  1814. _gl.enable( _gl.STENCIL_TEST );
  1815. _gl.depthMask( false );
  1816. _gl.colorMask( false, false, false, false );
  1817. _gl.stencilFunc( _gl.ALWAYS, 1, 0xFF );
  1818. _gl.stencilOpSeparate( _gl.BACK, _gl.KEEP, _gl.INCR, _gl.KEEP );
  1819. _gl.stencilOpSeparate( _gl.FRONT, _gl.KEEP, _gl.DECR, _gl.KEEP );
  1820. // loop through all directional lights
  1821. var l, ll = scene.lights.length;
  1822. var p;
  1823. var light, lights = scene.lights;
  1824. var dirLight = [];
  1825. var object, geometryGroup, material;
  1826. var program;
  1827. var p_uniforms;
  1828. var m_uniforms;
  1829. var attributes;
  1830. var o, ol = scene.__webglShadowVolumes.length;
  1831. for ( l = 0; l < ll; l++ ) {
  1832. light = scene.lights[ l ];
  1833. if ( light instanceof THREE.DirectionalLight ) {
  1834. dirLight[ 0 ] = -light.position.x;
  1835. dirLight[ 1 ] = -light.position.y;
  1836. dirLight[ 2 ] = -light.position.z;
  1837. // render all volumes
  1838. for ( o = 0; o < ol; o++ ) {
  1839. object = scene.__webglShadowVolumes[ o ].object;
  1840. geometryGroup = scene.__webglShadowVolumes[ o ].buffer;
  1841. material = object.materials[ 0 ];
  1842. if ( !material.program ) _this.initMaterial( material, lights, undefined, object );
  1843. program = material.program,
  1844. p_uniforms = program.uniforms,
  1845. m_uniforms = material.uniforms,
  1846. attributes = program.attributes;
  1847. if ( _currentProgram !== program ) {
  1848. _gl.useProgram( program );
  1849. _currentProgram = program;
  1850. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, _projectionMatrixArray );
  1851. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
  1852. _gl.uniform3fv( p_uniforms.directionalLightDirection, dirLight );
  1853. }
  1854. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1855. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  1856. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  1857. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1858. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  1859. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1860. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  1861. _gl.cullFace( _gl.FRONT );
  1862. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  1863. _gl.cullFace( _gl.BACK );
  1864. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  1865. }
  1866. }
  1867. }
  1868. // setup color+stencil
  1869. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  1870. _gl.colorMask( true, true, true, true );
  1871. _gl.stencilFunc( _gl.NOTEQUAL, 0, 0xFF );
  1872. _gl.stencilOp( _gl.KEEP, _gl.KEEP, _gl.KEEP );
  1873. _gl.disable( _gl.DEPTH_TEST );
  1874. // draw darkening polygon
  1875. _oldBlending = "";
  1876. _currentProgram = _stencilShadow.program;
  1877. _gl.useProgram( _stencilShadow.program );
  1878. _gl.uniformMatrix4fv( _stencilShadow.projectionLocation, false, _projectionMatrixArray );
  1879. _gl.uniform1f( _stencilShadow.darknessLocation, _stencilShadow.darkness );
  1880. _gl.bindBuffer( _gl.ARRAY_BUFFER, _stencilShadow.vertexBuffer );
  1881. _gl.vertexAttribPointer( _stencilShadow.vertexLocation, 3, _gl.FLOAT, false, 0, 0 );
  1882. _gl.enableVertexAttribArray( _stencilShadow.vertexLocation );
  1883. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  1884. _gl.blendEquation( _gl.FUNC_ADD );
  1885. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _stencilShadow.elementBuffer );
  1886. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  1887. // disable stencil
  1888. _gl.disable( _gl.STENCIL_TEST );
  1889. _gl.enable( _gl.DEPTH_TEST );
  1890. _gl.depthMask( _currentDepthMask );
  1891. }
  1892. /*
  1893. * Render lens flares
  1894. * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
  1895. * reads these back and calculates occlusion.
  1896. * Then LensFlare.updateLensFlares() is called to re-position and
  1897. * update transparency of flares. Then they are rendered.
  1898. *
  1899. */
  1900. function renderLensFlares( scene, camera ) {
  1901. var object, objectZ, geometryGroup, material;
  1902. var o, ol = scene.__webglLensFlares.length;
  1903. var f, fl, flare;
  1904. var tempPosition = new THREE.Vector3();
  1905. var invAspect = _viewportHeight / _viewportWidth;
  1906. var halfViewportWidth = _viewportWidth * 0.5;
  1907. var halfViewportHeight = _viewportHeight * 0.5;
  1908. var size = 16 / _viewportHeight;
  1909. var scale = [ size * invAspect, size ];
  1910. var screenPosition = [ 1, 1, 0 ];
  1911. var screenPositionPixels = [ 1, 1 ];
  1912. var sampleX, sampleY, readBackPixels = _lensFlare.readBackPixels;
  1913. var sampleMidX = 7 * 4;
  1914. var sampleMidY = 7 * 16 * 4;
  1915. var sampleIndex, visibility;
  1916. var uniforms = _lensFlare.uniforms;
  1917. var attributes = _lensFlare.attributes;
  1918. // set lensflare program and reset blending
  1919. _gl.useProgram( _lensFlare.program );
  1920. _currentProgram = _lensFlare.program;
  1921. _oldBlending = "";
  1922. // loop through all lens flares to update their occlusion and positions
  1923. // setup gl and common used attribs/unforms
  1924. _gl.uniform1i( uniforms.map, 0 );
  1925. _gl.activeTexture( _gl.TEXTURE0 );
  1926. _gl.uniform1f( uniforms.opacity, 1 );
  1927. _gl.uniform1f( uniforms.rotation, 0 );
  1928. _gl.uniform2fv( uniforms.scale, scale );
  1929. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  1930. _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
  1931. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  1932. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  1933. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  1934. _gl.disable( _gl.CULL_FACE );
  1935. _gl.depthMask( false );
  1936. for ( o = 0; o < ol; o ++ ) {
  1937. // calc object screen position
  1938. object = scene.__webglLensFlares[ o ].object;
  1939. tempPosition.set( object.matrixWorld.n14, object.matrixWorld.n24, object.matrixWorld.n34 );
  1940. camera.matrixWorldInverse.multiplyVector3( tempPosition );
  1941. objectZ = tempPosition.z;
  1942. camera.projectionMatrix.multiplyVector3( tempPosition );
  1943. // setup arrays for gl programs
  1944. screenPosition[ 0 ] = tempPosition.x;
  1945. screenPosition[ 1 ] = tempPosition.y;
  1946. screenPosition[ 2 ] = tempPosition.z;
  1947. screenPositionPixels[ 0 ] = screenPosition[ 0 ] * halfViewportWidth + halfViewportWidth;
  1948. screenPositionPixels[ 1 ] = screenPosition[ 1 ] * halfViewportHeight + halfViewportHeight;
  1949. // save current RGB to temp texture
  1950. _gl.copyTexSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, screenPositionPixels[ 0 ] - 8, screenPositionPixels[ 1 ] - 8, 16, 16 );
  1951. // render pink quad
  1952. _gl.uniform3fv( uniforms.screenPosition, screenPosition );
  1953. _gl.uniform1i( uniforms.renderPink, 1 );
  1954. _gl.enable( _gl.DEPTH_TEST );
  1955. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  1956. // read back
  1957. try {
  1958. _gl.readPixels( screenPositionPixels[ 0 ] - 8, screenPositionPixels[ 1 ] - 8, 16, 16, _gl.RGBA, _gl.UNSIGNED_BYTE, _lensFlare.readBackPixels );
  1959. } catch( error ) {
  1960. console.log( "WebGLRenderer.renderLensFlare: readPixels failed!" );
  1961. }
  1962. if ( _gl.getError() ) {
  1963. console.log( "WebGLRenderer.renderLensFlare: readPixels failed!" );
  1964. }
  1965. // sample read back pixels
  1966. sampleDistance = parseInt( 5 * ( 1 - Math.max( 0, Math.min( -objectZ, camera.far )) / camera.far ), 10 ) + 2;
  1967. sampleX = sampleDistance * 4;
  1968. sampleY = sampleDistance * 4 * 16;
  1969. visibility = 0;
  1970. sampleIndex = ( sampleMidX - sampleX ) + ( sampleMidY - sampleY ); // upper left
  1971. if( _lensFlare.readBackPixels[ sampleIndex + 0 ] === 255 &&
  1972. _lensFlare.readBackPixels[ sampleIndex + 1 ] === 0 &&
  1973. _lensFlare.readBackPixels[ sampleIndex + 2 ] === 255 ) visibility += 0.2;
  1974. sampleIndex = ( sampleMidX + sampleX ) + ( sampleMidY - sampleY ); // upper right
  1975. if( readBackPixels[ sampleIndex + 0 ] === 255 &&
  1976. readBackPixels[ sampleIndex + 1 ] === 0 &&
  1977. readBackPixels[ sampleIndex + 2 ] === 255 ) visibility += 0.2;
  1978. sampleIndex = ( sampleMidX + sampleX ) + ( sampleMidY + sampleY ); // lower right
  1979. if( readBackPixels[ sampleIndex + 0 ] === 255 &&
  1980. readBackPixels[ sampleIndex + 1 ] === 0 &&
  1981. readBackPixels[ sampleIndex + 2 ] === 255 ) visibility += 0.2;
  1982. sampleIndex = ( sampleMidX - sampleX ) + ( sampleMidY + sampleY ); // lower left
  1983. if( readBackPixels[ sampleIndex + 0 ] === 255 &&
  1984. readBackPixels[ sampleIndex + 1 ] === 0 &&
  1985. readBackPixels[ sampleIndex + 2 ] === 255 ) visibility += 0.2;
  1986. sampleIndex = sampleMidX + sampleMidY; // center
  1987. if( readBackPixels[ sampleIndex + 0 ] === 255 &&
  1988. readBackPixels[ sampleIndex + 1 ] === 0 &&
  1989. readBackPixels[ sampleIndex + 2 ] === 255 ) visibility += 0.2;
  1990. object.positionScreen.x = screenPosition[ 0 ];
  1991. object.positionScreen.y = screenPosition[ 1 ];
  1992. object.positionScreen.z = screenPosition[ 2 ];
  1993. if ( object.customUpdateCallback ) {
  1994. object.customUpdateCallback( visibility, object );
  1995. } else {
  1996. object.updateLensFlares( visibility );
  1997. }
  1998. // restore graphics
  1999. _gl.uniform1i( uniforms.renderPink, 0 );
  2000. _gl.disable( _gl.DEPTH_TEST );
  2001. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  2002. }
  2003. // loop through all lens flares and draw their flares
  2004. // setup gl
  2005. for ( o = 0; o < ol; o ++ ) {
  2006. object = scene.__webglLensFlares[ o ].object;
  2007. for ( f = 0, fl = object.lensFlares.length; f < fl; f ++ ) {
  2008. flare = object.lensFlares[ f ];
  2009. if ( flare.opacity > 0.001 && flare.scale > 0.001 ) {
  2010. screenPosition[ 0 ] = flare.x;
  2011. screenPosition[ 1 ] = flare.y;
  2012. screenPosition[ 2 ] = flare.z;
  2013. size = flare.size * flare.scale / _viewportHeight;
  2014. scale[ 0 ] = size * invAspect;
  2015. scale[ 1 ] = size;
  2016. _gl.uniform3fv( uniforms.screenPosition, screenPosition );
  2017. _gl.uniform1f( uniforms.rotation, flare.rotation );
  2018. _gl.uniform2fv( uniforms.scale, scale );
  2019. _gl.uniform1f( uniforms.opacity, flare.opacity );
  2020. setBlending( flare.blending );
  2021. setTexture( flare.texture, 0 );
  2022. // todo: only draw if loaded
  2023. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  2024. }
  2025. }
  2026. }
  2027. // restore gl
  2028. _gl.enable( _gl.CULL_FACE );
  2029. _gl.enable( _gl.DEPTH_TEST );
  2030. _gl.depthMask( _currentDepthMask );
  2031. }
  2032. function setupMatrices( object, camera ) {
  2033. object._modelViewMatrix.multiplyToArray( camera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
  2034. THREE.Matrix4.makeInvert3x3( object._modelViewMatrix ).transposeIntoArray( object._normalMatrixArray );
  2035. }
  2036. this.initWebGLObjects = function ( scene ) {
  2037. if ( !scene.__webglObjects ) {
  2038. scene.__webglObjects = [];
  2039. scene.__webglObjectsImmediate = [];
  2040. scene.__webglShadowVolumes = [];
  2041. scene.__webglLensFlares = [];
  2042. }
  2043. while ( scene.__objectsAdded.length ) {
  2044. addObject( scene.__objectsAdded[ 0 ], scene );
  2045. scene.__objectsAdded.splice( 0, 1 );
  2046. }
  2047. while ( scene.__objectsRemoved.length ) {
  2048. removeObject( scene.__objectsRemoved[ 0 ], scene );
  2049. scene.__objectsRemoved.splice( 0, 1 );
  2050. }
  2051. // update must be called after objects adding / removal
  2052. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  2053. updateObject( scene.__webglObjects[ o ].object, scene );
  2054. }
  2055. for ( var o = 0, ol = scene.__webglShadowVolumes.length; o < ol; o ++ ) {
  2056. updateObject( scene.__webglShadowVolumes[ o ].object, scene );
  2057. }
  2058. for ( var o = 0, ol = scene.__webglLensFlares.length; o < ol; o ++ ) {
  2059. updateObject( scene.__webglLensFlares[ o ].object, scene );
  2060. }
  2061. };
  2062. function addObject( object, scene ) {
  2063. var g, geometry, geometryGroup;
  2064. if ( object._modelViewMatrix == undefined ) {
  2065. object._modelViewMatrix = new THREE.Matrix4();
  2066. object._normalMatrixArray = new Float32Array( 9 );
  2067. object._modelViewMatrixArray = new Float32Array( 16 );
  2068. object._objectMatrixArray = new Float32Array( 16 );
  2069. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  2070. }
  2071. if ( object instanceof THREE.Mesh ) {
  2072. geometry = object.geometry;
  2073. if ( geometry.geometryGroups == undefined ) {
  2074. sortFacesByMaterial( geometry );
  2075. }
  2076. // create separate VBOs per geometry chunk
  2077. for ( g in geometry.geometryGroups ) {
  2078. geometryGroup = geometry.geometryGroups[ g ];
  2079. // initialise VBO on the first access
  2080. if ( ! geometryGroup.__webglVertexBuffer ) {
  2081. createMeshBuffers( geometryGroup );
  2082. initMeshBuffers( geometryGroup, object );
  2083. geometry.__dirtyVertices = true;
  2084. geometry.__dirtyMorphTargets = true;
  2085. geometry.__dirtyElements = true;
  2086. geometry.__dirtyUvs = true;
  2087. geometry.__dirtyNormals = true;
  2088. geometry.__dirtyTangents = true;
  2089. geometry.__dirtyColors = true;
  2090. }
  2091. // create separate wrapper per each use of VBO
  2092. if ( object instanceof THREE.ShadowVolume ) {
  2093. addBuffer( scene.__webglShadowVolumes, geometryGroup, object );
  2094. } else {
  2095. addBuffer( scene.__webglObjects, geometryGroup, object );
  2096. }
  2097. }
  2098. } else if ( object instanceof THREE.LensFlare ) {
  2099. addBuffer( scene.__webglLensFlares, undefined, object );
  2100. } else if ( object instanceof THREE.Ribbon ) {
  2101. geometry = object.geometry;
  2102. if( ! geometry.__webglVertexBuffer ) {
  2103. createRibbonBuffers( geometry );
  2104. initRibbonBuffers( geometry );
  2105. geometry.__dirtyVertices = true;
  2106. geometry.__dirtyColors = true;
  2107. }
  2108. addBuffer( scene.__webglObjects, geometry, object );
  2109. } else if ( object instanceof THREE.Line ) {
  2110. geometry = object.geometry;
  2111. if( ! geometry.__webglVertexBuffer ) {
  2112. createLineBuffers( geometry );
  2113. initLineBuffers( geometry );
  2114. geometry.__dirtyVertices = true;
  2115. geometry.__dirtyColors = true;
  2116. }
  2117. addBuffer( scene.__webglObjects, geometry, object );
  2118. } else if ( object instanceof THREE.ParticleSystem ) {
  2119. geometry = object.geometry;
  2120. if ( ! geometry.__webglVertexBuffer ) {
  2121. createParticleBuffers( geometry );
  2122. initParticleBuffers( geometry );
  2123. geometry.__dirtyVertices = true;
  2124. geometry.__dirtyColors = true;
  2125. }
  2126. addBuffer( scene.__webglObjects, geometry, object );
  2127. } else if ( THREE.MarchingCubes !== undefined && object instanceof THREE.MarchingCubes ) {
  2128. addBufferImmediate( scene.__webglObjectsImmediate, object );
  2129. }/*else if ( object instanceof THREE.Particle ) {
  2130. }*/
  2131. };
  2132. function updateObject( object, scene ) {
  2133. var g, geometry, geometryGroup, a, customAttributeDirty;
  2134. if ( object instanceof THREE.Mesh ) {
  2135. geometry = object.geometry;
  2136. // check all geometry groups
  2137. for ( g in geometry.geometryGroups ) {
  2138. geometryGroup = geometry.geometryGroups[ g ];
  2139. customAttributeDirty = false;
  2140. for ( a in geometryGroup.__webglCustomAttributes ) {
  2141. if( geometryGroup.__webglCustomAttributes[ a ].needsUpdate ) {
  2142. customAttributeDirty = true;
  2143. break;
  2144. }
  2145. }
  2146. if ( geometry.__dirtyVertices || geometry.__dirtyMorphTargets || geometry.__dirtyElements ||
  2147. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  2148. geometry.__dirtyColors || geometry.__dirtyTangents || customAttributeDirty ) {
  2149. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW );
  2150. }
  2151. }
  2152. geometry.__dirtyVertices = false;
  2153. geometry.__dirtyMorphTargets = false;
  2154. geometry.__dirtyElements = false;
  2155. geometry.__dirtyUvs = false;
  2156. geometry.__dirtyNormals = false;
  2157. geometry.__dirtyTangents = false;
  2158. geometry.__dirtyColors = false;
  2159. } else if ( object instanceof THREE.Ribbon ) {
  2160. geometry = object.geometry;
  2161. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  2162. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  2163. }
  2164. geometry.__dirtyVertices = false;
  2165. geometry.__dirtyColors = false;
  2166. } else if ( object instanceof THREE.Line ) {
  2167. geometry = object.geometry;
  2168. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  2169. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  2170. }
  2171. geometry.__dirtyVertices = false;
  2172. geometry.__dirtyColors = false;
  2173. } else if ( object instanceof THREE.ParticleSystem ) {
  2174. geometry = object.geometry;
  2175. if ( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles ) {
  2176. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  2177. }
  2178. geometry.__dirtyVertices = false;
  2179. geometry.__dirtyColors = false;
  2180. }/* else if ( THREE.MarchingCubes !== undefined && object instanceof THREE.MarchingCubes ) {
  2181. // it updates itself in render callback
  2182. } else if ( object instanceof THREE.Particle ) {
  2183. }*/
  2184. };
  2185. function removeObject( object, scene ) {
  2186. var o, ol, zobject;
  2187. for ( o = scene.__webglObjects.length - 1; o >= 0; o-- ) {
  2188. zobject = scene.__webglObjects[ o ].object;
  2189. if ( object == zobject ) {
  2190. scene.__webglObjects.splice( o, 1 );
  2191. }
  2192. }
  2193. };
  2194. function sortFacesByMaterial( geometry ) {
  2195. // TODO
  2196. // Should optimize by grouping faces with ColorFill / ColorStroke materials
  2197. // which could then use vertex color attributes instead of each being
  2198. // in its separate VBO
  2199. var i, l, f, fl, face, material, materials, vertices, mhash, ghash, hash_map = {};
  2200. var numMorphTargets = geometry.morphTargets !== undefined ? geometry.morphTargets.length : 0;
  2201. geometry.geometryGroups = {};
  2202. function materialHash( material ) {
  2203. var hash_array = [];
  2204. for ( i = 0, l = material.length; i < l; i++ ) {
  2205. if ( material[ i ] == undefined ) {
  2206. hash_array.push( "undefined" );
  2207. } else {
  2208. hash_array.push( material[ i ].id );
  2209. }
  2210. }
  2211. return hash_array.join( '_' );
  2212. }
  2213. for ( f = 0, fl = geometry.faces.length; f < fl; f++ ) {
  2214. face = geometry.faces[ f ];
  2215. materials = face.materials;
  2216. mhash = materialHash( materials );
  2217. if ( hash_map[ mhash ] == undefined ) {
  2218. hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
  2219. }
  2220. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  2221. if ( geometry.geometryGroups[ ghash ] == undefined ) {
  2222. geometry.geometryGroups[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0, 'numMorphTargets': numMorphTargets };
  2223. }
  2224. vertices = face instanceof THREE.Face3 ? 3 : 4;
  2225. if ( geometry.geometryGroups[ ghash ].vertices + vertices > 65535 ) {
  2226. hash_map[ mhash ].counter += 1;
  2227. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  2228. if ( geometry.geometryGroups[ ghash ] == undefined ) {
  2229. geometry.geometryGroups[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0, 'numMorphTargets': numMorphTargets };
  2230. }
  2231. }
  2232. geometry.geometryGroups[ ghash ].faces.push( f );
  2233. geometry.geometryGroups[ ghash ].vertices += vertices;
  2234. }
  2235. };
  2236. function addBuffer( objlist, buffer, object ) {
  2237. objlist.push( {
  2238. buffer: buffer, object: object,
  2239. opaque: { list: [], count: 0 },
  2240. transparent: { list: [], count: 0 }
  2241. } );
  2242. };
  2243. function addBufferImmediate( objlist, object ) {
  2244. objlist.push( {
  2245. object: object,
  2246. opaque: { list: [], count: 0 },
  2247. transparent: { list: [], count: 0 }
  2248. } );
  2249. };
  2250. this.setFaceCulling = function ( cullFace, frontFace ) {
  2251. if ( cullFace ) {
  2252. if ( !frontFace || frontFace == "ccw" ) {
  2253. _gl.frontFace( _gl.CCW );
  2254. } else {
  2255. _gl.frontFace( _gl.CW );
  2256. }
  2257. if( cullFace == "back" ) {
  2258. _gl.cullFace( _gl.BACK );
  2259. } else if( cullFace == "front" ) {
  2260. _gl.cullFace( _gl.FRONT );
  2261. } else {
  2262. _gl.cullFace( _gl.FRONT_AND_BACK );
  2263. }
  2264. _gl.enable( _gl.CULL_FACE );
  2265. } else {
  2266. _gl.disable( _gl.CULL_FACE );
  2267. }
  2268. };
  2269. this.supportsVertexTextures = function () {
  2270. return maxVertexTextures() > 0;
  2271. };
  2272. function maxVertexTextures() {
  2273. return _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  2274. };
  2275. function initGL( antialias, clearColor, clearAlpha, stencil ) {
  2276. try {
  2277. if ( ! ( _gl = _canvas.getContext( 'experimental-webgl', { antialias: antialias, stencil: stencil } ) ) ) {
  2278. throw 'Error creating WebGL context.';
  2279. }
  2280. } catch ( e ) {
  2281. console.error( e );
  2282. }
  2283. _gl.clearColor( 0, 0, 0, 1 );
  2284. _gl.clearDepth( 1 );
  2285. _gl.enable( _gl.DEPTH_TEST );
  2286. _gl.depthFunc( _gl.LEQUAL );
  2287. _gl.frontFace( _gl.CCW );
  2288. _gl.cullFace( _gl.BACK );
  2289. _gl.enable( _gl.CULL_FACE );
  2290. _gl.enable( _gl.BLEND );
  2291. _gl.blendEquation( _gl.FUNC_ADD );
  2292. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  2293. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  2294. // _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, true );
  2295. _cullEnabled = true;
  2296. };
  2297. function buildProgram( shaderID, fragmentShader, vertexShader, uniforms, attributes, parameters ) {
  2298. var p, pl, program, code;
  2299. var chunks = [];
  2300. // Generate code
  2301. if ( shaderID ) {
  2302. chunks.push( shaderID );
  2303. } else {
  2304. chunks.push( fragmentShader );
  2305. chunks.push( vertexShader );
  2306. }
  2307. for ( p in parameters ) {
  2308. chunks.push( p );
  2309. chunks.push( parameters[ p ] );
  2310. }
  2311. code = chunks.join();
  2312. // Check if code has been already compiled
  2313. for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
  2314. if ( _programs[ p ].code == code ) {
  2315. // console.log( "Code already compiled." /*: \n\n" + code*/ );
  2316. return _programs[ p ].program;
  2317. }
  2318. }
  2319. //console.log( "building new program " );
  2320. //
  2321. program = _gl.createProgram(),
  2322. prefix_fragment = [
  2323. "#ifdef GL_ES",
  2324. "precision highp float;",
  2325. "#endif",
  2326. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  2327. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  2328. parameters.fog ? "#define USE_FOG" : "",
  2329. parameters.fog instanceof THREE.FogExp2 ? "#define FOG_EXP2" : "",
  2330. parameters.map ? "#define USE_MAP" : "",
  2331. parameters.envMap ? "#define USE_ENVMAP" : "",
  2332. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  2333. parameters.vertexColors ? "#define USE_COLOR" : "",
  2334. "uniform mat4 viewMatrix;",
  2335. "uniform vec3 cameraPosition;",
  2336. ""
  2337. ].join("\n"),
  2338. prefix_vertex = [
  2339. maxVertexTextures() > 0 ? "#define VERTEX_TEXTURES" : "",
  2340. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  2341. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  2342. "#define MAX_BONES " + parameters.maxBones,
  2343. parameters.map ? "#define USE_MAP" : "",
  2344. parameters.envMap ? "#define USE_ENVMAP" : "",
  2345. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  2346. parameters.vertexColors ? "#define USE_COLOR" : "",
  2347. parameters.skinning ? "#define USE_SKINNING" : "",
  2348. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  2349. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  2350. "uniform mat4 objectMatrix;",
  2351. "uniform mat4 modelViewMatrix;",
  2352. "uniform mat4 projectionMatrix;",
  2353. "uniform mat4 viewMatrix;",
  2354. "uniform mat3 normalMatrix;",
  2355. "uniform vec3 cameraPosition;",
  2356. "uniform mat4 cameraInverseMatrix;",
  2357. "attribute vec3 position;",
  2358. "attribute vec3 normal;",
  2359. "attribute vec2 uv;",
  2360. "attribute vec2 uv2;",
  2361. "#ifdef USE_COLOR",
  2362. "attribute vec3 color;",
  2363. "#endif",
  2364. "#ifdef USE_MORPHTARGETS",
  2365. "attribute vec3 morphTarget0;",
  2366. "attribute vec3 morphTarget1;",
  2367. "attribute vec3 morphTarget2;",
  2368. "attribute vec3 morphTarget3;",
  2369. "attribute vec3 morphTarget4;",
  2370. "attribute vec3 morphTarget5;",
  2371. "attribute vec3 morphTarget6;",
  2372. "attribute vec3 morphTarget7;",
  2373. "#endif",
  2374. "#ifdef USE_SKINNING",
  2375. "attribute vec4 skinVertexA;",
  2376. "attribute vec4 skinVertexB;",
  2377. "attribute vec4 skinIndex;",
  2378. "attribute vec4 skinWeight;",
  2379. "#endif",
  2380. ""
  2381. ].join("\n");
  2382. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragmentShader ) );
  2383. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertexShader ) );
  2384. _gl.linkProgram( program );
  2385. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  2386. console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  2387. }
  2388. //console.log( prefix_fragment + fragmentShader );
  2389. //console.log( prefix_vertex + vertexShader );
  2390. program.uniforms = {};
  2391. program.attributes = {};
  2392. var identifiers, u, a, i;
  2393. // cache uniform locations
  2394. identifiers = [
  2395. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
  2396. 'cameraInverseMatrix', 'boneGlobalMatrices', 'morphTargetInfluences'
  2397. ];
  2398. for ( u in uniforms ) {
  2399. identifiers.push( u );
  2400. }
  2401. cacheUniformLocations( program, identifiers );
  2402. // cache attributes locations
  2403. identifiers = [
  2404. "position", "normal", "uv", "uv2", "tangent", "color",
  2405. "skinVertexA", "skinVertexB", "skinIndex", "skinWeight"
  2406. ];
  2407. for ( i = 0; i < parameters.maxMorphTargets; i++ ) {
  2408. identifiers.push( "morphTarget" + i );
  2409. }
  2410. for ( a in attributes ) {
  2411. identifiers.push( a );
  2412. }
  2413. cacheAttributeLocations( program, identifiers );
  2414. _programs.push( { program: program, code: code } );
  2415. return program;
  2416. };
  2417. function loadUniformsSkinning( uniforms, object ) {
  2418. _gl.uniformMatrix4fv( uniforms.cameraInverseMatrix, false, _viewMatrixArray );
  2419. _gl.uniformMatrix4fv( uniforms.boneGlobalMatrices, false, object.boneMatrices );
  2420. };
  2421. function loadUniformsMatrices( uniforms, object ) {
  2422. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrixArray );
  2423. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrixArray );
  2424. };
  2425. function loadUniformsGeneric( program, uniforms ) {
  2426. var u, uniform, value, type, location, texture;
  2427. for( u in uniforms ) {
  2428. location = program.uniforms[u];
  2429. if ( !location ) continue;
  2430. uniform = uniforms[u];
  2431. type = uniform.type;
  2432. value = uniform.value;
  2433. if( type == "i" ) {
  2434. _gl.uniform1i( location, value );
  2435. } else if( type == "f" ) {
  2436. _gl.uniform1f( location, value );
  2437. } else if( type == "fv1" ) {
  2438. _gl.uniform1fv( location, value );
  2439. } else if( type == "fv" ) {
  2440. _gl.uniform3fv( location, value );
  2441. } else if( type == "v2" ) {
  2442. _gl.uniform2f( location, value.x, value.y );
  2443. } else if( type == "v3" ) {
  2444. _gl.uniform3f( location, value.x, value.y, value.z );
  2445. } else if( type == "v4" ) {
  2446. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  2447. } else if( type == "c" ) {
  2448. _gl.uniform3f( location, value.r, value.g, value.b );
  2449. } else if( type == "t" ) {
  2450. _gl.uniform1i( location, value );
  2451. texture = uniform.texture;
  2452. if ( !texture ) continue;
  2453. if ( texture.image instanceof Array && texture.image.length == 6 ) {
  2454. setCubeTexture( texture, value );
  2455. } else {
  2456. setTexture( texture, value );
  2457. }
  2458. }
  2459. }
  2460. };
  2461. function setBlending( blending ) {
  2462. if ( blending != _oldBlending ) {
  2463. switch ( blending ) {
  2464. case THREE.AdditiveBlending:
  2465. _gl.blendEquation( _gl.FUNC_ADD );
  2466. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  2467. break;
  2468. case THREE.SubtractiveBlending:
  2469. // TODO: Find blendFuncSeparate() combination
  2470. _gl.blendEquation( _gl.FUNC_ADD );
  2471. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  2472. break;
  2473. case THREE.MultiplyBlending:
  2474. // TODO: Find blendFuncSeparate() combination
  2475. _gl.blendEquation( _gl.FUNC_ADD );
  2476. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  2477. break;
  2478. default:
  2479. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  2480. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  2481. break;
  2482. }
  2483. _oldBlending = blending;
  2484. }
  2485. };
  2486. function setTextureParameters( textureType, texture, image ) {
  2487. if ( isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ) ) {
  2488. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  2489. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  2490. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  2491. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  2492. _gl.generateMipmap( textureType );
  2493. } else {
  2494. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  2495. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  2496. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  2497. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  2498. }
  2499. };
  2500. function setTexture( texture, slot ) {
  2501. if ( texture.needsUpdate ) {
  2502. if ( !texture.__webglInit ) {
  2503. texture.__webglTexture = _gl.createTexture();
  2504. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  2505. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  2506. texture.__webglInit = true;
  2507. } else {
  2508. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  2509. _gl.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  2510. }
  2511. setTextureParameters( _gl.TEXTURE_2D, texture, texture.image );
  2512. _gl.bindTexture( _gl.TEXTURE_2D, null );
  2513. texture.needsUpdate = false;
  2514. }
  2515. _gl.activeTexture( _gl.TEXTURE0 + slot );
  2516. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  2517. };
  2518. function setCubeTexture( texture, slot ) {
  2519. if ( texture.image.length == 6 ) {
  2520. if ( texture.needsUpdate ) {
  2521. if ( !texture.__webglInit ) {
  2522. texture.image.__webglTextureCube = _gl.createTexture();
  2523. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  2524. for ( var i = 0; i < 6; ++i ) {
  2525. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image[ i ] );
  2526. }
  2527. texture.__webglInit = true;
  2528. } else {
  2529. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  2530. for ( var i = 0; i < 6; ++i ) {
  2531. _gl.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image[ i ] );
  2532. }
  2533. }
  2534. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, texture.image[0] );
  2535. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  2536. texture.needsUpdate = false;
  2537. }
  2538. _gl.activeTexture( _gl.TEXTURE0 + slot );
  2539. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  2540. }
  2541. };
  2542. function setRenderTarget( renderTexture ) {
  2543. if ( renderTexture && !renderTexture.__webglFramebuffer ) {
  2544. renderTexture.__webglFramebuffer = _gl.createFramebuffer();
  2545. renderTexture.__webglRenderbuffer = _gl.createRenderbuffer();
  2546. renderTexture.__webglTexture = _gl.createTexture();
  2547. // Setup renderbuffer
  2548. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTexture.__webglRenderbuffer );
  2549. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTexture.width, renderTexture.height );
  2550. // Setup texture
  2551. _gl.bindTexture( _gl.TEXTURE_2D, renderTexture.__webglTexture );
  2552. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( renderTexture.wrapS ) );
  2553. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( renderTexture.wrapT ) );
  2554. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( renderTexture.magFilter ) );
  2555. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( renderTexture.minFilter ) );
  2556. _gl.texImage2D( _gl.TEXTURE_2D, 0, paramThreeToGL( renderTexture.format ), renderTexture.width, renderTexture.height, 0, paramThreeToGL( renderTexture.format ), paramThreeToGL( renderTexture.type ), null );
  2557. // Setup framebuffer
  2558. _gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTexture.__webglFramebuffer );
  2559. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, renderTexture.__webglTexture, 0 );
  2560. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTexture.__webglRenderbuffer );
  2561. // Release everything
  2562. _gl.bindTexture( _gl.TEXTURE_2D, null );
  2563. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  2564. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  2565. }
  2566. var framebuffer, width, height;
  2567. if ( renderTexture ) {
  2568. framebuffer = renderTexture.__webglFramebuffer;
  2569. width = renderTexture.width;
  2570. height = renderTexture.height;
  2571. } else {
  2572. framebuffer = null;
  2573. width = _viewportWidth;
  2574. height = _viewportHeight;
  2575. }
  2576. if ( framebuffer != _currentFramebuffer ) {
  2577. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  2578. _gl.viewport( _viewportX, _viewportY, width, height );
  2579. _currentFramebuffer = framebuffer;
  2580. }
  2581. };
  2582. function updateRenderTargetMipmap( renderTarget ) {
  2583. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  2584. _gl.generateMipmap( _gl.TEXTURE_2D );
  2585. _gl.bindTexture( _gl.TEXTURE_2D, null );
  2586. };
  2587. function cacheUniformLocations( program, identifiers ) {
  2588. var i, l, id;
  2589. for( i = 0, l = identifiers.length; i < l; i++ ) {
  2590. id = identifiers[ i ];
  2591. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  2592. }
  2593. };
  2594. function cacheAttributeLocations( program, identifiers ) {
  2595. var i, l, id;
  2596. for( i = 0, l = identifiers.length; i < l; i++ ) {
  2597. id = identifiers[ i ];
  2598. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  2599. }
  2600. };
  2601. function getShader( type, string ) {
  2602. var shader;
  2603. if ( type == "fragment" ) {
  2604. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  2605. } else if ( type == "vertex" ) {
  2606. shader = _gl.createShader( _gl.VERTEX_SHADER );
  2607. }
  2608. _gl.shaderSource( shader, string );
  2609. _gl.compileShader( shader );
  2610. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  2611. console.error( _gl.getShaderInfoLog( shader ) );
  2612. console.error( string );
  2613. return null;
  2614. }
  2615. return shader;
  2616. };
  2617. // fallback filters for non-power-of-2 textures
  2618. function filterFallback( f ) {
  2619. switch ( f ) {
  2620. case THREE.NearestFilter:
  2621. case THREE.NearestMipMapNearestFilter:
  2622. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST; break;
  2623. case THREE.LinearFilter:
  2624. case THREE.LinearMipMapNearestFilter:
  2625. case THREE.LinearMipMapLinearFilter:
  2626. default:
  2627. return _gl.LINEAR; break;
  2628. }
  2629. };
  2630. function paramThreeToGL( p ) {
  2631. switch ( p ) {
  2632. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  2633. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  2634. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  2635. case THREE.NearestFilter: return _gl.NEAREST; break;
  2636. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  2637. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  2638. case THREE.LinearFilter: return _gl.LINEAR; break;
  2639. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  2640. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  2641. case THREE.ByteType: return _gl.BYTE; break;
  2642. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  2643. case THREE.ShortType: return _gl.SHORT; break;
  2644. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  2645. case THREE.IntType: return _gl.INT; break;
  2646. case THREE.UnsignedShortType: return _gl.UNSIGNED_INT; break;
  2647. case THREE.FloatType: return _gl.FLOAT; break;
  2648. case THREE.AlphaFormat: return _gl.ALPHA; break;
  2649. case THREE.RGBFormat: return _gl.RGB; break;
  2650. case THREE.RGBAFormat: return _gl.RGBA; break;
  2651. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  2652. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  2653. }
  2654. return 0;
  2655. };
  2656. function isPowerOfTwo( value ) {
  2657. return ( value & ( value - 1 ) ) == 0;
  2658. };
  2659. function materialNeedsSmoothNormals( material ) {
  2660. return material && material.shading != undefined && material.shading == THREE.SmoothShading;
  2661. };
  2662. function bufferNeedsSmoothNormals( geometryGroup, object ) {
  2663. var m, ml, i, l, meshMaterial, needsSmoothNormals = false;
  2664. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  2665. meshMaterial = object.materials[ m ];
  2666. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  2667. for ( i = 0, l = geometryGroup.materials.length; i < l; i++ ) {
  2668. if ( materialNeedsSmoothNormals( geometryGroup.materials[ i ] ) ) {
  2669. needsSmoothNormals = true;
  2670. break;
  2671. }
  2672. }
  2673. } else {
  2674. if ( materialNeedsSmoothNormals( meshMaterial ) ) {
  2675. needsSmoothNormals = true;
  2676. break;
  2677. }
  2678. }
  2679. if ( needsSmoothNormals ) break;
  2680. }
  2681. return needsSmoothNormals;
  2682. };
  2683. function unrollGroupMaterials( geometryGroup, object ) {
  2684. var m, ml, i, il,
  2685. material, meshMaterial,
  2686. materials = [];
  2687. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  2688. meshMaterial = object.materials[ m ];
  2689. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  2690. for ( i = 0, l = geometryGroup.materials.length; i < l; i++ ) {
  2691. material = geometryGroup.materials[ i ];
  2692. if ( material ) {
  2693. materials.push( material );
  2694. }
  2695. }
  2696. } else {
  2697. material = meshMaterial;
  2698. if ( material ) {
  2699. materials.push( material );
  2700. }
  2701. }
  2702. }
  2703. return materials;
  2704. };
  2705. function bufferGuessVertexColorType( materials, geometryGroup, object ) {
  2706. var i, m, ml = materials.length;
  2707. // use vertexColor type from the first material in unrolled materials
  2708. for ( i = 0; i < ml; i++ ) {
  2709. m = materials[ i ];
  2710. if ( m.vertexColors ) {
  2711. return m.vertexColors;
  2712. }
  2713. }
  2714. return false;
  2715. };
  2716. function bufferGuessNormalType( materials, geometryGroup, object ) {
  2717. var i, m, ml = materials.length;
  2718. // only MeshBasicMaterial and MeshDepthMaterial don't need normals
  2719. for ( i = 0; i < ml; i++ ) {
  2720. m = materials[ i ];
  2721. if ( ( m instanceof THREE.MeshBasicMaterial && !m.envMap ) || m instanceof THREE.MeshDepthMaterial ) continue;
  2722. if ( materialNeedsSmoothNormals( m ) ) {
  2723. return THREE.SmoothShading;
  2724. } else {
  2725. return THREE.FlatShading;
  2726. }
  2727. }
  2728. return false;
  2729. };
  2730. function bufferGuessUVType( materials, geometryGroup, object ) {
  2731. var i, m, ml = materials.length;
  2732. // material must use some texture to require uvs
  2733. for ( i = 0; i < ml; i++ ) {
  2734. m = materials[ i ];
  2735. if ( m.map || m.lightMap || m instanceof THREE.MeshShaderMaterial ) {
  2736. return true;
  2737. }
  2738. }
  2739. return false;
  2740. };
  2741. function allocateBones( object ) {
  2742. // default for when object is not specified
  2743. // ( for example when prebuilding shader
  2744. // to be used with multiple objects )
  2745. //
  2746. // - leave some extra space for other uniforms
  2747. // - limit here is ANGLE's 254 max uniform vectors
  2748. // (up to 54 should be safe)
  2749. var maxBones = 50;
  2750. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  2751. maxBones = object.bones.length;
  2752. }
  2753. return maxBones;
  2754. };
  2755. function allocateLights( lights, maxLights ) {
  2756. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  2757. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  2758. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  2759. light = lights[ l ];
  2760. if ( light instanceof THREE.DirectionalLight ) dirLights++;
  2761. if ( light instanceof THREE.PointLight ) pointLights++;
  2762. }
  2763. if ( ( pointLights + dirLights ) <= maxLights ) {
  2764. maxDirLights = dirLights;
  2765. maxPointLights = pointLights;
  2766. } else {
  2767. maxDirLights = Math.ceil( maxLights * dirLights / ( pointLights + dirLights ) );
  2768. maxPointLights = maxLights - maxDirLights;
  2769. }
  2770. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  2771. };
  2772. /* DEBUG
  2773. function getGLParams() {
  2774. var params = {
  2775. 'MAX_VARYING_VECTORS': _gl.getParameter( _gl.MAX_VARYING_VECTORS ),
  2776. 'MAX_VERTEX_ATTRIBS': _gl.getParameter( _gl.MAX_VERTEX_ATTRIBS ),
  2777. 'MAX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS ),
  2778. 'MAX_VERTEX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  2779. 'MAX_COMBINED_TEXTURE_IMAGE_UNITS' : _gl.getParameter( _gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS ),
  2780. 'MAX_VERTEX_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS ),
  2781. 'MAX_FRAGMENT_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_FRAGMENT_UNIFORM_VECTORS )
  2782. }
  2783. return params;
  2784. };
  2785. function dumpObject( obj ) {
  2786. var p, str = "";
  2787. for ( p in obj ) {
  2788. str += p + ": " + obj[p] + "\n";
  2789. }
  2790. return str;
  2791. }
  2792. */
  2793. };