SVGRenderer.js 12.2 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
THREE.SVGObject = function ( node ) {

	THREE.Object3D.call( this );

	this.node = node;

};

THREE.SVGObject.prototype = Object.create( THREE.Object3D.prototype );
14
THREE.SVGObject.prototype.constructor = THREE.SVGObject;
15

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

18
	console.log( 'THREE.SVGRenderer', THREE.REVISION );
19

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

M
Mugen87 已提交
26
		_v1, _v2, _v3,
27

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

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

M
Mugen87 已提交
39 40 41 42
		_vector3 = new THREE.Vector3(), // Needed for PointLight
		_centroid = new THREE.Vector3(),
		_normal = new THREE.Vector3(),
		_normalViewMatrix = new THREE.Matrix3(),
M
Mr.doob 已提交
43

M
Mugen87 已提交
44 45
		_viewMatrix = new THREE.Matrix4(),
		_viewProjectionMatrix = new THREE.Matrix4(),
46

M
Mugen87 已提交
47 48
		_svgPathPool = [],
		_svgNode, _pathCount = 0,
M
Mr.doob 已提交
49

M
Mugen87 已提交
50
		_currentPath, _currentStyle,
M
Mr.doob 已提交
51

M
Mugen87 已提交
52
		_quality = 1, _precision = null;
53

54
	this.domElement = _svg;
M
Mr.doob 已提交
55

M
Mr.doob 已提交
56
	this.autoClear = true;
M
Mr.doob 已提交
57 58
	this.sortObjects = true;
	this.sortElements = true;
M
Mr.doob 已提交
59

60 61
	this.overdraw = 0.5;

M
Mr.doob 已提交
62
	this.info = {
63

M
Mr.doob 已提交
64 65 66 67 68 69
		render: {

			vertices: 0,
			faces: 0

		}
70

B
brason 已提交
71
	};
72

M
Mugen87 已提交
73
	this.setQuality = function ( quality ) {
74

G
gero3 已提交
75
		switch ( quality ) {
76 77 78 79 80

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

		}
M
Mr.doob 已提交
81 82

	};
83

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

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

	};

M
Mr.doob 已提交
91 92
	this.setPixelRatio = function () {};

M
Mugen87 已提交
93
	this.setSize = function ( width, height ) {
94

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

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

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

M
Mr.doob 已提交
105
	};
106

107 108 109 110 111 112
	this.setPrecision = function ( precision ) {

		_precision = precision;

	};

113
	function removeChildNodes() {
M
Mr.doob 已提交
114

115 116
		_pathCount = 0;

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

119
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
120 121

		}
122

123 124
	}

M
migiyubi 已提交
125
	function getSvgColor( color, opacity, asStroke ) {
126 127 128 129 130

		var arg = Math.floor( color.r * 255 ) + ',' + Math.floor( color.g * 255 ) + ',' + Math.floor( color.b * 255 );

		if ( opacity === undefined || opacity === 1 ) return 'rgb(' + arg + ')';

M
migiyubi 已提交
131
		return 'rgb(' + arg + ');' + ( asStroke ? 'stroke-opacity' : 'fill-opacity' ) + ':' + opacity;
132 133 134

	}

M
Mugen87 已提交
135
	function convert( c ) {
136

M
Mugen87 已提交
137
		return _precision !== null ? c.toFixed( _precision ) : c;
138 139 140

	}

141 142 143
	this.clear = function () {

		removeChildNodes();
144
		_svg.style.backgroundColor = getSvgColor( _clearColor, _clearAlpha );
M
Mr.doob 已提交
145 146 147

	};

148
	this.render = function ( scene, camera ) {
149

M
Mr.doob 已提交
150 151
		if ( camera instanceof THREE.Camera === false ) {

152
			console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
153 154 155 156
			return;

		}

157 158 159 160 161
		var background = scene.background;

		if ( background && background.isColor ) {

			removeChildNodes();
162
			_svg.style.backgroundColor = getSvgColor( background );
163 164 165 166 167 168

		} else if ( this.autoClear === true ) {

			this.clear();

		}
169

M
Mr.doob 已提交
170 171
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
172

173
		_viewMatrix.copy( camera.matrixWorldInverse );
174 175
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

176
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
177 178
		_elements = _renderData.elements;
		_lights = _renderData.lights;
179

180 181
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

182
		calculateLights( _lights );
M
Mr.doob 已提交
183 184 185 186 187

		 // reset accumulated path

		_currentPath = '';
		_currentStyle = '';
188

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

191 192
			var element = _elements[ e ];
			var material = element.material;
193

194
			if ( material === undefined || material.opacity === 0 ) continue;
195

M
Mr.doob 已提交
196
			_elemBox.makeEmpty();
197

198
			if ( element instanceof THREE.RenderableSprite ) {
199

200
				_v1 = element;
M
Mr.doob 已提交
201
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
202

203
				renderSprite( _v1, element, material );
204

205
			} else if ( element instanceof THREE.RenderableLine ) {
206

207
				_v1 = element.v1; _v2 = element.v2;
208

209 210
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
211

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

214
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
215

216
					renderLine( _v1, _v2, element, material );
217

218
				}
219

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

222
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
223

G
gero3 已提交
224 225 226
				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;
227

228 229 230 231
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

W
WestLangley 已提交
232 233 234 235 236 237 238 239
				if ( this.overdraw > 0 ) {

					expand( _v1.positionScreen, _v2.positionScreen, this.overdraw );
					expand( _v2.positionScreen, _v3.positionScreen, this.overdraw );
					expand( _v3.positionScreen, _v1.positionScreen, this.overdraw );

				}

240 241 242 243 244
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
245

246
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
247 248 249 250

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

				}
251

M
Mr.doob 已提交
252
			}
253 254 255

		}

256
		flushPath(); // just to flush last svg:path
257

258 259 260 261 262
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

				_vector3.setFromMatrixPosition( object.matrixWorld );
F
Franklin Ta 已提交
263
				_vector3.applyMatrix4( _viewProjectionMatrix );
264

265
				if ( _vector3.z < - 1 || _vector3.z > 1 ) return;
266

M
Mugen87 已提交
267
				var x = _vector3.x * _svgWidthHalf;
268 269
				var y = - _vector3.y * _svgHeightHalf;

G
gero3 已提交
270
				var node = object.node;
271 272 273 274 275 276 277 278
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
279
	};
280

281
	function calculateLights( lights ) {
M
Mr.doob 已提交
282

M
Mr.doob 已提交
283
		_ambientLight.setRGB( 0, 0, 0 );
284 285
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
286

G
gero3 已提交
287
		for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
M
Mr.doob 已提交
288

289 290
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
291

M
Mugen87 已提交
292
			if ( light.isAmbientLight ) {
M
Mr.doob 已提交
293 294 295 296 297

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

M
Mugen87 已提交
298
			} else if ( light.isDirectionalLight ) {
299

300 301 302
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
303

M
Mugen87 已提交
304
			} else if ( light.isPointLight ) {
305

306 307 308
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
309 310 311 312 313 314 315

			}

		}

	}

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

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

320 321
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
322

M
Mugen87 已提交
323
			if ( light.isDirectionalLight ) {
M
Mr.doob 已提交
324

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

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

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

331 332 333 334 335
				amount *= light.intensity;

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

M
Mugen87 已提交
337
			} else if ( light.isPointLight ) {
M
Mr.doob 已提交
338

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

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

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

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

347 348 349 350 351 352 353
				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 已提交
354 355 356 357 358 359 360

			}

		}

	}

361
	function renderSprite( v1, element, material ) {
362

M
Mr.doob 已提交
363 364
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
365

366
		if ( material.isPointsMaterial ) {
M
Mugen87 已提交
367

368 369
			scaleX *= material.size;
			scaleY *= material.size;
M
Mugen87 已提交
370

371 372
		}

M
Mugen87 已提交
373
		var path = 'M' + convert( v1.x - scaleX * 0.5 ) + ',' + convert( v1.y - scaleY * 0.5 ) + 'h' + convert( scaleX ) + 'v' + convert( scaleY ) + 'h' + convert( - scaleX ) + 'z';
374
		var style = "";
375

376
		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
377

378
			style = 'fill:' + getSvgColor( material.color, material.opacity );
379 380 381

		}

382
		addPath( style, path );
383 384

	}
M
Mr.doob 已提交
385

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

388
		var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y );
389

390
		if ( material.isLineBasicMaterial ) {
391

M
migiyubi 已提交
392
			var style = 'fill:none;stroke:' + getSvgColor( material.color, material.opacity, true ) + ';stroke-width:' + material.linewidth + ';stroke-linecap:' + material.linecap;
393

394
			if ( material.isLineDashedMaterial ) {
395

396
				style = style + ';stroke-dasharray:' + material.dashSize + "," + material.gapSize;
397

398
			}
399 400 401

			addPath( style, path );

402
		}
403

404
	}
M
Mr.doob 已提交
405

406
	function renderFace3( v1, v2, v3, element, material ) {
407

M
Mr.doob 已提交
408 409
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
410

411
		var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y ) + 'L' + convert( v3.positionScreen.x ) + ',' + convert( v3.positionScreen.y ) + 'z';
412
		var style = '';
413

M
Mugen87 已提交
414
		if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
415

M
Mr.doob 已提交
416
			_color.copy( material.color );
M
Mr.doob 已提交
417

418
			if ( material.vertexColors === THREE.FaceColors || material.vertexColors === THREE.VertexColors ) {
419

420
				_color.multiply( element.color );
421 422 423

			}

M
Mugen87 已提交
424
		} else if ( material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) {
425

426
			_diffuseColor.copy( material.color );
427

428
			if ( material.vertexColors === THREE.FaceColors || material.vertexColors === THREE.VertexColors ) {
429

430
				_diffuseColor.multiply( element.color );
431 432 433

			}

434
			_color.copy( _ambientLight );
435

436 437 438
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

440
			_color.multiply( _diffuseColor ).add( material.emissive );
441

M
Mugen87 已提交
442
		} else if ( material.isMeshNormalMaterial ) {
443

444
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
445

446
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
447

M
Mr.doob 已提交
448
		}
449

M
Mr.doob 已提交
450
		if ( material.wireframe ) {
451

M
migiyubi 已提交
452
			style = 'fill:none;stroke:' + getSvgColor( _color, material.opacity, true ) + ';stroke-width:' + material.wireframeLinewidth + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
453

M
Mr.doob 已提交
454
		} else {
455

456
			style = 'fill:' + getSvgColor( _color, material.opacity );
457 458 459

		}

460 461 462 463
		addPath( style, path );

	}

W
WestLangley 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	// Hide anti-alias gaps

	function expand( v1, v2, pixels ) {

		var x = v2.x - v1.x, y = v2.y - v1.y,
			det = x * x + y * y, idet;

		if ( det === 0 ) return;

		idet = pixels / Math.sqrt( det );

		x *= idet; y *= idet;

		v2.x += x; v2.y += y;
		v1.x -= x; v1.y -= y;

	}

M
Mugen87 已提交
482
	function addPath( style, path ) {
483

M
Mr.doob 已提交
484
		if ( _currentStyle === style ) {
485

M
Mugen87 已提交
486
			_currentPath += path;
487 488 489

		} else {

490
			flushPath();
491

M
Mr.doob 已提交
492 493
			_currentStyle = style;
			_currentPath = path;
494 495

		}
496 497 498

	}

499 500
	function flushPath() {

M
Mr.doob 已提交
501
		if ( _currentPath ) {
502 503

			_svgNode = getPathNode( _pathCount ++ );
M
Mr.doob 已提交
504 505
			_svgNode.setAttribute( 'd', _currentPath );
			_svgNode.setAttribute( 'style', _currentStyle );
506 507 508 509
			_svg.appendChild( _svgNode );

		}

M
Mr.doob 已提交
510 511 512
		_currentPath = '';
		_currentStyle = '';

513 514
	}

515
	function getPathNode( id ) {
516

517
		if ( _svgPathPool[ id ] == null ) {
518 519 520

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

521
			if ( _quality == 0 ) {
522 523 524 525 526 527 528

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

			}

			return _svgPathPool[ id ];

529
		}
530 531 532

		return _svgPathPool[ id ];

M
Mr.doob 已提交
533
	}
534

M
Mr.doob 已提交
535
};