forked from schteppe/cannon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trimesh.js
196 lines (164 loc) · 5.97 KB
/
Trimesh.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var Vec3 = require("../src/math/Vec3");
var Quaternion = require("../src/math/Quaternion");
var Plane = require('../src/shapes/Plane');
var Trimesh = require('../src/shapes/Trimesh');
var World = require('../src/world/World');
var Body = require('../src/objects/Body');
var AABB = require('../src/collision/AABB');
module.exports = {
updateNormals: function(test){
var mesh = Trimesh.createTorus();
mesh.normals[0] = 1;
mesh.updateNormals();
test.ok(mesh.normals[0] !== 1);
test.done();
},
updateAABB: function(test){
var mesh = Trimesh.createTorus();
mesh.aabb.lowerBound.set(1,2,3);
mesh.updateAABB();
test.ok(mesh.aabb.lowerBound.y !== 2);
test.done();
},
updateTree: {
scaled: function(test){
var mesh = Trimesh.createTorus();
mesh.updateTree();
var bigMesh = Trimesh.createTorus();
bigMesh.setScale(new Vec3(2,2,2));
test.equal(bigMesh.aabb.upperBound.x, mesh.aabb.upperBound.x * 2, 'AABB does not scale with the mesh!');
test.equal(bigMesh.tree.aabb.upperBound.x, mesh.tree.aabb.upperBound.x, 'Octree AABB scales with the mesh, which is wrong!');
test.done();
}
},
getTrianglesInAABB: {
unscaled: function(test){
var mesh = Trimesh.createTorus(1,1,32,32);
var result = [];
// Should get all triangles if we use the full AABB
var aabb = mesh.aabb.clone();
mesh.getTrianglesInAABB(aabb, result);
test.equal(result.length, mesh.indices.length / 3);
// Should get less triangles if we use the half AABB
result.length = 0;
aabb.lowerBound.scale(0.1, aabb.lowerBound);
aabb.upperBound.scale(0.1, aabb.upperBound);
mesh.getTrianglesInAABB(aabb, result);
console.log(result.length, mesh.indices.length / 3)
test.ok(result.length < mesh.indices.length / 3);
test.done();
},
// scaled: function(test){
// var mesh = Trimesh.createTorus(1,1,16,16);
// var result = [];
// // Should get all triangles if we use the full AABB
// var aabb = mesh.aabb.clone();
// mesh.getTrianglesInAABB(aabb, result);
// test.equal(result.length, mesh.indices.length / 3);
// // Should get less triangles if we use the half AABB
// result.length = 0;
// aabb.lowerBound.scale(0.5, aabb.lowerBound);
// aabb.upperBound.scale(0.5, aabb.upperBound);
// mesh.getTrianglesInAABB(aabb, result);
// test.ok(result.length < mesh.indices.length / 3);
// test.done();
// }
},
getVertex: {
unscaled: function(test){
var mesh = Trimesh.createTorus();
var vertex = new Vec3();
mesh.getVertex(0, vertex);
test.deepEqual(vertex, new Vec3(mesh.vertices[0], mesh.vertices[1], mesh.vertices[2]));
test.done();
},
scaled: function(test){
var mesh = Trimesh.createTorus();
mesh.setScale(new Vec3(1,2,3));
var vertex = new Vec3();
mesh.getVertex(0, vertex);
test.deepEqual(vertex, new Vec3(1 * mesh.vertices[0], 2 * mesh.vertices[1], 3 * mesh.vertices[2]));
test.done();
}
},
getWorldVertex: function(test){
var mesh = Trimesh.createTorus();
var vertex = new Vec3();
mesh.getWorldVertex(0, new Vec3(), new Quaternion(), vertex);
test.deepEqual(vertex, new Vec3(mesh.vertices[0], mesh.vertices[1], mesh.vertices[2]));
test.done();
},
getTriangleVertices: function(test){
var mesh = Trimesh.createTorus();
var va = new Vec3();
var vb = new Vec3();
var vc = new Vec3();
var va1 = new Vec3();
var vb1 = new Vec3();
var vc1 = new Vec3();
mesh.getVertex(mesh.indices[0], va);
mesh.getVertex(mesh.indices[1], vb);
mesh.getVertex(mesh.indices[2], vc);
mesh.getTriangleVertices(0, va1, vb1, vc1);
test.deepEqual(va, va1);
test.deepEqual(vb, vb1);
test.deepEqual(vc, vc1);
test.done();
},
getNormal: function(test){
var mesh = Trimesh.createTorus();
var normal = new Vec3();
mesh.getNormal(0, normal);
test.deepEqual(new Vec3(mesh.normals[0], mesh.normals[1], mesh.normals[2]), normal);
test.done();
},
calculateLocalInertia: function(test){
var mesh = Trimesh.createTorus();
var inertia = new Vec3();
mesh.calculateLocalInertia(1, inertia);
test.done();
},
computeLocalAABB: function(test){
console.warn('Trimesh::computeLocalAABB is todo');
test.done();
},
updateBoundingSphereRadius: function(test){
console.warn('Trimesh::updateBoundingSphereRadius is todo');
test.done();
},
calculateWorldAABB : function(test){
var poly = Trimesh.createTorus();
var min = new Vec3();
var max = new Vec3();
poly.calculateWorldAABB(
new Vec3(1,0,0), // Translate 2 x in world
new Quaternion(0,0,0,1),
min,
max
);
test.ok(!isNaN(min.x));
test.ok(!isNaN(max.x));
test.done();
},
volume: function(test){
var mesh = Trimesh.createTorus();
test.ok(mesh.volume() > 0);
test.done();
},
narrowphaseAgainstPlane: function(test){
var world = new World();
var torusShape = Trimesh.createTorus();
var torusBody = new Body({
mass: 1
});
torusBody.addShape(torusShape);
var planeBody = new Body({
mass: 1
});
planeBody.addShape(new Plane());
world.addBody(torusBody);
world.addBody(planeBody);
world.step(1 / 60);
test.done();
}
};