2
0

SVGRenderer.js 12 KB

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