babylon.geometry.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 = this.getIndices(); // todo: clone
  273. var updatable = false;
  274. var stopChecking = false;
  275. for (var kind in this._vertexBuffers) {
  276. vertexData.set(this.getVerticesData(kind), kind);
  277. if (!stopChecking) {
  278. updatable = this.getVertexBuffer(kind).isUpdatable();
  279. stopChecking = !updatable;
  280. }
  281. }
  282. var geometry = new BABYLON.Geometry(id, this._engine, vertexData, updatable, null);
  283. geometry.delayLoadState = this.delayLoadState;
  284. geometry.delayLoadingFile = this.delayLoadingFile;
  285. geometry._delayLoadingFunction = this._delayLoadingFunction;
  286. for (var kind in this._delayInfo) {
  287. geometry._delayInfo = geometry._delayInfo || [];
  288. geometry._delayInfo.push(kind);
  289. }
  290. // Bounding info
  291. var extend = BABYLON.Tools.ExtractMinAndMax(this.getVerticesData(BABYLON.VertexBuffer.PositionKind), 0, this.getTotalVertices());
  292. geometry._boundingInfo = new BABYLON.BoundingInfo(extend.minimum, extend.maximum);
  293. return geometry;
  294. };
  295. // Statics
  296. Geometry.ExtractFromMesh = function (mesh, id) {
  297. var geometry = mesh._geometry;
  298. if (!geometry) {
  299. return null;
  300. }
  301. return geometry.copy(id);
  302. };
  303. // from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  304. // be aware Math.random() could cause collisions
  305. Geometry.RandomId = function () {
  306. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  307. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  308. return v.toString(16);
  309. });
  310. };
  311. return Geometry;
  312. })();
  313. BABYLON.Geometry = Geometry;
  314. (function (Geometry) {
  315. /////// Primitives //////////////////////////////////////////////
  316. (function (Primitives) {
  317. /// Abstract class
  318. var _Primitive = (function (_super) {
  319. __extends(_Primitive, _super);
  320. function _Primitive(id, engine, vertexData, canBeRegenerated, mesh) {
  321. this._beingRegenerated = true;
  322. this._canBeRegenerated = canBeRegenerated;
  323. _super.call(this, id, engine, vertexData, false, mesh); // updatable = false to be sure not to update vertices
  324. this._beingRegenerated = false;
  325. }
  326. _Primitive.prototype.canBeRegenerated = function () {
  327. return this._canBeRegenerated;
  328. };
  329. _Primitive.prototype.regenerate = function () {
  330. if (!this._canBeRegenerated) {
  331. return;
  332. }
  333. this._beingRegenerated = true;
  334. this.setAllVerticesData(this._regenerateVertexData(), false);
  335. this._beingRegenerated = false;
  336. };
  337. _Primitive.prototype.asNewGeometry = function (id) {
  338. return _super.prototype.copy.call(this, id);
  339. };
  340. // overrides
  341. _Primitive.prototype.setAllVerticesData = function (vertexData, updatable) {
  342. if (!this._beingRegenerated) {
  343. return;
  344. }
  345. return _super.prototype.setAllVerticesData.call(this, vertexData, false);
  346. };
  347. _Primitive.prototype.setVerticesData = function (data, kind, updatable) {
  348. if (!this._beingRegenerated) {
  349. return;
  350. }
  351. _super.prototype.setVerticesData.call(this, data, kind, false);
  352. };
  353. // to override
  354. // protected
  355. _Primitive.prototype._regenerateVertexData = function () {
  356. throw new Error("Abstract method");
  357. };
  358. _Primitive.prototype.copy = function (id) {
  359. throw new Error("Must be overriden in sub-classes.");
  360. };
  361. return _Primitive;
  362. })(Geometry);
  363. Primitives._Primitive = _Primitive;
  364. var Box = (function (_super) {
  365. __extends(Box, _super);
  366. function Box(id, engine, size, canBeRegenerated, mesh) {
  367. this.size = size;
  368. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  369. }
  370. Box.prototype._regenerateVertexData = function () {
  371. return BABYLON.VertexData.CreateBox(this.size);
  372. };
  373. Box.prototype.copy = function (id) {
  374. return new Box(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  375. };
  376. return Box;
  377. })(_Primitive);
  378. Primitives.Box = Box;
  379. var Sphere = (function (_super) {
  380. __extends(Sphere, _super);
  381. function Sphere(id, engine, segments, diameter, canBeRegenerated, mesh) {
  382. this.segments = segments;
  383. this.diameter = diameter;
  384. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  385. }
  386. Sphere.prototype._regenerateVertexData = function () {
  387. return BABYLON.VertexData.CreateSphere(this.segments, this.diameter);
  388. };
  389. Sphere.prototype.copy = function (id) {
  390. return new Sphere(id, this.getEngine(), this.segments, this.diameter, this.canBeRegenerated(), null);
  391. };
  392. return Sphere;
  393. })(_Primitive);
  394. Primitives.Sphere = Sphere;
  395. var Cylinder = (function (_super) {
  396. __extends(Cylinder, _super);
  397. function Cylinder(id, engine, height, diameterTop, diameterBottom, tessellation, canBeRegenerated, mesh) {
  398. this.height = height;
  399. this.diameterTop = diameterTop;
  400. this.diameterBottom = diameterBottom;
  401. this.tessellation = tessellation;
  402. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  403. }
  404. Cylinder.prototype._regenerateVertexData = function () {
  405. return BABYLON.VertexData.CreateCylinder(this.height, this.diameterTop, this.diameterBottom, this.tessellation);
  406. };
  407. Cylinder.prototype.copy = function (id) {
  408. return new Cylinder(id, this.getEngine(), this.height, this.diameterTop, this.diameterBottom, this.tessellation, this.canBeRegenerated(), null);
  409. };
  410. return Cylinder;
  411. })(_Primitive);
  412. Primitives.Cylinder = Cylinder;
  413. var Torus = (function (_super) {
  414. __extends(Torus, _super);
  415. function Torus(id, engine, diameter, thickness, tessellation, canBeRegenerated, mesh) {
  416. this.diameter = diameter;
  417. this.thickness = thickness;
  418. this.tessellation = tessellation;
  419. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  420. }
  421. Torus.prototype._regenerateVertexData = function () {
  422. return BABYLON.VertexData.CreateTorus(this.diameter, this.thickness, this.tessellation);
  423. };
  424. Torus.prototype.copy = function (id) {
  425. return new Torus(id, this.getEngine(), this.diameter, this.thickness, this.tessellation, this.canBeRegenerated(), null);
  426. };
  427. return Torus;
  428. })(_Primitive);
  429. Primitives.Torus = Torus;
  430. var Ground = (function (_super) {
  431. __extends(Ground, _super);
  432. function Ground(id, engine, width, height, subdivisions, canBeRegenerated, mesh) {
  433. this.width = width;
  434. this.height = height;
  435. this.subdivisions = subdivisions;
  436. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  437. }
  438. Ground.prototype._regenerateVertexData = function () {
  439. return BABYLON.VertexData.CreateGround(this.width, this.height, this.subdivisions);
  440. };
  441. Ground.prototype.copy = function (id) {
  442. return new Ground(id, this.getEngine(), this.width, this.height, this.subdivisions, this.canBeRegenerated(), null);
  443. };
  444. return Ground;
  445. })(_Primitive);
  446. Primitives.Ground = Ground;
  447. var Plane = (function (_super) {
  448. __extends(Plane, _super);
  449. function Plane(id, engine, size, canBeRegenerated, mesh) {
  450. this.size = size;
  451. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  452. }
  453. Plane.prototype._regenerateVertexData = function () {
  454. return BABYLON.VertexData.CreatePlane(this.size);
  455. };
  456. Plane.prototype.copy = function (id) {
  457. return new Plane(id, this.getEngine(), this.size, this.canBeRegenerated(), null);
  458. };
  459. return Plane;
  460. })(_Primitive);
  461. Primitives.Plane = Plane;
  462. var TorusKnot = (function (_super) {
  463. __extends(TorusKnot, _super);
  464. function TorusKnot(id, engine, radius, tube, radialSegments, tubularSegments, p, q, canBeRegenerated, mesh) {
  465. this.radius = radius;
  466. this.tube = tube;
  467. this.radialSegments = radialSegments;
  468. this.tubularSegments = tubularSegments;
  469. this.p = p;
  470. this.q = q;
  471. _super.call(this, id, engine, this._regenerateVertexData(), canBeRegenerated, mesh);
  472. }
  473. TorusKnot.prototype._regenerateVertexData = function () {
  474. return BABYLON.VertexData.CreateTorusKnot(this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q);
  475. };
  476. TorusKnot.prototype.copy = function (id) {
  477. return new TorusKnot(id, this.getEngine(), this.radius, this.tube, this.radialSegments, this.tubularSegments, this.p, this.q, this.canBeRegenerated(), null);
  478. };
  479. return TorusKnot;
  480. })(_Primitive);
  481. Primitives.TorusKnot = TorusKnot;
  482. })(Geometry.Primitives || (Geometry.Primitives = {}));
  483. var Primitives = Geometry.Primitives;
  484. })(BABYLON.Geometry || (BABYLON.Geometry = {}));
  485. var Geometry = BABYLON.Geometry;
  486. })(BABYLON || (BABYLON = {}));
  487. //# sourceMappingURL=babylon.geometry.js.map