babylon.geometry.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. var __extends = this.__extends || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var Geometry = (function () {
  10. function Geometry(id, engine, vertexData, updatable, mesh) {
  11. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  12. this._totalVertices = 0;
  13. this._indices = [];
  14. this.id = id;
  15. this._engine = engine;
  16. this._meshes = [];
  17. // vertexData
  18. if (vertexData) {
  19. this.setAllVerticesData(vertexData, updatable);
  20. } else {
  21. this._totalVertices = 0;
  22. this._indices = [];
  23. }
  24. // applyToMesh
  25. if (mesh) {
  26. this.applyToMesh(mesh);
  27. }
  28. }
  29. Geometry.prototype.getEngine = function () {
  30. return this._engine;
  31. };
  32. Geometry.prototype.isReady = function () {
  33. return this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADED || this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NONE;
  34. };
  35. Geometry.prototype.setAllVerticesData = function (vertexData, updatable) {
  36. vertexData.applyToGeometry(this, updatable);
  37. };
  38. Geometry.prototype.setVerticesData = function (data, kind, updatable) {
  39. this._vertexBuffers = this._vertexBuffers || {};
  40. if (this._vertexBuffers[kind]) {
  41. this._vertexBuffers[kind].dispose();
  42. }
  43. this._vertexBuffers[kind] = new BABYLON.VertexBuffer(this._engine, data, kind, updatable, this._meshes.length === 0);
  44. if (kind === BABYLON.VertexBuffer.PositionKind) {
  45. var stride = this._vertexBuffers[kind].getStrideSize();
  46. this._totalVertices = data.length / stride;
  47. var extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._totalVertices);
  48. var meshes = this._meshes;
  49. var numOfMeshes = meshes.length;
  50. for (var index = 0; index < numOfMeshes; index++) {
  51. var mesh = meshes[index];
  52. mesh._resetPointsArrayCache();
  53. mesh._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  54. mesh._createGlobalSubMesh();
  55. }
  56. }
  57. };
  58. Geometry.prototype.updateVerticesData = function (kind, data, updateExtends) {
  59. var vertexBuffer = this.getVertexBuffer(kind);
  60. if (!vertexBuffer) {
  61. return;
  62. }
  63. vertexBuffer.update(data);
  64. if (kind === BABYLON.VertexBuffer.PositionKind) {
  65. var extend;
  66. if (updateExtends) {
  67. var stride = vertexBuffer.getStrideSize();
  68. this._totalVertices = data.length / stride;
  69. extend = BABYLON.Tools.ExtractMinAndMax(data, 0, this._totalVertices);
  70. }
  71. var meshes = this._meshes;
  72. var numOfMeshes = meshes.length;
  73. for (var index = 0; index < numOfMeshes; index++) {
  74. var mesh = meshes[index];
  75. mesh._resetPointsArrayCache();
  76. if (updateExtends) {
  77. mesh._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  78. }
  79. }
  80. }
  81. };
  82. Geometry.prototype.getTotalVertices = function () {
  83. if (!this.isReady()) {
  84. return 0;
  85. }
  86. return this._totalVertices;
  87. };
  88. Geometry.prototype.getVerticesData = function (kind) {
  89. var vertexBuffer = this.getVertexBuffer(kind);
  90. if (!vertexBuffer) {
  91. return null;
  92. }
  93. return vertexBuffer.getData();
  94. };
  95. Geometry.prototype.getVertexBuffer = function (kind) {
  96. if (!this.isReady()) {
  97. return null;
  98. }
  99. return this._vertexBuffers[kind];
  100. };
  101. Geometry.prototype.getVertexBuffers = function () {
  102. if (!this.isReady()) {
  103. return null;
  104. }
  105. return this._vertexBuffers;
  106. };
  107. Geometry.prototype.isVerticesDataPresent = function (kind) {
  108. if (!this._vertexBuffers) {
  109. if (this._delayInfo) {
  110. return this._delayInfo.indexOf(kind) !== -1;
  111. }
  112. return false;
  113. }
  114. return this._vertexBuffers[kind] !== undefined;
  115. };
  116. Geometry.prototype.getVerticesDataKinds = function () {
  117. var result = [];
  118. if (!this._vertexBuffers && this._delayInfo) {
  119. for (var kind in this._delayInfo) {
  120. result.push(kind);
  121. }
  122. } else {
  123. for (var kind in this._vertexBuffers) {
  124. result.push(kind);
  125. }
  126. }
  127. return result;
  128. };
  129. Geometry.prototype.setIndices = function (indices) {
  130. if (this._indexBuffer) {
  131. this._engine._releaseBuffer(this._indexBuffer);
  132. }
  133. this._indices = indices;
  134. if (this._meshes.length !== 0 && this._indices) {
  135. this._indexBuffer = this._engine.createIndexBuffer(this._indices);
  136. }
  137. var meshes = this._meshes;
  138. var numOfMeshes = meshes.length;
  139. for (var index = 0; index < numOfMeshes; index++) {
  140. meshes[index]._createGlobalSubMesh();
  141. }
  142. };
  143. Geometry.prototype.getTotalIndices = function () {
  144. if (!this.isReady()) {
  145. return 0;
  146. }
  147. return this._indices.length;
  148. };
  149. Geometry.prototype.getIndices = function () {
  150. if (!this.isReady()) {
  151. return null;
  152. }
  153. return this._indices;
  154. };
  155. Geometry.prototype.getIndexBuffer = function () {
  156. if (!this.isReady()) {
  157. return null;
  158. }
  159. return this._indexBuffer;
  160. };
  161. Geometry.prototype.releaseForMesh = function (mesh) {
  162. var meshes = this._meshes;
  163. var index = meshes.indexOf(mesh);
  164. if (index === -1) {
  165. return;
  166. }
  167. for (var kind in this._vertexBuffers) {
  168. this._vertexBuffers[kind].dispose();
  169. }
  170. if (this._indexBuffer && this._engine._releaseBuffer(this._indexBuffer)) {
  171. this._indexBuffer = null;
  172. }
  173. meshes.splice(index, 1);
  174. mesh._geometry = null;
  175. };
  176. Geometry.prototype.applyToMesh = function (mesh) {
  177. if (mesh._geometry === this) {
  178. return;
  179. }
  180. var previousGeometry = mesh._geometry;
  181. if (previousGeometry) {
  182. previousGeometry.releaseForMesh(mesh);
  183. }
  184. var meshes = this._meshes;
  185. var numOfMeshes = meshes.length + 1;
  186. // must be done before setting vertexBuffers because of mesh._createGlobalSubMesh()
  187. mesh._geometry = this;
  188. mesh.getScene().pushGeometry(this);
  189. meshes.push(mesh);
  190. if (this.isReady()) {
  191. this._applyToMesh(mesh);
  192. } else {
  193. mesh._boundingInfo = this._boundingInfo;
  194. }
  195. };
  196. Geometry.prototype._applyToMesh = function (mesh) {
  197. var numOfMeshes = this._meshes.length;
  198. for (var kind in this._vertexBuffers) {
  199. if (numOfMeshes === 1) {
  200. this._vertexBuffers[kind].create();
  201. }
  202. this._vertexBuffers[kind]._buffer.references = numOfMeshes;
  203. if (kind === BABYLON.VertexBuffer.PositionKind) {
  204. mesh._resetPointsArrayCache();
  205. var extend = BABYLON.Tools.ExtractMinAndMax(this._vertexBuffers[kind].getData(), 0, this._totalVertices);
  206. mesh._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  207. mesh._createGlobalSubMesh();
  208. }
  209. }
  210. // indexBuffer
  211. if (numOfMeshes === 1 && this._indices) {
  212. this._indexBuffer = this._engine.createIndexBuffer(this._indices);
  213. }
  214. if (this._indexBuffer) {
  215. this._indexBuffer.references = numOfMeshes;
  216. }
  217. };
  218. Geometry.prototype.load = function (scene, onLoaded) {
  219. if (this.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADING) {
  220. return;
  221. }
  222. if (this.isReady()) {
  223. if (onLoaded) {
  224. onLoaded();
  225. }
  226. return;
  227. }
  228. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADING;
  229. scene._addPendingData(this);
  230. var that = this;
  231. BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
  232. that._delayLoadingFunction(JSON.parse(data), that);
  233. that.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  234. that._delayInfo = [];
  235. scene._removePendingData(that);
  236. var meshes = that._meshes;
  237. var numOfMeshes = meshes.length;
  238. for (var index = 0; index < numOfMeshes; index++) {
  239. that._applyToMesh(meshes[index]);
  240. }
  241. if (onLoaded) {
  242. onLoaded();
  243. }
  244. }, function () {
  245. }, scene.database);
  246. };
  247. Geometry.prototype.dispose = function () {
  248. var meshes = this._meshes;
  249. var numOfMeshes = meshes.length;
  250. for (var index = 0; index < numOfMeshes; index++) {
  251. this.releaseForMesh(meshes[index]);
  252. }
  253. this._meshes = [];
  254. for (var kind in this._vertexBuffers) {
  255. this._vertexBuffers[kind].dispose();
  256. }
  257. this._vertexBuffers = [];
  258. this._totalVertices = 0;
  259. if (this._indexBuffer) {
  260. this._engine._releaseBuffer(this._indexBuffer);
  261. }
  262. this._indexBuffer = null;
  263. this._indices = [];
  264. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  265. this.delayLoadingFile = null;
  266. this._delayLoadingFunction = null;
  267. this._delayInfo = [];
  268. this._boundingInfo = null; // todo: .dispose()
  269. };
  270. Geometry.prototype.copy = function (id) {
  271. var vertexData = new BABYLON.VertexData();
  272. vertexData.indices = [];
  273. var indices = this.getIndices();
  274. for (var index = 0; index < indices.length; index++) {
  275. vertexData.indices.push(indices[index]);
  276. }
  277. var updatable = false;
  278. var stopChecking = false;
  279. for (var kind in this._vertexBuffers) {
  280. vertexData.set(this.getVerticesData(kind), kind);
  281. if (!stopChecking) {
  282. updatable = this.getVertexBuffer(kind).isUpdatable();
  283. stopChecking = !updatable;
  284. }
  285. }
  286. var geometry = new BABYLON.Geometry(id, this._engine, vertexData, updatable, null);
  287. geometry.delayLoadState = this.delayLoadState;
  288. geometry.delayLoadingFile = this.delayLoadingFile;
  289. geometry._delayLoadingFunction = this._delayLoadingFunction;
  290. for (var kind in this._delayInfo) {
  291. geometry._delayInfo = geometry._delayInfo || [];
  292. geometry._delayInfo.push(kind);
  293. }
  294. // Bounding info
  295. var extend = BABYLON.Tools.ExtractMinAndMax(this.getVerticesData(BABYLON.VertexBuffer.PositionKind), 0, this.getTotalVertices());
  296. geometry._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  297. return geometry;
  298. };
  299. // Statics
  300. Geometry.ExtractFromMesh = function (mesh, id) {
  301. var geometry = mesh._geometry;
  302. if (!geometry) {
  303. return null;
  304. }
  305. return geometry.copy(id);
  306. };
  307. // from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  308. // be aware Math.random() could cause collisions
  309. Geometry.RandomId = function () {
  310. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  311. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  312. return v.toString(16);
  313. });
  314. };
  315. return Geometry;
  316. })();
  317. BABYLON.Geometry = Geometry;
  318. (function (Geometry) {
  319. /////// Primitives //////////////////////////////////////////////
  320. (function (Primitives) {
  321. /// Abstract class
  322. var _Primitive = (function (_super) {
  323. __extends(_Primitive, _super);
  324. function _Primitive(id, engine, vertexData, canBeRegenerated, mesh) {
  325. this._beingRegenerated = true;
  326. this._canBeRegenerated = canBeRegenerated;
  327. _super.call(this, id, engine, vertexData, false, mesh); // updatable = false to be sure not to update vertices
  328. this._beingRegenerated = false;
  329. }
  330. _Primitive.prototype.canBeRegenerated = function () {
  331. return this._canBeRegenerated;
  332. };
  333. _Primitive.prototype.regenerate = function () {
  334. if (!this._canBeRegenerated) {
  335. return;
  336. }
  337. this._beingRegenerated = true;
  338. this.setAllVerticesData(this._regenerateVertexData(), false);
  339. this._beingRegenerated = false;
  340. };
  341. _Primitive.prototype.asNewGeometry = function (id) {
  342. return _super.prototype.copy.call(this, id);
  343. };
  344. // overrides
  345. _Primitive.prototype.setAllVerticesData = function (vertexData, updatable) {
  346. if (!this._beingRegenerated) {
  347. return;
  348. }
  349. return _super.prototype.setAllVerticesData.call(this, vertexData, false);
  350. };
  351. _Primitive.prototype.setVerticesData = function (data, kind, updatable) {
  352. if (!this._beingRegenerated) {
  353. return;
  354. }
  355. _super.prototype.setVerticesData.call(this, data, kind, false);
  356. };
  357. // to override
  358. // protected
  359. _Primitive.prototype._regenerateVertexData = function () {
  360. throw new Error("Abstract method");
  361. };
  362. _Primitive.prototype.copy = function (id) {
  363. throw new Error("Must be overriden in sub-classes.");
  364. };
  365. return _Primitive;
  366. })(Geometry);
  367. Primitives._Primitive = _Primitive;
  368. var Box = (function (_super) {
  369. __extends(Box, _super);
  370. function Box(id, engine, size, canBeRegenerated, mesh) {
  371. this.size = size;
  372. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  373. }
  374. Box.prototype._regenerateVertexData = function () {
  375. return BABYLON.VertexData.CreateBox(this.size);
  376. };
  377. Box.prototype.copy = function (id) {
  378. return new Box(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  379. };
  380. return Box;
  381. })(_Primitive);
  382. Primitives.Box = Box;
  383. var Sphere = (function (_super) {
  384. __extends(Sphere, _super);
  385. function Sphere(id, engine, segments, diameter, canBeRegenerated, mesh) {
  386. this.segments = segments;
  387. this.diameter = diameter;
  388. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  389. }
  390. Sphere.prototype._regenerateVertexData = function () {
  391. return BABYLON.VertexData.CreateSphere(this.segments, this.diameter);
  392. };
  393. Sphere.prototype.copy = function (id) {
  394. return new Sphere(id, this.getEngine(), this.segments, this.diameter, this.canBeRegenerated(), null);
  395. };
  396. return Sphere;
  397. })(_Primitive);
  398. Primitives.Sphere = Sphere;
  399. var Cylinder = (function (_super) {
  400. __extends(Cylinder, _super);
  401. function Cylinder(id, engine, height, diameterTop, diameterBottom, tessellation, canBeRegenerated, mesh) {
  402. this.height = height;
  403. this.diameterTop = diameterTop;
  404. this.diameterBottom = diameterBottom;
  405. this.tessellation = tessellation;
  406. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  407. }
  408. Cylinder.prototype._regenerateVertexData = function () {
  409. return BABYLON.VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation);
  410. };
  411. Cylinder.prototype.copy = function (id) {
  412. return new Cylinder(id, this.getEngine(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.canBeRegenerated(), null);
  413. };
  414. return Cylinder;
  415. })(_Primitive);
  416. Primitives.Cylinder = Cylinder;
  417. var Torus = (function (_super) {
  418. __extends(Torus, _super);
  419. function Torus(id, engine, diameter, thickness, tessellation, canBeRegenerated, mesh) {
  420. this.diameter = diameter;
  421. this.thickness = thickness;
  422. this.tessellation = tessellation;
  423. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  424. }
  425. Torus.prototype._regenerateVertexData = function () {
  426. return BABYLON.VertexData.CreateTorus(this.diameter, this.thickness, this.tessellation);
  427. };
  428. Torus.prototype.copy = function (id) {
  429. return new Torus(id, this.getEngine(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null);
  430. };
  431. return Torus;
  432. })(_Primitive);
  433. Primitives.Torus = Torus;
  434. var Ground = (function (_super) {
  435. __extends(Ground, _super);
  436. function Ground(id, engine, width, height, subdivisions, canBeRegenerated, mesh) {
  437. this.width = width;
  438. this.height = height;
  439. this.subdivisions = subdivisions;
  440. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  441. }
  442. Ground.prototype._regenerateVertexData = function () {
  443. return BABYLON.VertexData.CreateGround(this.width, this.height, this.subdivisions);
  444. };
  445. Ground.prototype.copy = function (id) {
  446. return new Ground(id, this.getEngine(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
  447. };
  448. return Ground;
  449. })(_Primitive);
  450. Primitives.Ground = Ground;
  451. var Plane = (function (_super) {
  452. __extends(Plane, _super);
  453. function Plane(id, engine, size, canBeRegenerated, mesh) {
  454. this.size = size;
  455. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  456. }
  457. Plane.prototype._regenerateVertexData = function () {
  458. return BABYLON.VertexData.CreatePlane(this.size);
  459. };
  460. Plane.prototype.copy = function (id) {
  461. return new Plane(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  462. };
  463. return Plane;
  464. })(_Primitive);
  465. Primitives.Plane = Plane;
  466. var TorusKnot = (function (_super) {
  467. __extends(TorusKnot, _super);
  468. function TorusKnot(id, engine, radius, tube, radialSegments, tubularSegments, p, q, canBeRegenerated, mesh) {
  469. this.radius = radius;
  470. this.tube = tube;
  471. this.radialSegments = radialSegments;
  472. this.tubularSegments = tubularSegments;
  473. this.p = p;
  474. this.q = q;
  475. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  476. }
  477. TorusKnot.prototype._regenerateVertexData = function () {
  478. return BABYLON.VertexData.CreateTorusKnot(this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q);
  479. };
  480. TorusKnot.prototype.copy = function (id) {
  481. return new TorusKnot(id, this.getEngine(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null);
  482. };
  483. return TorusKnot;
  484. })(_Primitive);
  485. Primitives.TorusKnot = TorusKnot;
  486. })(Geometry.Primitives || (Geometry.Primitives = {}));
  487. var Primitives = Geometry.Primitives;
  488. })(BABYLON.Geometry || (BABYLON.Geometry = {}));
  489. var Geometry = BABYLON.Geometry;
  490. })(BABYLON || (BABYLON = {}));
  491. //# sourceMappingURL=babylon.geometry.js.map