babylon.geometry.js 24 KB

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