babylon.geometry.js 24 KB

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