WebGLRenderer2.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.WebGLRenderer2 = function () {
  5. var _canvas = document.createElement( 'canvas' ),
  6. _gl, _currentProgram,
  7. _modelViewMatrix = new THREE.Matrix4(),
  8. _viewMatrixArray = new Float32Array( 16 ),
  9. _modelViewMatrixArray = new Float32Array( 16 ),
  10. _projectionMatrixArray = new Float32Array( 16 ),
  11. _normalMatrixArray = new Float32Array( 9 ),
  12. _objectMatrixArray = new Float32Array( 16 );
  13. try {
  14. _gl = _canvas.getContext( 'experimental-webgl', { antialias: true } );
  15. } catch(e) { }
  16. if ( !_gl ) {
  17. alert("WebGL not supported");
  18. throw "cannot create webgl context";
  19. }
  20. _gl.clearColor( 0, 0, 0, 1 );
  21. _gl.clearDepth( 1 );
  22. _gl.enable( _gl.DEPTH_TEST );
  23. _gl.depthFunc( _gl.LEQUAL );
  24. _gl.frontFace( _gl.CCW );
  25. _gl.cullFace( _gl.BACK );
  26. _gl.enable( _gl.CULL_FACE );
  27. _gl.enable( _gl.BLEND );
  28. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  29. _gl.clearColor( 0, 0, 0, 0 );
  30. this.domElement = _canvas;
  31. this.autoClear = true;
  32. this.setSize = function ( width, height ) {
  33. _canvas.width = width;
  34. _canvas.height = height;
  35. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  36. };
  37. this.clear = function () {
  38. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  39. };
  40. this.render = function( scene, camera ) {
  41. var o, ol;
  42. this.autoClear && this.clear();
  43. camera.autoUpdateMatrix && camera.updateMatrix();
  44. // Setup camera matrices
  45. _viewMatrixArray.set( camera.matrix.flatten() );
  46. _projectionMatrixArray.set( camera.projectionMatrix.flatten() );
  47. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  48. object = scene.objects[ o ];
  49. if ( object.visible ) {
  50. renderObject( object );
  51. }
  52. }
  53. function renderObject( object ) {
  54. var geometry, material, m, nl,
  55. program, attributes;
  56. object.autoUpdateMatrix && object.updateMatrix();
  57. // Setup object matrices
  58. _objectMatrixArray.set( object.matrix.flatten() );
  59. _modelViewMatrix.multiply( camera.matrix, object.matrix );
  60. _modelViewMatrixArray.set( _modelViewMatrix.flatten() );
  61. _normalMatrix = THREE.Matrix4.makeInvert3x3( _modelViewMatrix ).transpose();
  62. _normalMatrixArray.set( _normalMatrix.m );
  63. if ( object instanceof THREE.Mesh ) {
  64. geometry = object.geometry;
  65. if ( geometry.__webglBuffers == undefined ) {
  66. if ( buildBuffers( geometry ) == false ) return;
  67. }
  68. for ( m = 0, ml = object.material.length; m < ml; m ++ ) {
  69. material = object.material[ m ];
  70. if ( material.__webglProgram == undefined ) {
  71. if ( createProgram( material ) == false ) continue;
  72. }
  73. program = material.__webglProgram;
  74. attributes = program.attributes;
  75. if( program != _currentProgram ) {
  76. _gl.useProgram( program );
  77. _currentProgram = program;
  78. }
  79. if ( material instanceof THREE.MeshBasicMaterial ||
  80. material instanceof THREE.MeshLambertMaterial ||
  81. material instanceof THREE.MeshPhongMaterial ) {
  82. _gl.uniform3f( program.uniforms.mColor, material.color.r, material.color.g, material.color.b );
  83. _gl.uniform1f( program.uniforms.mOpacity, material.opacity );
  84. if ( material.map ) {
  85. setTexture( material.map, 0 );
  86. _gl.uniform1i( program.uniforms.tMap, 0 );
  87. }
  88. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  89. _gl.uniform1f( program.uniforms.mOpacity, material.opacity );
  90. }
  91. _gl.uniform3f( program.uniforms.cameraPosition, camera.position.x, camera.position.y, camera.position.z );
  92. _gl.uniformMatrix4fv( program.uniforms.viewMatrix, false, _viewMatrixArray );
  93. _gl.uniformMatrix4fv( program.uniforms.projectionMatrix, false, _projectionMatrixArray );
  94. _gl.uniformMatrix4fv( program.uniforms.objectMatrix, false, _objectMatrixArray );
  95. _gl.uniformMatrix4fv( program.uniforms.modelViewMatrix, false, _modelViewMatrixArray );
  96. _gl.uniformMatrix3fv( program.uniforms.normalMatrix, false, _normalMatrixArray );
  97. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.vertexBuffer );
  98. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  99. if ( attributes.normal >= 0 ) {
  100. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.normalBuffer );
  101. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  102. }
  103. if ( attributes.uv >= 0 ) {
  104. if ( geometry.__webglBuffers.uvBuffer ) {
  105. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglBuffers.uvBuffer );
  106. _gl.enableVertexAttribArray( attributes.uv );
  107. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  108. } else {
  109. _gl.disableVertexAttribArray( attributes.uv );
  110. }
  111. }
  112. if ( ! material.wireframe ) {
  113. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webglBuffers.faceBuffer );
  114. _gl.drawElements( _gl.TRIANGLES, geometry.__webglBuffers.faceCount, _gl.UNSIGNED_SHORT, 0 );
  115. } else {
  116. _gl.lineWidth( material.wireframe_linewidth );
  117. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometry.__webglBuffers.lineBuffer );
  118. _gl.drawElements( _gl.LINES, geometry.__webglBuffers.lineCount, _gl.UNSIGNED_SHORT, 0 );
  119. }
  120. }
  121. }
  122. }
  123. // Buffers
  124. function buildBuffers( geometry ) {
  125. var f, fl, face, v1, v2, v3, vertexNormals, normal, uv,
  126. vertexIndex = 0, verticesArray = [], facesArray = [], linesArray = [],
  127. normalsArray = [], uvsArray = [],
  128. buffers = {};
  129. for ( f = 0, fl = geometry.faces.length; f < fl; f++ ) {
  130. face = geometry.faces[ f ];
  131. uv = geometry.uvs[ f ];
  132. vertexNormals = face.vertexNormals;
  133. faceNormal = face.normal;
  134. if ( face instanceof THREE.Face3 ) {
  135. v1 = geometry.vertices[ face.a ].position;
  136. v2 = geometry.vertices[ face.b ].position;
  137. v3 = geometry.vertices[ face.c ].position;
  138. verticesArray.push( v1.x, v1.y, v1.z );
  139. verticesArray.push( v2.x, v2.y, v2.z );
  140. verticesArray.push( v3.x, v3.y, v3.z );
  141. if ( vertexNormals.length == 3 ) {
  142. for ( i = 0; i < 3; i ++ ) {
  143. normalsArray.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
  144. }
  145. } else {
  146. for ( i = 0; i < 3; i ++ ) {
  147. normalsArray.push( faceNormal.x, faceNormal.y, faceNormal.z );
  148. }
  149. }
  150. if ( uv ) {
  151. for ( i = 0; i < 3; i ++ ) {
  152. uvsArray.push( uv[ i ].u, uv[ i ].v );
  153. }
  154. }
  155. facesArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  156. // TODO: don't add lines that already exist (faces sharing edge)
  157. linesArray.push( vertexIndex, vertexIndex + 1 );
  158. linesArray.push( vertexIndex, vertexIndex + 2 );
  159. linesArray.push( vertexIndex + 1, vertexIndex + 2 );
  160. vertexIndex += 3;
  161. } else if ( face instanceof THREE.Face4 ) {
  162. v1 = geometry.vertices[ face.a ].position;
  163. v2 = geometry.vertices[ face.b ].position;
  164. v3 = geometry.vertices[ face.c ].position;
  165. v4 = geometry.vertices[ face.d ].position;
  166. verticesArray.push( v1.x, v1.y, v1.z );
  167. verticesArray.push( v2.x, v2.y, v2.z );
  168. verticesArray.push( v3.x, v3.y, v3.z );
  169. verticesArray.push( v4.x, v4.y, v4.z );
  170. if ( vertexNormals.length == 4 ) {
  171. for ( i = 0; i < 4; i ++ ) {
  172. normalsArray.push( vertexNormals[ i ].x, vertexNormals[ i ].y, vertexNormals[ i ].z );
  173. }
  174. } else {
  175. for ( i = 0; i < 4; i ++ ) {
  176. normalsArray.push( faceNormal.x, faceNormal.y, faceNormal.z );
  177. }
  178. }
  179. if ( uv ) {
  180. for ( i = 0; i < 4; i ++ ) {
  181. uvsArray.push( uv[ i ].u, uv[ i ].v );
  182. }
  183. }
  184. facesArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  185. facesArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
  186. // TODO: don't add lines that already exist (faces sharing edge)
  187. linesArray.push( vertexIndex, vertexIndex + 1 );
  188. linesArray.push( vertexIndex, vertexIndex + 2 );
  189. linesArray.push( vertexIndex, vertexIndex + 3 );
  190. linesArray.push( vertexIndex + 1, vertexIndex + 2 );
  191. linesArray.push( vertexIndex + 2, vertexIndex + 3 );
  192. vertexIndex += 4;
  193. }
  194. }
  195. if ( !verticesArray.length ) return false;
  196. buffers.vertexBuffer = _gl.createBuffer();
  197. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.vertexBuffer );
  198. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( verticesArray ), _gl.STATIC_DRAW );
  199. buffers.normalBuffer = _gl.createBuffer();
  200. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normalBuffer );
  201. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalsArray ), _gl.STATIC_DRAW );
  202. if ( uvsArray.length > 0 ) {
  203. buffers.uvBuffer = _gl.createBuffer();
  204. _gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uvBuffer );
  205. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvsArray ), _gl.STATIC_DRAW );
  206. }
  207. buffers.faceBuffer = _gl.createBuffer();
  208. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffers.faceBuffer );
  209. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( facesArray ), _gl.STATIC_DRAW );
  210. buffers.lineBuffer = _gl.createBuffer();
  211. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, buffers.lineBuffer );
  212. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( linesArray ), _gl.STATIC_DRAW );
  213. buffers.faceCount = facesArray.length;
  214. buffers.lineCount = linesArray.length;
  215. geometry.__webglBuffers = buffers;
  216. return true;
  217. }
  218. // Shaders
  219. function createProgram( material ) {
  220. var pvs = '', vs = '', pfs = '', fs = '',
  221. identifiers = [ 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition' ];
  222. if ( material instanceof THREE.MeshBasicMaterial ||
  223. material instanceof THREE.MeshLambertMaterial ||
  224. material instanceof THREE.MeshPhongMaterial ) {
  225. vs += 'void main() {\n';
  226. fs += 'void main() {\n';
  227. vs += 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n';
  228. pfs += 'uniform vec3 mColor;\n';
  229. pfs += 'uniform float mOpacity;\n';
  230. fs += 'gl_FragColor = vec4( mColor.xyz, mOpacity );\n';
  231. if ( material.map ) {
  232. pvs += 'varying vec2 vUv;\n',
  233. vs += 'vUv = uv;\n',
  234. pfs += 'uniform sampler2D tMap;\n';
  235. pfs += 'varying vec2 vUv;\n';
  236. fs += 'gl_FragColor *= texture2D( tMap, vUv );\n';
  237. identifiers.push( 'tMap' );
  238. }
  239. identifiers.push( 'mColor' );
  240. identifiers.push( 'mOpacity' );
  241. vs += '}';
  242. fs += '}';
  243. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  244. vs += 'void main() {\n';
  245. fs += 'void main() {\n';
  246. pvs += 'varying vec3 vNormal;\n';
  247. vs += 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n';
  248. vs += 'vNormal = normalize( normalMatrix * normal );\n';
  249. pfs += 'uniform float mOpacity;\n';
  250. pfs += 'varying vec3 vNormal;\n';
  251. fs += 'gl_FragColor = vec4( ( normalize( vNormal ) + 1.0 ) * 0.5, mOpacity );\n';
  252. identifiers.push( 'mOpacity' );
  253. vs += '}';
  254. fs += '}';
  255. } else if ( material instanceof THREE.MeshShaderMaterial ) {
  256. vs = material.vertex_shader;
  257. fs = material.fragment_shader;
  258. for( uniform in material.uniforms ) {
  259. identifiers.push( uniform );
  260. }
  261. } else {
  262. return false;
  263. }
  264. material.__webglProgram = compileProgram( pvs + vs, pfs + fs );
  265. cacheUniformLocations( material.__webglProgram, identifiers );
  266. cacheAttributeLocations( material.__webglProgram, [ "position", "normal", "uv"/*, "tangent"*/ ] );
  267. return true;
  268. }
  269. function compileProgram( vertex_shader, fragment_shader ) {
  270. var program = _gl.createProgram(), shader
  271. prefix_vertex = [
  272. maxVertexTextures() > 0 ? "#define VERTEX_TEXTURES" : "",
  273. "uniform mat4 objectMatrix;",
  274. "uniform mat4 modelViewMatrix;",
  275. "uniform mat4 projectionMatrix;",
  276. //"uniform mat4 viewMatrix;",
  277. "uniform mat3 normalMatrix;",
  278. "uniform vec3 cameraPosition;",
  279. "attribute vec3 position;",
  280. "attribute vec3 normal;",
  281. "attribute vec2 uv;",
  282. ""
  283. ].join("\n"),
  284. prefix_fragment = [
  285. "#ifdef GL_ES",
  286. "precision highp float;",
  287. "#endif",
  288. //"uniform mat4 viewMatrix;",
  289. ""
  290. ].join("\n");
  291. // Vertex shader
  292. shader = _gl.createShader( _gl.VERTEX_SHADER );
  293. _gl.shaderSource( shader, prefix_vertex + vertex_shader );
  294. _gl.compileShader( shader );
  295. _gl.attachShader( program, shader );
  296. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  297. alert( _gl.getShaderInfoLog( shader ) );
  298. return null;
  299. }
  300. // Fragment Shader
  301. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  302. _gl.shaderSource( shader, prefix_fragment + fragment_shader );
  303. _gl.compileShader( shader );
  304. _gl.attachShader( program, shader );
  305. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  306. alert( _gl.getShaderInfoLog( shader ) );
  307. return null;
  308. }
  309. _gl.linkProgram( program );
  310. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  311. alert( "Could not initialise shaders.\n" +
  312. "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + "\n" +
  313. "ERROR: " + _gl.getError() + "\n\n" +
  314. "Vertex Shader: \n" + prefix_vertex + vertex_shader + "\n\n" +
  315. "Fragment Shader: \n" + prefix_fragment + fragment_shader );
  316. }
  317. program.uniforms = {};
  318. program.attributes = {};
  319. return program;
  320. }
  321. function cacheUniformLocations( program, identifiers ) {
  322. var i, l, id;
  323. for( i = 0, l = identifiers.length; i < l; i++ ) {
  324. id = identifiers[ i ];
  325. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  326. }
  327. }
  328. function cacheAttributeLocations( program, identifiers ) {
  329. var i, l, id;
  330. for( i = 0, l = identifiers.length; i < l; i++ ) {
  331. id = identifiers[ i ];
  332. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  333. if ( program.attributes[ id ] >= 0 ) {
  334. _gl.enableVertexAttribArray( program.attributes[ id ] );
  335. }
  336. }
  337. }
  338. // Textures
  339. function setTexture( texture, slot ) {
  340. if ( !texture.__webglTexture && texture.image.loaded ) {
  341. texture.__webglTexture = _gl.createTexture();
  342. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  343. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, texture.image );
  344. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrap_s ) );
  345. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrap_t ) );
  346. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.mag_filter ) );
  347. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.min_filter ) );
  348. _gl.generateMipmap( _gl.TEXTURE_2D );
  349. _gl.bindTexture( _gl.TEXTURE_2D, null );
  350. }
  351. _gl.activeTexture( _gl.TEXTURE0 + slot );
  352. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  353. }
  354. function maxVertexTextures() {
  355. return _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  356. };
  357. function paramThreeToGL( p ) {
  358. switch ( p ) {
  359. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  360. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  361. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  362. case THREE.NearestFilter: return _gl.NEAREST; break;
  363. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  364. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  365. case THREE.LinearFilter: return _gl.LINEAR; break;
  366. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  367. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  368. }
  369. return 0;
  370. }
  371. };
  372. };
粤ICP备19079148号