babylon.mesh.vertexData.ts 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. module BABYLON {
  2. export interface IGetSetVerticesData {
  3. isVerticesDataPresent(kind: string): boolean;
  4. getVerticesData(kind: string, copyWhenShared?: boolean): number[];
  5. getIndices(copyWhenShared?: boolean): 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 uvs2: number[];
  15. public uvs3: number[];
  16. public uvs4: number[];
  17. public uvs5: number[];
  18. public uvs6: number[];
  19. public colors: number[];
  20. public matricesIndices: number[];
  21. public matricesWeights: number[];
  22. public indices: number[];
  23. public set(data: number[], kind: string) {
  24. switch (kind) {
  25. case VertexBuffer.PositionKind:
  26. this.positions = data;
  27. break;
  28. case VertexBuffer.NormalKind:
  29. this.normals = data;
  30. break;
  31. case VertexBuffer.UVKind:
  32. this.uvs = data;
  33. break;
  34. case VertexBuffer.UV2Kind:
  35. this.uvs2 = data;
  36. break;
  37. case VertexBuffer.UV3Kind:
  38. this.uvs3 = data;
  39. break;
  40. case VertexBuffer.UV4Kind:
  41. this.uvs4 = data;
  42. break;
  43. case VertexBuffer.UV5Kind:
  44. this.uvs5 = data;
  45. break;
  46. case VertexBuffer.UV6Kind:
  47. this.uvs6 = data;
  48. break;
  49. case VertexBuffer.ColorKind:
  50. this.colors = data;
  51. break;
  52. case VertexBuffer.MatricesIndicesKind:
  53. this.matricesIndices = data;
  54. break;
  55. case VertexBuffer.MatricesWeightsKind:
  56. this.matricesWeights = data;
  57. break;
  58. }
  59. }
  60. public applyToMesh(mesh: Mesh, updatable?: boolean): void {
  61. this._applyTo(mesh, updatable);
  62. }
  63. public applyToGeometry(geometry: Geometry, updatable?: boolean): void {
  64. this._applyTo(geometry, updatable);
  65. }
  66. public updateMesh(mesh: Mesh, updateExtends?: boolean, makeItUnique?: boolean): void {
  67. this._update(mesh);
  68. }
  69. public updateGeometry(geometry: Geometry, updateExtends?: boolean, makeItUnique?: boolean): void {
  70. this._update(geometry);
  71. }
  72. private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean) {
  73. if (this.positions) {
  74. meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
  75. }
  76. if (this.normals) {
  77. meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
  78. }
  79. if (this.uvs) {
  80. meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
  81. }
  82. if (this.uvs2) {
  83. meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
  84. }
  85. if (this.uvs3) {
  86. meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
  87. }
  88. if (this.uvs4) {
  89. meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
  90. }
  91. if (this.uvs5) {
  92. meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
  93. }
  94. if (this.uvs6) {
  95. meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
  96. }
  97. if (this.colors) {
  98. meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
  99. }
  100. if (this.matricesIndices) {
  101. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  102. }
  103. if (this.matricesWeights) {
  104. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  105. }
  106. if (this.indices) {
  107. meshOrGeometry.setIndices(this.indices);
  108. }
  109. }
  110. private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean) {
  111. if (this.positions) {
  112. meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  113. }
  114. if (this.normals) {
  115. meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  116. }
  117. if (this.uvs) {
  118. meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  119. }
  120. if (this.uvs2) {
  121. meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  122. }
  123. if (this.uvs3) {
  124. meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  125. }
  126. if (this.uvs4) {
  127. meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  128. }
  129. if (this.uvs5) {
  130. meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  131. }
  132. if (this.uvs6) {
  133. meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  134. }
  135. if (this.colors) {
  136. meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  137. }
  138. if (this.matricesIndices) {
  139. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  140. }
  141. if (this.matricesWeights) {
  142. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  143. }
  144. if (this.indices) {
  145. meshOrGeometry.setIndices(this.indices);
  146. }
  147. }
  148. public transform(matrix: Matrix): void {
  149. var transformed = Vector3.Zero();
  150. if (this.positions) {
  151. var position = Vector3.Zero();
  152. for (var index = 0; index < this.positions.length; index += 3) {
  153. Vector3.FromArrayToRef(this.positions, index, position);
  154. Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  155. this.positions[index] = transformed.x;
  156. this.positions[index + 1] = transformed.y;
  157. this.positions[index + 2] = transformed.z;
  158. }
  159. }
  160. if (this.normals) {
  161. var normal = Vector3.Zero();
  162. for (index = 0; index < this.normals.length; index += 3) {
  163. Vector3.FromArrayToRef(this.normals, index, normal);
  164. Vector3.TransformNormalToRef(normal, matrix, transformed);
  165. this.normals[index] = transformed.x;
  166. this.normals[index + 1] = transformed.y;
  167. this.normals[index + 2] = transformed.z;
  168. }
  169. }
  170. }
  171. public merge(other: VertexData): void {
  172. if (other.indices) {
  173. if (!this.indices) {
  174. this.indices = [];
  175. }
  176. var offset = this.positions ? this.positions.length / 3 : 0;
  177. for (var index = 0; index < other.indices.length; index++) {
  178. this.indices.push(other.indices[index] + offset);
  179. }
  180. }
  181. if (other.positions) {
  182. if (!this.positions) {
  183. this.positions = [];
  184. }
  185. for (index = 0; index < other.positions.length; index++) {
  186. this.positions.push(other.positions[index]);
  187. }
  188. }
  189. if (other.normals) {
  190. if (!this.normals) {
  191. this.normals = [];
  192. }
  193. for (index = 0; index < other.normals.length; index++) {
  194. this.normals.push(other.normals[index]);
  195. }
  196. }
  197. if (other.uvs) {
  198. if (!this.uvs) {
  199. this.uvs = [];
  200. }
  201. for (index = 0; index < other.uvs.length; index++) {
  202. this.uvs.push(other.uvs[index]);
  203. }
  204. }
  205. if (other.uvs2) {
  206. if (!this.uvs2) {
  207. this.uvs2 = [];
  208. }
  209. for (index = 0; index < other.uvs2.length; index++) {
  210. this.uvs2.push(other.uvs2[index]);
  211. }
  212. }
  213. if (other.uvs3) {
  214. if (!this.uvs3) {
  215. this.uvs3 = [];
  216. }
  217. for (index = 0; index < other.uvs3.length; index++) {
  218. this.uvs3.push(other.uvs3[index]);
  219. }
  220. }
  221. if (other.uvs4) {
  222. if (!this.uvs4) {
  223. this.uvs4 = [];
  224. }
  225. for (index = 0; index < other.uvs4.length; index++) {
  226. this.uvs4.push(other.uvs4[index]);
  227. }
  228. }
  229. if (other.uvs5) {
  230. if (!this.uvs5) {
  231. this.uvs5 = [];
  232. }
  233. for (index = 0; index < other.uvs5.length; index++) {
  234. this.uvs5.push(other.uvs5[index]);
  235. }
  236. }
  237. if (other.uvs6) {
  238. if (!this.uvs6) {
  239. this.uvs6 = [];
  240. }
  241. for (index = 0; index < other.uvs6.length; index++) {
  242. this.uvs6.push(other.uvs6[index]);
  243. }
  244. }
  245. if (other.matricesIndices) {
  246. if (!this.matricesIndices) {
  247. this.matricesIndices = [];
  248. }
  249. for (index = 0; index < other.matricesIndices.length; index++) {
  250. this.matricesIndices.push(other.matricesIndices[index]);
  251. }
  252. }
  253. if (other.matricesWeights) {
  254. if (!this.matricesWeights) {
  255. this.matricesWeights = [];
  256. }
  257. for (index = 0; index < other.matricesWeights.length; index++) {
  258. this.matricesWeights.push(other.matricesWeights[index]);
  259. }
  260. }
  261. if (other.colors) {
  262. if (!this.colors) {
  263. this.colors = [];
  264. }
  265. for (index = 0; index < other.colors.length; index++) {
  266. this.colors.push(other.colors[index]);
  267. }
  268. }
  269. }
  270. // Statics
  271. public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean): VertexData {
  272. return VertexData._ExtractFrom(mesh, copyWhenShared);
  273. }
  274. public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean): VertexData {
  275. return VertexData._ExtractFrom(geometry, copyWhenShared);
  276. }
  277. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean): VertexData {
  278. var result = new VertexData();
  279. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  280. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared);
  281. }
  282. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  283. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared);
  284. }
  285. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  286. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared);
  287. }
  288. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  289. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared);
  290. }
  291. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  292. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared);
  293. }
  294. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  295. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared);
  296. }
  297. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  298. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared);
  299. }
  300. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  301. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared);
  302. }
  303. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  304. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared);
  305. }
  306. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  307. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared);
  308. }
  309. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  310. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared);
  311. }
  312. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  313. return result;
  314. }
  315. public static CreateRibbon(pathArray: Vector3[][], closeArray: boolean, closePath: boolean, offset: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  316. closeArray = closeArray || false;
  317. closePath = closePath || false;
  318. var defaultOffset = Math.floor(pathArray[0].length / 2);
  319. offset = offset || defaultOffset;
  320. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  321. var positions: number[] = [];
  322. var indices: number[] = [];
  323. var normals: number[] = [];
  324. var uvs: number[] = [];
  325. var us: number[][] = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  326. var vs: number[][] = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  327. var uTotalDistance: number[] = []; // uTotalDistance[p] : total distance of path p
  328. var vTotalDistance: number[] = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  329. var minlg: number; // minimal length among all paths from pathArray
  330. var lg: number[] = []; // array of path lengths : nb of vertex per path
  331. var idx: number[] = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  332. var p: number; // path iterator
  333. var i: number; // point iterator
  334. var j: number; // point iterator
  335. // if single path in pathArray
  336. if (pathArray.length < 2) {
  337. var ar1: Vector3[] = [];
  338. var ar2: Vector3[] = [];
  339. for (i = 0; i < pathArray[0].length - offset; i++) {
  340. ar1.push(pathArray[0][i]);
  341. ar2.push(pathArray[0][i + offset]);
  342. }
  343. pathArray = [ar1, ar2];
  344. }
  345. // positions and horizontal distances (u)
  346. var idc: number = 0;
  347. var closePathCorr: number = (closePath) ? 1 : 0;
  348. var path: Vector3[];
  349. var l: number;
  350. minlg = pathArray[0].length;
  351. for (p = 0; p < pathArray.length; p++) {
  352. uTotalDistance[p] = 0;
  353. us[p] = [0];
  354. path = pathArray[p];
  355. l = path.length;
  356. minlg = (minlg < l) ? minlg : l;
  357. j = 0;
  358. while (j < l) {
  359. positions.push(path[j].x, path[j].y, path[j].z);
  360. if (j > 0) {
  361. var vectlg: number = path[j].subtract(path[j - 1]).length();
  362. var dist: number = vectlg + uTotalDistance[p];
  363. us[p].push(dist);
  364. uTotalDistance[p] = dist;
  365. }
  366. j++;
  367. }
  368. if (closePath) {
  369. j--;
  370. positions.push(path[0].x, path[0].y, path[0].z);
  371. vectlg = path[j].subtract(path[0]).length();
  372. dist = vectlg + uTotalDistance[p];
  373. us[p].push(dist);
  374. uTotalDistance[p] = dist;
  375. }
  376. lg[p] = l + closePathCorr;
  377. idx[p] = idc;
  378. idc += (l + closePathCorr);
  379. }
  380. // vertical distances (v)
  381. var path1: Vector3[];
  382. var path2: Vector3[];
  383. var vertex1: Vector3;
  384. var vertex2: Vector3;
  385. for (i = 0; i < minlg + closePathCorr; i++) {
  386. vTotalDistance[i] = 0;
  387. vs[i] = [0];
  388. for (p = 0; p < pathArray.length - 1; p++) {
  389. path1 = pathArray[p];
  390. path2 = pathArray[p + 1];
  391. if (i === minlg) { // closePath
  392. vertex1 = path1[0];
  393. vertex2 = path2[0];
  394. }
  395. else {
  396. vertex1 = path1[i];
  397. vertex2 = path2[i];
  398. }
  399. vectlg = vertex2.subtract(vertex1).length();
  400. dist = vectlg + vTotalDistance[i];
  401. vs[i].push(dist);
  402. vTotalDistance[i] = dist;
  403. }
  404. if (closeArray) {
  405. path1 = pathArray[p];
  406. path2 = pathArray[0];
  407. vectlg = path2[i].subtract(path1[i]).length();
  408. dist = vectlg + vTotalDistance[i];
  409. vTotalDistance[i] = dist;
  410. }
  411. }
  412. // uvs
  413. var u: number;
  414. var v: number;
  415. for (p = 0; p < pathArray.length; p++) {
  416. for (i = 0; i < minlg + closePathCorr; i++) {
  417. u = us[p][i] / uTotalDistance[p];
  418. v = vs[i][p] / vTotalDistance[i];
  419. uvs.push(u, v);
  420. }
  421. }
  422. // indices
  423. p = 0; // path index
  424. var pi: number = 0; // positions array index
  425. var l1: number = lg[p] - 1; // path1 length
  426. var l2: number = lg[p + 1] - 1; // path2 length
  427. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  428. var shft: number = idx[1] - idx[0]; // shift
  429. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  430. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  431. // draw two triangles between path1 (p1) and path2 (p2) : (p1.pi, p2.pi, p1.pi+1) and (p2.pi+1, p1.pi+1, p2.pi) clockwise
  432. indices.push(pi, pi + shft, pi + 1);
  433. indices.push(pi + shft + 1, pi + 1, pi + shft);
  434. pi += 1;
  435. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  436. p++;
  437. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  438. shft = idx[0] - idx[p];
  439. l1 = lg[p] - 1;
  440. l2 = lg[0] - 1;
  441. }
  442. else {
  443. shft = idx[p + 1] - idx[p];
  444. l1 = lg[p] - 1;
  445. l2 = lg[p + 1] - 1;
  446. }
  447. pi = idx[p];
  448. min = (l1 < l2) ? l1 + pi : l2 + pi;
  449. }
  450. }
  451. // normals
  452. VertexData.ComputeNormals(positions, indices, normals);
  453. if (closePath) {
  454. var indexFirst: number = 0;
  455. var indexLast: number = 0;
  456. for (p = 0; p < pathArray.length; p++) {
  457. indexFirst = idx[p] * 3;
  458. if (p + 1 < pathArray.length) {
  459. indexLast = (idx[p + 1] - 1) * 3;
  460. }
  461. else {
  462. indexLast = normals.length - 3;
  463. }
  464. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  465. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  466. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  467. normals[indexLast] = normals[indexFirst];
  468. normals[indexLast + 1] = normals[indexFirst + 1];
  469. normals[indexLast + 2] = normals[indexFirst + 2];
  470. }
  471. }
  472. // sides
  473. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  474. // Result
  475. var vertexData = new VertexData();
  476. vertexData.indices = indices;
  477. vertexData.positions = positions;
  478. vertexData.normals = normals;
  479. vertexData.uvs = uvs;
  480. if (closePath) {
  481. (<any>vertexData)._idx = idx;
  482. }
  483. return vertexData;
  484. }
  485. public static CreateBox(size: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  486. var normalsSource = [
  487. new Vector3(0, 0, 1),
  488. new Vector3(0, 0, -1),
  489. new Vector3(1, 0, 0),
  490. new Vector3(-1, 0, 0),
  491. new Vector3(0, 1, 0),
  492. new Vector3(0, -1, 0)
  493. ];
  494. var indices = [];
  495. var positions = [];
  496. var normals = [];
  497. var uvs = [];
  498. size = size || 1;
  499. // Create each face in turn.
  500. for (var index = 0; index < normalsSource.length; index++) {
  501. var normal = normalsSource[index];
  502. // Get two vectors perpendicular to the face normal and to each other.
  503. var side1 = new Vector3(normal.y, normal.z, normal.x);
  504. var side2 = Vector3.Cross(normal, side1);
  505. // Six indices (two triangles) per face.
  506. var verticesLength = positions.length / 3;
  507. indices.push(verticesLength);
  508. indices.push(verticesLength + 1);
  509. indices.push(verticesLength + 2);
  510. indices.push(verticesLength);
  511. indices.push(verticesLength + 2);
  512. indices.push(verticesLength + 3);
  513. // Four vertices per face.
  514. var vertex = normal.subtract(side1).subtract(side2).scale(size / 2);
  515. positions.push(vertex.x, vertex.y, vertex.z);
  516. normals.push(normal.x, normal.y, normal.z);
  517. uvs.push(1.0, 1.0);
  518. vertex = normal.subtract(side1).add(side2).scale(size / 2);
  519. positions.push(vertex.x, vertex.y, vertex.z);
  520. normals.push(normal.x, normal.y, normal.z);
  521. uvs.push(0.0, 1.0);
  522. vertex = normal.add(side1).add(side2).scale(size / 2);
  523. positions.push(vertex.x, vertex.y, vertex.z);
  524. normals.push(normal.x, normal.y, normal.z);
  525. uvs.push(0.0, 0.0);
  526. vertex = normal.add(side1).subtract(side2).scale(size / 2);
  527. positions.push(vertex.x, vertex.y, vertex.z);
  528. normals.push(normal.x, normal.y, normal.z);
  529. uvs.push(1.0, 0.0);
  530. }
  531. // sides
  532. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  533. // Result
  534. var vertexData = new VertexData();
  535. vertexData.indices = indices;
  536. vertexData.positions = positions;
  537. vertexData.normals = normals;
  538. vertexData.uvs = uvs;
  539. return vertexData;
  540. }
  541. public static CreateSphere(segments: number, diameter: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  542. segments = segments || 32;
  543. diameter = diameter || 1;
  544. var radius = diameter / 2;
  545. var totalZRotationSteps = 2 + segments;
  546. var totalYRotationSteps = 2 * totalZRotationSteps;
  547. var indices = [];
  548. var positions = [];
  549. var normals = [];
  550. var uvs = [];
  551. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  552. var normalizedZ = zRotationStep / totalZRotationSteps;
  553. var angleZ = (normalizedZ * Math.PI);
  554. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  555. var normalizedY = yRotationStep / totalYRotationSteps;
  556. var angleY = normalizedY * Math.PI * 2;
  557. var rotationZ = Matrix.RotationZ(-angleZ);
  558. var rotationY = Matrix.RotationY(angleY);
  559. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  560. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  561. var vertex = complete.scale(radius);
  562. var normal = Vector3.Normalize(vertex);
  563. positions.push(vertex.x, vertex.y, vertex.z);
  564. normals.push(normal.x, normal.y, normal.z);
  565. uvs.push(normalizedZ, normalizedY);
  566. }
  567. if (zRotationStep > 0) {
  568. var verticesCount = positions.length / 3;
  569. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  570. indices.push((firstIndex));
  571. indices.push((firstIndex + 1));
  572. indices.push(firstIndex + totalYRotationSteps + 1);
  573. indices.push((firstIndex + totalYRotationSteps + 1));
  574. indices.push((firstIndex + 1));
  575. indices.push((firstIndex + totalYRotationSteps + 2));
  576. }
  577. }
  578. }
  579. // Sides
  580. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  581. // Result
  582. var vertexData = new VertexData();
  583. vertexData.indices = indices;
  584. vertexData.positions = positions;
  585. vertexData.normals = normals;
  586. vertexData.uvs = uvs;
  587. return vertexData;
  588. }
  589. public static CreateCylinder(height: number, diameterTop: number, diameterBottom: number, tessellation: number, subdivisions: number = 1, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  590. var radiusTop = diameterTop / 2;
  591. var radiusBottom = diameterBottom / 2;
  592. var indices = [];
  593. var positions = [];
  594. var normals = [];
  595. var uvs = [];
  596. height = height || 1;
  597. diameterTop = diameterTop || 0.5;
  598. diameterBottom = diameterBottom || 1;
  599. tessellation = tessellation || 16;
  600. subdivisions = subdivisions || 1;
  601. subdivisions = (subdivisions < 1) ? 1 : subdivisions;
  602. var getCircleVector = i => {
  603. var angle = (i * 2.0 * Math.PI / tessellation);
  604. var dx = Math.cos(angle);
  605. var dz = Math.sin(angle);
  606. return new Vector3(dx, 0, dz);
  607. };
  608. var createCylinderCap = isTop => {
  609. var radius = isTop ? radiusTop : radiusBottom;
  610. if (radius === 0) {
  611. return;
  612. }
  613. var vbase = positions.length / 3;
  614. var offset = new Vector3(0, height / 2, 0);
  615. var textureScale = new Vector2(0.5, 0.5);
  616. if (!isTop) {
  617. offset.scaleInPlace(-1);
  618. textureScale.x = -textureScale.x;
  619. }
  620. // Positions, normals & uvs
  621. for (var i = 0; i < tessellation; i++) {
  622. var circleVector = getCircleVector(i);
  623. var position = circleVector.scale(radius).add(offset);
  624. var textureCoordinate = new Vector2(
  625. circleVector.x * textureScale.x + 0.5,
  626. circleVector.z * textureScale.y + 0.5
  627. );
  628. positions.push(position.x, position.y, position.z);
  629. uvs.push(textureCoordinate.x, textureCoordinate.y);
  630. }
  631. // Indices
  632. for (i = 0; i < tessellation - 2; i++) {
  633. if (!isTop) {
  634. indices.push(vbase);
  635. indices.push(vbase + (i + 2) % tessellation);
  636. indices.push(vbase + (i + 1) % tessellation);
  637. } else {
  638. indices.push(vbase);
  639. indices.push(vbase + (i + 1) % tessellation);
  640. indices.push(vbase + (i + 2) % tessellation);
  641. }
  642. }
  643. };
  644. var base = new Vector3(0, -1, 0).scale(height / 2);
  645. var offset = new Vector3(0, 1, 0).scale(height / subdivisions);
  646. var stride = tessellation + 1;
  647. // Positions, normals & uvs
  648. for (var i = 0; i <= tessellation; i++) {
  649. var circleVector = getCircleVector(i);
  650. var textureCoordinate = new Vector2(i / tessellation, 0);
  651. var position: Vector3, radius = radiusBottom;
  652. for (var s = 0; s <= subdivisions; s++) {
  653. // Update variables
  654. position = circleVector.scale(radius);
  655. position.addInPlace(base.add(offset.scale(s)));
  656. textureCoordinate.y += 1 / subdivisions;
  657. radius += (radiusTop - radiusBottom) / subdivisions;
  658. // Push in arrays
  659. positions.push(position.x, position.y, position.z);
  660. uvs.push(textureCoordinate.x, textureCoordinate.y);
  661. }
  662. }
  663. subdivisions += 1;
  664. // Indices
  665. for (s = 0; s < subdivisions - 1; s++) {
  666. for (i = 0; i <= tessellation; i++) {
  667. indices.push(i * subdivisions + s);
  668. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  669. indices.push(i * subdivisions + (s + 1));
  670. indices.push(i * subdivisions + (s + 1));
  671. indices.push((i * subdivisions + (s + subdivisions)) % (stride * subdivisions));
  672. indices.push((i * subdivisions + (s + subdivisions + 1)) % (stride * subdivisions));
  673. }
  674. }
  675. // Create flat triangle fan caps to seal the top and bottom.
  676. createCylinderCap(true);
  677. createCylinderCap(false);
  678. // Normals
  679. VertexData.ComputeNormals(positions, indices, normals);
  680. // Sides
  681. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  682. // Result
  683. var vertexData = new VertexData();
  684. vertexData.indices = indices;
  685. vertexData.positions = positions;
  686. vertexData.normals = normals;
  687. vertexData.uvs = uvs;
  688. return vertexData;
  689. }
  690. public static CreateTorus(diameter, thickness, tessellation, sideOrientation: number = Mesh.DEFAULTSIDE) {
  691. var indices = [];
  692. var positions = [];
  693. var normals = [];
  694. var uvs = [];
  695. diameter = diameter || 1;
  696. thickness = thickness || 0.5;
  697. tessellation = tessellation || 16;
  698. var stride = tessellation + 1;
  699. for (var i = 0; i <= tessellation; i++) {
  700. var u = i / tessellation;
  701. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  702. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  703. for (var j = 0; j <= tessellation; j++) {
  704. var v = 1 - j / tessellation;
  705. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  706. var dx = Math.cos(innerAngle);
  707. var dy = Math.sin(innerAngle);
  708. // Create a vertex.
  709. var normal = new Vector3(dx, dy, 0);
  710. var position = normal.scale(thickness / 2);
  711. var textureCoordinate = new Vector2(u, v);
  712. position = Vector3.TransformCoordinates(position, transform);
  713. normal = Vector3.TransformNormal(normal, transform);
  714. positions.push(position.x, position.y, position.z);
  715. normals.push(normal.x, normal.y, normal.z);
  716. uvs.push(textureCoordinate.x, textureCoordinate.y);
  717. // And create indices for two triangles.
  718. var nextI = (i + 1) % stride;
  719. var nextJ = (j + 1) % stride;
  720. indices.push(i * stride + j);
  721. indices.push(i * stride + nextJ);
  722. indices.push(nextI * stride + j);
  723. indices.push(i * stride + nextJ);
  724. indices.push(nextI * stride + nextJ);
  725. indices.push(nextI * stride + j);
  726. }
  727. }
  728. // Sides
  729. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  730. // Result
  731. var vertexData = new VertexData();
  732. vertexData.indices = indices;
  733. vertexData.positions = positions;
  734. vertexData.normals = normals;
  735. vertexData.uvs = uvs;
  736. return vertexData;
  737. }
  738. public static CreateLines(points: Vector3[]): VertexData {
  739. var indices = [];
  740. var positions = [];
  741. for (var index = 0; index < points.length; index++) {
  742. positions.push(points[index].x, points[index].y, points[index].z);
  743. if (index > 0) {
  744. indices.push(index - 1);
  745. indices.push(index);
  746. }
  747. }
  748. // Result
  749. var vertexData = new VertexData();
  750. vertexData.indices = indices;
  751. vertexData.positions = positions;
  752. return vertexData;
  753. }
  754. public static CreateDashedLines(points: Vector3[], dashSize: number, gapSize: number, dashNb: number): VertexData {
  755. dashSize = dashSize || 3;
  756. gapSize = gapSize || 1;
  757. dashNb = dashNb || 200;
  758. var positions = new Array<number>();
  759. var indices = new Array<number>();
  760. var curvect = Vector3.Zero();
  761. var lg = 0;
  762. var nb = 0;
  763. var shft = 0;
  764. var dashshft = 0;
  765. var curshft = 0;
  766. var idx = 0;
  767. var i = 0;
  768. for (i = 0; i < points.length - 1; i++) {
  769. points[i + 1].subtractToRef(points[i], curvect);
  770. lg += curvect.length();
  771. }
  772. shft = lg / dashNb;
  773. dashshft = dashSize * shft / (dashSize + gapSize);
  774. for (i = 0; i < points.length - 1; i++) {
  775. points[i + 1].subtractToRef(points[i], curvect);
  776. nb = Math.floor(curvect.length() / shft);
  777. curvect.normalize();
  778. for (var j = 0; j < nb; j++) {
  779. curshft = shft * j;
  780. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  781. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  782. indices.push(idx, idx + 1);
  783. idx += 2;
  784. }
  785. }
  786. // Result
  787. var vertexData = new VertexData();
  788. vertexData.positions = positions;
  789. vertexData.indices = indices;
  790. return vertexData;
  791. }
  792. public static CreateGround(width: number, height: number, subdivisions: number): VertexData {
  793. var indices = [];
  794. var positions = [];
  795. var normals = [];
  796. var uvs = [];
  797. var row: number, col: number;
  798. width = width || 1;
  799. height = height || 1;
  800. subdivisions = subdivisions || 1;
  801. for (row = 0; row <= subdivisions; row++) {
  802. for (col = 0; col <= subdivisions; col++) {
  803. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  804. var normal = new Vector3(0, 1.0, 0);
  805. positions.push(position.x, position.y, position.z);
  806. normals.push(normal.x, normal.y, normal.z);
  807. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  808. }
  809. }
  810. for (row = 0; row < subdivisions; row++) {
  811. for (col = 0; col < subdivisions; col++) {
  812. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  813. indices.push(col + 1 + row * (subdivisions + 1));
  814. indices.push(col + row * (subdivisions + 1));
  815. indices.push(col + (row + 1) * (subdivisions + 1));
  816. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  817. indices.push(col + row * (subdivisions + 1));
  818. }
  819. }
  820. // Result
  821. var vertexData = new VertexData();
  822. vertexData.indices = indices;
  823. vertexData.positions = positions;
  824. vertexData.normals = normals;
  825. vertexData.uvs = uvs;
  826. return vertexData;
  827. }
  828. public static CreateTiledGround(xmin: number, zmin: number, xmax: number, zmax: number, subdivisions = { w: 1, h: 1 }, precision = { w: 1, h: 1 }): VertexData {
  829. var indices = [];
  830. var positions = [];
  831. var normals = [];
  832. var uvs = [];
  833. var row: number, col: number, tileRow: number, tileCol: number;
  834. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  835. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  836. precision.w = (precision.w < 1) ? 1 : precision.w;
  837. precision.h = (precision.h < 1) ? 1 : precision.h;
  838. var tileSize = {
  839. 'w': (xmax - xmin) / subdivisions.w,
  840. 'h': (zmax - zmin) / subdivisions.h
  841. };
  842. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  843. // Indices
  844. var base = positions.length / 3;
  845. var rowLength = precision.w + 1;
  846. for (row = 0; row < precision.h; row++) {
  847. for (col = 0; col < precision.w; col++) {
  848. var square = [
  849. base + col + row * rowLength,
  850. base + (col + 1) + row * rowLength,
  851. base + (col + 1) + (row + 1) * rowLength,
  852. base + col + (row + 1) * rowLength
  853. ];
  854. indices.push(square[1]);
  855. indices.push(square[2]);
  856. indices.push(square[3]);
  857. indices.push(square[0]);
  858. indices.push(square[1]);
  859. indices.push(square[3]);
  860. }
  861. }
  862. // Position, normals and uvs
  863. var position = Vector3.Zero();
  864. var normal = new Vector3(0, 1.0, 0);
  865. for (row = 0; row <= precision.h; row++) {
  866. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  867. for (col = 0; col <= precision.w; col++) {
  868. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  869. position.y = 0;
  870. positions.push(position.x, position.y, position.z);
  871. normals.push(normal.x, normal.y, normal.z);
  872. uvs.push(col / precision.w, row / precision.h);
  873. }
  874. }
  875. }
  876. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  877. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  878. applyTile(
  879. xmin + tileCol * tileSize.w,
  880. zmin + tileRow * tileSize.h,
  881. xmin + (tileCol + 1) * tileSize.w,
  882. zmin + (tileRow + 1) * tileSize.h
  883. );
  884. }
  885. }
  886. // Result
  887. var vertexData = new VertexData();
  888. vertexData.indices = indices;
  889. vertexData.positions = positions;
  890. vertexData.normals = normals;
  891. vertexData.uvs = uvs;
  892. return vertexData;
  893. }
  894. public static CreateGroundFromHeightMap(width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number): VertexData {
  895. var indices = [];
  896. var positions = [];
  897. var normals = [];
  898. var uvs = [];
  899. var row, col;
  900. // Vertices
  901. for (row = 0; row <= subdivisions; row++) {
  902. for (col = 0; col <= subdivisions; col++) {
  903. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  904. // Compute height
  905. var heightMapX = (((position.x + width / 2) / width) * (bufferWidth - 1)) | 0;
  906. var heightMapY = ((1.0 - (position.z + height / 2) / height) * (bufferHeight - 1)) | 0;
  907. var pos = (heightMapX + heightMapY * bufferWidth) * 4;
  908. var r = buffer[pos] / 255.0;
  909. var g = buffer[pos + 1] / 255.0;
  910. var b = buffer[pos + 2] / 255.0;
  911. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  912. position.y = minHeight + (maxHeight - minHeight) * gradient;
  913. // Add vertex
  914. positions.push(position.x, position.y, position.z);
  915. normals.push(0, 0, 0);
  916. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  917. }
  918. }
  919. // Indices
  920. for (row = 0; row < subdivisions; row++) {
  921. for (col = 0; col < subdivisions; col++) {
  922. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  923. indices.push(col + 1 + row * (subdivisions + 1));
  924. indices.push(col + row * (subdivisions + 1));
  925. indices.push(col + (row + 1) * (subdivisions + 1));
  926. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  927. indices.push(col + row * (subdivisions + 1));
  928. }
  929. }
  930. // Normals
  931. VertexData.ComputeNormals(positions, indices, normals);
  932. // Result
  933. var vertexData = new VertexData();
  934. vertexData.indices = indices;
  935. vertexData.positions = positions;
  936. vertexData.normals = normals;
  937. vertexData.uvs = uvs;
  938. return vertexData;
  939. }
  940. public static CreatePlane(size: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  941. var indices = [];
  942. var positions = [];
  943. var normals = [];
  944. var uvs = [];
  945. size = size || 1;
  946. // Vertices
  947. var halfSize = size / 2.0;
  948. positions.push(-halfSize, -halfSize, 0);
  949. normals.push(0, 0, -1.0);
  950. uvs.push(0.0, 0.0);
  951. positions.push(halfSize, -halfSize, 0);
  952. normals.push(0, 0, -1.0);
  953. uvs.push(1.0, 0.0);
  954. positions.push(halfSize, halfSize, 0);
  955. normals.push(0, 0, -1.0);
  956. uvs.push(1.0, 1.0);
  957. positions.push(-halfSize, halfSize, 0);
  958. normals.push(0, 0, -1.0);
  959. uvs.push(0.0, 1.0);
  960. // Indices
  961. indices.push(0);
  962. indices.push(1);
  963. indices.push(2);
  964. indices.push(0);
  965. indices.push(2);
  966. indices.push(3);
  967. // Sides
  968. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  969. // Result
  970. var vertexData = new VertexData();
  971. vertexData.indices = indices;
  972. vertexData.positions = positions;
  973. vertexData.normals = normals;
  974. vertexData.uvs = uvs;
  975. return vertexData;
  976. }
  977. public static CreateDisc(radius: number, tessellation: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  978. var positions = [];
  979. var indices = [];
  980. var normals = [];
  981. var uvs = [];
  982. // positions and uvs
  983. positions.push(0, 0, 0); // disc center first
  984. uvs.push(0.5, 0.5);
  985. var step = Math.PI * 2 / tessellation;
  986. for (var a = 0; a < Math.PI * 2; a += step) {
  987. var x = Math.cos(a);
  988. var y = Math.sin(a);
  989. var u = (x + 1) / 2;
  990. var v = (1 - y) / 2;
  991. positions.push(radius * x, radius * y, 0);
  992. uvs.push(u, v);
  993. }
  994. positions.push(positions[3], positions[4], positions[5]); // close the circle
  995. uvs.push(uvs[2], uvs[3]);
  996. //indices
  997. var vertexNb = positions.length / 3;
  998. for (var i = 1; i < vertexNb - 1; i++) {
  999. indices.push(i + 1, 0, i);
  1000. }
  1001. // result
  1002. VertexData.ComputeNormals(positions, indices, normals);
  1003. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1004. var vertexData = new VertexData();
  1005. vertexData.indices = indices;
  1006. vertexData.positions = positions;
  1007. vertexData.normals = normals;
  1008. vertexData.uvs = uvs;
  1009. return vertexData;
  1010. }
  1011. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1012. public static CreateTorusKnot(radius: number, tube: number, radialSegments: number, tubularSegments: number, p: number, q: number, sideOrientation: number = Mesh.DEFAULTSIDE): VertexData {
  1013. var indices = [];
  1014. var positions = [];
  1015. var normals = [];
  1016. var uvs = [];
  1017. radius = radius || 2;
  1018. tube = tube || 0.5;
  1019. radialSegments = radialSegments || 32;
  1020. tubularSegments = tubularSegments || 32;
  1021. p = p || 2;
  1022. q = q || 3;
  1023. // Helper
  1024. var getPos = (angle) => {
  1025. var cu = Math.cos(angle);
  1026. var su = Math.sin(angle);
  1027. var quOverP = q / p * angle;
  1028. var cs = Math.cos(quOverP);
  1029. var tx = radius * (2 + cs) * 0.5 * cu;
  1030. var ty = radius * (2 + cs) * su * 0.5;
  1031. var tz = radius * Math.sin(quOverP) * 0.5;
  1032. return new Vector3(tx, ty, tz);
  1033. };
  1034. // Vertices
  1035. for (var i = 0; i <= radialSegments; i++) {
  1036. var modI = i % radialSegments;
  1037. var u = modI / radialSegments * 2 * p * Math.PI;
  1038. var p1 = getPos(u);
  1039. var p2 = getPos(u + 0.01);
  1040. var tang = p2.subtract(p1);
  1041. var n = p2.add(p1);
  1042. var bitan = Vector3.Cross(tang, n);
  1043. n = Vector3.Cross(bitan, tang);
  1044. bitan.normalize();
  1045. n.normalize();
  1046. for (var j = 0; j < tubularSegments; j++) {
  1047. var modJ = j % tubularSegments;
  1048. var v = modJ / tubularSegments * 2 * Math.PI;
  1049. var cx = -tube * Math.cos(v);
  1050. var cy = tube * Math.sin(v);
  1051. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1052. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1053. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1054. uvs.push(i / radialSegments);
  1055. uvs.push(j / tubularSegments);
  1056. }
  1057. }
  1058. for (i = 0; i < radialSegments; i++) {
  1059. for (j = 0; j < tubularSegments; j++) {
  1060. var jNext = (j + 1) % tubularSegments;
  1061. var a = i * tubularSegments + j;
  1062. var b = (i + 1) * tubularSegments + j;
  1063. var c = (i + 1) * tubularSegments + jNext;
  1064. var d = i * tubularSegments + jNext;
  1065. indices.push(d); indices.push(b); indices.push(a);
  1066. indices.push(d); indices.push(c); indices.push(b);
  1067. }
  1068. }
  1069. // Normals
  1070. VertexData.ComputeNormals(positions, indices, normals);
  1071. // Sides
  1072. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1073. // Result
  1074. var vertexData = new VertexData();
  1075. vertexData.indices = indices;
  1076. vertexData.positions = positions;
  1077. vertexData.normals = normals;
  1078. vertexData.uvs = uvs;
  1079. return vertexData;
  1080. }
  1081. // Tools
  1082. /**
  1083. * @param {any} - positions (number[] or Float32Array)
  1084. * @param {any} - indices (number[] or Uint16Array)
  1085. * @param {any} - normals (number[] or Float32Array)
  1086. */
  1087. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1088. var index = 0;
  1089. // temp Vector3
  1090. var p1 = Vector3.Zero();
  1091. var p2 = Vector3.Zero();
  1092. var p3 = Vector3.Zero();
  1093. var p1p2 = Vector3.Zero();
  1094. var p3p2 = Vector3.Zero();
  1095. var faceNormal = Vector3.Zero();
  1096. var vertexNormali1 = Vector3.Zero();
  1097. var vertexNormali2 = Vector3.Zero();
  1098. var vertexNormali3 = Vector3.Zero();
  1099. // indice triplet = 1 face
  1100. var nbFaces = indices.length / 3;
  1101. for (index = 0; index < nbFaces; index++) {
  1102. var i1 = indices[index * 3];
  1103. var i2 = indices[index * 3 + 1];
  1104. var i3 = indices[index * 3 + 2];
  1105. // setting the temp V3
  1106. Vector3.FromFloatsToRef(positions[i1 * 3], positions[i1 * 3 + 1], positions[i1 * 3 + 2], p1);
  1107. Vector3.FromFloatsToRef(positions[i2 * 3], positions[i2 * 3 + 1], positions[i2 * 3 + 2], p2);
  1108. Vector3.FromFloatsToRef(positions[i3 * 3], positions[i3 * 3 + 1], positions[i3 * 3 + 2], p3);
  1109. p1.subtractToRef(p2, p1p2);
  1110. p3.subtractToRef(p2, p3p2);
  1111. Vector3.CrossToRef(p1p2, p3p2, faceNormal);
  1112. faceNormal.normalize();
  1113. // All intermediate results are stored in the normals array :
  1114. // get the normals at i1, i2 and i3 indexes
  1115. normals[i1 * 3] = normals[i1 * 3] || 0.0;
  1116. normals[i1 * 3 + 1] = normals[i1 * 3 + 1] || 0.0;
  1117. normals[i1 * 3 + 2] = normals[i1 * 3 + 2] || 0.0;
  1118. normals[i2 * 3] = normals[i2 * 3] || 0.0;
  1119. normals[i2 * 3 + 1] = normals[i2 * 3 + 1] || 0.0;
  1120. normals[i2 * 3 + 2] = normals[i2 * 3 + 2] || 0.0;
  1121. normals[i3 * 3] = normals[i3 * 3] || 0.0;
  1122. normals[i3 * 3 + 1] = normals[i3 * 3 + 1] || 0.0;
  1123. normals[i3 * 3 + 2] = normals[i3 * 3 + 2] || 0.0;
  1124. // make intermediate vectors3 from normals values
  1125. Vector3.FromFloatsToRef(normals[i1 * 3], normals[i1 * 3 + 1], normals[i1 * 3 + 2], vertexNormali1);
  1126. Vector3.FromFloatsToRef(normals[i2 * 3], normals[i2 * 3 + 1], normals[i2 * 3 + 2], vertexNormali2);
  1127. Vector3.FromFloatsToRef(normals[i3 * 3], normals[i3 * 3 + 1], normals[i3 * 3 + 2], vertexNormali3);
  1128. // add the current face normals to these intermediate vectors3
  1129. vertexNormali1 = vertexNormali1.addInPlace(faceNormal);
  1130. vertexNormali2 = vertexNormali2.addInPlace(faceNormal);
  1131. vertexNormali3 = vertexNormali3.addInPlace(faceNormal);
  1132. // store back intermediate vectors3 into the normals array
  1133. normals[i1 * 3] = vertexNormali1.x;
  1134. normals[i1 * 3 + 1] = vertexNormali1.y;
  1135. normals[i1 * 3 + 2] = vertexNormali1.z;
  1136. normals[i2 * 3] = vertexNormali2.x;
  1137. normals[i2 * 3 + 1] = vertexNormali2.y;
  1138. normals[i2 * 3 + 2] = vertexNormali2.z;
  1139. normals[i3 * 3] = vertexNormali3.x;
  1140. normals[i3 * 3 + 1] = vertexNormali3.y;
  1141. normals[i3 * 3 + 2] = vertexNormali3.z;
  1142. }
  1143. // last normalization
  1144. for (index = 0; index < normals.length / 3; index++) {
  1145. Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
  1146. vertexNormali1.normalize();
  1147. normals[index * 3] = vertexNormali1.x;
  1148. normals[index * 3 + 1] = vertexNormali1.y;
  1149. normals[index * 3 + 2] = vertexNormali1.z;
  1150. }
  1151. }
  1152. private static _ComputeSides(sideOrientation: number, positions: number[], indices: number[], normals: number[], uvs: number[]) {
  1153. var li: number = indices.length;
  1154. var ln: number = normals.length;
  1155. var i: number;
  1156. var n: number;
  1157. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1158. switch (sideOrientation) {
  1159. case Mesh.FRONTSIDE:
  1160. // nothing changed
  1161. break;
  1162. case Mesh.BACKSIDE:
  1163. var tmp: number;
  1164. // indices
  1165. for (i = 0; i < li; i += 3) {
  1166. tmp = indices[i];
  1167. indices[i] = indices[i + 2];
  1168. indices[i + 2] = tmp;
  1169. }
  1170. // normals
  1171. for (n = 0; n < ln; n++) {
  1172. normals[n] = -normals[n];
  1173. }
  1174. break;
  1175. case Mesh.DOUBLESIDE:
  1176. // positions
  1177. var lp: number = positions.length;
  1178. var l: number = lp / 3;
  1179. for (var p = 0; p < lp; p++) {
  1180. positions[lp + p] = positions[p];
  1181. }
  1182. // indices
  1183. for (i = 0; i < li; i += 3) {
  1184. indices[i + li] = indices[i + 2] + l;
  1185. indices[i + 1 + li] = indices[i + 1] + l;
  1186. indices[i + 2 + li] = indices[i] + l;
  1187. }
  1188. // normals
  1189. for (n = 0; n < ln; n++) {
  1190. normals[ln + n] = -normals[n];
  1191. }
  1192. // uvs
  1193. var lu: number = uvs.length;
  1194. for (var u: number = 0; u < lu; u++) {
  1195. uvs[u + lu] = uvs[u];
  1196. }
  1197. break;
  1198. }
  1199. }
  1200. }
  1201. }