babylon.polygonmesh.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. var __extends = (this && 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 || !point.equalsWithEpsilon(result[0])) {
  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 BABYLON.Vector2(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. this._outlinepoints = new PolygonPoints();
  103. this._holes = [];
  104. if (!("poly2tri" in window)) {
  105. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  106. }
  107. this._name = name;
  108. this._scene = scene;
  109. var points;
  110. if (contours instanceof BABYLON.Path2) {
  111. points = contours.getPoints();
  112. }
  113. else {
  114. points = contours;
  115. }
  116. this._swctx = new poly2tri.SweepContext(this._points.add(points));
  117. this._outlinepoints.add(points);
  118. }
  119. PolygonMeshBuilder.prototype.addHole = function (hole) {
  120. this._swctx.addHole(this._points.add(hole));
  121. var holepoints = new PolygonPoints();
  122. holepoints.add(hole);
  123. this._holes.push(holepoints);
  124. return this;
  125. };
  126. PolygonMeshBuilder.prototype.build = function (updatable, depth) {
  127. var _this = this;
  128. if (updatable === void 0) { updatable = false; }
  129. var result = new BABYLON.Mesh(this._name, this._scene);
  130. var normals = [];
  131. var positions = [];
  132. var uvs = [];
  133. var bounds = this._points.computeBounds();
  134. this._points.elements.forEach(function (p) {
  135. normals.push(0, 1.0, 0);
  136. positions.push(p.x, 0, p.y);
  137. uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
  138. });
  139. var indices = [];
  140. this._swctx.triangulate();
  141. this._swctx.getTriangles().forEach(function (triangle) {
  142. triangle.getPoints().forEach(function (point) {
  143. indices.push(point.index);
  144. });
  145. });
  146. if (depth > 0) {
  147. var positionscount = (positions.length / 3); //get the current pointcount
  148. this._points.elements.forEach(function (p) {
  149. normals.push(0, -1.0, 0);
  150. positions.push(p.x, -depth, p.y);
  151. uvs.push(1 - (p.x - bounds.min.x) / bounds.width, 1 - (p.y - bounds.min.y) / bounds.height);
  152. });
  153. var p1; //we need to change order of point so the triangles are made in the rigth way.
  154. var p2;
  155. var poscounter = 0;
  156. this._swctx.getTriangles().forEach(function (triangle) {
  157. triangle.getPoints().forEach(function (point) {
  158. switch (poscounter) {
  159. case 0:
  160. p1 = point;
  161. break;
  162. case 1:
  163. p2 = point;
  164. break;
  165. case 2:
  166. indices.push(point.index + positionscount);
  167. indices.push(p2.index + positionscount);
  168. indices.push(p1.index + positionscount);
  169. poscounter = -1;
  170. break;
  171. }
  172. poscounter++;
  173. //indices.push((<IndexedVector2>point).index + positionscount);
  174. });
  175. });
  176. //Add the sides
  177. this.addSide(positions, normals, uvs, indices, bounds, this._outlinepoints, depth, false);
  178. this._holes.forEach(function (hole) {
  179. _this.addSide(positions, normals, uvs, indices, bounds, hole, depth, true);
  180. });
  181. }
  182. result.setVerticesData(positions, BABYLON.VertexBuffer.PositionKind, updatable);
  183. result.setVerticesData(normals, BABYLON.VertexBuffer.NormalKind, updatable);
  184. result.setVerticesData(uvs, BABYLON.VertexBuffer.UVKind, updatable);
  185. result.setIndices(indices);
  186. return result;
  187. };
  188. PolygonMeshBuilder.prototype.addSide = function (positions, normals, uvs, indices, bounds, points, depth, flip) {
  189. var StartIndex = positions.length / 3;
  190. var ulength = 0;
  191. for (var i = 0; i < points.elements.length; i++) {
  192. var p = points.elements[i];
  193. var p1;
  194. if ((i + 1) > points.elements.length - 1) {
  195. p1 = points.elements[0];
  196. }
  197. else {
  198. p1 = points.elements[i + 1];
  199. }
  200. positions.push(p.x, 0, p.y);
  201. positions.push(p.x, -depth, p.y);
  202. positions.push(p1.x, 0, p1.y);
  203. positions.push(p1.x, -depth, p1.y);
  204. var v1 = new BABYLON.Vector3(p.x, 0, p.y);
  205. var v2 = new BABYLON.Vector3(p1.x, 0, p1.y);
  206. var v3 = v2.subtract(v1);
  207. var v4 = new BABYLON.Vector3(0, 1, 0);
  208. var vn = BABYLON.Vector3.Cross(v3, v4);
  209. vn = vn.normalize();
  210. uvs.push(ulength / bounds.width, 0);
  211. uvs.push(ulength / bounds.width, 1);
  212. ulength += v3.length();
  213. uvs.push((ulength / bounds.width), 0);
  214. uvs.push((ulength / bounds.width), 1);
  215. if (!flip) {
  216. normals.push(-vn.x, -vn.y, -vn.z);
  217. normals.push(-vn.x, -vn.y, -vn.z);
  218. normals.push(-vn.x, -vn.y, -vn.z);
  219. normals.push(-vn.x, -vn.y, -vn.z);
  220. indices.push(StartIndex);
  221. indices.push(StartIndex + 1);
  222. indices.push(StartIndex + 2);
  223. indices.push(StartIndex + 1);
  224. indices.push(StartIndex + 3);
  225. indices.push(StartIndex + 2);
  226. }
  227. else {
  228. normals.push(vn.x, vn.y, vn.z);
  229. normals.push(vn.x, vn.y, vn.z);
  230. normals.push(vn.x, vn.y, vn.z);
  231. normals.push(vn.x, vn.y, vn.z);
  232. indices.push(StartIndex);
  233. indices.push(StartIndex + 2);
  234. indices.push(StartIndex + 1);
  235. indices.push(StartIndex + 1);
  236. indices.push(StartIndex + 2);
  237. indices.push(StartIndex + 3);
  238. }
  239. StartIndex += 4;
  240. }
  241. ;
  242. };
  243. return PolygonMeshBuilder;
  244. })();
  245. BABYLON.PolygonMeshBuilder = PolygonMeshBuilder;
  246. })(BABYLON || (BABYLON = {}));
  247. //# sourceMappingURL=babylon.polygonMesh.js.map