SVGRenderer.js 11.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,
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 50
	_quality = 1;

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
	function removeChildNodes() {
M
Mr.doob 已提交
108

109 110
		_pathCount = 0;
		_lineCount = 0;
M
Mr.doob 已提交
111
		_rectCount = 0;
112

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

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

		}
118

119 120 121 122 123 124
	}

	this.clear = function () {

		removeChildNodes();
		_svg.style.backgroundColor = 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearAlpha + ')';
M
Mr.doob 已提交
125 126 127

	};

128
	this.render = function ( scene, camera ) {
129

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

132
			console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
133 134 135 136
			return;

		}

137 138 139 140 141 142 143 144 145 146 147 148
		var background = scene.background;

		if ( background && background.isColor ) {

			removeChildNodes();
			_svg.style.backgroundColor = 'rgb(' + Math.floor( background.r * 255 ) + ',' + Math.floor( background.g * 255 ) + ',' + Math.floor( background.b * 255 ) + ')';

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

			this.clear();

		}
149

M
Mr.doob 已提交
150 151
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
152

153 154 155
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

156
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
157 158
		_elements = _renderData.elements;
		_lights = _renderData.lights;
159

160 161
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

162
		calculateLights( _lights );
163
		_currPath = _currStyle = ""; // reset accumulated path
164

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

167 168
			var element = _elements[ e ];
			var material = element.material;
169

170
			if ( material === undefined || material.opacity === 0 ) continue;
171

M
Mr.doob 已提交
172
			_elemBox.makeEmpty();
173

174
			if ( element instanceof THREE.RenderableSprite ) {
175

176
				_v1 = element;
M
Mr.doob 已提交
177
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
178

179
				renderSprite( _v1, element, material );
180

181
			} else if ( element instanceof THREE.RenderableLine ) {
182

183
				_v1 = element.v1; _v2 = element.v2;
184

185 186
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
187

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

190
				if ( _clipBox.intersectsBox( _elemBox ) === true ) {
191

192
					renderLine( _v1, _v2, element, material );
193

194
				}
195

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

198
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
199

G
gero3 已提交
200 201 202
				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;
203

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

208 209 210 211 212
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
213

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

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

				}
219

M
Mr.doob 已提交
220
			}
221 222 223

		}

224 225
		addPath ( "!dummy!", ""); // just to flush last svg:path

226 227 228 229 230
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

				_vector3.setFromMatrixPosition( object.matrixWorld );
F
Franklin Ta 已提交
231
				_vector3.applyMatrix4( _viewProjectionMatrix );
232 233 234 235

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

G
gero3 已提交
236
				var node = object.node;
237 238 239 240 241 242 243 244
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
245
	};
246

247
	function calculateLights( lights ) {
M
Mr.doob 已提交
248

M
Mr.doob 已提交
249
		_ambientLight.setRGB( 0, 0, 0 );
250 251
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
252

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

255 256
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
257

M
Mr.doob 已提交
258 259 260 261 262 263 264
			if ( light instanceof THREE.AmbientLight ) {

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

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

266 267 268
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
269 270 271

			} else if ( light instanceof THREE.PointLight ) {

272 273 274
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
275 276 277 278 279 280 281

			}

		}

	}

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

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

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

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

297 298 299 300 301
				amount *= light.intensity;

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

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

313 314 315 316 317 318 319
				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 已提交
320 321 322 323 324 325 326

			}

		}

	}

327
	function renderSprite( v1, element, material ) {
328

M
Mr.doob 已提交
329 330
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
331

332 333 334 335 336
		if ( material.isPointsMaterial ) {
			scaleX *= material.size;
			scaleY *= material.size;
		}

337
		var path = 'M' + ( v1.x - scaleX * 0.5 ) + ',' + ( v1.y - scaleY * 0.5 ) + 'h' + scaleX + 'v' + scaleY + 'h' + (-scaleX) + 'z';
338
		var style = "";
339

340
		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
341

342
			style = 'fill:' + material.color.getStyle();
343 344 345

		}

346
		addPath( style, path );
347 348

	}
M
Mr.doob 已提交
349

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

352
		var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y;
353

354
		if ( material instanceof THREE.LineBasicMaterial ) {
355

356
			var style = 'fill:none;stroke:' + material.color.getStyle() + ';stroke-width:' + material.linewidth + ';stroke-opacity:' + material.opacity + ';stroke-linecap:' + material.linecap + ';stroke-linejoin:' + material.linejoin;
357

358
			addPath( style, path );
359

360
		}
361

362
	}
M
Mr.doob 已提交
363

364
	function renderFace3( v1, v2, v3, element, material ) {
365

M
Mr.doob 已提交
366 367
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
368

369
		var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y + 'L' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z';
370
		var style = '';
371

M
Mr.doob 已提交
372 373
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
374
			_color.copy( material.color );
M
Mr.doob 已提交
375

376 377
			if ( material.vertexColors === THREE.FaceColors ) {

378
				_color.multiply( element.color );
379 380 381

			}

382
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
383

384
			_diffuseColor.copy( material.color );
385

386 387
			if ( material.vertexColors === THREE.FaceColors ) {

388
				_diffuseColor.multiply( element.color );
389 390 391

			}

392
			_color.copy( _ambientLight );
393

394 395 396
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

398
			_color.multiply( _diffuseColor ).add( material.emissive );
399

400 401
		} else if ( material instanceof THREE.MeshNormalMaterial ) {

402
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
403

404
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
405

M
Mr.doob 已提交
406
		}
407

M
Mr.doob 已提交
408
		if ( material.wireframe ) {
409

410
			style = 'fill:none;stroke:' + _color.getStyle() + ';stroke-width:' + material.wireframeLinewidth + ';stroke-opacity:' + material.opacity + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
411

M
Mr.doob 已提交
412
		} else {
413

414
			style = 'fill:' + _color.getStyle() + ';fill-opacity:' + material.opacity;
415 416 417

		}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
		addPath( style, path );

	}

	function addPath ( style, path ) {

		if ( _currStyle == style ) {

			_currPath += path

		} else {

			if ( _currPath ) {

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

			}

			_currStyle = style;
			_currPath = path;

		}
443 444 445

	}

446
	function getPathNode( id ) {
447

448
		if ( _svgPathPool[ id ] == null ) {
449 450 451

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

452
			if ( _quality == 0 ) {
453 454 455 456 457 458 459

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

			}

			return _svgPathPool[ id ];

460
		}
461 462 463

		return _svgPathPool[ id ];

M
Mr.doob 已提交
464
	}
465

M
Mr.doob 已提交
466
};