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(),
M
Mr.doob 已提交
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

M
Mr.doob 已提交
39
	_w, // z-buffer to w-buffer
M
Mr.doob 已提交
40
	_vector3 = new THREE.Vector3(), // Needed for PointLight
41
	_centroid = new THREE.Vector3(),
42 43
	_normal = new THREE.Vector3(),
	_normalViewMatrix = new THREE.Matrix3(),
M
Mr.doob 已提交
44

45 46 47
	_viewMatrix = new THREE.Matrix4(),
	_viewProjectionMatrix = new THREE.Matrix4(),

M
Mr.doob 已提交
48 49
	_svgPathPool = [], _svgLinePool = [], _svgRectPool = [],
	_svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
50 51
	_quality = 1;

52
	this.domElement = _svg;
M
Mr.doob 已提交
53

M
Mr.doob 已提交
54
	this.autoClear = true;
M
Mr.doob 已提交
55 56
	this.sortObjects = true;
	this.sortElements = true;
M
Mr.doob 已提交
57

M
Mr.doob 已提交
58
	this.info = {
59

M
Mr.doob 已提交
60 61 62 63 64 65
		render: {

			vertices: 0,
			faces: 0

		}
66

B
brason 已提交
67
	};
68

69 70
	this.setQuality = function( quality ) {

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

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

		}
M
Mr.doob 已提交
77 78

	};
79

80 81 82 83 84
	// WebGLRenderer compatibility

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

85 86
	this.setClearColor = function ( color, alpha ) {

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

	};

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

94
	this.setSize = function( width, height ) {
95

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

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

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

M
Mr.doob 已提交
106
	};
107

M
Mr.doob 已提交
108 109
	this.clear = function () {

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

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

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

		}
119

120
		_svg.style.backgroundColor = 'rgba(' + ( ( _clearColor.r * 255 ) | 0 ) + ',' + ( ( _clearColor.g * 255 ) | 0 ) + ',' + ( ( _clearColor.b * 255 ) | 0 ) + ',' + _clearAlpha + ')';
M
Mr.doob 已提交
121 122 123

	};

124
	this.render = function ( scene, camera ) {
125

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

128
			console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
M
Mr.doob 已提交
129 130 131 132
			return;

		}

133
		if ( this.autoClear === true ) this.clear();
134

M
Mr.doob 已提交
135 136
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
137

138 139 140
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

141
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
142 143
		_elements = _renderData.elements;
		_lights = _renderData.lights;
144

145 146
		_normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );

147
		calculateLights( _lights );
148

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

151 152
			var element = _elements[ e ];
			var material = element.material;
153

154
			if ( material === undefined || material.opacity === 0 ) continue;
155

M
Mr.doob 已提交
156
			_elemBox.makeEmpty();
157

158
			if ( element instanceof THREE.RenderableSprite ) {
159

160
				_v1 = element;
M
Mr.doob 已提交
161
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
162

163
				renderSprite( _v1, element, material );
164

165
			} else if ( element instanceof THREE.RenderableLine ) {
166

167
				_v1 = element.v1; _v2 = element.v2;
168

169 170
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
171

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

M
Mr.doob 已提交
174
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {
175

176
					renderLine( _v1, _v2, element, material );
177

178
				}
179

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

182
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
183

184 185 186 187
				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;

188 189 190 191
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

192 193 194 195 196
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
197

198 199 200 201 202
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

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

				}
203

M
Mr.doob 已提交
204
			}
205 206 207

		}

208 209 210 211 212 213 214 215 216 217
		scene.traverseVisible( function ( object ) {

			 if ( object instanceof THREE.SVGObject ) {

				_vector3.setFromMatrixPosition( object.matrixWorld );
				_vector3.applyProjection( _viewProjectionMatrix );

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

G
gero3 已提交
218
				var node = object.node;
219 220 221 222 223 224 225 226
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
227
	};
228

229
	function calculateLights( lights ) {
M
Mr.doob 已提交
230

M
Mr.doob 已提交
231
		_ambientLight.setRGB( 0, 0, 0 );
232 233
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
234

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

237 238
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
239

M
Mr.doob 已提交
240 241 242 243 244 245 246
			if ( light instanceof THREE.AmbientLight ) {

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

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

248 249 250
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
251 252 253

			} else if ( light instanceof THREE.PointLight ) {

254 255 256
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
257 258 259 260 261 262 263

			}

		}

	}

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

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

268 269
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
270 271 272

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

279 280 281 282 283
				amount *= light.intensity;

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

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

295 296 297 298 299 300 301
				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 已提交
302 303 304 305 306 307 308

			}

		}

	}

309
	function renderSprite( v1, element, material ) {
310

M
Mr.doob 已提交
311 312
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
313

M
Mr.doob 已提交
314
		_svgNode = getRectNode( _rectCount ++ );
315

M
Mr.doob 已提交
316 317 318 319
		_svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
		_svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
		_svgNode.setAttribute( 'width', scaleX );
		_svgNode.setAttribute( 'height', scaleY );
320

321
		if ( material instanceof THREE.SpriteMaterial ) {
322

323
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
324 325 326 327 328 329

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
330

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

333
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
334

335 336 337 338 339
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

340
		if ( material instanceof THREE.LineBasicMaterial ) {
341

342
			_svgNode.setAttribute( 'style', 'fill: none; stroke: ' + material.color.getStyle() + '; stroke-width: ' + material.linewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.linecap + '; stroke-linejoin: ' + material.linejoin );
343

344
			_svg.appendChild( _svgNode );
345

346
		}
347

348
	}
M
Mr.doob 已提交
349

350
	function renderFace3( v1, v2, v3, element, material ) {
351

M
Mr.doob 已提交
352 353
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
354

355
		_svgNode = getPathNode( _pathCount ++ );
356
		_svgNode.setAttribute( 'd', 'M ' + v1.positionScreen.x + ' ' + v1.positionScreen.y + ' L ' + v2.positionScreen.x + ' ' + v2.positionScreen.y + ' L ' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z' );
357

M
Mr.doob 已提交
358 359
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
360
			_color.copy( material.color );
M
Mr.doob 已提交
361

362 363
			if ( material.vertexColors === THREE.FaceColors ) {

364
				_color.multiply( element.color );
365 366 367

			}

368
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
369

370
			_diffuseColor.copy( material.color );
371

372 373
			if ( material.vertexColors === THREE.FaceColors ) {

374
				_diffuseColor.multiply( element.color );
375 376 377

			}

378
			_color.copy( _ambientLight );
379

380 381 382
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

384
			_color.multiply( _diffuseColor ).add( material.emissive );
385

386 387 388
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
389
			_color.setRGB( _w, _w, _w );
390 391 392

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

393
			_normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix );
394

395
			_color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
396

M
Mr.doob 已提交
397
		}
398

M
Mr.doob 已提交
399
		if ( material.wireframe ) {
400

401
			_svgNode.setAttribute( 'style', 'fill: none; stroke: ' + _color.getStyle() + '; stroke-width: ' + material.wireframeLinewidth + '; stroke-opacity: ' + material.opacity + '; stroke-linecap: ' + material.wireframeLinecap + '; stroke-linejoin: ' + material.wireframeLinejoin );
402

M
Mr.doob 已提交
403
		} else {
404

405
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
406 407 408 409 410 411 412

		}

		_svg.appendChild( _svgNode );

	}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	function getLineNode( id ) {

		if ( _svgLinePool[ id ] == null ) {

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

			if ( _quality == 0 ) {

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

			}

			return _svgLinePool[ id ];

		}

		return _svgLinePool[ id ];

	}
432

433
	function getPathNode( id ) {
434

435
		if ( _svgPathPool[ id ] == null ) {
436 437 438

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

439
			if ( _quality == 0 ) {
440 441 442 443 444 445 446

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

			}

			return _svgPathPool[ id ];

447
		}
448 449 450

		return _svgPathPool[ id ];

M
Mr.doob 已提交
451
	}
452

453
	function getRectNode( id ) {
454

455
		if ( _svgRectPool[ id ] == null ) {
456

457
			_svgRectPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'rect' );
458

459
			if ( _quality == 0 ) {
460

M
Mr.doob 已提交
461
				_svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
462 463 464

			}

465
			return _svgRectPool[ id ];
466

467
		}
468

469
		return _svgRectPool[ id ];
470 471 472

	}

M
Mr.doob 已提交
473
};