SVGRenderer.js 11.4 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 14
THREE.SVGObject = function ( node ) {

	THREE.Object3D.call( this );

	this.node = node;

};

THREE.SVGObject.prototype = Object.create( THREE.Object3D.prototype );

M
Mr.doob 已提交
15
THREE.SVGRenderer = function () {
16

M
Mr.doob 已提交
17
	console.log( 'THREE.SVGRenderer', THREE.REVISION );
18

19
	var _this = this,
20
	_renderData, _elements, _lights,
M
Mr.doob 已提交
21 22
	_projector = new THREE.Projector(),
	_svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
M
Mr.doob 已提交
23
	_svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
24 25 26

	_v1, _v2, _v3, _v4,

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

30
	_color = new THREE.Color(),
31
	_diffuseColor = new THREE.Color(),
32 33 34
	_ambientLight = new THREE.Color(),
	_directionalLights = new THREE.Color(),
	_pointLights = new THREE.Color(),
S
srifqi 已提交
35
	_clearColor = new THREE.Color(),
36
	_clearAlpha = 1,
M
Mr.doob 已提交
37

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

42 43 44
	_viewMatrix = new THREE.Matrix4(),
	_viewProjectionMatrix = new THREE.Matrix4(),

M
Mr.doob 已提交
45 46
	_svgPathPool = [], _svgLinePool = [], _svgRectPool = [],
	_svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
47 48
	_quality = 1;

49
	this.domElement = _svg;
M
Mr.doob 已提交
50

M
Mr.doob 已提交
51
	this.autoClear = true;
M
Mr.doob 已提交
52 53
	this.sortObjects = true;
	this.sortElements = true;
M
Mr.doob 已提交
54

M
Mr.doob 已提交
55
	this.info = {
56

M
Mr.doob 已提交
57 58 59 60 61 62
		render: {

			vertices: 0,
			faces: 0

		}
63 64 65

	}

66 67 68 69 70 71 72 73
	this.setQuality = function( quality ) {

		switch(quality) {

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

		}
M
Mr.doob 已提交
74 75

	};
76

77 78 79 80 81
	// WebGLRenderer compatibility

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

82 83
	this.setClearColor = function ( color, alpha ) {

84 85
		_clearColor.set( color );
		_clearAlpha = alpha !== undefined ? alpha : 1;
86 87 88

	};

89
	this.setSize = function( width, height ) {
90

M
Mr.doob 已提交
91 92
		_svgWidth = width; _svgHeight = height;
		_svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
93

M
Mr.doob 已提交
94 95 96
		_svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
		_svg.setAttribute( 'width', _svgWidth );
		_svg.setAttribute( 'height', _svgHeight );
97

M
Mr.doob 已提交
98 99
		_clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
		_clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
100

M
Mr.doob 已提交
101
	};
102

M
Mr.doob 已提交
103 104
	this.clear = function () {

105 106
		_pathCount = 0;
		_lineCount = 0;
M
Mr.doob 已提交
107
		_rectCount = 0;
108

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

111
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
112 113

		}
S
srifqi 已提交
114
		
115
		_svg.style.backgroundColor = 'rgba(' + ( ( _clearColor.r * 255 ) | 0 ) + ',' + ( ( _clearColor.g * 255 ) | 0 ) + ',' + ( ( _clearColor.b * 255 ) | 0 ) + ',' + _clearAlpha + ')';
M
Mr.doob 已提交
116 117 118

	};

119
	this.render = function ( scene, camera ) {
120

M
Mr.doob 已提交
121 122 123 124 125 126 127
		if ( camera instanceof THREE.Camera === false ) {

			console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
			return;

		}

128
		if ( this.autoClear === true ) this.clear();
129

M
Mr.doob 已提交
130 131
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
132

133 134 135
		_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
		_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );

136
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
137 138
		_elements = _renderData.elements;
		_lights = _renderData.lights;
139

140
		calculateLights( _lights );
141

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

144 145
			var element = _elements[ e ];
			var material = element.material;
146

147
			if ( material === undefined || material.opacity === 0 ) continue;
148

M
Mr.doob 已提交
149
			_elemBox.makeEmpty();
150

151
			if ( element instanceof THREE.RenderableSprite ) {
152

153
				_v1 = element;
M
Mr.doob 已提交
154
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
155

156
				renderSprite( _v1, element, material );
157

158
			} else if ( element instanceof THREE.RenderableLine ) {
159

160
				_v1 = element.v1; _v2 = element.v2;
161

162 163
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
164

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

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

169
					renderLine( _v1, _v2, element, material );
170

171
				}
172

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

175
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
176

177 178 179 180
				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;

181 182 183 184
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

185 186 187 188 189
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
190

191 192 193 194 195
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

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

				}
196

M
Mr.doob 已提交
197
			}
198 199 200

		}

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		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;

			 	var node = object.node;
				node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );

				_svg.appendChild( node );

			}

		} );

M
Mr.doob 已提交
220
	};
221

222
	function calculateLights( lights ) {
M
Mr.doob 已提交
223

M
Mr.doob 已提交
224
		_ambientLight.setRGB( 0, 0, 0 );
225 226
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
227

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

230 231
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
232

M
Mr.doob 已提交
233 234 235 236 237 238 239
			if ( light instanceof THREE.AmbientLight ) {

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

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

241 242 243
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
244 245 246

			} else if ( light instanceof THREE.PointLight ) {

247 248 249
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
250 251 252 253 254 255 256

			}

		}

	}

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

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

261 262
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
263 264 265

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

272 273 274 275 276
				amount *= light.intensity;

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

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

288 289 290 291 292 293 294
				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 已提交
295 296 297 298 299 300 301

			}

		}

	}

302
	function renderSprite( v1, element, material ) {
303

M
Mr.doob 已提交
304 305
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
306

M
Mr.doob 已提交
307
		_svgNode = getRectNode( _rectCount ++ );
308

M
Mr.doob 已提交
309 310 311 312
		_svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
		_svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
		_svgNode.setAttribute( 'width', scaleX );
		_svgNode.setAttribute( 'height', scaleY );
313

314
		if ( material instanceof THREE.SpriteMaterial ) {
315

316
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
317 318 319 320 321 322

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
323

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

326
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
327

328 329 330 331 332
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

333
		if ( material instanceof THREE.LineBasicMaterial ) {
334

335
			_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 );
336

337
			_svg.appendChild( _svgNode );
338

339
		}
340

341
	}
M
Mr.doob 已提交
342

343
	function renderFace3( v1, v2, v3, element, material ) {
344

M
Mr.doob 已提交
345 346
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
347

348
		_svgNode = getPathNode( _pathCount ++ );
349
		_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' );
350

M
Mr.doob 已提交
351 352
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
353
			_color.copy( material.color );
M
Mr.doob 已提交
354

355 356
			if ( material.vertexColors === THREE.FaceColors ) {

357
				_color.multiply( element.color );
358 359 360

			}

361
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
362

363
			_diffuseColor.copy( material.color );
364

365 366
			if ( material.vertexColors === THREE.FaceColors ) {

367
				_diffuseColor.multiply( element.color );
368 369 370

			}

371
			_color.copy( _ambientLight );
372

373 374 375
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

377
			_color.multiply( _diffuseColor ).add( material.emissive );
378

379 380 381
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
382
			_color.setRGB( _w, _w, _w );
383 384 385

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

386 387 388
			var normal = element.normalModelView;

			_color.setRGB( normal.x, normal.y, normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
389

M
Mr.doob 已提交
390
		}
391

M
Mr.doob 已提交
392
		if ( material.wireframe ) {
393

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

M
Mr.doob 已提交
396
		} else {
397

398
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
399 400 401 402 403 404 405

		}

		_svg.appendChild( _svgNode );

	}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	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 ];

	}
425

426
	function getPathNode( id ) {
427

428
		if ( _svgPathPool[ id ] == null ) {
429 430 431

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

432
			if ( _quality == 0 ) {
433 434 435 436 437 438 439

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

			}

			return _svgPathPool[ id ];

440
		}
441 442 443

		return _svgPathPool[ id ];

M
Mr.doob 已提交
444
	}
445

446
	function getRectNode( id ) {
447

448
		if ( _svgRectPool[ id ] == null ) {
449

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

452
			if ( _quality == 0 ) {
453

M
Mr.doob 已提交
454
				_svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
455 456 457

			}

458
			return _svgRectPool[ id ];
459

460
		}
461

462
		return _svgRectPool[ id ];
463 464 465

	}

M
Mr.doob 已提交
466
};