WebGLRenderer.js 38 KB

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