SVGLoader.js 45 KB

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