WebGLRenderer.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**
  2. * @author supereggbert / http://www.paulbrunt.co.uk/
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. THREE.WebGLRenderer = function () {
  6. var _canvas = document.createElement( 'canvas' ), _gl, _program,
  7. viewMatrix = new THREE.Matrix4();
  8. this.domElement = _canvas;
  9. this.autoClear = true;
  10. initGL();
  11. initProgram();
  12. this.setSize = function ( width, height ) {
  13. _canvas.width = width;
  14. _canvas.height = height;
  15. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  16. };
  17. this.clear = function () {
  18. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  19. };
  20. this.render = function ( scene, camera ) {
  21. var face, faceColor, object, material,
  22. vertexArray, faceArray, colorArray, vertexIndex,
  23. o, ol, f, fl, m, ml, i, v1, v2, v3, v4;
  24. if ( this.autoClear ) {
  25. this.clear();
  26. }
  27. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  28. object = scene.objects[ o ];
  29. if ( object instanceof THREE.Mesh ) {
  30. if ( !object.__webGLVertexBuffer ) {
  31. vertexArray = [];
  32. faceArray = [];
  33. colorArray = [];
  34. vertexIndex = 0;
  35. for ( f = 0, fl = object.geometry.faces.length; f < fl; f++ ) {
  36. face = object.geometry.faces[ f ];
  37. faceColor = face.color;
  38. if ( face instanceof THREE.Face3 ) {
  39. v1 = object.geometry.vertices[ face.a ].position;
  40. v2 = object.geometry.vertices[ face.b ].position;
  41. v3 = object.geometry.vertices[ face.c ].position;
  42. vertexArray.push( v1.x, v1.y, v1.z );
  43. vertexArray.push( v2.x, v2.y, v2.z );
  44. vertexArray.push( v3.x, v3.y, v3.z );
  45. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  46. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  47. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  48. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  49. vertexIndex += 3;
  50. } else if ( face instanceof THREE.Face4 ) {
  51. v1 = object.geometry.vertices[ face.a ].position;
  52. v2 = object.geometry.vertices[ face.b ].position;
  53. v3 = object.geometry.vertices[ face.c ].position;
  54. v4 = object.geometry.vertices[ face.d ].position;
  55. vertexArray.push( v1.x, v1.y, v1.z );
  56. vertexArray.push( v2.x, v2.y, v2.z );
  57. vertexArray.push( v3.x, v3.y, v3.z );
  58. vertexArray.push( v4.x, v4.y, v4.z );
  59. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  60. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  61. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  62. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  63. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  64. faceArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
  65. vertexIndex += 4;
  66. }
  67. }
  68. if ( !vertexArray.length ) {
  69. continue;
  70. }
  71. object.__webGLVertexBuffer = _gl.createBuffer();
  72. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
  73. _gl.bufferData( _gl.ARRAY_BUFFER, new WebGLFloatArray( vertexArray ), _gl.STATIC_DRAW );
  74. object.__webGLColorBuffer = _gl.createBuffer();
  75. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLColorBuffer );
  76. _gl.bufferData( _gl.ARRAY_BUFFER, new WebGLFloatArray( colorArray ), _gl.STATIC_DRAW );
  77. object.__webGLFaceBuffer = _gl.createBuffer();
  78. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, object.__webGLFaceBuffer );
  79. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new WebGLUnsignedShortArray( faceArray ), _gl.STATIC_DRAW );
  80. object.__webGLFaceCount = faceArray.length;
  81. }
  82. viewMatrix.multiply( camera.matrix, object.matrix );
  83. matrix2Array( viewMatrix, _program.viewMatrixArray );
  84. matrix2Array( camera.projectionMatrix, _program.projectionMatrixArray );
  85. _gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
  86. _gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
  87. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
  88. _gl.vertexAttribPointer( _program.position, 3, _gl.FLOAT, false, 0, 0 );
  89. for ( m = 0, ml = object.material.length; m < ml; m++ ) {
  90. material = object.material[ m ];
  91. if ( material instanceof THREE.ColorFillMaterial ) {
  92. if ( !material.__webGLColorBuffer ) {
  93. colorArray = [];
  94. for ( i = 0; i < object.__webGLFaceCount; i ++ ) {
  95. colorArray.push( material.color.r, material.color.g, material.color.b, material.color.a );
  96. }
  97. material.__webGLColorBuffer = _gl.createBuffer();
  98. _gl.bindBuffer( _gl.ARRAY_BUFFER, material.__webGLColorBuffer );
  99. _gl.bufferData( _gl.ARRAY_BUFFER, new WebGLFloatArray( colorArray ), _gl.STATIC_DRAW );
  100. }
  101. _gl.bindBuffer( _gl.ARRAY_BUFFER, material.__webGLColorBuffer );
  102. _gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );
  103. } else if ( material instanceof THREE.FaceColorFillMaterial ) {
  104. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLColorBuffer );
  105. _gl.enableVertexAttribArray( _program.color );
  106. _gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );
  107. }
  108. }
  109. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, object.__webGLFaceBuffer );
  110. _gl.drawElements( _gl.TRIANGLES, object.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
  111. }
  112. }
  113. };
  114. function initGL() {
  115. try {
  116. _gl = _canvas.getContext( 'experimental-webgl' );
  117. } catch(e) { }
  118. if (!_gl) {
  119. alert("WebGL not supported");
  120. throw "cannot create webgl context";
  121. }
  122. _gl.clearColor( 0, 0, 0, 1 );
  123. _gl.clearDepth( 1 );
  124. _gl.enable( _gl.DEPTH_TEST );
  125. _gl.depthFunc( _gl.LEQUAL );
  126. _gl.enable( _gl.BLEND );
  127. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  128. // _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); // cool!
  129. _gl.clearColor( 0, 0, 0, 0 );
  130. }
  131. function initProgram() {
  132. _program = _gl.createProgram();
  133. _gl.attachShader( _program, getShader( "fragment", [
  134. "varying vec4 vcolor;",
  135. "void main(){",
  136. "gl_FragColor = vcolor;",
  137. "}"].join("") ) );
  138. _gl.attachShader( _program, getShader( "vertex", [
  139. "attribute vec3 position;",
  140. "attribute vec4 color;",
  141. "uniform mat4 viewMatrix;",
  142. "uniform mat4 projectionMatrix;",
  143. "varying vec4 vcolor;",
  144. "void main(void) {",
  145. "vcolor = color;",
  146. "gl_Position = projectionMatrix * viewMatrix * vec4( position, 1 );",
  147. "}"].join("") ) );
  148. _gl.linkProgram( _program );
  149. if ( !_gl.getProgramParameter( _program, _gl.LINK_STATUS ) ) {
  150. alert( "Could not initialise shaders" );
  151. }
  152. _gl.useProgram( _program );
  153. _program.viewMatrix = _gl.getUniformLocation( _program, "viewMatrix" );
  154. _program.projectionMatrix = _gl.getUniformLocation( _program, "projectionMatrix" );
  155. _program.color = _gl.getAttribLocation( _program, "color" );
  156. _gl.enableVertexAttribArray( _program.color );
  157. _program.position = _gl.getAttribLocation( _program, "position" );
  158. _gl.enableVertexAttribArray( _program.position );
  159. _program.viewMatrixArray = new WebGLFloatArray( 16 );
  160. _program.projectionMatrixArray = new WebGLFloatArray( 16 );
  161. }
  162. function getShader( type, string ) {
  163. var shader;
  164. if ( type == "fragment" ) {
  165. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  166. } else if ( type == "vertex" ) {
  167. shader = _gl.createShader( _gl.VERTEX_SHADER );
  168. }
  169. _gl.shaderSource( shader, string );
  170. _gl.compileShader( shader );
  171. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  172. alert( _gl.getShaderInfoLog( shader ) );
  173. return null;
  174. }
  175. return shader;
  176. }
  177. function matrix2Array( matrix, array ) {
  178. // Also flatten
  179. array[ 0 ] = matrix.n11; array[ 1 ] = matrix.n21; array[ 2 ] = matrix.n31; array[ 3 ] = matrix.n41;
  180. array[ 4 ] = matrix.n12; array[ 5 ] = matrix.n22; array[ 6 ] = matrix.n32; array[ 7 ] = matrix.n42;
  181. array[ 8 ] = matrix.n13; array[ 9 ] = matrix.n23; array[ 10 ] = matrix.n33; array[ 11 ] = matrix.n43;
  182. array[ 12 ] = matrix.n14; array[ 13 ] = matrix.n24; array[ 14 ] = matrix.n34; array[ 15 ] = matrix.n44;
  183. }
  184. };
粤ICP备19079148号