WebGLRenderer.js 95 KB

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