Skip to content

Commit a7e3b1e

Browse files
committed
optimize transforms
1 parent 3acacd3 commit a7e3b1e

File tree

3 files changed

+185
-147
lines changed

3 files changed

+185
-147
lines changed

glutil-transform.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ Transform.prototype.setMatrix=function(value){
8383
}
8484
Transform.prototype.isIdentity=function(){
8585
if(this._matrixDirty){
86-
this.getMatrix();
86+
if(this.complexMatrix){
87+
this.getMatrix();
88+
} else {
89+
return this.position[0]==0 && this.position[1]==0 &&
90+
this.position[2]==0 && this.scale[0]==1 &&
91+
this.scale[1]==1 && this.scale[2]==1 &&
92+
GLMath.quatIsIdentity(this.rotation);
93+
}
8794
}
8895
return this._isIdentity;
8996
}
@@ -303,26 +310,35 @@ Transform.prototype.multOrientation=function(angle, v,vy,vz){
303310
Transform.prototype.getMatrix=function(){
304311
if(this._matrixDirty){
305312
this._matrixDirty=false;
306-
// for best results, multiply in this order:
307-
// 1. translation
313+
if(GLMath.quatIsIdentity(this.rotation)){
314+
this.matrix=[this.scale[0],0,0,0,0,
315+
this.scale[1],0,0,0,0,
316+
this.scale[2],0,
317+
this.position[0],
318+
this.position[1],
319+
this.position[2],1];
320+
this._isIdentity=(this.position[0]==0 && this.position[1]==0 &&
321+
this.position[2]==0 && this.scale[0]==1 &&
322+
this.scale[1]==1 && this.scale[2]==1);
323+
} else {
324+
// for best results, multiply in this order:
325+
// 1. translation
308326
this.matrix=[1,0,0,0,0,1,0,0,0,0,1,0,
309327
this.position[0],
310328
this.position[1],
311329
this.position[2],1];
312-
// 2. rotation
313-
if(!GLMath.quatIsIdentity(this.rotation)){
330+
// 2. rotation
314331
this.matrix=GLMath.mat4multiply(this.matrix,
315332
GLMath.quatToMat4(this.rotation));
333+
// 3. scaling
334+
GLMath.mat4scaleInPlace(this.matrix,this.scale);
335+
this._isIdentity=(
336+
m[0]==1 && m[1]==0 && m[2]==0 && m[3]==0 &&
337+
m[4]==0 && m[5]==1 && m[6]==0 && m[7]==0 &&
338+
m[8]==0 && m[9]==0 && m[10]==1 && m[11]==0 &&
339+
m[12]==0 && m[13]==0 && m[14]==0 && m[15]==1
340+
);
316341
}
317-
// 3. scaling
318-
GLMath.mat4scaleInPlace(this.matrix,this.scale);
319-
var m=this.matrix
320-
this._isIdentity=(
321-
m[0]==1 && m[1]==0 && m[2]==0 && m[3]==0 &&
322-
m[4]==0 && m[5]==1 && m[6]==0 && m[7]==0 &&
323-
m[8]==0 && m[9]==0 && m[10]==1 && m[11]==0 &&
324-
m[12]==0 && m[13]==0 && m[14]==0 && m[15]==1
325-
);
326342
} else if(this._isIdentity){
327343
return GLMath.mat4identity();
328344
}

glutil.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@ Texture.loadTexture=function(name, textureCache){
883883
});
884884
}
885885

886+
886887
/**
887888
* Creates a texture from a byte array specifying the texture data.
888889
* @param {Uint8Array} array A byte array containing the texture data,
@@ -1906,11 +1907,30 @@ Scene3D.prototype.setPointLight=function(index,position,diffuse,specular){
19061907
return this;
19071908
}
19081909
/** @private */
1910+
Scene3D._isIdentityExceptTranslate=function(mat){
1911+
return (
1912+
mat[0]==1 && mat[1]==0 && mat[2]==0 && mat[3]==0 &&
1913+
mat[4]==0 && mat[5]==1 && mat[6]==0 && mat[7]==0 &&
1914+
mat[8]==0 && mat[9]==0 && mat[10]==1 && mat[11]==0 &&
1915+
mat[15]==1
1916+
);
1917+
};
1918+
/** @private */
19091919
Scene3D.prototype._setupMatrices=function(shape,program){
19101920
var uniforms={};
19111921
var currentMatrix=shape.getMatrix();
1912-
var viewWorld=GLMath.mat4multiply(this._viewMatrix,
1922+
var viewWorld;
1923+
if(Scene3D._isIdentityExceptTranslate(this._viewMatrix)){
1924+
// view matrix is just a translation matrix, so that getting the model-view
1925+
// matrix amounts to simply adding the view's position
1926+
viewWorld=currentMatrix.slice(0,16);
1927+
viewWorld[13]+=this._viewMatrix[13];
1928+
viewWorld[14]+=this._viewMatrix[14];
1929+
viewWorld[15]+=this._viewMatrix[15];
1930+
} else {
1931+
viewWorld=GLMath.mat4multiply(this._viewMatrix,
19131932
currentMatrix);
1933+
}
19141934
var invTrans=GLMath.mat4inverseTranspose3(viewWorld);
19151935
uniforms["world"]=currentMatrix;
19161936
uniforms["modelMatrix"]=currentMatrix;

0 commit comments

Comments
 (0)