Skip to content

Commit 3acacd3

Browse files
committed
expand loadAndMapTexture; add starfield.html demo; optimize getMatrix methods of Shape and ShapeGroup
1 parent e05771c commit 3acacd3

File tree

8 files changed

+308
-155
lines changed

8 files changed

+308
-155
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ and applying them to 3D shapes.
4343
* [tris.html](https://peteroupc.github.io/html3dutil/tris.html) - Demonstrates a particle system.
4444
* [gradient.html](https://peteroupc.github.io/html3dutil/gradient.html) - Demonstrates generating a custom
4545
texture -- a linear gradient from one color to another.
46+
* [starfield.html](https://peteroupc.github.io/html3dutil/starfield.html) - Demo of a star field.
4647

4748
Examples
4849
---------

camera.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Camera.prototype._updateView=function(){
4949
* @param {*} dist
5050
*/
5151
Camera.prototype.setDistance=function(dist){
52+
// don't move closer than the near plane
5253
this.distance=Math.max(this.near,dist);
5354
this.position=[0,0,this.distance]
5455
return this._updateView();
@@ -200,6 +201,7 @@ InputTracker.prototype.mousewheel=function(func){
200201
"click":click,
201202
"x":clientX,
202203
"y":clientY});
204+
e.preventDefault();
203205
}
204206
if("onmousewheel" in this.element){
205207
this.element.addEventListener("mousewheel",cb);

glmath.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,19 @@ mat4copy:function(mat){
342342
vec4copy:function(vec){
343343
return vec.slice(0,4);
344344
},
345+
/**
346+
* Returns whether a 4x4 matrix is the identity matrix.
347+
* @param {Array<number>} mat A 4x4 matrix.
348+
* @return {boolean}
349+
*/
350+
mat4isIdentity:function(mat){
351+
return (
352+
mat[0]==1 && mat[1]==0 && mat[2]==0 && mat[3]==0 &&
353+
mat[4]==0 && mat[5]==1 && mat[6]==0 && mat[7]==0 &&
354+
mat[8]==0 && mat[9]==0 && mat[10]==1 && mat[11]==0 &&
355+
mat[12]==0 && mat[13]==0 && mat[14]==0 && mat[15]==1
356+
);
357+
},
345358
/**
346359
* Finds the inverse of a 4x4 matrix.
347360
* @param {Array<number>} m A 4x4 matrix.

glutil-transform.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Transform.prototype.reset=function(){
5454
this.rotation=GLMath.quatIdentity();
5555
this.complexMatrix=false;
5656
this._matrixDirty=false;
57+
this._isIdentity=true;
5758
return this;
5859
}
5960
/**
@@ -66,16 +67,26 @@ Transform.prototype.reset=function(){
6667
*/
6768
Transform.prototype.setMatrix=function(value){
6869
this._matrixDirty=false;
69-
this._isIdentity=false;
7070
this.complexMatrix=true;
7171
this.matrix=value.slice(0,16);
7272
this.position=[this.matrix[12],this.matrix[13],this.matrix[14]];
7373
this.rotation=GLMath.quatFromMat4(this.matrix);
7474
this.rotation=GLMath.quatNormInPlace(this.rotation);
7575
this.scale=[this.matrix[0],this.matrix[5],this.matrix[10]];
76+
this._isIdentity=(
77+
value[0]==1 && value[1]==0 && value[2]==0 && value[3]==0 &&
78+
value[4]==0 && value[5]==1 && value[6]==0 && value[7]==0 &&
79+
value[8]==0 && value[9]==0 && value[10]==1 && value[11]==0 &&
80+
value[12]==0 && value[13]==0 && value[14]==0 && value[15]==1
81+
);
7682
return this;
7783
}
78-
84+
Transform.prototype.isIdentity=function(){
85+
if(this._matrixDirty){
86+
this.getMatrix();
87+
}
88+
return this._isIdentity;
89+
}
7990
/**
8091
* Resets this transform to the untransformed state.
8192
* @return {glutil.Transform} This object.
@@ -206,7 +217,6 @@ Transform.prototype.setQuaternion=function(quat){
206217
if(this.complexMatrix)return this;
207218
this.rotation=quat.slice(0,4);
208219
GLMath.quatNormInPlace(this.rotation);
209-
this._isIdentity=false;
210220
this._matrixDirty=true;
211221
return this;
212222
}
@@ -258,7 +268,6 @@ Transform.prototype.multQuaternion=function(quat){
258268
if(this.complexMatrix)return this;
259269
this.rotation=GLMath.quatNormInPlace(
260270
GLMath.quatMultiply(this.rotation,quat));
261-
this._isIdentity=false;
262271
this._matrixDirty=true;
263272
return this;
264273
}
@@ -292,9 +301,6 @@ Transform.prototype.multOrientation=function(angle, v,vy,vz){
292301
* @return {Array<number>}
293302
*/
294303
Transform.prototype.getMatrix=function(){
295-
if(this._isIdentity){
296-
return GLMath.mat4identity();
297-
}
298304
if(this._matrixDirty){
299305
this._matrixDirty=false;
300306
// for best results, multiply in this order:
@@ -310,6 +316,15 @@ Transform.prototype.getMatrix=function(){
310316
}
311317
// 3. scaling
312318
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+
);
326+
} else if(this._isIdentity){
327+
return GLMath.mat4identity();
313328
}
314329
return this.matrix.slice(0,16);
315330
}

glutil.js

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

886-
887886
/**
888887
* Creates a texture from a byte array specifying the texture data.
889888
* @param {Uint8Array} array A byte array containing the texture data,
@@ -1689,18 +1688,25 @@ Scene3D.prototype.loadTexture=function(name){
16891688
/**
16901689
* Loads a texture from an image URL and uploads it
16911690
* to a texture buffer object.
1692-
* @param {string} name URL of the image to load.
1691+
* @param {string|glutil.Texture} texture String giving the
1692+
* URL of the image to load, or
1693+
* a Texture object whose data may or may not be loaded.
16931694
* @return {Promise} A promise that is resolved when
16941695
* the image is loaded successfully and uploaded
16951696
* to a texture buffer (the result will be a Texture
16961697
* object), and is rejected when an error occurs.
16971698
*/
1698-
Scene3D.prototype.loadAndMapTexture=function(name){
1699+
Scene3D.prototype.loadAndMapTexture=function(texture){
16991700
var context=this.context;
1700-
return Texture.loadTexture(
1701-
name, this.context, this.textureCache).then(function(texture){
1702-
texture.loadedTexture=new LoadedTexture(texture,context);
1703-
return texture;
1701+
var tex=null;
1702+
if(texture.constructor==Texture){
1703+
tex=texture.loadImage();
1704+
} else {
1705+
tex=Texture.loadTexture(texture, this.context, this.textureCache)
1706+
}
1707+
return tex.then(function(textureInner){
1708+
textureInner.loadedTexture=new LoadedTexture(textureInner,context);
1709+
return textureInner;
17041710
});
17051711
}
17061712
/**
@@ -2058,10 +2064,19 @@ ShapeGroup.prototype.getTransform=function(){
20582064
* Not documented yet.
20592065
*/
20602066
ShapeGroup.prototype.getMatrix=function(){
2061-
var mat=this.getTransform().getMatrix();
2067+
var xform=this.getTransform();
2068+
var thisIdentity=xform.isIdentity();
20622069
if(this.parent!=null){
20632070
var pmat=this.parent.getMatrix();
2064-
mat=GLMath.mat4multiply(pmat,mat);
2071+
if(thisIdentity){
2072+
mat=GLMath.mat4multiply(pmat,xform.getMatrix());
2073+
} else if(GLMath.mat4isIdentity(pmat)){
2074+
mat=xform.getMatrix();
2075+
} else {
2076+
mat=pmat;
2077+
}
2078+
} else {
2079+
mat=xform.getMatrix();
20652080
}
20662081
return mat;
20672082
}
@@ -2289,10 +2304,19 @@ Shape.prototype.setQuaternion=function(quat){
22892304
* @return {Array<number>} The current transformation matrix.
22902305
*/
22912306
Shape.prototype.getMatrix=function(){
2292-
var mat=this.getTransform().getMatrix();
2307+
var xform=this.getTransform();
2308+
var thisIdentity=xform.isIdentity();
22932309
if(this.parent!=null){
22942310
var pmat=this.parent.getMatrix();
2295-
mat=GLMath.mat4multiply(pmat,mat);
2311+
if(thisIdentity){
2312+
mat=pmat;
2313+
} else if(GLMath.mat4isIdentity(pmat)){
2314+
mat=xform.getMatrix();
2315+
} else {
2316+
mat=GLMath.mat4multiply(pmat,xform.getMatrix());
2317+
}
2318+
} else {
2319+
mat=xform.getMatrix();
22962320
}
22972321
return mat;
22982322
}

0 commit comments

Comments
 (0)