Better mesh centreing

This commit is contained in:
Lucas Dower 2022-03-01 22:39:07 +00:00
parent a99fa2b452
commit a99799d539
2 changed files with 10 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { Vector3 } from './vector'; import { Vector3 } from './vector';
import { UV, Bounds, LOG, ASSERT, CustomError, LOG_WARN } from './util'; import { UV, Bounds, LOG, ASSERT, CustomError, LOG_WARN } from './util';
import { Triangle, UVTriangle } from './triangle'; import { UVTriangle } from './triangle';
import { RGB } from './util'; import { RGB } from './util';
import { AppContext } from './app_context'; import { AppContext } from './app_context';
@ -126,6 +126,7 @@ export class Mesh {
} }
private _centreMesh() { private _centreMesh() {
/*
const centre = new Vector3(0, 0, 0); const centre = new Vector3(0, 0, 0);
let totalWeight = 0.0; let totalWeight = 0.0;
@ -139,7 +140,9 @@ export class Mesh {
centre.add(triangle.getCentre().mulScalar(weight)); centre.add(triangle.getCentre().mulScalar(weight));
}); });
centre.divScalar(totalWeight); centre.divScalar(totalWeight);
*/
const centre = this.getBounds().getCentre();
if (!centre.isNumber()) { if (!centre.isNumber()) {
throw new CustomError('Could not find centre of mesh'); throw new CustomError('Could not find centre of mesh');
} }

View File

@ -117,6 +117,11 @@ export class Bounds {
public get max() { public get max() {
return this._max; return this._max;
} }
public getCentre() {
const extents = Vector3.sub(this._max, this._min).divScalar(2);
return Vector3.add(this.min, extents);
}
} }
export function ASSERT(condition: any, errorMessage = 'Assertion Failed'): asserts condition { export function ASSERT(condition: any, errorMessage = 'Assertion Failed'): asserts condition {