babylon.geometry.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 (kind, data, 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 (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. // must be done before setting vertexBuffers because of mesh._createGlobalSubMesh()
  186. mesh._geometry = this;
  187. mesh.getScene().pushGeometry(this);
  188. meshes.push(mesh);
  189. if (this.isReady()) {
  190. this._applyToMesh(mesh);
  191. } else {
  192. mesh._boundingInfo = this._boundingInfo;
  193. }
  194. };
  195. Geometry.prototype._applyToMesh = function (mesh) {
  196. var numOfMeshes = this._meshes.length;
  197. for (var kind in this._vertexBuffers) {
  198. if (numOfMeshes === 1) {
  199. this._vertexBuffers[kind].create();
  200. }
  201. this._vertexBuffers[kind]._buffer.references = numOfMeshes;
  202. if (kind === BABYLON.VertexBuffer.PositionKind) {
  203. mesh._resetPointsArrayCache();
  204. var extend = BABYLON.Tools.ExtractMinAndMax(this._vertexBuffers[kind].getData(), 0, this._totalVertices);
  205. mesh._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  206. mesh._createGlobalSubMesh();
  207. }
  208. }
  209. // indexBuffer
  210. if (numOfMeshes === 1 && this._indices) {
  211. this._indexBuffer = this._engine.createIndexBuffer(this._indices);
  212. }
  213. if (this._indexBuffer) {
  214. this._indexBuffer.references = numOfMeshes;
  215. }
  216. };
  217. Geometry.prototype.load = function (scene, onLoaded) {
  218. var _this = this;
  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. BABYLON.Tools.LoadFile(this.delayLoadingFile, function (data) {
  231. _this._delayLoadingFunction(JSON.parse(data), _this);
  232. _this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_LOADED;
  233. _this._delayInfo = [];
  234. scene._removePendingData(_this);
  235. var meshes = _this._meshes;
  236. var numOfMeshes = meshes.length;
  237. for (var index = 0; index < numOfMeshes; index++) {
  238. _this._applyToMesh(meshes[index]);
  239. }
  240. if (onLoaded) {
  241. onLoaded();
  242. }
  243. }, function () {
  244. }, scene.database);
  245. };
  246. Geometry.prototype.dispose = function () {
  247. var meshes = this._meshes;
  248. var numOfMeshes = meshes.length;
  249. for (var index = 0; index < numOfMeshes; index++) {
  250. this.releaseForMesh(meshes[index]);
  251. }
  252. this._meshes = [];
  253. for (var kind in this._vertexBuffers) {
  254. this._vertexBuffers[kind].dispose();
  255. }
  256. this._vertexBuffers = [];
  257. this._totalVertices = 0;
  258. if (this._indexBuffer) {
  259. this._engine._releaseBuffer(this._indexBuffer);
  260. }
  261. this._indexBuffer = null;
  262. this._indices = [];
  263. this.delayLoadState = BABYLON.Engine.DELAYLOADSTATE_NONE;
  264. this.delayLoadingFile = null;
  265. this._delayLoadingFunction = null;
  266. this._delayInfo = [];
  267. this._boundingInfo = null; // todo: .dispose()
  268. };
  269. Geometry.prototype.copy = function (id) {
  270. var vertexData = new BABYLON.VertexData();
  271. vertexData.indices = [];
  272. var indices = this.getIndices();
  273. for (var index = 0; index < indices.length; index++) {
  274. vertexData.indices.push(indices[index]);
  275. }
  276. var updatable = false;
  277. var stopChecking = false;
  278. for (var kind in this._vertexBuffers) {
  279. vertexData.set(this.getVerticesData(kind), kind);
  280. if (!stopChecking) {
  281. updatable = this.getVertexBuffer(kind).isUpdatable();
  282. stopChecking = !updatable;
  283. }
  284. }
  285. var geometry = new BABYLON.Geometry(id, this._engine, vertexData, updatable, null);
  286. geometry.delayLoadState = this.delayLoadState;
  287. geometry.delayLoadingFile = this.delayLoadingFile;
  288. geometry._delayLoadingFunction = this._delayLoadingFunction;
  289. for (kind in this._delayInfo) {
  290. geometry._delayInfo = geometry._delayInfo || [];
  291. geometry._delayInfo.push(kind);
  292. }
  293. // Bounding info
  294. var extend = BABYLON.Tools.ExtractMinAndMax(this.getVerticesData(BABYLON.VertexBuffer.PositionKind), 0, this.getTotalVertices());
  295. geometry._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  296. return geometry;
  297. };
  298. // Statics
  299. Geometry.ExtractFromMesh = function (mesh, id) {
  300. var geometry = mesh._geometry;
  301. if (!geometry) {
  302. return null;
  303. }
  304. return geometry.copy(id);
  305. };
  306. // from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  307. // be aware Math.random() could cause collisions
  308. Geometry.RandomId = function () {
  309. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  310. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  311. return v.toString(16);
  312. });
  313. };
  314. return Geometry;
  315. })();
  316. BABYLON.Geometry = Geometry;
  317. (function (Geometry) {
  318. /////// Primitives //////////////////////////////////////////////
  319. (function (Primitives) {
  320. /// Abstract class
  321. var _Primitive = (function (_super) {
  322. __extends(_Primitive, _super);
  323. function _Primitive(id, engine, vertexData, canBeRegenerated, mesh) {
  324. this._beingRegenerated = true;
  325. this._canBeRegenerated = canBeRegenerated;
  326. _super.call(this, id, engine, vertexData, false, mesh); // updatable = false to be sure not to update vertices
  327. this._beingRegenerated = false;
  328. }
  329. _Primitive.prototype.canBeRegenerated = function () {
  330. return this._canBeRegenerated;
  331. };
  332. _Primitive.prototype.regenerate = function () {
  333. if (!this._canBeRegenerated) {
  334. return;
  335. }
  336. this._beingRegenerated = true;
  337. this.setAllVerticesData(this._regenerateVertexData(), false);
  338. this._beingRegenerated = false;
  339. };
  340. _Primitive.prototype.asNewGeometry = function (id) {
  341. return _super.prototype.copy.call(this, id);
  342. };
  343. // overrides
  344. _Primitive.prototype.setAllVerticesData = function (vertexData, updatable) {
  345. if (!this._beingRegenerated) {
  346. return;
  347. }
  348. _super.prototype.setAllVerticesData.call(this, vertexData, false);
  349. };
  350. _Primitive.prototype.setVerticesData = function (kind, data, updatable) {
  351. if (!this._beingRegenerated) {
  352. return;
  353. }
  354. _super.prototype.setVerticesData.call(this, kind, data, false);
  355. };
  356. // to override
  357. // protected
  358. _Primitive.prototype._regenerateVertexData = function () {
  359. throw new Error("Abstract method");
  360. };
  361. _Primitive.prototype.copy = function (id) {
  362. throw new Error("Must be overriden in sub-classes.");
  363. };
  364. return _Primitive;
  365. })(Geometry);
  366. Primitives._Primitive = _Primitive;
  367. var Box = (function (_super) {
  368. __extends(Box, _super);
  369. function Box(id, engine, size, canBeRegenerated, mesh) {
  370. this.size = size;
  371. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  372. }
  373. Box.prototype._regenerateVertexData = function () {
  374. return BABYLON.VertexData.CreateBox(this.size);
  375. };
  376. Box.prototype.copy = function (id) {
  377. return new Box(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  378. };
  379. return Box;
  380. })(_Primitive);
  381. Primitives.Box = Box;
  382. var Sphere = (function (_super) {
  383. __extends(Sphere, _super);
  384. function Sphere(id, engine, segments, diameter, canBeRegenerated, mesh) {
  385. this.segments = segments;
  386. this.diameter = diameter;
  387. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  388. }
  389. Sphere.prototype._regenerateVertexData = function () {
  390. return BABYLON.VertexData.CreateSphere(this.segments, this.diameter);
  391. };
  392. Sphere.prototype.copy = function (id) {
  393. return new Sphere(id, this.getEngine(), this.segments, this.diameter, this.canBeRegenerated(), null);
  394. };
  395. return Sphere;
  396. })(_Primitive);
  397. Primitives.Sphere = Sphere;
  398. var Cylinder = (function (_super) {
  399. __extends(Cylinder, _super);
  400. function Cylinder(id, engine, height, diameterTop, diameterBottom, tessellation, canBeRegenerated, mesh) {
  401. this.height = height;
  402. this.diameterTop = diameterTop;
  403. this.diameterBottom = diameterBottom;
  404. this.tessellation = tessellation;
  405. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  406. }
  407. Cylinder.prototype._regenerateVertexData = function () {
  408. return BABYLON.VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation);
  409. };
  410. Cylinder.prototype.copy = function (id) {
  411. return new Cylinder(id, this.getEngine(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.canBeRegenerated(), null);
  412. };
  413. return Cylinder;
  414. })(_Primitive);
  415. Primitives.Cylinder = Cylinder;
  416. var Torus = (function (_super) {
  417. __extends(Torus, _super);
  418. function Torus(id, engine, diameter, thickness, tessellation, canBeRegenerated, mesh) {
  419. this.diameter = diameter;
  420. this.thickness = thickness;
  421. this.tessellation = tessellation;
  422. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  423. }
  424. Torus.prototype._regenerateVertexData = function () {
  425. return BABYLON.VertexData.CreateTorus(this.diameter, this.thickness, this.tessellation);
  426. };
  427. Torus.prototype.copy = function (id) {
  428. return new Torus(id, this.getEngine(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null);
  429. };
  430. return Torus;
  431. })(_Primitive);
  432. Primitives.Torus = Torus;
  433. var Ground = (function (_super) {
  434. __extends(Ground, _super);
  435. function Ground(id, engine, width, height, subdivisions, canBeRegenerated, mesh) {
  436. this.width = width;
  437. this.height = height;
  438. this.subdivisions = subdivisions;
  439. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  440. }
  441. Ground.prototype._regenerateVertexData = function () {
  442. return BABYLON.VertexData.CreateGround(this.width, this.height, this.subdivisions);
  443. };
  444. Ground.prototype.copy = function (id) {
  445. return new Ground(id, this.getEngine(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
  446. };
  447. return Ground;
  448. })(_Primitive);
  449. Primitives.Ground = Ground;
  450. var Plane = (function (_super) {
  451. __extends(Plane, _super);
  452. function Plane(id, engine, size, canBeRegenerated, mesh) {
  453. this.size = size;
  454. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  455. }
  456. Plane.prototype._regenerateVertexData = function () {
  457. return BABYLON.VertexData.CreatePlane(this.size);
  458. };
  459. Plane.prototype.copy = function (id) {
  460. return new Plane(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  461. };
  462. return Plane;
  463. })(_Primitive);
  464. Primitives.Plane = Plane;
  465. var TorusKnot = (function (_super) {
  466. __extends(TorusKnot, _super);
  467. function TorusKnot(id, engine, radius, tube, radialSegments, tubularSegments, p, q, canBeRegenerated, mesh) {
  468. this.radius = radius;
  469. this.tube = tube;
  470. this.radialSegments = radialSegments;
  471. this.tubularSegments = tubularSegments;
  472. this.p = p;
  473. this.q = q;
  474. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  475. }
  476. TorusKnot.prototype._regenerateVertexData = function () {
  477. return BABYLON.VertexData.CreateTorusKnot(this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q);
  478. };
  479. TorusKnot.prototype.copy = function (id) {
  480. return new TorusKnot(id, this.getEngine(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null);
  481. };
  482. return TorusKnot;
  483. })(_Primitive);
  484. Primitives.TorusKnot = TorusKnot;
  485. })(Geometry.Primitives || (Geometry.Primitives = {}));
  486. var Primitives = Geometry.Primitives;
  487. })(BABYLON.Geometry || (BABYLON.Geometry = {}));
  488. var Geometry = BABYLON.Geometry;
  489. })(BABYLON || (BABYLON = {}));
  490. //# sourceMappingURL=babylon.geometry.js.map