SVGRenderer.js 11.6 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

255
				if ( _vector3.z < - 1 || _vector3.z > 1 ) return;
256

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

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

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
269
	};
270

271
	function calculateLights( lights ) {
M
Mr.doob 已提交
272

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

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

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

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

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

M
Mugen87 已提交
288
			} else if ( light.isDirectionalLight ) {
289

290 291 292
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
293

M
Mugen87 已提交
294
			} else if ( light.isPointLight ) {
295

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

			}

		}

	}

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

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

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

M
Mugen87 已提交
313
			if ( light.isDirectionalLight ) {
M
Mr.doob 已提交
314

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

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

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

321 322 323 324 325
				amount *= light.intensity;

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

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

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

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

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

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

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

			}

		}

	}

351
	function renderSprite( v1, element, material ) {
352

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

356
		if ( material.isPointsMaterial ) {
M
Mugen87 已提交
357

358 359
			scaleX *= material.size;
			scaleY *= material.size;
M
Mugen87 已提交
360

361 362
		}

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

366
		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
367

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

		}

372
		addPath( style, path );
373 374

	}
M
Mr.doob 已提交
375

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

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

380
		if ( material.isLineBasicMaterial ) {
381

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

384
			if ( material.isLineDashedMaterial ) {
385

386
				style = style + ';stroke-dasharray:' + material.dashSize + "," + material.gapSize;
387

388
			}
389 390 391

			addPath( style, path );

392
		}
393

394
	}
M
Mr.doob 已提交
395

396
	function renderFace3( v1, v2, v3, element, material ) {
397

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

401
		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';
402
		var style = '';
403

M
Mugen87 已提交
404
		if ( material.isMeshBasicMaterial ) {
M
Mr.doob 已提交
405

M
Mr.doob 已提交
406
			_color.copy( material.color );
M
Mr.doob 已提交
407

408
			if ( material.vertexColors === THREE.FaceColors || material.vertexColors === THREE.VertexColors ) {
409

410
				_color.multiply( element.color );
411 412 413

			}

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

416
			_diffuseColor.copy( material.color );
417

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

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

			}

424
			_color.copy( _ambientLight );
425

426 427 428
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

430
			_color.multiply( _diffuseColor ).add( material.emissive );
431

M
Mugen87 已提交
432
		} else if ( material.isMeshNormalMaterial ) {
433

434
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
435

436
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
437

M
Mr.doob 已提交
438
		}
439

M
Mr.doob 已提交
440
		if ( material.wireframe ) {
441

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

M
Mr.doob 已提交
444
		} else {
445

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

		}

450 451 452 453
		addPath( style, path );

	}

M
Mugen87 已提交
454
	function addPath( style, path ) {
455

M
Mr.doob 已提交
456
		if ( _currentStyle === style ) {
457

M
Mugen87 已提交
458
			_currentPath += path;
459 460 461

		} else {

462
			flushPath();
463

M
Mr.doob 已提交
464 465
			_currentStyle = style;
			_currentPath = path;
466 467

		}
468 469 470

	}

471 472
	function flushPath() {

M
Mr.doob 已提交
473
		if ( _currentPath ) {
474 475

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

		}

M
Mr.doob 已提交
482 483 484
		_currentPath = '';
		_currentStyle = '';

485 486
	}

487
	function getPathNode( id ) {
488

489
		if ( _svgPathPool[ id ] == null ) {
490 491 492

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

493
			if ( _quality == 0 ) {
494 495 496 497 498 499 500

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

			}

			return _svgPathPool[ id ];

501
		}
502 503 504

		return _svgPathPool[ id ];

M
Mr.doob 已提交
505
	}
506

M
Mr.doob 已提交
507
};