babylon.meshSimplification.js 30 KB

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