SVGRenderer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. ( function () {
  2. var SVGObject = function ( node ) {
  3. THREE.Object3D.call( this );
  4. this.node = node;
  5. };
  6. SVGObject.prototype = Object.create( THREE.Object3D.prototype );
  7. SVGObject.prototype.constructor = SVGObject;
  8. var SVGRenderer = function () {
  9. var _this = this,
  10. _renderData,
  11. _elements,
  12. _lights,
  13. _projector = new THREE.Projector(),
  14. _svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ),
  15. _svgWidth,
  16. _svgHeight,
  17. _svgWidthHalf,
  18. _svgHeightHalf,
  19. _v1,
  20. _v2,
  21. _v3,
  22. _clipBox = new THREE.Box2(),
  23. _elemBox = new THREE.Box2(),
  24. _color = new THREE.Color(),
  25. _diffuseColor = new THREE.Color(),
  26. _ambientLight = new THREE.Color(),
  27. _directionalLights = new THREE.Color(),
  28. _pointLights = new THREE.Color(),
  29. _clearColor = new THREE.Color(),
  30. _vector3 = new THREE.Vector3(),
  31. // Needed for PointLight
  32. _centroid = new THREE.Vector3(),
  33. _normal = new THREE.Vector3(),
  34. _normalViewMatrix = new THREE.Matrix3(),
  35. _viewMatrix = new THREE.Matrix4(),
  36. _viewProjectionMatrix = new THREE.Matrix4(),
  37. _svgPathPool = [],
  38. _svgNode,
  39. _pathCount = 0,
  40. _currentPath,
  41. _currentStyle,
  42. _quality = 1,
  43. _precision = null;
  44. this.domElement = _svg;
  45. this.autoClear = true;
  46. this.sortObjects = true;
  47. this.sortElements = true;
  48. this.overdraw = 0.5;
  49. this.info = {
  50. render: {
  51. vertices: 0,
  52. faces: 0
  53. }
  54. };
  55. this.setQuality = function ( quality ) {
  56. switch ( quality ) {
  57. case 'high':
  58. _quality = 1;
  59. break;
  60. case 'low':
  61. _quality = 0;
  62. break;
  63. }
  64. };
  65. this.setClearColor = function ( color ) {
  66. _clearColor.set( color );
  67. };
  68. this.setPixelRatio = function () {};
  69. this.setSize = function ( width, height ) {
  70. _svgWidth = width;
  71. _svgHeight = height;
  72. _svgWidthHalf = _svgWidth / 2;
  73. _svgHeightHalf = _svgHeight / 2;
  74. _svg.setAttribute( 'viewBox', - _svgWidthHalf + ' ' + - _svgHeightHalf + ' ' + _svgWidth + ' ' + _svgHeight );
  75. _svg.setAttribute( 'width', _svgWidth );
  76. _svg.setAttribute( 'height', _svgHeight );
  77. _clipBox.min.set( - _svgWidthHalf, - _svgHeightHalf );
  78. _clipBox.max.set( _svgWidthHalf, _svgHeightHalf );
  79. };
  80. this.getSize = function () {
  81. return {
  82. width: _svgWidth,
  83. height: _svgHeight
  84. };
  85. };
  86. this.setPrecision = function ( precision ) {
  87. _precision = precision;
  88. };
  89. function removeChildNodes() {
  90. _pathCount = 0;
  91. while ( _svg.childNodes.length > 0 ) {
  92. _svg.removeChild( _svg.childNodes[ 0 ] );
  93. }
  94. }
  95. function convert( c ) {
  96. return _precision !== null ? c.toFixed( _precision ) : c;
  97. }
  98. this.clear = function () {
  99. removeChildNodes();
  100. _svg.style.backgroundColor = _clearColor.getStyle();
  101. };
  102. this.render = function ( scene, camera ) {
  103. if ( camera instanceof THREE.Camera === false ) {
  104. console.error( 'THREE.SVGRenderer.render: camera is not an instance of THREE.Camera.' );
  105. return;
  106. }
  107. var background = scene.background;
  108. if ( background && background.isColor ) {
  109. removeChildNodes();
  110. _svg.style.backgroundColor = background.getStyle();
  111. } else if ( this.autoClear === true ) {
  112. this.clear();
  113. }
  114. _this.info.render.vertices = 0;
  115. _this.info.render.faces = 0;
  116. _viewMatrix.copy( camera.matrixWorldInverse );
  117. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  118. _renderData = _projector.projectScene( scene, camera, this.sortObjects, this.sortElements );
  119. _elements = _renderData.elements;
  120. _lights = _renderData.lights;
  121. _normalViewMatrix.getNormalMatrix( camera.matrixWorldInverse );
  122. calculateLights( _lights ); // reset accumulated path
  123. _currentPath = '';
  124. _currentStyle = '';
  125. for ( var e = 0, el = _elements.length; e < el; e ++ ) {
  126. var element = _elements[ e ];
  127. var material = element.material;
  128. if ( material === undefined || material.opacity === 0 ) continue;
  129. _elemBox.makeEmpty();
  130. if ( element instanceof THREE.RenderableSprite ) {
  131. _v1 = element;
  132. _v1.x *= _svgWidthHalf;
  133. _v1.y *= - _svgHeightHalf;
  134. renderSprite( _v1, element, material );
  135. } else if ( element instanceof THREE.RenderableLine ) {
  136. _v1 = element.v1;
  137. _v2 = element.v2;
  138. _v1.positionScreen.x *= _svgWidthHalf;
  139. _v1.positionScreen.y *= - _svgHeightHalf;
  140. _v2.positionScreen.x *= _svgWidthHalf;
  141. _v2.positionScreen.y *= - _svgHeightHalf;
  142. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen ] );
  143. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  144. renderLine( _v1, _v2, element, material );
  145. }
  146. } else if ( element instanceof THREE.RenderableFace ) {
  147. _v1 = element.v1;
  148. _v2 = element.v2;
  149. _v3 = element.v3;
  150. if ( _v1.positionScreen.z < - 1 || _v1.positionScreen.z > 1 ) continue;
  151. if ( _v2.positionScreen.z < - 1 || _v2.positionScreen.z > 1 ) continue;
  152. if ( _v3.positionScreen.z < - 1 || _v3.positionScreen.z > 1 ) continue;
  153. _v1.positionScreen.x *= _svgWidthHalf;
  154. _v1.positionScreen.y *= - _svgHeightHalf;
  155. _v2.positionScreen.x *= _svgWidthHalf;
  156. _v2.positionScreen.y *= - _svgHeightHalf;
  157. _v3.positionScreen.x *= _svgWidthHalf;
  158. _v3.positionScreen.y *= - _svgHeightHalf;
  159. if ( this.overdraw > 0 ) {
  160. expand( _v1.positionScreen, _v2.positionScreen, this.overdraw );
  161. expand( _v2.positionScreen, _v3.positionScreen, this.overdraw );
  162. expand( _v3.positionScreen, _v1.positionScreen, this.overdraw );
  163. }
  164. _elemBox.setFromPoints( [ _v1.positionScreen, _v2.positionScreen, _v3.positionScreen ] );
  165. if ( _clipBox.intersectsBox( _elemBox ) === true ) {
  166. renderFace3( _v1, _v2, _v3, element, material );
  167. }
  168. }
  169. }
  170. flushPath(); // just to flush last svg:path
  171. scene.traverseVisible( function ( object ) {
  172. if ( object instanceof SVGObject ) {
  173. _vector3.setFromMatrixPosition( object.matrixWorld );
  174. _vector3.applyMatrix4( _viewProjectionMatrix );
  175. if ( _vector3.z < - 1 || _vector3.z > 1 ) return;
  176. var x = _vector3.x * _svgWidthHalf;
  177. var y = - _vector3.y * _svgHeightHalf;
  178. var node = object.node;
  179. node.setAttribute( 'transform', 'translate(' + x + ',' + y + ')' );
  180. _svg.appendChild( node );
  181. }
  182. } );
  183. };
  184. function calculateLights( lights ) {
  185. _ambientLight.setRGB( 0, 0, 0 );
  186. _directionalLights.setRGB( 0, 0, 0 );
  187. _pointLights.setRGB( 0, 0, 0 );
  188. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  189. var light = lights[ l ];
  190. var lightColor = light.color;
  191. if ( light.isAmbientLight ) {
  192. _ambientLight.r += lightColor.r;
  193. _ambientLight.g += lightColor.g;
  194. _ambientLight.b += lightColor.b;
  195. } else if ( light.isDirectionalLight ) {
  196. _directionalLights.r += lightColor.r;
  197. _directionalLights.g += lightColor.g;
  198. _directionalLights.b += lightColor.b;
  199. } else if ( light.isPointLight ) {
  200. _pointLights.r += lightColor.r;
  201. _pointLights.g += lightColor.g;
  202. _pointLights.b += lightColor.b;
  203. }
  204. }
  205. }
  206. function calculateLight( lights, position, normal, color ) {
  207. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  208. var light = lights[ l ];
  209. var lightColor = light.color;
  210. if ( light.isDirectionalLight ) {
  211. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld ).normalize();
  212. var amount = normal.dot( lightPosition );
  213. if ( amount <= 0 ) continue;
  214. amount *= light.intensity;
  215. color.r += lightColor.r * amount;
  216. color.g += lightColor.g * amount;
  217. color.b += lightColor.b * amount;
  218. } else if ( light.isPointLight ) {
  219. var lightPosition = _vector3.setFromMatrixPosition( light.matrixWorld );
  220. var amount = normal.dot( _vector3.subVectors( lightPosition, position ).normalize() );
  221. if ( amount <= 0 ) continue;
  222. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  223. if ( amount == 0 ) continue;
  224. amount *= light.intensity;
  225. color.r += lightColor.r * amount;
  226. color.g += lightColor.g * amount;
  227. color.b += lightColor.b * amount;
  228. }
  229. }
  230. }
  231. function renderSprite( v1, element, material ) {
  232. var scaleX = element.scale.x * _svgWidthHalf;
  233. var scaleY = element.scale.y * _svgHeightHalf;
  234. if ( material.isPointsMaterial ) {
  235. scaleX *= material.size;
  236. scaleY *= material.size;
  237. }
  238. var path = 'M' + convert( v1.x - scaleX * 0.5 ) + ',' + convert( v1.y - scaleY * 0.5 ) + 'h' + convert( scaleX ) + 'v' + convert( scaleY ) + 'h' + convert( - scaleX ) + 'z';
  239. var style = '';
  240. if ( material.isSpriteMaterial || material.isPointsMaterial ) {
  241. style = 'fill:' + material.color.getStyle() + ';fill-opacity:' + material.opacity;
  242. }
  243. addPath( style, path );
  244. }
  245. function renderLine( v1, v2, element, material ) {
  246. var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y );
  247. if ( material.isLineBasicMaterial ) {
  248. var style = 'fill:none;stroke:' + material.color.getStyle() + ';stroke-opacity:' + material.opacity + ';stroke-width:' + material.linewidth + ';stroke-linecap:' + material.linecap;
  249. if ( material.isLineDashedMaterial ) {
  250. style = style + ';stroke-dasharray:' + material.dashSize + ',' + material.gapSize;
  251. }
  252. addPath( style, path );
  253. }
  254. }
  255. function renderFace3( v1, v2, v3, element, material ) {
  256. _this.info.render.vertices += 3;
  257. _this.info.render.faces ++;
  258. var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y ) + 'L' + convert( v3.positionScreen.x ) + ',' + convert( v3.positionScreen.y ) + 'z';
  259. var style = '';
  260. if ( material.isMeshBasicMaterial ) {
  261. _color.copy( material.color );
  262. if ( material.vertexColors ) {
  263. _color.multiply( element.color );
  264. }
  265. } else if ( material.isMeshLambertMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial ) {
  266. _diffuseColor.copy( material.color );
  267. if ( material.vertexColors ) {
  268. _diffuseColor.multiply( element.color );
  269. }
  270. _color.copy( _ambientLight );
  271. _centroid.copy( v1.positionWorld ).add( v2.positionWorld ).add( v3.positionWorld ).divideScalar( 3 );
  272. calculateLight( _lights, _centroid, element.normalModel, _color );
  273. _color.multiply( _diffuseColor ).add( material.emissive );
  274. } else if ( material.isMeshNormalMaterial ) {
  275. _normal.copy( element.normalModel ).applyMatrix3( _normalViewMatrix ).normalize();
  276. _color.setRGB( _normal.x, _normal.y, _normal.z ).multiplyScalar( 0.5 ).addScalar( 0.5 );
  277. }
  278. if ( material.wireframe ) {
  279. style = 'fill:none;stroke:' + _color.getStyle() + ';stroke-opacity:' + material.opacity + ';stroke-width:' + material.wireframeLinewidth + ';stroke-linecap:' + material.wireframeLinecap + ';stroke-linejoin:' + material.wireframeLinejoin;
  280. } else {
  281. style = 'fill:' + _color.getStyle() + ';fill-opacity:' + material.opacity;
  282. }
  283. addPath( style, path );
  284. } // Hide anti-alias gaps
  285. function expand( v1, v2, pixels ) {
  286. var x = v2.x - v1.x,
  287. y = v2.y - v1.y,
  288. det = x * x + y * y,
  289. idet;
  290. if ( det === 0 ) return;
  291. idet = pixels / Math.sqrt( det );
  292. x *= idet;
  293. y *= idet;
  294. v2.x += x;
  295. v2.y += y;
  296. v1.x -= x;
  297. v1.y -= y;
  298. }
  299. function addPath( style, path ) {
  300. if ( _currentStyle === style ) {
  301. _currentPath += path;
  302. } else {
  303. flushPath();
  304. _currentStyle = style;
  305. _currentPath = path;
  306. }
  307. }
  308. function flushPath() {
  309. if ( _currentPath ) {
  310. _svgNode = getPathNode( _pathCount ++ );
  311. _svgNode.setAttribute( 'd', _currentPath );
  312. _svgNode.setAttribute( 'style', _currentStyle );
  313. _svg.appendChild( _svgNode );
  314. }
  315. _currentPath = '';
  316. _currentStyle = '';
  317. }
  318. function getPathNode( id ) {
  319. if ( _svgPathPool[ id ] == null ) {
  320. _svgPathPool[ id ] = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  321. if ( _quality == 0 ) {
  322. _svgPathPool[ id ].setAttribute( 'shape-rendering', 'crispEdges' ); //optimizeSpeed
  323. }
  324. return _svgPathPool[ id ];
  325. }
  326. return _svgPathPool[ id ];
  327. }
  328. };
  329. THREE.SVGObject = SVGObject;
  330. THREE.SVGRenderer = SVGRenderer;
  331. } )();