WebGLRenderer.js 84 KB

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