WebGLRenderer2.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. *
  4. * Based on code by:
  5. * @author alteredq / http://alteredqualia.com/
  6. * @author supereggbert / http://www.paulbrunt.co.uk/
  7. */
  8. THREE.WebGLRenderer2 = function ( antialias ) {
  9. var _renderList = null,
  10. _projector = new THREE.Projector(),
  11. _canvas = document.createElement( 'canvas' ),
  12. _gl, _currentProgram,
  13. _modelViewMatrix = new THREE.Matrix4(),
  14. _normalMatrix = new THREE.Matrix4(),
  15. _viewMatrixArray = new Float32Array( 16 ),
  16. _modelViewMatrixArray = new Float32Array( 16 ),
  17. _projectionMatrixArray = new Float32Array( 16 ),
  18. _normalMatrixArray = new Float32Array( 9 ),
  19. _objectMatrixArray = new Float32Array( 16 );
  20. try {
  21. antialias = antialias !== undefined ? antialias : true;
  22. _gl = _canvas.getContext( 'experimental-webgl', { antialias: antialias } );
  23. } catch(e) { }
  24. if ( !_gl ) {
  25. alert("WebGL not supported");
  26. throw "cannot create webgl context";
  27. }
  28. _gl.clearColor( 0, 0, 0, 1 );
  29. _gl.clearDepth( 1 );
  30. _gl.enable( _gl.DEPTH_TEST );
  31. _gl.depthFunc( _gl.LEQUAL );
  32. _gl.frontFace( _gl.CCW );
  33. _gl.cullFace( _gl.BACK );
  34. _gl.enable( _gl.CULL_FACE );
  35. _gl.enable( _gl.BLEND );
  36. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  37. _gl.clearColor( 0, 0, 0, 0 );
  38. this.domElement = _canvas;
  39. this.autoClear = true;
  40. this.sortObjects = true;
  41. this.setSize = function ( width, height ) {
  42. _canvas.width = width;
  43. _canvas.height = height;
  44. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  45. };
  46. this.clear = function () {
  47. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  48. };
  49. this._m33 = new THREE.Matrix3();
  50. this.render = function( scene, camera ) {
  51. var o, ol;
  52. this.autoClear && this.clear();
  53. camera.autoUpdateMatrix && camera.updateMatrix();
  54. // Setup camera matrices
  55. _viewMatrixArray.set( camera.matrix.flatten() );
  56. _projectionMatrixArray.set( camera.projectionMatrix.flatten() );
  57. _currentProgram = null;
  58. /*
  59. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  60. renderObject( scene.objects[ o ] );
  61. }
  62. */
  63. _renderList = _projector.projectObjects( scene, camera, this.sortObjects );
  64. for ( o = 0, ol = _renderList.length; o < ol; o++ ) {
  65. renderObject( _renderList[ o ].object );
  66. }
  67. function renderObject( object ) {
  68. var geometry, material, m, ml,
  69. program, uniforms, attributes;
  70. object.autoUpdateMatrix && object.updateMatrix();
  71. // Setup object matrices
  72. _objectMatrixArray.set( object.matrix.flatten() );
  73. _modelViewMatrix.multiply( camera.matrix, object.matrix );
  74. _modelViewMatrixArray.set( _modelViewMatrix.flatten() );
  75. _normalMatrix = THREE.Matrix4.makeInvert3x3( _modelViewMatrix, this._m33 ).transpose();
  76. _normalMatrixArray.set( _normalMatrix.m );
  77. if ( object instanceof THREE.Mesh ) {
  78. geometry = object.geometry;
  79. if ( geometry.__webglBuffers == undefined ) {
  80. if ( buildBuffers( geometry ) == false ) return;
  81. }
  82. for ( m = 0, ml = object.materials.length; m < ml; m ++ ) {
  83. material = object.materials[ m ];
  84. if ( material.__webglProgram == undefined ) {
  85. if ( createProgram( material ) == false ) continue;
  86. }
  87. program = material.__webglProgram;
  88. uniforms = program.uniforms;
  89. attributes = program.attributes;
  90. if( program != _currentProgram ) {
  91. _currentProgram = program;
  92. _gl.useProgram( program );
  93. // scene
  94. if ( scene.fog ) {
  95. _gl.uniform1f( uniforms.fog, 1 );
  96. _gl.uniform1f( uniforms.fogNear, scene.fog.near );
  97. _gl.uniform1f( uniforms.fogFar, scene.fog.far );
  98. _gl.uniform3f( uniforms.fogColor, scene.fog.color.r, scene.fog.color.g, scene.fog.color.b );
  99. } else {
  100. _gl.uniform1f( uniforms.fog, 0 );
  101. }
  102. // material
  103. if ( material instanceof THREE.MeshBasicMaterial ||
  104. material instanceof THREE.MeshLambertMaterial ||
  105. material instanceof THREE.MeshPhongMaterial ) {
  106. _gl.uniform3f( uniforms.mColor, material.color.r, material.color.g, material.color.b );
  107. _gl.uniform1f( uniforms.mOpacity, material.opacity );
  108. if ( material.map ) {
  109. setTexture( material.map, 0 );
  110. _gl.uniform1i( uniforms.tMap, 0 );
  111. }
  112. if ( material.env_map ) {
  113. setTexture( material.env_map, 1 );
  114. _gl.uniform1i( uniforms.tSpherical, 1 );
  115. }
  116. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  117. _gl.uniform1f( uniforms.mOpacity, material.opacity );
  118. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  119. _gl.uniform1f( uniforms.mNear, camera.near );
  120. _gl.uniform1f( uniforms.mFar, camera.far );
  121. _gl.uniform1f( uniforms.mOpacity, material.opacity );
  122. }
  123. _gl.uniform3f( uniforms.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  124. _gl.uniformMatrix4fv( uniforms.viewMatrix, false, _viewMatrixArray );
  125. _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, _projectionMatrixArray );
  126. }
  127. _gl.uniformMatrix4fv( uniforms.objectMatrix, false, _objectMatrixArray );
  128. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, _modelViewMatrixArray );
  129. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, _normalMatrixArray );
  130. var buffer, buffers = geometry.__webglBuffers;
  131. for ( var i = 0, l = buffers.length; i < l; i ++ ) {
  132. buffer = buffers[ i ];
  133. // vertices
  134. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.vertices );
  135. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  136. _gl.enableVertexAttribArray( attributes.position );
  137. // normals
  138. if ( attributes.normal >= 0 ) {
  139. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.normals );
  140. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  141. _gl.enableVertexAttribArray( attributes.normal );
  142. }
  143. // uvs
  144. if ( attributes.uv >= 0 ) {
  145. if ( buffer.uvs ) {
  146. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.uvs );
  147. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  148. _gl.enableVertexAttribArray( attributes.uv );
  149. } else {
  150. _gl.disableVertexAttribArray( attributes.uv );
  151. }
  152. }
  153. // render triangles
  154. if ( ! material.wireframe ) {
  155. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.faces );
  156. _gl.drawElements( _gl.TRIANGLES, buffer.faceCount, _gl.UNSIGNED_SHORT, 0 );
  157. // render lines
  158. } else {
  159. _gl.lineWidth( material.wireframe_linewidth );
  160. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.lines );
  161. _gl.drawElements( _gl.LINES, buffer.lineCount, _gl.UNSIGNED_SHORT, 0 );
  162. }
  163. }
  164. }
  165. } else if ( object instanceof THREE.Line ) {
  166. } else if ( object instanceof THREE.Particle ) {
  167. }
  168. }
  169. // Buffers
  170. function buildBuffers( geometry ) {
  171. var itemCount = 0, vertexIndex, group,
  172. f, fl, face, v1, v2, v3, v4, vertexNormals, faceNormal, normal, uv,
  173. vertexGroups = [], faceGroups = [], lineGroups = [], normalGroups = [], uvGroups = [],
  174. vertices, faces, lines, normals, uvs;
  175. for ( f = 0, fl = geometry.faces.length; f < fl; f++ ) {
  176. face = geometry.faces[ f ];
  177. uv = geometry.uvs[ f ];
  178. vertexNormals = face.vertexNormals;
  179. faceNormal = face.normal;
  180. if ( face instanceof THREE.Face3 ) {
  181. itemCount += 3;
  182. group = Math.floor( itemCount / 65535 );
  183. if ( !vertexGroups[ group ] ) {
  184. vertexIndex = 0;
  185. vertices = vertexGroups[ group ] = [];
  186. normals = normalGroups[ group ] = [];
  187. uvs = uvGroups[ group ] = [];
  188. faces = faceGroups[ group ] = [];
  189. lines = lineGroups[ group ] = [];
  190. }
  191. v1 = geometry.vertices[ face.a ].position;
  192. v2 = geometry.vertices[ face.b ].position;
  193. v3 = geometry.vertices[ face.c ].position;
  194. vertices.push( v1.x, v1.y, v1.z );
  195. vertices.push( v2.x, v2.y, v2.z );
  196. vertices.push( v3.x, v3.y, v3.z );
  197. if ( vertexNormals.length == 3 ) {
  198. for ( i = 0; i < 3; i ++ ) {
  199. normals.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
  200. }
  201. } else {
  202. for ( i = 0; i < 3; i ++ ) {
  203. normals.push( faceNormal.x, faceNormal.y, faceNormal.z );
  204. }
  205. }
  206. if ( uv ) {
  207. for ( i = 0; i < 3; i ++ ) {
  208. uvs.push( uv[ i ].u, uv[ i ].v );
  209. }
  210. }
  211. faces.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  212. // TODO: don't add lines that already exist (faces sharing edge)
  213. lines.push( vertexIndex, vertexIndex + 1 );
  214. lines.push( vertexIndex, vertexIndex + 2 );
  215. lines.push( vertexIndex + 1, vertexIndex + 2 );
  216. vertexIndex += 3;
  217. } else if ( face instanceof THREE.Face4 ) {
  218. itemCount += 4;
  219. group = Math.floor( itemCount / 65535 );
  220. if ( !vertexGroups[ group ] ) {
  221. vertexIndex = 0;
  222. vertices = vertexGroups[ group ] = [];
  223. normals = normalGroups[ group ] = [];
  224. uvs = uvGroups[ group ] = [];
  225. faces = faceGroups[ group ] = [];
  226. lines = lineGroups[ group ] = [];
  227. }
  228. v1 = geometry.vertices[ face.a ].position;
  229. v2 = geometry.vertices[ face.b ].position;
  230. v3 = geometry.vertices[ face.c ].position;
  231. v4 = geometry.vertices[ face.d ].position;
  232. vertices.push( v1.x, v1.y, v1.z );
  233. vertices.push( v2.x, v2.y, v2.z );
  234. vertices.push( v3.x, v3.y, v3.z );
  235. vertices.push( v4.x, v4.y, v4.z );
  236. if ( vertexNormals.length == 4 ) {
  237. for ( i = 0; i < 4; i ++ ) {
  238. normals.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
  239. }
  240. } else {
  241. for ( i = 0; i < 4; i ++ ) {
  242. normals.push( faceNormal.x, faceNormal.y, faceNormal.z );
  243. }
  244. }
  245. if ( uv ) {
  246. for ( i = 0; i < 4; i ++ ) {
  247. uvs.push( uv[ i ].u, uv[ i ].v );
  248. }
  249. }
  250. faces.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  251. faces.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
  252. // TODO: don't add lines that already exist (faces sharing edge)
  253. lines.push( vertexIndex, vertexIndex + 1 );
  254. lines.push( vertexIndex, vertexIndex + 3 );
  255. lines.push( vertexIndex + 1, vertexIndex + 2 );
  256. lines.push( vertexIndex + 2, vertexIndex + 3 );
  257. vertexIndex += 4;
  258. }
  259. }
  260. if ( !vertices.length ) return false;
  261. var buffer, buffers = [];
  262. for ( var i = 0, l = group; i <= l; i ++ ) {
  263. buffer = {
  264. vertices: null,
  265. faces: null,
  266. faceCount: faceGroups[ i ].length,
  267. normals: null,
  268. lines: null,
  269. lineCount: lineGroups[ i ].length,
  270. uvs: null
  271. };
  272. buffer.vertices = _gl.createBuffer();
  273. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.vertices );
  274. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexGroups[ i ] ), _gl.STATIC_DRAW );
  275. buffer.normals = _gl.createBuffer();
  276. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.normals );
  277. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalGroups[ i ] ), _gl.STATIC_DRAW );
  278. if ( uvs.length > 0 ) {
  279. buffer.uvs = _gl.createBuffer();
  280. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffer.uvs );
  281. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvGroups[ i ] ), _gl.STATIC_DRAW );
  282. }
  283. buffer.faces = _gl.createBuffer();
  284. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.faces );
  285. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faceGroups[ i ] ), _gl.STATIC_DRAW );
  286. buffer.lines = _gl.createBuffer();
  287. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffer.lines );
  288. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( lineGroups[ i ] ), _gl.STATIC_DRAW );
  289. buffers.push( buffer );
  290. }
  291. geometry.__webglBuffers = buffers;
  292. return true;
  293. }
  294. // Shaders
  295. function createProgram( material ) {
  296. var vs, fs, identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ];
  297. if ( material instanceof THREE.MeshBasicMaterial ||
  298. material instanceof THREE.MeshLambertMaterial ||
  299. material instanceof THREE.MeshPhongMaterial ) {
  300. vs = [
  301. material.map ? 'varying vec2 vUv;' : null,
  302. material.env_map ? 'varying vec2 vSpherical;' : null,
  303. 'void main() {',
  304. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  305. material.map ? 'vUv = uv;' : null,
  306. material.env_map ? 'vec3 u = normalize( modelViewMatrix * vec4( position, 1.0 ) ).xyz;' : null,
  307. material.env_map ? 'vec3 n = normalize( normalMatrix * normal );' : null,
  308. material.env_map ? 'vec3 r = reflect( u, n );' : null,
  309. material.env_map ? 'float m = 2.0 * sqrt( r.x * r.x + r.y * r.y + ( r.z + 1.0 ) * ( r.z + 1.0 ) );' : null,
  310. material.env_map ? 'vSpherical.x = r.x / m + 0.5;' : null,
  311. material.env_map ? 'vSpherical.y = - r.y / m + 0.5;' : null,
  312. '}'
  313. ].filter( removeNull ).join( '\n' );
  314. fs = [
  315. 'uniform vec3 mColor;',
  316. 'uniform float mOpacity;',
  317. material.map ? 'uniform sampler2D tMap;' : null,
  318. material.map ? 'varying vec2 vUv;' : null,
  319. material.env_map ? 'uniform sampler2D tSpherical;' : null,
  320. material.env_map ? 'varying vec2 vSpherical;' : null,
  321. material.fog ? 'uniform float fog;' : null,
  322. material.fog ? 'uniform float fogNear;' : null,
  323. material.fog ? 'uniform float fogFar;' : null,
  324. material.fog ? 'uniform vec3 fogColor;' : null,
  325. 'void main() {',
  326. 'gl_FragColor = vec4( mColor.xyz, mOpacity );',
  327. /* Premultiply alpha
  328. material.map ? 'vec4 mapColor = texture2D( tMap, vUv );' : null,
  329. material.map ? 'mapColor.xyz *= mapColor.w;' : null,
  330. */
  331. material.map ? 'gl_FragColor *= texture2D( tMap, vUv );' : null,
  332. material.env_map ? 'gl_FragColor += texture2D( tSpherical, vSpherical );' : null,
  333. material.fog ? 'float depth = gl_FragCoord.z / gl_FragCoord.w;' : null,
  334. material.fog ? 'float fogFactor = fog * smoothstep( fogNear, fogFar, depth );' : null,
  335. material.fog ? 'gl_FragColor = mix( gl_FragColor, vec4( fogColor, 1.0 ), fogFactor );' : null,
  336. '}'
  337. ].filter( removeNull ).join( '\n' );
  338. identifiers.push( 'mColor', 'mOpacity' );
  339. if ( material.map ) identifiers.push( 'tMap' );
  340. if ( material.env_map ) identifiers.push( 'tSpherical' );
  341. if ( material.fog ) identifiers.push( 'fog', 'fogColor', 'fogNear', 'fogFar' );
  342. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  343. vs = [
  344. 'varying vec3 vNormal;',
  345. 'void main() {',
  346. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  347. 'vNormal = normalize( normalMatrix * normal );',
  348. '}'
  349. ].join( '\n' );
  350. fs = [
  351. 'uniform float mOpacity;',
  352. 'varying vec3 vNormal;',
  353. 'void main() {',
  354. 'gl_FragColor = vec4( ( normalize( vNormal ) + 1.0 ) * 0.5, mOpacity );',
  355. '}'
  356. ].join( '\n' );
  357. identifiers.push( 'mOpacity' );
  358. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  359. vs = [
  360. 'void main() {',
  361. 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  362. '}'
  363. ].join( '\n' );
  364. fs = [
  365. 'uniform float mNear;',
  366. 'uniform float mFar;',
  367. 'uniform float mOpacity;',
  368. 'void main() {',
  369. 'float depth = gl_FragCoord.z / gl_FragCoord.w;',
  370. 'float color = 1.0 - smoothstep( mNear, mFar, depth );',
  371. 'gl_FragColor = vec4( vec3( color ), 1.0 );',
  372. '}'
  373. ].join( '\n' );
  374. identifiers.push( 'mNear', 'mFar', 'mOpacity' );
  375. } else if ( material instanceof THREE.MeshShaderMaterial ) {
  376. vs = material.vertex_shader;
  377. fs = material.fragment_shader;
  378. for( uniform in material.uniforms ) {
  379. identifiers.push( uniform );
  380. }
  381. } else {
  382. return false;
  383. }
  384. material.__webglProgram = compileProgram( vs, fs );
  385. cacheUniformLocations( material.__webglProgram, identifiers );
  386. cacheAttributeLocations( material.__webglProgram, [ "position", "normal", "uv"/*, "tangent"*/ ] );
  387. return true;
  388. }
  389. function compileProgram( vertex_shader, fragment_shader ) {
  390. var program = _gl.createProgram(), shader, prefix_vertex, prefix_fragment;
  391. prefix_vertex = [
  392. maxVertexTextures() > 0 ? "#define VERTEX_TEXTURES" : "",
  393. "uniform mat4 objectMatrix;",
  394. "uniform mat4 modelViewMatrix;",
  395. "uniform mat4 projectionMatrix;",
  396. //"uniform mat4 viewMatrix;",
  397. "uniform mat3 normalMatrix;",
  398. "uniform vec3 cameraPosition;",
  399. "attribute vec3 position;",
  400. "attribute vec3 normal;",
  401. "attribute vec2 uv;",
  402. ""
  403. ].join("\n"),
  404. prefix_fragment = [
  405. "#ifdef GL_ES",
  406. "precision highp float;",
  407. "#endif",
  408. //"uniform mat4 viewMatrix;",
  409. ""
  410. ].join("\n");
  411. // Vertex shader
  412. shader = _gl.createShader( _gl.VERTEX_SHADER );
  413. _gl.shaderSource( shader, prefix_vertex + vertex_shader );
  414. _gl.compileShader( shader );
  415. _gl.attachShader( program, shader );
  416. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  417. alert( _gl.getShaderInfoLog( shader ) );
  418. return null;
  419. }
  420. // Fragment Shader
  421. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  422. _gl.shaderSource( shader, prefix_fragment + fragment_shader );
  423. _gl.compileShader( shader );
  424. _gl.attachShader( program, shader );
  425. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  426. alert( _gl.getShaderInfoLog( shader ) );
  427. return null;
  428. }
  429. _gl.linkProgram( program );
  430. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  431. alert( "Could not initialise shaders.\n" +
  432. "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + "\n" +
  433. "ERROR: " + _gl.getError() + "\n\n" +
  434. "- Vertex Shader -\n" + prefix_vertex + vertex_shader + "\n\n" +
  435. "- Fragment Shader -\n" + prefix_fragment + fragment_shader );
  436. }
  437. program.uniforms = {};
  438. program.attributes = {};
  439. return program;
  440. }
  441. function cacheUniformLocations( program, identifiers ) {
  442. var i, l, id;
  443. for( i = 0, l = identifiers.length; i < l; i++ ) {
  444. id = identifiers[ i ];
  445. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  446. }
  447. }
  448. function cacheAttributeLocations( program, identifiers ) {
  449. var i, l, id;
  450. for( i = 0, l = identifiers.length; i < l; i++ ) {
  451. id = identifiers[ i ];
  452. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  453. if ( program.attributes[ id ] >= 0 ) {
  454. _gl.enableVertexAttribArray( program.attributes[ id ] );
  455. }
  456. }
  457. }
  458. // Textures
  459. function setTexture( texture, slot ) {
  460. if ( !texture.__webglTexture && texture.image.loaded ) {
  461. texture.__webglTexture = _gl.createTexture();
  462. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  463. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  464. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrap_s ) );
  465. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrap_t ) );
  466. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.mag_filter ) );
  467. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.min_filter ) );
  468. _gl.generateMipmap( _gl.TEXTURE_2D );
  469. _gl.bindTexture( _gl.TEXTURE_2D, null );
  470. }
  471. _gl.activeTexture( _gl.TEXTURE0 + slot );
  472. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  473. }
  474. function maxVertexTextures() {
  475. return _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  476. };
  477. function paramThreeToGL( p ) {
  478. switch ( p ) {
  479. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  480. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  481. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  482. case THREE.NearestFilter: return _gl.NEAREST; break;
  483. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  484. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  485. case THREE.LinearFilter: return _gl.LINEAR; break;
  486. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  487. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  488. }
  489. return 0;
  490. }
  491. function removeNull( element ) {
  492. return element !== null;
  493. }
  494. };
  495. };