SVGRenderer.js 11.5 KB
Newer Older
M
Mr.doob 已提交
1
/**
2
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
3 4
 */

5 6 7 8 9 10 11 12 13 14
THREE.SVGObject = function ( node ) {

	THREE.Object3D.call( this );

	this.node = node;

};

THREE.SVGObject.prototype = Object.create( THREE.Object3D.prototype );

M
Mr.doob 已提交
15
THREE.SVGRenderer = function () {
16

M
Mr.doob 已提交
17
	console.log( 'THREE.SVGRenderer', THREE.REVISION );
18

19
	var _this = this,
20
	_renderData, _elements, _lights,
M
Mr.doob 已提交
21 22
	_projector = new THREE.Projector(),
	_svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
M
Mr.doob 已提交
23
	_svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
24 25 26

	_v1, _v2, _v3, _v4,

M
Mr.doob 已提交
27 28
	_clipBox = new THREE.Box2(),
	_elemBox = new THREE.Box2(),
M
Mr.doob 已提交
29

30
	_color = new THREE.Color(),
31
	_diffuseColor = new THREE.Color(),
32 33 34
	_ambientLight = new THREE.Color(),
	_directionalLights = new THREE.Color(),
	_pointLights = new THREE.Color(),
S
srifqi 已提交
35
	_clearColor = new THREE.Color(),
36
	_clearAlpha = 1,
M
Mr.doob 已提交
37

M
Mr.doob 已提交
38
	_w, // z-buffer to w-buffer
M
Mr.doob 已提交
39
	_vector3 = new THREE.Vector3(), // Needed for PointLight
40
	_centroid = new THREE.Vector3(),
41 42
	_normal = new THREE.Vector3(),
	_normalViewMatrix = new THREE.Matrix3(),
M
Mr.doob 已提交
43

44 45 46
	_viewMatrix = new THREE.Matrix4(),
	_viewProjectionMatrix = new THREE.Matrix4(),

M
Mr.doob 已提交
47 48
	_svgPathPool = [], _svgLinePool = [], _svgRectPool = [],
	_svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
49 50
	_quality = 1;

51
	this.domElement = _svg;
M
Mr.doob 已提交
52

M
Mr.doob 已提交
53
	this.autoClear = true;
M
Mr.doob 已提交
54 55
	this.sortObjects = true;
	this.sortElements = true;
M
Mr.doob 已提交
56

M
Mr.doob 已提交
57
	this.info = {
58

M
Mr.doob 已提交
59 60 61 62 63 64
		render: {

			vertices: 0,
			faces: 0

		}
65 66 67

	}

68 69 70 71 72 73 74 75
	this.setQuality = function( quality ) {

		switch(quality) {

			case "high": _quality = 1; break;
			case "low": _quality = 0; break;

		}
M
Mr.doob 已提交
76 77

	};
78

79 80 81 82 83
	// WebGLRenderer compatibility

	this.supportsVertexTextures = function () {};
	this.setFaceCulling = function () {};

84 85
	this.setClearColor = function ( color, alpha ) {

86 87
		_clearColor.set( color );
		_clearAlpha = alpha !== undefined ? alpha : 1;
88 89 90

	};

91
	this.setSize = function( width, height ) {
92

M
Mr.doob 已提交
93 94
		_svgWidth = width; _svgHeight = height;
		_svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
95

M
Mr.doob 已提交
96 97 98
		_svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
		_svg.setAttribute( 'width', _svgWidth );
		_svg.setAttribute( 'height', _svgHeight );
99

M
Mr.doob 已提交
100 101
		_clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
		_clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
102

M
Mr.doob 已提交
103
	};
104

M
Mr.doob 已提交
105 106
	this.clear = function () {

107 108
		_pathCount = 0;
		_lineCount = 0;
M
Mr.doob 已提交
109
		_rectCount = 0;
110

111
		while ( _svg.childNodes.length > 0 ) {
M
Mr.doob 已提交
112

113
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
114 115

		}
S
srifqi 已提交
116
		
117
		_svg.style.backgroundColor = 'rgba(' + ( ( _clearColor.r * 255 ) | 0 ) + ',' + ( ( _clearColor.g * 255 ) | 0 ) + ',' + ( ( _clearColor.b * 255 ) | 0 ) + ',' + _clearAlpha + ')';
M
Mr.doob 已提交
118 119 120

	};

121
	this.render = function ( scene, camera ) {
122

M
Mr.doob 已提交
123 124 125 126 127 128 129
		if ( camera instanceof THREE.Camera === false ) {

			console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
			return;

		}

130
		if ( this.autoClear === true ) this.clear();
131

M
Mr.doob 已提交
132 133
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
134

135 136 137
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

138
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
139 140
		_elements = _renderData.elements;
		_lights = _renderData.lights;
141

142 143
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

144
		calculateLights( _lights );
145

146
		for ( var e = 0, el = _elements.length; e < el; e ++ ) {
147

148 149
			var element = _elements[ e ];
			var material = element.material;
150

151
			if ( material === undefined || material.opacity === 0 ) continue;
152

M
Mr.doob 已提交
153
			_elemBox.makeEmpty();
154

155
			if ( element instanceof THREE.RenderableSprite ) {
156

157
				_v1 = element;
M
Mr.doob 已提交
158
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
159

160
				renderSprite( _v1, element, material );
161

162
			} else if ( element instanceof THREE.RenderableLine ) {
163

164
				_v1 = element.v1; _v2 = element.v2;
165

166 167
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
168

M
Mr.doob 已提交
169
				_elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
170

M
Mr.doob 已提交
171
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
172

173
					renderLine( _v1, _v2, element, material );
174

175
				}
176

M
Mr.doob 已提交
177
			} else if ( element instanceof THREE.RenderableFace ) {
178

179
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
180

181 182 183 184
				if ( _v1.positionScreen.z < -1 || _v1.positionScreen.z > 1 ) continue;
				if ( _v2.positionScreen.z < -1 || _v2.positionScreen.z > 1 ) continue;
				if ( _v3.positionScreen.z < -1 || _v3.positionScreen.z > 1 ) continue;

185 186 187 188
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

189 190 191 192 193
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
194

195 196 197 198 199
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

					renderFace3( _v1, _v2, _v3, element, material );

				}
200

M
Mr.doob 已提交
201
			}
202 203 204

		}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

				_vector3.setFromMatrixPosition( object.matrixWorld );
				_vector3.applyProjection( _viewProjectionMatrix );

				var x =   _vector3.x * _svgWidthHalf;
				var y = - _vector3.y * _svgHeightHalf;

			 	var node = object.node;
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
224
	};
225

226
	function calculateLights( lights ) {
M
Mr.doob 已提交
227

M
Mr.doob 已提交
228
		_ambientLight.setRGB( 0, 0, 0 );
229 230
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
231

232
		for ( var l = 0, ll = lights.length; l < ll; l++ ) {
M
Mr.doob 已提交
233

234 235
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
236

M
Mr.doob 已提交
237 238 239 240 241 242 243
			if ( light instanceof THREE.AmbientLight ) {

				_ambientLight.r += lightColor.r;
				_ambientLight.g += lightColor.g;
				_ambientLight.b += lightColor.b;

			} else if ( light instanceof THREE.DirectionalLight ) {
244

245 246 247
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
248 249 250

			} else if ( light instanceof THREE.PointLight ) {

251 252 253
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
254 255 256 257 258 259 260

			}

		}

	}

261
	function calculateLight( lights, position, normal, color ) {
M
Mr.doob 已提交
262

263
		for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
M
Mr.doob 已提交
264

265 266
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
267 268 269

			if ( light instanceof THREE.DirectionalLight ) {

270
				var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
M
Mr.doob 已提交
271

272
				var amount = normal.dot( lightPosition );
M
Mr.doob 已提交
273

274
				if ( amount <= 0 ) continue;
M
Mr.doob 已提交
275

276 277 278 279 280
				amount *= light.intensity;

				color.r += lightColor.r * amount;
				color.g += lightColor.g * amount;
				color.b += lightColor.b * amount;
M
Mr.doob 已提交
281 282 283

			} else if ( light instanceof THREE.PointLight ) {

284
				var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
M
Mr.doob 已提交
285

286
				var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
M
Mr.doob 已提交
287

288
				if ( amount <= 0 ) continue;
M
Mr.doob 已提交
289

290
				amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
M
Mr.doob 已提交
291

292 293 294 295 296 297 298
				if ( amount == 0 ) continue;

				amount *= light.intensity;

				color.r += lightColor.r * amount;
				color.g += lightColor.g * amount;
				color.b += lightColor.b * amount;
M
Mr.doob 已提交
299 300 301 302 303 304 305

			}

		}

	}

306
	function renderSprite( v1, element, material ) {
307

M
Mr.doob 已提交
308 309
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
310

M
Mr.doob 已提交
311
		_svgNode = getRectNode( _rectCount ++ );
312

M
Mr.doob 已提交
313 314 315 316
		_svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
		_svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
		_svgNode.setAttribute( 'width', scaleX );
		_svgNode.setAttribute( 'height', scaleY );
317

318
		if ( material instanceof THREE.SpriteMaterial ) {
319

320
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
321 322 323 324 325 326

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
327

M
Mr.doob 已提交
328
	function renderLine( v1, v2, element, material ) {
329

330
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
331

332 333 334 335 336
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

337
		if ( material instanceof THREE.LineBasicMaterial ) {
338

339
			_svgNode.setAttribute( 'style', 'fill: none; stroke: ' + material.color.getStyle() + '; stroke-width: ' + material.linewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.linecap + '; stroke-linejoin: ' + material.linejoin );
340

341
			_svg.appendChild( _svgNode );
342

343
		}
344

345
	}
M
Mr.doob 已提交
346

347
	function renderFace3( v1, v2, v3, element, material ) {
348

M
Mr.doob 已提交
349 350
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
351

352
		_svgNode = getPathNode( _pathCount ++ );
353
		_svgNode.setAttribute( 'd', 'M ' + v1.positionScreen.x + ' ' + v1.positionScreen.y + ' L ' + v2.positionScreen.x + ' ' + v2.positionScreen.y + ' L ' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z' );
354

M
Mr.doob 已提交
355 356
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
357
			_color.copy( material.color );
M
Mr.doob 已提交
358

359 360
			if ( material.vertexColors === THREE.FaceColors ) {

361
				_color.multiply( element.color );
362 363 364

			}

365
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
366

367
			_diffuseColor.copy( material.color );
368

369 370
			if ( material.vertexColors === THREE.FaceColors ) {

371
				_diffuseColor.multiply( element.color );
372 373 374

			}

375
			_color.copy( _ambientLight );
376

377 378 379
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

			calculateLight( _lights, _centroid, element.normalModel, _color );
380

381
			_color.multiply( _diffuseColor ).add( material.emissive );
382

383 384 385
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
386
			_color.setRGB( _w, _w, _w );
387 388 389

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

390
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
391

392
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
393

M
Mr.doob 已提交
394
		}
395

M
Mr.doob 已提交
396
		if ( material.wireframe ) {
397

398
			_svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
399

M
Mr.doob 已提交
400
		} else {
401

402
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
403 404 405 406 407 408 409

		}

		_svg.appendChild( _svgNode );

	}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	function getLineNode( id ) {

		if ( _svgLinePool[ id ] == null ) {

			_svgLinePool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'line' );

			if ( _quality == 0 ) {

				_svgLinePool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed

			}

			return _svgLinePool[ id ];

		}

		return _svgLinePool[ id ];

	}
429

430
	function getPathNode( id ) {
431

432
		if ( _svgPathPool[ id ] == null ) {
433 434 435

			_svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );

436
			if ( _quality == 0 ) {
437 438 439 440 441 442 443

				_svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed

			}

			return _svgPathPool[ id ];

444
		}
445 446 447

		return _svgPathPool[ id ];

M
Mr.doob 已提交
448
	}
449

450
	function getRectNode( id ) {
451

452
		if ( _svgRectPool[ id ] == null ) {
453

454
			_svgRectPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'rect' );
455

456
			if ( _quality == 0 ) {
457

M
Mr.doob 已提交
458
				_svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
459 460 461

			}

462
			return _svgRectPool[ id ];
463

464
		}
465

466
		return _svgRectPool[ id ];
467 468 469

	}

M
Mr.doob 已提交
470
};