babylon.meshSimplification.js 33 KB

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