babylon.meshSimplification.js 33 KB

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