babylon.meshSimplification.ts 26 KB

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