error: WebGL warning: texImage2D: Desired upload requires more data than is available: (when loading the texture with triangle mesh data and normals)

error: WebGL warning: texImage2D: Desired upload requires more data than is available: (when loading the texture with triangle mesh data and normals)

我可能在加载 2 个纹理时搞砸了。我收到此错误:"WebGL warning: texImage2D: Desired upload requires more data than is available: (0 rows plus 246 pixels needed, 0 rows plus 244 pixels available)"

屏幕上什么也没有。我有 2 个纹理,一个带有网格的顶点,另一个带有法线。我将 post 我尝试加载纹理的方式:

const uLSr = gl.getUniformLocation(P, 'uMeshData');
const uNormData = gl.getUniformLocation(P, 'uNormData');

const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);

 const verts = [
         ...
         4.910892,0.000000,4.910892,
        -4.910892,0.000000,-4.910892,
        -4.910892,0.000000,4.910892,
        4.910892,0.000000,4.910892,
        4.910892,0.000000,-4.910892,
        ...
];

const vertsNorm = [
        ...
        0.0000,-1.0000,0.0000,
        0.4253,-0.8506,0.3090,
        -0.1625,-0.8506,0.5000,
        0.7236,-0.4472,0.5257,
        0.4253,-0.8506,0.3090,
        ...
];

const meshVerts = new Float32Array(verts);
    const vertsLenght = meshVerts.length / 3;
    gl.uniform1i(uLvertices, vertsLenght);

gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, vertsLenght, 1, 0, gl.RGB, gl.FLOAT, meshVerts);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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);


const textureNorm = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, textureNorm);

const meshNorm = new Float32Array(vertsNorm);
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
//vertsLength is the same cause normal and vertices have the same length
//foreachone normal there is one single vertex
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, vertsLenght, 1, 0, gl.RGB, gl.FLOAT, meshNorm);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, textureNorm);
gl.uniform1i(uLSr, 0);
gl.uniform1i(uNormData, 1);

然后在着色器中我尝试像这样解压它们:

for (int i = 6; i < vertsCount; i += 3) {

    a = texelFetch(uMeshData, ivec2(i, 0), 0);
    b = texelFetchOffset(uMeshData, ivec2(i, 0), 0, ivec2(1, 0));
    c = texelFetchOffset(uMeshData, ivec2(i, 0), 0, ivec2(2, 0));

    aN = texelFetch(uNormData, ivec2(i, 0), 0);
    bN = texelFetchOffset(uNormData, ivec2(i, 0), 0, ivec2(1, 0));
    cN = texelFetchOffset(uNormData, ivec2(i, 0), 0, ivec2(2, 0));

    triangleNormal = (aN.xyz + bN.xyz + cN.xyz) / 3.;

    vec3 uvt;
    vec3 intersect;
    float z;
    bool isHit = hitTriangleSecond(R_.orig, R_.dir, a.xyz, b.xyz, c.xyz, uvt, triangleNormal, intersect, z);;
    if (isHit) {

        if (z<mindist && z > 0.001) {
            hitPos1 = intersect;

            mindist = z;
            weHitSomething = true;
            material.type = DIEL;
            material.albedo = vec3(.8, .3, .4);
            normal = triangleNormal;
            hitPos = hitPos1;            
        }
    }      
} 

如果我评论我计算表面法线的那一行,然后用我从纹理中得到的法线替换它,我得到一个白页。所以我假设我在加载纹理时出了点问题。还是在 HitTriandglSecond 函数中计算三角形的面法线?

及以下我 post link 到 jsfiddle 的一个最小示例(它很长,因为我把一个平面的顶点和法线和一个三角形的球体): link 的 jsfiddle 示例

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, vertsLenght, 1, 0, gl.RGB, gl.FLOAT, meshNorm);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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);

我发现了错误。

这是 vertsLength 变量,我为第二个纹理创建了一个新变量,因为由于某些原因重用 vertsLength 似乎不起作用。

const normLength = meshNorm.length /3;
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, normLength, 1, 0, gl.RGB, gl.FLOAT, meshNorm);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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);