babylon.polygonmesh.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var IndexedVector2 = (function (_super) {
  10. __extends(IndexedVector2, _super);
  11. function IndexedVector2(original, index) {
  12. _super.call(this, original.x, original.y);
  13. this.index = index;
  14. }
  15. return IndexedVector2;
  16. })(BABYLON.Vector2);
  17. function nearlyEqual(a, b, epsilon) {
  18. if (epsilon === void 0) { epsilon = 0.0001; }
  19. if (a === b) {
  20. return true;
  21. }
  22. return Math.abs(a - b) < epsilon;
  23. }
  24. var PolygonPoints = (function () {
  25. function PolygonPoints() {
  26. this.elements = new Array();
  27. }
  28. PolygonPoints.prototype.add = function (originalPoints) {
  29. var _this = this;
  30. var result = new Array();
  31. originalPoints.forEach(function (point) {
  32. if (result.length == 0 || !(nearlyEqual(point.x, result[0].x) && nearlyEqual(point.y, result[0].y))) {
  33. var newPoint = new IndexedVector2(point, _this.elements.length);
  34. result.push(newPoint);
  35. _this.elements.push(newPoint);
  36. }
  37. });
  38. return result;
  39. };
  40. PolygonPoints.prototype.computeBounds = function () {
  41. var lmin = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
  42. var lmax = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
  43. this.elements.forEach(function (point) {
  44. // x
  45. if (point.x < lmin.x) {
  46. lmin.x = point.x;
  47. }
  48. else if (point.x > lmax.x) {
  49. lmax.x = point.x;
  50. }
  51. // y
  52. if (point.y < lmin.y) {
  53. lmin.y = point.y;
  54. }
  55. else if (point.y > lmax.y) {
  56. lmax.y = point.y;
  57. }
  58. });
  59. return {
  60. min: lmin,
  61. max: lmax,
  62. width: lmax.x - lmin.x,
  63. height: lmax.y - lmin.y
  64. };
  65. };
  66. return PolygonPoints;
  67. })();
  68. var Polygon = (function () {
  69. function Polygon() {
  70. }
  71. Polygon.Rectangle = function (xmin, ymin, xmax, ymax) {
  72. return [
  73. new BABYLON.Vector2(xmin, ymin),
  74. new BABYLON.Vector2(xmax, ymin),
  75. new BABYLON.Vector2(xmax, ymax),
  76. new BABYLON.Vector2(xmin, ymax)
  77. ];
  78. };
  79. Polygon.Circle = function (radius, cx, cy, numberOfSides) {
  80. if (cx === void 0) { cx = 0; }
  81. if (cy === void 0) { cy = 0; }
  82. if (numberOfSides === void 0) { numberOfSides = 32; }
  83. var result = new Array();
  84. var angle = 0;
  85. var increment = (Math.PI * 2) / numberOfSides;
  86. for (var i = 0; i < numberOfSides; i++) {
  87. result.push(new BABYLON.Vector2(cx + Math.cos(angle) * radius, cy + Math.sin(angle) * radius));
  88. angle -= increment;
  89. }
  90. return result;
  91. };
  92. Polygon.Parse = function (input) {
  93. var floats = input.split(/[^-+eE\.\d]+/).map(parseFloat).filter(function (val) { return (!isNaN(val)); });
  94. var i, result = [];
  95. for (i = 0; i < (floats.length & 0x7FFFFFFE); i += 2) {
  96. result.push(new poly2tri.Point(floats[i], floats[i + 1]));
  97. }
  98. return result;
  99. };
  100. Polygon.StartingAt = function (x, y) {
  101. return Path.StartingAt(x, y);
  102. };
  103. return Polygon;
  104. })();
  105. BABYLON.Polygon = Polygon;
  106. var Arc = (function () {
  107. function Arc(startPoint, midPoint, endPoint) {
  108. this.startPoint = startPoint;
  109. this.midPoint = midPoint;
  110. this.endPoint = endPoint;
  111. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  112. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  113. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  114. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  115. this.centerPoint = new BABYLON.Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
  116. this.radius = this.centerPoint.subtract(this.startPoint).length();
  117. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  118. var a1 = this.startAngle.degrees();
  119. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  120. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  121. // angles correction
  122. if (a2 - a1 > +180.0)
  123. a2 -= 360.0;
  124. if (a2 - a1 < -180.0)
  125. a2 += 360.0;
  126. if (a3 - a2 > +180.0)
  127. a3 -= 360.0;
  128. if (a3 - a2 < -180.0)
  129. a3 += 360.0;
  130. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  131. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  132. }
  133. return Arc;
  134. })();
  135. var Orientation;
  136. (function (Orientation) {
  137. Orientation[Orientation["CW"] = 0] = "CW";
  138. Orientation[Orientation["CCW"] = 1] = "CCW";
  139. })(Orientation || (Orientation = {}));
  140. var Angle = (function () {
  141. function Angle(radians) {
  142. var _this = this;
  143. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  144. this.radians = function () { return _this._radians; };
  145. this._radians = radians;
  146. if (this._radians < 0)
  147. this._radians += (2 * Math.PI);
  148. }
  149. Angle.BetweenTwoPoints = function (a, b) {
  150. var delta = b.subtract(a);
  151. var theta = Math.atan2(delta.y, delta.x);
  152. return new Angle(theta);
  153. };
  154. Angle.FromRadians = function (radians) {
  155. return new Angle(radians);
  156. };
  157. Angle.FromDegrees = function (degrees) {
  158. return new Angle(degrees * Math.PI / 180);
  159. };
  160. return Angle;
  161. })();
  162. var Path = (function () {
  163. function Path(x, y) {
  164. this._points = [];
  165. this._points.push(new BABYLON.Vector2(x, y));
  166. }
  167. Path.prototype.addLineTo = function (x, y) {
  168. this._points.push(new BABYLON.Vector2(x, y));
  169. return this;
  170. };
  171. Path.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  172. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  173. var startPoint = this._points[this._points.length - 1];
  174. var midPoint = new BABYLON.Vector2(midX, midY);
  175. var endPoint = new BABYLON.Vector2(endX, endY);
  176. var arc = new Arc(startPoint, midPoint, endPoint);
  177. var increment = arc.angle.radians() / numberOfSegments;
  178. if (arc.orientation === 0 /* CW */)
  179. increment *= -1;
  180. var currentAngle = arc.startAngle.radians() + increment;
  181. for (var i = 0; i < numberOfSegments; i++) {
  182. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  183. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  184. this.addLineTo(x, y);
  185. currentAngle += increment;
  186. }
  187. return this;
  188. };
  189. Path.prototype.close = function () {
  190. return this._points;
  191. };
  192. Path.StartingAt = function (x, y) {
  193. return new Path(x, y);
  194. };
  195. return Path;
  196. })();
  197. BABYLON.Path = Path;
  198. var PolygonMeshBuilder = (function () {
  199. function PolygonMeshBuilder(name, contours, scene) {
  200. this.name = name;
  201. this.scene = scene;
  202. this._points = new PolygonPoints();
  203. if (!("poly2tri" in window)) {
  204. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  205. }
  206. this._swctx = new poly2tri.SweepContext(this._points.add(contours));
  207. }
  208. PolygonMeshBuilder.prototype.addHole = function (hole) {
  209. this._swctx.addHole(this._points.add(hole));
  210. return this;
  211. };
  212. PolygonMeshBuilder.prototype.build = function (updatable) {
  213. if (updatable === void 0) { updatable = false; }
  214. var result = new BABYLON.Mesh(this.name, this.scene);
  215. var normals = [];
  216. var positions = [];
  217. var uvs = [];
  218. var bounds = this._points.computeBounds();
  219. this._points.elements.forEach(function (p) {
  220. normals.push(0, 1.0, 0);
  221. positions.push(p.x, 0, p.y);
  222. uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
  223. });
  224. var indices = [];
  225. this._swctx.triangulate();
  226. this._swctx.getTriangles().forEach(function (triangle) {
  227. triangle.getPoints().forEach(function (point) {
  228. indices.push(point.index);
  229. });
  230. });
  231. result.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
  232. result.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
  233. result.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
  234. result.setIndices(indices);
  235. return result;
  236. };
  237. return PolygonMeshBuilder;
  238. })();
  239. BABYLON.PolygonMeshBuilder = PolygonMeshBuilder;
  240. })(BABYLON || (BABYLON = {}));
  241. //# sourceMappingURL=babylon.polygonmesh.js.map