babylon.meshSimplification.js 26 KB

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