babylon.geometry.ts 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. module BABYLON {
  2. export class Geometry implements IGetSetVerticesData {
  3. // Members
  4. public id: string;
  5. public delayLoadState = Engine.DELAYLOADSTATE_NONE;
  6. public delayLoadingFile: string;
  7. public onGeometryUpdated: (geometry: Geometry, kind?: string) => void;
  8. // Private
  9. private _scene: Scene;
  10. private _engine: Engine;
  11. private _meshes: Mesh[];
  12. private _totalVertices = 0;
  13. private _indices: number[] | Int32Array;
  14. private _vertexBuffers: { [key: string]: VertexBuffer; };
  15. private _isDisposed = false;
  16. private _extend: { minimum: Vector3, maximum: Vector3 };
  17. private _boundingBias: Vector2;
  18. public _delayInfo; //ANY
  19. private _indexBuffer: WebGLBuffer;
  20. public _boundingInfo: BoundingInfo;
  21. public _delayLoadingFunction: (any: any, geometry: Geometry) => void;
  22. public _softwareSkinningRenderId: number;
  23. /**
  24. * The Bias Vector to apply on the bounding elements (box/sphere), the max extend is computed as v += v * bias.x + bias.y, the min is computed as v -= v * bias.x + bias.y
  25. * @returns The Bias Vector
  26. */
  27. public get boundingBias(): Vector2 {
  28. return this._boundingBias;
  29. }
  30. public set boundingBias(value: Vector2) {
  31. if (this._boundingBias && this._boundingBias.equals(value)) {
  32. return;
  33. }
  34. this._boundingBias = value.clone();
  35. this.updateBoundingInfo(true, null);
  36. }
  37. constructor(id: string, scene: Scene, vertexData?: VertexData, updatable?: boolean, mesh?: Mesh) {
  38. this.id = id;
  39. this._engine = scene.getEngine();
  40. this._meshes = [];
  41. this._scene = scene;
  42. //Init vertex buffer cache
  43. this._vertexBuffers = {};
  44. this._indices = [];
  45. // vertexData
  46. if (vertexData) {
  47. this.setAllVerticesData(vertexData, updatable);
  48. }
  49. else {
  50. this._totalVertices = 0;
  51. this._indices = [];
  52. }
  53. // applyToMesh
  54. if (mesh) {
  55. if (mesh instanceof LinesMesh) {
  56. this.boundingBias = new Vector2(0, mesh.intersectionThreshold);
  57. this.updateExtend();
  58. }
  59. this.applyToMesh(mesh);
  60. mesh.computeWorldMatrix(true);
  61. }
  62. }
  63. public get extend(): { minimum: Vector3, maximum: Vector3 } {
  64. return this._extend;
  65. }
  66. public getScene(): Scene {
  67. return this._scene;
  68. }
  69. public getEngine(): Engine {
  70. return this._engine;
  71. }
  72. public isReady(): boolean {
  73. return this.delayLoadState === Engine.DELAYLOADSTATE_LOADED || this.delayLoadState === Engine.DELAYLOADSTATE_NONE;
  74. }
  75. public setAllVerticesData(vertexData: VertexData, updatable?: boolean): void {
  76. vertexData.applyToGeometry(this, updatable);
  77. this.notifyUpdate();
  78. }
  79. public setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean, stride?: number): void {
  80. var buffer = new VertexBuffer(this._engine, data, kind, updatable, this._meshes.length === 0, stride);
  81. this.setVerticesBuffer(buffer);
  82. }
  83. public setVerticesBuffer(buffer: VertexBuffer): void {
  84. var kind = buffer.getKind();
  85. if (this._vertexBuffers[kind]) {
  86. this._vertexBuffers[kind].dispose();
  87. }
  88. this._vertexBuffers[kind] = buffer;
  89. if (kind === VertexBuffer.PositionKind) {
  90. var data = buffer.getData();
  91. var stride = buffer.getStrideSize();
  92. this._totalVertices = data.length / stride;
  93. this.updateExtend(data, stride);
  94. var meshes = this._meshes;
  95. var numOfMeshes = meshes.length;
  96. for (var index = 0; index < numOfMeshes; index++) {
  97. var mesh = meshes[index];
  98. mesh._resetPointsArrayCache();
  99. mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
  100. mesh._createGlobalSubMesh();
  101. mesh.computeWorldMatrix(true);
  102. }
  103. }
  104. this.notifyUpdate(kind);
  105. }
  106. public updateVerticesDataDirectly(kind: string, data: Float32Array, offset: number): void {
  107. var vertexBuffer = this.getVertexBuffer(kind);
  108. if (!vertexBuffer) {
  109. return;
  110. }
  111. vertexBuffer.updateDirectly(data, offset);
  112. this.notifyUpdate(kind);
  113. }
  114. public updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean): void {
  115. var vertexBuffer = this.getVertexBuffer(kind);
  116. if (!vertexBuffer) {
  117. return;
  118. }
  119. vertexBuffer.update(data);
  120. if (kind === VertexBuffer.PositionKind) {
  121. var stride = vertexBuffer.getStrideSize();
  122. this._totalVertices = data.length / stride;
  123. this.updateBoundingInfo(updateExtends, data);
  124. }
  125. this.notifyUpdate(kind);
  126. }
  127. private updateBoundingInfo(updateExtends: boolean, data: number[] | Float32Array) {
  128. if (updateExtends) {
  129. this.updateExtend(data);
  130. }
  131. var meshes = this._meshes;
  132. var numOfMeshes = meshes.length;
  133. for (var index = 0; index < numOfMeshes; index++) {
  134. var mesh = meshes[index];
  135. mesh._resetPointsArrayCache();
  136. if (updateExtends) {
  137. mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
  138. for (var subIndex = 0; subIndex < mesh.subMeshes.length; subIndex++) {
  139. var subMesh = mesh.subMeshes[subIndex];
  140. subMesh.refreshBoundingInfo();
  141. }
  142. }
  143. }
  144. }
  145. public getTotalVertices(): number {
  146. if (!this.isReady()) {
  147. return 0;
  148. }
  149. return this._totalVertices;
  150. }
  151. public getVerticesData(kind: string, copyWhenShared?: boolean): number[] | Float32Array {
  152. var vertexBuffer = this.getVertexBuffer(kind);
  153. if (!vertexBuffer) {
  154. return null;
  155. }
  156. var orig = vertexBuffer.getData();
  157. if (!copyWhenShared || this._meshes.length === 1) {
  158. return orig;
  159. } else {
  160. var len = orig.length;
  161. var copy = [];
  162. for (var i = 0; i < len; i++) {
  163. copy.push(orig[i]);
  164. }
  165. return copy;
  166. }
  167. }
  168. public getVertexBuffer(kind: string): VertexBuffer {
  169. if (!this.isReady()) {
  170. return null;
  171. }
  172. return this._vertexBuffers[kind];
  173. }
  174. public getVertexBuffers(): { [key: string]: VertexBuffer; } {
  175. if (!this.isReady()) {
  176. return null;
  177. }
  178. return this._vertexBuffers;
  179. }
  180. public isVerticesDataPresent(kind: string): boolean {
  181. if (!this._vertexBuffers) {
  182. if (this._delayInfo) {
  183. return this._delayInfo.indexOf(kind) !== -1;
  184. }
  185. return false;
  186. }
  187. return this._vertexBuffers[kind] !== undefined;
  188. }
  189. public getVerticesDataKinds(): string[] {
  190. var result = [];
  191. var kind;
  192. if (!this._vertexBuffers && this._delayInfo) {
  193. for (kind in this._delayInfo) {
  194. result.push(kind);
  195. }
  196. } else {
  197. for (kind in this._vertexBuffers) {
  198. result.push(kind);
  199. }
  200. }
  201. return result;
  202. }
  203. public setIndices(indices: number[] | Int32Array, totalVertices?: number): void {
  204. if (this._indexBuffer) {
  205. this._engine._releaseBuffer(this._indexBuffer);
  206. }
  207. this._indices = indices;
  208. if (this._meshes.length !== 0 && this._indices) {
  209. this._indexBuffer = this._engine.createIndexBuffer(this._indices);
  210. }
  211. if (totalVertices !== undefined) {
  212. this._totalVertices = totalVertices;
  213. }
  214. var meshes = this._meshes;
  215. var numOfMeshes = meshes.length;
  216. for (var index = 0; index < numOfMeshes; index++) {
  217. meshes[index]._createGlobalSubMesh();
  218. }
  219. this.notifyUpdate();
  220. }
  221. public getTotalIndices(): number {
  222. if (!this.isReady()) {
  223. return 0;
  224. }
  225. return this._indices.length;
  226. }
  227. public getIndices(copyWhenShared?: boolean): number[] | Int32Array {
  228. if (!this.isReady()) {
  229. return null;
  230. }
  231. var orig = this._indices;
  232. if (!copyWhenShared || this._meshes.length === 1) {
  233. return orig;
  234. } else {
  235. var len = orig.length;
  236. var copy = [];
  237. for (var i = 0; i < len; i++) {
  238. copy.push(orig[i]);
  239. }
  240. return copy;
  241. }
  242. }
  243. public getIndexBuffer(): WebGLBuffer {
  244. if (!this.isReady()) {
  245. return null;
  246. }
  247. return this._indexBuffer;
  248. }
  249. public releaseForMesh(mesh: Mesh, shouldDispose?: boolean): void {
  250. var meshes = this._meshes;
  251. var index = meshes.indexOf(mesh);
  252. if (index === -1) {
  253. return;
  254. }
  255. for (var kind in this._vertexBuffers) {
  256. this._vertexBuffers[kind].dispose();
  257. }
  258. if (this._indexBuffer && this._engine._releaseBuffer(this._indexBuffer)) {
  259. this._indexBuffer = null;
  260. }
  261. meshes.splice(index, 1);
  262. mesh._geometry = null;
  263. if (meshes.length === 0 && shouldDispose) {
  264. this.dispose();
  265. }
  266. }
  267. public applyToMesh(mesh: Mesh): void {
  268. if (mesh._geometry === this) {
  269. return;
  270. }
  271. var previousGeometry = mesh._geometry;
  272. if (previousGeometry) {
  273. previousGeometry.releaseForMesh(mesh);
  274. }
  275. var meshes = this._meshes;
  276. // must be done before setting vertexBuffers because of mesh._createGlobalSubMesh()
  277. mesh._geometry = this;
  278. this._scene.pushGeometry(this);
  279. meshes.push(mesh);
  280. if (this.isReady()) {
  281. this._applyToMesh(mesh);
  282. }
  283. else {
  284. mesh._boundingInfo = this._boundingInfo;
  285. }
  286. }
  287. private updateExtend(data = null, stride? : number) {
  288. if (!data) {
  289. data = this._vertexBuffers[VertexBuffer.PositionKind].getData();
  290. }
  291. this._extend = Tools.ExtractMinAndMax(data, 0, this._totalVertices, this.boundingBias, stride);
  292. }
  293. private _applyToMesh(mesh: Mesh): void {
  294. var numOfMeshes = this._meshes.length;
  295. // vertexBuffers
  296. for (var kind in this._vertexBuffers) {
  297. if (numOfMeshes === 1) {
  298. this._vertexBuffers[kind].create();
  299. }
  300. this._vertexBuffers[kind].getBuffer().references = numOfMeshes;
  301. if (kind === VertexBuffer.PositionKind) {
  302. mesh._resetPointsArrayCache();
  303. if (!this._extend) {
  304. this.updateExtend(this._vertexBuffers[kind].getData());
  305. }
  306. mesh._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
  307. mesh._createGlobalSubMesh();
  308. //bounding info was just created again, world matrix should be applied again.
  309. mesh._updateBoundingInfo();
  310. }
  311. }
  312. // indexBuffer
  313. if (numOfMeshes === 1 && this._indices && this._indices.length > 0) {
  314. this._indexBuffer = this._engine.createIndexBuffer(this._indices);
  315. }
  316. if (this._indexBuffer) {
  317. this._indexBuffer.references = numOfMeshes;
  318. }
  319. }
  320. private notifyUpdate(kind?: string) {
  321. if (this.onGeometryUpdated) {
  322. this.onGeometryUpdated(this, kind);
  323. }
  324. }
  325. public load(scene: Scene, onLoaded?: () => void): void {
  326. if (this.delayLoadState === Engine.DELAYLOADSTATE_LOADING) {
  327. return;
  328. }
  329. if (this.isReady()) {
  330. if (onLoaded) {
  331. onLoaded();
  332. }
  333. return;
  334. }
  335. this.delayLoadState = Engine.DELAYLOADSTATE_LOADING;
  336. this._queueLoad(scene, onLoaded);
  337. }
  338. private _queueLoad(scene: Scene, onLoaded?: () => void): void {
  339. scene._addPendingData(this);
  340. Tools.LoadFile(this.delayLoadingFile, data => {
  341. this._delayLoadingFunction(JSON.parse(data), this);
  342. this.delayLoadState = Engine.DELAYLOADSTATE_LOADED;
  343. this._delayInfo = [];
  344. scene._removePendingData(this);
  345. var meshes = this._meshes;
  346. var numOfMeshes = meshes.length;
  347. for (var index = 0; index < numOfMeshes; index++) {
  348. this._applyToMesh(meshes[index]);
  349. }
  350. if (onLoaded) {
  351. onLoaded();
  352. }
  353. }, () => { }, scene.database);
  354. }
  355. /**
  356. * Invert the geometry to move from a right handed system to a left handed one.
  357. */
  358. public toLeftHanded(): void {
  359. // Flip faces
  360. let tIndices = this.getIndices(false);
  361. if (tIndices != null && tIndices.length > 0) {
  362. for (let i = 0; i < tIndices.length; i += 3) {
  363. let tTemp = tIndices[i + 0];
  364. tIndices[i + 0] = tIndices[i + 2];
  365. tIndices[i + 2] = tTemp;
  366. }
  367. this.setIndices(tIndices);
  368. }
  369. // Negate position.z
  370. let tPositions = this.getVerticesData(VertexBuffer.PositionKind, false);
  371. if (tPositions != null && tPositions.length > 0) {
  372. for (let i = 0; i < tPositions.length; i += 3) {
  373. tPositions[i + 2] = -tPositions[i + 2];
  374. }
  375. this.setVerticesData(VertexBuffer.PositionKind, tPositions, false);
  376. }
  377. // Negate normal.z
  378. let tNormals = this.getVerticesData(VertexBuffer.NormalKind, false);
  379. if (tNormals != null && tNormals.length > 0) {
  380. for (let i = 0; i < tNormals.length; i += 3) {
  381. tNormals[i + 2] = -tNormals[i + 2];
  382. }
  383. this.setVerticesData(VertexBuffer.NormalKind, tNormals, false);
  384. }
  385. }
  386. public isDisposed(): boolean {
  387. return this._isDisposed;
  388. }
  389. public dispose(): void {
  390. var meshes = this._meshes;
  391. var numOfMeshes = meshes.length;
  392. var index: number;
  393. for (index = 0; index < numOfMeshes; index++) {
  394. this.releaseForMesh(meshes[index]);
  395. }
  396. this._meshes = [];
  397. for (var kind in this._vertexBuffers) {
  398. this._vertexBuffers[kind].dispose();
  399. }
  400. this._vertexBuffers = {};
  401. this._totalVertices = 0;
  402. if (this._indexBuffer) {
  403. this._engine._releaseBuffer(this._indexBuffer);
  404. }
  405. this._indexBuffer = null;
  406. this._indices = [];
  407. this.delayLoadState = Engine.DELAYLOADSTATE_NONE;
  408. this.delayLoadingFile = null;
  409. this._delayLoadingFunction = null;
  410. this._delayInfo = [];
  411. this._boundingInfo = null;
  412. this._scene.removeGeometry(this);
  413. this._isDisposed = true;
  414. }
  415. public copy(id: string): Geometry {
  416. var vertexData = new VertexData();
  417. vertexData.indices = [];
  418. var indices = this.getIndices();
  419. for (var index = 0; index < indices.length; index++) {
  420. (<number[]>vertexData.indices).push(indices[index]);
  421. }
  422. var updatable = false;
  423. var stopChecking = false;
  424. var kind;
  425. for (kind in this._vertexBuffers) {
  426. // using slice() to make a copy of the array and not just reference it
  427. var data = this.getVerticesData(kind);
  428. if (data instanceof Float32Array) {
  429. vertexData.set(new Float32Array(<Float32Array>data), kind);
  430. } else {
  431. vertexData.set((<number[]>data).slice(0), kind);
  432. }
  433. if (!stopChecking) {
  434. updatable = this.getVertexBuffer(kind).isUpdatable();
  435. stopChecking = !updatable;
  436. }
  437. }
  438. var geometry = new Geometry(id, this._scene, vertexData, updatable, null);
  439. geometry.delayLoadState = this.delayLoadState;
  440. geometry.delayLoadingFile = this.delayLoadingFile;
  441. geometry._delayLoadingFunction = this._delayLoadingFunction;
  442. for (kind in this._delayInfo) {
  443. geometry._delayInfo = geometry._delayInfo || [];
  444. geometry._delayInfo.push(kind);
  445. }
  446. // Bounding info
  447. geometry._boundingInfo = new BoundingInfo(this._extend.minimum, this._extend.maximum);
  448. return geometry;
  449. }
  450. public serialize(): any {
  451. var serializationObject: any = {};
  452. serializationObject.id = this.id;
  453. if (Tags.HasTags(this)) {
  454. serializationObject.tags = Tags.GetTags(this);
  455. }
  456. return serializationObject;
  457. }
  458. public serializeVerticeData(): any {
  459. var serializationObject = this.serialize();
  460. if (this.isVerticesDataPresent(VertexBuffer.PositionKind)) {
  461. serializationObject.positions = this.getVerticesData(VertexBuffer.PositionKind);
  462. }
  463. if (this.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  464. serializationObject.normals = this.getVerticesData(VertexBuffer.NormalKind);
  465. }
  466. if (this.isVerticesDataPresent(VertexBuffer.UVKind)) {
  467. serializationObject.uvs = this.getVerticesData(VertexBuffer.UVKind);
  468. }
  469. if (this.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  470. serializationObject.uvs2 = this.getVerticesData(VertexBuffer.UV2Kind);
  471. }
  472. if (this.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
  473. serializationObject.uvs3 = this.getVerticesData(VertexBuffer.UV3Kind);
  474. }
  475. if (this.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
  476. serializationObject.uvs4 = this.getVerticesData(VertexBuffer.UV4Kind);
  477. }
  478. if (this.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
  479. serializationObject.uvs5 = this.getVerticesData(VertexBuffer.UV5Kind);
  480. }
  481. if (this.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
  482. serializationObject.uvs6 = this.getVerticesData(VertexBuffer.UV6Kind);
  483. }
  484. if (this.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  485. serializationObject.colors = this.getVerticesData(VertexBuffer.ColorKind);
  486. }
  487. if (this.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
  488. serializationObject.matricesIndices = this.getVerticesData(VertexBuffer.MatricesIndicesKind);
  489. serializationObject.matricesIndices._isExpanded = true;
  490. }
  491. if (this.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
  492. serializationObject.matricesWeights = this.getVerticesData(VertexBuffer.MatricesWeightsKind);
  493. }
  494. serializationObject.indices = this.getIndices();
  495. return serializationObject;
  496. }
  497. // Statics
  498. public static ExtractFromMesh(mesh: Mesh, id: string): Geometry {
  499. var geometry = mesh._geometry;
  500. if (!geometry) {
  501. return null;
  502. }
  503. return geometry.copy(id);
  504. }
  505. /**
  506. * You should now use Tools.RandomId(), this method is still here for legacy reasons.
  507. * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  508. * Be aware Math.random() could cause collisions, but:
  509. * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"
  510. */
  511. public static RandomId(): string {
  512. return Tools.RandomId();
  513. }
  514. public static ImportGeometry(parsedGeometry: any, mesh: Mesh): void {
  515. var scene = mesh.getScene();
  516. // Geometry
  517. var geometryId = parsedGeometry.geometryId;
  518. if (geometryId) {
  519. var geometry = scene.getGeometryByID(geometryId);
  520. if (geometry) {
  521. geometry.applyToMesh(mesh);
  522. }
  523. } else if (parsedGeometry instanceof ArrayBuffer) {
  524. var binaryInfo = mesh._binaryInfo;
  525. if (binaryInfo.positionsAttrDesc && binaryInfo.positionsAttrDesc.count > 0) {
  526. var positionsData = new Float32Array(parsedGeometry, binaryInfo.positionsAttrDesc.offset, binaryInfo.positionsAttrDesc.count);
  527. mesh.setVerticesData(VertexBuffer.PositionKind, positionsData, false);
  528. }
  529. if (binaryInfo.normalsAttrDesc && binaryInfo.normalsAttrDesc.count > 0) {
  530. var normalsData = new Float32Array(parsedGeometry, binaryInfo.normalsAttrDesc.offset, binaryInfo.normalsAttrDesc.count);
  531. mesh.setVerticesData(VertexBuffer.NormalKind, normalsData, false);
  532. }
  533. if (binaryInfo.uvsAttrDesc && binaryInfo.uvsAttrDesc.count > 0) {
  534. var uvsData = new Float32Array(parsedGeometry, binaryInfo.uvsAttrDesc.offset, binaryInfo.uvsAttrDesc.count);
  535. mesh.setVerticesData(VertexBuffer.UVKind, uvsData, false);
  536. }
  537. if (binaryInfo.uvs2AttrDesc && binaryInfo.uvs2AttrDesc.count > 0) {
  538. var uvs2Data = new Float32Array(parsedGeometry, binaryInfo.uvs2AttrDesc.offset, binaryInfo.uvs2AttrDesc.count);
  539. mesh.setVerticesData(VertexBuffer.UV2Kind, uvs2Data, false);
  540. }
  541. if (binaryInfo.uvs3AttrDesc && binaryInfo.uvs3AttrDesc.count > 0) {
  542. var uvs3Data = new Float32Array(parsedGeometry, binaryInfo.uvs3AttrDesc.offset, binaryInfo.uvs3AttrDesc.count);
  543. mesh.setVerticesData(VertexBuffer.UV3Kind, uvs3Data, false);
  544. }
  545. if (binaryInfo.uvs4AttrDesc && binaryInfo.uvs4AttrDesc.count > 0) {
  546. var uvs4Data = new Float32Array(parsedGeometry, binaryInfo.uvs4AttrDesc.offset, binaryInfo.uvs4AttrDesc.count);
  547. mesh.setVerticesData(VertexBuffer.UV4Kind, uvs4Data, false);
  548. }
  549. if (binaryInfo.uvs5AttrDesc && binaryInfo.uvs5AttrDesc.count > 0) {
  550. var uvs5Data = new Float32Array(parsedGeometry, binaryInfo.uvs5AttrDesc.offset, binaryInfo.uvs5AttrDesc.count);
  551. mesh.setVerticesData(VertexBuffer.UV5Kind, uvs5Data, false);
  552. }
  553. if (binaryInfo.uvs6AttrDesc && binaryInfo.uvs6AttrDesc.count > 0) {
  554. var uvs6Data = new Float32Array(parsedGeometry, binaryInfo.uvs6AttrDesc.offset, binaryInfo.uvs6AttrDesc.count);
  555. mesh.setVerticesData(VertexBuffer.UV6Kind, uvs6Data, false);
  556. }
  557. if (binaryInfo.colorsAttrDesc && binaryInfo.colorsAttrDesc.count > 0) {
  558. var colorsData = new Float32Array(parsedGeometry, binaryInfo.colorsAttrDesc.offset, binaryInfo.colorsAttrDesc.count);
  559. mesh.setVerticesData(VertexBuffer.ColorKind, colorsData, false, binaryInfo.colorsAttrDesc.stride);
  560. }
  561. if (binaryInfo.matricesIndicesAttrDesc && binaryInfo.matricesIndicesAttrDesc.count > 0) {
  562. var matricesIndicesData = new Int32Array(parsedGeometry, binaryInfo.matricesIndicesAttrDesc.offset, binaryInfo.matricesIndicesAttrDesc.count);
  563. mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, matricesIndicesData, false);
  564. }
  565. if (binaryInfo.matricesWeightsAttrDesc && binaryInfo.matricesWeightsAttrDesc.count > 0) {
  566. var matricesWeightsData = new Float32Array(parsedGeometry, binaryInfo.matricesWeightsAttrDesc.offset, binaryInfo.matricesWeightsAttrDesc.count);
  567. mesh.setVerticesData(VertexBuffer.MatricesWeightsKind, matricesWeightsData, false);
  568. }
  569. if (binaryInfo.indicesAttrDesc && binaryInfo.indicesAttrDesc.count > 0) {
  570. var indicesData = new Int32Array(parsedGeometry, binaryInfo.indicesAttrDesc.offset, binaryInfo.indicesAttrDesc.count);
  571. mesh.setIndices(indicesData);
  572. }
  573. if (binaryInfo.subMeshesAttrDesc && binaryInfo.subMeshesAttrDesc.count > 0) {
  574. var subMeshesData = new Int32Array(parsedGeometry, binaryInfo.subMeshesAttrDesc.offset, binaryInfo.subMeshesAttrDesc.count * 5);
  575. mesh.subMeshes = [];
  576. for (var i = 0; i < binaryInfo.subMeshesAttrDesc.count; i++) {
  577. var materialIndex = subMeshesData[(i * 5) + 0];
  578. var verticesStart = subMeshesData[(i * 5) + 1];
  579. var verticesCount = subMeshesData[(i * 5) + 2];
  580. var indexStart = subMeshesData[(i * 5) + 3];
  581. var indexCount = subMeshesData[(i * 5) + 4];
  582. var subMesh = new SubMesh(materialIndex, verticesStart, verticesCount, indexStart, indexCount, mesh);
  583. }
  584. }
  585. } else if (parsedGeometry.positions && parsedGeometry.normals && parsedGeometry.indices) {
  586. mesh.setVerticesData(VertexBuffer.PositionKind, parsedGeometry.positions, false);
  587. mesh.setVerticesData(VertexBuffer.NormalKind, parsedGeometry.normals, false);
  588. if (parsedGeometry.uvs) {
  589. mesh.setVerticesData(VertexBuffer.UVKind, parsedGeometry.uvs, false);
  590. }
  591. if (parsedGeometry.uvs2) {
  592. mesh.setVerticesData(VertexBuffer.UV2Kind, parsedGeometry.uvs2, false);
  593. }
  594. if (parsedGeometry.uvs3) {
  595. mesh.setVerticesData(VertexBuffer.UV3Kind, parsedGeometry.uvs3, false);
  596. }
  597. if (parsedGeometry.uvs4) {
  598. mesh.setVerticesData(VertexBuffer.UV4Kind, parsedGeometry.uvs4, false);
  599. }
  600. if (parsedGeometry.uvs5) {
  601. mesh.setVerticesData(VertexBuffer.UV5Kind, parsedGeometry.uvs5, false);
  602. }
  603. if (parsedGeometry.uvs6) {
  604. mesh.setVerticesData(VertexBuffer.UV6Kind, parsedGeometry.uvs6, false);
  605. }
  606. if (parsedGeometry.colors) {
  607. mesh.setVerticesData(VertexBuffer.ColorKind, Color4.CheckColors4(parsedGeometry.colors, parsedGeometry.positions.length / 3), false);
  608. }
  609. if (parsedGeometry.matricesIndices) {
  610. if (!parsedGeometry.matricesIndices._isExpanded) {
  611. var floatIndices = [];
  612. for (var i = 0; i < parsedGeometry.matricesIndices.length; i++) {
  613. var matricesIndex = parsedGeometry.matricesIndices[i];
  614. floatIndices.push(matricesIndex & 0x000000FF);
  615. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  616. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  617. floatIndices.push(matricesIndex >> 24);
  618. }
  619. mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, floatIndices, false);
  620. } else {
  621. delete parsedGeometry.matricesIndices._isExpanded;
  622. mesh.setVerticesData(VertexBuffer.MatricesIndicesKind, parsedGeometry.matricesIndices, false);
  623. }
  624. }
  625. if (parsedGeometry.matricesIndicesExtra) {
  626. if (!parsedGeometry.matricesIndicesExtra._isExpanded) {
  627. var floatIndices = [];
  628. for (var i = 0; i < parsedGeometry.matricesIndicesExtra.length; i++) {
  629. var matricesIndex = parsedGeometry.matricesIndicesExtra[i];
  630. floatIndices.push(matricesIndex & 0x000000FF);
  631. floatIndices.push((matricesIndex & 0x0000FF00) >> 8);
  632. floatIndices.push((matricesIndex & 0x00FF0000) >> 16);
  633. floatIndices.push(matricesIndex >> 24);
  634. }
  635. mesh.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, floatIndices, false);
  636. } else {
  637. delete parsedGeometry.matricesIndices._isExpanded;
  638. mesh.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, parsedGeometry.matricesIndicesExtra, false);
  639. }
  640. }
  641. if (parsedGeometry.matricesWeights) {
  642. mesh.setVerticesData(VertexBuffer.MatricesWeightsKind, parsedGeometry.matricesWeights, false);
  643. }
  644. if (parsedGeometry.matricesWeightsExtra) {
  645. mesh.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, parsedGeometry.matricesWeightsExtra, false);
  646. }
  647. mesh.setIndices(parsedGeometry.indices);
  648. }
  649. // SubMeshes
  650. if (parsedGeometry.subMeshes) {
  651. mesh.subMeshes = [];
  652. for (var subIndex = 0; subIndex < parsedGeometry.subMeshes.length; subIndex++) {
  653. var parsedSubMesh = parsedGeometry.subMeshes[subIndex];
  654. var subMesh = new SubMesh(parsedSubMesh.materialIndex, parsedSubMesh.verticesStart, parsedSubMesh.verticesCount, parsedSubMesh.indexStart, parsedSubMesh.indexCount, mesh);
  655. }
  656. }
  657. // Flat shading
  658. if (mesh._shouldGenerateFlatShading) {
  659. mesh.convertToFlatShadedMesh();
  660. delete mesh._shouldGenerateFlatShading;
  661. }
  662. // Update
  663. mesh.computeWorldMatrix(true);
  664. // Octree
  665. if (scene['_selectionOctree']) {
  666. scene['_selectionOctree'].addMesh(mesh);
  667. }
  668. }
  669. public static Parse(parsedVertexData: any, scene: Scene, rootUrl: string): Geometry {
  670. if (scene.getGeometryByID(parsedVertexData.id)) {
  671. return null; // null since geometry could be something else than a box...
  672. }
  673. var geometry = new Geometry(parsedVertexData.id, scene);
  674. Tags.AddTagsTo(geometry, parsedVertexData.tags);
  675. if (parsedVertexData.delayLoadingFile) {
  676. geometry.delayLoadState = Engine.DELAYLOADSTATE_NOTLOADED;
  677. geometry.delayLoadingFile = rootUrl + parsedVertexData.delayLoadingFile;
  678. geometry._boundingInfo = new BoundingInfo(Vector3.FromArray(parsedVertexData.boundingBoxMinimum), Vector3.FromArray(parsedVertexData.boundingBoxMaximum));
  679. geometry._delayInfo = [];
  680. if (parsedVertexData.hasUVs) {
  681. geometry._delayInfo.push(VertexBuffer.UVKind);
  682. }
  683. if (parsedVertexData.hasUVs2) {
  684. geometry._delayInfo.push(VertexBuffer.UV2Kind);
  685. }
  686. if (parsedVertexData.hasUVs3) {
  687. geometry._delayInfo.push(VertexBuffer.UV3Kind);
  688. }
  689. if (parsedVertexData.hasUVs4) {
  690. geometry._delayInfo.push(VertexBuffer.UV4Kind);
  691. }
  692. if (parsedVertexData.hasUVs5) {
  693. geometry._delayInfo.push(VertexBuffer.UV5Kind);
  694. }
  695. if (parsedVertexData.hasUVs6) {
  696. geometry._delayInfo.push(VertexBuffer.UV6Kind);
  697. }
  698. if (parsedVertexData.hasColors) {
  699. geometry._delayInfo.push(VertexBuffer.ColorKind);
  700. }
  701. if (parsedVertexData.hasMatricesIndices) {
  702. geometry._delayInfo.push(VertexBuffer.MatricesIndicesKind);
  703. }
  704. if (parsedVertexData.hasMatricesWeights) {
  705. geometry._delayInfo.push(VertexBuffer.MatricesWeightsKind);
  706. }
  707. geometry._delayLoadingFunction = VertexData.ImportVertexData;
  708. } else {
  709. VertexData.ImportVertexData(parsedVertexData, geometry);
  710. }
  711. scene.pushGeometry(geometry, true);
  712. return geometry;
  713. }
  714. }
  715. /////// Primitives //////////////////////////////////////////////
  716. export module Geometry.Primitives {
  717. /// Abstract class
  718. export class _Primitive extends Geometry {
  719. private _beingRegenerated: boolean;
  720. constructor(id: string, scene: Scene, private _canBeRegenerated?: boolean, mesh?: Mesh) {
  721. super(id, scene, null, false, mesh); // updatable = false to be sure not to update vertices
  722. this._beingRegenerated = true;
  723. this.regenerate();
  724. this._beingRegenerated = false;
  725. }
  726. public canBeRegenerated(): boolean {
  727. return this._canBeRegenerated;
  728. }
  729. public regenerate(): void {
  730. if (!this._canBeRegenerated) {
  731. return;
  732. }
  733. this._beingRegenerated = true;
  734. this.setAllVerticesData(this._regenerateVertexData(), false);
  735. this._beingRegenerated = false;
  736. }
  737. public asNewGeometry(id: string): Geometry {
  738. return super.copy(id);
  739. }
  740. // overrides
  741. public setAllVerticesData(vertexData: VertexData, updatable?: boolean): void {
  742. if (!this._beingRegenerated) {
  743. return;
  744. }
  745. super.setAllVerticesData(vertexData, false);
  746. }
  747. public setVerticesData(kind: string, data: number[] | Int32Array | Float32Array, updatable?: boolean): void {
  748. if (!this._beingRegenerated) {
  749. return;
  750. }
  751. super.setVerticesData(kind, data, false);
  752. }
  753. // to override
  754. // protected
  755. public _regenerateVertexData(): VertexData {
  756. throw new Error("Abstract method");
  757. }
  758. public copy(id: string): Geometry {
  759. throw new Error("Must be overriden in sub-classes.");
  760. }
  761. public serialize(): any {
  762. var serializationObject = super.serialize();
  763. serializationObject.canBeRegenerated = this.canBeRegenerated();
  764. return serializationObject;
  765. }
  766. }
  767. export class Ribbon extends _Primitive {
  768. // Members
  769. constructor(id: string, scene: Scene, public pathArray: Vector3[][], public closeArray: boolean, public closePath: boolean, public offset: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  770. super(id, scene, canBeRegenerated, mesh);
  771. }
  772. public _regenerateVertexData(): VertexData {
  773. return VertexData.CreateRibbon({ pathArray: this.pathArray, closeArray: this.closeArray, closePath: this.closePath, offset: this.offset, sideOrientation: this.side });
  774. }
  775. public copy(id: string): Geometry {
  776. return new Ribbon(id, this.getScene(), this.pathArray, this.closeArray, this.closePath, this.offset, this.canBeRegenerated(), null, this.side);
  777. }
  778. }
  779. export class Box extends _Primitive {
  780. // Members
  781. constructor(id: string, scene: Scene, public size: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  782. super(id, scene, canBeRegenerated, mesh);
  783. }
  784. public _regenerateVertexData(): VertexData {
  785. return VertexData.CreateBox({ size: this.size, sideOrientation: this.side });
  786. }
  787. public copy(id: string): Geometry {
  788. return new Box(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
  789. }
  790. public serialize(): any {
  791. var serializationObject = super.serialize();
  792. serializationObject.size = this.size;
  793. return serializationObject;
  794. }
  795. public static Parse(parsedBox: any, scene: Scene): Box {
  796. if (scene.getGeometryByID(parsedBox.id)) {
  797. return null; // null since geometry could be something else than a box...
  798. }
  799. var box = new Geometry.Primitives.Box(parsedBox.id, scene, parsedBox.size, parsedBox.canBeRegenerated, null);
  800. Tags.AddTagsTo(box, parsedBox.tags);
  801. scene.pushGeometry(box, true);
  802. return box;
  803. }
  804. }
  805. export class Sphere extends _Primitive {
  806. constructor(id: string, scene: Scene, public segments: number, public diameter: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  807. super(id, scene, canBeRegenerated, mesh);
  808. }
  809. public _regenerateVertexData(): VertexData {
  810. return VertexData.CreateSphere({ segments: this.segments, diameter: this.diameter, sideOrientation: this.side });
  811. }
  812. public copy(id: string): Geometry {
  813. return new Sphere(id, this.getScene(), this.segments, this.diameter, this.canBeRegenerated(), null, this.side);
  814. }
  815. public serialize(): any {
  816. var serializationObject = super.serialize();
  817. serializationObject.segments = this.segments;
  818. serializationObject.diameter = this.diameter;
  819. return serializationObject;
  820. }
  821. public static Parse(parsedSphere: any, scene: Scene): Geometry.Primitives.Sphere {
  822. if (scene.getGeometryByID(parsedSphere.id)) {
  823. return null; // null since geometry could be something else than a sphere...
  824. }
  825. var sphere = new Geometry.Primitives.Sphere(parsedSphere.id, scene, parsedSphere.segments, parsedSphere.diameter, parsedSphere.canBeRegenerated, null);
  826. Tags.AddTagsTo(sphere, parsedSphere.tags);
  827. scene.pushGeometry(sphere, true);
  828. return sphere;
  829. }
  830. }
  831. export class Disc extends _Primitive {
  832. // Members
  833. constructor(id: string, scene: Scene, public radius: number, public tessellation: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  834. super(id, scene, canBeRegenerated, mesh);
  835. }
  836. public _regenerateVertexData(): VertexData {
  837. return VertexData.CreateDisc({ radius: this.radius, tessellation: this.tessellation, sideOrientation: this.side });
  838. }
  839. public copy(id: string): Geometry {
  840. return new Disc(id, this.getScene(), this.radius, this.tessellation, this.canBeRegenerated(), null, this.side);
  841. }
  842. }
  843. export class Cylinder extends _Primitive {
  844. constructor(id: string, scene: Scene, public height: number, public diameterTop: number, public diameterBottom: number, public tessellation: number, public subdivisions: number = 1, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  845. super(id, scene, canBeRegenerated, mesh);
  846. }
  847. public _regenerateVertexData(): VertexData {
  848. return VertexData.CreateCylinder({ height: this.height, diameterTop: this.diameterTop, diameterBottom: this.diameterBottom, tessellation: this.tessellation, subdivisions: this.subdivisions, sideOrientation: this.side });
  849. }
  850. public copy(id: string): Geometry {
  851. return new Cylinder(id, this.getScene(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.subdivisions, this.canBeRegenerated(), null, this.side);
  852. }
  853. public serialize(): any {
  854. var serializationObject = super.serialize();
  855. serializationObject.height = this.height;
  856. serializationObject.diameterTop = this.diameterTop;
  857. serializationObject.diameterBottom = this.diameterBottom;
  858. serializationObject.tessellation = this.tessellation;
  859. return serializationObject;
  860. }
  861. public static Parse(parsedCylinder: any, scene: Scene): Geometry.Primitives.Cylinder {
  862. if (scene.getGeometryByID(parsedCylinder.id)) {
  863. return null; // null since geometry could be something else than a cylinder...
  864. }
  865. var cylinder = new Geometry.Primitives.Cylinder(parsedCylinder.id, scene, parsedCylinder.height, parsedCylinder.diameterTop, parsedCylinder.diameterBottom, parsedCylinder.tessellation, parsedCylinder.subdivisions, parsedCylinder.canBeRegenerated, null);
  866. Tags.AddTagsTo(cylinder, parsedCylinder.tags);
  867. scene.pushGeometry(cylinder, true);
  868. return cylinder;
  869. }
  870. }
  871. export class Torus extends _Primitive {
  872. constructor(id: string, scene: Scene, public diameter: number, public thickness: number, public tessellation: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  873. super(id, scene, canBeRegenerated, mesh);
  874. }
  875. public _regenerateVertexData(): VertexData {
  876. return VertexData.CreateTorus({ diameter: this.diameter, thickness: this.thickness, tessellation: this.tessellation, sideOrientation: this.side });
  877. }
  878. public copy(id: string): Geometry {
  879. return new Torus(id, this.getScene(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null, this.side);
  880. }
  881. public serialize(): any {
  882. var serializationObject = super.serialize();
  883. serializationObject.diameter = this.diameter;
  884. serializationObject.thickness = this.thickness;
  885. serializationObject.tessellation = this.tessellation;
  886. return serializationObject;
  887. }
  888. public static Parse(parsedTorus: any, scene: Scene): Geometry.Primitives.Torus {
  889. if (scene.getGeometryByID(parsedTorus.id)) {
  890. return null; // null since geometry could be something else than a torus...
  891. }
  892. var torus = new Geometry.Primitives.Torus(parsedTorus.id, scene, parsedTorus.diameter, parsedTorus.thickness, parsedTorus.tessellation, parsedTorus.canBeRegenerated, null);
  893. Tags.AddTagsTo(torus, parsedTorus.tags);
  894. scene.pushGeometry(torus, true);
  895. return torus;
  896. }
  897. }
  898. export class Ground extends _Primitive {
  899. constructor(id: string, scene: Scene, public width: number, public height: number, public subdivisions: number, canBeRegenerated?: boolean, mesh?: Mesh) {
  900. super(id, scene, canBeRegenerated, mesh);
  901. }
  902. public _regenerateVertexData(): VertexData {
  903. return VertexData.CreateGround({ width: this.width, height: this.height, subdivisions: this.subdivisions });
  904. }
  905. public copy(id: string): Geometry {
  906. return new Ground(id, this.getScene(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
  907. }
  908. public serialize(): any {
  909. var serializationObject = super.serialize();
  910. serializationObject.width = this.width;
  911. serializationObject.height = this.height;
  912. serializationObject.subdivisions = this.subdivisions;
  913. return serializationObject;
  914. }
  915. public static Parse(parsedGround: any, scene: Scene): Geometry.Primitives.Ground {
  916. if (scene.getGeometryByID(parsedGround.id)) {
  917. return null; // null since geometry could be something else than a ground...
  918. }
  919. var ground = new Geometry.Primitives.Ground(parsedGround.id, scene, parsedGround.width, parsedGround.height, parsedGround.subdivisions, parsedGround.canBeRegenerated, null);
  920. Tags.AddTagsTo(ground, parsedGround.tags);
  921. scene.pushGeometry(ground, true);
  922. return ground;
  923. }
  924. }
  925. export class TiledGround extends _Primitive {
  926. constructor(id: string, scene: Scene, public xmin: number, public zmin: number, public xmax: number, public zmax: number, public subdivisions: { w: number; h: number; }, public precision: { w: number; h: number; }, canBeRegenerated?: boolean, mesh?: Mesh) {
  927. super(id, scene, canBeRegenerated, mesh);
  928. }
  929. public _regenerateVertexData(): VertexData {
  930. return VertexData.CreateTiledGround({ xmin: this.xmin, zmin: this.zmin, xmax: this.xmax, zmax: this.zmax, subdivisions: this.subdivisions, precision: this.precision });
  931. }
  932. public copy(id: string): Geometry {
  933. return new TiledGround(id, this.getScene(), this.xmin, this.zmin, this.xmax, this.zmax, this.subdivisions, this.precision, this.canBeRegenerated(), null);
  934. }
  935. }
  936. export class Plane extends _Primitive {
  937. constructor(id: string, scene: Scene, public size: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  938. super(id, scene, canBeRegenerated, mesh);
  939. }
  940. public _regenerateVertexData(): VertexData {
  941. return VertexData.CreatePlane({ size: this.size, sideOrientation: this.side });
  942. }
  943. public copy(id: string): Geometry {
  944. return new Plane(id, this.getScene(), this.size, this.canBeRegenerated(), null, this.side);
  945. }
  946. public serialize(): any {
  947. var serializationObject = super.serialize();
  948. serializationObject.size = this.size;
  949. return serializationObject;
  950. }
  951. public static Parse(parsedPlane: any, scene: Scene): Geometry.Primitives.Plane {
  952. if (scene.getGeometryByID(parsedPlane.id)) {
  953. return null; // null since geometry could be something else than a ground...
  954. }
  955. var plane = new Geometry.Primitives.Plane(parsedPlane.id, scene, parsedPlane.size, parsedPlane.canBeRegenerated, null);
  956. Tags.AddTagsTo(plane, parsedPlane.tags);
  957. scene.pushGeometry(plane, true);
  958. return plane;
  959. }
  960. }
  961. export class TorusKnot extends _Primitive {
  962. constructor(id: string, scene: Scene, public radius: number, public tube: number, public radialSegments: number, public tubularSegments: number, public p: number, public q: number, canBeRegenerated?: boolean, mesh?: Mesh, public side: number = Mesh.DEFAULTSIDE) {
  963. super(id, scene, canBeRegenerated, mesh);
  964. }
  965. public _regenerateVertexData(): VertexData {
  966. return VertexData.CreateTorusKnot({ radius: this.radius, tube: this.tube, radialSegments: this.radialSegments, tubularSegments: this.tubularSegments, p: this.p, q: this.q, sideOrientation: this.side });
  967. }
  968. public copy(id: string): Geometry {
  969. return new TorusKnot(id, this.getScene(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null, this.side);
  970. }
  971. public serialize(): any {
  972. var serializationObject = super.serialize();
  973. serializationObject.radius = this.radius;
  974. serializationObject.tube = this.tube;
  975. serializationObject.radialSegments = this.radialSegments;
  976. serializationObject.tubularSegments = this.tubularSegments;
  977. serializationObject.p = this.p;
  978. serializationObject.q = this.q;
  979. return serializationObject;
  980. };
  981. public static Parse(parsedTorusKnot: any, scene: Scene): Geometry.Primitives.TorusKnot {
  982. if (scene.getGeometryByID(parsedTorusKnot.id)) {
  983. return null; // null since geometry could be something else than a ground...
  984. }
  985. var torusKnot = new Geometry.Primitives.TorusKnot(parsedTorusKnot.id, scene, parsedTorusKnot.radius, parsedTorusKnot.tube, parsedTorusKnot.radialSegments, parsedTorusKnot.tubularSegments, parsedTorusKnot.p, parsedTorusKnot.q, parsedTorusKnot.canBeRegenerated, null);
  986. Tags.AddTagsTo(torusKnot, parsedTorusKnot.tags);
  987. scene.pushGeometry(torusKnot, true);
  988. return torusKnot;
  989. }
  990. }
  991. }
  992. }