babylon.mesh.vertexData.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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): 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. var getCircleVector = i => {
  343. var angle = (i * 2.0 * Math.PI / tessellation);
  344. var dx = Math.sin(angle);
  345. var dz = Math.cos(angle);
  346. return new BABYLON.Vector3(dx, 0, dz);
  347. };
  348. var createCylinderCap = isTop => {
  349. var radius = isTop ? radiusTop : radiusBottom;
  350. if (radius == 0) {
  351. return;
  352. }
  353. // Create cap indices.
  354. for (var i = 0; i < tessellation - 2; i++) {
  355. var i1 = (i + 1) % tessellation;
  356. var i2 = (i + 2) % tessellation;
  357. if (!isTop) {
  358. var tmp = i1;
  359. i1 = i2;
  360. i2 = tmp;
  361. }
  362. var vbase = positions.length / 3;
  363. indices.push(vbase);
  364. indices.push(vbase + i1);
  365. indices.push(vbase + i2);
  366. }
  367. // Which end of the cylinder is this?
  368. var normal = new BABYLON.Vector3(0, -1, 0);
  369. var textureScale = new BABYLON.Vector2(-0.5, -0.5);
  370. if (!isTop) {
  371. normal = normal.scale(-1);
  372. textureScale.x = -textureScale.x;
  373. }
  374. // Create cap vertices.
  375. for (i = 0; i < tessellation; i++) {
  376. var circleVector = getCircleVector(i);
  377. var position = circleVector.scale(radius).add(normal.scale(height));
  378. var textureCoordinate = new BABYLON.Vector2(circleVector.x * textureScale.x + 0.5, circleVector.z * textureScale.y + 0.5);
  379. positions.push(position.x, position.y, position.z);
  380. normals.push(normal.x, normal.y, normal.z);
  381. uvs.push(textureCoordinate.x, textureCoordinate.y);
  382. }
  383. };
  384. height /= 2;
  385. var topOffset = new BABYLON.Vector3(0, 1, 0).scale(height);
  386. var stride = tessellation + 1;
  387. // Create a ring of triangles around the outside of the cylinder.
  388. for (var i = 0; i <= tessellation; i++) {
  389. var normal = getCircleVector(i);
  390. var sideOffsetBottom = normal.scale(radiusBottom);
  391. var sideOffsetTop = normal.scale(radiusTop);
  392. var textureCoordinate = new BABYLON.Vector2(i / tessellation, 0);
  393. var position = sideOffsetBottom.add(topOffset);
  394. positions.push(position.x, position.y, position.z);
  395. normals.push(normal.x, normal.y, normal.z);
  396. uvs.push(textureCoordinate.x, textureCoordinate.y);
  397. position = sideOffsetTop.subtract(topOffset);
  398. textureCoordinate.y += 1;
  399. positions.push(position.x, position.y, position.z);
  400. normals.push(normal.x, normal.y, normal.z);
  401. uvs.push(textureCoordinate.x, textureCoordinate.y);
  402. indices.push(i * 2);
  403. indices.push((i * 2 + 2) % (stride * 2));
  404. indices.push(i * 2 + 1);
  405. indices.push(i * 2 + 1);
  406. indices.push((i * 2 + 2) % (stride * 2));
  407. indices.push((i * 2 + 3) % (stride * 2));
  408. }
  409. // Create flat triangle fan caps to seal the top and bottom.
  410. createCylinderCap(true);
  411. createCylinderCap(false);
  412. // Result
  413. var vertexData = new BABYLON.VertexData();
  414. vertexData.indices = indices;
  415. vertexData.positions = positions;
  416. vertexData.normals = normals;
  417. vertexData.uvs = uvs;
  418. return vertexData;
  419. }
  420. public static CreateTorus(diameter, thickness, tessellation) {
  421. var indices = [];
  422. var positions = [];
  423. var normals = [];
  424. var uvs = [];
  425. diameter = diameter || 1;
  426. thickness = thickness || 0.5;
  427. tessellation = tessellation || 16;
  428. var stride = tessellation + 1;
  429. for (var i = 0; i <= tessellation; i++) {
  430. var u = i / tessellation;
  431. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  432. var transform = BABYLON.Matrix.Translation(diameter / 2.0, 0, 0).multiply(BABYLON.Matrix.RotationY(outerAngle));
  433. for (var j = 0; j <= tessellation; j++) {
  434. var v = 1 - j / tessellation;
  435. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  436. var dx = Math.cos(innerAngle);
  437. var dy = Math.sin(innerAngle);
  438. // Create a vertex.
  439. var normal = new BABYLON.Vector3(dx, dy, 0);
  440. var position = normal.scale(thickness / 2);
  441. var textureCoordinate = new BABYLON.Vector2(u, v);
  442. position = BABYLON.Vector3.TransformCoordinates(position, transform);
  443. normal = BABYLON.Vector3.TransformNormal(normal, transform);
  444. positions.push(position.x, position.y, position.z);
  445. normals.push(normal.x, normal.y, normal.z);
  446. uvs.push(textureCoordinate.x, textureCoordinate.y);
  447. // And create indices for two triangles.
  448. var nextI = (i + 1) % stride;
  449. var nextJ = (j + 1) % stride;
  450. indices.push(i * stride + j);
  451. indices.push(i * stride + nextJ);
  452. indices.push(nextI * stride + j);
  453. indices.push(i * stride + nextJ);
  454. indices.push(nextI * stride + nextJ);
  455. indices.push(nextI * stride + j);
  456. }
  457. }
  458. // Result
  459. var vertexData = new BABYLON.VertexData();
  460. vertexData.indices = indices;
  461. vertexData.positions = positions;
  462. vertexData.normals = normals;
  463. vertexData.uvs = uvs;
  464. return vertexData;
  465. }
  466. public static CreateGround(width: number, height: number, subdivisions: number): VertexData {
  467. var indices = [];
  468. var positions = [];
  469. var normals = [];
  470. var uvs = [];
  471. var row, col;
  472. width = width || 1;
  473. height = height || 1;
  474. subdivisions = subdivisions || 1;
  475. for (row = 0; row <= subdivisions; row++) {
  476. for (col = 0; col <= subdivisions; col++) {
  477. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  478. var normal = new BABYLON.Vector3(0, 1.0, 0);
  479. positions.push(position.x, position.y, position.z);
  480. normals.push(normal.x, normal.y, normal.z);
  481. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  482. }
  483. }
  484. for (row = 0; row < subdivisions; row++) {
  485. for (col = 0; col < subdivisions; col++) {
  486. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  487. indices.push(col + 1 + row * (subdivisions + 1));
  488. indices.push(col + row * (subdivisions + 1));
  489. indices.push(col + (row + 1) * (subdivisions + 1));
  490. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  491. indices.push(col + row * (subdivisions + 1));
  492. }
  493. }
  494. // Result
  495. var vertexData = new BABYLON.VertexData();
  496. vertexData.indices = indices;
  497. vertexData.positions = positions;
  498. vertexData.normals = normals;
  499. vertexData.uvs = uvs;
  500. return vertexData;
  501. }
  502. public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number): VertexData {
  503. var indices = [];
  504. var positions = [];
  505. var normals = [];
  506. var uvs = [];
  507. var row, col;
  508. // Vertices
  509. for (row = 0; row <= subdivisions; row++) {
  510. for (col = 0; col <= subdivisions; col++) {
  511. var position = new BABYLON.Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  512. // Compute height
  513. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  514. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  515. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  516. var r = buffer[pos] / 255.0;
  517. var g = buffer[pos + 1] / 255.0;
  518. var b = buffer[pos + 2] / 255.0;
  519. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  520. position.y = minHeight + (maxHeight - minHeight) * gradient;
  521. // Add vertex
  522. positions.push(position.x, position.y, position.z);
  523. normals.push(0, 0, 0);
  524. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  525. }
  526. }
  527. // Indices
  528. for (row = 0; row < subdivisions; row++) {
  529. for (col = 0; col < subdivisions; col++) {
  530. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  531. indices.push(col + 1 + row * (subdivisions + 1));
  532. indices.push(col + row * (subdivisions + 1));
  533. indices.push(col + (row + 1) * (subdivisions + 1));
  534. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  535. indices.push(col + row * (subdivisions + 1));
  536. }
  537. }
  538. // Normals
  539. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  540. // Result
  541. var vertexData = new BABYLON.VertexData();
  542. vertexData.indices = indices;
  543. vertexData.positions = positions;
  544. vertexData.normals = normals;
  545. vertexData.uvs = uvs;
  546. return vertexData;
  547. }
  548. public static CreatePlane(size: number): VertexData {
  549. var indices = [];
  550. var positions = [];
  551. var normals = [];
  552. var uvs = [];
  553. size = size || 1;
  554. // Vertices
  555. var halfSize = size / 2.0;
  556. positions.push(-halfSize, -halfSize, 0);
  557. normals.push(0, 0, -1.0);
  558. uvs.push(0.0, 0.0);
  559. positions.push(halfSize, -halfSize, 0);
  560. normals.push(0, 0, -1.0);
  561. uvs.push(1.0, 0.0);
  562. positions.push(halfSize, halfSize, 0);
  563. normals.push(0, 0, -1.0);
  564. uvs.push(1.0, 1.0);
  565. positions.push(-halfSize, halfSize, 0);
  566. normals.push(0, 0, -1.0);
  567. uvs.push(0.0, 1.0);
  568. // Indices
  569. indices.push(0);
  570. indices.push(1);
  571. indices.push(2);
  572. indices.push(0);
  573. indices.push(2);
  574. indices.push(3);
  575. // Result
  576. var vertexData = new BABYLON.VertexData();
  577. vertexData.indices = indices;
  578. vertexData.positions = positions;
  579. vertexData.normals = normals;
  580. vertexData.uvs = uvs;
  581. return vertexData;
  582. }
  583. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  584. public static CreateTorusKnot(radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number): VertexData {
  585. var indices = [];
  586. var positions = [];
  587. var normals = [];
  588. var uvs = [];
  589. radius = radius || 2;
  590. tube = tube || 0.5;
  591. radialSegments = radialSegments || 32;
  592. tubularSegments = tubularSegments || 32;
  593. p = p || 2;
  594. q = q || 3;
  595. // Helper
  596. var getPos = (angle) => {
  597. var cu = Math.cos(angle);
  598. var su = Math.sin(angle);
  599. var quOverP = q / p * angle;
  600. var cs = Math.cos(quOverP);
  601. var tx = radius * (2 + cs) * 0.5 * cu;
  602. var ty = radius * (2 + cs) * su * 0.5;
  603. var tz = radius * Math.sin(quOverP) * 0.5;
  604. return new BABYLON.Vector3(tx, ty, tz);
  605. };
  606. // Vertices
  607. for (var i = 0; i <= radialSegments; i++) {
  608. var modI = i % radialSegments;
  609. var u = modI / radialSegments * 2 * p * Math.PI;
  610. var p1 = getPos(u);
  611. var p2 = getPos(u + 0.01);
  612. var tang = p2.subtract(p1);
  613. var n = p2.add(p1);
  614. var bitan = BABYLON.Vector3.Cross(tang, n);
  615. n = BABYLON.Vector3.Cross(bitan, tang);
  616. bitan.normalize();
  617. n.normalize();
  618. for (var j = 0; j < tubularSegments; j++) {
  619. var modJ = j % tubularSegments;
  620. var v = modJ / tubularSegments * 2 * Math.PI;
  621. var cx = -tube * Math.cos(v);
  622. var cy = tube * Math.sin(v);
  623. positions.push(p1.x + cx * n.x + cy * bitan.x);
  624. positions.push(p1.y + cx * n.y + cy * bitan.y);
  625. positions.push(p1.z + cx * n.z + cy * bitan.z);
  626. uvs.push(i / radialSegments);
  627. uvs.push(j / tubularSegments);
  628. }
  629. }
  630. for (i = 0; i < radialSegments; i++) {
  631. for (j = 0; j < tubularSegments; j++) {
  632. var jNext = (j + 1) % tubularSegments;
  633. var a = i * tubularSegments + j;
  634. var b = (i + 1) * tubularSegments + j;
  635. var c = (i + 1) * tubularSegments + jNext;
  636. var d = i * tubularSegments + jNext;
  637. indices.push(d); indices.push(b); indices.push(a);
  638. indices.push(d); indices.push(c); indices.push(b);
  639. }
  640. }
  641. // Normals
  642. BABYLON.VertexData.ComputeNormals(positions, indices, normals);
  643. // Result
  644. var vertexData = new BABYLON.VertexData();
  645. vertexData.indices = indices;
  646. vertexData.positions = positions;
  647. vertexData.normals = normals;
  648. vertexData.uvs = uvs;
  649. return vertexData;
  650. }
  651. // Tools
  652. public static ComputeNormals(positions: number[], indices: number[], normals: number[]) {
  653. var positionVectors = [];
  654. var facesOfVertices = [];
  655. var index;
  656. for (index = 0; index < positions.length; index += 3) {
  657. var vector3 = new BABYLON.Vector3(positions[index], positions[index + 1], positions[index + 2]);
  658. positionVectors.push(vector3);
  659. facesOfVertices.push([]);
  660. }
  661. // Compute normals
  662. var facesNormals = [];
  663. for (index = 0; index < indices.length / 3; index++) {
  664. var i1 = indices[index * 3];
  665. var i2 = indices[index * 3 + 1];
  666. var i3 = indices[index * 3 + 2];
  667. var p1 = positionVectors[i1];
  668. var p2 = positionVectors[i2];
  669. var p3 = positionVectors[i3];
  670. var p1p2 = p1.subtract(p2);
  671. var p3p2 = p3.subtract(p2);
  672. facesNormals[index] = BABYLON.Vector3.Normalize(BABYLON.Vector3.Cross(p1p2, p3p2));
  673. facesOfVertices[i1].push(index);
  674. facesOfVertices[i2].push(index);
  675. facesOfVertices[i3].push(index);
  676. }
  677. for (index = 0; index < positionVectors.length; index++) {
  678. var faces = facesOfVertices[index];
  679. var normal = BABYLON.Vector3.Zero();
  680. for (var faceIndex = 0; faceIndex < faces.length; faceIndex++) {
  681. normal.addInPlace(facesNormals[faces[faceIndex]]);
  682. }
  683. normal = BABYLON.Vector3.Normalize(normal.scale(1.0 / faces.length));
  684. normals[index * 3] = normal.x;
  685. normals[index * 3 + 1] = normal.y;
  686. normals[index * 3 + 2] = normal.z;
  687. }
  688. }
  689. }
  690. }