SVGLoader.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  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. }
  82. else {
  83. currentTransform.identity();
  84. }
  85. }
  86. }
  87. function parsePathNode( node, style ) {
  88. var path = new THREE.ShapePath();
  89. var point = new THREE.Vector2();
  90. var control = new THREE.Vector2();
  91. var firstPoint = new THREE.Vector2();
  92. var isFirstPoint = true;
  93. var doSetFirstPoint = false;
  94. var d = node.getAttribute( 'd' );
  95. // console.log( d );
  96. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  97. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  98. var command = commands[ i ];
  99. var type = command.charAt( 0 );
  100. var data = command.substr( 1 ).trim();
  101. if ( isFirstPoint === true ) {
  102. doSetFirstPoint = true;
  103. isFirstPoint = false;
  104. }
  105. switch ( type ) {
  106. case 'M':
  107. var numbers = parseFloats( data );
  108. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  109. point.x = numbers[ j + 0 ];
  110. point.y = numbers[ j + 1 ];
  111. control.x = point.x;
  112. control.y = point.y;
  113. if ( j === 0 ) {
  114. path.moveTo( point.x, point.y );
  115. } else {
  116. path.lineTo( point.x, point.y );
  117. }
  118. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  119. }
  120. break;
  121. case 'H':
  122. var numbers = parseFloats( data );
  123. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  124. point.x = numbers[ j ];
  125. control.x = point.x;
  126. control.y = point.y;
  127. path.lineTo( point.x, point.y );
  128. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  129. }
  130. break;
  131. case 'V':
  132. var numbers = parseFloats( data );
  133. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  134. point.y = numbers[ j ];
  135. control.x = point.x;
  136. control.y = point.y;
  137. path.lineTo( point.x, point.y );
  138. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  139. }
  140. break;
  141. case 'L':
  142. var numbers = parseFloats( data );
  143. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  144. point.x = numbers[ j + 0 ];
  145. point.y = numbers[ j + 1 ];
  146. control.x = point.x;
  147. control.y = point.y;
  148. path.lineTo( point.x, point.y );
  149. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  150. }
  151. break;
  152. case 'C':
  153. var numbers = parseFloats( data );
  154. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  155. path.bezierCurveTo(
  156. numbers[ j + 0 ],
  157. numbers[ j + 1 ],
  158. numbers[ j + 2 ],
  159. numbers[ j + 3 ],
  160. numbers[ j + 4 ],
  161. numbers[ j + 5 ]
  162. );
  163. control.x = numbers[ j + 2 ];
  164. control.y = numbers[ j + 3 ];
  165. point.x = numbers[ j + 4 ];
  166. point.y = numbers[ j + 5 ];
  167. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  168. }
  169. break;
  170. case 'S':
  171. var numbers = parseFloats( data );
  172. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  173. path.bezierCurveTo(
  174. getReflection( point.x, control.x ),
  175. getReflection( point.y, control.y ),
  176. numbers[ j + 0 ],
  177. numbers[ j + 1 ],
  178. numbers[ j + 2 ],
  179. numbers[ j + 3 ]
  180. );
  181. control.x = numbers[ j + 0 ];
  182. control.y = numbers[ j + 1 ];
  183. point.x = numbers[ j + 2 ];
  184. point.y = numbers[ j + 3 ];
  185. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  186. }
  187. break;
  188. case 'Q':
  189. var numbers = parseFloats( data );
  190. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  191. path.quadraticCurveTo(
  192. numbers[ j + 0 ],
  193. numbers[ j + 1 ],
  194. numbers[ j + 2 ],
  195. numbers[ j + 3 ]
  196. );
  197. control.x = numbers[ j + 0 ];
  198. control.y = numbers[ j + 1 ];
  199. point.x = numbers[ j + 2 ];
  200. point.y = numbers[ j + 3 ];
  201. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  202. }
  203. break;
  204. case 'T':
  205. var numbers = parseFloats( data );
  206. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  207. var rx = getReflection( point.x, control.x );
  208. var ry = getReflection( point.y, control.y );
  209. path.quadraticCurveTo(
  210. rx,
  211. ry,
  212. numbers[ j + 0 ],
  213. numbers[ j + 1 ]
  214. );
  215. control.x = rx;
  216. control.y = ry;
  217. point.x = numbers[ j + 0 ];
  218. point.y = numbers[ j + 1 ];
  219. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  220. }
  221. break;
  222. case 'A':
  223. var numbers = parseFloats( data );
  224. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  225. var start = point.clone();
  226. point.x = numbers[ j + 5 ];
  227. point.y = numbers[ j + 6 ];
  228. control.x = point.x;
  229. control.y = point.y;
  230. parseArcCommand(
  231. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  232. );
  233. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  234. }
  235. break;
  236. //
  237. case 'm':
  238. var numbers = parseFloats( data );
  239. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  240. point.x += numbers[ j + 0 ];
  241. point.y += numbers[ j + 1 ];
  242. control.x = point.x;
  243. control.y = point.y;
  244. if ( j === 0 ) {
  245. path.moveTo( point.x, point.y );
  246. } else {
  247. path.lineTo( point.x, point.y );
  248. }
  249. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  250. }
  251. break;
  252. case 'h':
  253. var numbers = parseFloats( data );
  254. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  255. point.x += numbers[ j ];
  256. control.x = point.x;
  257. control.y = point.y;
  258. path.lineTo( point.x, point.y );
  259. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  260. }
  261. break;
  262. case 'v':
  263. var numbers = parseFloats( data );
  264. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  265. point.y += numbers[ j ];
  266. control.x = point.x;
  267. control.y = point.y;
  268. path.lineTo( point.x, point.y );
  269. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  270. }
  271. break;
  272. case 'l':
  273. var numbers = parseFloats( data );
  274. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  275. point.x += numbers[ j + 0 ];
  276. point.y += numbers[ j + 1 ];
  277. control.x = point.x;
  278. control.y = point.y;
  279. path.lineTo( point.x, point.y );
  280. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  281. }
  282. break;
  283. case 'c':
  284. var numbers = parseFloats( data );
  285. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  286. path.bezierCurveTo(
  287. point.x + numbers[ j + 0 ],
  288. point.y + numbers[ j + 1 ],
  289. point.x + numbers[ j + 2 ],
  290. point.y + numbers[ j + 3 ],
  291. point.x + numbers[ j + 4 ],
  292. point.y + numbers[ j + 5 ]
  293. );
  294. control.x = point.x + numbers[ j + 2 ];
  295. control.y = point.y + numbers[ j + 3 ];
  296. point.x += numbers[ j + 4 ];
  297. point.y += numbers[ j + 5 ];
  298. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  299. }
  300. break;
  301. case 's':
  302. var numbers = parseFloats( data );
  303. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  304. path.bezierCurveTo(
  305. getReflection( point.x, control.x ),
  306. getReflection( point.y, control.y ),
  307. point.x + numbers[ j + 0 ],
  308. point.y + numbers[ j + 1 ],
  309. point.x + numbers[ j + 2 ],
  310. point.y + numbers[ j + 3 ]
  311. );
  312. control.x = point.x + numbers[ j + 0 ];
  313. control.y = point.y + numbers[ j + 1 ];
  314. point.x += numbers[ j + 2 ];
  315. point.y += numbers[ j + 3 ];
  316. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  317. }
  318. break;
  319. case 'q':
  320. var numbers = parseFloats( data );
  321. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  322. path.quadraticCurveTo(
  323. point.x + numbers[ j + 0 ],
  324. point.y + numbers[ j + 1 ],
  325. point.x + numbers[ j + 2 ],
  326. point.y + numbers[ j + 3 ]
  327. );
  328. control.x = point.x + numbers[ j + 0 ];
  329. control.y = point.y + numbers[ j + 1 ];
  330. point.x += numbers[ j + 2 ];
  331. point.y += numbers[ j + 3 ];
  332. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  333. }
  334. break;
  335. case 't':
  336. var numbers = parseFloats( data );
  337. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  338. var rx = getReflection( point.x, control.x );
  339. var ry = getReflection( point.y, control.y );
  340. path.quadraticCurveTo(
  341. rx,
  342. ry,
  343. point.x + numbers[ j + 0 ],
  344. point.y + numbers[ j + 1 ]
  345. );
  346. control.x = rx;
  347. control.y = ry;
  348. point.x = point.x + numbers[ j + 0 ];
  349. point.y = point.y + numbers[ j + 1 ];
  350. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  351. }
  352. break;
  353. case 'a':
  354. var numbers = parseFloats( data );
  355. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  356. var start = point.clone();
  357. point.x += numbers[ j + 5 ];
  358. point.y += numbers[ j + 6 ];
  359. control.x = point.x;
  360. control.y = point.y;
  361. parseArcCommand(
  362. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  363. );
  364. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  365. }
  366. break;
  367. //
  368. case 'Z':
  369. case 'z':
  370. path.currentPath.autoClose = true;
  371. if ( path.currentPath.curves.length > 0 ) {
  372. // Reset point to beginning of Path
  373. point.copy( firstPoint );
  374. path.currentPath.currentPoint.copy( point );
  375. isFirstPoint = true;
  376. }
  377. break;
  378. default:
  379. console.warn( command );
  380. }
  381. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  382. doSetFirstPoint = false;
  383. }
  384. return path;
  385. }
  386. /**
  387. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  388. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  389. * From
  390. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  391. * To
  392. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  393. */
  394. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  395. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  396. // Ensure radii are positive
  397. rx = Math.abs( rx );
  398. ry = Math.abs( ry );
  399. // Compute (x1′, y1′)
  400. var dx2 = ( start.x - end.x ) / 2.0;
  401. var dy2 = ( start.y - end.y ) / 2.0;
  402. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  403. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  404. // Compute (cx′, cy′)
  405. var rxs = rx * rx;
  406. var rys = ry * ry;
  407. var x1ps = x1p * x1p;
  408. var y1ps = y1p * y1p;
  409. // Ensure radii are large enough
  410. var cr = x1ps / rxs + y1ps / rys;
  411. if ( cr > 1 ) {
  412. // scale up rx,ry equally so cr == 1
  413. var s = Math.sqrt( cr );
  414. rx = s * rx;
  415. ry = s * ry;
  416. rxs = rx * rx;
  417. rys = ry * ry;
  418. }
  419. var dq = ( rxs * y1ps + rys * x1ps );
  420. var pq = ( rxs * rys - dq ) / dq;
  421. var q = Math.sqrt( Math.max( 0, pq ) );
  422. if ( large_arc_flag === sweep_flag ) q = - q;
  423. var cxp = q * rx * y1p / ry;
  424. var cyp = - q * ry * x1p / rx;
  425. // Step 3: Compute (cx, cy) from (cx′, cy′)
  426. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  427. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  428. // Step 4: Compute θ1 and Δθ
  429. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  430. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  431. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  432. }
  433. function svgAngle( ux, uy, vx, vy ) {
  434. var dot = ux * vx + uy * vy;
  435. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  436. var ang = Math.acos( Math.max( -1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  437. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  438. return ang;
  439. }
  440. /*
  441. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  442. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  443. */
  444. function parseRectNode( node, style ) {
  445. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  446. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  447. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  448. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  449. var w = parseFloat( node.getAttribute( 'width' ) );
  450. var h = parseFloat( node.getAttribute( 'height' ) );
  451. var path = new THREE.ShapePath();
  452. path.moveTo( x + 2 * rx, y );
  453. path.lineTo( x + w - 2 * rx, y );
  454. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  455. path.lineTo( x + w, y + h - 2 * ry );
  456. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  457. path.lineTo( x + 2 * rx, y + h );
  458. if ( rx !== 0 || ry !== 0 ) {
  459. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  460. }
  461. path.lineTo( x, y + 2 * ry );
  462. if ( rx !== 0 || ry !== 0 ) {
  463. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  464. }
  465. return path;
  466. }
  467. function parsePolygonNode( node, style ) {
  468. function iterator( match, a, b ) {
  469. var x = parseFloat( a );
  470. var y = parseFloat( b );
  471. if ( index === 0 ) {
  472. path.moveTo( x, y );
  473. } else {
  474. path.lineTo( x, y );
  475. }
  476. index ++;
  477. }
  478. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  479. var path = new THREE.ShapePath();
  480. var index = 0;
  481. node.getAttribute( 'points' ).replace(regex, iterator);
  482. path.currentPath.autoClose = true;
  483. return path;
  484. }
  485. function parsePolylineNode( node, style ) {
  486. function iterator( match, a, b ) {
  487. var x = parseFloat( a );
  488. var y = parseFloat( b );
  489. if ( index === 0 ) {
  490. path.moveTo( x, y );
  491. } else {
  492. path.lineTo( x, y );
  493. }
  494. index ++;
  495. }
  496. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  497. var path = new THREE.ShapePath();
  498. var index = 0;
  499. node.getAttribute( 'points' ).replace(regex, iterator);
  500. path.currentPath.autoClose = false;
  501. return path;
  502. }
  503. function parseCircleNode( node, style ) {
  504. var x = parseFloat( node.getAttribute( 'cx' ) );
  505. var y = parseFloat( node.getAttribute( 'cy' ) );
  506. var r = parseFloat( node.getAttribute( 'r' ) );
  507. var subpath = new THREE.Path();
  508. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  509. var path = new THREE.ShapePath();
  510. path.subPaths.push( subpath );
  511. return path;
  512. }
  513. function parseEllipseNode( node, style ) {
  514. var x = parseFloat( node.getAttribute( 'cx' ) );
  515. var y = parseFloat( node.getAttribute( 'cy' ) );
  516. var rx = parseFloat( node.getAttribute( 'rx' ) );
  517. var ry = parseFloat( node.getAttribute( 'ry' ) );
  518. var subpath = new THREE.Path();
  519. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  520. var path = new THREE.ShapePath();
  521. path.subPaths.push( subpath );
  522. return path;
  523. }
  524. function parseLineNode( node, style ) {
  525. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  526. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  527. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  528. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  529. var path = new THREE.ShapePath();
  530. path.moveTo( x1, y1 );
  531. path.lineTo( x2, y2 );
  532. path.currentPath.autoClose = false;
  533. return path;
  534. }
  535. //
  536. function parseStyle( node, style ) {
  537. style = Object.assign( {}, style ); // clone style
  538. function addStyle( svgName, jsName, adjustFunction ) {
  539. if ( adjustFunction === undefined ) adjustFunction = function copy( v ) { return v; };
  540. if ( node.hasAttribute( svgName ) ) style[ jsName ] = adjustFunction( node.getAttribute( svgName ) );
  541. if ( node.style[ svgName ] !== '' ) style[ jsName ] = adjustFunction( node.style[ svgName ] );
  542. }
  543. function clamp( v ) {
  544. return Math.max( 0, Math.min( 1, v ) );
  545. }
  546. function positive( v ) {
  547. return Math.max( 0, v );
  548. }
  549. addStyle( 'fill', 'fill' );
  550. addStyle( 'fill-opacity', 'fillOpacity', clamp );
  551. addStyle( 'stroke', 'stroke' );
  552. addStyle( 'stroke-opacity', 'strokeOpacity', clamp );
  553. addStyle( 'stroke-width', 'strokeWidth', positive );
  554. addStyle( 'stroke-linejoin', 'strokeLineJoin' );
  555. addStyle( 'stroke-linecap', 'strokeLineCap' );
  556. addStyle( 'stroke-miterlimit', 'strokeMiterLimit', positive );
  557. return style;
  558. }
  559. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  560. function getReflection( a, b ) {
  561. return a - ( b - a );
  562. }
  563. function parseFloats( string ) {
  564. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  565. for ( var i = 0; i < array.length; i ++ ) {
  566. var number = array[ i ];
  567. // Handle values like 48.6037.7.8
  568. // TODO Find a regex for this
  569. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  570. var split = number.split( '.' );
  571. for ( var s = 2; s < split.length; s ++ ) {
  572. array.splice( i + s - 1, 0, '0.' + split[ s ] );
  573. }
  574. }
  575. array[ i ] = parseFloat( number );
  576. }
  577. return array;
  578. }
  579. function getNodeTransform( node ) {
  580. if ( ! node.hasAttribute( 'transform' ) ) {
  581. return null;
  582. }
  583. var transform = parseNodeTransform( node );
  584. if ( transform ) {
  585. if ( transformStack.length > 0 ) {
  586. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  587. }
  588. currentTransform.copy( transform );
  589. transformStack.push( transform );
  590. }
  591. return transform;
  592. }
  593. function parseNodeTransform( node ) {
  594. var transform = new THREE.Matrix3();
  595. var currentTransform = tempTransform0;
  596. var transformsTexts = node.getAttribute( 'transform' ).split( ' ' );
  597. for ( var tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex -- ) {
  598. var transformText = transformsTexts[ tIndex ];
  599. var openParPos = transformText.indexOf( "(" );
  600. var closeParPos = transformText.indexOf( ")" );
  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. }();