babylon.mesh.vertexData.ts 124 KB

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