babylon.meshSimplification.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var SimplificationSettings = (function () {
  4. function SimplificationSettings(quality, distance) {
  5. this.quality = quality;
  6. this.distance = distance;
  7. }
  8. return SimplificationSettings;
  9. })();
  10. BABYLON.SimplificationSettings = SimplificationSettings;
  11. var SimplificationQueue = (function () {
  12. function SimplificationQueue() {
  13. this.running = false;
  14. this._simplificationArray = [];
  15. }
  16. SimplificationQueue.prototype.addTask = function (task) {
  17. this._simplificationArray.push(task);
  18. };
  19. SimplificationQueue.prototype.executeNext = function () {
  20. var task = this._simplificationArray.pop();
  21. if (task) {
  22. this.running = true;
  23. this.runSimplification(task);
  24. }
  25. else {
  26. this.running = false;
  27. }
  28. };
  29. SimplificationQueue.prototype.runSimplification = function (task) {
  30. var _this = this;
  31. if (task.parallelProcessing) {
  32. //parallel simplifier
  33. task.settings.forEach(function (setting) {
  34. var simplifier = _this.getSimplifier(task);
  35. simplifier.simplify(setting, function (newMesh) {
  36. task.mesh.addLODLevel(setting.distance, newMesh);
  37. //check if it is the last
  38. if (setting.quality === task.settings[task.settings.length - 1].quality && task.successCallback) {
  39. //all done, run the success callback.
  40. task.successCallback();
  41. }
  42. _this.executeNext();
  43. });
  44. });
  45. }
  46. else {
  47. //single simplifier.
  48. var simplifier = this.getSimplifier(task);
  49. var runDecimation = function (setting, callback) {
  50. simplifier.simplify(setting, function (newMesh) {
  51. task.mesh.addLODLevel(setting.distance, newMesh);
  52. //run the next quality level
  53. callback();
  54. });
  55. };
  56. BABYLON.AsyncLoop.Run(task.settings.length, function (loop) {
  57. runDecimation(task.settings[loop.index], function () {
  58. loop.executeNext();
  59. });
  60. }, function () {
  61. //execution ended, run the success callback.
  62. if (task.successCallback) {
  63. task.successCallback();
  64. }
  65. _this.executeNext();
  66. });
  67. }
  68. };
  69. SimplificationQueue.prototype.getSimplifier = function (task) {
  70. switch (task.simplificationType) {
  71. case 0 /* QUADRATIC */:
  72. default:
  73. return new QuadraticErrorSimplification(task.mesh);
  74. }
  75. };
  76. return SimplificationQueue;
  77. })();
  78. BABYLON.SimplificationQueue = SimplificationQueue;
  79. /**
  80. * The implemented types of simplification.
  81. * At the moment only Quadratic Error Decimation is implemented.
  82. */
  83. (function (SimplificationType) {
  84. SimplificationType[SimplificationType["QUADRATIC"] = 0] = "QUADRATIC";
  85. })(BABYLON.SimplificationType || (BABYLON.SimplificationType = {}));
  86. var SimplificationType = BABYLON.SimplificationType;
  87. var DecimationTriangle = (function () {
  88. function DecimationTriangle(vertices) {
  89. this.vertices = vertices;
  90. this.error = new Array(4);
  91. this.deleted = false;
  92. this.isDirty = false;
  93. this.borderFactor = 0;
  94. }
  95. return DecimationTriangle;
  96. })();
  97. BABYLON.DecimationTriangle = DecimationTriangle;
  98. var DecimationVertex = (function () {
  99. function DecimationVertex(position, normal, uv, id) {
  100. this.position = position;
  101. this.normal = normal;
  102. this.uv = uv;
  103. this.id = id;
  104. this.isBorder = true;
  105. this.q = new QuadraticMatrix();
  106. this.triangleCount = 0;
  107. this.triangleStart = 0;
  108. }
  109. return DecimationVertex;
  110. })();
  111. BABYLON.DecimationVertex = DecimationVertex;
  112. var QuadraticMatrix = (function () {
  113. function QuadraticMatrix(data) {
  114. this.data = new Array(10);
  115. for (var i = 0; i < 10; ++i) {
  116. if (data && data[i]) {
  117. this.data[i] = data[i];
  118. }
  119. else {
  120. this.data[i] = 0;
  121. }
  122. }
  123. }
  124. QuadraticMatrix.prototype.det = function (a11, a12, a13, a21, a22, a23, a31, a32, a33) {
  125. var det = this.data[a11] * this.data[a22] * this.data[a33] + this.data[a13] * this.data[a21] * this.data[a32] + this.data[a12] * this.data[a23] * this.data[a31] - this.data[a13] * this.data[a22] * this.data[a31] - this.data[a11] * this.data[a23] * this.data[a32] - this.data[a12] * this.data[a21] * this.data[a33];
  126. return det;
  127. };
  128. QuadraticMatrix.prototype.addInPlace = function (matrix) {
  129. for (var i = 0; i < 10; ++i) {
  130. this.data[i] += matrix.data[i];
  131. }
  132. };
  133. QuadraticMatrix.prototype.addArrayInPlace = function (data) {
  134. for (var i = 0; i < 10; ++i) {
  135. this.data[i] += data[i];
  136. }
  137. };
  138. QuadraticMatrix.prototype.add = function (matrix) {
  139. var m = new QuadraticMatrix();
  140. for (var i = 0; i < 10; ++i) {
  141. m.data[i] = this.data[i] + matrix.data[i];
  142. }
  143. return m;
  144. };
  145. QuadraticMatrix.FromData = function (a, b, c, d) {
  146. return new QuadraticMatrix(QuadraticMatrix.DataFromNumbers(a, b, c, d));
  147. };
  148. //returning an array to avoid garbage collection
  149. QuadraticMatrix.DataFromNumbers = function (a, b, c, d) {
  150. return [a * a, a * b, a * c, a * d, b * b, b * c, b * d, c * c, c * d, d * d];
  151. };
  152. return QuadraticMatrix;
  153. })();
  154. BABYLON.QuadraticMatrix = QuadraticMatrix;
  155. var Reference = (function () {
  156. function Reference(vertexId, triangleId) {
  157. this.vertexId = vertexId;
  158. this.triangleId = triangleId;
  159. }
  160. return Reference;
  161. })();
  162. BABYLON.Reference = Reference;
  163. /**
  164. * An implementation of the Quadratic Error simplification algorithm.
  165. * Original paper : http://www1.cs.columbia.edu/~cs4162/html05s/garland97.pdf
  166. * Ported mostly from QSlim and http://voxels.blogspot.de/2014/05/quadric-mesh-simplification-with-source.html to babylon JS
  167. * @author RaananW
  168. */
  169. var QuadraticErrorSimplification = (function () {
  170. function QuadraticErrorSimplification(_mesh) {
  171. this._mesh = _mesh;
  172. this.initialised = false;
  173. this.syncIterations = 5000;
  174. this.aggressiveness = 7;
  175. this.decimationIterations = 100;
  176. this.boundingBoxEpsilon = BABYLON.Engine.Epsilon;
  177. }
  178. QuadraticErrorSimplification.prototype.simplify = function (settings, successCallback) {
  179. var _this = this;
  180. this.initWithMesh(this._mesh, function () {
  181. _this.runDecimation(settings, successCallback);
  182. });
  183. };
  184. QuadraticErrorSimplification.prototype.isTriangleOnBoundingBox = function (triangle) {
  185. var _this = this;
  186. var gCount = 0;
  187. triangle.vertices.forEach(function (vId) {
  188. var count = 0;
  189. var vPos = _this.vertices[vId].position;
  190. var bbox = _this._mesh.getBoundingInfo().boundingBox;
  191. if (bbox.maximum.x - vPos.x < _this.boundingBoxEpsilon || vPos.x - bbox.minimum.x > _this.boundingBoxEpsilon)
  192. ++count;
  193. if (bbox.maximum.y == vPos.y || vPos.y == bbox.minimum.y)
  194. ++count;
  195. if (bbox.maximum.z == vPos.z || vPos.z == bbox.minimum.z)
  196. ++count;
  197. if (count > 1) {
  198. ++gCount;
  199. }
  200. ;
  201. });
  202. if (gCount > 1) {
  203. console.log(triangle, gCount);
  204. }
  205. return gCount > 1;
  206. };
  207. QuadraticErrorSimplification.prototype.runDecimation = function (settings, successCallback) {
  208. var _this = this;
  209. var targetCount = ~~(this.triangles.length * settings.quality);
  210. var deletedTriangles = 0;
  211. var triangleCount = this.triangles.length;
  212. var iterationFunction = function (iteration, callback) {
  213. setTimeout(function () {
  214. if (iteration % 5 === 0) {
  215. _this.updateMesh(iteration === 0);
  216. }
  217. for (var i = 0; i < _this.triangles.length; ++i) {
  218. _this.triangles[i].isDirty = false;
  219. }
  220. var threshold = 0.000000001 * Math.pow((iteration + 3), _this.aggressiveness);
  221. var trianglesIterator = function (i) {
  222. var tIdx = ~~(((_this.triangles.length / 2) + i) % _this.triangles.length);
  223. var t = _this.triangles[tIdx];
  224. if (!t)
  225. return;
  226. if (t.error[3] > threshold || t.deleted || t.isDirty) {
  227. return;
  228. }
  229. for (var j = 0; j < 3; ++j) {
  230. if (t.error[j] < threshold) {
  231. var deleted0 = [];
  232. var deleted1 = [];
  233. var i0 = t.vertices[j];
  234. var i1 = t.vertices[(j + 1) % 3];
  235. var v0 = _this.vertices[i0];
  236. var v1 = _this.vertices[i1];
  237. if (v0.isBorder !== v1.isBorder)
  238. continue;
  239. var p = BABYLON.Vector3.Zero();
  240. var n = BABYLON.Vector3.Zero();
  241. var uv = BABYLON.Vector2.Zero();
  242. var color = new BABYLON.Color4(0, 0, 0, 1);
  243. _this.calculateError(v0, v1, p, n, uv, color);
  244. var delTr = [];
  245. if (_this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr))
  246. continue;
  247. if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr))
  248. continue;
  249. if (delTr.length == 2 || delTr[0] === delTr[1]) {
  250. continue;
  251. }
  252. v0.normal = n;
  253. if (v0.uv)
  254. v0.uv = uv;
  255. else if (v0.color)
  256. v0.color = color;
  257. v0.q = v1.q.add(v0.q);
  258. if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
  259. continue;
  260. if (p.equals(v0.position))
  261. continue;
  262. v0.position = p;
  263. var tStart = _this.references.length;
  264. deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
  265. deletedTriangles = _this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
  266. var tCount = _this.references.length - tStart;
  267. if (tCount <= v0.triangleCount) {
  268. if (tCount) {
  269. for (var c = 0; c < tCount; c++) {
  270. _this.references[v0.triangleStart + c] = _this.references[tStart + c];
  271. }
  272. }
  273. }
  274. else {
  275. v0.triangleStart = tStart;
  276. }
  277. v0.triangleCount = tCount;
  278. break;
  279. }
  280. }
  281. };
  282. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, trianglesIterator, callback, function () {
  283. return (triangleCount - deletedTriangles <= targetCount);
  284. });
  285. }, 0);
  286. };
  287. BABYLON.AsyncLoop.Run(this.decimationIterations, function (loop) {
  288. if (triangleCount - deletedTriangles <= targetCount)
  289. loop.breakLoop();
  290. else {
  291. iterationFunction(loop.index, function () {
  292. loop.executeNext();
  293. });
  294. }
  295. }, function () {
  296. setTimeout(function () {
  297. successCallback(_this.reconstructMesh());
  298. }, 0);
  299. });
  300. };
  301. QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, callback) {
  302. var _this = this;
  303. if (!mesh)
  304. return;
  305. this.vertices = [];
  306. this.triangles = [];
  307. this._mesh = mesh;
  308. //It is assumed that a mesh has positions, normals and either uvs or colors.
  309. var positionData = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  310. var normalData = this._mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  311. var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  312. var colorsData = this._mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  313. var indices = mesh.getIndices();
  314. var vertexInit = function (i) {
  315. var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, i * 3), BABYLON.Vector3.FromArray(normalData, i * 3), null, i);
  316. if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  317. vertex.uv = BABYLON.Vector2.FromArray(uvs, i * 2);
  318. }
  319. else if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  320. vertex.color = BABYLON.Color4.FromArray(colorsData, i * 4);
  321. }
  322. _this.vertices.push(vertex);
  323. };
  324. var totalVertices = mesh.getTotalVertices();
  325. BABYLON.AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, function () {
  326. var indicesInit = function (i) {
  327. var pos = i * 3;
  328. var i0 = indices[pos + 0];
  329. var i1 = indices[pos + 1];
  330. var i2 = indices[pos + 2];
  331. var triangle = new DecimationTriangle([_this.vertices[i0].id, _this.vertices[i1].id, _this.vertices[i2].id]);
  332. _this.triangles.push(triangle);
  333. };
  334. BABYLON.AsyncLoop.SyncAsyncForLoop(indices.length / 3, _this.syncIterations, indicesInit, function () {
  335. _this.init(callback);
  336. });
  337. });
  338. };
  339. QuadraticErrorSimplification.prototype.init = function (callback) {
  340. var _this = this;
  341. var triangleInit1 = function (i) {
  342. var t = _this.triangles[i];
  343. t.normal = BABYLON.Vector3.Cross(_this.vertices[t.vertices[1]].position.subtract(_this.vertices[t.vertices[0]].position), _this.vertices[t.vertices[2]].position.subtract(_this.vertices[t.vertices[0]].position)).normalize();
  344. for (var j = 0; j < 3; j++) {
  345. _this.vertices[t.vertices[j]].q.addArrayInPlace(QuadraticMatrix.DataFromNumbers(t.normal.x, t.normal.y, t.normal.z, -(BABYLON.Vector3.Dot(t.normal, _this.vertices[t.vertices[0]].position))));
  346. }
  347. };
  348. BABYLON.AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1, function () {
  349. var triangleInit2 = function (i) {
  350. var t = _this.triangles[i];
  351. for (var j = 0; j < 3; ++j) {
  352. t.error[j] = _this.calculateError(_this.vertices[t.vertices[j]], _this.vertices[t.vertices[(j + 1) % 3]]);
  353. }
  354. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  355. };
  356. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, triangleInit2, function () {
  357. _this.initialised = true;
  358. callback();
  359. });
  360. });
  361. };
  362. QuadraticErrorSimplification.prototype.reconstructMesh = function () {
  363. var newTriangles = [];
  364. var i;
  365. for (i = 0; i < this.vertices.length; ++i) {
  366. this.vertices[i].triangleCount = 0;
  367. }
  368. var t;
  369. var j;
  370. for (i = 0; i < this.triangles.length; ++i) {
  371. if (!this.triangles[i].deleted) {
  372. t = this.triangles[i];
  373. for (j = 0; j < 3; ++j) {
  374. this.vertices[t.vertices[j]].triangleCount = 1;
  375. }
  376. newTriangles.push(t);
  377. }
  378. }
  379. var newVerticesOrder = [];
  380. //compact vertices, get the IDs of the vertices used.
  381. var dst = 0;
  382. for (i = 0; i < this.vertices.length; ++i) {
  383. if (this.vertices[i].triangleCount) {
  384. this.vertices[i].triangleStart = dst;
  385. this.vertices[dst].position = this.vertices[i].position;
  386. this.vertices[dst].normal = this.vertices[i].normal;
  387. this.vertices[dst].uv = this.vertices[i].uv;
  388. this.vertices[dst].color = this.vertices[i].color;
  389. newVerticesOrder.push(i);
  390. dst++;
  391. }
  392. }
  393. for (i = 0; i < newTriangles.length; ++i) {
  394. t = newTriangles[i];
  395. for (j = 0; j < 3; ++j) {
  396. t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
  397. }
  398. }
  399. this.vertices = this.vertices.slice(0, dst);
  400. var newPositionData = [];
  401. var newNormalData = [];
  402. var newUVsData = [];
  403. var newColorsData = [];
  404. for (i = 0; i < newVerticesOrder.length; ++i) {
  405. newPositionData.push(this.vertices[i].position.x);
  406. newPositionData.push(this.vertices[i].position.y);
  407. newPositionData.push(this.vertices[i].position.z);
  408. newNormalData.push(this.vertices[i].normal.x);
  409. newNormalData.push(this.vertices[i].normal.y);
  410. newNormalData.push(this.vertices[i].normal.z);
  411. if (this.vertices[i].uv) {
  412. newUVsData.push(this.vertices[i].uv.x);
  413. newUVsData.push(this.vertices[i].uv.y);
  414. }
  415. else if (this.vertices[i].color) {
  416. newColorsData.push(this.vertices[i].color.r);
  417. newColorsData.push(this.vertices[i].color.g);
  418. newColorsData.push(this.vertices[i].color.b);
  419. newColorsData.push(this.vertices[i].color.a);
  420. }
  421. }
  422. var newIndicesArray = [];
  423. for (i = 0; i < newTriangles.length; ++i) {
  424. newIndicesArray.push(newTriangles[i].vertices[0]);
  425. newIndicesArray.push(newTriangles[i].vertices[1]);
  426. newIndicesArray.push(newTriangles[i].vertices[2]);
  427. }
  428. //not cloning, to avoid geometry problems. Creating a whole new mesh.
  429. var newMesh = new BABYLON.Mesh(this._mesh.name + "Decimated", this._mesh.getScene());
  430. newMesh.material = this._mesh.material;
  431. newMesh.parent = this._mesh.parent;
  432. newMesh.setIndices(newIndicesArray);
  433. newMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, newPositionData);
  434. newMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormalData);
  435. if (newUVsData.length > 0)
  436. newMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, newUVsData);
  437. if (newColorsData.length > 0)
  438. newMesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, newColorsData);
  439. //preparing the skeleton support
  440. if (this._mesh.skeleton) {
  441. }
  442. return newMesh;
  443. };
  444. QuadraticErrorSimplification.prototype.isFlipped = function (vertex1, index2, point, deletedArray, borderFactor, delTr) {
  445. for (var i = 0; i < vertex1.triangleCount; ++i) {
  446. var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
  447. if (t.deleted)
  448. continue;
  449. var s = this.references[vertex1.triangleStart + i].vertexId;
  450. var id1 = t.vertices[(s + 1) % 3];
  451. var id2 = t.vertices[(s + 2) % 3];
  452. if ((id1 === index2 || id2 === index2)) {
  453. deletedArray[i] = true;
  454. delTr.push(t);
  455. continue;
  456. }
  457. var d1 = this.vertices[id1].position.subtract(point);
  458. d1 = d1.normalize();
  459. var d2 = this.vertices[id2].position.subtract(point);
  460. d2 = d2.normalize();
  461. if (Math.abs(BABYLON.Vector3.Dot(d1, d2)) > 0.999)
  462. return true;
  463. var normal = BABYLON.Vector3.Cross(d1, d2).normalize();
  464. deletedArray[i] = false;
  465. if (BABYLON.Vector3.Dot(normal, t.normal) < 0.2)
  466. return true;
  467. }
  468. return false;
  469. };
  470. QuadraticErrorSimplification.prototype.updateTriangles = function (vertexId, vertex, deletedArray, deletedTriangles) {
  471. var newDeleted = deletedTriangles;
  472. for (var i = 0; i < vertex.triangleCount; ++i) {
  473. var ref = this.references[vertex.triangleStart + i];
  474. var t = this.triangles[ref.triangleId];
  475. if (t.deleted)
  476. continue;
  477. if (deletedArray[i]) {
  478. t.deleted = true;
  479. newDeleted++;
  480. continue;
  481. }
  482. t.vertices[ref.vertexId] = vertexId;
  483. t.isDirty = true;
  484. t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
  485. t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
  486. t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
  487. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  488. this.references.push(ref);
  489. }
  490. return newDeleted;
  491. };
  492. QuadraticErrorSimplification.prototype.identifyBorder = function () {
  493. for (var i = 0; i < this.vertices.length; ++i) {
  494. var vCount = [];
  495. var vId = [];
  496. var v = this.vertices[i];
  497. var j;
  498. for (j = 0; j < v.triangleCount; ++j) {
  499. var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
  500. for (var ii = 0; ii < 3; ii++) {
  501. var ofs = 0;
  502. var id = triangle.vertices[ii];
  503. while (ofs < vCount.length) {
  504. if (vId[ofs] === id)
  505. break;
  506. ++ofs;
  507. }
  508. if (ofs === vCount.length) {
  509. vCount.push(1);
  510. vId.push(id);
  511. }
  512. else {
  513. vCount[ofs]++;
  514. }
  515. }
  516. }
  517. for (j = 0; j < vCount.length; ++j) {
  518. if (vCount[j] === 1) {
  519. this.vertices[vId[j]].isBorder = true;
  520. }
  521. else {
  522. this.vertices[vId[j]].isBorder = false;
  523. }
  524. }
  525. }
  526. };
  527. QuadraticErrorSimplification.prototype.updateMesh = function (identifyBorders) {
  528. if (identifyBorders === void 0) { identifyBorders = false; }
  529. var i;
  530. if (!identifyBorders) {
  531. var newTrianglesVector = [];
  532. for (i = 0; i < this.triangles.length; ++i) {
  533. if (!this.triangles[i].deleted) {
  534. newTrianglesVector.push(this.triangles[i]);
  535. }
  536. }
  537. this.triangles = newTrianglesVector;
  538. }
  539. for (i = 0; i < this.vertices.length; ++i) {
  540. this.vertices[i].triangleCount = 0;
  541. this.vertices[i].triangleStart = 0;
  542. }
  543. var t;
  544. var j;
  545. var v;
  546. for (i = 0; i < this.triangles.length; ++i) {
  547. t = this.triangles[i];
  548. for (j = 0; j < 3; ++j) {
  549. v = this.vertices[t.vertices[j]];
  550. v.triangleCount++;
  551. }
  552. }
  553. var tStart = 0;
  554. for (i = 0; i < this.vertices.length; ++i) {
  555. this.vertices[i].triangleStart = tStart;
  556. tStart += this.vertices[i].triangleCount;
  557. this.vertices[i].triangleCount = 0;
  558. }
  559. var newReferences = new Array(this.triangles.length * 3);
  560. for (i = 0; i < this.triangles.length; ++i) {
  561. t = this.triangles[i];
  562. for (j = 0; j < 3; ++j) {
  563. v = this.vertices[t.vertices[j]];
  564. newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
  565. v.triangleCount++;
  566. }
  567. }
  568. this.references = newReferences;
  569. if (identifyBorders) {
  570. this.identifyBorder();
  571. }
  572. };
  573. QuadraticErrorSimplification.prototype.vertexError = function (q, point) {
  574. var x = point.x;
  575. var y = point.y;
  576. var z = point.z;
  577. return q.data[0] * x * x + 2 * q.data[1] * x * y + 2 * q.data[2] * x * z + 2 * q.data[3] * x + q.data[4] * y * y + 2 * q.data[5] * y * z + 2 * q.data[6] * y + q.data[7] * z * z + 2 * q.data[8] * z + q.data[9];
  578. };
  579. QuadraticErrorSimplification.prototype.calculateError = function (vertex1, vertex2, pointResult, normalResult, uvResult, colorResult) {
  580. var q = vertex1.q.add(vertex2.q);
  581. var border = vertex1.isBorder && vertex2.isBorder;
  582. var error = 0;
  583. var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
  584. if (qDet !== 0 && !border) {
  585. if (!pointResult) {
  586. pointResult = BABYLON.Vector3.Zero();
  587. }
  588. pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
  589. pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
  590. pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
  591. error = this.vertexError(q, pointResult);
  592. //TODO this should be correctly calculated
  593. if (normalResult) {
  594. normalResult.copyFrom(vertex1.normal);
  595. if (vertex1.uv)
  596. uvResult.copyFrom(vertex1.uv);
  597. else if (vertex1.color)
  598. colorResult.copyFrom(vertex1.color);
  599. }
  600. }
  601. else {
  602. var p3 = (vertex1.position.add(vertex2.position)).divide(new BABYLON.Vector3(2, 2, 2));
  603. //var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new Vector3(2, 2, 2)).normalize();
  604. var error1 = this.vertexError(q, vertex1.position);
  605. var error2 = this.vertexError(q, vertex2.position);
  606. var error3 = this.vertexError(q, p3);
  607. error = Math.min(error1, error2, error3);
  608. if (error === error1) {
  609. if (pointResult) {
  610. pointResult.copyFrom(vertex1.position);
  611. normalResult.copyFrom(vertex1.normal);
  612. if (vertex1.uv)
  613. uvResult.copyFrom(vertex1.uv);
  614. else if (vertex1.color)
  615. colorResult.copyFrom(vertex1.color);
  616. }
  617. }
  618. else if (error === error2) {
  619. if (pointResult) {
  620. pointResult.copyFrom(vertex2.position);
  621. normalResult.copyFrom(vertex2.normal);
  622. if (vertex2.uv)
  623. uvResult.copyFrom(vertex2.uv);
  624. else if (vertex2.color)
  625. colorResult.copyFrom(vertex2.color);
  626. }
  627. }
  628. else {
  629. if (pointResult) {
  630. pointResult.copyFrom(p3);
  631. normalResult.copyFrom(vertex1.normal);
  632. if (vertex1.uv)
  633. uvResult.copyFrom(vertex1.uv);
  634. else if (vertex1.color)
  635. colorResult.copyFrom(vertex1.color);
  636. }
  637. }
  638. }
  639. return error;
  640. };
  641. return QuadraticErrorSimplification;
  642. })();
  643. BABYLON.QuadraticErrorSimplification = QuadraticErrorSimplification;
  644. })(BABYLON || (BABYLON = {}));
  645. //# sourceMappingURL=babylon.meshSimplification.js.map