本系列僅作為本人學(xué)習(xí)《WebGL編程指南》這本書(shū)的筆記所用
紋理圖像的加載是異步的惜辑,無(wú)法確定哪個(gè)圖像先加載完成,所以使用g_texUnit0和g_texUnit1來(lái)判斷是否加載完成,加載完成后才開(kāi)始繪圖
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>20矩形貼兩張圖片-使用多幅紋理</title>
<script src="./lib/cuon-matrix.js"></script>
<script src="./lib/cuon-utils.js"></script>
<script src="./lib/webgl-debug.js"></script>
<script src="./lib/webgl-utils.js"></script>
</head>
<body onload="main()">
<canvas id="webgl" width="400" height="400">
不支持canvas的瀏覽器會(huì)展示這段文字
</canvas>
<script>
// 頂點(diǎn)著色器程序
let VSHADER_SOURCE = `
attribute vec4 a_Position;
attribute vec2 a_TexCoord;
varying vec2 v_TexCoord;
void main() {
gl_Position = a_Position;
v_TexCoord = a_TexCoord;
}
`;
// ============================??
// 片元著色器程序
let FSHADER_SOURCE = `
precision mediump float; // 精度限定詞-來(lái)指定變量的范圍和精度指巡,這里是中等精度
uniform sampler2D u_Sampler0;
uniform sampler2D u_Sampler1;
varying vec2 v_TexCoord;
void main() {
vec4 color0 = texture2D(u_Sampler0, v_TexCoord); // 一張圖
vec4 color1 = texture2D(u_Sampler1, v_TexCoord); // 兩張圖
gl_FragColor = color0 * color1; // 兩張圖合并
}
`;
// ==============================??
// 主程序
function main() {
let canvas = document.getElementById("webgl");
// 獲取WebGL繪圖上下文
let gl = getWebGLContext(canvas);
if (!gl) {
console.error("無(wú)法使用WebGL");
return;
}
// 初始化著色器
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.error("無(wú)法使用著色器");
return;
}
// 設(shè)置頂點(diǎn)坐標(biāo)
let n = initVertexBuffers(gl);
if (n < 0) {
console.log("無(wú)法設(shè)置點(diǎn)的位置");
return;
}
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// 配置紋理
if (!initTextures(gl, n)) {
console.log('無(wú)法配置紋理');
return;
}
}
function initVertexBuffers(gl) {
// 定義頂點(diǎn)坐標(biāo)和紋理坐標(biāo)
let verticesTexCoords = new Float32Array([
-0.5, 0.5, 0.0, 1.0,
-0.5, -0.5, 0.0, 0.0,
0.5, 0.5, 1.0, 1.0,
0.5, -0.5, 1.0, 0.0,
]);
let n = 4; // 頂點(diǎn)數(shù)量
// 1.創(chuàng)建緩沖區(qū)對(duì)象
let vertexTexCoordBuffer = gl.createBuffer();
if (!vertexTexCoordBuffer) {
console.log("不能創(chuàng)建緩沖區(qū)對(duì)象");
return -1;
}
// 2.將緩沖區(qū)對(duì)象綁定到目標(biāo)
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexCoordBuffer);
// 3.向緩沖區(qū)對(duì)象寫(xiě)入數(shù)據(jù),不能直接向緩沖區(qū)寫(xiě)入數(shù)據(jù)质和,只能向綁定的目標(biāo)輸入數(shù)據(jù)
gl.bufferData(gl.ARRAY_BUFFER, verticesTexCoords, gl.STATIC_DRAW);
let FSIZE = verticesTexCoords.BYTES_PER_ELEMENT;
// 獲取a_Position、a_TexCoord變量的存儲(chǔ)位置棱诱,分配緩沖區(qū)并開(kāi)啟
let a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log("無(wú)法獲取 a_Position");
return -1;
}
// 參數(shù)1:類型泼橘。 參數(shù)2:占幾個(gè)元素。 參數(shù)3:?jiǎn)挝活愋汀?參數(shù)4:固定军俊。 參數(shù)5:5個(gè)元素代表1個(gè)點(diǎn)侥加。 參數(shù)6:間隔2個(gè)元素開(kāi)始算
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, FSIZE * 4, 0);
gl.enableVertexAttribArray(a_Position);
let a_TexCoord = gl.getAttribLocation(gl.program, 'a_TexCoord');
if (a_TexCoord < 0) {
console.log('無(wú)法獲取 a_TexCoord');
return -1;
}
gl.vertexAttribPointer(a_TexCoord, 2, gl.FLOAT, false, FSIZE * 4, FSIZE * 2);
gl.enableVertexAttribArray(a_TexCoord);
return n;
}
// ==============================??
// 兩個(gè)紋理
function initTextures(gl, n) {
// 創(chuàng)建兩個(gè)紋理對(duì)象
let texture0 = gl.createTexture();
let texture1 = gl.createTexture();
if (!texture0 || !texture1) {
console.log('無(wú)法創(chuàng)建紋理對(duì)象');
return false;
}
// 獲取u_Sampler0和u_Sampler1的存儲(chǔ)位置
let u_Sampler0 = gl.getUniformLocation(gl.program, 'u_Sampler0');
let u_Sampler1 = gl.getUniformLocation(gl.program, 'u_Sampler1');
if (!u_Sampler0 || !u_Sampler1) {
console.log('無(wú)法獲取u_Sampler0或u_Sampler1');
return false;
}
// 創(chuàng)建兩個(gè)image對(duì)象
let image0 = new Image();
let image1 = new Image();
if (!image0 || !image1) {
console.log('無(wú)法創(chuàng)建image對(duì)象');
return false;
}
// 注冊(cè)圖像加載事件的兩個(gè)響應(yīng)函數(shù)
image0.onload = function(){ loadTexture(gl, n, texture0, u_Sampler0, image0, 0); };
image1.onload = function(){ loadTexture(gl, n, texture1, u_Sampler1, image1, 1); };
// 瀏覽器開(kāi)始加載兩個(gè)圖像
image0.src = './sky.jpg';
image1.src = './circle.gif';
return true;
}
// ==============================??
// 標(biāo)記紋理單元是否已經(jīng)就緒
let g_texUnit0 = false;
let g_texUnit1 = false;
// ==============================??
// ==============================??
// 響應(yīng)函數(shù)
function loadTexture(gl, n, texture, u_Sampler, image, texUnit) {
// 對(duì)紋理圖像進(jìn)行y軸反轉(zhuǎn)
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 1);
// 激活紋理
if (texUnit === 0) {
gl.activeTexture(gl.TEXTURE0);
g_texUnit0 = true;
} else {
gl.activeTexture(gl.TEXTURE1);
g_texUnit1 = true;
}
// 綁定紋理對(duì)象到目標(biāo)上
gl.bindTexture(gl.TEXTURE_2D, texture);
// 配置紋理參數(shù)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
// 設(shè)置紋理圖像
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// 將紋理單元編號(hào)傳遞給取樣器 uniform1i不是uniformli
gl.uniform1i(u_Sampler, texUnit);
gl.clear(gl.COLOR_BUFFER_BIT);
// 繪制一個(gè)矩形
if (g_texUnit0 && g_texUnit1) {
gl.drawArrays(gl.TRIANGLE_STRIP, 0, n);
}
}
// ==============================??
</script>
</body>
</html>