WebGLRenderer.js 134 KB

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