babylon.geometry.ts 60 KB

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