当前位置: 代码迷 >> JavaScript >> 错误:WebGL警告:texImage2D:所需的上载需要的数据比可用的更多:(当使用三角形网格数据和法线加载纹理时)
  详细解决方案

错误:WebGL警告:texImage2D:所需的上载需要的数据比可用的更多:(当使用三角形网格数据和法线加载纹理时)

热度:68   发布时间:2023-06-13 11:32:30.0

加载2个纹理时,我可能已经搞砸了。 我收到此错误:“ WebGL警告:texImage2D:所需的上载需要的数据多于可用数据:(0行加246像素,0行加244像素可用)”

屏幕上没有任何内容。 我有2个纹理,其中一个具有网格的顶点,另一个具有法线。 我将发布尝试加载纹理的方式:

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);

然后在draw函数中我这样称呼他们:

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函数中计算三角形的面法线?

在下面,我以一个最小的示例发布了jsfiddle的链接(长期以来,我放置了一个平面和一个三角形球体的顶点和法线):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);
  相关解决方案