babylon.mesh.vertexData.ts 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  1. module BABYLON {
  2. export interface IGetSetVerticesData {
  3. isVerticesDataPresent(kind: string): boolean;
  4. getVerticesData(kind: string, copyWhenShared?: boolean): number[] | Int32Array | Float32Array;
  5. getIndices(copyWhenShared?: boolean): number[] | Int32Array;
  6. setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean): void;
  7. updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): void;
  8. setIndices(indices: number[] | Int32Array): void;
  9. }
  10. export class VertexData {
  11. public positions: number[] | Float32Array;
  12. public normals: number[] | Float32Array;
  13. public uvs: number[] | Float32Array;
  14. public uvs2: number[] | Float32Array;
  15. public uvs3: number[] | Float32Array;
  16. public uvs4: number[] | Float32Array;
  17. public uvs5: number[] | Float32Array;
  18. public uvs6: number[] | Float32Array;
  19. public colors: number[] | Float32Array;
  20. public matricesIndices: number[] | Float32Array;
  21. public matricesWeights: number[] | Float32Array;
  22. public matricesIndicesExtra: number[] | Float32Array;
  23. public matricesWeightsExtra: number[] | Float32Array;
  24. public indices: number[] | Int32Array;
  25. public set(data: number[] | Float32Array, kind: string) {
  26. switch (kind) {
  27. case VertexBuffer.PositionKind:
  28. this.positions = data;
  29. break;
  30. case VertexBuffer.NormalKind:
  31. this.normals = data;
  32. break;
  33. case VertexBuffer.UVKind:
  34. this.uvs = data;
  35. break;
  36. case VertexBuffer.UV2Kind:
  37. this.uvs2 = data;
  38. break;
  39. case VertexBuffer.UV3Kind:
  40. this.uvs3 = data;
  41. break;
  42. case VertexBuffer.UV4Kind:
  43. this.uvs4 = data;
  44. break;
  45. case VertexBuffer.UV5Kind:
  46. this.uvs5 = data;
  47. break;
  48. case VertexBuffer.UV6Kind:
  49. this.uvs6 = data;
  50. break;
  51. case VertexBuffer.ColorKind:
  52. this.colors = data;
  53. break;
  54. case VertexBuffer.MatricesIndicesKind:
  55. this.matricesIndices = data;
  56. break;
  57. case VertexBuffer.MatricesWeightsKind:
  58. this.matricesWeights = data;
  59. break;
  60. case VertexBuffer.MatricesIndicesExtraKind:
  61. this.matricesIndicesExtra = data;
  62. break;
  63. case VertexBuffer.MatricesWeightsExtraKind:
  64. this.matricesWeightsExtra = data;
  65. break;
  66. }
  67. }
  68. public applyToMesh(mesh: Mesh, updatable?: boolean): void {
  69. this._applyTo(mesh, updatable);
  70. }
  71. public applyToGeometry(geometry: Geometry, updatable?: boolean): void {
  72. this._applyTo(geometry, updatable);
  73. }
  74. public updateMesh(mesh: Mesh, updateExtends?: boolean, makeItUnique?: boolean): void {
  75. this._update(mesh);
  76. }
  77. public updateGeometry(geometry: Geometry, updateExtends?: boolean, makeItUnique?: boolean): void {
  78. this._update(geometry);
  79. }
  80. private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean) {
  81. if (this.positions) {
  82. meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
  83. }
  84. if (this.normals) {
  85. meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
  86. }
  87. if (this.uvs) {
  88. meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
  89. }
  90. if (this.uvs2) {
  91. meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
  92. }
  93. if (this.uvs3) {
  94. meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
  95. }
  96. if (this.uvs4) {
  97. meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
  98. }
  99. if (this.uvs5) {
  100. meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
  101. }
  102. if (this.uvs6) {
  103. meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
  104. }
  105. if (this.colors) {
  106. meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
  107. }
  108. if (this.matricesIndices) {
  109. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  110. }
  111. if (this.matricesWeights) {
  112. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  113. }
  114. if (this.matricesIndicesExtra) {
  115. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updatable);
  116. }
  117. if (this.matricesWeightsExtra) {
  118. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updatable);
  119. }
  120. if (this.indices) {
  121. meshOrGeometry.setIndices(this.indices);
  122. }
  123. }
  124. private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean) {
  125. if (this.positions) {
  126. meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  127. }
  128. if (this.normals) {
  129. meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  130. }
  131. if (this.uvs) {
  132. meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  133. }
  134. if (this.uvs2) {
  135. meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  136. }
  137. if (this.uvs3) {
  138. meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  139. }
  140. if (this.uvs4) {
  141. meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  142. }
  143. if (this.uvs5) {
  144. meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  145. }
  146. if (this.uvs6) {
  147. meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  148. }
  149. if (this.colors) {
  150. meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  151. }
  152. if (this.matricesIndices) {
  153. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  154. }
  155. if (this.matricesWeights) {
  156. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  157. }
  158. if (this.matricesIndicesExtra) {
  159. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updateExtends, makeItUnique);
  160. }
  161. if (this.matricesWeightsExtra) {
  162. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updateExtends, makeItUnique);
  163. }
  164. if (this.indices) {
  165. meshOrGeometry.setIndices(this.indices);
  166. }
  167. }
  168. public transform(matrix: Matrix): void {
  169. var transformed = Vector3.Zero();
  170. var index: number;
  171. if (this.positions) {
  172. var position = Vector3.Zero();
  173. for (index = 0; index < this.positions.length; index += 3) {
  174. Vector3.FromArrayToRef(this.positions, index, position);
  175. Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  176. this.positions[index] = transformed.x;
  177. this.positions[index + 1] = transformed.y;
  178. this.positions[index + 2] = transformed.z;
  179. }
  180. }
  181. if (this.normals) {
  182. var normal = Vector3.Zero();
  183. for (index = 0; index < this.normals.length; index += 3) {
  184. Vector3.FromArrayToRef(this.normals, index, normal);
  185. Vector3.TransformNormalToRef(normal, matrix, transformed);
  186. this.normals[index] = transformed.x;
  187. this.normals[index + 1] = transformed.y;
  188. this.normals[index + 2] = transformed.z;
  189. }
  190. }
  191. }
  192. public merge(other: VertexData): void {
  193. if (other.indices) {
  194. if (!this.indices) {
  195. this.indices = [];
  196. }
  197. var offset = this.positions ? this.positions.length / 3 : 0;
  198. for (var index = 0; index < other.indices.length; index++) {
  199. //TODO check type - if Int32Array!
  200. (<number[]>this.indices).push(other.indices[index] + offset);
  201. }
  202. }
  203. this.positions = this._mergeElement(this.positions, other.positions);
  204. this.normals = this._mergeElement(this.normals, other.normals);
  205. this.uvs = this._mergeElement(this.uvs, other.uvs);
  206. this.uvs2 = this._mergeElement(this.uvs2, other.uvs2);
  207. this.uvs3 = this._mergeElement(this.uvs3, other.uvs3);
  208. this.uvs4 = this._mergeElement(this.uvs4, other.uvs4);
  209. this.uvs5 = this._mergeElement(this.uvs5, other.uvs5);
  210. this.uvs6 = this._mergeElement(this.uvs6, other.uvs6);
  211. this.colors = this._mergeElement(this.colors, other.colors);
  212. this.matricesIndices = this._mergeElement(this.matricesIndices, other.matricesIndices);
  213. this.matricesWeights = this._mergeElement(this.matricesWeights, other.matricesWeights);
  214. this.matricesIndicesExtra = this._mergeElement(this.matricesIndicesExtra, other.matricesIndicesExtra);
  215. this.matricesWeightsExtra = this._mergeElement(this.matricesWeightsExtra, other.matricesWeightsExtra);
  216. }
  217. private _mergeElement(source: number[] | Float32Array, other: number[] | Float32Array): number[] | Float32Array {
  218. if (!other) return source;
  219. if (!source) return other;
  220. var len = other.length + source.length;
  221. var isSrcTypedArray = source instanceof Float32Array;
  222. var isOthTypedArray = other instanceof Float32Array;
  223. // use non-loop method when the source is Float32Array
  224. if (isSrcTypedArray) {
  225. var ret32 = new Float32Array(len);
  226. ret32.set(source);
  227. ret32.set(other, source.length);
  228. return ret32;
  229. // source is number[], when other is also use concat
  230. } else if (!isOthTypedArray) {
  231. return (<number[]>source).concat(<number[]>other);
  232. // source is a number[], but other is a Float32Array, loop required
  233. } else {
  234. var ret = (<number[]>source).slice(0); // copy source to a separate array
  235. for (var i = 0, len = other.length; i < len; i++) {
  236. ret.push(other[i]);
  237. }
  238. return ret;
  239. }
  240. }
  241. public serialize(): any {
  242. var serializationObject = this.serialize();
  243. if (this.positions) {
  244. serializationObject.positions = this.positions;
  245. }
  246. if (this.normals) {
  247. serializationObject.normals = this.normals;
  248. }
  249. if (this.uvs) {
  250. serializationObject.uvs = this.uvs;
  251. }
  252. if (this.uvs2) {
  253. serializationObject.uvs2 = this.uvs2;
  254. }
  255. if (this.uvs3) {
  256. serializationObject.uvs3 = this.uvs3;
  257. }
  258. if (this.uvs4) {
  259. serializationObject.uvs4 = this.uvs4;
  260. }
  261. if (this.uvs5) {
  262. serializationObject.uvs5 = this.uvs5;
  263. }
  264. if (this.uvs6) {
  265. serializationObject.uvs6 = this.uvs6;
  266. }
  267. if (this.colors) {
  268. serializationObject.colors = this.colors;
  269. }
  270. if (this.matricesIndices) {
  271. serializationObject.matricesIndices = this.matricesIndices;
  272. serializationObject.matricesIndices._isExpanded = true;
  273. }
  274. if (this.matricesWeights) {
  275. serializationObject.matricesWeights = this.matricesWeights;
  276. }
  277. if (this.matricesIndicesExtra) {
  278. serializationObject.matricesIndicesExtra = this.matricesIndicesExtra;
  279. serializationObject.matricesIndicesExtra._isExpanded = true;
  280. }
  281. if (this.matricesWeightsExtra) {
  282. serializationObject.matricesWeightsExtra = this.matricesWeightsExtra;
  283. }
  284. serializationObject.indices = this.indices;
  285. return serializationObject;
  286. }
  287. // Statics
  288. public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean): VertexData {
  289. return VertexData._ExtractFrom(mesh, copyWhenShared);
  290. }
  291. public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean): VertexData {
  292. return VertexData._ExtractFrom(geometry, copyWhenShared);
  293. }
  294. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean): VertexData {
  295. var result = new VertexData();
  296. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  297. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared);
  298. }
  299. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  300. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared);
  301. }
  302. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  303. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared);
  304. }
  305. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  306. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared);
  307. }
  308. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  309. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared);
  310. }
  311. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  312. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared);
  313. }
  314. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  315. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared);
  316. }
  317. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  318. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared);
  319. }
  320. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  321. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared);
  322. }
  323. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  324. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared);
  325. }
  326. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  327. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared);
  328. }
  329. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesExtraKind)) {
  330. result.matricesIndicesExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesExtraKind, copyWhenShared);
  331. }
  332. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsExtraKind)) {
  333. result.matricesWeightsExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsExtraKind, copyWhenShared);
  334. }
  335. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  336. return result;
  337. }
  338. public static CreateRibbon(options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, sideOrientation?: number }): VertexData {
  339. var pathArray: Vector3[][] = options.pathArray;
  340. var closeArray: boolean = options.closeArray || false;
  341. var closePath: boolean = options.closePath || false;
  342. var defaultOffset: number = Math.floor(pathArray[0].length / 2);
  343. var offset: number = options.offset || defaultOffset;
  344. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  345. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  346. var positions: number[] = [];
  347. var indices: number[] = [];
  348. var normals: number[] = [];
  349. var uvs: number[] = [];
  350. var us: number[][] = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  351. var vs: number[][] = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  352. var uTotalDistance: number[] = []; // uTotalDistance[p] : total distance of path p
  353. var vTotalDistance: number[] = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  354. var minlg: number; // minimal length among all paths from pathArray
  355. var lg: number[] = []; // array of path lengths : nb of vertex per path
  356. var idx: number[] = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  357. var p: number; // path iterator
  358. var i: number; // point iterator
  359. var j: number; // point iterator
  360. // if single path in pathArray
  361. if (pathArray.length < 2) {
  362. var ar1: Vector3[] = [];
  363. var ar2: Vector3[] = [];
  364. for (i = 0; i < pathArray[0].length - offset; i++) {
  365. ar1.push(pathArray[0][i]);
  366. ar2.push(pathArray[0][i + offset]);
  367. }
  368. pathArray = [ar1, ar2];
  369. }
  370. // positions and horizontal distances (u)
  371. var idc: number = 0;
  372. var closePathCorr: number = (closePath) ? 1 : 0;
  373. var path: Vector3[];
  374. var l: number;
  375. minlg = pathArray[0].length;
  376. var vectlg: number;
  377. var dist: number;
  378. for (p = 0; p < pathArray.length; p++) {
  379. uTotalDistance[p] = 0;
  380. us[p] = [0];
  381. path = pathArray[p];
  382. l = path.length;
  383. minlg = (minlg < l) ? minlg : l;
  384. j = 0;
  385. while (j < l) {
  386. positions.push(path[j].x, path[j].y, path[j].z);
  387. if (j > 0) {
  388. vectlg = path[j].subtract(path[j - 1]).length();
  389. dist = vectlg + uTotalDistance[p];
  390. us[p].push(dist);
  391. uTotalDistance[p] = dist;
  392. }
  393. j++;
  394. }
  395. if (closePath) {
  396. j--;
  397. positions.push(path[0].x, path[0].y, path[0].z);
  398. vectlg = path[j].subtract(path[0]).length();
  399. dist = vectlg + uTotalDistance[p];
  400. us[p].push(dist);
  401. uTotalDistance[p] = dist;
  402. }
  403. lg[p] = l + closePathCorr;
  404. idx[p] = idc;
  405. idc += (l + closePathCorr);
  406. }
  407. // vertical distances (v)
  408. var path1: Vector3[];
  409. var path2: Vector3[];
  410. var vertex1: Vector3;
  411. var vertex2: Vector3;
  412. for (i = 0; i < minlg + closePathCorr; i++) {
  413. vTotalDistance[i] = 0;
  414. vs[i] = [0];
  415. for (p = 0; p < pathArray.length - 1; p++) {
  416. path1 = pathArray[p];
  417. path2 = pathArray[p + 1];
  418. if (i === minlg) { // closePath
  419. vertex1 = path1[0];
  420. vertex2 = path2[0];
  421. }
  422. else {
  423. vertex1 = path1[i];
  424. vertex2 = path2[i];
  425. }
  426. vectlg = vertex2.subtract(vertex1).length();
  427. dist = vectlg + vTotalDistance[i];
  428. vs[i].push(dist);
  429. vTotalDistance[i] = dist;
  430. }
  431. if (closeArray) {
  432. path1 = pathArray[p];
  433. path2 = pathArray[0];
  434. if (i === minlg) { // closePath
  435. vertex2 = path2[0];
  436. }
  437. vectlg = vertex2.subtract(vertex1).length();
  438. dist = vectlg + vTotalDistance[i];
  439. vTotalDistance[i] = dist;
  440. }
  441. }
  442. // uvs
  443. var u: number;
  444. var v: number;
  445. for (p = 0; p < pathArray.length; p++) {
  446. for (i = 0; i < minlg + closePathCorr; i++) {
  447. u = us[p][i] / uTotalDistance[p];
  448. v = vs[i][p] / vTotalDistance[i];
  449. uvs.push(u, v);
  450. }
  451. }
  452. // indices
  453. p = 0; // path index
  454. var pi: number = 0; // positions array index
  455. var l1: number = lg[p] - 1; // path1 length
  456. var l2: number = lg[p + 1] - 1; // path2 length
  457. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  458. var shft: number = idx[1] - idx[0]; // shift
  459. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  460. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  461. // 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
  462. indices.push(pi, pi + shft, pi + 1);
  463. indices.push(pi + shft + 1, pi + 1, pi + shft);
  464. pi += 1;
  465. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  466. p++;
  467. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  468. shft = idx[0] - idx[p];
  469. l1 = lg[p] - 1;
  470. l2 = lg[0] - 1;
  471. }
  472. else {
  473. shft = idx[p + 1] - idx[p];
  474. l1 = lg[p] - 1;
  475. l2 = lg[p + 1] - 1;
  476. }
  477. pi = idx[p];
  478. min = (l1 < l2) ? l1 + pi : l2 + pi;
  479. }
  480. }
  481. // normals
  482. VertexData.ComputeNormals(positions, indices, normals);
  483. if (closePath) {
  484. var indexFirst: number = 0;
  485. var indexLast: number = 0;
  486. for (p = 0; p < pathArray.length; p++) {
  487. indexFirst = idx[p] * 3;
  488. if (p + 1 < pathArray.length) {
  489. indexLast = (idx[p + 1] - 1) * 3;
  490. }
  491. else {
  492. indexLast = normals.length - 3;
  493. }
  494. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  495. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  496. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  497. normals[indexLast] = normals[indexFirst];
  498. normals[indexLast + 1] = normals[indexFirst + 1];
  499. normals[indexLast + 2] = normals[indexFirst + 2];
  500. }
  501. }
  502. // sides
  503. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  504. // Result
  505. var vertexData = new VertexData();
  506. vertexData.indices = indices;
  507. vertexData.positions = positions;
  508. vertexData.normals = normals;
  509. vertexData.uvs = uvs;
  510. if (closePath) {
  511. (<any>vertexData)._idx = idx;
  512. }
  513. return vertexData;
  514. }
  515. public static CreateBox(options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number }): VertexData {
  516. var normalsSource = [
  517. new Vector3(0, 0, 1),
  518. new Vector3(0, 0, -1),
  519. new Vector3(1, 0, 0),
  520. new Vector3(-1, 0, 0),
  521. new Vector3(0, 1, 0),
  522. new Vector3(0, -1, 0)
  523. ];
  524. var indices = [];
  525. var positions = [];
  526. var normals = [];
  527. var uvs = [];
  528. var width = options.width || options.size || 1;
  529. var height = options.height || options.size || 1;
  530. var depth = options.depth || options.size || 1;
  531. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  532. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(6);
  533. var faceColors: Color4[] = options.faceColors;
  534. var colors = [];
  535. // default face colors and UV if undefined
  536. for (var f = 0; f < 6; f++) {
  537. if (faceUV[f] === undefined) {
  538. faceUV[f] = new Vector4(0, 0, 1, 1);
  539. }
  540. if (faceColors && faceColors[f] === undefined) {
  541. faceColors[f] = new Color4(1, 1, 1, 1);
  542. }
  543. }
  544. var scaleVector = new Vector3(width / 2, height / 2, depth / 2);
  545. // Create each face in turn.
  546. for (var index = 0; index < normalsSource.length; index++) {
  547. var normal = normalsSource[index];
  548. // Get two vectors perpendicular to the face normal and to each other.
  549. var side1 = new Vector3(normal.y, normal.z, normal.x);
  550. var side2 = Vector3.Cross(normal, side1);
  551. // Six indices (two triangles) per face.
  552. var verticesLength = positions.length / 3;
  553. indices.push(verticesLength);
  554. indices.push(verticesLength + 1);
  555. indices.push(verticesLength + 2);
  556. indices.push(verticesLength);
  557. indices.push(verticesLength + 2);
  558. indices.push(verticesLength + 3);
  559. // Four vertices per face.
  560. var vertex = normal.subtract(side1).subtract(side2).multiply(scaleVector);
  561. positions.push(vertex.x, vertex.y, vertex.z);
  562. normals.push(normal.x, normal.y, normal.z);
  563. uvs.push(faceUV[index].z, faceUV[index].w);
  564. if (faceColors) {
  565. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  566. }
  567. vertex = normal.subtract(side1).add(side2).multiply(scaleVector);
  568. positions.push(vertex.x, vertex.y, vertex.z);
  569. normals.push(normal.x, normal.y, normal.z);
  570. uvs.push(faceUV[index].x, faceUV[index].w);
  571. if (faceColors) {
  572. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  573. }
  574. vertex = normal.add(side1).add(side2).multiply(scaleVector);
  575. positions.push(vertex.x, vertex.y, vertex.z);
  576. normals.push(normal.x, normal.y, normal.z);
  577. uvs.push(faceUV[index].x, faceUV[index].y);
  578. if (faceColors) {
  579. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  580. }
  581. vertex = normal.add(side1).subtract(side2).multiply(scaleVector);
  582. positions.push(vertex.x, vertex.y, vertex.z);
  583. normals.push(normal.x, normal.y, normal.z);
  584. uvs.push(faceUV[index].z, faceUV[index].y);
  585. if (faceColors) {
  586. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  587. }
  588. }
  589. // sides
  590. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  591. // Result
  592. var vertexData = new VertexData();
  593. vertexData.indices = indices;
  594. vertexData.positions = positions;
  595. vertexData.normals = normals;
  596. vertexData.uvs = uvs;
  597. if (faceColors) {
  598. var totalColors = (sideOrientation === Mesh.DOUBLESIDE) ? colors.concat(colors) : colors;
  599. vertexData.colors = totalColors;
  600. }
  601. return vertexData;
  602. }
  603. public static CreateSphere(options: { segments?: number, diameter?: number, diameterX?: number, diameterY?: number, diameterZ?: number, arc?: number, slice?: number, sideOrientation?: number }): VertexData {
  604. var segments: number = options.segments || 32;
  605. var diameterX: number = options.diameterX || options.diameter || 1;
  606. var diameterY: number = options.diameterY || options.diameter || 1;
  607. var diameterZ: number = options.diameterZ || options.diameter || 1;
  608. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  609. var slice: number = (options.slice <= 0) ? 1.0 : options.slice || 1.0;
  610. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  611. var radius = new Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  612. var totalZRotationSteps = 2 + segments;
  613. var totalYRotationSteps = 2 * totalZRotationSteps;
  614. var indices = [];
  615. var positions = [];
  616. var normals = [];
  617. var uvs = [];
  618. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  619. var normalizedZ = zRotationStep / totalZRotationSteps;
  620. var angleZ = normalizedZ * Math.PI * slice;
  621. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  622. var normalizedY = yRotationStep / totalYRotationSteps;
  623. var angleY = normalizedY * Math.PI * 2 * arc;
  624. var rotationZ = Matrix.RotationZ(-angleZ);
  625. var rotationY = Matrix.RotationY(angleY);
  626. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  627. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  628. var vertex = complete.multiply(radius);
  629. var normal = complete.divide(radius).normalize();
  630. positions.push(vertex.x, vertex.y, vertex.z);
  631. normals.push(normal.x, normal.y, normal.z);
  632. uvs.push(normalizedY, normalizedZ);
  633. }
  634. if (zRotationStep > 0) {
  635. var verticesCount = positions.length / 3;
  636. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  637. indices.push((firstIndex));
  638. indices.push((firstIndex + 1));
  639. indices.push(firstIndex + totalYRotationSteps + 1);
  640. indices.push((firstIndex + totalYRotationSteps + 1));
  641. indices.push((firstIndex + 1));
  642. indices.push((firstIndex + totalYRotationSteps + 2));
  643. }
  644. }
  645. }
  646. // Sides
  647. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  648. // Result
  649. var vertexData = new VertexData();
  650. vertexData.indices = indices;
  651. vertexData.positions = positions;
  652. vertexData.normals = normals;
  653. vertexData.uvs = uvs;
  654. return vertexData;
  655. }
  656. // Cylinder and cone
  657. public static CreateCylinder(options: { height?: number, diameterTop?: number, diameterBottom?: number, diameter?: number, tessellation?: number, subdivisions?: number, arc?: number, faceColors?: Color4[], faceUV?: Vector4[], hasRings?: boolean, enclose?: boolean, sideOrientation?: number }): VertexData {
  658. var height: number = options.height || 2;
  659. var diameterTop: number = (options.diameterTop === 0) ? 0 : options.diameterTop || options.diameter || 1;
  660. var diameterBottom: number = options.diameterBottom || options.diameter || 1;
  661. var tessellation: number = options.tessellation || 24;
  662. var subdivisions: number = options.subdivisions || 1;
  663. var hasRings: boolean = options.hasRings;
  664. var enclose: boolean = options.enclose;
  665. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  666. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  667. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(3);
  668. var faceColors: Color4[] = options.faceColors;
  669. // default face colors and UV if undefined
  670. var quadNb: number = (arc !== 1 && enclose) ? 2 : 0;
  671. var ringNb: number = (hasRings) ? subdivisions : 1;
  672. var surfaceNb: number = 2 + (1 + quadNb) * ringNb;
  673. var f: number;
  674. for (f = 0; f < surfaceNb; f++) {
  675. if (faceColors && faceColors[f] === undefined) {
  676. faceColors[f] = new Color4(1, 1, 1, 1);
  677. }
  678. }
  679. for (f = 0; f < surfaceNb; f++) {
  680. if (faceUV && faceUV[f] === undefined) {
  681. faceUV[f] = new Vector4(0, 0, 1, 1);
  682. }
  683. }
  684. var indices = [];
  685. var positions = [];
  686. var normals = [];
  687. var uvs = [];
  688. var colors = [];
  689. var angle_step = Math.PI * 2 * arc / tessellation;
  690. var angle: number;
  691. var h: number;
  692. var radius: number;
  693. var tan = (diameterBottom - diameterTop) / 2 / height;
  694. var ringVertex: Vector3 = Vector3.Zero();
  695. var ringNormal: Vector3 = Vector3.Zero();
  696. var ringFirstVertex: Vector3 = Vector3.Zero();
  697. var ringFirstNormal: Vector3 = Vector3.Zero();
  698. var quadNormal: Vector3 = Vector3.Zero();
  699. var Y: Vector3 = Axis.Y;
  700. // positions, normals, uvs
  701. var i: number;
  702. var j: number;
  703. var r: number;
  704. var ringIdx: number = 1;
  705. var s: number = 1; // surface index
  706. var cs: number = 0;
  707. var v: number = 0;
  708. for (i = 0; i <= subdivisions; i++) {
  709. h = i / subdivisions;
  710. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  711. ringIdx = (hasRings && i !== 0 && i !== subdivisions) ? 2 : 1;
  712. for (r = 0; r < ringIdx; r++) {
  713. if (hasRings) {
  714. s += r;
  715. }
  716. if (enclose) {
  717. s += 2 * r;
  718. }
  719. for (j = 0; j <= tessellation; j++) {
  720. angle = j * angle_step;
  721. // position
  722. ringVertex.x = Math.cos(-angle) * radius;
  723. ringVertex.y = -height / 2 + h * height;
  724. ringVertex.z = Math.sin(-angle) * radius;
  725. // normal
  726. if (diameterTop === 0 && i === subdivisions) {
  727. // if no top cap, reuse former normals
  728. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  729. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  730. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  731. }
  732. else {
  733. ringNormal.x = ringVertex.x;
  734. ringNormal.z = ringVertex.z;
  735. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  736. ringNormal.normalize();
  737. }
  738. // keep first ring vertex values for enclose
  739. if (j === 0) {
  740. ringFirstVertex.copyFrom(ringVertex);
  741. ringFirstNormal.copyFrom(ringNormal);
  742. }
  743. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  744. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  745. if (hasRings) {
  746. v = (cs !== s) ? faceUV[s].y : faceUV[s].w;
  747. } else {
  748. v = faceUV[s].y + (faceUV[s].w - faceUV[s].y) * h;
  749. }
  750. uvs.push(faceUV[s].x + (faceUV[s].z - faceUV[s].x) * j / tessellation, v);
  751. if (faceColors) {
  752. colors.push(faceColors[s].r, faceColors[s].g, faceColors[s].b, faceColors[s].a);
  753. }
  754. }
  755. // if enclose, add four vertices and their dedicated normals
  756. if (arc !== 1 && enclose) {
  757. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  758. positions.push(0, ringVertex.y, 0);
  759. positions.push(0, ringVertex.y, 0);
  760. positions.push(ringFirstVertex.x, ringFirstVertex.y, ringFirstVertex.z);
  761. Vector3.CrossToRef(Y, ringNormal, quadNormal);
  762. quadNormal.normalize();
  763. normals.push(quadNormal.x, quadNormal.y, quadNormal.z, quadNormal.x, quadNormal.y, quadNormal.z);
  764. Vector3.CrossToRef(ringFirstNormal, Y, quadNormal);
  765. quadNormal.normalize();
  766. normals.push(quadNormal.x, quadNormal.y, quadNormal.z, quadNormal.x, quadNormal.y, quadNormal.z);
  767. if (hasRings) {
  768. v = (cs !== s) ? faceUV[s + 1].y : faceUV[s + 1].w;
  769. } else {
  770. v = faceUV[s + 1].y + (faceUV[s + 1].w - faceUV[s + 1].y) * h;
  771. }
  772. uvs.push(faceUV[s + 1].x, v);
  773. uvs.push(faceUV[s + 1].z, v);
  774. if (hasRings) {
  775. v = (cs !== s) ? faceUV[s + 2].y : faceUV[s + 2].w;
  776. } else {
  777. v = faceUV[s + 2].y + (faceUV[s + 2].w - faceUV[s + 2].y) * h;
  778. }
  779. uvs.push(faceUV[s + 2].x, v);
  780. uvs.push(faceUV[s + 2].z, v);
  781. if (faceColors) {
  782. colors.push(faceColors[s + 1].r, faceColors[s + 1].g, faceColors[s + 1].b, faceColors[s + 1].a);
  783. colors.push(faceColors[s + 1].r, faceColors[s + 1].g, faceColors[s + 1].b, faceColors[s + 1].a);
  784. colors.push(faceColors[s + 2].r, faceColors[s + 2].g, faceColors[s + 2].b, faceColors[s + 2].a);
  785. colors.push(faceColors[s + 2].r, faceColors[s + 2].g, faceColors[s + 2].b, faceColors[s + 2].a);
  786. }
  787. }
  788. if (cs !== s) {
  789. cs = s;
  790. }
  791. }
  792. }
  793. // indices
  794. var e: number = (arc !== 1 && enclose) ? tessellation + 4 : tessellation; // correction of number of iteration if enclose
  795. var s: number;
  796. i = 0;
  797. for (s = 0; s < subdivisions; s++) {
  798. for (j = 0; j < tessellation; j++) {
  799. var i0 = i * (e + 1) + j;
  800. var i1 = (i + 1) * (e + 1) + j;
  801. var i2 = i * (e + 1) + (j + 1);
  802. var i3 = (i + 1) * (e + 1) + (j + 1);
  803. indices.push(i0, i1, i2);
  804. indices.push(i3, i2, i1);
  805. }
  806. if (arc !== 1 && enclose) { // if enclose, add two quads
  807. indices.push(i0 + 2, i1 + 2, i2 + 2);
  808. indices.push(i3 + 2, i2 + 2, i1 + 2);
  809. indices.push(i0 + 4, i1 + 4, i2 + 4);
  810. indices.push(i3 + 4, i2 + 4, i1 + 4);
  811. }
  812. i = (hasRings) ? (i + 2) : (i + 1);
  813. }
  814. // Caps
  815. var createCylinderCap = isTop => {
  816. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  817. if (radius === 0) {
  818. return;
  819. }
  820. // Cap positions, normals & uvs
  821. var angle;
  822. var circleVector;
  823. var i: number;
  824. var u: Vector4 = (isTop) ? faceUV[surfaceNb - 1] : faceUV[0];
  825. var c: Color4;
  826. if (faceColors) {
  827. c = (isTop) ? faceColors[surfaceNb - 1] : faceColors[0];
  828. }
  829. // cap center
  830. var vbase = positions.length / 3;
  831. var offset = isTop ? height / 2 : -height / 2;
  832. var center = new Vector3(0, offset, 0);
  833. positions.push(center.x, center.y, center.z);
  834. normals.push(0, isTop ? 1 : -1, 0);
  835. uvs.push(u.x + (u.z - u.x) * 0.5, u.y + (u.w - u.y) * 0.5);
  836. if (faceColors) {
  837. colors.push(c.r, c.g, c.b, c.a);
  838. }
  839. var textureScale = new Vector2(0.5, 0.5);
  840. for (i = 0; i <= tessellation; i++) {
  841. angle = Math.PI * 2 * i * arc / tessellation;
  842. var cos = Math.cos(-angle);
  843. var sin = Math.sin(-angle);
  844. circleVector = new Vector3(cos * radius, offset, sin * radius);
  845. var textureCoordinate = new Vector2(cos * textureScale.x + 0.5, sin * textureScale.y + 0.5);
  846. positions.push(circleVector.x, circleVector.y, circleVector.z);
  847. normals.push(0, isTop ? 1 : -1, 0);
  848. uvs.push(u.x + (u.z - u.x) * textureCoordinate.x, u.y + (u.w - u.y) * textureCoordinate.y);
  849. if (faceColors) {
  850. colors.push(c.r, c.g, c.b, c.a);
  851. }
  852. }
  853. // Cap indices
  854. for (i = 0; i < tessellation; i++) {
  855. if (!isTop) {
  856. indices.push(vbase);
  857. indices.push(vbase + (i + 1));
  858. indices.push(vbase + (i + 2));
  859. }
  860. else {
  861. indices.push(vbase);
  862. indices.push(vbase + (i + 2));
  863. indices.push(vbase + (i + 1));
  864. }
  865. }
  866. };
  867. // add caps to geometry
  868. createCylinderCap(false);
  869. createCylinderCap(true);
  870. // Sides
  871. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  872. var vertexData = new VertexData();
  873. vertexData.indices = indices;
  874. vertexData.positions = positions;
  875. vertexData.normals = normals;
  876. vertexData.uvs = uvs;
  877. if (faceColors) {
  878. vertexData.colors = colors;
  879. }
  880. return vertexData;
  881. }
  882. public static CreateTorus(options: { diameter?: number, thickness?: number, tessellation?: number, sideOrientation?: number }) {
  883. var indices = [];
  884. var positions = [];
  885. var normals = [];
  886. var uvs = [];
  887. var diameter = options.diameter || 1;
  888. var thickness = options.thickness || 0.5;
  889. var tessellation = options.tessellation || 16;
  890. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  891. var stride = tessellation + 1;
  892. for (var i = 0; i <= tessellation; i++) {
  893. var u = i / tessellation;
  894. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  895. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  896. for (var j = 0; j <= tessellation; j++) {
  897. var v = 1 - j / tessellation;
  898. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  899. var dx = Math.cos(innerAngle);
  900. var dy = Math.sin(innerAngle);
  901. // Create a vertex.
  902. var normal = new Vector3(dx, dy, 0);
  903. var position = normal.scale(thickness / 2);
  904. var textureCoordinate = new Vector2(u, v);
  905. position = Vector3.TransformCoordinates(position, transform);
  906. normal = Vector3.TransformNormal(normal, transform);
  907. positions.push(position.x, position.y, position.z);
  908. normals.push(normal.x, normal.y, normal.z);
  909. uvs.push(textureCoordinate.x, textureCoordinate.y);
  910. // And create indices for two triangles.
  911. var nextI = (i + 1) % stride;
  912. var nextJ = (j + 1) % stride;
  913. indices.push(i * stride + j);
  914. indices.push(i * stride + nextJ);
  915. indices.push(nextI * stride + j);
  916. indices.push(i * stride + nextJ);
  917. indices.push(nextI * stride + nextJ);
  918. indices.push(nextI * stride + j);
  919. }
  920. }
  921. // Sides
  922. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  923. // Result
  924. var vertexData = new VertexData();
  925. vertexData.indices = indices;
  926. vertexData.positions = positions;
  927. vertexData.normals = normals;
  928. vertexData.uvs = uvs;
  929. return vertexData;
  930. }
  931. public static CreateLineSystem(options: { lines: Vector3[][] }): VertexData {
  932. var indices = [];
  933. var positions = [];
  934. var lines = options.lines;
  935. var idx = 0;
  936. for (var l = 0; l < lines.length; l++) {
  937. var points = lines[l];
  938. for (var index = 0; index < points.length; index++) {
  939. positions.push(points[index].x, points[index].y, points[index].z);
  940. if (index > 0) {
  941. indices.push(idx - 1);
  942. indices.push(idx);
  943. }
  944. idx++;
  945. }
  946. }
  947. var vertexData = new VertexData();
  948. vertexData.indices = indices;
  949. vertexData.positions = positions;
  950. return vertexData;
  951. }
  952. public static CreateDashedLines(options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number }): VertexData {
  953. var dashSize = options.dashSize || 3;
  954. var gapSize = options.gapSize || 1;
  955. var dashNb = options.dashNb || 200;
  956. var points = options.points;
  957. var positions = new Array<number>();
  958. var indices = new Array<number>();
  959. var curvect = Vector3.Zero();
  960. var lg = 0;
  961. var nb = 0;
  962. var shft = 0;
  963. var dashshft = 0;
  964. var curshft = 0;
  965. var idx = 0;
  966. var i = 0;
  967. for (i = 0; i < points.length - 1; i++) {
  968. points[i + 1].subtractToRef(points[i], curvect);
  969. lg += curvect.length();
  970. }
  971. shft = lg / dashNb;
  972. dashshft = dashSize * shft / (dashSize + gapSize);
  973. for (i = 0; i < points.length - 1; i++) {
  974. points[i + 1].subtractToRef(points[i], curvect);
  975. nb = Math.floor(curvect.length() / shft);
  976. curvect.normalize();
  977. for (var j = 0; j < nb; j++) {
  978. curshft = shft * j;
  979. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  980. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  981. indices.push(idx, idx + 1);
  982. idx += 2;
  983. }
  984. }
  985. // Result
  986. var vertexData = new VertexData();
  987. vertexData.positions = positions;
  988. vertexData.indices = indices;
  989. return vertexData;
  990. }
  991. public static CreateGround(options: { width?: number, height?: number, subdivisions?: number }): VertexData {
  992. var indices = [];
  993. var positions = [];
  994. var normals = [];
  995. var uvs = [];
  996. var row: number, col: number;
  997. var width: number = options.width || 1;
  998. var height: number = options.height || 1;
  999. var subdivisions: number = options.subdivisions || 1;
  1000. for (row = 0; row <= subdivisions; row++) {
  1001. for (col = 0; col <= subdivisions; col++) {
  1002. var position = new Vector3((col * width) / subdivisions - (width / 2.0), 0, ((subdivisions - row) * height) / subdivisions - (height / 2.0));
  1003. var normal = new Vector3(0, 1.0, 0);
  1004. positions.push(position.x, position.y, position.z);
  1005. normals.push(normal.x, normal.y, normal.z);
  1006. uvs.push(col / subdivisions, 1.0 - row / subdivisions);
  1007. }
  1008. }
  1009. for (row = 0; row < subdivisions; row++) {
  1010. for (col = 0; col < subdivisions; col++) {
  1011. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  1012. indices.push(col + 1 + row * (subdivisions + 1));
  1013. indices.push(col + row * (subdivisions + 1));
  1014. indices.push(col + (row + 1) * (subdivisions + 1));
  1015. indices.push(col + 1 + (row + 1) * (subdivisions + 1));
  1016. indices.push(col + row * (subdivisions + 1));
  1017. }
  1018. }
  1019. // Result
  1020. var vertexData = new VertexData();
  1021. vertexData.indices = indices;
  1022. vertexData.positions = positions;
  1023. vertexData.normals = normals;
  1024. vertexData.uvs = uvs;
  1025. return vertexData;
  1026. }
  1027. public static CreateTiledGround(options: { xmin: number, zmin: number, xmax: number, zmax: number, subdivisions?: { w: number; h: number; }, precision?: { w: number; h: number; } }): VertexData {
  1028. var xmin = options.xmin;
  1029. var zmin = options.zmin;
  1030. var xmax = options.xmax;
  1031. var zmax = options.zmax;
  1032. var subdivisions = options.subdivisions || { w: 1, h: 1 };
  1033. var precision = options.precision || { w: 1, h: 1 };
  1034. var indices = [];
  1035. var positions = [];
  1036. var normals = [];
  1037. var uvs = [];
  1038. var row: number, col: number, tileRow: number, tileCol: number;
  1039. subdivisions.h = (subdivisions.w < 1) ? 1 : subdivisions.h;
  1040. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  1041. precision.w = (precision.w < 1) ? 1 : precision.w;
  1042. precision.h = (precision.h < 1) ? 1 : precision.h;
  1043. var tileSize = {
  1044. 'w': (xmax - xmin) / subdivisions.w,
  1045. 'h': (zmax - zmin) / subdivisions.h
  1046. };
  1047. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  1048. // Indices
  1049. var base = positions.length / 3;
  1050. var rowLength = precision.w + 1;
  1051. for (row = 0; row < precision.h; row++) {
  1052. for (col = 0; col < precision.w; col++) {
  1053. var square = [
  1054. base + col + row * rowLength,
  1055. base + (col + 1) + row * rowLength,
  1056. base + (col + 1) + (row + 1) * rowLength,
  1057. base + col + (row + 1) * rowLength
  1058. ];
  1059. indices.push(square[1]);
  1060. indices.push(square[2]);
  1061. indices.push(square[3]);
  1062. indices.push(square[0]);
  1063. indices.push(square[1]);
  1064. indices.push(square[3]);
  1065. }
  1066. }
  1067. // Position, normals and uvs
  1068. var position = Vector3.Zero();
  1069. var normal = new Vector3(0, 1.0, 0);
  1070. for (row = 0; row <= precision.h; row++) {
  1071. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  1072. for (col = 0; col <= precision.w; col++) {
  1073. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  1074. position.y = 0;
  1075. positions.push(position.x, position.y, position.z);
  1076. normals.push(normal.x, normal.y, normal.z);
  1077. uvs.push(col / precision.w, row / precision.h);
  1078. }
  1079. }
  1080. }
  1081. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  1082. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  1083. applyTile(
  1084. xmin + tileCol * tileSize.w,
  1085. zmin + tileRow * tileSize.h,
  1086. xmin + (tileCol + 1) * tileSize.w,
  1087. zmin + (tileRow + 1) * tileSize.h
  1088. );
  1089. }
  1090. }
  1091. // Result
  1092. var vertexData = new VertexData();
  1093. vertexData.indices = indices;
  1094. vertexData.positions = positions;
  1095. vertexData.normals = normals;
  1096. vertexData.uvs = uvs;
  1097. return vertexData;
  1098. }
  1099. public static CreateGroundFromHeightMap(options: { width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, buffer: Uint8Array, bufferWidth: number, bufferHeight: number }): VertexData {
  1100. var indices = [];
  1101. var positions = [];
  1102. var normals = [];
  1103. var uvs = [];
  1104. var row, col;
  1105. // Vertices
  1106. for (row = 0; row <= options.subdivisions; row++) {
  1107. for (col = 0; col <= options.subdivisions; col++) {
  1108. var position = new Vector3((col * options.width) / options.subdivisions - (options.width / 2.0), 0, ((options.subdivisions - row) * options.height) / options.subdivisions - (options.height / 2.0));
  1109. // Compute height
  1110. var heightMapX = (((position.x + options.width / 2) / options.width) * (options.bufferWidth - 1)) | 0;
  1111. var heightMapY = ((1.0 - (position.z + options.height / 2) / options.height) * (options.bufferHeight - 1)) | 0;
  1112. var pos = (heightMapX + heightMapY * options.bufferWidth) * 4;
  1113. var r = options.buffer[pos] / 255.0;
  1114. var g = options.buffer[pos + 1] / 255.0;
  1115. var b = options.buffer[pos + 2] / 255.0;
  1116. var gradient = r * 0.3 + g * 0.59 + b * 0.11;
  1117. position.y = options.minHeight + (options.maxHeight - options.minHeight) * gradient;
  1118. // Add vertex
  1119. positions.push(position.x, position.y, position.z);
  1120. normals.push(0, 0, 0);
  1121. uvs.push(col / options.subdivisions, 1.0 - row / options.subdivisions);
  1122. }
  1123. }
  1124. // Indices
  1125. for (row = 0; row < options.subdivisions; row++) {
  1126. for (col = 0; col < options.subdivisions; col++) {
  1127. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1128. indices.push(col + 1 + row * (options.subdivisions + 1));
  1129. indices.push(col + row * (options.subdivisions + 1));
  1130. indices.push(col + (row + 1) * (options.subdivisions + 1));
  1131. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1132. indices.push(col + row * (options.subdivisions + 1));
  1133. }
  1134. }
  1135. // Normals
  1136. VertexData.ComputeNormals(positions, indices, normals);
  1137. // Result
  1138. var vertexData = new VertexData();
  1139. vertexData.indices = indices;
  1140. vertexData.positions = positions;
  1141. vertexData.normals = normals;
  1142. vertexData.uvs = uvs;
  1143. return vertexData;
  1144. }
  1145. public static CreatePlane(options: { size?: number, width?: number, height?: number, sideOrientation?: number }): VertexData {
  1146. var indices = [];
  1147. var positions = [];
  1148. var normals = [];
  1149. var uvs = [];
  1150. var width: number = options.width || options.size || 1;
  1151. var height: number = options.height || options.size || 1;
  1152. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1153. // Vertices
  1154. var halfWidth = width / 2.0;
  1155. var halfHeight = height / 2.0;
  1156. positions.push(-halfWidth, -halfHeight, 0);
  1157. normals.push(0, 0, -1.0);
  1158. uvs.push(0.0, 0.0);
  1159. positions.push(halfWidth, -halfHeight, 0);
  1160. normals.push(0, 0, -1.0);
  1161. uvs.push(1.0, 0.0);
  1162. positions.push(halfWidth, halfHeight, 0);
  1163. normals.push(0, 0, -1.0);
  1164. uvs.push(1.0, 1.0);
  1165. positions.push(-halfWidth, halfHeight, 0);
  1166. normals.push(0, 0, -1.0);
  1167. uvs.push(0.0, 1.0);
  1168. // Indices
  1169. indices.push(0);
  1170. indices.push(1);
  1171. indices.push(2);
  1172. indices.push(0);
  1173. indices.push(2);
  1174. indices.push(3);
  1175. // Sides
  1176. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1177. // Result
  1178. var vertexData = new VertexData();
  1179. vertexData.indices = indices;
  1180. vertexData.positions = positions;
  1181. vertexData.normals = normals;
  1182. vertexData.uvs = uvs;
  1183. return vertexData;
  1184. }
  1185. public static CreateDisc(options: { radius?: number, tessellation?: number, arc?: number, sideOrientation?: number }): VertexData {
  1186. var positions = [];
  1187. var indices = [];
  1188. var normals = [];
  1189. var uvs = [];
  1190. var radius = options.radius || 0.5;
  1191. var tessellation = options.tessellation || 64;
  1192. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  1193. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1194. // positions and uvs
  1195. positions.push(0, 0, 0); // disc center first
  1196. uvs.push(0.5, 0.5);
  1197. var theta = Math.PI * 2 * arc;
  1198. var step = theta / tessellation;
  1199. for (var a = 0; a < theta; a += step) {
  1200. var x = Math.cos(a);
  1201. var y = Math.sin(a);
  1202. var u = (x + 1) / 2;
  1203. var v = (1 - y) / 2;
  1204. positions.push(radius * x, radius * y, 0);
  1205. uvs.push(u, v);
  1206. }
  1207. if (arc === 1) {
  1208. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1209. uvs.push(uvs[2], uvs[3]);
  1210. }
  1211. //indices
  1212. var vertexNb = positions.length / 3;
  1213. for (var i = 1; i < vertexNb - 1; i++) {
  1214. indices.push(i + 1, 0, i);
  1215. }
  1216. // result
  1217. VertexData.ComputeNormals(positions, indices, normals);
  1218. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1219. var vertexData = new VertexData();
  1220. vertexData.indices = indices;
  1221. vertexData.positions = positions;
  1222. vertexData.normals = normals;
  1223. vertexData.uvs = uvs;
  1224. return vertexData;
  1225. }
  1226. public static CreateIcoSphere(options: { radius?: number, radiusX?: number, radiusY?: number, radiusZ?: number, flat?: boolean, subdivisions?: number, sideOrientation?: number }): VertexData {
  1227. var sideOrientation = options.sideOrientation || Mesh.DEFAULTSIDE;
  1228. var radius = options.radius || 1;
  1229. var flat = (options.flat === undefined) ? true : options.flat;
  1230. var subdivisions = options.subdivisions || 4;
  1231. var radiusX = options.radiusX || radius;
  1232. var radiusY = options.radiusY || radius;
  1233. var radiusZ = options.radiusZ || radius;
  1234. var t = (1 + Math.sqrt(5)) / 2;
  1235. // 12 vertex x,y,z
  1236. var ico_vertices = [
  1237. -1, t, -0, 1, t, 0, -1, -t, 0, 1, -t, 0, // v0-3
  1238. 0, -1, -t, 0, 1, -t, 0, -1, t, 0, 1, t, // v4-7
  1239. t, 0, 1, t, 0, -1, -t, 0, 1, -t, 0, -1 // v8-11
  1240. ];
  1241. // index of 3 vertex makes a face of icopshere
  1242. var ico_indices = [
  1243. 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 12, 22, 23,
  1244. 1, 5, 20, 5, 11, 4, 23, 22, 13, 22, 18, 6, 7, 1, 8,
  1245. 14, 21, 4, 14, 4, 2, 16, 13, 6, 15, 6, 19, 3, 8, 9,
  1246. 4, 21, 5, 13, 17, 23, 6, 13, 22, 19, 6, 18, 9, 8, 1
  1247. ];
  1248. // vertex for uv have aliased position, not for UV
  1249. var vertices_unalias_id = [
  1250. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  1251. // vertex alias
  1252. 0, // 12: 0 + 12
  1253. 2, // 13: 2 + 11
  1254. 3, // 14: 3 + 11
  1255. 3, // 15: 3 + 12
  1256. 3, // 16: 3 + 13
  1257. 4, // 17: 4 + 13
  1258. 7, // 18: 7 + 11
  1259. 8, // 19: 8 + 11
  1260. 9, // 20: 9 + 11
  1261. 9, // 21: 9 + 12
  1262. 10, // 22: A + 12
  1263. 11 // 23: B + 12
  1264. ];
  1265. // uv as integer step (not pixels !)
  1266. var ico_vertexuv = [
  1267. 5, 1, 3, 1, 6, 4, 0, 0, // v0-3
  1268. 5, 3, 4, 2, 2, 2, 4, 0, // v4-7
  1269. 2, 0, 1, 1, 6, 0, 6, 2, // v8-11
  1270. // vertex alias (for same vertex on different faces)
  1271. 0, 4, // 12: 0 + 12
  1272. 3, 3, // 13: 2 + 11
  1273. 4, 4, // 14: 3 + 11
  1274. 3, 1, // 15: 3 + 12
  1275. 4, 2, // 16: 3 + 13
  1276. 4, 4, // 17: 4 + 13
  1277. 0, 2, // 18: 7 + 11
  1278. 1, 1, // 19: 8 + 11
  1279. 2, 2, // 20: 9 + 11
  1280. 3, 3, // 21: 9 + 12
  1281. 1, 3, // 22: A + 12
  1282. 2, 4 // 23: B + 12
  1283. ];
  1284. // Vertices[0, 1, ...9, A, B] : position on UV plane
  1285. // '+' indicate duplicate position to be fixed (3,9:0,2,3,4,7,8,A,B)
  1286. // First island of uv mapping
  1287. // v = 4h 3+ 2
  1288. // v = 3h 9+ 4
  1289. // v = 2h 9+ 5 B
  1290. // v = 1h 9 1 0
  1291. // v = 0h 3 8 7 A
  1292. // u = 0 1 2 3 4 5 6 *a
  1293. // Second island of uv mapping
  1294. // v = 4h 0+ B+ 4+
  1295. // v = 3h A+ 2+
  1296. // v = 2h 7+ 6 3+
  1297. // v = 1h 8+ 3+
  1298. // v = 0h
  1299. // u = 0 1 2 3 4 5 6 *a
  1300. // Face layout on texture UV mapping
  1301. // ============
  1302. // \ 4 /\ 16 / ======
  1303. // \ / \ / /\ 11 /
  1304. // \/ 7 \/ / \ /
  1305. // ======= / 10 \/
  1306. // /\ 17 /\ =======
  1307. // / \ / \ \ 15 /\
  1308. // / 8 \/ 12 \ \ / \
  1309. // ============ \/ 6 \
  1310. // \ 18 /\ ============
  1311. // \ / \ \ 5 /\ 0 /
  1312. // \/ 13 \ \ / \ /
  1313. // ======= \/ 1 \/
  1314. // =============
  1315. // /\ 19 /\ 2 /\
  1316. // / \ / \ / \
  1317. // / 14 \/ 9 \/ 3 \
  1318. // ===================
  1319. // uv step is u:1 or 0.5, v:cos(30)=sqrt(3)/2, ratio approx is 84/97
  1320. var ustep = 138 / 1024;
  1321. var vstep = 239 / 1024;
  1322. var uoffset = 60 / 1024;
  1323. var voffset = 26 / 1024;
  1324. // Second island should have margin, not to touch the first island
  1325. // avoid any borderline artefact in pixel rounding
  1326. var island_u_offset = -40 / 1024;
  1327. var island_v_offset = +20 / 1024;
  1328. // face is either island 0 or 1 :
  1329. // second island is for faces : [4, 7, 8, 12, 13, 16, 17, 18]
  1330. var island = [
  1331. 0, 0, 0, 0, 1, // 0 - 4
  1332. 0, 0, 1, 1, 0, // 5 - 9
  1333. 0, 0, 1, 1, 0, // 10 - 14
  1334. 0, 1, 1, 1, 0 // 15 - 19
  1335. ];
  1336. var indices = [];
  1337. var positions = [];
  1338. var normals = [];
  1339. var uvs = [];
  1340. var current_indice = 0;
  1341. // prepare array of 3 vector (empty) (to be worked in place, shared for each face)
  1342. var face_vertex_pos = new Array(3);
  1343. var face_vertex_uv = new Array(3);
  1344. var v012;
  1345. for (v012 = 0; v012 < 3; v012++) {
  1346. face_vertex_pos[v012] = Vector3.Zero();
  1347. face_vertex_uv[v012] = Vector2.Zero();
  1348. }
  1349. // create all with normals
  1350. for (var face = 0; face < 20; face++) {
  1351. // 3 vertex per face
  1352. for (v012 = 0; v012 < 3; v012++) {
  1353. // look up vertex 0,1,2 to its index in 0 to 11 (or 23 including alias)
  1354. var v_id = ico_indices[3 * face + v012];
  1355. // vertex have 3D position (x,y,z)
  1356. face_vertex_pos[v012].copyFromFloats(
  1357. ico_vertices[3 * vertices_unalias_id[v_id]],
  1358. ico_vertices[3 * vertices_unalias_id[v_id] + 1],
  1359. ico_vertices[3 * vertices_unalias_id[v_id] + 2]);
  1360. // Normalize to get normal, then scale to radius
  1361. face_vertex_pos[v012].normalize().scaleInPlace(radius);
  1362. // uv Coordinates from vertex ID
  1363. face_vertex_uv[v012].copyFromFloats(
  1364. ico_vertexuv[2 * v_id] * ustep + uoffset + island[face] * island_u_offset,
  1365. ico_vertexuv[2 * v_id + 1] * vstep + voffset + island[face] * island_v_offset);
  1366. }
  1367. // Subdivide the face (interpolate pos, norm, uv)
  1368. // - pos is linear interpolation, then projected to sphere (converge polyhedron to sphere)
  1369. // - norm is linear interpolation of vertex corner normal
  1370. // (to be checked if better to re-calc from face vertex, or if approximation is OK ??? )
  1371. // - uv is linear interpolation
  1372. //
  1373. // Topology is as below for sub-divide by 2
  1374. // vertex shown as v0,v1,v2
  1375. // interp index is i1 to progress in range [v0,v1[
  1376. // interp index is i2 to progress in range [v0,v2[
  1377. // face index as (i1,i2) for /\ : (i1,i2),(i1+1,i2),(i1,i2+1)
  1378. // and (i1,i2)' for \/ : (i1+1,i2),(i1+1,i2+1),(i1,i2+1)
  1379. //
  1380. //
  1381. // i2 v2
  1382. // ^ ^
  1383. // / / \
  1384. // / / \
  1385. // / / \
  1386. // / / (0,1) \
  1387. // / #---------\
  1388. // / / \ (0,0)'/ \
  1389. // / / \ / \
  1390. // / / \ / \
  1391. // / / (0,0) \ / (1,0) \
  1392. // / #---------#---------\
  1393. // v0 v1
  1394. //
  1395. // --------------------> i1
  1396. //
  1397. // interp of (i1,i2):
  1398. // along i2 : x0=lerp(v0,v2, i2/S) <---> x1=lerp(v1,v2, i2/S)
  1399. // along i1 : lerp(x0,x1, i1/(S-i2))
  1400. //
  1401. // centroid of triangle is needed to get help normal computation
  1402. // (c1,c2) are used for centroid location
  1403. var interp_vertex = (i1: number, i2: number, c1: number, c2: number) => {
  1404. // vertex is interpolated from
  1405. // - face_vertex_pos[0..2]
  1406. // - face_vertex_uv[0..2]
  1407. var pos_x0 = Vector3.Lerp(face_vertex_pos[0], face_vertex_pos[2], i2 / subdivisions);
  1408. var pos_x1 = Vector3.Lerp(face_vertex_pos[1], face_vertex_pos[2], i2 / subdivisions);
  1409. var pos_interp = (subdivisions === i2) ? face_vertex_pos[2] : Vector3.Lerp(pos_x0, pos_x1, i1 / (subdivisions - i2));
  1410. pos_interp.normalize();
  1411. var vertex_normal;
  1412. if (flat) {
  1413. // in flat mode, recalculate normal as face centroid normal
  1414. var centroid_x0 = Vector3.Lerp(face_vertex_pos[0], face_vertex_pos[2], c2 / subdivisions);
  1415. var centroid_x1 = Vector3.Lerp(face_vertex_pos[1], face_vertex_pos[2], c2 / subdivisions);
  1416. vertex_normal = Vector3.Lerp(centroid_x0, centroid_x1, c1 / (subdivisions - c2));
  1417. } else {
  1418. // in smooth mode, recalculate normal from each single vertex position
  1419. vertex_normal = new Vector3(pos_interp.x, pos_interp.y, pos_interp.z);
  1420. }
  1421. // Vertex normal need correction due to X,Y,Z radius scaling
  1422. vertex_normal.x /= radiusX;
  1423. vertex_normal.y /= radiusY;
  1424. vertex_normal.z /= radiusZ;
  1425. vertex_normal.normalize();
  1426. var uv_x0 = Vector2.Lerp(face_vertex_uv[0], face_vertex_uv[2], i2 / subdivisions);
  1427. var uv_x1 = Vector2.Lerp(face_vertex_uv[1], face_vertex_uv[2], i2 / subdivisions);
  1428. var uv_interp = (subdivisions === i2) ? face_vertex_uv[2] : Vector2.Lerp(uv_x0, uv_x1, i1 / (subdivisions - i2));
  1429. positions.push(pos_interp.x * radiusX, pos_interp.y * radiusY, pos_interp.z * radiusZ);
  1430. normals.push(vertex_normal.x, vertex_normal.y, vertex_normal.z);
  1431. uvs.push(uv_interp.x, uv_interp.y);
  1432. // push each vertex has member of a face
  1433. // Same vertex can bleong to multiple face, it is pushed multiple time (duplicate vertex are present)
  1434. indices.push(current_indice);
  1435. current_indice++;
  1436. }
  1437. for (var i2 = 0; i2 < subdivisions; i2++) {
  1438. for (var i1 = 0; i1 + i2 < subdivisions; i1++) {
  1439. // face : (i1,i2) for /\ :
  1440. // interp for : (i1,i2),(i1+1,i2),(i1,i2+1)
  1441. interp_vertex(i1, i2, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1442. interp_vertex(i1 + 1, i2, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1443. interp_vertex(i1, i2 + 1, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1444. if (i1 + i2 + 1 < subdivisions) {
  1445. // face : (i1,i2)' for \/ :
  1446. // interp for (i1+1,i2),(i1+1,i2+1),(i1,i2+1)
  1447. interp_vertex(i1 + 1, i2, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1448. interp_vertex(i1 + 1, i2 + 1, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1449. interp_vertex(i1, i2 + 1, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1450. }
  1451. }
  1452. }
  1453. }
  1454. // Sides
  1455. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1456. // Result
  1457. var vertexData = new VertexData();
  1458. vertexData.indices = indices;
  1459. vertexData.positions = positions;
  1460. vertexData.normals = normals;
  1461. vertexData.uvs = uvs;
  1462. return vertexData;
  1463. }
  1464. // inspired from // http://stemkoski.github.io/Three.js/Polyhedra.html
  1465. public static CreatePolyhedron(options: { type?: number, size?: number, sizeX?: number, sizeY?: number, sizeZ?: number, custom?: any, faceUV?: Vector4[], faceColors?: Color4[], flat?: boolean, sideOrientation?: number }): VertexData {
  1466. // provided polyhedron types :
  1467. // 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  1468. // 9 : Pentagonal Pyramid (J2), 10 : Triangular Dipyramid (J12), 11 : Pentagonal Dipyramid (J13), 12 : Elongated Square Dipyramid (J15), 13 : Elongated Pentagonal Dipyramid (J16), 14 : Elongated Pentagonal Cupola (J20)
  1469. var polyhedra: { vertex: number[][], face: number[][] }[] = [];
  1470. polyhedra[0] = { vertex: [[0, 0, 1.732051], [1.632993, 0, -0.5773503], [-0.8164966, 1.414214, -0.5773503], [-0.8164966, -1.414214, -0.5773503]], face: [[0, 1, 2], [0, 2, 3], [0, 3, 1], [1, 3, 2]] };
  1471. polyhedra[1] = { vertex: [[0, 0, 1.414214], [1.414214, 0, 0], [0, 1.414214, 0], [-1.414214, 0, 0], [0, -1.414214, 0], [0, 0, -1.414214]], face: [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 1], [1, 4, 5], [1, 5, 2], [2, 5, 3], [3, 5, 4]] };
  1472. polyhedra[2] = {
  1473. vertex: [[0, 0, 1.070466], [0.7136442, 0, 0.7978784], [-0.3568221, 0.618034, 0.7978784], [-0.3568221, -0.618034, 0.7978784], [0.7978784, 0.618034, 0.3568221], [0.7978784, -0.618034, 0.3568221], [-0.9341724, 0.381966, 0.3568221], [0.1362939, 1, 0.3568221], [0.1362939, -1, 0.3568221], [-0.9341724, -0.381966, 0.3568221], [0.9341724, 0.381966, -0.3568221], [0.9341724, -0.381966, -0.3568221], [-0.7978784, 0.618034, -0.3568221], [-0.1362939, 1, -0.3568221], [-0.1362939, -1, -0.3568221], [-0.7978784, -0.618034, -0.3568221], [0.3568221, 0.618034, -0.7978784], [0.3568221, -0.618034, -0.7978784], [-0.7136442, 0, -0.7978784], [0, 0, -1.070466]],
  1474. face: [[0, 1, 4, 7, 2], [0, 2, 6, 9, 3], [0, 3, 8, 5, 1], [1, 5, 11, 10, 4], [2, 7, 13, 12, 6], [3, 9, 15, 14, 8], [4, 10, 16, 13, 7], [5, 8, 14, 17, 11], [6, 12, 18, 15, 9], [10, 11, 17, 19, 16], [12, 13, 16, 19, 18], [14, 15, 18, 19, 17]]
  1475. };
  1476. polyhedra[3] = {
  1477. vertex: [[0, 0, 1.175571], [1.051462, 0, 0.5257311], [0.3249197, 1, 0.5257311], [-0.8506508, 0.618034, 0.5257311], [-0.8506508, -0.618034, 0.5257311], [0.3249197, -1, 0.5257311], [0.8506508, 0.618034, -0.5257311], [0.8506508, -0.618034, -0.5257311], [-0.3249197, 1, -0.5257311], [-1.051462, 0, -0.5257311], [-0.3249197, -1, -0.5257311], [0, 0, -1.175571]],
  1478. face: [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5], [0, 5, 1], [1, 5, 7], [1, 7, 6], [1, 6, 2], [2, 6, 8], [2, 8, 3], [3, 8, 9], [3, 9, 4], [4, 9, 10], [4, 10, 5], [5, 10, 7], [6, 7, 11], [6, 11, 8], [7, 10, 11], [8, 11, 9], [9, 11, 10]]
  1479. };
  1480. polyhedra[4] = {
  1481. vertex: [[0, 0, 1.070722], [0.7148135, 0, 0.7971752], [-0.104682, 0.7071068, 0.7971752], [-0.6841528, 0.2071068, 0.7971752], [-0.104682, -0.7071068, 0.7971752], [0.6101315, 0.7071068, 0.5236279], [1.04156, 0.2071068, 0.1367736], [0.6101315, -0.7071068, 0.5236279], [-0.3574067, 1, 0.1367736], [-0.7888348, -0.5, 0.5236279], [-0.9368776, 0.5, 0.1367736], [-0.3574067, -1, 0.1367736], [0.3574067, 1, -0.1367736], [0.9368776, -0.5, -0.1367736], [0.7888348, 0.5, -0.5236279], [0.3574067, -1, -0.1367736], [-0.6101315, 0.7071068, -0.5236279], [-1.04156, -0.2071068, -0.1367736], [-0.6101315, -0.7071068, -0.5236279], [0.104682, 0.7071068, -0.7971752], [0.6841528, -0.2071068, -0.7971752], [0.104682, -0.7071068, -0.7971752], [-0.7148135, 0, -0.7971752], [0, 0, -1.070722]],
  1482. face: [[0, 2, 3], [1, 6, 5], [4, 9, 11], [7, 15, 13], [8, 16, 10], [12, 14, 19], [17, 22, 18], [20, 21, 23], [0, 1, 5, 2], [0, 3, 9, 4], [0, 4, 7, 1], [1, 7, 13, 6], [2, 5, 12, 8], [2, 8, 10, 3], [3, 10, 17, 9], [4, 11, 15, 7], [5, 6, 14, 12], [6, 13, 20, 14], [8, 12, 19, 16], [9, 17, 18, 11], [10, 16, 22, 17], [11, 18, 21, 15], [13, 15, 21, 20], [14, 20, 23, 19], [16, 19, 23, 22], [18, 22, 23, 21]]
  1483. };
  1484. polyhedra[5] = { vertex: [[0, 0, 1.322876], [1.309307, 0, 0.1889822], [-0.9819805, 0.8660254, 0.1889822], [0.1636634, -1.299038, 0.1889822], [0.3273268, 0.8660254, -0.9449112], [-0.8183171, -0.4330127, -0.9449112]], face: [[0, 3, 1], [2, 4, 5], [0, 1, 4, 2], [0, 2, 5, 3], [1, 3, 5, 4]] };
  1485. polyhedra[6] = { vertex: [[0, 0, 1.159953], [1.013464, 0, 0.5642542], [-0.3501431, 0.9510565, 0.5642542], [-0.7715208, -0.6571639, 0.5642542], [0.6633206, 0.9510565, -0.03144481], [0.8682979, -0.6571639, -0.3996071], [-1.121664, 0.2938926, -0.03144481], [-0.2348831, -1.063314, -0.3996071], [0.5181548, 0.2938926, -0.9953061], [-0.5850262, -0.112257, -0.9953061]], face: [[0, 1, 4, 2], [0, 2, 6, 3], [1, 5, 8, 4], [3, 6, 9, 7], [5, 7, 9, 8], [0, 3, 7, 5, 1], [2, 4, 8, 9, 6]] };
  1486. polyhedra[7] = { vertex: [[0, 0, 1.118034], [0.8944272, 0, 0.6708204], [-0.2236068, 0.8660254, 0.6708204], [-0.7826238, -0.4330127, 0.6708204], [0.6708204, 0.8660254, 0.2236068], [1.006231, -0.4330127, -0.2236068], [-1.006231, 0.4330127, 0.2236068], [-0.6708204, -0.8660254, -0.2236068], [0.7826238, 0.4330127, -0.6708204], [0.2236068, -0.8660254, -0.6708204], [-0.8944272, 0, -0.6708204], [0, 0, -1.118034]], face: [[0, 1, 4, 2], [0, 2, 6, 3], [1, 5, 8, 4], [3, 6, 10, 7], [5, 9, 11, 8], [7, 10, 11, 9], [0, 3, 7, 9, 5, 1], [2, 4, 8, 11, 10, 6]] };
  1487. polyhedra[8] = { vertex: [[-0.729665, 0.670121, 0.319155], [-0.655235, -0.29213, -0.754096], [-0.093922, -0.607123, 0.537818], [0.702196, 0.595691, 0.485187], [0.776626, -0.36656, -0.588064]], face: [[1, 4, 2], [0, 1, 2], [3, 0, 2], [4, 3, 2], [4, 1, 0, 3]] };
  1488. polyhedra[9] = { vertex: [[-0.868849, -0.100041, 0.61257], [-0.329458, 0.976099, 0.28078], [-0.26629, -0.013796, -0.477654], [-0.13392, -1.034115, 0.229829], [0.738834, 0.707117, -0.307018], [0.859683, -0.535264, -0.338508]], face: [[3, 0, 2], [5, 3, 2], [4, 5, 2], [1, 4, 2], [0, 1, 2], [0, 3, 5, 4, 1]] };
  1489. polyhedra[10] = { vertex: [[-0.610389, 0.243975, 0.531213], [-0.187812, -0.48795, -0.664016], [-0.187812, 0.9759, -0.664016], [0.187812, -0.9759, 0.664016], [0.798201, 0.243975, 0.132803]], face: [[1, 3, 0], [3, 4, 0], [3, 1, 4], [0, 2, 1], [0, 4, 2], [2, 4, 1]] };
  1490. polyhedra[11] = { vertex: [[-1.028778, 0.392027, -0.048786], [-0.640503, -0.646161, 0.621837], [-0.125162, -0.395663, -0.540059], [0.004683, 0.888447, -0.651988], [0.125161, 0.395663, 0.540059], [0.632925, -0.791376, 0.433102], [1.031672, 0.157063, -0.354165]], face: [[3, 2, 0], [2, 1, 0], [2, 5, 1], [0, 4, 3], [0, 1, 4], [4, 1, 5], [2, 3, 6], [3, 4, 6], [5, 2, 6], [4, 5, 6]] };
  1491. polyhedra[12] = { vertex: [[-0.669867, 0.334933, -0.529576], [-0.669867, 0.334933, 0.529577], [-0.4043, 1.212901, 0], [-0.334933, -0.669867, -0.529576], [-0.334933, -0.669867, 0.529577], [0.334933, 0.669867, -0.529576], [0.334933, 0.669867, 0.529577], [0.4043, -1.212901, 0], [0.669867, -0.334933, -0.529576], [0.669867, -0.334933, 0.529577]], face: [[8, 9, 7], [6, 5, 2], [3, 8, 7], [5, 0, 2], [4, 3, 7], [0, 1, 2], [9, 4, 7], [1, 6, 2], [9, 8, 5, 6], [8, 3, 0, 5], [3, 4, 1, 0], [4, 9, 6, 1]] };
  1492. polyhedra[13] = { vertex: [[-0.931836, 0.219976, -0.264632], [-0.636706, 0.318353, 0.692816], [-0.613483, -0.735083, -0.264632], [-0.326545, 0.979634, 0], [-0.318353, -0.636706, 0.692816], [-0.159176, 0.477529, -0.856368], [0.159176, -0.477529, -0.856368], [0.318353, 0.636706, 0.692816], [0.326545, -0.979634, 0], [0.613482, 0.735082, -0.264632], [0.636706, -0.318353, 0.692816], [0.931835, -0.219977, -0.264632]], face: [[11, 10, 8], [7, 9, 3], [6, 11, 8], [9, 5, 3], [2, 6, 8], [5, 0, 3], [4, 2, 8], [0, 1, 3], [10, 4, 8], [1, 7, 3], [10, 11, 9, 7], [11, 6, 5, 9], [6, 2, 0, 5], [2, 4, 1, 0], [4, 10, 7, 1]] };
  1493. polyhedra[14] = {
  1494. vertex: [[-0.93465, 0.300459, -0.271185], [-0.838689, -0.260219, -0.516017], [-0.711319, 0.717591, 0.128359], [-0.710334, -0.156922, 0.080946], [-0.599799, 0.556003, -0.725148], [-0.503838, -0.004675, -0.969981], [-0.487004, 0.26021, 0.48049], [-0.460089, -0.750282, -0.512622], [-0.376468, 0.973135, -0.325605], [-0.331735, -0.646985, 0.084342], [-0.254001, 0.831847, 0.530001], [-0.125239, -0.494738, -0.966586], [0.029622, 0.027949, 0.730817], [0.056536, -0.982543, -0.262295], [0.08085, 1.087391, 0.076037], [0.125583, -0.532729, 0.485984], [0.262625, 0.599586, 0.780328], [0.391387, -0.726999, -0.716259], [0.513854, -0.868287, 0.139347], [0.597475, 0.85513, 0.326364], [0.641224, 0.109523, 0.783723], [0.737185, -0.451155, 0.538891], [0.848705, -0.612742, -0.314616], [0.976075, 0.365067, 0.32976], [1.072036, -0.19561, 0.084927]],
  1495. face: [[15, 18, 21], [12, 20, 16], [6, 10, 2], [3, 0, 1], [9, 7, 13], [2, 8, 4, 0], [0, 4, 5, 1], [1, 5, 11, 7], [7, 11, 17, 13], [13, 17, 22, 18], [18, 22, 24, 21], [21, 24, 23, 20], [20, 23, 19, 16], [16, 19, 14, 10], [10, 14, 8, 2], [15, 9, 13, 18], [12, 15, 21, 20], [6, 12, 16, 10], [3, 6, 2, 0], [9, 3, 1, 7], [9, 15, 12, 6, 3], [22, 17, 11, 5, 4, 8, 14, 19, 23, 24]]
  1496. };
  1497. var type: number = (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
  1498. var size: number = options.size;
  1499. var sizeX: number = options.sizeX || size || 1;
  1500. var sizeY: number = options.sizeY || size || 1;
  1501. var sizeZ: number = options.sizeZ || size || 1;
  1502. var data: { vertex: number[][], face: number[][], name?: string, category?: string } = options.custom || polyhedra[type];
  1503. var nbfaces = data.face.length;
  1504. var faceUV = options.faceUV || new Array(nbfaces);
  1505. var faceColors = options.faceColors;
  1506. var flat = (options.flat === undefined) ? true : options.flat;
  1507. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1508. var positions = [];
  1509. var indices = [];
  1510. var normals = [];
  1511. var uvs = [];
  1512. var colors = [];
  1513. var index = 0;
  1514. var faceIdx = 0; // face cursor in the array "indexes"
  1515. var indexes = [];
  1516. var i = 0;
  1517. var f = 0;
  1518. var u: number, v: number, ang: number, x: number, y: number, tmp: number;
  1519. // default face colors and UV if undefined
  1520. if (flat) {
  1521. for (f = 0; f < nbfaces; f++) {
  1522. if (faceColors && faceColors[f] === undefined) {
  1523. faceColors[f] = new Color4(1, 1, 1, 1);
  1524. }
  1525. if (faceUV && faceUV[f] === undefined) {
  1526. faceUV[f] = new Vector4(0, 0, 1, 1);
  1527. }
  1528. }
  1529. }
  1530. if (!flat) {
  1531. for (i = 0; i < data.vertex.length; i++) {
  1532. positions.push(data.vertex[i][0] * sizeX, data.vertex[i][1] * sizeY, data.vertex[i][2] * sizeZ);
  1533. uvs.push(0, 0);
  1534. }
  1535. for (f = 0; f < nbfaces; f++) {
  1536. for (i = 0; i < data.face[f].length - 2; i++) {
  1537. indices.push(data.face[f][0], data.face[f][i + 2], data.face[f][i + 1]);
  1538. }
  1539. }
  1540. } else {
  1541. for (f = 0; f < nbfaces; f++) {
  1542. var fl = data.face[f].length; // number of vertices of the current face
  1543. ang = 2 * Math.PI / fl;
  1544. x = 0.5 * Math.tan(ang / 2);
  1545. y = 0.5;
  1546. // positions, uvs, colors
  1547. for (i = 0; i < fl; i++) {
  1548. // positions
  1549. positions.push(data.vertex[data.face[f][i]][0] * sizeX, data.vertex[data.face[f][i]][1] * sizeY, data.vertex[data.face[f][i]][2] * sizeZ);
  1550. indexes.push(index);
  1551. index++;
  1552. // uvs
  1553. u = faceUV[f].x + (faceUV[f].z - faceUV[f].x) * (0.5 + x);
  1554. v = faceUV[f].y + (faceUV[f].w - faceUV[f].y) * (y - 0.5);
  1555. uvs.push(u, v);
  1556. tmp = x * Math.cos(ang) - y * Math.sin(ang);
  1557. y = x * Math.sin(ang) + y * Math.cos(ang);
  1558. x = tmp;
  1559. // colors
  1560. if (faceColors) {
  1561. colors.push(faceColors[f].r, faceColors[f].g, faceColors[f].b, faceColors[f].a);
  1562. }
  1563. }
  1564. // indices from indexes
  1565. for (i = 0; i < fl - 2; i++) {
  1566. indices.push(indexes[0 + faceIdx], indexes[i + 2 + faceIdx], indexes[i + 1 + faceIdx]);
  1567. }
  1568. faceIdx += fl;
  1569. }
  1570. }
  1571. VertexData.ComputeNormals(positions, indices, normals);
  1572. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1573. var vertexData = new VertexData();
  1574. vertexData.positions = positions;
  1575. vertexData.indices = indices;
  1576. vertexData.normals = normals;
  1577. vertexData.uvs = uvs;
  1578. if (faceColors && flat) {
  1579. vertexData.colors = colors;
  1580. }
  1581. return vertexData;
  1582. }
  1583. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1584. public static CreateTorusKnot(options: { radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, p?: number, q?: number, sideOrientation?: number }): VertexData {
  1585. var indices = [];
  1586. var positions = [];
  1587. var normals = [];
  1588. var uvs = [];
  1589. var radius = options.radius || 2;
  1590. var tube = options.tube || 0.5;
  1591. var radialSegments = options.radialSegments || 32;
  1592. var tubularSegments = options.tubularSegments || 32;
  1593. var p = options.p || 2;
  1594. var q = options.q || 3;
  1595. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1596. // Helper
  1597. var getPos = (angle) => {
  1598. var cu = Math.cos(angle);
  1599. var su = Math.sin(angle);
  1600. var quOverP = q / p * angle;
  1601. var cs = Math.cos(quOverP);
  1602. var tx = radius * (2 + cs) * 0.5 * cu;
  1603. var ty = radius * (2 + cs) * su * 0.5;
  1604. var tz = radius * Math.sin(quOverP) * 0.5;
  1605. return new Vector3(tx, ty, tz);
  1606. };
  1607. // Vertices
  1608. var i: number;
  1609. var j: number;
  1610. for (i = 0; i <= radialSegments; i++) {
  1611. var modI = i % radialSegments;
  1612. var u = modI / radialSegments * 2 * p * Math.PI;
  1613. var p1 = getPos(u);
  1614. var p2 = getPos(u + 0.01);
  1615. var tang = p2.subtract(p1);
  1616. var n = p2.add(p1);
  1617. var bitan = Vector3.Cross(tang, n);
  1618. n = Vector3.Cross(bitan, tang);
  1619. bitan.normalize();
  1620. n.normalize();
  1621. for (j = 0; j < tubularSegments; j++) {
  1622. var modJ = j % tubularSegments;
  1623. var v = modJ / tubularSegments * 2 * Math.PI;
  1624. var cx = -tube * Math.cos(v);
  1625. var cy = tube * Math.sin(v);
  1626. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1627. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1628. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1629. uvs.push(i / radialSegments);
  1630. uvs.push(j / tubularSegments);
  1631. }
  1632. }
  1633. for (i = 0; i < radialSegments; i++) {
  1634. for (j = 0; j < tubularSegments; j++) {
  1635. var jNext = (j + 1) % tubularSegments;
  1636. var a = i * tubularSegments + j;
  1637. var b = (i + 1) * tubularSegments + j;
  1638. var c = (i + 1) * tubularSegments + jNext;
  1639. var d = i * tubularSegments + jNext;
  1640. indices.push(d); indices.push(b); indices.push(a);
  1641. indices.push(d); indices.push(c); indices.push(b);
  1642. }
  1643. }
  1644. // Normals
  1645. VertexData.ComputeNormals(positions, indices, normals);
  1646. // Sides
  1647. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs);
  1648. // Result
  1649. var vertexData = new VertexData();
  1650. vertexData.indices = indices;
  1651. vertexData.positions = positions;
  1652. vertexData.normals = normals;
  1653. vertexData.uvs = uvs;
  1654. return vertexData;
  1655. }
  1656. // Tools
  1657. /**
  1658. * @param {any} - positions (number[] or Float32Array)
  1659. * @param {any} - indices (number[] or Uint16Array)
  1660. * @param {any} - normals (number[] or Float32Array)
  1661. */
  1662. public static ComputeNormals(positions: any, indices: any, normals: any) {
  1663. var index = 0;
  1664. var p1p2x = 0.0;
  1665. var p1p2y = 0.0;
  1666. var p1p2z = 0.0;
  1667. var p3p2x = 0.0;
  1668. var p3p2y = 0.0;
  1669. var p3p2z = 0.0;
  1670. var faceNormalx = 0.0;
  1671. var faceNormaly = 0.0;
  1672. var faceNormalz = 0.0;
  1673. var length = 0.0;
  1674. var i1 = 0;
  1675. var i2 = 0;
  1676. var i3 = 0;
  1677. for (index = 0; index < positions.length; index++) {
  1678. normals[index] = 0.0;
  1679. }
  1680. // indice triplet = 1 face
  1681. var nbFaces = indices.length / 3;
  1682. for (index = 0; index < nbFaces; index++) {
  1683. i1 = indices[index * 3]; // get the indexes of each vertex of the face
  1684. i2 = indices[index * 3 + 1];
  1685. i3 = indices[index * 3 + 2];
  1686. p1p2x = positions[i1 * 3] - positions[i2 * 3]; // compute two vectors per face
  1687. p1p2y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
  1688. p1p2z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
  1689. p3p2x = positions[i3 * 3] - positions[i2 * 3];
  1690. p3p2y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
  1691. p3p2z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
  1692. faceNormalx = p1p2y * p3p2z - p1p2z * p3p2y; // compute the face normal with cross product
  1693. faceNormaly = p1p2z * p3p2x - p1p2x * p3p2z;
  1694. faceNormalz = p1p2x * p3p2y - p1p2y * p3p2x;
  1695. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  1696. length = (length === 0) ? 1.0 : length;
  1697. faceNormalx /= length; // normalize this normal
  1698. faceNormaly /= length;
  1699. faceNormalz /= length;
  1700. normals[i1 * 3] += faceNormalx; // accumulate all the normals per face
  1701. normals[i1 * 3 + 1] += faceNormaly;
  1702. normals[i1 * 3 + 2] += faceNormalz;
  1703. normals[i2 * 3] += faceNormalx;
  1704. normals[i2 * 3 + 1] += faceNormaly;
  1705. normals[i2 * 3 + 2] += faceNormalz;
  1706. normals[i3 * 3] += faceNormalx;
  1707. normals[i3 * 3 + 1] += faceNormaly;
  1708. normals[i3 * 3 + 2] += faceNormalz;
  1709. }
  1710. // last normalization of each normal
  1711. for (index = 0; index < normals.length / 3; index++) {
  1712. faceNormalx = normals[index * 3];
  1713. faceNormaly = normals[index * 3 + 1];
  1714. faceNormalz = normals[index * 3 + 2];
  1715. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  1716. length = (length === 0) ? 1.0 : length;
  1717. faceNormalx /= length;
  1718. faceNormaly /= length;
  1719. faceNormalz /= length;
  1720. normals[index * 3] = faceNormalx;
  1721. normals[index * 3 + 1] = faceNormaly;
  1722. normals[index * 3 + 2] = faceNormalz;
  1723. }
  1724. }
  1725. private static _ComputeSides(sideOrientation: number, positions: number[] | Float32Array, indices: number[] | Float32Array, normals: number[] | Float32Array, uvs: number[] | Float32Array) {
  1726. var li: number = indices.length;
  1727. var ln: number = normals.length;
  1728. var i: number;
  1729. var n: number;
  1730. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  1731. switch (sideOrientation) {
  1732. case Mesh.FRONTSIDE:
  1733. // nothing changed
  1734. break;
  1735. case Mesh.BACKSIDE:
  1736. var tmp: number;
  1737. // indices
  1738. for (i = 0; i < li; i += 3) {
  1739. tmp = indices[i];
  1740. indices[i] = indices[i + 2];
  1741. indices[i + 2] = tmp;
  1742. }
  1743. // normals
  1744. for (n = 0; n < ln; n++) {
  1745. normals[n] = -normals[n];
  1746. }
  1747. break;
  1748. case Mesh.DOUBLESIDE:
  1749. // positions
  1750. var lp: number = positions.length;
  1751. var l: number = lp / 3;
  1752. for (var p = 0; p < lp; p++) {
  1753. positions[lp + p] = positions[p];
  1754. }
  1755. // indices
  1756. for (i = 0; i < li; i += 3) {
  1757. indices[i + li] = indices[i + 2] + l;
  1758. indices[i + 1 + li] = indices[i + 1] + l;
  1759. indices[i + 2 + li] = indices[i] + l;
  1760. }
  1761. // normals
  1762. for (n = 0; n < ln; n++) {
  1763. normals[ln + n] = -normals[n];
  1764. }
  1765. // uvs
  1766. var lu: number = uvs.length;
  1767. for (var u: number = 0; u < lu; u++) {
  1768. uvs[u + lu] = uvs[u];
  1769. }
  1770. break;
  1771. }
  1772. }
  1773. public static ImportVertexData(parsedVertexData: any, geometry: Geometry) {
  1774. var vertexData = new VertexData();
  1775. // positions
  1776. var positions = parsedVertexData.positions;
  1777. if (positions) {
  1778. vertexData.set(positions, VertexBuffer.PositionKind);
  1779. }
  1780. // normals
  1781. var normals = parsedVertexData.normals;
  1782. if (normals) {
  1783. vertexData.set(normals, VertexBuffer.NormalKind);
  1784. }
  1785. // uvs
  1786. var uvs = parsedVertexData.uvs;
  1787. if (uvs) {
  1788. vertexData.set(uvs, VertexBuffer.UVKind);
  1789. }
  1790. // uv2s
  1791. var uv2s = parsedVertexData.uv2s;
  1792. if (uv2s) {
  1793. vertexData.set(uv2s, VertexBuffer.UV2Kind);
  1794. }
  1795. // uv3s
  1796. var uv3s = parsedVertexData.uv3s;
  1797. if (uv3s) {
  1798. vertexData.set(uv3s, VertexBuffer.UV3Kind);
  1799. }
  1800. // uv4s
  1801. var uv4s = parsedVertexData.uv4s;
  1802. if (uv4s) {
  1803. vertexData.set(uv4s, VertexBuffer.UV4Kind);
  1804. }
  1805. // uv5s
  1806. var uv5s = parsedVertexData.uv5s;
  1807. if (uv5s) {
  1808. vertexData.set(uv5s, VertexBuffer.UV5Kind);
  1809. }
  1810. // uv6s
  1811. var uv6s = parsedVertexData.uv6s;
  1812. if (uv6s) {
  1813. vertexData.set(uv6s, VertexBuffer.UV6Kind);
  1814. }
  1815. // colors
  1816. var colors = parsedVertexData.colors;
  1817. if (colors) {
  1818. vertexData.set(Color4.CheckColors4(colors, positions.length / 3), VertexBuffer.ColorKind);
  1819. }
  1820. // matricesIndices
  1821. var matricesIndices = parsedVertexData.matricesIndices;
  1822. if (matricesIndices) {
  1823. vertexData.set(matricesIndices, VertexBuffer.MatricesIndicesKind);
  1824. }
  1825. // matricesWeights
  1826. var matricesWeights = parsedVertexData.matricesWeights;
  1827. if (matricesWeights) {
  1828. vertexData.set(matricesWeights, VertexBuffer.MatricesWeightsKind);
  1829. }
  1830. // indices
  1831. var indices = parsedVertexData.indices;
  1832. if (indices) {
  1833. vertexData.indices = indices;
  1834. }
  1835. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  1836. }
  1837. }
  1838. }