mesh.vertexData.js 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. import { __decorate } from "../tslib.es6.js";
  2. import { Vector3, Vector4, TmpVectors } from "../Maths/math.vector.js";
  3. import { VertexBuffer } from "../Buffers/buffer.js";
  4. import { _WarnImport } from "../Misc/devTools.js";
  5. import { Color4 } from "../Maths/math.color.js";
  6. import { Logger } from "../Misc/logger.js";
  7. import { nativeOverride } from "../Misc/decorators.js";
  8. import { makeSyncFunction, runCoroutineSync } from "../Misc/coroutine.js";
  9. import { RuntimeError, ErrorCodes } from "../Misc/error.js";
  10. import { SubMesh } from "./subMesh.js";
  11. /** Class used to attach material info to sub section of a vertex data class */
  12. export class VertexDataMaterialInfo {
  13. }
  14. /**
  15. * This class contains the various kinds of data on every vertex of a mesh used in determining its shape and appearance
  16. */
  17. export class VertexData {
  18. /**
  19. * Creates a new VertexData
  20. */
  21. constructor() {
  22. /**
  23. * Gets the unique ID of this vertex Data
  24. */
  25. this.uniqueId = 0;
  26. /**
  27. * Metadata used to store contextual values
  28. */
  29. this.metadata = {};
  30. this._applyTo = makeSyncFunction(this._applyToCoroutine.bind(this));
  31. this.uniqueId = VertexData._UniqueIDGenerator;
  32. VertexData._UniqueIDGenerator++;
  33. }
  34. /**
  35. * Uses the passed data array to set the set the values for the specified kind of data
  36. * @param data a linear array of floating numbers
  37. * @param kind the type of data that is being set, eg positions, colors etc
  38. */
  39. set(data, kind) {
  40. if (!data.length) {
  41. Logger.Warn(`Setting vertex data kind '${kind}' with an empty array`);
  42. }
  43. switch (kind) {
  44. case VertexBuffer.PositionKind:
  45. this.positions = data;
  46. break;
  47. case VertexBuffer.NormalKind:
  48. this.normals = data;
  49. break;
  50. case VertexBuffer.TangentKind:
  51. this.tangents = data;
  52. break;
  53. case VertexBuffer.UVKind:
  54. this.uvs = data;
  55. break;
  56. case VertexBuffer.UV2Kind:
  57. this.uvs2 = data;
  58. break;
  59. case VertexBuffer.UV3Kind:
  60. this.uvs3 = data;
  61. break;
  62. case VertexBuffer.UV4Kind:
  63. this.uvs4 = data;
  64. break;
  65. case VertexBuffer.UV5Kind:
  66. this.uvs5 = data;
  67. break;
  68. case VertexBuffer.UV6Kind:
  69. this.uvs6 = data;
  70. break;
  71. case VertexBuffer.ColorKind:
  72. this.colors = data;
  73. break;
  74. case VertexBuffer.MatricesIndicesKind:
  75. this.matricesIndices = data;
  76. break;
  77. case VertexBuffer.MatricesWeightsKind:
  78. this.matricesWeights = data;
  79. break;
  80. case VertexBuffer.MatricesIndicesExtraKind:
  81. this.matricesIndicesExtra = data;
  82. break;
  83. case VertexBuffer.MatricesWeightsExtraKind:
  84. this.matricesWeightsExtra = data;
  85. break;
  86. }
  87. }
  88. /**
  89. * Associates the vertexData to the passed Mesh.
  90. * Sets it as updatable or not (default `false`)
  91. * @param mesh the mesh the vertexData is applied to
  92. * @param updatable when used and having the value true allows new data to update the vertexData
  93. * @returns the VertexData
  94. */
  95. applyToMesh(mesh, updatable) {
  96. this._applyTo(mesh, updatable, false);
  97. return this;
  98. }
  99. /**
  100. * Associates the vertexData to the passed Geometry.
  101. * Sets it as updatable or not (default `false`)
  102. * @param geometry the geometry the vertexData is applied to
  103. * @param updatable when used and having the value true allows new data to update the vertexData
  104. * @returns VertexData
  105. */
  106. applyToGeometry(geometry, updatable) {
  107. this._applyTo(geometry, updatable, false);
  108. return this;
  109. }
  110. /**
  111. * Updates the associated mesh
  112. * @param mesh the mesh to be updated
  113. * @returns VertexData
  114. */
  115. updateMesh(mesh) {
  116. this._update(mesh);
  117. return this;
  118. }
  119. /**
  120. * Updates the associated geometry
  121. * @param geometry the geometry to be updated
  122. * @returns VertexData.
  123. */
  124. updateGeometry(geometry) {
  125. this._update(geometry);
  126. return this;
  127. }
  128. /**
  129. * @internal
  130. */
  131. *_applyToCoroutine(meshOrGeometry, updatable = false, isAsync) {
  132. if (this.positions) {
  133. meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
  134. if (isAsync) {
  135. yield;
  136. }
  137. }
  138. if (this.normals) {
  139. meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
  140. if (isAsync) {
  141. yield;
  142. }
  143. }
  144. if (this.tangents) {
  145. meshOrGeometry.setVerticesData(VertexBuffer.TangentKind, this.tangents, updatable);
  146. if (isAsync) {
  147. yield;
  148. }
  149. }
  150. if (this.uvs) {
  151. meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
  152. if (isAsync) {
  153. yield;
  154. }
  155. }
  156. if (this.uvs2) {
  157. meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
  158. if (isAsync) {
  159. yield;
  160. }
  161. }
  162. if (this.uvs3) {
  163. meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
  164. if (isAsync) {
  165. yield;
  166. }
  167. }
  168. if (this.uvs4) {
  169. meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
  170. if (isAsync) {
  171. yield;
  172. }
  173. }
  174. if (this.uvs5) {
  175. meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
  176. if (isAsync) {
  177. yield;
  178. }
  179. }
  180. if (this.uvs6) {
  181. meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
  182. if (isAsync) {
  183. yield;
  184. }
  185. }
  186. if (this.colors) {
  187. meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
  188. if (this.hasVertexAlpha && meshOrGeometry.hasVertexAlpha !== undefined) {
  189. meshOrGeometry.hasVertexAlpha = true;
  190. }
  191. if (isAsync) {
  192. yield;
  193. }
  194. }
  195. if (this.matricesIndices) {
  196. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
  197. if (isAsync) {
  198. yield;
  199. }
  200. }
  201. if (this.matricesWeights) {
  202. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
  203. if (isAsync) {
  204. yield;
  205. }
  206. }
  207. if (this.matricesIndicesExtra) {
  208. meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updatable);
  209. if (isAsync) {
  210. yield;
  211. }
  212. }
  213. if (this.matricesWeightsExtra) {
  214. meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updatable);
  215. if (isAsync) {
  216. yield;
  217. }
  218. }
  219. if (this.indices) {
  220. meshOrGeometry.setIndices(this.indices, null, updatable);
  221. if (isAsync) {
  222. yield;
  223. }
  224. }
  225. else {
  226. meshOrGeometry.setIndices([], null);
  227. }
  228. if (meshOrGeometry.subMeshes && this.materialInfos && this.materialInfos.length > 1) {
  229. const mesh = meshOrGeometry;
  230. mesh.subMeshes = [];
  231. for (const matInfo of this.materialInfos) {
  232. new SubMesh(matInfo.materialIndex, matInfo.verticesStart, matInfo.verticesCount, matInfo.indexStart, matInfo.indexCount, mesh);
  233. }
  234. }
  235. return this;
  236. }
  237. _update(meshOrGeometry, updateExtends, makeItUnique) {
  238. if (this.positions) {
  239. meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
  240. }
  241. if (this.normals) {
  242. meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
  243. }
  244. if (this.tangents) {
  245. meshOrGeometry.updateVerticesData(VertexBuffer.TangentKind, this.tangents, updateExtends, makeItUnique);
  246. }
  247. if (this.uvs) {
  248. meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
  249. }
  250. if (this.uvs2) {
  251. meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
  252. }
  253. if (this.uvs3) {
  254. meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
  255. }
  256. if (this.uvs4) {
  257. meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
  258. }
  259. if (this.uvs5) {
  260. meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
  261. }
  262. if (this.uvs6) {
  263. meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
  264. }
  265. if (this.colors) {
  266. meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
  267. }
  268. if (this.matricesIndices) {
  269. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
  270. }
  271. if (this.matricesWeights) {
  272. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
  273. }
  274. if (this.matricesIndicesExtra) {
  275. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updateExtends, makeItUnique);
  276. }
  277. if (this.matricesWeightsExtra) {
  278. meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updateExtends, makeItUnique);
  279. }
  280. if (this.indices) {
  281. meshOrGeometry.setIndices(this.indices, null);
  282. }
  283. return this;
  284. }
  285. static _TransformVector3Coordinates(coordinates, transformation, offset = 0, length = coordinates.length) {
  286. const coordinate = TmpVectors.Vector3[0];
  287. const transformedCoordinate = TmpVectors.Vector3[1];
  288. for (let index = offset; index < offset + length; index += 3) {
  289. Vector3.FromArrayToRef(coordinates, index, coordinate);
  290. Vector3.TransformCoordinatesToRef(coordinate, transformation, transformedCoordinate);
  291. coordinates[index] = transformedCoordinate.x;
  292. coordinates[index + 1] = transformedCoordinate.y;
  293. coordinates[index + 2] = transformedCoordinate.z;
  294. }
  295. }
  296. static _TransformVector3Normals(normals, transformation, offset = 0, length = normals.length) {
  297. const normal = TmpVectors.Vector3[0];
  298. const transformedNormal = TmpVectors.Vector3[1];
  299. for (let index = offset; index < offset + length; index += 3) {
  300. Vector3.FromArrayToRef(normals, index, normal);
  301. Vector3.TransformNormalToRef(normal, transformation, transformedNormal);
  302. normals[index] = transformedNormal.x;
  303. normals[index + 1] = transformedNormal.y;
  304. normals[index + 2] = transformedNormal.z;
  305. }
  306. }
  307. static _TransformVector4Normals(normals, transformation, offset = 0, length = normals.length) {
  308. const normal = TmpVectors.Vector4[0];
  309. const transformedNormal = TmpVectors.Vector4[1];
  310. for (let index = offset; index < offset + length; index += 4) {
  311. Vector4.FromArrayToRef(normals, index, normal);
  312. Vector4.TransformNormalToRef(normal, transformation, transformedNormal);
  313. normals[index] = transformedNormal.x;
  314. normals[index + 1] = transformedNormal.y;
  315. normals[index + 2] = transformedNormal.z;
  316. normals[index + 3] = transformedNormal.w;
  317. }
  318. }
  319. static _FlipFaces(indices, offset = 0, length = indices.length) {
  320. for (let index = offset; index < offset + length; index += 3) {
  321. const tmp = indices[index + 1];
  322. indices[index + 1] = indices[index + 2];
  323. indices[index + 2] = tmp;
  324. }
  325. }
  326. /**
  327. * Transforms each position and each normal of the vertexData according to the passed Matrix
  328. * @param matrix the transforming matrix
  329. * @returns the VertexData
  330. */
  331. transform(matrix) {
  332. const flip = matrix.determinant() < 0;
  333. if (this.positions) {
  334. VertexData._TransformVector3Coordinates(this.positions, matrix);
  335. }
  336. if (this.normals) {
  337. VertexData._TransformVector3Normals(this.normals, matrix);
  338. }
  339. if (this.tangents) {
  340. VertexData._TransformVector4Normals(this.tangents, matrix);
  341. }
  342. if (flip && this.indices) {
  343. VertexData._FlipFaces(this.indices);
  344. }
  345. return this;
  346. }
  347. /**
  348. * Generates an array of vertex data where each vertex data only has one material info
  349. * @returns An array of VertexData
  350. */
  351. splitBasedOnMaterialID() {
  352. if (!this.materialInfos || this.materialInfos.length < 2) {
  353. return [this];
  354. }
  355. const result = [];
  356. for (const materialInfo of this.materialInfos) {
  357. const vertexData = new VertexData();
  358. if (this.positions) {
  359. vertexData.positions = this.positions.slice(materialInfo.verticesStart * 3, (materialInfo.verticesCount + materialInfo.verticesStart) * 3);
  360. }
  361. if (this.normals) {
  362. vertexData.normals = this.normals.slice(materialInfo.verticesStart * 3, (materialInfo.verticesCount + materialInfo.verticesStart) * 3);
  363. }
  364. if (this.tangents) {
  365. vertexData.tangents = this.tangents.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  366. }
  367. if (this.colors) {
  368. vertexData.colors = this.colors.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  369. }
  370. if (this.uvs) {
  371. vertexData.uvs = this.uvs.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  372. }
  373. if (this.uvs2) {
  374. vertexData.uvs2 = this.uvs2.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  375. }
  376. if (this.uvs3) {
  377. vertexData.uvs3 = this.uvs3.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  378. }
  379. if (this.uvs4) {
  380. vertexData.uvs4 = this.uvs4.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  381. }
  382. if (this.uvs5) {
  383. vertexData.uvs5 = this.uvs5.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  384. }
  385. if (this.uvs6) {
  386. vertexData.uvs6 = this.uvs6.slice(materialInfo.verticesStart * 2, (materialInfo.verticesCount + materialInfo.verticesStart) * 2);
  387. }
  388. if (this.matricesIndices) {
  389. vertexData.matricesIndices = this.matricesIndices.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  390. }
  391. if (this.matricesIndicesExtra) {
  392. vertexData.matricesIndicesExtra = this.matricesIndicesExtra.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  393. }
  394. if (this.matricesWeights) {
  395. vertexData.matricesWeights = this.matricesWeights.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  396. }
  397. if (this.matricesWeightsExtra) {
  398. vertexData.matricesWeightsExtra = this.matricesWeightsExtra.slice(materialInfo.verticesStart * 4, (materialInfo.verticesCount + materialInfo.verticesStart) * 4);
  399. }
  400. if (this.indices) {
  401. vertexData.indices = [];
  402. for (let index = materialInfo.indexStart; index < materialInfo.indexStart + materialInfo.indexCount; index++) {
  403. vertexData.indices.push(this.indices[index] - materialInfo.verticesStart);
  404. }
  405. }
  406. const newMaterialInfo = new VertexDataMaterialInfo();
  407. newMaterialInfo.indexStart = 0;
  408. newMaterialInfo.indexCount = vertexData.indices ? vertexData.indices.length : 0;
  409. newMaterialInfo.materialIndex = materialInfo.materialIndex;
  410. newMaterialInfo.verticesStart = 0;
  411. newMaterialInfo.verticesCount = (vertexData.positions ? vertexData.positions.length : 0) / 3;
  412. vertexData.materialInfos = [newMaterialInfo];
  413. result.push(vertexData);
  414. }
  415. return result;
  416. }
  417. /**
  418. * Merges the passed VertexData into the current one
  419. * @param others the VertexData to be merged into the current one
  420. * @param use32BitsIndices defines a boolean indicating if indices must be store in a 32 bits array
  421. * @param forceCloneIndices defines a boolean indicating if indices are forced to be cloned
  422. * @param mergeMaterialIds defines a boolean indicating if we need to merge the material infos
  423. * @param enableCompletion defines a boolean indicating if the vertex data should be completed to be compatible
  424. * @returns the modified VertexData
  425. */
  426. merge(others, use32BitsIndices = false, forceCloneIndices = false, mergeMaterialIds = false, enableCompletion = false) {
  427. const vertexDatas = Array.isArray(others)
  428. ? others.map((other) => {
  429. return { vertexData: other };
  430. })
  431. : [{ vertexData: others }];
  432. return runCoroutineSync(this._mergeCoroutine(undefined, vertexDatas, use32BitsIndices, false, forceCloneIndices, mergeMaterialIds, enableCompletion));
  433. }
  434. /**
  435. * @internal
  436. */
  437. *_mergeCoroutine(transform, vertexDatas, use32BitsIndices = false, isAsync, forceCloneIndices, mergeMaterialIds = false, enableCompletion = false) {
  438. this._validate();
  439. let others = vertexDatas.map((vertexData) => vertexData.vertexData);
  440. // eslint-disable-next-line @typescript-eslint/no-this-alias
  441. let root = this;
  442. if (enableCompletion) {
  443. // First let's make sure we have the max set of attributes on the main vertex data
  444. for (const other of others) {
  445. if (!other) {
  446. continue;
  447. }
  448. other._validate();
  449. if (!this.normals && other.normals) {
  450. this.normals = new Float32Array(this.positions.length);
  451. }
  452. if (!this.tangents && other.tangents) {
  453. this.tangents = new Float32Array((this.positions.length / 3) * 4);
  454. }
  455. if (!this.uvs && other.uvs) {
  456. this.uvs = new Float32Array((this.positions.length / 3) * 2);
  457. }
  458. if (!this.uvs2 && other.uvs2) {
  459. this.uvs2 = new Float32Array((this.positions.length / 3) * 2);
  460. }
  461. if (!this.uvs3 && other.uvs3) {
  462. this.uvs3 = new Float32Array((this.positions.length / 3) * 2);
  463. }
  464. if (!this.uvs4 && other.uvs4) {
  465. this.uvs4 = new Float32Array((this.positions.length / 3) * 2);
  466. }
  467. if (!this.uvs5 && other.uvs5) {
  468. this.uvs5 = new Float32Array((this.positions.length / 3) * 2);
  469. }
  470. if (!this.uvs6 && other.uvs6) {
  471. this.uvs6 = new Float32Array((this.positions.length / 3) * 2);
  472. }
  473. if (!this.colors && other.colors) {
  474. this.colors = new Float32Array((this.positions.length / 3) * 4);
  475. this.colors.fill(1); // Set to white by default
  476. }
  477. if (!this.matricesIndices && other.matricesIndices) {
  478. this.matricesIndices = new Float32Array((this.positions.length / 3) * 4);
  479. }
  480. if (!this.matricesWeights && other.matricesWeights) {
  481. this.matricesWeights = new Float32Array((this.positions.length / 3) * 4);
  482. }
  483. if (!this.matricesIndicesExtra && other.matricesIndicesExtra) {
  484. this.matricesIndicesExtra = new Float32Array((this.positions.length / 3) * 4);
  485. }
  486. if (!this.matricesWeightsExtra && other.matricesWeightsExtra) {
  487. this.matricesWeightsExtra = new Float32Array((this.positions.length / 3) * 4);
  488. }
  489. }
  490. }
  491. for (const other of others) {
  492. if (!other) {
  493. continue;
  494. }
  495. if (!enableCompletion) {
  496. other._validate();
  497. if (!this.normals !== !other.normals ||
  498. !this.tangents !== !other.tangents ||
  499. !this.uvs !== !other.uvs ||
  500. !this.uvs2 !== !other.uvs2 ||
  501. !this.uvs3 !== !other.uvs3 ||
  502. !this.uvs4 !== !other.uvs4 ||
  503. !this.uvs5 !== !other.uvs5 ||
  504. !this.uvs6 !== !other.uvs6 ||
  505. !this.colors !== !other.colors ||
  506. !this.matricesIndices !== !other.matricesIndices ||
  507. !this.matricesWeights !== !other.matricesWeights ||
  508. !this.matricesIndicesExtra !== !other.matricesIndicesExtra ||
  509. !this.matricesWeightsExtra !== !other.matricesWeightsExtra) {
  510. throw new Error("Cannot merge vertex data that do not have the same set of attributes");
  511. }
  512. }
  513. else {
  514. // Align the others with main set of attributes
  515. if (this.normals && !other.normals) {
  516. other.normals = new Float32Array(other.positions.length);
  517. }
  518. if (this.tangents && !other.tangents) {
  519. other.tangents = new Float32Array((other.positions.length / 3) * 4);
  520. }
  521. if (this.uvs && !other.uvs) {
  522. other.uvs = new Float32Array((other.positions.length / 3) * 2);
  523. }
  524. if (this.uvs2 && !other.uvs2) {
  525. other.uvs2 = new Float32Array((other.positions.length / 3) * 2);
  526. }
  527. if (this.uvs3 && !other.uvs3) {
  528. other.uvs3 = new Float32Array((other.positions.length / 3) * 2);
  529. }
  530. if (this.uvs4 && !other.uvs4) {
  531. other.uvs4 = new Float32Array((other.positions.length / 3) * 2);
  532. }
  533. if (this.uvs5 && !other.uvs5) {
  534. other.uvs5 = new Float32Array((other.positions.length / 3) * 2);
  535. }
  536. if (this.uvs6 && !other.uvs6) {
  537. other.uvs6 = new Float32Array((other.positions.length / 3) * 2);
  538. }
  539. if (this.colors && !other.colors) {
  540. other.colors = new Float32Array((other.positions.length / 3) * 4);
  541. other.colors.fill(1); // Set to white by default
  542. }
  543. if (this.matricesIndices && !other.matricesIndices) {
  544. other.matricesIndices = new Float32Array((other.positions.length / 3) * 4);
  545. }
  546. if (this.matricesWeights && !other.matricesWeights) {
  547. other.matricesWeights = new Float32Array((other.positions.length / 3) * 4);
  548. }
  549. if (this.matricesIndicesExtra && !other.matricesIndicesExtra) {
  550. other.matricesIndicesExtra = new Float32Array((other.positions.length / 3) * 4);
  551. }
  552. if (this.matricesWeightsExtra && !other.matricesWeightsExtra) {
  553. other.matricesWeightsExtra = new Float32Array((other.positions.length / 3) * 4);
  554. }
  555. }
  556. }
  557. if (mergeMaterialIds) {
  558. // Merge material infos
  559. let materialIndex = 0;
  560. let indexOffset = 0;
  561. let vertexOffset = 0;
  562. const materialInfos = [];
  563. let currentMaterialInfo = null;
  564. const vertexDataList = [];
  565. // We need to split vertexData with more than one materialInfo
  566. for (const split of this.splitBasedOnMaterialID()) {
  567. vertexDataList.push({ vertexData: split, transform: transform });
  568. }
  569. for (const data of vertexDatas) {
  570. if (!data.vertexData) {
  571. continue;
  572. }
  573. for (const split of data.vertexData.splitBasedOnMaterialID()) {
  574. vertexDataList.push({ vertexData: split, transform: data.transform });
  575. }
  576. }
  577. // Sort by material IDs
  578. vertexDataList.sort((a, b) => {
  579. const matInfoA = a.vertexData.materialInfos ? a.vertexData.materialInfos[0].materialIndex : 0;
  580. const matInfoB = b.vertexData.materialInfos ? b.vertexData.materialInfos[0].materialIndex : 0;
  581. if (matInfoA > matInfoB) {
  582. return 1;
  583. }
  584. if (matInfoA === matInfoB) {
  585. return 0;
  586. }
  587. return -1;
  588. });
  589. // Build the new material info
  590. for (const vertexDataSource of vertexDataList) {
  591. const vertexData = vertexDataSource.vertexData;
  592. if (vertexData.materialInfos) {
  593. materialIndex = vertexData.materialInfos[0].materialIndex;
  594. }
  595. else {
  596. materialIndex = 0;
  597. }
  598. if (currentMaterialInfo && currentMaterialInfo.materialIndex === materialIndex) {
  599. currentMaterialInfo.indexCount += vertexData.indices.length;
  600. currentMaterialInfo.verticesCount += vertexData.positions.length / 3;
  601. }
  602. else {
  603. const materialInfo = new VertexDataMaterialInfo();
  604. materialInfo.materialIndex = materialIndex;
  605. materialInfo.indexStart = indexOffset;
  606. materialInfo.indexCount = vertexData.indices.length;
  607. materialInfo.verticesStart = vertexOffset;
  608. materialInfo.verticesCount = vertexData.positions.length / 3;
  609. materialInfos.push(materialInfo);
  610. currentMaterialInfo = materialInfo;
  611. }
  612. indexOffset += vertexData.indices.length;
  613. vertexOffset += vertexData.positions.length / 3;
  614. }
  615. // Extract sorted values
  616. const first = vertexDataList.splice(0, 1)[0];
  617. root = first.vertexData;
  618. transform = first.transform;
  619. others = vertexDataList.map((v) => v.vertexData);
  620. vertexDatas = vertexDataList;
  621. this.materialInfos = materialInfos;
  622. }
  623. // Merge geometries
  624. const totalIndices = others.reduce((indexSum, vertexData) => indexSum + (vertexData.indices?.length ?? 0), root.indices?.length ?? 0);
  625. const sliceIndices = forceCloneIndices || others.some((vertexData) => vertexData.indices === root.indices);
  626. let indices = sliceIndices ? root.indices?.slice() : root.indices;
  627. if (totalIndices > 0) {
  628. let indicesOffset = indices?.length ?? 0;
  629. if (!indices) {
  630. indices = new Array(totalIndices);
  631. }
  632. if (indices.length !== totalIndices) {
  633. if (Array.isArray(indices)) {
  634. indices.length = totalIndices;
  635. }
  636. else {
  637. const temp = use32BitsIndices || indices instanceof Uint32Array ? new Uint32Array(totalIndices) : new Uint16Array(totalIndices);
  638. temp.set(indices);
  639. indices = temp;
  640. }
  641. if (transform && transform.determinant() < 0) {
  642. VertexData._FlipFaces(indices, 0, indicesOffset);
  643. }
  644. }
  645. let positionsOffset = root.positions ? root.positions.length / 3 : 0;
  646. for (const { vertexData: other, transform } of vertexDatas) {
  647. if (other.indices) {
  648. for (let index = 0; index < other.indices.length; index++) {
  649. indices[indicesOffset + index] = other.indices[index] + positionsOffset;
  650. }
  651. if (transform && transform.determinant() < 0) {
  652. VertexData._FlipFaces(indices, indicesOffset, other.indices.length);
  653. }
  654. // The call to _validate already checked for positions
  655. positionsOffset += other.positions.length / 3;
  656. indicesOffset += other.indices.length;
  657. if (isAsync) {
  658. yield;
  659. }
  660. }
  661. }
  662. }
  663. this.indices = indices;
  664. this.positions = VertexData._MergeElement(VertexBuffer.PositionKind, root.positions, transform, vertexDatas.map((other) => [other.vertexData.positions, other.transform]));
  665. if (isAsync) {
  666. yield;
  667. }
  668. if (root.normals) {
  669. this.normals = VertexData._MergeElement(VertexBuffer.NormalKind, root.normals, transform, vertexDatas.map((other) => [other.vertexData.normals, other.transform]));
  670. if (isAsync) {
  671. yield;
  672. }
  673. }
  674. if (root.tangents) {
  675. this.tangents = VertexData._MergeElement(VertexBuffer.TangentKind, root.tangents, transform, vertexDatas.map((other) => [other.vertexData.tangents, other.transform]));
  676. if (isAsync) {
  677. yield;
  678. }
  679. }
  680. if (root.uvs) {
  681. this.uvs = VertexData._MergeElement(VertexBuffer.UVKind, root.uvs, transform, vertexDatas.map((other) => [other.vertexData.uvs, other.transform]));
  682. if (isAsync) {
  683. yield;
  684. }
  685. }
  686. if (root.uvs2) {
  687. this.uvs2 = VertexData._MergeElement(VertexBuffer.UV2Kind, root.uvs2, transform, vertexDatas.map((other) => [other.vertexData.uvs2, other.transform]));
  688. if (isAsync) {
  689. yield;
  690. }
  691. }
  692. if (root.uvs3) {
  693. this.uvs3 = VertexData._MergeElement(VertexBuffer.UV3Kind, root.uvs3, transform, vertexDatas.map((other) => [other.vertexData.uvs3, other.transform]));
  694. if (isAsync) {
  695. yield;
  696. }
  697. }
  698. if (root.uvs4) {
  699. this.uvs4 = VertexData._MergeElement(VertexBuffer.UV4Kind, root.uvs4, transform, vertexDatas.map((other) => [other.vertexData.uvs4, other.transform]));
  700. if (isAsync) {
  701. yield;
  702. }
  703. }
  704. if (root.uvs5) {
  705. this.uvs5 = VertexData._MergeElement(VertexBuffer.UV5Kind, root.uvs5, transform, vertexDatas.map((other) => [other.vertexData.uvs5, other.transform]));
  706. if (isAsync) {
  707. yield;
  708. }
  709. }
  710. if (root.uvs6) {
  711. this.uvs6 = VertexData._MergeElement(VertexBuffer.UV6Kind, root.uvs6, transform, vertexDatas.map((other) => [other.vertexData.uvs6, other.transform]));
  712. if (isAsync) {
  713. yield;
  714. }
  715. }
  716. if (root.colors) {
  717. this.colors = VertexData._MergeElement(VertexBuffer.ColorKind, root.colors, transform, vertexDatas.map((other) => [other.vertexData.colors, other.transform]));
  718. if (root.hasVertexAlpha !== undefined || vertexDatas.some((other) => other.vertexData.hasVertexAlpha !== undefined)) {
  719. this.hasVertexAlpha = root.hasVertexAlpha || vertexDatas.some((other) => other.vertexData.hasVertexAlpha);
  720. }
  721. if (isAsync) {
  722. yield;
  723. }
  724. }
  725. if (root.matricesIndices) {
  726. this.matricesIndices = VertexData._MergeElement(VertexBuffer.MatricesIndicesKind, root.matricesIndices, transform, vertexDatas.map((other) => [other.vertexData.matricesIndices, other.transform]));
  727. if (isAsync) {
  728. yield;
  729. }
  730. }
  731. if (root.matricesWeights) {
  732. this.matricesWeights = VertexData._MergeElement(VertexBuffer.MatricesWeightsKind, root.matricesWeights, transform, vertexDatas.map((other) => [other.vertexData.matricesWeights, other.transform]));
  733. if (isAsync) {
  734. yield;
  735. }
  736. }
  737. if (root.matricesIndicesExtra) {
  738. this.matricesIndicesExtra = VertexData._MergeElement(VertexBuffer.MatricesIndicesExtraKind, root.matricesIndicesExtra, transform, vertexDatas.map((other) => [other.vertexData.matricesIndicesExtra, other.transform]));
  739. if (isAsync) {
  740. yield;
  741. }
  742. }
  743. if (root.matricesWeightsExtra) {
  744. this.matricesWeightsExtra = VertexData._MergeElement(VertexBuffer.MatricesWeightsExtraKind, root.matricesWeightsExtra, transform, vertexDatas.map((other) => [other.vertexData.matricesWeightsExtra, other.transform]));
  745. }
  746. return this;
  747. }
  748. static _MergeElement(kind, source, transform, others) {
  749. const nonNullOthers = others.filter((other) => other[0] !== null && other[0] !== undefined);
  750. // If there is no source to copy and no other non-null sources then skip this element.
  751. if (!source && nonNullOthers.length == 0) {
  752. return source;
  753. }
  754. if (!source) {
  755. return this._MergeElement(kind, nonNullOthers[0][0], nonNullOthers[0][1], nonNullOthers.slice(1));
  756. }
  757. const len = nonNullOthers.reduce((sumLen, elements) => sumLen + elements[0].length, source.length);
  758. const transformRange = kind === VertexBuffer.PositionKind
  759. ? VertexData._TransformVector3Coordinates
  760. : kind === VertexBuffer.NormalKind
  761. ? VertexData._TransformVector3Normals
  762. : kind === VertexBuffer.TangentKind
  763. ? VertexData._TransformVector4Normals
  764. : () => { };
  765. if (source instanceof Float32Array) {
  766. // use non-loop method when the source is Float32Array
  767. const ret32 = new Float32Array(len);
  768. ret32.set(source);
  769. transform && transformRange(ret32, transform, 0, source.length);
  770. let offset = source.length;
  771. for (const [vertexData, transform] of nonNullOthers) {
  772. ret32.set(vertexData, offset);
  773. transform && transformRange(ret32, transform, offset, vertexData.length);
  774. offset += vertexData.length;
  775. }
  776. return ret32;
  777. }
  778. else {
  779. // don't use concat as it is super slow, just loop for other cases
  780. const ret = new Array(len);
  781. for (let i = 0; i < source.length; i++) {
  782. ret[i] = source[i];
  783. }
  784. transform && transformRange(ret, transform, 0, source.length);
  785. let offset = source.length;
  786. for (const [vertexData, transform] of nonNullOthers) {
  787. for (let i = 0; i < vertexData.length; i++) {
  788. ret[offset + i] = vertexData[i];
  789. }
  790. transform && transformRange(ret, transform, offset, vertexData.length);
  791. offset += vertexData.length;
  792. }
  793. return ret;
  794. }
  795. }
  796. _validate() {
  797. if (!this.positions) {
  798. throw new RuntimeError("Positions are required", ErrorCodes.MeshInvalidPositionsError);
  799. }
  800. const getElementCount = (kind, values) => {
  801. const stride = VertexBuffer.DeduceStride(kind);
  802. if (values.length % stride !== 0) {
  803. throw new Error("The " + kind + "s array count must be a multiple of " + stride);
  804. }
  805. return values.length / stride;
  806. };
  807. const positionsElementCount = getElementCount(VertexBuffer.PositionKind, this.positions);
  808. const validateElementCount = (kind, values) => {
  809. const elementCount = getElementCount(kind, values);
  810. if (elementCount !== positionsElementCount) {
  811. throw new Error("The " + kind + "s element count (" + elementCount + ") does not match the positions count (" + positionsElementCount + ")");
  812. }
  813. };
  814. if (this.normals) {
  815. validateElementCount(VertexBuffer.NormalKind, this.normals);
  816. }
  817. if (this.tangents) {
  818. validateElementCount(VertexBuffer.TangentKind, this.tangents);
  819. }
  820. if (this.uvs) {
  821. validateElementCount(VertexBuffer.UVKind, this.uvs);
  822. }
  823. if (this.uvs2) {
  824. validateElementCount(VertexBuffer.UV2Kind, this.uvs2);
  825. }
  826. if (this.uvs3) {
  827. validateElementCount(VertexBuffer.UV3Kind, this.uvs3);
  828. }
  829. if (this.uvs4) {
  830. validateElementCount(VertexBuffer.UV4Kind, this.uvs4);
  831. }
  832. if (this.uvs5) {
  833. validateElementCount(VertexBuffer.UV5Kind, this.uvs5);
  834. }
  835. if (this.uvs6) {
  836. validateElementCount(VertexBuffer.UV6Kind, this.uvs6);
  837. }
  838. if (this.colors) {
  839. validateElementCount(VertexBuffer.ColorKind, this.colors);
  840. }
  841. if (this.matricesIndices) {
  842. validateElementCount(VertexBuffer.MatricesIndicesKind, this.matricesIndices);
  843. }
  844. if (this.matricesWeights) {
  845. validateElementCount(VertexBuffer.MatricesWeightsKind, this.matricesWeights);
  846. }
  847. if (this.matricesIndicesExtra) {
  848. validateElementCount(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra);
  849. }
  850. if (this.matricesWeightsExtra) {
  851. validateElementCount(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra);
  852. }
  853. }
  854. /**
  855. * Clone the current vertex data
  856. * @returns a copy of the current data
  857. */
  858. clone() {
  859. const serializationObject = this.serialize();
  860. return VertexData.Parse(serializationObject);
  861. }
  862. /**
  863. * Serializes the VertexData
  864. * @returns a serialized object
  865. */
  866. serialize() {
  867. const serializationObject = {};
  868. if (this.positions) {
  869. serializationObject.positions = Array.from(this.positions);
  870. }
  871. if (this.normals) {
  872. serializationObject.normals = Array.from(this.normals);
  873. }
  874. if (this.tangents) {
  875. serializationObject.tangents = Array.from(this.tangents);
  876. }
  877. if (this.uvs) {
  878. serializationObject.uvs = Array.from(this.uvs);
  879. }
  880. if (this.uvs2) {
  881. serializationObject.uvs2 = Array.from(this.uvs2);
  882. }
  883. if (this.uvs3) {
  884. serializationObject.uvs3 = Array.from(this.uvs3);
  885. }
  886. if (this.uvs4) {
  887. serializationObject.uvs4 = Array.from(this.uvs4);
  888. }
  889. if (this.uvs5) {
  890. serializationObject.uvs5 = Array.from(this.uvs5);
  891. }
  892. if (this.uvs6) {
  893. serializationObject.uvs6 = Array.from(this.uvs6);
  894. }
  895. if (this.colors) {
  896. serializationObject.colors = Array.from(this.colors);
  897. serializationObject.hasVertexAlpha = this.hasVertexAlpha;
  898. }
  899. if (this.matricesIndices) {
  900. serializationObject.matricesIndices = Array.from(this.matricesIndices);
  901. serializationObject.matricesIndices._isExpanded = true;
  902. }
  903. if (this.matricesWeights) {
  904. serializationObject.matricesWeights = Array.from(this.matricesWeights);
  905. }
  906. if (this.matricesIndicesExtra) {
  907. serializationObject.matricesIndicesExtra = Array.from(this.matricesIndicesExtra);
  908. serializationObject.matricesIndicesExtra._isExpanded = true;
  909. }
  910. if (this.matricesWeightsExtra) {
  911. serializationObject.matricesWeightsExtra = Array.from(this.matricesWeightsExtra);
  912. }
  913. serializationObject.indices = Array.from(this.indices);
  914. if (this.materialInfos) {
  915. serializationObject.materialInfos = [];
  916. for (const materialInfo of this.materialInfos) {
  917. const materialInfoSerializationObject = {
  918. indexStart: materialInfo.indexStart,
  919. indexCount: materialInfo.indexCount,
  920. materialIndex: materialInfo.materialIndex,
  921. verticesStart: materialInfo.verticesStart,
  922. verticesCount: materialInfo.verticesCount,
  923. };
  924. serializationObject.materialInfos.push(materialInfoSerializationObject);
  925. }
  926. }
  927. return serializationObject;
  928. }
  929. // Statics
  930. /**
  931. * Extracts the vertexData from a mesh
  932. * @param mesh the mesh from which to extract the VertexData
  933. * @param copyWhenShared defines if the VertexData must be cloned when shared between multiple meshes, optional, default false
  934. * @param forceCopy indicating that the VertexData must be cloned, optional, default false
  935. * @returns the object VertexData associated to the passed mesh
  936. */
  937. static ExtractFromMesh(mesh, copyWhenShared, forceCopy) {
  938. return VertexData._ExtractFrom(mesh, copyWhenShared, forceCopy);
  939. }
  940. /**
  941. * Extracts the vertexData from the geometry
  942. * @param geometry the geometry from which to extract the VertexData
  943. * @param copyWhenShared defines if the VertexData must be cloned when the geometry is shared between multiple meshes, optional, default false
  944. * @param forceCopy indicating that the VertexData must be cloned, optional, default false
  945. * @returns the object VertexData associated to the passed mesh
  946. */
  947. static ExtractFromGeometry(geometry, copyWhenShared, forceCopy) {
  948. return VertexData._ExtractFrom(geometry, copyWhenShared, forceCopy);
  949. }
  950. static _ExtractFrom(meshOrGeometry, copyWhenShared, forceCopy) {
  951. const result = new VertexData();
  952. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  953. result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared, forceCopy);
  954. }
  955. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  956. result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared, forceCopy);
  957. }
  958. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.TangentKind)) {
  959. result.tangents = meshOrGeometry.getVerticesData(VertexBuffer.TangentKind, copyWhenShared, forceCopy);
  960. }
  961. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
  962. result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared, forceCopy);
  963. }
  964. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  965. result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared, forceCopy);
  966. }
  967. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  968. result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared, forceCopy);
  969. }
  970. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  971. result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared, forceCopy);
  972. }
  973. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  974. result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared, forceCopy);
  975. }
  976. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  977. result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared, forceCopy);
  978. }
  979. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  980. result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared, forceCopy);
  981. }
  982. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  983. result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared, forceCopy);
  984. }
  985. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  986. result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared, forceCopy);
  987. }
  988. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesExtraKind)) {
  989. result.matricesIndicesExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesExtraKind, copyWhenShared, forceCopy);
  990. }
  991. if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsExtraKind)) {
  992. result.matricesWeightsExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsExtraKind, copyWhenShared, forceCopy);
  993. }
  994. result.indices = meshOrGeometry.getIndices(copyWhenShared, forceCopy);
  995. return result;
  996. }
  997. /**
  998. * Creates the VertexData for a Ribbon
  999. * @param options an object used to set the following optional parameters for the ribbon, required but can be empty
  1000. * * pathArray array of paths, each of which an array of successive Vector3
  1001. * * closeArray creates a seam between the first and the last paths of the pathArray, optional, default false
  1002. * * closePath creates a seam between the first and the last points of each path of the path array, optional, default false
  1003. * * offset a positive integer, only used when pathArray contains a single path (offset = 10 means the point 1 is joined to the point 11), default rounded half size of the pathArray length
  1004. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1005. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1006. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1007. * * invertUV swaps in the U and V coordinates when applying a texture, optional, default false
  1008. * * uvs a linear array, of length 2 * number of vertices, of custom UV values, optional
  1009. * * colors a linear array, of length 4 * number of vertices, of custom color values, optional
  1010. * @returns the VertexData of the ribbon
  1011. * @deprecated use CreateRibbonVertexData instead
  1012. */
  1013. static CreateRibbon(options) {
  1014. throw _WarnImport("ribbonBuilder");
  1015. }
  1016. /**
  1017. * Creates the VertexData for a box
  1018. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1019. * * size sets the width, height and depth of the box to the value of size, optional default 1
  1020. * * width sets the width (x direction) of the box, overwrites the width set by size, optional, default size
  1021. * * height sets the height (y direction) of the box, overwrites the height set by size, optional, default size
  1022. * * depth sets the depth (z direction) of the box, overwrites the depth set by size, optional, default size
  1023. * * faceUV an array of 6 Vector4 elements used to set different images to each box side
  1024. * * faceColors an array of 6 Color3 elements used to set different colors to each box side
  1025. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1026. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1027. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1028. * @returns the VertexData of the box
  1029. * @deprecated Please use CreateBoxVertexData from the BoxBuilder file instead
  1030. */
  1031. static CreateBox(options) {
  1032. throw _WarnImport("boxBuilder");
  1033. }
  1034. /**
  1035. * Creates the VertexData for a tiled box
  1036. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1037. * * faceTiles sets the pattern, tile size and number of tiles for a face
  1038. * * faceUV an array of 6 Vector4 elements used to set different images to each box side
  1039. * * faceColors an array of 6 Color3 elements used to set different colors to each box side
  1040. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1041. * @param options.pattern
  1042. * @param options.width
  1043. * @param options.height
  1044. * @param options.depth
  1045. * @param options.tileSize
  1046. * @param options.tileWidth
  1047. * @param options.tileHeight
  1048. * @param options.alignHorizontal
  1049. * @param options.alignVertical
  1050. * @param options.faceUV
  1051. * @param options.faceColors
  1052. * @param options.sideOrientation
  1053. * @returns the VertexData of the box
  1054. * @deprecated Please use CreateTiledBoxVertexData instead
  1055. */
  1056. static CreateTiledBox(options) {
  1057. throw _WarnImport("tiledBoxBuilder");
  1058. }
  1059. /**
  1060. * Creates the VertexData for a tiled plane
  1061. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1062. * * pattern a limited pattern arrangement depending on the number
  1063. * * tileSize sets the width, height and depth of the tile to the value of size, optional default 1
  1064. * * tileWidth sets the width (x direction) of the tile, overwrites the width set by size, optional, default size
  1065. * * tileHeight sets the height (y direction) of the tile, overwrites the height set by size, optional, default size
  1066. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1067. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1068. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1069. * @returns the VertexData of the tiled plane
  1070. * @deprecated use CreateTiledPlaneVertexData instead
  1071. */
  1072. static CreateTiledPlane(options) {
  1073. throw _WarnImport("tiledPlaneBuilder");
  1074. }
  1075. /**
  1076. * Creates the VertexData for an ellipsoid, defaults to a sphere
  1077. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1078. * * segments sets the number of horizontal strips optional, default 32
  1079. * * diameter sets the axes dimensions, diameterX, diameterY and diameterZ to the value of diameter, optional default 1
  1080. * * diameterX sets the diameterX (x direction) of the ellipsoid, overwrites the diameterX set by diameter, optional, default diameter
  1081. * * diameterY sets the diameterY (y direction) of the ellipsoid, overwrites the diameterY set by diameter, optional, default diameter
  1082. * * diameterZ sets the diameterZ (z direction) of the ellipsoid, overwrites the diameterZ set by diameter, optional, default diameter
  1083. * * arc a number from 0 to 1, to create an unclosed ellipsoid based on the fraction of the circumference (latitude) given by the arc value, optional, default 1
  1084. * * slice a number from 0 to 1, to create an unclosed ellipsoid based on the fraction of the height (latitude) given by the arc value, optional, default 1
  1085. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1086. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1087. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1088. * @returns the VertexData of the ellipsoid
  1089. * @deprecated use CreateSphereVertexData instead
  1090. */
  1091. static CreateSphere(options) {
  1092. throw _WarnImport("sphereBuilder");
  1093. }
  1094. /**
  1095. * Creates the VertexData for a cylinder, cone or prism
  1096. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1097. * * height sets the height (y direction) of the cylinder, optional, default 2
  1098. * * diameterTop sets the diameter of the top of the cone, overwrites diameter, optional, default diameter
  1099. * * diameterBottom sets the diameter of the bottom of the cone, overwrites diameter, optional, default diameter
  1100. * * diameter sets the diameter of the top and bottom of the cone, optional default 1
  1101. * * tessellation the number of prism sides, 3 for a triangular prism, optional, default 24
  1102. * * subdivisions` the number of rings along the cylinder height, optional, default 1
  1103. * * arc a number from 0 to 1, to create an unclosed cylinder based on the fraction of the circumference given by the arc value, optional, default 1
  1104. * * faceColors an array of Color3 elements used to set different colors to the top, rings and bottom respectively
  1105. * * faceUV an array of Vector4 elements used to set different images to the top, rings and bottom respectively
  1106. * * hasRings when true makes each subdivision independently treated as a face for faceUV and faceColors, optional, default false
  1107. * * enclose when true closes an open cylinder by adding extra flat faces between the height axis and vertical edges, think cut cake
  1108. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1109. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1110. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1111. * @returns the VertexData of the cylinder, cone or prism
  1112. * @deprecated please use CreateCylinderVertexData instead
  1113. */
  1114. static CreateCylinder(options) {
  1115. throw _WarnImport("cylinderBuilder");
  1116. }
  1117. /**
  1118. * Creates the VertexData for a torus
  1119. * @param options an object used to set the following optional parameters for the box, required but can be empty
  1120. * * diameter the diameter of the torus, optional default 1
  1121. * * thickness the diameter of the tube forming the torus, optional default 0.5
  1122. * * tessellation the number of prism sides, 3 for a triangular prism, optional, default 24
  1123. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1124. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1125. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1126. * @returns the VertexData of the torus
  1127. * @deprecated use CreateTorusVertexData instead
  1128. */
  1129. static CreateTorus(options) {
  1130. throw _WarnImport("torusBuilder");
  1131. }
  1132. /**
  1133. * Creates the VertexData of the LineSystem
  1134. * @param options an object used to set the following optional parameters for the LineSystem, required but can be empty
  1135. * - lines an array of lines, each line being an array of successive Vector3
  1136. * - colors an array of line colors, each of the line colors being an array of successive Color4, one per line point
  1137. * @returns the VertexData of the LineSystem
  1138. * @deprecated use CreateLineSystemVertexData instead
  1139. */
  1140. static CreateLineSystem(options) {
  1141. throw _WarnImport("linesBuilder");
  1142. }
  1143. /**
  1144. * Create the VertexData for a DashedLines
  1145. * @param options an object used to set the following optional parameters for the DashedLines, required but can be empty
  1146. * - points an array successive Vector3
  1147. * - dashSize the size of the dashes relative to the dash number, optional, default 3
  1148. * - gapSize the size of the gap between two successive dashes relative to the dash number, optional, default 1
  1149. * - dashNb the intended total number of dashes, optional, default 200
  1150. * @returns the VertexData for the DashedLines
  1151. * @deprecated use CreateDashedLinesVertexData instead
  1152. */
  1153. static CreateDashedLines(options) {
  1154. throw _WarnImport("linesBuilder");
  1155. }
  1156. /**
  1157. * Creates the VertexData for a Ground
  1158. * @param options an object used to set the following optional parameters for the Ground, required but can be empty
  1159. * - width the width (x direction) of the ground, optional, default 1
  1160. * - height the height (z direction) of the ground, optional, default 1
  1161. * - subdivisions the number of subdivisions per side, optional, default 1
  1162. * @returns the VertexData of the Ground
  1163. * @deprecated Please use CreateGroundVertexData instead
  1164. */
  1165. static CreateGround(options) {
  1166. throw _WarnImport("groundBuilder");
  1167. }
  1168. /**
  1169. * Creates the VertexData for a TiledGround by subdividing the ground into tiles
  1170. * @param options an object used to set the following optional parameters for the Ground, required but can be empty
  1171. * * xmin the ground minimum X coordinate, optional, default -1
  1172. * * zmin the ground minimum Z coordinate, optional, default -1
  1173. * * xmax the ground maximum X coordinate, optional, default 1
  1174. * * zmax the ground maximum Z coordinate, optional, default 1
  1175. * * subdivisions a javascript object {w: positive integer, h: positive integer}, `w` and `h` are the numbers of subdivisions on the ground width and height creating 'tiles', default {w: 6, h: 6}
  1176. * * precision a javascript object {w: positive integer, h: positive integer}, `w` and `h` are the numbers of subdivisions on the tile width and height, default {w: 2, h: 2}
  1177. * @returns the VertexData of the TiledGround
  1178. * @deprecated use CreateTiledGroundVertexData instead
  1179. */
  1180. static CreateTiledGround(options) {
  1181. throw _WarnImport("groundBuilder");
  1182. }
  1183. /**
  1184. * Creates the VertexData of the Ground designed from a heightmap
  1185. * @param options an object used to set the following parameters for the Ground, required and provided by CreateGroundFromHeightMap
  1186. * * width the width (x direction) of the ground
  1187. * * height the height (z direction) of the ground
  1188. * * subdivisions the number of subdivisions per side
  1189. * * minHeight the minimum altitude on the ground, optional, default 0
  1190. * * maxHeight the maximum altitude on the ground, optional default 1
  1191. * * colorFilter the filter to apply to the image pixel colors to compute the height, optional Color3, default (0.3, 0.59, 0.11)
  1192. * * buffer the array holding the image color data
  1193. * * bufferWidth the width of image
  1194. * * bufferHeight the height of image
  1195. * * alphaFilter Remove any data where the alpha channel is below this value, defaults 0 (all data visible)
  1196. * @returns the VertexData of the Ground designed from a heightmap
  1197. * @deprecated use CreateGroundFromHeightMapVertexData instead
  1198. */
  1199. static CreateGroundFromHeightMap(options) {
  1200. throw _WarnImport("groundBuilder");
  1201. }
  1202. /**
  1203. * Creates the VertexData for a Plane
  1204. * @param options an object used to set the following optional parameters for the plane, required but can be empty
  1205. * * size sets the width and height of the plane to the value of size, optional default 1
  1206. * * width sets the width (x direction) of the plane, overwrites the width set by size, optional, default size
  1207. * * height sets the height (y direction) of the plane, overwrites the height set by size, optional, default size
  1208. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1209. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1210. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1211. * @returns the VertexData of the box
  1212. * @deprecated use CreatePlaneVertexData instead
  1213. */
  1214. static CreatePlane(options) {
  1215. throw _WarnImport("planeBuilder");
  1216. }
  1217. /**
  1218. * Creates the VertexData of the Disc or regular Polygon
  1219. * @param options an object used to set the following optional parameters for the disc, required but can be empty
  1220. * * radius the radius of the disc, optional default 0.5
  1221. * * tessellation the number of polygon sides, optional, default 64
  1222. * * arc a number from 0 to 1, to create an unclosed polygon based on the fraction of the circumference given by the arc value, optional, default 1
  1223. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1224. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1225. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1226. * @returns the VertexData of the box
  1227. * @deprecated use CreateDiscVertexData instead
  1228. */
  1229. static CreateDisc(options) {
  1230. throw _WarnImport("discBuilder");
  1231. }
  1232. /**
  1233. * Creates the VertexData for an irregular Polygon in the XoZ plane using a mesh built by polygonTriangulation.build()
  1234. * All parameters are provided by CreatePolygon as needed
  1235. * @param polygon a mesh built from polygonTriangulation.build()
  1236. * @param sideOrientation takes the values Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1237. * @param fUV an array of Vector4 elements used to set different images to the top, rings and bottom respectively
  1238. * @param fColors an array of Color3 elements used to set different colors to the top, rings and bottom respectively
  1239. * @param frontUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1240. * @param backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1241. * @param wrap a boolean, default false, when true and fUVs used texture is wrapped around all sides, when false texture is applied side
  1242. * @returns the VertexData of the Polygon
  1243. * @deprecated use CreatePolygonVertexData instead
  1244. */
  1245. static CreatePolygon(polygon, sideOrientation, fUV, fColors, frontUVs, backUVs, wrap) {
  1246. throw _WarnImport("polygonBuilder");
  1247. }
  1248. /**
  1249. * Creates the VertexData of the IcoSphere
  1250. * @param options an object used to set the following optional parameters for the IcoSphere, required but can be empty
  1251. * * radius the radius of the IcoSphere, optional default 1
  1252. * * radiusX allows stretching in the x direction, optional, default radius
  1253. * * radiusY allows stretching in the y direction, optional, default radius
  1254. * * radiusZ allows stretching in the z direction, optional, default radius
  1255. * * flat when true creates a flat shaded mesh, optional, default true
  1256. * * subdivisions increasing the subdivisions increases the number of faces, optional, default 4
  1257. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1258. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1259. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1260. * @returns the VertexData of the IcoSphere
  1261. * @deprecated use CreateIcoSphereVertexData instead
  1262. */
  1263. static CreateIcoSphere(options) {
  1264. throw _WarnImport("icoSphereBuilder");
  1265. }
  1266. // inspired from // http://stemkoski.github.io/Three.js/Polyhedra.html
  1267. /**
  1268. * Creates the VertexData for a Polyhedron
  1269. * @param options an object used to set the following optional parameters for the polyhedron, required but can be empty
  1270. * * type provided types are:
  1271. * * 0 : Tetrahedron, 1 : Octahedron, 2 : Dodecahedron, 3 : Icosahedron, 4 : Rhombicuboctahedron, 5 : Triangular Prism, 6 : Pentagonal Prism, 7 : Hexagonal Prism, 8 : Square Pyramid (J1)
  1272. * * 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)
  1273. * * size the size of the IcoSphere, optional default 1
  1274. * * sizeX allows stretching in the x direction, optional, default size
  1275. * * sizeY allows stretching in the y direction, optional, default size
  1276. * * sizeZ allows stretching in the z direction, optional, default size
  1277. * * custom a number that overwrites the type to create from an extended set of polyhedron from https://www.babylonjs-playground.com/#21QRSK#15 with minimised editor
  1278. * * faceUV an array of Vector4 elements used to set different images to the top, rings and bottom respectively
  1279. * * faceColors an array of Color3 elements used to set different colors to the top, rings and bottom respectively
  1280. * * flat when true creates a flat shaded mesh, optional, default true
  1281. * * subdivisions increasing the subdivisions increases the number of faces, optional, default 4
  1282. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1283. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1284. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1285. * @returns the VertexData of the Polyhedron
  1286. * @deprecated use CreatePolyhedronVertexData instead
  1287. */
  1288. static CreatePolyhedron(options) {
  1289. throw _WarnImport("polyhedronBuilder");
  1290. }
  1291. /**
  1292. * Creates the VertexData for a Capsule, inspired from https://github.com/maximeq/three-js-capsule-geometry/blob/master/src/CapsuleBufferGeometry.js
  1293. * @param options an object used to set the following optional parameters for the capsule, required but can be empty
  1294. * @returns the VertexData of the Capsule
  1295. * @deprecated Please use CreateCapsuleVertexData from the capsuleBuilder file instead
  1296. */
  1297. static CreateCapsule(options = {
  1298. orientation: Vector3.Up(),
  1299. subdivisions: 2,
  1300. tessellation: 16,
  1301. height: 1,
  1302. radius: 0.25,
  1303. capSubdivisions: 6,
  1304. }) {
  1305. throw _WarnImport("capsuleBuilder");
  1306. }
  1307. // based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  1308. /**
  1309. * Creates the VertexData for a TorusKnot
  1310. * @param options an object used to set the following optional parameters for the TorusKnot, required but can be empty
  1311. * * radius the radius of the torus knot, optional, default 2
  1312. * * tube the thickness of the tube, optional, default 0.5
  1313. * * radialSegments the number of sides on each tube segments, optional, default 32
  1314. * * tubularSegments the number of tubes to decompose the knot into, optional, default 32
  1315. * * p the number of windings around the z axis, optional, default 2
  1316. * * q the number of windings around the x axis, optional, default 3
  1317. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  1318. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  1319. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  1320. * @returns the VertexData of the Torus Knot
  1321. * @deprecated use CreateTorusKnotVertexData instead
  1322. */
  1323. static CreateTorusKnot(options) {
  1324. throw _WarnImport("torusKnotBuilder");
  1325. }
  1326. // Tools
  1327. /**
  1328. * Compute normals for given positions and indices
  1329. * @param positions an array of vertex positions, [...., x, y, z, ......]
  1330. * @param indices an array of indices in groups of three for each triangular facet, [...., i, j, k, ......]
  1331. * @param normals an array of vertex normals, [...., x, y, z, ......]
  1332. * @param options an object used to set the following optional parameters for the TorusKnot, optional
  1333. * * facetNormals : optional array of facet normals (vector3)
  1334. * * facetPositions : optional array of facet positions (vector3)
  1335. * * facetPartitioning : optional partitioning array. facetPositions is required for facetPartitioning computation
  1336. * * ratio : optional partitioning ratio / bounding box, required for facetPartitioning computation
  1337. * * bInfo : optional bounding info, required for facetPartitioning computation
  1338. * * bbSize : optional bounding box size data, required for facetPartitioning computation
  1339. * * subDiv : optional partitioning data about subdivisions on each axis (int), required for facetPartitioning computation
  1340. * * useRightHandedSystem: optional boolean to for right handed system computation
  1341. * * depthSort : optional boolean to enable the facet depth sort computation
  1342. * * distanceTo : optional Vector3 to compute the facet depth from this location
  1343. * * depthSortedFacets : optional array of depthSortedFacets to store the facet distances from the reference location
  1344. */
  1345. static ComputeNormals(positions, indices, normals, options) {
  1346. // temporary scalar variables
  1347. let index = 0; // facet index
  1348. let p1p2x = 0.0; // p1p2 vector x coordinate
  1349. let p1p2y = 0.0; // p1p2 vector y coordinate
  1350. let p1p2z = 0.0; // p1p2 vector z coordinate
  1351. let p3p2x = 0.0; // p3p2 vector x coordinate
  1352. let p3p2y = 0.0; // p3p2 vector y coordinate
  1353. let p3p2z = 0.0; // p3p2 vector z coordinate
  1354. let faceNormalx = 0.0; // facet normal x coordinate
  1355. let faceNormaly = 0.0; // facet normal y coordinate
  1356. let faceNormalz = 0.0; // facet normal z coordinate
  1357. let length = 0.0; // facet normal length before normalization
  1358. let v1x = 0; // vector1 x index in the positions array
  1359. let v1y = 0; // vector1 y index in the positions array
  1360. let v1z = 0; // vector1 z index in the positions array
  1361. let v2x = 0; // vector2 x index in the positions array
  1362. let v2y = 0; // vector2 y index in the positions array
  1363. let v2z = 0; // vector2 z index in the positions array
  1364. let v3x = 0; // vector3 x index in the positions array
  1365. let v3y = 0; // vector3 y index in the positions array
  1366. let v3z = 0; // vector3 z index in the positions array
  1367. let computeFacetNormals = false;
  1368. let computeFacetPositions = false;
  1369. let computeFacetPartitioning = false;
  1370. let computeDepthSort = false;
  1371. let faceNormalSign = 1;
  1372. let ratio = 0;
  1373. let distanceTo = null;
  1374. if (options) {
  1375. computeFacetNormals = options.facetNormals ? true : false;
  1376. computeFacetPositions = options.facetPositions ? true : false;
  1377. computeFacetPartitioning = options.facetPartitioning ? true : false;
  1378. faceNormalSign = options.useRightHandedSystem === true ? -1 : 1;
  1379. ratio = options.ratio || 0;
  1380. computeDepthSort = options.depthSort ? true : false;
  1381. distanceTo = options.distanceTo;
  1382. if (computeDepthSort) {
  1383. if (distanceTo === undefined) {
  1384. distanceTo = Vector3.Zero();
  1385. }
  1386. }
  1387. }
  1388. // facetPartitioning reinit if needed
  1389. let xSubRatio = 0;
  1390. let ySubRatio = 0;
  1391. let zSubRatio = 0;
  1392. let subSq = 0;
  1393. if (computeFacetPartitioning && options && options.bbSize) {
  1394. //let bbSizeMax = options.bbSize.x > options.bbSize.y ? options.bbSize.x : options.bbSize.y;
  1395. //bbSizeMax = bbSizeMax > options.bbSize.z ? bbSizeMax : options.bbSize.z;
  1396. xSubRatio = (options.subDiv.X * ratio) / options.bbSize.x;
  1397. ySubRatio = (options.subDiv.Y * ratio) / options.bbSize.y;
  1398. zSubRatio = (options.subDiv.Z * ratio) / options.bbSize.z;
  1399. subSq = options.subDiv.max * options.subDiv.max;
  1400. options.facetPartitioning.length = 0;
  1401. }
  1402. // reset the normals
  1403. for (index = 0; index < positions.length; index++) {
  1404. normals[index] = 0.0;
  1405. }
  1406. // Loop : 1 indice triplet = 1 facet
  1407. const nbFaces = (indices.length / 3) | 0;
  1408. for (index = 0; index < nbFaces; index++) {
  1409. // get the indexes of the coordinates of each vertex of the facet
  1410. v1x = indices[index * 3] * 3;
  1411. v1y = v1x + 1;
  1412. v1z = v1x + 2;
  1413. v2x = indices[index * 3 + 1] * 3;
  1414. v2y = v2x + 1;
  1415. v2z = v2x + 2;
  1416. v3x = indices[index * 3 + 2] * 3;
  1417. v3y = v3x + 1;
  1418. v3z = v3x + 2;
  1419. p1p2x = positions[v1x] - positions[v2x]; // compute two vectors per facet : p1p2 and p3p2
  1420. p1p2y = positions[v1y] - positions[v2y];
  1421. p1p2z = positions[v1z] - positions[v2z];
  1422. p3p2x = positions[v3x] - positions[v2x];
  1423. p3p2y = positions[v3y] - positions[v2y];
  1424. p3p2z = positions[v3z] - positions[v2z];
  1425. // compute the face normal with the cross product
  1426. faceNormalx = faceNormalSign * (p1p2y * p3p2z - p1p2z * p3p2y);
  1427. faceNormaly = faceNormalSign * (p1p2z * p3p2x - p1p2x * p3p2z);
  1428. faceNormalz = faceNormalSign * (p1p2x * p3p2y - p1p2y * p3p2x);
  1429. // normalize this normal and store it in the array facetData
  1430. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  1431. length = length === 0 ? 1.0 : length;
  1432. faceNormalx /= length;
  1433. faceNormaly /= length;
  1434. faceNormalz /= length;
  1435. if (computeFacetNormals && options) {
  1436. options.facetNormals[index].x = faceNormalx;
  1437. options.facetNormals[index].y = faceNormaly;
  1438. options.facetNormals[index].z = faceNormalz;
  1439. }
  1440. if (computeFacetPositions && options) {
  1441. // compute and the facet barycenter coordinates in the array facetPositions
  1442. options.facetPositions[index].x = (positions[v1x] + positions[v2x] + positions[v3x]) / 3.0;
  1443. options.facetPositions[index].y = (positions[v1y] + positions[v2y] + positions[v3y]) / 3.0;
  1444. options.facetPositions[index].z = (positions[v1z] + positions[v2z] + positions[v3z]) / 3.0;
  1445. }
  1446. if (computeFacetPartitioning && options) {
  1447. // store the facet indexes in arrays in the main facetPartitioning array :
  1448. // compute each facet vertex (+ facet barycenter) index in the partiniong array
  1449. const ox = Math.floor((options.facetPositions[index].x - options.bInfo.minimum.x * ratio) * xSubRatio);
  1450. const oy = Math.floor((options.facetPositions[index].y - options.bInfo.minimum.y * ratio) * ySubRatio);
  1451. const oz = Math.floor((options.facetPositions[index].z - options.bInfo.minimum.z * ratio) * zSubRatio);
  1452. const b1x = Math.floor((positions[v1x] - options.bInfo.minimum.x * ratio) * xSubRatio);
  1453. const b1y = Math.floor((positions[v1y] - options.bInfo.minimum.y * ratio) * ySubRatio);
  1454. const b1z = Math.floor((positions[v1z] - options.bInfo.minimum.z * ratio) * zSubRatio);
  1455. const b2x = Math.floor((positions[v2x] - options.bInfo.minimum.x * ratio) * xSubRatio);
  1456. const b2y = Math.floor((positions[v2y] - options.bInfo.minimum.y * ratio) * ySubRatio);
  1457. const b2z = Math.floor((positions[v2z] - options.bInfo.minimum.z * ratio) * zSubRatio);
  1458. const b3x = Math.floor((positions[v3x] - options.bInfo.minimum.x * ratio) * xSubRatio);
  1459. const b3y = Math.floor((positions[v3y] - options.bInfo.minimum.y * ratio) * ySubRatio);
  1460. const b3z = Math.floor((positions[v3z] - options.bInfo.minimum.z * ratio) * zSubRatio);
  1461. const block_idx_v1 = b1x + options.subDiv.max * b1y + subSq * b1z;
  1462. const block_idx_v2 = b2x + options.subDiv.max * b2y + subSq * b2z;
  1463. const block_idx_v3 = b3x + options.subDiv.max * b3y + subSq * b3z;
  1464. const block_idx_o = ox + options.subDiv.max * oy + subSq * oz;
  1465. options.facetPartitioning[block_idx_o] = options.facetPartitioning[block_idx_o] ? options.facetPartitioning[block_idx_o] : new Array();
  1466. options.facetPartitioning[block_idx_v1] = options.facetPartitioning[block_idx_v1] ? options.facetPartitioning[block_idx_v1] : new Array();
  1467. options.facetPartitioning[block_idx_v2] = options.facetPartitioning[block_idx_v2] ? options.facetPartitioning[block_idx_v2] : new Array();
  1468. options.facetPartitioning[block_idx_v3] = options.facetPartitioning[block_idx_v3] ? options.facetPartitioning[block_idx_v3] : new Array();
  1469. // push each facet index in each block containing the vertex
  1470. options.facetPartitioning[block_idx_v1].push(index);
  1471. if (block_idx_v2 != block_idx_v1) {
  1472. options.facetPartitioning[block_idx_v2].push(index);
  1473. }
  1474. if (!(block_idx_v3 == block_idx_v2 || block_idx_v3 == block_idx_v1)) {
  1475. options.facetPartitioning[block_idx_v3].push(index);
  1476. }
  1477. if (!(block_idx_o == block_idx_v1 || block_idx_o == block_idx_v2 || block_idx_o == block_idx_v3)) {
  1478. options.facetPartitioning[block_idx_o].push(index);
  1479. }
  1480. }
  1481. if (computeDepthSort && options && options.facetPositions) {
  1482. const dsf = options.depthSortedFacets[index];
  1483. dsf.ind = index * 3;
  1484. dsf.sqDistance = Vector3.DistanceSquared(options.facetPositions[index], distanceTo);
  1485. }
  1486. // compute the normals anyway
  1487. normals[v1x] += faceNormalx; // accumulate all the normals per face
  1488. normals[v1y] += faceNormaly;
  1489. normals[v1z] += faceNormalz;
  1490. normals[v2x] += faceNormalx;
  1491. normals[v2y] += faceNormaly;
  1492. normals[v2z] += faceNormalz;
  1493. normals[v3x] += faceNormalx;
  1494. normals[v3y] += faceNormaly;
  1495. normals[v3z] += faceNormalz;
  1496. }
  1497. // last normalization of each normal
  1498. for (index = 0; index < normals.length / 3; index++) {
  1499. faceNormalx = normals[index * 3];
  1500. faceNormaly = normals[index * 3 + 1];
  1501. faceNormalz = normals[index * 3 + 2];
  1502. length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
  1503. length = length === 0 ? 1.0 : length;
  1504. faceNormalx /= length;
  1505. faceNormaly /= length;
  1506. faceNormalz /= length;
  1507. normals[index * 3] = faceNormalx;
  1508. normals[index * 3 + 1] = faceNormaly;
  1509. normals[index * 3 + 2] = faceNormalz;
  1510. }
  1511. }
  1512. /**
  1513. * @internal
  1514. */
  1515. static _ComputeSides(sideOrientation, positions, indices, normals, uvs, frontUVs, backUVs) {
  1516. const li = indices.length;
  1517. const ln = normals.length;
  1518. let i;
  1519. let n;
  1520. sideOrientation = sideOrientation || VertexData.DEFAULTSIDE;
  1521. switch (sideOrientation) {
  1522. case VertexData.FRONTSIDE:
  1523. // nothing changed
  1524. break;
  1525. case VertexData.BACKSIDE:
  1526. // indices
  1527. for (i = 0; i < li; i += 3) {
  1528. const tmp = indices[i];
  1529. indices[i] = indices[i + 2];
  1530. indices[i + 2] = tmp;
  1531. }
  1532. // normals
  1533. for (n = 0; n < ln; n++) {
  1534. normals[n] = -normals[n];
  1535. }
  1536. break;
  1537. case VertexData.DOUBLESIDE: {
  1538. // positions
  1539. const lp = positions.length;
  1540. const l = lp / 3;
  1541. for (let p = 0; p < lp; p++) {
  1542. positions[lp + p] = positions[p];
  1543. }
  1544. // indices
  1545. for (i = 0; i < li; i += 3) {
  1546. indices[i + li] = indices[i + 2] + l;
  1547. indices[i + 1 + li] = indices[i + 1] + l;
  1548. indices[i + 2 + li] = indices[i] + l;
  1549. }
  1550. // normals
  1551. for (n = 0; n < ln; n++) {
  1552. normals[ln + n] = -normals[n];
  1553. }
  1554. // uvs
  1555. const lu = uvs.length;
  1556. let u = 0;
  1557. for (u = 0; u < lu; u++) {
  1558. uvs[u + lu] = uvs[u];
  1559. }
  1560. frontUVs = frontUVs ? frontUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
  1561. backUVs = backUVs ? backUVs : new Vector4(0.0, 0.0, 1.0, 1.0);
  1562. u = 0;
  1563. for (i = 0; i < lu / 2; i++) {
  1564. uvs[u] = frontUVs.x + (frontUVs.z - frontUVs.x) * uvs[u];
  1565. uvs[u + 1] = frontUVs.y + (frontUVs.w - frontUVs.y) * uvs[u + 1];
  1566. uvs[u + lu] = backUVs.x + (backUVs.z - backUVs.x) * uvs[u + lu];
  1567. uvs[u + lu + 1] = backUVs.y + (backUVs.w - backUVs.y) * uvs[u + lu + 1];
  1568. u += 2;
  1569. }
  1570. break;
  1571. }
  1572. }
  1573. }
  1574. /**
  1575. * Creates a VertexData from serialized data
  1576. * @param parsedVertexData the parsed data from an imported file
  1577. * @returns a VertexData
  1578. */
  1579. static Parse(parsedVertexData) {
  1580. const vertexData = new VertexData();
  1581. // positions
  1582. const positions = parsedVertexData.positions;
  1583. if (positions) {
  1584. vertexData.set(positions, VertexBuffer.PositionKind);
  1585. }
  1586. // normals
  1587. const normals = parsedVertexData.normals;
  1588. if (normals) {
  1589. vertexData.set(normals, VertexBuffer.NormalKind);
  1590. }
  1591. // tangents
  1592. const tangents = parsedVertexData.tangents;
  1593. if (tangents) {
  1594. vertexData.set(tangents, VertexBuffer.TangentKind);
  1595. }
  1596. // uvs
  1597. const uvs = parsedVertexData.uvs;
  1598. if (uvs) {
  1599. vertexData.set(uvs, VertexBuffer.UVKind);
  1600. }
  1601. // uv2s
  1602. const uvs2 = parsedVertexData.uvs2;
  1603. if (uvs2) {
  1604. vertexData.set(uvs2, VertexBuffer.UV2Kind);
  1605. }
  1606. // uv3s
  1607. const uvs3 = parsedVertexData.uvs3;
  1608. if (uvs3) {
  1609. vertexData.set(uvs3, VertexBuffer.UV3Kind);
  1610. }
  1611. // uv4s
  1612. const uvs4 = parsedVertexData.uvs4;
  1613. if (uvs4) {
  1614. vertexData.set(uvs4, VertexBuffer.UV4Kind);
  1615. }
  1616. // uv5s
  1617. const uvs5 = parsedVertexData.uvs5;
  1618. if (uvs5) {
  1619. vertexData.set(uvs5, VertexBuffer.UV5Kind);
  1620. }
  1621. // uv6s
  1622. const uvs6 = parsedVertexData.uvs6;
  1623. if (uvs6) {
  1624. vertexData.set(uvs6, VertexBuffer.UV6Kind);
  1625. }
  1626. // colors
  1627. const colors = parsedVertexData.colors;
  1628. if (colors) {
  1629. vertexData.set(Color4.CheckColors4(colors, positions.length / 3), VertexBuffer.ColorKind);
  1630. if (parsedVertexData.hasVertexAlpha !== undefined) {
  1631. vertexData.hasVertexAlpha = parsedVertexData.hasVertexAlpha;
  1632. }
  1633. }
  1634. // matricesIndices
  1635. const matricesIndices = parsedVertexData.matricesIndices;
  1636. if (matricesIndices) {
  1637. vertexData.set(matricesIndices, VertexBuffer.MatricesIndicesKind);
  1638. }
  1639. // matricesWeights
  1640. const matricesWeights = parsedVertexData.matricesWeights;
  1641. if (matricesWeights) {
  1642. vertexData.set(matricesWeights, VertexBuffer.MatricesWeightsKind);
  1643. }
  1644. // indices
  1645. const indices = parsedVertexData.indices;
  1646. if (indices) {
  1647. vertexData.indices = indices;
  1648. }
  1649. // MaterialInfos
  1650. const materialInfos = parsedVertexData.materialInfos;
  1651. if (materialInfos) {
  1652. vertexData.materialInfos = [];
  1653. for (const materialInfoFromJSON of materialInfos) {
  1654. const materialInfo = new VertexDataMaterialInfo();
  1655. materialInfo.indexCount = materialInfoFromJSON.indexCount;
  1656. materialInfo.indexStart = materialInfoFromJSON.indexStart;
  1657. materialInfo.verticesCount = materialInfoFromJSON.verticesCount;
  1658. materialInfo.verticesStart = materialInfoFromJSON.verticesStart;
  1659. materialInfo.materialIndex = materialInfoFromJSON.materialIndex;
  1660. vertexData.materialInfos.push(materialInfo);
  1661. }
  1662. }
  1663. return vertexData;
  1664. }
  1665. /**
  1666. * Applies VertexData created from the imported parameters to the geometry
  1667. * @param parsedVertexData the parsed data from an imported file
  1668. * @param geometry the geometry to apply the VertexData to
  1669. */
  1670. static ImportVertexData(parsedVertexData, geometry) {
  1671. const vertexData = VertexData.Parse(parsedVertexData);
  1672. geometry.setAllVerticesData(vertexData, parsedVertexData.updatable);
  1673. }
  1674. }
  1675. /**
  1676. * Mesh side orientation : usually the external or front surface
  1677. */
  1678. VertexData.FRONTSIDE = 0;
  1679. /**
  1680. * Mesh side orientation : usually the internal or back surface
  1681. */
  1682. VertexData.BACKSIDE = 1;
  1683. /**
  1684. * Mesh side orientation : both internal and external or front and back surfaces
  1685. */
  1686. VertexData.DOUBLESIDE = 2;
  1687. /**
  1688. * Mesh side orientation : by default, `FRONTSIDE`
  1689. */
  1690. VertexData.DEFAULTSIDE = 0;
  1691. VertexData._UniqueIDGenerator = 0;
  1692. __decorate([
  1693. nativeOverride.filter((...[coordinates]) => !Array.isArray(coordinates))
  1694. ], VertexData, "_TransformVector3Coordinates", null);
  1695. __decorate([
  1696. nativeOverride.filter((...[normals]) => !Array.isArray(normals))
  1697. ], VertexData, "_TransformVector3Normals", null);
  1698. __decorate([
  1699. nativeOverride.filter((...[normals]) => !Array.isArray(normals))
  1700. ], VertexData, "_TransformVector4Normals", null);
  1701. __decorate([
  1702. nativeOverride.filter((...[indices]) => !Array.isArray(indices))
  1703. ], VertexData, "_FlipFaces", null);
  1704. //# sourceMappingURL=mesh.vertexData.js.map