babylon.geometry.ts 59 KB

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