Skip to content

Commit

Permalink
optimize transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Apr 30, 2015
1 parent 3acacd3 commit a7e3b1e
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 147 deletions.
44 changes: 30 additions & 14 deletions glutil-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ Transform.prototype.setMatrix=function(value){
}
Transform.prototype.isIdentity=function(){
if(this._matrixDirty){
this.getMatrix();
if(this.complexMatrix){
this.getMatrix();
} else {
return this.position[0]==0 && this.position[1]==0 &&
this.position[2]==0 && this.scale[0]==1 &&
this.scale[1]==1 && this.scale[2]==1 &&
GLMath.quatIsIdentity(this.rotation);
}
}
return this._isIdentity;
}
Expand Down Expand Up @@ -303,26 +310,35 @@ Transform.prototype.multOrientation=function(angle, v,vy,vz){
Transform.prototype.getMatrix=function(){
if(this._matrixDirty){
this._matrixDirty=false;
// for best results, multiply in this order:
// 1. translation
if(GLMath.quatIsIdentity(this.rotation)){
this.matrix=[this.scale[0],0,0,0,0,
this.scale[1],0,0,0,0,
this.scale[2],0,
this.position[0],
this.position[1],
this.position[2],1];
this._isIdentity=(this.position[0]==0 && this.position[1]==0 &&
this.position[2]==0 && this.scale[0]==1 &&
this.scale[1]==1 && this.scale[2]==1);
} else {
// for best results, multiply in this order:
// 1. translation
this.matrix=[1,0,0,0,0,1,0,0,0,0,1,0,
this.position[0],
this.position[1],
this.position[2],1];
// 2. rotation
if(!GLMath.quatIsIdentity(this.rotation)){
// 2. rotation
this.matrix=GLMath.mat4multiply(this.matrix,
GLMath.quatToMat4(this.rotation));
// 3. scaling
GLMath.mat4scaleInPlace(this.matrix,this.scale);
this._isIdentity=(
m[0]==1 && m[1]==0 && m[2]==0 && m[3]==0 &&
m[4]==0 && m[5]==1 && m[6]==0 && m[7]==0 &&
m[8]==0 && m[9]==0 && m[10]==1 && m[11]==0 &&
m[12]==0 && m[13]==0 && m[14]==0 && m[15]==1
);
}
// 3. scaling
GLMath.mat4scaleInPlace(this.matrix,this.scale);
var m=this.matrix
this._isIdentity=(
m[0]==1 && m[1]==0 && m[2]==0 && m[3]==0 &&
m[4]==0 && m[5]==1 && m[6]==0 && m[7]==0 &&
m[8]==0 && m[9]==0 && m[10]==1 && m[11]==0 &&
m[12]==0 && m[13]==0 && m[14]==0 && m[15]==1
);
} else if(this._isIdentity){
return GLMath.mat4identity();
}
Expand Down
22 changes: 21 additions & 1 deletion glutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,7 @@ Texture.loadTexture=function(name, textureCache){
});
}


/**
* Creates a texture from a byte array specifying the texture data.
* @param {Uint8Array} array A byte array containing the texture data,
Expand Down Expand Up @@ -1906,11 +1907,30 @@ Scene3D.prototype.setPointLight=function(index,position,diffuse,specular){
return this;
}
/** @private */
Scene3D._isIdentityExceptTranslate=function(mat){
return (
mat[0]==1 && mat[1]==0 && mat[2]==0 && mat[3]==0 &&
mat[4]==0 && mat[5]==1 && mat[6]==0 && mat[7]==0 &&
mat[8]==0 && mat[9]==0 && mat[10]==1 && mat[11]==0 &&
mat[15]==1
);
};
/** @private */
Scene3D.prototype._setupMatrices=function(shape,program){
var uniforms={};
var currentMatrix=shape.getMatrix();
var viewWorld=GLMath.mat4multiply(this._viewMatrix,
var viewWorld;
if(Scene3D._isIdentityExceptTranslate(this._viewMatrix)){
// view matrix is just a translation matrix, so that getting the model-view
// matrix amounts to simply adding the view's position
viewWorld=currentMatrix.slice(0,16);
viewWorld[13]+=this._viewMatrix[13];
viewWorld[14]+=this._viewMatrix[14];
viewWorld[15]+=this._viewMatrix[15];
} else {
viewWorld=GLMath.mat4multiply(this._viewMatrix,
currentMatrix);
}
var invTrans=GLMath.mat4inverseTranspose3(viewWorld);
uniforms["world"]=currentMatrix;
uniforms["modelMatrix"]=currentMatrix;
Expand Down
Loading

0 comments on commit a7e3b1e

Please sign in to comment.