babylon.meshSimplification.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. function setLODLevel(distance, mesh) {
  32. }
  33. if (task.parallelProcessing) {
  34. //parallel simplifier
  35. task.settings.forEach(function (setting) {
  36. var simplifier = _this.getSimplifier(task);
  37. simplifier.simplify(setting, function (newMesh) {
  38. task.mesh.addLODLevel(setting.distance, newMesh);
  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. //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.borderFactor = 0;
  96. }
  97. return DecimationTriangle;
  98. })();
  99. BABYLON.DecimationTriangle = DecimationTriangle;
  100. var DecimationVertex = (function () {
  101. function DecimationVertex(position, normal, uv, id) {
  102. this.position = position;
  103. this.normal = normal;
  104. this.uv = uv;
  105. this.id = id;
  106. this.isBorder = true;
  107. this.q = new QuadraticMatrix();
  108. this.triangleCount = 0;
  109. this.triangleStart = 0;
  110. }
  111. return DecimationVertex;
  112. })();
  113. BABYLON.DecimationVertex = DecimationVertex;
  114. var QuadraticMatrix = (function () {
  115. function QuadraticMatrix(data) {
  116. this.data = new Array(10);
  117. for (var i = 0; i < 10; ++i) {
  118. if (data && data[i]) {
  119. this.data[i] = data[i];
  120. }
  121. else {
  122. this.data[i] = 0;
  123. }
  124. }
  125. }
  126. QuadraticMatrix.prototype.det = function (a11, a12, a13, a21, a22, a23, a31, a32, a33) {
  127. 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];
  128. return det;
  129. };
  130. QuadraticMatrix.prototype.addInPlace = function (matrix) {
  131. for (var i = 0; i < 10; ++i) {
  132. this.data[i] += matrix.data[i];
  133. }
  134. };
  135. QuadraticMatrix.prototype.addArrayInPlace = function (data) {
  136. for (var i = 0; i < 10; ++i) {
  137. this.data[i] += data[i];
  138. }
  139. };
  140. QuadraticMatrix.prototype.add = function (matrix) {
  141. var m = new QuadraticMatrix();
  142. for (var i = 0; i < 10; ++i) {
  143. m.data[i] = this.data[i] + matrix.data[i];
  144. }
  145. return m;
  146. };
  147. QuadraticMatrix.FromData = function (a, b, c, d) {
  148. return new QuadraticMatrix(QuadraticMatrix.DataFromNumbers(a, b, c, d));
  149. };
  150. //returning an array to avoid garbage collection
  151. QuadraticMatrix.DataFromNumbers = function (a, b, c, d) {
  152. return [a * a, a * b, a * c, a * d, b * b, b * c, b * d, c * c, c * d, d * d];
  153. };
  154. return QuadraticMatrix;
  155. })();
  156. BABYLON.QuadraticMatrix = QuadraticMatrix;
  157. var Reference = (function () {
  158. function Reference(vertexId, triangleId) {
  159. this.vertexId = vertexId;
  160. this.triangleId = triangleId;
  161. }
  162. return Reference;
  163. })();
  164. BABYLON.Reference = Reference;
  165. /**
  166. * An implementation of the Quadratic Error simplification algorithm.
  167. * Original paper : http://www1.cs.columbia.edu/~cs4162/html05s/garland97.pdf
  168. * Ported mostly from QSlim and http://voxels.blogspot.de/2014/05/quadric-mesh-simplification-with-source.html to babylon JS
  169. * @author RaananW
  170. */
  171. var QuadraticErrorSimplification = (function () {
  172. function QuadraticErrorSimplification(_mesh) {
  173. this._mesh = _mesh;
  174. this.initialised = false;
  175. this.syncIterations = 5000;
  176. this.aggressiveness = 7;
  177. this.decimationIterations = 100;
  178. this.boundingBoxEpsilon = BABYLON.Engine.Epsilon;
  179. }
  180. QuadraticErrorSimplification.prototype.simplify = function (settings, successCallback) {
  181. var _this = this;
  182. //iterating through the submeshes array, one after the other.
  183. BABYLON.AsyncLoop.Run(this._mesh.subMeshes.length, function (loop) {
  184. _this.initWithMesh(_this._mesh, loop.index, function () {
  185. _this.runDecimation(settings, loop.index, function () {
  186. loop.executeNext();
  187. });
  188. });
  189. }, function () {
  190. setTimeout(function () {
  191. successCallback(_this._reconstructedMesh);
  192. }, 0);
  193. });
  194. };
  195. QuadraticErrorSimplification.prototype.isTriangleOnBoundingBox = function (triangle) {
  196. var _this = this;
  197. var gCount = 0;
  198. triangle.vertices.forEach(function (vId) {
  199. var count = 0;
  200. var vPos = _this.vertices[vId].position;
  201. var bbox = _this._mesh.getBoundingInfo().boundingBox;
  202. if (bbox.maximum.x - vPos.x < _this.boundingBoxEpsilon || vPos.x - bbox.minimum.x > _this.boundingBoxEpsilon)
  203. ++count;
  204. if (bbox.maximum.y == vPos.y || vPos.y == bbox.minimum.y)
  205. ++count;
  206. if (bbox.maximum.z == vPos.z || vPos.z == bbox.minimum.z)
  207. ++count;
  208. if (count > 1) {
  209. ++gCount;
  210. }
  211. ;
  212. });
  213. if (gCount > 1) {
  214. console.log(triangle, gCount);
  215. }
  216. return gCount > 1;
  217. };
  218. QuadraticErrorSimplification.prototype.runDecimation = function (settings, submeshIndex, successCallback) {
  219. var _this = this;
  220. var targetCount = ~~(this.triangles.length * settings.quality);
  221. var deletedTriangles = 0;
  222. var triangleCount = this.triangles.length;
  223. var iterationFunction = function (iteration, callback) {
  224. setTimeout(function () {
  225. if (iteration % 5 === 0) {
  226. _this.updateMesh(iteration === 0);
  227. }
  228. for (var i = 0; i < _this.triangles.length; ++i) {
  229. _this.triangles[i].isDirty = false;
  230. }
  231. var threshold = 0.000000001 * Math.pow((iteration + 3), _this.aggressiveness);
  232. var trianglesIterator = function (i) {
  233. var tIdx = ~~(((_this.triangles.length / 2) + i) % _this.triangles.length);
  234. var t = _this.triangles[tIdx];
  235. if (!t)
  236. return;
  237. if (t.error[3] > threshold || t.deleted || t.isDirty) {
  238. return;
  239. }
  240. for (var j = 0; j < 3; ++j) {
  241. if (t.error[j] < threshold) {
  242. var deleted0 = [];
  243. var deleted1 = [];
  244. var i0 = t.vertices[j];
  245. var i1 = t.vertices[(j + 1) % 3];
  246. var v0 = _this.vertices[i0];
  247. var v1 = _this.vertices[i1];
  248. if (v0.isBorder !== v1.isBorder)
  249. continue;
  250. var p = BABYLON.Vector3.Zero();
  251. var n = BABYLON.Vector3.Zero();
  252. var uv = BABYLON.Vector2.Zero();
  253. var color = new BABYLON.Color4(0, 0, 0, 1);
  254. _this.calculateError(v0, v1, p, n, uv, color);
  255. var delTr = [];
  256. if (_this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr))
  257. continue;
  258. if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr))
  259. continue;
  260. if (delTr.length == 2 || delTr[0] === delTr[1]) {
  261. continue;
  262. }
  263. v0.normal = n;
  264. if (v0.uv)
  265. v0.uv = uv;
  266. else if (v0.color)
  267. v0.color = color;
  268. v0.q = v1.q.add(v0.q);
  269. if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
  270. continue;
  271. if (p.equals(v0.position))
  272. continue;
  273. v0.position = p;
  274. var tStart = _this.references.length;
  275. deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
  276. deletedTriangles = _this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
  277. var tCount = _this.references.length - tStart;
  278. if (tCount <= v0.triangleCount) {
  279. if (tCount) {
  280. for (var c = 0; c < tCount; c++) {
  281. _this.references[v0.triangleStart + c] = _this.references[tStart + c];
  282. }
  283. }
  284. }
  285. else {
  286. v0.triangleStart = tStart;
  287. }
  288. v0.triangleCount = tCount;
  289. break;
  290. }
  291. }
  292. };
  293. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, trianglesIterator, callback, function () {
  294. return (triangleCount - deletedTriangles <= targetCount);
  295. });
  296. }, 0);
  297. };
  298. BABYLON.AsyncLoop.Run(this.decimationIterations, function (loop) {
  299. if (triangleCount - deletedTriangles <= targetCount)
  300. loop.breakLoop();
  301. else {
  302. iterationFunction(loop.index, function () {
  303. loop.executeNext();
  304. });
  305. }
  306. }, function () {
  307. setTimeout(function () {
  308. _this.reconstructMesh(submeshIndex);
  309. successCallback();
  310. }, 0);
  311. });
  312. };
  313. QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, submeshIndex, callback) {
  314. var _this = this;
  315. if (!mesh)
  316. return;
  317. this.vertices = [];
  318. this.triangles = [];
  319. this._mesh = mesh;
  320. //It is assumed that a mesh has positions, normals and either uvs or colors.
  321. var positionData = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  322. var normalData = this._mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  323. var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  324. var colorsData = this._mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  325. var indices = mesh.getIndices();
  326. var submesh = mesh.subMeshes[submeshIndex];
  327. var vertexInit = function (i) {
  328. var offset = i + submesh.verticesStart;
  329. var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, offset * 3), BABYLON.Vector3.FromArray(normalData, offset * 3), null, i);
  330. if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  331. vertex.uv = BABYLON.Vector2.FromArray(uvs, offset * 2);
  332. }
  333. else if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  334. vertex.color = BABYLON.Color4.FromArray(colorsData, offset * 4);
  335. }
  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 + i;
  343. var pos = offset * 3;
  344. var i0 = indices[pos + 0];
  345. var i1 = indices[pos + 1];
  346. var i2 = indices[pos + 2];
  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. this.vertices[dst].normal = this.vertices[i].normal;
  403. this.vertices[dst].uv = this.vertices[i].uv;
  404. this.vertices[dst].color = this.vertices[i].color;
  405. newVerticesOrder.push(i);
  406. dst++;
  407. }
  408. }
  409. for (i = 0; i < newTriangles.length; ++i) {
  410. t = newTriangles[i];
  411. for (j = 0; j < 3; ++j) {
  412. t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
  413. }
  414. }
  415. this.vertices = this.vertices.slice(0, dst);
  416. var newPositionData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.PositionKind); //[];
  417. var newNormalData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.NormalKind); //[];
  418. var newUVsData = this._reconstructedMesh.getVerticesData(BABYLON.VertexBuffer.UVKind); //[];
  419. var newColorsData = this._reconstructedMesh.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(this.vertices[i].normal.x);
  425. newNormalData.push(this.vertices[i].normal.y);
  426. newNormalData.push(this.vertices[i].normal.z);
  427. if (this.vertices[i].uv) {
  428. newUVsData.push(this.vertices[i].uv.x);
  429. newUVsData.push(this.vertices[i].uv.y);
  430. }
  431. else if (this.vertices[i].color) {
  432. newColorsData.push(this.vertices[i].color.r);
  433. newColorsData.push(this.vertices[i].color.g);
  434. newColorsData.push(this.vertices[i].color.b);
  435. newColorsData.push(this.vertices[i].color.a);
  436. }
  437. }
  438. var newIndicesArray = this._reconstructedMesh.getIndices(); //[];
  439. for (i = 0; i < newTriangles.length; ++i) {
  440. newIndicesArray.push(newTriangles[i].vertices[0]);
  441. newIndicesArray.push(newTriangles[i].vertices[1]);
  442. newIndicesArray.push(newTriangles[i].vertices[2]);
  443. }
  444. var startingVertex = this._reconstructedMesh.getTotalVertices();
  445. var startingIndex = this._reconstructedMesh.getTotalIndices();
  446. //overwriting the old vertex buffers and indices.
  447. this._reconstructedMesh.setIndices(newIndicesArray);
  448. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, newPositionData);
  449. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormalData);
  450. if (newUVsData.length > 0)
  451. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, newUVsData);
  452. if (newColorsData.length > 0)
  453. this._reconstructedMesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, newColorsData);
  454. //create submesh
  455. var originalSubmesh = this._mesh.subMeshes[submeshIndex];
  456. var newSubmesh = new BABYLON.SubMesh(originalSubmesh.materialIndex, startingVertex, newVerticesOrder.length, startingIndex, newTriangles.length, this._reconstructedMesh);
  457. //return newMesh;
  458. };
  459. QuadraticErrorSimplification.prototype.initDecimatedMesh = function () {
  460. this._reconstructedMesh = new BABYLON.Mesh(this._mesh.name + "Decimated", this._mesh.getScene());
  461. this._reconstructedMesh.material = this._mesh.material;
  462. this._reconstructedMesh.parent = this._mesh.parent;
  463. };
  464. QuadraticErrorSimplification.prototype.isFlipped = function (vertex1, index2, point, deletedArray, borderFactor, delTr) {
  465. for (var i = 0; i < vertex1.triangleCount; ++i) {
  466. var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
  467. if (t.deleted)
  468. continue;
  469. var s = this.references[vertex1.triangleStart + i].vertexId;
  470. var id1 = t.vertices[(s + 1) % 3];
  471. var id2 = t.vertices[(s + 2) % 3];
  472. if ((id1 === index2 || id2 === index2)) {
  473. deletedArray[i] = true;
  474. delTr.push(t);
  475. continue;
  476. }
  477. var d1 = this.vertices[id1].position.subtract(point);
  478. d1 = d1.normalize();
  479. var d2 = this.vertices[id2].position.subtract(point);
  480. d2 = d2.normalize();
  481. if (Math.abs(BABYLON.Vector3.Dot(d1, d2)) > 0.999)
  482. return true;
  483. var normal = BABYLON.Vector3.Cross(d1, d2).normalize();
  484. deletedArray[i] = false;
  485. if (BABYLON.Vector3.Dot(normal, t.normal) < 0.2)
  486. return true;
  487. }
  488. return false;
  489. };
  490. QuadraticErrorSimplification.prototype.updateTriangles = function (vertexId, vertex, deletedArray, deletedTriangles) {
  491. var newDeleted = deletedTriangles;
  492. for (var i = 0; i < vertex.triangleCount; ++i) {
  493. var ref = this.references[vertex.triangleStart + i];
  494. var t = this.triangles[ref.triangleId];
  495. if (t.deleted)
  496. continue;
  497. if (deletedArray[i]) {
  498. t.deleted = true;
  499. newDeleted++;
  500. continue;
  501. }
  502. t.vertices[ref.vertexId] = vertexId;
  503. t.isDirty = true;
  504. t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
  505. t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
  506. t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
  507. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  508. this.references.push(ref);
  509. }
  510. return newDeleted;
  511. };
  512. QuadraticErrorSimplification.prototype.identifyBorder = function () {
  513. for (var i = 0; i < this.vertices.length; ++i) {
  514. var vCount = [];
  515. var vId = [];
  516. var v = this.vertices[i];
  517. var j;
  518. for (j = 0; j < v.triangleCount; ++j) {
  519. var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
  520. for (var ii = 0; ii < 3; ii++) {
  521. var ofs = 0;
  522. var id = triangle.vertices[ii];
  523. while (ofs < vCount.length) {
  524. if (vId[ofs] === id)
  525. break;
  526. ++ofs;
  527. }
  528. if (ofs === vCount.length) {
  529. vCount.push(1);
  530. vId.push(id);
  531. }
  532. else {
  533. vCount[ofs]++;
  534. }
  535. }
  536. }
  537. for (j = 0; j < vCount.length; ++j) {
  538. if (vCount[j] === 1) {
  539. this.vertices[vId[j]].isBorder = true;
  540. }
  541. else {
  542. this.vertices[vId[j]].isBorder = false;
  543. }
  544. }
  545. }
  546. };
  547. QuadraticErrorSimplification.prototype.updateMesh = function (identifyBorders) {
  548. if (identifyBorders === void 0) { identifyBorders = false; }
  549. var i;
  550. if (!identifyBorders) {
  551. var newTrianglesVector = [];
  552. for (i = 0; i < this.triangles.length; ++i) {
  553. if (!this.triangles[i].deleted) {
  554. newTrianglesVector.push(this.triangles[i]);
  555. }
  556. }
  557. this.triangles = newTrianglesVector;
  558. }
  559. for (i = 0; i < this.vertices.length; ++i) {
  560. this.vertices[i].triangleCount = 0;
  561. this.vertices[i].triangleStart = 0;
  562. }
  563. var t;
  564. var j;
  565. var v;
  566. for (i = 0; i < this.triangles.length; ++i) {
  567. t = this.triangles[i];
  568. for (j = 0; j < 3; ++j) {
  569. v = this.vertices[t.vertices[j]];
  570. v.triangleCount++;
  571. }
  572. }
  573. var tStart = 0;
  574. for (i = 0; i < this.vertices.length; ++i) {
  575. this.vertices[i].triangleStart = tStart;
  576. tStart += this.vertices[i].triangleCount;
  577. this.vertices[i].triangleCount = 0;
  578. }
  579. var newReferences = new Array(this.triangles.length * 3);
  580. for (i = 0; i < this.triangles.length; ++i) {
  581. t = this.triangles[i];
  582. for (j = 0; j < 3; ++j) {
  583. v = this.vertices[t.vertices[j]];
  584. newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
  585. v.triangleCount++;
  586. }
  587. }
  588. this.references = newReferences;
  589. if (identifyBorders) {
  590. this.identifyBorder();
  591. }
  592. };
  593. QuadraticErrorSimplification.prototype.vertexError = function (q, point) {
  594. var x = point.x;
  595. var y = point.y;
  596. var z = point.z;
  597. 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];
  598. };
  599. QuadraticErrorSimplification.prototype.calculateError = function (vertex1, vertex2, pointResult, normalResult, uvResult, colorResult) {
  600. var q = vertex1.q.add(vertex2.q);
  601. var border = vertex1.isBorder && vertex2.isBorder;
  602. var error = 0;
  603. var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
  604. if (qDet !== 0 && !border) {
  605. if (!pointResult) {
  606. pointResult = BABYLON.Vector3.Zero();
  607. }
  608. pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
  609. pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
  610. pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
  611. error = this.vertexError(q, pointResult);
  612. //TODO this should be correctly calculated
  613. if (normalResult) {
  614. normalResult.copyFrom(vertex1.normal);
  615. if (vertex1.uv)
  616. uvResult.copyFrom(vertex1.uv);
  617. else if (vertex1.color)
  618. colorResult.copyFrom(vertex1.color);
  619. }
  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. normalResult.copyFrom(vertex1.normal);
  632. if (vertex1.uv)
  633. uvResult.copyFrom(vertex1.uv);
  634. else if (vertex1.color)
  635. colorResult.copyFrom(vertex1.color);
  636. }
  637. }
  638. else if (error === error2) {
  639. if (pointResult) {
  640. pointResult.copyFrom(vertex2.position);
  641. normalResult.copyFrom(vertex2.normal);
  642. if (vertex2.uv)
  643. uvResult.copyFrom(vertex2.uv);
  644. else if (vertex2.color)
  645. colorResult.copyFrom(vertex2.color);
  646. }
  647. }
  648. else {
  649. if (pointResult) {
  650. pointResult.copyFrom(p3);
  651. normalResult.copyFrom(vertex1.normal);
  652. if (vertex1.uv)
  653. uvResult.copyFrom(vertex1.uv);
  654. else if (vertex1.color)
  655. colorResult.copyFrom(vertex1.color);
  656. }
  657. }
  658. }
  659. return error;
  660. };
  661. return QuadraticErrorSimplification;
  662. })();
  663. BABYLON.QuadraticErrorSimplification = QuadraticErrorSimplification;
  664. })(BABYLON || (BABYLON = {}));
  665. //# sourceMappingURL=babylon.meshSimplification.js.map