babylon.mesh.vertexData.ts 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. module BABYLON {
  2. export type IndicesArray = number[] | Int32Array | Uint32Array | Uint16Array;
  3. export interface IGetSetVerticesData
  4. {
  5. isVerticesDataPresent(kind: string): boolean;
  6. getVerticesData(kind: string, copyWhenShared?: boolean, forceCopy?: boolean): number[] | Float32Array;
  7. getIndices(copyWhenShared?: boolean): IndicesArray;
  8. setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean): void;
  9. updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): void;
  10. setIndices(indices: IndicesArray): void;
  11. }
  12. export class VertexData {
  13. public positions: number[] | Float32Array;
  14. public normals: number[] | Float32Array;
  15. public tangents: number[] | Float32Array;
  16. public uvs: number[] | Float32Array;
  17. public uvs2: number[] | Float32Array;
  18. public uvs3: number[] | Float32Array;
  19. public uvs4: number[] | Float32Array;
  20. public uvs5: number[] | Float32Array;
  21. public uvs6: number[] | Float32Array;
  22. public colors: number[] | Float32Array;
  23. public matricesIndices: number[] | Float32Array;
  24. public matricesWeights: number[] | Float32Array;
  25. public matricesIndicesExtra: number[] | Float32Array;
  26. public matricesWeightsExtra: number[] | Float32Array;
  27. public indices: IndicesArray;
  28. public set(data: number[] | Float32Array, kind: string) {
  29. switch (kind) {
  30. case VertexBuffer.PositionKind:
  31. this.positions = data;
  32. break;
  33. case VertexBuffer.NormalKind:
  34. this.normals = data;
  35. break;
  36. case VertexBuffer.TangentKind:
  37. this.tangents = data;
  38. break;
  39. case VertexBuffer.UVKind:
  40. this.uvs = data;
  41. break;
  42. case VertexBuffer.UV2Kind:
  43. this.uvs2 = data;
  44. break;
  45. case VertexBuffer.UV3Kind:
  46. this.uvs3 = data;
  47. break;
  48. case VertexBuffer.UV4Kind:
  49. this.uvs4 = data;
  50. break;
  51. case VertexBuffer.UV5Kind:
  52. this.uvs5 = data;
  53. break;
  54. case VertexBuffer.UV6Kind:
  55. this.uvs6 = data;
  56. break;
  57. case VertexBuffer.ColorKind:
  58. this.colors = data;
  59. break;
  60. case VertexBuffer.MatricesIndicesKind:
  61. this.matricesIndices = data;
  62. break;
  63. case VertexBuffer.MatricesWeightsKind:
  64. this.matricesWeights = data;
  65. break;
  66. case VertexBuffer.MatricesIndicesExtraKind:
  67. this.matricesIndicesExtra = data;
  68. break;
  69. case VertexBuffer.MatricesWeightsExtraKind:
  70. this.matricesWeightsExtra = data;
  71. break;
  72. }
  73. }
  74. /**
  75. * Associates the vertexData to the passed Mesh.
  76. * Sets it as updatable or not (default `false`).
  77. * Returns the VertexData.
  78. */
  79. public applyToMesh(mesh: Mesh, updatable?: boolean): VertexData {
  80. this._applyTo(mesh, updatable);
  81. return this;
  82. }
  83. /**
  84. * Associates the vertexData to the passed Geometry.
  85. * Sets it as updatable or not (default `false`).
  86. * Returns the VertexData.
  87. */
  88. public applyToGeometry(geometry: Geometry, updatable?: boolean): VertexData {
  89. this._applyTo(geometry, updatable);
  90. return this;
  91. }
  92. /**
  93. * Updates the associated mesh.
  94. * Returns the VertexData.
  95. */
  96. public updateMesh(mesh: Mesh, updateExtends?: boolean, makeItUnique?: boolean): VertexData {
  97. this._update(mesh);
  98. return this;
  99. }
  100. /**
  101. * Updates the associated geometry.
  102. * Returns the VertexData.
  103. */
  104. public updateGeometry(geometry: Geometry, updateExtends?: boolean, makeItUnique?: boolean): VertexData {
  105. this._update(geometry);
  106. return this;
  107. }
  108. private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable?: boolean): VertexData {
  109. if (this.positions) {
  110. meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
  111. }
  112. if (this.normals) {
  113. meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
  114. }
  115. if (this.tangents) {
  116. meshOrGeometry.setVerticesData(VertexBuffer.TangentKind, this.tangents, updatable);
  117. }
  118. if (this.uvs) {
  119. meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
  120. }
  121. if (this.uvs2) {
  122. meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
  123. }
  124. if (this.uvs3) {
  125. meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
  126. }
  127. if (this.uvs4) {
  128. meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
  129. }
  130. if (this.uvs5) {
  131. meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
  132. }
  133. if (this.uvs6) {
  134. meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
  135. }
  136. if (this.colors) {
  137. meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
  138. }
  139. if (this.matricesIndices) {
  140. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  141. }
  142. if (this.matricesWeights) {
  143. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  144. }
  145. if (this.matricesIndicesExtra) {
  146. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updatable);
  147. }
  148. if (this.matricesWeightsExtra) {
  149. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updatable);
  150. }
  151. if (this.indices) {
  152. meshOrGeometry.setIndices(this.indices);
  153. }
  154. return this;
  155. }
  156. private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean): VertexData {
  157. if (this.positions) {
  158. meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  159. }
  160. if (this.normals) {
  161. meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  162. }
  163. if (this.tangents) {
  164. meshOrGeometry.updateVerticesData(VertexBuffer.TangentKind, this.tangents, updateExtends, makeItUnique);
  165. }
  166. if (this.uvs) {
  167. meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  168. }
  169. if (this.uvs2) {
  170. meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  171. }
  172. if (this.uvs3) {
  173. meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  174. }
  175. if (this.uvs4) {
  176. meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  177. }
  178. if (this.uvs5) {
  179. meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  180. }
  181. if (this.uvs6) {
  182. meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  183. }
  184. if (this.colors) {
  185. meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  186. }
  187. if (this.matricesIndices) {
  188. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  189. }
  190. if (this.matricesWeights) {
  191. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  192. }
  193. if (this.matricesIndicesExtra) {
  194. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updateExtends, makeItUnique);
  195. }
  196. if (this.matricesWeightsExtra) {
  197. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updateExtends, makeItUnique);
  198. }
  199. if (this.indices) {
  200. meshOrGeometry.setIndices(this.indices);
  201. }
  202. return this;
  203. }
  204. /**
  205. * Transforms each position and each normal of the vertexData according to the passed Matrix.
  206. * Returns the VertexData.
  207. */
  208. public transform(matrix: Matrix): VertexData {
  209. var transformed = Vector3.Zero();
  210. var index: number;
  211. if (this.positions) {
  212. var position = Vector3.Zero();
  213. for (index = 0; index < this.positions.length; index += 3) {
  214. Vector3.FromArrayToRef(this.positions, index, position);
  215. Vector3.TransformCoordinatesToRef(position, matrix, transformed);
  216. this.positions[index] = transformed.x;
  217. this.positions[index + 1] = transformed.y;
  218. this.positions[index + 2] = transformed.z;
  219. }
  220. }
  221. if (this.normals) {
  222. var normal = Vector3.Zero();
  223. for (index = 0; index < this.normals.length; index += 3) {
  224. Vector3.FromArrayToRef(this.normals, index, normal);
  225. Vector3.TransformNormalToRef(normal, matrix, transformed);
  226. this.normals[index] = transformed.x;
  227. this.normals[index + 1] = transformed.y;
  228. this.normals[index + 2] = transformed.z;
  229. }
  230. }
  231. if (this.tangents) {
  232. var tangent = Vector4.Zero();
  233. var tangentTransformed = Vector4.Zero();
  234. for (index = 0; index < this.tangents.length; index += 4) {
  235. Vector4.FromArrayToRef(this.tangents, index, tangent);
  236. Vector4.TransformNormalToRef(tangent, matrix, tangentTransformed);
  237. this.tangents[index] = tangentTransformed.x;
  238. this.tangents[index + 1] = tangentTransformed.y;
  239. this.tangents[index + 2] = tangentTransformed.z;
  240. this.tangents[index + 3] = tangentTransformed.w;
  241. }
  242. }
  243. return this;
  244. }
  245. /**
  246. * Merges the passed VertexData into the current one.
  247. * Returns the modified VertexData.
  248. */
  249. public merge(other: VertexData): VertexData {
  250. if (other.indices) {
  251. if (!this.indices) {
  252. this.indices = [];
  253. }
  254. var offset = this.positions ? this.positions.length / 3 : 0;
  255. for (var index = 0; index < other.indices.length; index++) {
  256. //TODO check type - if Int32Array | Uint32Array | Uint16Array!
  257. (<number[]>this.indices).push(other.indices[index] + offset);
  258. }
  259. }
  260. this.positions = this._mergeElement(this.positions, other.positions);
  261. this.normals = this._mergeElement(this.normals, other.normals);
  262. this.tangents = this._mergeElement(this.tangents, other.tangents);
  263. this.uvs = this._mergeElement(this.uvs, other.uvs);
  264. this.uvs2 = this._mergeElement(this.uvs2, other.uvs2);
  265. this.uvs3 = this._mergeElement(this.uvs3, other.uvs3);
  266. this.uvs4 = this._mergeElement(this.uvs4, other.uvs4);
  267. this.uvs5 = this._mergeElement(this.uvs5, other.uvs5);
  268. this.uvs6 = this._mergeElement(this.uvs6, other.uvs6);
  269. this.colors = this._mergeElement(this.colors, other.colors);
  270. this.matricesIndices = this._mergeElement(this.matricesIndices, other.matricesIndices);
  271. this.matricesWeights = this._mergeElement(this.matricesWeights, other.matricesWeights);
  272. this.matricesIndicesExtra = this._mergeElement(this.matricesIndicesExtra, other.matricesIndicesExtra);
  273. this.matricesWeightsExtra = this._mergeElement(this.matricesWeightsExtra, other.matricesWeightsExtra);
  274. return this;
  275. }
  276. private _mergeElement(source: number[] | Float32Array, other: number[] | Float32Array): number[] | Float32Array {
  277. if (!other && !source) {
  278. return null;
  279. }
  280. if (!other) {
  281. return this._mergeElement(source, new Float32Array(source.length));
  282. }
  283. if (!source) {
  284. return this._mergeElement(new Float32Array(other.length), other);
  285. }
  286. var len = other.length + source.length;
  287. var isSrcTypedArray = source instanceof Float32Array;
  288. var isOthTypedArray = other instanceof Float32Array;
  289. // use non-loop method when the source is Float32Array
  290. if (isSrcTypedArray) {
  291. var ret32 = new Float32Array(len);
  292. ret32.set(source);
  293. ret32.set(other, source.length);
  294. return ret32;
  295. // source is number[], when other is also use concat
  296. } else if (!isOthTypedArray) {
  297. return (<number[]>source).concat(<number[]>other);
  298. // source is a number[], but other is a Float32Array, loop required
  299. } else {
  300. var ret = (<number[]>source).slice(0); // copy source to a separate array
  301. for (var i = 0, len = other.length; i < len; i++) {
  302. ret.push(other[i]);
  303. }
  304. return ret;
  305. }
  306. }
  307. /**
  308. * Serializes the VertexData.
  309. * Returns a serialized object.
  310. */
  311. public serialize(): any {
  312. var serializationObject = this.serialize();
  313. if (this.positions) {
  314. serializationObject.positions = this.positions;
  315. }
  316. if (this.normals) {
  317. serializationObject.normals = this.normals;
  318. }
  319. if (this.tangents) {
  320. serializationObject.tangents = this.tangents;
  321. }
  322. if (this.uvs) {
  323. serializationObject.uvs = this.uvs;
  324. }
  325. if (this.uvs2) {
  326. serializationObject.uvs2 = this.uvs2;
  327. }
  328. if (this.uvs3) {
  329. serializationObject.uvs3 = this.uvs3;
  330. }
  331. if (this.uvs4) {
  332. serializationObject.uvs4 = this.uvs4;
  333. }
  334. if (this.uvs5) {
  335. serializationObject.uvs5 = this.uvs5;
  336. }
  337. if (this.uvs6) {
  338. serializationObject.uvs6 = this.uvs6;
  339. }
  340. if (this.colors) {
  341. serializationObject.colors = this.colors;
  342. }
  343. if (this.matricesIndices) {
  344. serializationObject.matricesIndices = this.matricesIndices;
  345. serializationObject.matricesIndices._isExpanded = true;
  346. }
  347. if (this.matricesWeights) {
  348. serializationObject.matricesWeights = this.matricesWeights;
  349. }
  350. if (this.matricesIndicesExtra) {
  351. serializationObject.matricesIndicesExtra = this.matricesIndicesExtra;
  352. serializationObject.matricesIndicesExtra._isExpanded = true;
  353. }
  354. if (this.matricesWeightsExtra) {
  355. serializationObject.matricesWeightsExtra = this.matricesWeightsExtra;
  356. }
  357. serializationObject.indices = this.indices;
  358. return serializationObject;
  359. }
  360. // Statics
  361. /**
  362. * Returns the object VertexData associated to the passed mesh.
  363. */
  364. public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
  365. return VertexData._ExtractFrom(mesh, copyWhenShared, forceCopy);
  366. }
  367. /**
  368. * Returns the object VertexData associated to the passed geometry.
  369. */
  370. public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
  371. return VertexData._ExtractFrom(geometry, copyWhenShared, forceCopy);
  372. }
  373. private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
  374. var result = new VertexData();
  375. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  376. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared, forceCopy);
  377. }
  378. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  379. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared, forceCopy);
  380. }
  381. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.TangentKind)) {
  382. result.tangents = meshOrGeometry.getVerticesData(VertexBuffer.TangentKind, copyWhenShared, forceCopy);
  383. }
  384. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  385. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared, forceCopy);
  386. }
  387. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  388. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared, forceCopy);
  389. }
  390. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  391. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared, forceCopy);
  392. }
  393. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  394. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared, forceCopy);
  395. }
  396. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  397. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared, forceCopy);
  398. }
  399. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  400. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared, forceCopy);
  401. }
  402. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  403. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared, forceCopy);
  404. }
  405. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  406. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared, forceCopy);
  407. }
  408. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  409. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared, forceCopy);
  410. }
  411. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesExtraKind)) {
  412. result.matricesIndicesExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesExtraKind, copyWhenShared, forceCopy);
  413. }
  414. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsExtraKind)) {
  415. result.matricesWeightsExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsExtraKind, copyWhenShared, forceCopy);
  416. }
  417. result.indices = meshOrGeometry.getIndices(copyWhenShared);
  418. return result;
  419. }
  420. /**
  421. * Creates the vertexData of the Ribbon.
  422. */
  423. public static CreateRibbon(options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, invertUV?: boolean, uvs?: Vector2[], colors?: Color4[] }): VertexData {
  424. var pathArray: Vector3[][] = options.pathArray;
  425. var closeArray: boolean = options.closeArray || false;
  426. var closePath: boolean = options.closePath || false;
  427. var invertUV: boolean = options.invertUV || false;
  428. var defaultOffset: number = Math.floor(pathArray[0].length / 2);
  429. var offset: number = options.offset || defaultOffset;
  430. offset = offset > defaultOffset ? defaultOffset : Math.floor(offset); // offset max allowed : defaultOffset
  431. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  432. var customUV = options.uvs;
  433. var customColors = options.colors;
  434. var positions: number[] = [];
  435. var indices: number[] = [];
  436. var normals: number[] = [];
  437. var uvs: number[] = [];
  438. var us: number[][] = []; // us[path_id] = [uDist1, uDist2, uDist3 ... ] distances between points on path path_id
  439. var vs: number[][] = []; // vs[i] = [vDist1, vDist2, vDist3, ... ] distances between points i of consecutives paths from pathArray
  440. var uTotalDistance: number[] = []; // uTotalDistance[p] : total distance of path p
  441. var vTotalDistance: number[] = []; // vTotalDistance[i] : total distance between points i of first and last path from pathArray
  442. var minlg: number; // minimal length among all paths from pathArray
  443. var lg: number[] = []; // array of path lengths : nb of vertex per path
  444. var idx: number[] = []; // array of path indexes : index of each path (first vertex) in the total vertex number
  445. var p: number; // path iterator
  446. var i: number; // point iterator
  447. var j: number; // point iterator
  448. // if single path in pathArray
  449. if (pathArray.length < 2) {
  450. var ar1: Vector3[] = [];
  451. var ar2: Vector3[] = [];
  452. for (i = 0; i < pathArray[0].length - offset; i++) {
  453. ar1.push(pathArray[0][i]);
  454. ar2.push(pathArray[0][i + offset]);
  455. }
  456. pathArray = [ar1, ar2];
  457. }
  458. // positions and horizontal distances (u)
  459. var idc: number = 0;
  460. var closePathCorr: number = (closePath) ? 1 : 0; // the final index will be +1 if closePath
  461. var path: Vector3[];
  462. var l: number;
  463. minlg = pathArray[0].length;
  464. var vectlg: number;
  465. var dist: number;
  466. for (p = 0; p < pathArray.length; p++) {
  467. uTotalDistance[p] = 0;
  468. us[p] = [0];
  469. path = pathArray[p];
  470. l = path.length;
  471. minlg = (minlg < l) ? minlg : l;
  472. j = 0;
  473. while (j < l) {
  474. positions.push(path[j].x, path[j].y, path[j].z);
  475. if (j > 0) {
  476. vectlg = path[j].subtract(path[j - 1]).length();
  477. dist = vectlg + uTotalDistance[p];
  478. us[p].push(dist);
  479. uTotalDistance[p] = dist;
  480. }
  481. j++;
  482. }
  483. if (closePath) { // an extra hidden vertex is added in the "positions" array
  484. j--;
  485. positions.push(path[0].x, path[0].y, path[0].z);
  486. vectlg = path[j].subtract(path[0]).length();
  487. dist = vectlg + uTotalDistance[p];
  488. us[p].push(dist);
  489. uTotalDistance[p] = dist;
  490. }
  491. lg[p] = l + closePathCorr;
  492. idx[p] = idc;
  493. idc += (l + closePathCorr);
  494. }
  495. // vertical distances (v)
  496. var path1: Vector3[];
  497. var path2: Vector3[];
  498. var vertex1: Vector3;
  499. var vertex2: Vector3;
  500. for (i = 0; i < minlg + closePathCorr; i++) {
  501. vTotalDistance[i] = 0;
  502. vs[i] = [0];
  503. for (p = 0; p < pathArray.length - 1; p++) {
  504. path1 = pathArray[p];
  505. path2 = pathArray[p + 1];
  506. if (i === minlg) { // closePath
  507. vertex1 = path1[0];
  508. vertex2 = path2[0];
  509. }
  510. else {
  511. vertex1 = path1[i];
  512. vertex2 = path2[i];
  513. }
  514. vectlg = vertex2.subtract(vertex1).length();
  515. dist = vectlg + vTotalDistance[i];
  516. vs[i].push(dist);
  517. vTotalDistance[i] = dist;
  518. }
  519. if (closeArray) {
  520. path1 = pathArray[p];
  521. path2 = pathArray[0];
  522. if (i === minlg) { // closePath
  523. vertex2 = path2[0];
  524. }
  525. vectlg = vertex2.subtract(vertex1).length();
  526. dist = vectlg + vTotalDistance[i];
  527. vTotalDistance[i] = dist;
  528. }
  529. }
  530. // uvs
  531. var u: number;
  532. var v: number;
  533. if (customUV) {
  534. for (p = 0; p < customUV.length; p++) {
  535. uvs.push(customUV[p].x, customUV[p].y);
  536. }
  537. }
  538. else {
  539. for (p = 0; p < pathArray.length; p++) {
  540. for (i = 0; i < minlg + closePathCorr; i++) {
  541. u = (uTotalDistance[p] != 0.0) ? us[p][i] / uTotalDistance[p] : 0.0;
  542. v = (vTotalDistance[i] != 0.0) ? vs[i][p] / vTotalDistance[i] : 0.0;
  543. if (invertUV) {
  544. uvs.push(v, u);
  545. } else {
  546. uvs.push(u, v);
  547. }
  548. }
  549. }
  550. }
  551. // indices
  552. p = 0; // path index
  553. var pi: number = 0; // positions array index
  554. var l1: number = lg[p] - 1; // path1 length
  555. var l2: number = lg[p + 1] - 1; // path2 length
  556. var min: number = (l1 < l2) ? l1 : l2; // current path stop index
  557. var shft: number = idx[1] - idx[0]; // shift
  558. var path1nb: number = closeArray ? lg.length : lg.length - 1; // number of path1 to iterate on
  559. while (pi <= min && p < path1nb) { // stay under min and don't go over next to last path
  560. // 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
  561. indices.push(pi, pi + shft, pi + 1);
  562. indices.push(pi + shft + 1, pi + 1, pi + shft);
  563. pi += 1;
  564. if (pi === min) { // if end of one of two consecutive paths reached, go to next existing path
  565. p++;
  566. if (p === lg.length - 1) { // last path of pathArray reached <=> closeArray == true
  567. shft = idx[0] - idx[p];
  568. l1 = lg[p] - 1;
  569. l2 = lg[0] - 1;
  570. }
  571. else {
  572. shft = idx[p + 1] - idx[p];
  573. l1 = lg[p] - 1;
  574. l2 = lg[p + 1] - 1;
  575. }
  576. pi = idx[p];
  577. min = (l1 < l2) ? l1 + pi : l2 + pi;
  578. }
  579. }
  580. // normals
  581. VertexData.ComputeNormals(positions, indices, normals);
  582. if (closePath) { // update both the first and last vertex normals to their average value
  583. var indexFirst: number = 0;
  584. var indexLast: number = 0;
  585. for (p = 0; p < pathArray.length; p++) {
  586. indexFirst = idx[p] * 3;
  587. if (p + 1 < pathArray.length) {
  588. indexLast = (idx[p + 1] - 1) * 3;
  589. }
  590. else {
  591. indexLast = normals.length - 3;
  592. }
  593. normals[indexFirst] = (normals[indexFirst] + normals[indexLast]) * 0.5;
  594. normals[indexFirst + 1] = (normals[indexFirst + 1] + normals[indexLast + 1]) * 0.5;
  595. normals[indexFirst + 2] = (normals[indexFirst + 2] + normals[indexLast + 2]) * 0.5;
  596. normals[indexLast] = normals[indexFirst];
  597. normals[indexLast + 1] = normals[indexFirst + 1];
  598. normals[indexLast + 2] = normals[indexFirst + 2];
  599. }
  600. }
  601. // sides
  602. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  603. // Colors
  604. if (customColors) {
  605. var colors = new Float32Array(customColors.length * 4);
  606. for (var c = 0; c < customColors.length; c++) {
  607. colors[c * 4] = customColors[c].r;
  608. colors[c * 4 + 1] = customColors[c].g;
  609. colors[c * 4 + 2] = customColors[c].b;
  610. colors[c * 4 + 3] = customColors[c].a;
  611. }
  612. }
  613. // Result
  614. var vertexData = new VertexData();
  615. var positions32 = new Float32Array(positions);
  616. var normals32 = new Float32Array(normals);
  617. var uvs32 = new Float32Array(uvs);
  618. vertexData.indices = indices;
  619. vertexData.positions = positions32;
  620. vertexData.normals = normals32;
  621. vertexData.uvs = uvs32;
  622. if (customColors) {
  623. vertexData.set(colors, VertexBuffer.ColorKind);
  624. }
  625. if (closePath) {
  626. (<any>vertexData)._idx = idx;
  627. }
  628. return vertexData;
  629. }
  630. /**
  631. * Creates the VertexData of the Box.
  632. */
  633. public static CreateBox(options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  634. var normalsSource = [
  635. new Vector3(0, 0, 1),
  636. new Vector3(0, 0, -1),
  637. new Vector3(1, 0, 0),
  638. new Vector3(-1, 0, 0),
  639. new Vector3(0, 1, 0),
  640. new Vector3(0, -1, 0)
  641. ];
  642. var indices = [];
  643. var positions = [];
  644. var normals = [];
  645. var uvs = [];
  646. var width = options.width || options.size || 1;
  647. var height = options.height || options.size || 1;
  648. var depth = options.depth || options.size || 1;
  649. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  650. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(6);
  651. var faceColors: Color4[] = options.faceColors;
  652. var colors = [];
  653. // default face colors and UV if undefined
  654. for (var f = 0; f < 6; f++) {
  655. if (faceUV[f] === undefined) {
  656. faceUV[f] = new Vector4(0, 0, 1, 1);
  657. }
  658. if (faceColors && faceColors[f] === undefined) {
  659. faceColors[f] = new Color4(1, 1, 1, 1);
  660. }
  661. }
  662. var scaleVector = new Vector3(width / 2, height / 2, depth / 2);
  663. // Create each face in turn.
  664. for (var index = 0; index < normalsSource.length; index++) {
  665. var normal = normalsSource[index];
  666. // Get two vectors perpendicular to the face normal and to each other.
  667. var side1 = new Vector3(normal.y, normal.z, normal.x);
  668. var side2 = Vector3.Cross(normal, side1);
  669. // Six indices (two triangles) per face.
  670. var verticesLength = positions.length / 3;
  671. indices.push(verticesLength);
  672. indices.push(verticesLength + 1);
  673. indices.push(verticesLength + 2);
  674. indices.push(verticesLength);
  675. indices.push(verticesLength + 2);
  676. indices.push(verticesLength + 3);
  677. // Four vertices per face.
  678. var vertex = normal.subtract(side1).subtract(side2).multiply(scaleVector);
  679. positions.push(vertex.x, vertex.y, vertex.z);
  680. normals.push(normal.x, normal.y, normal.z);
  681. uvs.push(faceUV[index].z, faceUV[index].w);
  682. if (faceColors) {
  683. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  684. }
  685. vertex = normal.subtract(side1).add(side2).multiply(scaleVector);
  686. positions.push(vertex.x, vertex.y, vertex.z);
  687. normals.push(normal.x, normal.y, normal.z);
  688. uvs.push(faceUV[index].x, faceUV[index].w);
  689. if (faceColors) {
  690. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  691. }
  692. vertex = normal.add(side1).add(side2).multiply(scaleVector);
  693. positions.push(vertex.x, vertex.y, vertex.z);
  694. normals.push(normal.x, normal.y, normal.z);
  695. uvs.push(faceUV[index].x, faceUV[index].y);
  696. if (faceColors) {
  697. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  698. }
  699. vertex = normal.add(side1).subtract(side2).multiply(scaleVector);
  700. positions.push(vertex.x, vertex.y, vertex.z);
  701. normals.push(normal.x, normal.y, normal.z);
  702. uvs.push(faceUV[index].z, faceUV[index].y);
  703. if (faceColors) {
  704. colors.push(faceColors[index].r, faceColors[index].g, faceColors[index].b, faceColors[index].a);
  705. }
  706. }
  707. // sides
  708. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  709. // Result
  710. var vertexData = new VertexData();
  711. vertexData.indices = indices;
  712. vertexData.positions = positions;
  713. vertexData.normals = normals;
  714. vertexData.uvs = uvs;
  715. if (faceColors) {
  716. var totalColors = (sideOrientation === Mesh.DOUBLESIDE) ? colors.concat(colors) : colors;
  717. vertexData.colors = totalColors;
  718. }
  719. return vertexData;
  720. }
  721. /**
  722. * Creates the VertexData of the Sphere.
  723. */
  724. public static CreateSphere(options: { segments?: number, diameter?: number, diameterX?: number, diameterY?: number, diameterZ?: number, arc?: number, slice?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  725. var segments: number = options.segments || 32;
  726. var diameterX: number = options.diameterX || options.diameter || 1;
  727. var diameterY: number = options.diameterY || options.diameter || 1;
  728. var diameterZ: number = options.diameterZ || options.diameter || 1;
  729. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  730. var slice: number = (options.slice <= 0) ? 1.0 : options.slice || 1.0;
  731. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  732. var radius = new Vector3(diameterX / 2, diameterY / 2, diameterZ / 2);
  733. var totalZRotationSteps = 2 + segments;
  734. var totalYRotationSteps = 2 * totalZRotationSteps;
  735. var indices = [];
  736. var positions = [];
  737. var normals = [];
  738. var uvs = [];
  739. for (var zRotationStep = 0; zRotationStep <= totalZRotationSteps; zRotationStep++) {
  740. var normalizedZ = zRotationStep / totalZRotationSteps;
  741. var angleZ = normalizedZ * Math.PI * slice;
  742. for (var yRotationStep = 0; yRotationStep <= totalYRotationSteps; yRotationStep++) {
  743. var normalizedY = yRotationStep / totalYRotationSteps;
  744. var angleY = normalizedY * Math.PI * 2 * arc;
  745. var rotationZ = Matrix.RotationZ(-angleZ);
  746. var rotationY = Matrix.RotationY(angleY);
  747. var afterRotZ = Vector3.TransformCoordinates(Vector3.Up(), rotationZ);
  748. var complete = Vector3.TransformCoordinates(afterRotZ, rotationY);
  749. var vertex = complete.multiply(radius);
  750. var normal = complete.divide(radius).normalize();
  751. positions.push(vertex.x, vertex.y, vertex.z);
  752. normals.push(normal.x, normal.y, normal.z);
  753. uvs.push(normalizedY, normalizedZ);
  754. }
  755. if (zRotationStep > 0) {
  756. var verticesCount = positions.length / 3;
  757. for (var firstIndex = verticesCount - 2 * (totalYRotationSteps + 1); (firstIndex + totalYRotationSteps + 2) < verticesCount; firstIndex++) {
  758. indices.push((firstIndex));
  759. indices.push((firstIndex + 1));
  760. indices.push(firstIndex + totalYRotationSteps + 1);
  761. indices.push((firstIndex + totalYRotationSteps + 1));
  762. indices.push((firstIndex + 1));
  763. indices.push((firstIndex + totalYRotationSteps + 2));
  764. }
  765. }
  766. }
  767. // Sides
  768. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  769. // Result
  770. var vertexData = new VertexData();
  771. vertexData.indices = indices;
  772. vertexData.positions = positions;
  773. vertexData.normals = normals;
  774. vertexData.uvs = uvs;
  775. return vertexData;
  776. }
  777. /**
  778. * Creates the VertexData of the Cylinder or Cone.
  779. */
  780. 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, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  781. var height: number = options.height || 2;
  782. var diameterTop: number = (options.diameterTop === 0) ? 0 : options.diameterTop || options.diameter || 1;
  783. var diameterBottom: number = (options.diameterBottom === 0) ? 0 : options.diameterBottom || options.diameter || 1;
  784. var tessellation: number = options.tessellation || 24;
  785. var subdivisions: number = options.subdivisions || 1;
  786. var hasRings: boolean = options.hasRings;
  787. var enclose: boolean = options.enclose;
  788. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  789. var sideOrientation: number = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  790. var faceUV: Vector4[] = options.faceUV || new Array<Vector4>(3);
  791. var faceColors: Color4[] = options.faceColors;
  792. // default face colors and UV if undefined
  793. var quadNb: number = (arc !== 1 && enclose) ? 2 : 0;
  794. var ringNb: number = (hasRings) ? subdivisions : 1;
  795. var surfaceNb: number = 2 + (1 + quadNb) * ringNb;
  796. var f: number;
  797. for (f = 0; f < surfaceNb; f++) {
  798. if (faceColors && faceColors[f] === undefined) {
  799. faceColors[f] = new Color4(1, 1, 1, 1);
  800. }
  801. }
  802. for (f = 0; f < surfaceNb; f++) {
  803. if (faceUV && faceUV[f] === undefined) {
  804. faceUV[f] = new Vector4(0, 0, 1, 1);
  805. }
  806. }
  807. var indices = [];
  808. var positions = [];
  809. var normals = [];
  810. var uvs = [];
  811. var colors = [];
  812. var angle_step = Math.PI * 2 * arc / tessellation;
  813. var angle: number;
  814. var h: number;
  815. var radius: number;
  816. var tan = (diameterBottom - diameterTop) / 2 / height;
  817. var ringVertex: Vector3 = Vector3.Zero();
  818. var ringNormal: Vector3 = Vector3.Zero();
  819. var ringFirstVertex: Vector3 = Vector3.Zero();
  820. var ringFirstNormal: Vector3 = Vector3.Zero();
  821. var quadNormal: Vector3 = Vector3.Zero();
  822. var Y: Vector3 = Axis.Y;
  823. // positions, normals, uvs
  824. var i: number;
  825. var j: number;
  826. var r: number;
  827. var ringIdx: number = 1;
  828. var s: number = 1; // surface index
  829. var cs: number = 0;
  830. var v: number = 0;
  831. for (i = 0; i <= subdivisions; i++) {
  832. h = i / subdivisions;
  833. radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
  834. ringIdx = (hasRings && i !== 0 && i !== subdivisions) ? 2 : 1;
  835. for (r = 0; r < ringIdx; r++) {
  836. if (hasRings) {
  837. s += r;
  838. }
  839. if (enclose) {
  840. s += 2 * r;
  841. }
  842. for (j = 0; j <= tessellation; j++) {
  843. angle = j * angle_step;
  844. // position
  845. ringVertex.x = Math.cos(-angle) * radius;
  846. ringVertex.y = -height / 2 + h * height;
  847. ringVertex.z = Math.sin(-angle) * radius;
  848. // normal
  849. if (diameterTop === 0 && i === subdivisions) {
  850. // if no top cap, reuse former normals
  851. ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
  852. ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
  853. ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
  854. }
  855. else {
  856. ringNormal.x = ringVertex.x;
  857. ringNormal.z = ringVertex.z;
  858. ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
  859. ringNormal.normalize();
  860. }
  861. // keep first ring vertex values for enclose
  862. if (j === 0) {
  863. ringFirstVertex.copyFrom(ringVertex);
  864. ringFirstNormal.copyFrom(ringNormal);
  865. }
  866. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  867. normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
  868. if (hasRings) {
  869. v = (cs !== s) ? faceUV[s].y : faceUV[s].w;
  870. } else {
  871. v = faceUV[s].y + (faceUV[s].w - faceUV[s].y) * h;
  872. }
  873. uvs.push(faceUV[s].x + (faceUV[s].z - faceUV[s].x) * j / tessellation, v);
  874. if (faceColors) {
  875. colors.push(faceColors[s].r, faceColors[s].g, faceColors[s].b, faceColors[s].a);
  876. }
  877. }
  878. // if enclose, add four vertices and their dedicated normals
  879. if (arc !== 1 && enclose) {
  880. positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
  881. positions.push(0, ringVertex.y, 0);
  882. positions.push(0, ringVertex.y, 0);
  883. positions.push(ringFirstVertex.x, ringFirstVertex.y, ringFirstVertex.z);
  884. Vector3.CrossToRef(Y, ringNormal, quadNormal);
  885. quadNormal.normalize();
  886. normals.push(quadNormal.x, quadNormal.y, quadNormal.z, quadNormal.x, quadNormal.y, quadNormal.z);
  887. Vector3.CrossToRef(ringFirstNormal, Y, quadNormal);
  888. quadNormal.normalize();
  889. normals.push(quadNormal.x, quadNormal.y, quadNormal.z, quadNormal.x, quadNormal.y, quadNormal.z);
  890. if (hasRings) {
  891. v = (cs !== s) ? faceUV[s + 1].y : faceUV[s + 1].w;
  892. } else {
  893. v = faceUV[s + 1].y + (faceUV[s + 1].w - faceUV[s + 1].y) * h;
  894. }
  895. uvs.push(faceUV[s + 1].x, v);
  896. uvs.push(faceUV[s + 1].z, v);
  897. if (hasRings) {
  898. v = (cs !== s) ? faceUV[s + 2].y : faceUV[s + 2].w;
  899. } else {
  900. v = faceUV[s + 2].y + (faceUV[s + 2].w - faceUV[s + 2].y) * h;
  901. }
  902. uvs.push(faceUV[s + 2].x, v);
  903. uvs.push(faceUV[s + 2].z, v);
  904. if (faceColors) {
  905. colors.push(faceColors[s + 1].r, faceColors[s + 1].g, faceColors[s + 1].b, faceColors[s + 1].a);
  906. colors.push(faceColors[s + 1].r, faceColors[s + 1].g, faceColors[s + 1].b, faceColors[s + 1].a);
  907. colors.push(faceColors[s + 2].r, faceColors[s + 2].g, faceColors[s + 2].b, faceColors[s + 2].a);
  908. colors.push(faceColors[s + 2].r, faceColors[s + 2].g, faceColors[s + 2].b, faceColors[s + 2].a);
  909. }
  910. }
  911. if (cs !== s) {
  912. cs = s;
  913. }
  914. }
  915. }
  916. // indices
  917. var e: number = (arc !== 1 && enclose) ? tessellation + 4 : tessellation; // correction of number of iteration if enclose
  918. var s: number;
  919. i = 0;
  920. for (s = 0; s < subdivisions; s++) {
  921. for (j = 0; j < tessellation; j++) {
  922. var i0 = i * (e + 1) + j;
  923. var i1 = (i + 1) * (e + 1) + j;
  924. var i2 = i * (e + 1) + (j + 1);
  925. var i3 = (i + 1) * (e + 1) + (j + 1);
  926. indices.push(i0, i1, i2);
  927. indices.push(i3, i2, i1);
  928. }
  929. if (arc !== 1 && enclose) { // if enclose, add two quads
  930. indices.push(i0 + 2, i1 + 2, i2 + 2);
  931. indices.push(i3 + 2, i2 + 2, i1 + 2);
  932. indices.push(i0 + 4, i1 + 4, i2 + 4);
  933. indices.push(i3 + 4, i2 + 4, i1 + 4);
  934. }
  935. i = (hasRings) ? (i + 2) : (i + 1);
  936. }
  937. // Caps
  938. var createCylinderCap = isTop => {
  939. var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
  940. if (radius === 0) {
  941. return;
  942. }
  943. // Cap positions, normals & uvs
  944. var angle;
  945. var circleVector;
  946. var i: number;
  947. var u: Vector4 = (isTop) ? faceUV[surfaceNb - 1] : faceUV[0];
  948. var c: Color4;
  949. if (faceColors) {
  950. c = (isTop) ? faceColors[surfaceNb - 1] : faceColors[0];
  951. }
  952. // cap center
  953. var vbase = positions.length / 3;
  954. var offset = isTop ? height / 2 : -height / 2;
  955. var center = new Vector3(0, offset, 0);
  956. positions.push(center.x, center.y, center.z);
  957. normals.push(0, isTop ? 1 : -1, 0);
  958. uvs.push(u.x + (u.z - u.x) * 0.5, u.y + (u.w - u.y) * 0.5);
  959. if (faceColors) {
  960. colors.push(c.r, c.g, c.b, c.a);
  961. }
  962. var textureScale = new Vector2(0.5, 0.5);
  963. for (i = 0; i <= tessellation; i++) {
  964. angle = Math.PI * 2 * i * arc / tessellation;
  965. var cos = Math.cos(-angle);
  966. var sin = Math.sin(-angle);
  967. circleVector = new Vector3(cos * radius, offset, sin * radius);
  968. var textureCoordinate = new Vector2(cos * textureScale.x + 0.5, sin * textureScale.y + 0.5);
  969. positions.push(circleVector.x, circleVector.y, circleVector.z);
  970. normals.push(0, isTop ? 1 : -1, 0);
  971. uvs.push(u.x + (u.z - u.x) * textureCoordinate.x, u.y + (u.w - u.y) * textureCoordinate.y);
  972. if (faceColors) {
  973. colors.push(c.r, c.g, c.b, c.a);
  974. }
  975. }
  976. // Cap indices
  977. for (i = 0; i < tessellation; i++) {
  978. if (!isTop) {
  979. indices.push(vbase);
  980. indices.push(vbase + (i + 1));
  981. indices.push(vbase + (i + 2));
  982. }
  983. else {
  984. indices.push(vbase);
  985. indices.push(vbase + (i + 2));
  986. indices.push(vbase + (i + 1));
  987. }
  988. }
  989. };
  990. // add caps to geometry
  991. createCylinderCap(false);
  992. createCylinderCap(true);
  993. // Sides
  994. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  995. var vertexData = new VertexData();
  996. vertexData.indices = indices;
  997. vertexData.positions = positions;
  998. vertexData.normals = normals;
  999. vertexData.uvs = uvs;
  1000. if (faceColors) {
  1001. vertexData.colors = colors;
  1002. }
  1003. return vertexData;
  1004. }
  1005. /**
  1006. * Creates the VertexData of the Torus.
  1007. */
  1008. public static CreateTorus(options: { diameter?: number, thickness?: number, tessellation?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }) {
  1009. var indices = [];
  1010. var positions = [];
  1011. var normals = [];
  1012. var uvs = [];
  1013. var diameter = options.diameter || 1;
  1014. var thickness = options.thickness || 0.5;
  1015. var tessellation = options.tessellation || 16;
  1016. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1017. var stride = tessellation + 1;
  1018. for (var i = 0; i <= tessellation; i++) {
  1019. var u = i / tessellation;
  1020. var outerAngle = i * Math.PI * 2.0 / tessellation - Math.PI / 2.0;
  1021. var transform = Matrix.Translation(diameter / 2.0, 0, 0).multiply(Matrix.RotationY(outerAngle));
  1022. for (var j = 0; j <= tessellation; j++) {
  1023. var v = 1 - j / tessellation;
  1024. var innerAngle = j * Math.PI * 2.0 / tessellation + Math.PI;
  1025. var dx = Math.cos(innerAngle);
  1026. var dy = Math.sin(innerAngle);
  1027. // Create a vertex.
  1028. var normal = new Vector3(dx, dy, 0);
  1029. var position = normal.scale(thickness / 2);
  1030. var textureCoordinate = new Vector2(u, v);
  1031. position = Vector3.TransformCoordinates(position, transform);
  1032. normal = Vector3.TransformNormal(normal, transform);
  1033. positions.push(position.x, position.y, position.z);
  1034. normals.push(normal.x, normal.y, normal.z);
  1035. uvs.push(textureCoordinate.x, textureCoordinate.y);
  1036. // And create indices for two triangles.
  1037. var nextI = (i + 1) % stride;
  1038. var nextJ = (j + 1) % stride;
  1039. indices.push(i * stride + j);
  1040. indices.push(i * stride + nextJ);
  1041. indices.push(nextI * stride + j);
  1042. indices.push(i * stride + nextJ);
  1043. indices.push(nextI * stride + nextJ);
  1044. indices.push(nextI * stride + j);
  1045. }
  1046. }
  1047. // Sides
  1048. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1049. // Result
  1050. var vertexData = new VertexData();
  1051. vertexData.indices = indices;
  1052. vertexData.positions = positions;
  1053. vertexData.normals = normals;
  1054. vertexData.uvs = uvs;
  1055. return vertexData;
  1056. }
  1057. /**
  1058. * Creates the VertexData of the LineSystem.
  1059. */
  1060. public static CreateLineSystem(options: { lines: Vector3[][] }): VertexData {
  1061. var indices = [];
  1062. var positions = [];
  1063. var lines = options.lines;
  1064. var idx = 0;
  1065. for (var l = 0; l < lines.length; l++) {
  1066. var points = lines[l];
  1067. for (var index = 0; index < points.length; index++) {
  1068. positions.push(points[index].x, points[index].y, points[index].z);
  1069. if (index > 0) {
  1070. indices.push(idx - 1);
  1071. indices.push(idx);
  1072. }
  1073. idx++;
  1074. }
  1075. }
  1076. var vertexData = new VertexData();
  1077. vertexData.indices = indices;
  1078. vertexData.positions = positions;
  1079. return vertexData;
  1080. }
  1081. /**
  1082. * Create the VertexData of the DashedLines.
  1083. */
  1084. public static CreateDashedLines(options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number }): VertexData {
  1085. var dashSize = options.dashSize || 3;
  1086. var gapSize = options.gapSize || 1;
  1087. var dashNb = options.dashNb || 200;
  1088. var points = options.points;
  1089. var positions = new Array<number>();
  1090. var indices = new Array<number>();
  1091. var curvect = Vector3.Zero();
  1092. var lg = 0;
  1093. var nb = 0;
  1094. var shft = 0;
  1095. var dashshft = 0;
  1096. var curshft = 0;
  1097. var idx = 0;
  1098. var i = 0;
  1099. for (i = 0; i < points.length - 1; i++) {
  1100. points[i + 1].subtractToRef(points[i], curvect);
  1101. lg += curvect.length();
  1102. }
  1103. shft = lg / dashNb;
  1104. dashshft = dashSize * shft / (dashSize + gapSize);
  1105. for (i = 0; i < points.length - 1; i++) {
  1106. points[i + 1].subtractToRef(points[i], curvect);
  1107. nb = Math.floor(curvect.length() / shft);
  1108. curvect.normalize();
  1109. for (var j = 0; j < nb; j++) {
  1110. curshft = shft * j;
  1111. positions.push(points[i].x + curshft * curvect.x, points[i].y + curshft * curvect.y, points[i].z + curshft * curvect.z);
  1112. positions.push(points[i].x + (curshft + dashshft) * curvect.x, points[i].y + (curshft + dashshft) * curvect.y, points[i].z + (curshft + dashshft) * curvect.z);
  1113. indices.push(idx, idx + 1);
  1114. idx += 2;
  1115. }
  1116. }
  1117. // Result
  1118. var vertexData = new VertexData();
  1119. vertexData.positions = positions;
  1120. vertexData.indices = indices;
  1121. return vertexData;
  1122. }
  1123. /**
  1124. * Creates the VertexData of the Ground.
  1125. */
  1126. public static CreateGround(options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number }): VertexData {
  1127. var indices = [];
  1128. var positions = [];
  1129. var normals = [];
  1130. var uvs = [];
  1131. var row: number, col: number;
  1132. var width: number = options.width || 1;
  1133. var height: number = options.height || 1;
  1134. var subdivisionsX: number = options.subdivisionsX || options.subdivisions || 1;
  1135. var subdivisionsY: number = options.subdivisionsY || options.subdivisions || 1;
  1136. for (row = 0; row <= subdivisionsY; row++) {
  1137. for (col = 0; col <= subdivisionsX; col++) {
  1138. var position = new Vector3((col * width) / subdivisionsX - (width / 2.0), 0, ((subdivisionsY - row) * height) / subdivisionsY - (height / 2.0));
  1139. var normal = new Vector3(0, 1.0, 0);
  1140. positions.push(position.x, position.y, position.z);
  1141. normals.push(normal.x, normal.y, normal.z);
  1142. uvs.push(col / subdivisionsX, 1.0 - row / subdivisionsY);
  1143. }
  1144. }
  1145. for (row = 0; row < subdivisionsY; row++) {
  1146. for (col = 0; col < subdivisionsX; col++) {
  1147. indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
  1148. indices.push(col + 1 + row * (subdivisionsX + 1));
  1149. indices.push(col + row * (subdivisionsX + 1));
  1150. indices.push(col + (row + 1) * (subdivisionsX + 1));
  1151. indices.push(col + 1 + (row + 1) * (subdivisionsX + 1));
  1152. indices.push(col + row * (subdivisionsX + 1));
  1153. }
  1154. }
  1155. // Result
  1156. var vertexData = new VertexData();
  1157. vertexData.indices = indices;
  1158. vertexData.positions = positions;
  1159. vertexData.normals = normals;
  1160. vertexData.uvs = uvs;
  1161. return vertexData;
  1162. }
  1163. /**
  1164. * Creates the VertexData of the TiledGround.
  1165. */
  1166. public static CreateTiledGround(options: { xmin: number, zmin: number, xmax: number, zmax: number, subdivisions?: { w: number; h: number; }, precision?: { w: number; h: number; } }): VertexData {
  1167. var xmin = options.xmin || -1.0;
  1168. var zmin = options.zmin || -1.0;
  1169. var xmax = options.xmax || 1.0;
  1170. var zmax = options.zmax || 1.0;
  1171. var subdivisions = options.subdivisions || { w: 1, h: 1 };
  1172. var precision = options.precision || { w: 1, h: 1 };
  1173. var indices = [];
  1174. var positions = [];
  1175. var normals = [];
  1176. var uvs = [];
  1177. var row: number, col: number, tileRow: number, tileCol: number;
  1178. subdivisions.h = (subdivisions.h < 1) ? 1 : subdivisions.h;
  1179. subdivisions.w = (subdivisions.w < 1) ? 1 : subdivisions.w;
  1180. precision.w = (precision.w < 1) ? 1 : precision.w;
  1181. precision.h = (precision.h < 1) ? 1 : precision.h;
  1182. var tileSize = {
  1183. 'w': (xmax - xmin) / subdivisions.w,
  1184. 'h': (zmax - zmin) / subdivisions.h
  1185. };
  1186. function applyTile(xTileMin: number, zTileMin: number, xTileMax: number, zTileMax: number) {
  1187. // Indices
  1188. var base = positions.length / 3;
  1189. var rowLength = precision.w + 1;
  1190. for (row = 0; row < precision.h; row++) {
  1191. for (col = 0; col < precision.w; col++) {
  1192. var square = [
  1193. base + col + row * rowLength,
  1194. base + (col + 1) + row * rowLength,
  1195. base + (col + 1) + (row + 1) * rowLength,
  1196. base + col + (row + 1) * rowLength
  1197. ];
  1198. indices.push(square[1]);
  1199. indices.push(square[2]);
  1200. indices.push(square[3]);
  1201. indices.push(square[0]);
  1202. indices.push(square[1]);
  1203. indices.push(square[3]);
  1204. }
  1205. }
  1206. // Position, normals and uvs
  1207. var position = Vector3.Zero();
  1208. var normal = new Vector3(0, 1.0, 0);
  1209. for (row = 0; row <= precision.h; row++) {
  1210. position.z = (row * (zTileMax - zTileMin)) / precision.h + zTileMin;
  1211. for (col = 0; col <= precision.w; col++) {
  1212. position.x = (col * (xTileMax - xTileMin)) / precision.w + xTileMin;
  1213. position.y = 0;
  1214. positions.push(position.x, position.y, position.z);
  1215. normals.push(normal.x, normal.y, normal.z);
  1216. uvs.push(col / precision.w, row / precision.h);
  1217. }
  1218. }
  1219. }
  1220. for (tileRow = 0; tileRow < subdivisions.h; tileRow++) {
  1221. for (tileCol = 0; tileCol < subdivisions.w; tileCol++) {
  1222. applyTile(
  1223. xmin + tileCol * tileSize.w,
  1224. zmin + tileRow * tileSize.h,
  1225. xmin + (tileCol + 1) * tileSize.w,
  1226. zmin + (tileRow + 1) * tileSize.h
  1227. );
  1228. }
  1229. }
  1230. // Result
  1231. var vertexData = new VertexData();
  1232. vertexData.indices = indices;
  1233. vertexData.positions = positions;
  1234. vertexData.normals = normals;
  1235. vertexData.uvs = uvs;
  1236. return vertexData;
  1237. }
  1238. /**
  1239. * Creates the VertexData of the Ground designed from a heightmap.
  1240. */
  1241. public static CreateGroundFromHeightMap(options: { width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, colorFilter: Color3, buffer: Uint8Array, bufferWidth: number, bufferHeight: number }): VertexData {
  1242. var indices = [];
  1243. var positions = [];
  1244. var normals = [];
  1245. var uvs = [];
  1246. var row, col;
  1247. var filter = options.colorFilter || new Color3(0.3, 0.59, 0.11);
  1248. // Vertices
  1249. for (row = 0; row <= options.subdivisions; row++) {
  1250. for (col = 0; col <= options.subdivisions; col++) {
  1251. 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));
  1252. // Compute height
  1253. var heightMapX = (((position.x + options.width / 2) / options.width) * (options.bufferWidth - 1)) | 0;
  1254. var heightMapY = ((1.0 - (position.z + options.height / 2) / options.height) * (options.bufferHeight - 1)) | 0;
  1255. var pos = (heightMapX + heightMapY * options.bufferWidth) * 4;
  1256. var r = options.buffer[pos] / 255.0;
  1257. var g = options.buffer[pos + 1] / 255.0;
  1258. var b = options.buffer[pos + 2] / 255.0;
  1259. var gradient = r * filter.r + g * filter.g + b * filter.b;
  1260. position.y = options.minHeight + (options.maxHeight - options.minHeight) * gradient;
  1261. // Add vertex
  1262. positions.push(position.x, position.y, position.z);
  1263. normals.push(0, 0, 0);
  1264. uvs.push(col / options.subdivisions, 1.0 - row / options.subdivisions);
  1265. }
  1266. }
  1267. // Indices
  1268. for (row = 0; row < options.subdivisions; row++) {
  1269. for (col = 0; col < options.subdivisions; col++) {
  1270. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1271. indices.push(col + 1 + row * (options.subdivisions + 1));
  1272. indices.push(col + row * (options.subdivisions + 1));
  1273. indices.push(col + (row + 1) * (options.subdivisions + 1));
  1274. indices.push(col + 1 + (row + 1) * (options.subdivisions + 1));
  1275. indices.push(col + row * (options.subdivisions + 1));
  1276. }
  1277. }
  1278. // Normals
  1279. VertexData.ComputeNormals(positions, indices, normals);
  1280. // Result
  1281. var vertexData = new VertexData();
  1282. vertexData.indices = indices;
  1283. vertexData.positions = positions;
  1284. vertexData.normals = normals;
  1285. vertexData.uvs = uvs;
  1286. return vertexData;
  1287. }
  1288. /**
  1289. * Creates the VertexData of the Plane.
  1290. */
  1291. public static CreatePlane(options: { size?: number, width?: number, height?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  1292. var indices = [];
  1293. var positions = [];
  1294. var normals = [];
  1295. var uvs = [];
  1296. var width: number = options.width || options.size || 1;
  1297. var height: number = options.height || options.size || 1;
  1298. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1299. // Vertices
  1300. var halfWidth = width / 2.0;
  1301. var halfHeight = height / 2.0;
  1302. positions.push(-halfWidth, -halfHeight, 0);
  1303. normals.push(0, 0, -1.0);
  1304. uvs.push(0.0, 0.0);
  1305. positions.push(halfWidth, -halfHeight, 0);
  1306. normals.push(0, 0, -1.0);
  1307. uvs.push(1.0, 0.0);
  1308. positions.push(halfWidth, halfHeight, 0);
  1309. normals.push(0, 0, -1.0);
  1310. uvs.push(1.0, 1.0);
  1311. positions.push(-halfWidth, halfHeight, 0);
  1312. normals.push(0, 0, -1.0);
  1313. uvs.push(0.0, 1.0);
  1314. // Indices
  1315. indices.push(0);
  1316. indices.push(1);
  1317. indices.push(2);
  1318. indices.push(0);
  1319. indices.push(2);
  1320. indices.push(3);
  1321. // Sides
  1322. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1323. // Result
  1324. var vertexData = new VertexData();
  1325. vertexData.indices = indices;
  1326. vertexData.positions = positions;
  1327. vertexData.normals = normals;
  1328. vertexData.uvs = uvs;
  1329. return vertexData;
  1330. }
  1331. /**
  1332. * Creates the VertexData of the Disc or regular Polygon.
  1333. */
  1334. public static CreateDisc(options: { radius?: number, tessellation?: number, arc?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  1335. var positions = [];
  1336. var indices = [];
  1337. var normals = [];
  1338. var uvs = [];
  1339. var radius = options.radius || 0.5;
  1340. var tessellation = options.tessellation || 64;
  1341. var arc: number = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  1342. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1343. // positions and uvs
  1344. positions.push(0, 0, 0); // disc center first
  1345. uvs.push(0.5, 0.5);
  1346. var theta = Math.PI * 2 * arc;
  1347. var step = theta / tessellation;
  1348. for (var a = 0; a < theta; a += step) {
  1349. var x = Math.cos(a);
  1350. var y = Math.sin(a);
  1351. var u = (x + 1) / 2;
  1352. var v = (1 - y) / 2;
  1353. positions.push(radius * x, radius * y, 0);
  1354. uvs.push(u, v);
  1355. }
  1356. if (arc === 1) {
  1357. positions.push(positions[3], positions[4], positions[5]); // close the circle
  1358. uvs.push(uvs[2], uvs[3]);
  1359. }
  1360. //indices
  1361. var vertexNb = positions.length / 3;
  1362. for (var i = 1; i < vertexNb - 1; i++) {
  1363. indices.push(i + 1, 0, i);
  1364. }
  1365. // result
  1366. VertexData.ComputeNormals(positions, indices, normals);
  1367. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1368. var vertexData = new VertexData();
  1369. vertexData.indices = indices;
  1370. vertexData.positions = positions;
  1371. vertexData.normals = normals;
  1372. vertexData.uvs = uvs;
  1373. return vertexData;
  1374. }
  1375. /**
  1376. * Re-creates the VertexData of the Polygon for sideOrientation.
  1377. */
  1378. public static CreatePolygon(polygon: Mesh, sideOrientation: number, fUV?, fColors?, frontUVs?: Vector4, backUVs?: Vector4) {
  1379. var faceUV: Vector4[] = fUV || new Array<Vector4>(3);
  1380. var faceColors: Color4[] = fColors;
  1381. var colors = [];
  1382. // default face colors and UV if undefined
  1383. for (var f = 0; f < 3; f++) {
  1384. if (faceUV[f] === undefined) {
  1385. faceUV[f] = new Vector4(0, 0, 1, 1);
  1386. }
  1387. if (faceColors && faceColors[f] === undefined) {
  1388. faceColors[f] = new Color4(1, 1, 1, 1);
  1389. }
  1390. }
  1391. var positions = polygon.getVerticesData(VertexBuffer.PositionKind);
  1392. var normals = polygon.getVerticesData(VertexBuffer.NormalKind);
  1393. var uvs = polygon.getVerticesData(VertexBuffer.UVKind);
  1394. var indices = polygon.getIndices();
  1395. // set face colours and textures
  1396. var idx: number = 0;
  1397. var face: number = 0;
  1398. for (var index = 0; index < normals.length; index += 3) {
  1399. //Edge Face no. 1
  1400. if(Math.abs(normals[index + 1]) < 0.001) {
  1401. face = 1;
  1402. }
  1403. //Top Face no. 0
  1404. if(Math.abs(normals[index + 1] - 1) < 0.001 ) {
  1405. face = 0;
  1406. }
  1407. //Bottom Face no. 2
  1408. if(Math.abs(normals[index + 1] + 1) < 0.001 ) {
  1409. face = 2;
  1410. }
  1411. idx = index / 3;
  1412. uvs[2*idx] = (1 - uvs[2*idx])*faceUV[face].x + uvs[2*idx]*faceUV[face].z;
  1413. uvs[2*idx + 1] = (1 - uvs[2*idx + 1])*faceUV[face].y + uvs[2*idx + 1]*faceUV[face].w;
  1414. if (faceColors) {
  1415. colors.push(faceColors[face].r, faceColors[face].g, faceColors[face].b, faceColors[face].a);
  1416. }
  1417. }
  1418. // sides
  1419. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, frontUVs, backUVs);
  1420. // Result
  1421. var vertexData = new VertexData();
  1422. vertexData.indices = indices;
  1423. vertexData.positions = positions;
  1424. vertexData.normals = normals;
  1425. vertexData.uvs = uvs;
  1426. if (faceColors) {
  1427. var totalColors = (sideOrientation === Mesh.DOUBLESIDE) ? colors.concat(colors) : colors;
  1428. vertexData.colors = totalColors;
  1429. }
  1430. return vertexData;
  1431. }
  1432. /**
  1433. * Creates the VertexData of the IcoSphere.
  1434. */
  1435. public static CreateIcoSphere(options: { radius?: number, radiusX?: number, radiusY?: number, radiusZ?: number, flat?: boolean, subdivisions?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  1436. var sideOrientation = options.sideOrientation || Mesh.DEFAULTSIDE;
  1437. var radius = options.radius || 1;
  1438. var flat = (options.flat === undefined) ? true : options.flat;
  1439. var subdivisions = options.subdivisions || 4;
  1440. var radiusX = options.radiusX || radius;
  1441. var radiusY = options.radiusY || radius;
  1442. var radiusZ = options.radiusZ || radius;
  1443. var t = (1 + Math.sqrt(5)) / 2;
  1444. // 12 vertex x,y,z
  1445. var ico_vertices = [
  1446. -1, t, -0, 1, t, 0, -1, -t, 0, 1, -t, 0, // v0-3
  1447. 0, -1, -t, 0, 1, -t, 0, -1, t, 0, 1, t, // v4-7
  1448. t, 0, 1, t, 0, -1, -t, 0, 1, -t, 0, -1 // v8-11
  1449. ];
  1450. // index of 3 vertex makes a face of icopshere
  1451. var ico_indices = [
  1452. 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 12, 22, 23,
  1453. 1, 5, 20, 5, 11, 4, 23, 22, 13, 22, 18, 6, 7, 1, 8,
  1454. 14, 21, 4, 14, 4, 2, 16, 13, 6, 15, 6, 19, 3, 8, 9,
  1455. 4, 21, 5, 13, 17, 23, 6, 13, 22, 19, 6, 18, 9, 8, 1
  1456. ];
  1457. // vertex for uv have aliased position, not for UV
  1458. var vertices_unalias_id = [
  1459. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  1460. // vertex alias
  1461. 0, // 12: 0 + 12
  1462. 2, // 13: 2 + 11
  1463. 3, // 14: 3 + 11
  1464. 3, // 15: 3 + 12
  1465. 3, // 16: 3 + 13
  1466. 4, // 17: 4 + 13
  1467. 7, // 18: 7 + 11
  1468. 8, // 19: 8 + 11
  1469. 9, // 20: 9 + 11
  1470. 9, // 21: 9 + 12
  1471. 10, // 22: A + 12
  1472. 11 // 23: B + 12
  1473. ];
  1474. // uv as integer step (not pixels !)
  1475. var ico_vertexuv = [
  1476. 5, 1, 3, 1, 6, 4, 0, 0, // v0-3
  1477. 5, 3, 4, 2, 2, 2, 4, 0, // v4-7
  1478. 2, 0, 1, 1, 6, 0, 6, 2, // v8-11
  1479. // vertex alias (for same vertex on different faces)
  1480. 0, 4, // 12: 0 + 12
  1481. 3, 3, // 13: 2 + 11
  1482. 4, 4, // 14: 3 + 11
  1483. 3, 1, // 15: 3 + 12
  1484. 4, 2, // 16: 3 + 13
  1485. 4, 4, // 17: 4 + 13
  1486. 0, 2, // 18: 7 + 11
  1487. 1, 1, // 19: 8 + 11
  1488. 2, 2, // 20: 9 + 11
  1489. 3, 3, // 21: 9 + 12
  1490. 1, 3, // 22: A + 12
  1491. 2, 4 // 23: B + 12
  1492. ];
  1493. // Vertices[0, 1, ...9, A, B] : position on UV plane
  1494. // '+' indicate duplicate position to be fixed (3,9:0,2,3,4,7,8,A,B)
  1495. // First island of uv mapping
  1496. // v = 4h 3+ 2
  1497. // v = 3h 9+ 4
  1498. // v = 2h 9+ 5 B
  1499. // v = 1h 9 1 0
  1500. // v = 0h 3 8 7 A
  1501. // u = 0 1 2 3 4 5 6 *a
  1502. // Second island of uv mapping
  1503. // v = 4h 0+ B+ 4+
  1504. // v = 3h A+ 2+
  1505. // v = 2h 7+ 6 3+
  1506. // v = 1h 8+ 3+
  1507. // v = 0h
  1508. // u = 0 1 2 3 4 5 6 *a
  1509. // Face layout on texture UV mapping
  1510. // ============
  1511. // \ 4 /\ 16 / ======
  1512. // \ / \ / /\ 11 /
  1513. // \/ 7 \/ / \ /
  1514. // ======= / 10 \/
  1515. // /\ 17 /\ =======
  1516. // / \ / \ \ 15 /\
  1517. // / 8 \/ 12 \ \ / \
  1518. // ============ \/ 6 \
  1519. // \ 18 /\ ============
  1520. // \ / \ \ 5 /\ 0 /
  1521. // \/ 13 \ \ / \ /
  1522. // ======= \/ 1 \/
  1523. // =============
  1524. // /\ 19 /\ 2 /\
  1525. // / \ / \ / \
  1526. // / 14 \/ 9 \/ 3 \
  1527. // ===================
  1528. // uv step is u:1 or 0.5, v:cos(30)=sqrt(3)/2, ratio approx is 84/97
  1529. var ustep = 138 / 1024;
  1530. var vstep = 239 / 1024;
  1531. var uoffset = 60 / 1024;
  1532. var voffset = 26 / 1024;
  1533. // Second island should have margin, not to touch the first island
  1534. // avoid any borderline artefact in pixel rounding
  1535. var island_u_offset = -40 / 1024;
  1536. var island_v_offset = +20 / 1024;
  1537. // face is either island 0 or 1 :
  1538. // second island is for faces : [4, 7, 8, 12, 13, 16, 17, 18]
  1539. var island = [
  1540. 0, 0, 0, 0, 1, // 0 - 4
  1541. 0, 0, 1, 1, 0, // 5 - 9
  1542. 0, 0, 1, 1, 0, // 10 - 14
  1543. 0, 1, 1, 1, 0 // 15 - 19
  1544. ];
  1545. var indices = [];
  1546. var positions = [];
  1547. var normals = [];
  1548. var uvs = [];
  1549. var current_indice = 0;
  1550. // prepare array of 3 vector (empty) (to be worked in place, shared for each face)
  1551. var face_vertex_pos = new Array(3);
  1552. var face_vertex_uv = new Array(3);
  1553. var v012;
  1554. for (v012 = 0; v012 < 3; v012++) {
  1555. face_vertex_pos[v012] = Vector3.Zero();
  1556. face_vertex_uv[v012] = Vector2.Zero();
  1557. }
  1558. // create all with normals
  1559. for (var face = 0; face < 20; face++) {
  1560. // 3 vertex per face
  1561. for (v012 = 0; v012 < 3; v012++) {
  1562. // look up vertex 0,1,2 to its index in 0 to 11 (or 23 including alias)
  1563. var v_id = ico_indices[3 * face + v012];
  1564. // vertex have 3D position (x,y,z)
  1565. face_vertex_pos[v012].copyFromFloats(
  1566. ico_vertices[3 * vertices_unalias_id[v_id]],
  1567. ico_vertices[3 * vertices_unalias_id[v_id] + 1],
  1568. ico_vertices[3 * vertices_unalias_id[v_id] + 2]);
  1569. // Normalize to get normal, then scale to radius
  1570. face_vertex_pos[v012].normalize().scaleInPlace(radius);
  1571. // uv Coordinates from vertex ID
  1572. face_vertex_uv[v012].copyFromFloats(
  1573. ico_vertexuv[2 * v_id] * ustep + uoffset + island[face] * island_u_offset,
  1574. ico_vertexuv[2 * v_id + 1] * vstep + voffset + island[face] * island_v_offset);
  1575. }
  1576. // Subdivide the face (interpolate pos, norm, uv)
  1577. // - pos is linear interpolation, then projected to sphere (converge polyhedron to sphere)
  1578. // - norm is linear interpolation of vertex corner normal
  1579. // (to be checked if better to re-calc from face vertex, or if approximation is OK ??? )
  1580. // - uv is linear interpolation
  1581. //
  1582. // Topology is as below for sub-divide by 2
  1583. // vertex shown as v0,v1,v2
  1584. // interp index is i1 to progress in range [v0,v1[
  1585. // interp index is i2 to progress in range [v0,v2[
  1586. // face index as (i1,i2) for /\ : (i1,i2),(i1+1,i2),(i1,i2+1)
  1587. // and (i1,i2)' for \/ : (i1+1,i2),(i1+1,i2+1),(i1,i2+1)
  1588. //
  1589. //
  1590. // i2 v2
  1591. // ^ ^
  1592. // / / \
  1593. // / / \
  1594. // / / \
  1595. // / / (0,1) \
  1596. // / #---------\
  1597. // / / \ (0,0)'/ \
  1598. // / / \ / \
  1599. // / / \ / \
  1600. // / / (0,0) \ / (1,0) \
  1601. // / #---------#---------\
  1602. // v0 v1
  1603. //
  1604. // --------------------> i1
  1605. //
  1606. // interp of (i1,i2):
  1607. // along i2 : x0=lerp(v0,v2, i2/S) <---> x1=lerp(v1,v2, i2/S)
  1608. // along i1 : lerp(x0,x1, i1/(S-i2))
  1609. //
  1610. // centroid of triangle is needed to get help normal computation
  1611. // (c1,c2) are used for centroid location
  1612. var interp_vertex = (i1: number, i2: number, c1: number, c2: number) => {
  1613. // vertex is interpolated from
  1614. // - face_vertex_pos[0..2]
  1615. // - face_vertex_uv[0..2]
  1616. var pos_x0 = Vector3.Lerp(face_vertex_pos[0], face_vertex_pos[2], i2 / subdivisions);
  1617. var pos_x1 = Vector3.Lerp(face_vertex_pos[1], face_vertex_pos[2], i2 / subdivisions);
  1618. var pos_interp = (subdivisions === i2) ? face_vertex_pos[2] : Vector3.Lerp(pos_x0, pos_x1, i1 / (subdivisions - i2));
  1619. pos_interp.normalize();
  1620. var vertex_normal;
  1621. if (flat) {
  1622. // in flat mode, recalculate normal as face centroid normal
  1623. var centroid_x0 = Vector3.Lerp(face_vertex_pos[0], face_vertex_pos[2], c2 / subdivisions);
  1624. var centroid_x1 = Vector3.Lerp(face_vertex_pos[1], face_vertex_pos[2], c2 / subdivisions);
  1625. vertex_normal = Vector3.Lerp(centroid_x0, centroid_x1, c1 / (subdivisions - c2));
  1626. } else {
  1627. // in smooth mode, recalculate normal from each single vertex position
  1628. vertex_normal = new Vector3(pos_interp.x, pos_interp.y, pos_interp.z);
  1629. }
  1630. // Vertex normal need correction due to X,Y,Z radius scaling
  1631. vertex_normal.x /= radiusX;
  1632. vertex_normal.y /= radiusY;
  1633. vertex_normal.z /= radiusZ;
  1634. vertex_normal.normalize();
  1635. var uv_x0 = Vector2.Lerp(face_vertex_uv[0], face_vertex_uv[2], i2 / subdivisions);
  1636. var uv_x1 = Vector2.Lerp(face_vertex_uv[1], face_vertex_uv[2], i2 / subdivisions);
  1637. var uv_interp = (subdivisions === i2) ? face_vertex_uv[2] : Vector2.Lerp(uv_x0, uv_x1, i1 / (subdivisions - i2));
  1638. positions.push(pos_interp.x * radiusX, pos_interp.y * radiusY, pos_interp.z * radiusZ);
  1639. normals.push(vertex_normal.x, vertex_normal.y, vertex_normal.z);
  1640. uvs.push(uv_interp.x, uv_interp.y);
  1641. // push each vertex has member of a face
  1642. // Same vertex can bleong to multiple face, it is pushed multiple time (duplicate vertex are present)
  1643. indices.push(current_indice);
  1644. current_indice++;
  1645. }
  1646. for (var i2 = 0; i2 < subdivisions; i2++) {
  1647. for (var i1 = 0; i1 + i2 < subdivisions; i1++) {
  1648. // face : (i1,i2) for /\ :
  1649. // interp for : (i1,i2),(i1+1,i2),(i1,i2+1)
  1650. interp_vertex(i1, i2, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1651. interp_vertex(i1 + 1, i2, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1652. interp_vertex(i1, i2 + 1, i1 + 1.0 / 3, i2 + 1.0 / 3);
  1653. if (i1 + i2 + 1 < subdivisions) {
  1654. // face : (i1,i2)' for \/ :
  1655. // interp for (i1+1,i2),(i1+1,i2+1),(i1,i2+1)
  1656. interp_vertex(i1 + 1, i2, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1657. interp_vertex(i1 + 1, i2 + 1, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1658. interp_vertex(i1, i2 + 1, i1 + 2.0 / 3, i2 + 2.0 / 3);
  1659. }
  1660. }
  1661. }
  1662. }
  1663. // Sides
  1664. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1665. // Result
  1666. var vertexData = new VertexData();
  1667. vertexData.indices = indices;
  1668. vertexData.positions = positions;
  1669. vertexData.normals = normals;
  1670. vertexData.uvs = uvs;
  1671. return vertexData;
  1672. }
  1673. // inspired from // http://stemkoski.github.io/Three.js/Polyhedra.html
  1674. /**
  1675. * Creates the VertexData of the Polyhedron.
  1676. */
  1677. public static CreatePolyhedron(options: { type?: number, size?: number, sizeX?: number, sizeY?: number, sizeZ?: number, custom?: any, faceUV?: Vector4[], faceColors?: Color4[], flat?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  1678. // provided polyhedron types :
  1679. // 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  1680. // 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)
  1681. var polyhedra: { vertex: number[][], face: number[][] }[] = [];
  1682. 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]] };
  1683. 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]] };
  1684. polyhedra[2] = {
  1685. 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]],
  1686. 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]]
  1687. };
  1688. polyhedra[3] = {
  1689. 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]],
  1690. 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]]
  1691. };
  1692. polyhedra[4] = {
  1693. 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]],
  1694. 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]]
  1695. };
  1696. 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]] };
  1697. 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]] };
  1698. 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]] };
  1699. 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]] };
  1700. 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]] };
  1701. 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]] };
  1702. 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]] };
  1703. 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]] };
  1704. 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]] };
  1705. polyhedra[14] = {
  1706. 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]],
  1707. 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]]
  1708. };
  1709. var type: number = (options.type < 0 || options.type >= polyhedra.length) ? 0 : options.type || 0;
  1710. var size: number = options.size;
  1711. var sizeX: number = options.sizeX || size || 1;
  1712. var sizeY: number = options.sizeY || size || 1;
  1713. var sizeZ: number = options.sizeZ || size || 1;
  1714. var data: { vertex: number[][], face: number[][], name?: string, category?: string } = options.custom || polyhedra[type];
  1715. var nbfaces = data.face.length;
  1716. var faceUV = options.faceUV || new Array(nbfaces);
  1717. var faceColors = options.faceColors;
  1718. var flat = (options.flat === undefined) ? true : options.flat;
  1719. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1720. var positions = [];
  1721. var indices = [];
  1722. var normals = [];
  1723. var uvs = [];
  1724. var colors = [];
  1725. var index = 0;
  1726. var faceIdx = 0; // face cursor in the array "indexes"
  1727. var indexes = [];
  1728. var i = 0;
  1729. var f = 0;
  1730. var u: number, v: number, ang: number, x: number, y: number, tmp: number;
  1731. // default face colors and UV if undefined
  1732. if (flat) {
  1733. for (f = 0; f < nbfaces; f++) {
  1734. if (faceColors && faceColors[f] === undefined) {
  1735. faceColors[f] = new Color4(1, 1, 1, 1);
  1736. }
  1737. if (faceUV && faceUV[f] === undefined) {
  1738. faceUV[f] = new Vector4(0, 0, 1, 1);
  1739. }
  1740. }
  1741. }
  1742. if (!flat) {
  1743. for (i = 0; i < data.vertex.length; i++) {
  1744. positions.push(data.vertex[i][0] * sizeX, data.vertex[i][1] * sizeY, data.vertex[i][2] * sizeZ);
  1745. uvs.push(0, 0);
  1746. }
  1747. for (f = 0; f < nbfaces; f++) {
  1748. for (i = 0; i < data.face[f].length - 2; i++) {
  1749. indices.push(data.face[f][0], data.face[f][i + 2], data.face[f][i + 1]);
  1750. }
  1751. }
  1752. } else {
  1753. for (f = 0; f < nbfaces; f++) {
  1754. var fl = data.face[f].length; // number of vertices of the current face
  1755. ang = 2 * Math.PI / fl;
  1756. x = 0.5 * Math.tan(ang / 2);
  1757. y = 0.5;
  1758. // positions, uvs, colors
  1759. for (i = 0; i < fl; i++) {
  1760. // positions
  1761. 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);
  1762. indexes.push(index);
  1763. index++;
  1764. // uvs
  1765. u = faceUV[f].x + (faceUV[f].z - faceUV[f].x) * (0.5 + x);
  1766. v = faceUV[f].y + (faceUV[f].w - faceUV[f].y) * (y - 0.5);
  1767. uvs.push(u, v);
  1768. tmp = x * Math.cos(ang) - y * Math.sin(ang);
  1769. y = x * Math.sin(ang) + y * Math.cos(ang);
  1770. x = tmp;
  1771. // colors
  1772. if (faceColors) {
  1773. colors.push(faceColors[f].r, faceColors[f].g, faceColors[f].b, faceColors[f].a);
  1774. }
  1775. }
  1776. // indices from indexes
  1777. for (i = 0; i < fl - 2; i++) {
  1778. indices.push(indexes[0 + faceIdx], indexes[i + 2 + faceIdx], indexes[i + 1 + faceIdx]);
  1779. }
  1780. faceIdx += fl;
  1781. }
  1782. }
  1783. VertexData.ComputeNormals(positions, indices, normals);
  1784. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1785. var vertexData = new VertexData();
  1786. vertexData.positions = positions;
  1787. vertexData.indices = indices;
  1788. vertexData.normals = normals;
  1789. vertexData.uvs = uvs;
  1790. if (faceColors && flat) {
  1791. vertexData.colors = colors;
  1792. }
  1793. return vertexData;
  1794. }
  1795. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1796. /**
  1797. * Creates the VertexData of the Torus Knot.
  1798. */
  1799. public static CreateTorusKnot(options: { radius?: number, tube?: number, radialSegments?: number, tubularSegments?: number, p?: number, q?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
  1800. var indices = [];
  1801. var positions = [];
  1802. var normals = [];
  1803. var uvs = [];
  1804. var radius = options.radius || 2;
  1805. var tube = options.tube || 0.5;
  1806. var radialSegments = options.radialSegments || 32;
  1807. var tubularSegments = options.tubularSegments || 32;
  1808. var p = options.p || 2;
  1809. var q = options.q || 3;
  1810. var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || Mesh.DEFAULTSIDE;
  1811. // Helper
  1812. var getPos = (angle) => {
  1813. var cu = Math.cos(angle);
  1814. var su = Math.sin(angle);
  1815. var quOverP = q / p * angle;
  1816. var cs = Math.cos(quOverP);
  1817. var tx = radius * (2 + cs) * 0.5 * cu;
  1818. var ty = radius * (2 + cs) * su * 0.5;
  1819. var tz = radius * Math.sin(quOverP) * 0.5;
  1820. return new Vector3(tx, ty, tz);
  1821. };
  1822. // Vertices
  1823. var i: number;
  1824. var j: number;
  1825. for (i = 0; i <= radialSegments; i++) {
  1826. var modI = i % radialSegments;
  1827. var u = modI / radialSegments * 2 * p * Math.PI;
  1828. var p1 = getPos(u);
  1829. var p2 = getPos(u + 0.01);
  1830. var tang = p2.subtract(p1);
  1831. var n = p2.add(p1);
  1832. var bitan = Vector3.Cross(tang, n);
  1833. n = Vector3.Cross(bitan, tang);
  1834. bitan.normalize();
  1835. n.normalize();
  1836. for (j = 0; j < tubularSegments; j++) {
  1837. var modJ = j % tubularSegments;
  1838. var v = modJ / tubularSegments * 2 * Math.PI;
  1839. var cx = -tube * Math.cos(v);
  1840. var cy = tube * Math.sin(v);
  1841. positions.push(p1.x + cx * n.x + cy * bitan.x);
  1842. positions.push(p1.y + cx * n.y + cy * bitan.y);
  1843. positions.push(p1.z + cx * n.z + cy * bitan.z);
  1844. uvs.push(i / radialSegments);
  1845. uvs.push(j / tubularSegments);
  1846. }
  1847. }
  1848. for (i = 0; i < radialSegments; i++) {
  1849. for (j = 0; j < tubularSegments; j++) {
  1850. var jNext = (j + 1) % tubularSegments;
  1851. var a = i * tubularSegments + j;
  1852. var b = (i + 1) * tubularSegments + j;
  1853. var c = (i + 1) * tubularSegments + jNext;
  1854. var d = i * tubularSegments + jNext;
  1855. indices.push(d); indices.push(b); indices.push(a);
  1856. indices.push(d); indices.push(c); indices.push(b);
  1857. }
  1858. }
  1859. // Normals
  1860. VertexData.ComputeNormals(positions, indices, normals);
  1861. // Sides
  1862. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  1863. // Result
  1864. var vertexData = new VertexData();
  1865. vertexData.indices = indices;
  1866. vertexData.positions = positions;
  1867. vertexData.normals = normals;
  1868. vertexData.uvs = uvs;
  1869. return vertexData;
  1870. }
  1871. // Tools
  1872. /**
  1873. * @param {any} - positions (number[] or Float32Array)
  1874. * @param {any} - indices (number[] or Uint16Array)
  1875. * @param {any} - normals (number[] or Float32Array)
  1876. * options (optional) :
  1877. * facetPositions : optional array of facet positions (vector3)
  1878. * facetNormals : optional array of facet normals (vector3)
  1879. * facetPartitioning : optional partitioning array. facetPositions is required for facetPartitioning computation
  1880. * subDiv : optional partitioning data about subdivsions on each axis (int), required for facetPartitioning computation
  1881. * ratio : optional partitioning ratio / bounding box, required for facetPartitioning computation
  1882. * bbSize : optional bounding box size data, required for facetPartitioning computation
  1883. * bInfo : optional bounding info, required for facetPartitioning computation
  1884. */
  1885. public static ComputeNormals(positions: any, indices: any, normals: any,
  1886. options?: { facetNormals?: any, facetPositions?: any, facetPartitioning?: any, ratio?: number, bInfo?: any, bbSize?: Vector3, subDiv?: any, useRightHandedSystem?: boolean }): void {
  1887. // temporary scalar variables
  1888. var index = 0; // facet index
  1889. var p1p2x = 0.0; // p1p2 vector x coordinate
  1890. var p1p2y = 0.0; // p1p2 vector y coordinate
  1891. var p1p2z = 0.0; // p1p2 vector z coordinate
  1892. var p3p2x = 0.0; // p3p2 vector x coordinate
  1893. var p3p2y = 0.0; // p3p2 vector y coordinate
  1894. var p3p2z = 0.0; // p3p2 vector z coordinate
  1895. var faceNormalx = 0.0; // facet normal x coordinate
  1896. var faceNormaly = 0.0; // facet normal y coordinate
  1897. var faceNormalz = 0.0; // facet normal z coordinate
  1898. var length = 0.0; // facet normal length before normalization
  1899. var v1x = 0; // vector1 x index in the positions array
  1900. var v1y = 0; // vector1 y index in the positions array
  1901. var v1z = 0; // vector1 z index in the positions array
  1902. var v2x = 0; // vector2 x index in the positions array
  1903. var v2y = 0; // vector2 y index in the positions array
  1904. var v2z = 0; // vector2 z index in the positions array
  1905. var v3x = 0; // vector3 x index in the positions array
  1906. var v3y = 0; // vector3 y index in the positions array
  1907. var v3z = 0; // vector3 z index in the positions array
  1908. var computeFacetNormals = false;
  1909. var computeFacetPositions = false;
  1910. var computeFacetPartitioning = false;
  1911. var faceNormalSign = 1;
  1912. if (options) {
  1913. computeFacetNormals = (options.facetNormals) ? true : false;
  1914. computeFacetPositions = (options.facetPositions) ? true : false;
  1915. computeFacetPartitioning = (options.facetPartitioning) ? true : false;
  1916. faceNormalSign = (options.useRightHandedSystem === true) ? -1 : 1;
  1917. }
  1918. // facetPartitioning reinit if needed
  1919. if (computeFacetPartitioning) {
  1920. var ox = 0; // X partitioning index for facet position
  1921. var oy = 0; // Y partinioning index for facet position
  1922. var oz = 0; // Z partinioning index for facet position
  1923. var b1x = 0; // X partitioning index for facet v1 vertex
  1924. var b1y = 0; // Y partitioning index for facet v1 vertex
  1925. var b1z = 0; // z partitioning index for facet v1 vertex
  1926. var b2x = 0; // X partitioning index for facet v2 vertex
  1927. var b2y = 0; // Y partitioning index for facet v2 vertex
  1928. var b2z = 0; // Z partitioning index for facet v2 vertex
  1929. var b3x = 0; // X partitioning index for facet v3 vertex
  1930. var b3y = 0; // Y partitioning index for facet v3 vertex
  1931. var b3z = 0; // Z partitioning index for facet v3 vertex
  1932. var block_idx_o = 0; // facet barycenter block index
  1933. var block_idx_v1 = 0; // v1 vertex block index
  1934. var block_idx_v2 = 0; // v2 vertex block index
  1935. var block_idx_v3 = 0; // v3 vertex block index
  1936. var bbSizeMax = (options.bbSize.x > options.bbSize.y) ? options.bbSize.x : options.bbSize.y;
  1937. bbSizeMax = (bbSizeMax > options.bbSize.z) ? bbSizeMax : options.bbSize.z;
  1938. var xSubRatio = options.subDiv.X * options.ratio / options.bbSize.x;
  1939. var ySubRatio = options.subDiv.Y * options.ratio / options.bbSize.y;
  1940. var zSubRatio = options.subDiv.Z * options.ratio / options.bbSize.z;
  1941. var subSq = options.subDiv.max * options.subDiv.max;
  1942. options.facetPartitioning.length = 0;
  1943. }
  1944. // reset the normals
  1945. for (index = 0; index < positions.length; index++) {
  1946. normals[index] = 0.0;
  1947. }
  1948. // Loop : 1 indice triplet = 1 facet
  1949. var nbFaces = indices.length / 3;
  1950. for (index = 0; index < nbFaces; index++) {
  1951. // get the indexes of the coordinates of each vertex of the facet
  1952. v1x = indices[index * 3] * 3;
  1953. v1y = v1x + 1;
  1954. v1z = v1x + 2;
  1955. v2x = indices[index * 3 + 1] * 3;
  1956. v2y = v2x + 1;
  1957. v2z = v2x + 2;
  1958. v3x = indices[index * 3 + 2] * 3;
  1959. v3y = v3x + 1;
  1960. v3z = v3x + 2;
  1961. p1p2x = positions[v1x] - positions[v2x]; // compute two vectors per facet : p1p2 and p3p2
  1962. p1p2y = positions[v1y] - positions[v2y];
  1963. p1p2z = positions[v1z] - positions[v2z];
  1964. p3p2x = positions[v3x] - positions[v2x];
  1965. p3p2y = positions[v3y] - positions[v2y];
  1966. p3p2z = positions[v3z] - positions[v2z];
  1967. // compute the face normal with the cross product
  1968. faceNormalx = faceNormalSign * (p1p2y * p3p2z - p1p2z * p3p2y);
  1969. faceNormaly = faceNormalSign * (p1p2z * p3p2x - p1p2x * p3p2z);
  1970. faceNormalz = faceNormalSign * (p1p2x * p3p2y - p1p2y * p3p2x);
  1971. // normalize this normal and store it in the array facetData
  1972. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  1973. length = (length === 0) ? 1.0 : length;
  1974. faceNormalx /= length;
  1975. faceNormaly /= length;
  1976. faceNormalz /= length;
  1977. if (computeFacetNormals) {
  1978. options.facetNormals[index].x = faceNormalx;
  1979. options.facetNormals[index].y = faceNormaly;
  1980. options.facetNormals[index].z = faceNormalz;
  1981. }
  1982. if (computeFacetPositions) {
  1983. // compute and the facet barycenter coordinates in the array facetPositions
  1984. options.facetPositions[index].x = (positions[v1x] + positions[v2x] + positions[v3x]) / 3.0;
  1985. options.facetPositions[index].y = (positions[v1y] + positions[v2y] + positions[v3y]) / 3.0;
  1986. options.facetPositions[index].z = (positions[v1z] + positions[v2z] + positions[v3z]) / 3.0;
  1987. }
  1988. if (computeFacetPartitioning) {
  1989. // store the facet indexes in arrays in the main facetPartitioning array :
  1990. // compute each facet vertex (+ facet barycenter) index in the partiniong array
  1991. ox = Math.floor((options.facetPositions[index].x - options.bInfo.minimum.x * options.ratio) * xSubRatio);
  1992. oy = Math.floor((options.facetPositions[index].y - options.bInfo.minimum.y * options.ratio) * ySubRatio);
  1993. oz = Math.floor((options.facetPositions[index].z - options.bInfo.minimum.z * options.ratio) * zSubRatio);
  1994. b1x = Math.floor((positions[v1x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
  1995. b1y = Math.floor((positions[v1y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
  1996. b1z = Math.floor((positions[v1z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
  1997. b2x = Math.floor((positions[v2x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
  1998. b2y = Math.floor((positions[v2y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
  1999. b2z = Math.floor((positions[v2z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
  2000. b3x = Math.floor((positions[v3x] - options.bInfo.minimum.x * options.ratio) * xSubRatio);
  2001. b3y = Math.floor((positions[v3y] - options.bInfo.minimum.y * options.ratio) * ySubRatio);
  2002. b3z = Math.floor((positions[v3z] - options.bInfo.minimum.z * options.ratio) * zSubRatio);
  2003. block_idx_v1 = b1x + options.subDiv.max * b1y + subSq * b1z;
  2004. block_idx_v2 = b2x + options.subDiv.max * b2y + subSq * b2z;
  2005. block_idx_v3 = b3x + options.subDiv.max * b3y + subSq * b3z;
  2006. block_idx_o = ox + options.subDiv.max * oy + subSq * oz;
  2007. options.facetPartitioning[block_idx_o] = options.facetPartitioning[block_idx_o] ? options.facetPartitioning[block_idx_o] :new Array();
  2008. options.facetPartitioning[block_idx_v1] = options.facetPartitioning[block_idx_v1] ? options.facetPartitioning[block_idx_v1] :new Array();
  2009. options.facetPartitioning[block_idx_v2] = options.facetPartitioning[block_idx_v2] ? options.facetPartitioning[block_idx_v2] :new Array();
  2010. options.facetPartitioning[block_idx_v3] = options.facetPartitioning[block_idx_v3] ? options.facetPartitioning[block_idx_v3] :new Array();
  2011. // push each facet index in each block containing the vertex
  2012. options.facetPartitioning[block_idx_v1].push(index);
  2013. if (block_idx_v2 != block_idx_v1) {
  2014. options.facetPartitioning[block_idx_v2].push(index);
  2015. }
  2016. if (!(block_idx_v3 == block_idx_v2 || block_idx_v3 == block_idx_v1)) {
  2017. options.facetPartitioning[block_idx_v3].push(index);
  2018. }
  2019. if (!(block_idx_o == block_idx_v1 || block_idx_o == block_idx_v2 || block_idx_o == block_idx_v3)) {
  2020. options.facetPartitioning[block_idx_o].push(index);
  2021. }
  2022. }
  2023. // compute the normals anyway
  2024. normals[v1x] += faceNormalx; // accumulate all the normals per face
  2025. normals[v1y] += faceNormaly;
  2026. normals[v1z] += faceNormalz;
  2027. normals[v2x] += faceNormalx;
  2028. normals[v2y] += faceNormaly;
  2029. normals[v2z] += faceNormalz;
  2030. normals[v3x] += faceNormalx;
  2031. normals[v3y] += faceNormaly;
  2032. normals[v3z] += faceNormalz;
  2033. }
  2034. // last normalization of each normal
  2035. for (index = 0; index < normals.length / 3; index++) {
  2036. faceNormalx = normals[index * 3];
  2037. faceNormaly = normals[index * 3 + 1];
  2038. faceNormalz = normals[index * 3 + 2];
  2039. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  2040. length = (length === 0) ? 1.0 : length;
  2041. faceNormalx /= length;
  2042. faceNormaly /= length;
  2043. faceNormalz /= length;
  2044. normals[index * 3] = faceNormalx;
  2045. normals[index * 3 + 1] = faceNormaly;
  2046. normals[index * 3 + 2] = faceNormalz;
  2047. }
  2048. }
  2049. private static _ComputeSides(sideOrientation: number, positions: number[] | Float32Array, indices: number[] | Float32Array, normals: number[] | Float32Array, uvs: number[] | Float32Array, frontUVs?: Vector4, backUVs?: Vector4) {
  2050. var li: number = indices.length;
  2051. var ln: number = normals.length;
  2052. var i: number;
  2053. var n: number;
  2054. sideOrientation = sideOrientation || Mesh.DEFAULTSIDE;
  2055. switch (sideOrientation) {
  2056. case Mesh.FRONTSIDE:
  2057. // nothing changed
  2058. break;
  2059. case Mesh.BACKSIDE:
  2060. var tmp: number;
  2061. // indices
  2062. for (i = 0; i < li; i += 3) {
  2063. tmp = indices[i];
  2064. indices[i] = indices[i + 2];
  2065. indices[i + 2] = tmp;
  2066. }
  2067. // normals
  2068. for (n = 0; n < ln; n++) {
  2069. normals[n] = -normals[n];
  2070. }
  2071. break;
  2072. case Mesh.DOUBLESIDE:
  2073. // positions
  2074. var lp: number = positions.length;
  2075. var l: number = lp / 3;
  2076. for (var p = 0; p < lp; p++) {
  2077. positions[lp + p] = positions[p];
  2078. }
  2079. // indices
  2080. for (i = 0; i < li; i += 3) {
  2081. indices[i + li] = indices[i + 2] + l;
  2082. indices[i + 1 + li] = indices[i + 1] + l;
  2083. indices[i + 2 + li] = indices[i] + l;
  2084. }
  2085. // normals
  2086. for (n = 0; n < ln; n++) {
  2087. normals[ln + n] = -normals[n];
  2088. }
  2089. // uvs
  2090. var lu: number = uvs.length;
  2091. var u: number = 0;
  2092. for (u = 0; u < lu; u++) {
  2093. uvs[u + lu] = uvs[u];
  2094. }
  2095. var frontUVs = frontUVs ? frontUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
  2096. var backUVs = backUVs ? backUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
  2097. u = 0;
  2098. for (i = 0; i < lu / 2; i++) {
  2099. uvs[u] = frontUVs.x + (frontUVs.z - frontUVs.x) * uvs[u];
  2100. uvs[u + 1] = frontUVs.y + (frontUVs.w - frontUVs.y) * uvs[u + 1];
  2101. uvs[u + lu] = backUVs.x + (backUVs.z - backUVs.x) * uvs[u + lu];
  2102. uvs[u + lu + 1] = backUVs.y + (backUVs.w - backUVs.y) * uvs[u + lu + 1];
  2103. u += 2;
  2104. }
  2105. break;
  2106. }
  2107. }
  2108. /**
  2109. * Creates a new VertexData from the imported parameters.
  2110. */
  2111. public static ImportVertexData(parsedVertexData: any, geometry: Geometry) {
  2112. var vertexData = new VertexData();
  2113. // positions
  2114. var positions = parsedVertexData.positions;
  2115. if (positions) {
  2116. vertexData.set(positions, VertexBuffer.PositionKind);
  2117. }
  2118. // normals
  2119. var normals = parsedVertexData.normals;
  2120. if (normals) {
  2121. vertexData.set(normals, VertexBuffer.NormalKind);
  2122. }
  2123. // tangents
  2124. var tangents = parsedVertexData.tangents;
  2125. if (tangents) {
  2126. vertexData.set(tangents, VertexBuffer.TangentKind);
  2127. }
  2128. // uvs
  2129. var uvs = parsedVertexData.uvs;
  2130. if (uvs) {
  2131. vertexData.set(uvs, VertexBuffer.UVKind);
  2132. }
  2133. // uv2s
  2134. var uv2s = parsedVertexData.uv2s;
  2135. if (uv2s) {
  2136. vertexData.set(uv2s, VertexBuffer.UV2Kind);
  2137. }
  2138. // uv3s
  2139. var uv3s = parsedVertexData.uv3s;
  2140. if (uv3s) {
  2141. vertexData.set(uv3s, VertexBuffer.UV3Kind);
  2142. }
  2143. // uv4s
  2144. var uv4s = parsedVertexData.uv4s;
  2145. if (uv4s) {
  2146. vertexData.set(uv4s, VertexBuffer.UV4Kind);
  2147. }
  2148. // uv5s
  2149. var uv5s = parsedVertexData.uv5s;
  2150. if (uv5s) {
  2151. vertexData.set(uv5s, VertexBuffer.UV5Kind);
  2152. }
  2153. // uv6s
  2154. var uv6s = parsedVertexData.uv6s;
  2155. if (uv6s) {
  2156. vertexData.set(uv6s, VertexBuffer.UV6Kind);
  2157. }
  2158. // colors
  2159. var colors = parsedVertexData.colors;
  2160. if (colors) {
  2161. vertexData.set(Color4.CheckColors4(colors, positions.length / 3), VertexBuffer.ColorKind);
  2162. }
  2163. // matricesIndices
  2164. var matricesIndices = parsedVertexData.matricesIndices;
  2165. if (matricesIndices) {
  2166. vertexData.set(matricesIndices, VertexBuffer.MatricesIndicesKind);
  2167. }
  2168. // matricesWeights
  2169. var matricesWeights = parsedVertexData.matricesWeights;
  2170. if (matricesWeights) {
  2171. vertexData.set(matricesWeights, VertexBuffer.MatricesWeightsKind);
  2172. }
  2173. // indices
  2174. var indices = parsedVertexData.indices;
  2175. if (indices) {
  2176. vertexData.indices = indices;
  2177. }
  2178. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  2179. }
  2180. }
  2181. }