babylon.meshSimplification.js 26 KB

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