WebGLRenderer.js 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412
  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. setupLights( program, lights );
  1041. refreshUniformsLights( m_uniforms, _lights );
  1042. }
  1043. if ( material instanceof THREE.MeshBasicMaterial ||
  1044. material instanceof THREE.MeshLambertMaterial ||
  1045. material instanceof THREE.MeshPhongMaterial ) {
  1046. refreshUniformsCommon( m_uniforms, material );
  1047. }
  1048. // refresh single material specific uniforms
  1049. if ( material instanceof THREE.LineBasicMaterial ) {
  1050. refreshUniformsLine( m_uniforms, material );
  1051. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  1052. refreshUniformsParticle( m_uniforms, material );
  1053. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  1054. refreshUniformsPhong( m_uniforms, material );
  1055. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  1056. m_uniforms.mNear.value = camera.near;
  1057. m_uniforms.mFar.value = camera.far;
  1058. m_uniforms.opacity.value = material.opacity;
  1059. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  1060. m_uniforms.opacity.value = material.opacity;
  1061. }
  1062. // load common uniforms
  1063. loadUniformsGeneric( program, m_uniforms );
  1064. loadUniformsMatrices( p_uniforms, object );
  1065. // load material specific uniforms
  1066. // (shader material also gets them for the sake of genericity)
  1067. if ( material instanceof THREE.MeshShaderMaterial ||
  1068. material instanceof THREE.MeshPhongMaterial ||
  1069. material.envMap ) {
  1070. _gl.uniform3f( p_uniforms.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  1071. }
  1072. if ( material instanceof THREE.MeshShaderMaterial ||
  1073. material.envMap ||
  1074. material.skinning ) {
  1075. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  1076. }
  1077. if ( material instanceof THREE.MeshPhongMaterial ||
  1078. material instanceof THREE.MeshLambertMaterial ||
  1079. material instanceof THREE.MeshShaderMaterial ||
  1080. material.skinning ) {
  1081. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, _viewMatrixArray );
  1082. }
  1083. if ( material.skinning ) {
  1084. loadUniformsSkinning( p_uniforms, object );
  1085. }
  1086. return program;
  1087. };
  1088. function renderBuffer ( camera, lights, fog, material, geometryGroup, object ) {
  1089. if ( material.opacity == 0 ) return;
  1090. var program, attributes, linewidth, primitives;
  1091. program = setProgram( camera, lights, fog, material, object );
  1092. attributes = program.attributes;
  1093. // vertices
  1094. if ( !material.morphTargets ) {
  1095. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLVertexBuffer );
  1096. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1097. } else {
  1098. // set base
  1099. if( object.morphTargetBase !== -1 ) {
  1100. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLMorphTargetsBuffers[ object.morphTargetBase ] );
  1101. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1102. } else {
  1103. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLVertexBuffer );
  1104. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1105. }
  1106. // find most influencing
  1107. var used = [];
  1108. var candidateInfluence = -1;
  1109. var candidate = 0;
  1110. var influences = object.morphTargetInfluences;
  1111. var i, il = influences.length;
  1112. var m = 0;
  1113. if( object.morphTargetBase !== -1 ) {
  1114. used[ object.morphTargetBase ] = true;
  1115. }
  1116. while( m < material.numSupportedMorphTargets ) {
  1117. for( i = 0; i < il; i++ ) {
  1118. if( !used[ i ] && influences[ i ] > candidateInfluence ) {
  1119. candidate = i;
  1120. candidateInfluence = influences[ candidate ];
  1121. }
  1122. }
  1123. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLMorphTargetsBuffers[ candidate ] );
  1124. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  1125. object.__webGLMorphTargetInfluences[ m ] = candidateInfluence;
  1126. used[ candidate ] = 1;
  1127. candidateInfluence = -1;
  1128. m++;
  1129. }
  1130. // load updated influences uniform
  1131. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webGLMorphTargetInfluences );
  1132. }
  1133. // colors
  1134. if ( attributes.color >= 0 ) {
  1135. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLColorBuffer );
  1136. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  1137. }
  1138. // normals
  1139. if ( attributes.normal >= 0 ) {
  1140. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLNormalBuffer );
  1141. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1142. }
  1143. // tangents
  1144. if ( attributes.tangent >= 0 ) {
  1145. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLTangentBuffer );
  1146. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  1147. }
  1148. // uvs
  1149. if ( attributes.uv >= 0 ) {
  1150. if ( geometryGroup.__webGLUVBuffer ) {
  1151. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLUVBuffer );
  1152. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  1153. _gl.enableVertexAttribArray( attributes.uv );
  1154. } else {
  1155. _gl.disableVertexAttribArray( attributes.uv );
  1156. }
  1157. }
  1158. if ( attributes.uv2 >= 0 ) {
  1159. if ( geometryGroup.__webGLUV2Buffer ) {
  1160. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLUV2Buffer );
  1161. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  1162. _gl.enableVertexAttribArray( attributes.uv2 );
  1163. } else {
  1164. _gl.disableVertexAttribArray( attributes.uv2 );
  1165. }
  1166. }
  1167. if ( material.skinning &&
  1168. attributes.skinVertexA >= 0 && attributes.skinVertexB >= 0 &&
  1169. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  1170. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLSkinVertexABuffer );
  1171. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  1172. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLSkinVertexBBuffer );
  1173. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  1174. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLSkinIndicesBuffer );
  1175. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  1176. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webGLSkinWeightsBuffer );
  1177. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  1178. }
  1179. // render mesh
  1180. if ( object instanceof THREE.Mesh ) {
  1181. // wireframe
  1182. if ( material.wireframe ) {
  1183. _gl.lineWidth( material.wireframeLinewidth );
  1184. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webGLLineBuffer );
  1185. _gl.drawElements( _gl.LINES, geometryGroup.__webGLLineCount, _gl.UNSIGNED_SHORT, 0 );
  1186. // triangles
  1187. } else {
  1188. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webGLFaceBuffer );
  1189. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
  1190. }
  1191. // render lines
  1192. } else if ( object instanceof THREE.Line ) {
  1193. primitives = ( object.type == THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  1194. _gl.lineWidth( material.linewidth );
  1195. _gl.drawArrays( primitives, 0, geometryGroup.__webGLLineCount );
  1196. // render particles
  1197. } else if ( object instanceof THREE.ParticleSystem ) {
  1198. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webGLParticleCount );
  1199. // render ribbon
  1200. } else if ( object instanceof THREE.Ribbon ) {
  1201. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webGLVertexCount );
  1202. }
  1203. };
  1204. function renderBufferImmediate ( object, program ) {
  1205. if ( ! object.__webGLVertexBuffer ) object.__webGLVertexBuffer = _gl.createBuffer();
  1206. if ( ! object.__webGLNormalBuffer ) object.__webGLNormalBuffer = _gl.createBuffer();
  1207. if ( object.hasPos ) {
  1208. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
  1209. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  1210. _gl.enableVertexAttribArray( program.attributes.position );
  1211. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  1212. }
  1213. if ( object.hasNormal ) {
  1214. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
  1215. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  1216. _gl.enableVertexAttribArray( program.attributes.normal );
  1217. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  1218. }
  1219. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  1220. object.count = 0;
  1221. };
  1222. function setObjectFaces ( object ) {
  1223. if ( _oldDoubleSided != object.doubleSided ) {
  1224. if( object.doubleSided ) {
  1225. _gl.disable( _gl.CULL_FACE );
  1226. } else {
  1227. _gl.enable( _gl.CULL_FACE );
  1228. }
  1229. _oldDoubleSided = object.doubleSided;
  1230. }
  1231. if ( _oldFlipSided != object.flipSided ) {
  1232. if( object.flipSided ) {
  1233. _gl.frontFace( _gl.CW );
  1234. } else {
  1235. _gl.frontFace( _gl.CCW );
  1236. }
  1237. _oldFlipSided = object.flipSided;
  1238. }
  1239. };
  1240. function setDepthTest ( test ) {
  1241. if ( _oldDepth != test ) {
  1242. if( test ) {
  1243. _gl.enable( _gl.DEPTH_TEST );
  1244. } else {
  1245. _gl.disable( _gl.DEPTH_TEST );
  1246. }
  1247. _oldDepth = test;
  1248. }
  1249. };
  1250. function computeFrustum ( m ) {
  1251. _frustum[ 0 ].set( m.n41 - m.n11, m.n42 - m.n12, m.n43 - m.n13, m.n44 - m.n14 );
  1252. _frustum[ 1 ].set( m.n41 + m.n11, m.n42 + m.n12, m.n43 + m.n13, m.n44 + m.n14 );
  1253. _frustum[ 2 ].set( m.n41 + m.n21, m.n42 + m.n22, m.n43 + m.n23, m.n44 + m.n24 );
  1254. _frustum[ 3 ].set( m.n41 - m.n21, m.n42 - m.n22, m.n43 - m.n23, m.n44 - m.n24 );
  1255. _frustum[ 4 ].set( m.n41 - m.n31, m.n42 - m.n32, m.n43 - m.n33, m.n44 - m.n34 );
  1256. _frustum[ 5 ].set( m.n41 + m.n31, m.n42 + m.n32, m.n43 + m.n33, m.n44 + m.n34 );
  1257. var i, plane;
  1258. for ( i = 0; i < 6; i ++ ) {
  1259. plane = _frustum[ i ];
  1260. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  1261. }
  1262. };
  1263. function isInFrustum ( object ) {
  1264. var distance, matrix = object.matrixWorld,
  1265. radius = - object.geometry.boundingSphere.radius * Math.max( object.scale.x, Math.max( object.scale.y, object.scale.z ) );
  1266. for ( var i = 0; i < 6; i ++ ) {
  1267. distance = _frustum[ i ].x * matrix.n14 + _frustum[ i ].y * matrix.n24 + _frustum[ i ].z * matrix.n34 + _frustum[ i ].w;
  1268. if ( distance <= radius ) return false;
  1269. }
  1270. return true;
  1271. };
  1272. function addToFixedArray ( where, what ) {
  1273. where.list[ where.count ] = what;
  1274. where.count += 1;
  1275. };
  1276. function unrollImmediateBufferMaterials ( globject ) {
  1277. var i, l, m, ml, material,
  1278. object = globject.object,
  1279. opaque = globject.opaque,
  1280. transparent = globject.transparent;
  1281. transparent.count = 0;
  1282. opaque.count = 0;
  1283. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1284. material = object.materials[ m ];
  1285. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending )
  1286. addToFixedArray( transparent, material );
  1287. else
  1288. addToFixedArray( opaque, material );
  1289. }
  1290. };
  1291. function unrollBufferMaterials ( globject ) {
  1292. var i, l, m, ml, material, meshMaterial,
  1293. object = globject.object,
  1294. buffer = globject.buffer,
  1295. opaque = globject.opaque,
  1296. transparent = globject.transparent;
  1297. transparent.count = 0;
  1298. opaque.count = 0;
  1299. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  1300. meshMaterial = object.materials[ m ];
  1301. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  1302. for ( i = 0, l = buffer.materials.length; i < l; i++ ) {
  1303. material = buffer.materials[ i ];
  1304. if ( material ) {
  1305. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending )
  1306. addToFixedArray( transparent, material );
  1307. else
  1308. addToFixedArray( opaque, material );
  1309. }
  1310. }
  1311. } else {
  1312. material = meshMaterial;
  1313. if ( ( material.opacity && material.opacity < 1.0 ) || material.blending != THREE.NormalBlending ) {
  1314. addToFixedArray( transparent, material );
  1315. } else {
  1316. addToFixedArray( opaque, material );
  1317. }
  1318. }
  1319. }
  1320. };
  1321. function painterSort ( a, b ) {
  1322. return b.z - a.z;
  1323. };
  1324. this.render = function ( scene, camera, renderTarget, forceClear ) {
  1325. var i, program, opaque, transparent, material,
  1326. o, ol, oil, webglObject, object, buffer,
  1327. lights = scene.lights,
  1328. fog = scene.fog,
  1329. ol;
  1330. camera.matrixAutoUpdate && camera.updateMatrix();
  1331. scene.update( undefined, false, camera );
  1332. camera.matrixWorldInverse.flattenToArray( _viewMatrixArray );
  1333. camera.projectionMatrix.flattenToArray( _projectionMatrixArray );
  1334. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1335. computeFrustum( _projScreenMatrix );
  1336. this.initWebGLObjects( scene );
  1337. setRenderTarget( renderTarget );
  1338. if ( this.autoClear || forceClear ) {
  1339. this.clear();
  1340. }
  1341. // set matrices
  1342. ol = scene.__webglObjects.length;
  1343. for ( o = 0; o < ol; o++ ) {
  1344. webglObject = scene.__webglObjects[ o ];
  1345. object = webglObject.object;
  1346. if ( object.visible ) {
  1347. if ( ! ( object instanceof THREE.Mesh ) || isInFrustum( object ) ) {
  1348. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1349. setupMatrices( object, camera );
  1350. unrollBufferMaterials( webglObject );
  1351. webglObject.render = true;
  1352. if ( this.sortObjects ) {
  1353. _vector3.copy( object.position );
  1354. _projScreenMatrix.multiplyVector3( _vector3 );
  1355. webglObject.z = _vector3.z;
  1356. }
  1357. } else {
  1358. webglObject.render = false;
  1359. }
  1360. } else {
  1361. webglObject.render = false;
  1362. }
  1363. }
  1364. if ( this.sortObjects ) {
  1365. scene.__webglObjects.sort( painterSort );
  1366. }
  1367. oil = scene.__webglObjectsImmediate.length;
  1368. for ( o = 0; o < oil; o++ ) {
  1369. webglObject = scene.__webglObjectsImmediate[ o ];
  1370. object = webglObject.object;
  1371. if ( object.visible ) {
  1372. if( object.matrixAutoUpdate ) {
  1373. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1374. }
  1375. setupMatrices( object, camera );
  1376. unrollImmediateBufferMaterials( webglObject );
  1377. }
  1378. }
  1379. // opaque pass
  1380. setBlending( THREE.NormalBlending );
  1381. for ( o = 0; o < ol; o++ ) {
  1382. webglObject = scene.__webglObjects[ o ];
  1383. if ( webglObject.render ) {
  1384. object = webglObject.object;
  1385. buffer = webglObject.buffer;
  1386. opaque = webglObject.opaque;
  1387. setObjectFaces( object );
  1388. for( i = 0; i < opaque.count; i++ ) {
  1389. material = opaque.list[ i ];
  1390. setDepthTest( material.depthTest );
  1391. renderBuffer( camera, lights, fog, material, buffer, object );
  1392. }
  1393. }
  1394. }
  1395. // opaque pass (immediate simulator)
  1396. for ( o = 0; o < oil; o++ ) {
  1397. webglObject = scene.__webglObjectsImmediate[ o ];
  1398. object = webglObject.object;
  1399. if ( object.visible ) {
  1400. opaque = webglObject.opaque;
  1401. setObjectFaces( object );
  1402. for( i = 0; i < opaque.count; i++ ) {
  1403. material = opaque.list[ i ];
  1404. setDepthTest( material.depthTest );
  1405. program = setProgram( camera, lights, fog, material, object );
  1406. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1407. }
  1408. }
  1409. }
  1410. // transparent pass
  1411. for ( o = 0; o < ol; o++ ) {
  1412. webglObject = scene.__webglObjects[ o ];
  1413. if ( webglObject.render ) {
  1414. object = webglObject.object;
  1415. buffer = webglObject.buffer;
  1416. transparent = webglObject.transparent;
  1417. setObjectFaces( object );
  1418. for( i = 0; i < transparent.count; i++ ) {
  1419. material = transparent.list[ i ];
  1420. setBlending( material.blending );
  1421. setDepthTest( material.depthTest );
  1422. renderBuffer( camera, lights, fog, material, buffer, object );
  1423. }
  1424. }
  1425. }
  1426. // transparent pass (immediate simulator)
  1427. for ( o = 0; o < oil; o++ ) {
  1428. webglObject = scene.__webglObjectsImmediate[ o ];
  1429. object = webglObject.object;
  1430. if ( object.visible ) {
  1431. transparent = webglObject.transparent;
  1432. setObjectFaces( object );
  1433. for( i = 0; i < transparent.count; i++ ) {
  1434. material = transparent.list[ i ];
  1435. setBlending( material.blending );
  1436. setDepthTest( material.depthTest );
  1437. program = setProgram( camera, lights, fog, material, object );
  1438. object.render( function( object ) { renderBufferImmediate( object, program ); } );
  1439. }
  1440. }
  1441. }
  1442. // Generate mipmap if we're using any kind of mipmap filtering
  1443. if ( renderTarget && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  1444. updateRenderTargetMipmap( renderTarget );
  1445. }
  1446. };
  1447. function setupMatrices ( object, camera ) {
  1448. object._modelViewMatrix.multiplyToArray( camera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
  1449. THREE.Matrix4.makeInvert3x3( object._modelViewMatrix ).transposeIntoArray( object._normalMatrixArray );
  1450. };
  1451. this.initWebGLObjects = function ( scene ) {
  1452. if ( !scene.__webglObjects ) {
  1453. scene.__webglObjects = [];
  1454. scene.__webglObjectsImmediate = [];
  1455. }
  1456. while ( scene.__objectsAdded.length ) {
  1457. addObject( scene.__objectsAdded[ 0 ], scene );
  1458. scene.__objectsAdded.splice( 0, 1 );
  1459. }
  1460. while ( scene.__objectsRemoved.length ) {
  1461. removeObject( scene.__objectsRemoved[ 0 ], scene );
  1462. scene.__objectsRemoved.splice( 0, 1 );
  1463. }
  1464. // update must be called after objects adding / removal
  1465. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  1466. updateObject( scene.__webglObjects[ o ].object, scene );
  1467. }
  1468. };
  1469. function addObject ( object, scene ) {
  1470. var g, geometry, geometryGroup;
  1471. if ( object._modelViewMatrix == undefined ) {
  1472. object._modelViewMatrix = new THREE.Matrix4();
  1473. object._normalMatrixArray = new Float32Array( 9 );
  1474. object._modelViewMatrixArray = new Float32Array( 16 );
  1475. object._objectMatrixArray = new Float32Array( 16 );
  1476. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  1477. }
  1478. if ( object instanceof THREE.Mesh ) {
  1479. geometry = object.geometry;
  1480. if ( geometry.geometryGroups == undefined ) {
  1481. sortFacesByMaterial( geometry );
  1482. }
  1483. // create separate VBOs per geometry chunk
  1484. for ( g in geometry.geometryGroups ) {
  1485. geometryGroup = geometry.geometryGroups[ g ];
  1486. // initialise VBO on the first access
  1487. if ( ! geometryGroup.__webGLVertexBuffer ) {
  1488. createMeshBuffers( geometryGroup );
  1489. initMeshBuffers( geometryGroup, object );
  1490. geometry.__dirtyVertices = true;
  1491. geometry.__dirtyMorphTargets = true;
  1492. geometry.__dirtyElements = true;
  1493. geometry.__dirtyUvs = true;
  1494. geometry.__dirtyNormals = true;
  1495. geometry.__dirtyTangents = true;
  1496. geometry.__dirtyColors = true;
  1497. }
  1498. // create separate wrapper per each use of VBO
  1499. addBuffer( scene.__webglObjects, geometryGroup, object );
  1500. }
  1501. } else if ( object instanceof THREE.Ribbon ) {
  1502. geometry = object.geometry;
  1503. if( ! geometry.__webGLVertexBuffer ) {
  1504. createRibbonBuffers( geometry );
  1505. initRibbonBuffers( geometry );
  1506. geometry.__dirtyVertices = true;
  1507. geometry.__dirtyColors = true;
  1508. }
  1509. addBuffer( scene.__webglObjects, geometry, object );
  1510. } else if ( object instanceof THREE.Line ) {
  1511. geometry = object.geometry;
  1512. if( ! geometry.__webGLVertexBuffer ) {
  1513. createLineBuffers( geometry );
  1514. initLineBuffers( geometry );
  1515. geometry.__dirtyVertices = true;
  1516. geometry.__dirtyColors = true;
  1517. }
  1518. addBuffer( scene.__webglObjects, geometry, object );
  1519. } else if ( object instanceof THREE.ParticleSystem ) {
  1520. geometry = object.geometry;
  1521. if ( ! geometry.__webGLVertexBuffer ) {
  1522. createParticleBuffers( geometry );
  1523. initParticleBuffers( geometry );
  1524. geometry.__dirtyVertices = true;
  1525. geometry.__dirtyColors = true;
  1526. }
  1527. addBuffer( scene.__webglObjects, geometry, object );
  1528. } else if ( THREE.MarchingCubes !== undefined && object instanceof THREE.MarchingCubes ) {
  1529. addBufferImmediate( scene.__webglObjectsImmediate, object );
  1530. }/*else if ( object instanceof THREE.Particle ) {
  1531. }*/
  1532. };
  1533. function updateObject ( object, scene ) {
  1534. var g, geometry, geometryGroup;
  1535. if ( object instanceof THREE.Mesh ) {
  1536. geometry = object.geometry;
  1537. // check all geometry groups
  1538. for ( g in geometry.geometryGroups ) {
  1539. geometryGroup = geometry.geometryGroups[ g ];
  1540. if ( geometry.__dirtyVertices || geometry.__dirtyMorphTargets || geometry.__dirtyElements ||
  1541. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  1542. geometry.__dirtyColors || geometry.__dirtyTangents ) {
  1543. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW );
  1544. }
  1545. }
  1546. geometry.__dirtyVertices = false;
  1547. geometry.__dirtyMorphTargets = false;
  1548. geometry.__dirtyElements = false;
  1549. geometry.__dirtyUvs = false;
  1550. geometry.__dirtyNormals = false;
  1551. geometry.__dirtyTangents = false;
  1552. geometry.__dirtyColors = false;
  1553. } else if ( object instanceof THREE.Ribbon ) {
  1554. geometry = object.geometry;
  1555. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  1556. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  1557. }
  1558. geometry.__dirtyVertices = false;
  1559. geometry.__dirtyColors = false;
  1560. } else if ( object instanceof THREE.Line ) {
  1561. geometry = object.geometry;
  1562. if( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  1563. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  1564. }
  1565. geometry.__dirtyVertices = false;
  1566. geometry.__dirtyColors = false;
  1567. } else if ( object instanceof THREE.ParticleSystem ) {
  1568. geometry = object.geometry;
  1569. if ( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles ) {
  1570. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  1571. }
  1572. geometry.__dirtyVertices = false;
  1573. geometry.__dirtyColors = false;
  1574. }/* else if ( THREE.MarchingCubes !== undefined && object instanceof THREE.MarchingCubes ) {
  1575. // it updates itself in render callback
  1576. } else if ( object instanceof THREE.Particle ) {
  1577. }*/
  1578. };
  1579. function removeObject ( object, scene ) {
  1580. var o, ol, zobject;
  1581. for ( o = scene.__webglObjects.length - 1; o >= 0; o-- ) {
  1582. zobject = scene.__webglObjects[ o ].object;
  1583. if ( object == zobject ) {
  1584. scene.__webglObjects.splice( o, 1 );
  1585. }
  1586. }
  1587. };
  1588. function sortFacesByMaterial ( geometry ) {
  1589. // TODO
  1590. // Should optimize by grouping faces with ColorFill / ColorStroke materials
  1591. // which could then use vertex color attributes instead of each being
  1592. // in its separate VBO
  1593. var i, l, f, fl, face, material, materials, vertices, mhash, ghash, hash_map = {};
  1594. var numMorphTargets = geometry.morphTargets !== undefined ? geometry.morphTargets.length : 0;
  1595. geometry.geometryGroups = {};
  1596. function materialHash( material ) {
  1597. var hash_array = [];
  1598. for ( i = 0, l = material.length; i < l; i++ ) {
  1599. if ( material[ i ] == undefined ) {
  1600. hash_array.push( "undefined" );
  1601. } else {
  1602. hash_array.push( material[ i ].id );
  1603. }
  1604. }
  1605. return hash_array.join( '_' );
  1606. }
  1607. for ( f = 0, fl = geometry.faces.length; f < fl; f++ ) {
  1608. face = geometry.faces[ f ];
  1609. materials = face.materials;
  1610. mhash = materialHash( materials );
  1611. if ( hash_map[ mhash ] == undefined ) {
  1612. hash_map[ mhash ] = { 'hash': mhash, 'counter': 0 };
  1613. }
  1614. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  1615. if ( geometry.geometryGroups[ ghash ] == undefined ) {
  1616. geometry.geometryGroups[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0, 'numMorphTargets': numMorphTargets };
  1617. }
  1618. vertices = face instanceof THREE.Face3 ? 3 : 4;
  1619. if ( geometry.geometryGroups[ ghash ].vertices + vertices > 65535 ) {
  1620. hash_map[ mhash ].counter += 1;
  1621. ghash = hash_map[ mhash ].hash + '_' + hash_map[ mhash ].counter;
  1622. if ( geometry.geometryGroups[ ghash ] == undefined ) {
  1623. geometry.geometryGroups[ ghash ] = { 'faces': [], 'materials': materials, 'vertices': 0, 'numMorphTargets': numMorphTargets };
  1624. }
  1625. }
  1626. geometry.geometryGroups[ ghash ].faces.push( f );
  1627. geometry.geometryGroups[ ghash ].vertices += vertices;
  1628. }
  1629. };
  1630. function addBuffer ( objlist, buffer, object ) {
  1631. objlist.push( { buffer: buffer, object: object,
  1632. opaque: { list: [], count: 0 },
  1633. transparent: { list: [], count: 0 }
  1634. } );
  1635. };
  1636. function addBufferImmediate ( objlist, object ) {
  1637. objlist.push( { object: object,
  1638. opaque: { list: [], count: 0 },
  1639. transparent: { list: [], count: 0 }
  1640. } );
  1641. };
  1642. this.setFaceCulling = function ( cullFace, frontFace ) {
  1643. if ( cullFace ) {
  1644. if ( !frontFace || frontFace == "ccw" ) {
  1645. _gl.frontFace( _gl.CCW );
  1646. } else {
  1647. _gl.frontFace( _gl.CW );
  1648. }
  1649. if( cullFace == "back" ) {
  1650. _gl.cullFace( _gl.BACK );
  1651. } else if( cullFace == "front" ) {
  1652. _gl.cullFace( _gl.FRONT );
  1653. } else {
  1654. _gl.cullFace( _gl.FRONT_AND_BACK );
  1655. }
  1656. _gl.enable( _gl.CULL_FACE );
  1657. } else {
  1658. _gl.disable( _gl.CULL_FACE );
  1659. }
  1660. };
  1661. this.supportsVertexTextures = function () {
  1662. return maxVertexTextures() > 0;
  1663. };
  1664. function maxVertexTextures () {
  1665. return _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  1666. };
  1667. function initGL ( antialias, clearColor, clearAlpha ) {
  1668. try {
  1669. if ( ! ( _gl = _canvas.getContext( 'experimental-webgl', { antialias: antialias } ) ) ) {
  1670. throw 'Error creating WebGL context.';
  1671. }
  1672. } catch ( e ) {
  1673. console.error( e );
  1674. }
  1675. _gl.clearColor( 0, 0, 0, 1 );
  1676. _gl.clearDepth( 1 );
  1677. _gl.enable( _gl.DEPTH_TEST );
  1678. _gl.depthFunc( _gl.LEQUAL );
  1679. _gl.frontFace( _gl.CCW );
  1680. _gl.cullFace( _gl.BACK );
  1681. _gl.enable( _gl.CULL_FACE );
  1682. _gl.enable( _gl.BLEND );
  1683. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  1684. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  1685. _cullEnabled = true;
  1686. };
  1687. function buildProgram ( fragmentShader, vertexShader, parameters ) {
  1688. var program = _gl.createProgram(),
  1689. prefix_fragment = [
  1690. "#ifdef GL_ES",
  1691. "precision highp float;",
  1692. "#endif",
  1693. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  1694. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  1695. parameters.fog ? "#define USE_FOG" : "",
  1696. parameters.fog instanceof THREE.FogExp2 ? "#define FOG_EXP2" : "",
  1697. parameters.map ? "#define USE_MAP" : "",
  1698. parameters.envMap ? "#define USE_ENVMAP" : "",
  1699. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  1700. parameters.vertexColors ? "#define USE_COLOR" : "",
  1701. "uniform mat4 viewMatrix;",
  1702. "uniform vec3 cameraPosition;",
  1703. ""
  1704. ].join("\n"),
  1705. prefix_vertex = [
  1706. maxVertexTextures() > 0 ? "#define VERTEX_TEXTURES" : "",
  1707. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  1708. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  1709. "#define MAX_BONES " + parameters.maxBones,
  1710. parameters.map ? "#define USE_MAP" : "",
  1711. parameters.envMap ? "#define USE_ENVMAP" : "",
  1712. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  1713. parameters.vertexColors ? "#define USE_COLOR" : "",
  1714. parameters.skinning ? "#define USE_SKINNING" : "",
  1715. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  1716. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  1717. "uniform mat4 objectMatrix;",
  1718. "uniform mat4 modelViewMatrix;",
  1719. "uniform mat4 projectionMatrix;",
  1720. "uniform mat4 viewMatrix;",
  1721. "uniform mat3 normalMatrix;",
  1722. "uniform vec3 cameraPosition;",
  1723. "uniform mat4 cameraInverseMatrix;",
  1724. "attribute vec3 position;",
  1725. "attribute vec3 morphTarget0;",
  1726. "attribute vec3 morphTarget1;",
  1727. "attribute vec3 morphTarget2;",
  1728. "attribute vec3 morphTarget3;",
  1729. "attribute vec3 morphTarget4;",
  1730. "attribute vec3 morphTarget5;",
  1731. "attribute vec3 morphTarget6;",
  1732. "attribute vec3 morphTarget7;",
  1733. "attribute vec3 normal;",
  1734. "attribute vec3 color;",
  1735. "attribute vec2 uv;",
  1736. "attribute vec2 uv2;",
  1737. "attribute vec4 skinVertexA;",
  1738. "attribute vec4 skinVertexB;",
  1739. "attribute vec4 skinIndex;",
  1740. "attribute vec4 skinWeight;",
  1741. ""
  1742. ].join("\n");
  1743. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragmentShader ) );
  1744. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertexShader ) );
  1745. _gl.linkProgram( program );
  1746. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  1747. alert( "Could not initialise shaders\n"+
  1748. "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  1749. //console.log( prefix_fragment + fragmentShader );
  1750. //console.log( prefix_vertex + vertexShader );
  1751. }
  1752. //console.log( prefix_fragment + fragmentShader );
  1753. //console.log( prefix_vertex + vertexShader );
  1754. program.uniforms = {};
  1755. program.attributes = {};
  1756. return program;
  1757. };
  1758. function loadUniformsSkinning ( uniforms, object ) {
  1759. _gl.uniformMatrix4fv( uniforms.cameraInverseMatrix, false, _viewMatrixArray );
  1760. _gl.uniformMatrix4fv( uniforms.boneGlobalMatrices, false, object.boneMatrices );
  1761. };
  1762. function loadUniformsMatrices ( uniforms, object ) {
  1763. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrixArray );
  1764. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrixArray );
  1765. };
  1766. function loadUniformsGeneric ( program, uniforms ) {
  1767. var u, uniform, value, type, location, texture;
  1768. for( u in uniforms ) {
  1769. location = program.uniforms[u];
  1770. if ( !location ) continue;
  1771. uniform = uniforms[u];
  1772. type = uniform.type;
  1773. value = uniform.value;
  1774. if( type == "i" ) {
  1775. _gl.uniform1i( location, value );
  1776. } else if( type == "f" ) {
  1777. _gl.uniform1f( location, value );
  1778. } else if( type == "fv1" ) {
  1779. _gl.uniform1fv( location, value );
  1780. } else if( type == "fv" ) {
  1781. _gl.uniform3fv( location, value );
  1782. } else if( type == "v2" ) {
  1783. _gl.uniform2f( location, value.x, value.y );
  1784. } else if( type == "v3" ) {
  1785. _gl.uniform3f( location, value.x, value.y, value.z );
  1786. } else if( type == "c" ) {
  1787. _gl.uniform3f( location, value.r, value.g, value.b );
  1788. } else if( type == "t" ) {
  1789. _gl.uniform1i( location, value );
  1790. texture = uniform.texture;
  1791. if ( !texture ) continue;
  1792. if ( texture.image instanceof Array && texture.image.length == 6 ) {
  1793. setCubeTexture( texture, value );
  1794. } else {
  1795. setTexture( texture, value );
  1796. }
  1797. }
  1798. }
  1799. };
  1800. function setBlending ( blending ) {
  1801. if ( blending != _oldBlending ) {
  1802. switch ( blending ) {
  1803. case THREE.AdditiveBlending:
  1804. _gl.blendEquation( _gl.FUNC_ADD );
  1805. _gl.blendFunc( _gl.ONE, _gl.ONE );
  1806. break;
  1807. case THREE.SubtractiveBlending:
  1808. //_gl.blendEquation( _gl.FUNC_SUBTRACT );
  1809. _gl.blendFunc( _gl.DST_COLOR, _gl.ZERO );
  1810. break;
  1811. case THREE.BillboardBlending:
  1812. _gl.blendEquation( _gl.FUNC_ADD );
  1813. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA);
  1814. break;
  1815. case THREE.ReverseSubtractiveBlending:
  1816. _gl.blendEquation( _gl.FUNC_REVERSE_SUBTRACT );
  1817. _gl.blendFunc( _gl.ONE, _gl.ONE );
  1818. break;
  1819. default:
  1820. _gl.blendEquation( _gl.FUNC_ADD );
  1821. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  1822. break;
  1823. }
  1824. _oldBlending = blending;
  1825. }
  1826. };
  1827. function setTextureParameters ( textureType, texture, image ) {
  1828. if ( isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ) ) {
  1829. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  1830. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  1831. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  1832. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  1833. _gl.generateMipmap( textureType );
  1834. } else {
  1835. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  1836. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  1837. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  1838. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  1839. }
  1840. };
  1841. function setTexture ( texture, slot ) {
  1842. if ( texture.needsUpdate ) {
  1843. if ( !texture.__wasSetOnce ) {
  1844. texture.__webGLTexture = _gl.createTexture();
  1845. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1846. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  1847. texture.__wasSetOnce = true;
  1848. } else {
  1849. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1850. _gl.texSubImage2D( _gl.TEXTURE_2D, 0, 0, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  1851. }
  1852. setTextureParameters( _gl.TEXTURE_2D, texture, texture.image );
  1853. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1854. texture.needsUpdate = false;
  1855. }
  1856. _gl.activeTexture( _gl.TEXTURE0 + slot );
  1857. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webGLTexture );
  1858. };
  1859. function setCubeTexture ( texture, slot ) {
  1860. if ( texture.image.length == 6 ) {
  1861. if ( texture.needsUpdate ) {
  1862. if ( !texture.__wasSetOnce ) {
  1863. texture.image.__webGLTextureCube = _gl.createTexture();
  1864. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webGLTextureCube );
  1865. for ( var i = 0; i < 6; ++i ) {
  1866. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image[ i ] );
  1867. }
  1868. texture.__wasSetOnce = true;
  1869. } else {
  1870. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webGLTextureCube );
  1871. for ( var i = 0; i < 6; ++i ) {
  1872. _gl.texSubImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image[ i ] );
  1873. }
  1874. }
  1875. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, texture.image[0] );
  1876. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  1877. texture.needsUpdate = false;
  1878. }
  1879. _gl.activeTexture( _gl.TEXTURE0 + slot );
  1880. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webGLTextureCube );
  1881. }
  1882. };
  1883. function setRenderTarget ( renderTexture ) {
  1884. if ( renderTexture && !renderTexture.__webGLFramebuffer ) {
  1885. renderTexture.__webGLFramebuffer = _gl.createFramebuffer();
  1886. renderTexture.__webGLRenderbuffer = _gl.createRenderbuffer();
  1887. renderTexture.__webGLTexture = _gl.createTexture();
  1888. // Setup renderbuffer
  1889. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer );
  1890. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTexture.width, renderTexture.height );
  1891. // Setup texture
  1892. _gl.bindTexture( _gl.TEXTURE_2D, renderTexture.__webGLTexture );
  1893. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( renderTexture.wrapS ) );
  1894. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( renderTexture.wrapT ) );
  1895. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( renderTexture.magFilter ) );
  1896. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( renderTexture.minFilter ) );
  1897. _gl.texImage2D( _gl.TEXTURE_2D, 0, paramThreeToGL( renderTexture.format ), renderTexture.width, renderTexture.height, 0, paramThreeToGL( renderTexture.format ), paramThreeToGL( renderTexture.type ), null );
  1898. // Setup framebuffer
  1899. _gl.bindFramebuffer( _gl.FRAMEBUFFER, renderTexture.__webGLFramebuffer );
  1900. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, renderTexture.__webGLTexture, 0 );
  1901. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTexture.__webGLRenderbuffer );
  1902. // Release everything
  1903. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1904. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  1905. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  1906. }
  1907. var framebuffer, width, height;
  1908. if ( renderTexture ) {
  1909. framebuffer = renderTexture.__webGLFramebuffer;
  1910. width = renderTexture.width;
  1911. height = renderTexture.height;
  1912. } else {
  1913. framebuffer = null;
  1914. width = _viewportWidth;
  1915. height = _viewportHeight;
  1916. }
  1917. if( framebuffer != _oldFramebuffer ) {
  1918. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  1919. _gl.viewport( _viewportX, _viewportY, width, height );
  1920. _oldFramebuffer = framebuffer;
  1921. }
  1922. };
  1923. function updateRenderTargetMipmap ( renderTarget ) {
  1924. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webGLTexture );
  1925. _gl.generateMipmap( _gl.TEXTURE_2D );
  1926. _gl.bindTexture( _gl.TEXTURE_2D, null );
  1927. };
  1928. function cacheUniformLocations ( program, identifiers ) {
  1929. var i, l, id;
  1930. for( i = 0, l = identifiers.length; i < l; i++ ) {
  1931. id = identifiers[ i ];
  1932. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  1933. }
  1934. };
  1935. function cacheAttributeLocations ( program, identifiers ) {
  1936. var i, l, id;
  1937. for( i = 0, l = identifiers.length; i < l; i++ ) {
  1938. id = identifiers[ i ];
  1939. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  1940. }
  1941. };
  1942. function getShader ( type, string ) {
  1943. var shader;
  1944. if ( type == "fragment" ) {
  1945. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  1946. } else if ( type == "vertex" ) {
  1947. shader = _gl.createShader( _gl.VERTEX_SHADER );
  1948. }
  1949. _gl.shaderSource( shader, string );
  1950. _gl.compileShader( shader );
  1951. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  1952. alert( _gl.getShaderInfoLog( shader ) );
  1953. return null;
  1954. }
  1955. return shader;
  1956. };
  1957. // fallback filters for non-power-of-2 textures
  1958. function filterFallback ( f ) {
  1959. switch ( f ) {
  1960. case THREE.NearestFilter:
  1961. case THREE.NearestMipMapNearestFilter:
  1962. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST; break;
  1963. case THREE.LinearFilter:
  1964. case THREE.LinearMipMapNearestFilter:
  1965. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR; break;
  1966. }
  1967. };
  1968. function paramThreeToGL ( p ) {
  1969. switch ( p ) {
  1970. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  1971. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  1972. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  1973. case THREE.NearestFilter: return _gl.NEAREST; break;
  1974. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  1975. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  1976. case THREE.LinearFilter: return _gl.LINEAR; break;
  1977. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  1978. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  1979. case THREE.ByteType: return _gl.BYTE; break;
  1980. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  1981. case THREE.ShortType: return _gl.SHORT; break;
  1982. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  1983. case THREE.IntType: return _gl.INT; break;
  1984. case THREE.UnsignedShortType: return _gl.UNSIGNED_INT; break;
  1985. case THREE.FloatType: return _gl.FLOAT; break;
  1986. case THREE.AlphaFormat: return _gl.ALPHA; break;
  1987. case THREE.RGBFormat: return _gl.RGB; break;
  1988. case THREE.RGBAFormat: return _gl.RGBA; break;
  1989. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  1990. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  1991. }
  1992. return 0;
  1993. };
  1994. function isPowerOfTwo ( value ) {
  1995. return ( value & ( value - 1 ) ) == 0;
  1996. };
  1997. function materialNeedsSmoothNormals ( material ) {
  1998. return material && material.shading != undefined && material.shading == THREE.SmoothShading;
  1999. };
  2000. function bufferNeedsSmoothNormals ( geometryGroup, object ) {
  2001. var m, ml, i, l, meshMaterial, needsSmoothNormals = false;
  2002. for ( m = 0, ml = object.materials.length; m < ml; m++ ) {
  2003. meshMaterial = object.materials[ m ];
  2004. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  2005. for ( i = 0, l = geometryGroup.materials.length; i < l; i++ ) {
  2006. if ( materialNeedsSmoothNormals( geometryGroup.materials[ i ] ) ) {
  2007. needsSmoothNormals = true;
  2008. break;
  2009. }
  2010. }
  2011. } else {
  2012. if ( materialNeedsSmoothNormals( meshMaterial ) ) {
  2013. needsSmoothNormals = true;
  2014. break;
  2015. }
  2016. }
  2017. if ( needsSmoothNormals ) break;
  2018. }
  2019. return needsSmoothNormals;
  2020. };
  2021. function allocateBones ( object ) {
  2022. // default for when object is not specified
  2023. // ( for example when prebuilding shader
  2024. // to be used with multiple objects )
  2025. //
  2026. // - leave some extra space for other uniforms
  2027. // - limit here is ANGLE's 254 max uniform vectors
  2028. // (up to 54 should be safe)
  2029. var maxBones = 50;
  2030. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  2031. maxBones = object.bones.length;
  2032. }
  2033. return maxBones;
  2034. };
  2035. function allocateLights ( lights, maxLights ) {
  2036. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  2037. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  2038. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  2039. light = lights[ l ];
  2040. if ( light instanceof THREE.DirectionalLight ) dirLights++;
  2041. if ( light instanceof THREE.PointLight ) pointLights++;
  2042. }
  2043. if ( ( pointLights + dirLights ) <= maxLights ) {
  2044. maxDirLights = dirLights;
  2045. maxPointLights = pointLights;
  2046. } else {
  2047. maxDirLights = Math.ceil( maxLights * dirLights / ( pointLights + dirLights ) );
  2048. maxPointLights = maxLights - maxDirLights;
  2049. }
  2050. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  2051. };
  2052. /* DEBUG
  2053. function getGLParams() {
  2054. var params = {
  2055. 'MAX_VARYING_VECTORS': _gl.getParameter( _gl.MAX_VARYING_VECTORS ),
  2056. 'MAX_VERTEX_ATTRIBS': _gl.getParameter( _gl.MAX_VERTEX_ATTRIBS ),
  2057. 'MAX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS ),
  2058. 'MAX_VERTEX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  2059. 'MAX_COMBINED_TEXTURE_IMAGE_UNITS' : _gl.getParameter( _gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS ),
  2060. 'MAX_VERTEX_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS ),
  2061. 'MAX_FRAGMENT_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_FRAGMENT_UNIFORM_VECTORS )
  2062. }
  2063. return params;
  2064. };
  2065. function dumpObject( obj ) {
  2066. var p, str = "";
  2067. for ( p in obj ) {
  2068. str += p + ": " + obj[p] + "\n";
  2069. }
  2070. return str;
  2071. }
  2072. */
  2073. };