babylon.mesh.vertexData.ts 95 KB

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