1

我正在我的顶点着色器中进行 GPU 蒙皮,它在 PC 上运行良好,并且我正在移植到 Android。我的顶点着色器在下面,问题是matTransform矩阵的创建似乎只使用了boneMatrices中的第一个矩阵:

#version 300 es

precision highp float;
precision highp int;

//Uniform count: projectionMatrix(16) + modelViewMatrix(16) + MVPMatrix(16) + textureMatrix(16) + normalMatrix(9) + lightMVPMatrices(16*5) + nShadowLights(1) + boneMatrices(16*boneMax)  = 73 + 1 + 16*shadowLightMax + 16*boneMax = (out of ~1024 components)
//GLSL ES (vectors): projectionMatrix(4) + modelViewMatrix(4) + MVPMatrix(4) + textureMatrix(4) + normalMatrix(3) + lightMVPMatrices(4*5) + nShadowLights(1) + boneMatrices(4*boneMax) = 19 + 4*shadowLightMax + 4*boneMax = 239 out of 256 vectors on Nexus 5 (shadowLightMax = 5, boneMax = 50, 17 vec4s remain, or 4 matrices and 1 vec4)
//Matrices
//uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 MVPMatrix;
uniform mat4 textureMatrix;
uniform mat3 normalMatrix;
uniform mat4 lightMVPMatrices[5];
uniform int nShadowLights;

//Bones
uniform mat4 boneMatrices[50];

//Vertex information
in vec3 position;
in vec4 colour;
in vec2 texCoord;
in vec3 normal;
in vec3 boneWeights;
in vec4 boneIndices;

out vec4 _colour;
out vec2 _texCoord;
out vec3 _normal;
out vec3 _eyePos;
out vec4 _lightPos[5];

void main(void)
{
    vec4 positionSkinned;
    vec4 normalSkinned;

    mat4 matTransform = boneMatrices[int(boneIndices[0])] * boneWeights[0];
    matTransform += boneMatrices[int(boneIndices[1])] * boneWeights[1];
    matTransform += boneMatrices[int(boneIndices[2])] * boneWeights[2];
    float finalWeight = 1.0 - (boneWeights[0] + boneWeights[1] + boneWeights[2]);
    matTransform += boneMatrices[int(boneIndices[3])] * finalWeight;

    positionSkinned = matTransform * vec4(position, 1.0);
    //positionSkinned.w = 1.0;
    normalSkinned = matTransform * vec4(normal, 0.0);

    gl_Position = MVPMatrix * positionSkinned;
    _colour = colour;
    _texCoord = (textureMatrix * vec4(texCoord, 0.0, 1.0)).xy;
    _normal = normalize(normalMatrix * normalize(normalSkinned.xyz));
    _eyePos = (modelViewMatrix * positionSkinned).xyz;
    for(int i = 0; i < nShadowLights; i++)
        _lightPos[i] = lightMVPMatrices[i] * positionSkinned;
}

我已经证实:

1) 正确的矩阵被推入 boneMatrices
2) 正确的骨骼索引存在于 boneIndices
3) 正确的 boneWeights 存在于 boneWeights
4) 使用点符号(.x、.y、.z 和 .w)访问 boneIndices 的组件不会t 做出不同
5) 根本没有 OpenGL 错误,因为我在每次调用后检查错误,并且统一大小不是问题(如果我将 boneMatrices 增加 5 个额外的矩阵,每次我都会得到无效的操作错误将矩阵推送到着色器,但在这个大小和更低的情况下很好)

我通过执行以下操作检查了第 1、2 和 3 点(boneMatrices、boneIndices 和 boneWeights 是正确的):

1) 使用仅修改少数骨骼的特定动画(例如boneMatrix[6]),然后对boneMatrix[6] 进行硬编码并验证所有顶点都被这个单一矩阵正确修改,在PC 和Android 上具有相同的结果

2) 通过在顶点着色器中执行以下操作来绘制 boneIndices:

_colour = vec4(boneIndices[0], boneIndices[1], boneIndices[2], boneIndices[3]);

以及片段着色器中的以下内容:

gl_FragColor = _colour

在 PC 和 Android 上具有相同的颜色

3) 执行与上述相同的操作,但将 _colour 设置为:

_colour = vec4(boneWeights[0], boneWeights[1], boneWeights[2], finalWeight);

我不知道还有什么可以尝试的,而且肯定似乎只使用了第一个矩阵,并且由于某种原因,int(boneIndices[x]) 对任何 x 都会导致 0。这是在带有 OpenGL ES 3.0 的 Nexus 5 上。帮助!

编辑:不幸的是,采用 Andon 的建议使用 ivec4 而不是 vec4 的 boneIndices 会导致相同的结果,但至少这清楚地表明这不是浮动的铸造问题。现在我觉得自己像个没有任何线索的警察:/

4

1 回答 1

2

最后,这看起来是 Adreno 驱动程序的一个限制,它不支持索引没有常量整数的统一矩阵数组,这与 OpenGL ES 规范中的强制要求相反。然而,一种解决方法是只使用统一的 vec4 数组,因为它似乎支持用变量索引这些数组(就像在他们的 SDK 中所做的那样)。

于 2013-12-09T22:57:44.257 回答