Skip to content

Commit a3fa79d

Browse files
committed
typos
1 parent 9ea2645 commit a3fa79d

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

webgl/lessons/webgl-qna-how-to-use-texture--and-color-also-in-webgl-.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,65 @@ TOC: How to use texture, and color also in WebGL?
66

77
I'm learning WebGL, and I would like to make a program, where there are colored, and textured objects also. I tried to modify a bit the fragment shader: If the object don't have texture, then it will be colored.
88

9-
precision mediump float;
9+
```glsl
10+
precision mediump float;
1011
11-
varying vec4 vColor;
12-
varying vec2 vTextureCoord;
13-
14-
uniform sampler2D uSampler;
12+
varying vec4 vColor;
13+
varying vec2 vTextureCoord;
1514
16-
void main(void) {
17-
gl_FragColor = vColor;
18-
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
19-
}
15+
uniform sampler2D uSampler;
16+
17+
void main(void) {
18+
gl_FragColor = vColor;
19+
gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
20+
}
21+
```
2022

2123
It doesn't works. I get an error: Could not initialize shaders.
2224

2325
## Answer:
2426

2527
This may or may not make sense but whether you have a texture or not requires different shaders. To get around this many people use a single pixel white texture when they don't want textures. That way they only need 1 shader to cover both cases. The shader might look something like this
2628

27-
uniform vec4 u_color;
28-
uniform sampler2D u_texture;
29+
```glsl
30+
uniform vec4 u_color;
31+
uniform sampler2D u_texture;
2932
30-
varying vec2 v_texCoord;
33+
varying vec2 v_texCoord;
3134
32-
void main(void) {
33-
gl_FragColor = texture2D(u_texture, v_texCoord) * u_color;
34-
}
35+
void main(void) {
36+
gl_FragColor = texture2D(u_texture, v_texCoord) * u_color;
37+
}
38+
```
3539

3640
Now, in your code you can draw textured like this
3741

38-
// at init time.
39-
var whiteColor = new Float32Array([1, 1, 1, 1]);
42+
```js
43+
// at init time.
44+
var whiteColor = new Float32Array([1, 1, 1, 1]);
4045

41-
// at draw time
42-
gl.uniform4fv(u_colorLocation, whiteColor); // use white color
43-
gl.bindTexture(gl.TEXTURE_2D, someTexture); // and some texture
44-
... draw ...
46+
// at draw time
47+
gl.uniform4fv(u_colorLocation, whiteColor); // use white color
48+
gl.bindTexture(gl.TEXTURE_2D, someTexture); // and some texture
49+
... draw ...
50+
```
4551

4652
And to draw un-textured you do this
4753

48-
// at init time.
49-
var whiteTexture = gl.createTexture();
50-
gl.bindTexture(gl.TEXTURE_2D, whiteTexture);
51-
var whitePixel = new Uint8Array([255, 255, 255, 255]);
52-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0,
53-
gl.RGBA, gl.UNSIGNED_BYTE, whitePixel);
54-
55-
56-
// at draw time
57-
gl.uniform4fv(u_colorLocation, someColor); // use the color I want
58-
gl.bindTexture(gl.TEXTURE_2D, whiteTexture); // use the white texture
59-
... draw ...
54+
```js
55+
// at init time.
56+
var whiteTexture = gl.createTexture();
57+
gl.bindTexture(gl.TEXTURE_2D, whiteTexture);
58+
var whitePixel = new Uint8Array([255, 255, 255, 255]);
59+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0,
60+
gl.RGBA, gl.UNSIGNED_BYTE, whitePixel);
61+
62+
63+
// at draw time
64+
gl.uniform4fv(u_colorLocation, someColor); // use the color I want
65+
gl.bindTexture(gl.TEXTURE_2D, whiteTexture); // use the white texture
66+
... draw ...
67+
```
6068

6169
That also gives you the ability to tint your textures by using a non white color and a texture together.
6270

0 commit comments

Comments
 (0)