WebGL:如何绑定一组采样器 [英] WebGL: How to bind an array of samplers

查看:133
本文介绍了WebGL:如何绑定一组采样器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处所述,可以将所有需要的纹理""绑定到一个采样器阵列中着色器,然后使用顶点属性对其进行索引".我将如何进行装订?目前,我像这样绑定我的纹理(如果一开始是正确的;至少可以正常工作):

As mentioned here it would be possible to "bind all the textures you need to a sampler array in the shader and then index it with a vertex attribute". How would I do the binding? Currently I bind my textures like so (if that's correct in the first place; it works at least):

sampler[i] = gl.getUniformLocation(program, "u_sampler" + i);
...
for (var i = 0, len = textures.length; i < len; i++) {
    gl.activeTexture(gl.TEXTURE0 + i);
    gl.bindTexture(gl.TEXTURE_2D, textures[i]);
    gl.uniform1i(sampler[i], i);
}

现在要绑定一组采样器,我会扔掉activeTexturebindTexture并使用类似的东西吗?

To bind an array of samplers now would I throw away activeTexture and bindTexture and use something like this?

gl.uniform1iv(sampler, [0,...,len-1]);

推荐答案

没关系,我想我找到了解决方案.我的代码如下:

Never mind, I think I found the solution. My code looks as follows:

var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
gl.texParameteri(...
gl.bindTexture(gl.TEXTURE_2D, null);
textures[i] = texture;

...compose texture images...

gl.activeTexture(gl.TEXTURE0 + i);
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
gl.texSubImage2D(gl.TEXTURE_2D, 0, xoffset, yoffset, gl.RGBA, gl.UNSIGNED_BYTE, image);

...fill all textures...

var sampler = gl.getUniformLocation(program, "u_sampler");
var samplerArray = new Int32Array(textures.length);
var len = samplerArray.length;
while (len--) {
    samplerArray[len] = len;
}
gl.uniform1iv(sampler, samplerArray);

现在,我可以通过u_sampler[i]正确访问片段着色器中的采样器.

Now I can access the samplers in the fragment shader via u_sampler[i] correctly.

这篇关于WebGL:如何绑定一组采样器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆