WebGLRenderer.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.WebGLRenderer = function ( scene ) {
  7. // Currently you can use just up to 5 directional / point lights total.
  8. // Chrome barfs on shader linking when there are more than 5 lights :(
  9. // It seems problem comes from having too many varying vectors.
  10. // Weirdly, this is not GPU limitation as the same shader works ok in Firefox.
  11. // This difference could come from Chrome using ANGLE on Windows,
  12. // thus going DirectX9 route (while FF uses OpenGL).
  13. var _canvas = document.createElement( 'canvas' ), _gl, _program,
  14. _modelViewMatrix = new THREE.Matrix4(), _normalMatrix,
  15. BASIC = 0, LAMBERT = 1, PHONG = 2, DEPTH = 3, NORMAL = 4, // material constants used in shader
  16. maxLightCount = allocateLights( scene, 5 );
  17. this.domElement = _canvas;
  18. this.autoClear = true;
  19. initGL();
  20. initProgram( maxLightCount.directional, maxLightCount.point );
  21. // Querying via gl.getParameter() reports different values for CH and FF for many max parameters.
  22. // On my GPU Chrome reports MAX_VARYING_VECTORS = 8, FF reports 0 yet compiles shaders with many
  23. // more varying vectors (up to 29 lights are ok, more start to throw warnings to FF error console
  24. // and then crash the browser).
  25. //alert( dumpObject( getGLParams() ) );
  26. function allocateLights( scene, maxLights ) {
  27. // heuristics to create shader parameters according to lights in the scene
  28. // (not to blow over maxLights budget)
  29. if ( scene ) {
  30. var l, ll, light, dirLights = pointLights = maxDirLights = maxPointLights = 0;
  31. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  32. light = scene.lights[ l ];
  33. if ( light instanceof THREE.DirectionalLight ) dirLights++;
  34. if ( light instanceof THREE.PointLight ) pointLights++;
  35. }
  36. if ( ( pointLights + dirLights ) <= maxLights ) {
  37. maxDirLights = dirLights;
  38. maxPointLights = pointLights;
  39. } else {
  40. maxDirLights = Math.ceil( maxLights * dirLights / ( pointLights + dirLights ) );
  41. maxPointLights = maxLights - maxDirLights;
  42. }
  43. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  44. }
  45. return { 'directional' : 1, 'point' : maxLights - 1 };
  46. };
  47. this.setSize = function ( width, height ) {
  48. _canvas.width = width;
  49. _canvas.height = height;
  50. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  51. };
  52. this.clear = function () {
  53. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  54. };
  55. this.setupLights = function ( scene ) {
  56. var l, ll, light, r, g, b,
  57. ambientLights = [], pointLights = [], directionalLights = [],
  58. colors = [], positions = [];
  59. _gl.uniform1i( _program.enableLighting, scene.lights.length );
  60. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  61. light = scene.lights[ l ];
  62. if ( light instanceof THREE.AmbientLight ) {
  63. ambientLights.push( light );
  64. } else if ( light instanceof THREE.DirectionalLight ) {
  65. directionalLights.push( light );
  66. } else if( light instanceof THREE.PointLight ) {
  67. pointLights.push( light );
  68. }
  69. }
  70. // sum all ambient lights
  71. r = g = b = 0.0;
  72. for ( l = 0, ll = ambientLights.length; l < ll; l++ ) {
  73. r += ambientLights[ l ].color.r;
  74. g += ambientLights[ l ].color.g;
  75. b += ambientLights[ l ].color.b;
  76. }
  77. _gl.uniform3f( _program.ambientLightColor, r, g, b );
  78. // pass directional lights as float arrays
  79. colors = []; positions = [];
  80. for ( l = 0, ll = directionalLights.length; l < ll; l++ ) {
  81. light = directionalLights[ l ];
  82. colors.push( light.color.r * light.intensity );
  83. colors.push( light.color.g * light.intensity );
  84. colors.push( light.color.b * light.intensity );
  85. positions.push( light.position.x );
  86. positions.push( light.position.y );
  87. positions.push( light.position.z );
  88. }
  89. if ( directionalLights.length ) {
  90. _gl.uniform1i( _program.directionalLightNumber, directionalLights.length );
  91. _gl.uniform3fv( _program.directionalLightDirection, positions );
  92. _gl.uniform3fv( _program.directionalLightColor, colors );
  93. }
  94. // pass point lights as float arrays
  95. colors = []; positions = [];
  96. for ( l = 0, ll = pointLights.length; l < ll; l++ ) {
  97. light = pointLights[ l ];
  98. colors.push( light.color.r * light.intensity );
  99. colors.push( light.color.g * light.intensity );
  100. colors.push( light.color.b * light.intensity );
  101. positions.push( light.position.x );
  102. positions.push( light.position.y );
  103. positions.push( light.position.z );
  104. }
  105. if ( pointLights.length ) {
  106. _gl.uniform1i( _program.pointLightNumber, pointLights.length );
  107. _gl.uniform3fv( _program.pointLightPosition, positions );
  108. _gl.uniform3fv( _program.pointLightColor, colors );
  109. }
  110. };
  111. this.createBuffers = function ( object, mf ) {
  112. var f, fl, fi, face, vertexNormals, normal, uv, v1, v2, v3, v4, m, ml, i, l,
  113. materialFaceGroup = object.materialFaceGroup[ mf ],
  114. faceArray = [],
  115. lineArray = [],
  116. vertexArray = [],
  117. normalArray = [],
  118. uvArray = [],
  119. vertexIndex = 0,
  120. useSmoothNormals = false;
  121. // need to find out if there is any material in the object
  122. // (among all mesh materials and also face materials)
  123. // which would need smooth normals
  124. function needsSmoothNormals( material ) {
  125. return material && material.shading != undefined && material.shading == THREE.SmoothShading;
  126. }
  127. for ( m = 0, ml = object.material.length; m < ml; m++ ) {
  128. meshMaterial = object.material[ m ];
  129. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  130. for ( i = 0, l = materialFaceGroup.material.length; i < l; i++ ) {
  131. if ( needsSmoothNormals( materialFaceGroup.material[ i ] ) ) {
  132. useSmoothNormals = true;
  133. break;
  134. }
  135. }
  136. } else {
  137. if ( needsSmoothNormals( meshMaterial ) ) {
  138. useSmoothNormals = true;
  139. break;
  140. }
  141. }
  142. if ( useSmoothNormals ) break;
  143. }
  144. for ( f = 0, fl = materialFaceGroup.faces.length; f < fl; f++ ) {
  145. fi = materialFaceGroup.faces[f];
  146. face = object.geometry.faces[ fi ];
  147. vertexNormals = face.vertexNormals;
  148. normal = face.normal;
  149. uv = object.geometry.uvs[ fi ];
  150. if ( face instanceof THREE.Face3 ) {
  151. v1 = object.geometry.vertices[ face.a ].position;
  152. v2 = object.geometry.vertices[ face.b ].position;
  153. v3 = object.geometry.vertices[ face.c ].position;
  154. vertexArray.push( v1.x, v1.y, v1.z );
  155. vertexArray.push( v2.x, v2.y, v2.z );
  156. vertexArray.push( v3.x, v3.y, v3.z );
  157. if ( vertexNormals.length == 3 && useSmoothNormals ) {
  158. normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
  159. normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
  160. normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
  161. } else {
  162. normalArray.push( normal.x, normal.y, normal.z );
  163. normalArray.push( normal.x, normal.y, normal.z );
  164. normalArray.push( normal.x, normal.y, normal.z );
  165. }
  166. if ( uv ) {
  167. uvArray.push( uv[0].u, uv[0].v );
  168. uvArray.push( uv[1].u, uv[1].v );
  169. uvArray.push( uv[2].u, uv[2].v );
  170. }
  171. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  172. // TODO: don't add lines that already exist (faces sharing edge)
  173. lineArray.push( vertexIndex, vertexIndex + 1 );
  174. lineArray.push( vertexIndex, vertexIndex + 2 );
  175. lineArray.push( vertexIndex + 1, vertexIndex + 2 );
  176. vertexIndex += 3;
  177. } else if ( face instanceof THREE.Face4 ) {
  178. v1 = object.geometry.vertices[ face.a ].position;
  179. v2 = object.geometry.vertices[ face.b ].position;
  180. v3 = object.geometry.vertices[ face.c ].position;
  181. v4 = object.geometry.vertices[ face.d ].position;
  182. vertexArray.push( v1.x, v1.y, v1.z );
  183. vertexArray.push( v2.x, v2.y, v2.z );
  184. vertexArray.push( v3.x, v3.y, v3.z );
  185. vertexArray.push( v4.x, v4.y, v4.z );
  186. if ( vertexNormals.length == 4 && useSmoothNormals ) {
  187. normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
  188. normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
  189. normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
  190. normalArray.push( vertexNormals[3].x, vertexNormals[3].y, vertexNormals[3].z );
  191. } else {
  192. normalArray.push( normal.x, normal.y, normal.z );
  193. normalArray.push( normal.x, normal.y, normal.z );
  194. normalArray.push( normal.x, normal.y, normal.z );
  195. normalArray.push( normal.x, normal.y, normal.z );
  196. }
  197. if ( uv ) {
  198. uvArray.push( uv[0].u, uv[0].v );
  199. uvArray.push( uv[1].u, uv[1].v );
  200. uvArray.push( uv[2].u, uv[2].v );
  201. uvArray.push( uv[3].u, uv[3].v );
  202. }
  203. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  204. faceArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
  205. // TODO: don't add lines that already exist (faces sharing edge)
  206. lineArray.push( vertexIndex, vertexIndex + 1 );
  207. lineArray.push( vertexIndex, vertexIndex + 2 );
  208. lineArray.push( vertexIndex, vertexIndex + 3 );
  209. lineArray.push( vertexIndex + 1, vertexIndex + 2 );
  210. lineArray.push( vertexIndex + 2, vertexIndex + 3 );
  211. vertexIndex += 4;
  212. }
  213. }
  214. if ( !vertexArray.length ) {
  215. return;
  216. }
  217. materialFaceGroup.__webGLVertexBuffer = _gl.createBuffer();
  218. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLVertexBuffer );
  219. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexArray ), _gl.STATIC_DRAW );
  220. materialFaceGroup.__webGLNormalBuffer = _gl.createBuffer();
  221. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLNormalBuffer );
  222. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );
  223. materialFaceGroup.__webGLUVBuffer = _gl.createBuffer();
  224. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLUVBuffer );
  225. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvArray ), _gl.STATIC_DRAW );
  226. materialFaceGroup.__webGLFaceBuffer = _gl.createBuffer();
  227. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFaceGroup.__webGLFaceBuffer );
  228. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faceArray ), _gl.STATIC_DRAW );
  229. materialFaceGroup.__webGLLineBuffer = _gl.createBuffer();
  230. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFaceGroup.__webGLLineBuffer );
  231. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( lineArray ), _gl.STATIC_DRAW );
  232. materialFaceGroup.__webGLFaceCount = faceArray.length;
  233. materialFaceGroup.__webGLLineCount = lineArray.length;
  234. };
  235. this.renderBuffer = function ( material, materialFaceGroup ) {
  236. var mColor, mOpacity, mReflectivity,
  237. mWireframe, mLineWidth, mBlending,
  238. mAmbient, mSpecular, mShininess,
  239. mMap, envMap;
  240. if ( material instanceof THREE.MeshPhongMaterial ||
  241. material instanceof THREE.MeshLambertMaterial ||
  242. material instanceof THREE.MeshBasicMaterial ) {
  243. mColor = material.color;
  244. mOpacity = material.opacity;
  245. mReflectivity = material.reflectivity;
  246. mWireframe = material.wireframe;
  247. mLineWidth = material.wireframe_linewidth;
  248. mBlending = material.blending;
  249. mMap = material.map;
  250. envMap = material.env_map;
  251. _gl.uniform4f( _program.mColor, mColor.r * mOpacity, mColor.g * mOpacity, mColor.b * mOpacity, mOpacity );
  252. _gl.uniform1f( _program.mReflectivity, mReflectivity );
  253. }
  254. if ( material instanceof THREE.MeshNormalMaterial ) {
  255. mOpacity = material.opacity;
  256. mBlending = material.blending;
  257. _gl.uniform1f( _program.mOpacity, mOpacity );
  258. _gl.uniform1i( _program.material, NORMAL );
  259. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  260. mOpacity = material.opacity;
  261. mWireframe = material.wireframe;
  262. mLineWidth = material.wireframe_linewidth;
  263. _gl.uniform1f( _program.mOpacity, mOpacity );
  264. _gl.uniform1f( _program.m2Near, material.__2near );
  265. _gl.uniform1f( _program.mFarPlusNear, material.__farPlusNear );
  266. _gl.uniform1f( _program.mFarMinusNear, material.__farMinusNear );
  267. _gl.uniform1i( _program.material, DEPTH );
  268. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  269. mAmbient = material.ambient;
  270. mSpecular = material.specular;
  271. mShininess = material.shininess;
  272. _gl.uniform4f( _program.mAmbient, mAmbient.r, mAmbient.g, mAmbient.b, mOpacity );
  273. _gl.uniform4f( _program.mSpecular, mSpecular.r, mSpecular.g, mSpecular.b, mOpacity );
  274. _gl.uniform1f( _program.mShininess, mShininess );
  275. _gl.uniform1i( _program.material, PHONG );
  276. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  277. _gl.uniform1i( _program.material, LAMBERT );
  278. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  279. _gl.uniform1i( _program.material, BASIC );
  280. }
  281. if ( mMap ) {
  282. if ( !material.__webGLTexture && material.map.image.loaded ) {
  283. material.__webGLTexture = _gl.createTexture();
  284. _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
  285. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.map.image ) ;
  286. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.LINEAR );
  287. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_LINEAR );
  288. _gl.generateMipmap( _gl.TEXTURE_2D );
  289. _gl.bindTexture( _gl.TEXTURE_2D, null );
  290. }
  291. _gl.activeTexture( _gl.TEXTURE0 );
  292. _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
  293. _gl.uniform1i( _program.tMap, 0 );
  294. _gl.uniform1i( _program.enableMap, 1 );
  295. } else {
  296. _gl.uniform1i( _program.enableMap, 0 );
  297. }
  298. if ( envMap ) {
  299. if ( material.env_map &&
  300. material.env_map instanceof THREE.TextureCube &&
  301. material.env_map.image.length == 6 ) {
  302. if ( !material.__webGLTextureCube && !material.__cubeMapInitialized && material.env_map.image.loadCount == 6 ) {
  303. material.__webGLTextureCube = _gl.createTexture();
  304. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, material.__webGLTextureCube );
  305. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  306. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  307. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MAG_FILTER, _gl.LINEAR );
  308. _gl.texParameteri( _gl.TEXTURE_CUBE_MAP, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_LINEAR );
  309. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[0] );
  310. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_NEGATIVE_X, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[1] );
  311. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_Y, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[2] );
  312. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[3] );
  313. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_Z, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[4] );
  314. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.env_map.image[5] );
  315. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  316. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  317. material.__cubeMapInitialized = true;
  318. }
  319. _gl.activeTexture( _gl.TEXTURE1 );
  320. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, material.__webGLTextureCube );
  321. _gl.uniform1i( _program.tCube, 1 );
  322. }
  323. _gl.uniform1i( _program.enableCubeMap, 1 );
  324. } else {
  325. _gl.uniform1i( _program.enableCubeMap, 0 );
  326. }
  327. // vertices
  328. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLVertexBuffer );
  329. _gl.vertexAttribPointer( _program.position, 3, _gl.FLOAT, false, 0, 0 );
  330. // normals
  331. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLNormalBuffer );
  332. _gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );
  333. // uvs
  334. if ( mMap ) {
  335. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFaceGroup.__webGLUVBuffer );
  336. _gl.enableVertexAttribArray( _program.uv );
  337. _gl.vertexAttribPointer( _program.uv, 2, _gl.FLOAT, false, 0, 0 );
  338. } else {
  339. _gl.disableVertexAttribArray( _program.uv );
  340. }
  341. // render triangles
  342. if ( ! mWireframe ) {
  343. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFaceGroup.__webGLFaceBuffer );
  344. _gl.drawElements( _gl.TRIANGLES, materialFaceGroup.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
  345. // render lines
  346. } else {
  347. _gl.lineWidth( mLineWidth );
  348. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFaceGroup.__webGLLineBuffer );
  349. _gl.drawElements( _gl.LINES, materialFaceGroup.__webGLLineCount, _gl.UNSIGNED_SHORT, 0 );
  350. }
  351. };
  352. this.renderPass = function ( object, materialFaceGroup, blending ) {
  353. var i, l, m, ml, material, meshMaterial;
  354. for ( m = 0, ml = object.material.length; m < ml; m++ ) {
  355. meshMaterial = object.material[ m ];
  356. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  357. for ( i = 0, l = materialFaceGroup.material.length; i < l; i++ ) {
  358. material = materialFaceGroup.material[ i ];
  359. if ( material && material.blending == blending ) {
  360. this.setBlending( material.blending );
  361. this.renderBuffer( material, materialFaceGroup );
  362. }
  363. }
  364. } else {
  365. material = meshMaterial;
  366. if ( material && material.blending == blending ) {
  367. this.setBlending( material.blending );
  368. this.renderBuffer( material, materialFaceGroup );
  369. }
  370. }
  371. }
  372. };
  373. this.render = function( scene, camera ) {
  374. var o, ol;
  375. this.initWebGLObjects( scene );
  376. if ( this.autoClear ) {
  377. this.clear();
  378. }
  379. camera.autoUpdateMatrix && camera.updateMatrix();
  380. _gl.uniform3f( _program.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  381. this.setupLights( scene );
  382. // opaque pass
  383. for ( o = 0, ol = scene.__webGLObjects.length; o < ol; o++ ) {
  384. webGLObject = scene.__webGLObjects[ o ];
  385. this.setupMatrices( webGLObject.__object, camera );
  386. this.renderPass( webGLObject.__object, webGLObject, THREE.NormalBlending );
  387. }
  388. // transparent pass
  389. for ( o = 0, ol = scene.__webGLObjects.length; o < ol; o++ ) {
  390. webGLObject = scene.__webGLObjects[ o ];
  391. this.setupMatrices( webGLObject.__object, camera );
  392. this.renderPass( webGLObject.__object, webGLObject, THREE.AdditiveBlending );
  393. this.renderPass( webGLObject.__object, webGLObject, THREE.SubtractiveBlending );
  394. }
  395. };
  396. this.initWebGLObjects = function( scene ) {
  397. var o, ol, object, mf, materialFaceGroup;
  398. if ( !scene.__webGLObjects ) {
  399. scene.__webGLObjects = [];
  400. }
  401. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  402. object = scene.objects[ o ];
  403. if ( object instanceof THREE.Mesh ) {
  404. // create separate VBOs per material
  405. for ( mf in object.materialFaceGroup ) {
  406. materialFaceGroup = object.materialFaceGroup[ mf ];
  407. // initialise buffers on the first access
  408. if( ! materialFaceGroup.__webGLVertexBuffer ) {
  409. this.createBuffers( object, mf );
  410. materialFaceGroup.__object = object;
  411. scene.__webGLObjects.push( materialFaceGroup );
  412. }
  413. }
  414. } else if ( object instanceof THREE.Line ) {
  415. } else if ( object instanceof THREE.Particle ) {
  416. }
  417. }
  418. };
  419. this.setupMatrices = function ( object, camera ) {
  420. object.autoUpdateMatrix && object.updateMatrix();
  421. _modelViewMatrix.multiply( camera.matrix, object.matrix );
  422. _program.viewMatrixArray = new Float32Array( camera.matrix.flatten() );
  423. _program.modelViewMatrixArray = new Float32Array( _modelViewMatrix.flatten() );
  424. _program.projectionMatrixArray = new Float32Array( camera.projectionMatrix.flatten() );
  425. _normalMatrix = THREE.Matrix4.makeInvert3x3( _modelViewMatrix ).transpose();
  426. _program.normalMatrixArray = new Float32Array( _normalMatrix.m );
  427. _gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
  428. _gl.uniformMatrix4fv( _program.modelViewMatrix, false, _program.modelViewMatrixArray );
  429. _gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
  430. _gl.uniformMatrix3fv( _program.normalMatrix, false, _program.normalMatrixArray );
  431. _gl.uniformMatrix4fv( _program.objMatrix, false, new Float32Array( object.matrix.flatten() ) );
  432. };
  433. this.setBlending = function( blending ) {
  434. switch ( blending ) {
  435. case THREE.AdditiveBlending:
  436. _gl.blendEquation( _gl.FUNC_ADD );
  437. _gl.blendFunc( _gl.ONE, _gl.ONE );
  438. break;
  439. case THREE.SubtractiveBlending:
  440. //_gl.blendEquation( _gl.FUNC_SUBTRACT );
  441. _gl.blendFunc( _gl.DST_COLOR, _gl.ZERO );
  442. break;
  443. default:
  444. _gl.blendEquation( _gl.FUNC_ADD );
  445. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  446. break;
  447. }
  448. };
  449. this.setFaceCulling = function( cullFace, frontFace ) {
  450. if ( cullFace ) {
  451. if ( !frontFace || frontFace == "ccw" ) {
  452. _gl.frontFace( _gl.CCW );
  453. } else {
  454. _gl.frontFace( _gl.CW );
  455. }
  456. if( cullFace == "back" ) {
  457. _gl.cullFace( _gl.BACK );
  458. } else if( cullFace == "front" ) {
  459. _gl.cullFace( _gl.FRONT );
  460. } else {
  461. _gl.cullFace( _gl.FRONT_AND_BACK );
  462. }
  463. _gl.enable( _gl.CULL_FACE );
  464. } else {
  465. _gl.disable( _gl.CULL_FACE );
  466. }
  467. };
  468. function initGL() {
  469. try {
  470. _gl = _canvas.getContext( 'experimental-webgl', { antialias: true} );
  471. } catch(e) { }
  472. if (!_gl) {
  473. alert("WebGL not supported");
  474. throw "cannot create webgl context";
  475. }
  476. _gl.clearColor( 0, 0, 0, 1 );
  477. _gl.clearDepth( 1 );
  478. _gl.enable( _gl.DEPTH_TEST );
  479. _gl.depthFunc( _gl.LEQUAL );
  480. _gl.frontFace( _gl.CCW );
  481. _gl.cullFace( _gl.BACK );
  482. _gl.enable( _gl.CULL_FACE );
  483. _gl.enable( _gl.BLEND );
  484. //_gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  485. //_gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); // cool!
  486. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  487. _gl.clearColor( 0, 0, 0, 0 );
  488. };
  489. function generateFragmentShader( maxDirLights, maxPointLights ) {
  490. var chunks = [
  491. "#ifdef GL_ES",
  492. "precision highp float;",
  493. "#endif",
  494. maxDirLights ? "#define MAX_DIR_LIGHTS " + maxDirLights : "",
  495. maxPointLights ? "#define MAX_POINT_LIGHTS " + maxPointLights : "",
  496. "uniform int material;", // 0 - Basic, 1 - Lambert, 2 - Phong, 3 - Depth, 4 - Normal
  497. "uniform bool enableMap;",
  498. "uniform bool enableCubeMap;",
  499. "uniform samplerCube tCube;",
  500. "uniform float mReflectivity;",
  501. "uniform sampler2D tMap;",
  502. "uniform vec4 mColor;",
  503. "uniform float mOpacity;",
  504. "uniform vec4 mAmbient;",
  505. "uniform vec4 mSpecular;",
  506. "uniform float mShininess;",
  507. "uniform float m2Near;",
  508. "uniform float mFarPlusNear;",
  509. "uniform float mFarMinusNear;",
  510. "uniform int pointLightNumber;",
  511. "uniform int directionalLightNumber;",
  512. maxDirLights ? "uniform mat4 viewMatrix;" : "",
  513. maxDirLights ? "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];" : "",
  514. "varying vec3 vNormal;",
  515. "varying vec2 vUv;",
  516. "varying vec3 vLightWeighting;",
  517. maxPointLights ? "varying vec3 vPointLightVector[ MAX_POINT_LIGHTS ];" : "",
  518. "varying vec3 vViewPosition;",
  519. "varying vec3 vReflect;",
  520. "void main() {",
  521. "vec4 mapColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  522. "vec4 cubeColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
  523. // diffuse map
  524. "if ( enableMap ) {",
  525. "mapColor = texture2D( tMap, vUv );",
  526. "}",
  527. // cube map
  528. "if ( enableCubeMap ) {",
  529. "cubeColor = mReflectivity * textureCube( tCube, vReflect );",
  530. "}",
  531. // Normals
  532. "if ( material == 4 ) { ",
  533. "gl_FragColor = vec4( 0.5*normalize( vNormal ) + vec3(0.5, 0.5, 0.5), mOpacity );",
  534. // Depth
  535. "} else if ( material == 3 ) { ",
  536. // this breaks shader validation in Chrome 9.0.576.0 dev
  537. // and also latest continuous build Chromium 9.0.583.0 (66089)
  538. // (curiously it works in Chrome 9.0.576.0 canary build and Firefox 4b7)
  539. //"float w = 1.0 - ( m2Near / ( mFarPlusNear - gl_FragCoord.z * mFarMinusNear ) );",
  540. "float w = 0.5;",
  541. "gl_FragColor = vec4( w, w, w, mOpacity );",
  542. // Blinn-Phong
  543. // based on o3d example
  544. "} else if ( material == 2 ) { ",
  545. "vec3 normal = normalize( vNormal );",
  546. "vec3 viewPosition = normalize( vViewPosition );",
  547. // point lights
  548. maxPointLights ? "vec4 pointDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );" : "",
  549. maxPointLights ? "vec4 pointSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );" : "",
  550. maxPointLights ? "for( int i = 0; i < pointLightNumber; i++ ) {" : "",
  551. maxPointLights ? "vec3 pointVector = normalize( vPointLightVector[ i ] );" : "",
  552. maxPointLights ? "vec3 pointHalfVector = normalize( vPointLightVector[ i ] + vViewPosition );" : "",
  553. maxPointLights ? "float pointDotNormalHalf = dot( normal, pointHalfVector );" : "",
  554. maxPointLights ? "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );" : "",
  555. // Ternary conditional is from the original o3d shader. Here it produces abrupt dark cutoff artefacts.
  556. // Using just pow works ok in Chrome, but makes different artefact in Firefox 4.
  557. // Zeroing on negative pointDotNormalHalf seems to work in both.
  558. //"float specularCompPoint = dot( normal, pointVector ) < 0.0 || pointDotNormalHalf < 0.0 ? 0.0 : pow( pointDotNormalHalf, mShininess );",
  559. //"float specularCompPoint = pow( pointDotNormalHalf, mShininess );",
  560. //"float pointSpecularWeight = pointDotNormalHalf < 0.0 ? 0.0 : pow( pointDotNormalHalf, mShininess );",
  561. // Ternary conditional inside for loop breaks Chrome shader linking.
  562. // Must do it with if.
  563. maxPointLights ? "float pointSpecularWeight = 0.0;" : "",
  564. maxPointLights ? "if ( pointDotNormalHalf >= 0.0 )" : "",
  565. maxPointLights ? "pointSpecularWeight = pow( pointDotNormalHalf, mShininess );" : "",
  566. maxPointLights ? "pointDiffuse += mColor * pointDiffuseWeight;" : "",
  567. maxPointLights ? "pointSpecular += mSpecular * pointSpecularWeight;" : "",
  568. maxPointLights ? "}" : "",
  569. // directional lights
  570. maxDirLights ? "vec4 dirDiffuse = vec4( 0.0, 0.0, 0.0, 0.0 );" : "",
  571. maxDirLights ? "vec4 dirSpecular = vec4( 0.0, 0.0, 0.0, 0.0 );" : "",
  572. maxDirLights ? "for( int i = 0; i < directionalLightNumber; i++ ) {" : "",
  573. maxDirLights ? "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );" : "",
  574. maxDirLights ? "vec3 dirVector = normalize( lDirection.xyz );" : "",
  575. maxDirLights ? "vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );" : "",
  576. maxDirLights ? "float dirDotNormalHalf = dot( normal, dirHalfVector );" : "",
  577. maxDirLights ? "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );" : "",
  578. maxDirLights ? "float dirSpecularWeight = 0.0;" : "",
  579. maxDirLights ? "if ( dirDotNormalHalf >= 0.0 )" : "",
  580. maxDirLights ? "dirSpecularWeight = pow( dirDotNormalHalf, mShininess );" : "",
  581. maxDirLights ? "dirDiffuse += mColor * dirDiffuseWeight;" : "",
  582. maxDirLights ? "dirSpecular += mSpecular * dirSpecularWeight;" : "",
  583. maxDirLights ? "}" : "",
  584. // all lights contribution summation
  585. "vec4 totalLight = mAmbient;",
  586. maxDirLights ? "totalLight += dirDiffuse + dirSpecular;" : "",
  587. maxPointLights ? "totalLight += pointDiffuse + pointSpecular;" : "",
  588. // looks nicer with weighting
  589. "gl_FragColor = vec4( mapColor.rgb * cubeColor.rgb * totalLight.xyz * vLightWeighting, mapColor.a );",
  590. // Lambert: diffuse lighting
  591. "} else if ( material == 1 ) {",
  592. "gl_FragColor = vec4( mColor.rgb * mapColor.rgb * cubeColor.rgb * vLightWeighting, mColor.a * mapColor.a );",
  593. // Basic: unlit color / texture
  594. "} else {",
  595. "gl_FragColor = mColor * mapColor * cubeColor;",
  596. "}",
  597. "}" ];
  598. return chunks.join("\n");
  599. };
  600. function generateVertexShader( maxDirLights, maxPointLights ) {
  601. var chunks = [
  602. maxDirLights ? "#define MAX_DIR_LIGHTS " + maxDirLights : "",
  603. maxPointLights ? "#define MAX_POINT_LIGHTS " + maxPointLights : "",
  604. "attribute vec3 position;",
  605. "attribute vec3 normal;",
  606. "attribute vec2 uv;",
  607. "uniform vec3 cameraPosition;",
  608. "uniform bool enableLighting;",
  609. "uniform int pointLightNumber;",
  610. "uniform int directionalLightNumber;",
  611. "uniform vec3 ambientLightColor;",
  612. maxDirLights ? "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];" : "",
  613. maxDirLights ? "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];" : "",
  614. maxPointLights ? "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];" : "",
  615. maxPointLights ? "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];" : "",
  616. "uniform mat4 objMatrix;",
  617. "uniform mat4 viewMatrix;",
  618. "uniform mat4 modelViewMatrix;",
  619. "uniform mat4 projectionMatrix;",
  620. "uniform mat3 normalMatrix;",
  621. "varying vec3 vNormal;",
  622. "varying vec2 vUv;",
  623. "varying vec3 vLightWeighting;",
  624. maxPointLights ? "varying vec3 vPointLightVector[ MAX_POINT_LIGHTS ];" : "",
  625. "varying vec3 vViewPosition;",
  626. "varying vec3 vReflect;",
  627. "void main(void) {",
  628. // world space
  629. "vec4 mPosition = objMatrix * vec4( position, 1.0 );",
  630. "vViewPosition = cameraPosition - mPosition.xyz;",
  631. "vec3 nWorld = mat3(objMatrix) * normal;",
  632. // eye space
  633. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  634. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  635. "if ( !enableLighting ) {",
  636. "vLightWeighting = vec3( 1.0, 1.0, 1.0 );",
  637. "} else {",
  638. "vLightWeighting = ambientLightColor;",
  639. // directional lights
  640. maxDirLights ? "for( int i = 0; i < directionalLightNumber; i++ ) {" : "",
  641. maxDirLights ? "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );" : "",
  642. maxDirLights ? "float directionalLightWeighting = max( dot( transformedNormal, normalize(lDirection.xyz ) ), 0.0 );" : "",
  643. maxDirLights ? "vLightWeighting += directionalLightColor[ i ] * directionalLightWeighting;" : "",
  644. maxDirLights ? "}" : "",
  645. // point lights
  646. maxPointLights ? "for( int i = 0; i < pointLightNumber; i++ ) {" : "",
  647. maxPointLights ? "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );" : "",
  648. maxPointLights ? "vPointLightVector[ i ] = normalize( lPosition.xyz - mvPosition.xyz );" : "",
  649. maxPointLights ? "float pointLightWeighting = max( dot( transformedNormal, vPointLightVector[ i ] ), 0.0 );" : "",
  650. maxPointLights ? "vLightWeighting += pointLightColor[ i ] * pointLightWeighting;" : "",
  651. maxPointLights ? "}" : "",
  652. "}",
  653. "vNormal = transformedNormal;",
  654. "vUv = uv;",
  655. "vReflect = reflect( normalize(mPosition.xyz - cameraPosition), normalize(nWorld.xyz) );",
  656. "gl_Position = projectionMatrix * mvPosition;",
  657. "}" ];
  658. return chunks.join("\n");
  659. };
  660. function initProgram( maxDirLights, maxPointLights ) {
  661. _program = _gl.createProgram();
  662. //log ( generateVertexShader( maxDirLights, maxPointLights ) );
  663. //log ( generateFragmentShader( maxDirLights, maxPointLights ) );
  664. _gl.attachShader( _program, getShader( "fragment", generateFragmentShader( maxDirLights, maxPointLights ) ) );
  665. _gl.attachShader( _program, getShader( "vertex", generateVertexShader( maxDirLights, maxPointLights ) ) );
  666. _gl.linkProgram( _program );
  667. if ( !_gl.getProgramParameter( _program, _gl.LINK_STATUS ) ) {
  668. alert( "Could not initialise shaders" );
  669. //alert( "VALIDATE_STATUS: " + _gl.getProgramParameter( _program, _gl.VALIDATE_STATUS ) );
  670. //alert( _gl.getError() );
  671. }
  672. _gl.useProgram( _program );
  673. // matrices
  674. _program.viewMatrix = _gl.getUniformLocation( _program, "viewMatrix" );
  675. _program.modelViewMatrix = _gl.getUniformLocation( _program, "modelViewMatrix" );
  676. _program.projectionMatrix = _gl.getUniformLocation( _program, "projectionMatrix" );
  677. _program.normalMatrix = _gl.getUniformLocation( _program, "normalMatrix" );
  678. _program.objMatrix = _gl.getUniformLocation( _program, "objMatrix" );
  679. _program.cameraPosition = _gl.getUniformLocation( _program, 'cameraPosition' );
  680. // lights
  681. _program.enableLighting = _gl.getUniformLocation( _program, 'enableLighting' );
  682. _program.ambientLightColor = _gl.getUniformLocation( _program, 'ambientLightColor' );
  683. if ( maxDirLights ) {
  684. _program.directionalLightNumber = _gl.getUniformLocation( _program, 'directionalLightNumber' );
  685. _program.directionalLightColor = _gl.getUniformLocation( _program, 'directionalLightColor' );
  686. _program.directionalLightDirection = _gl.getUniformLocation( _program, 'directionalLightDirection' );
  687. }
  688. if ( maxPointLights ) {
  689. _program.pointLightNumber = _gl.getUniformLocation( _program, 'pointLightNumber' );
  690. _program.pointLightColor = _gl.getUniformLocation( _program, 'pointLightColor' );
  691. _program.pointLightPosition = _gl.getUniformLocation( _program, 'pointLightPosition' );
  692. }
  693. // material
  694. _program.material = _gl.getUniformLocation( _program, 'material' );
  695. // material properties (Basic / Lambert / Blinn-Phong shader)
  696. _program.mColor = _gl.getUniformLocation( _program, 'mColor' );
  697. _program.mOpacity = _gl.getUniformLocation( _program, 'mOpacity' );
  698. _program.mReflectivity = _gl.getUniformLocation( _program, 'mReflectivity' );
  699. // material properties (Blinn-Phong shader)
  700. _program.mAmbient = _gl.getUniformLocation( _program, 'mAmbient' );
  701. _program.mSpecular = _gl.getUniformLocation( _program, 'mSpecular' );
  702. _program.mShininess = _gl.getUniformLocation( _program, 'mShininess' );
  703. // texture (diffuse map)
  704. _program.enableMap = _gl.getUniformLocation( _program, "enableMap" );
  705. _gl.uniform1i( _program.enableMap, 0 );
  706. _program.tMap = _gl.getUniformLocation( _program, "tMap" );
  707. _gl.uniform1i( _program.tMap, 0 );
  708. // cube texture
  709. _program.enableCubeMap = _gl.getUniformLocation( _program, "enableCubeMap" );
  710. _gl.uniform1i( _program.enableCubeMap, 0 );
  711. _program.tCube = _gl.getUniformLocation( _program, "tCube" );
  712. _gl.uniform1i( _program.tCube, 1 ); // it's important to use non-zero texture unit, otherwise it doesn't work
  713. // material properties (Depth)
  714. _program.m2Near = _gl.getUniformLocation( _program, 'm2Near' );
  715. _program.mFarPlusNear = _gl.getUniformLocation( _program, 'mFarPlusNear' );
  716. _program.mFarMinusNear = _gl.getUniformLocation( _program, 'mFarMinusNear' );
  717. // vertex arrays
  718. _program.position = _gl.getAttribLocation( _program, "position" );
  719. _gl.enableVertexAttribArray( _program.position );
  720. _program.normal = _gl.getAttribLocation( _program, "normal" );
  721. _gl.enableVertexAttribArray( _program.normal );
  722. _program.uv = _gl.getAttribLocation( _program, "uv" );
  723. _gl.enableVertexAttribArray( _program.uv );
  724. _program.viewMatrixArray = new Float32Array(16);
  725. _program.modelViewMatrixArray = new Float32Array(16);
  726. _program.projectionMatrixArray = new Float32Array(16);
  727. };
  728. function getShader( type, string ) {
  729. var shader;
  730. if ( type == "fragment" ) {
  731. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  732. } else if ( type == "vertex" ) {
  733. shader = _gl.createShader( _gl.VERTEX_SHADER );
  734. }
  735. _gl.shaderSource( shader, string );
  736. _gl.compileShader( shader );
  737. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  738. alert( _gl.getShaderInfoLog( shader ) );
  739. return null;
  740. }
  741. return shader;
  742. };
  743. /* DEBUG
  744. function getGLParams() {
  745. var params = {
  746. 'MAX_VARYING_VECTORS': _gl.getParameter( _gl.MAX_VARYING_VECTORS ),
  747. 'MAX_VERTEX_ATTRIBS': _gl.getParameter( _gl.MAX_VERTEX_ATTRIBS ),
  748. 'MAX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS ),
  749. 'MAX_VERTEX_TEXTURE_IMAGE_UNITS': _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  750. 'MAX_COMBINED_TEXTURE_IMAGE_UNITS' : _gl.getParameter( _gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS ),
  751. 'MAX_VERTEX_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS ),
  752. 'MAX_FRAGMENT_UNIFORM_VECTORS': _gl.getParameter( _gl.MAX_FRAGMENT_UNIFORM_VECTORS )
  753. }
  754. return params;
  755. };
  756. function dumpObject( obj ) {
  757. var p, str = "";
  758. for ( p in obj ) {
  759. str += p + ": " + obj[p] + "\n";
  760. }
  761. return str;
  762. }
  763. */
  764. };