babylon.polygonmesh.ts 5.4 KB

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