babylon.mesh.vertexData.ts 31 KB

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