SVGRenderer.js 10.1 KB
Newer Older
M
Mr.doob 已提交
1
/**
2
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
3 4
 */

M
Mr.doob 已提交
5
THREE.SVGRenderer = function () {
6

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

9
	var _this = this,
10
	_renderData, _elements, _lights,
M
Mr.doob 已提交
11 12
	_projector = new THREE.Projector(),
	_svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'),
M
Mr.doob 已提交
13
	_svgWidth, _svgHeight, _svgWidthHalf, _svgHeightHalf,
14 15 16

	_v1, _v2, _v3, _v4,

M
Mr.doob 已提交
17 18
	_clipBox = new THREE.Box2(),
	_elemBox = new THREE.Box2(),
M
Mr.doob 已提交
19

20
	_color = new THREE.Color(),
21
	_diffuseColor = new THREE.Color(),
22 23 24
	_ambientLight = new THREE.Color(),
	_directionalLights = new THREE.Color(),
	_pointLights = new THREE.Color(),
M
Mr.doob 已提交
25

M
Mr.doob 已提交
26
	_w, // z-buffer to w-buffer
M
Mr.doob 已提交
27 28
	_vector3 = new THREE.Vector3(), // Needed for PointLight

M
Mr.doob 已提交
29 30
	_svgPathPool = [], _svgLinePool = [], _svgRectPool = [],
	_svgNode, _pathCount = 0, _lineCount = 0, _rectCount = 0,
31 32
	_quality = 1;

33
	this.domElement = _svg;
M
Mr.doob 已提交
34

M
Mr.doob 已提交
35
	this.autoClear = true;
M
Mr.doob 已提交
36 37
	this.sortObjects = true;
	this.sortElements = true;
M
Mr.doob 已提交
38

M
Mr.doob 已提交
39
	this.info = {
40

M
Mr.doob 已提交
41 42 43 44 45 46
		render: {

			vertices: 0,
			faces: 0

		}
47 48 49

	}

50 51 52 53 54 55 56 57
	this.setQuality = function( quality ) {

		switch(quality) {

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

		}
M
Mr.doob 已提交
58 59

	};
60

61 62 63 64 65
	// WebGLRenderer compatibility

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

66 67 68 69 70 71
	this.setClearColor = function ( color, alpha ) {

		// TODO

	};

72
	this.setSize = function( width, height ) {
73

M
Mr.doob 已提交
74 75
		_svgWidth = width; _svgHeight = height;
		_svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
76

M
Mr.doob 已提交
77 78 79
		_svg.setAttribute( 'viewBox', ( - _svgWidthHalf ) + ' ' + ( - _svgHeightHalf ) + ' ' + _svgWidth + ' ' + _svgHeight );
		_svg.setAttribute( 'width', _svgWidth );
		_svg.setAttribute( 'height', _svgHeight );
80

M
Mr.doob 已提交
81 82
		_clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
		_clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
83

M
Mr.doob 已提交
84
	};
85

M
Mr.doob 已提交
86 87
	this.clear = function () {

88 89
		_pathCount = 0;
		_lineCount = 0;
M
Mr.doob 已提交
90
		_rectCount = 0;
91

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

94
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
95 96 97 98 99

		}

	};

100
	this.render = function ( scene, camera ) {
101

M
Mr.doob 已提交
102 103 104 105 106 107 108
		if ( camera instanceof THREE.Camera === false ) {

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

		}

109
		if ( this.autoClear === true ) this.clear();
110

M
Mr.doob 已提交
111 112
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
113

114
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
115 116
		_elements = _renderData.elements;
		_lights = _renderData.lights;
117

118
		calculateLights( _lights );
119

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

122 123
			var element = _elements[ e ];
			var material = element.material;
124

125
			if ( material === undefined || material.visible === false ) continue;
126

M
Mr.doob 已提交
127
			_elemBox.makeEmpty();
128

129
			if ( element instanceof THREE.RenderableSprite ) {
130

131
				_v1 = element;
M
Mr.doob 已提交
132
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
133

134
				renderSprite( _v1, element, material );
135

136
			} else if ( element instanceof THREE.RenderableLine ) {
137

138
				_v1 = element.v1; _v2 = element.v2;
139

140 141
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
142

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

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

147
					renderLine( _v1, _v2, element, material );
148

149
				}
150

151
			} else if ( element instanceof THREE.RenderableFace3 ) {
152

153
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
154

155 156 157 158
				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;

159 160 161 162
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

163 164 165 166 167
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
168

169 170 171 172 173
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

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

				}
174

M
Mr.doob 已提交
175
			}
176 177 178

		}

M
Mr.doob 已提交
179
	};
180

181
	function calculateLights( lights ) {
M
Mr.doob 已提交
182

M
Mr.doob 已提交
183
		_ambientLight.setRGB( 0, 0, 0 );
184 185
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
186

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

189 190
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
191

M
Mr.doob 已提交
192 193 194 195 196 197 198
			if ( light instanceof THREE.AmbientLight ) {

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

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

200 201 202
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
203 204 205

			} else if ( light instanceof THREE.PointLight ) {

206 207 208
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
209 210 211 212 213 214 215

			}

		}

	}

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

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

220 221
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
222 223 224

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

231 232 233 234 235
				amount *= light.intensity;

				color.r += lightColor.r * amount;
				color.g += lightColor.g * amount;
				color.b += lightColor.b * amount;
M
Mr.doob 已提交
236 237 238

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

247 248 249 250 251 252 253
				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 已提交
254 255 256 257 258 259 260

			}

		}

	}

261
	function renderSprite( v1, element, material ) {
262

M
Mr.doob 已提交
263 264
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
265

M
Mr.doob 已提交
266
		_svgNode = getRectNode( _rectCount ++ );
267

M
Mr.doob 已提交
268 269 270 271
		_svgNode.setAttribute( 'x', v1.x - ( scaleX * 0.5 ) );
		_svgNode.setAttribute( 'y', v1.y - ( scaleY * 0.5 ) );
		_svgNode.setAttribute( 'width', scaleX );
		_svgNode.setAttribute( 'height', scaleY );
272

273
		if ( material instanceof THREE.SpriteMaterial ) {
274

275
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
276 277 278 279 280 281

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
282

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

285
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
286

287 288 289 290 291
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

292
		if ( material instanceof THREE.LineBasicMaterial ) {
293

294
			_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 );
295

296
			_svg.appendChild( _svgNode );
297

298
		}
299

300
	}
M
Mr.doob 已提交
301

302
	function renderFace3( v1, v2, v3, element, material ) {
303

M
Mr.doob 已提交
304 305
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
306

307
		_svgNode = getPathNode( _pathCount ++ );
308
		_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' );
309

M
Mr.doob 已提交
310 311
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
312
			_color.copy( material.color );
M
Mr.doob 已提交
313

314 315
			if ( material.vertexColors === THREE.FaceColors ) {

316
				_color.multiply( element.color );
317 318 319

			}

320
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
321

322
			_diffuseColor.copy( material.color );
323

324 325
			if ( material.vertexColors === THREE.FaceColors ) {

326
				_diffuseColor.multiply( element.color );
327 328 329

			}

330
			_color.copy( _ambientLight );
331

332
			calculateLight( _lights, element.centroidModel, element.normalModel, _color );
333

334
			_color.multiply( _diffuseColor ).add( material.emissive );
335

336 337 338
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
339
			_color.setRGB( _w, _w, _w );
340 341 342

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

343 344 345
			var normal = element.normalModelView;

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

M
Mr.doob 已提交
347
		}
348

M
Mr.doob 已提交
349
		if ( material.wireframe ) {
350

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

M
Mr.doob 已提交
353
		} else {
354

355
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
356 357 358 359 360 361 362

		}

		_svg.appendChild( _svgNode );

	}

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	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 ];

	}
382

383
	function getPathNode( id ) {
384

385
		if ( _svgPathPool[ id ] == null ) {
386 387 388

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

389
			if ( _quality == 0 ) {
390 391 392 393 394 395 396

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

			}

			return _svgPathPool[ id ];

397
		}
398 399 400

		return _svgPathPool[ id ];

M
Mr.doob 已提交
401
	}
402

403
	function getRectNode( id ) {
404

405
		if ( _svgRectPool[ id ] == null ) {
406

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

409
			if ( _quality == 0 ) {
410

M
Mr.doob 已提交
411
				_svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
412 413 414

			}

415
			return _svgRectPool[ id ];
416

417
		}
418

419
		return _svgRectPool[ id ];
420 421 422

	}

M
Mr.doob 已提交
423
};