babylon.meshSimplification.js 31 KB

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