- Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathwebgl-3d-textures-mips.html
312 lines (259 loc) · 10.4 KB
/
webgl-3d-textures-mips.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!-- Licensed under a BSD license. See license.html for license -->
<!DOCTYPE html>
<html>
<head>
<metacharset="utf-8">
<metaname="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>WebGL - Textures - Mips for stability</title>
<linktype="text/css" href="resources/webgl-tutorials.css" rel="stylesheet" />
</head>
<body>
<divclass="description">
Show how mips are important for stability and color<br/>
</div>
<canvasid="canvas"></canvas>
</body>
<!-- vertex shader -->
<scriptid="vertex-shader-3d" type="x-shader/x-vertex">
attributevec4a_position;
attributevec2a_texcoord;
uniformmat4u_matrix;
varyingvec2v_texcoord;
voidmain(){
// Multiply the position by the matrix.
gl_Position=u_matrix*a_position;
// Pass the texcoord to the fragment shader.
v_texcoord=a_texcoord;
}
</script>
<!-- fragment shader -->
<scriptid="fragment-shader-3d" type="x-shader/x-fragment">
precisionmediumpfloat;
// Passed in from the vertex shader.
varyingvec2v_texcoord;
// The texture.
uniformsampler2Du_texture;
voidmain(){
gl_FragColor=texture2D(u_texture,v_texcoord);
}
</script>
<!--
for most samples webgl-utils only provides shader compiling/linking and
canvas resizing because why clutter the examples with code that's the same in every sample.
See https://webglfundamentals.org/webgl/lessons/webgl-boilerplate.html
and https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
for webgl-utils, m3, m4, and webgl-lessons-ui.
-->
<scriptsrc="resources/webgl-utils.js"></script>
<scriptsrc="resources/m4.js"></script>
<script>
"use strict";
functionmain(){
// Get A WebGL context
varcanvas=document.querySelector("#canvas");
vargl=canvas.getContext("webgl",{antialias: false});
if(!gl){
return;
}
// setup GLSL program
varprogram=webglUtils.createProgramFromScripts(gl,["vertex-shader-3d","fragment-shader-3d"]);
// look up where the vertex data needs to go.
varpositionLocation=gl.getAttribLocation(program,"a_position");
vartexcoordLocation=gl.getAttribLocation(program,"a_texcoord");
// lookup uniforms
varmatrixLocation=gl.getUniformLocation(program,"u_matrix");
vartextureLocation=gl.getUniformLocation(program,"u_texture");
// Create a buffer for positions
varpositionBuffer=gl.createBuffer();
// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Put the positions in the buffer
setGeometry(gl);
// provide texture coordinates for the rectangle.
vartexcoordBuffer=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
// Set Texcoords.
setTexcoords(gl);
varallocateFBTexture=true;
varframebufferWidth;// set at render time
varframebufferHeight;// set at render time
varframebuffer=gl.createFramebuffer();
varfbTexture=gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D,fbTexture);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
gl.bindFramebuffer(gl.FRAMEBUFFER,framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,fbTexture,0);
// Create a texture.
vartexture=gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D,texture);
// Fill the texture with a 1x1 blue pixel.
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,1,1,0,gl.RGBA,gl.UNSIGNED_BYTE,
newUint8Array([0,0,255,255]));
// Asynchronously load an image
varimage=newImage();
image.src="resources/mip-low-res-example.png";
image.addEventListener('load',function(){
// Now that the image has loaded make copy it to the texture.
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,image);
// Check if the image is a power of 2 in both dimensions.
if(isPowerOf2(image.width)&&isPowerOf2(image.height)){
// Yes, it's a power of 2. Generate mips.
gl.generateMipmap(gl.TEXTURE_2D);
}else{
// No, it's not a power of 2. Turn of mips and set wrapping to clamp to edge
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.LINEAR);
}
});
functionisPowerOf2(value){
return(value&(value-1))===0;
}
functionradToDeg(r){
returnr*180/Math.PI;
}
functiondegToRad(d){
returnd*Math.PI/180;
}
varfieldOfViewRadians=degToRad(60);
varmodelXRotationRadians=degToRad(0);
varmodelYRotationRadians=degToRad(0);
requestAnimationFrame(drawScene);
// Draw the scene.
functiondrawScene(time){
time*=0.001;// convert to seconds
if(webglUtils.resizeCanvasToDisplaySize(gl.canvas,window.devicePixelRatio)||allocateFBTexture){
allocateFBTexture=false;
framebufferWidth=gl.canvas.clientWidth/4;
framebufferHeight=gl.canvas.clientHeight/4;
gl.bindTexture(gl.TEXTURE_2D,fbTexture);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,framebufferWidth,framebufferHeight,
0,gl.RGBA,gl.UNSIGNED_BYTE,null);
}
gl.bindFramebuffer(gl.FRAMEBUFFER,framebuffer);
gl.viewport(0,0,framebufferWidth,framebufferHeight);
// Clear the framebuffer texture.
gl.clearColor(0,0,0,1);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
// Tell it to use our program (pair of shaders)
gl.useProgram(program);
// Turn on the position attribute
gl.enableVertexAttribArray(positionLocation);
// Bind the position buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,positionBuffer);
// Tell the position attribute how to get data out of positionBuffer (ARRAY_BUFFER)
varsize=3;// 3 components per iteration
vartype=gl.FLOAT;// the data is 32bit floats
varnormalize=false;// don't normalize the data
varstride=0;// 0 = move forward size * sizeof(type) each iteration to get the next position
varoffset=0;// start at the beginning of the buffer
gl.vertexAttribPointer(
positionLocation,size,type,normalize,stride,offset);
// Turn on the texcoord attribute
gl.enableVertexAttribArray(texcoordLocation);
// bind the texcoord buffer.
gl.bindBuffer(gl.ARRAY_BUFFER,texcoordBuffer);
// Tell the texcoord attribute how to get data out of texcoordBuffer (ARRAY_BUFFER)
varsize=2;// 2 components per iteration
vartype=gl.FLOAT;// the data is 32bit floats
varnormalize=false;// don't normalize the data
varstride=0;// 0 = move forward size * sizeof(type) each iteration to get the next position
varoffset=0;// start at the beginning of the buffer
gl.vertexAttribPointer(
texcoordLocation,size,type,normalize,stride,offset);
// Compute the projection matrix
varaspect=gl.canvas.clientWidth/gl.canvas.clientHeight;
varzNear=1;
varzFar=2000;
varprojectionMatrix=
m4.perspective(fieldOfViewRadians,aspect,zNear,zFar);
varcameraPosition=[0,0,3];
varup=[0,1,0];
vartarget=[0,0,0];
// Compute the camera's matrix using look at.
varcameraMatrix=m4.lookAt(cameraPosition,target,up);
// Make a view matrix from the camera matrix.
varviewMatrix=m4.inverse(cameraMatrix);
varsettings=[
{x: -1,y: -3,z: -30,filter: gl.NEAREST,},
{x: 0,y: -3,z: -30,filter: gl.LINEAR,},
{x: 1,y: -3,z: -30,filter: gl.NEAREST_MIPMAP_LINEAR,},
{x: -1,y: -1,z: -10,filter: gl.NEAREST,},
{x: 0,y: -1,z: -10,filter: gl.LINEAR,},
{x: 1,y: -1,z: -10,filter: gl.NEAREST_MIPMAP_LINEAR,},
{x: -1,y: 1,z: 0,filter: gl.NEAREST,},
{x: 0,y: 1,z: 0,filter: gl.LINEAR,},
{x: 1,y: 1,z: 0,filter: gl.LINEAR_MIPMAP_NEAREST,},
];
varxSpacing=1.2;
varySpacing=0.7;
varzDistance=30;
settings.forEach(function(s){
varz=-5+s.z;// Math.cos(time * 0.3) * zDistance - zDistance;
varr=Math.abs(z)*Math.sin(fieldOfViewRadians*0.5);
varx=Math.sin(time*0.2)*r;
vary=Math.cos(time*0.2)*r*0.5;
varr2=1+r*0.2;
gl.bindTexture(gl.TEXTURE_2D,texture);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,s.filter);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.LINEAR);
varmatrix=m4.translate(projectionMatrix,x+s.x*xSpacing*r2,y+s.y*ySpacing*r2,z);
matrix=m4.xRotate(matrix,modelXRotationRadians);
matrix=m4.yRotate(matrix,modelYRotationRadians);
// Set the matrix.
gl.uniformMatrix4fv(matrixLocation,false,matrix);
// Tell the shader to use texture unit 0 for u_texture
gl.uniform1i(textureLocation,0);
// Draw the geometry.
gl.drawArrays(gl.TRIANGLES,0,1*6);
});
gl.bindFramebuffer(gl.FRAMEBUFFER,null);
gl.viewport(0,0,gl.canvas.width,gl.canvas.height);
gl.bindTexture(gl.TEXTURE_2D,fbTexture);
gl.uniformMatrix4fv(matrixLocation,false,
[2,0,0,0,
0,2,0,0,
0,0,1,0,
0,0,0,1]);
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES,0,1*6);
requestAnimationFrame(drawScene);
}
}
// Fill the buffer with the values that define a plane.
functionsetGeometry(gl){
varpositions=newFloat32Array(
[
-0.5,-0.5,0.5,
0.5,-0.5,0.5,
-0.5,0.5,0.5,
-0.5,0.5,0.5,
0.5,-0.5,0.5,
0.5,0.5,0.5,
]);
gl.bufferData(gl.ARRAY_BUFFER,positions,gl.STATIC_DRAW);
}
// Fill the buffer with texture coordinates for a plane.
functionsetTexcoords(gl){
gl.bufferData(
gl.ARRAY_BUFFER,
newFloat32Array(
[
0,0,
1,0,
0,1,
0,1,
1,0,
1,1,
]),
gl.STATIC_DRAW);
}
main();
</script>
</html>