babylon.polygonmesh.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. var PolygonPoints = (function () {
  18. function PolygonPoints() {
  19. this.elements = new Array();
  20. }
  21. PolygonPoints.prototype.add = function (originalPoints) {
  22. var _this = this;
  23. var result = new Array();
  24. originalPoints.forEach(function (point) {
  25. if (result.length === 0 || !(BABYLON.Tools.WithinEpsilon(point.x, result[0].x, 0.00001) && BABYLON.Tools.WithinEpsilon(point.y, result[0].y, 0.00001))) {
  26. var newPoint = new IndexedVector2(point, _this.elements.length);
  27. result.push(newPoint);
  28. _this.elements.push(newPoint);
  29. }
  30. });
  31. return result;
  32. };
  33. PolygonPoints.prototype.computeBounds = function () {
  34. var lmin = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
  35. var lmax = new BABYLON.Vector2(this.elements[0].x, this.elements[0].y);
  36. this.elements.forEach(function (point) {
  37. // x
  38. if (point.x < lmin.x) {
  39. lmin.x = point.x;
  40. }
  41. else if (point.x > lmax.x) {
  42. lmax.x = point.x;
  43. }
  44. // y
  45. if (point.y < lmin.y) {
  46. lmin.y = point.y;
  47. }
  48. else if (point.y > lmax.y) {
  49. lmax.y = point.y;
  50. }
  51. });
  52. return {
  53. min: lmin,
  54. max: lmax,
  55. width: lmax.x - lmin.x,
  56. height: lmax.y - lmin.y
  57. };
  58. };
  59. return PolygonPoints;
  60. })();
  61. var Polygon = (function () {
  62. function Polygon() {
  63. }
  64. Polygon.Rectangle = function (xmin, ymin, xmax, ymax) {
  65. return [
  66. new BABYLON.Vector2(xmin, ymin),
  67. new BABYLON.Vector2(xmax, ymin),
  68. new BABYLON.Vector2(xmax, ymax),
  69. new BABYLON.Vector2(xmin, ymax)
  70. ];
  71. };
  72. Polygon.Circle = function (radius, cx, cy, numberOfSides) {
  73. if (cx === void 0) { cx = 0; }
  74. if (cy === void 0) { cy = 0; }
  75. if (numberOfSides === void 0) { numberOfSides = 32; }
  76. var result = new Array();
  77. var angle = 0;
  78. var increment = (Math.PI * 2) / numberOfSides;
  79. for (var i = 0; i < numberOfSides; i++) {
  80. result.push(new BABYLON.Vector2(cx + Math.cos(angle) * radius, cy + Math.sin(angle) * radius));
  81. angle -= increment;
  82. }
  83. return result;
  84. };
  85. Polygon.Parse = function (input) {
  86. var floats = input.split(/[^-+eE\.\d]+/).map(parseFloat).filter(function (val) { return (!isNaN(val)); });
  87. var i, result = [];
  88. for (i = 0; i < (floats.length & 0x7FFFFFFE); i += 2) {
  89. result.push(new poly2tri.Point(floats[i], floats[i + 1]));
  90. }
  91. return result;
  92. };
  93. Polygon.StartingAt = function (x, y) {
  94. return BABYLON.Path2.StartingAt(x, y);
  95. };
  96. return Polygon;
  97. })();
  98. BABYLON.Polygon = Polygon;
  99. var PolygonMeshBuilder = (function () {
  100. function PolygonMeshBuilder(name, contours, scene) {
  101. this._points = new PolygonPoints();
  102. if (!("poly2tri" in window)) {
  103. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  104. }
  105. this._name = name;
  106. this._scene = scene;
  107. var points;
  108. if (contours instanceof BABYLON.Path2) {
  109. points = contours.getPoints();
  110. }
  111. else {
  112. points = contours;
  113. }
  114. this._swctx = new poly2tri.SweepContext(this._points.add(points));
  115. }
  116. PolygonMeshBuilder.prototype.addHole = function (hole) {
  117. this._swctx.addHole(this._points.add(hole));
  118. return this;
  119. };
  120. PolygonMeshBuilder.prototype.build = function (updatable) {
  121. if (updatable === void 0) { updatable = false; }
  122. var result = new BABYLON.Mesh(this._name, this._scene);
  123. var normals = [];
  124. var positions = [];
  125. var uvs = [];
  126. var bounds = this._points.computeBounds();
  127. this._points.elements.forEach(function (p) {
  128. normals.push(0, 1.0, 0);
  129. positions.push(p.x, 0, p.y);
  130. uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
  131. });
  132. var indices = [];
  133. this._swctx.triangulate();
  134. this._swctx.getTriangles().forEach(function (triangle) {
  135. triangle.getPoints().forEach(function (point) {
  136. indices.push(point.index);
  137. });
  138. });
  139. result.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
  140. result.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
  141. result.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
  142. result.setIndices(indices);
  143. return result;
  144. };
  145. return PolygonMeshBuilder;
  146. })();
  147. BABYLON.PolygonMeshBuilder = PolygonMeshBuilder;
  148. })(BABYLON || (BABYLON = {}));
  149. //# sourceMappingURL=babylon.polygonMesh.js.map