1

我正在处理 WebGL 中的现有项目,纹理有问题,我在为正确的对象设置正确的纹理时遇到了一些麻烦,在 khronos.org 论坛上,我有一些屏幕截图和发生的代码。

http://www.khronos.org/message_boards/viewtopic.php?f=43&t=5203

目前我正在创建一个小盒子,只是为了了解一切是如何运作的,但它从墙壁和地板上获取纹理。

http://tinypic.com/r/23mr1j8/6

先感谢您。

这是“handleloadedtexture”

function handleLoadedTexture(texture, image) {
    gl.bindTexture(gl.TEXTURE_2D, texture);
    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,
            texture.image);

    if (isPowerOfTwo(texture.image.width) && isPowerOfTwo(texture.image.height)) {
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    } else {
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
        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.bindTexture(gl.TEXTURE_2D, null);

    texturesToLoad--;
}

所有纹理都已加载,如果我在这部分更改某些内容,我可以在它们之间切换:

  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, texture0);
  gl.uniform1i(shaderProgram.texture0Uniform, 0);


  gl.activeTexture(gl.TEXTURE0 + 1);
  gl.bindTexture(gl.TEXTURE_2D, texture1);
  gl.uniform1i(shaderProgram.texture1Uniform, 1);

  gl.activeTexture(gl.TEXTURE0 + 2);
  gl.bindTexture(gl.TEXTURE_2D, texture2);
  gl.uniform1i(shaderProgram.texture2Uniform, 2);

  gl.activeTexture(gl.TEXTURE0 + 3);
  gl.bindTexture(gl.TEXTURE_2D, texture3);
  gl.uniform1i(shaderProgram.texture3Uniform, 3);

我的着色器:fs 看起来像这样:

 <script id="shader-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif

varying vec2 vTextureCoord;
varying float vTextureID;
varying vec4 vColor;

uniform sampler2D uTexture0;
uniform sampler2D uTexture1;
uniform sampler2D uTexture2;

void main(void) {
    vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
    if (vTextureID < 0.1)
        gl_FragColor = texture2D(uTexture0, vTextureCoord.st);
    else if (vTextureID < 1.1)
        gl_FragColor = texture2D(uTexture1, vTextureCoord.st);
    else if (vTextureID < 2.1)
        gl_FragColor = texture2D(uTexture2, vTextureCoord.st);
    else if (vTextureID < 3.1)
        gl_FragColor = texture2D(uTexture3, vTextureCoord.st); 

你能看出它有什么问题吗?

4

1 回答 1

0
uniform sampler2D uTexture0;
uniform sampler2D uTexture1;
uniform sampler2D uTexture2;

再添加一行:

“统一 sampler2d uTexture3;”

对于声明

"gl_FragColor = texture2D(uTexture3, vTextureCoord.st);"

不过也可能有别的东西……

于 2013-08-03T15:31:07.757 回答