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,
21
	_renderData, _elements, _lights,
22
	_projector = new THREE.Projector(),
G
gero3 已提交
23
	_svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ),
M
Mr.doob 已提交
24
	_svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
25 26 27

	_v1, _v2, _v3, _v4,

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

31
	_color = new THREE.Color(),
32
	_diffuseColor = new THREE.Color(),
33 34 35
	_ambientLight = new THREE.Color(),
	_directionalLights = new THREE.Color(),
	_pointLights = new THREE.Color(),
S
srifqi 已提交
36
	_clearColor = new THREE.Color(),
37
	_clearAlpha = 1,
M
Mr.doob 已提交
38 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(),

47
	_svgPathPool = [],
48
	_svgNode, _pathCount = 0, _currPath, _currStyle,
49
	_quality = 1, _precision = null;
50

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

B
brason 已提交
66
	};
67

68 69
	this.setQuality = function( quality ) {

G
gero3 已提交
70
		switch ( quality ) {
71 72 73 74 75

			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

	};

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

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
	}

125 126 127 128 129 130 131 132 133 134
	function getSvgColor ( color, opacity ) {

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

		return 'rgba(' + arg + ',' + opacity + ')';

	}

135 136
	function convert ( c ) {

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 174 175
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_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 );
183
		_currPath = _currStyle = ""; // reset accumulated path
184

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

187 188
			var element = _elements[ e ];
			var material = element.material;
189

190
			if ( material === undefined || material.opacity === 0 ) continue;
191

M
Mr.doob 已提交
192
			_elemBox.makeEmpty();
193

194
			if ( element instanceof THREE.RenderableSprite ) {
195

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

199
				renderSprite( _v1, element, material );
200

201
			} else if ( element instanceof THREE.RenderableLine ) {
202

203
				_v1 = element.v1; _v2 = element.v2;
204

205 206
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
207

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

210
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
211

212
					renderLine( _v1, _v2, element, material );
213

214
				}
215

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

218
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
219

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

224 225 226 227
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

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

234
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
235 236 237 238

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

				}
239

M
Mr.doob 已提交
240
			}
241 242 243

		}

244
		flushPath(); // just to flush last svg:path
245

246 247 248 249 250
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

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

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

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

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
265
	};
266

267
	function calculateLights( lights ) {
M
Mr.doob 已提交
268

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

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

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

M
Mr.doob 已提交
278 279 280 281 282 283 284
			if ( light instanceof THREE.AmbientLight ) {

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

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

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

			} else if ( light instanceof THREE.PointLight ) {

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

			}

		}

	}

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

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

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

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

317 318 319 320 321
				amount *= light.intensity;

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

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

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

			}

		}

	}

347
	function renderSprite( v1, element, material ) {
348

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

352 353 354 355 356
		if ( material.isPointsMaterial ) {
			scaleX *= material.size;
			scaleY *= material.size;
		}

357
		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';
358
		var style = "";
359

360
		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
361

362
			style = 'fill:' + getSvgColor( material.color, material.opacity );
363 364 365

		}

366
		addPath( style, path );
367 368

	}
M
Mr.doob 已提交
369

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

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

374
		if ( material instanceof THREE.LineBasicMaterial ) {
375

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

378
			addPath( style, path );
379

380
		}
381

382
	}
M
Mr.doob 已提交
383

384
	function renderFace3( v1, v2, v3, element, material ) {
385

M
Mr.doob 已提交
386 387
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
388

389
		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';
390
		var style = '';
391

M
Mr.doob 已提交
392 393
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
394
			_color.copy( material.color );
M
Mr.doob 已提交
395

396 397
			if ( material.vertexColors === THREE.FaceColors ) {

398
				_color.multiply( element.color );
399 400 401

			}

402
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
403

404
			_diffuseColor.copy( material.color );
405

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

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

			}

412
			_color.copy( _ambientLight );
413

414 415 416
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

418
			_color.multiply( _diffuseColor ).add( material.emissive );
419

420 421
		} else if ( material instanceof THREE.MeshNormalMaterial ) {

422
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
423

424
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
425

M
Mr.doob 已提交
426
		}
427

M
Mr.doob 已提交
428
		if ( material.wireframe ) {
429

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

M
Mr.doob 已提交
432
		} else {
433

434
			style = 'fill:' + getSvgColor( _color, material.opacity );
435 436 437

		}

438 439 440 441 442 443 444 445 446 447 448 449
		addPath( style, path );

	}

	function addPath ( style, path ) {

		if ( _currStyle == style ) {

			_currPath += path

		} else {

450
			flushPath();
451 452 453 454 455

			_currStyle = style;
			_currPath = path;

		}
456 457 458

	}

459 460 461 462 463 464 465 466 467 468 469 470 471 472
	function flushPath() {

		if ( _currPath ) {

			_svgNode = getPathNode( _pathCount ++ );
			_svgNode.setAttribute( 'd', _currPath );
			_svgNode.setAttribute( 'style', _currStyle );
			_svg.appendChild( _svgNode );

		}

		_currPath = _currStyle = "";
	}

473
	function getPathNode( id ) {
474

475
		if ( _svgPathPool[ id ] == null ) {
476 477 478

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

479
			if ( _quality == 0 ) {
480 481 482 483 484 485 486

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

			}

			return _svgPathPool[ id ];

487
		}
488 489 490

		return _svgPathPool[ id ];

M
Mr.doob 已提交
491
	}
492

M
Mr.doob 已提交
493
};