babylon.polygonmesh.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. module BABYLON {
  2. class IndexedVector2 extends Vector2 {
  3. constructor(original: Vector2, public index: number) {
  4. super(original.x, original.y);
  5. }
  6. }
  7. class PolygonPoints {
  8. elements = new Array<IndexedVector2>();
  9. add(originalPoints: Array<Vector2>): Array<IndexedVector2> {
  10. var result = new Array<IndexedVector2>();
  11. originalPoints.forEach(point => {
  12. if (result.length == 0 || !(Tools.WithinEpsilon(point.x, result[0].x) && Tools.WithinEpsilon(point.y, result[0].y))) {
  13. var newPoint = new IndexedVector2(point, this.elements.length);
  14. result.push(newPoint);
  15. this.elements.push(newPoint);
  16. }
  17. });
  18. return result;
  19. }
  20. computeBounds(): { min: Vector2; max: Vector2; width: number; height: number } {
  21. var lmin = new Vector2(this.elements[0].x, this.elements[0].y);
  22. var lmax = new Vector2(this.elements[0].x, this.elements[0].y);
  23. this.elements.forEach(point => {
  24. // x
  25. if (point.x < lmin.x) {
  26. lmin.x = point.x;
  27. }
  28. else if (point.x > lmax.x) {
  29. lmax.x = point.x;
  30. }
  31. // y
  32. if (point.y < lmin.y) {
  33. lmin.y = point.y;
  34. }
  35. else if (point.y > lmax.y) {
  36. lmax.y = point.y;
  37. }
  38. });
  39. return {
  40. min: lmin,
  41. max: lmax,
  42. width: lmax.x - lmin.x,
  43. height: lmax.y - lmin.y
  44. };
  45. }
  46. }
  47. export class Polygon {
  48. static Rectangle(xmin: number, ymin: number, xmax: number, ymax: number): Vector2[] {
  49. return [
  50. new Vector2(xmin, ymin),
  51. new Vector2(xmax, ymin),
  52. new Vector2(xmax, ymax),
  53. new Vector2(xmin, ymax)
  54. ];
  55. }
  56. static Circle(radius: number, cx: number = 0, cy: number = 0, numberOfSides: number = 32): Vector2[] {
  57. var result = new Array<Vector2>();
  58. var angle = 0;
  59. var increment = (Math.PI * 2) / numberOfSides;
  60. for (var i = 0; i < numberOfSides; i++) {
  61. result.push(new Vector2(
  62. cx + Math.cos(angle) * radius,
  63. cy + Math.sin(angle) * radius
  64. ));
  65. angle -= increment;
  66. }
  67. return result;
  68. }
  69. static Parse(input: string): Vector2[] {
  70. var floats = input.split(/[^-+eE\.\d]+/).map(parseFloat).filter(val => (!isNaN(val)));
  71. var i: number, result = [];
  72. for (i = 0; i < (floats.length & 0x7FFFFFFE); i += 2) {
  73. result.push(new poly2tri.Point(floats[i], floats[i + 1]));
  74. }
  75. return result;
  76. }
  77. static StartingAt(x: number, y: number): Path {
  78. return Path.StartingAt(x, y);
  79. }
  80. }
  81. export class PolygonMeshBuilder {
  82. private _swctx: poly2tri.SweepContext;
  83. private _points = new PolygonPoints();
  84. constructor(private name: string, contours: Vector2[], private scene: Scene) {
  85. if (!("poly2tri" in window)) {
  86. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  87. }
  88. this._swctx = new poly2tri.SweepContext(this._points.add(contours));
  89. }
  90. addHole(hole: Vector2[]): PolygonMeshBuilder {
  91. this._swctx.addHole(this._points.add(hole));
  92. return this;
  93. }
  94. build(updatable: boolean = false): Mesh {
  95. var result = new Mesh(this.name, this.scene);
  96. var normals = [];
  97. var positions = [];
  98. var uvs = [];
  99. var bounds = this._points.computeBounds();
  100. this._points.elements.forEach((p) => {
  101. normals.push(0, 1.0, 0);
  102. positions.push(p.x, 0, p.y);
  103. uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
  104. });
  105. var indices = [];
  106. this._swctx.triangulate();
  107. this._swctx.getTriangles().forEach((triangle) => {
  108. triangle.getPoints().forEach((point) => {
  109. indices.push((<IndexedVector2>point).index);
  110. });
  111. });
  112. result.setVerticesData(positions, VertexBuffer.PositionKind, updatable);
  113. result.setVerticesData(normals, VertexBuffer.NormalKind, updatable);
  114. result.setVerticesData(uvs, VertexBuffer.UVKind, updatable);
  115. result.setIndices(indices);
  116. return result;
  117. }
  118. }
  119. }