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

THREE.WebGLRenderer = function () {

	var _canvas = document.createElement( 'canvas' ), _gl, _program,
9
	viewMatrix = new THREE.Matrix4(), normalMatrix;
N
Nicolas Garcia Belmonte 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

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

	initGL();
	initProgram();

	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 );

	};

	this.render = function ( scene, camera ) {

33
		var face, faceColor, object, material, normal, lightColor, lightDirection, light,
34
		vertexArray, faceArray, colorArray, normalArray, vertexIndex,
35 36
		o, ol, f, fl, m, ml, i, v1, v2, v3, v4,
		l, ll;
N
Nicolas Garcia Belmonte 已提交
37 38 39 40 41 42 43

		if ( this.autoClear ) {

			this.clear();

		}

44 45
		//lighting
		_gl.uniform1i( _program.enableLighting, scene.lights.length );
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

		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 );

			} else if( light instanceof THREE.DirectionalLight ) {

				lightColor = light.color;
				lightDirection = light.direction;
				_gl.uniform3f( _program.lightingDirection, lightDirection.x, lightDirection.y, lightDirection.z );
				_gl.uniform3f( _program.directionalColor, lightColor.r, lightColor.g, lightColor.b );

			}

		}

		for ( o = 0, ol = scene.objects.length; o < ol; o++ ) {
N
Nicolas Garcia Belmonte 已提交
68 69 70 71 72 73 74 75 76 77

			object = scene.objects[ o ];

			if ( object instanceof THREE.Mesh ) {

				if ( !object.__webGLVertexBuffer ) {

					vertexArray = [];
					faceArray = [];
					colorArray = [];
78
					normalArray = [];
N
Nicolas Garcia Belmonte 已提交
79 80 81 82 83 84
					vertexIndex = 0;

					for ( f = 0, fl = object.geometry.faces.length; f < fl; f++ ) {

						face = object.geometry.faces[ f ];
						faceColor = face.color;
85
						normal = face.normal;
N
Nicolas Garcia Belmonte 已提交
86 87 88 89 90 91 92 93 94 95 96

						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 );

97 98 99
							normalArray.push( normal.x, normal.y, normal.z );
							normalArray.push( normal.x, normal.y, normal.z );
							normalArray.push( normal.x, normal.y, normal.z );
100

N
Nicolas Garcia Belmonte 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
							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 );

							faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );

							vertexIndex += 3;

						} else if ( face instanceof THREE.Face4 ) {

							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;

							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 );

121 122 123 124
							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 );
125

N
Nicolas Garcia Belmonte 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
							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 );

							faceArray.push( vertexIndex, vertexIndex + 1, vertexIndex + 2 );
							faceArray.push( vertexIndex, vertexIndex + 2, vertexIndex + 3 );

							vertexIndex += 4;
						}
					}

					if ( !vertexArray.length ) {

						continue;

					}

					object.__webGLVertexBuffer = _gl.createBuffer();
					_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLVertexBuffer );
N
Nicolas Garcia Belmonte 已提交
146
					_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( vertexArray ), _gl.STATIC_DRAW );
N
Nicolas Garcia Belmonte 已提交
147

148 149 150
					object.__webGLNormalBuffer = _gl.createBuffer();
					_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
					_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( normalArray ), _gl.STATIC_DRAW );
151

N
Nicolas Garcia Belmonte 已提交
152 153
					object.__webGLColorBuffer = _gl.createBuffer();
					_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLColorBuffer );
N
Nicolas Garcia Belmonte 已提交
154
					_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( colorArray ), _gl.STATIC_DRAW );
N
Nicolas Garcia Belmonte 已提交
155 156 157

					object.__webGLFaceBuffer = _gl.createBuffer();
					_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, object.__webGLFaceBuffer );
N
Nicolas Garcia Belmonte 已提交
158
					_gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, new Uint16Array( faceArray ), _gl.STATIC_DRAW );
N
Nicolas Garcia Belmonte 已提交
159 160 161 162 163 164 165 166

					object.__webGLFaceCount = faceArray.length;

				}


				viewMatrix.multiply( camera.matrix, object.matrix );

N
Nicolas Garcia Belmonte 已提交
167 168
				_program.viewMatrixArray = new Float32Array( viewMatrix.flatten() );
				_program.projectionMatrixArray = new Float32Array( camera.projectionMatrix.flatten() );
N
Nicolas Garcia Belmonte 已提交
169

170 171 172
				normalMatrix =  THREE.Matrix4.makeInvert(viewMatrix).transpose();
				_program.normalMatrixArray = new Float32Array( normalMatrix.flatten() );

N
Nicolas Garcia Belmonte 已提交
173 174
				_gl.uniformMatrix4fv( _program.viewMatrix, false, _program.viewMatrixArray );
				_gl.uniformMatrix4fv( _program.projectionMatrix, false, _program.projectionMatrixArray );
175
				_gl.uniformMatrix4fv( _program.normalMatrix, false, _program.normalMatrixArray );
N
Nicolas Garcia Belmonte 已提交
176 177 178 179

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

180 181
				_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLNormalBuffer );
				_gl.vertexAttribPointer( _program.normal, 3, _gl.FLOAT, false, 0, 0 );
N
Nicolas Garcia Belmonte 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

				for ( m = 0, ml = object.material.length; m < ml; m++ ) {

					material = object.material[ m ];

					if ( material instanceof THREE.MeshColorFillMaterial ) {

						if ( !material.__webGLColorBuffer ) {

							colorArray = [];

							for ( i = 0; i < object.__webGLFaceCount; i ++ ) {

								colorArray.push( material.color.r, material.color.g, material.color.b, material.color.a );

							}

							material.__webGLColorBuffer = _gl.createBuffer();
							_gl.bindBuffer( _gl.ARRAY_BUFFER, material.__webGLColorBuffer );
N
Nicolas Garcia Belmonte 已提交
201
							_gl.bufferData( _gl.ARRAY_BUFFER, new Float32Array( colorArray ), _gl.STATIC_DRAW );
N
Nicolas Garcia Belmonte 已提交
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

						}

						_gl.bindBuffer( _gl.ARRAY_BUFFER, material.__webGLColorBuffer );
						_gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );

					} else if ( material instanceof THREE.MeshFaceColorFillMaterial ) {

						_gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webGLColorBuffer );
						_gl.enableVertexAttribArray( _program.color );
						_gl.vertexAttribPointer( _program.color, 4, _gl.FLOAT, false, 0, 0 );

					}

				}

				_gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, object.__webGLFaceBuffer );
				_gl.drawElements( _gl.TRIANGLES, object.__webGLFaceCount, _gl.UNSIGNED_SHORT, 0 );

			}
		}

	};

	function initGL() {

		try {

			_gl = _canvas.getContext( 'experimental-webgl' );

		} 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 );
		_gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
		// _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE ); // cool!
		_gl.clearColor( 0, 0, 0, 0 );

	}

	function initProgram() {

		_program = _gl.createProgram();

		_gl.attachShader( _program, getShader( "fragment", [
259 260 261 262 263 264 265 266 267 268 269 270 271
			"#ifdef GL_ES",
			"precision highp float;",
			"#endif",

			"varying vec4 vcolor;",
			"varying vec3 lightWeighting;",

			"void main(){",

				"gl_FragColor = vec4(vcolor.rgb * lightWeighting, vcolor.a);",

			"}"
			].join("\n") ) );
N
Nicolas Garcia Belmonte 已提交
272 273

		_gl.attachShader( _program, getShader( "vertex", [
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
			"attribute vec3 position;",
			"attribute vec3 normal;",
			"attribute vec4 color;",

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

			"uniform mat4 viewMatrix;",
			"uniform mat4 projectionMatrix;",
			"uniform mat4 normalMatrix;",
			"varying vec4 vcolor;",
			"varying vec3 lightWeighting;",

			"void main(void) {",

						"if(!enableLighting) {",
							"lightWeighting = vec3(1.0, 1.0, 1.0);",
						"} else {",
							"vec4 transformedNormal = normalMatrix * vec4(normal, 1.0);",
							"float directionalLightWeighting = max(dot(transformedNormal.xyz, lightingDirection), 0.0);",
							"lightWeighting = ambientColor + directionalColor * directionalLightWeighting;",
						"}",

				"vcolor = color;",
				"gl_Position = projectionMatrix * viewMatrix * vec4( position, 1.0 );",

			"}"].join("\n") ) );
N
Nicolas Garcia Belmonte 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315

		_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" );
316
		_program.normalMatrix = _gl.getUniformLocation( _program, "normalMatrix" );
N
Nicolas Garcia Belmonte 已提交
317

318 319 320 321 322
		_program.enableLighting = _gl.getUniformLocation(_program, 'enableLighting');
		_program.ambientColor = _gl.getUniformLocation(_program, 'ambientColor');
		_program.directionalColor = _gl.getUniformLocation(_program, 'directionalColor');
		_program.lightingDirection = _gl.getUniformLocation(_program, 'lightingDirection');

N
Nicolas Garcia Belmonte 已提交
323 324 325 326 327 328
		_program.color = _gl.getAttribLocation( _program, "color" );
		_gl.enableVertexAttribArray( _program.color );

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

329 330
		_program.normal = _gl.getAttribLocation( _program, "normal" );
		_gl.enableVertexAttribArray( _program.normal );
331

N
Nicolas Garcia Belmonte 已提交
332 333
		_program.viewMatrixArray = new Float32Array(16);
		_program.projectionMatrixArray = new Float32Array(16);
N
Nicolas Garcia Belmonte 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

	}

	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;
	}

};