WebGLRenderer.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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' ),
  7. _gl, _program,
  8. _viewMatrix = new THREE.Matrix4(), _normalMatrix;
  9. this.domElement = _canvas;
  10. this.autoClear = true;
  11. initGL();
  12. initProgram();
  13. // material constants used in shader
  14. var COLORFILL = 0, COLORSTROKE = 1, FACECOLORFILL = 2, FACECOLORSTROKE = 3, BITMAP = 4;
  15. this.setSize = function ( width, height ) {
  16. _canvas.width = width;
  17. _canvas.height = height;
  18. _gl.viewport( 0, 0, _canvas.width, _canvas.height );
  19. };
  20. this.clear = function () {
  21. _gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );
  22. };
  23. this.setupLights = function ( scene ) {
  24. var l, ll, lightColor, lightPosition, lightIntensity, light;
  25. _gl.uniform1i( _program.enableLighting, scene.lights.length );
  26. for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {
  27. light = scene.lights[ l ];
  28. if ( light instanceof THREE.AmbientLight ) {
  29. lightColor = light.color;
  30. _gl.uniform3f( _program.ambientColor, lightColor.r, lightColor.g, lightColor.b );
  31. } else if ( light instanceof THREE.DirectionalLight ) {
  32. lightColor = light.color;
  33. lightPosition = light.position;
  34. lightIntensity = light.intensity;
  35. _gl.uniform3f( _program.lightingDirection, lightPosition.x, lightPosition.y, lightPosition.z );
  36. _gl.uniform3f( _program.directionalColor, lightColor.r * lightIntensity, lightColor.g * lightIntensity, lightColor.b * lightIntensity );
  37. } else if( light instanceof THREE.PointLight ) {
  38. lightColor = light.color;
  39. lightPosition = light.position;
  40. lightIntensity = light.intensity;
  41. _gl.uniform3f( _program.pointPosition, lightPosition.x, lightPosition.y, lightPosition.z );
  42. _gl.uniform3f( _program.pointColor, lightColor.r * lightIntensity, lightColor.g * lightIntensity, lightColor.b * lightIntensity );
  43. }
  44. }
  45. };
  46. this.createBuffers = function ( object, materialIndex ) {
  47. var materialFace = object.materialFaces[ materialIndex ];
  48. var material = object.material[ materialIndex ];
  49. var faceArray = [];
  50. var lineArray = [];
  51. var vertexArray = [];
  52. var colorArray = [];
  53. var normalArray = [];
  54. var uvArray = [];
  55. var vertexIndex = 0;
  56. var f, fl, fi, face, faceColor, vertexNormals, normal, uv, v1, v2, v3, v4;
  57. for ( f = 0, fl = materialFace.faces.length; f < fl; f++ ) {
  58. fi = materialFace.faces[f];
  59. face = object.geometry.faces[ fi ];
  60. faceColor = face.color;
  61. vertexNormals = face.vertexNormals;
  62. normal = face.normal;
  63. uv = object.geometry.uvs[ fi ];
  64. if ( face instanceof THREE.Face3 ) {
  65. v1 = object.geometry.vertices[ face.a ].position;
  66. v2 = object.geometry.vertices[ face.b ].position;
  67. v3 = object.geometry.vertices[ face.c ].position;
  68. vertexArray.push( v1.x, v1.y, v1.z );
  69. vertexArray.push( v2.x, v2.y, v2.z );
  70. vertexArray.push( v3.x, v3.y, v3.z );
  71. if ( vertexNormals.length == 3 ) {
  72. normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
  73. normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
  74. normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
  75. }
  76. else {
  77. normalArray.push( normal.x, normal.y, normal.z );
  78. normalArray.push( normal.x, normal.y, normal.z );
  79. normalArray.push( normal.x, normal.y, normal.z );
  80. }
  81. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  82. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  83. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  84. if ( uv ) {
  85. uvArray.push( uv[0].u, uv[0].v );
  86. uvArray.push( uv[1].u, uv[1].v );
  87. uvArray.push( uv[2].u, uv[2].v );
  88. }
  89. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  90. // TODO: don't add lines that already exist (faces sharing edge)
  91. lineArray.push( vertexIndex, vertexIndex + 1 );
  92. lineArray.push( vertexIndex, vertexIndex + 2 );
  93. lineArray.push( vertexIndex + 1, vertexIndex + 2 );
  94. vertexIndex += 3;
  95. } else if ( face instanceof THREE.Face4 ) {
  96. v1 = object.geometry.vertices[ face.a ].position;
  97. v2 = object.geometry.vertices[ face.b ].position;
  98. v3 = object.geometry.vertices[ face.c ].position;
  99. v4 = object.geometry.vertices[ face.d ].position;
  100. vertexArray.push( v1.x, v1.y, v1.z );
  101. vertexArray.push( v2.x, v2.y, v2.z );
  102. vertexArray.push( v3.x, v3.y, v3.z );
  103. vertexArray.push( v4.x, v4.y, v4.z );
  104. if ( vertexNormals.length == 4 ) {
  105. normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
  106. normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
  107. normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
  108. normalArray.push( vertexNormals[3].x, vertexNormals[3].y, vertexNormals[3].z );
  109. }
  110. else {
  111. normalArray.push( normal.x, normal.y, normal.z );
  112. normalArray.push( normal.x, normal.y, normal.z );
  113. normalArray.push( normal.x, normal.y, normal.z );
  114. normalArray.push( normal.x, normal.y, normal.z );
  115. }
  116. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  117. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  118. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  119. colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
  120. if ( uv ) {
  121. uvArray.push( uv[0].u, uv[0].v );
  122. uvArray.push( uv[1].u, uv[1].v );
  123. uvArray.push( uv[2].u, uv[2].v );
  124. uvArray.push( uv[3].u, uv[3].v );
  125. }
  126. faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
  127. faceArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );
  128. // TODO: don't add lines that already exist (faces sharing edge)
  129. lineArray.push( vertexIndex, vertexIndex + 1 );
  130. lineArray.push( vertexIndex, vertexIndex + 2 );
  131. lineArray.push( vertexIndex, vertexIndex + 3 );
  132. lineArray.push( vertexIndex + 1, vertexIndex + 2 );
  133. lineArray.push( vertexIndex + 2, vertexIndex + 3 );
  134. vertexIndex += 4;
  135. }
  136. }
  137. if ( !vertexArray.length ) {
  138. return;
  139. }
  140. /*
  141. log( "vertices: " + vertexArray.length/3 );
  142. log( "faces: " + faceArray.length/3 );
  143. log( "normals: " + normalArray.length/3 );
  144. log( "colors: " + colorArray.length/4 );
  145. log( "uvs: " + uvArray.length/2 );
  146. */
  147. materialFace.__webGLVertexBuffer = _gl.createBuffer();
  148. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLVertexBuffer );
  149. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexArray ), _gl.STATIC_DRAW );
  150. materialFace.__webGLNormalBuffer = _gl.createBuffer();
  151. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLNormalBuffer );
  152. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );
  153. materialFace.__webGLColorBuffer = _gl.createBuffer();
  154. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLColorBuffer );
  155. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( colorArray ), _gl.STATIC_DRAW );
  156. materialFace.__webGLUVBuffer = _gl.createBuffer();
  157. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLUVBuffer );
  158. _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvArray ), _gl.STATIC_DRAW );
  159. materialFace.__webGLFaceBuffer = _gl.createBuffer();
  160. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLFaceBuffer );
  161. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faceArray ), _gl.STATIC_DRAW );
  162. materialFace.__webGLLineBuffer = _gl.createBuffer();
  163. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLLineBuffer );
  164. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( lineArray ), _gl.STATIC_DRAW );
  165. materialFace.__webGLFaceCount = faceArray.length;
  166. materialFace.__webGLLineCount = lineArray.length;
  167. }
  168. this.renderMesh = function ( object, camera ) {
  169. var m, ml, mf, material, materialFace, fi, lineWidth;
  170. // create separate VBOs per material
  171. for (var mf in object.materialFaces ) {
  172. materialFace = object.materialFaces[ mf ];
  173. material = object.material[ mf ];
  174. if( !material ) continue;
  175. // initialise on the first access
  176. if( !materialFace.__webGLVertexBuffer ) {
  177. this.createBuffers( object, mf );
  178. }
  179. for ( m = 0, ml = object.material.length; m < ml; m++ ) {
  180. material = object.material[ m ];
  181. // these materials can be either the only material for whole mesh
  182. // or if they are overlays in multimaterials, they apply only to
  183. // group of faces with single material (specified by decalIndex)
  184. if ( ( material instanceof THREE.MeshBitmapUVMappingMaterial ||
  185. material instanceof THREE.MeshFaceColorFillMaterial ||
  186. material instanceof THREE.MeshColorFillMaterial
  187. )
  188. &&
  189. ! ( m == mf || mf == material.decalIndex ) ) {
  190. continue;
  191. }
  192. if ( material instanceof THREE.MeshColorFillMaterial ) {
  193. color = material.color;
  194. _gl.uniform4f( _program.uniformColor, color.r, color.g, color.b, color.a );
  195. _gl.uniform1i( _program.material, COLORFILL );
  196. } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
  197. lineWidth = material.lineWidth;
  198. color = material.color;
  199. _gl.uniform4f( _program.uniformColor, color.r, color.g, color.b, color.a );
  200. _gl.uniform1i( _program.material, COLORSTROKE );
  201. } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
  202. _gl.uniform1i( _program.material, FACECOLORFILL );
  203. } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  204. lineWidth = material.lineWidth;
  205. _gl.uniform1i( _program.material, FACECOLORSTROKE );
  206. } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
  207. if ( !material.__webGLTexture && material.loaded ) {
  208. material.__webGLTexture = _gl.createTexture();
  209. _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
  210. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.bitmap ) ;
  211. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.LINEAR );
  212. //_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_NEAREST );
  213. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_LINEAR );
  214. _gl.generateMipmap( _gl.TEXTURE_2D );
  215. _gl.bindTexture( _gl.TEXTURE_2D, null );
  216. }
  217. _gl.activeTexture( _gl.TEXTURE0 );
  218. _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
  219. _gl.uniform1i( _program.diffuse, 0 );
  220. _gl.uniform1i( _program.material, BITMAP );
  221. }
  222. // vertices
  223. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLVertexBuffer );
  224. _gl.vertexAttribPointer( _program.position, 3, _gl.FLOAT, false, 0, 0 );
  225. // normals
  226. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLNormalBuffer );
  227. _gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );
  228. // colors
  229. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLColorBuffer );
  230. _gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );
  231. // uvs
  232. if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
  233. _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLUVBuffer );
  234. _gl.enableVertexAttribArray( _program.uv );
  235. _gl.vertexAttribPointer( _program.uv, 2, _gl.FLOAT, false, 0, 0 );
  236. }
  237. else {
  238. _gl.disableVertexAttribArray( _program.uv );
  239. }
  240. // render triangles
  241. if ( material instanceof THREE.MeshBitmapUVMappingMaterial ||
  242. material instanceof THREE.MeshFaceColorFillMaterial ||
  243. material instanceof THREE.MeshColorFillMaterial ) {
  244. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLFaceBuffer );
  245. _gl.drawElements( _gl.TRIANGLES, materialFace.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
  246. }
  247. // render lines
  248. else if ( material instanceof THREE.MeshColorStrokeMaterial ||
  249. material instanceof THREE.MeshFaceColorStrokeMaterial ) {
  250. _gl.lineWidth( lineWidth );
  251. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLLineBuffer );
  252. _gl.drawElements( _gl.LINES, materialFace.__webGLLineCount, _gl.UNSIGNED_SHORT, 0 );
  253. }
  254. }
  255. }
  256. };
  257. this.setupMatrices = function ( object, camera ) {
  258. camera.autoUpdateMatrix && camera.updateMatrix();
  259. object.autoUpdateMatrix && object.updateMatrix();
  260. _viewMatrix.multiply( camera.matrix, object.matrix );
  261. _program.viewMatrixArray = new Float32Array( _viewMatrix.flatten() );
  262. _program.projectionMatrixArray = new Float32Array( camera.projectionMatrix.flatten() );
  263. _normalMatrix = THREE.Matrix4.makeInvert3x3( object.matrix ).transpose();
  264. _program.normalMatrixArray = new Float32Array( _normalMatrix.m );
  265. _gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
  266. _gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
  267. _gl.uniformMatrix3fv( _program.normalMatrix, false, _program.normalMatrixArray );
  268. _gl.uniformMatrix4fv( _program.objMatrix, false, new Float32Array( object.matrix.flatten() ) );
  269. };
  270. this.render = function ( scene, camera ) {
  271. var o, ol, object;
  272. if ( this.autoClear ) {
  273. this.clear();
  274. }
  275. this.setupLights( scene );
  276. for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
  277. object = scene.objects[ o ];
  278. this.setupMatrices( object, camera );
  279. if ( object instanceof THREE.Mesh ) {
  280. this.renderMesh( object, camera );
  281. } else if ( object instanceof THREE.Line ) {
  282. // TODO
  283. // It would be very inefficient to do lines one-by-one.
  284. // This will need a complete redesign from how CanvasRenderer does it.
  285. // Though it could be brute forced, if only used for lightweight
  286. // stuff (as CanvasRenderer can only handle small number of elements
  287. // anyways).
  288. // Heavy-duty wireframe lines are handled efficiently in mesh renderer.
  289. } else if ( object instanceof THREE.Particle ) {
  290. // TODO
  291. // The same as with lines, particles shouldn't be handled one-by-one.
  292. // Again, heavy duty particle system would require different approach,
  293. // like one VBO per particle system and then update attribute arrays,
  294. // though the best would be to move also behavior computation
  295. // into the shader (ala http://spidergl.org/example.php?id=11)
  296. }
  297. }
  298. };
  299. function initGL() {
  300. try {
  301. _gl = _canvas.getContext( 'experimental-webgl', { antialias: true} );
  302. } catch(e) { }
  303. if (!_gl) {
  304. alert("WebGL not supported");
  305. throw "cannot create webgl context";
  306. }
  307. _gl.clearColor( 0, 0, 0, 1 );
  308. _gl.clearDepth( 1 );
  309. _gl.enable( _gl.DEPTH_TEST );
  310. _gl.depthFunc( _gl.LEQUAL );
  311. _gl.enable( _gl.BLEND );
  312. //_gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  313. // _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); // cool!
  314. _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  315. _gl.clearColor( 0, 0, 0, 0 );
  316. }
  317. function initProgram() {
  318. _program = _gl.createProgram();
  319. _gl.attachShader( _program, getShader( "fragment", [
  320. "#ifdef GL_ES",
  321. "precision highp float;",
  322. "#endif",
  323. "uniform sampler2D diffuse;",
  324. "uniform vec4 uniformColor;",
  325. "varying vec2 vertexUv;",
  326. "varying vec4 vertexColor;",
  327. "varying vec3 lightWeighting;",
  328. "varying vec3 vNormal;",
  329. "uniform int material;", // 0 - ColorFill, 1 - ColorStroke, 2 - FaceColorFill, 3 - FaceColorStroke, 4 - Bitmap
  330. "void main(){",
  331. "if(material==4) {", // texture
  332. "vec4 texelColor = texture2D(diffuse, vertexUv);",
  333. "gl_FragColor = vec4(texelColor.rgb * lightWeighting, texelColor.a);",
  334. "} else if(material==3) {", // wireframe using vertex color
  335. "gl_FragColor = vec4(vertexColor.rgb * lightWeighting, vertexColor.a);",
  336. "} else if(material==2) {", // triangle using vertex color
  337. "gl_FragColor = vec4(vertexColor.rgb * lightWeighting, vertexColor.a);",
  338. "} else if(material==1) {", // wireframe using uniform color
  339. "gl_FragColor = vec4(uniformColor.rgb * lightWeighting, uniformColor.a);",
  340. "} else {", // triangle using uniform color
  341. "gl_FragColor = vec4(uniformColor.rgb * lightWeighting, uniformColor.a);",
  342. //"gl_FragColor = vec4(vNormal, 1.0);",
  343. "}",
  344. "}"
  345. ].join("\n") ) );
  346. _gl.attachShader( _program, getShader( "vertex", [
  347. "attribute vec3 position;",
  348. "attribute vec3 normal;",
  349. "attribute vec4 color;",
  350. "attribute vec2 uv;",
  351. "uniform bool enableLighting;",
  352. "uniform vec3 ambientColor;",
  353. "uniform vec3 directionalColor;",
  354. "uniform vec3 lightingDirection;",
  355. "uniform vec3 pointColor;",
  356. "uniform vec3 pointPosition;",
  357. "uniform mat4 viewMatrix;",
  358. "uniform mat4 projectionMatrix;",
  359. "uniform mat4 objMatrix;",
  360. "uniform mat3 normalMatrix;",
  361. "varying vec4 vertexColor;",
  362. "varying vec2 vertexUv;",
  363. "varying vec3 lightWeighting;",
  364. "varying vec3 vNormal;",
  365. "void main(void) {",
  366. "vec4 mvPosition = viewMatrix * vec4( position, 1.0 );",
  367. "vec4 mPosition = objMatrix * vec4( position, 1.0 );",
  368. "vec3 transformedNormal = normalize(normalMatrix * normal);",
  369. "if(!enableLighting) {",
  370. "lightWeighting = vec3(1.0, 1.0, 1.0);",
  371. "} else {",
  372. "vec3 pointLight = normalize(pointPosition.xyz - mPosition.xyz);",
  373. "float directionalLightWeighting = max(dot(transformedNormal, normalize(lightingDirection)), 0.0);",
  374. "float pointLightWeighting = max(dot(transformedNormal, pointLight), 0.0);",
  375. "lightWeighting = ambientColor + directionalColor * directionalLightWeighting + pointColor * pointLightWeighting;",
  376. "}",
  377. "vNormal = transformedNormal;",
  378. "vertexColor = color;",
  379. "vertexUv = uv;",
  380. "gl_Position = projectionMatrix * mvPosition;",
  381. "}"].join("\n") ) );
  382. _gl.linkProgram( _program );
  383. if ( !_gl.getProgramParameter( _program, _gl.LINK_STATUS ) ) {
  384. alert( "Could not initialise shaders" );
  385. }
  386. _gl.useProgram( _program );
  387. _program.viewMatrix = _gl.getUniformLocation( _program, "viewMatrix" );
  388. _program.projectionMatrix = _gl.getUniformLocation( _program, "projectionMatrix" );
  389. _program.normalMatrix = _gl.getUniformLocation( _program, "normalMatrix" );
  390. _program.objMatrix = _gl.getUniformLocation( _program, "objMatrix" );
  391. _program.enableLighting = _gl.getUniformLocation(_program, 'enableLighting');
  392. _program.ambientColor = _gl.getUniformLocation(_program, 'ambientColor');
  393. _program.directionalColor = _gl.getUniformLocation(_program, 'directionalColor');
  394. _program.lightingDirection = _gl.getUniformLocation(_program, 'lightingDirection');
  395. _program.pointColor = _gl.getUniformLocation(_program, 'pointColor');
  396. _program.pointPosition = _gl.getUniformLocation(_program, 'pointPosition');
  397. _program.material = _gl.getUniformLocation(_program, 'material');
  398. _program.uniformColor = _gl.getUniformLocation(_program, 'uniformColor');
  399. _program.color = _gl.getAttribLocation( _program, "color" );
  400. _gl.enableVertexAttribArray( _program.color );
  401. _program.position = _gl.getAttribLocation( _program, "position" );
  402. _gl.enableVertexAttribArray( _program.position );
  403. _program.normal = _gl.getAttribLocation( _program, "normal" );
  404. _gl.enableVertexAttribArray( _program.normal );
  405. _program.uv = _gl.getAttribLocation( _program, "uv" );
  406. _gl.enableVertexAttribArray( _program.uv );
  407. _program.diffuse = _gl.getUniformLocation( _program, "diffuse");
  408. _gl.uniform1i( _program.diffuse, 0 );
  409. _program.viewMatrixArray = new Float32Array(16);
  410. _program.projectionMatrixArray = new Float32Array(16);
  411. }
  412. function getShader( type, string ) {
  413. var shader;
  414. if ( type == "fragment" ) {
  415. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  416. } else if ( type == "vertex" ) {
  417. shader = _gl.createShader( _gl.VERTEX_SHADER );
  418. }
  419. _gl.shaderSource( shader, string );
  420. _gl.compileShader( shader );
  421. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  422. alert( _gl.getShaderInfoLog( shader ) );
  423. return null;
  424. }
  425. return shader;
  426. }
  427. };
粤ICP备19079148号