Skip to content

Commit bbb6670

Browse files
committed
optimize setUniforms; add Transform methods; expand color3 method; etc.
1 parent ea5f9ef commit bbb6670

File tree

6 files changed

+239
-172
lines changed

6 files changed

+239
-172
lines changed

glmath.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,6 @@ mat4transposeInPlace:function(mat){
894894
* can't be inverted, returns the identity 3x3 matrix.
895895
*/
896896
mat4inverseTranspose3:function(m4){
897-
// Operation equivalent to transpose(invert(mat3(m4)))
898897
var m=[m4[0],m4[1],m4[2],m4[4],m4[5],m4[6],
899898
m4[8],m4[9],m4[10]];
900899
var det=m[0] * m[4] * m[8] +

glutil-mesh.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@ Mesh.prototype.normal3=function(x,y,z){
314314
* defined (with vertex3()) will have this color. The new current
315315
* color will apply to future vertices even if the current mode
316316
* is TRIANGLE_FAN and some vertices were already given for
317-
* that mode.
318-
* @param {number} r Red component of the color (0-1).
319-
* Can also be a string
317+
* that mode. Only the red, green, and blue components will be used.
318+
* @param {Array<number>|number|string} r Array of three or
319+
* four color components; or the red color component (0-1); or a string
320320
* specifying an [HTML or CSS color]{@link glutil.GLUtil.toGLColor}.
321-
* @param {number} g Green component of the color (0-1).
322-
* May be null or omitted if a string is given as the "r" parameter.
323-
* @param {number} b Blue component of the color (0-1).
324-
* May be null or omitted if a string is given as the "r" parameter.
321+
* @param {number} g Green color component (0-1).
322+
* May be null or omitted if a string or array is given as the "r" parameter.
323+
* @param {number} b Blue color component (0-1).
324+
* May be null or omitted if a string or array is given as the "r" parameter.
325325
* @return {glutil.Mesh} This object.
326326
*/
327327
Mesh.prototype.color3=function(x,y,z){
@@ -330,10 +330,15 @@ Mesh.prototype.normal3=function(x,y,z){
330330
this.color[0]=c[0];
331331
this.color[1]=c[1];
332332
this.color[2]=c[2];
333-
} else {
333+
} else if(typeof x=="number" && typeof y=="number" &&
334+
typeof z=="number"){
334335
this.color[0]=x;
335336
this.color[1]=y;
336337
this.color[2]=z;
338+
} else {
339+
this.color[0]=x[0];
340+
this.color[1]=x[1];
341+
this.color[2]=x[2];
337342
}
338343
this._elementsDefined|=Mesh.COLORS_BIT;
339344
return this;

glutil-shaderprog.js

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,26 @@ ShaderProgram.prototype._log=function(i,v){
156156
//console.log("setting "+i+": "+v);
157157
}
158158
/** @private */
159+
ShaderProgram.prototype._saveIfNotCurrent=function(v,i,isCurrentProgram){
160+
this._log(i,v);
161+
if(isCurrentProgram==null){
162+
isCurrentProgram=this.context.getParameter(
163+
this.context.CURRENT_PROGRAM)==this.program;
164+
}
165+
if(!isCurrentProgram){
166+
// Save this uniform to write later
167+
this.savedUniforms[i]=(typeof v=="number") ? v : v.slice(0,v.length);
168+
this.uniformValues[i]=null;
169+
}
170+
return isCurrentProgram;
171+
}
172+
/** @private */
159173
ShaderProgram.prototype._setUniform=function(uniforms,i,isCurrentProgram){
160174
var isCurrentProgram=null;
161175
var v=uniforms[i];
162176
var uniform=this.get(i);
163177
if(uniform===null)return isCurrentProgram;
164-
if(isCurrentProgram==null){
165-
isCurrentProgram=this.context.getParameter(
166-
this.context.CURRENT_PROGRAM)==this.program;
167-
}
168178
var uv=this.uniformValues[i];
169-
if(!isCurrentProgram){
170-
// Save this uniform to write later
171-
this.savedUniforms[i]=(v instanceof Array) ? v.slice(0,v.length) : v;
172-
return isCurrentProgram;
173-
}
174179
if(typeof v=="number"){
175180
var newUv=false;
176181
if(uv==null){
@@ -182,62 +187,74 @@ ShaderProgram.prototype._setUniform=function(uniforms,i,isCurrentProgram){
182187
newUv=true;
183188
}
184189
if(newUv){
185-
if(this.uniformTypes[i]==this.context.FLOAT){
190+
if(this.uniformTypes[i]==this.context.FLOAT){
191+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
192+
return isCurrentProgram;
186193
this.context.uniform1f(uniform, uv);
187-
this._log(i,v);
188194
} else {
195+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
196+
return isCurrentProgram;
189197
this.context.uniform1i(uniform, uv);
190-
this._log(i,v);
191198
}
192199
}
193200
}
194201
else if(v.length==3){
195202
if(!uv){
196203
this.uniformValues[i]=uv=v.slice(0,v.length)
197-
this.context.uniform3f(uniform, uv[0],uv[1],uv[2]);
198-
this._log(i,v);
204+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
205+
return isCurrentProgram;
206+
this.context.uniform3fv(uniform, uv);
199207
} else if(uv[0]!=v[0] || uv[1]!=v[1] || uv[2]!=v[2]){
200208
uv[0]=v[0]; uv[1]=v[1]; uv[2]=v[2];
201-
this._log(i,v);
202-
this.context.uniform3f(uniform, uv[0],uv[1],uv[2]);
209+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
210+
return isCurrentProgram;
211+
this.context.uniform3fv(uniform, uv);
203212
}
204213
} else if(v.length==2){
205214
if(!uv){
206215
this.uniformValues[i]=uv=v.slice(0,v.length)
207-
this.context.uniform2f(uniform, uv[0],uv[1]);
208-
this._log(i,v);
216+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
217+
return isCurrentProgram;
218+
this.context.uniform2fv(uniform, uv);
209219
} else if(uv[0]!=v[0] || uv[1]!=v[1]){
210220
uv[0]=v[0]; uv[1]=v[1];
211-
this.context.uniform2f(uniform, uv[0],uv[1]);
212-
this._log(i,v);
221+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
222+
return isCurrentProgram;
223+
this.context.uniform2fv(uniform, uv);
213224
}
214225
} else if(v.length==4){
215226
if(!uv){
216227
this.uniformValues[i]=uv=v.slice(0,v.length)
217-
this.context.uniform4f(uniform, uv[0],uv[1],uv[2],uv[3]);
218-
this._log(i,v);
228+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
229+
return isCurrentProgram;
230+
this.context.uniform4fv(uniform, uv);
219231
} else if(uv[0]!=v[0] || uv[1]!=v[1] || uv[2]!=v[2] || uv[3]!=v[3]){
220232
uv[0]=v[0]; uv[1]=v[1]; uv[2]=v[2]; uv[3]=v[3];
221-
this.context.uniform4f(uniform, uv[0],uv[1],uv[2],uv[3]);
222-
this._log(i,v);
233+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
234+
return isCurrentProgram;
235+
this.context.uniform4fv(uniform, uv);
223236
}
224237
} else if(v.length==16){
225238
if(!uv){
226239
this.uniformValues[i]=uv=v.slice(0,v.length)
240+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
241+
return isCurrentProgram;
227242
this.context.uniformMatrix4fv(uniform,false,uv);
228-
this._log(i,v);
229243
} else if(ShaderProgram._copyIfDifferent(v,uv,16)){
244+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
245+
return isCurrentProgram;
230246
this.context.uniformMatrix4fv(uniform,false,uv);
231-
this._log(i,v);
232247
}
233248
} else if(v.length==9){
234249
if(!uv){
235250
this.uniformValues[i]=uv=v.slice(0,v.length)
251+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
252+
return isCurrentProgram;
236253
this.context.uniformMatrix3fv(uniform,false,uv);
237-
this._log(i,v);
238254
} else if(ShaderProgram._copyIfDifferent(v,uv,9)){
255+
if(!(isCurrentProgram=this._saveIfNotCurrent(uv,i,isCurrentProgram)))
256+
return isCurrentProgram;
239257
this.context.uniformMatrix3fv(uniform,false,uv);
240-
this._log(i,v);
241258
}
242259
}
243260
return isCurrentProgram;

glutil-transform.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ Transform.prototype.resetTransform=function(){
105105
Transform.prototype.setScale=function(x,y,z){
106106
if(this.complexMatrix)return this;
107107
if(x!=null && y==null && z==null){
108-
if(x.constructor==Array)
109-
this.scale=x.slice(0,3);
108+
if(typeof x!="number")
109+
this.scale=[x[0],x[1],x[2]];
110110
else
111111
this.scale=[x,x,x];
112112
} else {
@@ -134,8 +134,8 @@ Transform.prototype.setScale=function(x,y,z){
134134
Transform.prototype.setPosition=function(x,y,z){
135135
if(this.complexMatrix)return this;
136136
if(x!=null && y==null && z==null){
137-
if(x.constructor==Array)
138-
this.position=x.slice(0,3);
137+
if(typeof x!="number")
138+
this.position=[x[0],x[1],x[2]];
139139
else
140140
this.position=[x,x,x];
141141
} else {
@@ -148,6 +148,43 @@ Transform.prototype.setPosition=function(x,y,z){
148148
this._matrixDirty=true;
149149
return this;
150150
}
151+
152+
/**
153+
* Moves the relative position of an object from its original
154+
* position. Has no effect if a matrix was defined with {@link glutil.Transform#setMatrix}
155+
* and the transform wasn't reset yet with {@link glutil.Transform#resetTransform}.
156+
* @param {Array<number>|number} x Number to add to the X-coordinate,
157+
* or an array of 3 numbers giving the numbers to add to the x, y, and z coordinates.
158+
* @param {number} y Number to add to the Y-coordinate.
159+
* If "x" is an array, this parameter may be omitted.
160+
* @param {number} z Number to add to the Z-coordinate.
161+
* If "x" is an array, this parameter may be omitted.
162+
* @return {glutil.Transform} This object.
163+
*/
164+
Transform.prototype.movePosition=function(x,y,z){
165+
if(this.complexMatrix)return this;
166+
if(x!=null && y==null && z==null){
167+
if(typeof x!="number"){
168+
this.position[0]+=x[0];
169+
this.position[1]+=x[1];
170+
this.position[2]+=x[2];
171+
} else {
172+
this.position[0]+=x;
173+
this.position[1]+=x;
174+
this.position[2]+=x;
175+
}
176+
} else {
177+
this.position[0]+=x;
178+
this.position[1]+=y;
179+
this.position[2]+=z;
180+
}
181+
this._isIdentity=(this._isIdentity &&
182+
this.position[0]==0 &&
183+
this.position[1]==0 &&
184+
this.position[2]==0);
185+
this._matrixDirty=true;
186+
return this;
187+
}
151188
/**
152189
* Sets this object's orientation in the form of a [quaternion]{@tutorial glmath} (a 4-element array
153190
* for describing 3D rotations). Has no effect if a matrix was defined with {@link glutil.Transform#setMatrix}

glutil.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,12 @@ function Material(ambient, diffuse, specular,shininess,emission) {
689689
*/
690690
this.diffuse=diffuse ? diffuse.slice(0,diffuse.length) : [0.8,0.8,0.8,1.0];
691691
/** Specular highlight reflection of this material.
692-
* Similar to diffuse reflection, specular reflection
693-
* is affected by how directly each part of an object faces
694-
* a light that gives off specular highlights, but unlike diffuse light,
695-
* specular light doesn't scatter very much to other parts of an object.
696-
* Specular reflection can make an object shiny.
692+
* Specular reflection is a reflection in the same angle as
693+
* the light reaches the material, similar to a mirror. As a result, depending
694+
* on the viewing angle, specular reflection can give off
695+
* shiny highlights on the material.<p>
697696
* This value is a 3-element array giving the red, green, and blue
698-
* components of the ambient reflection; the final specular color depends
697+
* components of the specular reflection; the final specular color depends
699698
* on the specular color of lights that shine on the material.
700699
* (0,0,0) means no specular reflection,
701700
* and (1,1,1) means total specular reflection.<p>
@@ -1029,6 +1028,9 @@ BufferedSubMesh.prototype.draw=function(program){
10291028
_vertexAttrib(context,
10301029
attr, 3,
10311030
context.FLOAT, stride*4, offset*4);
1031+
} else {
1032+
attr=program.get("normal");
1033+
if(attr!==null)context.disableVertexAttribArray(attr);
10321034
}
10331035
offset=Mesh.colorOffset(format);
10341036
if(offset>=0){
@@ -1040,6 +1042,8 @@ BufferedSubMesh.prototype.draw=function(program){
10401042
context.FLOAT, stride*4, offset*4);
10411043
} else {
10421044
program.setUniforms({"useColorAttr":0.0});
1045+
attr=program.get("uv");
1046+
if(attr!==null)context.disableVertexAttribArray(attr);
10431047
}
10441048
offset=Mesh.texCoordOffset(format);
10451049
if(offset>=0){
@@ -1048,6 +1052,9 @@ BufferedSubMesh.prototype.draw=function(program){
10481052
_vertexAttrib(context,
10491053
attr, 2,
10501054
context.FLOAT, stride*4, offset*4);
1055+
} else {
1056+
attr=program.get("uv");
1057+
if(attr!==null)context.disableVertexAttribArray(attr);
10511058
}
10521059
// Drawing phase
10531060
var context=program.getContext();
@@ -1067,7 +1074,7 @@ BufferedSubMesh.prototype.draw=function(program){
10671074
// drawElements calls (in case a future vertex buffer object
10681075
// that doesn't use the attribute needs to be drawn)
10691076
for(var i=0;i<boundAttributes.length;i++){
1070-
context.disableVertexAttribArray(boundAttributes[i]);
1077+
//context.disableVertexAttribArray(boundAttributes[i]);
10711078
}
10721079
}
10731080
/**

0 commit comments

Comments
 (0)