SVGRenderer.js 10.2 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(),
S
srifqi 已提交
25
	_clearColor = new THREE.Color(),
M
Mr.doob 已提交
26

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

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

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

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

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

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

			vertices: 0,
			faces: 0

		}
48 49 50

	}

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

		switch(quality) {

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

		}
M
Mr.doob 已提交
59 60

	};
61

62 63 64 65 66
	// WebGLRenderer compatibility

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

67 68
	this.setClearColor = function ( color, alpha ) {

S
srifqi 已提交
69
		_clearColor.set(color);
70 71 72

	};

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

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

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

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

M
Mr.doob 已提交
85
	};
86

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

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

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

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

		}
S
srifqi 已提交
98 99
		
		_svg.style.backgroundColor = "#" + _clearColor.getHexString();
M
Mr.doob 已提交
100 101 102

	};

103
	this.render = function ( scene, camera ) {
104

M
Mr.doob 已提交
105 106 107 108 109 110 111
		if ( camera instanceof THREE.Camera === false ) {

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

		}

112
		if ( this.autoClear === true ) this.clear();
113

M
Mr.doob 已提交
114 115
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
116

117
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
118 119
		_elements = _renderData.elements;
		_lights = _renderData.lights;
120

121
		calculateLights( _lights );
122

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

125 126
			var element = _elements[ e ];
			var material = element.material;
127

S
srifqi 已提交
128
			if ( material === undefined || material.visible === false ) continue;
129

M
Mr.doob 已提交
130
			_elemBox.makeEmpty();
131

132
			if ( element instanceof THREE.RenderableSprite ) {
133

134
				_v1 = element;
M
Mr.doob 已提交
135
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
136

137
				renderSprite( _v1, element, material );
138

139
			} else if ( element instanceof THREE.RenderableLine ) {
140

141
				_v1 = element.v1; _v2 = element.v2;
142

143 144
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
145

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

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

150
					renderLine( _v1, _v2, element, material );
151

152
				}
153

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

156
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
157

158 159 160 161
				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;

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

166 167 168 169 170
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
171

172 173 174 175 176
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

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

				}
177

M
Mr.doob 已提交
178
			}
179 180 181

		}

M
Mr.doob 已提交
182
	};
183

184
	function calculateLights( lights ) {
M
Mr.doob 已提交
185

M
Mr.doob 已提交
186
		_ambientLight.setRGB( 0, 0, 0 );
187 188
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
189

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

192 193
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
194

M
Mr.doob 已提交
195 196 197 198 199 200 201
			if ( light instanceof THREE.AmbientLight ) {

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

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

203 204 205
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
206 207 208

			} else if ( light instanceof THREE.PointLight ) {

209 210 211
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
212 213 214 215 216 217 218

			}

		}

	}

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

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

223 224
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
225 226 227

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

234 235 236 237 238
				amount *= light.intensity;

				color.r += lightColor.r * amount;
				color.g += lightColor.g * amount;
				color.b += lightColor.b * amount;
M
Mr.doob 已提交
239 240 241

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

250 251 252 253 254 255 256
				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 已提交
257 258 259 260 261 262 263

			}

		}

	}

264
	function renderSprite( v1, element, material ) {
265

M
Mr.doob 已提交
266 267
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
268

M
Mr.doob 已提交
269
		_svgNode = getRectNode( _rectCount ++ );
270

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

276
		if ( material instanceof THREE.SpriteMaterial ) {
277

278
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
279 280 281 282 283 284

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
285

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

288
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
289

290 291 292 293 294
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

295
		if ( material instanceof THREE.LineBasicMaterial ) {
296

297
			_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 );
298

299
			_svg.appendChild( _svgNode );
300

301
		}
302

303
	}
M
Mr.doob 已提交
304

305
	function renderFace3( v1, v2, v3, element, material ) {
306

M
Mr.doob 已提交
307 308
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
309

310
		_svgNode = getPathNode( _pathCount ++ );
311
		_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' );
312

M
Mr.doob 已提交
313 314
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
315
			_color.copy( material.color );
M
Mr.doob 已提交
316

317 318
			if ( material.vertexColors === THREE.FaceColors ) {

319
				_color.multiply( element.color );
320 321 322

			}

323
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
324

325
			_diffuseColor.copy( material.color );
326

327 328
			if ( material.vertexColors === THREE.FaceColors ) {

329
				_diffuseColor.multiply( element.color );
330 331 332

			}

333
			_color.copy( _ambientLight );
334

S
srifqi 已提交
335
			calculateLight( _lights, element.centroidModel, element.normalModel, _color );
336

337
			_color.multiply( _diffuseColor ).add( material.emissive );
338

339 340 341
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
342
			_color.setRGB( _w, _w, _w );
343 344 345

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

346 347 348
			var normal = element.normalModelView;

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

M
Mr.doob 已提交
350
		}
351

M
Mr.doob 已提交
352
		if ( material.wireframe ) {
353

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

M
Mr.doob 已提交
356
		} else {
357

358
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
359 360 361 362 363 364 365

		}

		_svg.appendChild( _svgNode );

	}

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

	}
385

386
	function getPathNode( id ) {
387

388
		if ( _svgPathPool[ id ] == null ) {
389 390 391

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

392
			if ( _quality == 0 ) {
393 394 395 396 397 398 399

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

			}

			return _svgPathPool[ id ];

400
		}
401 402 403

		return _svgPathPool[ id ];

M
Mr.doob 已提交
404
	}
405

406
	function getRectNode( id ) {
407

408
		if ( _svgRectPool[ id ] == null ) {
409

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

412
			if ( _quality == 0 ) {
413

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

			}

418
			return _svgRectPool[ id ];
419

420
		}
421

422
		return _svgRectPool[ id ];
423 424 425

	}

M
Mr.doob 已提交
426
};