babylon.polygonmesh.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, 0.00001) && Tools.WithinEpsilon(point.y, result[0].y, 0.00001))) {
  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): Path2 {
  78. return Path2.StartingAt(x, y);
  79. }
  80. }
  81. export class PolygonMeshBuilder {
  82. private _swctx: poly2tri.SweepContext;
  83. private _points = new PolygonPoints();
  84. private _name: string;
  85. private _scene: Scene;
  86. constructor(name: string, contours: Path2, scene: Scene)
  87. constructor(name: string, contours: Vector2[], scene: Scene)
  88. constructor(name: string, contours: any, scene: Scene) {
  89. if (!("poly2tri" in window)) {
  90. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  91. }
  92. this._name = name;
  93. this._scene = scene;
  94. var points: Vector2[];
  95. if (contours instanceof Path2) {
  96. points = (<Path2>contours).getPoints();
  97. } else {
  98. points = (<Vector2[]>contours);
  99. }
  100. this._swctx = new poly2tri.SweepContext(this._points.add(points));
  101. }
  102. addHole(hole: Vector2[]): PolygonMeshBuilder {
  103. this._swctx.addHole(this._points.add(hole));
  104. return this;
  105. }
  106. build(updatable: boolean = false): Mesh {
  107. var result = new Mesh(this._name, this._scene);
  108. var normals = [];
  109. var positions = [];
  110. var uvs = [];
  111. var bounds = this._points.computeBounds();
  112. this._points.elements.forEach((p) => {
  113. normals.push(0, 1.0, 0);
  114. positions.push(p.x, 0, p.y);
  115. uvs.push((p.x - bounds.min.x) / bounds.width, (p.y - bounds.min.y) / bounds.height);
  116. });
  117. var indices = [];
  118. this._swctx.triangulate();
  119. this._swctx.getTriangles().forEach((triangle) => {
  120. triangle.getPoints().forEach((point) => {
  121. indices.push((<IndexedVector2>point).index);
  122. });
  123. });
  124. result.setVerticesData(VertexBuffer.PositionKind, positions, updatable);
  125. result.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
  126. result.setVerticesData(VertexBuffer.UVKind, uvs, updatable);
  127. result.setIndices(indices);
  128. return result;
  129. }
  130. }
  131. }