babylon.polygonmesh.js 5.7 KB

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