Skip to content

Commit

Permalink
scale for the Trimesh
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Feb 11, 2015
1 parent a77b6e9 commit 52db535
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
65 changes: 61 additions & 4 deletions src/shapes/Trimesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ function Trimesh(vertices, indices) {
*/
this.edges = null;

/**
* Local scaling of the mesh. Use .setScale() to set it.
* @property {Vec3} scale
*/
this.scale = new Vec3(1, 1, 1);

/**
* The indexed triangles. Use .updateTree() to update it.
* @property {Octree} tree
*/
this.tree = new Octree();

this.updateEdges();
Expand Down Expand Up @@ -91,17 +101,46 @@ Trimesh.prototype.updateTree = function(){
var c = new Vec3();
var points = [a, b, c];
for (var i = 0; i < this.indices.length / 3; i++) {
this.getTriangleVertices(i, a, b, c);
//this.getTriangleVertices(i, a, b, c);

// Get unscaled triangle verts
var i3 = i * 3;
this._getUnscaledVertex(this.indices[i3], a);
this._getUnscaledVertex(this.indices[i3 + 1], b);
this._getUnscaledVertex(this.indices[i3 + 2], c);

triangleAABB.setFromPoints(points);
tree.insert(triangleAABB, i);
}
tree.removeEmptyNodes();
};

/**
* Get triangles in a local AABB from the triangle.
* @param {AABB} aabb
* @param {array} result An array of integers, referencing the queried triangles.
*/
Trimesh.prototype.getTrianglesInAABB = function(aabb, result){
return this.tree.aabbQuery(aabb, result);
};

/**
* @method setScale
* @param {Vec3} scale
*/
Trimesh.prototype.setScale = function(scale){
var wasUniform = this.scale.x === this.scale.y === this.scale.z;
var isUniform = scale.x === scale.y === scale.z;

if(!(wasUniform && isUniform)){
// Non-uniform scaling. Need to update normals.
this.updateNormals();
}
this.scale.copy(scale);
this.updateAABB();
this.updateBoundingSphereRadius();
};

/**
* Compute the normals of the faces. Will save in the .normals array.
* @method updateNormals
Expand Down Expand Up @@ -219,11 +258,29 @@ var vc = new Vec3();
* @return {Vec3} The "out" vector object
*/
Trimesh.prototype.getVertex = function(i, out){
var scale = this.scale;
this._getUnscaledVertex(i, out);
out.x *= scale.x;
out.y *= scale.y;
out.z *= scale.z;
return out;
};

/**
* Get raw vertex i
* @private
* @method _getUnscaledVertex
* @param {number} i
* @param {Vec3} out
* @return {Vec3} The "out" vector object
*/
Trimesh.prototype._getUnscaledVertex = function(i, out){
var i3 = i * 3;
var vertices = this.vertices;
return out.set(
this.vertices[i3],
this.vertices[i3 + 1],
this.vertices[i3 + 2]
vertices[i3],
vertices[i3 + 1],
vertices[i3 + 2]
);
};

Expand Down
22 changes: 16 additions & 6 deletions test/Trimesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@ module.exports = {
test.done();
},

getVertex: 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();
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){
Expand Down

0 comments on commit 52db535

Please sign in to comment.