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

M
Mr.doob 已提交
60
	this.info = {
61

M
Mr.doob 已提交
62 63 64 65 66 67
		render: {

			vertices: 0,
			faces: 0

		}
68

B
brason 已提交
69
	};
70

M
Mugen87 已提交
71
	this.setQuality = function ( quality ) {
72

G
gero3 已提交
73
		switch ( quality ) {
74 75 76 77 78

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

		}
M
Mr.doob 已提交
79 80

	};
81

82 83
	this.setClearColor = function ( color, alpha ) {

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

	};

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

M
Mugen87 已提交
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

105 106 107 108 109 110
	this.setPrecision = function ( precision ) {

		_precision = precision;

	};

111
	function removeChildNodes() {
M
Mr.doob 已提交
112

113 114
		_pathCount = 0;

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

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

		}
120

121 122
	}

M
Mugen87 已提交
123
	function getSvgColor( color, opacity ) {
124 125 126 127 128

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

129
		return 'rgb(' + arg + '); fill-opacity: ' + opacity;
130 131 132

	}

M
Mugen87 已提交
133
	function convert( c ) {
134

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

	}

139 140 141
	this.clear = function () {

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

	};

146
	this.render = function ( scene, camera ) {
147

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

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

		}

155 156 157 158 159
		var background = scene.background;

		if ( background && background.isColor ) {

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

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

			this.clear();

		}
167

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

171
		_viewMatrix.copy( camera.matrixWorldInverse );
172 173
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

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

178 179
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

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

		 // reset accumulated path

		_currentPath = '';
		_currentStyle = '';
186

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

189 190
			var element = _elements[ e ];
			var material = element.material;
191

192
			if ( material === undefined || material.opacity === 0 ) continue;
193

M
Mr.doob 已提交
194
			_elemBox.makeEmpty();
195

196
			if ( element instanceof THREE.RenderableSprite ) {
197

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

201
				renderSprite( _v1, element, material );
202

203
			} else if ( element instanceof THREE.RenderableLine ) {
204

205
				_v1 = element.v1; _v2 = element.v2;
206

207 208
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
209

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

212
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
213

214
					renderLine( _v1, _v2, element, material );
215

216
				}
217

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

220
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
221

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

226 227 228 229
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

230 231 232 233 234
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
235

236
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
237 238 239 240

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

				}
241

M
Mr.doob 已提交
242
			}
243 244 245

		}

246
		flushPath(); // just to flush last svg:path
247

248 249 250 251 252
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

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

M
Mugen87 已提交
255
				var x = _vector3.x * _svgWidthHalf;
256 257
				var y = - _vector3.y * _svgHeightHalf;

G
gero3 已提交
258
				var node = object.node;
259 260 261 262 263 264 265 266
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
267
	};
268

269
	function calculateLights( lights ) {
M
Mr.doob 已提交
270

M
Mr.doob 已提交
271
		_ambientLight.setRGB( 0, 0, 0 );
272 273
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
274

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

277 278
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
279

M
Mugen87 已提交
280
			if ( light.isAmbientLight ) {
M
Mr.doob 已提交
281 282 283 284 285

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

M
Mugen87 已提交
286
			} else if ( light.isDirectionalLight ) {
287

288 289 290
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
291

M
Mugen87 已提交
292
			} else if ( light.isPointLight ) {
293

294 295 296
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
297 298 299 300 301 302 303

			}

		}

	}

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

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

308 309
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
310

M
Mugen87 已提交
311
			if ( light.isDirectionalLight ) {
M
Mr.doob 已提交
312

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

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

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

319 320 321 322 323
				amount *= light.intensity;

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

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

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

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

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

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

335 336 337 338 339 340 341
				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 已提交
342 343 344 345 346 347 348

			}

		}

	}

349
	function renderSprite( v1, element, material ) {
350

M
Mr.doob 已提交
351 352
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
353

354
		if ( material.isPointsMaterial ) {
M
Mugen87 已提交
355

356 357
			scaleX *= material.size;
			scaleY *= material.size;
M
Mugen87 已提交
358

359 360
		}

M
Mugen87 已提交
361
		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';
362
		var style = "";
363

364
		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
365

366
			style = 'fill:' + getSvgColor( material.color, material.opacity );
367 368 369

		}

370
		addPath( style, path );
371 372

	}
M
Mr.doob 已提交
373

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

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

378
		if ( material.isLineBasicMaterial ) {
379

380
			var style = 'fill:none;stroke:' + getSvgColor( material.color, material.opacity ) + ';stroke-width:' + material.linewidth + ';stroke-linecap:' + material.linecap;
381

382
			if ( material.isLineDashedMaterial ) {
383

384
				style = style + ';stroke-dasharray:' + material.dashSize + "," + material.gapSize;
385

386
			}
387 388 389

			addPath( style, path );

390
		}
391

392
	}
M
Mr.doob 已提交
393

394
	function renderFace3( v1, v2, v3, element, material ) {
395

M
Mr.doob 已提交
396 397
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
398

399
		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';
400
		var style = '';
401

M
Mugen87 已提交
402
		if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
403

M
Mr.doob 已提交
404
			_color.copy( material.color );
M
Mr.doob 已提交
405

406 407
			if ( material.vertexColors === THREE.FaceColors ) {

408
				_color.multiply( element.color );
409 410 411

			}

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

414
			_diffuseColor.copy( material.color );
415

416 417
			if ( material.vertexColors === THREE.FaceColors ) {

418
				_diffuseColor.multiply( element.color );
419 420 421

			}

422
			_color.copy( _ambientLight );
423

424 425 426
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

428
			_color.multiply( _diffuseColor ).add( material.emissive );
429

M
Mugen87 已提交
430
		} else if ( material.isMeshNormalMaterial ) {
431

432
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
433

434
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
435

M
Mr.doob 已提交
436
		}
437

M
Mr.doob 已提交
438
		if ( material.wireframe ) {
439

440
			style = 'fill:none;stroke:' + getSvgColor( _color, material.opacity ) + ';stroke-width:' + material.wireframeLinewidth + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
441

M
Mr.doob 已提交
442
		} else {
443

444
			style = 'fill:' + getSvgColor( _color, material.opacity );
445 446 447

		}

448 449 450 451
		addPath( style, path );

	}

M
Mugen87 已提交
452
	function addPath( style, path ) {
453

M
Mr.doob 已提交
454
		if ( _currentStyle === style ) {
455

M
Mugen87 已提交
456
			_currentPath += path;
457 458 459

		} else {

460
			flushPath();
461

M
Mr.doob 已提交
462 463
			_currentStyle = style;
			_currentPath = path;
464 465

		}
466 467 468

	}

469 470
	function flushPath() {

M
Mr.doob 已提交
471
		if ( _currentPath ) {
472 473

			_svgNode = getPathNode( _pathCount ++ );
M
Mr.doob 已提交
474 475
			_svgNode.setAttribute( 'd', _currentPath );
			_svgNode.setAttribute( 'style', _currentStyle );
476 477 478 479
			_svg.appendChild( _svgNode );

		}

M
Mr.doob 已提交
480 481 482
		_currentPath = '';
		_currentStyle = '';

483 484
	}

485
	function getPathNode( id ) {
486

487
		if ( _svgPathPool[ id ] == null ) {
488 489 490

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

491
			if ( _quality == 0 ) {
492 493 494 495 496 497 498

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

			}

			return _svgPathPool[ id ];

499
		}
500 501 502

		return _svgPathPool[ id ];

M
Mr.doob 已提交
503
	}
504

M
Mr.doob 已提交
505
};