babylon.geometry.js 24 KB

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