WebGLRenderer.js 24.2 KB
Newer Older
N
Nicolas Garcia Belmonte 已提交
1 2 3 4 5 6 7
/**
 * @author supereggbert / http://www.paulbrunt.co.uk/
 * @author mrdoob / http://mrdoob.com/
 */

THREE.WebGLRenderer = function () {

8 9 10
	var _canvas = document.createElement( 'canvas' ), 
    _gl, _program,
	_viewMatrix = new THREE.Matrix4(), _normalMatrix;
N
Nicolas Garcia Belmonte 已提交
11 12 13 14 15 16 17

	this.domElement = _canvas;
	this.autoClear = true;

	initGL();
	initProgram();

18 19 20
    // material constants used in shader
    var COLORFILL = 0, COLORSTROKE = 1, FACECOLORFILL = 2, FACECOLORSTROKE = 3, BITMAP = 4;
    
N
Nicolas Garcia Belmonte 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
	this.setSize = function ( width, height ) {

		_canvas.width = width;
		_canvas.height = height;
		_gl.viewport( 0, 0, _canvas.width, _canvas.height );

	};

	this.clear = function () {

		_gl.clear( _gl.COLOR_BUFFER_BIT | _gl.DEPTH_BUFFER_BIT );

	};

35 36
    this.setupLights = function ( scene ) {
        
37
        var l, ll, lightColor, lightPosition, lightIntensity, light;
38
        
39
		_gl.uniform1i( _program.enableLighting, scene.lights.length );
40 41 42 43 44 45 46 47 48 49

		for ( l = 0, ll = scene.lights.length; l < ll; l++ ) {

			light = scene.lights[ l ];

			if ( light instanceof THREE.AmbientLight ) {

				lightColor = light.color;
				_gl.uniform3f( _program.ambientColor, lightColor.r, lightColor.g, lightColor.b );

50
			} else if ( light instanceof THREE.DirectionalLight ) {
51 52

				lightColor = light.color;
M
Mr.doob 已提交
53
				lightPosition = light.position;
54
                lightIntensity = light.intensity;
M
Mr.doob 已提交
55
				_gl.uniform3f( _program.lightingDirection, lightPosition.x, lightPosition.y, lightPosition.z );
56
				_gl.uniform3f( _program.directionalColor, lightColor.r * lightIntensity, lightColor.g * lightIntensity, lightColor.b * lightIntensity );
57

58 59 60 61
			} else if( light instanceof THREE.PointLight ) {

				lightColor = light.color;
				lightPosition = light.position;
62
                lightIntensity = light.intensity;
63
				_gl.uniform3f( _program.pointPosition, lightPosition.x, lightPosition.y, lightPosition.z );
64
				_gl.uniform3f( _program.pointColor, lightColor.r * lightIntensity, lightColor.g * lightIntensity, lightColor.b * lightIntensity );
65 66
                
            }
67 68

		}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    };
    
    this.createBuffers = function ( object, materialIndex ) {
        
        var materialFace = object.materialFaces[ materialIndex ];
        var material = object.material[ materialIndex ];
        
        var faceArray = [];
        var lineArray = [];
        
        var vertexArray = [];
        var colorArray = [];
        var normalArray = [];
        var uvArray = [];
        
        var vertexIndex = 0;

        var f, fl, fi, face, faceColor, vertexNormals, normal, uv, v1, v2, v3, v4;
        
        for ( f = 0, fl = materialFace.faces.length; f < fl; f++ ) {

            fi = materialFace.faces[f];

            face = object.geometry.faces[ fi ];
            faceColor = face.color;
            vertexNormals = face.vertexNormals;
            normal = face.normal;
            uv = object.geometry.uvs[ fi ];

            if ( face instanceof THREE.Face3 ) {

                v1 = object.geometry.vertices[ face.a ].position;
                v2 = object.geometry.vertices[ face.b ].position;
                v3 = object.geometry.vertices[ face.c ].position;

                vertexArray.push( v1.x, v1.y, v1.z );
                vertexArray.push( v2.x, v2.y, v2.z );
                vertexArray.push( v3.x, v3.y, v3.z );

                if ( vertexNormals.length == 3 ) {
                    
                    normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
                    normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
                    normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
                    
                }
                else {
                    
                    normalArray.push( normal.x, normal.y, normal.z );
                    normalArray.push( normal.x, normal.y, normal.z );
                    normalArray.push( normal.x, normal.y, normal.z );
                    
                }
122

123 124 125
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
U
unknown 已提交
126

127 128 129 130 131 132 133
                if ( uv ) {
                    
                    uvArray.push( uv[0].u, uv[0].v );
                    uvArray.push( uv[1].u, uv[1].v );
                    uvArray.push( uv[2].u, uv[2].v );
                    
                }
U
unknown 已提交
134

135 136 137 138 139 140 141
                faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
                
                // TODO: don't add lines that already exist (faces sharing edge)
                
                lineArray.push( vertexIndex, vertexIndex + 1 );
                lineArray.push( vertexIndex, vertexIndex + 2 );
                lineArray.push( vertexIndex + 1, vertexIndex + 2 );
U
unknown 已提交
142

143
                vertexIndex += 3;
U
unknown 已提交
144

145
            } else if ( face instanceof THREE.Face4 ) {
U
unknown 已提交
146

147 148 149 150
                v1 = object.geometry.vertices[ face.a ].position;
                v2 = object.geometry.vertices[ face.b ].position;
                v3 = object.geometry.vertices[ face.c ].position;
                v4 = object.geometry.vertices[ face.d ].position;
U
unknown 已提交
151

152 153 154 155
                vertexArray.push( v1.x, v1.y, v1.z );
                vertexArray.push( v2.x, v2.y, v2.z );
                vertexArray.push( v3.x, v3.y, v3.z );
                vertexArray.push( v4.x, v4.y, v4.z );
U
unknown 已提交
156

157
                if ( vertexNormals.length == 4 ) {
U
unknown 已提交
158
                    
159 160 161 162 163 164 165 166 167 168 169 170 171 172
                    normalArray.push( vertexNormals[0].x, vertexNormals[0].y, vertexNormals[0].z );
                    normalArray.push( vertexNormals[1].x, vertexNormals[1].y, vertexNormals[1].z );
                    normalArray.push( vertexNormals[2].x, vertexNormals[2].y, vertexNormals[2].z );
                    normalArray.push( vertexNormals[3].x, vertexNormals[3].y, vertexNormals[3].z );
                    
                }
                else {
                    
                    normalArray.push( normal.x, normal.y, normal.z );
                    normalArray.push( normal.x, normal.y, normal.z );
                    normalArray.push( normal.x, normal.y, normal.z );
                    normalArray.push( normal.x, normal.y, normal.z );
                    
                }
N
Nicolas Garcia Belmonte 已提交
173

174 175 176 177
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
                colorArray.push( faceColor.r, faceColor.g, faceColor.b, faceColor.a );
N
Nicolas Garcia Belmonte 已提交
178

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
                if ( uv ) {
                    
                    uvArray.push( uv[0].u, uv[0].v );
                    uvArray.push( uv[1].u, uv[1].v );
                    uvArray.push( uv[2].u, uv[2].v );
                    uvArray.push( uv[3].u, uv[3].v );
                    
                }
                
                faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
                faceArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );

                // TODO: don't add lines that already exist (faces sharing edge)
                
                lineArray.push( vertexIndex, vertexIndex + 1 );
                lineArray.push( vertexIndex, vertexIndex + 2 );
                lineArray.push( vertexIndex, vertexIndex + 3 );
                lineArray.push( vertexIndex + 1, vertexIndex + 2 );
                lineArray.push( vertexIndex + 2, vertexIndex + 3 );
                
                vertexIndex += 4;
            }
        }

        if ( !vertexArray.length ) {

            return;

        }

        /*
        log( "vertices: " + vertexArray.length/3 );
        log( "faces: " + faceArray.length/3 );
        log( "normals: " + normalArray.length/3 );
        log( "colors: " + colorArray.length/4 );
        log( "uvs: " + uvArray.length/2 );
        */
        
        materialFace.__webGLVertexBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLVertexBuffer );
        _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexArray ), _gl.STATIC_DRAW );

        materialFace.__webGLNormalBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLNormalBuffer );
        _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );

        materialFace.__webGLColorBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLColorBuffer );
        _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( colorArray ), _gl.STATIC_DRAW );

        materialFace.__webGLUVBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLUVBuffer );
        _gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( uvArray ), _gl.STATIC_DRAW );

        materialFace.__webGLFaceBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLFaceBuffer );
        _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faceArray ), _gl.STATIC_DRAW );

        materialFace.__webGLLineBuffer = _gl.createBuffer();
        _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLLineBuffer );
        _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( lineArray ), _gl.STATIC_DRAW );

        materialFace.__webGLFaceCount = faceArray.length;
        materialFace.__webGLLineCount = lineArray.length;

    }
    
    this.renderMesh = function ( object, camera ) {
        
        var m, ml, mf, material, materialFace, fi, lineWidth;

        // create separate VBOs per material
        for (var mf in object.materialFaces ) {

            materialFace = object.materialFaces[ mf ];
            material = object.material[ mf ];
            if( !material ) continue;

            // initialise on the first access
            if( !materialFace.__webGLVertexBuffer ) {
                
                this.createBuffers( object, mf );
                
            }
            
            for ( m = 0, ml = object.material.length; m < ml; m++ ) {
                
                material = object.material[ m ];
                
268 269 270 271 272 273 274 275 276 277
                // these materials can be either the only material for whole mesh
                // or if they are overlays in multimaterials, they apply only to
                // group of faces with single material (specified by decalIndex)
                
                if ( ( material instanceof THREE.MeshBitmapUVMappingMaterial || 
                       material instanceof THREE.MeshFaceColorFillMaterial ||
                       material instanceof THREE.MeshColorFillMaterial
                     ) 
                    &&
                     ! ( m == mf || mf == material.decalIndex ) ) {
278 279 280 281 282 283
                    
                    continue;
                    
                }
                
                if ( material instanceof THREE.MeshColorFillMaterial ) {
U
unknown 已提交
284

285
                    color = material.color;
286
                    _gl.uniform4f( _program.uniformColor,  color.r * color.a, color.g * color.a, color.b * color.a, color.a );
287 288 289 290 291 292 293 294
                    
                    _gl.uniform1i( _program.material, COLORFILL );
                    
                } else if ( material instanceof THREE.MeshColorStrokeMaterial ) {
                    
                    lineWidth = material.lineWidth;
                    
                    color = material.color;
295
                    _gl.uniform4f( _program.uniformColor,  color.r * color.a, color.g * color.a, color.b * color.a, color.a );
296 297 298 299 300 301 302 303
                    
                    _gl.uniform1i( _program.material, COLORSTROKE );
                    
                } else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {
                    
                    _gl.uniform1i( _program.material, FACECOLORFILL );
                
                } else if ( material instanceof THREE.MeshFaceColorStrokeMaterial ) {
U
unknown 已提交
304

305 306 307
                    lineWidth = material.lineWidth;
                    
                    _gl.uniform1i( _program.material, FACECOLORSTROKE );
U
unknown 已提交
308

309 310 311
                } else if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
                    
                    if ( !material.__webGLTexture && material.loaded ) {
U
unknown 已提交
312
                        
313
                        material.__webGLTexture = _gl.createTexture();
U
unknown 已提交
314
                        _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
315 316 317 318 319 320 321
                        _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, _gl.RGBA, _gl.UNSIGNED_BYTE, material.bitmap ) ;
                        _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.LINEAR );
                        //_gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_NEAREST );
                        _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.LINEAR_MIPMAP_LINEAR );
                        _gl.generateMipmap( _gl.TEXTURE_2D );
                        _gl.bindTexture( _gl.TEXTURE_2D, null );
                        
U
unknown 已提交
322
                    }
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                    
                    _gl.activeTexture( _gl.TEXTURE0 );
                    _gl.bindTexture( _gl.TEXTURE_2D, material.__webGLTexture );
                    _gl.uniform1i( _program.diffuse,  0 );
                    
                    _gl.uniform1i( _program.material, BITMAP );
                    
                }
                

                // vertices
                _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLVertexBuffer );
                _gl.vertexAttribPointer( _program.position, 3, _gl.FLOAT, false, 0, 0 );

                // normals
                _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLNormalBuffer );
                _gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );

                // colors
                _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLColorBuffer );
                _gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );
                
                // uvs
                
                if ( material instanceof THREE.MeshBitmapUVMappingMaterial ) {
                    
U
unknown 已提交
349 350
                    _gl.bindBuffer( _gl.ARRAY_BUFFER, materialFace.__webGLUVBuffer );
                        
351 352
                    _gl.enableVertexAttribArray( _program.uv );
                    _gl.vertexAttribPointer( _program.uv, 2, _gl.FLOAT, false, 0, 0 );
U
unknown 已提交
353
                        
354 355
                }
                else {
U
unknown 已提交
356
                        
357
                    _gl.disableVertexAttribArray( _program.uv );
U
unknown 已提交
358
                        
359
                }
U
unknown 已提交
360

361
                // render triangles
U
unknown 已提交
362

363 364 365 366
                if ( material instanceof THREE.MeshBitmapUVMappingMaterial || 
                     material instanceof THREE.MeshFaceColorFillMaterial ||
                     material instanceof THREE.MeshColorFillMaterial ) {
                    
U
unknown 已提交
367 368 369
                    _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLFaceBuffer );
                    _gl.drawElements( _gl.TRIANGLES, materialFace.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );
                    
370 371 372 373 374 375 376 377 378 379 380
                } 
                
                // render lines
                
                else if ( material instanceof THREE.MeshColorStrokeMaterial ||
                          material instanceof THREE.MeshFaceColorStrokeMaterial ) {
                    
                    _gl.lineWidth( lineWidth );
                    _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, materialFace.__webGLLineBuffer );
                    _gl.drawElements( _gl.LINES, materialFace.__webGLLineCount, _gl.UNSIGNED_SHORT, 0 );
                    
U
unknown 已提交
381
                }
382
                
U
unknown 已提交
383
            }
N
Nicolas Garcia Belmonte 已提交
384

385 386 387 388 389
        }
    };
    
    this.setupMatrices = function ( object, camera ) {
        
A
alteredq 已提交
390 391
        object.autoUpdateMatrix && object.updateMatrix();

392 393 394 395 396
        _viewMatrix.multiply( camera.matrix, object.matrix );

        _program.viewMatrixArray = new Float32Array( _viewMatrix.flatten() );
        _program.projectionMatrixArray = new Float32Array( camera.projectionMatrix.flatten() );

397 398 399
        _normalMatrix = THREE.Matrix4.makeInvert3x3( object.matrix ).transpose();
        _program.normalMatrixArray = new Float32Array( _normalMatrix.m );
        
400 401
        _gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
        _gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
402 403
        _gl.uniformMatrix3fv( _program.normalMatrix, false, _program.normalMatrixArray );
        _gl.uniformMatrix4fv( _program.objMatrix, false, new Float32Array( object.matrix.flatten() ) );
404 405 406 407 408
        
    };
    
	this.render = function ( scene, camera ) {
        
409 410
        camera.autoUpdateMatrix && camera.updateMatrix();

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
        var o, ol, object;

		if ( this.autoClear ) {

			this.clear();

		}

        this.setupLights( scene );

		for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {

			object = scene.objects[ o ];

            this.setupMatrices( object, camera );

			if ( object instanceof THREE.Mesh ) {

                this.renderMesh( object, camera );
                
            } else if ( object instanceof THREE.Line ) {
                
                // TODO

                // It would be very inefficient to do lines one-by-one.
                
                // This will need a complete redesign from how CanvasRenderer does it.
                
                // Though it could be brute forced, if only used for lightweight
                // stuff (as CanvasRenderer can only handle small number of elements 
                // anyways). 
                
                // Heavy-duty wireframe lines are handled efficiently in mesh renderer.

            } else if ( object instanceof THREE.Particle ) {
                
                // TODO
                
                // The same as with lines, particles shouldn't be handled one-by-one.
                
                // Again, heavy duty particle system would require different approach,
                // like one VBO per particle system and then update attribute arrays, 
                // though the best would be to move also behavior computation
                // into the shader (ala http://spidergl.org/example.php?id=11)
                
            }
            
N
Nicolas Garcia Belmonte 已提交
458 459 460 461 462 463 464 465
		}

	};

	function initGL() {

		try {

U
unknown 已提交
466
			_gl = _canvas.getContext( 'experimental-webgl', { antialias: true} );
N
Nicolas Garcia Belmonte 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

		} catch(e) { }

		if (!_gl) {

			alert("WebGL not supported");
			throw "cannot create webgl context";

		}

		_gl.clearColor( 0, 0, 0, 1 );
		_gl.clearDepth( 1 );

		_gl.enable( _gl.DEPTH_TEST );
		_gl.depthFunc( _gl.LEQUAL );

		_gl.enable( _gl.BLEND );
U
unknown 已提交
484
		//_gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
N
Nicolas Garcia Belmonte 已提交
485
		// _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); // cool!
U
unknown 已提交
486
         _gl.blendFunc( _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
N
Nicolas Garcia Belmonte 已提交
487 488 489 490 491 492 493 494 495
		_gl.clearColor( 0, 0, 0, 0 );

	}

	function initProgram() {

		_program = _gl.createProgram();

		_gl.attachShader( _program, getShader( "fragment", [
496 497 498 499
			"#ifdef GL_ES",
			"precision highp float;",
			"#endif",

U
unknown 已提交
500 501
            "uniform sampler2D diffuse;",

502 503 504 505
			"uniform vec4 uniformColor;",
        
            "varying vec2 vertexUv;",
			"varying vec4 vertexColor;",
506 507
			"varying vec3 lightWeighting;",

508 509
            "varying vec3 vNormal;",
        
510 511
            "uniform int material;", // 0 - ColorFill, 1 - ColorStroke, 2 - FaceColorFill, 3 - FaceColorStroke, 4 - Bitmap

512
			"void main(){",
513
                "if(material==4) {", // Bitmap: texture
514
                    "vec4 texelColor = texture2D(diffuse, vertexUv);",
U
unknown 已提交
515
                    "gl_FragColor = vec4(texelColor.rgb * lightWeighting, texelColor.a);",
516
                
517
                "} else if(material==3) {", // FaceColorStroke: wireframe using vertex color 
518 519
                    "gl_FragColor = vec4(vertexColor.rgb * lightWeighting, vertexColor.a);",
                
520
                "} else if(material==2) {", // FaceColorFill: triangle using vertex color
521 522
                    "gl_FragColor = vec4(vertexColor.rgb * lightWeighting, vertexColor.a);",
                
523
                "} else if(material==1) {", // ColorStroke: wireframe using uniform color
524 525
                    "gl_FragColor = vec4(uniformColor.rgb * lightWeighting, uniformColor.a);",
                
526
                "} else {", // ColorFill: triangle using uniform color
527
                    "gl_FragColor = vec4(uniformColor.rgb * lightWeighting, uniformColor.a);",
528
                    //"gl_FragColor = vec4(vNormal, 1.0);",
U
unknown 已提交
529
                "}",
530 531
			"}"
			].join("\n") ) );
N
Nicolas Garcia Belmonte 已提交
532 533

		_gl.attachShader( _program, getShader( "vertex", [
534 535 536
			"attribute vec3 position;",
			"attribute vec3 normal;",
			"attribute vec4 color;",
U
unknown 已提交
537
            "attribute vec2 uv;",
538 539 540 541 542 543

			"uniform bool enableLighting;",
			"uniform vec3 ambientColor;",
			"uniform vec3 directionalColor;",
			"uniform vec3 lightingDirection;",

544 545 546
			"uniform vec3 pointColor;",
			"uniform vec3 pointPosition;",

547 548
			"uniform mat4 viewMatrix;",
			"uniform mat4 projectionMatrix;",
549 550
			"uniform mat4 objMatrix;",
			"uniform mat3 normalMatrix;",
551 552 553
			
            "varying vec4 vertexColor;",
            "varying vec2 vertexUv;",
554 555
			"varying vec3 lightWeighting;",

556 557
            "varying vec3 vNormal;",
            
558
			"void main(void) {",
559 560 561
                "vec4 mvPosition = viewMatrix * vec4( position, 1.0 );",
                "vec4 mPosition = objMatrix * vec4( position, 1.0 );",
                "vec3 transformedNormal = normalize(normalMatrix * normal);",
562

563 564 565
                "if(!enableLighting) {",
                    "lightWeighting = vec3(1.0, 1.0, 1.0);",
                "} else {",
566 567 568 569
                    "vec3 pointLight = normalize(pointPosition.xyz - mPosition.xyz);",
                    "float directionalLightWeighting = max(dot(transformedNormal, normalize(lightingDirection)), 0.0);",
                    "float pointLightWeighting = max(dot(transformedNormal, pointLight), 0.0);",
                    "lightWeighting = ambientColor + directionalColor * directionalLightWeighting + pointColor * pointLightWeighting;",
570
                "}",
571

572 573
				"vNormal = transformedNormal;",
                "vertexColor = color;",
574
				"vertexUv = uv;",
575 576
                
				"gl_Position = projectionMatrix * mvPosition;",
577 578

			"}"].join("\n") ) );
N
Nicolas Garcia Belmonte 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591

		_gl.linkProgram( _program );

		if ( !_gl.getProgramParameter( _program, _gl.LINK_STATUS ) ) {

			alert( "Could not initialise shaders" );

		}

		_gl.useProgram( _program );

		_program.viewMatrix = _gl.getUniformLocation( _program, "viewMatrix" );
		_program.projectionMatrix = _gl.getUniformLocation( _program, "projectionMatrix" );
592
		_program.normalMatrix = _gl.getUniformLocation( _program, "normalMatrix" );
593
		_program.objMatrix = _gl.getUniformLocation( _program, "objMatrix" );
N
Nicolas Garcia Belmonte 已提交
594

595 596 597 598 599
		_program.enableLighting = _gl.getUniformLocation(_program, 'enableLighting');
		_program.ambientColor = _gl.getUniformLocation(_program, 'ambientColor');
		_program.directionalColor = _gl.getUniformLocation(_program, 'directionalColor');
		_program.lightingDirection = _gl.getUniformLocation(_program, 'lightingDirection');

600 601 602
		_program.pointColor = _gl.getUniformLocation(_program, 'pointColor');
		_program.pointPosition = _gl.getUniformLocation(_program, 'pointPosition');
        
603 604
        _program.material = _gl.getUniformLocation(_program, 'material');
        _program.uniformColor = _gl.getUniformLocation(_program, 'uniformColor');
U
unknown 已提交
605

N
Nicolas Garcia Belmonte 已提交
606 607 608 609 610 611
		_program.color = _gl.getAttribLocation( _program, "color" );
		_gl.enableVertexAttribArray( _program.color );

		_program.position = _gl.getAttribLocation( _program, "position" );
		_gl.enableVertexAttribArray( _program.position );

612 613
		_program.normal = _gl.getAttribLocation( _program, "normal" );
		_gl.enableVertexAttribArray( _program.normal );
614

U
unknown 已提交
615 616 617 618 619 620
		_program.uv = _gl.getAttribLocation( _program, "uv" );
		_gl.enableVertexAttribArray( _program.uv );

        _program.diffuse = _gl.getUniformLocation( _program, "diffuse");
        _gl.uniform1i( _program.diffuse,  0 );

N
Nicolas Garcia Belmonte 已提交
621 622
		_program.viewMatrixArray = new Float32Array(16);
		_program.projectionMatrixArray = new Float32Array(16);
N
Nicolas Garcia Belmonte 已提交
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

	}

	function getShader( type, string ) {

		var shader;

		if ( type == "fragment" ) {

			shader = _gl.createShader( _gl.FRAGMENT_SHADER );

		} else if ( type == "vertex" ) {

			shader = _gl.createShader( _gl.VERTEX_SHADER );

		}

		_gl.shaderSource( shader, string );
		_gl.compileShader( shader );

		if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {

			alert( _gl.getShaderInfoLog( shader ) );
			return null;

		}

		return shader;
	}

};