babylon.meshSimplification.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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.initialised = 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(_this._mesh, 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 (mesh, submeshIndex, callback) {
  323. var _this = this;
  324. if (!mesh)
  325. return;
  326. this.vertices = [];
  327. this.triangles = [];
  328. this._mesh = mesh;
  329. //It is assumed that a mesh has positions, normals and either uvs or colors.
  330. var positionData = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  331. var indices = mesh.getIndices();
  332. var submesh = mesh.subMeshes[submeshIndex];
  333. var vertexInit = function (i) {
  334. var offset = i + submesh.verticesStart;
  335. var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, offset * 3), offset, i);
  336. _this.vertices.push(vertex);
  337. };
  338. //var totalVertices = mesh.getTotalVertices();
  339. var totalVertices = submesh.verticesCount;
  340. BABYLON.AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, function () {
  341. var indicesInit = function (i) {
  342. var offset = (submesh.indexStart / 3) + i;
  343. var pos = (offset * 3);
  344. var i0 = indices[pos + 0] - submesh.verticesStart;
  345. var i1 = indices[pos + 1] - submesh.verticesStart;
  346. var i2 = indices[pos + 2] - submesh.verticesStart;
  347. var triangle = new DecimationTriangle([_this.vertices[i0].id, _this.vertices[i1].id, _this.vertices[i2].id]);
  348. _this.triangles.push(triangle);
  349. };
  350. BABYLON.AsyncLoop.SyncAsyncForLoop(submesh.indexCount / 3, _this.syncIterations, indicesInit, function () {
  351. _this.init(callback);
  352. });
  353. });
  354. };
  355. QuadraticErrorSimplification.prototype.init = function (callback) {
  356. var _this = this;
  357. var triangleInit1 = function (i) {
  358. var t = _this.triangles[i];
  359. 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();
  360. for (var j = 0; j < 3; j++) {
  361. _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))));
  362. }
  363. };
  364. BABYLON.AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1, function () {
  365. var triangleInit2 = function (i) {
  366. var t = _this.triangles[i];
  367. for (var j = 0; j < 3; ++j) {
  368. t.error[j] = _this.calculateError(_this.vertices[t.vertices[j]], _this.vertices[t.vertices[(j + 1) % 3]]);
  369. }
  370. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  371. };
  372. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, triangleInit2, function () {
  373. _this.initialised = true;
  374. callback();
  375. });
  376. });
  377. };
  378. QuadraticErrorSimplification.prototype.reconstructMesh = function (submeshIndex) {
  379. var newTriangles = [];
  380. var i;
  381. for (i = 0; i < this.vertices.length; ++i) {
  382. this.vertices[i].triangleCount = 0;
  383. }
  384. var t;
  385. var j;
  386. for (i = 0; i < this.triangles.length; ++i) {
  387. if (!this.triangles[i].deleted) {
  388. t = this.triangles[i];
  389. for (j = 0; j < 3; ++j) {
  390. this.vertices[t.vertices[j]].triangleCount = 1;
  391. }
  392. newTriangles.push(t);
  393. }
  394. }
  395. var newVerticesOrder = [];
  396. //compact vertices, get the IDs of the vertices used.
  397. var dst = 0;
  398. for (i = 0; i < this.vertices.length; ++i) {
  399. if (this.vertices[i].triangleCount) {
  400. this.vertices[i].triangleStart = dst;
  401. this.vertices[dst].position = this.vertices[i].position;
  402. newVerticesOrder.push(dst);
  403. dst++;
  404. }
  405. }
  406. for (i = 0; i < newTriangles.length; ++i) {
  407. t = newTriangles[i];
  408. for (j = 0; j < 3; ++j) {
  409. t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
  410. }
  411. }
  412. this.vertices = this.vertices.slice(0, dst);
  413. var newPositionData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind) || [];
  414. var newNormalData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind) || [];
  415. var newUVsData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind) || [];
  416. var newColorsData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.ColorKind) || [];
  417. var normalData = this._mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  418. var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  419. var colorsData = this._mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  420. for (i = 0; i < newVerticesOrder.length; ++i) {
  421. newPositionData.push(this.vertices[i].position.x);
  422. newPositionData.push(this.vertices[i].position.y);
  423. newPositionData.push(this.vertices[i].position.z);
  424. newNormalData.push(normalData[this.vertices[i].originalPosition * 3]);
  425. newNormalData.push(normalData[(this.vertices[i].originalPosition * 3) + 1]);
  426. newNormalData.push(normalData[(this.vertices[i].originalPosition * 3) + 2]);
  427. if (uvs.length) {
  428. newUVsData.push(uvs[(this.vertices[i].originalPosition * 2)]);
  429. newUVsData.push(uvs[(this.vertices[i].originalPosition * 2) + 1]);
  430. }
  431. else if (colorsData.length) {
  432. newColorsData.push(colorsData[(this.vertices[i].originalPosition * 4)]);
  433. newColorsData.push(colorsData[(this.vertices[i].originalPosition * 4) + 1]);
  434. newColorsData.push(colorsData[(this.vertices[i].originalPosition * 4) + 2]);
  435. newColorsData.push(colorsData[(this.vertices[i].originalPosition * 4) + 3]);
  436. }
  437. }
  438. var startingIndex = this._reconstructedMesh.getTotalIndices();
  439. var startingVertex = this._reconstructedMesh.getTotalVertices();
  440. var submeshesArray = this._reconstructedMesh.subMeshes;
  441. this._reconstructedMesh.subMeshes = [];
  442. var newIndicesArray = this._reconstructedMesh.getIndices(); //[];
  443. for (i = 0; i < newTriangles.length; ++i) {
  444. newIndicesArray.push(newTriangles[i].vertices[0] + startingVertex);
  445. newIndicesArray.push(newTriangles[i].vertices[1] + startingVertex);
  446. newIndicesArray.push(newTriangles[i].vertices[2] + startingVertex);
  447. }
  448. //overwriting the old vertex buffers and indices.
  449. this._reconstructedMesh.setIndices(newIndicesArray);
  450. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, newPositionData);
  451. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormalData);
  452. if (newUVsData.length > 0)
  453. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, newUVsData);
  454. if (newColorsData.length > 0)
  455. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, newColorsData);
  456. //create submesh
  457. var originalSubmesh = this._mesh.subMeshes[submeshIndex];
  458. if (submeshIndex > 0) {
  459. this._reconstructedMesh.subMeshes = [];
  460. submeshesArray.forEach(function (submesh) {
  461. new BABYLON.SubMesh(submesh.materialIndex, submesh.verticesStart, submesh.verticesCount, submesh.indexStart, submesh.indexCount, submesh.getMesh());
  462. });
  463. var newSubmesh = new BABYLON.SubMesh(originalSubmesh.materialIndex, startingVertex, newVerticesOrder.length, startingIndex, newTriangles.length * 3, this._reconstructedMesh);
  464. }
  465. };
  466. QuadraticErrorSimplification.prototype.initDecimatedMesh = function () {
  467. this._reconstructedMesh = new BABYLON.Mesh(this._mesh.name + "Decimated", this._mesh.getScene());
  468. this._reconstructedMesh.material = this._mesh.material;
  469. this._reconstructedMesh.parent = this._mesh.parent;
  470. this._reconstructedMesh.isVisible = false;
  471. };
  472. QuadraticErrorSimplification.prototype.isFlipped = function (vertex1, index2, point, deletedArray, borderFactor, delTr) {
  473. for (var i = 0; i < vertex1.triangleCount; ++i) {
  474. var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
  475. if (t.deleted)
  476. continue;
  477. var s = this.references[vertex1.triangleStart + i].vertexId;
  478. var id1 = t.vertices[(s + 1) % 3];
  479. var id2 = t.vertices[(s + 2) % 3];
  480. if ((id1 === index2 || id2 === index2)) {
  481. deletedArray[i] = true;
  482. delTr.push(t);
  483. continue;
  484. }
  485. var d1 = this.vertices[id1].position.subtract(point);
  486. d1 = d1.normalize();
  487. var d2 = this.vertices[id2].position.subtract(point);
  488. d2 = d2.normalize();
  489. if (Math.abs(BABYLON.Vector3.Dot(d1, d2)) > 0.999)
  490. return true;
  491. var normal = BABYLON.Vector3.Cross(d1, d2).normalize();
  492. deletedArray[i] = false;
  493. if (BABYLON.Vector3.Dot(normal, t.normal) < 0.2)
  494. return true;
  495. }
  496. return false;
  497. };
  498. QuadraticErrorSimplification.prototype.updateTriangles = function (vertexId, vertex, deletedArray, deletedTriangles) {
  499. var newDeleted = deletedTriangles;
  500. for (var i = 0; i < vertex.triangleCount; ++i) {
  501. var ref = this.references[vertex.triangleStart + i];
  502. var t = this.triangles[ref.triangleId];
  503. if (t.deleted)
  504. continue;
  505. if (deletedArray[i] && t.deletePending) {
  506. t.deleted = true;
  507. newDeleted++;
  508. continue;
  509. }
  510. t.vertices[ref.vertexId] = vertexId;
  511. t.isDirty = true;
  512. t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
  513. t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
  514. t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
  515. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  516. this.references.push(ref);
  517. }
  518. return newDeleted;
  519. };
  520. QuadraticErrorSimplification.prototype.identifyBorder = function () {
  521. for (var i = 0; i < this.vertices.length; ++i) {
  522. var vCount = [];
  523. var vId = [];
  524. var v = this.vertices[i];
  525. var j;
  526. for (j = 0; j < v.triangleCount; ++j) {
  527. var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
  528. for (var ii = 0; ii < 3; ii++) {
  529. var ofs = 0;
  530. var id = triangle.vertices[ii];
  531. while (ofs < vCount.length) {
  532. if (vId[ofs] === id)
  533. break;
  534. ++ofs;
  535. }
  536. if (ofs === vCount.length) {
  537. vCount.push(1);
  538. vId.push(id);
  539. }
  540. else {
  541. vCount[ofs]++;
  542. }
  543. }
  544. }
  545. for (j = 0; j < vCount.length; ++j) {
  546. if (vCount[j] === 1) {
  547. this.vertices[vId[j]].isBorder = true;
  548. }
  549. else {
  550. this.vertices[vId[j]].isBorder = false;
  551. }
  552. }
  553. }
  554. };
  555. QuadraticErrorSimplification.prototype.updateMesh = function (identifyBorders) {
  556. if (identifyBorders === void 0) { identifyBorders = false; }
  557. var i;
  558. if (!identifyBorders) {
  559. var newTrianglesVector = [];
  560. for (i = 0; i < this.triangles.length; ++i) {
  561. if (!this.triangles[i].deleted) {
  562. newTrianglesVector.push(this.triangles[i]);
  563. }
  564. }
  565. this.triangles = newTrianglesVector;
  566. }
  567. for (i = 0; i < this.vertices.length; ++i) {
  568. this.vertices[i].triangleCount = 0;
  569. this.vertices[i].triangleStart = 0;
  570. }
  571. var t;
  572. var j;
  573. var v;
  574. for (i = 0; i < this.triangles.length; ++i) {
  575. t = this.triangles[i];
  576. for (j = 0; j < 3; ++j) {
  577. v = this.vertices[t.vertices[j]];
  578. v.triangleCount++;
  579. }
  580. }
  581. var tStart = 0;
  582. for (i = 0; i < this.vertices.length; ++i) {
  583. this.vertices[i].triangleStart = tStart;
  584. tStart += this.vertices[i].triangleCount;
  585. this.vertices[i].triangleCount = 0;
  586. }
  587. var newReferences = new Array(this.triangles.length * 3);
  588. for (i = 0; i < this.triangles.length; ++i) {
  589. t = this.triangles[i];
  590. for (j = 0; j < 3; ++j) {
  591. v = this.vertices[t.vertices[j]];
  592. newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
  593. v.triangleCount++;
  594. }
  595. }
  596. this.references = newReferences;
  597. if (identifyBorders) {
  598. this.identifyBorder();
  599. }
  600. };
  601. QuadraticErrorSimplification.prototype.vertexError = function (q, point) {
  602. var x = point.x;
  603. var y = point.y;
  604. var z = point.z;
  605. 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];
  606. };
  607. QuadraticErrorSimplification.prototype.calculateError = function (vertex1, vertex2, pointResult, normalResult, uvResult, colorResult) {
  608. var q = vertex1.q.add(vertex2.q);
  609. var border = vertex1.isBorder && vertex2.isBorder;
  610. var error = 0;
  611. var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
  612. if (qDet !== 0 && !border) {
  613. if (!pointResult) {
  614. pointResult = BABYLON.Vector3.Zero();
  615. }
  616. pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
  617. pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
  618. pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
  619. error = this.vertexError(q, pointResult);
  620. }
  621. else {
  622. var p3 = (vertex1.position.add(vertex2.position)).divide(new BABYLON.Vector3(2, 2, 2));
  623. //var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new Vector3(2, 2, 2)).normalize();
  624. var error1 = this.vertexError(q, vertex1.position);
  625. var error2 = this.vertexError(q, vertex2.position);
  626. var error3 = this.vertexError(q, p3);
  627. error = Math.min(error1, error2, error3);
  628. if (error === error1) {
  629. if (pointResult) {
  630. pointResult.copyFrom(vertex1.position);
  631. }
  632. }
  633. else if (error === error2) {
  634. if (pointResult) {
  635. pointResult.copyFrom(vertex2.position);
  636. }
  637. }
  638. else {
  639. if (pointResult) {
  640. pointResult.copyFrom(p3);
  641. }
  642. }
  643. }
  644. return error;
  645. };
  646. return QuadraticErrorSimplification;
  647. })();
  648. BABYLON.QuadraticErrorSimplification = QuadraticErrorSimplification;
  649. })(BABYLON || (BABYLON = {}));
  650. //# sourceMappingURL=babylon.meshSimplification.js.map