babylon.geometry.js 22 KB

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