SVGLoader.js 45 KB

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