-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNormaliseMesh.js
More file actions
54 lines (36 loc) · 1.3 KB
/
NormaliseMesh.js
File metadata and controls
54 lines (36 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { calculateCentriod } from './GeometryUtils';
export function normaliseMesh(geometry, scale = 1.0) {
var maxLengthSquared = 0;
for(var i = 0; i < geometry.vertices.length; i++)
{
var lengthSquared = geometry.vertices[i].lengthSq();
if(lengthSquared > maxLengthSquared) {
maxLengthSquared = lengthSquared;
}
}
const inverseScale = scale/Math.sqrt(maxLengthSquared);
scaleMesh(geometry, inverseScale);
return 1/inverseScale;
}
export function scaleMesh(geometry, scale) {
for(var i = 0; i < geometry.vertices.length; i++)
{
geometry.vertices[i].multiplyScalar(scale);
}
geometry.verticesNeedUpdate = true;
}
export function normaliseCenter(object) {
const centroid = calculateCentriod(object.geometry.vertices);
for(var i = 0; i < object.geometry.vertices.length; i++) {
// Move vert to local centroid space.
object.geometry.vertices[i].sub(centroid);
}
// Move the object centroid world position to preserve vert world position.
object.position.add(centroid);
}
export function normaliseObject(object, scale) {
const reverseScaleCoef = normaliseMesh(object.geometry, scale);
// TODO: potential loss of information here.
object.matrix.identity();
return reverseScaleCoef;
}