SVGLoader.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author zz85 / http://joshuakoo.com/
  4. * @author yomboprime / https://yombo.org
  5. */
  6. THREE.SVGLoader = function ( manager ) {
  7. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  8. };
  9. THREE.SVGLoader.prototype = {
  10. constructor: THREE.SVGLoader,
  11. load: function ( url, onLoad, onProgress, onError ) {
  12. var scope = this;
  13. var loader = new THREE.FileLoader( scope.manager );
  14. loader.setPath( scope.path );
  15. loader.load( url, function ( text ) {
  16. onLoad( scope.parse( text ) );
  17. }, onProgress, onError );
  18. },
  19. setPath: function ( value ) {
  20. this.path = value;
  21. return this;
  22. },
  23. parse: function ( text ) {
  24. function parseNode( node, style ) {
  25. if ( node.nodeType !== 1 ) return;
  26. var transform = getNodeTransform( node );
  27. var path = null;
  28. switch ( node.nodeName ) {
  29. case 'svg':
  30. break;
  31. case 'g':
  32. style = parseStyle( node, style );
  33. break;
  34. case 'path':
  35. style = parseStyle( node, style );
  36. if ( node.hasAttribute( 'd' ) ) path = parsePathNode( node, style );
  37. break;
  38. case 'rect':
  39. style = parseStyle( node, style );
  40. path = parseRectNode( node, style );
  41. break;
  42. case 'polygon':
  43. style = parseStyle( node, style );
  44. path = parsePolygonNode( node, style );
  45. break;
  46. case 'polyline':
  47. style = parseStyle( node, style );
  48. path = parsePolylineNode( node, style );
  49. break;
  50. case 'circle':
  51. style = parseStyle( node, style );
  52. path = parseCircleNode( node, style );
  53. break;
  54. case 'ellipse':
  55. style = parseStyle( node, style );
  56. path = parseEllipseNode( node, style );
  57. break;
  58. case 'line':
  59. style = parseStyle( node, style );
  60. path = parseLineNode( node, style );
  61. break;
  62. default:
  63. console.log( node );
  64. }
  65. if ( path ) {
  66. if ( style.fill !== undefined && style.fill !== 'none' ) {
  67. path.color.setStyle( style.fill );
  68. }
  69. transformPath( path, currentTransform );
  70. paths.push( path );
  71. path.userData = { node: node, style: style };
  72. }
  73. var nodes = node.childNodes;
  74. for ( var i = 0; i < nodes.length; i ++ ) {
  75. parseNode( nodes[ i ], style );
  76. }
  77. if ( transform ) {
  78. transformStack.pop();
  79. if ( transformStack.length > 0 ) {
  80. currentTransform.copy( transformStack[ transformStack.length - 1 ] );
  81. } else {
  82. currentTransform.identity();
  83. }
  84. }
  85. }
  86. function parsePathNode( node, style ) {
  87. var path = new THREE.ShapePath();
  88. var point = new THREE.Vector2();
  89. var control = new THREE.Vector2();
  90. var firstPoint = new THREE.Vector2();
  91. var isFirstPoint = true;
  92. var doSetFirstPoint = false;
  93. var d = node.getAttribute( 'd' );
  94. // console.log( d );
  95. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  96. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  97. var command = commands[ i ];
  98. var type = command.charAt( 0 );
  99. var data = command.substr( 1 ).trim();
  100. if ( isFirstPoint === true ) {
  101. doSetFirstPoint = true;
  102. isFirstPoint = false;
  103. }
  104. switch ( type ) {
  105. case 'M':
  106. var numbers = parseFloats( data );
  107. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  108. point.x = numbers[ j + 0 ];
  109. point.y = numbers[ j + 1 ];
  110. control.x = point.x;
  111. control.y = point.y;
  112. if ( j === 0 ) {
  113. path.moveTo( point.x, point.y );
  114. } else {
  115. path.lineTo( point.x, point.y );
  116. }
  117. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  118. }
  119. break;
  120. case 'H':
  121. var numbers = parseFloats( data );
  122. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  123. point.x = numbers[ j ];
  124. control.x = point.x;
  125. control.y = point.y;
  126. path.lineTo( point.x, point.y );
  127. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  128. }
  129. break;
  130. case 'V':
  131. var numbers = parseFloats( data );
  132. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  133. point.y = numbers[ j ];
  134. control.x = point.x;
  135. control.y = point.y;
  136. path.lineTo( point.x, point.y );
  137. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  138. }
  139. break;
  140. case 'L':
  141. var numbers = parseFloats( data );
  142. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  143. point.x = numbers[ j + 0 ];
  144. point.y = numbers[ j + 1 ];
  145. control.x = point.x;
  146. control.y = point.y;
  147. path.lineTo( point.x, point.y );
  148. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  149. }
  150. break;
  151. case 'C':
  152. var numbers = parseFloats( data );
  153. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  154. path.bezierCurveTo(
  155. numbers[ j + 0 ],
  156. numbers[ j + 1 ],
  157. numbers[ j + 2 ],
  158. numbers[ j + 3 ],
  159. numbers[ j + 4 ],
  160. numbers[ j + 5 ]
  161. );
  162. control.x = numbers[ j + 2 ];
  163. control.y = numbers[ j + 3 ];
  164. point.x = numbers[ j + 4 ];
  165. point.y = numbers[ j + 5 ];
  166. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  167. }
  168. break;
  169. case 'S':
  170. var numbers = parseFloats( data );
  171. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  172. path.bezierCurveTo(
  173. getReflection( point.x, control.x ),
  174. getReflection( point.y, control.y ),
  175. numbers[ j + 0 ],
  176. numbers[ j + 1 ],
  177. numbers[ j + 2 ],
  178. numbers[ j + 3 ]
  179. );
  180. control.x = numbers[ j + 0 ];
  181. control.y = numbers[ j + 1 ];
  182. point.x = numbers[ j + 2 ];
  183. point.y = numbers[ j + 3 ];
  184. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  185. }
  186. break;
  187. case 'Q':
  188. var numbers = parseFloats( data );
  189. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  190. path.quadraticCurveTo(
  191. numbers[ j + 0 ],
  192. numbers[ j + 1 ],
  193. numbers[ j + 2 ],
  194. numbers[ j + 3 ]
  195. );
  196. control.x = numbers[ j + 0 ];
  197. control.y = numbers[ j + 1 ];
  198. point.x = numbers[ j + 2 ];
  199. point.y = numbers[ j + 3 ];
  200. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  201. }
  202. break;
  203. case 'T':
  204. var numbers = parseFloats( data );
  205. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  206. var rx = getReflection( point.x, control.x );
  207. var ry = getReflection( point.y, control.y );
  208. path.quadraticCurveTo(
  209. rx,
  210. ry,
  211. numbers[ j + 0 ],
  212. numbers[ j + 1 ]
  213. );
  214. control.x = rx;
  215. control.y = ry;
  216. point.x = numbers[ j + 0 ];
  217. point.y = numbers[ j + 1 ];
  218. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  219. }
  220. break;
  221. case 'A':
  222. var numbers = parseFloats( data );
  223. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  224. var start = point.clone();
  225. point.x = numbers[ j + 5 ];
  226. point.y = numbers[ j + 6 ];
  227. control.x = point.x;
  228. control.y = point.y;
  229. parseArcCommand(
  230. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  231. );
  232. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  233. }
  234. break;
  235. //
  236. case 'm':
  237. var numbers = parseFloats( data );
  238. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  239. point.x += numbers[ j + 0 ];
  240. point.y += numbers[ j + 1 ];
  241. control.x = point.x;
  242. control.y = point.y;
  243. if ( j === 0 ) {
  244. path.moveTo( point.x, point.y );
  245. } else {
  246. path.lineTo( point.x, point.y );
  247. }
  248. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  249. }
  250. break;
  251. case 'h':
  252. var numbers = parseFloats( data );
  253. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  254. point.x += numbers[ j ];
  255. control.x = point.x;
  256. control.y = point.y;
  257. path.lineTo( point.x, point.y );
  258. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  259. }
  260. break;
  261. case 'v':
  262. var numbers = parseFloats( data );
  263. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  264. point.y += numbers[ j ];
  265. control.x = point.x;
  266. control.y = point.y;
  267. path.lineTo( point.x, point.y );
  268. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  269. }
  270. break;
  271. case 'l':
  272. var numbers = parseFloats( data );
  273. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  274. point.x += numbers[ j + 0 ];
  275. point.y += numbers[ j + 1 ];
  276. control.x = point.x;
  277. control.y = point.y;
  278. path.lineTo( point.x, point.y );
  279. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  280. }
  281. break;
  282. case 'c':
  283. var numbers = parseFloats( data );
  284. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  285. path.bezierCurveTo(
  286. point.x + numbers[ j + 0 ],
  287. point.y + numbers[ j + 1 ],
  288. point.x + numbers[ j + 2 ],
  289. point.y + numbers[ j + 3 ],
  290. point.x + numbers[ j + 4 ],
  291. point.y + numbers[ j + 5 ]
  292. );
  293. control.x = point.x + numbers[ j + 2 ];
  294. control.y = point.y + numbers[ j + 3 ];
  295. point.x += numbers[ j + 4 ];
  296. point.y += numbers[ j + 5 ];
  297. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  298. }
  299. break;
  300. case 's':
  301. var numbers = parseFloats( data );
  302. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  303. path.bezierCurveTo(
  304. getReflection( point.x, control.x ),
  305. getReflection( point.y, control.y ),
  306. point.x + numbers[ j + 0 ],
  307. point.y + numbers[ j + 1 ],
  308. point.x + numbers[ j + 2 ],
  309. point.y + numbers[ j + 3 ]
  310. );
  311. control.x = point.x + numbers[ j + 0 ];
  312. control.y = point.y + numbers[ j + 1 ];
  313. point.x += numbers[ j + 2 ];
  314. point.y += numbers[ j + 3 ];
  315. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  316. }
  317. break;
  318. case 'q':
  319. var numbers = parseFloats( data );
  320. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  321. path.quadraticCurveTo(
  322. point.x + numbers[ j + 0 ],
  323. point.y + numbers[ j + 1 ],
  324. point.x + numbers[ j + 2 ],
  325. point.y + numbers[ j + 3 ]
  326. );
  327. control.x = point.x + numbers[ j + 0 ];
  328. control.y = point.y + numbers[ j + 1 ];
  329. point.x += numbers[ j + 2 ];
  330. point.y += numbers[ j + 3 ];
  331. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  332. }
  333. break;
  334. case 't':
  335. var numbers = parseFloats( data );
  336. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  337. var rx = getReflection( point.x, control.x );
  338. var ry = getReflection( point.y, control.y );
  339. path.quadraticCurveTo(
  340. rx,
  341. ry,
  342. point.x + numbers[ j + 0 ],
  343. point.y + numbers[ j + 1 ]
  344. );
  345. control.x = rx;
  346. control.y = ry;
  347. point.x = point.x + numbers[ j + 0 ];
  348. point.y = point.y + numbers[ j + 1 ];
  349. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  350. }
  351. break;
  352. case 'a':
  353. var numbers = parseFloats( data );
  354. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  355. var start = point.clone();
  356. point.x += numbers[ j + 5 ];
  357. point.y += numbers[ j + 6 ];
  358. control.x = point.x;
  359. control.y = point.y;
  360. parseArcCommand(
  361. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  362. );
  363. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  364. }
  365. break;
  366. //
  367. case 'Z':
  368. case 'z':
  369. path.currentPath.autoClose = true;
  370. if ( path.currentPath.curves.length > 0 ) {
  371. // Reset point to beginning of Path
  372. point.copy( firstPoint );
  373. path.currentPath.currentPoint.copy( point );
  374. isFirstPoint = true;
  375. }
  376. break;
  377. default:
  378. console.warn( command );
  379. }
  380. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  381. doSetFirstPoint = false;
  382. }
  383. return path;
  384. }
  385. /**
  386. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  387. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  388. * From
  389. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  390. * To
  391. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  392. */
  393. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  394. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  395. // Ensure radii are positive
  396. rx = Math.abs( rx );
  397. ry = Math.abs( ry );
  398. // Compute (x1′, y1′)
  399. var dx2 = ( start.x - end.x ) / 2.0;
  400. var dy2 = ( start.y - end.y ) / 2.0;
  401. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  402. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  403. // Compute (cx′, cy′)
  404. var rxs = rx * rx;
  405. var rys = ry * ry;
  406. var x1ps = x1p * x1p;
  407. var y1ps = y1p * y1p;
  408. // Ensure radii are large enough
  409. var cr = x1ps / rxs + y1ps / rys;
  410. if ( cr > 1 ) {
  411. // scale up rx,ry equally so cr == 1
  412. var s = Math.sqrt( cr );
  413. rx = s * rx;
  414. ry = s * ry;
  415. rxs = rx * rx;
  416. rys = ry * ry;
  417. }
  418. var dq = ( rxs * y1ps + rys * x1ps );
  419. var pq = ( rxs * rys - dq ) / dq;
  420. var q = Math.sqrt( Math.max( 0, pq ) );
  421. if ( large_arc_flag === sweep_flag ) q = - q;
  422. var cxp = q * rx * y1p / ry;
  423. var cyp = - q * ry * x1p / rx;
  424. // Step 3: Compute (cx, cy) from (cx′, cy′)
  425. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  426. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  427. // Step 4: Compute θ1 and Δθ
  428. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  429. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  430. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  431. }
  432. function svgAngle( ux, uy, vx, vy ) {
  433. var dot = ux * vx + uy * vy;
  434. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  435. var ang = Math.acos( Math.max( -1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  436. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  437. return ang;
  438. }
  439. /*
  440. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  441. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  442. */
  443. function parseRectNode( node, style ) {
  444. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  445. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  446. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  447. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  448. var w = parseFloat( node.getAttribute( 'width' ) );
  449. var h = parseFloat( node.getAttribute( 'height' ) );
  450. var path = new THREE.ShapePath();
  451. path.moveTo( x + 2 * rx, y );
  452. path.lineTo( x + w - 2 * rx, y );
  453. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  454. path.lineTo( x + w, y + h - 2 * ry );
  455. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  456. path.lineTo( x + 2 * rx, y + h );
  457. if ( rx !== 0 || ry !== 0 ) {
  458. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  459. }
  460. path.lineTo( x, y + 2 * ry );
  461. if ( rx !== 0 || ry !== 0 ) {
  462. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  463. }
  464. return path;
  465. }
  466. function parsePolygonNode( node, style ) {
  467. function iterator( match, a, b ) {
  468. var x = parseFloat( a );
  469. var y = parseFloat( b );
  470. if ( index === 0 ) {
  471. path.moveTo( x, y );
  472. } else {
  473. path.lineTo( x, y );
  474. }
  475. index ++;
  476. }
  477. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  478. var path = new THREE.ShapePath();
  479. var index = 0;
  480. node.getAttribute( 'points' ).replace(regex, iterator);
  481. path.currentPath.autoClose = true;
  482. return path;
  483. }
  484. function parsePolylineNode( node, style ) {
  485. function iterator( match, a, b ) {
  486. var x = parseFloat( a );
  487. var y = parseFloat( b );
  488. if ( index === 0 ) {
  489. path.moveTo( x, y );
  490. } else {
  491. path.lineTo( x, y );
  492. }
  493. index ++;
  494. }
  495. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  496. var path = new THREE.ShapePath();
  497. var index = 0;
  498. node.getAttribute( 'points' ).replace(regex, iterator);
  499. path.currentPath.autoClose = false;
  500. return path;
  501. }
  502. function parseCircleNode( node, style ) {
  503. var x = parseFloat( node.getAttribute( 'cx' ) );
  504. var y = parseFloat( node.getAttribute( 'cy' ) );
  505. var r = parseFloat( node.getAttribute( 'r' ) );
  506. var subpath = new THREE.Path();
  507. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  508. var path = new THREE.ShapePath();
  509. path.subPaths.push( subpath );
  510. return path;
  511. }
  512. function parseEllipseNode( node, style ) {
  513. var x = parseFloat( node.getAttribute( 'cx' ) );
  514. var y = parseFloat( node.getAttribute( 'cy' ) );
  515. var rx = parseFloat( node.getAttribute( 'rx' ) );
  516. var ry = parseFloat( node.getAttribute( 'ry' ) );
  517. var subpath = new THREE.Path();
  518. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  519. var path = new THREE.ShapePath();
  520. path.subPaths.push( subpath );
  521. return path;
  522. }
  523. function parseLineNode( node, style ) {
  524. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  525. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  526. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  527. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  528. var path = new THREE.ShapePath();
  529. path.moveTo( x1, y1 );
  530. path.lineTo( x2, y2 );
  531. path.currentPath.autoClose = false;
  532. return path;
  533. }
  534. //
  535. function parseStyle( node, style ) {
  536. style = Object.assign( {}, style ); // clone style
  537. function addStyle( svgName, jsName, adjustFunction ) {
  538. if ( adjustFunction === undefined ) adjustFunction = function copy( v ) { return v; };
  539. if ( node.hasAttribute( svgName ) ) style[ jsName ] = adjustFunction( node.getAttribute( svgName ) );
  540. if ( node.style[ svgName ] !== '' ) style[ jsName ] = adjustFunction( node.style[ svgName ] );
  541. }
  542. function clamp( v ) {
  543. return Math.max( 0, Math.min( 1, v ) );
  544. }
  545. function positive( v ) {
  546. return Math.max( 0, v );
  547. }
  548. addStyle( 'fill', 'fill' );
  549. addStyle( 'fill-opacity', 'fillOpacity', clamp );
  550. addStyle( 'stroke', 'stroke' );
  551. addStyle( 'stroke-opacity', 'strokeOpacity', clamp );
  552. addStyle( 'stroke-width', 'strokeWidth', positive );
  553. addStyle( 'stroke-linejoin', 'strokeLineJoin' );
  554. addStyle( 'stroke-linecap', 'strokeLineCap' );
  555. addStyle( 'stroke-miterlimit', 'strokeMiterLimit', positive );
  556. return style;
  557. }
  558. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  559. function getReflection( a, b ) {
  560. return a - ( b - a );
  561. }
  562. function parseFloats( string ) {
  563. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  564. for ( var i = 0; i < array.length; i ++ ) {
  565. var number = array[ i ];
  566. // Handle values like 48.6037.7.8
  567. // TODO Find a regex for this
  568. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  569. var split = number.split( '.' );
  570. for ( var s = 2; s < split.length; s ++ ) {
  571. array.splice( i + s - 1, 0, '0.' + split[ s ] );
  572. }
  573. }
  574. array[ i ] = parseFloat( number );
  575. }
  576. return array;
  577. }
  578. function getNodeTransform( node ) {
  579. if ( ! node.hasAttribute( 'transform' ) ) {
  580. return null;
  581. }
  582. var transform = parseNodeTransform( node );
  583. if ( transform ) {
  584. if ( transformStack.length > 0 ) {
  585. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  586. }
  587. currentTransform.copy( transform );
  588. transformStack.push( transform );
  589. }
  590. return transform;
  591. }
  592. function parseNodeTransform( node ) {
  593. var transform = new THREE.Matrix3();
  594. var currentTransform = tempTransform0;
  595. var transformsTexts = node.getAttribute( 'transform' ).split( ')' );
  596. for ( var tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex -- ) {
  597. var transformText = transformsTexts[ tIndex ].trim();
  598. if (transformText == "")
  599. continue;
  600. var openParPos = transformText.indexOf( "(" );
  601. var closeParPos = transformText.length;
  602. if ( openParPos > 0 && openParPos < closeParPos ) {
  603. var transformType = transformText.substr( 0, openParPos );
  604. var array = parseFloats( transformText.substr( openParPos + 1, closeParPos - openParPos - 1 ) );
  605. currentTransform.identity();
  606. switch ( transformType ) {
  607. case "translate":
  608. if ( array.length >= 1 ) {
  609. var tx = array[ 0 ];
  610. var ty = tx;
  611. if ( array.length >= 2 ) {
  612. ty = array[ 1 ];
  613. }
  614. currentTransform.translate( tx, ty );
  615. }
  616. break;
  617. case "rotate":
  618. if ( array.length >= 1 ) {
  619. var angle = 0;
  620. var cx = 0;
  621. var cy = 0;
  622. // Angle
  623. angle = - array[ 0 ] * Math.PI / 180;
  624. if ( array.length >= 3 ) {
  625. // Center x, y
  626. cx = array[ 1 ];
  627. cy = array[ 2 ];
  628. }
  629. // Rotate around center (cx, cy)
  630. tempTransform1.identity().translate( -cx, -cy );
  631. tempTransform2.identity().rotate( angle );
  632. tempTransform3.multiplyMatrices( tempTransform2, tempTransform1 );
  633. tempTransform1.identity().translate( cx, cy );
  634. currentTransform.multiplyMatrices( tempTransform1, tempTransform3 );
  635. }
  636. break;
  637. case "scale":
  638. if ( array.length >= 1 ) {
  639. var scaleX = array[ 0 ];
  640. var scaleY = scaleX;
  641. if ( array.length >= 2 ) {
  642. scaleY = array[ 1 ];
  643. }
  644. currentTransform.scale( scaleX, scaleY );
  645. }
  646. break;
  647. case "skewX":
  648. if ( array.length === 1 ) {
  649. currentTransform.set(
  650. 1, Math.tan( array[ 0 ] * Math.PI / 180 ), 0,
  651. 0, 1, 0,
  652. 0, 0, 1
  653. );
  654. }
  655. break;
  656. case "skewY":
  657. if ( array.length === 1 ) {
  658. currentTransform.set(
  659. 1, 0, 0,
  660. Math.tan( array[ 0 ] * Math.PI / 180 ), 1, 0,
  661. 0, 0, 1
  662. );
  663. }
  664. break;
  665. case "matrix":
  666. if ( array.length === 6 ) {
  667. currentTransform.set(
  668. array[ 0 ], array[ 2 ], array[ 4 ],
  669. array[ 1 ], array[ 3 ], array[ 5 ],
  670. 0, 0, 1
  671. );
  672. }
  673. break;
  674. }
  675. }
  676. transform.premultiply( currentTransform );
  677. }
  678. return transform;
  679. }
  680. function transformPath( path, m ) {
  681. function transfVec2( v2 ) {
  682. tempV3.set( v2.x, v2.y, 1 ).applyMatrix3( m );
  683. v2.set( tempV3.x, tempV3.y );
  684. }
  685. var isRotated = isTransformRotated( m );
  686. var subPaths = path.subPaths;
  687. for ( var i = 0, n = subPaths.length; i < n; i++ ) {
  688. var subPath = subPaths[ i ];
  689. var curves = subPath.curves;
  690. for ( var j = 0; j < curves.length; j++ ) {
  691. var curve = curves[ j ];
  692. if ( curve.isLineCurve ) {
  693. transfVec2( curve.v1 );
  694. transfVec2( curve.v2 );
  695. } else if ( curve.isCubicBezierCurve ) {
  696. transfVec2( curve.v0 );
  697. transfVec2( curve.v1 );
  698. transfVec2( curve.v2 );
  699. transfVec2( curve.v3 );
  700. } else if ( curve.isQuadraticBezierCurve ) {
  701. transfVec2( curve.v0 );
  702. transfVec2( curve.v1 );
  703. transfVec2( curve.v2 );
  704. } else if ( curve.isEllipseCurve ) {
  705. if ( isRotated ) {
  706. console.warn( "SVGLoader: Elliptic arc or ellipse rotation or skewing is not implemented." );
  707. }
  708. tempV2.set( curve.aX, curve.aY );
  709. transfVec2( tempV2 );
  710. curve.aX = tempV2.x;
  711. curve.aY = tempV2.y;
  712. curve.xRadius *= getTransformScaleX( m );
  713. curve.yRadius *= getTransformScaleY( m );
  714. }
  715. }
  716. }
  717. }
  718. function isTransformRotated( m ) {
  719. return m.elements[ 1 ] !== 0 || m.elements[ 3 ] !== 0;
  720. }
  721. function getTransformScaleX( m ) {
  722. var te = m.elements;
  723. return Math.sqrt( te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] )
  724. }
  725. function getTransformScaleY( m ) {
  726. var te = m.elements;
  727. return Math.sqrt( te[ 3 ] * te[ 3 ] + te[ 4 ] * te[ 4 ] )
  728. }
  729. //
  730. console.log( 'THREE.SVGLoader' );
  731. var paths = [];
  732. var transformStack = [];
  733. var tempTransform0 = new THREE.Matrix3();
  734. var tempTransform1 = new THREE.Matrix3();
  735. var tempTransform2 = new THREE.Matrix3();
  736. var tempTransform3 = new THREE.Matrix3();
  737. var tempV2 = new THREE.Vector2();
  738. var tempV3 = new THREE.Vector3();
  739. var currentTransform = new THREE.Matrix3();
  740. var scope = this;
  741. console.time( 'THREE.SVGLoader: DOMParser' );
  742. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  743. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  744. console.time( 'THREE.SVGLoader: Parse' );
  745. parseNode( xml.documentElement, {
  746. fill: '#000',
  747. fillOpacity: 1,
  748. strokeOpacity: 1,
  749. strokeWidth: 1,
  750. strokeLineJoin: 'miter',
  751. strokeLineCap: 'butt',
  752. strokeMiterLimit: 4
  753. } );
  754. var data = { paths: paths, xml: xml.documentElement };
  755. // console.log( paths );
  756. console.timeEnd( 'THREE.SVGLoader: Parse' );
  757. return data;
  758. }
  759. };
  760. THREE.SVGLoader.getStrokeStyle = function ( width, color, opacity, lineJoin, lineCap, miterLimit ) {
  761. // Param width: Stroke width
  762. // Param color: As returned by THREE.Color.getStyle()
  763. // Param opacity: 0 (transparent) to 1 (opaque)
  764. // Param lineJoin: One of "round", "bevel", "miter" or "miter-limit"
  765. // Param lineCap: One of "round", "square" or "butt"
  766. // Param miterLimit: Maximum join length, in multiples of the "width" parameter (join is truncated if it exceeds that distance)
  767. // Returns style object
  768. width = width !== undefined ? width : 1;
  769. color = color !== undefined ? color : '#000';
  770. opacity = opacity !== undefined ? opacity : 1;
  771. lineJoin = lineJoin !== undefined ? lineJoin : 'miter';
  772. lineCap = lineCap !== undefined ? lineCap : 'butt';
  773. miterLimit = miterLimit !== undefined ? miterLimit : 4;
  774. return {
  775. strokeColor: color,
  776. strokeWidth: width,
  777. strokeLineJoin: lineJoin,
  778. strokeLineCap: lineCap,
  779. strokeMiterLimit: miterLimit
  780. };
  781. };
  782. THREE.SVGLoader.pointsToStroke = function ( points, style, arcDivisions, minDistance ) {
  783. // Generates a stroke with some witdh around the given path.
  784. // The path can be open or closed (last point equals to first point)
  785. // Param points: Array of Vector2D (the path). Minimum 2 points.
  786. // Param style: Object with SVG properties as returned by SVGLoader.getStrokeStyle(), or SVGLoader.parse() in the path.userData.style object
  787. // Params arcDivisions: Arc divisions for round joins and endcaps. (Optional)
  788. // Param minDistance: Points closer to this distance will be merged. (Optional)
  789. // Returns BufferGeometry with stroke triangles (In plane z = 0). UV coordinates are generated ('u' along path. 'v' across it, from left to right)
  790. var vertices = [];
  791. var normals = [];
  792. var uvs = [];
  793. if ( THREE.SVGLoader.pointsToStrokeWithBuffers( points, style, arcDivisions, minDistance, vertices, normals, uvs ) === 0 ) {
  794. return null;
  795. }
  796. var geometry = new THREE.BufferGeometry();
  797. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  798. geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  799. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  800. return geometry;
  801. };
  802. THREE.SVGLoader.pointsToStrokeWithBuffers = function () {
  803. var tempV2_1 = new THREE.Vector2();
  804. var tempV2_2 = new THREE.Vector2();
  805. var tempV2_3 = new THREE.Vector2();
  806. var tempV2_4 = new THREE.Vector2();
  807. var tempV2_5 = new THREE.Vector2();
  808. var tempV2_6 = new THREE.Vector2();
  809. var tempV2_7 = new THREE.Vector2();
  810. var tempV3_1 = new THREE.Vector3();
  811. var lastPointL = new THREE.Vector2();
  812. var lastPointR = new THREE.Vector2();
  813. var point0L = new THREE.Vector2();
  814. var point0R = new THREE.Vector2();
  815. var currentPointL = new THREE.Vector2();
  816. var currentPointR = new THREE.Vector2();
  817. var nextPointL = new THREE.Vector2();
  818. var nextPointR = new THREE.Vector2();
  819. var innerPoint = new THREE.Vector2();
  820. var outerPoint = new THREE.Vector2();
  821. var tempTransform0 = new THREE.Matrix3();
  822. var tempTransform1 = new THREE.Matrix3();
  823. var tempTransform2 = new THREE.Matrix3();
  824. return function ( points, style, arcDivisions, minDistance, vertices, normals, uvs, vertexOffset ) {
  825. // This function can be called to update existing arrays or buffers.
  826. // Accepts same parameters as pointsToStroke, plus the buffers and optional offset.
  827. // Param vertexOffset: Offset vertices to start writing in the buffers (3 elements/vertex for vertices and normals, and 2 elements/vertex for uvs)
  828. // Returns number of written vertices / normals / uvs pairs
  829. // if 'vertices' parameter is undefined no triangles will be generated, but the returned vertices count will still be valid (useful to preallocate the buffers)
  830. // 'normals' and 'uvs' buffers are optional
  831. arcLengthDivisions = arcDivisions !== undefined ? arcDivisions : 12;
  832. minDistance = minDistance !== undefined ? minDistance : 0.001;
  833. vertexOffset = vertexOffset !== undefined ? vertexOffset : 0;
  834. // First ensure there are no duplicated points
  835. points = removeDuplicatedPoints( points );
  836. var numPoints = points.length;
  837. if ( numPoints < 2 ) return 0;
  838. var isClosed = points[ 0 ].equals( points[ numPoints - 1 ] );
  839. var currentPoint;
  840. var previousPoint = points[ 0 ];
  841. var nextPoint;
  842. var strokeWidth2 = style.strokeWidth / 2;
  843. var deltaU = 1 / ( numPoints - 1 );
  844. var u0 = 0;
  845. var innerSideModified;
  846. var joinIsOnLeftSide;
  847. var isMiter;
  848. var initialJoinIsOnLeftSide = false;
  849. var numVertices = 0;
  850. var currentCoordinate = vertexOffset * 3;
  851. var currentCoordinateUV = vertexOffset * 2;
  852. // Get initial left and right stroke points
  853. getNormal( points[ 0 ], points[ 1 ], tempV2_1 ).multiplyScalar( strokeWidth2 );
  854. lastPointL.copy( points[ 0 ] ).sub( tempV2_1 );
  855. lastPointR.copy( points[ 0 ] ).add( tempV2_1 );
  856. point0L.copy( lastPointL );
  857. point0R.copy( lastPointR );
  858. for ( var iPoint = 1; iPoint < numPoints; iPoint ++ ) {
  859. currentPoint = points[ iPoint ];
  860. // Get next point
  861. if ( iPoint === numPoints - 1 ) {
  862. if ( isClosed ) {
  863. // Skip duplicated initial point
  864. nextPoint = points[ 1 ];
  865. }
  866. else nextPoint = undefined;
  867. }
  868. else {
  869. nextPoint = points[ iPoint + 1 ];
  870. }
  871. // Normal of previous segment in tempV2_1
  872. var normal1 = tempV2_1;
  873. getNormal( previousPoint, currentPoint, normal1 );
  874. tempV2_3.copy( normal1 ).multiplyScalar( strokeWidth2 );
  875. currentPointL.copy( currentPoint ).sub( tempV2_3 );
  876. currentPointR.copy( currentPoint ).add( tempV2_3 );
  877. var u1 = u0 + deltaU;
  878. innerSideModified = false;
  879. if ( nextPoint !== undefined ) {
  880. // Normal of next segment in tempV2_2
  881. getNormal( currentPoint, nextPoint, tempV2_2 );
  882. tempV2_3.copy( tempV2_2 ).multiplyScalar( strokeWidth2 );
  883. nextPointL.copy( currentPoint ).sub( tempV2_3 );
  884. nextPointR.copy( currentPoint ).add( tempV2_3 );
  885. joinIsOnLeftSide = true;
  886. tempV2_3.subVectors( nextPoint, previousPoint );
  887. if ( normal1.dot( tempV2_3 ) < 0 ) {
  888. joinIsOnLeftSide = false;
  889. }
  890. if ( iPoint === 1 ) initialJoinIsOnLeftSide = joinIsOnLeftSide;
  891. tempV2_3.subVectors( nextPoint, currentPoint )
  892. var maxInnerDistance = tempV2_3.normalize();
  893. var dot = Math.abs( normal1.dot( tempV2_3 ) );
  894. // If path is straight, don't create join
  895. if ( dot !== 0 ) {
  896. // Compute inner and outer segment intersections
  897. var miterSide = strokeWidth2 / dot;
  898. tempV2_3.multiplyScalar( - miterSide );
  899. tempV2_4.subVectors( currentPoint, previousPoint );
  900. tempV2_5.copy( tempV2_4 ).setLength( miterSide ).add( tempV2_3 );
  901. innerPoint.copy( tempV2_5 ).negate();
  902. var miterLength2 = tempV2_5.length();
  903. var segmentLengthPrev = tempV2_4.length();
  904. tempV2_4.divideScalar( segmentLengthPrev );
  905. tempV2_6.subVectors( nextPoint, currentPoint );
  906. var segmentLengthNext = tempV2_6.length();
  907. tempV2_6.divideScalar( segmentLengthNext );
  908. // Check that previous and next segments doesn't overlap with the innerPoint of intersection
  909. if ( tempV2_4.dot( innerPoint ) < segmentLengthPrev && tempV2_6.dot( innerPoint ) < segmentLengthNext ) {
  910. innerSideModified = true;
  911. }
  912. outerPoint.copy( tempV2_5 ).add( currentPoint );
  913. innerPoint.add( currentPoint );
  914. isMiter = false;
  915. if ( innerSideModified ) {
  916. if ( joinIsOnLeftSide ) {
  917. nextPointR.copy( innerPoint );
  918. currentPointR.copy( innerPoint );
  919. }
  920. else {
  921. nextPointL.copy( innerPoint );
  922. currentPointL.copy( innerPoint );
  923. }
  924. }
  925. else {
  926. // The segment triangles are generated here if there was overlapping
  927. makeSegmentTriangles();
  928. }
  929. switch ( style.strokeLineJoin ) {
  930. case 'bevel':
  931. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  932. break;
  933. case 'round':
  934. // Segment triangles
  935. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  936. // Join triangles
  937. if ( joinIsOnLeftSide ) {
  938. makeCircularSector( currentPoint, currentPointL, nextPointL, u1, 0 );
  939. }
  940. else {
  941. makeCircularSector( currentPoint, nextPointR, currentPointR, u1, 1 );
  942. }
  943. break;
  944. case 'miter':
  945. case 'miter-clip':
  946. default:
  947. var miterFraction = ( strokeWidth2 * style.strokeMiterLimit ) / miterLength2;
  948. if ( miterFraction < 1 ) {
  949. // The join miter length exceeds the miter limit
  950. if ( style.strokeLineJoin !== 'miter-clip' ) {
  951. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  952. break;
  953. }
  954. else {
  955. // Segment triangles
  956. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  957. // Miter-clip join triangles
  958. if ( joinIsOnLeftSide ) {
  959. tempV2_6.subVectors( outerPoint, currentPointL ).multiplyScalar( miterFraction ).add( currentPointL );
  960. tempV2_7.subVectors( outerPoint, nextPointL ).multiplyScalar( miterFraction ).add( nextPointL );
  961. addVertex( currentPointL, u1, 0 );
  962. addVertex( tempV2_6, u1, 0 );
  963. addVertex( currentPoint, u1, 0.5 );
  964. addVertex( currentPoint, u1, 0.5 );
  965. addVertex( tempV2_6, u1, 0 );
  966. addVertex( tempV2_7, u1, 0 );
  967. addVertex( currentPoint, u1, 0.5 );
  968. addVertex( tempV2_7, u1, 0 );
  969. addVertex( nextPointL, u1, 0 );
  970. }
  971. else {
  972. tempV2_6.subVectors( outerPoint, currentPointR ).multiplyScalar( miterFraction ).add( currentPointR );
  973. tempV2_7.subVectors( outerPoint, nextPointR ).multiplyScalar( miterFraction ).add( nextPointR );
  974. addVertex( currentPointR, u1, 1 );
  975. addVertex( tempV2_6, u1, 1 );
  976. addVertex( currentPoint, u1, 0.5 );
  977. addVertex( currentPoint, u1, 0.5 );
  978. addVertex( tempV2_6, u1, 1 );
  979. addVertex( tempV2_7, u1, 1 );
  980. addVertex( currentPoint, u1, 0.5 );
  981. addVertex( tempV2_7, u1, 1 );
  982. addVertex( nextPointR, u1, 1 );
  983. }
  984. }
  985. }
  986. else {
  987. // Miter join segment triangles
  988. if ( innerSideModified ) {
  989. // Optimized segment + join triangles
  990. if ( joinIsOnLeftSide ) {
  991. addVertex( lastPointR, u0, 1 );
  992. addVertex( lastPointL, u0, 0 );
  993. addVertex( outerPoint, u1, 0 );
  994. addVertex( lastPointR, u0, 1 );
  995. addVertex( outerPoint, u1, 0 );
  996. addVertex( innerPoint, u1, 1 );
  997. }
  998. else {
  999. addVertex( lastPointR, u0, 1 );
  1000. addVertex( lastPointL, u0, 0 );
  1001. addVertex( outerPoint, u1, 1 );
  1002. addVertex( lastPointL, u0, 0 );
  1003. addVertex( innerPoint, u1, 0 );
  1004. addVertex( outerPoint, u1, 1 );
  1005. }
  1006. if ( joinIsOnLeftSide ) {
  1007. nextPointL.copy( outerPoint );
  1008. }
  1009. else {
  1010. nextPointR.copy( outerPoint );
  1011. }
  1012. }
  1013. else {
  1014. // Add extra miter join triangles
  1015. if ( joinIsOnLeftSide ) {
  1016. addVertex( currentPointL, u1, 0 );
  1017. addVertex( outerPoint, u1, 0 );
  1018. addVertex( currentPoint, u1, 0.5 );
  1019. addVertex( currentPoint, u1, 0.5 );
  1020. addVertex( outerPoint, u1, 0 );
  1021. addVertex( nextPointL, u1, 0 );
  1022. }
  1023. else {
  1024. addVertex( currentPointR, u1, 1 );
  1025. addVertex( outerPoint, u1, 1 );
  1026. addVertex( currentPoint, u1, 0.5 );
  1027. addVertex( currentPoint, u1, 0.5 );
  1028. addVertex( outerPoint, u1, 1 );
  1029. addVertex( nextPointR, u1, 1 );
  1030. }
  1031. }
  1032. isMiter = true;
  1033. }
  1034. break;
  1035. }
  1036. }
  1037. else {
  1038. // The segment triangles are generated here when two consecutive points are collinear
  1039. makeSegmentTriangles();
  1040. }
  1041. }
  1042. else {
  1043. // The segment triangles are generated here if it is the ending segment
  1044. makeSegmentTriangles();
  1045. }
  1046. if ( ! isClosed && iPoint === numPoints - 1 ) {
  1047. // Start line endcap
  1048. addCapGeometry( points[ 0 ], point0L, point0R, joinIsOnLeftSide, true, u0 );
  1049. }
  1050. // Increment loop variables
  1051. u0 = u1;
  1052. previousPoint = currentPoint;
  1053. lastPointL.copy( nextPointL );
  1054. lastPointR.copy( nextPointR );
  1055. }
  1056. if ( ! isClosed ) {
  1057. // Ending line endcap
  1058. addCapGeometry( currentPoint, currentPointL, currentPointR, joinIsOnLeftSide, false, u1 );
  1059. }
  1060. else if ( innerSideModified && vertices ) {
  1061. // Modify path first segment vertices to adjust to the segments inner and outer intersections
  1062. var lastOuter = outerPoint;
  1063. var lastInner = innerPoint;
  1064. if ( initialJoinIsOnLeftSide !== joinIsOnLeftSide) {
  1065. lastOuter = innerPoint;
  1066. lastInner = outerPoint;
  1067. }
  1068. if ( joinIsOnLeftSide ) {
  1069. lastInner.toArray( vertices, 0 * 3 );
  1070. lastInner.toArray( vertices, 3 * 3 );
  1071. if ( isMiter ) {
  1072. lastOuter.toArray( vertices, 1 * 3 );
  1073. }
  1074. }
  1075. else {
  1076. lastInner.toArray( vertices, 1 * 3 );
  1077. lastInner.toArray( vertices, 3 * 3 );
  1078. if ( isMiter ) {
  1079. lastOuter.toArray( vertices, 0 * 3 );
  1080. }
  1081. }
  1082. }
  1083. return numVertices;
  1084. // -- End of algorithm
  1085. // -- Functions
  1086. function getNormal( p1, p2, result ) {
  1087. result.subVectors( p2, p1 );
  1088. return result.set( - result.y, result.x ).normalize();
  1089. }
  1090. function addVertex( position, u, v ) {
  1091. if ( vertices ) {
  1092. vertices[ currentCoordinate ] = position.x;
  1093. vertices[ currentCoordinate + 1 ] = position.y;
  1094. vertices[ currentCoordinate + 2 ] = 0;
  1095. if ( normals ) {
  1096. normals[ currentCoordinate ] = 0;
  1097. normals[ currentCoordinate + 1 ] = 0;
  1098. normals[ currentCoordinate + 2 ] = 1;
  1099. }
  1100. currentCoordinate += 3;
  1101. if ( uvs ) {
  1102. uvs[ currentCoordinateUV ] = u;
  1103. uvs[ currentCoordinateUV + 1 ] = v;
  1104. currentCoordinateUV += 2;
  1105. }
  1106. }
  1107. numVertices += 3;
  1108. }
  1109. function makeCircularSector( center, p1, p2, u, v ) {
  1110. // param p1, p2: Points in the circle arc.
  1111. // p1 and p2 are in clockwise direction.
  1112. tempV2_1.copy( p1 ).sub( center ).normalize();
  1113. tempV2_2.copy( p2 ).sub( center ).normalize();
  1114. var angle = Math.PI;
  1115. var dot = tempV2_1.dot( tempV2_2 );
  1116. if ( Math.abs( dot ) < 1 ) angle = Math.abs( Math.acos( dot ) );
  1117. angle /= arcLengthDivisions;
  1118. tempV2_3.copy( p1 );
  1119. for ( var i = 0, il = arcLengthDivisions - 1; i < il; i++ ) {
  1120. tempV2_4.copy( tempV2_3 ).rotateAround( center, angle );
  1121. addVertex( tempV2_3, u, v );
  1122. addVertex( tempV2_4, u, v );
  1123. addVertex( center, u, 0.5 );
  1124. tempV2_3.copy( tempV2_4 );
  1125. }
  1126. addVertex( tempV2_4, u, v );
  1127. addVertex( p2, u, v );
  1128. addVertex( center, u, 0.5 );
  1129. }
  1130. function makeSegmentTriangles() {
  1131. addVertex( lastPointR, u0, 1 );
  1132. addVertex( lastPointL, u0, 0 );
  1133. addVertex( currentPointL, u1, 0 );
  1134. addVertex( lastPointR, u0, 1 );
  1135. addVertex( currentPointL, u1, 1 );
  1136. addVertex( currentPointR, u1, 0 );
  1137. }
  1138. function makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u ) {
  1139. if ( innerSideModified ) {
  1140. // Optimized segment + bevel triangles
  1141. if ( joinIsOnLeftSide ) {
  1142. // Path segments triangles
  1143. addVertex( lastPointR, u0, 1 );
  1144. addVertex( lastPointL, u0, 0 );
  1145. addVertex( currentPointL, u1, 0 );
  1146. addVertex( lastPointR, u0, 1 );
  1147. addVertex( currentPointL, u1, 0 );
  1148. addVertex( innerPoint, u1, 1 );
  1149. // Bevel join triangle
  1150. addVertex( currentPointL, u, 0 );
  1151. addVertex( nextPointL, u, 0 );
  1152. addVertex( innerPoint, u, 0.5 );
  1153. }
  1154. else {
  1155. // Path segments triangles
  1156. addVertex( lastPointR, u0, 1 );
  1157. addVertex( lastPointL, u0, 0 );
  1158. addVertex( currentPointR, u1, 1 );
  1159. addVertex( lastPointL, u0, 0 );
  1160. addVertex( innerPoint, u1, 0 );
  1161. addVertex( currentPointR, u1, 1 );
  1162. // Bevel join triangle
  1163. addVertex( currentPointR, u, 1 );
  1164. addVertex( nextPointR, u, 0 );
  1165. addVertex( innerPoint, u, 0.5 );
  1166. }
  1167. }
  1168. else {
  1169. // Bevel join triangle. The segment triangles are done in the main loop
  1170. if ( joinIsOnLeftSide ) {
  1171. addVertex( currentPointL, u, 0 );
  1172. addVertex( nextPointL, u, 0 );
  1173. addVertex( currentPoint, u, 0.5 );
  1174. }
  1175. else {
  1176. addVertex( currentPointR, u, 1 );
  1177. addVertex( nextPointR, u, 0 );
  1178. addVertex( currentPoint, u, 0.5 );
  1179. }
  1180. }
  1181. }
  1182. function createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified ) {
  1183. if ( innerSideModified ) {
  1184. if ( joinIsOnLeftSide ) {
  1185. addVertex( lastPointR, u0, 1 );
  1186. addVertex( lastPointL, u0, 0 );
  1187. addVertex( currentPointL, u1, 0 );
  1188. addVertex( lastPointR, u0, 1 );
  1189. addVertex( currentPointL, u1, 0 );
  1190. addVertex( innerPoint, u1, 1 );
  1191. addVertex( currentPointL, u0, 0 );
  1192. addVertex( currentPoint, u1, 0.5 );
  1193. addVertex( innerPoint, u1, 1 );
  1194. addVertex( currentPoint, u1, 0.5 );
  1195. addVertex( nextPointL, u0, 0 );
  1196. addVertex( innerPoint, u1, 1 );
  1197. }
  1198. else {
  1199. addVertex( lastPointR, u0, 1 );
  1200. addVertex( lastPointL, u0, 0 );
  1201. addVertex( currentPointR, u1, 1 );
  1202. addVertex( lastPointL, u0, 0 );
  1203. addVertex( innerPoint, u1, 0 );
  1204. addVertex( currentPointR, u1, 1 );
  1205. addVertex( currentPointR, u0, 1 );
  1206. addVertex( innerPoint, u1, 0 );
  1207. addVertex( currentPoint, u1, 0.5 );
  1208. addVertex( currentPoint, u1, 0.5 );
  1209. addVertex( innerPoint, u1, 0 );
  1210. addVertex( nextPointR, u0, 1 );
  1211. }
  1212. }
  1213. }
  1214. function addCapGeometry( center, p1, p2, joinIsOnLeftSide, start, u ) {
  1215. // param center: End point of the path
  1216. // param p1, p2: Left and right cap points
  1217. switch ( style.strokeLineCap ) {
  1218. case 'round':
  1219. if ( start ) {
  1220. makeCircularSector( center, p2, p1, u, 0.5 );
  1221. }
  1222. else {
  1223. makeCircularSector( center, p1, p2, u, 0.5 );
  1224. }
  1225. break;
  1226. case 'square':
  1227. if ( start ) {
  1228. tempV2_1.subVectors( p1, center );
  1229. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1230. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1231. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1232. // Modify already existing vertices
  1233. if ( joinIsOnLeftSide ) {
  1234. tempV2_3.toArray( vertices, 1 * 3 );
  1235. tempV2_4.toArray( vertices, 0 * 3 );
  1236. tempV2_4.toArray( vertices, 3 * 3 );
  1237. }
  1238. else {
  1239. tempV2_3.toArray( vertices, 1 * 3 );
  1240. tempV2_3.toArray( vertices, 3 * 3 );
  1241. tempV2_4.toArray( vertices, 0 * 3 );
  1242. }
  1243. }
  1244. else {
  1245. tempV2_1.subVectors( p2, center );
  1246. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1247. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1248. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1249. var vl = vertices.length;
  1250. // Modify already existing vertices
  1251. if ( joinIsOnLeftSide ) {
  1252. tempV2_3.toArray( vertices, vl - 1 * 3 );
  1253. tempV2_4.toArray( vertices, vl - 2 * 3 );
  1254. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1255. }
  1256. else {
  1257. tempV2_3.toArray( vertices, vl - 2 * 3 );
  1258. tempV2_4.toArray( vertices, vl - 1 * 3 );
  1259. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1260. }
  1261. }
  1262. break;
  1263. case 'butt':
  1264. default:
  1265. // Nothing to do here
  1266. break;
  1267. }
  1268. }
  1269. function removeDuplicatedPoints( points ) {
  1270. // Creates a new array if necessary with duplicated points removed.
  1271. // This does not remove duplicated initial and ending points of a closed path.
  1272. var dupPoints = false;
  1273. for ( var i = 1, n = points.length - 1; i < n; i ++ ) {
  1274. if ( points[ i ].distanceTo( points[ i + 1 ] ) < minDistance ) {
  1275. dupPoints = true;
  1276. break;
  1277. }
  1278. }
  1279. if ( ! dupPoints ) return points;
  1280. var newPoints = [];
  1281. newPoints.push( points[ 0 ] );
  1282. for ( var i = 1, n = points.length - 1; i < n; i ++ ) {
  1283. if ( points[ i ].distanceTo( points[ i + 1 ] ) >= minDistance ) {
  1284. newPoints.push( points[ i ] );
  1285. }
  1286. }
  1287. newPoints.push( points[ points.length - 1 ] );
  1288. return newPoints;
  1289. }
  1290. };
  1291. }();