Skip to content

Instantly share code, notes, and snippets.

@kig
Created November 28, 2010 20:14
Show Gist options
  • Save kig/719267 to your computer and use it in GitHub Desktop.
Save kig/719267 to your computer and use it in GitHub Desktop.
BlenderExport.tmpVec = vec4.create();
BlenderExport.tmpNo = vec4.create();
BlenderExport.rv = vec3.create();
BlenderExport.nrv = vec3.create();
BlenderExport.v = vec4.create();
BlenderExport.n = vec4.create();
BlenderExport.setFrame = function(frame) {
if (!this.frameVertices)
this.frameVertices = new Float32Array(this.vertices);
if (!this.frameNormals)
this.frameNormals = new Float32Array(this.normals);
var tmpVec = BlenderExport.tmpVec;
var tmpNo = BlenderExport.tmpNo;
var rv = BlenderExport.rv;
var nrv = BlenderExport.nrv;
var v = BlenderExport.v;
var n = BlenderExport.n;
if (!this.boneMatrices)
this.boneMatrices = [];
var boneMatrices = this.boneMatrices;
for (var i=0; i<this.vertexGroups.length; i++) {
var vg = this.vertexGroups[i];
boneMatrices[i] = this.bones[vg][frame];
}
for (var i=0; i<this.weights.length; i++) {
var wt = this.weights[i];
v[0] = this.vertices[i*3+0];
v[1] = this.vertices[i*3+1];
v[2] = this.vertices[i*3+2];
v[3] = 1;
n[0] = this.normals[i*3+0];
n[1] = this.normals[i*3+1];
n[2] = this.normals[i*3+2];
n[3] = 0;
rv[0] = rv[1] = rv[2] = 0;
nrv[0] = nrv[1] = nrv[2] = 0;
var totalWeight = 0;
if (wt.length == 0) {
totalWeight = 1;
vec3.setLeft(rv, v);
vec3.setLeft(nrv, n);
}
for (var j=0; j<wt.length; j++) {
var boneIndex = wt[j][0];
var weight = wt[j][1];
totalWeight += weight;
var mat = boneMatrices[boneIndex];
if (!mat) continue;
mat4.multiplyVec4(mat, v, tmpVec);
mat4.multiplyVec4(mat, n, tmpNo);
vec3.scale(tmpVec, weight, tmpVec);
vec3.add(rv, tmpVec, rv);
vec3.scale(tmpNo, weight, tmpNo);
vec3.add(nrv, tmpNo, nrv);
}
vec3.scale(rv, 1/totalWeight);
vec3.scale(nrv, 1/totalWeight);
this.frameVertices[i*3+0] = rv[0];
this.frameVertices[i*3+1] = rv[1];
this.frameVertices[i*3+2] = rv[2];
this.frameNormals[i*3+0] = nrv[0];
this.frameNormals[i*3+1] = nrv[1];
this.frameNormals[i*3+2] = nrv[2];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment