SVGLoader.js 45 KB

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