Skip to content

Commit 1f59429

Browse files
committed
expand gradient.html demo
1 parent fc4c265 commit 1f59429

File tree

9 files changed

+102
-32
lines changed

9 files changed

+102
-32
lines changed

camera.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,30 @@ Camera.prototype._updateView=function(){
4444
this.scene.setViewMatrix(GLMath.mat4invert(look));
4545
return this;
4646
}
47+
/**
48+
* Not documented yet.
49+
* @param {*} dist
50+
*/
4751
Camera.prototype.setDistance=function(dist){
4852
this.distance=Math.max(this.near,dist);
4953
this.position=[0,0,this.distance]
5054
return this._updateView();
5155
}
52-
56+
/** @private */
5357
Camera.prototype._orbit=function(deltaMouseX,deltaMouseY,angleMultiplier){
5458
var x=deltaMouseX
5559
var y=deltaMouseY
5660
this.angleX+=x*angleMultiplier;
5761
this.angleY+=y*angleMultiplier;
58-
this.angleX=((this.angleX>=0 && this.angleX<360) ? this.angleX :
62+
this.angleX=((this.angleX>=0 && this.angleX<360) ? this.angleX :
5963
((this.angleX%360)+(this.angleX<0 ? 360 : 0)));
6064
if(this.angleY>=89.99999)this.angleY=89.99999;
61-
if(this.angleY<=-89.99999)this.angleY=-89.99999;
65+
if(this.angleY<=-89.99999)this.angleY=-89.99999;
6266
this.inverseQuat=GLMath.quatFromTaitBryan(
6367
this.angleY,this.angleX,0,GLMath.RollYawPitch);
6468
this.inverseQuat=GLMath.quatInverse(this.inverseQuat);
6569
}
66-
70+
/** @private */
6771
Camera.prototype._trackball=function(deltaMouseX,deltaMouseY,angleMultiplier){
6872
var x=deltaMouseX
6973
var y=deltaMouseY
@@ -77,19 +81,26 @@ Camera.prototype._trackball=function(deltaMouseX,deltaMouseY,angleMultiplier){
7781
GLMath.quatNormInPlace(this.inverseQuat);
7882
}
7983
}
84+
/**
85+
* Not documented yet.
86+
* @param {*} e
87+
*/
8088
Camera.prototype.mousewheel=function(e){
8189
var ticks=e.delta/120.0;
8290
// mousewheel up (negative) means move forward,
8391
// mousewheel down (positive) means move back
8492
this.setDistance(this.distance-0.6*ticks)
8593
}
94+
/**
95+
* Not documented yet.
96+
*/
8697
Camera.prototype.update=function(){
8798
var delta=this.input.deltaXY();
8899
if(this.input.leftButton){
89100
if(this.trackballMode){
90101
this._trackball(delta.x,delta.y,0.3);
91102
} else {
92-
this._orbit(delta.x,delta.y,0.3);
103+
this._orbit(delta.x,delta.y,0.3);
93104
}
94105
} else if(this.input.middleButton){
95106
this.setDistance(this.distance+0.2*delta.y)
@@ -165,6 +176,10 @@ function InputTracker(element){
165176
mouseEvent(thisObj,{"target":e.target,"isDown":false,"button":-1});
166177
})
167178
};
179+
/**
180+
* Not documented yet.
181+
* @param {*} func
182+
*/
168183
InputTracker.prototype.mousewheel=function(func){
169184
var cb=function(e, click){
170185
var clientX=e.clientX-InputTracker._getPageX(e.target);
@@ -189,11 +204,10 @@ InputTracker.prototype.mousewheel=function(func){
189204
if("onmousewheel" in this.element){
190205
this.element.addEventListener("mousewheel",cb);
191206
} else {
192-
this.element.addEventListener("DOMMouseScroll",cb);
207+
this.element.addEventListener("DOMMouseScroll",cb);
193208
}
194209
}
195210

196-
197211
/** @private */
198212
InputTracker._getPageX=function(o) {
199213
"use strict";

glmath.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,15 +1103,15 @@ mat4translate:function(mat,v3,v3y,v3z){
11031103
* the near clipping plane. Objects closer than this distance won't be
11041104
* seen.<p>This value should not be 0 or less, and should be set to the highest distance
11051105
* from the camera that the application can afford to clip out for being too
1106-
* close, for example, 0.5, 1, or higher.
1106+
* close, for example, 0.5, 1, or higher.
11071107
* @param {number} far The distance from the camera to
11081108
* the far clipping plane. Objects beyond this distance will be too far
11091109
* to be seen. This value should be greater than "near" and be set so that the ratio of "far" to "near"
11101110
* is as small as the application can accept.<p>
11111111
* (Depth buffers often allow only 65536 possible values per pixel,
11121112
* which are more spread out toward the far clipping plane than toward the
1113-
* near plane due to the perspective projection. The greater the ratio of "far" to
1114-
* "near", the more the values spread out, and the more likely two objects close
1113+
* near plane due to the perspective projection. The greater the ratio of "far" to
1114+
* "near", the more the values spread out, and the more likely two objects close
11151115
* to the far plane will have identical depth values.)
11161116
* @return {Array<number>} The resulting 4x4 matrix.
11171117
*/
@@ -1204,7 +1204,7 @@ mat4ortho:function(l,r,b,t,n,f){
12041204
* Returns a 4x4 matrix representing a perspective projection
12051205
* given an X-axis field of view.<p>
12061206
* This method assumes a right-hand coordinate system;
1207-
* see {@link glmath.GLMath.mat4perspective}. For considerations
1207+
* see {@link glmath.GLMath.mat4perspective}. For considerations
12081208
* when choosing the "n" and "f" parameters,
12091209
* see {@link glmath.GLMath.mat4perspective}.
12101210
* @param {number} fovX X-axis field of view, in degrees. Should be less

glutil-mesh.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ Mesh.prototype.normal3=function(x,y,z){
338338
} else {
339339
this.color[0]=x[0];
340340
this.color[1]=x[1];
341-
this.color[2]=x[2];
341+
this.color[2]=x[2];
342342
}
343343
this._elementsDefined|=Mesh.COLORS_BIT;
344344
return this;
@@ -796,7 +796,7 @@ function SubMesh(vertices,faces,format){
796796
(this.vertices.length-this.startIndex)>=(stride*3)){
797797
var index=(this.vertices.length/stride)-2;
798798
var firstIndex=(this.startIndex/stride);
799-
this._setTriangle(vertexStartIndex,stride,firstIndex,index,index+1);
799+
this._setTriangle(vertexStartIndex,stride,firstIndex,index,index+1);
800800
} else if(currentMode==Mesh.LINE_STRIP &&
801801
(this.vertices.length-this.startIndex)>=(stride*2)){
802802
var index=(this.vertices.length/stride)-2;

glutil-meshes.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ Meshes.createSphere=function(radius, slices, stacks, flat, inside){
517517
return Meshes._createCapsule(radius,0,slices,stacks,1,flat,inside);
518518
}
519519

520-
521-
522520
/**
523521
* Creates a mesh of a capsule, centered at the origin.
524522
* The length of the capsule will run along the z-axis. (If the capsule
@@ -528,16 +526,16 @@ Meshes.createSphere=function(radius, slices, stacks, flat, inside){
528526
* @param {number} radius Radius of each spherical
529527
* end of the capsule.
530528
* May be null or omitted, in which case the default is 1.
531-
* @param {number} length Length of the middle section.
529+
* @param {number} length Length of the middle section.
532530
* May be null or omitted, in which case the default is 1.
533531
* If this value is 0, an approximation to a sphere will be generated.
534532
* @param {number} slices Number of vertical sections the capsule consists
535533
* of. This function will create an octahedron if "slices" is 4 and "stacks" is 2.
536534
* Must be 3 or greater. May be null or omitted, in which case the default is 16.
537-
* @param {number} stacks Number of horizontal sections
535+
* @param {number} stacks Number of horizontal sections
538536
* each spherical half consists of.
539537
* May be null or omitted, in which case the default is 8.
540-
* @param {number} middleStacks Number of vertical sections
538+
* @param {number} middleStacks Number of vertical sections
541539
* the middle of the capsule consists of.
542540
* May be null or omitted, in which case the default is 1.
543541
* @param {boolean} flat If true, will generate normals such that the
@@ -630,7 +628,7 @@ Meshes._createCapsule=function(radius, length, slices, stacks, middleStacks, fla
630628
txs=(i<halfStacks) ? texStart*sphereRatio :
631629
(1.0-(1.0-texStart)*sphereRatio)
632630
txe=(i<halfStacks) ? texEnd*sphereRatio :
633-
(1.0-(1.0-texEnd)*sphereRatio)
631+
(1.0-(1.0-texEnd)*sphereRatio)
634632
}
635633
lastZeCen=zeCen;
636634
lastTex=texEnd;

glutil-shaderprog.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ ShaderProgram.prototype._saveIfNotCurrent=function(v,i,isCurrentProgram){
166166
// Save this uniform to write later
167167
this.savedUniforms[i]=(typeof v=="number") ? v : v.slice(0,v.length);
168168
this.uniformValues[i]=null;
169-
}
169+
}
170170
return isCurrentProgram;
171171
}
172172
/** @private */

glutil-transform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ Transform.prototype.movePosition=function(x,y,z){
171171
} else {
172172
this.position[0]+=x;
173173
this.position[1]+=x;
174-
this.position[2]+=x;
174+
this.position[2]+=x;
175175
}
176176
} else {
177177
this.position[0]+=x;
178178
this.position[1]+=y;
179-
this.position[2]+=z;
179+
this.position[2]+=z;
180180
}
181181
this._isIdentity=(this._isIdentity &&
182182
this.position[0]==0 &&

glutil.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ var GLUtil={
207207
* Loads a file from a URL asynchronously, using XMLHttpRequest.
208208
* @param {string} url URL of the file to load.
209209
* @param {string|null} responseType Expected data type of
210-
* the file. Can be "json", "xml", "text", or "arraybuffer".
210+
* the file. Can be "json", "xml", "text", or "arraybuffer".
211211
* If null or omitted, the default is "text".
212212
* @return {Promise} A promise that resolves when the data
213213
* file is loaded successfully (the result will be an object with
@@ -831,7 +831,7 @@ var Texture=function(name){
831831
}
832832

833833
/**
834-
* Sets the wrapping behavior of texture coordinates that
834+
* Sets the wrapping behavior of texture coordinates that
835835
* fall out of range when using this texture. This setting
836836
* will only have an effect on textures whose width and height
837837
* are both powers of two. For other textures, this setting
@@ -852,7 +852,7 @@ Texture.prototype.setClamp=function(clamp){
852852
/**
853853
* Loads a texture by its URL.
854854
* @param {string} name URL of the texture data. Images with a TGA
855-
* extension that use the RGBA or grayscale format are supported.
855+
* extension that use the RGBA or grayscale format are supported.
856856
* Images supported by the browser will be loaded via
857857
* the JavaScript DOM's Image class.
858858
* @param {Object} [textureCache] An object whose keys
@@ -881,6 +881,17 @@ Texture.loadTexture=function(name, textureCache){
881881
});
882882
}
883883

884+
885+
/**
886+
* Creates a texture from a byte array specifying the texture data.
887+
* @param {Uint8Array} array A byte array containing the texture data,
888+
* with the pixels arranged in left-to-right rows from top to bottom.
889+
* Each pixel takes 4 bytes, where the bytes are the red, green, blue,
890+
* and alpha components, in that order.
891+
* @param {Uint8Array} width Width, in pixels, of the texture.
892+
* @param {Uint8Array} height Height, in pixels, of the texture.
893+
* @return {glutil.Texture} The new Texture object.
894+
*/
884895
Texture.fromUint8Array=function(array, width, height){
885896
if(width<0)throw new Error("width less than 0")
886897
if(height<0)throw new Error("height less than 0")
@@ -941,7 +952,7 @@ Texture.loadTga=function(name){
941952
arr[io+2]=col
942953
arr[io+3]=0xFF
943954
offset++;
944-
}
955+
}
945956
}
946957
return {"width":width,"height":height,"image":arr}
947958
})
@@ -1701,7 +1712,7 @@ Scene3D.prototype.loadAndMapTexture=function(name){
17011712
* rejection).
17021713
* @return {Promise} A promise that is resolved when
17031714
* all the URLs in the textureFiles array are either resolved or rejected.
1704-
* The result will be an object with three properties:
1715+
* The result will be an object with three properties:
17051716
* "successes", "failures", and "results".
17061717
* See {@link glutil.GLUtil.getPromiseResults}.
17071718
*/

gradient.html

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
<script type="text/javascript" src="frame.js"></script>
55
</head>
66
<body>
7+
<a href="javascript:link1()">Red/blue linear</a>,
8+
<a href="javascript:link2()">Green/yellow linear</a>,
9+
<a href="javascript:link3()">Red/blue radial</a>,
10+
<a href="javascript:link4()">Green/yellow radial</a>
711
<canvas width="600" height="450" id=canvas></canvas>
812
<script id="demo">
913
//<!--
@@ -32,16 +36,59 @@
3236
var i=0;
3337
for(var y=0;y<size;y++){
3438
for(var x=0;x<size;x++,i+=4){
35-
gradient[i]=Math.floor(arr[x][0])
36-
gradient[i+1]=Math.floor(arr[x][1])
37-
gradient[i+2]=Math.floor(arr[x][2])
38-
gradient[i+3]=Math.floor(arr[x][3])
39+
gradient[i]=Math.round(arr[x][0])
40+
gradient[i+1]=Math.round(arr[x][1])
41+
gradient[i+2]=Math.round(arr[x][2])
42+
gradient[i+3]=Math.round(arr[x][3])
3943
}
4044
}
4145
return Texture.fromUint8Array(
4246
new Uint8Array(gradient),size,size).setClamp(true);
4347
}
4448

49+
// Generates a radial gradient
50+
function radialGradient(colorCenter,colorEdges){
51+
colorCenter=GLUtil.toGLColor(colorCenter);
52+
colorEdges=GLUtil.toGLColor(colorEdges);
53+
var gradient=[]
54+
var arr=[];
55+
var size=32;
56+
for(var i=0;i<32;i++){
57+
arr.push(
58+
GLMath.vec4scaleInPlace(GLMath.vec4lerp(colorCenter,colorEdges,i/31),255)
59+
)
60+
}
61+
var i=0
62+
var radius=(size-1)*0.5;
63+
var recipradius=1.0/radius;
64+
for (var y=0;y<size;y++){
65+
for (var x=0;x<size;x++,i+=4){
66+
var yy=(y-radius)*recipradius;
67+
var xx=(x-radius)*recipradius;
68+
var a=arr[Math.min(31,Math.floor(31 * Math.sqrt(xx * xx + yy * yy)))];
69+
gradient[i]=Math.round(a[0])
70+
gradient[i+1]=Math.round(a[1])
71+
gradient[i+2]=Math.round(a[2])
72+
gradient[i+3]=Math.round(a[3])
73+
}
74+
}
75+
return Texture.fromUint8Array(
76+
new Uint8Array(gradient),size,size).setClamp(true);
77+
}
78+
79+
function link1(){
80+
shape.setTexture(horizontalGradient("red","blue"))
81+
}
82+
function link2(){
83+
shape.setTexture(horizontalGradient("green","yellow"))
84+
}
85+
function link3(){
86+
shape.setTexture(radialGradient("red","blue"))
87+
}
88+
function link4(){
89+
shape.setTexture(radialGradient("green","yellow"))
90+
}
91+
4592
// Create the 3D scene; find the HTML canvas and pass it
4693
// to Scene3D.
4794
var scene=new Scene3D(document.getElementById("canvas"));

objmtl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ ObjData._loadObj=function(str){
401401
vertexKind=-1;
402402
lastPrimitiveSeen=-1;
403403
haveNormals=false;
404-
mesh=new Mesh();
404+
mesh=new Mesh();
405405
}
406406
mesh.mode(prim==Mesh.TRIANGLES ?
407407
Mesh.TRIANGLE_FAN :

0 commit comments

Comments
 (0)