Path.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. * Creates free form 2d path using series of points, lines or curves.
  4. *
  5. **/
  6. THREE.Path = function ( points ) {
  7. THREE.CurvePath.call(this);
  8. this.actions = [];
  9. if ( points ) {
  10. this.fromPoints( points );
  11. }
  12. };
  13. THREE.Path.prototype = Object.create( THREE.CurvePath.prototype );
  14. THREE.PathActions = {
  15. MOVE_TO: 'moveTo',
  16. LINE_TO: 'lineTo',
  17. QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
  18. BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve
  19. CSPLINE_THRU: 'splineThru', // Catmull-rom spline
  20. ARC: 'arc', // Circle
  21. ELLIPSE: 'ellipse'
  22. };
  23. // TODO Clean up PATH API
  24. // Create path using straight lines to connect all points
  25. // - vectors: array of Vector2
  26. THREE.Path.prototype.fromPoints = function ( vectors ) {
  27. this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y );
  28. for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) {
  29. this.lineTo( vectors[ v ].x, vectors[ v ].y );
  30. };
  31. };
  32. // startPath() endPath()?
  33. THREE.Path.prototype.moveTo = function ( x, y ) {
  34. var args = Array.prototype.slice.call( arguments );
  35. this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );
  36. };
  37. THREE.Path.prototype.lineTo = function ( x, y ) {
  38. var args = Array.prototype.slice.call( arguments );
  39. var lastargs = this.actions[ this.actions.length - 1 ].args;
  40. var x0 = lastargs[ lastargs.length - 2 ];
  41. var y0 = lastargs[ lastargs.length - 1 ];
  42. var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
  43. this.curves.push( curve );
  44. this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
  45. };
  46. THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {
  47. var args = Array.prototype.slice.call( arguments );
  48. var lastargs = this.actions[ this.actions.length - 1 ].args;
  49. var x0 = lastargs[ lastargs.length - 2 ];
  50. var y0 = lastargs[ lastargs.length - 1 ];
  51. var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
  52. new THREE.Vector2( aCPx, aCPy ),
  53. new THREE.Vector2( aX, aY ) );
  54. this.curves.push( curve );
  55. this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
  56. };
  57. THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
  58. aCP2x, aCP2y,
  59. aX, aY ) {
  60. var args = Array.prototype.slice.call( arguments );
  61. var lastargs = this.actions[ this.actions.length - 1 ].args;
  62. var x0 = lastargs[ lastargs.length - 2 ];
  63. var y0 = lastargs[ lastargs.length - 1 ];
  64. var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
  65. new THREE.Vector2( aCP1x, aCP1y ),
  66. new THREE.Vector2( aCP2x, aCP2y ),
  67. new THREE.Vector2( aX, aY ) );
  68. this.curves.push( curve );
  69. this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
  70. };
  71. THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
  72. var args = Array.prototype.slice.call( arguments );
  73. var lastargs = this.actions[ this.actions.length - 1 ].args;
  74. var x0 = lastargs[ lastargs.length - 2 ];
  75. var y0 = lastargs[ lastargs.length - 1 ];
  76. //---
  77. var npts = [ new THREE.Vector2( x0, y0 ) ];
  78. Array.prototype.push.apply( npts, pts );
  79. var curve = new THREE.SplineCurve( npts );
  80. this.curves.push( curve );
  81. this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } );
  82. };
  83. // FUTURE: Change the API or follow canvas API?
  84. THREE.Path.prototype.arc = function ( aX, aY, aRadius,
  85. aStartAngle, aEndAngle, aClockwise ) {
  86. var lastargs = this.actions[ this.actions.length - 1].args;
  87. var x0 = lastargs[ lastargs.length - 2 ];
  88. var y0 = lastargs[ lastargs.length - 1 ];
  89. this.absarc(aX + x0, aY + y0, aRadius,
  90. aStartAngle, aEndAngle, aClockwise );
  91. };
  92. THREE.Path.prototype.absarc = function ( aX, aY, aRadius,
  93. aStartAngle, aEndAngle, aClockwise ) {
  94. this.absellipse(aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise);
  95. };
  96. THREE.Path.prototype.ellipse = function ( aX, aY, xRadius, yRadius,
  97. aStartAngle, aEndAngle, aClockwise ) {
  98. var lastargs = this.actions[ this.actions.length - 1].args;
  99. var x0 = lastargs[ lastargs.length - 2 ];
  100. var y0 = lastargs[ lastargs.length - 1 ];
  101. this.absellipse(aX + x0, aY + y0, xRadius, yRadius,
  102. aStartAngle, aEndAngle, aClockwise );
  103. };
  104. THREE.Path.prototype.absellipse = function ( aX, aY, xRadius, yRadius,
  105. aStartAngle, aEndAngle, aClockwise ) {
  106. var args = Array.prototype.slice.call( arguments );
  107. var curve = new THREE.EllipseCurve( aX, aY, xRadius, yRadius,
  108. aStartAngle, aEndAngle, aClockwise );
  109. this.curves.push( curve );
  110. var lastPoint = curve.getPoint(1);
  111. args.push(lastPoint.x);
  112. args.push(lastPoint.y);
  113. this.actions.push( { action: THREE.PathActions.ELLIPSE, args: args } );
  114. };
  115. THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
  116. if ( ! divisions ) divisions = 40;
  117. var points = [];
  118. for ( var i = 0; i < divisions; i ++ ) {
  119. points.push( this.getPoint( i / divisions ) );
  120. //if( !this.getPoint( i / divisions ) ) throw "DIE";
  121. }
  122. // if ( closedPath ) {
  123. //
  124. // points.push( points[ 0 ] );
  125. //
  126. // }
  127. return points;
  128. };
  129. /* Return an array of vectors based on contour of the path */
  130. THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
  131. if (this.useSpacedPoints) {
  132. console.log('tata');
  133. return this.getSpacedPoints( divisions, closedPath );
  134. }
  135. divisions = divisions || 12;
  136. var points = [];
  137. var i, il, item, action, args;
  138. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  139. laste, j,
  140. t, tx, ty;
  141. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  142. item = this.actions[ i ];
  143. action = item.action;
  144. args = item.args;
  145. switch( action ) {
  146. case THREE.PathActions.MOVE_TO:
  147. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  148. break;
  149. case THREE.PathActions.LINE_TO:
  150. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  151. break;
  152. case THREE.PathActions.QUADRATIC_CURVE_TO:
  153. cpx = args[ 2 ];
  154. cpy = args[ 3 ];
  155. cpx1 = args[ 0 ];
  156. cpy1 = args[ 1 ];
  157. if ( points.length > 0 ) {
  158. laste = points[ points.length - 1 ];
  159. cpx0 = laste.x;
  160. cpy0 = laste.y;
  161. } else {
  162. laste = this.actions[ i - 1 ].args;
  163. cpx0 = laste[ laste.length - 2 ];
  164. cpy0 = laste[ laste.length - 1 ];
  165. }
  166. for ( j = 1; j <= divisions; j ++ ) {
  167. t = j / divisions;
  168. tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  169. ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  170. points.push( new THREE.Vector2( tx, ty ) );
  171. }
  172. break;
  173. case THREE.PathActions.BEZIER_CURVE_TO:
  174. cpx = args[ 4 ];
  175. cpy = args[ 5 ];
  176. cpx1 = args[ 0 ];
  177. cpy1 = args[ 1 ];
  178. cpx2 = args[ 2 ];
  179. cpy2 = args[ 3 ];
  180. if ( points.length > 0 ) {
  181. laste = points[ points.length - 1 ];
  182. cpx0 = laste.x;
  183. cpy0 = laste.y;
  184. } else {
  185. laste = this.actions[ i - 1 ].args;
  186. cpx0 = laste[ laste.length - 2 ];
  187. cpy0 = laste[ laste.length - 1 ];
  188. }
  189. for ( j = 1; j <= divisions; j ++ ) {
  190. t = j / divisions;
  191. tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  192. ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  193. points.push( new THREE.Vector2( tx, ty ) );
  194. }
  195. break;
  196. case THREE.PathActions.CSPLINE_THRU:
  197. laste = this.actions[ i - 1 ].args;
  198. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  199. var spts = [ last ];
  200. var n = divisions * args[ 0 ].length;
  201. spts = spts.concat( args[ 0 ] );
  202. var spline = new THREE.SplineCurve( spts );
  203. for ( j = 1; j <= n; j ++ ) {
  204. points.push( spline.getPointAt( j / n ) ) ;
  205. }
  206. break;
  207. case THREE.PathActions.ARC:
  208. var aX = args[ 0 ], aY = args[ 1 ],
  209. aRadius = args[ 2 ],
  210. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  211. aClockwise = !!args[ 5 ];
  212. var deltaAngle = aEndAngle - aStartAngle;
  213. var angle;
  214. var tdivisions = divisions * 2;
  215. for ( j = 1; j <= tdivisions; j ++ ) {
  216. t = j / tdivisions;
  217. if ( ! aClockwise ) {
  218. t = 1 - t;
  219. }
  220. angle = aStartAngle + t * deltaAngle;
  221. tx = aX + aRadius * Math.cos( angle );
  222. ty = aY + aRadius * Math.sin( angle );
  223. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  224. points.push( new THREE.Vector2( tx, ty ) );
  225. }
  226. //console.log(points);
  227. break;
  228. case THREE.PathActions.ELLIPSE:
  229. var aX = args[ 0 ], aY = args[ 1 ],
  230. xRadius = args[ 2 ],
  231. yRadius = args[ 3 ],
  232. aStartAngle = args[ 4 ], aEndAngle = args[ 5 ],
  233. aClockwise = !!args[ 6 ];
  234. var deltaAngle = aEndAngle - aStartAngle;
  235. var angle;
  236. var tdivisions = divisions * 2;
  237. for ( j = 1; j <= tdivisions; j ++ ) {
  238. t = j / tdivisions;
  239. if ( ! aClockwise ) {
  240. t = 1 - t;
  241. }
  242. angle = aStartAngle + t * deltaAngle;
  243. tx = aX + xRadius * Math.cos( angle );
  244. ty = aY + yRadius * Math.sin( angle );
  245. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  246. points.push( new THREE.Vector2( tx, ty ) );
  247. }
  248. //console.log(points);
  249. break;
  250. } // end switch
  251. }
  252. // Normalize to remove the closing point by default.
  253. var lastPoint = points[ points.length - 1];
  254. var EPSILON = 0.0000000001;
  255. if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
  256. Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
  257. points.splice( points.length - 1, 1);
  258. if ( closedPath ) {
  259. points.push( points[ 0 ] );
  260. }
  261. return points;
  262. };
  263. // Breaks path into shapes
  264. THREE.Path.prototype.toShapes = function( isCCW ) {
  265. function isPointInsidePolygon( inPt, inPolygon ) {
  266. var EPSILON = 0.0000000001;
  267. var polyLen = inPolygon.length;
  268. // inPt on polygon contour => immediate success or
  269. // toggling of inside/outside at every single! intersection point of an edge
  270. // with the horizontal line through inPt, left of inPt
  271. // not counting lowerY endpoints of edges and whole edges on that line
  272. var inside = false;
  273. for( var p = polyLen - 1, q = 0; q < polyLen; p = q++ ) {
  274. var edgeLowPt = inPolygon[ p ];
  275. var edgeHighPt = inPolygon[ q ];
  276. var edgeDx = edgeHighPt.x - edgeLowPt.x;
  277. var edgeDy = edgeHighPt.y - edgeLowPt.y;
  278. if ( Math.abs(edgeDy) > EPSILON ) { // not parallel
  279. if ( edgeDy < 0 ) {
  280. edgeLowPt = inPolygon[ q ]; edgeDx = -edgeDx;
  281. edgeHighPt = inPolygon[ p ]; edgeDy = -edgeDy;
  282. }
  283. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  284. if ( inPt.y == edgeLowPt.y ) {
  285. if ( inPt.x == edgeLowPt.x ) return true; // inPt is on contour ?
  286. // continue; // no intersection or edgeLowPt => doesn't count !!!
  287. } else {
  288. var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y);
  289. if ( perpEdge == 0 ) return true; // inPt is on contour ?
  290. if ( perpEdge < 0 ) continue;
  291. inside = !inside; // true intersection left of inPt
  292. }
  293. } else { // parallel or colinear
  294. if ( inPt.y != edgeLowPt.y ) continue; // parallel
  295. // egde lies on the same horizontal line as inPt
  296. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  297. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  298. // continue;
  299. }
  300. }
  301. return inside;
  302. }
  303. var i, il, item, action, args;
  304. var subPaths = [], lastPath = new THREE.Path();
  305. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  306. item = this.actions[ i ];
  307. args = item.args;
  308. action = item.action;
  309. if ( action == THREE.PathActions.MOVE_TO ) {
  310. if ( lastPath.actions.length != 0 ) {
  311. subPaths.push( lastPath );
  312. lastPath = new THREE.Path();
  313. }
  314. }
  315. lastPath[ action ].apply( lastPath, args );
  316. }
  317. if ( lastPath.actions.length != 0 ) {
  318. subPaths.push( lastPath );
  319. }
  320. // console.log(subPaths);
  321. if ( subPaths.length == 0 ) return [];
  322. var solid, tmpPath, tmpShape, shapes = [];
  323. if ( subPaths.length == 1) {
  324. tmpPath = subPaths[0];
  325. tmpShape = new THREE.Shape();
  326. tmpShape.actions = tmpPath.actions;
  327. tmpShape.curves = tmpPath.curves;
  328. shapes.push( tmpShape );
  329. return shapes;
  330. }
  331. var holesFirst = !THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  332. holesFirst = isCCW ? !holesFirst : holesFirst;
  333. // console.log("Holes first", holesFirst);
  334. var betterShapeHoles = [];
  335. var newShapes = [];
  336. var newShapeHoles = [];
  337. var mainIdx = 0;
  338. var tmpPoints;
  339. newShapes[mainIdx] = undefined;
  340. newShapeHoles[mainIdx] = [];
  341. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  342. tmpPath = subPaths[ i ];
  343. tmpPoints = tmpPath.getPoints();
  344. solid = THREE.Shape.Utils.isClockWise( tmpPoints );
  345. solid = isCCW ? !solid : solid;
  346. if ( solid ) {
  347. if ( (! holesFirst ) && ( newShapes[mainIdx] ) ) mainIdx++;
  348. newShapes[mainIdx] = { s: new THREE.Shape(), p: tmpPoints };
  349. newShapes[mainIdx].s.actions = tmpPath.actions;
  350. newShapes[mainIdx].s.curves = tmpPath.curves;
  351. if ( holesFirst ) mainIdx++;
  352. newShapeHoles[mainIdx] = [];
  353. //console.log('cw', i);
  354. } else {
  355. newShapeHoles[mainIdx].push( { h: tmpPath, p: tmpPoints[0] } );
  356. //console.log('ccw', i);
  357. }
  358. }
  359. if ( newShapes.length > 1 ) {
  360. var ambigious = false;
  361. var toChange = [];
  362. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++ ) {
  363. betterShapeHoles[sIdx] = [];
  364. }
  365. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx++ ) {
  366. var sh = newShapes[sIdx];
  367. var sho = newShapeHoles[sIdx];
  368. for (var hIdx = 0; hIdx < sho.length; hIdx++ ) {
  369. var ho = sho[hIdx];
  370. var hole_unassigned = true;
  371. for (var s2Idx = 0; s2Idx < newShapes.length; s2Idx++ ) {
  372. if ( isPointInsidePolygon( ho.p, newShapes[s2Idx].p ) ) {
  373. if ( sIdx != s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } );
  374. if ( hole_unassigned ) {
  375. hole_unassigned = false;
  376. betterShapeHoles[s2Idx].push( ho );
  377. } else {
  378. ambigious = true;
  379. }
  380. }
  381. }
  382. if ( hole_unassigned ) { betterShapeHoles[sIdx].push( ho ); }
  383. }
  384. }
  385. // console.log("ambigious: ", ambigious);
  386. if ( toChange.length > 0 ) {
  387. // console.log("to change: ", toChange);
  388. if (! ambigious) newShapeHoles = betterShapeHoles;
  389. }
  390. }
  391. var tmpHoles, j, jl;
  392. for ( i = 0, il = newShapes.length; i < il; i ++ ) {
  393. tmpShape = newShapes[i].s;
  394. shapes.push( tmpShape );
  395. tmpHoles = newShapeHoles[i];
  396. for ( j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  397. tmpShape.holes.push( tmpHoles[j].h );
  398. }
  399. }
  400. //console.log("shape", shapes);
  401. return shapes;
  402. };
粤ICP备19079148号