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 = [],
M
Mr.doob 已提交
48 49 50 51
	_svgNode, _pathCount = 0,

	_currentPath, _currentStyle,

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

71 72
	this.setQuality = function( quality ) {

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 84 85 86
	// WebGLRenderer compatibility

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

87 88
	this.setClearColor = function ( color, alpha ) {

89 90
		_clearColor.set( color );
		_clearAlpha = alpha !== undefined ? alpha : 1;
91 92 93

	};

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

96
	this.setSize = function( width, height ) {
97

M
Mr.doob 已提交
98 99
		_svgWidth = width; _svgHeight = height;
		_svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
100

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

M
Mr.doob 已提交
105 106
		_clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
		_clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
107

M
Mr.doob 已提交
108
	};
109

110 111 112 113 114 115
	this.setPrecision = function ( precision ) {

		_precision = precision;

	};

116
	function removeChildNodes() {
M
Mr.doob 已提交
117

118 119
		_pathCount = 0;

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

122
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
123 124

		}
125

126 127
	}

128 129 130 131 132 133 134 135 136 137
	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 + ')';

	}

138 139
	function convert ( c ) {

140
		return _precision !== null ? c.toFixed(_precision) : c;
141 142 143

	}

144 145 146
	this.clear = function () {

		removeChildNodes();
147
		_svg.style.backgroundColor = getSvgColor( _clearColor, _clearAlpha );
M
Mr.doob 已提交
148 149 150

	};

151
	this.render = function ( scene, camera ) {
152

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

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

		}

160 161 162 163 164
		var background = scene.background;

		if ( background && background.isColor ) {

			removeChildNodes();
165
			_svg.style.backgroundColor = getSvgColor( background );
166 167 168 169 170 171

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

			this.clear();

		}
172

M
Mr.doob 已提交
173 174
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
175

176 177 178
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

179
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
180 181
		_elements = _renderData.elements;
		_lights = _renderData.lights;
182

183 184
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

185
		calculateLights( _lights );
M
Mr.doob 已提交
186 187 188 189 190

		 // reset accumulated path

		_currentPath = '';
		_currentStyle = '';
191

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

194 195
			var element = _elements[ e ];
			var material = element.material;
196

197
			if ( material === undefined || material.opacity === 0 ) continue;
198

M
Mr.doob 已提交
199
			_elemBox.makeEmpty();
200

201
			if ( element instanceof THREE.RenderableSprite ) {
202

203
				_v1 = element;
M
Mr.doob 已提交
204
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
205

206
				renderSprite( _v1, element, material );
207

208
			} else if ( element instanceof THREE.RenderableLine ) {
209

210
				_v1 = element.v1; _v2 = element.v2;
211

212 213
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
214

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

217
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
218

219
					renderLine( _v1, _v2, element, material );
220

221
				}
222

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

225
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
226

G
gero3 已提交
227 228 229
				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;
230

231 232 233 234
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

235 236 237 238 239
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
240

241
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
242 243 244 245

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

				}
246

M
Mr.doob 已提交
247
			}
248 249 250

		}

251
		flushPath(); // just to flush last svg:path
252

253 254 255 256 257
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

				_vector3.setFromMatrixPosition( object.matrixWorld );
F
Franklin Ta 已提交
258
				_vector3.applyMatrix4( _viewProjectionMatrix );
259 260 261 262

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

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

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
272
	};
273

274
	function calculateLights( lights ) {
M
Mr.doob 已提交
275

M
Mr.doob 已提交
276
		_ambientLight.setRGB( 0, 0, 0 );
277 278
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
279

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

282 283
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
284

M
Mr.doob 已提交
285 286 287 288 289 290 291
			if ( light instanceof THREE.AmbientLight ) {

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

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

293 294 295
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
296 297 298

			} else if ( light instanceof THREE.PointLight ) {

299 300 301
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
302 303 304 305 306 307 308

			}

		}

	}

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

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

313 314
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
315 316 317

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

324 325 326 327 328
				amount *= light.intensity;

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

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

340 341 342 343 344 345 346
				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 已提交
347 348 349 350 351 352 353

			}

		}

	}

354
	function renderSprite( v1, element, material ) {
355

M
Mr.doob 已提交
356 357
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
358

359 360 361 362 363
		if ( material.isPointsMaterial ) {
			scaleX *= material.size;
			scaleY *= material.size;
		}

364
		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';
365
		var style = "";
366

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

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

		}

373
		addPath( style, path );
374 375

	}
M
Mr.doob 已提交
376

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

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

381
		if ( material instanceof THREE.LineBasicMaterial ) {
382

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

385
			addPath( style, path );
386

387
		}
388

389
	}
M
Mr.doob 已提交
390

391
	function renderFace3( v1, v2, v3, element, material ) {
392

M
Mr.doob 已提交
393 394
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
395

396
		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';
397
		var style = '';
398

M
Mr.doob 已提交
399 400
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
401
			_color.copy( material.color );
M
Mr.doob 已提交
402

403 404
			if ( material.vertexColors === THREE.FaceColors ) {

405
				_color.multiply( element.color );
406 407 408

			}

409
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
410

411
			_diffuseColor.copy( material.color );
412

413 414
			if ( material.vertexColors === THREE.FaceColors ) {

415
				_diffuseColor.multiply( element.color );
416 417 418

			}

419
			_color.copy( _ambientLight );
420

421 422 423
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

425
			_color.multiply( _diffuseColor ).add( material.emissive );
426

427 428
		} else if ( material instanceof THREE.MeshNormalMaterial ) {

429
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
430

431
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
432

M
Mr.doob 已提交
433
		}
434

M
Mr.doob 已提交
435
		if ( material.wireframe ) {
436

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

M
Mr.doob 已提交
439
		} else {
440

441
			style = 'fill:' + getSvgColor( _color, material.opacity );
442 443 444

		}

445 446 447 448 449 450
		addPath( style, path );

	}

	function addPath ( style, path ) {

M
Mr.doob 已提交
451
		if ( _currentStyle === style ) {
452

M
Mr.doob 已提交
453
			_currentPath += path
454 455 456

		} else {

457
			flushPath();
458

M
Mr.doob 已提交
459 460
			_currentStyle = style;
			_currentPath = path;
461 462

		}
463 464 465

	}

466 467
	function flushPath() {

M
Mr.doob 已提交
468
		if ( _currentPath ) {
469 470

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

		}

M
Mr.doob 已提交
477 478 479
		_currentPath = '';
		_currentStyle = '';

480 481
	}

482
	function getPathNode( id ) {
483

484
		if ( _svgPathPool[ id ] == null ) {
485 486 487

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

488
			if ( _quality == 0 ) {
489 490 491 492 493 494 495

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

			}

			return _svgPathPool[ id ];

496
		}
497 498 499

		return _svgPathPool[ id ];

M
Mr.doob 已提交
500
	}
501

M
Mr.doob 已提交
502
};