babylon.geometry.ts 51 KB

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