SVGRenderer.js 10.5 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(),
26
	_clearAlpha = 1,
M
Mr.doob 已提交
27

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

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

36
	this.domElement = _svg;
M
Mr.doob 已提交
37

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

M
Mr.doob 已提交
42
	this.info = {
43

M
Mr.doob 已提交
44 45 46 47 48 49
		render: {

			vertices: 0,
			faces: 0

		}
50 51 52

	}

53 54 55 56 57 58 59 60
	this.setQuality = function( quality ) {

		switch(quality) {

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

		}
M
Mr.doob 已提交
61 62

	};
63

64 65 66 67 68
	// WebGLRenderer compatibility

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

69 70
	this.setClearColor = function ( color, alpha ) {

71 72
		_clearColor.set( color );
		_clearAlpha = alpha !== undefined ? alpha : 1;
73 74 75

	};

76
	this.setSize = function( width, height ) {
77

M
Mr.doob 已提交
78 79
		_svgWidth = width; _svgHeight = height;
		_svgWidthHalf = _svgWidth / 2; _svgHeightHalf = _svgHeight / 2;
80

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

M
Mr.doob 已提交
85 86
		_clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
		_clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
87

M
Mr.doob 已提交
88
	};
89

M
Mr.doob 已提交
90 91
	this.clear = function () {

92 93
		_pathCount = 0;
		_lineCount = 0;
M
Mr.doob 已提交
94
		_rectCount = 0;
95

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

98
			_svg.removeChild( _svg.childNodes[ 0 ] );
M
Mr.doob 已提交
99 100

		}
S
srifqi 已提交
101
		
102
		_svg.style.backgroundColor = 'rgba(' + ( ( _clearColor.r * 255 ) | 0 ) + ',' + ( ( _clearColor.g * 255 ) | 0 ) + ',' + ( ( _clearColor.b * 255 ) | 0 ) + ',' + _clearAlpha + ')';
M
Mr.doob 已提交
103 104 105

	};

106
	this.render = function ( scene, camera ) {
107

M
Mr.doob 已提交
108 109 110 111 112 113 114
		if ( camera instanceof THREE.Camera === false ) {

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

		}

115
		if ( this.autoClear === true ) this.clear();
116

M
Mr.doob 已提交
117 118
		_this.info.render.vertices = 0;
		_this.info.render.faces = 0;
119

120
		_renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
121 122
		_elements = _renderData.elements;
		_lights = _renderData.lights;
123

124
		calculateLights( _lights );
125

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

128 129
			var element = _elements[ e ];
			var material = element.material;
130

131
			if ( material === undefined || material.opacity === 0 ) continue;
132

M
Mr.doob 已提交
133
			_elemBox.makeEmpty();
134

135
			if ( element instanceof THREE.RenderableSprite ) {
136

137
				_v1 = element;
M
Mr.doob 已提交
138
				_v1.x *= _svgWidthHalf; _v1.y *= - _svgHeightHalf;
139

140
				renderSprite( _v1, element, material );
141

142
			} else if ( element instanceof THREE.RenderableLine ) {
143

144
				_v1 = element.v1; _v2 = element.v2;
145

146 147
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
148

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

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

153
					renderLine( _v1, _v2, element, material );
154

155
				}
156

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

159
				_v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
160

161 162 163 164
				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;

165 166 167 168
				_v1.positionScreen.x *= _svgWidthHalf; _v1.positionScreen.y *= - _svgHeightHalf;
				_v2.positionScreen.x *= _svgWidthHalf; _v2.positionScreen.y *= - _svgHeightHalf;
				_v3.positionScreen.x *= _svgWidthHalf; _v3.positionScreen.y *= - _svgHeightHalf;

169 170 171 172 173
				_elemBox.setFromPoints( [
					_v1.positionScreen,
					_v2.positionScreen,
					_v3.positionScreen
				] );
174

175 176 177 178 179
				if ( _clipBox.isIntersectionBox( _elemBox ) === true ) {

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

				}
180

M
Mr.doob 已提交
181
			}
182 183 184

		}

M
Mr.doob 已提交
185
	};
186

187
	function calculateLights( lights ) {
M
Mr.doob 已提交
188

M
Mr.doob 已提交
189
		_ambientLight.setRGB( 0, 0, 0 );
190 191
		_directionalLights.setRGB( 0, 0, 0 );
		_pointLights.setRGB( 0, 0, 0 );
M
Mr.doob 已提交
192

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

195 196
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
197

M
Mr.doob 已提交
198 199 200 201 202 203 204
			if ( light instanceof THREE.AmbientLight ) {

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

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

206 207 208
				_directionalLights.r += lightColor.r;
				_directionalLights.g += lightColor.g;
				_directionalLights.b += lightColor.b;
209 210 211

			} else if ( light instanceof THREE.PointLight ) {

212 213 214
				_pointLights.r += lightColor.r;
				_pointLights.g += lightColor.g;
				_pointLights.b += lightColor.b;
215 216 217 218 219 220 221

			}

		}

	}

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

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

226 227
			var light = lights[ l ];
			var lightColor = light.color;
M
Mr.doob 已提交
228 229 230

			if ( light instanceof THREE.DirectionalLight ) {

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

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

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

237 238 239 240 241
				amount *= light.intensity;

				color.r += lightColor.r * amount;
				color.g += lightColor.g * amount;
				color.b += lightColor.b * amount;
M
Mr.doob 已提交
242 243 244

			} else if ( light instanceof THREE.PointLight ) {

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

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

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

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

253 254 255 256 257 258 259
				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 已提交
260 261 262 263 264 265 266

			}

		}

	}

267
	function renderSprite( v1, element, material ) {
268

M
Mr.doob 已提交
269 270
		var scaleX = element.scale.x * _svgWidthHalf;
		var scaleY = element.scale.y * _svgHeightHalf;
271

M
Mr.doob 已提交
272
		_svgNode = getRectNode( _rectCount ++ );
273

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

279
		if ( material instanceof THREE.SpriteMaterial ) {
280

281
			_svgNode.setAttribute( 'style', 'fill: ' + material.color.getStyle() );
282 283 284 285 286 287

		}

		_svg.appendChild( _svgNode );

	}
M
Mr.doob 已提交
288

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

291
		_svgNode = getLineNode( _lineCount ++ );
M
Mr.doob 已提交
292

293 294 295 296 297
		_svgNode.setAttribute( 'x1', v1.positionScreen.x );
		_svgNode.setAttribute( 'y1', v1.positionScreen.y );
		_svgNode.setAttribute( 'x2', v2.positionScreen.x );
		_svgNode.setAttribute( 'y2', v2.positionScreen.y );

298
		if ( material instanceof THREE.LineBasicMaterial ) {
299

300
			_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 );
301

302
			_svg.appendChild( _svgNode );
303

304
		}
305

306
	}
M
Mr.doob 已提交
307

308
	function renderFace3( v1, v2, v3, element, material ) {
309

M
Mr.doob 已提交
310 311
		_this.info.render.vertices += 3;
		_this.info.render.faces ++;
312

313
		_svgNode = getPathNode( _pathCount ++ );
314
		_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' );
315

M
Mr.doob 已提交
316 317
		if ( material instanceof THREE.MeshBasicMaterial ) {

M
Mr.doob 已提交
318
			_color.copy( material.color );
M
Mr.doob 已提交
319

320 321
			if ( material.vertexColors === THREE.FaceColors ) {

322
				_color.multiply( element.color );
323 324 325

			}

326
		} else if ( material instanceof THREE.MeshLambertMaterial || material instanceof THREE.MeshPhongMaterial ) {
327

328
			_diffuseColor.copy( material.color );
329

330 331
			if ( material.vertexColors === THREE.FaceColors ) {

332
				_diffuseColor.multiply( element.color );
333 334 335

			}

336
			_color.copy( _ambientLight );
337

338 339 340
			_centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );

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

342
			_color.multiply( _diffuseColor ).add( material.emissive );
343

344 345 346
		} else if ( material instanceof THREE.MeshDepthMaterial ) {

			_w = 1 - ( material.__2near / (material.__farPlusNear - element.z * material.__farMinusNear) );
347
			_color.setRGB( _w, _w, _w );
348 349 350

		} else if ( material instanceof THREE.MeshNormalMaterial ) {

351 352 353
			var normal = element.normalModelView;

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

M
Mr.doob 已提交
355
		}
356

M
Mr.doob 已提交
357
		if ( material.wireframe ) {
358

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

M
Mr.doob 已提交
361
		} else {
362

363
			_svgNode.setAttribute( 'style', 'fill: ' + _color.getStyle() + '; fill-opacity: ' + material.opacity );
364 365 366 367 368 369 370

		}

		_svg.appendChild( _svgNode );

	}

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	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 ];

	}
390

391
	function getPathNode( id ) {
392

393
		if ( _svgPathPool[ id ] == null ) {
394 395 396

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

397
			if ( _quality == 0 ) {
398 399 400 401 402 403 404

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

			}

			return _svgPathPool[ id ];

405
		}
406 407 408

		return _svgPathPool[ id ];

M
Mr.doob 已提交
409
	}
410

411
	function getRectNode( id ) {
412

413
		if ( _svgRectPool[ id ] == null ) {
414

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

417
			if ( _quality == 0 ) {
418

M
Mr.doob 已提交
419
				_svgRectPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
420 421 422

			}

423
			return _svgRectPool[ id ];
424

425
		}
426

427
		return _svgRectPool[ id ];
428 429 430

	}

M
Mr.doob 已提交
431
};