babylon.mesh.vertexData.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. module BABYLON {
  2. export interface IGetSetVerticesData {
  3. isVerticesDataPresent(kind: string): boolean;
  4. getVerticesData(kind: string): number[];
  5. getIndices(): number[];
  6. setVerticesData(kind: string, data: number[], updatable?: boolean): void;
  7. updateVerticesData(kind: string, data: number[], updateExtends?: boolean, makeItUnique?: boolean): void;
  8. setIndices(indices: number[]): void;
  9. }
  10. export class VertexData {
  11. public positions: number[];
  12. public normals: number[];
  13. public uvs: number[];
  14. public uv2s: number[];
  15. public colors: number[];
  16. public matricesIndices: number[];
  17. public matricesWeights: number[];
  18. public indices: number[];
  19. public set(data: number[], kind: string) {
  20. switch (kind) {
  21. case BABYLON.VertexBuffer.PositionKind:
  22. this.positions = data;
  23. break;
  24. case BABYLON.VertexBuffer.NormalKind:
  25. this.normals = data;
  26. break;
  27. case BABYLON.VertexBuffer.UVKind:
  28. this.uvs = data;
  29. break;
  30. case BABYLON.VertexBuffer.UV2Kind:
  31. this.uv2s = data;
  32. break;
  33. case BABYLON.VertexBuffer.ColorKind:
  34. this.colors = data;
  35. break;
  36. case BABYLON.VertexBuffer.MatricesIndicesKind:
  37. this.matricesIndices = data;
  38. break;
  39. case BABYLON.VertexBuffer.MatricesWeightsKind:
  40. this.matricesWeights = data;
  41. break;
  42. }
  43. }
  44. public applyToMesh(mesh: Mesh, updatable?: boolean): void {
  45. this._applyTo(mesh, updatable);
  46. }
  47. public applyToGeometry(geometry: Geometry, updatable?: boolean): void {
  48. this._applyTo(geometry, updatable);
  49. }
  50. public updateMesh(mesh: Mesh, updateExtends?: boolean, makeItUnique?: boolean): void {
  51. this._update(mesh);
  52. }
  53. public updateGeometry(geometry: Geometry, updateExtends?: boolean, makeItUnique?: boolean): void {
  54. this._update(geometry);
  55. }
  56. private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean) {
  57. if (this.positions) {
  58. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updatable);
  59. }
  60. if (this.normals) {
  61. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updatable);
  62. }
  63. if (this.uvs) {
  64. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updatable);
  65. }
  66. if (this.uv2s) {
  67. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uv2s, updatable);
  68. }
  69. if (this.colors) {
  70. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updatable);
  71. }
  72. if (this.matricesIndices) {
  73. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  74. }
  75. if (this.matricesWeights) {
  76. meshOrGeometry.setVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  77. }
  78. if (this.indices) {
  79. meshOrGeometry.setIndices(this.indices);
  80. }
  81. }
  82. private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean) {
  83. if (this.positions) {
  84. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  85. }
  86. if (this.normals) {
  87. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  88. }
  89. if (this.uvs) {
  90. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  91. }
  92. if (this.uv2s) {
  93. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.UV2Kind, this.uv2s, updateExtends, makeItUnique);
  94. }
  95. if (this.colors) {
  96. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  97. }
  98. if (this.matricesIndices) {
  99. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  100. }
  101. if (this.matricesWeights) {
  102. meshOrGeometry.updateVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  103. }
  104. if (this.indices) {
  105. meshOrGeometry.setIndices(this.indices);
  106. }
  107. }
  108. public transform(matrix: Matrix): void {
  109. var transformed = BABYLON.Vector3.Zero();
  110. if (this.positions) {
  111. var position = BABYLON.Vector3.Zero();
  112. for (var index = 0; index < this.positions.length; index += 3) {
  113. BABYLON.Vector3.FromArrayToRef(this.positions, index, position);
  114. BABYLON.Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  115. this.positions[index] = transformed.x;
  116. this.positions[index + 1] = transformed.y;
  117. this.positions[index + 2] = transformed.z;
  118. }
  119. }
  120. if (this.normals) {
  121. var normal = BABYLON.Vector3.Zero();
  122. for (index = 0; index < this.normals.length; index += 3) {
  123. BABYLON.Vector3.FromArrayToRef(this.normals, index, normal);
  124. BABYLON.Vector3.TransformNormalToRef(normal, matrix, transformed);
  125. this.normals[index] = transformed.x;
  126. this.normals[index + 1] = transformed.y;
  127. this.normals[index + 2] = transformed.z;
  128. }
  129. }
  130. }
  131. public merge(other: VertexData): void {
  132. if (other.indices) {
  133. if (!this.indices) {
  134. this.indices = [];
  135. }
  136. var offset = this.positions ? this.positions.length / 3 : 0;
  137. for (var index = 0; index < other.indices.length; index++) {
  138. this.indices.push(other.indices[index] + offset);
  139. }
  140. }
  141. if (other.positions) {
  142. if (!this.positions) {
  143. this.positions = [];
  144. }
  145. for (index = 0; index < other.positions.length; index++) {
  146. this.positions.push(other.positions[index]);
  147. }
  148. }
  149. if (other.normals) {
  150. if (!this.normals) {
  151. this.normals = [];
  152. }
  153. for (index = 0; index < other.normals.length; index++) {
  154. this.normals.push(other.normals[index]);
  155. }
  156. }
  157. if (other.uvs) {
  158. if (!this.uvs) {
  159. this.uvs = [];
  160. }
  161. for (index = 0; index < other.uvs.length; index++) {
  162. this.uvs.push(other.uvs[index]);
  163. }
  164. }
  165. if (other.uv2s) {
  166. if (!this.uv2s) {
  167. this.uv2s = [];
  168. }
  169. for (index = 0; index < other.uv2s.length; index++) {
  170. this.uv2s.push(other.uv2s[index]);
  171. }
  172. }
  173. if (other.matricesIndices) {
  174. if (!this.matricesIndices) {
  175. this.matricesIndices = [];
  176. }
  177. for (index = 0; index < other.matricesIndices.length; index++) {
  178. this.matricesIndices.push(other.matricesIndices[index]);
  179. }
  180. }
  181. if (other.matricesWeights) {
  182. if (!this.matricesWeights) {
  183. this.matricesWeights = [];
  184. }
  185. for (index = 0; index < other.matricesWeights.length; index++) {
  186. this.matricesWeights.push(other.matricesWeights[index]);
  187. }
  188. }
  189. if (other.colors) {
  190. if (!this.colors) {
  191. this.colors = [];
  192. }
  193. for (index = 0; index < other.colors.length; index++) {
  194. this.colors.push(other.colors[index]);
  195. }
  196. }
  197. }
  198. // Statics
  199. public static ExtractFromMesh(mesh: Mesh): VertexData {
  200. return VertexData._ExtractFrom(mesh);
  201. }
  202. public static ExtractFromGeometry(geometry: Geometry): VertexData {
  203. return VertexData._ExtractFrom(geometry);
  204. }
  205. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData): VertexData {
  206. var result = new BABYLON.VertexData();
  207. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  208. result.positions = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  209. }
  210. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  211. result.normals = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  212. }
  213. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  214. result.uvs = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UVKind);
  215. }
  216. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  217. result.uv2s = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.UV2Kind);
  218. }
  219. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  220. result.colors = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  221. }
  222. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind)) {
  223. result.matricesIndices = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesIndicesKind);
  224. }
  225. if (meshOrGeometry.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  226. result.matricesWeights = meshOrGeometry.getVerticesData(BABYLON.VertexBuffer.MatricesWeightsKind);
  227. }
  228. result.indices = meshOrGeometry.getIndices();
  229. return result;
  230. }
  231. public static CreateBox(size: number): VertexData {
  232. var normalsSource = [
  233. new BABYLON.Vector3(0, 0, 1),
  234. new BABYLON.Vector3(0, 0, -1),
  235. new BABYLON.Vector3(1, 0, 0),
  236. new BABYLON.Vector3(-1, 0, 0),
  237. new BABYLON.Vector3(0, 1, 0),
  238. new BABYLON.Vector3(0, -1, 0)
  239. ];
  240. var indices = [];
  241. var positions = [];
  242. var normals = [];
  243. var uvs = [];
  244. size = size || 1;
  245. // Create each face in turn.
  246. for (var index = 0; index < normalsSource.length; index++) {
  247. var normal = normalsSource[index];
  248. // Get two vectors perpendicular to the face normal and to each other.
  249. var side1 = new BABYLON.Vector3(normal.y, normal.z, normal.x);
  250. var side2 = BABYLON.Vector3.Cross(normal, side1);
  251. // Six indices (two triangles) per face.
  252. var verticesLength = positions.length / 3;
  253. indices.push(verticesLength);
  254. indices.push(verticesLength + 1);
  255. indices.push(verticesLength + 2);
  256. indices.push(verticesLength);
  257. indices.push(verticesLength + 2);
  258. indices.push(verticesLength + 3);
  259. // Four vertices per face.
  260. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  261. positions.push(vertex.x, vertex.y, vertex.z);
  262. normals.push(normal.x, normal.y, normal.z);
  263. uvs.push(1.0, 1.0);
  264. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  265. positions.push(vertex.x, vertex.y, vertex.z);
  266. normals.push(normal.x, normal.y, normal.z);
  267. uvs.push(0.0, 1.0);
  268. vertex = normal.add(side1).add(side2).scale(size / 2);
  269. positions.push(vertex.x, vertex.y, vertex.z);
  270. normals.push(normal.x, normal.y, normal.z);
  271. uvs.push(0.0, 0.0);
  272. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  273. positions.push(vertex.x, vertex.y, vertex.z);
  274. normals.push(normal.x, normal.y, normal.z);
  275. uvs.push(1.0, 0.0);
  276. }
  277. // Result
  278. var vertexData = new BABYLON.VertexData();
  279. vertexData.indices = indices;
  280. vertexData.positions = positions;
  281. vertexData.normals = normals;
  282. vertexData.uvs = uvs;
  283. return vertexData;
  284. }
  285. public static CreateSphere(segments: number, diameter: number): VertexData {
  286. segments = segments || 32;
  287. diameter = diameter || 1;
  288. var radius = diameter / 2;
  289. var totalZRotationSteps = 2 + segments;
  290. var totalYRotationSteps = 2 * totalZRotationSteps;
  291. var indices = [];
  292. var positions = [];
  293. var normals = [];
  294. var uvs = [];
  295. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  296. var normalizedZ = zRotationStep / totalZRotationSteps;
  297. var angleZ = (normalizedZ * Math.PI);
  298. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  299. var normalizedY = yRotationStep / totalYRotationSteps;
  300. var angleY = normalizedY * Math.PI * 2;
  301. var rotationZ = BABYLON.Matrix.RotationZ(-angleZ);
  302. var rotationY = BABYLON.Matrix.RotationY(angleY);
  303. var afterRotZ = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Up(), rotationZ);
  304. var complete = BABYLON.Vector3.TransformCoordinates(afterRotZ, rotationY);
  305. var vertex = complete.scale(radius);
  306. var normal = BABYLON.Vector3.Normalize(vertex);
  307. positions.push(vertex.x, vertex.y, vertex.z);
  308. normals.push(normal.x, normal.y, normal.z);
  309. uvs.push(normalizedZ, normalizedY);
  310. }
  311. if (zRotationStep > 0) {
  312. var verticesCount = positions.length / 3;
  313. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  314. indices.push((firstIndex));
  315. indices.push((firstIndex + 1));
  316. indices.push(firstIndex + totalYRotationSteps + 1);
  317. indices.push((firstIndex + totalYRotationSteps + 1));
  318. indices.push((firstIndex + 1));
  319. indices.push((firstIndex + totalYRotationSteps + 2));
  320. }
  321. }
  322. }
  323. // Result
  324. var vertexData = new BABYLON.VertexData();
  325. vertexData.indices = indices;
  326. vertexData.positions = positions;
  327. vertexData.normals = normals;
  328. vertexData.uvs = uvs;
  329. return vertexData;
  330. }
  331. public static CreateCylinder(height: number, diameterTop: number, diameterBottom: number, tessellation: number, subdivisions: number = 1): VertexData {
  332. var radiusTop = diameterTop / 2;
  333. var radiusBottom = diameterBottom / 2;
  334. var indices = [];
  335. var positions = [];
  336. var normals = [];
  337. var uvs = [];
  338. height = height || 1;
  339. diameterTop = diameterTop || 0.5;
  340. diameterBottom = diameterBottom || 1;
  341. tessellation = tessellation || 16;
  342. subdivisions = subdivisions || 1;
  343. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  344. var getCircleVector = i => {
  345. var angle = (i * 2.0 * Math.PI / tessellation);
  346. var dx = Math.cos(angle);
  347. var dz = Math.sin(angle);
  348. return new BABYLON.Vector3(dx, 0, dz);
  349. };
  350. var createCylinderCap = isTop => {
  351. var radius = isTop ? radiusTop : radiusBottom;
  352. if (radius == 0) {
  353. return;
  354. }
  355. var vbase = positions.length / 3;
  356. var offset = new BABYLON.Vector3(0, height / 2, 0);
  357. var textureScale = new BABYLON.Vector2(0.5, 0.5);
  358. if (!isTop) {
  359. offset.scaleInPlace(-1);
  360. textureScale.x = -textureScale.x;
  361. }
  362. // Positions, normals & uvs
  363. for (i = 0; i < tessellation; i++) {
  364. var circleVector = getCircleVector(i);
  365. var position = circleVector.scale(radius).add(offset);
  366. var textureCoordinate = new BABYLON.Vector2(
  367. circleVector.x * textureScale.x + 0.5,
  368. circleVector.z * textureScale.y + 0.5
  369. );
  370. positions.push(position.x, position.y, position.z);
  371. uvs.push(textureCoordinate.x, textureCoordinate.y);
  372. }
  373. // Indices
  374. for (var i = 0; i < tessellation - 2; i++) {
  375. if (!isTop) {
  376. indices.push(vbase);
  377. indices.push(vbase + (i + 2) % tessellation);
  378. indices.push(vbase + (i + 1) % tessellation);
  379. } else {
  380. indices.push(vbase);
  381. indices.push(vbase + (i + 1) % tessellation);
  382. indices.push(vbase + (i + 2) % tessellation);
  383. }
  384. }
  385. };
  386. var base = new BABYLON.Vector3(0, -1, 0).scale(height / 2);
  387. var offset = new BABYLON.Vector3(0, 1, 0).scale(height / subdivisions);
  388. var stride = tessellation + 1;
  389. // Positions, normals & uvs
  390. for (var i = 0; i <= tessellation; i++) {
  391. var circleVector = getCircleVector(i);
  392. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  393. var position, radius = radiusBottom;
  394. for (var s = 0; s <= subdivisions; s++) {
  395. // Update variables
  396. position = circleVector.scale(radius);
  397. position.addInPlace(base.add(offset.scale(s)));
  398. textureCoordinate.y += 1 / subdivisions;
  399. radius += (radiusTop - radiusBottom) / subdivisions;
  400. // Push in arrays
  401. positions.push(position.x, position.y, position.z);
  402. uvs.push(textureCoordinate.x, textureCoordinate.y);
  403. }
  404. }
  405. subdivisions += 1;
  406. // Indices
  407. for (var s = 0; s < subdivisions - 1; s++) {
  408. for (var i = 0; i <= tessellation; i++) {
  409. indices.push(i * subdivisions + s);
  410. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  411. indices.push(i * subdivisions + (s + 1));
  412. indices.push(i * subdivisions + (s + 1));
  413. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  414. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  415. }
  416. }
  417. // Create flat triangle fan caps to seal the top and bottom.
  418. createCylinderCap(true);
  419. createCylinderCap(false);
  420. // Normals
  421. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  422. // Result
  423. var vertexData = new BABYLON.VertexData();
  424. vertexData.indices = indices;
  425. vertexData.positions = positions;
  426. vertexData.normals = normals;
  427. vertexData.uvs = uvs;
  428. return vertexData;
  429. }
  430. public static CreateTorus(diameter, thickness, tessellation) {
  431. var indices = [];
  432. var positions = [];
  433. var normals = [];
  434. var uvs = [];
  435. diameter = diameter || 1;
  436. thickness = thickness || 0.5;
  437. tessellation = tessellation || 16;
  438. var stride = tessellation + 1;
  439. for (var i = 0; i <= tessellation; i++) {
  440. var u = i / tessellation;
  441. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  442. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  443. for (var j = 0; j <= tessellation; j++) {
  444. var v = 1 - j / tessellation;
  445. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  446. var dx = Math.cos(innerAngle);
  447. var dy = Math.sin(innerAngle);
  448. // Create a vertex.
  449. var normal = new BABYLON.Vector3(dx, dy, 0);
  450. var position = normal.scale(thickness / 2);
  451. var textureCoordinate = new BABYLON.Vector2(u, v);
  452. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  453. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  454. positions.push(position.x, position.y, position.z);
  455. normals.push(normal.x, normal.y, normal.z);
  456. uvs.push(textureCoordinate.x, textureCoordinate.y);
  457. // And create indices for two triangles.
  458. var nextI = (i + 1) % stride;
  459. var nextJ = (j + 1) % stride;
  460. indices.push(i * stride + j);
  461. indices.push(i * stride + nextJ);
  462. indices.push(nextI * stride + j);
  463. indices.push(i * stride + nextJ);
  464. indices.push(nextI * stride + nextJ);
  465. indices.push(nextI * stride + j);
  466. }
  467. }
  468. // Result
  469. var vertexData = new BABYLON.VertexData();
  470. vertexData.indices = indices;
  471. vertexData.positions = positions;
  472. vertexData.normals = normals;
  473. vertexData.uvs = uvs;
  474. return vertexData;
  475. }
  476. public static CreateLines(points: Vector3[]): VertexData {
  477. var indices = [];
  478. var positions = [];
  479. for (var index = 0; index < points.length; index++) {
  480. positions.push(points[index].x, points[index].y, points[index].z);
  481. if (index > 0) {
  482. indices.push(index - 1);
  483. indices.push(index);
  484. }
  485. }
  486. // Result
  487. var vertexData = new BABYLON.VertexData();
  488. vertexData.indices = indices;
  489. vertexData.positions = positions;
  490. return vertexData;
  491. }
  492. public static CreateGround(width: number, height: number, subdivisions: number): VertexData {
  493. var indices = [];
  494. var positions = [];
  495. var normals = [];
  496. var uvs = [];
  497. var row, col;
  498. width = width || 1;
  499. height = height || 1;
  500. subdivisions = subdivisions || 1;
  501. for (row = 0; row <= subdivisions; row++) {
  502. for (col = 0; col <= subdivisions; col++) {
  503. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  504. var normal = new BABYLON.Vector3(0, 1.0, 0);
  505. positions.push(position.x, position.y, position.z);
  506. normals.push(normal.x, normal.y, normal.z);
  507. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  508. }
  509. }
  510. for (row = 0; row < subdivisions; row++) {
  511. for (col = 0; col < subdivisions; col++) {
  512. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  513. indices.push(col + 1 + row * (subdivisions + 1));
  514. indices.push(col + row * (subdivisions + 1));
  515. indices.push(col + (row + 1) * (subdivisions + 1));
  516. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  517. indices.push(col + row * (subdivisions + 1));
  518. }
  519. }
  520. // Result
  521. var vertexData = new BABYLON.VertexData();
  522. vertexData.indices = indices;
  523. vertexData.positions = positions;
  524. vertexData.normals = normals;
  525. vertexData.uvs = uvs;
  526. return vertexData;
  527. }
  528. public static CreateTiledGround(xmin: number, zmin: number, xmax: number, zmax: number, subdivisions = { w: 1, h: 1 }, precision = { w: 1, h: 1 }): VertexData {
  529. var indices = [];
  530. var positions = [];
  531. var normals = [];
  532. var uvs = [];
  533. var row, col, tileRow, tileCol;
  534. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  535. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  536. precision.w = (precision.w < 1) ? 1 : precision.w;
  537. precision.h = (precision.h < 1) ? 1 : precision.h;
  538. var tileSize = {
  539. 'w': (xmax - xmin) / subdivisions.w,
  540. 'h': (zmax - zmin) / subdivisions.h
  541. };
  542. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  543. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  544. applyTile(
  545. xmin + tileCol * tileSize.w,
  546. zmin + tileRow * tileSize.h,
  547. xmin + (tileCol + 1) * tileSize.w,
  548. zmin + (tileRow + 1) * tileSize.h
  549. );
  550. }
  551. }
  552. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  553. // Indices
  554. var base = positions.length / 3;
  555. var rowLength = precision.w + 1;
  556. for (row = 0; row < precision.h; row++) {
  557. for (col = 0; col < precision.w; col++) {
  558. var square = [
  559. base + col + row * rowLength,
  560. base + (col + 1) + row * rowLength,
  561. base + (col + 1) + (row + 1) * rowLength,
  562. base + col + (row + 1) * rowLength
  563. ];
  564. indices.push(square[1]);
  565. indices.push(square[2]);
  566. indices.push(square[3]);
  567. indices.push(square[0]);
  568. indices.push(square[1]);
  569. indices.push(square[3]);
  570. }
  571. }
  572. // Position, normals and uvs
  573. var position = BABYLON.Vector3.Zero();
  574. var normal = new BABYLON.Vector3(0, 1.0, 0);
  575. for (row = 0; row <= precision.h; row++) {
  576. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  577. for (col = 0; col <= precision.w; col++) {
  578. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  579. position.y = 0;
  580. positions.push(position.x, position.y, position.z);
  581. normals.push(normal.x, normal.y, normal.z);
  582. uvs.push(col / precision.w, row / precision.h);
  583. }
  584. }
  585. }
  586. // Result
  587. var vertexData = new BABYLON.VertexData();
  588. vertexData.indices = indices;
  589. vertexData.positions = positions;
  590. vertexData.normals = normals;
  591. vertexData.uvs = uvs;
  592. return vertexData;
  593. }
  594. public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number): VertexData {
  595. var indices = [];
  596. var positions = [];
  597. var normals = [];
  598. var uvs = [];
  599. var row, col;
  600. // Vertices
  601. for (row = 0; row <= subdivisions; row++) {
  602. for (col = 0; col <= subdivisions; col++) {
  603. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  604. // Compute height
  605. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  606. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  607. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  608. var r = buffer[pos] / 255.0;
  609. var g = buffer[pos + 1] / 255.0;
  610. var b = buffer[pos + 2] / 255.0;
  611. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  612. position.y = minHeight + (maxHeight - minHeight) * gradient;
  613. // Add vertex
  614. positions.push(position.x, position.y, position.z);
  615. normals.push(0, 0, 0);
  616. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  617. }
  618. }
  619. // Indices
  620. for (row = 0; row < subdivisions; row++) {
  621. for (col = 0; col < subdivisions; col++) {
  622. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  623. indices.push(col + 1 + row * (subdivisions + 1));
  624. indices.push(col + row * (subdivisions + 1));
  625. indices.push(col + (row + 1) * (subdivisions + 1));
  626. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  627. indices.push(col + row * (subdivisions + 1));
  628. }
  629. }
  630. // Normals
  631. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  632. // Result
  633. var vertexData = new BABYLON.VertexData();
  634. vertexData.indices = indices;
  635. vertexData.positions = positions;
  636. vertexData.normals = normals;
  637. vertexData.uvs = uvs;
  638. return vertexData;
  639. }
  640. public static CreatePlane(size: number): VertexData {
  641. var indices = [];
  642. var positions = [];
  643. var normals = [];
  644. var uvs = [];
  645. size = size || 1;
  646. // Vertices
  647. var halfSize = size / 2.0;
  648. positions.push(-halfSize, -halfSize, 0);
  649. normals.push(0, 0, -1.0);
  650. uvs.push(0.0, 0.0);
  651. positions.push(halfSize, -halfSize, 0);
  652. normals.push(0, 0, -1.0);
  653. uvs.push(1.0, 0.0);
  654. positions.push(halfSize, halfSize, 0);
  655. normals.push(0, 0, -1.0);
  656. uvs.push(1.0, 1.0);
  657. positions.push(-halfSize, halfSize, 0);
  658. normals.push(0, 0, -1.0);
  659. uvs.push(0.0, 1.0);
  660. // Indices
  661. indices.push(0);
  662. indices.push(1);
  663. indices.push(2);
  664. indices.push(0);
  665. indices.push(2);
  666. indices.push(3);
  667. // Result
  668. var vertexData = new BABYLON.VertexData();
  669. vertexData.indices = indices;
  670. vertexData.positions = positions;
  671. vertexData.normals = normals;
  672. vertexData.uvs = uvs;
  673. return vertexData;
  674. }
  675. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  676. public static CreateTorusKnot(radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number): VertexData {
  677. var indices = [];
  678. var positions = [];
  679. var normals = [];
  680. var uvs = [];
  681. radius = radius || 2;
  682. tube = tube || 0.5;
  683. radialSegments = radialSegments || 32;
  684. tubularSegments = tubularSegments || 32;
  685. p = p || 2;
  686. q = q || 3;
  687. // Helper
  688. var getPos = (angle) => {
  689. var cu = Math.cos(angle);
  690. var su = Math.sin(angle);
  691. var quOverP = q / p * angle;
  692. var cs = Math.cos(quOverP);
  693. var tx = radius * (2 + cs) * 0.5 * cu;
  694. var ty = radius * (2 + cs) * su * 0.5;
  695. var tz = radius * Math.sin(quOverP) * 0.5;
  696. return new BABYLON.Vector3(tx, ty, tz);
  697. };
  698. // Vertices
  699. for (var i = 0; i <= radialSegments; i++) {
  700. var modI = i % radialSegments;
  701. var u = modI / radialSegments * 2 * p * Math.PI;
  702. var p1 = getPos(u);
  703. var p2 = getPos(u + 0.01);
  704. var tang = p2.subtract(p1);
  705. var n = p2.add(p1);
  706. var bitan = BABYLON.Vector3.Cross(tang, n);
  707. n = BABYLON.Vector3.Cross(bitan, tang);
  708. bitan.normalize();
  709. n.normalize();
  710. for (var j = 0; j < tubularSegments; j++) {
  711. var modJ = j % tubularSegments;
  712. var v = modJ / tubularSegments * 2 * Math.PI;
  713. var cx = -tube * Math.cos(v);
  714. var cy = tube * Math.sin(v);
  715. positions.push(p1.x + cx * n.x + cy * bitan.x);
  716. positions.push(p1.y + cx * n.y + cy * bitan.y);
  717. positions.push(p1.z + cx * n.z + cy * bitan.z);
  718. uvs.push(i / radialSegments);
  719. uvs.push(j / tubularSegments);
  720. }
  721. }
  722. for (i = 0; i < radialSegments; i++) {
  723. for (j = 0; j < tubularSegments; j++) {
  724. var jNext = (j + 1) % tubularSegments;
  725. var a = i * tubularSegments + j;
  726. var b = (i + 1) * tubularSegments + j;
  727. var c = (i + 1) * tubularSegments + jNext;
  728. var d = i * tubularSegments + jNext;
  729. indices.push(d); indices.push(b); indices.push(a);
  730. indices.push(d); indices.push(c); indices.push(b);
  731. }
  732. }
  733. // Normals
  734. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  735. // Result
  736. var vertexData = new BABYLON.VertexData();
  737. vertexData.indices = indices;
  738. vertexData.positions = positions;
  739. vertexData.normals = normals;
  740. vertexData.uvs = uvs;
  741. return vertexData;
  742. }
  743. // Tools
  744. public static ComputeNormals(positions: number[], indices: number[], normals: number[]) {
  745. var positionVectors = [];
  746. var facesOfVertices = [];
  747. var index;
  748. for (index = 0; index < positions.length; index += 3) {
  749. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  750. positionVectors.push(vector3);
  751. facesOfVertices.push([]);
  752. }
  753. // Compute normals
  754. var facesNormals = [];
  755. for (index = 0; index < indices.length / 3; index++) {
  756. var i1 = indices[index * 3];
  757. var i2 = indices[index * 3 + 1];
  758. var i3 = indices[index * 3 + 2];
  759. var p1 = positionVectors[i1];
  760. var p2 = positionVectors[i2];
  761. var p3 = positionVectors[i3];
  762. var p1p2 = p1.subtract(p2);
  763. var p3p2 = p3.subtract(p2);
  764. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  765. facesOfVertices[i1].push(index);
  766. facesOfVertices[i2].push(index);
  767. facesOfVertices[i3].push(index);
  768. }
  769. for (index = 0; index < positionVectors.length; index++) {
  770. var faces = facesOfVertices[index];
  771. var normal = BABYLON.Vector3.Zero();
  772. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  773. normal.addInPlace(facesNormals[faces[faceIndex]]);
  774. }
  775. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  776. normals[index * 3] = normal.x;
  777. normals[index * 3 + 1] = normal.y;
  778. normals[index * 3 + 2] = normal.z;
  779. }
  780. }
  781. }
  782. }