babylon.geometry.js 24 KB

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