babylon.polygonmesh.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 || !point.equalsWithEpsilon(result[0])) {
  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 Vector2(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 _outlinepoints = new PolygonPoints();
  85. private _holes = [];
  86. private _name: string;
  87. private _scene: Scene;
  88. constructor(name: string, contours: Path2, scene: Scene)
  89. constructor(name: string, contours: Vector2[], scene: Scene)
  90. constructor(name: string, contours: any, scene: Scene) {
  91. if (!("poly2tri" in window)) {
  92. throw "PolygonMeshBuilder cannot be used because poly2tri is not referenced";
  93. }
  94. this._name = name;
  95. this._scene = scene;
  96. var points: Vector2[];
  97. if (contours instanceof Path2) {
  98. points = (<Path2>contours).getPoints();
  99. } else {
  100. points = (<Vector2[]>contours);
  101. }
  102. this._swctx = new poly2tri.SweepContext(this._points.add(points));
  103. this._outlinepoints.add(points)
  104. }
  105. addHole(hole: Vector2[]): PolygonMeshBuilder {
  106. this._swctx.addHole(this._points.add(hole));
  107. var holepoints = new PolygonPoints();
  108. holepoints.add(hole);
  109. this._holes.push(holepoints) ;
  110. return this;
  111. }
  112. build(updatable: boolean = false, depth?:number): 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. if (depth > 0) {
  131. var positionscount = (positions.length / 3); //get the current pointcount
  132. this._points.elements.forEach((p) => { //add the elements at the depth
  133. normals.push(0, -1.0, 0);
  134. positions.push(p.x, -depth, p.y);
  135. uvs.push(1-(p.x - bounds.min.x) / bounds.width,1-(p.y - bounds.min.y) / bounds.height);
  136. });
  137. var p1: IndexedVector2; //we need to change order of point so the triangles are made in the rigth way.
  138. var p2: IndexedVector2;
  139. var poscounter: number = 0;
  140. this._swctx.getTriangles().forEach((triangle) => {
  141. triangle.getPoints().forEach((point) => {
  142. switch (poscounter) {
  143. case 0:
  144. p1 = <IndexedVector2>point;
  145. break;
  146. case 1:
  147. p2 = <IndexedVector2>point;
  148. break;
  149. case 2:
  150. indices.push((<IndexedVector2>point).index + positionscount);
  151. indices.push(p2.index + positionscount);
  152. indices.push(p1.index + positionscount);
  153. poscounter = -1;
  154. break;
  155. }
  156. poscounter++;
  157. //indices.push((<IndexedVector2>point).index + positionscount);
  158. });
  159. });
  160. //Add the sides
  161. this.addSide(positions, normals, uvs, indices, bounds, this._outlinepoints, depth, false)
  162. this._holes.forEach((hole) => {
  163. this.addSide(positions, normals, uvs, indices, bounds, hole, depth, true)
  164. });
  165. }
  166. result.setVerticesData(VertexBuffer.PositionKind, positions, updatable);
  167. result.setVerticesData(VertexBuffer.NormalKind, normals, updatable);
  168. result.setVerticesData(VertexBuffer.UVKind, uvs, updatable);
  169. result.setIndices(indices);
  170. return result;
  171. }
  172. private addSide(positions: any[], normals: any[], uvs: any[], indices:any[],bounds: any, points: PolygonPoints, depth:number, flip:boolean ){
  173. var StartIndex: number = positions.length / 3;
  174. var ulength: number = 0;
  175. for (var i: number = 0; i < points.elements.length; i++) {
  176. var p: IndexedVector2 = points.elements[i];
  177. var p1: IndexedVector2
  178. if ((i + 1) > points.elements.length - 1) {
  179. p1 = points.elements[0];
  180. }
  181. else {
  182. p1 = points.elements[i + 1];
  183. }
  184. positions.push(p.x, 0, p.y);
  185. positions.push(p.x, -depth, p.y);
  186. positions.push(p1.x, 0, p1.y);
  187. positions.push(p1.x, -depth, p1.y);
  188. var v1 = new Vector3(p.x, 0, p.y);
  189. var v2 = new Vector3(p1.x, 0, p1.y);
  190. var v3 = v2.subtract(v1);
  191. var v4 = new Vector3(0, 1, 0);
  192. var vn = Vector3.Cross(v3, v4);
  193. vn = vn.normalize();
  194. uvs.push(ulength / bounds.width, 0);
  195. uvs.push(ulength / bounds.width, 1);
  196. ulength += v3.length();
  197. uvs.push((ulength / bounds.width), 0);
  198. uvs.push((ulength / bounds.width), 1);
  199. if (!flip) {
  200. normals.push(-vn.x,- vn.y, -vn.z);
  201. normals.push(-vn.x, -vn.y, -vn.z);
  202. normals.push(-vn.x, -vn.y, -vn.z);
  203. normals.push(-vn.x, -vn.y, -vn.z);
  204. indices.push(StartIndex);
  205. indices.push(StartIndex + 1);
  206. indices.push(StartIndex + 2);
  207. indices.push(StartIndex + 1);
  208. indices.push(StartIndex + 3);
  209. indices.push(StartIndex + 2);
  210. }
  211. else {
  212. normals.push(vn.x, vn.y, vn.z);
  213. normals.push(vn.x, vn.y, vn.z);
  214. normals.push(vn.x, vn.y, vn.z);
  215. normals.push(vn.x, vn.y, vn.z);
  216. indices.push(StartIndex);
  217. indices.push(StartIndex + 2);
  218. indices.push(StartIndex + 1);
  219. indices.push(StartIndex + 1);
  220. indices.push(StartIndex + 2);
  221. indices.push(StartIndex + 3);
  222. }
  223. StartIndex += 4;
  224. };
  225. }
  226. }
  227. }