Skip to content

Commit 0ef5b2c

Browse files
committed
颜色与纹理
多幅纹理
1 parent 9de585b commit 0ef5b2c

File tree

5 files changed

+52
-19
lines changed

5 files changed

+52
-19
lines changed

12.Texture/18.00.08.png

453 KB
Loading

12.Texture/18.02.35.png

50.3 KB
Loading

12.Texture/camel.png

42.1 KB
Loading

12.Texture/f-texture.png

13.3 KB
Loading

12.Texture/main.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ const FSHADER_SOURCE =
1111
'precision mediump float;\n'+
1212
// 'uniform float u_Width;\n'+
1313
// 'uniform float u_Height;\n' +
14-
'uniform sampler2D u_Sampler;\n' +
14+
'uniform sampler2D u_Sampler1;\n' +
15+
'uniform sampler2D u_Sampler2;\n' +
1516
'varying vec2 v_TexCoord;\n' +
1617
'void main(){' +
17-
'gl_FragColor = texture2D(u_Sampler, v_TexCoord);\n' +
18+
'vec4 color1 = texture2D(u_Sampler1, v_TexCoord);\n' +
19+
'vec4 color2 = texture2D(u_Sampler2, v_TexCoord);\n' +
20+
'gl_FragColor = color1 * color2;\n' +
1821
'}';
1922

2023
// import textture from '../images/textture.jpeg'/
@@ -87,46 +90,76 @@ function initVertexBuffers(gl) {
8790
}
8891

8992
function initTextures(gl, n) {
90-
const texture = gl.createTexture() // 创建纹理对象
93+
const texture1 = gl.createTexture() // 创建纹理对象
94+
const texture2 = gl.createTexture()
9195

92-
const u_Sampler = gl.getUniformLocation(gl.program, 'u_Sampler') // 获取u_Sampler的存储位置
96+
const u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1') // 获取u_Sampler的存储位置
97+
const u_Sampler2 = gl.getUniformLocation(gl.program, 'u_Sampler2')
9398

94-
const image = new Image() // 创建一个image对象
95-
96-
image.onload = function () {
97-
loadTexture(gl, n, texture, u_Sampler, image);
99+
const image1 = new Image() // 创建一个image对象
100+
const image2 = new Image()
101+
image1.onload = function () {
102+
loadTexture(gl, n, texture1, u_Sampler1, image1, 1);
98103
console.log(333)
99104
}
105+
image2.onload = function () {
106+
loadTexture(gl, n ,texture2, u_Sampler2, image2, 2)
107+
}
100108

101-
image.onerror = function() {
102-
console.error('Failed to load image at ' + image.src);
109+
image1.onerror = function() {
110+
console.error('Failed to load image at ' + image1.src);
103111
};
104-
image.src = 'https://webglfundamentals.org/webgl/resources/f-texture.png';
112+
image1.src = './camel.png';
105113
// image.src='http://127.0.0.1:5501/images/vshaderAndFshader.png'
106-
image.setAttribute("crossOrigin", "Anonymous");
114+
image1.setAttribute("crossOrigin", "Anonymous");
115+
116+
117+
118+
119+
image2.src = './camel.png'
120+
121+
image2.setAttribute("crossOrigin", "Anonymous");
122+
107123
return true;
108124
}
109125

110-
function loadTexture(gl, n, texture, u_Sampler, image) {
126+
var g_texUnit1 = false, g_texUnit2 = false
127+
128+
function loadTexture(gl, n, texture, u_Sampler, image, texUnit) {
111129
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1) // 对纹理图像进行y轴反转
112130

113-
gl.activeTexture(gl.TEXTURE0)
131+
if(texUnit == 1) {
132+
gl.activeTexture(gl.TEXTURE1)
133+
g_texUnit1 = true
134+
}else {
135+
gl.activeTexture(gl.TEXTURE2)
136+
g_texUnit2 = true
137+
}
138+
139+
// gl.activeTexture(gl.TEXTURE0)
114140

115141
gl.bindTexture(gl.TEXTURE_2D, texture)
116142

117143
// 配置纹理参数
118-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
144+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
119145

120146
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image)
121147

122-
gl.uniform1i(u_Sampler, 0)
148+
gl.uniform1i(u_Sampler, texUnit)
123149

124150
console.log('Texture loaded:', image.width, image.height);
125151

126152

153+
127154
// gl.drawArrays(gl.TRIANGLES, 0, n)
128-
gl.clearColor(0.0, 0.0, 0.0, 1.0)
129155

130-
gl.clear(gl.COLOR_BUFFER_BIT)
131-
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n)
156+
// gl.drawArrays(gl.TRIANGLE_STRIP, 0, n)
157+
158+
if(g_texUnit1 && g_texUnit2) {
159+
gl.clearColor(0.0, 0.0, 0.0, 1.0)
160+
161+
gl.clear(gl.COLOR_BUFFER_BIT)
162+
console.log(4)
163+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n)
164+
}
132165
}

0 commit comments

Comments
 (0)