babylon.meshSimplification.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. var delTr = [];
  152. if (_this.isFlipped(v0, i1, p, deleted0, t.borderFactor, delTr))
  153. continue;
  154. if (_this.isFlipped(v1, i0, p, deleted1, t.borderFactor, delTr))
  155. continue;
  156. if (delTr.length == 2 || delTr[0] === delTr[1]) {
  157. continue;
  158. }
  159. v0.normal = n;
  160. if (v0.uv)
  161. v0.uv = uv;
  162. else if (v0.color)
  163. v0.color = color;
  164. v0.q = v1.q.add(v0.q);
  165. if (deleted0.indexOf(true) < 0 || deleted1.indexOf(true) < 0)
  166. continue;
  167. if (p.equals(v0.position))
  168. continue;
  169. v0.position = p;
  170. var tStart = _this.references.length;
  171. deletedTriangles = _this.updateTriangles(v0.id, v0, deleted0, deletedTriangles);
  172. deletedTriangles = _this.updateTriangles(v0.id, v1, deleted1, deletedTriangles);
  173. var tCount = _this.references.length - tStart;
  174. if (tCount <= v0.triangleCount) {
  175. if (tCount) {
  176. for (var c = 0; c < tCount; c++) {
  177. _this.references[v0.triangleStart + c] = _this.references[tStart + c];
  178. }
  179. }
  180. } else {
  181. v0.triangleStart = tStart;
  182. }
  183. v0.triangleCount = tCount;
  184. break;
  185. }
  186. }
  187. };
  188. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, trianglesIterator, callback, function () {
  189. return (triangleCount - deletedTriangles <= targetCount);
  190. });
  191. }, 0);
  192. };
  193. BABYLON.AsyncLoop.Run(this.decimationIterations, function (loop) {
  194. if (triangleCount - deletedTriangles <= targetCount)
  195. loop.breakLoop();
  196. else {
  197. iterationFunction(loop.index, function () {
  198. loop.executeNext();
  199. });
  200. }
  201. }, function () {
  202. setTimeout(function () {
  203. successCallback(_this.reconstructMesh());
  204. }, 0);
  205. });
  206. };
  207. QuadraticErrorSimplification.prototype.initWithMesh = function (mesh, callback) {
  208. var _this = this;
  209. if (!mesh)
  210. return;
  211. this.vertices = [];
  212. this.triangles = [];
  213. this._mesh = mesh;
  214. //It is assumed that a mesh has positions, normals and either uvs or colors.
  215. var positionData = this._mesh.getVerticesData(BABYLON.VertexBuffer.PositionKind);
  216. var normalData = this._mesh.getVerticesData(BABYLON.VertexBuffer.NormalKind);
  217. var uvs = this._mesh.getVerticesData(BABYLON.VertexBuffer.UVKind);
  218. var colorsData = this._mesh.getVerticesData(BABYLON.VertexBuffer.ColorKind);
  219. var indices = mesh.getIndices();
  220. var vertexInit = function (i) {
  221. var vertex = new DecimationVertex(BABYLON.Vector3.FromArray(positionData, i * 3), BABYLON.Vector3.FromArray(normalData, i * 3), null, i);
  222. if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  223. vertex.uv = BABYLON.Vector2.FromArray(uvs, i * 2);
  224. } else if (_this._mesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  225. vertex.color = BABYLON.Color4.FromArray(colorsData, i * 4);
  226. }
  227. _this.vertices.push(vertex);
  228. };
  229. var totalVertices = mesh.getTotalVertices();
  230. BABYLON.AsyncLoop.SyncAsyncForLoop(totalVertices, this.syncIterations, vertexInit, function () {
  231. var indicesInit = function (i) {
  232. var pos = i * 3;
  233. var i0 = indices[pos + 0];
  234. var i1 = indices[pos + 1];
  235. var i2 = indices[pos + 2];
  236. var triangle = new DecimationTriangle([_this.vertices[i0].id, _this.vertices[i1].id, _this.vertices[i2].id]);
  237. _this.triangles.push(triangle);
  238. };
  239. BABYLON.AsyncLoop.SyncAsyncForLoop(indices.length / 3, _this.syncIterations, indicesInit, function () {
  240. _this.init(callback);
  241. });
  242. });
  243. };
  244. QuadraticErrorSimplification.prototype.init = function (callback) {
  245. var _this = this;
  246. var triangleInit1 = function (i) {
  247. var t = _this.triangles[i];
  248. 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();
  249. for (var j = 0; j < 3; j++) {
  250. _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))));
  251. }
  252. };
  253. BABYLON.AsyncLoop.SyncAsyncForLoop(this.triangles.length, this.syncIterations, triangleInit1, function () {
  254. var triangleInit2 = function (i) {
  255. var t = _this.triangles[i];
  256. for (var j = 0; j < 3; ++j) {
  257. t.error[j] = _this.calculateError(_this.vertices[t.vertices[j]], _this.vertices[t.vertices[(j + 1) % 3]]);
  258. }
  259. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  260. };
  261. BABYLON.AsyncLoop.SyncAsyncForLoop(_this.triangles.length, _this.syncIterations, triangleInit2, function () {
  262. _this.initialised = true;
  263. callback();
  264. });
  265. });
  266. };
  267. QuadraticErrorSimplification.prototype.reconstructMesh = function () {
  268. var newTriangles = [];
  269. var i;
  270. for (i = 0; i < this.vertices.length; ++i) {
  271. this.vertices[i].triangleCount = 0;
  272. }
  273. var t;
  274. var j;
  275. for (i = 0; i < this.triangles.length; ++i) {
  276. if (!this.triangles[i].deleted) {
  277. t = this.triangles[i];
  278. for (j = 0; j < 3; ++j) {
  279. this.vertices[t.vertices[j]].triangleCount = 1;
  280. }
  281. newTriangles.push(t);
  282. }
  283. }
  284. var newVerticesOrder = [];
  285. //compact vertices, get the IDs of the vertices used.
  286. var dst = 0;
  287. for (i = 0; i < this.vertices.length; ++i) {
  288. if (this.vertices[i].triangleCount) {
  289. this.vertices[i].triangleStart = dst;
  290. this.vertices[dst].position = this.vertices[i].position;
  291. this.vertices[dst].normal = this.vertices[i].normal;
  292. this.vertices[dst].uv = this.vertices[i].uv;
  293. this.vertices[dst].color = this.vertices[i].color;
  294. newVerticesOrder.push(i);
  295. dst++;
  296. }
  297. }
  298. for (i = 0; i < newTriangles.length; ++i) {
  299. t = newTriangles[i];
  300. for (j = 0; j < 3; ++j) {
  301. t.vertices[j] = this.vertices[t.vertices[j]].triangleStart;
  302. }
  303. }
  304. this.vertices = this.vertices.slice(0, dst);
  305. var newPositionData = [];
  306. var newNormalData = [];
  307. var newUVsData = [];
  308. var newColorsData = [];
  309. for (i = 0; i < newVerticesOrder.length; ++i) {
  310. newPositionData.push(this.vertices[i].position.x);
  311. newPositionData.push(this.vertices[i].position.y);
  312. newPositionData.push(this.vertices[i].position.z);
  313. newNormalData.push(this.vertices[i].normal.x);
  314. newNormalData.push(this.vertices[i].normal.y);
  315. newNormalData.push(this.vertices[i].normal.z);
  316. if (this.vertices[i].uv) {
  317. newUVsData.push(this.vertices[i].uv.x);
  318. newUVsData.push(this.vertices[i].uv.y);
  319. } else if (this.vertices[i].color) {
  320. newColorsData.push(this.vertices[i].color.r);
  321. newColorsData.push(this.vertices[i].color.g);
  322. newColorsData.push(this.vertices[i].color.b);
  323. newColorsData.push(this.vertices[i].color.a);
  324. }
  325. }
  326. var newIndicesArray = [];
  327. for (i = 0; i < newTriangles.length; ++i) {
  328. newIndicesArray.push(newTriangles[i].vertices[0]);
  329. newIndicesArray.push(newTriangles[i].vertices[1]);
  330. newIndicesArray.push(newTriangles[i].vertices[2]);
  331. }
  332. //not cloning, to avoid geometry problems. Creating a whole new mesh.
  333. var newMesh = new BABYLON.Mesh(this._mesh.name + "Decimated", this._mesh.getScene());
  334. newMesh.material = this._mesh.material;
  335. newMesh.parent = this._mesh.parent;
  336. newMesh.setIndices(newIndicesArray);
  337. newMesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, newPositionData);
  338. newMesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, newNormalData);
  339. if (newUVsData.length > 0)
  340. newMesh.setVerticesData(BABYLON.VertexBuffer.UVKind, newUVsData);
  341. if (newColorsData.length > 0)
  342. newMesh.setVerticesData(BABYLON.VertexBuffer.ColorKind, newColorsData);
  343. //preparing the skeleton support
  344. if (this._mesh.skeleton) {
  345. //newMesh.skeleton = this._mesh.skeleton.clone("", "");
  346. //newMesh.getScene().beginAnimation(newMesh.skeleton, 0, 100, true, 1.0);
  347. }
  348. return newMesh;
  349. };
  350. QuadraticErrorSimplification.prototype.isFlipped = function (vertex1, index2, point, deletedArray, borderFactor, delTr) {
  351. for (var i = 0; i < vertex1.triangleCount; ++i) {
  352. var t = this.triangles[this.references[vertex1.triangleStart + i].triangleId];
  353. if (t.deleted)
  354. continue;
  355. var s = this.references[vertex1.triangleStart + i].vertexId;
  356. var id1 = t.vertices[(s + 1) % 3];
  357. var id2 = t.vertices[(s + 2) % 3];
  358. if ((id1 === index2 || id2 === index2) && borderFactor < 2) {
  359. deletedArray[i] = true;
  360. delTr.push(t);
  361. continue;
  362. }
  363. var d1 = this.vertices[id1].position.subtract(point);
  364. d1 = d1.normalize();
  365. var d2 = this.vertices[id2].position.subtract(point);
  366. d2 = d2.normalize();
  367. if (Math.abs(BABYLON.Vector3.Dot(d1, d2)) > 0.999)
  368. return true;
  369. var normal = BABYLON.Vector3.Cross(d1, d2).normalize();
  370. deletedArray[i] = false;
  371. if (BABYLON.Vector3.Dot(normal, t.normal) < 0.2)
  372. return true;
  373. }
  374. return false;
  375. };
  376. QuadraticErrorSimplification.prototype.updateTriangles = function (vertexId, vertex, deletedArray, deletedTriangles) {
  377. var newDeleted = deletedTriangles;
  378. for (var i = 0; i < vertex.triangleCount; ++i) {
  379. var ref = this.references[vertex.triangleStart + i];
  380. var t = this.triangles[ref.triangleId];
  381. if (t.deleted)
  382. continue;
  383. if (deletedArray[i]) {
  384. t.deleted = true;
  385. newDeleted++;
  386. continue;
  387. }
  388. t.vertices[ref.vertexId] = vertexId;
  389. t.isDirty = true;
  390. t.error[0] = this.calculateError(this.vertices[t.vertices[0]], this.vertices[t.vertices[1]]) + (t.borderFactor / 2);
  391. t.error[1] = this.calculateError(this.vertices[t.vertices[1]], this.vertices[t.vertices[2]]) + (t.borderFactor / 2);
  392. t.error[2] = this.calculateError(this.vertices[t.vertices[2]], this.vertices[t.vertices[0]]) + (t.borderFactor / 2);
  393. t.error[3] = Math.min(t.error[0], t.error[1], t.error[2]);
  394. this.references.push(ref);
  395. }
  396. return newDeleted;
  397. };
  398. QuadraticErrorSimplification.prototype.identifyBorder = function () {
  399. for (var i = 0; i < this.vertices.length; ++i) {
  400. var vCount = [];
  401. var vId = [];
  402. var v = this.vertices[i];
  403. var j;
  404. for (j = 0; j < v.triangleCount; ++j) {
  405. var triangle = this.triangles[this.references[v.triangleStart + j].triangleId];
  406. for (var ii = 0; ii < 3; ii++) {
  407. var ofs = 0;
  408. var id = triangle.vertices[ii];
  409. while (ofs < vCount.length) {
  410. if (vId[ofs] === id)
  411. break;
  412. ++ofs;
  413. }
  414. if (ofs === vCount.length) {
  415. vCount.push(1);
  416. vId.push(id);
  417. } else {
  418. vCount[ofs]++;
  419. }
  420. }
  421. }
  422. for (j = 0; j < vCount.length; ++j) {
  423. if (vCount[j] === 1) {
  424. this.vertices[vId[j]].isBorder = true;
  425. } else {
  426. this.vertices[vId[j]].isBorder = false;
  427. }
  428. }
  429. }
  430. };
  431. QuadraticErrorSimplification.prototype.updateMesh = function (identifyBorders) {
  432. if (typeof identifyBorders === "undefined") { identifyBorders = false; }
  433. var i;
  434. if (!identifyBorders) {
  435. var newTrianglesVector = [];
  436. for (i = 0; i < this.triangles.length; ++i) {
  437. if (!this.triangles[i].deleted) {
  438. newTrianglesVector.push(this.triangles[i]);
  439. }
  440. }
  441. this.triangles = newTrianglesVector;
  442. }
  443. for (i = 0; i < this.vertices.length; ++i) {
  444. this.vertices[i].triangleCount = 0;
  445. this.vertices[i].triangleStart = 0;
  446. }
  447. var t;
  448. var j;
  449. var v;
  450. for (i = 0; i < this.triangles.length; ++i) {
  451. t = this.triangles[i];
  452. for (j = 0; j < 3; ++j) {
  453. v = this.vertices[t.vertices[j]];
  454. v.triangleCount++;
  455. }
  456. }
  457. var tStart = 0;
  458. for (i = 0; i < this.vertices.length; ++i) {
  459. this.vertices[i].triangleStart = tStart;
  460. tStart += this.vertices[i].triangleCount;
  461. this.vertices[i].triangleCount = 0;
  462. }
  463. var newReferences = new Array(this.triangles.length * 3);
  464. for (i = 0; i < this.triangles.length; ++i) {
  465. t = this.triangles[i];
  466. for (j = 0; j < 3; ++j) {
  467. v = this.vertices[t.vertices[j]];
  468. newReferences[v.triangleStart + v.triangleCount] = new Reference(j, i);
  469. v.triangleCount++;
  470. }
  471. }
  472. this.references = newReferences;
  473. if (identifyBorders) {
  474. this.identifyBorder();
  475. }
  476. };
  477. QuadraticErrorSimplification.prototype.vertexError = function (q, point) {
  478. var x = point.x;
  479. var y = point.y;
  480. var z = point.z;
  481. 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];
  482. };
  483. QuadraticErrorSimplification.prototype.calculateError = function (vertex1, vertex2, pointResult, normalResult, uvResult, colorResult) {
  484. var q = vertex1.q.add(vertex2.q);
  485. var border = vertex1.isBorder && vertex2.isBorder;
  486. var error = 0;
  487. var qDet = q.det(0, 1, 2, 1, 4, 5, 2, 5, 7);
  488. if (qDet !== 0 && !border) {
  489. if (!pointResult) {
  490. pointResult = BABYLON.Vector3.Zero();
  491. }
  492. pointResult.x = -1 / qDet * (q.det(1, 2, 3, 4, 5, 6, 5, 7, 8));
  493. pointResult.y = 1 / qDet * (q.det(0, 2, 3, 1, 5, 6, 2, 7, 8));
  494. pointResult.z = -1 / qDet * (q.det(0, 1, 3, 1, 4, 6, 2, 5, 8));
  495. error = this.vertexError(q, pointResult);
  496. //TODO this should be correctly calculated
  497. if (normalResult) {
  498. normalResult.copyFrom(vertex1.normal);
  499. if (vertex1.uv)
  500. uvResult.copyFrom(vertex1.uv);
  501. else if (vertex1.color)
  502. colorResult.copyFrom(vertex1.color);
  503. }
  504. } else {
  505. var p3 = (vertex1.position.add(vertex2.position)).divide(new BABYLON.Vector3(2, 2, 2));
  506. //var norm3 = (vertex1.normal.add(vertex2.normal)).divide(new Vector3(2, 2, 2)).normalize();
  507. var error1 = this.vertexError(q, vertex1.position);
  508. var error2 = this.vertexError(q, vertex2.position);
  509. var error3 = this.vertexError(q, p3);
  510. error = Math.min(error1, error2, error3);
  511. if (error === error1) {
  512. if (pointResult) {
  513. pointResult.copyFrom(vertex1.position);
  514. normalResult.copyFrom(vertex1.normal);
  515. if (vertex1.uv)
  516. uvResult.copyFrom(vertex1.uv);
  517. else if (vertex1.color)
  518. colorResult.copyFrom(vertex1.color);
  519. }
  520. } else if (error === error2) {
  521. if (pointResult) {
  522. pointResult.copyFrom(vertex2.position);
  523. normalResult.copyFrom(vertex2.normal);
  524. if (vertex2.uv)
  525. uvResult.copyFrom(vertex2.uv);
  526. else if (vertex2.color)
  527. colorResult.copyFrom(vertex2.color);
  528. }
  529. } else {
  530. if (pointResult) {
  531. pointResult.copyFrom(p3);
  532. normalResult.copyFrom(vertex1.normal);
  533. if (vertex1.uv)
  534. uvResult.copyFrom(vertex1.uv);
  535. else if (vertex1.color)
  536. colorResult.copyFrom(vertex1.color);
  537. }
  538. }
  539. }
  540. return error;
  541. };
  542. return QuadraticErrorSimplification;
  543. })();
  544. BABYLON.QuadraticErrorSimplification = QuadraticErrorSimplification;
  545. })(BABYLON || (BABYLON = {}));
  546. //# sourceMappingURL=babylon.meshSimplification.js.map