babylon.meshSimplification.js 31 KB

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